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 reordering.
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
)
463 isl_bool equal_params
;
468 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
469 if (equal_params
< 0)
474 model
= isl_space_drop_dims(model
, isl_dim_in
,
475 0, isl_space_dim(model
, isl_dim_in
));
476 model
= isl_space_drop_dims(model
, isl_dim_out
,
477 0, isl_space_dim(model
, isl_dim_out
));
478 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
479 exp
= isl_reordering_extend_space(exp
,
480 isl_aff_get_domain_space(aff
));
481 aff
= isl_aff_realign_domain(aff
, exp
);
484 isl_space_free(model
);
487 isl_space_free(model
);
492 /* Is "aff" obviously equal to zero?
494 * If the denominator is zero, then "aff" is not equal to zero.
496 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
499 return isl_bool_error
;
501 if (isl_int_is_zero(aff
->v
->el
[0]))
502 return isl_bool_false
;
503 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
506 /* Does "aff" represent NaN?
508 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
511 return isl_bool_error
;
513 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
516 /* Are "aff1" and "aff2" obviously equal?
518 * NaN is not equal to anything, not even to another NaN.
520 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
521 __isl_keep isl_aff
*aff2
)
526 return isl_bool_error
;
528 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
529 return isl_bool_false
;
531 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
532 if (equal
< 0 || !equal
)
535 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
538 /* Return the common denominator of "aff" in "v".
540 * We cannot return anything meaningful in case of a NaN.
542 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
545 return isl_stat_error
;
546 if (isl_aff_is_nan(aff
))
547 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
548 "cannot get denominator of NaN", return isl_stat_error
);
549 isl_int_set(*v
, aff
->v
->el
[0]);
553 /* Return the common denominator of "aff".
555 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
562 ctx
= isl_aff_get_ctx(aff
);
563 if (isl_aff_is_nan(aff
))
564 return isl_val_nan(ctx
);
565 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
568 /* Return the constant term of "aff" in "v".
570 * We cannot return anything meaningful in case of a NaN.
572 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
576 if (isl_aff_is_nan(aff
))
577 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
578 "cannot get constant term of NaN", return -1);
579 isl_int_set(*v
, aff
->v
->el
[1]);
583 /* Return the constant term of "aff".
585 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
593 ctx
= isl_aff_get_ctx(aff
);
594 if (isl_aff_is_nan(aff
))
595 return isl_val_nan(ctx
);
596 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
597 return isl_val_normalize(v
);
600 /* Return the coefficient of the variable of type "type" at position "pos"
603 * We cannot return anything meaningful in case of a NaN.
605 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
606 enum isl_dim_type type
, int pos
, isl_int
*v
)
611 if (type
== isl_dim_out
)
612 isl_die(aff
->v
->ctx
, isl_error_invalid
,
613 "output/set dimension does not have a coefficient",
615 if (type
== isl_dim_in
)
618 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
619 isl_die(aff
->v
->ctx
, isl_error_invalid
,
620 "position out of bounds", return -1);
622 if (isl_aff_is_nan(aff
))
623 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
624 "cannot get coefficient of NaN", return -1);
625 pos
+= isl_local_space_offset(aff
->ls
, type
);
626 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
631 /* Return the coefficient of the variable of type "type" at position "pos"
634 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
635 enum isl_dim_type type
, int pos
)
643 ctx
= isl_aff_get_ctx(aff
);
644 if (type
== isl_dim_out
)
645 isl_die(ctx
, isl_error_invalid
,
646 "output/set dimension does not have a coefficient",
648 if (type
== isl_dim_in
)
651 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
652 isl_die(ctx
, isl_error_invalid
,
653 "position out of bounds", return NULL
);
655 if (isl_aff_is_nan(aff
))
656 return isl_val_nan(ctx
);
657 pos
+= isl_local_space_offset(aff
->ls
, type
);
658 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
659 return isl_val_normalize(v
);
662 /* Return the sign of the coefficient of the variable of type "type"
663 * at position "pos" of "aff".
665 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
673 ctx
= isl_aff_get_ctx(aff
);
674 if (type
== isl_dim_out
)
675 isl_die(ctx
, isl_error_invalid
,
676 "output/set dimension does not have a coefficient",
678 if (type
== isl_dim_in
)
681 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
682 isl_die(ctx
, isl_error_invalid
,
683 "position out of bounds", return 0);
685 pos
+= isl_local_space_offset(aff
->ls
, type
);
686 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
689 /* Replace the denominator of "aff" by "v".
691 * A NaN is unaffected by this operation.
693 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
697 if (isl_aff_is_nan(aff
))
699 aff
= isl_aff_cow(aff
);
703 aff
->v
= isl_vec_cow(aff
->v
);
705 return isl_aff_free(aff
);
707 isl_int_set(aff
->v
->el
[0], v
);
712 /* Replace the numerator of the constant term of "aff" by "v".
714 * A NaN is unaffected by this operation.
716 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
720 if (isl_aff_is_nan(aff
))
722 aff
= isl_aff_cow(aff
);
726 aff
->v
= isl_vec_cow(aff
->v
);
728 return isl_aff_free(aff
);
730 isl_int_set(aff
->v
->el
[1], v
);
735 /* Replace the constant term of "aff" by "v".
737 * A NaN is unaffected by this operation.
739 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
740 __isl_take isl_val
*v
)
745 if (isl_aff_is_nan(aff
)) {
750 if (!isl_val_is_rat(v
))
751 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
752 "expecting rational value", goto error
);
754 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
755 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
760 aff
= isl_aff_cow(aff
);
763 aff
->v
= isl_vec_cow(aff
->v
);
767 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
768 isl_int_set(aff
->v
->el
[1], v
->n
);
769 } else if (isl_int_is_one(v
->d
)) {
770 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
772 isl_seq_scale(aff
->v
->el
+ 1,
773 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
774 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
775 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
776 aff
->v
= isl_vec_normalize(aff
->v
);
789 /* Add "v" to the constant term of "aff".
791 * A NaN is unaffected by this operation.
793 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
795 if (isl_int_is_zero(v
))
800 if (isl_aff_is_nan(aff
))
802 aff
= isl_aff_cow(aff
);
806 aff
->v
= isl_vec_cow(aff
->v
);
808 return isl_aff_free(aff
);
810 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
815 /* Add "v" to the constant term of "aff".
817 * A NaN is unaffected by this operation.
819 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
820 __isl_take isl_val
*v
)
825 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
830 if (!isl_val_is_rat(v
))
831 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
832 "expecting rational value", goto error
);
834 aff
= isl_aff_cow(aff
);
838 aff
->v
= isl_vec_cow(aff
->v
);
842 if (isl_int_is_one(v
->d
)) {
843 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
844 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
845 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
846 aff
->v
= isl_vec_normalize(aff
->v
);
850 isl_seq_scale(aff
->v
->el
+ 1,
851 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
852 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
853 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
854 aff
->v
= isl_vec_normalize(aff
->v
);
867 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
872 isl_int_set_si(t
, v
);
873 aff
= isl_aff_add_constant(aff
, t
);
879 /* Add "v" to the numerator of the constant term of "aff".
881 * A NaN is unaffected by this operation.
883 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
885 if (isl_int_is_zero(v
))
890 if (isl_aff_is_nan(aff
))
892 aff
= isl_aff_cow(aff
);
896 aff
->v
= isl_vec_cow(aff
->v
);
898 return isl_aff_free(aff
);
900 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
905 /* Add "v" to the numerator of the constant term of "aff".
907 * A NaN is unaffected by this operation.
909 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
917 isl_int_set_si(t
, v
);
918 aff
= isl_aff_add_constant_num(aff
, t
);
924 /* Replace the numerator of the constant term of "aff" by "v".
926 * A NaN is unaffected by this operation.
928 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
932 if (isl_aff_is_nan(aff
))
934 aff
= isl_aff_cow(aff
);
938 aff
->v
= isl_vec_cow(aff
->v
);
940 return isl_aff_free(aff
);
942 isl_int_set_si(aff
->v
->el
[1], v
);
947 /* Replace the numerator of the coefficient of the variable of type "type"
948 * at position "pos" of "aff" by "v".
950 * A NaN is unaffected by this operation.
952 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
953 enum isl_dim_type type
, int pos
, isl_int v
)
958 if (type
== isl_dim_out
)
959 isl_die(aff
->v
->ctx
, isl_error_invalid
,
960 "output/set dimension does not have a coefficient",
961 return isl_aff_free(aff
));
962 if (type
== isl_dim_in
)
965 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
966 isl_die(aff
->v
->ctx
, isl_error_invalid
,
967 "position out of bounds", return isl_aff_free(aff
));
969 if (isl_aff_is_nan(aff
))
971 aff
= isl_aff_cow(aff
);
975 aff
->v
= isl_vec_cow(aff
->v
);
977 return isl_aff_free(aff
);
979 pos
+= isl_local_space_offset(aff
->ls
, type
);
980 isl_int_set(aff
->v
->el
[1 + pos
], v
);
985 /* Replace the numerator of the coefficient of the variable of type "type"
986 * at position "pos" of "aff" by "v".
988 * A NaN is unaffected by this operation.
990 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
991 enum isl_dim_type type
, int pos
, int v
)
996 if (type
== isl_dim_out
)
997 isl_die(aff
->v
->ctx
, isl_error_invalid
,
998 "output/set dimension does not have a coefficient",
999 return isl_aff_free(aff
));
1000 if (type
== isl_dim_in
)
1003 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1004 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1005 "position out of bounds", return isl_aff_free(aff
));
1007 if (isl_aff_is_nan(aff
))
1009 pos
+= isl_local_space_offset(aff
->ls
, type
);
1010 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1013 aff
= isl_aff_cow(aff
);
1017 aff
->v
= isl_vec_cow(aff
->v
);
1019 return isl_aff_free(aff
);
1021 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1026 /* Replace the coefficient of the variable of type "type" at position "pos"
1029 * A NaN is unaffected by this operation.
1031 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1032 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1037 if (type
== isl_dim_out
)
1038 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1039 "output/set dimension does not have a coefficient",
1041 if (type
== isl_dim_in
)
1044 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1045 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1046 "position out of bounds", goto error
);
1048 if (isl_aff_is_nan(aff
)) {
1052 if (!isl_val_is_rat(v
))
1053 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1054 "expecting rational value", goto error
);
1056 pos
+= isl_local_space_offset(aff
->ls
, type
);
1057 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1058 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1063 aff
= isl_aff_cow(aff
);
1066 aff
->v
= isl_vec_cow(aff
->v
);
1070 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1071 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1072 } else if (isl_int_is_one(v
->d
)) {
1073 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1075 isl_seq_scale(aff
->v
->el
+ 1,
1076 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1077 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1078 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1079 aff
->v
= isl_vec_normalize(aff
->v
);
1092 /* Add "v" to the coefficient of the variable of type "type"
1093 * at position "pos" of "aff".
1095 * A NaN is unaffected by this operation.
1097 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1098 enum isl_dim_type type
, int pos
, isl_int v
)
1103 if (type
== isl_dim_out
)
1104 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1105 "output/set dimension does not have a coefficient",
1106 return isl_aff_free(aff
));
1107 if (type
== isl_dim_in
)
1110 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1111 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1112 "position out of bounds", return isl_aff_free(aff
));
1114 if (isl_aff_is_nan(aff
))
1116 aff
= isl_aff_cow(aff
);
1120 aff
->v
= isl_vec_cow(aff
->v
);
1122 return isl_aff_free(aff
);
1124 pos
+= isl_local_space_offset(aff
->ls
, type
);
1125 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1130 /* Add "v" to the coefficient of the variable of type "type"
1131 * at position "pos" of "aff".
1133 * A NaN is unaffected by this operation.
1135 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1136 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1141 if (isl_val_is_zero(v
)) {
1146 if (type
== isl_dim_out
)
1147 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1148 "output/set dimension does not have a coefficient",
1150 if (type
== isl_dim_in
)
1153 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1154 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1155 "position out of bounds", goto error
);
1157 if (isl_aff_is_nan(aff
)) {
1161 if (!isl_val_is_rat(v
))
1162 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1163 "expecting rational value", goto error
);
1165 aff
= isl_aff_cow(aff
);
1169 aff
->v
= isl_vec_cow(aff
->v
);
1173 pos
+= isl_local_space_offset(aff
->ls
, type
);
1174 if (isl_int_is_one(v
->d
)) {
1175 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1176 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1177 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1178 aff
->v
= isl_vec_normalize(aff
->v
);
1182 isl_seq_scale(aff
->v
->el
+ 1,
1183 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1184 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1185 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1186 aff
->v
= isl_vec_normalize(aff
->v
);
1199 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1200 enum isl_dim_type type
, int pos
, int v
)
1205 isl_int_set_si(t
, v
);
1206 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1212 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1217 return isl_local_space_get_div(aff
->ls
, pos
);
1220 /* Return the negation of "aff".
1222 * As a special case, -NaN = NaN.
1224 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1228 if (isl_aff_is_nan(aff
))
1230 aff
= isl_aff_cow(aff
);
1233 aff
->v
= isl_vec_cow(aff
->v
);
1235 return isl_aff_free(aff
);
1237 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1242 /* Remove divs from the local space that do not appear in the affine
1244 * We currently only remove divs at the end.
1245 * Some intermediate divs may also not appear directly in the affine
1246 * expression, but we would also need to check that no other divs are
1247 * defined in terms of them.
1249 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1258 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1259 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1261 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1265 aff
= isl_aff_cow(aff
);
1269 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1270 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1271 if (!aff
->ls
|| !aff
->v
)
1272 return isl_aff_free(aff
);
1277 /* Given two affine expressions "p" of length p_len (including the
1278 * denominator and the constant term) and "subs" of length subs_len,
1279 * plug in "subs" for the variable at position "pos".
1280 * The variables of "subs" and "p" are assumed to match up to subs_len,
1281 * but "p" may have additional variables.
1282 * "v" is an initialized isl_int that can be used internally.
1284 * In particular, if "p" represents the expression
1288 * with i the variable at position "pos" and "subs" represents the expression
1292 * then the result represents the expression
1297 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1298 int p_len
, int subs_len
, isl_int v
)
1300 isl_int_set(v
, p
[1 + pos
]);
1301 isl_int_set_si(p
[1 + pos
], 0);
1302 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1303 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1304 isl_int_mul(p
[0], p
[0], subs
[0]);
1307 /* Look for any divs in the aff->ls with a denominator equal to one
1308 * and plug them into the affine expression and any subsequent divs
1309 * that may reference the div.
1311 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1317 isl_local_space
*ls
;
1323 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1325 for (i
= 0; i
< n
; ++i
) {
1326 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1328 ls
= isl_local_space_copy(aff
->ls
);
1329 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1330 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1331 vec
= isl_vec_copy(aff
->v
);
1332 vec
= isl_vec_cow(vec
);
1338 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1339 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1344 isl_vec_free(aff
->v
);
1346 isl_local_space_free(aff
->ls
);
1353 isl_local_space_free(ls
);
1354 return isl_aff_free(aff
);
1357 /* Look for any divs j that appear with a unit coefficient inside
1358 * the definitions of other divs i and plug them into the definitions
1361 * In particular, an expression of the form
1363 * floor((f(..) + floor(g(..)/n))/m)
1367 * floor((n * f(..) + g(..))/(n * m))
1369 * This simplification is correct because we can move the expression
1370 * f(..) into the inner floor in the original expression to obtain
1372 * floor(floor((n * f(..) + g(..))/n)/m)
1374 * from which we can derive the simplified expression.
1376 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1384 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1385 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1386 for (i
= 1; i
< n
; ++i
) {
1387 for (j
= 0; j
< i
; ++j
) {
1388 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1390 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1391 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1392 aff
->v
->size
, i
, 1);
1394 return isl_aff_free(aff
);
1401 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1403 * Even though this function is only called on isl_affs with a single
1404 * reference, we are careful to only change aff->v and aff->ls together.
1406 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1408 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1409 isl_local_space
*ls
;
1412 ls
= isl_local_space_copy(aff
->ls
);
1413 ls
= isl_local_space_swap_div(ls
, a
, b
);
1414 v
= isl_vec_copy(aff
->v
);
1419 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1420 isl_vec_free(aff
->v
);
1422 isl_local_space_free(aff
->ls
);
1428 isl_local_space_free(ls
);
1429 return isl_aff_free(aff
);
1432 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1434 * We currently do not actually remove div "b", but simply add its
1435 * coefficient to that of "a" and then zero it out.
1437 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1439 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1441 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1444 aff
->v
= isl_vec_cow(aff
->v
);
1446 return isl_aff_free(aff
);
1448 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1449 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1450 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1455 /* Sort the divs in the local space of "aff" according to
1456 * the comparison function "cmp_row" in isl_local_space.c,
1457 * combining the coefficients of identical divs.
1459 * Reordering divs does not change the semantics of "aff",
1460 * so there is no need to call isl_aff_cow.
1461 * Moreover, this function is currently only called on isl_affs
1462 * with a single reference.
1464 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1471 n
= isl_aff_dim(aff
, isl_dim_div
);
1472 for (i
= 1; i
< n
; ++i
) {
1473 for (j
= i
- 1; j
>= 0; --j
) {
1474 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1478 aff
= merge_divs(aff
, j
, j
+ 1);
1480 aff
= swap_div(aff
, j
, j
+ 1);
1489 /* Normalize the representation of "aff".
1491 * This function should only be called of "new" isl_affs, i.e.,
1492 * with only a single reference. We therefore do not need to
1493 * worry about affecting other instances.
1495 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1499 aff
->v
= isl_vec_normalize(aff
->v
);
1501 return isl_aff_free(aff
);
1502 aff
= plug_in_integral_divs(aff
);
1503 aff
= plug_in_unit_divs(aff
);
1504 aff
= sort_divs(aff
);
1505 aff
= isl_aff_remove_unused_divs(aff
);
1509 /* Given f, return floor(f).
1510 * If f is an integer expression, then just return f.
1511 * If f is a constant, then return the constant floor(f).
1512 * Otherwise, if f = g/m, write g = q m + r,
1513 * create a new div d = [r/m] and return the expression q + d.
1514 * The coefficients in r are taken to lie between -m/2 and m/2.
1516 * reduce_div_coefficients performs the same normalization.
1518 * As a special case, floor(NaN) = NaN.
1520 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1530 if (isl_aff_is_nan(aff
))
1532 if (isl_int_is_one(aff
->v
->el
[0]))
1535 aff
= isl_aff_cow(aff
);
1539 aff
->v
= isl_vec_cow(aff
->v
);
1541 return isl_aff_free(aff
);
1543 if (isl_aff_is_cst(aff
)) {
1544 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1545 isl_int_set_si(aff
->v
->el
[0], 1);
1549 div
= isl_vec_copy(aff
->v
);
1550 div
= isl_vec_cow(div
);
1552 return isl_aff_free(aff
);
1554 ctx
= isl_aff_get_ctx(aff
);
1555 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1556 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1557 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1558 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1559 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1560 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1561 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1565 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1567 return isl_aff_free(aff
);
1569 size
= aff
->v
->size
;
1570 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1572 return isl_aff_free(aff
);
1573 isl_int_set_si(aff
->v
->el
[0], 1);
1574 isl_int_set_si(aff
->v
->el
[size
], 1);
1576 aff
= isl_aff_normalize(aff
);
1583 * aff mod m = aff - m * floor(aff/m)
1585 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1589 res
= isl_aff_copy(aff
);
1590 aff
= isl_aff_scale_down(aff
, m
);
1591 aff
= isl_aff_floor(aff
);
1592 aff
= isl_aff_scale(aff
, m
);
1593 res
= isl_aff_sub(res
, aff
);
1600 * aff mod m = aff - m * floor(aff/m)
1602 * with m an integer value.
1604 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1605 __isl_take isl_val
*m
)
1612 if (!isl_val_is_int(m
))
1613 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1614 "expecting integer modulo", goto error
);
1616 res
= isl_aff_copy(aff
);
1617 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1618 aff
= isl_aff_floor(aff
);
1619 aff
= isl_aff_scale_val(aff
, m
);
1620 res
= isl_aff_sub(res
, aff
);
1631 * pwaff mod m = pwaff - m * floor(pwaff/m)
1633 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1637 res
= isl_pw_aff_copy(pwaff
);
1638 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1639 pwaff
= isl_pw_aff_floor(pwaff
);
1640 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1641 res
= isl_pw_aff_sub(res
, pwaff
);
1648 * pa mod m = pa - m * floor(pa/m)
1650 * with m an integer value.
1652 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1653 __isl_take isl_val
*m
)
1657 if (!isl_val_is_int(m
))
1658 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1659 "expecting integer modulo", goto error
);
1660 pa
= isl_pw_aff_mod(pa
, m
->n
);
1664 isl_pw_aff_free(pa
);
1669 /* Given f, return ceil(f).
1670 * If f is an integer expression, then just return f.
1671 * Otherwise, let f be the expression
1677 * floor((e + m - 1)/m)
1679 * As a special case, ceil(NaN) = NaN.
1681 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1686 if (isl_aff_is_nan(aff
))
1688 if (isl_int_is_one(aff
->v
->el
[0]))
1691 aff
= isl_aff_cow(aff
);
1694 aff
->v
= isl_vec_cow(aff
->v
);
1696 return isl_aff_free(aff
);
1698 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1699 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1700 aff
= isl_aff_floor(aff
);
1705 /* Apply the expansion computed by isl_merge_divs.
1706 * The expansion itself is given by "exp" while the resulting
1707 * list of divs is given by "div".
1709 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1710 __isl_take isl_mat
*div
, int *exp
)
1716 aff
= isl_aff_cow(aff
);
1720 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1721 new_n_div
= isl_mat_rows(div
);
1722 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1724 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1725 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1726 if (!aff
->v
|| !aff
->ls
)
1727 return isl_aff_free(aff
);
1735 /* Add two affine expressions that live in the same local space.
1737 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1738 __isl_take isl_aff
*aff2
)
1742 aff1
= isl_aff_cow(aff1
);
1746 aff1
->v
= isl_vec_cow(aff1
->v
);
1752 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1753 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1754 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1755 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1756 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1757 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1758 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1770 /* Return the sum of "aff1" and "aff2".
1772 * If either of the two is NaN, then the result is NaN.
1774 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1775 __isl_take isl_aff
*aff2
)
1786 ctx
= isl_aff_get_ctx(aff1
);
1787 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1788 isl_die(ctx
, isl_error_invalid
,
1789 "spaces don't match", goto error
);
1791 if (isl_aff_is_nan(aff1
)) {
1795 if (isl_aff_is_nan(aff2
)) {
1800 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1801 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1802 if (n_div1
== 0 && n_div2
== 0)
1803 return add_expanded(aff1
, aff2
);
1805 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1806 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1807 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1810 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1811 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1812 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1816 return add_expanded(aff1
, aff2
);
1825 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1826 __isl_take isl_aff
*aff2
)
1828 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1831 /* Return the result of scaling "aff" by a factor of "f".
1833 * As a special case, f * NaN = NaN.
1835 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1841 if (isl_aff_is_nan(aff
))
1844 if (isl_int_is_one(f
))
1847 aff
= isl_aff_cow(aff
);
1850 aff
->v
= isl_vec_cow(aff
->v
);
1852 return isl_aff_free(aff
);
1854 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1855 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1860 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1861 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1862 isl_int_divexact(gcd
, f
, gcd
);
1863 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1869 /* Multiple "aff" by "v".
1871 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1872 __isl_take isl_val
*v
)
1877 if (isl_val_is_one(v
)) {
1882 if (!isl_val_is_rat(v
))
1883 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1884 "expecting rational factor", goto error
);
1886 aff
= isl_aff_scale(aff
, v
->n
);
1887 aff
= isl_aff_scale_down(aff
, v
->d
);
1897 /* Return the result of scaling "aff" down by a factor of "f".
1899 * As a special case, NaN/f = NaN.
1901 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1907 if (isl_aff_is_nan(aff
))
1910 if (isl_int_is_one(f
))
1913 aff
= isl_aff_cow(aff
);
1917 if (isl_int_is_zero(f
))
1918 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1919 "cannot scale down by zero", return isl_aff_free(aff
));
1921 aff
->v
= isl_vec_cow(aff
->v
);
1923 return isl_aff_free(aff
);
1926 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1927 isl_int_gcd(gcd
, gcd
, f
);
1928 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1929 isl_int_divexact(gcd
, f
, gcd
);
1930 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1936 /* Divide "aff" by "v".
1938 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1939 __isl_take isl_val
*v
)
1944 if (isl_val_is_one(v
)) {
1949 if (!isl_val_is_rat(v
))
1950 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1951 "expecting rational factor", goto error
);
1952 if (!isl_val_is_pos(v
))
1953 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1954 "factor needs to be positive", goto error
);
1956 aff
= isl_aff_scale(aff
, v
->d
);
1957 aff
= isl_aff_scale_down(aff
, v
->n
);
1967 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1975 isl_int_set_ui(v
, f
);
1976 aff
= isl_aff_scale_down(aff
, v
);
1982 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1983 enum isl_dim_type type
, unsigned pos
, const char *s
)
1985 aff
= isl_aff_cow(aff
);
1988 if (type
== isl_dim_out
)
1989 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1990 "cannot set name of output/set dimension",
1991 return isl_aff_free(aff
));
1992 if (type
== isl_dim_in
)
1994 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1996 return isl_aff_free(aff
);
2001 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2002 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2004 aff
= isl_aff_cow(aff
);
2007 if (type
== isl_dim_out
)
2008 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2009 "cannot set name of output/set dimension",
2011 if (type
== isl_dim_in
)
2013 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2015 return isl_aff_free(aff
);
2024 /* Replace the identifier of the input tuple of "aff" by "id".
2025 * type is currently required to be equal to isl_dim_in
2027 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2028 enum isl_dim_type type
, __isl_take isl_id
*id
)
2030 aff
= isl_aff_cow(aff
);
2033 if (type
!= isl_dim_out
)
2034 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2035 "cannot only set id of input tuple", goto error
);
2036 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2038 return isl_aff_free(aff
);
2047 /* Exploit the equalities in "eq" to simplify the affine expression
2048 * and the expressions of the integer divisions in the local space.
2049 * The integer divisions in this local space are assumed to appear
2050 * as regular dimensions in "eq".
2052 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2053 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2061 if (eq
->n_eq
== 0) {
2062 isl_basic_set_free(eq
);
2066 aff
= isl_aff_cow(aff
);
2070 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2071 isl_basic_set_copy(eq
));
2072 aff
->v
= isl_vec_cow(aff
->v
);
2073 if (!aff
->ls
|| !aff
->v
)
2076 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2078 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2079 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2080 if (j
< 0 || j
== 0 || j
>= total
)
2083 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2087 isl_basic_set_free(eq
);
2088 aff
= isl_aff_normalize(aff
);
2091 isl_basic_set_free(eq
);
2096 /* Exploit the equalities in "eq" to simplify the affine expression
2097 * and the expressions of the integer divisions in the local space.
2099 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2100 __isl_take isl_basic_set
*eq
)
2106 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2108 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2109 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2111 isl_basic_set_free(eq
);
2116 /* Look for equalities among the variables shared by context and aff
2117 * and the integer divisions of aff, if any.
2118 * The equalities are then used to eliminate coefficients and/or integer
2119 * divisions from aff.
2121 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2122 __isl_take isl_set
*context
)
2124 isl_basic_set
*hull
;
2129 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2131 isl_basic_set
*bset
;
2132 isl_local_space
*ls
;
2133 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2134 ls
= isl_aff_get_domain_local_space(aff
);
2135 bset
= isl_basic_set_from_local_space(ls
);
2136 bset
= isl_basic_set_lift(bset
);
2137 bset
= isl_basic_set_flatten(bset
);
2138 context
= isl_set_intersect(context
,
2139 isl_set_from_basic_set(bset
));
2142 hull
= isl_set_affine_hull(context
);
2143 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2146 isl_set_free(context
);
2150 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2151 __isl_take isl_set
*context
)
2153 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2154 dom_context
= isl_set_intersect_params(dom_context
, context
);
2155 return isl_aff_gist(aff
, dom_context
);
2158 /* Return a basic set containing those elements in the space
2159 * of aff where it is positive. "rational" should not be set.
2161 * If "aff" is NaN, then it is not positive.
2163 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2166 isl_constraint
*ineq
;
2167 isl_basic_set
*bset
;
2172 if (isl_aff_is_nan(aff
)) {
2173 isl_space
*space
= isl_aff_get_domain_space(aff
);
2175 return isl_basic_set_empty(space
);
2178 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2179 "rational sets not supported", goto error
);
2181 ineq
= isl_inequality_from_aff(aff
);
2182 c
= isl_constraint_get_constant_val(ineq
);
2183 c
= isl_val_sub_ui(c
, 1);
2184 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2186 bset
= isl_basic_set_from_constraint(ineq
);
2187 bset
= isl_basic_set_simplify(bset
);
2194 /* Return a basic set containing those elements in the space
2195 * of aff where it is non-negative.
2196 * If "rational" is set, then return a rational basic set.
2198 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2200 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2201 __isl_take isl_aff
*aff
, int rational
)
2203 isl_constraint
*ineq
;
2204 isl_basic_set
*bset
;
2208 if (isl_aff_is_nan(aff
)) {
2209 isl_space
*space
= isl_aff_get_domain_space(aff
);
2211 return isl_basic_set_empty(space
);
2214 ineq
= isl_inequality_from_aff(aff
);
2216 bset
= isl_basic_set_from_constraint(ineq
);
2218 bset
= isl_basic_set_set_rational(bset
);
2219 bset
= isl_basic_set_simplify(bset
);
2223 /* Return a basic set containing those elements in the space
2224 * of aff where it is non-negative.
2226 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2228 return aff_nonneg_basic_set(aff
, 0);
2231 /* Return a basic set containing those elements in the domain space
2232 * of aff where it is negative.
2234 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2236 aff
= isl_aff_neg(aff
);
2237 aff
= isl_aff_add_constant_num_si(aff
, -1);
2238 return isl_aff_nonneg_basic_set(aff
);
2241 /* Return a basic set containing those elements in the space
2242 * of aff where it is zero.
2243 * If "rational" is set, then return a rational basic set.
2245 * If "aff" is NaN, then it is not zero.
2247 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2250 isl_constraint
*ineq
;
2251 isl_basic_set
*bset
;
2255 if (isl_aff_is_nan(aff
)) {
2256 isl_space
*space
= isl_aff_get_domain_space(aff
);
2258 return isl_basic_set_empty(space
);
2261 ineq
= isl_equality_from_aff(aff
);
2263 bset
= isl_basic_set_from_constraint(ineq
);
2265 bset
= isl_basic_set_set_rational(bset
);
2266 bset
= isl_basic_set_simplify(bset
);
2270 /* Return a basic set containing those elements in the space
2271 * of aff where it is zero.
2273 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2275 return aff_zero_basic_set(aff
, 0);
2278 /* Return a basic set containing those elements in the shared space
2279 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2281 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2282 __isl_take isl_aff
*aff2
)
2284 aff1
= isl_aff_sub(aff1
, aff2
);
2286 return isl_aff_nonneg_basic_set(aff1
);
2289 /* Return a set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2292 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2293 __isl_take isl_aff
*aff2
)
2295 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2298 /* Return a basic set containing those elements in the shared space
2299 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2301 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2302 __isl_take isl_aff
*aff2
)
2304 return isl_aff_ge_basic_set(aff2
, aff1
);
2307 /* Return a set containing those elements in the shared space
2308 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2310 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2311 __isl_take isl_aff
*aff2
)
2313 return isl_aff_ge_set(aff2
, aff1
);
2316 /* Return a basic set containing those elements in the shared space
2317 * of aff1 and aff2 where aff1 and aff2 are equal.
2319 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2320 __isl_take isl_aff
*aff2
)
2322 aff1
= isl_aff_sub(aff1
, aff2
);
2324 return isl_aff_zero_basic_set(aff1
);
2327 /* Return a set containing those elements in the shared space
2328 * of aff1 and aff2 where aff1 and aff2 are equal.
2330 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2331 __isl_take isl_aff
*aff2
)
2333 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2336 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2337 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2339 aff1
= isl_aff_add(aff1
, aff2
);
2340 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2344 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2352 /* Check whether the given affine expression has non-zero coefficient
2353 * for any dimension in the given range or if any of these dimensions
2354 * appear with non-zero coefficients in any of the integer divisions
2355 * involved in the affine expression.
2357 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2358 enum isl_dim_type type
, unsigned first
, unsigned n
)
2363 isl_bool involves
= isl_bool_false
;
2366 return isl_bool_error
;
2368 return isl_bool_false
;
2370 ctx
= isl_aff_get_ctx(aff
);
2371 if (first
+ n
> isl_aff_dim(aff
, type
))
2372 isl_die(ctx
, isl_error_invalid
,
2373 "range out of bounds", return isl_bool_error
);
2375 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2379 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2380 for (i
= 0; i
< n
; ++i
)
2381 if (active
[first
+ i
]) {
2382 involves
= isl_bool_true
;
2391 return isl_bool_error
;
2394 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2395 enum isl_dim_type type
, unsigned first
, unsigned n
)
2401 if (type
== isl_dim_out
)
2402 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2403 "cannot drop output/set dimension",
2404 return isl_aff_free(aff
));
2405 if (type
== isl_dim_in
)
2407 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2410 ctx
= isl_aff_get_ctx(aff
);
2411 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2412 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2413 return isl_aff_free(aff
));
2415 aff
= isl_aff_cow(aff
);
2419 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2421 return isl_aff_free(aff
);
2423 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2424 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2426 return isl_aff_free(aff
);
2431 /* Project the domain of the affine expression onto its parameter space.
2432 * The affine expression may not involve any of the domain dimensions.
2434 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2440 n
= isl_aff_dim(aff
, isl_dim_in
);
2441 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2443 return isl_aff_free(aff
);
2445 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2446 "affine expression involves some of the domain dimensions",
2447 return isl_aff_free(aff
));
2448 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2449 space
= isl_aff_get_domain_space(aff
);
2450 space
= isl_space_params(space
);
2451 aff
= isl_aff_reset_domain_space(aff
, space
);
2455 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2456 enum isl_dim_type type
, unsigned first
, unsigned n
)
2462 if (type
== isl_dim_out
)
2463 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2464 "cannot insert output/set dimensions",
2465 return isl_aff_free(aff
));
2466 if (type
== isl_dim_in
)
2468 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2471 ctx
= isl_aff_get_ctx(aff
);
2472 if (first
> isl_local_space_dim(aff
->ls
, type
))
2473 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2474 return isl_aff_free(aff
));
2476 aff
= isl_aff_cow(aff
);
2480 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2482 return isl_aff_free(aff
);
2484 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2485 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2487 return isl_aff_free(aff
);
2492 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2493 enum isl_dim_type type
, unsigned n
)
2497 pos
= isl_aff_dim(aff
, type
);
2499 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2502 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2503 enum isl_dim_type type
, unsigned n
)
2507 pos
= isl_pw_aff_dim(pwaff
, type
);
2509 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2512 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2513 * to dimensions of "dst_type" at "dst_pos".
2515 * We only support moving input dimensions to parameters and vice versa.
2517 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2518 enum isl_dim_type dst_type
, unsigned dst_pos
,
2519 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2527 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2528 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2531 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2532 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2533 "cannot move output/set dimension",
2534 return isl_aff_free(aff
));
2535 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2536 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2537 "cannot move divs", return isl_aff_free(aff
));
2538 if (dst_type
== isl_dim_in
)
2539 dst_type
= isl_dim_set
;
2540 if (src_type
== isl_dim_in
)
2541 src_type
= isl_dim_set
;
2543 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2544 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2545 "range out of bounds", return isl_aff_free(aff
));
2546 if (dst_type
== src_type
)
2547 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2548 "moving dims within the same type not supported",
2549 return isl_aff_free(aff
));
2551 aff
= isl_aff_cow(aff
);
2555 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2556 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2557 if (dst_type
> src_type
)
2560 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2561 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2562 src_type
, src_pos
, n
);
2563 if (!aff
->v
|| !aff
->ls
)
2564 return isl_aff_free(aff
);
2566 aff
= sort_divs(aff
);
2571 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2573 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2574 return isl_pw_aff_alloc(dom
, aff
);
2577 #define isl_aff_involves_nan isl_aff_is_nan
2580 #define PW isl_pw_aff
2584 #define EL_IS_ZERO is_empty
2588 #define IS_ZERO is_empty
2591 #undef DEFAULT_IS_ZERO
2592 #define DEFAULT_IS_ZERO 0
2599 #include <isl_pw_templ.c>
2600 #include <isl_pw_hash.c>
2601 #include <isl_pw_union_opt.c>
2604 #define UNION isl_union_pw_aff
2606 #define PART isl_pw_aff
2608 #define PARTS pw_aff
2610 #include <isl_union_single.c>
2611 #include <isl_union_neg.c>
2613 static __isl_give isl_set
*align_params_pw_pw_set_and(
2614 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2615 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2616 __isl_take isl_pw_aff
*pwaff2
))
2618 isl_bool equal_params
;
2620 if (!pwaff1
|| !pwaff2
)
2622 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2623 if (equal_params
< 0)
2626 return fn(pwaff1
, pwaff2
);
2627 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2628 !isl_space_has_named_params(pwaff2
->dim
))
2629 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2630 "unaligned unnamed parameters", goto error
);
2631 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2632 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2633 return fn(pwaff1
, pwaff2
);
2635 isl_pw_aff_free(pwaff1
);
2636 isl_pw_aff_free(pwaff2
);
2640 /* Align the parameters of the to isl_pw_aff arguments and
2641 * then apply a function "fn" on them that returns an isl_map.
2643 static __isl_give isl_map
*align_params_pw_pw_map_and(
2644 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2645 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2646 __isl_take isl_pw_aff
*pa2
))
2648 isl_bool equal_params
;
2652 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2653 if (equal_params
< 0)
2656 return fn(pa1
, pa2
);
2657 if (!isl_space_has_named_params(pa1
->dim
) ||
2658 !isl_space_has_named_params(pa2
->dim
))
2659 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2660 "unaligned unnamed parameters", goto error
);
2661 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2662 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2663 return fn(pa1
, pa2
);
2665 isl_pw_aff_free(pa1
);
2666 isl_pw_aff_free(pa2
);
2670 /* Compute a piecewise quasi-affine expression with a domain that
2671 * is the union of those of pwaff1 and pwaff2 and such that on each
2672 * cell, the quasi-affine expression is the maximum of those of pwaff1
2673 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2674 * cell, then the associated expression is the defined one.
2676 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2677 __isl_take isl_pw_aff
*pwaff2
)
2679 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2682 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2683 __isl_take isl_pw_aff
*pwaff2
)
2685 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2689 /* Compute a piecewise quasi-affine expression with a domain that
2690 * is the union of those of pwaff1 and pwaff2 and such that on each
2691 * cell, the quasi-affine expression is the minimum of those of pwaff1
2692 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2693 * cell, then the associated expression is the defined one.
2695 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2696 __isl_take isl_pw_aff
*pwaff2
)
2698 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2701 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2702 __isl_take isl_pw_aff
*pwaff2
)
2704 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2708 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2709 __isl_take isl_pw_aff
*pwaff2
, int max
)
2712 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2714 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2717 /* Construct a map with as domain the domain of pwaff and
2718 * one-dimensional range corresponding to the affine expressions.
2720 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2729 dim
= isl_pw_aff_get_space(pwaff
);
2730 map
= isl_map_empty(dim
);
2732 for (i
= 0; i
< pwaff
->n
; ++i
) {
2733 isl_basic_map
*bmap
;
2736 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2737 map_i
= isl_map_from_basic_map(bmap
);
2738 map_i
= isl_map_intersect_domain(map_i
,
2739 isl_set_copy(pwaff
->p
[i
].set
));
2740 map
= isl_map_union_disjoint(map
, map_i
);
2743 isl_pw_aff_free(pwaff
);
2748 /* Construct a map with as domain the domain of pwaff and
2749 * one-dimensional range corresponding to the affine expressions.
2751 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2755 if (isl_space_is_set(pwaff
->dim
))
2756 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2757 "space of input is not a map", goto error
);
2758 return map_from_pw_aff(pwaff
);
2760 isl_pw_aff_free(pwaff
);
2764 /* Construct a one-dimensional set with as parameter domain
2765 * the domain of pwaff and the single set dimension
2766 * corresponding to the affine expressions.
2768 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2772 if (!isl_space_is_set(pwaff
->dim
))
2773 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2774 "space of input is not a set", goto error
);
2775 return map_from_pw_aff(pwaff
);
2777 isl_pw_aff_free(pwaff
);
2781 /* Return a set containing those elements in the domain
2782 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2783 * does not satisfy "fn" (if complement is 1).
2785 * The pieces with a NaN never belong to the result since
2786 * NaN does not satisfy any property.
2788 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2789 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2798 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2800 for (i
= 0; i
< pwaff
->n
; ++i
) {
2801 isl_basic_set
*bset
;
2802 isl_set
*set_i
, *locus
;
2805 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2808 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2809 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2810 locus
= isl_set_from_basic_set(bset
);
2811 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2813 set_i
= isl_set_subtract(set_i
, locus
);
2815 set_i
= isl_set_intersect(set_i
, locus
);
2816 set
= isl_set_union_disjoint(set
, set_i
);
2819 isl_pw_aff_free(pwaff
);
2824 /* Return a set containing those elements in the domain
2825 * of "pa" where it is positive.
2827 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2829 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2832 /* Return a set containing those elements in the domain
2833 * of pwaff where it is non-negative.
2835 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2837 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2840 /* Return a set containing those elements in the domain
2841 * of pwaff where it is zero.
2843 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2845 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2848 /* Return a set containing those elements in the domain
2849 * of pwaff where it is not zero.
2851 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2853 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2856 /* Return a set containing those elements in the shared domain
2857 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2859 * We compute the difference on the shared domain and then construct
2860 * the set of values where this difference is non-negative.
2861 * If strict is set, we first subtract 1 from the difference.
2862 * If equal is set, we only return the elements where pwaff1 and pwaff2
2865 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2866 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2868 isl_set
*set1
, *set2
;
2870 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2871 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2872 set1
= isl_set_intersect(set1
, set2
);
2873 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2874 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2875 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2878 isl_space
*dim
= isl_set_get_space(set1
);
2880 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2881 aff
= isl_aff_add_constant_si(aff
, -1);
2882 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2887 return isl_pw_aff_zero_set(pwaff1
);
2888 return isl_pw_aff_nonneg_set(pwaff1
);
2891 /* Return a set containing those elements in the shared domain
2892 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2894 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2895 __isl_take isl_pw_aff
*pwaff2
)
2897 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2900 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2901 __isl_take isl_pw_aff
*pwaff2
)
2903 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2906 /* Return a set containing those elements in the shared domain
2907 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2909 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2910 __isl_take isl_pw_aff
*pwaff2
)
2912 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2915 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2916 __isl_take isl_pw_aff
*pwaff2
)
2918 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2921 /* Return a set containing those elements in the shared domain
2922 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2924 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2925 __isl_take isl_pw_aff
*pwaff2
)
2927 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2930 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2931 __isl_take isl_pw_aff
*pwaff2
)
2933 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2936 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2937 __isl_take isl_pw_aff
*pwaff2
)
2939 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2942 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2943 __isl_take isl_pw_aff
*pwaff2
)
2945 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2948 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2949 * where the function values are ordered in the same way as "order",
2950 * which returns a set in the shared domain of its two arguments.
2951 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2953 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2954 * We first pull back the two functions such that they are defined on
2955 * the domain [A -> B]. Then we apply "order", resulting in a set
2956 * in the space [A -> B]. Finally, we unwrap this set to obtain
2957 * a map in the space A -> B.
2959 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2960 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2961 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2962 __isl_take isl_pw_aff
*pa2
))
2964 isl_space
*space1
, *space2
;
2968 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2969 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2970 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2971 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2972 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2973 ma
= isl_multi_aff_range_map(space1
);
2974 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2975 set
= order(pa1
, pa2
);
2977 return isl_set_unwrap(set
);
2980 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2981 * where the function values are equal.
2982 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2984 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2985 __isl_take isl_pw_aff
*pa2
)
2987 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2990 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2991 * where the function values are equal.
2993 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2994 __isl_take isl_pw_aff
*pa2
)
2996 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
2999 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3000 * where the function value of "pa1" is less than the function value of "pa2".
3001 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3003 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3004 __isl_take isl_pw_aff
*pa2
)
3006 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3009 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3010 * where the function value of "pa1" is less than the function value of "pa2".
3012 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3013 __isl_take isl_pw_aff
*pa2
)
3015 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3018 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3019 * where the function value of "pa1" is greater than the function value
3021 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3023 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3024 __isl_take isl_pw_aff
*pa2
)
3026 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is greater than the function value
3033 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3034 __isl_take isl_pw_aff
*pa2
)
3036 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3039 /* Return a set containing those elements in the shared domain
3040 * of the elements of list1 and list2 where each element in list1
3041 * has the relation specified by "fn" with each element in list2.
3043 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3044 __isl_take isl_pw_aff_list
*list2
,
3045 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3046 __isl_take isl_pw_aff
*pwaff2
))
3052 if (!list1
|| !list2
)
3055 ctx
= isl_pw_aff_list_get_ctx(list1
);
3056 if (list1
->n
< 1 || list2
->n
< 1)
3057 isl_die(ctx
, isl_error_invalid
,
3058 "list should contain at least one element", goto error
);
3060 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3061 for (i
= 0; i
< list1
->n
; ++i
)
3062 for (j
= 0; j
< list2
->n
; ++j
) {
3065 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3066 isl_pw_aff_copy(list2
->p
[j
]));
3067 set
= isl_set_intersect(set
, set_ij
);
3070 isl_pw_aff_list_free(list1
);
3071 isl_pw_aff_list_free(list2
);
3074 isl_pw_aff_list_free(list1
);
3075 isl_pw_aff_list_free(list2
);
3079 /* Return a set containing those elements in the shared domain
3080 * of the elements of list1 and list2 where each element in list1
3081 * is equal to each element in list2.
3083 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3084 __isl_take isl_pw_aff_list
*list2
)
3086 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3089 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3090 __isl_take isl_pw_aff_list
*list2
)
3092 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3095 /* Return a set containing those elements in the shared domain
3096 * of the elements of list1 and list2 where each element in list1
3097 * is less than or equal to each element in list2.
3099 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3100 __isl_take isl_pw_aff_list
*list2
)
3102 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3105 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3106 __isl_take isl_pw_aff_list
*list2
)
3108 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3111 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3112 __isl_take isl_pw_aff_list
*list2
)
3114 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3117 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3118 __isl_take isl_pw_aff_list
*list2
)
3120 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3124 /* Return a set containing those elements in the shared domain
3125 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3127 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3128 __isl_take isl_pw_aff
*pwaff2
)
3130 isl_set
*set_lt
, *set_gt
;
3132 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3133 isl_pw_aff_copy(pwaff2
));
3134 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3135 return isl_set_union_disjoint(set_lt
, set_gt
);
3138 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3139 __isl_take isl_pw_aff
*pwaff2
)
3141 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3144 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3149 if (isl_int_is_one(v
))
3151 if (!isl_int_is_pos(v
))
3152 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3153 "factor needs to be positive",
3154 return isl_pw_aff_free(pwaff
));
3155 pwaff
= isl_pw_aff_cow(pwaff
);
3161 for (i
= 0; i
< pwaff
->n
; ++i
) {
3162 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3163 if (!pwaff
->p
[i
].aff
)
3164 return isl_pw_aff_free(pwaff
);
3170 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3174 pwaff
= isl_pw_aff_cow(pwaff
);
3180 for (i
= 0; i
< pwaff
->n
; ++i
) {
3181 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3182 if (!pwaff
->p
[i
].aff
)
3183 return isl_pw_aff_free(pwaff
);
3189 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3193 pwaff
= isl_pw_aff_cow(pwaff
);
3199 for (i
= 0; i
< pwaff
->n
; ++i
) {
3200 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3201 if (!pwaff
->p
[i
].aff
)
3202 return isl_pw_aff_free(pwaff
);
3208 /* Assuming that "cond1" and "cond2" are disjoint,
3209 * return an affine expression that is equal to pwaff1 on cond1
3210 * and to pwaff2 on cond2.
3212 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3213 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3214 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3216 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3217 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3219 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3222 /* Return an affine expression that is equal to pwaff_true for elements
3223 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3225 * That is, return cond ? pwaff_true : pwaff_false;
3227 * If "cond" involves and NaN, then we conservatively return a NaN
3228 * on its entire domain. In principle, we could consider the pieces
3229 * where it is NaN separately from those where it is not.
3231 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3232 * then only use the domain of "cond" to restrict the domain.
3234 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3235 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3237 isl_set
*cond_true
, *cond_false
;
3242 if (isl_pw_aff_involves_nan(cond
)) {
3243 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3244 isl_local_space
*ls
= isl_local_space_from_space(space
);
3245 isl_pw_aff_free(cond
);
3246 isl_pw_aff_free(pwaff_true
);
3247 isl_pw_aff_free(pwaff_false
);
3248 return isl_pw_aff_nan_on_domain(ls
);
3251 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3252 isl_pw_aff_get_space(pwaff_false
));
3253 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3254 isl_pw_aff_get_space(pwaff_true
));
3255 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3261 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3262 isl_pw_aff_free(pwaff_false
);
3263 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3266 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3267 cond_false
= isl_pw_aff_zero_set(cond
);
3268 return isl_pw_aff_select(cond_true
, pwaff_true
,
3269 cond_false
, pwaff_false
);
3271 isl_pw_aff_free(cond
);
3272 isl_pw_aff_free(pwaff_true
);
3273 isl_pw_aff_free(pwaff_false
);
3277 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3280 return isl_bool_error
;
3282 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3285 /* Check whether pwaff is a piecewise constant.
3287 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3292 return isl_bool_error
;
3294 for (i
= 0; i
< pwaff
->n
; ++i
) {
3295 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3296 if (is_cst
< 0 || !is_cst
)
3300 return isl_bool_true
;
3303 /* Are all elements of "mpa" piecewise constants?
3305 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3310 return isl_bool_error
;
3312 for (i
= 0; i
< mpa
->n
; ++i
) {
3313 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3314 if (is_cst
< 0 || !is_cst
)
3318 return isl_bool_true
;
3321 /* Return the product of "aff1" and "aff2".
3323 * If either of the two is NaN, then the result is NaN.
3325 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3327 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3328 __isl_take isl_aff
*aff2
)
3333 if (isl_aff_is_nan(aff1
)) {
3337 if (isl_aff_is_nan(aff2
)) {
3342 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3343 return isl_aff_mul(aff2
, aff1
);
3345 if (!isl_aff_is_cst(aff2
))
3346 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3347 "at least one affine expression should be constant",
3350 aff1
= isl_aff_cow(aff1
);
3354 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3355 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3365 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3367 * If either of the two is NaN, then the result is NaN.
3369 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3370 __isl_take isl_aff
*aff2
)
3378 if (isl_aff_is_nan(aff1
)) {
3382 if (isl_aff_is_nan(aff2
)) {
3387 is_cst
= isl_aff_is_cst(aff2
);
3391 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3392 "second argument should be a constant", goto error
);
3397 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3399 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3400 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3403 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3404 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3407 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3408 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3419 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3420 __isl_take isl_pw_aff
*pwaff2
)
3422 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3425 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3426 __isl_take isl_pw_aff
*pwaff2
)
3428 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3431 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3432 __isl_take isl_pw_aff
*pwaff2
)
3434 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3437 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3438 __isl_take isl_pw_aff
*pwaff2
)
3440 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3443 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3444 __isl_take isl_pw_aff
*pwaff2
)
3446 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3449 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3450 __isl_take isl_pw_aff
*pa2
)
3452 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3455 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3457 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3458 __isl_take isl_pw_aff
*pa2
)
3462 is_cst
= isl_pw_aff_is_cst(pa2
);
3466 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3467 "second argument should be a piecewise constant",
3469 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3471 isl_pw_aff_free(pa1
);
3472 isl_pw_aff_free(pa2
);
3476 /* Compute the quotient of the integer division of "pa1" by "pa2"
3477 * with rounding towards zero.
3478 * "pa2" is assumed to be a piecewise constant.
3480 * In particular, return
3482 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3485 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3486 __isl_take isl_pw_aff
*pa2
)
3492 is_cst
= isl_pw_aff_is_cst(pa2
);
3496 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3497 "second argument should be a piecewise constant",
3500 pa1
= isl_pw_aff_div(pa1
, pa2
);
3502 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3503 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3504 c
= isl_pw_aff_ceil(pa1
);
3505 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3507 isl_pw_aff_free(pa1
);
3508 isl_pw_aff_free(pa2
);
3512 /* Compute the remainder of the integer division of "pa1" by "pa2"
3513 * with rounding towards zero.
3514 * "pa2" is assumed to be a piecewise constant.
3516 * In particular, return
3518 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3521 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3522 __isl_take isl_pw_aff
*pa2
)
3527 is_cst
= isl_pw_aff_is_cst(pa2
);
3531 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3532 "second argument should be a piecewise constant",
3534 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3535 res
= isl_pw_aff_mul(pa2
, res
);
3536 res
= isl_pw_aff_sub(pa1
, res
);
3539 isl_pw_aff_free(pa1
);
3540 isl_pw_aff_free(pa2
);
3544 /* Does either of "pa1" or "pa2" involve any NaN2?
3546 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3547 __isl_keep isl_pw_aff
*pa2
)
3551 has_nan
= isl_pw_aff_involves_nan(pa1
);
3552 if (has_nan
< 0 || has_nan
)
3554 return isl_pw_aff_involves_nan(pa2
);
3557 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3558 * by a NaN on their shared domain.
3560 * In principle, the result could be refined to only being NaN
3561 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3563 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3564 __isl_take isl_pw_aff
*pa2
)
3566 isl_local_space
*ls
;
3570 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3571 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3572 pa
= isl_pw_aff_nan_on_domain(ls
);
3573 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3578 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3579 __isl_take isl_pw_aff
*pwaff2
)
3584 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3585 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3586 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3587 isl_pw_aff_copy(pwaff2
));
3588 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3589 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3592 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3593 __isl_take isl_pw_aff
*pwaff2
)
3598 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3599 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3600 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3601 isl_pw_aff_copy(pwaff2
));
3602 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3603 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3606 /* Return an expression for the minimum (if "max" is not set) or
3607 * the maximum (if "max" is set) of "pa1" and "pa2".
3608 * If either expression involves any NaN, then return a NaN
3609 * on the shared domain as result.
3611 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3612 __isl_take isl_pw_aff
*pa2
, int max
)
3616 has_nan
= either_involves_nan(pa1
, pa2
);
3618 pa1
= isl_pw_aff_free(pa1
);
3620 return replace_by_nan(pa1
, pa2
);
3623 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3625 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3628 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3630 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3631 __isl_take isl_pw_aff
*pwaff2
)
3633 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3636 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3638 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3639 __isl_take isl_pw_aff
*pwaff2
)
3641 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3644 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3645 __isl_take isl_pw_aff_list
*list
,
3646 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3647 __isl_take isl_pw_aff
*pwaff2
))
3656 ctx
= isl_pw_aff_list_get_ctx(list
);
3658 isl_die(ctx
, isl_error_invalid
,
3659 "list should contain at least one element", goto error
);
3661 res
= isl_pw_aff_copy(list
->p
[0]);
3662 for (i
= 1; i
< list
->n
; ++i
)
3663 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3665 isl_pw_aff_list_free(list
);
3668 isl_pw_aff_list_free(list
);
3672 /* Return an isl_pw_aff that maps each element in the intersection of the
3673 * domains of the elements of list to the minimal corresponding affine
3676 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3678 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3681 /* Return an isl_pw_aff that maps each element in the intersection of the
3682 * domains of the elements of list to the maximal corresponding affine
3685 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3687 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3690 /* Mark the domains of "pwaff" as rational.
3692 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3696 pwaff
= isl_pw_aff_cow(pwaff
);
3702 for (i
= 0; i
< pwaff
->n
; ++i
) {
3703 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3704 if (!pwaff
->p
[i
].set
)
3705 return isl_pw_aff_free(pwaff
);
3711 /* Mark the domains of the elements of "list" as rational.
3713 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3714 __isl_take isl_pw_aff_list
*list
)
3724 for (i
= 0; i
< n
; ++i
) {
3727 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3728 pa
= isl_pw_aff_set_rational(pa
);
3729 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3735 /* Do the parameters of "aff" match those of "space"?
3737 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3738 __isl_keep isl_space
*space
)
3740 isl_space
*aff_space
;
3744 return isl_bool_error
;
3746 aff_space
= isl_aff_get_domain_space(aff
);
3748 match
= isl_space_has_equal_params(space
, aff_space
);
3750 isl_space_free(aff_space
);
3754 /* Check that the domain space of "aff" matches "space".
3756 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3757 __isl_keep isl_space
*space
)
3759 isl_space
*aff_space
;
3763 return isl_stat_error
;
3765 aff_space
= isl_aff_get_domain_space(aff
);
3767 match
= isl_space_has_equal_params(space
, aff_space
);
3771 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3772 "parameters don't match", goto error
);
3773 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3774 aff_space
, isl_dim_set
);
3778 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3779 "domains don't match", goto error
);
3780 isl_space_free(aff_space
);
3783 isl_space_free(aff_space
);
3784 return isl_stat_error
;
3793 #include <isl_multi_templ.c>
3794 #include <isl_multi_apply_set.c>
3795 #include <isl_multi_cmp.c>
3796 #include <isl_multi_floor.c>
3797 #include <isl_multi_gist.c>
3801 /* Remove any internal structure of the domain of "ma".
3802 * If there is any such internal structure in the input,
3803 * then the name of the corresponding space is also removed.
3805 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3806 __isl_take isl_multi_aff
*ma
)
3813 if (!ma
->space
->nested
[0])
3816 space
= isl_multi_aff_get_space(ma
);
3817 space
= isl_space_flatten_domain(space
);
3818 ma
= isl_multi_aff_reset_space(ma
, space
);
3823 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3824 * of the space to its domain.
3826 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3829 isl_local_space
*ls
;
3834 if (!isl_space_is_map(space
))
3835 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3836 "not a map space", goto error
);
3838 n_in
= isl_space_dim(space
, isl_dim_in
);
3839 space
= isl_space_domain_map(space
);
3841 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3843 isl_space_free(space
);
3847 space
= isl_space_domain(space
);
3848 ls
= isl_local_space_from_space(space
);
3849 for (i
= 0; i
< n_in
; ++i
) {
3852 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3854 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3856 isl_local_space_free(ls
);
3859 isl_space_free(space
);
3863 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3864 * of the space to its range.
3866 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3869 isl_local_space
*ls
;
3874 if (!isl_space_is_map(space
))
3875 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3876 "not a map space", goto error
);
3878 n_in
= isl_space_dim(space
, isl_dim_in
);
3879 n_out
= isl_space_dim(space
, isl_dim_out
);
3880 space
= isl_space_range_map(space
);
3882 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3884 isl_space_free(space
);
3888 space
= isl_space_domain(space
);
3889 ls
= isl_local_space_from_space(space
);
3890 for (i
= 0; i
< n_out
; ++i
) {
3893 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3894 isl_dim_set
, n_in
+ i
);
3895 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3897 isl_local_space_free(ls
);
3900 isl_space_free(space
);
3904 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3905 * of the space to its range.
3907 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3908 __isl_take isl_space
*space
)
3910 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3913 /* Given the space of a set and a range of set dimensions,
3914 * construct an isl_multi_aff that projects out those dimensions.
3916 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3917 __isl_take isl_space
*space
, enum isl_dim_type type
,
3918 unsigned first
, unsigned n
)
3921 isl_local_space
*ls
;
3926 if (!isl_space_is_set(space
))
3927 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3928 "expecting set space", goto error
);
3929 if (type
!= isl_dim_set
)
3930 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3931 "only set dimensions can be projected out", goto error
);
3933 dim
= isl_space_dim(space
, isl_dim_set
);
3934 if (first
+ n
> dim
)
3935 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3936 "range out of bounds", goto error
);
3938 space
= isl_space_from_domain(space
);
3939 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3942 return isl_multi_aff_alloc(space
);
3944 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3945 space
= isl_space_domain(space
);
3946 ls
= isl_local_space_from_space(space
);
3948 for (i
= 0; i
< first
; ++i
) {
3951 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3953 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3956 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3959 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3960 isl_dim_set
, first
+ n
+ i
);
3961 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3964 isl_local_space_free(ls
);
3967 isl_space_free(space
);
3971 /* Given the space of a set and a range of set dimensions,
3972 * construct an isl_pw_multi_aff that projects out those dimensions.
3974 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3975 __isl_take isl_space
*space
, enum isl_dim_type type
,
3976 unsigned first
, unsigned n
)
3980 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3981 return isl_pw_multi_aff_from_multi_aff(ma
);
3984 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3987 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3988 __isl_take isl_multi_aff
*ma
)
3990 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3991 return isl_pw_multi_aff_alloc(dom
, ma
);
3994 /* Create a piecewise multi-affine expression in the given space that maps each
3995 * input dimension to the corresponding output dimension.
3997 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3998 __isl_take isl_space
*space
)
4000 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4003 /* Exploit the equalities in "eq" to simplify the affine expressions.
4005 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4006 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4010 maff
= isl_multi_aff_cow(maff
);
4014 for (i
= 0; i
< maff
->n
; ++i
) {
4015 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
4016 isl_basic_set_copy(eq
));
4021 isl_basic_set_free(eq
);
4024 isl_basic_set_free(eq
);
4025 isl_multi_aff_free(maff
);
4029 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4034 maff
= isl_multi_aff_cow(maff
);
4038 for (i
= 0; i
< maff
->n
; ++i
) {
4039 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4041 return isl_multi_aff_free(maff
);
4047 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4048 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4050 maff1
= isl_multi_aff_add(maff1
, maff2
);
4051 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4055 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4063 /* Return the set of domain elements where "ma1" is lexicographically
4064 * smaller than or equal to "ma2".
4066 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4067 __isl_take isl_multi_aff
*ma2
)
4069 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4072 /* Return the set of domain elements where "ma1" is lexicographically
4073 * smaller than "ma2".
4075 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4076 __isl_take isl_multi_aff
*ma2
)
4078 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4081 /* Return the set of domain elements where "ma1" and "ma2"
4084 static __isl_give isl_set
*isl_multi_aff_order_set(
4085 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4086 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4089 isl_map
*map1
, *map2
;
4092 map1
= isl_map_from_multi_aff(ma1
);
4093 map2
= isl_map_from_multi_aff(ma2
);
4094 map
= isl_map_range_product(map1
, map2
);
4095 space
= isl_space_range(isl_map_get_space(map
));
4096 space
= isl_space_domain(isl_space_unwrap(space
));
4098 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4100 return isl_map_domain(map
);
4103 /* Return the set of domain elements where "ma1" is lexicographically
4104 * greater than or equal to "ma2".
4106 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4107 __isl_take isl_multi_aff
*ma2
)
4109 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4112 /* Return the set of domain elements where "ma1" is lexicographically
4113 * greater than "ma2".
4115 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4116 __isl_take isl_multi_aff
*ma2
)
4118 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4122 #define PW isl_pw_multi_aff
4124 #define EL isl_multi_aff
4126 #define EL_IS_ZERO is_empty
4130 #define IS_ZERO is_empty
4133 #undef DEFAULT_IS_ZERO
4134 #define DEFAULT_IS_ZERO 0
4139 #define NO_INVOLVES_DIMS
4140 #define NO_INSERT_DIMS
4144 #include <isl_pw_templ.c>
4145 #include <isl_pw_union_opt.c>
4150 #define UNION isl_union_pw_multi_aff
4152 #define PART isl_pw_multi_aff
4154 #define PARTS pw_multi_aff
4156 #include <isl_union_multi.c>
4157 #include <isl_union_neg.c>
4159 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4160 __isl_take isl_pw_multi_aff
*pma1
,
4161 __isl_take isl_pw_multi_aff
*pma2
)
4163 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4164 &isl_multi_aff_lex_ge_set
);
4167 /* Given two piecewise multi affine expressions, return a piecewise
4168 * multi-affine expression defined on the union of the definition domains
4169 * of the inputs that is equal to the lexicographic maximum of the two
4170 * inputs on each cell. If only one of the two inputs is defined on
4171 * a given cell, then it is considered to be the maximum.
4173 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4174 __isl_take isl_pw_multi_aff
*pma1
,
4175 __isl_take isl_pw_multi_aff
*pma2
)
4177 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4178 &pw_multi_aff_union_lexmax
);
4181 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4182 __isl_take isl_pw_multi_aff
*pma1
,
4183 __isl_take isl_pw_multi_aff
*pma2
)
4185 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4186 &isl_multi_aff_lex_le_set
);
4189 /* Given two piecewise multi affine expressions, return a piecewise
4190 * multi-affine expression defined on the union of the definition domains
4191 * of the inputs that is equal to the lexicographic minimum of the two
4192 * inputs on each cell. If only one of the two inputs is defined on
4193 * a given cell, then it is considered to be the minimum.
4195 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4196 __isl_take isl_pw_multi_aff
*pma1
,
4197 __isl_take isl_pw_multi_aff
*pma2
)
4199 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4200 &pw_multi_aff_union_lexmin
);
4203 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4204 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4206 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4207 &isl_multi_aff_add
);
4210 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4211 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4213 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4217 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4218 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4220 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4221 &isl_multi_aff_sub
);
4224 /* Subtract "pma2" from "pma1" and return the result.
4226 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4227 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4229 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4233 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4234 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4236 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4239 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4240 * with the actual sum on the shared domain and
4241 * the defined expression on the symmetric difference of the domains.
4243 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4244 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4246 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4249 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4250 * with the actual sum on the shared domain and
4251 * the defined expression on the symmetric difference of the domains.
4253 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4254 __isl_take isl_union_pw_multi_aff
*upma1
,
4255 __isl_take isl_union_pw_multi_aff
*upma2
)
4257 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4260 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4261 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4263 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4264 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4268 isl_pw_multi_aff
*res
;
4273 n
= pma1
->n
* pma2
->n
;
4274 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4275 isl_space_copy(pma2
->dim
));
4276 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4278 for (i
= 0; i
< pma1
->n
; ++i
) {
4279 for (j
= 0; j
< pma2
->n
; ++j
) {
4283 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4284 isl_set_copy(pma2
->p
[j
].set
));
4285 ma
= isl_multi_aff_product(
4286 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4287 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4288 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4292 isl_pw_multi_aff_free(pma1
);
4293 isl_pw_multi_aff_free(pma2
);
4296 isl_pw_multi_aff_free(pma1
);
4297 isl_pw_multi_aff_free(pma2
);
4301 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4302 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4304 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4305 &pw_multi_aff_product
);
4308 /* Construct a map mapping the domain of the piecewise multi-affine expression
4309 * to its range, with each dimension in the range equated to the
4310 * corresponding affine expression on its cell.
4312 * If the domain of "pma" is rational, then so is the constructed "map".
4314 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4322 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4324 for (i
= 0; i
< pma
->n
; ++i
) {
4326 isl_multi_aff
*maff
;
4327 isl_basic_map
*bmap
;
4330 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4332 map
= isl_map_free(map
);
4333 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4334 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4335 map_i
= isl_map_from_basic_map(bmap
);
4336 map_i
= isl_map_intersect_domain(map_i
,
4337 isl_set_copy(pma
->p
[i
].set
));
4338 map
= isl_map_union_disjoint(map
, map_i
);
4341 isl_pw_multi_aff_free(pma
);
4345 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4350 if (!isl_space_is_set(pma
->dim
))
4351 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4352 "isl_pw_multi_aff cannot be converted into an isl_set",
4355 return isl_map_from_pw_multi_aff(pma
);
4357 isl_pw_multi_aff_free(pma
);
4361 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4362 * denominator "denom".
4363 * "denom" is allowed to be negative, in which case the actual denominator
4364 * is -denom and the expressions are added instead.
4366 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4367 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4373 first
= isl_seq_first_non_zero(c
, n
);
4377 sign
= isl_int_sgn(denom
);
4379 isl_int_abs(d
, denom
);
4380 for (i
= first
; i
< n
; ++i
) {
4383 if (isl_int_is_zero(c
[i
]))
4385 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4386 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4387 aff_i
= isl_aff_scale_down(aff_i
, d
);
4389 aff
= isl_aff_sub(aff
, aff_i
);
4391 aff
= isl_aff_add(aff
, aff_i
);
4398 /* Extract an affine expression that expresses the output dimension "pos"
4399 * of "bmap" in terms of the parameters and input dimensions from
4401 * Note that this expression may involve integer divisions defined
4402 * in terms of parameters and input dimensions.
4403 * The equality may also involve references to earlier (but not later)
4404 * output dimensions. These are replaced by the corresponding elements
4407 * If the equality is of the form
4409 * f(i) + h(j) + a x + g(i) = 0,
4411 * with f(i) a linear combinations of the parameters and input dimensions,
4412 * g(i) a linear combination of integer divisions defined in terms of the same
4413 * and h(j) a linear combinations of earlier output dimensions,
4414 * then the affine expression is
4416 * (-f(i) - g(i))/a - h(j)/a
4418 * If the equality is of the form
4420 * f(i) + h(j) - a x + g(i) = 0,
4422 * then the affine expression is
4424 * (f(i) + g(i))/a - h(j)/(-a)
4427 * If "div" refers to an integer division (i.e., it is smaller than
4428 * the number of integer divisions), then the equality constraint
4429 * does involve an integer division (the one at position "div") that
4430 * is defined in terms of output dimensions. However, this integer
4431 * division can be eliminated by exploiting a pair of constraints
4432 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4433 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4435 * In particular, let
4437 * x = e(i) + m floor(...)
4439 * with e(i) the expression derived above and floor(...) the integer
4440 * division involving output dimensions.
4451 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4452 * = (e(i) - l) mod m
4456 * x - l = (e(i) - l) mod m
4460 * x = ((e(i) - l) mod m) + l
4462 * The variable "shift" below contains the expression -l, which may
4463 * also involve a linear combination of earlier output dimensions.
4465 static __isl_give isl_aff
*extract_aff_from_equality(
4466 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4467 __isl_keep isl_multi_aff
*ma
)
4470 unsigned n_div
, n_out
;
4472 isl_local_space
*ls
;
4473 isl_aff
*aff
, *shift
;
4476 ctx
= isl_basic_map_get_ctx(bmap
);
4477 ls
= isl_basic_map_get_local_space(bmap
);
4478 ls
= isl_local_space_domain(ls
);
4479 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4482 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4483 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4484 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4485 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4486 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4487 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4488 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4490 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4491 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4492 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4495 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4496 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4497 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4498 bmap
->eq
[eq
][o_out
+ pos
]);
4500 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4503 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4504 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4505 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4506 isl_int_set_si(shift
->v
->el
[0], 1);
4507 shift
= subtract_initial(shift
, ma
, pos
,
4508 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4509 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4510 mod
= isl_val_int_from_isl_int(ctx
,
4511 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4512 mod
= isl_val_abs(mod
);
4513 aff
= isl_aff_mod_val(aff
, mod
);
4514 aff
= isl_aff_sub(aff
, shift
);
4517 isl_local_space_free(ls
);
4520 isl_local_space_free(ls
);
4525 /* Given a basic map with output dimensions defined
4526 * in terms of the parameters input dimensions and earlier
4527 * output dimensions using an equality (and possibly a pair on inequalities),
4528 * extract an isl_aff that expresses output dimension "pos" in terms
4529 * of the parameters and input dimensions.
4530 * Note that this expression may involve integer divisions defined
4531 * in terms of parameters and input dimensions.
4532 * "ma" contains the expressions corresponding to earlier output dimensions.
4534 * This function shares some similarities with
4535 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4537 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4538 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4545 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4546 if (eq
>= bmap
->n_eq
)
4547 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4548 "unable to find suitable equality", return NULL
);
4549 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4551 aff
= isl_aff_remove_unused_divs(aff
);
4555 /* Given a basic map where each output dimension is defined
4556 * in terms of the parameters and input dimensions using an equality,
4557 * extract an isl_multi_aff that expresses the output dimensions in terms
4558 * of the parameters and input dimensions.
4560 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4561 __isl_take isl_basic_map
*bmap
)
4570 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4571 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4573 for (i
= 0; i
< n_out
; ++i
) {
4576 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4577 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4580 isl_basic_map_free(bmap
);
4585 /* Given a basic set where each set dimension is defined
4586 * in terms of the parameters using an equality,
4587 * extract an isl_multi_aff that expresses the set dimensions in terms
4588 * of the parameters.
4590 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4591 __isl_take isl_basic_set
*bset
)
4593 return extract_isl_multi_aff_from_basic_map(bset
);
4596 /* Create an isl_pw_multi_aff that is equivalent to
4597 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4598 * The given basic map is such that each output dimension is defined
4599 * in terms of the parameters and input dimensions using an equality.
4601 * Since some applications expect the result of isl_pw_multi_aff_from_map
4602 * to only contain integer affine expressions, we compute the floor
4603 * of the expression before returning.
4605 * Remove all constraints involving local variables without
4606 * an explicit representation (resulting in the removal of those
4607 * local variables) prior to the actual extraction to ensure
4608 * that the local spaces in which the resulting affine expressions
4609 * are created do not contain any unknown local variables.
4610 * Removing such constraints is safe because constraints involving
4611 * unknown local variables are not used to determine whether
4612 * a basic map is obviously single-valued.
4614 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4615 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4619 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4620 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4621 ma
= isl_multi_aff_floor(ma
);
4622 return isl_pw_multi_aff_alloc(domain
, ma
);
4625 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4626 * This obviously only works if the input "map" is single-valued.
4627 * If so, we compute the lexicographic minimum of the image in the form
4628 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4629 * to its lexicographic minimum.
4630 * If the input is not single-valued, we produce an error.
4632 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4633 __isl_take isl_map
*map
)
4637 isl_pw_multi_aff
*pma
;
4639 sv
= isl_map_is_single_valued(map
);
4643 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4644 "map is not single-valued", goto error
);
4645 map
= isl_map_make_disjoint(map
);
4649 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4651 for (i
= 0; i
< map
->n
; ++i
) {
4652 isl_pw_multi_aff
*pma_i
;
4653 isl_basic_map
*bmap
;
4654 bmap
= isl_basic_map_copy(map
->p
[i
]);
4655 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4656 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4666 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4667 * taking into account that the output dimension at position "d"
4668 * can be represented as
4670 * x = floor((e(...) + c1) / m)
4672 * given that constraint "i" is of the form
4674 * e(...) + c1 - m x >= 0
4677 * Let "map" be of the form
4681 * We construct a mapping
4683 * A -> [A -> x = floor(...)]
4685 * apply that to the map, obtaining
4687 * [A -> x = floor(...)] -> B
4689 * and equate dimension "d" to x.
4690 * We then compute a isl_pw_multi_aff representation of the resulting map
4691 * and plug in the mapping above.
4693 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4694 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4698 isl_local_space
*ls
;
4706 isl_pw_multi_aff
*pma
;
4709 is_set
= isl_map_is_set(map
);
4713 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4714 ctx
= isl_map_get_ctx(map
);
4715 space
= isl_space_domain(isl_map_get_space(map
));
4716 n_in
= isl_space_dim(space
, isl_dim_set
);
4717 n
= isl_space_dim(space
, isl_dim_all
);
4719 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4721 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4722 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4724 isl_basic_map_free(hull
);
4726 ls
= isl_local_space_from_space(isl_space_copy(space
));
4727 aff
= isl_aff_alloc_vec(ls
, v
);
4728 aff
= isl_aff_floor(aff
);
4730 isl_space_free(space
);
4731 ma
= isl_multi_aff_from_aff(aff
);
4733 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4734 ma
= isl_multi_aff_range_product(ma
,
4735 isl_multi_aff_from_aff(aff
));
4738 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4739 map
= isl_map_apply_domain(map
, insert
);
4740 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4741 pma
= isl_pw_multi_aff_from_map(map
);
4742 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4747 isl_basic_map_free(hull
);
4751 /* Is constraint "c" of the form
4753 * e(...) + c1 - m x >= 0
4757 * -e(...) + c2 + m x >= 0
4759 * where m > 1 and e only depends on parameters and input dimemnsions?
4761 * "offset" is the offset of the output dimensions
4762 * "pos" is the position of output dimension x.
4764 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4766 if (isl_int_is_zero(c
[offset
+ d
]))
4768 if (isl_int_is_one(c
[offset
+ d
]))
4770 if (isl_int_is_negone(c
[offset
+ d
]))
4772 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4774 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4775 total
- (offset
+ d
+ 1)) != -1)
4780 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4782 * As a special case, we first check if there is any pair of constraints,
4783 * shared by all the basic maps in "map" that force a given dimension
4784 * to be equal to the floor of some affine combination of the input dimensions.
4786 * In particular, if we can find two constraints
4788 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4792 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4794 * where m > 1 and e only depends on parameters and input dimemnsions,
4797 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4799 * then we know that we can take
4801 * x = floor((e(...) + c1) / m)
4803 * without having to perform any computation.
4805 * Note that we know that
4809 * If c1 + c2 were 0, then we would have detected an equality during
4810 * simplification. If c1 + c2 were negative, then we would have detected
4813 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4814 __isl_take isl_map
*map
)
4820 isl_basic_map
*hull
;
4822 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4827 dim
= isl_map_dim(map
, isl_dim_out
);
4828 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4829 total
= 1 + isl_basic_map_total_dim(hull
);
4831 for (d
= 0; d
< dim
; ++d
) {
4832 for (i
= 0; i
< n
; ++i
) {
4833 if (!is_potential_div_constraint(hull
->ineq
[i
],
4836 for (j
= i
+ 1; j
< n
; ++j
) {
4837 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4838 hull
->ineq
[j
] + 1, total
- 1))
4840 isl_int_add(sum
, hull
->ineq
[i
][0],
4842 if (isl_int_abs_lt(sum
,
4843 hull
->ineq
[i
][offset
+ d
]))
4850 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4852 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4856 isl_basic_map_free(hull
);
4857 return pw_multi_aff_from_map_base(map
);
4860 isl_basic_map_free(hull
);
4864 /* Given an affine expression
4866 * [A -> B] -> f(A,B)
4868 * construct an isl_multi_aff
4872 * such that dimension "d" in B' is set to "aff" and the remaining
4873 * dimensions are set equal to the corresponding dimensions in B.
4874 * "n_in" is the dimension of the space A.
4875 * "n_out" is the dimension of the space B.
4877 * If "is_set" is set, then the affine expression is of the form
4881 * and we construct an isl_multi_aff
4885 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4886 unsigned n_in
, unsigned n_out
, int is_set
)
4890 isl_space
*space
, *space2
;
4891 isl_local_space
*ls
;
4893 space
= isl_aff_get_domain_space(aff
);
4894 ls
= isl_local_space_from_space(isl_space_copy(space
));
4895 space2
= isl_space_copy(space
);
4897 space2
= isl_space_range(isl_space_unwrap(space2
));
4898 space
= isl_space_map_from_domain_and_range(space
, space2
);
4899 ma
= isl_multi_aff_alloc(space
);
4900 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4902 for (i
= 0; i
< n_out
; ++i
) {
4905 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4906 isl_dim_set
, n_in
+ i
);
4907 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4910 isl_local_space_free(ls
);
4915 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4916 * taking into account that the dimension at position "d" can be written as
4918 * x = m a + f(..) (1)
4920 * where m is equal to "gcd".
4921 * "i" is the index of the equality in "hull" that defines f(..).
4922 * In particular, the equality is of the form
4924 * f(..) - x + m g(existentials) = 0
4928 * -f(..) + x + m g(existentials) = 0
4930 * We basically plug (1) into "map", resulting in a map with "a"
4931 * in the range instead of "x". The corresponding isl_pw_multi_aff
4932 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4934 * Specifically, given the input map
4938 * We first wrap it into a set
4942 * and define (1) on top of the corresponding space, resulting in "aff".
4943 * We use this to create an isl_multi_aff that maps the output position "d"
4944 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4945 * We plug this into the wrapped map, unwrap the result and compute the
4946 * corresponding isl_pw_multi_aff.
4947 * The result is an expression
4955 * so that we can plug that into "aff", after extending the latter to
4961 * If "map" is actually a set, then there is no "A" space, meaning
4962 * that we do not need to perform any wrapping, and that the result
4963 * of the recursive call is of the form
4967 * which is plugged into a mapping of the form
4971 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4972 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4977 isl_local_space
*ls
;
4980 isl_pw_multi_aff
*pma
, *id
;
4986 is_set
= isl_map_is_set(map
);
4990 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4991 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4992 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4997 set
= isl_map_wrap(map
);
4998 space
= isl_space_map_from_set(isl_set_get_space(set
));
4999 ma
= isl_multi_aff_identity(space
);
5000 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5001 aff
= isl_aff_alloc(ls
);
5003 isl_int_set_si(aff
->v
->el
[0], 1);
5004 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5005 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5008 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5010 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5012 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5013 set
= isl_set_preimage_multi_aff(set
, ma
);
5015 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5020 map
= isl_set_unwrap(set
);
5021 pma
= isl_pw_multi_aff_from_map(map
);
5024 space
= isl_pw_multi_aff_get_domain_space(pma
);
5025 space
= isl_space_map_from_set(space
);
5026 id
= isl_pw_multi_aff_identity(space
);
5027 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5029 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5030 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5032 isl_basic_map_free(hull
);
5036 isl_basic_map_free(hull
);
5040 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5041 * "hull" contains the equalities valid for "map".
5043 * Check if any of the output dimensions is "strided".
5044 * That is, we check if it can be written as
5048 * with m greater than 1, a some combination of existentially quantified
5049 * variables and f an expression in the parameters and input dimensions.
5050 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5052 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5055 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5056 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5065 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5066 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5069 isl_basic_map_free(hull
);
5070 return pw_multi_aff_from_map_check_div(map
);
5075 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5076 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5078 for (i
= 0; i
< n_out
; ++i
) {
5079 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5080 isl_int
*eq
= hull
->eq
[j
];
5081 isl_pw_multi_aff
*res
;
5083 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5084 !isl_int_is_negone(eq
[o_out
+ i
]))
5086 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5088 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5089 n_out
- (i
+ 1)) != -1)
5091 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5092 if (isl_int_is_zero(gcd
))
5094 if (isl_int_is_one(gcd
))
5097 res
= pw_multi_aff_from_map_stride(map
, hull
,
5105 isl_basic_map_free(hull
);
5106 return pw_multi_aff_from_map_check_div(map
);
5109 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5111 * As a special case, we first check if all output dimensions are uniquely
5112 * defined in terms of the parameters and input dimensions over the entire
5113 * domain. If so, we extract the desired isl_pw_multi_aff directly
5114 * from the affine hull of "map" and its domain.
5116 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5119 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5122 isl_basic_map
*hull
;
5127 if (isl_map_n_basic_map(map
) == 1) {
5128 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5129 hull
= isl_basic_map_plain_affine_hull(hull
);
5130 sv
= isl_basic_map_plain_is_single_valued(hull
);
5132 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5134 isl_basic_map_free(hull
);
5136 map
= isl_map_detect_equalities(map
);
5137 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5138 sv
= isl_basic_map_plain_is_single_valued(hull
);
5140 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5142 return pw_multi_aff_from_map_check_strides(map
, hull
);
5143 isl_basic_map_free(hull
);
5148 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5150 return isl_pw_multi_aff_from_map(set
);
5153 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5156 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5158 isl_union_pw_multi_aff
**upma
= user
;
5159 isl_pw_multi_aff
*pma
;
5161 pma
= isl_pw_multi_aff_from_map(map
);
5162 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5164 return *upma
? isl_stat_ok
: isl_stat_error
;
5167 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5170 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5171 __isl_take isl_aff
*aff
)
5174 isl_pw_multi_aff
*pma
;
5176 ma
= isl_multi_aff_from_aff(aff
);
5177 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5178 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5181 /* Try and create an isl_union_pw_multi_aff that is equivalent
5182 * to the given isl_union_map.
5183 * The isl_union_map is required to be single-valued in each space.
5184 * Otherwise, an error is produced.
5186 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5187 __isl_take isl_union_map
*umap
)
5190 isl_union_pw_multi_aff
*upma
;
5192 space
= isl_union_map_get_space(umap
);
5193 upma
= isl_union_pw_multi_aff_empty(space
);
5194 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5195 upma
= isl_union_pw_multi_aff_free(upma
);
5196 isl_union_map_free(umap
);
5201 /* Try and create an isl_union_pw_multi_aff that is equivalent
5202 * to the given isl_union_set.
5203 * The isl_union_set is required to be a singleton in each space.
5204 * Otherwise, an error is produced.
5206 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5207 __isl_take isl_union_set
*uset
)
5209 return isl_union_pw_multi_aff_from_union_map(uset
);
5212 /* Return the piecewise affine expression "set ? 1 : 0".
5214 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5217 isl_space
*space
= isl_set_get_space(set
);
5218 isl_local_space
*ls
= isl_local_space_from_space(space
);
5219 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5220 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5222 one
= isl_aff_add_constant_si(one
, 1);
5223 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5224 set
= isl_set_complement(set
);
5225 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5230 /* Plug in "subs" for dimension "type", "pos" of "aff".
5232 * Let i be the dimension to replace and let "subs" be of the form
5236 * and "aff" of the form
5242 * (a f + d g')/(m d)
5244 * where g' is the result of plugging in "subs" in each of the integer
5247 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5248 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5253 aff
= isl_aff_cow(aff
);
5255 return isl_aff_free(aff
);
5257 ctx
= isl_aff_get_ctx(aff
);
5258 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5259 isl_die(ctx
, isl_error_invalid
,
5260 "spaces don't match", return isl_aff_free(aff
));
5261 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5262 isl_die(ctx
, isl_error_unsupported
,
5263 "cannot handle divs yet", return isl_aff_free(aff
));
5265 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5267 return isl_aff_free(aff
);
5269 aff
->v
= isl_vec_cow(aff
->v
);
5271 return isl_aff_free(aff
);
5273 pos
+= isl_local_space_offset(aff
->ls
, type
);
5276 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5277 aff
->v
->size
, subs
->v
->size
, v
);
5283 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5284 * expressions in "maff".
5286 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5287 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5288 __isl_keep isl_aff
*subs
)
5292 maff
= isl_multi_aff_cow(maff
);
5294 return isl_multi_aff_free(maff
);
5296 if (type
== isl_dim_in
)
5299 for (i
= 0; i
< maff
->n
; ++i
) {
5300 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5302 return isl_multi_aff_free(maff
);
5308 /* Plug in "subs" for dimension "type", "pos" of "pma".
5310 * pma is of the form
5314 * while subs is of the form
5316 * v' = B_j(v) -> S_j
5318 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5319 * has a contribution in the result, in particular
5321 * C_ij(S_j) -> M_i(S_j)
5323 * Note that plugging in S_j in C_ij may also result in an empty set
5324 * and this contribution should simply be discarded.
5326 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5327 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5328 __isl_keep isl_pw_aff
*subs
)
5331 isl_pw_multi_aff
*res
;
5334 return isl_pw_multi_aff_free(pma
);
5336 n
= pma
->n
* subs
->n
;
5337 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5339 for (i
= 0; i
< pma
->n
; ++i
) {
5340 for (j
= 0; j
< subs
->n
; ++j
) {
5342 isl_multi_aff
*res_ij
;
5345 common
= isl_set_intersect(
5346 isl_set_copy(pma
->p
[i
].set
),
5347 isl_set_copy(subs
->p
[j
].set
));
5348 common
= isl_set_substitute(common
,
5349 type
, pos
, subs
->p
[j
].aff
);
5350 empty
= isl_set_plain_is_empty(common
);
5351 if (empty
< 0 || empty
) {
5352 isl_set_free(common
);
5358 res_ij
= isl_multi_aff_substitute(
5359 isl_multi_aff_copy(pma
->p
[i
].maff
),
5360 type
, pos
, subs
->p
[j
].aff
);
5362 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5366 isl_pw_multi_aff_free(pma
);
5369 isl_pw_multi_aff_free(pma
);
5370 isl_pw_multi_aff_free(res
);
5374 /* Compute the preimage of a range of dimensions in the affine expression "src"
5375 * under "ma" and put the result in "dst". The number of dimensions in "src"
5376 * that precede the range is given by "n_before". The number of dimensions
5377 * in the range is given by the number of output dimensions of "ma".
5378 * The number of dimensions that follow the range is given by "n_after".
5379 * If "has_denom" is set (to one),
5380 * then "src" and "dst" have an extra initial denominator.
5381 * "n_div_ma" is the number of existentials in "ma"
5382 * "n_div_bset" is the number of existentials in "src"
5383 * The resulting "dst" (which is assumed to have been allocated by
5384 * the caller) contains coefficients for both sets of existentials,
5385 * first those in "ma" and then those in "src".
5386 * f, c1, c2 and g are temporary objects that have been initialized
5389 * Let src represent the expression
5391 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5393 * and let ma represent the expressions
5395 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5397 * We start out with the following expression for dst:
5399 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5401 * with the multiplication factor f initially equal to 1
5402 * and f \sum_i b_i v_i kept separately.
5403 * For each x_i that we substitute, we multiply the numerator
5404 * (and denominator) of dst by c_1 = m_i and add the numerator
5405 * of the x_i expression multiplied by c_2 = f b_i,
5406 * after removing the common factors of c_1 and c_2.
5407 * The multiplication factor f also needs to be multiplied by c_1
5408 * for the next x_j, j > i.
5410 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5411 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5412 int n_div_ma
, int n_div_bmap
,
5413 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5416 int n_param
, n_in
, n_out
;
5419 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5420 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5421 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5423 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5424 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5425 isl_seq_clr(dst
+ o_dst
, n_in
);
5428 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5431 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5433 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5435 isl_int_set_si(f
, 1);
5437 for (i
= 0; i
< n_out
; ++i
) {
5438 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5440 if (isl_int_is_zero(src
[offset
]))
5442 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5443 isl_int_mul(c2
, f
, src
[offset
]);
5444 isl_int_gcd(g
, c1
, c2
);
5445 isl_int_divexact(c1
, c1
, g
);
5446 isl_int_divexact(c2
, c2
, g
);
5448 isl_int_mul(f
, f
, c1
);
5451 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5452 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5453 o_dst
+= 1 + n_param
;
5454 o_src
+= 1 + n_param
;
5455 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5457 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5458 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5461 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5463 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5464 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5467 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5469 isl_int_mul(dst
[0], dst
[0], c1
);
5473 /* Compute the pullback of "aff" by the function represented by "ma".
5474 * In other words, plug in "ma" in "aff". The result is an affine expression
5475 * defined over the domain space of "ma".
5477 * If "aff" is represented by
5479 * (a(p) + b x + c(divs))/d
5481 * and ma is represented by
5483 * x = D(p) + F(y) + G(divs')
5485 * then the result is
5487 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5489 * The divs in the local space of the input are similarly adjusted
5490 * through a call to isl_local_space_preimage_multi_aff.
5492 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5493 __isl_take isl_multi_aff
*ma
)
5495 isl_aff
*res
= NULL
;
5496 isl_local_space
*ls
;
5497 int n_div_aff
, n_div_ma
;
5498 isl_int f
, c1
, c2
, g
;
5500 ma
= isl_multi_aff_align_divs(ma
);
5504 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5505 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5507 ls
= isl_aff_get_domain_local_space(aff
);
5508 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5509 res
= isl_aff_alloc(ls
);
5518 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5527 isl_multi_aff_free(ma
);
5528 res
= isl_aff_normalize(res
);
5532 isl_multi_aff_free(ma
);
5537 /* Compute the pullback of "aff1" by the function represented by "aff2".
5538 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5539 * defined over the domain space of "aff1".
5541 * The domain of "aff1" should match the range of "aff2", which means
5542 * that it should be single-dimensional.
5544 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5545 __isl_take isl_aff
*aff2
)
5549 ma
= isl_multi_aff_from_aff(aff2
);
5550 return isl_aff_pullback_multi_aff(aff1
, ma
);
5553 /* Compute the pullback of "ma1" by the function represented by "ma2".
5554 * In other words, plug in "ma2" in "ma1".
5556 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5558 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5559 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5562 isl_space
*space
= NULL
;
5564 ma2
= isl_multi_aff_align_divs(ma2
);
5565 ma1
= isl_multi_aff_cow(ma1
);
5569 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5570 isl_multi_aff_get_space(ma1
));
5572 for (i
= 0; i
< ma1
->n
; ++i
) {
5573 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5574 isl_multi_aff_copy(ma2
));
5579 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5580 isl_multi_aff_free(ma2
);
5583 isl_space_free(space
);
5584 isl_multi_aff_free(ma2
);
5585 isl_multi_aff_free(ma1
);
5589 /* Compute the pullback of "ma1" by the function represented by "ma2".
5590 * In other words, plug in "ma2" in "ma1".
5592 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5593 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5595 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5596 &isl_multi_aff_pullback_multi_aff_aligned
);
5599 /* Extend the local space of "dst" to include the divs
5600 * in the local space of "src".
5602 * If "src" does not have any divs or if the local spaces of "dst" and
5603 * "src" are the same, then no extension is required.
5605 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5606 __isl_keep isl_aff
*src
)
5609 int src_n_div
, dst_n_div
;
5616 return isl_aff_free(dst
);
5618 ctx
= isl_aff_get_ctx(src
);
5619 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5621 return isl_aff_free(dst
);
5623 isl_die(ctx
, isl_error_invalid
,
5624 "spaces don't match", goto error
);
5626 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5629 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5631 return isl_aff_free(dst
);
5635 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5636 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5637 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5638 if (!exp1
|| (dst_n_div
&& !exp2
))
5641 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5642 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5650 return isl_aff_free(dst
);
5653 /* Adjust the local spaces of the affine expressions in "maff"
5654 * such that they all have the save divs.
5656 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5657 __isl_take isl_multi_aff
*maff
)
5665 maff
= isl_multi_aff_cow(maff
);
5669 for (i
= 1; i
< maff
->n
; ++i
)
5670 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5671 for (i
= 1; i
< maff
->n
; ++i
) {
5672 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5674 return isl_multi_aff_free(maff
);
5680 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5682 aff
= isl_aff_cow(aff
);
5686 aff
->ls
= isl_local_space_lift(aff
->ls
);
5688 return isl_aff_free(aff
);
5693 /* Lift "maff" to a space with extra dimensions such that the result
5694 * has no more existentially quantified variables.
5695 * If "ls" is not NULL, then *ls is assigned the local space that lies
5696 * at the basis of the lifting applied to "maff".
5698 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5699 __isl_give isl_local_space
**ls
)
5713 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5714 *ls
= isl_local_space_from_space(space
);
5716 return isl_multi_aff_free(maff
);
5721 maff
= isl_multi_aff_cow(maff
);
5722 maff
= isl_multi_aff_align_divs(maff
);
5726 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5727 space
= isl_multi_aff_get_space(maff
);
5728 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5729 space
= isl_space_extend_domain_with_range(space
,
5730 isl_multi_aff_get_space(maff
));
5732 return isl_multi_aff_free(maff
);
5733 isl_space_free(maff
->space
);
5734 maff
->space
= space
;
5737 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5739 return isl_multi_aff_free(maff
);
5742 for (i
= 0; i
< maff
->n
; ++i
) {
5743 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5751 isl_local_space_free(*ls
);
5752 return isl_multi_aff_free(maff
);
5756 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5758 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5759 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5769 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5770 if (pos
< 0 || pos
>= n_out
)
5771 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5772 "index out of bounds", return NULL
);
5774 space
= isl_pw_multi_aff_get_space(pma
);
5775 space
= isl_space_drop_dims(space
, isl_dim_out
,
5776 pos
+ 1, n_out
- pos
- 1);
5777 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5779 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5780 for (i
= 0; i
< pma
->n
; ++i
) {
5782 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5783 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5789 /* Return an isl_pw_multi_aff with the given "set" as domain and
5790 * an unnamed zero-dimensional range.
5792 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5793 __isl_take isl_set
*set
)
5798 space
= isl_set_get_space(set
);
5799 space
= isl_space_from_domain(space
);
5800 ma
= isl_multi_aff_zero(space
);
5801 return isl_pw_multi_aff_alloc(set
, ma
);
5804 /* Add an isl_pw_multi_aff with the given "set" as domain and
5805 * an unnamed zero-dimensional range to *user.
5807 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5810 isl_union_pw_multi_aff
**upma
= user
;
5811 isl_pw_multi_aff
*pma
;
5813 pma
= isl_pw_multi_aff_from_domain(set
);
5814 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5819 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5820 * an unnamed zero-dimensional range.
5822 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5823 __isl_take isl_union_set
*uset
)
5826 isl_union_pw_multi_aff
*upma
;
5831 space
= isl_union_set_get_space(uset
);
5832 upma
= isl_union_pw_multi_aff_empty(space
);
5834 if (isl_union_set_foreach_set(uset
,
5835 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5838 isl_union_set_free(uset
);
5841 isl_union_set_free(uset
);
5842 isl_union_pw_multi_aff_free(upma
);
5846 /* Convert "pma" to an isl_map and add it to *umap.
5848 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5851 isl_union_map
**umap
= user
;
5854 map
= isl_map_from_pw_multi_aff(pma
);
5855 *umap
= isl_union_map_add_map(*umap
, map
);
5860 /* Construct a union map mapping the domain of the union
5861 * piecewise multi-affine expression to its range, with each dimension
5862 * in the range equated to the corresponding affine expression on its cell.
5864 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5865 __isl_take isl_union_pw_multi_aff
*upma
)
5868 isl_union_map
*umap
;
5873 space
= isl_union_pw_multi_aff_get_space(upma
);
5874 umap
= isl_union_map_empty(space
);
5876 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5877 &map_from_pw_multi_aff
, &umap
) < 0)
5880 isl_union_pw_multi_aff_free(upma
);
5883 isl_union_pw_multi_aff_free(upma
);
5884 isl_union_map_free(umap
);
5888 /* Local data for bin_entry and the callback "fn".
5890 struct isl_union_pw_multi_aff_bin_data
{
5891 isl_union_pw_multi_aff
*upma2
;
5892 isl_union_pw_multi_aff
*res
;
5893 isl_pw_multi_aff
*pma
;
5894 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5897 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5898 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5900 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5902 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5906 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5908 isl_pw_multi_aff_free(pma
);
5913 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5914 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5915 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5916 * as *entry. The callback should adjust data->res if desired.
5918 static __isl_give isl_union_pw_multi_aff
*bin_op(
5919 __isl_take isl_union_pw_multi_aff
*upma1
,
5920 __isl_take isl_union_pw_multi_aff
*upma2
,
5921 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5924 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5926 space
= isl_union_pw_multi_aff_get_space(upma2
);
5927 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5928 space
= isl_union_pw_multi_aff_get_space(upma1
);
5929 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5931 if (!upma1
|| !upma2
)
5935 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5936 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5937 &bin_entry
, &data
) < 0)
5940 isl_union_pw_multi_aff_free(upma1
);
5941 isl_union_pw_multi_aff_free(upma2
);
5944 isl_union_pw_multi_aff_free(upma1
);
5945 isl_union_pw_multi_aff_free(upma2
);
5946 isl_union_pw_multi_aff_free(data
.res
);
5950 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5951 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5953 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5954 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5958 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5959 isl_pw_multi_aff_get_space(pma2
));
5960 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5961 &isl_multi_aff_range_product
);
5964 /* Given two isl_pw_multi_affs A -> B and C -> D,
5965 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5967 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5968 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5970 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5971 &pw_multi_aff_range_product
);
5974 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5975 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5977 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5978 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5982 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5983 isl_pw_multi_aff_get_space(pma2
));
5984 space
= isl_space_flatten_range(space
);
5985 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5986 &isl_multi_aff_flat_range_product
);
5989 /* Given two isl_pw_multi_affs A -> B and C -> D,
5990 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5992 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5993 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5995 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5996 &pw_multi_aff_flat_range_product
);
5999 /* If data->pma and "pma2" have the same domain space, then compute
6000 * their flat range product and the result to data->res.
6002 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6005 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6007 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6008 pma2
->dim
, isl_dim_in
)) {
6009 isl_pw_multi_aff_free(pma2
);
6013 pma2
= isl_pw_multi_aff_flat_range_product(
6014 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6016 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6021 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6022 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6024 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6025 __isl_take isl_union_pw_multi_aff
*upma1
,
6026 __isl_take isl_union_pw_multi_aff
*upma2
)
6028 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6031 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6032 * The parameters are assumed to have been aligned.
6034 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6035 * except that it works on two different isl_pw_* types.
6037 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6038 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6039 __isl_take isl_pw_aff
*pa
)
6042 isl_pw_multi_aff
*res
= NULL
;
6047 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6048 pa
->dim
, isl_dim_in
))
6049 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6050 "domains don't match", goto error
);
6051 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6052 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6053 "index out of bounds", goto error
);
6056 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6058 for (i
= 0; i
< pma
->n
; ++i
) {
6059 for (j
= 0; j
< pa
->n
; ++j
) {
6061 isl_multi_aff
*res_ij
;
6064 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6065 isl_set_copy(pa
->p
[j
].set
));
6066 empty
= isl_set_plain_is_empty(common
);
6067 if (empty
< 0 || empty
) {
6068 isl_set_free(common
);
6074 res_ij
= isl_multi_aff_set_aff(
6075 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6076 isl_aff_copy(pa
->p
[j
].aff
));
6077 res_ij
= isl_multi_aff_gist(res_ij
,
6078 isl_set_copy(common
));
6080 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6084 isl_pw_multi_aff_free(pma
);
6085 isl_pw_aff_free(pa
);
6088 isl_pw_multi_aff_free(pma
);
6089 isl_pw_aff_free(pa
);
6090 return isl_pw_multi_aff_free(res
);
6093 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6095 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6096 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6097 __isl_take isl_pw_aff
*pa
)
6099 isl_bool equal_params
;
6103 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6104 if (equal_params
< 0)
6107 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6108 if (!isl_space_has_named_params(pma
->dim
) ||
6109 !isl_space_has_named_params(pa
->dim
))
6110 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6111 "unaligned unnamed parameters", goto error
);
6112 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6113 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6114 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6116 isl_pw_multi_aff_free(pma
);
6117 isl_pw_aff_free(pa
);
6121 /* Do the parameters of "pa" match those of "space"?
6123 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6124 __isl_keep isl_space
*space
)
6126 isl_space
*pa_space
;
6130 return isl_bool_error
;
6132 pa_space
= isl_pw_aff_get_space(pa
);
6134 match
= isl_space_has_equal_params(space
, pa_space
);
6136 isl_space_free(pa_space
);
6140 /* Check that the domain space of "pa" matches "space".
6142 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6143 __isl_keep isl_space
*space
)
6145 isl_space
*pa_space
;
6149 return isl_stat_error
;
6151 pa_space
= isl_pw_aff_get_space(pa
);
6153 match
= isl_space_has_equal_params(space
, pa_space
);
6157 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6158 "parameters don't match", goto error
);
6159 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6160 pa_space
, isl_dim_in
);
6164 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6165 "domains don't match", goto error
);
6166 isl_space_free(pa_space
);
6169 isl_space_free(pa_space
);
6170 return isl_stat_error
;
6178 #include <isl_multi_templ.c>
6179 #include <isl_multi_apply_set.c>
6180 #include <isl_multi_coalesce.c>
6181 #include <isl_multi_gist.c>
6182 #include <isl_multi_hash.c>
6183 #include <isl_multi_intersect.c>
6185 /* Scale the elements of "pma" by the corresponding elements of "mv".
6187 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6188 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6191 isl_bool equal_params
;
6193 pma
= isl_pw_multi_aff_cow(pma
);
6196 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6197 mv
->space
, isl_dim_set
))
6198 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6199 "spaces don't match", goto error
);
6200 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6201 if (equal_params
< 0)
6203 if (!equal_params
) {
6204 pma
= isl_pw_multi_aff_align_params(pma
,
6205 isl_multi_val_get_space(mv
));
6206 mv
= isl_multi_val_align_params(mv
,
6207 isl_pw_multi_aff_get_space(pma
));
6212 for (i
= 0; i
< pma
->n
; ++i
) {
6213 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6214 isl_multi_val_copy(mv
));
6215 if (!pma
->p
[i
].maff
)
6219 isl_multi_val_free(mv
);
6222 isl_multi_val_free(mv
);
6223 isl_pw_multi_aff_free(pma
);
6227 /* This function is called for each entry of an isl_union_pw_multi_aff.
6228 * If the space of the entry matches that of data->mv,
6229 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6230 * Otherwise, return an empty isl_pw_multi_aff.
6232 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6233 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6235 isl_multi_val
*mv
= user
;
6239 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6240 mv
->space
, isl_dim_set
)) {
6241 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6242 isl_pw_multi_aff_free(pma
);
6243 return isl_pw_multi_aff_empty(space
);
6246 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6249 /* Scale the elements of "upma" by the corresponding elements of "mv",
6250 * for those entries that match the space of "mv".
6252 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6253 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6255 upma
= isl_union_pw_multi_aff_align_params(upma
,
6256 isl_multi_val_get_space(mv
));
6257 mv
= isl_multi_val_align_params(mv
,
6258 isl_union_pw_multi_aff_get_space(upma
));
6262 return isl_union_pw_multi_aff_transform(upma
,
6263 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6265 isl_multi_val_free(mv
);
6268 isl_multi_val_free(mv
);
6269 isl_union_pw_multi_aff_free(upma
);
6273 /* Construct and return a piecewise multi affine expression
6274 * in the given space with value zero in each of the output dimensions and
6275 * a universe domain.
6277 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6279 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6282 /* Construct and return a piecewise multi affine expression
6283 * that is equal to the given piecewise affine expression.
6285 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6286 __isl_take isl_pw_aff
*pa
)
6290 isl_pw_multi_aff
*pma
;
6295 space
= isl_pw_aff_get_space(pa
);
6296 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6298 for (i
= 0; i
< pa
->n
; ++i
) {
6302 set
= isl_set_copy(pa
->p
[i
].set
);
6303 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6304 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6307 isl_pw_aff_free(pa
);
6311 /* Construct a set or map mapping the shared (parameter) domain
6312 * of the piecewise affine expressions to the range of "mpa"
6313 * with each dimension in the range equated to the
6314 * corresponding piecewise affine expression.
6316 static __isl_give isl_map
*map_from_multi_pw_aff(
6317 __isl_take isl_multi_pw_aff
*mpa
)
6326 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6327 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6328 "invalid space", goto error
);
6330 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6331 map
= isl_map_universe(isl_space_from_domain(space
));
6333 for (i
= 0; i
< mpa
->n
; ++i
) {
6337 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6338 map_i
= map_from_pw_aff(pa
);
6340 map
= isl_map_flat_range_product(map
, map_i
);
6343 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6345 isl_multi_pw_aff_free(mpa
);
6348 isl_multi_pw_aff_free(mpa
);
6352 /* Construct a map mapping the shared domain
6353 * of the piecewise affine expressions to the range of "mpa"
6354 * with each dimension in the range equated to the
6355 * corresponding piecewise affine expression.
6357 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6361 if (isl_space_is_set(mpa
->space
))
6362 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6363 "space of input is not a map", goto error
);
6365 return map_from_multi_pw_aff(mpa
);
6367 isl_multi_pw_aff_free(mpa
);
6371 /* Construct a set mapping the shared parameter domain
6372 * of the piecewise affine expressions to the space of "mpa"
6373 * with each dimension in the range equated to the
6374 * corresponding piecewise affine expression.
6376 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6380 if (!isl_space_is_set(mpa
->space
))
6381 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6382 "space of input is not a set", goto error
);
6384 return map_from_multi_pw_aff(mpa
);
6386 isl_multi_pw_aff_free(mpa
);
6390 /* Construct and return a piecewise multi affine expression
6391 * that is equal to the given multi piecewise affine expression
6392 * on the shared domain of the piecewise affine expressions.
6394 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6395 __isl_take isl_multi_pw_aff
*mpa
)
6400 isl_pw_multi_aff
*pma
;
6405 space
= isl_multi_pw_aff_get_space(mpa
);
6408 isl_multi_pw_aff_free(mpa
);
6409 return isl_pw_multi_aff_zero(space
);
6412 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6413 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6415 for (i
= 1; i
< mpa
->n
; ++i
) {
6416 isl_pw_multi_aff
*pma_i
;
6418 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6419 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6420 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6423 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6425 isl_multi_pw_aff_free(mpa
);
6429 /* Construct and return a multi piecewise affine expression
6430 * that is equal to the given multi affine expression.
6432 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6433 __isl_take isl_multi_aff
*ma
)
6436 isl_multi_pw_aff
*mpa
;
6441 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6442 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6444 for (i
= 0; i
< n
; ++i
) {
6447 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6448 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6451 isl_multi_aff_free(ma
);
6455 /* Construct and return a multi piecewise affine expression
6456 * that is equal to the given piecewise multi affine expression.
6458 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6459 __isl_take isl_pw_multi_aff
*pma
)
6463 isl_multi_pw_aff
*mpa
;
6468 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6469 space
= isl_pw_multi_aff_get_space(pma
);
6470 mpa
= isl_multi_pw_aff_alloc(space
);
6472 for (i
= 0; i
< n
; ++i
) {
6475 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6476 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6479 isl_pw_multi_aff_free(pma
);
6483 /* Do "pa1" and "pa2" represent the same function?
6485 * We first check if they are obviously equal.
6486 * If not, we convert them to maps and check if those are equal.
6488 * If "pa1" or "pa2" contain any NaNs, then they are considered
6489 * not to be the same. A NaN is not equal to anything, not even
6492 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6493 __isl_keep isl_pw_aff
*pa2
)
6497 isl_map
*map1
, *map2
;
6500 return isl_bool_error
;
6502 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6503 if (equal
< 0 || equal
)
6505 has_nan
= either_involves_nan(pa1
, pa2
);
6507 return isl_bool_error
;
6509 return isl_bool_false
;
6511 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6512 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6513 equal
= isl_map_is_equal(map1
, map2
);
6520 /* Do "mpa1" and "mpa2" represent the same function?
6522 * Note that we cannot convert the entire isl_multi_pw_aff
6523 * to a map because the domains of the piecewise affine expressions
6524 * may not be the same.
6526 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6527 __isl_keep isl_multi_pw_aff
*mpa2
)
6530 isl_bool equal
, equal_params
;
6533 return isl_bool_error
;
6535 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6536 if (equal_params
< 0)
6537 return isl_bool_error
;
6538 if (!equal_params
) {
6539 if (!isl_space_has_named_params(mpa1
->space
))
6540 return isl_bool_false
;
6541 if (!isl_space_has_named_params(mpa2
->space
))
6542 return isl_bool_false
;
6543 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6544 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6545 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6546 isl_multi_pw_aff_get_space(mpa2
));
6547 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6548 isl_multi_pw_aff_get_space(mpa1
));
6549 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6550 isl_multi_pw_aff_free(mpa1
);
6551 isl_multi_pw_aff_free(mpa2
);
6555 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6556 if (equal
< 0 || !equal
)
6559 for (i
= 0; i
< mpa1
->n
; ++i
) {
6560 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6561 if (equal
< 0 || !equal
)
6565 return isl_bool_true
;
6568 /* Do "pma1" and "pma2" represent the same function?
6570 * First check if they are obviously equal.
6571 * If not, then convert them to maps and check if those are equal.
6573 * If "pa1" or "pa2" contain any NaNs, then they are considered
6574 * not to be the same. A NaN is not equal to anything, not even
6577 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6578 __isl_keep isl_pw_multi_aff
*pma2
)
6582 isl_map
*map1
, *map2
;
6585 return isl_bool_error
;
6587 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6588 if (equal
< 0 || equal
)
6590 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6591 if (has_nan
>= 0 && !has_nan
)
6592 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6593 if (has_nan
< 0 || has_nan
)
6594 return isl_bool_not(has_nan
);
6596 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6597 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6598 equal
= isl_map_is_equal(map1
, map2
);
6605 /* Compute the pullback of "mpa" by the function represented by "ma".
6606 * In other words, plug in "ma" in "mpa".
6608 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6610 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6611 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6614 isl_space
*space
= NULL
;
6616 mpa
= isl_multi_pw_aff_cow(mpa
);
6620 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6621 isl_multi_pw_aff_get_space(mpa
));
6625 for (i
= 0; i
< mpa
->n
; ++i
) {
6626 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6627 isl_multi_aff_copy(ma
));
6632 isl_multi_aff_free(ma
);
6633 isl_space_free(mpa
->space
);
6637 isl_space_free(space
);
6638 isl_multi_pw_aff_free(mpa
);
6639 isl_multi_aff_free(ma
);
6643 /* Compute the pullback of "mpa" by the function represented by "ma".
6644 * In other words, plug in "ma" in "mpa".
6646 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6647 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6649 isl_bool equal_params
;
6653 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6654 if (equal_params
< 0)
6657 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6658 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6659 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6660 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6662 isl_multi_pw_aff_free(mpa
);
6663 isl_multi_aff_free(ma
);
6667 /* Compute the pullback of "mpa" by the function represented by "pma".
6668 * In other words, plug in "pma" in "mpa".
6670 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6672 static __isl_give isl_multi_pw_aff
*
6673 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6674 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6677 isl_space
*space
= NULL
;
6679 mpa
= isl_multi_pw_aff_cow(mpa
);
6683 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6684 isl_multi_pw_aff_get_space(mpa
));
6686 for (i
= 0; i
< mpa
->n
; ++i
) {
6687 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6688 isl_pw_multi_aff_copy(pma
));
6693 isl_pw_multi_aff_free(pma
);
6694 isl_space_free(mpa
->space
);
6698 isl_space_free(space
);
6699 isl_multi_pw_aff_free(mpa
);
6700 isl_pw_multi_aff_free(pma
);
6704 /* Compute the pullback of "mpa" by the function represented by "pma".
6705 * In other words, plug in "pma" in "mpa".
6707 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6708 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6710 isl_bool equal_params
;
6714 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6715 if (equal_params
< 0)
6718 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6719 mpa
= isl_multi_pw_aff_align_params(mpa
,
6720 isl_pw_multi_aff_get_space(pma
));
6721 pma
= isl_pw_multi_aff_align_params(pma
,
6722 isl_multi_pw_aff_get_space(mpa
));
6723 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6725 isl_multi_pw_aff_free(mpa
);
6726 isl_pw_multi_aff_free(pma
);
6730 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6731 * with the domain of "aff". The domain of the result is the same
6733 * "mpa" and "aff" are assumed to have been aligned.
6735 * We first extract the parametric constant from "aff", defined
6736 * over the correct domain.
6737 * Then we add the appropriate combinations of the members of "mpa".
6738 * Finally, we add the integer divisions through recursive calls.
6740 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6741 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6749 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6750 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6752 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6753 tmp
= isl_aff_copy(aff
);
6754 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6755 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6756 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6757 isl_space_dim(space
, isl_dim_set
));
6758 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6759 pa
= isl_pw_aff_from_aff(tmp
);
6761 for (i
= 0; i
< n_in
; ++i
) {
6764 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6766 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6767 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6768 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6769 pa
= isl_pw_aff_add(pa
, pa_i
);
6772 for (i
= 0; i
< n_div
; ++i
) {
6776 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6778 div
= isl_aff_get_div(aff
, i
);
6779 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6780 isl_multi_pw_aff_copy(mpa
), div
);
6781 pa_i
= isl_pw_aff_floor(pa_i
);
6782 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6783 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6784 pa
= isl_pw_aff_add(pa
, pa_i
);
6787 isl_multi_pw_aff_free(mpa
);
6793 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6794 * with the domain of "aff". The domain of the result is the same
6797 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6798 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6800 isl_bool equal_params
;
6804 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6805 if (equal_params
< 0)
6808 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6810 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6811 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6813 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6816 isl_multi_pw_aff_free(mpa
);
6820 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6821 * with the domain of "pa". The domain of the result is the same
6823 * "mpa" and "pa" are assumed to have been aligned.
6825 * We consider each piece in turn. Note that the domains of the
6826 * pieces are assumed to be disjoint and they remain disjoint
6827 * after taking the preimage (over the same function).
6829 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6830 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6839 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6840 isl_pw_aff_get_space(pa
));
6841 res
= isl_pw_aff_empty(space
);
6843 for (i
= 0; i
< pa
->n
; ++i
) {
6847 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6848 isl_multi_pw_aff_copy(mpa
),
6849 isl_aff_copy(pa
->p
[i
].aff
));
6850 domain
= isl_set_copy(pa
->p
[i
].set
);
6851 domain
= isl_set_preimage_multi_pw_aff(domain
,
6852 isl_multi_pw_aff_copy(mpa
));
6853 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6854 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6857 isl_pw_aff_free(pa
);
6858 isl_multi_pw_aff_free(mpa
);
6861 isl_pw_aff_free(pa
);
6862 isl_multi_pw_aff_free(mpa
);
6866 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6867 * with the domain of "pa". The domain of the result is the same
6870 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6871 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6873 isl_bool equal_params
;
6877 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6878 if (equal_params
< 0)
6881 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6883 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6884 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6886 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6888 isl_pw_aff_free(pa
);
6889 isl_multi_pw_aff_free(mpa
);
6893 /* Compute the pullback of "pa" by the function represented by "mpa".
6894 * In other words, plug in "mpa" in "pa".
6895 * "pa" and "mpa" are assumed to have been aligned.
6897 * The pullback is computed by applying "pa" to "mpa".
6899 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6900 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6902 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6905 /* Compute the pullback of "pa" by the function represented by "mpa".
6906 * In other words, plug in "mpa" in "pa".
6908 * The pullback is computed by applying "pa" to "mpa".
6910 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6911 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6913 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6916 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6917 * In other words, plug in "mpa2" in "mpa1".
6919 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6921 * We pullback each member of "mpa1" in turn.
6923 static __isl_give isl_multi_pw_aff
*
6924 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6925 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6928 isl_space
*space
= NULL
;
6930 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6934 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6935 isl_multi_pw_aff_get_space(mpa1
));
6937 for (i
= 0; i
< mpa1
->n
; ++i
) {
6938 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6939 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6944 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6946 isl_multi_pw_aff_free(mpa2
);
6949 isl_space_free(space
);
6950 isl_multi_pw_aff_free(mpa1
);
6951 isl_multi_pw_aff_free(mpa2
);
6955 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6956 * In other words, plug in "mpa2" in "mpa1".
6958 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6959 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6961 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6962 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6965 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6966 * of "mpa1" and "mpa2" live in the same space, construct map space
6967 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6968 * with this map space as extract argument.
6970 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6971 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6972 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6973 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6976 isl_space
*space1
, *space2
;
6979 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6980 isl_multi_pw_aff_get_space(mpa2
));
6981 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6982 isl_multi_pw_aff_get_space(mpa1
));
6985 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6986 mpa2
->space
, isl_dim_out
);
6990 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6991 "range spaces don't match", goto error
);
6992 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6993 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6994 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6996 res
= order(mpa1
, mpa2
, space1
);
6997 isl_multi_pw_aff_free(mpa1
);
6998 isl_multi_pw_aff_free(mpa2
);
7001 isl_multi_pw_aff_free(mpa1
);
7002 isl_multi_pw_aff_free(mpa2
);
7006 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7007 * where the function values are equal. "space" is the space of the result.
7008 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7010 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7011 * in the sequences are equal.
7013 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7014 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7015 __isl_take isl_space
*space
)
7020 res
= isl_map_universe(space
);
7022 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7023 for (i
= 0; i
< n
; ++i
) {
7024 isl_pw_aff
*pa1
, *pa2
;
7027 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7028 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7029 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7030 res
= isl_map_intersect(res
, map
);
7036 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7037 * where the function values are equal.
7039 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7040 __isl_take isl_multi_pw_aff
*mpa2
)
7042 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7043 &isl_multi_pw_aff_eq_map_on_space
);
7046 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7047 * where the function values of "mpa1" is lexicographically satisfies "base"
7048 * compared to that of "mpa2". "space" is the space of the result.
7049 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7051 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7052 * if its i-th element satisfies "base" when compared to
7053 * the i-th element of "mpa2" while all previous elements are
7056 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7057 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7058 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7059 __isl_take isl_pw_aff
*pa2
),
7060 __isl_take isl_space
*space
)
7063 isl_map
*res
, *rest
;
7065 res
= isl_map_empty(isl_space_copy(space
));
7066 rest
= isl_map_universe(space
);
7068 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7069 for (i
= 0; i
< n
; ++i
) {
7070 isl_pw_aff
*pa1
, *pa2
;
7073 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7074 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7075 map
= base(pa1
, pa2
);
7076 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7077 res
= isl_map_union(res
, map
);
7082 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7083 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7084 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7085 rest
= isl_map_intersect(rest
, map
);
7092 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7093 * where the function value of "mpa1" is lexicographically less than that
7094 * of "mpa2". "space" is the space of the result.
7095 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7097 * "mpa1" is less than "mpa2" if its i-th element is smaller
7098 * than the i-th element of "mpa2" while all previous elements are
7101 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7102 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7103 __isl_take isl_space
*space
)
7105 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7106 &isl_pw_aff_lt_map
, space
);
7109 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7110 * where the function value of "mpa1" is lexicographically less than that
7113 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7114 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7116 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7117 &isl_multi_pw_aff_lex_lt_map_on_space
);
7120 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7121 * where the function value of "mpa1" is lexicographically greater than that
7122 * of "mpa2". "space" is the space of the result.
7123 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7125 * "mpa1" is greater than "mpa2" if its i-th element is greater
7126 * than the i-th element of "mpa2" while all previous elements are
7129 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7130 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7131 __isl_take isl_space
*space
)
7133 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7134 &isl_pw_aff_gt_map
, space
);
7137 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7138 * where the function value of "mpa1" is lexicographically greater than that
7141 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7142 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7144 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7145 &isl_multi_pw_aff_lex_gt_map_on_space
);
7148 /* Compare two isl_affs.
7150 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7151 * than "aff2" and 0 if they are equal.
7153 * The order is fairly arbitrary. We do consider expressions that only involve
7154 * earlier dimensions as "smaller".
7156 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7169 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7173 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7174 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7176 return last1
- last2
;
7178 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7181 /* Compare two isl_pw_affs.
7183 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7184 * than "pa2" and 0 if they are equal.
7186 * The order is fairly arbitrary. We do consider expressions that only involve
7187 * earlier dimensions as "smaller".
7189 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7190 __isl_keep isl_pw_aff
*pa2
)
7203 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7207 if (pa1
->n
!= pa2
->n
)
7208 return pa1
->n
- pa2
->n
;
7210 for (i
= 0; i
< pa1
->n
; ++i
) {
7211 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7214 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7222 /* Return a piecewise affine expression that is equal to "v" on "domain".
7224 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7225 __isl_take isl_val
*v
)
7228 isl_local_space
*ls
;
7231 space
= isl_set_get_space(domain
);
7232 ls
= isl_local_space_from_space(space
);
7233 aff
= isl_aff_val_on_domain(ls
, v
);
7235 return isl_pw_aff_alloc(domain
, aff
);
7238 /* Return a multi affine expression that is equal to "mv" on domain
7241 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7242 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7246 isl_local_space
*ls
;
7252 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7253 space2
= isl_multi_val_get_space(mv
);
7254 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7255 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7256 space
= isl_space_map_from_domain_and_range(space
, space2
);
7257 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7258 ls
= isl_local_space_from_space(isl_space_domain(space
));
7259 for (i
= 0; i
< n
; ++i
) {
7263 v
= isl_multi_val_get_val(mv
, i
);
7264 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7265 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7267 isl_local_space_free(ls
);
7269 isl_multi_val_free(mv
);
7272 isl_space_free(space
);
7273 isl_multi_val_free(mv
);
7277 /* Return a piecewise multi-affine expression
7278 * that is equal to "mv" on "domain".
7280 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7281 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7286 space
= isl_set_get_space(domain
);
7287 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7289 return isl_pw_multi_aff_alloc(domain
, ma
);
7292 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7293 * mv is the value that should be attained on each domain set
7294 * res collects the results
7296 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7298 isl_union_pw_multi_aff
*res
;
7301 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7302 * and add it to data->res.
7304 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7307 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7308 isl_pw_multi_aff
*pma
;
7311 mv
= isl_multi_val_copy(data
->mv
);
7312 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7313 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7315 return data
->res
? isl_stat_ok
: isl_stat_error
;
7318 /* Return a union piecewise multi-affine expression
7319 * that is equal to "mv" on "domain".
7321 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7322 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7324 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7327 space
= isl_union_set_get_space(domain
);
7328 data
.res
= isl_union_pw_multi_aff_empty(space
);
7330 if (isl_union_set_foreach_set(domain
,
7331 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7332 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7333 isl_union_set_free(domain
);
7334 isl_multi_val_free(mv
);
7338 /* Compute the pullback of data->pma by the function represented by "pma2",
7339 * provided the spaces match, and add the results to data->res.
7341 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7343 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7345 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7346 pma2
->dim
, isl_dim_out
)) {
7347 isl_pw_multi_aff_free(pma2
);
7351 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7352 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7354 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7356 return isl_stat_error
;
7361 /* Compute the pullback of "upma1" by the function represented by "upma2".
7363 __isl_give isl_union_pw_multi_aff
*
7364 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7365 __isl_take isl_union_pw_multi_aff
*upma1
,
7366 __isl_take isl_union_pw_multi_aff
*upma2
)
7368 return bin_op(upma1
, upma2
, &pullback_entry
);
7371 /* Check that the domain space of "upa" matches "space".
7373 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7374 * can in principle never fail since the space "space" is that
7375 * of the isl_multi_union_pw_aff and is a set space such that
7376 * there is no domain space to match.
7378 * We check the parameters and double-check that "space" is
7379 * indeed that of a set.
7381 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7382 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7384 isl_space
*upa_space
;
7388 return isl_stat_error
;
7390 match
= isl_space_is_set(space
);
7392 return isl_stat_error
;
7394 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7395 "expecting set space", return -1);
7397 upa_space
= isl_union_pw_aff_get_space(upa
);
7398 match
= isl_space_has_equal_params(space
, upa_space
);
7402 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7403 "parameters don't match", goto error
);
7405 isl_space_free(upa_space
);
7408 isl_space_free(upa_space
);
7409 return isl_stat_error
;
7412 /* Do the parameters of "upa" match those of "space"?
7414 static isl_bool
isl_union_pw_aff_matching_params(
7415 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7417 isl_space
*upa_space
;
7421 return isl_bool_error
;
7423 upa_space
= isl_union_pw_aff_get_space(upa
);
7425 match
= isl_space_has_equal_params(space
, upa_space
);
7427 isl_space_free(upa_space
);
7431 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7432 * space represents the new parameters.
7433 * res collects the results.
7435 struct isl_union_pw_aff_reset_params_data
{
7437 isl_union_pw_aff
*res
;
7440 /* Replace the parameters of "pa" by data->space and
7441 * add the result to data->res.
7443 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7445 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7448 space
= isl_pw_aff_get_space(pa
);
7449 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7450 pa
= isl_pw_aff_reset_space(pa
, space
);
7451 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7453 return data
->res
? isl_stat_ok
: isl_stat_error
;
7456 /* Replace the domain space of "upa" by "space".
7457 * Since a union expression does not have a (single) domain space,
7458 * "space" is necessarily a parameter space.
7460 * Since the order and the names of the parameters determine
7461 * the hash value, we need to create a new hash table.
7463 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7464 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7466 struct isl_union_pw_aff_reset_params_data data
= { space
};
7469 match
= isl_union_pw_aff_matching_params(upa
, space
);
7471 upa
= isl_union_pw_aff_free(upa
);
7473 isl_space_free(space
);
7477 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7478 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7479 data
.res
= isl_union_pw_aff_free(data
.res
);
7481 isl_union_pw_aff_free(upa
);
7482 isl_space_free(space
);
7486 /* Return the floor of "pa".
7488 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7490 return isl_pw_aff_floor(pa
);
7493 /* Given f, return floor(f).
7495 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7496 __isl_take isl_union_pw_aff
*upa
)
7498 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7503 * upa mod m = upa - m * floor(upa/m)
7505 * with m an integer value.
7507 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7508 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7510 isl_union_pw_aff
*res
;
7515 if (!isl_val_is_int(m
))
7516 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7517 "expecting integer modulo", goto error
);
7518 if (!isl_val_is_pos(m
))
7519 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7520 "expecting positive modulo", goto error
);
7522 res
= isl_union_pw_aff_copy(upa
);
7523 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7524 upa
= isl_union_pw_aff_floor(upa
);
7525 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7526 res
= isl_union_pw_aff_sub(res
, upa
);
7531 isl_union_pw_aff_free(upa
);
7535 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7536 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7538 * "res" collects the results.
7540 struct isl_union_pw_aff_aff_on_domain_data
{
7542 isl_union_pw_aff
*res
;
7545 /* Construct a piecewise affine expression that is equal to data->aff
7546 * on "domain" and add the result to data->res.
7548 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7550 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7555 aff
= isl_aff_copy(data
->aff
);
7556 dim
= isl_set_dim(domain
, isl_dim_set
);
7557 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7558 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7559 pa
= isl_pw_aff_alloc(domain
, aff
);
7560 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7562 return data
->res
? isl_stat_ok
: isl_stat_error
;
7565 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7566 * pos is the output position that needs to be extracted.
7567 * res collects the results.
7569 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7571 isl_union_pw_aff
*res
;
7574 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7575 * (assuming it has such a dimension) and add it to data->res.
7577 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7579 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7584 return isl_stat_error
;
7586 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7587 if (data
->pos
>= n_out
) {
7588 isl_pw_multi_aff_free(pma
);
7592 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7593 isl_pw_multi_aff_free(pma
);
7595 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7597 return data
->res
? isl_stat_ok
: isl_stat_error
;
7600 /* Extract an isl_union_pw_aff corresponding to
7601 * output dimension "pos" of "upma".
7603 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7604 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7606 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7613 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7614 "cannot extract at negative position", return NULL
);
7616 space
= isl_union_pw_multi_aff_get_space(upma
);
7617 data
.res
= isl_union_pw_aff_empty(space
);
7619 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7620 &get_union_pw_aff
, &data
) < 0)
7621 data
.res
= isl_union_pw_aff_free(data
.res
);
7626 /* Return a union piecewise affine expression
7627 * that is equal to "aff" on "domain".
7629 * Construct an isl_pw_aff on each of the sets in "domain" and
7630 * collect the results.
7632 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7633 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7635 struct isl_union_pw_aff_aff_on_domain_data data
;
7638 if (!domain
|| !aff
)
7640 if (!isl_local_space_is_params(aff
->ls
))
7641 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7642 "expecting parametric expression", goto error
);
7644 space
= isl_union_set_get_space(domain
);
7645 data
.res
= isl_union_pw_aff_empty(space
);
7647 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7648 data
.res
= isl_union_pw_aff_free(data
.res
);
7649 isl_union_set_free(domain
);
7653 isl_union_set_free(domain
);
7658 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7659 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7660 * "res" collects the results.
7662 struct isl_union_pw_aff_val_on_domain_data
{
7664 isl_union_pw_aff
*res
;
7667 /* Construct a piecewise affine expression that is equal to data->v
7668 * on "domain" and add the result to data->res.
7670 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7672 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7676 v
= isl_val_copy(data
->v
);
7677 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7678 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7680 return data
->res
? isl_stat_ok
: isl_stat_error
;
7683 /* Return a union piecewise affine expression
7684 * that is equal to "v" on "domain".
7686 * Construct an isl_pw_aff on each of the sets in "domain" and
7687 * collect the results.
7689 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7690 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7692 struct isl_union_pw_aff_val_on_domain_data data
;
7695 space
= isl_union_set_get_space(domain
);
7696 data
.res
= isl_union_pw_aff_empty(space
);
7698 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7699 data
.res
= isl_union_pw_aff_free(data
.res
);
7700 isl_union_set_free(domain
);
7705 /* Construct a piecewise multi affine expression
7706 * that is equal to "pa" and add it to upma.
7708 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7711 isl_union_pw_multi_aff
**upma
= user
;
7712 isl_pw_multi_aff
*pma
;
7714 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7715 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7717 return *upma
? isl_stat_ok
: isl_stat_error
;
7720 /* Construct and return a union piecewise multi affine expression
7721 * that is equal to the given union piecewise affine expression.
7723 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7724 __isl_take isl_union_pw_aff
*upa
)
7727 isl_union_pw_multi_aff
*upma
;
7732 space
= isl_union_pw_aff_get_space(upa
);
7733 upma
= isl_union_pw_multi_aff_empty(space
);
7735 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7736 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7737 upma
= isl_union_pw_multi_aff_free(upma
);
7739 isl_union_pw_aff_free(upa
);
7743 /* Compute the set of elements in the domain of "pa" where it is zero and
7744 * add this set to "uset".
7746 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7748 isl_union_set
**uset
= (isl_union_set
**)user
;
7750 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7752 return *uset
? isl_stat_ok
: isl_stat_error
;
7755 /* Return a union set containing those elements in the domain
7756 * of "upa" where it is zero.
7758 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7759 __isl_take isl_union_pw_aff
*upa
)
7761 isl_union_set
*zero
;
7763 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7764 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7765 zero
= isl_union_set_free(zero
);
7767 isl_union_pw_aff_free(upa
);
7771 /* Convert "pa" to an isl_map and add it to *umap.
7773 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7775 isl_union_map
**umap
= user
;
7778 map
= isl_map_from_pw_aff(pa
);
7779 *umap
= isl_union_map_add_map(*umap
, map
);
7781 return *umap
? isl_stat_ok
: isl_stat_error
;
7784 /* Construct a union map mapping the domain of the union
7785 * piecewise affine expression to its range, with the single output dimension
7786 * equated to the corresponding affine expressions on their cells.
7788 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7789 __isl_take isl_union_pw_aff
*upa
)
7792 isl_union_map
*umap
;
7797 space
= isl_union_pw_aff_get_space(upa
);
7798 umap
= isl_union_map_empty(space
);
7800 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7802 umap
= isl_union_map_free(umap
);
7804 isl_union_pw_aff_free(upa
);
7808 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7809 * upma is the function that is plugged in.
7810 * pa is the current part of the function in which upma is plugged in.
7811 * res collects the results.
7813 struct isl_union_pw_aff_pullback_upma_data
{
7814 isl_union_pw_multi_aff
*upma
;
7816 isl_union_pw_aff
*res
;
7819 /* Check if "pma" can be plugged into data->pa.
7820 * If so, perform the pullback and add the result to data->res.
7822 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7824 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7827 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7828 pma
->dim
, isl_dim_out
)) {
7829 isl_pw_multi_aff_free(pma
);
7833 pa
= isl_pw_aff_copy(data
->pa
);
7834 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7836 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7838 return data
->res
? isl_stat_ok
: isl_stat_error
;
7841 /* Check if any of the elements of data->upma can be plugged into pa,
7842 * add if so add the result to data->res.
7844 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7846 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7850 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7852 isl_pw_aff_free(pa
);
7857 /* Compute the pullback of "upa" by the function represented by "upma".
7858 * In other words, plug in "upma" in "upa". The result contains
7859 * expressions defined over the domain space of "upma".
7861 * Run over all pairs of elements in "upa" and "upma", perform
7862 * the pullback when appropriate and collect the results.
7863 * If the hash value were based on the domain space rather than
7864 * the function space, then we could run through all elements
7865 * of "upma" and directly pick out the corresponding element of "upa".
7867 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7868 __isl_take isl_union_pw_aff
*upa
,
7869 __isl_take isl_union_pw_multi_aff
*upma
)
7871 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7874 space
= isl_union_pw_multi_aff_get_space(upma
);
7875 upa
= isl_union_pw_aff_align_params(upa
, space
);
7876 space
= isl_union_pw_aff_get_space(upa
);
7877 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7883 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7884 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7885 data
.res
= isl_union_pw_aff_free(data
.res
);
7887 isl_union_pw_aff_free(upa
);
7888 isl_union_pw_multi_aff_free(upma
);
7891 isl_union_pw_aff_free(upa
);
7892 isl_union_pw_multi_aff_free(upma
);
7897 #define BASE union_pw_aff
7899 #define DOMBASE union_set
7901 #define NO_MOVE_DIMS
7910 #include <isl_multi_templ.c>
7911 #include <isl_multi_apply_set.c>
7912 #include <isl_multi_apply_union_set.c>
7913 #include <isl_multi_coalesce.c>
7914 #include <isl_multi_floor.c>
7915 #include <isl_multi_gist.c>
7916 #include <isl_multi_intersect.c>
7918 /* Construct a multiple union piecewise affine expression
7919 * in the given space with value zero in each of the output dimensions.
7921 * Since there is no canonical zero value for
7922 * a union piecewise affine expression, we can only construct
7923 * zero-dimensional "zero" value.
7925 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7926 __isl_take isl_space
*space
)
7931 if (!isl_space_is_set(space
))
7932 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7933 "expecting set space", goto error
);
7934 if (isl_space_dim(space
, isl_dim_out
) != 0)
7935 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7936 "expecting 0D space", goto error
);
7938 return isl_multi_union_pw_aff_alloc(space
);
7940 isl_space_free(space
);
7944 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7945 * with the actual sum on the shared domain and
7946 * the defined expression on the symmetric difference of the domains.
7948 * We simply iterate over the elements in both arguments and
7949 * call isl_union_pw_aff_union_add on each of them.
7951 static __isl_give isl_multi_union_pw_aff
*
7952 isl_multi_union_pw_aff_union_add_aligned(
7953 __isl_take isl_multi_union_pw_aff
*mupa1
,
7954 __isl_take isl_multi_union_pw_aff
*mupa2
)
7956 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7957 &isl_union_pw_aff_union_add
);
7960 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7961 * with the actual sum on the shared domain and
7962 * the defined expression on the symmetric difference of the domains.
7964 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7965 __isl_take isl_multi_union_pw_aff
*mupa1
,
7966 __isl_take isl_multi_union_pw_aff
*mupa2
)
7968 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7969 &isl_multi_union_pw_aff_union_add_aligned
);
7972 /* Construct and return a multi union piecewise affine expression
7973 * that is equal to the given multi affine expression.
7975 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7976 __isl_take isl_multi_aff
*ma
)
7978 isl_multi_pw_aff
*mpa
;
7980 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7981 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7984 /* Construct and return a multi union piecewise affine expression
7985 * that is equal to the given multi piecewise affine expression.
7987 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7988 __isl_take isl_multi_pw_aff
*mpa
)
7992 isl_multi_union_pw_aff
*mupa
;
7997 space
= isl_multi_pw_aff_get_space(mpa
);
7998 space
= isl_space_range(space
);
7999 mupa
= isl_multi_union_pw_aff_alloc(space
);
8001 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8002 for (i
= 0; i
< n
; ++i
) {
8004 isl_union_pw_aff
*upa
;
8006 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8007 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8008 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8011 isl_multi_pw_aff_free(mpa
);
8016 /* Extract the range space of "pma" and assign it to *space.
8017 * If *space has already been set (through a previous call to this function),
8018 * then check that the range space is the same.
8020 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8022 isl_space
**space
= user
;
8023 isl_space
*pma_space
;
8026 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8027 isl_pw_multi_aff_free(pma
);
8030 return isl_stat_error
;
8036 equal
= isl_space_is_equal(pma_space
, *space
);
8037 isl_space_free(pma_space
);
8040 return isl_stat_error
;
8042 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8043 "range spaces not the same", return isl_stat_error
);
8047 /* Construct and return a multi union piecewise affine expression
8048 * that is equal to the given union piecewise multi affine expression.
8050 * In order to be able to perform the conversion, the input
8051 * needs to be non-empty and may only involve a single range space.
8053 __isl_give isl_multi_union_pw_aff
*
8054 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8055 __isl_take isl_union_pw_multi_aff
*upma
)
8057 isl_space
*space
= NULL
;
8058 isl_multi_union_pw_aff
*mupa
;
8063 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8064 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8065 "cannot extract range space from empty input",
8067 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8074 n
= isl_space_dim(space
, isl_dim_set
);
8075 mupa
= isl_multi_union_pw_aff_alloc(space
);
8077 for (i
= 0; i
< n
; ++i
) {
8078 isl_union_pw_aff
*upa
;
8080 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8081 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8084 isl_union_pw_multi_aff_free(upma
);
8087 isl_space_free(space
);
8088 isl_union_pw_multi_aff_free(upma
);
8092 /* Try and create an isl_multi_union_pw_aff that is equivalent
8093 * to the given isl_union_map.
8094 * The isl_union_map is required to be single-valued in each space.
8095 * Moreover, it cannot be empty and all range spaces need to be the same.
8096 * Otherwise, an error is produced.
8098 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8099 __isl_take isl_union_map
*umap
)
8101 isl_union_pw_multi_aff
*upma
;
8103 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8104 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8107 /* Return a multiple union piecewise affine expression
8108 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8109 * have been aligned.
8111 static __isl_give isl_multi_union_pw_aff
*
8112 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8113 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8117 isl_multi_union_pw_aff
*mupa
;
8122 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8123 space
= isl_multi_val_get_space(mv
);
8124 mupa
= isl_multi_union_pw_aff_alloc(space
);
8125 for (i
= 0; i
< n
; ++i
) {
8127 isl_union_pw_aff
*upa
;
8129 v
= isl_multi_val_get_val(mv
, i
);
8130 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8132 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8135 isl_union_set_free(domain
);
8136 isl_multi_val_free(mv
);
8139 isl_union_set_free(domain
);
8140 isl_multi_val_free(mv
);
8144 /* Return a multiple union piecewise affine expression
8145 * that is equal to "mv" on "domain".
8147 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8148 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8150 isl_bool equal_params
;
8154 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8155 if (equal_params
< 0)
8158 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8160 domain
= isl_union_set_align_params(domain
,
8161 isl_multi_val_get_space(mv
));
8162 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8163 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8165 isl_union_set_free(domain
);
8166 isl_multi_val_free(mv
);
8170 /* Return a multiple union piecewise affine expression
8171 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8172 * have been aligned.
8174 static __isl_give isl_multi_union_pw_aff
*
8175 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8176 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8180 isl_multi_union_pw_aff
*mupa
;
8185 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8186 space
= isl_multi_aff_get_space(ma
);
8187 mupa
= isl_multi_union_pw_aff_alloc(space
);
8188 for (i
= 0; i
< n
; ++i
) {
8190 isl_union_pw_aff
*upa
;
8192 aff
= isl_multi_aff_get_aff(ma
, i
);
8193 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8195 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8198 isl_union_set_free(domain
);
8199 isl_multi_aff_free(ma
);
8202 isl_union_set_free(domain
);
8203 isl_multi_aff_free(ma
);
8207 /* Return a multiple union piecewise affine expression
8208 * that is equal to "ma" on "domain".
8210 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8211 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8213 isl_bool equal_params
;
8217 equal_params
= isl_space_has_equal_params(domain
->dim
, ma
->space
);
8218 if (equal_params
< 0)
8221 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8223 domain
= isl_union_set_align_params(domain
,
8224 isl_multi_aff_get_space(ma
));
8225 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8226 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8228 isl_union_set_free(domain
);
8229 isl_multi_aff_free(ma
);
8233 /* Return a union set containing those elements in the domains
8234 * of the elements of "mupa" where they are all zero.
8236 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8237 __isl_take isl_multi_union_pw_aff
*mupa
)
8240 isl_union_pw_aff
*upa
;
8241 isl_union_set
*zero
;
8246 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8248 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8249 "cannot determine zero set "
8250 "of zero-dimensional function", goto error
);
8252 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8253 zero
= isl_union_pw_aff_zero_union_set(upa
);
8255 for (i
= 1; i
< n
; ++i
) {
8256 isl_union_set
*zero_i
;
8258 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8259 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8261 zero
= isl_union_set_intersect(zero
, zero_i
);
8264 isl_multi_union_pw_aff_free(mupa
);
8267 isl_multi_union_pw_aff_free(mupa
);
8271 /* Construct a union map mapping the shared domain
8272 * of the union piecewise affine expressions to the range of "mupa"
8273 * with each dimension in the range equated to the
8274 * corresponding union piecewise affine expression.
8276 * The input cannot be zero-dimensional as there is
8277 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8279 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8280 __isl_take isl_multi_union_pw_aff
*mupa
)
8284 isl_union_map
*umap
;
8285 isl_union_pw_aff
*upa
;
8290 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8292 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8293 "cannot determine domain of zero-dimensional "
8294 "isl_multi_union_pw_aff", goto error
);
8296 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8297 umap
= isl_union_map_from_union_pw_aff(upa
);
8299 for (i
= 1; i
< n
; ++i
) {
8300 isl_union_map
*umap_i
;
8302 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8303 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8304 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8307 space
= isl_multi_union_pw_aff_get_space(mupa
);
8308 umap
= isl_union_map_reset_range_space(umap
, space
);
8310 isl_multi_union_pw_aff_free(mupa
);
8313 isl_multi_union_pw_aff_free(mupa
);
8317 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8318 * "range" is the space from which to set the range space.
8319 * "res" collects the results.
8321 struct isl_union_pw_multi_aff_reset_range_space_data
{
8323 isl_union_pw_multi_aff
*res
;
8326 /* Replace the range space of "pma" by the range space of data->range and
8327 * add the result to data->res.
8329 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8331 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8334 space
= isl_pw_multi_aff_get_space(pma
);
8335 space
= isl_space_domain(space
);
8336 space
= isl_space_extend_domain_with_range(space
,
8337 isl_space_copy(data
->range
));
8338 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8339 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8341 return data
->res
? isl_stat_ok
: isl_stat_error
;
8344 /* Replace the range space of all the piecewise affine expressions in "upma" by
8345 * the range space of "space".
8347 * This assumes that all these expressions have the same output dimension.
8349 * Since the spaces of the expressions change, so do their hash values.
8350 * We therefore need to create a new isl_union_pw_multi_aff.
8351 * Note that the hash value is currently computed based on the entire
8352 * space even though there can only be a single expression with a given
8355 static __isl_give isl_union_pw_multi_aff
*
8356 isl_union_pw_multi_aff_reset_range_space(
8357 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8359 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8360 isl_space
*space_upma
;
8362 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8363 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8364 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8365 &reset_range_space
, &data
) < 0)
8366 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8368 isl_space_free(space
);
8369 isl_union_pw_multi_aff_free(upma
);
8373 /* Construct and return a union piecewise multi affine expression
8374 * that is equal to the given multi union piecewise affine expression.
8376 * In order to be able to perform the conversion, the input
8377 * needs to have a least one output dimension.
8379 __isl_give isl_union_pw_multi_aff
*
8380 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8381 __isl_take isl_multi_union_pw_aff
*mupa
)
8385 isl_union_pw_multi_aff
*upma
;
8386 isl_union_pw_aff
*upa
;
8391 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8393 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8394 "cannot determine domain of zero-dimensional "
8395 "isl_multi_union_pw_aff", goto error
);
8397 space
= isl_multi_union_pw_aff_get_space(mupa
);
8398 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8399 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8401 for (i
= 1; i
< n
; ++i
) {
8402 isl_union_pw_multi_aff
*upma_i
;
8404 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8405 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8406 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8409 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8411 isl_multi_union_pw_aff_free(mupa
);
8414 isl_multi_union_pw_aff_free(mupa
);
8418 /* Intersect the range of "mupa" with "range".
8419 * That is, keep only those domain elements that have a function value
8422 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8423 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8425 isl_union_pw_multi_aff
*upma
;
8426 isl_union_set
*domain
;
8431 if (!mupa
|| !range
)
8434 space
= isl_set_get_space(range
);
8435 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8436 space
, isl_dim_set
);
8437 isl_space_free(space
);
8441 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8442 "space don't match", goto error
);
8443 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8445 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8446 "cannot intersect range of zero-dimensional "
8447 "isl_multi_union_pw_aff", goto error
);
8449 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8450 isl_multi_union_pw_aff_copy(mupa
));
8451 domain
= isl_union_set_from_set(range
);
8452 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8453 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8457 isl_multi_union_pw_aff_free(mupa
);
8458 isl_set_free(range
);
8462 /* Return the shared domain of the elements of "mupa".
8464 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8465 __isl_take isl_multi_union_pw_aff
*mupa
)
8468 isl_union_pw_aff
*upa
;
8474 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8476 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8477 "cannot determine domain", goto error
);
8479 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8480 dom
= isl_union_pw_aff_domain(upa
);
8481 for (i
= 1; i
< n
; ++i
) {
8482 isl_union_set
*dom_i
;
8484 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8485 dom_i
= isl_union_pw_aff_domain(upa
);
8486 dom
= isl_union_set_intersect(dom
, dom_i
);
8489 isl_multi_union_pw_aff_free(mupa
);
8492 isl_multi_union_pw_aff_free(mupa
);
8496 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8497 * In particular, the spaces have been aligned.
8498 * The result is defined over the shared domain of the elements of "mupa"
8500 * We first extract the parametric constant part of "aff" and
8501 * define that over the shared domain.
8502 * Then we iterate over all input dimensions of "aff" and add the corresponding
8503 * multiples of the elements of "mupa".
8504 * Finally, we consider the integer divisions, calling the function
8505 * recursively to obtain an isl_union_pw_aff corresponding to the
8506 * integer division argument.
8508 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8509 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8512 isl_union_pw_aff
*upa
;
8513 isl_union_set
*uset
;
8517 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8518 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8520 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8521 cst
= isl_aff_copy(aff
);
8522 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8523 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8524 cst
= isl_aff_project_domain_on_params(cst
);
8525 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8527 for (i
= 0; i
< n_in
; ++i
) {
8528 isl_union_pw_aff
*upa_i
;
8530 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8532 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8533 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8534 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8535 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8538 for (i
= 0; i
< n_div
; ++i
) {
8540 isl_union_pw_aff
*upa_i
;
8542 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8544 div
= isl_aff_get_div(aff
, i
);
8545 upa_i
= multi_union_pw_aff_apply_aff(
8546 isl_multi_union_pw_aff_copy(mupa
), div
);
8547 upa_i
= isl_union_pw_aff_floor(upa_i
);
8548 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8549 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8550 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8553 isl_multi_union_pw_aff_free(mupa
);
8559 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8560 * with the domain of "aff".
8561 * Furthermore, the dimension of this space needs to be greater than zero.
8562 * The result is defined over the shared domain of the elements of "mupa"
8564 * We perform these checks and then hand over control to
8565 * multi_union_pw_aff_apply_aff.
8567 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8568 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8570 isl_space
*space1
, *space2
;
8573 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8574 isl_aff_get_space(aff
));
8575 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8579 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8580 space2
= isl_aff_get_domain_space(aff
);
8581 equal
= isl_space_is_equal(space1
, space2
);
8582 isl_space_free(space1
);
8583 isl_space_free(space2
);
8587 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8588 "spaces don't match", goto error
);
8589 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8590 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8591 "cannot determine domains", goto error
);
8593 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8595 isl_multi_union_pw_aff_free(mupa
);
8600 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8601 * with the domain of "ma".
8602 * Furthermore, the dimension of this space needs to be greater than zero,
8603 * unless the dimension of the target space of "ma" is also zero.
8604 * The result is defined over the shared domain of the elements of "mupa"
8606 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8607 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8609 isl_space
*space1
, *space2
;
8610 isl_multi_union_pw_aff
*res
;
8614 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8615 isl_multi_aff_get_space(ma
));
8616 ma
= isl_multi_aff_align_params(ma
,
8617 isl_multi_union_pw_aff_get_space(mupa
));
8621 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8622 space2
= isl_multi_aff_get_domain_space(ma
);
8623 equal
= isl_space_is_equal(space1
, space2
);
8624 isl_space_free(space1
);
8625 isl_space_free(space2
);
8629 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8630 "spaces don't match", goto error
);
8631 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8632 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8633 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8634 "cannot determine domains", goto error
);
8636 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8637 res
= isl_multi_union_pw_aff_alloc(space1
);
8639 for (i
= 0; i
< n_out
; ++i
) {
8641 isl_union_pw_aff
*upa
;
8643 aff
= isl_multi_aff_get_aff(ma
, i
);
8644 upa
= multi_union_pw_aff_apply_aff(
8645 isl_multi_union_pw_aff_copy(mupa
), aff
);
8646 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8649 isl_multi_aff_free(ma
);
8650 isl_multi_union_pw_aff_free(mupa
);
8653 isl_multi_union_pw_aff_free(mupa
);
8654 isl_multi_aff_free(ma
);
8658 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8659 * with the domain of "pa".
8660 * Furthermore, the dimension of this space needs to be greater than zero.
8661 * The result is defined over the shared domain of the elements of "mupa"
8663 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8664 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8668 isl_space
*space
, *space2
;
8669 isl_union_pw_aff
*upa
;
8671 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8672 isl_pw_aff_get_space(pa
));
8673 pa
= isl_pw_aff_align_params(pa
,
8674 isl_multi_union_pw_aff_get_space(mupa
));
8678 space
= isl_multi_union_pw_aff_get_space(mupa
);
8679 space2
= isl_pw_aff_get_domain_space(pa
);
8680 equal
= isl_space_is_equal(space
, space2
);
8681 isl_space_free(space
);
8682 isl_space_free(space2
);
8686 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8687 "spaces don't match", goto error
);
8688 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8689 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8690 "cannot determine domains", goto error
);
8692 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8693 upa
= isl_union_pw_aff_empty(space
);
8695 for (i
= 0; i
< pa
->n
; ++i
) {
8698 isl_multi_union_pw_aff
*mupa_i
;
8699 isl_union_pw_aff
*upa_i
;
8701 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8702 domain
= isl_set_copy(pa
->p
[i
].set
);
8703 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8704 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8705 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8706 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8709 isl_multi_union_pw_aff_free(mupa
);
8710 isl_pw_aff_free(pa
);
8713 isl_multi_union_pw_aff_free(mupa
);
8714 isl_pw_aff_free(pa
);
8718 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8719 * with the domain of "pma".
8720 * Furthermore, the dimension of this space needs to be greater than zero,
8721 * unless the dimension of the target space of "pma" is also zero.
8722 * The result is defined over the shared domain of the elements of "mupa"
8724 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8725 __isl_take isl_multi_union_pw_aff
*mupa
,
8726 __isl_take isl_pw_multi_aff
*pma
)
8728 isl_space
*space1
, *space2
;
8729 isl_multi_union_pw_aff
*res
;
8733 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8734 isl_pw_multi_aff_get_space(pma
));
8735 pma
= isl_pw_multi_aff_align_params(pma
,
8736 isl_multi_union_pw_aff_get_space(mupa
));
8740 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8741 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8742 equal
= isl_space_is_equal(space1
, space2
);
8743 isl_space_free(space1
);
8744 isl_space_free(space2
);
8748 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8749 "spaces don't match", goto error
);
8750 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8751 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8752 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8753 "cannot determine domains", goto error
);
8755 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8756 res
= isl_multi_union_pw_aff_alloc(space1
);
8758 for (i
= 0; i
< n_out
; ++i
) {
8760 isl_union_pw_aff
*upa
;
8762 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8763 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8764 isl_multi_union_pw_aff_copy(mupa
), pa
);
8765 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8768 isl_pw_multi_aff_free(pma
);
8769 isl_multi_union_pw_aff_free(mupa
);
8772 isl_multi_union_pw_aff_free(mupa
);
8773 isl_pw_multi_aff_free(pma
);
8777 /* Compute the pullback of "mupa" by the function represented by "upma".
8778 * In other words, plug in "upma" in "mupa". The result contains
8779 * expressions defined over the domain space of "upma".
8781 * Run over all elements of "mupa" and plug in "upma" in each of them.
8783 __isl_give isl_multi_union_pw_aff
*
8784 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8785 __isl_take isl_multi_union_pw_aff
*mupa
,
8786 __isl_take isl_union_pw_multi_aff
*upma
)
8790 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8791 isl_union_pw_multi_aff_get_space(upma
));
8792 upma
= isl_union_pw_multi_aff_align_params(upma
,
8793 isl_multi_union_pw_aff_get_space(mupa
));
8797 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8798 for (i
= 0; i
< n
; ++i
) {
8799 isl_union_pw_aff
*upa
;
8801 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8802 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8803 isl_union_pw_multi_aff_copy(upma
));
8804 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8807 isl_union_pw_multi_aff_free(upma
);
8810 isl_multi_union_pw_aff_free(mupa
);
8811 isl_union_pw_multi_aff_free(upma
);
8815 /* Extract the sequence of elements in "mupa" with domain space "space"
8816 * (ignoring parameters).
8818 * For the elements of "mupa" that are not defined on the specified space,
8819 * the corresponding element in the result is empty.
8821 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8822 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8825 isl_bool equal_params
;
8826 isl_space
*space_mpa
= NULL
;
8827 isl_multi_pw_aff
*mpa
;
8829 if (!mupa
|| !space
)
8832 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8833 equal_params
= isl_space_has_equal_params(space_mpa
, space
);
8834 if (equal_params
< 0)
8836 if (!equal_params
) {
8837 space
= isl_space_drop_dims(space
, isl_dim_param
,
8838 0, isl_space_dim(space
, isl_dim_param
));
8839 space
= isl_space_align_params(space
,
8840 isl_space_copy(space_mpa
));
8844 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8846 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8848 space
= isl_space_from_domain(space
);
8849 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8850 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8851 for (i
= 0; i
< n
; ++i
) {
8852 isl_union_pw_aff
*upa
;
8855 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8856 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8857 isl_space_copy(space
));
8858 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8859 isl_union_pw_aff_free(upa
);
8862 isl_space_free(space
);
8865 isl_space_free(space_mpa
);
8866 isl_space_free(space
);