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_config.h>
35 #include <isl_list_templ.c>
40 #include <isl_list_templ.c>
43 #define BASE union_pw_aff
45 #include <isl_list_templ.c>
48 #define BASE union_pw_multi_aff
50 #include <isl_list_templ.c>
52 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
53 __isl_take isl_vec
*v
)
60 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
70 isl_local_space_free(ls
);
75 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
84 ctx
= isl_local_space_get_ctx(ls
);
85 if (!isl_local_space_divs_known(ls
))
86 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
88 if (!isl_local_space_is_set(ls
))
89 isl_die(ctx
, isl_error_invalid
,
90 "domain of affine expression should be a set",
93 total
= isl_local_space_dim(ls
, isl_dim_all
);
94 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
95 return isl_aff_alloc_vec(ls
, v
);
97 isl_local_space_free(ls
);
101 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
105 aff
= isl_aff_alloc(ls
);
109 isl_int_set_si(aff
->v
->el
[0], 1);
110 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
115 /* Return a piecewise affine expression defined on the specified domain
116 * that is equal to zero.
118 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
120 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
123 /* Return an affine expression defined on the specified domain
124 * that represents NaN.
126 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
130 aff
= isl_aff_alloc(ls
);
134 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
139 /* Return a piecewise affine expression defined on the specified domain
140 * that represents NaN.
142 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
144 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
147 /* Return an affine expression that is equal to "val" on
148 * domain local space "ls".
150 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
151 __isl_take isl_val
*val
)
157 if (!isl_val_is_rat(val
))
158 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
159 "expecting rational value", goto error
);
161 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
165 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
166 isl_int_set(aff
->v
->el
[1], val
->n
);
167 isl_int_set(aff
->v
->el
[0], val
->d
);
169 isl_local_space_free(ls
);
173 isl_local_space_free(ls
);
178 /* Return an affine expression that is equal to the specified dimension
181 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
182 enum isl_dim_type type
, unsigned pos
)
190 space
= isl_local_space_get_space(ls
);
193 if (isl_space_is_map(space
))
194 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
195 "expecting (parameter) set space", goto error
);
196 if (pos
>= isl_local_space_dim(ls
, type
))
197 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
198 "position out of bounds", goto error
);
200 isl_space_free(space
);
201 aff
= isl_aff_alloc(ls
);
205 pos
+= isl_local_space_offset(aff
->ls
, type
);
207 isl_int_set_si(aff
->v
->el
[0], 1);
208 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
209 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
213 isl_local_space_free(ls
);
214 isl_space_free(space
);
218 /* Return a piecewise affine expression that is equal to
219 * the specified dimension in "ls".
221 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
222 enum isl_dim_type type
, unsigned pos
)
224 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
227 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
236 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
241 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
242 isl_vec_copy(aff
->v
));
245 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
253 return isl_aff_dup(aff
);
256 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
264 isl_local_space_free(aff
->ls
);
265 isl_vec_free(aff
->v
);
272 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
274 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
277 /* Return a hash value that digests "aff".
279 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
281 uint32_t hash
, ls_hash
, v_hash
;
286 hash
= isl_hash_init();
287 ls_hash
= isl_local_space_get_hash(aff
->ls
);
288 isl_hash_hash(hash
, ls_hash
);
289 v_hash
= isl_vec_get_hash(aff
->v
);
290 isl_hash_hash(hash
, v_hash
);
295 /* Externally, an isl_aff has a map space, but internally, the
296 * ls field corresponds to the domain of that space.
298 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
302 if (type
== isl_dim_out
)
304 if (type
== isl_dim_in
)
306 return isl_local_space_dim(aff
->ls
, type
);
309 /* Return the position of the dimension of the given type and name
311 * Return -1 if no such dimension can be found.
313 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
318 if (type
== isl_dim_out
)
320 if (type
== isl_dim_in
)
322 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
325 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
327 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
330 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
335 space
= isl_local_space_get_space(aff
->ls
);
336 space
= isl_space_from_domain(space
);
337 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
341 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
342 __isl_keep isl_aff
*aff
)
344 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
347 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
352 ls
= isl_local_space_copy(aff
->ls
);
353 ls
= isl_local_space_from_domain(ls
);
354 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
358 /* Return the local space of the domain of "aff".
359 * This may be either a copy or the local space itself
360 * if there is only one reference to "aff".
361 * This allows the local space to be modified inplace
362 * if both the expression and its local space have only a single reference.
363 * The caller is not allowed to modify "aff" between this call and
364 * a subsequent call to isl_aff_restore_domain_local_space.
365 * The only exception is that isl_aff_free can be called instead.
367 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
368 __isl_keep isl_aff
*aff
)
375 return isl_aff_get_domain_local_space(aff
);
381 /* Set the local space of the domain of "aff" to "ls",
382 * where the local space of "aff" may be missing
383 * due to a preceding call to isl_aff_take_domain_local_space.
384 * However, in this case, "aff" only has a single reference and
385 * then the call to isl_aff_cow has no effect.
387 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
388 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
394 isl_local_space_free(ls
);
398 aff
= isl_aff_cow(aff
);
401 isl_local_space_free(aff
->ls
);
407 isl_local_space_free(ls
);
411 /* Externally, an isl_aff has a map space, but internally, the
412 * ls field corresponds to the domain of that space.
414 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
415 enum isl_dim_type type
, unsigned pos
)
419 if (type
== isl_dim_out
)
421 if (type
== isl_dim_in
)
423 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
426 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
427 __isl_take isl_space
*dim
)
429 aff
= isl_aff_cow(aff
);
433 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
435 return isl_aff_free(aff
);
444 /* Reset the space of "aff". This function is called from isl_pw_templ.c
445 * and doesn't know if the space of an element object is represented
446 * directly or through its domain. It therefore passes along both.
448 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
449 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
451 isl_space_free(space
);
452 return isl_aff_reset_domain_space(aff
, domain
);
455 /* Reorder the coefficients of the affine expression based
456 * on the given reordering.
457 * The reordering r is assumed to have been extended with the local
460 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
461 __isl_take isl_reordering
*r
, int n_div
)
469 res
= isl_vec_alloc(vec
->ctx
,
470 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
473 isl_seq_cpy(res
->el
, vec
->el
, 2);
474 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
475 for (i
= 0; i
< r
->len
; ++i
)
476 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
478 isl_reordering_free(r
);
483 isl_reordering_free(r
);
487 /* Reorder the dimensions of the domain of "aff" according
488 * to the given reordering.
490 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
491 __isl_take isl_reordering
*r
)
493 aff
= isl_aff_cow(aff
);
497 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
498 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
499 aff
->ls
->div
->n_row
);
500 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
502 if (!aff
->v
|| !aff
->ls
)
503 return isl_aff_free(aff
);
508 isl_reordering_free(r
);
512 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
513 __isl_take isl_space
*model
)
515 isl_bool equal_params
;
520 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
521 if (equal_params
< 0)
526 model
= isl_space_drop_dims(model
, isl_dim_in
,
527 0, isl_space_dim(model
, isl_dim_in
));
528 model
= isl_space_drop_dims(model
, isl_dim_out
,
529 0, isl_space_dim(model
, isl_dim_out
));
530 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
531 exp
= isl_reordering_extend_space(exp
,
532 isl_aff_get_domain_space(aff
));
533 aff
= isl_aff_realign_domain(aff
, exp
);
536 isl_space_free(model
);
539 isl_space_free(model
);
544 /* Is "aff" obviously equal to zero?
546 * If the denominator is zero, then "aff" is not equal to zero.
548 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
551 return isl_bool_error
;
553 if (isl_int_is_zero(aff
->v
->el
[0]))
554 return isl_bool_false
;
555 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
558 /* Does "aff" represent NaN?
560 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
563 return isl_bool_error
;
565 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
568 /* Are "aff1" and "aff2" obviously equal?
570 * NaN is not equal to anything, not even to another NaN.
572 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
573 __isl_keep isl_aff
*aff2
)
578 return isl_bool_error
;
580 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
581 return isl_bool_false
;
583 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
584 if (equal
< 0 || !equal
)
587 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
590 /* Return the common denominator of "aff" in "v".
592 * We cannot return anything meaningful in case of a NaN.
594 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
597 return isl_stat_error
;
598 if (isl_aff_is_nan(aff
))
599 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
600 "cannot get denominator of NaN", return isl_stat_error
);
601 isl_int_set(*v
, aff
->v
->el
[0]);
605 /* Return the common denominator of "aff".
607 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
614 ctx
= isl_aff_get_ctx(aff
);
615 if (isl_aff_is_nan(aff
))
616 return isl_val_nan(ctx
);
617 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
620 /* Return the constant term of "aff".
622 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
630 ctx
= isl_aff_get_ctx(aff
);
631 if (isl_aff_is_nan(aff
))
632 return isl_val_nan(ctx
);
633 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
634 return isl_val_normalize(v
);
637 /* Return the coefficient of the variable of type "type" at position "pos"
640 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
641 enum isl_dim_type type
, int pos
)
649 ctx
= isl_aff_get_ctx(aff
);
650 if (type
== isl_dim_out
)
651 isl_die(ctx
, isl_error_invalid
,
652 "output/set dimension does not have a coefficient",
654 if (type
== isl_dim_in
)
657 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
658 isl_die(ctx
, isl_error_invalid
,
659 "position out of bounds", return NULL
);
661 if (isl_aff_is_nan(aff
))
662 return isl_val_nan(ctx
);
663 pos
+= isl_local_space_offset(aff
->ls
, type
);
664 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
665 return isl_val_normalize(v
);
668 /* Return the sign of the coefficient of the variable of type "type"
669 * at position "pos" of "aff".
671 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
679 ctx
= isl_aff_get_ctx(aff
);
680 if (type
== isl_dim_out
)
681 isl_die(ctx
, isl_error_invalid
,
682 "output/set dimension does not have a coefficient",
684 if (type
== isl_dim_in
)
687 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
688 isl_die(ctx
, isl_error_invalid
,
689 "position out of bounds", return 0);
691 pos
+= isl_local_space_offset(aff
->ls
, type
);
692 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
695 /* Replace the numerator of the constant term of "aff" by "v".
697 * A NaN is unaffected by this operation.
699 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
703 if (isl_aff_is_nan(aff
))
705 aff
= isl_aff_cow(aff
);
709 aff
->v
= isl_vec_cow(aff
->v
);
711 return isl_aff_free(aff
);
713 isl_int_set(aff
->v
->el
[1], v
);
718 /* Replace the constant term of "aff" by "v".
720 * A NaN is unaffected by this operation.
722 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
723 __isl_take isl_val
*v
)
728 if (isl_aff_is_nan(aff
)) {
733 if (!isl_val_is_rat(v
))
734 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
735 "expecting rational value", goto error
);
737 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
738 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
743 aff
= isl_aff_cow(aff
);
746 aff
->v
= isl_vec_cow(aff
->v
);
750 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
751 isl_int_set(aff
->v
->el
[1], v
->n
);
752 } else if (isl_int_is_one(v
->d
)) {
753 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
755 isl_seq_scale(aff
->v
->el
+ 1,
756 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
757 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
758 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
759 aff
->v
= isl_vec_normalize(aff
->v
);
772 /* Add "v" to the constant term of "aff".
774 * A NaN is unaffected by this operation.
776 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
778 if (isl_int_is_zero(v
))
783 if (isl_aff_is_nan(aff
))
785 aff
= isl_aff_cow(aff
);
789 aff
->v
= isl_vec_cow(aff
->v
);
791 return isl_aff_free(aff
);
793 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
798 /* Add "v" to the constant term of "aff".
800 * A NaN is unaffected by this operation.
802 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
803 __isl_take isl_val
*v
)
808 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
813 if (!isl_val_is_rat(v
))
814 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
815 "expecting rational value", goto error
);
817 aff
= isl_aff_cow(aff
);
821 aff
->v
= isl_vec_cow(aff
->v
);
825 if (isl_int_is_one(v
->d
)) {
826 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
827 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
828 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
829 aff
->v
= isl_vec_normalize(aff
->v
);
833 isl_seq_scale(aff
->v
->el
+ 1,
834 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
835 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
836 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
837 aff
->v
= isl_vec_normalize(aff
->v
);
850 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
855 isl_int_set_si(t
, v
);
856 aff
= isl_aff_add_constant(aff
, t
);
862 /* Add "v" to the numerator of the constant term of "aff".
864 * A NaN is unaffected by this operation.
866 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
868 if (isl_int_is_zero(v
))
873 if (isl_aff_is_nan(aff
))
875 aff
= isl_aff_cow(aff
);
879 aff
->v
= isl_vec_cow(aff
->v
);
881 return isl_aff_free(aff
);
883 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
888 /* Add "v" to the numerator of the constant term of "aff".
890 * A NaN is unaffected by this operation.
892 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
900 isl_int_set_si(t
, v
);
901 aff
= isl_aff_add_constant_num(aff
, t
);
907 /* Replace the numerator of the constant term of "aff" by "v".
909 * A NaN is unaffected by this operation.
911 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
915 if (isl_aff_is_nan(aff
))
917 aff
= isl_aff_cow(aff
);
921 aff
->v
= isl_vec_cow(aff
->v
);
923 return isl_aff_free(aff
);
925 isl_int_set_si(aff
->v
->el
[1], v
);
930 /* Replace the numerator of the coefficient of the variable of type "type"
931 * at position "pos" of "aff" by "v".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
936 enum isl_dim_type type
, int pos
, isl_int v
)
941 if (type
== isl_dim_out
)
942 isl_die(aff
->v
->ctx
, isl_error_invalid
,
943 "output/set dimension does not have a coefficient",
944 return isl_aff_free(aff
));
945 if (type
== isl_dim_in
)
948 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
949 isl_die(aff
->v
->ctx
, isl_error_invalid
,
950 "position out of bounds", return isl_aff_free(aff
));
952 if (isl_aff_is_nan(aff
))
954 aff
= isl_aff_cow(aff
);
958 aff
->v
= isl_vec_cow(aff
->v
);
960 return isl_aff_free(aff
);
962 pos
+= isl_local_space_offset(aff
->ls
, type
);
963 isl_int_set(aff
->v
->el
[1 + pos
], v
);
968 /* Replace the numerator of the coefficient of the variable of type "type"
969 * at position "pos" of "aff" by "v".
971 * A NaN is unaffected by this operation.
973 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
974 enum isl_dim_type type
, int pos
, int v
)
979 if (type
== isl_dim_out
)
980 isl_die(aff
->v
->ctx
, isl_error_invalid
,
981 "output/set dimension does not have a coefficient",
982 return isl_aff_free(aff
));
983 if (type
== isl_dim_in
)
986 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
987 isl_die(aff
->v
->ctx
, isl_error_invalid
,
988 "position out of bounds", return isl_aff_free(aff
));
990 if (isl_aff_is_nan(aff
))
992 pos
+= isl_local_space_offset(aff
->ls
, type
);
993 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
996 aff
= isl_aff_cow(aff
);
1000 aff
->v
= isl_vec_cow(aff
->v
);
1002 return isl_aff_free(aff
);
1004 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1009 /* Replace the coefficient of the variable of type "type" at position "pos"
1012 * A NaN is unaffected by this operation.
1014 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1015 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1020 if (type
== isl_dim_out
)
1021 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1022 "output/set dimension does not have a coefficient",
1024 if (type
== isl_dim_in
)
1027 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1028 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1029 "position out of bounds", goto error
);
1031 if (isl_aff_is_nan(aff
)) {
1035 if (!isl_val_is_rat(v
))
1036 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1037 "expecting rational value", goto error
);
1039 pos
+= isl_local_space_offset(aff
->ls
, type
);
1040 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1041 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1046 aff
= isl_aff_cow(aff
);
1049 aff
->v
= isl_vec_cow(aff
->v
);
1053 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1054 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1055 } else if (isl_int_is_one(v
->d
)) {
1056 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1058 isl_seq_scale(aff
->v
->el
+ 1,
1059 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1060 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1061 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1062 aff
->v
= isl_vec_normalize(aff
->v
);
1075 /* Add "v" to the coefficient of the variable of type "type"
1076 * at position "pos" of "aff".
1078 * A NaN is unaffected by this operation.
1080 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1081 enum isl_dim_type type
, int pos
, isl_int v
)
1086 if (type
== isl_dim_out
)
1087 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1088 "output/set dimension does not have a coefficient",
1089 return isl_aff_free(aff
));
1090 if (type
== isl_dim_in
)
1093 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1094 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1095 "position out of bounds", return isl_aff_free(aff
));
1097 if (isl_aff_is_nan(aff
))
1099 aff
= isl_aff_cow(aff
);
1103 aff
->v
= isl_vec_cow(aff
->v
);
1105 return isl_aff_free(aff
);
1107 pos
+= isl_local_space_offset(aff
->ls
, type
);
1108 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1113 /* Add "v" to the coefficient of the variable of type "type"
1114 * at position "pos" of "aff".
1116 * A NaN is unaffected by this operation.
1118 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1119 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1124 if (isl_val_is_zero(v
)) {
1129 if (type
== isl_dim_out
)
1130 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1131 "output/set dimension does not have a coefficient",
1133 if (type
== isl_dim_in
)
1136 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1137 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1138 "position out of bounds", goto error
);
1140 if (isl_aff_is_nan(aff
)) {
1144 if (!isl_val_is_rat(v
))
1145 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1146 "expecting rational value", goto error
);
1148 aff
= isl_aff_cow(aff
);
1152 aff
->v
= isl_vec_cow(aff
->v
);
1156 pos
+= isl_local_space_offset(aff
->ls
, type
);
1157 if (isl_int_is_one(v
->d
)) {
1158 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1159 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1160 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1161 aff
->v
= isl_vec_normalize(aff
->v
);
1165 isl_seq_scale(aff
->v
->el
+ 1,
1166 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1167 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1168 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1169 aff
->v
= isl_vec_normalize(aff
->v
);
1182 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1183 enum isl_dim_type type
, int pos
, int v
)
1188 isl_int_set_si(t
, v
);
1189 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1195 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1200 return isl_local_space_get_div(aff
->ls
, pos
);
1203 /* Return the negation of "aff".
1205 * As a special case, -NaN = NaN.
1207 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1211 if (isl_aff_is_nan(aff
))
1213 aff
= isl_aff_cow(aff
);
1216 aff
->v
= isl_vec_cow(aff
->v
);
1218 return isl_aff_free(aff
);
1220 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1225 /* Remove divs from the local space that do not appear in the affine
1227 * We currently only remove divs at the end.
1228 * Some intermediate divs may also not appear directly in the affine
1229 * expression, but we would also need to check that no other divs are
1230 * defined in terms of them.
1232 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1241 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1242 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1244 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1248 aff
= isl_aff_cow(aff
);
1252 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1253 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1254 if (!aff
->ls
|| !aff
->v
)
1255 return isl_aff_free(aff
);
1260 /* Look for any divs in the aff->ls with a denominator equal to one
1261 * and plug them into the affine expression and any subsequent divs
1262 * that may reference the div.
1264 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1270 isl_local_space
*ls
;
1276 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1278 for (i
= 0; i
< n
; ++i
) {
1279 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1281 ls
= isl_local_space_copy(aff
->ls
);
1282 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1283 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1284 vec
= isl_vec_copy(aff
->v
);
1285 vec
= isl_vec_cow(vec
);
1291 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1292 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1297 isl_vec_free(aff
->v
);
1299 isl_local_space_free(aff
->ls
);
1306 isl_local_space_free(ls
);
1307 return isl_aff_free(aff
);
1310 /* Look for any divs j that appear with a unit coefficient inside
1311 * the definitions of other divs i and plug them into the definitions
1314 * In particular, an expression of the form
1316 * floor((f(..) + floor(g(..)/n))/m)
1320 * floor((n * f(..) + g(..))/(n * m))
1322 * This simplification is correct because we can move the expression
1323 * f(..) into the inner floor in the original expression to obtain
1325 * floor(floor((n * f(..) + g(..))/n)/m)
1327 * from which we can derive the simplified expression.
1329 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1337 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1338 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1339 for (i
= 1; i
< n
; ++i
) {
1340 for (j
= 0; j
< i
; ++j
) {
1341 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1343 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1344 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1345 aff
->v
->size
, i
, 1);
1347 return isl_aff_free(aff
);
1354 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1356 * Even though this function is only called on isl_affs with a single
1357 * reference, we are careful to only change aff->v and aff->ls together.
1359 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1361 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1362 isl_local_space
*ls
;
1365 ls
= isl_local_space_copy(aff
->ls
);
1366 ls
= isl_local_space_swap_div(ls
, a
, b
);
1367 v
= isl_vec_copy(aff
->v
);
1372 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1373 isl_vec_free(aff
->v
);
1375 isl_local_space_free(aff
->ls
);
1381 isl_local_space_free(ls
);
1382 return isl_aff_free(aff
);
1385 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1387 * We currently do not actually remove div "b", but simply add its
1388 * coefficient to that of "a" and then zero it out.
1390 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1392 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1394 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1397 aff
->v
= isl_vec_cow(aff
->v
);
1399 return isl_aff_free(aff
);
1401 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1402 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1403 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1408 /* Sort the divs in the local space of "aff" according to
1409 * the comparison function "cmp_row" in isl_local_space.c,
1410 * combining the coefficients of identical divs.
1412 * Reordering divs does not change the semantics of "aff",
1413 * so there is no need to call isl_aff_cow.
1414 * Moreover, this function is currently only called on isl_affs
1415 * with a single reference.
1417 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1424 n
= isl_aff_dim(aff
, isl_dim_div
);
1425 for (i
= 1; i
< n
; ++i
) {
1426 for (j
= i
- 1; j
>= 0; --j
) {
1427 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1431 aff
= merge_divs(aff
, j
, j
+ 1);
1433 aff
= swap_div(aff
, j
, j
+ 1);
1442 /* Normalize the representation of "aff".
1444 * This function should only be called of "new" isl_affs, i.e.,
1445 * with only a single reference. We therefore do not need to
1446 * worry about affecting other instances.
1448 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1452 aff
->v
= isl_vec_normalize(aff
->v
);
1454 return isl_aff_free(aff
);
1455 aff
= plug_in_integral_divs(aff
);
1456 aff
= plug_in_unit_divs(aff
);
1457 aff
= sort_divs(aff
);
1458 aff
= isl_aff_remove_unused_divs(aff
);
1462 /* Given f, return floor(f).
1463 * If f is an integer expression, then just return f.
1464 * If f is a constant, then return the constant floor(f).
1465 * Otherwise, if f = g/m, write g = q m + r,
1466 * create a new div d = [r/m] and return the expression q + d.
1467 * The coefficients in r are taken to lie between -m/2 and m/2.
1469 * reduce_div_coefficients performs the same normalization.
1471 * As a special case, floor(NaN) = NaN.
1473 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1483 if (isl_aff_is_nan(aff
))
1485 if (isl_int_is_one(aff
->v
->el
[0]))
1488 aff
= isl_aff_cow(aff
);
1492 aff
->v
= isl_vec_cow(aff
->v
);
1494 return isl_aff_free(aff
);
1496 if (isl_aff_is_cst(aff
)) {
1497 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1498 isl_int_set_si(aff
->v
->el
[0], 1);
1502 div
= isl_vec_copy(aff
->v
);
1503 div
= isl_vec_cow(div
);
1505 return isl_aff_free(aff
);
1507 ctx
= isl_aff_get_ctx(aff
);
1508 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1509 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1510 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1511 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1512 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1513 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1514 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1518 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1520 return isl_aff_free(aff
);
1522 size
= aff
->v
->size
;
1523 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1525 return isl_aff_free(aff
);
1526 isl_int_set_si(aff
->v
->el
[0], 1);
1527 isl_int_set_si(aff
->v
->el
[size
], 1);
1529 aff
= isl_aff_normalize(aff
);
1536 * aff mod m = aff - m * floor(aff/m)
1538 * with m an integer value.
1540 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1541 __isl_take isl_val
*m
)
1548 if (!isl_val_is_int(m
))
1549 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1550 "expecting integer modulo", goto error
);
1552 res
= isl_aff_copy(aff
);
1553 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1554 aff
= isl_aff_floor(aff
);
1555 aff
= isl_aff_scale_val(aff
, m
);
1556 res
= isl_aff_sub(res
, aff
);
1567 * pwaff mod m = pwaff - m * floor(pwaff/m)
1569 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1573 res
= isl_pw_aff_copy(pwaff
);
1574 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1575 pwaff
= isl_pw_aff_floor(pwaff
);
1576 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1577 res
= isl_pw_aff_sub(res
, pwaff
);
1584 * pa mod m = pa - m * floor(pa/m)
1586 * with m an integer value.
1588 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1589 __isl_take isl_val
*m
)
1593 if (!isl_val_is_int(m
))
1594 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1595 "expecting integer modulo", goto error
);
1596 pa
= isl_pw_aff_mod(pa
, m
->n
);
1600 isl_pw_aff_free(pa
);
1605 /* Given f, return ceil(f).
1606 * If f is an integer expression, then just return f.
1607 * Otherwise, let f be the expression
1613 * floor((e + m - 1)/m)
1615 * As a special case, ceil(NaN) = NaN.
1617 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1622 if (isl_aff_is_nan(aff
))
1624 if (isl_int_is_one(aff
->v
->el
[0]))
1627 aff
= isl_aff_cow(aff
);
1630 aff
->v
= isl_vec_cow(aff
->v
);
1632 return isl_aff_free(aff
);
1634 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1635 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1636 aff
= isl_aff_floor(aff
);
1641 /* Apply the expansion computed by isl_merge_divs.
1642 * The expansion itself is given by "exp" while the resulting
1643 * list of divs is given by "div".
1645 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1646 __isl_take isl_mat
*div
, int *exp
)
1652 aff
= isl_aff_cow(aff
);
1656 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1657 new_n_div
= isl_mat_rows(div
);
1658 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1660 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1661 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1662 if (!aff
->v
|| !aff
->ls
)
1663 return isl_aff_free(aff
);
1671 /* Add two affine expressions that live in the same local space.
1673 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1674 __isl_take isl_aff
*aff2
)
1678 aff1
= isl_aff_cow(aff1
);
1682 aff1
->v
= isl_vec_cow(aff1
->v
);
1688 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1689 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1690 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1691 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1692 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1693 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1694 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1706 /* Return the sum of "aff1" and "aff2".
1708 * If either of the two is NaN, then the result is NaN.
1710 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1711 __isl_take isl_aff
*aff2
)
1722 ctx
= isl_aff_get_ctx(aff1
);
1723 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1724 isl_die(ctx
, isl_error_invalid
,
1725 "spaces don't match", goto error
);
1727 if (isl_aff_is_nan(aff1
)) {
1731 if (isl_aff_is_nan(aff2
)) {
1736 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1737 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1738 if (n_div1
== 0 && n_div2
== 0)
1739 return add_expanded(aff1
, aff2
);
1741 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1742 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1743 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1746 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1747 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1748 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1752 return add_expanded(aff1
, aff2
);
1761 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1762 __isl_take isl_aff
*aff2
)
1764 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1767 /* Return the result of scaling "aff" by a factor of "f".
1769 * As a special case, f * NaN = NaN.
1771 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1777 if (isl_aff_is_nan(aff
))
1780 if (isl_int_is_one(f
))
1783 aff
= isl_aff_cow(aff
);
1786 aff
->v
= isl_vec_cow(aff
->v
);
1788 return isl_aff_free(aff
);
1790 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1791 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1796 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1797 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1798 isl_int_divexact(gcd
, f
, gcd
);
1799 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1805 /* Multiple "aff" by "v".
1807 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1808 __isl_take isl_val
*v
)
1813 if (isl_val_is_one(v
)) {
1818 if (!isl_val_is_rat(v
))
1819 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1820 "expecting rational factor", goto error
);
1822 aff
= isl_aff_scale(aff
, v
->n
);
1823 aff
= isl_aff_scale_down(aff
, v
->d
);
1833 /* Return the result of scaling "aff" down by a factor of "f".
1835 * As a special case, NaN/f = NaN.
1837 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1843 if (isl_aff_is_nan(aff
))
1846 if (isl_int_is_one(f
))
1849 aff
= isl_aff_cow(aff
);
1853 if (isl_int_is_zero(f
))
1854 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1855 "cannot scale down by zero", return isl_aff_free(aff
));
1857 aff
->v
= isl_vec_cow(aff
->v
);
1859 return isl_aff_free(aff
);
1862 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1863 isl_int_gcd(gcd
, gcd
, f
);
1864 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1865 isl_int_divexact(gcd
, f
, gcd
);
1866 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1872 /* Divide "aff" by "v".
1874 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1875 __isl_take isl_val
*v
)
1880 if (isl_val_is_one(v
)) {
1885 if (!isl_val_is_rat(v
))
1886 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1887 "expecting rational factor", goto error
);
1888 if (!isl_val_is_pos(v
))
1889 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1890 "factor needs to be positive", goto error
);
1892 aff
= isl_aff_scale(aff
, v
->d
);
1893 aff
= isl_aff_scale_down(aff
, v
->n
);
1903 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1911 isl_int_set_ui(v
, f
);
1912 aff
= isl_aff_scale_down(aff
, v
);
1918 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1919 enum isl_dim_type type
, unsigned pos
, const char *s
)
1921 aff
= isl_aff_cow(aff
);
1924 if (type
== isl_dim_out
)
1925 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1926 "cannot set name of output/set dimension",
1927 return isl_aff_free(aff
));
1928 if (type
== isl_dim_in
)
1930 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1932 return isl_aff_free(aff
);
1937 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1938 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1940 aff
= isl_aff_cow(aff
);
1943 if (type
== isl_dim_out
)
1944 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1945 "cannot set name of output/set dimension",
1947 if (type
== isl_dim_in
)
1949 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1951 return isl_aff_free(aff
);
1960 /* Replace the identifier of the input tuple of "aff" by "id".
1961 * type is currently required to be equal to isl_dim_in
1963 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1964 enum isl_dim_type type
, __isl_take isl_id
*id
)
1966 aff
= isl_aff_cow(aff
);
1969 if (type
!= isl_dim_out
)
1970 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1971 "cannot only set id of input tuple", goto error
);
1972 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
1974 return isl_aff_free(aff
);
1983 /* Exploit the equalities in "eq" to simplify the affine expression
1984 * and the expressions of the integer divisions in the local space.
1985 * The integer divisions in this local space are assumed to appear
1986 * as regular dimensions in "eq".
1988 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1989 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1997 if (eq
->n_eq
== 0) {
1998 isl_basic_set_free(eq
);
2002 aff
= isl_aff_cow(aff
);
2006 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2007 isl_basic_set_copy(eq
));
2008 aff
->v
= isl_vec_cow(aff
->v
);
2009 if (!aff
->ls
|| !aff
->v
)
2012 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2014 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2015 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2016 if (j
< 0 || j
== 0 || j
>= total
)
2019 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2023 isl_basic_set_free(eq
);
2024 aff
= isl_aff_normalize(aff
);
2027 isl_basic_set_free(eq
);
2032 /* Exploit the equalities in "eq" to simplify the affine expression
2033 * and the expressions of the integer divisions in the local space.
2035 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2036 __isl_take isl_basic_set
*eq
)
2042 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2044 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2045 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2047 isl_basic_set_free(eq
);
2052 /* Look for equalities among the variables shared by context and aff
2053 * and the integer divisions of aff, if any.
2054 * The equalities are then used to eliminate coefficients and/or integer
2055 * divisions from aff.
2057 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2058 __isl_take isl_set
*context
)
2060 isl_basic_set
*hull
;
2065 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2067 isl_basic_set
*bset
;
2068 isl_local_space
*ls
;
2069 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2070 ls
= isl_aff_get_domain_local_space(aff
);
2071 bset
= isl_basic_set_from_local_space(ls
);
2072 bset
= isl_basic_set_lift(bset
);
2073 bset
= isl_basic_set_flatten(bset
);
2074 context
= isl_set_intersect(context
,
2075 isl_set_from_basic_set(bset
));
2078 hull
= isl_set_affine_hull(context
);
2079 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2082 isl_set_free(context
);
2086 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2087 __isl_take isl_set
*context
)
2089 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2090 dom_context
= isl_set_intersect_params(dom_context
, context
);
2091 return isl_aff_gist(aff
, dom_context
);
2094 /* Return a basic set containing those elements in the space
2095 * of aff where it is positive. "rational" should not be set.
2097 * If "aff" is NaN, then it is not positive.
2099 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2102 isl_constraint
*ineq
;
2103 isl_basic_set
*bset
;
2108 if (isl_aff_is_nan(aff
)) {
2109 isl_space
*space
= isl_aff_get_domain_space(aff
);
2111 return isl_basic_set_empty(space
);
2114 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2115 "rational sets not supported", goto error
);
2117 ineq
= isl_inequality_from_aff(aff
);
2118 c
= isl_constraint_get_constant_val(ineq
);
2119 c
= isl_val_sub_ui(c
, 1);
2120 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2122 bset
= isl_basic_set_from_constraint(ineq
);
2123 bset
= isl_basic_set_simplify(bset
);
2130 /* Return a basic set containing those elements in the space
2131 * of aff where it is non-negative.
2132 * If "rational" is set, then return a rational basic set.
2134 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2136 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2137 __isl_take isl_aff
*aff
, int rational
)
2139 isl_constraint
*ineq
;
2140 isl_basic_set
*bset
;
2144 if (isl_aff_is_nan(aff
)) {
2145 isl_space
*space
= isl_aff_get_domain_space(aff
);
2147 return isl_basic_set_empty(space
);
2150 ineq
= isl_inequality_from_aff(aff
);
2152 bset
= isl_basic_set_from_constraint(ineq
);
2154 bset
= isl_basic_set_set_rational(bset
);
2155 bset
= isl_basic_set_simplify(bset
);
2159 /* Return a basic set containing those elements in the space
2160 * of aff where it is non-negative.
2162 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2164 return aff_nonneg_basic_set(aff
, 0);
2167 /* Return a basic set containing those elements in the domain space
2168 * of "aff" where it is positive.
2170 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2172 aff
= isl_aff_add_constant_num_si(aff
, -1);
2173 return isl_aff_nonneg_basic_set(aff
);
2176 /* Return a basic set containing those elements in the domain space
2177 * of aff where it is negative.
2179 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2181 aff
= isl_aff_neg(aff
);
2182 return isl_aff_pos_basic_set(aff
);
2185 /* Return a basic set containing those elements in the space
2186 * of aff where it is zero.
2187 * If "rational" is set, then return a rational basic set.
2189 * If "aff" is NaN, then it is not zero.
2191 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2194 isl_constraint
*ineq
;
2195 isl_basic_set
*bset
;
2199 if (isl_aff_is_nan(aff
)) {
2200 isl_space
*space
= isl_aff_get_domain_space(aff
);
2202 return isl_basic_set_empty(space
);
2205 ineq
= isl_equality_from_aff(aff
);
2207 bset
= isl_basic_set_from_constraint(ineq
);
2209 bset
= isl_basic_set_set_rational(bset
);
2210 bset
= isl_basic_set_simplify(bset
);
2214 /* Return a basic set containing those elements in the space
2215 * of aff where it is zero.
2217 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2219 return aff_zero_basic_set(aff
, 0);
2222 /* Return a basic set containing those elements in the shared space
2223 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2225 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2226 __isl_take isl_aff
*aff2
)
2228 aff1
= isl_aff_sub(aff1
, aff2
);
2230 return isl_aff_nonneg_basic_set(aff1
);
2233 /* Return a basic set containing those elements in the shared domain space
2234 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2236 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2237 __isl_take isl_aff
*aff2
)
2239 aff1
= isl_aff_sub(aff1
, aff2
);
2241 return isl_aff_pos_basic_set(aff1
);
2244 /* Return a set containing those elements in the shared space
2245 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2247 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2248 __isl_take isl_aff
*aff2
)
2250 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2253 /* Return a set containing those elements in the shared domain space
2254 * of aff1 and aff2 where aff1 is greater than aff2.
2256 * If either of the two inputs is NaN, then the result is empty,
2257 * as comparisons with NaN always return false.
2259 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2260 __isl_take isl_aff
*aff2
)
2262 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2265 /* Return a basic set containing those elements in the shared space
2266 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2268 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2269 __isl_take isl_aff
*aff2
)
2271 return isl_aff_ge_basic_set(aff2
, aff1
);
2274 /* Return a basic set containing those elements in the shared domain space
2275 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2277 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2278 __isl_take isl_aff
*aff2
)
2280 return isl_aff_gt_basic_set(aff2
, aff1
);
2283 /* Return a set containing those elements in the shared space
2284 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2286 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2287 __isl_take isl_aff
*aff2
)
2289 return isl_aff_ge_set(aff2
, aff1
);
2292 /* Return a set containing those elements in the shared domain space
2293 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2295 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2296 __isl_take isl_aff
*aff2
)
2298 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2301 /* Return a basic set containing those elements in the shared space
2302 * of aff1 and aff2 where aff1 and aff2 are equal.
2304 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2305 __isl_take isl_aff
*aff2
)
2307 aff1
= isl_aff_sub(aff1
, aff2
);
2309 return isl_aff_zero_basic_set(aff1
);
2312 /* Return a set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 and aff2 are equal.
2315 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2316 __isl_take isl_aff
*aff2
)
2318 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2321 /* Return a set containing those elements in the shared domain space
2322 * of aff1 and aff2 where aff1 and aff2 are not equal.
2324 * If either of the two inputs is NaN, then the result is empty,
2325 * as comparisons with NaN always return false.
2327 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2328 __isl_take isl_aff
*aff2
)
2330 isl_set
*set_lt
, *set_gt
;
2332 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2333 isl_aff_copy(aff2
));
2334 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2335 return isl_set_union_disjoint(set_lt
, set_gt
);
2338 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2339 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2341 aff1
= isl_aff_add(aff1
, aff2
);
2342 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2346 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2354 /* Check whether the given affine expression has non-zero coefficient
2355 * for any dimension in the given range or if any of these dimensions
2356 * appear with non-zero coefficients in any of the integer divisions
2357 * involved in the affine expression.
2359 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2360 enum isl_dim_type type
, unsigned first
, unsigned n
)
2365 isl_bool involves
= isl_bool_false
;
2368 return isl_bool_error
;
2370 return isl_bool_false
;
2372 ctx
= isl_aff_get_ctx(aff
);
2373 if (first
+ n
> isl_aff_dim(aff
, type
))
2374 isl_die(ctx
, isl_error_invalid
,
2375 "range out of bounds", return isl_bool_error
);
2377 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2381 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2382 for (i
= 0; i
< n
; ++i
)
2383 if (active
[first
+ i
]) {
2384 involves
= isl_bool_true
;
2393 return isl_bool_error
;
2396 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2397 enum isl_dim_type type
, unsigned first
, unsigned n
)
2403 if (type
== isl_dim_out
)
2404 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2405 "cannot drop output/set dimension",
2406 return isl_aff_free(aff
));
2407 if (type
== isl_dim_in
)
2409 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2412 ctx
= isl_aff_get_ctx(aff
);
2413 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2414 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2415 return isl_aff_free(aff
));
2417 aff
= isl_aff_cow(aff
);
2421 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2423 return isl_aff_free(aff
);
2425 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2426 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2428 return isl_aff_free(aff
);
2433 /* Project the domain of the affine expression onto its parameter space.
2434 * The affine expression may not involve any of the domain dimensions.
2436 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2442 n
= isl_aff_dim(aff
, isl_dim_in
);
2443 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2445 return isl_aff_free(aff
);
2447 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2448 "affine expression involves some of the domain dimensions",
2449 return isl_aff_free(aff
));
2450 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2451 space
= isl_aff_get_domain_space(aff
);
2452 space
= isl_space_params(space
);
2453 aff
= isl_aff_reset_domain_space(aff
, space
);
2457 /* Convert an affine expression defined over a parameter domain
2458 * into one that is defined over a zero-dimensional set.
2460 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2462 isl_local_space
*ls
;
2464 ls
= isl_aff_take_domain_local_space(aff
);
2465 ls
= isl_local_space_set_from_params(ls
);
2466 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2471 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2472 enum isl_dim_type type
, unsigned first
, unsigned n
)
2478 if (type
== isl_dim_out
)
2479 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2480 "cannot insert output/set dimensions",
2481 return isl_aff_free(aff
));
2482 if (type
== isl_dim_in
)
2484 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2487 ctx
= isl_aff_get_ctx(aff
);
2488 if (first
> isl_local_space_dim(aff
->ls
, type
))
2489 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2490 return isl_aff_free(aff
));
2492 aff
= isl_aff_cow(aff
);
2496 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2498 return isl_aff_free(aff
);
2500 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2501 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2503 return isl_aff_free(aff
);
2508 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2509 enum isl_dim_type type
, unsigned n
)
2513 pos
= isl_aff_dim(aff
, type
);
2515 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2518 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2519 enum isl_dim_type type
, unsigned n
)
2523 pos
= isl_pw_aff_dim(pwaff
, type
);
2525 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2528 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2529 * to dimensions of "dst_type" at "dst_pos".
2531 * We only support moving input dimensions to parameters and vice versa.
2533 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2534 enum isl_dim_type dst_type
, unsigned dst_pos
,
2535 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2543 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2544 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2547 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2548 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2549 "cannot move output/set dimension",
2550 return isl_aff_free(aff
));
2551 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2552 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2553 "cannot move divs", return isl_aff_free(aff
));
2554 if (dst_type
== isl_dim_in
)
2555 dst_type
= isl_dim_set
;
2556 if (src_type
== isl_dim_in
)
2557 src_type
= isl_dim_set
;
2559 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2560 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2561 "range out of bounds", return isl_aff_free(aff
));
2562 if (dst_type
== src_type
)
2563 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2564 "moving dims within the same type not supported",
2565 return isl_aff_free(aff
));
2567 aff
= isl_aff_cow(aff
);
2571 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2572 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2573 if (dst_type
> src_type
)
2576 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2577 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2578 src_type
, src_pos
, n
);
2579 if (!aff
->v
|| !aff
->ls
)
2580 return isl_aff_free(aff
);
2582 aff
= sort_divs(aff
);
2587 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2589 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2590 return isl_pw_aff_alloc(dom
, aff
);
2593 #define isl_aff_involves_nan isl_aff_is_nan
2596 #define PW isl_pw_aff
2600 #define EL_IS_ZERO is_empty
2604 #define IS_ZERO is_empty
2607 #undef DEFAULT_IS_ZERO
2608 #define DEFAULT_IS_ZERO 0
2615 #include <isl_pw_templ.c>
2616 #include <isl_pw_hash.c>
2617 #include <isl_pw_union_opt.c>
2620 #define UNION isl_union_pw_aff
2622 #define PART isl_pw_aff
2624 #define PARTS pw_aff
2626 #include <isl_union_single.c>
2627 #include <isl_union_neg.c>
2629 static __isl_give isl_set
*align_params_pw_pw_set_and(
2630 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2631 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2632 __isl_take isl_pw_aff
*pwaff2
))
2634 isl_bool equal_params
;
2636 if (!pwaff1
|| !pwaff2
)
2638 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2639 if (equal_params
< 0)
2642 return fn(pwaff1
, pwaff2
);
2643 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2644 !isl_space_has_named_params(pwaff2
->dim
))
2645 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2646 "unaligned unnamed parameters", goto error
);
2647 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2648 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2649 return fn(pwaff1
, pwaff2
);
2651 isl_pw_aff_free(pwaff1
);
2652 isl_pw_aff_free(pwaff2
);
2656 /* Align the parameters of the to isl_pw_aff arguments and
2657 * then apply a function "fn" on them that returns an isl_map.
2659 static __isl_give isl_map
*align_params_pw_pw_map_and(
2660 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2661 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2662 __isl_take isl_pw_aff
*pa2
))
2664 isl_bool equal_params
;
2668 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2669 if (equal_params
< 0)
2672 return fn(pa1
, pa2
);
2673 if (!isl_space_has_named_params(pa1
->dim
) ||
2674 !isl_space_has_named_params(pa2
->dim
))
2675 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2676 "unaligned unnamed parameters", goto error
);
2677 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2678 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2679 return fn(pa1
, pa2
);
2681 isl_pw_aff_free(pa1
);
2682 isl_pw_aff_free(pa2
);
2686 /* Compute a piecewise quasi-affine expression with a domain that
2687 * is the union of those of pwaff1 and pwaff2 and such that on each
2688 * cell, the quasi-affine expression is the maximum of those of pwaff1
2689 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2690 * cell, then the associated expression is the defined one.
2692 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2693 __isl_take isl_pw_aff
*pwaff2
)
2695 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2698 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2699 __isl_take isl_pw_aff
*pwaff2
)
2701 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2705 /* Compute a piecewise quasi-affine expression with a domain that
2706 * is the union of those of pwaff1 and pwaff2 and such that on each
2707 * cell, the quasi-affine expression is the minimum of those of pwaff1
2708 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2709 * cell, then the associated expression is the defined one.
2711 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2712 __isl_take isl_pw_aff
*pwaff2
)
2714 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2717 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2718 __isl_take isl_pw_aff
*pwaff2
)
2720 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2724 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2725 __isl_take isl_pw_aff
*pwaff2
, int max
)
2728 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2730 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2733 /* Construct a map with as domain the domain of pwaff and
2734 * one-dimensional range corresponding to the affine expressions.
2736 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2745 dim
= isl_pw_aff_get_space(pwaff
);
2746 map
= isl_map_empty(dim
);
2748 for (i
= 0; i
< pwaff
->n
; ++i
) {
2749 isl_basic_map
*bmap
;
2752 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2753 map_i
= isl_map_from_basic_map(bmap
);
2754 map_i
= isl_map_intersect_domain(map_i
,
2755 isl_set_copy(pwaff
->p
[i
].set
));
2756 map
= isl_map_union_disjoint(map
, map_i
);
2759 isl_pw_aff_free(pwaff
);
2764 /* Construct a map with as domain the domain of pwaff and
2765 * one-dimensional range corresponding to the affine expressions.
2767 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2771 if (isl_space_is_set(pwaff
->dim
))
2772 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2773 "space of input is not a map", goto error
);
2774 return map_from_pw_aff(pwaff
);
2776 isl_pw_aff_free(pwaff
);
2780 /* Construct a one-dimensional set with as parameter domain
2781 * the domain of pwaff and the single set dimension
2782 * corresponding to the affine expressions.
2784 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2788 if (!isl_space_is_set(pwaff
->dim
))
2789 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2790 "space of input is not a set", goto error
);
2791 return map_from_pw_aff(pwaff
);
2793 isl_pw_aff_free(pwaff
);
2797 /* Return a set containing those elements in the domain
2798 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2799 * does not satisfy "fn" (if complement is 1).
2801 * The pieces with a NaN never belong to the result since
2802 * NaN does not satisfy any property.
2804 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2805 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2814 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2816 for (i
= 0; i
< pwaff
->n
; ++i
) {
2817 isl_basic_set
*bset
;
2818 isl_set
*set_i
, *locus
;
2821 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2824 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2825 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2826 locus
= isl_set_from_basic_set(bset
);
2827 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2829 set_i
= isl_set_subtract(set_i
, locus
);
2831 set_i
= isl_set_intersect(set_i
, locus
);
2832 set
= isl_set_union_disjoint(set
, set_i
);
2835 isl_pw_aff_free(pwaff
);
2840 /* Return a set containing those elements in the domain
2841 * of "pa" where it is positive.
2843 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2845 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2848 /* Return a set containing those elements in the domain
2849 * of pwaff where it is non-negative.
2851 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2853 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2856 /* Return a set containing those elements in the domain
2857 * of pwaff where it is zero.
2859 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2861 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2864 /* Return a set containing those elements in the domain
2865 * of pwaff where it is not zero.
2867 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2869 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2872 /* Return a set containing those elements in the shared domain
2873 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2875 * We compute the difference on the shared domain and then construct
2876 * the set of values where this difference is non-negative.
2877 * If strict is set, we first subtract 1 from the difference.
2878 * If equal is set, we only return the elements where pwaff1 and pwaff2
2881 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2882 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2884 isl_set
*set1
, *set2
;
2886 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2887 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2888 set1
= isl_set_intersect(set1
, set2
);
2889 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2890 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2891 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2894 isl_space
*dim
= isl_set_get_space(set1
);
2896 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2897 aff
= isl_aff_add_constant_si(aff
, -1);
2898 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2903 return isl_pw_aff_zero_set(pwaff1
);
2904 return isl_pw_aff_nonneg_set(pwaff1
);
2907 /* Return a set containing those elements in the shared domain
2908 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2910 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2911 __isl_take isl_pw_aff
*pwaff2
)
2913 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2916 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2917 __isl_take isl_pw_aff
*pwaff2
)
2919 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2922 /* Return a set containing those elements in the shared domain
2923 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2925 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2926 __isl_take isl_pw_aff
*pwaff2
)
2928 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2931 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2932 __isl_take isl_pw_aff
*pwaff2
)
2934 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2937 /* Return a set containing those elements in the shared domain
2938 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2940 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2941 __isl_take isl_pw_aff
*pwaff2
)
2943 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2946 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2947 __isl_take isl_pw_aff
*pwaff2
)
2949 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2952 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2953 __isl_take isl_pw_aff
*pwaff2
)
2955 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2958 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2959 __isl_take isl_pw_aff
*pwaff2
)
2961 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2964 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2965 * where the function values are ordered in the same way as "order",
2966 * which returns a set in the shared domain of its two arguments.
2967 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2969 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2970 * We first pull back the two functions such that they are defined on
2971 * the domain [A -> B]. Then we apply "order", resulting in a set
2972 * in the space [A -> B]. Finally, we unwrap this set to obtain
2973 * a map in the space A -> B.
2975 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2976 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2977 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2978 __isl_take isl_pw_aff
*pa2
))
2980 isl_space
*space1
, *space2
;
2984 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2985 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2986 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2987 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2988 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2989 ma
= isl_multi_aff_range_map(space1
);
2990 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2991 set
= order(pa1
, pa2
);
2993 return isl_set_unwrap(set
);
2996 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2997 * where the function values are equal.
2998 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3000 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
3001 __isl_take isl_pw_aff
*pa2
)
3003 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
3006 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3007 * where the function values are equal.
3009 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3010 __isl_take isl_pw_aff
*pa2
)
3012 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3015 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3016 * where the function value of "pa1" is less than the function value of "pa2".
3017 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3019 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3020 __isl_take isl_pw_aff
*pa2
)
3022 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3025 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3026 * where the function value of "pa1" is less than the function value of "pa2".
3028 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3029 __isl_take isl_pw_aff
*pa2
)
3031 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function value of "pa1" is greater than the function value
3037 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3039 static __isl_give isl_map
*isl_pw_aff_gt_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_gt_set
);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is greater than the function value
3049 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3050 __isl_take isl_pw_aff
*pa2
)
3052 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3055 /* Return a set containing those elements in the shared domain
3056 * of the elements of list1 and list2 where each element in list1
3057 * has the relation specified by "fn" with each element in list2.
3059 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3060 __isl_take isl_pw_aff_list
*list2
,
3061 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3062 __isl_take isl_pw_aff
*pwaff2
))
3068 if (!list1
|| !list2
)
3071 ctx
= isl_pw_aff_list_get_ctx(list1
);
3072 if (list1
->n
< 1 || list2
->n
< 1)
3073 isl_die(ctx
, isl_error_invalid
,
3074 "list should contain at least one element", goto error
);
3076 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3077 for (i
= 0; i
< list1
->n
; ++i
)
3078 for (j
= 0; j
< list2
->n
; ++j
) {
3081 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3082 isl_pw_aff_copy(list2
->p
[j
]));
3083 set
= isl_set_intersect(set
, set_ij
);
3086 isl_pw_aff_list_free(list1
);
3087 isl_pw_aff_list_free(list2
);
3090 isl_pw_aff_list_free(list1
);
3091 isl_pw_aff_list_free(list2
);
3095 /* Return a set containing those elements in the shared domain
3096 * of the elements of list1 and list2 where each element in list1
3097 * is equal to each element in list2.
3099 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3100 __isl_take isl_pw_aff_list
*list2
)
3102 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3105 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3106 __isl_take isl_pw_aff_list
*list2
)
3108 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3111 /* Return a set containing those elements in the shared domain
3112 * of the elements of list1 and list2 where each element in list1
3113 * is less than or equal to each element in list2.
3115 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3116 __isl_take isl_pw_aff_list
*list2
)
3118 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3121 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3122 __isl_take isl_pw_aff_list
*list2
)
3124 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3127 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3128 __isl_take isl_pw_aff_list
*list2
)
3130 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3133 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3134 __isl_take isl_pw_aff_list
*list2
)
3136 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3140 /* Return a set containing those elements in the shared domain
3141 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3143 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3144 __isl_take isl_pw_aff
*pwaff2
)
3146 isl_set
*set_lt
, *set_gt
;
3148 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3149 isl_pw_aff_copy(pwaff2
));
3150 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3151 return isl_set_union_disjoint(set_lt
, set_gt
);
3154 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3155 __isl_take isl_pw_aff
*pwaff2
)
3157 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3160 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3165 if (isl_int_is_one(v
))
3167 if (!isl_int_is_pos(v
))
3168 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3169 "factor needs to be positive",
3170 return isl_pw_aff_free(pwaff
));
3171 pwaff
= isl_pw_aff_cow(pwaff
);
3177 for (i
= 0; i
< pwaff
->n
; ++i
) {
3178 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3179 if (!pwaff
->p
[i
].aff
)
3180 return isl_pw_aff_free(pwaff
);
3186 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3190 pwaff
= isl_pw_aff_cow(pwaff
);
3196 for (i
= 0; i
< pwaff
->n
; ++i
) {
3197 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3198 if (!pwaff
->p
[i
].aff
)
3199 return isl_pw_aff_free(pwaff
);
3205 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3209 pwaff
= isl_pw_aff_cow(pwaff
);
3215 for (i
= 0; i
< pwaff
->n
; ++i
) {
3216 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3217 if (!pwaff
->p
[i
].aff
)
3218 return isl_pw_aff_free(pwaff
);
3224 /* Assuming that "cond1" and "cond2" are disjoint,
3225 * return an affine expression that is equal to pwaff1 on cond1
3226 * and to pwaff2 on cond2.
3228 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3229 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3230 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3232 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3233 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3235 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3238 /* Return an affine expression that is equal to pwaff_true for elements
3239 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3241 * That is, return cond ? pwaff_true : pwaff_false;
3243 * If "cond" involves and NaN, then we conservatively return a NaN
3244 * on its entire domain. In principle, we could consider the pieces
3245 * where it is NaN separately from those where it is not.
3247 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3248 * then only use the domain of "cond" to restrict the domain.
3250 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3251 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3253 isl_set
*cond_true
, *cond_false
;
3258 if (isl_pw_aff_involves_nan(cond
)) {
3259 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3260 isl_local_space
*ls
= isl_local_space_from_space(space
);
3261 isl_pw_aff_free(cond
);
3262 isl_pw_aff_free(pwaff_true
);
3263 isl_pw_aff_free(pwaff_false
);
3264 return isl_pw_aff_nan_on_domain(ls
);
3267 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3268 isl_pw_aff_get_space(pwaff_false
));
3269 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3270 isl_pw_aff_get_space(pwaff_true
));
3271 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3277 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3278 isl_pw_aff_free(pwaff_false
);
3279 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3282 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3283 cond_false
= isl_pw_aff_zero_set(cond
);
3284 return isl_pw_aff_select(cond_true
, pwaff_true
,
3285 cond_false
, pwaff_false
);
3287 isl_pw_aff_free(cond
);
3288 isl_pw_aff_free(pwaff_true
);
3289 isl_pw_aff_free(pwaff_false
);
3293 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3296 return isl_bool_error
;
3298 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3301 /* Check whether pwaff is a piecewise constant.
3303 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3308 return isl_bool_error
;
3310 for (i
= 0; i
< pwaff
->n
; ++i
) {
3311 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3312 if (is_cst
< 0 || !is_cst
)
3316 return isl_bool_true
;
3319 /* Are all elements of "mpa" piecewise constants?
3321 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3326 return isl_bool_error
;
3328 for (i
= 0; i
< mpa
->n
; ++i
) {
3329 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3330 if (is_cst
< 0 || !is_cst
)
3334 return isl_bool_true
;
3337 /* Return the product of "aff1" and "aff2".
3339 * If either of the two is NaN, then the result is NaN.
3341 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3343 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3344 __isl_take isl_aff
*aff2
)
3349 if (isl_aff_is_nan(aff1
)) {
3353 if (isl_aff_is_nan(aff2
)) {
3358 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3359 return isl_aff_mul(aff2
, aff1
);
3361 if (!isl_aff_is_cst(aff2
))
3362 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3363 "at least one affine expression should be constant",
3366 aff1
= isl_aff_cow(aff1
);
3370 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3371 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3381 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3383 * If either of the two is NaN, then the result is NaN.
3385 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3386 __isl_take isl_aff
*aff2
)
3394 if (isl_aff_is_nan(aff1
)) {
3398 if (isl_aff_is_nan(aff2
)) {
3403 is_cst
= isl_aff_is_cst(aff2
);
3407 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3408 "second argument should be a constant", goto error
);
3413 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3415 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3416 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3419 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3420 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3423 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3424 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3435 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3436 __isl_take isl_pw_aff
*pwaff2
)
3438 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3441 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3442 __isl_take isl_pw_aff
*pwaff2
)
3444 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3447 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3448 __isl_take isl_pw_aff
*pwaff2
)
3450 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3453 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3454 __isl_take isl_pw_aff
*pwaff2
)
3456 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3459 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3460 __isl_take isl_pw_aff
*pwaff2
)
3462 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3465 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3466 __isl_take isl_pw_aff
*pa2
)
3468 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3471 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3473 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3474 __isl_take isl_pw_aff
*pa2
)
3478 is_cst
= isl_pw_aff_is_cst(pa2
);
3482 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3483 "second argument should be a piecewise constant",
3485 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3487 isl_pw_aff_free(pa1
);
3488 isl_pw_aff_free(pa2
);
3492 /* Compute the quotient of the integer division of "pa1" by "pa2"
3493 * with rounding towards zero.
3494 * "pa2" is assumed to be a piecewise constant.
3496 * In particular, return
3498 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3501 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3502 __isl_take isl_pw_aff
*pa2
)
3508 is_cst
= isl_pw_aff_is_cst(pa2
);
3512 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3513 "second argument should be a piecewise constant",
3516 pa1
= isl_pw_aff_div(pa1
, pa2
);
3518 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3519 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3520 c
= isl_pw_aff_ceil(pa1
);
3521 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3523 isl_pw_aff_free(pa1
);
3524 isl_pw_aff_free(pa2
);
3528 /* Compute the remainder of the integer division of "pa1" by "pa2"
3529 * with rounding towards zero.
3530 * "pa2" is assumed to be a piecewise constant.
3532 * In particular, return
3534 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3537 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3538 __isl_take isl_pw_aff
*pa2
)
3543 is_cst
= isl_pw_aff_is_cst(pa2
);
3547 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3548 "second argument should be a piecewise constant",
3550 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3551 res
= isl_pw_aff_mul(pa2
, res
);
3552 res
= isl_pw_aff_sub(pa1
, res
);
3555 isl_pw_aff_free(pa1
);
3556 isl_pw_aff_free(pa2
);
3560 /* Does either of "pa1" or "pa2" involve any NaN2?
3562 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3563 __isl_keep isl_pw_aff
*pa2
)
3567 has_nan
= isl_pw_aff_involves_nan(pa1
);
3568 if (has_nan
< 0 || has_nan
)
3570 return isl_pw_aff_involves_nan(pa2
);
3573 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3574 * by a NaN on their shared domain.
3576 * In principle, the result could be refined to only being NaN
3577 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3579 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3580 __isl_take isl_pw_aff
*pa2
)
3582 isl_local_space
*ls
;
3586 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3587 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3588 pa
= isl_pw_aff_nan_on_domain(ls
);
3589 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3594 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3595 __isl_take isl_pw_aff
*pwaff2
)
3600 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3601 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3602 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3603 isl_pw_aff_copy(pwaff2
));
3604 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3605 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3608 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3609 __isl_take isl_pw_aff
*pwaff2
)
3614 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3615 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3616 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3617 isl_pw_aff_copy(pwaff2
));
3618 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3619 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3622 /* Return an expression for the minimum (if "max" is not set) or
3623 * the maximum (if "max" is set) of "pa1" and "pa2".
3624 * If either expression involves any NaN, then return a NaN
3625 * on the shared domain as result.
3627 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3628 __isl_take isl_pw_aff
*pa2
, int max
)
3632 has_nan
= either_involves_nan(pa1
, pa2
);
3634 pa1
= isl_pw_aff_free(pa1
);
3636 return replace_by_nan(pa1
, pa2
);
3639 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3641 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3644 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3646 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3647 __isl_take isl_pw_aff
*pwaff2
)
3649 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3652 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3654 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3655 __isl_take isl_pw_aff
*pwaff2
)
3657 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3660 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3661 __isl_take isl_pw_aff_list
*list
,
3662 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3663 __isl_take isl_pw_aff
*pwaff2
))
3672 ctx
= isl_pw_aff_list_get_ctx(list
);
3674 isl_die(ctx
, isl_error_invalid
,
3675 "list should contain at least one element", goto error
);
3677 res
= isl_pw_aff_copy(list
->p
[0]);
3678 for (i
= 1; i
< list
->n
; ++i
)
3679 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3681 isl_pw_aff_list_free(list
);
3684 isl_pw_aff_list_free(list
);
3688 /* Return an isl_pw_aff that maps each element in the intersection of the
3689 * domains of the elements of list to the minimal corresponding affine
3692 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3694 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3697 /* Return an isl_pw_aff that maps each element in the intersection of the
3698 * domains of the elements of list to the maximal corresponding affine
3701 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3703 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3706 /* Mark the domains of "pwaff" as rational.
3708 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3712 pwaff
= isl_pw_aff_cow(pwaff
);
3718 for (i
= 0; i
< pwaff
->n
; ++i
) {
3719 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3720 if (!pwaff
->p
[i
].set
)
3721 return isl_pw_aff_free(pwaff
);
3727 /* Mark the domains of the elements of "list" as rational.
3729 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3730 __isl_take isl_pw_aff_list
*list
)
3740 for (i
= 0; i
< n
; ++i
) {
3743 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3744 pa
= isl_pw_aff_set_rational(pa
);
3745 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3751 /* Do the parameters of "aff" match those of "space"?
3753 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3754 __isl_keep isl_space
*space
)
3756 isl_space
*aff_space
;
3760 return isl_bool_error
;
3762 aff_space
= isl_aff_get_domain_space(aff
);
3764 match
= isl_space_has_equal_params(space
, aff_space
);
3766 isl_space_free(aff_space
);
3770 /* Check that the domain space of "aff" matches "space".
3772 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3773 __isl_keep isl_space
*space
)
3775 isl_space
*aff_space
;
3779 return isl_stat_error
;
3781 aff_space
= isl_aff_get_domain_space(aff
);
3783 match
= isl_space_has_equal_params(space
, aff_space
);
3787 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3788 "parameters don't match", goto error
);
3789 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3790 aff_space
, isl_dim_set
);
3794 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3795 "domains don't match", goto error
);
3796 isl_space_free(aff_space
);
3799 isl_space_free(aff_space
);
3800 return isl_stat_error
;
3809 #include <isl_multi_templ.c>
3810 #include <isl_multi_apply_set.c>
3811 #include <isl_multi_cmp.c>
3812 #include <isl_multi_dims.c>
3813 #include <isl_multi_floor.c>
3814 #include <isl_multi_gist.c>
3818 /* Construct an isl_multi_aff living in "space" that corresponds
3819 * to the affine transformation matrix "mat".
3821 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3822 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3825 isl_local_space
*ls
= NULL
;
3826 isl_multi_aff
*ma
= NULL
;
3827 int n_row
, n_col
, n_out
, total
;
3833 ctx
= isl_mat_get_ctx(mat
);
3835 n_row
= isl_mat_rows(mat
);
3836 n_col
= isl_mat_cols(mat
);
3838 isl_die(ctx
, isl_error_invalid
,
3839 "insufficient number of rows", goto error
);
3841 isl_die(ctx
, isl_error_invalid
,
3842 "insufficient number of columns", goto error
);
3843 n_out
= isl_space_dim(space
, isl_dim_out
);
3844 total
= isl_space_dim(space
, isl_dim_all
);
3845 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3846 isl_die(ctx
, isl_error_invalid
,
3847 "dimension mismatch", goto error
);
3849 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3850 ls
= isl_local_space_from_space(isl_space_domain(space
));
3852 for (i
= 0; i
< n_row
- 1; ++i
) {
3856 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3859 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3860 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3861 v
= isl_vec_normalize(v
);
3862 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3863 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3866 isl_local_space_free(ls
);
3870 isl_local_space_free(ls
);
3872 isl_multi_aff_free(ma
);
3876 /* Remove any internal structure of the domain of "ma".
3877 * If there is any such internal structure in the input,
3878 * then the name of the corresponding space is also removed.
3880 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3881 __isl_take isl_multi_aff
*ma
)
3888 if (!ma
->space
->nested
[0])
3891 space
= isl_multi_aff_get_space(ma
);
3892 space
= isl_space_flatten_domain(space
);
3893 ma
= isl_multi_aff_reset_space(ma
, space
);
3898 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3899 * of the space to its domain.
3901 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3904 isl_local_space
*ls
;
3909 if (!isl_space_is_map(space
))
3910 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3911 "not a map space", goto error
);
3913 n_in
= isl_space_dim(space
, isl_dim_in
);
3914 space
= isl_space_domain_map(space
);
3916 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3918 isl_space_free(space
);
3922 space
= isl_space_domain(space
);
3923 ls
= isl_local_space_from_space(space
);
3924 for (i
= 0; i
< n_in
; ++i
) {
3927 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3929 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3931 isl_local_space_free(ls
);
3934 isl_space_free(space
);
3938 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3939 * of the space to its range.
3941 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3944 isl_local_space
*ls
;
3949 if (!isl_space_is_map(space
))
3950 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3951 "not a map space", goto error
);
3953 n_in
= isl_space_dim(space
, isl_dim_in
);
3954 n_out
= isl_space_dim(space
, isl_dim_out
);
3955 space
= isl_space_range_map(space
);
3957 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3959 isl_space_free(space
);
3963 space
= isl_space_domain(space
);
3964 ls
= isl_local_space_from_space(space
);
3965 for (i
= 0; i
< n_out
; ++i
) {
3968 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3969 isl_dim_set
, n_in
+ i
);
3970 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3972 isl_local_space_free(ls
);
3975 isl_space_free(space
);
3979 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3980 * of the space to its range.
3982 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3983 __isl_take isl_space
*space
)
3985 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3988 /* Given the space of a set and a range of set dimensions,
3989 * construct an isl_multi_aff that projects out those dimensions.
3991 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3992 __isl_take isl_space
*space
, enum isl_dim_type type
,
3993 unsigned first
, unsigned n
)
3996 isl_local_space
*ls
;
4001 if (!isl_space_is_set(space
))
4002 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4003 "expecting set space", goto error
);
4004 if (type
!= isl_dim_set
)
4005 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4006 "only set dimensions can be projected out", goto error
);
4008 dim
= isl_space_dim(space
, isl_dim_set
);
4009 if (first
+ n
> dim
)
4010 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4011 "range out of bounds", goto error
);
4013 space
= isl_space_from_domain(space
);
4014 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4017 return isl_multi_aff_alloc(space
);
4019 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4020 space
= isl_space_domain(space
);
4021 ls
= isl_local_space_from_space(space
);
4023 for (i
= 0; i
< first
; ++i
) {
4026 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4028 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4031 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4034 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4035 isl_dim_set
, first
+ n
+ i
);
4036 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4039 isl_local_space_free(ls
);
4042 isl_space_free(space
);
4046 /* Given the space of a set and a range of set dimensions,
4047 * construct an isl_pw_multi_aff that projects out those dimensions.
4049 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4050 __isl_take isl_space
*space
, enum isl_dim_type type
,
4051 unsigned first
, unsigned n
)
4055 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4056 return isl_pw_multi_aff_from_multi_aff(ma
);
4059 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4062 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
4063 __isl_take isl_multi_aff
*ma
)
4065 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
4066 return isl_pw_multi_aff_alloc(dom
, ma
);
4069 /* Create a piecewise multi-affine expression in the given space that maps each
4070 * input dimension to the corresponding output dimension.
4072 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4073 __isl_take isl_space
*space
)
4075 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4078 /* Exploit the equalities in "eq" to simplify the affine expressions.
4080 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4081 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4085 maff
= isl_multi_aff_cow(maff
);
4089 for (i
= 0; i
< maff
->n
; ++i
) {
4090 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
4091 isl_basic_set_copy(eq
));
4096 isl_basic_set_free(eq
);
4099 isl_basic_set_free(eq
);
4100 isl_multi_aff_free(maff
);
4104 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4109 maff
= isl_multi_aff_cow(maff
);
4113 for (i
= 0; i
< maff
->n
; ++i
) {
4114 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4116 return isl_multi_aff_free(maff
);
4122 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4123 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4125 maff1
= isl_multi_aff_add(maff1
, maff2
);
4126 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4130 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4138 /* Return the set of domain elements where "ma1" is lexicographically
4139 * smaller than or equal to "ma2".
4141 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4142 __isl_take isl_multi_aff
*ma2
)
4144 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4147 /* Return the set of domain elements where "ma1" is lexicographically
4148 * smaller than "ma2".
4150 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4151 __isl_take isl_multi_aff
*ma2
)
4153 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4156 /* Return the set of domain elements where "ma1" and "ma2"
4159 static __isl_give isl_set
*isl_multi_aff_order_set(
4160 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4161 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4164 isl_map
*map1
, *map2
;
4167 map1
= isl_map_from_multi_aff(ma1
);
4168 map2
= isl_map_from_multi_aff(ma2
);
4169 map
= isl_map_range_product(map1
, map2
);
4170 space
= isl_space_range(isl_map_get_space(map
));
4171 space
= isl_space_domain(isl_space_unwrap(space
));
4173 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4175 return isl_map_domain(map
);
4178 /* Return the set of domain elements where "ma1" is lexicographically
4179 * greater than or equal to "ma2".
4181 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4182 __isl_take isl_multi_aff
*ma2
)
4184 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4187 /* Return the set of domain elements where "ma1" is lexicographically
4188 * greater than "ma2".
4190 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4191 __isl_take isl_multi_aff
*ma2
)
4193 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4197 #define PW isl_pw_multi_aff
4199 #define EL isl_multi_aff
4201 #define EL_IS_ZERO is_empty
4205 #define IS_ZERO is_empty
4208 #undef DEFAULT_IS_ZERO
4209 #define DEFAULT_IS_ZERO 0
4214 #define NO_INVOLVES_DIMS
4215 #define NO_INSERT_DIMS
4219 #include <isl_pw_templ.c>
4220 #include <isl_pw_union_opt.c>
4225 #define UNION isl_union_pw_multi_aff
4227 #define PART isl_pw_multi_aff
4229 #define PARTS pw_multi_aff
4231 #include <isl_union_multi.c>
4232 #include <isl_union_neg.c>
4234 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4235 __isl_take isl_pw_multi_aff
*pma1
,
4236 __isl_take isl_pw_multi_aff
*pma2
)
4238 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4239 &isl_multi_aff_lex_ge_set
);
4242 /* Given two piecewise multi affine expressions, return a piecewise
4243 * multi-affine expression defined on the union of the definition domains
4244 * of the inputs that is equal to the lexicographic maximum of the two
4245 * inputs on each cell. If only one of the two inputs is defined on
4246 * a given cell, then it is considered to be the maximum.
4248 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4249 __isl_take isl_pw_multi_aff
*pma1
,
4250 __isl_take isl_pw_multi_aff
*pma2
)
4252 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4253 &pw_multi_aff_union_lexmax
);
4256 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4257 __isl_take isl_pw_multi_aff
*pma1
,
4258 __isl_take isl_pw_multi_aff
*pma2
)
4260 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4261 &isl_multi_aff_lex_le_set
);
4264 /* Given two piecewise multi affine expressions, return a piecewise
4265 * multi-affine expression defined on the union of the definition domains
4266 * of the inputs that is equal to the lexicographic minimum of the two
4267 * inputs on each cell. If only one of the two inputs is defined on
4268 * a given cell, then it is considered to be the minimum.
4270 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4271 __isl_take isl_pw_multi_aff
*pma1
,
4272 __isl_take isl_pw_multi_aff
*pma2
)
4274 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4275 &pw_multi_aff_union_lexmin
);
4278 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4279 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4281 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4282 &isl_multi_aff_add
);
4285 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4286 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4288 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4292 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4293 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4295 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4296 &isl_multi_aff_sub
);
4299 /* Subtract "pma2" from "pma1" and return the result.
4301 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4302 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4304 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4308 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4309 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4311 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4314 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4315 * with the actual sum on the shared domain and
4316 * the defined expression on the symmetric difference of the domains.
4318 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4319 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4321 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4324 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4325 * with the actual sum on the shared domain and
4326 * the defined expression on the symmetric difference of the domains.
4328 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4329 __isl_take isl_union_pw_multi_aff
*upma1
,
4330 __isl_take isl_union_pw_multi_aff
*upma2
)
4332 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4335 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4336 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4338 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4339 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4343 isl_pw_multi_aff
*res
;
4348 n
= pma1
->n
* pma2
->n
;
4349 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4350 isl_space_copy(pma2
->dim
));
4351 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4353 for (i
= 0; i
< pma1
->n
; ++i
) {
4354 for (j
= 0; j
< pma2
->n
; ++j
) {
4358 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4359 isl_set_copy(pma2
->p
[j
].set
));
4360 ma
= isl_multi_aff_product(
4361 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4362 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4363 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4367 isl_pw_multi_aff_free(pma1
);
4368 isl_pw_multi_aff_free(pma2
);
4371 isl_pw_multi_aff_free(pma1
);
4372 isl_pw_multi_aff_free(pma2
);
4376 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4377 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4379 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4380 &pw_multi_aff_product
);
4383 /* Construct a map mapping the domain of the piecewise multi-affine expression
4384 * to its range, with each dimension in the range equated to the
4385 * corresponding affine expression on its cell.
4387 * If the domain of "pma" is rational, then so is the constructed "map".
4389 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4397 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4399 for (i
= 0; i
< pma
->n
; ++i
) {
4401 isl_multi_aff
*maff
;
4402 isl_basic_map
*bmap
;
4405 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4407 map
= isl_map_free(map
);
4408 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4409 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4410 map_i
= isl_map_from_basic_map(bmap
);
4411 map_i
= isl_map_intersect_domain(map_i
,
4412 isl_set_copy(pma
->p
[i
].set
));
4413 map
= isl_map_union_disjoint(map
, map_i
);
4416 isl_pw_multi_aff_free(pma
);
4420 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4425 if (!isl_space_is_set(pma
->dim
))
4426 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4427 "isl_pw_multi_aff cannot be converted into an isl_set",
4430 return isl_map_from_pw_multi_aff(pma
);
4432 isl_pw_multi_aff_free(pma
);
4436 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4437 * denominator "denom".
4438 * "denom" is allowed to be negative, in which case the actual denominator
4439 * is -denom and the expressions are added instead.
4441 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4442 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4448 first
= isl_seq_first_non_zero(c
, n
);
4452 sign
= isl_int_sgn(denom
);
4454 isl_int_abs(d
, denom
);
4455 for (i
= first
; i
< n
; ++i
) {
4458 if (isl_int_is_zero(c
[i
]))
4460 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4461 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4462 aff_i
= isl_aff_scale_down(aff_i
, d
);
4464 aff
= isl_aff_sub(aff
, aff_i
);
4466 aff
= isl_aff_add(aff
, aff_i
);
4473 /* Extract an affine expression that expresses the output dimension "pos"
4474 * of "bmap" in terms of the parameters and input dimensions from
4476 * Note that this expression may involve integer divisions defined
4477 * in terms of parameters and input dimensions.
4478 * The equality may also involve references to earlier (but not later)
4479 * output dimensions. These are replaced by the corresponding elements
4482 * If the equality is of the form
4484 * f(i) + h(j) + a x + g(i) = 0,
4486 * with f(i) a linear combinations of the parameters and input dimensions,
4487 * g(i) a linear combination of integer divisions defined in terms of the same
4488 * and h(j) a linear combinations of earlier output dimensions,
4489 * then the affine expression is
4491 * (-f(i) - g(i))/a - h(j)/a
4493 * If the equality is of the form
4495 * f(i) + h(j) - a x + g(i) = 0,
4497 * then the affine expression is
4499 * (f(i) + g(i))/a - h(j)/(-a)
4502 * If "div" refers to an integer division (i.e., it is smaller than
4503 * the number of integer divisions), then the equality constraint
4504 * does involve an integer division (the one at position "div") that
4505 * is defined in terms of output dimensions. However, this integer
4506 * division can be eliminated by exploiting a pair of constraints
4507 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4508 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4510 * In particular, let
4512 * x = e(i) + m floor(...)
4514 * with e(i) the expression derived above and floor(...) the integer
4515 * division involving output dimensions.
4526 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4527 * = (e(i) - l) mod m
4531 * x - l = (e(i) - l) mod m
4535 * x = ((e(i) - l) mod m) + l
4537 * The variable "shift" below contains the expression -l, which may
4538 * also involve a linear combination of earlier output dimensions.
4540 static __isl_give isl_aff
*extract_aff_from_equality(
4541 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4542 __isl_keep isl_multi_aff
*ma
)
4545 unsigned n_div
, n_out
;
4547 isl_local_space
*ls
;
4548 isl_aff
*aff
, *shift
;
4551 ctx
= isl_basic_map_get_ctx(bmap
);
4552 ls
= isl_basic_map_get_local_space(bmap
);
4553 ls
= isl_local_space_domain(ls
);
4554 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4557 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4558 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4559 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4560 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4561 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4562 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4563 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4565 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4566 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4567 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4570 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4571 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4572 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4573 bmap
->eq
[eq
][o_out
+ pos
]);
4575 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4578 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4579 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4580 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4581 isl_int_set_si(shift
->v
->el
[0], 1);
4582 shift
= subtract_initial(shift
, ma
, pos
,
4583 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4584 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4585 mod
= isl_val_int_from_isl_int(ctx
,
4586 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4587 mod
= isl_val_abs(mod
);
4588 aff
= isl_aff_mod_val(aff
, mod
);
4589 aff
= isl_aff_sub(aff
, shift
);
4592 isl_local_space_free(ls
);
4595 isl_local_space_free(ls
);
4600 /* Given a basic map with output dimensions defined
4601 * in terms of the parameters input dimensions and earlier
4602 * output dimensions using an equality (and possibly a pair on inequalities),
4603 * extract an isl_aff that expresses output dimension "pos" in terms
4604 * of the parameters and input dimensions.
4605 * Note that this expression may involve integer divisions defined
4606 * in terms of parameters and input dimensions.
4607 * "ma" contains the expressions corresponding to earlier output dimensions.
4609 * This function shares some similarities with
4610 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4612 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4613 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4620 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4621 if (eq
>= bmap
->n_eq
)
4622 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4623 "unable to find suitable equality", return NULL
);
4624 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4626 aff
= isl_aff_remove_unused_divs(aff
);
4630 /* Given a basic map where each output dimension is defined
4631 * in terms of the parameters and input dimensions using an equality,
4632 * extract an isl_multi_aff that expresses the output dimensions in terms
4633 * of the parameters and input dimensions.
4635 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4636 __isl_take isl_basic_map
*bmap
)
4645 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4646 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4648 for (i
= 0; i
< n_out
; ++i
) {
4651 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4652 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4655 isl_basic_map_free(bmap
);
4660 /* Given a basic set where each set dimension is defined
4661 * in terms of the parameters using an equality,
4662 * extract an isl_multi_aff that expresses the set dimensions in terms
4663 * of the parameters.
4665 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4666 __isl_take isl_basic_set
*bset
)
4668 return extract_isl_multi_aff_from_basic_map(bset
);
4671 /* Create an isl_pw_multi_aff that is equivalent to
4672 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4673 * The given basic map is such that each output dimension is defined
4674 * in terms of the parameters and input dimensions using an equality.
4676 * Since some applications expect the result of isl_pw_multi_aff_from_map
4677 * to only contain integer affine expressions, we compute the floor
4678 * of the expression before returning.
4680 * Remove all constraints involving local variables without
4681 * an explicit representation (resulting in the removal of those
4682 * local variables) prior to the actual extraction to ensure
4683 * that the local spaces in which the resulting affine expressions
4684 * are created do not contain any unknown local variables.
4685 * Removing such constraints is safe because constraints involving
4686 * unknown local variables are not used to determine whether
4687 * a basic map is obviously single-valued.
4689 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4690 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4694 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4695 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4696 ma
= isl_multi_aff_floor(ma
);
4697 return isl_pw_multi_aff_alloc(domain
, ma
);
4700 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4701 * This obviously only works if the input "map" is single-valued.
4702 * If so, we compute the lexicographic minimum of the image in the form
4703 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4704 * to its lexicographic minimum.
4705 * If the input is not single-valued, we produce an error.
4707 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4708 __isl_take isl_map
*map
)
4712 isl_pw_multi_aff
*pma
;
4714 sv
= isl_map_is_single_valued(map
);
4718 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4719 "map is not single-valued", goto error
);
4720 map
= isl_map_make_disjoint(map
);
4724 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4726 for (i
= 0; i
< map
->n
; ++i
) {
4727 isl_pw_multi_aff
*pma_i
;
4728 isl_basic_map
*bmap
;
4729 bmap
= isl_basic_map_copy(map
->p
[i
]);
4730 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4731 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4741 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4742 * taking into account that the output dimension at position "d"
4743 * can be represented as
4745 * x = floor((e(...) + c1) / m)
4747 * given that constraint "i" is of the form
4749 * e(...) + c1 - m x >= 0
4752 * Let "map" be of the form
4756 * We construct a mapping
4758 * A -> [A -> x = floor(...)]
4760 * apply that to the map, obtaining
4762 * [A -> x = floor(...)] -> B
4764 * and equate dimension "d" to x.
4765 * We then compute a isl_pw_multi_aff representation of the resulting map
4766 * and plug in the mapping above.
4768 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4769 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4773 isl_local_space
*ls
;
4781 isl_pw_multi_aff
*pma
;
4784 is_set
= isl_map_is_set(map
);
4788 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4789 ctx
= isl_map_get_ctx(map
);
4790 space
= isl_space_domain(isl_map_get_space(map
));
4791 n_in
= isl_space_dim(space
, isl_dim_set
);
4792 n
= isl_space_dim(space
, isl_dim_all
);
4794 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4796 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4797 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4799 isl_basic_map_free(hull
);
4801 ls
= isl_local_space_from_space(isl_space_copy(space
));
4802 aff
= isl_aff_alloc_vec(ls
, v
);
4803 aff
= isl_aff_floor(aff
);
4805 isl_space_free(space
);
4806 ma
= isl_multi_aff_from_aff(aff
);
4808 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4809 ma
= isl_multi_aff_range_product(ma
,
4810 isl_multi_aff_from_aff(aff
));
4813 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4814 map
= isl_map_apply_domain(map
, insert
);
4815 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4816 pma
= isl_pw_multi_aff_from_map(map
);
4817 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4822 isl_basic_map_free(hull
);
4826 /* Is constraint "c" of the form
4828 * e(...) + c1 - m x >= 0
4832 * -e(...) + c2 + m x >= 0
4834 * where m > 1 and e only depends on parameters and input dimemnsions?
4836 * "offset" is the offset of the output dimensions
4837 * "pos" is the position of output dimension x.
4839 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4841 if (isl_int_is_zero(c
[offset
+ d
]))
4843 if (isl_int_is_one(c
[offset
+ d
]))
4845 if (isl_int_is_negone(c
[offset
+ d
]))
4847 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4849 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4850 total
- (offset
+ d
+ 1)) != -1)
4855 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4857 * As a special case, we first check if there is any pair of constraints,
4858 * shared by all the basic maps in "map" that force a given dimension
4859 * to be equal to the floor of some affine combination of the input dimensions.
4861 * In particular, if we can find two constraints
4863 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4867 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4869 * where m > 1 and e only depends on parameters and input dimemnsions,
4872 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4874 * then we know that we can take
4876 * x = floor((e(...) + c1) / m)
4878 * without having to perform any computation.
4880 * Note that we know that
4884 * If c1 + c2 were 0, then we would have detected an equality during
4885 * simplification. If c1 + c2 were negative, then we would have detected
4888 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4889 __isl_take isl_map
*map
)
4895 isl_basic_map
*hull
;
4897 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4902 dim
= isl_map_dim(map
, isl_dim_out
);
4903 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4904 total
= 1 + isl_basic_map_total_dim(hull
);
4906 for (d
= 0; d
< dim
; ++d
) {
4907 for (i
= 0; i
< n
; ++i
) {
4908 if (!is_potential_div_constraint(hull
->ineq
[i
],
4911 for (j
= i
+ 1; j
< n
; ++j
) {
4912 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4913 hull
->ineq
[j
] + 1, total
- 1))
4915 isl_int_add(sum
, hull
->ineq
[i
][0],
4917 if (isl_int_abs_lt(sum
,
4918 hull
->ineq
[i
][offset
+ d
]))
4925 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4927 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4931 isl_basic_map_free(hull
);
4932 return pw_multi_aff_from_map_base(map
);
4935 isl_basic_map_free(hull
);
4939 /* Given an affine expression
4941 * [A -> B] -> f(A,B)
4943 * construct an isl_multi_aff
4947 * such that dimension "d" in B' is set to "aff" and the remaining
4948 * dimensions are set equal to the corresponding dimensions in B.
4949 * "n_in" is the dimension of the space A.
4950 * "n_out" is the dimension of the space B.
4952 * If "is_set" is set, then the affine expression is of the form
4956 * and we construct an isl_multi_aff
4960 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4961 unsigned n_in
, unsigned n_out
, int is_set
)
4965 isl_space
*space
, *space2
;
4966 isl_local_space
*ls
;
4968 space
= isl_aff_get_domain_space(aff
);
4969 ls
= isl_local_space_from_space(isl_space_copy(space
));
4970 space2
= isl_space_copy(space
);
4972 space2
= isl_space_range(isl_space_unwrap(space2
));
4973 space
= isl_space_map_from_domain_and_range(space
, space2
);
4974 ma
= isl_multi_aff_alloc(space
);
4975 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4977 for (i
= 0; i
< n_out
; ++i
) {
4980 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4981 isl_dim_set
, n_in
+ i
);
4982 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4985 isl_local_space_free(ls
);
4990 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4991 * taking into account that the dimension at position "d" can be written as
4993 * x = m a + f(..) (1)
4995 * where m is equal to "gcd".
4996 * "i" is the index of the equality in "hull" that defines f(..).
4997 * In particular, the equality is of the form
4999 * f(..) - x + m g(existentials) = 0
5003 * -f(..) + x + m g(existentials) = 0
5005 * We basically plug (1) into "map", resulting in a map with "a"
5006 * in the range instead of "x". The corresponding isl_pw_multi_aff
5007 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5009 * Specifically, given the input map
5013 * We first wrap it into a set
5017 * and define (1) on top of the corresponding space, resulting in "aff".
5018 * We use this to create an isl_multi_aff that maps the output position "d"
5019 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5020 * We plug this into the wrapped map, unwrap the result and compute the
5021 * corresponding isl_pw_multi_aff.
5022 * The result is an expression
5030 * so that we can plug that into "aff", after extending the latter to
5036 * If "map" is actually a set, then there is no "A" space, meaning
5037 * that we do not need to perform any wrapping, and that the result
5038 * of the recursive call is of the form
5042 * which is plugged into a mapping of the form
5046 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5047 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5052 isl_local_space
*ls
;
5055 isl_pw_multi_aff
*pma
, *id
;
5061 is_set
= isl_map_is_set(map
);
5065 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5066 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5067 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5072 set
= isl_map_wrap(map
);
5073 space
= isl_space_map_from_set(isl_set_get_space(set
));
5074 ma
= isl_multi_aff_identity(space
);
5075 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5076 aff
= isl_aff_alloc(ls
);
5078 isl_int_set_si(aff
->v
->el
[0], 1);
5079 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5080 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5083 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5085 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5087 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5088 set
= isl_set_preimage_multi_aff(set
, ma
);
5090 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5095 map
= isl_set_unwrap(set
);
5096 pma
= isl_pw_multi_aff_from_map(map
);
5099 space
= isl_pw_multi_aff_get_domain_space(pma
);
5100 space
= isl_space_map_from_set(space
);
5101 id
= isl_pw_multi_aff_identity(space
);
5102 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5104 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5105 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5107 isl_basic_map_free(hull
);
5111 isl_basic_map_free(hull
);
5115 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5116 * "hull" contains the equalities valid for "map".
5118 * Check if any of the output dimensions is "strided".
5119 * That is, we check if it can be written as
5123 * with m greater than 1, a some combination of existentially quantified
5124 * variables and f an expression in the parameters and input dimensions.
5125 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5127 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5130 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5131 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5140 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5141 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5144 isl_basic_map_free(hull
);
5145 return pw_multi_aff_from_map_check_div(map
);
5150 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5151 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5153 for (i
= 0; i
< n_out
; ++i
) {
5154 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5155 isl_int
*eq
= hull
->eq
[j
];
5156 isl_pw_multi_aff
*res
;
5158 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5159 !isl_int_is_negone(eq
[o_out
+ i
]))
5161 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5163 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5164 n_out
- (i
+ 1)) != -1)
5166 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5167 if (isl_int_is_zero(gcd
))
5169 if (isl_int_is_one(gcd
))
5172 res
= pw_multi_aff_from_map_stride(map
, hull
,
5180 isl_basic_map_free(hull
);
5181 return pw_multi_aff_from_map_check_div(map
);
5184 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5186 * As a special case, we first check if all output dimensions are uniquely
5187 * defined in terms of the parameters and input dimensions over the entire
5188 * domain. If so, we extract the desired isl_pw_multi_aff directly
5189 * from the affine hull of "map" and its domain.
5191 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5194 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5197 isl_basic_map
*hull
;
5202 if (isl_map_n_basic_map(map
) == 1) {
5203 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5204 hull
= isl_basic_map_plain_affine_hull(hull
);
5205 sv
= isl_basic_map_plain_is_single_valued(hull
);
5207 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5209 isl_basic_map_free(hull
);
5211 map
= isl_map_detect_equalities(map
);
5212 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5213 sv
= isl_basic_map_plain_is_single_valued(hull
);
5215 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5217 return pw_multi_aff_from_map_check_strides(map
, hull
);
5218 isl_basic_map_free(hull
);
5223 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5225 return isl_pw_multi_aff_from_map(set
);
5228 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5231 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5233 isl_union_pw_multi_aff
**upma
= user
;
5234 isl_pw_multi_aff
*pma
;
5236 pma
= isl_pw_multi_aff_from_map(map
);
5237 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5239 return *upma
? isl_stat_ok
: isl_stat_error
;
5242 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5245 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5246 __isl_take isl_aff
*aff
)
5249 isl_pw_multi_aff
*pma
;
5251 ma
= isl_multi_aff_from_aff(aff
);
5252 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5253 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5256 /* Try and create an isl_union_pw_multi_aff that is equivalent
5257 * to the given isl_union_map.
5258 * The isl_union_map is required to be single-valued in each space.
5259 * Otherwise, an error is produced.
5261 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5262 __isl_take isl_union_map
*umap
)
5265 isl_union_pw_multi_aff
*upma
;
5267 space
= isl_union_map_get_space(umap
);
5268 upma
= isl_union_pw_multi_aff_empty(space
);
5269 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5270 upma
= isl_union_pw_multi_aff_free(upma
);
5271 isl_union_map_free(umap
);
5276 /* Try and create an isl_union_pw_multi_aff that is equivalent
5277 * to the given isl_union_set.
5278 * The isl_union_set is required to be a singleton in each space.
5279 * Otherwise, an error is produced.
5281 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5282 __isl_take isl_union_set
*uset
)
5284 return isl_union_pw_multi_aff_from_union_map(uset
);
5287 /* Return the piecewise affine expression "set ? 1 : 0".
5289 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5292 isl_space
*space
= isl_set_get_space(set
);
5293 isl_local_space
*ls
= isl_local_space_from_space(space
);
5294 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5295 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5297 one
= isl_aff_add_constant_si(one
, 1);
5298 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5299 set
= isl_set_complement(set
);
5300 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5305 /* Plug in "subs" for dimension "type", "pos" of "aff".
5307 * Let i be the dimension to replace and let "subs" be of the form
5311 * and "aff" of the form
5317 * (a f + d g')/(m d)
5319 * where g' is the result of plugging in "subs" in each of the integer
5322 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5323 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5328 aff
= isl_aff_cow(aff
);
5330 return isl_aff_free(aff
);
5332 ctx
= isl_aff_get_ctx(aff
);
5333 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5334 isl_die(ctx
, isl_error_invalid
,
5335 "spaces don't match", return isl_aff_free(aff
));
5336 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5337 isl_die(ctx
, isl_error_unsupported
,
5338 "cannot handle divs yet", return isl_aff_free(aff
));
5340 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5342 return isl_aff_free(aff
);
5344 aff
->v
= isl_vec_cow(aff
->v
);
5346 return isl_aff_free(aff
);
5348 pos
+= isl_local_space_offset(aff
->ls
, type
);
5351 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5352 aff
->v
->size
, subs
->v
->size
, v
);
5358 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5359 * expressions in "maff".
5361 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5362 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5363 __isl_keep isl_aff
*subs
)
5367 maff
= isl_multi_aff_cow(maff
);
5369 return isl_multi_aff_free(maff
);
5371 if (type
== isl_dim_in
)
5374 for (i
= 0; i
< maff
->n
; ++i
) {
5375 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5377 return isl_multi_aff_free(maff
);
5383 /* Plug in "subs" for dimension "type", "pos" of "pma".
5385 * pma is of the form
5389 * while subs is of the form
5391 * v' = B_j(v) -> S_j
5393 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5394 * has a contribution in the result, in particular
5396 * C_ij(S_j) -> M_i(S_j)
5398 * Note that plugging in S_j in C_ij may also result in an empty set
5399 * and this contribution should simply be discarded.
5401 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5402 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5403 __isl_keep isl_pw_aff
*subs
)
5406 isl_pw_multi_aff
*res
;
5409 return isl_pw_multi_aff_free(pma
);
5411 n
= pma
->n
* subs
->n
;
5412 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5414 for (i
= 0; i
< pma
->n
; ++i
) {
5415 for (j
= 0; j
< subs
->n
; ++j
) {
5417 isl_multi_aff
*res_ij
;
5420 common
= isl_set_intersect(
5421 isl_set_copy(pma
->p
[i
].set
),
5422 isl_set_copy(subs
->p
[j
].set
));
5423 common
= isl_set_substitute(common
,
5424 type
, pos
, subs
->p
[j
].aff
);
5425 empty
= isl_set_plain_is_empty(common
);
5426 if (empty
< 0 || empty
) {
5427 isl_set_free(common
);
5433 res_ij
= isl_multi_aff_substitute(
5434 isl_multi_aff_copy(pma
->p
[i
].maff
),
5435 type
, pos
, subs
->p
[j
].aff
);
5437 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5441 isl_pw_multi_aff_free(pma
);
5444 isl_pw_multi_aff_free(pma
);
5445 isl_pw_multi_aff_free(res
);
5449 /* Compute the preimage of a range of dimensions in the affine expression "src"
5450 * under "ma" and put the result in "dst". The number of dimensions in "src"
5451 * that precede the range is given by "n_before". The number of dimensions
5452 * in the range is given by the number of output dimensions of "ma".
5453 * The number of dimensions that follow the range is given by "n_after".
5454 * If "has_denom" is set (to one),
5455 * then "src" and "dst" have an extra initial denominator.
5456 * "n_div_ma" is the number of existentials in "ma"
5457 * "n_div_bset" is the number of existentials in "src"
5458 * The resulting "dst" (which is assumed to have been allocated by
5459 * the caller) contains coefficients for both sets of existentials,
5460 * first those in "ma" and then those in "src".
5461 * f, c1, c2 and g are temporary objects that have been initialized
5464 * Let src represent the expression
5466 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5468 * and let ma represent the expressions
5470 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5472 * We start out with the following expression for dst:
5474 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5476 * with the multiplication factor f initially equal to 1
5477 * and f \sum_i b_i v_i kept separately.
5478 * For each x_i that we substitute, we multiply the numerator
5479 * (and denominator) of dst by c_1 = m_i and add the numerator
5480 * of the x_i expression multiplied by c_2 = f b_i,
5481 * after removing the common factors of c_1 and c_2.
5482 * The multiplication factor f also needs to be multiplied by c_1
5483 * for the next x_j, j > i.
5485 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5486 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5487 int n_div_ma
, int n_div_bmap
,
5488 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5491 int n_param
, n_in
, n_out
;
5494 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5495 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5496 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5498 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5499 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5500 isl_seq_clr(dst
+ o_dst
, n_in
);
5503 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5506 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5508 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5510 isl_int_set_si(f
, 1);
5512 for (i
= 0; i
< n_out
; ++i
) {
5513 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5515 if (isl_int_is_zero(src
[offset
]))
5517 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5518 isl_int_mul(c2
, f
, src
[offset
]);
5519 isl_int_gcd(g
, c1
, c2
);
5520 isl_int_divexact(c1
, c1
, g
);
5521 isl_int_divexact(c2
, c2
, g
);
5523 isl_int_mul(f
, f
, c1
);
5526 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5527 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5528 o_dst
+= 1 + n_param
;
5529 o_src
+= 1 + n_param
;
5530 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5532 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5533 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5536 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5538 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5539 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5542 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5544 isl_int_mul(dst
[0], dst
[0], c1
);
5548 /* Compute the pullback of "aff" by the function represented by "ma".
5549 * In other words, plug in "ma" in "aff". The result is an affine expression
5550 * defined over the domain space of "ma".
5552 * If "aff" is represented by
5554 * (a(p) + b x + c(divs))/d
5556 * and ma is represented by
5558 * x = D(p) + F(y) + G(divs')
5560 * then the result is
5562 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5564 * The divs in the local space of the input are similarly adjusted
5565 * through a call to isl_local_space_preimage_multi_aff.
5567 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5568 __isl_take isl_multi_aff
*ma
)
5570 isl_aff
*res
= NULL
;
5571 isl_local_space
*ls
;
5572 int n_div_aff
, n_div_ma
;
5573 isl_int f
, c1
, c2
, g
;
5575 ma
= isl_multi_aff_align_divs(ma
);
5579 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5580 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5582 ls
= isl_aff_get_domain_local_space(aff
);
5583 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5584 res
= isl_aff_alloc(ls
);
5593 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5602 isl_multi_aff_free(ma
);
5603 res
= isl_aff_normalize(res
);
5607 isl_multi_aff_free(ma
);
5612 /* Compute the pullback of "aff1" by the function represented by "aff2".
5613 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5614 * defined over the domain space of "aff1".
5616 * The domain of "aff1" should match the range of "aff2", which means
5617 * that it should be single-dimensional.
5619 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5620 __isl_take isl_aff
*aff2
)
5624 ma
= isl_multi_aff_from_aff(aff2
);
5625 return isl_aff_pullback_multi_aff(aff1
, ma
);
5628 /* Compute the pullback of "ma1" by the function represented by "ma2".
5629 * In other words, plug in "ma2" in "ma1".
5631 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5633 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5634 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5637 isl_space
*space
= NULL
;
5639 ma2
= isl_multi_aff_align_divs(ma2
);
5640 ma1
= isl_multi_aff_cow(ma1
);
5644 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5645 isl_multi_aff_get_space(ma1
));
5647 for (i
= 0; i
< ma1
->n
; ++i
) {
5648 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5649 isl_multi_aff_copy(ma2
));
5654 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5655 isl_multi_aff_free(ma2
);
5658 isl_space_free(space
);
5659 isl_multi_aff_free(ma2
);
5660 isl_multi_aff_free(ma1
);
5664 /* Compute the pullback of "ma1" by the function represented by "ma2".
5665 * In other words, plug in "ma2" in "ma1".
5667 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5668 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5670 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5671 &isl_multi_aff_pullback_multi_aff_aligned
);
5674 /* Extend the local space of "dst" to include the divs
5675 * in the local space of "src".
5677 * If "src" does not have any divs or if the local spaces of "dst" and
5678 * "src" are the same, then no extension is required.
5680 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5681 __isl_keep isl_aff
*src
)
5684 int src_n_div
, dst_n_div
;
5691 return isl_aff_free(dst
);
5693 ctx
= isl_aff_get_ctx(src
);
5694 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5696 return isl_aff_free(dst
);
5698 isl_die(ctx
, isl_error_invalid
,
5699 "spaces don't match", goto error
);
5701 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5704 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5706 return isl_aff_free(dst
);
5710 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5711 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5712 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5713 if (!exp1
|| (dst_n_div
&& !exp2
))
5716 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5717 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5725 return isl_aff_free(dst
);
5728 /* Adjust the local spaces of the affine expressions in "maff"
5729 * such that they all have the save divs.
5731 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5732 __isl_take isl_multi_aff
*maff
)
5740 maff
= isl_multi_aff_cow(maff
);
5744 for (i
= 1; i
< maff
->n
; ++i
)
5745 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5746 for (i
= 1; i
< maff
->n
; ++i
) {
5747 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5749 return isl_multi_aff_free(maff
);
5755 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5757 aff
= isl_aff_cow(aff
);
5761 aff
->ls
= isl_local_space_lift(aff
->ls
);
5763 return isl_aff_free(aff
);
5768 /* Lift "maff" to a space with extra dimensions such that the result
5769 * has no more existentially quantified variables.
5770 * If "ls" is not NULL, then *ls is assigned the local space that lies
5771 * at the basis of the lifting applied to "maff".
5773 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5774 __isl_give isl_local_space
**ls
)
5788 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5789 *ls
= isl_local_space_from_space(space
);
5791 return isl_multi_aff_free(maff
);
5796 maff
= isl_multi_aff_cow(maff
);
5797 maff
= isl_multi_aff_align_divs(maff
);
5801 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5802 space
= isl_multi_aff_get_space(maff
);
5803 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5804 space
= isl_space_extend_domain_with_range(space
,
5805 isl_multi_aff_get_space(maff
));
5807 return isl_multi_aff_free(maff
);
5808 isl_space_free(maff
->space
);
5809 maff
->space
= space
;
5812 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5814 return isl_multi_aff_free(maff
);
5817 for (i
= 0; i
< maff
->n
; ++i
) {
5818 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5826 isl_local_space_free(*ls
);
5827 return isl_multi_aff_free(maff
);
5831 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5833 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5834 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5844 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5845 if (pos
< 0 || pos
>= n_out
)
5846 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5847 "index out of bounds", return NULL
);
5849 space
= isl_pw_multi_aff_get_space(pma
);
5850 space
= isl_space_drop_dims(space
, isl_dim_out
,
5851 pos
+ 1, n_out
- pos
- 1);
5852 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5854 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5855 for (i
= 0; i
< pma
->n
; ++i
) {
5857 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5858 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5864 /* Return an isl_pw_multi_aff with the given "set" as domain and
5865 * an unnamed zero-dimensional range.
5867 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5868 __isl_take isl_set
*set
)
5873 space
= isl_set_get_space(set
);
5874 space
= isl_space_from_domain(space
);
5875 ma
= isl_multi_aff_zero(space
);
5876 return isl_pw_multi_aff_alloc(set
, ma
);
5879 /* Add an isl_pw_multi_aff with the given "set" as domain and
5880 * an unnamed zero-dimensional range to *user.
5882 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5885 isl_union_pw_multi_aff
**upma
= user
;
5886 isl_pw_multi_aff
*pma
;
5888 pma
= isl_pw_multi_aff_from_domain(set
);
5889 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5894 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5895 * an unnamed zero-dimensional range.
5897 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5898 __isl_take isl_union_set
*uset
)
5901 isl_union_pw_multi_aff
*upma
;
5906 space
= isl_union_set_get_space(uset
);
5907 upma
= isl_union_pw_multi_aff_empty(space
);
5909 if (isl_union_set_foreach_set(uset
,
5910 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5913 isl_union_set_free(uset
);
5916 isl_union_set_free(uset
);
5917 isl_union_pw_multi_aff_free(upma
);
5921 /* Convert "pma" to an isl_map and add it to *umap.
5923 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5926 isl_union_map
**umap
= user
;
5929 map
= isl_map_from_pw_multi_aff(pma
);
5930 *umap
= isl_union_map_add_map(*umap
, map
);
5935 /* Construct a union map mapping the domain of the union
5936 * piecewise multi-affine expression to its range, with each dimension
5937 * in the range equated to the corresponding affine expression on its cell.
5939 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5940 __isl_take isl_union_pw_multi_aff
*upma
)
5943 isl_union_map
*umap
;
5948 space
= isl_union_pw_multi_aff_get_space(upma
);
5949 umap
= isl_union_map_empty(space
);
5951 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5952 &map_from_pw_multi_aff
, &umap
) < 0)
5955 isl_union_pw_multi_aff_free(upma
);
5958 isl_union_pw_multi_aff_free(upma
);
5959 isl_union_map_free(umap
);
5963 /* Local data for bin_entry and the callback "fn".
5965 struct isl_union_pw_multi_aff_bin_data
{
5966 isl_union_pw_multi_aff
*upma2
;
5967 isl_union_pw_multi_aff
*res
;
5968 isl_pw_multi_aff
*pma
;
5969 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5972 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5973 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5975 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5977 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5981 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5983 isl_pw_multi_aff_free(pma
);
5988 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5989 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5990 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5991 * as *entry. The callback should adjust data->res if desired.
5993 static __isl_give isl_union_pw_multi_aff
*bin_op(
5994 __isl_take isl_union_pw_multi_aff
*upma1
,
5995 __isl_take isl_union_pw_multi_aff
*upma2
,
5996 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5999 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6001 space
= isl_union_pw_multi_aff_get_space(upma2
);
6002 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6003 space
= isl_union_pw_multi_aff_get_space(upma1
);
6004 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6006 if (!upma1
|| !upma2
)
6010 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6011 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6012 &bin_entry
, &data
) < 0)
6015 isl_union_pw_multi_aff_free(upma1
);
6016 isl_union_pw_multi_aff_free(upma2
);
6019 isl_union_pw_multi_aff_free(upma1
);
6020 isl_union_pw_multi_aff_free(upma2
);
6021 isl_union_pw_multi_aff_free(data
.res
);
6025 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6026 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6028 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
6029 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6033 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6034 isl_pw_multi_aff_get_space(pma2
));
6035 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6036 &isl_multi_aff_range_product
);
6039 /* Given two isl_pw_multi_affs A -> B and C -> D,
6040 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6042 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6043 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6045 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6046 &pw_multi_aff_range_product
);
6049 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6050 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6052 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
6053 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6057 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6058 isl_pw_multi_aff_get_space(pma2
));
6059 space
= isl_space_flatten_range(space
);
6060 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6061 &isl_multi_aff_flat_range_product
);
6064 /* Given two isl_pw_multi_affs A -> B and C -> D,
6065 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6067 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6068 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6070 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6071 &pw_multi_aff_flat_range_product
);
6074 /* If data->pma and "pma2" have the same domain space, then compute
6075 * their flat range product and the result to data->res.
6077 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6080 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6082 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6083 pma2
->dim
, isl_dim_in
)) {
6084 isl_pw_multi_aff_free(pma2
);
6088 pma2
= isl_pw_multi_aff_flat_range_product(
6089 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6091 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6096 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6099 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6100 __isl_take isl_union_pw_multi_aff
*upma1
,
6101 __isl_take isl_union_pw_multi_aff
*upma2
)
6103 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6106 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6107 * The parameters are assumed to have been aligned.
6109 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6110 * except that it works on two different isl_pw_* types.
6112 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6113 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6114 __isl_take isl_pw_aff
*pa
)
6117 isl_pw_multi_aff
*res
= NULL
;
6122 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6123 pa
->dim
, isl_dim_in
))
6124 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6125 "domains don't match", goto error
);
6126 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6127 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6128 "index out of bounds", goto error
);
6131 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6133 for (i
= 0; i
< pma
->n
; ++i
) {
6134 for (j
= 0; j
< pa
->n
; ++j
) {
6136 isl_multi_aff
*res_ij
;
6139 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6140 isl_set_copy(pa
->p
[j
].set
));
6141 empty
= isl_set_plain_is_empty(common
);
6142 if (empty
< 0 || empty
) {
6143 isl_set_free(common
);
6149 res_ij
= isl_multi_aff_set_aff(
6150 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6151 isl_aff_copy(pa
->p
[j
].aff
));
6152 res_ij
= isl_multi_aff_gist(res_ij
,
6153 isl_set_copy(common
));
6155 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6159 isl_pw_multi_aff_free(pma
);
6160 isl_pw_aff_free(pa
);
6163 isl_pw_multi_aff_free(pma
);
6164 isl_pw_aff_free(pa
);
6165 return isl_pw_multi_aff_free(res
);
6168 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6170 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6171 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6172 __isl_take isl_pw_aff
*pa
)
6174 isl_bool equal_params
;
6178 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6179 if (equal_params
< 0)
6182 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6183 if (!isl_space_has_named_params(pma
->dim
) ||
6184 !isl_space_has_named_params(pa
->dim
))
6185 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6186 "unaligned unnamed parameters", goto error
);
6187 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6188 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6189 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6191 isl_pw_multi_aff_free(pma
);
6192 isl_pw_aff_free(pa
);
6196 /* Do the parameters of "pa" match those of "space"?
6198 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6199 __isl_keep isl_space
*space
)
6201 isl_space
*pa_space
;
6205 return isl_bool_error
;
6207 pa_space
= isl_pw_aff_get_space(pa
);
6209 match
= isl_space_has_equal_params(space
, pa_space
);
6211 isl_space_free(pa_space
);
6215 /* Check that the domain space of "pa" matches "space".
6217 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6218 __isl_keep isl_space
*space
)
6220 isl_space
*pa_space
;
6224 return isl_stat_error
;
6226 pa_space
= isl_pw_aff_get_space(pa
);
6228 match
= isl_space_has_equal_params(space
, pa_space
);
6232 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6233 "parameters don't match", goto error
);
6234 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6235 pa_space
, isl_dim_in
);
6239 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6240 "domains don't match", goto error
);
6241 isl_space_free(pa_space
);
6244 isl_space_free(pa_space
);
6245 return isl_stat_error
;
6253 #include <isl_multi_templ.c>
6254 #include <isl_multi_apply_set.c>
6255 #include <isl_multi_coalesce.c>
6256 #include <isl_multi_dims.c>
6257 #include <isl_multi_gist.c>
6258 #include <isl_multi_hash.c>
6259 #include <isl_multi_intersect.c>
6261 /* Scale the elements of "pma" by the corresponding elements of "mv".
6263 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6264 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6267 isl_bool equal_params
;
6269 pma
= isl_pw_multi_aff_cow(pma
);
6272 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6273 mv
->space
, isl_dim_set
))
6274 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6275 "spaces don't match", goto error
);
6276 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6277 if (equal_params
< 0)
6279 if (!equal_params
) {
6280 pma
= isl_pw_multi_aff_align_params(pma
,
6281 isl_multi_val_get_space(mv
));
6282 mv
= isl_multi_val_align_params(mv
,
6283 isl_pw_multi_aff_get_space(pma
));
6288 for (i
= 0; i
< pma
->n
; ++i
) {
6289 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6290 isl_multi_val_copy(mv
));
6291 if (!pma
->p
[i
].maff
)
6295 isl_multi_val_free(mv
);
6298 isl_multi_val_free(mv
);
6299 isl_pw_multi_aff_free(pma
);
6303 /* This function is called for each entry of an isl_union_pw_multi_aff.
6304 * If the space of the entry matches that of data->mv,
6305 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6306 * Otherwise, return an empty isl_pw_multi_aff.
6308 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6309 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6311 isl_multi_val
*mv
= user
;
6315 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6316 mv
->space
, isl_dim_set
)) {
6317 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6318 isl_pw_multi_aff_free(pma
);
6319 return isl_pw_multi_aff_empty(space
);
6322 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6325 /* Scale the elements of "upma" by the corresponding elements of "mv",
6326 * for those entries that match the space of "mv".
6328 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6329 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6331 upma
= isl_union_pw_multi_aff_align_params(upma
,
6332 isl_multi_val_get_space(mv
));
6333 mv
= isl_multi_val_align_params(mv
,
6334 isl_union_pw_multi_aff_get_space(upma
));
6338 return isl_union_pw_multi_aff_transform(upma
,
6339 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6341 isl_multi_val_free(mv
);
6344 isl_multi_val_free(mv
);
6345 isl_union_pw_multi_aff_free(upma
);
6349 /* Construct and return a piecewise multi affine expression
6350 * in the given space with value zero in each of the output dimensions and
6351 * a universe domain.
6353 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6355 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6358 /* Construct and return a piecewise multi affine expression
6359 * that is equal to the given piecewise affine expression.
6361 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6362 __isl_take isl_pw_aff
*pa
)
6366 isl_pw_multi_aff
*pma
;
6371 space
= isl_pw_aff_get_space(pa
);
6372 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6374 for (i
= 0; i
< pa
->n
; ++i
) {
6378 set
= isl_set_copy(pa
->p
[i
].set
);
6379 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6380 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6383 isl_pw_aff_free(pa
);
6387 /* Construct a set or map mapping the shared (parameter) domain
6388 * of the piecewise affine expressions to the range of "mpa"
6389 * with each dimension in the range equated to the
6390 * corresponding piecewise affine expression.
6392 static __isl_give isl_map
*map_from_multi_pw_aff(
6393 __isl_take isl_multi_pw_aff
*mpa
)
6402 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6403 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6404 "invalid space", goto error
);
6406 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6407 map
= isl_map_universe(isl_space_from_domain(space
));
6409 for (i
= 0; i
< mpa
->n
; ++i
) {
6413 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6414 map_i
= map_from_pw_aff(pa
);
6416 map
= isl_map_flat_range_product(map
, map_i
);
6419 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6421 isl_multi_pw_aff_free(mpa
);
6424 isl_multi_pw_aff_free(mpa
);
6428 /* Construct a map mapping the shared domain
6429 * of the piecewise affine expressions to the range of "mpa"
6430 * with each dimension in the range equated to the
6431 * corresponding piecewise affine expression.
6433 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6437 if (isl_space_is_set(mpa
->space
))
6438 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6439 "space of input is not a map", goto error
);
6441 return map_from_multi_pw_aff(mpa
);
6443 isl_multi_pw_aff_free(mpa
);
6447 /* Construct a set mapping the shared parameter domain
6448 * of the piecewise affine expressions to the space of "mpa"
6449 * with each dimension in the range equated to the
6450 * corresponding piecewise affine expression.
6452 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6456 if (!isl_space_is_set(mpa
->space
))
6457 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6458 "space of input is not a set", goto error
);
6460 return map_from_multi_pw_aff(mpa
);
6462 isl_multi_pw_aff_free(mpa
);
6466 /* Construct and return a piecewise multi affine expression
6467 * that is equal to the given multi piecewise affine expression
6468 * on the shared domain of the piecewise affine expressions.
6470 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6471 __isl_take isl_multi_pw_aff
*mpa
)
6476 isl_pw_multi_aff
*pma
;
6481 space
= isl_multi_pw_aff_get_space(mpa
);
6484 isl_multi_pw_aff_free(mpa
);
6485 return isl_pw_multi_aff_zero(space
);
6488 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6489 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6491 for (i
= 1; i
< mpa
->n
; ++i
) {
6492 isl_pw_multi_aff
*pma_i
;
6494 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6495 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6496 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6499 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6501 isl_multi_pw_aff_free(mpa
);
6505 /* Construct and return a multi piecewise affine expression
6506 * that is equal to the given multi affine expression.
6508 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6509 __isl_take isl_multi_aff
*ma
)
6512 isl_multi_pw_aff
*mpa
;
6517 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6518 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6520 for (i
= 0; i
< n
; ++i
) {
6523 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6524 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6527 isl_multi_aff_free(ma
);
6531 /* Construct and return a multi piecewise affine expression
6532 * that is equal to the given piecewise multi affine expression.
6534 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6535 __isl_take isl_pw_multi_aff
*pma
)
6539 isl_multi_pw_aff
*mpa
;
6544 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6545 space
= isl_pw_multi_aff_get_space(pma
);
6546 mpa
= isl_multi_pw_aff_alloc(space
);
6548 for (i
= 0; i
< n
; ++i
) {
6551 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6552 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6555 isl_pw_multi_aff_free(pma
);
6559 /* Do "pa1" and "pa2" represent the same function?
6561 * We first check if they are obviously equal.
6562 * If not, we convert them to maps and check if those are equal.
6564 * If "pa1" or "pa2" contain any NaNs, then they are considered
6565 * not to be the same. A NaN is not equal to anything, not even
6568 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6569 __isl_keep isl_pw_aff
*pa2
)
6573 isl_map
*map1
, *map2
;
6576 return isl_bool_error
;
6578 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6579 if (equal
< 0 || equal
)
6581 has_nan
= either_involves_nan(pa1
, pa2
);
6583 return isl_bool_error
;
6585 return isl_bool_false
;
6587 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6588 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6589 equal
= isl_map_is_equal(map1
, map2
);
6596 /* Do "mpa1" and "mpa2" represent the same function?
6598 * Note that we cannot convert the entire isl_multi_pw_aff
6599 * to a map because the domains of the piecewise affine expressions
6600 * may not be the same.
6602 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6603 __isl_keep isl_multi_pw_aff
*mpa2
)
6606 isl_bool equal
, equal_params
;
6609 return isl_bool_error
;
6611 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6612 if (equal_params
< 0)
6613 return isl_bool_error
;
6614 if (!equal_params
) {
6615 if (!isl_space_has_named_params(mpa1
->space
))
6616 return isl_bool_false
;
6617 if (!isl_space_has_named_params(mpa2
->space
))
6618 return isl_bool_false
;
6619 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6620 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6621 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6622 isl_multi_pw_aff_get_space(mpa2
));
6623 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6624 isl_multi_pw_aff_get_space(mpa1
));
6625 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6626 isl_multi_pw_aff_free(mpa1
);
6627 isl_multi_pw_aff_free(mpa2
);
6631 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6632 if (equal
< 0 || !equal
)
6635 for (i
= 0; i
< mpa1
->n
; ++i
) {
6636 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6637 if (equal
< 0 || !equal
)
6641 return isl_bool_true
;
6644 /* Do "pma1" and "pma2" represent the same function?
6646 * First check if they are obviously equal.
6647 * If not, then convert them to maps and check if those are equal.
6649 * If "pa1" or "pa2" contain any NaNs, then they are considered
6650 * not to be the same. A NaN is not equal to anything, not even
6653 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6654 __isl_keep isl_pw_multi_aff
*pma2
)
6658 isl_map
*map1
, *map2
;
6661 return isl_bool_error
;
6663 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6664 if (equal
< 0 || equal
)
6666 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6667 if (has_nan
>= 0 && !has_nan
)
6668 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6669 if (has_nan
< 0 || has_nan
)
6670 return isl_bool_not(has_nan
);
6672 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6673 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6674 equal
= isl_map_is_equal(map1
, map2
);
6681 /* Compute the pullback of "mpa" by the function represented by "ma".
6682 * In other words, plug in "ma" in "mpa".
6684 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6686 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6687 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6690 isl_space
*space
= NULL
;
6692 mpa
= isl_multi_pw_aff_cow(mpa
);
6696 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6697 isl_multi_pw_aff_get_space(mpa
));
6701 for (i
= 0; i
< mpa
->n
; ++i
) {
6702 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6703 isl_multi_aff_copy(ma
));
6708 isl_multi_aff_free(ma
);
6709 isl_space_free(mpa
->space
);
6713 isl_space_free(space
);
6714 isl_multi_pw_aff_free(mpa
);
6715 isl_multi_aff_free(ma
);
6719 /* Compute the pullback of "mpa" by the function represented by "ma".
6720 * In other words, plug in "ma" in "mpa".
6722 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6723 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6725 isl_bool equal_params
;
6729 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6730 if (equal_params
< 0)
6733 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6734 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6735 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6736 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6738 isl_multi_pw_aff_free(mpa
);
6739 isl_multi_aff_free(ma
);
6743 /* Compute the pullback of "mpa" by the function represented by "pma".
6744 * In other words, plug in "pma" in "mpa".
6746 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6748 static __isl_give isl_multi_pw_aff
*
6749 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6750 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6753 isl_space
*space
= NULL
;
6755 mpa
= isl_multi_pw_aff_cow(mpa
);
6759 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6760 isl_multi_pw_aff_get_space(mpa
));
6762 for (i
= 0; i
< mpa
->n
; ++i
) {
6763 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6764 isl_pw_multi_aff_copy(pma
));
6769 isl_pw_multi_aff_free(pma
);
6770 isl_space_free(mpa
->space
);
6774 isl_space_free(space
);
6775 isl_multi_pw_aff_free(mpa
);
6776 isl_pw_multi_aff_free(pma
);
6780 /* Compute the pullback of "mpa" by the function represented by "pma".
6781 * In other words, plug in "pma" in "mpa".
6783 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6784 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6786 isl_bool equal_params
;
6790 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6791 if (equal_params
< 0)
6794 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6795 mpa
= isl_multi_pw_aff_align_params(mpa
,
6796 isl_pw_multi_aff_get_space(pma
));
6797 pma
= isl_pw_multi_aff_align_params(pma
,
6798 isl_multi_pw_aff_get_space(mpa
));
6799 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6801 isl_multi_pw_aff_free(mpa
);
6802 isl_pw_multi_aff_free(pma
);
6806 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6807 * with the domain of "aff". The domain of the result is the same
6809 * "mpa" and "aff" are assumed to have been aligned.
6811 * We first extract the parametric constant from "aff", defined
6812 * over the correct domain.
6813 * Then we add the appropriate combinations of the members of "mpa".
6814 * Finally, we add the integer divisions through recursive calls.
6816 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6817 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6825 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6826 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6828 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6829 tmp
= isl_aff_copy(aff
);
6830 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6831 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6832 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6833 isl_space_dim(space
, isl_dim_set
));
6834 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6835 pa
= isl_pw_aff_from_aff(tmp
);
6837 for (i
= 0; i
< n_in
; ++i
) {
6840 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6842 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6843 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6844 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6845 pa
= isl_pw_aff_add(pa
, pa_i
);
6848 for (i
= 0; i
< n_div
; ++i
) {
6852 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6854 div
= isl_aff_get_div(aff
, i
);
6855 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6856 isl_multi_pw_aff_copy(mpa
), div
);
6857 pa_i
= isl_pw_aff_floor(pa_i
);
6858 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6859 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6860 pa
= isl_pw_aff_add(pa
, pa_i
);
6863 isl_multi_pw_aff_free(mpa
);
6869 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6870 * with the domain of "aff". The domain of the result is the same
6873 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6874 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6876 isl_bool equal_params
;
6880 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6881 if (equal_params
< 0)
6884 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6886 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6887 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6889 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6892 isl_multi_pw_aff_free(mpa
);
6896 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6897 * with the domain of "pa". The domain of the result is the same
6899 * "mpa" and "pa" are assumed to have been aligned.
6901 * We consider each piece in turn. Note that the domains of the
6902 * pieces are assumed to be disjoint and they remain disjoint
6903 * after taking the preimage (over the same function).
6905 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6906 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6915 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6916 isl_pw_aff_get_space(pa
));
6917 res
= isl_pw_aff_empty(space
);
6919 for (i
= 0; i
< pa
->n
; ++i
) {
6923 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6924 isl_multi_pw_aff_copy(mpa
),
6925 isl_aff_copy(pa
->p
[i
].aff
));
6926 domain
= isl_set_copy(pa
->p
[i
].set
);
6927 domain
= isl_set_preimage_multi_pw_aff(domain
,
6928 isl_multi_pw_aff_copy(mpa
));
6929 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6930 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6933 isl_pw_aff_free(pa
);
6934 isl_multi_pw_aff_free(mpa
);
6937 isl_pw_aff_free(pa
);
6938 isl_multi_pw_aff_free(mpa
);
6942 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6943 * with the domain of "pa". The domain of the result is the same
6946 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6947 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6949 isl_bool equal_params
;
6953 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6954 if (equal_params
< 0)
6957 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6959 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6960 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6962 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6964 isl_pw_aff_free(pa
);
6965 isl_multi_pw_aff_free(mpa
);
6969 /* Compute the pullback of "pa" by the function represented by "mpa".
6970 * In other words, plug in "mpa" in "pa".
6971 * "pa" and "mpa" are assumed to have been aligned.
6973 * The pullback is computed by applying "pa" to "mpa".
6975 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6976 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6978 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6981 /* Compute the pullback of "pa" by the function represented by "mpa".
6982 * In other words, plug in "mpa" in "pa".
6984 * The pullback is computed by applying "pa" to "mpa".
6986 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6987 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6989 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6992 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6993 * In other words, plug in "mpa2" in "mpa1".
6995 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6997 * We pullback each member of "mpa1" in turn.
6999 static __isl_give isl_multi_pw_aff
*
7000 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7001 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7004 isl_space
*space
= NULL
;
7006 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7010 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7011 isl_multi_pw_aff_get_space(mpa1
));
7013 for (i
= 0; i
< mpa1
->n
; ++i
) {
7014 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7015 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
7020 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7022 isl_multi_pw_aff_free(mpa2
);
7025 isl_space_free(space
);
7026 isl_multi_pw_aff_free(mpa1
);
7027 isl_multi_pw_aff_free(mpa2
);
7031 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7032 * In other words, plug in "mpa2" in "mpa1".
7034 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7035 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7037 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
7038 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
7041 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7042 * of "mpa1" and "mpa2" live in the same space, construct map space
7043 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7044 * with this map space as extract argument.
7046 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7047 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7048 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7049 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7052 isl_space
*space1
, *space2
;
7055 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7056 isl_multi_pw_aff_get_space(mpa2
));
7057 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7058 isl_multi_pw_aff_get_space(mpa1
));
7061 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7062 mpa2
->space
, isl_dim_out
);
7066 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7067 "range spaces don't match", goto error
);
7068 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7069 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7070 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7072 res
= order(mpa1
, mpa2
, space1
);
7073 isl_multi_pw_aff_free(mpa1
);
7074 isl_multi_pw_aff_free(mpa2
);
7077 isl_multi_pw_aff_free(mpa1
);
7078 isl_multi_pw_aff_free(mpa2
);
7082 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7083 * where the function values are equal. "space" is the space of the result.
7084 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7086 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7087 * in the sequences are equal.
7089 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7090 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7091 __isl_take isl_space
*space
)
7096 res
= isl_map_universe(space
);
7098 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7099 for (i
= 0; i
< n
; ++i
) {
7100 isl_pw_aff
*pa1
, *pa2
;
7103 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7104 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7105 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7106 res
= isl_map_intersect(res
, map
);
7112 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7113 * where the function values are equal.
7115 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7116 __isl_take isl_multi_pw_aff
*mpa2
)
7118 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7119 &isl_multi_pw_aff_eq_map_on_space
);
7122 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7123 * where the function values of "mpa1" is lexicographically satisfies "base"
7124 * compared to that of "mpa2". "space" is the space of the result.
7125 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7127 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7128 * if its i-th element satisfies "base" when compared to
7129 * the i-th element of "mpa2" while all previous elements are
7132 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7133 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7134 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7135 __isl_take isl_pw_aff
*pa2
),
7136 __isl_take isl_space
*space
)
7139 isl_map
*res
, *rest
;
7141 res
= isl_map_empty(isl_space_copy(space
));
7142 rest
= isl_map_universe(space
);
7144 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7145 for (i
= 0; i
< n
; ++i
) {
7146 isl_pw_aff
*pa1
, *pa2
;
7149 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7150 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7151 map
= base(pa1
, pa2
);
7152 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7153 res
= isl_map_union(res
, map
);
7158 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7159 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7160 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7161 rest
= isl_map_intersect(rest
, map
);
7168 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7169 * where the function value of "mpa1" is lexicographically less than that
7170 * of "mpa2". "space" is the space of the result.
7171 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7173 * "mpa1" is less than "mpa2" if its i-th element is smaller
7174 * than the i-th element of "mpa2" while all previous elements are
7177 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7178 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7179 __isl_take isl_space
*space
)
7181 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7182 &isl_pw_aff_lt_map
, space
);
7185 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7186 * where the function value of "mpa1" is lexicographically less than that
7189 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7190 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7192 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7193 &isl_multi_pw_aff_lex_lt_map_on_space
);
7196 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7197 * where the function value of "mpa1" is lexicographically greater than that
7198 * of "mpa2". "space" is the space of the result.
7199 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7201 * "mpa1" is greater than "mpa2" if its i-th element is greater
7202 * than the i-th element of "mpa2" while all previous elements are
7205 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7206 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7207 __isl_take isl_space
*space
)
7209 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7210 &isl_pw_aff_gt_map
, space
);
7213 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7214 * where the function value of "mpa1" is lexicographically greater than that
7217 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7218 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7220 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7221 &isl_multi_pw_aff_lex_gt_map_on_space
);
7224 /* Compare two isl_affs.
7226 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7227 * than "aff2" and 0 if they are equal.
7229 * The order is fairly arbitrary. We do consider expressions that only involve
7230 * earlier dimensions as "smaller".
7232 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7245 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7249 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7250 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7252 return last1
- last2
;
7254 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7257 /* Compare two isl_pw_affs.
7259 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7260 * than "pa2" and 0 if they are equal.
7262 * The order is fairly arbitrary. We do consider expressions that only involve
7263 * earlier dimensions as "smaller".
7265 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7266 __isl_keep isl_pw_aff
*pa2
)
7279 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7283 if (pa1
->n
!= pa2
->n
)
7284 return pa1
->n
- pa2
->n
;
7286 for (i
= 0; i
< pa1
->n
; ++i
) {
7287 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7290 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7298 /* Return a piecewise affine expression that is equal to "v" on "domain".
7300 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7301 __isl_take isl_val
*v
)
7304 isl_local_space
*ls
;
7307 space
= isl_set_get_space(domain
);
7308 ls
= isl_local_space_from_space(space
);
7309 aff
= isl_aff_val_on_domain(ls
, v
);
7311 return isl_pw_aff_alloc(domain
, aff
);
7314 /* Return a multi affine expression that is equal to "mv" on domain
7317 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7318 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7322 isl_local_space
*ls
;
7328 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7329 space2
= isl_multi_val_get_space(mv
);
7330 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7331 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7332 space
= isl_space_map_from_domain_and_range(space
, space2
);
7333 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7334 ls
= isl_local_space_from_space(isl_space_domain(space
));
7335 for (i
= 0; i
< n
; ++i
) {
7339 v
= isl_multi_val_get_val(mv
, i
);
7340 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7341 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7343 isl_local_space_free(ls
);
7345 isl_multi_val_free(mv
);
7348 isl_space_free(space
);
7349 isl_multi_val_free(mv
);
7353 /* Return a piecewise multi-affine expression
7354 * that is equal to "mv" on "domain".
7356 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7357 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7362 space
= isl_set_get_space(domain
);
7363 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7365 return isl_pw_multi_aff_alloc(domain
, ma
);
7368 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7369 * mv is the value that should be attained on each domain set
7370 * res collects the results
7372 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7374 isl_union_pw_multi_aff
*res
;
7377 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7378 * and add it to data->res.
7380 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7383 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7384 isl_pw_multi_aff
*pma
;
7387 mv
= isl_multi_val_copy(data
->mv
);
7388 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7389 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7391 return data
->res
? isl_stat_ok
: isl_stat_error
;
7394 /* Return a union piecewise multi-affine expression
7395 * that is equal to "mv" on "domain".
7397 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7398 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7400 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7403 space
= isl_union_set_get_space(domain
);
7404 data
.res
= isl_union_pw_multi_aff_empty(space
);
7406 if (isl_union_set_foreach_set(domain
,
7407 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7408 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7409 isl_union_set_free(domain
);
7410 isl_multi_val_free(mv
);
7414 /* Compute the pullback of data->pma by the function represented by "pma2",
7415 * provided the spaces match, and add the results to data->res.
7417 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7419 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7421 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7422 pma2
->dim
, isl_dim_out
)) {
7423 isl_pw_multi_aff_free(pma2
);
7427 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7428 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7430 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7432 return isl_stat_error
;
7437 /* Compute the pullback of "upma1" by the function represented by "upma2".
7439 __isl_give isl_union_pw_multi_aff
*
7440 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7441 __isl_take isl_union_pw_multi_aff
*upma1
,
7442 __isl_take isl_union_pw_multi_aff
*upma2
)
7444 return bin_op(upma1
, upma2
, &pullback_entry
);
7447 /* Check that the domain space of "upa" matches "space".
7449 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7450 * can in principle never fail since the space "space" is that
7451 * of the isl_multi_union_pw_aff and is a set space such that
7452 * there is no domain space to match.
7454 * We check the parameters and double-check that "space" is
7455 * indeed that of a set.
7457 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7458 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7460 isl_space
*upa_space
;
7464 return isl_stat_error
;
7466 match
= isl_space_is_set(space
);
7468 return isl_stat_error
;
7470 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7471 "expecting set space", return -1);
7473 upa_space
= isl_union_pw_aff_get_space(upa
);
7474 match
= isl_space_has_equal_params(space
, upa_space
);
7478 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7479 "parameters don't match", goto error
);
7481 isl_space_free(upa_space
);
7484 isl_space_free(upa_space
);
7485 return isl_stat_error
;
7488 /* Do the parameters of "upa" match those of "space"?
7490 static isl_bool
isl_union_pw_aff_matching_params(
7491 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7493 isl_space
*upa_space
;
7497 return isl_bool_error
;
7499 upa_space
= isl_union_pw_aff_get_space(upa
);
7501 match
= isl_space_has_equal_params(space
, upa_space
);
7503 isl_space_free(upa_space
);
7507 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7508 * space represents the new parameters.
7509 * res collects the results.
7511 struct isl_union_pw_aff_reset_params_data
{
7513 isl_union_pw_aff
*res
;
7516 /* Replace the parameters of "pa" by data->space and
7517 * add the result to data->res.
7519 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7521 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7524 space
= isl_pw_aff_get_space(pa
);
7525 space
= isl_space_replace_params(space
, data
->space
);
7526 pa
= isl_pw_aff_reset_space(pa
, space
);
7527 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7529 return data
->res
? isl_stat_ok
: isl_stat_error
;
7532 /* Replace the domain space of "upa" by "space".
7533 * Since a union expression does not have a (single) domain space,
7534 * "space" is necessarily a parameter space.
7536 * Since the order and the names of the parameters determine
7537 * the hash value, we need to create a new hash table.
7539 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7540 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7542 struct isl_union_pw_aff_reset_params_data data
= { space
};
7545 match
= isl_union_pw_aff_matching_params(upa
, space
);
7547 upa
= isl_union_pw_aff_free(upa
);
7549 isl_space_free(space
);
7553 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7554 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7555 data
.res
= isl_union_pw_aff_free(data
.res
);
7557 isl_union_pw_aff_free(upa
);
7558 isl_space_free(space
);
7562 /* Return the floor of "pa".
7564 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7566 return isl_pw_aff_floor(pa
);
7569 /* Given f, return floor(f).
7571 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7572 __isl_take isl_union_pw_aff
*upa
)
7574 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7579 * upa mod m = upa - m * floor(upa/m)
7581 * with m an integer value.
7583 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7584 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7586 isl_union_pw_aff
*res
;
7591 if (!isl_val_is_int(m
))
7592 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7593 "expecting integer modulo", goto error
);
7594 if (!isl_val_is_pos(m
))
7595 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7596 "expecting positive modulo", goto error
);
7598 res
= isl_union_pw_aff_copy(upa
);
7599 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7600 upa
= isl_union_pw_aff_floor(upa
);
7601 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7602 res
= isl_union_pw_aff_sub(res
, upa
);
7607 isl_union_pw_aff_free(upa
);
7611 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7612 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7614 * "res" collects the results.
7616 struct isl_union_pw_aff_aff_on_domain_data
{
7618 isl_union_pw_aff
*res
;
7621 /* Construct a piecewise affine expression that is equal to data->aff
7622 * on "domain" and add the result to data->res.
7624 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7626 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7631 aff
= isl_aff_copy(data
->aff
);
7632 dim
= isl_set_dim(domain
, isl_dim_set
);
7633 aff
= isl_aff_from_range(aff
);
7634 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7635 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7636 pa
= isl_pw_aff_alloc(domain
, aff
);
7637 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7639 return data
->res
? isl_stat_ok
: isl_stat_error
;
7642 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7643 * pos is the output position that needs to be extracted.
7644 * res collects the results.
7646 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7648 isl_union_pw_aff
*res
;
7651 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7652 * (assuming it has such a dimension) and add it to data->res.
7654 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7656 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7661 return isl_stat_error
;
7663 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7664 if (data
->pos
>= n_out
) {
7665 isl_pw_multi_aff_free(pma
);
7669 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7670 isl_pw_multi_aff_free(pma
);
7672 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7674 return data
->res
? isl_stat_ok
: isl_stat_error
;
7677 /* Extract an isl_union_pw_aff corresponding to
7678 * output dimension "pos" of "upma".
7680 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7681 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7683 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7690 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7691 "cannot extract at negative position", return NULL
);
7693 space
= isl_union_pw_multi_aff_get_space(upma
);
7694 data
.res
= isl_union_pw_aff_empty(space
);
7696 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7697 &get_union_pw_aff
, &data
) < 0)
7698 data
.res
= isl_union_pw_aff_free(data
.res
);
7703 /* Return a union piecewise affine expression
7704 * that is equal to "aff" on "domain", assuming "domain" and "aff"
7705 * have been aligned.
7707 * Construct an isl_pw_aff on each of the sets in "domain" and
7708 * collect the results.
7710 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain_aligned(
7711 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7713 struct isl_union_pw_aff_aff_on_domain_data data
;
7716 space
= isl_union_set_get_space(domain
);
7717 data
.res
= isl_union_pw_aff_empty(space
);
7719 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7720 data
.res
= isl_union_pw_aff_free(data
.res
);
7721 isl_union_set_free(domain
);
7726 /* Return a union piecewise affine expression
7727 * that is equal to "aff" on "domain".
7729 * Check that "aff" is a parametric expression,
7730 * align the parameters if needed and call
7731 * isl_union_pw_aff_aff_on_domain_aligned.
7733 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7734 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7736 isl_bool domain_is_params
;
7737 isl_bool equal_params
;
7738 isl_space
*domain_space
, *aff_space
;
7740 if (!domain
|| !aff
)
7742 domain_is_params
= isl_local_space_is_params(aff
->ls
);
7743 if (domain_is_params
< 0)
7745 if (!domain_is_params
)
7746 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7747 "expecting parametric expression", goto error
);
7749 domain_space
= isl_union_set_get_space(domain
);
7750 aff_space
= isl_aff_get_domain_space(aff
);
7751 equal_params
= isl_space_has_equal_params(domain_space
, aff_space
);
7752 if (equal_params
>= 0 && !equal_params
) {
7755 space
= isl_space_align_params(domain_space
, aff_space
);
7756 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
7757 domain
= isl_union_set_align_params(domain
, space
);
7759 isl_space_free(domain_space
);
7760 isl_space_free(aff_space
);
7763 if (equal_params
< 0)
7765 return isl_union_pw_aff_aff_on_domain_aligned(domain
, aff
);
7767 isl_union_set_free(domain
);
7772 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7773 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7774 * "res" collects the results.
7776 struct isl_union_pw_aff_val_on_domain_data
{
7778 isl_union_pw_aff
*res
;
7781 /* Construct a piecewise affine expression that is equal to data->v
7782 * on "domain" and add the result to data->res.
7784 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7786 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7790 v
= isl_val_copy(data
->v
);
7791 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7792 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7794 return data
->res
? isl_stat_ok
: isl_stat_error
;
7797 /* Return a union piecewise affine expression
7798 * that is equal to "v" on "domain".
7800 * Construct an isl_pw_aff on each of the sets in "domain" and
7801 * collect the results.
7803 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7804 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7806 struct isl_union_pw_aff_val_on_domain_data data
;
7809 space
= isl_union_set_get_space(domain
);
7810 data
.res
= isl_union_pw_aff_empty(space
);
7812 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7813 data
.res
= isl_union_pw_aff_free(data
.res
);
7814 isl_union_set_free(domain
);
7819 /* Construct a piecewise multi affine expression
7820 * that is equal to "pa" and add it to upma.
7822 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7825 isl_union_pw_multi_aff
**upma
= user
;
7826 isl_pw_multi_aff
*pma
;
7828 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7829 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7831 return *upma
? isl_stat_ok
: isl_stat_error
;
7834 /* Construct and return a union piecewise multi affine expression
7835 * that is equal to the given union piecewise affine expression.
7837 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7838 __isl_take isl_union_pw_aff
*upa
)
7841 isl_union_pw_multi_aff
*upma
;
7846 space
= isl_union_pw_aff_get_space(upa
);
7847 upma
= isl_union_pw_multi_aff_empty(space
);
7849 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7850 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7851 upma
= isl_union_pw_multi_aff_free(upma
);
7853 isl_union_pw_aff_free(upa
);
7857 /* Compute the set of elements in the domain of "pa" where it is zero and
7858 * add this set to "uset".
7860 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7862 isl_union_set
**uset
= (isl_union_set
**)user
;
7864 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7866 return *uset
? isl_stat_ok
: isl_stat_error
;
7869 /* Return a union set containing those elements in the domain
7870 * of "upa" where it is zero.
7872 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7873 __isl_take isl_union_pw_aff
*upa
)
7875 isl_union_set
*zero
;
7877 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7878 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7879 zero
= isl_union_set_free(zero
);
7881 isl_union_pw_aff_free(upa
);
7885 /* Convert "pa" to an isl_map and add it to *umap.
7887 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7889 isl_union_map
**umap
= user
;
7892 map
= isl_map_from_pw_aff(pa
);
7893 *umap
= isl_union_map_add_map(*umap
, map
);
7895 return *umap
? isl_stat_ok
: isl_stat_error
;
7898 /* Construct a union map mapping the domain of the union
7899 * piecewise affine expression to its range, with the single output dimension
7900 * equated to the corresponding affine expressions on their cells.
7902 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7903 __isl_take isl_union_pw_aff
*upa
)
7906 isl_union_map
*umap
;
7911 space
= isl_union_pw_aff_get_space(upa
);
7912 umap
= isl_union_map_empty(space
);
7914 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7916 umap
= isl_union_map_free(umap
);
7918 isl_union_pw_aff_free(upa
);
7922 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7923 * upma is the function that is plugged in.
7924 * pa is the current part of the function in which upma is plugged in.
7925 * res collects the results.
7927 struct isl_union_pw_aff_pullback_upma_data
{
7928 isl_union_pw_multi_aff
*upma
;
7930 isl_union_pw_aff
*res
;
7933 /* Check if "pma" can be plugged into data->pa.
7934 * If so, perform the pullback and add the result to data->res.
7936 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7938 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7941 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7942 pma
->dim
, isl_dim_out
)) {
7943 isl_pw_multi_aff_free(pma
);
7947 pa
= isl_pw_aff_copy(data
->pa
);
7948 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7950 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7952 return data
->res
? isl_stat_ok
: isl_stat_error
;
7955 /* Check if any of the elements of data->upma can be plugged into pa,
7956 * add if so add the result to data->res.
7958 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7960 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7964 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7966 isl_pw_aff_free(pa
);
7971 /* Compute the pullback of "upa" by the function represented by "upma".
7972 * In other words, plug in "upma" in "upa". The result contains
7973 * expressions defined over the domain space of "upma".
7975 * Run over all pairs of elements in "upa" and "upma", perform
7976 * the pullback when appropriate and collect the results.
7977 * If the hash value were based on the domain space rather than
7978 * the function space, then we could run through all elements
7979 * of "upma" and directly pick out the corresponding element of "upa".
7981 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7982 __isl_take isl_union_pw_aff
*upa
,
7983 __isl_take isl_union_pw_multi_aff
*upma
)
7985 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7988 space
= isl_union_pw_multi_aff_get_space(upma
);
7989 upa
= isl_union_pw_aff_align_params(upa
, space
);
7990 space
= isl_union_pw_aff_get_space(upa
);
7991 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7997 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7998 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7999 data
.res
= isl_union_pw_aff_free(data
.res
);
8001 isl_union_pw_aff_free(upa
);
8002 isl_union_pw_multi_aff_free(upma
);
8005 isl_union_pw_aff_free(upa
);
8006 isl_union_pw_multi_aff_free(upma
);
8011 #define BASE union_pw_aff
8013 #define DOMBASE union_set
8015 #define NO_MOVE_DIMS
8023 #include <isl_multi_templ.c>
8024 #include <isl_multi_apply_set.c>
8025 #include <isl_multi_apply_union_set.c>
8026 #include <isl_multi_coalesce.c>
8027 #include <isl_multi_floor.c>
8028 #include <isl_multi_gist.c>
8029 #include <isl_multi_intersect.c>
8031 /* Construct a multiple union piecewise affine expression
8032 * in the given space with value zero in each of the output dimensions.
8034 * Since there is no canonical zero value for
8035 * a union piecewise affine expression, we can only construct
8036 * a zero-dimensional "zero" value.
8038 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8039 __isl_take isl_space
*space
)
8046 params
= isl_space_is_params(space
);
8050 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8051 "expecting proper set space", goto error
);
8052 if (!isl_space_is_set(space
))
8053 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8054 "expecting set space", goto error
);
8055 if (isl_space_dim(space
, isl_dim_out
) != 0)
8056 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8057 "expecting 0D space", goto error
);
8059 return isl_multi_union_pw_aff_alloc(space
);
8061 isl_space_free(space
);
8065 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8066 * with the actual sum on the shared domain and
8067 * the defined expression on the symmetric difference of the domains.
8069 * We simply iterate over the elements in both arguments and
8070 * call isl_union_pw_aff_union_add on each of them.
8072 static __isl_give isl_multi_union_pw_aff
*
8073 isl_multi_union_pw_aff_union_add_aligned(
8074 __isl_take isl_multi_union_pw_aff
*mupa1
,
8075 __isl_take isl_multi_union_pw_aff
*mupa2
)
8077 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
8078 &isl_union_pw_aff_union_add
);
8081 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8082 * with the actual sum on the shared domain and
8083 * the defined expression on the symmetric difference of the domains.
8085 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
8086 __isl_take isl_multi_union_pw_aff
*mupa1
,
8087 __isl_take isl_multi_union_pw_aff
*mupa2
)
8089 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
8090 &isl_multi_union_pw_aff_union_add_aligned
);
8093 /* Construct and return a multi union piecewise affine expression
8094 * that is equal to the given multi affine expression.
8096 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8097 __isl_take isl_multi_aff
*ma
)
8099 isl_multi_pw_aff
*mpa
;
8101 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8102 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8105 /* Construct and return a multi union piecewise affine expression
8106 * that is equal to the given multi piecewise affine expression.
8108 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8109 __isl_take isl_multi_pw_aff
*mpa
)
8113 isl_multi_union_pw_aff
*mupa
;
8118 space
= isl_multi_pw_aff_get_space(mpa
);
8119 space
= isl_space_range(space
);
8120 mupa
= isl_multi_union_pw_aff_alloc(space
);
8122 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8123 for (i
= 0; i
< n
; ++i
) {
8125 isl_union_pw_aff
*upa
;
8127 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8128 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8129 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8132 isl_multi_pw_aff_free(mpa
);
8137 /* Extract the range space of "pma" and assign it to *space.
8138 * If *space has already been set (through a previous call to this function),
8139 * then check that the range space is the same.
8141 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8143 isl_space
**space
= user
;
8144 isl_space
*pma_space
;
8147 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8148 isl_pw_multi_aff_free(pma
);
8151 return isl_stat_error
;
8157 equal
= isl_space_is_equal(pma_space
, *space
);
8158 isl_space_free(pma_space
);
8161 return isl_stat_error
;
8163 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8164 "range spaces not the same", return isl_stat_error
);
8168 /* Construct and return a multi union piecewise affine expression
8169 * that is equal to the given union piecewise multi affine expression.
8171 * In order to be able to perform the conversion, the input
8172 * needs to be non-empty and may only involve a single range space.
8174 __isl_give isl_multi_union_pw_aff
*
8175 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8176 __isl_take isl_union_pw_multi_aff
*upma
)
8178 isl_space
*space
= NULL
;
8179 isl_multi_union_pw_aff
*mupa
;
8184 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8185 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8186 "cannot extract range space from empty input",
8188 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8195 n
= isl_space_dim(space
, isl_dim_set
);
8196 mupa
= isl_multi_union_pw_aff_alloc(space
);
8198 for (i
= 0; i
< n
; ++i
) {
8199 isl_union_pw_aff
*upa
;
8201 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8202 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8205 isl_union_pw_multi_aff_free(upma
);
8208 isl_space_free(space
);
8209 isl_union_pw_multi_aff_free(upma
);
8213 /* Try and create an isl_multi_union_pw_aff that is equivalent
8214 * to the given isl_union_map.
8215 * The isl_union_map is required to be single-valued in each space.
8216 * Moreover, it cannot be empty and all range spaces need to be the same.
8217 * Otherwise, an error is produced.
8219 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8220 __isl_take isl_union_map
*umap
)
8222 isl_union_pw_multi_aff
*upma
;
8224 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8225 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8228 /* Return a multiple union piecewise affine expression
8229 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8230 * have been aligned.
8232 static __isl_give isl_multi_union_pw_aff
*
8233 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8234 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8238 isl_multi_union_pw_aff
*mupa
;
8243 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8244 space
= isl_multi_val_get_space(mv
);
8245 mupa
= isl_multi_union_pw_aff_alloc(space
);
8246 for (i
= 0; i
< n
; ++i
) {
8248 isl_union_pw_aff
*upa
;
8250 v
= isl_multi_val_get_val(mv
, i
);
8251 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8253 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8256 isl_union_set_free(domain
);
8257 isl_multi_val_free(mv
);
8260 isl_union_set_free(domain
);
8261 isl_multi_val_free(mv
);
8265 /* Return a multiple union piecewise affine expression
8266 * that is equal to "mv" on "domain".
8268 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8269 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8271 isl_bool equal_params
;
8275 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8276 if (equal_params
< 0)
8279 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8281 domain
= isl_union_set_align_params(domain
,
8282 isl_multi_val_get_space(mv
));
8283 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8284 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8286 isl_union_set_free(domain
);
8287 isl_multi_val_free(mv
);
8291 /* Return a multiple union piecewise affine expression
8292 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8293 * have been aligned.
8295 static __isl_give isl_multi_union_pw_aff
*
8296 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8297 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8301 isl_multi_union_pw_aff
*mupa
;
8306 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8307 space
= isl_multi_aff_get_space(ma
);
8308 mupa
= isl_multi_union_pw_aff_alloc(space
);
8309 for (i
= 0; i
< n
; ++i
) {
8311 isl_union_pw_aff
*upa
;
8313 aff
= isl_multi_aff_get_aff(ma
, i
);
8314 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8316 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8319 isl_union_set_free(domain
);
8320 isl_multi_aff_free(ma
);
8323 isl_union_set_free(domain
);
8324 isl_multi_aff_free(ma
);
8328 /* Return a multiple union piecewise affine expression
8329 * that is equal to "ma" on "domain".
8331 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8332 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8334 isl_bool equal_params
;
8338 equal_params
= isl_space_has_equal_params(domain
->dim
, ma
->space
);
8339 if (equal_params
< 0)
8342 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8344 domain
= isl_union_set_align_params(domain
,
8345 isl_multi_aff_get_space(ma
));
8346 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8347 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8349 isl_union_set_free(domain
);
8350 isl_multi_aff_free(ma
);
8354 /* Return a union set containing those elements in the domains
8355 * of the elements of "mupa" where they are all zero.
8357 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8358 __isl_take isl_multi_union_pw_aff
*mupa
)
8361 isl_union_pw_aff
*upa
;
8362 isl_union_set
*zero
;
8367 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8369 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8370 "cannot determine zero set "
8371 "of zero-dimensional function", goto error
);
8373 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8374 zero
= isl_union_pw_aff_zero_union_set(upa
);
8376 for (i
= 1; i
< n
; ++i
) {
8377 isl_union_set
*zero_i
;
8379 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8380 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8382 zero
= isl_union_set_intersect(zero
, zero_i
);
8385 isl_multi_union_pw_aff_free(mupa
);
8388 isl_multi_union_pw_aff_free(mupa
);
8392 /* Construct a union map mapping the shared domain
8393 * of the union piecewise affine expressions to the range of "mupa"
8394 * with each dimension in the range equated to the
8395 * corresponding union piecewise affine expression.
8397 * The input cannot be zero-dimensional as there is
8398 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8400 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8401 __isl_take isl_multi_union_pw_aff
*mupa
)
8405 isl_union_map
*umap
;
8406 isl_union_pw_aff
*upa
;
8411 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8413 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8414 "cannot determine domain of zero-dimensional "
8415 "isl_multi_union_pw_aff", goto error
);
8417 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8418 umap
= isl_union_map_from_union_pw_aff(upa
);
8420 for (i
= 1; i
< n
; ++i
) {
8421 isl_union_map
*umap_i
;
8423 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8424 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8425 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8428 space
= isl_multi_union_pw_aff_get_space(mupa
);
8429 umap
= isl_union_map_reset_range_space(umap
, space
);
8431 isl_multi_union_pw_aff_free(mupa
);
8434 isl_multi_union_pw_aff_free(mupa
);
8438 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8439 * "range" is the space from which to set the range space.
8440 * "res" collects the results.
8442 struct isl_union_pw_multi_aff_reset_range_space_data
{
8444 isl_union_pw_multi_aff
*res
;
8447 /* Replace the range space of "pma" by the range space of data->range and
8448 * add the result to data->res.
8450 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8452 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8455 space
= isl_pw_multi_aff_get_space(pma
);
8456 space
= isl_space_domain(space
);
8457 space
= isl_space_extend_domain_with_range(space
,
8458 isl_space_copy(data
->range
));
8459 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8460 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8462 return data
->res
? isl_stat_ok
: isl_stat_error
;
8465 /* Replace the range space of all the piecewise affine expressions in "upma" by
8466 * the range space of "space".
8468 * This assumes that all these expressions have the same output dimension.
8470 * Since the spaces of the expressions change, so do their hash values.
8471 * We therefore need to create a new isl_union_pw_multi_aff.
8472 * Note that the hash value is currently computed based on the entire
8473 * space even though there can only be a single expression with a given
8476 static __isl_give isl_union_pw_multi_aff
*
8477 isl_union_pw_multi_aff_reset_range_space(
8478 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8480 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8481 isl_space
*space_upma
;
8483 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8484 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8485 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8486 &reset_range_space
, &data
) < 0)
8487 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8489 isl_space_free(space
);
8490 isl_union_pw_multi_aff_free(upma
);
8494 /* Construct and return a union piecewise multi affine expression
8495 * that is equal to the given multi union piecewise affine expression.
8497 * In order to be able to perform the conversion, the input
8498 * needs to have a least one output dimension.
8500 __isl_give isl_union_pw_multi_aff
*
8501 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8502 __isl_take isl_multi_union_pw_aff
*mupa
)
8506 isl_union_pw_multi_aff
*upma
;
8507 isl_union_pw_aff
*upa
;
8512 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8514 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8515 "cannot determine domain of zero-dimensional "
8516 "isl_multi_union_pw_aff", goto error
);
8518 space
= isl_multi_union_pw_aff_get_space(mupa
);
8519 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8520 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8522 for (i
= 1; i
< n
; ++i
) {
8523 isl_union_pw_multi_aff
*upma_i
;
8525 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8526 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8527 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8530 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8532 isl_multi_union_pw_aff_free(mupa
);
8535 isl_multi_union_pw_aff_free(mupa
);
8539 /* Intersect the range of "mupa" with "range".
8540 * That is, keep only those domain elements that have a function value
8543 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8544 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8546 isl_union_pw_multi_aff
*upma
;
8547 isl_union_set
*domain
;
8552 if (!mupa
|| !range
)
8555 space
= isl_set_get_space(range
);
8556 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8557 space
, isl_dim_set
);
8558 isl_space_free(space
);
8562 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8563 "space don't match", goto error
);
8564 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8566 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8567 "cannot intersect range of zero-dimensional "
8568 "isl_multi_union_pw_aff", goto error
);
8570 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8571 isl_multi_union_pw_aff_copy(mupa
));
8572 domain
= isl_union_set_from_set(range
);
8573 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8574 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8578 isl_multi_union_pw_aff_free(mupa
);
8579 isl_set_free(range
);
8583 /* Return the shared domain of the elements of "mupa".
8585 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8586 __isl_take isl_multi_union_pw_aff
*mupa
)
8589 isl_union_pw_aff
*upa
;
8595 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8597 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8598 "cannot determine domain", goto error
);
8600 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8601 dom
= isl_union_pw_aff_domain(upa
);
8602 for (i
= 1; i
< n
; ++i
) {
8603 isl_union_set
*dom_i
;
8605 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8606 dom_i
= isl_union_pw_aff_domain(upa
);
8607 dom
= isl_union_set_intersect(dom
, dom_i
);
8610 isl_multi_union_pw_aff_free(mupa
);
8613 isl_multi_union_pw_aff_free(mupa
);
8617 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8618 * In particular, the spaces have been aligned.
8619 * The result is defined over the shared domain of the elements of "mupa"
8621 * We first extract the parametric constant part of "aff" and
8622 * define that over the shared domain.
8623 * Then we iterate over all input dimensions of "aff" and add the corresponding
8624 * multiples of the elements of "mupa".
8625 * Finally, we consider the integer divisions, calling the function
8626 * recursively to obtain an isl_union_pw_aff corresponding to the
8627 * integer division argument.
8629 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8630 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8633 isl_union_pw_aff
*upa
;
8634 isl_union_set
*uset
;
8638 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8639 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8641 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8642 cst
= isl_aff_copy(aff
);
8643 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8644 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8645 cst
= isl_aff_project_domain_on_params(cst
);
8646 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8648 for (i
= 0; i
< n_in
; ++i
) {
8649 isl_union_pw_aff
*upa_i
;
8651 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8653 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8654 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8655 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8656 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8659 for (i
= 0; i
< n_div
; ++i
) {
8661 isl_union_pw_aff
*upa_i
;
8663 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8665 div
= isl_aff_get_div(aff
, i
);
8666 upa_i
= multi_union_pw_aff_apply_aff(
8667 isl_multi_union_pw_aff_copy(mupa
), div
);
8668 upa_i
= isl_union_pw_aff_floor(upa_i
);
8669 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8670 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8671 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8674 isl_multi_union_pw_aff_free(mupa
);
8680 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8681 * with the domain of "aff".
8682 * Furthermore, the dimension of this space needs to be greater than zero.
8683 * The result is defined over the shared domain of the elements of "mupa"
8685 * We perform these checks and then hand over control to
8686 * multi_union_pw_aff_apply_aff.
8688 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8689 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8691 isl_space
*space1
, *space2
;
8694 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8695 isl_aff_get_space(aff
));
8696 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8700 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8701 space2
= isl_aff_get_domain_space(aff
);
8702 equal
= isl_space_is_equal(space1
, space2
);
8703 isl_space_free(space1
);
8704 isl_space_free(space2
);
8708 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8709 "spaces don't match", goto error
);
8710 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8711 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8712 "cannot determine domains", goto error
);
8714 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8716 isl_multi_union_pw_aff_free(mupa
);
8721 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8722 * with the domain of "ma".
8723 * Furthermore, the dimension of this space needs to be greater than zero,
8724 * unless the dimension of the target space of "ma" is also zero.
8725 * The result is defined over the shared domain of the elements of "mupa"
8727 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8728 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8730 isl_space
*space1
, *space2
;
8731 isl_multi_union_pw_aff
*res
;
8735 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8736 isl_multi_aff_get_space(ma
));
8737 ma
= isl_multi_aff_align_params(ma
,
8738 isl_multi_union_pw_aff_get_space(mupa
));
8742 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8743 space2
= isl_multi_aff_get_domain_space(ma
);
8744 equal
= isl_space_is_equal(space1
, space2
);
8745 isl_space_free(space1
);
8746 isl_space_free(space2
);
8750 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8751 "spaces don't match", goto error
);
8752 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8753 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8754 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8755 "cannot determine domains", goto error
);
8757 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8758 res
= isl_multi_union_pw_aff_alloc(space1
);
8760 for (i
= 0; i
< n_out
; ++i
) {
8762 isl_union_pw_aff
*upa
;
8764 aff
= isl_multi_aff_get_aff(ma
, i
);
8765 upa
= multi_union_pw_aff_apply_aff(
8766 isl_multi_union_pw_aff_copy(mupa
), aff
);
8767 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8770 isl_multi_aff_free(ma
);
8771 isl_multi_union_pw_aff_free(mupa
);
8774 isl_multi_union_pw_aff_free(mupa
);
8775 isl_multi_aff_free(ma
);
8779 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8780 * with the domain of "pa".
8781 * Furthermore, the dimension of this space needs to be greater than zero.
8782 * The result is defined over the shared domain of the elements of "mupa"
8784 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8785 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8789 isl_space
*space
, *space2
;
8790 isl_union_pw_aff
*upa
;
8792 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8793 isl_pw_aff_get_space(pa
));
8794 pa
= isl_pw_aff_align_params(pa
,
8795 isl_multi_union_pw_aff_get_space(mupa
));
8799 space
= isl_multi_union_pw_aff_get_space(mupa
);
8800 space2
= isl_pw_aff_get_domain_space(pa
);
8801 equal
= isl_space_is_equal(space
, space2
);
8802 isl_space_free(space
);
8803 isl_space_free(space2
);
8807 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8808 "spaces don't match", goto error
);
8809 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8810 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8811 "cannot determine domains", goto error
);
8813 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8814 upa
= isl_union_pw_aff_empty(space
);
8816 for (i
= 0; i
< pa
->n
; ++i
) {
8819 isl_multi_union_pw_aff
*mupa_i
;
8820 isl_union_pw_aff
*upa_i
;
8822 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8823 domain
= isl_set_copy(pa
->p
[i
].set
);
8824 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8825 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8826 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8827 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8830 isl_multi_union_pw_aff_free(mupa
);
8831 isl_pw_aff_free(pa
);
8834 isl_multi_union_pw_aff_free(mupa
);
8835 isl_pw_aff_free(pa
);
8839 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8840 * with the domain of "pma".
8841 * Furthermore, the dimension of this space needs to be greater than zero,
8842 * unless the dimension of the target space of "pma" is also zero.
8843 * The result is defined over the shared domain of the elements of "mupa"
8845 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8846 __isl_take isl_multi_union_pw_aff
*mupa
,
8847 __isl_take isl_pw_multi_aff
*pma
)
8849 isl_space
*space1
, *space2
;
8850 isl_multi_union_pw_aff
*res
;
8854 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8855 isl_pw_multi_aff_get_space(pma
));
8856 pma
= isl_pw_multi_aff_align_params(pma
,
8857 isl_multi_union_pw_aff_get_space(mupa
));
8861 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8862 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8863 equal
= isl_space_is_equal(space1
, space2
);
8864 isl_space_free(space1
);
8865 isl_space_free(space2
);
8869 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8870 "spaces don't match", goto error
);
8871 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8872 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8873 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8874 "cannot determine domains", goto error
);
8876 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8877 res
= isl_multi_union_pw_aff_alloc(space1
);
8879 for (i
= 0; i
< n_out
; ++i
) {
8881 isl_union_pw_aff
*upa
;
8883 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8884 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8885 isl_multi_union_pw_aff_copy(mupa
), pa
);
8886 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8889 isl_pw_multi_aff_free(pma
);
8890 isl_multi_union_pw_aff_free(mupa
);
8893 isl_multi_union_pw_aff_free(mupa
);
8894 isl_pw_multi_aff_free(pma
);
8898 /* Compute the pullback of "mupa" by the function represented by "upma".
8899 * In other words, plug in "upma" in "mupa". The result contains
8900 * expressions defined over the domain space of "upma".
8902 * Run over all elements of "mupa" and plug in "upma" in each of them.
8904 __isl_give isl_multi_union_pw_aff
*
8905 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8906 __isl_take isl_multi_union_pw_aff
*mupa
,
8907 __isl_take isl_union_pw_multi_aff
*upma
)
8911 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8912 isl_union_pw_multi_aff_get_space(upma
));
8913 upma
= isl_union_pw_multi_aff_align_params(upma
,
8914 isl_multi_union_pw_aff_get_space(mupa
));
8918 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8919 for (i
= 0; i
< n
; ++i
) {
8920 isl_union_pw_aff
*upa
;
8922 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8923 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8924 isl_union_pw_multi_aff_copy(upma
));
8925 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8928 isl_union_pw_multi_aff_free(upma
);
8931 isl_multi_union_pw_aff_free(mupa
);
8932 isl_union_pw_multi_aff_free(upma
);
8936 /* Extract the sequence of elements in "mupa" with domain space "space"
8937 * (ignoring parameters).
8939 * For the elements of "mupa" that are not defined on the specified space,
8940 * the corresponding element in the result is empty.
8942 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8943 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8946 isl_bool equal_params
;
8947 isl_space
*space_mpa
= NULL
;
8948 isl_multi_pw_aff
*mpa
;
8950 if (!mupa
|| !space
)
8953 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8954 equal_params
= isl_space_has_equal_params(space_mpa
, space
);
8955 if (equal_params
< 0)
8957 if (!equal_params
) {
8958 space
= isl_space_drop_dims(space
, isl_dim_param
,
8959 0, isl_space_dim(space
, isl_dim_param
));
8960 space
= isl_space_align_params(space
,
8961 isl_space_copy(space_mpa
));
8965 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8967 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8969 space
= isl_space_from_domain(space
);
8970 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8971 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8972 for (i
= 0; i
< n
; ++i
) {
8973 isl_union_pw_aff
*upa
;
8976 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8977 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8978 isl_space_copy(space
));
8979 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8980 isl_union_pw_aff_free(upa
);
8983 isl_space_free(space
);
8986 isl_space_free(space_mpa
);
8987 isl_space_free(space
);