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
)
2595 if (type
== isl_dim_out
)
2596 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2597 "cannot drop output/set dimension",
2598 return isl_aff_free(aff
));
2599 if (type
== isl_dim_in
)
2601 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2604 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2605 return isl_aff_free(aff
);
2607 aff
= isl_aff_cow(aff
);
2611 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2613 return isl_aff_free(aff
);
2615 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2616 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2618 return isl_aff_free(aff
);
2623 /* Is the domain of "aff" a product?
2625 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2627 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2631 #define TYPE isl_aff
2632 #include <isl_domain_factor_templ.c>
2634 /* Project the domain of the affine expression onto its parameter space.
2635 * The affine expression may not involve any of the domain dimensions.
2637 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2642 n
= isl_aff_dim(aff
, isl_dim_in
);
2644 return isl_aff_free(aff
);
2645 aff
= isl_aff_drop_domain(aff
, 0, n
);
2646 space
= isl_aff_get_domain_space(aff
);
2647 space
= isl_space_params(space
);
2648 aff
= isl_aff_reset_domain_space(aff
, space
);
2652 /* Convert an affine expression defined over a parameter domain
2653 * into one that is defined over a zero-dimensional set.
2655 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2657 isl_local_space
*ls
;
2659 ls
= isl_aff_take_domain_local_space(aff
);
2660 ls
= isl_local_space_set_from_params(ls
);
2661 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2666 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2667 enum isl_dim_type type
, unsigned first
, unsigned n
)
2671 if (type
== isl_dim_out
)
2672 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2673 "cannot insert output/set dimensions",
2674 return isl_aff_free(aff
));
2675 if (type
== isl_dim_in
)
2677 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2680 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2681 return isl_aff_free(aff
);
2683 aff
= isl_aff_cow(aff
);
2687 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2689 return isl_aff_free(aff
);
2691 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2692 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2694 return isl_aff_free(aff
);
2699 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2700 enum isl_dim_type type
, unsigned n
)
2704 pos
= isl_aff_dim(aff
, type
);
2706 return isl_aff_free(aff
);
2708 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2711 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2712 * to dimensions of "dst_type" at "dst_pos".
2714 * We only support moving input dimensions to parameters and vice versa.
2716 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2717 enum isl_dim_type dst_type
, unsigned dst_pos
,
2718 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2722 isl_size src_off
, dst_off
;
2727 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2728 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2731 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2732 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2733 "cannot move output/set dimension",
2734 return isl_aff_free(aff
));
2735 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2736 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2737 "cannot move divs", return isl_aff_free(aff
));
2738 if (dst_type
== isl_dim_in
)
2739 dst_type
= isl_dim_set
;
2740 if (src_type
== isl_dim_in
)
2741 src_type
= isl_dim_set
;
2743 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2744 return isl_aff_free(aff
);
2745 if (dst_type
== src_type
)
2746 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2747 "moving dims within the same type not supported",
2748 return isl_aff_free(aff
));
2750 aff
= isl_aff_cow(aff
);
2751 src_off
= isl_aff_domain_offset(aff
, src_type
);
2752 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2753 if (src_off
< 0 || dst_off
< 0)
2754 return isl_aff_free(aff
);
2756 g_src_pos
= 1 + src_off
+ src_pos
;
2757 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2758 if (dst_type
> src_type
)
2761 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2762 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2763 src_type
, src_pos
, n
);
2764 if (!aff
->v
|| !aff
->ls
)
2765 return isl_aff_free(aff
);
2767 aff
= sort_divs(aff
);
2772 /* Return a zero isl_aff in the given space.
2774 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2775 * interface over all piecewise types.
2777 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2779 isl_local_space
*ls
;
2781 ls
= isl_local_space_from_space(isl_space_domain(space
));
2782 return isl_aff_zero_on_domain(ls
);
2785 #define isl_aff_involves_nan isl_aff_is_nan
2788 #define PW isl_pw_aff
2792 #define EL_IS_ZERO is_empty
2796 #define IS_ZERO is_empty
2799 #undef DEFAULT_IS_ZERO
2800 #define DEFAULT_IS_ZERO 0
2802 #include <isl_pw_templ.c>
2803 #include <isl_pw_un_op_templ.c>
2804 #include <isl_pw_add_constant_val_templ.c>
2805 #include <isl_pw_bind_domain_templ.c>
2806 #include <isl_pw_eval.c>
2807 #include <isl_pw_hash.c>
2808 #include <isl_pw_insert_dims_templ.c>
2809 #include <isl_pw_insert_domain_templ.c>
2810 #include <isl_pw_move_dims_templ.c>
2811 #include <isl_pw_neg_templ.c>
2812 #include <isl_pw_pullback_templ.c>
2813 #include <isl_pw_sub_templ.c>
2814 #include <isl_pw_union_opt.c>
2819 #include <isl_union_single.c>
2820 #include <isl_union_neg.c>
2825 #include <isl_union_pw_templ.c>
2827 /* Compute a piecewise quasi-affine expression with a domain that
2828 * is the union of those of pwaff1 and pwaff2 and such that on each
2829 * cell, the quasi-affine expression is the maximum of those of pwaff1
2830 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2831 * cell, then the associated expression is the defined one.
2833 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2834 __isl_take isl_pw_aff
*pwaff2
)
2836 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2837 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2840 /* Compute a piecewise quasi-affine expression with a domain that
2841 * is the union of those of pwaff1 and pwaff2 and such that on each
2842 * cell, the quasi-affine expression is the minimum of those of pwaff1
2843 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2844 * cell, then the associated expression is the defined one.
2846 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2847 __isl_take isl_pw_aff
*pwaff2
)
2849 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2850 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2853 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2854 __isl_take isl_pw_aff
*pwaff2
, int max
)
2857 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2859 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2862 /* Is the domain of "pa" a product?
2864 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2866 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2870 #define TYPE isl_pw_aff
2871 #include <isl_domain_factor_templ.c>
2873 /* Return a set containing those elements in the domain
2874 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2875 * does not satisfy "fn" (if complement is 1).
2877 * The pieces with a NaN never belong to the result since
2878 * NaN does not satisfy any property.
2880 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2881 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2883 int complement
, void *user
)
2891 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2893 for (i
= 0; i
< pwaff
->n
; ++i
) {
2894 isl_basic_set
*bset
;
2895 isl_set
*set_i
, *locus
;
2898 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2901 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2902 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2903 locus
= isl_set_from_basic_set(bset
);
2904 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2906 set_i
= isl_set_subtract(set_i
, locus
);
2908 set_i
= isl_set_intersect(set_i
, locus
);
2909 set
= isl_set_union_disjoint(set
, set_i
);
2912 isl_pw_aff_free(pwaff
);
2917 /* Return a set containing those elements in the domain
2918 * of "pa" where it is positive.
2920 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2922 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2925 /* Return a set containing those elements in the domain
2926 * of pwaff where it is non-negative.
2928 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2930 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2933 /* Return a set containing those elements in the domain
2934 * of pwaff where it is zero.
2936 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2938 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2941 /* Return a set containing those elements in the domain
2942 * of pwaff where it is not zero.
2944 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2946 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2949 /* Bind the affine function "aff" to the parameter "id",
2950 * returning the elements in the domain where the affine expression
2951 * is equal to the parameter.
2953 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2954 __isl_take isl_id
*id
)
2959 space
= isl_aff_get_domain_space(aff
);
2960 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2962 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2963 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2965 return isl_aff_eq_basic_set(aff
, aff_id
);
2968 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2969 * "rational" should not be set.
2971 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2972 int rational
, void *user
)
2979 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2980 "rational binding not supported", goto error
);
2981 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2987 /* Bind the piecewise affine function "pa" to the parameter "id",
2988 * returning the elements in the domain where the expression
2989 * is equal to the parameter.
2991 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2992 __isl_take isl_id
*id
)
2996 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3002 /* Return a set containing those elements in the shared domain
3003 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3005 * We compute the difference on the shared domain and then construct
3006 * the set of values where this difference is non-negative.
3007 * If strict is set, we first subtract 1 from the difference.
3008 * If equal is set, we only return the elements where pwaff1 and pwaff2
3011 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3012 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3014 isl_set
*set1
, *set2
;
3016 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3017 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3018 set1
= isl_set_intersect(set1
, set2
);
3019 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3020 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3021 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3024 isl_space
*space
= isl_set_get_space(set1
);
3026 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3027 aff
= isl_aff_add_constant_si(aff
, -1);
3028 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3033 return isl_pw_aff_zero_set(pwaff1
);
3034 return isl_pw_aff_nonneg_set(pwaff1
);
3037 /* Return a set containing those elements in the shared domain
3038 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3040 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3041 __isl_take isl_pw_aff
*pwaff2
)
3043 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3044 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3047 /* Return a set containing those elements in the shared domain
3048 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3050 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3051 __isl_take isl_pw_aff
*pwaff2
)
3053 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3054 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3057 /* Return a set containing those elements in the shared domain
3058 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3060 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3061 __isl_take isl_pw_aff
*pwaff2
)
3063 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3064 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3067 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3068 __isl_take isl_pw_aff
*pwaff2
)
3070 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3073 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3074 __isl_take isl_pw_aff
*pwaff2
)
3076 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3079 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3080 * where the function values are ordered in the same way as "order",
3081 * which returns a set in the shared domain of its two arguments.
3083 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3084 * We first pull back the two functions such that they are defined on
3085 * the domain [A -> B]. Then we apply "order", resulting in a set
3086 * in the space [A -> B]. Finally, we unwrap this set to obtain
3087 * a map in the space A -> B.
3089 static __isl_give isl_map
*isl_pw_aff_order_map(
3090 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3091 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3092 __isl_take isl_pw_aff
*pa2
))
3094 isl_space
*space1
, *space2
;
3098 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3099 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3100 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3101 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3102 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3103 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3104 ma
= isl_multi_aff_range_map(space1
);
3105 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3106 set
= order(pa1
, pa2
);
3108 return isl_set_unwrap(set
);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function values are equal.
3114 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3115 __isl_take isl_pw_aff
*pa2
)
3117 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3120 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3121 * where the function value of "pa1" is less than or equal to
3122 * the function value of "pa2".
3124 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3125 __isl_take isl_pw_aff
*pa2
)
3127 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3130 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3131 * where the function value of "pa1" is less than the function value of "pa2".
3133 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3134 __isl_take isl_pw_aff
*pa2
)
3136 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3139 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3140 * where the function value of "pa1" is greater than or equal to
3141 * the function value of "pa2".
3143 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3144 __isl_take isl_pw_aff
*pa2
)
3146 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3149 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3150 * where the function value of "pa1" is greater than the function value
3153 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3154 __isl_take isl_pw_aff
*pa2
)
3156 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3159 /* Return a set containing those elements in the shared domain
3160 * of the elements of list1 and list2 where each element in list1
3161 * has the relation specified by "fn" with each element in list2.
3163 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3164 __isl_take isl_pw_aff_list
*list2
,
3165 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3166 __isl_take isl_pw_aff
*pwaff2
))
3172 if (!list1
|| !list2
)
3175 ctx
= isl_pw_aff_list_get_ctx(list1
);
3176 if (list1
->n
< 1 || list2
->n
< 1)
3177 isl_die(ctx
, isl_error_invalid
,
3178 "list should contain at least one element", goto error
);
3180 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3181 for (i
= 0; i
< list1
->n
; ++i
)
3182 for (j
= 0; j
< list2
->n
; ++j
) {
3185 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3186 isl_pw_aff_copy(list2
->p
[j
]));
3187 set
= isl_set_intersect(set
, set_ij
);
3190 isl_pw_aff_list_free(list1
);
3191 isl_pw_aff_list_free(list2
);
3194 isl_pw_aff_list_free(list1
);
3195 isl_pw_aff_list_free(list2
);
3199 /* Return a set containing those elements in the shared domain
3200 * of the elements of list1 and list2 where each element in list1
3201 * is equal to each element in list2.
3203 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3204 __isl_take isl_pw_aff_list
*list2
)
3206 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3209 __isl_give isl_set
*isl_pw_aff_list_ne_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_ne_set
);
3215 /* Return a set containing those elements in the shared domain
3216 * of the elements of list1 and list2 where each element in list1
3217 * is less than or equal to each element in list2.
3219 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3220 __isl_take isl_pw_aff_list
*list2
)
3222 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3225 __isl_give isl_set
*isl_pw_aff_list_lt_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_lt_set
);
3231 __isl_give isl_set
*isl_pw_aff_list_ge_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_ge_set
);
3237 __isl_give isl_set
*isl_pw_aff_list_gt_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_gt_set
);
3244 /* Return a set containing those elements in the shared domain
3245 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3247 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3248 __isl_take isl_pw_aff
*pwaff2
)
3250 isl_set
*set_lt
, *set_gt
;
3252 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3253 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3254 isl_pw_aff_copy(pwaff2
));
3255 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3256 return isl_set_union_disjoint(set_lt
, set_gt
);
3259 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3264 if (isl_int_is_one(v
))
3266 if (!isl_int_is_pos(v
))
3267 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3268 "factor needs to be positive",
3269 return isl_pw_aff_free(pwaff
));
3270 pwaff
= isl_pw_aff_cow(pwaff
);
3276 for (i
= 0; i
< pwaff
->n
; ++i
) {
3277 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3278 if (!pwaff
->p
[i
].aff
)
3279 return isl_pw_aff_free(pwaff
);
3285 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3287 return isl_pw_aff_un_op(pwaff
, &isl_aff_floor
);
3290 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3292 return isl_pw_aff_un_op(pwaff
, &isl_aff_ceil
);
3295 /* Assuming that "cond1" and "cond2" are disjoint,
3296 * return an affine expression that is equal to pwaff1 on cond1
3297 * and to pwaff2 on cond2.
3299 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3300 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3301 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3303 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3304 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3306 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3309 /* Return an affine expression that is equal to pwaff_true for elements
3310 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3312 * That is, return cond ? pwaff_true : pwaff_false;
3314 * If "cond" involves and NaN, then we conservatively return a NaN
3315 * on its entire domain. In principle, we could consider the pieces
3316 * where it is NaN separately from those where it is not.
3318 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3319 * then only use the domain of "cond" to restrict the domain.
3321 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3322 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3324 isl_set
*cond_true
, *cond_false
;
3329 if (isl_pw_aff_involves_nan(cond
)) {
3330 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3331 isl_local_space
*ls
= isl_local_space_from_space(space
);
3332 isl_pw_aff_free(cond
);
3333 isl_pw_aff_free(pwaff_true
);
3334 isl_pw_aff_free(pwaff_false
);
3335 return isl_pw_aff_nan_on_domain(ls
);
3338 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3339 isl_pw_aff_get_space(pwaff_false
));
3340 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3341 isl_pw_aff_get_space(pwaff_true
));
3342 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3348 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3349 isl_pw_aff_free(pwaff_false
);
3350 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3353 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3354 cond_false
= isl_pw_aff_zero_set(cond
);
3355 return isl_pw_aff_select(cond_true
, pwaff_true
,
3356 cond_false
, pwaff_false
);
3358 isl_pw_aff_free(cond
);
3359 isl_pw_aff_free(pwaff_true
);
3360 isl_pw_aff_free(pwaff_false
);
3364 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3369 return isl_bool_error
;
3371 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3372 return isl_bool_ok(pos
== -1);
3375 /* Check whether pwaff is a piecewise constant.
3377 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3382 return isl_bool_error
;
3384 for (i
= 0; i
< pwaff
->n
; ++i
) {
3385 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3386 if (is_cst
< 0 || !is_cst
)
3390 return isl_bool_true
;
3393 /* Return the product of "aff1" and "aff2".
3395 * If either of the two is NaN, then the result is NaN.
3397 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3399 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3400 __isl_take isl_aff
*aff2
)
3405 if (isl_aff_is_nan(aff1
)) {
3409 if (isl_aff_is_nan(aff2
)) {
3414 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3415 return isl_aff_mul(aff2
, aff1
);
3417 if (!isl_aff_is_cst(aff2
))
3418 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3419 "at least one affine expression should be constant",
3422 aff1
= isl_aff_cow(aff1
);
3426 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3427 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3437 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3439 * If either of the two is NaN, then the result is NaN.
3440 * A division by zero also results in NaN.
3442 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3443 __isl_take isl_aff
*aff2
)
3445 isl_bool is_cst
, is_zero
;
3451 if (isl_aff_is_nan(aff1
)) {
3455 if (isl_aff_is_nan(aff2
)) {
3460 is_cst
= isl_aff_is_cst(aff2
);
3464 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3465 "second argument should be a constant", goto error
);
3466 is_zero
= isl_aff_plain_is_zero(aff2
);
3470 return set_nan_free(aff1
, aff2
);
3472 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3474 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3475 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3478 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3479 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3482 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3483 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3494 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3495 __isl_take isl_pw_aff
*pwaff2
)
3497 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3498 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3501 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3502 __isl_take isl_pw_aff
*pwaff2
)
3504 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3507 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3508 __isl_take isl_pw_aff
*pwaff2
)
3510 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3511 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3514 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3516 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3517 __isl_take isl_pw_aff
*pa2
)
3521 is_cst
= isl_pw_aff_is_cst(pa2
);
3525 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3526 "second argument should be a piecewise constant",
3528 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3529 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3531 isl_pw_aff_free(pa1
);
3532 isl_pw_aff_free(pa2
);
3536 /* Compute the quotient of the integer division of "pa1" by "pa2"
3537 * with rounding towards zero.
3538 * "pa2" is assumed to be a piecewise constant.
3540 * In particular, return
3542 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3545 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3546 __isl_take isl_pw_aff
*pa2
)
3552 is_cst
= isl_pw_aff_is_cst(pa2
);
3556 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3557 "second argument should be a piecewise constant",
3560 pa1
= isl_pw_aff_div(pa1
, pa2
);
3562 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3563 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3564 c
= isl_pw_aff_ceil(pa1
);
3565 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3567 isl_pw_aff_free(pa1
);
3568 isl_pw_aff_free(pa2
);
3572 /* Compute the remainder of the integer division of "pa1" by "pa2"
3573 * with rounding towards zero.
3574 * "pa2" is assumed to be a piecewise constant.
3576 * In particular, return
3578 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3581 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3582 __isl_take isl_pw_aff
*pa2
)
3587 is_cst
= isl_pw_aff_is_cst(pa2
);
3591 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3592 "second argument should be a piecewise constant",
3594 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3595 res
= isl_pw_aff_mul(pa2
, res
);
3596 res
= isl_pw_aff_sub(pa1
, res
);
3599 isl_pw_aff_free(pa1
);
3600 isl_pw_aff_free(pa2
);
3604 /* Does either of "pa1" or "pa2" involve any NaN2?
3606 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3607 __isl_keep isl_pw_aff
*pa2
)
3611 has_nan
= isl_pw_aff_involves_nan(pa1
);
3612 if (has_nan
< 0 || has_nan
)
3614 return isl_pw_aff_involves_nan(pa2
);
3617 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3618 * by a NaN on their shared domain.
3620 * In principle, the result could be refined to only being NaN
3621 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3623 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3624 __isl_take isl_pw_aff
*pa2
)
3626 isl_local_space
*ls
;
3630 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3631 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3632 pa
= isl_pw_aff_nan_on_domain(ls
);
3633 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3638 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3639 __isl_take isl_pw_aff
*pwaff2
)
3644 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3645 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3646 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3647 isl_pw_aff_copy(pwaff2
));
3648 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3649 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3652 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3653 __isl_take isl_pw_aff
*pwaff2
)
3658 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3659 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3660 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3661 isl_pw_aff_copy(pwaff2
));
3662 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3663 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3666 /* Return an expression for the minimum (if "max" is not set) or
3667 * the maximum (if "max" is set) of "pa1" and "pa2".
3668 * If either expression involves any NaN, then return a NaN
3669 * on the shared domain as result.
3671 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3672 __isl_take isl_pw_aff
*pa2
, int max
)
3676 has_nan
= either_involves_nan(pa1
, pa2
);
3678 pa1
= isl_pw_aff_free(pa1
);
3680 return replace_by_nan(pa1
, pa2
);
3682 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3684 return pw_aff_max(pa1
, pa2
);
3686 return pw_aff_min(pa1
, pa2
);
3689 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3691 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3692 __isl_take isl_pw_aff
*pwaff2
)
3694 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3697 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3699 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3700 __isl_take isl_pw_aff
*pwaff2
)
3702 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3705 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3706 __isl_take isl_pw_aff_list
*list
,
3707 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3708 __isl_take isl_pw_aff
*pwaff2
))
3717 ctx
= isl_pw_aff_list_get_ctx(list
);
3719 isl_die(ctx
, isl_error_invalid
,
3720 "list should contain at least one element", goto error
);
3722 res
= isl_pw_aff_copy(list
->p
[0]);
3723 for (i
= 1; i
< list
->n
; ++i
)
3724 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3726 isl_pw_aff_list_free(list
);
3729 isl_pw_aff_list_free(list
);
3733 /* Return an isl_pw_aff that maps each element in the intersection of the
3734 * domains of the elements of list to the minimal corresponding affine
3737 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3739 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3742 /* Return an isl_pw_aff that maps each element in the intersection of the
3743 * domains of the elements of list to the maximal corresponding affine
3746 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3748 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3751 /* Mark the domains of "pwaff" as rational.
3753 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3757 pwaff
= isl_pw_aff_cow(pwaff
);
3763 for (i
= 0; i
< pwaff
->n
; ++i
) {
3764 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3765 if (!pwaff
->p
[i
].set
)
3766 return isl_pw_aff_free(pwaff
);
3772 /* Mark the domains of the elements of "list" as rational.
3774 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3775 __isl_take isl_pw_aff_list
*list
)
3785 for (i
= 0; i
< n
; ++i
) {
3788 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3789 pa
= isl_pw_aff_set_rational(pa
);
3790 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3796 /* Do the parameters of "aff" match those of "space"?
3798 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3799 __isl_keep isl_space
*space
)
3801 isl_space
*aff_space
;
3805 return isl_bool_error
;
3807 aff_space
= isl_aff_get_domain_space(aff
);
3809 match
= isl_space_has_equal_params(space
, aff_space
);
3811 isl_space_free(aff_space
);
3815 /* Check that the domain space of "aff" matches "space".
3817 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3818 __isl_keep isl_space
*space
)
3820 isl_space
*aff_space
;
3824 return isl_stat_error
;
3826 aff_space
= isl_aff_get_domain_space(aff
);
3828 match
= isl_space_has_equal_params(space
, aff_space
);
3832 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3833 "parameters don't match", goto error
);
3834 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3835 aff_space
, isl_dim_set
);
3839 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3840 "domains don't match", goto error
);
3841 isl_space_free(aff_space
);
3844 isl_space_free(aff_space
);
3845 return isl_stat_error
;
3848 /* Return the shared (universe) domain of the elements of "ma".
3850 * Since an isl_multi_aff (and an isl_aff) is always total,
3851 * the domain is always the universe set in its domain space.
3852 * This is a helper function for use in the generic isl_multi_*_bind.
3854 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3855 __isl_take isl_multi_aff
*ma
)
3859 space
= isl_multi_aff_get_space(ma
);
3860 isl_multi_aff_free(ma
);
3862 return isl_basic_set_universe(isl_space_domain(space
));
3868 #include <isl_multi_no_explicit_domain.c>
3869 #include <isl_multi_templ.c>
3870 #include <isl_multi_un_op_templ.c>
3871 #include <isl_multi_bin_val_templ.c>
3872 #include <isl_multi_add_constant_templ.c>
3873 #include <isl_multi_apply_set.c>
3874 #include <isl_multi_arith_templ.c>
3875 #include <isl_multi_bind_domain_templ.c>
3876 #include <isl_multi_cmp.c>
3877 #include <isl_multi_dim_id_templ.c>
3878 #include <isl_multi_dims.c>
3879 #include <isl_multi_floor.c>
3880 #include <isl_multi_from_base_templ.c>
3881 #include <isl_multi_identity_templ.c>
3882 #include <isl_multi_insert_domain_templ.c>
3883 #include <isl_multi_locals_templ.c>
3884 #include <isl_multi_move_dims_templ.c>
3885 #include <isl_multi_nan_templ.c>
3886 #include <isl_multi_product_templ.c>
3887 #include <isl_multi_splice_templ.c>
3888 #include <isl_multi_tuple_id_templ.c>
3889 #include <isl_multi_unbind_params_templ.c>
3890 #include <isl_multi_zero_templ.c>
3894 #include <isl_multi_gist.c>
3897 #define DOMBASE basic_set
3898 #include <isl_multi_bind_templ.c>
3900 /* Construct an isl_multi_aff living in "space" that corresponds
3901 * to the affine transformation matrix "mat".
3903 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3904 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3907 isl_local_space
*ls
= NULL
;
3908 isl_multi_aff
*ma
= NULL
;
3909 isl_size n_row
, n_col
, n_out
, total
;
3915 ctx
= isl_mat_get_ctx(mat
);
3917 n_row
= isl_mat_rows(mat
);
3918 n_col
= isl_mat_cols(mat
);
3919 n_out
= isl_space_dim(space
, isl_dim_out
);
3920 total
= isl_space_dim(space
, isl_dim_all
);
3921 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3924 isl_die(ctx
, isl_error_invalid
,
3925 "insufficient number of rows", goto error
);
3927 isl_die(ctx
, isl_error_invalid
,
3928 "insufficient number of columns", goto error
);
3929 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3930 isl_die(ctx
, isl_error_invalid
,
3931 "dimension mismatch", goto error
);
3933 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3934 space
= isl_space_domain(space
);
3935 ls
= isl_local_space_from_space(isl_space_copy(space
));
3937 for (i
= 0; i
< n_row
- 1; ++i
) {
3941 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3944 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3945 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3946 v
= isl_vec_normalize(v
);
3947 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3948 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3951 isl_space_free(space
);
3952 isl_local_space_free(ls
);
3956 isl_space_free(space
);
3957 isl_local_space_free(ls
);
3959 isl_multi_aff_free(ma
);
3963 /* Return the constant terms of the affine expressions of "ma".
3965 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3966 __isl_keep isl_multi_aff
*ma
)
3973 n
= isl_multi_aff_size(ma
);
3976 space
= isl_space_range(isl_multi_aff_get_space(ma
));
3977 space
= isl_space_drop_all_params(space
);
3978 mv
= isl_multi_val_zero(space
);
3980 for (i
= 0; i
< n
; ++i
) {
3984 aff
= isl_multi_aff_get_at(ma
, i
);
3985 val
= isl_aff_get_constant_val(aff
);
3987 mv
= isl_multi_val_set_at(mv
, i
, val
);
3993 /* Remove any internal structure of the domain of "ma".
3994 * If there is any such internal structure in the input,
3995 * then the name of the corresponding space is also removed.
3997 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3998 __isl_take isl_multi_aff
*ma
)
4005 if (!ma
->space
->nested
[0])
4008 space
= isl_multi_aff_get_space(ma
);
4009 space
= isl_space_flatten_domain(space
);
4010 ma
= isl_multi_aff_reset_space(ma
, space
);
4015 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4016 * of the space to its domain.
4018 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4022 isl_local_space
*ls
;
4027 if (!isl_space_is_map(space
))
4028 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4029 "not a map space", goto error
);
4031 n_in
= isl_space_dim(space
, isl_dim_in
);
4034 space
= isl_space_domain_map(space
);
4036 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4038 isl_space_free(space
);
4042 space
= isl_space_domain(space
);
4043 ls
= isl_local_space_from_space(space
);
4044 for (i
= 0; i
< n_in
; ++i
) {
4047 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4049 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4051 isl_local_space_free(ls
);
4054 isl_space_free(space
);
4058 /* This function performs the same operation as isl_multi_aff_domain_map,
4059 * but is considered as a function on an isl_space when exported.
4061 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4062 __isl_take isl_space
*space
)
4064 return isl_multi_aff_domain_map(space
);
4067 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4068 * of the space to its range.
4070 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4073 isl_size n_in
, n_out
;
4074 isl_local_space
*ls
;
4079 if (!isl_space_is_map(space
))
4080 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4081 "not a map space", goto error
);
4083 n_in
= isl_space_dim(space
, isl_dim_in
);
4084 n_out
= isl_space_dim(space
, isl_dim_out
);
4085 if (n_in
< 0 || n_out
< 0)
4087 space
= isl_space_range_map(space
);
4089 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4091 isl_space_free(space
);
4095 space
= isl_space_domain(space
);
4096 ls
= isl_local_space_from_space(space
);
4097 for (i
= 0; i
< n_out
; ++i
) {
4100 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4101 isl_dim_set
, n_in
+ i
);
4102 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4104 isl_local_space_free(ls
);
4107 isl_space_free(space
);
4111 /* This function performs the same operation as isl_multi_aff_range_map,
4112 * but is considered as a function on an isl_space when exported.
4114 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4115 __isl_take isl_space
*space
)
4117 return isl_multi_aff_range_map(space
);
4120 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4121 * of the space to its domain.
4123 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4124 __isl_take isl_space
*space
)
4126 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4129 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4130 * but is considered as a function on an isl_space when exported.
4132 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4133 __isl_take isl_space
*space
)
4135 return isl_pw_multi_aff_domain_map(space
);
4138 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4139 * of the space to its range.
4141 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4142 __isl_take isl_space
*space
)
4144 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4147 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4148 * but is considered as a function on an isl_space when exported.
4150 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4151 __isl_take isl_space
*space
)
4153 return isl_pw_multi_aff_range_map(space
);
4156 /* Given the space of a set and a range of set dimensions,
4157 * construct an isl_multi_aff that projects out those dimensions.
4159 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4160 __isl_take isl_space
*space
, enum isl_dim_type type
,
4161 unsigned first
, unsigned n
)
4165 isl_local_space
*ls
;
4170 if (!isl_space_is_set(space
))
4171 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4172 "expecting set space", goto error
);
4173 if (type
!= isl_dim_set
)
4174 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4175 "only set dimensions can be projected out", goto error
);
4176 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4179 dim
= isl_space_dim(space
, isl_dim_set
);
4183 space
= isl_space_from_domain(space
);
4184 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4187 return isl_multi_aff_alloc(space
);
4189 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4190 space
= isl_space_domain(space
);
4191 ls
= isl_local_space_from_space(space
);
4193 for (i
= 0; i
< first
; ++i
) {
4196 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4198 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4201 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4204 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4205 isl_dim_set
, first
+ n
+ i
);
4206 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4209 isl_local_space_free(ls
);
4212 isl_space_free(space
);
4216 /* Given the space of a set and a range of set dimensions,
4217 * construct an isl_pw_multi_aff that projects out those dimensions.
4219 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4220 __isl_take isl_space
*space
, enum isl_dim_type type
,
4221 unsigned first
, unsigned n
)
4225 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4226 return isl_pw_multi_aff_from_multi_aff(ma
);
4229 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4230 * but is considered as a function on an isl_multi_aff when exported.
4232 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4233 __isl_take isl_multi_aff
*ma
)
4235 return isl_pw_multi_aff_from_multi_aff(ma
);
4238 /* Create a piecewise multi-affine expression in the given space that maps each
4239 * input dimension to the corresponding output dimension.
4241 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4242 __isl_take isl_space
*space
)
4244 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4247 /* Create a piecewise multi expression that maps elements in the given space
4250 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4251 __isl_take isl_space
*space
)
4255 ma
= isl_multi_aff_identity_on_domain_space(space
);
4256 return isl_pw_multi_aff_from_multi_aff(ma
);
4259 /* This function performs the same operation as
4260 * isl_pw_multi_aff_identity_on_domain_space,
4261 * but is considered as a function on an isl_space when exported.
4263 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4264 __isl_take isl_space
*space
)
4266 return isl_pw_multi_aff_identity_on_domain_space(space
);
4269 /* Exploit the equalities in "eq" to simplify the affine expressions.
4271 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4272 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4277 n
= isl_multi_aff_size(maff
);
4281 for (i
= 0; i
< n
; ++i
) {
4284 aff
= isl_multi_aff_take_at(maff
, i
);
4285 aff
= isl_aff_substitute_equalities(aff
,
4286 isl_basic_set_copy(eq
));
4287 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4290 isl_basic_set_free(eq
);
4293 isl_basic_set_free(eq
);
4294 isl_multi_aff_free(maff
);
4298 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4304 n
= isl_multi_aff_size(maff
);
4306 return isl_multi_aff_free(maff
);
4308 for (i
= 0; i
< n
; ++i
) {
4311 aff
= isl_multi_aff_take_at(maff
, i
);
4312 aff
= isl_aff_scale(aff
, f
);
4313 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4319 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4320 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4322 maff1
= isl_multi_aff_add(maff1
, maff2
);
4323 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4327 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4330 return isl_bool_error
;
4332 return isl_bool_false
;
4335 /* Return the set of domain elements where "ma1" is lexicographically
4336 * smaller than or equal to "ma2".
4338 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4339 __isl_take isl_multi_aff
*ma2
)
4341 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4344 /* Return the set of domain elements where "ma1" is lexicographically
4345 * smaller than "ma2".
4347 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4348 __isl_take isl_multi_aff
*ma2
)
4350 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4353 /* Return the set of domain elements where "ma1" is lexicographically
4354 * greater than to "ma2". If "equal" is set, then include the domain
4355 * elements where they are equal.
4356 * Do this for the case where there are no entries.
4357 * In this case, "ma1" cannot be greater than "ma2",
4358 * but it is (greater than or) equal to "ma2".
4360 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4361 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4365 space
= isl_multi_aff_get_domain_space(ma1
);
4367 isl_multi_aff_free(ma1
);
4368 isl_multi_aff_free(ma2
);
4371 return isl_set_universe(space
);
4373 return isl_set_empty(space
);
4376 /* Return the set where entry "i" of "ma1" and "ma2"
4377 * satisfy the relation prescribed by "cmp".
4379 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4380 __isl_keep isl_multi_aff
*ma2
, int i
,
4381 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4382 __isl_take isl_aff
*aff2
))
4384 isl_aff
*aff1
, *aff2
;
4386 aff1
= isl_multi_aff_get_at(ma1
, i
);
4387 aff2
= isl_multi_aff_get_at(ma2
, i
);
4388 return cmp(aff1
, aff2
);
4391 /* Return the set of domain elements where "ma1" is lexicographically
4392 * greater than to "ma2". If "equal" is set, then include the domain
4393 * elements where they are equal.
4395 * In particular, for all but the final entry,
4396 * include the set of elements where this entry is strictly greater in "ma1"
4397 * and all previous entries are equal.
4398 * The final entry is also allowed to be equal in the two functions
4399 * if "equal" is set.
4401 * The case where there are no entries is handled separately.
4403 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4404 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4413 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4415 n
= isl_multi_aff_size(ma1
);
4419 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4421 space
= isl_multi_aff_get_domain_space(ma1
);
4422 res
= isl_set_empty(isl_space_copy(space
));
4423 equal_set
= isl_set_universe(space
);
4425 for (i
= 0; i
+ 1 < n
; ++i
) {
4429 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4430 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4431 res
= isl_set_union(res
, gt
);
4432 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4433 equal_set
= isl_set_intersect(equal_set
, eq
);
4435 empty
= isl_set_is_empty(equal_set
);
4436 if (empty
>= 0 && empty
)
4441 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4443 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4444 isl_multi_aff_free(ma1
);
4445 isl_multi_aff_free(ma2
);
4447 gte
= isl_set_intersect(gte
, equal_set
);
4448 return isl_set_union(res
, gte
);
4450 isl_multi_aff_free(ma1
);
4451 isl_multi_aff_free(ma2
);
4455 /* Return the set of domain elements where "ma1" is lexicographically
4456 * greater than or equal to "ma2".
4458 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4459 __isl_take isl_multi_aff
*ma2
)
4461 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4464 /* Return the set of domain elements where "ma1" is lexicographically
4465 * greater than "ma2".
4467 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4468 __isl_take isl_multi_aff
*ma2
)
4470 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4473 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4476 #define PW isl_pw_multi_aff
4478 #define BASE multi_aff
4480 #define EL_IS_ZERO is_empty
4484 #define IS_ZERO is_empty
4487 #undef DEFAULT_IS_ZERO
4488 #define DEFAULT_IS_ZERO 0
4490 #include <isl_pw_templ.c>
4491 #include <isl_pw_un_op_templ.c>
4492 #include <isl_pw_add_constant_multi_val_templ.c>
4493 #include <isl_pw_add_constant_val_templ.c>
4494 #include <isl_pw_bind_domain_templ.c>
4495 #include <isl_pw_insert_dims_templ.c>
4496 #include <isl_pw_insert_domain_templ.c>
4497 #include <isl_pw_locals_templ.c>
4498 #include <isl_pw_move_dims_templ.c>
4499 #include <isl_pw_neg_templ.c>
4500 #include <isl_pw_pullback_templ.c>
4501 #include <isl_pw_range_tuple_id_templ.c>
4502 #include <isl_pw_union_opt.c>
4505 #define BASE pw_multi_aff
4507 #include <isl_union_multi.c>
4508 #include "isl_union_locals_templ.c"
4509 #include <isl_union_neg.c>
4512 #define BASE multi_aff
4514 #include <isl_union_pw_templ.c>
4516 /* Generic function for extracting a factor from a product "pma".
4517 * "check_space" checks that the space is that of the right kind of product.
4518 * "space_factor" extracts the factor from the space.
4519 * "multi_aff_factor" extracts the factor from the constituent functions.
4521 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4522 __isl_take isl_pw_multi_aff
*pma
,
4523 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4524 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4525 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4526 __isl_take isl_multi_aff
*ma
))
4531 if (check_space(pma
) < 0)
4532 return isl_pw_multi_aff_free(pma
);
4534 space
= isl_pw_multi_aff_take_space(pma
);
4535 space
= space_factor(space
);
4537 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4540 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4541 ma
= multi_aff_factor(ma
);
4542 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4545 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4550 /* Is the range of "pma" a wrapped relation?
4552 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4553 __isl_keep isl_pw_multi_aff
*pma
)
4555 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4558 /* Check that the range of "pma" is a product.
4560 static isl_stat
pw_multi_aff_check_range_product(
4561 __isl_keep isl_pw_multi_aff
*pma
)
4565 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4567 return isl_stat_error
;
4569 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4570 "range is not a product", return isl_stat_error
);
4574 /* Given a function A -> [B -> C], extract the function A -> B.
4576 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4577 __isl_take isl_pw_multi_aff
*pma
)
4579 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4580 &isl_space_range_factor_domain
,
4581 &isl_multi_aff_range_factor_domain
);
4584 /* Given a function A -> [B -> C], extract the function A -> C.
4586 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4587 __isl_take isl_pw_multi_aff
*pma
)
4589 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4590 &isl_space_range_factor_range
,
4591 &isl_multi_aff_range_factor_range
);
4594 /* Given two piecewise multi affine expressions, return a piecewise
4595 * multi-affine expression defined on the union of the definition domains
4596 * of the inputs that is equal to the lexicographic maximum of the two
4597 * inputs on each cell. If only one of the two inputs is defined on
4598 * a given cell, then it is considered to be the maximum.
4600 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4601 __isl_take isl_pw_multi_aff
*pma1
,
4602 __isl_take isl_pw_multi_aff
*pma2
)
4604 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4605 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4606 &isl_multi_aff_lex_ge_set
);
4609 /* Given two piecewise multi affine expressions, return a piecewise
4610 * multi-affine expression defined on the union of the definition domains
4611 * of the inputs that is equal to the lexicographic minimum of the two
4612 * inputs on each cell. If only one of the two inputs is defined on
4613 * a given cell, then it is considered to be the minimum.
4615 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4616 __isl_take isl_pw_multi_aff
*pma1
,
4617 __isl_take isl_pw_multi_aff
*pma2
)
4619 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4620 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4621 &isl_multi_aff_lex_le_set
);
4624 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4625 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4627 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4628 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4629 &isl_multi_aff_add
);
4632 /* Subtract "pma2" from "pma1" and return the result.
4634 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4635 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4637 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4638 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4639 &isl_multi_aff_sub
);
4642 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4643 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4645 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4648 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4649 * with the actual sum on the shared domain and
4650 * the defined expression on the symmetric difference of the domains.
4652 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4653 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4655 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4658 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4659 * with the actual sum on the shared domain and
4660 * the defined expression on the symmetric difference of the domains.
4662 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4663 __isl_take isl_union_pw_multi_aff
*upma1
,
4664 __isl_take isl_union_pw_multi_aff
*upma2
)
4666 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4669 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4670 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4672 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4673 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4677 isl_pw_multi_aff
*res
;
4679 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4682 n
= pma1
->n
* pma2
->n
;
4683 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4684 isl_space_copy(pma2
->dim
));
4685 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4687 for (i
= 0; i
< pma1
->n
; ++i
) {
4688 for (j
= 0; j
< pma2
->n
; ++j
) {
4692 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4693 isl_set_copy(pma2
->p
[j
].set
));
4694 ma
= isl_multi_aff_product(
4695 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4696 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4697 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4701 isl_pw_multi_aff_free(pma1
);
4702 isl_pw_multi_aff_free(pma2
);
4705 isl_pw_multi_aff_free(pma1
);
4706 isl_pw_multi_aff_free(pma2
);
4710 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4711 * denominator "denom".
4712 * "denom" is allowed to be negative, in which case the actual denominator
4713 * is -denom and the expressions are added instead.
4715 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4716 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4722 first
= isl_seq_first_non_zero(c
, n
);
4726 sign
= isl_int_sgn(denom
);
4728 isl_int_abs(d
, denom
);
4729 for (i
= first
; i
< n
; ++i
) {
4732 if (isl_int_is_zero(c
[i
]))
4734 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4735 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4736 aff_i
= isl_aff_scale_down(aff_i
, d
);
4738 aff
= isl_aff_sub(aff
, aff_i
);
4740 aff
= isl_aff_add(aff
, aff_i
);
4747 /* Extract an affine expression that expresses the output dimension "pos"
4748 * of "bmap" in terms of the parameters and input dimensions from
4750 * Note that this expression may involve integer divisions defined
4751 * in terms of parameters and input dimensions.
4752 * The equality may also involve references to earlier (but not later)
4753 * output dimensions. These are replaced by the corresponding elements
4756 * If the equality is of the form
4758 * f(i) + h(j) + a x + g(i) = 0,
4760 * with f(i) a linear combinations of the parameters and input dimensions,
4761 * g(i) a linear combination of integer divisions defined in terms of the same
4762 * and h(j) a linear combinations of earlier output dimensions,
4763 * then the affine expression is
4765 * (-f(i) - g(i))/a - h(j)/a
4767 * If the equality is of the form
4769 * f(i) + h(j) - a x + g(i) = 0,
4771 * then the affine expression is
4773 * (f(i) + g(i))/a - h(j)/(-a)
4776 * If "div" refers to an integer division (i.e., it is smaller than
4777 * the number of integer divisions), then the equality constraint
4778 * does involve an integer division (the one at position "div") that
4779 * is defined in terms of output dimensions. However, this integer
4780 * division can be eliminated by exploiting a pair of constraints
4781 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4782 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4784 * In particular, let
4786 * x = e(i) + m floor(...)
4788 * with e(i) the expression derived above and floor(...) the integer
4789 * division involving output dimensions.
4800 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4801 * = (e(i) - l) mod m
4805 * x - l = (e(i) - l) mod m
4809 * x = ((e(i) - l) mod m) + l
4811 * The variable "shift" below contains the expression -l, which may
4812 * also involve a linear combination of earlier output dimensions.
4814 static __isl_give isl_aff
*extract_aff_from_equality(
4815 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4816 __isl_keep isl_multi_aff
*ma
)
4819 isl_size n_div
, n_out
;
4821 isl_local_space
*ls
;
4822 isl_aff
*aff
, *shift
;
4825 ctx
= isl_basic_map_get_ctx(bmap
);
4826 ls
= isl_basic_map_get_local_space(bmap
);
4827 ls
= isl_local_space_domain(ls
);
4828 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4831 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4832 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4833 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4834 if (n_out
< 0 || n_div
< 0)
4836 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4837 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4838 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4839 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4841 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4842 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4843 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4846 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4847 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4848 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4849 bmap
->eq
[eq
][o_out
+ pos
]);
4851 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4854 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4855 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4856 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4857 isl_int_set_si(shift
->v
->el
[0], 1);
4858 shift
= subtract_initial(shift
, ma
, pos
,
4859 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4860 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4861 mod
= isl_val_int_from_isl_int(ctx
,
4862 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4863 mod
= isl_val_abs(mod
);
4864 aff
= isl_aff_mod_val(aff
, mod
);
4865 aff
= isl_aff_sub(aff
, shift
);
4868 isl_local_space_free(ls
);
4871 isl_local_space_free(ls
);
4876 /* Given a basic map with output dimensions defined
4877 * in terms of the parameters input dimensions and earlier
4878 * output dimensions using an equality (and possibly a pair on inequalities),
4879 * extract an isl_aff that expresses output dimension "pos" in terms
4880 * of the parameters and input dimensions.
4881 * Note that this expression may involve integer divisions defined
4882 * in terms of parameters and input dimensions.
4883 * "ma" contains the expressions corresponding to earlier output dimensions.
4885 * This function shares some similarities with
4886 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4888 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4889 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4896 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4897 if (eq
>= bmap
->n_eq
)
4898 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4899 "unable to find suitable equality", return NULL
);
4900 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4902 aff
= isl_aff_remove_unused_divs(aff
);
4906 /* Given a basic map where each output dimension is defined
4907 * in terms of the parameters and input dimensions using an equality,
4908 * extract an isl_multi_aff that expresses the output dimensions in terms
4909 * of the parameters and input dimensions.
4911 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4912 __isl_take isl_basic_map
*bmap
)
4921 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4922 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4924 ma
= isl_multi_aff_free(ma
);
4926 for (i
= 0; i
< n_out
; ++i
) {
4929 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4930 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4933 isl_basic_map_free(bmap
);
4938 /* Given a basic set where each set dimension is defined
4939 * in terms of the parameters using an equality,
4940 * extract an isl_multi_aff that expresses the set dimensions in terms
4941 * of the parameters.
4943 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4944 __isl_take isl_basic_set
*bset
)
4946 return extract_isl_multi_aff_from_basic_map(bset
);
4949 /* Create an isl_pw_multi_aff that is equivalent to
4950 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4951 * The given basic map is such that each output dimension is defined
4952 * in terms of the parameters and input dimensions using an equality.
4954 * Since some applications expect the result of isl_pw_multi_aff_from_map
4955 * to only contain integer affine expressions, we compute the floor
4956 * of the expression before returning.
4958 * Remove all constraints involving local variables without
4959 * an explicit representation (resulting in the removal of those
4960 * local variables) prior to the actual extraction to ensure
4961 * that the local spaces in which the resulting affine expressions
4962 * are created do not contain any unknown local variables.
4963 * Removing such constraints is safe because constraints involving
4964 * unknown local variables are not used to determine whether
4965 * a basic map is obviously single-valued.
4967 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4968 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4972 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4973 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4974 ma
= isl_multi_aff_floor(ma
);
4975 return isl_pw_multi_aff_alloc(domain
, ma
);
4978 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4979 * This obviously only works if the input "map" is single-valued.
4980 * If so, we compute the lexicographic minimum of the image in the form
4981 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4982 * to its lexicographic minimum.
4983 * If the input is not single-valued, we produce an error.
4985 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4986 __isl_take isl_map
*map
)
4990 isl_pw_multi_aff
*pma
;
4992 sv
= isl_map_is_single_valued(map
);
4996 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4997 "map is not single-valued", goto error
);
4998 map
= isl_map_make_disjoint(map
);
5002 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5004 for (i
= 0; i
< map
->n
; ++i
) {
5005 isl_pw_multi_aff
*pma_i
;
5006 isl_basic_map
*bmap
;
5007 bmap
= isl_basic_map_copy(map
->p
[i
]);
5008 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5009 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5019 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5020 * taking into account that the output dimension at position "d"
5021 * can be represented as
5023 * x = floor((e(...) + c1) / m)
5025 * given that constraint "i" is of the form
5027 * e(...) + c1 - m x >= 0
5030 * Let "map" be of the form
5034 * We construct a mapping
5036 * A -> [A -> x = floor(...)]
5038 * apply that to the map, obtaining
5040 * [A -> x = floor(...)] -> B
5042 * and equate dimension "d" to x.
5043 * We then compute a isl_pw_multi_aff representation of the resulting map
5044 * and plug in the mapping above.
5046 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5047 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5050 isl_space
*space
= NULL
;
5051 isl_local_space
*ls
;
5059 isl_pw_multi_aff
*pma
;
5062 is_set
= isl_map_is_set(map
);
5066 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5067 ctx
= isl_map_get_ctx(map
);
5068 space
= isl_space_domain(isl_map_get_space(map
));
5069 n_in
= isl_space_dim(space
, isl_dim_set
);
5070 n
= isl_space_dim(space
, isl_dim_all
);
5071 if (n_in
< 0 || n
< 0)
5074 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5076 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5077 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5079 isl_basic_map_free(hull
);
5081 ls
= isl_local_space_from_space(isl_space_copy(space
));
5082 aff
= isl_aff_alloc_vec(ls
, v
);
5083 aff
= isl_aff_floor(aff
);
5085 isl_space_free(space
);
5086 ma
= isl_multi_aff_from_aff(aff
);
5088 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5089 ma
= isl_multi_aff_range_product(ma
,
5090 isl_multi_aff_from_aff(aff
));
5093 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5094 map
= isl_map_apply_domain(map
, insert
);
5095 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5096 pma
= isl_pw_multi_aff_from_map(map
);
5097 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5101 isl_space_free(space
);
5103 isl_basic_map_free(hull
);
5107 /* Is constraint "c" of the form
5109 * e(...) + c1 - m x >= 0
5113 * -e(...) + c2 + m x >= 0
5115 * where m > 1 and e only depends on parameters and input dimensions?
5117 * "offset" is the offset of the output dimensions
5118 * "pos" is the position of output dimension x.
5120 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5122 if (isl_int_is_zero(c
[offset
+ d
]))
5124 if (isl_int_is_one(c
[offset
+ d
]))
5126 if (isl_int_is_negone(c
[offset
+ d
]))
5128 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5130 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5131 total
- (offset
+ d
+ 1)) != -1)
5136 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5138 * As a special case, we first check if there is any pair of constraints,
5139 * shared by all the basic maps in "map" that force a given dimension
5140 * to be equal to the floor of some affine combination of the input dimensions.
5142 * In particular, if we can find two constraints
5144 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5148 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5150 * where m > 1 and e only depends on parameters and input dimensions,
5153 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5155 * then we know that we can take
5157 * x = floor((e(...) + c1) / m)
5159 * without having to perform any computation.
5161 * Note that we know that
5165 * If c1 + c2 were 0, then we would have detected an equality during
5166 * simplification. If c1 + c2 were negative, then we would have detected
5169 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5170 __isl_take isl_map
*map
)
5178 isl_basic_map
*hull
;
5180 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5181 dim
= isl_map_dim(map
, isl_dim_out
);
5182 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5183 if (dim
< 0 || total
< 0)
5187 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5189 for (d
= 0; d
< dim
; ++d
) {
5190 for (i
= 0; i
< n
; ++i
) {
5191 if (!is_potential_div_constraint(hull
->ineq
[i
],
5192 offset
, d
, 1 + total
))
5194 for (j
= i
+ 1; j
< n
; ++j
) {
5195 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5196 hull
->ineq
[j
] + 1, total
))
5198 isl_int_add(sum
, hull
->ineq
[i
][0],
5200 if (isl_int_abs_lt(sum
,
5201 hull
->ineq
[i
][offset
+ d
]))
5208 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5210 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5214 isl_basic_map_free(hull
);
5215 return pw_multi_aff_from_map_base(map
);
5218 isl_basic_map_free(hull
);
5222 /* Given an affine expression
5224 * [A -> B] -> f(A,B)
5226 * construct an isl_multi_aff
5230 * such that dimension "d" in B' is set to "aff" and the remaining
5231 * dimensions are set equal to the corresponding dimensions in B.
5232 * "n_in" is the dimension of the space A.
5233 * "n_out" is the dimension of the space B.
5235 * If "is_set" is set, then the affine expression is of the form
5239 * and we construct an isl_multi_aff
5243 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5244 unsigned n_in
, unsigned n_out
, int is_set
)
5248 isl_space
*space
, *space2
;
5249 isl_local_space
*ls
;
5251 space
= isl_aff_get_domain_space(aff
);
5252 ls
= isl_local_space_from_space(isl_space_copy(space
));
5253 space2
= isl_space_copy(space
);
5255 space2
= isl_space_range(isl_space_unwrap(space2
));
5256 space
= isl_space_map_from_domain_and_range(space
, space2
);
5257 ma
= isl_multi_aff_alloc(space
);
5258 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5260 for (i
= 0; i
< n_out
; ++i
) {
5263 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5264 isl_dim_set
, n_in
+ i
);
5265 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5268 isl_local_space_free(ls
);
5273 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5274 * taking into account that the dimension at position "d" can be written as
5276 * x = m a + f(..) (1)
5278 * where m is equal to "gcd".
5279 * "i" is the index of the equality in "hull" that defines f(..).
5280 * In particular, the equality is of the form
5282 * f(..) - x + m g(existentials) = 0
5286 * -f(..) + x + m g(existentials) = 0
5288 * We basically plug (1) into "map", resulting in a map with "a"
5289 * in the range instead of "x". The corresponding isl_pw_multi_aff
5290 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5292 * Specifically, given the input map
5296 * We first wrap it into a set
5300 * and define (1) on top of the corresponding space, resulting in "aff".
5301 * We use this to create an isl_multi_aff that maps the output position "d"
5302 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5303 * We plug this into the wrapped map, unwrap the result and compute the
5304 * corresponding isl_pw_multi_aff.
5305 * The result is an expression
5313 * so that we can plug that into "aff", after extending the latter to
5319 * If "map" is actually a set, then there is no "A" space, meaning
5320 * that we do not need to perform any wrapping, and that the result
5321 * of the recursive call is of the form
5325 * which is plugged into a mapping of the form
5329 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5330 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5335 isl_local_space
*ls
;
5338 isl_pw_multi_aff
*pma
, *id
;
5344 is_set
= isl_map_is_set(map
);
5348 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5349 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5350 if (n_in
< 0 || n_out
< 0)
5352 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5357 set
= isl_map_wrap(map
);
5358 space
= isl_space_map_from_set(isl_set_get_space(set
));
5359 ma
= isl_multi_aff_identity(space
);
5360 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5361 aff
= isl_aff_alloc(ls
);
5363 isl_int_set_si(aff
->v
->el
[0], 1);
5364 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5365 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5368 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5370 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5372 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5373 set
= isl_set_preimage_multi_aff(set
, ma
);
5375 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5380 map
= isl_set_unwrap(set
);
5381 pma
= isl_pw_multi_aff_from_map(map
);
5384 space
= isl_pw_multi_aff_get_domain_space(pma
);
5385 space
= isl_space_map_from_set(space
);
5386 id
= isl_pw_multi_aff_identity(space
);
5387 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5389 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5390 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5392 isl_basic_map_free(hull
);
5396 isl_basic_map_free(hull
);
5400 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5401 * "hull" contains the equalities valid for "map".
5403 * Check if any of the output dimensions is "strided".
5404 * That is, we check if it can be written as
5408 * with m greater than 1, a some combination of existentially quantified
5409 * variables and f an expression in the parameters and input dimensions.
5410 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5412 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5415 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5416 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5425 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5426 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5427 if (n_div
< 0 || n_out
< 0)
5431 isl_basic_map_free(hull
);
5432 return pw_multi_aff_from_map_check_div(map
);
5437 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5438 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5440 for (i
= 0; i
< n_out
; ++i
) {
5441 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5442 isl_int
*eq
= hull
->eq
[j
];
5443 isl_pw_multi_aff
*res
;
5445 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5446 !isl_int_is_negone(eq
[o_out
+ i
]))
5448 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5450 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5451 n_out
- (i
+ 1)) != -1)
5453 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5454 if (isl_int_is_zero(gcd
))
5456 if (isl_int_is_one(gcd
))
5459 res
= pw_multi_aff_from_map_stride(map
, hull
,
5467 isl_basic_map_free(hull
);
5468 return pw_multi_aff_from_map_check_div(map
);
5471 isl_basic_map_free(hull
);
5475 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5477 * As a special case, we first check if all output dimensions are uniquely
5478 * defined in terms of the parameters and input dimensions over the entire
5479 * domain. If so, we extract the desired isl_pw_multi_aff directly
5480 * from the affine hull of "map" and its domain.
5482 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5485 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5489 isl_basic_map
*hull
;
5491 n
= isl_map_n_basic_map(map
);
5496 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5497 hull
= isl_basic_map_plain_affine_hull(hull
);
5498 sv
= isl_basic_map_plain_is_single_valued(hull
);
5500 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5502 isl_basic_map_free(hull
);
5504 map
= isl_map_detect_equalities(map
);
5505 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5506 sv
= isl_basic_map_plain_is_single_valued(hull
);
5508 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5510 return pw_multi_aff_from_map_check_strides(map
, hull
);
5511 isl_basic_map_free(hull
);
5517 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5518 * but is considered as a function on an isl_map when exported.
5520 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5522 return isl_pw_multi_aff_from_map(map
);
5525 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5527 return isl_pw_multi_aff_from_map(set
);
5530 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5531 * but is considered as a function on an isl_set when exported.
5533 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5535 return isl_pw_multi_aff_from_set(set
);
5538 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5541 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5543 isl_union_pw_multi_aff
**upma
= user
;
5544 isl_pw_multi_aff
*pma
;
5546 pma
= isl_pw_multi_aff_from_map(map
);
5547 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5549 return *upma
? isl_stat_ok
: isl_stat_error
;
5552 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5555 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5556 __isl_take isl_aff
*aff
)
5559 isl_pw_multi_aff
*pma
;
5561 ma
= isl_multi_aff_from_aff(aff
);
5562 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5563 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5566 /* Try and create an isl_union_pw_multi_aff that is equivalent
5567 * to the given isl_union_map.
5568 * The isl_union_map is required to be single-valued in each space.
5569 * Otherwise, an error is produced.
5571 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5572 __isl_take isl_union_map
*umap
)
5575 isl_union_pw_multi_aff
*upma
;
5577 space
= isl_union_map_get_space(umap
);
5578 upma
= isl_union_pw_multi_aff_empty(space
);
5579 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5580 upma
= isl_union_pw_multi_aff_free(upma
);
5581 isl_union_map_free(umap
);
5586 /* This function performs the same operation as
5587 * isl_union_pw_multi_aff_from_union_map,
5588 * but is considered as a function on an isl_union_map when exported.
5590 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5591 __isl_take isl_union_map
*umap
)
5593 return isl_union_pw_multi_aff_from_union_map(umap
);
5596 /* Try and create an isl_union_pw_multi_aff that is equivalent
5597 * to the given isl_union_set.
5598 * The isl_union_set is required to be a singleton in each space.
5599 * Otherwise, an error is produced.
5601 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5602 __isl_take isl_union_set
*uset
)
5604 return isl_union_pw_multi_aff_from_union_map(uset
);
5607 /* Return the piecewise affine expression "set ? 1 : 0".
5609 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5612 isl_space
*space
= isl_set_get_space(set
);
5613 isl_local_space
*ls
= isl_local_space_from_space(space
);
5614 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5615 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5617 one
= isl_aff_add_constant_si(one
, 1);
5618 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5619 set
= isl_set_complement(set
);
5620 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5625 /* Plug in "subs" for dimension "type", "pos" of "aff".
5627 * Let i be the dimension to replace and let "subs" be of the form
5631 * and "aff" of the form
5637 * (a f + d g')/(m d)
5639 * where g' is the result of plugging in "subs" in each of the integer
5642 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5643 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5649 aff
= isl_aff_cow(aff
);
5651 return isl_aff_free(aff
);
5653 ctx
= isl_aff_get_ctx(aff
);
5654 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5655 isl_die(ctx
, isl_error_invalid
,
5656 "spaces don't match", return isl_aff_free(aff
));
5657 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5659 return isl_aff_free(aff
);
5661 isl_die(ctx
, isl_error_unsupported
,
5662 "cannot handle divs yet", return isl_aff_free(aff
));
5664 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5666 return isl_aff_free(aff
);
5668 aff
->v
= isl_vec_cow(aff
->v
);
5670 return isl_aff_free(aff
);
5672 pos
+= isl_local_space_offset(aff
->ls
, type
);
5675 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5676 aff
->v
->size
, subs
->v
->size
, v
);
5682 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5683 * expressions in "maff".
5685 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5686 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5687 __isl_keep isl_aff
*subs
)
5692 n
= isl_multi_aff_size(maff
);
5694 return isl_multi_aff_free(maff
);
5696 if (type
== isl_dim_in
)
5699 for (i
= 0; i
< n
; ++i
) {
5702 aff
= isl_multi_aff_take_at(maff
, i
);
5703 aff
= isl_aff_substitute(aff
, type
, pos
, subs
);
5704 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
5710 /* Plug in "subs" for input dimension "pos" of "pma".
5712 * pma is of the form
5716 * while subs is of the form
5718 * v' = B_j(v) -> S_j
5720 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5721 * has a contribution in the result, in particular
5723 * C_ij(S_j) -> M_i(S_j)
5725 * Note that plugging in S_j in C_ij may also result in an empty set
5726 * and this contribution should simply be discarded.
5728 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5729 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5730 __isl_keep isl_pw_aff
*subs
)
5733 isl_pw_multi_aff
*res
;
5736 return isl_pw_multi_aff_free(pma
);
5738 n
= pma
->n
* subs
->n
;
5739 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5741 for (i
= 0; i
< pma
->n
; ++i
) {
5742 for (j
= 0; j
< subs
->n
; ++j
) {
5744 isl_multi_aff
*res_ij
;
5747 common
= isl_set_intersect(
5748 isl_set_copy(pma
->p
[i
].set
),
5749 isl_set_copy(subs
->p
[j
].set
));
5750 common
= isl_set_substitute(common
,
5751 pos
, subs
->p
[j
].aff
);
5752 empty
= isl_set_plain_is_empty(common
);
5753 if (empty
< 0 || empty
) {
5754 isl_set_free(common
);
5760 res_ij
= isl_multi_aff_substitute(
5761 isl_multi_aff_copy(pma
->p
[i
].maff
),
5762 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5764 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5768 isl_pw_multi_aff_free(pma
);
5771 isl_pw_multi_aff_free(pma
);
5772 isl_pw_multi_aff_free(res
);
5776 /* Compute the preimage of a range of dimensions in the affine expression "src"
5777 * under "ma" and put the result in "dst". The number of dimensions in "src"
5778 * that precede the range is given by "n_before". The number of dimensions
5779 * in the range is given by the number of output dimensions of "ma".
5780 * The number of dimensions that follow the range is given by "n_after".
5781 * If "has_denom" is set (to one),
5782 * then "src" and "dst" have an extra initial denominator.
5783 * "n_div_ma" is the number of existentials in "ma"
5784 * "n_div_bset" is the number of existentials in "src"
5785 * The resulting "dst" (which is assumed to have been allocated by
5786 * the caller) contains coefficients for both sets of existentials,
5787 * first those in "ma" and then those in "src".
5788 * f, c1, c2 and g are temporary objects that have been initialized
5791 * Let src represent the expression
5793 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5795 * and let ma represent the expressions
5797 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5799 * We start out with the following expression for dst:
5801 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5803 * with the multiplication factor f initially equal to 1
5804 * and f \sum_i b_i v_i kept separately.
5805 * For each x_i that we substitute, we multiply the numerator
5806 * (and denominator) of dst by c_1 = m_i and add the numerator
5807 * of the x_i expression multiplied by c_2 = f b_i,
5808 * after removing the common factors of c_1 and c_2.
5809 * The multiplication factor f also needs to be multiplied by c_1
5810 * for the next x_j, j > i.
5812 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5813 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5814 int n_div_ma
, int n_div_bmap
,
5815 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5818 isl_size n_param
, n_in
, n_out
;
5821 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5822 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5823 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5824 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5825 return isl_stat_error
;
5827 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5828 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5829 isl_seq_clr(dst
+ o_dst
, n_in
);
5832 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5835 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5837 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5839 isl_int_set_si(f
, 1);
5841 for (i
= 0; i
< n_out
; ++i
) {
5842 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5844 if (isl_int_is_zero(src
[offset
]))
5846 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5847 isl_int_mul(c2
, f
, src
[offset
]);
5848 isl_int_gcd(g
, c1
, c2
);
5849 isl_int_divexact(c1
, c1
, g
);
5850 isl_int_divexact(c2
, c2
, g
);
5852 isl_int_mul(f
, f
, c1
);
5855 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5856 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5857 o_dst
+= 1 + n_param
;
5858 o_src
+= 1 + n_param
;
5859 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5861 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5862 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5865 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5867 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5868 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5871 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5873 isl_int_mul(dst
[0], dst
[0], c1
);
5879 /* Compute the pullback of "aff" by the function represented by "ma".
5880 * In other words, plug in "ma" in "aff". The result is an affine expression
5881 * defined over the domain space of "ma".
5883 * If "aff" is represented by
5885 * (a(p) + b x + c(divs))/d
5887 * and ma is represented by
5889 * x = D(p) + F(y) + G(divs')
5891 * then the result is
5893 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5895 * The divs in the local space of the input are similarly adjusted
5896 * through a call to isl_local_space_preimage_multi_aff.
5898 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5899 __isl_take isl_multi_aff
*ma
)
5901 isl_aff
*res
= NULL
;
5902 isl_local_space
*ls
;
5903 isl_size n_div_aff
, n_div_ma
;
5904 isl_int f
, c1
, c2
, g
;
5906 ma
= isl_multi_aff_align_divs(ma
);
5910 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5911 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5912 if (n_div_aff
< 0 || n_div_ma
< 0)
5915 ls
= isl_aff_get_domain_local_space(aff
);
5916 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5917 res
= isl_aff_alloc(ls
);
5926 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5927 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5928 res
= isl_aff_free(res
);
5936 isl_multi_aff_free(ma
);
5937 res
= isl_aff_normalize(res
);
5941 isl_multi_aff_free(ma
);
5946 /* Compute the pullback of "aff1" by the function represented by "aff2".
5947 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5948 * defined over the domain space of "aff1".
5950 * The domain of "aff1" should match the range of "aff2", which means
5951 * that it should be single-dimensional.
5953 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5954 __isl_take isl_aff
*aff2
)
5958 ma
= isl_multi_aff_from_aff(aff2
);
5959 return isl_aff_pullback_multi_aff(aff1
, ma
);
5962 /* Compute the pullback of "ma1" by the function represented by "ma2".
5963 * In other words, plug in "ma2" in "ma1".
5965 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5966 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5970 isl_space
*space
= NULL
;
5972 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5973 ma2
= isl_multi_aff_align_divs(ma2
);
5974 n
= isl_multi_aff_size(ma1
);
5978 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5979 isl_multi_aff_get_space(ma1
));
5981 for (i
= 0; i
< n
; ++i
) {
5984 aff
= isl_multi_aff_take_at(ma1
, i
);
5985 aff
= isl_aff_pullback_multi_aff(aff
, isl_multi_aff_copy(ma2
));
5986 ma1
= isl_multi_aff_restore_at(ma1
, i
, aff
);
5989 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5990 isl_multi_aff_free(ma2
);
5993 isl_space_free(space
);
5994 isl_multi_aff_free(ma2
);
5995 isl_multi_aff_free(ma1
);
5999 /* Extend the local space of "dst" to include the divs
6000 * in the local space of "src".
6002 * If "src" does not have any divs or if the local spaces of "dst" and
6003 * "src" are the same, then no extension is required.
6005 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6006 __isl_keep isl_aff
*src
)
6009 isl_size src_n_div
, dst_n_div
;
6016 return isl_aff_free(dst
);
6018 ctx
= isl_aff_get_ctx(src
);
6019 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6021 return isl_aff_free(dst
);
6023 isl_die(ctx
, isl_error_invalid
,
6024 "spaces don't match", goto error
);
6026 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6027 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6030 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6031 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6032 return isl_aff_free(dst
);
6036 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6037 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6038 if (!exp1
|| (dst_n_div
&& !exp2
))
6041 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6042 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6050 return isl_aff_free(dst
);
6053 /* Adjust the local spaces of the affine expressions in "maff"
6054 * such that they all have the save divs.
6056 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6057 __isl_take isl_multi_aff
*maff
)
6063 n
= isl_multi_aff_size(maff
);
6065 return isl_multi_aff_free(maff
);
6069 aff_0
= isl_multi_aff_take_at(maff
, 0);
6070 for (i
= 1; i
< n
; ++i
) {
6073 aff_i
= isl_multi_aff_peek_at(maff
, i
);
6074 aff_0
= isl_aff_align_divs(aff_0
, aff_i
);
6076 maff
= isl_multi_aff_restore_at(maff
, 0, aff_0
);
6078 aff_0
= isl_multi_aff_peek_at(maff
, 0);
6079 for (i
= 1; i
< n
; ++i
) {
6082 aff_i
= isl_multi_aff_take_at(maff
, i
);
6083 aff_i
= isl_aff_align_divs(aff_i
, aff_0
);
6084 maff
= isl_multi_aff_restore_at(maff
, i
, aff_i
);
6090 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6092 aff
= isl_aff_cow(aff
);
6096 aff
->ls
= isl_local_space_lift(aff
->ls
);
6098 return isl_aff_free(aff
);
6103 /* Lift "maff" to a space with extra dimensions such that the result
6104 * has no more existentially quantified variables.
6105 * If "ls" is not NULL, then *ls is assigned the local space that lies
6106 * at the basis of the lifting applied to "maff".
6108 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6109 __isl_give isl_local_space
**ls
)
6119 n
= isl_multi_aff_size(maff
);
6121 return isl_multi_aff_free(maff
);
6125 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6126 *ls
= isl_local_space_from_space(space
);
6128 return isl_multi_aff_free(maff
);
6133 maff
= isl_multi_aff_align_divs(maff
);
6135 aff
= isl_multi_aff_peek_at(maff
, 0);
6136 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6138 return isl_multi_aff_free(maff
);
6139 space
= isl_multi_aff_get_space(maff
);
6140 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6141 space
= isl_space_extend_domain_with_range(space
,
6142 isl_multi_aff_get_space(maff
));
6143 maff
= isl_multi_aff_restore_space(maff
, space
);
6146 aff
= isl_multi_aff_peek_at(maff
, 0);
6147 *ls
= isl_aff_get_domain_local_space(aff
);
6149 return isl_multi_aff_free(maff
);
6152 for (i
= 0; i
< n
; ++i
) {
6153 aff
= isl_multi_aff_take_at(maff
, i
);
6154 aff
= isl_aff_lift(aff
);
6155 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
6162 #define TYPE isl_pw_multi_aff
6164 #include "check_type_range_templ.c"
6166 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6168 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6169 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6176 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6179 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6183 space
= isl_pw_multi_aff_get_space(pma
);
6184 space
= isl_space_drop_dims(space
, isl_dim_out
,
6185 pos
+ 1, n_out
- pos
- 1);
6186 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6188 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6189 for (i
= 0; i
< pma
->n
; ++i
) {
6191 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6192 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6198 /* This is an alternative name for the function above.
6200 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6201 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6203 return isl_pw_multi_aff_get_at(pma
, pos
);
6206 /* Return an isl_pw_multi_aff with the given "set" as domain and
6207 * an unnamed zero-dimensional range.
6209 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6210 __isl_take isl_set
*set
)
6215 space
= isl_set_get_space(set
);
6216 space
= isl_space_from_domain(space
);
6217 ma
= isl_multi_aff_zero(space
);
6218 return isl_pw_multi_aff_alloc(set
, ma
);
6221 /* Add an isl_pw_multi_aff with the given "set" as domain and
6222 * an unnamed zero-dimensional range to *user.
6224 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6227 isl_union_pw_multi_aff
**upma
= user
;
6228 isl_pw_multi_aff
*pma
;
6230 pma
= isl_pw_multi_aff_from_domain(set
);
6231 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6236 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6237 * an unnamed zero-dimensional range.
6239 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6240 __isl_take isl_union_set
*uset
)
6243 isl_union_pw_multi_aff
*upma
;
6248 space
= isl_union_set_get_space(uset
);
6249 upma
= isl_union_pw_multi_aff_empty(space
);
6251 if (isl_union_set_foreach_set(uset
,
6252 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6255 isl_union_set_free(uset
);
6258 isl_union_set_free(uset
);
6259 isl_union_pw_multi_aff_free(upma
);
6263 /* Local data for bin_entry and the callback "fn".
6265 struct isl_union_pw_multi_aff_bin_data
{
6266 isl_union_pw_multi_aff
*upma2
;
6267 isl_union_pw_multi_aff
*res
;
6268 isl_pw_multi_aff
*pma
;
6269 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6272 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6273 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6275 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6277 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6281 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6283 isl_pw_multi_aff_free(pma
);
6288 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6289 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6290 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6291 * as *entry. The callback should adjust data->res if desired.
6293 static __isl_give isl_union_pw_multi_aff
*bin_op(
6294 __isl_take isl_union_pw_multi_aff
*upma1
,
6295 __isl_take isl_union_pw_multi_aff
*upma2
,
6296 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6299 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6301 space
= isl_union_pw_multi_aff_get_space(upma2
);
6302 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6303 space
= isl_union_pw_multi_aff_get_space(upma1
);
6304 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6306 if (!upma1
|| !upma2
)
6310 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6311 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6312 &bin_entry
, &data
) < 0)
6315 isl_union_pw_multi_aff_free(upma1
);
6316 isl_union_pw_multi_aff_free(upma2
);
6319 isl_union_pw_multi_aff_free(upma1
);
6320 isl_union_pw_multi_aff_free(upma2
);
6321 isl_union_pw_multi_aff_free(data
.res
);
6325 /* Given two isl_pw_multi_affs A -> B and C -> D,
6326 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6328 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6329 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6333 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6334 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6335 isl_pw_multi_aff_get_space(pma2
));
6336 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6337 &isl_multi_aff_range_product
);
6340 /* Given two isl_pw_multi_affs A -> B and C -> D,
6341 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6343 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6344 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6348 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6349 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6350 isl_pw_multi_aff_get_space(pma2
));
6351 space
= isl_space_flatten_range(space
);
6352 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6353 &isl_multi_aff_flat_range_product
);
6356 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6357 * to compute some form of range product and add the result to data->res.
6359 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6360 __isl_give isl_pw_multi_aff
*(*range_product
)(
6361 __isl_take isl_pw_multi_aff
*pma1
,
6362 __isl_take isl_pw_multi_aff
*pma2
),
6365 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6367 isl_space
*space1
, *space2
;
6369 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6370 space2
= isl_pw_multi_aff_peek_space(pma2
);
6371 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6372 space2
, isl_dim_in
);
6373 if (match
< 0 || !match
) {
6374 isl_pw_multi_aff_free(pma2
);
6375 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6378 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6380 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6385 /* If data->pma and "pma2" have the same domain space, then compute
6386 * their flat range product and add the result to data->res.
6388 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6391 return gen_range_product_entry(pma2
,
6392 &isl_pw_multi_aff_flat_range_product
, user
);
6395 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6396 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6398 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6399 __isl_take isl_union_pw_multi_aff
*upma1
,
6400 __isl_take isl_union_pw_multi_aff
*upma2
)
6402 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6405 /* If data->pma and "pma2" have the same domain space, then compute
6406 * their range product and add the result to data->res.
6408 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6411 return gen_range_product_entry(pma2
,
6412 &isl_pw_multi_aff_range_product
, user
);
6415 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6416 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6418 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6419 __isl_take isl_union_pw_multi_aff
*upma1
,
6420 __isl_take isl_union_pw_multi_aff
*upma2
)
6422 return bin_op(upma1
, upma2
, &range_product_entry
);
6425 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6426 * The parameters are assumed to have been aligned.
6428 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6429 * except that it works on two different isl_pw_* types.
6431 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6432 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6433 __isl_take isl_pw_aff
*pa
)
6436 isl_pw_multi_aff
*res
= NULL
;
6441 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6442 pa
->dim
, isl_dim_in
))
6443 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6444 "domains don't match", goto error
);
6445 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6449 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6451 for (i
= 0; i
< pma
->n
; ++i
) {
6452 for (j
= 0; j
< pa
->n
; ++j
) {
6454 isl_multi_aff
*res_ij
;
6457 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6458 isl_set_copy(pa
->p
[j
].set
));
6459 empty
= isl_set_plain_is_empty(common
);
6460 if (empty
< 0 || empty
) {
6461 isl_set_free(common
);
6467 res_ij
= isl_multi_aff_set_aff(
6468 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6469 isl_aff_copy(pa
->p
[j
].aff
));
6470 res_ij
= isl_multi_aff_gist(res_ij
,
6471 isl_set_copy(common
));
6473 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6477 isl_pw_multi_aff_free(pma
);
6478 isl_pw_aff_free(pa
);
6481 isl_pw_multi_aff_free(pma
);
6482 isl_pw_aff_free(pa
);
6483 return isl_pw_multi_aff_free(res
);
6486 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6488 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6489 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6490 __isl_take isl_pw_aff
*pa
)
6492 isl_bool equal_params
;
6496 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6497 if (equal_params
< 0)
6500 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6501 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6502 isl_pw_aff_check_named_params(pa
) < 0)
6504 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6505 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6506 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6508 isl_pw_multi_aff_free(pma
);
6509 isl_pw_aff_free(pa
);
6513 /* Do the parameters of "pa" match those of "space"?
6515 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6516 __isl_keep isl_space
*space
)
6518 isl_space
*pa_space
;
6522 return isl_bool_error
;
6524 pa_space
= isl_pw_aff_get_space(pa
);
6526 match
= isl_space_has_equal_params(space
, pa_space
);
6528 isl_space_free(pa_space
);
6532 /* Check that the domain space of "pa" matches "space".
6534 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6535 __isl_keep isl_space
*space
)
6537 isl_space
*pa_space
;
6541 return isl_stat_error
;
6543 pa_space
= isl_pw_aff_get_space(pa
);
6545 match
= isl_space_has_equal_params(space
, pa_space
);
6549 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6550 "parameters don't match", goto error
);
6551 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6552 pa_space
, isl_dim_in
);
6556 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6557 "domains don't match", goto error
);
6558 isl_space_free(pa_space
);
6561 isl_space_free(pa_space
);
6562 return isl_stat_error
;
6570 #include <isl_multi_explicit_domain.c>
6571 #include <isl_multi_pw_aff_explicit_domain.c>
6572 #include <isl_multi_templ.c>
6573 #include <isl_multi_un_op_templ.c>
6574 #include <isl_multi_bin_val_templ.c>
6575 #include <isl_multi_add_constant_templ.c>
6576 #include <isl_multi_apply_set.c>
6577 #include <isl_multi_arith_templ.c>
6578 #include <isl_multi_bind_templ.c>
6579 #include <isl_multi_bind_domain_templ.c>
6580 #include <isl_multi_coalesce.c>
6581 #include <isl_multi_domain_templ.c>
6582 #include <isl_multi_dim_id_templ.c>
6583 #include <isl_multi_dims.c>
6584 #include <isl_multi_from_base_templ.c>
6585 #include <isl_multi_gist.c>
6586 #include <isl_multi_hash.c>
6587 #include <isl_multi_identity_templ.c>
6588 #include <isl_multi_align_set.c>
6589 #include <isl_multi_insert_domain_templ.c>
6590 #include <isl_multi_intersect.c>
6591 #include <isl_multi_min_max_templ.c>
6592 #include <isl_multi_move_dims_templ.c>
6593 #include <isl_multi_nan_templ.c>
6594 #include <isl_multi_param_templ.c>
6595 #include <isl_multi_product_templ.c>
6596 #include <isl_multi_splice_templ.c>
6597 #include <isl_multi_tuple_id_templ.c>
6598 #include <isl_multi_union_add_templ.c>
6599 #include <isl_multi_zero_templ.c>
6600 #include <isl_multi_unbind_params_templ.c>
6602 /* Is every element of "mpa" defined over a single universe domain?
6604 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6606 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6609 /* Given that every element of "mpa" is defined over a single universe domain,
6610 * return the corresponding base expressions.
6612 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6613 __isl_take isl_multi_pw_aff
*mpa
)
6619 n
= isl_multi_pw_aff_size(mpa
);
6621 mpa
= isl_multi_pw_aff_free(mpa
);
6622 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6623 for (i
= 0; i
< n
; ++i
) {
6626 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6627 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6629 isl_multi_pw_aff_free(mpa
);
6633 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6634 * with this explicit domain.
6636 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6637 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6641 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6644 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6645 map
= isl_map_intersect_domain(map
, dom
);
6650 /* Are all elements of "mpa" piecewise constants?
6652 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6654 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6657 /* Does "mpa" have a non-trivial explicit domain?
6659 * The explicit domain, if present, is trivial if it represents
6660 * an (obviously) universe set.
6662 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6663 __isl_keep isl_multi_pw_aff
*mpa
)
6666 return isl_bool_error
;
6667 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6668 return isl_bool_false
;
6669 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6675 #include "isl_opt_mpa_templ.c"
6677 /* Compute the minima of the set dimensions as a function of the
6678 * parameters, but independently of the other set dimensions.
6680 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6682 return set_opt_mpa(set
, &isl_set_dim_min
);
6685 /* Compute the maxima of the set dimensions as a function of the
6686 * parameters, but independently of the other set dimensions.
6688 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6690 return set_opt_mpa(set
, &isl_set_dim_max
);
6696 #include "isl_opt_mpa_templ.c"
6698 /* Compute the minima of the output dimensions as a function of the
6699 * parameters and input dimensions, but independently of
6700 * the other output dimensions.
6702 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6704 return map_opt_mpa(map
, &isl_map_dim_min
);
6707 /* Compute the maxima of the output dimensions as a function of the
6708 * parameters and input dimensions, but independently of
6709 * the other output dimensions.
6711 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6713 return map_opt_mpa(map
, &isl_map_dim_max
);
6717 #define TYPE isl_pw_multi_aff
6718 #include "isl_type_check_match_range_multi_val.c"
6720 /* Apply "fn" to the base expressions of "pma" and "mv".
6722 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6723 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6724 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6725 __isl_take isl_multi_val
*mv
))
6730 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6733 n
= isl_pw_multi_aff_n_piece(pma
);
6737 for (i
= 0; i
< n
; ++i
) {
6740 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6741 ma
= fn(ma
, isl_multi_val_copy(mv
));
6742 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6745 isl_multi_val_free(mv
);
6748 isl_multi_val_free(mv
);
6749 isl_pw_multi_aff_free(pma
);
6753 /* Scale the elements of "pma" by the corresponding elements of "mv".
6755 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6756 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6758 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6759 &isl_multi_aff_scale_multi_val
);
6762 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6764 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
6765 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6767 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6768 &isl_multi_aff_scale_down_multi_val
);
6771 /* This function is called for each entry of an isl_union_pw_multi_aff.
6772 * If the space of the entry matches that of data->mv,
6773 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6774 * Otherwise, return an empty isl_pw_multi_aff.
6776 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6777 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6780 isl_multi_val
*mv
= user
;
6782 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
6784 return isl_pw_multi_aff_free(pma
);
6786 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6787 isl_pw_multi_aff_free(pma
);
6788 return isl_pw_multi_aff_empty(space
);
6791 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6794 /* Scale the elements of "upma" by the corresponding elements of "mv",
6795 * for those entries that match the space of "mv".
6797 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6798 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6800 struct isl_union_pw_multi_aff_transform_control control
= {
6801 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6805 upma
= isl_union_pw_multi_aff_align_params(upma
,
6806 isl_multi_val_get_space(mv
));
6807 mv
= isl_multi_val_align_params(mv
,
6808 isl_union_pw_multi_aff_get_space(upma
));
6812 return isl_union_pw_multi_aff_transform(upma
, &control
);
6814 isl_multi_val_free(mv
);
6817 isl_multi_val_free(mv
);
6818 isl_union_pw_multi_aff_free(upma
);
6822 /* Construct and return a piecewise multi affine expression
6823 * in the given space with value zero in each of the output dimensions and
6824 * a universe domain.
6826 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6828 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6831 /* Construct and return a piecewise multi affine expression
6832 * that is equal to the given piecewise affine expression.
6834 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6835 __isl_take isl_pw_aff
*pa
)
6839 isl_pw_multi_aff
*pma
;
6844 space
= isl_pw_aff_get_space(pa
);
6845 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6847 for (i
= 0; i
< pa
->n
; ++i
) {
6851 set
= isl_set_copy(pa
->p
[i
].set
);
6852 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6853 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6856 isl_pw_aff_free(pa
);
6860 /* Construct and return a piecewise multi affine expression
6861 * that is equal to the given multi piecewise affine expression
6862 * on the shared domain of the piecewise affine expressions,
6863 * in the special case of a 0D multi piecewise affine expression.
6865 * Create a piecewise multi affine expression with the explicit domain of
6866 * the 0D multi piecewise affine expression as domain.
6868 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
6869 __isl_take isl_multi_pw_aff
*mpa
)
6875 space
= isl_multi_pw_aff_get_space(mpa
);
6876 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
6877 isl_multi_pw_aff_free(mpa
);
6879 ma
= isl_multi_aff_zero(space
);
6880 return isl_pw_multi_aff_alloc(dom
, ma
);
6883 /* Construct and return a piecewise multi affine expression
6884 * that is equal to the given multi piecewise affine expression
6885 * on the shared domain of the piecewise affine expressions.
6887 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6888 __isl_take isl_multi_pw_aff
*mpa
)
6893 isl_pw_multi_aff
*pma
;
6899 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
6901 space
= isl_multi_pw_aff_get_space(mpa
);
6902 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6903 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6905 for (i
= 1; i
< mpa
->n
; ++i
) {
6906 isl_pw_multi_aff
*pma_i
;
6908 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6909 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6910 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6913 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6915 isl_multi_pw_aff_free(mpa
);
6919 /* Convenience function that constructs an isl_multi_pw_aff
6920 * directly from an isl_aff.
6922 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
6924 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
6927 /* Construct and return a multi piecewise affine expression
6928 * that is equal to the given multi affine expression.
6930 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6931 __isl_take isl_multi_aff
*ma
)
6935 isl_multi_pw_aff
*mpa
;
6937 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6939 ma
= isl_multi_aff_free(ma
);
6943 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6945 for (i
= 0; i
< n
; ++i
) {
6948 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6949 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6952 isl_multi_aff_free(ma
);
6956 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6957 * but is considered as a function on an isl_multi_aff when exported.
6959 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
6960 __isl_take isl_multi_aff
*ma
)
6962 return isl_multi_pw_aff_from_multi_aff(ma
);
6965 /* Construct and return a multi piecewise affine expression
6966 * that is equal to the given piecewise multi affine expression.
6968 * If the resulting multi piecewise affine expression has
6969 * an explicit domain, then assign it the domain of the input.
6970 * In other cases, the domain is stored in the individual elements.
6972 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6973 __isl_take isl_pw_multi_aff
*pma
)
6978 isl_multi_pw_aff
*mpa
;
6980 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6982 pma
= isl_pw_multi_aff_free(pma
);
6983 space
= isl_pw_multi_aff_get_space(pma
);
6984 mpa
= isl_multi_pw_aff_alloc(space
);
6986 for (i
= 0; i
< n
; ++i
) {
6989 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6990 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6992 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
6995 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
6996 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
6999 isl_pw_multi_aff_free(pma
);
7003 /* This function performs the same operation as
7004 * isl_multi_pw_aff_from_pw_multi_aff,
7005 * but is considered as a function on an isl_pw_multi_aff when exported.
7007 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
7008 __isl_take isl_pw_multi_aff
*pma
)
7010 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7013 /* Do "pa1" and "pa2" represent the same function?
7015 * We first check if they are obviously equal.
7016 * If not, we convert them to maps and check if those are equal.
7018 * If "pa1" or "pa2" contain any NaNs, then they are considered
7019 * not to be the same. A NaN is not equal to anything, not even
7022 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7023 __isl_keep isl_pw_aff
*pa2
)
7027 isl_map
*map1
, *map2
;
7030 return isl_bool_error
;
7032 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7033 if (equal
< 0 || equal
)
7035 has_nan
= either_involves_nan(pa1
, pa2
);
7037 return isl_bool_error
;
7039 return isl_bool_false
;
7041 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7042 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7043 equal
= isl_map_is_equal(map1
, map2
);
7050 /* Do "mpa1" and "mpa2" represent the same function?
7052 * Note that we cannot convert the entire isl_multi_pw_aff
7053 * to a map because the domains of the piecewise affine expressions
7054 * may not be the same.
7056 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7057 __isl_keep isl_multi_pw_aff
*mpa2
)
7060 isl_bool equal
, equal_params
;
7063 return isl_bool_error
;
7065 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7066 if (equal_params
< 0)
7067 return isl_bool_error
;
7068 if (!equal_params
) {
7069 if (!isl_space_has_named_params(mpa1
->space
))
7070 return isl_bool_false
;
7071 if (!isl_space_has_named_params(mpa2
->space
))
7072 return isl_bool_false
;
7073 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7074 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7075 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7076 isl_multi_pw_aff_get_space(mpa2
));
7077 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7078 isl_multi_pw_aff_get_space(mpa1
));
7079 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7080 isl_multi_pw_aff_free(mpa1
);
7081 isl_multi_pw_aff_free(mpa2
);
7085 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7086 if (equal
< 0 || !equal
)
7089 for (i
= 0; i
< mpa1
->n
; ++i
) {
7090 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7091 if (equal
< 0 || !equal
)
7095 return isl_bool_true
;
7098 /* Do "pma1" and "pma2" represent the same function?
7100 * First check if they are obviously equal.
7101 * If not, then convert them to maps and check if those are equal.
7103 * If "pa1" or "pa2" contain any NaNs, then they are considered
7104 * not to be the same. A NaN is not equal to anything, not even
7107 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7108 __isl_keep isl_pw_multi_aff
*pma2
)
7112 isl_map
*map1
, *map2
;
7115 return isl_bool_error
;
7117 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7118 if (equal
< 0 || equal
)
7120 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7121 if (has_nan
>= 0 && !has_nan
)
7122 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7123 if (has_nan
< 0 || has_nan
)
7124 return isl_bool_not(has_nan
);
7126 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7127 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7128 equal
= isl_map_is_equal(map1
, map2
);
7136 #define BASE multi_aff
7138 #include "isl_multi_pw_aff_pullback_templ.c"
7141 #define BASE pw_multi_aff
7143 #include "isl_multi_pw_aff_pullback_templ.c"
7145 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7146 * with the domain of "aff". The domain of the result is the same
7148 * "mpa" and "aff" are assumed to have been aligned.
7150 * We first extract the parametric constant from "aff", defined
7151 * over the correct domain.
7152 * Then we add the appropriate combinations of the members of "mpa".
7153 * Finally, we add the integer divisions through recursive calls.
7155 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7156 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7159 isl_size n_in
, n_div
, n_mpa_in
;
7165 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7166 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7167 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7168 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7171 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7172 tmp
= isl_aff_copy(aff
);
7173 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7174 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7175 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7176 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7177 pa
= isl_pw_aff_from_aff(tmp
);
7179 for (i
= 0; i
< n_in
; ++i
) {
7182 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7184 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7185 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7186 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7187 pa
= isl_pw_aff_add(pa
, pa_i
);
7190 for (i
= 0; i
< n_div
; ++i
) {
7194 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7196 div
= isl_aff_get_div(aff
, i
);
7197 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7198 isl_multi_pw_aff_copy(mpa
), div
);
7199 pa_i
= isl_pw_aff_floor(pa_i
);
7200 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7201 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7202 pa
= isl_pw_aff_add(pa
, pa_i
);
7205 isl_multi_pw_aff_free(mpa
);
7210 isl_multi_pw_aff_free(mpa
);
7215 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7216 * with the domain of "aff". The domain of the result is the same
7219 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7220 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7222 isl_bool equal_params
;
7226 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7227 if (equal_params
< 0)
7230 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7232 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7233 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7235 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7238 isl_multi_pw_aff_free(mpa
);
7242 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7243 * with the domain of "pa". The domain of the result is the same
7245 * "mpa" and "pa" are assumed to have been aligned.
7247 * We consider each piece in turn. Note that the domains of the
7248 * pieces are assumed to be disjoint and they remain disjoint
7249 * after taking the preimage (over the same function).
7251 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7252 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7261 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7262 isl_pw_aff_get_space(pa
));
7263 res
= isl_pw_aff_empty(space
);
7265 for (i
= 0; i
< pa
->n
; ++i
) {
7269 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7270 isl_multi_pw_aff_copy(mpa
),
7271 isl_aff_copy(pa
->p
[i
].aff
));
7272 domain
= isl_set_copy(pa
->p
[i
].set
);
7273 domain
= isl_set_preimage_multi_pw_aff(domain
,
7274 isl_multi_pw_aff_copy(mpa
));
7275 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7276 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7279 isl_pw_aff_free(pa
);
7280 isl_multi_pw_aff_free(mpa
);
7283 isl_pw_aff_free(pa
);
7284 isl_multi_pw_aff_free(mpa
);
7288 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7289 * with the domain of "pa". The domain of the result is the same
7292 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7293 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7295 isl_bool equal_params
;
7299 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7300 if (equal_params
< 0)
7303 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7305 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7306 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7308 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7310 isl_pw_aff_free(pa
);
7311 isl_multi_pw_aff_free(mpa
);
7315 /* Compute the pullback of "pa" by the function represented by "mpa".
7316 * In other words, plug in "mpa" in "pa".
7318 * The pullback is computed by applying "pa" to "mpa".
7320 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7321 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7323 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7327 #define BASE multi_pw_aff
7329 #include "isl_multi_pw_aff_pullback_templ.c"
7331 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7332 * of "mpa1" and "mpa2" live in the same space, construct map space
7333 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7334 * with this map space as extract argument.
7336 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7337 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7338 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7339 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7342 isl_space
*space1
, *space2
;
7345 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7346 isl_multi_pw_aff_get_space(mpa2
));
7347 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7348 isl_multi_pw_aff_get_space(mpa1
));
7351 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7352 mpa2
->space
, isl_dim_out
);
7356 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7357 "range spaces don't match", goto error
);
7358 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7359 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7360 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7362 res
= order(mpa1
, mpa2
, space1
);
7363 isl_multi_pw_aff_free(mpa1
);
7364 isl_multi_pw_aff_free(mpa2
);
7367 isl_multi_pw_aff_free(mpa1
);
7368 isl_multi_pw_aff_free(mpa2
);
7372 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7373 * where the function values are equal. "space" is the space of the result.
7374 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7376 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7377 * in the sequences are equal.
7379 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7380 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7381 __isl_take isl_space
*space
)
7387 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7389 space
= isl_space_free(space
);
7390 res
= isl_map_universe(space
);
7392 for (i
= 0; i
< n
; ++i
) {
7393 isl_pw_aff
*pa1
, *pa2
;
7396 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7397 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7398 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7399 res
= isl_map_intersect(res
, map
);
7405 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7406 * where the function values are equal.
7408 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7409 __isl_take isl_multi_pw_aff
*mpa2
)
7411 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7412 &isl_multi_pw_aff_eq_map_on_space
);
7415 /* Intersect "map" with the result of applying "order"
7416 * on two copies of "mpa".
7418 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7419 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7420 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7421 __isl_take isl_multi_pw_aff
*mpa2
))
7423 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7426 /* Return the subset of "map" where the domain and the range
7427 * have equal "mpa" values.
7429 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7430 __isl_take isl_multi_pw_aff
*mpa
)
7432 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7433 &isl_multi_pw_aff_eq_map
);
7436 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7437 * where the function values of "mpa1" lexicographically satisfies
7438 * "strict_base"/"base" compared to that of "mpa2".
7439 * "space" is the space of the result.
7440 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7442 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7443 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7444 * when compared to the i-th element of "mpa2" while all previous elements are
7446 * In particular, if i corresponds to the final elements
7447 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7448 * for other values of i.
7449 * If "base" is a strict order, then "base" and "strict_base" are the same.
7451 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7452 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7453 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7454 __isl_take isl_pw_aff
*pa2
),
7455 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7456 __isl_take isl_pw_aff
*pa2
),
7457 __isl_take isl_space
*space
)
7461 isl_map
*res
, *rest
;
7463 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7465 space
= isl_space_free(space
);
7466 res
= isl_map_empty(isl_space_copy(space
));
7467 rest
= isl_map_universe(space
);
7469 for (i
= 0; i
< n
; ++i
) {
7471 isl_pw_aff
*pa1
, *pa2
;
7476 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7477 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7478 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7479 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7480 res
= isl_map_union(res
, map
);
7485 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7486 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7487 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7488 rest
= isl_map_intersect(rest
, map
);
7498 #define STRICT_ORDER lt
7499 #include "isl_aff_lex_templ.c"
7504 #define STRICT_ORDER lt
7505 #include "isl_aff_lex_templ.c"
7510 #define STRICT_ORDER gt
7511 #include "isl_aff_lex_templ.c"
7516 #define STRICT_ORDER gt
7517 #include "isl_aff_lex_templ.c"
7519 /* Compare two isl_affs.
7521 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7522 * than "aff2" and 0 if they are equal.
7524 * The order is fairly arbitrary. We do consider expressions that only involve
7525 * earlier dimensions as "smaller".
7527 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7540 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7544 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7545 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7547 return last1
- last2
;
7549 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7552 /* Compare two isl_pw_affs.
7554 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7555 * than "pa2" and 0 if they are equal.
7557 * The order is fairly arbitrary. We do consider expressions that only involve
7558 * earlier dimensions as "smaller".
7560 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7561 __isl_keep isl_pw_aff
*pa2
)
7574 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7578 if (pa1
->n
!= pa2
->n
)
7579 return pa1
->n
- pa2
->n
;
7581 for (i
= 0; i
< pa1
->n
; ++i
) {
7582 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7585 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7593 /* Return a piecewise affine expression that is equal to "v" on "domain".
7595 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7596 __isl_take isl_val
*v
)
7599 isl_local_space
*ls
;
7602 space
= isl_set_get_space(domain
);
7603 ls
= isl_local_space_from_space(space
);
7604 aff
= isl_aff_val_on_domain(ls
, v
);
7606 return isl_pw_aff_alloc(domain
, aff
);
7609 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7610 * but is considered as a function on an isl_set when exported.
7612 __isl_give isl_pw_aff
*isl_set_pw_aff_on_domain_val(__isl_take isl_set
*domain
,
7613 __isl_take isl_val
*v
)
7615 return isl_pw_aff_val_on_domain(domain
, v
);
7618 /* Return a piecewise affine expression that is equal to the parameter
7619 * with identifier "id" on "domain".
7621 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7622 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7627 space
= isl_set_get_space(domain
);
7628 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7629 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7630 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7632 return isl_pw_aff_alloc(domain
, aff
);
7635 /* This function performs the same operation as
7636 * isl_pw_aff_param_on_domain_id,
7637 * but is considered as a function on an isl_set when exported.
7639 __isl_give isl_pw_aff
*isl_set_param_pw_aff_on_domain_id(
7640 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7642 return isl_pw_aff_param_on_domain_id(domain
, id
);
7645 /* Return a multi affine expression that is equal to "mv" on domain
7648 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7649 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7654 isl_local_space
*ls
;
7657 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7658 if (!space
|| n
< 0)
7661 space2
= isl_multi_val_get_space(mv
);
7662 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7663 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7664 space
= isl_space_map_from_domain_and_range(space
, space2
);
7665 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7666 ls
= isl_local_space_from_space(isl_space_domain(space
));
7667 for (i
= 0; i
< n
; ++i
) {
7671 v
= isl_multi_val_get_val(mv
, i
);
7672 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7673 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7675 isl_local_space_free(ls
);
7677 isl_multi_val_free(mv
);
7680 isl_space_free(space
);
7681 isl_multi_val_free(mv
);
7685 /* This is an alternative name for the function above.
7687 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7688 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7690 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7693 /* This function performs the same operation as
7694 * isl_multi_aff_multi_val_on_domain_space,
7695 * but is considered as a function on an isl_space when exported.
7697 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7698 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7700 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7703 /* Return a piecewise multi-affine expression
7704 * that is equal to "mv" on "domain".
7706 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7707 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7712 space
= isl_set_get_space(domain
);
7713 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7715 return isl_pw_multi_aff_alloc(domain
, ma
);
7718 /* This function performs the same operation as
7719 * isl_pw_multi_aff_multi_val_on_domain,
7720 * but is considered as a function on an isl_set when exported.
7722 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7723 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7725 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7728 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7729 * mv is the value that should be attained on each domain set
7730 * res collects the results
7732 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7734 isl_union_pw_multi_aff
*res
;
7737 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7738 * and add it to data->res.
7740 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7743 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7744 isl_pw_multi_aff
*pma
;
7747 mv
= isl_multi_val_copy(data
->mv
);
7748 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7749 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7751 return data
->res
? isl_stat_ok
: isl_stat_error
;
7754 /* Return a union piecewise multi-affine expression
7755 * that is equal to "mv" on "domain".
7757 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7758 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7760 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7763 space
= isl_union_set_get_space(domain
);
7764 data
.res
= isl_union_pw_multi_aff_empty(space
);
7766 if (isl_union_set_foreach_set(domain
,
7767 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7768 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7769 isl_union_set_free(domain
);
7770 isl_multi_val_free(mv
);
7774 /* Compute the pullback of data->pma by the function represented by "pma2",
7775 * provided the spaces match, and add the results to data->res.
7777 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7779 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7781 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7782 pma2
->dim
, isl_dim_out
)) {
7783 isl_pw_multi_aff_free(pma2
);
7787 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7788 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7790 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7792 return isl_stat_error
;
7797 /* Compute the pullback of "upma1" by the function represented by "upma2".
7799 __isl_give isl_union_pw_multi_aff
*
7800 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7801 __isl_take isl_union_pw_multi_aff
*upma1
,
7802 __isl_take isl_union_pw_multi_aff
*upma2
)
7804 return bin_op(upma1
, upma2
, &pullback_entry
);
7807 /* Apply "upma2" to "upma1".
7809 * That is, compute the pullback of "upma2" by "upma1".
7811 __isl_give isl_union_pw_multi_aff
*
7812 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7813 __isl_take isl_union_pw_multi_aff
*upma1
,
7814 __isl_take isl_union_pw_multi_aff
*upma2
)
7816 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7820 #define TYPE isl_pw_multi_aff
7822 #include "isl_copy_tuple_id_templ.c"
7824 /* Given a function "pma1" of the form A[B -> C] -> D and
7825 * a function "pma2" of the form E -> B,
7826 * replace the domain of the wrapped relation inside the domain of "pma1"
7827 * by the preimage with respect to "pma2".
7828 * In other words, plug in "pma2" in this nested domain.
7829 * The result is of the form A[E -> C] -> D.
7831 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7832 * plug that into "pma1".
7834 __isl_give isl_pw_multi_aff
*
7835 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7836 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
7838 isl_space
*pma1_space
, *pma2_space
;
7840 isl_pw_multi_aff
*id
;
7842 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
7843 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7845 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
7847 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
7848 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
7851 space
= isl_space_domain(isl_space_copy(pma1_space
));
7852 space
= isl_space_range(isl_space_unwrap(space
));
7853 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
7854 pma2
= isl_pw_multi_aff_product(pma2
, id
);
7856 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
7857 pma1_space
, isl_dim_in
);
7858 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
7859 pma1_space
, isl_dim_in
);
7861 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
7863 isl_pw_multi_aff_free(pma1
);
7864 isl_pw_multi_aff_free(pma2
);
7868 /* If data->pma and "pma2" are such that
7869 * data->pma is of the form A[B -> C] -> D and
7870 * "pma2" is of the form E -> B,
7871 * then replace the domain of the wrapped relation
7872 * inside the domain of data->pma by the preimage with respect to "pma2" and
7873 * add the result to data->res.
7875 static isl_stat
preimage_domain_wrapped_domain_entry(
7876 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
7878 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7879 isl_space
*pma1_space
, *pma2_space
;
7882 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
7883 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7885 match
= isl_space_domain_is_wrapping(pma1_space
);
7886 if (match
>= 0 && match
)
7887 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
7888 isl_dim_in
, pma2_space
, isl_dim_out
);
7889 if (match
< 0 || !match
) {
7890 isl_pw_multi_aff_free(pma2
);
7891 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
7894 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7895 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7897 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7899 return isl_stat_non_null(data
->res
);
7902 /* For each pair of functions A[B -> C] -> D in "upma1" and
7903 * E -> B in "upma2",
7904 * replace the domain of the wrapped relation inside the domain of the first
7905 * by the preimage with respect to the second and collect the results.
7906 * In other words, plug in the second function in this nested domain.
7907 * The results are of the form A[E -> C] -> D.
7909 __isl_give isl_union_pw_multi_aff
*
7910 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7911 __isl_take isl_union_pw_multi_aff
*upma1
,
7912 __isl_take isl_union_pw_multi_aff
*upma2
)
7914 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
7917 /* Check that the domain space of "upa" matches "space".
7919 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7920 * can in principle never fail since the space "space" is that
7921 * of the isl_multi_union_pw_aff and is a set space such that
7922 * there is no domain space to match.
7924 * We check the parameters and double-check that "space" is
7925 * indeed that of a set.
7927 static isl_stat
isl_union_pw_aff_check_match_domain_space(
7928 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7930 isl_space
*upa_space
;
7934 return isl_stat_error
;
7936 match
= isl_space_is_set(space
);
7938 return isl_stat_error
;
7940 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7941 "expecting set space", return isl_stat_error
);
7943 upa_space
= isl_union_pw_aff_get_space(upa
);
7944 match
= isl_space_has_equal_params(space
, upa_space
);
7948 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7949 "parameters don't match", goto error
);
7951 isl_space_free(upa_space
);
7954 isl_space_free(upa_space
);
7955 return isl_stat_error
;
7958 /* Do the parameters of "upa" match those of "space"?
7960 static isl_bool
isl_union_pw_aff_matching_params(
7961 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7963 isl_space
*upa_space
;
7967 return isl_bool_error
;
7969 upa_space
= isl_union_pw_aff_get_space(upa
);
7971 match
= isl_space_has_equal_params(space
, upa_space
);
7973 isl_space_free(upa_space
);
7977 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7978 * space represents the new parameters.
7979 * res collects the results.
7981 struct isl_union_pw_aff_reset_params_data
{
7983 isl_union_pw_aff
*res
;
7986 /* Replace the parameters of "pa" by data->space and
7987 * add the result to data->res.
7989 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7991 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7994 space
= isl_pw_aff_get_space(pa
);
7995 space
= isl_space_replace_params(space
, data
->space
);
7996 pa
= isl_pw_aff_reset_space(pa
, space
);
7997 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7999 return data
->res
? isl_stat_ok
: isl_stat_error
;
8002 /* Replace the domain space of "upa" by "space".
8003 * Since a union expression does not have a (single) domain space,
8004 * "space" is necessarily a parameter space.
8006 * Since the order and the names of the parameters determine
8007 * the hash value, we need to create a new hash table.
8009 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8010 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8012 struct isl_union_pw_aff_reset_params_data data
= { space
};
8015 match
= isl_union_pw_aff_matching_params(upa
, space
);
8017 upa
= isl_union_pw_aff_free(upa
);
8019 isl_space_free(space
);
8023 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8024 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8025 data
.res
= isl_union_pw_aff_free(data
.res
);
8027 isl_union_pw_aff_free(upa
);
8028 isl_space_free(space
);
8032 /* Return the floor of "pa".
8034 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8036 return isl_pw_aff_floor(pa
);
8039 /* Given f, return floor(f).
8041 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8042 __isl_take isl_union_pw_aff
*upa
)
8044 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8049 * upa mod m = upa - m * floor(upa/m)
8051 * with m an integer value.
8053 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8054 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8056 isl_union_pw_aff
*res
;
8061 if (!isl_val_is_int(m
))
8062 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8063 "expecting integer modulo", goto error
);
8064 if (!isl_val_is_pos(m
))
8065 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8066 "expecting positive modulo", goto error
);
8068 res
= isl_union_pw_aff_copy(upa
);
8069 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8070 upa
= isl_union_pw_aff_floor(upa
);
8071 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8072 res
= isl_union_pw_aff_sub(res
, upa
);
8077 isl_union_pw_aff_free(upa
);
8081 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8082 * pos is the output position that needs to be extracted.
8083 * res collects the results.
8085 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8087 isl_union_pw_aff
*res
;
8090 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8091 * (assuming it has such a dimension) and add it to data->res.
8093 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8095 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8099 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8101 return isl_stat_error
;
8102 if (data
->pos
>= n_out
) {
8103 isl_pw_multi_aff_free(pma
);
8107 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8108 isl_pw_multi_aff_free(pma
);
8110 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8112 return data
->res
? isl_stat_ok
: isl_stat_error
;
8115 /* Extract an isl_union_pw_aff corresponding to
8116 * output dimension "pos" of "upma".
8118 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8119 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8121 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8128 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8129 "cannot extract at negative position", return NULL
);
8131 space
= isl_union_pw_multi_aff_get_space(upma
);
8132 data
.res
= isl_union_pw_aff_empty(space
);
8134 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8135 &get_union_pw_aff
, &data
) < 0)
8136 data
.res
= isl_union_pw_aff_free(data
.res
);
8141 /* Return a union piecewise affine expression
8142 * that is equal to "aff" on "domain".
8144 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8145 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8149 pa
= isl_pw_aff_from_aff(aff
);
8150 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8153 /* Return a union piecewise affine expression
8154 * that is equal to the parameter identified by "id" on "domain".
8156 * Make sure the parameter appears in the space passed to
8157 * isl_aff_param_on_domain_space_id.
8159 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8160 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8165 space
= isl_union_set_get_space(domain
);
8166 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8167 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8168 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8171 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8172 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8174 * "res" collects the results.
8176 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8178 isl_union_pw_aff
*res
;
8181 /* Construct a piecewise affine expression that is equal to data->pa
8182 * on "domain" and add the result to data->res.
8184 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8186 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8190 pa
= isl_pw_aff_copy(data
->pa
);
8191 dim
= isl_set_dim(domain
, isl_dim_set
);
8193 pa
= isl_pw_aff_free(pa
);
8194 pa
= isl_pw_aff_from_range(pa
);
8195 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8196 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8197 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8198 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8200 return data
->res
? isl_stat_ok
: isl_stat_error
;
8203 /* Return a union piecewise affine expression
8204 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8205 * have been aligned.
8207 * Construct an isl_pw_aff on each of the sets in "domain" and
8208 * collect the results.
8210 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8211 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8213 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8216 space
= isl_union_set_get_space(domain
);
8217 data
.res
= isl_union_pw_aff_empty(space
);
8219 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8220 data
.res
= isl_union_pw_aff_free(data
.res
);
8221 isl_union_set_free(domain
);
8222 isl_pw_aff_free(pa
);
8226 /* Return a union piecewise affine expression
8227 * that is equal to "pa" on "domain".
8229 * Check that "pa" is a parametric expression,
8230 * align the parameters if needed and call
8231 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8233 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8234 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8237 isl_bool equal_params
;
8238 isl_space
*domain_space
, *pa_space
;
8240 pa_space
= isl_pw_aff_peek_space(pa
);
8241 is_set
= isl_space_is_set(pa_space
);
8245 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8246 "expecting parametric expression", goto error
);
8248 domain_space
= isl_union_set_get_space(domain
);
8249 pa_space
= isl_pw_aff_get_space(pa
);
8250 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8251 if (equal_params
>= 0 && !equal_params
) {
8254 space
= isl_space_align_params(domain_space
, pa_space
);
8255 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8256 domain
= isl_union_set_align_params(domain
, space
);
8258 isl_space_free(domain_space
);
8259 isl_space_free(pa_space
);
8262 if (equal_params
< 0)
8264 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8266 isl_union_set_free(domain
);
8267 isl_pw_aff_free(pa
);
8271 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8272 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8273 * "res" collects the results.
8275 struct isl_union_pw_aff_val_on_domain_data
{
8277 isl_union_pw_aff
*res
;
8280 /* Construct a piecewise affine expression that is equal to data->v
8281 * on "domain" and add the result to data->res.
8283 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8285 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8289 v
= isl_val_copy(data
->v
);
8290 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8291 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8293 return data
->res
? isl_stat_ok
: isl_stat_error
;
8296 /* Return a union piecewise affine expression
8297 * that is equal to "v" on "domain".
8299 * Construct an isl_pw_aff on each of the sets in "domain" and
8300 * collect the results.
8302 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8303 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8305 struct isl_union_pw_aff_val_on_domain_data data
;
8308 space
= isl_union_set_get_space(domain
);
8309 data
.res
= isl_union_pw_aff_empty(space
);
8311 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8312 data
.res
= isl_union_pw_aff_free(data
.res
);
8313 isl_union_set_free(domain
);
8318 /* Construct a piecewise multi affine expression
8319 * that is equal to "pa" and add it to upma.
8321 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8324 isl_union_pw_multi_aff
**upma
= user
;
8325 isl_pw_multi_aff
*pma
;
8327 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8328 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8330 return *upma
? isl_stat_ok
: isl_stat_error
;
8333 /* Construct and return a union piecewise multi affine expression
8334 * that is equal to the given union piecewise affine expression.
8336 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8337 __isl_take isl_union_pw_aff
*upa
)
8340 isl_union_pw_multi_aff
*upma
;
8345 space
= isl_union_pw_aff_get_space(upa
);
8346 upma
= isl_union_pw_multi_aff_empty(space
);
8348 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8349 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8350 upma
= isl_union_pw_multi_aff_free(upma
);
8352 isl_union_pw_aff_free(upa
);
8356 /* Compute the set of elements in the domain of "pa" where it is zero and
8357 * add this set to "uset".
8359 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8361 isl_union_set
**uset
= (isl_union_set
**)user
;
8363 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8365 return *uset
? isl_stat_ok
: isl_stat_error
;
8368 /* Return a union set containing those elements in the domain
8369 * of "upa" where it is zero.
8371 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8372 __isl_take isl_union_pw_aff
*upa
)
8374 isl_union_set
*zero
;
8376 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8377 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8378 zero
= isl_union_set_free(zero
);
8380 isl_union_pw_aff_free(upa
);
8384 /* Internal data structure for isl_union_pw_aff_bind_id,
8385 * storing the parameter that needs to be bound and
8386 * the accumulated results.
8388 struct isl_bind_id_data
{
8390 isl_union_set
*bound
;
8393 /* Bind the piecewise affine function "pa" to the parameter data->id,
8394 * adding the resulting elements in the domain where the expression
8395 * is equal to the parameter to data->bound.
8397 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8399 struct isl_bind_id_data
*data
= user
;
8402 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8403 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8405 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8408 /* Bind the union piecewise affine function "upa" to the parameter "id",
8409 * returning the elements in the domain where the expression
8410 * is equal to the parameter.
8412 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8413 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8415 struct isl_bind_id_data data
= { id
};
8417 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8418 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8419 data
.bound
= isl_union_set_free(data
.bound
);
8421 isl_union_pw_aff_free(upa
);
8426 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8427 * upma is the function that is plugged in.
8428 * pa is the current part of the function in which upma is plugged in.
8429 * res collects the results.
8431 struct isl_union_pw_aff_pullback_upma_data
{
8432 isl_union_pw_multi_aff
*upma
;
8434 isl_union_pw_aff
*res
;
8437 /* Check if "pma" can be plugged into data->pa.
8438 * If so, perform the pullback and add the result to data->res.
8440 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8442 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8445 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8446 pma
->dim
, isl_dim_out
)) {
8447 isl_pw_multi_aff_free(pma
);
8451 pa
= isl_pw_aff_copy(data
->pa
);
8452 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8454 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8456 return data
->res
? isl_stat_ok
: isl_stat_error
;
8459 /* Check if any of the elements of data->upma can be plugged into pa,
8460 * add if so add the result to data->res.
8462 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8464 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8468 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8470 isl_pw_aff_free(pa
);
8475 /* Compute the pullback of "upa" by the function represented by "upma".
8476 * In other words, plug in "upma" in "upa". The result contains
8477 * expressions defined over the domain space of "upma".
8479 * Run over all pairs of elements in "upa" and "upma", perform
8480 * the pullback when appropriate and collect the results.
8481 * If the hash value were based on the domain space rather than
8482 * the function space, then we could run through all elements
8483 * of "upma" and directly pick out the corresponding element of "upa".
8485 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8486 __isl_take isl_union_pw_aff
*upa
,
8487 __isl_take isl_union_pw_multi_aff
*upma
)
8489 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8492 space
= isl_union_pw_multi_aff_get_space(upma
);
8493 upa
= isl_union_pw_aff_align_params(upa
, space
);
8494 space
= isl_union_pw_aff_get_space(upa
);
8495 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8501 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8502 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8503 data
.res
= isl_union_pw_aff_free(data
.res
);
8505 isl_union_pw_aff_free(upa
);
8506 isl_union_pw_multi_aff_free(upma
);
8509 isl_union_pw_aff_free(upa
);
8510 isl_union_pw_multi_aff_free(upma
);
8515 #define BASE union_pw_aff
8517 #define DOMBASE union_set
8519 #include <isl_multi_explicit_domain.c>
8520 #include <isl_multi_union_pw_aff_explicit_domain.c>
8521 #include <isl_multi_templ.c>
8522 #include <isl_multi_un_op_templ.c>
8523 #include <isl_multi_bin_val_templ.c>
8524 #include <isl_multi_apply_set.c>
8525 #include <isl_multi_apply_union_set.c>
8526 #include <isl_multi_arith_templ.c>
8527 #include <isl_multi_bind_templ.c>
8528 #include <isl_multi_coalesce.c>
8529 #include <isl_multi_dim_id_templ.c>
8530 #include <isl_multi_floor.c>
8531 #include <isl_multi_from_base_templ.c>
8532 #include <isl_multi_gist.c>
8533 #include <isl_multi_align_set.c>
8534 #include <isl_multi_align_union_set.c>
8535 #include <isl_multi_intersect.c>
8536 #include <isl_multi_nan_templ.c>
8537 #include <isl_multi_tuple_id_templ.c>
8538 #include <isl_multi_union_add_templ.c>
8539 #include <isl_multi_zero_space_templ.c>
8541 /* Does "mupa" have a non-trivial explicit domain?
8543 * The explicit domain, if present, is trivial if it represents
8544 * an (obviously) universe parameter set.
8546 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8547 __isl_keep isl_multi_union_pw_aff
*mupa
)
8549 isl_bool is_params
, trivial
;
8553 return isl_bool_error
;
8554 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8555 return isl_bool_false
;
8556 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8557 if (is_params
< 0 || !is_params
)
8558 return isl_bool_not(is_params
);
8559 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8560 trivial
= isl_set_plain_is_universe(set
);
8562 return isl_bool_not(trivial
);
8565 /* Construct a multiple union piecewise affine expression
8566 * in the given space with value zero in each of the output dimensions.
8568 * Since there is no canonical zero value for
8569 * a union piecewise affine expression, we can only construct
8570 * a zero-dimensional "zero" value.
8572 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8573 __isl_take isl_space
*space
)
8581 params
= isl_space_is_params(space
);
8585 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8586 "expecting proper set space", goto error
);
8587 if (!isl_space_is_set(space
))
8588 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8589 "expecting set space", goto error
);
8590 dim
= isl_space_dim(space
, isl_dim_out
);
8594 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8595 "expecting 0D space", goto error
);
8597 return isl_multi_union_pw_aff_alloc(space
);
8599 isl_space_free(space
);
8603 /* Construct and return a multi union piecewise affine expression
8604 * that is equal to the given multi affine expression.
8606 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8607 __isl_take isl_multi_aff
*ma
)
8609 isl_multi_pw_aff
*mpa
;
8611 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8612 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8615 /* This function performs the same operation as
8616 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8617 * isl_multi_aff when exported.
8619 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8620 __isl_take isl_multi_aff
*ma
)
8622 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8625 /* Construct and return a multi union piecewise affine expression
8626 * that is equal to the given multi piecewise affine expression.
8628 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8629 __isl_take isl_multi_pw_aff
*mpa
)
8634 isl_multi_union_pw_aff
*mupa
;
8636 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8638 mpa
= isl_multi_pw_aff_free(mpa
);
8642 space
= isl_multi_pw_aff_get_space(mpa
);
8643 space
= isl_space_range(space
);
8644 mupa
= isl_multi_union_pw_aff_alloc(space
);
8646 for (i
= 0; i
< n
; ++i
) {
8648 isl_union_pw_aff
*upa
;
8650 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8651 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8652 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8655 isl_multi_pw_aff_free(mpa
);
8660 /* Extract the range space of "pma" and assign it to *space.
8661 * If *space has already been set (through a previous call to this function),
8662 * then check that the range space is the same.
8664 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8666 isl_space
**space
= user
;
8667 isl_space
*pma_space
;
8670 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8671 isl_pw_multi_aff_free(pma
);
8674 return isl_stat_error
;
8680 equal
= isl_space_is_equal(pma_space
, *space
);
8681 isl_space_free(pma_space
);
8684 return isl_stat_error
;
8686 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8687 "range spaces not the same", return isl_stat_error
);
8691 /* Construct and return a multi union piecewise affine expression
8692 * that is equal to the given union piecewise multi affine expression.
8694 * In order to be able to perform the conversion, the input
8695 * needs to be non-empty and may only involve a single range space.
8697 * If the resulting multi union piecewise affine expression has
8698 * an explicit domain, then assign it the domain of the input.
8699 * In other cases, the domain is stored in the individual elements.
8701 __isl_give isl_multi_union_pw_aff
*
8702 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8703 __isl_take isl_union_pw_multi_aff
*upma
)
8705 isl_space
*space
= NULL
;
8706 isl_multi_union_pw_aff
*mupa
;
8710 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8714 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8715 "cannot extract range space from empty input",
8717 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8724 n
= isl_space_dim(space
, isl_dim_set
);
8726 space
= isl_space_free(space
);
8727 mupa
= isl_multi_union_pw_aff_alloc(space
);
8729 for (i
= 0; i
< n
; ++i
) {
8730 isl_union_pw_aff
*upa
;
8732 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8733 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8735 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8737 isl_union_pw_multi_aff
*copy
;
8739 copy
= isl_union_pw_multi_aff_copy(upma
);
8740 dom
= isl_union_pw_multi_aff_domain(copy
);
8741 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8744 isl_union_pw_multi_aff_free(upma
);
8747 isl_space_free(space
);
8748 isl_union_pw_multi_aff_free(upma
);
8752 /* This function performs the same operation as
8753 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8754 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8756 __isl_give isl_multi_union_pw_aff
*
8757 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8758 __isl_take isl_union_pw_multi_aff
*upma
)
8760 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8763 /* Try and create an isl_multi_union_pw_aff that is equivalent
8764 * to the given isl_union_map.
8765 * The isl_union_map is required to be single-valued in each space.
8766 * Moreover, it cannot be empty and all range spaces need to be the same.
8767 * Otherwise, an error is produced.
8769 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8770 __isl_take isl_union_map
*umap
)
8772 isl_union_pw_multi_aff
*upma
;
8774 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8775 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8778 /* This function performs the same operation as
8779 * isl_multi_union_pw_aff_from_union_map,
8780 * but is considered as a function on an isl_union_map when exported.
8782 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8783 __isl_take isl_union_map
*umap
)
8785 return isl_multi_union_pw_aff_from_union_map(umap
);
8788 /* Return a multiple union piecewise affine expression
8789 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8790 * have been aligned.
8792 * If the resulting multi union piecewise affine expression has
8793 * an explicit domain, then assign it the input domain.
8794 * In other cases, the domain is stored in the individual elements.
8796 static __isl_give isl_multi_union_pw_aff
*
8797 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8798 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8803 isl_multi_union_pw_aff
*mupa
;
8805 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8806 if (!domain
|| n
< 0)
8809 space
= isl_multi_val_get_space(mv
);
8810 mupa
= isl_multi_union_pw_aff_alloc(space
);
8811 for (i
= 0; i
< n
; ++i
) {
8813 isl_union_pw_aff
*upa
;
8815 v
= isl_multi_val_get_val(mv
, i
);
8816 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8818 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8820 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8821 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8822 isl_union_set_copy(domain
));
8824 isl_union_set_free(domain
);
8825 isl_multi_val_free(mv
);
8828 isl_union_set_free(domain
);
8829 isl_multi_val_free(mv
);
8833 /* Return a multiple union piecewise affine expression
8834 * that is equal to "mv" on "domain".
8836 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8837 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8839 isl_bool equal_params
;
8843 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
8844 if (equal_params
< 0)
8847 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8849 domain
= isl_union_set_align_params(domain
,
8850 isl_multi_val_get_space(mv
));
8851 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8852 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8854 isl_union_set_free(domain
);
8855 isl_multi_val_free(mv
);
8859 /* Return a multiple union piecewise affine expression
8860 * that is equal to "ma" on "domain".
8862 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8863 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8865 isl_pw_multi_aff
*pma
;
8867 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
8868 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
8871 /* Return a multiple union piecewise affine expression
8872 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8873 * have been aligned.
8875 * If the resulting multi union piecewise affine expression has
8876 * an explicit domain, then assign it the input domain.
8877 * In other cases, the domain is stored in the individual elements.
8879 static __isl_give isl_multi_union_pw_aff
*
8880 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8881 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
8886 isl_multi_union_pw_aff
*mupa
;
8888 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
8889 if (!domain
|| n
< 0)
8891 space
= isl_pw_multi_aff_get_space(pma
);
8892 mupa
= isl_multi_union_pw_aff_alloc(space
);
8893 for (i
= 0; i
< n
; ++i
) {
8895 isl_union_pw_aff
*upa
;
8897 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8898 upa
= isl_union_pw_aff_pw_aff_on_domain(
8899 isl_union_set_copy(domain
), pa
);
8900 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8902 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8903 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8904 isl_union_set_copy(domain
));
8906 isl_union_set_free(domain
);
8907 isl_pw_multi_aff_free(pma
);
8910 isl_union_set_free(domain
);
8911 isl_pw_multi_aff_free(pma
);
8915 /* Return a multiple union piecewise affine expression
8916 * that is equal to "pma" on "domain".
8918 __isl_give isl_multi_union_pw_aff
*
8919 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
8920 __isl_take isl_pw_multi_aff
*pma
)
8922 isl_bool equal_params
;
8925 space
= isl_pw_multi_aff_peek_space(pma
);
8926 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
8927 if (equal_params
< 0)
8930 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8932 domain
= isl_union_set_align_params(domain
,
8933 isl_pw_multi_aff_get_space(pma
));
8934 pma
= isl_pw_multi_aff_align_params(pma
,
8935 isl_union_set_get_space(domain
));
8936 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
8939 isl_union_set_free(domain
);
8940 isl_pw_multi_aff_free(pma
);
8944 /* Return a union set containing those elements in the domains
8945 * of the elements of "mupa" where they are all zero.
8947 * If there are no elements, then simply return the entire domain.
8949 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8950 __isl_take isl_multi_union_pw_aff
*mupa
)
8954 isl_union_pw_aff
*upa
;
8955 isl_union_set
*zero
;
8957 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8959 mupa
= isl_multi_union_pw_aff_free(mupa
);
8964 return isl_multi_union_pw_aff_domain(mupa
);
8966 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8967 zero
= isl_union_pw_aff_zero_union_set(upa
);
8969 for (i
= 1; i
< n
; ++i
) {
8970 isl_union_set
*zero_i
;
8972 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8973 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8975 zero
= isl_union_set_intersect(zero
, zero_i
);
8978 isl_multi_union_pw_aff_free(mupa
);
8982 /* Construct a union map mapping the shared domain
8983 * of the union piecewise affine expressions to the range of "mupa"
8984 * in the special case of a 0D multi union piecewise affine expression.
8986 * Construct a map between the explicit domain of "mupa" and
8988 * Note that this assumes that the domain consists of explicit elements.
8990 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
8991 __isl_take isl_multi_union_pw_aff
*mupa
)
8995 isl_union_set
*dom
, *ran
;
8997 space
= isl_multi_union_pw_aff_get_space(mupa
);
8998 dom
= isl_multi_union_pw_aff_domain(mupa
);
8999 ran
= isl_union_set_from_set(isl_set_universe(space
));
9001 is_params
= isl_union_set_is_params(dom
);
9003 dom
= isl_union_set_free(dom
);
9005 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9006 "cannot create union map from expression without "
9007 "explicit domain elements",
9008 dom
= isl_union_set_free(dom
));
9010 return isl_union_map_from_domain_and_range(dom
, ran
);
9013 /* Construct a union map mapping the shared domain
9014 * of the union piecewise affine expressions to the range of "mupa"
9015 * with each dimension in the range equated to the
9016 * corresponding union piecewise affine expression.
9018 * If the input is zero-dimensional, then construct a mapping
9019 * from its explicit domain.
9021 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9022 __isl_take isl_multi_union_pw_aff
*mupa
)
9027 isl_union_map
*umap
;
9028 isl_union_pw_aff
*upa
;
9030 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9032 mupa
= isl_multi_union_pw_aff_free(mupa
);
9037 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9039 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9040 umap
= isl_union_map_from_union_pw_aff(upa
);
9042 for (i
= 1; i
< n
; ++i
) {
9043 isl_union_map
*umap_i
;
9045 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9046 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9047 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9050 space
= isl_multi_union_pw_aff_get_space(mupa
);
9051 umap
= isl_union_map_reset_range_space(umap
, space
);
9053 isl_multi_union_pw_aff_free(mupa
);
9057 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9058 * "range" is the space from which to set the range space.
9059 * "res" collects the results.
9061 struct isl_union_pw_multi_aff_reset_range_space_data
{
9063 isl_union_pw_multi_aff
*res
;
9066 /* Replace the range space of "pma" by the range space of data->range and
9067 * add the result to data->res.
9069 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9071 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9074 space
= isl_pw_multi_aff_get_space(pma
);
9075 space
= isl_space_domain(space
);
9076 space
= isl_space_extend_domain_with_range(space
,
9077 isl_space_copy(data
->range
));
9078 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9079 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9081 return data
->res
? isl_stat_ok
: isl_stat_error
;
9084 /* Replace the range space of all the piecewise affine expressions in "upma" by
9085 * the range space of "space".
9087 * This assumes that all these expressions have the same output dimension.
9089 * Since the spaces of the expressions change, so do their hash values.
9090 * We therefore need to create a new isl_union_pw_multi_aff.
9091 * Note that the hash value is currently computed based on the entire
9092 * space even though there can only be a single expression with a given
9095 static __isl_give isl_union_pw_multi_aff
*
9096 isl_union_pw_multi_aff_reset_range_space(
9097 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9099 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9100 isl_space
*space_upma
;
9102 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9103 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9104 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9105 &reset_range_space
, &data
) < 0)
9106 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9108 isl_space_free(space
);
9109 isl_union_pw_multi_aff_free(upma
);
9113 /* Construct and return a union piecewise multi affine expression
9114 * that is equal to the given multi union piecewise affine expression,
9115 * in the special case of a 0D multi union piecewise affine expression.
9117 * Construct a union piecewise multi affine expression
9118 * on top of the explicit domain of the input.
9120 __isl_give isl_union_pw_multi_aff
*
9121 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9122 __isl_take isl_multi_union_pw_aff
*mupa
)
9126 isl_union_set
*domain
;
9128 space
= isl_multi_union_pw_aff_get_space(mupa
);
9129 mv
= isl_multi_val_zero(space
);
9130 domain
= isl_multi_union_pw_aff_domain(mupa
);
9131 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9134 /* Construct and return a union piecewise multi affine expression
9135 * that is equal to the given multi union piecewise affine expression.
9137 * If the input is zero-dimensional, then
9138 * construct a union piecewise multi affine expression
9139 * on top of the explicit domain of the input.
9141 __isl_give isl_union_pw_multi_aff
*
9142 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9143 __isl_take isl_multi_union_pw_aff
*mupa
)
9148 isl_union_pw_multi_aff
*upma
;
9149 isl_union_pw_aff
*upa
;
9151 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9153 mupa
= isl_multi_union_pw_aff_free(mupa
);
9158 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9160 space
= isl_multi_union_pw_aff_get_space(mupa
);
9161 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9162 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9164 for (i
= 1; i
< n
; ++i
) {
9165 isl_union_pw_multi_aff
*upma_i
;
9167 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9168 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9169 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9172 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9174 isl_multi_union_pw_aff_free(mupa
);
9178 /* Intersect the range of "mupa" with "range",
9179 * in the special case where "mupa" is 0D.
9181 * Intersect the domain of "mupa" with the constraints on the parameters
9184 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9185 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9187 range
= isl_set_params(range
);
9188 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9192 /* Intersect the range of "mupa" with "range".
9193 * That is, keep only those domain elements that have a function value
9196 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9197 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9199 isl_union_pw_multi_aff
*upma
;
9200 isl_union_set
*domain
;
9205 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9206 if (n
< 0 || !range
)
9209 space
= isl_set_get_space(range
);
9210 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9211 space
, isl_dim_set
);
9212 isl_space_free(space
);
9216 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9217 "space don't match", goto error
);
9219 return mupa_intersect_range_0D(mupa
, range
);
9221 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9222 isl_multi_union_pw_aff_copy(mupa
));
9223 domain
= isl_union_set_from_set(range
);
9224 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9225 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9229 isl_multi_union_pw_aff_free(mupa
);
9230 isl_set_free(range
);
9234 /* Return the shared domain of the elements of "mupa",
9235 * in the special case where "mupa" is zero-dimensional.
9237 * Return the explicit domain of "mupa".
9238 * Note that this domain may be a parameter set, either
9239 * because "mupa" is meant to live in a set space or
9240 * because no explicit domain has been set.
9242 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9243 __isl_take isl_multi_union_pw_aff
*mupa
)
9247 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9248 isl_multi_union_pw_aff_free(mupa
);
9253 /* Return the shared domain of the elements of "mupa".
9255 * If "mupa" is zero-dimensional, then return its explicit domain.
9257 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9258 __isl_take isl_multi_union_pw_aff
*mupa
)
9262 isl_union_pw_aff
*upa
;
9265 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9267 mupa
= isl_multi_union_pw_aff_free(mupa
);
9272 return isl_multi_union_pw_aff_domain_0D(mupa
);
9274 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9275 dom
= isl_union_pw_aff_domain(upa
);
9276 for (i
= 1; i
< n
; ++i
) {
9277 isl_union_set
*dom_i
;
9279 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9280 dom_i
= isl_union_pw_aff_domain(upa
);
9281 dom
= isl_union_set_intersect(dom
, dom_i
);
9284 isl_multi_union_pw_aff_free(mupa
);
9288 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9289 * In particular, the spaces have been aligned.
9290 * The result is defined over the shared domain of the elements of "mupa"
9292 * We first extract the parametric constant part of "aff" and
9293 * define that over the shared domain.
9294 * Then we iterate over all input dimensions of "aff" and add the corresponding
9295 * multiples of the elements of "mupa".
9296 * Finally, we consider the integer divisions, calling the function
9297 * recursively to obtain an isl_union_pw_aff corresponding to the
9298 * integer division argument.
9300 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9301 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9304 isl_size n_in
, n_div
;
9305 isl_union_pw_aff
*upa
;
9306 isl_union_set
*uset
;
9310 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9311 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9312 if (n_in
< 0 || n_div
< 0)
9315 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9316 cst
= isl_aff_copy(aff
);
9317 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9318 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9319 cst
= isl_aff_project_domain_on_params(cst
);
9320 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9322 for (i
= 0; i
< n_in
; ++i
) {
9323 isl_union_pw_aff
*upa_i
;
9325 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9327 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9328 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9329 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9330 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9333 for (i
= 0; i
< n_div
; ++i
) {
9335 isl_union_pw_aff
*upa_i
;
9337 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9339 div
= isl_aff_get_div(aff
, i
);
9340 upa_i
= multi_union_pw_aff_apply_aff(
9341 isl_multi_union_pw_aff_copy(mupa
), div
);
9342 upa_i
= isl_union_pw_aff_floor(upa_i
);
9343 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9344 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9345 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9348 isl_multi_union_pw_aff_free(mupa
);
9353 isl_multi_union_pw_aff_free(mupa
);
9358 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9359 * with the domain of "aff".
9360 * Furthermore, the dimension of this space needs to be greater than zero.
9361 * The result is defined over the shared domain of the elements of "mupa"
9363 * We perform these checks and then hand over control to
9364 * multi_union_pw_aff_apply_aff.
9366 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9367 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9370 isl_space
*space1
, *space2
;
9373 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9374 isl_aff_get_space(aff
));
9375 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9379 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9380 space2
= isl_aff_get_domain_space(aff
);
9381 equal
= isl_space_is_equal(space1
, space2
);
9382 isl_space_free(space1
);
9383 isl_space_free(space2
);
9387 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9388 "spaces don't match", goto error
);
9389 dim
= isl_aff_dim(aff
, isl_dim_in
);
9393 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9394 "cannot determine domains", goto error
);
9396 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9398 isl_multi_union_pw_aff_free(mupa
);
9403 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9404 * The space of "mupa" is known to be compatible with the domain of "ma".
9406 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9407 * on the domain of "mupa".
9409 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9410 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9414 dom
= isl_multi_union_pw_aff_domain(mupa
);
9415 ma
= isl_multi_aff_project_domain_on_params(ma
);
9417 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9420 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9421 * with the domain of "ma".
9422 * The result is defined over the shared domain of the elements of "mupa"
9424 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9425 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9427 isl_space
*space1
, *space2
;
9428 isl_multi_union_pw_aff
*res
;
9431 isl_size n_in
, n_out
;
9433 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9434 isl_multi_aff_get_space(ma
));
9435 ma
= isl_multi_aff_align_params(ma
,
9436 isl_multi_union_pw_aff_get_space(mupa
));
9437 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9438 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9439 if (!mupa
|| n_in
< 0 || n_out
< 0)
9442 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9443 space2
= isl_multi_aff_get_domain_space(ma
);
9444 equal
= isl_space_is_equal(space1
, space2
);
9445 isl_space_free(space1
);
9446 isl_space_free(space2
);
9450 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9451 "spaces don't match", goto error
);
9453 return mupa_apply_multi_aff_0D(mupa
, ma
);
9455 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9456 res
= isl_multi_union_pw_aff_alloc(space1
);
9458 for (i
= 0; i
< n_out
; ++i
) {
9460 isl_union_pw_aff
*upa
;
9462 aff
= isl_multi_aff_get_aff(ma
, i
);
9463 upa
= multi_union_pw_aff_apply_aff(
9464 isl_multi_union_pw_aff_copy(mupa
), aff
);
9465 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9468 isl_multi_aff_free(ma
);
9469 isl_multi_union_pw_aff_free(mupa
);
9472 isl_multi_union_pw_aff_free(mupa
);
9473 isl_multi_aff_free(ma
);
9477 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9478 * The space of "mupa" is known to be compatible with the domain of "pa".
9480 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9481 * on the domain of "mupa".
9483 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9484 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9488 dom
= isl_multi_union_pw_aff_domain(mupa
);
9489 pa
= isl_pw_aff_project_domain_on_params(pa
);
9491 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9494 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9495 * with the domain of "pa".
9496 * Furthermore, the dimension of this space needs to be greater than zero.
9497 * The result is defined over the shared domain of the elements of "mupa"
9499 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9500 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9505 isl_space
*space
, *space2
;
9506 isl_union_pw_aff
*upa
;
9508 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9509 isl_pw_aff_get_space(pa
));
9510 pa
= isl_pw_aff_align_params(pa
,
9511 isl_multi_union_pw_aff_get_space(mupa
));
9515 space
= isl_multi_union_pw_aff_get_space(mupa
);
9516 space2
= isl_pw_aff_get_domain_space(pa
);
9517 equal
= isl_space_is_equal(space
, space2
);
9518 isl_space_free(space
);
9519 isl_space_free(space2
);
9523 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9524 "spaces don't match", goto error
);
9525 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9529 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9531 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9532 upa
= isl_union_pw_aff_empty(space
);
9534 for (i
= 0; i
< pa
->n
; ++i
) {
9537 isl_multi_union_pw_aff
*mupa_i
;
9538 isl_union_pw_aff
*upa_i
;
9540 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9541 domain
= isl_set_copy(pa
->p
[i
].set
);
9542 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9543 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9544 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9545 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9548 isl_multi_union_pw_aff_free(mupa
);
9549 isl_pw_aff_free(pa
);
9552 isl_multi_union_pw_aff_free(mupa
);
9553 isl_pw_aff_free(pa
);
9557 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9558 * The space of "mupa" is known to be compatible with the domain of "pma".
9560 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9561 * on the domain of "mupa".
9563 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9564 __isl_take isl_multi_union_pw_aff
*mupa
,
9565 __isl_take isl_pw_multi_aff
*pma
)
9569 dom
= isl_multi_union_pw_aff_domain(mupa
);
9570 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9572 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9575 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9576 * with the domain of "pma".
9577 * The result is defined over the shared domain of the elements of "mupa"
9579 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9580 __isl_take isl_multi_union_pw_aff
*mupa
,
9581 __isl_take isl_pw_multi_aff
*pma
)
9583 isl_space
*space1
, *space2
;
9584 isl_multi_union_pw_aff
*res
;
9587 isl_size n_in
, n_out
;
9589 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9590 isl_pw_multi_aff_get_space(pma
));
9591 pma
= isl_pw_multi_aff_align_params(pma
,
9592 isl_multi_union_pw_aff_get_space(mupa
));
9596 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9597 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9598 equal
= isl_space_is_equal(space1
, space2
);
9599 isl_space_free(space1
);
9600 isl_space_free(space2
);
9604 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9605 "spaces don't match", goto error
);
9606 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9607 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9608 if (n_in
< 0 || n_out
< 0)
9611 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9613 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9614 res
= isl_multi_union_pw_aff_alloc(space1
);
9616 for (i
= 0; i
< n_out
; ++i
) {
9618 isl_union_pw_aff
*upa
;
9620 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9621 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9622 isl_multi_union_pw_aff_copy(mupa
), pa
);
9623 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9626 isl_pw_multi_aff_free(pma
);
9627 isl_multi_union_pw_aff_free(mupa
);
9630 isl_multi_union_pw_aff_free(mupa
);
9631 isl_pw_multi_aff_free(pma
);
9635 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9636 * If the explicit domain only keeps track of constraints on the parameters,
9637 * then only update those constraints.
9639 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9640 __isl_take isl_multi_union_pw_aff
*mupa
,
9641 __isl_keep isl_union_pw_multi_aff
*upma
)
9645 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9646 return isl_multi_union_pw_aff_free(mupa
);
9648 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9652 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9654 return isl_multi_union_pw_aff_free(mupa
);
9656 upma
= isl_union_pw_multi_aff_copy(upma
);
9658 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9659 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9661 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9664 return isl_multi_union_pw_aff_free(mupa
);
9668 /* Compute the pullback of "mupa" by the function represented by "upma".
9669 * In other words, plug in "upma" in "mupa". The result contains
9670 * expressions defined over the domain space of "upma".
9672 * Run over all elements of "mupa" and plug in "upma" in each of them.
9674 * If "mupa" has an explicit domain, then it is this domain
9675 * that needs to undergo a pullback instead, i.e., a preimage.
9677 __isl_give isl_multi_union_pw_aff
*
9678 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9679 __isl_take isl_multi_union_pw_aff
*mupa
,
9680 __isl_take isl_union_pw_multi_aff
*upma
)
9685 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9686 isl_union_pw_multi_aff_get_space(upma
));
9687 upma
= isl_union_pw_multi_aff_align_params(upma
,
9688 isl_multi_union_pw_aff_get_space(mupa
));
9689 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9690 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9694 for (i
= 0; i
< n
; ++i
) {
9695 isl_union_pw_aff
*upa
;
9697 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9698 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9699 isl_union_pw_multi_aff_copy(upma
));
9700 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9703 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9704 mupa
= preimage_explicit_domain(mupa
, upma
);
9706 isl_union_pw_multi_aff_free(upma
);
9709 isl_multi_union_pw_aff_free(mupa
);
9710 isl_union_pw_multi_aff_free(upma
);
9714 /* Extract the sequence of elements in "mupa" with domain space "space"
9715 * (ignoring parameters).
9717 * For the elements of "mupa" that are not defined on the specified space,
9718 * the corresponding element in the result is empty.
9720 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9721 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9725 isl_space
*space_mpa
;
9726 isl_multi_pw_aff
*mpa
;
9728 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9729 if (n
< 0 || !space
)
9732 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9733 space
= isl_space_replace_params(space
, space_mpa
);
9734 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9736 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9738 space
= isl_space_from_domain(space
);
9739 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9740 for (i
= 0; i
< n
; ++i
) {
9741 isl_union_pw_aff
*upa
;
9744 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9745 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9746 isl_space_copy(space
));
9747 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9748 isl_union_pw_aff_free(upa
);
9751 isl_space_free(space
);
9754 isl_space_free(space
);
9758 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9759 * should modify the base expressions in the input.
9761 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9762 * are taken into account.
9763 * "fn" is applied to each entry in the input.
9765 struct isl_union_pw_multi_aff_un_op_control
{
9766 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9767 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9770 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9771 * a second argument) for use as an isl_union_pw_multi_aff_transform
9772 * filter function (which does take a second argument).
9773 * Simply call control->filter without the second argument.
9775 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9776 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9778 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9780 return control
->filter(pma
);
9783 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9784 * a second argument) for use as an isl_union_pw_multi_aff_transform
9785 * base function (which does take a second argument).
9786 * Simply call control->fn without the second argument.
9788 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9789 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9791 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9793 return control
->fn(pma
);
9796 /* Construct an isl_union_pw_multi_aff that is obtained by
9797 * modifying "upma" according to "control".
9799 * isl_union_pw_multi_aff_transform performs essentially
9800 * the same operation, but takes a filter and a callback function
9801 * of a different form (with an extra argument).
9802 * Call isl_union_pw_multi_aff_transform with wrappers
9803 * that remove this extra argument.
9805 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9806 __isl_take isl_union_pw_multi_aff
*upma
,
9807 struct isl_union_pw_multi_aff_un_op_control
*control
)
9809 struct isl_union_pw_multi_aff_transform_control t_control
= {
9810 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9811 .filter_user
= control
,
9812 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9816 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9819 /* For each function in "upma" of the form A -> [B -> C],
9820 * extract the function A -> B and collect the results.
9822 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9823 __isl_take isl_union_pw_multi_aff
*upma
)
9825 struct isl_union_pw_multi_aff_un_op_control control
= {
9826 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9827 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9829 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9832 /* For each function in "upma" of the form A -> [B -> C],
9833 * extract the function A -> C and collect the results.
9835 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9836 __isl_take isl_union_pw_multi_aff
*upma
)
9838 struct isl_union_pw_multi_aff_un_op_control control
= {
9839 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9840 .fn
= &isl_pw_multi_aff_range_factor_range
,
9842 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9845 /* Evaluate the affine function "aff" in the void point "pnt".
9846 * In particular, return the value NaN.
9848 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
9849 __isl_take isl_point
*pnt
)
9853 ctx
= isl_point_get_ctx(pnt
);
9855 isl_point_free(pnt
);
9856 return isl_val_nan(ctx
);
9859 /* Evaluate the affine expression "aff"
9860 * in the coordinates (with denominator) "pnt".
9862 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
9863 __isl_keep isl_vec
*pnt
)
9872 ctx
= isl_vec_get_ctx(aff
);
9875 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
9876 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
9877 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
9878 v
= isl_val_normalize(v
);
9885 /* Check that the domain space of "aff" is equal to "space".
9887 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
9888 __isl_keep isl_space
*space
)
9892 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
9894 return isl_stat_error
;
9896 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9897 "incompatible spaces", return isl_stat_error
);
9901 /* Evaluate the affine function "aff" in "pnt".
9903 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
9904 __isl_take isl_point
*pnt
)
9908 isl_local_space
*ls
;
9910 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
9912 is_void
= isl_point_is_void(pnt
);
9916 return eval_void(aff
, pnt
);
9918 ls
= isl_aff_get_domain_local_space(aff
);
9919 pnt
= isl_local_space_lift_point(ls
, pnt
);
9921 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
9924 isl_point_free(pnt
);
9929 isl_point_free(pnt
);