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
2583 #include <isl_union_templ.c>
2585 static __isl_give isl_set
*align_params_pw_pw_set_and(
2586 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2587 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2588 __isl_take isl_pw_aff
*pwaff2
))
2590 if (!pwaff1
|| !pwaff2
)
2592 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2593 pwaff2
->dim
, isl_dim_param
))
2594 return fn(pwaff1
, pwaff2
);
2595 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2596 !isl_space_has_named_params(pwaff2
->dim
))
2597 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2598 "unaligned unnamed parameters", goto error
);
2599 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2600 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2601 return fn(pwaff1
, pwaff2
);
2603 isl_pw_aff_free(pwaff1
);
2604 isl_pw_aff_free(pwaff2
);
2608 /* Align the parameters of the to isl_pw_aff arguments and
2609 * then apply a function "fn" on them that returns an isl_map.
2611 static __isl_give isl_map
*align_params_pw_pw_map_and(
2612 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2613 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2614 __isl_take isl_pw_aff
*pa2
))
2618 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2619 return fn(pa1
, pa2
);
2620 if (!isl_space_has_named_params(pa1
->dim
) ||
2621 !isl_space_has_named_params(pa2
->dim
))
2622 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2623 "unaligned unnamed parameters", goto error
);
2624 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2625 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2626 return fn(pa1
, pa2
);
2628 isl_pw_aff_free(pa1
);
2629 isl_pw_aff_free(pa2
);
2633 /* Compute a piecewise quasi-affine expression with a domain that
2634 * is the union of those of pwaff1 and pwaff2 and such that on each
2635 * cell, the quasi-affine expression is the better (according to cmp)
2636 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2637 * is defined on a given cell, then the associated expression
2638 * is the defined one.
2640 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2641 __isl_take isl_pw_aff
*pwaff2
,
2642 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2643 __isl_take isl_aff
*aff2
))
2650 if (!pwaff1
|| !pwaff2
)
2653 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2654 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2655 isl_die(ctx
, isl_error_invalid
,
2656 "arguments should live in same space", goto error
);
2658 if (isl_pw_aff_is_empty(pwaff1
)) {
2659 isl_pw_aff_free(pwaff1
);
2663 if (isl_pw_aff_is_empty(pwaff2
)) {
2664 isl_pw_aff_free(pwaff2
);
2668 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2669 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2671 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2672 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2673 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2674 struct isl_set
*common
;
2677 common
= isl_set_intersect(
2678 isl_set_copy(pwaff1
->p
[i
].set
),
2679 isl_set_copy(pwaff2
->p
[j
].set
));
2680 better
= isl_set_from_basic_set(cmp(
2681 isl_aff_copy(pwaff2
->p
[j
].aff
),
2682 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2683 better
= isl_set_intersect(common
, better
);
2684 if (isl_set_plain_is_empty(better
)) {
2685 isl_set_free(better
);
2688 set
= isl_set_subtract(set
, isl_set_copy(better
));
2690 res
= isl_pw_aff_add_piece(res
, better
,
2691 isl_aff_copy(pwaff2
->p
[j
].aff
));
2693 res
= isl_pw_aff_add_piece(res
, set
,
2694 isl_aff_copy(pwaff1
->p
[i
].aff
));
2697 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2698 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2699 for (i
= 0; i
< pwaff1
->n
; ++i
)
2700 set
= isl_set_subtract(set
,
2701 isl_set_copy(pwaff1
->p
[i
].set
));
2702 res
= isl_pw_aff_add_piece(res
, set
,
2703 isl_aff_copy(pwaff2
->p
[j
].aff
));
2706 isl_pw_aff_free(pwaff1
);
2707 isl_pw_aff_free(pwaff2
);
2711 isl_pw_aff_free(pwaff1
);
2712 isl_pw_aff_free(pwaff2
);
2716 /* Compute a piecewise quasi-affine expression with a domain that
2717 * is the union of those of pwaff1 and pwaff2 and such that on each
2718 * cell, the quasi-affine expression is the maximum of those of pwaff1
2719 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2720 * cell, then the associated expression is the defined one.
2722 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2723 __isl_take isl_pw_aff
*pwaff2
)
2725 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2728 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2729 __isl_take isl_pw_aff
*pwaff2
)
2731 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2735 /* Compute a piecewise quasi-affine expression with a domain that
2736 * is the union of those of pwaff1 and pwaff2 and such that on each
2737 * cell, the quasi-affine expression is the minimum of those of pwaff1
2738 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2739 * cell, then the associated expression is the defined one.
2741 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2742 __isl_take isl_pw_aff
*pwaff2
)
2744 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2747 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2748 __isl_take isl_pw_aff
*pwaff2
)
2750 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2754 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2755 __isl_take isl_pw_aff
*pwaff2
, int max
)
2758 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2760 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2763 /* Construct a map with as domain the domain of pwaff and
2764 * one-dimensional range corresponding to the affine expressions.
2766 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2775 dim
= isl_pw_aff_get_space(pwaff
);
2776 map
= isl_map_empty(dim
);
2778 for (i
= 0; i
< pwaff
->n
; ++i
) {
2779 isl_basic_map
*bmap
;
2782 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2783 map_i
= isl_map_from_basic_map(bmap
);
2784 map_i
= isl_map_intersect_domain(map_i
,
2785 isl_set_copy(pwaff
->p
[i
].set
));
2786 map
= isl_map_union_disjoint(map
, map_i
);
2789 isl_pw_aff_free(pwaff
);
2794 /* Construct a map with as domain the domain of pwaff and
2795 * one-dimensional range corresponding to the affine expressions.
2797 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2801 if (isl_space_is_set(pwaff
->dim
))
2802 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2803 "space of input is not a map", goto error
);
2804 return map_from_pw_aff(pwaff
);
2806 isl_pw_aff_free(pwaff
);
2810 /* Construct a one-dimensional set with as parameter domain
2811 * the domain of pwaff and the single set dimension
2812 * corresponding to the affine expressions.
2814 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2818 if (!isl_space_is_set(pwaff
->dim
))
2819 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2820 "space of input is not a set", goto error
);
2821 return map_from_pw_aff(pwaff
);
2823 isl_pw_aff_free(pwaff
);
2827 /* Return a set containing those elements in the domain
2828 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2829 * does not satisfy "fn" (if complement is 1).
2831 * The pieces with a NaN never belong to the result since
2832 * NaN does not satisfy any property.
2834 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2835 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2844 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2846 for (i
= 0; i
< pwaff
->n
; ++i
) {
2847 isl_basic_set
*bset
;
2848 isl_set
*set_i
, *locus
;
2851 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2854 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2855 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2856 locus
= isl_set_from_basic_set(bset
);
2857 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2859 set_i
= isl_set_subtract(set_i
, locus
);
2861 set_i
= isl_set_intersect(set_i
, locus
);
2862 set
= isl_set_union_disjoint(set
, set_i
);
2865 isl_pw_aff_free(pwaff
);
2870 /* Return a set containing those elements in the domain
2871 * of "pa" where it is positive.
2873 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2875 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2878 /* Return a set containing those elements in the domain
2879 * of pwaff where it is non-negative.
2881 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2883 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2886 /* Return a set containing those elements in the domain
2887 * of pwaff where it is zero.
2889 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2891 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2894 /* Return a set containing those elements in the domain
2895 * of pwaff where it is not zero.
2897 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2899 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2902 /* Return a set containing those elements in the shared domain
2903 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2905 * We compute the difference on the shared domain and then construct
2906 * the set of values where this difference is non-negative.
2907 * If strict is set, we first subtract 1 from the difference.
2908 * If equal is set, we only return the elements where pwaff1 and pwaff2
2911 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2912 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2914 isl_set
*set1
, *set2
;
2916 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2917 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2918 set1
= isl_set_intersect(set1
, set2
);
2919 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2920 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2921 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2924 isl_space
*dim
= isl_set_get_space(set1
);
2926 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2927 aff
= isl_aff_add_constant_si(aff
, -1);
2928 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2933 return isl_pw_aff_zero_set(pwaff1
);
2934 return isl_pw_aff_nonneg_set(pwaff1
);
2937 /* Return a set containing those elements in the shared domain
2938 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2940 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2941 __isl_take isl_pw_aff
*pwaff2
)
2943 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2946 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2947 __isl_take isl_pw_aff
*pwaff2
)
2949 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2952 /* Return a set containing those elements in the shared domain
2953 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2955 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2956 __isl_take isl_pw_aff
*pwaff2
)
2958 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2961 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2962 __isl_take isl_pw_aff
*pwaff2
)
2964 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2967 /* Return a set containing those elements in the shared domain
2968 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2970 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2971 __isl_take isl_pw_aff
*pwaff2
)
2973 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2976 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2977 __isl_take isl_pw_aff
*pwaff2
)
2979 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2982 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2983 __isl_take isl_pw_aff
*pwaff2
)
2985 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2988 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2989 __isl_take isl_pw_aff
*pwaff2
)
2991 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2994 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2995 * where the function values are ordered in the same way as "order",
2996 * which returns a set in the shared domain of its two arguments.
2997 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2999 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3000 * We first pull back the two functions such that they are defined on
3001 * the domain [A -> B]. Then we apply "order", resulting in a set
3002 * in the space [A -> B]. Finally, we unwrap this set to obtain
3003 * a map in the space A -> B.
3005 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
3006 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3007 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3008 __isl_take isl_pw_aff
*pa2
))
3010 isl_space
*space1
, *space2
;
3014 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3015 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3016 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3017 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3018 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3019 ma
= isl_multi_aff_range_map(space1
);
3020 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3021 set
= order(pa1
, pa2
);
3023 return isl_set_unwrap(set
);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function values are equal.
3028 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3030 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3031 __isl_take isl_pw_aff
*pa2
)
3033 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function values are equal.
3039 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3040 __isl_take isl_pw_aff
*pa2
)
3042 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is less than the function value of "pa2".
3047 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3049 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3050 __isl_take isl_pw_aff
*pa2
)
3052 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3055 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3056 * where the function value of "pa1" is less than the function value of "pa2".
3058 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3059 __isl_take isl_pw_aff
*pa2
)
3061 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3064 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3065 * where the function value of "pa1" is greater than the function value
3067 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3069 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3070 __isl_take isl_pw_aff
*pa2
)
3072 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3075 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function value of "pa1" is greater than the function value
3079 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3080 __isl_take isl_pw_aff
*pa2
)
3082 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3085 /* Return a set containing those elements in the shared domain
3086 * of the elements of list1 and list2 where each element in list1
3087 * has the relation specified by "fn" with each element in list2.
3089 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3090 __isl_take isl_pw_aff_list
*list2
,
3091 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3092 __isl_take isl_pw_aff
*pwaff2
))
3098 if (!list1
|| !list2
)
3101 ctx
= isl_pw_aff_list_get_ctx(list1
);
3102 if (list1
->n
< 1 || list2
->n
< 1)
3103 isl_die(ctx
, isl_error_invalid
,
3104 "list should contain at least one element", goto error
);
3106 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3107 for (i
= 0; i
< list1
->n
; ++i
)
3108 for (j
= 0; j
< list2
->n
; ++j
) {
3111 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3112 isl_pw_aff_copy(list2
->p
[j
]));
3113 set
= isl_set_intersect(set
, set_ij
);
3116 isl_pw_aff_list_free(list1
);
3117 isl_pw_aff_list_free(list2
);
3120 isl_pw_aff_list_free(list1
);
3121 isl_pw_aff_list_free(list2
);
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is equal to each element in list2.
3129 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3130 __isl_take isl_pw_aff_list
*list2
)
3132 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3135 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3136 __isl_take isl_pw_aff_list
*list2
)
3138 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * is less than or equal to each element in list2.
3145 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3146 __isl_take isl_pw_aff_list
*list2
)
3148 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3151 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3152 __isl_take isl_pw_aff_list
*list2
)
3154 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3157 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3158 __isl_take isl_pw_aff_list
*list2
)
3160 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3163 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3164 __isl_take isl_pw_aff_list
*list2
)
3166 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3170 /* Return a set containing those elements in the shared domain
3171 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3173 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3174 __isl_take isl_pw_aff
*pwaff2
)
3176 isl_set
*set_lt
, *set_gt
;
3178 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3179 isl_pw_aff_copy(pwaff2
));
3180 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3181 return isl_set_union_disjoint(set_lt
, set_gt
);
3184 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3185 __isl_take isl_pw_aff
*pwaff2
)
3187 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3190 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3195 if (isl_int_is_one(v
))
3197 if (!isl_int_is_pos(v
))
3198 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3199 "factor needs to be positive",
3200 return isl_pw_aff_free(pwaff
));
3201 pwaff
= isl_pw_aff_cow(pwaff
);
3207 for (i
= 0; i
< pwaff
->n
; ++i
) {
3208 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3209 if (!pwaff
->p
[i
].aff
)
3210 return isl_pw_aff_free(pwaff
);
3216 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3220 pwaff
= isl_pw_aff_cow(pwaff
);
3226 for (i
= 0; i
< pwaff
->n
; ++i
) {
3227 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3228 if (!pwaff
->p
[i
].aff
)
3229 return isl_pw_aff_free(pwaff
);
3235 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3239 pwaff
= isl_pw_aff_cow(pwaff
);
3245 for (i
= 0; i
< pwaff
->n
; ++i
) {
3246 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3247 if (!pwaff
->p
[i
].aff
)
3248 return isl_pw_aff_free(pwaff
);
3254 /* Assuming that "cond1" and "cond2" are disjoint,
3255 * return an affine expression that is equal to pwaff1 on cond1
3256 * and to pwaff2 on cond2.
3258 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3259 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3260 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3262 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3263 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3265 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3268 /* Return an affine expression that is equal to pwaff_true for elements
3269 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3271 * That is, return cond ? pwaff_true : pwaff_false;
3273 * If "cond" involves and NaN, then we conservatively return a NaN
3274 * on its entire domain. In principle, we could consider the pieces
3275 * where it is NaN separately from those where it is not.
3277 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3278 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3280 isl_set
*cond_true
, *cond_false
;
3284 if (isl_pw_aff_involves_nan(cond
)) {
3285 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3286 isl_local_space
*ls
= isl_local_space_from_space(space
);
3287 isl_pw_aff_free(cond
);
3288 isl_pw_aff_free(pwaff_true
);
3289 isl_pw_aff_free(pwaff_false
);
3290 return isl_pw_aff_nan_on_domain(ls
);
3293 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3294 cond_false
= isl_pw_aff_zero_set(cond
);
3295 return isl_pw_aff_select(cond_true
, pwaff_true
,
3296 cond_false
, pwaff_false
);
3298 isl_pw_aff_free(cond
);
3299 isl_pw_aff_free(pwaff_true
);
3300 isl_pw_aff_free(pwaff_false
);
3304 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3307 return isl_bool_error
;
3309 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3312 /* Check whether pwaff is a piecewise constant.
3314 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3319 return isl_bool_error
;
3321 for (i
= 0; i
< pwaff
->n
; ++i
) {
3322 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3323 if (is_cst
< 0 || !is_cst
)
3327 return isl_bool_true
;
3330 /* Return the product of "aff1" and "aff2".
3332 * If either of the two is NaN, then the result is NaN.
3334 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3336 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3337 __isl_take isl_aff
*aff2
)
3342 if (isl_aff_is_nan(aff1
)) {
3346 if (isl_aff_is_nan(aff2
)) {
3351 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3352 return isl_aff_mul(aff2
, aff1
);
3354 if (!isl_aff_is_cst(aff2
))
3355 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3356 "at least one affine expression should be constant",
3359 aff1
= isl_aff_cow(aff1
);
3363 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3364 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3374 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3376 * If either of the two is NaN, then the result is NaN.
3378 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3379 __isl_take isl_aff
*aff2
)
3387 if (isl_aff_is_nan(aff1
)) {
3391 if (isl_aff_is_nan(aff2
)) {
3396 is_cst
= isl_aff_is_cst(aff2
);
3400 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3401 "second argument should be a constant", goto error
);
3406 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3408 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3409 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3412 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3413 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3416 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3417 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3428 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3429 __isl_take isl_pw_aff
*pwaff2
)
3431 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3434 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3435 __isl_take isl_pw_aff
*pwaff2
)
3437 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3440 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3441 __isl_take isl_pw_aff
*pwaff2
)
3443 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3446 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3447 __isl_take isl_pw_aff
*pwaff2
)
3449 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3452 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3453 __isl_take isl_pw_aff
*pwaff2
)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3458 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3459 __isl_take isl_pw_aff
*pa2
)
3461 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3464 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3466 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3467 __isl_take isl_pw_aff
*pa2
)
3471 is_cst
= isl_pw_aff_is_cst(pa2
);
3475 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3476 "second argument should be a piecewise constant",
3478 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3480 isl_pw_aff_free(pa1
);
3481 isl_pw_aff_free(pa2
);
3485 /* Compute the quotient of the integer division of "pa1" by "pa2"
3486 * with rounding towards zero.
3487 * "pa2" is assumed to be a piecewise constant.
3489 * In particular, return
3491 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3494 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3495 __isl_take isl_pw_aff
*pa2
)
3501 is_cst
= isl_pw_aff_is_cst(pa2
);
3505 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3506 "second argument should be a piecewise constant",
3509 pa1
= isl_pw_aff_div(pa1
, pa2
);
3511 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3512 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3513 c
= isl_pw_aff_ceil(pa1
);
3514 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3516 isl_pw_aff_free(pa1
);
3517 isl_pw_aff_free(pa2
);
3521 /* Compute the remainder of the integer division of "pa1" by "pa2"
3522 * with rounding towards zero.
3523 * "pa2" is assumed to be a piecewise constant.
3525 * In particular, return
3527 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3530 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3531 __isl_take isl_pw_aff
*pa2
)
3536 is_cst
= isl_pw_aff_is_cst(pa2
);
3540 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3541 "second argument should be a piecewise constant",
3543 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3544 res
= isl_pw_aff_mul(pa2
, res
);
3545 res
= isl_pw_aff_sub(pa1
, res
);
3548 isl_pw_aff_free(pa1
);
3549 isl_pw_aff_free(pa2
);
3553 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3554 __isl_take isl_pw_aff
*pwaff2
)
3559 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3560 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3561 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3562 isl_pw_aff_copy(pwaff2
));
3563 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3564 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3567 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3568 __isl_take isl_pw_aff
*pwaff2
)
3570 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3573 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3574 __isl_take isl_pw_aff
*pwaff2
)
3579 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3580 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3581 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3582 isl_pw_aff_copy(pwaff2
));
3583 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3584 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3587 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3588 __isl_take isl_pw_aff
*pwaff2
)
3590 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3593 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3594 __isl_take isl_pw_aff_list
*list
,
3595 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3596 __isl_take isl_pw_aff
*pwaff2
))
3605 ctx
= isl_pw_aff_list_get_ctx(list
);
3607 isl_die(ctx
, isl_error_invalid
,
3608 "list should contain at least one element", goto error
);
3610 res
= isl_pw_aff_copy(list
->p
[0]);
3611 for (i
= 1; i
< list
->n
; ++i
)
3612 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3614 isl_pw_aff_list_free(list
);
3617 isl_pw_aff_list_free(list
);
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the minimal corresponding affine
3625 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3627 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3630 /* Return an isl_pw_aff that maps each element in the intersection of the
3631 * domains of the elements of list to the maximal corresponding affine
3634 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3636 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3639 /* Mark the domains of "pwaff" as rational.
3641 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3645 pwaff
= isl_pw_aff_cow(pwaff
);
3651 for (i
= 0; i
< pwaff
->n
; ++i
) {
3652 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3653 if (!pwaff
->p
[i
].set
)
3654 return isl_pw_aff_free(pwaff
);
3660 /* Mark the domains of the elements of "list" as rational.
3662 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3663 __isl_take isl_pw_aff_list
*list
)
3673 for (i
= 0; i
< n
; ++i
) {
3676 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3677 pa
= isl_pw_aff_set_rational(pa
);
3678 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3684 /* Do the parameters of "aff" match those of "space"?
3686 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3687 __isl_keep isl_space
*space
)
3689 isl_space
*aff_space
;
3695 aff_space
= isl_aff_get_domain_space(aff
);
3697 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3699 isl_space_free(aff_space
);
3703 /* Check that the domain space of "aff" matches "space".
3705 * Return 0 on success and -1 on error.
3707 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3708 __isl_keep isl_space
*space
)
3710 isl_space
*aff_space
;
3716 aff_space
= isl_aff_get_domain_space(aff
);
3718 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3722 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3723 "parameters don't match", goto error
);
3724 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3725 aff_space
, isl_dim_set
);
3729 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3730 "domains don't match", goto error
);
3731 isl_space_free(aff_space
);
3734 isl_space_free(aff_space
);
3744 #include <isl_multi_templ.c>
3745 #include <isl_multi_apply_set.c>
3746 #include <isl_multi_floor.c>
3747 #include <isl_multi_gist.c>
3751 /* Remove any internal structure of the domain of "ma".
3752 * If there is any such internal structure in the input,
3753 * then the name of the corresponding space is also removed.
3755 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3756 __isl_take isl_multi_aff
*ma
)
3763 if (!ma
->space
->nested
[0])
3766 space
= isl_multi_aff_get_space(ma
);
3767 space
= isl_space_flatten_domain(space
);
3768 ma
= isl_multi_aff_reset_space(ma
, space
);
3773 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3774 * of the space to its domain.
3776 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3779 isl_local_space
*ls
;
3784 if (!isl_space_is_map(space
))
3785 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3786 "not a map space", goto error
);
3788 n_in
= isl_space_dim(space
, isl_dim_in
);
3789 space
= isl_space_domain_map(space
);
3791 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3793 isl_space_free(space
);
3797 space
= isl_space_domain(space
);
3798 ls
= isl_local_space_from_space(space
);
3799 for (i
= 0; i
< n_in
; ++i
) {
3802 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3804 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3806 isl_local_space_free(ls
);
3809 isl_space_free(space
);
3813 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3814 * of the space to its range.
3816 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3819 isl_local_space
*ls
;
3824 if (!isl_space_is_map(space
))
3825 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3826 "not a map space", goto error
);
3828 n_in
= isl_space_dim(space
, isl_dim_in
);
3829 n_out
= isl_space_dim(space
, isl_dim_out
);
3830 space
= isl_space_range_map(space
);
3832 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3834 isl_space_free(space
);
3838 space
= isl_space_domain(space
);
3839 ls
= isl_local_space_from_space(space
);
3840 for (i
= 0; i
< n_out
; ++i
) {
3843 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3844 isl_dim_set
, n_in
+ i
);
3845 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3847 isl_local_space_free(ls
);
3850 isl_space_free(space
);
3854 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3855 * of the space to its range.
3857 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3858 __isl_take isl_space
*space
)
3860 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3863 /* Given the space of a set and a range of set dimensions,
3864 * construct an isl_multi_aff that projects out those dimensions.
3866 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3867 __isl_take isl_space
*space
, enum isl_dim_type type
,
3868 unsigned first
, unsigned n
)
3871 isl_local_space
*ls
;
3876 if (!isl_space_is_set(space
))
3877 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3878 "expecting set space", goto error
);
3879 if (type
!= isl_dim_set
)
3880 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3881 "only set dimensions can be projected out", goto error
);
3883 dim
= isl_space_dim(space
, isl_dim_set
);
3884 if (first
+ n
> dim
)
3885 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3886 "range out of bounds", goto error
);
3888 space
= isl_space_from_domain(space
);
3889 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3892 return isl_multi_aff_alloc(space
);
3894 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3895 space
= isl_space_domain(space
);
3896 ls
= isl_local_space_from_space(space
);
3898 for (i
= 0; i
< first
; ++i
) {
3901 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3903 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3906 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3909 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3910 isl_dim_set
, first
+ n
+ i
);
3911 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3914 isl_local_space_free(ls
);
3917 isl_space_free(space
);
3921 /* Given the space of a set and a range of set dimensions,
3922 * construct an isl_pw_multi_aff that projects out those dimensions.
3924 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3925 __isl_take isl_space
*space
, enum isl_dim_type type
,
3926 unsigned first
, unsigned n
)
3930 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3931 return isl_pw_multi_aff_from_multi_aff(ma
);
3934 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3937 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3938 __isl_take isl_multi_aff
*ma
)
3940 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3941 return isl_pw_multi_aff_alloc(dom
, ma
);
3944 /* Create a piecewise multi-affine expression in the given space that maps each
3945 * input dimension to the corresponding output dimension.
3947 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3948 __isl_take isl_space
*space
)
3950 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3953 /* Add "ma2" to "ma1" and return the result.
3955 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3957 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3958 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3960 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3963 /* Add "ma2" to "ma1" and return the result.
3965 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3966 __isl_take isl_multi_aff
*ma2
)
3968 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3969 &isl_multi_aff_add_aligned
);
3972 /* Exploit the equalities in "eq" to simplify the affine expressions.
3974 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3975 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3979 maff
= isl_multi_aff_cow(maff
);
3983 for (i
= 0; i
< maff
->n
; ++i
) {
3984 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3985 isl_basic_set_copy(eq
));
3990 isl_basic_set_free(eq
);
3993 isl_basic_set_free(eq
);
3994 isl_multi_aff_free(maff
);
3998 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4003 maff
= isl_multi_aff_cow(maff
);
4007 for (i
= 0; i
< maff
->n
; ++i
) {
4008 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4010 return isl_multi_aff_free(maff
);
4016 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4017 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4019 maff1
= isl_multi_aff_add(maff1
, maff2
);
4020 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4024 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4032 /* Return the set of domain elements where "ma1" is lexicographically
4033 * smaller than or equal to "ma2".
4035 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4036 __isl_take isl_multi_aff
*ma2
)
4038 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4041 /* Return the set of domain elements where "ma1" is lexicographically
4042 * greater than or equal to "ma2".
4044 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4045 __isl_take isl_multi_aff
*ma2
)
4048 isl_map
*map1
, *map2
;
4051 map1
= isl_map_from_multi_aff(ma1
);
4052 map2
= isl_map_from_multi_aff(ma2
);
4053 map
= isl_map_range_product(map1
, map2
);
4054 space
= isl_space_range(isl_map_get_space(map
));
4055 space
= isl_space_domain(isl_space_unwrap(space
));
4056 ge
= isl_map_lex_ge(space
);
4057 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4059 return isl_map_domain(map
);
4063 #define PW isl_pw_multi_aff
4065 #define EL isl_multi_aff
4067 #define EL_IS_ZERO is_empty
4071 #define IS_ZERO is_empty
4074 #undef DEFAULT_IS_ZERO
4075 #define DEFAULT_IS_ZERO 0
4080 #define NO_INVOLVES_DIMS
4081 #define NO_INSERT_DIMS
4085 #include <isl_pw_templ.c>
4090 #define UNION isl_union_pw_multi_aff
4092 #define PART isl_pw_multi_aff
4094 #define PARTS pw_multi_aff
4098 #include <isl_union_templ.c>
4100 /* Given a function "cmp" that returns the set of elements where
4101 * "ma1" is "better" than "ma2", return the intersection of this
4102 * set with "dom1" and "dom2".
4104 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
4105 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
4106 __isl_keep isl_multi_aff
*ma2
,
4107 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4108 __isl_take isl_multi_aff
*ma2
))
4114 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
4115 is_empty
= isl_set_plain_is_empty(common
);
4116 if (is_empty
>= 0 && is_empty
)
4119 return isl_set_free(common
);
4120 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
4121 better
= isl_set_intersect(common
, better
);
4126 /* Given a function "cmp" that returns the set of elements where
4127 * "ma1" is "better" than "ma2", return a piecewise multi affine
4128 * expression defined on the union of the definition domains
4129 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4130 * "pma2" on each cell. If only one of the two input functions
4131 * is defined on a given cell, then it is considered the best.
4133 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
4134 __isl_take isl_pw_multi_aff
*pma1
,
4135 __isl_take isl_pw_multi_aff
*pma2
,
4136 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4137 __isl_take isl_multi_aff
*ma2
))
4140 isl_pw_multi_aff
*res
= NULL
;
4142 isl_set
*set
= NULL
;
4147 ctx
= isl_space_get_ctx(pma1
->dim
);
4148 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
4149 isl_die(ctx
, isl_error_invalid
,
4150 "arguments should live in the same space", goto error
);
4152 if (isl_pw_multi_aff_is_empty(pma1
)) {
4153 isl_pw_multi_aff_free(pma1
);
4157 if (isl_pw_multi_aff_is_empty(pma2
)) {
4158 isl_pw_multi_aff_free(pma2
);
4162 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4163 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4165 for (i
= 0; i
< pma1
->n
; ++i
) {
4166 set
= isl_set_copy(pma1
->p
[i
].set
);
4167 for (j
= 0; j
< pma2
->n
; ++j
) {
4171 better
= shared_and_better(pma2
->p
[j
].set
,
4172 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4173 pma1
->p
[i
].maff
, cmp
);
4174 is_empty
= isl_set_plain_is_empty(better
);
4175 if (is_empty
< 0 || is_empty
) {
4176 isl_set_free(better
);
4181 set
= isl_set_subtract(set
, isl_set_copy(better
));
4183 res
= isl_pw_multi_aff_add_piece(res
, better
,
4184 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4186 res
= isl_pw_multi_aff_add_piece(res
, set
,
4187 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4190 for (j
= 0; j
< pma2
->n
; ++j
) {
4191 set
= isl_set_copy(pma2
->p
[j
].set
);
4192 for (i
= 0; i
< pma1
->n
; ++i
)
4193 set
= isl_set_subtract(set
,
4194 isl_set_copy(pma1
->p
[i
].set
));
4195 res
= isl_pw_multi_aff_add_piece(res
, set
,
4196 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4199 isl_pw_multi_aff_free(pma1
);
4200 isl_pw_multi_aff_free(pma2
);
4204 isl_pw_multi_aff_free(pma1
);
4205 isl_pw_multi_aff_free(pma2
);
4207 return isl_pw_multi_aff_free(res
);
4210 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4211 __isl_take isl_pw_multi_aff
*pma1
,
4212 __isl_take isl_pw_multi_aff
*pma2
)
4214 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4217 /* Given two piecewise multi affine expressions, return a piecewise
4218 * multi-affine expression defined on the union of the definition domains
4219 * of the inputs that is equal to the lexicographic maximum of the two
4220 * inputs on each cell. If only one of the two inputs is defined on
4221 * a given cell, then it is considered to be the maximum.
4223 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4224 __isl_take isl_pw_multi_aff
*pma1
,
4225 __isl_take isl_pw_multi_aff
*pma2
)
4227 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4228 &pw_multi_aff_union_lexmax
);
4231 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4232 __isl_take isl_pw_multi_aff
*pma1
,
4233 __isl_take isl_pw_multi_aff
*pma2
)
4235 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4238 /* Given two piecewise multi affine expressions, return a piecewise
4239 * multi-affine expression defined on the union of the definition domains
4240 * of the inputs that is equal to the lexicographic minimum of the two
4241 * inputs on each cell. If only one of the two inputs is defined on
4242 * a given cell, then it is considered to be the minimum.
4244 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4245 __isl_take isl_pw_multi_aff
*pma1
,
4246 __isl_take isl_pw_multi_aff
*pma2
)
4248 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4249 &pw_multi_aff_union_lexmin
);
4252 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4253 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4255 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4256 &isl_multi_aff_add
);
4259 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4260 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4262 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4266 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4267 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4269 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4270 &isl_multi_aff_sub
);
4273 /* Subtract "pma2" from "pma1" and return the result.
4275 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4276 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4278 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4282 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4283 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4285 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4288 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4289 * with the actual sum on the shared domain and
4290 * the defined expression on the symmetric difference of the domains.
4292 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4293 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4295 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4298 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4299 * with the actual sum on the shared domain and
4300 * the defined expression on the symmetric difference of the domains.
4302 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4303 __isl_take isl_union_pw_multi_aff
*upma1
,
4304 __isl_take isl_union_pw_multi_aff
*upma2
)
4306 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4309 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4310 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4312 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4313 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4317 isl_pw_multi_aff
*res
;
4322 n
= pma1
->n
* pma2
->n
;
4323 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4324 isl_space_copy(pma2
->dim
));
4325 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4327 for (i
= 0; i
< pma1
->n
; ++i
) {
4328 for (j
= 0; j
< pma2
->n
; ++j
) {
4332 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4333 isl_set_copy(pma2
->p
[j
].set
));
4334 ma
= isl_multi_aff_product(
4335 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4336 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4337 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4341 isl_pw_multi_aff_free(pma1
);
4342 isl_pw_multi_aff_free(pma2
);
4345 isl_pw_multi_aff_free(pma1
);
4346 isl_pw_multi_aff_free(pma2
);
4350 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4351 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4353 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4354 &pw_multi_aff_product
);
4357 /* Construct a map mapping the domain of the piecewise multi-affine expression
4358 * to its range, with each dimension in the range equated to the
4359 * corresponding affine expression on its cell.
4361 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4369 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4371 for (i
= 0; i
< pma
->n
; ++i
) {
4372 isl_multi_aff
*maff
;
4373 isl_basic_map
*bmap
;
4376 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4377 bmap
= isl_basic_map_from_multi_aff(maff
);
4378 map_i
= isl_map_from_basic_map(bmap
);
4379 map_i
= isl_map_intersect_domain(map_i
,
4380 isl_set_copy(pma
->p
[i
].set
));
4381 map
= isl_map_union_disjoint(map
, map_i
);
4384 isl_pw_multi_aff_free(pma
);
4388 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4393 if (!isl_space_is_set(pma
->dim
))
4394 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4395 "isl_pw_multi_aff cannot be converted into an isl_set",
4398 return isl_map_from_pw_multi_aff(pma
);
4400 isl_pw_multi_aff_free(pma
);
4404 /* Given a basic map with a single output dimension that is defined
4405 * in terms of the parameters and input dimensions using an equality,
4406 * extract an isl_aff that expresses the output dimension in terms
4407 * of the parameters and input dimensions.
4408 * Note that this expression may involve integer divisions defined
4409 * in terms of parameters and input dimensions.
4411 * This function shares some similarities with
4412 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4414 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4415 __isl_take isl_basic_map
*bmap
)
4420 isl_local_space
*ls
;
4425 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
4426 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4427 "basic map should have a single output dimension",
4429 eq
= isl_basic_map_output_defining_equality(bmap
, 0);
4430 if (eq
>= bmap
->n_eq
)
4431 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4432 "unable to find suitable equality", goto error
);
4433 ls
= isl_basic_map_get_local_space(bmap
);
4434 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
4437 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
4438 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4439 if (isl_int_is_neg(bmap
->eq
[eq
][offset
])) {
4440 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4441 isl_seq_cpy(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4444 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4445 isl_seq_neg(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4448 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][offset
]);
4449 isl_basic_map_free(bmap
);
4451 aff
= isl_aff_remove_unused_divs(aff
);
4454 isl_basic_map_free(bmap
);
4458 /* Given a basic map where each output dimension is defined
4459 * in terms of the parameters and input dimensions using an equality,
4460 * extract an isl_multi_aff that expresses the output dimensions in terms
4461 * of the parameters and input dimensions.
4463 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4464 __isl_take isl_basic_map
*bmap
)
4473 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4474 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4476 for (i
= 0; i
< n_out
; ++i
) {
4477 isl_basic_map
*bmap_i
;
4480 bmap_i
= isl_basic_map_copy(bmap
);
4481 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4482 i
+ 1, n_out
- (1 + i
));
4483 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4484 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4485 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4488 isl_basic_map_free(bmap
);
4493 /* Given a basic set where each set dimension is defined
4494 * in terms of the parameters using an equality,
4495 * extract an isl_multi_aff that expresses the set dimensions in terms
4496 * of the parameters.
4498 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4499 __isl_take isl_basic_set
*bset
)
4501 return extract_isl_multi_aff_from_basic_map(bset
);
4504 /* Create an isl_pw_multi_aff that is equivalent to
4505 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4506 * The given basic map is such that each output dimension is defined
4507 * in terms of the parameters and input dimensions using an equality.
4509 * Since some applications expect the result of isl_pw_multi_aff_from_map
4510 * to only contain integer affine expressions, we compute the floor
4511 * of the expression before returning.
4513 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4514 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4518 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4519 ma
= isl_multi_aff_floor(ma
);
4520 return isl_pw_multi_aff_alloc(domain
, ma
);
4523 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4524 * This obviously only works if the input "map" is single-valued.
4525 * If so, we compute the lexicographic minimum of the image in the form
4526 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4527 * to its lexicographic minimum.
4528 * If the input is not single-valued, we produce an error.
4530 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4531 __isl_take isl_map
*map
)
4535 isl_pw_multi_aff
*pma
;
4537 sv
= isl_map_is_single_valued(map
);
4541 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4542 "map is not single-valued", goto error
);
4543 map
= isl_map_make_disjoint(map
);
4547 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4549 for (i
= 0; i
< map
->n
; ++i
) {
4550 isl_pw_multi_aff
*pma_i
;
4551 isl_basic_map
*bmap
;
4552 bmap
= isl_basic_map_copy(map
->p
[i
]);
4553 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4554 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4564 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4565 * taking into account that the output dimension at position "d"
4566 * can be represented as
4568 * x = floor((e(...) + c1) / m)
4570 * given that constraint "i" is of the form
4572 * e(...) + c1 - m x >= 0
4575 * Let "map" be of the form
4579 * We construct a mapping
4581 * A -> [A -> x = floor(...)]
4583 * apply that to the map, obtaining
4585 * [A -> x = floor(...)] -> B
4587 * and equate dimension "d" to x.
4588 * We then compute a isl_pw_multi_aff representation of the resulting map
4589 * and plug in the mapping above.
4591 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4592 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4596 isl_local_space
*ls
;
4604 isl_pw_multi_aff
*pma
;
4607 is_set
= isl_map_is_set(map
);
4609 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4610 ctx
= isl_map_get_ctx(map
);
4611 space
= isl_space_domain(isl_map_get_space(map
));
4612 n_in
= isl_space_dim(space
, isl_dim_set
);
4613 n
= isl_space_dim(space
, isl_dim_all
);
4615 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4617 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4618 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4620 isl_basic_map_free(hull
);
4622 ls
= isl_local_space_from_space(isl_space_copy(space
));
4623 aff
= isl_aff_alloc_vec(ls
, v
);
4624 aff
= isl_aff_floor(aff
);
4626 isl_space_free(space
);
4627 ma
= isl_multi_aff_from_aff(aff
);
4629 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4630 ma
= isl_multi_aff_range_product(ma
,
4631 isl_multi_aff_from_aff(aff
));
4634 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4635 map
= isl_map_apply_domain(map
, insert
);
4636 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4637 pma
= isl_pw_multi_aff_from_map(map
);
4638 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4643 /* Is constraint "c" of the form
4645 * e(...) + c1 - m x >= 0
4649 * -e(...) + c2 + m x >= 0
4651 * where m > 1 and e only depends on parameters and input dimemnsions?
4653 * "offset" is the offset of the output dimensions
4654 * "pos" is the position of output dimension x.
4656 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4658 if (isl_int_is_zero(c
[offset
+ d
]))
4660 if (isl_int_is_one(c
[offset
+ d
]))
4662 if (isl_int_is_negone(c
[offset
+ d
]))
4664 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4666 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4667 total
- (offset
+ d
+ 1)) != -1)
4672 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4674 * As a special case, we first check if there is any pair of constraints,
4675 * shared by all the basic maps in "map" that force a given dimension
4676 * to be equal to the floor of some affine combination of the input dimensions.
4678 * In particular, if we can find two constraints
4680 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4684 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4686 * where m > 1 and e only depends on parameters and input dimemnsions,
4689 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4691 * then we know that we can take
4693 * x = floor((e(...) + c1) / m)
4695 * without having to perform any computation.
4697 * Note that we know that
4701 * If c1 + c2 were 0, then we would have detected an equality during
4702 * simplification. If c1 + c2 were negative, then we would have detected
4705 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4706 __isl_take isl_map
*map
)
4712 isl_basic_map
*hull
;
4714 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4719 dim
= isl_map_dim(map
, isl_dim_out
);
4720 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4721 total
= 1 + isl_basic_map_total_dim(hull
);
4723 for (d
= 0; d
< dim
; ++d
) {
4724 for (i
= 0; i
< n
; ++i
) {
4725 if (!is_potential_div_constraint(hull
->ineq
[i
],
4728 for (j
= i
+ 1; j
< n
; ++j
) {
4729 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4730 hull
->ineq
[j
] + 1, total
- 1))
4732 isl_int_add(sum
, hull
->ineq
[i
][0],
4734 if (isl_int_abs_lt(sum
,
4735 hull
->ineq
[i
][offset
+ d
]))
4742 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4744 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4748 isl_basic_map_free(hull
);
4749 return pw_multi_aff_from_map_base(map
);
4752 isl_basic_map_free(hull
);
4756 /* Given an affine expression
4758 * [A -> B] -> f(A,B)
4760 * construct an isl_multi_aff
4764 * such that dimension "d" in B' is set to "aff" and the remaining
4765 * dimensions are set equal to the corresponding dimensions in B.
4766 * "n_in" is the dimension of the space A.
4767 * "n_out" is the dimension of the space B.
4769 * If "is_set" is set, then the affine expression is of the form
4773 * and we construct an isl_multi_aff
4777 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4778 unsigned n_in
, unsigned n_out
, int is_set
)
4782 isl_space
*space
, *space2
;
4783 isl_local_space
*ls
;
4785 space
= isl_aff_get_domain_space(aff
);
4786 ls
= isl_local_space_from_space(isl_space_copy(space
));
4787 space2
= isl_space_copy(space
);
4789 space2
= isl_space_range(isl_space_unwrap(space2
));
4790 space
= isl_space_map_from_domain_and_range(space
, space2
);
4791 ma
= isl_multi_aff_alloc(space
);
4792 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4794 for (i
= 0; i
< n_out
; ++i
) {
4797 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4798 isl_dim_set
, n_in
+ i
);
4799 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4802 isl_local_space_free(ls
);
4807 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4808 * taking into account that the dimension at position "d" can be written as
4810 * x = m a + f(..) (1)
4812 * where m is equal to "gcd".
4813 * "i" is the index of the equality in "hull" that defines f(..).
4814 * In particular, the equality is of the form
4816 * f(..) - x + m g(existentials) = 0
4820 * -f(..) + x + m g(existentials) = 0
4822 * We basically plug (1) into "map", resulting in a map with "a"
4823 * in the range instead of "x". The corresponding isl_pw_multi_aff
4824 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4826 * Specifically, given the input map
4830 * We first wrap it into a set
4834 * and define (1) on top of the corresponding space, resulting in "aff".
4835 * We use this to create an isl_multi_aff that maps the output position "d"
4836 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4837 * We plug this into the wrapped map, unwrap the result and compute the
4838 * corresponding isl_pw_multi_aff.
4839 * The result is an expression
4847 * so that we can plug that into "aff", after extending the latter to
4853 * If "map" is actually a set, then there is no "A" space, meaning
4854 * that we do not need to perform any wrapping, and that the result
4855 * of the recursive call is of the form
4859 * which is plugged into a mapping of the form
4863 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4864 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4869 isl_local_space
*ls
;
4872 isl_pw_multi_aff
*pma
, *id
;
4878 is_set
= isl_map_is_set(map
);
4880 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4881 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4882 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4887 set
= isl_map_wrap(map
);
4888 space
= isl_space_map_from_set(isl_set_get_space(set
));
4889 ma
= isl_multi_aff_identity(space
);
4890 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4891 aff
= isl_aff_alloc(ls
);
4893 isl_int_set_si(aff
->v
->el
[0], 1);
4894 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4895 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4898 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4900 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4902 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4903 set
= isl_set_preimage_multi_aff(set
, ma
);
4905 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4910 map
= isl_set_unwrap(set
);
4911 pma
= isl_pw_multi_aff_from_map(map
);
4914 space
= isl_pw_multi_aff_get_domain_space(pma
);
4915 space
= isl_space_map_from_set(space
);
4916 id
= isl_pw_multi_aff_identity(space
);
4917 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4919 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4920 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4922 isl_basic_map_free(hull
);
4926 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4928 * As a special case, we first check if all output dimensions are uniquely
4929 * defined in terms of the parameters and input dimensions over the entire
4930 * domain. If so, we extract the desired isl_pw_multi_aff directly
4931 * from the affine hull of "map" and its domain.
4933 * Otherwise, we check if any of the output dimensions is "strided".
4934 * That is, we check if can be written as
4938 * with m greater than 1, a some combination of existentiall quantified
4939 * variables and f and expression in the parameters and input dimensions.
4940 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4942 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4945 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4949 isl_basic_map
*hull
;
4959 hull
= isl_map_affine_hull(isl_map_copy(map
));
4960 sv
= isl_basic_map_plain_is_single_valued(hull
);
4962 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4964 hull
= isl_basic_map_free(hull
);
4968 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4969 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4972 isl_basic_map_free(hull
);
4973 return pw_multi_aff_from_map_check_div(map
);
4978 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4979 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4981 for (i
= 0; i
< n_out
; ++i
) {
4982 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4983 isl_int
*eq
= hull
->eq
[j
];
4984 isl_pw_multi_aff
*res
;
4986 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4987 !isl_int_is_negone(eq
[o_out
+ i
]))
4989 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4991 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4992 n_out
- (i
+ 1)) != -1)
4994 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4995 if (isl_int_is_zero(gcd
))
4997 if (isl_int_is_one(gcd
))
5000 res
= pw_multi_aff_from_map_stride(map
, hull
,
5008 isl_basic_map_free(hull
);
5009 return pw_multi_aff_from_map_check_div(map
);
5015 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5017 return isl_pw_multi_aff_from_map(set
);
5020 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5023 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5025 isl_union_pw_multi_aff
**upma
= user
;
5026 isl_pw_multi_aff
*pma
;
5028 pma
= isl_pw_multi_aff_from_map(map
);
5029 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5031 return *upma
? isl_stat_ok
: isl_stat_error
;
5034 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5037 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5038 __isl_take isl_aff
*aff
)
5041 isl_pw_multi_aff
*pma
;
5043 ma
= isl_multi_aff_from_aff(aff
);
5044 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5045 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5048 /* Try and create an isl_union_pw_multi_aff that is equivalent
5049 * to the given isl_union_map.
5050 * The isl_union_map is required to be single-valued in each space.
5051 * Otherwise, an error is produced.
5053 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5054 __isl_take isl_union_map
*umap
)
5057 isl_union_pw_multi_aff
*upma
;
5059 space
= isl_union_map_get_space(umap
);
5060 upma
= isl_union_pw_multi_aff_empty(space
);
5061 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5062 upma
= isl_union_pw_multi_aff_free(upma
);
5063 isl_union_map_free(umap
);
5068 /* Try and create an isl_union_pw_multi_aff that is equivalent
5069 * to the given isl_union_set.
5070 * The isl_union_set is required to be a singleton in each space.
5071 * Otherwise, an error is produced.
5073 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5074 __isl_take isl_union_set
*uset
)
5076 return isl_union_pw_multi_aff_from_union_map(uset
);
5079 /* Return the piecewise affine expression "set ? 1 : 0".
5081 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5084 isl_space
*space
= isl_set_get_space(set
);
5085 isl_local_space
*ls
= isl_local_space_from_space(space
);
5086 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5087 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5089 one
= isl_aff_add_constant_si(one
, 1);
5090 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5091 set
= isl_set_complement(set
);
5092 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5097 /* Plug in "subs" for dimension "type", "pos" of "aff".
5099 * Let i be the dimension to replace and let "subs" be of the form
5103 * and "aff" of the form
5109 * (a f + d g')/(m d)
5111 * where g' is the result of plugging in "subs" in each of the integer
5114 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5115 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5120 aff
= isl_aff_cow(aff
);
5122 return isl_aff_free(aff
);
5124 ctx
= isl_aff_get_ctx(aff
);
5125 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5126 isl_die(ctx
, isl_error_invalid
,
5127 "spaces don't match", return isl_aff_free(aff
));
5128 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5129 isl_die(ctx
, isl_error_unsupported
,
5130 "cannot handle divs yet", return isl_aff_free(aff
));
5132 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5134 return isl_aff_free(aff
);
5136 aff
->v
= isl_vec_cow(aff
->v
);
5138 return isl_aff_free(aff
);
5140 pos
+= isl_local_space_offset(aff
->ls
, type
);
5143 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5144 aff
->v
->size
, subs
->v
->size
, v
);
5150 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5151 * expressions in "maff".
5153 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5154 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5155 __isl_keep isl_aff
*subs
)
5159 maff
= isl_multi_aff_cow(maff
);
5161 return isl_multi_aff_free(maff
);
5163 if (type
== isl_dim_in
)
5166 for (i
= 0; i
< maff
->n
; ++i
) {
5167 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5169 return isl_multi_aff_free(maff
);
5175 /* Plug in "subs" for dimension "type", "pos" of "pma".
5177 * pma is of the form
5181 * while subs is of the form
5183 * v' = B_j(v) -> S_j
5185 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5186 * has a contribution in the result, in particular
5188 * C_ij(S_j) -> M_i(S_j)
5190 * Note that plugging in S_j in C_ij may also result in an empty set
5191 * and this contribution should simply be discarded.
5193 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5194 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5195 __isl_keep isl_pw_aff
*subs
)
5198 isl_pw_multi_aff
*res
;
5201 return isl_pw_multi_aff_free(pma
);
5203 n
= pma
->n
* subs
->n
;
5204 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5206 for (i
= 0; i
< pma
->n
; ++i
) {
5207 for (j
= 0; j
< subs
->n
; ++j
) {
5209 isl_multi_aff
*res_ij
;
5212 common
= isl_set_intersect(
5213 isl_set_copy(pma
->p
[i
].set
),
5214 isl_set_copy(subs
->p
[j
].set
));
5215 common
= isl_set_substitute(common
,
5216 type
, pos
, subs
->p
[j
].aff
);
5217 empty
= isl_set_plain_is_empty(common
);
5218 if (empty
< 0 || empty
) {
5219 isl_set_free(common
);
5225 res_ij
= isl_multi_aff_substitute(
5226 isl_multi_aff_copy(pma
->p
[i
].maff
),
5227 type
, pos
, subs
->p
[j
].aff
);
5229 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5233 isl_pw_multi_aff_free(pma
);
5236 isl_pw_multi_aff_free(pma
);
5237 isl_pw_multi_aff_free(res
);
5241 /* Compute the preimage of a range of dimensions in the affine expression "src"
5242 * under "ma" and put the result in "dst". The number of dimensions in "src"
5243 * that precede the range is given by "n_before". The number of dimensions
5244 * in the range is given by the number of output dimensions of "ma".
5245 * The number of dimensions that follow the range is given by "n_after".
5246 * If "has_denom" is set (to one),
5247 * then "src" and "dst" have an extra initial denominator.
5248 * "n_div_ma" is the number of existentials in "ma"
5249 * "n_div_bset" is the number of existentials in "src"
5250 * The resulting "dst" (which is assumed to have been allocated by
5251 * the caller) contains coefficients for both sets of existentials,
5252 * first those in "ma" and then those in "src".
5253 * f, c1, c2 and g are temporary objects that have been initialized
5256 * Let src represent the expression
5258 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5260 * and let ma represent the expressions
5262 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5264 * We start out with the following expression for dst:
5266 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5268 * with the multiplication factor f initially equal to 1
5269 * and f \sum_i b_i v_i kept separately.
5270 * For each x_i that we substitute, we multiply the numerator
5271 * (and denominator) of dst by c_1 = m_i and add the numerator
5272 * of the x_i expression multiplied by c_2 = f b_i,
5273 * after removing the common factors of c_1 and c_2.
5274 * The multiplication factor f also needs to be multiplied by c_1
5275 * for the next x_j, j > i.
5277 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5278 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5279 int n_div_ma
, int n_div_bmap
,
5280 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5283 int n_param
, n_in
, n_out
;
5286 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5287 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5288 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5290 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5291 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5292 isl_seq_clr(dst
+ o_dst
, n_in
);
5295 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5298 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5300 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5302 isl_int_set_si(f
, 1);
5304 for (i
= 0; i
< n_out
; ++i
) {
5305 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5307 if (isl_int_is_zero(src
[offset
]))
5309 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5310 isl_int_mul(c2
, f
, src
[offset
]);
5311 isl_int_gcd(g
, c1
, c2
);
5312 isl_int_divexact(c1
, c1
, g
);
5313 isl_int_divexact(c2
, c2
, g
);
5315 isl_int_mul(f
, f
, c1
);
5318 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5319 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5320 o_dst
+= 1 + n_param
;
5321 o_src
+= 1 + n_param
;
5322 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5324 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5325 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5328 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5330 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5331 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5334 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5336 isl_int_mul(dst
[0], dst
[0], c1
);
5340 /* Compute the pullback of "aff" by the function represented by "ma".
5341 * In other words, plug in "ma" in "aff". The result is an affine expression
5342 * defined over the domain space of "ma".
5344 * If "aff" is represented by
5346 * (a(p) + b x + c(divs))/d
5348 * and ma is represented by
5350 * x = D(p) + F(y) + G(divs')
5352 * then the result is
5354 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5356 * The divs in the local space of the input are similarly adjusted
5357 * through a call to isl_local_space_preimage_multi_aff.
5359 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5360 __isl_take isl_multi_aff
*ma
)
5362 isl_aff
*res
= NULL
;
5363 isl_local_space
*ls
;
5364 int n_div_aff
, n_div_ma
;
5365 isl_int f
, c1
, c2
, g
;
5367 ma
= isl_multi_aff_align_divs(ma
);
5371 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5372 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5374 ls
= isl_aff_get_domain_local_space(aff
);
5375 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5376 res
= isl_aff_alloc(ls
);
5385 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5394 isl_multi_aff_free(ma
);
5395 res
= isl_aff_normalize(res
);
5399 isl_multi_aff_free(ma
);
5404 /* Compute the pullback of "aff1" by the function represented by "aff2".
5405 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5406 * defined over the domain space of "aff1".
5408 * The domain of "aff1" should match the range of "aff2", which means
5409 * that it should be single-dimensional.
5411 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5412 __isl_take isl_aff
*aff2
)
5416 ma
= isl_multi_aff_from_aff(aff2
);
5417 return isl_aff_pullback_multi_aff(aff1
, ma
);
5420 /* Compute the pullback of "ma1" by the function represented by "ma2".
5421 * In other words, plug in "ma2" in "ma1".
5423 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5425 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5426 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5429 isl_space
*space
= NULL
;
5431 ma2
= isl_multi_aff_align_divs(ma2
);
5432 ma1
= isl_multi_aff_cow(ma1
);
5436 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5437 isl_multi_aff_get_space(ma1
));
5439 for (i
= 0; i
< ma1
->n
; ++i
) {
5440 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5441 isl_multi_aff_copy(ma2
));
5446 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5447 isl_multi_aff_free(ma2
);
5450 isl_space_free(space
);
5451 isl_multi_aff_free(ma2
);
5452 isl_multi_aff_free(ma1
);
5456 /* Compute the pullback of "ma1" by the function represented by "ma2".
5457 * In other words, plug in "ma2" in "ma1".
5459 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5460 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5462 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5463 &isl_multi_aff_pullback_multi_aff_aligned
);
5466 /* Extend the local space of "dst" to include the divs
5467 * in the local space of "src".
5469 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5470 __isl_keep isl_aff
*src
)
5478 return isl_aff_free(dst
);
5480 ctx
= isl_aff_get_ctx(src
);
5481 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5482 isl_die(ctx
, isl_error_invalid
,
5483 "spaces don't match", goto error
);
5485 if (src
->ls
->div
->n_row
== 0)
5488 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5489 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5490 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5493 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5494 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5502 return isl_aff_free(dst
);
5505 /* Adjust the local spaces of the affine expressions in "maff"
5506 * such that they all have the save divs.
5508 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5509 __isl_take isl_multi_aff
*maff
)
5517 maff
= isl_multi_aff_cow(maff
);
5521 for (i
= 1; i
< maff
->n
; ++i
)
5522 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5523 for (i
= 1; i
< maff
->n
; ++i
) {
5524 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5526 return isl_multi_aff_free(maff
);
5532 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5534 aff
= isl_aff_cow(aff
);
5538 aff
->ls
= isl_local_space_lift(aff
->ls
);
5540 return isl_aff_free(aff
);
5545 /* Lift "maff" to a space with extra dimensions such that the result
5546 * has no more existentially quantified variables.
5547 * If "ls" is not NULL, then *ls is assigned the local space that lies
5548 * at the basis of the lifting applied to "maff".
5550 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5551 __isl_give isl_local_space
**ls
)
5565 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5566 *ls
= isl_local_space_from_space(space
);
5568 return isl_multi_aff_free(maff
);
5573 maff
= isl_multi_aff_cow(maff
);
5574 maff
= isl_multi_aff_align_divs(maff
);
5578 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5579 space
= isl_multi_aff_get_space(maff
);
5580 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5581 space
= isl_space_extend_domain_with_range(space
,
5582 isl_multi_aff_get_space(maff
));
5584 return isl_multi_aff_free(maff
);
5585 isl_space_free(maff
->space
);
5586 maff
->space
= space
;
5589 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5591 return isl_multi_aff_free(maff
);
5594 for (i
= 0; i
< maff
->n
; ++i
) {
5595 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5603 isl_local_space_free(*ls
);
5604 return isl_multi_aff_free(maff
);
5608 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5610 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5611 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5621 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5622 if (pos
< 0 || pos
>= n_out
)
5623 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5624 "index out of bounds", return NULL
);
5626 space
= isl_pw_multi_aff_get_space(pma
);
5627 space
= isl_space_drop_dims(space
, isl_dim_out
,
5628 pos
+ 1, n_out
- pos
- 1);
5629 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5631 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5632 for (i
= 0; i
< pma
->n
; ++i
) {
5634 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5635 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5641 /* Return an isl_pw_multi_aff with the given "set" as domain and
5642 * an unnamed zero-dimensional range.
5644 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5645 __isl_take isl_set
*set
)
5650 space
= isl_set_get_space(set
);
5651 space
= isl_space_from_domain(space
);
5652 ma
= isl_multi_aff_zero(space
);
5653 return isl_pw_multi_aff_alloc(set
, ma
);
5656 /* Add an isl_pw_multi_aff with the given "set" as domain and
5657 * an unnamed zero-dimensional range to *user.
5659 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5662 isl_union_pw_multi_aff
**upma
= user
;
5663 isl_pw_multi_aff
*pma
;
5665 pma
= isl_pw_multi_aff_from_domain(set
);
5666 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5671 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5672 * an unnamed zero-dimensional range.
5674 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5675 __isl_take isl_union_set
*uset
)
5678 isl_union_pw_multi_aff
*upma
;
5683 space
= isl_union_set_get_space(uset
);
5684 upma
= isl_union_pw_multi_aff_empty(space
);
5686 if (isl_union_set_foreach_set(uset
,
5687 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5690 isl_union_set_free(uset
);
5693 isl_union_set_free(uset
);
5694 isl_union_pw_multi_aff_free(upma
);
5698 /* Convert "pma" to an isl_map and add it to *umap.
5700 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5703 isl_union_map
**umap
= user
;
5706 map
= isl_map_from_pw_multi_aff(pma
);
5707 *umap
= isl_union_map_add_map(*umap
, map
);
5712 /* Construct a union map mapping the domain of the union
5713 * piecewise multi-affine expression to its range, with each dimension
5714 * in the range equated to the corresponding affine expression on its cell.
5716 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5717 __isl_take isl_union_pw_multi_aff
*upma
)
5720 isl_union_map
*umap
;
5725 space
= isl_union_pw_multi_aff_get_space(upma
);
5726 umap
= isl_union_map_empty(space
);
5728 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5729 &map_from_pw_multi_aff
, &umap
) < 0)
5732 isl_union_pw_multi_aff_free(upma
);
5735 isl_union_pw_multi_aff_free(upma
);
5736 isl_union_map_free(umap
);
5740 /* Local data for bin_entry and the callback "fn".
5742 struct isl_union_pw_multi_aff_bin_data
{
5743 isl_union_pw_multi_aff
*upma2
;
5744 isl_union_pw_multi_aff
*res
;
5745 isl_pw_multi_aff
*pma
;
5746 isl_stat (*fn
)(void **entry
, void *user
);
5749 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5750 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5752 static isl_stat
bin_entry(void **entry
, void *user
)
5754 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5755 isl_pw_multi_aff
*pma
= *entry
;
5758 if (isl_hash_table_foreach(data
->upma2
->space
->ctx
, &data
->upma2
->table
,
5759 data
->fn
, data
) < 0)
5760 return isl_stat_error
;
5765 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5766 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5767 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5768 * as *entry. The callback should adjust data->res if desired.
5770 static __isl_give isl_union_pw_multi_aff
*bin_op(
5771 __isl_take isl_union_pw_multi_aff
*upma1
,
5772 __isl_take isl_union_pw_multi_aff
*upma2
,
5773 isl_stat (*fn
)(void **entry
, void *user
))
5776 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5778 space
= isl_union_pw_multi_aff_get_space(upma2
);
5779 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5780 space
= isl_union_pw_multi_aff_get_space(upma1
);
5781 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5783 if (!upma1
|| !upma2
)
5787 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->space
),
5789 if (isl_hash_table_foreach(upma1
->space
->ctx
, &upma1
->table
,
5790 &bin_entry
, &data
) < 0)
5793 isl_union_pw_multi_aff_free(upma1
);
5794 isl_union_pw_multi_aff_free(upma2
);
5797 isl_union_pw_multi_aff_free(upma1
);
5798 isl_union_pw_multi_aff_free(upma2
);
5799 isl_union_pw_multi_aff_free(data
.res
);
5803 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5804 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5806 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5807 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5811 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5812 isl_pw_multi_aff_get_space(pma2
));
5813 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5814 &isl_multi_aff_range_product
);
5817 /* Given two isl_pw_multi_affs A -> B and C -> D,
5818 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5820 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5821 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5823 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5824 &pw_multi_aff_range_product
);
5827 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5828 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5830 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5831 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5835 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5836 isl_pw_multi_aff_get_space(pma2
));
5837 space
= isl_space_flatten_range(space
);
5838 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5839 &isl_multi_aff_flat_range_product
);
5842 /* Given two isl_pw_multi_affs A -> B and C -> D,
5843 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5845 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5846 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5848 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5849 &pw_multi_aff_flat_range_product
);
5852 /* If data->pma and *entry have the same domain space, then compute
5853 * their flat range product and the result to data->res.
5855 static isl_stat
flat_range_product_entry(void **entry
, void *user
)
5857 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5858 isl_pw_multi_aff
*pma2
= *entry
;
5860 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5861 pma2
->dim
, isl_dim_in
))
5864 pma2
= isl_pw_multi_aff_flat_range_product(
5865 isl_pw_multi_aff_copy(data
->pma
),
5866 isl_pw_multi_aff_copy(pma2
));
5868 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5873 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5874 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5876 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5877 __isl_take isl_union_pw_multi_aff
*upma1
,
5878 __isl_take isl_union_pw_multi_aff
*upma2
)
5880 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5883 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5884 * The parameters are assumed to have been aligned.
5886 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5887 * except that it works on two different isl_pw_* types.
5889 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5890 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5891 __isl_take isl_pw_aff
*pa
)
5894 isl_pw_multi_aff
*res
= NULL
;
5899 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5900 pa
->dim
, isl_dim_in
))
5901 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5902 "domains don't match", goto error
);
5903 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5904 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5905 "index out of bounds", goto error
);
5908 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5910 for (i
= 0; i
< pma
->n
; ++i
) {
5911 for (j
= 0; j
< pa
->n
; ++j
) {
5913 isl_multi_aff
*res_ij
;
5916 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5917 isl_set_copy(pa
->p
[j
].set
));
5918 empty
= isl_set_plain_is_empty(common
);
5919 if (empty
< 0 || empty
) {
5920 isl_set_free(common
);
5926 res_ij
= isl_multi_aff_set_aff(
5927 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5928 isl_aff_copy(pa
->p
[j
].aff
));
5929 res_ij
= isl_multi_aff_gist(res_ij
,
5930 isl_set_copy(common
));
5932 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5936 isl_pw_multi_aff_free(pma
);
5937 isl_pw_aff_free(pa
);
5940 isl_pw_multi_aff_free(pma
);
5941 isl_pw_aff_free(pa
);
5942 return isl_pw_multi_aff_free(res
);
5945 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5947 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5948 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5949 __isl_take isl_pw_aff
*pa
)
5953 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5954 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5955 if (!isl_space_has_named_params(pma
->dim
) ||
5956 !isl_space_has_named_params(pa
->dim
))
5957 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5958 "unaligned unnamed parameters", goto error
);
5959 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5960 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5961 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5963 isl_pw_multi_aff_free(pma
);
5964 isl_pw_aff_free(pa
);
5968 /* Do the parameters of "pa" match those of "space"?
5970 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5971 __isl_keep isl_space
*space
)
5973 isl_space
*pa_space
;
5979 pa_space
= isl_pw_aff_get_space(pa
);
5981 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5983 isl_space_free(pa_space
);
5987 /* Check that the domain space of "pa" matches "space".
5989 * Return 0 on success and -1 on error.
5991 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5992 __isl_keep isl_space
*space
)
5994 isl_space
*pa_space
;
6000 pa_space
= isl_pw_aff_get_space(pa
);
6002 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6006 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6007 "parameters don't match", goto error
);
6008 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6009 pa_space
, isl_dim_in
);
6013 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6014 "domains don't match", goto error
);
6015 isl_space_free(pa_space
);
6018 isl_space_free(pa_space
);
6027 #include <isl_multi_templ.c>
6028 #include <isl_multi_apply_set.c>
6029 #include <isl_multi_gist.c>
6030 #include <isl_multi_intersect.c>
6032 /* Scale the elements of "pma" by the corresponding elements of "mv".
6034 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6035 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6039 pma
= isl_pw_multi_aff_cow(pma
);
6042 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6043 mv
->space
, isl_dim_set
))
6044 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6045 "spaces don't match", goto error
);
6046 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6047 mv
->space
, isl_dim_param
)) {
6048 pma
= isl_pw_multi_aff_align_params(pma
,
6049 isl_multi_val_get_space(mv
));
6050 mv
= isl_multi_val_align_params(mv
,
6051 isl_pw_multi_aff_get_space(pma
));
6056 for (i
= 0; i
< pma
->n
; ++i
) {
6057 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6058 isl_multi_val_copy(mv
));
6059 if (!pma
->p
[i
].maff
)
6063 isl_multi_val_free(mv
);
6066 isl_multi_val_free(mv
);
6067 isl_pw_multi_aff_free(pma
);
6071 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
6072 * mv contains the mv argument.
6073 * res collects the results.
6075 struct isl_union_pw_multi_aff_scale_multi_val_data
{
6077 isl_union_pw_multi_aff
*res
;
6080 /* This function is called for each entry of an isl_union_pw_multi_aff.
6081 * If the space of the entry matches that of data->mv,
6082 * then apply isl_pw_multi_aff_scale_multi_val and add the result
6085 static isl_stat
union_pw_multi_aff_scale_multi_val_entry(void **entry
,
6088 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
6089 isl_pw_multi_aff
*pma
= *entry
;
6092 return isl_stat_error
;
6093 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6094 data
->mv
->space
, isl_dim_set
))
6097 pma
= isl_pw_multi_aff_copy(pma
);
6098 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
6099 isl_multi_val_copy(data
->mv
));
6100 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
6102 return isl_stat_error
;
6107 /* Scale the elements of "upma" by the corresponding elements of "mv",
6108 * for those entries that match the space of "mv".
6110 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6111 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6113 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
6115 upma
= isl_union_pw_multi_aff_align_params(upma
,
6116 isl_multi_val_get_space(mv
));
6117 mv
= isl_multi_val_align_params(mv
,
6118 isl_union_pw_multi_aff_get_space(upma
));
6123 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->space
),
6125 if (isl_hash_table_foreach(upma
->space
->ctx
, &upma
->table
,
6126 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
6129 isl_multi_val_free(mv
);
6130 isl_union_pw_multi_aff_free(upma
);
6133 isl_multi_val_free(mv
);
6134 isl_union_pw_multi_aff_free(upma
);
6138 /* Construct and return a piecewise multi affine expression
6139 * in the given space with value zero in each of the output dimensions and
6140 * a universe domain.
6142 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6144 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6147 /* Construct and return a piecewise multi affine expression
6148 * that is equal to the given piecewise affine expression.
6150 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6151 __isl_take isl_pw_aff
*pa
)
6155 isl_pw_multi_aff
*pma
;
6160 space
= isl_pw_aff_get_space(pa
);
6161 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6163 for (i
= 0; i
< pa
->n
; ++i
) {
6167 set
= isl_set_copy(pa
->p
[i
].set
);
6168 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6169 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6172 isl_pw_aff_free(pa
);
6176 /* Construct a set or map mapping the shared (parameter) domain
6177 * of the piecewise affine expressions to the range of "mpa"
6178 * with each dimension in the range equated to the
6179 * corresponding piecewise affine expression.
6181 static __isl_give isl_map
*map_from_multi_pw_aff(
6182 __isl_take isl_multi_pw_aff
*mpa
)
6191 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6192 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6193 "invalid space", goto error
);
6195 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6196 map
= isl_map_universe(isl_space_from_domain(space
));
6198 for (i
= 0; i
< mpa
->n
; ++i
) {
6202 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6203 map_i
= map_from_pw_aff(pa
);
6205 map
= isl_map_flat_range_product(map
, map_i
);
6208 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6210 isl_multi_pw_aff_free(mpa
);
6213 isl_multi_pw_aff_free(mpa
);
6217 /* Construct a map mapping the shared domain
6218 * of the piecewise affine expressions to the range of "mpa"
6219 * with each dimension in the range equated to the
6220 * corresponding piecewise affine expression.
6222 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6226 if (isl_space_is_set(mpa
->space
))
6227 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6228 "space of input is not a map", goto error
);
6230 return map_from_multi_pw_aff(mpa
);
6232 isl_multi_pw_aff_free(mpa
);
6236 /* Construct a set mapping the shared parameter domain
6237 * of the piecewise affine expressions to the space of "mpa"
6238 * with each dimension in the range equated to the
6239 * corresponding piecewise affine expression.
6241 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6245 if (!isl_space_is_set(mpa
->space
))
6246 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6247 "space of input is not a set", goto error
);
6249 return map_from_multi_pw_aff(mpa
);
6251 isl_multi_pw_aff_free(mpa
);
6255 /* Construct and return a piecewise multi affine expression
6256 * that is equal to the given multi piecewise affine expression
6257 * on the shared domain of the piecewise affine expressions.
6259 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6260 __isl_take isl_multi_pw_aff
*mpa
)
6265 isl_pw_multi_aff
*pma
;
6270 space
= isl_multi_pw_aff_get_space(mpa
);
6273 isl_multi_pw_aff_free(mpa
);
6274 return isl_pw_multi_aff_zero(space
);
6277 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6278 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6280 for (i
= 1; i
< mpa
->n
; ++i
) {
6281 isl_pw_multi_aff
*pma_i
;
6283 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6284 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6285 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6288 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6290 isl_multi_pw_aff_free(mpa
);
6294 /* Construct and return a multi piecewise affine expression
6295 * that is equal to the given multi affine expression.
6297 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6298 __isl_take isl_multi_aff
*ma
)
6301 isl_multi_pw_aff
*mpa
;
6306 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6307 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6309 for (i
= 0; i
< n
; ++i
) {
6312 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6313 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6316 isl_multi_aff_free(ma
);
6320 /* Construct and return a multi piecewise affine expression
6321 * that is equal to the given piecewise multi affine expression.
6323 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6324 __isl_take isl_pw_multi_aff
*pma
)
6328 isl_multi_pw_aff
*mpa
;
6333 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6334 space
= isl_pw_multi_aff_get_space(pma
);
6335 mpa
= isl_multi_pw_aff_alloc(space
);
6337 for (i
= 0; i
< n
; ++i
) {
6340 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6341 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6344 isl_pw_multi_aff_free(pma
);
6348 /* Do "pa1" and "pa2" represent the same function?
6350 * We first check if they are obviously equal.
6351 * If not, we convert them to maps and check if those are equal.
6353 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6356 isl_map
*map1
, *map2
;
6361 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6362 if (equal
< 0 || equal
)
6365 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6366 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6367 equal
= isl_map_is_equal(map1
, map2
);
6374 /* Do "mpa1" and "mpa2" represent the same function?
6376 * Note that we cannot convert the entire isl_multi_pw_aff
6377 * to a map because the domains of the piecewise affine expressions
6378 * may not be the same.
6380 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6381 __isl_keep isl_multi_pw_aff
*mpa2
)
6387 return isl_bool_error
;
6389 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6390 mpa2
->space
, isl_dim_param
)) {
6391 if (!isl_space_has_named_params(mpa1
->space
))
6392 return isl_bool_false
;
6393 if (!isl_space_has_named_params(mpa2
->space
))
6394 return isl_bool_false
;
6395 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6396 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6397 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6398 isl_multi_pw_aff_get_space(mpa2
));
6399 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6400 isl_multi_pw_aff_get_space(mpa1
));
6401 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6402 isl_multi_pw_aff_free(mpa1
);
6403 isl_multi_pw_aff_free(mpa2
);
6407 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6408 if (equal
< 0 || !equal
)
6411 for (i
= 0; i
< mpa1
->n
; ++i
) {
6412 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6413 if (equal
< 0 || !equal
)
6417 return isl_bool_true
;
6420 /* Coalesce the elements of "mpa".
6422 * Note that such coalescing does not change the meaning of "mpa"
6423 * so there is no need to cow. We do need to be careful not to
6424 * destroy any other copies of "mpa" in case of failure.
6426 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
6427 __isl_take isl_multi_pw_aff
*mpa
)
6434 for (i
= 0; i
< mpa
->n
; ++i
) {
6435 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6436 pa
= isl_pw_aff_coalesce(pa
);
6438 return isl_multi_pw_aff_free(mpa
);
6439 isl_pw_aff_free(mpa
->p
[i
]);
6446 /* Compute the pullback of "mpa" by the function represented by "ma".
6447 * In other words, plug in "ma" in "mpa".
6449 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6451 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6452 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6455 isl_space
*space
= NULL
;
6457 mpa
= isl_multi_pw_aff_cow(mpa
);
6461 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6462 isl_multi_pw_aff_get_space(mpa
));
6466 for (i
= 0; i
< mpa
->n
; ++i
) {
6467 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6468 isl_multi_aff_copy(ma
));
6473 isl_multi_aff_free(ma
);
6474 isl_space_free(mpa
->space
);
6478 isl_space_free(space
);
6479 isl_multi_pw_aff_free(mpa
);
6480 isl_multi_aff_free(ma
);
6484 /* Compute the pullback of "mpa" by the function represented by "ma".
6485 * In other words, plug in "ma" in "mpa".
6487 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6488 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6492 if (isl_space_match(mpa
->space
, isl_dim_param
,
6493 ma
->space
, isl_dim_param
))
6494 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6495 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6496 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6497 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6499 isl_multi_pw_aff_free(mpa
);
6500 isl_multi_aff_free(ma
);
6504 /* Compute the pullback of "mpa" by the function represented by "pma".
6505 * In other words, plug in "pma" in "mpa".
6507 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6509 static __isl_give isl_multi_pw_aff
*
6510 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6511 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6514 isl_space
*space
= NULL
;
6516 mpa
= isl_multi_pw_aff_cow(mpa
);
6520 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6521 isl_multi_pw_aff_get_space(mpa
));
6523 for (i
= 0; i
< mpa
->n
; ++i
) {
6524 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6525 isl_pw_multi_aff_copy(pma
));
6530 isl_pw_multi_aff_free(pma
);
6531 isl_space_free(mpa
->space
);
6535 isl_space_free(space
);
6536 isl_multi_pw_aff_free(mpa
);
6537 isl_pw_multi_aff_free(pma
);
6541 /* Compute the pullback of "mpa" by the function represented by "pma".
6542 * In other words, plug in "pma" in "mpa".
6544 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6545 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6549 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6550 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6551 mpa
= isl_multi_pw_aff_align_params(mpa
,
6552 isl_pw_multi_aff_get_space(pma
));
6553 pma
= isl_pw_multi_aff_align_params(pma
,
6554 isl_multi_pw_aff_get_space(mpa
));
6555 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6557 isl_multi_pw_aff_free(mpa
);
6558 isl_pw_multi_aff_free(pma
);
6562 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6563 * with the domain of "aff". The domain of the result is the same
6565 * "mpa" and "aff" are assumed to have been aligned.
6567 * We first extract the parametric constant from "aff", defined
6568 * over the correct domain.
6569 * Then we add the appropriate combinations of the members of "mpa".
6570 * Finally, we add the integer divisions through recursive calls.
6572 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6573 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6581 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6582 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6584 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6585 tmp
= isl_aff_copy(aff
);
6586 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6587 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6588 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6589 isl_space_dim(space
, isl_dim_set
));
6590 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6591 pa
= isl_pw_aff_from_aff(tmp
);
6593 for (i
= 0; i
< n_in
; ++i
) {
6596 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6598 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6599 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6600 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6601 pa
= isl_pw_aff_add(pa
, pa_i
);
6604 for (i
= 0; i
< n_div
; ++i
) {
6608 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6610 div
= isl_aff_get_div(aff
, i
);
6611 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6612 isl_multi_pw_aff_copy(mpa
), div
);
6613 pa_i
= isl_pw_aff_floor(pa_i
);
6614 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6615 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6616 pa
= isl_pw_aff_add(pa
, pa_i
);
6619 isl_multi_pw_aff_free(mpa
);
6625 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6626 * with the domain of "aff". The domain of the result is the same
6629 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6630 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6634 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6635 mpa
->space
, isl_dim_param
))
6636 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6638 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6639 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6641 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6644 isl_multi_pw_aff_free(mpa
);
6648 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6649 * with the domain of "pa". The domain of the result is the same
6651 * "mpa" and "pa" are assumed to have been aligned.
6653 * We consider each piece in turn. Note that the domains of the
6654 * pieces are assumed to be disjoint and they remain disjoint
6655 * after taking the preimage (over the same function).
6657 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6658 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6667 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6668 isl_pw_aff_get_space(pa
));
6669 res
= isl_pw_aff_empty(space
);
6671 for (i
= 0; i
< pa
->n
; ++i
) {
6675 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6676 isl_multi_pw_aff_copy(mpa
),
6677 isl_aff_copy(pa
->p
[i
].aff
));
6678 domain
= isl_set_copy(pa
->p
[i
].set
);
6679 domain
= isl_set_preimage_multi_pw_aff(domain
,
6680 isl_multi_pw_aff_copy(mpa
));
6681 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6682 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6685 isl_pw_aff_free(pa
);
6686 isl_multi_pw_aff_free(mpa
);
6689 isl_pw_aff_free(pa
);
6690 isl_multi_pw_aff_free(mpa
);
6694 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6695 * with the domain of "pa". The domain of the result is the same
6698 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6699 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6703 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6704 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6706 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6707 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6709 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6711 isl_pw_aff_free(pa
);
6712 isl_multi_pw_aff_free(mpa
);
6716 /* Compute the pullback of "pa" by the function represented by "mpa".
6717 * In other words, plug in "mpa" in "pa".
6718 * "pa" and "mpa" are assumed to have been aligned.
6720 * The pullback is computed by applying "pa" to "mpa".
6722 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6723 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6725 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6728 /* Compute the pullback of "pa" by the function represented by "mpa".
6729 * In other words, plug in "mpa" in "pa".
6731 * The pullback is computed by applying "pa" to "mpa".
6733 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6734 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6736 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6739 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6740 * In other words, plug in "mpa2" in "mpa1".
6742 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6744 * We pullback each member of "mpa1" in turn.
6746 static __isl_give isl_multi_pw_aff
*
6747 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6748 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6751 isl_space
*space
= NULL
;
6753 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6757 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6758 isl_multi_pw_aff_get_space(mpa1
));
6760 for (i
= 0; i
< mpa1
->n
; ++i
) {
6761 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6762 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6767 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6769 isl_multi_pw_aff_free(mpa2
);
6772 isl_space_free(space
);
6773 isl_multi_pw_aff_free(mpa1
);
6774 isl_multi_pw_aff_free(mpa2
);
6778 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6779 * In other words, plug in "mpa2" in "mpa1".
6781 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6782 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6784 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6785 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6788 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6789 * of "mpa1" and "mpa2" live in the same space, construct map space
6790 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6791 * with this map space as extract argument.
6793 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6794 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6795 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6796 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6799 isl_space
*space1
, *space2
;
6802 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6803 isl_multi_pw_aff_get_space(mpa2
));
6804 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6805 isl_multi_pw_aff_get_space(mpa1
));
6808 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6809 mpa2
->space
, isl_dim_out
);
6813 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6814 "range spaces don't match", goto error
);
6815 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6816 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6817 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6819 res
= order(mpa1
, mpa2
, space1
);
6820 isl_multi_pw_aff_free(mpa1
);
6821 isl_multi_pw_aff_free(mpa2
);
6824 isl_multi_pw_aff_free(mpa1
);
6825 isl_multi_pw_aff_free(mpa2
);
6829 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6830 * where the function values are equal. "space" is the space of the result.
6831 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6833 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6834 * in the sequences are equal.
6836 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6837 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6838 __isl_take isl_space
*space
)
6843 res
= isl_map_universe(space
);
6845 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6846 for (i
= 0; i
< n
; ++i
) {
6847 isl_pw_aff
*pa1
, *pa2
;
6850 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6851 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6852 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6853 res
= isl_map_intersect(res
, map
);
6859 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6860 * where the function values are equal.
6862 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6863 __isl_take isl_multi_pw_aff
*mpa2
)
6865 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6866 &isl_multi_pw_aff_eq_map_on_space
);
6869 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6870 * where the function values of "mpa1" is lexicographically satisfies "base"
6871 * compared to that of "mpa2". "space" is the space of the result.
6872 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6874 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6875 * if its i-th element satisfies "base" when compared to
6876 * the i-th element of "mpa2" while all previous elements are
6879 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6880 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6881 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6882 __isl_take isl_pw_aff
*pa2
),
6883 __isl_take isl_space
*space
)
6886 isl_map
*res
, *rest
;
6888 res
= isl_map_empty(isl_space_copy(space
));
6889 rest
= isl_map_universe(space
);
6891 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6892 for (i
= 0; i
< n
; ++i
) {
6893 isl_pw_aff
*pa1
, *pa2
;
6896 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6897 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6898 map
= base(pa1
, pa2
);
6899 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6900 res
= isl_map_union(res
, map
);
6905 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6906 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6907 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6908 rest
= isl_map_intersect(rest
, map
);
6915 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6916 * where the function value of "mpa1" is lexicographically less than that
6917 * of "mpa2". "space" is the space of the result.
6918 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6920 * "mpa1" is less than "mpa2" if its i-th element is smaller
6921 * than the i-th element of "mpa2" while all previous elements are
6924 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
6925 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6926 __isl_take isl_space
*space
)
6928 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6929 &isl_pw_aff_lt_map
, space
);
6932 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6933 * where the function value of "mpa1" is lexicographically less than that
6936 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
6937 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6939 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6940 &isl_multi_pw_aff_lex_lt_map_on_space
);
6943 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6944 * where the function value of "mpa1" is lexicographically greater than that
6945 * of "mpa2". "space" is the space of the result.
6946 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6948 * "mpa1" is greater than "mpa2" if its i-th element is greater
6949 * than the i-th element of "mpa2" while all previous elements are
6952 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
6953 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6954 __isl_take isl_space
*space
)
6956 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6957 &isl_pw_aff_gt_map
, space
);
6960 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6961 * where the function value of "mpa1" is lexicographically greater than that
6964 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
6965 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6967 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6968 &isl_multi_pw_aff_lex_gt_map_on_space
);
6971 /* Compare two isl_affs.
6973 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6974 * than "aff2" and 0 if they are equal.
6976 * The order is fairly arbitrary. We do consider expressions that only involve
6977 * earlier dimensions as "smaller".
6979 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6992 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6996 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6997 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6999 return last1
- last2
;
7001 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7004 /* Compare two isl_pw_affs.
7006 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7007 * than "pa2" and 0 if they are equal.
7009 * The order is fairly arbitrary. We do consider expressions that only involve
7010 * earlier dimensions as "smaller".
7012 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7013 __isl_keep isl_pw_aff
*pa2
)
7026 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7030 if (pa1
->n
!= pa2
->n
)
7031 return pa1
->n
- pa2
->n
;
7033 for (i
= 0; i
< pa1
->n
; ++i
) {
7034 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7037 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7045 /* Return a piecewise affine expression that is equal to "v" on "domain".
7047 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7048 __isl_take isl_val
*v
)
7051 isl_local_space
*ls
;
7054 space
= isl_set_get_space(domain
);
7055 ls
= isl_local_space_from_space(space
);
7056 aff
= isl_aff_val_on_domain(ls
, v
);
7058 return isl_pw_aff_alloc(domain
, aff
);
7061 /* Return a multi affine expression that is equal to "mv" on domain
7064 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7065 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7069 isl_local_space
*ls
;
7075 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7076 space2
= isl_multi_val_get_space(mv
);
7077 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7078 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7079 space
= isl_space_map_from_domain_and_range(space
, space2
);
7080 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7081 ls
= isl_local_space_from_space(isl_space_domain(space
));
7082 for (i
= 0; i
< n
; ++i
) {
7086 v
= isl_multi_val_get_val(mv
, i
);
7087 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7088 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7090 isl_local_space_free(ls
);
7092 isl_multi_val_free(mv
);
7095 isl_space_free(space
);
7096 isl_multi_val_free(mv
);
7100 /* Return a piecewise multi-affine expression
7101 * that is equal to "mv" on "domain".
7103 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7104 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7109 space
= isl_set_get_space(domain
);
7110 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7112 return isl_pw_multi_aff_alloc(domain
, ma
);
7115 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7116 * mv is the value that should be attained on each domain set
7117 * res collects the results
7119 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7121 isl_union_pw_multi_aff
*res
;
7124 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7125 * and add it to data->res.
7127 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7130 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7131 isl_pw_multi_aff
*pma
;
7134 mv
= isl_multi_val_copy(data
->mv
);
7135 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7136 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7138 return data
->res
? isl_stat_ok
: isl_stat_error
;
7141 /* Return a union piecewise multi-affine expression
7142 * that is equal to "mv" on "domain".
7144 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7145 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7147 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7150 space
= isl_union_set_get_space(domain
);
7151 data
.res
= isl_union_pw_multi_aff_empty(space
);
7153 if (isl_union_set_foreach_set(domain
,
7154 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7155 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7156 isl_union_set_free(domain
);
7157 isl_multi_val_free(mv
);
7161 /* Compute the pullback of data->pma by the function represented by "pma2",
7162 * provided the spaces match, and add the results to data->res.
7164 static isl_stat
pullback_entry(void **entry
, void *user
)
7166 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7167 isl_pw_multi_aff
*pma2
= *entry
;
7169 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7170 pma2
->dim
, isl_dim_out
))
7173 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7174 isl_pw_multi_aff_copy(data
->pma
),
7175 isl_pw_multi_aff_copy(pma2
));
7177 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7179 return isl_stat_error
;
7184 /* Compute the pullback of "upma1" by the function represented by "upma2".
7186 __isl_give isl_union_pw_multi_aff
*
7187 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7188 __isl_take isl_union_pw_multi_aff
*upma1
,
7189 __isl_take isl_union_pw_multi_aff
*upma2
)
7191 return bin_op(upma1
, upma2
, &pullback_entry
);
7194 /* Check that the domain space of "upa" matches "space".
7196 * Return 0 on success and -1 on error.
7198 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7199 * can in principle never fail since the space "space" is that
7200 * of the isl_multi_union_pw_aff and is a set space such that
7201 * there is no domain space to match.
7203 * We check the parameters and double-check that "space" is
7204 * indeed that of a set.
7206 static int isl_union_pw_aff_check_match_domain_space(
7207 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7209 isl_space
*upa_space
;
7215 match
= isl_space_is_set(space
);
7219 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7220 "expecting set space", return -1);
7222 upa_space
= isl_union_pw_aff_get_space(upa
);
7223 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7227 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7228 "parameters don't match", goto error
);
7230 isl_space_free(upa_space
);
7233 isl_space_free(upa_space
);
7237 /* Do the parameters of "upa" match those of "space"?
7239 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7240 __isl_keep isl_space
*space
)
7242 isl_space
*upa_space
;
7248 upa_space
= isl_union_pw_aff_get_space(upa
);
7250 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7252 isl_space_free(upa_space
);
7256 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7257 * space represents the new parameters.
7258 * res collects the results.
7260 struct isl_union_pw_aff_reset_params_data
{
7262 isl_union_pw_aff
*res
;
7265 /* Replace the parameters of "pa" by data->space and
7266 * add the result to data->res.
7268 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7270 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7273 space
= isl_pw_aff_get_space(pa
);
7274 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7275 pa
= isl_pw_aff_reset_space(pa
, space
);
7276 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7278 return data
->res
? isl_stat_ok
: isl_stat_error
;
7281 /* Replace the domain space of "upa" by "space".
7282 * Since a union expression does not have a (single) domain space,
7283 * "space" is necessarily a parameter space.
7285 * Since the order and the names of the parameters determine
7286 * the hash value, we need to create a new hash table.
7288 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7289 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7291 struct isl_union_pw_aff_reset_params_data data
= { space
};
7294 match
= isl_union_pw_aff_matching_params(upa
, space
);
7296 upa
= isl_union_pw_aff_free(upa
);
7298 isl_space_free(space
);
7302 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7303 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7304 data
.res
= isl_union_pw_aff_free(data
.res
);
7306 isl_union_pw_aff_free(upa
);
7307 isl_space_free(space
);
7311 /* Replace the entry of isl_union_pw_aff to which "entry" points
7314 static isl_stat
floor_entry(void **entry
, void *user
)
7316 isl_pw_aff
**pa
= (isl_pw_aff
**) entry
;
7318 *pa
= isl_pw_aff_floor(*pa
);
7320 return isl_stat_error
;
7325 /* Given f, return floor(f).
7327 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7328 __isl_take isl_union_pw_aff
*upa
)
7332 upa
= isl_union_pw_aff_cow(upa
);
7336 ctx
= isl_union_pw_aff_get_ctx(upa
);
7337 if (isl_hash_table_foreach(ctx
, &upa
->table
, &floor_entry
, NULL
) < 0)
7338 upa
= isl_union_pw_aff_free(upa
);
7345 * upa mod m = upa - m * floor(upa/m)
7347 * with m an integer value.
7349 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7350 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7352 isl_union_pw_aff
*res
;
7357 if (!isl_val_is_int(m
))
7358 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7359 "expecting integer modulo", goto error
);
7360 if (!isl_val_is_pos(m
))
7361 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7362 "expecting positive modulo", goto error
);
7364 res
= isl_union_pw_aff_copy(upa
);
7365 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7366 upa
= isl_union_pw_aff_floor(upa
);
7367 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7368 res
= isl_union_pw_aff_sub(res
, upa
);
7373 isl_union_pw_aff_free(upa
);
7377 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7378 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7380 * "res" collects the results.
7382 struct isl_union_pw_aff_aff_on_domain_data
{
7384 isl_union_pw_aff
*res
;
7387 /* Construct a piecewise affine expression that is equal to data->aff
7388 * on "domain" and add the result to data->res.
7390 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7392 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7397 aff
= isl_aff_copy(data
->aff
);
7398 dim
= isl_set_dim(domain
, isl_dim_set
);
7399 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7400 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7401 pa
= isl_pw_aff_alloc(domain
, aff
);
7402 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7404 return data
->res
? isl_stat_ok
: isl_stat_error
;
7407 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7408 * pos is the output position that needs to be extracted.
7409 * res collects the results.
7411 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7413 isl_union_pw_aff
*res
;
7416 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7417 * (assuming it has such a dimension) and add it to data->res.
7419 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7421 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7426 return isl_stat_error
;
7428 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7429 if (data
->pos
>= n_out
) {
7430 isl_pw_multi_aff_free(pma
);
7434 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7435 isl_pw_multi_aff_free(pma
);
7437 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7439 return data
->res
? isl_stat_ok
: isl_stat_error
;
7442 /* Extract an isl_union_pw_aff corresponding to
7443 * output dimension "pos" of "upma".
7445 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7446 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7448 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7455 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7456 "cannot extract at negative position", return NULL
);
7458 space
= isl_union_pw_multi_aff_get_space(upma
);
7459 data
.res
= isl_union_pw_aff_empty(space
);
7461 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7462 &get_union_pw_aff
, &data
) < 0)
7463 data
.res
= isl_union_pw_aff_free(data
.res
);
7468 /* Return a union piecewise affine expression
7469 * that is equal to "aff" on "domain".
7471 * Construct an isl_pw_aff on each of the sets in "domain" and
7472 * collect the results.
7474 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7475 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7477 struct isl_union_pw_aff_aff_on_domain_data data
;
7480 if (!domain
|| !aff
)
7482 if (!isl_local_space_is_params(aff
->ls
))
7483 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7484 "expecting parametric expression", goto error
);
7486 space
= isl_union_set_get_space(domain
);
7487 data
.res
= isl_union_pw_aff_empty(space
);
7489 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7490 data
.res
= isl_union_pw_aff_free(data
.res
);
7491 isl_union_set_free(domain
);
7495 isl_union_set_free(domain
);
7500 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7501 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7502 * "res" collects the results.
7504 struct isl_union_pw_aff_val_on_domain_data
{
7506 isl_union_pw_aff
*res
;
7509 /* Construct a piecewise affine expression that is equal to data->v
7510 * on "domain" and add the result to data->res.
7512 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7514 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7518 v
= isl_val_copy(data
->v
);
7519 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7520 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7522 return data
->res
? isl_stat_ok
: isl_stat_error
;
7525 /* Return a union piecewise affine expression
7526 * that is equal to "v" on "domain".
7528 * Construct an isl_pw_aff on each of the sets in "domain" and
7529 * collect the results.
7531 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7532 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7534 struct isl_union_pw_aff_val_on_domain_data data
;
7537 space
= isl_union_set_get_space(domain
);
7538 data
.res
= isl_union_pw_aff_empty(space
);
7540 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7541 data
.res
= isl_union_pw_aff_free(data
.res
);
7542 isl_union_set_free(domain
);
7547 /* Construct a piecewise multi affine expression
7548 * that is equal to "pa" and add it to upma.
7550 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7553 isl_union_pw_multi_aff
**upma
= user
;
7554 isl_pw_multi_aff
*pma
;
7556 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7557 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7559 return *upma
? isl_stat_ok
: isl_stat_error
;
7562 /* Construct and return a union piecewise multi affine expression
7563 * that is equal to the given union piecewise affine expression.
7565 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7566 __isl_take isl_union_pw_aff
*upa
)
7569 isl_union_pw_multi_aff
*upma
;
7574 space
= isl_union_pw_aff_get_space(upa
);
7575 upma
= isl_union_pw_multi_aff_empty(space
);
7577 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7578 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7579 upma
= isl_union_pw_multi_aff_free(upma
);
7581 isl_union_pw_aff_free(upa
);
7585 /* Compute the set of elements in the domain of "pa" where it is zero and
7586 * add this set to "uset".
7588 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7590 isl_union_set
**uset
= (isl_union_set
**)user
;
7592 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7594 return *uset
? isl_stat_ok
: isl_stat_error
;
7597 /* Return a union set containing those elements in the domain
7598 * of "upa" where it is zero.
7600 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7601 __isl_take isl_union_pw_aff
*upa
)
7603 isl_union_set
*zero
;
7605 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7606 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7607 zero
= isl_union_set_free(zero
);
7609 isl_union_pw_aff_free(upa
);
7613 /* Convert "pa" to an isl_map and add it to *umap.
7615 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7617 isl_union_map
**umap
= user
;
7620 map
= isl_map_from_pw_aff(pa
);
7621 *umap
= isl_union_map_add_map(*umap
, map
);
7623 return *umap
? isl_stat_ok
: isl_stat_error
;
7626 /* Construct a union map mapping the domain of the union
7627 * piecewise affine expression to its range, with the single output dimension
7628 * equated to the corresponding affine expressions on their cells.
7630 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7631 __isl_take isl_union_pw_aff
*upa
)
7634 isl_union_map
*umap
;
7639 space
= isl_union_pw_aff_get_space(upa
);
7640 umap
= isl_union_map_empty(space
);
7642 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7644 umap
= isl_union_map_free(umap
);
7646 isl_union_pw_aff_free(upa
);
7650 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7651 * upma is the function that is plugged in.
7652 * pa is the current part of the function in which upma is plugged in.
7653 * res collects the results.
7655 struct isl_union_pw_aff_pullback_upma_data
{
7656 isl_union_pw_multi_aff
*upma
;
7658 isl_union_pw_aff
*res
;
7661 /* Check if "pma" can be plugged into data->pa.
7662 * If so, perform the pullback and add the result to data->res.
7664 static isl_stat
pa_pb_pma(void **entry
, void *user
)
7666 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7667 isl_pw_multi_aff
*pma
= *entry
;
7670 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7671 pma
->dim
, isl_dim_out
))
7674 pma
= isl_pw_multi_aff_copy(pma
);
7675 pa
= isl_pw_aff_copy(data
->pa
);
7676 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7678 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7680 return data
->res
? isl_stat_ok
: isl_stat_error
;
7683 /* Check if any of the elements of data->upma can be plugged into pa,
7684 * add if so add the result to data->res.
7686 static isl_stat
upa_pb_upma(void **entry
, void *user
)
7688 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7690 isl_pw_aff
*pa
= *entry
;
7693 ctx
= isl_union_pw_multi_aff_get_ctx(data
->upma
);
7694 if (isl_hash_table_foreach(ctx
, &data
->upma
->table
,
7695 &pa_pb_pma
, data
) < 0)
7696 return isl_stat_error
;
7701 /* Compute the pullback of "upa" by the function represented by "upma".
7702 * In other words, plug in "upma" in "upa". The result contains
7703 * expressions defined over the domain space of "upma".
7705 * Run over all pairs of elements in "upa" and "upma", perform
7706 * the pullback when appropriate and collect the results.
7707 * If the hash value were based on the domain space rather than
7708 * the function space, then we could run through all elements
7709 * of "upma" and directly pick out the corresponding element of "upa".
7711 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7712 __isl_take isl_union_pw_aff
*upa
,
7713 __isl_take isl_union_pw_multi_aff
*upma
)
7715 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7719 space
= isl_union_pw_multi_aff_get_space(upma
);
7720 upa
= isl_union_pw_aff_align_params(upa
, space
);
7721 space
= isl_union_pw_aff_get_space(upa
);
7722 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7727 ctx
= isl_union_pw_aff_get_ctx(upa
);
7729 space
= isl_union_pw_aff_get_space(upa
);
7730 data
.res
= isl_union_pw_aff_alloc(space
, upa
->table
.n
);
7731 if (isl_hash_table_foreach(ctx
, &upa
->table
, &upa_pb_upma
, &data
) < 0)
7732 data
.res
= isl_union_pw_aff_free(data
.res
);
7734 isl_union_pw_aff_free(upa
);
7735 isl_union_pw_multi_aff_free(upma
);
7738 isl_union_pw_aff_free(upa
);
7739 isl_union_pw_multi_aff_free(upma
);
7744 #define BASE union_pw_aff
7746 #define DOMBASE union_set
7748 #define NO_MOVE_DIMS
7757 #include <isl_multi_templ.c>
7758 #include <isl_multi_apply_set.c>
7759 #include <isl_multi_apply_union_set.c>
7760 #include <isl_multi_floor.c>
7761 #include <isl_multi_gist.c>
7762 #include <isl_multi_intersect.c>
7764 /* Construct a multiple union piecewise affine expression
7765 * in the given space with value zero in each of the output dimensions.
7767 * Since there is no canonical zero value for
7768 * a union piecewise affine expression, we can only construct
7769 * zero-dimensional "zero" value.
7771 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7772 __isl_take isl_space
*space
)
7777 if (!isl_space_is_set(space
))
7778 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7779 "expecting set space", goto error
);
7780 if (isl_space_dim(space
, isl_dim_out
) != 0)
7781 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7782 "expecting 0D space", goto error
);
7784 return isl_multi_union_pw_aff_alloc(space
);
7786 isl_space_free(space
);
7790 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7791 * with the actual sum on the shared domain and
7792 * the defined expression on the symmetric difference of the domains.
7794 * We simply iterate over the elements in both arguments and
7795 * call isl_union_pw_aff_union_add on each of them.
7797 static __isl_give isl_multi_union_pw_aff
*
7798 isl_multi_union_pw_aff_union_add_aligned(
7799 __isl_take isl_multi_union_pw_aff
*mupa1
,
7800 __isl_take isl_multi_union_pw_aff
*mupa2
)
7802 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7803 &isl_union_pw_aff_union_add
);
7806 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7807 * with the actual sum on the shared domain and
7808 * the defined expression on the symmetric difference of the domains.
7810 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7811 __isl_take isl_multi_union_pw_aff
*mupa1
,
7812 __isl_take isl_multi_union_pw_aff
*mupa2
)
7814 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7815 &isl_multi_union_pw_aff_union_add_aligned
);
7818 /* Construct and return a multi union piecewise affine expression
7819 * that is equal to the given multi affine expression.
7821 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7822 __isl_take isl_multi_aff
*ma
)
7824 isl_multi_pw_aff
*mpa
;
7826 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7827 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7830 /* Construct and return a multi union piecewise affine expression
7831 * that is equal to the given multi piecewise affine expression.
7833 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7834 __isl_take isl_multi_pw_aff
*mpa
)
7838 isl_multi_union_pw_aff
*mupa
;
7843 space
= isl_multi_pw_aff_get_space(mpa
);
7844 space
= isl_space_range(space
);
7845 mupa
= isl_multi_union_pw_aff_alloc(space
);
7847 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7848 for (i
= 0; i
< n
; ++i
) {
7850 isl_union_pw_aff
*upa
;
7852 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7853 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7854 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7857 isl_multi_pw_aff_free(mpa
);
7862 /* Extract the range space of "pma" and assign it to *space.
7863 * If *space has already been set (through a previous call to this function),
7864 * then check that the range space is the same.
7866 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7868 isl_space
**space
= user
;
7869 isl_space
*pma_space
;
7872 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7873 isl_pw_multi_aff_free(pma
);
7876 return isl_stat_error
;
7882 equal
= isl_space_is_equal(pma_space
, *space
);
7883 isl_space_free(pma_space
);
7886 return isl_stat_error
;
7888 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7889 "range spaces not the same", return isl_stat_error
);
7893 /* Construct and return a multi union piecewise affine expression
7894 * that is equal to the given union piecewise multi affine expression.
7896 * In order to be able to perform the conversion, the input
7897 * needs to be non-empty and may only involve a single range space.
7899 __isl_give isl_multi_union_pw_aff
*
7900 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7901 __isl_take isl_union_pw_multi_aff
*upma
)
7903 isl_space
*space
= NULL
;
7904 isl_multi_union_pw_aff
*mupa
;
7909 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7910 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7911 "cannot extract range space from empty input",
7913 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7920 n
= isl_space_dim(space
, isl_dim_set
);
7921 mupa
= isl_multi_union_pw_aff_alloc(space
);
7923 for (i
= 0; i
< n
; ++i
) {
7924 isl_union_pw_aff
*upa
;
7926 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7927 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7930 isl_union_pw_multi_aff_free(upma
);
7933 isl_space_free(space
);
7934 isl_union_pw_multi_aff_free(upma
);
7938 /* Try and create an isl_multi_union_pw_aff that is equivalent
7939 * to the given isl_union_map.
7940 * The isl_union_map is required to be single-valued in each space.
7941 * Moreover, it cannot be empty and all range spaces need to be the same.
7942 * Otherwise, an error is produced.
7944 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7945 __isl_take isl_union_map
*umap
)
7947 isl_union_pw_multi_aff
*upma
;
7949 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7950 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7953 /* Return a multiple union piecewise affine expression
7954 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7955 * have been aligned.
7957 static __isl_give isl_multi_union_pw_aff
*
7958 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7959 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7963 isl_multi_union_pw_aff
*mupa
;
7968 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7969 space
= isl_multi_val_get_space(mv
);
7970 mupa
= isl_multi_union_pw_aff_alloc(space
);
7971 for (i
= 0; i
< n
; ++i
) {
7973 isl_union_pw_aff
*upa
;
7975 v
= isl_multi_val_get_val(mv
, i
);
7976 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
7978 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7981 isl_union_set_free(domain
);
7982 isl_multi_val_free(mv
);
7985 isl_union_set_free(domain
);
7986 isl_multi_val_free(mv
);
7990 /* Return a multiple union piecewise affine expression
7991 * that is equal to "mv" on "domain".
7993 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
7994 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7998 if (isl_space_match(domain
->dim
, isl_dim_param
,
7999 mv
->space
, isl_dim_param
))
8000 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8002 domain
= isl_union_set_align_params(domain
,
8003 isl_multi_val_get_space(mv
));
8004 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8005 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8007 isl_union_set_free(domain
);
8008 isl_multi_val_free(mv
);
8012 /* Return a multiple union piecewise affine expression
8013 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8014 * have been aligned.
8016 static __isl_give isl_multi_union_pw_aff
*
8017 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8018 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8022 isl_multi_union_pw_aff
*mupa
;
8027 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8028 space
= isl_multi_aff_get_space(ma
);
8029 mupa
= isl_multi_union_pw_aff_alloc(space
);
8030 for (i
= 0; i
< n
; ++i
) {
8032 isl_union_pw_aff
*upa
;
8034 aff
= isl_multi_aff_get_aff(ma
, i
);
8035 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8037 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8040 isl_union_set_free(domain
);
8041 isl_multi_aff_free(ma
);
8044 isl_union_set_free(domain
);
8045 isl_multi_aff_free(ma
);
8049 /* Return a multiple union piecewise affine expression
8050 * that is equal to "ma" on "domain".
8052 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8053 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8057 if (isl_space_match(domain
->dim
, isl_dim_param
,
8058 ma
->space
, isl_dim_param
))
8059 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8061 domain
= isl_union_set_align_params(domain
,
8062 isl_multi_aff_get_space(ma
));
8063 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8064 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8066 isl_union_set_free(domain
);
8067 isl_multi_aff_free(ma
);
8071 /* Return a union set containing those elements in the domains
8072 * of the elements of "mupa" where they are all zero.
8074 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8075 __isl_take isl_multi_union_pw_aff
*mupa
)
8078 isl_union_pw_aff
*upa
;
8079 isl_union_set
*zero
;
8084 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8086 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8087 "cannot determine zero set "
8088 "of zero-dimensional function", goto error
);
8090 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8091 zero
= isl_union_pw_aff_zero_union_set(upa
);
8093 for (i
= 1; i
< n
; ++i
) {
8094 isl_union_set
*zero_i
;
8096 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8097 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8099 zero
= isl_union_set_intersect(zero
, zero_i
);
8102 isl_multi_union_pw_aff_free(mupa
);
8105 isl_multi_union_pw_aff_free(mupa
);
8109 /* Construct a union map mapping the shared domain
8110 * of the union piecewise affine expressions to the range of "mupa"
8111 * with each dimension in the range equated to the
8112 * corresponding union piecewise affine expression.
8114 * The input cannot be zero-dimensional as there is
8115 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8117 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8118 __isl_take isl_multi_union_pw_aff
*mupa
)
8122 isl_union_map
*umap
;
8123 isl_union_pw_aff
*upa
;
8128 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8130 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8131 "cannot determine domain of zero-dimensional "
8132 "isl_multi_union_pw_aff", goto error
);
8134 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8135 umap
= isl_union_map_from_union_pw_aff(upa
);
8137 for (i
= 1; i
< n
; ++i
) {
8138 isl_union_map
*umap_i
;
8140 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8141 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8142 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8145 space
= isl_multi_union_pw_aff_get_space(mupa
);
8146 umap
= isl_union_map_reset_range_space(umap
, space
);
8148 isl_multi_union_pw_aff_free(mupa
);
8151 isl_multi_union_pw_aff_free(mupa
);
8155 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8156 * "range" is the space from which to set the range space.
8157 * "res" collects the results.
8159 struct isl_union_pw_multi_aff_reset_range_space_data
{
8161 isl_union_pw_multi_aff
*res
;
8164 /* Replace the range space of "pma" by the range space of data->range and
8165 * add the result to data->res.
8167 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8169 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8172 space
= isl_pw_multi_aff_get_space(pma
);
8173 space
= isl_space_domain(space
);
8174 space
= isl_space_extend_domain_with_range(space
,
8175 isl_space_copy(data
->range
));
8176 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8177 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8179 return data
->res
? isl_stat_ok
: isl_stat_error
;
8182 /* Replace the range space of all the piecewise affine expressions in "upma" by
8183 * the range space of "space".
8185 * This assumes that all these expressions have the same output dimension.
8187 * Since the spaces of the expressions change, so do their hash values.
8188 * We therefore need to create a new isl_union_pw_multi_aff.
8189 * Note that the hash value is currently computed based on the entire
8190 * space even though there can only be a single expression with a given
8193 static __isl_give isl_union_pw_multi_aff
*
8194 isl_union_pw_multi_aff_reset_range_space(
8195 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8197 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8198 isl_space
*space_upma
;
8200 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8201 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8202 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8203 &reset_range_space
, &data
) < 0)
8204 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8206 isl_space_free(space
);
8207 isl_union_pw_multi_aff_free(upma
);
8211 /* Construct and return a union piecewise multi affine expression
8212 * that is equal to the given multi union piecewise affine expression.
8214 * In order to be able to perform the conversion, the input
8215 * needs to have a least one output dimension.
8217 __isl_give isl_union_pw_multi_aff
*
8218 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8219 __isl_take isl_multi_union_pw_aff
*mupa
)
8223 isl_union_pw_multi_aff
*upma
;
8224 isl_union_pw_aff
*upa
;
8229 space
= isl_multi_union_pw_aff_get_space(mupa
);
8231 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8233 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8234 "cannot determine domain of zero-dimensional "
8235 "isl_multi_union_pw_aff", goto error
);
8237 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8238 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8240 for (i
= 1; i
< n
; ++i
) {
8241 isl_union_pw_multi_aff
*upma_i
;
8243 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8244 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8245 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8248 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8250 isl_multi_union_pw_aff_free(mupa
);
8253 isl_multi_union_pw_aff_free(mupa
);
8257 /* Intersect the range of "mupa" with "range".
8258 * That is, keep only those domain elements that have a function value
8261 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8262 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8264 isl_union_pw_multi_aff
*upma
;
8265 isl_union_set
*domain
;
8270 if (!mupa
|| !range
)
8273 space
= isl_set_get_space(range
);
8274 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8275 space
, isl_dim_set
);
8276 isl_space_free(space
);
8280 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8281 "space don't match", goto error
);
8282 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8284 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8285 "cannot intersect range of zero-dimensional "
8286 "isl_multi_union_pw_aff", goto error
);
8288 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8289 isl_multi_union_pw_aff_copy(mupa
));
8290 domain
= isl_union_set_from_set(range
);
8291 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8292 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8296 isl_multi_union_pw_aff_free(mupa
);
8297 isl_set_free(range
);
8301 /* Return the shared domain of the elements of "mupa".
8303 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8304 __isl_take isl_multi_union_pw_aff
*mupa
)
8307 isl_union_pw_aff
*upa
;
8313 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8315 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8316 "cannot determine domain", goto error
);
8318 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8319 dom
= isl_union_pw_aff_domain(upa
);
8320 for (i
= 1; i
< n
; ++i
) {
8321 isl_union_set
*dom_i
;
8323 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8324 dom_i
= isl_union_pw_aff_domain(upa
);
8325 dom
= isl_union_set_intersect(dom
, dom_i
);
8328 isl_multi_union_pw_aff_free(mupa
);
8331 isl_multi_union_pw_aff_free(mupa
);
8335 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8336 * In particular, the spaces have been aligned.
8337 * The result is defined over the shared domain of the elements of "mupa"
8339 * We first extract the parametric constant part of "aff" and
8340 * define that over the shared domain.
8341 * Then we iterate over all input dimensions of "aff" and add the corresponding
8342 * multiples of the elements of "mupa".
8343 * Finally, we consider the integer divisions, calling the function
8344 * recursively to obtain an isl_union_pw_aff corresponding to the
8345 * integer division argument.
8347 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8348 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8351 isl_union_pw_aff
*upa
;
8352 isl_union_set
*uset
;
8356 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8357 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8359 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8360 cst
= isl_aff_copy(aff
);
8361 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8362 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8363 cst
= isl_aff_project_domain_on_params(cst
);
8364 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8366 for (i
= 0; i
< n_in
; ++i
) {
8367 isl_union_pw_aff
*upa_i
;
8369 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8371 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8372 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8373 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8374 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8377 for (i
= 0; i
< n_div
; ++i
) {
8379 isl_union_pw_aff
*upa_i
;
8381 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8383 div
= isl_aff_get_div(aff
, i
);
8384 upa_i
= multi_union_pw_aff_apply_aff(
8385 isl_multi_union_pw_aff_copy(mupa
), div
);
8386 upa_i
= isl_union_pw_aff_floor(upa_i
);
8387 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8388 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8389 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8392 isl_multi_union_pw_aff_free(mupa
);
8398 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8399 * with the domain of "aff".
8400 * Furthermore, the dimension of this space needs to be greater than zero.
8401 * The result is defined over the shared domain of the elements of "mupa"
8403 * We perform these checks and then hand over control to
8404 * multi_union_pw_aff_apply_aff.
8406 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8407 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8409 isl_space
*space1
, *space2
;
8412 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8413 isl_aff_get_space(aff
));
8414 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8418 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8419 space2
= isl_aff_get_domain_space(aff
);
8420 equal
= isl_space_is_equal(space1
, space2
);
8421 isl_space_free(space1
);
8422 isl_space_free(space2
);
8426 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8427 "spaces don't match", goto error
);
8428 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8429 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8430 "cannot determine domains", goto error
);
8432 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8434 isl_multi_union_pw_aff_free(mupa
);
8439 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8440 * with the domain of "ma".
8441 * Furthermore, the dimension of this space needs to be greater than zero,
8442 * unless the dimension of the target space of "ma" is also zero.
8443 * The result is defined over the shared domain of the elements of "mupa"
8445 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8446 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8448 isl_space
*space1
, *space2
;
8449 isl_multi_union_pw_aff
*res
;
8453 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8454 isl_multi_aff_get_space(ma
));
8455 ma
= isl_multi_aff_align_params(ma
,
8456 isl_multi_union_pw_aff_get_space(mupa
));
8460 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8461 space2
= isl_multi_aff_get_domain_space(ma
);
8462 equal
= isl_space_is_equal(space1
, space2
);
8463 isl_space_free(space1
);
8464 isl_space_free(space2
);
8468 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8469 "spaces don't match", goto error
);
8470 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8471 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8472 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8473 "cannot determine domains", goto error
);
8475 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8476 res
= isl_multi_union_pw_aff_alloc(space1
);
8478 for (i
= 0; i
< n_out
; ++i
) {
8480 isl_union_pw_aff
*upa
;
8482 aff
= isl_multi_aff_get_aff(ma
, i
);
8483 upa
= multi_union_pw_aff_apply_aff(
8484 isl_multi_union_pw_aff_copy(mupa
), aff
);
8485 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8488 isl_multi_aff_free(ma
);
8489 isl_multi_union_pw_aff_free(mupa
);
8492 isl_multi_union_pw_aff_free(mupa
);
8493 isl_multi_aff_free(ma
);
8497 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8498 * with the domain of "pa".
8499 * Furthermore, the dimension of this space needs to be greater than zero.
8500 * The result is defined over the shared domain of the elements of "mupa"
8502 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8503 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8507 isl_space
*space
, *space2
;
8508 isl_union_pw_aff
*upa
;
8510 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8511 isl_pw_aff_get_space(pa
));
8512 pa
= isl_pw_aff_align_params(pa
,
8513 isl_multi_union_pw_aff_get_space(mupa
));
8517 space
= isl_multi_union_pw_aff_get_space(mupa
);
8518 space2
= isl_pw_aff_get_domain_space(pa
);
8519 equal
= isl_space_is_equal(space
, space2
);
8520 isl_space_free(space
);
8521 isl_space_free(space2
);
8525 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8526 "spaces don't match", goto error
);
8527 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8528 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8529 "cannot determine domains", goto error
);
8531 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8532 upa
= isl_union_pw_aff_empty(space
);
8534 for (i
= 0; i
< pa
->n
; ++i
) {
8537 isl_multi_union_pw_aff
*mupa_i
;
8538 isl_union_pw_aff
*upa_i
;
8540 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8541 domain
= isl_set_copy(pa
->p
[i
].set
);
8542 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8543 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8544 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8545 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8548 isl_multi_union_pw_aff_free(mupa
);
8549 isl_pw_aff_free(pa
);
8552 isl_multi_union_pw_aff_free(mupa
);
8553 isl_pw_aff_free(pa
);
8557 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8558 * with the domain of "pma".
8559 * Furthermore, the dimension of this space needs to be greater than zero,
8560 * unless the dimension of the target space of "pma" is also zero.
8561 * The result is defined over the shared domain of the elements of "mupa"
8563 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8564 __isl_take isl_multi_union_pw_aff
*mupa
,
8565 __isl_take isl_pw_multi_aff
*pma
)
8567 isl_space
*space1
, *space2
;
8568 isl_multi_union_pw_aff
*res
;
8572 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8573 isl_pw_multi_aff_get_space(pma
));
8574 pma
= isl_pw_multi_aff_align_params(pma
,
8575 isl_multi_union_pw_aff_get_space(mupa
));
8579 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8580 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8581 equal
= isl_space_is_equal(space1
, space2
);
8582 isl_space_free(space1
);
8583 isl_space_free(space2
);
8587 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8588 "spaces don't match", goto error
);
8589 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8590 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8591 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8592 "cannot determine domains", goto error
);
8594 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8595 res
= isl_multi_union_pw_aff_alloc(space1
);
8597 for (i
= 0; i
< n_out
; ++i
) {
8599 isl_union_pw_aff
*upa
;
8601 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8602 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8603 isl_multi_union_pw_aff_copy(mupa
), pa
);
8604 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8607 isl_pw_multi_aff_free(pma
);
8608 isl_multi_union_pw_aff_free(mupa
);
8611 isl_multi_union_pw_aff_free(mupa
);
8612 isl_pw_multi_aff_free(pma
);
8616 /* Compute the pullback of "mupa" by the function represented by "upma".
8617 * In other words, plug in "upma" in "mupa". The result contains
8618 * expressions defined over the domain space of "upma".
8620 * Run over all elements of "mupa" and plug in "upma" in each of them.
8622 __isl_give isl_multi_union_pw_aff
*
8623 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8624 __isl_take isl_multi_union_pw_aff
*mupa
,
8625 __isl_take isl_union_pw_multi_aff
*upma
)
8629 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8630 isl_union_pw_multi_aff_get_space(upma
));
8631 upma
= isl_union_pw_multi_aff_align_params(upma
,
8632 isl_multi_union_pw_aff_get_space(mupa
));
8636 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8637 for (i
= 0; i
< n
; ++i
) {
8638 isl_union_pw_aff
*upa
;
8640 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8641 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8642 isl_union_pw_multi_aff_copy(upma
));
8643 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8646 isl_union_pw_multi_aff_free(upma
);
8649 isl_multi_union_pw_aff_free(mupa
);
8650 isl_union_pw_multi_aff_free(upma
);
8654 /* Extract the sequence of elements in "mupa" with domain space "space"
8655 * (ignoring parameters).
8657 * For the elements of "mupa" that are not defined on the specified space,
8658 * the corresponding element in the result is empty.
8660 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8661 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8664 isl_space
*space_mpa
= NULL
;
8665 isl_multi_pw_aff
*mpa
;
8667 if (!mupa
|| !space
)
8670 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8671 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8672 space
= isl_space_drop_dims(space
, isl_dim_param
,
8673 0, isl_space_dim(space
, isl_dim_param
));
8674 space
= isl_space_align_params(space
,
8675 isl_space_copy(space_mpa
));
8679 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8681 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8683 space
= isl_space_from_domain(space
);
8684 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8685 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8686 for (i
= 0; i
< n
; ++i
) {
8687 isl_union_pw_aff
*upa
;
8690 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8691 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8692 isl_space_copy(space
));
8693 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8694 isl_union_pw_aff_free(upa
);
8697 isl_space_free(space
);
8700 isl_space_free(space_mpa
);
8701 isl_space_free(space
);