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 /* Externally, an isl_aff has a map space, but internally, the
359 * ls field corresponds to the domain of that space.
361 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
362 enum isl_dim_type type
, unsigned pos
)
366 if (type
== isl_dim_out
)
368 if (type
== isl_dim_in
)
370 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
373 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
374 __isl_take isl_space
*dim
)
376 aff
= isl_aff_cow(aff
);
380 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
382 return isl_aff_free(aff
);
391 /* Reset the space of "aff". This function is called from isl_pw_templ.c
392 * and doesn't know if the space of an element object is represented
393 * directly or through its domain. It therefore passes along both.
395 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
396 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
398 isl_space_free(space
);
399 return isl_aff_reset_domain_space(aff
, domain
);
402 /* Reorder the coefficients of the affine expression based
403 * on the given reordering.
404 * The reordering r is assumed to have been extended with the local
407 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
408 __isl_take isl_reordering
*r
, int n_div
)
416 res
= isl_vec_alloc(vec
->ctx
,
417 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
420 isl_seq_cpy(res
->el
, vec
->el
, 2);
421 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
422 for (i
= 0; i
< r
->len
; ++i
)
423 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
425 isl_reordering_free(r
);
430 isl_reordering_free(r
);
434 /* Reorder the dimensions of the domain of "aff" according
435 * to the given reordering.
437 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
438 __isl_take isl_reordering
*r
)
440 aff
= isl_aff_cow(aff
);
444 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
445 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
446 aff
->ls
->div
->n_row
);
447 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
449 if (!aff
->v
|| !aff
->ls
)
450 return isl_aff_free(aff
);
455 isl_reordering_free(r
);
459 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
460 __isl_take isl_space
*model
)
462 isl_bool equal_params
;
467 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
468 if (equal_params
< 0)
473 model
= isl_space_drop_dims(model
, isl_dim_in
,
474 0, isl_space_dim(model
, isl_dim_in
));
475 model
= isl_space_drop_dims(model
, isl_dim_out
,
476 0, isl_space_dim(model
, isl_dim_out
));
477 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
478 exp
= isl_reordering_extend_space(exp
,
479 isl_aff_get_domain_space(aff
));
480 aff
= isl_aff_realign_domain(aff
, exp
);
483 isl_space_free(model
);
486 isl_space_free(model
);
491 /* Is "aff" obviously equal to zero?
493 * If the denominator is zero, then "aff" is not equal to zero.
495 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
498 return isl_bool_error
;
500 if (isl_int_is_zero(aff
->v
->el
[0]))
501 return isl_bool_false
;
502 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
505 /* Does "aff" represent NaN?
507 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
510 return isl_bool_error
;
512 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
515 /* Are "aff1" and "aff2" obviously equal?
517 * NaN is not equal to anything, not even to another NaN.
519 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
520 __isl_keep isl_aff
*aff2
)
525 return isl_bool_error
;
527 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
528 return isl_bool_false
;
530 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
531 if (equal
< 0 || !equal
)
534 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
537 /* Return the common denominator of "aff" in "v".
539 * We cannot return anything meaningful in case of a NaN.
541 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
544 return isl_stat_error
;
545 if (isl_aff_is_nan(aff
))
546 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
547 "cannot get denominator of NaN", return isl_stat_error
);
548 isl_int_set(*v
, aff
->v
->el
[0]);
552 /* Return the common denominator of "aff".
554 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
561 ctx
= isl_aff_get_ctx(aff
);
562 if (isl_aff_is_nan(aff
))
563 return isl_val_nan(ctx
);
564 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
567 /* Return the constant term of "aff".
569 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
577 ctx
= isl_aff_get_ctx(aff
);
578 if (isl_aff_is_nan(aff
))
579 return isl_val_nan(ctx
);
580 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
581 return isl_val_normalize(v
);
584 /* Return the coefficient of the variable of type "type" at position "pos"
587 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
588 enum isl_dim_type type
, int pos
)
596 ctx
= isl_aff_get_ctx(aff
);
597 if (type
== isl_dim_out
)
598 isl_die(ctx
, isl_error_invalid
,
599 "output/set dimension does not have a coefficient",
601 if (type
== isl_dim_in
)
604 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
605 isl_die(ctx
, isl_error_invalid
,
606 "position out of bounds", return NULL
);
608 if (isl_aff_is_nan(aff
))
609 return isl_val_nan(ctx
);
610 pos
+= isl_local_space_offset(aff
->ls
, type
);
611 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
612 return isl_val_normalize(v
);
615 /* Return the sign of the coefficient of the variable of type "type"
616 * at position "pos" of "aff".
618 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
626 ctx
= isl_aff_get_ctx(aff
);
627 if (type
== isl_dim_out
)
628 isl_die(ctx
, isl_error_invalid
,
629 "output/set dimension does not have a coefficient",
631 if (type
== isl_dim_in
)
634 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
635 isl_die(ctx
, isl_error_invalid
,
636 "position out of bounds", return 0);
638 pos
+= isl_local_space_offset(aff
->ls
, type
);
639 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
642 /* Replace the numerator of the constant term of "aff" by "v".
644 * A NaN is unaffected by this operation.
646 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
650 if (isl_aff_is_nan(aff
))
652 aff
= isl_aff_cow(aff
);
656 aff
->v
= isl_vec_cow(aff
->v
);
658 return isl_aff_free(aff
);
660 isl_int_set(aff
->v
->el
[1], v
);
665 /* Replace the constant term of "aff" by "v".
667 * A NaN is unaffected by this operation.
669 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
670 __isl_take isl_val
*v
)
675 if (isl_aff_is_nan(aff
)) {
680 if (!isl_val_is_rat(v
))
681 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
682 "expecting rational value", goto error
);
684 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
685 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
690 aff
= isl_aff_cow(aff
);
693 aff
->v
= isl_vec_cow(aff
->v
);
697 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
698 isl_int_set(aff
->v
->el
[1], v
->n
);
699 } else if (isl_int_is_one(v
->d
)) {
700 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
702 isl_seq_scale(aff
->v
->el
+ 1,
703 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
704 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
705 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
706 aff
->v
= isl_vec_normalize(aff
->v
);
719 /* Add "v" to the constant term of "aff".
721 * A NaN is unaffected by this operation.
723 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
725 if (isl_int_is_zero(v
))
730 if (isl_aff_is_nan(aff
))
732 aff
= isl_aff_cow(aff
);
736 aff
->v
= isl_vec_cow(aff
->v
);
738 return isl_aff_free(aff
);
740 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
745 /* Add "v" to the constant term of "aff".
747 * A NaN is unaffected by this operation.
749 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
750 __isl_take isl_val
*v
)
755 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
760 if (!isl_val_is_rat(v
))
761 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
762 "expecting rational value", goto error
);
764 aff
= isl_aff_cow(aff
);
768 aff
->v
= isl_vec_cow(aff
->v
);
772 if (isl_int_is_one(v
->d
)) {
773 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
774 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
775 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
776 aff
->v
= isl_vec_normalize(aff
->v
);
780 isl_seq_scale(aff
->v
->el
+ 1,
781 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
782 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
783 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
784 aff
->v
= isl_vec_normalize(aff
->v
);
797 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
802 isl_int_set_si(t
, v
);
803 aff
= isl_aff_add_constant(aff
, t
);
809 /* Add "v" to the numerator of the constant term of "aff".
811 * A NaN is unaffected by this operation.
813 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
815 if (isl_int_is_zero(v
))
820 if (isl_aff_is_nan(aff
))
822 aff
= isl_aff_cow(aff
);
826 aff
->v
= isl_vec_cow(aff
->v
);
828 return isl_aff_free(aff
);
830 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
835 /* Add "v" to the numerator of the constant term of "aff".
837 * A NaN is unaffected by this operation.
839 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
847 isl_int_set_si(t
, v
);
848 aff
= isl_aff_add_constant_num(aff
, t
);
854 /* Replace the numerator of the constant term of "aff" by "v".
856 * A NaN is unaffected by this operation.
858 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
862 if (isl_aff_is_nan(aff
))
864 aff
= isl_aff_cow(aff
);
868 aff
->v
= isl_vec_cow(aff
->v
);
870 return isl_aff_free(aff
);
872 isl_int_set_si(aff
->v
->el
[1], v
);
877 /* Replace the numerator of the coefficient of the variable of type "type"
878 * at position "pos" of "aff" by "v".
880 * A NaN is unaffected by this operation.
882 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
883 enum isl_dim_type type
, int pos
, isl_int v
)
888 if (type
== isl_dim_out
)
889 isl_die(aff
->v
->ctx
, isl_error_invalid
,
890 "output/set dimension does not have a coefficient",
891 return isl_aff_free(aff
));
892 if (type
== isl_dim_in
)
895 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
896 isl_die(aff
->v
->ctx
, isl_error_invalid
,
897 "position out of bounds", return isl_aff_free(aff
));
899 if (isl_aff_is_nan(aff
))
901 aff
= isl_aff_cow(aff
);
905 aff
->v
= isl_vec_cow(aff
->v
);
907 return isl_aff_free(aff
);
909 pos
+= isl_local_space_offset(aff
->ls
, type
);
910 isl_int_set(aff
->v
->el
[1 + pos
], v
);
915 /* Replace the numerator of the coefficient of the variable of type "type"
916 * at position "pos" of "aff" by "v".
918 * A NaN is unaffected by this operation.
920 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
921 enum isl_dim_type type
, int pos
, int v
)
926 if (type
== isl_dim_out
)
927 isl_die(aff
->v
->ctx
, isl_error_invalid
,
928 "output/set dimension does not have a coefficient",
929 return isl_aff_free(aff
));
930 if (type
== isl_dim_in
)
933 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
934 isl_die(aff
->v
->ctx
, isl_error_invalid
,
935 "position out of bounds", return isl_aff_free(aff
));
937 if (isl_aff_is_nan(aff
))
939 pos
+= isl_local_space_offset(aff
->ls
, type
);
940 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
943 aff
= isl_aff_cow(aff
);
947 aff
->v
= isl_vec_cow(aff
->v
);
949 return isl_aff_free(aff
);
951 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
956 /* Replace the coefficient of the variable of type "type" at position "pos"
959 * A NaN is unaffected by this operation.
961 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
962 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
967 if (type
== isl_dim_out
)
968 isl_die(aff
->v
->ctx
, isl_error_invalid
,
969 "output/set dimension does not have a coefficient",
971 if (type
== isl_dim_in
)
974 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
975 isl_die(aff
->v
->ctx
, isl_error_invalid
,
976 "position out of bounds", goto error
);
978 if (isl_aff_is_nan(aff
)) {
982 if (!isl_val_is_rat(v
))
983 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
984 "expecting rational value", goto error
);
986 pos
+= isl_local_space_offset(aff
->ls
, type
);
987 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
988 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
993 aff
= isl_aff_cow(aff
);
996 aff
->v
= isl_vec_cow(aff
->v
);
1000 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1001 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1002 } else if (isl_int_is_one(v
->d
)) {
1003 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1005 isl_seq_scale(aff
->v
->el
+ 1,
1006 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1007 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1008 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1009 aff
->v
= isl_vec_normalize(aff
->v
);
1022 /* Add "v" to the coefficient of the variable of type "type"
1023 * at position "pos" of "aff".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1028 enum isl_dim_type type
, int pos
, isl_int v
)
1033 if (type
== isl_dim_out
)
1034 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1035 "output/set dimension does not have a coefficient",
1036 return isl_aff_free(aff
));
1037 if (type
== isl_dim_in
)
1040 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1041 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1042 "position out of bounds", return isl_aff_free(aff
));
1044 if (isl_aff_is_nan(aff
))
1046 aff
= isl_aff_cow(aff
);
1050 aff
->v
= isl_vec_cow(aff
->v
);
1052 return isl_aff_free(aff
);
1054 pos
+= isl_local_space_offset(aff
->ls
, type
);
1055 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1060 /* Add "v" to the coefficient of the variable of type "type"
1061 * at position "pos" of "aff".
1063 * A NaN is unaffected by this operation.
1065 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1066 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1071 if (isl_val_is_zero(v
)) {
1076 if (type
== isl_dim_out
)
1077 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1078 "output/set dimension does not have a coefficient",
1080 if (type
== isl_dim_in
)
1083 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1084 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1085 "position out of bounds", goto error
);
1087 if (isl_aff_is_nan(aff
)) {
1091 if (!isl_val_is_rat(v
))
1092 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1093 "expecting rational value", goto error
);
1095 aff
= isl_aff_cow(aff
);
1099 aff
->v
= isl_vec_cow(aff
->v
);
1103 pos
+= isl_local_space_offset(aff
->ls
, type
);
1104 if (isl_int_is_one(v
->d
)) {
1105 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1106 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1107 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1108 aff
->v
= isl_vec_normalize(aff
->v
);
1112 isl_seq_scale(aff
->v
->el
+ 1,
1113 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1114 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1115 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1116 aff
->v
= isl_vec_normalize(aff
->v
);
1129 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1130 enum isl_dim_type type
, int pos
, int v
)
1135 isl_int_set_si(t
, v
);
1136 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1142 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1147 return isl_local_space_get_div(aff
->ls
, pos
);
1150 /* Return the negation of "aff".
1152 * As a special case, -NaN = NaN.
1154 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1158 if (isl_aff_is_nan(aff
))
1160 aff
= isl_aff_cow(aff
);
1163 aff
->v
= isl_vec_cow(aff
->v
);
1165 return isl_aff_free(aff
);
1167 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1172 /* Remove divs from the local space that do not appear in the affine
1174 * We currently only remove divs at the end.
1175 * Some intermediate divs may also not appear directly in the affine
1176 * expression, but we would also need to check that no other divs are
1177 * defined in terms of them.
1179 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1188 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1189 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1191 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1195 aff
= isl_aff_cow(aff
);
1199 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1200 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1201 if (!aff
->ls
|| !aff
->v
)
1202 return isl_aff_free(aff
);
1207 /* Look for any divs in the aff->ls with a denominator equal to one
1208 * and plug them into the affine expression and any subsequent divs
1209 * that may reference the div.
1211 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1217 isl_local_space
*ls
;
1223 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1225 for (i
= 0; i
< n
; ++i
) {
1226 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1228 ls
= isl_local_space_copy(aff
->ls
);
1229 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1230 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1231 vec
= isl_vec_copy(aff
->v
);
1232 vec
= isl_vec_cow(vec
);
1238 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1239 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1244 isl_vec_free(aff
->v
);
1246 isl_local_space_free(aff
->ls
);
1253 isl_local_space_free(ls
);
1254 return isl_aff_free(aff
);
1257 /* Look for any divs j that appear with a unit coefficient inside
1258 * the definitions of other divs i and plug them into the definitions
1261 * In particular, an expression of the form
1263 * floor((f(..) + floor(g(..)/n))/m)
1267 * floor((n * f(..) + g(..))/(n * m))
1269 * This simplification is correct because we can move the expression
1270 * f(..) into the inner floor in the original expression to obtain
1272 * floor(floor((n * f(..) + g(..))/n)/m)
1274 * from which we can derive the simplified expression.
1276 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1284 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1285 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1286 for (i
= 1; i
< n
; ++i
) {
1287 for (j
= 0; j
< i
; ++j
) {
1288 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1290 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1291 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1292 aff
->v
->size
, i
, 1);
1294 return isl_aff_free(aff
);
1301 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1303 * Even though this function is only called on isl_affs with a single
1304 * reference, we are careful to only change aff->v and aff->ls together.
1306 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1308 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1309 isl_local_space
*ls
;
1312 ls
= isl_local_space_copy(aff
->ls
);
1313 ls
= isl_local_space_swap_div(ls
, a
, b
);
1314 v
= isl_vec_copy(aff
->v
);
1319 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1320 isl_vec_free(aff
->v
);
1322 isl_local_space_free(aff
->ls
);
1328 isl_local_space_free(ls
);
1329 return isl_aff_free(aff
);
1332 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1334 * We currently do not actually remove div "b", but simply add its
1335 * coefficient to that of "a" and then zero it out.
1337 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1339 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1341 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1344 aff
->v
= isl_vec_cow(aff
->v
);
1346 return isl_aff_free(aff
);
1348 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1349 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1350 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1355 /* Sort the divs in the local space of "aff" according to
1356 * the comparison function "cmp_row" in isl_local_space.c,
1357 * combining the coefficients of identical divs.
1359 * Reordering divs does not change the semantics of "aff",
1360 * so there is no need to call isl_aff_cow.
1361 * Moreover, this function is currently only called on isl_affs
1362 * with a single reference.
1364 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1371 n
= isl_aff_dim(aff
, isl_dim_div
);
1372 for (i
= 1; i
< n
; ++i
) {
1373 for (j
= i
- 1; j
>= 0; --j
) {
1374 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1378 aff
= merge_divs(aff
, j
, j
+ 1);
1380 aff
= swap_div(aff
, j
, j
+ 1);
1389 /* Normalize the representation of "aff".
1391 * This function should only be called of "new" isl_affs, i.e.,
1392 * with only a single reference. We therefore do not need to
1393 * worry about affecting other instances.
1395 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1399 aff
->v
= isl_vec_normalize(aff
->v
);
1401 return isl_aff_free(aff
);
1402 aff
= plug_in_integral_divs(aff
);
1403 aff
= plug_in_unit_divs(aff
);
1404 aff
= sort_divs(aff
);
1405 aff
= isl_aff_remove_unused_divs(aff
);
1409 /* Given f, return floor(f).
1410 * If f is an integer expression, then just return f.
1411 * If f is a constant, then return the constant floor(f).
1412 * Otherwise, if f = g/m, write g = q m + r,
1413 * create a new div d = [r/m] and return the expression q + d.
1414 * The coefficients in r are taken to lie between -m/2 and m/2.
1416 * reduce_div_coefficients performs the same normalization.
1418 * As a special case, floor(NaN) = NaN.
1420 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1430 if (isl_aff_is_nan(aff
))
1432 if (isl_int_is_one(aff
->v
->el
[0]))
1435 aff
= isl_aff_cow(aff
);
1439 aff
->v
= isl_vec_cow(aff
->v
);
1441 return isl_aff_free(aff
);
1443 if (isl_aff_is_cst(aff
)) {
1444 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1445 isl_int_set_si(aff
->v
->el
[0], 1);
1449 div
= isl_vec_copy(aff
->v
);
1450 div
= isl_vec_cow(div
);
1452 return isl_aff_free(aff
);
1454 ctx
= isl_aff_get_ctx(aff
);
1455 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1456 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1457 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1458 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1459 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1460 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1461 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1465 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1467 return isl_aff_free(aff
);
1469 size
= aff
->v
->size
;
1470 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1472 return isl_aff_free(aff
);
1473 isl_int_set_si(aff
->v
->el
[0], 1);
1474 isl_int_set_si(aff
->v
->el
[size
], 1);
1476 aff
= isl_aff_normalize(aff
);
1483 * aff mod m = aff - m * floor(aff/m)
1485 * with m an integer value.
1487 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1488 __isl_take isl_val
*m
)
1495 if (!isl_val_is_int(m
))
1496 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1497 "expecting integer modulo", goto error
);
1499 res
= isl_aff_copy(aff
);
1500 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1501 aff
= isl_aff_floor(aff
);
1502 aff
= isl_aff_scale_val(aff
, m
);
1503 res
= isl_aff_sub(res
, aff
);
1514 * pwaff mod m = pwaff - m * floor(pwaff/m)
1516 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1520 res
= isl_pw_aff_copy(pwaff
);
1521 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1522 pwaff
= isl_pw_aff_floor(pwaff
);
1523 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1524 res
= isl_pw_aff_sub(res
, pwaff
);
1531 * pa mod m = pa - m * floor(pa/m)
1533 * with m an integer value.
1535 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1536 __isl_take isl_val
*m
)
1540 if (!isl_val_is_int(m
))
1541 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1542 "expecting integer modulo", goto error
);
1543 pa
= isl_pw_aff_mod(pa
, m
->n
);
1547 isl_pw_aff_free(pa
);
1552 /* Given f, return ceil(f).
1553 * If f is an integer expression, then just return f.
1554 * Otherwise, let f be the expression
1560 * floor((e + m - 1)/m)
1562 * As a special case, ceil(NaN) = NaN.
1564 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1569 if (isl_aff_is_nan(aff
))
1571 if (isl_int_is_one(aff
->v
->el
[0]))
1574 aff
= isl_aff_cow(aff
);
1577 aff
->v
= isl_vec_cow(aff
->v
);
1579 return isl_aff_free(aff
);
1581 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1582 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1583 aff
= isl_aff_floor(aff
);
1588 /* Apply the expansion computed by isl_merge_divs.
1589 * The expansion itself is given by "exp" while the resulting
1590 * list of divs is given by "div".
1592 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1593 __isl_take isl_mat
*div
, int *exp
)
1599 aff
= isl_aff_cow(aff
);
1603 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1604 new_n_div
= isl_mat_rows(div
);
1605 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1607 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1608 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1609 if (!aff
->v
|| !aff
->ls
)
1610 return isl_aff_free(aff
);
1618 /* Add two affine expressions that live in the same local space.
1620 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1621 __isl_take isl_aff
*aff2
)
1625 aff1
= isl_aff_cow(aff1
);
1629 aff1
->v
= isl_vec_cow(aff1
->v
);
1635 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1636 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1637 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1638 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1639 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1640 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1641 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1653 /* Return the sum of "aff1" and "aff2".
1655 * If either of the two is NaN, then the result is NaN.
1657 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1658 __isl_take isl_aff
*aff2
)
1669 ctx
= isl_aff_get_ctx(aff1
);
1670 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1671 isl_die(ctx
, isl_error_invalid
,
1672 "spaces don't match", goto error
);
1674 if (isl_aff_is_nan(aff1
)) {
1678 if (isl_aff_is_nan(aff2
)) {
1683 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1684 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1685 if (n_div1
== 0 && n_div2
== 0)
1686 return add_expanded(aff1
, aff2
);
1688 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1689 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1690 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1693 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1694 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1695 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1699 return add_expanded(aff1
, aff2
);
1708 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1709 __isl_take isl_aff
*aff2
)
1711 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1714 /* Return the result of scaling "aff" by a factor of "f".
1716 * As a special case, f * NaN = NaN.
1718 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1724 if (isl_aff_is_nan(aff
))
1727 if (isl_int_is_one(f
))
1730 aff
= isl_aff_cow(aff
);
1733 aff
->v
= isl_vec_cow(aff
->v
);
1735 return isl_aff_free(aff
);
1737 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1738 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1743 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1744 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1745 isl_int_divexact(gcd
, f
, gcd
);
1746 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1752 /* Multiple "aff" by "v".
1754 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1755 __isl_take isl_val
*v
)
1760 if (isl_val_is_one(v
)) {
1765 if (!isl_val_is_rat(v
))
1766 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1767 "expecting rational factor", goto error
);
1769 aff
= isl_aff_scale(aff
, v
->n
);
1770 aff
= isl_aff_scale_down(aff
, v
->d
);
1780 /* Return the result of scaling "aff" down by a factor of "f".
1782 * As a special case, NaN/f = NaN.
1784 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1790 if (isl_aff_is_nan(aff
))
1793 if (isl_int_is_one(f
))
1796 aff
= isl_aff_cow(aff
);
1800 if (isl_int_is_zero(f
))
1801 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1802 "cannot scale down by zero", return isl_aff_free(aff
));
1804 aff
->v
= isl_vec_cow(aff
->v
);
1806 return isl_aff_free(aff
);
1809 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1810 isl_int_gcd(gcd
, gcd
, f
);
1811 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1812 isl_int_divexact(gcd
, f
, gcd
);
1813 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1819 /* Divide "aff" by "v".
1821 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1822 __isl_take isl_val
*v
)
1827 if (isl_val_is_one(v
)) {
1832 if (!isl_val_is_rat(v
))
1833 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1834 "expecting rational factor", goto error
);
1835 if (!isl_val_is_pos(v
))
1836 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1837 "factor needs to be positive", goto error
);
1839 aff
= isl_aff_scale(aff
, v
->d
);
1840 aff
= isl_aff_scale_down(aff
, v
->n
);
1850 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1858 isl_int_set_ui(v
, f
);
1859 aff
= isl_aff_scale_down(aff
, v
);
1865 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1866 enum isl_dim_type type
, unsigned pos
, const char *s
)
1868 aff
= isl_aff_cow(aff
);
1871 if (type
== isl_dim_out
)
1872 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1873 "cannot set name of output/set dimension",
1874 return isl_aff_free(aff
));
1875 if (type
== isl_dim_in
)
1877 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1879 return isl_aff_free(aff
);
1884 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1885 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1887 aff
= isl_aff_cow(aff
);
1890 if (type
== isl_dim_out
)
1891 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1892 "cannot set name of output/set dimension",
1894 if (type
== isl_dim_in
)
1896 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1898 return isl_aff_free(aff
);
1907 /* Replace the identifier of the input tuple of "aff" by "id".
1908 * type is currently required to be equal to isl_dim_in
1910 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1911 enum isl_dim_type type
, __isl_take isl_id
*id
)
1913 aff
= isl_aff_cow(aff
);
1916 if (type
!= isl_dim_out
)
1917 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1918 "cannot only set id of input tuple", goto error
);
1919 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
1921 return isl_aff_free(aff
);
1930 /* Exploit the equalities in "eq" to simplify the affine expression
1931 * and the expressions of the integer divisions in the local space.
1932 * The integer divisions in this local space are assumed to appear
1933 * as regular dimensions in "eq".
1935 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1936 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1944 if (eq
->n_eq
== 0) {
1945 isl_basic_set_free(eq
);
1949 aff
= isl_aff_cow(aff
);
1953 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1954 isl_basic_set_copy(eq
));
1955 aff
->v
= isl_vec_cow(aff
->v
);
1956 if (!aff
->ls
|| !aff
->v
)
1959 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1961 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1962 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1963 if (j
< 0 || j
== 0 || j
>= total
)
1966 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1970 isl_basic_set_free(eq
);
1971 aff
= isl_aff_normalize(aff
);
1974 isl_basic_set_free(eq
);
1979 /* Exploit the equalities in "eq" to simplify the affine expression
1980 * and the expressions of the integer divisions in the local space.
1982 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
1983 __isl_take isl_basic_set
*eq
)
1989 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1991 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
1992 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1994 isl_basic_set_free(eq
);
1999 /* Look for equalities among the variables shared by context and aff
2000 * and the integer divisions of aff, if any.
2001 * The equalities are then used to eliminate coefficients and/or integer
2002 * divisions from aff.
2004 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2005 __isl_take isl_set
*context
)
2007 isl_basic_set
*hull
;
2012 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2014 isl_basic_set
*bset
;
2015 isl_local_space
*ls
;
2016 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2017 ls
= isl_aff_get_domain_local_space(aff
);
2018 bset
= isl_basic_set_from_local_space(ls
);
2019 bset
= isl_basic_set_lift(bset
);
2020 bset
= isl_basic_set_flatten(bset
);
2021 context
= isl_set_intersect(context
,
2022 isl_set_from_basic_set(bset
));
2025 hull
= isl_set_affine_hull(context
);
2026 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2029 isl_set_free(context
);
2033 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2034 __isl_take isl_set
*context
)
2036 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2037 dom_context
= isl_set_intersect_params(dom_context
, context
);
2038 return isl_aff_gist(aff
, dom_context
);
2041 /* Return a basic set containing those elements in the space
2042 * of aff where it is positive. "rational" should not be set.
2044 * If "aff" is NaN, then it is not positive.
2046 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2049 isl_constraint
*ineq
;
2050 isl_basic_set
*bset
;
2055 if (isl_aff_is_nan(aff
)) {
2056 isl_space
*space
= isl_aff_get_domain_space(aff
);
2058 return isl_basic_set_empty(space
);
2061 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2062 "rational sets not supported", goto error
);
2064 ineq
= isl_inequality_from_aff(aff
);
2065 c
= isl_constraint_get_constant_val(ineq
);
2066 c
= isl_val_sub_ui(c
, 1);
2067 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2069 bset
= isl_basic_set_from_constraint(ineq
);
2070 bset
= isl_basic_set_simplify(bset
);
2077 /* Return a basic set containing those elements in the space
2078 * of aff where it is non-negative.
2079 * If "rational" is set, then return a rational basic set.
2081 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2083 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2084 __isl_take isl_aff
*aff
, int rational
)
2086 isl_constraint
*ineq
;
2087 isl_basic_set
*bset
;
2091 if (isl_aff_is_nan(aff
)) {
2092 isl_space
*space
= isl_aff_get_domain_space(aff
);
2094 return isl_basic_set_empty(space
);
2097 ineq
= isl_inequality_from_aff(aff
);
2099 bset
= isl_basic_set_from_constraint(ineq
);
2101 bset
= isl_basic_set_set_rational(bset
);
2102 bset
= isl_basic_set_simplify(bset
);
2106 /* Return a basic set containing those elements in the space
2107 * of aff where it is non-negative.
2109 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2111 return aff_nonneg_basic_set(aff
, 0);
2114 /* Return a basic set containing those elements in the domain space
2115 * of "aff" where it is positive.
2117 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2119 aff
= isl_aff_add_constant_num_si(aff
, -1);
2120 return isl_aff_nonneg_basic_set(aff
);
2123 /* Return a basic set containing those elements in the domain space
2124 * of aff where it is negative.
2126 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2128 aff
= isl_aff_neg(aff
);
2129 return isl_aff_pos_basic_set(aff
);
2132 /* Return a basic set containing those elements in the space
2133 * of aff where it is zero.
2134 * If "rational" is set, then return a rational basic set.
2136 * If "aff" is NaN, then it is not zero.
2138 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2141 isl_constraint
*ineq
;
2142 isl_basic_set
*bset
;
2146 if (isl_aff_is_nan(aff
)) {
2147 isl_space
*space
= isl_aff_get_domain_space(aff
);
2149 return isl_basic_set_empty(space
);
2152 ineq
= isl_equality_from_aff(aff
);
2154 bset
= isl_basic_set_from_constraint(ineq
);
2156 bset
= isl_basic_set_set_rational(bset
);
2157 bset
= isl_basic_set_simplify(bset
);
2161 /* Return a basic set containing those elements in the space
2162 * of aff where it is zero.
2164 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2166 return aff_zero_basic_set(aff
, 0);
2169 /* Return a basic set containing those elements in the shared space
2170 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2172 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2173 __isl_take isl_aff
*aff2
)
2175 aff1
= isl_aff_sub(aff1
, aff2
);
2177 return isl_aff_nonneg_basic_set(aff1
);
2180 /* Return a basic set containing those elements in the shared domain space
2181 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2183 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2184 __isl_take isl_aff
*aff2
)
2186 aff1
= isl_aff_sub(aff1
, aff2
);
2188 return isl_aff_pos_basic_set(aff1
);
2191 /* Return a set containing those elements in the shared space
2192 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2194 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2195 __isl_take isl_aff
*aff2
)
2197 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2200 /* Return a set containing those elements in the shared domain space
2201 * of aff1 and aff2 where aff1 is greater than aff2.
2203 * If either of the two inputs is NaN, then the result is empty,
2204 * as comparisons with NaN always return false.
2206 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2207 __isl_take isl_aff
*aff2
)
2209 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2212 /* Return a basic set containing those elements in the shared space
2213 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2215 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2216 __isl_take isl_aff
*aff2
)
2218 return isl_aff_ge_basic_set(aff2
, aff1
);
2221 /* Return a basic set containing those elements in the shared domain space
2222 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2224 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2225 __isl_take isl_aff
*aff2
)
2227 return isl_aff_gt_basic_set(aff2
, aff1
);
2230 /* Return a set containing those elements in the shared space
2231 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2233 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2234 __isl_take isl_aff
*aff2
)
2236 return isl_aff_ge_set(aff2
, aff1
);
2239 /* Return a set containing those elements in the shared domain space
2240 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2242 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2243 __isl_take isl_aff
*aff2
)
2245 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2248 /* Return a basic set containing those elements in the shared space
2249 * of aff1 and aff2 where aff1 and aff2 are equal.
2251 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2252 __isl_take isl_aff
*aff2
)
2254 aff1
= isl_aff_sub(aff1
, aff2
);
2256 return isl_aff_zero_basic_set(aff1
);
2259 /* Return a set containing those elements in the shared space
2260 * of aff1 and aff2 where aff1 and aff2 are equal.
2262 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2263 __isl_take isl_aff
*aff2
)
2265 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2268 /* Return a set containing those elements in the shared domain space
2269 * of aff1 and aff2 where aff1 and aff2 are not equal.
2271 * If either of the two inputs is NaN, then the result is empty,
2272 * as comparisons with NaN always return false.
2274 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2275 __isl_take isl_aff
*aff2
)
2277 isl_set
*set_lt
, *set_gt
;
2279 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2280 isl_aff_copy(aff2
));
2281 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2282 return isl_set_union_disjoint(set_lt
, set_gt
);
2285 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2286 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2288 aff1
= isl_aff_add(aff1
, aff2
);
2289 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2293 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2301 /* Check whether the given affine expression has non-zero coefficient
2302 * for any dimension in the given range or if any of these dimensions
2303 * appear with non-zero coefficients in any of the integer divisions
2304 * involved in the affine expression.
2306 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2307 enum isl_dim_type type
, unsigned first
, unsigned n
)
2312 isl_bool involves
= isl_bool_false
;
2315 return isl_bool_error
;
2317 return isl_bool_false
;
2319 ctx
= isl_aff_get_ctx(aff
);
2320 if (first
+ n
> isl_aff_dim(aff
, type
))
2321 isl_die(ctx
, isl_error_invalid
,
2322 "range out of bounds", return isl_bool_error
);
2324 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2328 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2329 for (i
= 0; i
< n
; ++i
)
2330 if (active
[first
+ i
]) {
2331 involves
= isl_bool_true
;
2340 return isl_bool_error
;
2343 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2344 enum isl_dim_type type
, unsigned first
, unsigned n
)
2350 if (type
== isl_dim_out
)
2351 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2352 "cannot drop output/set dimension",
2353 return isl_aff_free(aff
));
2354 if (type
== isl_dim_in
)
2356 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2359 ctx
= isl_aff_get_ctx(aff
);
2360 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2361 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2362 return isl_aff_free(aff
));
2364 aff
= isl_aff_cow(aff
);
2368 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2370 return isl_aff_free(aff
);
2372 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2373 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2375 return isl_aff_free(aff
);
2380 /* Project the domain of the affine expression onto its parameter space.
2381 * The affine expression may not involve any of the domain dimensions.
2383 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2389 n
= isl_aff_dim(aff
, isl_dim_in
);
2390 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2392 return isl_aff_free(aff
);
2394 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2395 "affine expression involves some of the domain dimensions",
2396 return isl_aff_free(aff
));
2397 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2398 space
= isl_aff_get_domain_space(aff
);
2399 space
= isl_space_params(space
);
2400 aff
= isl_aff_reset_domain_space(aff
, space
);
2404 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2405 enum isl_dim_type type
, unsigned first
, unsigned n
)
2411 if (type
== isl_dim_out
)
2412 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2413 "cannot insert output/set dimensions",
2414 return isl_aff_free(aff
));
2415 if (type
== isl_dim_in
)
2417 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2420 ctx
= isl_aff_get_ctx(aff
);
2421 if (first
> isl_local_space_dim(aff
->ls
, type
))
2422 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2423 return isl_aff_free(aff
));
2425 aff
= isl_aff_cow(aff
);
2429 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2431 return isl_aff_free(aff
);
2433 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2434 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2436 return isl_aff_free(aff
);
2441 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2442 enum isl_dim_type type
, unsigned n
)
2446 pos
= isl_aff_dim(aff
, type
);
2448 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2451 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2452 enum isl_dim_type type
, unsigned n
)
2456 pos
= isl_pw_aff_dim(pwaff
, type
);
2458 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2461 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2462 * to dimensions of "dst_type" at "dst_pos".
2464 * We only support moving input dimensions to parameters and vice versa.
2466 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2467 enum isl_dim_type dst_type
, unsigned dst_pos
,
2468 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2476 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2477 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2480 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2481 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2482 "cannot move output/set dimension",
2483 return isl_aff_free(aff
));
2484 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2485 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2486 "cannot move divs", return isl_aff_free(aff
));
2487 if (dst_type
== isl_dim_in
)
2488 dst_type
= isl_dim_set
;
2489 if (src_type
== isl_dim_in
)
2490 src_type
= isl_dim_set
;
2492 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2493 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2494 "range out of bounds", return isl_aff_free(aff
));
2495 if (dst_type
== src_type
)
2496 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2497 "moving dims within the same type not supported",
2498 return isl_aff_free(aff
));
2500 aff
= isl_aff_cow(aff
);
2504 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2505 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2506 if (dst_type
> src_type
)
2509 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2510 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2511 src_type
, src_pos
, n
);
2512 if (!aff
->v
|| !aff
->ls
)
2513 return isl_aff_free(aff
);
2515 aff
= sort_divs(aff
);
2520 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2522 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2523 return isl_pw_aff_alloc(dom
, aff
);
2526 #define isl_aff_involves_nan isl_aff_is_nan
2529 #define PW isl_pw_aff
2533 #define EL_IS_ZERO is_empty
2537 #define IS_ZERO is_empty
2540 #undef DEFAULT_IS_ZERO
2541 #define DEFAULT_IS_ZERO 0
2548 #include <isl_pw_templ.c>
2549 #include <isl_pw_hash.c>
2550 #include <isl_pw_union_opt.c>
2553 #define UNION isl_union_pw_aff
2555 #define PART isl_pw_aff
2557 #define PARTS pw_aff
2559 #include <isl_union_single.c>
2560 #include <isl_union_neg.c>
2562 static __isl_give isl_set
*align_params_pw_pw_set_and(
2563 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2564 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2565 __isl_take isl_pw_aff
*pwaff2
))
2567 isl_bool equal_params
;
2569 if (!pwaff1
|| !pwaff2
)
2571 equal_params
= isl_space_has_equal_params(pwaff1
->dim
, pwaff2
->dim
);
2572 if (equal_params
< 0)
2575 return fn(pwaff1
, pwaff2
);
2576 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2577 !isl_space_has_named_params(pwaff2
->dim
))
2578 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2579 "unaligned unnamed parameters", goto error
);
2580 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2581 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2582 return fn(pwaff1
, pwaff2
);
2584 isl_pw_aff_free(pwaff1
);
2585 isl_pw_aff_free(pwaff2
);
2589 /* Align the parameters of the to isl_pw_aff arguments and
2590 * then apply a function "fn" on them that returns an isl_map.
2592 static __isl_give isl_map
*align_params_pw_pw_map_and(
2593 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2594 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2595 __isl_take isl_pw_aff
*pa2
))
2597 isl_bool equal_params
;
2601 equal_params
= isl_space_has_equal_params(pa1
->dim
, pa2
->dim
);
2602 if (equal_params
< 0)
2605 return fn(pa1
, pa2
);
2606 if (!isl_space_has_named_params(pa1
->dim
) ||
2607 !isl_space_has_named_params(pa2
->dim
))
2608 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2609 "unaligned unnamed parameters", goto error
);
2610 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2611 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2612 return fn(pa1
, pa2
);
2614 isl_pw_aff_free(pa1
);
2615 isl_pw_aff_free(pa2
);
2619 /* Compute a piecewise quasi-affine expression with a domain that
2620 * is the union of those of pwaff1 and pwaff2 and such that on each
2621 * cell, the quasi-affine expression is the maximum of those of pwaff1
2622 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2623 * cell, then the associated expression is the defined one.
2625 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2626 __isl_take isl_pw_aff
*pwaff2
)
2628 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2631 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2632 __isl_take isl_pw_aff
*pwaff2
)
2634 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2638 /* Compute a piecewise quasi-affine expression with a domain that
2639 * is the union of those of pwaff1 and pwaff2 and such that on each
2640 * cell, the quasi-affine expression is the minimum of those of pwaff1
2641 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2642 * cell, then the associated expression is the defined one.
2644 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2645 __isl_take isl_pw_aff
*pwaff2
)
2647 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2650 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2651 __isl_take isl_pw_aff
*pwaff2
)
2653 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2657 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2658 __isl_take isl_pw_aff
*pwaff2
, int max
)
2661 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2663 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2666 /* Construct a map with as domain the domain of pwaff and
2667 * one-dimensional range corresponding to the affine expressions.
2669 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2678 dim
= isl_pw_aff_get_space(pwaff
);
2679 map
= isl_map_empty(dim
);
2681 for (i
= 0; i
< pwaff
->n
; ++i
) {
2682 isl_basic_map
*bmap
;
2685 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2686 map_i
= isl_map_from_basic_map(bmap
);
2687 map_i
= isl_map_intersect_domain(map_i
,
2688 isl_set_copy(pwaff
->p
[i
].set
));
2689 map
= isl_map_union_disjoint(map
, map_i
);
2692 isl_pw_aff_free(pwaff
);
2697 /* Construct a map with as domain the domain of pwaff and
2698 * one-dimensional range corresponding to the affine expressions.
2700 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2704 if (isl_space_is_set(pwaff
->dim
))
2705 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2706 "space of input is not a map", goto error
);
2707 return map_from_pw_aff(pwaff
);
2709 isl_pw_aff_free(pwaff
);
2713 /* Construct a one-dimensional set with as parameter domain
2714 * the domain of pwaff and the single set dimension
2715 * corresponding to the affine expressions.
2717 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2721 if (!isl_space_is_set(pwaff
->dim
))
2722 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2723 "space of input is not a set", goto error
);
2724 return map_from_pw_aff(pwaff
);
2726 isl_pw_aff_free(pwaff
);
2730 /* Return a set containing those elements in the domain
2731 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2732 * does not satisfy "fn" (if complement is 1).
2734 * The pieces with a NaN never belong to the result since
2735 * NaN does not satisfy any property.
2737 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2738 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2747 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2749 for (i
= 0; i
< pwaff
->n
; ++i
) {
2750 isl_basic_set
*bset
;
2751 isl_set
*set_i
, *locus
;
2754 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2757 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2758 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2759 locus
= isl_set_from_basic_set(bset
);
2760 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2762 set_i
= isl_set_subtract(set_i
, locus
);
2764 set_i
= isl_set_intersect(set_i
, locus
);
2765 set
= isl_set_union_disjoint(set
, set_i
);
2768 isl_pw_aff_free(pwaff
);
2773 /* Return a set containing those elements in the domain
2774 * of "pa" where it is positive.
2776 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2778 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2781 /* Return a set containing those elements in the domain
2782 * of pwaff where it is non-negative.
2784 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2786 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2789 /* Return a set containing those elements in the domain
2790 * of pwaff where it is zero.
2792 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2794 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2797 /* Return a set containing those elements in the domain
2798 * of pwaff where it is not zero.
2800 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2802 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2805 /* Return a set containing those elements in the shared domain
2806 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2808 * We compute the difference on the shared domain and then construct
2809 * the set of values where this difference is non-negative.
2810 * If strict is set, we first subtract 1 from the difference.
2811 * If equal is set, we only return the elements where pwaff1 and pwaff2
2814 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2815 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2817 isl_set
*set1
, *set2
;
2819 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2820 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2821 set1
= isl_set_intersect(set1
, set2
);
2822 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2823 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2824 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2827 isl_space
*dim
= isl_set_get_space(set1
);
2829 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2830 aff
= isl_aff_add_constant_si(aff
, -1);
2831 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2836 return isl_pw_aff_zero_set(pwaff1
);
2837 return isl_pw_aff_nonneg_set(pwaff1
);
2840 /* Return a set containing those elements in the shared domain
2841 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2843 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2844 __isl_take isl_pw_aff
*pwaff2
)
2846 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2849 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2850 __isl_take isl_pw_aff
*pwaff2
)
2852 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2855 /* Return a set containing those elements in the shared domain
2856 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2858 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2859 __isl_take isl_pw_aff
*pwaff2
)
2861 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2864 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2865 __isl_take isl_pw_aff
*pwaff2
)
2867 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2870 /* Return a set containing those elements in the shared domain
2871 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2873 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2874 __isl_take isl_pw_aff
*pwaff2
)
2876 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2879 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2880 __isl_take isl_pw_aff
*pwaff2
)
2882 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2885 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2886 __isl_take isl_pw_aff
*pwaff2
)
2888 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2891 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2892 __isl_take isl_pw_aff
*pwaff2
)
2894 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2897 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2898 * where the function values are ordered in the same way as "order",
2899 * which returns a set in the shared domain of its two arguments.
2900 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2902 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2903 * We first pull back the two functions such that they are defined on
2904 * the domain [A -> B]. Then we apply "order", resulting in a set
2905 * in the space [A -> B]. Finally, we unwrap this set to obtain
2906 * a map in the space A -> B.
2908 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2909 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2910 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2911 __isl_take isl_pw_aff
*pa2
))
2913 isl_space
*space1
, *space2
;
2917 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2918 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2919 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2920 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2921 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2922 ma
= isl_multi_aff_range_map(space1
);
2923 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2924 set
= order(pa1
, pa2
);
2926 return isl_set_unwrap(set
);
2929 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2930 * where the function values are equal.
2931 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2933 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2934 __isl_take isl_pw_aff
*pa2
)
2936 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2939 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2940 * where the function values are equal.
2942 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2943 __isl_take isl_pw_aff
*pa2
)
2945 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
2948 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2949 * where the function value of "pa1" is less than the function value of "pa2".
2950 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2952 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
2953 __isl_take isl_pw_aff
*pa2
)
2955 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
2958 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2959 * where the function value of "pa1" is less than the function value of "pa2".
2961 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
2962 __isl_take isl_pw_aff
*pa2
)
2964 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
2967 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2968 * where the function value of "pa1" is greater than the function value
2970 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2972 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
2973 __isl_take isl_pw_aff
*pa2
)
2975 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
2978 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2979 * where the function value of "pa1" is greater than the function value
2982 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
2983 __isl_take isl_pw_aff
*pa2
)
2985 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
2988 /* Return a set containing those elements in the shared domain
2989 * of the elements of list1 and list2 where each element in list1
2990 * has the relation specified by "fn" with each element in list2.
2992 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2993 __isl_take isl_pw_aff_list
*list2
,
2994 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2995 __isl_take isl_pw_aff
*pwaff2
))
3001 if (!list1
|| !list2
)
3004 ctx
= isl_pw_aff_list_get_ctx(list1
);
3005 if (list1
->n
< 1 || list2
->n
< 1)
3006 isl_die(ctx
, isl_error_invalid
,
3007 "list should contain at least one element", goto error
);
3009 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3010 for (i
= 0; i
< list1
->n
; ++i
)
3011 for (j
= 0; j
< list2
->n
; ++j
) {
3014 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3015 isl_pw_aff_copy(list2
->p
[j
]));
3016 set
= isl_set_intersect(set
, set_ij
);
3019 isl_pw_aff_list_free(list1
);
3020 isl_pw_aff_list_free(list2
);
3023 isl_pw_aff_list_free(list1
);
3024 isl_pw_aff_list_free(list2
);
3028 /* Return a set containing those elements in the shared domain
3029 * of the elements of list1 and list2 where each element in list1
3030 * is equal to each element in list2.
3032 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3033 __isl_take isl_pw_aff_list
*list2
)
3035 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3038 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3039 __isl_take isl_pw_aff_list
*list2
)
3041 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3044 /* Return a set containing those elements in the shared domain
3045 * of the elements of list1 and list2 where each element in list1
3046 * is less than or equal to each element in list2.
3048 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3049 __isl_take isl_pw_aff_list
*list2
)
3051 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3054 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3055 __isl_take isl_pw_aff_list
*list2
)
3057 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3060 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3061 __isl_take isl_pw_aff_list
*list2
)
3063 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3066 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3067 __isl_take isl_pw_aff_list
*list2
)
3069 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3073 /* Return a set containing those elements in the shared domain
3074 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3076 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3077 __isl_take isl_pw_aff
*pwaff2
)
3079 isl_set
*set_lt
, *set_gt
;
3081 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3082 isl_pw_aff_copy(pwaff2
));
3083 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3084 return isl_set_union_disjoint(set_lt
, set_gt
);
3087 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3088 __isl_take isl_pw_aff
*pwaff2
)
3090 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3093 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3098 if (isl_int_is_one(v
))
3100 if (!isl_int_is_pos(v
))
3101 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3102 "factor needs to be positive",
3103 return isl_pw_aff_free(pwaff
));
3104 pwaff
= isl_pw_aff_cow(pwaff
);
3110 for (i
= 0; i
< pwaff
->n
; ++i
) {
3111 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3112 if (!pwaff
->p
[i
].aff
)
3113 return isl_pw_aff_free(pwaff
);
3119 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3123 pwaff
= isl_pw_aff_cow(pwaff
);
3129 for (i
= 0; i
< pwaff
->n
; ++i
) {
3130 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3131 if (!pwaff
->p
[i
].aff
)
3132 return isl_pw_aff_free(pwaff
);
3138 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3142 pwaff
= isl_pw_aff_cow(pwaff
);
3148 for (i
= 0; i
< pwaff
->n
; ++i
) {
3149 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3150 if (!pwaff
->p
[i
].aff
)
3151 return isl_pw_aff_free(pwaff
);
3157 /* Assuming that "cond1" and "cond2" are disjoint,
3158 * return an affine expression that is equal to pwaff1 on cond1
3159 * and to pwaff2 on cond2.
3161 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3162 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3163 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3165 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3166 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3168 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3171 /* Return an affine expression that is equal to pwaff_true for elements
3172 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3174 * That is, return cond ? pwaff_true : pwaff_false;
3176 * If "cond" involves and NaN, then we conservatively return a NaN
3177 * on its entire domain. In principle, we could consider the pieces
3178 * where it is NaN separately from those where it is not.
3180 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3181 * then only use the domain of "cond" to restrict the domain.
3183 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3184 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3186 isl_set
*cond_true
, *cond_false
;
3191 if (isl_pw_aff_involves_nan(cond
)) {
3192 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3193 isl_local_space
*ls
= isl_local_space_from_space(space
);
3194 isl_pw_aff_free(cond
);
3195 isl_pw_aff_free(pwaff_true
);
3196 isl_pw_aff_free(pwaff_false
);
3197 return isl_pw_aff_nan_on_domain(ls
);
3200 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3201 isl_pw_aff_get_space(pwaff_false
));
3202 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3203 isl_pw_aff_get_space(pwaff_true
));
3204 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3210 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3211 isl_pw_aff_free(pwaff_false
);
3212 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3215 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3216 cond_false
= isl_pw_aff_zero_set(cond
);
3217 return isl_pw_aff_select(cond_true
, pwaff_true
,
3218 cond_false
, pwaff_false
);
3220 isl_pw_aff_free(cond
);
3221 isl_pw_aff_free(pwaff_true
);
3222 isl_pw_aff_free(pwaff_false
);
3226 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3229 return isl_bool_error
;
3231 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3234 /* Check whether pwaff is a piecewise constant.
3236 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3241 return isl_bool_error
;
3243 for (i
= 0; i
< pwaff
->n
; ++i
) {
3244 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3245 if (is_cst
< 0 || !is_cst
)
3249 return isl_bool_true
;
3252 /* Are all elements of "mpa" piecewise constants?
3254 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3259 return isl_bool_error
;
3261 for (i
= 0; i
< mpa
->n
; ++i
) {
3262 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3263 if (is_cst
< 0 || !is_cst
)
3267 return isl_bool_true
;
3270 /* Return the product of "aff1" and "aff2".
3272 * If either of the two is NaN, then the result is NaN.
3274 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3276 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3277 __isl_take isl_aff
*aff2
)
3282 if (isl_aff_is_nan(aff1
)) {
3286 if (isl_aff_is_nan(aff2
)) {
3291 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3292 return isl_aff_mul(aff2
, aff1
);
3294 if (!isl_aff_is_cst(aff2
))
3295 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3296 "at least one affine expression should be constant",
3299 aff1
= isl_aff_cow(aff1
);
3303 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3304 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3314 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3316 * If either of the two is NaN, then the result is NaN.
3318 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3319 __isl_take isl_aff
*aff2
)
3327 if (isl_aff_is_nan(aff1
)) {
3331 if (isl_aff_is_nan(aff2
)) {
3336 is_cst
= isl_aff_is_cst(aff2
);
3340 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3341 "second argument should be a constant", goto error
);
3346 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3348 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3349 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3352 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3353 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3356 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3357 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3368 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3369 __isl_take isl_pw_aff
*pwaff2
)
3371 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3374 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3375 __isl_take isl_pw_aff
*pwaff2
)
3377 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3380 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3381 __isl_take isl_pw_aff
*pwaff2
)
3383 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3386 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3387 __isl_take isl_pw_aff
*pwaff2
)
3389 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3392 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3393 __isl_take isl_pw_aff
*pwaff2
)
3395 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3398 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3399 __isl_take isl_pw_aff
*pa2
)
3401 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3404 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3406 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3407 __isl_take isl_pw_aff
*pa2
)
3411 is_cst
= isl_pw_aff_is_cst(pa2
);
3415 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3416 "second argument should be a piecewise constant",
3418 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3420 isl_pw_aff_free(pa1
);
3421 isl_pw_aff_free(pa2
);
3425 /* Compute the quotient of the integer division of "pa1" by "pa2"
3426 * with rounding towards zero.
3427 * "pa2" is assumed to be a piecewise constant.
3429 * In particular, return
3431 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3434 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3435 __isl_take isl_pw_aff
*pa2
)
3441 is_cst
= isl_pw_aff_is_cst(pa2
);
3445 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3446 "second argument should be a piecewise constant",
3449 pa1
= isl_pw_aff_div(pa1
, pa2
);
3451 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3452 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3453 c
= isl_pw_aff_ceil(pa1
);
3454 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3456 isl_pw_aff_free(pa1
);
3457 isl_pw_aff_free(pa2
);
3461 /* Compute the remainder of the integer division of "pa1" by "pa2"
3462 * with rounding towards zero.
3463 * "pa2" is assumed to be a piecewise constant.
3465 * In particular, return
3467 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3470 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3471 __isl_take isl_pw_aff
*pa2
)
3476 is_cst
= isl_pw_aff_is_cst(pa2
);
3480 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3481 "second argument should be a piecewise constant",
3483 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3484 res
= isl_pw_aff_mul(pa2
, res
);
3485 res
= isl_pw_aff_sub(pa1
, res
);
3488 isl_pw_aff_free(pa1
);
3489 isl_pw_aff_free(pa2
);
3493 /* Does either of "pa1" or "pa2" involve any NaN2?
3495 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3496 __isl_keep isl_pw_aff
*pa2
)
3500 has_nan
= isl_pw_aff_involves_nan(pa1
);
3501 if (has_nan
< 0 || has_nan
)
3503 return isl_pw_aff_involves_nan(pa2
);
3506 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3507 * by a NaN on their shared domain.
3509 * In principle, the result could be refined to only being NaN
3510 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3512 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3513 __isl_take isl_pw_aff
*pa2
)
3515 isl_local_space
*ls
;
3519 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3520 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3521 pa
= isl_pw_aff_nan_on_domain(ls
);
3522 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3527 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3528 __isl_take isl_pw_aff
*pwaff2
)
3533 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3534 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3535 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3536 isl_pw_aff_copy(pwaff2
));
3537 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3538 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3541 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3542 __isl_take isl_pw_aff
*pwaff2
)
3547 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3548 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3549 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3550 isl_pw_aff_copy(pwaff2
));
3551 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3552 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3555 /* Return an expression for the minimum (if "max" is not set) or
3556 * the maximum (if "max" is set) of "pa1" and "pa2".
3557 * If either expression involves any NaN, then return a NaN
3558 * on the shared domain as result.
3560 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3561 __isl_take isl_pw_aff
*pa2
, int max
)
3565 has_nan
= either_involves_nan(pa1
, pa2
);
3567 pa1
= isl_pw_aff_free(pa1
);
3569 return replace_by_nan(pa1
, pa2
);
3572 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_max
);
3574 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_min
);
3577 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3579 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3580 __isl_take isl_pw_aff
*pwaff2
)
3582 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3585 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3587 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3588 __isl_take isl_pw_aff
*pwaff2
)
3590 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3593 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3594 __isl_take isl_pw_aff_list
*list
,
3595 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3596 __isl_take isl_pw_aff
*pwaff2
))
3605 ctx
= isl_pw_aff_list_get_ctx(list
);
3607 isl_die(ctx
, isl_error_invalid
,
3608 "list should contain at least one element", goto error
);
3610 res
= isl_pw_aff_copy(list
->p
[0]);
3611 for (i
= 1; i
< list
->n
; ++i
)
3612 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3614 isl_pw_aff_list_free(list
);
3617 isl_pw_aff_list_free(list
);
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the minimal corresponding affine
3625 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3627 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3630 /* Return an isl_pw_aff that maps each element in the intersection of the
3631 * domains of the elements of list to the maximal corresponding affine
3634 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3636 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3639 /* Mark the domains of "pwaff" as rational.
3641 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3645 pwaff
= isl_pw_aff_cow(pwaff
);
3651 for (i
= 0; i
< pwaff
->n
; ++i
) {
3652 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3653 if (!pwaff
->p
[i
].set
)
3654 return isl_pw_aff_free(pwaff
);
3660 /* Mark the domains of the elements of "list" as rational.
3662 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3663 __isl_take isl_pw_aff_list
*list
)
3673 for (i
= 0; i
< n
; ++i
) {
3676 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3677 pa
= isl_pw_aff_set_rational(pa
);
3678 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3684 /* Do the parameters of "aff" match those of "space"?
3686 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3687 __isl_keep isl_space
*space
)
3689 isl_space
*aff_space
;
3693 return isl_bool_error
;
3695 aff_space
= isl_aff_get_domain_space(aff
);
3697 match
= isl_space_has_equal_params(space
, aff_space
);
3699 isl_space_free(aff_space
);
3703 /* Check that the domain space of "aff" matches "space".
3705 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3706 __isl_keep isl_space
*space
)
3708 isl_space
*aff_space
;
3712 return isl_stat_error
;
3714 aff_space
= isl_aff_get_domain_space(aff
);
3716 match
= isl_space_has_equal_params(space
, aff_space
);
3720 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3721 "parameters don't match", goto error
);
3722 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3723 aff_space
, isl_dim_set
);
3727 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3728 "domains don't match", goto error
);
3729 isl_space_free(aff_space
);
3732 isl_space_free(aff_space
);
3733 return isl_stat_error
;
3742 #include <isl_multi_templ.c>
3743 #include <isl_multi_apply_set.c>
3744 #include <isl_multi_cmp.c>
3745 #include <isl_multi_floor.c>
3746 #include <isl_multi_gist.c>
3750 /* Construct an isl_multi_aff living in "space" that corresponds
3751 * to the affine transformation matrix "mat".
3753 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3754 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3757 isl_local_space
*ls
= NULL
;
3758 isl_multi_aff
*ma
= NULL
;
3759 int n_row
, n_col
, n_out
, total
;
3765 ctx
= isl_mat_get_ctx(mat
);
3767 n_row
= isl_mat_rows(mat
);
3768 n_col
= isl_mat_cols(mat
);
3770 isl_die(ctx
, isl_error_invalid
,
3771 "insufficient number of rows", goto error
);
3773 isl_die(ctx
, isl_error_invalid
,
3774 "insufficient number of columns", goto error
);
3775 n_out
= isl_space_dim(space
, isl_dim_out
);
3776 total
= isl_space_dim(space
, isl_dim_all
);
3777 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3778 isl_die(ctx
, isl_error_invalid
,
3779 "dimension mismatch", goto error
);
3781 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3782 ls
= isl_local_space_from_space(isl_space_domain(space
));
3784 for (i
= 0; i
< n_row
- 1; ++i
) {
3788 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3791 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3792 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3793 v
= isl_vec_normalize(v
);
3794 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3795 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3798 isl_local_space_free(ls
);
3802 isl_local_space_free(ls
);
3804 isl_multi_aff_free(ma
);
3808 /* Remove any internal structure of the domain of "ma".
3809 * If there is any such internal structure in the input,
3810 * then the name of the corresponding space is also removed.
3812 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3813 __isl_take isl_multi_aff
*ma
)
3820 if (!ma
->space
->nested
[0])
3823 space
= isl_multi_aff_get_space(ma
);
3824 space
= isl_space_flatten_domain(space
);
3825 ma
= isl_multi_aff_reset_space(ma
, space
);
3830 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3831 * of the space to its domain.
3833 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3836 isl_local_space
*ls
;
3841 if (!isl_space_is_map(space
))
3842 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3843 "not a map space", goto error
);
3845 n_in
= isl_space_dim(space
, isl_dim_in
);
3846 space
= isl_space_domain_map(space
);
3848 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3850 isl_space_free(space
);
3854 space
= isl_space_domain(space
);
3855 ls
= isl_local_space_from_space(space
);
3856 for (i
= 0; i
< n_in
; ++i
) {
3859 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3861 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3863 isl_local_space_free(ls
);
3866 isl_space_free(space
);
3870 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3871 * of the space to its range.
3873 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3876 isl_local_space
*ls
;
3881 if (!isl_space_is_map(space
))
3882 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3883 "not a map space", goto error
);
3885 n_in
= isl_space_dim(space
, isl_dim_in
);
3886 n_out
= isl_space_dim(space
, isl_dim_out
);
3887 space
= isl_space_range_map(space
);
3889 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3891 isl_space_free(space
);
3895 space
= isl_space_domain(space
);
3896 ls
= isl_local_space_from_space(space
);
3897 for (i
= 0; i
< n_out
; ++i
) {
3900 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3901 isl_dim_set
, n_in
+ i
);
3902 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3904 isl_local_space_free(ls
);
3907 isl_space_free(space
);
3911 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3912 * of the space to its range.
3914 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3915 __isl_take isl_space
*space
)
3917 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3920 /* Given the space of a set and a range of set dimensions,
3921 * construct an isl_multi_aff that projects out those dimensions.
3923 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3924 __isl_take isl_space
*space
, enum isl_dim_type type
,
3925 unsigned first
, unsigned n
)
3928 isl_local_space
*ls
;
3933 if (!isl_space_is_set(space
))
3934 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3935 "expecting set space", goto error
);
3936 if (type
!= isl_dim_set
)
3937 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3938 "only set dimensions can be projected out", goto error
);
3940 dim
= isl_space_dim(space
, isl_dim_set
);
3941 if (first
+ n
> dim
)
3942 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3943 "range out of bounds", goto error
);
3945 space
= isl_space_from_domain(space
);
3946 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3949 return isl_multi_aff_alloc(space
);
3951 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3952 space
= isl_space_domain(space
);
3953 ls
= isl_local_space_from_space(space
);
3955 for (i
= 0; i
< first
; ++i
) {
3958 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3960 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3963 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3966 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3967 isl_dim_set
, first
+ n
+ i
);
3968 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3971 isl_local_space_free(ls
);
3974 isl_space_free(space
);
3978 /* Given the space of a set and a range of set dimensions,
3979 * construct an isl_pw_multi_aff that projects out those dimensions.
3981 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3982 __isl_take isl_space
*space
, enum isl_dim_type type
,
3983 unsigned first
, unsigned n
)
3987 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3988 return isl_pw_multi_aff_from_multi_aff(ma
);
3991 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3994 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3995 __isl_take isl_multi_aff
*ma
)
3997 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3998 return isl_pw_multi_aff_alloc(dom
, ma
);
4001 /* Create a piecewise multi-affine expression in the given space that maps each
4002 * input dimension to the corresponding output dimension.
4004 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4005 __isl_take isl_space
*space
)
4007 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4010 /* Exploit the equalities in "eq" to simplify the affine expressions.
4012 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4013 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4017 maff
= isl_multi_aff_cow(maff
);
4021 for (i
= 0; i
< maff
->n
; ++i
) {
4022 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
4023 isl_basic_set_copy(eq
));
4028 isl_basic_set_free(eq
);
4031 isl_basic_set_free(eq
);
4032 isl_multi_aff_free(maff
);
4036 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4041 maff
= isl_multi_aff_cow(maff
);
4045 for (i
= 0; i
< maff
->n
; ++i
) {
4046 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
4048 return isl_multi_aff_free(maff
);
4054 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4055 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4057 maff1
= isl_multi_aff_add(maff1
, maff2
);
4058 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4062 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4070 /* Return the set of domain elements where "ma1" is lexicographically
4071 * smaller than or equal to "ma2".
4073 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4074 __isl_take isl_multi_aff
*ma2
)
4076 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4079 /* Return the set of domain elements where "ma1" is lexicographically
4080 * smaller than "ma2".
4082 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4083 __isl_take isl_multi_aff
*ma2
)
4085 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4088 /* Return the set of domain elements where "ma1" and "ma2"
4091 static __isl_give isl_set
*isl_multi_aff_order_set(
4092 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4093 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4096 isl_map
*map1
, *map2
;
4099 map1
= isl_map_from_multi_aff(ma1
);
4100 map2
= isl_map_from_multi_aff(ma2
);
4101 map
= isl_map_range_product(map1
, map2
);
4102 space
= isl_space_range(isl_map_get_space(map
));
4103 space
= isl_space_domain(isl_space_unwrap(space
));
4105 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4107 return isl_map_domain(map
);
4110 /* Return the set of domain elements where "ma1" is lexicographically
4111 * greater than or equal to "ma2".
4113 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4114 __isl_take isl_multi_aff
*ma2
)
4116 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4119 /* Return the set of domain elements where "ma1" is lexicographically
4120 * greater than "ma2".
4122 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4123 __isl_take isl_multi_aff
*ma2
)
4125 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4129 #define PW isl_pw_multi_aff
4131 #define EL isl_multi_aff
4133 #define EL_IS_ZERO is_empty
4137 #define IS_ZERO is_empty
4140 #undef DEFAULT_IS_ZERO
4141 #define DEFAULT_IS_ZERO 0
4146 #define NO_INVOLVES_DIMS
4147 #define NO_INSERT_DIMS
4151 #include <isl_pw_templ.c>
4152 #include <isl_pw_union_opt.c>
4157 #define UNION isl_union_pw_multi_aff
4159 #define PART isl_pw_multi_aff
4161 #define PARTS pw_multi_aff
4163 #include <isl_union_multi.c>
4164 #include <isl_union_neg.c>
4166 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4167 __isl_take isl_pw_multi_aff
*pma1
,
4168 __isl_take isl_pw_multi_aff
*pma2
)
4170 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4171 &isl_multi_aff_lex_ge_set
);
4174 /* Given two piecewise multi affine expressions, return a piecewise
4175 * multi-affine expression defined on the union of the definition domains
4176 * of the inputs that is equal to the lexicographic maximum of the two
4177 * inputs on each cell. If only one of the two inputs is defined on
4178 * a given cell, then it is considered to be the maximum.
4180 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4181 __isl_take isl_pw_multi_aff
*pma1
,
4182 __isl_take isl_pw_multi_aff
*pma2
)
4184 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4185 &pw_multi_aff_union_lexmax
);
4188 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4189 __isl_take isl_pw_multi_aff
*pma1
,
4190 __isl_take isl_pw_multi_aff
*pma2
)
4192 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4193 &isl_multi_aff_lex_le_set
);
4196 /* Given two piecewise multi affine expressions, return a piecewise
4197 * multi-affine expression defined on the union of the definition domains
4198 * of the inputs that is equal to the lexicographic minimum of the two
4199 * inputs on each cell. If only one of the two inputs is defined on
4200 * a given cell, then it is considered to be the minimum.
4202 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4203 __isl_take isl_pw_multi_aff
*pma1
,
4204 __isl_take isl_pw_multi_aff
*pma2
)
4206 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4207 &pw_multi_aff_union_lexmin
);
4210 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4211 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4213 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4214 &isl_multi_aff_add
);
4217 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4218 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4220 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4224 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4225 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4227 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4228 &isl_multi_aff_sub
);
4231 /* Subtract "pma2" from "pma1" and return the result.
4233 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4234 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4236 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4240 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4241 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4243 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4246 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4247 * with the actual sum on the shared domain and
4248 * the defined expression on the symmetric difference of the domains.
4250 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4251 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4253 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4256 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4257 * with the actual sum on the shared domain and
4258 * the defined expression on the symmetric difference of the domains.
4260 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4261 __isl_take isl_union_pw_multi_aff
*upma1
,
4262 __isl_take isl_union_pw_multi_aff
*upma2
)
4264 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4267 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4268 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4270 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4271 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4275 isl_pw_multi_aff
*res
;
4280 n
= pma1
->n
* pma2
->n
;
4281 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4282 isl_space_copy(pma2
->dim
));
4283 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4285 for (i
= 0; i
< pma1
->n
; ++i
) {
4286 for (j
= 0; j
< pma2
->n
; ++j
) {
4290 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4291 isl_set_copy(pma2
->p
[j
].set
));
4292 ma
= isl_multi_aff_product(
4293 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4294 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4295 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4299 isl_pw_multi_aff_free(pma1
);
4300 isl_pw_multi_aff_free(pma2
);
4303 isl_pw_multi_aff_free(pma1
);
4304 isl_pw_multi_aff_free(pma2
);
4308 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4309 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4311 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4312 &pw_multi_aff_product
);
4315 /* Construct a map mapping the domain of the piecewise multi-affine expression
4316 * to its range, with each dimension in the range equated to the
4317 * corresponding affine expression on its cell.
4319 * If the domain of "pma" is rational, then so is the constructed "map".
4321 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4329 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4331 for (i
= 0; i
< pma
->n
; ++i
) {
4333 isl_multi_aff
*maff
;
4334 isl_basic_map
*bmap
;
4337 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4339 map
= isl_map_free(map
);
4340 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4341 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4342 map_i
= isl_map_from_basic_map(bmap
);
4343 map_i
= isl_map_intersect_domain(map_i
,
4344 isl_set_copy(pma
->p
[i
].set
));
4345 map
= isl_map_union_disjoint(map
, map_i
);
4348 isl_pw_multi_aff_free(pma
);
4352 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4357 if (!isl_space_is_set(pma
->dim
))
4358 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4359 "isl_pw_multi_aff cannot be converted into an isl_set",
4362 return isl_map_from_pw_multi_aff(pma
);
4364 isl_pw_multi_aff_free(pma
);
4368 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4369 * denominator "denom".
4370 * "denom" is allowed to be negative, in which case the actual denominator
4371 * is -denom and the expressions are added instead.
4373 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4374 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4380 first
= isl_seq_first_non_zero(c
, n
);
4384 sign
= isl_int_sgn(denom
);
4386 isl_int_abs(d
, denom
);
4387 for (i
= first
; i
< n
; ++i
) {
4390 if (isl_int_is_zero(c
[i
]))
4392 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4393 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4394 aff_i
= isl_aff_scale_down(aff_i
, d
);
4396 aff
= isl_aff_sub(aff
, aff_i
);
4398 aff
= isl_aff_add(aff
, aff_i
);
4405 /* Extract an affine expression that expresses the output dimension "pos"
4406 * of "bmap" in terms of the parameters and input dimensions from
4408 * Note that this expression may involve integer divisions defined
4409 * in terms of parameters and input dimensions.
4410 * The equality may also involve references to earlier (but not later)
4411 * output dimensions. These are replaced by the corresponding elements
4414 * If the equality is of the form
4416 * f(i) + h(j) + a x + g(i) = 0,
4418 * with f(i) a linear combinations of the parameters and input dimensions,
4419 * g(i) a linear combination of integer divisions defined in terms of the same
4420 * and h(j) a linear combinations of earlier output dimensions,
4421 * then the affine expression is
4423 * (-f(i) - g(i))/a - h(j)/a
4425 * If the equality is of the form
4427 * f(i) + h(j) - a x + g(i) = 0,
4429 * then the affine expression is
4431 * (f(i) + g(i))/a - h(j)/(-a)
4434 * If "div" refers to an integer division (i.e., it is smaller than
4435 * the number of integer divisions), then the equality constraint
4436 * does involve an integer division (the one at position "div") that
4437 * is defined in terms of output dimensions. However, this integer
4438 * division can be eliminated by exploiting a pair of constraints
4439 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4440 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4442 * In particular, let
4444 * x = e(i) + m floor(...)
4446 * with e(i) the expression derived above and floor(...) the integer
4447 * division involving output dimensions.
4458 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4459 * = (e(i) - l) mod m
4463 * x - l = (e(i) - l) mod m
4467 * x = ((e(i) - l) mod m) + l
4469 * The variable "shift" below contains the expression -l, which may
4470 * also involve a linear combination of earlier output dimensions.
4472 static __isl_give isl_aff
*extract_aff_from_equality(
4473 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4474 __isl_keep isl_multi_aff
*ma
)
4477 unsigned n_div
, n_out
;
4479 isl_local_space
*ls
;
4480 isl_aff
*aff
, *shift
;
4483 ctx
= isl_basic_map_get_ctx(bmap
);
4484 ls
= isl_basic_map_get_local_space(bmap
);
4485 ls
= isl_local_space_domain(ls
);
4486 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4489 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4490 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4491 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4492 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4493 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4494 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4495 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4497 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4498 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4499 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4502 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4503 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4504 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4505 bmap
->eq
[eq
][o_out
+ pos
]);
4507 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4510 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4511 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4512 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4513 isl_int_set_si(shift
->v
->el
[0], 1);
4514 shift
= subtract_initial(shift
, ma
, pos
,
4515 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4516 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4517 mod
= isl_val_int_from_isl_int(ctx
,
4518 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4519 mod
= isl_val_abs(mod
);
4520 aff
= isl_aff_mod_val(aff
, mod
);
4521 aff
= isl_aff_sub(aff
, shift
);
4524 isl_local_space_free(ls
);
4527 isl_local_space_free(ls
);
4532 /* Given a basic map with output dimensions defined
4533 * in terms of the parameters input dimensions and earlier
4534 * output dimensions using an equality (and possibly a pair on inequalities),
4535 * extract an isl_aff that expresses output dimension "pos" in terms
4536 * of the parameters and input dimensions.
4537 * Note that this expression may involve integer divisions defined
4538 * in terms of parameters and input dimensions.
4539 * "ma" contains the expressions corresponding to earlier output dimensions.
4541 * This function shares some similarities with
4542 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4544 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4545 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4552 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4553 if (eq
>= bmap
->n_eq
)
4554 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4555 "unable to find suitable equality", return NULL
);
4556 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4558 aff
= isl_aff_remove_unused_divs(aff
);
4562 /* Given a basic map where each output dimension is defined
4563 * in terms of the parameters and input dimensions using an equality,
4564 * extract an isl_multi_aff that expresses the output dimensions in terms
4565 * of the parameters and input dimensions.
4567 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4568 __isl_take isl_basic_map
*bmap
)
4577 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4578 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4580 for (i
= 0; i
< n_out
; ++i
) {
4583 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4584 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4587 isl_basic_map_free(bmap
);
4592 /* Given a basic set where each set dimension is defined
4593 * in terms of the parameters using an equality,
4594 * extract an isl_multi_aff that expresses the set dimensions in terms
4595 * of the parameters.
4597 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4598 __isl_take isl_basic_set
*bset
)
4600 return extract_isl_multi_aff_from_basic_map(bset
);
4603 /* Create an isl_pw_multi_aff that is equivalent to
4604 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4605 * The given basic map is such that each output dimension is defined
4606 * in terms of the parameters and input dimensions using an equality.
4608 * Since some applications expect the result of isl_pw_multi_aff_from_map
4609 * to only contain integer affine expressions, we compute the floor
4610 * of the expression before returning.
4612 * Remove all constraints involving local variables without
4613 * an explicit representation (resulting in the removal of those
4614 * local variables) prior to the actual extraction to ensure
4615 * that the local spaces in which the resulting affine expressions
4616 * are created do not contain any unknown local variables.
4617 * Removing such constraints is safe because constraints involving
4618 * unknown local variables are not used to determine whether
4619 * a basic map is obviously single-valued.
4621 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4622 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4626 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4627 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4628 ma
= isl_multi_aff_floor(ma
);
4629 return isl_pw_multi_aff_alloc(domain
, ma
);
4632 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4633 * This obviously only works if the input "map" is single-valued.
4634 * If so, we compute the lexicographic minimum of the image in the form
4635 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4636 * to its lexicographic minimum.
4637 * If the input is not single-valued, we produce an error.
4639 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4640 __isl_take isl_map
*map
)
4644 isl_pw_multi_aff
*pma
;
4646 sv
= isl_map_is_single_valued(map
);
4650 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4651 "map is not single-valued", goto error
);
4652 map
= isl_map_make_disjoint(map
);
4656 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4658 for (i
= 0; i
< map
->n
; ++i
) {
4659 isl_pw_multi_aff
*pma_i
;
4660 isl_basic_map
*bmap
;
4661 bmap
= isl_basic_map_copy(map
->p
[i
]);
4662 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4663 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4673 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4674 * taking into account that the output dimension at position "d"
4675 * can be represented as
4677 * x = floor((e(...) + c1) / m)
4679 * given that constraint "i" is of the form
4681 * e(...) + c1 - m x >= 0
4684 * Let "map" be of the form
4688 * We construct a mapping
4690 * A -> [A -> x = floor(...)]
4692 * apply that to the map, obtaining
4694 * [A -> x = floor(...)] -> B
4696 * and equate dimension "d" to x.
4697 * We then compute a isl_pw_multi_aff representation of the resulting map
4698 * and plug in the mapping above.
4700 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4701 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4705 isl_local_space
*ls
;
4713 isl_pw_multi_aff
*pma
;
4716 is_set
= isl_map_is_set(map
);
4720 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4721 ctx
= isl_map_get_ctx(map
);
4722 space
= isl_space_domain(isl_map_get_space(map
));
4723 n_in
= isl_space_dim(space
, isl_dim_set
);
4724 n
= isl_space_dim(space
, isl_dim_all
);
4726 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4728 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4729 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4731 isl_basic_map_free(hull
);
4733 ls
= isl_local_space_from_space(isl_space_copy(space
));
4734 aff
= isl_aff_alloc_vec(ls
, v
);
4735 aff
= isl_aff_floor(aff
);
4737 isl_space_free(space
);
4738 ma
= isl_multi_aff_from_aff(aff
);
4740 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4741 ma
= isl_multi_aff_range_product(ma
,
4742 isl_multi_aff_from_aff(aff
));
4745 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4746 map
= isl_map_apply_domain(map
, insert
);
4747 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4748 pma
= isl_pw_multi_aff_from_map(map
);
4749 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4754 isl_basic_map_free(hull
);
4758 /* Is constraint "c" of the form
4760 * e(...) + c1 - m x >= 0
4764 * -e(...) + c2 + m x >= 0
4766 * where m > 1 and e only depends on parameters and input dimemnsions?
4768 * "offset" is the offset of the output dimensions
4769 * "pos" is the position of output dimension x.
4771 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4773 if (isl_int_is_zero(c
[offset
+ d
]))
4775 if (isl_int_is_one(c
[offset
+ d
]))
4777 if (isl_int_is_negone(c
[offset
+ d
]))
4779 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4781 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4782 total
- (offset
+ d
+ 1)) != -1)
4787 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4789 * As a special case, we first check if there is any pair of constraints,
4790 * shared by all the basic maps in "map" that force a given dimension
4791 * to be equal to the floor of some affine combination of the input dimensions.
4793 * In particular, if we can find two constraints
4795 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4799 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4801 * where m > 1 and e only depends on parameters and input dimemnsions,
4804 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4806 * then we know that we can take
4808 * x = floor((e(...) + c1) / m)
4810 * without having to perform any computation.
4812 * Note that we know that
4816 * If c1 + c2 were 0, then we would have detected an equality during
4817 * simplification. If c1 + c2 were negative, then we would have detected
4820 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4821 __isl_take isl_map
*map
)
4827 isl_basic_map
*hull
;
4829 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4834 dim
= isl_map_dim(map
, isl_dim_out
);
4835 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4836 total
= 1 + isl_basic_map_total_dim(hull
);
4838 for (d
= 0; d
< dim
; ++d
) {
4839 for (i
= 0; i
< n
; ++i
) {
4840 if (!is_potential_div_constraint(hull
->ineq
[i
],
4843 for (j
= i
+ 1; j
< n
; ++j
) {
4844 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4845 hull
->ineq
[j
] + 1, total
- 1))
4847 isl_int_add(sum
, hull
->ineq
[i
][0],
4849 if (isl_int_abs_lt(sum
,
4850 hull
->ineq
[i
][offset
+ d
]))
4857 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4859 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4863 isl_basic_map_free(hull
);
4864 return pw_multi_aff_from_map_base(map
);
4867 isl_basic_map_free(hull
);
4871 /* Given an affine expression
4873 * [A -> B] -> f(A,B)
4875 * construct an isl_multi_aff
4879 * such that dimension "d" in B' is set to "aff" and the remaining
4880 * dimensions are set equal to the corresponding dimensions in B.
4881 * "n_in" is the dimension of the space A.
4882 * "n_out" is the dimension of the space B.
4884 * If "is_set" is set, then the affine expression is of the form
4888 * and we construct an isl_multi_aff
4892 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4893 unsigned n_in
, unsigned n_out
, int is_set
)
4897 isl_space
*space
, *space2
;
4898 isl_local_space
*ls
;
4900 space
= isl_aff_get_domain_space(aff
);
4901 ls
= isl_local_space_from_space(isl_space_copy(space
));
4902 space2
= isl_space_copy(space
);
4904 space2
= isl_space_range(isl_space_unwrap(space2
));
4905 space
= isl_space_map_from_domain_and_range(space
, space2
);
4906 ma
= isl_multi_aff_alloc(space
);
4907 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4909 for (i
= 0; i
< n_out
; ++i
) {
4912 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4913 isl_dim_set
, n_in
+ i
);
4914 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4917 isl_local_space_free(ls
);
4922 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4923 * taking into account that the dimension at position "d" can be written as
4925 * x = m a + f(..) (1)
4927 * where m is equal to "gcd".
4928 * "i" is the index of the equality in "hull" that defines f(..).
4929 * In particular, the equality is of the form
4931 * f(..) - x + m g(existentials) = 0
4935 * -f(..) + x + m g(existentials) = 0
4937 * We basically plug (1) into "map", resulting in a map with "a"
4938 * in the range instead of "x". The corresponding isl_pw_multi_aff
4939 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4941 * Specifically, given the input map
4945 * We first wrap it into a set
4949 * and define (1) on top of the corresponding space, resulting in "aff".
4950 * We use this to create an isl_multi_aff that maps the output position "d"
4951 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4952 * We plug this into the wrapped map, unwrap the result and compute the
4953 * corresponding isl_pw_multi_aff.
4954 * The result is an expression
4962 * so that we can plug that into "aff", after extending the latter to
4968 * If "map" is actually a set, then there is no "A" space, meaning
4969 * that we do not need to perform any wrapping, and that the result
4970 * of the recursive call is of the form
4974 * which is plugged into a mapping of the form
4978 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4979 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4984 isl_local_space
*ls
;
4987 isl_pw_multi_aff
*pma
, *id
;
4993 is_set
= isl_map_is_set(map
);
4997 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4998 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4999 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5004 set
= isl_map_wrap(map
);
5005 space
= isl_space_map_from_set(isl_set_get_space(set
));
5006 ma
= isl_multi_aff_identity(space
);
5007 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5008 aff
= isl_aff_alloc(ls
);
5010 isl_int_set_si(aff
->v
->el
[0], 1);
5011 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5012 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5015 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5017 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5019 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5020 set
= isl_set_preimage_multi_aff(set
, ma
);
5022 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5027 map
= isl_set_unwrap(set
);
5028 pma
= isl_pw_multi_aff_from_map(map
);
5031 space
= isl_pw_multi_aff_get_domain_space(pma
);
5032 space
= isl_space_map_from_set(space
);
5033 id
= isl_pw_multi_aff_identity(space
);
5034 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5036 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5037 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5039 isl_basic_map_free(hull
);
5043 isl_basic_map_free(hull
);
5047 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5048 * "hull" contains the equalities valid for "map".
5050 * Check if any of the output dimensions is "strided".
5051 * That is, we check if it can be written as
5055 * with m greater than 1, a some combination of existentially quantified
5056 * variables and f an expression in the parameters and input dimensions.
5057 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5059 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5062 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5063 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5072 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5073 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5076 isl_basic_map_free(hull
);
5077 return pw_multi_aff_from_map_check_div(map
);
5082 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5083 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5085 for (i
= 0; i
< n_out
; ++i
) {
5086 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5087 isl_int
*eq
= hull
->eq
[j
];
5088 isl_pw_multi_aff
*res
;
5090 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5091 !isl_int_is_negone(eq
[o_out
+ i
]))
5093 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5095 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5096 n_out
- (i
+ 1)) != -1)
5098 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5099 if (isl_int_is_zero(gcd
))
5101 if (isl_int_is_one(gcd
))
5104 res
= pw_multi_aff_from_map_stride(map
, hull
,
5112 isl_basic_map_free(hull
);
5113 return pw_multi_aff_from_map_check_div(map
);
5116 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5118 * As a special case, we first check if all output dimensions are uniquely
5119 * defined in terms of the parameters and input dimensions over the entire
5120 * domain. If so, we extract the desired isl_pw_multi_aff directly
5121 * from the affine hull of "map" and its domain.
5123 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5126 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5129 isl_basic_map
*hull
;
5134 if (isl_map_n_basic_map(map
) == 1) {
5135 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5136 hull
= isl_basic_map_plain_affine_hull(hull
);
5137 sv
= isl_basic_map_plain_is_single_valued(hull
);
5139 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5141 isl_basic_map_free(hull
);
5143 map
= isl_map_detect_equalities(map
);
5144 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5145 sv
= isl_basic_map_plain_is_single_valued(hull
);
5147 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5149 return pw_multi_aff_from_map_check_strides(map
, hull
);
5150 isl_basic_map_free(hull
);
5155 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5157 return isl_pw_multi_aff_from_map(set
);
5160 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5163 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5165 isl_union_pw_multi_aff
**upma
= user
;
5166 isl_pw_multi_aff
*pma
;
5168 pma
= isl_pw_multi_aff_from_map(map
);
5169 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5171 return *upma
? isl_stat_ok
: isl_stat_error
;
5174 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5177 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5178 __isl_take isl_aff
*aff
)
5181 isl_pw_multi_aff
*pma
;
5183 ma
= isl_multi_aff_from_aff(aff
);
5184 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5185 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5188 /* Try and create an isl_union_pw_multi_aff that is equivalent
5189 * to the given isl_union_map.
5190 * The isl_union_map is required to be single-valued in each space.
5191 * Otherwise, an error is produced.
5193 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5194 __isl_take isl_union_map
*umap
)
5197 isl_union_pw_multi_aff
*upma
;
5199 space
= isl_union_map_get_space(umap
);
5200 upma
= isl_union_pw_multi_aff_empty(space
);
5201 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5202 upma
= isl_union_pw_multi_aff_free(upma
);
5203 isl_union_map_free(umap
);
5208 /* Try and create an isl_union_pw_multi_aff that is equivalent
5209 * to the given isl_union_set.
5210 * The isl_union_set is required to be a singleton in each space.
5211 * Otherwise, an error is produced.
5213 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5214 __isl_take isl_union_set
*uset
)
5216 return isl_union_pw_multi_aff_from_union_map(uset
);
5219 /* Return the piecewise affine expression "set ? 1 : 0".
5221 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5224 isl_space
*space
= isl_set_get_space(set
);
5225 isl_local_space
*ls
= isl_local_space_from_space(space
);
5226 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5227 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5229 one
= isl_aff_add_constant_si(one
, 1);
5230 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5231 set
= isl_set_complement(set
);
5232 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5237 /* Plug in "subs" for dimension "type", "pos" of "aff".
5239 * Let i be the dimension to replace and let "subs" be of the form
5243 * and "aff" of the form
5249 * (a f + d g')/(m d)
5251 * where g' is the result of plugging in "subs" in each of the integer
5254 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5255 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5260 aff
= isl_aff_cow(aff
);
5262 return isl_aff_free(aff
);
5264 ctx
= isl_aff_get_ctx(aff
);
5265 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5266 isl_die(ctx
, isl_error_invalid
,
5267 "spaces don't match", return isl_aff_free(aff
));
5268 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5269 isl_die(ctx
, isl_error_unsupported
,
5270 "cannot handle divs yet", return isl_aff_free(aff
));
5272 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5274 return isl_aff_free(aff
);
5276 aff
->v
= isl_vec_cow(aff
->v
);
5278 return isl_aff_free(aff
);
5280 pos
+= isl_local_space_offset(aff
->ls
, type
);
5283 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5284 aff
->v
->size
, subs
->v
->size
, v
);
5290 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5291 * expressions in "maff".
5293 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5294 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5295 __isl_keep isl_aff
*subs
)
5299 maff
= isl_multi_aff_cow(maff
);
5301 return isl_multi_aff_free(maff
);
5303 if (type
== isl_dim_in
)
5306 for (i
= 0; i
< maff
->n
; ++i
) {
5307 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5309 return isl_multi_aff_free(maff
);
5315 /* Plug in "subs" for dimension "type", "pos" of "pma".
5317 * pma is of the form
5321 * while subs is of the form
5323 * v' = B_j(v) -> S_j
5325 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5326 * has a contribution in the result, in particular
5328 * C_ij(S_j) -> M_i(S_j)
5330 * Note that plugging in S_j in C_ij may also result in an empty set
5331 * and this contribution should simply be discarded.
5333 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5334 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5335 __isl_keep isl_pw_aff
*subs
)
5338 isl_pw_multi_aff
*res
;
5341 return isl_pw_multi_aff_free(pma
);
5343 n
= pma
->n
* subs
->n
;
5344 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5346 for (i
= 0; i
< pma
->n
; ++i
) {
5347 for (j
= 0; j
< subs
->n
; ++j
) {
5349 isl_multi_aff
*res_ij
;
5352 common
= isl_set_intersect(
5353 isl_set_copy(pma
->p
[i
].set
),
5354 isl_set_copy(subs
->p
[j
].set
));
5355 common
= isl_set_substitute(common
,
5356 type
, pos
, subs
->p
[j
].aff
);
5357 empty
= isl_set_plain_is_empty(common
);
5358 if (empty
< 0 || empty
) {
5359 isl_set_free(common
);
5365 res_ij
= isl_multi_aff_substitute(
5366 isl_multi_aff_copy(pma
->p
[i
].maff
),
5367 type
, pos
, subs
->p
[j
].aff
);
5369 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5373 isl_pw_multi_aff_free(pma
);
5376 isl_pw_multi_aff_free(pma
);
5377 isl_pw_multi_aff_free(res
);
5381 /* Compute the preimage of a range of dimensions in the affine expression "src"
5382 * under "ma" and put the result in "dst". The number of dimensions in "src"
5383 * that precede the range is given by "n_before". The number of dimensions
5384 * in the range is given by the number of output dimensions of "ma".
5385 * The number of dimensions that follow the range is given by "n_after".
5386 * If "has_denom" is set (to one),
5387 * then "src" and "dst" have an extra initial denominator.
5388 * "n_div_ma" is the number of existentials in "ma"
5389 * "n_div_bset" is the number of existentials in "src"
5390 * The resulting "dst" (which is assumed to have been allocated by
5391 * the caller) contains coefficients for both sets of existentials,
5392 * first those in "ma" and then those in "src".
5393 * f, c1, c2 and g are temporary objects that have been initialized
5396 * Let src represent the expression
5398 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5400 * and let ma represent the expressions
5402 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5404 * We start out with the following expression for dst:
5406 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5408 * with the multiplication factor f initially equal to 1
5409 * and f \sum_i b_i v_i kept separately.
5410 * For each x_i that we substitute, we multiply the numerator
5411 * (and denominator) of dst by c_1 = m_i and add the numerator
5412 * of the x_i expression multiplied by c_2 = f b_i,
5413 * after removing the common factors of c_1 and c_2.
5414 * The multiplication factor f also needs to be multiplied by c_1
5415 * for the next x_j, j > i.
5417 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5418 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5419 int n_div_ma
, int n_div_bmap
,
5420 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5423 int n_param
, n_in
, n_out
;
5426 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5427 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5428 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5430 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5431 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5432 isl_seq_clr(dst
+ o_dst
, n_in
);
5435 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5438 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5440 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5442 isl_int_set_si(f
, 1);
5444 for (i
= 0; i
< n_out
; ++i
) {
5445 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5447 if (isl_int_is_zero(src
[offset
]))
5449 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5450 isl_int_mul(c2
, f
, src
[offset
]);
5451 isl_int_gcd(g
, c1
, c2
);
5452 isl_int_divexact(c1
, c1
, g
);
5453 isl_int_divexact(c2
, c2
, g
);
5455 isl_int_mul(f
, f
, c1
);
5458 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5459 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5460 o_dst
+= 1 + n_param
;
5461 o_src
+= 1 + n_param
;
5462 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5464 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5465 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5468 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5470 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5471 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5474 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5476 isl_int_mul(dst
[0], dst
[0], c1
);
5480 /* Compute the pullback of "aff" by the function represented by "ma".
5481 * In other words, plug in "ma" in "aff". The result is an affine expression
5482 * defined over the domain space of "ma".
5484 * If "aff" is represented by
5486 * (a(p) + b x + c(divs))/d
5488 * and ma is represented by
5490 * x = D(p) + F(y) + G(divs')
5492 * then the result is
5494 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5496 * The divs in the local space of the input are similarly adjusted
5497 * through a call to isl_local_space_preimage_multi_aff.
5499 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5500 __isl_take isl_multi_aff
*ma
)
5502 isl_aff
*res
= NULL
;
5503 isl_local_space
*ls
;
5504 int n_div_aff
, n_div_ma
;
5505 isl_int f
, c1
, c2
, g
;
5507 ma
= isl_multi_aff_align_divs(ma
);
5511 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5512 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5514 ls
= isl_aff_get_domain_local_space(aff
);
5515 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5516 res
= isl_aff_alloc(ls
);
5525 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5534 isl_multi_aff_free(ma
);
5535 res
= isl_aff_normalize(res
);
5539 isl_multi_aff_free(ma
);
5544 /* Compute the pullback of "aff1" by the function represented by "aff2".
5545 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5546 * defined over the domain space of "aff1".
5548 * The domain of "aff1" should match the range of "aff2", which means
5549 * that it should be single-dimensional.
5551 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5552 __isl_take isl_aff
*aff2
)
5556 ma
= isl_multi_aff_from_aff(aff2
);
5557 return isl_aff_pullback_multi_aff(aff1
, ma
);
5560 /* Compute the pullback of "ma1" by the function represented by "ma2".
5561 * In other words, plug in "ma2" in "ma1".
5563 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5565 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5566 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5569 isl_space
*space
= NULL
;
5571 ma2
= isl_multi_aff_align_divs(ma2
);
5572 ma1
= isl_multi_aff_cow(ma1
);
5576 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5577 isl_multi_aff_get_space(ma1
));
5579 for (i
= 0; i
< ma1
->n
; ++i
) {
5580 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5581 isl_multi_aff_copy(ma2
));
5586 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5587 isl_multi_aff_free(ma2
);
5590 isl_space_free(space
);
5591 isl_multi_aff_free(ma2
);
5592 isl_multi_aff_free(ma1
);
5596 /* Compute the pullback of "ma1" by the function represented by "ma2".
5597 * In other words, plug in "ma2" in "ma1".
5599 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5600 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5602 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5603 &isl_multi_aff_pullback_multi_aff_aligned
);
5606 /* Extend the local space of "dst" to include the divs
5607 * in the local space of "src".
5609 * If "src" does not have any divs or if the local spaces of "dst" and
5610 * "src" are the same, then no extension is required.
5612 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5613 __isl_keep isl_aff
*src
)
5616 int src_n_div
, dst_n_div
;
5623 return isl_aff_free(dst
);
5625 ctx
= isl_aff_get_ctx(src
);
5626 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5628 return isl_aff_free(dst
);
5630 isl_die(ctx
, isl_error_invalid
,
5631 "spaces don't match", goto error
);
5633 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5636 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5638 return isl_aff_free(dst
);
5642 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5643 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5644 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5645 if (!exp1
|| (dst_n_div
&& !exp2
))
5648 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5649 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5657 return isl_aff_free(dst
);
5660 /* Adjust the local spaces of the affine expressions in "maff"
5661 * such that they all have the save divs.
5663 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5664 __isl_take isl_multi_aff
*maff
)
5672 maff
= isl_multi_aff_cow(maff
);
5676 for (i
= 1; i
< maff
->n
; ++i
)
5677 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5678 for (i
= 1; i
< maff
->n
; ++i
) {
5679 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5681 return isl_multi_aff_free(maff
);
5687 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5689 aff
= isl_aff_cow(aff
);
5693 aff
->ls
= isl_local_space_lift(aff
->ls
);
5695 return isl_aff_free(aff
);
5700 /* Lift "maff" to a space with extra dimensions such that the result
5701 * has no more existentially quantified variables.
5702 * If "ls" is not NULL, then *ls is assigned the local space that lies
5703 * at the basis of the lifting applied to "maff".
5705 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5706 __isl_give isl_local_space
**ls
)
5720 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5721 *ls
= isl_local_space_from_space(space
);
5723 return isl_multi_aff_free(maff
);
5728 maff
= isl_multi_aff_cow(maff
);
5729 maff
= isl_multi_aff_align_divs(maff
);
5733 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5734 space
= isl_multi_aff_get_space(maff
);
5735 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5736 space
= isl_space_extend_domain_with_range(space
,
5737 isl_multi_aff_get_space(maff
));
5739 return isl_multi_aff_free(maff
);
5740 isl_space_free(maff
->space
);
5741 maff
->space
= space
;
5744 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5746 return isl_multi_aff_free(maff
);
5749 for (i
= 0; i
< maff
->n
; ++i
) {
5750 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5758 isl_local_space_free(*ls
);
5759 return isl_multi_aff_free(maff
);
5763 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5765 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5766 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5776 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5777 if (pos
< 0 || pos
>= n_out
)
5778 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5779 "index out of bounds", return NULL
);
5781 space
= isl_pw_multi_aff_get_space(pma
);
5782 space
= isl_space_drop_dims(space
, isl_dim_out
,
5783 pos
+ 1, n_out
- pos
- 1);
5784 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5786 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5787 for (i
= 0; i
< pma
->n
; ++i
) {
5789 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5790 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5796 /* Return an isl_pw_multi_aff with the given "set" as domain and
5797 * an unnamed zero-dimensional range.
5799 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5800 __isl_take isl_set
*set
)
5805 space
= isl_set_get_space(set
);
5806 space
= isl_space_from_domain(space
);
5807 ma
= isl_multi_aff_zero(space
);
5808 return isl_pw_multi_aff_alloc(set
, ma
);
5811 /* Add an isl_pw_multi_aff with the given "set" as domain and
5812 * an unnamed zero-dimensional range to *user.
5814 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5817 isl_union_pw_multi_aff
**upma
= user
;
5818 isl_pw_multi_aff
*pma
;
5820 pma
= isl_pw_multi_aff_from_domain(set
);
5821 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5826 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5827 * an unnamed zero-dimensional range.
5829 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5830 __isl_take isl_union_set
*uset
)
5833 isl_union_pw_multi_aff
*upma
;
5838 space
= isl_union_set_get_space(uset
);
5839 upma
= isl_union_pw_multi_aff_empty(space
);
5841 if (isl_union_set_foreach_set(uset
,
5842 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5845 isl_union_set_free(uset
);
5848 isl_union_set_free(uset
);
5849 isl_union_pw_multi_aff_free(upma
);
5853 /* Convert "pma" to an isl_map and add it to *umap.
5855 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5858 isl_union_map
**umap
= user
;
5861 map
= isl_map_from_pw_multi_aff(pma
);
5862 *umap
= isl_union_map_add_map(*umap
, map
);
5867 /* Construct a union map mapping the domain of the union
5868 * piecewise multi-affine expression to its range, with each dimension
5869 * in the range equated to the corresponding affine expression on its cell.
5871 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5872 __isl_take isl_union_pw_multi_aff
*upma
)
5875 isl_union_map
*umap
;
5880 space
= isl_union_pw_multi_aff_get_space(upma
);
5881 umap
= isl_union_map_empty(space
);
5883 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5884 &map_from_pw_multi_aff
, &umap
) < 0)
5887 isl_union_pw_multi_aff_free(upma
);
5890 isl_union_pw_multi_aff_free(upma
);
5891 isl_union_map_free(umap
);
5895 /* Local data for bin_entry and the callback "fn".
5897 struct isl_union_pw_multi_aff_bin_data
{
5898 isl_union_pw_multi_aff
*upma2
;
5899 isl_union_pw_multi_aff
*res
;
5900 isl_pw_multi_aff
*pma
;
5901 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5904 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5905 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5907 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5909 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5913 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5915 isl_pw_multi_aff_free(pma
);
5920 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5921 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5922 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5923 * as *entry. The callback should adjust data->res if desired.
5925 static __isl_give isl_union_pw_multi_aff
*bin_op(
5926 __isl_take isl_union_pw_multi_aff
*upma1
,
5927 __isl_take isl_union_pw_multi_aff
*upma2
,
5928 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5931 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5933 space
= isl_union_pw_multi_aff_get_space(upma2
);
5934 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5935 space
= isl_union_pw_multi_aff_get_space(upma1
);
5936 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5938 if (!upma1
|| !upma2
)
5942 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5943 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5944 &bin_entry
, &data
) < 0)
5947 isl_union_pw_multi_aff_free(upma1
);
5948 isl_union_pw_multi_aff_free(upma2
);
5951 isl_union_pw_multi_aff_free(upma1
);
5952 isl_union_pw_multi_aff_free(upma2
);
5953 isl_union_pw_multi_aff_free(data
.res
);
5957 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5958 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5960 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5961 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5965 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5966 isl_pw_multi_aff_get_space(pma2
));
5967 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5968 &isl_multi_aff_range_product
);
5971 /* Given two isl_pw_multi_affs A -> B and C -> D,
5972 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5974 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5975 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5977 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5978 &pw_multi_aff_range_product
);
5981 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5982 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5984 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5985 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5989 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5990 isl_pw_multi_aff_get_space(pma2
));
5991 space
= isl_space_flatten_range(space
);
5992 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5993 &isl_multi_aff_flat_range_product
);
5996 /* Given two isl_pw_multi_affs A -> B and C -> D,
5997 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5999 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6000 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6002 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
6003 &pw_multi_aff_flat_range_product
);
6006 /* If data->pma and "pma2" have the same domain space, then compute
6007 * their flat range product and the result to data->res.
6009 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6012 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6014 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6015 pma2
->dim
, isl_dim_in
)) {
6016 isl_pw_multi_aff_free(pma2
);
6020 pma2
= isl_pw_multi_aff_flat_range_product(
6021 isl_pw_multi_aff_copy(data
->pma
), pma2
);
6023 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6028 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6029 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6031 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6032 __isl_take isl_union_pw_multi_aff
*upma1
,
6033 __isl_take isl_union_pw_multi_aff
*upma2
)
6035 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6038 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6039 * The parameters are assumed to have been aligned.
6041 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6042 * except that it works on two different isl_pw_* types.
6044 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6045 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6046 __isl_take isl_pw_aff
*pa
)
6049 isl_pw_multi_aff
*res
= NULL
;
6054 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6055 pa
->dim
, isl_dim_in
))
6056 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6057 "domains don't match", goto error
);
6058 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
6059 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6060 "index out of bounds", goto error
);
6063 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6065 for (i
= 0; i
< pma
->n
; ++i
) {
6066 for (j
= 0; j
< pa
->n
; ++j
) {
6068 isl_multi_aff
*res_ij
;
6071 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6072 isl_set_copy(pa
->p
[j
].set
));
6073 empty
= isl_set_plain_is_empty(common
);
6074 if (empty
< 0 || empty
) {
6075 isl_set_free(common
);
6081 res_ij
= isl_multi_aff_set_aff(
6082 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6083 isl_aff_copy(pa
->p
[j
].aff
));
6084 res_ij
= isl_multi_aff_gist(res_ij
,
6085 isl_set_copy(common
));
6087 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6091 isl_pw_multi_aff_free(pma
);
6092 isl_pw_aff_free(pa
);
6095 isl_pw_multi_aff_free(pma
);
6096 isl_pw_aff_free(pa
);
6097 return isl_pw_multi_aff_free(res
);
6100 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6102 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6103 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6104 __isl_take isl_pw_aff
*pa
)
6106 isl_bool equal_params
;
6110 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6111 if (equal_params
< 0)
6114 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6115 if (!isl_space_has_named_params(pma
->dim
) ||
6116 !isl_space_has_named_params(pa
->dim
))
6117 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6118 "unaligned unnamed parameters", goto error
);
6119 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6120 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6121 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6123 isl_pw_multi_aff_free(pma
);
6124 isl_pw_aff_free(pa
);
6128 /* Do the parameters of "pa" match those of "space"?
6130 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6131 __isl_keep isl_space
*space
)
6133 isl_space
*pa_space
;
6137 return isl_bool_error
;
6139 pa_space
= isl_pw_aff_get_space(pa
);
6141 match
= isl_space_has_equal_params(space
, pa_space
);
6143 isl_space_free(pa_space
);
6147 /* Check that the domain space of "pa" matches "space".
6149 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6150 __isl_keep isl_space
*space
)
6152 isl_space
*pa_space
;
6156 return isl_stat_error
;
6158 pa_space
= isl_pw_aff_get_space(pa
);
6160 match
= isl_space_has_equal_params(space
, pa_space
);
6164 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6165 "parameters don't match", goto error
);
6166 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6167 pa_space
, isl_dim_in
);
6171 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6172 "domains don't match", goto error
);
6173 isl_space_free(pa_space
);
6176 isl_space_free(pa_space
);
6177 return isl_stat_error
;
6185 #include <isl_multi_templ.c>
6186 #include <isl_multi_apply_set.c>
6187 #include <isl_multi_coalesce.c>
6188 #include <isl_multi_gist.c>
6189 #include <isl_multi_hash.c>
6190 #include <isl_multi_intersect.c>
6192 /* Scale the elements of "pma" by the corresponding elements of "mv".
6194 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6195 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6198 isl_bool equal_params
;
6200 pma
= isl_pw_multi_aff_cow(pma
);
6203 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6204 mv
->space
, isl_dim_set
))
6205 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6206 "spaces don't match", goto error
);
6207 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6208 if (equal_params
< 0)
6210 if (!equal_params
) {
6211 pma
= isl_pw_multi_aff_align_params(pma
,
6212 isl_multi_val_get_space(mv
));
6213 mv
= isl_multi_val_align_params(mv
,
6214 isl_pw_multi_aff_get_space(pma
));
6219 for (i
= 0; i
< pma
->n
; ++i
) {
6220 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6221 isl_multi_val_copy(mv
));
6222 if (!pma
->p
[i
].maff
)
6226 isl_multi_val_free(mv
);
6229 isl_multi_val_free(mv
);
6230 isl_pw_multi_aff_free(pma
);
6234 /* This function is called for each entry of an isl_union_pw_multi_aff.
6235 * If the space of the entry matches that of data->mv,
6236 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6237 * Otherwise, return an empty isl_pw_multi_aff.
6239 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6240 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6242 isl_multi_val
*mv
= user
;
6246 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6247 mv
->space
, isl_dim_set
)) {
6248 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6249 isl_pw_multi_aff_free(pma
);
6250 return isl_pw_multi_aff_empty(space
);
6253 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6256 /* Scale the elements of "upma" by the corresponding elements of "mv",
6257 * for those entries that match the space of "mv".
6259 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6260 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6262 upma
= isl_union_pw_multi_aff_align_params(upma
,
6263 isl_multi_val_get_space(mv
));
6264 mv
= isl_multi_val_align_params(mv
,
6265 isl_union_pw_multi_aff_get_space(upma
));
6269 return isl_union_pw_multi_aff_transform(upma
,
6270 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6272 isl_multi_val_free(mv
);
6275 isl_multi_val_free(mv
);
6276 isl_union_pw_multi_aff_free(upma
);
6280 /* Construct and return a piecewise multi affine expression
6281 * in the given space with value zero in each of the output dimensions and
6282 * a universe domain.
6284 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6286 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6289 /* Construct and return a piecewise multi affine expression
6290 * that is equal to the given piecewise affine expression.
6292 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6293 __isl_take isl_pw_aff
*pa
)
6297 isl_pw_multi_aff
*pma
;
6302 space
= isl_pw_aff_get_space(pa
);
6303 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6305 for (i
= 0; i
< pa
->n
; ++i
) {
6309 set
= isl_set_copy(pa
->p
[i
].set
);
6310 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6311 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6314 isl_pw_aff_free(pa
);
6318 /* Construct a set or map mapping the shared (parameter) domain
6319 * of the piecewise affine expressions to the range of "mpa"
6320 * with each dimension in the range equated to the
6321 * corresponding piecewise affine expression.
6323 static __isl_give isl_map
*map_from_multi_pw_aff(
6324 __isl_take isl_multi_pw_aff
*mpa
)
6333 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6334 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6335 "invalid space", goto error
);
6337 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6338 map
= isl_map_universe(isl_space_from_domain(space
));
6340 for (i
= 0; i
< mpa
->n
; ++i
) {
6344 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6345 map_i
= map_from_pw_aff(pa
);
6347 map
= isl_map_flat_range_product(map
, map_i
);
6350 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6352 isl_multi_pw_aff_free(mpa
);
6355 isl_multi_pw_aff_free(mpa
);
6359 /* Construct a map mapping the shared domain
6360 * of the piecewise affine expressions to the range of "mpa"
6361 * with each dimension in the range equated to the
6362 * corresponding piecewise affine expression.
6364 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6368 if (isl_space_is_set(mpa
->space
))
6369 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6370 "space of input is not a map", goto error
);
6372 return map_from_multi_pw_aff(mpa
);
6374 isl_multi_pw_aff_free(mpa
);
6378 /* Construct a set mapping the shared parameter domain
6379 * of the piecewise affine expressions to the space of "mpa"
6380 * with each dimension in the range equated to the
6381 * corresponding piecewise affine expression.
6383 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6387 if (!isl_space_is_set(mpa
->space
))
6388 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6389 "space of input is not a set", goto error
);
6391 return map_from_multi_pw_aff(mpa
);
6393 isl_multi_pw_aff_free(mpa
);
6397 /* Construct and return a piecewise multi affine expression
6398 * that is equal to the given multi piecewise affine expression
6399 * on the shared domain of the piecewise affine expressions.
6401 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6402 __isl_take isl_multi_pw_aff
*mpa
)
6407 isl_pw_multi_aff
*pma
;
6412 space
= isl_multi_pw_aff_get_space(mpa
);
6415 isl_multi_pw_aff_free(mpa
);
6416 return isl_pw_multi_aff_zero(space
);
6419 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6420 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6422 for (i
= 1; i
< mpa
->n
; ++i
) {
6423 isl_pw_multi_aff
*pma_i
;
6425 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6426 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6427 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6430 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6432 isl_multi_pw_aff_free(mpa
);
6436 /* Construct and return a multi piecewise affine expression
6437 * that is equal to the given multi affine expression.
6439 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6440 __isl_take isl_multi_aff
*ma
)
6443 isl_multi_pw_aff
*mpa
;
6448 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6449 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6451 for (i
= 0; i
< n
; ++i
) {
6454 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6455 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6458 isl_multi_aff_free(ma
);
6462 /* Construct and return a multi piecewise affine expression
6463 * that is equal to the given piecewise multi affine expression.
6465 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6466 __isl_take isl_pw_multi_aff
*pma
)
6470 isl_multi_pw_aff
*mpa
;
6475 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6476 space
= isl_pw_multi_aff_get_space(pma
);
6477 mpa
= isl_multi_pw_aff_alloc(space
);
6479 for (i
= 0; i
< n
; ++i
) {
6482 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6483 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6486 isl_pw_multi_aff_free(pma
);
6490 /* Do "pa1" and "pa2" represent the same function?
6492 * We first check if they are obviously equal.
6493 * If not, we convert them to maps and check if those are equal.
6495 * If "pa1" or "pa2" contain any NaNs, then they are considered
6496 * not to be the same. A NaN is not equal to anything, not even
6499 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6500 __isl_keep isl_pw_aff
*pa2
)
6504 isl_map
*map1
, *map2
;
6507 return isl_bool_error
;
6509 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6510 if (equal
< 0 || equal
)
6512 has_nan
= either_involves_nan(pa1
, pa2
);
6514 return isl_bool_error
;
6516 return isl_bool_false
;
6518 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6519 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6520 equal
= isl_map_is_equal(map1
, map2
);
6527 /* Do "mpa1" and "mpa2" represent the same function?
6529 * Note that we cannot convert the entire isl_multi_pw_aff
6530 * to a map because the domains of the piecewise affine expressions
6531 * may not be the same.
6533 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6534 __isl_keep isl_multi_pw_aff
*mpa2
)
6537 isl_bool equal
, equal_params
;
6540 return isl_bool_error
;
6542 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
6543 if (equal_params
< 0)
6544 return isl_bool_error
;
6545 if (!equal_params
) {
6546 if (!isl_space_has_named_params(mpa1
->space
))
6547 return isl_bool_false
;
6548 if (!isl_space_has_named_params(mpa2
->space
))
6549 return isl_bool_false
;
6550 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6551 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6552 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6553 isl_multi_pw_aff_get_space(mpa2
));
6554 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6555 isl_multi_pw_aff_get_space(mpa1
));
6556 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6557 isl_multi_pw_aff_free(mpa1
);
6558 isl_multi_pw_aff_free(mpa2
);
6562 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6563 if (equal
< 0 || !equal
)
6566 for (i
= 0; i
< mpa1
->n
; ++i
) {
6567 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6568 if (equal
< 0 || !equal
)
6572 return isl_bool_true
;
6575 /* Do "pma1" and "pma2" represent the same function?
6577 * First check if they are obviously equal.
6578 * If not, then convert them to maps and check if those are equal.
6580 * If "pa1" or "pa2" contain any NaNs, then they are considered
6581 * not to be the same. A NaN is not equal to anything, not even
6584 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
6585 __isl_keep isl_pw_multi_aff
*pma2
)
6589 isl_map
*map1
, *map2
;
6592 return isl_bool_error
;
6594 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
6595 if (equal
< 0 || equal
)
6597 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
6598 if (has_nan
>= 0 && !has_nan
)
6599 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
6600 if (has_nan
< 0 || has_nan
)
6601 return isl_bool_not(has_nan
);
6603 map1
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1
));
6604 map2
= isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2
));
6605 equal
= isl_map_is_equal(map1
, map2
);
6612 /* Compute the pullback of "mpa" by the function represented by "ma".
6613 * In other words, plug in "ma" in "mpa".
6615 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6617 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6618 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6621 isl_space
*space
= NULL
;
6623 mpa
= isl_multi_pw_aff_cow(mpa
);
6627 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6628 isl_multi_pw_aff_get_space(mpa
));
6632 for (i
= 0; i
< mpa
->n
; ++i
) {
6633 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6634 isl_multi_aff_copy(ma
));
6639 isl_multi_aff_free(ma
);
6640 isl_space_free(mpa
->space
);
6644 isl_space_free(space
);
6645 isl_multi_pw_aff_free(mpa
);
6646 isl_multi_aff_free(ma
);
6650 /* Compute the pullback of "mpa" by the function represented by "ma".
6651 * In other words, plug in "ma" in "mpa".
6653 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6654 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6656 isl_bool equal_params
;
6660 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
6661 if (equal_params
< 0)
6664 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6665 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6666 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6667 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6669 isl_multi_pw_aff_free(mpa
);
6670 isl_multi_aff_free(ma
);
6674 /* Compute the pullback of "mpa" by the function represented by "pma".
6675 * In other words, plug in "pma" in "mpa".
6677 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6679 static __isl_give isl_multi_pw_aff
*
6680 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6681 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6684 isl_space
*space
= NULL
;
6686 mpa
= isl_multi_pw_aff_cow(mpa
);
6690 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6691 isl_multi_pw_aff_get_space(mpa
));
6693 for (i
= 0; i
< mpa
->n
; ++i
) {
6694 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6695 isl_pw_multi_aff_copy(pma
));
6700 isl_pw_multi_aff_free(pma
);
6701 isl_space_free(mpa
->space
);
6705 isl_space_free(space
);
6706 isl_multi_pw_aff_free(mpa
);
6707 isl_pw_multi_aff_free(pma
);
6711 /* Compute the pullback of "mpa" by the function represented by "pma".
6712 * In other words, plug in "pma" in "mpa".
6714 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6715 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6717 isl_bool equal_params
;
6721 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
6722 if (equal_params
< 0)
6725 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6726 mpa
= isl_multi_pw_aff_align_params(mpa
,
6727 isl_pw_multi_aff_get_space(pma
));
6728 pma
= isl_pw_multi_aff_align_params(pma
,
6729 isl_multi_pw_aff_get_space(mpa
));
6730 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6732 isl_multi_pw_aff_free(mpa
);
6733 isl_pw_multi_aff_free(pma
);
6737 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6738 * with the domain of "aff". The domain of the result is the same
6740 * "mpa" and "aff" are assumed to have been aligned.
6742 * We first extract the parametric constant from "aff", defined
6743 * over the correct domain.
6744 * Then we add the appropriate combinations of the members of "mpa".
6745 * Finally, we add the integer divisions through recursive calls.
6747 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6748 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6756 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6757 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6759 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6760 tmp
= isl_aff_copy(aff
);
6761 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6762 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6763 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6764 isl_space_dim(space
, isl_dim_set
));
6765 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6766 pa
= isl_pw_aff_from_aff(tmp
);
6768 for (i
= 0; i
< n_in
; ++i
) {
6771 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6773 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6774 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6775 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6776 pa
= isl_pw_aff_add(pa
, pa_i
);
6779 for (i
= 0; i
< n_div
; ++i
) {
6783 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6785 div
= isl_aff_get_div(aff
, i
);
6786 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6787 isl_multi_pw_aff_copy(mpa
), div
);
6788 pa_i
= isl_pw_aff_floor(pa_i
);
6789 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6790 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6791 pa
= isl_pw_aff_add(pa
, pa_i
);
6794 isl_multi_pw_aff_free(mpa
);
6800 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6801 * with the domain of "aff". The domain of the result is the same
6804 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6805 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6807 isl_bool equal_params
;
6811 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
6812 if (equal_params
< 0)
6815 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6817 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6818 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6820 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6823 isl_multi_pw_aff_free(mpa
);
6827 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6828 * with the domain of "pa". The domain of the result is the same
6830 * "mpa" and "pa" are assumed to have been aligned.
6832 * We consider each piece in turn. Note that the domains of the
6833 * pieces are assumed to be disjoint and they remain disjoint
6834 * after taking the preimage (over the same function).
6836 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6837 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6846 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6847 isl_pw_aff_get_space(pa
));
6848 res
= isl_pw_aff_empty(space
);
6850 for (i
= 0; i
< pa
->n
; ++i
) {
6854 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6855 isl_multi_pw_aff_copy(mpa
),
6856 isl_aff_copy(pa
->p
[i
].aff
));
6857 domain
= isl_set_copy(pa
->p
[i
].set
);
6858 domain
= isl_set_preimage_multi_pw_aff(domain
,
6859 isl_multi_pw_aff_copy(mpa
));
6860 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6861 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6864 isl_pw_aff_free(pa
);
6865 isl_multi_pw_aff_free(mpa
);
6868 isl_pw_aff_free(pa
);
6869 isl_multi_pw_aff_free(mpa
);
6873 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6874 * with the domain of "pa". The domain of the result is the same
6877 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6878 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6880 isl_bool equal_params
;
6884 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
6885 if (equal_params
< 0)
6888 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6890 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6891 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6893 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6895 isl_pw_aff_free(pa
);
6896 isl_multi_pw_aff_free(mpa
);
6900 /* Compute the pullback of "pa" by the function represented by "mpa".
6901 * In other words, plug in "mpa" in "pa".
6902 * "pa" and "mpa" are assumed to have been aligned.
6904 * The pullback is computed by applying "pa" to "mpa".
6906 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6907 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6909 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6912 /* Compute the pullback of "pa" by the function represented by "mpa".
6913 * In other words, plug in "mpa" in "pa".
6915 * The pullback is computed by applying "pa" to "mpa".
6917 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6918 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6920 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6923 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6924 * In other words, plug in "mpa2" in "mpa1".
6926 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6928 * We pullback each member of "mpa1" in turn.
6930 static __isl_give isl_multi_pw_aff
*
6931 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6932 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6935 isl_space
*space
= NULL
;
6937 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6941 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6942 isl_multi_pw_aff_get_space(mpa1
));
6944 for (i
= 0; i
< mpa1
->n
; ++i
) {
6945 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6946 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6951 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6953 isl_multi_pw_aff_free(mpa2
);
6956 isl_space_free(space
);
6957 isl_multi_pw_aff_free(mpa1
);
6958 isl_multi_pw_aff_free(mpa2
);
6962 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6963 * In other words, plug in "mpa2" in "mpa1".
6965 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6966 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6968 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6969 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6972 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6973 * of "mpa1" and "mpa2" live in the same space, construct map space
6974 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6975 * with this map space as extract argument.
6977 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6978 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6979 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6980 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6983 isl_space
*space1
, *space2
;
6986 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6987 isl_multi_pw_aff_get_space(mpa2
));
6988 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6989 isl_multi_pw_aff_get_space(mpa1
));
6992 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6993 mpa2
->space
, isl_dim_out
);
6997 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6998 "range spaces don't match", goto error
);
6999 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7000 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7001 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7003 res
= order(mpa1
, mpa2
, space1
);
7004 isl_multi_pw_aff_free(mpa1
);
7005 isl_multi_pw_aff_free(mpa2
);
7008 isl_multi_pw_aff_free(mpa1
);
7009 isl_multi_pw_aff_free(mpa2
);
7013 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7014 * where the function values are equal. "space" is the space of the result.
7015 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7017 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7018 * in the sequences are equal.
7020 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7021 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7022 __isl_take isl_space
*space
)
7027 res
= isl_map_universe(space
);
7029 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7030 for (i
= 0; i
< n
; ++i
) {
7031 isl_pw_aff
*pa1
, *pa2
;
7034 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7035 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7036 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7037 res
= isl_map_intersect(res
, map
);
7043 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7044 * where the function values are equal.
7046 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7047 __isl_take isl_multi_pw_aff
*mpa2
)
7049 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7050 &isl_multi_pw_aff_eq_map_on_space
);
7053 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7054 * where the function values of "mpa1" is lexicographically satisfies "base"
7055 * compared to that of "mpa2". "space" is the space of the result.
7056 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7058 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7059 * if its i-th element satisfies "base" when compared to
7060 * the i-th element of "mpa2" while all previous elements are
7063 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7064 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7065 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7066 __isl_take isl_pw_aff
*pa2
),
7067 __isl_take isl_space
*space
)
7070 isl_map
*res
, *rest
;
7072 res
= isl_map_empty(isl_space_copy(space
));
7073 rest
= isl_map_universe(space
);
7075 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7076 for (i
= 0; i
< n
; ++i
) {
7077 isl_pw_aff
*pa1
, *pa2
;
7080 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7081 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7082 map
= base(pa1
, pa2
);
7083 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7084 res
= isl_map_union(res
, map
);
7089 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7090 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7091 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7092 rest
= isl_map_intersect(rest
, map
);
7099 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7100 * where the function value of "mpa1" is lexicographically less than that
7101 * of "mpa2". "space" is the space of the result.
7102 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7104 * "mpa1" is less than "mpa2" if its i-th element is smaller
7105 * than the i-th element of "mpa2" while all previous elements are
7108 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
7109 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7110 __isl_take isl_space
*space
)
7112 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7113 &isl_pw_aff_lt_map
, space
);
7116 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7117 * where the function value of "mpa1" is lexicographically less than that
7120 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
7121 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7123 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7124 &isl_multi_pw_aff_lex_lt_map_on_space
);
7127 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7128 * where the function value of "mpa1" is lexicographically greater than that
7129 * of "mpa2". "space" is the space of the result.
7130 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7132 * "mpa1" is greater than "mpa2" if its i-th element is greater
7133 * than the i-th element of "mpa2" while all previous elements are
7136 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7137 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7138 __isl_take isl_space
*space
)
7140 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7141 &isl_pw_aff_gt_map
, space
);
7144 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7145 * where the function value of "mpa1" is lexicographically greater than that
7148 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7149 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7151 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7152 &isl_multi_pw_aff_lex_gt_map_on_space
);
7155 /* Compare two isl_affs.
7157 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7158 * than "aff2" and 0 if they are equal.
7160 * The order is fairly arbitrary. We do consider expressions that only involve
7161 * earlier dimensions as "smaller".
7163 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7176 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7180 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7181 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7183 return last1
- last2
;
7185 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7188 /* Compare two isl_pw_affs.
7190 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7191 * than "pa2" and 0 if they are equal.
7193 * The order is fairly arbitrary. We do consider expressions that only involve
7194 * earlier dimensions as "smaller".
7196 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7197 __isl_keep isl_pw_aff
*pa2
)
7210 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7214 if (pa1
->n
!= pa2
->n
)
7215 return pa1
->n
- pa2
->n
;
7217 for (i
= 0; i
< pa1
->n
; ++i
) {
7218 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7221 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7229 /* Return a piecewise affine expression that is equal to "v" on "domain".
7231 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7232 __isl_take isl_val
*v
)
7235 isl_local_space
*ls
;
7238 space
= isl_set_get_space(domain
);
7239 ls
= isl_local_space_from_space(space
);
7240 aff
= isl_aff_val_on_domain(ls
, v
);
7242 return isl_pw_aff_alloc(domain
, aff
);
7245 /* Return a multi affine expression that is equal to "mv" on domain
7248 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7249 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7253 isl_local_space
*ls
;
7259 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7260 space2
= isl_multi_val_get_space(mv
);
7261 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7262 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7263 space
= isl_space_map_from_domain_and_range(space
, space2
);
7264 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7265 ls
= isl_local_space_from_space(isl_space_domain(space
));
7266 for (i
= 0; i
< n
; ++i
) {
7270 v
= isl_multi_val_get_val(mv
, i
);
7271 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7272 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7274 isl_local_space_free(ls
);
7276 isl_multi_val_free(mv
);
7279 isl_space_free(space
);
7280 isl_multi_val_free(mv
);
7284 /* Return a piecewise multi-affine expression
7285 * that is equal to "mv" on "domain".
7287 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7288 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7293 space
= isl_set_get_space(domain
);
7294 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7296 return isl_pw_multi_aff_alloc(domain
, ma
);
7299 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7300 * mv is the value that should be attained on each domain set
7301 * res collects the results
7303 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7305 isl_union_pw_multi_aff
*res
;
7308 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7309 * and add it to data->res.
7311 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7314 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7315 isl_pw_multi_aff
*pma
;
7318 mv
= isl_multi_val_copy(data
->mv
);
7319 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7320 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7322 return data
->res
? isl_stat_ok
: isl_stat_error
;
7325 /* Return a union piecewise multi-affine expression
7326 * that is equal to "mv" on "domain".
7328 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7329 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7331 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7334 space
= isl_union_set_get_space(domain
);
7335 data
.res
= isl_union_pw_multi_aff_empty(space
);
7337 if (isl_union_set_foreach_set(domain
,
7338 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7339 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7340 isl_union_set_free(domain
);
7341 isl_multi_val_free(mv
);
7345 /* Compute the pullback of data->pma by the function represented by "pma2",
7346 * provided the spaces match, and add the results to data->res.
7348 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7350 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7352 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7353 pma2
->dim
, isl_dim_out
)) {
7354 isl_pw_multi_aff_free(pma2
);
7358 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7359 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7361 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7363 return isl_stat_error
;
7368 /* Compute the pullback of "upma1" by the function represented by "upma2".
7370 __isl_give isl_union_pw_multi_aff
*
7371 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7372 __isl_take isl_union_pw_multi_aff
*upma1
,
7373 __isl_take isl_union_pw_multi_aff
*upma2
)
7375 return bin_op(upma1
, upma2
, &pullback_entry
);
7378 /* Check that the domain space of "upa" matches "space".
7380 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7381 * can in principle never fail since the space "space" is that
7382 * of the isl_multi_union_pw_aff and is a set space such that
7383 * there is no domain space to match.
7385 * We check the parameters and double-check that "space" is
7386 * indeed that of a set.
7388 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7389 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7391 isl_space
*upa_space
;
7395 return isl_stat_error
;
7397 match
= isl_space_is_set(space
);
7399 return isl_stat_error
;
7401 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7402 "expecting set space", return -1);
7404 upa_space
= isl_union_pw_aff_get_space(upa
);
7405 match
= isl_space_has_equal_params(space
, upa_space
);
7409 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7410 "parameters don't match", goto error
);
7412 isl_space_free(upa_space
);
7415 isl_space_free(upa_space
);
7416 return isl_stat_error
;
7419 /* Do the parameters of "upa" match those of "space"?
7421 static isl_bool
isl_union_pw_aff_matching_params(
7422 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7424 isl_space
*upa_space
;
7428 return isl_bool_error
;
7430 upa_space
= isl_union_pw_aff_get_space(upa
);
7432 match
= isl_space_has_equal_params(space
, upa_space
);
7434 isl_space_free(upa_space
);
7438 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7439 * space represents the new parameters.
7440 * res collects the results.
7442 struct isl_union_pw_aff_reset_params_data
{
7444 isl_union_pw_aff
*res
;
7447 /* Replace the parameters of "pa" by data->space and
7448 * add the result to data->res.
7450 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7452 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7455 space
= isl_pw_aff_get_space(pa
);
7456 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7457 pa
= isl_pw_aff_reset_space(pa
, space
);
7458 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7460 return data
->res
? isl_stat_ok
: isl_stat_error
;
7463 /* Replace the domain space of "upa" by "space".
7464 * Since a union expression does not have a (single) domain space,
7465 * "space" is necessarily a parameter space.
7467 * Since the order and the names of the parameters determine
7468 * the hash value, we need to create a new hash table.
7470 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7471 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7473 struct isl_union_pw_aff_reset_params_data data
= { space
};
7476 match
= isl_union_pw_aff_matching_params(upa
, space
);
7478 upa
= isl_union_pw_aff_free(upa
);
7480 isl_space_free(space
);
7484 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7485 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7486 data
.res
= isl_union_pw_aff_free(data
.res
);
7488 isl_union_pw_aff_free(upa
);
7489 isl_space_free(space
);
7493 /* Return the floor of "pa".
7495 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7497 return isl_pw_aff_floor(pa
);
7500 /* Given f, return floor(f).
7502 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7503 __isl_take isl_union_pw_aff
*upa
)
7505 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7510 * upa mod m = upa - m * floor(upa/m)
7512 * with m an integer value.
7514 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7515 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7517 isl_union_pw_aff
*res
;
7522 if (!isl_val_is_int(m
))
7523 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7524 "expecting integer modulo", goto error
);
7525 if (!isl_val_is_pos(m
))
7526 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7527 "expecting positive modulo", goto error
);
7529 res
= isl_union_pw_aff_copy(upa
);
7530 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7531 upa
= isl_union_pw_aff_floor(upa
);
7532 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7533 res
= isl_union_pw_aff_sub(res
, upa
);
7538 isl_union_pw_aff_free(upa
);
7542 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7543 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7545 * "res" collects the results.
7547 struct isl_union_pw_aff_aff_on_domain_data
{
7549 isl_union_pw_aff
*res
;
7552 /* Construct a piecewise affine expression that is equal to data->aff
7553 * on "domain" and add the result to data->res.
7555 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7557 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7562 aff
= isl_aff_copy(data
->aff
);
7563 dim
= isl_set_dim(domain
, isl_dim_set
);
7564 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7565 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7566 pa
= isl_pw_aff_alloc(domain
, aff
);
7567 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7569 return data
->res
? isl_stat_ok
: isl_stat_error
;
7572 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7573 * pos is the output position that needs to be extracted.
7574 * res collects the results.
7576 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7578 isl_union_pw_aff
*res
;
7581 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7582 * (assuming it has such a dimension) and add it to data->res.
7584 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7586 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7591 return isl_stat_error
;
7593 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7594 if (data
->pos
>= n_out
) {
7595 isl_pw_multi_aff_free(pma
);
7599 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7600 isl_pw_multi_aff_free(pma
);
7602 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7604 return data
->res
? isl_stat_ok
: isl_stat_error
;
7607 /* Extract an isl_union_pw_aff corresponding to
7608 * output dimension "pos" of "upma".
7610 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7611 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7613 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7620 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7621 "cannot extract at negative position", return NULL
);
7623 space
= isl_union_pw_multi_aff_get_space(upma
);
7624 data
.res
= isl_union_pw_aff_empty(space
);
7626 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7627 &get_union_pw_aff
, &data
) < 0)
7628 data
.res
= isl_union_pw_aff_free(data
.res
);
7633 /* Return a union piecewise affine expression
7634 * that is equal to "aff" on "domain".
7636 * Construct an isl_pw_aff on each of the sets in "domain" and
7637 * collect the results.
7639 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7640 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7642 struct isl_union_pw_aff_aff_on_domain_data data
;
7645 if (!domain
|| !aff
)
7647 if (!isl_local_space_is_params(aff
->ls
))
7648 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7649 "expecting parametric expression", goto error
);
7651 space
= isl_union_set_get_space(domain
);
7652 data
.res
= isl_union_pw_aff_empty(space
);
7654 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7655 data
.res
= isl_union_pw_aff_free(data
.res
);
7656 isl_union_set_free(domain
);
7660 isl_union_set_free(domain
);
7665 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7666 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7667 * "res" collects the results.
7669 struct isl_union_pw_aff_val_on_domain_data
{
7671 isl_union_pw_aff
*res
;
7674 /* Construct a piecewise affine expression that is equal to data->v
7675 * on "domain" and add the result to data->res.
7677 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7679 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7683 v
= isl_val_copy(data
->v
);
7684 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7685 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7687 return data
->res
? isl_stat_ok
: isl_stat_error
;
7690 /* Return a union piecewise affine expression
7691 * that is equal to "v" on "domain".
7693 * Construct an isl_pw_aff on each of the sets in "domain" and
7694 * collect the results.
7696 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7697 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7699 struct isl_union_pw_aff_val_on_domain_data data
;
7702 space
= isl_union_set_get_space(domain
);
7703 data
.res
= isl_union_pw_aff_empty(space
);
7705 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7706 data
.res
= isl_union_pw_aff_free(data
.res
);
7707 isl_union_set_free(domain
);
7712 /* Construct a piecewise multi affine expression
7713 * that is equal to "pa" and add it to upma.
7715 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7718 isl_union_pw_multi_aff
**upma
= user
;
7719 isl_pw_multi_aff
*pma
;
7721 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7722 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7724 return *upma
? isl_stat_ok
: isl_stat_error
;
7727 /* Construct and return a union piecewise multi affine expression
7728 * that is equal to the given union piecewise affine expression.
7730 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7731 __isl_take isl_union_pw_aff
*upa
)
7734 isl_union_pw_multi_aff
*upma
;
7739 space
= isl_union_pw_aff_get_space(upa
);
7740 upma
= isl_union_pw_multi_aff_empty(space
);
7742 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7743 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7744 upma
= isl_union_pw_multi_aff_free(upma
);
7746 isl_union_pw_aff_free(upa
);
7750 /* Compute the set of elements in the domain of "pa" where it is zero and
7751 * add this set to "uset".
7753 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7755 isl_union_set
**uset
= (isl_union_set
**)user
;
7757 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7759 return *uset
? isl_stat_ok
: isl_stat_error
;
7762 /* Return a union set containing those elements in the domain
7763 * of "upa" where it is zero.
7765 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7766 __isl_take isl_union_pw_aff
*upa
)
7768 isl_union_set
*zero
;
7770 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7771 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7772 zero
= isl_union_set_free(zero
);
7774 isl_union_pw_aff_free(upa
);
7778 /* Convert "pa" to an isl_map and add it to *umap.
7780 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7782 isl_union_map
**umap
= user
;
7785 map
= isl_map_from_pw_aff(pa
);
7786 *umap
= isl_union_map_add_map(*umap
, map
);
7788 return *umap
? isl_stat_ok
: isl_stat_error
;
7791 /* Construct a union map mapping the domain of the union
7792 * piecewise affine expression to its range, with the single output dimension
7793 * equated to the corresponding affine expressions on their cells.
7795 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7796 __isl_take isl_union_pw_aff
*upa
)
7799 isl_union_map
*umap
;
7804 space
= isl_union_pw_aff_get_space(upa
);
7805 umap
= isl_union_map_empty(space
);
7807 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7809 umap
= isl_union_map_free(umap
);
7811 isl_union_pw_aff_free(upa
);
7815 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7816 * upma is the function that is plugged in.
7817 * pa is the current part of the function in which upma is plugged in.
7818 * res collects the results.
7820 struct isl_union_pw_aff_pullback_upma_data
{
7821 isl_union_pw_multi_aff
*upma
;
7823 isl_union_pw_aff
*res
;
7826 /* Check if "pma" can be plugged into data->pa.
7827 * If so, perform the pullback and add the result to data->res.
7829 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7831 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7834 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7835 pma
->dim
, isl_dim_out
)) {
7836 isl_pw_multi_aff_free(pma
);
7840 pa
= isl_pw_aff_copy(data
->pa
);
7841 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7843 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7845 return data
->res
? isl_stat_ok
: isl_stat_error
;
7848 /* Check if any of the elements of data->upma can be plugged into pa,
7849 * add if so add the result to data->res.
7851 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7853 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7857 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7859 isl_pw_aff_free(pa
);
7864 /* Compute the pullback of "upa" by the function represented by "upma".
7865 * In other words, plug in "upma" in "upa". The result contains
7866 * expressions defined over the domain space of "upma".
7868 * Run over all pairs of elements in "upa" and "upma", perform
7869 * the pullback when appropriate and collect the results.
7870 * If the hash value were based on the domain space rather than
7871 * the function space, then we could run through all elements
7872 * of "upma" and directly pick out the corresponding element of "upa".
7874 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7875 __isl_take isl_union_pw_aff
*upa
,
7876 __isl_take isl_union_pw_multi_aff
*upma
)
7878 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7881 space
= isl_union_pw_multi_aff_get_space(upma
);
7882 upa
= isl_union_pw_aff_align_params(upa
, space
);
7883 space
= isl_union_pw_aff_get_space(upa
);
7884 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7890 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7891 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7892 data
.res
= isl_union_pw_aff_free(data
.res
);
7894 isl_union_pw_aff_free(upa
);
7895 isl_union_pw_multi_aff_free(upma
);
7898 isl_union_pw_aff_free(upa
);
7899 isl_union_pw_multi_aff_free(upma
);
7904 #define BASE union_pw_aff
7906 #define DOMBASE union_set
7908 #define NO_MOVE_DIMS
7917 #include <isl_multi_templ.c>
7918 #include <isl_multi_apply_set.c>
7919 #include <isl_multi_apply_union_set.c>
7920 #include <isl_multi_coalesce.c>
7921 #include <isl_multi_floor.c>
7922 #include <isl_multi_gist.c>
7923 #include <isl_multi_intersect.c>
7925 /* Construct a multiple union piecewise affine expression
7926 * in the given space with value zero in each of the output dimensions.
7928 * Since there is no canonical zero value for
7929 * a union piecewise affine expression, we can only construct
7930 * a zero-dimensional "zero" value.
7932 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7933 __isl_take isl_space
*space
)
7940 params
= isl_space_is_params(space
);
7944 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7945 "expecting proper set space", goto error
);
7946 if (!isl_space_is_set(space
))
7947 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7948 "expecting set space", goto error
);
7949 if (isl_space_dim(space
, isl_dim_out
) != 0)
7950 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7951 "expecting 0D space", goto error
);
7953 return isl_multi_union_pw_aff_alloc(space
);
7955 isl_space_free(space
);
7959 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7960 * with the actual sum on the shared domain and
7961 * the defined expression on the symmetric difference of the domains.
7963 * We simply iterate over the elements in both arguments and
7964 * call isl_union_pw_aff_union_add on each of them.
7966 static __isl_give isl_multi_union_pw_aff
*
7967 isl_multi_union_pw_aff_union_add_aligned(
7968 __isl_take isl_multi_union_pw_aff
*mupa1
,
7969 __isl_take isl_multi_union_pw_aff
*mupa2
)
7971 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7972 &isl_union_pw_aff_union_add
);
7975 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7976 * with the actual sum on the shared domain and
7977 * the defined expression on the symmetric difference of the domains.
7979 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7980 __isl_take isl_multi_union_pw_aff
*mupa1
,
7981 __isl_take isl_multi_union_pw_aff
*mupa2
)
7983 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7984 &isl_multi_union_pw_aff_union_add_aligned
);
7987 /* Construct and return a multi union piecewise affine expression
7988 * that is equal to the given multi affine expression.
7990 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7991 __isl_take isl_multi_aff
*ma
)
7993 isl_multi_pw_aff
*mpa
;
7995 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7996 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7999 /* Construct and return a multi union piecewise affine expression
8000 * that is equal to the given multi piecewise affine expression.
8002 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8003 __isl_take isl_multi_pw_aff
*mpa
)
8007 isl_multi_union_pw_aff
*mupa
;
8012 space
= isl_multi_pw_aff_get_space(mpa
);
8013 space
= isl_space_range(space
);
8014 mupa
= isl_multi_union_pw_aff_alloc(space
);
8016 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8017 for (i
= 0; i
< n
; ++i
) {
8019 isl_union_pw_aff
*upa
;
8021 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8022 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8023 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8026 isl_multi_pw_aff_free(mpa
);
8031 /* Extract the range space of "pma" and assign it to *space.
8032 * If *space has already been set (through a previous call to this function),
8033 * then check that the range space is the same.
8035 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8037 isl_space
**space
= user
;
8038 isl_space
*pma_space
;
8041 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8042 isl_pw_multi_aff_free(pma
);
8045 return isl_stat_error
;
8051 equal
= isl_space_is_equal(pma_space
, *space
);
8052 isl_space_free(pma_space
);
8055 return isl_stat_error
;
8057 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8058 "range spaces not the same", return isl_stat_error
);
8062 /* Construct and return a multi union piecewise affine expression
8063 * that is equal to the given union piecewise multi affine expression.
8065 * In order to be able to perform the conversion, the input
8066 * needs to be non-empty and may only involve a single range space.
8068 __isl_give isl_multi_union_pw_aff
*
8069 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8070 __isl_take isl_union_pw_multi_aff
*upma
)
8072 isl_space
*space
= NULL
;
8073 isl_multi_union_pw_aff
*mupa
;
8078 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
8079 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8080 "cannot extract range space from empty input",
8082 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8089 n
= isl_space_dim(space
, isl_dim_set
);
8090 mupa
= isl_multi_union_pw_aff_alloc(space
);
8092 for (i
= 0; i
< n
; ++i
) {
8093 isl_union_pw_aff
*upa
;
8095 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8096 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8099 isl_union_pw_multi_aff_free(upma
);
8102 isl_space_free(space
);
8103 isl_union_pw_multi_aff_free(upma
);
8107 /* Try and create an isl_multi_union_pw_aff that is equivalent
8108 * to the given isl_union_map.
8109 * The isl_union_map is required to be single-valued in each space.
8110 * Moreover, it cannot be empty and all range spaces need to be the same.
8111 * Otherwise, an error is produced.
8113 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8114 __isl_take isl_union_map
*umap
)
8116 isl_union_pw_multi_aff
*upma
;
8118 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8119 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8122 /* Return a multiple union piecewise affine expression
8123 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8124 * have been aligned.
8126 static __isl_give isl_multi_union_pw_aff
*
8127 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8128 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8132 isl_multi_union_pw_aff
*mupa
;
8137 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8138 space
= isl_multi_val_get_space(mv
);
8139 mupa
= isl_multi_union_pw_aff_alloc(space
);
8140 for (i
= 0; i
< n
; ++i
) {
8142 isl_union_pw_aff
*upa
;
8144 v
= isl_multi_val_get_val(mv
, i
);
8145 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8147 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8150 isl_union_set_free(domain
);
8151 isl_multi_val_free(mv
);
8154 isl_union_set_free(domain
);
8155 isl_multi_val_free(mv
);
8159 /* Return a multiple union piecewise affine expression
8160 * that is equal to "mv" on "domain".
8162 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8163 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8165 isl_bool equal_params
;
8169 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8170 if (equal_params
< 0)
8173 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8175 domain
= isl_union_set_align_params(domain
,
8176 isl_multi_val_get_space(mv
));
8177 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8178 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8180 isl_union_set_free(domain
);
8181 isl_multi_val_free(mv
);
8185 /* Return a multiple union piecewise affine expression
8186 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8187 * have been aligned.
8189 static __isl_give isl_multi_union_pw_aff
*
8190 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8191 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8195 isl_multi_union_pw_aff
*mupa
;
8200 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8201 space
= isl_multi_aff_get_space(ma
);
8202 mupa
= isl_multi_union_pw_aff_alloc(space
);
8203 for (i
= 0; i
< n
; ++i
) {
8205 isl_union_pw_aff
*upa
;
8207 aff
= isl_multi_aff_get_aff(ma
, i
);
8208 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8210 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8213 isl_union_set_free(domain
);
8214 isl_multi_aff_free(ma
);
8217 isl_union_set_free(domain
);
8218 isl_multi_aff_free(ma
);
8222 /* Return a multiple union piecewise affine expression
8223 * that is equal to "ma" on "domain".
8225 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8226 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8228 isl_bool equal_params
;
8232 equal_params
= isl_space_has_equal_params(domain
->dim
, ma
->space
);
8233 if (equal_params
< 0)
8236 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8238 domain
= isl_union_set_align_params(domain
,
8239 isl_multi_aff_get_space(ma
));
8240 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8241 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8243 isl_union_set_free(domain
);
8244 isl_multi_aff_free(ma
);
8248 /* Return a union set containing those elements in the domains
8249 * of the elements of "mupa" where they are all zero.
8251 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8252 __isl_take isl_multi_union_pw_aff
*mupa
)
8255 isl_union_pw_aff
*upa
;
8256 isl_union_set
*zero
;
8261 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8263 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8264 "cannot determine zero set "
8265 "of zero-dimensional function", goto error
);
8267 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8268 zero
= isl_union_pw_aff_zero_union_set(upa
);
8270 for (i
= 1; i
< n
; ++i
) {
8271 isl_union_set
*zero_i
;
8273 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8274 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8276 zero
= isl_union_set_intersect(zero
, zero_i
);
8279 isl_multi_union_pw_aff_free(mupa
);
8282 isl_multi_union_pw_aff_free(mupa
);
8286 /* Construct a union map mapping the shared domain
8287 * of the union piecewise affine expressions to the range of "mupa"
8288 * with each dimension in the range equated to the
8289 * corresponding union piecewise affine expression.
8291 * The input cannot be zero-dimensional as there is
8292 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8294 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8295 __isl_take isl_multi_union_pw_aff
*mupa
)
8299 isl_union_map
*umap
;
8300 isl_union_pw_aff
*upa
;
8305 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8307 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8308 "cannot determine domain of zero-dimensional "
8309 "isl_multi_union_pw_aff", goto error
);
8311 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8312 umap
= isl_union_map_from_union_pw_aff(upa
);
8314 for (i
= 1; i
< n
; ++i
) {
8315 isl_union_map
*umap_i
;
8317 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8318 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8319 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8322 space
= isl_multi_union_pw_aff_get_space(mupa
);
8323 umap
= isl_union_map_reset_range_space(umap
, space
);
8325 isl_multi_union_pw_aff_free(mupa
);
8328 isl_multi_union_pw_aff_free(mupa
);
8332 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8333 * "range" is the space from which to set the range space.
8334 * "res" collects the results.
8336 struct isl_union_pw_multi_aff_reset_range_space_data
{
8338 isl_union_pw_multi_aff
*res
;
8341 /* Replace the range space of "pma" by the range space of data->range and
8342 * add the result to data->res.
8344 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8346 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8349 space
= isl_pw_multi_aff_get_space(pma
);
8350 space
= isl_space_domain(space
);
8351 space
= isl_space_extend_domain_with_range(space
,
8352 isl_space_copy(data
->range
));
8353 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8354 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8356 return data
->res
? isl_stat_ok
: isl_stat_error
;
8359 /* Replace the range space of all the piecewise affine expressions in "upma" by
8360 * the range space of "space".
8362 * This assumes that all these expressions have the same output dimension.
8364 * Since the spaces of the expressions change, so do their hash values.
8365 * We therefore need to create a new isl_union_pw_multi_aff.
8366 * Note that the hash value is currently computed based on the entire
8367 * space even though there can only be a single expression with a given
8370 static __isl_give isl_union_pw_multi_aff
*
8371 isl_union_pw_multi_aff_reset_range_space(
8372 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8374 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8375 isl_space
*space_upma
;
8377 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8378 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8379 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8380 &reset_range_space
, &data
) < 0)
8381 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8383 isl_space_free(space
);
8384 isl_union_pw_multi_aff_free(upma
);
8388 /* Construct and return a union piecewise multi affine expression
8389 * that is equal to the given multi union piecewise affine expression.
8391 * In order to be able to perform the conversion, the input
8392 * needs to have a least one output dimension.
8394 __isl_give isl_union_pw_multi_aff
*
8395 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8396 __isl_take isl_multi_union_pw_aff
*mupa
)
8400 isl_union_pw_multi_aff
*upma
;
8401 isl_union_pw_aff
*upa
;
8406 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8408 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8409 "cannot determine domain of zero-dimensional "
8410 "isl_multi_union_pw_aff", goto error
);
8412 space
= isl_multi_union_pw_aff_get_space(mupa
);
8413 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8414 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8416 for (i
= 1; i
< n
; ++i
) {
8417 isl_union_pw_multi_aff
*upma_i
;
8419 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8420 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8421 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8424 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8426 isl_multi_union_pw_aff_free(mupa
);
8429 isl_multi_union_pw_aff_free(mupa
);
8433 /* Intersect the range of "mupa" with "range".
8434 * That is, keep only those domain elements that have a function value
8437 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8438 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8440 isl_union_pw_multi_aff
*upma
;
8441 isl_union_set
*domain
;
8446 if (!mupa
|| !range
)
8449 space
= isl_set_get_space(range
);
8450 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8451 space
, isl_dim_set
);
8452 isl_space_free(space
);
8456 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8457 "space don't match", goto error
);
8458 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8460 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8461 "cannot intersect range of zero-dimensional "
8462 "isl_multi_union_pw_aff", goto error
);
8464 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8465 isl_multi_union_pw_aff_copy(mupa
));
8466 domain
= isl_union_set_from_set(range
);
8467 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8468 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8472 isl_multi_union_pw_aff_free(mupa
);
8473 isl_set_free(range
);
8477 /* Return the shared domain of the elements of "mupa".
8479 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8480 __isl_take isl_multi_union_pw_aff
*mupa
)
8483 isl_union_pw_aff
*upa
;
8489 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8491 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8492 "cannot determine domain", goto error
);
8494 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8495 dom
= isl_union_pw_aff_domain(upa
);
8496 for (i
= 1; i
< n
; ++i
) {
8497 isl_union_set
*dom_i
;
8499 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8500 dom_i
= isl_union_pw_aff_domain(upa
);
8501 dom
= isl_union_set_intersect(dom
, dom_i
);
8504 isl_multi_union_pw_aff_free(mupa
);
8507 isl_multi_union_pw_aff_free(mupa
);
8511 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8512 * In particular, the spaces have been aligned.
8513 * The result is defined over the shared domain of the elements of "mupa"
8515 * We first extract the parametric constant part of "aff" and
8516 * define that over the shared domain.
8517 * Then we iterate over all input dimensions of "aff" and add the corresponding
8518 * multiples of the elements of "mupa".
8519 * Finally, we consider the integer divisions, calling the function
8520 * recursively to obtain an isl_union_pw_aff corresponding to the
8521 * integer division argument.
8523 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8524 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8527 isl_union_pw_aff
*upa
;
8528 isl_union_set
*uset
;
8532 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8533 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8535 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8536 cst
= isl_aff_copy(aff
);
8537 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8538 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8539 cst
= isl_aff_project_domain_on_params(cst
);
8540 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8542 for (i
= 0; i
< n_in
; ++i
) {
8543 isl_union_pw_aff
*upa_i
;
8545 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8547 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8548 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8549 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8550 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8553 for (i
= 0; i
< n_div
; ++i
) {
8555 isl_union_pw_aff
*upa_i
;
8557 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8559 div
= isl_aff_get_div(aff
, i
);
8560 upa_i
= multi_union_pw_aff_apply_aff(
8561 isl_multi_union_pw_aff_copy(mupa
), div
);
8562 upa_i
= isl_union_pw_aff_floor(upa_i
);
8563 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8564 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8565 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8568 isl_multi_union_pw_aff_free(mupa
);
8574 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8575 * with the domain of "aff".
8576 * Furthermore, the dimension of this space needs to be greater than zero.
8577 * The result is defined over the shared domain of the elements of "mupa"
8579 * We perform these checks and then hand over control to
8580 * multi_union_pw_aff_apply_aff.
8582 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8583 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8585 isl_space
*space1
, *space2
;
8588 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8589 isl_aff_get_space(aff
));
8590 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8594 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8595 space2
= isl_aff_get_domain_space(aff
);
8596 equal
= isl_space_is_equal(space1
, space2
);
8597 isl_space_free(space1
);
8598 isl_space_free(space2
);
8602 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8603 "spaces don't match", goto error
);
8604 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8605 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8606 "cannot determine domains", goto error
);
8608 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8610 isl_multi_union_pw_aff_free(mupa
);
8615 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8616 * with the domain of "ma".
8617 * Furthermore, the dimension of this space needs to be greater than zero,
8618 * unless the dimension of the target space of "ma" is also zero.
8619 * The result is defined over the shared domain of the elements of "mupa"
8621 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8622 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8624 isl_space
*space1
, *space2
;
8625 isl_multi_union_pw_aff
*res
;
8629 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8630 isl_multi_aff_get_space(ma
));
8631 ma
= isl_multi_aff_align_params(ma
,
8632 isl_multi_union_pw_aff_get_space(mupa
));
8636 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8637 space2
= isl_multi_aff_get_domain_space(ma
);
8638 equal
= isl_space_is_equal(space1
, space2
);
8639 isl_space_free(space1
);
8640 isl_space_free(space2
);
8644 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8645 "spaces don't match", goto error
);
8646 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8647 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8648 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8649 "cannot determine domains", goto error
);
8651 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8652 res
= isl_multi_union_pw_aff_alloc(space1
);
8654 for (i
= 0; i
< n_out
; ++i
) {
8656 isl_union_pw_aff
*upa
;
8658 aff
= isl_multi_aff_get_aff(ma
, i
);
8659 upa
= multi_union_pw_aff_apply_aff(
8660 isl_multi_union_pw_aff_copy(mupa
), aff
);
8661 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8664 isl_multi_aff_free(ma
);
8665 isl_multi_union_pw_aff_free(mupa
);
8668 isl_multi_union_pw_aff_free(mupa
);
8669 isl_multi_aff_free(ma
);
8673 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8674 * with the domain of "pa".
8675 * Furthermore, the dimension of this space needs to be greater than zero.
8676 * The result is defined over the shared domain of the elements of "mupa"
8678 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8679 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8683 isl_space
*space
, *space2
;
8684 isl_union_pw_aff
*upa
;
8686 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8687 isl_pw_aff_get_space(pa
));
8688 pa
= isl_pw_aff_align_params(pa
,
8689 isl_multi_union_pw_aff_get_space(mupa
));
8693 space
= isl_multi_union_pw_aff_get_space(mupa
);
8694 space2
= isl_pw_aff_get_domain_space(pa
);
8695 equal
= isl_space_is_equal(space
, space2
);
8696 isl_space_free(space
);
8697 isl_space_free(space2
);
8701 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8702 "spaces don't match", goto error
);
8703 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8704 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8705 "cannot determine domains", goto error
);
8707 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8708 upa
= isl_union_pw_aff_empty(space
);
8710 for (i
= 0; i
< pa
->n
; ++i
) {
8713 isl_multi_union_pw_aff
*mupa_i
;
8714 isl_union_pw_aff
*upa_i
;
8716 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8717 domain
= isl_set_copy(pa
->p
[i
].set
);
8718 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8719 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8720 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8721 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8724 isl_multi_union_pw_aff_free(mupa
);
8725 isl_pw_aff_free(pa
);
8728 isl_multi_union_pw_aff_free(mupa
);
8729 isl_pw_aff_free(pa
);
8733 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8734 * with the domain of "pma".
8735 * Furthermore, the dimension of this space needs to be greater than zero,
8736 * unless the dimension of the target space of "pma" is also zero.
8737 * The result is defined over the shared domain of the elements of "mupa"
8739 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8740 __isl_take isl_multi_union_pw_aff
*mupa
,
8741 __isl_take isl_pw_multi_aff
*pma
)
8743 isl_space
*space1
, *space2
;
8744 isl_multi_union_pw_aff
*res
;
8748 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8749 isl_pw_multi_aff_get_space(pma
));
8750 pma
= isl_pw_multi_aff_align_params(pma
,
8751 isl_multi_union_pw_aff_get_space(mupa
));
8755 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8756 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8757 equal
= isl_space_is_equal(space1
, space2
);
8758 isl_space_free(space1
);
8759 isl_space_free(space2
);
8763 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8764 "spaces don't match", goto error
);
8765 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8766 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8767 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8768 "cannot determine domains", goto error
);
8770 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8771 res
= isl_multi_union_pw_aff_alloc(space1
);
8773 for (i
= 0; i
< n_out
; ++i
) {
8775 isl_union_pw_aff
*upa
;
8777 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8778 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8779 isl_multi_union_pw_aff_copy(mupa
), pa
);
8780 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8783 isl_pw_multi_aff_free(pma
);
8784 isl_multi_union_pw_aff_free(mupa
);
8787 isl_multi_union_pw_aff_free(mupa
);
8788 isl_pw_multi_aff_free(pma
);
8792 /* Compute the pullback of "mupa" by the function represented by "upma".
8793 * In other words, plug in "upma" in "mupa". The result contains
8794 * expressions defined over the domain space of "upma".
8796 * Run over all elements of "mupa" and plug in "upma" in each of them.
8798 __isl_give isl_multi_union_pw_aff
*
8799 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8800 __isl_take isl_multi_union_pw_aff
*mupa
,
8801 __isl_take isl_union_pw_multi_aff
*upma
)
8805 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8806 isl_union_pw_multi_aff_get_space(upma
));
8807 upma
= isl_union_pw_multi_aff_align_params(upma
,
8808 isl_multi_union_pw_aff_get_space(mupa
));
8812 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8813 for (i
= 0; i
< n
; ++i
) {
8814 isl_union_pw_aff
*upa
;
8816 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8817 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8818 isl_union_pw_multi_aff_copy(upma
));
8819 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8822 isl_union_pw_multi_aff_free(upma
);
8825 isl_multi_union_pw_aff_free(mupa
);
8826 isl_union_pw_multi_aff_free(upma
);
8830 /* Extract the sequence of elements in "mupa" with domain space "space"
8831 * (ignoring parameters).
8833 * For the elements of "mupa" that are not defined on the specified space,
8834 * the corresponding element in the result is empty.
8836 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8837 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8840 isl_bool equal_params
;
8841 isl_space
*space_mpa
= NULL
;
8842 isl_multi_pw_aff
*mpa
;
8844 if (!mupa
|| !space
)
8847 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8848 equal_params
= isl_space_has_equal_params(space_mpa
, space
);
8849 if (equal_params
< 0)
8851 if (!equal_params
) {
8852 space
= isl_space_drop_dims(space
, isl_dim_param
,
8853 0, isl_space_dim(space
, isl_dim_param
));
8854 space
= isl_space_align_params(space
,
8855 isl_space_copy(space_mpa
));
8859 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8861 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8863 space
= isl_space_from_domain(space
);
8864 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8865 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8866 for (i
= 0; i
< n
; ++i
) {
8867 isl_union_pw_aff
*upa
;
8870 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8871 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8872 isl_space_copy(space
));
8873 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8874 isl_union_pw_aff_free(upa
);
8877 isl_space_free(space
);
8880 isl_space_free(space_mpa
);
8881 isl_space_free(space
);