2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
39 #include <isl_list_templ.c>
42 #define EL_BASE pw_aff
44 #include <isl_list_templ.c>
47 #define EL_BASE pw_multi_aff
49 #include <isl_list_templ.c>
52 #define EL_BASE union_pw_aff
54 #include <isl_list_templ.c>
57 #define EL_BASE union_pw_multi_aff
59 #include <isl_list_templ.c>
61 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
62 __isl_take isl_vec
*v
)
69 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
79 isl_local_space_free(ls
);
84 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
93 ctx
= isl_local_space_get_ctx(ls
);
94 if (!isl_local_space_divs_known(ls
))
95 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
97 if (!isl_local_space_is_set(ls
))
98 isl_die(ctx
, isl_error_invalid
,
99 "domain of affine expression should be a set",
102 total
= isl_local_space_dim(ls
, isl_dim_all
);
105 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
106 return isl_aff_alloc_vec(ls
, v
);
108 isl_local_space_free(ls
);
112 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
121 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
126 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
127 isl_vec_copy(aff
->v
));
130 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
138 return isl_aff_dup(aff
);
141 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
145 aff
= isl_aff_alloc(ls
);
149 isl_int_set_si(aff
->v
->el
[0], 1);
150 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
155 /* Return an affine expression that is equal to zero on domain space "space".
157 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
159 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
162 /* This function performs the same operation as isl_aff_zero_on_domain_space,
163 * but is considered as a function on an isl_space when exported.
165 __isl_give isl_aff
*isl_space_zero_aff_on_domain(__isl_take isl_space
*space
)
167 return isl_aff_zero_on_domain_space(space
);
170 /* Return a piecewise affine expression defined on the specified domain
171 * that is equal to zero.
173 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
175 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
178 /* Change "aff" into a NaN.
180 * Note that this function gets called from isl_aff_nan_on_domain,
181 * so "aff" may not have been initialized yet.
183 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
185 aff
= isl_aff_cow(aff
);
189 aff
->v
= isl_vec_clr(aff
->v
);
191 return isl_aff_free(aff
);
196 /* Return an affine expression defined on the specified domain
197 * that represents NaN.
199 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
203 aff
= isl_aff_alloc(ls
);
204 return isl_aff_set_nan(aff
);
207 /* Return an affine expression defined on the specified domain space
208 * that represents NaN.
210 __isl_give isl_aff
*isl_aff_nan_on_domain_space(__isl_take isl_space
*space
)
212 return isl_aff_nan_on_domain(isl_local_space_from_space(space
));
215 /* Return a piecewise affine expression defined on the specified domain space
216 * that represents NaN.
218 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain_space(
219 __isl_take isl_space
*space
)
221 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space
));
224 /* Return a piecewise affine expression defined on the specified domain
225 * that represents NaN.
227 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
229 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
232 /* Return an affine expression that is equal to "val" on
233 * domain local space "ls".
235 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
236 __isl_take isl_val
*val
)
242 if (!isl_val_is_rat(val
))
243 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
244 "expecting rational value", goto error
);
246 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
250 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
251 isl_int_set(aff
->v
->el
[1], val
->n
);
252 isl_int_set(aff
->v
->el
[0], val
->d
);
254 isl_local_space_free(ls
);
258 isl_local_space_free(ls
);
263 /* Return an affine expression that is equal to "val" on domain space "space".
265 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
266 __isl_take isl_val
*val
)
268 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
271 /* Return an affine expression that is equal to the specified dimension
274 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
275 enum isl_dim_type type
, unsigned pos
)
283 space
= isl_local_space_get_space(ls
);
286 if (isl_space_is_map(space
))
287 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
288 "expecting (parameter) set space", goto error
);
289 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
292 isl_space_free(space
);
293 aff
= isl_aff_alloc(ls
);
297 pos
+= isl_local_space_offset(aff
->ls
, type
);
299 isl_int_set_si(aff
->v
->el
[0], 1);
300 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
301 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
305 isl_local_space_free(ls
);
306 isl_space_free(space
);
310 /* Return a piecewise affine expression that is equal to
311 * the specified dimension in "ls".
313 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
314 enum isl_dim_type type
, unsigned pos
)
316 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
319 /* Return an affine expression that is equal to the parameter
320 * in the domain space "space" with identifier "id".
322 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
323 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
330 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
332 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
333 "parameter not found in space", goto error
);
335 ls
= isl_local_space_from_space(space
);
336 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
338 isl_space_free(space
);
343 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
351 isl_local_space_free(aff
->ls
);
352 isl_vec_free(aff
->v
);
359 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
361 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
364 /* Return a hash value that digests "aff".
366 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
368 uint32_t hash
, ls_hash
, v_hash
;
373 hash
= isl_hash_init();
374 ls_hash
= isl_local_space_get_hash(aff
->ls
);
375 isl_hash_hash(hash
, ls_hash
);
376 v_hash
= isl_vec_get_hash(aff
->v
);
377 isl_hash_hash(hash
, v_hash
);
382 /* Return the domain local space of "aff".
384 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
385 __isl_keep isl_aff
*aff
)
387 return aff
? aff
->ls
: NULL
;
390 /* Return the number of variables of the given type in the domain of "aff".
392 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
396 ls
= isl_aff_peek_domain_local_space(aff
);
397 return isl_local_space_dim(ls
, type
);
400 /* Externally, an isl_aff has a map space, but internally, the
401 * ls field corresponds to the domain of that space.
403 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
406 return isl_size_error
;
407 if (type
== isl_dim_out
)
409 if (type
== isl_dim_in
)
411 return isl_aff_domain_dim(aff
, type
);
414 /* Return the offset of the first coefficient of type "type" in
415 * the domain of "aff".
417 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
421 ls
= isl_aff_peek_domain_local_space(aff
);
422 return isl_local_space_offset(ls
, type
);
425 /* Return the position of the dimension of the given type and name
427 * Return -1 if no such dimension can be found.
429 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
434 if (type
== isl_dim_out
)
436 if (type
== isl_dim_in
)
438 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
441 /* Return the domain space of "aff".
443 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
445 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
448 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
450 return isl_space_copy(isl_aff_peek_domain_space(aff
));
453 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
458 space
= isl_local_space_get_space(aff
->ls
);
459 space
= isl_space_from_domain(space
);
460 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
464 /* Return a copy of the domain space of "aff".
466 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
467 __isl_keep isl_aff
*aff
)
469 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
472 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
477 ls
= isl_local_space_copy(aff
->ls
);
478 ls
= isl_local_space_from_domain(ls
);
479 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
483 /* Return the local space of the domain of "aff".
484 * This may be either a copy or the local space itself
485 * if there is only one reference to "aff".
486 * This allows the local space to be modified inplace
487 * if both the expression and its local space have only a single reference.
488 * The caller is not allowed to modify "aff" between this call and
489 * a subsequent call to isl_aff_restore_domain_local_space.
490 * The only exception is that isl_aff_free can be called instead.
492 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
493 __isl_keep isl_aff
*aff
)
500 return isl_aff_get_domain_local_space(aff
);
506 /* Set the local space of the domain of "aff" to "ls",
507 * where the local space of "aff" may be missing
508 * due to a preceding call to isl_aff_take_domain_local_space.
509 * However, in this case, "aff" only has a single reference and
510 * then the call to isl_aff_cow has no effect.
512 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
513 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
519 isl_local_space_free(ls
);
523 aff
= isl_aff_cow(aff
);
526 isl_local_space_free(aff
->ls
);
532 isl_local_space_free(ls
);
536 /* Externally, an isl_aff has a map space, but internally, the
537 * ls field corresponds to the domain of that space.
539 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
540 enum isl_dim_type type
, unsigned pos
)
544 if (type
== isl_dim_out
)
546 if (type
== isl_dim_in
)
548 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
551 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
552 __isl_take isl_space
*space
)
554 aff
= isl_aff_cow(aff
);
558 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
560 return isl_aff_free(aff
);
565 isl_space_free(space
);
569 /* Reset the space of "aff". This function is called from isl_pw_templ.c
570 * and doesn't know if the space of an element object is represented
571 * directly or through its domain. It therefore passes along both.
573 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
574 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
576 isl_space_free(space
);
577 return isl_aff_reset_domain_space(aff
, domain
);
580 /* Reorder the coefficients of the affine expression based
581 * on the given reordering.
582 * The reordering r is assumed to have been extended with the local
585 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
586 __isl_take isl_reordering
*r
, int n_div
)
596 space
= isl_reordering_peek_space(r
);
597 dim
= isl_space_dim(space
, isl_dim_all
);
600 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
603 isl_seq_cpy(res
->el
, vec
->el
, 2);
604 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
605 for (i
= 0; i
< r
->len
; ++i
)
606 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
608 isl_reordering_free(r
);
613 isl_reordering_free(r
);
617 /* Reorder the dimensions of the domain of "aff" according
618 * to the given reordering.
620 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
621 __isl_take isl_reordering
*r
)
623 aff
= isl_aff_cow(aff
);
627 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
628 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
629 aff
->ls
->div
->n_row
);
630 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
632 if (!aff
->v
|| !aff
->ls
)
633 return isl_aff_free(aff
);
638 isl_reordering_free(r
);
642 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
643 __isl_take isl_space
*model
)
645 isl_bool equal_params
;
650 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
651 if (equal_params
< 0)
656 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
657 exp
= isl_reordering_extend_space(exp
,
658 isl_aff_get_domain_space(aff
));
659 aff
= isl_aff_realign_domain(aff
, exp
);
662 isl_space_free(model
);
665 isl_space_free(model
);
672 #include "isl_unbind_params_templ.c"
674 /* Is "aff" obviously equal to zero?
676 * If the denominator is zero, then "aff" is not equal to zero.
678 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
683 return isl_bool_error
;
685 if (isl_int_is_zero(aff
->v
->el
[0]))
686 return isl_bool_false
;
687 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
688 return isl_bool_ok(pos
< 0);
691 /* Does "aff" represent NaN?
693 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
696 return isl_bool_error
;
698 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
701 /* Are "aff1" and "aff2" obviously equal?
703 * NaN is not equal to anything, not even to another NaN.
705 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
706 __isl_keep isl_aff
*aff2
)
711 return isl_bool_error
;
713 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
714 return isl_bool_false
;
716 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
717 if (equal
< 0 || !equal
)
720 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
723 /* Return the common denominator of "aff" in "v".
725 * We cannot return anything meaningful in case of a NaN.
727 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
730 return isl_stat_error
;
731 if (isl_aff_is_nan(aff
))
732 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
733 "cannot get denominator of NaN", return isl_stat_error
);
734 isl_int_set(*v
, aff
->v
->el
[0]);
738 /* Return the common denominator of "aff".
740 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
747 ctx
= isl_aff_get_ctx(aff
);
748 if (isl_aff_is_nan(aff
))
749 return isl_val_nan(ctx
);
750 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
753 /* Return the constant term of "aff".
755 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
763 ctx
= isl_aff_get_ctx(aff
);
764 if (isl_aff_is_nan(aff
))
765 return isl_val_nan(ctx
);
766 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
767 return isl_val_normalize(v
);
770 /* Return the coefficient of the variable of type "type" at position "pos"
773 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
774 enum isl_dim_type type
, int pos
)
782 ctx
= isl_aff_get_ctx(aff
);
783 if (type
== isl_dim_out
)
784 isl_die(ctx
, isl_error_invalid
,
785 "output/set dimension does not have a coefficient",
787 if (type
== isl_dim_in
)
790 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
793 if (isl_aff_is_nan(aff
))
794 return isl_val_nan(ctx
);
795 pos
+= isl_local_space_offset(aff
->ls
, type
);
796 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
797 return isl_val_normalize(v
);
800 /* Return the sign of the coefficient of the variable of type "type"
801 * at position "pos" of "aff".
803 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
811 ctx
= isl_aff_get_ctx(aff
);
812 if (type
== isl_dim_out
)
813 isl_die(ctx
, isl_error_invalid
,
814 "output/set dimension does not have a coefficient",
816 if (type
== isl_dim_in
)
819 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
822 pos
+= isl_local_space_offset(aff
->ls
, type
);
823 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
826 /* Replace the numerator of the constant term of "aff" by "v".
828 * A NaN is unaffected by this operation.
830 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
834 if (isl_aff_is_nan(aff
))
836 aff
= isl_aff_cow(aff
);
840 aff
->v
= isl_vec_cow(aff
->v
);
842 return isl_aff_free(aff
);
844 isl_int_set(aff
->v
->el
[1], v
);
849 /* Replace the constant term of "aff" by "v".
851 * A NaN is unaffected by this operation.
853 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
854 __isl_take isl_val
*v
)
859 if (isl_aff_is_nan(aff
)) {
864 if (!isl_val_is_rat(v
))
865 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
866 "expecting rational value", goto error
);
868 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
869 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
874 aff
= isl_aff_cow(aff
);
877 aff
->v
= isl_vec_cow(aff
->v
);
881 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
882 isl_int_set(aff
->v
->el
[1], v
->n
);
883 } else if (isl_int_is_one(v
->d
)) {
884 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
886 isl_seq_scale(aff
->v
->el
+ 1,
887 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
888 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
889 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
890 aff
->v
= isl_vec_normalize(aff
->v
);
903 /* Add "v" to the constant term of "aff".
905 * A NaN is unaffected by this operation.
907 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
909 if (isl_int_is_zero(v
))
914 if (isl_aff_is_nan(aff
))
916 aff
= isl_aff_cow(aff
);
920 aff
->v
= isl_vec_cow(aff
->v
);
922 return isl_aff_free(aff
);
924 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
929 /* Add "v" to the constant term of "aff",
930 * in case "aff" is a rational expression.
932 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
933 __isl_take isl_val
*v
)
935 aff
= isl_aff_cow(aff
);
939 aff
->v
= isl_vec_cow(aff
->v
);
943 if (isl_int_is_one(v
->d
)) {
944 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
945 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
946 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
947 aff
->v
= isl_vec_normalize(aff
->v
);
951 isl_seq_scale(aff
->v
->el
+ 1,
952 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
953 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
954 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
955 aff
->v
= isl_vec_normalize(aff
->v
);
968 /* Return the first argument and free the second.
970 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
971 __isl_take isl_val
*v
)
977 /* Replace the first argument by NaN and free the second argument.
979 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
980 __isl_take isl_val
*v
)
983 return isl_aff_set_nan(aff
);
986 /* Add "v" to the constant term of "aff".
988 * A NaN is unaffected by this operation.
989 * Conversely, adding a NaN turns "aff" into a NaN.
991 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
992 __isl_take isl_val
*v
)
994 isl_bool is_nan
, is_zero
, is_rat
;
996 is_nan
= isl_aff_is_nan(aff
);
997 is_zero
= isl_val_is_zero(v
);
998 if (is_nan
< 0 || is_zero
< 0)
1000 if (is_nan
|| is_zero
)
1001 return pick_free(aff
, v
);
1003 is_nan
= isl_val_is_nan(v
);
1004 is_rat
= isl_val_is_rat(v
);
1005 if (is_nan
< 0 || is_rat
< 0)
1008 return set_nan_free_val(aff
, v
);
1010 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1011 "expecting rational value or NaN", goto error
);
1013 return isl_aff_add_rat_constant_val(aff
, v
);
1020 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1025 isl_int_set_si(t
, v
);
1026 aff
= isl_aff_add_constant(aff
, t
);
1032 /* Add "v" to the numerator of the constant term of "aff".
1034 * A NaN is unaffected by this operation.
1036 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1038 if (isl_int_is_zero(v
))
1043 if (isl_aff_is_nan(aff
))
1045 aff
= isl_aff_cow(aff
);
1049 aff
->v
= isl_vec_cow(aff
->v
);
1051 return isl_aff_free(aff
);
1053 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1058 /* Add "v" to the numerator of the constant term of "aff".
1060 * A NaN is unaffected by this operation.
1062 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1070 isl_int_set_si(t
, v
);
1071 aff
= isl_aff_add_constant_num(aff
, t
);
1077 /* Replace the numerator of the constant term of "aff" by "v".
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1085 if (isl_aff_is_nan(aff
))
1087 aff
= isl_aff_cow(aff
);
1091 aff
->v
= isl_vec_cow(aff
->v
);
1093 return isl_aff_free(aff
);
1095 isl_int_set_si(aff
->v
->el
[1], v
);
1100 /* Replace the numerator of the coefficient of the variable of type "type"
1101 * at position "pos" of "aff" by "v".
1103 * A NaN is unaffected by this operation.
1105 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1106 enum isl_dim_type type
, int pos
, isl_int v
)
1111 if (type
== isl_dim_out
)
1112 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1113 "output/set dimension does not have a coefficient",
1114 return isl_aff_free(aff
));
1115 if (type
== isl_dim_in
)
1118 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1119 return isl_aff_free(aff
);
1121 if (isl_aff_is_nan(aff
))
1123 aff
= isl_aff_cow(aff
);
1127 aff
->v
= isl_vec_cow(aff
->v
);
1129 return isl_aff_free(aff
);
1131 pos
+= isl_local_space_offset(aff
->ls
, type
);
1132 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1137 /* Replace the numerator of the coefficient of the variable of type "type"
1138 * at position "pos" of "aff" by "v".
1140 * A NaN is unaffected by this operation.
1142 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1143 enum isl_dim_type type
, int pos
, int v
)
1148 if (type
== isl_dim_out
)
1149 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1150 "output/set dimension does not have a coefficient",
1151 return isl_aff_free(aff
));
1152 if (type
== isl_dim_in
)
1155 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1156 return isl_aff_free(aff
);
1158 if (isl_aff_is_nan(aff
))
1160 pos
+= isl_local_space_offset(aff
->ls
, type
);
1161 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1164 aff
= isl_aff_cow(aff
);
1168 aff
->v
= isl_vec_cow(aff
->v
);
1170 return isl_aff_free(aff
);
1172 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1177 /* Replace the coefficient of the variable of type "type" at position "pos"
1180 * A NaN is unaffected by this operation.
1182 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1183 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1188 if (type
== isl_dim_out
)
1189 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1190 "output/set dimension does not have a coefficient",
1192 if (type
== isl_dim_in
)
1195 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1196 return isl_aff_free(aff
);
1198 if (isl_aff_is_nan(aff
)) {
1202 if (!isl_val_is_rat(v
))
1203 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1204 "expecting rational value", goto error
);
1206 pos
+= isl_local_space_offset(aff
->ls
, type
);
1207 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1208 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1213 aff
= isl_aff_cow(aff
);
1216 aff
->v
= isl_vec_cow(aff
->v
);
1220 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1221 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1222 } else if (isl_int_is_one(v
->d
)) {
1223 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1225 isl_seq_scale(aff
->v
->el
+ 1,
1226 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1227 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1228 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1229 aff
->v
= isl_vec_normalize(aff
->v
);
1242 /* Add "v" to the coefficient of the variable of type "type"
1243 * at position "pos" of "aff".
1245 * A NaN is unaffected by this operation.
1247 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1248 enum isl_dim_type type
, int pos
, isl_int v
)
1253 if (type
== isl_dim_out
)
1254 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1255 "output/set dimension does not have a coefficient",
1256 return isl_aff_free(aff
));
1257 if (type
== isl_dim_in
)
1260 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1261 return isl_aff_free(aff
);
1263 if (isl_aff_is_nan(aff
))
1265 aff
= isl_aff_cow(aff
);
1269 aff
->v
= isl_vec_cow(aff
->v
);
1271 return isl_aff_free(aff
);
1273 pos
+= isl_local_space_offset(aff
->ls
, type
);
1274 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1279 /* Add "v" to the coefficient of the variable of type "type"
1280 * at position "pos" of "aff".
1282 * A NaN is unaffected by this operation.
1284 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1285 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1290 if (isl_val_is_zero(v
)) {
1295 if (type
== isl_dim_out
)
1296 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1297 "output/set dimension does not have a coefficient",
1299 if (type
== isl_dim_in
)
1302 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1305 if (isl_aff_is_nan(aff
)) {
1309 if (!isl_val_is_rat(v
))
1310 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1311 "expecting rational value", goto error
);
1313 aff
= isl_aff_cow(aff
);
1317 aff
->v
= isl_vec_cow(aff
->v
);
1321 pos
+= isl_local_space_offset(aff
->ls
, type
);
1322 if (isl_int_is_one(v
->d
)) {
1323 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1324 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1325 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1326 aff
->v
= isl_vec_normalize(aff
->v
);
1330 isl_seq_scale(aff
->v
->el
+ 1,
1331 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1332 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1333 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1334 aff
->v
= isl_vec_normalize(aff
->v
);
1347 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1348 enum isl_dim_type type
, int pos
, int v
)
1353 isl_int_set_si(t
, v
);
1354 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1360 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1365 return isl_local_space_get_div(aff
->ls
, pos
);
1368 /* Return the negation of "aff".
1370 * As a special case, -NaN = NaN.
1372 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1376 if (isl_aff_is_nan(aff
))
1378 aff
= isl_aff_cow(aff
);
1381 aff
->v
= isl_vec_cow(aff
->v
);
1383 return isl_aff_free(aff
);
1385 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1390 /* Remove divs from the local space that do not appear in the affine
1392 * We currently only remove divs at the end.
1393 * Some intermediate divs may also not appear directly in the affine
1394 * expression, but we would also need to check that no other divs are
1395 * defined in terms of them.
1397 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1403 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1404 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1405 if (n
< 0 || off
< 0)
1406 return isl_aff_free(aff
);
1408 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1412 aff
= isl_aff_cow(aff
);
1416 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1417 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1418 if (!aff
->ls
|| !aff
->v
)
1419 return isl_aff_free(aff
);
1424 /* Look for any divs in the aff->ls with a denominator equal to one
1425 * and plug them into the affine expression and any subsequent divs
1426 * that may reference the div.
1428 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1435 isl_local_space
*ls
;
1438 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1439 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1440 if (n
< 0 || off
< 0)
1441 return isl_aff_free(aff
);
1443 for (i
= 0; i
< n
; ++i
) {
1444 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1446 ls
= isl_local_space_copy(aff
->ls
);
1447 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1448 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1449 vec
= isl_vec_copy(aff
->v
);
1450 vec
= isl_vec_cow(vec
);
1456 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1461 isl_vec_free(aff
->v
);
1463 isl_local_space_free(aff
->ls
);
1470 isl_local_space_free(ls
);
1471 return isl_aff_free(aff
);
1474 /* Look for any divs j that appear with a unit coefficient inside
1475 * the definitions of other divs i and plug them into the definitions
1478 * In particular, an expression of the form
1480 * floor((f(..) + floor(g(..)/n))/m)
1484 * floor((n * f(..) + g(..))/(n * m))
1486 * This simplification is correct because we can move the expression
1487 * f(..) into the inner floor in the original expression to obtain
1489 * floor(floor((n * f(..) + g(..))/n)/m)
1491 * from which we can derive the simplified expression.
1493 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1499 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1500 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1501 if (n
< 0 || off
< 0)
1502 return isl_aff_free(aff
);
1503 for (i
= 1; i
< n
; ++i
) {
1504 for (j
= 0; j
< i
; ++j
) {
1505 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1507 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1508 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1509 aff
->v
->size
, i
, 1);
1511 return isl_aff_free(aff
);
1518 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1520 * Even though this function is only called on isl_affs with a single
1521 * reference, we are careful to only change aff->v and aff->ls together.
1523 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1525 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1526 isl_local_space
*ls
;
1530 return isl_aff_free(aff
);
1532 ls
= isl_local_space_copy(aff
->ls
);
1533 ls
= isl_local_space_swap_div(ls
, a
, b
);
1534 v
= isl_vec_copy(aff
->v
);
1539 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1540 isl_vec_free(aff
->v
);
1542 isl_local_space_free(aff
->ls
);
1548 isl_local_space_free(ls
);
1549 return isl_aff_free(aff
);
1552 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1554 * We currently do not actually remove div "b", but simply add its
1555 * coefficient to that of "a" and then zero it out.
1557 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1559 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1562 return isl_aff_free(aff
);
1564 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1567 aff
->v
= isl_vec_cow(aff
->v
);
1569 return isl_aff_free(aff
);
1571 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1572 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1573 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1578 /* Sort the divs in the local space of "aff" according to
1579 * the comparison function "cmp_row" in isl_local_space.c,
1580 * combining the coefficients of identical divs.
1582 * Reordering divs does not change the semantics of "aff",
1583 * so there is no need to call isl_aff_cow.
1584 * Moreover, this function is currently only called on isl_affs
1585 * with a single reference.
1587 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1592 n
= isl_aff_dim(aff
, isl_dim_div
);
1594 return isl_aff_free(aff
);
1595 for (i
= 1; i
< n
; ++i
) {
1596 for (j
= i
- 1; j
>= 0; --j
) {
1597 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1601 aff
= merge_divs(aff
, j
, j
+ 1);
1603 aff
= swap_div(aff
, j
, j
+ 1);
1612 /* Normalize the representation of "aff".
1614 * This function should only be called on "new" isl_affs, i.e.,
1615 * with only a single reference. We therefore do not need to
1616 * worry about affecting other instances.
1618 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1622 aff
->v
= isl_vec_normalize(aff
->v
);
1624 return isl_aff_free(aff
);
1625 aff
= plug_in_integral_divs(aff
);
1626 aff
= plug_in_unit_divs(aff
);
1627 aff
= sort_divs(aff
);
1628 aff
= isl_aff_remove_unused_divs(aff
);
1632 /* Given f, return floor(f).
1633 * If f is an integer expression, then just return f.
1634 * If f is a constant, then return the constant floor(f).
1635 * Otherwise, if f = g/m, write g = q m + r,
1636 * create a new div d = [r/m] and return the expression q + d.
1637 * The coefficients in r are taken to lie between -m/2 and m/2.
1639 * reduce_div_coefficients performs the same normalization.
1641 * As a special case, floor(NaN) = NaN.
1643 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1653 if (isl_aff_is_nan(aff
))
1655 if (isl_int_is_one(aff
->v
->el
[0]))
1658 aff
= isl_aff_cow(aff
);
1662 aff
->v
= isl_vec_cow(aff
->v
);
1664 return isl_aff_free(aff
);
1666 if (isl_aff_is_cst(aff
)) {
1667 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1668 isl_int_set_si(aff
->v
->el
[0], 1);
1672 div
= isl_vec_copy(aff
->v
);
1673 div
= isl_vec_cow(div
);
1675 return isl_aff_free(aff
);
1677 ctx
= isl_aff_get_ctx(aff
);
1678 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1679 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1680 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1681 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1682 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1683 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1684 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1688 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1690 return isl_aff_free(aff
);
1692 size
= aff
->v
->size
;
1693 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1695 return isl_aff_free(aff
);
1696 isl_int_set_si(aff
->v
->el
[0], 1);
1697 isl_int_set_si(aff
->v
->el
[size
], 1);
1699 aff
= isl_aff_normalize(aff
);
1706 * aff mod m = aff - m * floor(aff/m)
1708 * with m an integer value.
1710 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1711 __isl_take isl_val
*m
)
1718 if (!isl_val_is_int(m
))
1719 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1720 "expecting integer modulo", goto error
);
1722 res
= isl_aff_copy(aff
);
1723 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1724 aff
= isl_aff_floor(aff
);
1725 aff
= isl_aff_scale_val(aff
, m
);
1726 res
= isl_aff_sub(res
, aff
);
1737 * pwaff mod m = pwaff - m * floor(pwaff/m)
1739 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1743 res
= isl_pw_aff_copy(pwaff
);
1744 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1745 pwaff
= isl_pw_aff_floor(pwaff
);
1746 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1747 res
= isl_pw_aff_sub(res
, pwaff
);
1754 * pa mod m = pa - m * floor(pa/m)
1756 * with m an integer value.
1758 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1759 __isl_take isl_val
*m
)
1763 if (!isl_val_is_int(m
))
1764 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1765 "expecting integer modulo", goto error
);
1766 pa
= isl_pw_aff_mod(pa
, m
->n
);
1770 isl_pw_aff_free(pa
);
1775 /* Given f, return ceil(f).
1776 * If f is an integer expression, then just return f.
1777 * Otherwise, let f be the expression
1783 * floor((e + m - 1)/m)
1785 * As a special case, ceil(NaN) = NaN.
1787 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1792 if (isl_aff_is_nan(aff
))
1794 if (isl_int_is_one(aff
->v
->el
[0]))
1797 aff
= isl_aff_cow(aff
);
1800 aff
->v
= isl_vec_cow(aff
->v
);
1802 return isl_aff_free(aff
);
1804 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1805 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1806 aff
= isl_aff_floor(aff
);
1811 /* Apply the expansion computed by isl_merge_divs.
1812 * The expansion itself is given by "exp" while the resulting
1813 * list of divs is given by "div".
1815 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1816 __isl_take isl_mat
*div
, int *exp
)
1822 aff
= isl_aff_cow(aff
);
1824 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1825 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1826 new_n_div
= isl_mat_rows(div
);
1827 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1830 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1831 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1832 if (!aff
->v
|| !aff
->ls
)
1833 return isl_aff_free(aff
);
1841 /* Add two affine expressions that live in the same local space.
1843 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1844 __isl_take isl_aff
*aff2
)
1848 aff1
= isl_aff_cow(aff1
);
1852 aff1
->v
= isl_vec_cow(aff1
->v
);
1858 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1859 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1860 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1861 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1862 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1863 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1864 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1869 aff1
= isl_aff_normalize(aff1
);
1877 /* Replace one of the arguments by a NaN and free the other one.
1879 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1880 __isl_take isl_aff
*aff2
)
1883 return isl_aff_set_nan(aff1
);
1886 /* Return the sum of "aff1" and "aff2".
1888 * If either of the two is NaN, then the result is NaN.
1890 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1891 __isl_take isl_aff
*aff2
)
1897 isl_size n_div1
, n_div2
;
1902 ctx
= isl_aff_get_ctx(aff1
);
1903 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1904 isl_die(ctx
, isl_error_invalid
,
1905 "spaces don't match", goto error
);
1907 if (isl_aff_is_nan(aff1
)) {
1911 if (isl_aff_is_nan(aff2
)) {
1916 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1917 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1918 if (n_div1
< 0 || n_div2
< 0)
1920 if (n_div1
== 0 && n_div2
== 0)
1921 return add_expanded(aff1
, aff2
);
1923 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1924 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1925 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1928 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1929 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1930 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1934 return add_expanded(aff1
, aff2
);
1943 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1944 __isl_take isl_aff
*aff2
)
1946 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1949 /* Return the result of scaling "aff" by a factor of "f".
1951 * As a special case, f * NaN = NaN.
1953 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1959 if (isl_aff_is_nan(aff
))
1962 if (isl_int_is_one(f
))
1965 aff
= isl_aff_cow(aff
);
1968 aff
->v
= isl_vec_cow(aff
->v
);
1970 return isl_aff_free(aff
);
1972 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1973 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1978 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1979 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1980 isl_int_divexact(gcd
, f
, gcd
);
1981 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1987 /* Multiple "aff" by "v".
1989 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1990 __isl_take isl_val
*v
)
1995 if (isl_val_is_one(v
)) {
2000 if (!isl_val_is_rat(v
))
2001 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2002 "expecting rational factor", goto error
);
2004 aff
= isl_aff_scale(aff
, v
->n
);
2005 aff
= isl_aff_scale_down(aff
, v
->d
);
2015 /* Return the result of scaling "aff" down by a factor of "f".
2017 * As a special case, NaN/f = NaN.
2019 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2025 if (isl_aff_is_nan(aff
))
2028 if (isl_int_is_one(f
))
2031 aff
= isl_aff_cow(aff
);
2035 if (isl_int_is_zero(f
))
2036 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2037 "cannot scale down by zero", return isl_aff_free(aff
));
2039 aff
->v
= isl_vec_cow(aff
->v
);
2041 return isl_aff_free(aff
);
2044 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2045 isl_int_gcd(gcd
, gcd
, f
);
2046 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2047 isl_int_divexact(gcd
, f
, gcd
);
2048 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2054 /* Divide "aff" by "v".
2056 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2057 __isl_take isl_val
*v
)
2062 if (isl_val_is_one(v
)) {
2067 if (!isl_val_is_rat(v
))
2068 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2069 "expecting rational factor", goto error
);
2070 if (!isl_val_is_pos(v
))
2071 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2072 "factor needs to be positive", goto error
);
2074 aff
= isl_aff_scale(aff
, v
->d
);
2075 aff
= isl_aff_scale_down(aff
, v
->n
);
2085 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2093 isl_int_set_ui(v
, f
);
2094 aff
= isl_aff_scale_down(aff
, v
);
2100 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2101 enum isl_dim_type type
, unsigned pos
, const char *s
)
2103 aff
= isl_aff_cow(aff
);
2106 if (type
== isl_dim_out
)
2107 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2108 "cannot set name of output/set dimension",
2109 return isl_aff_free(aff
));
2110 if (type
== isl_dim_in
)
2112 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2114 return isl_aff_free(aff
);
2119 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2120 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2122 aff
= isl_aff_cow(aff
);
2125 if (type
== isl_dim_out
)
2126 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2127 "cannot set name of output/set dimension",
2129 if (type
== isl_dim_in
)
2131 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2133 return isl_aff_free(aff
);
2142 /* Replace the identifier of the input tuple of "aff" by "id".
2143 * type is currently required to be equal to isl_dim_in
2145 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2146 enum isl_dim_type type
, __isl_take isl_id
*id
)
2148 aff
= isl_aff_cow(aff
);
2151 if (type
!= isl_dim_in
)
2152 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2153 "cannot only set id of input tuple", goto error
);
2154 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2156 return isl_aff_free(aff
);
2165 /* Exploit the equalities in "eq" to simplify the affine expression
2166 * and the expressions of the integer divisions in the local space.
2167 * The integer divisions in this local space are assumed to appear
2168 * as regular dimensions in "eq".
2170 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2171 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2179 if (eq
->n_eq
== 0) {
2180 isl_basic_set_free(eq
);
2184 aff
= isl_aff_cow(aff
);
2188 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2189 isl_basic_set_copy(eq
));
2190 aff
->v
= isl_vec_cow(aff
->v
);
2191 if (!aff
->ls
|| !aff
->v
)
2194 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2196 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2197 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2198 if (j
< 0 || j
== 0 || j
>= o_div
)
2201 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2205 isl_basic_set_free(eq
);
2206 aff
= isl_aff_normalize(aff
);
2209 isl_basic_set_free(eq
);
2214 /* Exploit the equalities in "eq" to simplify the affine expression
2215 * and the expressions of the integer divisions in the local space.
2217 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2218 __isl_take isl_basic_set
*eq
)
2222 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2226 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2227 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2229 isl_basic_set_free(eq
);
2234 /* Look for equalities among the variables shared by context and aff
2235 * and the integer divisions of aff, if any.
2236 * The equalities are then used to eliminate coefficients and/or integer
2237 * divisions from aff.
2239 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2240 __isl_take isl_set
*context
)
2242 isl_local_space
*ls
;
2243 isl_basic_set
*hull
;
2245 ls
= isl_aff_get_domain_local_space(aff
);
2246 context
= isl_local_space_lift_set(ls
, context
);
2248 hull
= isl_set_affine_hull(context
);
2249 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2252 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2253 __isl_take isl_set
*context
)
2255 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2256 dom_context
= isl_set_intersect_params(dom_context
, context
);
2257 return isl_aff_gist(aff
, dom_context
);
2260 /* Return a basic set containing those elements in the space
2261 * of aff where it is positive. "rational" should not be set.
2263 * If "aff" is NaN, then it is not positive.
2265 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2266 int rational
, void *user
)
2268 isl_constraint
*ineq
;
2269 isl_basic_set
*bset
;
2274 if (isl_aff_is_nan(aff
)) {
2275 isl_space
*space
= isl_aff_get_domain_space(aff
);
2277 return isl_basic_set_empty(space
);
2280 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2281 "rational sets not supported", goto error
);
2283 ineq
= isl_inequality_from_aff(aff
);
2284 c
= isl_constraint_get_constant_val(ineq
);
2285 c
= isl_val_sub_ui(c
, 1);
2286 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2288 bset
= isl_basic_set_from_constraint(ineq
);
2289 bset
= isl_basic_set_simplify(bset
);
2296 /* Return a basic set containing those elements in the space
2297 * of aff where it is non-negative.
2298 * If "rational" is set, then return a rational basic set.
2300 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2302 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2303 __isl_take isl_aff
*aff
, int rational
, void *user
)
2305 isl_constraint
*ineq
;
2306 isl_basic_set
*bset
;
2310 if (isl_aff_is_nan(aff
)) {
2311 isl_space
*space
= isl_aff_get_domain_space(aff
);
2313 return isl_basic_set_empty(space
);
2316 ineq
= isl_inequality_from_aff(aff
);
2318 bset
= isl_basic_set_from_constraint(ineq
);
2320 bset
= isl_basic_set_set_rational(bset
);
2321 bset
= isl_basic_set_simplify(bset
);
2325 /* Return a basic set containing those elements in the space
2326 * of aff where it is non-negative.
2328 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2330 return aff_nonneg_basic_set(aff
, 0, NULL
);
2333 /* Return a basic set containing those elements in the domain space
2334 * of "aff" where it is positive.
2336 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2338 aff
= isl_aff_add_constant_num_si(aff
, -1);
2339 return isl_aff_nonneg_basic_set(aff
);
2342 /* Return a basic set containing those elements in the domain space
2343 * of aff where it is negative.
2345 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2347 aff
= isl_aff_neg(aff
);
2348 return isl_aff_pos_basic_set(aff
);
2351 /* Return a basic set containing those elements in the space
2352 * of aff where it is zero.
2353 * If "rational" is set, then return a rational basic set.
2355 * If "aff" is NaN, then it is not zero.
2357 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2358 int rational
, void *user
)
2360 isl_constraint
*ineq
;
2361 isl_basic_set
*bset
;
2365 if (isl_aff_is_nan(aff
)) {
2366 isl_space
*space
= isl_aff_get_domain_space(aff
);
2368 return isl_basic_set_empty(space
);
2371 ineq
= isl_equality_from_aff(aff
);
2373 bset
= isl_basic_set_from_constraint(ineq
);
2375 bset
= isl_basic_set_set_rational(bset
);
2376 bset
= isl_basic_set_simplify(bset
);
2380 /* Return a basic set containing those elements in the space
2381 * of aff where it is zero.
2383 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2385 return aff_zero_basic_set(aff
, 0, NULL
);
2388 /* Return a basic set containing those elements in the shared space
2389 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2391 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2392 __isl_take isl_aff
*aff2
)
2394 aff1
= isl_aff_sub(aff1
, aff2
);
2396 return isl_aff_nonneg_basic_set(aff1
);
2399 /* Return a basic set containing those elements in the shared domain space
2400 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2402 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2403 __isl_take isl_aff
*aff2
)
2405 aff1
= isl_aff_sub(aff1
, aff2
);
2407 return isl_aff_pos_basic_set(aff1
);
2410 /* Return a set containing those elements in the shared space
2411 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2413 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2414 __isl_take isl_aff
*aff2
)
2416 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2419 /* Return a set containing those elements in the shared domain space
2420 * of aff1 and aff2 where aff1 is greater than aff2.
2422 * If either of the two inputs is NaN, then the result is empty,
2423 * as comparisons with NaN always return false.
2425 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2426 __isl_take isl_aff
*aff2
)
2428 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2431 /* Return a basic set containing those elements in the shared space
2432 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2434 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2435 __isl_take isl_aff
*aff2
)
2437 return isl_aff_ge_basic_set(aff2
, aff1
);
2440 /* Return a basic set containing those elements in the shared domain space
2441 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2443 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2444 __isl_take isl_aff
*aff2
)
2446 return isl_aff_gt_basic_set(aff2
, aff1
);
2449 /* Return a set containing those elements in the shared space
2450 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2452 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2453 __isl_take isl_aff
*aff2
)
2455 return isl_aff_ge_set(aff2
, aff1
);
2458 /* Return a set containing those elements in the shared domain space
2459 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2461 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2462 __isl_take isl_aff
*aff2
)
2464 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2467 /* Return a basic set containing those elements in the shared space
2468 * of aff1 and aff2 where aff1 and aff2 are equal.
2470 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2471 __isl_take isl_aff
*aff2
)
2473 aff1
= isl_aff_sub(aff1
, aff2
);
2475 return isl_aff_zero_basic_set(aff1
);
2478 /* Return a set containing those elements in the shared space
2479 * of aff1 and aff2 where aff1 and aff2 are equal.
2481 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2482 __isl_take isl_aff
*aff2
)
2484 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2487 /* Return a set containing those elements in the shared domain space
2488 * of aff1 and aff2 where aff1 and aff2 are not equal.
2490 * If either of the two inputs is NaN, then the result is empty,
2491 * as comparisons with NaN always return false.
2493 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2494 __isl_take isl_aff
*aff2
)
2496 isl_set
*set_lt
, *set_gt
;
2498 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2499 isl_aff_copy(aff2
));
2500 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2501 return isl_set_union_disjoint(set_lt
, set_gt
);
2504 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2505 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2507 aff1
= isl_aff_add(aff1
, aff2
);
2508 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2512 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2515 return isl_bool_error
;
2517 return isl_bool_false
;
2521 #define TYPE isl_aff
2523 #include "check_type_range_templ.c"
2525 /* Check whether the given affine expression has non-zero coefficient
2526 * for any dimension in the given range or if any of these dimensions
2527 * appear with non-zero coefficients in any of the integer divisions
2528 * involved in the affine expression.
2530 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2531 enum isl_dim_type type
, unsigned first
, unsigned n
)
2535 isl_bool involves
= isl_bool_false
;
2538 return isl_bool_error
;
2540 return isl_bool_false
;
2541 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2542 return isl_bool_error
;
2544 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2548 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2549 for (i
= 0; i
< n
; ++i
)
2550 if (active
[first
+ i
]) {
2551 involves
= isl_bool_true
;
2560 return isl_bool_error
;
2563 /* Does "aff" involve any local variables, i.e., integer divisions?
2565 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2569 n
= isl_aff_dim(aff
, isl_dim_div
);
2571 return isl_bool_error
;
2572 return isl_bool_ok(n
> 0);
2575 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2576 enum isl_dim_type type
, unsigned first
, unsigned n
)
2582 if (type
== isl_dim_out
)
2583 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2584 "cannot drop output/set dimension",
2585 return isl_aff_free(aff
));
2586 if (type
== isl_dim_in
)
2588 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2591 ctx
= isl_aff_get_ctx(aff
);
2592 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2593 return isl_aff_free(aff
);
2595 aff
= isl_aff_cow(aff
);
2599 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2601 return isl_aff_free(aff
);
2603 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2604 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2606 return isl_aff_free(aff
);
2611 /* Is the domain of "aff" a product?
2613 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2615 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2619 #define TYPE isl_aff
2620 #include <isl_domain_factor_templ.c>
2622 /* Project the domain of the affine expression onto its parameter space.
2623 * The affine expression may not involve any of the domain dimensions.
2625 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2630 n
= isl_aff_dim(aff
, isl_dim_in
);
2632 return isl_aff_free(aff
);
2633 aff
= isl_aff_drop_domain(aff
, 0, n
);
2634 space
= isl_aff_get_domain_space(aff
);
2635 space
= isl_space_params(space
);
2636 aff
= isl_aff_reset_domain_space(aff
, space
);
2640 /* Convert an affine expression defined over a parameter domain
2641 * into one that is defined over a zero-dimensional set.
2643 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2645 isl_local_space
*ls
;
2647 ls
= isl_aff_take_domain_local_space(aff
);
2648 ls
= isl_local_space_set_from_params(ls
);
2649 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2654 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2655 enum isl_dim_type type
, unsigned first
, unsigned n
)
2661 if (type
== isl_dim_out
)
2662 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2663 "cannot insert output/set dimensions",
2664 return isl_aff_free(aff
));
2665 if (type
== isl_dim_in
)
2667 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2670 ctx
= isl_aff_get_ctx(aff
);
2671 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2672 return isl_aff_free(aff
);
2674 aff
= isl_aff_cow(aff
);
2678 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2680 return isl_aff_free(aff
);
2682 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2683 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2685 return isl_aff_free(aff
);
2690 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2691 enum isl_dim_type type
, unsigned n
)
2695 pos
= isl_aff_dim(aff
, type
);
2697 return isl_aff_free(aff
);
2699 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2702 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2703 * to dimensions of "dst_type" at "dst_pos".
2705 * We only support moving input dimensions to parameters and vice versa.
2707 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2708 enum isl_dim_type dst_type
, unsigned dst_pos
,
2709 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2713 isl_size src_off
, dst_off
;
2718 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2719 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2722 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2723 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2724 "cannot move output/set dimension",
2725 return isl_aff_free(aff
));
2726 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2727 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2728 "cannot move divs", return isl_aff_free(aff
));
2729 if (dst_type
== isl_dim_in
)
2730 dst_type
= isl_dim_set
;
2731 if (src_type
== isl_dim_in
)
2732 src_type
= isl_dim_set
;
2734 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2735 return isl_aff_free(aff
);
2736 if (dst_type
== src_type
)
2737 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2738 "moving dims within the same type not supported",
2739 return isl_aff_free(aff
));
2741 aff
= isl_aff_cow(aff
);
2742 src_off
= isl_aff_domain_offset(aff
, src_type
);
2743 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2744 if (src_off
< 0 || dst_off
< 0)
2745 return isl_aff_free(aff
);
2747 g_src_pos
= 1 + src_off
+ src_pos
;
2748 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2749 if (dst_type
> src_type
)
2752 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2753 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2754 src_type
, src_pos
, n
);
2755 if (!aff
->v
|| !aff
->ls
)
2756 return isl_aff_free(aff
);
2758 aff
= sort_divs(aff
);
2763 /* Return a zero isl_aff in the given space.
2765 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2766 * interface over all piecewise types.
2768 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2770 isl_local_space
*ls
;
2772 ls
= isl_local_space_from_space(isl_space_domain(space
));
2773 return isl_aff_zero_on_domain(ls
);
2776 #define isl_aff_involves_nan isl_aff_is_nan
2779 #define PW isl_pw_aff
2783 #define EL_IS_ZERO is_empty
2787 #define IS_ZERO is_empty
2790 #undef DEFAULT_IS_ZERO
2791 #define DEFAULT_IS_ZERO 0
2793 #include <isl_pw_templ.c>
2794 #include <isl_pw_add_constant_val_templ.c>
2795 #include <isl_pw_bind_domain_templ.c>
2796 #include <isl_pw_eval.c>
2797 #include <isl_pw_hash.c>
2798 #include <isl_pw_insert_dims_templ.c>
2799 #include <isl_pw_insert_domain_templ.c>
2800 #include <isl_pw_move_dims_templ.c>
2801 #include <isl_pw_neg_templ.c>
2802 #include <isl_pw_pullback_templ.c>
2803 #include <isl_pw_sub_templ.c>
2804 #include <isl_pw_union_opt.c>
2809 #include <isl_union_single.c>
2810 #include <isl_union_neg.c>
2815 #include <isl_union_pw_templ.c>
2817 /* Compute a piecewise quasi-affine expression with a domain that
2818 * is the union of those of pwaff1 and pwaff2 and such that on each
2819 * cell, the quasi-affine expression is the maximum of those of pwaff1
2820 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2821 * cell, then the associated expression is the defined one.
2823 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2824 __isl_take isl_pw_aff
*pwaff2
)
2826 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2827 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2830 /* Compute a piecewise quasi-affine expression with a domain that
2831 * is the union of those of pwaff1 and pwaff2 and such that on each
2832 * cell, the quasi-affine expression is the minimum of those of pwaff1
2833 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2834 * cell, then the associated expression is the defined one.
2836 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2837 __isl_take isl_pw_aff
*pwaff2
)
2839 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2840 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2843 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2844 __isl_take isl_pw_aff
*pwaff2
, int max
)
2847 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2849 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2852 /* Is the domain of "pa" a product?
2854 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2856 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2860 #define TYPE isl_pw_aff
2861 #include <isl_domain_factor_templ.c>
2863 /* Return a set containing those elements in the domain
2864 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2865 * does not satisfy "fn" (if complement is 1).
2867 * The pieces with a NaN never belong to the result since
2868 * NaN does not satisfy any property.
2870 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2871 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2873 int complement
, void *user
)
2881 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2883 for (i
= 0; i
< pwaff
->n
; ++i
) {
2884 isl_basic_set
*bset
;
2885 isl_set
*set_i
, *locus
;
2888 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2891 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2892 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2893 locus
= isl_set_from_basic_set(bset
);
2894 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2896 set_i
= isl_set_subtract(set_i
, locus
);
2898 set_i
= isl_set_intersect(set_i
, locus
);
2899 set
= isl_set_union_disjoint(set
, set_i
);
2902 isl_pw_aff_free(pwaff
);
2907 /* Return a set containing those elements in the domain
2908 * of "pa" where it is positive.
2910 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2912 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2915 /* Return a set containing those elements in the domain
2916 * of pwaff where it is non-negative.
2918 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2920 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is zero.
2926 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2928 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is not zero.
2934 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2936 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2939 /* Bind the affine function "aff" to the parameter "id",
2940 * returning the elements in the domain where the affine expression
2941 * is equal to the parameter.
2943 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2944 __isl_take isl_id
*id
)
2949 space
= isl_aff_get_domain_space(aff
);
2950 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2952 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2953 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2955 return isl_aff_eq_basic_set(aff
, aff_id
);
2958 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2959 * "rational" should not be set.
2961 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2962 int rational
, void *user
)
2969 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2970 "rational binding not supported", goto error
);
2971 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2977 /* Bind the piecewise affine function "pa" to the parameter "id",
2978 * returning the elements in the domain where the expression
2979 * is equal to the parameter.
2981 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2982 __isl_take isl_id
*id
)
2986 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
2992 /* Return a set containing those elements in the shared domain
2993 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2995 * We compute the difference on the shared domain and then construct
2996 * the set of values where this difference is non-negative.
2997 * If strict is set, we first subtract 1 from the difference.
2998 * If equal is set, we only return the elements where pwaff1 and pwaff2
3001 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3002 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3004 isl_set
*set1
, *set2
;
3006 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3007 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3008 set1
= isl_set_intersect(set1
, set2
);
3009 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3010 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3011 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3014 isl_space
*space
= isl_set_get_space(set1
);
3016 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3017 aff
= isl_aff_add_constant_si(aff
, -1);
3018 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3023 return isl_pw_aff_zero_set(pwaff1
);
3024 return isl_pw_aff_nonneg_set(pwaff1
);
3027 /* Return a set containing those elements in the shared domain
3028 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3030 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3031 __isl_take isl_pw_aff
*pwaff2
)
3033 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3034 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3037 /* Return a set containing those elements in the shared domain
3038 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3040 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3041 __isl_take isl_pw_aff
*pwaff2
)
3043 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3044 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3047 /* Return a set containing those elements in the shared domain
3048 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3050 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3051 __isl_take isl_pw_aff
*pwaff2
)
3053 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3054 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3057 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3058 __isl_take isl_pw_aff
*pwaff2
)
3060 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3063 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3064 __isl_take isl_pw_aff
*pwaff2
)
3066 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function values are ordered in the same way as "order",
3071 * which returns a set in the shared domain of its two arguments.
3073 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3074 * We first pull back the two functions such that they are defined on
3075 * the domain [A -> B]. Then we apply "order", resulting in a set
3076 * in the space [A -> B]. Finally, we unwrap this set to obtain
3077 * a map in the space A -> B.
3079 static __isl_give isl_map
*isl_pw_aff_order_map(
3080 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3081 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3082 __isl_take isl_pw_aff
*pa2
))
3084 isl_space
*space1
, *space2
;
3088 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3089 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3090 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3091 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3092 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3093 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3094 ma
= isl_multi_aff_range_map(space1
);
3095 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3096 set
= order(pa1
, pa2
);
3098 return isl_set_unwrap(set
);
3101 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3102 * where the function values are equal.
3104 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3105 __isl_take isl_pw_aff
*pa2
)
3107 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3110 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3111 * where the function value of "pa1" is less than or equal to
3112 * the function value of "pa2".
3114 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3115 __isl_take isl_pw_aff
*pa2
)
3117 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3120 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3121 * where the function value of "pa1" is less than the function value of "pa2".
3123 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3124 __isl_take isl_pw_aff
*pa2
)
3126 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3129 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3130 * where the function value of "pa1" is greater than or equal to
3131 * the function value of "pa2".
3133 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3134 __isl_take isl_pw_aff
*pa2
)
3136 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3139 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3140 * where the function value of "pa1" is greater than the function value
3143 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3144 __isl_take isl_pw_aff
*pa2
)
3146 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3149 /* Return a set containing those elements in the shared domain
3150 * of the elements of list1 and list2 where each element in list1
3151 * has the relation specified by "fn" with each element in list2.
3153 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3154 __isl_take isl_pw_aff_list
*list2
,
3155 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3156 __isl_take isl_pw_aff
*pwaff2
))
3162 if (!list1
|| !list2
)
3165 ctx
= isl_pw_aff_list_get_ctx(list1
);
3166 if (list1
->n
< 1 || list2
->n
< 1)
3167 isl_die(ctx
, isl_error_invalid
,
3168 "list should contain at least one element", goto error
);
3170 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3171 for (i
= 0; i
< list1
->n
; ++i
)
3172 for (j
= 0; j
< list2
->n
; ++j
) {
3175 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3176 isl_pw_aff_copy(list2
->p
[j
]));
3177 set
= isl_set_intersect(set
, set_ij
);
3180 isl_pw_aff_list_free(list1
);
3181 isl_pw_aff_list_free(list2
);
3184 isl_pw_aff_list_free(list1
);
3185 isl_pw_aff_list_free(list2
);
3189 /* Return a set containing those elements in the shared domain
3190 * of the elements of list1 and list2 where each element in list1
3191 * is equal to each element in list2.
3193 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3194 __isl_take isl_pw_aff_list
*list2
)
3196 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3199 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3200 __isl_take isl_pw_aff_list
*list2
)
3202 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3205 /* Return a set containing those elements in the shared domain
3206 * of the elements of list1 and list2 where each element in list1
3207 * is less than or equal to each element in list2.
3209 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3210 __isl_take isl_pw_aff_list
*list2
)
3212 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3215 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3216 __isl_take isl_pw_aff_list
*list2
)
3218 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3221 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3222 __isl_take isl_pw_aff_list
*list2
)
3224 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3227 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3228 __isl_take isl_pw_aff_list
*list2
)
3230 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3234 /* Return a set containing those elements in the shared domain
3235 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3237 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3238 __isl_take isl_pw_aff
*pwaff2
)
3240 isl_set
*set_lt
, *set_gt
;
3242 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3243 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3244 isl_pw_aff_copy(pwaff2
));
3245 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3246 return isl_set_union_disjoint(set_lt
, set_gt
);
3249 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3254 if (isl_int_is_one(v
))
3256 if (!isl_int_is_pos(v
))
3257 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3258 "factor needs to be positive",
3259 return isl_pw_aff_free(pwaff
));
3260 pwaff
= isl_pw_aff_cow(pwaff
);
3266 for (i
= 0; i
< pwaff
->n
; ++i
) {
3267 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3268 if (!pwaff
->p
[i
].aff
)
3269 return isl_pw_aff_free(pwaff
);
3275 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3279 pwaff
= isl_pw_aff_cow(pwaff
);
3285 for (i
= 0; i
< pwaff
->n
; ++i
) {
3286 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3287 if (!pwaff
->p
[i
].aff
)
3288 return isl_pw_aff_free(pwaff
);
3294 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3298 pwaff
= isl_pw_aff_cow(pwaff
);
3304 for (i
= 0; i
< pwaff
->n
; ++i
) {
3305 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3306 if (!pwaff
->p
[i
].aff
)
3307 return isl_pw_aff_free(pwaff
);
3313 /* Assuming that "cond1" and "cond2" are disjoint,
3314 * return an affine expression that is equal to pwaff1 on cond1
3315 * and to pwaff2 on cond2.
3317 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3318 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3319 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3321 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3322 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3324 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3327 /* Return an affine expression that is equal to pwaff_true for elements
3328 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3330 * That is, return cond ? pwaff_true : pwaff_false;
3332 * If "cond" involves and NaN, then we conservatively return a NaN
3333 * on its entire domain. In principle, we could consider the pieces
3334 * where it is NaN separately from those where it is not.
3336 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3337 * then only use the domain of "cond" to restrict the domain.
3339 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3340 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3342 isl_set
*cond_true
, *cond_false
;
3347 if (isl_pw_aff_involves_nan(cond
)) {
3348 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3349 isl_local_space
*ls
= isl_local_space_from_space(space
);
3350 isl_pw_aff_free(cond
);
3351 isl_pw_aff_free(pwaff_true
);
3352 isl_pw_aff_free(pwaff_false
);
3353 return isl_pw_aff_nan_on_domain(ls
);
3356 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3357 isl_pw_aff_get_space(pwaff_false
));
3358 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3359 isl_pw_aff_get_space(pwaff_true
));
3360 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3366 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3367 isl_pw_aff_free(pwaff_false
);
3368 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3371 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3372 cond_false
= isl_pw_aff_zero_set(cond
);
3373 return isl_pw_aff_select(cond_true
, pwaff_true
,
3374 cond_false
, pwaff_false
);
3376 isl_pw_aff_free(cond
);
3377 isl_pw_aff_free(pwaff_true
);
3378 isl_pw_aff_free(pwaff_false
);
3382 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3387 return isl_bool_error
;
3389 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3390 return isl_bool_ok(pos
== -1);
3393 /* Check whether pwaff is a piecewise constant.
3395 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3400 return isl_bool_error
;
3402 for (i
= 0; i
< pwaff
->n
; ++i
) {
3403 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3404 if (is_cst
< 0 || !is_cst
)
3408 return isl_bool_true
;
3411 /* Return the product of "aff1" and "aff2".
3413 * If either of the two is NaN, then the result is NaN.
3415 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3417 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3418 __isl_take isl_aff
*aff2
)
3423 if (isl_aff_is_nan(aff1
)) {
3427 if (isl_aff_is_nan(aff2
)) {
3432 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3433 return isl_aff_mul(aff2
, aff1
);
3435 if (!isl_aff_is_cst(aff2
))
3436 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3437 "at least one affine expression should be constant",
3440 aff1
= isl_aff_cow(aff1
);
3444 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3445 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3455 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3457 * If either of the two is NaN, then the result is NaN.
3458 * A division by zero also results in NaN.
3460 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3461 __isl_take isl_aff
*aff2
)
3463 isl_bool is_cst
, is_zero
;
3469 if (isl_aff_is_nan(aff1
)) {
3473 if (isl_aff_is_nan(aff2
)) {
3478 is_cst
= isl_aff_is_cst(aff2
);
3482 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3483 "second argument should be a constant", goto error
);
3484 is_zero
= isl_aff_plain_is_zero(aff2
);
3488 return set_nan_free(aff1
, aff2
);
3490 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3492 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3493 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3496 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3497 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3500 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3501 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3512 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3513 __isl_take isl_pw_aff
*pwaff2
)
3515 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3516 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3519 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3520 __isl_take isl_pw_aff
*pwaff2
)
3522 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3525 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3526 __isl_take isl_pw_aff
*pwaff2
)
3528 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3529 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3532 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3534 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3535 __isl_take isl_pw_aff
*pa2
)
3539 is_cst
= isl_pw_aff_is_cst(pa2
);
3543 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3544 "second argument should be a piecewise constant",
3546 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3547 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3549 isl_pw_aff_free(pa1
);
3550 isl_pw_aff_free(pa2
);
3554 /* Compute the quotient of the integer division of "pa1" by "pa2"
3555 * with rounding towards zero.
3556 * "pa2" is assumed to be a piecewise constant.
3558 * In particular, return
3560 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3563 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3564 __isl_take isl_pw_aff
*pa2
)
3570 is_cst
= isl_pw_aff_is_cst(pa2
);
3574 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3575 "second argument should be a piecewise constant",
3578 pa1
= isl_pw_aff_div(pa1
, pa2
);
3580 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3581 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3582 c
= isl_pw_aff_ceil(pa1
);
3583 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3585 isl_pw_aff_free(pa1
);
3586 isl_pw_aff_free(pa2
);
3590 /* Compute the remainder of the integer division of "pa1" by "pa2"
3591 * with rounding towards zero.
3592 * "pa2" is assumed to be a piecewise constant.
3594 * In particular, return
3596 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3599 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3600 __isl_take isl_pw_aff
*pa2
)
3605 is_cst
= isl_pw_aff_is_cst(pa2
);
3609 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3610 "second argument should be a piecewise constant",
3612 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3613 res
= isl_pw_aff_mul(pa2
, res
);
3614 res
= isl_pw_aff_sub(pa1
, res
);
3617 isl_pw_aff_free(pa1
);
3618 isl_pw_aff_free(pa2
);
3622 /* Does either of "pa1" or "pa2" involve any NaN2?
3624 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3625 __isl_keep isl_pw_aff
*pa2
)
3629 has_nan
= isl_pw_aff_involves_nan(pa1
);
3630 if (has_nan
< 0 || has_nan
)
3632 return isl_pw_aff_involves_nan(pa2
);
3635 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3636 * by a NaN on their shared domain.
3638 * In principle, the result could be refined to only being NaN
3639 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3641 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3642 __isl_take isl_pw_aff
*pa2
)
3644 isl_local_space
*ls
;
3648 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3649 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3650 pa
= isl_pw_aff_nan_on_domain(ls
);
3651 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3656 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3657 __isl_take isl_pw_aff
*pwaff2
)
3662 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3663 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3664 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3665 isl_pw_aff_copy(pwaff2
));
3666 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3667 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3670 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3671 __isl_take isl_pw_aff
*pwaff2
)
3676 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3677 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3678 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3679 isl_pw_aff_copy(pwaff2
));
3680 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3681 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3684 /* Return an expression for the minimum (if "max" is not set) or
3685 * the maximum (if "max" is set) of "pa1" and "pa2".
3686 * If either expression involves any NaN, then return a NaN
3687 * on the shared domain as result.
3689 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3690 __isl_take isl_pw_aff
*pa2
, int max
)
3694 has_nan
= either_involves_nan(pa1
, pa2
);
3696 pa1
= isl_pw_aff_free(pa1
);
3698 return replace_by_nan(pa1
, pa2
);
3700 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3702 return pw_aff_max(pa1
, pa2
);
3704 return pw_aff_min(pa1
, pa2
);
3707 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3709 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3710 __isl_take isl_pw_aff
*pwaff2
)
3712 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3715 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3717 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3718 __isl_take isl_pw_aff
*pwaff2
)
3720 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3723 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3724 __isl_take isl_pw_aff_list
*list
,
3725 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3726 __isl_take isl_pw_aff
*pwaff2
))
3735 ctx
= isl_pw_aff_list_get_ctx(list
);
3737 isl_die(ctx
, isl_error_invalid
,
3738 "list should contain at least one element", goto error
);
3740 res
= isl_pw_aff_copy(list
->p
[0]);
3741 for (i
= 1; i
< list
->n
; ++i
)
3742 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3744 isl_pw_aff_list_free(list
);
3747 isl_pw_aff_list_free(list
);
3751 /* Return an isl_pw_aff that maps each element in the intersection of the
3752 * domains of the elements of list to the minimal corresponding affine
3755 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3757 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3760 /* Return an isl_pw_aff that maps each element in the intersection of the
3761 * domains of the elements of list to the maximal corresponding affine
3764 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3766 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3769 /* Mark the domains of "pwaff" as rational.
3771 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3775 pwaff
= isl_pw_aff_cow(pwaff
);
3781 for (i
= 0; i
< pwaff
->n
; ++i
) {
3782 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3783 if (!pwaff
->p
[i
].set
)
3784 return isl_pw_aff_free(pwaff
);
3790 /* Mark the domains of the elements of "list" as rational.
3792 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3793 __isl_take isl_pw_aff_list
*list
)
3803 for (i
= 0; i
< n
; ++i
) {
3806 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3807 pa
= isl_pw_aff_set_rational(pa
);
3808 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3814 /* Do the parameters of "aff" match those of "space"?
3816 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3817 __isl_keep isl_space
*space
)
3819 isl_space
*aff_space
;
3823 return isl_bool_error
;
3825 aff_space
= isl_aff_get_domain_space(aff
);
3827 match
= isl_space_has_equal_params(space
, aff_space
);
3829 isl_space_free(aff_space
);
3833 /* Check that the domain space of "aff" matches "space".
3835 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3836 __isl_keep isl_space
*space
)
3838 isl_space
*aff_space
;
3842 return isl_stat_error
;
3844 aff_space
= isl_aff_get_domain_space(aff
);
3846 match
= isl_space_has_equal_params(space
, aff_space
);
3850 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3851 "parameters don't match", goto error
);
3852 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3853 aff_space
, isl_dim_set
);
3857 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3858 "domains don't match", goto error
);
3859 isl_space_free(aff_space
);
3862 isl_space_free(aff_space
);
3863 return isl_stat_error
;
3866 /* Return the shared (universe) domain of the elements of "ma".
3868 * Since an isl_multi_aff (and an isl_aff) is always total,
3869 * the domain is always the universe set in its domain space.
3870 * This is a helper function for use in the generic isl_multi_*_bind.
3872 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3873 __isl_take isl_multi_aff
*ma
)
3877 space
= isl_multi_aff_get_space(ma
);
3878 isl_multi_aff_free(ma
);
3880 return isl_basic_set_universe(isl_space_domain(space
));
3886 #include <isl_multi_no_explicit_domain.c>
3887 #include <isl_multi_templ.c>
3888 #include <isl_multi_add_constant_templ.c>
3889 #include <isl_multi_apply_set.c>
3890 #include <isl_multi_arith_templ.c>
3891 #include <isl_multi_bind_domain_templ.c>
3892 #include <isl_multi_cmp.c>
3893 #include <isl_multi_dim_id_templ.c>
3894 #include <isl_multi_dims.c>
3895 #include <isl_multi_floor.c>
3896 #include <isl_multi_from_base_templ.c>
3897 #include <isl_multi_identity_templ.c>
3898 #include <isl_multi_insert_domain_templ.c>
3899 #include <isl_multi_locals_templ.c>
3900 #include <isl_multi_move_dims_templ.c>
3901 #include <isl_multi_nan_templ.c>
3902 #include <isl_multi_product_templ.c>
3903 #include <isl_multi_splice_templ.c>
3904 #include <isl_multi_tuple_id_templ.c>
3905 #include <isl_multi_unbind_params_templ.c>
3906 #include <isl_multi_zero_templ.c>
3910 #include <isl_multi_gist.c>
3913 #define DOMBASE basic_set
3914 #include <isl_multi_bind_templ.c>
3916 /* Construct an isl_multi_aff living in "space" that corresponds
3917 * to the affine transformation matrix "mat".
3919 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3920 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3923 isl_local_space
*ls
= NULL
;
3924 isl_multi_aff
*ma
= NULL
;
3925 isl_size n_row
, n_col
, n_out
, total
;
3931 ctx
= isl_mat_get_ctx(mat
);
3933 n_row
= isl_mat_rows(mat
);
3934 n_col
= isl_mat_cols(mat
);
3935 n_out
= isl_space_dim(space
, isl_dim_out
);
3936 total
= isl_space_dim(space
, isl_dim_all
);
3937 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3940 isl_die(ctx
, isl_error_invalid
,
3941 "insufficient number of rows", goto error
);
3943 isl_die(ctx
, isl_error_invalid
,
3944 "insufficient number of columns", goto error
);
3945 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3946 isl_die(ctx
, isl_error_invalid
,
3947 "dimension mismatch", goto error
);
3949 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3950 space
= isl_space_domain(space
);
3951 ls
= isl_local_space_from_space(isl_space_copy(space
));
3953 for (i
= 0; i
< n_row
- 1; ++i
) {
3957 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3960 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3961 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3962 v
= isl_vec_normalize(v
);
3963 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3964 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3967 isl_space_free(space
);
3968 isl_local_space_free(ls
);
3972 isl_space_free(space
);
3973 isl_local_space_free(ls
);
3975 isl_multi_aff_free(ma
);
3979 /* Return the constant terms of the affine expressions of "ma".
3981 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3982 __isl_keep isl_multi_aff
*ma
)
3989 n
= isl_multi_aff_size(ma
);
3992 space
= isl_space_range(isl_multi_aff_get_space(ma
));
3993 space
= isl_space_drop_all_params(space
);
3994 mv
= isl_multi_val_zero(space
);
3996 for (i
= 0; i
< n
; ++i
) {
4000 aff
= isl_multi_aff_get_at(ma
, i
);
4001 val
= isl_aff_get_constant_val(aff
);
4003 mv
= isl_multi_val_set_at(mv
, i
, val
);
4009 /* Remove any internal structure of the domain of "ma".
4010 * If there is any such internal structure in the input,
4011 * then the name of the corresponding space is also removed.
4013 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4014 __isl_take isl_multi_aff
*ma
)
4021 if (!ma
->space
->nested
[0])
4024 space
= isl_multi_aff_get_space(ma
);
4025 space
= isl_space_flatten_domain(space
);
4026 ma
= isl_multi_aff_reset_space(ma
, space
);
4031 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4032 * of the space to its domain.
4034 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4038 isl_local_space
*ls
;
4043 if (!isl_space_is_map(space
))
4044 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4045 "not a map space", goto error
);
4047 n_in
= isl_space_dim(space
, isl_dim_in
);
4050 space
= isl_space_domain_map(space
);
4052 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4054 isl_space_free(space
);
4058 space
= isl_space_domain(space
);
4059 ls
= isl_local_space_from_space(space
);
4060 for (i
= 0; i
< n_in
; ++i
) {
4063 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4065 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4067 isl_local_space_free(ls
);
4070 isl_space_free(space
);
4074 /* This function performs the same operation as isl_multi_aff_domain_map,
4075 * but is considered as a function on an isl_space when exported.
4077 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4078 __isl_take isl_space
*space
)
4080 return isl_multi_aff_domain_map(space
);
4083 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4084 * of the space to its range.
4086 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4089 isl_size n_in
, n_out
;
4090 isl_local_space
*ls
;
4095 if (!isl_space_is_map(space
))
4096 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4097 "not a map space", goto error
);
4099 n_in
= isl_space_dim(space
, isl_dim_in
);
4100 n_out
= isl_space_dim(space
, isl_dim_out
);
4101 if (n_in
< 0 || n_out
< 0)
4103 space
= isl_space_range_map(space
);
4105 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4107 isl_space_free(space
);
4111 space
= isl_space_domain(space
);
4112 ls
= isl_local_space_from_space(space
);
4113 for (i
= 0; i
< n_out
; ++i
) {
4116 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4117 isl_dim_set
, n_in
+ i
);
4118 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4120 isl_local_space_free(ls
);
4123 isl_space_free(space
);
4127 /* This function performs the same operation as isl_multi_aff_range_map,
4128 * but is considered as a function on an isl_space when exported.
4130 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4131 __isl_take isl_space
*space
)
4133 return isl_multi_aff_range_map(space
);
4136 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4137 * of the space to its domain.
4139 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4140 __isl_take isl_space
*space
)
4142 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4145 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4146 * but is considered as a function on an isl_space when exported.
4148 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4149 __isl_take isl_space
*space
)
4151 return isl_pw_multi_aff_domain_map(space
);
4154 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4155 * of the space to its range.
4157 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4158 __isl_take isl_space
*space
)
4160 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4163 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4164 * but is considered as a function on an isl_space when exported.
4166 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4167 __isl_take isl_space
*space
)
4169 return isl_pw_multi_aff_range_map(space
);
4172 /* Given the space of a set and a range of set dimensions,
4173 * construct an isl_multi_aff that projects out those dimensions.
4175 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4176 __isl_take isl_space
*space
, enum isl_dim_type type
,
4177 unsigned first
, unsigned n
)
4181 isl_local_space
*ls
;
4186 if (!isl_space_is_set(space
))
4187 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4188 "expecting set space", goto error
);
4189 if (type
!= isl_dim_set
)
4190 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4191 "only set dimensions can be projected out", goto error
);
4192 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4195 dim
= isl_space_dim(space
, isl_dim_set
);
4199 space
= isl_space_from_domain(space
);
4200 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4203 return isl_multi_aff_alloc(space
);
4205 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4206 space
= isl_space_domain(space
);
4207 ls
= isl_local_space_from_space(space
);
4209 for (i
= 0; i
< first
; ++i
) {
4212 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4214 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4217 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4220 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4221 isl_dim_set
, first
+ n
+ i
);
4222 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4225 isl_local_space_free(ls
);
4228 isl_space_free(space
);
4232 /* Given the space of a set and a range of set dimensions,
4233 * construct an isl_pw_multi_aff that projects out those dimensions.
4235 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4236 __isl_take isl_space
*space
, enum isl_dim_type type
,
4237 unsigned first
, unsigned n
)
4241 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4242 return isl_pw_multi_aff_from_multi_aff(ma
);
4245 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4246 * but is considered as a function on an isl_multi_aff when exported.
4248 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4249 __isl_take isl_multi_aff
*ma
)
4251 return isl_pw_multi_aff_from_multi_aff(ma
);
4254 /* Create a piecewise multi-affine expression in the given space that maps each
4255 * input dimension to the corresponding output dimension.
4257 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4258 __isl_take isl_space
*space
)
4260 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4263 /* Create a piecewise multi expression that maps elements in the given space
4266 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4267 __isl_take isl_space
*space
)
4271 ma
= isl_multi_aff_identity_on_domain_space(space
);
4272 return isl_pw_multi_aff_from_multi_aff(ma
);
4275 /* This function performs the same operation as
4276 * isl_pw_multi_aff_identity_on_domain_space,
4277 * but is considered as a function on an isl_space when exported.
4279 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4280 __isl_take isl_space
*space
)
4282 return isl_pw_multi_aff_identity_on_domain_space(space
);
4285 /* Exploit the equalities in "eq" to simplify the affine expressions.
4287 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4288 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4292 maff
= isl_multi_aff_cow(maff
);
4296 for (i
= 0; i
< maff
->n
; ++i
) {
4297 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4298 isl_basic_set_copy(eq
));
4303 isl_basic_set_free(eq
);
4306 isl_basic_set_free(eq
);
4307 isl_multi_aff_free(maff
);
4311 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4316 maff
= isl_multi_aff_cow(maff
);
4320 for (i
= 0; i
< maff
->n
; ++i
) {
4321 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4323 return isl_multi_aff_free(maff
);
4329 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4330 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4332 maff1
= isl_multi_aff_add(maff1
, maff2
);
4333 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4337 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4340 return isl_bool_error
;
4342 return isl_bool_false
;
4345 /* Return the set of domain elements where "ma1" is lexicographically
4346 * smaller than or equal to "ma2".
4348 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4349 __isl_take isl_multi_aff
*ma2
)
4351 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4354 /* Return the set of domain elements where "ma1" is lexicographically
4355 * smaller than "ma2".
4357 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4358 __isl_take isl_multi_aff
*ma2
)
4360 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4363 /* Return the set of domain elements where "ma1" is lexicographically
4364 * greater than to "ma2". If "equal" is set, then include the domain
4365 * elements where they are equal.
4366 * Do this for the case where there are no entries.
4367 * In this case, "ma1" cannot be greater than "ma2",
4368 * but it is (greater than or) equal to "ma2".
4370 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4371 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4375 space
= isl_multi_aff_get_domain_space(ma1
);
4377 isl_multi_aff_free(ma1
);
4378 isl_multi_aff_free(ma2
);
4381 return isl_set_universe(space
);
4383 return isl_set_empty(space
);
4386 /* Return the set where entry "i" of "ma1" and "ma2"
4387 * satisfy the relation prescribed by "cmp".
4389 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4390 __isl_keep isl_multi_aff
*ma2
, int i
,
4391 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4392 __isl_take isl_aff
*aff2
))
4394 isl_aff
*aff1
, *aff2
;
4396 aff1
= isl_multi_aff_get_at(ma1
, i
);
4397 aff2
= isl_multi_aff_get_at(ma2
, i
);
4398 return cmp(aff1
, aff2
);
4401 /* Return the set of domain elements where "ma1" is lexicographically
4402 * greater than to "ma2". If "equal" is set, then include the domain
4403 * elements where they are equal.
4405 * In particular, for all but the final entry,
4406 * include the set of elements where this entry is strictly greater in "ma1"
4407 * and all previous entries are equal.
4408 * The final entry is also allowed to be equal in the two functions
4409 * if "equal" is set.
4411 * The case where there are no entries is handled separately.
4413 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4414 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4423 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4425 n
= isl_multi_aff_size(ma1
);
4429 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4431 space
= isl_multi_aff_get_domain_space(ma1
);
4432 res
= isl_set_empty(isl_space_copy(space
));
4433 equal_set
= isl_set_universe(space
);
4435 for (i
= 0; i
+ 1 < n
; ++i
) {
4439 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4440 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4441 res
= isl_set_union(res
, gt
);
4442 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4443 equal_set
= isl_set_intersect(equal_set
, eq
);
4445 empty
= isl_set_is_empty(equal_set
);
4446 if (empty
>= 0 && empty
)
4451 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4453 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4454 isl_multi_aff_free(ma1
);
4455 isl_multi_aff_free(ma2
);
4457 gte
= isl_set_intersect(gte
, equal_set
);
4458 return isl_set_union(res
, gte
);
4460 isl_multi_aff_free(ma1
);
4461 isl_multi_aff_free(ma2
);
4465 /* Return the set of domain elements where "ma1" is lexicographically
4466 * greater than or equal to "ma2".
4468 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4469 __isl_take isl_multi_aff
*ma2
)
4471 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4474 /* Return the set of domain elements where "ma1" is lexicographically
4475 * greater than "ma2".
4477 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4478 __isl_take isl_multi_aff
*ma2
)
4480 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4483 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4486 #define PW isl_pw_multi_aff
4488 #define BASE multi_aff
4490 #define EL_IS_ZERO is_empty
4494 #define IS_ZERO is_empty
4497 #undef DEFAULT_IS_ZERO
4498 #define DEFAULT_IS_ZERO 0
4500 #include <isl_pw_templ.c>
4501 #include <isl_pw_add_constant_multi_val_templ.c>
4502 #include <isl_pw_add_constant_val_templ.c>
4503 #include <isl_pw_bind_domain_templ.c>
4504 #include <isl_pw_insert_dims_templ.c>
4505 #include <isl_pw_insert_domain_templ.c>
4506 #include <isl_pw_locals_templ.c>
4507 #include <isl_pw_move_dims_templ.c>
4508 #include <isl_pw_neg_templ.c>
4509 #include <isl_pw_pullback_templ.c>
4510 #include <isl_pw_range_tuple_id_templ.c>
4511 #include <isl_pw_union_opt.c>
4514 #define BASE pw_multi_aff
4516 #include <isl_union_multi.c>
4517 #include "isl_union_locals_templ.c"
4518 #include <isl_union_neg.c>
4521 #define BASE multi_aff
4523 #include <isl_union_pw_templ.c>
4525 /* Generic function for extracting a factor from a product "pma".
4526 * "check_space" checks that the space is that of the right kind of product.
4527 * "space_factor" extracts the factor from the space.
4528 * "multi_aff_factor" extracts the factor from the constituent functions.
4530 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4531 __isl_take isl_pw_multi_aff
*pma
,
4532 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4533 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4534 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4535 __isl_take isl_multi_aff
*ma
))
4540 if (check_space(pma
) < 0)
4541 return isl_pw_multi_aff_free(pma
);
4543 space
= isl_pw_multi_aff_take_space(pma
);
4544 space
= space_factor(space
);
4546 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4549 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4550 ma
= multi_aff_factor(ma
);
4551 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4554 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4559 /* Is the range of "pma" a wrapped relation?
4561 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4562 __isl_keep isl_pw_multi_aff
*pma
)
4564 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4567 /* Check that the range of "pma" is a product.
4569 static isl_stat
pw_multi_aff_check_range_product(
4570 __isl_keep isl_pw_multi_aff
*pma
)
4574 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4576 return isl_stat_error
;
4578 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4579 "range is not a product", return isl_stat_error
);
4583 /* Given a function A -> [B -> C], extract the function A -> B.
4585 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4586 __isl_take isl_pw_multi_aff
*pma
)
4588 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4589 &isl_space_range_factor_domain
,
4590 &isl_multi_aff_range_factor_domain
);
4593 /* Given a function A -> [B -> C], extract the function A -> C.
4595 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4596 __isl_take isl_pw_multi_aff
*pma
)
4598 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4599 &isl_space_range_factor_range
,
4600 &isl_multi_aff_range_factor_range
);
4603 /* Given two piecewise multi affine expressions, return a piecewise
4604 * multi-affine expression defined on the union of the definition domains
4605 * of the inputs that is equal to the lexicographic maximum of the two
4606 * inputs on each cell. If only one of the two inputs is defined on
4607 * a given cell, then it is considered to be the maximum.
4609 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4610 __isl_take isl_pw_multi_aff
*pma1
,
4611 __isl_take isl_pw_multi_aff
*pma2
)
4613 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4614 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4615 &isl_multi_aff_lex_ge_set
);
4618 /* Given two piecewise multi affine expressions, return a piecewise
4619 * multi-affine expression defined on the union of the definition domains
4620 * of the inputs that is equal to the lexicographic minimum of the two
4621 * inputs on each cell. If only one of the two inputs is defined on
4622 * a given cell, then it is considered to be the minimum.
4624 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4625 __isl_take isl_pw_multi_aff
*pma1
,
4626 __isl_take isl_pw_multi_aff
*pma2
)
4628 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4629 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4630 &isl_multi_aff_lex_le_set
);
4633 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4634 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4636 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4637 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4638 &isl_multi_aff_add
);
4641 /* Subtract "pma2" from "pma1" and return the result.
4643 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4644 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4646 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4647 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4648 &isl_multi_aff_sub
);
4651 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4652 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4654 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4657 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4658 * with the actual sum on the shared domain and
4659 * the defined expression on the symmetric difference of the domains.
4661 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4662 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4664 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4667 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4668 * with the actual sum on the shared domain and
4669 * the defined expression on the symmetric difference of the domains.
4671 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4672 __isl_take isl_union_pw_multi_aff
*upma1
,
4673 __isl_take isl_union_pw_multi_aff
*upma2
)
4675 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4678 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4679 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4681 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4682 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4686 isl_pw_multi_aff
*res
;
4688 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4691 n
= pma1
->n
* pma2
->n
;
4692 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4693 isl_space_copy(pma2
->dim
));
4694 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4696 for (i
= 0; i
< pma1
->n
; ++i
) {
4697 for (j
= 0; j
< pma2
->n
; ++j
) {
4701 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4702 isl_set_copy(pma2
->p
[j
].set
));
4703 ma
= isl_multi_aff_product(
4704 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4705 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4706 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4710 isl_pw_multi_aff_free(pma1
);
4711 isl_pw_multi_aff_free(pma2
);
4714 isl_pw_multi_aff_free(pma1
);
4715 isl_pw_multi_aff_free(pma2
);
4719 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4720 * denominator "denom".
4721 * "denom" is allowed to be negative, in which case the actual denominator
4722 * is -denom and the expressions are added instead.
4724 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4725 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4731 first
= isl_seq_first_non_zero(c
, n
);
4735 sign
= isl_int_sgn(denom
);
4737 isl_int_abs(d
, denom
);
4738 for (i
= first
; i
< n
; ++i
) {
4741 if (isl_int_is_zero(c
[i
]))
4743 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4744 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4745 aff_i
= isl_aff_scale_down(aff_i
, d
);
4747 aff
= isl_aff_sub(aff
, aff_i
);
4749 aff
= isl_aff_add(aff
, aff_i
);
4756 /* Extract an affine expression that expresses the output dimension "pos"
4757 * of "bmap" in terms of the parameters and input dimensions from
4759 * Note that this expression may involve integer divisions defined
4760 * in terms of parameters and input dimensions.
4761 * The equality may also involve references to earlier (but not later)
4762 * output dimensions. These are replaced by the corresponding elements
4765 * If the equality is of the form
4767 * f(i) + h(j) + a x + g(i) = 0,
4769 * with f(i) a linear combinations of the parameters and input dimensions,
4770 * g(i) a linear combination of integer divisions defined in terms of the same
4771 * and h(j) a linear combinations of earlier output dimensions,
4772 * then the affine expression is
4774 * (-f(i) - g(i))/a - h(j)/a
4776 * If the equality is of the form
4778 * f(i) + h(j) - a x + g(i) = 0,
4780 * then the affine expression is
4782 * (f(i) + g(i))/a - h(j)/(-a)
4785 * If "div" refers to an integer division (i.e., it is smaller than
4786 * the number of integer divisions), then the equality constraint
4787 * does involve an integer division (the one at position "div") that
4788 * is defined in terms of output dimensions. However, this integer
4789 * division can be eliminated by exploiting a pair of constraints
4790 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4791 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4793 * In particular, let
4795 * x = e(i) + m floor(...)
4797 * with e(i) the expression derived above and floor(...) the integer
4798 * division involving output dimensions.
4809 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4810 * = (e(i) - l) mod m
4814 * x - l = (e(i) - l) mod m
4818 * x = ((e(i) - l) mod m) + l
4820 * The variable "shift" below contains the expression -l, which may
4821 * also involve a linear combination of earlier output dimensions.
4823 static __isl_give isl_aff
*extract_aff_from_equality(
4824 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4825 __isl_keep isl_multi_aff
*ma
)
4828 isl_size n_div
, n_out
;
4830 isl_local_space
*ls
;
4831 isl_aff
*aff
, *shift
;
4834 ctx
= isl_basic_map_get_ctx(bmap
);
4835 ls
= isl_basic_map_get_local_space(bmap
);
4836 ls
= isl_local_space_domain(ls
);
4837 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4840 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4841 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4842 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4843 if (n_out
< 0 || n_div
< 0)
4845 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4846 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4847 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4848 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4850 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4851 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4852 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4855 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4856 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4857 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4858 bmap
->eq
[eq
][o_out
+ pos
]);
4860 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4863 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4864 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4865 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4866 isl_int_set_si(shift
->v
->el
[0], 1);
4867 shift
= subtract_initial(shift
, ma
, pos
,
4868 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4869 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4870 mod
= isl_val_int_from_isl_int(ctx
,
4871 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4872 mod
= isl_val_abs(mod
);
4873 aff
= isl_aff_mod_val(aff
, mod
);
4874 aff
= isl_aff_sub(aff
, shift
);
4877 isl_local_space_free(ls
);
4880 isl_local_space_free(ls
);
4885 /* Given a basic map with output dimensions defined
4886 * in terms of the parameters input dimensions and earlier
4887 * output dimensions using an equality (and possibly a pair on inequalities),
4888 * extract an isl_aff that expresses output dimension "pos" in terms
4889 * of the parameters and input dimensions.
4890 * Note that this expression may involve integer divisions defined
4891 * in terms of parameters and input dimensions.
4892 * "ma" contains the expressions corresponding to earlier output dimensions.
4894 * This function shares some similarities with
4895 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4897 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4898 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4905 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4906 if (eq
>= bmap
->n_eq
)
4907 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4908 "unable to find suitable equality", return NULL
);
4909 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4911 aff
= isl_aff_remove_unused_divs(aff
);
4915 /* Given a basic map where each output dimension is defined
4916 * in terms of the parameters and input dimensions using an equality,
4917 * extract an isl_multi_aff that expresses the output dimensions in terms
4918 * of the parameters and input dimensions.
4920 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4921 __isl_take isl_basic_map
*bmap
)
4930 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4931 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4933 ma
= isl_multi_aff_free(ma
);
4935 for (i
= 0; i
< n_out
; ++i
) {
4938 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4939 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4942 isl_basic_map_free(bmap
);
4947 /* Given a basic set where each set dimension is defined
4948 * in terms of the parameters using an equality,
4949 * extract an isl_multi_aff that expresses the set dimensions in terms
4950 * of the parameters.
4952 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4953 __isl_take isl_basic_set
*bset
)
4955 return extract_isl_multi_aff_from_basic_map(bset
);
4958 /* Create an isl_pw_multi_aff that is equivalent to
4959 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4960 * The given basic map is such that each output dimension is defined
4961 * in terms of the parameters and input dimensions using an equality.
4963 * Since some applications expect the result of isl_pw_multi_aff_from_map
4964 * to only contain integer affine expressions, we compute the floor
4965 * of the expression before returning.
4967 * Remove all constraints involving local variables without
4968 * an explicit representation (resulting in the removal of those
4969 * local variables) prior to the actual extraction to ensure
4970 * that the local spaces in which the resulting affine expressions
4971 * are created do not contain any unknown local variables.
4972 * Removing such constraints is safe because constraints involving
4973 * unknown local variables are not used to determine whether
4974 * a basic map is obviously single-valued.
4976 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4977 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4981 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4982 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4983 ma
= isl_multi_aff_floor(ma
);
4984 return isl_pw_multi_aff_alloc(domain
, ma
);
4987 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4988 * This obviously only works if the input "map" is single-valued.
4989 * If so, we compute the lexicographic minimum of the image in the form
4990 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4991 * to its lexicographic minimum.
4992 * If the input is not single-valued, we produce an error.
4994 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4995 __isl_take isl_map
*map
)
4999 isl_pw_multi_aff
*pma
;
5001 sv
= isl_map_is_single_valued(map
);
5005 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5006 "map is not single-valued", goto error
);
5007 map
= isl_map_make_disjoint(map
);
5011 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5013 for (i
= 0; i
< map
->n
; ++i
) {
5014 isl_pw_multi_aff
*pma_i
;
5015 isl_basic_map
*bmap
;
5016 bmap
= isl_basic_map_copy(map
->p
[i
]);
5017 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5018 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5028 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5029 * taking into account that the output dimension at position "d"
5030 * can be represented as
5032 * x = floor((e(...) + c1) / m)
5034 * given that constraint "i" is of the form
5036 * e(...) + c1 - m x >= 0
5039 * Let "map" be of the form
5043 * We construct a mapping
5045 * A -> [A -> x = floor(...)]
5047 * apply that to the map, obtaining
5049 * [A -> x = floor(...)] -> B
5051 * and equate dimension "d" to x.
5052 * We then compute a isl_pw_multi_aff representation of the resulting map
5053 * and plug in the mapping above.
5055 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5056 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5059 isl_space
*space
= NULL
;
5060 isl_local_space
*ls
;
5068 isl_pw_multi_aff
*pma
;
5071 is_set
= isl_map_is_set(map
);
5075 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5076 ctx
= isl_map_get_ctx(map
);
5077 space
= isl_space_domain(isl_map_get_space(map
));
5078 n_in
= isl_space_dim(space
, isl_dim_set
);
5079 n
= isl_space_dim(space
, isl_dim_all
);
5080 if (n_in
< 0 || n
< 0)
5083 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5085 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5086 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5088 isl_basic_map_free(hull
);
5090 ls
= isl_local_space_from_space(isl_space_copy(space
));
5091 aff
= isl_aff_alloc_vec(ls
, v
);
5092 aff
= isl_aff_floor(aff
);
5094 isl_space_free(space
);
5095 ma
= isl_multi_aff_from_aff(aff
);
5097 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5098 ma
= isl_multi_aff_range_product(ma
,
5099 isl_multi_aff_from_aff(aff
));
5102 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5103 map
= isl_map_apply_domain(map
, insert
);
5104 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5105 pma
= isl_pw_multi_aff_from_map(map
);
5106 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5110 isl_space_free(space
);
5112 isl_basic_map_free(hull
);
5116 /* Is constraint "c" of the form
5118 * e(...) + c1 - m x >= 0
5122 * -e(...) + c2 + m x >= 0
5124 * where m > 1 and e only depends on parameters and input dimemnsions?
5126 * "offset" is the offset of the output dimensions
5127 * "pos" is the position of output dimension x.
5129 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5131 if (isl_int_is_zero(c
[offset
+ d
]))
5133 if (isl_int_is_one(c
[offset
+ d
]))
5135 if (isl_int_is_negone(c
[offset
+ d
]))
5137 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5139 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5140 total
- (offset
+ d
+ 1)) != -1)
5145 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5147 * As a special case, we first check if there is any pair of constraints,
5148 * shared by all the basic maps in "map" that force a given dimension
5149 * to be equal to the floor of some affine combination of the input dimensions.
5151 * In particular, if we can find two constraints
5153 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5157 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5159 * where m > 1 and e only depends on parameters and input dimemnsions,
5162 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5164 * then we know that we can take
5166 * x = floor((e(...) + c1) / m)
5168 * without having to perform any computation.
5170 * Note that we know that
5174 * If c1 + c2 were 0, then we would have detected an equality during
5175 * simplification. If c1 + c2 were negative, then we would have detected
5178 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5179 __isl_take isl_map
*map
)
5187 isl_basic_map
*hull
;
5189 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5190 dim
= isl_map_dim(map
, isl_dim_out
);
5191 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5192 if (dim
< 0 || total
< 0)
5196 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5198 for (d
= 0; d
< dim
; ++d
) {
5199 for (i
= 0; i
< n
; ++i
) {
5200 if (!is_potential_div_constraint(hull
->ineq
[i
],
5201 offset
, d
, 1 + total
))
5203 for (j
= i
+ 1; j
< n
; ++j
) {
5204 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5205 hull
->ineq
[j
] + 1, total
))
5207 isl_int_add(sum
, hull
->ineq
[i
][0],
5209 if (isl_int_abs_lt(sum
,
5210 hull
->ineq
[i
][offset
+ d
]))
5217 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5219 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5223 isl_basic_map_free(hull
);
5224 return pw_multi_aff_from_map_base(map
);
5227 isl_basic_map_free(hull
);
5231 /* Given an affine expression
5233 * [A -> B] -> f(A,B)
5235 * construct an isl_multi_aff
5239 * such that dimension "d" in B' is set to "aff" and the remaining
5240 * dimensions are set equal to the corresponding dimensions in B.
5241 * "n_in" is the dimension of the space A.
5242 * "n_out" is the dimension of the space B.
5244 * If "is_set" is set, then the affine expression is of the form
5248 * and we construct an isl_multi_aff
5252 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5253 unsigned n_in
, unsigned n_out
, int is_set
)
5257 isl_space
*space
, *space2
;
5258 isl_local_space
*ls
;
5260 space
= isl_aff_get_domain_space(aff
);
5261 ls
= isl_local_space_from_space(isl_space_copy(space
));
5262 space2
= isl_space_copy(space
);
5264 space2
= isl_space_range(isl_space_unwrap(space2
));
5265 space
= isl_space_map_from_domain_and_range(space
, space2
);
5266 ma
= isl_multi_aff_alloc(space
);
5267 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5269 for (i
= 0; i
< n_out
; ++i
) {
5272 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5273 isl_dim_set
, n_in
+ i
);
5274 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5277 isl_local_space_free(ls
);
5282 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5283 * taking into account that the dimension at position "d" can be written as
5285 * x = m a + f(..) (1)
5287 * where m is equal to "gcd".
5288 * "i" is the index of the equality in "hull" that defines f(..).
5289 * In particular, the equality is of the form
5291 * f(..) - x + m g(existentials) = 0
5295 * -f(..) + x + m g(existentials) = 0
5297 * We basically plug (1) into "map", resulting in a map with "a"
5298 * in the range instead of "x". The corresponding isl_pw_multi_aff
5299 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5301 * Specifically, given the input map
5305 * We first wrap it into a set
5309 * and define (1) on top of the corresponding space, resulting in "aff".
5310 * We use this to create an isl_multi_aff that maps the output position "d"
5311 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5312 * We plug this into the wrapped map, unwrap the result and compute the
5313 * corresponding isl_pw_multi_aff.
5314 * The result is an expression
5322 * so that we can plug that into "aff", after extending the latter to
5328 * If "map" is actually a set, then there is no "A" space, meaning
5329 * that we do not need to perform any wrapping, and that the result
5330 * of the recursive call is of the form
5334 * which is plugged into a mapping of the form
5338 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5339 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5344 isl_local_space
*ls
;
5347 isl_pw_multi_aff
*pma
, *id
;
5353 is_set
= isl_map_is_set(map
);
5357 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5358 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5359 if (n_in
< 0 || n_out
< 0)
5361 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5366 set
= isl_map_wrap(map
);
5367 space
= isl_space_map_from_set(isl_set_get_space(set
));
5368 ma
= isl_multi_aff_identity(space
);
5369 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5370 aff
= isl_aff_alloc(ls
);
5372 isl_int_set_si(aff
->v
->el
[0], 1);
5373 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5374 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5377 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5379 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5381 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5382 set
= isl_set_preimage_multi_aff(set
, ma
);
5384 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5389 map
= isl_set_unwrap(set
);
5390 pma
= isl_pw_multi_aff_from_map(map
);
5393 space
= isl_pw_multi_aff_get_domain_space(pma
);
5394 space
= isl_space_map_from_set(space
);
5395 id
= isl_pw_multi_aff_identity(space
);
5396 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5398 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5399 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5401 isl_basic_map_free(hull
);
5405 isl_basic_map_free(hull
);
5409 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5410 * "hull" contains the equalities valid for "map".
5412 * Check if any of the output dimensions is "strided".
5413 * That is, we check if it can be written as
5417 * with m greater than 1, a some combination of existentially quantified
5418 * variables and f an expression in the parameters and input dimensions.
5419 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5421 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5424 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5425 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5434 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5435 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5436 if (n_div
< 0 || n_out
< 0)
5440 isl_basic_map_free(hull
);
5441 return pw_multi_aff_from_map_check_div(map
);
5446 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5447 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5449 for (i
= 0; i
< n_out
; ++i
) {
5450 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5451 isl_int
*eq
= hull
->eq
[j
];
5452 isl_pw_multi_aff
*res
;
5454 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5455 !isl_int_is_negone(eq
[o_out
+ i
]))
5457 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5459 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5460 n_out
- (i
+ 1)) != -1)
5462 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5463 if (isl_int_is_zero(gcd
))
5465 if (isl_int_is_one(gcd
))
5468 res
= pw_multi_aff_from_map_stride(map
, hull
,
5476 isl_basic_map_free(hull
);
5477 return pw_multi_aff_from_map_check_div(map
);
5480 isl_basic_map_free(hull
);
5484 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5486 * As a special case, we first check if all output dimensions are uniquely
5487 * defined in terms of the parameters and input dimensions over the entire
5488 * domain. If so, we extract the desired isl_pw_multi_aff directly
5489 * from the affine hull of "map" and its domain.
5491 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5494 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5498 isl_basic_map
*hull
;
5500 n
= isl_map_n_basic_map(map
);
5505 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5506 hull
= isl_basic_map_plain_affine_hull(hull
);
5507 sv
= isl_basic_map_plain_is_single_valued(hull
);
5509 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5511 isl_basic_map_free(hull
);
5513 map
= isl_map_detect_equalities(map
);
5514 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5515 sv
= isl_basic_map_plain_is_single_valued(hull
);
5517 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5519 return pw_multi_aff_from_map_check_strides(map
, hull
);
5520 isl_basic_map_free(hull
);
5526 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5527 * but is considered as a function on an isl_map when exported.
5529 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5531 return isl_pw_multi_aff_from_map(map
);
5534 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5536 return isl_pw_multi_aff_from_map(set
);
5539 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5540 * but is considered as a function on an isl_set when exported.
5542 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5544 return isl_pw_multi_aff_from_set(set
);
5547 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5550 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5552 isl_union_pw_multi_aff
**upma
= user
;
5553 isl_pw_multi_aff
*pma
;
5555 pma
= isl_pw_multi_aff_from_map(map
);
5556 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5558 return *upma
? isl_stat_ok
: isl_stat_error
;
5561 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5564 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5565 __isl_take isl_aff
*aff
)
5568 isl_pw_multi_aff
*pma
;
5570 ma
= isl_multi_aff_from_aff(aff
);
5571 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5572 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5575 /* Try and create an isl_union_pw_multi_aff that is equivalent
5576 * to the given isl_union_map.
5577 * The isl_union_map is required to be single-valued in each space.
5578 * Otherwise, an error is produced.
5580 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5581 __isl_take isl_union_map
*umap
)
5584 isl_union_pw_multi_aff
*upma
;
5586 space
= isl_union_map_get_space(umap
);
5587 upma
= isl_union_pw_multi_aff_empty(space
);
5588 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5589 upma
= isl_union_pw_multi_aff_free(upma
);
5590 isl_union_map_free(umap
);
5595 /* This function performs the same operation as
5596 * isl_union_pw_multi_aff_from_union_map,
5597 * but is considered as a function on an isl_union_map when exported.
5599 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5600 __isl_take isl_union_map
*umap
)
5602 return isl_union_pw_multi_aff_from_union_map(umap
);
5605 /* Try and create an isl_union_pw_multi_aff that is equivalent
5606 * to the given isl_union_set.
5607 * The isl_union_set is required to be a singleton in each space.
5608 * Otherwise, an error is produced.
5610 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5611 __isl_take isl_union_set
*uset
)
5613 return isl_union_pw_multi_aff_from_union_map(uset
);
5616 /* Return the piecewise affine expression "set ? 1 : 0".
5618 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5621 isl_space
*space
= isl_set_get_space(set
);
5622 isl_local_space
*ls
= isl_local_space_from_space(space
);
5623 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5624 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5626 one
= isl_aff_add_constant_si(one
, 1);
5627 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5628 set
= isl_set_complement(set
);
5629 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5634 /* Plug in "subs" for dimension "type", "pos" of "aff".
5636 * Let i be the dimension to replace and let "subs" be of the form
5640 * and "aff" of the form
5646 * (a f + d g')/(m d)
5648 * where g' is the result of plugging in "subs" in each of the integer
5651 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5652 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5658 aff
= isl_aff_cow(aff
);
5660 return isl_aff_free(aff
);
5662 ctx
= isl_aff_get_ctx(aff
);
5663 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5664 isl_die(ctx
, isl_error_invalid
,
5665 "spaces don't match", return isl_aff_free(aff
));
5666 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5668 return isl_aff_free(aff
);
5670 isl_die(ctx
, isl_error_unsupported
,
5671 "cannot handle divs yet", return isl_aff_free(aff
));
5673 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5675 return isl_aff_free(aff
);
5677 aff
->v
= isl_vec_cow(aff
->v
);
5679 return isl_aff_free(aff
);
5681 pos
+= isl_local_space_offset(aff
->ls
, type
);
5684 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5685 aff
->v
->size
, subs
->v
->size
, v
);
5691 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5692 * expressions in "maff".
5694 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5695 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5696 __isl_keep isl_aff
*subs
)
5700 maff
= isl_multi_aff_cow(maff
);
5702 return isl_multi_aff_free(maff
);
5704 if (type
== isl_dim_in
)
5707 for (i
= 0; i
< maff
->n
; ++i
) {
5708 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5711 return isl_multi_aff_free(maff
);
5717 /* Plug in "subs" for dimension "type", "pos" of "pma".
5719 * pma is of the form
5723 * while subs is of the form
5725 * v' = B_j(v) -> S_j
5727 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5728 * has a contribution in the result, in particular
5730 * C_ij(S_j) -> M_i(S_j)
5732 * Note that plugging in S_j in C_ij may also result in an empty set
5733 * and this contribution should simply be discarded.
5735 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5736 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5737 __isl_keep isl_pw_aff
*subs
)
5740 isl_pw_multi_aff
*res
;
5743 return isl_pw_multi_aff_free(pma
);
5745 n
= pma
->n
* subs
->n
;
5746 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5748 for (i
= 0; i
< pma
->n
; ++i
) {
5749 for (j
= 0; j
< subs
->n
; ++j
) {
5751 isl_multi_aff
*res_ij
;
5754 common
= isl_set_intersect(
5755 isl_set_copy(pma
->p
[i
].set
),
5756 isl_set_copy(subs
->p
[j
].set
));
5757 common
= isl_set_substitute(common
,
5758 type
, pos
, subs
->p
[j
].aff
);
5759 empty
= isl_set_plain_is_empty(common
);
5760 if (empty
< 0 || empty
) {
5761 isl_set_free(common
);
5767 res_ij
= isl_multi_aff_substitute(
5768 isl_multi_aff_copy(pma
->p
[i
].maff
),
5769 type
, pos
, subs
->p
[j
].aff
);
5771 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5775 isl_pw_multi_aff_free(pma
);
5778 isl_pw_multi_aff_free(pma
);
5779 isl_pw_multi_aff_free(res
);
5783 /* Compute the preimage of a range of dimensions in the affine expression "src"
5784 * under "ma" and put the result in "dst". The number of dimensions in "src"
5785 * that precede the range is given by "n_before". The number of dimensions
5786 * in the range is given by the number of output dimensions of "ma".
5787 * The number of dimensions that follow the range is given by "n_after".
5788 * If "has_denom" is set (to one),
5789 * then "src" and "dst" have an extra initial denominator.
5790 * "n_div_ma" is the number of existentials in "ma"
5791 * "n_div_bset" is the number of existentials in "src"
5792 * The resulting "dst" (which is assumed to have been allocated by
5793 * the caller) contains coefficients for both sets of existentials,
5794 * first those in "ma" and then those in "src".
5795 * f, c1, c2 and g are temporary objects that have been initialized
5798 * Let src represent the expression
5800 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5802 * and let ma represent the expressions
5804 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5806 * We start out with the following expression for dst:
5808 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5810 * with the multiplication factor f initially equal to 1
5811 * and f \sum_i b_i v_i kept separately.
5812 * For each x_i that we substitute, we multiply the numerator
5813 * (and denominator) of dst by c_1 = m_i and add the numerator
5814 * of the x_i expression multiplied by c_2 = f b_i,
5815 * after removing the common factors of c_1 and c_2.
5816 * The multiplication factor f also needs to be multiplied by c_1
5817 * for the next x_j, j > i.
5819 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5820 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5821 int n_div_ma
, int n_div_bmap
,
5822 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5825 isl_size n_param
, n_in
, n_out
;
5828 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5829 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5830 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5831 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5832 return isl_stat_error
;
5834 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5835 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5836 isl_seq_clr(dst
+ o_dst
, n_in
);
5839 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5842 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5844 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5846 isl_int_set_si(f
, 1);
5848 for (i
= 0; i
< n_out
; ++i
) {
5849 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5851 if (isl_int_is_zero(src
[offset
]))
5853 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5854 isl_int_mul(c2
, f
, src
[offset
]);
5855 isl_int_gcd(g
, c1
, c2
);
5856 isl_int_divexact(c1
, c1
, g
);
5857 isl_int_divexact(c2
, c2
, g
);
5859 isl_int_mul(f
, f
, c1
);
5862 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5863 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5864 o_dst
+= 1 + n_param
;
5865 o_src
+= 1 + n_param
;
5866 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5868 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5869 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5872 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5874 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5875 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5878 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5880 isl_int_mul(dst
[0], dst
[0], c1
);
5886 /* Compute the pullback of "aff" by the function represented by "ma".
5887 * In other words, plug in "ma" in "aff". The result is an affine expression
5888 * defined over the domain space of "ma".
5890 * If "aff" is represented by
5892 * (a(p) + b x + c(divs))/d
5894 * and ma is represented by
5896 * x = D(p) + F(y) + G(divs')
5898 * then the result is
5900 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5902 * The divs in the local space of the input are similarly adjusted
5903 * through a call to isl_local_space_preimage_multi_aff.
5905 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5906 __isl_take isl_multi_aff
*ma
)
5908 isl_aff
*res
= NULL
;
5909 isl_local_space
*ls
;
5910 isl_size n_div_aff
, n_div_ma
;
5911 isl_int f
, c1
, c2
, g
;
5913 ma
= isl_multi_aff_align_divs(ma
);
5917 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5918 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5919 if (n_div_aff
< 0 || n_div_ma
< 0)
5922 ls
= isl_aff_get_domain_local_space(aff
);
5923 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5924 res
= isl_aff_alloc(ls
);
5933 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5934 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5935 res
= isl_aff_free(res
);
5943 isl_multi_aff_free(ma
);
5944 res
= isl_aff_normalize(res
);
5948 isl_multi_aff_free(ma
);
5953 /* Compute the pullback of "aff1" by the function represented by "aff2".
5954 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5955 * defined over the domain space of "aff1".
5957 * The domain of "aff1" should match the range of "aff2", which means
5958 * that it should be single-dimensional.
5960 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5961 __isl_take isl_aff
*aff2
)
5965 ma
= isl_multi_aff_from_aff(aff2
);
5966 return isl_aff_pullback_multi_aff(aff1
, ma
);
5969 /* Compute the pullback of "ma1" by the function represented by "ma2".
5970 * In other words, plug in "ma2" in "ma1".
5972 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5973 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5976 isl_space
*space
= NULL
;
5978 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5979 ma2
= isl_multi_aff_align_divs(ma2
);
5980 ma1
= isl_multi_aff_cow(ma1
);
5984 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5985 isl_multi_aff_get_space(ma1
));
5987 for (i
= 0; i
< ma1
->n
; ++i
) {
5988 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5989 isl_multi_aff_copy(ma2
));
5994 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5995 isl_multi_aff_free(ma2
);
5998 isl_space_free(space
);
5999 isl_multi_aff_free(ma2
);
6000 isl_multi_aff_free(ma1
);
6004 /* Extend the local space of "dst" to include the divs
6005 * in the local space of "src".
6007 * If "src" does not have any divs or if the local spaces of "dst" and
6008 * "src" are the same, then no extension is required.
6010 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6011 __isl_keep isl_aff
*src
)
6014 isl_size src_n_div
, dst_n_div
;
6021 return isl_aff_free(dst
);
6023 ctx
= isl_aff_get_ctx(src
);
6024 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6026 return isl_aff_free(dst
);
6028 isl_die(ctx
, isl_error_invalid
,
6029 "spaces don't match", goto error
);
6031 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6032 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6035 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6036 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6037 return isl_aff_free(dst
);
6041 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6042 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6043 if (!exp1
|| (dst_n_div
&& !exp2
))
6046 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6047 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6055 return isl_aff_free(dst
);
6058 /* Adjust the local spaces of the affine expressions in "maff"
6059 * such that they all have the save divs.
6061 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6062 __isl_take isl_multi_aff
*maff
)
6070 maff
= isl_multi_aff_cow(maff
);
6074 for (i
= 1; i
< maff
->n
; ++i
)
6075 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
6076 for (i
= 1; i
< maff
->n
; ++i
) {
6077 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
6079 return isl_multi_aff_free(maff
);
6085 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6087 aff
= isl_aff_cow(aff
);
6091 aff
->ls
= isl_local_space_lift(aff
->ls
);
6093 return isl_aff_free(aff
);
6098 /* Lift "maff" to a space with extra dimensions such that the result
6099 * has no more existentially quantified variables.
6100 * If "ls" is not NULL, then *ls is assigned the local space that lies
6101 * at the basis of the lifting applied to "maff".
6103 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6104 __isl_give isl_local_space
**ls
)
6118 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6119 *ls
= isl_local_space_from_space(space
);
6121 return isl_multi_aff_free(maff
);
6126 maff
= isl_multi_aff_cow(maff
);
6127 maff
= isl_multi_aff_align_divs(maff
);
6131 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
6133 return isl_multi_aff_free(maff
);
6134 space
= isl_multi_aff_get_space(maff
);
6135 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6136 space
= isl_space_extend_domain_with_range(space
,
6137 isl_multi_aff_get_space(maff
));
6139 return isl_multi_aff_free(maff
);
6140 isl_space_free(maff
->space
);
6141 maff
->space
= space
;
6144 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
6146 return isl_multi_aff_free(maff
);
6149 for (i
= 0; i
< maff
->n
; ++i
) {
6150 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
6158 isl_local_space_free(*ls
);
6159 return isl_multi_aff_free(maff
);
6163 #define TYPE isl_pw_multi_aff
6165 #include "check_type_range_templ.c"
6167 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6169 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6170 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6177 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6180 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6184 space
= isl_pw_multi_aff_get_space(pma
);
6185 space
= isl_space_drop_dims(space
, isl_dim_out
,
6186 pos
+ 1, n_out
- pos
- 1);
6187 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6189 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6190 for (i
= 0; i
< pma
->n
; ++i
) {
6192 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6193 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6199 /* This is an alternative name for the function above.
6201 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6202 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6204 return isl_pw_multi_aff_get_at(pma
, pos
);
6207 /* Return an isl_pw_multi_aff with the given "set" as domain and
6208 * an unnamed zero-dimensional range.
6210 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6211 __isl_take isl_set
*set
)
6216 space
= isl_set_get_space(set
);
6217 space
= isl_space_from_domain(space
);
6218 ma
= isl_multi_aff_zero(space
);
6219 return isl_pw_multi_aff_alloc(set
, ma
);
6222 /* Add an isl_pw_multi_aff with the given "set" as domain and
6223 * an unnamed zero-dimensional range to *user.
6225 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6228 isl_union_pw_multi_aff
**upma
= user
;
6229 isl_pw_multi_aff
*pma
;
6231 pma
= isl_pw_multi_aff_from_domain(set
);
6232 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6237 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6238 * an unnamed zero-dimensional range.
6240 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6241 __isl_take isl_union_set
*uset
)
6244 isl_union_pw_multi_aff
*upma
;
6249 space
= isl_union_set_get_space(uset
);
6250 upma
= isl_union_pw_multi_aff_empty(space
);
6252 if (isl_union_set_foreach_set(uset
,
6253 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6256 isl_union_set_free(uset
);
6259 isl_union_set_free(uset
);
6260 isl_union_pw_multi_aff_free(upma
);
6264 /* Local data for bin_entry and the callback "fn".
6266 struct isl_union_pw_multi_aff_bin_data
{
6267 isl_union_pw_multi_aff
*upma2
;
6268 isl_union_pw_multi_aff
*res
;
6269 isl_pw_multi_aff
*pma
;
6270 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6273 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6274 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6276 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6278 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6282 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6284 isl_pw_multi_aff_free(pma
);
6289 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6290 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6291 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6292 * as *entry. The callback should adjust data->res if desired.
6294 static __isl_give isl_union_pw_multi_aff
*bin_op(
6295 __isl_take isl_union_pw_multi_aff
*upma1
,
6296 __isl_take isl_union_pw_multi_aff
*upma2
,
6297 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6300 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6302 space
= isl_union_pw_multi_aff_get_space(upma2
);
6303 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6304 space
= isl_union_pw_multi_aff_get_space(upma1
);
6305 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6307 if (!upma1
|| !upma2
)
6311 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6312 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6313 &bin_entry
, &data
) < 0)
6316 isl_union_pw_multi_aff_free(upma1
);
6317 isl_union_pw_multi_aff_free(upma2
);
6320 isl_union_pw_multi_aff_free(upma1
);
6321 isl_union_pw_multi_aff_free(upma2
);
6322 isl_union_pw_multi_aff_free(data
.res
);
6326 /* Given two isl_pw_multi_affs A -> B and C -> D,
6327 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6329 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6330 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6334 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6335 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6336 isl_pw_multi_aff_get_space(pma2
));
6337 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6338 &isl_multi_aff_range_product
);
6341 /* Given two isl_pw_multi_affs A -> B and C -> D,
6342 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6344 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6345 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6349 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6350 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6351 isl_pw_multi_aff_get_space(pma2
));
6352 space
= isl_space_flatten_range(space
);
6353 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6354 &isl_multi_aff_flat_range_product
);
6357 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6358 * to compute some form of range product and add the result to data->res.
6360 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6361 __isl_give isl_pw_multi_aff
*(*range_product
)(
6362 __isl_take isl_pw_multi_aff
*pma1
,
6363 __isl_take isl_pw_multi_aff
*pma2
),
6366 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6368 isl_space
*space1
, *space2
;
6370 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6371 space2
= isl_pw_multi_aff_peek_space(pma2
);
6372 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6373 space2
, isl_dim_in
);
6374 if (match
< 0 || !match
) {
6375 isl_pw_multi_aff_free(pma2
);
6376 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6379 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6381 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6386 /* If data->pma and "pma2" have the same domain space, then compute
6387 * their flat range product and add the result to data->res.
6389 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6392 return gen_range_product_entry(pma2
,
6393 &isl_pw_multi_aff_flat_range_product
, user
);
6396 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6397 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6399 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6400 __isl_take isl_union_pw_multi_aff
*upma1
,
6401 __isl_take isl_union_pw_multi_aff
*upma2
)
6403 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6406 /* If data->pma and "pma2" have the same domain space, then compute
6407 * their range product and add the result to data->res.
6409 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6412 return gen_range_product_entry(pma2
,
6413 &isl_pw_multi_aff_range_product
, user
);
6416 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6417 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6419 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6420 __isl_take isl_union_pw_multi_aff
*upma1
,
6421 __isl_take isl_union_pw_multi_aff
*upma2
)
6423 return bin_op(upma1
, upma2
, &range_product_entry
);
6426 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6427 * The parameters are assumed to have been aligned.
6429 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6430 * except that it works on two different isl_pw_* types.
6432 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6433 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6434 __isl_take isl_pw_aff
*pa
)
6437 isl_pw_multi_aff
*res
= NULL
;
6442 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6443 pa
->dim
, isl_dim_in
))
6444 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6445 "domains don't match", goto error
);
6446 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6450 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6452 for (i
= 0; i
< pma
->n
; ++i
) {
6453 for (j
= 0; j
< pa
->n
; ++j
) {
6455 isl_multi_aff
*res_ij
;
6458 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6459 isl_set_copy(pa
->p
[j
].set
));
6460 empty
= isl_set_plain_is_empty(common
);
6461 if (empty
< 0 || empty
) {
6462 isl_set_free(common
);
6468 res_ij
= isl_multi_aff_set_aff(
6469 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6470 isl_aff_copy(pa
->p
[j
].aff
));
6471 res_ij
= isl_multi_aff_gist(res_ij
,
6472 isl_set_copy(common
));
6474 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6478 isl_pw_multi_aff_free(pma
);
6479 isl_pw_aff_free(pa
);
6482 isl_pw_multi_aff_free(pma
);
6483 isl_pw_aff_free(pa
);
6484 return isl_pw_multi_aff_free(res
);
6487 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6489 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6490 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6491 __isl_take isl_pw_aff
*pa
)
6493 isl_bool equal_params
;
6497 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6498 if (equal_params
< 0)
6501 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6502 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6503 isl_pw_aff_check_named_params(pa
) < 0)
6505 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6506 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6507 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6509 isl_pw_multi_aff_free(pma
);
6510 isl_pw_aff_free(pa
);
6514 /* Do the parameters of "pa" match those of "space"?
6516 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6517 __isl_keep isl_space
*space
)
6519 isl_space
*pa_space
;
6523 return isl_bool_error
;
6525 pa_space
= isl_pw_aff_get_space(pa
);
6527 match
= isl_space_has_equal_params(space
, pa_space
);
6529 isl_space_free(pa_space
);
6533 /* Check that the domain space of "pa" matches "space".
6535 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6536 __isl_keep isl_space
*space
)
6538 isl_space
*pa_space
;
6542 return isl_stat_error
;
6544 pa_space
= isl_pw_aff_get_space(pa
);
6546 match
= isl_space_has_equal_params(space
, pa_space
);
6550 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6551 "parameters don't match", goto error
);
6552 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6553 pa_space
, isl_dim_in
);
6557 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6558 "domains don't match", goto error
);
6559 isl_space_free(pa_space
);
6562 isl_space_free(pa_space
);
6563 return isl_stat_error
;
6571 #include <isl_multi_explicit_domain.c>
6572 #include <isl_multi_pw_aff_explicit_domain.c>
6573 #include <isl_multi_templ.c>
6574 #include <isl_multi_add_constant_templ.c>
6575 #include <isl_multi_apply_set.c>
6576 #include <isl_multi_arith_templ.c>
6577 #include <isl_multi_bind_templ.c>
6578 #include <isl_multi_bind_domain_templ.c>
6579 #include <isl_multi_coalesce.c>
6580 #include <isl_multi_domain_templ.c>
6581 #include <isl_multi_dim_id_templ.c>
6582 #include <isl_multi_dims.c>
6583 #include <isl_multi_from_base_templ.c>
6584 #include <isl_multi_gist.c>
6585 #include <isl_multi_hash.c>
6586 #include <isl_multi_identity_templ.c>
6587 #include <isl_multi_align_set.c>
6588 #include <isl_multi_insert_domain_templ.c>
6589 #include <isl_multi_intersect.c>
6590 #include <isl_multi_min_max_templ.c>
6591 #include <isl_multi_move_dims_templ.c>
6592 #include <isl_multi_nan_templ.c>
6593 #include <isl_multi_param_templ.c>
6594 #include <isl_multi_product_templ.c>
6595 #include <isl_multi_splice_templ.c>
6596 #include <isl_multi_tuple_id_templ.c>
6597 #include <isl_multi_union_add_templ.c>
6598 #include <isl_multi_zero_templ.c>
6599 #include <isl_multi_unbind_params_templ.c>
6601 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6602 * with this explicit domain.
6604 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6605 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6609 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6612 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6613 map
= isl_map_intersect_domain(map
, dom
);
6618 /* Are all elements of "mpa" piecewise constants?
6620 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6622 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6625 /* Does "mpa" have a non-trivial explicit domain?
6627 * The explicit domain, if present, is trivial if it represents
6628 * an (obviously) universe set.
6630 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6631 __isl_keep isl_multi_pw_aff
*mpa
)
6634 return isl_bool_error
;
6635 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6636 return isl_bool_false
;
6637 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6643 #include "isl_opt_mpa_templ.c"
6645 /* Compute the minima of the set dimensions as a function of the
6646 * parameters, but independently of the other set dimensions.
6648 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6650 return set_opt_mpa(set
, &isl_set_dim_min
);
6653 /* Compute the maxima of the set dimensions as a function of the
6654 * parameters, but independently of the other set dimensions.
6656 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6658 return set_opt_mpa(set
, &isl_set_dim_max
);
6664 #include "isl_opt_mpa_templ.c"
6666 /* Compute the minima of the output dimensions as a function of the
6667 * parameters and input dimensions, but independently of
6668 * the other output dimensions.
6670 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6672 return map_opt_mpa(map
, &isl_map_dim_min
);
6675 /* Compute the maxima of the output dimensions as a function of the
6676 * parameters and input dimensions, but independently of
6677 * the other output dimensions.
6679 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6681 return map_opt_mpa(map
, &isl_map_dim_max
);
6684 /* Scale the elements of "pma" by the corresponding elements of "mv".
6686 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6687 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6690 isl_bool equal_params
;
6692 pma
= isl_pw_multi_aff_cow(pma
);
6695 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6696 mv
->space
, isl_dim_set
))
6697 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6698 "spaces don't match", goto error
);
6699 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6700 if (equal_params
< 0)
6702 if (!equal_params
) {
6703 pma
= isl_pw_multi_aff_align_params(pma
,
6704 isl_multi_val_get_space(mv
));
6705 mv
= isl_multi_val_align_params(mv
,
6706 isl_pw_multi_aff_get_space(pma
));
6711 for (i
= 0; i
< pma
->n
; ++i
) {
6712 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6713 isl_multi_val_copy(mv
));
6714 if (!pma
->p
[i
].maff
)
6718 isl_multi_val_free(mv
);
6721 isl_multi_val_free(mv
);
6722 isl_pw_multi_aff_free(pma
);
6726 /* This function is called for each entry of an isl_union_pw_multi_aff.
6727 * If the space of the entry matches that of data->mv,
6728 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6729 * Otherwise, return an empty isl_pw_multi_aff.
6731 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6732 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6734 isl_multi_val
*mv
= user
;
6738 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6739 mv
->space
, isl_dim_set
)) {
6740 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6741 isl_pw_multi_aff_free(pma
);
6742 return isl_pw_multi_aff_empty(space
);
6745 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6748 /* Scale the elements of "upma" by the corresponding elements of "mv",
6749 * for those entries that match the space of "mv".
6751 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6752 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6754 struct isl_union_pw_multi_aff_transform_control control
= {
6755 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6759 upma
= isl_union_pw_multi_aff_align_params(upma
,
6760 isl_multi_val_get_space(mv
));
6761 mv
= isl_multi_val_align_params(mv
,
6762 isl_union_pw_multi_aff_get_space(upma
));
6766 return isl_union_pw_multi_aff_transform(upma
, &control
);
6768 isl_multi_val_free(mv
);
6771 isl_multi_val_free(mv
);
6772 isl_union_pw_multi_aff_free(upma
);
6776 /* Construct and return a piecewise multi affine expression
6777 * in the given space with value zero in each of the output dimensions and
6778 * a universe domain.
6780 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6782 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6785 /* Construct and return a piecewise multi affine expression
6786 * that is equal to the given piecewise affine expression.
6788 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6789 __isl_take isl_pw_aff
*pa
)
6793 isl_pw_multi_aff
*pma
;
6798 space
= isl_pw_aff_get_space(pa
);
6799 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6801 for (i
= 0; i
< pa
->n
; ++i
) {
6805 set
= isl_set_copy(pa
->p
[i
].set
);
6806 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6807 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6810 isl_pw_aff_free(pa
);
6814 /* Construct and return a piecewise multi affine expression
6815 * that is equal to the given multi piecewise affine expression
6816 * on the shared domain of the piecewise affine expressions,
6817 * in the special case of a 0D multi piecewise affine expression.
6819 * Create a piecewise multi affine expression with the explicit domain of
6820 * the 0D multi piecewise affine expression as domain.
6822 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6823 __isl_take isl_multi_pw_aff
*mpa
)
6829 space
= isl_multi_pw_aff_get_space(mpa
);
6830 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6831 isl_multi_pw_aff_free(mpa
);
6833 ma
= isl_multi_aff_zero(space
);
6834 return isl_pw_multi_aff_alloc(dom
, ma
);
6837 /* Construct and return a piecewise multi affine expression
6838 * that is equal to the given multi piecewise affine expression
6839 * on the shared domain of the piecewise affine expressions.
6841 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6842 __isl_take isl_multi_pw_aff
*mpa
)
6847 isl_pw_multi_aff
*pma
;
6853 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6855 space
= isl_multi_pw_aff_get_space(mpa
);
6856 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6857 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6859 for (i
= 1; i
< mpa
->n
; ++i
) {
6860 isl_pw_multi_aff
*pma_i
;
6862 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6863 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6864 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6867 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6869 isl_multi_pw_aff_free(mpa
);
6873 /* Convenience function that constructs an isl_multi_pw_aff
6874 * directly from an isl_aff.
6876 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
6878 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
6881 /* Construct and return a multi piecewise affine expression
6882 * that is equal to the given multi affine expression.
6884 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6885 __isl_take isl_multi_aff
*ma
)
6889 isl_multi_pw_aff
*mpa
;
6891 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6893 ma
= isl_multi_aff_free(ma
);
6897 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6899 for (i
= 0; i
< n
; ++i
) {
6902 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6903 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6906 isl_multi_aff_free(ma
);
6910 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6911 * but is considered as a function on an isl_multi_aff when exported.
6913 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
6914 __isl_take isl_multi_aff
*ma
)
6916 return isl_multi_pw_aff_from_multi_aff(ma
);
6919 /* Construct and return a multi piecewise affine expression
6920 * that is equal to the given piecewise multi affine expression.
6922 * If the resulting multi piecewise affine expression has
6923 * an explicit domain, then assign it the domain of the input.
6924 * In other cases, the domain is stored in the individual elements.
6926 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6927 __isl_take isl_pw_multi_aff
*pma
)
6932 isl_multi_pw_aff
*mpa
;
6934 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6936 pma
= isl_pw_multi_aff_free(pma
);
6937 space
= isl_pw_multi_aff_get_space(pma
);
6938 mpa
= isl_multi_pw_aff_alloc(space
);
6940 for (i
= 0; i
< n
; ++i
) {
6943 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6944 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6946 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6949 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6950 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6953 isl_pw_multi_aff_free(pma
);
6957 /* This function performs the same operation as
6958 * isl_multi_pw_aff_from_pw_multi_aff,
6959 * but is considered as a function on an isl_pw_multi_aff when exported.
6961 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
6962 __isl_take isl_pw_multi_aff
*pma
)
6964 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
6967 /* Do "pa1" and "pa2" represent the same function?
6969 * We first check if they are obviously equal.
6970 * If not, we convert them to maps and check if those are equal.
6972 * If "pa1" or "pa2" contain any NaNs, then they are considered
6973 * not to be the same. A NaN is not equal to anything, not even
6976 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
6977 __isl_keep isl_pw_aff
*pa2
)
6981 isl_map
*map1
, *map2
;
6984 return isl_bool_error
;
6986 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6987 if (equal
< 0 || equal
)
6989 has_nan
= either_involves_nan(pa1
, pa2
);
6991 return isl_bool_error
;
6993 return isl_bool_false
;
6995 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
6996 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
6997 equal
= isl_map_is_equal(map1
, map2
);
7004 /* Do "mpa1" and "mpa2" represent the same function?
7006 * Note that we cannot convert the entire isl_multi_pw_aff
7007 * to a map because the domains of the piecewise affine expressions
7008 * may not be the same.
7010 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7011 __isl_keep isl_multi_pw_aff
*mpa2
)
7014 isl_bool equal
, equal_params
;
7017 return isl_bool_error
;
7019 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7020 if (equal_params
< 0)
7021 return isl_bool_error
;
7022 if (!equal_params
) {
7023 if (!isl_space_has_named_params(mpa1
->space
))
7024 return isl_bool_false
;
7025 if (!isl_space_has_named_params(mpa2
->space
))
7026 return isl_bool_false
;
7027 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7028 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7029 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7030 isl_multi_pw_aff_get_space(mpa2
));
7031 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7032 isl_multi_pw_aff_get_space(mpa1
));
7033 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7034 isl_multi_pw_aff_free(mpa1
);
7035 isl_multi_pw_aff_free(mpa2
);
7039 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7040 if (equal
< 0 || !equal
)
7043 for (i
= 0; i
< mpa1
->n
; ++i
) {
7044 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7045 if (equal
< 0 || !equal
)
7049 return isl_bool_true
;
7052 /* Do "pma1" and "pma2" represent the same function?
7054 * First check if they are obviously equal.
7055 * If not, then convert them to maps and check if those are equal.
7057 * If "pa1" or "pa2" contain any NaNs, then they are considered
7058 * not to be the same. A NaN is not equal to anything, not even
7061 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7062 __isl_keep isl_pw_multi_aff
*pma2
)
7066 isl_map
*map1
, *map2
;
7069 return isl_bool_error
;
7071 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7072 if (equal
< 0 || equal
)
7074 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7075 if (has_nan
>= 0 && !has_nan
)
7076 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7077 if (has_nan
< 0 || has_nan
)
7078 return isl_bool_not(has_nan
);
7080 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7081 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7082 equal
= isl_map_is_equal(map1
, map2
);
7089 /* Compute the pullback of "mpa" by the function represented by "ma".
7090 * In other words, plug in "ma" in "mpa".
7092 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7094 * If "mpa" has an explicit domain, then it is this domain
7095 * that needs to undergo a pullback, i.e., a preimage.
7097 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
7098 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7101 isl_space
*space
= NULL
;
7103 mpa
= isl_multi_pw_aff_cow(mpa
);
7107 space
= isl_space_join(isl_multi_aff_get_space(ma
),
7108 isl_multi_pw_aff_get_space(mpa
));
7112 for (i
= 0; i
< mpa
->n
; ++i
) {
7113 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
7114 isl_multi_aff_copy(ma
));
7118 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7119 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
7120 isl_multi_aff_copy(ma
));
7125 isl_multi_aff_free(ma
);
7126 isl_space_free(mpa
->space
);
7130 isl_space_free(space
);
7131 isl_multi_pw_aff_free(mpa
);
7132 isl_multi_aff_free(ma
);
7136 /* Compute the pullback of "mpa" by the function represented by "ma".
7137 * In other words, plug in "ma" in "mpa".
7139 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
7140 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7142 isl_bool equal_params
;
7146 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
7147 if (equal_params
< 0)
7150 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7151 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
7152 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
7153 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7155 isl_multi_pw_aff_free(mpa
);
7156 isl_multi_aff_free(ma
);
7160 /* Compute the pullback of "mpa" by the function represented by "pma".
7161 * In other words, plug in "pma" in "mpa".
7163 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7165 * If "mpa" has an explicit domain, then it is this domain
7166 * that needs to undergo a pullback, i.e., a preimage.
7168 static __isl_give isl_multi_pw_aff
*
7169 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7170 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7173 isl_space
*space
= NULL
;
7175 mpa
= isl_multi_pw_aff_cow(mpa
);
7179 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
7180 isl_multi_pw_aff_get_space(mpa
));
7182 for (i
= 0; i
< mpa
->n
; ++i
) {
7183 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7184 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
7188 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7189 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
7190 isl_pw_multi_aff_copy(pma
));
7195 isl_pw_multi_aff_free(pma
);
7196 isl_space_free(mpa
->space
);
7200 isl_space_free(space
);
7201 isl_multi_pw_aff_free(mpa
);
7202 isl_pw_multi_aff_free(pma
);
7206 /* Compute the pullback of "mpa" by the function represented by "pma".
7207 * In other words, plug in "pma" in "mpa".
7209 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
7210 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7212 isl_bool equal_params
;
7216 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
7217 if (equal_params
< 0)
7220 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7221 mpa
= isl_multi_pw_aff_align_params(mpa
,
7222 isl_pw_multi_aff_get_space(pma
));
7223 pma
= isl_pw_multi_aff_align_params(pma
,
7224 isl_multi_pw_aff_get_space(mpa
));
7225 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7227 isl_multi_pw_aff_free(mpa
);
7228 isl_pw_multi_aff_free(pma
);
7232 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7233 * with the domain of "aff". The domain of the result is the same
7235 * "mpa" and "aff" are assumed to have been aligned.
7237 * We first extract the parametric constant from "aff", defined
7238 * over the correct domain.
7239 * Then we add the appropriate combinations of the members of "mpa".
7240 * Finally, we add the integer divisions through recursive calls.
7242 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7243 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7246 isl_size n_in
, n_div
, n_mpa_in
;
7252 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7253 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7254 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7255 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7258 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7259 tmp
= isl_aff_copy(aff
);
7260 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7261 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7262 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7263 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7264 pa
= isl_pw_aff_from_aff(tmp
);
7266 for (i
= 0; i
< n_in
; ++i
) {
7269 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7271 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7272 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7273 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7274 pa
= isl_pw_aff_add(pa
, pa_i
);
7277 for (i
= 0; i
< n_div
; ++i
) {
7281 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7283 div
= isl_aff_get_div(aff
, i
);
7284 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7285 isl_multi_pw_aff_copy(mpa
), div
);
7286 pa_i
= isl_pw_aff_floor(pa_i
);
7287 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7288 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7289 pa
= isl_pw_aff_add(pa
, pa_i
);
7292 isl_multi_pw_aff_free(mpa
);
7297 isl_multi_pw_aff_free(mpa
);
7302 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7303 * with the domain of "aff". The domain of the result is the same
7306 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7307 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7309 isl_bool equal_params
;
7313 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7314 if (equal_params
< 0)
7317 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7319 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7320 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7322 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7325 isl_multi_pw_aff_free(mpa
);
7329 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7330 * with the domain of "pa". The domain of the result is the same
7332 * "mpa" and "pa" are assumed to have been aligned.
7334 * We consider each piece in turn. Note that the domains of the
7335 * pieces are assumed to be disjoint and they remain disjoint
7336 * after taking the preimage (over the same function).
7338 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7339 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7348 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7349 isl_pw_aff_get_space(pa
));
7350 res
= isl_pw_aff_empty(space
);
7352 for (i
= 0; i
< pa
->n
; ++i
) {
7356 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7357 isl_multi_pw_aff_copy(mpa
),
7358 isl_aff_copy(pa
->p
[i
].aff
));
7359 domain
= isl_set_copy(pa
->p
[i
].set
);
7360 domain
= isl_set_preimage_multi_pw_aff(domain
,
7361 isl_multi_pw_aff_copy(mpa
));
7362 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7363 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7366 isl_pw_aff_free(pa
);
7367 isl_multi_pw_aff_free(mpa
);
7370 isl_pw_aff_free(pa
);
7371 isl_multi_pw_aff_free(mpa
);
7375 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7376 * with the domain of "pa". The domain of the result is the same
7379 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7380 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7382 isl_bool equal_params
;
7386 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7387 if (equal_params
< 0)
7390 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7392 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7393 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7395 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7397 isl_pw_aff_free(pa
);
7398 isl_multi_pw_aff_free(mpa
);
7402 /* Compute the pullback of "pa" by the function represented by "mpa".
7403 * In other words, plug in "mpa" in "pa".
7404 * "pa" and "mpa" are assumed to have been aligned.
7406 * The pullback is computed by applying "pa" to "mpa".
7408 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7409 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7411 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7414 /* Compute the pullback of "pa" by the function represented by "mpa".
7415 * In other words, plug in "mpa" in "pa".
7417 * The pullback is computed by applying "pa" to "mpa".
7419 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7420 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7422 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7425 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7426 * In other words, plug in "mpa2" in "mpa1".
7428 * We pullback each member of "mpa1" in turn.
7430 * If "mpa1" has an explicit domain, then it is this domain
7431 * that needs to undergo a pullback instead, i.e., a preimage.
7433 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7434 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7437 isl_space
*space
= NULL
;
7439 isl_multi_pw_aff_align_params_bin(&mpa1
, &mpa2
);
7440 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7444 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7445 isl_multi_pw_aff_get_space(mpa1
));
7447 for (i
= 0; i
< mpa1
->n
; ++i
) {
7448 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7449 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7454 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7455 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7456 isl_multi_pw_aff_copy(mpa2
));
7460 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7462 isl_multi_pw_aff_free(mpa2
);
7465 isl_space_free(space
);
7466 isl_multi_pw_aff_free(mpa1
);
7467 isl_multi_pw_aff_free(mpa2
);
7471 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7472 * of "mpa1" and "mpa2" live in the same space, construct map space
7473 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7474 * with this map space as extract argument.
7476 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7477 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7478 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7479 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7482 isl_space
*space1
, *space2
;
7485 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7486 isl_multi_pw_aff_get_space(mpa2
));
7487 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7488 isl_multi_pw_aff_get_space(mpa1
));
7491 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7492 mpa2
->space
, isl_dim_out
);
7496 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7497 "range spaces don't match", goto error
);
7498 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7499 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7500 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7502 res
= order(mpa1
, mpa2
, space1
);
7503 isl_multi_pw_aff_free(mpa1
);
7504 isl_multi_pw_aff_free(mpa2
);
7507 isl_multi_pw_aff_free(mpa1
);
7508 isl_multi_pw_aff_free(mpa2
);
7512 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7513 * where the function values are equal. "space" is the space of the result.
7514 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7516 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7517 * in the sequences are equal.
7519 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7520 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7521 __isl_take isl_space
*space
)
7527 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7529 space
= isl_space_free(space
);
7530 res
= isl_map_universe(space
);
7532 for (i
= 0; i
< n
; ++i
) {
7533 isl_pw_aff
*pa1
, *pa2
;
7536 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7537 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7538 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7539 res
= isl_map_intersect(res
, map
);
7545 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7546 * where the function values are equal.
7548 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7549 __isl_take isl_multi_pw_aff
*mpa2
)
7551 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7552 &isl_multi_pw_aff_eq_map_on_space
);
7555 /* Intersect "map" with the result of applying "order"
7556 * on two copies of "mpa".
7558 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7559 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7560 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7561 __isl_take isl_multi_pw_aff
*mpa2
))
7563 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7566 /* Return the subset of "map" where the domain and the range
7567 * have equal "mpa" values.
7569 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7570 __isl_take isl_multi_pw_aff
*mpa
)
7572 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7573 &isl_multi_pw_aff_eq_map
);
7576 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7577 * where the function values of "mpa1" lexicographically satisfies
7578 * "strict_base"/"base" compared to that of "mpa2".
7579 * "space" is the space of the result.
7580 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7582 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7583 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7584 * when compared to the i-th element of "mpa2" while all previous elements are
7586 * In particular, if i corresponds to the final elements
7587 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7588 * for other values of i.
7589 * If "base" is a strict order, then "base" and "strict_base" are the same.
7591 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7592 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7593 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7594 __isl_take isl_pw_aff
*pa2
),
7595 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7596 __isl_take isl_pw_aff
*pa2
),
7597 __isl_take isl_space
*space
)
7601 isl_map
*res
, *rest
;
7603 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7605 space
= isl_space_free(space
);
7606 res
= isl_map_empty(isl_space_copy(space
));
7607 rest
= isl_map_universe(space
);
7609 for (i
= 0; i
< n
; ++i
) {
7611 isl_pw_aff
*pa1
, *pa2
;
7616 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7617 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7618 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7619 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7620 res
= isl_map_union(res
, map
);
7625 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7626 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7627 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7628 rest
= isl_map_intersect(rest
, map
);
7638 #define STRICT_ORDER lt
7639 #include "isl_aff_lex_templ.c"
7644 #define STRICT_ORDER lt
7645 #include "isl_aff_lex_templ.c"
7650 #define STRICT_ORDER gt
7651 #include "isl_aff_lex_templ.c"
7656 #define STRICT_ORDER gt
7657 #include "isl_aff_lex_templ.c"
7659 /* Compare two isl_affs.
7661 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7662 * than "aff2" and 0 if they are equal.
7664 * The order is fairly arbitrary. We do consider expressions that only involve
7665 * earlier dimensions as "smaller".
7667 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7680 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7684 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7685 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7687 return last1
- last2
;
7689 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7692 /* Compare two isl_pw_affs.
7694 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7695 * than "pa2" and 0 if they are equal.
7697 * The order is fairly arbitrary. We do consider expressions that only involve
7698 * earlier dimensions as "smaller".
7700 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7701 __isl_keep isl_pw_aff
*pa2
)
7714 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7718 if (pa1
->n
!= pa2
->n
)
7719 return pa1
->n
- pa2
->n
;
7721 for (i
= 0; i
< pa1
->n
; ++i
) {
7722 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7725 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7733 /* Return a piecewise affine expression that is equal to "v" on "domain".
7735 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7736 __isl_take isl_val
*v
)
7739 isl_local_space
*ls
;
7742 space
= isl_set_get_space(domain
);
7743 ls
= isl_local_space_from_space(space
);
7744 aff
= isl_aff_val_on_domain(ls
, v
);
7746 return isl_pw_aff_alloc(domain
, aff
);
7749 /* Return a piecewise affine expression that is equal to the parameter
7750 * with identifier "id" on "domain".
7752 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7753 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7758 space
= isl_set_get_space(domain
);
7759 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7760 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7761 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7763 return isl_pw_aff_alloc(domain
, aff
);
7766 /* Return a multi affine expression that is equal to "mv" on domain
7769 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7770 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7775 isl_local_space
*ls
;
7778 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7779 if (!space
|| n
< 0)
7782 space2
= isl_multi_val_get_space(mv
);
7783 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7784 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7785 space
= isl_space_map_from_domain_and_range(space
, space2
);
7786 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7787 ls
= isl_local_space_from_space(isl_space_domain(space
));
7788 for (i
= 0; i
< n
; ++i
) {
7792 v
= isl_multi_val_get_val(mv
, i
);
7793 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7794 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7796 isl_local_space_free(ls
);
7798 isl_multi_val_free(mv
);
7801 isl_space_free(space
);
7802 isl_multi_val_free(mv
);
7806 /* This is an alternative name for the function above.
7808 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7809 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7811 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7814 /* This function performs the same operation as
7815 * isl_multi_aff_multi_val_on_domain_space,
7816 * but is considered as a function on an isl_space when exported.
7818 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7819 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7821 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7824 /* Return a piecewise multi-affine expression
7825 * that is equal to "mv" on "domain".
7827 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7828 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7833 space
= isl_set_get_space(domain
);
7834 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7836 return isl_pw_multi_aff_alloc(domain
, ma
);
7839 /* This function performs the same operation as
7840 * isl_pw_multi_aff_multi_val_on_domain,
7841 * but is considered as a function on an isl_set when exported.
7843 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7844 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7846 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7849 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7850 * mv is the value that should be attained on each domain set
7851 * res collects the results
7853 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7855 isl_union_pw_multi_aff
*res
;
7858 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7859 * and add it to data->res.
7861 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7864 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7865 isl_pw_multi_aff
*pma
;
7868 mv
= isl_multi_val_copy(data
->mv
);
7869 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7870 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7872 return data
->res
? isl_stat_ok
: isl_stat_error
;
7875 /* Return a union piecewise multi-affine expression
7876 * that is equal to "mv" on "domain".
7878 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7879 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7881 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7884 space
= isl_union_set_get_space(domain
);
7885 data
.res
= isl_union_pw_multi_aff_empty(space
);
7887 if (isl_union_set_foreach_set(domain
,
7888 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7889 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7890 isl_union_set_free(domain
);
7891 isl_multi_val_free(mv
);
7895 /* Compute the pullback of data->pma by the function represented by "pma2",
7896 * provided the spaces match, and add the results to data->res.
7898 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7900 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7902 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7903 pma2
->dim
, isl_dim_out
)) {
7904 isl_pw_multi_aff_free(pma2
);
7908 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7909 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7911 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7913 return isl_stat_error
;
7918 /* Compute the pullback of "upma1" by the function represented by "upma2".
7920 __isl_give isl_union_pw_multi_aff
*
7921 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7922 __isl_take isl_union_pw_multi_aff
*upma1
,
7923 __isl_take isl_union_pw_multi_aff
*upma2
)
7925 return bin_op(upma1
, upma2
, &pullback_entry
);
7928 /* Apply "upma2" to "upma1".
7930 * That is, compute the pullback of "upma2" by "upma1".
7932 __isl_give isl_union_pw_multi_aff
*
7933 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7934 __isl_take isl_union_pw_multi_aff
*upma1
,
7935 __isl_take isl_union_pw_multi_aff
*upma2
)
7937 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7941 #define TYPE isl_pw_multi_aff
7943 #include "isl_copy_tuple_id_templ.c"
7945 /* Given a function "pma1" of the form A[B -> C] -> D and
7946 * a function "pma2" of the form E -> B,
7947 * replace the domain of the wrapped relation inside the domain of "pma1"
7948 * by the preimage with respect to "pma2".
7949 * In other words, plug in "pma2" in this nested domain.
7950 * The result is of the form A[E -> C] -> D.
7952 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7953 * plug that into "pma1".
7955 __isl_give isl_pw_multi_aff
*
7956 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7957 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
7959 isl_space
*pma1_space
, *pma2_space
;
7961 isl_pw_multi_aff
*id
;
7963 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
7964 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7966 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
7968 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
7969 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
7972 space
= isl_space_domain(isl_space_copy(pma1_space
));
7973 space
= isl_space_range(isl_space_unwrap(space
));
7974 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
7975 pma2
= isl_pw_multi_aff_product(pma2
, id
);
7977 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
7978 pma1_space
, isl_dim_in
);
7979 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
7980 pma1_space
, isl_dim_in
);
7982 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
7984 isl_pw_multi_aff_free(pma1
);
7985 isl_pw_multi_aff_free(pma2
);
7989 /* If data->pma and "pma2" are such that
7990 * data->pma is of the form A[B -> C] -> D and
7991 * "pma2" is of the form E -> B,
7992 * then replace the domain of the wrapped relation
7993 * inside the domain of data->pma by the preimage with respect to "pma2" and
7994 * add the result to data->res.
7996 static isl_stat
preimage_domain_wrapped_domain_entry(
7997 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
7999 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8000 isl_space
*pma1_space
, *pma2_space
;
8003 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8004 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8006 match
= isl_space_domain_is_wrapping(pma1_space
);
8007 if (match
>= 0 && match
)
8008 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8009 isl_dim_in
, pma2_space
, isl_dim_out
);
8010 if (match
< 0 || !match
) {
8011 isl_pw_multi_aff_free(pma2
);
8012 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8015 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8016 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8018 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8020 return isl_stat_non_null(data
->res
);
8023 /* For each pair of functions A[B -> C] -> D in "upma1" and
8024 * E -> B in "upma2",
8025 * replace the domain of the wrapped relation inside the domain of the first
8026 * by the preimage with respect to the second and collect the results.
8027 * In other words, plug in the second function in this nested domain.
8028 * The results are of the form A[E -> C] -> D.
8030 __isl_give isl_union_pw_multi_aff
*
8031 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8032 __isl_take isl_union_pw_multi_aff
*upma1
,
8033 __isl_take isl_union_pw_multi_aff
*upma2
)
8035 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8038 /* Check that the domain space of "upa" matches "space".
8040 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8041 * can in principle never fail since the space "space" is that
8042 * of the isl_multi_union_pw_aff and is a set space such that
8043 * there is no domain space to match.
8045 * We check the parameters and double-check that "space" is
8046 * indeed that of a set.
8048 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8049 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8051 isl_space
*upa_space
;
8055 return isl_stat_error
;
8057 match
= isl_space_is_set(space
);
8059 return isl_stat_error
;
8061 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8062 "expecting set space", return isl_stat_error
);
8064 upa_space
= isl_union_pw_aff_get_space(upa
);
8065 match
= isl_space_has_equal_params(space
, upa_space
);
8069 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8070 "parameters don't match", goto error
);
8072 isl_space_free(upa_space
);
8075 isl_space_free(upa_space
);
8076 return isl_stat_error
;
8079 /* Do the parameters of "upa" match those of "space"?
8081 static isl_bool
isl_union_pw_aff_matching_params(
8082 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8084 isl_space
*upa_space
;
8088 return isl_bool_error
;
8090 upa_space
= isl_union_pw_aff_get_space(upa
);
8092 match
= isl_space_has_equal_params(space
, upa_space
);
8094 isl_space_free(upa_space
);
8098 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8099 * space represents the new parameters.
8100 * res collects the results.
8102 struct isl_union_pw_aff_reset_params_data
{
8104 isl_union_pw_aff
*res
;
8107 /* Replace the parameters of "pa" by data->space and
8108 * add the result to data->res.
8110 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8112 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8115 space
= isl_pw_aff_get_space(pa
);
8116 space
= isl_space_replace_params(space
, data
->space
);
8117 pa
= isl_pw_aff_reset_space(pa
, space
);
8118 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8120 return data
->res
? isl_stat_ok
: isl_stat_error
;
8123 /* Replace the domain space of "upa" by "space".
8124 * Since a union expression does not have a (single) domain space,
8125 * "space" is necessarily a parameter space.
8127 * Since the order and the names of the parameters determine
8128 * the hash value, we need to create a new hash table.
8130 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8131 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8133 struct isl_union_pw_aff_reset_params_data data
= { space
};
8136 match
= isl_union_pw_aff_matching_params(upa
, space
);
8138 upa
= isl_union_pw_aff_free(upa
);
8140 isl_space_free(space
);
8144 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8145 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8146 data
.res
= isl_union_pw_aff_free(data
.res
);
8148 isl_union_pw_aff_free(upa
);
8149 isl_space_free(space
);
8153 /* Return the floor of "pa".
8155 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8157 return isl_pw_aff_floor(pa
);
8160 /* Given f, return floor(f).
8162 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8163 __isl_take isl_union_pw_aff
*upa
)
8165 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8170 * upa mod m = upa - m * floor(upa/m)
8172 * with m an integer value.
8174 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8175 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8177 isl_union_pw_aff
*res
;
8182 if (!isl_val_is_int(m
))
8183 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8184 "expecting integer modulo", goto error
);
8185 if (!isl_val_is_pos(m
))
8186 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8187 "expecting positive modulo", goto error
);
8189 res
= isl_union_pw_aff_copy(upa
);
8190 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8191 upa
= isl_union_pw_aff_floor(upa
);
8192 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8193 res
= isl_union_pw_aff_sub(res
, upa
);
8198 isl_union_pw_aff_free(upa
);
8202 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8203 * pos is the output position that needs to be extracted.
8204 * res collects the results.
8206 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8208 isl_union_pw_aff
*res
;
8211 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8212 * (assuming it has such a dimension) and add it to data->res.
8214 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8216 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8220 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8222 return isl_stat_error
;
8223 if (data
->pos
>= n_out
) {
8224 isl_pw_multi_aff_free(pma
);
8228 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8229 isl_pw_multi_aff_free(pma
);
8231 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8233 return data
->res
? isl_stat_ok
: isl_stat_error
;
8236 /* Extract an isl_union_pw_aff corresponding to
8237 * output dimension "pos" of "upma".
8239 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8240 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8242 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8249 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8250 "cannot extract at negative position", return NULL
);
8252 space
= isl_union_pw_multi_aff_get_space(upma
);
8253 data
.res
= isl_union_pw_aff_empty(space
);
8255 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8256 &get_union_pw_aff
, &data
) < 0)
8257 data
.res
= isl_union_pw_aff_free(data
.res
);
8262 /* Return a union piecewise affine expression
8263 * that is equal to "aff" on "domain".
8265 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8266 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8270 pa
= isl_pw_aff_from_aff(aff
);
8271 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8274 /* Return a union piecewise affine expression
8275 * that is equal to the parameter identified by "id" on "domain".
8277 * Make sure the parameter appears in the space passed to
8278 * isl_aff_param_on_domain_space_id.
8280 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8281 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8286 space
= isl_union_set_get_space(domain
);
8287 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8288 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8289 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8292 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8293 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8295 * "res" collects the results.
8297 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8299 isl_union_pw_aff
*res
;
8302 /* Construct a piecewise affine expression that is equal to data->pa
8303 * on "domain" and add the result to data->res.
8305 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8307 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8311 pa
= isl_pw_aff_copy(data
->pa
);
8312 dim
= isl_set_dim(domain
, isl_dim_set
);
8314 pa
= isl_pw_aff_free(pa
);
8315 pa
= isl_pw_aff_from_range(pa
);
8316 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8317 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8318 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8319 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8321 return data
->res
? isl_stat_ok
: isl_stat_error
;
8324 /* Return a union piecewise affine expression
8325 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8326 * have been aligned.
8328 * Construct an isl_pw_aff on each of the sets in "domain" and
8329 * collect the results.
8331 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8332 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8334 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8337 space
= isl_union_set_get_space(domain
);
8338 data
.res
= isl_union_pw_aff_empty(space
);
8340 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8341 data
.res
= isl_union_pw_aff_free(data
.res
);
8342 isl_union_set_free(domain
);
8343 isl_pw_aff_free(pa
);
8347 /* Return a union piecewise affine expression
8348 * that is equal to "pa" on "domain".
8350 * Check that "pa" is a parametric expression,
8351 * align the parameters if needed and call
8352 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8354 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8355 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8358 isl_bool equal_params
;
8359 isl_space
*domain_space
, *pa_space
;
8361 pa_space
= isl_pw_aff_peek_space(pa
);
8362 is_set
= isl_space_is_set(pa_space
);
8366 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8367 "expecting parametric expression", goto error
);
8369 domain_space
= isl_union_set_get_space(domain
);
8370 pa_space
= isl_pw_aff_get_space(pa
);
8371 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8372 if (equal_params
>= 0 && !equal_params
) {
8375 space
= isl_space_align_params(domain_space
, pa_space
);
8376 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8377 domain
= isl_union_set_align_params(domain
, space
);
8379 isl_space_free(domain_space
);
8380 isl_space_free(pa_space
);
8383 if (equal_params
< 0)
8385 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8387 isl_union_set_free(domain
);
8388 isl_pw_aff_free(pa
);
8392 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8393 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8394 * "res" collects the results.
8396 struct isl_union_pw_aff_val_on_domain_data
{
8398 isl_union_pw_aff
*res
;
8401 /* Construct a piecewise affine expression that is equal to data->v
8402 * on "domain" and add the result to data->res.
8404 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8406 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8410 v
= isl_val_copy(data
->v
);
8411 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8412 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8414 return data
->res
? isl_stat_ok
: isl_stat_error
;
8417 /* Return a union piecewise affine expression
8418 * that is equal to "v" on "domain".
8420 * Construct an isl_pw_aff on each of the sets in "domain" and
8421 * collect the results.
8423 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8424 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8426 struct isl_union_pw_aff_val_on_domain_data data
;
8429 space
= isl_union_set_get_space(domain
);
8430 data
.res
= isl_union_pw_aff_empty(space
);
8432 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8433 data
.res
= isl_union_pw_aff_free(data
.res
);
8434 isl_union_set_free(domain
);
8439 /* Construct a piecewise multi affine expression
8440 * that is equal to "pa" and add it to upma.
8442 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8445 isl_union_pw_multi_aff
**upma
= user
;
8446 isl_pw_multi_aff
*pma
;
8448 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8449 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8451 return *upma
? isl_stat_ok
: isl_stat_error
;
8454 /* Construct and return a union piecewise multi affine expression
8455 * that is equal to the given union piecewise affine expression.
8457 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8458 __isl_take isl_union_pw_aff
*upa
)
8461 isl_union_pw_multi_aff
*upma
;
8466 space
= isl_union_pw_aff_get_space(upa
);
8467 upma
= isl_union_pw_multi_aff_empty(space
);
8469 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8470 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8471 upma
= isl_union_pw_multi_aff_free(upma
);
8473 isl_union_pw_aff_free(upa
);
8477 /* Compute the set of elements in the domain of "pa" where it is zero and
8478 * add this set to "uset".
8480 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8482 isl_union_set
**uset
= (isl_union_set
**)user
;
8484 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8486 return *uset
? isl_stat_ok
: isl_stat_error
;
8489 /* Return a union set containing those elements in the domain
8490 * of "upa" where it is zero.
8492 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8493 __isl_take isl_union_pw_aff
*upa
)
8495 isl_union_set
*zero
;
8497 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8498 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8499 zero
= isl_union_set_free(zero
);
8501 isl_union_pw_aff_free(upa
);
8505 /* Internal data structure for isl_union_pw_aff_bind_id,
8506 * storing the parameter that needs to be bound and
8507 * the accumulated results.
8509 struct isl_bind_id_data
{
8511 isl_union_set
*bound
;
8514 /* Bind the piecewise affine function "pa" to the parameter data->id,
8515 * adding the resulting elements in the domain where the expression
8516 * is equal to the parameter to data->bound.
8518 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8520 struct isl_bind_id_data
*data
= user
;
8523 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8524 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8526 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8529 /* Bind the union piecewise affine function "upa" to the parameter "id",
8530 * returning the elements in the domain where the expression
8531 * is equal to the parameter.
8533 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8534 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8536 struct isl_bind_id_data data
= { id
};
8538 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8539 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8540 data
.bound
= isl_union_set_free(data
.bound
);
8542 isl_union_pw_aff_free(upa
);
8547 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8548 * upma is the function that is plugged in.
8549 * pa is the current part of the function in which upma is plugged in.
8550 * res collects the results.
8552 struct isl_union_pw_aff_pullback_upma_data
{
8553 isl_union_pw_multi_aff
*upma
;
8555 isl_union_pw_aff
*res
;
8558 /* Check if "pma" can be plugged into data->pa.
8559 * If so, perform the pullback and add the result to data->res.
8561 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8563 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8566 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8567 pma
->dim
, isl_dim_out
)) {
8568 isl_pw_multi_aff_free(pma
);
8572 pa
= isl_pw_aff_copy(data
->pa
);
8573 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8575 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8577 return data
->res
? isl_stat_ok
: isl_stat_error
;
8580 /* Check if any of the elements of data->upma can be plugged into pa,
8581 * add if so add the result to data->res.
8583 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8585 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8589 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8591 isl_pw_aff_free(pa
);
8596 /* Compute the pullback of "upa" by the function represented by "upma".
8597 * In other words, plug in "upma" in "upa". The result contains
8598 * expressions defined over the domain space of "upma".
8600 * Run over all pairs of elements in "upa" and "upma", perform
8601 * the pullback when appropriate and collect the results.
8602 * If the hash value were based on the domain space rather than
8603 * the function space, then we could run through all elements
8604 * of "upma" and directly pick out the corresponding element of "upa".
8606 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8607 __isl_take isl_union_pw_aff
*upa
,
8608 __isl_take isl_union_pw_multi_aff
*upma
)
8610 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8613 space
= isl_union_pw_multi_aff_get_space(upma
);
8614 upa
= isl_union_pw_aff_align_params(upa
, space
);
8615 space
= isl_union_pw_aff_get_space(upa
);
8616 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8622 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8623 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8624 data
.res
= isl_union_pw_aff_free(data
.res
);
8626 isl_union_pw_aff_free(upa
);
8627 isl_union_pw_multi_aff_free(upma
);
8630 isl_union_pw_aff_free(upa
);
8631 isl_union_pw_multi_aff_free(upma
);
8636 #define BASE union_pw_aff
8638 #define DOMBASE union_set
8640 #include <isl_multi_explicit_domain.c>
8641 #include <isl_multi_union_pw_aff_explicit_domain.c>
8642 #include <isl_multi_templ.c>
8643 #include <isl_multi_apply_set.c>
8644 #include <isl_multi_apply_union_set.c>
8645 #include <isl_multi_arith_templ.c>
8646 #include <isl_multi_bind_templ.c>
8647 #include <isl_multi_coalesce.c>
8648 #include <isl_multi_dim_id_templ.c>
8649 #include <isl_multi_floor.c>
8650 #include <isl_multi_from_base_templ.c>
8651 #include <isl_multi_gist.c>
8652 #include <isl_multi_align_set.c>
8653 #include <isl_multi_align_union_set.c>
8654 #include <isl_multi_intersect.c>
8655 #include <isl_multi_nan_templ.c>
8656 #include <isl_multi_tuple_id_templ.c>
8657 #include <isl_multi_union_add_templ.c>
8658 #include <isl_multi_zero_space_templ.c>
8660 /* Does "mupa" have a non-trivial explicit domain?
8662 * The explicit domain, if present, is trivial if it represents
8663 * an (obviously) universe parameter set.
8665 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8666 __isl_keep isl_multi_union_pw_aff
*mupa
)
8668 isl_bool is_params
, trivial
;
8672 return isl_bool_error
;
8673 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8674 return isl_bool_false
;
8675 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8676 if (is_params
< 0 || !is_params
)
8677 return isl_bool_not(is_params
);
8678 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8679 trivial
= isl_set_plain_is_universe(set
);
8681 return isl_bool_not(trivial
);
8684 /* Construct a multiple union piecewise affine expression
8685 * in the given space with value zero in each of the output dimensions.
8687 * Since there is no canonical zero value for
8688 * a union piecewise affine expression, we can only construct
8689 * a zero-dimensional "zero" value.
8691 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8692 __isl_take isl_space
*space
)
8700 params
= isl_space_is_params(space
);
8704 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8705 "expecting proper set space", goto error
);
8706 if (!isl_space_is_set(space
))
8707 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8708 "expecting set space", goto error
);
8709 dim
= isl_space_dim(space
, isl_dim_out
);
8713 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8714 "expecting 0D space", goto error
);
8716 return isl_multi_union_pw_aff_alloc(space
);
8718 isl_space_free(space
);
8722 /* Construct and return a multi union piecewise affine expression
8723 * that is equal to the given multi affine expression.
8725 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8726 __isl_take isl_multi_aff
*ma
)
8728 isl_multi_pw_aff
*mpa
;
8730 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8731 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8734 /* This function performs the same operation as
8735 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8736 * isl_multi_aff when exported.
8738 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8739 __isl_take isl_multi_aff
*ma
)
8741 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8744 /* Construct and return a multi union piecewise affine expression
8745 * that is equal to the given multi piecewise affine expression.
8747 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8748 __isl_take isl_multi_pw_aff
*mpa
)
8753 isl_multi_union_pw_aff
*mupa
;
8755 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8757 mpa
= isl_multi_pw_aff_free(mpa
);
8761 space
= isl_multi_pw_aff_get_space(mpa
);
8762 space
= isl_space_range(space
);
8763 mupa
= isl_multi_union_pw_aff_alloc(space
);
8765 for (i
= 0; i
< n
; ++i
) {
8767 isl_union_pw_aff
*upa
;
8769 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8770 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8771 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8774 isl_multi_pw_aff_free(mpa
);
8779 /* Extract the range space of "pma" and assign it to *space.
8780 * If *space has already been set (through a previous call to this function),
8781 * then check that the range space is the same.
8783 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8785 isl_space
**space
= user
;
8786 isl_space
*pma_space
;
8789 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8790 isl_pw_multi_aff_free(pma
);
8793 return isl_stat_error
;
8799 equal
= isl_space_is_equal(pma_space
, *space
);
8800 isl_space_free(pma_space
);
8803 return isl_stat_error
;
8805 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8806 "range spaces not the same", return isl_stat_error
);
8810 /* Construct and return a multi union piecewise affine expression
8811 * that is equal to the given union piecewise multi affine expression.
8813 * In order to be able to perform the conversion, the input
8814 * needs to be non-empty and may only involve a single range space.
8816 * If the resulting multi union piecewise affine expression has
8817 * an explicit domain, then assign it the domain of the input.
8818 * In other cases, the domain is stored in the individual elements.
8820 __isl_give isl_multi_union_pw_aff
*
8821 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8822 __isl_take isl_union_pw_multi_aff
*upma
)
8824 isl_space
*space
= NULL
;
8825 isl_multi_union_pw_aff
*mupa
;
8829 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8833 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8834 "cannot extract range space from empty input",
8836 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8843 n
= isl_space_dim(space
, isl_dim_set
);
8845 space
= isl_space_free(space
);
8846 mupa
= isl_multi_union_pw_aff_alloc(space
);
8848 for (i
= 0; i
< n
; ++i
) {
8849 isl_union_pw_aff
*upa
;
8851 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8852 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8854 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8856 isl_union_pw_multi_aff
*copy
;
8858 copy
= isl_union_pw_multi_aff_copy(upma
);
8859 dom
= isl_union_pw_multi_aff_domain(copy
);
8860 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8863 isl_union_pw_multi_aff_free(upma
);
8866 isl_space_free(space
);
8867 isl_union_pw_multi_aff_free(upma
);
8871 /* This function performs the same operation as
8872 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8873 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8875 __isl_give isl_multi_union_pw_aff
*
8876 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8877 __isl_take isl_union_pw_multi_aff
*upma
)
8879 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8882 /* Try and create an isl_multi_union_pw_aff that is equivalent
8883 * to the given isl_union_map.
8884 * The isl_union_map is required to be single-valued in each space.
8885 * Moreover, it cannot be empty and all range spaces need to be the same.
8886 * Otherwise, an error is produced.
8888 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8889 __isl_take isl_union_map
*umap
)
8891 isl_union_pw_multi_aff
*upma
;
8893 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8894 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8897 /* This function performs the same operation as
8898 * isl_multi_union_pw_aff_from_union_map,
8899 * but is considered as a function on an isl_union_map when exported.
8901 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8902 __isl_take isl_union_map
*umap
)
8904 return isl_multi_union_pw_aff_from_union_map(umap
);
8907 /* Return a multiple union piecewise affine expression
8908 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8909 * have been aligned.
8911 * If the resulting multi union piecewise affine expression has
8912 * an explicit domain, then assign it the input domain.
8913 * In other cases, the domain is stored in the individual elements.
8915 static __isl_give isl_multi_union_pw_aff
*
8916 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8917 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8922 isl_multi_union_pw_aff
*mupa
;
8924 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8925 if (!domain
|| n
< 0)
8928 space
= isl_multi_val_get_space(mv
);
8929 mupa
= isl_multi_union_pw_aff_alloc(space
);
8930 for (i
= 0; i
< n
; ++i
) {
8932 isl_union_pw_aff
*upa
;
8934 v
= isl_multi_val_get_val(mv
, i
);
8935 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8937 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8939 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8940 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8941 isl_union_set_copy(domain
));
8943 isl_union_set_free(domain
);
8944 isl_multi_val_free(mv
);
8947 isl_union_set_free(domain
);
8948 isl_multi_val_free(mv
);
8952 /* Return a multiple union piecewise affine expression
8953 * that is equal to "mv" on "domain".
8955 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8956 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8958 isl_bool equal_params
;
8962 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8963 if (equal_params
< 0)
8966 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8968 domain
= isl_union_set_align_params(domain
,
8969 isl_multi_val_get_space(mv
));
8970 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8971 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8973 isl_union_set_free(domain
);
8974 isl_multi_val_free(mv
);
8978 /* Return a multiple union piecewise affine expression
8979 * that is equal to "ma" on "domain".
8981 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8982 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8984 isl_pw_multi_aff
*pma
;
8986 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8987 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8990 /* Return a multiple union piecewise affine expression
8991 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8992 * have been aligned.
8994 * If the resulting multi union piecewise affine expression has
8995 * an explicit domain, then assign it the input domain.
8996 * In other cases, the domain is stored in the individual elements.
8998 static __isl_give isl_multi_union_pw_aff
*
8999 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9000 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9005 isl_multi_union_pw_aff
*mupa
;
9007 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9008 if (!domain
|| n
< 0)
9010 space
= isl_pw_multi_aff_get_space(pma
);
9011 mupa
= isl_multi_union_pw_aff_alloc(space
);
9012 for (i
= 0; i
< n
; ++i
) {
9014 isl_union_pw_aff
*upa
;
9016 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9017 upa
= isl_union_pw_aff_pw_aff_on_domain(
9018 isl_union_set_copy(domain
), pa
);
9019 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9021 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9022 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9023 isl_union_set_copy(domain
));
9025 isl_union_set_free(domain
);
9026 isl_pw_multi_aff_free(pma
);
9029 isl_union_set_free(domain
);
9030 isl_pw_multi_aff_free(pma
);
9034 /* Return a multiple union piecewise affine expression
9035 * that is equal to "pma" on "domain".
9037 __isl_give isl_multi_union_pw_aff
*
9038 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9039 __isl_take isl_pw_multi_aff
*pma
)
9041 isl_bool equal_params
;
9044 space
= isl_pw_multi_aff_peek_space(pma
);
9045 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9046 if (equal_params
< 0)
9049 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9051 domain
= isl_union_set_align_params(domain
,
9052 isl_pw_multi_aff_get_space(pma
));
9053 pma
= isl_pw_multi_aff_align_params(pma
,
9054 isl_union_set_get_space(domain
));
9055 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9058 isl_union_set_free(domain
);
9059 isl_pw_multi_aff_free(pma
);
9063 /* Return a union set containing those elements in the domains
9064 * of the elements of "mupa" where they are all zero.
9066 * If there are no elements, then simply return the entire domain.
9068 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9069 __isl_take isl_multi_union_pw_aff
*mupa
)
9073 isl_union_pw_aff
*upa
;
9074 isl_union_set
*zero
;
9076 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9078 mupa
= isl_multi_union_pw_aff_free(mupa
);
9083 return isl_multi_union_pw_aff_domain(mupa
);
9085 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9086 zero
= isl_union_pw_aff_zero_union_set(upa
);
9088 for (i
= 1; i
< n
; ++i
) {
9089 isl_union_set
*zero_i
;
9091 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9092 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9094 zero
= isl_union_set_intersect(zero
, zero_i
);
9097 isl_multi_union_pw_aff_free(mupa
);
9101 /* Construct a union map mapping the shared domain
9102 * of the union piecewise affine expressions to the range of "mupa"
9103 * in the special case of a 0D multi union piecewise affine expression.
9105 * Construct a map between the explicit domain of "mupa" and
9107 * Note that this assumes that the domain consists of explicit elements.
9109 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9110 __isl_take isl_multi_union_pw_aff
*mupa
)
9114 isl_union_set
*dom
, *ran
;
9116 space
= isl_multi_union_pw_aff_get_space(mupa
);
9117 dom
= isl_multi_union_pw_aff_domain(mupa
);
9118 ran
= isl_union_set_from_set(isl_set_universe(space
));
9120 is_params
= isl_union_set_is_params(dom
);
9122 dom
= isl_union_set_free(dom
);
9124 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9125 "cannot create union map from expression without "
9126 "explicit domain elements",
9127 dom
= isl_union_set_free(dom
));
9129 return isl_union_map_from_domain_and_range(dom
, ran
);
9132 /* Construct a union map mapping the shared domain
9133 * of the union piecewise affine expressions to the range of "mupa"
9134 * with each dimension in the range equated to the
9135 * corresponding union piecewise affine expression.
9137 * If the input is zero-dimensional, then construct a mapping
9138 * from its explicit domain.
9140 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9141 __isl_take isl_multi_union_pw_aff
*mupa
)
9146 isl_union_map
*umap
;
9147 isl_union_pw_aff
*upa
;
9149 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9151 mupa
= isl_multi_union_pw_aff_free(mupa
);
9156 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9158 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9159 umap
= isl_union_map_from_union_pw_aff(upa
);
9161 for (i
= 1; i
< n
; ++i
) {
9162 isl_union_map
*umap_i
;
9164 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9165 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9166 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9169 space
= isl_multi_union_pw_aff_get_space(mupa
);
9170 umap
= isl_union_map_reset_range_space(umap
, space
);
9172 isl_multi_union_pw_aff_free(mupa
);
9176 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9177 * "range" is the space from which to set the range space.
9178 * "res" collects the results.
9180 struct isl_union_pw_multi_aff_reset_range_space_data
{
9182 isl_union_pw_multi_aff
*res
;
9185 /* Replace the range space of "pma" by the range space of data->range and
9186 * add the result to data->res.
9188 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9190 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9193 space
= isl_pw_multi_aff_get_space(pma
);
9194 space
= isl_space_domain(space
);
9195 space
= isl_space_extend_domain_with_range(space
,
9196 isl_space_copy(data
->range
));
9197 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9198 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9200 return data
->res
? isl_stat_ok
: isl_stat_error
;
9203 /* Replace the range space of all the piecewise affine expressions in "upma" by
9204 * the range space of "space".
9206 * This assumes that all these expressions have the same output dimension.
9208 * Since the spaces of the expressions change, so do their hash values.
9209 * We therefore need to create a new isl_union_pw_multi_aff.
9210 * Note that the hash value is currently computed based on the entire
9211 * space even though there can only be a single expression with a given
9214 static __isl_give isl_union_pw_multi_aff
*
9215 isl_union_pw_multi_aff_reset_range_space(
9216 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9218 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9219 isl_space
*space_upma
;
9221 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9222 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9223 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9224 &reset_range_space
, &data
) < 0)
9225 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9227 isl_space_free(space
);
9228 isl_union_pw_multi_aff_free(upma
);
9232 /* Construct and return a union piecewise multi affine expression
9233 * that is equal to the given multi union piecewise affine expression,
9234 * in the special case of a 0D multi union piecewise affine expression.
9236 * Construct a union piecewise multi affine expression
9237 * on top of the explicit domain of the input.
9239 __isl_give isl_union_pw_multi_aff
*
9240 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9241 __isl_take isl_multi_union_pw_aff
*mupa
)
9245 isl_union_set
*domain
;
9247 space
= isl_multi_union_pw_aff_get_space(mupa
);
9248 mv
= isl_multi_val_zero(space
);
9249 domain
= isl_multi_union_pw_aff_domain(mupa
);
9250 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9253 /* Construct and return a union piecewise multi affine expression
9254 * that is equal to the given multi union piecewise affine expression.
9256 * If the input is zero-dimensional, then
9257 * construct a union piecewise multi affine expression
9258 * on top of the explicit domain of the input.
9260 __isl_give isl_union_pw_multi_aff
*
9261 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9262 __isl_take isl_multi_union_pw_aff
*mupa
)
9267 isl_union_pw_multi_aff
*upma
;
9268 isl_union_pw_aff
*upa
;
9270 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9272 mupa
= isl_multi_union_pw_aff_free(mupa
);
9277 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9279 space
= isl_multi_union_pw_aff_get_space(mupa
);
9280 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9281 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9283 for (i
= 1; i
< n
; ++i
) {
9284 isl_union_pw_multi_aff
*upma_i
;
9286 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9287 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9288 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9291 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9293 isl_multi_union_pw_aff_free(mupa
);
9297 /* Intersect the range of "mupa" with "range",
9298 * in the special case where "mupa" is 0D.
9300 * Intersect the domain of "mupa" with the constraints on the parameters
9303 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9304 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9306 range
= isl_set_params(range
);
9307 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9311 /* Intersect the range of "mupa" with "range".
9312 * That is, keep only those domain elements that have a function value
9315 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9316 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9318 isl_union_pw_multi_aff
*upma
;
9319 isl_union_set
*domain
;
9324 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9325 if (n
< 0 || !range
)
9328 space
= isl_set_get_space(range
);
9329 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9330 space
, isl_dim_set
);
9331 isl_space_free(space
);
9335 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9336 "space don't match", goto error
);
9338 return mupa_intersect_range_0D(mupa
, range
);
9340 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9341 isl_multi_union_pw_aff_copy(mupa
));
9342 domain
= isl_union_set_from_set(range
);
9343 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9344 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9348 isl_multi_union_pw_aff_free(mupa
);
9349 isl_set_free(range
);
9353 /* Return the shared domain of the elements of "mupa",
9354 * in the special case where "mupa" is zero-dimensional.
9356 * Return the explicit domain of "mupa".
9357 * Note that this domain may be a parameter set, either
9358 * because "mupa" is meant to live in a set space or
9359 * because no explicit domain has been set.
9361 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9362 __isl_take isl_multi_union_pw_aff
*mupa
)
9366 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9367 isl_multi_union_pw_aff_free(mupa
);
9372 /* Return the shared domain of the elements of "mupa".
9374 * If "mupa" is zero-dimensional, then return its explicit domain.
9376 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9377 __isl_take isl_multi_union_pw_aff
*mupa
)
9381 isl_union_pw_aff
*upa
;
9384 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9386 mupa
= isl_multi_union_pw_aff_free(mupa
);
9391 return isl_multi_union_pw_aff_domain_0D(mupa
);
9393 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9394 dom
= isl_union_pw_aff_domain(upa
);
9395 for (i
= 1; i
< n
; ++i
) {
9396 isl_union_set
*dom_i
;
9398 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9399 dom_i
= isl_union_pw_aff_domain(upa
);
9400 dom
= isl_union_set_intersect(dom
, dom_i
);
9403 isl_multi_union_pw_aff_free(mupa
);
9407 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9408 * In particular, the spaces have been aligned.
9409 * The result is defined over the shared domain of the elements of "mupa"
9411 * We first extract the parametric constant part of "aff" and
9412 * define that over the shared domain.
9413 * Then we iterate over all input dimensions of "aff" and add the corresponding
9414 * multiples of the elements of "mupa".
9415 * Finally, we consider the integer divisions, calling the function
9416 * recursively to obtain an isl_union_pw_aff corresponding to the
9417 * integer division argument.
9419 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9420 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9423 isl_size n_in
, n_div
;
9424 isl_union_pw_aff
*upa
;
9425 isl_union_set
*uset
;
9429 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9430 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9431 if (n_in
< 0 || n_div
< 0)
9434 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9435 cst
= isl_aff_copy(aff
);
9436 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9437 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9438 cst
= isl_aff_project_domain_on_params(cst
);
9439 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9441 for (i
= 0; i
< n_in
; ++i
) {
9442 isl_union_pw_aff
*upa_i
;
9444 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9446 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9447 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9448 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9449 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9452 for (i
= 0; i
< n_div
; ++i
) {
9454 isl_union_pw_aff
*upa_i
;
9456 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9458 div
= isl_aff_get_div(aff
, i
);
9459 upa_i
= multi_union_pw_aff_apply_aff(
9460 isl_multi_union_pw_aff_copy(mupa
), div
);
9461 upa_i
= isl_union_pw_aff_floor(upa_i
);
9462 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9463 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9464 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9467 isl_multi_union_pw_aff_free(mupa
);
9472 isl_multi_union_pw_aff_free(mupa
);
9477 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9478 * with the domain of "aff".
9479 * Furthermore, the dimension of this space needs to be greater than zero.
9480 * The result is defined over the shared domain of the elements of "mupa"
9482 * We perform these checks and then hand over control to
9483 * multi_union_pw_aff_apply_aff.
9485 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9486 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9489 isl_space
*space1
, *space2
;
9492 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9493 isl_aff_get_space(aff
));
9494 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9498 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9499 space2
= isl_aff_get_domain_space(aff
);
9500 equal
= isl_space_is_equal(space1
, space2
);
9501 isl_space_free(space1
);
9502 isl_space_free(space2
);
9506 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9507 "spaces don't match", goto error
);
9508 dim
= isl_aff_dim(aff
, isl_dim_in
);
9512 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9513 "cannot determine domains", goto error
);
9515 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9517 isl_multi_union_pw_aff_free(mupa
);
9522 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9523 * The space of "mupa" is known to be compatible with the domain of "ma".
9525 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9526 * on the domain of "mupa".
9528 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9529 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9533 dom
= isl_multi_union_pw_aff_domain(mupa
);
9534 ma
= isl_multi_aff_project_domain_on_params(ma
);
9536 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9539 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9540 * with the domain of "ma".
9541 * The result is defined over the shared domain of the elements of "mupa"
9543 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9544 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9546 isl_space
*space1
, *space2
;
9547 isl_multi_union_pw_aff
*res
;
9550 isl_size n_in
, n_out
;
9552 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9553 isl_multi_aff_get_space(ma
));
9554 ma
= isl_multi_aff_align_params(ma
,
9555 isl_multi_union_pw_aff_get_space(mupa
));
9556 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9557 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9558 if (!mupa
|| n_in
< 0 || n_out
< 0)
9561 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9562 space2
= isl_multi_aff_get_domain_space(ma
);
9563 equal
= isl_space_is_equal(space1
, space2
);
9564 isl_space_free(space1
);
9565 isl_space_free(space2
);
9569 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9570 "spaces don't match", goto error
);
9572 return mupa_apply_multi_aff_0D(mupa
, ma
);
9574 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9575 res
= isl_multi_union_pw_aff_alloc(space1
);
9577 for (i
= 0; i
< n_out
; ++i
) {
9579 isl_union_pw_aff
*upa
;
9581 aff
= isl_multi_aff_get_aff(ma
, i
);
9582 upa
= multi_union_pw_aff_apply_aff(
9583 isl_multi_union_pw_aff_copy(mupa
), aff
);
9584 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9587 isl_multi_aff_free(ma
);
9588 isl_multi_union_pw_aff_free(mupa
);
9591 isl_multi_union_pw_aff_free(mupa
);
9592 isl_multi_aff_free(ma
);
9596 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9597 * The space of "mupa" is known to be compatible with the domain of "pa".
9599 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9600 * on the domain of "mupa".
9602 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9603 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9607 dom
= isl_multi_union_pw_aff_domain(mupa
);
9608 pa
= isl_pw_aff_project_domain_on_params(pa
);
9610 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9613 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9614 * with the domain of "pa".
9615 * Furthermore, the dimension of this space needs to be greater than zero.
9616 * The result is defined over the shared domain of the elements of "mupa"
9618 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9619 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9624 isl_space
*space
, *space2
;
9625 isl_union_pw_aff
*upa
;
9627 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9628 isl_pw_aff_get_space(pa
));
9629 pa
= isl_pw_aff_align_params(pa
,
9630 isl_multi_union_pw_aff_get_space(mupa
));
9634 space
= isl_multi_union_pw_aff_get_space(mupa
);
9635 space2
= isl_pw_aff_get_domain_space(pa
);
9636 equal
= isl_space_is_equal(space
, space2
);
9637 isl_space_free(space
);
9638 isl_space_free(space2
);
9642 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9643 "spaces don't match", goto error
);
9644 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9648 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9650 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9651 upa
= isl_union_pw_aff_empty(space
);
9653 for (i
= 0; i
< pa
->n
; ++i
) {
9656 isl_multi_union_pw_aff
*mupa_i
;
9657 isl_union_pw_aff
*upa_i
;
9659 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9660 domain
= isl_set_copy(pa
->p
[i
].set
);
9661 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9662 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9663 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9664 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9667 isl_multi_union_pw_aff_free(mupa
);
9668 isl_pw_aff_free(pa
);
9671 isl_multi_union_pw_aff_free(mupa
);
9672 isl_pw_aff_free(pa
);
9676 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9677 * The space of "mupa" is known to be compatible with the domain of "pma".
9679 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9680 * on the domain of "mupa".
9682 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9683 __isl_take isl_multi_union_pw_aff
*mupa
,
9684 __isl_take isl_pw_multi_aff
*pma
)
9688 dom
= isl_multi_union_pw_aff_domain(mupa
);
9689 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9691 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9694 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9695 * with the domain of "pma".
9696 * The result is defined over the shared domain of the elements of "mupa"
9698 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9699 __isl_take isl_multi_union_pw_aff
*mupa
,
9700 __isl_take isl_pw_multi_aff
*pma
)
9702 isl_space
*space1
, *space2
;
9703 isl_multi_union_pw_aff
*res
;
9706 isl_size n_in
, n_out
;
9708 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9709 isl_pw_multi_aff_get_space(pma
));
9710 pma
= isl_pw_multi_aff_align_params(pma
,
9711 isl_multi_union_pw_aff_get_space(mupa
));
9715 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9716 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9717 equal
= isl_space_is_equal(space1
, space2
);
9718 isl_space_free(space1
);
9719 isl_space_free(space2
);
9723 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9724 "spaces don't match", goto error
);
9725 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9726 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9727 if (n_in
< 0 || n_out
< 0)
9730 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9732 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9733 res
= isl_multi_union_pw_aff_alloc(space1
);
9735 for (i
= 0; i
< n_out
; ++i
) {
9737 isl_union_pw_aff
*upa
;
9739 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9740 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9741 isl_multi_union_pw_aff_copy(mupa
), pa
);
9742 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9745 isl_pw_multi_aff_free(pma
);
9746 isl_multi_union_pw_aff_free(mupa
);
9749 isl_multi_union_pw_aff_free(mupa
);
9750 isl_pw_multi_aff_free(pma
);
9754 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9755 * If the explicit domain only keeps track of constraints on the parameters,
9756 * then only update those constraints.
9758 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9759 __isl_take isl_multi_union_pw_aff
*mupa
,
9760 __isl_keep isl_union_pw_multi_aff
*upma
)
9764 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9765 return isl_multi_union_pw_aff_free(mupa
);
9767 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9771 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9773 return isl_multi_union_pw_aff_free(mupa
);
9775 upma
= isl_union_pw_multi_aff_copy(upma
);
9777 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9778 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9780 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9783 return isl_multi_union_pw_aff_free(mupa
);
9787 /* Compute the pullback of "mupa" by the function represented by "upma".
9788 * In other words, plug in "upma" in "mupa". The result contains
9789 * expressions defined over the domain space of "upma".
9791 * Run over all elements of "mupa" and plug in "upma" in each of them.
9793 * If "mupa" has an explicit domain, then it is this domain
9794 * that needs to undergo a pullback instead, i.e., a preimage.
9796 __isl_give isl_multi_union_pw_aff
*
9797 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9798 __isl_take isl_multi_union_pw_aff
*mupa
,
9799 __isl_take isl_union_pw_multi_aff
*upma
)
9804 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9805 isl_union_pw_multi_aff_get_space(upma
));
9806 upma
= isl_union_pw_multi_aff_align_params(upma
,
9807 isl_multi_union_pw_aff_get_space(mupa
));
9808 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9809 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9813 for (i
= 0; i
< n
; ++i
) {
9814 isl_union_pw_aff
*upa
;
9816 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9817 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9818 isl_union_pw_multi_aff_copy(upma
));
9819 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9822 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9823 mupa
= preimage_explicit_domain(mupa
, upma
);
9825 isl_union_pw_multi_aff_free(upma
);
9828 isl_multi_union_pw_aff_free(mupa
);
9829 isl_union_pw_multi_aff_free(upma
);
9833 /* Extract the sequence of elements in "mupa" with domain space "space"
9834 * (ignoring parameters).
9836 * For the elements of "mupa" that are not defined on the specified space,
9837 * the corresponding element in the result is empty.
9839 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9840 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9844 isl_space
*space_mpa
;
9845 isl_multi_pw_aff
*mpa
;
9847 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9848 if (n
< 0 || !space
)
9851 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9852 space
= isl_space_replace_params(space
, space_mpa
);
9853 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9855 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9857 space
= isl_space_from_domain(space
);
9858 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9859 for (i
= 0; i
< n
; ++i
) {
9860 isl_union_pw_aff
*upa
;
9863 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9864 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9865 isl_space_copy(space
));
9866 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9867 isl_union_pw_aff_free(upa
);
9870 isl_space_free(space
);
9873 isl_space_free(space
);
9877 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9878 * should modify the base expressions in the input.
9880 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9881 * are taken into account.
9882 * "fn" is applied to each entry in the input.
9884 struct isl_union_pw_multi_aff_un_op_control
{
9885 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9886 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9889 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9890 * a second argument) for use as an isl_union_pw_multi_aff_transform
9891 * filter function (which does take a second argument).
9892 * Simply call control->filter without the second argument.
9894 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9895 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9897 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9899 return control
->filter(pma
);
9902 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9903 * a second argument) for use as an isl_union_pw_multi_aff_transform
9904 * base function (which does take a second argument).
9905 * Simply call control->fn without the second argument.
9907 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9908 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9910 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9912 return control
->fn(pma
);
9915 /* Construct an isl_union_pw_multi_aff that is obtained by
9916 * modifying "upma" according to "control".
9918 * isl_union_pw_multi_aff_transform performs essentially
9919 * the same operation, but takes a filter and a callback function
9920 * of a different form (with an extra argument).
9921 * Call isl_union_pw_multi_aff_transform with wrappers
9922 * that remove this extra argument.
9924 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9925 __isl_take isl_union_pw_multi_aff
*upma
,
9926 struct isl_union_pw_multi_aff_un_op_control
*control
)
9928 struct isl_union_pw_multi_aff_transform_control t_control
= {
9929 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9930 .filter_user
= control
,
9931 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9935 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9938 /* For each function in "upma" of the form A -> [B -> C],
9939 * extract the function A -> B and collect the results.
9941 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9942 __isl_take isl_union_pw_multi_aff
*upma
)
9944 struct isl_union_pw_multi_aff_un_op_control control
= {
9945 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9946 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9948 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9951 /* For each function in "upma" of the form A -> [B -> C],
9952 * extract the function A -> C and collect the results.
9954 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9955 __isl_take isl_union_pw_multi_aff
*upma
)
9957 struct isl_union_pw_multi_aff_un_op_control control
= {
9958 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9959 .fn
= &isl_pw_multi_aff_range_factor_range
,
9961 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9964 /* Evaluate the affine function "aff" in the void point "pnt".
9965 * In particular, return the value NaN.
9967 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9968 __isl_take isl_point
*pnt
)
9972 ctx
= isl_point_get_ctx(pnt
);
9974 isl_point_free(pnt
);
9975 return isl_val_nan(ctx
);
9978 /* Evaluate the affine expression "aff"
9979 * in the coordinates (with denominator) "pnt".
9981 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9982 __isl_keep isl_vec
*pnt
)
9991 ctx
= isl_vec_get_ctx(aff
);
9994 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9995 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9996 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9997 v
= isl_val_normalize(v
);
10004 /* Check that the domain space of "aff" is equal to "space".
10006 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10007 __isl_keep isl_space
*space
)
10011 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10013 return isl_stat_error
;
10015 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10016 "incompatible spaces", return isl_stat_error
);
10017 return isl_stat_ok
;
10020 /* Evaluate the affine function "aff" in "pnt".
10022 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10023 __isl_take isl_point
*pnt
)
10027 isl_local_space
*ls
;
10029 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10031 is_void
= isl_point_is_void(pnt
);
10035 return eval_void(aff
, pnt
);
10037 ls
= isl_aff_get_domain_local_space(aff
);
10038 pnt
= isl_local_space_lift_point(ls
, pnt
);
10040 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10043 isl_point_free(pnt
);
10048 isl_point_free(pnt
);