2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
54 __isl_take isl_vec
*v
)
61 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
71 isl_local_space_free(ls
);
76 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
85 ctx
= isl_local_space_get_ctx(ls
);
86 if (!isl_local_space_divs_known(ls
))
87 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
89 if (!isl_local_space_is_set(ls
))
90 isl_die(ctx
, isl_error_invalid
,
91 "domain of affine expression should be a set",
94 total
= isl_local_space_dim(ls
, isl_dim_all
);
95 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
96 return isl_aff_alloc_vec(ls
, v
);
98 isl_local_space_free(ls
);
102 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
106 aff
= isl_aff_alloc(ls
);
110 isl_int_set_si(aff
->v
->el
[0], 1);
111 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
131 aff
= isl_aff_alloc(ls
);
135 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
152 __isl_take isl_val
*val
)
158 if (!isl_val_is_rat(val
))
159 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
160 "expecting rational value", goto error
);
162 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
166 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
167 isl_int_set(aff
->v
->el
[1], val
->n
);
168 isl_int_set(aff
->v
->el
[0], val
->d
);
170 isl_local_space_free(ls
);
174 isl_local_space_free(ls
);
179 /* Return an affine expression that is equal to the specified dimension
182 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
183 enum isl_dim_type type
, unsigned pos
)
191 space
= isl_local_space_get_space(ls
);
194 if (isl_space_is_map(space
))
195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
196 "expecting (parameter) set space", goto error
);
197 if (pos
>= isl_local_space_dim(ls
, type
))
198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
199 "position out of bounds", goto error
);
201 isl_space_free(space
);
202 aff
= isl_aff_alloc(ls
);
206 pos
+= isl_local_space_offset(aff
->ls
, type
);
208 isl_int_set_si(aff
->v
->el
[0], 1);
209 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
210 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
214 isl_local_space_free(ls
);
215 isl_space_free(space
);
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
223 enum isl_dim_type type
, unsigned pos
)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
228 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
237 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
242 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
243 isl_vec_copy(aff
->v
));
246 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
254 return isl_aff_dup(aff
);
257 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
265 isl_local_space_free(aff
->ls
);
266 isl_vec_free(aff
->v
);
273 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
275 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
278 /* Externally, an isl_aff has a map space, but internally, the
279 * ls field corresponds to the domain of that space.
281 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
285 if (type
== isl_dim_out
)
287 if (type
== isl_dim_in
)
289 return isl_local_space_dim(aff
->ls
, type
);
292 /* Return the position of the dimension of the given type and name
294 * Return -1 if no such dimension can be found.
296 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
301 if (type
== isl_dim_out
)
303 if (type
== isl_dim_in
)
305 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
308 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
310 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
313 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
318 space
= isl_local_space_get_space(aff
->ls
);
319 space
= isl_space_from_domain(space
);
320 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
324 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
325 __isl_keep isl_aff
*aff
)
327 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
330 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
335 ls
= isl_local_space_copy(aff
->ls
);
336 ls
= isl_local_space_from_domain(ls
);
337 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
341 /* Externally, an isl_aff has a map space, but internally, the
342 * ls field corresponds to the domain of that space.
344 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
345 enum isl_dim_type type
, unsigned pos
)
349 if (type
== isl_dim_out
)
351 if (type
== isl_dim_in
)
353 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
356 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
357 __isl_take isl_space
*dim
)
359 aff
= isl_aff_cow(aff
);
363 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
365 return isl_aff_free(aff
);
374 /* Reset the space of "aff". This function is called from isl_pw_templ.c
375 * and doesn't know if the space of an element object is represented
376 * directly or through its domain. It therefore passes along both.
378 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
379 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
381 isl_space_free(space
);
382 return isl_aff_reset_domain_space(aff
, domain
);
385 /* Reorder the coefficients of the affine expression based
386 * on the given reodering.
387 * The reordering r is assumed to have been extended with the local
390 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
391 __isl_take isl_reordering
*r
, int n_div
)
399 res
= isl_vec_alloc(vec
->ctx
,
400 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
401 isl_seq_cpy(res
->el
, vec
->el
, 2);
402 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
403 for (i
= 0; i
< r
->len
; ++i
)
404 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
406 isl_reordering_free(r
);
411 isl_reordering_free(r
);
415 /* Reorder the dimensions of the domain of "aff" according
416 * to the given reordering.
418 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
419 __isl_take isl_reordering
*r
)
421 aff
= isl_aff_cow(aff
);
425 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
426 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
427 aff
->ls
->div
->n_row
);
428 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
430 if (!aff
->v
|| !aff
->ls
)
431 return isl_aff_free(aff
);
436 isl_reordering_free(r
);
440 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
441 __isl_take isl_space
*model
)
446 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
447 model
, isl_dim_param
)) {
450 model
= isl_space_drop_dims(model
, isl_dim_in
,
451 0, isl_space_dim(model
, isl_dim_in
));
452 model
= isl_space_drop_dims(model
, isl_dim_out
,
453 0, isl_space_dim(model
, isl_dim_out
));
454 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
455 exp
= isl_reordering_extend_space(exp
,
456 isl_aff_get_domain_space(aff
));
457 aff
= isl_aff_realign_domain(aff
, exp
);
460 isl_space_free(model
);
463 isl_space_free(model
);
468 /* Is "aff" obviously equal to zero?
470 * If the denominator is zero, then "aff" is not equal to zero.
472 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
475 return isl_bool_error
;
477 if (isl_int_is_zero(aff
->v
->el
[0]))
478 return isl_bool_false
;
479 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
482 /* Does "aff" represent NaN?
484 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
487 return isl_bool_error
;
489 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
492 /* Does "pa" involve any NaNs?
494 isl_bool
isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
499 return isl_bool_error
;
501 return isl_bool_false
;
503 for (i
= 0; i
< pa
->n
; ++i
) {
504 isl_bool is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
505 if (is_nan
< 0 || is_nan
)
509 return isl_bool_false
;
512 /* Are "aff1" and "aff2" obviously equal?
514 * NaN is not equal to anything, not even to another NaN.
516 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
517 __isl_keep isl_aff
*aff2
)
522 return isl_bool_error
;
524 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
525 return isl_bool_false
;
527 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
528 if (equal
< 0 || !equal
)
531 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
534 /* Return the common denominator of "aff" in "v".
536 * We cannot return anything meaningful in case of a NaN.
538 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
542 if (isl_aff_is_nan(aff
))
543 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
544 "cannot get denominator of NaN", return -1);
545 isl_int_set(*v
, aff
->v
->el
[0]);
549 /* Return the common denominator of "aff".
551 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
558 ctx
= isl_aff_get_ctx(aff
);
559 if (isl_aff_is_nan(aff
))
560 return isl_val_nan(ctx
);
561 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
564 /* Return the constant term of "aff" in "v".
566 * We cannot return anything meaningful in case of a NaN.
568 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
572 if (isl_aff_is_nan(aff
))
573 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
574 "cannot get constant term of NaN", return -1);
575 isl_int_set(*v
, aff
->v
->el
[1]);
579 /* Return the constant term of "aff".
581 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
589 ctx
= isl_aff_get_ctx(aff
);
590 if (isl_aff_is_nan(aff
))
591 return isl_val_nan(ctx
);
592 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
593 return isl_val_normalize(v
);
596 /* Return the coefficient of the variable of type "type" at position "pos"
599 * We cannot return anything meaningful in case of a NaN.
601 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
602 enum isl_dim_type type
, int pos
, isl_int
*v
)
607 if (type
== isl_dim_out
)
608 isl_die(aff
->v
->ctx
, isl_error_invalid
,
609 "output/set dimension does not have a coefficient",
611 if (type
== isl_dim_in
)
614 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
615 isl_die(aff
->v
->ctx
, isl_error_invalid
,
616 "position out of bounds", return -1);
618 if (isl_aff_is_nan(aff
))
619 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
620 "cannot get coefficient of NaN", return -1);
621 pos
+= isl_local_space_offset(aff
->ls
, type
);
622 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
627 /* Return the coefficient of the variable of type "type" at position "pos"
630 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
631 enum isl_dim_type type
, int pos
)
639 ctx
= isl_aff_get_ctx(aff
);
640 if (type
== isl_dim_out
)
641 isl_die(ctx
, isl_error_invalid
,
642 "output/set dimension does not have a coefficient",
644 if (type
== isl_dim_in
)
647 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
648 isl_die(ctx
, isl_error_invalid
,
649 "position out of bounds", return NULL
);
651 if (isl_aff_is_nan(aff
))
652 return isl_val_nan(ctx
);
653 pos
+= isl_local_space_offset(aff
->ls
, type
);
654 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
655 return isl_val_normalize(v
);
658 /* Return the sign of the coefficient of the variable of type "type"
659 * at position "pos" of "aff".
661 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
669 ctx
= isl_aff_get_ctx(aff
);
670 if (type
== isl_dim_out
)
671 isl_die(ctx
, isl_error_invalid
,
672 "output/set dimension does not have a coefficient",
674 if (type
== isl_dim_in
)
677 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
678 isl_die(ctx
, isl_error_invalid
,
679 "position out of bounds", return 0);
681 pos
+= isl_local_space_offset(aff
->ls
, type
);
682 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
685 /* Replace the denominator of "aff" by "v".
687 * A NaN is unaffected by this operation.
689 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
693 if (isl_aff_is_nan(aff
))
695 aff
= isl_aff_cow(aff
);
699 aff
->v
= isl_vec_cow(aff
->v
);
701 return isl_aff_free(aff
);
703 isl_int_set(aff
->v
->el
[0], v
);
708 /* Replace the numerator of the constant term of "aff" by "v".
710 * A NaN is unaffected by this operation.
712 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
716 if (isl_aff_is_nan(aff
))
718 aff
= isl_aff_cow(aff
);
722 aff
->v
= isl_vec_cow(aff
->v
);
724 return isl_aff_free(aff
);
726 isl_int_set(aff
->v
->el
[1], v
);
731 /* Replace the constant term of "aff" by "v".
733 * A NaN is unaffected by this operation.
735 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
736 __isl_take isl_val
*v
)
741 if (isl_aff_is_nan(aff
)) {
746 if (!isl_val_is_rat(v
))
747 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
748 "expecting rational value", goto error
);
750 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
751 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
756 aff
= isl_aff_cow(aff
);
759 aff
->v
= isl_vec_cow(aff
->v
);
763 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
764 isl_int_set(aff
->v
->el
[1], v
->n
);
765 } else if (isl_int_is_one(v
->d
)) {
766 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
768 isl_seq_scale(aff
->v
->el
+ 1,
769 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
770 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
771 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
772 aff
->v
= isl_vec_normalize(aff
->v
);
785 /* Add "v" to the constant term of "aff".
787 * A NaN is unaffected by this operation.
789 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
791 if (isl_int_is_zero(v
))
796 if (isl_aff_is_nan(aff
))
798 aff
= isl_aff_cow(aff
);
802 aff
->v
= isl_vec_cow(aff
->v
);
804 return isl_aff_free(aff
);
806 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
811 /* Add "v" to the constant term of "aff".
813 * A NaN is unaffected by this operation.
815 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
816 __isl_take isl_val
*v
)
821 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
826 if (!isl_val_is_rat(v
))
827 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
828 "expecting rational value", goto error
);
830 aff
= isl_aff_cow(aff
);
834 aff
->v
= isl_vec_cow(aff
->v
);
838 if (isl_int_is_one(v
->d
)) {
839 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
840 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
841 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
842 aff
->v
= isl_vec_normalize(aff
->v
);
846 isl_seq_scale(aff
->v
->el
+ 1,
847 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
848 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
849 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
850 aff
->v
= isl_vec_normalize(aff
->v
);
863 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
868 isl_int_set_si(t
, v
);
869 aff
= isl_aff_add_constant(aff
, t
);
875 /* Add "v" to the numerator of the constant term of "aff".
877 * A NaN is unaffected by this operation.
879 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
881 if (isl_int_is_zero(v
))
886 if (isl_aff_is_nan(aff
))
888 aff
= isl_aff_cow(aff
);
892 aff
->v
= isl_vec_cow(aff
->v
);
894 return isl_aff_free(aff
);
896 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
901 /* Add "v" to the numerator of the constant term of "aff".
903 * A NaN is unaffected by this operation.
905 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
913 isl_int_set_si(t
, v
);
914 aff
= isl_aff_add_constant_num(aff
, t
);
920 /* Replace the numerator of the constant term of "aff" by "v".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
928 if (isl_aff_is_nan(aff
))
930 aff
= isl_aff_cow(aff
);
934 aff
->v
= isl_vec_cow(aff
->v
);
936 return isl_aff_free(aff
);
938 isl_int_set_si(aff
->v
->el
[1], v
);
943 /* Replace the numerator of the coefficient of the variable of type "type"
944 * at position "pos" of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
949 enum isl_dim_type type
, int pos
, isl_int v
)
954 if (type
== isl_dim_out
)
955 isl_die(aff
->v
->ctx
, isl_error_invalid
,
956 "output/set dimension does not have a coefficient",
957 return isl_aff_free(aff
));
958 if (type
== isl_dim_in
)
961 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
962 isl_die(aff
->v
->ctx
, isl_error_invalid
,
963 "position out of bounds", return isl_aff_free(aff
));
965 if (isl_aff_is_nan(aff
))
967 aff
= isl_aff_cow(aff
);
971 aff
->v
= isl_vec_cow(aff
->v
);
973 return isl_aff_free(aff
);
975 pos
+= isl_local_space_offset(aff
->ls
, type
);
976 isl_int_set(aff
->v
->el
[1 + pos
], v
);
981 /* Replace the numerator of the coefficient of the variable of type "type"
982 * at position "pos" of "aff" by "v".
984 * A NaN is unaffected by this operation.
986 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
987 enum isl_dim_type type
, int pos
, int v
)
992 if (type
== isl_dim_out
)
993 isl_die(aff
->v
->ctx
, isl_error_invalid
,
994 "output/set dimension does not have a coefficient",
995 return isl_aff_free(aff
));
996 if (type
== isl_dim_in
)
999 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1000 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1001 "position out of bounds", return isl_aff_free(aff
));
1003 if (isl_aff_is_nan(aff
))
1005 pos
+= isl_local_space_offset(aff
->ls
, type
);
1006 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1009 aff
= isl_aff_cow(aff
);
1013 aff
->v
= isl_vec_cow(aff
->v
);
1015 return isl_aff_free(aff
);
1017 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1022 /* Replace the coefficient of the variable of type "type" at position "pos"
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1028 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1033 if (type
== isl_dim_out
)
1034 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1035 "output/set dimension does not have a coefficient",
1037 if (type
== isl_dim_in
)
1040 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1041 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1042 "position out of bounds", goto error
);
1044 if (isl_aff_is_nan(aff
)) {
1048 if (!isl_val_is_rat(v
))
1049 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1050 "expecting rational value", goto error
);
1052 pos
+= isl_local_space_offset(aff
->ls
, type
);
1053 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1054 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1059 aff
= isl_aff_cow(aff
);
1062 aff
->v
= isl_vec_cow(aff
->v
);
1066 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1067 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1068 } else if (isl_int_is_one(v
->d
)) {
1069 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1071 isl_seq_scale(aff
->v
->el
+ 1,
1072 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1073 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1074 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1075 aff
->v
= isl_vec_normalize(aff
->v
);
1088 /* Add "v" to the coefficient of the variable of type "type"
1089 * at position "pos" of "aff".
1091 * A NaN is unaffected by this operation.
1093 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1094 enum isl_dim_type type
, int pos
, isl_int v
)
1099 if (type
== isl_dim_out
)
1100 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1101 "output/set dimension does not have a coefficient",
1102 return isl_aff_free(aff
));
1103 if (type
== isl_dim_in
)
1106 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1107 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1108 "position out of bounds", return isl_aff_free(aff
));
1110 if (isl_aff_is_nan(aff
))
1112 aff
= isl_aff_cow(aff
);
1116 aff
->v
= isl_vec_cow(aff
->v
);
1118 return isl_aff_free(aff
);
1120 pos
+= isl_local_space_offset(aff
->ls
, type
);
1121 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1126 /* Add "v" to the coefficient of the variable of type "type"
1127 * at position "pos" of "aff".
1129 * A NaN is unaffected by this operation.
1131 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1132 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1137 if (isl_val_is_zero(v
)) {
1142 if (type
== isl_dim_out
)
1143 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1144 "output/set dimension does not have a coefficient",
1146 if (type
== isl_dim_in
)
1149 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1150 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1151 "position out of bounds", goto error
);
1153 if (isl_aff_is_nan(aff
)) {
1157 if (!isl_val_is_rat(v
))
1158 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1159 "expecting rational value", goto error
);
1161 aff
= isl_aff_cow(aff
);
1165 aff
->v
= isl_vec_cow(aff
->v
);
1169 pos
+= isl_local_space_offset(aff
->ls
, type
);
1170 if (isl_int_is_one(v
->d
)) {
1171 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1172 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1173 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1174 aff
->v
= isl_vec_normalize(aff
->v
);
1178 isl_seq_scale(aff
->v
->el
+ 1,
1179 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1180 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1181 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1182 aff
->v
= isl_vec_normalize(aff
->v
);
1195 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1196 enum isl_dim_type type
, int pos
, int v
)
1201 isl_int_set_si(t
, v
);
1202 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1208 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1213 return isl_local_space_get_div(aff
->ls
, pos
);
1216 /* Return the negation of "aff".
1218 * As a special case, -NaN = NaN.
1220 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1224 if (isl_aff_is_nan(aff
))
1226 aff
= isl_aff_cow(aff
);
1229 aff
->v
= isl_vec_cow(aff
->v
);
1231 return isl_aff_free(aff
);
1233 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1238 /* Remove divs from the local space that do not appear in the affine
1240 * We currently only remove divs at the end.
1241 * Some intermediate divs may also not appear directly in the affine
1242 * expression, but we would also need to check that no other divs are
1243 * defined in terms of them.
1245 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1254 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1255 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1257 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1261 aff
= isl_aff_cow(aff
);
1265 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1266 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1267 if (!aff
->ls
|| !aff
->v
)
1268 return isl_aff_free(aff
);
1273 /* Given two affine expressions "p" of length p_len (including the
1274 * denominator and the constant term) and "subs" of length subs_len,
1275 * plug in "subs" for the variable at position "pos".
1276 * The variables of "subs" and "p" are assumed to match up to subs_len,
1277 * but "p" may have additional variables.
1278 * "v" is an initialized isl_int that can be used internally.
1280 * In particular, if "p" represents the expression
1284 * with i the variable at position "pos" and "subs" represents the expression
1288 * then the result represents the expression
1293 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1294 int p_len
, int subs_len
, isl_int v
)
1296 isl_int_set(v
, p
[1 + pos
]);
1297 isl_int_set_si(p
[1 + pos
], 0);
1298 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1299 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1300 isl_int_mul(p
[0], p
[0], subs
[0]);
1303 /* Look for any divs in the aff->ls with a denominator equal to one
1304 * and plug them into the affine expression and any subsequent divs
1305 * that may reference the div.
1307 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1313 isl_local_space
*ls
;
1319 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1321 for (i
= 0; i
< n
; ++i
) {
1322 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1324 ls
= isl_local_space_copy(aff
->ls
);
1325 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1326 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1327 vec
= isl_vec_copy(aff
->v
);
1328 vec
= isl_vec_cow(vec
);
1334 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1335 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1340 isl_vec_free(aff
->v
);
1342 isl_local_space_free(aff
->ls
);
1349 isl_local_space_free(ls
);
1350 return isl_aff_free(aff
);
1353 /* Look for any divs j that appear with a unit coefficient inside
1354 * the definitions of other divs i and plug them into the definitions
1357 * In particular, an expression of the form
1359 * floor((f(..) + floor(g(..)/n))/m)
1363 * floor((n * f(..) + g(..))/(n * m))
1365 * This simplification is correct because we can move the expression
1366 * f(..) into the inner floor in the original expression to obtain
1368 * floor(floor((n * f(..) + g(..))/n)/m)
1370 * from which we can derive the simplified expression.
1372 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1380 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1381 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1382 for (i
= 1; i
< n
; ++i
) {
1383 for (j
= 0; j
< i
; ++j
) {
1384 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1386 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1387 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1388 aff
->v
->size
, i
, 1);
1390 return isl_aff_free(aff
);
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1404 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1405 isl_local_space
*ls
;
1408 ls
= isl_local_space_copy(aff
->ls
);
1409 ls
= isl_local_space_swap_div(ls
, a
, b
);
1410 v
= isl_vec_copy(aff
->v
);
1415 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1416 isl_vec_free(aff
->v
);
1418 isl_local_space_free(aff
->ls
);
1424 isl_local_space_free(ls
);
1425 return isl_aff_free(aff
);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1435 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1437 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1440 aff
->v
= isl_vec_cow(aff
->v
);
1442 return isl_aff_free(aff
);
1444 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1445 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1446 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1467 n
= isl_aff_dim(aff
, isl_dim_div
);
1468 for (i
= 1; i
< n
; ++i
) {
1469 for (j
= i
- 1; j
>= 0; --j
) {
1470 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1474 aff
= merge_divs(aff
, j
, j
+ 1);
1476 aff
= swap_div(aff
, j
, j
+ 1);
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1495 aff
->v
= isl_vec_normalize(aff
->v
);
1497 return isl_aff_free(aff
);
1498 aff
= plug_in_integral_divs(aff
);
1499 aff
= plug_in_unit_divs(aff
);
1500 aff
= sort_divs(aff
);
1501 aff
= isl_aff_remove_unused_divs(aff
);
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * As a special case, floor(NaN) = NaN.
1514 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1524 if (isl_aff_is_nan(aff
))
1526 if (isl_int_is_one(aff
->v
->el
[0]))
1529 aff
= isl_aff_cow(aff
);
1533 aff
->v
= isl_vec_cow(aff
->v
);
1535 return isl_aff_free(aff
);
1537 if (isl_aff_is_cst(aff
)) {
1538 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1539 isl_int_set_si(aff
->v
->el
[0], 1);
1543 div
= isl_vec_copy(aff
->v
);
1544 div
= isl_vec_cow(div
);
1546 return isl_aff_free(aff
);
1548 ctx
= isl_aff_get_ctx(aff
);
1549 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1550 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1551 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1552 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1553 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1554 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1555 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1559 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1561 return isl_aff_free(aff
);
1563 size
= aff
->v
->size
;
1564 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1566 return isl_aff_free(aff
);
1567 isl_int_set_si(aff
->v
->el
[0], 1);
1568 isl_int_set_si(aff
->v
->el
[size
], 1);
1570 aff
= isl_aff_normalize(aff
);
1577 * aff mod m = aff - m * floor(aff/m)
1579 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1583 res
= isl_aff_copy(aff
);
1584 aff
= isl_aff_scale_down(aff
, m
);
1585 aff
= isl_aff_floor(aff
);
1586 aff
= isl_aff_scale(aff
, m
);
1587 res
= isl_aff_sub(res
, aff
);
1594 * aff mod m = aff - m * floor(aff/m)
1596 * with m an integer value.
1598 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1599 __isl_take isl_val
*m
)
1606 if (!isl_val_is_int(m
))
1607 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1608 "expecting integer modulo", goto error
);
1610 res
= isl_aff_copy(aff
);
1611 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1612 aff
= isl_aff_floor(aff
);
1613 aff
= isl_aff_scale_val(aff
, m
);
1614 res
= isl_aff_sub(res
, aff
);
1625 * pwaff mod m = pwaff - m * floor(pwaff/m)
1627 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1631 res
= isl_pw_aff_copy(pwaff
);
1632 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1633 pwaff
= isl_pw_aff_floor(pwaff
);
1634 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1635 res
= isl_pw_aff_sub(res
, pwaff
);
1642 * pa mod m = pa - m * floor(pa/m)
1644 * with m an integer value.
1646 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1647 __isl_take isl_val
*m
)
1651 if (!isl_val_is_int(m
))
1652 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1653 "expecting integer modulo", goto error
);
1654 pa
= isl_pw_aff_mod(pa
, m
->n
);
1658 isl_pw_aff_free(pa
);
1663 /* Given f, return ceil(f).
1664 * If f is an integer expression, then just return f.
1665 * Otherwise, let f be the expression
1671 * floor((e + m - 1)/m)
1673 * As a special case, ceil(NaN) = NaN.
1675 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1680 if (isl_aff_is_nan(aff
))
1682 if (isl_int_is_one(aff
->v
->el
[0]))
1685 aff
= isl_aff_cow(aff
);
1688 aff
->v
= isl_vec_cow(aff
->v
);
1690 return isl_aff_free(aff
);
1692 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1693 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1694 aff
= isl_aff_floor(aff
);
1699 /* Apply the expansion computed by isl_merge_divs.
1700 * The expansion itself is given by "exp" while the resulting
1701 * list of divs is given by "div".
1703 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1704 __isl_take isl_mat
*div
, int *exp
)
1711 aff
= isl_aff_cow(aff
);
1715 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1716 new_n_div
= isl_mat_rows(div
);
1717 if (new_n_div
< old_n_div
)
1718 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1719 "not an expansion", goto error
);
1721 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1725 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1727 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1728 if (j
>= 0 && exp
[j
] == i
) {
1730 isl_int_swap(aff
->v
->el
[offset
+ i
],
1731 aff
->v
->el
[offset
+ j
]);
1734 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1737 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1748 /* Add two affine expressions that live in the same local space.
1750 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1751 __isl_take isl_aff
*aff2
)
1755 aff1
= isl_aff_cow(aff1
);
1759 aff1
->v
= isl_vec_cow(aff1
->v
);
1765 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1766 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1767 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1768 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1769 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1770 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1771 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1783 /* Return the sum of "aff1" and "aff2".
1785 * If either of the two is NaN, then the result is NaN.
1787 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1788 __isl_take isl_aff
*aff2
)
1799 ctx
= isl_aff_get_ctx(aff1
);
1800 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1801 isl_die(ctx
, isl_error_invalid
,
1802 "spaces don't match", goto error
);
1804 if (isl_aff_is_nan(aff1
)) {
1808 if (isl_aff_is_nan(aff2
)) {
1813 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1814 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1815 if (n_div1
== 0 && n_div2
== 0)
1816 return add_expanded(aff1
, aff2
);
1818 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1819 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1820 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1823 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1824 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1825 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1829 return add_expanded(aff1
, aff2
);
1838 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1839 __isl_take isl_aff
*aff2
)
1841 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1844 /* Return the result of scaling "aff" by a factor of "f".
1846 * As a special case, f * NaN = NaN.
1848 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1854 if (isl_aff_is_nan(aff
))
1857 if (isl_int_is_one(f
))
1860 aff
= isl_aff_cow(aff
);
1863 aff
->v
= isl_vec_cow(aff
->v
);
1865 return isl_aff_free(aff
);
1867 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1868 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1873 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1874 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1875 isl_int_divexact(gcd
, f
, gcd
);
1876 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1882 /* Multiple "aff" by "v".
1884 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1885 __isl_take isl_val
*v
)
1890 if (isl_val_is_one(v
)) {
1895 if (!isl_val_is_rat(v
))
1896 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1897 "expecting rational factor", goto error
);
1899 aff
= isl_aff_scale(aff
, v
->n
);
1900 aff
= isl_aff_scale_down(aff
, v
->d
);
1910 /* Return the result of scaling "aff" down by a factor of "f".
1912 * As a special case, NaN/f = NaN.
1914 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1920 if (isl_aff_is_nan(aff
))
1923 if (isl_int_is_one(f
))
1926 aff
= isl_aff_cow(aff
);
1930 if (isl_int_is_zero(f
))
1931 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1932 "cannot scale down by zero", return isl_aff_free(aff
));
1934 aff
->v
= isl_vec_cow(aff
->v
);
1936 return isl_aff_free(aff
);
1939 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1940 isl_int_gcd(gcd
, gcd
, f
);
1941 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1942 isl_int_divexact(gcd
, f
, gcd
);
1943 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1949 /* Divide "aff" by "v".
1951 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1952 __isl_take isl_val
*v
)
1957 if (isl_val_is_one(v
)) {
1962 if (!isl_val_is_rat(v
))
1963 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1964 "expecting rational factor", goto error
);
1965 if (!isl_val_is_pos(v
))
1966 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1967 "factor needs to be positive", goto error
);
1969 aff
= isl_aff_scale(aff
, v
->d
);
1970 aff
= isl_aff_scale_down(aff
, v
->n
);
1980 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1988 isl_int_set_ui(v
, f
);
1989 aff
= isl_aff_scale_down(aff
, v
);
1995 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1996 enum isl_dim_type type
, unsigned pos
, const char *s
)
1998 aff
= isl_aff_cow(aff
);
2001 if (type
== isl_dim_out
)
2002 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2003 "cannot set name of output/set dimension",
2004 return isl_aff_free(aff
));
2005 if (type
== isl_dim_in
)
2007 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2009 return isl_aff_free(aff
);
2014 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2015 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2017 aff
= isl_aff_cow(aff
);
2020 if (type
== isl_dim_out
)
2021 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2022 "cannot set name of output/set dimension",
2024 if (type
== isl_dim_in
)
2026 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2028 return isl_aff_free(aff
);
2037 /* Replace the identifier of the input tuple of "aff" by "id".
2038 * type is currently required to be equal to isl_dim_in
2040 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2041 enum isl_dim_type type
, __isl_take isl_id
*id
)
2043 aff
= isl_aff_cow(aff
);
2046 if (type
!= isl_dim_out
)
2047 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2048 "cannot only set id of input tuple", goto error
);
2049 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2051 return isl_aff_free(aff
);
2060 /* Exploit the equalities in "eq" to simplify the affine expression
2061 * and the expressions of the integer divisions in the local space.
2062 * The integer divisions in this local space are assumed to appear
2063 * as regular dimensions in "eq".
2065 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2066 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2074 if (eq
->n_eq
== 0) {
2075 isl_basic_set_free(eq
);
2079 aff
= isl_aff_cow(aff
);
2083 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2084 isl_basic_set_copy(eq
));
2085 aff
->v
= isl_vec_cow(aff
->v
);
2086 if (!aff
->ls
|| !aff
->v
)
2089 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2091 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2092 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2093 if (j
< 0 || j
== 0 || j
>= total
)
2096 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2100 isl_basic_set_free(eq
);
2101 aff
= isl_aff_normalize(aff
);
2104 isl_basic_set_free(eq
);
2109 /* Exploit the equalities in "eq" to simplify the affine expression
2110 * and the expressions of the integer divisions in the local space.
2112 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2113 __isl_take isl_basic_set
*eq
)
2119 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2121 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2122 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2124 isl_basic_set_free(eq
);
2129 /* Look for equalities among the variables shared by context and aff
2130 * and the integer divisions of aff, if any.
2131 * The equalities are then used to eliminate coefficients and/or integer
2132 * divisions from aff.
2134 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2135 __isl_take isl_set
*context
)
2137 isl_basic_set
*hull
;
2142 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2144 isl_basic_set
*bset
;
2145 isl_local_space
*ls
;
2146 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2147 ls
= isl_aff_get_domain_local_space(aff
);
2148 bset
= isl_basic_set_from_local_space(ls
);
2149 bset
= isl_basic_set_lift(bset
);
2150 bset
= isl_basic_set_flatten(bset
);
2151 context
= isl_set_intersect(context
,
2152 isl_set_from_basic_set(bset
));
2155 hull
= isl_set_affine_hull(context
);
2156 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2159 isl_set_free(context
);
2163 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2164 __isl_take isl_set
*context
)
2166 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2167 dom_context
= isl_set_intersect_params(dom_context
, context
);
2168 return isl_aff_gist(aff
, dom_context
);
2171 /* Return a basic set containing those elements in the space
2172 * of aff where it is positive. "rational" should not be set.
2174 * If "aff" is NaN, then it is not positive.
2176 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2179 isl_constraint
*ineq
;
2180 isl_basic_set
*bset
;
2185 if (isl_aff_is_nan(aff
)) {
2186 isl_space
*space
= isl_aff_get_domain_space(aff
);
2188 return isl_basic_set_empty(space
);
2191 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2192 "rational sets not supported", goto error
);
2194 ineq
= isl_inequality_from_aff(aff
);
2195 c
= isl_constraint_get_constant_val(ineq
);
2196 c
= isl_val_sub_ui(c
, 1);
2197 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2199 bset
= isl_basic_set_from_constraint(ineq
);
2200 bset
= isl_basic_set_simplify(bset
);
2207 /* Return a basic set containing those elements in the space
2208 * of aff where it is non-negative.
2209 * If "rational" is set, then return a rational basic set.
2211 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2213 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2214 __isl_take isl_aff
*aff
, int rational
)
2216 isl_constraint
*ineq
;
2217 isl_basic_set
*bset
;
2221 if (isl_aff_is_nan(aff
)) {
2222 isl_space
*space
= isl_aff_get_domain_space(aff
);
2224 return isl_basic_set_empty(space
);
2227 ineq
= isl_inequality_from_aff(aff
);
2229 bset
= isl_basic_set_from_constraint(ineq
);
2231 bset
= isl_basic_set_set_rational(bset
);
2232 bset
= isl_basic_set_simplify(bset
);
2236 /* Return a basic set containing those elements in the space
2237 * of aff where it is non-negative.
2239 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2241 return aff_nonneg_basic_set(aff
, 0);
2244 /* Return a basic set containing those elements in the domain space
2245 * of aff where it is negative.
2247 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2249 aff
= isl_aff_neg(aff
);
2250 aff
= isl_aff_add_constant_num_si(aff
, -1);
2251 return isl_aff_nonneg_basic_set(aff
);
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is zero.
2256 * If "rational" is set, then return a rational basic set.
2258 * If "aff" is NaN, then it is not zero.
2260 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2263 isl_constraint
*ineq
;
2264 isl_basic_set
*bset
;
2268 if (isl_aff_is_nan(aff
)) {
2269 isl_space
*space
= isl_aff_get_domain_space(aff
);
2271 return isl_basic_set_empty(space
);
2274 ineq
= isl_equality_from_aff(aff
);
2276 bset
= isl_basic_set_from_constraint(ineq
);
2278 bset
= isl_basic_set_set_rational(bset
);
2279 bset
= isl_basic_set_simplify(bset
);
2283 /* Return a basic set containing those elements in the space
2284 * of aff where it is zero.
2286 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2288 return aff_zero_basic_set(aff
, 0);
2291 /* Return a basic set containing those elements in the shared space
2292 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2294 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2295 __isl_take isl_aff
*aff2
)
2297 aff1
= isl_aff_sub(aff1
, aff2
);
2299 return isl_aff_nonneg_basic_set(aff1
);
2302 /* Return a basic set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2305 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2306 __isl_take isl_aff
*aff2
)
2308 return isl_aff_ge_basic_set(aff2
, aff1
);
2311 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2312 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2314 aff1
= isl_aff_add(aff1
, aff2
);
2315 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2319 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2327 /* Check whether the given affine expression has non-zero coefficient
2328 * for any dimension in the given range or if any of these dimensions
2329 * appear with non-zero coefficients in any of the integer divisions
2330 * involved in the affine expression.
2332 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2333 enum isl_dim_type type
, unsigned first
, unsigned n
)
2338 isl_bool involves
= isl_bool_false
;
2341 return isl_bool_error
;
2343 return isl_bool_false
;
2345 ctx
= isl_aff_get_ctx(aff
);
2346 if (first
+ n
> isl_aff_dim(aff
, type
))
2347 isl_die(ctx
, isl_error_invalid
,
2348 "range out of bounds", return isl_bool_error
);
2350 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2354 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2355 for (i
= 0; i
< n
; ++i
)
2356 if (active
[first
+ i
]) {
2357 involves
= isl_bool_true
;
2366 return isl_bool_error
;
2369 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2370 enum isl_dim_type type
, unsigned first
, unsigned n
)
2376 if (type
== isl_dim_out
)
2377 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2378 "cannot drop output/set dimension",
2379 return isl_aff_free(aff
));
2380 if (type
== isl_dim_in
)
2382 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2385 ctx
= isl_aff_get_ctx(aff
);
2386 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2387 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2388 return isl_aff_free(aff
));
2390 aff
= isl_aff_cow(aff
);
2394 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2396 return isl_aff_free(aff
);
2398 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2399 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2401 return isl_aff_free(aff
);
2406 /* Project the domain of the affine expression onto its parameter space.
2407 * The affine expression may not involve any of the domain dimensions.
2409 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2415 n
= isl_aff_dim(aff
, isl_dim_in
);
2416 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2418 return isl_aff_free(aff
);
2420 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2421 "affine expression involves some of the domain dimensions",
2422 return isl_aff_free(aff
));
2423 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2424 space
= isl_aff_get_domain_space(aff
);
2425 space
= isl_space_params(space
);
2426 aff
= isl_aff_reset_domain_space(aff
, space
);
2430 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2431 enum isl_dim_type type
, unsigned first
, unsigned n
)
2437 if (type
== isl_dim_out
)
2438 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2439 "cannot insert output/set dimensions",
2440 return isl_aff_free(aff
));
2441 if (type
== isl_dim_in
)
2443 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2446 ctx
= isl_aff_get_ctx(aff
);
2447 if (first
> isl_local_space_dim(aff
->ls
, type
))
2448 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2449 return isl_aff_free(aff
));
2451 aff
= isl_aff_cow(aff
);
2455 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2457 return isl_aff_free(aff
);
2459 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2460 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2462 return isl_aff_free(aff
);
2467 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2468 enum isl_dim_type type
, unsigned n
)
2472 pos
= isl_aff_dim(aff
, type
);
2474 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2477 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2478 enum isl_dim_type type
, unsigned n
)
2482 pos
= isl_pw_aff_dim(pwaff
, type
);
2484 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2487 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2488 * to dimensions of "dst_type" at "dst_pos".
2490 * We only support moving input dimensions to parameters and vice versa.
2492 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2493 enum isl_dim_type dst_type
, unsigned dst_pos
,
2494 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2502 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2503 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2506 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2507 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2508 "cannot move output/set dimension",
2509 return isl_aff_free(aff
));
2510 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2511 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2512 "cannot move divs", return isl_aff_free(aff
));
2513 if (dst_type
== isl_dim_in
)
2514 dst_type
= isl_dim_set
;
2515 if (src_type
== isl_dim_in
)
2516 src_type
= isl_dim_set
;
2518 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2519 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2520 "range out of bounds", return isl_aff_free(aff
));
2521 if (dst_type
== src_type
)
2522 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2523 "moving dims within the same type not supported",
2524 return isl_aff_free(aff
));
2526 aff
= isl_aff_cow(aff
);
2530 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2531 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2532 if (dst_type
> src_type
)
2535 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2536 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2537 src_type
, src_pos
, n
);
2538 if (!aff
->v
|| !aff
->ls
)
2539 return isl_aff_free(aff
);
2541 aff
= sort_divs(aff
);
2546 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2548 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2549 return isl_pw_aff_alloc(dom
, aff
);
2553 #define PW isl_pw_aff
2557 #define EL_IS_ZERO is_empty
2561 #define IS_ZERO is_empty
2564 #undef DEFAULT_IS_ZERO
2565 #define DEFAULT_IS_ZERO 0
2572 #include <isl_pw_templ.c>
2575 #define UNION isl_union_pw_aff
2577 #define PART isl_pw_aff
2579 #define PARTS pw_aff
2581 #include <isl_union_single.c>
2582 #include <isl_union_neg.c>
2584 static __isl_give isl_set
*align_params_pw_pw_set_and(
2585 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2586 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2587 __isl_take isl_pw_aff
*pwaff2
))
2589 if (!pwaff1
|| !pwaff2
)
2591 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2592 pwaff2
->dim
, isl_dim_param
))
2593 return fn(pwaff1
, pwaff2
);
2594 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2595 !isl_space_has_named_params(pwaff2
->dim
))
2596 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2597 "unaligned unnamed parameters", goto error
);
2598 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2599 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2600 return fn(pwaff1
, pwaff2
);
2602 isl_pw_aff_free(pwaff1
);
2603 isl_pw_aff_free(pwaff2
);
2607 /* Align the parameters of the to isl_pw_aff arguments and
2608 * then apply a function "fn" on them that returns an isl_map.
2610 static __isl_give isl_map
*align_params_pw_pw_map_and(
2611 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2612 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2613 __isl_take isl_pw_aff
*pa2
))
2617 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2618 return fn(pa1
, pa2
);
2619 if (!isl_space_has_named_params(pa1
->dim
) ||
2620 !isl_space_has_named_params(pa2
->dim
))
2621 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2622 "unaligned unnamed parameters", goto error
);
2623 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2624 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2625 return fn(pa1
, pa2
);
2627 isl_pw_aff_free(pa1
);
2628 isl_pw_aff_free(pa2
);
2632 /* Compute a piecewise quasi-affine expression with a domain that
2633 * is the union of those of pwaff1 and pwaff2 and such that on each
2634 * cell, the quasi-affine expression is the better (according to cmp)
2635 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2636 * is defined on a given cell, then the associated expression
2637 * is the defined one.
2639 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2640 __isl_take isl_pw_aff
*pwaff2
,
2641 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2642 __isl_take isl_aff
*aff2
))
2649 if (!pwaff1
|| !pwaff2
)
2652 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2653 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2654 isl_die(ctx
, isl_error_invalid
,
2655 "arguments should live in same space", goto error
);
2657 if (isl_pw_aff_is_empty(pwaff1
)) {
2658 isl_pw_aff_free(pwaff1
);
2662 if (isl_pw_aff_is_empty(pwaff2
)) {
2663 isl_pw_aff_free(pwaff2
);
2667 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2668 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2670 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2671 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2672 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2673 struct isl_set
*common
;
2676 common
= isl_set_intersect(
2677 isl_set_copy(pwaff1
->p
[i
].set
),
2678 isl_set_copy(pwaff2
->p
[j
].set
));
2679 better
= isl_set_from_basic_set(cmp(
2680 isl_aff_copy(pwaff2
->p
[j
].aff
),
2681 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2682 better
= isl_set_intersect(common
, better
);
2683 if (isl_set_plain_is_empty(better
)) {
2684 isl_set_free(better
);
2687 set
= isl_set_subtract(set
, isl_set_copy(better
));
2689 res
= isl_pw_aff_add_piece(res
, better
,
2690 isl_aff_copy(pwaff2
->p
[j
].aff
));
2692 res
= isl_pw_aff_add_piece(res
, set
,
2693 isl_aff_copy(pwaff1
->p
[i
].aff
));
2696 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2697 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2698 for (i
= 0; i
< pwaff1
->n
; ++i
)
2699 set
= isl_set_subtract(set
,
2700 isl_set_copy(pwaff1
->p
[i
].set
));
2701 res
= isl_pw_aff_add_piece(res
, set
,
2702 isl_aff_copy(pwaff2
->p
[j
].aff
));
2705 isl_pw_aff_free(pwaff1
);
2706 isl_pw_aff_free(pwaff2
);
2710 isl_pw_aff_free(pwaff1
);
2711 isl_pw_aff_free(pwaff2
);
2715 /* Compute a piecewise quasi-affine expression with a domain that
2716 * is the union of those of pwaff1 and pwaff2 and such that on each
2717 * cell, the quasi-affine expression is the maximum of those of pwaff1
2718 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2719 * cell, then the associated expression is the defined one.
2721 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2722 __isl_take isl_pw_aff
*pwaff2
)
2724 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2727 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2728 __isl_take isl_pw_aff
*pwaff2
)
2730 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2734 /* Compute a piecewise quasi-affine expression with a domain that
2735 * is the union of those of pwaff1 and pwaff2 and such that on each
2736 * cell, the quasi-affine expression is the minimum of those of pwaff1
2737 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2738 * cell, then the associated expression is the defined one.
2740 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2741 __isl_take isl_pw_aff
*pwaff2
)
2743 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2746 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2747 __isl_take isl_pw_aff
*pwaff2
)
2749 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2753 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2754 __isl_take isl_pw_aff
*pwaff2
, int max
)
2757 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2759 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2762 /* Construct a map with as domain the domain of pwaff and
2763 * one-dimensional range corresponding to the affine expressions.
2765 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2774 dim
= isl_pw_aff_get_space(pwaff
);
2775 map
= isl_map_empty(dim
);
2777 for (i
= 0; i
< pwaff
->n
; ++i
) {
2778 isl_basic_map
*bmap
;
2781 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2782 map_i
= isl_map_from_basic_map(bmap
);
2783 map_i
= isl_map_intersect_domain(map_i
,
2784 isl_set_copy(pwaff
->p
[i
].set
));
2785 map
= isl_map_union_disjoint(map
, map_i
);
2788 isl_pw_aff_free(pwaff
);
2793 /* Construct a map with as domain the domain of pwaff and
2794 * one-dimensional range corresponding to the affine expressions.
2796 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2800 if (isl_space_is_set(pwaff
->dim
))
2801 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2802 "space of input is not a map", goto error
);
2803 return map_from_pw_aff(pwaff
);
2805 isl_pw_aff_free(pwaff
);
2809 /* Construct a one-dimensional set with as parameter domain
2810 * the domain of pwaff and the single set dimension
2811 * corresponding to the affine expressions.
2813 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2817 if (!isl_space_is_set(pwaff
->dim
))
2818 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2819 "space of input is not a set", goto error
);
2820 return map_from_pw_aff(pwaff
);
2822 isl_pw_aff_free(pwaff
);
2826 /* Return a set containing those elements in the domain
2827 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2828 * does not satisfy "fn" (if complement is 1).
2830 * The pieces with a NaN never belong to the result since
2831 * NaN does not satisfy any property.
2833 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2834 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2843 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2845 for (i
= 0; i
< pwaff
->n
; ++i
) {
2846 isl_basic_set
*bset
;
2847 isl_set
*set_i
, *locus
;
2850 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2853 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2854 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2855 locus
= isl_set_from_basic_set(bset
);
2856 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2858 set_i
= isl_set_subtract(set_i
, locus
);
2860 set_i
= isl_set_intersect(set_i
, locus
);
2861 set
= isl_set_union_disjoint(set
, set_i
);
2864 isl_pw_aff_free(pwaff
);
2869 /* Return a set containing those elements in the domain
2870 * of "pa" where it is positive.
2872 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2874 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2877 /* Return a set containing those elements in the domain
2878 * of pwaff where it is non-negative.
2880 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2882 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2885 /* Return a set containing those elements in the domain
2886 * of pwaff where it is zero.
2888 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2890 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2893 /* Return a set containing those elements in the domain
2894 * of pwaff where it is not zero.
2896 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2898 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2901 /* Return a set containing those elements in the shared domain
2902 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2904 * We compute the difference on the shared domain and then construct
2905 * the set of values where this difference is non-negative.
2906 * If strict is set, we first subtract 1 from the difference.
2907 * If equal is set, we only return the elements where pwaff1 and pwaff2
2910 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2911 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2913 isl_set
*set1
, *set2
;
2915 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2916 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2917 set1
= isl_set_intersect(set1
, set2
);
2918 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2919 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2920 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2923 isl_space
*dim
= isl_set_get_space(set1
);
2925 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2926 aff
= isl_aff_add_constant_si(aff
, -1);
2927 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2932 return isl_pw_aff_zero_set(pwaff1
);
2933 return isl_pw_aff_nonneg_set(pwaff1
);
2936 /* Return a set containing those elements in the shared domain
2937 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2939 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2940 __isl_take isl_pw_aff
*pwaff2
)
2942 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2945 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2946 __isl_take isl_pw_aff
*pwaff2
)
2948 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2951 /* Return a set containing those elements in the shared domain
2952 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2954 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2955 __isl_take isl_pw_aff
*pwaff2
)
2957 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2960 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2961 __isl_take isl_pw_aff
*pwaff2
)
2963 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2966 /* Return a set containing those elements in the shared domain
2967 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2969 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2970 __isl_take isl_pw_aff
*pwaff2
)
2972 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2975 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2976 __isl_take isl_pw_aff
*pwaff2
)
2978 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2981 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2982 __isl_take isl_pw_aff
*pwaff2
)
2984 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2987 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2988 __isl_take isl_pw_aff
*pwaff2
)
2990 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2993 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2994 * where the function values are ordered in the same way as "order",
2995 * which returns a set in the shared domain of its two arguments.
2996 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2998 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2999 * We first pull back the two functions such that they are defined on
3000 * the domain [A -> B]. Then we apply "order", resulting in a set
3001 * in the space [A -> B]. Finally, we unwrap this set to obtain
3002 * a map in the space A -> B.
3004 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3005 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3006 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3007 __isl_take isl_pw_aff
*pa2
))
3009 isl_space
*space1
, *space2
;
3013 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3014 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3015 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3016 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3017 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3018 ma
= isl_multi_aff_range_map(space1
);
3019 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3020 set
= order(pa1
, pa2
);
3022 return isl_set_unwrap(set
);
3025 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3026 * where the function values are equal.
3027 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3029 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3030 __isl_take isl_pw_aff
*pa2
)
3032 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3035 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3036 * where the function values are equal.
3038 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3039 __isl_take isl_pw_aff
*pa2
)
3041 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3044 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3045 * where the function value of "pa1" is less than the function value of "pa2".
3046 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3048 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3049 __isl_take isl_pw_aff
*pa2
)
3051 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3054 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3055 * where the function value of "pa1" is less than the function value of "pa2".
3057 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3058 __isl_take isl_pw_aff
*pa2
)
3060 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3063 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3064 * where the function value of "pa1" is greater than the function value
3066 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3068 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3069 __isl_take isl_pw_aff
*pa2
)
3071 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3074 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3075 * where the function value of "pa1" is greater than the function value
3078 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3079 __isl_take isl_pw_aff
*pa2
)
3081 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3084 /* Return a set containing those elements in the shared domain
3085 * of the elements of list1 and list2 where each element in list1
3086 * has the relation specified by "fn" with each element in list2.
3088 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3089 __isl_take isl_pw_aff_list
*list2
,
3090 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3091 __isl_take isl_pw_aff
*pwaff2
))
3097 if (!list1
|| !list2
)
3100 ctx
= isl_pw_aff_list_get_ctx(list1
);
3101 if (list1
->n
< 1 || list2
->n
< 1)
3102 isl_die(ctx
, isl_error_invalid
,
3103 "list should contain at least one element", goto error
);
3105 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3106 for (i
= 0; i
< list1
->n
; ++i
)
3107 for (j
= 0; j
< list2
->n
; ++j
) {
3110 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3111 isl_pw_aff_copy(list2
->p
[j
]));
3112 set
= isl_set_intersect(set
, set_ij
);
3115 isl_pw_aff_list_free(list1
);
3116 isl_pw_aff_list_free(list2
);
3119 isl_pw_aff_list_free(list1
);
3120 isl_pw_aff_list_free(list2
);
3124 /* Return a set containing those elements in the shared domain
3125 * of the elements of list1 and list2 where each element in list1
3126 * is equal to each element in list2.
3128 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3129 __isl_take isl_pw_aff_list
*list2
)
3131 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3134 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3135 __isl_take isl_pw_aff_list
*list2
)
3137 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3140 /* Return a set containing those elements in the shared domain
3141 * of the elements of list1 and list2 where each element in list1
3142 * is less than or equal to each element in list2.
3144 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3145 __isl_take isl_pw_aff_list
*list2
)
3147 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3150 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3151 __isl_take isl_pw_aff_list
*list2
)
3153 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3156 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3157 __isl_take isl_pw_aff_list
*list2
)
3159 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3162 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3163 __isl_take isl_pw_aff_list
*list2
)
3165 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3169 /* Return a set containing those elements in the shared domain
3170 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3172 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3173 __isl_take isl_pw_aff
*pwaff2
)
3175 isl_set
*set_lt
, *set_gt
;
3177 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3178 isl_pw_aff_copy(pwaff2
));
3179 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3180 return isl_set_union_disjoint(set_lt
, set_gt
);
3183 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3184 __isl_take isl_pw_aff
*pwaff2
)
3186 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3189 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3194 if (isl_int_is_one(v
))
3196 if (!isl_int_is_pos(v
))
3197 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3198 "factor needs to be positive",
3199 return isl_pw_aff_free(pwaff
));
3200 pwaff
= isl_pw_aff_cow(pwaff
);
3206 for (i
= 0; i
< pwaff
->n
; ++i
) {
3207 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3208 if (!pwaff
->p
[i
].aff
)
3209 return isl_pw_aff_free(pwaff
);
3215 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3219 pwaff
= isl_pw_aff_cow(pwaff
);
3225 for (i
= 0; i
< pwaff
->n
; ++i
) {
3226 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3227 if (!pwaff
->p
[i
].aff
)
3228 return isl_pw_aff_free(pwaff
);
3234 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3238 pwaff
= isl_pw_aff_cow(pwaff
);
3244 for (i
= 0; i
< pwaff
->n
; ++i
) {
3245 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3246 if (!pwaff
->p
[i
].aff
)
3247 return isl_pw_aff_free(pwaff
);
3253 /* Assuming that "cond1" and "cond2" are disjoint,
3254 * return an affine expression that is equal to pwaff1 on cond1
3255 * and to pwaff2 on cond2.
3257 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3258 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3259 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3261 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3262 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3264 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3267 /* Return an affine expression that is equal to pwaff_true for elements
3268 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3270 * That is, return cond ? pwaff_true : pwaff_false;
3272 * If "cond" involves and NaN, then we conservatively return a NaN
3273 * on its entire domain. In principle, we could consider the pieces
3274 * where it is NaN separately from those where it is not.
3276 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3277 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3279 isl_set
*cond_true
, *cond_false
;
3283 if (isl_pw_aff_involves_nan(cond
)) {
3284 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3285 isl_local_space
*ls
= isl_local_space_from_space(space
);
3286 isl_pw_aff_free(cond
);
3287 isl_pw_aff_free(pwaff_true
);
3288 isl_pw_aff_free(pwaff_false
);
3289 return isl_pw_aff_nan_on_domain(ls
);
3292 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3293 cond_false
= isl_pw_aff_zero_set(cond
);
3294 return isl_pw_aff_select(cond_true
, pwaff_true
,
3295 cond_false
, pwaff_false
);
3297 isl_pw_aff_free(cond
);
3298 isl_pw_aff_free(pwaff_true
);
3299 isl_pw_aff_free(pwaff_false
);
3303 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3306 return isl_bool_error
;
3308 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3311 /* Check whether pwaff is a piecewise constant.
3313 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3318 return isl_bool_error
;
3320 for (i
= 0; i
< pwaff
->n
; ++i
) {
3321 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3322 if (is_cst
< 0 || !is_cst
)
3326 return isl_bool_true
;
3329 /* Are all elements of "mpa" piecewise constants?
3331 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3336 return isl_bool_error
;
3338 for (i
= 0; i
< mpa
->n
; ++i
) {
3339 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3340 if (is_cst
< 0 || !is_cst
)
3344 return isl_bool_true
;
3347 /* Return the product of "aff1" and "aff2".
3349 * If either of the two is NaN, then the result is NaN.
3351 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3353 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3354 __isl_take isl_aff
*aff2
)
3359 if (isl_aff_is_nan(aff1
)) {
3363 if (isl_aff_is_nan(aff2
)) {
3368 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3369 return isl_aff_mul(aff2
, aff1
);
3371 if (!isl_aff_is_cst(aff2
))
3372 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3373 "at least one affine expression should be constant",
3376 aff1
= isl_aff_cow(aff1
);
3380 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3381 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3391 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3393 * If either of the two is NaN, then the result is NaN.
3395 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3396 __isl_take isl_aff
*aff2
)
3404 if (isl_aff_is_nan(aff1
)) {
3408 if (isl_aff_is_nan(aff2
)) {
3413 is_cst
= isl_aff_is_cst(aff2
);
3417 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3418 "second argument should be a constant", goto error
);
3423 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3425 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3426 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3429 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3430 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3433 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3434 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3445 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3446 __isl_take isl_pw_aff
*pwaff2
)
3448 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3451 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3452 __isl_take isl_pw_aff
*pwaff2
)
3454 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3457 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3458 __isl_take isl_pw_aff
*pwaff2
)
3460 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3463 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3464 __isl_take isl_pw_aff
*pwaff2
)
3466 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3469 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3470 __isl_take isl_pw_aff
*pwaff2
)
3472 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3475 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3476 __isl_take isl_pw_aff
*pa2
)
3478 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3481 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3483 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3484 __isl_take isl_pw_aff
*pa2
)
3488 is_cst
= isl_pw_aff_is_cst(pa2
);
3492 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3493 "second argument should be a piecewise constant",
3495 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3497 isl_pw_aff_free(pa1
);
3498 isl_pw_aff_free(pa2
);
3502 /* Compute the quotient of the integer division of "pa1" by "pa2"
3503 * with rounding towards zero.
3504 * "pa2" is assumed to be a piecewise constant.
3506 * In particular, return
3508 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3511 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3512 __isl_take isl_pw_aff
*pa2
)
3518 is_cst
= isl_pw_aff_is_cst(pa2
);
3522 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3523 "second argument should be a piecewise constant",
3526 pa1
= isl_pw_aff_div(pa1
, pa2
);
3528 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3529 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3530 c
= isl_pw_aff_ceil(pa1
);
3531 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3533 isl_pw_aff_free(pa1
);
3534 isl_pw_aff_free(pa2
);
3538 /* Compute the remainder of the integer division of "pa1" by "pa2"
3539 * with rounding towards zero.
3540 * "pa2" is assumed to be a piecewise constant.
3542 * In particular, return
3544 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3547 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3548 __isl_take isl_pw_aff
*pa2
)
3553 is_cst
= isl_pw_aff_is_cst(pa2
);
3557 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3558 "second argument should be a piecewise constant",
3560 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3561 res
= isl_pw_aff_mul(pa2
, res
);
3562 res
= isl_pw_aff_sub(pa1
, res
);
3565 isl_pw_aff_free(pa1
);
3566 isl_pw_aff_free(pa2
);
3570 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3571 __isl_take isl_pw_aff
*pwaff2
)
3576 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3577 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3578 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3579 isl_pw_aff_copy(pwaff2
));
3580 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3581 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3584 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3585 __isl_take isl_pw_aff
*pwaff2
)
3587 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3590 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3591 __isl_take isl_pw_aff
*pwaff2
)
3596 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3597 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3598 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3599 isl_pw_aff_copy(pwaff2
));
3600 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3601 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3604 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3605 __isl_take isl_pw_aff
*pwaff2
)
3607 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3610 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3611 __isl_take isl_pw_aff_list
*list
,
3612 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3613 __isl_take isl_pw_aff
*pwaff2
))
3622 ctx
= isl_pw_aff_list_get_ctx(list
);
3624 isl_die(ctx
, isl_error_invalid
,
3625 "list should contain at least one element", goto error
);
3627 res
= isl_pw_aff_copy(list
->p
[0]);
3628 for (i
= 1; i
< list
->n
; ++i
)
3629 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3631 isl_pw_aff_list_free(list
);
3634 isl_pw_aff_list_free(list
);
3638 /* Return an isl_pw_aff that maps each element in the intersection of the
3639 * domains of the elements of list to the minimal corresponding affine
3642 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3644 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3647 /* Return an isl_pw_aff that maps each element in the intersection of the
3648 * domains of the elements of list to the maximal corresponding affine
3651 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3653 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3656 /* Mark the domains of "pwaff" as rational.
3658 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3662 pwaff
= isl_pw_aff_cow(pwaff
);
3668 for (i
= 0; i
< pwaff
->n
; ++i
) {
3669 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3670 if (!pwaff
->p
[i
].set
)
3671 return isl_pw_aff_free(pwaff
);
3677 /* Mark the domains of the elements of "list" as rational.
3679 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3680 __isl_take isl_pw_aff_list
*list
)
3690 for (i
= 0; i
< n
; ++i
) {
3693 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3694 pa
= isl_pw_aff_set_rational(pa
);
3695 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3701 /* Do the parameters of "aff" match those of "space"?
3703 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3704 __isl_keep isl_space
*space
)
3706 isl_space
*aff_space
;
3712 aff_space
= isl_aff_get_domain_space(aff
);
3714 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3716 isl_space_free(aff_space
);
3720 /* Check that the domain space of "aff" matches "space".
3722 * Return 0 on success and -1 on error.
3724 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3725 __isl_keep isl_space
*space
)
3727 isl_space
*aff_space
;
3733 aff_space
= isl_aff_get_domain_space(aff
);
3735 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3739 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3740 "parameters don't match", goto error
);
3741 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3742 aff_space
, isl_dim_set
);
3746 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3747 "domains don't match", goto error
);
3748 isl_space_free(aff_space
);
3751 isl_space_free(aff_space
);
3761 #include <isl_multi_templ.c>
3762 #include <isl_multi_apply_set.c>
3763 #include <isl_multi_floor.c>
3764 #include <isl_multi_gist.c>
3768 /* Remove any internal structure of the domain of "ma".
3769 * If there is any such internal structure in the input,
3770 * then the name of the corresponding space is also removed.
3772 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3773 __isl_take isl_multi_aff
*ma
)
3780 if (!ma
->space
->nested
[0])
3783 space
= isl_multi_aff_get_space(ma
);
3784 space
= isl_space_flatten_domain(space
);
3785 ma
= isl_multi_aff_reset_space(ma
, space
);
3790 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3791 * of the space to its domain.
3793 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3796 isl_local_space
*ls
;
3801 if (!isl_space_is_map(space
))
3802 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3803 "not a map space", goto error
);
3805 n_in
= isl_space_dim(space
, isl_dim_in
);
3806 space
= isl_space_domain_map(space
);
3808 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3810 isl_space_free(space
);
3814 space
= isl_space_domain(space
);
3815 ls
= isl_local_space_from_space(space
);
3816 for (i
= 0; i
< n_in
; ++i
) {
3819 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3821 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3823 isl_local_space_free(ls
);
3826 isl_space_free(space
);
3830 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3831 * of the space to its range.
3833 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3836 isl_local_space
*ls
;
3841 if (!isl_space_is_map(space
))
3842 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3843 "not a map space", goto error
);
3845 n_in
= isl_space_dim(space
, isl_dim_in
);
3846 n_out
= isl_space_dim(space
, isl_dim_out
);
3847 space
= isl_space_range_map(space
);
3849 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3851 isl_space_free(space
);
3855 space
= isl_space_domain(space
);
3856 ls
= isl_local_space_from_space(space
);
3857 for (i
= 0; i
< n_out
; ++i
) {
3860 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3861 isl_dim_set
, n_in
+ i
);
3862 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3864 isl_local_space_free(ls
);
3867 isl_space_free(space
);
3871 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3872 * of the space to its range.
3874 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3875 __isl_take isl_space
*space
)
3877 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3880 /* Given the space of a set and a range of set dimensions,
3881 * construct an isl_multi_aff that projects out those dimensions.
3883 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3884 __isl_take isl_space
*space
, enum isl_dim_type type
,
3885 unsigned first
, unsigned n
)
3888 isl_local_space
*ls
;
3893 if (!isl_space_is_set(space
))
3894 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3895 "expecting set space", goto error
);
3896 if (type
!= isl_dim_set
)
3897 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3898 "only set dimensions can be projected out", goto error
);
3900 dim
= isl_space_dim(space
, isl_dim_set
);
3901 if (first
+ n
> dim
)
3902 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3903 "range out of bounds", goto error
);
3905 space
= isl_space_from_domain(space
);
3906 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3909 return isl_multi_aff_alloc(space
);
3911 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3912 space
= isl_space_domain(space
);
3913 ls
= isl_local_space_from_space(space
);
3915 for (i
= 0; i
< first
; ++i
) {
3918 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3920 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3923 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3926 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3927 isl_dim_set
, first
+ n
+ i
);
3928 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3931 isl_local_space_free(ls
);
3934 isl_space_free(space
);
3938 /* Given the space of a set and a range of set dimensions,
3939 * construct an isl_pw_multi_aff that projects out those dimensions.
3941 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3942 __isl_take isl_space
*space
, enum isl_dim_type type
,
3943 unsigned first
, unsigned n
)
3947 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3948 return isl_pw_multi_aff_from_multi_aff(ma
);
3951 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3954 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3955 __isl_take isl_multi_aff
*ma
)
3957 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3958 return isl_pw_multi_aff_alloc(dom
, ma
);
3961 /* Create a piecewise multi-affine expression in the given space that maps each
3962 * input dimension to the corresponding output dimension.
3964 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3965 __isl_take isl_space
*space
)
3967 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3970 /* Exploit the equalities in "eq" to simplify the affine expressions.
3972 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3973 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3977 maff
= isl_multi_aff_cow(maff
);
3981 for (i
= 0; i
< maff
->n
; ++i
) {
3982 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3983 isl_basic_set_copy(eq
));
3988 isl_basic_set_free(eq
);
3991 isl_basic_set_free(eq
);
3992 isl_multi_aff_free(maff
);
3996 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4001 maff
= isl_multi_aff_cow(maff
);
4005 for (i
= 0; i
< maff
->n
; ++i
) {
4006 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4008 return isl_multi_aff_free(maff
);
4014 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4015 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4017 maff1
= isl_multi_aff_add(maff1
, maff2
);
4018 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4022 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4030 /* Return the set of domain elements where "ma1" is lexicographically
4031 * smaller than or equal to "ma2".
4033 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4034 __isl_take isl_multi_aff
*ma2
)
4036 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4039 /* Return the set of domain elements where "ma1" is lexicographically
4040 * greater than or equal to "ma2".
4042 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4043 __isl_take isl_multi_aff
*ma2
)
4046 isl_map
*map1
, *map2
;
4049 map1
= isl_map_from_multi_aff(ma1
);
4050 map2
= isl_map_from_multi_aff(ma2
);
4051 map
= isl_map_range_product(map1
, map2
);
4052 space
= isl_space_range(isl_map_get_space(map
));
4053 space
= isl_space_domain(isl_space_unwrap(space
));
4054 ge
= isl_map_lex_ge(space
);
4055 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4057 return isl_map_domain(map
);
4061 #define PW isl_pw_multi_aff
4063 #define EL isl_multi_aff
4065 #define EL_IS_ZERO is_empty
4069 #define IS_ZERO is_empty
4072 #undef DEFAULT_IS_ZERO
4073 #define DEFAULT_IS_ZERO 0
4078 #define NO_INVOLVES_DIMS
4079 #define NO_INSERT_DIMS
4083 #include <isl_pw_templ.c>
4088 #define UNION isl_union_pw_multi_aff
4090 #define PART isl_pw_multi_aff
4092 #define PARTS pw_multi_aff
4094 #include <isl_union_multi.c>
4095 #include <isl_union_neg.c>
4097 /* Given a function "cmp" that returns the set of elements where
4098 * "ma1" is "better" than "ma2", return the intersection of this
4099 * set with "dom1" and "dom2".
4101 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
4102 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
4103 __isl_keep isl_multi_aff
*ma2
,
4104 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4105 __isl_take isl_multi_aff
*ma2
))
4111 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
4112 is_empty
= isl_set_plain_is_empty(common
);
4113 if (is_empty
>= 0 && is_empty
)
4116 return isl_set_free(common
);
4117 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
4118 better
= isl_set_intersect(common
, better
);
4123 /* Given a function "cmp" that returns the set of elements where
4124 * "ma1" is "better" than "ma2", return a piecewise multi affine
4125 * expression defined on the union of the definition domains
4126 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4127 * "pma2" on each cell. If only one of the two input functions
4128 * is defined on a given cell, then it is considered the best.
4130 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
4131 __isl_take isl_pw_multi_aff
*pma1
,
4132 __isl_take isl_pw_multi_aff
*pma2
,
4133 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4134 __isl_take isl_multi_aff
*ma2
))
4137 isl_pw_multi_aff
*res
= NULL
;
4139 isl_set
*set
= NULL
;
4144 ctx
= isl_space_get_ctx(pma1
->dim
);
4145 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
4146 isl_die(ctx
, isl_error_invalid
,
4147 "arguments should live in the same space", goto error
);
4149 if (isl_pw_multi_aff_is_empty(pma1
)) {
4150 isl_pw_multi_aff_free(pma1
);
4154 if (isl_pw_multi_aff_is_empty(pma2
)) {
4155 isl_pw_multi_aff_free(pma2
);
4159 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4160 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4162 for (i
= 0; i
< pma1
->n
; ++i
) {
4163 set
= isl_set_copy(pma1
->p
[i
].set
);
4164 for (j
= 0; j
< pma2
->n
; ++j
) {
4168 better
= shared_and_better(pma2
->p
[j
].set
,
4169 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4170 pma1
->p
[i
].maff
, cmp
);
4171 is_empty
= isl_set_plain_is_empty(better
);
4172 if (is_empty
< 0 || is_empty
) {
4173 isl_set_free(better
);
4178 set
= isl_set_subtract(set
, isl_set_copy(better
));
4180 res
= isl_pw_multi_aff_add_piece(res
, better
,
4181 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4183 res
= isl_pw_multi_aff_add_piece(res
, set
,
4184 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4187 for (j
= 0; j
< pma2
->n
; ++j
) {
4188 set
= isl_set_copy(pma2
->p
[j
].set
);
4189 for (i
= 0; i
< pma1
->n
; ++i
)
4190 set
= isl_set_subtract(set
,
4191 isl_set_copy(pma1
->p
[i
].set
));
4192 res
= isl_pw_multi_aff_add_piece(res
, set
,
4193 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4196 isl_pw_multi_aff_free(pma1
);
4197 isl_pw_multi_aff_free(pma2
);
4201 isl_pw_multi_aff_free(pma1
);
4202 isl_pw_multi_aff_free(pma2
);
4204 return isl_pw_multi_aff_free(res
);
4207 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4208 __isl_take isl_pw_multi_aff
*pma1
,
4209 __isl_take isl_pw_multi_aff
*pma2
)
4211 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4214 /* Given two piecewise multi affine expressions, return a piecewise
4215 * multi-affine expression defined on the union of the definition domains
4216 * of the inputs that is equal to the lexicographic maximum of the two
4217 * inputs on each cell. If only one of the two inputs is defined on
4218 * a given cell, then it is considered to be the maximum.
4220 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4221 __isl_take isl_pw_multi_aff
*pma1
,
4222 __isl_take isl_pw_multi_aff
*pma2
)
4224 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4225 &pw_multi_aff_union_lexmax
);
4228 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4229 __isl_take isl_pw_multi_aff
*pma1
,
4230 __isl_take isl_pw_multi_aff
*pma2
)
4232 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4235 /* Given two piecewise multi affine expressions, return a piecewise
4236 * multi-affine expression defined on the union of the definition domains
4237 * of the inputs that is equal to the lexicographic minimum of the two
4238 * inputs on each cell. If only one of the two inputs is defined on
4239 * a given cell, then it is considered to be the minimum.
4241 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4242 __isl_take isl_pw_multi_aff
*pma1
,
4243 __isl_take isl_pw_multi_aff
*pma2
)
4245 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4246 &pw_multi_aff_union_lexmin
);
4249 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4250 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4252 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4253 &isl_multi_aff_add
);
4256 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4257 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4259 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4263 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4264 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4266 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4267 &isl_multi_aff_sub
);
4270 /* Subtract "pma2" from "pma1" and return the result.
4272 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4273 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4275 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4279 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4280 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4282 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4285 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4286 * with the actual sum on the shared domain and
4287 * the defined expression on the symmetric difference of the domains.
4289 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4290 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4292 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4295 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4296 * with the actual sum on the shared domain and
4297 * the defined expression on the symmetric difference of the domains.
4299 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4300 __isl_take isl_union_pw_multi_aff
*upma1
,
4301 __isl_take isl_union_pw_multi_aff
*upma2
)
4303 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4306 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4307 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4309 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4310 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4314 isl_pw_multi_aff
*res
;
4319 n
= pma1
->n
* pma2
->n
;
4320 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4321 isl_space_copy(pma2
->dim
));
4322 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4324 for (i
= 0; i
< pma1
->n
; ++i
) {
4325 for (j
= 0; j
< pma2
->n
; ++j
) {
4329 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4330 isl_set_copy(pma2
->p
[j
].set
));
4331 ma
= isl_multi_aff_product(
4332 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4333 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4334 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4338 isl_pw_multi_aff_free(pma1
);
4339 isl_pw_multi_aff_free(pma2
);
4342 isl_pw_multi_aff_free(pma1
);
4343 isl_pw_multi_aff_free(pma2
);
4347 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4348 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4350 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4351 &pw_multi_aff_product
);
4354 /* Construct a map mapping the domain of the piecewise multi-affine expression
4355 * to its range, with each dimension in the range equated to the
4356 * corresponding affine expression on its cell.
4358 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4366 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4368 for (i
= 0; i
< pma
->n
; ++i
) {
4369 isl_multi_aff
*maff
;
4370 isl_basic_map
*bmap
;
4373 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4374 bmap
= isl_basic_map_from_multi_aff(maff
);
4375 map_i
= isl_map_from_basic_map(bmap
);
4376 map_i
= isl_map_intersect_domain(map_i
,
4377 isl_set_copy(pma
->p
[i
].set
));
4378 map
= isl_map_union_disjoint(map
, map_i
);
4381 isl_pw_multi_aff_free(pma
);
4385 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4390 if (!isl_space_is_set(pma
->dim
))
4391 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4392 "isl_pw_multi_aff cannot be converted into an isl_set",
4395 return isl_map_from_pw_multi_aff(pma
);
4397 isl_pw_multi_aff_free(pma
);
4401 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4402 * denominator "denom".
4403 * "denom" is allowed to be negative, in which case the actual denominator
4404 * is -denom and the expressions are added instead.
4406 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4407 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4413 first
= isl_seq_first_non_zero(c
, n
);
4417 sign
= isl_int_sgn(denom
);
4419 isl_int_abs(d
, denom
);
4420 for (i
= first
; i
< n
; ++i
) {
4423 if (isl_int_is_zero(c
[i
]))
4425 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4426 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4427 aff_i
= isl_aff_scale_down(aff_i
, d
);
4429 aff
= isl_aff_sub(aff
, aff_i
);
4431 aff
= isl_aff_add(aff
, aff_i
);
4438 /* Extract an affine expression that expresses the output dimension "pos"
4439 * of "bmap" in terms of the parameters and input dimensions from
4441 * Note that this expression may involve integer divisions defined
4442 * in terms of parameters and input dimensions.
4443 * The equality may also involve references to earlier (but not later)
4444 * output dimensions. These are replaced by the corresponding elements
4447 * If the equality is of the form
4449 * f(i) + h(j) + a x + g(i) = 0,
4451 * with f(i) a linear combinations of the parameters and input dimensions,
4452 * g(i) a linear combination of integer divisions defined in terms of the same
4453 * and h(j) a linear combinations of earlier output dimensions,
4454 * then the affine expression is
4456 * (-f(i) - g(i))/a - h(j)/a
4458 * If the equality is of the form
4460 * f(i) + h(j) - a x + g(i) = 0,
4462 * then the affine expression is
4464 * (f(i) + g(i))/a - h(j)/(-a)
4467 * If "div" refers to an integer division (i.e., it is smaller than
4468 * the number of integer divisions), then the equality constraint
4469 * does involve an integer division (the one at position "div") that
4470 * is defined in terms of output dimensions. However, this integer
4471 * division can be eliminated by exploiting a pair of constraints
4472 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4473 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4475 * In particular, let
4477 * x = e(i) + m floor(...)
4479 * with e(i) the expression derived above and floor(...) the integer
4480 * division involving output dimensions.
4491 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4492 * = (e(i) - l) mod m
4496 * x - l = (e(i) - l) mod m
4500 * x = ((e(i) - l) mod m) + l
4502 * The variable "shift" below contains the expression -l, which may
4503 * also involve a linear combination of earlier output dimensions.
4505 static __isl_give isl_aff
*extract_aff_from_equality(
4506 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4507 __isl_keep isl_multi_aff
*ma
)
4510 unsigned n_div
, n_out
;
4512 isl_local_space
*ls
;
4513 isl_aff
*aff
, *shift
;
4516 ctx
= isl_basic_map_get_ctx(bmap
);
4517 ls
= isl_basic_map_get_local_space(bmap
);
4518 ls
= isl_local_space_domain(ls
);
4519 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4522 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4523 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4524 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4525 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4526 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4527 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4528 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4530 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4531 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4532 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4535 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4536 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4537 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4538 bmap
->eq
[eq
][o_out
+ pos
]);
4540 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4543 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4544 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4545 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4546 isl_int_set_si(shift
->v
->el
[0], 1);
4547 shift
= subtract_initial(shift
, ma
, pos
,
4548 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4549 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4550 mod
= isl_val_int_from_isl_int(ctx
,
4551 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4552 mod
= isl_val_abs(mod
);
4553 aff
= isl_aff_mod_val(aff
, mod
);
4554 aff
= isl_aff_sub(aff
, shift
);
4557 isl_local_space_free(ls
);
4560 isl_local_space_free(ls
);
4565 /* Given a basic map with output dimensions defined
4566 * in terms of the parameters input dimensions and earlier
4567 * output dimensions using an equality (and possibly a pair on inequalities),
4568 * extract an isl_aff that expresses output dimension "pos" in terms
4569 * of the parameters and input dimensions.
4570 * Note that this expression may involve integer divisions defined
4571 * in terms of parameters and input dimensions.
4572 * "ma" contains the expressions corresponding to earlier output dimensions.
4574 * This function shares some similarities with
4575 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4577 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4578 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4585 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4586 if (eq
>= bmap
->n_eq
)
4587 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4588 "unable to find suitable equality", return NULL
);
4589 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4591 aff
= isl_aff_remove_unused_divs(aff
);
4595 /* Given a basic map where each output dimension is defined
4596 * in terms of the parameters and input dimensions using an equality,
4597 * extract an isl_multi_aff that expresses the output dimensions in terms
4598 * of the parameters and input dimensions.
4600 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4601 __isl_take isl_basic_map
*bmap
)
4610 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4611 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4613 for (i
= 0; i
< n_out
; ++i
) {
4616 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4617 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4620 isl_basic_map_free(bmap
);
4625 /* Given a basic set where each set dimension is defined
4626 * in terms of the parameters using an equality,
4627 * extract an isl_multi_aff that expresses the set dimensions in terms
4628 * of the parameters.
4630 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4631 __isl_take isl_basic_set
*bset
)
4633 return extract_isl_multi_aff_from_basic_map(bset
);
4636 /* Create an isl_pw_multi_aff that is equivalent to
4637 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4638 * The given basic map is such that each output dimension is defined
4639 * in terms of the parameters and input dimensions using an equality.
4641 * Since some applications expect the result of isl_pw_multi_aff_from_map
4642 * to only contain integer affine expressions, we compute the floor
4643 * of the expression before returning.
4645 * Remove all constraints involving local variables without
4646 * an explicit representation (resulting in the removal of those
4647 * local variables) prior to the actual extraction to ensure
4648 * that the local spaces in which the resulting affine expressions
4649 * are created do not contain any unknown local variables.
4650 * Removing such constraints is safe because constraints involving
4651 * unknown local variables are not used to determine whether
4652 * a basic map is obviously single-valued.
4654 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4655 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4659 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4660 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4661 ma
= isl_multi_aff_floor(ma
);
4662 return isl_pw_multi_aff_alloc(domain
, ma
);
4665 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4666 * This obviously only works if the input "map" is single-valued.
4667 * If so, we compute the lexicographic minimum of the image in the form
4668 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4669 * to its lexicographic minimum.
4670 * If the input is not single-valued, we produce an error.
4672 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4673 __isl_take isl_map
*map
)
4677 isl_pw_multi_aff
*pma
;
4679 sv
= isl_map_is_single_valued(map
);
4683 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4684 "map is not single-valued", goto error
);
4685 map
= isl_map_make_disjoint(map
);
4689 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4691 for (i
= 0; i
< map
->n
; ++i
) {
4692 isl_pw_multi_aff
*pma_i
;
4693 isl_basic_map
*bmap
;
4694 bmap
= isl_basic_map_copy(map
->p
[i
]);
4695 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4696 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4706 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4707 * taking into account that the output dimension at position "d"
4708 * can be represented as
4710 * x = floor((e(...) + c1) / m)
4712 * given that constraint "i" is of the form
4714 * e(...) + c1 - m x >= 0
4717 * Let "map" be of the form
4721 * We construct a mapping
4723 * A -> [A -> x = floor(...)]
4725 * apply that to the map, obtaining
4727 * [A -> x = floor(...)] -> B
4729 * and equate dimension "d" to x.
4730 * We then compute a isl_pw_multi_aff representation of the resulting map
4731 * and plug in the mapping above.
4733 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4734 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4738 isl_local_space
*ls
;
4746 isl_pw_multi_aff
*pma
;
4749 is_set
= isl_map_is_set(map
);
4751 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4752 ctx
= isl_map_get_ctx(map
);
4753 space
= isl_space_domain(isl_map_get_space(map
));
4754 n_in
= isl_space_dim(space
, isl_dim_set
);
4755 n
= isl_space_dim(space
, isl_dim_all
);
4757 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4759 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4760 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4762 isl_basic_map_free(hull
);
4764 ls
= isl_local_space_from_space(isl_space_copy(space
));
4765 aff
= isl_aff_alloc_vec(ls
, v
);
4766 aff
= isl_aff_floor(aff
);
4768 isl_space_free(space
);
4769 ma
= isl_multi_aff_from_aff(aff
);
4771 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4772 ma
= isl_multi_aff_range_product(ma
,
4773 isl_multi_aff_from_aff(aff
));
4776 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4777 map
= isl_map_apply_domain(map
, insert
);
4778 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4779 pma
= isl_pw_multi_aff_from_map(map
);
4780 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4785 /* Is constraint "c" of the form
4787 * e(...) + c1 - m x >= 0
4791 * -e(...) + c2 + m x >= 0
4793 * where m > 1 and e only depends on parameters and input dimemnsions?
4795 * "offset" is the offset of the output dimensions
4796 * "pos" is the position of output dimension x.
4798 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4800 if (isl_int_is_zero(c
[offset
+ d
]))
4802 if (isl_int_is_one(c
[offset
+ d
]))
4804 if (isl_int_is_negone(c
[offset
+ d
]))
4806 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4808 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4809 total
- (offset
+ d
+ 1)) != -1)
4814 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4816 * As a special case, we first check if there is any pair of constraints,
4817 * shared by all the basic maps in "map" that force a given dimension
4818 * to be equal to the floor of some affine combination of the input dimensions.
4820 * In particular, if we can find two constraints
4822 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4826 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4828 * where m > 1 and e only depends on parameters and input dimemnsions,
4831 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4833 * then we know that we can take
4835 * x = floor((e(...) + c1) / m)
4837 * without having to perform any computation.
4839 * Note that we know that
4843 * If c1 + c2 were 0, then we would have detected an equality during
4844 * simplification. If c1 + c2 were negative, then we would have detected
4847 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4848 __isl_take isl_map
*map
)
4854 isl_basic_map
*hull
;
4856 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4861 dim
= isl_map_dim(map
, isl_dim_out
);
4862 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4863 total
= 1 + isl_basic_map_total_dim(hull
);
4865 for (d
= 0; d
< dim
; ++d
) {
4866 for (i
= 0; i
< n
; ++i
) {
4867 if (!is_potential_div_constraint(hull
->ineq
[i
],
4870 for (j
= i
+ 1; j
< n
; ++j
) {
4871 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4872 hull
->ineq
[j
] + 1, total
- 1))
4874 isl_int_add(sum
, hull
->ineq
[i
][0],
4876 if (isl_int_abs_lt(sum
,
4877 hull
->ineq
[i
][offset
+ d
]))
4884 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4886 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4890 isl_basic_map_free(hull
);
4891 return pw_multi_aff_from_map_base(map
);
4894 isl_basic_map_free(hull
);
4898 /* Given an affine expression
4900 * [A -> B] -> f(A,B)
4902 * construct an isl_multi_aff
4906 * such that dimension "d" in B' is set to "aff" and the remaining
4907 * dimensions are set equal to the corresponding dimensions in B.
4908 * "n_in" is the dimension of the space A.
4909 * "n_out" is the dimension of the space B.
4911 * If "is_set" is set, then the affine expression is of the form
4915 * and we construct an isl_multi_aff
4919 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4920 unsigned n_in
, unsigned n_out
, int is_set
)
4924 isl_space
*space
, *space2
;
4925 isl_local_space
*ls
;
4927 space
= isl_aff_get_domain_space(aff
);
4928 ls
= isl_local_space_from_space(isl_space_copy(space
));
4929 space2
= isl_space_copy(space
);
4931 space2
= isl_space_range(isl_space_unwrap(space2
));
4932 space
= isl_space_map_from_domain_and_range(space
, space2
);
4933 ma
= isl_multi_aff_alloc(space
);
4934 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4936 for (i
= 0; i
< n_out
; ++i
) {
4939 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4940 isl_dim_set
, n_in
+ i
);
4941 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4944 isl_local_space_free(ls
);
4949 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4950 * taking into account that the dimension at position "d" can be written as
4952 * x = m a + f(..) (1)
4954 * where m is equal to "gcd".
4955 * "i" is the index of the equality in "hull" that defines f(..).
4956 * In particular, the equality is of the form
4958 * f(..) - x + m g(existentials) = 0
4962 * -f(..) + x + m g(existentials) = 0
4964 * We basically plug (1) into "map", resulting in a map with "a"
4965 * in the range instead of "x". The corresponding isl_pw_multi_aff
4966 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4968 * Specifically, given the input map
4972 * We first wrap it into a set
4976 * and define (1) on top of the corresponding space, resulting in "aff".
4977 * We use this to create an isl_multi_aff that maps the output position "d"
4978 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4979 * We plug this into the wrapped map, unwrap the result and compute the
4980 * corresponding isl_pw_multi_aff.
4981 * The result is an expression
4989 * so that we can plug that into "aff", after extending the latter to
4995 * If "map" is actually a set, then there is no "A" space, meaning
4996 * that we do not need to perform any wrapping, and that the result
4997 * of the recursive call is of the form
5001 * which is plugged into a mapping of the form
5005 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5006 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5011 isl_local_space
*ls
;
5014 isl_pw_multi_aff
*pma
, *id
;
5020 is_set
= isl_map_is_set(map
);
5022 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5023 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5024 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5029 set
= isl_map_wrap(map
);
5030 space
= isl_space_map_from_set(isl_set_get_space(set
));
5031 ma
= isl_multi_aff_identity(space
);
5032 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5033 aff
= isl_aff_alloc(ls
);
5035 isl_int_set_si(aff
->v
->el
[0], 1);
5036 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5037 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5040 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5042 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5044 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5045 set
= isl_set_preimage_multi_aff(set
, ma
);
5047 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5052 map
= isl_set_unwrap(set
);
5053 pma
= isl_pw_multi_aff_from_map(map
);
5056 space
= isl_pw_multi_aff_get_domain_space(pma
);
5057 space
= isl_space_map_from_set(space
);
5058 id
= isl_pw_multi_aff_identity(space
);
5059 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5061 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5062 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5064 isl_basic_map_free(hull
);
5068 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5070 * As a special case, we first check if all output dimensions are uniquely
5071 * defined in terms of the parameters and input dimensions over the entire
5072 * domain. If so, we extract the desired isl_pw_multi_aff directly
5073 * from the affine hull of "map" and its domain.
5075 * Otherwise, we check if any of the output dimensions is "strided".
5076 * That is, we check if can be written as
5080 * with m greater than 1, a some combination of existentially quantified
5081 * variables and f an expression in the parameters and input dimensions.
5082 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5084 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5087 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5091 isl_basic_map
*hull
;
5101 map
= isl_map_detect_equalities(map
);
5102 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5103 sv
= isl_basic_map_plain_is_single_valued(hull
);
5105 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5107 hull
= isl_basic_map_free(hull
);
5111 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5112 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5115 isl_basic_map_free(hull
);
5116 return pw_multi_aff_from_map_check_div(map
);
5121 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5122 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5124 for (i
= 0; i
< n_out
; ++i
) {
5125 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5126 isl_int
*eq
= hull
->eq
[j
];
5127 isl_pw_multi_aff
*res
;
5129 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5130 !isl_int_is_negone(eq
[o_out
+ i
]))
5132 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5134 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5135 n_out
- (i
+ 1)) != -1)
5137 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5138 if (isl_int_is_zero(gcd
))
5140 if (isl_int_is_one(gcd
))
5143 res
= pw_multi_aff_from_map_stride(map
, hull
,
5151 isl_basic_map_free(hull
);
5152 return pw_multi_aff_from_map_check_div(map
);
5158 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5160 return isl_pw_multi_aff_from_map(set
);
5163 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5166 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5168 isl_union_pw_multi_aff
**upma
= user
;
5169 isl_pw_multi_aff
*pma
;
5171 pma
= isl_pw_multi_aff_from_map(map
);
5172 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5174 return *upma
? isl_stat_ok
: isl_stat_error
;
5177 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5180 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5181 __isl_take isl_aff
*aff
)
5184 isl_pw_multi_aff
*pma
;
5186 ma
= isl_multi_aff_from_aff(aff
);
5187 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5188 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5191 /* Try and create an isl_union_pw_multi_aff that is equivalent
5192 * to the given isl_union_map.
5193 * The isl_union_map is required to be single-valued in each space.
5194 * Otherwise, an error is produced.
5196 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5197 __isl_take isl_union_map
*umap
)
5200 isl_union_pw_multi_aff
*upma
;
5202 space
= isl_union_map_get_space(umap
);
5203 upma
= isl_union_pw_multi_aff_empty(space
);
5204 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5205 upma
= isl_union_pw_multi_aff_free(upma
);
5206 isl_union_map_free(umap
);
5211 /* Try and create an isl_union_pw_multi_aff that is equivalent
5212 * to the given isl_union_set.
5213 * The isl_union_set is required to be a singleton in each space.
5214 * Otherwise, an error is produced.
5216 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5217 __isl_take isl_union_set
*uset
)
5219 return isl_union_pw_multi_aff_from_union_map(uset
);
5222 /* Return the piecewise affine expression "set ? 1 : 0".
5224 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5227 isl_space
*space
= isl_set_get_space(set
);
5228 isl_local_space
*ls
= isl_local_space_from_space(space
);
5229 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5230 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5232 one
= isl_aff_add_constant_si(one
, 1);
5233 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5234 set
= isl_set_complement(set
);
5235 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5240 /* Plug in "subs" for dimension "type", "pos" of "aff".
5242 * Let i be the dimension to replace and let "subs" be of the form
5246 * and "aff" of the form
5252 * (a f + d g')/(m d)
5254 * where g' is the result of plugging in "subs" in each of the integer
5257 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5258 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5263 aff
= isl_aff_cow(aff
);
5265 return isl_aff_free(aff
);
5267 ctx
= isl_aff_get_ctx(aff
);
5268 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5269 isl_die(ctx
, isl_error_invalid
,
5270 "spaces don't match", return isl_aff_free(aff
));
5271 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5272 isl_die(ctx
, isl_error_unsupported
,
5273 "cannot handle divs yet", return isl_aff_free(aff
));
5275 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5277 return isl_aff_free(aff
);
5279 aff
->v
= isl_vec_cow(aff
->v
);
5281 return isl_aff_free(aff
);
5283 pos
+= isl_local_space_offset(aff
->ls
, type
);
5286 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5287 aff
->v
->size
, subs
->v
->size
, v
);
5293 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5294 * expressions in "maff".
5296 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5297 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5298 __isl_keep isl_aff
*subs
)
5302 maff
= isl_multi_aff_cow(maff
);
5304 return isl_multi_aff_free(maff
);
5306 if (type
== isl_dim_in
)
5309 for (i
= 0; i
< maff
->n
; ++i
) {
5310 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5312 return isl_multi_aff_free(maff
);
5318 /* Plug in "subs" for dimension "type", "pos" of "pma".
5320 * pma is of the form
5324 * while subs is of the form
5326 * v' = B_j(v) -> S_j
5328 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5329 * has a contribution in the result, in particular
5331 * C_ij(S_j) -> M_i(S_j)
5333 * Note that plugging in S_j in C_ij may also result in an empty set
5334 * and this contribution should simply be discarded.
5336 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5337 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5338 __isl_keep isl_pw_aff
*subs
)
5341 isl_pw_multi_aff
*res
;
5344 return isl_pw_multi_aff_free(pma
);
5346 n
= pma
->n
* subs
->n
;
5347 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5349 for (i
= 0; i
< pma
->n
; ++i
) {
5350 for (j
= 0; j
< subs
->n
; ++j
) {
5352 isl_multi_aff
*res_ij
;
5355 common
= isl_set_intersect(
5356 isl_set_copy(pma
->p
[i
].set
),
5357 isl_set_copy(subs
->p
[j
].set
));
5358 common
= isl_set_substitute(common
,
5359 type
, pos
, subs
->p
[j
].aff
);
5360 empty
= isl_set_plain_is_empty(common
);
5361 if (empty
< 0 || empty
) {
5362 isl_set_free(common
);
5368 res_ij
= isl_multi_aff_substitute(
5369 isl_multi_aff_copy(pma
->p
[i
].maff
),
5370 type
, pos
, subs
->p
[j
].aff
);
5372 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5376 isl_pw_multi_aff_free(pma
);
5379 isl_pw_multi_aff_free(pma
);
5380 isl_pw_multi_aff_free(res
);
5384 /* Compute the preimage of a range of dimensions in the affine expression "src"
5385 * under "ma" and put the result in "dst". The number of dimensions in "src"
5386 * that precede the range is given by "n_before". The number of dimensions
5387 * in the range is given by the number of output dimensions of "ma".
5388 * The number of dimensions that follow the range is given by "n_after".
5389 * If "has_denom" is set (to one),
5390 * then "src" and "dst" have an extra initial denominator.
5391 * "n_div_ma" is the number of existentials in "ma"
5392 * "n_div_bset" is the number of existentials in "src"
5393 * The resulting "dst" (which is assumed to have been allocated by
5394 * the caller) contains coefficients for both sets of existentials,
5395 * first those in "ma" and then those in "src".
5396 * f, c1, c2 and g are temporary objects that have been initialized
5399 * Let src represent the expression
5401 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5403 * and let ma represent the expressions
5405 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5407 * We start out with the following expression for dst:
5409 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5411 * with the multiplication factor f initially equal to 1
5412 * and f \sum_i b_i v_i kept separately.
5413 * For each x_i that we substitute, we multiply the numerator
5414 * (and denominator) of dst by c_1 = m_i and add the numerator
5415 * of the x_i expression multiplied by c_2 = f b_i,
5416 * after removing the common factors of c_1 and c_2.
5417 * The multiplication factor f also needs to be multiplied by c_1
5418 * for the next x_j, j > i.
5420 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5421 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5422 int n_div_ma
, int n_div_bmap
,
5423 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5426 int n_param
, n_in
, n_out
;
5429 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5430 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5431 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5433 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5434 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5435 isl_seq_clr(dst
+ o_dst
, n_in
);
5438 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5441 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5443 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5445 isl_int_set_si(f
, 1);
5447 for (i
= 0; i
< n_out
; ++i
) {
5448 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5450 if (isl_int_is_zero(src
[offset
]))
5452 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5453 isl_int_mul(c2
, f
, src
[offset
]);
5454 isl_int_gcd(g
, c1
, c2
);
5455 isl_int_divexact(c1
, c1
, g
);
5456 isl_int_divexact(c2
, c2
, g
);
5458 isl_int_mul(f
, f
, c1
);
5461 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5462 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5463 o_dst
+= 1 + n_param
;
5464 o_src
+= 1 + n_param
;
5465 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5467 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5468 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5471 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5473 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5474 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5477 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5479 isl_int_mul(dst
[0], dst
[0], c1
);
5483 /* Compute the pullback of "aff" by the function represented by "ma".
5484 * In other words, plug in "ma" in "aff". The result is an affine expression
5485 * defined over the domain space of "ma".
5487 * If "aff" is represented by
5489 * (a(p) + b x + c(divs))/d
5491 * and ma is represented by
5493 * x = D(p) + F(y) + G(divs')
5495 * then the result is
5497 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5499 * The divs in the local space of the input are similarly adjusted
5500 * through a call to isl_local_space_preimage_multi_aff.
5502 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5503 __isl_take isl_multi_aff
*ma
)
5505 isl_aff
*res
= NULL
;
5506 isl_local_space
*ls
;
5507 int n_div_aff
, n_div_ma
;
5508 isl_int f
, c1
, c2
, g
;
5510 ma
= isl_multi_aff_align_divs(ma
);
5514 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5515 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5517 ls
= isl_aff_get_domain_local_space(aff
);
5518 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5519 res
= isl_aff_alloc(ls
);
5528 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5537 isl_multi_aff_free(ma
);
5538 res
= isl_aff_normalize(res
);
5542 isl_multi_aff_free(ma
);
5547 /* Compute the pullback of "aff1" by the function represented by "aff2".
5548 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5549 * defined over the domain space of "aff1".
5551 * The domain of "aff1" should match the range of "aff2", which means
5552 * that it should be single-dimensional.
5554 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5555 __isl_take isl_aff
*aff2
)
5559 ma
= isl_multi_aff_from_aff(aff2
);
5560 return isl_aff_pullback_multi_aff(aff1
, ma
);
5563 /* Compute the pullback of "ma1" by the function represented by "ma2".
5564 * In other words, plug in "ma2" in "ma1".
5566 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5568 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5569 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5572 isl_space
*space
= NULL
;
5574 ma2
= isl_multi_aff_align_divs(ma2
);
5575 ma1
= isl_multi_aff_cow(ma1
);
5579 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5580 isl_multi_aff_get_space(ma1
));
5582 for (i
= 0; i
< ma1
->n
; ++i
) {
5583 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5584 isl_multi_aff_copy(ma2
));
5589 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5590 isl_multi_aff_free(ma2
);
5593 isl_space_free(space
);
5594 isl_multi_aff_free(ma2
);
5595 isl_multi_aff_free(ma1
);
5599 /* Compute the pullback of "ma1" by the function represented by "ma2".
5600 * In other words, plug in "ma2" in "ma1".
5602 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5603 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5605 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5606 &isl_multi_aff_pullback_multi_aff_aligned
);
5609 /* Extend the local space of "dst" to include the divs
5610 * in the local space of "src".
5612 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5613 __isl_keep isl_aff
*src
)
5621 return isl_aff_free(dst
);
5623 ctx
= isl_aff_get_ctx(src
);
5624 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5625 isl_die(ctx
, isl_error_invalid
,
5626 "spaces don't match", goto error
);
5628 if (src
->ls
->div
->n_row
== 0)
5631 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5632 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5633 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5636 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5637 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5645 return isl_aff_free(dst
);
5648 /* Adjust the local spaces of the affine expressions in "maff"
5649 * such that they all have the save divs.
5651 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5652 __isl_take isl_multi_aff
*maff
)
5660 maff
= isl_multi_aff_cow(maff
);
5664 for (i
= 1; i
< maff
->n
; ++i
)
5665 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5666 for (i
= 1; i
< maff
->n
; ++i
) {
5667 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5669 return isl_multi_aff_free(maff
);
5675 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5677 aff
= isl_aff_cow(aff
);
5681 aff
->ls
= isl_local_space_lift(aff
->ls
);
5683 return isl_aff_free(aff
);
5688 /* Lift "maff" to a space with extra dimensions such that the result
5689 * has no more existentially quantified variables.
5690 * If "ls" is not NULL, then *ls is assigned the local space that lies
5691 * at the basis of the lifting applied to "maff".
5693 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5694 __isl_give isl_local_space
**ls
)
5708 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5709 *ls
= isl_local_space_from_space(space
);
5711 return isl_multi_aff_free(maff
);
5716 maff
= isl_multi_aff_cow(maff
);
5717 maff
= isl_multi_aff_align_divs(maff
);
5721 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5722 space
= isl_multi_aff_get_space(maff
);
5723 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5724 space
= isl_space_extend_domain_with_range(space
,
5725 isl_multi_aff_get_space(maff
));
5727 return isl_multi_aff_free(maff
);
5728 isl_space_free(maff
->space
);
5729 maff
->space
= space
;
5732 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5734 return isl_multi_aff_free(maff
);
5737 for (i
= 0; i
< maff
->n
; ++i
) {
5738 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5746 isl_local_space_free(*ls
);
5747 return isl_multi_aff_free(maff
);
5751 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5753 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5754 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5764 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5765 if (pos
< 0 || pos
>= n_out
)
5766 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5767 "index out of bounds", return NULL
);
5769 space
= isl_pw_multi_aff_get_space(pma
);
5770 space
= isl_space_drop_dims(space
, isl_dim_out
,
5771 pos
+ 1, n_out
- pos
- 1);
5772 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5774 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5775 for (i
= 0; i
< pma
->n
; ++i
) {
5777 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5778 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5784 /* Return an isl_pw_multi_aff with the given "set" as domain and
5785 * an unnamed zero-dimensional range.
5787 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5788 __isl_take isl_set
*set
)
5793 space
= isl_set_get_space(set
);
5794 space
= isl_space_from_domain(space
);
5795 ma
= isl_multi_aff_zero(space
);
5796 return isl_pw_multi_aff_alloc(set
, ma
);
5799 /* Add an isl_pw_multi_aff with the given "set" as domain and
5800 * an unnamed zero-dimensional range to *user.
5802 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5805 isl_union_pw_multi_aff
**upma
= user
;
5806 isl_pw_multi_aff
*pma
;
5808 pma
= isl_pw_multi_aff_from_domain(set
);
5809 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5814 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5815 * an unnamed zero-dimensional range.
5817 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5818 __isl_take isl_union_set
*uset
)
5821 isl_union_pw_multi_aff
*upma
;
5826 space
= isl_union_set_get_space(uset
);
5827 upma
= isl_union_pw_multi_aff_empty(space
);
5829 if (isl_union_set_foreach_set(uset
,
5830 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5833 isl_union_set_free(uset
);
5836 isl_union_set_free(uset
);
5837 isl_union_pw_multi_aff_free(upma
);
5841 /* Convert "pma" to an isl_map and add it to *umap.
5843 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5846 isl_union_map
**umap
= user
;
5849 map
= isl_map_from_pw_multi_aff(pma
);
5850 *umap
= isl_union_map_add_map(*umap
, map
);
5855 /* Construct a union map mapping the domain of the union
5856 * piecewise multi-affine expression to its range, with each dimension
5857 * in the range equated to the corresponding affine expression on its cell.
5859 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5860 __isl_take isl_union_pw_multi_aff
*upma
)
5863 isl_union_map
*umap
;
5868 space
= isl_union_pw_multi_aff_get_space(upma
);
5869 umap
= isl_union_map_empty(space
);
5871 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5872 &map_from_pw_multi_aff
, &umap
) < 0)
5875 isl_union_pw_multi_aff_free(upma
);
5878 isl_union_pw_multi_aff_free(upma
);
5879 isl_union_map_free(umap
);
5883 /* Local data for bin_entry and the callback "fn".
5885 struct isl_union_pw_multi_aff_bin_data
{
5886 isl_union_pw_multi_aff
*upma2
;
5887 isl_union_pw_multi_aff
*res
;
5888 isl_pw_multi_aff
*pma
;
5889 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5892 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5893 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5895 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5897 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5901 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5903 isl_pw_multi_aff_free(pma
);
5908 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5909 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5910 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5911 * as *entry. The callback should adjust data->res if desired.
5913 static __isl_give isl_union_pw_multi_aff
*bin_op(
5914 __isl_take isl_union_pw_multi_aff
*upma1
,
5915 __isl_take isl_union_pw_multi_aff
*upma2
,
5916 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5919 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5921 space
= isl_union_pw_multi_aff_get_space(upma2
);
5922 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5923 space
= isl_union_pw_multi_aff_get_space(upma1
);
5924 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5926 if (!upma1
|| !upma2
)
5930 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5931 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5932 &bin_entry
, &data
) < 0)
5935 isl_union_pw_multi_aff_free(upma1
);
5936 isl_union_pw_multi_aff_free(upma2
);
5939 isl_union_pw_multi_aff_free(upma1
);
5940 isl_union_pw_multi_aff_free(upma2
);
5941 isl_union_pw_multi_aff_free(data
.res
);
5945 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5946 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5948 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5949 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5953 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5954 isl_pw_multi_aff_get_space(pma2
));
5955 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5956 &isl_multi_aff_range_product
);
5959 /* Given two isl_pw_multi_affs A -> B and C -> D,
5960 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5962 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5963 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5965 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5966 &pw_multi_aff_range_product
);
5969 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5970 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5972 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5973 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5977 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5978 isl_pw_multi_aff_get_space(pma2
));
5979 space
= isl_space_flatten_range(space
);
5980 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5981 &isl_multi_aff_flat_range_product
);
5984 /* Given two isl_pw_multi_affs A -> B and C -> D,
5985 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5987 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5988 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5990 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5991 &pw_multi_aff_flat_range_product
);
5994 /* If data->pma and "pma2" have the same domain space, then compute
5995 * their flat range product and the result to data->res.
5997 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6000 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6002 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6003 pma2
->dim
, isl_dim_in
)) {
6004 isl_pw_multi_aff_free(pma2
);
6008 pma2
= isl_pw_multi_aff_flat_range_product(
6009 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6011 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6016 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6017 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6019 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6020 __isl_take isl_union_pw_multi_aff
*upma1
,
6021 __isl_take isl_union_pw_multi_aff
*upma2
)
6023 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6026 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6027 * The parameters are assumed to have been aligned.
6029 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6030 * except that it works on two different isl_pw_* types.
6032 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6033 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6034 __isl_take isl_pw_aff
*pa
)
6037 isl_pw_multi_aff
*res
= NULL
;
6042 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6043 pa
->dim
, isl_dim_in
))
6044 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6045 "domains don't match", goto error
);
6046 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6047 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6048 "index out of bounds", goto error
);
6051 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6053 for (i
= 0; i
< pma
->n
; ++i
) {
6054 for (j
= 0; j
< pa
->n
; ++j
) {
6056 isl_multi_aff
*res_ij
;
6059 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6060 isl_set_copy(pa
->p
[j
].set
));
6061 empty
= isl_set_plain_is_empty(common
);
6062 if (empty
< 0 || empty
) {
6063 isl_set_free(common
);
6069 res_ij
= isl_multi_aff_set_aff(
6070 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6071 isl_aff_copy(pa
->p
[j
].aff
));
6072 res_ij
= isl_multi_aff_gist(res_ij
,
6073 isl_set_copy(common
));
6075 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6079 isl_pw_multi_aff_free(pma
);
6080 isl_pw_aff_free(pa
);
6083 isl_pw_multi_aff_free(pma
);
6084 isl_pw_aff_free(pa
);
6085 return isl_pw_multi_aff_free(res
);
6088 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6090 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6091 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6092 __isl_take isl_pw_aff
*pa
)
6096 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
6097 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6098 if (!isl_space_has_named_params(pma
->dim
) ||
6099 !isl_space_has_named_params(pa
->dim
))
6100 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6101 "unaligned unnamed parameters", goto error
);
6102 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6103 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6104 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6106 isl_pw_multi_aff_free(pma
);
6107 isl_pw_aff_free(pa
);
6111 /* Do the parameters of "pa" match those of "space"?
6113 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6114 __isl_keep isl_space
*space
)
6116 isl_space
*pa_space
;
6122 pa_space
= isl_pw_aff_get_space(pa
);
6124 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6126 isl_space_free(pa_space
);
6130 /* Check that the domain space of "pa" matches "space".
6132 * Return 0 on success and -1 on error.
6134 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6135 __isl_keep isl_space
*space
)
6137 isl_space
*pa_space
;
6143 pa_space
= isl_pw_aff_get_space(pa
);
6145 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6149 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6150 "parameters don't match", goto error
);
6151 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6152 pa_space
, isl_dim_in
);
6156 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6157 "domains don't match", goto error
);
6158 isl_space_free(pa_space
);
6161 isl_space_free(pa_space
);
6170 #include <isl_multi_templ.c>
6171 #include <isl_multi_apply_set.c>
6172 #include <isl_multi_coalesce.c>
6173 #include <isl_multi_gist.c>
6174 #include <isl_multi_intersect.c>
6176 /* Scale the elements of "pma" by the corresponding elements of "mv".
6178 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6179 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6183 pma
= isl_pw_multi_aff_cow(pma
);
6186 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6187 mv
->space
, isl_dim_set
))
6188 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6189 "spaces don't match", goto error
);
6190 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6191 mv
->space
, isl_dim_param
)) {
6192 pma
= isl_pw_multi_aff_align_params(pma
,
6193 isl_multi_val_get_space(mv
));
6194 mv
= isl_multi_val_align_params(mv
,
6195 isl_pw_multi_aff_get_space(pma
));
6200 for (i
= 0; i
< pma
->n
; ++i
) {
6201 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6202 isl_multi_val_copy(mv
));
6203 if (!pma
->p
[i
].maff
)
6207 isl_multi_val_free(mv
);
6210 isl_multi_val_free(mv
);
6211 isl_pw_multi_aff_free(pma
);
6215 /* This function is called for each entry of an isl_union_pw_multi_aff.
6216 * If the space of the entry matches that of data->mv,
6217 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6218 * Otherwise, return an empty isl_pw_multi_aff.
6220 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6221 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6223 isl_multi_val
*mv
= user
;
6227 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6228 mv
->space
, isl_dim_set
)) {
6229 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6230 isl_pw_multi_aff_free(pma
);
6231 return isl_pw_multi_aff_empty(space
);
6234 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6237 /* Scale the elements of "upma" by the corresponding elements of "mv",
6238 * for those entries that match the space of "mv".
6240 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6241 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6243 upma
= isl_union_pw_multi_aff_align_params(upma
,
6244 isl_multi_val_get_space(mv
));
6245 mv
= isl_multi_val_align_params(mv
,
6246 isl_union_pw_multi_aff_get_space(upma
));
6250 return isl_union_pw_multi_aff_transform(upma
,
6251 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6253 isl_multi_val_free(mv
);
6256 isl_multi_val_free(mv
);
6257 isl_union_pw_multi_aff_free(upma
);
6261 /* Construct and return a piecewise multi affine expression
6262 * in the given space with value zero in each of the output dimensions and
6263 * a universe domain.
6265 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6267 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6270 /* Construct and return a piecewise multi affine expression
6271 * that is equal to the given piecewise affine expression.
6273 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6274 __isl_take isl_pw_aff
*pa
)
6278 isl_pw_multi_aff
*pma
;
6283 space
= isl_pw_aff_get_space(pa
);
6284 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6286 for (i
= 0; i
< pa
->n
; ++i
) {
6290 set
= isl_set_copy(pa
->p
[i
].set
);
6291 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6292 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6295 isl_pw_aff_free(pa
);
6299 /* Construct a set or map mapping the shared (parameter) domain
6300 * of the piecewise affine expressions to the range of "mpa"
6301 * with each dimension in the range equated to the
6302 * corresponding piecewise affine expression.
6304 static __isl_give isl_map
*map_from_multi_pw_aff(
6305 __isl_take isl_multi_pw_aff
*mpa
)
6314 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6315 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6316 "invalid space", goto error
);
6318 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6319 map
= isl_map_universe(isl_space_from_domain(space
));
6321 for (i
= 0; i
< mpa
->n
; ++i
) {
6325 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6326 map_i
= map_from_pw_aff(pa
);
6328 map
= isl_map_flat_range_product(map
, map_i
);
6331 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6333 isl_multi_pw_aff_free(mpa
);
6336 isl_multi_pw_aff_free(mpa
);
6340 /* Construct a map mapping the shared domain
6341 * of the piecewise affine expressions to the range of "mpa"
6342 * with each dimension in the range equated to the
6343 * corresponding piecewise affine expression.
6345 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6349 if (isl_space_is_set(mpa
->space
))
6350 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6351 "space of input is not a map", goto error
);
6353 return map_from_multi_pw_aff(mpa
);
6355 isl_multi_pw_aff_free(mpa
);
6359 /* Construct a set mapping the shared parameter domain
6360 * of the piecewise affine expressions to the space of "mpa"
6361 * with each dimension in the range equated to the
6362 * corresponding piecewise affine expression.
6364 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6368 if (!isl_space_is_set(mpa
->space
))
6369 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6370 "space of input is not a set", goto error
);
6372 return map_from_multi_pw_aff(mpa
);
6374 isl_multi_pw_aff_free(mpa
);
6378 /* Construct and return a piecewise multi affine expression
6379 * that is equal to the given multi piecewise affine expression
6380 * on the shared domain of the piecewise affine expressions.
6382 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6383 __isl_take isl_multi_pw_aff
*mpa
)
6388 isl_pw_multi_aff
*pma
;
6393 space
= isl_multi_pw_aff_get_space(mpa
);
6396 isl_multi_pw_aff_free(mpa
);
6397 return isl_pw_multi_aff_zero(space
);
6400 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6401 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6403 for (i
= 1; i
< mpa
->n
; ++i
) {
6404 isl_pw_multi_aff
*pma_i
;
6406 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6407 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6408 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6411 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6413 isl_multi_pw_aff_free(mpa
);
6417 /* Construct and return a multi piecewise affine expression
6418 * that is equal to the given multi affine expression.
6420 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6421 __isl_take isl_multi_aff
*ma
)
6424 isl_multi_pw_aff
*mpa
;
6429 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6430 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6432 for (i
= 0; i
< n
; ++i
) {
6435 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6436 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6439 isl_multi_aff_free(ma
);
6443 /* Construct and return a multi piecewise affine expression
6444 * that is equal to the given piecewise multi affine expression.
6446 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6447 __isl_take isl_pw_multi_aff
*pma
)
6451 isl_multi_pw_aff
*mpa
;
6456 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6457 space
= isl_pw_multi_aff_get_space(pma
);
6458 mpa
= isl_multi_pw_aff_alloc(space
);
6460 for (i
= 0; i
< n
; ++i
) {
6463 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6464 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6467 isl_pw_multi_aff_free(pma
);
6471 /* Do "pa1" and "pa2" represent the same function?
6473 * We first check if they are obviously equal.
6474 * If not, we convert them to maps and check if those are equal.
6476 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6479 isl_map
*map1
, *map2
;
6484 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6485 if (equal
< 0 || equal
)
6488 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6489 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6490 equal
= isl_map_is_equal(map1
, map2
);
6497 /* Do "mpa1" and "mpa2" represent the same function?
6499 * Note that we cannot convert the entire isl_multi_pw_aff
6500 * to a map because the domains of the piecewise affine expressions
6501 * may not be the same.
6503 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6504 __isl_keep isl_multi_pw_aff
*mpa2
)
6510 return isl_bool_error
;
6512 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6513 mpa2
->space
, isl_dim_param
)) {
6514 if (!isl_space_has_named_params(mpa1
->space
))
6515 return isl_bool_false
;
6516 if (!isl_space_has_named_params(mpa2
->space
))
6517 return isl_bool_false
;
6518 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6519 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6520 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6521 isl_multi_pw_aff_get_space(mpa2
));
6522 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6523 isl_multi_pw_aff_get_space(mpa1
));
6524 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6525 isl_multi_pw_aff_free(mpa1
);
6526 isl_multi_pw_aff_free(mpa2
);
6530 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6531 if (equal
< 0 || !equal
)
6534 for (i
= 0; i
< mpa1
->n
; ++i
) {
6535 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6536 if (equal
< 0 || !equal
)
6540 return isl_bool_true
;
6543 /* Compute the pullback of "mpa" by the function represented by "ma".
6544 * In other words, plug in "ma" in "mpa".
6546 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6548 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6549 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6552 isl_space
*space
= NULL
;
6554 mpa
= isl_multi_pw_aff_cow(mpa
);
6558 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6559 isl_multi_pw_aff_get_space(mpa
));
6563 for (i
= 0; i
< mpa
->n
; ++i
) {
6564 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6565 isl_multi_aff_copy(ma
));
6570 isl_multi_aff_free(ma
);
6571 isl_space_free(mpa
->space
);
6575 isl_space_free(space
);
6576 isl_multi_pw_aff_free(mpa
);
6577 isl_multi_aff_free(ma
);
6581 /* Compute the pullback of "mpa" by the function represented by "ma".
6582 * In other words, plug in "ma" in "mpa".
6584 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6585 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6589 if (isl_space_match(mpa
->space
, isl_dim_param
,
6590 ma
->space
, isl_dim_param
))
6591 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6592 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6593 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6594 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6596 isl_multi_pw_aff_free(mpa
);
6597 isl_multi_aff_free(ma
);
6601 /* Compute the pullback of "mpa" by the function represented by "pma".
6602 * In other words, plug in "pma" in "mpa".
6604 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6606 static __isl_give isl_multi_pw_aff
*
6607 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6608 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6611 isl_space
*space
= NULL
;
6613 mpa
= isl_multi_pw_aff_cow(mpa
);
6617 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6618 isl_multi_pw_aff_get_space(mpa
));
6620 for (i
= 0; i
< mpa
->n
; ++i
) {
6621 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6622 isl_pw_multi_aff_copy(pma
));
6627 isl_pw_multi_aff_free(pma
);
6628 isl_space_free(mpa
->space
);
6632 isl_space_free(space
);
6633 isl_multi_pw_aff_free(mpa
);
6634 isl_pw_multi_aff_free(pma
);
6638 /* Compute the pullback of "mpa" by the function represented by "pma".
6639 * In other words, plug in "pma" in "mpa".
6641 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6642 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6646 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6647 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6648 mpa
= isl_multi_pw_aff_align_params(mpa
,
6649 isl_pw_multi_aff_get_space(pma
));
6650 pma
= isl_pw_multi_aff_align_params(pma
,
6651 isl_multi_pw_aff_get_space(mpa
));
6652 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6654 isl_multi_pw_aff_free(mpa
);
6655 isl_pw_multi_aff_free(pma
);
6659 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6660 * with the domain of "aff". The domain of the result is the same
6662 * "mpa" and "aff" are assumed to have been aligned.
6664 * We first extract the parametric constant from "aff", defined
6665 * over the correct domain.
6666 * Then we add the appropriate combinations of the members of "mpa".
6667 * Finally, we add the integer divisions through recursive calls.
6669 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6670 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6678 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6679 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6681 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6682 tmp
= isl_aff_copy(aff
);
6683 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6684 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6685 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6686 isl_space_dim(space
, isl_dim_set
));
6687 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6688 pa
= isl_pw_aff_from_aff(tmp
);
6690 for (i
= 0; i
< n_in
; ++i
) {
6693 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6695 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6696 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6697 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6698 pa
= isl_pw_aff_add(pa
, pa_i
);
6701 for (i
= 0; i
< n_div
; ++i
) {
6705 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6707 div
= isl_aff_get_div(aff
, i
);
6708 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6709 isl_multi_pw_aff_copy(mpa
), div
);
6710 pa_i
= isl_pw_aff_floor(pa_i
);
6711 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6712 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6713 pa
= isl_pw_aff_add(pa
, pa_i
);
6716 isl_multi_pw_aff_free(mpa
);
6722 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6723 * with the domain of "aff". The domain of the result is the same
6726 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6727 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6731 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6732 mpa
->space
, isl_dim_param
))
6733 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6735 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6736 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6738 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6741 isl_multi_pw_aff_free(mpa
);
6745 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6746 * with the domain of "pa". The domain of the result is the same
6748 * "mpa" and "pa" are assumed to have been aligned.
6750 * We consider each piece in turn. Note that the domains of the
6751 * pieces are assumed to be disjoint and they remain disjoint
6752 * after taking the preimage (over the same function).
6754 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6755 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6764 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6765 isl_pw_aff_get_space(pa
));
6766 res
= isl_pw_aff_empty(space
);
6768 for (i
= 0; i
< pa
->n
; ++i
) {
6772 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6773 isl_multi_pw_aff_copy(mpa
),
6774 isl_aff_copy(pa
->p
[i
].aff
));
6775 domain
= isl_set_copy(pa
->p
[i
].set
);
6776 domain
= isl_set_preimage_multi_pw_aff(domain
,
6777 isl_multi_pw_aff_copy(mpa
));
6778 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6779 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6782 isl_pw_aff_free(pa
);
6783 isl_multi_pw_aff_free(mpa
);
6786 isl_pw_aff_free(pa
);
6787 isl_multi_pw_aff_free(mpa
);
6791 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6792 * with the domain of "pa". The domain of the result is the same
6795 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6796 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6800 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6801 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6803 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6804 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6806 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6808 isl_pw_aff_free(pa
);
6809 isl_multi_pw_aff_free(mpa
);
6813 /* Compute the pullback of "pa" by the function represented by "mpa".
6814 * In other words, plug in "mpa" in "pa".
6815 * "pa" and "mpa" are assumed to have been aligned.
6817 * The pullback is computed by applying "pa" to "mpa".
6819 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6820 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6822 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6825 /* Compute the pullback of "pa" by the function represented by "mpa".
6826 * In other words, plug in "mpa" in "pa".
6828 * The pullback is computed by applying "pa" to "mpa".
6830 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6831 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6833 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6836 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6837 * In other words, plug in "mpa2" in "mpa1".
6839 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6841 * We pullback each member of "mpa1" in turn.
6843 static __isl_give isl_multi_pw_aff
*
6844 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6845 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6848 isl_space
*space
= NULL
;
6850 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6854 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6855 isl_multi_pw_aff_get_space(mpa1
));
6857 for (i
= 0; i
< mpa1
->n
; ++i
) {
6858 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6859 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6864 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6866 isl_multi_pw_aff_free(mpa2
);
6869 isl_space_free(space
);
6870 isl_multi_pw_aff_free(mpa1
);
6871 isl_multi_pw_aff_free(mpa2
);
6875 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6876 * In other words, plug in "mpa2" in "mpa1".
6878 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6879 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6881 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6882 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6885 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6886 * of "mpa1" and "mpa2" live in the same space, construct map space
6887 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6888 * with this map space as extract argument.
6890 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6891 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6892 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6893 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6896 isl_space
*space1
, *space2
;
6899 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6900 isl_multi_pw_aff_get_space(mpa2
));
6901 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6902 isl_multi_pw_aff_get_space(mpa1
));
6905 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6906 mpa2
->space
, isl_dim_out
);
6910 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6911 "range spaces don't match", goto error
);
6912 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6913 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6914 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6916 res
= order(mpa1
, mpa2
, space1
);
6917 isl_multi_pw_aff_free(mpa1
);
6918 isl_multi_pw_aff_free(mpa2
);
6921 isl_multi_pw_aff_free(mpa1
);
6922 isl_multi_pw_aff_free(mpa2
);
6926 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6927 * where the function values are equal. "space" is the space of the result.
6928 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6930 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6931 * in the sequences are equal.
6933 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6934 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6935 __isl_take isl_space
*space
)
6940 res
= isl_map_universe(space
);
6942 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6943 for (i
= 0; i
< n
; ++i
) {
6944 isl_pw_aff
*pa1
, *pa2
;
6947 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6948 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6949 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6950 res
= isl_map_intersect(res
, map
);
6956 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6957 * where the function values are equal.
6959 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6960 __isl_take isl_multi_pw_aff
*mpa2
)
6962 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6963 &isl_multi_pw_aff_eq_map_on_space
);
6966 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6967 * where the function values of "mpa1" is lexicographically satisfies "base"
6968 * compared to that of "mpa2". "space" is the space of the result.
6969 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6971 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6972 * if its i-th element satisfies "base" when compared to
6973 * the i-th element of "mpa2" while all previous elements are
6976 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6977 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6978 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6979 __isl_take isl_pw_aff
*pa2
),
6980 __isl_take isl_space
*space
)
6983 isl_map
*res
, *rest
;
6985 res
= isl_map_empty(isl_space_copy(space
));
6986 rest
= isl_map_universe(space
);
6988 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6989 for (i
= 0; i
< n
; ++i
) {
6990 isl_pw_aff
*pa1
, *pa2
;
6993 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6994 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6995 map
= base(pa1
, pa2
);
6996 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6997 res
= isl_map_union(res
, map
);
7002 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7003 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7004 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7005 rest
= isl_map_intersect(rest
, map
);
7012 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7013 * where the function value of "mpa1" is lexicographically less than that
7014 * of "mpa2". "space" is the space of the result.
7015 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7017 * "mpa1" is less than "mpa2" if its i-th element is smaller
7018 * than the i-th element of "mpa2" while all previous elements are
7021 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7022 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7023 __isl_take isl_space
*space
)
7025 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7026 &isl_pw_aff_lt_map
, space
);
7029 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7030 * where the function value of "mpa1" is lexicographically less than that
7033 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7034 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7036 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7037 &isl_multi_pw_aff_lex_lt_map_on_space
);
7040 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7041 * where the function value of "mpa1" is lexicographically greater than that
7042 * of "mpa2". "space" is the space of the result.
7043 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7045 * "mpa1" is greater than "mpa2" if its i-th element is greater
7046 * than the i-th element of "mpa2" while all previous elements are
7049 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7050 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7051 __isl_take isl_space
*space
)
7053 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7054 &isl_pw_aff_gt_map
, space
);
7057 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7058 * where the function value of "mpa1" is lexicographically greater than that
7061 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7062 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7064 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7065 &isl_multi_pw_aff_lex_gt_map_on_space
);
7068 /* Compare two isl_affs.
7070 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7071 * than "aff2" and 0 if they are equal.
7073 * The order is fairly arbitrary. We do consider expressions that only involve
7074 * earlier dimensions as "smaller".
7076 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7089 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7093 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7094 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7096 return last1
- last2
;
7098 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7101 /* Compare two isl_pw_affs.
7103 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7104 * than "pa2" and 0 if they are equal.
7106 * The order is fairly arbitrary. We do consider expressions that only involve
7107 * earlier dimensions as "smaller".
7109 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7110 __isl_keep isl_pw_aff
*pa2
)
7123 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7127 if (pa1
->n
!= pa2
->n
)
7128 return pa1
->n
- pa2
->n
;
7130 for (i
= 0; i
< pa1
->n
; ++i
) {
7131 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7134 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7142 /* Return a piecewise affine expression that is equal to "v" on "domain".
7144 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7145 __isl_take isl_val
*v
)
7148 isl_local_space
*ls
;
7151 space
= isl_set_get_space(domain
);
7152 ls
= isl_local_space_from_space(space
);
7153 aff
= isl_aff_val_on_domain(ls
, v
);
7155 return isl_pw_aff_alloc(domain
, aff
);
7158 /* Return a multi affine expression that is equal to "mv" on domain
7161 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7162 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7166 isl_local_space
*ls
;
7172 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7173 space2
= isl_multi_val_get_space(mv
);
7174 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7175 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7176 space
= isl_space_map_from_domain_and_range(space
, space2
);
7177 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7178 ls
= isl_local_space_from_space(isl_space_domain(space
));
7179 for (i
= 0; i
< n
; ++i
) {
7183 v
= isl_multi_val_get_val(mv
, i
);
7184 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7185 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7187 isl_local_space_free(ls
);
7189 isl_multi_val_free(mv
);
7192 isl_space_free(space
);
7193 isl_multi_val_free(mv
);
7197 /* Return a piecewise multi-affine expression
7198 * that is equal to "mv" on "domain".
7200 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7201 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7206 space
= isl_set_get_space(domain
);
7207 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7209 return isl_pw_multi_aff_alloc(domain
, ma
);
7212 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7213 * mv is the value that should be attained on each domain set
7214 * res collects the results
7216 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7218 isl_union_pw_multi_aff
*res
;
7221 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7222 * and add it to data->res.
7224 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7227 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7228 isl_pw_multi_aff
*pma
;
7231 mv
= isl_multi_val_copy(data
->mv
);
7232 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7233 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7235 return data
->res
? isl_stat_ok
: isl_stat_error
;
7238 /* Return a union piecewise multi-affine expression
7239 * that is equal to "mv" on "domain".
7241 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7242 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7244 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7247 space
= isl_union_set_get_space(domain
);
7248 data
.res
= isl_union_pw_multi_aff_empty(space
);
7250 if (isl_union_set_foreach_set(domain
,
7251 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7252 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7253 isl_union_set_free(domain
);
7254 isl_multi_val_free(mv
);
7258 /* Compute the pullback of data->pma by the function represented by "pma2",
7259 * provided the spaces match, and add the results to data->res.
7261 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7263 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7265 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7266 pma2
->dim
, isl_dim_out
)) {
7267 isl_pw_multi_aff_free(pma2
);
7271 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7272 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7274 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7276 return isl_stat_error
;
7281 /* Compute the pullback of "upma1" by the function represented by "upma2".
7283 __isl_give isl_union_pw_multi_aff
*
7284 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7285 __isl_take isl_union_pw_multi_aff
*upma1
,
7286 __isl_take isl_union_pw_multi_aff
*upma2
)
7288 return bin_op(upma1
, upma2
, &pullback_entry
);
7291 /* Check that the domain space of "upa" matches "space".
7293 * Return 0 on success and -1 on error.
7295 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7296 * can in principle never fail since the space "space" is that
7297 * of the isl_multi_union_pw_aff and is a set space such that
7298 * there is no domain space to match.
7300 * We check the parameters and double-check that "space" is
7301 * indeed that of a set.
7303 static int isl_union_pw_aff_check_match_domain_space(
7304 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7306 isl_space
*upa_space
;
7312 match
= isl_space_is_set(space
);
7316 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7317 "expecting set space", return -1);
7319 upa_space
= isl_union_pw_aff_get_space(upa
);
7320 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7324 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7325 "parameters don't match", goto error
);
7327 isl_space_free(upa_space
);
7330 isl_space_free(upa_space
);
7334 /* Do the parameters of "upa" match those of "space"?
7336 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7337 __isl_keep isl_space
*space
)
7339 isl_space
*upa_space
;
7345 upa_space
= isl_union_pw_aff_get_space(upa
);
7347 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7349 isl_space_free(upa_space
);
7353 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7354 * space represents the new parameters.
7355 * res collects the results.
7357 struct isl_union_pw_aff_reset_params_data
{
7359 isl_union_pw_aff
*res
;
7362 /* Replace the parameters of "pa" by data->space and
7363 * add the result to data->res.
7365 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7367 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7370 space
= isl_pw_aff_get_space(pa
);
7371 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7372 pa
= isl_pw_aff_reset_space(pa
, space
);
7373 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7375 return data
->res
? isl_stat_ok
: isl_stat_error
;
7378 /* Replace the domain space of "upa" by "space".
7379 * Since a union expression does not have a (single) domain space,
7380 * "space" is necessarily a parameter space.
7382 * Since the order and the names of the parameters determine
7383 * the hash value, we need to create a new hash table.
7385 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7386 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7388 struct isl_union_pw_aff_reset_params_data data
= { space
};
7391 match
= isl_union_pw_aff_matching_params(upa
, space
);
7393 upa
= isl_union_pw_aff_free(upa
);
7395 isl_space_free(space
);
7399 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7400 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7401 data
.res
= isl_union_pw_aff_free(data
.res
);
7403 isl_union_pw_aff_free(upa
);
7404 isl_space_free(space
);
7408 /* Return the floor of "pa".
7410 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7412 return isl_pw_aff_floor(pa
);
7415 /* Given f, return floor(f).
7417 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7418 __isl_take isl_union_pw_aff
*upa
)
7420 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7425 * upa mod m = upa - m * floor(upa/m)
7427 * with m an integer value.
7429 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7430 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7432 isl_union_pw_aff
*res
;
7437 if (!isl_val_is_int(m
))
7438 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7439 "expecting integer modulo", goto error
);
7440 if (!isl_val_is_pos(m
))
7441 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7442 "expecting positive modulo", goto error
);
7444 res
= isl_union_pw_aff_copy(upa
);
7445 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7446 upa
= isl_union_pw_aff_floor(upa
);
7447 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7448 res
= isl_union_pw_aff_sub(res
, upa
);
7453 isl_union_pw_aff_free(upa
);
7457 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7458 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7460 * "res" collects the results.
7462 struct isl_union_pw_aff_aff_on_domain_data
{
7464 isl_union_pw_aff
*res
;
7467 /* Construct a piecewise affine expression that is equal to data->aff
7468 * on "domain" and add the result to data->res.
7470 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7472 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7477 aff
= isl_aff_copy(data
->aff
);
7478 dim
= isl_set_dim(domain
, isl_dim_set
);
7479 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7480 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7481 pa
= isl_pw_aff_alloc(domain
, aff
);
7482 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7484 return data
->res
? isl_stat_ok
: isl_stat_error
;
7487 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7488 * pos is the output position that needs to be extracted.
7489 * res collects the results.
7491 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7493 isl_union_pw_aff
*res
;
7496 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7497 * (assuming it has such a dimension) and add it to data->res.
7499 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7501 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7506 return isl_stat_error
;
7508 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7509 if (data
->pos
>= n_out
) {
7510 isl_pw_multi_aff_free(pma
);
7514 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7515 isl_pw_multi_aff_free(pma
);
7517 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7519 return data
->res
? isl_stat_ok
: isl_stat_error
;
7522 /* Extract an isl_union_pw_aff corresponding to
7523 * output dimension "pos" of "upma".
7525 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7526 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7528 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7535 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7536 "cannot extract at negative position", return NULL
);
7538 space
= isl_union_pw_multi_aff_get_space(upma
);
7539 data
.res
= isl_union_pw_aff_empty(space
);
7541 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7542 &get_union_pw_aff
, &data
) < 0)
7543 data
.res
= isl_union_pw_aff_free(data
.res
);
7548 /* Return a union piecewise affine expression
7549 * that is equal to "aff" on "domain".
7551 * Construct an isl_pw_aff on each of the sets in "domain" and
7552 * collect the results.
7554 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7555 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7557 struct isl_union_pw_aff_aff_on_domain_data data
;
7560 if (!domain
|| !aff
)
7562 if (!isl_local_space_is_params(aff
->ls
))
7563 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7564 "expecting parametric expression", goto error
);
7566 space
= isl_union_set_get_space(domain
);
7567 data
.res
= isl_union_pw_aff_empty(space
);
7569 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7570 data
.res
= isl_union_pw_aff_free(data
.res
);
7571 isl_union_set_free(domain
);
7575 isl_union_set_free(domain
);
7580 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7581 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7582 * "res" collects the results.
7584 struct isl_union_pw_aff_val_on_domain_data
{
7586 isl_union_pw_aff
*res
;
7589 /* Construct a piecewise affine expression that is equal to data->v
7590 * on "domain" and add the result to data->res.
7592 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7594 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7598 v
= isl_val_copy(data
->v
);
7599 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7600 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7602 return data
->res
? isl_stat_ok
: isl_stat_error
;
7605 /* Return a union piecewise affine expression
7606 * that is equal to "v" on "domain".
7608 * Construct an isl_pw_aff on each of the sets in "domain" and
7609 * collect the results.
7611 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7612 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7614 struct isl_union_pw_aff_val_on_domain_data data
;
7617 space
= isl_union_set_get_space(domain
);
7618 data
.res
= isl_union_pw_aff_empty(space
);
7620 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7621 data
.res
= isl_union_pw_aff_free(data
.res
);
7622 isl_union_set_free(domain
);
7627 /* Construct a piecewise multi affine expression
7628 * that is equal to "pa" and add it to upma.
7630 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7633 isl_union_pw_multi_aff
**upma
= user
;
7634 isl_pw_multi_aff
*pma
;
7636 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7637 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7639 return *upma
? isl_stat_ok
: isl_stat_error
;
7642 /* Construct and return a union piecewise multi affine expression
7643 * that is equal to the given union piecewise affine expression.
7645 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7646 __isl_take isl_union_pw_aff
*upa
)
7649 isl_union_pw_multi_aff
*upma
;
7654 space
= isl_union_pw_aff_get_space(upa
);
7655 upma
= isl_union_pw_multi_aff_empty(space
);
7657 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7658 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7659 upma
= isl_union_pw_multi_aff_free(upma
);
7661 isl_union_pw_aff_free(upa
);
7665 /* Compute the set of elements in the domain of "pa" where it is zero and
7666 * add this set to "uset".
7668 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7670 isl_union_set
**uset
= (isl_union_set
**)user
;
7672 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7674 return *uset
? isl_stat_ok
: isl_stat_error
;
7677 /* Return a union set containing those elements in the domain
7678 * of "upa" where it is zero.
7680 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7681 __isl_take isl_union_pw_aff
*upa
)
7683 isl_union_set
*zero
;
7685 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7686 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7687 zero
= isl_union_set_free(zero
);
7689 isl_union_pw_aff_free(upa
);
7693 /* Convert "pa" to an isl_map and add it to *umap.
7695 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7697 isl_union_map
**umap
= user
;
7700 map
= isl_map_from_pw_aff(pa
);
7701 *umap
= isl_union_map_add_map(*umap
, map
);
7703 return *umap
? isl_stat_ok
: isl_stat_error
;
7706 /* Construct a union map mapping the domain of the union
7707 * piecewise affine expression to its range, with the single output dimension
7708 * equated to the corresponding affine expressions on their cells.
7710 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7711 __isl_take isl_union_pw_aff
*upa
)
7714 isl_union_map
*umap
;
7719 space
= isl_union_pw_aff_get_space(upa
);
7720 umap
= isl_union_map_empty(space
);
7722 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7724 umap
= isl_union_map_free(umap
);
7726 isl_union_pw_aff_free(upa
);
7730 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7731 * upma is the function that is plugged in.
7732 * pa is the current part of the function in which upma is plugged in.
7733 * res collects the results.
7735 struct isl_union_pw_aff_pullback_upma_data
{
7736 isl_union_pw_multi_aff
*upma
;
7738 isl_union_pw_aff
*res
;
7741 /* Check if "pma" can be plugged into data->pa.
7742 * If so, perform the pullback and add the result to data->res.
7744 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7746 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7749 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7750 pma
->dim
, isl_dim_out
)) {
7751 isl_pw_multi_aff_free(pma
);
7755 pa
= isl_pw_aff_copy(data
->pa
);
7756 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7758 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7760 return data
->res
? isl_stat_ok
: isl_stat_error
;
7763 /* Check if any of the elements of data->upma can be plugged into pa,
7764 * add if so add the result to data->res.
7766 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7768 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7772 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7774 isl_pw_aff_free(pa
);
7779 /* Compute the pullback of "upa" by the function represented by "upma".
7780 * In other words, plug in "upma" in "upa". The result contains
7781 * expressions defined over the domain space of "upma".
7783 * Run over all pairs of elements in "upa" and "upma", perform
7784 * the pullback when appropriate and collect the results.
7785 * If the hash value were based on the domain space rather than
7786 * the function space, then we could run through all elements
7787 * of "upma" and directly pick out the corresponding element of "upa".
7789 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7790 __isl_take isl_union_pw_aff
*upa
,
7791 __isl_take isl_union_pw_multi_aff
*upma
)
7793 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7796 space
= isl_union_pw_multi_aff_get_space(upma
);
7797 upa
= isl_union_pw_aff_align_params(upa
, space
);
7798 space
= isl_union_pw_aff_get_space(upa
);
7799 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7805 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7806 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7807 data
.res
= isl_union_pw_aff_free(data
.res
);
7809 isl_union_pw_aff_free(upa
);
7810 isl_union_pw_multi_aff_free(upma
);
7813 isl_union_pw_aff_free(upa
);
7814 isl_union_pw_multi_aff_free(upma
);
7819 #define BASE union_pw_aff
7821 #define DOMBASE union_set
7823 #define NO_MOVE_DIMS
7832 #include <isl_multi_templ.c>
7833 #include <isl_multi_apply_set.c>
7834 #include <isl_multi_apply_union_set.c>
7835 #include <isl_multi_coalesce.c>
7836 #include <isl_multi_floor.c>
7837 #include <isl_multi_gist.c>
7838 #include <isl_multi_intersect.c>
7840 /* Construct a multiple union piecewise affine expression
7841 * in the given space with value zero in each of the output dimensions.
7843 * Since there is no canonical zero value for
7844 * a union piecewise affine expression, we can only construct
7845 * zero-dimensional "zero" value.
7847 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7848 __isl_take isl_space
*space
)
7853 if (!isl_space_is_set(space
))
7854 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7855 "expecting set space", goto error
);
7856 if (isl_space_dim(space
, isl_dim_out
) != 0)
7857 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7858 "expecting 0D space", goto error
);
7860 return isl_multi_union_pw_aff_alloc(space
);
7862 isl_space_free(space
);
7866 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7867 * with the actual sum on the shared domain and
7868 * the defined expression on the symmetric difference of the domains.
7870 * We simply iterate over the elements in both arguments and
7871 * call isl_union_pw_aff_union_add on each of them.
7873 static __isl_give isl_multi_union_pw_aff
*
7874 isl_multi_union_pw_aff_union_add_aligned(
7875 __isl_take isl_multi_union_pw_aff
*mupa1
,
7876 __isl_take isl_multi_union_pw_aff
*mupa2
)
7878 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7879 &isl_union_pw_aff_union_add
);
7882 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7883 * with the actual sum on the shared domain and
7884 * the defined expression on the symmetric difference of the domains.
7886 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7887 __isl_take isl_multi_union_pw_aff
*mupa1
,
7888 __isl_take isl_multi_union_pw_aff
*mupa2
)
7890 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7891 &isl_multi_union_pw_aff_union_add_aligned
);
7894 /* Construct and return a multi union piecewise affine expression
7895 * that is equal to the given multi affine expression.
7897 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7898 __isl_take isl_multi_aff
*ma
)
7900 isl_multi_pw_aff
*mpa
;
7902 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7903 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7906 /* Construct and return a multi union piecewise affine expression
7907 * that is equal to the given multi piecewise affine expression.
7909 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7910 __isl_take isl_multi_pw_aff
*mpa
)
7914 isl_multi_union_pw_aff
*mupa
;
7919 space
= isl_multi_pw_aff_get_space(mpa
);
7920 space
= isl_space_range(space
);
7921 mupa
= isl_multi_union_pw_aff_alloc(space
);
7923 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7924 for (i
= 0; i
< n
; ++i
) {
7926 isl_union_pw_aff
*upa
;
7928 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7929 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7930 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7933 isl_multi_pw_aff_free(mpa
);
7938 /* Extract the range space of "pma" and assign it to *space.
7939 * If *space has already been set (through a previous call to this function),
7940 * then check that the range space is the same.
7942 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7944 isl_space
**space
= user
;
7945 isl_space
*pma_space
;
7948 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7949 isl_pw_multi_aff_free(pma
);
7952 return isl_stat_error
;
7958 equal
= isl_space_is_equal(pma_space
, *space
);
7959 isl_space_free(pma_space
);
7962 return isl_stat_error
;
7964 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7965 "range spaces not the same", return isl_stat_error
);
7969 /* Construct and return a multi union piecewise affine expression
7970 * that is equal to the given union piecewise multi affine expression.
7972 * In order to be able to perform the conversion, the input
7973 * needs to be non-empty and may only involve a single range space.
7975 __isl_give isl_multi_union_pw_aff
*
7976 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7977 __isl_take isl_union_pw_multi_aff
*upma
)
7979 isl_space
*space
= NULL
;
7980 isl_multi_union_pw_aff
*mupa
;
7985 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7986 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7987 "cannot extract range space from empty input",
7989 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7996 n
= isl_space_dim(space
, isl_dim_set
);
7997 mupa
= isl_multi_union_pw_aff_alloc(space
);
7999 for (i
= 0; i
< n
; ++i
) {
8000 isl_union_pw_aff
*upa
;
8002 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8003 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8006 isl_union_pw_multi_aff_free(upma
);
8009 isl_space_free(space
);
8010 isl_union_pw_multi_aff_free(upma
);
8014 /* Try and create an isl_multi_union_pw_aff that is equivalent
8015 * to the given isl_union_map.
8016 * The isl_union_map is required to be single-valued in each space.
8017 * Moreover, it cannot be empty and all range spaces need to be the same.
8018 * Otherwise, an error is produced.
8020 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8021 __isl_take isl_union_map
*umap
)
8023 isl_union_pw_multi_aff
*upma
;
8025 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8026 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8029 /* Return a multiple union piecewise affine expression
8030 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8031 * have been aligned.
8033 static __isl_give isl_multi_union_pw_aff
*
8034 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8035 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8039 isl_multi_union_pw_aff
*mupa
;
8044 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8045 space
= isl_multi_val_get_space(mv
);
8046 mupa
= isl_multi_union_pw_aff_alloc(space
);
8047 for (i
= 0; i
< n
; ++i
) {
8049 isl_union_pw_aff
*upa
;
8051 v
= isl_multi_val_get_val(mv
, i
);
8052 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8054 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8057 isl_union_set_free(domain
);
8058 isl_multi_val_free(mv
);
8061 isl_union_set_free(domain
);
8062 isl_multi_val_free(mv
);
8066 /* Return a multiple union piecewise affine expression
8067 * that is equal to "mv" on "domain".
8069 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8070 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8074 if (isl_space_match(domain
->dim
, isl_dim_param
,
8075 mv
->space
, isl_dim_param
))
8076 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8078 domain
= isl_union_set_align_params(domain
,
8079 isl_multi_val_get_space(mv
));
8080 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8081 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8083 isl_union_set_free(domain
);
8084 isl_multi_val_free(mv
);
8088 /* Return a multiple union piecewise affine expression
8089 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8090 * have been aligned.
8092 static __isl_give isl_multi_union_pw_aff
*
8093 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8094 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8098 isl_multi_union_pw_aff
*mupa
;
8103 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8104 space
= isl_multi_aff_get_space(ma
);
8105 mupa
= isl_multi_union_pw_aff_alloc(space
);
8106 for (i
= 0; i
< n
; ++i
) {
8108 isl_union_pw_aff
*upa
;
8110 aff
= isl_multi_aff_get_aff(ma
, i
);
8111 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8113 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8116 isl_union_set_free(domain
);
8117 isl_multi_aff_free(ma
);
8120 isl_union_set_free(domain
);
8121 isl_multi_aff_free(ma
);
8125 /* Return a multiple union piecewise affine expression
8126 * that is equal to "ma" on "domain".
8128 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8129 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8133 if (isl_space_match(domain
->dim
, isl_dim_param
,
8134 ma
->space
, isl_dim_param
))
8135 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8137 domain
= isl_union_set_align_params(domain
,
8138 isl_multi_aff_get_space(ma
));
8139 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8140 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8142 isl_union_set_free(domain
);
8143 isl_multi_aff_free(ma
);
8147 /* Return a union set containing those elements in the domains
8148 * of the elements of "mupa" where they are all zero.
8150 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8151 __isl_take isl_multi_union_pw_aff
*mupa
)
8154 isl_union_pw_aff
*upa
;
8155 isl_union_set
*zero
;
8160 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8162 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8163 "cannot determine zero set "
8164 "of zero-dimensional function", goto error
);
8166 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8167 zero
= isl_union_pw_aff_zero_union_set(upa
);
8169 for (i
= 1; i
< n
; ++i
) {
8170 isl_union_set
*zero_i
;
8172 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8173 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8175 zero
= isl_union_set_intersect(zero
, zero_i
);
8178 isl_multi_union_pw_aff_free(mupa
);
8181 isl_multi_union_pw_aff_free(mupa
);
8185 /* Construct a union map mapping the shared domain
8186 * of the union piecewise affine expressions to the range of "mupa"
8187 * with each dimension in the range equated to the
8188 * corresponding union piecewise affine expression.
8190 * The input cannot be zero-dimensional as there is
8191 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8193 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8194 __isl_take isl_multi_union_pw_aff
*mupa
)
8198 isl_union_map
*umap
;
8199 isl_union_pw_aff
*upa
;
8204 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8206 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8207 "cannot determine domain of zero-dimensional "
8208 "isl_multi_union_pw_aff", goto error
);
8210 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8211 umap
= isl_union_map_from_union_pw_aff(upa
);
8213 for (i
= 1; i
< n
; ++i
) {
8214 isl_union_map
*umap_i
;
8216 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8217 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8218 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8221 space
= isl_multi_union_pw_aff_get_space(mupa
);
8222 umap
= isl_union_map_reset_range_space(umap
, space
);
8224 isl_multi_union_pw_aff_free(mupa
);
8227 isl_multi_union_pw_aff_free(mupa
);
8231 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8232 * "range" is the space from which to set the range space.
8233 * "res" collects the results.
8235 struct isl_union_pw_multi_aff_reset_range_space_data
{
8237 isl_union_pw_multi_aff
*res
;
8240 /* Replace the range space of "pma" by the range space of data->range and
8241 * add the result to data->res.
8243 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8245 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8248 space
= isl_pw_multi_aff_get_space(pma
);
8249 space
= isl_space_domain(space
);
8250 space
= isl_space_extend_domain_with_range(space
,
8251 isl_space_copy(data
->range
));
8252 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8253 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8255 return data
->res
? isl_stat_ok
: isl_stat_error
;
8258 /* Replace the range space of all the piecewise affine expressions in "upma" by
8259 * the range space of "space".
8261 * This assumes that all these expressions have the same output dimension.
8263 * Since the spaces of the expressions change, so do their hash values.
8264 * We therefore need to create a new isl_union_pw_multi_aff.
8265 * Note that the hash value is currently computed based on the entire
8266 * space even though there can only be a single expression with a given
8269 static __isl_give isl_union_pw_multi_aff
*
8270 isl_union_pw_multi_aff_reset_range_space(
8271 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8273 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8274 isl_space
*space_upma
;
8276 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8277 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8278 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8279 &reset_range_space
, &data
) < 0)
8280 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8282 isl_space_free(space
);
8283 isl_union_pw_multi_aff_free(upma
);
8287 /* Construct and return a union piecewise multi affine expression
8288 * that is equal to the given multi union piecewise affine expression.
8290 * In order to be able to perform the conversion, the input
8291 * needs to have a least one output dimension.
8293 __isl_give isl_union_pw_multi_aff
*
8294 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8295 __isl_take isl_multi_union_pw_aff
*mupa
)
8299 isl_union_pw_multi_aff
*upma
;
8300 isl_union_pw_aff
*upa
;
8305 space
= isl_multi_union_pw_aff_get_space(mupa
);
8307 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8309 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8310 "cannot determine domain of zero-dimensional "
8311 "isl_multi_union_pw_aff", goto error
);
8313 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8314 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8316 for (i
= 1; i
< n
; ++i
) {
8317 isl_union_pw_multi_aff
*upma_i
;
8319 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8320 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8321 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8324 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8326 isl_multi_union_pw_aff_free(mupa
);
8329 isl_multi_union_pw_aff_free(mupa
);
8333 /* Intersect the range of "mupa" with "range".
8334 * That is, keep only those domain elements that have a function value
8337 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8338 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8340 isl_union_pw_multi_aff
*upma
;
8341 isl_union_set
*domain
;
8346 if (!mupa
|| !range
)
8349 space
= isl_set_get_space(range
);
8350 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8351 space
, isl_dim_set
);
8352 isl_space_free(space
);
8356 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8357 "space don't match", goto error
);
8358 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8360 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8361 "cannot intersect range of zero-dimensional "
8362 "isl_multi_union_pw_aff", goto error
);
8364 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8365 isl_multi_union_pw_aff_copy(mupa
));
8366 domain
= isl_union_set_from_set(range
);
8367 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8368 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8372 isl_multi_union_pw_aff_free(mupa
);
8373 isl_set_free(range
);
8377 /* Return the shared domain of the elements of "mupa".
8379 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8380 __isl_take isl_multi_union_pw_aff
*mupa
)
8383 isl_union_pw_aff
*upa
;
8389 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8391 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8392 "cannot determine domain", goto error
);
8394 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8395 dom
= isl_union_pw_aff_domain(upa
);
8396 for (i
= 1; i
< n
; ++i
) {
8397 isl_union_set
*dom_i
;
8399 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8400 dom_i
= isl_union_pw_aff_domain(upa
);
8401 dom
= isl_union_set_intersect(dom
, dom_i
);
8404 isl_multi_union_pw_aff_free(mupa
);
8407 isl_multi_union_pw_aff_free(mupa
);
8411 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8412 * In particular, the spaces have been aligned.
8413 * The result is defined over the shared domain of the elements of "mupa"
8415 * We first extract the parametric constant part of "aff" and
8416 * define that over the shared domain.
8417 * Then we iterate over all input dimensions of "aff" and add the corresponding
8418 * multiples of the elements of "mupa".
8419 * Finally, we consider the integer divisions, calling the function
8420 * recursively to obtain an isl_union_pw_aff corresponding to the
8421 * integer division argument.
8423 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8424 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8427 isl_union_pw_aff
*upa
;
8428 isl_union_set
*uset
;
8432 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8433 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8435 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8436 cst
= isl_aff_copy(aff
);
8437 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8438 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8439 cst
= isl_aff_project_domain_on_params(cst
);
8440 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8442 for (i
= 0; i
< n_in
; ++i
) {
8443 isl_union_pw_aff
*upa_i
;
8445 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8447 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8448 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8449 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8450 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8453 for (i
= 0; i
< n_div
; ++i
) {
8455 isl_union_pw_aff
*upa_i
;
8457 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8459 div
= isl_aff_get_div(aff
, i
);
8460 upa_i
= multi_union_pw_aff_apply_aff(
8461 isl_multi_union_pw_aff_copy(mupa
), div
);
8462 upa_i
= isl_union_pw_aff_floor(upa_i
);
8463 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8464 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8465 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8468 isl_multi_union_pw_aff_free(mupa
);
8474 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8475 * with the domain of "aff".
8476 * Furthermore, the dimension of this space needs to be greater than zero.
8477 * The result is defined over the shared domain of the elements of "mupa"
8479 * We perform these checks and then hand over control to
8480 * multi_union_pw_aff_apply_aff.
8482 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8483 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8485 isl_space
*space1
, *space2
;
8488 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8489 isl_aff_get_space(aff
));
8490 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8494 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8495 space2
= isl_aff_get_domain_space(aff
);
8496 equal
= isl_space_is_equal(space1
, space2
);
8497 isl_space_free(space1
);
8498 isl_space_free(space2
);
8502 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8503 "spaces don't match", goto error
);
8504 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8505 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8506 "cannot determine domains", goto error
);
8508 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8510 isl_multi_union_pw_aff_free(mupa
);
8515 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8516 * with the domain of "ma".
8517 * Furthermore, the dimension of this space needs to be greater than zero,
8518 * unless the dimension of the target space of "ma" is also zero.
8519 * The result is defined over the shared domain of the elements of "mupa"
8521 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8522 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8524 isl_space
*space1
, *space2
;
8525 isl_multi_union_pw_aff
*res
;
8529 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8530 isl_multi_aff_get_space(ma
));
8531 ma
= isl_multi_aff_align_params(ma
,
8532 isl_multi_union_pw_aff_get_space(mupa
));
8536 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8537 space2
= isl_multi_aff_get_domain_space(ma
);
8538 equal
= isl_space_is_equal(space1
, space2
);
8539 isl_space_free(space1
);
8540 isl_space_free(space2
);
8544 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8545 "spaces don't match", goto error
);
8546 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8547 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8548 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8549 "cannot determine domains", goto error
);
8551 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8552 res
= isl_multi_union_pw_aff_alloc(space1
);
8554 for (i
= 0; i
< n_out
; ++i
) {
8556 isl_union_pw_aff
*upa
;
8558 aff
= isl_multi_aff_get_aff(ma
, i
);
8559 upa
= multi_union_pw_aff_apply_aff(
8560 isl_multi_union_pw_aff_copy(mupa
), aff
);
8561 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8564 isl_multi_aff_free(ma
);
8565 isl_multi_union_pw_aff_free(mupa
);
8568 isl_multi_union_pw_aff_free(mupa
);
8569 isl_multi_aff_free(ma
);
8573 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8574 * with the domain of "pa".
8575 * Furthermore, the dimension of this space needs to be greater than zero.
8576 * The result is defined over the shared domain of the elements of "mupa"
8578 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8579 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8583 isl_space
*space
, *space2
;
8584 isl_union_pw_aff
*upa
;
8586 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8587 isl_pw_aff_get_space(pa
));
8588 pa
= isl_pw_aff_align_params(pa
,
8589 isl_multi_union_pw_aff_get_space(mupa
));
8593 space
= isl_multi_union_pw_aff_get_space(mupa
);
8594 space2
= isl_pw_aff_get_domain_space(pa
);
8595 equal
= isl_space_is_equal(space
, space2
);
8596 isl_space_free(space
);
8597 isl_space_free(space2
);
8601 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8602 "spaces don't match", goto error
);
8603 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8604 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8605 "cannot determine domains", goto error
);
8607 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8608 upa
= isl_union_pw_aff_empty(space
);
8610 for (i
= 0; i
< pa
->n
; ++i
) {
8613 isl_multi_union_pw_aff
*mupa_i
;
8614 isl_union_pw_aff
*upa_i
;
8616 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8617 domain
= isl_set_copy(pa
->p
[i
].set
);
8618 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8619 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8620 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8621 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8624 isl_multi_union_pw_aff_free(mupa
);
8625 isl_pw_aff_free(pa
);
8628 isl_multi_union_pw_aff_free(mupa
);
8629 isl_pw_aff_free(pa
);
8633 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8634 * with the domain of "pma".
8635 * Furthermore, the dimension of this space needs to be greater than zero,
8636 * unless the dimension of the target space of "pma" is also zero.
8637 * The result is defined over the shared domain of the elements of "mupa"
8639 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8640 __isl_take isl_multi_union_pw_aff
*mupa
,
8641 __isl_take isl_pw_multi_aff
*pma
)
8643 isl_space
*space1
, *space2
;
8644 isl_multi_union_pw_aff
*res
;
8648 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8649 isl_pw_multi_aff_get_space(pma
));
8650 pma
= isl_pw_multi_aff_align_params(pma
,
8651 isl_multi_union_pw_aff_get_space(mupa
));
8655 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8656 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8657 equal
= isl_space_is_equal(space1
, space2
);
8658 isl_space_free(space1
);
8659 isl_space_free(space2
);
8663 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8664 "spaces don't match", goto error
);
8665 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8666 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8667 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8668 "cannot determine domains", goto error
);
8670 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8671 res
= isl_multi_union_pw_aff_alloc(space1
);
8673 for (i
= 0; i
< n_out
; ++i
) {
8675 isl_union_pw_aff
*upa
;
8677 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8678 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8679 isl_multi_union_pw_aff_copy(mupa
), pa
);
8680 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8683 isl_pw_multi_aff_free(pma
);
8684 isl_multi_union_pw_aff_free(mupa
);
8687 isl_multi_union_pw_aff_free(mupa
);
8688 isl_pw_multi_aff_free(pma
);
8692 /* Compute the pullback of "mupa" by the function represented by "upma".
8693 * In other words, plug in "upma" in "mupa". The result contains
8694 * expressions defined over the domain space of "upma".
8696 * Run over all elements of "mupa" and plug in "upma" in each of them.
8698 __isl_give isl_multi_union_pw_aff
*
8699 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8700 __isl_take isl_multi_union_pw_aff
*mupa
,
8701 __isl_take isl_union_pw_multi_aff
*upma
)
8705 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8706 isl_union_pw_multi_aff_get_space(upma
));
8707 upma
= isl_union_pw_multi_aff_align_params(upma
,
8708 isl_multi_union_pw_aff_get_space(mupa
));
8712 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8713 for (i
= 0; i
< n
; ++i
) {
8714 isl_union_pw_aff
*upa
;
8716 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8717 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8718 isl_union_pw_multi_aff_copy(upma
));
8719 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8722 isl_union_pw_multi_aff_free(upma
);
8725 isl_multi_union_pw_aff_free(mupa
);
8726 isl_union_pw_multi_aff_free(upma
);
8730 /* Extract the sequence of elements in "mupa" with domain space "space"
8731 * (ignoring parameters).
8733 * For the elements of "mupa" that are not defined on the specified space,
8734 * the corresponding element in the result is empty.
8736 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8737 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8740 isl_space
*space_mpa
= NULL
;
8741 isl_multi_pw_aff
*mpa
;
8743 if (!mupa
|| !space
)
8746 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8747 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8748 space
= isl_space_drop_dims(space
, isl_dim_param
,
8749 0, isl_space_dim(space
, isl_dim_param
));
8750 space
= isl_space_align_params(space
,
8751 isl_space_copy(space_mpa
));
8755 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8757 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8759 space
= isl_space_from_domain(space
);
8760 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8761 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8762 for (i
= 0; i
< n
; ++i
) {
8763 isl_union_pw_aff
*upa
;
8766 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8767 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8768 isl_space_copy(space
));
8769 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8770 isl_union_pw_aff_free(upa
);
8773 isl_space_free(space
);
8776 isl_space_free(space_mpa
);
8777 isl_space_free(space
);