2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
33 #include <isl_list_templ.c>
38 #include <isl_list_templ.c>
40 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
41 __isl_take isl_vec
*v
)
48 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
58 isl_local_space_free(ls
);
63 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
72 ctx
= isl_local_space_get_ctx(ls
);
73 if (!isl_local_space_divs_known(ls
))
74 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
76 if (!isl_local_space_is_set(ls
))
77 isl_die(ctx
, isl_error_invalid
,
78 "domain of affine expression should be a set",
81 total
= isl_local_space_dim(ls
, isl_dim_all
);
82 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
83 return isl_aff_alloc_vec(ls
, v
);
85 isl_local_space_free(ls
);
89 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
93 aff
= isl_aff_alloc(ls
);
97 isl_int_set_si(aff
->v
->el
[0], 1);
98 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
111 /* Return an affine expression defined on the specified domain
112 * that represents NaN.
114 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
118 aff
= isl_aff_alloc(ls
);
122 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
127 /* Return a piecewise affine expression defined on the specified domain
128 * that represents NaN.
130 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
132 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
135 /* Return an affine expression that is equal to "val" on
136 * domain local space "ls".
138 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
139 __isl_take isl_val
*val
)
145 if (!isl_val_is_rat(val
))
146 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
147 "expecting rational value", goto error
);
149 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
153 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
154 isl_int_set(aff
->v
->el
[1], val
->n
);
155 isl_int_set(aff
->v
->el
[0], val
->d
);
157 isl_local_space_free(ls
);
161 isl_local_space_free(ls
);
166 /* Return an affine expression that is equal to the specified dimension
169 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
170 enum isl_dim_type type
, unsigned pos
)
178 space
= isl_local_space_get_space(ls
);
181 if (isl_space_is_map(space
))
182 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
183 "expecting (parameter) set space", goto error
);
184 if (pos
>= isl_local_space_dim(ls
, type
))
185 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
186 "position out of bounds", goto error
);
188 isl_space_free(space
);
189 aff
= isl_aff_alloc(ls
);
193 pos
+= isl_local_space_offset(aff
->ls
, type
);
195 isl_int_set_si(aff
->v
->el
[0], 1);
196 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
197 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
201 isl_local_space_free(ls
);
202 isl_space_free(space
);
206 /* Return a piecewise affine expression that is equal to
207 * the specified dimension in "ls".
209 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
210 enum isl_dim_type type
, unsigned pos
)
212 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
215 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
224 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
229 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
230 isl_vec_copy(aff
->v
));
233 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
241 return isl_aff_dup(aff
);
244 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
252 isl_local_space_free(aff
->ls
);
253 isl_vec_free(aff
->v
);
260 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
262 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
265 /* Externally, an isl_aff has a map space, but internally, the
266 * ls field corresponds to the domain of that space.
268 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
272 if (type
== isl_dim_out
)
274 if (type
== isl_dim_in
)
276 return isl_local_space_dim(aff
->ls
, type
);
279 /* Return the position of the dimension of the given type and name
281 * Return -1 if no such dimension can be found.
283 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
288 if (type
== isl_dim_out
)
290 if (type
== isl_dim_in
)
292 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
295 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
297 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
300 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
305 space
= isl_local_space_get_space(aff
->ls
);
306 space
= isl_space_from_domain(space
);
307 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
311 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
312 __isl_keep isl_aff
*aff
)
314 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
317 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
322 ls
= isl_local_space_copy(aff
->ls
);
323 ls
= isl_local_space_from_domain(ls
);
324 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
328 /* Externally, an isl_aff has a map space, but internally, the
329 * ls field corresponds to the domain of that space.
331 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
332 enum isl_dim_type type
, unsigned pos
)
336 if (type
== isl_dim_out
)
338 if (type
== isl_dim_in
)
340 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
343 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
344 __isl_take isl_space
*dim
)
346 aff
= isl_aff_cow(aff
);
350 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
352 return isl_aff_free(aff
);
361 /* Reset the space of "aff". This function is called from isl_pw_templ.c
362 * and doesn't know if the space of an element object is represented
363 * directly or through its domain. It therefore passes along both.
365 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
366 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
368 isl_space_free(space
);
369 return isl_aff_reset_domain_space(aff
, domain
);
372 /* Reorder the coefficients of the affine expression based
373 * on the given reodering.
374 * The reordering r is assumed to have been extended with the local
377 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
378 __isl_take isl_reordering
*r
, int n_div
)
386 res
= isl_vec_alloc(vec
->ctx
,
387 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
388 isl_seq_cpy(res
->el
, vec
->el
, 2);
389 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
390 for (i
= 0; i
< r
->len
; ++i
)
391 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
393 isl_reordering_free(r
);
398 isl_reordering_free(r
);
402 /* Reorder the dimensions of the domain of "aff" according
403 * to the given reordering.
405 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
406 __isl_take isl_reordering
*r
)
408 aff
= isl_aff_cow(aff
);
412 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
413 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
414 aff
->ls
->div
->n_row
);
415 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
417 if (!aff
->v
|| !aff
->ls
)
418 return isl_aff_free(aff
);
423 isl_reordering_free(r
);
427 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
428 __isl_take isl_space
*model
)
433 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
434 model
, isl_dim_param
)) {
437 model
= isl_space_drop_dims(model
, isl_dim_in
,
438 0, isl_space_dim(model
, isl_dim_in
));
439 model
= isl_space_drop_dims(model
, isl_dim_out
,
440 0, isl_space_dim(model
, isl_dim_out
));
441 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
442 exp
= isl_reordering_extend_space(exp
,
443 isl_aff_get_domain_space(aff
));
444 aff
= isl_aff_realign_domain(aff
, exp
);
447 isl_space_free(model
);
450 isl_space_free(model
);
455 /* Is "aff" obviously equal to zero?
457 * If the denominator is zero, then "aff" is not equal to zero.
459 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
464 if (isl_int_is_zero(aff
->v
->el
[0]))
466 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
469 /* Does "aff" represent NaN?
471 int isl_aff_is_nan(__isl_keep isl_aff
*aff
)
476 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
479 /* Does "pa" involve any NaNs?
481 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
490 for (i
= 0; i
< pa
->n
; ++i
) {
491 int is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
492 if (is_nan
< 0 || is_nan
)
499 /* Are "aff1" and "aff2" obviously equal?
501 * NaN is not equal to anything, not even to another NaN.
503 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
510 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
513 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
514 if (equal
< 0 || !equal
)
517 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
520 /* Return the common denominator of "aff" in "v".
522 * We cannot return anything meaningful in case of a NaN.
524 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
528 if (isl_aff_is_nan(aff
))
529 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
530 "cannot get denominator of NaN", return -1);
531 isl_int_set(*v
, aff
->v
->el
[0]);
535 /* Return the common denominator of "aff".
537 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
544 ctx
= isl_aff_get_ctx(aff
);
545 if (isl_aff_is_nan(aff
))
546 return isl_val_nan(ctx
);
547 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
550 /* Return the constant term of "aff" in "v".
552 * We cannot return anything meaningful in case of a NaN.
554 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
558 if (isl_aff_is_nan(aff
))
559 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
560 "cannot get constant term of NaN", return -1);
561 isl_int_set(*v
, aff
->v
->el
[1]);
565 /* Return the constant term of "aff".
567 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
575 ctx
= isl_aff_get_ctx(aff
);
576 if (isl_aff_is_nan(aff
))
577 return isl_val_nan(ctx
);
578 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
579 return isl_val_normalize(v
);
582 /* Return the coefficient of the variable of type "type" at position "pos"
585 * We cannot return anything meaningful in case of a NaN.
587 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
588 enum isl_dim_type type
, int pos
, isl_int
*v
)
593 if (type
== isl_dim_out
)
594 isl_die(aff
->v
->ctx
, isl_error_invalid
,
595 "output/set dimension does not have a coefficient",
597 if (type
== isl_dim_in
)
600 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
601 isl_die(aff
->v
->ctx
, isl_error_invalid
,
602 "position out of bounds", return -1);
604 if (isl_aff_is_nan(aff
))
605 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
606 "cannot get coefficient of NaN", return -1);
607 pos
+= isl_local_space_offset(aff
->ls
, type
);
608 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
613 /* Return the coefficient of the variable of type "type" at position "pos"
616 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
617 enum isl_dim_type type
, int pos
)
625 ctx
= isl_aff_get_ctx(aff
);
626 if (type
== isl_dim_out
)
627 isl_die(ctx
, isl_error_invalid
,
628 "output/set dimension does not have a coefficient",
630 if (type
== isl_dim_in
)
633 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
634 isl_die(ctx
, isl_error_invalid
,
635 "position out of bounds", return NULL
);
637 if (isl_aff_is_nan(aff
))
638 return isl_val_nan(ctx
);
639 pos
+= isl_local_space_offset(aff
->ls
, type
);
640 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
641 return isl_val_normalize(v
);
644 /* Return the sign of the coefficient of the variable of type "type"
645 * at position "pos" of "aff".
647 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
655 ctx
= isl_aff_get_ctx(aff
);
656 if (type
== isl_dim_out
)
657 isl_die(ctx
, isl_error_invalid
,
658 "output/set dimension does not have a coefficient",
660 if (type
== isl_dim_in
)
663 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
664 isl_die(ctx
, isl_error_invalid
,
665 "position out of bounds", return 0);
667 pos
+= isl_local_space_offset(aff
->ls
, type
);
668 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
671 /* Replace the denominator of "aff" by "v".
673 * A NaN is unaffected by this operation.
675 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
679 if (isl_aff_is_nan(aff
))
681 aff
= isl_aff_cow(aff
);
685 aff
->v
= isl_vec_cow(aff
->v
);
687 return isl_aff_free(aff
);
689 isl_int_set(aff
->v
->el
[0], v
);
694 /* Replace the numerator of the constant term of "aff" by "v".
696 * A NaN is unaffected by this operation.
698 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
702 if (isl_aff_is_nan(aff
))
704 aff
= isl_aff_cow(aff
);
708 aff
->v
= isl_vec_cow(aff
->v
);
710 return isl_aff_free(aff
);
712 isl_int_set(aff
->v
->el
[1], v
);
717 /* Replace the constant term of "aff" by "v".
719 * A NaN is unaffected by this operation.
721 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
722 __isl_take isl_val
*v
)
727 if (isl_aff_is_nan(aff
)) {
732 if (!isl_val_is_rat(v
))
733 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
734 "expecting rational value", goto error
);
736 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
737 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
742 aff
= isl_aff_cow(aff
);
745 aff
->v
= isl_vec_cow(aff
->v
);
749 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
750 isl_int_set(aff
->v
->el
[1], v
->n
);
751 } else if (isl_int_is_one(v
->d
)) {
752 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
754 isl_seq_scale(aff
->v
->el
+ 1,
755 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
756 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
757 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
758 aff
->v
= isl_vec_normalize(aff
->v
);
771 /* Add "v" to the constant term of "aff".
773 * A NaN is unaffected by this operation.
775 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
777 if (isl_int_is_zero(v
))
782 if (isl_aff_is_nan(aff
))
784 aff
= isl_aff_cow(aff
);
788 aff
->v
= isl_vec_cow(aff
->v
);
790 return isl_aff_free(aff
);
792 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
797 /* Add "v" to the constant term of "aff".
799 * A NaN is unaffected by this operation.
801 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
802 __isl_take isl_val
*v
)
807 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
812 if (!isl_val_is_rat(v
))
813 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
814 "expecting rational value", goto error
);
816 aff
= isl_aff_cow(aff
);
820 aff
->v
= isl_vec_cow(aff
->v
);
824 if (isl_int_is_one(v
->d
)) {
825 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
826 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
827 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
828 aff
->v
= isl_vec_normalize(aff
->v
);
832 isl_seq_scale(aff
->v
->el
+ 1,
833 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
834 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
835 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
836 aff
->v
= isl_vec_normalize(aff
->v
);
849 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
854 isl_int_set_si(t
, v
);
855 aff
= isl_aff_add_constant(aff
, t
);
861 /* Add "v" to the numerator of the constant term of "aff".
863 * A NaN is unaffected by this operation.
865 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
867 if (isl_int_is_zero(v
))
872 if (isl_aff_is_nan(aff
))
874 aff
= isl_aff_cow(aff
);
878 aff
->v
= isl_vec_cow(aff
->v
);
880 return isl_aff_free(aff
);
882 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
887 /* Add "v" to the numerator of the constant term of "aff".
889 * A NaN is unaffected by this operation.
891 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
899 isl_int_set_si(t
, v
);
900 aff
= isl_aff_add_constant_num(aff
, t
);
906 /* Replace the numerator of the constant term of "aff" by "v".
908 * A NaN is unaffected by this operation.
910 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
914 if (isl_aff_is_nan(aff
))
916 aff
= isl_aff_cow(aff
);
920 aff
->v
= isl_vec_cow(aff
->v
);
922 return isl_aff_free(aff
);
924 isl_int_set_si(aff
->v
->el
[1], v
);
929 /* Replace the numerator of the coefficient of the variable of type "type"
930 * at position "pos" of "aff" by "v".
932 * A NaN is unaffected by this operation.
934 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
935 enum isl_dim_type type
, int pos
, isl_int v
)
940 if (type
== isl_dim_out
)
941 isl_die(aff
->v
->ctx
, isl_error_invalid
,
942 "output/set dimension does not have a coefficient",
943 return isl_aff_free(aff
));
944 if (type
== isl_dim_in
)
947 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
948 isl_die(aff
->v
->ctx
, isl_error_invalid
,
949 "position out of bounds", return isl_aff_free(aff
));
951 if (isl_aff_is_nan(aff
))
953 aff
= isl_aff_cow(aff
);
957 aff
->v
= isl_vec_cow(aff
->v
);
959 return isl_aff_free(aff
);
961 pos
+= isl_local_space_offset(aff
->ls
, type
);
962 isl_int_set(aff
->v
->el
[1 + pos
], v
);
967 /* Replace the numerator of the coefficient of the variable of type "type"
968 * at position "pos" of "aff" by "v".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
973 enum isl_dim_type type
, int pos
, int v
)
978 if (type
== isl_dim_out
)
979 isl_die(aff
->v
->ctx
, isl_error_invalid
,
980 "output/set dimension does not have a coefficient",
981 return isl_aff_free(aff
));
982 if (type
== isl_dim_in
)
985 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
986 isl_die(aff
->v
->ctx
, isl_error_invalid
,
987 "position out of bounds", return isl_aff_free(aff
));
989 if (isl_aff_is_nan(aff
))
991 pos
+= isl_local_space_offset(aff
->ls
, type
);
992 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
995 aff
= isl_aff_cow(aff
);
999 aff
->v
= isl_vec_cow(aff
->v
);
1001 return isl_aff_free(aff
);
1003 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1008 /* Replace the coefficient of the variable of type "type" at position "pos"
1011 * A NaN is unaffected by this operation.
1013 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1014 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1019 if (type
== isl_dim_out
)
1020 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1021 "output/set dimension does not have a coefficient",
1023 if (type
== isl_dim_in
)
1026 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1027 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1028 "position out of bounds", goto error
);
1030 if (isl_aff_is_nan(aff
)) {
1034 if (!isl_val_is_rat(v
))
1035 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1036 "expecting rational value", goto error
);
1038 pos
+= isl_local_space_offset(aff
->ls
, type
);
1039 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1040 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1045 aff
= isl_aff_cow(aff
);
1048 aff
->v
= isl_vec_cow(aff
->v
);
1052 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1053 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1054 } else if (isl_int_is_one(v
->d
)) {
1055 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1057 isl_seq_scale(aff
->v
->el
+ 1,
1058 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1059 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1060 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1061 aff
->v
= isl_vec_normalize(aff
->v
);
1074 /* Add "v" to the coefficient of the variable of type "type"
1075 * at position "pos" of "aff".
1077 * A NaN is unaffected by this operation.
1079 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1080 enum isl_dim_type type
, int pos
, isl_int v
)
1085 if (type
== isl_dim_out
)
1086 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1087 "output/set dimension does not have a coefficient",
1088 return isl_aff_free(aff
));
1089 if (type
== isl_dim_in
)
1092 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1093 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1094 "position out of bounds", return isl_aff_free(aff
));
1096 if (isl_aff_is_nan(aff
))
1098 aff
= isl_aff_cow(aff
);
1102 aff
->v
= isl_vec_cow(aff
->v
);
1104 return isl_aff_free(aff
);
1106 pos
+= isl_local_space_offset(aff
->ls
, type
);
1107 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1118 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1123 if (isl_val_is_zero(v
)) {
1128 if (type
== isl_dim_out
)
1129 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1130 "output/set dimension does not have a coefficient",
1132 if (type
== isl_dim_in
)
1135 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1136 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1137 "position out of bounds", goto error
);
1139 if (isl_aff_is_nan(aff
)) {
1143 if (!isl_val_is_rat(v
))
1144 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1145 "expecting rational value", goto error
);
1147 aff
= isl_aff_cow(aff
);
1151 aff
->v
= isl_vec_cow(aff
->v
);
1155 pos
+= isl_local_space_offset(aff
->ls
, type
);
1156 if (isl_int_is_one(v
->d
)) {
1157 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1158 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1159 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1160 aff
->v
= isl_vec_normalize(aff
->v
);
1164 isl_seq_scale(aff
->v
->el
+ 1,
1165 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1166 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1167 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1168 aff
->v
= isl_vec_normalize(aff
->v
);
1181 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1182 enum isl_dim_type type
, int pos
, int v
)
1187 isl_int_set_si(t
, v
);
1188 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1194 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1199 return isl_local_space_get_div(aff
->ls
, pos
);
1202 /* Return the negation of "aff".
1204 * As a special case, -NaN = NaN.
1206 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1210 if (isl_aff_is_nan(aff
))
1212 aff
= isl_aff_cow(aff
);
1215 aff
->v
= isl_vec_cow(aff
->v
);
1217 return isl_aff_free(aff
);
1219 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1224 /* Remove divs from the local space that do not appear in the affine
1226 * We currently only remove divs at the end.
1227 * Some intermediate divs may also not appear directly in the affine
1228 * expression, but we would also need to check that no other divs are
1229 * defined in terms of them.
1231 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1240 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1241 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1243 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1247 aff
= isl_aff_cow(aff
);
1251 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1252 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1253 if (!aff
->ls
|| !aff
->v
)
1254 return isl_aff_free(aff
);
1259 /* Given two affine expressions "p" of length p_len (including the
1260 * denominator and the constant term) and "subs" of length subs_len,
1261 * plug in "subs" for the variable at position "pos".
1262 * The variables of "subs" and "p" are assumed to match up to subs_len,
1263 * but "p" may have additional variables.
1264 * "v" is an initialized isl_int that can be used internally.
1266 * In particular, if "p" represents the expression
1270 * with i the variable at position "pos" and "subs" represents the expression
1274 * then the result represents the expression
1279 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1280 int p_len
, int subs_len
, isl_int v
)
1282 isl_int_set(v
, p
[1 + pos
]);
1283 isl_int_set_si(p
[1 + pos
], 0);
1284 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1285 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1286 isl_int_mul(p
[0], p
[0], subs
[0]);
1289 /* Look for any divs in the aff->ls with a denominator equal to one
1290 * and plug them into the affine expression and any subsequent divs
1291 * that may reference the div.
1293 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1299 isl_local_space
*ls
;
1305 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1307 for (i
= 0; i
< n
; ++i
) {
1308 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1310 ls
= isl_local_space_copy(aff
->ls
);
1311 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1312 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1313 vec
= isl_vec_copy(aff
->v
);
1314 vec
= isl_vec_cow(vec
);
1320 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1321 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1326 isl_vec_free(aff
->v
);
1328 isl_local_space_free(aff
->ls
);
1335 isl_local_space_free(ls
);
1336 return isl_aff_free(aff
);
1339 /* Look for any divs j that appear with a unit coefficient inside
1340 * the definitions of other divs i and plug them into the definitions
1343 * In particular, an expression of the form
1345 * floor((f(..) + floor(g(..)/n))/m)
1349 * floor((n * f(..) + g(..))/(n * m))
1351 * This simplification is correct because we can move the expression
1352 * f(..) into the inner floor in the original expression to obtain
1354 * floor(floor((n * f(..) + g(..))/n)/m)
1356 * from which we can derive the simplified expression.
1358 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1366 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1367 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1368 for (i
= 1; i
< n
; ++i
) {
1369 for (j
= 0; j
< i
; ++j
) {
1370 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1372 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1373 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1374 aff
->v
->size
, i
, 1);
1376 return isl_aff_free(aff
);
1383 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1385 * Even though this function is only called on isl_affs with a single
1386 * reference, we are careful to only change aff->v and aff->ls together.
1388 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1390 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1391 isl_local_space
*ls
;
1394 ls
= isl_local_space_copy(aff
->ls
);
1395 ls
= isl_local_space_swap_div(ls
, a
, b
);
1396 v
= isl_vec_copy(aff
->v
);
1401 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1402 isl_vec_free(aff
->v
);
1404 isl_local_space_free(aff
->ls
);
1410 isl_local_space_free(ls
);
1411 return isl_aff_free(aff
);
1414 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1416 * We currently do not actually remove div "b", but simply add its
1417 * coefficient to that of "a" and then zero it out.
1419 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1421 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1423 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1426 aff
->v
= isl_vec_cow(aff
->v
);
1428 return isl_aff_free(aff
);
1430 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1431 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1432 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1437 /* Sort the divs in the local space of "aff" according to
1438 * the comparison function "cmp_row" in isl_local_space.c,
1439 * combining the coefficients of identical divs.
1441 * Reordering divs does not change the semantics of "aff",
1442 * so there is no need to call isl_aff_cow.
1443 * Moreover, this function is currently only called on isl_affs
1444 * with a single reference.
1446 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1454 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1455 n
= isl_aff_dim(aff
, isl_dim_div
);
1456 for (i
= 1; i
< n
; ++i
) {
1457 for (j
= i
- 1; j
>= 0; --j
) {
1458 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1462 aff
= merge_divs(aff
, j
, j
+ 1);
1464 aff
= swap_div(aff
, j
, j
+ 1);
1473 /* Normalize the representation of "aff".
1475 * This function should only be called of "new" isl_affs, i.e.,
1476 * with only a single reference. We therefore do not need to
1477 * worry about affecting other instances.
1479 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1483 aff
->v
= isl_vec_normalize(aff
->v
);
1485 return isl_aff_free(aff
);
1486 aff
= plug_in_integral_divs(aff
);
1487 aff
= plug_in_unit_divs(aff
);
1488 aff
= sort_divs(aff
);
1489 aff
= isl_aff_remove_unused_divs(aff
);
1493 /* Given f, return floor(f).
1494 * If f is an integer expression, then just return f.
1495 * If f is a constant, then return the constant floor(f).
1496 * Otherwise, if f = g/m, write g = q m + r,
1497 * create a new div d = [r/m] and return the expression q + d.
1498 * The coefficients in r are taken to lie between -m/2 and m/2.
1500 * As a special case, floor(NaN) = NaN.
1502 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1512 if (isl_aff_is_nan(aff
))
1514 if (isl_int_is_one(aff
->v
->el
[0]))
1517 aff
= isl_aff_cow(aff
);
1521 aff
->v
= isl_vec_cow(aff
->v
);
1523 return isl_aff_free(aff
);
1525 if (isl_aff_is_cst(aff
)) {
1526 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1527 isl_int_set_si(aff
->v
->el
[0], 1);
1531 div
= isl_vec_copy(aff
->v
);
1532 div
= isl_vec_cow(div
);
1534 return isl_aff_free(aff
);
1536 ctx
= isl_aff_get_ctx(aff
);
1537 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1538 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1539 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1540 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1541 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1542 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1543 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1547 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1549 return isl_aff_free(aff
);
1551 size
= aff
->v
->size
;
1552 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1554 return isl_aff_free(aff
);
1555 isl_int_set_si(aff
->v
->el
[0], 1);
1556 isl_int_set_si(aff
->v
->el
[size
], 1);
1558 aff
= isl_aff_normalize(aff
);
1565 * aff mod m = aff - m * floor(aff/m)
1567 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1571 res
= isl_aff_copy(aff
);
1572 aff
= isl_aff_scale_down(aff
, m
);
1573 aff
= isl_aff_floor(aff
);
1574 aff
= isl_aff_scale(aff
, m
);
1575 res
= isl_aff_sub(res
, aff
);
1582 * aff mod m = aff - m * floor(aff/m)
1584 * with m an integer value.
1586 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1587 __isl_take isl_val
*m
)
1594 if (!isl_val_is_int(m
))
1595 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1596 "expecting integer modulo", goto error
);
1598 res
= isl_aff_copy(aff
);
1599 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1600 aff
= isl_aff_floor(aff
);
1601 aff
= isl_aff_scale_val(aff
, m
);
1602 res
= isl_aff_sub(res
, aff
);
1613 * pwaff mod m = pwaff - m * floor(pwaff/m)
1615 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1619 res
= isl_pw_aff_copy(pwaff
);
1620 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1621 pwaff
= isl_pw_aff_floor(pwaff
);
1622 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1623 res
= isl_pw_aff_sub(res
, pwaff
);
1630 * pa mod m = pa - m * floor(pa/m)
1632 * with m an integer value.
1634 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1635 __isl_take isl_val
*m
)
1639 if (!isl_val_is_int(m
))
1640 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1641 "expecting integer modulo", goto error
);
1642 pa
= isl_pw_aff_mod(pa
, m
->n
);
1646 isl_pw_aff_free(pa
);
1651 /* Given f, return ceil(f).
1652 * If f is an integer expression, then just return f.
1653 * Otherwise, let f be the expression
1659 * floor((e + m - 1)/m)
1661 * As a special case, ceil(NaN) = NaN.
1663 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1668 if (isl_aff_is_nan(aff
))
1670 if (isl_int_is_one(aff
->v
->el
[0]))
1673 aff
= isl_aff_cow(aff
);
1676 aff
->v
= isl_vec_cow(aff
->v
);
1678 return isl_aff_free(aff
);
1680 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1681 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1682 aff
= isl_aff_floor(aff
);
1687 /* Apply the expansion computed by isl_merge_divs.
1688 * The expansion itself is given by "exp" while the resulting
1689 * list of divs is given by "div".
1691 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1692 __isl_take isl_mat
*div
, int *exp
)
1699 aff
= isl_aff_cow(aff
);
1703 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1704 new_n_div
= isl_mat_rows(div
);
1705 if (new_n_div
< old_n_div
)
1706 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1707 "not an expansion", goto error
);
1709 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1713 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1715 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1716 if (j
>= 0 && exp
[j
] == i
) {
1718 isl_int_swap(aff
->v
->el
[offset
+ i
],
1719 aff
->v
->el
[offset
+ j
]);
1722 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1725 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1736 /* Add two affine expressions that live in the same local space.
1738 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1739 __isl_take isl_aff
*aff2
)
1743 aff1
= isl_aff_cow(aff1
);
1747 aff1
->v
= isl_vec_cow(aff1
->v
);
1753 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1754 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1755 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1756 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1757 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1758 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1759 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1771 /* Return the sum of "aff1" and "aff2".
1773 * If either of the two is NaN, then the result is NaN.
1775 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1776 __isl_take isl_aff
*aff2
)
1787 ctx
= isl_aff_get_ctx(aff1
);
1788 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1789 isl_die(ctx
, isl_error_invalid
,
1790 "spaces don't match", goto error
);
1792 if (isl_aff_is_nan(aff1
)) {
1796 if (isl_aff_is_nan(aff2
)) {
1801 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1802 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1803 if (n_div1
== 0 && n_div2
== 0)
1804 return add_expanded(aff1
, aff2
);
1806 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1807 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1808 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1811 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1812 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1813 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1817 return add_expanded(aff1
, aff2
);
1826 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1827 __isl_take isl_aff
*aff2
)
1829 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1832 /* Return the result of scaling "aff" by a factor of "f".
1834 * As a special case, f * NaN = NaN.
1836 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1842 if (isl_aff_is_nan(aff
))
1845 if (isl_int_is_one(f
))
1848 aff
= isl_aff_cow(aff
);
1851 aff
->v
= isl_vec_cow(aff
->v
);
1853 return isl_aff_free(aff
);
1855 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1856 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1861 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1862 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1863 isl_int_divexact(gcd
, f
, gcd
);
1864 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1870 /* Multiple "aff" by "v".
1872 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1873 __isl_take isl_val
*v
)
1878 if (isl_val_is_one(v
)) {
1883 if (!isl_val_is_rat(v
))
1884 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1885 "expecting rational factor", goto error
);
1887 aff
= isl_aff_scale(aff
, v
->n
);
1888 aff
= isl_aff_scale_down(aff
, v
->d
);
1898 /* Return the result of scaling "aff" down by a factor of "f".
1900 * As a special case, NaN/f = NaN.
1902 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1908 if (isl_aff_is_nan(aff
))
1911 if (isl_int_is_one(f
))
1914 aff
= isl_aff_cow(aff
);
1918 if (isl_int_is_zero(f
))
1919 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1920 "cannot scale down by zero", return isl_aff_free(aff
));
1922 aff
->v
= isl_vec_cow(aff
->v
);
1924 return isl_aff_free(aff
);
1927 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1928 isl_int_gcd(gcd
, gcd
, f
);
1929 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1930 isl_int_divexact(gcd
, f
, gcd
);
1931 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1937 /* Divide "aff" by "v".
1939 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1940 __isl_take isl_val
*v
)
1945 if (isl_val_is_one(v
)) {
1950 if (!isl_val_is_rat(v
))
1951 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1952 "expecting rational factor", goto error
);
1953 if (!isl_val_is_pos(v
))
1954 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1955 "factor needs to be positive", goto error
);
1957 aff
= isl_aff_scale(aff
, v
->d
);
1958 aff
= isl_aff_scale_down(aff
, v
->n
);
1968 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1976 isl_int_set_ui(v
, f
);
1977 aff
= isl_aff_scale_down(aff
, v
);
1983 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1984 enum isl_dim_type type
, unsigned pos
, const char *s
)
1986 aff
= isl_aff_cow(aff
);
1989 if (type
== isl_dim_out
)
1990 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1991 "cannot set name of output/set dimension",
1992 return isl_aff_free(aff
));
1993 if (type
== isl_dim_in
)
1995 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1997 return isl_aff_free(aff
);
2002 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2003 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2005 aff
= isl_aff_cow(aff
);
2008 if (type
== isl_dim_out
)
2009 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2010 "cannot set name of output/set dimension",
2012 if (type
== isl_dim_in
)
2014 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2016 return isl_aff_free(aff
);
2025 /* Replace the identifier of the input tuple of "aff" by "id".
2026 * type is currently required to be equal to isl_dim_in
2028 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2029 enum isl_dim_type type
, __isl_take isl_id
*id
)
2031 aff
= isl_aff_cow(aff
);
2034 if (type
!= isl_dim_out
)
2035 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2036 "cannot only set id of input tuple", goto error
);
2037 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2039 return isl_aff_free(aff
);
2048 /* Exploit the equalities in "eq" to simplify the affine expression
2049 * and the expressions of the integer divisions in the local space.
2050 * The integer divisions in this local space are assumed to appear
2051 * as regular dimensions in "eq".
2053 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2054 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2062 if (eq
->n_eq
== 0) {
2063 isl_basic_set_free(eq
);
2067 aff
= isl_aff_cow(aff
);
2071 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2072 isl_basic_set_copy(eq
));
2073 aff
->v
= isl_vec_cow(aff
->v
);
2074 if (!aff
->ls
|| !aff
->v
)
2077 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2079 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2080 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2081 if (j
< 0 || j
== 0 || j
>= total
)
2084 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2088 isl_basic_set_free(eq
);
2089 aff
= isl_aff_normalize(aff
);
2092 isl_basic_set_free(eq
);
2097 /* Exploit the equalities in "eq" to simplify the affine expression
2098 * and the expressions of the integer divisions in the local space.
2100 static __isl_give isl_aff
*isl_aff_substitute_equalities(
2101 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2107 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2109 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2110 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2112 isl_basic_set_free(eq
);
2117 /* Look for equalities among the variables shared by context and aff
2118 * and the integer divisions of aff, if any.
2119 * The equalities are then used to eliminate coefficients and/or integer
2120 * divisions from aff.
2122 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2123 __isl_take isl_set
*context
)
2125 isl_basic_set
*hull
;
2130 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2132 isl_basic_set
*bset
;
2133 isl_local_space
*ls
;
2134 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2135 ls
= isl_aff_get_domain_local_space(aff
);
2136 bset
= isl_basic_set_from_local_space(ls
);
2137 bset
= isl_basic_set_lift(bset
);
2138 bset
= isl_basic_set_flatten(bset
);
2139 context
= isl_set_intersect(context
,
2140 isl_set_from_basic_set(bset
));
2143 hull
= isl_set_affine_hull(context
);
2144 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2147 isl_set_free(context
);
2151 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2152 __isl_take isl_set
*context
)
2154 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2155 dom_context
= isl_set_intersect_params(dom_context
, context
);
2156 return isl_aff_gist(aff
, dom_context
);
2159 /* Return a basic set containing those elements in the space
2160 * of aff where it is non-negative.
2161 * If "rational" is set, then return a rational basic set.
2163 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2165 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2166 __isl_take isl_aff
*aff
, int rational
)
2168 isl_constraint
*ineq
;
2169 isl_basic_set
*bset
;
2173 if (isl_aff_is_nan(aff
)) {
2174 isl_space
*space
= isl_aff_get_domain_space(aff
);
2176 return isl_basic_set_empty(space
);
2179 ineq
= isl_inequality_from_aff(aff
);
2181 bset
= isl_basic_set_from_constraint(ineq
);
2183 bset
= isl_basic_set_set_rational(bset
);
2184 bset
= isl_basic_set_simplify(bset
);
2188 /* Return a basic set containing those elements in the space
2189 * of aff where it is non-negative.
2191 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2193 return aff_nonneg_basic_set(aff
, 0);
2196 /* Return a basic set containing those elements in the domain space
2197 * of aff where it is negative.
2199 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2201 aff
= isl_aff_neg(aff
);
2202 aff
= isl_aff_add_constant_num_si(aff
, -1);
2203 return isl_aff_nonneg_basic_set(aff
);
2206 /* Return a basic set containing those elements in the space
2207 * of aff where it is zero.
2208 * If "rational" is set, then return a rational basic set.
2210 * If "aff" is NaN, then it is not zero.
2212 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
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_equality_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 zero.
2238 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2240 return aff_zero_basic_set(aff
, 0);
2243 /* Return a basic set containing those elements in the shared space
2244 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2246 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2247 __isl_take isl_aff
*aff2
)
2249 aff1
= isl_aff_sub(aff1
, aff2
);
2251 return isl_aff_nonneg_basic_set(aff1
);
2254 /* Return a basic set containing those elements in the shared space
2255 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2257 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2258 __isl_take isl_aff
*aff2
)
2260 return isl_aff_ge_basic_set(aff2
, aff1
);
2263 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2264 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2266 aff1
= isl_aff_add(aff1
, aff2
);
2267 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2271 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2279 /* Check whether the given affine expression has non-zero coefficient
2280 * for any dimension in the given range or if any of these dimensions
2281 * appear with non-zero coefficients in any of the integer divisions
2282 * involved in the affine expression.
2284 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2285 enum isl_dim_type type
, unsigned first
, unsigned n
)
2297 ctx
= isl_aff_get_ctx(aff
);
2298 if (first
+ n
> isl_aff_dim(aff
, type
))
2299 isl_die(ctx
, isl_error_invalid
,
2300 "range out of bounds", return -1);
2302 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2306 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2307 for (i
= 0; i
< n
; ++i
)
2308 if (active
[first
+ i
]) {
2321 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2322 enum isl_dim_type type
, unsigned first
, unsigned n
)
2328 if (type
== isl_dim_out
)
2329 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2330 "cannot drop output/set dimension",
2331 return isl_aff_free(aff
));
2332 if (type
== isl_dim_in
)
2334 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2337 ctx
= isl_aff_get_ctx(aff
);
2338 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2339 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2340 return isl_aff_free(aff
));
2342 aff
= isl_aff_cow(aff
);
2346 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2348 return isl_aff_free(aff
);
2350 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2351 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2353 return isl_aff_free(aff
);
2358 /* Project the domain of the affine expression onto its parameter space.
2359 * The affine expression may not involve any of the domain dimensions.
2361 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2367 n
= isl_aff_dim(aff
, isl_dim_in
);
2368 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2370 return isl_aff_free(aff
);
2372 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2373 "affine expression involves some of the domain dimensions",
2374 return isl_aff_free(aff
));
2375 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2376 space
= isl_aff_get_domain_space(aff
);
2377 space
= isl_space_params(space
);
2378 aff
= isl_aff_reset_domain_space(aff
, space
);
2382 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2383 enum isl_dim_type type
, unsigned first
, unsigned n
)
2389 if (type
== isl_dim_out
)
2390 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2391 "cannot insert output/set dimensions",
2392 return isl_aff_free(aff
));
2393 if (type
== isl_dim_in
)
2395 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2398 ctx
= isl_aff_get_ctx(aff
);
2399 if (first
> isl_local_space_dim(aff
->ls
, type
))
2400 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2401 return isl_aff_free(aff
));
2403 aff
= isl_aff_cow(aff
);
2407 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2409 return isl_aff_free(aff
);
2411 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2412 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2414 return isl_aff_free(aff
);
2419 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2420 enum isl_dim_type type
, unsigned n
)
2424 pos
= isl_aff_dim(aff
, type
);
2426 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2429 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2430 enum isl_dim_type type
, unsigned n
)
2434 pos
= isl_pw_aff_dim(pwaff
, type
);
2436 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2439 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2440 * to dimensions of "dst_type" at "dst_pos".
2442 * We only support moving input dimensions to parameters and vice versa.
2444 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2445 enum isl_dim_type dst_type
, unsigned dst_pos
,
2446 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2454 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2455 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2458 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2459 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2460 "cannot move output/set dimension", isl_aff_free(aff
));
2461 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2462 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2463 "cannot move divs", isl_aff_free(aff
));
2464 if (dst_type
== isl_dim_in
)
2465 dst_type
= isl_dim_set
;
2466 if (src_type
== isl_dim_in
)
2467 src_type
= isl_dim_set
;
2469 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2470 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2471 "range out of bounds", isl_aff_free(aff
));
2472 if (dst_type
== src_type
)
2473 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2474 "moving dims within the same type not supported",
2477 aff
= isl_aff_cow(aff
);
2481 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2482 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2483 if (dst_type
> src_type
)
2486 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2487 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2488 src_type
, src_pos
, n
);
2489 if (!aff
->v
|| !aff
->ls
)
2490 return isl_aff_free(aff
);
2492 aff
= sort_divs(aff
);
2497 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2499 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2500 return isl_pw_aff_alloc(dom
, aff
);
2504 #define PW isl_pw_aff
2508 #define EL_IS_ZERO is_empty
2512 #define IS_ZERO is_empty
2515 #undef DEFAULT_IS_ZERO
2516 #define DEFAULT_IS_ZERO 0
2523 #include <isl_pw_templ.c>
2525 static __isl_give isl_set
*align_params_pw_pw_set_and(
2526 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2527 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2528 __isl_take isl_pw_aff
*pwaff2
))
2530 if (!pwaff1
|| !pwaff2
)
2532 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2533 pwaff2
->dim
, isl_dim_param
))
2534 return fn(pwaff1
, pwaff2
);
2535 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2536 !isl_space_has_named_params(pwaff2
->dim
))
2537 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2538 "unaligned unnamed parameters", goto error
);
2539 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2540 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2541 return fn(pwaff1
, pwaff2
);
2543 isl_pw_aff_free(pwaff1
);
2544 isl_pw_aff_free(pwaff2
);
2548 /* Compute a piecewise quasi-affine expression with a domain that
2549 * is the union of those of pwaff1 and pwaff2 and such that on each
2550 * cell, the quasi-affine expression is the better (according to cmp)
2551 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2552 * is defined on a given cell, then the associated expression
2553 * is the defined one.
2555 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2556 __isl_take isl_pw_aff
*pwaff2
,
2557 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2558 __isl_take isl_aff
*aff2
))
2565 if (!pwaff1
|| !pwaff2
)
2568 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2569 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2570 isl_die(ctx
, isl_error_invalid
,
2571 "arguments should live in same space", goto error
);
2573 if (isl_pw_aff_is_empty(pwaff1
)) {
2574 isl_pw_aff_free(pwaff1
);
2578 if (isl_pw_aff_is_empty(pwaff2
)) {
2579 isl_pw_aff_free(pwaff2
);
2583 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2584 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2586 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2587 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2588 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2589 struct isl_set
*common
;
2592 common
= isl_set_intersect(
2593 isl_set_copy(pwaff1
->p
[i
].set
),
2594 isl_set_copy(pwaff2
->p
[j
].set
));
2595 better
= isl_set_from_basic_set(cmp(
2596 isl_aff_copy(pwaff2
->p
[j
].aff
),
2597 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2598 better
= isl_set_intersect(common
, better
);
2599 if (isl_set_plain_is_empty(better
)) {
2600 isl_set_free(better
);
2603 set
= isl_set_subtract(set
, isl_set_copy(better
));
2605 res
= isl_pw_aff_add_piece(res
, better
,
2606 isl_aff_copy(pwaff2
->p
[j
].aff
));
2608 res
= isl_pw_aff_add_piece(res
, set
,
2609 isl_aff_copy(pwaff1
->p
[i
].aff
));
2612 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2613 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2614 for (i
= 0; i
< pwaff1
->n
; ++i
)
2615 set
= isl_set_subtract(set
,
2616 isl_set_copy(pwaff1
->p
[i
].set
));
2617 res
= isl_pw_aff_add_piece(res
, set
,
2618 isl_aff_copy(pwaff2
->p
[j
].aff
));
2621 isl_pw_aff_free(pwaff1
);
2622 isl_pw_aff_free(pwaff2
);
2626 isl_pw_aff_free(pwaff1
);
2627 isl_pw_aff_free(pwaff2
);
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 maximum of those of pwaff1
2634 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2635 * cell, then the associated expression is the defined one.
2637 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2638 __isl_take isl_pw_aff
*pwaff2
)
2640 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2643 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2644 __isl_take isl_pw_aff
*pwaff2
)
2646 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2650 /* Compute a piecewise quasi-affine expression with a domain that
2651 * is the union of those of pwaff1 and pwaff2 and such that on each
2652 * cell, the quasi-affine expression is the minimum of those of pwaff1
2653 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2654 * cell, then the associated expression is the defined one.
2656 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2657 __isl_take isl_pw_aff
*pwaff2
)
2659 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2662 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2663 __isl_take isl_pw_aff
*pwaff2
)
2665 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2669 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2670 __isl_take isl_pw_aff
*pwaff2
, int max
)
2673 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2675 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2678 /* Construct a map with as domain the domain of pwaff and
2679 * one-dimensional range corresponding to the affine expressions.
2681 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2690 dim
= isl_pw_aff_get_space(pwaff
);
2691 map
= isl_map_empty(dim
);
2693 for (i
= 0; i
< pwaff
->n
; ++i
) {
2694 isl_basic_map
*bmap
;
2697 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2698 map_i
= isl_map_from_basic_map(bmap
);
2699 map_i
= isl_map_intersect_domain(map_i
,
2700 isl_set_copy(pwaff
->p
[i
].set
));
2701 map
= isl_map_union_disjoint(map
, map_i
);
2704 isl_pw_aff_free(pwaff
);
2709 /* Construct a map with as domain the domain of pwaff and
2710 * one-dimensional range corresponding to the affine expressions.
2712 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2716 if (isl_space_is_set(pwaff
->dim
))
2717 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2718 "space of input is not a map", goto error
);
2719 return map_from_pw_aff(pwaff
);
2721 isl_pw_aff_free(pwaff
);
2725 /* Construct a one-dimensional set with as parameter domain
2726 * the domain of pwaff and the single set dimension
2727 * corresponding to the affine expressions.
2729 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2733 if (!isl_space_is_set(pwaff
->dim
))
2734 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2735 "space of input is not a set", goto error
);
2736 return map_from_pw_aff(pwaff
);
2738 isl_pw_aff_free(pwaff
);
2742 /* Return a set containing those elements in the domain
2743 * of pwaff where it is non-negative.
2745 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2753 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2755 for (i
= 0; i
< pwaff
->n
; ++i
) {
2756 isl_basic_set
*bset
;
2760 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2761 bset
= aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2763 set_i
= isl_set_from_basic_set(bset
);
2764 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
2765 set
= isl_set_union_disjoint(set
, set_i
);
2768 isl_pw_aff_free(pwaff
);
2773 /* Return a set containing those elements in the domain
2774 * of pwaff where it is zero (if complement is 0) or not zero
2775 * (if complement is 1).
2777 * The pieces with a NaN never belong to the result since
2778 * NaN is neither zero nor non-zero.
2780 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
2789 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2791 for (i
= 0; i
< pwaff
->n
; ++i
) {
2792 isl_basic_set
*bset
;
2793 isl_set
*set_i
, *zero
;
2796 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2799 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2800 bset
= aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2802 zero
= isl_set_from_basic_set(bset
);
2803 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2805 set_i
= isl_set_subtract(set_i
, zero
);
2807 set_i
= isl_set_intersect(set_i
, zero
);
2808 set
= isl_set_union_disjoint(set
, set_i
);
2811 isl_pw_aff_free(pwaff
);
2816 /* Return a set containing those elements in the domain
2817 * of pwaff where it is zero.
2819 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2821 return pw_aff_zero_set(pwaff
, 0);
2824 /* Return a set containing those elements in the domain
2825 * of pwaff where it is not zero.
2827 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2829 return pw_aff_zero_set(pwaff
, 1);
2832 /* Return a set containing those elements in the shared domain
2833 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2835 * We compute the difference on the shared domain and then construct
2836 * the set of values where this difference is non-negative.
2837 * If strict is set, we first subtract 1 from the difference.
2838 * If equal is set, we only return the elements where pwaff1 and pwaff2
2841 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2842 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2844 isl_set
*set1
, *set2
;
2846 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2847 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2848 set1
= isl_set_intersect(set1
, set2
);
2849 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2850 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2851 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2854 isl_space
*dim
= isl_set_get_space(set1
);
2856 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2857 aff
= isl_aff_add_constant_si(aff
, -1);
2858 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2863 return isl_pw_aff_zero_set(pwaff1
);
2864 return isl_pw_aff_nonneg_set(pwaff1
);
2867 /* Return a set containing those elements in the shared domain
2868 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2870 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2871 __isl_take isl_pw_aff
*pwaff2
)
2873 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2876 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2877 __isl_take isl_pw_aff
*pwaff2
)
2879 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2882 /* Return a set containing those elements in the shared domain
2883 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2885 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2886 __isl_take isl_pw_aff
*pwaff2
)
2888 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2891 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2892 __isl_take isl_pw_aff
*pwaff2
)
2894 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2897 /* Return a set containing those elements in the shared domain
2898 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2900 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2901 __isl_take isl_pw_aff
*pwaff2
)
2903 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2906 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2907 __isl_take isl_pw_aff
*pwaff2
)
2909 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2912 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2913 __isl_take isl_pw_aff
*pwaff2
)
2915 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2918 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2919 __isl_take isl_pw_aff
*pwaff2
)
2921 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2924 /* Return a set containing those elements in the shared domain
2925 * of the elements of list1 and list2 where each element in list1
2926 * has the relation specified by "fn" with each element in list2.
2928 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2929 __isl_take isl_pw_aff_list
*list2
,
2930 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2931 __isl_take isl_pw_aff
*pwaff2
))
2937 if (!list1
|| !list2
)
2940 ctx
= isl_pw_aff_list_get_ctx(list1
);
2941 if (list1
->n
< 1 || list2
->n
< 1)
2942 isl_die(ctx
, isl_error_invalid
,
2943 "list should contain at least one element", goto error
);
2945 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2946 for (i
= 0; i
< list1
->n
; ++i
)
2947 for (j
= 0; j
< list2
->n
; ++j
) {
2950 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2951 isl_pw_aff_copy(list2
->p
[j
]));
2952 set
= isl_set_intersect(set
, set_ij
);
2955 isl_pw_aff_list_free(list1
);
2956 isl_pw_aff_list_free(list2
);
2959 isl_pw_aff_list_free(list1
);
2960 isl_pw_aff_list_free(list2
);
2964 /* Return a set containing those elements in the shared domain
2965 * of the elements of list1 and list2 where each element in list1
2966 * is equal to each element in list2.
2968 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2969 __isl_take isl_pw_aff_list
*list2
)
2971 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2974 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2975 __isl_take isl_pw_aff_list
*list2
)
2977 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2980 /* Return a set containing those elements in the shared domain
2981 * of the elements of list1 and list2 where each element in list1
2982 * is less than or equal to each element in list2.
2984 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2985 __isl_take isl_pw_aff_list
*list2
)
2987 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2990 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2991 __isl_take isl_pw_aff_list
*list2
)
2993 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2996 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2997 __isl_take isl_pw_aff_list
*list2
)
2999 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3002 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3003 __isl_take isl_pw_aff_list
*list2
)
3005 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3009 /* Return a set containing those elements in the shared domain
3010 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3012 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3013 __isl_take isl_pw_aff
*pwaff2
)
3015 isl_set
*set_lt
, *set_gt
;
3017 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3018 isl_pw_aff_copy(pwaff2
));
3019 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3020 return isl_set_union_disjoint(set_lt
, set_gt
);
3023 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3024 __isl_take isl_pw_aff
*pwaff2
)
3026 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3029 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3034 if (isl_int_is_one(v
))
3036 if (!isl_int_is_pos(v
))
3037 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3038 "factor needs to be positive",
3039 return isl_pw_aff_free(pwaff
));
3040 pwaff
= isl_pw_aff_cow(pwaff
);
3046 for (i
= 0; i
< pwaff
->n
; ++i
) {
3047 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3048 if (!pwaff
->p
[i
].aff
)
3049 return isl_pw_aff_free(pwaff
);
3055 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3059 pwaff
= isl_pw_aff_cow(pwaff
);
3065 for (i
= 0; i
< pwaff
->n
; ++i
) {
3066 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3067 if (!pwaff
->p
[i
].aff
)
3068 return isl_pw_aff_free(pwaff
);
3074 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3078 pwaff
= isl_pw_aff_cow(pwaff
);
3084 for (i
= 0; i
< pwaff
->n
; ++i
) {
3085 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3086 if (!pwaff
->p
[i
].aff
)
3087 return isl_pw_aff_free(pwaff
);
3093 /* Assuming that "cond1" and "cond2" are disjoint,
3094 * return an affine expression that is equal to pwaff1 on cond1
3095 * and to pwaff2 on cond2.
3097 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3098 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3099 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3101 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3102 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3104 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3107 /* Return an affine expression that is equal to pwaff_true for elements
3108 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3110 * That is, return cond ? pwaff_true : pwaff_false;
3112 * If "cond" involves and NaN, then we conservatively return a NaN
3113 * on its entire domain. In principle, we could consider the pieces
3114 * where it is NaN separately from those where it is not.
3116 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3117 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3119 isl_set
*cond_true
, *cond_false
;
3123 if (isl_pw_aff_involves_nan(cond
)) {
3124 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3125 isl_local_space
*ls
= isl_local_space_from_space(space
);
3126 isl_pw_aff_free(cond
);
3127 isl_pw_aff_free(pwaff_true
);
3128 isl_pw_aff_free(pwaff_false
);
3129 return isl_pw_aff_nan_on_domain(ls
);
3132 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3133 cond_false
= isl_pw_aff_zero_set(cond
);
3134 return isl_pw_aff_select(cond_true
, pwaff_true
,
3135 cond_false
, pwaff_false
);
3137 isl_pw_aff_free(cond
);
3138 isl_pw_aff_free(pwaff_true
);
3139 isl_pw_aff_free(pwaff_false
);
3143 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3148 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3151 /* Check whether pwaff is a piecewise constant.
3153 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3160 for (i
= 0; i
< pwaff
->n
; ++i
) {
3161 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3162 if (is_cst
< 0 || !is_cst
)
3169 /* Return the product of "aff1" and "aff2".
3171 * If either of the two is NaN, then the result is NaN.
3173 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3175 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3176 __isl_take isl_aff
*aff2
)
3181 if (isl_aff_is_nan(aff1
)) {
3185 if (isl_aff_is_nan(aff2
)) {
3190 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3191 return isl_aff_mul(aff2
, aff1
);
3193 if (!isl_aff_is_cst(aff2
))
3194 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3195 "at least one affine expression should be constant",
3198 aff1
= isl_aff_cow(aff1
);
3202 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3203 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3213 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3215 * If either of the two is NaN, then the result is NaN.
3217 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3218 __isl_take isl_aff
*aff2
)
3226 if (isl_aff_is_nan(aff1
)) {
3230 if (isl_aff_is_nan(aff2
)) {
3235 is_cst
= isl_aff_is_cst(aff2
);
3239 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3240 "second argument should be a constant", goto error
);
3245 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3247 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3248 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3251 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3252 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3255 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3256 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3267 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3268 __isl_take isl_pw_aff
*pwaff2
)
3270 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3273 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3274 __isl_take isl_pw_aff
*pwaff2
)
3276 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3279 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3280 __isl_take isl_pw_aff
*pwaff2
)
3282 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3285 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3286 __isl_take isl_pw_aff
*pwaff2
)
3288 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3291 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3292 __isl_take isl_pw_aff
*pwaff2
)
3294 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3297 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3298 __isl_take isl_pw_aff
*pa2
)
3300 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3303 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3305 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3306 __isl_take isl_pw_aff
*pa2
)
3310 is_cst
= isl_pw_aff_is_cst(pa2
);
3314 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3315 "second argument should be a piecewise constant",
3317 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3319 isl_pw_aff_free(pa1
);
3320 isl_pw_aff_free(pa2
);
3324 /* Compute the quotient of the integer division of "pa1" by "pa2"
3325 * with rounding towards zero.
3326 * "pa2" is assumed to be a piecewise constant.
3328 * In particular, return
3330 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3333 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3334 __isl_take isl_pw_aff
*pa2
)
3340 is_cst
= isl_pw_aff_is_cst(pa2
);
3344 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3345 "second argument should be a piecewise constant",
3348 pa1
= isl_pw_aff_div(pa1
, pa2
);
3350 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3351 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3352 c
= isl_pw_aff_ceil(pa1
);
3353 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3355 isl_pw_aff_free(pa1
);
3356 isl_pw_aff_free(pa2
);
3360 /* Compute the remainder of the integer division of "pa1" by "pa2"
3361 * with rounding towards zero.
3362 * "pa2" is assumed to be a piecewise constant.
3364 * In particular, return
3366 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3369 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3370 __isl_take isl_pw_aff
*pa2
)
3375 is_cst
= isl_pw_aff_is_cst(pa2
);
3379 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3380 "second argument should be a piecewise constant",
3382 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3383 res
= isl_pw_aff_mul(pa2
, res
);
3384 res
= isl_pw_aff_sub(pa1
, res
);
3387 isl_pw_aff_free(pa1
);
3388 isl_pw_aff_free(pa2
);
3392 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3393 __isl_take isl_pw_aff
*pwaff2
)
3398 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3399 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3400 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3401 isl_pw_aff_copy(pwaff2
));
3402 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3403 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3406 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3407 __isl_take isl_pw_aff
*pwaff2
)
3409 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3412 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3413 __isl_take isl_pw_aff
*pwaff2
)
3418 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3419 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3420 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3421 isl_pw_aff_copy(pwaff2
));
3422 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3423 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3426 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3427 __isl_take isl_pw_aff
*pwaff2
)
3429 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3432 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3433 __isl_take isl_pw_aff_list
*list
,
3434 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3435 __isl_take isl_pw_aff
*pwaff2
))
3444 ctx
= isl_pw_aff_list_get_ctx(list
);
3446 isl_die(ctx
, isl_error_invalid
,
3447 "list should contain at least one element", goto error
);
3449 res
= isl_pw_aff_copy(list
->p
[0]);
3450 for (i
= 1; i
< list
->n
; ++i
)
3451 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3453 isl_pw_aff_list_free(list
);
3456 isl_pw_aff_list_free(list
);
3460 /* Return an isl_pw_aff that maps each element in the intersection of the
3461 * domains of the elements of list to the minimal corresponding affine
3464 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3466 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3469 /* Return an isl_pw_aff that maps each element in the intersection of the
3470 * domains of the elements of list to the maximal corresponding affine
3473 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3475 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3478 /* Mark the domains of "pwaff" as rational.
3480 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3484 pwaff
= isl_pw_aff_cow(pwaff
);
3490 for (i
= 0; i
< pwaff
->n
; ++i
) {
3491 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3492 if (!pwaff
->p
[i
].set
)
3493 return isl_pw_aff_free(pwaff
);
3499 /* Mark the domains of the elements of "list" as rational.
3501 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3502 __isl_take isl_pw_aff_list
*list
)
3512 for (i
= 0; i
< n
; ++i
) {
3515 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3516 pa
= isl_pw_aff_set_rational(pa
);
3517 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3523 /* Do the parameters of "aff" match those of "space"?
3525 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3526 __isl_keep isl_space
*space
)
3528 isl_space
*aff_space
;
3534 aff_space
= isl_aff_get_domain_space(aff
);
3536 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3538 isl_space_free(aff_space
);
3542 /* Check that the domain space of "aff" matches "space".
3544 * Return 0 on success and -1 on error.
3546 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3547 __isl_keep isl_space
*space
)
3549 isl_space
*aff_space
;
3555 aff_space
= isl_aff_get_domain_space(aff
);
3557 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3561 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3562 "parameters don't match", goto error
);
3563 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3564 aff_space
, isl_dim_set
);
3568 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3569 "domains don't match", goto error
);
3570 isl_space_free(aff_space
);
3573 isl_space_free(aff_space
);
3579 #define NO_INTERSECT_DOMAIN
3582 #include <isl_multi_templ.c>
3585 #undef NO_INTERSECT_DOMAIN
3587 /* Remove any internal structure of the domain of "ma".
3588 * If there is any such internal structure in the input,
3589 * then the name of the corresponding space is also removed.
3591 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3592 __isl_take isl_multi_aff
*ma
)
3599 if (!ma
->space
->nested
[0])
3602 space
= isl_multi_aff_get_space(ma
);
3603 space
= isl_space_flatten_domain(space
);
3604 ma
= isl_multi_aff_reset_space(ma
, space
);
3609 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3610 * of the space to its domain.
3612 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3615 isl_local_space
*ls
;
3620 if (!isl_space_is_map(space
))
3621 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3622 "not a map space", goto error
);
3624 n_in
= isl_space_dim(space
, isl_dim_in
);
3625 space
= isl_space_domain_map(space
);
3627 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3629 isl_space_free(space
);
3633 space
= isl_space_domain(space
);
3634 ls
= isl_local_space_from_space(space
);
3635 for (i
= 0; i
< n_in
; ++i
) {
3638 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3640 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3642 isl_local_space_free(ls
);
3645 isl_space_free(space
);
3649 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3650 * of the space to its range.
3652 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3655 isl_local_space
*ls
;
3660 if (!isl_space_is_map(space
))
3661 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3662 "not a map space", goto error
);
3664 n_in
= isl_space_dim(space
, isl_dim_in
);
3665 n_out
= isl_space_dim(space
, isl_dim_out
);
3666 space
= isl_space_range_map(space
);
3668 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3670 isl_space_free(space
);
3674 space
= isl_space_domain(space
);
3675 ls
= isl_local_space_from_space(space
);
3676 for (i
= 0; i
< n_out
; ++i
) {
3679 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3680 isl_dim_set
, n_in
+ i
);
3681 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3683 isl_local_space_free(ls
);
3686 isl_space_free(space
);
3690 /* Given the space of a set and a range of set dimensions,
3691 * construct an isl_multi_aff that projects out those dimensions.
3693 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3694 __isl_take isl_space
*space
, enum isl_dim_type type
,
3695 unsigned first
, unsigned n
)
3698 isl_local_space
*ls
;
3703 if (!isl_space_is_set(space
))
3704 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3705 "expecting set space", goto error
);
3706 if (type
!= isl_dim_set
)
3707 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3708 "only set dimensions can be projected out", goto error
);
3710 dim
= isl_space_dim(space
, isl_dim_set
);
3711 if (first
+ n
> dim
)
3712 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3713 "range out of bounds", goto error
);
3715 space
= isl_space_from_domain(space
);
3716 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3719 return isl_multi_aff_alloc(space
);
3721 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3722 space
= isl_space_domain(space
);
3723 ls
= isl_local_space_from_space(space
);
3725 for (i
= 0; i
< first
; ++i
) {
3728 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3730 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3733 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3736 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3737 isl_dim_set
, first
+ n
+ i
);
3738 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3741 isl_local_space_free(ls
);
3744 isl_space_free(space
);
3748 /* Given the space of a set and a range of set dimensions,
3749 * construct an isl_pw_multi_aff that projects out those dimensions.
3751 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3752 __isl_take isl_space
*space
, enum isl_dim_type type
,
3753 unsigned first
, unsigned n
)
3757 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3758 return isl_pw_multi_aff_from_multi_aff(ma
);
3761 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3764 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3765 __isl_take isl_multi_aff
*ma
)
3767 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3768 return isl_pw_multi_aff_alloc(dom
, ma
);
3771 /* Create a piecewise multi-affine expression in the given space that maps each
3772 * input dimension to the corresponding output dimension.
3774 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3775 __isl_take isl_space
*space
)
3777 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3780 /* Add "ma2" to "ma1" and return the result.
3782 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3784 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3785 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3787 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3790 /* Add "ma2" to "ma1" and return the result.
3792 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3793 __isl_take isl_multi_aff
*ma2
)
3795 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3796 &isl_multi_aff_add_aligned
);
3799 /* Subtract "ma2" from "ma1" and return the result.
3801 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3803 static __isl_give isl_multi_aff
*isl_multi_aff_sub_aligned(
3804 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3806 return isl_multi_aff_bin_op(ma1
, ma2
, &isl_aff_sub
);
3809 /* Subtract "ma2" from "ma1" and return the result.
3811 __isl_give isl_multi_aff
*isl_multi_aff_sub(__isl_take isl_multi_aff
*ma1
,
3812 __isl_take isl_multi_aff
*ma2
)
3814 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3815 &isl_multi_aff_sub_aligned
);
3818 /* Exploit the equalities in "eq" to simplify the affine expressions.
3820 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3821 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3825 maff
= isl_multi_aff_cow(maff
);
3829 for (i
= 0; i
< maff
->n
; ++i
) {
3830 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3831 isl_basic_set_copy(eq
));
3836 isl_basic_set_free(eq
);
3839 isl_basic_set_free(eq
);
3840 isl_multi_aff_free(maff
);
3844 /* Given f, return floor(f).
3846 __isl_give isl_multi_aff
*isl_multi_aff_floor(__isl_take isl_multi_aff
*ma
)
3850 ma
= isl_multi_aff_cow(ma
);
3854 for (i
= 0; i
< ma
->n
; ++i
) {
3855 ma
->p
[i
] = isl_aff_floor(ma
->p
[i
]);
3857 return isl_multi_aff_free(ma
);
3863 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3868 maff
= isl_multi_aff_cow(maff
);
3872 for (i
= 0; i
< maff
->n
; ++i
) {
3873 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3875 return isl_multi_aff_free(maff
);
3881 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3882 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3884 maff1
= isl_multi_aff_add(maff1
, maff2
);
3885 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3889 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3897 /* Return the set of domain elements where "ma1" is lexicographically
3898 * smaller than or equal to "ma2".
3900 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3901 __isl_take isl_multi_aff
*ma2
)
3903 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3906 /* Return the set of domain elements where "ma1" is lexicographically
3907 * greater than or equal to "ma2".
3909 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3910 __isl_take isl_multi_aff
*ma2
)
3913 isl_map
*map1
, *map2
;
3916 map1
= isl_map_from_multi_aff(ma1
);
3917 map2
= isl_map_from_multi_aff(ma2
);
3918 map
= isl_map_range_product(map1
, map2
);
3919 space
= isl_space_range(isl_map_get_space(map
));
3920 space
= isl_space_domain(isl_space_unwrap(space
));
3921 ge
= isl_map_lex_ge(space
);
3922 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3924 return isl_map_domain(map
);
3928 #define PW isl_pw_multi_aff
3930 #define EL isl_multi_aff
3932 #define EL_IS_ZERO is_empty
3936 #define IS_ZERO is_empty
3939 #undef DEFAULT_IS_ZERO
3940 #define DEFAULT_IS_ZERO 0
3945 #define NO_INVOLVES_DIMS
3946 #define NO_INSERT_DIMS
3950 #include <isl_pw_templ.c>
3953 #define UNION isl_union_pw_multi_aff
3955 #define PART isl_pw_multi_aff
3957 #define PARTS pw_multi_aff
3958 #define ALIGN_DOMAIN
3962 #include <isl_union_templ.c>
3964 /* Given a function "cmp" that returns the set of elements where
3965 * "ma1" is "better" than "ma2", return the intersection of this
3966 * set with "dom1" and "dom2".
3968 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
3969 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
3970 __isl_keep isl_multi_aff
*ma2
,
3971 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3972 __isl_take isl_multi_aff
*ma2
))
3978 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
3979 is_empty
= isl_set_plain_is_empty(common
);
3980 if (is_empty
>= 0 && is_empty
)
3983 return isl_set_free(common
);
3984 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
3985 better
= isl_set_intersect(common
, better
);
3990 /* Given a function "cmp" that returns the set of elements where
3991 * "ma1" is "better" than "ma2", return a piecewise multi affine
3992 * expression defined on the union of the definition domains
3993 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3994 * "pma2" on each cell. If only one of the two input functions
3995 * is defined on a given cell, then it is considered the best.
3997 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
3998 __isl_take isl_pw_multi_aff
*pma1
,
3999 __isl_take isl_pw_multi_aff
*pma2
,
4000 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4001 __isl_take isl_multi_aff
*ma2
))
4004 isl_pw_multi_aff
*res
= NULL
;
4006 isl_set
*set
= NULL
;
4011 ctx
= isl_space_get_ctx(pma1
->dim
);
4012 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
4013 isl_die(ctx
, isl_error_invalid
,
4014 "arguments should live in the same space", goto error
);
4016 if (isl_pw_multi_aff_is_empty(pma1
)) {
4017 isl_pw_multi_aff_free(pma1
);
4021 if (isl_pw_multi_aff_is_empty(pma2
)) {
4022 isl_pw_multi_aff_free(pma2
);
4026 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4027 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4029 for (i
= 0; i
< pma1
->n
; ++i
) {
4030 set
= isl_set_copy(pma1
->p
[i
].set
);
4031 for (j
= 0; j
< pma2
->n
; ++j
) {
4035 better
= shared_and_better(pma2
->p
[j
].set
,
4036 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4037 pma1
->p
[i
].maff
, cmp
);
4038 is_empty
= isl_set_plain_is_empty(better
);
4039 if (is_empty
< 0 || is_empty
) {
4040 isl_set_free(better
);
4045 set
= isl_set_subtract(set
, isl_set_copy(better
));
4047 res
= isl_pw_multi_aff_add_piece(res
, better
,
4048 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4050 res
= isl_pw_multi_aff_add_piece(res
, set
,
4051 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4054 for (j
= 0; j
< pma2
->n
; ++j
) {
4055 set
= isl_set_copy(pma2
->p
[j
].set
);
4056 for (i
= 0; i
< pma1
->n
; ++i
)
4057 set
= isl_set_subtract(set
,
4058 isl_set_copy(pma1
->p
[i
].set
));
4059 res
= isl_pw_multi_aff_add_piece(res
, set
,
4060 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4063 isl_pw_multi_aff_free(pma1
);
4064 isl_pw_multi_aff_free(pma2
);
4068 isl_pw_multi_aff_free(pma1
);
4069 isl_pw_multi_aff_free(pma2
);
4071 return isl_pw_multi_aff_free(res
);
4074 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4075 __isl_take isl_pw_multi_aff
*pma1
,
4076 __isl_take isl_pw_multi_aff
*pma2
)
4078 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4081 /* Given two piecewise multi affine expressions, return a piecewise
4082 * multi-affine expression defined on the union of the definition domains
4083 * of the inputs that is equal to the lexicographic maximum of the two
4084 * inputs on each cell. If only one of the two inputs is defined on
4085 * a given cell, then it is considered to be the maximum.
4087 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4088 __isl_take isl_pw_multi_aff
*pma1
,
4089 __isl_take isl_pw_multi_aff
*pma2
)
4091 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4092 &pw_multi_aff_union_lexmax
);
4095 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4096 __isl_take isl_pw_multi_aff
*pma1
,
4097 __isl_take isl_pw_multi_aff
*pma2
)
4099 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4102 /* Given two piecewise multi affine expressions, return a piecewise
4103 * multi-affine expression defined on the union of the definition domains
4104 * of the inputs that is equal to the lexicographic minimum of the two
4105 * inputs on each cell. If only one of the two inputs is defined on
4106 * a given cell, then it is considered to be the minimum.
4108 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4109 __isl_take isl_pw_multi_aff
*pma1
,
4110 __isl_take isl_pw_multi_aff
*pma2
)
4112 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4113 &pw_multi_aff_union_lexmin
);
4116 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4117 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4119 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4120 &isl_multi_aff_add
);
4123 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4124 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4126 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4130 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4131 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4133 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4134 &isl_multi_aff_sub
);
4137 /* Subtract "pma2" from "pma1" and return the result.
4139 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4140 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4142 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4146 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4147 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4149 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4152 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4153 * with the actual sum on the shared domain and
4154 * the defined expression on the symmetric difference of the domains.
4156 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4157 __isl_take isl_union_pw_multi_aff
*upma1
,
4158 __isl_take isl_union_pw_multi_aff
*upma2
)
4160 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4163 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4164 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4166 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4167 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4171 isl_pw_multi_aff
*res
;
4176 n
= pma1
->n
* pma2
->n
;
4177 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4178 isl_space_copy(pma2
->dim
));
4179 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4181 for (i
= 0; i
< pma1
->n
; ++i
) {
4182 for (j
= 0; j
< pma2
->n
; ++j
) {
4186 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4187 isl_set_copy(pma2
->p
[j
].set
));
4188 ma
= isl_multi_aff_product(
4189 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4190 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4191 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4195 isl_pw_multi_aff_free(pma1
);
4196 isl_pw_multi_aff_free(pma2
);
4199 isl_pw_multi_aff_free(pma1
);
4200 isl_pw_multi_aff_free(pma2
);
4204 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4205 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4207 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4208 &pw_multi_aff_product
);
4211 /* Construct a map mapping the domain of the piecewise multi-affine expression
4212 * to its range, with each dimension in the range equated to the
4213 * corresponding affine expression on its cell.
4215 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4223 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4225 for (i
= 0; i
< pma
->n
; ++i
) {
4226 isl_multi_aff
*maff
;
4227 isl_basic_map
*bmap
;
4230 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4231 bmap
= isl_basic_map_from_multi_aff(maff
);
4232 map_i
= isl_map_from_basic_map(bmap
);
4233 map_i
= isl_map_intersect_domain(map_i
,
4234 isl_set_copy(pma
->p
[i
].set
));
4235 map
= isl_map_union_disjoint(map
, map_i
);
4238 isl_pw_multi_aff_free(pma
);
4242 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4247 if (!isl_space_is_set(pma
->dim
))
4248 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4249 "isl_pw_multi_aff cannot be converted into an isl_set",
4252 return isl_map_from_pw_multi_aff(pma
);
4254 isl_pw_multi_aff_free(pma
);
4258 /* Given a basic map with a single output dimension that is defined
4259 * in terms of the parameters and input dimensions using an equality,
4260 * extract an isl_aff that expresses the output dimension in terms
4261 * of the parameters and input dimensions.
4262 * Note that this expression may involve integer divisions defined
4263 * in terms of parameters and input dimensions.
4265 * This function shares some similarities with
4266 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4268 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4269 __isl_take isl_basic_map
*bmap
)
4274 isl_local_space
*ls
;
4279 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
4280 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4281 "basic map should have a single output dimension",
4283 eq
= isl_basic_map_output_defining_equality(bmap
, 0);
4284 if (eq
>= bmap
->n_eq
)
4285 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4286 "unable to find suitable equality", goto error
);
4287 ls
= isl_basic_map_get_local_space(bmap
);
4288 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
4291 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
4292 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4293 if (isl_int_is_neg(bmap
->eq
[eq
][offset
])) {
4294 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4295 isl_seq_cpy(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4298 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4299 isl_seq_neg(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4302 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][offset
]);
4303 isl_basic_map_free(bmap
);
4305 aff
= isl_aff_remove_unused_divs(aff
);
4308 isl_basic_map_free(bmap
);
4312 /* Given a basic map where each output dimension is defined
4313 * in terms of the parameters and input dimensions using an equality,
4314 * extract an isl_multi_aff that expresses the output dimensions in terms
4315 * of the parameters and input dimensions.
4317 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4318 __isl_take isl_basic_map
*bmap
)
4327 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4328 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4330 for (i
= 0; i
< n_out
; ++i
) {
4331 isl_basic_map
*bmap_i
;
4334 bmap_i
= isl_basic_map_copy(bmap
);
4335 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4336 i
+ 1, n_out
- (1 + i
));
4337 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4338 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4339 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4342 isl_basic_map_free(bmap
);
4347 /* Given a basic set where each set dimension is defined
4348 * in terms of the parameters using an equality,
4349 * extract an isl_multi_aff that expresses the set dimensions in terms
4350 * of the parameters.
4352 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4353 __isl_take isl_basic_set
*bset
)
4355 return extract_isl_multi_aff_from_basic_map(bset
);
4358 /* Create an isl_pw_multi_aff that is equivalent to
4359 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4360 * The given basic map is such that each output dimension is defined
4361 * in terms of the parameters and input dimensions using an equality.
4363 * Since some applications expect the result of isl_pw_multi_aff_from_map
4364 * to only contain integer affine expressions, we compute the floor
4365 * of the expression before returning.
4367 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4368 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4372 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4373 ma
= isl_multi_aff_floor(ma
);
4374 return isl_pw_multi_aff_alloc(domain
, ma
);
4377 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4378 * This obviously only works if the input "map" is single-valued.
4379 * If so, we compute the lexicographic minimum of the image in the form
4380 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4381 * to its lexicographic minimum.
4382 * If the input is not single-valued, we produce an error.
4384 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4385 __isl_take isl_map
*map
)
4389 isl_pw_multi_aff
*pma
;
4391 sv
= isl_map_is_single_valued(map
);
4395 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4396 "map is not single-valued", goto error
);
4397 map
= isl_map_make_disjoint(map
);
4401 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4403 for (i
= 0; i
< map
->n
; ++i
) {
4404 isl_pw_multi_aff
*pma_i
;
4405 isl_basic_map
*bmap
;
4406 bmap
= isl_basic_map_copy(map
->p
[i
]);
4407 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4408 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4418 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4419 * taking into account that the output dimension at position "d"
4420 * can be represented as
4422 * x = floor((e(...) + c1) / m)
4424 * given that constraint "i" is of the form
4426 * e(...) + c1 - m x >= 0
4429 * Let "map" be of the form
4433 * We construct a mapping
4435 * A -> [A -> x = floor(...)]
4437 * apply that to the map, obtaining
4439 * [A -> x = floor(...)] -> B
4441 * and equate dimension "d" to x.
4442 * We then compute a isl_pw_multi_aff representation of the resulting map
4443 * and plug in the mapping above.
4445 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4446 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4450 isl_local_space
*ls
;
4458 isl_pw_multi_aff
*pma
;
4461 is_set
= isl_map_is_set(map
);
4463 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4464 ctx
= isl_map_get_ctx(map
);
4465 space
= isl_space_domain(isl_map_get_space(map
));
4466 n_in
= isl_space_dim(space
, isl_dim_set
);
4467 n
= isl_space_dim(space
, isl_dim_all
);
4469 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4471 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4472 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4474 isl_basic_map_free(hull
);
4476 ls
= isl_local_space_from_space(isl_space_copy(space
));
4477 aff
= isl_aff_alloc_vec(ls
, v
);
4478 aff
= isl_aff_floor(aff
);
4480 isl_space_free(space
);
4481 ma
= isl_multi_aff_from_aff(aff
);
4483 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4484 ma
= isl_multi_aff_range_product(ma
,
4485 isl_multi_aff_from_aff(aff
));
4488 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4489 map
= isl_map_apply_domain(map
, insert
);
4490 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4491 pma
= isl_pw_multi_aff_from_map(map
);
4492 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4497 /* Is constraint "c" of the form
4499 * e(...) + c1 - m x >= 0
4503 * -e(...) + c2 + m x >= 0
4505 * where m > 1 and e only depends on parameters and input dimemnsions?
4507 * "offset" is the offset of the output dimensions
4508 * "pos" is the position of output dimension x.
4510 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4512 if (isl_int_is_zero(c
[offset
+ d
]))
4514 if (isl_int_is_one(c
[offset
+ d
]))
4516 if (isl_int_is_negone(c
[offset
+ d
]))
4518 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4520 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4521 total
- (offset
+ d
+ 1)) != -1)
4526 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4528 * As a special case, we first check if there is any pair of constraints,
4529 * shared by all the basic maps in "map" that force a given dimension
4530 * to be equal to the floor of some affine combination of the input dimensions.
4532 * In particular, if we can find two constraints
4534 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4538 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4540 * where m > 1 and e only depends on parameters and input dimemnsions,
4543 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4545 * then we know that we can take
4547 * x = floor((e(...) + c1) / m)
4549 * without having to perform any computation.
4551 * Note that we know that
4555 * If c1 + c2 were 0, then we would have detected an equality during
4556 * simplification. If c1 + c2 were negative, then we would have detected
4559 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4560 __isl_take isl_map
*map
)
4566 isl_basic_map
*hull
;
4568 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4573 dim
= isl_map_dim(map
, isl_dim_out
);
4574 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4575 total
= 1 + isl_basic_map_total_dim(hull
);
4577 for (d
= 0; d
< dim
; ++d
) {
4578 for (i
= 0; i
< n
; ++i
) {
4579 if (!is_potential_div_constraint(hull
->ineq
[i
],
4582 for (j
= i
+ 1; j
< n
; ++j
) {
4583 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4584 hull
->ineq
[j
] + 1, total
- 1))
4586 isl_int_add(sum
, hull
->ineq
[i
][0],
4588 if (isl_int_abs_lt(sum
,
4589 hull
->ineq
[i
][offset
+ d
]))
4596 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4598 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4602 isl_basic_map_free(hull
);
4603 return pw_multi_aff_from_map_base(map
);
4606 isl_basic_map_free(hull
);
4610 /* Given an affine expression
4612 * [A -> B] -> f(A,B)
4614 * construct an isl_multi_aff
4618 * such that dimension "d" in B' is set to "aff" and the remaining
4619 * dimensions are set equal to the corresponding dimensions in B.
4620 * "n_in" is the dimension of the space A.
4621 * "n_out" is the dimension of the space B.
4623 * If "is_set" is set, then the affine expression is of the form
4627 * and we construct an isl_multi_aff
4631 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4632 unsigned n_in
, unsigned n_out
, int is_set
)
4636 isl_space
*space
, *space2
;
4637 isl_local_space
*ls
;
4639 space
= isl_aff_get_domain_space(aff
);
4640 ls
= isl_local_space_from_space(isl_space_copy(space
));
4641 space2
= isl_space_copy(space
);
4643 space2
= isl_space_range(isl_space_unwrap(space2
));
4644 space
= isl_space_map_from_domain_and_range(space
, space2
);
4645 ma
= isl_multi_aff_alloc(space
);
4646 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4648 for (i
= 0; i
< n_out
; ++i
) {
4651 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4652 isl_dim_set
, n_in
+ i
);
4653 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4656 isl_local_space_free(ls
);
4661 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4662 * taking into account that the dimension at position "d" can be written as
4664 * x = m a + f(..) (1)
4666 * where m is equal to "gcd".
4667 * "i" is the index of the equality in "hull" that defines f(..).
4668 * In particular, the equality is of the form
4670 * f(..) - x + m g(existentials) = 0
4674 * -f(..) + x + m g(existentials) = 0
4676 * We basically plug (1) into "map", resulting in a map with "a"
4677 * in the range instead of "x". The corresponding isl_pw_multi_aff
4678 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4680 * Specifically, given the input map
4684 * We first wrap it into a set
4688 * and define (1) on top of the corresponding space, resulting in "aff".
4689 * We use this to create an isl_multi_aff that maps the output position "d"
4690 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4691 * We plug this into the wrapped map, unwrap the result and compute the
4692 * corresponding isl_pw_multi_aff.
4693 * The result is an expression
4701 * so that we can plug that into "aff", after extending the latter to
4707 * If "map" is actually a set, then there is no "A" space, meaning
4708 * that we do not need to perform any wrapping, and that the result
4709 * of the recursive call is of the form
4713 * which is plugged into a mapping of the form
4717 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4718 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4723 isl_local_space
*ls
;
4726 isl_pw_multi_aff
*pma
, *id
;
4732 is_set
= isl_map_is_set(map
);
4734 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4735 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4736 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4741 set
= isl_map_wrap(map
);
4742 space
= isl_space_map_from_set(isl_set_get_space(set
));
4743 ma
= isl_multi_aff_identity(space
);
4744 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4745 aff
= isl_aff_alloc(ls
);
4747 isl_int_set_si(aff
->v
->el
[0], 1);
4748 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4749 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4752 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4754 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4756 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4757 set
= isl_set_preimage_multi_aff(set
, ma
);
4759 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4764 map
= isl_set_unwrap(set
);
4765 pma
= isl_pw_multi_aff_from_map(set
);
4768 space
= isl_pw_multi_aff_get_domain_space(pma
);
4769 space
= isl_space_map_from_set(space
);
4770 id
= isl_pw_multi_aff_identity(space
);
4771 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4773 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4774 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4776 isl_basic_map_free(hull
);
4780 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4782 * As a special case, we first check if all output dimensions are uniquely
4783 * defined in terms of the parameters and input dimensions over the entire
4784 * domain. If so, we extract the desired isl_pw_multi_aff directly
4785 * from the affine hull of "map" and its domain.
4787 * Otherwise, we check if any of the output dimensions is "strided".
4788 * That is, we check if can be written as
4792 * with m greater than 1, a some combination of existentiall quantified
4793 * variables and f and expression in the parameters and input dimensions.
4794 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4796 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4799 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4803 isl_basic_map
*hull
;
4813 hull
= isl_map_affine_hull(isl_map_copy(map
));
4814 sv
= isl_basic_map_plain_is_single_valued(hull
);
4816 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4818 hull
= isl_basic_map_free(hull
);
4822 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4823 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4826 isl_basic_map_free(hull
);
4827 return pw_multi_aff_from_map_check_div(map
);
4832 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4833 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4835 for (i
= 0; i
< n_out
; ++i
) {
4836 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4837 isl_int
*eq
= hull
->eq
[j
];
4838 isl_pw_multi_aff
*res
;
4840 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4841 !isl_int_is_negone(eq
[o_out
+ i
]))
4843 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4845 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4846 n_out
- (i
+ 1)) != -1)
4848 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4849 if (isl_int_is_zero(gcd
))
4851 if (isl_int_is_one(gcd
))
4854 res
= pw_multi_aff_from_map_stride(map
, hull
,
4862 isl_basic_map_free(hull
);
4863 return pw_multi_aff_from_map_check_div(map
);
4869 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4871 return isl_pw_multi_aff_from_map(set
);
4874 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4877 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4879 isl_union_pw_multi_aff
**upma
= user
;
4880 isl_pw_multi_aff
*pma
;
4882 pma
= isl_pw_multi_aff_from_map(map
);
4883 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4885 return *upma
? 0 : -1;
4888 /* Try and create an isl_union_pw_multi_aff that is equivalent
4889 * to the given isl_union_map.
4890 * The isl_union_map is required to be single-valued in each space.
4891 * Otherwise, an error is produced.
4893 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4894 __isl_take isl_union_map
*umap
)
4897 isl_union_pw_multi_aff
*upma
;
4899 space
= isl_union_map_get_space(umap
);
4900 upma
= isl_union_pw_multi_aff_empty(space
);
4901 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4902 upma
= isl_union_pw_multi_aff_free(upma
);
4903 isl_union_map_free(umap
);
4908 /* Try and create an isl_union_pw_multi_aff that is equivalent
4909 * to the given isl_union_set.
4910 * The isl_union_set is required to be a singleton in each space.
4911 * Otherwise, an error is produced.
4913 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4914 __isl_take isl_union_set
*uset
)
4916 return isl_union_pw_multi_aff_from_union_map(uset
);
4919 /* Return the piecewise affine expression "set ? 1 : 0".
4921 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4924 isl_space
*space
= isl_set_get_space(set
);
4925 isl_local_space
*ls
= isl_local_space_from_space(space
);
4926 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4927 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4929 one
= isl_aff_add_constant_si(one
, 1);
4930 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4931 set
= isl_set_complement(set
);
4932 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4937 /* Plug in "subs" for dimension "type", "pos" of "aff".
4939 * Let i be the dimension to replace and let "subs" be of the form
4943 * and "aff" of the form
4949 * (a f + d g')/(m d)
4951 * where g' is the result of plugging in "subs" in each of the integer
4954 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
4955 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
4960 aff
= isl_aff_cow(aff
);
4962 return isl_aff_free(aff
);
4964 ctx
= isl_aff_get_ctx(aff
);
4965 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
4966 isl_die(ctx
, isl_error_invalid
,
4967 "spaces don't match", return isl_aff_free(aff
));
4968 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
4969 isl_die(ctx
, isl_error_unsupported
,
4970 "cannot handle divs yet", return isl_aff_free(aff
));
4972 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
4974 return isl_aff_free(aff
);
4976 aff
->v
= isl_vec_cow(aff
->v
);
4978 return isl_aff_free(aff
);
4980 pos
+= isl_local_space_offset(aff
->ls
, type
);
4983 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
4984 aff
->v
->size
, subs
->v
->size
, v
);
4990 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4991 * expressions in "maff".
4993 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
4994 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
4995 __isl_keep isl_aff
*subs
)
4999 maff
= isl_multi_aff_cow(maff
);
5001 return isl_multi_aff_free(maff
);
5003 if (type
== isl_dim_in
)
5006 for (i
= 0; i
< maff
->n
; ++i
) {
5007 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5009 return isl_multi_aff_free(maff
);
5015 /* Plug in "subs" for dimension "type", "pos" of "pma".
5017 * pma is of the form
5021 * while subs is of the form
5023 * v' = B_j(v) -> S_j
5025 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5026 * has a contribution in the result, in particular
5028 * C_ij(S_j) -> M_i(S_j)
5030 * Note that plugging in S_j in C_ij may also result in an empty set
5031 * and this contribution should simply be discarded.
5033 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5034 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5035 __isl_keep isl_pw_aff
*subs
)
5038 isl_pw_multi_aff
*res
;
5041 return isl_pw_multi_aff_free(pma
);
5043 n
= pma
->n
* subs
->n
;
5044 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5046 for (i
= 0; i
< pma
->n
; ++i
) {
5047 for (j
= 0; j
< subs
->n
; ++j
) {
5049 isl_multi_aff
*res_ij
;
5052 common
= isl_set_intersect(
5053 isl_set_copy(pma
->p
[i
].set
),
5054 isl_set_copy(subs
->p
[j
].set
));
5055 common
= isl_set_substitute(common
,
5056 type
, pos
, subs
->p
[j
].aff
);
5057 empty
= isl_set_plain_is_empty(common
);
5058 if (empty
< 0 || empty
) {
5059 isl_set_free(common
);
5065 res_ij
= isl_multi_aff_substitute(
5066 isl_multi_aff_copy(pma
->p
[i
].maff
),
5067 type
, pos
, subs
->p
[j
].aff
);
5069 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5073 isl_pw_multi_aff_free(pma
);
5076 isl_pw_multi_aff_free(pma
);
5077 isl_pw_multi_aff_free(res
);
5081 /* Compute the preimage of a range of dimensions in the affine expression "src"
5082 * under "ma" and put the result in "dst". The number of dimensions in "src"
5083 * that precede the range is given by "n_before". The number of dimensions
5084 * in the range is given by the number of output dimensions of "ma".
5085 * The number of dimensions that follow the range is given by "n_after".
5086 * If "has_denom" is set (to one),
5087 * then "src" and "dst" have an extra initial denominator.
5088 * "n_div_ma" is the number of existentials in "ma"
5089 * "n_div_bset" is the number of existentials in "src"
5090 * The resulting "dst" (which is assumed to have been allocated by
5091 * the caller) contains coefficients for both sets of existentials,
5092 * first those in "ma" and then those in "src".
5093 * f, c1, c2 and g are temporary objects that have been initialized
5096 * Let src represent the expression
5098 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5100 * and let ma represent the expressions
5102 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5104 * We start out with the following expression for dst:
5106 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5108 * with the multiplication factor f initially equal to 1
5109 * and f \sum_i b_i v_i kept separately.
5110 * For each x_i that we substitute, we multiply the numerator
5111 * (and denominator) of dst by c_1 = m_i and add the numerator
5112 * of the x_i expression multiplied by c_2 = f b_i,
5113 * after removing the common factors of c_1 and c_2.
5114 * The multiplication factor f also needs to be multiplied by c_1
5115 * for the next x_j, j > i.
5117 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5118 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5119 int n_div_ma
, int n_div_bmap
,
5120 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5123 int n_param
, n_in
, n_out
;
5126 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5127 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5128 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5130 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5131 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5132 isl_seq_clr(dst
+ o_dst
, n_in
);
5135 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5138 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5140 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5142 isl_int_set_si(f
, 1);
5144 for (i
= 0; i
< n_out
; ++i
) {
5145 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5147 if (isl_int_is_zero(src
[offset
]))
5149 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5150 isl_int_mul(c2
, f
, src
[offset
]);
5151 isl_int_gcd(g
, c1
, c2
);
5152 isl_int_divexact(c1
, c1
, g
);
5153 isl_int_divexact(c2
, c2
, g
);
5155 isl_int_mul(f
, f
, c1
);
5158 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5159 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5160 o_dst
+= 1 + n_param
;
5161 o_src
+= 1 + n_param
;
5162 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5164 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5165 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5168 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5170 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5171 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5174 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5176 isl_int_mul(dst
[0], dst
[0], c1
);
5180 /* Compute the pullback of "aff" by the function represented by "ma".
5181 * In other words, plug in "ma" in "aff". The result is an affine expression
5182 * defined over the domain space of "ma".
5184 * If "aff" is represented by
5186 * (a(p) + b x + c(divs))/d
5188 * and ma is represented by
5190 * x = D(p) + F(y) + G(divs')
5192 * then the result is
5194 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5196 * The divs in the local space of the input are similarly adjusted
5197 * through a call to isl_local_space_preimage_multi_aff.
5199 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5200 __isl_take isl_multi_aff
*ma
)
5202 isl_aff
*res
= NULL
;
5203 isl_local_space
*ls
;
5204 int n_div_aff
, n_div_ma
;
5205 isl_int f
, c1
, c2
, g
;
5207 ma
= isl_multi_aff_align_divs(ma
);
5211 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5212 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5214 ls
= isl_aff_get_domain_local_space(aff
);
5215 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5216 res
= isl_aff_alloc(ls
);
5225 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5234 isl_multi_aff_free(ma
);
5235 res
= isl_aff_normalize(res
);
5239 isl_multi_aff_free(ma
);
5244 /* Compute the pullback of "aff1" by the function represented by "aff2".
5245 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5246 * defined over the domain space of "aff1".
5248 * The domain of "aff1" should match the range of "aff2", which means
5249 * that it should be single-dimensional.
5251 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5252 __isl_take isl_aff
*aff2
)
5256 ma
= isl_multi_aff_from_aff(aff2
);
5257 return isl_aff_pullback_multi_aff(aff1
, ma
);
5260 /* Compute the pullback of "ma1" by the function represented by "ma2".
5261 * In other words, plug in "ma2" in "ma1".
5263 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5265 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5266 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5269 isl_space
*space
= NULL
;
5271 ma2
= isl_multi_aff_align_divs(ma2
);
5272 ma1
= isl_multi_aff_cow(ma1
);
5276 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5277 isl_multi_aff_get_space(ma1
));
5279 for (i
= 0; i
< ma1
->n
; ++i
) {
5280 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5281 isl_multi_aff_copy(ma2
));
5286 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5287 isl_multi_aff_free(ma2
);
5290 isl_space_free(space
);
5291 isl_multi_aff_free(ma2
);
5292 isl_multi_aff_free(ma1
);
5296 /* Compute the pullback of "ma1" by the function represented by "ma2".
5297 * In other words, plug in "ma2" in "ma1".
5299 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5300 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5302 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5303 &isl_multi_aff_pullback_multi_aff_aligned
);
5306 /* Extend the local space of "dst" to include the divs
5307 * in the local space of "src".
5309 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5310 __isl_keep isl_aff
*src
)
5318 return isl_aff_free(dst
);
5320 ctx
= isl_aff_get_ctx(src
);
5321 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5322 isl_die(ctx
, isl_error_invalid
,
5323 "spaces don't match", goto error
);
5325 if (src
->ls
->div
->n_row
== 0)
5328 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5329 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5330 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5333 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5334 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5342 return isl_aff_free(dst
);
5345 /* Adjust the local spaces of the affine expressions in "maff"
5346 * such that they all have the save divs.
5348 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5349 __isl_take isl_multi_aff
*maff
)
5357 maff
= isl_multi_aff_cow(maff
);
5361 for (i
= 1; i
< maff
->n
; ++i
)
5362 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5363 for (i
= 1; i
< maff
->n
; ++i
) {
5364 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5366 return isl_multi_aff_free(maff
);
5372 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5374 aff
= isl_aff_cow(aff
);
5378 aff
->ls
= isl_local_space_lift(aff
->ls
);
5380 return isl_aff_free(aff
);
5385 /* Lift "maff" to a space with extra dimensions such that the result
5386 * has no more existentially quantified variables.
5387 * If "ls" is not NULL, then *ls is assigned the local space that lies
5388 * at the basis of the lifting applied to "maff".
5390 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5391 __isl_give isl_local_space
**ls
)
5405 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5406 *ls
= isl_local_space_from_space(space
);
5408 return isl_multi_aff_free(maff
);
5413 maff
= isl_multi_aff_cow(maff
);
5414 maff
= isl_multi_aff_align_divs(maff
);
5418 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5419 space
= isl_multi_aff_get_space(maff
);
5420 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5421 space
= isl_space_extend_domain_with_range(space
,
5422 isl_multi_aff_get_space(maff
));
5424 return isl_multi_aff_free(maff
);
5425 isl_space_free(maff
->space
);
5426 maff
->space
= space
;
5429 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5431 return isl_multi_aff_free(maff
);
5434 for (i
= 0; i
< maff
->n
; ++i
) {
5435 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5443 isl_local_space_free(*ls
);
5444 return isl_multi_aff_free(maff
);
5448 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5450 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5451 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5461 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5462 if (pos
< 0 || pos
>= n_out
)
5463 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5464 "index out of bounds", return NULL
);
5466 space
= isl_pw_multi_aff_get_space(pma
);
5467 space
= isl_space_drop_dims(space
, isl_dim_out
,
5468 pos
+ 1, n_out
- pos
- 1);
5469 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5471 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5472 for (i
= 0; i
< pma
->n
; ++i
) {
5474 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5475 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5481 /* Return an isl_pw_multi_aff with the given "set" as domain and
5482 * an unnamed zero-dimensional range.
5484 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5485 __isl_take isl_set
*set
)
5490 space
= isl_set_get_space(set
);
5491 space
= isl_space_from_domain(space
);
5492 ma
= isl_multi_aff_zero(space
);
5493 return isl_pw_multi_aff_alloc(set
, ma
);
5496 /* Add an isl_pw_multi_aff with the given "set" as domain and
5497 * an unnamed zero-dimensional range to *user.
5499 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5501 isl_union_pw_multi_aff
**upma
= user
;
5502 isl_pw_multi_aff
*pma
;
5504 pma
= isl_pw_multi_aff_from_domain(set
);
5505 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5510 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5511 * an unnamed zero-dimensional range.
5513 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5514 __isl_take isl_union_set
*uset
)
5517 isl_union_pw_multi_aff
*upma
;
5522 space
= isl_union_set_get_space(uset
);
5523 upma
= isl_union_pw_multi_aff_empty(space
);
5525 if (isl_union_set_foreach_set(uset
,
5526 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5529 isl_union_set_free(uset
);
5532 isl_union_set_free(uset
);
5533 isl_union_pw_multi_aff_free(upma
);
5537 /* Convert "pma" to an isl_map and add it to *umap.
5539 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5541 isl_union_map
**umap
= user
;
5544 map
= isl_map_from_pw_multi_aff(pma
);
5545 *umap
= isl_union_map_add_map(*umap
, map
);
5550 /* Construct a union map mapping the domain of the union
5551 * piecewise multi-affine expression to its range, with each dimension
5552 * in the range equated to the corresponding affine expression on its cell.
5554 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5555 __isl_take isl_union_pw_multi_aff
*upma
)
5558 isl_union_map
*umap
;
5563 space
= isl_union_pw_multi_aff_get_space(upma
);
5564 umap
= isl_union_map_empty(space
);
5566 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5567 &map_from_pw_multi_aff
, &umap
) < 0)
5570 isl_union_pw_multi_aff_free(upma
);
5573 isl_union_pw_multi_aff_free(upma
);
5574 isl_union_map_free(umap
);
5578 /* Local data for bin_entry and the callback "fn".
5580 struct isl_union_pw_multi_aff_bin_data
{
5581 isl_union_pw_multi_aff
*upma2
;
5582 isl_union_pw_multi_aff
*res
;
5583 isl_pw_multi_aff
*pma
;
5584 int (*fn
)(void **entry
, void *user
);
5587 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5588 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5590 static int bin_entry(void **entry
, void *user
)
5592 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5593 isl_pw_multi_aff
*pma
= *entry
;
5596 if (isl_hash_table_foreach(data
->upma2
->space
->ctx
, &data
->upma2
->table
,
5597 data
->fn
, data
) < 0)
5603 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5604 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5605 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5606 * as *entry. The callback should adjust data->res if desired.
5608 static __isl_give isl_union_pw_multi_aff
*bin_op(
5609 __isl_take isl_union_pw_multi_aff
*upma1
,
5610 __isl_take isl_union_pw_multi_aff
*upma2
,
5611 int (*fn
)(void **entry
, void *user
))
5614 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5616 space
= isl_union_pw_multi_aff_get_space(upma2
);
5617 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5618 space
= isl_union_pw_multi_aff_get_space(upma1
);
5619 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5621 if (!upma1
|| !upma2
)
5625 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->space
),
5627 if (isl_hash_table_foreach(upma1
->space
->ctx
, &upma1
->table
,
5628 &bin_entry
, &data
) < 0)
5631 isl_union_pw_multi_aff_free(upma1
);
5632 isl_union_pw_multi_aff_free(upma2
);
5635 isl_union_pw_multi_aff_free(upma1
);
5636 isl_union_pw_multi_aff_free(upma2
);
5637 isl_union_pw_multi_aff_free(data
.res
);
5641 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5642 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5644 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5645 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5649 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5650 isl_pw_multi_aff_get_space(pma2
));
5651 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5652 &isl_multi_aff_range_product
);
5655 /* Given two isl_pw_multi_affs A -> B and C -> D,
5656 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5658 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5659 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5661 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5662 &pw_multi_aff_range_product
);
5665 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5666 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5668 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5669 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5673 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5674 isl_pw_multi_aff_get_space(pma2
));
5675 space
= isl_space_flatten_range(space
);
5676 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5677 &isl_multi_aff_flat_range_product
);
5680 /* Given two isl_pw_multi_affs A -> B and C -> D,
5681 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5683 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5684 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5686 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5687 &pw_multi_aff_flat_range_product
);
5690 /* If data->pma and *entry have the same domain space, then compute
5691 * their flat range product and the result to data->res.
5693 static int flat_range_product_entry(void **entry
, void *user
)
5695 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5696 isl_pw_multi_aff
*pma2
= *entry
;
5698 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5699 pma2
->dim
, isl_dim_in
))
5702 pma2
= isl_pw_multi_aff_flat_range_product(
5703 isl_pw_multi_aff_copy(data
->pma
),
5704 isl_pw_multi_aff_copy(pma2
));
5706 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5711 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5712 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5714 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5715 __isl_take isl_union_pw_multi_aff
*upma1
,
5716 __isl_take isl_union_pw_multi_aff
*upma2
)
5718 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5721 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5722 * The parameters are assumed to have been aligned.
5724 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5725 * except that it works on two different isl_pw_* types.
5727 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5728 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5729 __isl_take isl_pw_aff
*pa
)
5732 isl_pw_multi_aff
*res
= NULL
;
5737 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5738 pa
->dim
, isl_dim_in
))
5739 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5740 "domains don't match", goto error
);
5741 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5742 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5743 "index out of bounds", goto error
);
5746 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5748 for (i
= 0; i
< pma
->n
; ++i
) {
5749 for (j
= 0; j
< pa
->n
; ++j
) {
5751 isl_multi_aff
*res_ij
;
5754 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5755 isl_set_copy(pa
->p
[j
].set
));
5756 empty
= isl_set_plain_is_empty(common
);
5757 if (empty
< 0 || empty
) {
5758 isl_set_free(common
);
5764 res_ij
= isl_multi_aff_set_aff(
5765 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5766 isl_aff_copy(pa
->p
[j
].aff
));
5767 res_ij
= isl_multi_aff_gist(res_ij
,
5768 isl_set_copy(common
));
5770 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5774 isl_pw_multi_aff_free(pma
);
5775 isl_pw_aff_free(pa
);
5778 isl_pw_multi_aff_free(pma
);
5779 isl_pw_aff_free(pa
);
5780 return isl_pw_multi_aff_free(res
);
5783 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5785 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5786 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5787 __isl_take isl_pw_aff
*pa
)
5791 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5792 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5793 if (!isl_space_has_named_params(pma
->dim
) ||
5794 !isl_space_has_named_params(pa
->dim
))
5795 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5796 "unaligned unnamed parameters", goto error
);
5797 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5798 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5799 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5801 isl_pw_multi_aff_free(pma
);
5802 isl_pw_aff_free(pa
);
5806 /* Do the parameters of "pa" match those of "space"?
5808 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5809 __isl_keep isl_space
*space
)
5811 isl_space
*pa_space
;
5817 pa_space
= isl_pw_aff_get_space(pa
);
5819 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5821 isl_space_free(pa_space
);
5825 /* Check that the domain space of "pa" matches "space".
5827 * Return 0 on success and -1 on error.
5829 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5830 __isl_keep isl_space
*space
)
5832 isl_space
*pa_space
;
5838 pa_space
= isl_pw_aff_get_space(pa
);
5840 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5844 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5845 "parameters don't match", goto error
);
5846 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
5847 pa_space
, isl_dim_in
);
5851 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5852 "domains don't match", goto error
);
5853 isl_space_free(pa_space
);
5856 isl_space_free(pa_space
);
5863 #include <isl_multi_templ.c>
5865 /* Scale the elements of "pma" by the corresponding elements of "mv".
5867 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5868 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5872 pma
= isl_pw_multi_aff_cow(pma
);
5875 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5876 mv
->space
, isl_dim_set
))
5877 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5878 "spaces don't match", goto error
);
5879 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5880 mv
->space
, isl_dim_param
)) {
5881 pma
= isl_pw_multi_aff_align_params(pma
,
5882 isl_multi_val_get_space(mv
));
5883 mv
= isl_multi_val_align_params(mv
,
5884 isl_pw_multi_aff_get_space(pma
));
5889 for (i
= 0; i
< pma
->n
; ++i
) {
5890 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5891 isl_multi_val_copy(mv
));
5892 if (!pma
->p
[i
].maff
)
5896 isl_multi_val_free(mv
);
5899 isl_multi_val_free(mv
);
5900 isl_pw_multi_aff_free(pma
);
5904 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5905 * mv contains the mv argument.
5906 * res collects the results.
5908 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5910 isl_union_pw_multi_aff
*res
;
5913 /* This function is called for each entry of an isl_union_pw_multi_aff.
5914 * If the space of the entry matches that of data->mv,
5915 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5918 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5920 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5921 isl_pw_multi_aff
*pma
= *entry
;
5925 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5926 data
->mv
->space
, isl_dim_set
))
5929 pma
= isl_pw_multi_aff_copy(pma
);
5930 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5931 isl_multi_val_copy(data
->mv
));
5932 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
5939 /* Scale the elements of "upma" by the corresponding elements of "mv",
5940 * for those entries that match the space of "mv".
5942 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
5943 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
5945 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
5947 upma
= isl_union_pw_multi_aff_align_params(upma
,
5948 isl_multi_val_get_space(mv
));
5949 mv
= isl_multi_val_align_params(mv
,
5950 isl_union_pw_multi_aff_get_space(upma
));
5955 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->space
),
5957 if (isl_hash_table_foreach(upma
->space
->ctx
, &upma
->table
,
5958 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
5961 isl_multi_val_free(mv
);
5962 isl_union_pw_multi_aff_free(upma
);
5965 isl_multi_val_free(mv
);
5966 isl_union_pw_multi_aff_free(upma
);
5970 /* Construct and return a piecewise multi affine expression
5971 * in the given space with value zero in each of the output dimensions and
5972 * a universe domain.
5974 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
5976 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
5979 /* Construct and return a piecewise multi affine expression
5980 * that is equal to the given piecewise affine expression.
5982 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
5983 __isl_take isl_pw_aff
*pa
)
5987 isl_pw_multi_aff
*pma
;
5992 space
= isl_pw_aff_get_space(pa
);
5993 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
5995 for (i
= 0; i
< pa
->n
; ++i
) {
5999 set
= isl_set_copy(pa
->p
[i
].set
);
6000 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6001 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6004 isl_pw_aff_free(pa
);
6008 /* Construct a set or map mapping the shared (parameter) domain
6009 * of the piecewise affine expressions to the range of "mpa"
6010 * with each dimension in the range equated to the
6011 * corresponding piecewise affine expression.
6013 static __isl_give isl_map
*map_from_multi_pw_aff(
6014 __isl_take isl_multi_pw_aff
*mpa
)
6023 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6024 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6025 "invalid space", goto error
);
6027 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6028 map
= isl_map_universe(isl_space_from_domain(space
));
6030 for (i
= 0; i
< mpa
->n
; ++i
) {
6034 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6035 map_i
= map_from_pw_aff(pa
);
6037 map
= isl_map_flat_range_product(map
, map_i
);
6040 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6042 isl_multi_pw_aff_free(mpa
);
6045 isl_multi_pw_aff_free(mpa
);
6049 /* Construct a map mapping the shared domain
6050 * of the piecewise affine expressions to the range of "mpa"
6051 * with each dimension in the range equated to the
6052 * corresponding piecewise affine expression.
6054 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6058 if (isl_space_is_set(mpa
->space
))
6059 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6060 "space of input is not a map", goto error
);
6062 return map_from_multi_pw_aff(mpa
);
6064 isl_multi_pw_aff_free(mpa
);
6068 /* Construct a set mapping the shared parameter domain
6069 * of the piecewise affine expressions to the space of "mpa"
6070 * with each dimension in the range equated to the
6071 * corresponding piecewise affine expression.
6073 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6077 if (!isl_space_is_set(mpa
->space
))
6078 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6079 "space of input is not a set", goto error
);
6081 return map_from_multi_pw_aff(mpa
);
6083 isl_multi_pw_aff_free(mpa
);
6087 /* Construct and return a piecewise multi affine expression
6088 * that is equal to the given multi piecewise affine expression
6089 * on the shared domain of the piecewise affine expressions.
6091 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6092 __isl_take isl_multi_pw_aff
*mpa
)
6097 isl_pw_multi_aff
*pma
;
6102 space
= isl_multi_pw_aff_get_space(mpa
);
6105 isl_multi_pw_aff_free(mpa
);
6106 return isl_pw_multi_aff_zero(space
);
6109 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6110 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6112 for (i
= 1; i
< mpa
->n
; ++i
) {
6113 isl_pw_multi_aff
*pma_i
;
6115 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6116 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6117 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6120 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6122 isl_multi_pw_aff_free(mpa
);
6126 /* Construct and return a multi piecewise affine expression
6127 * that is equal to the given multi affine expression.
6129 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6130 __isl_take isl_multi_aff
*ma
)
6133 isl_multi_pw_aff
*mpa
;
6138 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6139 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6141 for (i
= 0; i
< n
; ++i
) {
6144 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6145 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6148 isl_multi_aff_free(ma
);
6152 /* Construct and return a multi piecewise affine expression
6153 * that is equal to the given piecewise multi affine expression.
6155 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6156 __isl_take isl_pw_multi_aff
*pma
)
6160 isl_multi_pw_aff
*mpa
;
6165 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6166 space
= isl_pw_multi_aff_get_space(pma
);
6167 mpa
= isl_multi_pw_aff_alloc(space
);
6169 for (i
= 0; i
< n
; ++i
) {
6172 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6173 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6176 isl_pw_multi_aff_free(pma
);
6180 /* Do "pa1" and "pa2" represent the same function?
6182 * We first check if they are obviously equal.
6183 * If not, we convert them to maps and check if those are equal.
6185 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6188 isl_map
*map1
, *map2
;
6193 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6194 if (equal
< 0 || equal
)
6197 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6198 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6199 equal
= isl_map_is_equal(map1
, map2
);
6206 /* Do "mpa1" and "mpa2" represent the same function?
6208 * Note that we cannot convert the entire isl_multi_pw_aff
6209 * to a map because the domains of the piecewise affine expressions
6210 * may not be the same.
6212 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6213 __isl_keep isl_multi_pw_aff
*mpa2
)
6221 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6222 mpa2
->space
, isl_dim_param
)) {
6223 if (!isl_space_has_named_params(mpa1
->space
))
6225 if (!isl_space_has_named_params(mpa2
->space
))
6227 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6228 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6229 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6230 isl_multi_pw_aff_get_space(mpa2
));
6231 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6232 isl_multi_pw_aff_get_space(mpa1
));
6233 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6234 isl_multi_pw_aff_free(mpa1
);
6235 isl_multi_pw_aff_free(mpa2
);
6239 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6240 if (equal
< 0 || !equal
)
6243 for (i
= 0; i
< mpa1
->n
; ++i
) {
6244 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6245 if (equal
< 0 || !equal
)
6252 /* Coalesce the elements of "mpa".
6254 * Note that such coalescing does not change the meaning of "mpa"
6255 * so there is no need to cow. We do need to be careful not to
6256 * destroy any other copies of "mpa" in case of failure.
6258 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
6259 __isl_take isl_multi_pw_aff
*mpa
)
6266 for (i
= 0; i
< mpa
->n
; ++i
) {
6267 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6268 pa
= isl_pw_aff_coalesce(pa
);
6270 return isl_multi_pw_aff_free(mpa
);
6271 isl_pw_aff_free(mpa
->p
[i
]);
6278 /* Compute the pullback of "mpa" by the function represented by "ma".
6279 * In other words, plug in "ma" in "mpa".
6281 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6283 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6284 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6287 isl_space
*space
= NULL
;
6289 mpa
= isl_multi_pw_aff_cow(mpa
);
6293 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6294 isl_multi_pw_aff_get_space(mpa
));
6298 for (i
= 0; i
< mpa
->n
; ++i
) {
6299 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6300 isl_multi_aff_copy(ma
));
6305 isl_multi_aff_free(ma
);
6306 isl_space_free(mpa
->space
);
6310 isl_space_free(space
);
6311 isl_multi_pw_aff_free(mpa
);
6312 isl_multi_aff_free(ma
);
6316 /* Compute the pullback of "mpa" by the function represented by "ma".
6317 * In other words, plug in "ma" in "mpa".
6319 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6320 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6324 if (isl_space_match(mpa
->space
, isl_dim_param
,
6325 ma
->space
, isl_dim_param
))
6326 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6327 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6328 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6329 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6331 isl_multi_pw_aff_free(mpa
);
6332 isl_multi_aff_free(ma
);
6336 /* Compute the pullback of "mpa" by the function represented by "pma".
6337 * In other words, plug in "pma" in "mpa".
6339 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6341 static __isl_give isl_multi_pw_aff
*
6342 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6343 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6346 isl_space
*space
= NULL
;
6348 mpa
= isl_multi_pw_aff_cow(mpa
);
6352 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6353 isl_multi_pw_aff_get_space(mpa
));
6355 for (i
= 0; i
< mpa
->n
; ++i
) {
6356 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6357 isl_pw_multi_aff_copy(pma
));
6362 isl_pw_multi_aff_free(pma
);
6363 isl_space_free(mpa
->space
);
6367 isl_space_free(space
);
6368 isl_multi_pw_aff_free(mpa
);
6369 isl_pw_multi_aff_free(pma
);
6373 /* Compute the pullback of "mpa" by the function represented by "pma".
6374 * In other words, plug in "pma" in "mpa".
6376 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6377 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6381 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6382 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6383 mpa
= isl_multi_pw_aff_align_params(mpa
,
6384 isl_pw_multi_aff_get_space(pma
));
6385 pma
= isl_pw_multi_aff_align_params(pma
,
6386 isl_multi_pw_aff_get_space(mpa
));
6387 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6389 isl_multi_pw_aff_free(mpa
);
6390 isl_pw_multi_aff_free(pma
);
6394 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6395 * with the domain of "aff". The domain of the result is the same
6397 * "mpa" and "aff" are assumed to have been aligned.
6399 * We first extract the parametric constant from "aff", defined
6400 * over the correct domain.
6401 * Then we add the appropriate combinations of the members of "mpa".
6402 * Finally, we add the integer divisions through recursive calls.
6404 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6405 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6407 int i
, n_param
, n_in
, n_div
;
6413 n_param
= isl_aff_dim(aff
, isl_dim_param
);
6414 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6415 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6417 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6418 tmp
= isl_aff_copy(aff
);
6419 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6420 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6421 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6422 isl_space_dim(space
, isl_dim_set
));
6423 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6424 pa
= isl_pw_aff_from_aff(tmp
);
6426 for (i
= 0; i
< n_in
; ++i
) {
6429 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6431 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6432 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6433 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6434 pa
= isl_pw_aff_add(pa
, pa_i
);
6437 for (i
= 0; i
< n_div
; ++i
) {
6441 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6443 div
= isl_aff_get_div(aff
, i
);
6444 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6445 isl_multi_pw_aff_copy(mpa
), div
);
6446 pa_i
= isl_pw_aff_floor(pa_i
);
6447 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6448 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6449 pa
= isl_pw_aff_add(pa
, pa_i
);
6452 isl_multi_pw_aff_free(mpa
);
6458 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6459 * with the domain of "aff". The domain of the result is the same
6462 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6463 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6467 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6468 mpa
->space
, isl_dim_param
))
6469 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6471 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6472 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6474 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6477 isl_multi_pw_aff_free(mpa
);
6481 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6482 * with the domain of "pa". The domain of the result is the same
6484 * "mpa" and "pa" are assumed to have been aligned.
6486 * We consider each piece in turn. Note that the domains of the
6487 * pieces are assumed to be disjoint and they remain disjoint
6488 * after taking the preimage (over the same function).
6490 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6491 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6500 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6501 isl_pw_aff_get_space(pa
));
6502 res
= isl_pw_aff_empty(space
);
6504 for (i
= 0; i
< pa
->n
; ++i
) {
6508 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6509 isl_multi_pw_aff_copy(mpa
),
6510 isl_aff_copy(pa
->p
[i
].aff
));
6511 domain
= isl_set_copy(pa
->p
[i
].set
);
6512 domain
= isl_set_preimage_multi_pw_aff(domain
,
6513 isl_multi_pw_aff_copy(mpa
));
6514 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6515 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6518 isl_pw_aff_free(pa
);
6519 isl_multi_pw_aff_free(mpa
);
6522 isl_pw_aff_free(pa
);
6523 isl_multi_pw_aff_free(mpa
);
6527 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6528 * with the domain of "pa". The domain of the result is the same
6531 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6532 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6536 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6537 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6539 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6540 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6542 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6544 isl_pw_aff_free(pa
);
6545 isl_multi_pw_aff_free(mpa
);
6549 /* Compute the pullback of "pa" by the function represented by "mpa".
6550 * In other words, plug in "mpa" in "pa".
6551 * "pa" and "mpa" are assumed to have been aligned.
6553 * The pullback is computed by applying "pa" to "mpa".
6555 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6556 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6558 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6561 /* Compute the pullback of "pa" by the function represented by "mpa".
6562 * In other words, plug in "mpa" in "pa".
6564 * The pullback is computed by applying "pa" to "mpa".
6566 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6567 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6569 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6572 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6573 * In other words, plug in "mpa2" in "mpa1".
6575 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6577 * We pullback each member of "mpa1" in turn.
6579 static __isl_give isl_multi_pw_aff
*
6580 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6581 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6584 isl_space
*space
= NULL
;
6586 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6590 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6591 isl_multi_pw_aff_get_space(mpa1
));
6593 for (i
= 0; i
< mpa1
->n
; ++i
) {
6594 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6595 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6600 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6602 isl_multi_pw_aff_free(mpa2
);
6605 isl_space_free(space
);
6606 isl_multi_pw_aff_free(mpa1
);
6607 isl_multi_pw_aff_free(mpa2
);
6611 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6612 * In other words, plug in "mpa2" in "mpa1".
6614 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6615 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6617 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6618 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6621 /* Compare two isl_affs.
6623 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6624 * than "aff2" and 0 if they are equal.
6626 * The order is fairly arbitrary. We do consider expressions that only involve
6627 * earlier dimensions as "smaller".
6629 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6642 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6646 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6647 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6649 return last1
- last2
;
6651 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
6654 /* Compare two isl_pw_affs.
6656 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6657 * than "pa2" and 0 if they are equal.
6659 * The order is fairly arbitrary. We do consider expressions that only involve
6660 * earlier dimensions as "smaller".
6662 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
6663 __isl_keep isl_pw_aff
*pa2
)
6676 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
6680 if (pa1
->n
!= pa2
->n
)
6681 return pa1
->n
- pa2
->n
;
6683 for (i
= 0; i
< pa1
->n
; ++i
) {
6684 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
6687 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
6695 /* Return a piecewise affine expression that is equal to "v" on "domain".
6697 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
6698 __isl_take isl_val
*v
)
6701 isl_local_space
*ls
;
6704 space
= isl_set_get_space(domain
);
6705 ls
= isl_local_space_from_space(space
);
6706 aff
= isl_aff_val_on_domain(ls
, v
);
6708 return isl_pw_aff_alloc(domain
, aff
);
6711 /* Return a multi affine expression that is equal to "mv" on domain
6714 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
6715 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
6719 isl_local_space
*ls
;
6725 n
= isl_multi_val_dim(mv
, isl_dim_set
);
6726 space2
= isl_multi_val_get_space(mv
);
6727 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
6728 space
= isl_space_align_params(space
, isl_space_copy(space2
));
6729 space
= isl_space_map_from_domain_and_range(space
, space2
);
6730 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
6731 ls
= isl_local_space_from_space(isl_space_domain(space
));
6732 for (i
= 0; i
< n
; ++i
) {
6736 v
= isl_multi_val_get_val(mv
, i
);
6737 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
6738 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6740 isl_local_space_free(ls
);
6742 isl_multi_val_free(mv
);
6745 isl_space_free(space
);
6746 isl_multi_val_free(mv
);
6750 /* Return a piecewise multi-affine expression
6751 * that is equal to "mv" on "domain".
6753 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
6754 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
6759 space
= isl_set_get_space(domain
);
6760 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
6762 return isl_pw_multi_aff_alloc(domain
, ma
);
6765 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6766 * mv is the value that should be attained on each domain set
6767 * res collects the results
6769 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
6771 isl_union_pw_multi_aff
*res
;
6774 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6775 * and add it to data->res.
6777 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
6780 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
6781 isl_pw_multi_aff
*pma
;
6784 mv
= isl_multi_val_copy(data
->mv
);
6785 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
6786 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
6788 return data
->res
? 0 : -1;
6791 /* Return a union piecewise multi-affine expression
6792 * that is equal to "mv" on "domain".
6794 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
6795 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
6797 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
6800 space
= isl_union_set_get_space(domain
);
6801 data
.res
= isl_union_pw_multi_aff_empty(space
);
6803 if (isl_union_set_foreach_set(domain
,
6804 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
6805 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
6806 isl_union_set_free(domain
);
6807 isl_multi_val_free(mv
);