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 /* Look for any divs in the aff->ls with a denominator equal to one
1278 * and plug them into the affine expression and any subsequent divs
1279 * that may reference the div.
1281 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1287 isl_local_space
*ls
;
1293 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1295 for (i
= 0; i
< n
; ++i
) {
1296 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1298 ls
= isl_local_space_copy(aff
->ls
);
1299 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1300 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1301 vec
= isl_vec_copy(aff
->v
);
1302 vec
= isl_vec_cow(vec
);
1308 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1309 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1314 isl_vec_free(aff
->v
);
1316 isl_local_space_free(aff
->ls
);
1323 isl_local_space_free(ls
);
1324 return isl_aff_free(aff
);
1327 /* Look for any divs j that appear with a unit coefficient inside
1328 * the definitions of other divs i and plug them into the definitions
1331 * In particular, an expression of the form
1333 * floor((f(..) + floor(g(..)/n))/m)
1337 * floor((n * f(..) + g(..))/(n * m))
1339 * This simplification is correct because we can move the expression
1340 * f(..) into the inner floor in the original expression to obtain
1342 * floor(floor((n * f(..) + g(..))/n)/m)
1344 * from which we can derive the simplified expression.
1346 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1354 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1355 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1356 for (i
= 1; i
< n
; ++i
) {
1357 for (j
= 0; j
< i
; ++j
) {
1358 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1360 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1361 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1362 aff
->v
->size
, i
, 1);
1364 return isl_aff_free(aff
);
1371 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1373 * Even though this function is only called on isl_affs with a single
1374 * reference, we are careful to only change aff->v and aff->ls together.
1376 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1378 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1379 isl_local_space
*ls
;
1382 ls
= isl_local_space_copy(aff
->ls
);
1383 ls
= isl_local_space_swap_div(ls
, a
, b
);
1384 v
= isl_vec_copy(aff
->v
);
1389 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1390 isl_vec_free(aff
->v
);
1392 isl_local_space_free(aff
->ls
);
1398 isl_local_space_free(ls
);
1399 return isl_aff_free(aff
);
1402 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1404 * We currently do not actually remove div "b", but simply add its
1405 * coefficient to that of "a" and then zero it out.
1407 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1409 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1411 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1414 aff
->v
= isl_vec_cow(aff
->v
);
1416 return isl_aff_free(aff
);
1418 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1419 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1420 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1425 /* Sort the divs in the local space of "aff" according to
1426 * the comparison function "cmp_row" in isl_local_space.c,
1427 * combining the coefficients of identical divs.
1429 * Reordering divs does not change the semantics of "aff",
1430 * so there is no need to call isl_aff_cow.
1431 * Moreover, this function is currently only called on isl_affs
1432 * with a single reference.
1434 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1441 n
= isl_aff_dim(aff
, isl_dim_div
);
1442 for (i
= 1; i
< n
; ++i
) {
1443 for (j
= i
- 1; j
>= 0; --j
) {
1444 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1448 aff
= merge_divs(aff
, j
, j
+ 1);
1450 aff
= swap_div(aff
, j
, j
+ 1);
1459 /* Normalize the representation of "aff".
1461 * This function should only be called of "new" isl_affs, i.e.,
1462 * with only a single reference. We therefore do not need to
1463 * worry about affecting other instances.
1465 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1469 aff
->v
= isl_vec_normalize(aff
->v
);
1471 return isl_aff_free(aff
);
1472 aff
= plug_in_integral_divs(aff
);
1473 aff
= plug_in_unit_divs(aff
);
1474 aff
= sort_divs(aff
);
1475 aff
= isl_aff_remove_unused_divs(aff
);
1479 /* Given f, return floor(f).
1480 * If f is an integer expression, then just return f.
1481 * If f is a constant, then return the constant floor(f).
1482 * Otherwise, if f = g/m, write g = q m + r,
1483 * create a new div d = [r/m] and return the expression q + d.
1484 * The coefficients in r are taken to lie between -m/2 and m/2.
1486 * reduce_div_coefficients performs the same normalization.
1488 * As a special case, floor(NaN) = NaN.
1490 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1500 if (isl_aff_is_nan(aff
))
1502 if (isl_int_is_one(aff
->v
->el
[0]))
1505 aff
= isl_aff_cow(aff
);
1509 aff
->v
= isl_vec_cow(aff
->v
);
1511 return isl_aff_free(aff
);
1513 if (isl_aff_is_cst(aff
)) {
1514 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1515 isl_int_set_si(aff
->v
->el
[0], 1);
1519 div
= isl_vec_copy(aff
->v
);
1520 div
= isl_vec_cow(div
);
1522 return isl_aff_free(aff
);
1524 ctx
= isl_aff_get_ctx(aff
);
1525 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1526 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1527 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1528 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1529 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1530 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1531 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1535 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1537 return isl_aff_free(aff
);
1539 size
= aff
->v
->size
;
1540 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1542 return isl_aff_free(aff
);
1543 isl_int_set_si(aff
->v
->el
[0], 1);
1544 isl_int_set_si(aff
->v
->el
[size
], 1);
1546 aff
= isl_aff_normalize(aff
);
1553 * aff mod m = aff - m * floor(aff/m)
1555 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1559 res
= isl_aff_copy(aff
);
1560 aff
= isl_aff_scale_down(aff
, m
);
1561 aff
= isl_aff_floor(aff
);
1562 aff
= isl_aff_scale(aff
, m
);
1563 res
= isl_aff_sub(res
, aff
);
1570 * aff mod m = aff - m * floor(aff/m)
1572 * with m an integer value.
1574 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1575 __isl_take isl_val
*m
)
1582 if (!isl_val_is_int(m
))
1583 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1584 "expecting integer modulo", goto error
);
1586 res
= isl_aff_copy(aff
);
1587 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1588 aff
= isl_aff_floor(aff
);
1589 aff
= isl_aff_scale_val(aff
, m
);
1590 res
= isl_aff_sub(res
, aff
);
1601 * pwaff mod m = pwaff - m * floor(pwaff/m)
1603 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1607 res
= isl_pw_aff_copy(pwaff
);
1608 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1609 pwaff
= isl_pw_aff_floor(pwaff
);
1610 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1611 res
= isl_pw_aff_sub(res
, pwaff
);
1618 * pa mod m = pa - m * floor(pa/m)
1620 * with m an integer value.
1622 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1623 __isl_take isl_val
*m
)
1627 if (!isl_val_is_int(m
))
1628 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1629 "expecting integer modulo", goto error
);
1630 pa
= isl_pw_aff_mod(pa
, m
->n
);
1634 isl_pw_aff_free(pa
);
1639 /* Given f, return ceil(f).
1640 * If f is an integer expression, then just return f.
1641 * Otherwise, let f be the expression
1647 * floor((e + m - 1)/m)
1649 * As a special case, ceil(NaN) = NaN.
1651 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1656 if (isl_aff_is_nan(aff
))
1658 if (isl_int_is_one(aff
->v
->el
[0]))
1661 aff
= isl_aff_cow(aff
);
1664 aff
->v
= isl_vec_cow(aff
->v
);
1666 return isl_aff_free(aff
);
1668 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1669 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1670 aff
= isl_aff_floor(aff
);
1675 /* Apply the expansion computed by isl_merge_divs.
1676 * The expansion itself is given by "exp" while the resulting
1677 * list of divs is given by "div".
1679 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1680 __isl_take isl_mat
*div
, int *exp
)
1686 aff
= isl_aff_cow(aff
);
1690 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1691 new_n_div
= isl_mat_rows(div
);
1692 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1694 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1695 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1696 if (!aff
->v
|| !aff
->ls
)
1697 return isl_aff_free(aff
);
1705 /* Add two affine expressions that live in the same local space.
1707 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1708 __isl_take isl_aff
*aff2
)
1712 aff1
= isl_aff_cow(aff1
);
1716 aff1
->v
= isl_vec_cow(aff1
->v
);
1722 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1723 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1724 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1725 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1726 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1727 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1728 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1740 /* Return the sum of "aff1" and "aff2".
1742 * If either of the two is NaN, then the result is NaN.
1744 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1745 __isl_take isl_aff
*aff2
)
1756 ctx
= isl_aff_get_ctx(aff1
);
1757 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1758 isl_die(ctx
, isl_error_invalid
,
1759 "spaces don't match", goto error
);
1761 if (isl_aff_is_nan(aff1
)) {
1765 if (isl_aff_is_nan(aff2
)) {
1770 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1771 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1772 if (n_div1
== 0 && n_div2
== 0)
1773 return add_expanded(aff1
, aff2
);
1775 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1776 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1777 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1780 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1781 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1782 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1786 return add_expanded(aff1
, aff2
);
1795 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1796 __isl_take isl_aff
*aff2
)
1798 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1801 /* Return the result of scaling "aff" by a factor of "f".
1803 * As a special case, f * NaN = NaN.
1805 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1811 if (isl_aff_is_nan(aff
))
1814 if (isl_int_is_one(f
))
1817 aff
= isl_aff_cow(aff
);
1820 aff
->v
= isl_vec_cow(aff
->v
);
1822 return isl_aff_free(aff
);
1824 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1825 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1830 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1831 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1832 isl_int_divexact(gcd
, f
, gcd
);
1833 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1839 /* Multiple "aff" by "v".
1841 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1842 __isl_take isl_val
*v
)
1847 if (isl_val_is_one(v
)) {
1852 if (!isl_val_is_rat(v
))
1853 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1854 "expecting rational factor", goto error
);
1856 aff
= isl_aff_scale(aff
, v
->n
);
1857 aff
= isl_aff_scale_down(aff
, v
->d
);
1867 /* Return the result of scaling "aff" down by a factor of "f".
1869 * As a special case, NaN/f = NaN.
1871 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1877 if (isl_aff_is_nan(aff
))
1880 if (isl_int_is_one(f
))
1883 aff
= isl_aff_cow(aff
);
1887 if (isl_int_is_zero(f
))
1888 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1889 "cannot scale down by zero", return isl_aff_free(aff
));
1891 aff
->v
= isl_vec_cow(aff
->v
);
1893 return isl_aff_free(aff
);
1896 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1897 isl_int_gcd(gcd
, gcd
, f
);
1898 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1899 isl_int_divexact(gcd
, f
, gcd
);
1900 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1906 /* Divide "aff" by "v".
1908 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1909 __isl_take isl_val
*v
)
1914 if (isl_val_is_one(v
)) {
1919 if (!isl_val_is_rat(v
))
1920 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1921 "expecting rational factor", goto error
);
1922 if (!isl_val_is_pos(v
))
1923 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1924 "factor needs to be positive", goto error
);
1926 aff
= isl_aff_scale(aff
, v
->d
);
1927 aff
= isl_aff_scale_down(aff
, v
->n
);
1937 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1945 isl_int_set_ui(v
, f
);
1946 aff
= isl_aff_scale_down(aff
, v
);
1952 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1953 enum isl_dim_type type
, unsigned pos
, const char *s
)
1955 aff
= isl_aff_cow(aff
);
1958 if (type
== isl_dim_out
)
1959 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1960 "cannot set name of output/set dimension",
1961 return isl_aff_free(aff
));
1962 if (type
== isl_dim_in
)
1964 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1966 return isl_aff_free(aff
);
1971 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1972 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1974 aff
= isl_aff_cow(aff
);
1977 if (type
== isl_dim_out
)
1978 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1979 "cannot set name of output/set dimension",
1981 if (type
== isl_dim_in
)
1983 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1985 return isl_aff_free(aff
);
1994 /* Replace the identifier of the input tuple of "aff" by "id".
1995 * type is currently required to be equal to isl_dim_in
1997 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1998 enum isl_dim_type type
, __isl_take isl_id
*id
)
2000 aff
= isl_aff_cow(aff
);
2003 if (type
!= isl_dim_out
)
2004 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2005 "cannot only set id of input tuple", goto error
);
2006 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2008 return isl_aff_free(aff
);
2017 /* Exploit the equalities in "eq" to simplify the affine expression
2018 * and the expressions of the integer divisions in the local space.
2019 * The integer divisions in this local space are assumed to appear
2020 * as regular dimensions in "eq".
2022 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2023 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2031 if (eq
->n_eq
== 0) {
2032 isl_basic_set_free(eq
);
2036 aff
= isl_aff_cow(aff
);
2040 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2041 isl_basic_set_copy(eq
));
2042 aff
->v
= isl_vec_cow(aff
->v
);
2043 if (!aff
->ls
|| !aff
->v
)
2046 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2048 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2049 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2050 if (j
< 0 || j
== 0 || j
>= total
)
2053 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2057 isl_basic_set_free(eq
);
2058 aff
= isl_aff_normalize(aff
);
2061 isl_basic_set_free(eq
);
2066 /* Exploit the equalities in "eq" to simplify the affine expression
2067 * and the expressions of the integer divisions in the local space.
2069 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2070 __isl_take isl_basic_set
*eq
)
2076 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2078 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2079 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2081 isl_basic_set_free(eq
);
2086 /* Look for equalities among the variables shared by context and aff
2087 * and the integer divisions of aff, if any.
2088 * The equalities are then used to eliminate coefficients and/or integer
2089 * divisions from aff.
2091 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2092 __isl_take isl_set
*context
)
2094 isl_basic_set
*hull
;
2099 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2101 isl_basic_set
*bset
;
2102 isl_local_space
*ls
;
2103 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2104 ls
= isl_aff_get_domain_local_space(aff
);
2105 bset
= isl_basic_set_from_local_space(ls
);
2106 bset
= isl_basic_set_lift(bset
);
2107 bset
= isl_basic_set_flatten(bset
);
2108 context
= isl_set_intersect(context
,
2109 isl_set_from_basic_set(bset
));
2112 hull
= isl_set_affine_hull(context
);
2113 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2116 isl_set_free(context
);
2120 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2121 __isl_take isl_set
*context
)
2123 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2124 dom_context
= isl_set_intersect_params(dom_context
, context
);
2125 return isl_aff_gist(aff
, dom_context
);
2128 /* Return a basic set containing those elements in the space
2129 * of aff where it is positive. "rational" should not be set.
2131 * If "aff" is NaN, then it is not positive.
2133 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2136 isl_constraint
*ineq
;
2137 isl_basic_set
*bset
;
2142 if (isl_aff_is_nan(aff
)) {
2143 isl_space
*space
= isl_aff_get_domain_space(aff
);
2145 return isl_basic_set_empty(space
);
2148 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2149 "rational sets not supported", goto error
);
2151 ineq
= isl_inequality_from_aff(aff
);
2152 c
= isl_constraint_get_constant_val(ineq
);
2153 c
= isl_val_sub_ui(c
, 1);
2154 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2156 bset
= isl_basic_set_from_constraint(ineq
);
2157 bset
= isl_basic_set_simplify(bset
);
2164 /* Return a basic set containing those elements in the space
2165 * of aff where it is non-negative.
2166 * If "rational" is set, then return a rational basic set.
2168 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2171 __isl_take isl_aff
*aff
, int rational
)
2173 isl_constraint
*ineq
;
2174 isl_basic_set
*bset
;
2178 if (isl_aff_is_nan(aff
)) {
2179 isl_space
*space
= isl_aff_get_domain_space(aff
);
2181 return isl_basic_set_empty(space
);
2184 ineq
= isl_inequality_from_aff(aff
);
2186 bset
= isl_basic_set_from_constraint(ineq
);
2188 bset
= isl_basic_set_set_rational(bset
);
2189 bset
= isl_basic_set_simplify(bset
);
2193 /* Return a basic set containing those elements in the space
2194 * of aff where it is non-negative.
2196 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2198 return aff_nonneg_basic_set(aff
, 0);
2201 /* Return a basic set containing those elements in the domain space
2202 * of "aff" where it is positive.
2204 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2206 aff
= isl_aff_add_constant_num_si(aff
, -1);
2207 return isl_aff_nonneg_basic_set(aff
);
2210 /* Return a basic set containing those elements in the domain space
2211 * of aff where it is negative.
2213 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2215 aff
= isl_aff_neg(aff
);
2216 return isl_aff_pos_basic_set(aff
);
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is zero.
2221 * If "rational" is set, then return a rational basic set.
2223 * If "aff" is NaN, then it is not zero.
2225 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2228 isl_constraint
*ineq
;
2229 isl_basic_set
*bset
;
2233 if (isl_aff_is_nan(aff
)) {
2234 isl_space
*space
= isl_aff_get_domain_space(aff
);
2236 return isl_basic_set_empty(space
);
2239 ineq
= isl_equality_from_aff(aff
);
2241 bset
= isl_basic_set_from_constraint(ineq
);
2243 bset
= isl_basic_set_set_rational(bset
);
2244 bset
= isl_basic_set_simplify(bset
);
2248 /* Return a basic set containing those elements in the space
2249 * of aff where it is zero.
2251 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2253 return aff_zero_basic_set(aff
, 0);
2256 /* Return a basic set containing those elements in the shared space
2257 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2259 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2260 __isl_take isl_aff
*aff2
)
2262 aff1
= isl_aff_sub(aff1
, aff2
);
2264 return isl_aff_nonneg_basic_set(aff1
);
2267 /* Return a basic set containing those elements in the shared domain space
2268 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2270 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2271 __isl_take isl_aff
*aff2
)
2273 aff1
= isl_aff_sub(aff1
, aff2
);
2275 return isl_aff_pos_basic_set(aff1
);
2278 /* Return a 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_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2282 __isl_take isl_aff
*aff2
)
2284 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2287 /* Return a set containing those elements in the shared domain space
2288 * of aff1 and aff2 where aff1 is greater than aff2.
2290 * If either of the two inputs is NaN, then the result is empty,
2291 * as comparisons with NaN always return false.
2293 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2294 __isl_take isl_aff
*aff2
)
2296 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2299 /* Return a basic set containing those elements in the shared space
2300 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2302 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2303 __isl_take isl_aff
*aff2
)
2305 return isl_aff_ge_basic_set(aff2
, aff1
);
2308 /* Return a basic set containing those elements in the shared domain space
2309 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2311 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2312 __isl_take isl_aff
*aff2
)
2314 return isl_aff_gt_basic_set(aff2
, aff1
);
2317 /* Return a set containing those elements in the shared space
2318 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2320 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2321 __isl_take isl_aff
*aff2
)
2323 return isl_aff_ge_set(aff2
, aff1
);
2326 /* Return a set containing those elements in the shared domain space
2327 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2329 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2330 __isl_take isl_aff
*aff2
)
2332 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2335 /* Return a basic set containing those elements in the shared space
2336 * of aff1 and aff2 where aff1 and aff2 are equal.
2338 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2339 __isl_take isl_aff
*aff2
)
2341 aff1
= isl_aff_sub(aff1
, aff2
);
2343 return isl_aff_zero_basic_set(aff1
);
2346 /* Return a set containing those elements in the shared space
2347 * of aff1 and aff2 where aff1 and aff2 are equal.
2349 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2350 __isl_take isl_aff
*aff2
)
2352 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2355 /* Return a set containing those elements in the shared domain space
2356 * of aff1 and aff2 where aff1 and aff2 are not equal.
2358 * If either of the two inputs is NaN, then the result is empty,
2359 * as comparisons with NaN always return false.
2361 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2362 __isl_take isl_aff
*aff2
)
2364 isl_set
*set_lt
, *set_gt
;
2366 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2367 isl_aff_copy(aff2
));
2368 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2369 return isl_set_union_disjoint(set_lt
, set_gt
);
2372 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2373 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2375 aff1
= isl_aff_add(aff1
, aff2
);
2376 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2380 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2388 /* Check whether the given affine expression has non-zero coefficient
2389 * for any dimension in the given range or if any of these dimensions
2390 * appear with non-zero coefficients in any of the integer divisions
2391 * involved in the affine expression.
2393 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2394 enum isl_dim_type type
, unsigned first
, unsigned n
)
2399 isl_bool involves
= isl_bool_false
;
2402 return isl_bool_error
;
2404 return isl_bool_false
;
2406 ctx
= isl_aff_get_ctx(aff
);
2407 if (first
+ n
> isl_aff_dim(aff
, type
))
2408 isl_die(ctx
, isl_error_invalid
,
2409 "range out of bounds", return isl_bool_error
);
2411 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2415 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2416 for (i
= 0; i
< n
; ++i
)
2417 if (active
[first
+ i
]) {
2418 involves
= isl_bool_true
;
2427 return isl_bool_error
;
2430 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2431 enum isl_dim_type type
, unsigned first
, unsigned n
)
2437 if (type
== isl_dim_out
)
2438 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2439 "cannot drop output/set dimension",
2440 return isl_aff_free(aff
));
2441 if (type
== isl_dim_in
)
2443 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2446 ctx
= isl_aff_get_ctx(aff
);
2447 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2448 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2449 return isl_aff_free(aff
));
2451 aff
= isl_aff_cow(aff
);
2455 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2457 return isl_aff_free(aff
);
2459 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2460 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2462 return isl_aff_free(aff
);
2467 /* Project the domain of the affine expression onto its parameter space.
2468 * The affine expression may not involve any of the domain dimensions.
2470 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2476 n
= isl_aff_dim(aff
, isl_dim_in
);
2477 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2479 return isl_aff_free(aff
);
2481 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2482 "affine expression involves some of the domain dimensions",
2483 return isl_aff_free(aff
));
2484 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2485 space
= isl_aff_get_domain_space(aff
);
2486 space
= isl_space_params(space
);
2487 aff
= isl_aff_reset_domain_space(aff
, space
);
2491 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2492 enum isl_dim_type type
, unsigned first
, unsigned n
)
2498 if (type
== isl_dim_out
)
2499 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2500 "cannot insert output/set dimensions",
2501 return isl_aff_free(aff
));
2502 if (type
== isl_dim_in
)
2504 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2507 ctx
= isl_aff_get_ctx(aff
);
2508 if (first
> isl_local_space_dim(aff
->ls
, type
))
2509 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2510 return isl_aff_free(aff
));
2512 aff
= isl_aff_cow(aff
);
2516 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2518 return isl_aff_free(aff
);
2520 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2521 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2523 return isl_aff_free(aff
);
2528 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2529 enum isl_dim_type type
, unsigned n
)
2533 pos
= isl_aff_dim(aff
, type
);
2535 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2538 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2539 enum isl_dim_type type
, unsigned n
)
2543 pos
= isl_pw_aff_dim(pwaff
, type
);
2545 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2548 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2549 * to dimensions of "dst_type" at "dst_pos".
2551 * We only support moving input dimensions to parameters and vice versa.
2553 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2554 enum isl_dim_type dst_type
, unsigned dst_pos
,
2555 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2563 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2564 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2567 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2568 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2569 "cannot move output/set dimension",
2570 return isl_aff_free(aff
));
2571 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2572 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2573 "cannot move divs", return isl_aff_free(aff
));
2574 if (dst_type
== isl_dim_in
)
2575 dst_type
= isl_dim_set
;
2576 if (src_type
== isl_dim_in
)
2577 src_type
= isl_dim_set
;
2579 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2580 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2581 "range out of bounds", return isl_aff_free(aff
));
2582 if (dst_type
== src_type
)
2583 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2584 "moving dims within the same type not supported",
2585 return isl_aff_free(aff
));
2587 aff
= isl_aff_cow(aff
);
2591 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2592 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2593 if (dst_type
> src_type
)
2596 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2597 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2598 src_type
, src_pos
, n
);
2599 if (!aff
->v
|| !aff
->ls
)
2600 return isl_aff_free(aff
);
2602 aff
= sort_divs(aff
);
2607 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2609 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2610 return isl_pw_aff_alloc(dom
, aff
);
2613 #define isl_aff_involves_nan isl_aff_is_nan
2616 #define PW isl_pw_aff
2620 #define EL_IS_ZERO is_empty
2624 #define IS_ZERO is_empty
2627 #undef DEFAULT_IS_ZERO
2628 #define DEFAULT_IS_ZERO 0
2635 #include <isl_pw_templ.c>
2636 #include <isl_pw_hash.c>
2637 #include <isl_pw_union_opt.c>
2640 #define UNION isl_union_pw_aff
2642 #define PART isl_pw_aff
2644 #define PARTS pw_aff
2646 #include <isl_union_single.c>
2647 #include <isl_union_neg.c>
2649 static __isl_give isl_set
*align_params_pw_pw_set_and(
2650 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2651 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2652 __isl_take isl_pw_aff
*pwaff2
))
2654 isl_bool equal_params
;
2656 if (!pwaff1
|| !pwaff2
)
2658 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2659 if (equal_params
< 0)
2662 return fn(pwaff1
, pwaff2
);
2663 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2664 !isl_space_has_named_params(pwaff2
->dim
))
2665 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2666 "unaligned unnamed parameters", goto error
);
2667 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2668 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2669 return fn(pwaff1
, pwaff2
);
2671 isl_pw_aff_free(pwaff1
);
2672 isl_pw_aff_free(pwaff2
);
2676 /* Align the parameters of the to isl_pw_aff arguments and
2677 * then apply a function "fn" on them that returns an isl_map.
2679 static __isl_give isl_map
*align_params_pw_pw_map_and(
2680 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2681 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2682 __isl_take isl_pw_aff
*pa2
))
2684 isl_bool equal_params
;
2688 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2689 if (equal_params
< 0)
2692 return fn(pa1
, pa2
);
2693 if (!isl_space_has_named_params(pa1
->dim
) ||
2694 !isl_space_has_named_params(pa2
->dim
))
2695 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2696 "unaligned unnamed parameters", goto error
);
2697 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2698 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2699 return fn(pa1
, pa2
);
2701 isl_pw_aff_free(pa1
);
2702 isl_pw_aff_free(pa2
);
2706 /* Compute a piecewise quasi-affine expression with a domain that
2707 * is the union of those of pwaff1 and pwaff2 and such that on each
2708 * cell, the quasi-affine expression is the maximum of those of pwaff1
2709 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2710 * cell, then the associated expression is the defined one.
2712 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2713 __isl_take isl_pw_aff
*pwaff2
)
2715 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2718 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2719 __isl_take isl_pw_aff
*pwaff2
)
2721 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2725 /* Compute a piecewise quasi-affine expression with a domain that
2726 * is the union of those of pwaff1 and pwaff2 and such that on each
2727 * cell, the quasi-affine expression is the minimum of those of pwaff1
2728 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2729 * cell, then the associated expression is the defined one.
2731 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2732 __isl_take isl_pw_aff
*pwaff2
)
2734 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2737 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2738 __isl_take isl_pw_aff
*pwaff2
)
2740 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2744 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2745 __isl_take isl_pw_aff
*pwaff2
, int max
)
2748 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2750 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2753 /* Construct a map with as domain the domain of pwaff and
2754 * one-dimensional range corresponding to the affine expressions.
2756 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2765 dim
= isl_pw_aff_get_space(pwaff
);
2766 map
= isl_map_empty(dim
);
2768 for (i
= 0; i
< pwaff
->n
; ++i
) {
2769 isl_basic_map
*bmap
;
2772 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2773 map_i
= isl_map_from_basic_map(bmap
);
2774 map_i
= isl_map_intersect_domain(map_i
,
2775 isl_set_copy(pwaff
->p
[i
].set
));
2776 map
= isl_map_union_disjoint(map
, map_i
);
2779 isl_pw_aff_free(pwaff
);
2784 /* Construct a map with as domain the domain of pwaff and
2785 * one-dimensional range corresponding to the affine expressions.
2787 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2791 if (isl_space_is_set(pwaff
->dim
))
2792 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2793 "space of input is not a map", goto error
);
2794 return map_from_pw_aff(pwaff
);
2796 isl_pw_aff_free(pwaff
);
2800 /* Construct a one-dimensional set with as parameter domain
2801 * the domain of pwaff and the single set dimension
2802 * corresponding to the affine expressions.
2804 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2808 if (!isl_space_is_set(pwaff
->dim
))
2809 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2810 "space of input is not a set", goto error
);
2811 return map_from_pw_aff(pwaff
);
2813 isl_pw_aff_free(pwaff
);
2817 /* Return a set containing those elements in the domain
2818 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2819 * does not satisfy "fn" (if complement is 1).
2821 * The pieces with a NaN never belong to the result since
2822 * NaN does not satisfy any property.
2824 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2825 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2834 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2836 for (i
= 0; i
< pwaff
->n
; ++i
) {
2837 isl_basic_set
*bset
;
2838 isl_set
*set_i
, *locus
;
2841 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2844 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2845 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2846 locus
= isl_set_from_basic_set(bset
);
2847 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2849 set_i
= isl_set_subtract(set_i
, locus
);
2851 set_i
= isl_set_intersect(set_i
, locus
);
2852 set
= isl_set_union_disjoint(set
, set_i
);
2855 isl_pw_aff_free(pwaff
);
2860 /* Return a set containing those elements in the domain
2861 * of "pa" where it is positive.
2863 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2865 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2868 /* Return a set containing those elements in the domain
2869 * of pwaff where it is non-negative.
2871 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2873 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2876 /* Return a set containing those elements in the domain
2877 * of pwaff where it is zero.
2879 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2881 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2884 /* Return a set containing those elements in the domain
2885 * of pwaff where it is not zero.
2887 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2889 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2892 /* Return a set containing those elements in the shared domain
2893 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2895 * We compute the difference on the shared domain and then construct
2896 * the set of values where this difference is non-negative.
2897 * If strict is set, we first subtract 1 from the difference.
2898 * If equal is set, we only return the elements where pwaff1 and pwaff2
2901 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2902 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2904 isl_set
*set1
, *set2
;
2906 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2907 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2908 set1
= isl_set_intersect(set1
, set2
);
2909 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2910 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2911 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2914 isl_space
*dim
= isl_set_get_space(set1
);
2916 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2917 aff
= isl_aff_add_constant_si(aff
, -1);
2918 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2923 return isl_pw_aff_zero_set(pwaff1
);
2924 return isl_pw_aff_nonneg_set(pwaff1
);
2927 /* Return a set containing those elements in the shared domain
2928 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2930 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2931 __isl_take isl_pw_aff
*pwaff2
)
2933 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2936 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2937 __isl_take isl_pw_aff
*pwaff2
)
2939 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2942 /* Return a set containing those elements in the shared domain
2943 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2945 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2946 __isl_take isl_pw_aff
*pwaff2
)
2948 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2951 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2952 __isl_take isl_pw_aff
*pwaff2
)
2954 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2957 /* Return a set containing those elements in the shared domain
2958 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2960 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2961 __isl_take isl_pw_aff
*pwaff2
)
2963 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2966 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2967 __isl_take isl_pw_aff
*pwaff2
)
2969 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2972 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2973 __isl_take isl_pw_aff
*pwaff2
)
2975 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2978 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2979 __isl_take isl_pw_aff
*pwaff2
)
2981 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2984 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2985 * where the function values are ordered in the same way as "order",
2986 * which returns a set in the shared domain of its two arguments.
2987 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2989 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2990 * We first pull back the two functions such that they are defined on
2991 * the domain [A -> B]. Then we apply "order", resulting in a set
2992 * in the space [A -> B]. Finally, we unwrap this set to obtain
2993 * a map in the space A -> B.
2995 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2996 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2997 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2998 __isl_take isl_pw_aff
*pa2
))
3000 isl_space
*space1
, *space2
;
3004 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3005 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3006 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3007 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3008 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3009 ma
= isl_multi_aff_range_map(space1
);
3010 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3011 set
= order(pa1
, pa2
);
3013 return isl_set_unwrap(set
);
3016 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3017 * where the function values are equal.
3018 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3020 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3021 __isl_take isl_pw_aff
*pa2
)
3023 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function values are equal.
3029 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3030 __isl_take isl_pw_aff
*pa2
)
3032 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3035 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3036 * where the function value of "pa1" is less than the function value of "pa2".
3037 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3039 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3040 __isl_take isl_pw_aff
*pa2
)
3042 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is less than the function value of "pa2".
3048 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3049 __isl_take isl_pw_aff
*pa2
)
3051 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3054 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3055 * where the function value of "pa1" is greater than the function value
3057 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3059 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3060 __isl_take isl_pw_aff
*pa2
)
3062 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3065 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than the function value
3069 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3070 __isl_take isl_pw_aff
*pa2
)
3072 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3075 /* Return a set containing those elements in the shared domain
3076 * of the elements of list1 and list2 where each element in list1
3077 * has the relation specified by "fn" with each element in list2.
3079 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3080 __isl_take isl_pw_aff_list
*list2
,
3081 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3082 __isl_take isl_pw_aff
*pwaff2
))
3088 if (!list1
|| !list2
)
3091 ctx
= isl_pw_aff_list_get_ctx(list1
);
3092 if (list1
->n
< 1 || list2
->n
< 1)
3093 isl_die(ctx
, isl_error_invalid
,
3094 "list should contain at least one element", goto error
);
3096 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3097 for (i
= 0; i
< list1
->n
; ++i
)
3098 for (j
= 0; j
< list2
->n
; ++j
) {
3101 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3102 isl_pw_aff_copy(list2
->p
[j
]));
3103 set
= isl_set_intersect(set
, set_ij
);
3106 isl_pw_aff_list_free(list1
);
3107 isl_pw_aff_list_free(list2
);
3110 isl_pw_aff_list_free(list1
);
3111 isl_pw_aff_list_free(list2
);
3115 /* Return a set containing those elements in the shared domain
3116 * of the elements of list1 and list2 where each element in list1
3117 * is equal to each element in list2.
3119 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3120 __isl_take isl_pw_aff_list
*list2
)
3122 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3125 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3126 __isl_take isl_pw_aff_list
*list2
)
3128 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3131 /* Return a set containing those elements in the shared domain
3132 * of the elements of list1 and list2 where each element in list1
3133 * is less than or equal to each element in list2.
3135 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3136 __isl_take isl_pw_aff_list
*list2
)
3138 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3141 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3142 __isl_take isl_pw_aff_list
*list2
)
3144 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3147 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3148 __isl_take isl_pw_aff_list
*list2
)
3150 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3153 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3154 __isl_take isl_pw_aff_list
*list2
)
3156 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3160 /* Return a set containing those elements in the shared domain
3161 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3163 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3164 __isl_take isl_pw_aff
*pwaff2
)
3166 isl_set
*set_lt
, *set_gt
;
3168 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3169 isl_pw_aff_copy(pwaff2
));
3170 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3171 return isl_set_union_disjoint(set_lt
, set_gt
);
3174 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3175 __isl_take isl_pw_aff
*pwaff2
)
3177 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3180 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3185 if (isl_int_is_one(v
))
3187 if (!isl_int_is_pos(v
))
3188 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3189 "factor needs to be positive",
3190 return isl_pw_aff_free(pwaff
));
3191 pwaff
= isl_pw_aff_cow(pwaff
);
3197 for (i
= 0; i
< pwaff
->n
; ++i
) {
3198 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3199 if (!pwaff
->p
[i
].aff
)
3200 return isl_pw_aff_free(pwaff
);
3206 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3210 pwaff
= isl_pw_aff_cow(pwaff
);
3216 for (i
= 0; i
< pwaff
->n
; ++i
) {
3217 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3218 if (!pwaff
->p
[i
].aff
)
3219 return isl_pw_aff_free(pwaff
);
3225 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3229 pwaff
= isl_pw_aff_cow(pwaff
);
3235 for (i
= 0; i
< pwaff
->n
; ++i
) {
3236 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3237 if (!pwaff
->p
[i
].aff
)
3238 return isl_pw_aff_free(pwaff
);
3244 /* Assuming that "cond1" and "cond2" are disjoint,
3245 * return an affine expression that is equal to pwaff1 on cond1
3246 * and to pwaff2 on cond2.
3248 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3249 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3250 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3252 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3253 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3255 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3258 /* Return an affine expression that is equal to pwaff_true for elements
3259 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3261 * That is, return cond ? pwaff_true : pwaff_false;
3263 * If "cond" involves and NaN, then we conservatively return a NaN
3264 * on its entire domain. In principle, we could consider the pieces
3265 * where it is NaN separately from those where it is not.
3267 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3268 * then only use the domain of "cond" to restrict the domain.
3270 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3271 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3273 isl_set
*cond_true
, *cond_false
;
3278 if (isl_pw_aff_involves_nan(cond
)) {
3279 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3280 isl_local_space
*ls
= isl_local_space_from_space(space
);
3281 isl_pw_aff_free(cond
);
3282 isl_pw_aff_free(pwaff_true
);
3283 isl_pw_aff_free(pwaff_false
);
3284 return isl_pw_aff_nan_on_domain(ls
);
3287 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3288 isl_pw_aff_get_space(pwaff_false
));
3289 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3290 isl_pw_aff_get_space(pwaff_true
));
3291 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3297 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3298 isl_pw_aff_free(pwaff_false
);
3299 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3302 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3303 cond_false
= isl_pw_aff_zero_set(cond
);
3304 return isl_pw_aff_select(cond_true
, pwaff_true
,
3305 cond_false
, pwaff_false
);
3307 isl_pw_aff_free(cond
);
3308 isl_pw_aff_free(pwaff_true
);
3309 isl_pw_aff_free(pwaff_false
);
3313 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3316 return isl_bool_error
;
3318 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3321 /* Check whether pwaff is a piecewise constant.
3323 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3328 return isl_bool_error
;
3330 for (i
= 0; i
< pwaff
->n
; ++i
) {
3331 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3332 if (is_cst
< 0 || !is_cst
)
3336 return isl_bool_true
;
3339 /* Are all elements of "mpa" piecewise constants?
3341 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3346 return isl_bool_error
;
3348 for (i
= 0; i
< mpa
->n
; ++i
) {
3349 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3350 if (is_cst
< 0 || !is_cst
)
3354 return isl_bool_true
;
3357 /* Return the product of "aff1" and "aff2".
3359 * If either of the two is NaN, then the result is NaN.
3361 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3363 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3364 __isl_take isl_aff
*aff2
)
3369 if (isl_aff_is_nan(aff1
)) {
3373 if (isl_aff_is_nan(aff2
)) {
3378 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3379 return isl_aff_mul(aff2
, aff1
);
3381 if (!isl_aff_is_cst(aff2
))
3382 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3383 "at least one affine expression should be constant",
3386 aff1
= isl_aff_cow(aff1
);
3390 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3391 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3401 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3403 * If either of the two is NaN, then the result is NaN.
3405 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3406 __isl_take isl_aff
*aff2
)
3414 if (isl_aff_is_nan(aff1
)) {
3418 if (isl_aff_is_nan(aff2
)) {
3423 is_cst
= isl_aff_is_cst(aff2
);
3427 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3428 "second argument should be a constant", goto error
);
3433 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3435 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3436 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3439 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3440 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3443 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3444 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3455 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3456 __isl_take isl_pw_aff
*pwaff2
)
3458 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3461 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3462 __isl_take isl_pw_aff
*pwaff2
)
3464 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3467 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3468 __isl_take isl_pw_aff
*pwaff2
)
3470 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3473 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3474 __isl_take isl_pw_aff
*pwaff2
)
3476 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3479 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3480 __isl_take isl_pw_aff
*pwaff2
)
3482 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3485 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3486 __isl_take isl_pw_aff
*pa2
)
3488 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3491 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3493 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3494 __isl_take isl_pw_aff
*pa2
)
3498 is_cst
= isl_pw_aff_is_cst(pa2
);
3502 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3503 "second argument should be a piecewise constant",
3505 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3507 isl_pw_aff_free(pa1
);
3508 isl_pw_aff_free(pa2
);
3512 /* Compute the quotient 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 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3521 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3522 __isl_take isl_pw_aff
*pa2
)
3528 is_cst
= isl_pw_aff_is_cst(pa2
);
3532 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3533 "second argument should be a piecewise constant",
3536 pa1
= isl_pw_aff_div(pa1
, pa2
);
3538 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3539 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3540 c
= isl_pw_aff_ceil(pa1
);
3541 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3543 isl_pw_aff_free(pa1
);
3544 isl_pw_aff_free(pa2
);
3548 /* Compute the remainder of the integer division of "pa1" by "pa2"
3549 * with rounding towards zero.
3550 * "pa2" is assumed to be a piecewise constant.
3552 * In particular, return
3554 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3557 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3558 __isl_take isl_pw_aff
*pa2
)
3563 is_cst
= isl_pw_aff_is_cst(pa2
);
3567 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3568 "second argument should be a piecewise constant",
3570 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3571 res
= isl_pw_aff_mul(pa2
, res
);
3572 res
= isl_pw_aff_sub(pa1
, res
);
3575 isl_pw_aff_free(pa1
);
3576 isl_pw_aff_free(pa2
);
3580 /* Does either of "pa1" or "pa2" involve any NaN2?
3582 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3583 __isl_keep isl_pw_aff
*pa2
)
3587 has_nan
= isl_pw_aff_involves_nan(pa1
);
3588 if (has_nan
< 0 || has_nan
)
3590 return isl_pw_aff_involves_nan(pa2
);
3593 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3594 * by a NaN on their shared domain.
3596 * In principle, the result could be refined to only being NaN
3597 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3599 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3600 __isl_take isl_pw_aff
*pa2
)
3602 isl_local_space
*ls
;
3606 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3607 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3608 pa
= isl_pw_aff_nan_on_domain(ls
);
3609 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3614 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3615 __isl_take isl_pw_aff
*pwaff2
)
3620 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3621 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3622 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3623 isl_pw_aff_copy(pwaff2
));
3624 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3625 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3628 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3629 __isl_take isl_pw_aff
*pwaff2
)
3634 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3635 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3636 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3637 isl_pw_aff_copy(pwaff2
));
3638 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3639 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3642 /* Return an expression for the minimum (if "max" is not set) or
3643 * the maximum (if "max" is set) of "pa1" and "pa2".
3644 * If either expression involves any NaN, then return a NaN
3645 * on the shared domain as result.
3647 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3648 __isl_take isl_pw_aff
*pa2
, int max
)
3652 has_nan
= either_involves_nan(pa1
, pa2
);
3654 pa1
= isl_pw_aff_free(pa1
);
3656 return replace_by_nan(pa1
, pa2
);
3659 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3661 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3664 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3666 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3667 __isl_take isl_pw_aff
*pwaff2
)
3669 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3672 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3674 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3675 __isl_take isl_pw_aff
*pwaff2
)
3677 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3680 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3681 __isl_take isl_pw_aff_list
*list
,
3682 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3683 __isl_take isl_pw_aff
*pwaff2
))
3692 ctx
= isl_pw_aff_list_get_ctx(list
);
3694 isl_die(ctx
, isl_error_invalid
,
3695 "list should contain at least one element", goto error
);
3697 res
= isl_pw_aff_copy(list
->p
[0]);
3698 for (i
= 1; i
< list
->n
; ++i
)
3699 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3701 isl_pw_aff_list_free(list
);
3704 isl_pw_aff_list_free(list
);
3708 /* Return an isl_pw_aff that maps each element in the intersection of the
3709 * domains of the elements of list to the minimal corresponding affine
3712 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3714 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3717 /* Return an isl_pw_aff that maps each element in the intersection of the
3718 * domains of the elements of list to the maximal corresponding affine
3721 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3723 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3726 /* Mark the domains of "pwaff" as rational.
3728 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3732 pwaff
= isl_pw_aff_cow(pwaff
);
3738 for (i
= 0; i
< pwaff
->n
; ++i
) {
3739 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3740 if (!pwaff
->p
[i
].set
)
3741 return isl_pw_aff_free(pwaff
);
3747 /* Mark the domains of the elements of "list" as rational.
3749 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3750 __isl_take isl_pw_aff_list
*list
)
3760 for (i
= 0; i
< n
; ++i
) {
3763 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3764 pa
= isl_pw_aff_set_rational(pa
);
3765 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3771 /* Do the parameters of "aff" match those of "space"?
3773 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3774 __isl_keep isl_space
*space
)
3776 isl_space
*aff_space
;
3780 return isl_bool_error
;
3782 aff_space
= isl_aff_get_domain_space(aff
);
3784 match
= isl_space_has_equal_params(space
, aff_space
);
3786 isl_space_free(aff_space
);
3790 /* Check that the domain space of "aff" matches "space".
3792 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3793 __isl_keep isl_space
*space
)
3795 isl_space
*aff_space
;
3799 return isl_stat_error
;
3801 aff_space
= isl_aff_get_domain_space(aff
);
3803 match
= isl_space_has_equal_params(space
, aff_space
);
3807 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3808 "parameters don't match", goto error
);
3809 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3810 aff_space
, isl_dim_set
);
3814 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3815 "domains don't match", goto error
);
3816 isl_space_free(aff_space
);
3819 isl_space_free(aff_space
);
3820 return isl_stat_error
;
3829 #include <isl_multi_templ.c>
3830 #include <isl_multi_apply_set.c>
3831 #include <isl_multi_cmp.c>
3832 #include <isl_multi_floor.c>
3833 #include <isl_multi_gist.c>
3837 /* Construct an isl_multi_aff living in "space" that corresponds
3838 * to the affine transformation matrix "mat".
3840 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3841 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3844 isl_local_space
*ls
= NULL
;
3845 isl_multi_aff
*ma
= NULL
;
3846 int n_row
, n_col
, n_out
, total
;
3852 ctx
= isl_mat_get_ctx(mat
);
3854 n_row
= isl_mat_rows(mat
);
3855 n_col
= isl_mat_cols(mat
);
3857 isl_die(ctx
, isl_error_invalid
,
3858 "insufficient number of rows", goto error
);
3860 isl_die(ctx
, isl_error_invalid
,
3861 "insufficient number of columns", goto error
);
3862 n_out
= isl_space_dim(space
, isl_dim_out
);
3863 total
= isl_space_dim(space
, isl_dim_all
);
3864 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3865 isl_die(ctx
, isl_error_invalid
,
3866 "dimension mismatch", goto error
);
3868 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3869 ls
= isl_local_space_from_space(isl_space_domain(space
));
3871 for (i
= 0; i
< n_row
- 1; ++i
) {
3875 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3878 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3879 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3880 v
= isl_vec_normalize(v
);
3881 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3882 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3885 isl_local_space_free(ls
);
3889 isl_local_space_free(ls
);
3891 isl_multi_aff_free(ma
);
3895 /* Remove any internal structure of the domain of "ma".
3896 * If there is any such internal structure in the input,
3897 * then the name of the corresponding space is also removed.
3899 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3900 __isl_take isl_multi_aff
*ma
)
3907 if (!ma
->space
->nested
[0])
3910 space
= isl_multi_aff_get_space(ma
);
3911 space
= isl_space_flatten_domain(space
);
3912 ma
= isl_multi_aff_reset_space(ma
, space
);
3917 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3918 * of the space to its domain.
3920 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3923 isl_local_space
*ls
;
3928 if (!isl_space_is_map(space
))
3929 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3930 "not a map space", goto error
);
3932 n_in
= isl_space_dim(space
, isl_dim_in
);
3933 space
= isl_space_domain_map(space
);
3935 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3937 isl_space_free(space
);
3941 space
= isl_space_domain(space
);
3942 ls
= isl_local_space_from_space(space
);
3943 for (i
= 0; i
< n_in
; ++i
) {
3946 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3948 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3950 isl_local_space_free(ls
);
3953 isl_space_free(space
);
3957 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3958 * of the space to its range.
3960 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3963 isl_local_space
*ls
;
3968 if (!isl_space_is_map(space
))
3969 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3970 "not a map space", goto error
);
3972 n_in
= isl_space_dim(space
, isl_dim_in
);
3973 n_out
= isl_space_dim(space
, isl_dim_out
);
3974 space
= isl_space_range_map(space
);
3976 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3978 isl_space_free(space
);
3982 space
= isl_space_domain(space
);
3983 ls
= isl_local_space_from_space(space
);
3984 for (i
= 0; i
< n_out
; ++i
) {
3987 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3988 isl_dim_set
, n_in
+ i
);
3989 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3991 isl_local_space_free(ls
);
3994 isl_space_free(space
);
3998 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3999 * of the space to its range.
4001 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4002 __isl_take isl_space
*space
)
4004 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4007 /* Given the space of a set and a range of set dimensions,
4008 * construct an isl_multi_aff that projects out those dimensions.
4010 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4011 __isl_take isl_space
*space
, enum isl_dim_type type
,
4012 unsigned first
, unsigned n
)
4015 isl_local_space
*ls
;
4020 if (!isl_space_is_set(space
))
4021 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4022 "expecting set space", goto error
);
4023 if (type
!= isl_dim_set
)
4024 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4025 "only set dimensions can be projected out", goto error
);
4027 dim
= isl_space_dim(space
, isl_dim_set
);
4028 if (first
+ n
> dim
)
4029 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4030 "range out of bounds", goto error
);
4032 space
= isl_space_from_domain(space
);
4033 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4036 return isl_multi_aff_alloc(space
);
4038 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4039 space
= isl_space_domain(space
);
4040 ls
= isl_local_space_from_space(space
);
4042 for (i
= 0; i
< first
; ++i
) {
4045 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4047 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4050 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4053 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4054 isl_dim_set
, first
+ n
+ i
);
4055 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4058 isl_local_space_free(ls
);
4061 isl_space_free(space
);
4065 /* Given the space of a set and a range of set dimensions,
4066 * construct an isl_pw_multi_aff that projects out those dimensions.
4068 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4069 __isl_take isl_space
*space
, enum isl_dim_type type
,
4070 unsigned first
, unsigned n
)
4074 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4075 return isl_pw_multi_aff_from_multi_aff(ma
);
4078 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4081 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4082 __isl_take isl_multi_aff
*ma
)
4084 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4085 return isl_pw_multi_aff_alloc(dom
, ma
);
4088 /* Create a piecewise multi-affine expression in the given space that maps each
4089 * input dimension to the corresponding output dimension.
4091 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4092 __isl_take isl_space
*space
)
4094 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4097 /* Exploit the equalities in "eq" to simplify the affine expressions.
4099 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4100 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4104 maff
= isl_multi_aff_cow(maff
);
4108 for (i
= 0; i
< maff
->n
; ++i
) {
4109 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
4110 isl_basic_set_copy(eq
));
4115 isl_basic_set_free(eq
);
4118 isl_basic_set_free(eq
);
4119 isl_multi_aff_free(maff
);
4123 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4128 maff
= isl_multi_aff_cow(maff
);
4132 for (i
= 0; i
< maff
->n
; ++i
) {
4133 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4135 return isl_multi_aff_free(maff
);
4141 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4142 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4144 maff1
= isl_multi_aff_add(maff1
, maff2
);
4145 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4149 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4157 /* Return the set of domain elements where "ma1" is lexicographically
4158 * smaller than or equal to "ma2".
4160 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4161 __isl_take isl_multi_aff
*ma2
)
4163 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4166 /* Return the set of domain elements where "ma1" is lexicographically
4167 * smaller than "ma2".
4169 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4170 __isl_take isl_multi_aff
*ma2
)
4172 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4175 /* Return the set of domain elements where "ma1" and "ma2"
4178 static __isl_give isl_set
*isl_multi_aff_order_set(
4179 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4180 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4183 isl_map
*map1
, *map2
;
4186 map1
= isl_map_from_multi_aff(ma1
);
4187 map2
= isl_map_from_multi_aff(ma2
);
4188 map
= isl_map_range_product(map1
, map2
);
4189 space
= isl_space_range(isl_map_get_space(map
));
4190 space
= isl_space_domain(isl_space_unwrap(space
));
4192 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4194 return isl_map_domain(map
);
4197 /* Return the set of domain elements where "ma1" is lexicographically
4198 * greater than or equal to "ma2".
4200 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4201 __isl_take isl_multi_aff
*ma2
)
4203 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4206 /* Return the set of domain elements where "ma1" is lexicographically
4207 * greater than "ma2".
4209 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4210 __isl_take isl_multi_aff
*ma2
)
4212 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4216 #define PW isl_pw_multi_aff
4218 #define EL isl_multi_aff
4220 #define EL_IS_ZERO is_empty
4224 #define IS_ZERO is_empty
4227 #undef DEFAULT_IS_ZERO
4228 #define DEFAULT_IS_ZERO 0
4233 #define NO_INVOLVES_DIMS
4234 #define NO_INSERT_DIMS
4238 #include <isl_pw_templ.c>
4239 #include <isl_pw_union_opt.c>
4244 #define UNION isl_union_pw_multi_aff
4246 #define PART isl_pw_multi_aff
4248 #define PARTS pw_multi_aff
4250 #include <isl_union_multi.c>
4251 #include <isl_union_neg.c>
4253 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4254 __isl_take isl_pw_multi_aff
*pma1
,
4255 __isl_take isl_pw_multi_aff
*pma2
)
4257 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4258 &isl_multi_aff_lex_ge_set
);
4261 /* Given two piecewise multi affine expressions, return a piecewise
4262 * multi-affine expression defined on the union of the definition domains
4263 * of the inputs that is equal to the lexicographic maximum of the two
4264 * inputs on each cell. If only one of the two inputs is defined on
4265 * a given cell, then it is considered to be the maximum.
4267 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4268 __isl_take isl_pw_multi_aff
*pma1
,
4269 __isl_take isl_pw_multi_aff
*pma2
)
4271 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4272 &pw_multi_aff_union_lexmax
);
4275 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4276 __isl_take isl_pw_multi_aff
*pma1
,
4277 __isl_take isl_pw_multi_aff
*pma2
)
4279 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4280 &isl_multi_aff_lex_le_set
);
4283 /* Given two piecewise multi affine expressions, return a piecewise
4284 * multi-affine expression defined on the union of the definition domains
4285 * of the inputs that is equal to the lexicographic minimum of the two
4286 * inputs on each cell. If only one of the two inputs is defined on
4287 * a given cell, then it is considered to be the minimum.
4289 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4290 __isl_take isl_pw_multi_aff
*pma1
,
4291 __isl_take isl_pw_multi_aff
*pma2
)
4293 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4294 &pw_multi_aff_union_lexmin
);
4297 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4298 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4300 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4301 &isl_multi_aff_add
);
4304 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4305 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4307 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4311 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4312 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4314 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4315 &isl_multi_aff_sub
);
4318 /* Subtract "pma2" from "pma1" and return the result.
4320 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4321 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4323 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4327 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4328 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4330 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4333 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4334 * with the actual sum on the shared domain and
4335 * the defined expression on the symmetric difference of the domains.
4337 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4338 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4340 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4343 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4344 * with the actual sum on the shared domain and
4345 * the defined expression on the symmetric difference of the domains.
4347 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4348 __isl_take isl_union_pw_multi_aff
*upma1
,
4349 __isl_take isl_union_pw_multi_aff
*upma2
)
4351 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4354 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4355 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4357 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4358 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4362 isl_pw_multi_aff
*res
;
4367 n
= pma1
->n
* pma2
->n
;
4368 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4369 isl_space_copy(pma2
->dim
));
4370 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4372 for (i
= 0; i
< pma1
->n
; ++i
) {
4373 for (j
= 0; j
< pma2
->n
; ++j
) {
4377 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4378 isl_set_copy(pma2
->p
[j
].set
));
4379 ma
= isl_multi_aff_product(
4380 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4381 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4382 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4386 isl_pw_multi_aff_free(pma1
);
4387 isl_pw_multi_aff_free(pma2
);
4390 isl_pw_multi_aff_free(pma1
);
4391 isl_pw_multi_aff_free(pma2
);
4395 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4396 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4398 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4399 &pw_multi_aff_product
);
4402 /* Construct a map mapping the domain of the piecewise multi-affine expression
4403 * to its range, with each dimension in the range equated to the
4404 * corresponding affine expression on its cell.
4406 * If the domain of "pma" is rational, then so is the constructed "map".
4408 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4416 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4418 for (i
= 0; i
< pma
->n
; ++i
) {
4420 isl_multi_aff
*maff
;
4421 isl_basic_map
*bmap
;
4424 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4426 map
= isl_map_free(map
);
4427 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4428 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4429 map_i
= isl_map_from_basic_map(bmap
);
4430 map_i
= isl_map_intersect_domain(map_i
,
4431 isl_set_copy(pma
->p
[i
].set
));
4432 map
= isl_map_union_disjoint(map
, map_i
);
4435 isl_pw_multi_aff_free(pma
);
4439 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4444 if (!isl_space_is_set(pma
->dim
))
4445 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4446 "isl_pw_multi_aff cannot be converted into an isl_set",
4449 return isl_map_from_pw_multi_aff(pma
);
4451 isl_pw_multi_aff_free(pma
);
4455 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4456 * denominator "denom".
4457 * "denom" is allowed to be negative, in which case the actual denominator
4458 * is -denom and the expressions are added instead.
4460 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4461 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4467 first
= isl_seq_first_non_zero(c
, n
);
4471 sign
= isl_int_sgn(denom
);
4473 isl_int_abs(d
, denom
);
4474 for (i
= first
; i
< n
; ++i
) {
4477 if (isl_int_is_zero(c
[i
]))
4479 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4480 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4481 aff_i
= isl_aff_scale_down(aff_i
, d
);
4483 aff
= isl_aff_sub(aff
, aff_i
);
4485 aff
= isl_aff_add(aff
, aff_i
);
4492 /* Extract an affine expression that expresses the output dimension "pos"
4493 * of "bmap" in terms of the parameters and input dimensions from
4495 * Note that this expression may involve integer divisions defined
4496 * in terms of parameters and input dimensions.
4497 * The equality may also involve references to earlier (but not later)
4498 * output dimensions. These are replaced by the corresponding elements
4501 * If the equality is of the form
4503 * f(i) + h(j) + a x + g(i) = 0,
4505 * with f(i) a linear combinations of the parameters and input dimensions,
4506 * g(i) a linear combination of integer divisions defined in terms of the same
4507 * and h(j) a linear combinations of earlier output dimensions,
4508 * then the affine expression is
4510 * (-f(i) - g(i))/a - h(j)/a
4512 * If the equality is of the form
4514 * f(i) + h(j) - a x + g(i) = 0,
4516 * then the affine expression is
4518 * (f(i) + g(i))/a - h(j)/(-a)
4521 * If "div" refers to an integer division (i.e., it is smaller than
4522 * the number of integer divisions), then the equality constraint
4523 * does involve an integer division (the one at position "div") that
4524 * is defined in terms of output dimensions. However, this integer
4525 * division can be eliminated by exploiting a pair of constraints
4526 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4527 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4529 * In particular, let
4531 * x = e(i) + m floor(...)
4533 * with e(i) the expression derived above and floor(...) the integer
4534 * division involving output dimensions.
4545 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4546 * = (e(i) - l) mod m
4550 * x - l = (e(i) - l) mod m
4554 * x = ((e(i) - l) mod m) + l
4556 * The variable "shift" below contains the expression -l, which may
4557 * also involve a linear combination of earlier output dimensions.
4559 static __isl_give isl_aff
*extract_aff_from_equality(
4560 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4561 __isl_keep isl_multi_aff
*ma
)
4564 unsigned n_div
, n_out
;
4566 isl_local_space
*ls
;
4567 isl_aff
*aff
, *shift
;
4570 ctx
= isl_basic_map_get_ctx(bmap
);
4571 ls
= isl_basic_map_get_local_space(bmap
);
4572 ls
= isl_local_space_domain(ls
);
4573 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4576 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4577 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4578 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4579 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4580 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4581 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4582 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4584 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4585 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4586 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4589 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4590 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4591 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4592 bmap
->eq
[eq
][o_out
+ pos
]);
4594 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4597 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4598 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4599 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4600 isl_int_set_si(shift
->v
->el
[0], 1);
4601 shift
= subtract_initial(shift
, ma
, pos
,
4602 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4603 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4604 mod
= isl_val_int_from_isl_int(ctx
,
4605 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4606 mod
= isl_val_abs(mod
);
4607 aff
= isl_aff_mod_val(aff
, mod
);
4608 aff
= isl_aff_sub(aff
, shift
);
4611 isl_local_space_free(ls
);
4614 isl_local_space_free(ls
);
4619 /* Given a basic map with output dimensions defined
4620 * in terms of the parameters input dimensions and earlier
4621 * output dimensions using an equality (and possibly a pair on inequalities),
4622 * extract an isl_aff that expresses output dimension "pos" in terms
4623 * of the parameters and input dimensions.
4624 * Note that this expression may involve integer divisions defined
4625 * in terms of parameters and input dimensions.
4626 * "ma" contains the expressions corresponding to earlier output dimensions.
4628 * This function shares some similarities with
4629 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4631 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4632 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4639 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4640 if (eq
>= bmap
->n_eq
)
4641 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4642 "unable to find suitable equality", return NULL
);
4643 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4645 aff
= isl_aff_remove_unused_divs(aff
);
4649 /* Given a basic map where each output dimension is defined
4650 * in terms of the parameters and input dimensions using an equality,
4651 * extract an isl_multi_aff that expresses the output dimensions in terms
4652 * of the parameters and input dimensions.
4654 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4655 __isl_take isl_basic_map
*bmap
)
4664 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4665 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4667 for (i
= 0; i
< n_out
; ++i
) {
4670 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4671 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4674 isl_basic_map_free(bmap
);
4679 /* Given a basic set where each set dimension is defined
4680 * in terms of the parameters using an equality,
4681 * extract an isl_multi_aff that expresses the set dimensions in terms
4682 * of the parameters.
4684 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4685 __isl_take isl_basic_set
*bset
)
4687 return extract_isl_multi_aff_from_basic_map(bset
);
4690 /* Create an isl_pw_multi_aff that is equivalent to
4691 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4692 * The given basic map is such that each output dimension is defined
4693 * in terms of the parameters and input dimensions using an equality.
4695 * Since some applications expect the result of isl_pw_multi_aff_from_map
4696 * to only contain integer affine expressions, we compute the floor
4697 * of the expression before returning.
4699 * Remove all constraints involving local variables without
4700 * an explicit representation (resulting in the removal of those
4701 * local variables) prior to the actual extraction to ensure
4702 * that the local spaces in which the resulting affine expressions
4703 * are created do not contain any unknown local variables.
4704 * Removing such constraints is safe because constraints involving
4705 * unknown local variables are not used to determine whether
4706 * a basic map is obviously single-valued.
4708 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4709 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4713 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4714 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4715 ma
= isl_multi_aff_floor(ma
);
4716 return isl_pw_multi_aff_alloc(domain
, ma
);
4719 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4720 * This obviously only works if the input "map" is single-valued.
4721 * If so, we compute the lexicographic minimum of the image in the form
4722 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4723 * to its lexicographic minimum.
4724 * If the input is not single-valued, we produce an error.
4726 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4727 __isl_take isl_map
*map
)
4731 isl_pw_multi_aff
*pma
;
4733 sv
= isl_map_is_single_valued(map
);
4737 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4738 "map is not single-valued", goto error
);
4739 map
= isl_map_make_disjoint(map
);
4743 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4745 for (i
= 0; i
< map
->n
; ++i
) {
4746 isl_pw_multi_aff
*pma_i
;
4747 isl_basic_map
*bmap
;
4748 bmap
= isl_basic_map_copy(map
->p
[i
]);
4749 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4750 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4760 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4761 * taking into account that the output dimension at position "d"
4762 * can be represented as
4764 * x = floor((e(...) + c1) / m)
4766 * given that constraint "i" is of the form
4768 * e(...) + c1 - m x >= 0
4771 * Let "map" be of the form
4775 * We construct a mapping
4777 * A -> [A -> x = floor(...)]
4779 * apply that to the map, obtaining
4781 * [A -> x = floor(...)] -> B
4783 * and equate dimension "d" to x.
4784 * We then compute a isl_pw_multi_aff representation of the resulting map
4785 * and plug in the mapping above.
4787 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4788 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4792 isl_local_space
*ls
;
4800 isl_pw_multi_aff
*pma
;
4803 is_set
= isl_map_is_set(map
);
4807 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4808 ctx
= isl_map_get_ctx(map
);
4809 space
= isl_space_domain(isl_map_get_space(map
));
4810 n_in
= isl_space_dim(space
, isl_dim_set
);
4811 n
= isl_space_dim(space
, isl_dim_all
);
4813 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4815 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4816 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4818 isl_basic_map_free(hull
);
4820 ls
= isl_local_space_from_space(isl_space_copy(space
));
4821 aff
= isl_aff_alloc_vec(ls
, v
);
4822 aff
= isl_aff_floor(aff
);
4824 isl_space_free(space
);
4825 ma
= isl_multi_aff_from_aff(aff
);
4827 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4828 ma
= isl_multi_aff_range_product(ma
,
4829 isl_multi_aff_from_aff(aff
));
4832 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4833 map
= isl_map_apply_domain(map
, insert
);
4834 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4835 pma
= isl_pw_multi_aff_from_map(map
);
4836 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4841 isl_basic_map_free(hull
);
4845 /* Is constraint "c" of the form
4847 * e(...) + c1 - m x >= 0
4851 * -e(...) + c2 + m x >= 0
4853 * where m > 1 and e only depends on parameters and input dimemnsions?
4855 * "offset" is the offset of the output dimensions
4856 * "pos" is the position of output dimension x.
4858 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4860 if (isl_int_is_zero(c
[offset
+ d
]))
4862 if (isl_int_is_one(c
[offset
+ d
]))
4864 if (isl_int_is_negone(c
[offset
+ d
]))
4866 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4868 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4869 total
- (offset
+ d
+ 1)) != -1)
4874 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4876 * As a special case, we first check if there is any pair of constraints,
4877 * shared by all the basic maps in "map" that force a given dimension
4878 * to be equal to the floor of some affine combination of the input dimensions.
4880 * In particular, if we can find two constraints
4882 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4886 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4888 * where m > 1 and e only depends on parameters and input dimemnsions,
4891 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4893 * then we know that we can take
4895 * x = floor((e(...) + c1) / m)
4897 * without having to perform any computation.
4899 * Note that we know that
4903 * If c1 + c2 were 0, then we would have detected an equality during
4904 * simplification. If c1 + c2 were negative, then we would have detected
4907 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4908 __isl_take isl_map
*map
)
4914 isl_basic_map
*hull
;
4916 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4921 dim
= isl_map_dim(map
, isl_dim_out
);
4922 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4923 total
= 1 + isl_basic_map_total_dim(hull
);
4925 for (d
= 0; d
< dim
; ++d
) {
4926 for (i
= 0; i
< n
; ++i
) {
4927 if (!is_potential_div_constraint(hull
->ineq
[i
],
4930 for (j
= i
+ 1; j
< n
; ++j
) {
4931 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4932 hull
->ineq
[j
] + 1, total
- 1))
4934 isl_int_add(sum
, hull
->ineq
[i
][0],
4936 if (isl_int_abs_lt(sum
,
4937 hull
->ineq
[i
][offset
+ d
]))
4944 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4946 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4950 isl_basic_map_free(hull
);
4951 return pw_multi_aff_from_map_base(map
);
4954 isl_basic_map_free(hull
);
4958 /* Given an affine expression
4960 * [A -> B] -> f(A,B)
4962 * construct an isl_multi_aff
4966 * such that dimension "d" in B' is set to "aff" and the remaining
4967 * dimensions are set equal to the corresponding dimensions in B.
4968 * "n_in" is the dimension of the space A.
4969 * "n_out" is the dimension of the space B.
4971 * If "is_set" is set, then the affine expression is of the form
4975 * and we construct an isl_multi_aff
4979 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4980 unsigned n_in
, unsigned n_out
, int is_set
)
4984 isl_space
*space
, *space2
;
4985 isl_local_space
*ls
;
4987 space
= isl_aff_get_domain_space(aff
);
4988 ls
= isl_local_space_from_space(isl_space_copy(space
));
4989 space2
= isl_space_copy(space
);
4991 space2
= isl_space_range(isl_space_unwrap(space2
));
4992 space
= isl_space_map_from_domain_and_range(space
, space2
);
4993 ma
= isl_multi_aff_alloc(space
);
4994 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4996 for (i
= 0; i
< n_out
; ++i
) {
4999 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5000 isl_dim_set
, n_in
+ i
);
5001 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5004 isl_local_space_free(ls
);
5009 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5010 * taking into account that the dimension at position "d" can be written as
5012 * x = m a + f(..) (1)
5014 * where m is equal to "gcd".
5015 * "i" is the index of the equality in "hull" that defines f(..).
5016 * In particular, the equality is of the form
5018 * f(..) - x + m g(existentials) = 0
5022 * -f(..) + x + m g(existentials) = 0
5024 * We basically plug (1) into "map", resulting in a map with "a"
5025 * in the range instead of "x". The corresponding isl_pw_multi_aff
5026 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5028 * Specifically, given the input map
5032 * We first wrap it into a set
5036 * and define (1) on top of the corresponding space, resulting in "aff".
5037 * We use this to create an isl_multi_aff that maps the output position "d"
5038 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5039 * We plug this into the wrapped map, unwrap the result and compute the
5040 * corresponding isl_pw_multi_aff.
5041 * The result is an expression
5049 * so that we can plug that into "aff", after extending the latter to
5055 * If "map" is actually a set, then there is no "A" space, meaning
5056 * that we do not need to perform any wrapping, and that the result
5057 * of the recursive call is of the form
5061 * which is plugged into a mapping of the form
5065 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5066 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5071 isl_local_space
*ls
;
5074 isl_pw_multi_aff
*pma
, *id
;
5080 is_set
= isl_map_is_set(map
);
5084 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5085 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5086 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5091 set
= isl_map_wrap(map
);
5092 space
= isl_space_map_from_set(isl_set_get_space(set
));
5093 ma
= isl_multi_aff_identity(space
);
5094 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5095 aff
= isl_aff_alloc(ls
);
5097 isl_int_set_si(aff
->v
->el
[0], 1);
5098 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5099 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5102 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5104 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5106 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5107 set
= isl_set_preimage_multi_aff(set
, ma
);
5109 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5114 map
= isl_set_unwrap(set
);
5115 pma
= isl_pw_multi_aff_from_map(map
);
5118 space
= isl_pw_multi_aff_get_domain_space(pma
);
5119 space
= isl_space_map_from_set(space
);
5120 id
= isl_pw_multi_aff_identity(space
);
5121 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5123 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5124 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5126 isl_basic_map_free(hull
);
5130 isl_basic_map_free(hull
);
5134 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5135 * "hull" contains the equalities valid for "map".
5137 * Check if any of the output dimensions is "strided".
5138 * That is, we check if it can be written as
5142 * with m greater than 1, a some combination of existentially quantified
5143 * variables and f an expression in the parameters and input dimensions.
5144 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5146 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5149 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5150 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5159 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5160 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5163 isl_basic_map_free(hull
);
5164 return pw_multi_aff_from_map_check_div(map
);
5169 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5170 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5172 for (i
= 0; i
< n_out
; ++i
) {
5173 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5174 isl_int
*eq
= hull
->eq
[j
];
5175 isl_pw_multi_aff
*res
;
5177 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5178 !isl_int_is_negone(eq
[o_out
+ i
]))
5180 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5182 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5183 n_out
- (i
+ 1)) != -1)
5185 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5186 if (isl_int_is_zero(gcd
))
5188 if (isl_int_is_one(gcd
))
5191 res
= pw_multi_aff_from_map_stride(map
, hull
,
5199 isl_basic_map_free(hull
);
5200 return pw_multi_aff_from_map_check_div(map
);
5203 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5205 * As a special case, we first check if all output dimensions are uniquely
5206 * defined in terms of the parameters and input dimensions over the entire
5207 * domain. If so, we extract the desired isl_pw_multi_aff directly
5208 * from the affine hull of "map" and its domain.
5210 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5213 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5216 isl_basic_map
*hull
;
5221 if (isl_map_n_basic_map(map
) == 1) {
5222 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5223 hull
= isl_basic_map_plain_affine_hull(hull
);
5224 sv
= isl_basic_map_plain_is_single_valued(hull
);
5226 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5228 isl_basic_map_free(hull
);
5230 map
= isl_map_detect_equalities(map
);
5231 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5232 sv
= isl_basic_map_plain_is_single_valued(hull
);
5234 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5236 return pw_multi_aff_from_map_check_strides(map
, hull
);
5237 isl_basic_map_free(hull
);
5242 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5244 return isl_pw_multi_aff_from_map(set
);
5247 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5250 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5252 isl_union_pw_multi_aff
**upma
= user
;
5253 isl_pw_multi_aff
*pma
;
5255 pma
= isl_pw_multi_aff_from_map(map
);
5256 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5258 return *upma
? isl_stat_ok
: isl_stat_error
;
5261 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5264 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5265 __isl_take isl_aff
*aff
)
5268 isl_pw_multi_aff
*pma
;
5270 ma
= isl_multi_aff_from_aff(aff
);
5271 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5272 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5275 /* Try and create an isl_union_pw_multi_aff that is equivalent
5276 * to the given isl_union_map.
5277 * The isl_union_map is required to be single-valued in each space.
5278 * Otherwise, an error is produced.
5280 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5281 __isl_take isl_union_map
*umap
)
5284 isl_union_pw_multi_aff
*upma
;
5286 space
= isl_union_map_get_space(umap
);
5287 upma
= isl_union_pw_multi_aff_empty(space
);
5288 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5289 upma
= isl_union_pw_multi_aff_free(upma
);
5290 isl_union_map_free(umap
);
5295 /* Try and create an isl_union_pw_multi_aff that is equivalent
5296 * to the given isl_union_set.
5297 * The isl_union_set is required to be a singleton in each space.
5298 * Otherwise, an error is produced.
5300 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5301 __isl_take isl_union_set
*uset
)
5303 return isl_union_pw_multi_aff_from_union_map(uset
);
5306 /* Return the piecewise affine expression "set ? 1 : 0".
5308 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5311 isl_space
*space
= isl_set_get_space(set
);
5312 isl_local_space
*ls
= isl_local_space_from_space(space
);
5313 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5314 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5316 one
= isl_aff_add_constant_si(one
, 1);
5317 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5318 set
= isl_set_complement(set
);
5319 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5324 /* Plug in "subs" for dimension "type", "pos" of "aff".
5326 * Let i be the dimension to replace and let "subs" be of the form
5330 * and "aff" of the form
5336 * (a f + d g')/(m d)
5338 * where g' is the result of plugging in "subs" in each of the integer
5341 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5342 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5347 aff
= isl_aff_cow(aff
);
5349 return isl_aff_free(aff
);
5351 ctx
= isl_aff_get_ctx(aff
);
5352 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5353 isl_die(ctx
, isl_error_invalid
,
5354 "spaces don't match", return isl_aff_free(aff
));
5355 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5356 isl_die(ctx
, isl_error_unsupported
,
5357 "cannot handle divs yet", return isl_aff_free(aff
));
5359 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5361 return isl_aff_free(aff
);
5363 aff
->v
= isl_vec_cow(aff
->v
);
5365 return isl_aff_free(aff
);
5367 pos
+= isl_local_space_offset(aff
->ls
, type
);
5370 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5371 aff
->v
->size
, subs
->v
->size
, v
);
5377 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5378 * expressions in "maff".
5380 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5381 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5382 __isl_keep isl_aff
*subs
)
5386 maff
= isl_multi_aff_cow(maff
);
5388 return isl_multi_aff_free(maff
);
5390 if (type
== isl_dim_in
)
5393 for (i
= 0; i
< maff
->n
; ++i
) {
5394 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5396 return isl_multi_aff_free(maff
);
5402 /* Plug in "subs" for dimension "type", "pos" of "pma".
5404 * pma is of the form
5408 * while subs is of the form
5410 * v' = B_j(v) -> S_j
5412 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5413 * has a contribution in the result, in particular
5415 * C_ij(S_j) -> M_i(S_j)
5417 * Note that plugging in S_j in C_ij may also result in an empty set
5418 * and this contribution should simply be discarded.
5420 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5421 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5422 __isl_keep isl_pw_aff
*subs
)
5425 isl_pw_multi_aff
*res
;
5428 return isl_pw_multi_aff_free(pma
);
5430 n
= pma
->n
* subs
->n
;
5431 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5433 for (i
= 0; i
< pma
->n
; ++i
) {
5434 for (j
= 0; j
< subs
->n
; ++j
) {
5436 isl_multi_aff
*res_ij
;
5439 common
= isl_set_intersect(
5440 isl_set_copy(pma
->p
[i
].set
),
5441 isl_set_copy(subs
->p
[j
].set
));
5442 common
= isl_set_substitute(common
,
5443 type
, pos
, subs
->p
[j
].aff
);
5444 empty
= isl_set_plain_is_empty(common
);
5445 if (empty
< 0 || empty
) {
5446 isl_set_free(common
);
5452 res_ij
= isl_multi_aff_substitute(
5453 isl_multi_aff_copy(pma
->p
[i
].maff
),
5454 type
, pos
, subs
->p
[j
].aff
);
5456 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5460 isl_pw_multi_aff_free(pma
);
5463 isl_pw_multi_aff_free(pma
);
5464 isl_pw_multi_aff_free(res
);
5468 /* Compute the preimage of a range of dimensions in the affine expression "src"
5469 * under "ma" and put the result in "dst". The number of dimensions in "src"
5470 * that precede the range is given by "n_before". The number of dimensions
5471 * in the range is given by the number of output dimensions of "ma".
5472 * The number of dimensions that follow the range is given by "n_after".
5473 * If "has_denom" is set (to one),
5474 * then "src" and "dst" have an extra initial denominator.
5475 * "n_div_ma" is the number of existentials in "ma"
5476 * "n_div_bset" is the number of existentials in "src"
5477 * The resulting "dst" (which is assumed to have been allocated by
5478 * the caller) contains coefficients for both sets of existentials,
5479 * first those in "ma" and then those in "src".
5480 * f, c1, c2 and g are temporary objects that have been initialized
5483 * Let src represent the expression
5485 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5487 * and let ma represent the expressions
5489 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5491 * We start out with the following expression for dst:
5493 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5495 * with the multiplication factor f initially equal to 1
5496 * and f \sum_i b_i v_i kept separately.
5497 * For each x_i that we substitute, we multiply the numerator
5498 * (and denominator) of dst by c_1 = m_i and add the numerator
5499 * of the x_i expression multiplied by c_2 = f b_i,
5500 * after removing the common factors of c_1 and c_2.
5501 * The multiplication factor f also needs to be multiplied by c_1
5502 * for the next x_j, j > i.
5504 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5505 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5506 int n_div_ma
, int n_div_bmap
,
5507 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5510 int n_param
, n_in
, n_out
;
5513 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5514 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5515 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5517 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5518 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5519 isl_seq_clr(dst
+ o_dst
, n_in
);
5522 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5525 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5527 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5529 isl_int_set_si(f
, 1);
5531 for (i
= 0; i
< n_out
; ++i
) {
5532 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5534 if (isl_int_is_zero(src
[offset
]))
5536 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5537 isl_int_mul(c2
, f
, src
[offset
]);
5538 isl_int_gcd(g
, c1
, c2
);
5539 isl_int_divexact(c1
, c1
, g
);
5540 isl_int_divexact(c2
, c2
, g
);
5542 isl_int_mul(f
, f
, c1
);
5545 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5546 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5547 o_dst
+= 1 + n_param
;
5548 o_src
+= 1 + n_param
;
5549 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5551 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5552 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5555 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5557 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5558 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5561 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5563 isl_int_mul(dst
[0], dst
[0], c1
);
5567 /* Compute the pullback of "aff" by the function represented by "ma".
5568 * In other words, plug in "ma" in "aff". The result is an affine expression
5569 * defined over the domain space of "ma".
5571 * If "aff" is represented by
5573 * (a(p) + b x + c(divs))/d
5575 * and ma is represented by
5577 * x = D(p) + F(y) + G(divs')
5579 * then the result is
5581 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5583 * The divs in the local space of the input are similarly adjusted
5584 * through a call to isl_local_space_preimage_multi_aff.
5586 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5587 __isl_take isl_multi_aff
*ma
)
5589 isl_aff
*res
= NULL
;
5590 isl_local_space
*ls
;
5591 int n_div_aff
, n_div_ma
;
5592 isl_int f
, c1
, c2
, g
;
5594 ma
= isl_multi_aff_align_divs(ma
);
5598 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5599 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5601 ls
= isl_aff_get_domain_local_space(aff
);
5602 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5603 res
= isl_aff_alloc(ls
);
5612 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5621 isl_multi_aff_free(ma
);
5622 res
= isl_aff_normalize(res
);
5626 isl_multi_aff_free(ma
);
5631 /* Compute the pullback of "aff1" by the function represented by "aff2".
5632 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5633 * defined over the domain space of "aff1".
5635 * The domain of "aff1" should match the range of "aff2", which means
5636 * that it should be single-dimensional.
5638 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5639 __isl_take isl_aff
*aff2
)
5643 ma
= isl_multi_aff_from_aff(aff2
);
5644 return isl_aff_pullback_multi_aff(aff1
, ma
);
5647 /* Compute the pullback of "ma1" by the function represented by "ma2".
5648 * In other words, plug in "ma2" in "ma1".
5650 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5652 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5653 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5656 isl_space
*space
= NULL
;
5658 ma2
= isl_multi_aff_align_divs(ma2
);
5659 ma1
= isl_multi_aff_cow(ma1
);
5663 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5664 isl_multi_aff_get_space(ma1
));
5666 for (i
= 0; i
< ma1
->n
; ++i
) {
5667 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5668 isl_multi_aff_copy(ma2
));
5673 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5674 isl_multi_aff_free(ma2
);
5677 isl_space_free(space
);
5678 isl_multi_aff_free(ma2
);
5679 isl_multi_aff_free(ma1
);
5683 /* Compute the pullback of "ma1" by the function represented by "ma2".
5684 * In other words, plug in "ma2" in "ma1".
5686 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5687 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5689 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5690 &isl_multi_aff_pullback_multi_aff_aligned
);
5693 /* Extend the local space of "dst" to include the divs
5694 * in the local space of "src".
5696 * If "src" does not have any divs or if the local spaces of "dst" and
5697 * "src" are the same, then no extension is required.
5699 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5700 __isl_keep isl_aff
*src
)
5703 int src_n_div
, dst_n_div
;
5710 return isl_aff_free(dst
);
5712 ctx
= isl_aff_get_ctx(src
);
5713 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5715 return isl_aff_free(dst
);
5717 isl_die(ctx
, isl_error_invalid
,
5718 "spaces don't match", goto error
);
5720 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5723 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5725 return isl_aff_free(dst
);
5729 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5730 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5731 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5732 if (!exp1
|| (dst_n_div
&& !exp2
))
5735 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5736 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5744 return isl_aff_free(dst
);
5747 /* Adjust the local spaces of the affine expressions in "maff"
5748 * such that they all have the save divs.
5750 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5751 __isl_take isl_multi_aff
*maff
)
5759 maff
= isl_multi_aff_cow(maff
);
5763 for (i
= 1; i
< maff
->n
; ++i
)
5764 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5765 for (i
= 1; i
< maff
->n
; ++i
) {
5766 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5768 return isl_multi_aff_free(maff
);
5774 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5776 aff
= isl_aff_cow(aff
);
5780 aff
->ls
= isl_local_space_lift(aff
->ls
);
5782 return isl_aff_free(aff
);
5787 /* Lift "maff" to a space with extra dimensions such that the result
5788 * has no more existentially quantified variables.
5789 * If "ls" is not NULL, then *ls is assigned the local space that lies
5790 * at the basis of the lifting applied to "maff".
5792 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5793 __isl_give isl_local_space
**ls
)
5807 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5808 *ls
= isl_local_space_from_space(space
);
5810 return isl_multi_aff_free(maff
);
5815 maff
= isl_multi_aff_cow(maff
);
5816 maff
= isl_multi_aff_align_divs(maff
);
5820 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5821 space
= isl_multi_aff_get_space(maff
);
5822 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5823 space
= isl_space_extend_domain_with_range(space
,
5824 isl_multi_aff_get_space(maff
));
5826 return isl_multi_aff_free(maff
);
5827 isl_space_free(maff
->space
);
5828 maff
->space
= space
;
5831 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5833 return isl_multi_aff_free(maff
);
5836 for (i
= 0; i
< maff
->n
; ++i
) {
5837 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5845 isl_local_space_free(*ls
);
5846 return isl_multi_aff_free(maff
);
5850 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5852 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5853 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5863 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5864 if (pos
< 0 || pos
>= n_out
)
5865 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5866 "index out of bounds", return NULL
);
5868 space
= isl_pw_multi_aff_get_space(pma
);
5869 space
= isl_space_drop_dims(space
, isl_dim_out
,
5870 pos
+ 1, n_out
- pos
- 1);
5871 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5873 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5874 for (i
= 0; i
< pma
->n
; ++i
) {
5876 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5877 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5883 /* Return an isl_pw_multi_aff with the given "set" as domain and
5884 * an unnamed zero-dimensional range.
5886 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5887 __isl_take isl_set
*set
)
5892 space
= isl_set_get_space(set
);
5893 space
= isl_space_from_domain(space
);
5894 ma
= isl_multi_aff_zero(space
);
5895 return isl_pw_multi_aff_alloc(set
, ma
);
5898 /* Add an isl_pw_multi_aff with the given "set" as domain and
5899 * an unnamed zero-dimensional range to *user.
5901 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5904 isl_union_pw_multi_aff
**upma
= user
;
5905 isl_pw_multi_aff
*pma
;
5907 pma
= isl_pw_multi_aff_from_domain(set
);
5908 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5913 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5914 * an unnamed zero-dimensional range.
5916 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5917 __isl_take isl_union_set
*uset
)
5920 isl_union_pw_multi_aff
*upma
;
5925 space
= isl_union_set_get_space(uset
);
5926 upma
= isl_union_pw_multi_aff_empty(space
);
5928 if (isl_union_set_foreach_set(uset
,
5929 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5932 isl_union_set_free(uset
);
5935 isl_union_set_free(uset
);
5936 isl_union_pw_multi_aff_free(upma
);
5940 /* Convert "pma" to an isl_map and add it to *umap.
5942 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5945 isl_union_map
**umap
= user
;
5948 map
= isl_map_from_pw_multi_aff(pma
);
5949 *umap
= isl_union_map_add_map(*umap
, map
);
5954 /* Construct a union map mapping the domain of the union
5955 * piecewise multi-affine expression to its range, with each dimension
5956 * in the range equated to the corresponding affine expression on its cell.
5958 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5959 __isl_take isl_union_pw_multi_aff
*upma
)
5962 isl_union_map
*umap
;
5967 space
= isl_union_pw_multi_aff_get_space(upma
);
5968 umap
= isl_union_map_empty(space
);
5970 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5971 &map_from_pw_multi_aff
, &umap
) < 0)
5974 isl_union_pw_multi_aff_free(upma
);
5977 isl_union_pw_multi_aff_free(upma
);
5978 isl_union_map_free(umap
);
5982 /* Local data for bin_entry and the callback "fn".
5984 struct isl_union_pw_multi_aff_bin_data
{
5985 isl_union_pw_multi_aff
*upma2
;
5986 isl_union_pw_multi_aff
*res
;
5987 isl_pw_multi_aff
*pma
;
5988 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5991 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5992 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5994 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5996 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6000 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6002 isl_pw_multi_aff_free(pma
);
6007 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6008 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6009 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6010 * as *entry. The callback should adjust data->res if desired.
6012 static __isl_give isl_union_pw_multi_aff
*bin_op(
6013 __isl_take isl_union_pw_multi_aff
*upma1
,
6014 __isl_take isl_union_pw_multi_aff
*upma2
,
6015 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6018 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6020 space
= isl_union_pw_multi_aff_get_space(upma2
);
6021 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6022 space
= isl_union_pw_multi_aff_get_space(upma1
);
6023 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6025 if (!upma1
|| !upma2
)
6029 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6030 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6031 &bin_entry
, &data
) < 0)
6034 isl_union_pw_multi_aff_free(upma1
);
6035 isl_union_pw_multi_aff_free(upma2
);
6038 isl_union_pw_multi_aff_free(upma1
);
6039 isl_union_pw_multi_aff_free(upma2
);
6040 isl_union_pw_multi_aff_free(data
.res
);
6044 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6045 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6047 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6048 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6052 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6053 isl_pw_multi_aff_get_space(pma2
));
6054 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6055 &isl_multi_aff_range_product
);
6058 /* Given two isl_pw_multi_affs A -> B and C -> D,
6059 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6061 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6062 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6064 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6065 &pw_multi_aff_range_product
);
6068 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6069 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6071 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6072 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6076 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6077 isl_pw_multi_aff_get_space(pma2
));
6078 space
= isl_space_flatten_range(space
);
6079 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6080 &isl_multi_aff_flat_range_product
);
6083 /* Given two isl_pw_multi_affs A -> B and C -> D,
6084 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6086 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6087 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6089 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6090 &pw_multi_aff_flat_range_product
);
6093 /* If data->pma and "pma2" have the same domain space, then compute
6094 * their flat range product and the result to data->res.
6096 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6099 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6101 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6102 pma2
->dim
, isl_dim_in
)) {
6103 isl_pw_multi_aff_free(pma2
);
6107 pma2
= isl_pw_multi_aff_flat_range_product(
6108 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6110 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6115 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6116 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6118 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6119 __isl_take isl_union_pw_multi_aff
*upma1
,
6120 __isl_take isl_union_pw_multi_aff
*upma2
)
6122 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6125 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6126 * The parameters are assumed to have been aligned.
6128 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6129 * except that it works on two different isl_pw_* types.
6131 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6132 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6133 __isl_take isl_pw_aff
*pa
)
6136 isl_pw_multi_aff
*res
= NULL
;
6141 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6142 pa
->dim
, isl_dim_in
))
6143 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6144 "domains don't match", goto error
);
6145 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6146 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6147 "index out of bounds", goto error
);
6150 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6152 for (i
= 0; i
< pma
->n
; ++i
) {
6153 for (j
= 0; j
< pa
->n
; ++j
) {
6155 isl_multi_aff
*res_ij
;
6158 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6159 isl_set_copy(pa
->p
[j
].set
));
6160 empty
= isl_set_plain_is_empty(common
);
6161 if (empty
< 0 || empty
) {
6162 isl_set_free(common
);
6168 res_ij
= isl_multi_aff_set_aff(
6169 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6170 isl_aff_copy(pa
->p
[j
].aff
));
6171 res_ij
= isl_multi_aff_gist(res_ij
,
6172 isl_set_copy(common
));
6174 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6178 isl_pw_multi_aff_free(pma
);
6179 isl_pw_aff_free(pa
);
6182 isl_pw_multi_aff_free(pma
);
6183 isl_pw_aff_free(pa
);
6184 return isl_pw_multi_aff_free(res
);
6187 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6189 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6190 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6191 __isl_take isl_pw_aff
*pa
)
6193 isl_bool equal_params
;
6197 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6198 if (equal_params
< 0)
6201 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6202 if (!isl_space_has_named_params(pma
->dim
) ||
6203 !isl_space_has_named_params(pa
->dim
))
6204 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6205 "unaligned unnamed parameters", goto error
);
6206 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6207 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6208 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6210 isl_pw_multi_aff_free(pma
);
6211 isl_pw_aff_free(pa
);
6215 /* Do the parameters of "pa" match those of "space"?
6217 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6218 __isl_keep isl_space
*space
)
6220 isl_space
*pa_space
;
6224 return isl_bool_error
;
6226 pa_space
= isl_pw_aff_get_space(pa
);
6228 match
= isl_space_has_equal_params(space
, pa_space
);
6230 isl_space_free(pa_space
);
6234 /* Check that the domain space of "pa" matches "space".
6236 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6237 __isl_keep isl_space
*space
)
6239 isl_space
*pa_space
;
6243 return isl_stat_error
;
6245 pa_space
= isl_pw_aff_get_space(pa
);
6247 match
= isl_space_has_equal_params(space
, pa_space
);
6251 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6252 "parameters don't match", goto error
);
6253 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6254 pa_space
, isl_dim_in
);
6258 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6259 "domains don't match", goto error
);
6260 isl_space_free(pa_space
);
6263 isl_space_free(pa_space
);
6264 return isl_stat_error
;
6272 #include <isl_multi_templ.c>
6273 #include <isl_multi_apply_set.c>
6274 #include <isl_multi_coalesce.c>
6275 #include <isl_multi_gist.c>
6276 #include <isl_multi_hash.c>
6277 #include <isl_multi_intersect.c>
6279 /* Scale the elements of "pma" by the corresponding elements of "mv".
6281 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6282 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6285 isl_bool equal_params
;
6287 pma
= isl_pw_multi_aff_cow(pma
);
6290 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6291 mv
->space
, isl_dim_set
))
6292 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6293 "spaces don't match", goto error
);
6294 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6295 if (equal_params
< 0)
6297 if (!equal_params
) {
6298 pma
= isl_pw_multi_aff_align_params(pma
,
6299 isl_multi_val_get_space(mv
));
6300 mv
= isl_multi_val_align_params(mv
,
6301 isl_pw_multi_aff_get_space(pma
));
6306 for (i
= 0; i
< pma
->n
; ++i
) {
6307 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6308 isl_multi_val_copy(mv
));
6309 if (!pma
->p
[i
].maff
)
6313 isl_multi_val_free(mv
);
6316 isl_multi_val_free(mv
);
6317 isl_pw_multi_aff_free(pma
);
6321 /* This function is called for each entry of an isl_union_pw_multi_aff.
6322 * If the space of the entry matches that of data->mv,
6323 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6324 * Otherwise, return an empty isl_pw_multi_aff.
6326 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6327 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6329 isl_multi_val
*mv
= user
;
6333 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6334 mv
->space
, isl_dim_set
)) {
6335 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6336 isl_pw_multi_aff_free(pma
);
6337 return isl_pw_multi_aff_empty(space
);
6340 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6343 /* Scale the elements of "upma" by the corresponding elements of "mv",
6344 * for those entries that match the space of "mv".
6346 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6347 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6349 upma
= isl_union_pw_multi_aff_align_params(upma
,
6350 isl_multi_val_get_space(mv
));
6351 mv
= isl_multi_val_align_params(mv
,
6352 isl_union_pw_multi_aff_get_space(upma
));
6356 return isl_union_pw_multi_aff_transform(upma
,
6357 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6359 isl_multi_val_free(mv
);
6362 isl_multi_val_free(mv
);
6363 isl_union_pw_multi_aff_free(upma
);
6367 /* Construct and return a piecewise multi affine expression
6368 * in the given space with value zero in each of the output dimensions and
6369 * a universe domain.
6371 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6373 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6376 /* Construct and return a piecewise multi affine expression
6377 * that is equal to the given piecewise affine expression.
6379 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6380 __isl_take isl_pw_aff
*pa
)
6384 isl_pw_multi_aff
*pma
;
6389 space
= isl_pw_aff_get_space(pa
);
6390 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6392 for (i
= 0; i
< pa
->n
; ++i
) {
6396 set
= isl_set_copy(pa
->p
[i
].set
);
6397 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6398 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6401 isl_pw_aff_free(pa
);
6405 /* Construct a set or map mapping the shared (parameter) domain
6406 * of the piecewise affine expressions to the range of "mpa"
6407 * with each dimension in the range equated to the
6408 * corresponding piecewise affine expression.
6410 static __isl_give isl_map
*map_from_multi_pw_aff(
6411 __isl_take isl_multi_pw_aff
*mpa
)
6420 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6421 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6422 "invalid space", goto error
);
6424 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6425 map
= isl_map_universe(isl_space_from_domain(space
));
6427 for (i
= 0; i
< mpa
->n
; ++i
) {
6431 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6432 map_i
= map_from_pw_aff(pa
);
6434 map
= isl_map_flat_range_product(map
, map_i
);
6437 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6439 isl_multi_pw_aff_free(mpa
);
6442 isl_multi_pw_aff_free(mpa
);
6446 /* Construct a map mapping the shared domain
6447 * of the piecewise affine expressions to the range of "mpa"
6448 * with each dimension in the range equated to the
6449 * corresponding piecewise affine expression.
6451 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6455 if (isl_space_is_set(mpa
->space
))
6456 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6457 "space of input is not a map", goto error
);
6459 return map_from_multi_pw_aff(mpa
);
6461 isl_multi_pw_aff_free(mpa
);
6465 /* Construct a set mapping the shared parameter domain
6466 * of the piecewise affine expressions to the space of "mpa"
6467 * with each dimension in the range equated to the
6468 * corresponding piecewise affine expression.
6470 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6474 if (!isl_space_is_set(mpa
->space
))
6475 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6476 "space of input is not a set", goto error
);
6478 return map_from_multi_pw_aff(mpa
);
6480 isl_multi_pw_aff_free(mpa
);
6484 /* Construct and return a piecewise multi affine expression
6485 * that is equal to the given multi piecewise affine expression
6486 * on the shared domain of the piecewise affine expressions.
6488 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6489 __isl_take isl_multi_pw_aff
*mpa
)
6494 isl_pw_multi_aff
*pma
;
6499 space
= isl_multi_pw_aff_get_space(mpa
);
6502 isl_multi_pw_aff_free(mpa
);
6503 return isl_pw_multi_aff_zero(space
);
6506 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6507 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6509 for (i
= 1; i
< mpa
->n
; ++i
) {
6510 isl_pw_multi_aff
*pma_i
;
6512 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6513 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6514 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6517 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6519 isl_multi_pw_aff_free(mpa
);
6523 /* Construct and return a multi piecewise affine expression
6524 * that is equal to the given multi affine expression.
6526 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6527 __isl_take isl_multi_aff
*ma
)
6530 isl_multi_pw_aff
*mpa
;
6535 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6536 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6538 for (i
= 0; i
< n
; ++i
) {
6541 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6542 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6545 isl_multi_aff_free(ma
);
6549 /* Construct and return a multi piecewise affine expression
6550 * that is equal to the given piecewise multi affine expression.
6552 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6553 __isl_take isl_pw_multi_aff
*pma
)
6557 isl_multi_pw_aff
*mpa
;
6562 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6563 space
= isl_pw_multi_aff_get_space(pma
);
6564 mpa
= isl_multi_pw_aff_alloc(space
);
6566 for (i
= 0; i
< n
; ++i
) {
6569 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6570 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6573 isl_pw_multi_aff_free(pma
);
6577 /* Do "pa1" and "pa2" represent the same function?
6579 * We first check if they are obviously equal.
6580 * If not, we convert them to maps and check if those are equal.
6582 * If "pa1" or "pa2" contain any NaNs, then they are considered
6583 * not to be the same. A NaN is not equal to anything, not even
6586 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6587 __isl_keep isl_pw_aff
*pa2
)
6591 isl_map
*map1
, *map2
;
6594 return isl_bool_error
;
6596 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6597 if (equal
< 0 || equal
)
6599 has_nan
= either_involves_nan(pa1
, pa2
);
6601 return isl_bool_error
;
6603 return isl_bool_false
;
6605 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6606 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6607 equal
= isl_map_is_equal(map1
, map2
);
6614 /* Do "mpa1" and "mpa2" represent the same function?
6616 * Note that we cannot convert the entire isl_multi_pw_aff
6617 * to a map because the domains of the piecewise affine expressions
6618 * may not be the same.
6620 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6621 __isl_keep isl_multi_pw_aff
*mpa2
)
6624 isl_bool equal
, equal_params
;
6627 return isl_bool_error
;
6629 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6630 if (equal_params
< 0)
6631 return isl_bool_error
;
6632 if (!equal_params
) {
6633 if (!isl_space_has_named_params(mpa1
->space
))
6634 return isl_bool_false
;
6635 if (!isl_space_has_named_params(mpa2
->space
))
6636 return isl_bool_false
;
6637 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6638 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6639 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6640 isl_multi_pw_aff_get_space(mpa2
));
6641 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6642 isl_multi_pw_aff_get_space(mpa1
));
6643 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6644 isl_multi_pw_aff_free(mpa1
);
6645 isl_multi_pw_aff_free(mpa2
);
6649 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6650 if (equal
< 0 || !equal
)
6653 for (i
= 0; i
< mpa1
->n
; ++i
) {
6654 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6655 if (equal
< 0 || !equal
)
6659 return isl_bool_true
;
6662 /* Do "pma1" and "pma2" represent the same function?
6664 * First check if they are obviously equal.
6665 * If not, then convert them to maps and check if those are equal.
6667 * If "pa1" or "pa2" contain any NaNs, then they are considered
6668 * not to be the same. A NaN is not equal to anything, not even
6671 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6672 __isl_keep isl_pw_multi_aff
*pma2
)
6676 isl_map
*map1
, *map2
;
6679 return isl_bool_error
;
6681 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6682 if (equal
< 0 || equal
)
6684 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6685 if (has_nan
>= 0 && !has_nan
)
6686 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6687 if (has_nan
< 0 || has_nan
)
6688 return isl_bool_not(has_nan
);
6690 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6691 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6692 equal
= isl_map_is_equal(map1
, map2
);
6699 /* Compute the pullback of "mpa" by the function represented by "ma".
6700 * In other words, plug in "ma" in "mpa".
6702 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6704 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6705 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6708 isl_space
*space
= NULL
;
6710 mpa
= isl_multi_pw_aff_cow(mpa
);
6714 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6715 isl_multi_pw_aff_get_space(mpa
));
6719 for (i
= 0; i
< mpa
->n
; ++i
) {
6720 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6721 isl_multi_aff_copy(ma
));
6726 isl_multi_aff_free(ma
);
6727 isl_space_free(mpa
->space
);
6731 isl_space_free(space
);
6732 isl_multi_pw_aff_free(mpa
);
6733 isl_multi_aff_free(ma
);
6737 /* Compute the pullback of "mpa" by the function represented by "ma".
6738 * In other words, plug in "ma" in "mpa".
6740 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6741 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6743 isl_bool equal_params
;
6747 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6748 if (equal_params
< 0)
6751 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6752 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6753 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6754 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6756 isl_multi_pw_aff_free(mpa
);
6757 isl_multi_aff_free(ma
);
6761 /* Compute the pullback of "mpa" by the function represented by "pma".
6762 * In other words, plug in "pma" in "mpa".
6764 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6766 static __isl_give isl_multi_pw_aff
*
6767 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6768 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6771 isl_space
*space
= NULL
;
6773 mpa
= isl_multi_pw_aff_cow(mpa
);
6777 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6778 isl_multi_pw_aff_get_space(mpa
));
6780 for (i
= 0; i
< mpa
->n
; ++i
) {
6781 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6782 isl_pw_multi_aff_copy(pma
));
6787 isl_pw_multi_aff_free(pma
);
6788 isl_space_free(mpa
->space
);
6792 isl_space_free(space
);
6793 isl_multi_pw_aff_free(mpa
);
6794 isl_pw_multi_aff_free(pma
);
6798 /* Compute the pullback of "mpa" by the function represented by "pma".
6799 * In other words, plug in "pma" in "mpa".
6801 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6802 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6804 isl_bool equal_params
;
6808 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6809 if (equal_params
< 0)
6812 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6813 mpa
= isl_multi_pw_aff_align_params(mpa
,
6814 isl_pw_multi_aff_get_space(pma
));
6815 pma
= isl_pw_multi_aff_align_params(pma
,
6816 isl_multi_pw_aff_get_space(mpa
));
6817 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6819 isl_multi_pw_aff_free(mpa
);
6820 isl_pw_multi_aff_free(pma
);
6824 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6825 * with the domain of "aff". The domain of the result is the same
6827 * "mpa" and "aff" are assumed to have been aligned.
6829 * We first extract the parametric constant from "aff", defined
6830 * over the correct domain.
6831 * Then we add the appropriate combinations of the members of "mpa".
6832 * Finally, we add the integer divisions through recursive calls.
6834 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6835 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6843 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6844 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6846 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6847 tmp
= isl_aff_copy(aff
);
6848 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6849 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6850 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6851 isl_space_dim(space
, isl_dim_set
));
6852 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6853 pa
= isl_pw_aff_from_aff(tmp
);
6855 for (i
= 0; i
< n_in
; ++i
) {
6858 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6860 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6861 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6862 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6863 pa
= isl_pw_aff_add(pa
, pa_i
);
6866 for (i
= 0; i
< n_div
; ++i
) {
6870 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6872 div
= isl_aff_get_div(aff
, i
);
6873 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6874 isl_multi_pw_aff_copy(mpa
), div
);
6875 pa_i
= isl_pw_aff_floor(pa_i
);
6876 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6877 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6878 pa
= isl_pw_aff_add(pa
, pa_i
);
6881 isl_multi_pw_aff_free(mpa
);
6887 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6888 * with the domain of "aff". The domain of the result is the same
6891 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6892 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6894 isl_bool equal_params
;
6898 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6899 if (equal_params
< 0)
6902 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6904 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6905 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6907 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6910 isl_multi_pw_aff_free(mpa
);
6914 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6915 * with the domain of "pa". The domain of the result is the same
6917 * "mpa" and "pa" are assumed to have been aligned.
6919 * We consider each piece in turn. Note that the domains of the
6920 * pieces are assumed to be disjoint and they remain disjoint
6921 * after taking the preimage (over the same function).
6923 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6924 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6933 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6934 isl_pw_aff_get_space(pa
));
6935 res
= isl_pw_aff_empty(space
);
6937 for (i
= 0; i
< pa
->n
; ++i
) {
6941 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6942 isl_multi_pw_aff_copy(mpa
),
6943 isl_aff_copy(pa
->p
[i
].aff
));
6944 domain
= isl_set_copy(pa
->p
[i
].set
);
6945 domain
= isl_set_preimage_multi_pw_aff(domain
,
6946 isl_multi_pw_aff_copy(mpa
));
6947 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6948 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6951 isl_pw_aff_free(pa
);
6952 isl_multi_pw_aff_free(mpa
);
6955 isl_pw_aff_free(pa
);
6956 isl_multi_pw_aff_free(mpa
);
6960 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6961 * with the domain of "pa". The domain of the result is the same
6964 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6965 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6967 isl_bool equal_params
;
6971 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6972 if (equal_params
< 0)
6975 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6977 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6978 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6980 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6982 isl_pw_aff_free(pa
);
6983 isl_multi_pw_aff_free(mpa
);
6987 /* Compute the pullback of "pa" by the function represented by "mpa".
6988 * In other words, plug in "mpa" in "pa".
6989 * "pa" and "mpa" are assumed to have been aligned.
6991 * The pullback is computed by applying "pa" to "mpa".
6993 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6994 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6996 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6999 /* Compute the pullback of "pa" by the function represented by "mpa".
7000 * In other words, plug in "mpa" in "pa".
7002 * The pullback is computed by applying "pa" to "mpa".
7004 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7005 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7007 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7010 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7011 * In other words, plug in "mpa2" in "mpa1".
7013 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7015 * We pullback each member of "mpa1" in turn.
7017 static __isl_give isl_multi_pw_aff
*
7018 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7019 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7022 isl_space
*space
= NULL
;
7024 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7028 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7029 isl_multi_pw_aff_get_space(mpa1
));
7031 for (i
= 0; i
< mpa1
->n
; ++i
) {
7032 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7033 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
7038 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7040 isl_multi_pw_aff_free(mpa2
);
7043 isl_space_free(space
);
7044 isl_multi_pw_aff_free(mpa1
);
7045 isl_multi_pw_aff_free(mpa2
);
7049 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7050 * In other words, plug in "mpa2" in "mpa1".
7052 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7053 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7055 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7056 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7059 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7060 * of "mpa1" and "mpa2" live in the same space, construct map space
7061 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7062 * with this map space as extract argument.
7064 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7065 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7066 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7067 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7070 isl_space
*space1
, *space2
;
7073 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7074 isl_multi_pw_aff_get_space(mpa2
));
7075 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7076 isl_multi_pw_aff_get_space(mpa1
));
7079 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7080 mpa2
->space
, isl_dim_out
);
7084 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7085 "range spaces don't match", goto error
);
7086 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7087 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7088 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7090 res
= order(mpa1
, mpa2
, space1
);
7091 isl_multi_pw_aff_free(mpa1
);
7092 isl_multi_pw_aff_free(mpa2
);
7095 isl_multi_pw_aff_free(mpa1
);
7096 isl_multi_pw_aff_free(mpa2
);
7100 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7101 * where the function values are equal. "space" is the space of the result.
7102 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7104 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7105 * in the sequences are equal.
7107 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7108 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7109 __isl_take isl_space
*space
)
7114 res
= isl_map_universe(space
);
7116 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7117 for (i
= 0; i
< n
; ++i
) {
7118 isl_pw_aff
*pa1
, *pa2
;
7121 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7122 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7123 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7124 res
= isl_map_intersect(res
, map
);
7130 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7131 * where the function values are equal.
7133 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7134 __isl_take isl_multi_pw_aff
*mpa2
)
7136 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7137 &isl_multi_pw_aff_eq_map_on_space
);
7140 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7141 * where the function values of "mpa1" is lexicographically satisfies "base"
7142 * compared to that of "mpa2". "space" is the space of the result.
7143 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7145 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7146 * if its i-th element satisfies "base" when compared to
7147 * the i-th element of "mpa2" while all previous elements are
7150 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7151 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7152 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7153 __isl_take isl_pw_aff
*pa2
),
7154 __isl_take isl_space
*space
)
7157 isl_map
*res
, *rest
;
7159 res
= isl_map_empty(isl_space_copy(space
));
7160 rest
= isl_map_universe(space
);
7162 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7163 for (i
= 0; i
< n
; ++i
) {
7164 isl_pw_aff
*pa1
, *pa2
;
7167 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7168 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7169 map
= base(pa1
, pa2
);
7170 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7171 res
= isl_map_union(res
, map
);
7176 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7177 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7178 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7179 rest
= isl_map_intersect(rest
, map
);
7186 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7187 * where the function value of "mpa1" is lexicographically less than that
7188 * of "mpa2". "space" is the space of the result.
7189 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7191 * "mpa1" is less than "mpa2" if its i-th element is smaller
7192 * than the i-th element of "mpa2" while all previous elements are
7195 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7196 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7197 __isl_take isl_space
*space
)
7199 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7200 &isl_pw_aff_lt_map
, space
);
7203 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7204 * where the function value of "mpa1" is lexicographically less than that
7207 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7208 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7210 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7211 &isl_multi_pw_aff_lex_lt_map_on_space
);
7214 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7215 * where the function value of "mpa1" is lexicographically greater than that
7216 * of "mpa2". "space" is the space of the result.
7217 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7219 * "mpa1" is greater than "mpa2" if its i-th element is greater
7220 * than the i-th element of "mpa2" while all previous elements are
7223 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7224 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7225 __isl_take isl_space
*space
)
7227 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7228 &isl_pw_aff_gt_map
, space
);
7231 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7232 * where the function value of "mpa1" is lexicographically greater than that
7235 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7236 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7238 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7239 &isl_multi_pw_aff_lex_gt_map_on_space
);
7242 /* Compare two isl_affs.
7244 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7245 * than "aff2" and 0 if they are equal.
7247 * The order is fairly arbitrary. We do consider expressions that only involve
7248 * earlier dimensions as "smaller".
7250 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7263 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7267 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7268 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7270 return last1
- last2
;
7272 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7275 /* Compare two isl_pw_affs.
7277 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7278 * than "pa2" and 0 if they are equal.
7280 * The order is fairly arbitrary. We do consider expressions that only involve
7281 * earlier dimensions as "smaller".
7283 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7284 __isl_keep isl_pw_aff
*pa2
)
7297 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7301 if (pa1
->n
!= pa2
->n
)
7302 return pa1
->n
- pa2
->n
;
7304 for (i
= 0; i
< pa1
->n
; ++i
) {
7305 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7308 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7316 /* Return a piecewise affine expression that is equal to "v" on "domain".
7318 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7319 __isl_take isl_val
*v
)
7322 isl_local_space
*ls
;
7325 space
= isl_set_get_space(domain
);
7326 ls
= isl_local_space_from_space(space
);
7327 aff
= isl_aff_val_on_domain(ls
, v
);
7329 return isl_pw_aff_alloc(domain
, aff
);
7332 /* Return a multi affine expression that is equal to "mv" on domain
7335 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7336 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7340 isl_local_space
*ls
;
7346 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7347 space2
= isl_multi_val_get_space(mv
);
7348 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7349 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7350 space
= isl_space_map_from_domain_and_range(space
, space2
);
7351 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7352 ls
= isl_local_space_from_space(isl_space_domain(space
));
7353 for (i
= 0; i
< n
; ++i
) {
7357 v
= isl_multi_val_get_val(mv
, i
);
7358 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7359 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7361 isl_local_space_free(ls
);
7363 isl_multi_val_free(mv
);
7366 isl_space_free(space
);
7367 isl_multi_val_free(mv
);
7371 /* Return a piecewise multi-affine expression
7372 * that is equal to "mv" on "domain".
7374 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7375 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7380 space
= isl_set_get_space(domain
);
7381 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7383 return isl_pw_multi_aff_alloc(domain
, ma
);
7386 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7387 * mv is the value that should be attained on each domain set
7388 * res collects the results
7390 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7392 isl_union_pw_multi_aff
*res
;
7395 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7396 * and add it to data->res.
7398 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7401 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7402 isl_pw_multi_aff
*pma
;
7405 mv
= isl_multi_val_copy(data
->mv
);
7406 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7407 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7409 return data
->res
? isl_stat_ok
: isl_stat_error
;
7412 /* Return a union piecewise multi-affine expression
7413 * that is equal to "mv" on "domain".
7415 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7416 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7418 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7421 space
= isl_union_set_get_space(domain
);
7422 data
.res
= isl_union_pw_multi_aff_empty(space
);
7424 if (isl_union_set_foreach_set(domain
,
7425 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7426 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7427 isl_union_set_free(domain
);
7428 isl_multi_val_free(mv
);
7432 /* Compute the pullback of data->pma by the function represented by "pma2",
7433 * provided the spaces match, and add the results to data->res.
7435 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7437 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7439 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7440 pma2
->dim
, isl_dim_out
)) {
7441 isl_pw_multi_aff_free(pma2
);
7445 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7446 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7448 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7450 return isl_stat_error
;
7455 /* Compute the pullback of "upma1" by the function represented by "upma2".
7457 __isl_give isl_union_pw_multi_aff
*
7458 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7459 __isl_take isl_union_pw_multi_aff
*upma1
,
7460 __isl_take isl_union_pw_multi_aff
*upma2
)
7462 return bin_op(upma1
, upma2
, &pullback_entry
);
7465 /* Check that the domain space of "upa" matches "space".
7467 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7468 * can in principle never fail since the space "space" is that
7469 * of the isl_multi_union_pw_aff and is a set space such that
7470 * there is no domain space to match.
7472 * We check the parameters and double-check that "space" is
7473 * indeed that of a set.
7475 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7476 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7478 isl_space
*upa_space
;
7482 return isl_stat_error
;
7484 match
= isl_space_is_set(space
);
7486 return isl_stat_error
;
7488 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7489 "expecting set space", return -1);
7491 upa_space
= isl_union_pw_aff_get_space(upa
);
7492 match
= isl_space_has_equal_params(space
, upa_space
);
7496 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7497 "parameters don't match", goto error
);
7499 isl_space_free(upa_space
);
7502 isl_space_free(upa_space
);
7503 return isl_stat_error
;
7506 /* Do the parameters of "upa" match those of "space"?
7508 static isl_bool
isl_union_pw_aff_matching_params(
7509 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7511 isl_space
*upa_space
;
7515 return isl_bool_error
;
7517 upa_space
= isl_union_pw_aff_get_space(upa
);
7519 match
= isl_space_has_equal_params(space
, upa_space
);
7521 isl_space_free(upa_space
);
7525 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7526 * space represents the new parameters.
7527 * res collects the results.
7529 struct isl_union_pw_aff_reset_params_data
{
7531 isl_union_pw_aff
*res
;
7534 /* Replace the parameters of "pa" by data->space and
7535 * add the result to data->res.
7537 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7539 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7542 space
= isl_pw_aff_get_space(pa
);
7543 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7544 pa
= isl_pw_aff_reset_space(pa
, space
);
7545 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7547 return data
->res
? isl_stat_ok
: isl_stat_error
;
7550 /* Replace the domain space of "upa" by "space".
7551 * Since a union expression does not have a (single) domain space,
7552 * "space" is necessarily a parameter space.
7554 * Since the order and the names of the parameters determine
7555 * the hash value, we need to create a new hash table.
7557 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7558 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7560 struct isl_union_pw_aff_reset_params_data data
= { space
};
7563 match
= isl_union_pw_aff_matching_params(upa
, space
);
7565 upa
= isl_union_pw_aff_free(upa
);
7567 isl_space_free(space
);
7571 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7572 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7573 data
.res
= isl_union_pw_aff_free(data
.res
);
7575 isl_union_pw_aff_free(upa
);
7576 isl_space_free(space
);
7580 /* Return the floor of "pa".
7582 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7584 return isl_pw_aff_floor(pa
);
7587 /* Given f, return floor(f).
7589 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7590 __isl_take isl_union_pw_aff
*upa
)
7592 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7597 * upa mod m = upa - m * floor(upa/m)
7599 * with m an integer value.
7601 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7602 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7604 isl_union_pw_aff
*res
;
7609 if (!isl_val_is_int(m
))
7610 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7611 "expecting integer modulo", goto error
);
7612 if (!isl_val_is_pos(m
))
7613 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7614 "expecting positive modulo", goto error
);
7616 res
= isl_union_pw_aff_copy(upa
);
7617 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7618 upa
= isl_union_pw_aff_floor(upa
);
7619 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7620 res
= isl_union_pw_aff_sub(res
, upa
);
7625 isl_union_pw_aff_free(upa
);
7629 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7630 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7632 * "res" collects the results.
7634 struct isl_union_pw_aff_aff_on_domain_data
{
7636 isl_union_pw_aff
*res
;
7639 /* Construct a piecewise affine expression that is equal to data->aff
7640 * on "domain" and add the result to data->res.
7642 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7644 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7649 aff
= isl_aff_copy(data
->aff
);
7650 dim
= isl_set_dim(domain
, isl_dim_set
);
7651 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7652 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7653 pa
= isl_pw_aff_alloc(domain
, aff
);
7654 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7656 return data
->res
? isl_stat_ok
: isl_stat_error
;
7659 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7660 * pos is the output position that needs to be extracted.
7661 * res collects the results.
7663 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7665 isl_union_pw_aff
*res
;
7668 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7669 * (assuming it has such a dimension) and add it to data->res.
7671 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7673 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7678 return isl_stat_error
;
7680 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7681 if (data
->pos
>= n_out
) {
7682 isl_pw_multi_aff_free(pma
);
7686 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7687 isl_pw_multi_aff_free(pma
);
7689 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7691 return data
->res
? isl_stat_ok
: isl_stat_error
;
7694 /* Extract an isl_union_pw_aff corresponding to
7695 * output dimension "pos" of "upma".
7697 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7698 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7700 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7707 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7708 "cannot extract at negative position", return NULL
);
7710 space
= isl_union_pw_multi_aff_get_space(upma
);
7711 data
.res
= isl_union_pw_aff_empty(space
);
7713 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7714 &get_union_pw_aff
, &data
) < 0)
7715 data
.res
= isl_union_pw_aff_free(data
.res
);
7720 /* Return a union piecewise affine expression
7721 * that is equal to "aff" on "domain".
7723 * Construct an isl_pw_aff on each of the sets in "domain" and
7724 * collect the results.
7726 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7727 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7729 struct isl_union_pw_aff_aff_on_domain_data data
;
7732 if (!domain
|| !aff
)
7734 if (!isl_local_space_is_params(aff
->ls
))
7735 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7736 "expecting parametric expression", goto error
);
7738 space
= isl_union_set_get_space(domain
);
7739 data
.res
= isl_union_pw_aff_empty(space
);
7741 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7742 data
.res
= isl_union_pw_aff_free(data
.res
);
7743 isl_union_set_free(domain
);
7747 isl_union_set_free(domain
);
7752 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7753 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7754 * "res" collects the results.
7756 struct isl_union_pw_aff_val_on_domain_data
{
7758 isl_union_pw_aff
*res
;
7761 /* Construct a piecewise affine expression that is equal to data->v
7762 * on "domain" and add the result to data->res.
7764 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7766 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7770 v
= isl_val_copy(data
->v
);
7771 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7772 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7774 return data
->res
? isl_stat_ok
: isl_stat_error
;
7777 /* Return a union piecewise affine expression
7778 * that is equal to "v" on "domain".
7780 * Construct an isl_pw_aff on each of the sets in "domain" and
7781 * collect the results.
7783 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7784 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7786 struct isl_union_pw_aff_val_on_domain_data data
;
7789 space
= isl_union_set_get_space(domain
);
7790 data
.res
= isl_union_pw_aff_empty(space
);
7792 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7793 data
.res
= isl_union_pw_aff_free(data
.res
);
7794 isl_union_set_free(domain
);
7799 /* Construct a piecewise multi affine expression
7800 * that is equal to "pa" and add it to upma.
7802 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7805 isl_union_pw_multi_aff
**upma
= user
;
7806 isl_pw_multi_aff
*pma
;
7808 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7809 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7811 return *upma
? isl_stat_ok
: isl_stat_error
;
7814 /* Construct and return a union piecewise multi affine expression
7815 * that is equal to the given union piecewise affine expression.
7817 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7818 __isl_take isl_union_pw_aff
*upa
)
7821 isl_union_pw_multi_aff
*upma
;
7826 space
= isl_union_pw_aff_get_space(upa
);
7827 upma
= isl_union_pw_multi_aff_empty(space
);
7829 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7830 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7831 upma
= isl_union_pw_multi_aff_free(upma
);
7833 isl_union_pw_aff_free(upa
);
7837 /* Compute the set of elements in the domain of "pa" where it is zero and
7838 * add this set to "uset".
7840 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7842 isl_union_set
**uset
= (isl_union_set
**)user
;
7844 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7846 return *uset
? isl_stat_ok
: isl_stat_error
;
7849 /* Return a union set containing those elements in the domain
7850 * of "upa" where it is zero.
7852 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7853 __isl_take isl_union_pw_aff
*upa
)
7855 isl_union_set
*zero
;
7857 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7858 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7859 zero
= isl_union_set_free(zero
);
7861 isl_union_pw_aff_free(upa
);
7865 /* Convert "pa" to an isl_map and add it to *umap.
7867 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7869 isl_union_map
**umap
= user
;
7872 map
= isl_map_from_pw_aff(pa
);
7873 *umap
= isl_union_map_add_map(*umap
, map
);
7875 return *umap
? isl_stat_ok
: isl_stat_error
;
7878 /* Construct a union map mapping the domain of the union
7879 * piecewise affine expression to its range, with the single output dimension
7880 * equated to the corresponding affine expressions on their cells.
7882 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7883 __isl_take isl_union_pw_aff
*upa
)
7886 isl_union_map
*umap
;
7891 space
= isl_union_pw_aff_get_space(upa
);
7892 umap
= isl_union_map_empty(space
);
7894 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7896 umap
= isl_union_map_free(umap
);
7898 isl_union_pw_aff_free(upa
);
7902 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7903 * upma is the function that is plugged in.
7904 * pa is the current part of the function in which upma is plugged in.
7905 * res collects the results.
7907 struct isl_union_pw_aff_pullback_upma_data
{
7908 isl_union_pw_multi_aff
*upma
;
7910 isl_union_pw_aff
*res
;
7913 /* Check if "pma" can be plugged into data->pa.
7914 * If so, perform the pullback and add the result to data->res.
7916 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7918 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7921 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7922 pma
->dim
, isl_dim_out
)) {
7923 isl_pw_multi_aff_free(pma
);
7927 pa
= isl_pw_aff_copy(data
->pa
);
7928 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7930 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7932 return data
->res
? isl_stat_ok
: isl_stat_error
;
7935 /* Check if any of the elements of data->upma can be plugged into pa,
7936 * add if so add the result to data->res.
7938 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7940 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7944 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7946 isl_pw_aff_free(pa
);
7951 /* Compute the pullback of "upa" by the function represented by "upma".
7952 * In other words, plug in "upma" in "upa". The result contains
7953 * expressions defined over the domain space of "upma".
7955 * Run over all pairs of elements in "upa" and "upma", perform
7956 * the pullback when appropriate and collect the results.
7957 * If the hash value were based on the domain space rather than
7958 * the function space, then we could run through all elements
7959 * of "upma" and directly pick out the corresponding element of "upa".
7961 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7962 __isl_take isl_union_pw_aff
*upa
,
7963 __isl_take isl_union_pw_multi_aff
*upma
)
7965 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7968 space
= isl_union_pw_multi_aff_get_space(upma
);
7969 upa
= isl_union_pw_aff_align_params(upa
, space
);
7970 space
= isl_union_pw_aff_get_space(upa
);
7971 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7977 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7978 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7979 data
.res
= isl_union_pw_aff_free(data
.res
);
7981 isl_union_pw_aff_free(upa
);
7982 isl_union_pw_multi_aff_free(upma
);
7985 isl_union_pw_aff_free(upa
);
7986 isl_union_pw_multi_aff_free(upma
);
7991 #define BASE union_pw_aff
7993 #define DOMBASE union_set
7995 #define NO_MOVE_DIMS
8004 #include <isl_multi_templ.c>
8005 #include <isl_multi_apply_set.c>
8006 #include <isl_multi_apply_union_set.c>
8007 #include <isl_multi_coalesce.c>
8008 #include <isl_multi_floor.c>
8009 #include <isl_multi_gist.c>
8010 #include <isl_multi_intersect.c>
8012 /* Construct a multiple union piecewise affine expression
8013 * in the given space with value zero in each of the output dimensions.
8015 * Since there is no canonical zero value for
8016 * a union piecewise affine expression, we can only construct
8017 * zero-dimensional "zero" value.
8019 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8020 __isl_take isl_space
*space
)
8025 if (!isl_space_is_set(space
))
8026 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8027 "expecting set space", goto error
);
8028 if (isl_space_dim(space
, isl_dim_out
) != 0)
8029 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8030 "expecting 0D space", goto error
);
8032 return isl_multi_union_pw_aff_alloc(space
);
8034 isl_space_free(space
);
8038 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8039 * with the actual sum on the shared domain and
8040 * the defined expression on the symmetric difference of the domains.
8042 * We simply iterate over the elements in both arguments and
8043 * call isl_union_pw_aff_union_add on each of them.
8045 static __isl_give isl_multi_union_pw_aff
*
8046 isl_multi_union_pw_aff_union_add_aligned(
8047 __isl_take isl_multi_union_pw_aff
*mupa1
,
8048 __isl_take isl_multi_union_pw_aff
*mupa2
)
8050 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8051 &isl_union_pw_aff_union_add
);
8054 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8055 * with the actual sum on the shared domain and
8056 * the defined expression on the symmetric difference of the domains.
8058 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8059 __isl_take isl_multi_union_pw_aff
*mupa1
,
8060 __isl_take isl_multi_union_pw_aff
*mupa2
)
8062 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8063 &isl_multi_union_pw_aff_union_add_aligned
);
8066 /* Construct and return a multi union piecewise affine expression
8067 * that is equal to the given multi affine expression.
8069 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8070 __isl_take isl_multi_aff
*ma
)
8072 isl_multi_pw_aff
*mpa
;
8074 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8075 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8078 /* Construct and return a multi union piecewise affine expression
8079 * that is equal to the given multi piecewise affine expression.
8081 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8082 __isl_take isl_multi_pw_aff
*mpa
)
8086 isl_multi_union_pw_aff
*mupa
;
8091 space
= isl_multi_pw_aff_get_space(mpa
);
8092 space
= isl_space_range(space
);
8093 mupa
= isl_multi_union_pw_aff_alloc(space
);
8095 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8096 for (i
= 0; i
< n
; ++i
) {
8098 isl_union_pw_aff
*upa
;
8100 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8101 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8102 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8105 isl_multi_pw_aff_free(mpa
);
8110 /* Extract the range space of "pma" and assign it to *space.
8111 * If *space has already been set (through a previous call to this function),
8112 * then check that the range space is the same.
8114 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8116 isl_space
**space
= user
;
8117 isl_space
*pma_space
;
8120 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8121 isl_pw_multi_aff_free(pma
);
8124 return isl_stat_error
;
8130 equal
= isl_space_is_equal(pma_space
, *space
);
8131 isl_space_free(pma_space
);
8134 return isl_stat_error
;
8136 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8137 "range spaces not the same", return isl_stat_error
);
8141 /* Construct and return a multi union piecewise affine expression
8142 * that is equal to the given union piecewise multi affine expression.
8144 * In order to be able to perform the conversion, the input
8145 * needs to be non-empty and may only involve a single range space.
8147 __isl_give isl_multi_union_pw_aff
*
8148 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8149 __isl_take isl_union_pw_multi_aff
*upma
)
8151 isl_space
*space
= NULL
;
8152 isl_multi_union_pw_aff
*mupa
;
8157 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8158 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8159 "cannot extract range space from empty input",
8161 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8168 n
= isl_space_dim(space
, isl_dim_set
);
8169 mupa
= isl_multi_union_pw_aff_alloc(space
);
8171 for (i
= 0; i
< n
; ++i
) {
8172 isl_union_pw_aff
*upa
;
8174 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8175 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8178 isl_union_pw_multi_aff_free(upma
);
8181 isl_space_free(space
);
8182 isl_union_pw_multi_aff_free(upma
);
8186 /* Try and create an isl_multi_union_pw_aff that is equivalent
8187 * to the given isl_union_map.
8188 * The isl_union_map is required to be single-valued in each space.
8189 * Moreover, it cannot be empty and all range spaces need to be the same.
8190 * Otherwise, an error is produced.
8192 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8193 __isl_take isl_union_map
*umap
)
8195 isl_union_pw_multi_aff
*upma
;
8197 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8198 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8201 /* Return a multiple union piecewise affine expression
8202 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8203 * have been aligned.
8205 static __isl_give isl_multi_union_pw_aff
*
8206 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8207 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8211 isl_multi_union_pw_aff
*mupa
;
8216 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8217 space
= isl_multi_val_get_space(mv
);
8218 mupa
= isl_multi_union_pw_aff_alloc(space
);
8219 for (i
= 0; i
< n
; ++i
) {
8221 isl_union_pw_aff
*upa
;
8223 v
= isl_multi_val_get_val(mv
, i
);
8224 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8226 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8229 isl_union_set_free(domain
);
8230 isl_multi_val_free(mv
);
8233 isl_union_set_free(domain
);
8234 isl_multi_val_free(mv
);
8238 /* Return a multiple union piecewise affine expression
8239 * that is equal to "mv" on "domain".
8241 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8242 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8244 isl_bool equal_params
;
8248 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8249 if (equal_params
< 0)
8252 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8254 domain
= isl_union_set_align_params(domain
,
8255 isl_multi_val_get_space(mv
));
8256 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8257 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8259 isl_union_set_free(domain
);
8260 isl_multi_val_free(mv
);
8264 /* Return a multiple union piecewise affine expression
8265 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8266 * have been aligned.
8268 static __isl_give isl_multi_union_pw_aff
*
8269 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8270 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8274 isl_multi_union_pw_aff
*mupa
;
8279 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8280 space
= isl_multi_aff_get_space(ma
);
8281 mupa
= isl_multi_union_pw_aff_alloc(space
);
8282 for (i
= 0; i
< n
; ++i
) {
8284 isl_union_pw_aff
*upa
;
8286 aff
= isl_multi_aff_get_aff(ma
, i
);
8287 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8289 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8292 isl_union_set_free(domain
);
8293 isl_multi_aff_free(ma
);
8296 isl_union_set_free(domain
);
8297 isl_multi_aff_free(ma
);
8301 /* Return a multiple union piecewise affine expression
8302 * that is equal to "ma" on "domain".
8304 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8305 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8307 isl_bool equal_params
;
8311 equal_params
= isl_space_has_equal_params(domain
->dim
, ma
->space
);
8312 if (equal_params
< 0)
8315 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8317 domain
= isl_union_set_align_params(domain
,
8318 isl_multi_aff_get_space(ma
));
8319 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8320 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8322 isl_union_set_free(domain
);
8323 isl_multi_aff_free(ma
);
8327 /* Return a union set containing those elements in the domains
8328 * of the elements of "mupa" where they are all zero.
8330 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8331 __isl_take isl_multi_union_pw_aff
*mupa
)
8334 isl_union_pw_aff
*upa
;
8335 isl_union_set
*zero
;
8340 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8342 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8343 "cannot determine zero set "
8344 "of zero-dimensional function", goto error
);
8346 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8347 zero
= isl_union_pw_aff_zero_union_set(upa
);
8349 for (i
= 1; i
< n
; ++i
) {
8350 isl_union_set
*zero_i
;
8352 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8353 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8355 zero
= isl_union_set_intersect(zero
, zero_i
);
8358 isl_multi_union_pw_aff_free(mupa
);
8361 isl_multi_union_pw_aff_free(mupa
);
8365 /* Construct a union map mapping the shared domain
8366 * of the union piecewise affine expressions to the range of "mupa"
8367 * with each dimension in the range equated to the
8368 * corresponding union piecewise affine expression.
8370 * The input cannot be zero-dimensional as there is
8371 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8373 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8374 __isl_take isl_multi_union_pw_aff
*mupa
)
8378 isl_union_map
*umap
;
8379 isl_union_pw_aff
*upa
;
8384 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8386 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8387 "cannot determine domain of zero-dimensional "
8388 "isl_multi_union_pw_aff", goto error
);
8390 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8391 umap
= isl_union_map_from_union_pw_aff(upa
);
8393 for (i
= 1; i
< n
; ++i
) {
8394 isl_union_map
*umap_i
;
8396 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8397 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8398 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8401 space
= isl_multi_union_pw_aff_get_space(mupa
);
8402 umap
= isl_union_map_reset_range_space(umap
, space
);
8404 isl_multi_union_pw_aff_free(mupa
);
8407 isl_multi_union_pw_aff_free(mupa
);
8411 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8412 * "range" is the space from which to set the range space.
8413 * "res" collects the results.
8415 struct isl_union_pw_multi_aff_reset_range_space_data
{
8417 isl_union_pw_multi_aff
*res
;
8420 /* Replace the range space of "pma" by the range space of data->range and
8421 * add the result to data->res.
8423 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8425 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8428 space
= isl_pw_multi_aff_get_space(pma
);
8429 space
= isl_space_domain(space
);
8430 space
= isl_space_extend_domain_with_range(space
,
8431 isl_space_copy(data
->range
));
8432 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8433 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8435 return data
->res
? isl_stat_ok
: isl_stat_error
;
8438 /* Replace the range space of all the piecewise affine expressions in "upma" by
8439 * the range space of "space".
8441 * This assumes that all these expressions have the same output dimension.
8443 * Since the spaces of the expressions change, so do their hash values.
8444 * We therefore need to create a new isl_union_pw_multi_aff.
8445 * Note that the hash value is currently computed based on the entire
8446 * space even though there can only be a single expression with a given
8449 static __isl_give isl_union_pw_multi_aff
*
8450 isl_union_pw_multi_aff_reset_range_space(
8451 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8453 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8454 isl_space
*space_upma
;
8456 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8457 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8458 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8459 &reset_range_space
, &data
) < 0)
8460 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8462 isl_space_free(space
);
8463 isl_union_pw_multi_aff_free(upma
);
8467 /* Construct and return a union piecewise multi affine expression
8468 * that is equal to the given multi union piecewise affine expression.
8470 * In order to be able to perform the conversion, the input
8471 * needs to have a least one output dimension.
8473 __isl_give isl_union_pw_multi_aff
*
8474 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8475 __isl_take isl_multi_union_pw_aff
*mupa
)
8479 isl_union_pw_multi_aff
*upma
;
8480 isl_union_pw_aff
*upa
;
8485 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8487 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8488 "cannot determine domain of zero-dimensional "
8489 "isl_multi_union_pw_aff", goto error
);
8491 space
= isl_multi_union_pw_aff_get_space(mupa
);
8492 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8493 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8495 for (i
= 1; i
< n
; ++i
) {
8496 isl_union_pw_multi_aff
*upma_i
;
8498 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8499 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8500 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8503 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8505 isl_multi_union_pw_aff_free(mupa
);
8508 isl_multi_union_pw_aff_free(mupa
);
8512 /* Intersect the range of "mupa" with "range".
8513 * That is, keep only those domain elements that have a function value
8516 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8517 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8519 isl_union_pw_multi_aff
*upma
;
8520 isl_union_set
*domain
;
8525 if (!mupa
|| !range
)
8528 space
= isl_set_get_space(range
);
8529 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8530 space
, isl_dim_set
);
8531 isl_space_free(space
);
8535 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8536 "space don't match", goto error
);
8537 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8539 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8540 "cannot intersect range of zero-dimensional "
8541 "isl_multi_union_pw_aff", goto error
);
8543 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8544 isl_multi_union_pw_aff_copy(mupa
));
8545 domain
= isl_union_set_from_set(range
);
8546 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8547 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8551 isl_multi_union_pw_aff_free(mupa
);
8552 isl_set_free(range
);
8556 /* Return the shared domain of the elements of "mupa".
8558 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8559 __isl_take isl_multi_union_pw_aff
*mupa
)
8562 isl_union_pw_aff
*upa
;
8568 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8570 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8571 "cannot determine domain", goto error
);
8573 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8574 dom
= isl_union_pw_aff_domain(upa
);
8575 for (i
= 1; i
< n
; ++i
) {
8576 isl_union_set
*dom_i
;
8578 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8579 dom_i
= isl_union_pw_aff_domain(upa
);
8580 dom
= isl_union_set_intersect(dom
, dom_i
);
8583 isl_multi_union_pw_aff_free(mupa
);
8586 isl_multi_union_pw_aff_free(mupa
);
8590 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8591 * In particular, the spaces have been aligned.
8592 * The result is defined over the shared domain of the elements of "mupa"
8594 * We first extract the parametric constant part of "aff" and
8595 * define that over the shared domain.
8596 * Then we iterate over all input dimensions of "aff" and add the corresponding
8597 * multiples of the elements of "mupa".
8598 * Finally, we consider the integer divisions, calling the function
8599 * recursively to obtain an isl_union_pw_aff corresponding to the
8600 * integer division argument.
8602 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8603 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8606 isl_union_pw_aff
*upa
;
8607 isl_union_set
*uset
;
8611 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8612 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8614 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8615 cst
= isl_aff_copy(aff
);
8616 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8617 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8618 cst
= isl_aff_project_domain_on_params(cst
);
8619 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8621 for (i
= 0; i
< n_in
; ++i
) {
8622 isl_union_pw_aff
*upa_i
;
8624 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8626 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8627 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8628 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8629 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8632 for (i
= 0; i
< n_div
; ++i
) {
8634 isl_union_pw_aff
*upa_i
;
8636 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8638 div
= isl_aff_get_div(aff
, i
);
8639 upa_i
= multi_union_pw_aff_apply_aff(
8640 isl_multi_union_pw_aff_copy(mupa
), div
);
8641 upa_i
= isl_union_pw_aff_floor(upa_i
);
8642 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8643 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8644 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8647 isl_multi_union_pw_aff_free(mupa
);
8653 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8654 * with the domain of "aff".
8655 * Furthermore, the dimension of this space needs to be greater than zero.
8656 * The result is defined over the shared domain of the elements of "mupa"
8658 * We perform these checks and then hand over control to
8659 * multi_union_pw_aff_apply_aff.
8661 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8662 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8664 isl_space
*space1
, *space2
;
8667 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8668 isl_aff_get_space(aff
));
8669 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8673 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8674 space2
= isl_aff_get_domain_space(aff
);
8675 equal
= isl_space_is_equal(space1
, space2
);
8676 isl_space_free(space1
);
8677 isl_space_free(space2
);
8681 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8682 "spaces don't match", goto error
);
8683 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8684 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8685 "cannot determine domains", goto error
);
8687 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8689 isl_multi_union_pw_aff_free(mupa
);
8694 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8695 * with the domain of "ma".
8696 * Furthermore, the dimension of this space needs to be greater than zero,
8697 * unless the dimension of the target space of "ma" is also zero.
8698 * The result is defined over the shared domain of the elements of "mupa"
8700 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8701 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8703 isl_space
*space1
, *space2
;
8704 isl_multi_union_pw_aff
*res
;
8708 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8709 isl_multi_aff_get_space(ma
));
8710 ma
= isl_multi_aff_align_params(ma
,
8711 isl_multi_union_pw_aff_get_space(mupa
));
8715 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8716 space2
= isl_multi_aff_get_domain_space(ma
);
8717 equal
= isl_space_is_equal(space1
, space2
);
8718 isl_space_free(space1
);
8719 isl_space_free(space2
);
8723 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8724 "spaces don't match", goto error
);
8725 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8726 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8727 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8728 "cannot determine domains", goto error
);
8730 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8731 res
= isl_multi_union_pw_aff_alloc(space1
);
8733 for (i
= 0; i
< n_out
; ++i
) {
8735 isl_union_pw_aff
*upa
;
8737 aff
= isl_multi_aff_get_aff(ma
, i
);
8738 upa
= multi_union_pw_aff_apply_aff(
8739 isl_multi_union_pw_aff_copy(mupa
), aff
);
8740 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8743 isl_multi_aff_free(ma
);
8744 isl_multi_union_pw_aff_free(mupa
);
8747 isl_multi_union_pw_aff_free(mupa
);
8748 isl_multi_aff_free(ma
);
8752 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8753 * with the domain of "pa".
8754 * Furthermore, the dimension of this space needs to be greater than zero.
8755 * The result is defined over the shared domain of the elements of "mupa"
8757 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8758 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8762 isl_space
*space
, *space2
;
8763 isl_union_pw_aff
*upa
;
8765 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8766 isl_pw_aff_get_space(pa
));
8767 pa
= isl_pw_aff_align_params(pa
,
8768 isl_multi_union_pw_aff_get_space(mupa
));
8772 space
= isl_multi_union_pw_aff_get_space(mupa
);
8773 space2
= isl_pw_aff_get_domain_space(pa
);
8774 equal
= isl_space_is_equal(space
, space2
);
8775 isl_space_free(space
);
8776 isl_space_free(space2
);
8780 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8781 "spaces don't match", goto error
);
8782 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8783 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8784 "cannot determine domains", goto error
);
8786 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8787 upa
= isl_union_pw_aff_empty(space
);
8789 for (i
= 0; i
< pa
->n
; ++i
) {
8792 isl_multi_union_pw_aff
*mupa_i
;
8793 isl_union_pw_aff
*upa_i
;
8795 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8796 domain
= isl_set_copy(pa
->p
[i
].set
);
8797 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8798 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8799 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8800 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8803 isl_multi_union_pw_aff_free(mupa
);
8804 isl_pw_aff_free(pa
);
8807 isl_multi_union_pw_aff_free(mupa
);
8808 isl_pw_aff_free(pa
);
8812 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8813 * with the domain of "pma".
8814 * Furthermore, the dimension of this space needs to be greater than zero,
8815 * unless the dimension of the target space of "pma" is also zero.
8816 * The result is defined over the shared domain of the elements of "mupa"
8818 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8819 __isl_take isl_multi_union_pw_aff
*mupa
,
8820 __isl_take isl_pw_multi_aff
*pma
)
8822 isl_space
*space1
, *space2
;
8823 isl_multi_union_pw_aff
*res
;
8827 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8828 isl_pw_multi_aff_get_space(pma
));
8829 pma
= isl_pw_multi_aff_align_params(pma
,
8830 isl_multi_union_pw_aff_get_space(mupa
));
8834 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8835 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8836 equal
= isl_space_is_equal(space1
, space2
);
8837 isl_space_free(space1
);
8838 isl_space_free(space2
);
8842 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8843 "spaces don't match", goto error
);
8844 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8845 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8846 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8847 "cannot determine domains", goto error
);
8849 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8850 res
= isl_multi_union_pw_aff_alloc(space1
);
8852 for (i
= 0; i
< n_out
; ++i
) {
8854 isl_union_pw_aff
*upa
;
8856 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8857 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8858 isl_multi_union_pw_aff_copy(mupa
), pa
);
8859 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8862 isl_pw_multi_aff_free(pma
);
8863 isl_multi_union_pw_aff_free(mupa
);
8866 isl_multi_union_pw_aff_free(mupa
);
8867 isl_pw_multi_aff_free(pma
);
8871 /* Compute the pullback of "mupa" by the function represented by "upma".
8872 * In other words, plug in "upma" in "mupa". The result contains
8873 * expressions defined over the domain space of "upma".
8875 * Run over all elements of "mupa" and plug in "upma" in each of them.
8877 __isl_give isl_multi_union_pw_aff
*
8878 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8879 __isl_take isl_multi_union_pw_aff
*mupa
,
8880 __isl_take isl_union_pw_multi_aff
*upma
)
8884 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8885 isl_union_pw_multi_aff_get_space(upma
));
8886 upma
= isl_union_pw_multi_aff_align_params(upma
,
8887 isl_multi_union_pw_aff_get_space(mupa
));
8891 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8892 for (i
= 0; i
< n
; ++i
) {
8893 isl_union_pw_aff
*upa
;
8895 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8896 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8897 isl_union_pw_multi_aff_copy(upma
));
8898 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8901 isl_union_pw_multi_aff_free(upma
);
8904 isl_multi_union_pw_aff_free(mupa
);
8905 isl_union_pw_multi_aff_free(upma
);
8909 /* Extract the sequence of elements in "mupa" with domain space "space"
8910 * (ignoring parameters).
8912 * For the elements of "mupa" that are not defined on the specified space,
8913 * the corresponding element in the result is empty.
8915 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8916 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8919 isl_bool equal_params
;
8920 isl_space
*space_mpa
= NULL
;
8921 isl_multi_pw_aff
*mpa
;
8923 if (!mupa
|| !space
)
8926 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8927 equal_params
= isl_space_has_equal_params(space_mpa
, space
);
8928 if (equal_params
< 0)
8930 if (!equal_params
) {
8931 space
= isl_space_drop_dims(space
, isl_dim_param
,
8932 0, isl_space_dim(space
, isl_dim_param
));
8933 space
= isl_space_align_params(space
,
8934 isl_space_copy(space_mpa
));
8938 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8940 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8942 space
= isl_space_from_domain(space
);
8943 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8944 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8945 for (i
= 0; i
< n
; ++i
) {
8946 isl_union_pw_aff
*upa
;
8949 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8950 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8951 isl_space_copy(space
));
8952 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8953 isl_union_pw_aff_free(upa
);
8956 isl_space_free(space
);
8959 isl_space_free(space_mpa
);
8960 isl_space_free(space
);