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 /* Return a hash value that digests "aff".
280 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
282 uint32_t hash
, ls_hash
, v_hash
;
287 hash
= isl_hash_init();
288 ls_hash
= isl_local_space_get_hash(aff
->ls
);
289 isl_hash_hash(hash
, ls_hash
);
290 v_hash
= isl_vec_get_hash(aff
->v
);
291 isl_hash_hash(hash
, v_hash
);
296 /* Externally, an isl_aff has a map space, but internally, the
297 * ls field corresponds to the domain of that space.
299 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
303 if (type
== isl_dim_out
)
305 if (type
== isl_dim_in
)
307 return isl_local_space_dim(aff
->ls
, type
);
310 /* Return the position of the dimension of the given type and name
312 * Return -1 if no such dimension can be found.
314 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
319 if (type
== isl_dim_out
)
321 if (type
== isl_dim_in
)
323 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
326 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
328 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
331 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
336 space
= isl_local_space_get_space(aff
->ls
);
337 space
= isl_space_from_domain(space
);
338 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
342 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
343 __isl_keep isl_aff
*aff
)
345 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
348 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
353 ls
= isl_local_space_copy(aff
->ls
);
354 ls
= isl_local_space_from_domain(ls
);
355 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
359 /* Externally, an isl_aff has a map space, but internally, the
360 * ls field corresponds to the domain of that space.
362 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
363 enum isl_dim_type type
, unsigned pos
)
367 if (type
== isl_dim_out
)
369 if (type
== isl_dim_in
)
371 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
374 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
375 __isl_take isl_space
*dim
)
377 aff
= isl_aff_cow(aff
);
381 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
383 return isl_aff_free(aff
);
392 /* Reset the space of "aff". This function is called from isl_pw_templ.c
393 * and doesn't know if the space of an element object is represented
394 * directly or through its domain. It therefore passes along both.
396 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
397 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
399 isl_space_free(space
);
400 return isl_aff_reset_domain_space(aff
, domain
);
403 /* Reorder the coefficients of the affine expression based
404 * on the given reodering.
405 * The reordering r is assumed to have been extended with the local
408 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
409 __isl_take isl_reordering
*r
, int n_div
)
417 res
= isl_vec_alloc(vec
->ctx
,
418 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
421 isl_seq_cpy(res
->el
, vec
->el
, 2);
422 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
423 for (i
= 0; i
< r
->len
; ++i
)
424 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
426 isl_reordering_free(r
);
431 isl_reordering_free(r
);
435 /* Reorder the dimensions of the domain of "aff" according
436 * to the given reordering.
438 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
439 __isl_take isl_reordering
*r
)
441 aff
= isl_aff_cow(aff
);
445 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
446 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
447 aff
->ls
->div
->n_row
);
448 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
450 if (!aff
->v
|| !aff
->ls
)
451 return isl_aff_free(aff
);
456 isl_reordering_free(r
);
460 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
461 __isl_take isl_space
*model
)
466 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
467 model
, isl_dim_param
)) {
470 model
= isl_space_drop_dims(model
, isl_dim_in
,
471 0, isl_space_dim(model
, isl_dim_in
));
472 model
= isl_space_drop_dims(model
, isl_dim_out
,
473 0, isl_space_dim(model
, isl_dim_out
));
474 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
475 exp
= isl_reordering_extend_space(exp
,
476 isl_aff_get_domain_space(aff
));
477 aff
= isl_aff_realign_domain(aff
, exp
);
480 isl_space_free(model
);
483 isl_space_free(model
);
488 /* Is "aff" obviously equal to zero?
490 * If the denominator is zero, then "aff" is not equal to zero.
492 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
495 return isl_bool_error
;
497 if (isl_int_is_zero(aff
->v
->el
[0]))
498 return isl_bool_false
;
499 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
502 /* Does "aff" represent NaN?
504 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
507 return isl_bool_error
;
509 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
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 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
541 return isl_stat_error
;
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 isl_stat_error
);
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 * reduce_div_coefficients performs the same normalization.
1514 * As a special case, floor(NaN) = NaN.
1516 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1526 if (isl_aff_is_nan(aff
))
1528 if (isl_int_is_one(aff
->v
->el
[0]))
1531 aff
= isl_aff_cow(aff
);
1535 aff
->v
= isl_vec_cow(aff
->v
);
1537 return isl_aff_free(aff
);
1539 if (isl_aff_is_cst(aff
)) {
1540 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1541 isl_int_set_si(aff
->v
->el
[0], 1);
1545 div
= isl_vec_copy(aff
->v
);
1546 div
= isl_vec_cow(div
);
1548 return isl_aff_free(aff
);
1550 ctx
= isl_aff_get_ctx(aff
);
1551 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1552 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1553 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1554 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1555 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1556 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1557 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1561 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1563 return isl_aff_free(aff
);
1565 size
= aff
->v
->size
;
1566 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1568 return isl_aff_free(aff
);
1569 isl_int_set_si(aff
->v
->el
[0], 1);
1570 isl_int_set_si(aff
->v
->el
[size
], 1);
1572 aff
= isl_aff_normalize(aff
);
1579 * aff mod m = aff - m * floor(aff/m)
1581 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1585 res
= isl_aff_copy(aff
);
1586 aff
= isl_aff_scale_down(aff
, m
);
1587 aff
= isl_aff_floor(aff
);
1588 aff
= isl_aff_scale(aff
, m
);
1589 res
= isl_aff_sub(res
, aff
);
1596 * aff mod m = aff - m * floor(aff/m)
1598 * with m an integer value.
1600 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1601 __isl_take isl_val
*m
)
1608 if (!isl_val_is_int(m
))
1609 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1610 "expecting integer modulo", goto error
);
1612 res
= isl_aff_copy(aff
);
1613 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1614 aff
= isl_aff_floor(aff
);
1615 aff
= isl_aff_scale_val(aff
, m
);
1616 res
= isl_aff_sub(res
, aff
);
1627 * pwaff mod m = pwaff - m * floor(pwaff/m)
1629 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1633 res
= isl_pw_aff_copy(pwaff
);
1634 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1635 pwaff
= isl_pw_aff_floor(pwaff
);
1636 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1637 res
= isl_pw_aff_sub(res
, pwaff
);
1644 * pa mod m = pa - m * floor(pa/m)
1646 * with m an integer value.
1648 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1649 __isl_take isl_val
*m
)
1653 if (!isl_val_is_int(m
))
1654 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1655 "expecting integer modulo", goto error
);
1656 pa
= isl_pw_aff_mod(pa
, m
->n
);
1660 isl_pw_aff_free(pa
);
1665 /* Given f, return ceil(f).
1666 * If f is an integer expression, then just return f.
1667 * Otherwise, let f be the expression
1673 * floor((e + m - 1)/m)
1675 * As a special case, ceil(NaN) = NaN.
1677 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1682 if (isl_aff_is_nan(aff
))
1684 if (isl_int_is_one(aff
->v
->el
[0]))
1687 aff
= isl_aff_cow(aff
);
1690 aff
->v
= isl_vec_cow(aff
->v
);
1692 return isl_aff_free(aff
);
1694 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1695 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1696 aff
= isl_aff_floor(aff
);
1701 /* Apply the expansion computed by isl_merge_divs.
1702 * The expansion itself is given by "exp" while the resulting
1703 * list of divs is given by "div".
1705 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1706 __isl_take isl_mat
*div
, int *exp
)
1712 aff
= isl_aff_cow(aff
);
1716 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1717 new_n_div
= isl_mat_rows(div
);
1718 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1720 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1721 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1722 if (!aff
->v
|| !aff
->ls
)
1723 return isl_aff_free(aff
);
1731 /* Add two affine expressions that live in the same local space.
1733 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1734 __isl_take isl_aff
*aff2
)
1738 aff1
= isl_aff_cow(aff1
);
1742 aff1
->v
= isl_vec_cow(aff1
->v
);
1748 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1749 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1750 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1751 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1752 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1753 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1754 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1766 /* Return the sum of "aff1" and "aff2".
1768 * If either of the two is NaN, then the result is NaN.
1770 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1771 __isl_take isl_aff
*aff2
)
1782 ctx
= isl_aff_get_ctx(aff1
);
1783 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1784 isl_die(ctx
, isl_error_invalid
,
1785 "spaces don't match", goto error
);
1787 if (isl_aff_is_nan(aff1
)) {
1791 if (isl_aff_is_nan(aff2
)) {
1796 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1797 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1798 if (n_div1
== 0 && n_div2
== 0)
1799 return add_expanded(aff1
, aff2
);
1801 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1802 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1803 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1806 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1807 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1808 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1812 return add_expanded(aff1
, aff2
);
1821 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1822 __isl_take isl_aff
*aff2
)
1824 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1827 /* Return the result of scaling "aff" by a factor of "f".
1829 * As a special case, f * NaN = NaN.
1831 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1837 if (isl_aff_is_nan(aff
))
1840 if (isl_int_is_one(f
))
1843 aff
= isl_aff_cow(aff
);
1846 aff
->v
= isl_vec_cow(aff
->v
);
1848 return isl_aff_free(aff
);
1850 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1851 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1856 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1857 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1858 isl_int_divexact(gcd
, f
, gcd
);
1859 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1865 /* Multiple "aff" by "v".
1867 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1868 __isl_take isl_val
*v
)
1873 if (isl_val_is_one(v
)) {
1878 if (!isl_val_is_rat(v
))
1879 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1880 "expecting rational factor", goto error
);
1882 aff
= isl_aff_scale(aff
, v
->n
);
1883 aff
= isl_aff_scale_down(aff
, v
->d
);
1893 /* Return the result of scaling "aff" down by a factor of "f".
1895 * As a special case, NaN/f = NaN.
1897 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1903 if (isl_aff_is_nan(aff
))
1906 if (isl_int_is_one(f
))
1909 aff
= isl_aff_cow(aff
);
1913 if (isl_int_is_zero(f
))
1914 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1915 "cannot scale down by zero", return isl_aff_free(aff
));
1917 aff
->v
= isl_vec_cow(aff
->v
);
1919 return isl_aff_free(aff
);
1922 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1923 isl_int_gcd(gcd
, gcd
, f
);
1924 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1925 isl_int_divexact(gcd
, f
, gcd
);
1926 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1932 /* Divide "aff" by "v".
1934 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1935 __isl_take isl_val
*v
)
1940 if (isl_val_is_one(v
)) {
1945 if (!isl_val_is_rat(v
))
1946 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1947 "expecting rational factor", goto error
);
1948 if (!isl_val_is_pos(v
))
1949 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1950 "factor needs to be positive", goto error
);
1952 aff
= isl_aff_scale(aff
, v
->d
);
1953 aff
= isl_aff_scale_down(aff
, v
->n
);
1963 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1971 isl_int_set_ui(v
, f
);
1972 aff
= isl_aff_scale_down(aff
, v
);
1978 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1979 enum isl_dim_type type
, unsigned pos
, const char *s
)
1981 aff
= isl_aff_cow(aff
);
1984 if (type
== isl_dim_out
)
1985 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1986 "cannot set name of output/set dimension",
1987 return isl_aff_free(aff
));
1988 if (type
== isl_dim_in
)
1990 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1992 return isl_aff_free(aff
);
1997 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1998 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2000 aff
= isl_aff_cow(aff
);
2003 if (type
== isl_dim_out
)
2004 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2005 "cannot set name of output/set dimension",
2007 if (type
== isl_dim_in
)
2009 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2011 return isl_aff_free(aff
);
2020 /* Replace the identifier of the input tuple of "aff" by "id".
2021 * type is currently required to be equal to isl_dim_in
2023 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2024 enum isl_dim_type type
, __isl_take isl_id
*id
)
2026 aff
= isl_aff_cow(aff
);
2029 if (type
!= isl_dim_out
)
2030 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2031 "cannot only set id of input tuple", goto error
);
2032 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2034 return isl_aff_free(aff
);
2043 /* Exploit the equalities in "eq" to simplify the affine expression
2044 * and the expressions of the integer divisions in the local space.
2045 * The integer divisions in this local space are assumed to appear
2046 * as regular dimensions in "eq".
2048 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2049 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2057 if (eq
->n_eq
== 0) {
2058 isl_basic_set_free(eq
);
2062 aff
= isl_aff_cow(aff
);
2066 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2067 isl_basic_set_copy(eq
));
2068 aff
->v
= isl_vec_cow(aff
->v
);
2069 if (!aff
->ls
|| !aff
->v
)
2072 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2074 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2075 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2076 if (j
< 0 || j
== 0 || j
>= total
)
2079 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2083 isl_basic_set_free(eq
);
2084 aff
= isl_aff_normalize(aff
);
2087 isl_basic_set_free(eq
);
2092 /* Exploit the equalities in "eq" to simplify the affine expression
2093 * and the expressions of the integer divisions in the local space.
2095 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2096 __isl_take isl_basic_set
*eq
)
2102 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2104 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2105 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2107 isl_basic_set_free(eq
);
2112 /* Look for equalities among the variables shared by context and aff
2113 * and the integer divisions of aff, if any.
2114 * The equalities are then used to eliminate coefficients and/or integer
2115 * divisions from aff.
2117 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2118 __isl_take isl_set
*context
)
2120 isl_basic_set
*hull
;
2125 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2127 isl_basic_set
*bset
;
2128 isl_local_space
*ls
;
2129 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2130 ls
= isl_aff_get_domain_local_space(aff
);
2131 bset
= isl_basic_set_from_local_space(ls
);
2132 bset
= isl_basic_set_lift(bset
);
2133 bset
= isl_basic_set_flatten(bset
);
2134 context
= isl_set_intersect(context
,
2135 isl_set_from_basic_set(bset
));
2138 hull
= isl_set_affine_hull(context
);
2139 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2142 isl_set_free(context
);
2146 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2147 __isl_take isl_set
*context
)
2149 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2150 dom_context
= isl_set_intersect_params(dom_context
, context
);
2151 return isl_aff_gist(aff
, dom_context
);
2154 /* Return a basic set containing those elements in the space
2155 * of aff where it is positive. "rational" should not be set.
2157 * If "aff" is NaN, then it is not positive.
2159 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2162 isl_constraint
*ineq
;
2163 isl_basic_set
*bset
;
2168 if (isl_aff_is_nan(aff
)) {
2169 isl_space
*space
= isl_aff_get_domain_space(aff
);
2171 return isl_basic_set_empty(space
);
2174 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2175 "rational sets not supported", goto error
);
2177 ineq
= isl_inequality_from_aff(aff
);
2178 c
= isl_constraint_get_constant_val(ineq
);
2179 c
= isl_val_sub_ui(c
, 1);
2180 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2182 bset
= isl_basic_set_from_constraint(ineq
);
2183 bset
= isl_basic_set_simplify(bset
);
2190 /* Return a basic set containing those elements in the space
2191 * of aff where it is non-negative.
2192 * If "rational" is set, then return a rational basic set.
2194 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2196 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2197 __isl_take isl_aff
*aff
, int rational
)
2199 isl_constraint
*ineq
;
2200 isl_basic_set
*bset
;
2204 if (isl_aff_is_nan(aff
)) {
2205 isl_space
*space
= isl_aff_get_domain_space(aff
);
2207 return isl_basic_set_empty(space
);
2210 ineq
= isl_inequality_from_aff(aff
);
2212 bset
= isl_basic_set_from_constraint(ineq
);
2214 bset
= isl_basic_set_set_rational(bset
);
2215 bset
= isl_basic_set_simplify(bset
);
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is non-negative.
2222 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2224 return aff_nonneg_basic_set(aff
, 0);
2227 /* Return a basic set containing those elements in the domain space
2228 * of aff where it is negative.
2230 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2232 aff
= isl_aff_neg(aff
);
2233 aff
= isl_aff_add_constant_num_si(aff
, -1);
2234 return isl_aff_nonneg_basic_set(aff
);
2237 /* Return a basic set containing those elements in the space
2238 * of aff where it is zero.
2239 * If "rational" is set, then return a rational basic set.
2241 * If "aff" is NaN, then it is not zero.
2243 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2246 isl_constraint
*ineq
;
2247 isl_basic_set
*bset
;
2251 if (isl_aff_is_nan(aff
)) {
2252 isl_space
*space
= isl_aff_get_domain_space(aff
);
2254 return isl_basic_set_empty(space
);
2257 ineq
= isl_equality_from_aff(aff
);
2259 bset
= isl_basic_set_from_constraint(ineq
);
2261 bset
= isl_basic_set_set_rational(bset
);
2262 bset
= isl_basic_set_simplify(bset
);
2266 /* Return a basic set containing those elements in the space
2267 * of aff where it is zero.
2269 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2271 return aff_zero_basic_set(aff
, 0);
2274 /* Return a basic set containing those elements in the shared space
2275 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2277 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2278 __isl_take isl_aff
*aff2
)
2280 aff1
= isl_aff_sub(aff1
, aff2
);
2282 return isl_aff_nonneg_basic_set(aff1
);
2285 /* Return a set containing those elements in the shared space
2286 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2288 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2289 __isl_take isl_aff
*aff2
)
2291 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2294 /* Return a basic set containing those elements in the shared space
2295 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2297 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2298 __isl_take isl_aff
*aff2
)
2300 return isl_aff_ge_basic_set(aff2
, aff1
);
2303 /* Return a set containing those elements in the shared space
2304 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2306 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2307 __isl_take isl_aff
*aff2
)
2309 return isl_aff_ge_set(aff2
, aff1
);
2312 /* Return a basic set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 and aff2 are equal.
2315 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2316 __isl_take isl_aff
*aff2
)
2318 aff1
= isl_aff_sub(aff1
, aff2
);
2320 return isl_aff_zero_basic_set(aff1
);
2323 /* Return a set containing those elements in the shared space
2324 * of aff1 and aff2 where aff1 and aff2 are equal.
2326 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2327 __isl_take isl_aff
*aff2
)
2329 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2332 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2333 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2335 aff1
= isl_aff_add(aff1
, aff2
);
2336 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2340 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2348 /* Check whether the given affine expression has non-zero coefficient
2349 * for any dimension in the given range or if any of these dimensions
2350 * appear with non-zero coefficients in any of the integer divisions
2351 * involved in the affine expression.
2353 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2354 enum isl_dim_type type
, unsigned first
, unsigned n
)
2359 isl_bool involves
= isl_bool_false
;
2362 return isl_bool_error
;
2364 return isl_bool_false
;
2366 ctx
= isl_aff_get_ctx(aff
);
2367 if (first
+ n
> isl_aff_dim(aff
, type
))
2368 isl_die(ctx
, isl_error_invalid
,
2369 "range out of bounds", return isl_bool_error
);
2371 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2375 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2376 for (i
= 0; i
< n
; ++i
)
2377 if (active
[first
+ i
]) {
2378 involves
= isl_bool_true
;
2387 return isl_bool_error
;
2390 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2391 enum isl_dim_type type
, unsigned first
, unsigned n
)
2397 if (type
== isl_dim_out
)
2398 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2399 "cannot drop output/set dimension",
2400 return isl_aff_free(aff
));
2401 if (type
== isl_dim_in
)
2403 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2406 ctx
= isl_aff_get_ctx(aff
);
2407 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2408 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2409 return isl_aff_free(aff
));
2411 aff
= isl_aff_cow(aff
);
2415 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2417 return isl_aff_free(aff
);
2419 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2420 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2422 return isl_aff_free(aff
);
2427 /* Project the domain of the affine expression onto its parameter space.
2428 * The affine expression may not involve any of the domain dimensions.
2430 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2436 n
= isl_aff_dim(aff
, isl_dim_in
);
2437 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2439 return isl_aff_free(aff
);
2441 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2442 "affine expression involves some of the domain dimensions",
2443 return isl_aff_free(aff
));
2444 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2445 space
= isl_aff_get_domain_space(aff
);
2446 space
= isl_space_params(space
);
2447 aff
= isl_aff_reset_domain_space(aff
, space
);
2451 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2452 enum isl_dim_type type
, unsigned first
, unsigned n
)
2458 if (type
== isl_dim_out
)
2459 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2460 "cannot insert output/set dimensions",
2461 return isl_aff_free(aff
));
2462 if (type
== isl_dim_in
)
2464 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2467 ctx
= isl_aff_get_ctx(aff
);
2468 if (first
> isl_local_space_dim(aff
->ls
, type
))
2469 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2470 return isl_aff_free(aff
));
2472 aff
= isl_aff_cow(aff
);
2476 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2478 return isl_aff_free(aff
);
2480 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2481 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2483 return isl_aff_free(aff
);
2488 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2489 enum isl_dim_type type
, unsigned n
)
2493 pos
= isl_aff_dim(aff
, type
);
2495 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2498 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2499 enum isl_dim_type type
, unsigned n
)
2503 pos
= isl_pw_aff_dim(pwaff
, type
);
2505 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2508 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2509 * to dimensions of "dst_type" at "dst_pos".
2511 * We only support moving input dimensions to parameters and vice versa.
2513 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2514 enum isl_dim_type dst_type
, unsigned dst_pos
,
2515 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2523 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2524 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2527 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2528 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2529 "cannot move output/set dimension",
2530 return isl_aff_free(aff
));
2531 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2532 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2533 "cannot move divs", return isl_aff_free(aff
));
2534 if (dst_type
== isl_dim_in
)
2535 dst_type
= isl_dim_set
;
2536 if (src_type
== isl_dim_in
)
2537 src_type
= isl_dim_set
;
2539 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2540 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2541 "range out of bounds", return isl_aff_free(aff
));
2542 if (dst_type
== src_type
)
2543 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2544 "moving dims within the same type not supported",
2545 return isl_aff_free(aff
));
2547 aff
= isl_aff_cow(aff
);
2551 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2552 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2553 if (dst_type
> src_type
)
2556 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2557 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2558 src_type
, src_pos
, n
);
2559 if (!aff
->v
|| !aff
->ls
)
2560 return isl_aff_free(aff
);
2562 aff
= sort_divs(aff
);
2567 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2569 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2570 return isl_pw_aff_alloc(dom
, aff
);
2573 #define isl_aff_involves_nan isl_aff_is_nan
2576 #define PW isl_pw_aff
2580 #define EL_IS_ZERO is_empty
2584 #define IS_ZERO is_empty
2587 #undef DEFAULT_IS_ZERO
2588 #define DEFAULT_IS_ZERO 0
2595 #include <isl_pw_templ.c>
2596 #include <isl_pw_hash.c>
2597 #include <isl_pw_union_opt.c>
2600 #define UNION isl_union_pw_aff
2602 #define PART isl_pw_aff
2604 #define PARTS pw_aff
2606 #include <isl_union_single.c>
2607 #include <isl_union_neg.c>
2609 static __isl_give isl_set
*align_params_pw_pw_set_and(
2610 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2611 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2612 __isl_take isl_pw_aff
*pwaff2
))
2614 if (!pwaff1
|| !pwaff2
)
2616 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2617 pwaff2
->dim
, isl_dim_param
))
2618 return fn(pwaff1
, pwaff2
);
2619 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2620 !isl_space_has_named_params(pwaff2
->dim
))
2621 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2622 "unaligned unnamed parameters", goto error
);
2623 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2624 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2625 return fn(pwaff1
, pwaff2
);
2627 isl_pw_aff_free(pwaff1
);
2628 isl_pw_aff_free(pwaff2
);
2632 /* Align the parameters of the to isl_pw_aff arguments and
2633 * then apply a function "fn" on them that returns an isl_map.
2635 static __isl_give isl_map
*align_params_pw_pw_map_and(
2636 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2637 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2638 __isl_take isl_pw_aff
*pa2
))
2642 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2643 return fn(pa1
, pa2
);
2644 if (!isl_space_has_named_params(pa1
->dim
) ||
2645 !isl_space_has_named_params(pa2
->dim
))
2646 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2647 "unaligned unnamed parameters", goto error
);
2648 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2649 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2650 return fn(pa1
, pa2
);
2652 isl_pw_aff_free(pa1
);
2653 isl_pw_aff_free(pa2
);
2657 /* Compute a piecewise quasi-affine expression with a domain that
2658 * is the union of those of pwaff1 and pwaff2 and such that on each
2659 * cell, the quasi-affine expression is the maximum of those of pwaff1
2660 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2661 * cell, then the associated expression is the defined one.
2663 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2664 __isl_take isl_pw_aff
*pwaff2
)
2666 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2669 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2670 __isl_take isl_pw_aff
*pwaff2
)
2672 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2676 /* Compute a piecewise quasi-affine expression with a domain that
2677 * is the union of those of pwaff1 and pwaff2 and such that on each
2678 * cell, the quasi-affine expression is the minimum of those of pwaff1
2679 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2680 * cell, then the associated expression is the defined one.
2682 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2683 __isl_take isl_pw_aff
*pwaff2
)
2685 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2688 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2689 __isl_take isl_pw_aff
*pwaff2
)
2691 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2695 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2696 __isl_take isl_pw_aff
*pwaff2
, int max
)
2699 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2701 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2704 /* Construct a map with as domain the domain of pwaff and
2705 * one-dimensional range corresponding to the affine expressions.
2707 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2716 dim
= isl_pw_aff_get_space(pwaff
);
2717 map
= isl_map_empty(dim
);
2719 for (i
= 0; i
< pwaff
->n
; ++i
) {
2720 isl_basic_map
*bmap
;
2723 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2724 map_i
= isl_map_from_basic_map(bmap
);
2725 map_i
= isl_map_intersect_domain(map_i
,
2726 isl_set_copy(pwaff
->p
[i
].set
));
2727 map
= isl_map_union_disjoint(map
, map_i
);
2730 isl_pw_aff_free(pwaff
);
2735 /* Construct a map with as domain the domain of pwaff and
2736 * one-dimensional range corresponding to the affine expressions.
2738 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2742 if (isl_space_is_set(pwaff
->dim
))
2743 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2744 "space of input is not a map", goto error
);
2745 return map_from_pw_aff(pwaff
);
2747 isl_pw_aff_free(pwaff
);
2751 /* Construct a one-dimensional set with as parameter domain
2752 * the domain of pwaff and the single set dimension
2753 * corresponding to the affine expressions.
2755 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2759 if (!isl_space_is_set(pwaff
->dim
))
2760 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2761 "space of input is not a set", goto error
);
2762 return map_from_pw_aff(pwaff
);
2764 isl_pw_aff_free(pwaff
);
2768 /* Return a set containing those elements in the domain
2769 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2770 * does not satisfy "fn" (if complement is 1).
2772 * The pieces with a NaN never belong to the result since
2773 * NaN does not satisfy any property.
2775 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2776 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2785 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2787 for (i
= 0; i
< pwaff
->n
; ++i
) {
2788 isl_basic_set
*bset
;
2789 isl_set
*set_i
, *locus
;
2792 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2795 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2796 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2797 locus
= isl_set_from_basic_set(bset
);
2798 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2800 set_i
= isl_set_subtract(set_i
, locus
);
2802 set_i
= isl_set_intersect(set_i
, locus
);
2803 set
= isl_set_union_disjoint(set
, set_i
);
2806 isl_pw_aff_free(pwaff
);
2811 /* Return a set containing those elements in the domain
2812 * of "pa" where it is positive.
2814 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2816 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2819 /* Return a set containing those elements in the domain
2820 * of pwaff where it is non-negative.
2822 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2824 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2827 /* Return a set containing those elements in the domain
2828 * of pwaff where it is zero.
2830 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2832 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2835 /* Return a set containing those elements in the domain
2836 * of pwaff where it is not zero.
2838 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2840 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2843 /* Return a set containing those elements in the shared domain
2844 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2846 * We compute the difference on the shared domain and then construct
2847 * the set of values where this difference is non-negative.
2848 * If strict is set, we first subtract 1 from the difference.
2849 * If equal is set, we only return the elements where pwaff1 and pwaff2
2852 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2853 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2855 isl_set
*set1
, *set2
;
2857 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2858 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2859 set1
= isl_set_intersect(set1
, set2
);
2860 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2861 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2862 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2865 isl_space
*dim
= isl_set_get_space(set1
);
2867 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2868 aff
= isl_aff_add_constant_si(aff
, -1);
2869 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2874 return isl_pw_aff_zero_set(pwaff1
);
2875 return isl_pw_aff_nonneg_set(pwaff1
);
2878 /* Return a set containing those elements in the shared domain
2879 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2881 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2882 __isl_take isl_pw_aff
*pwaff2
)
2884 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2887 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2888 __isl_take isl_pw_aff
*pwaff2
)
2890 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2893 /* Return a set containing those elements in the shared domain
2894 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2896 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2897 __isl_take isl_pw_aff
*pwaff2
)
2899 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2902 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2903 __isl_take isl_pw_aff
*pwaff2
)
2905 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2908 /* Return a set containing those elements in the shared domain
2909 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2911 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2912 __isl_take isl_pw_aff
*pwaff2
)
2914 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2917 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2918 __isl_take isl_pw_aff
*pwaff2
)
2920 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2923 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2924 __isl_take isl_pw_aff
*pwaff2
)
2926 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2929 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2930 __isl_take isl_pw_aff
*pwaff2
)
2932 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2935 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2936 * where the function values are ordered in the same way as "order",
2937 * which returns a set in the shared domain of its two arguments.
2938 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2940 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2941 * We first pull back the two functions such that they are defined on
2942 * the domain [A -> B]. Then we apply "order", resulting in a set
2943 * in the space [A -> B]. Finally, we unwrap this set to obtain
2944 * a map in the space A -> B.
2946 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2947 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2948 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2949 __isl_take isl_pw_aff
*pa2
))
2951 isl_space
*space1
, *space2
;
2955 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2956 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2957 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2958 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2959 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2960 ma
= isl_multi_aff_range_map(space1
);
2961 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2962 set
= order(pa1
, pa2
);
2964 return isl_set_unwrap(set
);
2967 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2968 * where the function values are equal.
2969 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2971 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2972 __isl_take isl_pw_aff
*pa2
)
2974 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2977 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2978 * where the function values are equal.
2980 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2981 __isl_take isl_pw_aff
*pa2
)
2983 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
2986 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2987 * where the function value of "pa1" is less than the function value of "pa2".
2988 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2990 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
2991 __isl_take isl_pw_aff
*pa2
)
2993 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
2996 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2997 * where the function value of "pa1" is less than the function value of "pa2".
2999 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3000 __isl_take isl_pw_aff
*pa2
)
3002 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3005 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3006 * where the function value of "pa1" is greater than the function value
3008 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3010 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3011 __isl_take isl_pw_aff
*pa2
)
3013 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3016 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3017 * where the function value of "pa1" is greater than the function value
3020 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3021 __isl_take isl_pw_aff
*pa2
)
3023 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3026 /* Return a set containing those elements in the shared domain
3027 * of the elements of list1 and list2 where each element in list1
3028 * has the relation specified by "fn" with each element in list2.
3030 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3031 __isl_take isl_pw_aff_list
*list2
,
3032 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3033 __isl_take isl_pw_aff
*pwaff2
))
3039 if (!list1
|| !list2
)
3042 ctx
= isl_pw_aff_list_get_ctx(list1
);
3043 if (list1
->n
< 1 || list2
->n
< 1)
3044 isl_die(ctx
, isl_error_invalid
,
3045 "list should contain at least one element", goto error
);
3047 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3048 for (i
= 0; i
< list1
->n
; ++i
)
3049 for (j
= 0; j
< list2
->n
; ++j
) {
3052 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3053 isl_pw_aff_copy(list2
->p
[j
]));
3054 set
= isl_set_intersect(set
, set_ij
);
3057 isl_pw_aff_list_free(list1
);
3058 isl_pw_aff_list_free(list2
);
3061 isl_pw_aff_list_free(list1
);
3062 isl_pw_aff_list_free(list2
);
3066 /* Return a set containing those elements in the shared domain
3067 * of the elements of list1 and list2 where each element in list1
3068 * is equal to each element in list2.
3070 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3071 __isl_take isl_pw_aff_list
*list2
)
3073 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3076 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3077 __isl_take isl_pw_aff_list
*list2
)
3079 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3082 /* Return a set containing those elements in the shared domain
3083 * of the elements of list1 and list2 where each element in list1
3084 * is less than or equal to each element in list2.
3086 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3087 __isl_take isl_pw_aff_list
*list2
)
3089 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3092 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3093 __isl_take isl_pw_aff_list
*list2
)
3095 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3098 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3099 __isl_take isl_pw_aff_list
*list2
)
3101 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3104 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3105 __isl_take isl_pw_aff_list
*list2
)
3107 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3111 /* Return a set containing those elements in the shared domain
3112 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3114 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3115 __isl_take isl_pw_aff
*pwaff2
)
3117 isl_set
*set_lt
, *set_gt
;
3119 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3120 isl_pw_aff_copy(pwaff2
));
3121 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3122 return isl_set_union_disjoint(set_lt
, set_gt
);
3125 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3126 __isl_take isl_pw_aff
*pwaff2
)
3128 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3131 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3136 if (isl_int_is_one(v
))
3138 if (!isl_int_is_pos(v
))
3139 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3140 "factor needs to be positive",
3141 return isl_pw_aff_free(pwaff
));
3142 pwaff
= isl_pw_aff_cow(pwaff
);
3148 for (i
= 0; i
< pwaff
->n
; ++i
) {
3149 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3150 if (!pwaff
->p
[i
].aff
)
3151 return isl_pw_aff_free(pwaff
);
3157 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3161 pwaff
= isl_pw_aff_cow(pwaff
);
3167 for (i
= 0; i
< pwaff
->n
; ++i
) {
3168 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3169 if (!pwaff
->p
[i
].aff
)
3170 return isl_pw_aff_free(pwaff
);
3176 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3180 pwaff
= isl_pw_aff_cow(pwaff
);
3186 for (i
= 0; i
< pwaff
->n
; ++i
) {
3187 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3188 if (!pwaff
->p
[i
].aff
)
3189 return isl_pw_aff_free(pwaff
);
3195 /* Assuming that "cond1" and "cond2" are disjoint,
3196 * return an affine expression that is equal to pwaff1 on cond1
3197 * and to pwaff2 on cond2.
3199 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3200 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3201 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3203 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3204 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3206 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3209 /* Return an affine expression that is equal to pwaff_true for elements
3210 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3212 * That is, return cond ? pwaff_true : pwaff_false;
3214 * If "cond" involves and NaN, then we conservatively return a NaN
3215 * on its entire domain. In principle, we could consider the pieces
3216 * where it is NaN separately from those where it is not.
3218 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3219 * then only use the domain of "cond" to restrict the domain.
3221 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3222 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3224 isl_set
*cond_true
, *cond_false
;
3229 if (isl_pw_aff_involves_nan(cond
)) {
3230 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3231 isl_local_space
*ls
= isl_local_space_from_space(space
);
3232 isl_pw_aff_free(cond
);
3233 isl_pw_aff_free(pwaff_true
);
3234 isl_pw_aff_free(pwaff_false
);
3235 return isl_pw_aff_nan_on_domain(ls
);
3238 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3239 isl_pw_aff_get_space(pwaff_false
));
3240 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3241 isl_pw_aff_get_space(pwaff_true
));
3242 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3248 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3249 isl_pw_aff_free(pwaff_false
);
3250 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3253 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3254 cond_false
= isl_pw_aff_zero_set(cond
);
3255 return isl_pw_aff_select(cond_true
, pwaff_true
,
3256 cond_false
, pwaff_false
);
3258 isl_pw_aff_free(cond
);
3259 isl_pw_aff_free(pwaff_true
);
3260 isl_pw_aff_free(pwaff_false
);
3264 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3267 return isl_bool_error
;
3269 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3272 /* Check whether pwaff is a piecewise constant.
3274 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3279 return isl_bool_error
;
3281 for (i
= 0; i
< pwaff
->n
; ++i
) {
3282 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3283 if (is_cst
< 0 || !is_cst
)
3287 return isl_bool_true
;
3290 /* Are all elements of "mpa" piecewise constants?
3292 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3297 return isl_bool_error
;
3299 for (i
= 0; i
< mpa
->n
; ++i
) {
3300 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3301 if (is_cst
< 0 || !is_cst
)
3305 return isl_bool_true
;
3308 /* Return the product of "aff1" and "aff2".
3310 * If either of the two is NaN, then the result is NaN.
3312 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3314 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3315 __isl_take isl_aff
*aff2
)
3320 if (isl_aff_is_nan(aff1
)) {
3324 if (isl_aff_is_nan(aff2
)) {
3329 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3330 return isl_aff_mul(aff2
, aff1
);
3332 if (!isl_aff_is_cst(aff2
))
3333 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3334 "at least one affine expression should be constant",
3337 aff1
= isl_aff_cow(aff1
);
3341 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3342 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3352 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3354 * If either of the two is NaN, then the result is NaN.
3356 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3357 __isl_take isl_aff
*aff2
)
3365 if (isl_aff_is_nan(aff1
)) {
3369 if (isl_aff_is_nan(aff2
)) {
3374 is_cst
= isl_aff_is_cst(aff2
);
3378 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3379 "second argument should be a constant", goto error
);
3384 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3386 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3387 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3390 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3391 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3394 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3395 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3406 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3407 __isl_take isl_pw_aff
*pwaff2
)
3409 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3412 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3413 __isl_take isl_pw_aff
*pwaff2
)
3415 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3418 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3419 __isl_take isl_pw_aff
*pwaff2
)
3421 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3424 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3425 __isl_take isl_pw_aff
*pwaff2
)
3427 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3430 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3431 __isl_take isl_pw_aff
*pwaff2
)
3433 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3436 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3437 __isl_take isl_pw_aff
*pa2
)
3439 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3442 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3444 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3445 __isl_take isl_pw_aff
*pa2
)
3449 is_cst
= isl_pw_aff_is_cst(pa2
);
3453 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3454 "second argument should be a piecewise constant",
3456 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3458 isl_pw_aff_free(pa1
);
3459 isl_pw_aff_free(pa2
);
3463 /* Compute the quotient of the integer division of "pa1" by "pa2"
3464 * with rounding towards zero.
3465 * "pa2" is assumed to be a piecewise constant.
3467 * In particular, return
3469 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3472 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3473 __isl_take isl_pw_aff
*pa2
)
3479 is_cst
= isl_pw_aff_is_cst(pa2
);
3483 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3484 "second argument should be a piecewise constant",
3487 pa1
= isl_pw_aff_div(pa1
, pa2
);
3489 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3490 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3491 c
= isl_pw_aff_ceil(pa1
);
3492 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3494 isl_pw_aff_free(pa1
);
3495 isl_pw_aff_free(pa2
);
3499 /* Compute the remainder of the integer division of "pa1" by "pa2"
3500 * with rounding towards zero.
3501 * "pa2" is assumed to be a piecewise constant.
3503 * In particular, return
3505 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3508 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3509 __isl_take isl_pw_aff
*pa2
)
3514 is_cst
= isl_pw_aff_is_cst(pa2
);
3518 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3519 "second argument should be a piecewise constant",
3521 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3522 res
= isl_pw_aff_mul(pa2
, res
);
3523 res
= isl_pw_aff_sub(pa1
, res
);
3526 isl_pw_aff_free(pa1
);
3527 isl_pw_aff_free(pa2
);
3531 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3532 __isl_take isl_pw_aff
*pwaff2
)
3537 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3538 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3539 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3540 isl_pw_aff_copy(pwaff2
));
3541 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3542 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3545 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3546 __isl_take isl_pw_aff
*pwaff2
)
3548 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3551 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3552 __isl_take isl_pw_aff
*pwaff2
)
3557 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3558 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3559 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3560 isl_pw_aff_copy(pwaff2
));
3561 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3562 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3565 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3566 __isl_take isl_pw_aff
*pwaff2
)
3568 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3571 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3572 __isl_take isl_pw_aff_list
*list
,
3573 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3574 __isl_take isl_pw_aff
*pwaff2
))
3583 ctx
= isl_pw_aff_list_get_ctx(list
);
3585 isl_die(ctx
, isl_error_invalid
,
3586 "list should contain at least one element", goto error
);
3588 res
= isl_pw_aff_copy(list
->p
[0]);
3589 for (i
= 1; i
< list
->n
; ++i
)
3590 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3592 isl_pw_aff_list_free(list
);
3595 isl_pw_aff_list_free(list
);
3599 /* Return an isl_pw_aff that maps each element in the intersection of the
3600 * domains of the elements of list to the minimal corresponding affine
3603 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3605 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3608 /* Return an isl_pw_aff that maps each element in the intersection of the
3609 * domains of the elements of list to the maximal corresponding affine
3612 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3614 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3617 /* Mark the domains of "pwaff" as rational.
3619 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3623 pwaff
= isl_pw_aff_cow(pwaff
);
3629 for (i
= 0; i
< pwaff
->n
; ++i
) {
3630 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3631 if (!pwaff
->p
[i
].set
)
3632 return isl_pw_aff_free(pwaff
);
3638 /* Mark the domains of the elements of "list" as rational.
3640 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3641 __isl_take isl_pw_aff_list
*list
)
3651 for (i
= 0; i
< n
; ++i
) {
3654 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3655 pa
= isl_pw_aff_set_rational(pa
);
3656 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3662 /* Do the parameters of "aff" match those of "space"?
3664 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3665 __isl_keep isl_space
*space
)
3667 isl_space
*aff_space
;
3671 return isl_bool_error
;
3673 aff_space
= isl_aff_get_domain_space(aff
);
3675 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3677 isl_space_free(aff_space
);
3681 /* Check that the domain space of "aff" matches "space".
3683 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3684 __isl_keep isl_space
*space
)
3686 isl_space
*aff_space
;
3690 return isl_stat_error
;
3692 aff_space
= isl_aff_get_domain_space(aff
);
3694 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3698 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3699 "parameters don't match", goto error
);
3700 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3701 aff_space
, isl_dim_set
);
3705 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3706 "domains don't match", goto error
);
3707 isl_space_free(aff_space
);
3710 isl_space_free(aff_space
);
3711 return isl_stat_error
;
3720 #include <isl_multi_templ.c>
3721 #include <isl_multi_apply_set.c>
3722 #include <isl_multi_cmp.c>
3723 #include <isl_multi_floor.c>
3724 #include <isl_multi_gist.c>
3728 /* Remove any internal structure of the domain of "ma".
3729 * If there is any such internal structure in the input,
3730 * then the name of the corresponding space is also removed.
3732 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3733 __isl_take isl_multi_aff
*ma
)
3740 if (!ma
->space
->nested
[0])
3743 space
= isl_multi_aff_get_space(ma
);
3744 space
= isl_space_flatten_domain(space
);
3745 ma
= isl_multi_aff_reset_space(ma
, space
);
3750 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3751 * of the space to its domain.
3753 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3756 isl_local_space
*ls
;
3761 if (!isl_space_is_map(space
))
3762 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3763 "not a map space", goto error
);
3765 n_in
= isl_space_dim(space
, isl_dim_in
);
3766 space
= isl_space_domain_map(space
);
3768 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3770 isl_space_free(space
);
3774 space
= isl_space_domain(space
);
3775 ls
= isl_local_space_from_space(space
);
3776 for (i
= 0; i
< n_in
; ++i
) {
3779 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3781 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3783 isl_local_space_free(ls
);
3786 isl_space_free(space
);
3790 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3791 * of the space to its range.
3793 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3796 isl_local_space
*ls
;
3801 if (!isl_space_is_map(space
))
3802 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3803 "not a map space", goto error
);
3805 n_in
= isl_space_dim(space
, isl_dim_in
);
3806 n_out
= isl_space_dim(space
, isl_dim_out
);
3807 space
= isl_space_range_map(space
);
3809 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3811 isl_space_free(space
);
3815 space
= isl_space_domain(space
);
3816 ls
= isl_local_space_from_space(space
);
3817 for (i
= 0; i
< n_out
; ++i
) {
3820 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3821 isl_dim_set
, n_in
+ i
);
3822 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3824 isl_local_space_free(ls
);
3827 isl_space_free(space
);
3831 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3832 * of the space to its range.
3834 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3835 __isl_take isl_space
*space
)
3837 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3840 /* Given the space of a set and a range of set dimensions,
3841 * construct an isl_multi_aff that projects out those dimensions.
3843 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3844 __isl_take isl_space
*space
, enum isl_dim_type type
,
3845 unsigned first
, unsigned n
)
3848 isl_local_space
*ls
;
3853 if (!isl_space_is_set(space
))
3854 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3855 "expecting set space", goto error
);
3856 if (type
!= isl_dim_set
)
3857 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3858 "only set dimensions can be projected out", goto error
);
3860 dim
= isl_space_dim(space
, isl_dim_set
);
3861 if (first
+ n
> dim
)
3862 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3863 "range out of bounds", goto error
);
3865 space
= isl_space_from_domain(space
);
3866 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3869 return isl_multi_aff_alloc(space
);
3871 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3872 space
= isl_space_domain(space
);
3873 ls
= isl_local_space_from_space(space
);
3875 for (i
= 0; i
< first
; ++i
) {
3878 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3880 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3883 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3886 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3887 isl_dim_set
, first
+ n
+ i
);
3888 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3891 isl_local_space_free(ls
);
3894 isl_space_free(space
);
3898 /* Given the space of a set and a range of set dimensions,
3899 * construct an isl_pw_multi_aff that projects out those dimensions.
3901 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3902 __isl_take isl_space
*space
, enum isl_dim_type type
,
3903 unsigned first
, unsigned n
)
3907 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3908 return isl_pw_multi_aff_from_multi_aff(ma
);
3911 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3914 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3915 __isl_take isl_multi_aff
*ma
)
3917 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3918 return isl_pw_multi_aff_alloc(dom
, ma
);
3921 /* Create a piecewise multi-affine expression in the given space that maps each
3922 * input dimension to the corresponding output dimension.
3924 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3925 __isl_take isl_space
*space
)
3927 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3930 /* Exploit the equalities in "eq" to simplify the affine expressions.
3932 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3933 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3937 maff
= isl_multi_aff_cow(maff
);
3941 for (i
= 0; i
< maff
->n
; ++i
) {
3942 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3943 isl_basic_set_copy(eq
));
3948 isl_basic_set_free(eq
);
3951 isl_basic_set_free(eq
);
3952 isl_multi_aff_free(maff
);
3956 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3961 maff
= isl_multi_aff_cow(maff
);
3965 for (i
= 0; i
< maff
->n
; ++i
) {
3966 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3968 return isl_multi_aff_free(maff
);
3974 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3975 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3977 maff1
= isl_multi_aff_add(maff1
, maff2
);
3978 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3982 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3990 /* Return the set of domain elements where "ma1" is lexicographically
3991 * smaller than or equal to "ma2".
3993 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3994 __isl_take isl_multi_aff
*ma2
)
3996 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3999 /* Return the set of domain elements where "ma1" is lexicographically
4000 * smaller than "ma2".
4002 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4003 __isl_take isl_multi_aff
*ma2
)
4005 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4008 /* Return the set of domain elements where "ma1" and "ma2"
4011 static __isl_give isl_set
*isl_multi_aff_order_set(
4012 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4013 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4016 isl_map
*map1
, *map2
;
4019 map1
= isl_map_from_multi_aff(ma1
);
4020 map2
= isl_map_from_multi_aff(ma2
);
4021 map
= isl_map_range_product(map1
, map2
);
4022 space
= isl_space_range(isl_map_get_space(map
));
4023 space
= isl_space_domain(isl_space_unwrap(space
));
4025 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4027 return isl_map_domain(map
);
4030 /* Return the set of domain elements where "ma1" is lexicographically
4031 * greater than or equal to "ma2".
4033 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4034 __isl_take isl_multi_aff
*ma2
)
4036 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4039 /* Return the set of domain elements where "ma1" is lexicographically
4040 * greater than "ma2".
4042 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4043 __isl_take isl_multi_aff
*ma2
)
4045 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4049 #define PW isl_pw_multi_aff
4051 #define EL isl_multi_aff
4053 #define EL_IS_ZERO is_empty
4057 #define IS_ZERO is_empty
4060 #undef DEFAULT_IS_ZERO
4061 #define DEFAULT_IS_ZERO 0
4066 #define NO_INVOLVES_DIMS
4067 #define NO_INSERT_DIMS
4071 #include <isl_pw_templ.c>
4072 #include <isl_pw_union_opt.c>
4077 #define UNION isl_union_pw_multi_aff
4079 #define PART isl_pw_multi_aff
4081 #define PARTS pw_multi_aff
4083 #include <isl_union_multi.c>
4084 #include <isl_union_neg.c>
4086 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4087 __isl_take isl_pw_multi_aff
*pma1
,
4088 __isl_take isl_pw_multi_aff
*pma2
)
4090 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4091 &isl_multi_aff_lex_ge_set
);
4094 /* Given two piecewise multi affine expressions, return a piecewise
4095 * multi-affine expression defined on the union of the definition domains
4096 * of the inputs that is equal to the lexicographic maximum of the two
4097 * inputs on each cell. If only one of the two inputs is defined on
4098 * a given cell, then it is considered to be the maximum.
4100 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4101 __isl_take isl_pw_multi_aff
*pma1
,
4102 __isl_take isl_pw_multi_aff
*pma2
)
4104 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4105 &pw_multi_aff_union_lexmax
);
4108 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4109 __isl_take isl_pw_multi_aff
*pma1
,
4110 __isl_take isl_pw_multi_aff
*pma2
)
4112 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4113 &isl_multi_aff_lex_le_set
);
4116 /* Given two piecewise multi affine expressions, return a piecewise
4117 * multi-affine expression defined on the union of the definition domains
4118 * of the inputs that is equal to the lexicographic minimum of the two
4119 * inputs on each cell. If only one of the two inputs is defined on
4120 * a given cell, then it is considered to be the minimum.
4122 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4123 __isl_take isl_pw_multi_aff
*pma1
,
4124 __isl_take isl_pw_multi_aff
*pma2
)
4126 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4127 &pw_multi_aff_union_lexmin
);
4130 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4131 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4133 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4134 &isl_multi_aff_add
);
4137 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4138 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4140 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4144 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4145 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4147 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4148 &isl_multi_aff_sub
);
4151 /* Subtract "pma2" from "pma1" and return the result.
4153 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4154 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4156 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4160 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4161 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4163 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4166 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4167 * with the actual sum on the shared domain and
4168 * the defined expression on the symmetric difference of the domains.
4170 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4171 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4173 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4176 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4177 * with the actual sum on the shared domain and
4178 * the defined expression on the symmetric difference of the domains.
4180 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4181 __isl_take isl_union_pw_multi_aff
*upma1
,
4182 __isl_take isl_union_pw_multi_aff
*upma2
)
4184 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4187 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4188 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4190 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4191 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4195 isl_pw_multi_aff
*res
;
4200 n
= pma1
->n
* pma2
->n
;
4201 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4202 isl_space_copy(pma2
->dim
));
4203 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4205 for (i
= 0; i
< pma1
->n
; ++i
) {
4206 for (j
= 0; j
< pma2
->n
; ++j
) {
4210 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4211 isl_set_copy(pma2
->p
[j
].set
));
4212 ma
= isl_multi_aff_product(
4213 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4214 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4215 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4219 isl_pw_multi_aff_free(pma1
);
4220 isl_pw_multi_aff_free(pma2
);
4223 isl_pw_multi_aff_free(pma1
);
4224 isl_pw_multi_aff_free(pma2
);
4228 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4229 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4231 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4232 &pw_multi_aff_product
);
4235 /* Construct a map mapping the domain of the piecewise multi-affine expression
4236 * to its range, with each dimension in the range equated to the
4237 * corresponding affine expression on its cell.
4239 * If the domain of "pma" is rational, then so is the constructed "map".
4241 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4249 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4251 for (i
= 0; i
< pma
->n
; ++i
) {
4253 isl_multi_aff
*maff
;
4254 isl_basic_map
*bmap
;
4257 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4259 map
= isl_map_free(map
);
4260 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4261 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4262 map_i
= isl_map_from_basic_map(bmap
);
4263 map_i
= isl_map_intersect_domain(map_i
,
4264 isl_set_copy(pma
->p
[i
].set
));
4265 map
= isl_map_union_disjoint(map
, map_i
);
4268 isl_pw_multi_aff_free(pma
);
4272 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4277 if (!isl_space_is_set(pma
->dim
))
4278 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4279 "isl_pw_multi_aff cannot be converted into an isl_set",
4282 return isl_map_from_pw_multi_aff(pma
);
4284 isl_pw_multi_aff_free(pma
);
4288 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4289 * denominator "denom".
4290 * "denom" is allowed to be negative, in which case the actual denominator
4291 * is -denom and the expressions are added instead.
4293 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4294 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4300 first
= isl_seq_first_non_zero(c
, n
);
4304 sign
= isl_int_sgn(denom
);
4306 isl_int_abs(d
, denom
);
4307 for (i
= first
; i
< n
; ++i
) {
4310 if (isl_int_is_zero(c
[i
]))
4312 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4313 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4314 aff_i
= isl_aff_scale_down(aff_i
, d
);
4316 aff
= isl_aff_sub(aff
, aff_i
);
4318 aff
= isl_aff_add(aff
, aff_i
);
4325 /* Extract an affine expression that expresses the output dimension "pos"
4326 * of "bmap" in terms of the parameters and input dimensions from
4328 * Note that this expression may involve integer divisions defined
4329 * in terms of parameters and input dimensions.
4330 * The equality may also involve references to earlier (but not later)
4331 * output dimensions. These are replaced by the corresponding elements
4334 * If the equality is of the form
4336 * f(i) + h(j) + a x + g(i) = 0,
4338 * with f(i) a linear combinations of the parameters and input dimensions,
4339 * g(i) a linear combination of integer divisions defined in terms of the same
4340 * and h(j) a linear combinations of earlier output dimensions,
4341 * then the affine expression is
4343 * (-f(i) - g(i))/a - h(j)/a
4345 * If the equality is of the form
4347 * f(i) + h(j) - a x + g(i) = 0,
4349 * then the affine expression is
4351 * (f(i) + g(i))/a - h(j)/(-a)
4354 * If "div" refers to an integer division (i.e., it is smaller than
4355 * the number of integer divisions), then the equality constraint
4356 * does involve an integer division (the one at position "div") that
4357 * is defined in terms of output dimensions. However, this integer
4358 * division can be eliminated by exploiting a pair of constraints
4359 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4360 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4362 * In particular, let
4364 * x = e(i) + m floor(...)
4366 * with e(i) the expression derived above and floor(...) the integer
4367 * division involving output dimensions.
4378 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4379 * = (e(i) - l) mod m
4383 * x - l = (e(i) - l) mod m
4387 * x = ((e(i) - l) mod m) + l
4389 * The variable "shift" below contains the expression -l, which may
4390 * also involve a linear combination of earlier output dimensions.
4392 static __isl_give isl_aff
*extract_aff_from_equality(
4393 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4394 __isl_keep isl_multi_aff
*ma
)
4397 unsigned n_div
, n_out
;
4399 isl_local_space
*ls
;
4400 isl_aff
*aff
, *shift
;
4403 ctx
= isl_basic_map_get_ctx(bmap
);
4404 ls
= isl_basic_map_get_local_space(bmap
);
4405 ls
= isl_local_space_domain(ls
);
4406 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4409 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4410 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4411 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4412 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4413 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4414 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4415 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4417 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4418 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4419 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4422 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4423 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4424 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4425 bmap
->eq
[eq
][o_out
+ pos
]);
4427 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4430 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4431 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4432 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4433 isl_int_set_si(shift
->v
->el
[0], 1);
4434 shift
= subtract_initial(shift
, ma
, pos
,
4435 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4436 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4437 mod
= isl_val_int_from_isl_int(ctx
,
4438 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4439 mod
= isl_val_abs(mod
);
4440 aff
= isl_aff_mod_val(aff
, mod
);
4441 aff
= isl_aff_sub(aff
, shift
);
4444 isl_local_space_free(ls
);
4447 isl_local_space_free(ls
);
4452 /* Given a basic map with output dimensions defined
4453 * in terms of the parameters input dimensions and earlier
4454 * output dimensions using an equality (and possibly a pair on inequalities),
4455 * extract an isl_aff that expresses output dimension "pos" in terms
4456 * of the parameters and input dimensions.
4457 * Note that this expression may involve integer divisions defined
4458 * in terms of parameters and input dimensions.
4459 * "ma" contains the expressions corresponding to earlier output dimensions.
4461 * This function shares some similarities with
4462 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4464 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4465 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4472 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4473 if (eq
>= bmap
->n_eq
)
4474 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4475 "unable to find suitable equality", return NULL
);
4476 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4478 aff
= isl_aff_remove_unused_divs(aff
);
4482 /* Given a basic map where each output dimension is defined
4483 * in terms of the parameters and input dimensions using an equality,
4484 * extract an isl_multi_aff that expresses the output dimensions in terms
4485 * of the parameters and input dimensions.
4487 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4488 __isl_take isl_basic_map
*bmap
)
4497 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4498 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4500 for (i
= 0; i
< n_out
; ++i
) {
4503 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4504 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4507 isl_basic_map_free(bmap
);
4512 /* Given a basic set where each set dimension is defined
4513 * in terms of the parameters using an equality,
4514 * extract an isl_multi_aff that expresses the set dimensions in terms
4515 * of the parameters.
4517 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4518 __isl_take isl_basic_set
*bset
)
4520 return extract_isl_multi_aff_from_basic_map(bset
);
4523 /* Create an isl_pw_multi_aff that is equivalent to
4524 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4525 * The given basic map is such that each output dimension is defined
4526 * in terms of the parameters and input dimensions using an equality.
4528 * Since some applications expect the result of isl_pw_multi_aff_from_map
4529 * to only contain integer affine expressions, we compute the floor
4530 * of the expression before returning.
4532 * Remove all constraints involving local variables without
4533 * an explicit representation (resulting in the removal of those
4534 * local variables) prior to the actual extraction to ensure
4535 * that the local spaces in which the resulting affine expressions
4536 * are created do not contain any unknown local variables.
4537 * Removing such constraints is safe because constraints involving
4538 * unknown local variables are not used to determine whether
4539 * a basic map is obviously single-valued.
4541 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4542 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4546 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4547 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4548 ma
= isl_multi_aff_floor(ma
);
4549 return isl_pw_multi_aff_alloc(domain
, ma
);
4552 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4553 * This obviously only works if the input "map" is single-valued.
4554 * If so, we compute the lexicographic minimum of the image in the form
4555 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4556 * to its lexicographic minimum.
4557 * If the input is not single-valued, we produce an error.
4559 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4560 __isl_take isl_map
*map
)
4564 isl_pw_multi_aff
*pma
;
4566 sv
= isl_map_is_single_valued(map
);
4570 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4571 "map is not single-valued", goto error
);
4572 map
= isl_map_make_disjoint(map
);
4576 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4578 for (i
= 0; i
< map
->n
; ++i
) {
4579 isl_pw_multi_aff
*pma_i
;
4580 isl_basic_map
*bmap
;
4581 bmap
= isl_basic_map_copy(map
->p
[i
]);
4582 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4583 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4593 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4594 * taking into account that the output dimension at position "d"
4595 * can be represented as
4597 * x = floor((e(...) + c1) / m)
4599 * given that constraint "i" is of the form
4601 * e(...) + c1 - m x >= 0
4604 * Let "map" be of the form
4608 * We construct a mapping
4610 * A -> [A -> x = floor(...)]
4612 * apply that to the map, obtaining
4614 * [A -> x = floor(...)] -> B
4616 * and equate dimension "d" to x.
4617 * We then compute a isl_pw_multi_aff representation of the resulting map
4618 * and plug in the mapping above.
4620 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4621 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4625 isl_local_space
*ls
;
4633 isl_pw_multi_aff
*pma
;
4636 is_set
= isl_map_is_set(map
);
4640 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4641 ctx
= isl_map_get_ctx(map
);
4642 space
= isl_space_domain(isl_map_get_space(map
));
4643 n_in
= isl_space_dim(space
, isl_dim_set
);
4644 n
= isl_space_dim(space
, isl_dim_all
);
4646 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4648 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4649 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4651 isl_basic_map_free(hull
);
4653 ls
= isl_local_space_from_space(isl_space_copy(space
));
4654 aff
= isl_aff_alloc_vec(ls
, v
);
4655 aff
= isl_aff_floor(aff
);
4657 isl_space_free(space
);
4658 ma
= isl_multi_aff_from_aff(aff
);
4660 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4661 ma
= isl_multi_aff_range_product(ma
,
4662 isl_multi_aff_from_aff(aff
));
4665 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4666 map
= isl_map_apply_domain(map
, insert
);
4667 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4668 pma
= isl_pw_multi_aff_from_map(map
);
4669 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4674 isl_basic_map_free(hull
);
4678 /* Is constraint "c" of the form
4680 * e(...) + c1 - m x >= 0
4684 * -e(...) + c2 + m x >= 0
4686 * where m > 1 and e only depends on parameters and input dimemnsions?
4688 * "offset" is the offset of the output dimensions
4689 * "pos" is the position of output dimension x.
4691 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4693 if (isl_int_is_zero(c
[offset
+ d
]))
4695 if (isl_int_is_one(c
[offset
+ d
]))
4697 if (isl_int_is_negone(c
[offset
+ d
]))
4699 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4701 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4702 total
- (offset
+ d
+ 1)) != -1)
4707 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4709 * As a special case, we first check if there is any pair of constraints,
4710 * shared by all the basic maps in "map" that force a given dimension
4711 * to be equal to the floor of some affine combination of the input dimensions.
4713 * In particular, if we can find two constraints
4715 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4719 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4721 * where m > 1 and e only depends on parameters and input dimemnsions,
4724 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4726 * then we know that we can take
4728 * x = floor((e(...) + c1) / m)
4730 * without having to perform any computation.
4732 * Note that we know that
4736 * If c1 + c2 were 0, then we would have detected an equality during
4737 * simplification. If c1 + c2 were negative, then we would have detected
4740 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4741 __isl_take isl_map
*map
)
4747 isl_basic_map
*hull
;
4749 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4754 dim
= isl_map_dim(map
, isl_dim_out
);
4755 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4756 total
= 1 + isl_basic_map_total_dim(hull
);
4758 for (d
= 0; d
< dim
; ++d
) {
4759 for (i
= 0; i
< n
; ++i
) {
4760 if (!is_potential_div_constraint(hull
->ineq
[i
],
4763 for (j
= i
+ 1; j
< n
; ++j
) {
4764 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4765 hull
->ineq
[j
] + 1, total
- 1))
4767 isl_int_add(sum
, hull
->ineq
[i
][0],
4769 if (isl_int_abs_lt(sum
,
4770 hull
->ineq
[i
][offset
+ d
]))
4777 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4779 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4783 isl_basic_map_free(hull
);
4784 return pw_multi_aff_from_map_base(map
);
4787 isl_basic_map_free(hull
);
4791 /* Given an affine expression
4793 * [A -> B] -> f(A,B)
4795 * construct an isl_multi_aff
4799 * such that dimension "d" in B' is set to "aff" and the remaining
4800 * dimensions are set equal to the corresponding dimensions in B.
4801 * "n_in" is the dimension of the space A.
4802 * "n_out" is the dimension of the space B.
4804 * If "is_set" is set, then the affine expression is of the form
4808 * and we construct an isl_multi_aff
4812 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4813 unsigned n_in
, unsigned n_out
, int is_set
)
4817 isl_space
*space
, *space2
;
4818 isl_local_space
*ls
;
4820 space
= isl_aff_get_domain_space(aff
);
4821 ls
= isl_local_space_from_space(isl_space_copy(space
));
4822 space2
= isl_space_copy(space
);
4824 space2
= isl_space_range(isl_space_unwrap(space2
));
4825 space
= isl_space_map_from_domain_and_range(space
, space2
);
4826 ma
= isl_multi_aff_alloc(space
);
4827 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4829 for (i
= 0; i
< n_out
; ++i
) {
4832 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4833 isl_dim_set
, n_in
+ i
);
4834 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4837 isl_local_space_free(ls
);
4842 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4843 * taking into account that the dimension at position "d" can be written as
4845 * x = m a + f(..) (1)
4847 * where m is equal to "gcd".
4848 * "i" is the index of the equality in "hull" that defines f(..).
4849 * In particular, the equality is of the form
4851 * f(..) - x + m g(existentials) = 0
4855 * -f(..) + x + m g(existentials) = 0
4857 * We basically plug (1) into "map", resulting in a map with "a"
4858 * in the range instead of "x". The corresponding isl_pw_multi_aff
4859 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4861 * Specifically, given the input map
4865 * We first wrap it into a set
4869 * and define (1) on top of the corresponding space, resulting in "aff".
4870 * We use this to create an isl_multi_aff that maps the output position "d"
4871 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4872 * We plug this into the wrapped map, unwrap the result and compute the
4873 * corresponding isl_pw_multi_aff.
4874 * The result is an expression
4882 * so that we can plug that into "aff", after extending the latter to
4888 * If "map" is actually a set, then there is no "A" space, meaning
4889 * that we do not need to perform any wrapping, and that the result
4890 * of the recursive call is of the form
4894 * which is plugged into a mapping of the form
4898 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4899 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4904 isl_local_space
*ls
;
4907 isl_pw_multi_aff
*pma
, *id
;
4913 is_set
= isl_map_is_set(map
);
4917 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4918 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4919 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4924 set
= isl_map_wrap(map
);
4925 space
= isl_space_map_from_set(isl_set_get_space(set
));
4926 ma
= isl_multi_aff_identity(space
);
4927 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4928 aff
= isl_aff_alloc(ls
);
4930 isl_int_set_si(aff
->v
->el
[0], 1);
4931 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4932 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4935 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4937 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4939 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4940 set
= isl_set_preimage_multi_aff(set
, ma
);
4942 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4947 map
= isl_set_unwrap(set
);
4948 pma
= isl_pw_multi_aff_from_map(map
);
4951 space
= isl_pw_multi_aff_get_domain_space(pma
);
4952 space
= isl_space_map_from_set(space
);
4953 id
= isl_pw_multi_aff_identity(space
);
4954 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4956 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4957 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4959 isl_basic_map_free(hull
);
4963 isl_basic_map_free(hull
);
4967 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4968 * "hull" contains the equalities valid for "map".
4970 * Check if any of the output dimensions is "strided".
4971 * That is, we check if it can be written as
4975 * with m greater than 1, a some combination of existentially quantified
4976 * variables and f an expression in the parameters and input dimensions.
4977 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4979 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4982 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
4983 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
4992 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4993 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4996 isl_basic_map_free(hull
);
4997 return pw_multi_aff_from_map_check_div(map
);
5002 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5003 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5005 for (i
= 0; i
< n_out
; ++i
) {
5006 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5007 isl_int
*eq
= hull
->eq
[j
];
5008 isl_pw_multi_aff
*res
;
5010 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5011 !isl_int_is_negone(eq
[o_out
+ i
]))
5013 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5015 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5016 n_out
- (i
+ 1)) != -1)
5018 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5019 if (isl_int_is_zero(gcd
))
5021 if (isl_int_is_one(gcd
))
5024 res
= pw_multi_aff_from_map_stride(map
, hull
,
5032 isl_basic_map_free(hull
);
5033 return pw_multi_aff_from_map_check_div(map
);
5036 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5038 * As a special case, we first check if all output dimensions are uniquely
5039 * defined in terms of the parameters and input dimensions over the entire
5040 * domain. If so, we extract the desired isl_pw_multi_aff directly
5041 * from the affine hull of "map" and its domain.
5043 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5046 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5049 isl_basic_map
*hull
;
5054 if (isl_map_n_basic_map(map
) == 1) {
5055 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5056 hull
= isl_basic_map_plain_affine_hull(hull
);
5057 sv
= isl_basic_map_plain_is_single_valued(hull
);
5059 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5061 isl_basic_map_free(hull
);
5063 map
= isl_map_detect_equalities(map
);
5064 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5065 sv
= isl_basic_map_plain_is_single_valued(hull
);
5067 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5069 return pw_multi_aff_from_map_check_strides(map
, hull
);
5070 isl_basic_map_free(hull
);
5075 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5077 return isl_pw_multi_aff_from_map(set
);
5080 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5083 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5085 isl_union_pw_multi_aff
**upma
= user
;
5086 isl_pw_multi_aff
*pma
;
5088 pma
= isl_pw_multi_aff_from_map(map
);
5089 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5091 return *upma
? isl_stat_ok
: isl_stat_error
;
5094 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5097 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5098 __isl_take isl_aff
*aff
)
5101 isl_pw_multi_aff
*pma
;
5103 ma
= isl_multi_aff_from_aff(aff
);
5104 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5105 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5108 /* Try and create an isl_union_pw_multi_aff that is equivalent
5109 * to the given isl_union_map.
5110 * The isl_union_map is required to be single-valued in each space.
5111 * Otherwise, an error is produced.
5113 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5114 __isl_take isl_union_map
*umap
)
5117 isl_union_pw_multi_aff
*upma
;
5119 space
= isl_union_map_get_space(umap
);
5120 upma
= isl_union_pw_multi_aff_empty(space
);
5121 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5122 upma
= isl_union_pw_multi_aff_free(upma
);
5123 isl_union_map_free(umap
);
5128 /* Try and create an isl_union_pw_multi_aff that is equivalent
5129 * to the given isl_union_set.
5130 * The isl_union_set is required to be a singleton in each space.
5131 * Otherwise, an error is produced.
5133 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5134 __isl_take isl_union_set
*uset
)
5136 return isl_union_pw_multi_aff_from_union_map(uset
);
5139 /* Return the piecewise affine expression "set ? 1 : 0".
5141 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5144 isl_space
*space
= isl_set_get_space(set
);
5145 isl_local_space
*ls
= isl_local_space_from_space(space
);
5146 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5147 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5149 one
= isl_aff_add_constant_si(one
, 1);
5150 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5151 set
= isl_set_complement(set
);
5152 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5157 /* Plug in "subs" for dimension "type", "pos" of "aff".
5159 * Let i be the dimension to replace and let "subs" be of the form
5163 * and "aff" of the form
5169 * (a f + d g')/(m d)
5171 * where g' is the result of plugging in "subs" in each of the integer
5174 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5175 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5180 aff
= isl_aff_cow(aff
);
5182 return isl_aff_free(aff
);
5184 ctx
= isl_aff_get_ctx(aff
);
5185 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5186 isl_die(ctx
, isl_error_invalid
,
5187 "spaces don't match", return isl_aff_free(aff
));
5188 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5189 isl_die(ctx
, isl_error_unsupported
,
5190 "cannot handle divs yet", return isl_aff_free(aff
));
5192 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5194 return isl_aff_free(aff
);
5196 aff
->v
= isl_vec_cow(aff
->v
);
5198 return isl_aff_free(aff
);
5200 pos
+= isl_local_space_offset(aff
->ls
, type
);
5203 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5204 aff
->v
->size
, subs
->v
->size
, v
);
5210 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5211 * expressions in "maff".
5213 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5214 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5215 __isl_keep isl_aff
*subs
)
5219 maff
= isl_multi_aff_cow(maff
);
5221 return isl_multi_aff_free(maff
);
5223 if (type
== isl_dim_in
)
5226 for (i
= 0; i
< maff
->n
; ++i
) {
5227 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5229 return isl_multi_aff_free(maff
);
5235 /* Plug in "subs" for dimension "type", "pos" of "pma".
5237 * pma is of the form
5241 * while subs is of the form
5243 * v' = B_j(v) -> S_j
5245 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5246 * has a contribution in the result, in particular
5248 * C_ij(S_j) -> M_i(S_j)
5250 * Note that plugging in S_j in C_ij may also result in an empty set
5251 * and this contribution should simply be discarded.
5253 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5254 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5255 __isl_keep isl_pw_aff
*subs
)
5258 isl_pw_multi_aff
*res
;
5261 return isl_pw_multi_aff_free(pma
);
5263 n
= pma
->n
* subs
->n
;
5264 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5266 for (i
= 0; i
< pma
->n
; ++i
) {
5267 for (j
= 0; j
< subs
->n
; ++j
) {
5269 isl_multi_aff
*res_ij
;
5272 common
= isl_set_intersect(
5273 isl_set_copy(pma
->p
[i
].set
),
5274 isl_set_copy(subs
->p
[j
].set
));
5275 common
= isl_set_substitute(common
,
5276 type
, pos
, subs
->p
[j
].aff
);
5277 empty
= isl_set_plain_is_empty(common
);
5278 if (empty
< 0 || empty
) {
5279 isl_set_free(common
);
5285 res_ij
= isl_multi_aff_substitute(
5286 isl_multi_aff_copy(pma
->p
[i
].maff
),
5287 type
, pos
, subs
->p
[j
].aff
);
5289 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5293 isl_pw_multi_aff_free(pma
);
5296 isl_pw_multi_aff_free(pma
);
5297 isl_pw_multi_aff_free(res
);
5301 /* Compute the preimage of a range of dimensions in the affine expression "src"
5302 * under "ma" and put the result in "dst". The number of dimensions in "src"
5303 * that precede the range is given by "n_before". The number of dimensions
5304 * in the range is given by the number of output dimensions of "ma".
5305 * The number of dimensions that follow the range is given by "n_after".
5306 * If "has_denom" is set (to one),
5307 * then "src" and "dst" have an extra initial denominator.
5308 * "n_div_ma" is the number of existentials in "ma"
5309 * "n_div_bset" is the number of existentials in "src"
5310 * The resulting "dst" (which is assumed to have been allocated by
5311 * the caller) contains coefficients for both sets of existentials,
5312 * first those in "ma" and then those in "src".
5313 * f, c1, c2 and g are temporary objects that have been initialized
5316 * Let src represent the expression
5318 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5320 * and let ma represent the expressions
5322 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5324 * We start out with the following expression for dst:
5326 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5328 * with the multiplication factor f initially equal to 1
5329 * and f \sum_i b_i v_i kept separately.
5330 * For each x_i that we substitute, we multiply the numerator
5331 * (and denominator) of dst by c_1 = m_i and add the numerator
5332 * of the x_i expression multiplied by c_2 = f b_i,
5333 * after removing the common factors of c_1 and c_2.
5334 * The multiplication factor f also needs to be multiplied by c_1
5335 * for the next x_j, j > i.
5337 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5338 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5339 int n_div_ma
, int n_div_bmap
,
5340 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5343 int n_param
, n_in
, n_out
;
5346 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5347 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5348 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5350 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5351 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5352 isl_seq_clr(dst
+ o_dst
, n_in
);
5355 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5358 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5360 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5362 isl_int_set_si(f
, 1);
5364 for (i
= 0; i
< n_out
; ++i
) {
5365 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5367 if (isl_int_is_zero(src
[offset
]))
5369 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5370 isl_int_mul(c2
, f
, src
[offset
]);
5371 isl_int_gcd(g
, c1
, c2
);
5372 isl_int_divexact(c1
, c1
, g
);
5373 isl_int_divexact(c2
, c2
, g
);
5375 isl_int_mul(f
, f
, c1
);
5378 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5379 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5380 o_dst
+= 1 + n_param
;
5381 o_src
+= 1 + n_param
;
5382 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5384 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5385 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5388 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5390 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5391 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5394 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5396 isl_int_mul(dst
[0], dst
[0], c1
);
5400 /* Compute the pullback of "aff" by the function represented by "ma".
5401 * In other words, plug in "ma" in "aff". The result is an affine expression
5402 * defined over the domain space of "ma".
5404 * If "aff" is represented by
5406 * (a(p) + b x + c(divs))/d
5408 * and ma is represented by
5410 * x = D(p) + F(y) + G(divs')
5412 * then the result is
5414 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5416 * The divs in the local space of the input are similarly adjusted
5417 * through a call to isl_local_space_preimage_multi_aff.
5419 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5420 __isl_take isl_multi_aff
*ma
)
5422 isl_aff
*res
= NULL
;
5423 isl_local_space
*ls
;
5424 int n_div_aff
, n_div_ma
;
5425 isl_int f
, c1
, c2
, g
;
5427 ma
= isl_multi_aff_align_divs(ma
);
5431 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5432 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5434 ls
= isl_aff_get_domain_local_space(aff
);
5435 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5436 res
= isl_aff_alloc(ls
);
5445 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5454 isl_multi_aff_free(ma
);
5455 res
= isl_aff_normalize(res
);
5459 isl_multi_aff_free(ma
);
5464 /* Compute the pullback of "aff1" by the function represented by "aff2".
5465 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5466 * defined over the domain space of "aff1".
5468 * The domain of "aff1" should match the range of "aff2", which means
5469 * that it should be single-dimensional.
5471 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5472 __isl_take isl_aff
*aff2
)
5476 ma
= isl_multi_aff_from_aff(aff2
);
5477 return isl_aff_pullback_multi_aff(aff1
, ma
);
5480 /* Compute the pullback of "ma1" by the function represented by "ma2".
5481 * In other words, plug in "ma2" in "ma1".
5483 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5485 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5486 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5489 isl_space
*space
= NULL
;
5491 ma2
= isl_multi_aff_align_divs(ma2
);
5492 ma1
= isl_multi_aff_cow(ma1
);
5496 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5497 isl_multi_aff_get_space(ma1
));
5499 for (i
= 0; i
< ma1
->n
; ++i
) {
5500 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5501 isl_multi_aff_copy(ma2
));
5506 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5507 isl_multi_aff_free(ma2
);
5510 isl_space_free(space
);
5511 isl_multi_aff_free(ma2
);
5512 isl_multi_aff_free(ma1
);
5516 /* Compute the pullback of "ma1" by the function represented by "ma2".
5517 * In other words, plug in "ma2" in "ma1".
5519 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5520 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5522 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5523 &isl_multi_aff_pullback_multi_aff_aligned
);
5526 /* Extend the local space of "dst" to include the divs
5527 * in the local space of "src".
5529 * If "src" does not have any divs or if the local spaces of "dst" and
5530 * "src" are the same, then no extension is required.
5532 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5533 __isl_keep isl_aff
*src
)
5536 int src_n_div
, dst_n_div
;
5543 return isl_aff_free(dst
);
5545 ctx
= isl_aff_get_ctx(src
);
5546 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5548 return isl_aff_free(dst
);
5550 isl_die(ctx
, isl_error_invalid
,
5551 "spaces don't match", goto error
);
5553 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5556 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5558 return isl_aff_free(dst
);
5562 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5563 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5564 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5565 if (!exp1
|| (dst_n_div
&& !exp2
))
5568 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5569 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5577 return isl_aff_free(dst
);
5580 /* Adjust the local spaces of the affine expressions in "maff"
5581 * such that they all have the save divs.
5583 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5584 __isl_take isl_multi_aff
*maff
)
5592 maff
= isl_multi_aff_cow(maff
);
5596 for (i
= 1; i
< maff
->n
; ++i
)
5597 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5598 for (i
= 1; i
< maff
->n
; ++i
) {
5599 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5601 return isl_multi_aff_free(maff
);
5607 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5609 aff
= isl_aff_cow(aff
);
5613 aff
->ls
= isl_local_space_lift(aff
->ls
);
5615 return isl_aff_free(aff
);
5620 /* Lift "maff" to a space with extra dimensions such that the result
5621 * has no more existentially quantified variables.
5622 * If "ls" is not NULL, then *ls is assigned the local space that lies
5623 * at the basis of the lifting applied to "maff".
5625 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5626 __isl_give isl_local_space
**ls
)
5640 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5641 *ls
= isl_local_space_from_space(space
);
5643 return isl_multi_aff_free(maff
);
5648 maff
= isl_multi_aff_cow(maff
);
5649 maff
= isl_multi_aff_align_divs(maff
);
5653 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5654 space
= isl_multi_aff_get_space(maff
);
5655 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5656 space
= isl_space_extend_domain_with_range(space
,
5657 isl_multi_aff_get_space(maff
));
5659 return isl_multi_aff_free(maff
);
5660 isl_space_free(maff
->space
);
5661 maff
->space
= space
;
5664 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5666 return isl_multi_aff_free(maff
);
5669 for (i
= 0; i
< maff
->n
; ++i
) {
5670 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5678 isl_local_space_free(*ls
);
5679 return isl_multi_aff_free(maff
);
5683 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5685 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5686 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5696 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5697 if (pos
< 0 || pos
>= n_out
)
5698 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5699 "index out of bounds", return NULL
);
5701 space
= isl_pw_multi_aff_get_space(pma
);
5702 space
= isl_space_drop_dims(space
, isl_dim_out
,
5703 pos
+ 1, n_out
- pos
- 1);
5704 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5706 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5707 for (i
= 0; i
< pma
->n
; ++i
) {
5709 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5710 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5716 /* Return an isl_pw_multi_aff with the given "set" as domain and
5717 * an unnamed zero-dimensional range.
5719 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5720 __isl_take isl_set
*set
)
5725 space
= isl_set_get_space(set
);
5726 space
= isl_space_from_domain(space
);
5727 ma
= isl_multi_aff_zero(space
);
5728 return isl_pw_multi_aff_alloc(set
, ma
);
5731 /* Add an isl_pw_multi_aff with the given "set" as domain and
5732 * an unnamed zero-dimensional range to *user.
5734 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5737 isl_union_pw_multi_aff
**upma
= user
;
5738 isl_pw_multi_aff
*pma
;
5740 pma
= isl_pw_multi_aff_from_domain(set
);
5741 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5746 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5747 * an unnamed zero-dimensional range.
5749 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5750 __isl_take isl_union_set
*uset
)
5753 isl_union_pw_multi_aff
*upma
;
5758 space
= isl_union_set_get_space(uset
);
5759 upma
= isl_union_pw_multi_aff_empty(space
);
5761 if (isl_union_set_foreach_set(uset
,
5762 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5765 isl_union_set_free(uset
);
5768 isl_union_set_free(uset
);
5769 isl_union_pw_multi_aff_free(upma
);
5773 /* Convert "pma" to an isl_map and add it to *umap.
5775 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5778 isl_union_map
**umap
= user
;
5781 map
= isl_map_from_pw_multi_aff(pma
);
5782 *umap
= isl_union_map_add_map(*umap
, map
);
5787 /* Construct a union map mapping the domain of the union
5788 * piecewise multi-affine expression to its range, with each dimension
5789 * in the range equated to the corresponding affine expression on its cell.
5791 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5792 __isl_take isl_union_pw_multi_aff
*upma
)
5795 isl_union_map
*umap
;
5800 space
= isl_union_pw_multi_aff_get_space(upma
);
5801 umap
= isl_union_map_empty(space
);
5803 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5804 &map_from_pw_multi_aff
, &umap
) < 0)
5807 isl_union_pw_multi_aff_free(upma
);
5810 isl_union_pw_multi_aff_free(upma
);
5811 isl_union_map_free(umap
);
5815 /* Local data for bin_entry and the callback "fn".
5817 struct isl_union_pw_multi_aff_bin_data
{
5818 isl_union_pw_multi_aff
*upma2
;
5819 isl_union_pw_multi_aff
*res
;
5820 isl_pw_multi_aff
*pma
;
5821 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5824 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5825 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5827 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5829 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5833 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5835 isl_pw_multi_aff_free(pma
);
5840 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5841 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5842 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5843 * as *entry. The callback should adjust data->res if desired.
5845 static __isl_give isl_union_pw_multi_aff
*bin_op(
5846 __isl_take isl_union_pw_multi_aff
*upma1
,
5847 __isl_take isl_union_pw_multi_aff
*upma2
,
5848 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5851 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5853 space
= isl_union_pw_multi_aff_get_space(upma2
);
5854 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5855 space
= isl_union_pw_multi_aff_get_space(upma1
);
5856 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5858 if (!upma1
|| !upma2
)
5862 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5863 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5864 &bin_entry
, &data
) < 0)
5867 isl_union_pw_multi_aff_free(upma1
);
5868 isl_union_pw_multi_aff_free(upma2
);
5871 isl_union_pw_multi_aff_free(upma1
);
5872 isl_union_pw_multi_aff_free(upma2
);
5873 isl_union_pw_multi_aff_free(data
.res
);
5877 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5878 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5880 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5881 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5885 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5886 isl_pw_multi_aff_get_space(pma2
));
5887 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5888 &isl_multi_aff_range_product
);
5891 /* Given two isl_pw_multi_affs A -> B and C -> D,
5892 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5894 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5895 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5897 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5898 &pw_multi_aff_range_product
);
5901 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5902 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5904 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5905 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5909 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5910 isl_pw_multi_aff_get_space(pma2
));
5911 space
= isl_space_flatten_range(space
);
5912 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5913 &isl_multi_aff_flat_range_product
);
5916 /* Given two isl_pw_multi_affs A -> B and C -> D,
5917 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5919 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5920 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5922 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5923 &pw_multi_aff_flat_range_product
);
5926 /* If data->pma and "pma2" have the same domain space, then compute
5927 * their flat range product and the result to data->res.
5929 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5932 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5934 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5935 pma2
->dim
, isl_dim_in
)) {
5936 isl_pw_multi_aff_free(pma2
);
5940 pma2
= isl_pw_multi_aff_flat_range_product(
5941 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5943 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5948 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5949 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5951 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5952 __isl_take isl_union_pw_multi_aff
*upma1
,
5953 __isl_take isl_union_pw_multi_aff
*upma2
)
5955 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5958 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5959 * The parameters are assumed to have been aligned.
5961 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5962 * except that it works on two different isl_pw_* types.
5964 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5965 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5966 __isl_take isl_pw_aff
*pa
)
5969 isl_pw_multi_aff
*res
= NULL
;
5974 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5975 pa
->dim
, isl_dim_in
))
5976 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5977 "domains don't match", goto error
);
5978 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5979 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5980 "index out of bounds", goto error
);
5983 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5985 for (i
= 0; i
< pma
->n
; ++i
) {
5986 for (j
= 0; j
< pa
->n
; ++j
) {
5988 isl_multi_aff
*res_ij
;
5991 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5992 isl_set_copy(pa
->p
[j
].set
));
5993 empty
= isl_set_plain_is_empty(common
);
5994 if (empty
< 0 || empty
) {
5995 isl_set_free(common
);
6001 res_ij
= isl_multi_aff_set_aff(
6002 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6003 isl_aff_copy(pa
->p
[j
].aff
));
6004 res_ij
= isl_multi_aff_gist(res_ij
,
6005 isl_set_copy(common
));
6007 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6011 isl_pw_multi_aff_free(pma
);
6012 isl_pw_aff_free(pa
);
6015 isl_pw_multi_aff_free(pma
);
6016 isl_pw_aff_free(pa
);
6017 return isl_pw_multi_aff_free(res
);
6020 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6022 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6023 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6024 __isl_take isl_pw_aff
*pa
)
6028 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
6029 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6030 if (!isl_space_has_named_params(pma
->dim
) ||
6031 !isl_space_has_named_params(pa
->dim
))
6032 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6033 "unaligned unnamed parameters", goto error
);
6034 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6035 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6036 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6038 isl_pw_multi_aff_free(pma
);
6039 isl_pw_aff_free(pa
);
6043 /* Do the parameters of "pa" match those of "space"?
6045 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6046 __isl_keep isl_space
*space
)
6048 isl_space
*pa_space
;
6052 return isl_bool_error
;
6054 pa_space
= isl_pw_aff_get_space(pa
);
6056 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6058 isl_space_free(pa_space
);
6062 /* Check that the domain space of "pa" matches "space".
6064 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6065 __isl_keep isl_space
*space
)
6067 isl_space
*pa_space
;
6071 return isl_stat_error
;
6073 pa_space
= isl_pw_aff_get_space(pa
);
6075 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6079 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6080 "parameters don't match", goto error
);
6081 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6082 pa_space
, isl_dim_in
);
6086 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6087 "domains don't match", goto error
);
6088 isl_space_free(pa_space
);
6091 isl_space_free(pa_space
);
6092 return isl_stat_error
;
6100 #include <isl_multi_templ.c>
6101 #include <isl_multi_apply_set.c>
6102 #include <isl_multi_coalesce.c>
6103 #include <isl_multi_gist.c>
6104 #include <isl_multi_hash.c>
6105 #include <isl_multi_intersect.c>
6107 /* Scale the elements of "pma" by the corresponding elements of "mv".
6109 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6110 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6114 pma
= isl_pw_multi_aff_cow(pma
);
6117 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6118 mv
->space
, isl_dim_set
))
6119 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6120 "spaces don't match", goto error
);
6121 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6122 mv
->space
, isl_dim_param
)) {
6123 pma
= isl_pw_multi_aff_align_params(pma
,
6124 isl_multi_val_get_space(mv
));
6125 mv
= isl_multi_val_align_params(mv
,
6126 isl_pw_multi_aff_get_space(pma
));
6131 for (i
= 0; i
< pma
->n
; ++i
) {
6132 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6133 isl_multi_val_copy(mv
));
6134 if (!pma
->p
[i
].maff
)
6138 isl_multi_val_free(mv
);
6141 isl_multi_val_free(mv
);
6142 isl_pw_multi_aff_free(pma
);
6146 /* This function is called for each entry of an isl_union_pw_multi_aff.
6147 * If the space of the entry matches that of data->mv,
6148 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6149 * Otherwise, return an empty isl_pw_multi_aff.
6151 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6152 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6154 isl_multi_val
*mv
= user
;
6158 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6159 mv
->space
, isl_dim_set
)) {
6160 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6161 isl_pw_multi_aff_free(pma
);
6162 return isl_pw_multi_aff_empty(space
);
6165 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6168 /* Scale the elements of "upma" by the corresponding elements of "mv",
6169 * for those entries that match the space of "mv".
6171 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6172 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6174 upma
= isl_union_pw_multi_aff_align_params(upma
,
6175 isl_multi_val_get_space(mv
));
6176 mv
= isl_multi_val_align_params(mv
,
6177 isl_union_pw_multi_aff_get_space(upma
));
6181 return isl_union_pw_multi_aff_transform(upma
,
6182 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6184 isl_multi_val_free(mv
);
6187 isl_multi_val_free(mv
);
6188 isl_union_pw_multi_aff_free(upma
);
6192 /* Construct and return a piecewise multi affine expression
6193 * in the given space with value zero in each of the output dimensions and
6194 * a universe domain.
6196 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6198 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6201 /* Construct and return a piecewise multi affine expression
6202 * that is equal to the given piecewise affine expression.
6204 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6205 __isl_take isl_pw_aff
*pa
)
6209 isl_pw_multi_aff
*pma
;
6214 space
= isl_pw_aff_get_space(pa
);
6215 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6217 for (i
= 0; i
< pa
->n
; ++i
) {
6221 set
= isl_set_copy(pa
->p
[i
].set
);
6222 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6223 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6226 isl_pw_aff_free(pa
);
6230 /* Construct a set or map mapping the shared (parameter) domain
6231 * of the piecewise affine expressions to the range of "mpa"
6232 * with each dimension in the range equated to the
6233 * corresponding piecewise affine expression.
6235 static __isl_give isl_map
*map_from_multi_pw_aff(
6236 __isl_take isl_multi_pw_aff
*mpa
)
6245 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6246 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6247 "invalid space", goto error
);
6249 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6250 map
= isl_map_universe(isl_space_from_domain(space
));
6252 for (i
= 0; i
< mpa
->n
; ++i
) {
6256 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6257 map_i
= map_from_pw_aff(pa
);
6259 map
= isl_map_flat_range_product(map
, map_i
);
6262 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6264 isl_multi_pw_aff_free(mpa
);
6267 isl_multi_pw_aff_free(mpa
);
6271 /* Construct a map mapping the shared domain
6272 * of the piecewise affine expressions to the range of "mpa"
6273 * with each dimension in the range equated to the
6274 * corresponding piecewise affine expression.
6276 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6280 if (isl_space_is_set(mpa
->space
))
6281 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6282 "space of input is not a map", goto error
);
6284 return map_from_multi_pw_aff(mpa
);
6286 isl_multi_pw_aff_free(mpa
);
6290 /* Construct a set mapping the shared parameter domain
6291 * of the piecewise affine expressions to the space of "mpa"
6292 * with each dimension in the range equated to the
6293 * corresponding piecewise affine expression.
6295 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6299 if (!isl_space_is_set(mpa
->space
))
6300 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6301 "space of input is not a set", goto error
);
6303 return map_from_multi_pw_aff(mpa
);
6305 isl_multi_pw_aff_free(mpa
);
6309 /* Construct and return a piecewise multi affine expression
6310 * that is equal to the given multi piecewise affine expression
6311 * on the shared domain of the piecewise affine expressions.
6313 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6314 __isl_take isl_multi_pw_aff
*mpa
)
6319 isl_pw_multi_aff
*pma
;
6324 space
= isl_multi_pw_aff_get_space(mpa
);
6327 isl_multi_pw_aff_free(mpa
);
6328 return isl_pw_multi_aff_zero(space
);
6331 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6332 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6334 for (i
= 1; i
< mpa
->n
; ++i
) {
6335 isl_pw_multi_aff
*pma_i
;
6337 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6338 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6339 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6342 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6344 isl_multi_pw_aff_free(mpa
);
6348 /* Construct and return a multi piecewise affine expression
6349 * that is equal to the given multi affine expression.
6351 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6352 __isl_take isl_multi_aff
*ma
)
6355 isl_multi_pw_aff
*mpa
;
6360 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6361 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6363 for (i
= 0; i
< n
; ++i
) {
6366 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6367 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6370 isl_multi_aff_free(ma
);
6374 /* Construct and return a multi piecewise affine expression
6375 * that is equal to the given piecewise multi affine expression.
6377 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6378 __isl_take isl_pw_multi_aff
*pma
)
6382 isl_multi_pw_aff
*mpa
;
6387 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6388 space
= isl_pw_multi_aff_get_space(pma
);
6389 mpa
= isl_multi_pw_aff_alloc(space
);
6391 for (i
= 0; i
< n
; ++i
) {
6394 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6395 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6398 isl_pw_multi_aff_free(pma
);
6402 /* Do "pa1" and "pa2" represent the same function?
6404 * We first check if they are obviously equal.
6405 * If not, we convert them to maps and check if those are equal.
6407 * If "pa1" or "pa2" contain any NaNs, then they are considered
6408 * not to be the same. A NaN is not equal to anything, not even
6411 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6412 __isl_keep isl_pw_aff
*pa2
)
6416 isl_map
*map1
, *map2
;
6419 return isl_bool_error
;
6421 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6422 if (equal
< 0 || equal
)
6424 has_nan
= isl_pw_aff_involves_nan(pa1
);
6425 if (has_nan
>= 0 && !has_nan
)
6426 has_nan
= isl_pw_aff_involves_nan(pa2
);
6428 return isl_bool_error
;
6430 return isl_bool_false
;
6432 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6433 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6434 equal
= isl_map_is_equal(map1
, map2
);
6441 /* Do "mpa1" and "mpa2" represent the same function?
6443 * Note that we cannot convert the entire isl_multi_pw_aff
6444 * to a map because the domains of the piecewise affine expressions
6445 * may not be the same.
6447 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6448 __isl_keep isl_multi_pw_aff
*mpa2
)
6454 return isl_bool_error
;
6456 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6457 mpa2
->space
, isl_dim_param
)) {
6458 if (!isl_space_has_named_params(mpa1
->space
))
6459 return isl_bool_false
;
6460 if (!isl_space_has_named_params(mpa2
->space
))
6461 return isl_bool_false
;
6462 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6463 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6464 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6465 isl_multi_pw_aff_get_space(mpa2
));
6466 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6467 isl_multi_pw_aff_get_space(mpa1
));
6468 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6469 isl_multi_pw_aff_free(mpa1
);
6470 isl_multi_pw_aff_free(mpa2
);
6474 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6475 if (equal
< 0 || !equal
)
6478 for (i
= 0; i
< mpa1
->n
; ++i
) {
6479 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6480 if (equal
< 0 || !equal
)
6484 return isl_bool_true
;
6487 /* Do "pma1" and "pma2" represent the same function?
6489 * First check if they are obviously equal.
6490 * If not, then convert them to maps and check if those are equal.
6492 * If "pa1" or "pa2" contain any NaNs, then they are considered
6493 * not to be the same. A NaN is not equal to anything, not even
6496 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6497 __isl_keep isl_pw_multi_aff
*pma2
)
6501 isl_map
*map1
, *map2
;
6504 return isl_bool_error
;
6506 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6507 if (equal
< 0 || equal
)
6509 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6510 if (has_nan
>= 0 && !has_nan
)
6511 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6512 if (has_nan
< 0 || has_nan
)
6513 return isl_bool_not(has_nan
);
6515 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6516 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6517 equal
= isl_map_is_equal(map1
, map2
);
6524 /* Compute the pullback of "mpa" by the function represented by "ma".
6525 * In other words, plug in "ma" in "mpa".
6527 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6529 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6530 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6533 isl_space
*space
= NULL
;
6535 mpa
= isl_multi_pw_aff_cow(mpa
);
6539 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6540 isl_multi_pw_aff_get_space(mpa
));
6544 for (i
= 0; i
< mpa
->n
; ++i
) {
6545 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6546 isl_multi_aff_copy(ma
));
6551 isl_multi_aff_free(ma
);
6552 isl_space_free(mpa
->space
);
6556 isl_space_free(space
);
6557 isl_multi_pw_aff_free(mpa
);
6558 isl_multi_aff_free(ma
);
6562 /* Compute the pullback of "mpa" by the function represented by "ma".
6563 * In other words, plug in "ma" in "mpa".
6565 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6566 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6570 if (isl_space_match(mpa
->space
, isl_dim_param
,
6571 ma
->space
, isl_dim_param
))
6572 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6573 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6574 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6575 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6577 isl_multi_pw_aff_free(mpa
);
6578 isl_multi_aff_free(ma
);
6582 /* Compute the pullback of "mpa" by the function represented by "pma".
6583 * In other words, plug in "pma" in "mpa".
6585 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6587 static __isl_give isl_multi_pw_aff
*
6588 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6589 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6592 isl_space
*space
= NULL
;
6594 mpa
= isl_multi_pw_aff_cow(mpa
);
6598 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6599 isl_multi_pw_aff_get_space(mpa
));
6601 for (i
= 0; i
< mpa
->n
; ++i
) {
6602 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6603 isl_pw_multi_aff_copy(pma
));
6608 isl_pw_multi_aff_free(pma
);
6609 isl_space_free(mpa
->space
);
6613 isl_space_free(space
);
6614 isl_multi_pw_aff_free(mpa
);
6615 isl_pw_multi_aff_free(pma
);
6619 /* Compute the pullback of "mpa" by the function represented by "pma".
6620 * In other words, plug in "pma" in "mpa".
6622 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6623 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6627 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6628 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6629 mpa
= isl_multi_pw_aff_align_params(mpa
,
6630 isl_pw_multi_aff_get_space(pma
));
6631 pma
= isl_pw_multi_aff_align_params(pma
,
6632 isl_multi_pw_aff_get_space(mpa
));
6633 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6635 isl_multi_pw_aff_free(mpa
);
6636 isl_pw_multi_aff_free(pma
);
6640 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6641 * with the domain of "aff". The domain of the result is the same
6643 * "mpa" and "aff" are assumed to have been aligned.
6645 * We first extract the parametric constant from "aff", defined
6646 * over the correct domain.
6647 * Then we add the appropriate combinations of the members of "mpa".
6648 * Finally, we add the integer divisions through recursive calls.
6650 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6651 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6659 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6660 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6662 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6663 tmp
= isl_aff_copy(aff
);
6664 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6665 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6666 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6667 isl_space_dim(space
, isl_dim_set
));
6668 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6669 pa
= isl_pw_aff_from_aff(tmp
);
6671 for (i
= 0; i
< n_in
; ++i
) {
6674 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6676 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6677 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6678 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6679 pa
= isl_pw_aff_add(pa
, pa_i
);
6682 for (i
= 0; i
< n_div
; ++i
) {
6686 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6688 div
= isl_aff_get_div(aff
, i
);
6689 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6690 isl_multi_pw_aff_copy(mpa
), div
);
6691 pa_i
= isl_pw_aff_floor(pa_i
);
6692 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6693 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6694 pa
= isl_pw_aff_add(pa
, pa_i
);
6697 isl_multi_pw_aff_free(mpa
);
6703 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6704 * with the domain of "aff". The domain of the result is the same
6707 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6708 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6712 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6713 mpa
->space
, isl_dim_param
))
6714 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6716 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6717 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6719 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6722 isl_multi_pw_aff_free(mpa
);
6726 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6727 * with the domain of "pa". The domain of the result is the same
6729 * "mpa" and "pa" are assumed to have been aligned.
6731 * We consider each piece in turn. Note that the domains of the
6732 * pieces are assumed to be disjoint and they remain disjoint
6733 * after taking the preimage (over the same function).
6735 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6736 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6745 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6746 isl_pw_aff_get_space(pa
));
6747 res
= isl_pw_aff_empty(space
);
6749 for (i
= 0; i
< pa
->n
; ++i
) {
6753 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6754 isl_multi_pw_aff_copy(mpa
),
6755 isl_aff_copy(pa
->p
[i
].aff
));
6756 domain
= isl_set_copy(pa
->p
[i
].set
);
6757 domain
= isl_set_preimage_multi_pw_aff(domain
,
6758 isl_multi_pw_aff_copy(mpa
));
6759 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6760 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6763 isl_pw_aff_free(pa
);
6764 isl_multi_pw_aff_free(mpa
);
6767 isl_pw_aff_free(pa
);
6768 isl_multi_pw_aff_free(mpa
);
6772 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6773 * with the domain of "pa". The domain of the result is the same
6776 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6777 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6781 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6782 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6784 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6785 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6787 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6789 isl_pw_aff_free(pa
);
6790 isl_multi_pw_aff_free(mpa
);
6794 /* Compute the pullback of "pa" by the function represented by "mpa".
6795 * In other words, plug in "mpa" in "pa".
6796 * "pa" and "mpa" are assumed to have been aligned.
6798 * The pullback is computed by applying "pa" to "mpa".
6800 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6801 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6803 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6806 /* Compute the pullback of "pa" by the function represented by "mpa".
6807 * In other words, plug in "mpa" in "pa".
6809 * The pullback is computed by applying "pa" to "mpa".
6811 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6812 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6814 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6817 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6818 * In other words, plug in "mpa2" in "mpa1".
6820 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6822 * We pullback each member of "mpa1" in turn.
6824 static __isl_give isl_multi_pw_aff
*
6825 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6826 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6829 isl_space
*space
= NULL
;
6831 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6835 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6836 isl_multi_pw_aff_get_space(mpa1
));
6838 for (i
= 0; i
< mpa1
->n
; ++i
) {
6839 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6840 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6845 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6847 isl_multi_pw_aff_free(mpa2
);
6850 isl_space_free(space
);
6851 isl_multi_pw_aff_free(mpa1
);
6852 isl_multi_pw_aff_free(mpa2
);
6856 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6857 * In other words, plug in "mpa2" in "mpa1".
6859 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6860 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6862 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6863 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6866 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6867 * of "mpa1" and "mpa2" live in the same space, construct map space
6868 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6869 * with this map space as extract argument.
6871 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6872 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6873 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6874 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6877 isl_space
*space1
, *space2
;
6880 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6881 isl_multi_pw_aff_get_space(mpa2
));
6882 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6883 isl_multi_pw_aff_get_space(mpa1
));
6886 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6887 mpa2
->space
, isl_dim_out
);
6891 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6892 "range spaces don't match", goto error
);
6893 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6894 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6895 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6897 res
= order(mpa1
, mpa2
, space1
);
6898 isl_multi_pw_aff_free(mpa1
);
6899 isl_multi_pw_aff_free(mpa2
);
6902 isl_multi_pw_aff_free(mpa1
);
6903 isl_multi_pw_aff_free(mpa2
);
6907 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6908 * where the function values are equal. "space" is the space of the result.
6909 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6911 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6912 * in the sequences are equal.
6914 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6915 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6916 __isl_take isl_space
*space
)
6921 res
= isl_map_universe(space
);
6923 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6924 for (i
= 0; i
< n
; ++i
) {
6925 isl_pw_aff
*pa1
, *pa2
;
6928 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6929 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6930 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6931 res
= isl_map_intersect(res
, map
);
6937 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6938 * where the function values are equal.
6940 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6941 __isl_take isl_multi_pw_aff
*mpa2
)
6943 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6944 &isl_multi_pw_aff_eq_map_on_space
);
6947 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6948 * where the function values of "mpa1" is lexicographically satisfies "base"
6949 * compared to that of "mpa2". "space" is the space of the result.
6950 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6952 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6953 * if its i-th element satisfies "base" when compared to
6954 * the i-th element of "mpa2" while all previous elements are
6957 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6958 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6959 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6960 __isl_take isl_pw_aff
*pa2
),
6961 __isl_take isl_space
*space
)
6964 isl_map
*res
, *rest
;
6966 res
= isl_map_empty(isl_space_copy(space
));
6967 rest
= isl_map_universe(space
);
6969 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6970 for (i
= 0; i
< n
; ++i
) {
6971 isl_pw_aff
*pa1
, *pa2
;
6974 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6975 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6976 map
= base(pa1
, pa2
);
6977 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6978 res
= isl_map_union(res
, map
);
6983 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6984 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6985 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6986 rest
= isl_map_intersect(rest
, map
);
6993 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6994 * where the function value of "mpa1" is lexicographically less than that
6995 * of "mpa2". "space" is the space of the result.
6996 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6998 * "mpa1" is less than "mpa2" if its i-th element is smaller
6999 * than the i-th element of "mpa2" while all previous elements are
7002 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7003 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7004 __isl_take isl_space
*space
)
7006 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7007 &isl_pw_aff_lt_map
, space
);
7010 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7011 * where the function value of "mpa1" is lexicographically less than that
7014 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7015 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7017 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7018 &isl_multi_pw_aff_lex_lt_map_on_space
);
7021 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7022 * where the function value of "mpa1" is lexicographically greater than that
7023 * of "mpa2". "space" is the space of the result.
7024 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7026 * "mpa1" is greater than "mpa2" if its i-th element is greater
7027 * than the i-th element of "mpa2" while all previous elements are
7030 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7031 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7032 __isl_take isl_space
*space
)
7034 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7035 &isl_pw_aff_gt_map
, space
);
7038 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7039 * where the function value of "mpa1" is lexicographically greater than that
7042 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7043 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7045 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7046 &isl_multi_pw_aff_lex_gt_map_on_space
);
7049 /* Compare two isl_affs.
7051 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7052 * than "aff2" and 0 if they are equal.
7054 * The order is fairly arbitrary. We do consider expressions that only involve
7055 * earlier dimensions as "smaller".
7057 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7070 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7074 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7075 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7077 return last1
- last2
;
7079 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7082 /* Compare two isl_pw_affs.
7084 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7085 * than "pa2" and 0 if they are equal.
7087 * The order is fairly arbitrary. We do consider expressions that only involve
7088 * earlier dimensions as "smaller".
7090 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7091 __isl_keep isl_pw_aff
*pa2
)
7104 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7108 if (pa1
->n
!= pa2
->n
)
7109 return pa1
->n
- pa2
->n
;
7111 for (i
= 0; i
< pa1
->n
; ++i
) {
7112 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7115 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7123 /* Return a piecewise affine expression that is equal to "v" on "domain".
7125 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7126 __isl_take isl_val
*v
)
7129 isl_local_space
*ls
;
7132 space
= isl_set_get_space(domain
);
7133 ls
= isl_local_space_from_space(space
);
7134 aff
= isl_aff_val_on_domain(ls
, v
);
7136 return isl_pw_aff_alloc(domain
, aff
);
7139 /* Return a multi affine expression that is equal to "mv" on domain
7142 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7143 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7147 isl_local_space
*ls
;
7153 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7154 space2
= isl_multi_val_get_space(mv
);
7155 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7156 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7157 space
= isl_space_map_from_domain_and_range(space
, space2
);
7158 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7159 ls
= isl_local_space_from_space(isl_space_domain(space
));
7160 for (i
= 0; i
< n
; ++i
) {
7164 v
= isl_multi_val_get_val(mv
, i
);
7165 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7166 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7168 isl_local_space_free(ls
);
7170 isl_multi_val_free(mv
);
7173 isl_space_free(space
);
7174 isl_multi_val_free(mv
);
7178 /* Return a piecewise multi-affine expression
7179 * that is equal to "mv" on "domain".
7181 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7182 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7187 space
= isl_set_get_space(domain
);
7188 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7190 return isl_pw_multi_aff_alloc(domain
, ma
);
7193 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7194 * mv is the value that should be attained on each domain set
7195 * res collects the results
7197 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7199 isl_union_pw_multi_aff
*res
;
7202 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7203 * and add it to data->res.
7205 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7208 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7209 isl_pw_multi_aff
*pma
;
7212 mv
= isl_multi_val_copy(data
->mv
);
7213 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7214 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7216 return data
->res
? isl_stat_ok
: isl_stat_error
;
7219 /* Return a union piecewise multi-affine expression
7220 * that is equal to "mv" on "domain".
7222 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7223 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7225 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7228 space
= isl_union_set_get_space(domain
);
7229 data
.res
= isl_union_pw_multi_aff_empty(space
);
7231 if (isl_union_set_foreach_set(domain
,
7232 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7233 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7234 isl_union_set_free(domain
);
7235 isl_multi_val_free(mv
);
7239 /* Compute the pullback of data->pma by the function represented by "pma2",
7240 * provided the spaces match, and add the results to data->res.
7242 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7244 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7246 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7247 pma2
->dim
, isl_dim_out
)) {
7248 isl_pw_multi_aff_free(pma2
);
7252 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7253 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7255 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7257 return isl_stat_error
;
7262 /* Compute the pullback of "upma1" by the function represented by "upma2".
7264 __isl_give isl_union_pw_multi_aff
*
7265 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7266 __isl_take isl_union_pw_multi_aff
*upma1
,
7267 __isl_take isl_union_pw_multi_aff
*upma2
)
7269 return bin_op(upma1
, upma2
, &pullback_entry
);
7272 /* Check that the domain space of "upa" matches "space".
7274 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7275 * can in principle never fail since the space "space" is that
7276 * of the isl_multi_union_pw_aff and is a set space such that
7277 * there is no domain space to match.
7279 * We check the parameters and double-check that "space" is
7280 * indeed that of a set.
7282 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7283 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7285 isl_space
*upa_space
;
7289 return isl_stat_error
;
7291 match
= isl_space_is_set(space
);
7293 return isl_stat_error
;
7295 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7296 "expecting set space", return -1);
7298 upa_space
= isl_union_pw_aff_get_space(upa
);
7299 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7303 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7304 "parameters don't match", goto error
);
7306 isl_space_free(upa_space
);
7309 isl_space_free(upa_space
);
7310 return isl_stat_error
;
7313 /* Do the parameters of "upa" match those of "space"?
7315 static isl_bool
isl_union_pw_aff_matching_params(
7316 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7318 isl_space
*upa_space
;
7322 return isl_bool_error
;
7324 upa_space
= isl_union_pw_aff_get_space(upa
);
7326 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7328 isl_space_free(upa_space
);
7332 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7333 * space represents the new parameters.
7334 * res collects the results.
7336 struct isl_union_pw_aff_reset_params_data
{
7338 isl_union_pw_aff
*res
;
7341 /* Replace the parameters of "pa" by data->space and
7342 * add the result to data->res.
7344 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7346 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7349 space
= isl_pw_aff_get_space(pa
);
7350 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7351 pa
= isl_pw_aff_reset_space(pa
, space
);
7352 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7354 return data
->res
? isl_stat_ok
: isl_stat_error
;
7357 /* Replace the domain space of "upa" by "space".
7358 * Since a union expression does not have a (single) domain space,
7359 * "space" is necessarily a parameter space.
7361 * Since the order and the names of the parameters determine
7362 * the hash value, we need to create a new hash table.
7364 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7365 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7367 struct isl_union_pw_aff_reset_params_data data
= { space
};
7370 match
= isl_union_pw_aff_matching_params(upa
, space
);
7372 upa
= isl_union_pw_aff_free(upa
);
7374 isl_space_free(space
);
7378 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7379 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7380 data
.res
= isl_union_pw_aff_free(data
.res
);
7382 isl_union_pw_aff_free(upa
);
7383 isl_space_free(space
);
7387 /* Return the floor of "pa".
7389 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7391 return isl_pw_aff_floor(pa
);
7394 /* Given f, return floor(f).
7396 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7397 __isl_take isl_union_pw_aff
*upa
)
7399 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7404 * upa mod m = upa - m * floor(upa/m)
7406 * with m an integer value.
7408 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7409 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7411 isl_union_pw_aff
*res
;
7416 if (!isl_val_is_int(m
))
7417 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7418 "expecting integer modulo", goto error
);
7419 if (!isl_val_is_pos(m
))
7420 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7421 "expecting positive modulo", goto error
);
7423 res
= isl_union_pw_aff_copy(upa
);
7424 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7425 upa
= isl_union_pw_aff_floor(upa
);
7426 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7427 res
= isl_union_pw_aff_sub(res
, upa
);
7432 isl_union_pw_aff_free(upa
);
7436 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7437 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7439 * "res" collects the results.
7441 struct isl_union_pw_aff_aff_on_domain_data
{
7443 isl_union_pw_aff
*res
;
7446 /* Construct a piecewise affine expression that is equal to data->aff
7447 * on "domain" and add the result to data->res.
7449 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7451 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7456 aff
= isl_aff_copy(data
->aff
);
7457 dim
= isl_set_dim(domain
, isl_dim_set
);
7458 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7459 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7460 pa
= isl_pw_aff_alloc(domain
, aff
);
7461 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7463 return data
->res
? isl_stat_ok
: isl_stat_error
;
7466 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7467 * pos is the output position that needs to be extracted.
7468 * res collects the results.
7470 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7472 isl_union_pw_aff
*res
;
7475 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7476 * (assuming it has such a dimension) and add it to data->res.
7478 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7480 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7485 return isl_stat_error
;
7487 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7488 if (data
->pos
>= n_out
) {
7489 isl_pw_multi_aff_free(pma
);
7493 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7494 isl_pw_multi_aff_free(pma
);
7496 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7498 return data
->res
? isl_stat_ok
: isl_stat_error
;
7501 /* Extract an isl_union_pw_aff corresponding to
7502 * output dimension "pos" of "upma".
7504 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7505 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7507 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7514 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7515 "cannot extract at negative position", return NULL
);
7517 space
= isl_union_pw_multi_aff_get_space(upma
);
7518 data
.res
= isl_union_pw_aff_empty(space
);
7520 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7521 &get_union_pw_aff
, &data
) < 0)
7522 data
.res
= isl_union_pw_aff_free(data
.res
);
7527 /* Return a union piecewise affine expression
7528 * that is equal to "aff" on "domain".
7530 * Construct an isl_pw_aff on each of the sets in "domain" and
7531 * collect the results.
7533 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7534 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7536 struct isl_union_pw_aff_aff_on_domain_data data
;
7539 if (!domain
|| !aff
)
7541 if (!isl_local_space_is_params(aff
->ls
))
7542 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7543 "expecting parametric expression", goto error
);
7545 space
= isl_union_set_get_space(domain
);
7546 data
.res
= isl_union_pw_aff_empty(space
);
7548 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7549 data
.res
= isl_union_pw_aff_free(data
.res
);
7550 isl_union_set_free(domain
);
7554 isl_union_set_free(domain
);
7559 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7560 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7561 * "res" collects the results.
7563 struct isl_union_pw_aff_val_on_domain_data
{
7565 isl_union_pw_aff
*res
;
7568 /* Construct a piecewise affine expression that is equal to data->v
7569 * on "domain" and add the result to data->res.
7571 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7573 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7577 v
= isl_val_copy(data
->v
);
7578 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7579 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7581 return data
->res
? isl_stat_ok
: isl_stat_error
;
7584 /* Return a union piecewise affine expression
7585 * that is equal to "v" on "domain".
7587 * Construct an isl_pw_aff on each of the sets in "domain" and
7588 * collect the results.
7590 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7591 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7593 struct isl_union_pw_aff_val_on_domain_data data
;
7596 space
= isl_union_set_get_space(domain
);
7597 data
.res
= isl_union_pw_aff_empty(space
);
7599 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7600 data
.res
= isl_union_pw_aff_free(data
.res
);
7601 isl_union_set_free(domain
);
7606 /* Construct a piecewise multi affine expression
7607 * that is equal to "pa" and add it to upma.
7609 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7612 isl_union_pw_multi_aff
**upma
= user
;
7613 isl_pw_multi_aff
*pma
;
7615 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7616 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7618 return *upma
? isl_stat_ok
: isl_stat_error
;
7621 /* Construct and return a union piecewise multi affine expression
7622 * that is equal to the given union piecewise affine expression.
7624 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7625 __isl_take isl_union_pw_aff
*upa
)
7628 isl_union_pw_multi_aff
*upma
;
7633 space
= isl_union_pw_aff_get_space(upa
);
7634 upma
= isl_union_pw_multi_aff_empty(space
);
7636 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7637 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7638 upma
= isl_union_pw_multi_aff_free(upma
);
7640 isl_union_pw_aff_free(upa
);
7644 /* Compute the set of elements in the domain of "pa" where it is zero and
7645 * add this set to "uset".
7647 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7649 isl_union_set
**uset
= (isl_union_set
**)user
;
7651 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7653 return *uset
? isl_stat_ok
: isl_stat_error
;
7656 /* Return a union set containing those elements in the domain
7657 * of "upa" where it is zero.
7659 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7660 __isl_take isl_union_pw_aff
*upa
)
7662 isl_union_set
*zero
;
7664 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7665 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7666 zero
= isl_union_set_free(zero
);
7668 isl_union_pw_aff_free(upa
);
7672 /* Convert "pa" to an isl_map and add it to *umap.
7674 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7676 isl_union_map
**umap
= user
;
7679 map
= isl_map_from_pw_aff(pa
);
7680 *umap
= isl_union_map_add_map(*umap
, map
);
7682 return *umap
? isl_stat_ok
: isl_stat_error
;
7685 /* Construct a union map mapping the domain of the union
7686 * piecewise affine expression to its range, with the single output dimension
7687 * equated to the corresponding affine expressions on their cells.
7689 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7690 __isl_take isl_union_pw_aff
*upa
)
7693 isl_union_map
*umap
;
7698 space
= isl_union_pw_aff_get_space(upa
);
7699 umap
= isl_union_map_empty(space
);
7701 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7703 umap
= isl_union_map_free(umap
);
7705 isl_union_pw_aff_free(upa
);
7709 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7710 * upma is the function that is plugged in.
7711 * pa is the current part of the function in which upma is plugged in.
7712 * res collects the results.
7714 struct isl_union_pw_aff_pullback_upma_data
{
7715 isl_union_pw_multi_aff
*upma
;
7717 isl_union_pw_aff
*res
;
7720 /* Check if "pma" can be plugged into data->pa.
7721 * If so, perform the pullback and add the result to data->res.
7723 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7725 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7728 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7729 pma
->dim
, isl_dim_out
)) {
7730 isl_pw_multi_aff_free(pma
);
7734 pa
= isl_pw_aff_copy(data
->pa
);
7735 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7737 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7739 return data
->res
? isl_stat_ok
: isl_stat_error
;
7742 /* Check if any of the elements of data->upma can be plugged into pa,
7743 * add if so add the result to data->res.
7745 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7747 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7751 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7753 isl_pw_aff_free(pa
);
7758 /* Compute the pullback of "upa" by the function represented by "upma".
7759 * In other words, plug in "upma" in "upa". The result contains
7760 * expressions defined over the domain space of "upma".
7762 * Run over all pairs of elements in "upa" and "upma", perform
7763 * the pullback when appropriate and collect the results.
7764 * If the hash value were based on the domain space rather than
7765 * the function space, then we could run through all elements
7766 * of "upma" and directly pick out the corresponding element of "upa".
7768 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7769 __isl_take isl_union_pw_aff
*upa
,
7770 __isl_take isl_union_pw_multi_aff
*upma
)
7772 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7775 space
= isl_union_pw_multi_aff_get_space(upma
);
7776 upa
= isl_union_pw_aff_align_params(upa
, space
);
7777 space
= isl_union_pw_aff_get_space(upa
);
7778 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7784 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7785 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7786 data
.res
= isl_union_pw_aff_free(data
.res
);
7788 isl_union_pw_aff_free(upa
);
7789 isl_union_pw_multi_aff_free(upma
);
7792 isl_union_pw_aff_free(upa
);
7793 isl_union_pw_multi_aff_free(upma
);
7798 #define BASE union_pw_aff
7800 #define DOMBASE union_set
7802 #define NO_MOVE_DIMS
7811 #include <isl_multi_templ.c>
7812 #include <isl_multi_apply_set.c>
7813 #include <isl_multi_apply_union_set.c>
7814 #include <isl_multi_coalesce.c>
7815 #include <isl_multi_floor.c>
7816 #include <isl_multi_gist.c>
7817 #include <isl_multi_intersect.c>
7819 /* Construct a multiple union piecewise affine expression
7820 * in the given space with value zero in each of the output dimensions.
7822 * Since there is no canonical zero value for
7823 * a union piecewise affine expression, we can only construct
7824 * zero-dimensional "zero" value.
7826 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7827 __isl_take isl_space
*space
)
7832 if (!isl_space_is_set(space
))
7833 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7834 "expecting set space", goto error
);
7835 if (isl_space_dim(space
, isl_dim_out
) != 0)
7836 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7837 "expecting 0D space", goto error
);
7839 return isl_multi_union_pw_aff_alloc(space
);
7841 isl_space_free(space
);
7845 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7846 * with the actual sum on the shared domain and
7847 * the defined expression on the symmetric difference of the domains.
7849 * We simply iterate over the elements in both arguments and
7850 * call isl_union_pw_aff_union_add on each of them.
7852 static __isl_give isl_multi_union_pw_aff
*
7853 isl_multi_union_pw_aff_union_add_aligned(
7854 __isl_take isl_multi_union_pw_aff
*mupa1
,
7855 __isl_take isl_multi_union_pw_aff
*mupa2
)
7857 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7858 &isl_union_pw_aff_union_add
);
7861 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7862 * with the actual sum on the shared domain and
7863 * the defined expression on the symmetric difference of the domains.
7865 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7866 __isl_take isl_multi_union_pw_aff
*mupa1
,
7867 __isl_take isl_multi_union_pw_aff
*mupa2
)
7869 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7870 &isl_multi_union_pw_aff_union_add_aligned
);
7873 /* Construct and return a multi union piecewise affine expression
7874 * that is equal to the given multi affine expression.
7876 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7877 __isl_take isl_multi_aff
*ma
)
7879 isl_multi_pw_aff
*mpa
;
7881 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7882 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7885 /* Construct and return a multi union piecewise affine expression
7886 * that is equal to the given multi piecewise affine expression.
7888 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7889 __isl_take isl_multi_pw_aff
*mpa
)
7893 isl_multi_union_pw_aff
*mupa
;
7898 space
= isl_multi_pw_aff_get_space(mpa
);
7899 space
= isl_space_range(space
);
7900 mupa
= isl_multi_union_pw_aff_alloc(space
);
7902 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7903 for (i
= 0; i
< n
; ++i
) {
7905 isl_union_pw_aff
*upa
;
7907 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7908 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7909 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7912 isl_multi_pw_aff_free(mpa
);
7917 /* Extract the range space of "pma" and assign it to *space.
7918 * If *space has already been set (through a previous call to this function),
7919 * then check that the range space is the same.
7921 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7923 isl_space
**space
= user
;
7924 isl_space
*pma_space
;
7927 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7928 isl_pw_multi_aff_free(pma
);
7931 return isl_stat_error
;
7937 equal
= isl_space_is_equal(pma_space
, *space
);
7938 isl_space_free(pma_space
);
7941 return isl_stat_error
;
7943 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7944 "range spaces not the same", return isl_stat_error
);
7948 /* Construct and return a multi union piecewise affine expression
7949 * that is equal to the given union piecewise multi affine expression.
7951 * In order to be able to perform the conversion, the input
7952 * needs to be non-empty and may only involve a single range space.
7954 __isl_give isl_multi_union_pw_aff
*
7955 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7956 __isl_take isl_union_pw_multi_aff
*upma
)
7958 isl_space
*space
= NULL
;
7959 isl_multi_union_pw_aff
*mupa
;
7964 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7965 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7966 "cannot extract range space from empty input",
7968 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7975 n
= isl_space_dim(space
, isl_dim_set
);
7976 mupa
= isl_multi_union_pw_aff_alloc(space
);
7978 for (i
= 0; i
< n
; ++i
) {
7979 isl_union_pw_aff
*upa
;
7981 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7982 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7985 isl_union_pw_multi_aff_free(upma
);
7988 isl_space_free(space
);
7989 isl_union_pw_multi_aff_free(upma
);
7993 /* Try and create an isl_multi_union_pw_aff that is equivalent
7994 * to the given isl_union_map.
7995 * The isl_union_map is required to be single-valued in each space.
7996 * Moreover, it cannot be empty and all range spaces need to be the same.
7997 * Otherwise, an error is produced.
7999 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8000 __isl_take isl_union_map
*umap
)
8002 isl_union_pw_multi_aff
*upma
;
8004 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8005 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8008 /* Return a multiple union piecewise affine expression
8009 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8010 * have been aligned.
8012 static __isl_give isl_multi_union_pw_aff
*
8013 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8014 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8018 isl_multi_union_pw_aff
*mupa
;
8023 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8024 space
= isl_multi_val_get_space(mv
);
8025 mupa
= isl_multi_union_pw_aff_alloc(space
);
8026 for (i
= 0; i
< n
; ++i
) {
8028 isl_union_pw_aff
*upa
;
8030 v
= isl_multi_val_get_val(mv
, i
);
8031 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8033 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8036 isl_union_set_free(domain
);
8037 isl_multi_val_free(mv
);
8040 isl_union_set_free(domain
);
8041 isl_multi_val_free(mv
);
8045 /* Return a multiple union piecewise affine expression
8046 * that is equal to "mv" on "domain".
8048 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8049 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8053 if (isl_space_match(domain
->dim
, isl_dim_param
,
8054 mv
->space
, isl_dim_param
))
8055 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8057 domain
= isl_union_set_align_params(domain
,
8058 isl_multi_val_get_space(mv
));
8059 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8060 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8062 isl_union_set_free(domain
);
8063 isl_multi_val_free(mv
);
8067 /* Return a multiple union piecewise affine expression
8068 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8069 * have been aligned.
8071 static __isl_give isl_multi_union_pw_aff
*
8072 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8073 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8077 isl_multi_union_pw_aff
*mupa
;
8082 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8083 space
= isl_multi_aff_get_space(ma
);
8084 mupa
= isl_multi_union_pw_aff_alloc(space
);
8085 for (i
= 0; i
< n
; ++i
) {
8087 isl_union_pw_aff
*upa
;
8089 aff
= isl_multi_aff_get_aff(ma
, i
);
8090 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8092 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8095 isl_union_set_free(domain
);
8096 isl_multi_aff_free(ma
);
8099 isl_union_set_free(domain
);
8100 isl_multi_aff_free(ma
);
8104 /* Return a multiple union piecewise affine expression
8105 * that is equal to "ma" on "domain".
8107 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8108 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8112 if (isl_space_match(domain
->dim
, isl_dim_param
,
8113 ma
->space
, isl_dim_param
))
8114 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8116 domain
= isl_union_set_align_params(domain
,
8117 isl_multi_aff_get_space(ma
));
8118 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8119 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8121 isl_union_set_free(domain
);
8122 isl_multi_aff_free(ma
);
8126 /* Return a union set containing those elements in the domains
8127 * of the elements of "mupa" where they are all zero.
8129 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8130 __isl_take isl_multi_union_pw_aff
*mupa
)
8133 isl_union_pw_aff
*upa
;
8134 isl_union_set
*zero
;
8139 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8141 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8142 "cannot determine zero set "
8143 "of zero-dimensional function", goto error
);
8145 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8146 zero
= isl_union_pw_aff_zero_union_set(upa
);
8148 for (i
= 1; i
< n
; ++i
) {
8149 isl_union_set
*zero_i
;
8151 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8152 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8154 zero
= isl_union_set_intersect(zero
, zero_i
);
8157 isl_multi_union_pw_aff_free(mupa
);
8160 isl_multi_union_pw_aff_free(mupa
);
8164 /* Construct a union map mapping the shared domain
8165 * of the union piecewise affine expressions to the range of "mupa"
8166 * with each dimension in the range equated to the
8167 * corresponding union piecewise affine expression.
8169 * The input cannot be zero-dimensional as there is
8170 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8172 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8173 __isl_take isl_multi_union_pw_aff
*mupa
)
8177 isl_union_map
*umap
;
8178 isl_union_pw_aff
*upa
;
8183 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8185 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8186 "cannot determine domain of zero-dimensional "
8187 "isl_multi_union_pw_aff", goto error
);
8189 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8190 umap
= isl_union_map_from_union_pw_aff(upa
);
8192 for (i
= 1; i
< n
; ++i
) {
8193 isl_union_map
*umap_i
;
8195 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8196 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8197 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8200 space
= isl_multi_union_pw_aff_get_space(mupa
);
8201 umap
= isl_union_map_reset_range_space(umap
, space
);
8203 isl_multi_union_pw_aff_free(mupa
);
8206 isl_multi_union_pw_aff_free(mupa
);
8210 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8211 * "range" is the space from which to set the range space.
8212 * "res" collects the results.
8214 struct isl_union_pw_multi_aff_reset_range_space_data
{
8216 isl_union_pw_multi_aff
*res
;
8219 /* Replace the range space of "pma" by the range space of data->range and
8220 * add the result to data->res.
8222 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8224 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8227 space
= isl_pw_multi_aff_get_space(pma
);
8228 space
= isl_space_domain(space
);
8229 space
= isl_space_extend_domain_with_range(space
,
8230 isl_space_copy(data
->range
));
8231 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8232 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8234 return data
->res
? isl_stat_ok
: isl_stat_error
;
8237 /* Replace the range space of all the piecewise affine expressions in "upma" by
8238 * the range space of "space".
8240 * This assumes that all these expressions have the same output dimension.
8242 * Since the spaces of the expressions change, so do their hash values.
8243 * We therefore need to create a new isl_union_pw_multi_aff.
8244 * Note that the hash value is currently computed based on the entire
8245 * space even though there can only be a single expression with a given
8248 static __isl_give isl_union_pw_multi_aff
*
8249 isl_union_pw_multi_aff_reset_range_space(
8250 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8252 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8253 isl_space
*space_upma
;
8255 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8256 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8257 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8258 &reset_range_space
, &data
) < 0)
8259 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8261 isl_space_free(space
);
8262 isl_union_pw_multi_aff_free(upma
);
8266 /* Construct and return a union piecewise multi affine expression
8267 * that is equal to the given multi union piecewise affine expression.
8269 * In order to be able to perform the conversion, the input
8270 * needs to have a least one output dimension.
8272 __isl_give isl_union_pw_multi_aff
*
8273 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8274 __isl_take isl_multi_union_pw_aff
*mupa
)
8278 isl_union_pw_multi_aff
*upma
;
8279 isl_union_pw_aff
*upa
;
8284 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8286 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8287 "cannot determine domain of zero-dimensional "
8288 "isl_multi_union_pw_aff", goto error
);
8290 space
= isl_multi_union_pw_aff_get_space(mupa
);
8291 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8292 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8294 for (i
= 1; i
< n
; ++i
) {
8295 isl_union_pw_multi_aff
*upma_i
;
8297 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8298 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8299 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8302 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8304 isl_multi_union_pw_aff_free(mupa
);
8307 isl_multi_union_pw_aff_free(mupa
);
8311 /* Intersect the range of "mupa" with "range".
8312 * That is, keep only those domain elements that have a function value
8315 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8316 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8318 isl_union_pw_multi_aff
*upma
;
8319 isl_union_set
*domain
;
8324 if (!mupa
|| !range
)
8327 space
= isl_set_get_space(range
);
8328 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8329 space
, isl_dim_set
);
8330 isl_space_free(space
);
8334 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8335 "space don't match", goto error
);
8336 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8338 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8339 "cannot intersect range of zero-dimensional "
8340 "isl_multi_union_pw_aff", goto error
);
8342 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8343 isl_multi_union_pw_aff_copy(mupa
));
8344 domain
= isl_union_set_from_set(range
);
8345 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8346 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8350 isl_multi_union_pw_aff_free(mupa
);
8351 isl_set_free(range
);
8355 /* Return the shared domain of the elements of "mupa".
8357 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8358 __isl_take isl_multi_union_pw_aff
*mupa
)
8361 isl_union_pw_aff
*upa
;
8367 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8369 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8370 "cannot determine domain", goto error
);
8372 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8373 dom
= isl_union_pw_aff_domain(upa
);
8374 for (i
= 1; i
< n
; ++i
) {
8375 isl_union_set
*dom_i
;
8377 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8378 dom_i
= isl_union_pw_aff_domain(upa
);
8379 dom
= isl_union_set_intersect(dom
, dom_i
);
8382 isl_multi_union_pw_aff_free(mupa
);
8385 isl_multi_union_pw_aff_free(mupa
);
8389 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8390 * In particular, the spaces have been aligned.
8391 * The result is defined over the shared domain of the elements of "mupa"
8393 * We first extract the parametric constant part of "aff" and
8394 * define that over the shared domain.
8395 * Then we iterate over all input dimensions of "aff" and add the corresponding
8396 * multiples of the elements of "mupa".
8397 * Finally, we consider the integer divisions, calling the function
8398 * recursively to obtain an isl_union_pw_aff corresponding to the
8399 * integer division argument.
8401 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8402 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8405 isl_union_pw_aff
*upa
;
8406 isl_union_set
*uset
;
8410 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8411 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8413 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8414 cst
= isl_aff_copy(aff
);
8415 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8416 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8417 cst
= isl_aff_project_domain_on_params(cst
);
8418 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8420 for (i
= 0; i
< n_in
; ++i
) {
8421 isl_union_pw_aff
*upa_i
;
8423 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8425 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8426 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8427 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8428 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8431 for (i
= 0; i
< n_div
; ++i
) {
8433 isl_union_pw_aff
*upa_i
;
8435 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8437 div
= isl_aff_get_div(aff
, i
);
8438 upa_i
= multi_union_pw_aff_apply_aff(
8439 isl_multi_union_pw_aff_copy(mupa
), div
);
8440 upa_i
= isl_union_pw_aff_floor(upa_i
);
8441 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8442 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8443 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8446 isl_multi_union_pw_aff_free(mupa
);
8452 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8453 * with the domain of "aff".
8454 * Furthermore, the dimension of this space needs to be greater than zero.
8455 * The result is defined over the shared domain of the elements of "mupa"
8457 * We perform these checks and then hand over control to
8458 * multi_union_pw_aff_apply_aff.
8460 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8461 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8463 isl_space
*space1
, *space2
;
8466 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8467 isl_aff_get_space(aff
));
8468 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8472 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8473 space2
= isl_aff_get_domain_space(aff
);
8474 equal
= isl_space_is_equal(space1
, space2
);
8475 isl_space_free(space1
);
8476 isl_space_free(space2
);
8480 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8481 "spaces don't match", goto error
);
8482 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8483 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8484 "cannot determine domains", goto error
);
8486 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8488 isl_multi_union_pw_aff_free(mupa
);
8493 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8494 * with the domain of "ma".
8495 * Furthermore, the dimension of this space needs to be greater than zero,
8496 * unless the dimension of the target space of "ma" is also zero.
8497 * The result is defined over the shared domain of the elements of "mupa"
8499 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8500 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8502 isl_space
*space1
, *space2
;
8503 isl_multi_union_pw_aff
*res
;
8507 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8508 isl_multi_aff_get_space(ma
));
8509 ma
= isl_multi_aff_align_params(ma
,
8510 isl_multi_union_pw_aff_get_space(mupa
));
8514 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8515 space2
= isl_multi_aff_get_domain_space(ma
);
8516 equal
= isl_space_is_equal(space1
, space2
);
8517 isl_space_free(space1
);
8518 isl_space_free(space2
);
8522 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8523 "spaces don't match", goto error
);
8524 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8525 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8526 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8527 "cannot determine domains", goto error
);
8529 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8530 res
= isl_multi_union_pw_aff_alloc(space1
);
8532 for (i
= 0; i
< n_out
; ++i
) {
8534 isl_union_pw_aff
*upa
;
8536 aff
= isl_multi_aff_get_aff(ma
, i
);
8537 upa
= multi_union_pw_aff_apply_aff(
8538 isl_multi_union_pw_aff_copy(mupa
), aff
);
8539 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8542 isl_multi_aff_free(ma
);
8543 isl_multi_union_pw_aff_free(mupa
);
8546 isl_multi_union_pw_aff_free(mupa
);
8547 isl_multi_aff_free(ma
);
8551 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8552 * with the domain of "pa".
8553 * Furthermore, the dimension of this space needs to be greater than zero.
8554 * The result is defined over the shared domain of the elements of "mupa"
8556 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8557 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8561 isl_space
*space
, *space2
;
8562 isl_union_pw_aff
*upa
;
8564 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8565 isl_pw_aff_get_space(pa
));
8566 pa
= isl_pw_aff_align_params(pa
,
8567 isl_multi_union_pw_aff_get_space(mupa
));
8571 space
= isl_multi_union_pw_aff_get_space(mupa
);
8572 space2
= isl_pw_aff_get_domain_space(pa
);
8573 equal
= isl_space_is_equal(space
, space2
);
8574 isl_space_free(space
);
8575 isl_space_free(space2
);
8579 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8580 "spaces don't match", goto error
);
8581 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8582 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8583 "cannot determine domains", goto error
);
8585 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8586 upa
= isl_union_pw_aff_empty(space
);
8588 for (i
= 0; i
< pa
->n
; ++i
) {
8591 isl_multi_union_pw_aff
*mupa_i
;
8592 isl_union_pw_aff
*upa_i
;
8594 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8595 domain
= isl_set_copy(pa
->p
[i
].set
);
8596 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8597 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8598 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8599 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8602 isl_multi_union_pw_aff_free(mupa
);
8603 isl_pw_aff_free(pa
);
8606 isl_multi_union_pw_aff_free(mupa
);
8607 isl_pw_aff_free(pa
);
8611 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8612 * with the domain of "pma".
8613 * Furthermore, the dimension of this space needs to be greater than zero,
8614 * unless the dimension of the target space of "pma" is also zero.
8615 * The result is defined over the shared domain of the elements of "mupa"
8617 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8618 __isl_take isl_multi_union_pw_aff
*mupa
,
8619 __isl_take isl_pw_multi_aff
*pma
)
8621 isl_space
*space1
, *space2
;
8622 isl_multi_union_pw_aff
*res
;
8626 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8627 isl_pw_multi_aff_get_space(pma
));
8628 pma
= isl_pw_multi_aff_align_params(pma
,
8629 isl_multi_union_pw_aff_get_space(mupa
));
8633 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8634 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8635 equal
= isl_space_is_equal(space1
, space2
);
8636 isl_space_free(space1
);
8637 isl_space_free(space2
);
8641 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8642 "spaces don't match", goto error
);
8643 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8644 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8645 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8646 "cannot determine domains", goto error
);
8648 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8649 res
= isl_multi_union_pw_aff_alloc(space1
);
8651 for (i
= 0; i
< n_out
; ++i
) {
8653 isl_union_pw_aff
*upa
;
8655 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8656 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8657 isl_multi_union_pw_aff_copy(mupa
), pa
);
8658 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8661 isl_pw_multi_aff_free(pma
);
8662 isl_multi_union_pw_aff_free(mupa
);
8665 isl_multi_union_pw_aff_free(mupa
);
8666 isl_pw_multi_aff_free(pma
);
8670 /* Compute the pullback of "mupa" by the function represented by "upma".
8671 * In other words, plug in "upma" in "mupa". The result contains
8672 * expressions defined over the domain space of "upma".
8674 * Run over all elements of "mupa" and plug in "upma" in each of them.
8676 __isl_give isl_multi_union_pw_aff
*
8677 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8678 __isl_take isl_multi_union_pw_aff
*mupa
,
8679 __isl_take isl_union_pw_multi_aff
*upma
)
8683 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8684 isl_union_pw_multi_aff_get_space(upma
));
8685 upma
= isl_union_pw_multi_aff_align_params(upma
,
8686 isl_multi_union_pw_aff_get_space(mupa
));
8690 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8691 for (i
= 0; i
< n
; ++i
) {
8692 isl_union_pw_aff
*upa
;
8694 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8695 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8696 isl_union_pw_multi_aff_copy(upma
));
8697 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8700 isl_union_pw_multi_aff_free(upma
);
8703 isl_multi_union_pw_aff_free(mupa
);
8704 isl_union_pw_multi_aff_free(upma
);
8708 /* Extract the sequence of elements in "mupa" with domain space "space"
8709 * (ignoring parameters).
8711 * For the elements of "mupa" that are not defined on the specified space,
8712 * the corresponding element in the result is empty.
8714 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8715 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8718 isl_space
*space_mpa
= NULL
;
8719 isl_multi_pw_aff
*mpa
;
8721 if (!mupa
|| !space
)
8724 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8725 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8726 space
= isl_space_drop_dims(space
, isl_dim_param
,
8727 0, isl_space_dim(space
, isl_dim_param
));
8728 space
= isl_space_align_params(space
,
8729 isl_space_copy(space_mpa
));
8733 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8735 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8737 space
= isl_space_from_domain(space
);
8738 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8739 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8740 for (i
= 0; i
< n
; ++i
) {
8741 isl_union_pw_aff
*upa
;
8744 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8745 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8746 isl_space_copy(space
));
8747 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8748 isl_union_pw_aff_free(upa
);
8751 isl_space_free(space
);
8754 isl_space_free(space_mpa
);
8755 isl_space_free(space
);