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
8 * Copyright 2021 Sven Verdoolaege
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
40 #include <isl_list_templ.c>
41 #include <isl_list_read_templ.c>
44 #define EL_BASE pw_aff
46 #include <isl_list_templ.c>
47 #include <isl_list_read_templ.c>
50 #define EL_BASE pw_multi_aff
52 #include <isl_list_templ.c>
53 #include <isl_list_read_templ.c>
56 #define EL_BASE union_pw_aff
58 #include <isl_list_templ.c>
59 #include <isl_list_read_templ.c>
62 #define EL_BASE union_pw_multi_aff
64 #include <isl_list_templ.c>
66 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
67 __isl_take isl_vec
*v
)
74 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
84 isl_local_space_free(ls
);
89 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
98 ctx
= isl_local_space_get_ctx(ls
);
99 if (!isl_local_space_divs_known(ls
))
100 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
102 if (!isl_local_space_is_set(ls
))
103 isl_die(ctx
, isl_error_invalid
,
104 "domain of affine expression should be a set",
107 total
= isl_local_space_dim(ls
, isl_dim_all
);
110 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
111 return isl_aff_alloc_vec(ls
, v
);
113 isl_local_space_free(ls
);
117 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
126 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
131 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
132 isl_vec_copy(aff
->v
));
135 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
143 return isl_aff_dup(aff
);
146 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
150 aff
= isl_aff_alloc(ls
);
154 isl_int_set_si(aff
->v
->el
[0], 1);
155 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
160 /* Return an affine expression that is equal to zero on domain space "space".
162 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
164 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
167 /* This function performs the same operation as isl_aff_zero_on_domain_space,
168 * but is considered as a function on an isl_space when exported.
170 __isl_give isl_aff
*isl_space_zero_aff_on_domain(__isl_take isl_space
*space
)
172 return isl_aff_zero_on_domain_space(space
);
175 /* Return a piecewise affine expression defined on the specified domain
176 * that is equal to zero.
178 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
180 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
183 /* Change "aff" into a NaN.
185 * Note that this function gets called from isl_aff_nan_on_domain,
186 * so "aff" may not have been initialized yet.
188 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
190 aff
= isl_aff_cow(aff
);
194 aff
->v
= isl_vec_clr(aff
->v
);
196 return isl_aff_free(aff
);
201 /* Return an affine expression defined on the specified domain
202 * that represents NaN.
204 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
208 aff
= isl_aff_alloc(ls
);
209 return isl_aff_set_nan(aff
);
212 /* Return an affine expression defined on the specified domain space
213 * that represents NaN.
215 __isl_give isl_aff
*isl_aff_nan_on_domain_space(__isl_take isl_space
*space
)
217 return isl_aff_nan_on_domain(isl_local_space_from_space(space
));
220 /* Return a piecewise affine expression defined on the specified domain space
221 * that represents NaN.
223 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain_space(
224 __isl_take isl_space
*space
)
226 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space
));
229 /* Return a piecewise affine expression defined on the specified domain
230 * that represents NaN.
232 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
234 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
237 /* Return an affine expression that is equal to "val" on
238 * domain local space "ls".
240 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
241 __isl_take isl_val
*val
)
247 if (!isl_val_is_rat(val
))
248 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
249 "expecting rational value", goto error
);
251 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
255 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
256 isl_int_set(aff
->v
->el
[1], val
->n
);
257 isl_int_set(aff
->v
->el
[0], val
->d
);
259 isl_local_space_free(ls
);
263 isl_local_space_free(ls
);
268 /* Return an affine expression that is equal to "val" on domain space "space".
270 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
271 __isl_take isl_val
*val
)
273 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
276 /* Return an affine expression that is equal to the specified dimension
279 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
280 enum isl_dim_type type
, unsigned pos
)
288 space
= isl_local_space_get_space(ls
);
291 if (isl_space_is_map(space
))
292 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
293 "expecting (parameter) set space", goto error
);
294 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
297 isl_space_free(space
);
298 aff
= isl_aff_alloc(ls
);
302 pos
+= isl_local_space_offset(aff
->ls
, type
);
304 isl_int_set_si(aff
->v
->el
[0], 1);
305 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
306 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
310 isl_local_space_free(ls
);
311 isl_space_free(space
);
315 /* Return a piecewise affine expression that is equal to
316 * the specified dimension in "ls".
318 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
319 enum isl_dim_type type
, unsigned pos
)
321 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
324 /* Return an affine expression that is equal to the parameter
325 * in the domain space "space" with identifier "id".
327 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
328 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
335 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
337 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
338 "parameter not found in space", goto error
);
340 ls
= isl_local_space_from_space(space
);
341 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
343 isl_space_free(space
);
348 /* This function performs the same operation as
349 * isl_aff_param_on_domain_space_id,
350 * but is considered as a function on an isl_space when exported.
352 __isl_give isl_aff
*isl_space_param_aff_on_domain_id(
353 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
355 return isl_aff_param_on_domain_space_id(space
, id
);
358 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
366 isl_local_space_free(aff
->ls
);
367 isl_vec_free(aff
->v
);
374 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
376 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
379 /* Return a hash value that digests "aff".
381 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
383 uint32_t hash
, ls_hash
, v_hash
;
388 hash
= isl_hash_init();
389 ls_hash
= isl_local_space_get_hash(aff
->ls
);
390 isl_hash_hash(hash
, ls_hash
);
391 v_hash
= isl_vec_get_hash(aff
->v
);
392 isl_hash_hash(hash
, v_hash
);
397 /* Return the domain local space of "aff".
399 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
400 __isl_keep isl_aff
*aff
)
402 return aff
? aff
->ls
: NULL
;
405 /* Return the number of variables of the given type in the domain of "aff".
407 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
411 ls
= isl_aff_peek_domain_local_space(aff
);
412 return isl_local_space_dim(ls
, type
);
415 /* Externally, an isl_aff has a map space, but internally, the
416 * ls field corresponds to the domain of that space.
418 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
421 return isl_size_error
;
422 if (type
== isl_dim_out
)
424 if (type
== isl_dim_in
)
426 return isl_aff_domain_dim(aff
, type
);
429 /* Return the offset of the first coefficient of type "type" in
430 * the domain of "aff".
432 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
436 ls
= isl_aff_peek_domain_local_space(aff
);
437 return isl_local_space_offset(ls
, type
);
440 /* Return the position of the dimension of the given type and name
442 * Return -1 if no such dimension can be found.
444 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
449 if (type
== isl_dim_out
)
451 if (type
== isl_dim_in
)
453 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
456 /* Return the domain space of "aff".
458 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
460 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
463 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
465 return isl_space_copy(isl_aff_peek_domain_space(aff
));
468 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
473 space
= isl_local_space_get_space(aff
->ls
);
474 space
= isl_space_from_domain(space
);
475 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
479 /* Return a copy of the domain space of "aff".
481 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
482 __isl_keep isl_aff
*aff
)
484 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
487 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
492 ls
= isl_local_space_copy(aff
->ls
);
493 ls
= isl_local_space_from_domain(ls
);
494 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
498 /* Return the local space of the domain of "aff".
499 * This may be either a copy or the local space itself
500 * if there is only one reference to "aff".
501 * This allows the local space to be modified inplace
502 * if both the expression and its local space have only a single reference.
503 * The caller is not allowed to modify "aff" between this call and
504 * a subsequent call to isl_aff_restore_domain_local_space.
505 * The only exception is that isl_aff_free can be called instead.
507 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
508 __isl_keep isl_aff
*aff
)
515 return isl_aff_get_domain_local_space(aff
);
521 /* Set the local space of the domain of "aff" to "ls",
522 * where the local space of "aff" may be missing
523 * due to a preceding call to isl_aff_take_domain_local_space.
524 * However, in this case, "aff" only has a single reference and
525 * then the call to isl_aff_cow has no effect.
527 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
528 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
534 isl_local_space_free(ls
);
538 aff
= isl_aff_cow(aff
);
541 isl_local_space_free(aff
->ls
);
547 isl_local_space_free(ls
);
551 /* Externally, an isl_aff has a map space, but internally, the
552 * ls field corresponds to the domain of that space.
554 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
555 enum isl_dim_type type
, unsigned pos
)
559 if (type
== isl_dim_out
)
561 if (type
== isl_dim_in
)
563 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
566 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
567 __isl_take isl_space
*space
)
569 aff
= isl_aff_cow(aff
);
573 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
575 return isl_aff_free(aff
);
580 isl_space_free(space
);
584 /* Reset the space of "aff". This function is called from isl_pw_templ.c
585 * and doesn't know if the space of an element object is represented
586 * directly or through its domain. It therefore passes along both.
588 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
589 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
591 isl_space_free(space
);
592 return isl_aff_reset_domain_space(aff
, domain
);
595 /* Reorder the coefficients of the affine expression based
596 * on the given reordering.
597 * The reordering r is assumed to have been extended with the local
600 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
601 __isl_take isl_reordering
*r
, int n_div
)
611 space
= isl_reordering_peek_space(r
);
612 dim
= isl_space_dim(space
, isl_dim_all
);
615 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
618 isl_seq_cpy(res
->el
, vec
->el
, 2);
619 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
620 for (i
= 0; i
< r
->len
; ++i
)
621 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
623 isl_reordering_free(r
);
628 isl_reordering_free(r
);
632 /* Reorder the dimensions of the domain of "aff" according
633 * to the given reordering.
635 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
636 __isl_take isl_reordering
*r
)
638 aff
= isl_aff_cow(aff
);
642 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
643 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
644 aff
->ls
->div
->n_row
);
645 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
647 if (!aff
->v
|| !aff
->ls
)
648 return isl_aff_free(aff
);
653 isl_reordering_free(r
);
657 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
658 __isl_take isl_space
*model
)
660 isl_bool equal_params
;
665 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
666 if (equal_params
< 0)
671 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
672 exp
= isl_reordering_extend_space(exp
,
673 isl_aff_get_domain_space(aff
));
674 aff
= isl_aff_realign_domain(aff
, exp
);
677 isl_space_free(model
);
680 isl_space_free(model
);
687 #include "isl_unbind_params_templ.c"
689 /* Is "aff" obviously equal to zero?
691 * If the denominator is zero, then "aff" is not equal to zero.
693 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
698 return isl_bool_error
;
700 if (isl_int_is_zero(aff
->v
->el
[0]))
701 return isl_bool_false
;
702 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
703 return isl_bool_ok(pos
< 0);
706 /* Does "aff" represent NaN?
708 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
711 return isl_bool_error
;
713 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
716 /* Are "aff1" and "aff2" obviously equal?
718 * NaN is not equal to anything, not even to another NaN.
720 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
721 __isl_keep isl_aff
*aff2
)
726 return isl_bool_error
;
728 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
729 return isl_bool_false
;
731 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
732 if (equal
< 0 || !equal
)
735 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
738 /* Return the common denominator of "aff" in "v".
740 * We cannot return anything meaningful in case of a NaN.
742 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
745 return isl_stat_error
;
746 if (isl_aff_is_nan(aff
))
747 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
748 "cannot get denominator of NaN", return isl_stat_error
);
749 isl_int_set(*v
, aff
->v
->el
[0]);
753 /* Return the common denominator of "aff".
755 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
762 ctx
= isl_aff_get_ctx(aff
);
763 if (isl_aff_is_nan(aff
))
764 return isl_val_nan(ctx
);
765 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
768 /* Return the constant term of "aff".
770 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
778 ctx
= isl_aff_get_ctx(aff
);
779 if (isl_aff_is_nan(aff
))
780 return isl_val_nan(ctx
);
781 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
782 return isl_val_normalize(v
);
785 /* Return the coefficient of the variable of type "type" at position "pos"
788 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
789 enum isl_dim_type type
, int pos
)
797 ctx
= isl_aff_get_ctx(aff
);
798 if (type
== isl_dim_out
)
799 isl_die(ctx
, isl_error_invalid
,
800 "output/set dimension does not have a coefficient",
802 if (type
== isl_dim_in
)
805 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
808 if (isl_aff_is_nan(aff
))
809 return isl_val_nan(ctx
);
810 pos
+= isl_local_space_offset(aff
->ls
, type
);
811 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
812 return isl_val_normalize(v
);
815 /* Return the sign of the coefficient of the variable of type "type"
816 * at position "pos" of "aff".
818 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
826 ctx
= isl_aff_get_ctx(aff
);
827 if (type
== isl_dim_out
)
828 isl_die(ctx
, isl_error_invalid
,
829 "output/set dimension does not have a coefficient",
831 if (type
== isl_dim_in
)
834 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
837 pos
+= isl_local_space_offset(aff
->ls
, type
);
838 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
841 /* Replace the numerator of the constant term of "aff" by "v".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
849 if (isl_aff_is_nan(aff
))
851 aff
= isl_aff_cow(aff
);
855 aff
->v
= isl_vec_cow(aff
->v
);
857 return isl_aff_free(aff
);
859 isl_int_set(aff
->v
->el
[1], v
);
864 /* Replace the constant term of "aff" by "v".
866 * A NaN is unaffected by this operation.
868 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
869 __isl_take isl_val
*v
)
874 if (isl_aff_is_nan(aff
)) {
879 if (!isl_val_is_rat(v
))
880 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
881 "expecting rational value", goto error
);
883 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
884 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
889 aff
= isl_aff_cow(aff
);
892 aff
->v
= isl_vec_cow(aff
->v
);
896 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
897 isl_int_set(aff
->v
->el
[1], v
->n
);
898 } else if (isl_int_is_one(v
->d
)) {
899 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
901 isl_seq_scale(aff
->v
->el
+ 1,
902 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
903 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
904 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
905 aff
->v
= isl_vec_normalize(aff
->v
);
918 /* Add "v" to the constant term of "aff".
920 * A NaN is unaffected by this operation.
922 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
924 if (isl_int_is_zero(v
))
929 if (isl_aff_is_nan(aff
))
931 aff
= isl_aff_cow(aff
);
935 aff
->v
= isl_vec_cow(aff
->v
);
937 return isl_aff_free(aff
);
939 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
944 /* Add "v" to the constant term of "aff",
945 * in case "aff" is a rational expression.
947 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
948 __isl_take isl_val
*v
)
950 aff
= isl_aff_cow(aff
);
954 aff
->v
= isl_vec_cow(aff
->v
);
958 if (isl_int_is_one(v
->d
)) {
959 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
960 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
961 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
962 aff
->v
= isl_vec_normalize(aff
->v
);
966 isl_seq_scale(aff
->v
->el
+ 1,
967 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
968 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
969 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
970 aff
->v
= isl_vec_normalize(aff
->v
);
983 /* Return the first argument and free the second.
985 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
986 __isl_take isl_val
*v
)
992 /* Replace the first argument by NaN and free the second argument.
994 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
995 __isl_take isl_val
*v
)
998 return isl_aff_set_nan(aff
);
1001 /* Add "v" to the constant term of "aff".
1003 * A NaN is unaffected by this operation.
1004 * Conversely, adding a NaN turns "aff" into a NaN.
1006 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
1007 __isl_take isl_val
*v
)
1009 isl_bool is_nan
, is_zero
, is_rat
;
1011 is_nan
= isl_aff_is_nan(aff
);
1012 is_zero
= isl_val_is_zero(v
);
1013 if (is_nan
< 0 || is_zero
< 0)
1015 if (is_nan
|| is_zero
)
1016 return pick_free(aff
, v
);
1018 is_nan
= isl_val_is_nan(v
);
1019 is_rat
= isl_val_is_rat(v
);
1020 if (is_nan
< 0 || is_rat
< 0)
1023 return set_nan_free_val(aff
, v
);
1025 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1026 "expecting rational value or NaN", goto error
);
1028 return isl_aff_add_rat_constant_val(aff
, v
);
1035 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1040 isl_int_set_si(t
, v
);
1041 aff
= isl_aff_add_constant(aff
, t
);
1047 /* Add "v" to the numerator of the constant term of "aff".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1053 if (isl_int_is_zero(v
))
1058 if (isl_aff_is_nan(aff
))
1060 aff
= isl_aff_cow(aff
);
1064 aff
->v
= isl_vec_cow(aff
->v
);
1066 return isl_aff_free(aff
);
1068 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1073 /* Add "v" to the numerator of the constant term of "aff".
1075 * A NaN is unaffected by this operation.
1077 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1085 isl_int_set_si(t
, v
);
1086 aff
= isl_aff_add_constant_num(aff
, t
);
1092 /* Replace the numerator of the constant term of "aff" by "v".
1094 * A NaN is unaffected by this operation.
1096 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1100 if (isl_aff_is_nan(aff
))
1102 aff
= isl_aff_cow(aff
);
1106 aff
->v
= isl_vec_cow(aff
->v
);
1108 return isl_aff_free(aff
);
1110 isl_int_set_si(aff
->v
->el
[1], v
);
1115 /* Replace the numerator of the coefficient of the variable of type "type"
1116 * at position "pos" of "aff" by "v".
1118 * A NaN is unaffected by this operation.
1120 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1121 enum isl_dim_type type
, int pos
, isl_int v
)
1126 if (type
== isl_dim_out
)
1127 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1128 "output/set dimension does not have a coefficient",
1129 return isl_aff_free(aff
));
1130 if (type
== isl_dim_in
)
1133 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1134 return isl_aff_free(aff
);
1136 if (isl_aff_is_nan(aff
))
1138 aff
= isl_aff_cow(aff
);
1142 aff
->v
= isl_vec_cow(aff
->v
);
1144 return isl_aff_free(aff
);
1146 pos
+= isl_local_space_offset(aff
->ls
, type
);
1147 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1152 /* Replace the numerator of the coefficient of the variable of type "type"
1153 * at position "pos" of "aff" by "v".
1155 * A NaN is unaffected by this operation.
1157 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1158 enum isl_dim_type type
, int pos
, int v
)
1163 if (type
== isl_dim_out
)
1164 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1165 "output/set dimension does not have a coefficient",
1166 return isl_aff_free(aff
));
1167 if (type
== isl_dim_in
)
1170 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1171 return isl_aff_free(aff
);
1173 if (isl_aff_is_nan(aff
))
1175 pos
+= isl_local_space_offset(aff
->ls
, type
);
1176 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1179 aff
= isl_aff_cow(aff
);
1183 aff
->v
= isl_vec_cow(aff
->v
);
1185 return isl_aff_free(aff
);
1187 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1192 /* Replace the coefficient of the variable of type "type" at position "pos"
1195 * A NaN is unaffected by this operation.
1197 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1198 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1203 if (type
== isl_dim_out
)
1204 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1205 "output/set dimension does not have a coefficient",
1207 if (type
== isl_dim_in
)
1210 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1211 return isl_aff_free(aff
);
1213 if (isl_aff_is_nan(aff
)) {
1217 if (!isl_val_is_rat(v
))
1218 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1219 "expecting rational value", goto error
);
1221 pos
+= isl_local_space_offset(aff
->ls
, type
);
1222 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1223 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1228 aff
= isl_aff_cow(aff
);
1231 aff
->v
= isl_vec_cow(aff
->v
);
1235 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1236 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1237 } else if (isl_int_is_one(v
->d
)) {
1238 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1240 isl_seq_scale(aff
->v
->el
+ 1,
1241 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1242 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1243 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1244 aff
->v
= isl_vec_normalize(aff
->v
);
1257 /* Add "v" to the coefficient of the variable of type "type"
1258 * at position "pos" of "aff".
1260 * A NaN is unaffected by this operation.
1262 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1263 enum isl_dim_type type
, int pos
, isl_int v
)
1268 if (type
== isl_dim_out
)
1269 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1270 "output/set dimension does not have a coefficient",
1271 return isl_aff_free(aff
));
1272 if (type
== isl_dim_in
)
1275 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1276 return isl_aff_free(aff
);
1278 if (isl_aff_is_nan(aff
))
1280 aff
= isl_aff_cow(aff
);
1284 aff
->v
= isl_vec_cow(aff
->v
);
1286 return isl_aff_free(aff
);
1288 pos
+= isl_local_space_offset(aff
->ls
, type
);
1289 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1294 /* Add "v" to the coefficient of the variable of type "type"
1295 * at position "pos" of "aff".
1297 * A NaN is unaffected by this operation.
1299 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1300 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1305 if (isl_val_is_zero(v
)) {
1310 if (type
== isl_dim_out
)
1311 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1312 "output/set dimension does not have a coefficient",
1314 if (type
== isl_dim_in
)
1317 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1320 if (isl_aff_is_nan(aff
)) {
1324 if (!isl_val_is_rat(v
))
1325 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1326 "expecting rational value", goto error
);
1328 aff
= isl_aff_cow(aff
);
1332 aff
->v
= isl_vec_cow(aff
->v
);
1336 pos
+= isl_local_space_offset(aff
->ls
, type
);
1337 if (isl_int_is_one(v
->d
)) {
1338 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1339 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1340 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1341 aff
->v
= isl_vec_normalize(aff
->v
);
1345 isl_seq_scale(aff
->v
->el
+ 1,
1346 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1347 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1348 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1349 aff
->v
= isl_vec_normalize(aff
->v
);
1362 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1363 enum isl_dim_type type
, int pos
, int v
)
1368 isl_int_set_si(t
, v
);
1369 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1375 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1380 return isl_local_space_get_div(aff
->ls
, pos
);
1383 /* Return the negation of "aff".
1385 * As a special case, -NaN = NaN.
1387 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1391 if (isl_aff_is_nan(aff
))
1393 aff
= isl_aff_cow(aff
);
1396 aff
->v
= isl_vec_cow(aff
->v
);
1398 return isl_aff_free(aff
);
1400 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1405 /* Remove divs from the local space that do not appear in the affine
1407 * We currently only remove divs at the end.
1408 * Some intermediate divs may also not appear directly in the affine
1409 * expression, but we would also need to check that no other divs are
1410 * defined in terms of them.
1412 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1418 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1419 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1420 if (n
< 0 || off
< 0)
1421 return isl_aff_free(aff
);
1423 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1427 aff
= isl_aff_cow(aff
);
1431 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1432 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1433 if (!aff
->ls
|| !aff
->v
)
1434 return isl_aff_free(aff
);
1439 /* Look for any divs in the aff->ls with a denominator equal to one
1440 * and plug them into the affine expression and any subsequent divs
1441 * that may reference the div.
1443 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1450 isl_local_space
*ls
;
1453 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1454 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1455 if (n
< 0 || off
< 0)
1456 return isl_aff_free(aff
);
1458 for (i
= 0; i
< n
; ++i
) {
1459 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1461 ls
= isl_local_space_copy(aff
->ls
);
1462 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1463 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1464 vec
= isl_vec_copy(aff
->v
);
1465 vec
= isl_vec_cow(vec
);
1471 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1476 isl_vec_free(aff
->v
);
1478 isl_local_space_free(aff
->ls
);
1485 isl_local_space_free(ls
);
1486 return isl_aff_free(aff
);
1489 /* Look for any divs j that appear with a unit coefficient inside
1490 * the definitions of other divs i and plug them into the definitions
1493 * In particular, an expression of the form
1495 * floor((f(..) + floor(g(..)/n))/m)
1499 * floor((n * f(..) + g(..))/(n * m))
1501 * This simplification is correct because we can move the expression
1502 * f(..) into the inner floor in the original expression to obtain
1504 * floor(floor((n * f(..) + g(..))/n)/m)
1506 * from which we can derive the simplified expression.
1508 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1514 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1515 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1516 if (n
< 0 || off
< 0)
1517 return isl_aff_free(aff
);
1518 for (i
= 1; i
< n
; ++i
) {
1519 for (j
= 0; j
< i
; ++j
) {
1520 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1522 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1523 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1524 aff
->v
->size
, i
, 1);
1526 return isl_aff_free(aff
);
1533 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1535 * Even though this function is only called on isl_affs with a single
1536 * reference, we are careful to only change aff->v and aff->ls together.
1538 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1540 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1541 isl_local_space
*ls
;
1545 return isl_aff_free(aff
);
1547 ls
= isl_local_space_copy(aff
->ls
);
1548 ls
= isl_local_space_swap_div(ls
, a
, b
);
1549 v
= isl_vec_copy(aff
->v
);
1554 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1555 isl_vec_free(aff
->v
);
1557 isl_local_space_free(aff
->ls
);
1563 isl_local_space_free(ls
);
1564 return isl_aff_free(aff
);
1567 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1569 * We currently do not actually remove div "b", but simply add its
1570 * coefficient to that of "a" and then zero it out.
1572 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1574 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1577 return isl_aff_free(aff
);
1579 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1582 aff
->v
= isl_vec_cow(aff
->v
);
1584 return isl_aff_free(aff
);
1586 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1587 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1588 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1593 /* Sort the divs in the local space of "aff" according to
1594 * the comparison function "cmp_row" in isl_local_space.c,
1595 * combining the coefficients of identical divs.
1597 * Reordering divs does not change the semantics of "aff",
1598 * so there is no need to call isl_aff_cow.
1599 * Moreover, this function is currently only called on isl_affs
1600 * with a single reference.
1602 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1607 n
= isl_aff_dim(aff
, isl_dim_div
);
1609 return isl_aff_free(aff
);
1610 for (i
= 1; i
< n
; ++i
) {
1611 for (j
= i
- 1; j
>= 0; --j
) {
1612 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1616 aff
= merge_divs(aff
, j
, j
+ 1);
1618 aff
= swap_div(aff
, j
, j
+ 1);
1627 /* Normalize the representation of "aff".
1629 * This function should only be called on "new" isl_affs, i.e.,
1630 * with only a single reference. We therefore do not need to
1631 * worry about affecting other instances.
1633 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1637 aff
->v
= isl_vec_normalize(aff
->v
);
1639 return isl_aff_free(aff
);
1640 aff
= plug_in_integral_divs(aff
);
1641 aff
= plug_in_unit_divs(aff
);
1642 aff
= sort_divs(aff
);
1643 aff
= isl_aff_remove_unused_divs(aff
);
1647 /* Given f, return floor(f).
1648 * If f is an integer expression, then just return f.
1649 * If f is a constant, then return the constant floor(f).
1650 * Otherwise, if f = g/m, write g = q m + r,
1651 * create a new div d = [r/m] and return the expression q + d.
1652 * The coefficients in r are taken to lie between -m/2 and m/2.
1654 * reduce_div_coefficients performs the same normalization.
1656 * As a special case, floor(NaN) = NaN.
1658 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1668 if (isl_aff_is_nan(aff
))
1670 if (isl_int_is_one(aff
->v
->el
[0]))
1673 aff
= isl_aff_cow(aff
);
1677 aff
->v
= isl_vec_cow(aff
->v
);
1679 return isl_aff_free(aff
);
1681 if (isl_aff_is_cst(aff
)) {
1682 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1683 isl_int_set_si(aff
->v
->el
[0], 1);
1687 div
= isl_vec_copy(aff
->v
);
1688 div
= isl_vec_cow(div
);
1690 return isl_aff_free(aff
);
1692 ctx
= isl_aff_get_ctx(aff
);
1693 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1694 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1695 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1696 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1697 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1698 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1699 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1703 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1705 return isl_aff_free(aff
);
1707 size
= aff
->v
->size
;
1708 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1710 return isl_aff_free(aff
);
1711 isl_int_set_si(aff
->v
->el
[0], 1);
1712 isl_int_set_si(aff
->v
->el
[size
], 1);
1714 aff
= isl_aff_normalize(aff
);
1721 * aff mod m = aff - m * floor(aff/m)
1723 * with m an integer value.
1725 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1726 __isl_take isl_val
*m
)
1733 if (!isl_val_is_int(m
))
1734 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1735 "expecting integer modulo", goto error
);
1737 res
= isl_aff_copy(aff
);
1738 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1739 aff
= isl_aff_floor(aff
);
1740 aff
= isl_aff_scale_val(aff
, m
);
1741 res
= isl_aff_sub(res
, aff
);
1752 * pwaff mod m = pwaff - m * floor(pwaff/m)
1754 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1758 res
= isl_pw_aff_copy(pwaff
);
1759 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1760 pwaff
= isl_pw_aff_floor(pwaff
);
1761 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1762 res
= isl_pw_aff_sub(res
, pwaff
);
1769 * pa mod m = pa - m * floor(pa/m)
1771 * with m an integer value.
1773 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1774 __isl_take isl_val
*m
)
1778 if (!isl_val_is_int(m
))
1779 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1780 "expecting integer modulo", goto error
);
1781 pa
= isl_pw_aff_mod(pa
, m
->n
);
1785 isl_pw_aff_free(pa
);
1790 /* Given f, return ceil(f).
1791 * If f is an integer expression, then just return f.
1792 * Otherwise, let f be the expression
1798 * floor((e + m - 1)/m)
1800 * As a special case, ceil(NaN) = NaN.
1802 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1807 if (isl_aff_is_nan(aff
))
1809 if (isl_int_is_one(aff
->v
->el
[0]))
1812 aff
= isl_aff_cow(aff
);
1815 aff
->v
= isl_vec_cow(aff
->v
);
1817 return isl_aff_free(aff
);
1819 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1820 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1821 aff
= isl_aff_floor(aff
);
1826 /* Apply the expansion computed by isl_merge_divs.
1827 * The expansion itself is given by "exp" while the resulting
1828 * list of divs is given by "div".
1830 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1831 __isl_take isl_mat
*div
, int *exp
)
1837 aff
= isl_aff_cow(aff
);
1839 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1840 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1841 new_n_div
= isl_mat_rows(div
);
1842 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1845 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1846 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1847 if (!aff
->v
|| !aff
->ls
)
1848 return isl_aff_free(aff
);
1856 /* Add two affine expressions that live in the same local space.
1858 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1859 __isl_take isl_aff
*aff2
)
1863 aff1
= isl_aff_cow(aff1
);
1867 aff1
->v
= isl_vec_cow(aff1
->v
);
1873 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1874 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1875 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1876 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1877 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1878 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1879 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1884 aff1
= isl_aff_normalize(aff1
);
1892 /* Replace one of the arguments by a NaN and free the other one.
1894 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1895 __isl_take isl_aff
*aff2
)
1898 return isl_aff_set_nan(aff1
);
1901 /* Return the sum of "aff1" and "aff2".
1903 * If either of the two is NaN, then the result is NaN.
1905 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1906 __isl_take isl_aff
*aff2
)
1912 isl_size n_div1
, n_div2
;
1917 ctx
= isl_aff_get_ctx(aff1
);
1918 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1919 isl_die(ctx
, isl_error_invalid
,
1920 "spaces don't match", goto error
);
1922 if (isl_aff_is_nan(aff1
)) {
1926 if (isl_aff_is_nan(aff2
)) {
1931 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1932 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1933 if (n_div1
< 0 || n_div2
< 0)
1935 if (n_div1
== 0 && n_div2
== 0)
1936 return add_expanded(aff1
, aff2
);
1938 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1939 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1940 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1943 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1944 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1945 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1949 return add_expanded(aff1
, aff2
);
1958 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1959 __isl_take isl_aff
*aff2
)
1961 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1964 /* Return the result of scaling "aff" by a factor of "f".
1966 * As a special case, f * NaN = NaN.
1968 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1974 if (isl_aff_is_nan(aff
))
1977 if (isl_int_is_one(f
))
1980 aff
= isl_aff_cow(aff
);
1983 aff
->v
= isl_vec_cow(aff
->v
);
1985 return isl_aff_free(aff
);
1987 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1988 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1993 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1994 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1995 isl_int_divexact(gcd
, f
, gcd
);
1996 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2002 /* Multiple "aff" by "v".
2004 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
2005 __isl_take isl_val
*v
)
2010 if (isl_val_is_one(v
)) {
2015 if (!isl_val_is_rat(v
))
2016 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2017 "expecting rational factor", goto error
);
2019 aff
= isl_aff_scale(aff
, v
->n
);
2020 aff
= isl_aff_scale_down(aff
, v
->d
);
2030 /* Return the result of scaling "aff" down by a factor of "f".
2032 * As a special case, NaN/f = NaN.
2034 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2040 if (isl_aff_is_nan(aff
))
2043 if (isl_int_is_one(f
))
2046 aff
= isl_aff_cow(aff
);
2050 if (isl_int_is_zero(f
))
2051 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2052 "cannot scale down by zero", return isl_aff_free(aff
));
2054 aff
->v
= isl_vec_cow(aff
->v
);
2056 return isl_aff_free(aff
);
2059 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2060 isl_int_gcd(gcd
, gcd
, f
);
2061 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2062 isl_int_divexact(gcd
, f
, gcd
);
2063 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2069 /* Divide "aff" by "v".
2071 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2072 __isl_take isl_val
*v
)
2077 if (isl_val_is_one(v
)) {
2082 if (!isl_val_is_rat(v
))
2083 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2084 "expecting rational factor", goto error
);
2085 if (!isl_val_is_pos(v
))
2086 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2087 "factor needs to be positive", goto error
);
2089 aff
= isl_aff_scale(aff
, v
->d
);
2090 aff
= isl_aff_scale_down(aff
, v
->n
);
2100 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2108 isl_int_set_ui(v
, f
);
2109 aff
= isl_aff_scale_down(aff
, v
);
2115 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2116 enum isl_dim_type type
, unsigned pos
, const char *s
)
2118 aff
= isl_aff_cow(aff
);
2121 if (type
== isl_dim_out
)
2122 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2123 "cannot set name of output/set dimension",
2124 return isl_aff_free(aff
));
2125 if (type
== isl_dim_in
)
2127 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2129 return isl_aff_free(aff
);
2134 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2135 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2137 aff
= isl_aff_cow(aff
);
2140 if (type
== isl_dim_out
)
2141 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2142 "cannot set name of output/set dimension",
2144 if (type
== isl_dim_in
)
2146 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2148 return isl_aff_free(aff
);
2157 /* Replace the identifier of the input tuple of "aff" by "id".
2158 * type is currently required to be equal to isl_dim_in
2160 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2161 enum isl_dim_type type
, __isl_take isl_id
*id
)
2163 aff
= isl_aff_cow(aff
);
2166 if (type
!= isl_dim_in
)
2167 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2168 "cannot only set id of input tuple", goto error
);
2169 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2171 return isl_aff_free(aff
);
2180 /* Exploit the equalities in "eq" to simplify the affine expression
2181 * and the expressions of the integer divisions in the local space.
2182 * The integer divisions in this local space are assumed to appear
2183 * as regular dimensions in "eq".
2185 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2186 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2194 if (eq
->n_eq
== 0) {
2195 isl_basic_set_free(eq
);
2199 aff
= isl_aff_cow(aff
);
2203 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2204 isl_basic_set_copy(eq
));
2205 aff
->v
= isl_vec_cow(aff
->v
);
2206 if (!aff
->ls
|| !aff
->v
)
2209 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2211 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2212 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2213 if (j
< 0 || j
== 0 || j
>= o_div
)
2216 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2220 isl_basic_set_free(eq
);
2221 aff
= isl_aff_normalize(aff
);
2224 isl_basic_set_free(eq
);
2229 /* Exploit the equalities in "eq" to simplify the affine expression
2230 * and the expressions of the integer divisions in the local space.
2232 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2233 __isl_take isl_basic_set
*eq
)
2237 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2241 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2242 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2244 isl_basic_set_free(eq
);
2249 /* Look for equalities among the variables shared by context and aff
2250 * and the integer divisions of aff, if any.
2251 * The equalities are then used to eliminate coefficients and/or integer
2252 * divisions from aff.
2254 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2255 __isl_take isl_set
*context
)
2257 isl_local_space
*ls
;
2258 isl_basic_set
*hull
;
2260 ls
= isl_aff_get_domain_local_space(aff
);
2261 context
= isl_local_space_lift_set(ls
, context
);
2263 hull
= isl_set_affine_hull(context
);
2264 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2267 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2268 __isl_take isl_set
*context
)
2270 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2271 dom_context
= isl_set_intersect_params(dom_context
, context
);
2272 return isl_aff_gist(aff
, dom_context
);
2275 /* Return a basic set containing those elements in the space
2276 * of aff where it is positive. "rational" should not be set.
2278 * If "aff" is NaN, then it is not positive.
2280 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2281 int rational
, void *user
)
2283 isl_constraint
*ineq
;
2284 isl_basic_set
*bset
;
2289 if (isl_aff_is_nan(aff
)) {
2290 isl_space
*space
= isl_aff_get_domain_space(aff
);
2292 return isl_basic_set_empty(space
);
2295 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2296 "rational sets not supported", goto error
);
2298 ineq
= isl_inequality_from_aff(aff
);
2299 c
= isl_constraint_get_constant_val(ineq
);
2300 c
= isl_val_sub_ui(c
, 1);
2301 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2303 bset
= isl_basic_set_from_constraint(ineq
);
2304 bset
= isl_basic_set_simplify(bset
);
2311 /* Return a basic set containing those elements in the space
2312 * of aff where it is non-negative.
2313 * If "rational" is set, then return a rational basic set.
2315 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2317 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2318 __isl_take isl_aff
*aff
, int rational
, void *user
)
2320 isl_constraint
*ineq
;
2321 isl_basic_set
*bset
;
2325 if (isl_aff_is_nan(aff
)) {
2326 isl_space
*space
= isl_aff_get_domain_space(aff
);
2328 return isl_basic_set_empty(space
);
2331 ineq
= isl_inequality_from_aff(aff
);
2333 bset
= isl_basic_set_from_constraint(ineq
);
2335 bset
= isl_basic_set_set_rational(bset
);
2336 bset
= isl_basic_set_simplify(bset
);
2340 /* Return a basic set containing those elements in the space
2341 * of aff where it is non-negative.
2343 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2345 return aff_nonneg_basic_set(aff
, 0, NULL
);
2348 /* Return a basic set containing those elements in the domain space
2349 * of "aff" where it is positive.
2351 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2353 aff
= isl_aff_add_constant_num_si(aff
, -1);
2354 return isl_aff_nonneg_basic_set(aff
);
2357 /* Return a basic set containing those elements in the domain space
2358 * of aff where it is negative.
2360 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2362 aff
= isl_aff_neg(aff
);
2363 return isl_aff_pos_basic_set(aff
);
2366 /* Return a basic set containing those elements in the space
2367 * of aff where it is zero.
2368 * If "rational" is set, then return a rational basic set.
2370 * If "aff" is NaN, then it is not zero.
2372 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2373 int rational
, void *user
)
2375 isl_constraint
*ineq
;
2376 isl_basic_set
*bset
;
2380 if (isl_aff_is_nan(aff
)) {
2381 isl_space
*space
= isl_aff_get_domain_space(aff
);
2383 return isl_basic_set_empty(space
);
2386 ineq
= isl_equality_from_aff(aff
);
2388 bset
= isl_basic_set_from_constraint(ineq
);
2390 bset
= isl_basic_set_set_rational(bset
);
2391 bset
= isl_basic_set_simplify(bset
);
2395 /* Return a basic set containing those elements in the space
2396 * of aff where it is zero.
2398 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2400 return aff_zero_basic_set(aff
, 0, NULL
);
2403 /* Return a basic set containing those elements in the shared space
2404 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2406 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2407 __isl_take isl_aff
*aff2
)
2409 aff1
= isl_aff_sub(aff1
, aff2
);
2411 return isl_aff_nonneg_basic_set(aff1
);
2414 /* Return a basic set containing those elements in the shared domain space
2415 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2417 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2418 __isl_take isl_aff
*aff2
)
2420 aff1
= isl_aff_sub(aff1
, aff2
);
2422 return isl_aff_pos_basic_set(aff1
);
2425 /* Return a set containing those elements in the shared space
2426 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2428 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2429 __isl_take isl_aff
*aff2
)
2431 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2434 /* Return a set containing those elements in the shared domain space
2435 * of aff1 and aff2 where aff1 is greater than aff2.
2437 * If either of the two inputs is NaN, then the result is empty,
2438 * as comparisons with NaN always return false.
2440 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2441 __isl_take isl_aff
*aff2
)
2443 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2446 /* Return a basic set containing those elements in the shared space
2447 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2449 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2450 __isl_take isl_aff
*aff2
)
2452 return isl_aff_ge_basic_set(aff2
, aff1
);
2455 /* Return a basic set containing those elements in the shared domain space
2456 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2458 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2459 __isl_take isl_aff
*aff2
)
2461 return isl_aff_gt_basic_set(aff2
, aff1
);
2464 /* Return a set containing those elements in the shared space
2465 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2467 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2468 __isl_take isl_aff
*aff2
)
2470 return isl_aff_ge_set(aff2
, aff1
);
2473 /* Return a set containing those elements in the shared domain space
2474 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2476 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2477 __isl_take isl_aff
*aff2
)
2479 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2482 /* Return a basic set containing those elements in the shared space
2483 * of aff1 and aff2 where aff1 and aff2 are equal.
2485 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2486 __isl_take isl_aff
*aff2
)
2488 aff1
= isl_aff_sub(aff1
, aff2
);
2490 return isl_aff_zero_basic_set(aff1
);
2493 /* Return a set containing those elements in the shared space
2494 * of aff1 and aff2 where aff1 and aff2 are equal.
2496 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2497 __isl_take isl_aff
*aff2
)
2499 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2502 /* Return a set containing those elements in the shared domain space
2503 * of aff1 and aff2 where aff1 and aff2 are not equal.
2505 * If either of the two inputs is NaN, then the result is empty,
2506 * as comparisons with NaN always return false.
2508 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2509 __isl_take isl_aff
*aff2
)
2511 isl_set
*set_lt
, *set_gt
;
2513 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2514 isl_aff_copy(aff2
));
2515 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2516 return isl_set_union_disjoint(set_lt
, set_gt
);
2519 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2520 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2522 aff1
= isl_aff_add(aff1
, aff2
);
2523 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2527 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2530 return isl_bool_error
;
2532 return isl_bool_false
;
2536 #define TYPE isl_aff
2538 #include "check_type_range_templ.c"
2540 /* Check whether the given affine expression has non-zero coefficient
2541 * for any dimension in the given range or if any of these dimensions
2542 * appear with non-zero coefficients in any of the integer divisions
2543 * involved in the affine expression.
2545 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2546 enum isl_dim_type type
, unsigned first
, unsigned n
)
2550 isl_bool involves
= isl_bool_false
;
2553 return isl_bool_error
;
2555 return isl_bool_false
;
2556 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2557 return isl_bool_error
;
2559 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2563 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2564 for (i
= 0; i
< n
; ++i
)
2565 if (active
[first
+ i
]) {
2566 involves
= isl_bool_true
;
2575 return isl_bool_error
;
2578 /* Does "aff" involve any local variables, i.e., integer divisions?
2580 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2584 n
= isl_aff_dim(aff
, isl_dim_div
);
2586 return isl_bool_error
;
2587 return isl_bool_ok(n
> 0);
2590 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2591 enum isl_dim_type type
, unsigned first
, unsigned n
)
2597 if (type
== isl_dim_out
)
2598 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2599 "cannot drop output/set dimension",
2600 return isl_aff_free(aff
));
2601 if (type
== isl_dim_in
)
2603 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2606 ctx
= isl_aff_get_ctx(aff
);
2607 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2608 return isl_aff_free(aff
);
2610 aff
= isl_aff_cow(aff
);
2614 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2616 return isl_aff_free(aff
);
2618 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2619 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2621 return isl_aff_free(aff
);
2626 /* Is the domain of "aff" a product?
2628 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2630 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2634 #define TYPE isl_aff
2635 #include <isl_domain_factor_templ.c>
2637 /* Project the domain of the affine expression onto its parameter space.
2638 * The affine expression may not involve any of the domain dimensions.
2640 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2645 n
= isl_aff_dim(aff
, isl_dim_in
);
2647 return isl_aff_free(aff
);
2648 aff
= isl_aff_drop_domain(aff
, 0, n
);
2649 space
= isl_aff_get_domain_space(aff
);
2650 space
= isl_space_params(space
);
2651 aff
= isl_aff_reset_domain_space(aff
, space
);
2655 /* Convert an affine expression defined over a parameter domain
2656 * into one that is defined over a zero-dimensional set.
2658 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2660 isl_local_space
*ls
;
2662 ls
= isl_aff_take_domain_local_space(aff
);
2663 ls
= isl_local_space_set_from_params(ls
);
2664 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2669 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2670 enum isl_dim_type type
, unsigned first
, unsigned n
)
2676 if (type
== isl_dim_out
)
2677 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2678 "cannot insert output/set dimensions",
2679 return isl_aff_free(aff
));
2680 if (type
== isl_dim_in
)
2682 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2685 ctx
= isl_aff_get_ctx(aff
);
2686 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2687 return isl_aff_free(aff
);
2689 aff
= isl_aff_cow(aff
);
2693 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2695 return isl_aff_free(aff
);
2697 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2698 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2700 return isl_aff_free(aff
);
2705 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2706 enum isl_dim_type type
, unsigned n
)
2710 pos
= isl_aff_dim(aff
, type
);
2712 return isl_aff_free(aff
);
2714 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2717 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2718 * to dimensions of "dst_type" at "dst_pos".
2720 * We only support moving input dimensions to parameters and vice versa.
2722 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2723 enum isl_dim_type dst_type
, unsigned dst_pos
,
2724 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2728 isl_size src_off
, dst_off
;
2733 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2734 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2737 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2738 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2739 "cannot move output/set dimension",
2740 return isl_aff_free(aff
));
2741 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2742 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2743 "cannot move divs", return isl_aff_free(aff
));
2744 if (dst_type
== isl_dim_in
)
2745 dst_type
= isl_dim_set
;
2746 if (src_type
== isl_dim_in
)
2747 src_type
= isl_dim_set
;
2749 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2750 return isl_aff_free(aff
);
2751 if (dst_type
== src_type
)
2752 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2753 "moving dims within the same type not supported",
2754 return isl_aff_free(aff
));
2756 aff
= isl_aff_cow(aff
);
2757 src_off
= isl_aff_domain_offset(aff
, src_type
);
2758 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2759 if (src_off
< 0 || dst_off
< 0)
2760 return isl_aff_free(aff
);
2762 g_src_pos
= 1 + src_off
+ src_pos
;
2763 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2764 if (dst_type
> src_type
)
2767 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2768 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2769 src_type
, src_pos
, n
);
2770 if (!aff
->v
|| !aff
->ls
)
2771 return isl_aff_free(aff
);
2773 aff
= sort_divs(aff
);
2778 /* Return a zero isl_aff in the given space.
2780 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2781 * interface over all piecewise types.
2783 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2785 isl_local_space
*ls
;
2787 ls
= isl_local_space_from_space(isl_space_domain(space
));
2788 return isl_aff_zero_on_domain(ls
);
2791 #define isl_aff_involves_nan isl_aff_is_nan
2794 #define PW isl_pw_aff
2798 #define EL_IS_ZERO is_empty
2802 #define IS_ZERO is_empty
2805 #undef DEFAULT_IS_ZERO
2806 #define DEFAULT_IS_ZERO 0
2808 #include <isl_pw_templ.c>
2809 #include <isl_pw_un_op_templ.c>
2810 #include <isl_pw_add_constant_val_templ.c>
2811 #include <isl_pw_bind_domain_templ.c>
2812 #include <isl_pw_eval.c>
2813 #include <isl_pw_hash.c>
2814 #include <isl_pw_insert_dims_templ.c>
2815 #include <isl_pw_insert_domain_templ.c>
2816 #include <isl_pw_move_dims_templ.c>
2817 #include <isl_pw_neg_templ.c>
2818 #include <isl_pw_pullback_templ.c>
2819 #include <isl_pw_sub_templ.c>
2820 #include <isl_pw_union_opt.c>
2825 #include <isl_union_single.c>
2826 #include <isl_union_neg.c>
2831 #include <isl_union_pw_templ.c>
2833 /* Compute a piecewise quasi-affine expression with a domain that
2834 * is the union of those of pwaff1 and pwaff2 and such that on each
2835 * cell, the quasi-affine expression is the maximum of those of pwaff1
2836 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2837 * cell, then the associated expression is the defined one.
2839 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2840 __isl_take isl_pw_aff
*pwaff2
)
2842 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2843 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2846 /* Compute a piecewise quasi-affine expression with a domain that
2847 * is the union of those of pwaff1 and pwaff2 and such that on each
2848 * cell, the quasi-affine expression is the minimum of those of pwaff1
2849 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2850 * cell, then the associated expression is the defined one.
2852 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2853 __isl_take isl_pw_aff
*pwaff2
)
2855 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2856 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2859 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2860 __isl_take isl_pw_aff
*pwaff2
, int max
)
2863 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2865 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2868 /* Is the domain of "pa" a product?
2870 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2872 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2876 #define TYPE isl_pw_aff
2877 #include <isl_domain_factor_templ.c>
2879 /* Return a set containing those elements in the domain
2880 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2881 * does not satisfy "fn" (if complement is 1).
2883 * The pieces with a NaN never belong to the result since
2884 * NaN does not satisfy any property.
2886 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2887 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2889 int complement
, void *user
)
2897 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2899 for (i
= 0; i
< pwaff
->n
; ++i
) {
2900 isl_basic_set
*bset
;
2901 isl_set
*set_i
, *locus
;
2904 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2907 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2908 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2909 locus
= isl_set_from_basic_set(bset
);
2910 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2912 set_i
= isl_set_subtract(set_i
, locus
);
2914 set_i
= isl_set_intersect(set_i
, locus
);
2915 set
= isl_set_union_disjoint(set
, set_i
);
2918 isl_pw_aff_free(pwaff
);
2923 /* Return a set containing those elements in the domain
2924 * of "pa" where it is positive.
2926 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2928 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is non-negative.
2934 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2936 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2939 /* Return a set containing those elements in the domain
2940 * of pwaff where it is zero.
2942 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2944 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2947 /* Return a set containing those elements in the domain
2948 * of pwaff where it is not zero.
2950 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2952 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2955 /* Bind the affine function "aff" to the parameter "id",
2956 * returning the elements in the domain where the affine expression
2957 * is equal to the parameter.
2959 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2960 __isl_take isl_id
*id
)
2965 space
= isl_aff_get_domain_space(aff
);
2966 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2968 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2969 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2971 return isl_aff_eq_basic_set(aff
, aff_id
);
2974 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2975 * "rational" should not be set.
2977 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2978 int rational
, void *user
)
2985 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2986 "rational binding not supported", goto error
);
2987 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2993 /* Bind the piecewise affine function "pa" to the parameter "id",
2994 * returning the elements in the domain where the expression
2995 * is equal to the parameter.
2997 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2998 __isl_take isl_id
*id
)
3002 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3008 /* Return a set containing those elements in the shared domain
3009 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3011 * We compute the difference on the shared domain and then construct
3012 * the set of values where this difference is non-negative.
3013 * If strict is set, we first subtract 1 from the difference.
3014 * If equal is set, we only return the elements where pwaff1 and pwaff2
3017 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3018 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3020 isl_set
*set1
, *set2
;
3022 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3023 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3024 set1
= isl_set_intersect(set1
, set2
);
3025 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3026 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3027 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3030 isl_space
*space
= isl_set_get_space(set1
);
3032 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3033 aff
= isl_aff_add_constant_si(aff
, -1);
3034 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3039 return isl_pw_aff_zero_set(pwaff1
);
3040 return isl_pw_aff_nonneg_set(pwaff1
);
3043 /* Return a set containing those elements in the shared domain
3044 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3046 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3047 __isl_take isl_pw_aff
*pwaff2
)
3049 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3050 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3053 /* Return a set containing those elements in the shared domain
3054 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3056 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3057 __isl_take isl_pw_aff
*pwaff2
)
3059 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3060 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3063 /* Return a set containing those elements in the shared domain
3064 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3066 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3067 __isl_take isl_pw_aff
*pwaff2
)
3069 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3070 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3073 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3074 __isl_take isl_pw_aff
*pwaff2
)
3076 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3079 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3080 __isl_take isl_pw_aff
*pwaff2
)
3082 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3085 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3086 * where the function values are ordered in the same way as "order",
3087 * which returns a set in the shared domain of its two arguments.
3089 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3090 * We first pull back the two functions such that they are defined on
3091 * the domain [A -> B]. Then we apply "order", resulting in a set
3092 * in the space [A -> B]. Finally, we unwrap this set to obtain
3093 * a map in the space A -> B.
3095 static __isl_give isl_map
*isl_pw_aff_order_map(
3096 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3097 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3098 __isl_take isl_pw_aff
*pa2
))
3100 isl_space
*space1
, *space2
;
3104 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3105 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3106 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3107 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3108 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3109 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3110 ma
= isl_multi_aff_range_map(space1
);
3111 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3112 set
= order(pa1
, pa2
);
3114 return isl_set_unwrap(set
);
3117 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3118 * where the function values are equal.
3120 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3121 __isl_take isl_pw_aff
*pa2
)
3123 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3126 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3127 * where the function value of "pa1" is less than or equal to
3128 * the function value of "pa2".
3130 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3131 __isl_take isl_pw_aff
*pa2
)
3133 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3136 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3137 * where the function value of "pa1" is less than the function value of "pa2".
3139 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3140 __isl_take isl_pw_aff
*pa2
)
3142 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3145 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3146 * where the function value of "pa1" is greater than or equal to
3147 * the function value of "pa2".
3149 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3150 __isl_take isl_pw_aff
*pa2
)
3152 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3155 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3156 * where the function value of "pa1" is greater than the function value
3159 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3160 __isl_take isl_pw_aff
*pa2
)
3162 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3165 /* Return a set containing those elements in the shared domain
3166 * of the elements of list1 and list2 where each element in list1
3167 * has the relation specified by "fn" with each element in list2.
3169 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3170 __isl_take isl_pw_aff_list
*list2
,
3171 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3172 __isl_take isl_pw_aff
*pwaff2
))
3178 if (!list1
|| !list2
)
3181 ctx
= isl_pw_aff_list_get_ctx(list1
);
3182 if (list1
->n
< 1 || list2
->n
< 1)
3183 isl_die(ctx
, isl_error_invalid
,
3184 "list should contain at least one element", goto error
);
3186 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3187 for (i
= 0; i
< list1
->n
; ++i
)
3188 for (j
= 0; j
< list2
->n
; ++j
) {
3191 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3192 isl_pw_aff_copy(list2
->p
[j
]));
3193 set
= isl_set_intersect(set
, set_ij
);
3196 isl_pw_aff_list_free(list1
);
3197 isl_pw_aff_list_free(list2
);
3200 isl_pw_aff_list_free(list1
);
3201 isl_pw_aff_list_free(list2
);
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 equal to each element in list2.
3209 __isl_give isl_set
*isl_pw_aff_list_eq_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_eq_set
);
3215 __isl_give isl_set
*isl_pw_aff_list_ne_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_ne_set
);
3221 /* Return a set containing those elements in the shared domain
3222 * of the elements of list1 and list2 where each element in list1
3223 * is less than or equal to each element in list2.
3225 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3226 __isl_take isl_pw_aff_list
*list2
)
3228 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3231 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3232 __isl_take isl_pw_aff_list
*list2
)
3234 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3237 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3238 __isl_take isl_pw_aff_list
*list2
)
3240 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3243 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3244 __isl_take isl_pw_aff_list
*list2
)
3246 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3250 /* Return a set containing those elements in the shared domain
3251 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3253 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3254 __isl_take isl_pw_aff
*pwaff2
)
3256 isl_set
*set_lt
, *set_gt
;
3258 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3259 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3260 isl_pw_aff_copy(pwaff2
));
3261 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3262 return isl_set_union_disjoint(set_lt
, set_gt
);
3265 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3270 if (isl_int_is_one(v
))
3272 if (!isl_int_is_pos(v
))
3273 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3274 "factor needs to be positive",
3275 return isl_pw_aff_free(pwaff
));
3276 pwaff
= isl_pw_aff_cow(pwaff
);
3282 for (i
= 0; i
< pwaff
->n
; ++i
) {
3283 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3284 if (!pwaff
->p
[i
].aff
)
3285 return isl_pw_aff_free(pwaff
);
3291 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3293 return isl_pw_aff_un_op(pwaff
, &isl_aff_floor
);
3296 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3298 return isl_pw_aff_un_op(pwaff
, &isl_aff_ceil
);
3301 /* Assuming that "cond1" and "cond2" are disjoint,
3302 * return an affine expression that is equal to pwaff1 on cond1
3303 * and to pwaff2 on cond2.
3305 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3306 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3307 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3309 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3310 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3312 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3315 /* Return an affine expression that is equal to pwaff_true for elements
3316 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3318 * That is, return cond ? pwaff_true : pwaff_false;
3320 * If "cond" involves and NaN, then we conservatively return a NaN
3321 * on its entire domain. In principle, we could consider the pieces
3322 * where it is NaN separately from those where it is not.
3324 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3325 * then only use the domain of "cond" to restrict the domain.
3327 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3328 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3330 isl_set
*cond_true
, *cond_false
;
3335 if (isl_pw_aff_involves_nan(cond
)) {
3336 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3337 isl_local_space
*ls
= isl_local_space_from_space(space
);
3338 isl_pw_aff_free(cond
);
3339 isl_pw_aff_free(pwaff_true
);
3340 isl_pw_aff_free(pwaff_false
);
3341 return isl_pw_aff_nan_on_domain(ls
);
3344 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3345 isl_pw_aff_get_space(pwaff_false
));
3346 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3347 isl_pw_aff_get_space(pwaff_true
));
3348 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3354 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3355 isl_pw_aff_free(pwaff_false
);
3356 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3359 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3360 cond_false
= isl_pw_aff_zero_set(cond
);
3361 return isl_pw_aff_select(cond_true
, pwaff_true
,
3362 cond_false
, pwaff_false
);
3364 isl_pw_aff_free(cond
);
3365 isl_pw_aff_free(pwaff_true
);
3366 isl_pw_aff_free(pwaff_false
);
3370 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3375 return isl_bool_error
;
3377 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3378 return isl_bool_ok(pos
== -1);
3381 /* Check whether pwaff is a piecewise constant.
3383 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3388 return isl_bool_error
;
3390 for (i
= 0; i
< pwaff
->n
; ++i
) {
3391 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3392 if (is_cst
< 0 || !is_cst
)
3396 return isl_bool_true
;
3399 /* Return the product of "aff1" and "aff2".
3401 * If either of the two is NaN, then the result is NaN.
3403 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3405 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3406 __isl_take isl_aff
*aff2
)
3411 if (isl_aff_is_nan(aff1
)) {
3415 if (isl_aff_is_nan(aff2
)) {
3420 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3421 return isl_aff_mul(aff2
, aff1
);
3423 if (!isl_aff_is_cst(aff2
))
3424 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3425 "at least one affine expression should be constant",
3428 aff1
= isl_aff_cow(aff1
);
3432 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3433 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3443 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3445 * If either of the two is NaN, then the result is NaN.
3446 * A division by zero also results in NaN.
3448 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3449 __isl_take isl_aff
*aff2
)
3451 isl_bool is_cst
, is_zero
;
3457 if (isl_aff_is_nan(aff1
)) {
3461 if (isl_aff_is_nan(aff2
)) {
3466 is_cst
= isl_aff_is_cst(aff2
);
3470 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3471 "second argument should be a constant", goto error
);
3472 is_zero
= isl_aff_plain_is_zero(aff2
);
3476 return set_nan_free(aff1
, aff2
);
3478 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3480 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3481 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3484 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3485 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3488 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3489 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3500 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3501 __isl_take isl_pw_aff
*pwaff2
)
3503 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3504 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3507 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3508 __isl_take isl_pw_aff
*pwaff2
)
3510 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3513 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3514 __isl_take isl_pw_aff
*pwaff2
)
3516 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3517 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3520 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3522 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3523 __isl_take isl_pw_aff
*pa2
)
3527 is_cst
= isl_pw_aff_is_cst(pa2
);
3531 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3532 "second argument should be a piecewise constant",
3534 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3535 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3537 isl_pw_aff_free(pa1
);
3538 isl_pw_aff_free(pa2
);
3542 /* Compute the quotient of the integer division of "pa1" by "pa2"
3543 * with rounding towards zero.
3544 * "pa2" is assumed to be a piecewise constant.
3546 * In particular, return
3548 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3551 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3552 __isl_take isl_pw_aff
*pa2
)
3558 is_cst
= isl_pw_aff_is_cst(pa2
);
3562 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3563 "second argument should be a piecewise constant",
3566 pa1
= isl_pw_aff_div(pa1
, pa2
);
3568 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3569 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3570 c
= isl_pw_aff_ceil(pa1
);
3571 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3573 isl_pw_aff_free(pa1
);
3574 isl_pw_aff_free(pa2
);
3578 /* Compute the remainder of the integer division of "pa1" by "pa2"
3579 * with rounding towards zero.
3580 * "pa2" is assumed to be a piecewise constant.
3582 * In particular, return
3584 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3587 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3588 __isl_take isl_pw_aff
*pa2
)
3593 is_cst
= isl_pw_aff_is_cst(pa2
);
3597 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3598 "second argument should be a piecewise constant",
3600 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3601 res
= isl_pw_aff_mul(pa2
, res
);
3602 res
= isl_pw_aff_sub(pa1
, res
);
3605 isl_pw_aff_free(pa1
);
3606 isl_pw_aff_free(pa2
);
3610 /* Does either of "pa1" or "pa2" involve any NaN2?
3612 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3613 __isl_keep isl_pw_aff
*pa2
)
3617 has_nan
= isl_pw_aff_involves_nan(pa1
);
3618 if (has_nan
< 0 || has_nan
)
3620 return isl_pw_aff_involves_nan(pa2
);
3623 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3624 * by a NaN on their shared domain.
3626 * In principle, the result could be refined to only being NaN
3627 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3629 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3630 __isl_take isl_pw_aff
*pa2
)
3632 isl_local_space
*ls
;
3636 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3637 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3638 pa
= isl_pw_aff_nan_on_domain(ls
);
3639 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3644 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3645 __isl_take isl_pw_aff
*pwaff2
)
3650 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3651 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3652 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3653 isl_pw_aff_copy(pwaff2
));
3654 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3655 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3658 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3659 __isl_take isl_pw_aff
*pwaff2
)
3664 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3665 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3666 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3667 isl_pw_aff_copy(pwaff2
));
3668 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3669 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3672 /* Return an expression for the minimum (if "max" is not set) or
3673 * the maximum (if "max" is set) of "pa1" and "pa2".
3674 * If either expression involves any NaN, then return a NaN
3675 * on the shared domain as result.
3677 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3678 __isl_take isl_pw_aff
*pa2
, int max
)
3682 has_nan
= either_involves_nan(pa1
, pa2
);
3684 pa1
= isl_pw_aff_free(pa1
);
3686 return replace_by_nan(pa1
, pa2
);
3688 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3690 return pw_aff_max(pa1
, pa2
);
3692 return pw_aff_min(pa1
, pa2
);
3695 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3697 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3698 __isl_take isl_pw_aff
*pwaff2
)
3700 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3703 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3705 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3706 __isl_take isl_pw_aff
*pwaff2
)
3708 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3711 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3712 __isl_take isl_pw_aff_list
*list
,
3713 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3714 __isl_take isl_pw_aff
*pwaff2
))
3723 ctx
= isl_pw_aff_list_get_ctx(list
);
3725 isl_die(ctx
, isl_error_invalid
,
3726 "list should contain at least one element", goto error
);
3728 res
= isl_pw_aff_copy(list
->p
[0]);
3729 for (i
= 1; i
< list
->n
; ++i
)
3730 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3732 isl_pw_aff_list_free(list
);
3735 isl_pw_aff_list_free(list
);
3739 /* Return an isl_pw_aff that maps each element in the intersection of the
3740 * domains of the elements of list to the minimal corresponding affine
3743 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3745 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3748 /* Return an isl_pw_aff that maps each element in the intersection of the
3749 * domains of the elements of list to the maximal corresponding affine
3752 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3754 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3757 /* Mark the domains of "pwaff" as rational.
3759 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3763 pwaff
= isl_pw_aff_cow(pwaff
);
3769 for (i
= 0; i
< pwaff
->n
; ++i
) {
3770 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3771 if (!pwaff
->p
[i
].set
)
3772 return isl_pw_aff_free(pwaff
);
3778 /* Mark the domains of the elements of "list" as rational.
3780 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3781 __isl_take isl_pw_aff_list
*list
)
3791 for (i
= 0; i
< n
; ++i
) {
3794 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3795 pa
= isl_pw_aff_set_rational(pa
);
3796 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3802 /* Do the parameters of "aff" match those of "space"?
3804 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3805 __isl_keep isl_space
*space
)
3807 isl_space
*aff_space
;
3811 return isl_bool_error
;
3813 aff_space
= isl_aff_get_domain_space(aff
);
3815 match
= isl_space_has_equal_params(space
, aff_space
);
3817 isl_space_free(aff_space
);
3821 /* Check that the domain space of "aff" matches "space".
3823 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3824 __isl_keep isl_space
*space
)
3826 isl_space
*aff_space
;
3830 return isl_stat_error
;
3832 aff_space
= isl_aff_get_domain_space(aff
);
3834 match
= isl_space_has_equal_params(space
, aff_space
);
3838 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3839 "parameters don't match", goto error
);
3840 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3841 aff_space
, isl_dim_set
);
3845 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3846 "domains don't match", goto error
);
3847 isl_space_free(aff_space
);
3850 isl_space_free(aff_space
);
3851 return isl_stat_error
;
3854 /* Return the shared (universe) domain of the elements of "ma".
3856 * Since an isl_multi_aff (and an isl_aff) is always total,
3857 * the domain is always the universe set in its domain space.
3858 * This is a helper function for use in the generic isl_multi_*_bind.
3860 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3861 __isl_take isl_multi_aff
*ma
)
3865 space
= isl_multi_aff_get_space(ma
);
3866 isl_multi_aff_free(ma
);
3868 return isl_basic_set_universe(isl_space_domain(space
));
3874 #include <isl_multi_no_explicit_domain.c>
3875 #include <isl_multi_templ.c>
3876 #include <isl_multi_add_constant_templ.c>
3877 #include <isl_multi_apply_set.c>
3878 #include <isl_multi_arith_templ.c>
3879 #include <isl_multi_bind_domain_templ.c>
3880 #include <isl_multi_cmp.c>
3881 #include <isl_multi_dim_id_templ.c>
3882 #include <isl_multi_dims.c>
3883 #include <isl_multi_floor.c>
3884 #include <isl_multi_from_base_templ.c>
3885 #include <isl_multi_identity_templ.c>
3886 #include <isl_multi_insert_domain_templ.c>
3887 #include <isl_multi_locals_templ.c>
3888 #include <isl_multi_move_dims_templ.c>
3889 #include <isl_multi_nan_templ.c>
3890 #include <isl_multi_product_templ.c>
3891 #include <isl_multi_splice_templ.c>
3892 #include <isl_multi_tuple_id_templ.c>
3893 #include <isl_multi_unbind_params_templ.c>
3894 #include <isl_multi_zero_templ.c>
3898 #include <isl_multi_gist.c>
3901 #define DOMBASE basic_set
3902 #include <isl_multi_bind_templ.c>
3904 /* Construct an isl_multi_aff living in "space" that corresponds
3905 * to the affine transformation matrix "mat".
3907 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3908 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3911 isl_local_space
*ls
= NULL
;
3912 isl_multi_aff
*ma
= NULL
;
3913 isl_size n_row
, n_col
, n_out
, total
;
3919 ctx
= isl_mat_get_ctx(mat
);
3921 n_row
= isl_mat_rows(mat
);
3922 n_col
= isl_mat_cols(mat
);
3923 n_out
= isl_space_dim(space
, isl_dim_out
);
3924 total
= isl_space_dim(space
, isl_dim_all
);
3925 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3928 isl_die(ctx
, isl_error_invalid
,
3929 "insufficient number of rows", goto error
);
3931 isl_die(ctx
, isl_error_invalid
,
3932 "insufficient number of columns", goto error
);
3933 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3934 isl_die(ctx
, isl_error_invalid
,
3935 "dimension mismatch", goto error
);
3937 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3938 space
= isl_space_domain(space
);
3939 ls
= isl_local_space_from_space(isl_space_copy(space
));
3941 for (i
= 0; i
< n_row
- 1; ++i
) {
3945 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3948 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3949 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3950 v
= isl_vec_normalize(v
);
3951 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3952 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3955 isl_space_free(space
);
3956 isl_local_space_free(ls
);
3960 isl_space_free(space
);
3961 isl_local_space_free(ls
);
3963 isl_multi_aff_free(ma
);
3967 /* Return the constant terms of the affine expressions of "ma".
3969 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3970 __isl_keep isl_multi_aff
*ma
)
3977 n
= isl_multi_aff_size(ma
);
3980 space
= isl_space_range(isl_multi_aff_get_space(ma
));
3981 space
= isl_space_drop_all_params(space
);
3982 mv
= isl_multi_val_zero(space
);
3984 for (i
= 0; i
< n
; ++i
) {
3988 aff
= isl_multi_aff_get_at(ma
, i
);
3989 val
= isl_aff_get_constant_val(aff
);
3991 mv
= isl_multi_val_set_at(mv
, i
, val
);
3997 /* Remove any internal structure of the domain of "ma".
3998 * If there is any such internal structure in the input,
3999 * then the name of the corresponding space is also removed.
4001 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4002 __isl_take isl_multi_aff
*ma
)
4009 if (!ma
->space
->nested
[0])
4012 space
= isl_multi_aff_get_space(ma
);
4013 space
= isl_space_flatten_domain(space
);
4014 ma
= isl_multi_aff_reset_space(ma
, space
);
4019 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4020 * of the space to its domain.
4022 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4026 isl_local_space
*ls
;
4031 if (!isl_space_is_map(space
))
4032 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4033 "not a map space", goto error
);
4035 n_in
= isl_space_dim(space
, isl_dim_in
);
4038 space
= isl_space_domain_map(space
);
4040 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4042 isl_space_free(space
);
4046 space
= isl_space_domain(space
);
4047 ls
= isl_local_space_from_space(space
);
4048 for (i
= 0; i
< n_in
; ++i
) {
4051 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4053 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4055 isl_local_space_free(ls
);
4058 isl_space_free(space
);
4062 /* This function performs the same operation as isl_multi_aff_domain_map,
4063 * but is considered as a function on an isl_space when exported.
4065 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4066 __isl_take isl_space
*space
)
4068 return isl_multi_aff_domain_map(space
);
4071 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4072 * of the space to its range.
4074 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4077 isl_size n_in
, n_out
;
4078 isl_local_space
*ls
;
4083 if (!isl_space_is_map(space
))
4084 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4085 "not a map space", goto error
);
4087 n_in
= isl_space_dim(space
, isl_dim_in
);
4088 n_out
= isl_space_dim(space
, isl_dim_out
);
4089 if (n_in
< 0 || n_out
< 0)
4091 space
= isl_space_range_map(space
);
4093 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4095 isl_space_free(space
);
4099 space
= isl_space_domain(space
);
4100 ls
= isl_local_space_from_space(space
);
4101 for (i
= 0; i
< n_out
; ++i
) {
4104 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4105 isl_dim_set
, n_in
+ i
);
4106 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4108 isl_local_space_free(ls
);
4111 isl_space_free(space
);
4115 /* This function performs the same operation as isl_multi_aff_range_map,
4116 * but is considered as a function on an isl_space when exported.
4118 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4119 __isl_take isl_space
*space
)
4121 return isl_multi_aff_range_map(space
);
4124 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4125 * of the space to its domain.
4127 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4128 __isl_take isl_space
*space
)
4130 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4133 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4134 * but is considered as a function on an isl_space when exported.
4136 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4137 __isl_take isl_space
*space
)
4139 return isl_pw_multi_aff_domain_map(space
);
4142 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4143 * of the space to its range.
4145 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4146 __isl_take isl_space
*space
)
4148 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4151 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4152 * but is considered as a function on an isl_space when exported.
4154 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4155 __isl_take isl_space
*space
)
4157 return isl_pw_multi_aff_range_map(space
);
4160 /* Given the space of a set and a range of set dimensions,
4161 * construct an isl_multi_aff that projects out those dimensions.
4163 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4164 __isl_take isl_space
*space
, enum isl_dim_type type
,
4165 unsigned first
, unsigned n
)
4169 isl_local_space
*ls
;
4174 if (!isl_space_is_set(space
))
4175 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4176 "expecting set space", goto error
);
4177 if (type
!= isl_dim_set
)
4178 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4179 "only set dimensions can be projected out", goto error
);
4180 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4183 dim
= isl_space_dim(space
, isl_dim_set
);
4187 space
= isl_space_from_domain(space
);
4188 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4191 return isl_multi_aff_alloc(space
);
4193 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4194 space
= isl_space_domain(space
);
4195 ls
= isl_local_space_from_space(space
);
4197 for (i
= 0; i
< first
; ++i
) {
4200 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4202 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4205 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4208 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4209 isl_dim_set
, first
+ n
+ i
);
4210 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4213 isl_local_space_free(ls
);
4216 isl_space_free(space
);
4220 /* Given the space of a set and a range of set dimensions,
4221 * construct an isl_pw_multi_aff that projects out those dimensions.
4223 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4224 __isl_take isl_space
*space
, enum isl_dim_type type
,
4225 unsigned first
, unsigned n
)
4229 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4230 return isl_pw_multi_aff_from_multi_aff(ma
);
4233 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4234 * but is considered as a function on an isl_multi_aff when exported.
4236 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4237 __isl_take isl_multi_aff
*ma
)
4239 return isl_pw_multi_aff_from_multi_aff(ma
);
4242 /* Create a piecewise multi-affine expression in the given space that maps each
4243 * input dimension to the corresponding output dimension.
4245 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4246 __isl_take isl_space
*space
)
4248 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4251 /* Create a piecewise multi expression that maps elements in the given space
4254 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4255 __isl_take isl_space
*space
)
4259 ma
= isl_multi_aff_identity_on_domain_space(space
);
4260 return isl_pw_multi_aff_from_multi_aff(ma
);
4263 /* This function performs the same operation as
4264 * isl_pw_multi_aff_identity_on_domain_space,
4265 * but is considered as a function on an isl_space when exported.
4267 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4268 __isl_take isl_space
*space
)
4270 return isl_pw_multi_aff_identity_on_domain_space(space
);
4273 /* Exploit the equalities in "eq" to simplify the affine expressions.
4275 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4276 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4280 maff
= isl_multi_aff_cow(maff
);
4284 for (i
= 0; i
< maff
->n
; ++i
) {
4285 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4286 isl_basic_set_copy(eq
));
4291 isl_basic_set_free(eq
);
4294 isl_basic_set_free(eq
);
4295 isl_multi_aff_free(maff
);
4299 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4304 maff
= isl_multi_aff_cow(maff
);
4308 for (i
= 0; i
< maff
->n
; ++i
) {
4309 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4311 return isl_multi_aff_free(maff
);
4317 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4318 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4320 maff1
= isl_multi_aff_add(maff1
, maff2
);
4321 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4325 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4328 return isl_bool_error
;
4330 return isl_bool_false
;
4333 /* Return the set of domain elements where "ma1" is lexicographically
4334 * smaller than or equal to "ma2".
4336 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4337 __isl_take isl_multi_aff
*ma2
)
4339 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4342 /* Return the set of domain elements where "ma1" is lexicographically
4343 * smaller than "ma2".
4345 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4346 __isl_take isl_multi_aff
*ma2
)
4348 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4351 /* Return the set of domain elements where "ma1" is lexicographically
4352 * greater than to "ma2". If "equal" is set, then include the domain
4353 * elements where they are equal.
4354 * Do this for the case where there are no entries.
4355 * In this case, "ma1" cannot be greater than "ma2",
4356 * but it is (greater than or) equal to "ma2".
4358 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4359 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4363 space
= isl_multi_aff_get_domain_space(ma1
);
4365 isl_multi_aff_free(ma1
);
4366 isl_multi_aff_free(ma2
);
4369 return isl_set_universe(space
);
4371 return isl_set_empty(space
);
4374 /* Return the set where entry "i" of "ma1" and "ma2"
4375 * satisfy the relation prescribed by "cmp".
4377 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4378 __isl_keep isl_multi_aff
*ma2
, int i
,
4379 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4380 __isl_take isl_aff
*aff2
))
4382 isl_aff
*aff1
, *aff2
;
4384 aff1
= isl_multi_aff_get_at(ma1
, i
);
4385 aff2
= isl_multi_aff_get_at(ma2
, i
);
4386 return cmp(aff1
, aff2
);
4389 /* Return the set of domain elements where "ma1" is lexicographically
4390 * greater than to "ma2". If "equal" is set, then include the domain
4391 * elements where they are equal.
4393 * In particular, for all but the final entry,
4394 * include the set of elements where this entry is strictly greater in "ma1"
4395 * and all previous entries are equal.
4396 * The final entry is also allowed to be equal in the two functions
4397 * if "equal" is set.
4399 * The case where there are no entries is handled separately.
4401 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4402 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4411 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4413 n
= isl_multi_aff_size(ma1
);
4417 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4419 space
= isl_multi_aff_get_domain_space(ma1
);
4420 res
= isl_set_empty(isl_space_copy(space
));
4421 equal_set
= isl_set_universe(space
);
4423 for (i
= 0; i
+ 1 < n
; ++i
) {
4427 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4428 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4429 res
= isl_set_union(res
, gt
);
4430 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4431 equal_set
= isl_set_intersect(equal_set
, eq
);
4433 empty
= isl_set_is_empty(equal_set
);
4434 if (empty
>= 0 && empty
)
4439 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4441 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4442 isl_multi_aff_free(ma1
);
4443 isl_multi_aff_free(ma2
);
4445 gte
= isl_set_intersect(gte
, equal_set
);
4446 return isl_set_union(res
, gte
);
4448 isl_multi_aff_free(ma1
);
4449 isl_multi_aff_free(ma2
);
4453 /* Return the set of domain elements where "ma1" is lexicographically
4454 * greater than or equal to "ma2".
4456 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4457 __isl_take isl_multi_aff
*ma2
)
4459 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4462 /* Return the set of domain elements where "ma1" is lexicographically
4463 * greater than "ma2".
4465 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4466 __isl_take isl_multi_aff
*ma2
)
4468 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4471 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4474 #define PW isl_pw_multi_aff
4476 #define BASE multi_aff
4478 #define EL_IS_ZERO is_empty
4482 #define IS_ZERO is_empty
4485 #undef DEFAULT_IS_ZERO
4486 #define DEFAULT_IS_ZERO 0
4488 #include <isl_pw_templ.c>
4489 #include <isl_pw_un_op_templ.c>
4490 #include <isl_pw_add_constant_multi_val_templ.c>
4491 #include <isl_pw_add_constant_val_templ.c>
4492 #include <isl_pw_bind_domain_templ.c>
4493 #include <isl_pw_insert_dims_templ.c>
4494 #include <isl_pw_insert_domain_templ.c>
4495 #include <isl_pw_locals_templ.c>
4496 #include <isl_pw_move_dims_templ.c>
4497 #include <isl_pw_neg_templ.c>
4498 #include <isl_pw_pullback_templ.c>
4499 #include <isl_pw_range_tuple_id_templ.c>
4500 #include <isl_pw_union_opt.c>
4503 #define BASE pw_multi_aff
4505 #include <isl_union_multi.c>
4506 #include "isl_union_locals_templ.c"
4507 #include <isl_union_neg.c>
4510 #define BASE multi_aff
4512 #include <isl_union_pw_templ.c>
4514 /* Generic function for extracting a factor from a product "pma".
4515 * "check_space" checks that the space is that of the right kind of product.
4516 * "space_factor" extracts the factor from the space.
4517 * "multi_aff_factor" extracts the factor from the constituent functions.
4519 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4520 __isl_take isl_pw_multi_aff
*pma
,
4521 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4522 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4523 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4524 __isl_take isl_multi_aff
*ma
))
4529 if (check_space(pma
) < 0)
4530 return isl_pw_multi_aff_free(pma
);
4532 space
= isl_pw_multi_aff_take_space(pma
);
4533 space
= space_factor(space
);
4535 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4538 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4539 ma
= multi_aff_factor(ma
);
4540 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4543 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4548 /* Is the range of "pma" a wrapped relation?
4550 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4551 __isl_keep isl_pw_multi_aff
*pma
)
4553 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4556 /* Check that the range of "pma" is a product.
4558 static isl_stat
pw_multi_aff_check_range_product(
4559 __isl_keep isl_pw_multi_aff
*pma
)
4563 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4565 return isl_stat_error
;
4567 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4568 "range is not a product", return isl_stat_error
);
4572 /* Given a function A -> [B -> C], extract the function A -> B.
4574 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4575 __isl_take isl_pw_multi_aff
*pma
)
4577 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4578 &isl_space_range_factor_domain
,
4579 &isl_multi_aff_range_factor_domain
);
4582 /* Given a function A -> [B -> C], extract the function A -> C.
4584 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4585 __isl_take isl_pw_multi_aff
*pma
)
4587 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4588 &isl_space_range_factor_range
,
4589 &isl_multi_aff_range_factor_range
);
4592 /* Given two piecewise multi affine expressions, return a piecewise
4593 * multi-affine expression defined on the union of the definition domains
4594 * of the inputs that is equal to the lexicographic maximum of the two
4595 * inputs on each cell. If only one of the two inputs is defined on
4596 * a given cell, then it is considered to be the maximum.
4598 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4599 __isl_take isl_pw_multi_aff
*pma1
,
4600 __isl_take isl_pw_multi_aff
*pma2
)
4602 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4603 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4604 &isl_multi_aff_lex_ge_set
);
4607 /* Given two piecewise multi affine expressions, return a piecewise
4608 * multi-affine expression defined on the union of the definition domains
4609 * of the inputs that is equal to the lexicographic minimum of the two
4610 * inputs on each cell. If only one of the two inputs is defined on
4611 * a given cell, then it is considered to be the minimum.
4613 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4614 __isl_take isl_pw_multi_aff
*pma1
,
4615 __isl_take isl_pw_multi_aff
*pma2
)
4617 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4618 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4619 &isl_multi_aff_lex_le_set
);
4622 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4623 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4625 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4626 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4627 &isl_multi_aff_add
);
4630 /* Subtract "pma2" from "pma1" and return the result.
4632 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4633 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4635 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4636 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4637 &isl_multi_aff_sub
);
4640 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4641 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4643 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4646 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4647 * with the actual sum on the shared domain and
4648 * the defined expression on the symmetric difference of the domains.
4650 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4651 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4653 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4656 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4657 * with the actual sum on the shared domain and
4658 * the defined expression on the symmetric difference of the domains.
4660 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4661 __isl_take isl_union_pw_multi_aff
*upma1
,
4662 __isl_take isl_union_pw_multi_aff
*upma2
)
4664 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4667 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4668 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4670 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4671 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4675 isl_pw_multi_aff
*res
;
4677 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4680 n
= pma1
->n
* pma2
->n
;
4681 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4682 isl_space_copy(pma2
->dim
));
4683 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4685 for (i
= 0; i
< pma1
->n
; ++i
) {
4686 for (j
= 0; j
< pma2
->n
; ++j
) {
4690 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4691 isl_set_copy(pma2
->p
[j
].set
));
4692 ma
= isl_multi_aff_product(
4693 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4694 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4695 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4699 isl_pw_multi_aff_free(pma1
);
4700 isl_pw_multi_aff_free(pma2
);
4703 isl_pw_multi_aff_free(pma1
);
4704 isl_pw_multi_aff_free(pma2
);
4708 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4709 * denominator "denom".
4710 * "denom" is allowed to be negative, in which case the actual denominator
4711 * is -denom and the expressions are added instead.
4713 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4714 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4720 first
= isl_seq_first_non_zero(c
, n
);
4724 sign
= isl_int_sgn(denom
);
4726 isl_int_abs(d
, denom
);
4727 for (i
= first
; i
< n
; ++i
) {
4730 if (isl_int_is_zero(c
[i
]))
4732 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4733 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4734 aff_i
= isl_aff_scale_down(aff_i
, d
);
4736 aff
= isl_aff_sub(aff
, aff_i
);
4738 aff
= isl_aff_add(aff
, aff_i
);
4745 /* Extract an affine expression that expresses the output dimension "pos"
4746 * of "bmap" in terms of the parameters and input dimensions from
4748 * Note that this expression may involve integer divisions defined
4749 * in terms of parameters and input dimensions.
4750 * The equality may also involve references to earlier (but not later)
4751 * output dimensions. These are replaced by the corresponding elements
4754 * If the equality is of the form
4756 * f(i) + h(j) + a x + g(i) = 0,
4758 * with f(i) a linear combinations of the parameters and input dimensions,
4759 * g(i) a linear combination of integer divisions defined in terms of the same
4760 * and h(j) a linear combinations of earlier output dimensions,
4761 * then the affine expression is
4763 * (-f(i) - g(i))/a - h(j)/a
4765 * If the equality is of the form
4767 * f(i) + h(j) - a x + g(i) = 0,
4769 * then the affine expression is
4771 * (f(i) + g(i))/a - h(j)/(-a)
4774 * If "div" refers to an integer division (i.e., it is smaller than
4775 * the number of integer divisions), then the equality constraint
4776 * does involve an integer division (the one at position "div") that
4777 * is defined in terms of output dimensions. However, this integer
4778 * division can be eliminated by exploiting a pair of constraints
4779 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4780 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4782 * In particular, let
4784 * x = e(i) + m floor(...)
4786 * with e(i) the expression derived above and floor(...) the integer
4787 * division involving output dimensions.
4798 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4799 * = (e(i) - l) mod m
4803 * x - l = (e(i) - l) mod m
4807 * x = ((e(i) - l) mod m) + l
4809 * The variable "shift" below contains the expression -l, which may
4810 * also involve a linear combination of earlier output dimensions.
4812 static __isl_give isl_aff
*extract_aff_from_equality(
4813 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4814 __isl_keep isl_multi_aff
*ma
)
4817 isl_size n_div
, n_out
;
4819 isl_local_space
*ls
;
4820 isl_aff
*aff
, *shift
;
4823 ctx
= isl_basic_map_get_ctx(bmap
);
4824 ls
= isl_basic_map_get_local_space(bmap
);
4825 ls
= isl_local_space_domain(ls
);
4826 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4829 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4830 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4831 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4832 if (n_out
< 0 || n_div
< 0)
4834 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4835 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4836 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4837 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4839 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4840 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4841 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4844 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4845 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4846 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4847 bmap
->eq
[eq
][o_out
+ pos
]);
4849 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4852 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4853 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4854 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4855 isl_int_set_si(shift
->v
->el
[0], 1);
4856 shift
= subtract_initial(shift
, ma
, pos
,
4857 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4858 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4859 mod
= isl_val_int_from_isl_int(ctx
,
4860 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4861 mod
= isl_val_abs(mod
);
4862 aff
= isl_aff_mod_val(aff
, mod
);
4863 aff
= isl_aff_sub(aff
, shift
);
4866 isl_local_space_free(ls
);
4869 isl_local_space_free(ls
);
4874 /* Given a basic map with output dimensions defined
4875 * in terms of the parameters input dimensions and earlier
4876 * output dimensions using an equality (and possibly a pair on inequalities),
4877 * extract an isl_aff that expresses output dimension "pos" in terms
4878 * of the parameters and input dimensions.
4879 * Note that this expression may involve integer divisions defined
4880 * in terms of parameters and input dimensions.
4881 * "ma" contains the expressions corresponding to earlier output dimensions.
4883 * This function shares some similarities with
4884 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4886 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4887 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4894 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4895 if (eq
>= bmap
->n_eq
)
4896 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4897 "unable to find suitable equality", return NULL
);
4898 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4900 aff
= isl_aff_remove_unused_divs(aff
);
4904 /* Given a basic map where each output dimension is defined
4905 * in terms of the parameters and input dimensions using an equality,
4906 * extract an isl_multi_aff that expresses the output dimensions in terms
4907 * of the parameters and input dimensions.
4909 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4910 __isl_take isl_basic_map
*bmap
)
4919 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4920 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4922 ma
= isl_multi_aff_free(ma
);
4924 for (i
= 0; i
< n_out
; ++i
) {
4927 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4928 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4931 isl_basic_map_free(bmap
);
4936 /* Given a basic set where each set dimension is defined
4937 * in terms of the parameters using an equality,
4938 * extract an isl_multi_aff that expresses the set dimensions in terms
4939 * of the parameters.
4941 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4942 __isl_take isl_basic_set
*bset
)
4944 return extract_isl_multi_aff_from_basic_map(bset
);
4947 /* Create an isl_pw_multi_aff that is equivalent to
4948 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4949 * The given basic map is such that each output dimension is defined
4950 * in terms of the parameters and input dimensions using an equality.
4952 * Since some applications expect the result of isl_pw_multi_aff_from_map
4953 * to only contain integer affine expressions, we compute the floor
4954 * of the expression before returning.
4956 * Remove all constraints involving local variables without
4957 * an explicit representation (resulting in the removal of those
4958 * local variables) prior to the actual extraction to ensure
4959 * that the local spaces in which the resulting affine expressions
4960 * are created do not contain any unknown local variables.
4961 * Removing such constraints is safe because constraints involving
4962 * unknown local variables are not used to determine whether
4963 * a basic map is obviously single-valued.
4965 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4966 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4970 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4971 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4972 ma
= isl_multi_aff_floor(ma
);
4973 return isl_pw_multi_aff_alloc(domain
, ma
);
4976 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4977 * This obviously only works if the input "map" is single-valued.
4978 * If so, we compute the lexicographic minimum of the image in the form
4979 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4980 * to its lexicographic minimum.
4981 * If the input is not single-valued, we produce an error.
4983 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4984 __isl_take isl_map
*map
)
4988 isl_pw_multi_aff
*pma
;
4990 sv
= isl_map_is_single_valued(map
);
4994 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4995 "map is not single-valued", goto error
);
4996 map
= isl_map_make_disjoint(map
);
5000 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5002 for (i
= 0; i
< map
->n
; ++i
) {
5003 isl_pw_multi_aff
*pma_i
;
5004 isl_basic_map
*bmap
;
5005 bmap
= isl_basic_map_copy(map
->p
[i
]);
5006 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5007 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5017 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5018 * taking into account that the output dimension at position "d"
5019 * can be represented as
5021 * x = floor((e(...) + c1) / m)
5023 * given that constraint "i" is of the form
5025 * e(...) + c1 - m x >= 0
5028 * Let "map" be of the form
5032 * We construct a mapping
5034 * A -> [A -> x = floor(...)]
5036 * apply that to the map, obtaining
5038 * [A -> x = floor(...)] -> B
5040 * and equate dimension "d" to x.
5041 * We then compute a isl_pw_multi_aff representation of the resulting map
5042 * and plug in the mapping above.
5044 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5045 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5048 isl_space
*space
= NULL
;
5049 isl_local_space
*ls
;
5057 isl_pw_multi_aff
*pma
;
5060 is_set
= isl_map_is_set(map
);
5064 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5065 ctx
= isl_map_get_ctx(map
);
5066 space
= isl_space_domain(isl_map_get_space(map
));
5067 n_in
= isl_space_dim(space
, isl_dim_set
);
5068 n
= isl_space_dim(space
, isl_dim_all
);
5069 if (n_in
< 0 || n
< 0)
5072 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5074 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5075 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5077 isl_basic_map_free(hull
);
5079 ls
= isl_local_space_from_space(isl_space_copy(space
));
5080 aff
= isl_aff_alloc_vec(ls
, v
);
5081 aff
= isl_aff_floor(aff
);
5083 isl_space_free(space
);
5084 ma
= isl_multi_aff_from_aff(aff
);
5086 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5087 ma
= isl_multi_aff_range_product(ma
,
5088 isl_multi_aff_from_aff(aff
));
5091 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5092 map
= isl_map_apply_domain(map
, insert
);
5093 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5094 pma
= isl_pw_multi_aff_from_map(map
);
5095 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5099 isl_space_free(space
);
5101 isl_basic_map_free(hull
);
5105 /* Is constraint "c" of the form
5107 * e(...) + c1 - m x >= 0
5111 * -e(...) + c2 + m x >= 0
5113 * where m > 1 and e only depends on parameters and input dimensions?
5115 * "offset" is the offset of the output dimensions
5116 * "pos" is the position of output dimension x.
5118 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5120 if (isl_int_is_zero(c
[offset
+ d
]))
5122 if (isl_int_is_one(c
[offset
+ d
]))
5124 if (isl_int_is_negone(c
[offset
+ d
]))
5126 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5128 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5129 total
- (offset
+ d
+ 1)) != -1)
5134 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5136 * As a special case, we first check if there is any pair of constraints,
5137 * shared by all the basic maps in "map" that force a given dimension
5138 * to be equal to the floor of some affine combination of the input dimensions.
5140 * In particular, if we can find two constraints
5142 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5146 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5148 * where m > 1 and e only depends on parameters and input dimensions,
5151 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5153 * then we know that we can take
5155 * x = floor((e(...) + c1) / m)
5157 * without having to perform any computation.
5159 * Note that we know that
5163 * If c1 + c2 were 0, then we would have detected an equality during
5164 * simplification. If c1 + c2 were negative, then we would have detected
5167 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5168 __isl_take isl_map
*map
)
5176 isl_basic_map
*hull
;
5178 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5179 dim
= isl_map_dim(map
, isl_dim_out
);
5180 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5181 if (dim
< 0 || total
< 0)
5185 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5187 for (d
= 0; d
< dim
; ++d
) {
5188 for (i
= 0; i
< n
; ++i
) {
5189 if (!is_potential_div_constraint(hull
->ineq
[i
],
5190 offset
, d
, 1 + total
))
5192 for (j
= i
+ 1; j
< n
; ++j
) {
5193 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5194 hull
->ineq
[j
] + 1, total
))
5196 isl_int_add(sum
, hull
->ineq
[i
][0],
5198 if (isl_int_abs_lt(sum
,
5199 hull
->ineq
[i
][offset
+ d
]))
5206 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5208 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5212 isl_basic_map_free(hull
);
5213 return pw_multi_aff_from_map_base(map
);
5216 isl_basic_map_free(hull
);
5220 /* Given an affine expression
5222 * [A -> B] -> f(A,B)
5224 * construct an isl_multi_aff
5228 * such that dimension "d" in B' is set to "aff" and the remaining
5229 * dimensions are set equal to the corresponding dimensions in B.
5230 * "n_in" is the dimension of the space A.
5231 * "n_out" is the dimension of the space B.
5233 * If "is_set" is set, then the affine expression is of the form
5237 * and we construct an isl_multi_aff
5241 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5242 unsigned n_in
, unsigned n_out
, int is_set
)
5246 isl_space
*space
, *space2
;
5247 isl_local_space
*ls
;
5249 space
= isl_aff_get_domain_space(aff
);
5250 ls
= isl_local_space_from_space(isl_space_copy(space
));
5251 space2
= isl_space_copy(space
);
5253 space2
= isl_space_range(isl_space_unwrap(space2
));
5254 space
= isl_space_map_from_domain_and_range(space
, space2
);
5255 ma
= isl_multi_aff_alloc(space
);
5256 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5258 for (i
= 0; i
< n_out
; ++i
) {
5261 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5262 isl_dim_set
, n_in
+ i
);
5263 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5266 isl_local_space_free(ls
);
5271 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5272 * taking into account that the dimension at position "d" can be written as
5274 * x = m a + f(..) (1)
5276 * where m is equal to "gcd".
5277 * "i" is the index of the equality in "hull" that defines f(..).
5278 * In particular, the equality is of the form
5280 * f(..) - x + m g(existentials) = 0
5284 * -f(..) + x + m g(existentials) = 0
5286 * We basically plug (1) into "map", resulting in a map with "a"
5287 * in the range instead of "x". The corresponding isl_pw_multi_aff
5288 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5290 * Specifically, given the input map
5294 * We first wrap it into a set
5298 * and define (1) on top of the corresponding space, resulting in "aff".
5299 * We use this to create an isl_multi_aff that maps the output position "d"
5300 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5301 * We plug this into the wrapped map, unwrap the result and compute the
5302 * corresponding isl_pw_multi_aff.
5303 * The result is an expression
5311 * so that we can plug that into "aff", after extending the latter to
5317 * If "map" is actually a set, then there is no "A" space, meaning
5318 * that we do not need to perform any wrapping, and that the result
5319 * of the recursive call is of the form
5323 * which is plugged into a mapping of the form
5327 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5328 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5333 isl_local_space
*ls
;
5336 isl_pw_multi_aff
*pma
, *id
;
5342 is_set
= isl_map_is_set(map
);
5346 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5347 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5348 if (n_in
< 0 || n_out
< 0)
5350 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5355 set
= isl_map_wrap(map
);
5356 space
= isl_space_map_from_set(isl_set_get_space(set
));
5357 ma
= isl_multi_aff_identity(space
);
5358 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5359 aff
= isl_aff_alloc(ls
);
5361 isl_int_set_si(aff
->v
->el
[0], 1);
5362 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5363 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5366 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5368 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5370 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5371 set
= isl_set_preimage_multi_aff(set
, ma
);
5373 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5378 map
= isl_set_unwrap(set
);
5379 pma
= isl_pw_multi_aff_from_map(map
);
5382 space
= isl_pw_multi_aff_get_domain_space(pma
);
5383 space
= isl_space_map_from_set(space
);
5384 id
= isl_pw_multi_aff_identity(space
);
5385 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5387 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5388 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5390 isl_basic_map_free(hull
);
5394 isl_basic_map_free(hull
);
5398 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5399 * "hull" contains the equalities valid for "map".
5401 * Check if any of the output dimensions is "strided".
5402 * That is, we check if it can be written as
5406 * with m greater than 1, a some combination of existentially quantified
5407 * variables and f an expression in the parameters and input dimensions.
5408 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5410 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5413 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5414 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5423 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5424 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5425 if (n_div
< 0 || n_out
< 0)
5429 isl_basic_map_free(hull
);
5430 return pw_multi_aff_from_map_check_div(map
);
5435 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5436 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5438 for (i
= 0; i
< n_out
; ++i
) {
5439 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5440 isl_int
*eq
= hull
->eq
[j
];
5441 isl_pw_multi_aff
*res
;
5443 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5444 !isl_int_is_negone(eq
[o_out
+ i
]))
5446 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5448 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5449 n_out
- (i
+ 1)) != -1)
5451 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5452 if (isl_int_is_zero(gcd
))
5454 if (isl_int_is_one(gcd
))
5457 res
= pw_multi_aff_from_map_stride(map
, hull
,
5465 isl_basic_map_free(hull
);
5466 return pw_multi_aff_from_map_check_div(map
);
5469 isl_basic_map_free(hull
);
5473 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5475 * As a special case, we first check if all output dimensions are uniquely
5476 * defined in terms of the parameters and input dimensions over the entire
5477 * domain. If so, we extract the desired isl_pw_multi_aff directly
5478 * from the affine hull of "map" and its domain.
5480 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5483 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5487 isl_basic_map
*hull
;
5489 n
= isl_map_n_basic_map(map
);
5494 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5495 hull
= isl_basic_map_plain_affine_hull(hull
);
5496 sv
= isl_basic_map_plain_is_single_valued(hull
);
5498 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5500 isl_basic_map_free(hull
);
5502 map
= isl_map_detect_equalities(map
);
5503 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5504 sv
= isl_basic_map_plain_is_single_valued(hull
);
5506 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5508 return pw_multi_aff_from_map_check_strides(map
, hull
);
5509 isl_basic_map_free(hull
);
5515 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5516 * but is considered as a function on an isl_map when exported.
5518 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5520 return isl_pw_multi_aff_from_map(map
);
5523 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5525 return isl_pw_multi_aff_from_map(set
);
5528 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5529 * but is considered as a function on an isl_set when exported.
5531 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5533 return isl_pw_multi_aff_from_set(set
);
5536 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5539 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5541 isl_union_pw_multi_aff
**upma
= user
;
5542 isl_pw_multi_aff
*pma
;
5544 pma
= isl_pw_multi_aff_from_map(map
);
5545 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5547 return *upma
? isl_stat_ok
: isl_stat_error
;
5550 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5553 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5554 __isl_take isl_aff
*aff
)
5557 isl_pw_multi_aff
*pma
;
5559 ma
= isl_multi_aff_from_aff(aff
);
5560 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5561 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5564 /* Try and create an isl_union_pw_multi_aff that is equivalent
5565 * to the given isl_union_map.
5566 * The isl_union_map is required to be single-valued in each space.
5567 * Otherwise, an error is produced.
5569 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5570 __isl_take isl_union_map
*umap
)
5573 isl_union_pw_multi_aff
*upma
;
5575 space
= isl_union_map_get_space(umap
);
5576 upma
= isl_union_pw_multi_aff_empty(space
);
5577 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5578 upma
= isl_union_pw_multi_aff_free(upma
);
5579 isl_union_map_free(umap
);
5584 /* This function performs the same operation as
5585 * isl_union_pw_multi_aff_from_union_map,
5586 * but is considered as a function on an isl_union_map when exported.
5588 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5589 __isl_take isl_union_map
*umap
)
5591 return isl_union_pw_multi_aff_from_union_map(umap
);
5594 /* Try and create an isl_union_pw_multi_aff that is equivalent
5595 * to the given isl_union_set.
5596 * The isl_union_set is required to be a singleton in each space.
5597 * Otherwise, an error is produced.
5599 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5600 __isl_take isl_union_set
*uset
)
5602 return isl_union_pw_multi_aff_from_union_map(uset
);
5605 /* Return the piecewise affine expression "set ? 1 : 0".
5607 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5610 isl_space
*space
= isl_set_get_space(set
);
5611 isl_local_space
*ls
= isl_local_space_from_space(space
);
5612 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5613 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5615 one
= isl_aff_add_constant_si(one
, 1);
5616 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5617 set
= isl_set_complement(set
);
5618 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5623 /* Plug in "subs" for dimension "type", "pos" of "aff".
5625 * Let i be the dimension to replace and let "subs" be of the form
5629 * and "aff" of the form
5635 * (a f + d g')/(m d)
5637 * where g' is the result of plugging in "subs" in each of the integer
5640 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5641 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5647 aff
= isl_aff_cow(aff
);
5649 return isl_aff_free(aff
);
5651 ctx
= isl_aff_get_ctx(aff
);
5652 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5653 isl_die(ctx
, isl_error_invalid
,
5654 "spaces don't match", return isl_aff_free(aff
));
5655 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5657 return isl_aff_free(aff
);
5659 isl_die(ctx
, isl_error_unsupported
,
5660 "cannot handle divs yet", return isl_aff_free(aff
));
5662 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5664 return isl_aff_free(aff
);
5666 aff
->v
= isl_vec_cow(aff
->v
);
5668 return isl_aff_free(aff
);
5670 pos
+= isl_local_space_offset(aff
->ls
, type
);
5673 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5674 aff
->v
->size
, subs
->v
->size
, v
);
5680 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5681 * expressions in "maff".
5683 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5684 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5685 __isl_keep isl_aff
*subs
)
5689 maff
= isl_multi_aff_cow(maff
);
5691 return isl_multi_aff_free(maff
);
5693 if (type
== isl_dim_in
)
5696 for (i
= 0; i
< maff
->n
; ++i
) {
5697 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5700 return isl_multi_aff_free(maff
);
5706 /* Plug in "subs" for input dimension "pos" of "pma".
5708 * pma is of the form
5712 * while subs is of the form
5714 * v' = B_j(v) -> S_j
5716 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5717 * has a contribution in the result, in particular
5719 * C_ij(S_j) -> M_i(S_j)
5721 * Note that plugging in S_j in C_ij may also result in an empty set
5722 * and this contribution should simply be discarded.
5724 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5725 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5726 __isl_keep isl_pw_aff
*subs
)
5729 isl_pw_multi_aff
*res
;
5732 return isl_pw_multi_aff_free(pma
);
5734 n
= pma
->n
* subs
->n
;
5735 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5737 for (i
= 0; i
< pma
->n
; ++i
) {
5738 for (j
= 0; j
< subs
->n
; ++j
) {
5740 isl_multi_aff
*res_ij
;
5743 common
= isl_set_intersect(
5744 isl_set_copy(pma
->p
[i
].set
),
5745 isl_set_copy(subs
->p
[j
].set
));
5746 common
= isl_set_substitute(common
,
5747 pos
, subs
->p
[j
].aff
);
5748 empty
= isl_set_plain_is_empty(common
);
5749 if (empty
< 0 || empty
) {
5750 isl_set_free(common
);
5756 res_ij
= isl_multi_aff_substitute(
5757 isl_multi_aff_copy(pma
->p
[i
].maff
),
5758 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5760 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5764 isl_pw_multi_aff_free(pma
);
5767 isl_pw_multi_aff_free(pma
);
5768 isl_pw_multi_aff_free(res
);
5772 /* Compute the preimage of a range of dimensions in the affine expression "src"
5773 * under "ma" and put the result in "dst". The number of dimensions in "src"
5774 * that precede the range is given by "n_before". The number of dimensions
5775 * in the range is given by the number of output dimensions of "ma".
5776 * The number of dimensions that follow the range is given by "n_after".
5777 * If "has_denom" is set (to one),
5778 * then "src" and "dst" have an extra initial denominator.
5779 * "n_div_ma" is the number of existentials in "ma"
5780 * "n_div_bset" is the number of existentials in "src"
5781 * The resulting "dst" (which is assumed to have been allocated by
5782 * the caller) contains coefficients for both sets of existentials,
5783 * first those in "ma" and then those in "src".
5784 * f, c1, c2 and g are temporary objects that have been initialized
5787 * Let src represent the expression
5789 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5791 * and let ma represent the expressions
5793 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5795 * We start out with the following expression for dst:
5797 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5799 * with the multiplication factor f initially equal to 1
5800 * and f \sum_i b_i v_i kept separately.
5801 * For each x_i that we substitute, we multiply the numerator
5802 * (and denominator) of dst by c_1 = m_i and add the numerator
5803 * of the x_i expression multiplied by c_2 = f b_i,
5804 * after removing the common factors of c_1 and c_2.
5805 * The multiplication factor f also needs to be multiplied by c_1
5806 * for the next x_j, j > i.
5808 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5809 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5810 int n_div_ma
, int n_div_bmap
,
5811 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5814 isl_size n_param
, n_in
, n_out
;
5817 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5818 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5819 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5820 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5821 return isl_stat_error
;
5823 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5824 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5825 isl_seq_clr(dst
+ o_dst
, n_in
);
5828 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5831 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5833 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5835 isl_int_set_si(f
, 1);
5837 for (i
= 0; i
< n_out
; ++i
) {
5838 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5840 if (isl_int_is_zero(src
[offset
]))
5842 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5843 isl_int_mul(c2
, f
, src
[offset
]);
5844 isl_int_gcd(g
, c1
, c2
);
5845 isl_int_divexact(c1
, c1
, g
);
5846 isl_int_divexact(c2
, c2
, g
);
5848 isl_int_mul(f
, f
, c1
);
5851 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5852 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5853 o_dst
+= 1 + n_param
;
5854 o_src
+= 1 + n_param
;
5855 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5857 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5858 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5861 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5863 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5864 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5867 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5869 isl_int_mul(dst
[0], dst
[0], c1
);
5875 /* Compute the pullback of "aff" by the function represented by "ma".
5876 * In other words, plug in "ma" in "aff". The result is an affine expression
5877 * defined over the domain space of "ma".
5879 * If "aff" is represented by
5881 * (a(p) + b x + c(divs))/d
5883 * and ma is represented by
5885 * x = D(p) + F(y) + G(divs')
5887 * then the result is
5889 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5891 * The divs in the local space of the input are similarly adjusted
5892 * through a call to isl_local_space_preimage_multi_aff.
5894 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5895 __isl_take isl_multi_aff
*ma
)
5897 isl_aff
*res
= NULL
;
5898 isl_local_space
*ls
;
5899 isl_size n_div_aff
, n_div_ma
;
5900 isl_int f
, c1
, c2
, g
;
5902 ma
= isl_multi_aff_align_divs(ma
);
5906 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5907 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5908 if (n_div_aff
< 0 || n_div_ma
< 0)
5911 ls
= isl_aff_get_domain_local_space(aff
);
5912 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5913 res
= isl_aff_alloc(ls
);
5922 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5923 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5924 res
= isl_aff_free(res
);
5932 isl_multi_aff_free(ma
);
5933 res
= isl_aff_normalize(res
);
5937 isl_multi_aff_free(ma
);
5942 /* Compute the pullback of "aff1" by the function represented by "aff2".
5943 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5944 * defined over the domain space of "aff1".
5946 * The domain of "aff1" should match the range of "aff2", which means
5947 * that it should be single-dimensional.
5949 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5950 __isl_take isl_aff
*aff2
)
5954 ma
= isl_multi_aff_from_aff(aff2
);
5955 return isl_aff_pullback_multi_aff(aff1
, ma
);
5958 /* Compute the pullback of "ma1" by the function represented by "ma2".
5959 * In other words, plug in "ma2" in "ma1".
5961 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5962 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5965 isl_space
*space
= NULL
;
5967 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5968 ma2
= isl_multi_aff_align_divs(ma2
);
5969 ma1
= isl_multi_aff_cow(ma1
);
5973 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5974 isl_multi_aff_get_space(ma1
));
5976 for (i
= 0; i
< ma1
->n
; ++i
) {
5977 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
5978 isl_multi_aff_copy(ma2
));
5983 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5984 isl_multi_aff_free(ma2
);
5987 isl_space_free(space
);
5988 isl_multi_aff_free(ma2
);
5989 isl_multi_aff_free(ma1
);
5993 /* Extend the local space of "dst" to include the divs
5994 * in the local space of "src".
5996 * If "src" does not have any divs or if the local spaces of "dst" and
5997 * "src" are the same, then no extension is required.
5999 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6000 __isl_keep isl_aff
*src
)
6003 isl_size src_n_div
, dst_n_div
;
6010 return isl_aff_free(dst
);
6012 ctx
= isl_aff_get_ctx(src
);
6013 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6015 return isl_aff_free(dst
);
6017 isl_die(ctx
, isl_error_invalid
,
6018 "spaces don't match", goto error
);
6020 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6021 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6024 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6025 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6026 return isl_aff_free(dst
);
6030 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6031 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6032 if (!exp1
|| (dst_n_div
&& !exp2
))
6035 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6036 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6044 return isl_aff_free(dst
);
6047 /* Adjust the local spaces of the affine expressions in "maff"
6048 * such that they all have the save divs.
6050 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6051 __isl_take isl_multi_aff
*maff
)
6059 maff
= isl_multi_aff_cow(maff
);
6063 for (i
= 1; i
< maff
->n
; ++i
)
6064 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
6065 for (i
= 1; i
< maff
->n
; ++i
) {
6066 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
6068 return isl_multi_aff_free(maff
);
6074 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6076 aff
= isl_aff_cow(aff
);
6080 aff
->ls
= isl_local_space_lift(aff
->ls
);
6082 return isl_aff_free(aff
);
6087 /* Lift "maff" to a space with extra dimensions such that the result
6088 * has no more existentially quantified variables.
6089 * If "ls" is not NULL, then *ls is assigned the local space that lies
6090 * at the basis of the lifting applied to "maff".
6092 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6093 __isl_give isl_local_space
**ls
)
6107 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6108 *ls
= isl_local_space_from_space(space
);
6110 return isl_multi_aff_free(maff
);
6115 maff
= isl_multi_aff_cow(maff
);
6116 maff
= isl_multi_aff_align_divs(maff
);
6120 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
6122 return isl_multi_aff_free(maff
);
6123 space
= isl_multi_aff_get_space(maff
);
6124 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6125 space
= isl_space_extend_domain_with_range(space
,
6126 isl_multi_aff_get_space(maff
));
6128 return isl_multi_aff_free(maff
);
6129 isl_space_free(maff
->space
);
6130 maff
->space
= space
;
6133 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
6135 return isl_multi_aff_free(maff
);
6138 for (i
= 0; i
< maff
->n
; ++i
) {
6139 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
6147 isl_local_space_free(*ls
);
6148 return isl_multi_aff_free(maff
);
6152 #define TYPE isl_pw_multi_aff
6154 #include "check_type_range_templ.c"
6156 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6158 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6159 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6166 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6169 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6173 space
= isl_pw_multi_aff_get_space(pma
);
6174 space
= isl_space_drop_dims(space
, isl_dim_out
,
6175 pos
+ 1, n_out
- pos
- 1);
6176 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6178 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6179 for (i
= 0; i
< pma
->n
; ++i
) {
6181 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6182 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6188 /* This is an alternative name for the function above.
6190 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6191 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6193 return isl_pw_multi_aff_get_at(pma
, pos
);
6196 /* Return an isl_pw_multi_aff with the given "set" as domain and
6197 * an unnamed zero-dimensional range.
6199 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6200 __isl_take isl_set
*set
)
6205 space
= isl_set_get_space(set
);
6206 space
= isl_space_from_domain(space
);
6207 ma
= isl_multi_aff_zero(space
);
6208 return isl_pw_multi_aff_alloc(set
, ma
);
6211 /* Add an isl_pw_multi_aff with the given "set" as domain and
6212 * an unnamed zero-dimensional range to *user.
6214 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6217 isl_union_pw_multi_aff
**upma
= user
;
6218 isl_pw_multi_aff
*pma
;
6220 pma
= isl_pw_multi_aff_from_domain(set
);
6221 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6226 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6227 * an unnamed zero-dimensional range.
6229 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6230 __isl_take isl_union_set
*uset
)
6233 isl_union_pw_multi_aff
*upma
;
6238 space
= isl_union_set_get_space(uset
);
6239 upma
= isl_union_pw_multi_aff_empty(space
);
6241 if (isl_union_set_foreach_set(uset
,
6242 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6245 isl_union_set_free(uset
);
6248 isl_union_set_free(uset
);
6249 isl_union_pw_multi_aff_free(upma
);
6253 /* Local data for bin_entry and the callback "fn".
6255 struct isl_union_pw_multi_aff_bin_data
{
6256 isl_union_pw_multi_aff
*upma2
;
6257 isl_union_pw_multi_aff
*res
;
6258 isl_pw_multi_aff
*pma
;
6259 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6262 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6263 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6265 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6267 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6271 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6273 isl_pw_multi_aff_free(pma
);
6278 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6279 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6280 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6281 * as *entry. The callback should adjust data->res if desired.
6283 static __isl_give isl_union_pw_multi_aff
*bin_op(
6284 __isl_take isl_union_pw_multi_aff
*upma1
,
6285 __isl_take isl_union_pw_multi_aff
*upma2
,
6286 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6289 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6291 space
= isl_union_pw_multi_aff_get_space(upma2
);
6292 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6293 space
= isl_union_pw_multi_aff_get_space(upma1
);
6294 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6296 if (!upma1
|| !upma2
)
6300 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6301 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6302 &bin_entry
, &data
) < 0)
6305 isl_union_pw_multi_aff_free(upma1
);
6306 isl_union_pw_multi_aff_free(upma2
);
6309 isl_union_pw_multi_aff_free(upma1
);
6310 isl_union_pw_multi_aff_free(upma2
);
6311 isl_union_pw_multi_aff_free(data
.res
);
6315 /* Given two isl_pw_multi_affs A -> B and C -> D,
6316 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6318 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6319 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6323 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6324 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6325 isl_pw_multi_aff_get_space(pma2
));
6326 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6327 &isl_multi_aff_range_product
);
6330 /* Given two isl_pw_multi_affs A -> B and C -> D,
6331 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6333 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6334 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6338 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6339 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6340 isl_pw_multi_aff_get_space(pma2
));
6341 space
= isl_space_flatten_range(space
);
6342 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6343 &isl_multi_aff_flat_range_product
);
6346 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6347 * to compute some form of range product and add the result to data->res.
6349 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6350 __isl_give isl_pw_multi_aff
*(*range_product
)(
6351 __isl_take isl_pw_multi_aff
*pma1
,
6352 __isl_take isl_pw_multi_aff
*pma2
),
6355 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6357 isl_space
*space1
, *space2
;
6359 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6360 space2
= isl_pw_multi_aff_peek_space(pma2
);
6361 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6362 space2
, isl_dim_in
);
6363 if (match
< 0 || !match
) {
6364 isl_pw_multi_aff_free(pma2
);
6365 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6368 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6370 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6375 /* If data->pma and "pma2" have the same domain space, then compute
6376 * their flat range product and add the result to data->res.
6378 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6381 return gen_range_product_entry(pma2
,
6382 &isl_pw_multi_aff_flat_range_product
, user
);
6385 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6386 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6388 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6389 __isl_take isl_union_pw_multi_aff
*upma1
,
6390 __isl_take isl_union_pw_multi_aff
*upma2
)
6392 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6395 /* If data->pma and "pma2" have the same domain space, then compute
6396 * their range product and add the result to data->res.
6398 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6401 return gen_range_product_entry(pma2
,
6402 &isl_pw_multi_aff_range_product
, user
);
6405 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6406 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6408 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6409 __isl_take isl_union_pw_multi_aff
*upma1
,
6410 __isl_take isl_union_pw_multi_aff
*upma2
)
6412 return bin_op(upma1
, upma2
, &range_product_entry
);
6415 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6416 * The parameters are assumed to have been aligned.
6418 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6419 * except that it works on two different isl_pw_* types.
6421 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6422 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6423 __isl_take isl_pw_aff
*pa
)
6426 isl_pw_multi_aff
*res
= NULL
;
6431 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6432 pa
->dim
, isl_dim_in
))
6433 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6434 "domains don't match", goto error
);
6435 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6439 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6441 for (i
= 0; i
< pma
->n
; ++i
) {
6442 for (j
= 0; j
< pa
->n
; ++j
) {
6444 isl_multi_aff
*res_ij
;
6447 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6448 isl_set_copy(pa
->p
[j
].set
));
6449 empty
= isl_set_plain_is_empty(common
);
6450 if (empty
< 0 || empty
) {
6451 isl_set_free(common
);
6457 res_ij
= isl_multi_aff_set_aff(
6458 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6459 isl_aff_copy(pa
->p
[j
].aff
));
6460 res_ij
= isl_multi_aff_gist(res_ij
,
6461 isl_set_copy(common
));
6463 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6467 isl_pw_multi_aff_free(pma
);
6468 isl_pw_aff_free(pa
);
6471 isl_pw_multi_aff_free(pma
);
6472 isl_pw_aff_free(pa
);
6473 return isl_pw_multi_aff_free(res
);
6476 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6478 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6479 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6480 __isl_take isl_pw_aff
*pa
)
6482 isl_bool equal_params
;
6486 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6487 if (equal_params
< 0)
6490 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6491 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6492 isl_pw_aff_check_named_params(pa
) < 0)
6494 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6495 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6496 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6498 isl_pw_multi_aff_free(pma
);
6499 isl_pw_aff_free(pa
);
6503 /* Do the parameters of "pa" match those of "space"?
6505 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6506 __isl_keep isl_space
*space
)
6508 isl_space
*pa_space
;
6512 return isl_bool_error
;
6514 pa_space
= isl_pw_aff_get_space(pa
);
6516 match
= isl_space_has_equal_params(space
, pa_space
);
6518 isl_space_free(pa_space
);
6522 /* Check that the domain space of "pa" matches "space".
6524 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6525 __isl_keep isl_space
*space
)
6527 isl_space
*pa_space
;
6531 return isl_stat_error
;
6533 pa_space
= isl_pw_aff_get_space(pa
);
6535 match
= isl_space_has_equal_params(space
, pa_space
);
6539 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6540 "parameters don't match", goto error
);
6541 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6542 pa_space
, isl_dim_in
);
6546 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6547 "domains don't match", goto error
);
6548 isl_space_free(pa_space
);
6551 isl_space_free(pa_space
);
6552 return isl_stat_error
;
6560 #include <isl_multi_explicit_domain.c>
6561 #include <isl_multi_pw_aff_explicit_domain.c>
6562 #include <isl_multi_templ.c>
6563 #include <isl_multi_add_constant_templ.c>
6564 #include <isl_multi_apply_set.c>
6565 #include <isl_multi_arith_templ.c>
6566 #include <isl_multi_bind_templ.c>
6567 #include <isl_multi_bind_domain_templ.c>
6568 #include <isl_multi_coalesce.c>
6569 #include <isl_multi_domain_templ.c>
6570 #include <isl_multi_dim_id_templ.c>
6571 #include <isl_multi_dims.c>
6572 #include <isl_multi_from_base_templ.c>
6573 #include <isl_multi_gist.c>
6574 #include <isl_multi_hash.c>
6575 #include <isl_multi_identity_templ.c>
6576 #include <isl_multi_align_set.c>
6577 #include <isl_multi_insert_domain_templ.c>
6578 #include <isl_multi_intersect.c>
6579 #include <isl_multi_min_max_templ.c>
6580 #include <isl_multi_move_dims_templ.c>
6581 #include <isl_multi_nan_templ.c>
6582 #include <isl_multi_param_templ.c>
6583 #include <isl_multi_product_templ.c>
6584 #include <isl_multi_splice_templ.c>
6585 #include <isl_multi_tuple_id_templ.c>
6586 #include <isl_multi_union_add_templ.c>
6587 #include <isl_multi_zero_templ.c>
6588 #include <isl_multi_unbind_params_templ.c>
6590 /* Is every element of "mpa" defined over a single universe domain?
6592 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6594 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6597 /* Given that every element of "mpa" is defined over a single universe domain,
6598 * return the corresponding base expressions.
6600 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6601 __isl_take isl_multi_pw_aff
*mpa
)
6607 n
= isl_multi_pw_aff_size(mpa
);
6609 mpa
= isl_multi_pw_aff_free(mpa
);
6610 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6611 for (i
= 0; i
< n
; ++i
) {
6614 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6615 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6617 isl_multi_pw_aff_free(mpa
);
6621 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6622 * with this explicit domain.
6624 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6625 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6629 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6632 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6633 map
= isl_map_intersect_domain(map
, dom
);
6638 /* Are all elements of "mpa" piecewise constants?
6640 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6642 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6645 /* Does "mpa" have a non-trivial explicit domain?
6647 * The explicit domain, if present, is trivial if it represents
6648 * an (obviously) universe set.
6650 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6651 __isl_keep isl_multi_pw_aff
*mpa
)
6654 return isl_bool_error
;
6655 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6656 return isl_bool_false
;
6657 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6663 #include "isl_opt_mpa_templ.c"
6665 /* Compute the minima of the set dimensions as a function of the
6666 * parameters, but independently of the other set dimensions.
6668 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6670 return set_opt_mpa(set
, &isl_set_dim_min
);
6673 /* Compute the maxima of the set dimensions as a function of the
6674 * parameters, but independently of the other set dimensions.
6676 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6678 return set_opt_mpa(set
, &isl_set_dim_max
);
6684 #include "isl_opt_mpa_templ.c"
6686 /* Compute the minima of the output dimensions as a function of the
6687 * parameters and input dimensions, but independently of
6688 * the other output dimensions.
6690 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6692 return map_opt_mpa(map
, &isl_map_dim_min
);
6695 /* Compute the maxima of the output dimensions as a function of the
6696 * parameters and input dimensions, but independently of
6697 * the other output dimensions.
6699 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6701 return map_opt_mpa(map
, &isl_map_dim_max
);
6705 #define TYPE isl_pw_multi_aff
6706 #include "isl_type_check_match_range_multi_val.c"
6708 /* Apply "fn" to the base expressions of "pma" and "mv".
6710 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6711 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6712 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6713 __isl_take isl_multi_val
*mv
))
6718 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6721 n
= isl_pw_multi_aff_n_piece(pma
);
6725 for (i
= 0; i
< n
; ++i
) {
6728 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6729 ma
= fn(ma
, isl_multi_val_copy(mv
));
6730 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6733 isl_multi_val_free(mv
);
6736 isl_multi_val_free(mv
);
6737 isl_pw_multi_aff_free(pma
);
6741 /* Scale the elements of "pma" by the corresponding elements of "mv".
6743 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6744 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6746 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6747 &isl_multi_aff_scale_multi_val
);
6750 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6752 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
6753 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6755 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6756 &isl_multi_aff_scale_down_multi_val
);
6759 /* This function is called for each entry of an isl_union_pw_multi_aff.
6760 * If the space of the entry matches that of data->mv,
6761 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6762 * Otherwise, return an empty isl_pw_multi_aff.
6764 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6765 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6768 isl_multi_val
*mv
= user
;
6770 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
6772 return isl_pw_multi_aff_free(pma
);
6774 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6775 isl_pw_multi_aff_free(pma
);
6776 return isl_pw_multi_aff_empty(space
);
6779 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6782 /* Scale the elements of "upma" by the corresponding elements of "mv",
6783 * for those entries that match the space of "mv".
6785 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6786 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6788 struct isl_union_pw_multi_aff_transform_control control
= {
6789 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6793 upma
= isl_union_pw_multi_aff_align_params(upma
,
6794 isl_multi_val_get_space(mv
));
6795 mv
= isl_multi_val_align_params(mv
,
6796 isl_union_pw_multi_aff_get_space(upma
));
6800 return isl_union_pw_multi_aff_transform(upma
, &control
);
6802 isl_multi_val_free(mv
);
6805 isl_multi_val_free(mv
);
6806 isl_union_pw_multi_aff_free(upma
);
6810 /* Construct and return a piecewise multi affine expression
6811 * in the given space with value zero in each of the output dimensions and
6812 * a universe domain.
6814 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6816 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6819 /* Construct and return a piecewise multi affine expression
6820 * that is equal to the given piecewise affine expression.
6822 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6823 __isl_take isl_pw_aff
*pa
)
6827 isl_pw_multi_aff
*pma
;
6832 space
= isl_pw_aff_get_space(pa
);
6833 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6835 for (i
= 0; i
< pa
->n
; ++i
) {
6839 set
= isl_set_copy(pa
->p
[i
].set
);
6840 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6841 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6844 isl_pw_aff_free(pa
);
6848 /* Construct and return a piecewise multi affine expression
6849 * that is equal to the given multi piecewise affine expression
6850 * on the shared domain of the piecewise affine expressions,
6851 * in the special case of a 0D multi piecewise affine expression.
6853 * Create a piecewise multi affine expression with the explicit domain of
6854 * the 0D multi piecewise affine expression as domain.
6856 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6857 __isl_take isl_multi_pw_aff
*mpa
)
6863 space
= isl_multi_pw_aff_get_space(mpa
);
6864 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6865 isl_multi_pw_aff_free(mpa
);
6867 ma
= isl_multi_aff_zero(space
);
6868 return isl_pw_multi_aff_alloc(dom
, ma
);
6871 /* Construct and return a piecewise multi affine expression
6872 * that is equal to the given multi piecewise affine expression
6873 * on the shared domain of the piecewise affine expressions.
6875 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6876 __isl_take isl_multi_pw_aff
*mpa
)
6881 isl_pw_multi_aff
*pma
;
6887 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6889 space
= isl_multi_pw_aff_get_space(mpa
);
6890 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6891 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6893 for (i
= 1; i
< mpa
->n
; ++i
) {
6894 isl_pw_multi_aff
*pma_i
;
6896 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6897 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6898 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6901 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6903 isl_multi_pw_aff_free(mpa
);
6907 /* Convenience function that constructs an isl_multi_pw_aff
6908 * directly from an isl_aff.
6910 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
6912 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
6915 /* Construct and return a multi piecewise affine expression
6916 * that is equal to the given multi affine expression.
6918 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6919 __isl_take isl_multi_aff
*ma
)
6923 isl_multi_pw_aff
*mpa
;
6925 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6927 ma
= isl_multi_aff_free(ma
);
6931 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6933 for (i
= 0; i
< n
; ++i
) {
6936 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6937 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6940 isl_multi_aff_free(ma
);
6944 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6945 * but is considered as a function on an isl_multi_aff when exported.
6947 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
6948 __isl_take isl_multi_aff
*ma
)
6950 return isl_multi_pw_aff_from_multi_aff(ma
);
6953 /* Construct and return a multi piecewise affine expression
6954 * that is equal to the given piecewise multi affine expression.
6956 * If the resulting multi piecewise affine expression has
6957 * an explicit domain, then assign it the domain of the input.
6958 * In other cases, the domain is stored in the individual elements.
6960 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6961 __isl_take isl_pw_multi_aff
*pma
)
6966 isl_multi_pw_aff
*mpa
;
6968 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6970 pma
= isl_pw_multi_aff_free(pma
);
6971 space
= isl_pw_multi_aff_get_space(pma
);
6972 mpa
= isl_multi_pw_aff_alloc(space
);
6974 for (i
= 0; i
< n
; ++i
) {
6977 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6978 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6980 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6983 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6984 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6987 isl_pw_multi_aff_free(pma
);
6991 /* This function performs the same operation as
6992 * isl_multi_pw_aff_from_pw_multi_aff,
6993 * but is considered as a function on an isl_pw_multi_aff when exported.
6995 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
6996 __isl_take isl_pw_multi_aff
*pma
)
6998 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7001 /* Do "pa1" and "pa2" represent the same function?
7003 * We first check if they are obviously equal.
7004 * If not, we convert them to maps and check if those are equal.
7006 * If "pa1" or "pa2" contain any NaNs, then they are considered
7007 * not to be the same. A NaN is not equal to anything, not even
7010 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7011 __isl_keep isl_pw_aff
*pa2
)
7015 isl_map
*map1
, *map2
;
7018 return isl_bool_error
;
7020 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7021 if (equal
< 0 || equal
)
7023 has_nan
= either_involves_nan(pa1
, pa2
);
7025 return isl_bool_error
;
7027 return isl_bool_false
;
7029 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7030 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7031 equal
= isl_map_is_equal(map1
, map2
);
7038 /* Do "mpa1" and "mpa2" represent the same function?
7040 * Note that we cannot convert the entire isl_multi_pw_aff
7041 * to a map because the domains of the piecewise affine expressions
7042 * may not be the same.
7044 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7045 __isl_keep isl_multi_pw_aff
*mpa2
)
7048 isl_bool equal
, equal_params
;
7051 return isl_bool_error
;
7053 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7054 if (equal_params
< 0)
7055 return isl_bool_error
;
7056 if (!equal_params
) {
7057 if (!isl_space_has_named_params(mpa1
->space
))
7058 return isl_bool_false
;
7059 if (!isl_space_has_named_params(mpa2
->space
))
7060 return isl_bool_false
;
7061 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7062 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7063 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7064 isl_multi_pw_aff_get_space(mpa2
));
7065 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7066 isl_multi_pw_aff_get_space(mpa1
));
7067 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7068 isl_multi_pw_aff_free(mpa1
);
7069 isl_multi_pw_aff_free(mpa2
);
7073 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7074 if (equal
< 0 || !equal
)
7077 for (i
= 0; i
< mpa1
->n
; ++i
) {
7078 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7079 if (equal
< 0 || !equal
)
7083 return isl_bool_true
;
7086 /* Do "pma1" and "pma2" represent the same function?
7088 * First check if they are obviously equal.
7089 * If not, then convert them to maps and check if those are equal.
7091 * If "pa1" or "pa2" contain any NaNs, then they are considered
7092 * not to be the same. A NaN is not equal to anything, not even
7095 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7096 __isl_keep isl_pw_multi_aff
*pma2
)
7100 isl_map
*map1
, *map2
;
7103 return isl_bool_error
;
7105 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7106 if (equal
< 0 || equal
)
7108 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7109 if (has_nan
>= 0 && !has_nan
)
7110 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7111 if (has_nan
< 0 || has_nan
)
7112 return isl_bool_not(has_nan
);
7114 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7115 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7116 equal
= isl_map_is_equal(map1
, map2
);
7123 /* Compute the pullback of "mpa" by the function represented by "ma".
7124 * In other words, plug in "ma" in "mpa".
7126 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7128 * If "mpa" has an explicit domain, then it is this domain
7129 * that needs to undergo a pullback, i.e., a preimage.
7131 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
7132 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7135 isl_space
*space
= NULL
;
7137 mpa
= isl_multi_pw_aff_cow(mpa
);
7141 space
= isl_space_join(isl_multi_aff_get_space(ma
),
7142 isl_multi_pw_aff_get_space(mpa
));
7146 for (i
= 0; i
< mpa
->n
; ++i
) {
7147 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
7148 isl_multi_aff_copy(ma
));
7152 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7153 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
7154 isl_multi_aff_copy(ma
));
7159 isl_multi_aff_free(ma
);
7160 isl_space_free(mpa
->space
);
7164 isl_space_free(space
);
7165 isl_multi_pw_aff_free(mpa
);
7166 isl_multi_aff_free(ma
);
7170 /* Compute the pullback of "mpa" by the function represented by "ma".
7171 * In other words, plug in "ma" in "mpa".
7173 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
7174 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7176 isl_bool equal_params
;
7180 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
7181 if (equal_params
< 0)
7184 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7185 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
7186 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
7187 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7189 isl_multi_pw_aff_free(mpa
);
7190 isl_multi_aff_free(ma
);
7194 /* Compute the pullback of "mpa" by the function represented by "pma".
7195 * In other words, plug in "pma" in "mpa".
7197 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7199 * If "mpa" has an explicit domain, then it is this domain
7200 * that needs to undergo a pullback, i.e., a preimage.
7202 static __isl_give isl_multi_pw_aff
*
7203 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7204 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7207 isl_space
*space
= NULL
;
7209 mpa
= isl_multi_pw_aff_cow(mpa
);
7213 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
7214 isl_multi_pw_aff_get_space(mpa
));
7216 for (i
= 0; i
< mpa
->n
; ++i
) {
7217 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7218 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
7222 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7223 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
7224 isl_pw_multi_aff_copy(pma
));
7229 isl_pw_multi_aff_free(pma
);
7230 isl_space_free(mpa
->space
);
7234 isl_space_free(space
);
7235 isl_multi_pw_aff_free(mpa
);
7236 isl_pw_multi_aff_free(pma
);
7240 /* Compute the pullback of "mpa" by the function represented by "pma".
7241 * In other words, plug in "pma" in "mpa".
7243 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
7244 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7246 isl_bool equal_params
;
7250 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
7251 if (equal_params
< 0)
7254 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7255 mpa
= isl_multi_pw_aff_align_params(mpa
,
7256 isl_pw_multi_aff_get_space(pma
));
7257 pma
= isl_pw_multi_aff_align_params(pma
,
7258 isl_multi_pw_aff_get_space(mpa
));
7259 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7261 isl_multi_pw_aff_free(mpa
);
7262 isl_pw_multi_aff_free(pma
);
7266 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7267 * with the domain of "aff". The domain of the result is the same
7269 * "mpa" and "aff" are assumed to have been aligned.
7271 * We first extract the parametric constant from "aff", defined
7272 * over the correct domain.
7273 * Then we add the appropriate combinations of the members of "mpa".
7274 * Finally, we add the integer divisions through recursive calls.
7276 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7277 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7280 isl_size n_in
, n_div
, n_mpa_in
;
7286 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7287 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7288 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7289 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7292 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7293 tmp
= isl_aff_copy(aff
);
7294 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7295 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7296 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7297 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7298 pa
= isl_pw_aff_from_aff(tmp
);
7300 for (i
= 0; i
< n_in
; ++i
) {
7303 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7305 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7306 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7307 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7308 pa
= isl_pw_aff_add(pa
, pa_i
);
7311 for (i
= 0; i
< n_div
; ++i
) {
7315 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7317 div
= isl_aff_get_div(aff
, i
);
7318 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7319 isl_multi_pw_aff_copy(mpa
), div
);
7320 pa_i
= isl_pw_aff_floor(pa_i
);
7321 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7322 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7323 pa
= isl_pw_aff_add(pa
, pa_i
);
7326 isl_multi_pw_aff_free(mpa
);
7331 isl_multi_pw_aff_free(mpa
);
7336 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7337 * with the domain of "aff". The domain of the result is the same
7340 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7341 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7343 isl_bool equal_params
;
7347 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7348 if (equal_params
< 0)
7351 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7353 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7354 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7356 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7359 isl_multi_pw_aff_free(mpa
);
7363 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7364 * with the domain of "pa". The domain of the result is the same
7366 * "mpa" and "pa" are assumed to have been aligned.
7368 * We consider each piece in turn. Note that the domains of the
7369 * pieces are assumed to be disjoint and they remain disjoint
7370 * after taking the preimage (over the same function).
7372 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7373 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7382 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7383 isl_pw_aff_get_space(pa
));
7384 res
= isl_pw_aff_empty(space
);
7386 for (i
= 0; i
< pa
->n
; ++i
) {
7390 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7391 isl_multi_pw_aff_copy(mpa
),
7392 isl_aff_copy(pa
->p
[i
].aff
));
7393 domain
= isl_set_copy(pa
->p
[i
].set
);
7394 domain
= isl_set_preimage_multi_pw_aff(domain
,
7395 isl_multi_pw_aff_copy(mpa
));
7396 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7397 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7400 isl_pw_aff_free(pa
);
7401 isl_multi_pw_aff_free(mpa
);
7404 isl_pw_aff_free(pa
);
7405 isl_multi_pw_aff_free(mpa
);
7409 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7410 * with the domain of "pa". The domain of the result is the same
7413 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7414 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7416 isl_bool equal_params
;
7420 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7421 if (equal_params
< 0)
7424 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7426 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7427 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7429 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7431 isl_pw_aff_free(pa
);
7432 isl_multi_pw_aff_free(mpa
);
7436 /* Compute the pullback of "pa" by the function represented by "mpa".
7437 * In other words, plug in "mpa" in "pa".
7438 * "pa" and "mpa" are assumed to have been aligned.
7440 * The pullback is computed by applying "pa" to "mpa".
7442 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7443 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7445 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7448 /* Compute the pullback of "pa" by the function represented by "mpa".
7449 * In other words, plug in "mpa" in "pa".
7451 * The pullback is computed by applying "pa" to "mpa".
7453 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7454 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7456 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7459 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7460 * In other words, plug in "mpa2" in "mpa1".
7462 * We pullback each member of "mpa1" in turn.
7464 * If "mpa1" has an explicit domain, then it is this domain
7465 * that needs to undergo a pullback instead, i.e., a preimage.
7467 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7468 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7471 isl_space
*space
= NULL
;
7473 isl_multi_pw_aff_align_params_bin(&mpa1
, &mpa2
);
7474 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7478 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7479 isl_multi_pw_aff_get_space(mpa1
));
7481 for (i
= 0; i
< mpa1
->n
; ++i
) {
7482 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7483 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7488 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7489 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7490 isl_multi_pw_aff_copy(mpa2
));
7494 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7496 isl_multi_pw_aff_free(mpa2
);
7499 isl_space_free(space
);
7500 isl_multi_pw_aff_free(mpa1
);
7501 isl_multi_pw_aff_free(mpa2
);
7505 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7506 * of "mpa1" and "mpa2" live in the same space, construct map space
7507 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7508 * with this map space as extract argument.
7510 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7511 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7512 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7513 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7516 isl_space
*space1
, *space2
;
7519 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7520 isl_multi_pw_aff_get_space(mpa2
));
7521 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7522 isl_multi_pw_aff_get_space(mpa1
));
7525 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7526 mpa2
->space
, isl_dim_out
);
7530 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7531 "range spaces don't match", goto error
);
7532 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7533 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7534 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7536 res
= order(mpa1
, mpa2
, space1
);
7537 isl_multi_pw_aff_free(mpa1
);
7538 isl_multi_pw_aff_free(mpa2
);
7541 isl_multi_pw_aff_free(mpa1
);
7542 isl_multi_pw_aff_free(mpa2
);
7546 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7547 * where the function values are equal. "space" is the space of the result.
7548 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7550 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7551 * in the sequences are equal.
7553 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7554 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7555 __isl_take isl_space
*space
)
7561 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7563 space
= isl_space_free(space
);
7564 res
= isl_map_universe(space
);
7566 for (i
= 0; i
< n
; ++i
) {
7567 isl_pw_aff
*pa1
, *pa2
;
7570 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7571 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7572 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7573 res
= isl_map_intersect(res
, map
);
7579 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7580 * where the function values are equal.
7582 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7583 __isl_take isl_multi_pw_aff
*mpa2
)
7585 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7586 &isl_multi_pw_aff_eq_map_on_space
);
7589 /* Intersect "map" with the result of applying "order"
7590 * on two copies of "mpa".
7592 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7593 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7594 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7595 __isl_take isl_multi_pw_aff
*mpa2
))
7597 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7600 /* Return the subset of "map" where the domain and the range
7601 * have equal "mpa" values.
7603 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7604 __isl_take isl_multi_pw_aff
*mpa
)
7606 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7607 &isl_multi_pw_aff_eq_map
);
7610 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7611 * where the function values of "mpa1" lexicographically satisfies
7612 * "strict_base"/"base" compared to that of "mpa2".
7613 * "space" is the space of the result.
7614 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7616 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7617 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7618 * when compared to the i-th element of "mpa2" while all previous elements are
7620 * In particular, if i corresponds to the final elements
7621 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7622 * for other values of i.
7623 * If "base" is a strict order, then "base" and "strict_base" are the same.
7625 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7626 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7627 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7628 __isl_take isl_pw_aff
*pa2
),
7629 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7630 __isl_take isl_pw_aff
*pa2
),
7631 __isl_take isl_space
*space
)
7635 isl_map
*res
, *rest
;
7637 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7639 space
= isl_space_free(space
);
7640 res
= isl_map_empty(isl_space_copy(space
));
7641 rest
= isl_map_universe(space
);
7643 for (i
= 0; i
< n
; ++i
) {
7645 isl_pw_aff
*pa1
, *pa2
;
7650 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7651 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7652 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7653 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7654 res
= isl_map_union(res
, map
);
7659 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7660 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7661 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7662 rest
= isl_map_intersect(rest
, map
);
7672 #define STRICT_ORDER lt
7673 #include "isl_aff_lex_templ.c"
7678 #define STRICT_ORDER lt
7679 #include "isl_aff_lex_templ.c"
7684 #define STRICT_ORDER gt
7685 #include "isl_aff_lex_templ.c"
7690 #define STRICT_ORDER gt
7691 #include "isl_aff_lex_templ.c"
7693 /* Compare two isl_affs.
7695 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7696 * than "aff2" and 0 if they are equal.
7698 * The order is fairly arbitrary. We do consider expressions that only involve
7699 * earlier dimensions as "smaller".
7701 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7714 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7718 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7719 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7721 return last1
- last2
;
7723 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7726 /* Compare two isl_pw_affs.
7728 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7729 * than "pa2" and 0 if they are equal.
7731 * The order is fairly arbitrary. We do consider expressions that only involve
7732 * earlier dimensions as "smaller".
7734 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7735 __isl_keep isl_pw_aff
*pa2
)
7748 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7752 if (pa1
->n
!= pa2
->n
)
7753 return pa1
->n
- pa2
->n
;
7755 for (i
= 0; i
< pa1
->n
; ++i
) {
7756 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7759 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7767 /* Return a piecewise affine expression that is equal to "v" on "domain".
7769 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7770 __isl_take isl_val
*v
)
7773 isl_local_space
*ls
;
7776 space
= isl_set_get_space(domain
);
7777 ls
= isl_local_space_from_space(space
);
7778 aff
= isl_aff_val_on_domain(ls
, v
);
7780 return isl_pw_aff_alloc(domain
, aff
);
7783 /* Return a piecewise affine expression that is equal to the parameter
7784 * with identifier "id" on "domain".
7786 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7787 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7792 space
= isl_set_get_space(domain
);
7793 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7794 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7795 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7797 return isl_pw_aff_alloc(domain
, aff
);
7800 /* Return a multi affine expression that is equal to "mv" on domain
7803 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7804 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7809 isl_local_space
*ls
;
7812 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7813 if (!space
|| n
< 0)
7816 space2
= isl_multi_val_get_space(mv
);
7817 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7818 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7819 space
= isl_space_map_from_domain_and_range(space
, space2
);
7820 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7821 ls
= isl_local_space_from_space(isl_space_domain(space
));
7822 for (i
= 0; i
< n
; ++i
) {
7826 v
= isl_multi_val_get_val(mv
, i
);
7827 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7828 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7830 isl_local_space_free(ls
);
7832 isl_multi_val_free(mv
);
7835 isl_space_free(space
);
7836 isl_multi_val_free(mv
);
7840 /* This is an alternative name for the function above.
7842 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7843 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7845 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7848 /* This function performs the same operation as
7849 * isl_multi_aff_multi_val_on_domain_space,
7850 * but is considered as a function on an isl_space when exported.
7852 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7853 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7855 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7858 /* Return a piecewise multi-affine expression
7859 * that is equal to "mv" on "domain".
7861 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7862 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7867 space
= isl_set_get_space(domain
);
7868 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7870 return isl_pw_multi_aff_alloc(domain
, ma
);
7873 /* This function performs the same operation as
7874 * isl_pw_multi_aff_multi_val_on_domain,
7875 * but is considered as a function on an isl_set when exported.
7877 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7878 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7880 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7883 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7884 * mv is the value that should be attained on each domain set
7885 * res collects the results
7887 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7889 isl_union_pw_multi_aff
*res
;
7892 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7893 * and add it to data->res.
7895 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7898 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7899 isl_pw_multi_aff
*pma
;
7902 mv
= isl_multi_val_copy(data
->mv
);
7903 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7904 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7906 return data
->res
? isl_stat_ok
: isl_stat_error
;
7909 /* Return a union piecewise multi-affine expression
7910 * that is equal to "mv" on "domain".
7912 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7913 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7915 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7918 space
= isl_union_set_get_space(domain
);
7919 data
.res
= isl_union_pw_multi_aff_empty(space
);
7921 if (isl_union_set_foreach_set(domain
,
7922 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7923 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7924 isl_union_set_free(domain
);
7925 isl_multi_val_free(mv
);
7929 /* Compute the pullback of data->pma by the function represented by "pma2",
7930 * provided the spaces match, and add the results to data->res.
7932 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7934 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7936 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7937 pma2
->dim
, isl_dim_out
)) {
7938 isl_pw_multi_aff_free(pma2
);
7942 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7943 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7945 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7947 return isl_stat_error
;
7952 /* Compute the pullback of "upma1" by the function represented by "upma2".
7954 __isl_give isl_union_pw_multi_aff
*
7955 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7956 __isl_take isl_union_pw_multi_aff
*upma1
,
7957 __isl_take isl_union_pw_multi_aff
*upma2
)
7959 return bin_op(upma1
, upma2
, &pullback_entry
);
7962 /* Apply "upma2" to "upma1".
7964 * That is, compute the pullback of "upma2" by "upma1".
7966 __isl_give isl_union_pw_multi_aff
*
7967 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7968 __isl_take isl_union_pw_multi_aff
*upma1
,
7969 __isl_take isl_union_pw_multi_aff
*upma2
)
7971 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7975 #define TYPE isl_pw_multi_aff
7977 #include "isl_copy_tuple_id_templ.c"
7979 /* Given a function "pma1" of the form A[B -> C] -> D and
7980 * a function "pma2" of the form E -> B,
7981 * replace the domain of the wrapped relation inside the domain of "pma1"
7982 * by the preimage with respect to "pma2".
7983 * In other words, plug in "pma2" in this nested domain.
7984 * The result is of the form A[E -> C] -> D.
7986 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7987 * plug that into "pma1".
7989 __isl_give isl_pw_multi_aff
*
7990 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7991 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
7993 isl_space
*pma1_space
, *pma2_space
;
7995 isl_pw_multi_aff
*id
;
7997 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
7998 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8000 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
8002 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
8003 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
8006 space
= isl_space_domain(isl_space_copy(pma1_space
));
8007 space
= isl_space_range(isl_space_unwrap(space
));
8008 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
8009 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8011 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8012 pma1_space
, isl_dim_in
);
8013 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8014 pma1_space
, isl_dim_in
);
8016 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8018 isl_pw_multi_aff_free(pma1
);
8019 isl_pw_multi_aff_free(pma2
);
8023 /* If data->pma and "pma2" are such that
8024 * data->pma is of the form A[B -> C] -> D and
8025 * "pma2" is of the form E -> B,
8026 * then replace the domain of the wrapped relation
8027 * inside the domain of data->pma by the preimage with respect to "pma2" and
8028 * add the result to data->res.
8030 static isl_stat
preimage_domain_wrapped_domain_entry(
8031 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8033 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8034 isl_space
*pma1_space
, *pma2_space
;
8037 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8038 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8040 match
= isl_space_domain_is_wrapping(pma1_space
);
8041 if (match
>= 0 && match
)
8042 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8043 isl_dim_in
, pma2_space
, isl_dim_out
);
8044 if (match
< 0 || !match
) {
8045 isl_pw_multi_aff_free(pma2
);
8046 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8049 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8050 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8052 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8054 return isl_stat_non_null(data
->res
);
8057 /* For each pair of functions A[B -> C] -> D in "upma1" and
8058 * E -> B in "upma2",
8059 * replace the domain of the wrapped relation inside the domain of the first
8060 * by the preimage with respect to the second and collect the results.
8061 * In other words, plug in the second function in this nested domain.
8062 * The results are of the form A[E -> C] -> D.
8064 __isl_give isl_union_pw_multi_aff
*
8065 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8066 __isl_take isl_union_pw_multi_aff
*upma1
,
8067 __isl_take isl_union_pw_multi_aff
*upma2
)
8069 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8072 /* Check that the domain space of "upa" matches "space".
8074 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8075 * can in principle never fail since the space "space" is that
8076 * of the isl_multi_union_pw_aff and is a set space such that
8077 * there is no domain space to match.
8079 * We check the parameters and double-check that "space" is
8080 * indeed that of a set.
8082 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8083 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8085 isl_space
*upa_space
;
8089 return isl_stat_error
;
8091 match
= isl_space_is_set(space
);
8093 return isl_stat_error
;
8095 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8096 "expecting set space", return isl_stat_error
);
8098 upa_space
= isl_union_pw_aff_get_space(upa
);
8099 match
= isl_space_has_equal_params(space
, upa_space
);
8103 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8104 "parameters don't match", goto error
);
8106 isl_space_free(upa_space
);
8109 isl_space_free(upa_space
);
8110 return isl_stat_error
;
8113 /* Do the parameters of "upa" match those of "space"?
8115 static isl_bool
isl_union_pw_aff_matching_params(
8116 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8118 isl_space
*upa_space
;
8122 return isl_bool_error
;
8124 upa_space
= isl_union_pw_aff_get_space(upa
);
8126 match
= isl_space_has_equal_params(space
, upa_space
);
8128 isl_space_free(upa_space
);
8132 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8133 * space represents the new parameters.
8134 * res collects the results.
8136 struct isl_union_pw_aff_reset_params_data
{
8138 isl_union_pw_aff
*res
;
8141 /* Replace the parameters of "pa" by data->space and
8142 * add the result to data->res.
8144 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8146 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8149 space
= isl_pw_aff_get_space(pa
);
8150 space
= isl_space_replace_params(space
, data
->space
);
8151 pa
= isl_pw_aff_reset_space(pa
, space
);
8152 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8154 return data
->res
? isl_stat_ok
: isl_stat_error
;
8157 /* Replace the domain space of "upa" by "space".
8158 * Since a union expression does not have a (single) domain space,
8159 * "space" is necessarily a parameter space.
8161 * Since the order and the names of the parameters determine
8162 * the hash value, we need to create a new hash table.
8164 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8165 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8167 struct isl_union_pw_aff_reset_params_data data
= { space
};
8170 match
= isl_union_pw_aff_matching_params(upa
, space
);
8172 upa
= isl_union_pw_aff_free(upa
);
8174 isl_space_free(space
);
8178 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8179 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8180 data
.res
= isl_union_pw_aff_free(data
.res
);
8182 isl_union_pw_aff_free(upa
);
8183 isl_space_free(space
);
8187 /* Return the floor of "pa".
8189 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8191 return isl_pw_aff_floor(pa
);
8194 /* Given f, return floor(f).
8196 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8197 __isl_take isl_union_pw_aff
*upa
)
8199 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8204 * upa mod m = upa - m * floor(upa/m)
8206 * with m an integer value.
8208 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8209 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8211 isl_union_pw_aff
*res
;
8216 if (!isl_val_is_int(m
))
8217 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8218 "expecting integer modulo", goto error
);
8219 if (!isl_val_is_pos(m
))
8220 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8221 "expecting positive modulo", goto error
);
8223 res
= isl_union_pw_aff_copy(upa
);
8224 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8225 upa
= isl_union_pw_aff_floor(upa
);
8226 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8227 res
= isl_union_pw_aff_sub(res
, upa
);
8232 isl_union_pw_aff_free(upa
);
8236 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8237 * pos is the output position that needs to be extracted.
8238 * res collects the results.
8240 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8242 isl_union_pw_aff
*res
;
8245 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8246 * (assuming it has such a dimension) and add it to data->res.
8248 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8250 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8254 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8256 return isl_stat_error
;
8257 if (data
->pos
>= n_out
) {
8258 isl_pw_multi_aff_free(pma
);
8262 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8263 isl_pw_multi_aff_free(pma
);
8265 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8267 return data
->res
? isl_stat_ok
: isl_stat_error
;
8270 /* Extract an isl_union_pw_aff corresponding to
8271 * output dimension "pos" of "upma".
8273 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8274 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8276 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8283 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8284 "cannot extract at negative position", return NULL
);
8286 space
= isl_union_pw_multi_aff_get_space(upma
);
8287 data
.res
= isl_union_pw_aff_empty(space
);
8289 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8290 &get_union_pw_aff
, &data
) < 0)
8291 data
.res
= isl_union_pw_aff_free(data
.res
);
8296 /* Return a union piecewise affine expression
8297 * that is equal to "aff" on "domain".
8299 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8300 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8304 pa
= isl_pw_aff_from_aff(aff
);
8305 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8308 /* Return a union piecewise affine expression
8309 * that is equal to the parameter identified by "id" on "domain".
8311 * Make sure the parameter appears in the space passed to
8312 * isl_aff_param_on_domain_space_id.
8314 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8315 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8320 space
= isl_union_set_get_space(domain
);
8321 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8322 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8323 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8326 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8327 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8329 * "res" collects the results.
8331 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8333 isl_union_pw_aff
*res
;
8336 /* Construct a piecewise affine expression that is equal to data->pa
8337 * on "domain" and add the result to data->res.
8339 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8341 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8345 pa
= isl_pw_aff_copy(data
->pa
);
8346 dim
= isl_set_dim(domain
, isl_dim_set
);
8348 pa
= isl_pw_aff_free(pa
);
8349 pa
= isl_pw_aff_from_range(pa
);
8350 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8351 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8352 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8353 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8355 return data
->res
? isl_stat_ok
: isl_stat_error
;
8358 /* Return a union piecewise affine expression
8359 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8360 * have been aligned.
8362 * Construct an isl_pw_aff on each of the sets in "domain" and
8363 * collect the results.
8365 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8366 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8368 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8371 space
= isl_union_set_get_space(domain
);
8372 data
.res
= isl_union_pw_aff_empty(space
);
8374 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8375 data
.res
= isl_union_pw_aff_free(data
.res
);
8376 isl_union_set_free(domain
);
8377 isl_pw_aff_free(pa
);
8381 /* Return a union piecewise affine expression
8382 * that is equal to "pa" on "domain".
8384 * Check that "pa" is a parametric expression,
8385 * align the parameters if needed and call
8386 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8388 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8389 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8392 isl_bool equal_params
;
8393 isl_space
*domain_space
, *pa_space
;
8395 pa_space
= isl_pw_aff_peek_space(pa
);
8396 is_set
= isl_space_is_set(pa_space
);
8400 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8401 "expecting parametric expression", goto error
);
8403 domain_space
= isl_union_set_get_space(domain
);
8404 pa_space
= isl_pw_aff_get_space(pa
);
8405 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8406 if (equal_params
>= 0 && !equal_params
) {
8409 space
= isl_space_align_params(domain_space
, pa_space
);
8410 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8411 domain
= isl_union_set_align_params(domain
, space
);
8413 isl_space_free(domain_space
);
8414 isl_space_free(pa_space
);
8417 if (equal_params
< 0)
8419 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8421 isl_union_set_free(domain
);
8422 isl_pw_aff_free(pa
);
8426 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8427 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8428 * "res" collects the results.
8430 struct isl_union_pw_aff_val_on_domain_data
{
8432 isl_union_pw_aff
*res
;
8435 /* Construct a piecewise affine expression that is equal to data->v
8436 * on "domain" and add the result to data->res.
8438 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8440 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8444 v
= isl_val_copy(data
->v
);
8445 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8446 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8448 return data
->res
? isl_stat_ok
: isl_stat_error
;
8451 /* Return a union piecewise affine expression
8452 * that is equal to "v" on "domain".
8454 * Construct an isl_pw_aff on each of the sets in "domain" and
8455 * collect the results.
8457 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8458 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8460 struct isl_union_pw_aff_val_on_domain_data data
;
8463 space
= isl_union_set_get_space(domain
);
8464 data
.res
= isl_union_pw_aff_empty(space
);
8466 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8467 data
.res
= isl_union_pw_aff_free(data
.res
);
8468 isl_union_set_free(domain
);
8473 /* Construct a piecewise multi affine expression
8474 * that is equal to "pa" and add it to upma.
8476 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8479 isl_union_pw_multi_aff
**upma
= user
;
8480 isl_pw_multi_aff
*pma
;
8482 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8483 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8485 return *upma
? isl_stat_ok
: isl_stat_error
;
8488 /* Construct and return a union piecewise multi affine expression
8489 * that is equal to the given union piecewise affine expression.
8491 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8492 __isl_take isl_union_pw_aff
*upa
)
8495 isl_union_pw_multi_aff
*upma
;
8500 space
= isl_union_pw_aff_get_space(upa
);
8501 upma
= isl_union_pw_multi_aff_empty(space
);
8503 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8504 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8505 upma
= isl_union_pw_multi_aff_free(upma
);
8507 isl_union_pw_aff_free(upa
);
8511 /* Compute the set of elements in the domain of "pa" where it is zero and
8512 * add this set to "uset".
8514 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8516 isl_union_set
**uset
= (isl_union_set
**)user
;
8518 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8520 return *uset
? isl_stat_ok
: isl_stat_error
;
8523 /* Return a union set containing those elements in the domain
8524 * of "upa" where it is zero.
8526 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8527 __isl_take isl_union_pw_aff
*upa
)
8529 isl_union_set
*zero
;
8531 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8532 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8533 zero
= isl_union_set_free(zero
);
8535 isl_union_pw_aff_free(upa
);
8539 /* Internal data structure for isl_union_pw_aff_bind_id,
8540 * storing the parameter that needs to be bound and
8541 * the accumulated results.
8543 struct isl_bind_id_data
{
8545 isl_union_set
*bound
;
8548 /* Bind the piecewise affine function "pa" to the parameter data->id,
8549 * adding the resulting elements in the domain where the expression
8550 * is equal to the parameter to data->bound.
8552 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8554 struct isl_bind_id_data
*data
= user
;
8557 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8558 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8560 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8563 /* Bind the union piecewise affine function "upa" to the parameter "id",
8564 * returning the elements in the domain where the expression
8565 * is equal to the parameter.
8567 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8568 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8570 struct isl_bind_id_data data
= { id
};
8572 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8573 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8574 data
.bound
= isl_union_set_free(data
.bound
);
8576 isl_union_pw_aff_free(upa
);
8581 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8582 * upma is the function that is plugged in.
8583 * pa is the current part of the function in which upma is plugged in.
8584 * res collects the results.
8586 struct isl_union_pw_aff_pullback_upma_data
{
8587 isl_union_pw_multi_aff
*upma
;
8589 isl_union_pw_aff
*res
;
8592 /* Check if "pma" can be plugged into data->pa.
8593 * If so, perform the pullback and add the result to data->res.
8595 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8597 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8600 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8601 pma
->dim
, isl_dim_out
)) {
8602 isl_pw_multi_aff_free(pma
);
8606 pa
= isl_pw_aff_copy(data
->pa
);
8607 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8609 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8611 return data
->res
? isl_stat_ok
: isl_stat_error
;
8614 /* Check if any of the elements of data->upma can be plugged into pa,
8615 * add if so add the result to data->res.
8617 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8619 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8623 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8625 isl_pw_aff_free(pa
);
8630 /* Compute the pullback of "upa" by the function represented by "upma".
8631 * In other words, plug in "upma" in "upa". The result contains
8632 * expressions defined over the domain space of "upma".
8634 * Run over all pairs of elements in "upa" and "upma", perform
8635 * the pullback when appropriate and collect the results.
8636 * If the hash value were based on the domain space rather than
8637 * the function space, then we could run through all elements
8638 * of "upma" and directly pick out the corresponding element of "upa".
8640 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8641 __isl_take isl_union_pw_aff
*upa
,
8642 __isl_take isl_union_pw_multi_aff
*upma
)
8644 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8647 space
= isl_union_pw_multi_aff_get_space(upma
);
8648 upa
= isl_union_pw_aff_align_params(upa
, space
);
8649 space
= isl_union_pw_aff_get_space(upa
);
8650 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8656 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8657 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8658 data
.res
= isl_union_pw_aff_free(data
.res
);
8660 isl_union_pw_aff_free(upa
);
8661 isl_union_pw_multi_aff_free(upma
);
8664 isl_union_pw_aff_free(upa
);
8665 isl_union_pw_multi_aff_free(upma
);
8670 #define BASE union_pw_aff
8672 #define DOMBASE union_set
8674 #include <isl_multi_explicit_domain.c>
8675 #include <isl_multi_union_pw_aff_explicit_domain.c>
8676 #include <isl_multi_templ.c>
8677 #include <isl_multi_apply_set.c>
8678 #include <isl_multi_apply_union_set.c>
8679 #include <isl_multi_arith_templ.c>
8680 #include <isl_multi_bind_templ.c>
8681 #include <isl_multi_coalesce.c>
8682 #include <isl_multi_dim_id_templ.c>
8683 #include <isl_multi_floor.c>
8684 #include <isl_multi_from_base_templ.c>
8685 #include <isl_multi_gist.c>
8686 #include <isl_multi_align_set.c>
8687 #include <isl_multi_align_union_set.c>
8688 #include <isl_multi_intersect.c>
8689 #include <isl_multi_nan_templ.c>
8690 #include <isl_multi_tuple_id_templ.c>
8691 #include <isl_multi_union_add_templ.c>
8692 #include <isl_multi_zero_space_templ.c>
8694 /* Does "mupa" have a non-trivial explicit domain?
8696 * The explicit domain, if present, is trivial if it represents
8697 * an (obviously) universe parameter set.
8699 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8700 __isl_keep isl_multi_union_pw_aff
*mupa
)
8702 isl_bool is_params
, trivial
;
8706 return isl_bool_error
;
8707 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8708 return isl_bool_false
;
8709 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8710 if (is_params
< 0 || !is_params
)
8711 return isl_bool_not(is_params
);
8712 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8713 trivial
= isl_set_plain_is_universe(set
);
8715 return isl_bool_not(trivial
);
8718 /* Construct a multiple union piecewise affine expression
8719 * in the given space with value zero in each of the output dimensions.
8721 * Since there is no canonical zero value for
8722 * a union piecewise affine expression, we can only construct
8723 * a zero-dimensional "zero" value.
8725 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8726 __isl_take isl_space
*space
)
8734 params
= isl_space_is_params(space
);
8738 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8739 "expecting proper set space", goto error
);
8740 if (!isl_space_is_set(space
))
8741 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8742 "expecting set space", goto error
);
8743 dim
= isl_space_dim(space
, isl_dim_out
);
8747 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8748 "expecting 0D space", goto error
);
8750 return isl_multi_union_pw_aff_alloc(space
);
8752 isl_space_free(space
);
8756 /* Construct and return a multi union piecewise affine expression
8757 * that is equal to the given multi affine expression.
8759 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8760 __isl_take isl_multi_aff
*ma
)
8762 isl_multi_pw_aff
*mpa
;
8764 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8765 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8768 /* This function performs the same operation as
8769 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8770 * isl_multi_aff when exported.
8772 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8773 __isl_take isl_multi_aff
*ma
)
8775 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8778 /* Construct and return a multi union piecewise affine expression
8779 * that is equal to the given multi piecewise affine expression.
8781 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8782 __isl_take isl_multi_pw_aff
*mpa
)
8787 isl_multi_union_pw_aff
*mupa
;
8789 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8791 mpa
= isl_multi_pw_aff_free(mpa
);
8795 space
= isl_multi_pw_aff_get_space(mpa
);
8796 space
= isl_space_range(space
);
8797 mupa
= isl_multi_union_pw_aff_alloc(space
);
8799 for (i
= 0; i
< n
; ++i
) {
8801 isl_union_pw_aff
*upa
;
8803 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8804 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8805 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8808 isl_multi_pw_aff_free(mpa
);
8813 /* Extract the range space of "pma" and assign it to *space.
8814 * If *space has already been set (through a previous call to this function),
8815 * then check that the range space is the same.
8817 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8819 isl_space
**space
= user
;
8820 isl_space
*pma_space
;
8823 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8824 isl_pw_multi_aff_free(pma
);
8827 return isl_stat_error
;
8833 equal
= isl_space_is_equal(pma_space
, *space
);
8834 isl_space_free(pma_space
);
8837 return isl_stat_error
;
8839 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8840 "range spaces not the same", return isl_stat_error
);
8844 /* Construct and return a multi union piecewise affine expression
8845 * that is equal to the given union piecewise multi affine expression.
8847 * In order to be able to perform the conversion, the input
8848 * needs to be non-empty and may only involve a single range space.
8850 * If the resulting multi union piecewise affine expression has
8851 * an explicit domain, then assign it the domain of the input.
8852 * In other cases, the domain is stored in the individual elements.
8854 __isl_give isl_multi_union_pw_aff
*
8855 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8856 __isl_take isl_union_pw_multi_aff
*upma
)
8858 isl_space
*space
= NULL
;
8859 isl_multi_union_pw_aff
*mupa
;
8863 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8867 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8868 "cannot extract range space from empty input",
8870 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8877 n
= isl_space_dim(space
, isl_dim_set
);
8879 space
= isl_space_free(space
);
8880 mupa
= isl_multi_union_pw_aff_alloc(space
);
8882 for (i
= 0; i
< n
; ++i
) {
8883 isl_union_pw_aff
*upa
;
8885 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8886 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8888 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8890 isl_union_pw_multi_aff
*copy
;
8892 copy
= isl_union_pw_multi_aff_copy(upma
);
8893 dom
= isl_union_pw_multi_aff_domain(copy
);
8894 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8897 isl_union_pw_multi_aff_free(upma
);
8900 isl_space_free(space
);
8901 isl_union_pw_multi_aff_free(upma
);
8905 /* This function performs the same operation as
8906 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8907 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8909 __isl_give isl_multi_union_pw_aff
*
8910 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8911 __isl_take isl_union_pw_multi_aff
*upma
)
8913 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8916 /* Try and create an isl_multi_union_pw_aff that is equivalent
8917 * to the given isl_union_map.
8918 * The isl_union_map is required to be single-valued in each space.
8919 * Moreover, it cannot be empty and all range spaces need to be the same.
8920 * Otherwise, an error is produced.
8922 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8923 __isl_take isl_union_map
*umap
)
8925 isl_union_pw_multi_aff
*upma
;
8927 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8928 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8931 /* This function performs the same operation as
8932 * isl_multi_union_pw_aff_from_union_map,
8933 * but is considered as a function on an isl_union_map when exported.
8935 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8936 __isl_take isl_union_map
*umap
)
8938 return isl_multi_union_pw_aff_from_union_map(umap
);
8941 /* Return a multiple union piecewise affine expression
8942 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8943 * have been aligned.
8945 * If the resulting multi union piecewise affine expression has
8946 * an explicit domain, then assign it the input domain.
8947 * In other cases, the domain is stored in the individual elements.
8949 static __isl_give isl_multi_union_pw_aff
*
8950 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8951 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8956 isl_multi_union_pw_aff
*mupa
;
8958 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8959 if (!domain
|| n
< 0)
8962 space
= isl_multi_val_get_space(mv
);
8963 mupa
= isl_multi_union_pw_aff_alloc(space
);
8964 for (i
= 0; i
< n
; ++i
) {
8966 isl_union_pw_aff
*upa
;
8968 v
= isl_multi_val_get_val(mv
, i
);
8969 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8971 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8973 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8974 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8975 isl_union_set_copy(domain
));
8977 isl_union_set_free(domain
);
8978 isl_multi_val_free(mv
);
8981 isl_union_set_free(domain
);
8982 isl_multi_val_free(mv
);
8986 /* Return a multiple union piecewise affine expression
8987 * that is equal to "mv" on "domain".
8989 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8990 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8992 isl_bool equal_params
;
8996 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8997 if (equal_params
< 0)
9000 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9002 domain
= isl_union_set_align_params(domain
,
9003 isl_multi_val_get_space(mv
));
9004 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9005 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9007 isl_union_set_free(domain
);
9008 isl_multi_val_free(mv
);
9012 /* Return a multiple union piecewise affine expression
9013 * that is equal to "ma" on "domain".
9015 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9016 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9018 isl_pw_multi_aff
*pma
;
9020 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9021 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9024 /* Return a multiple union piecewise affine expression
9025 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9026 * have been aligned.
9028 * If the resulting multi union piecewise affine expression has
9029 * an explicit domain, then assign it the input domain.
9030 * In other cases, the domain is stored in the individual elements.
9032 static __isl_give isl_multi_union_pw_aff
*
9033 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9034 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9039 isl_multi_union_pw_aff
*mupa
;
9041 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9042 if (!domain
|| n
< 0)
9044 space
= isl_pw_multi_aff_get_space(pma
);
9045 mupa
= isl_multi_union_pw_aff_alloc(space
);
9046 for (i
= 0; i
< n
; ++i
) {
9048 isl_union_pw_aff
*upa
;
9050 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9051 upa
= isl_union_pw_aff_pw_aff_on_domain(
9052 isl_union_set_copy(domain
), pa
);
9053 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9055 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9056 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9057 isl_union_set_copy(domain
));
9059 isl_union_set_free(domain
);
9060 isl_pw_multi_aff_free(pma
);
9063 isl_union_set_free(domain
);
9064 isl_pw_multi_aff_free(pma
);
9068 /* Return a multiple union piecewise affine expression
9069 * that is equal to "pma" on "domain".
9071 __isl_give isl_multi_union_pw_aff
*
9072 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9073 __isl_take isl_pw_multi_aff
*pma
)
9075 isl_bool equal_params
;
9078 space
= isl_pw_multi_aff_peek_space(pma
);
9079 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9080 if (equal_params
< 0)
9083 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9085 domain
= isl_union_set_align_params(domain
,
9086 isl_pw_multi_aff_get_space(pma
));
9087 pma
= isl_pw_multi_aff_align_params(pma
,
9088 isl_union_set_get_space(domain
));
9089 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9092 isl_union_set_free(domain
);
9093 isl_pw_multi_aff_free(pma
);
9097 /* Return a union set containing those elements in the domains
9098 * of the elements of "mupa" where they are all zero.
9100 * If there are no elements, then simply return the entire domain.
9102 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9103 __isl_take isl_multi_union_pw_aff
*mupa
)
9107 isl_union_pw_aff
*upa
;
9108 isl_union_set
*zero
;
9110 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9112 mupa
= isl_multi_union_pw_aff_free(mupa
);
9117 return isl_multi_union_pw_aff_domain(mupa
);
9119 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9120 zero
= isl_union_pw_aff_zero_union_set(upa
);
9122 for (i
= 1; i
< n
; ++i
) {
9123 isl_union_set
*zero_i
;
9125 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9126 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9128 zero
= isl_union_set_intersect(zero
, zero_i
);
9131 isl_multi_union_pw_aff_free(mupa
);
9135 /* Construct a union map mapping the shared domain
9136 * of the union piecewise affine expressions to the range of "mupa"
9137 * in the special case of a 0D multi union piecewise affine expression.
9139 * Construct a map between the explicit domain of "mupa" and
9141 * Note that this assumes that the domain consists of explicit elements.
9143 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9144 __isl_take isl_multi_union_pw_aff
*mupa
)
9148 isl_union_set
*dom
, *ran
;
9150 space
= isl_multi_union_pw_aff_get_space(mupa
);
9151 dom
= isl_multi_union_pw_aff_domain(mupa
);
9152 ran
= isl_union_set_from_set(isl_set_universe(space
));
9154 is_params
= isl_union_set_is_params(dom
);
9156 dom
= isl_union_set_free(dom
);
9158 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9159 "cannot create union map from expression without "
9160 "explicit domain elements",
9161 dom
= isl_union_set_free(dom
));
9163 return isl_union_map_from_domain_and_range(dom
, ran
);
9166 /* Construct a union map mapping the shared domain
9167 * of the union piecewise affine expressions to the range of "mupa"
9168 * with each dimension in the range equated to the
9169 * corresponding union piecewise affine expression.
9171 * If the input is zero-dimensional, then construct a mapping
9172 * from its explicit domain.
9174 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9175 __isl_take isl_multi_union_pw_aff
*mupa
)
9180 isl_union_map
*umap
;
9181 isl_union_pw_aff
*upa
;
9183 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9185 mupa
= isl_multi_union_pw_aff_free(mupa
);
9190 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9192 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9193 umap
= isl_union_map_from_union_pw_aff(upa
);
9195 for (i
= 1; i
< n
; ++i
) {
9196 isl_union_map
*umap_i
;
9198 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9199 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9200 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9203 space
= isl_multi_union_pw_aff_get_space(mupa
);
9204 umap
= isl_union_map_reset_range_space(umap
, space
);
9206 isl_multi_union_pw_aff_free(mupa
);
9210 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9211 * "range" is the space from which to set the range space.
9212 * "res" collects the results.
9214 struct isl_union_pw_multi_aff_reset_range_space_data
{
9216 isl_union_pw_multi_aff
*res
;
9219 /* Replace the range space of "pma" by the range space of data->range and
9220 * add the result to data->res.
9222 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9224 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9227 space
= isl_pw_multi_aff_get_space(pma
);
9228 space
= isl_space_domain(space
);
9229 space
= isl_space_extend_domain_with_range(space
,
9230 isl_space_copy(data
->range
));
9231 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9232 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9234 return data
->res
? isl_stat_ok
: isl_stat_error
;
9237 /* Replace the range space of all the piecewise affine expressions in "upma" by
9238 * the range space of "space".
9240 * This assumes that all these expressions have the same output dimension.
9242 * Since the spaces of the expressions change, so do their hash values.
9243 * We therefore need to create a new isl_union_pw_multi_aff.
9244 * Note that the hash value is currently computed based on the entire
9245 * space even though there can only be a single expression with a given
9248 static __isl_give isl_union_pw_multi_aff
*
9249 isl_union_pw_multi_aff_reset_range_space(
9250 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9252 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9253 isl_space
*space_upma
;
9255 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9256 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9257 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9258 &reset_range_space
, &data
) < 0)
9259 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9261 isl_space_free(space
);
9262 isl_union_pw_multi_aff_free(upma
);
9266 /* Construct and return a union piecewise multi affine expression
9267 * that is equal to the given multi union piecewise affine expression,
9268 * in the special case of a 0D multi union piecewise affine expression.
9270 * Construct a union piecewise multi affine expression
9271 * on top of the explicit domain of the input.
9273 __isl_give isl_union_pw_multi_aff
*
9274 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9275 __isl_take isl_multi_union_pw_aff
*mupa
)
9279 isl_union_set
*domain
;
9281 space
= isl_multi_union_pw_aff_get_space(mupa
);
9282 mv
= isl_multi_val_zero(space
);
9283 domain
= isl_multi_union_pw_aff_domain(mupa
);
9284 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9287 /* Construct and return a union piecewise multi affine expression
9288 * that is equal to the given multi union piecewise affine expression.
9290 * If the input is zero-dimensional, then
9291 * construct a union piecewise multi affine expression
9292 * on top of the explicit domain of the input.
9294 __isl_give isl_union_pw_multi_aff
*
9295 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9296 __isl_take isl_multi_union_pw_aff
*mupa
)
9301 isl_union_pw_multi_aff
*upma
;
9302 isl_union_pw_aff
*upa
;
9304 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9306 mupa
= isl_multi_union_pw_aff_free(mupa
);
9311 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9313 space
= isl_multi_union_pw_aff_get_space(mupa
);
9314 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9315 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9317 for (i
= 1; i
< n
; ++i
) {
9318 isl_union_pw_multi_aff
*upma_i
;
9320 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9321 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9322 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9325 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9327 isl_multi_union_pw_aff_free(mupa
);
9331 /* Intersect the range of "mupa" with "range",
9332 * in the special case where "mupa" is 0D.
9334 * Intersect the domain of "mupa" with the constraints on the parameters
9337 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9338 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9340 range
= isl_set_params(range
);
9341 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9345 /* Intersect the range of "mupa" with "range".
9346 * That is, keep only those domain elements that have a function value
9349 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9350 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9352 isl_union_pw_multi_aff
*upma
;
9353 isl_union_set
*domain
;
9358 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9359 if (n
< 0 || !range
)
9362 space
= isl_set_get_space(range
);
9363 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9364 space
, isl_dim_set
);
9365 isl_space_free(space
);
9369 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9370 "space don't match", goto error
);
9372 return mupa_intersect_range_0D(mupa
, range
);
9374 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9375 isl_multi_union_pw_aff_copy(mupa
));
9376 domain
= isl_union_set_from_set(range
);
9377 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9378 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9382 isl_multi_union_pw_aff_free(mupa
);
9383 isl_set_free(range
);
9387 /* Return the shared domain of the elements of "mupa",
9388 * in the special case where "mupa" is zero-dimensional.
9390 * Return the explicit domain of "mupa".
9391 * Note that this domain may be a parameter set, either
9392 * because "mupa" is meant to live in a set space or
9393 * because no explicit domain has been set.
9395 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9396 __isl_take isl_multi_union_pw_aff
*mupa
)
9400 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9401 isl_multi_union_pw_aff_free(mupa
);
9406 /* Return the shared domain of the elements of "mupa".
9408 * If "mupa" is zero-dimensional, then return its explicit domain.
9410 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9411 __isl_take isl_multi_union_pw_aff
*mupa
)
9415 isl_union_pw_aff
*upa
;
9418 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9420 mupa
= isl_multi_union_pw_aff_free(mupa
);
9425 return isl_multi_union_pw_aff_domain_0D(mupa
);
9427 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9428 dom
= isl_union_pw_aff_domain(upa
);
9429 for (i
= 1; i
< n
; ++i
) {
9430 isl_union_set
*dom_i
;
9432 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9433 dom_i
= isl_union_pw_aff_domain(upa
);
9434 dom
= isl_union_set_intersect(dom
, dom_i
);
9437 isl_multi_union_pw_aff_free(mupa
);
9441 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9442 * In particular, the spaces have been aligned.
9443 * The result is defined over the shared domain of the elements of "mupa"
9445 * We first extract the parametric constant part of "aff" and
9446 * define that over the shared domain.
9447 * Then we iterate over all input dimensions of "aff" and add the corresponding
9448 * multiples of the elements of "mupa".
9449 * Finally, we consider the integer divisions, calling the function
9450 * recursively to obtain an isl_union_pw_aff corresponding to the
9451 * integer division argument.
9453 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9454 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9457 isl_size n_in
, n_div
;
9458 isl_union_pw_aff
*upa
;
9459 isl_union_set
*uset
;
9463 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9464 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9465 if (n_in
< 0 || n_div
< 0)
9468 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9469 cst
= isl_aff_copy(aff
);
9470 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9471 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9472 cst
= isl_aff_project_domain_on_params(cst
);
9473 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9475 for (i
= 0; i
< n_in
; ++i
) {
9476 isl_union_pw_aff
*upa_i
;
9478 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9480 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9481 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9482 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9483 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9486 for (i
= 0; i
< n_div
; ++i
) {
9488 isl_union_pw_aff
*upa_i
;
9490 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9492 div
= isl_aff_get_div(aff
, i
);
9493 upa_i
= multi_union_pw_aff_apply_aff(
9494 isl_multi_union_pw_aff_copy(mupa
), div
);
9495 upa_i
= isl_union_pw_aff_floor(upa_i
);
9496 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9497 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9498 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9501 isl_multi_union_pw_aff_free(mupa
);
9506 isl_multi_union_pw_aff_free(mupa
);
9511 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9512 * with the domain of "aff".
9513 * Furthermore, the dimension of this space needs to be greater than zero.
9514 * The result is defined over the shared domain of the elements of "mupa"
9516 * We perform these checks and then hand over control to
9517 * multi_union_pw_aff_apply_aff.
9519 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9520 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9523 isl_space
*space1
, *space2
;
9526 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9527 isl_aff_get_space(aff
));
9528 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9532 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9533 space2
= isl_aff_get_domain_space(aff
);
9534 equal
= isl_space_is_equal(space1
, space2
);
9535 isl_space_free(space1
);
9536 isl_space_free(space2
);
9540 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9541 "spaces don't match", goto error
);
9542 dim
= isl_aff_dim(aff
, isl_dim_in
);
9546 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9547 "cannot determine domains", goto error
);
9549 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9551 isl_multi_union_pw_aff_free(mupa
);
9556 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9557 * The space of "mupa" is known to be compatible with the domain of "ma".
9559 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9560 * on the domain of "mupa".
9562 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9563 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9567 dom
= isl_multi_union_pw_aff_domain(mupa
);
9568 ma
= isl_multi_aff_project_domain_on_params(ma
);
9570 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9573 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9574 * with the domain of "ma".
9575 * The result is defined over the shared domain of the elements of "mupa"
9577 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9578 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9580 isl_space
*space1
, *space2
;
9581 isl_multi_union_pw_aff
*res
;
9584 isl_size n_in
, n_out
;
9586 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9587 isl_multi_aff_get_space(ma
));
9588 ma
= isl_multi_aff_align_params(ma
,
9589 isl_multi_union_pw_aff_get_space(mupa
));
9590 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9591 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9592 if (!mupa
|| n_in
< 0 || n_out
< 0)
9595 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9596 space2
= isl_multi_aff_get_domain_space(ma
);
9597 equal
= isl_space_is_equal(space1
, space2
);
9598 isl_space_free(space1
);
9599 isl_space_free(space2
);
9603 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9604 "spaces don't match", goto error
);
9606 return mupa_apply_multi_aff_0D(mupa
, ma
);
9608 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9609 res
= isl_multi_union_pw_aff_alloc(space1
);
9611 for (i
= 0; i
< n_out
; ++i
) {
9613 isl_union_pw_aff
*upa
;
9615 aff
= isl_multi_aff_get_aff(ma
, i
);
9616 upa
= multi_union_pw_aff_apply_aff(
9617 isl_multi_union_pw_aff_copy(mupa
), aff
);
9618 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9621 isl_multi_aff_free(ma
);
9622 isl_multi_union_pw_aff_free(mupa
);
9625 isl_multi_union_pw_aff_free(mupa
);
9626 isl_multi_aff_free(ma
);
9630 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9631 * The space of "mupa" is known to be compatible with the domain of "pa".
9633 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9634 * on the domain of "mupa".
9636 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9637 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9641 dom
= isl_multi_union_pw_aff_domain(mupa
);
9642 pa
= isl_pw_aff_project_domain_on_params(pa
);
9644 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9647 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9648 * with the domain of "pa".
9649 * Furthermore, the dimension of this space needs to be greater than zero.
9650 * The result is defined over the shared domain of the elements of "mupa"
9652 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9653 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9658 isl_space
*space
, *space2
;
9659 isl_union_pw_aff
*upa
;
9661 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9662 isl_pw_aff_get_space(pa
));
9663 pa
= isl_pw_aff_align_params(pa
,
9664 isl_multi_union_pw_aff_get_space(mupa
));
9668 space
= isl_multi_union_pw_aff_get_space(mupa
);
9669 space2
= isl_pw_aff_get_domain_space(pa
);
9670 equal
= isl_space_is_equal(space
, space2
);
9671 isl_space_free(space
);
9672 isl_space_free(space2
);
9676 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9677 "spaces don't match", goto error
);
9678 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9682 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9684 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9685 upa
= isl_union_pw_aff_empty(space
);
9687 for (i
= 0; i
< pa
->n
; ++i
) {
9690 isl_multi_union_pw_aff
*mupa_i
;
9691 isl_union_pw_aff
*upa_i
;
9693 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9694 domain
= isl_set_copy(pa
->p
[i
].set
);
9695 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9696 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9697 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9698 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9701 isl_multi_union_pw_aff_free(mupa
);
9702 isl_pw_aff_free(pa
);
9705 isl_multi_union_pw_aff_free(mupa
);
9706 isl_pw_aff_free(pa
);
9710 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9711 * The space of "mupa" is known to be compatible with the domain of "pma".
9713 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9714 * on the domain of "mupa".
9716 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9717 __isl_take isl_multi_union_pw_aff
*mupa
,
9718 __isl_take isl_pw_multi_aff
*pma
)
9722 dom
= isl_multi_union_pw_aff_domain(mupa
);
9723 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9725 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9728 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9729 * with the domain of "pma".
9730 * The result is defined over the shared domain of the elements of "mupa"
9732 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9733 __isl_take isl_multi_union_pw_aff
*mupa
,
9734 __isl_take isl_pw_multi_aff
*pma
)
9736 isl_space
*space1
, *space2
;
9737 isl_multi_union_pw_aff
*res
;
9740 isl_size n_in
, n_out
;
9742 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9743 isl_pw_multi_aff_get_space(pma
));
9744 pma
= isl_pw_multi_aff_align_params(pma
,
9745 isl_multi_union_pw_aff_get_space(mupa
));
9749 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9750 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9751 equal
= isl_space_is_equal(space1
, space2
);
9752 isl_space_free(space1
);
9753 isl_space_free(space2
);
9757 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9758 "spaces don't match", goto error
);
9759 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9760 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9761 if (n_in
< 0 || n_out
< 0)
9764 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9766 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9767 res
= isl_multi_union_pw_aff_alloc(space1
);
9769 for (i
= 0; i
< n_out
; ++i
) {
9771 isl_union_pw_aff
*upa
;
9773 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9774 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9775 isl_multi_union_pw_aff_copy(mupa
), pa
);
9776 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9779 isl_pw_multi_aff_free(pma
);
9780 isl_multi_union_pw_aff_free(mupa
);
9783 isl_multi_union_pw_aff_free(mupa
);
9784 isl_pw_multi_aff_free(pma
);
9788 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9789 * If the explicit domain only keeps track of constraints on the parameters,
9790 * then only update those constraints.
9792 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9793 __isl_take isl_multi_union_pw_aff
*mupa
,
9794 __isl_keep isl_union_pw_multi_aff
*upma
)
9798 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9799 return isl_multi_union_pw_aff_free(mupa
);
9801 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9805 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9807 return isl_multi_union_pw_aff_free(mupa
);
9809 upma
= isl_union_pw_multi_aff_copy(upma
);
9811 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9812 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9814 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9817 return isl_multi_union_pw_aff_free(mupa
);
9821 /* Compute the pullback of "mupa" by the function represented by "upma".
9822 * In other words, plug in "upma" in "mupa". The result contains
9823 * expressions defined over the domain space of "upma".
9825 * Run over all elements of "mupa" and plug in "upma" in each of them.
9827 * If "mupa" has an explicit domain, then it is this domain
9828 * that needs to undergo a pullback instead, i.e., a preimage.
9830 __isl_give isl_multi_union_pw_aff
*
9831 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9832 __isl_take isl_multi_union_pw_aff
*mupa
,
9833 __isl_take isl_union_pw_multi_aff
*upma
)
9838 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9839 isl_union_pw_multi_aff_get_space(upma
));
9840 upma
= isl_union_pw_multi_aff_align_params(upma
,
9841 isl_multi_union_pw_aff_get_space(mupa
));
9842 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9843 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9847 for (i
= 0; i
< n
; ++i
) {
9848 isl_union_pw_aff
*upa
;
9850 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9851 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9852 isl_union_pw_multi_aff_copy(upma
));
9853 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9856 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9857 mupa
= preimage_explicit_domain(mupa
, upma
);
9859 isl_union_pw_multi_aff_free(upma
);
9862 isl_multi_union_pw_aff_free(mupa
);
9863 isl_union_pw_multi_aff_free(upma
);
9867 /* Extract the sequence of elements in "mupa" with domain space "space"
9868 * (ignoring parameters).
9870 * For the elements of "mupa" that are not defined on the specified space,
9871 * the corresponding element in the result is empty.
9873 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9874 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9878 isl_space
*space_mpa
;
9879 isl_multi_pw_aff
*mpa
;
9881 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9882 if (n
< 0 || !space
)
9885 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9886 space
= isl_space_replace_params(space
, space_mpa
);
9887 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9889 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9891 space
= isl_space_from_domain(space
);
9892 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9893 for (i
= 0; i
< n
; ++i
) {
9894 isl_union_pw_aff
*upa
;
9897 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9898 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9899 isl_space_copy(space
));
9900 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9901 isl_union_pw_aff_free(upa
);
9904 isl_space_free(space
);
9907 isl_space_free(space
);
9911 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9912 * should modify the base expressions in the input.
9914 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9915 * are taken into account.
9916 * "fn" is applied to each entry in the input.
9918 struct isl_union_pw_multi_aff_un_op_control
{
9919 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9920 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9923 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9924 * a second argument) for use as an isl_union_pw_multi_aff_transform
9925 * filter function (which does take a second argument).
9926 * Simply call control->filter without the second argument.
9928 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9929 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9931 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9933 return control
->filter(pma
);
9936 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9937 * a second argument) for use as an isl_union_pw_multi_aff_transform
9938 * base function (which does take a second argument).
9939 * Simply call control->fn without the second argument.
9941 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9942 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9944 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9946 return control
->fn(pma
);
9949 /* Construct an isl_union_pw_multi_aff that is obtained by
9950 * modifying "upma" according to "control".
9952 * isl_union_pw_multi_aff_transform performs essentially
9953 * the same operation, but takes a filter and a callback function
9954 * of a different form (with an extra argument).
9955 * Call isl_union_pw_multi_aff_transform with wrappers
9956 * that remove this extra argument.
9958 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9959 __isl_take isl_union_pw_multi_aff
*upma
,
9960 struct isl_union_pw_multi_aff_un_op_control
*control
)
9962 struct isl_union_pw_multi_aff_transform_control t_control
= {
9963 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9964 .filter_user
= control
,
9965 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9969 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9972 /* For each function in "upma" of the form A -> [B -> C],
9973 * extract the function A -> B and collect the results.
9975 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9976 __isl_take isl_union_pw_multi_aff
*upma
)
9978 struct isl_union_pw_multi_aff_un_op_control control
= {
9979 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9980 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9982 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9985 /* For each function in "upma" of the form A -> [B -> C],
9986 * extract the function A -> C and collect the results.
9988 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9989 __isl_take isl_union_pw_multi_aff
*upma
)
9991 struct isl_union_pw_multi_aff_un_op_control control
= {
9992 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9993 .fn
= &isl_pw_multi_aff_range_factor_range
,
9995 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9998 /* Evaluate the affine function "aff" in the void point "pnt".
9999 * In particular, return the value NaN.
10001 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10002 __isl_take isl_point
*pnt
)
10006 ctx
= isl_point_get_ctx(pnt
);
10008 isl_point_free(pnt
);
10009 return isl_val_nan(ctx
);
10012 /* Evaluate the affine expression "aff"
10013 * in the coordinates (with denominator) "pnt".
10015 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10016 __isl_keep isl_vec
*pnt
)
10025 ctx
= isl_vec_get_ctx(aff
);
10028 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10029 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10030 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10031 v
= isl_val_normalize(v
);
10038 /* Check that the domain space of "aff" is equal to "space".
10040 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10041 __isl_keep isl_space
*space
)
10045 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10047 return isl_stat_error
;
10049 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10050 "incompatible spaces", return isl_stat_error
);
10051 return isl_stat_ok
;
10054 /* Evaluate the affine function "aff" in "pnt".
10056 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10057 __isl_take isl_point
*pnt
)
10061 isl_local_space
*ls
;
10063 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10065 is_void
= isl_point_is_void(pnt
);
10069 return eval_void(aff
, pnt
);
10071 ls
= isl_aff_get_domain_local_space(aff
);
10072 pnt
= isl_local_space_lift_point(ls
, pnt
);
10074 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10077 isl_point_free(pnt
);
10082 isl_point_free(pnt
);