2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
40 #include <isl_list_templ.c>
41 #include <isl_list_read_templ.c>
44 #define EL_BASE pw_aff
46 #include <isl_list_templ.c>
47 #include <isl_list_read_templ.c>
50 #define EL_BASE pw_multi_aff
52 #include <isl_list_templ.c>
53 #include <isl_list_read_templ.c>
56 #define EL_BASE union_pw_aff
58 #include <isl_list_templ.c>
59 #include <isl_list_read_templ.c>
62 #define EL_BASE union_pw_multi_aff
64 #include <isl_list_templ.c>
66 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
67 __isl_take isl_vec
*v
)
74 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
84 isl_local_space_free(ls
);
89 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
98 ctx
= isl_local_space_get_ctx(ls
);
99 if (!isl_local_space_divs_known(ls
))
100 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
102 if (!isl_local_space_is_set(ls
))
103 isl_die(ctx
, isl_error_invalid
,
104 "domain of affine expression should be a set",
107 total
= isl_local_space_dim(ls
, isl_dim_all
);
110 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
111 return isl_aff_alloc_vec(ls
, v
);
113 isl_local_space_free(ls
);
117 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
126 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
131 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
132 isl_vec_copy(aff
->v
));
135 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
143 return isl_aff_dup(aff
);
146 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
150 aff
= isl_aff_alloc(ls
);
154 isl_int_set_si(aff
->v
->el
[0], 1);
155 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
160 /* Return an affine expression that is equal to zero on domain space "space".
162 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
164 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
167 /* This function performs the same operation as isl_aff_zero_on_domain_space,
168 * but is considered as a function on an isl_space when exported.
170 __isl_give isl_aff
*isl_space_zero_aff_on_domain(__isl_take isl_space
*space
)
172 return isl_aff_zero_on_domain_space(space
);
175 /* Return a piecewise affine expression defined on the specified domain
176 * that is equal to zero.
178 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
180 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
183 /* Change "aff" into a NaN.
185 * Note that this function gets called from isl_aff_nan_on_domain,
186 * so "aff" may not have been initialized yet.
188 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
190 aff
= isl_aff_cow(aff
);
194 aff
->v
= isl_vec_clr(aff
->v
);
196 return isl_aff_free(aff
);
201 /* Return an affine expression defined on the specified domain
202 * that represents NaN.
204 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
208 aff
= isl_aff_alloc(ls
);
209 return isl_aff_set_nan(aff
);
212 /* Return an affine expression defined on the specified domain space
213 * that represents NaN.
215 __isl_give isl_aff
*isl_aff_nan_on_domain_space(__isl_take isl_space
*space
)
217 return isl_aff_nan_on_domain(isl_local_space_from_space(space
));
220 /* Return a piecewise affine expression defined on the specified domain space
221 * that represents NaN.
223 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain_space(
224 __isl_take isl_space
*space
)
226 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space
));
229 /* Return a piecewise affine expression defined on the specified domain
230 * that represents NaN.
232 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
234 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
237 /* Return an affine expression that is equal to "val" on
238 * domain local space "ls".
240 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
241 __isl_take isl_val
*val
)
247 if (!isl_val_is_rat(val
))
248 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
249 "expecting rational value", goto error
);
251 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
255 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
256 isl_int_set(aff
->v
->el
[1], val
->n
);
257 isl_int_set(aff
->v
->el
[0], val
->d
);
259 isl_local_space_free(ls
);
263 isl_local_space_free(ls
);
268 /* Return an affine expression that is equal to "val" on domain space "space".
270 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
271 __isl_take isl_val
*val
)
273 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
276 /* Return an affine expression that is equal to the specified dimension
279 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
280 enum isl_dim_type type
, unsigned pos
)
288 space
= isl_local_space_get_space(ls
);
291 if (isl_space_is_map(space
))
292 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
293 "expecting (parameter) set space", goto error
);
294 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
297 isl_space_free(space
);
298 aff
= isl_aff_alloc(ls
);
302 pos
+= isl_local_space_offset(aff
->ls
, type
);
304 isl_int_set_si(aff
->v
->el
[0], 1);
305 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
306 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
310 isl_local_space_free(ls
);
311 isl_space_free(space
);
315 /* Return a piecewise affine expression that is equal to
316 * the specified dimension in "ls".
318 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
319 enum isl_dim_type type
, unsigned pos
)
321 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
324 /* Return an affine expression that is equal to the parameter
325 * in the domain space "space" with identifier "id".
327 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
328 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
335 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
337 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
338 "parameter not found in space", goto error
);
340 ls
= isl_local_space_from_space(space
);
341 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
343 isl_space_free(space
);
348 /* This function performs the same operation as
349 * isl_aff_param_on_domain_space_id,
350 * but is considered as a function on an isl_space when exported.
352 __isl_give isl_aff
*isl_space_param_aff_on_domain_id(
353 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
355 return isl_aff_param_on_domain_space_id(space
, id
);
358 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
366 isl_local_space_free(aff
->ls
);
367 isl_vec_free(aff
->v
);
374 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
376 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
379 /* Return a hash value that digests "aff".
381 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
383 uint32_t hash
, ls_hash
, v_hash
;
388 hash
= isl_hash_init();
389 ls_hash
= isl_local_space_get_hash(aff
->ls
);
390 isl_hash_hash(hash
, ls_hash
);
391 v_hash
= isl_vec_get_hash(aff
->v
);
392 isl_hash_hash(hash
, v_hash
);
397 /* Return the domain local space of "aff".
399 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
400 __isl_keep isl_aff
*aff
)
402 return aff
? aff
->ls
: NULL
;
405 /* Return the number of variables of the given type in the domain of "aff".
407 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
411 ls
= isl_aff_peek_domain_local_space(aff
);
412 return isl_local_space_dim(ls
, type
);
415 /* Externally, an isl_aff has a map space, but internally, the
416 * ls field corresponds to the domain of that space.
418 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
421 return isl_size_error
;
422 if (type
== isl_dim_out
)
424 if (type
== isl_dim_in
)
426 return isl_aff_domain_dim(aff
, type
);
429 /* Return the offset of the first coefficient of type "type" in
430 * the domain of "aff".
432 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
436 ls
= isl_aff_peek_domain_local_space(aff
);
437 return isl_local_space_offset(ls
, type
);
440 /* Return the position of the dimension of the given type and name
442 * Return -1 if no such dimension can be found.
444 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
449 if (type
== isl_dim_out
)
451 if (type
== isl_dim_in
)
453 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
456 /* Return the domain space of "aff".
458 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
460 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
463 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
465 return isl_space_copy(isl_aff_peek_domain_space(aff
));
468 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
473 space
= isl_local_space_get_space(aff
->ls
);
474 space
= isl_space_from_domain(space
);
475 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
479 /* Return a copy of the domain space of "aff".
481 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
482 __isl_keep isl_aff
*aff
)
484 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
487 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
492 ls
= isl_local_space_copy(aff
->ls
);
493 ls
= isl_local_space_from_domain(ls
);
494 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
498 /* Return the local space of the domain of "aff".
499 * This may be either a copy or the local space itself
500 * if there is only one reference to "aff".
501 * This allows the local space to be modified inplace
502 * if both the expression and its local space have only a single reference.
503 * The caller is not allowed to modify "aff" between this call and
504 * a subsequent call to isl_aff_restore_domain_local_space.
505 * The only exception is that isl_aff_free can be called instead.
507 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
508 __isl_keep isl_aff
*aff
)
515 return isl_aff_get_domain_local_space(aff
);
521 /* Set the local space of the domain of "aff" to "ls",
522 * where the local space of "aff" may be missing
523 * due to a preceding call to isl_aff_take_domain_local_space.
524 * However, in this case, "aff" only has a single reference and
525 * then the call to isl_aff_cow has no effect.
527 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
528 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
534 isl_local_space_free(ls
);
538 aff
= isl_aff_cow(aff
);
541 isl_local_space_free(aff
->ls
);
547 isl_local_space_free(ls
);
551 /* Externally, an isl_aff has a map space, but internally, the
552 * ls field corresponds to the domain of that space.
554 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
555 enum isl_dim_type type
, unsigned pos
)
559 if (type
== isl_dim_out
)
561 if (type
== isl_dim_in
)
563 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
566 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
567 __isl_take isl_space
*space
)
569 aff
= isl_aff_cow(aff
);
573 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
575 return isl_aff_free(aff
);
580 isl_space_free(space
);
584 /* Reset the space of "aff". This function is called from isl_pw_templ.c
585 * and doesn't know if the space of an element object is represented
586 * directly or through its domain. It therefore passes along both.
588 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
589 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
591 isl_space_free(space
);
592 return isl_aff_reset_domain_space(aff
, domain
);
595 /* Reorder the coefficients of the affine expression based
596 * on the given reordering.
597 * The reordering r is assumed to have been extended with the local
600 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
601 __isl_take isl_reordering
*r
, int n_div
)
611 space
= isl_reordering_peek_space(r
);
612 dim
= isl_space_dim(space
, isl_dim_all
);
615 res
= isl_vec_alloc(vec
->ctx
, 2 + dim
+ n_div
);
618 isl_seq_cpy(res
->el
, vec
->el
, 2);
619 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
620 for (i
= 0; i
< r
->len
; ++i
)
621 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
623 isl_reordering_free(r
);
628 isl_reordering_free(r
);
632 /* Reorder the dimensions of the domain of "aff" according
633 * to the given reordering.
635 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
636 __isl_take isl_reordering
*r
)
638 aff
= isl_aff_cow(aff
);
642 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
643 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
644 aff
->ls
->div
->n_row
);
645 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
647 if (!aff
->v
|| !aff
->ls
)
648 return isl_aff_free(aff
);
653 isl_reordering_free(r
);
657 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
658 __isl_take isl_space
*model
)
660 isl_bool equal_params
;
665 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, model
);
666 if (equal_params
< 0)
671 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
672 exp
= isl_reordering_extend_space(exp
,
673 isl_aff_get_domain_space(aff
));
674 aff
= isl_aff_realign_domain(aff
, exp
);
677 isl_space_free(model
);
680 isl_space_free(model
);
687 #include "isl_unbind_params_templ.c"
689 /* Is "aff" obviously equal to zero?
691 * If the denominator is zero, then "aff" is not equal to zero.
693 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
698 return isl_bool_error
;
700 if (isl_int_is_zero(aff
->v
->el
[0]))
701 return isl_bool_false
;
702 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
703 return isl_bool_ok(pos
< 0);
706 /* Does "aff" represent NaN?
708 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
711 return isl_bool_error
;
713 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
716 /* Are "aff1" and "aff2" obviously equal?
718 * NaN is not equal to anything, not even to another NaN.
720 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
721 __isl_keep isl_aff
*aff2
)
726 return isl_bool_error
;
728 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
729 return isl_bool_false
;
731 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
732 if (equal
< 0 || !equal
)
735 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
738 /* Return the common denominator of "aff" in "v".
740 * We cannot return anything meaningful in case of a NaN.
742 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
745 return isl_stat_error
;
746 if (isl_aff_is_nan(aff
))
747 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
748 "cannot get denominator of NaN", return isl_stat_error
);
749 isl_int_set(*v
, aff
->v
->el
[0]);
753 /* Return the common denominator of "aff".
755 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
762 ctx
= isl_aff_get_ctx(aff
);
763 if (isl_aff_is_nan(aff
))
764 return isl_val_nan(ctx
);
765 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
768 /* Return the constant term of "aff".
770 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
778 ctx
= isl_aff_get_ctx(aff
);
779 if (isl_aff_is_nan(aff
))
780 return isl_val_nan(ctx
);
781 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
782 return isl_val_normalize(v
);
785 /* Return the coefficient of the variable of type "type" at position "pos"
788 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
789 enum isl_dim_type type
, int pos
)
797 ctx
= isl_aff_get_ctx(aff
);
798 if (type
== isl_dim_out
)
799 isl_die(ctx
, isl_error_invalid
,
800 "output/set dimension does not have a coefficient",
802 if (type
== isl_dim_in
)
805 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
808 if (isl_aff_is_nan(aff
))
809 return isl_val_nan(ctx
);
810 pos
+= isl_local_space_offset(aff
->ls
, type
);
811 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
812 return isl_val_normalize(v
);
815 /* Return the sign of the coefficient of the variable of type "type"
816 * at position "pos" of "aff".
818 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
826 ctx
= isl_aff_get_ctx(aff
);
827 if (type
== isl_dim_out
)
828 isl_die(ctx
, isl_error_invalid
,
829 "output/set dimension does not have a coefficient",
831 if (type
== isl_dim_in
)
834 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
837 pos
+= isl_local_space_offset(aff
->ls
, type
);
838 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
841 /* Replace the numerator of the constant term of "aff" by "v".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
849 if (isl_aff_is_nan(aff
))
851 aff
= isl_aff_cow(aff
);
855 aff
->v
= isl_vec_cow(aff
->v
);
857 return isl_aff_free(aff
);
859 isl_int_set(aff
->v
->el
[1], v
);
864 /* Replace the constant term of "aff" by "v".
866 * A NaN is unaffected by this operation.
868 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
869 __isl_take isl_val
*v
)
874 if (isl_aff_is_nan(aff
)) {
879 if (!isl_val_is_rat(v
))
880 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
881 "expecting rational value", goto error
);
883 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
884 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
889 aff
= isl_aff_cow(aff
);
892 aff
->v
= isl_vec_cow(aff
->v
);
896 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
897 isl_int_set(aff
->v
->el
[1], v
->n
);
898 } else if (isl_int_is_one(v
->d
)) {
899 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
901 isl_seq_scale(aff
->v
->el
+ 1,
902 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
903 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
904 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
905 aff
->v
= isl_vec_normalize(aff
->v
);
918 /* Add "v" to the constant term of "aff".
920 * A NaN is unaffected by this operation.
922 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
924 if (isl_int_is_zero(v
))
929 if (isl_aff_is_nan(aff
))
931 aff
= isl_aff_cow(aff
);
935 aff
->v
= isl_vec_cow(aff
->v
);
937 return isl_aff_free(aff
);
939 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
944 /* Add "v" to the constant term of "aff",
945 * in case "aff" is a rational expression.
947 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
948 __isl_take isl_val
*v
)
950 aff
= isl_aff_cow(aff
);
954 aff
->v
= isl_vec_cow(aff
->v
);
958 if (isl_int_is_one(v
->d
)) {
959 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
960 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
961 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
962 aff
->v
= isl_vec_normalize(aff
->v
);
966 isl_seq_scale(aff
->v
->el
+ 1,
967 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
968 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
969 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
970 aff
->v
= isl_vec_normalize(aff
->v
);
983 /* Return the first argument and free the second.
985 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
986 __isl_take isl_val
*v
)
992 /* Replace the first argument by NaN and free the second argument.
994 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
995 __isl_take isl_val
*v
)
998 return isl_aff_set_nan(aff
);
1001 /* Add "v" to the constant term of "aff".
1003 * A NaN is unaffected by this operation.
1004 * Conversely, adding a NaN turns "aff" into a NaN.
1006 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
1007 __isl_take isl_val
*v
)
1009 isl_bool is_nan
, is_zero
, is_rat
;
1011 is_nan
= isl_aff_is_nan(aff
);
1012 is_zero
= isl_val_is_zero(v
);
1013 if (is_nan
< 0 || is_zero
< 0)
1015 if (is_nan
|| is_zero
)
1016 return pick_free(aff
, v
);
1018 is_nan
= isl_val_is_nan(v
);
1019 is_rat
= isl_val_is_rat(v
);
1020 if (is_nan
< 0 || is_rat
< 0)
1023 return set_nan_free_val(aff
, v
);
1025 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1026 "expecting rational value or NaN", goto error
);
1028 return isl_aff_add_rat_constant_val(aff
, v
);
1035 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1040 isl_int_set_si(t
, v
);
1041 aff
= isl_aff_add_constant(aff
, t
);
1047 /* Add "v" to the numerator of the constant term of "aff".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1053 if (isl_int_is_zero(v
))
1058 if (isl_aff_is_nan(aff
))
1060 aff
= isl_aff_cow(aff
);
1064 aff
->v
= isl_vec_cow(aff
->v
);
1066 return isl_aff_free(aff
);
1068 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1073 /* Add "v" to the numerator of the constant term of "aff".
1075 * A NaN is unaffected by this operation.
1077 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1085 isl_int_set_si(t
, v
);
1086 aff
= isl_aff_add_constant_num(aff
, t
);
1092 /* Replace the numerator of the constant term of "aff" by "v".
1094 * A NaN is unaffected by this operation.
1096 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1100 if (isl_aff_is_nan(aff
))
1102 aff
= isl_aff_cow(aff
);
1106 aff
->v
= isl_vec_cow(aff
->v
);
1108 return isl_aff_free(aff
);
1110 isl_int_set_si(aff
->v
->el
[1], v
);
1115 /* Replace the numerator of the coefficient of the variable of type "type"
1116 * at position "pos" of "aff" by "v".
1118 * A NaN is unaffected by this operation.
1120 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1121 enum isl_dim_type type
, int pos
, isl_int v
)
1126 if (type
== isl_dim_out
)
1127 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1128 "output/set dimension does not have a coefficient",
1129 return isl_aff_free(aff
));
1130 if (type
== isl_dim_in
)
1133 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1134 return isl_aff_free(aff
);
1136 if (isl_aff_is_nan(aff
))
1138 aff
= isl_aff_cow(aff
);
1142 aff
->v
= isl_vec_cow(aff
->v
);
1144 return isl_aff_free(aff
);
1146 pos
+= isl_local_space_offset(aff
->ls
, type
);
1147 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1152 /* Replace the numerator of the coefficient of the variable of type "type"
1153 * at position "pos" of "aff" by "v".
1155 * A NaN is unaffected by this operation.
1157 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1158 enum isl_dim_type type
, int pos
, int v
)
1163 if (type
== isl_dim_out
)
1164 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1165 "output/set dimension does not have a coefficient",
1166 return isl_aff_free(aff
));
1167 if (type
== isl_dim_in
)
1170 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1171 return isl_aff_free(aff
);
1173 if (isl_aff_is_nan(aff
))
1175 pos
+= isl_local_space_offset(aff
->ls
, type
);
1176 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1179 aff
= isl_aff_cow(aff
);
1183 aff
->v
= isl_vec_cow(aff
->v
);
1185 return isl_aff_free(aff
);
1187 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1192 /* Replace the coefficient of the variable of type "type" at position "pos"
1195 * A NaN is unaffected by this operation.
1197 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1198 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1203 if (type
== isl_dim_out
)
1204 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1205 "output/set dimension does not have a coefficient",
1207 if (type
== isl_dim_in
)
1210 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1211 return isl_aff_free(aff
);
1213 if (isl_aff_is_nan(aff
)) {
1217 if (!isl_val_is_rat(v
))
1218 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1219 "expecting rational value", goto error
);
1221 pos
+= isl_local_space_offset(aff
->ls
, type
);
1222 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1223 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1228 aff
= isl_aff_cow(aff
);
1231 aff
->v
= isl_vec_cow(aff
->v
);
1235 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1236 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1237 } else if (isl_int_is_one(v
->d
)) {
1238 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1240 isl_seq_scale(aff
->v
->el
+ 1,
1241 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1242 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1243 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1244 aff
->v
= isl_vec_normalize(aff
->v
);
1257 /* Add "v" to the coefficient of the variable of type "type"
1258 * at position "pos" of "aff".
1260 * A NaN is unaffected by this operation.
1262 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1263 enum isl_dim_type type
, int pos
, isl_int v
)
1268 if (type
== isl_dim_out
)
1269 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1270 "output/set dimension does not have a coefficient",
1271 return isl_aff_free(aff
));
1272 if (type
== isl_dim_in
)
1275 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1276 return isl_aff_free(aff
);
1278 if (isl_aff_is_nan(aff
))
1280 aff
= isl_aff_cow(aff
);
1284 aff
->v
= isl_vec_cow(aff
->v
);
1286 return isl_aff_free(aff
);
1288 pos
+= isl_local_space_offset(aff
->ls
, type
);
1289 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1294 /* Add "v" to the coefficient of the variable of type "type"
1295 * at position "pos" of "aff".
1297 * A NaN is unaffected by this operation.
1299 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1300 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1305 if (isl_val_is_zero(v
)) {
1310 if (type
== isl_dim_out
)
1311 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1312 "output/set dimension does not have a coefficient",
1314 if (type
== isl_dim_in
)
1317 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1320 if (isl_aff_is_nan(aff
)) {
1324 if (!isl_val_is_rat(v
))
1325 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1326 "expecting rational value", goto error
);
1328 aff
= isl_aff_cow(aff
);
1332 aff
->v
= isl_vec_cow(aff
->v
);
1336 pos
+= isl_local_space_offset(aff
->ls
, type
);
1337 if (isl_int_is_one(v
->d
)) {
1338 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1339 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1340 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1341 aff
->v
= isl_vec_normalize(aff
->v
);
1345 isl_seq_scale(aff
->v
->el
+ 1,
1346 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1347 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1348 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1349 aff
->v
= isl_vec_normalize(aff
->v
);
1362 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1363 enum isl_dim_type type
, int pos
, int v
)
1368 isl_int_set_si(t
, v
);
1369 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1375 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1380 return isl_local_space_get_div(aff
->ls
, pos
);
1383 /* Return the negation of "aff".
1385 * As a special case, -NaN = NaN.
1387 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1391 if (isl_aff_is_nan(aff
))
1393 aff
= isl_aff_cow(aff
);
1396 aff
->v
= isl_vec_cow(aff
->v
);
1398 return isl_aff_free(aff
);
1400 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1405 /* Remove divs from the local space that do not appear in the affine
1407 * We currently only remove divs at the end.
1408 * Some intermediate divs may also not appear directly in the affine
1409 * expression, but we would also need to check that no other divs are
1410 * defined in terms of them.
1412 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1418 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1419 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1420 if (n
< 0 || off
< 0)
1421 return isl_aff_free(aff
);
1423 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1427 aff
= isl_aff_cow(aff
);
1431 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1432 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1433 if (!aff
->ls
|| !aff
->v
)
1434 return isl_aff_free(aff
);
1439 /* Look for any divs in the aff->ls with a denominator equal to one
1440 * and plug them into the affine expression and any subsequent divs
1441 * that may reference the div.
1443 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1450 isl_local_space
*ls
;
1453 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1454 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1455 if (n
< 0 || off
< 0)
1456 return isl_aff_free(aff
);
1458 for (i
= 0; i
< n
; ++i
) {
1459 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1461 ls
= isl_local_space_copy(aff
->ls
);
1462 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1463 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1464 vec
= isl_vec_copy(aff
->v
);
1465 vec
= isl_vec_cow(vec
);
1471 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1476 isl_vec_free(aff
->v
);
1478 isl_local_space_free(aff
->ls
);
1485 isl_local_space_free(ls
);
1486 return isl_aff_free(aff
);
1489 /* Look for any divs j that appear with a unit coefficient inside
1490 * the definitions of other divs i and plug them into the definitions
1493 * In particular, an expression of the form
1495 * floor((f(..) + floor(g(..)/n))/m)
1499 * floor((n * f(..) + g(..))/(n * m))
1501 * This simplification is correct because we can move the expression
1502 * f(..) into the inner floor in the original expression to obtain
1504 * floor(floor((n * f(..) + g(..))/n)/m)
1506 * from which we can derive the simplified expression.
1508 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1514 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1515 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1516 if (n
< 0 || off
< 0)
1517 return isl_aff_free(aff
);
1518 for (i
= 1; i
< n
; ++i
) {
1519 for (j
= 0; j
< i
; ++j
) {
1520 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1522 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1523 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1524 aff
->v
->size
, i
, 1);
1526 return isl_aff_free(aff
);
1533 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1535 * Even though this function is only called on isl_affs with a single
1536 * reference, we are careful to only change aff->v and aff->ls together.
1538 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1540 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1541 isl_local_space
*ls
;
1545 return isl_aff_free(aff
);
1547 ls
= isl_local_space_copy(aff
->ls
);
1548 ls
= isl_local_space_swap_div(ls
, a
, b
);
1549 v
= isl_vec_copy(aff
->v
);
1554 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1555 isl_vec_free(aff
->v
);
1557 isl_local_space_free(aff
->ls
);
1563 isl_local_space_free(ls
);
1564 return isl_aff_free(aff
);
1567 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1569 * We currently do not actually remove div "b", but simply add its
1570 * coefficient to that of "a" and then zero it out.
1572 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1574 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1577 return isl_aff_free(aff
);
1579 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1582 aff
->v
= isl_vec_cow(aff
->v
);
1584 return isl_aff_free(aff
);
1586 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1587 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1588 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1593 /* Sort the divs in the local space of "aff" according to
1594 * the comparison function "cmp_row" in isl_local_space.c,
1595 * combining the coefficients of identical divs.
1597 * Reordering divs does not change the semantics of "aff",
1598 * so there is no need to call isl_aff_cow.
1599 * Moreover, this function is currently only called on isl_affs
1600 * with a single reference.
1602 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1607 n
= isl_aff_dim(aff
, isl_dim_div
);
1609 return isl_aff_free(aff
);
1610 for (i
= 1; i
< n
; ++i
) {
1611 for (j
= i
- 1; j
>= 0; --j
) {
1612 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1616 aff
= merge_divs(aff
, j
, j
+ 1);
1618 aff
= swap_div(aff
, j
, j
+ 1);
1627 /* Normalize the representation of "aff".
1629 * This function should only be called on "new" isl_affs, i.e.,
1630 * with only a single reference. We therefore do not need to
1631 * worry about affecting other instances.
1633 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1637 aff
->v
= isl_vec_normalize(aff
->v
);
1639 return isl_aff_free(aff
);
1640 aff
= plug_in_integral_divs(aff
);
1641 aff
= plug_in_unit_divs(aff
);
1642 aff
= sort_divs(aff
);
1643 aff
= isl_aff_remove_unused_divs(aff
);
1647 /* Given f, return floor(f).
1648 * If f is an integer expression, then just return f.
1649 * If f is a constant, then return the constant floor(f).
1650 * Otherwise, if f = g/m, write g = q m + r,
1651 * create a new div d = [r/m] and return the expression q + d.
1652 * The coefficients in r are taken to lie between -m/2 and m/2.
1654 * reduce_div_coefficients performs the same normalization.
1656 * As a special case, floor(NaN) = NaN.
1658 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1668 if (isl_aff_is_nan(aff
))
1670 if (isl_int_is_one(aff
->v
->el
[0]))
1673 aff
= isl_aff_cow(aff
);
1677 aff
->v
= isl_vec_cow(aff
->v
);
1679 return isl_aff_free(aff
);
1681 if (isl_aff_is_cst(aff
)) {
1682 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1683 isl_int_set_si(aff
->v
->el
[0], 1);
1687 div
= isl_vec_copy(aff
->v
);
1688 div
= isl_vec_cow(div
);
1690 return isl_aff_free(aff
);
1692 ctx
= isl_aff_get_ctx(aff
);
1693 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1694 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1695 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1696 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1697 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1698 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1699 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1703 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1705 return isl_aff_free(aff
);
1707 size
= aff
->v
->size
;
1708 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1710 return isl_aff_free(aff
);
1711 isl_int_set_si(aff
->v
->el
[0], 1);
1712 isl_int_set_si(aff
->v
->el
[size
], 1);
1714 aff
= isl_aff_normalize(aff
);
1721 * aff mod m = aff - m * floor(aff/m)
1723 * with m an integer value.
1725 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1726 __isl_take isl_val
*m
)
1733 if (!isl_val_is_int(m
))
1734 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1735 "expecting integer modulo", goto error
);
1737 res
= isl_aff_copy(aff
);
1738 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1739 aff
= isl_aff_floor(aff
);
1740 aff
= isl_aff_scale_val(aff
, m
);
1741 res
= isl_aff_sub(res
, aff
);
1752 * pwaff mod m = pwaff - m * floor(pwaff/m)
1754 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1758 res
= isl_pw_aff_copy(pwaff
);
1759 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1760 pwaff
= isl_pw_aff_floor(pwaff
);
1761 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1762 res
= isl_pw_aff_sub(res
, pwaff
);
1769 * pa mod m = pa - m * floor(pa/m)
1771 * with m an integer value.
1773 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1774 __isl_take isl_val
*m
)
1778 if (!isl_val_is_int(m
))
1779 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1780 "expecting integer modulo", goto error
);
1781 pa
= isl_pw_aff_mod(pa
, m
->n
);
1785 isl_pw_aff_free(pa
);
1790 /* Given f, return ceil(f).
1791 * If f is an integer expression, then just return f.
1792 * Otherwise, let f be the expression
1798 * floor((e + m - 1)/m)
1800 * As a special case, ceil(NaN) = NaN.
1802 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1807 if (isl_aff_is_nan(aff
))
1809 if (isl_int_is_one(aff
->v
->el
[0]))
1812 aff
= isl_aff_cow(aff
);
1815 aff
->v
= isl_vec_cow(aff
->v
);
1817 return isl_aff_free(aff
);
1819 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1820 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1821 aff
= isl_aff_floor(aff
);
1826 /* Apply the expansion computed by isl_merge_divs.
1827 * The expansion itself is given by "exp" while the resulting
1828 * list of divs is given by "div".
1830 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1831 __isl_take isl_mat
*div
, int *exp
)
1837 aff
= isl_aff_cow(aff
);
1839 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1840 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1841 new_n_div
= isl_mat_rows(div
);
1842 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1845 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1846 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1847 if (!aff
->v
|| !aff
->ls
)
1848 return isl_aff_free(aff
);
1856 /* Add two affine expressions that live in the same local space.
1858 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1859 __isl_take isl_aff
*aff2
)
1863 aff1
= isl_aff_cow(aff1
);
1867 aff1
->v
= isl_vec_cow(aff1
->v
);
1873 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1874 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1875 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1876 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1877 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1878 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1879 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1884 aff1
= isl_aff_normalize(aff1
);
1892 /* Replace one of the arguments by a NaN and free the other one.
1894 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1895 __isl_take isl_aff
*aff2
)
1898 return isl_aff_set_nan(aff1
);
1901 /* Return the sum of "aff1" and "aff2".
1903 * If either of the two is NaN, then the result is NaN.
1905 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1906 __isl_take isl_aff
*aff2
)
1912 isl_size n_div1
, n_div2
;
1917 ctx
= isl_aff_get_ctx(aff1
);
1918 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1919 isl_die(ctx
, isl_error_invalid
,
1920 "spaces don't match", goto error
);
1922 if (isl_aff_is_nan(aff1
)) {
1926 if (isl_aff_is_nan(aff2
)) {
1931 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1932 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1933 if (n_div1
< 0 || n_div2
< 0)
1935 if (n_div1
== 0 && n_div2
== 0)
1936 return add_expanded(aff1
, aff2
);
1938 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1939 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1940 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1943 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1944 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1945 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1949 return add_expanded(aff1
, aff2
);
1958 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1959 __isl_take isl_aff
*aff2
)
1961 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1964 /* Return the result of scaling "aff" by a factor of "f".
1966 * As a special case, f * NaN = NaN.
1968 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1974 if (isl_aff_is_nan(aff
))
1977 if (isl_int_is_one(f
))
1980 aff
= isl_aff_cow(aff
);
1983 aff
->v
= isl_vec_cow(aff
->v
);
1985 return isl_aff_free(aff
);
1987 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1988 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1993 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1994 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1995 isl_int_divexact(gcd
, f
, gcd
);
1996 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2002 /* Multiple "aff" by "v".
2004 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
2005 __isl_take isl_val
*v
)
2010 if (isl_val_is_one(v
)) {
2015 if (!isl_val_is_rat(v
))
2016 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2017 "expecting rational factor", goto error
);
2019 aff
= isl_aff_scale(aff
, v
->n
);
2020 aff
= isl_aff_scale_down(aff
, v
->d
);
2030 /* Return the result of scaling "aff" down by a factor of "f".
2032 * As a special case, NaN/f = NaN.
2034 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2040 if (isl_aff_is_nan(aff
))
2043 if (isl_int_is_one(f
))
2046 aff
= isl_aff_cow(aff
);
2050 if (isl_int_is_zero(f
))
2051 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2052 "cannot scale down by zero", return isl_aff_free(aff
));
2054 aff
->v
= isl_vec_cow(aff
->v
);
2056 return isl_aff_free(aff
);
2059 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2060 isl_int_gcd(gcd
, gcd
, f
);
2061 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2062 isl_int_divexact(gcd
, f
, gcd
);
2063 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2069 /* Divide "aff" by "v".
2071 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2072 __isl_take isl_val
*v
)
2077 if (isl_val_is_one(v
)) {
2082 if (!isl_val_is_rat(v
))
2083 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2084 "expecting rational factor", goto error
);
2085 if (!isl_val_is_pos(v
))
2086 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2087 "factor needs to be positive", goto error
);
2089 aff
= isl_aff_scale(aff
, v
->d
);
2090 aff
= isl_aff_scale_down(aff
, v
->n
);
2100 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2108 isl_int_set_ui(v
, f
);
2109 aff
= isl_aff_scale_down(aff
, v
);
2115 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2116 enum isl_dim_type type
, unsigned pos
, const char *s
)
2118 aff
= isl_aff_cow(aff
);
2121 if (type
== isl_dim_out
)
2122 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2123 "cannot set name of output/set dimension",
2124 return isl_aff_free(aff
));
2125 if (type
== isl_dim_in
)
2127 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2129 return isl_aff_free(aff
);
2134 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2135 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2137 aff
= isl_aff_cow(aff
);
2140 if (type
== isl_dim_out
)
2141 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2142 "cannot set name of output/set dimension",
2144 if (type
== isl_dim_in
)
2146 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2148 return isl_aff_free(aff
);
2157 /* Replace the identifier of the input tuple of "aff" by "id".
2158 * type is currently required to be equal to isl_dim_in
2160 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2161 enum isl_dim_type type
, __isl_take isl_id
*id
)
2163 aff
= isl_aff_cow(aff
);
2166 if (type
!= isl_dim_in
)
2167 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2168 "cannot only set id of input tuple", goto error
);
2169 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2171 return isl_aff_free(aff
);
2180 /* Exploit the equalities in "eq" to simplify the affine expression
2181 * and the expressions of the integer divisions in the local space.
2182 * The integer divisions in this local space are assumed to appear
2183 * as regular dimensions in "eq".
2185 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2186 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2194 if (eq
->n_eq
== 0) {
2195 isl_basic_set_free(eq
);
2199 aff
= isl_aff_cow(aff
);
2203 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2204 isl_basic_set_copy(eq
));
2205 aff
->v
= isl_vec_cow(aff
->v
);
2206 if (!aff
->ls
|| !aff
->v
)
2209 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2211 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2212 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2213 if (j
< 0 || j
== 0 || j
>= o_div
)
2216 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2220 isl_basic_set_free(eq
);
2221 aff
= isl_aff_normalize(aff
);
2224 isl_basic_set_free(eq
);
2229 /* Exploit the equalities in "eq" to simplify the affine expression
2230 * and the expressions of the integer divisions in the local space.
2232 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2233 __isl_take isl_basic_set
*eq
)
2237 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2241 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2242 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2244 isl_basic_set_free(eq
);
2249 /* Look for equalities among the variables shared by context and aff
2250 * and the integer divisions of aff, if any.
2251 * The equalities are then used to eliminate coefficients and/or integer
2252 * divisions from aff.
2254 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2255 __isl_take isl_set
*context
)
2257 isl_local_space
*ls
;
2258 isl_basic_set
*hull
;
2260 ls
= isl_aff_get_domain_local_space(aff
);
2261 context
= isl_local_space_lift_set(ls
, context
);
2263 hull
= isl_set_affine_hull(context
);
2264 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2267 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2268 __isl_take isl_set
*context
)
2270 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2271 dom_context
= isl_set_intersect_params(dom_context
, context
);
2272 return isl_aff_gist(aff
, dom_context
);
2275 /* Return a basic set containing those elements in the space
2276 * of aff where it is positive. "rational" should not be set.
2278 * If "aff" is NaN, then it is not positive.
2280 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2281 int rational
, void *user
)
2283 isl_constraint
*ineq
;
2284 isl_basic_set
*bset
;
2289 if (isl_aff_is_nan(aff
)) {
2290 isl_space
*space
= isl_aff_get_domain_space(aff
);
2292 return isl_basic_set_empty(space
);
2295 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2296 "rational sets not supported", goto error
);
2298 ineq
= isl_inequality_from_aff(aff
);
2299 c
= isl_constraint_get_constant_val(ineq
);
2300 c
= isl_val_sub_ui(c
, 1);
2301 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2303 bset
= isl_basic_set_from_constraint(ineq
);
2304 bset
= isl_basic_set_simplify(bset
);
2311 /* Return a basic set containing those elements in the space
2312 * of aff where it is non-negative.
2313 * If "rational" is set, then return a rational basic set.
2315 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2317 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2318 __isl_take isl_aff
*aff
, int rational
, void *user
)
2320 isl_constraint
*ineq
;
2321 isl_basic_set
*bset
;
2325 if (isl_aff_is_nan(aff
)) {
2326 isl_space
*space
= isl_aff_get_domain_space(aff
);
2328 return isl_basic_set_empty(space
);
2331 ineq
= isl_inequality_from_aff(aff
);
2333 bset
= isl_basic_set_from_constraint(ineq
);
2335 bset
= isl_basic_set_set_rational(bset
);
2336 bset
= isl_basic_set_simplify(bset
);
2340 /* Return a basic set containing those elements in the space
2341 * of aff where it is non-negative.
2343 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2345 return aff_nonneg_basic_set(aff
, 0, NULL
);
2348 /* Return a basic set containing those elements in the domain space
2349 * of "aff" where it is positive.
2351 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2353 aff
= isl_aff_add_constant_num_si(aff
, -1);
2354 return isl_aff_nonneg_basic_set(aff
);
2357 /* Return a basic set containing those elements in the domain space
2358 * of aff where it is negative.
2360 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2362 aff
= isl_aff_neg(aff
);
2363 return isl_aff_pos_basic_set(aff
);
2366 /* Return a basic set containing those elements in the space
2367 * of aff where it is zero.
2368 * If "rational" is set, then return a rational basic set.
2370 * If "aff" is NaN, then it is not zero.
2372 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2373 int rational
, void *user
)
2375 isl_constraint
*ineq
;
2376 isl_basic_set
*bset
;
2380 if (isl_aff_is_nan(aff
)) {
2381 isl_space
*space
= isl_aff_get_domain_space(aff
);
2383 return isl_basic_set_empty(space
);
2386 ineq
= isl_equality_from_aff(aff
);
2388 bset
= isl_basic_set_from_constraint(ineq
);
2390 bset
= isl_basic_set_set_rational(bset
);
2391 bset
= isl_basic_set_simplify(bset
);
2395 /* Return a basic set containing those elements in the space
2396 * of aff where it is zero.
2398 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2400 return aff_zero_basic_set(aff
, 0, NULL
);
2403 /* Return a basic set containing those elements in the shared space
2404 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2406 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2407 __isl_take isl_aff
*aff2
)
2409 aff1
= isl_aff_sub(aff1
, aff2
);
2411 return isl_aff_nonneg_basic_set(aff1
);
2414 /* Return a basic set containing those elements in the shared domain space
2415 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2417 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2418 __isl_take isl_aff
*aff2
)
2420 aff1
= isl_aff_sub(aff1
, aff2
);
2422 return isl_aff_pos_basic_set(aff1
);
2425 /* Return a set containing those elements in the shared space
2426 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2428 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2429 __isl_take isl_aff
*aff2
)
2431 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2434 /* Return a set containing those elements in the shared domain space
2435 * of aff1 and aff2 where aff1 is greater than aff2.
2437 * If either of the two inputs is NaN, then the result is empty,
2438 * as comparisons with NaN always return false.
2440 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2441 __isl_take isl_aff
*aff2
)
2443 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2446 /* Return a basic set containing those elements in the shared space
2447 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2449 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2450 __isl_take isl_aff
*aff2
)
2452 return isl_aff_ge_basic_set(aff2
, aff1
);
2455 /* Return a basic set containing those elements in the shared domain space
2456 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2458 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2459 __isl_take isl_aff
*aff2
)
2461 return isl_aff_gt_basic_set(aff2
, aff1
);
2464 /* Return a set containing those elements in the shared space
2465 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2467 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2468 __isl_take isl_aff
*aff2
)
2470 return isl_aff_ge_set(aff2
, aff1
);
2473 /* Return a set containing those elements in the shared domain space
2474 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2476 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2477 __isl_take isl_aff
*aff2
)
2479 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2482 /* Return a basic set containing those elements in the shared space
2483 * of aff1 and aff2 where aff1 and aff2 are equal.
2485 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2486 __isl_take isl_aff
*aff2
)
2488 aff1
= isl_aff_sub(aff1
, aff2
);
2490 return isl_aff_zero_basic_set(aff1
);
2493 /* Return a set containing those elements in the shared space
2494 * of aff1 and aff2 where aff1 and aff2 are equal.
2496 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2497 __isl_take isl_aff
*aff2
)
2499 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2502 /* Return a set containing those elements in the shared domain space
2503 * of aff1 and aff2 where aff1 and aff2 are not equal.
2505 * If either of the two inputs is NaN, then the result is empty,
2506 * as comparisons with NaN always return false.
2508 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2509 __isl_take isl_aff
*aff2
)
2511 isl_set
*set_lt
, *set_gt
;
2513 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2514 isl_aff_copy(aff2
));
2515 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2516 return isl_set_union_disjoint(set_lt
, set_gt
);
2519 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2520 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2522 aff1
= isl_aff_add(aff1
, aff2
);
2523 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2527 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2530 return isl_bool_error
;
2532 return isl_bool_false
;
2536 #define TYPE isl_aff
2538 #include "check_type_range_templ.c"
2540 /* Check whether the given affine expression has non-zero coefficient
2541 * for any dimension in the given range or if any of these dimensions
2542 * appear with non-zero coefficients in any of the integer divisions
2543 * involved in the affine expression.
2545 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2546 enum isl_dim_type type
, unsigned first
, unsigned n
)
2550 isl_bool involves
= isl_bool_false
;
2553 return isl_bool_error
;
2555 return isl_bool_false
;
2556 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2557 return isl_bool_error
;
2559 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2563 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2564 for (i
= 0; i
< n
; ++i
)
2565 if (active
[first
+ i
]) {
2566 involves
= isl_bool_true
;
2575 return isl_bool_error
;
2578 /* Does "aff" involve any local variables, i.e., integer divisions?
2580 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2584 n
= isl_aff_dim(aff
, isl_dim_div
);
2586 return isl_bool_error
;
2587 return isl_bool_ok(n
> 0);
2590 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2591 enum isl_dim_type type
, unsigned first
, unsigned n
)
2597 if (type
== isl_dim_out
)
2598 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2599 "cannot drop output/set dimension",
2600 return isl_aff_free(aff
));
2601 if (type
== isl_dim_in
)
2603 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2606 ctx
= isl_aff_get_ctx(aff
);
2607 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2608 return isl_aff_free(aff
);
2610 aff
= isl_aff_cow(aff
);
2614 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2616 return isl_aff_free(aff
);
2618 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2619 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2621 return isl_aff_free(aff
);
2626 /* Is the domain of "aff" a product?
2628 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2630 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2634 #define TYPE isl_aff
2635 #include <isl_domain_factor_templ.c>
2637 /* Project the domain of the affine expression onto its parameter space.
2638 * The affine expression may not involve any of the domain dimensions.
2640 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2645 n
= isl_aff_dim(aff
, isl_dim_in
);
2647 return isl_aff_free(aff
);
2648 aff
= isl_aff_drop_domain(aff
, 0, n
);
2649 space
= isl_aff_get_domain_space(aff
);
2650 space
= isl_space_params(space
);
2651 aff
= isl_aff_reset_domain_space(aff
, space
);
2655 /* Convert an affine expression defined over a parameter domain
2656 * into one that is defined over a zero-dimensional set.
2658 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2660 isl_local_space
*ls
;
2662 ls
= isl_aff_take_domain_local_space(aff
);
2663 ls
= isl_local_space_set_from_params(ls
);
2664 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2669 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2670 enum isl_dim_type type
, unsigned first
, unsigned n
)
2676 if (type
== isl_dim_out
)
2677 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2678 "cannot insert output/set dimensions",
2679 return isl_aff_free(aff
));
2680 if (type
== isl_dim_in
)
2682 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2685 ctx
= isl_aff_get_ctx(aff
);
2686 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2687 return isl_aff_free(aff
);
2689 aff
= isl_aff_cow(aff
);
2693 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2695 return isl_aff_free(aff
);
2697 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2698 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2700 return isl_aff_free(aff
);
2705 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2706 enum isl_dim_type type
, unsigned n
)
2710 pos
= isl_aff_dim(aff
, type
);
2712 return isl_aff_free(aff
);
2714 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2717 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2718 * to dimensions of "dst_type" at "dst_pos".
2720 * We only support moving input dimensions to parameters and vice versa.
2722 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2723 enum isl_dim_type dst_type
, unsigned dst_pos
,
2724 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2728 isl_size src_off
, dst_off
;
2733 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2734 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2737 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2738 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2739 "cannot move output/set dimension",
2740 return isl_aff_free(aff
));
2741 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2742 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2743 "cannot move divs", return isl_aff_free(aff
));
2744 if (dst_type
== isl_dim_in
)
2745 dst_type
= isl_dim_set
;
2746 if (src_type
== isl_dim_in
)
2747 src_type
= isl_dim_set
;
2749 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2750 return isl_aff_free(aff
);
2751 if (dst_type
== src_type
)
2752 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2753 "moving dims within the same type not supported",
2754 return isl_aff_free(aff
));
2756 aff
= isl_aff_cow(aff
);
2757 src_off
= isl_aff_domain_offset(aff
, src_type
);
2758 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2759 if (src_off
< 0 || dst_off
< 0)
2760 return isl_aff_free(aff
);
2762 g_src_pos
= 1 + src_off
+ src_pos
;
2763 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2764 if (dst_type
> src_type
)
2767 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2768 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2769 src_type
, src_pos
, n
);
2770 if (!aff
->v
|| !aff
->ls
)
2771 return isl_aff_free(aff
);
2773 aff
= sort_divs(aff
);
2778 /* Return a zero isl_aff in the given space.
2780 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2781 * interface over all piecewise types.
2783 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2785 isl_local_space
*ls
;
2787 ls
= isl_local_space_from_space(isl_space_domain(space
));
2788 return isl_aff_zero_on_domain(ls
);
2791 #define isl_aff_involves_nan isl_aff_is_nan
2794 #define PW isl_pw_aff
2798 #define EL_IS_ZERO is_empty
2802 #define IS_ZERO is_empty
2805 #undef DEFAULT_IS_ZERO
2806 #define DEFAULT_IS_ZERO 0
2808 #include <isl_pw_templ.c>
2809 #include <isl_pw_add_constant_val_templ.c>
2810 #include <isl_pw_bind_domain_templ.c>
2811 #include <isl_pw_eval.c>
2812 #include <isl_pw_hash.c>
2813 #include <isl_pw_insert_dims_templ.c>
2814 #include <isl_pw_insert_domain_templ.c>
2815 #include <isl_pw_move_dims_templ.c>
2816 #include <isl_pw_neg_templ.c>
2817 #include <isl_pw_pullback_templ.c>
2818 #include <isl_pw_sub_templ.c>
2819 #include <isl_pw_union_opt.c>
2824 #include <isl_union_single.c>
2825 #include <isl_union_neg.c>
2830 #include <isl_union_pw_templ.c>
2832 /* Compute a piecewise quasi-affine expression with a domain that
2833 * is the union of those of pwaff1 and pwaff2 and such that on each
2834 * cell, the quasi-affine expression is the maximum of those of pwaff1
2835 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2836 * cell, then the associated expression is the defined one.
2838 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2839 __isl_take isl_pw_aff
*pwaff2
)
2841 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2842 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2845 /* Compute a piecewise quasi-affine expression with a domain that
2846 * is the union of those of pwaff1 and pwaff2 and such that on each
2847 * cell, the quasi-affine expression is the minimum of those of pwaff1
2848 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2849 * cell, then the associated expression is the defined one.
2851 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2852 __isl_take isl_pw_aff
*pwaff2
)
2854 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2855 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2858 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2859 __isl_take isl_pw_aff
*pwaff2
, int max
)
2862 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2864 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2867 /* Is the domain of "pa" a product?
2869 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2871 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2875 #define TYPE isl_pw_aff
2876 #include <isl_domain_factor_templ.c>
2878 /* Return a set containing those elements in the domain
2879 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2880 * does not satisfy "fn" (if complement is 1).
2882 * The pieces with a NaN never belong to the result since
2883 * NaN does not satisfy any property.
2885 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2886 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2888 int complement
, void *user
)
2896 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2898 for (i
= 0; i
< pwaff
->n
; ++i
) {
2899 isl_basic_set
*bset
;
2900 isl_set
*set_i
, *locus
;
2903 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2906 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2907 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2908 locus
= isl_set_from_basic_set(bset
);
2909 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2911 set_i
= isl_set_subtract(set_i
, locus
);
2913 set_i
= isl_set_intersect(set_i
, locus
);
2914 set
= isl_set_union_disjoint(set
, set_i
);
2917 isl_pw_aff_free(pwaff
);
2922 /* Return a set containing those elements in the domain
2923 * of "pa" where it is positive.
2925 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2927 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2930 /* Return a set containing those elements in the domain
2931 * of pwaff where it is non-negative.
2933 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2935 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2938 /* Return a set containing those elements in the domain
2939 * of pwaff where it is zero.
2941 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2943 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2946 /* Return a set containing those elements in the domain
2947 * of pwaff where it is not zero.
2949 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2951 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
2954 /* Bind the affine function "aff" to the parameter "id",
2955 * returning the elements in the domain where the affine expression
2956 * is equal to the parameter.
2958 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
2959 __isl_take isl_id
*id
)
2964 space
= isl_aff_get_domain_space(aff
);
2965 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
2967 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
2968 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
2970 return isl_aff_eq_basic_set(aff
, aff_id
);
2973 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2974 * "rational" should not be set.
2976 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
2977 int rational
, void *user
)
2984 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2985 "rational binding not supported", goto error
);
2986 return isl_aff_bind_id(aff
, isl_id_copy(id
));
2992 /* Bind the piecewise affine function "pa" to the parameter "id",
2993 * returning the elements in the domain where the expression
2994 * is equal to the parameter.
2996 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
2997 __isl_take isl_id
*id
)
3001 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3007 /* Return a set containing those elements in the shared domain
3008 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3010 * We compute the difference on the shared domain and then construct
3011 * the set of values where this difference is non-negative.
3012 * If strict is set, we first subtract 1 from the difference.
3013 * If equal is set, we only return the elements where pwaff1 and pwaff2
3016 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3017 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3019 isl_set
*set1
, *set2
;
3021 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3022 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3023 set1
= isl_set_intersect(set1
, set2
);
3024 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3025 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3026 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3029 isl_space
*space
= isl_set_get_space(set1
);
3031 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3032 aff
= isl_aff_add_constant_si(aff
, -1);
3033 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3038 return isl_pw_aff_zero_set(pwaff1
);
3039 return isl_pw_aff_nonneg_set(pwaff1
);
3042 /* Return a set containing those elements in the shared domain
3043 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3045 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3046 __isl_take isl_pw_aff
*pwaff2
)
3048 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3049 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3052 /* Return a set containing those elements in the shared domain
3053 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3055 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3056 __isl_take isl_pw_aff
*pwaff2
)
3058 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3059 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3062 /* Return a set containing those elements in the shared domain
3063 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3065 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3066 __isl_take isl_pw_aff
*pwaff2
)
3068 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3069 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3072 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3073 __isl_take isl_pw_aff
*pwaff2
)
3075 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3078 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3079 __isl_take isl_pw_aff
*pwaff2
)
3081 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3084 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3085 * where the function values are ordered in the same way as "order",
3086 * which returns a set in the shared domain of its two arguments.
3088 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3089 * We first pull back the two functions such that they are defined on
3090 * the domain [A -> B]. Then we apply "order", resulting in a set
3091 * in the space [A -> B]. Finally, we unwrap this set to obtain
3092 * a map in the space A -> B.
3094 static __isl_give isl_map
*isl_pw_aff_order_map(
3095 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3096 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3097 __isl_take isl_pw_aff
*pa2
))
3099 isl_space
*space1
, *space2
;
3103 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3104 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3105 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3106 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3107 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3108 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3109 ma
= isl_multi_aff_range_map(space1
);
3110 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3111 set
= order(pa1
, pa2
);
3113 return isl_set_unwrap(set
);
3116 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3117 * where the function values are equal.
3119 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3120 __isl_take isl_pw_aff
*pa2
)
3122 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3125 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3126 * where the function value of "pa1" is less than or equal to
3127 * the function value of "pa2".
3129 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3130 __isl_take isl_pw_aff
*pa2
)
3132 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3135 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3136 * where the function value of "pa1" is less than the function value of "pa2".
3138 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3139 __isl_take isl_pw_aff
*pa2
)
3141 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3144 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3145 * where the function value of "pa1" is greater than or equal to
3146 * the function value of "pa2".
3148 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3149 __isl_take isl_pw_aff
*pa2
)
3151 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3154 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3155 * where the function value of "pa1" is greater than the function value
3158 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3159 __isl_take isl_pw_aff
*pa2
)
3161 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3164 /* Return a set containing those elements in the shared domain
3165 * of the elements of list1 and list2 where each element in list1
3166 * has the relation specified by "fn" with each element in list2.
3168 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3169 __isl_take isl_pw_aff_list
*list2
,
3170 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3171 __isl_take isl_pw_aff
*pwaff2
))
3177 if (!list1
|| !list2
)
3180 ctx
= isl_pw_aff_list_get_ctx(list1
);
3181 if (list1
->n
< 1 || list2
->n
< 1)
3182 isl_die(ctx
, isl_error_invalid
,
3183 "list should contain at least one element", goto error
);
3185 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3186 for (i
= 0; i
< list1
->n
; ++i
)
3187 for (j
= 0; j
< list2
->n
; ++j
) {
3190 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3191 isl_pw_aff_copy(list2
->p
[j
]));
3192 set
= isl_set_intersect(set
, set_ij
);
3195 isl_pw_aff_list_free(list1
);
3196 isl_pw_aff_list_free(list2
);
3199 isl_pw_aff_list_free(list1
);
3200 isl_pw_aff_list_free(list2
);
3204 /* Return a set containing those elements in the shared domain
3205 * of the elements of list1 and list2 where each element in list1
3206 * is equal to each element in list2.
3208 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3209 __isl_take isl_pw_aff_list
*list2
)
3211 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3214 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3215 __isl_take isl_pw_aff_list
*list2
)
3217 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3220 /* Return a set containing those elements in the shared domain
3221 * of the elements of list1 and list2 where each element in list1
3222 * is less than or equal to each element in list2.
3224 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3225 __isl_take isl_pw_aff_list
*list2
)
3227 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3230 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3231 __isl_take isl_pw_aff_list
*list2
)
3233 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3236 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3237 __isl_take isl_pw_aff_list
*list2
)
3239 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3242 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3243 __isl_take isl_pw_aff_list
*list2
)
3245 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3249 /* Return a set containing those elements in the shared domain
3250 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3252 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3253 __isl_take isl_pw_aff
*pwaff2
)
3255 isl_set
*set_lt
, *set_gt
;
3257 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3258 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3259 isl_pw_aff_copy(pwaff2
));
3260 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3261 return isl_set_union_disjoint(set_lt
, set_gt
);
3264 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3269 if (isl_int_is_one(v
))
3271 if (!isl_int_is_pos(v
))
3272 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3273 "factor needs to be positive",
3274 return isl_pw_aff_free(pwaff
));
3275 pwaff
= isl_pw_aff_cow(pwaff
);
3281 for (i
= 0; i
< pwaff
->n
; ++i
) {
3282 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3283 if (!pwaff
->p
[i
].aff
)
3284 return isl_pw_aff_free(pwaff
);
3290 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3294 pwaff
= isl_pw_aff_cow(pwaff
);
3300 for (i
= 0; i
< pwaff
->n
; ++i
) {
3301 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3302 if (!pwaff
->p
[i
].aff
)
3303 return isl_pw_aff_free(pwaff
);
3309 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3313 pwaff
= isl_pw_aff_cow(pwaff
);
3319 for (i
= 0; i
< pwaff
->n
; ++i
) {
3320 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3321 if (!pwaff
->p
[i
].aff
)
3322 return isl_pw_aff_free(pwaff
);
3328 /* Assuming that "cond1" and "cond2" are disjoint,
3329 * return an affine expression that is equal to pwaff1 on cond1
3330 * and to pwaff2 on cond2.
3332 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3333 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3334 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3336 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3337 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3339 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3342 /* Return an affine expression that is equal to pwaff_true for elements
3343 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3345 * That is, return cond ? pwaff_true : pwaff_false;
3347 * If "cond" involves and NaN, then we conservatively return a NaN
3348 * on its entire domain. In principle, we could consider the pieces
3349 * where it is NaN separately from those where it is not.
3351 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3352 * then only use the domain of "cond" to restrict the domain.
3354 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3355 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3357 isl_set
*cond_true
, *cond_false
;
3362 if (isl_pw_aff_involves_nan(cond
)) {
3363 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3364 isl_local_space
*ls
= isl_local_space_from_space(space
);
3365 isl_pw_aff_free(cond
);
3366 isl_pw_aff_free(pwaff_true
);
3367 isl_pw_aff_free(pwaff_false
);
3368 return isl_pw_aff_nan_on_domain(ls
);
3371 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3372 isl_pw_aff_get_space(pwaff_false
));
3373 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3374 isl_pw_aff_get_space(pwaff_true
));
3375 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3381 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3382 isl_pw_aff_free(pwaff_false
);
3383 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3386 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3387 cond_false
= isl_pw_aff_zero_set(cond
);
3388 return isl_pw_aff_select(cond_true
, pwaff_true
,
3389 cond_false
, pwaff_false
);
3391 isl_pw_aff_free(cond
);
3392 isl_pw_aff_free(pwaff_true
);
3393 isl_pw_aff_free(pwaff_false
);
3397 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3402 return isl_bool_error
;
3404 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3405 return isl_bool_ok(pos
== -1);
3408 /* Check whether pwaff is a piecewise constant.
3410 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3415 return isl_bool_error
;
3417 for (i
= 0; i
< pwaff
->n
; ++i
) {
3418 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3419 if (is_cst
< 0 || !is_cst
)
3423 return isl_bool_true
;
3426 /* Return the product of "aff1" and "aff2".
3428 * If either of the two is NaN, then the result is NaN.
3430 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3432 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3433 __isl_take isl_aff
*aff2
)
3438 if (isl_aff_is_nan(aff1
)) {
3442 if (isl_aff_is_nan(aff2
)) {
3447 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3448 return isl_aff_mul(aff2
, aff1
);
3450 if (!isl_aff_is_cst(aff2
))
3451 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3452 "at least one affine expression should be constant",
3455 aff1
= isl_aff_cow(aff1
);
3459 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3460 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3470 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3472 * If either of the two is NaN, then the result is NaN.
3473 * A division by zero also results in NaN.
3475 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3476 __isl_take isl_aff
*aff2
)
3478 isl_bool is_cst
, is_zero
;
3484 if (isl_aff_is_nan(aff1
)) {
3488 if (isl_aff_is_nan(aff2
)) {
3493 is_cst
= isl_aff_is_cst(aff2
);
3497 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3498 "second argument should be a constant", goto error
);
3499 is_zero
= isl_aff_plain_is_zero(aff2
);
3503 return set_nan_free(aff1
, aff2
);
3505 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3507 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3508 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3511 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3512 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3515 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3516 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3527 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3528 __isl_take isl_pw_aff
*pwaff2
)
3530 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3531 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3534 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3535 __isl_take isl_pw_aff
*pwaff2
)
3537 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3540 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3541 __isl_take isl_pw_aff
*pwaff2
)
3543 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3544 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3547 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3549 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3550 __isl_take isl_pw_aff
*pa2
)
3554 is_cst
= isl_pw_aff_is_cst(pa2
);
3558 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3559 "second argument should be a piecewise constant",
3561 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3562 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3564 isl_pw_aff_free(pa1
);
3565 isl_pw_aff_free(pa2
);
3569 /* Compute the quotient of the integer division of "pa1" by "pa2"
3570 * with rounding towards zero.
3571 * "pa2" is assumed to be a piecewise constant.
3573 * In particular, return
3575 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3578 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3579 __isl_take isl_pw_aff
*pa2
)
3585 is_cst
= isl_pw_aff_is_cst(pa2
);
3589 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3590 "second argument should be a piecewise constant",
3593 pa1
= isl_pw_aff_div(pa1
, pa2
);
3595 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3596 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3597 c
= isl_pw_aff_ceil(pa1
);
3598 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3600 isl_pw_aff_free(pa1
);
3601 isl_pw_aff_free(pa2
);
3605 /* Compute the remainder of the integer division of "pa1" by "pa2"
3606 * with rounding towards zero.
3607 * "pa2" is assumed to be a piecewise constant.
3609 * In particular, return
3611 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3614 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3615 __isl_take isl_pw_aff
*pa2
)
3620 is_cst
= isl_pw_aff_is_cst(pa2
);
3624 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3625 "second argument should be a piecewise constant",
3627 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3628 res
= isl_pw_aff_mul(pa2
, res
);
3629 res
= isl_pw_aff_sub(pa1
, res
);
3632 isl_pw_aff_free(pa1
);
3633 isl_pw_aff_free(pa2
);
3637 /* Does either of "pa1" or "pa2" involve any NaN2?
3639 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3640 __isl_keep isl_pw_aff
*pa2
)
3644 has_nan
= isl_pw_aff_involves_nan(pa1
);
3645 if (has_nan
< 0 || has_nan
)
3647 return isl_pw_aff_involves_nan(pa2
);
3650 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3651 * by a NaN on their shared domain.
3653 * In principle, the result could be refined to only being NaN
3654 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3656 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3657 __isl_take isl_pw_aff
*pa2
)
3659 isl_local_space
*ls
;
3663 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3664 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3665 pa
= isl_pw_aff_nan_on_domain(ls
);
3666 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3671 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3672 __isl_take isl_pw_aff
*pwaff2
)
3677 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3678 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3679 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3680 isl_pw_aff_copy(pwaff2
));
3681 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3682 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3685 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3686 __isl_take isl_pw_aff
*pwaff2
)
3691 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3692 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3693 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3694 isl_pw_aff_copy(pwaff2
));
3695 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3696 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3699 /* Return an expression for the minimum (if "max" is not set) or
3700 * the maximum (if "max" is set) of "pa1" and "pa2".
3701 * If either expression involves any NaN, then return a NaN
3702 * on the shared domain as result.
3704 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3705 __isl_take isl_pw_aff
*pa2
, int max
)
3709 has_nan
= either_involves_nan(pa1
, pa2
);
3711 pa1
= isl_pw_aff_free(pa1
);
3713 return replace_by_nan(pa1
, pa2
);
3715 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3717 return pw_aff_max(pa1
, pa2
);
3719 return pw_aff_min(pa1
, pa2
);
3722 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3724 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3725 __isl_take isl_pw_aff
*pwaff2
)
3727 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3730 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3732 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3733 __isl_take isl_pw_aff
*pwaff2
)
3735 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3738 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3739 __isl_take isl_pw_aff_list
*list
,
3740 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3741 __isl_take isl_pw_aff
*pwaff2
))
3750 ctx
= isl_pw_aff_list_get_ctx(list
);
3752 isl_die(ctx
, isl_error_invalid
,
3753 "list should contain at least one element", goto error
);
3755 res
= isl_pw_aff_copy(list
->p
[0]);
3756 for (i
= 1; i
< list
->n
; ++i
)
3757 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3759 isl_pw_aff_list_free(list
);
3762 isl_pw_aff_list_free(list
);
3766 /* Return an isl_pw_aff that maps each element in the intersection of the
3767 * domains of the elements of list to the minimal corresponding affine
3770 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3772 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3775 /* Return an isl_pw_aff that maps each element in the intersection of the
3776 * domains of the elements of list to the maximal corresponding affine
3779 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3781 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3784 /* Mark the domains of "pwaff" as rational.
3786 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3790 pwaff
= isl_pw_aff_cow(pwaff
);
3796 for (i
= 0; i
< pwaff
->n
; ++i
) {
3797 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3798 if (!pwaff
->p
[i
].set
)
3799 return isl_pw_aff_free(pwaff
);
3805 /* Mark the domains of the elements of "list" as rational.
3807 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3808 __isl_take isl_pw_aff_list
*list
)
3818 for (i
= 0; i
< n
; ++i
) {
3821 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3822 pa
= isl_pw_aff_set_rational(pa
);
3823 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3829 /* Do the parameters of "aff" match those of "space"?
3831 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3832 __isl_keep isl_space
*space
)
3834 isl_space
*aff_space
;
3838 return isl_bool_error
;
3840 aff_space
= isl_aff_get_domain_space(aff
);
3842 match
= isl_space_has_equal_params(space
, aff_space
);
3844 isl_space_free(aff_space
);
3848 /* Check that the domain space of "aff" matches "space".
3850 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3851 __isl_keep isl_space
*space
)
3853 isl_space
*aff_space
;
3857 return isl_stat_error
;
3859 aff_space
= isl_aff_get_domain_space(aff
);
3861 match
= isl_space_has_equal_params(space
, aff_space
);
3865 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3866 "parameters don't match", goto error
);
3867 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3868 aff_space
, isl_dim_set
);
3872 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3873 "domains don't match", goto error
);
3874 isl_space_free(aff_space
);
3877 isl_space_free(aff_space
);
3878 return isl_stat_error
;
3881 /* Return the shared (universe) domain of the elements of "ma".
3883 * Since an isl_multi_aff (and an isl_aff) is always total,
3884 * the domain is always the universe set in its domain space.
3885 * This is a helper function for use in the generic isl_multi_*_bind.
3887 static __isl_give isl_basic_set
*isl_multi_aff_domain(
3888 __isl_take isl_multi_aff
*ma
)
3892 space
= isl_multi_aff_get_space(ma
);
3893 isl_multi_aff_free(ma
);
3895 return isl_basic_set_universe(isl_space_domain(space
));
3901 #include <isl_multi_no_explicit_domain.c>
3902 #include <isl_multi_templ.c>
3903 #include <isl_multi_add_constant_templ.c>
3904 #include <isl_multi_apply_set.c>
3905 #include <isl_multi_arith_templ.c>
3906 #include <isl_multi_bind_domain_templ.c>
3907 #include <isl_multi_cmp.c>
3908 #include <isl_multi_dim_id_templ.c>
3909 #include <isl_multi_dims.c>
3910 #include <isl_multi_floor.c>
3911 #include <isl_multi_from_base_templ.c>
3912 #include <isl_multi_identity_templ.c>
3913 #include <isl_multi_insert_domain_templ.c>
3914 #include <isl_multi_locals_templ.c>
3915 #include <isl_multi_move_dims_templ.c>
3916 #include <isl_multi_nan_templ.c>
3917 #include <isl_multi_product_templ.c>
3918 #include <isl_multi_splice_templ.c>
3919 #include <isl_multi_tuple_id_templ.c>
3920 #include <isl_multi_unbind_params_templ.c>
3921 #include <isl_multi_zero_templ.c>
3925 #include <isl_multi_gist.c>
3928 #define DOMBASE basic_set
3929 #include <isl_multi_bind_templ.c>
3931 /* Construct an isl_multi_aff living in "space" that corresponds
3932 * to the affine transformation matrix "mat".
3934 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
3935 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
3938 isl_local_space
*ls
= NULL
;
3939 isl_multi_aff
*ma
= NULL
;
3940 isl_size n_row
, n_col
, n_out
, total
;
3946 ctx
= isl_mat_get_ctx(mat
);
3948 n_row
= isl_mat_rows(mat
);
3949 n_col
= isl_mat_cols(mat
);
3950 n_out
= isl_space_dim(space
, isl_dim_out
);
3951 total
= isl_space_dim(space
, isl_dim_all
);
3952 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
3955 isl_die(ctx
, isl_error_invalid
,
3956 "insufficient number of rows", goto error
);
3958 isl_die(ctx
, isl_error_invalid
,
3959 "insufficient number of columns", goto error
);
3960 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
3961 isl_die(ctx
, isl_error_invalid
,
3962 "dimension mismatch", goto error
);
3964 ma
= isl_multi_aff_zero(isl_space_copy(space
));
3965 space
= isl_space_domain(space
);
3966 ls
= isl_local_space_from_space(isl_space_copy(space
));
3968 for (i
= 0; i
< n_row
- 1; ++i
) {
3972 v
= isl_vec_alloc(ctx
, 1 + n_col
);
3975 isl_int_set(v
->el
[0], mat
->row
[0][0]);
3976 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
3977 v
= isl_vec_normalize(v
);
3978 aff
= isl_aff_alloc_vec(isl_local_space_copy(ls
), v
);
3979 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3982 isl_space_free(space
);
3983 isl_local_space_free(ls
);
3987 isl_space_free(space
);
3988 isl_local_space_free(ls
);
3990 isl_multi_aff_free(ma
);
3994 /* Return the constant terms of the affine expressions of "ma".
3996 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
3997 __isl_keep isl_multi_aff
*ma
)
4004 n
= isl_multi_aff_size(ma
);
4007 space
= isl_space_range(isl_multi_aff_get_space(ma
));
4008 space
= isl_space_drop_all_params(space
);
4009 mv
= isl_multi_val_zero(space
);
4011 for (i
= 0; i
< n
; ++i
) {
4015 aff
= isl_multi_aff_get_at(ma
, i
);
4016 val
= isl_aff_get_constant_val(aff
);
4018 mv
= isl_multi_val_set_at(mv
, i
, val
);
4024 /* Remove any internal structure of the domain of "ma".
4025 * If there is any such internal structure in the input,
4026 * then the name of the corresponding space is also removed.
4028 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4029 __isl_take isl_multi_aff
*ma
)
4036 if (!ma
->space
->nested
[0])
4039 space
= isl_multi_aff_get_space(ma
);
4040 space
= isl_space_flatten_domain(space
);
4041 ma
= isl_multi_aff_reset_space(ma
, space
);
4046 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4047 * of the space to its domain.
4049 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4053 isl_local_space
*ls
;
4058 if (!isl_space_is_map(space
))
4059 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4060 "not a map space", goto error
);
4062 n_in
= isl_space_dim(space
, isl_dim_in
);
4065 space
= isl_space_domain_map(space
);
4067 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4069 isl_space_free(space
);
4073 space
= isl_space_domain(space
);
4074 ls
= isl_local_space_from_space(space
);
4075 for (i
= 0; i
< n_in
; ++i
) {
4078 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4080 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4082 isl_local_space_free(ls
);
4085 isl_space_free(space
);
4089 /* This function performs the same operation as isl_multi_aff_domain_map,
4090 * but is considered as a function on an isl_space when exported.
4092 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4093 __isl_take isl_space
*space
)
4095 return isl_multi_aff_domain_map(space
);
4098 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4099 * of the space to its range.
4101 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4104 isl_size n_in
, n_out
;
4105 isl_local_space
*ls
;
4110 if (!isl_space_is_map(space
))
4111 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4112 "not a map space", goto error
);
4114 n_in
= isl_space_dim(space
, isl_dim_in
);
4115 n_out
= isl_space_dim(space
, isl_dim_out
);
4116 if (n_in
< 0 || n_out
< 0)
4118 space
= isl_space_range_map(space
);
4120 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4122 isl_space_free(space
);
4126 space
= isl_space_domain(space
);
4127 ls
= isl_local_space_from_space(space
);
4128 for (i
= 0; i
< n_out
; ++i
) {
4131 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4132 isl_dim_set
, n_in
+ i
);
4133 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4135 isl_local_space_free(ls
);
4138 isl_space_free(space
);
4142 /* This function performs the same operation as isl_multi_aff_range_map,
4143 * but is considered as a function on an isl_space when exported.
4145 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4146 __isl_take isl_space
*space
)
4148 return isl_multi_aff_range_map(space
);
4151 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4152 * of the space to its domain.
4154 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4155 __isl_take isl_space
*space
)
4157 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4160 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4161 * but is considered as a function on an isl_space when exported.
4163 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4164 __isl_take isl_space
*space
)
4166 return isl_pw_multi_aff_domain_map(space
);
4169 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4170 * of the space to its range.
4172 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4173 __isl_take isl_space
*space
)
4175 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4178 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4179 * but is considered as a function on an isl_space when exported.
4181 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4182 __isl_take isl_space
*space
)
4184 return isl_pw_multi_aff_range_map(space
);
4187 /* Given the space of a set and a range of set dimensions,
4188 * construct an isl_multi_aff that projects out those dimensions.
4190 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4191 __isl_take isl_space
*space
, enum isl_dim_type type
,
4192 unsigned first
, unsigned n
)
4196 isl_local_space
*ls
;
4201 if (!isl_space_is_set(space
))
4202 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4203 "expecting set space", goto error
);
4204 if (type
!= isl_dim_set
)
4205 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4206 "only set dimensions can be projected out", goto error
);
4207 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4210 dim
= isl_space_dim(space
, isl_dim_set
);
4214 space
= isl_space_from_domain(space
);
4215 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4218 return isl_multi_aff_alloc(space
);
4220 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4221 space
= isl_space_domain(space
);
4222 ls
= isl_local_space_from_space(space
);
4224 for (i
= 0; i
< first
; ++i
) {
4227 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4229 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4232 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4235 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4236 isl_dim_set
, first
+ n
+ i
);
4237 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4240 isl_local_space_free(ls
);
4243 isl_space_free(space
);
4247 /* Given the space of a set and a range of set dimensions,
4248 * construct an isl_pw_multi_aff that projects out those dimensions.
4250 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4251 __isl_take isl_space
*space
, enum isl_dim_type type
,
4252 unsigned first
, unsigned n
)
4256 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4257 return isl_pw_multi_aff_from_multi_aff(ma
);
4260 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4261 * but is considered as a function on an isl_multi_aff when exported.
4263 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4264 __isl_take isl_multi_aff
*ma
)
4266 return isl_pw_multi_aff_from_multi_aff(ma
);
4269 /* Create a piecewise multi-affine expression in the given space that maps each
4270 * input dimension to the corresponding output dimension.
4272 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4273 __isl_take isl_space
*space
)
4275 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4278 /* Create a piecewise multi expression that maps elements in the given space
4281 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4282 __isl_take isl_space
*space
)
4286 ma
= isl_multi_aff_identity_on_domain_space(space
);
4287 return isl_pw_multi_aff_from_multi_aff(ma
);
4290 /* This function performs the same operation as
4291 * isl_pw_multi_aff_identity_on_domain_space,
4292 * but is considered as a function on an isl_space when exported.
4294 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4295 __isl_take isl_space
*space
)
4297 return isl_pw_multi_aff_identity_on_domain_space(space
);
4300 /* Exploit the equalities in "eq" to simplify the affine expressions.
4302 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4303 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4307 maff
= isl_multi_aff_cow(maff
);
4311 for (i
= 0; i
< maff
->n
; ++i
) {
4312 maff
->u
.p
[i
] = isl_aff_substitute_equalities(maff
->u
.p
[i
],
4313 isl_basic_set_copy(eq
));
4318 isl_basic_set_free(eq
);
4321 isl_basic_set_free(eq
);
4322 isl_multi_aff_free(maff
);
4326 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4331 maff
= isl_multi_aff_cow(maff
);
4335 for (i
= 0; i
< maff
->n
; ++i
) {
4336 maff
->u
.p
[i
] = isl_aff_scale(maff
->u
.p
[i
], f
);
4338 return isl_multi_aff_free(maff
);
4344 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4345 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4347 maff1
= isl_multi_aff_add(maff1
, maff2
);
4348 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4352 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4355 return isl_bool_error
;
4357 return isl_bool_false
;
4360 /* Return the set of domain elements where "ma1" is lexicographically
4361 * smaller than or equal to "ma2".
4363 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4364 __isl_take isl_multi_aff
*ma2
)
4366 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4369 /* Return the set of domain elements where "ma1" is lexicographically
4370 * smaller than "ma2".
4372 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4373 __isl_take isl_multi_aff
*ma2
)
4375 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4378 /* Return the set of domain elements where "ma1" is lexicographically
4379 * greater than to "ma2". If "equal" is set, then include the domain
4380 * elements where they are equal.
4381 * Do this for the case where there are no entries.
4382 * In this case, "ma1" cannot be greater than "ma2",
4383 * but it is (greater than or) equal to "ma2".
4385 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4386 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4390 space
= isl_multi_aff_get_domain_space(ma1
);
4392 isl_multi_aff_free(ma1
);
4393 isl_multi_aff_free(ma2
);
4396 return isl_set_universe(space
);
4398 return isl_set_empty(space
);
4401 /* Return the set where entry "i" of "ma1" and "ma2"
4402 * satisfy the relation prescribed by "cmp".
4404 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4405 __isl_keep isl_multi_aff
*ma2
, int i
,
4406 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4407 __isl_take isl_aff
*aff2
))
4409 isl_aff
*aff1
, *aff2
;
4411 aff1
= isl_multi_aff_get_at(ma1
, i
);
4412 aff2
= isl_multi_aff_get_at(ma2
, i
);
4413 return cmp(aff1
, aff2
);
4416 /* Return the set of domain elements where "ma1" is lexicographically
4417 * greater than to "ma2". If "equal" is set, then include the domain
4418 * elements where they are equal.
4420 * In particular, for all but the final entry,
4421 * include the set of elements where this entry is strictly greater in "ma1"
4422 * and all previous entries are equal.
4423 * The final entry is also allowed to be equal in the two functions
4424 * if "equal" is set.
4426 * The case where there are no entries is handled separately.
4428 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4429 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4438 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4440 n
= isl_multi_aff_size(ma1
);
4444 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4446 space
= isl_multi_aff_get_domain_space(ma1
);
4447 res
= isl_set_empty(isl_space_copy(space
));
4448 equal_set
= isl_set_universe(space
);
4450 for (i
= 0; i
+ 1 < n
; ++i
) {
4454 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4455 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4456 res
= isl_set_union(res
, gt
);
4457 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4458 equal_set
= isl_set_intersect(equal_set
, eq
);
4460 empty
= isl_set_is_empty(equal_set
);
4461 if (empty
>= 0 && empty
)
4466 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4468 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4469 isl_multi_aff_free(ma1
);
4470 isl_multi_aff_free(ma2
);
4472 gte
= isl_set_intersect(gte
, equal_set
);
4473 return isl_set_union(res
, gte
);
4475 isl_multi_aff_free(ma1
);
4476 isl_multi_aff_free(ma2
);
4480 /* Return the set of domain elements where "ma1" is lexicographically
4481 * greater than or equal to "ma2".
4483 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4484 __isl_take isl_multi_aff
*ma2
)
4486 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4489 /* Return the set of domain elements where "ma1" is lexicographically
4490 * greater than "ma2".
4492 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4493 __isl_take isl_multi_aff
*ma2
)
4495 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4498 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4501 #define PW isl_pw_multi_aff
4503 #define BASE multi_aff
4505 #define EL_IS_ZERO is_empty
4509 #define IS_ZERO is_empty
4512 #undef DEFAULT_IS_ZERO
4513 #define DEFAULT_IS_ZERO 0
4515 #include <isl_pw_templ.c>
4516 #include <isl_pw_add_constant_multi_val_templ.c>
4517 #include <isl_pw_add_constant_val_templ.c>
4518 #include <isl_pw_bind_domain_templ.c>
4519 #include <isl_pw_insert_dims_templ.c>
4520 #include <isl_pw_insert_domain_templ.c>
4521 #include <isl_pw_locals_templ.c>
4522 #include <isl_pw_move_dims_templ.c>
4523 #include <isl_pw_neg_templ.c>
4524 #include <isl_pw_pullback_templ.c>
4525 #include <isl_pw_range_tuple_id_templ.c>
4526 #include <isl_pw_union_opt.c>
4529 #define BASE pw_multi_aff
4531 #include <isl_union_multi.c>
4532 #include "isl_union_locals_templ.c"
4533 #include <isl_union_neg.c>
4536 #define BASE multi_aff
4538 #include <isl_union_pw_templ.c>
4540 /* Generic function for extracting a factor from a product "pma".
4541 * "check_space" checks that the space is that of the right kind of product.
4542 * "space_factor" extracts the factor from the space.
4543 * "multi_aff_factor" extracts the factor from the constituent functions.
4545 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4546 __isl_take isl_pw_multi_aff
*pma
,
4547 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4548 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4549 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4550 __isl_take isl_multi_aff
*ma
))
4555 if (check_space(pma
) < 0)
4556 return isl_pw_multi_aff_free(pma
);
4558 space
= isl_pw_multi_aff_take_space(pma
);
4559 space
= space_factor(space
);
4561 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4564 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4565 ma
= multi_aff_factor(ma
);
4566 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4569 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4574 /* Is the range of "pma" a wrapped relation?
4576 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4577 __isl_keep isl_pw_multi_aff
*pma
)
4579 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4582 /* Check that the range of "pma" is a product.
4584 static isl_stat
pw_multi_aff_check_range_product(
4585 __isl_keep isl_pw_multi_aff
*pma
)
4589 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4591 return isl_stat_error
;
4593 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4594 "range is not a product", return isl_stat_error
);
4598 /* Given a function A -> [B -> C], extract the function A -> B.
4600 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4601 __isl_take isl_pw_multi_aff
*pma
)
4603 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4604 &isl_space_range_factor_domain
,
4605 &isl_multi_aff_range_factor_domain
);
4608 /* Given a function A -> [B -> C], extract the function A -> C.
4610 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4611 __isl_take isl_pw_multi_aff
*pma
)
4613 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4614 &isl_space_range_factor_range
,
4615 &isl_multi_aff_range_factor_range
);
4618 /* Given two piecewise multi affine expressions, return a piecewise
4619 * multi-affine expression defined on the union of the definition domains
4620 * of the inputs that is equal to the lexicographic maximum of the two
4621 * inputs on each cell. If only one of the two inputs is defined on
4622 * a given cell, then it is considered to be the maximum.
4624 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4625 __isl_take isl_pw_multi_aff
*pma1
,
4626 __isl_take isl_pw_multi_aff
*pma2
)
4628 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4629 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4630 &isl_multi_aff_lex_ge_set
);
4633 /* Given two piecewise multi affine expressions, return a piecewise
4634 * multi-affine expression defined on the union of the definition domains
4635 * of the inputs that is equal to the lexicographic minimum of the two
4636 * inputs on each cell. If only one of the two inputs is defined on
4637 * a given cell, then it is considered to be the minimum.
4639 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4640 __isl_take isl_pw_multi_aff
*pma1
,
4641 __isl_take isl_pw_multi_aff
*pma2
)
4643 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4644 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4645 &isl_multi_aff_lex_le_set
);
4648 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4649 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4651 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4652 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4653 &isl_multi_aff_add
);
4656 /* Subtract "pma2" from "pma1" and return the result.
4658 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4659 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4661 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4662 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4663 &isl_multi_aff_sub
);
4666 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4667 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4669 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4672 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4673 * with the actual sum on the shared domain and
4674 * the defined expression on the symmetric difference of the domains.
4676 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4677 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4679 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4682 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4683 * with the actual sum on the shared domain and
4684 * the defined expression on the symmetric difference of the domains.
4686 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4687 __isl_take isl_union_pw_multi_aff
*upma1
,
4688 __isl_take isl_union_pw_multi_aff
*upma2
)
4690 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4693 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4694 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4696 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4697 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4701 isl_pw_multi_aff
*res
;
4703 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4706 n
= pma1
->n
* pma2
->n
;
4707 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4708 isl_space_copy(pma2
->dim
));
4709 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4711 for (i
= 0; i
< pma1
->n
; ++i
) {
4712 for (j
= 0; j
< pma2
->n
; ++j
) {
4716 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4717 isl_set_copy(pma2
->p
[j
].set
));
4718 ma
= isl_multi_aff_product(
4719 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4720 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4721 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4725 isl_pw_multi_aff_free(pma1
);
4726 isl_pw_multi_aff_free(pma2
);
4729 isl_pw_multi_aff_free(pma1
);
4730 isl_pw_multi_aff_free(pma2
);
4734 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4735 * denominator "denom".
4736 * "denom" is allowed to be negative, in which case the actual denominator
4737 * is -denom and the expressions are added instead.
4739 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4740 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4746 first
= isl_seq_first_non_zero(c
, n
);
4750 sign
= isl_int_sgn(denom
);
4752 isl_int_abs(d
, denom
);
4753 for (i
= first
; i
< n
; ++i
) {
4756 if (isl_int_is_zero(c
[i
]))
4758 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4759 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4760 aff_i
= isl_aff_scale_down(aff_i
, d
);
4762 aff
= isl_aff_sub(aff
, aff_i
);
4764 aff
= isl_aff_add(aff
, aff_i
);
4771 /* Extract an affine expression that expresses the output dimension "pos"
4772 * of "bmap" in terms of the parameters and input dimensions from
4774 * Note that this expression may involve integer divisions defined
4775 * in terms of parameters and input dimensions.
4776 * The equality may also involve references to earlier (but not later)
4777 * output dimensions. These are replaced by the corresponding elements
4780 * If the equality is of the form
4782 * f(i) + h(j) + a x + g(i) = 0,
4784 * with f(i) a linear combinations of the parameters and input dimensions,
4785 * g(i) a linear combination of integer divisions defined in terms of the same
4786 * and h(j) a linear combinations of earlier output dimensions,
4787 * then the affine expression is
4789 * (-f(i) - g(i))/a - h(j)/a
4791 * If the equality is of the form
4793 * f(i) + h(j) - a x + g(i) = 0,
4795 * then the affine expression is
4797 * (f(i) + g(i))/a - h(j)/(-a)
4800 * If "div" refers to an integer division (i.e., it is smaller than
4801 * the number of integer divisions), then the equality constraint
4802 * does involve an integer division (the one at position "div") that
4803 * is defined in terms of output dimensions. However, this integer
4804 * division can be eliminated by exploiting a pair of constraints
4805 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4806 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4808 * In particular, let
4810 * x = e(i) + m floor(...)
4812 * with e(i) the expression derived above and floor(...) the integer
4813 * division involving output dimensions.
4824 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4825 * = (e(i) - l) mod m
4829 * x - l = (e(i) - l) mod m
4833 * x = ((e(i) - l) mod m) + l
4835 * The variable "shift" below contains the expression -l, which may
4836 * also involve a linear combination of earlier output dimensions.
4838 static __isl_give isl_aff
*extract_aff_from_equality(
4839 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4840 __isl_keep isl_multi_aff
*ma
)
4843 isl_size n_div
, n_out
;
4845 isl_local_space
*ls
;
4846 isl_aff
*aff
, *shift
;
4849 ctx
= isl_basic_map_get_ctx(bmap
);
4850 ls
= isl_basic_map_get_local_space(bmap
);
4851 ls
= isl_local_space_domain(ls
);
4852 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4855 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4856 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4857 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4858 if (n_out
< 0 || n_div
< 0)
4860 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4861 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4862 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4863 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4865 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4866 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4867 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4870 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4871 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4872 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4873 bmap
->eq
[eq
][o_out
+ pos
]);
4875 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4878 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4879 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4880 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4881 isl_int_set_si(shift
->v
->el
[0], 1);
4882 shift
= subtract_initial(shift
, ma
, pos
,
4883 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4884 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4885 mod
= isl_val_int_from_isl_int(ctx
,
4886 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4887 mod
= isl_val_abs(mod
);
4888 aff
= isl_aff_mod_val(aff
, mod
);
4889 aff
= isl_aff_sub(aff
, shift
);
4892 isl_local_space_free(ls
);
4895 isl_local_space_free(ls
);
4900 /* Given a basic map with output dimensions defined
4901 * in terms of the parameters input dimensions and earlier
4902 * output dimensions using an equality (and possibly a pair on inequalities),
4903 * extract an isl_aff that expresses output dimension "pos" in terms
4904 * of the parameters and input dimensions.
4905 * Note that this expression may involve integer divisions defined
4906 * in terms of parameters and input dimensions.
4907 * "ma" contains the expressions corresponding to earlier output dimensions.
4909 * This function shares some similarities with
4910 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4912 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4913 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4920 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4921 if (eq
>= bmap
->n_eq
)
4922 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4923 "unable to find suitable equality", return NULL
);
4924 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4926 aff
= isl_aff_remove_unused_divs(aff
);
4930 /* Given a basic map where each output dimension is defined
4931 * in terms of the parameters and input dimensions using an equality,
4932 * extract an isl_multi_aff that expresses the output dimensions in terms
4933 * of the parameters and input dimensions.
4935 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4936 __isl_take isl_basic_map
*bmap
)
4945 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4946 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4948 ma
= isl_multi_aff_free(ma
);
4950 for (i
= 0; i
< n_out
; ++i
) {
4953 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4954 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4957 isl_basic_map_free(bmap
);
4962 /* Given a basic set where each set dimension is defined
4963 * in terms of the parameters using an equality,
4964 * extract an isl_multi_aff that expresses the set dimensions in terms
4965 * of the parameters.
4967 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4968 __isl_take isl_basic_set
*bset
)
4970 return extract_isl_multi_aff_from_basic_map(bset
);
4973 /* Create an isl_pw_multi_aff that is equivalent to
4974 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4975 * The given basic map is such that each output dimension is defined
4976 * in terms of the parameters and input dimensions using an equality.
4978 * Since some applications expect the result of isl_pw_multi_aff_from_map
4979 * to only contain integer affine expressions, we compute the floor
4980 * of the expression before returning.
4982 * Remove all constraints involving local variables without
4983 * an explicit representation (resulting in the removal of those
4984 * local variables) prior to the actual extraction to ensure
4985 * that the local spaces in which the resulting affine expressions
4986 * are created do not contain any unknown local variables.
4987 * Removing such constraints is safe because constraints involving
4988 * unknown local variables are not used to determine whether
4989 * a basic map is obviously single-valued.
4991 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4992 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4996 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
4997 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4998 ma
= isl_multi_aff_floor(ma
);
4999 return isl_pw_multi_aff_alloc(domain
, ma
);
5002 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5003 * This obviously only works if the input "map" is single-valued.
5004 * If so, we compute the lexicographic minimum of the image in the form
5005 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5006 * to its lexicographic minimum.
5007 * If the input is not single-valued, we produce an error.
5009 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
5010 __isl_take isl_map
*map
)
5014 isl_pw_multi_aff
*pma
;
5016 sv
= isl_map_is_single_valued(map
);
5020 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5021 "map is not single-valued", goto error
);
5022 map
= isl_map_make_disjoint(map
);
5026 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5028 for (i
= 0; i
< map
->n
; ++i
) {
5029 isl_pw_multi_aff
*pma_i
;
5030 isl_basic_map
*bmap
;
5031 bmap
= isl_basic_map_copy(map
->p
[i
]);
5032 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5033 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5043 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5044 * taking into account that the output dimension at position "d"
5045 * can be represented as
5047 * x = floor((e(...) + c1) / m)
5049 * given that constraint "i" is of the form
5051 * e(...) + c1 - m x >= 0
5054 * Let "map" be of the form
5058 * We construct a mapping
5060 * A -> [A -> x = floor(...)]
5062 * apply that to the map, obtaining
5064 * [A -> x = floor(...)] -> B
5066 * and equate dimension "d" to x.
5067 * We then compute a isl_pw_multi_aff representation of the resulting map
5068 * and plug in the mapping above.
5070 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5071 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5074 isl_space
*space
= NULL
;
5075 isl_local_space
*ls
;
5083 isl_pw_multi_aff
*pma
;
5086 is_set
= isl_map_is_set(map
);
5090 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5091 ctx
= isl_map_get_ctx(map
);
5092 space
= isl_space_domain(isl_map_get_space(map
));
5093 n_in
= isl_space_dim(space
, isl_dim_set
);
5094 n
= isl_space_dim(space
, isl_dim_all
);
5095 if (n_in
< 0 || n
< 0)
5098 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5100 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5101 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5103 isl_basic_map_free(hull
);
5105 ls
= isl_local_space_from_space(isl_space_copy(space
));
5106 aff
= isl_aff_alloc_vec(ls
, v
);
5107 aff
= isl_aff_floor(aff
);
5109 isl_space_free(space
);
5110 ma
= isl_multi_aff_from_aff(aff
);
5112 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5113 ma
= isl_multi_aff_range_product(ma
,
5114 isl_multi_aff_from_aff(aff
));
5117 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5118 map
= isl_map_apply_domain(map
, insert
);
5119 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5120 pma
= isl_pw_multi_aff_from_map(map
);
5121 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5125 isl_space_free(space
);
5127 isl_basic_map_free(hull
);
5131 /* Is constraint "c" of the form
5133 * e(...) + c1 - m x >= 0
5137 * -e(...) + c2 + m x >= 0
5139 * where m > 1 and e only depends on parameters and input dimensions?
5141 * "offset" is the offset of the output dimensions
5142 * "pos" is the position of output dimension x.
5144 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5146 if (isl_int_is_zero(c
[offset
+ d
]))
5148 if (isl_int_is_one(c
[offset
+ d
]))
5150 if (isl_int_is_negone(c
[offset
+ d
]))
5152 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5154 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5155 total
- (offset
+ d
+ 1)) != -1)
5160 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5162 * As a special case, we first check if there is any pair of constraints,
5163 * shared by all the basic maps in "map" that force a given dimension
5164 * to be equal to the floor of some affine combination of the input dimensions.
5166 * In particular, if we can find two constraints
5168 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5172 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5174 * where m > 1 and e only depends on parameters and input dimensions,
5177 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5179 * then we know that we can take
5181 * x = floor((e(...) + c1) / m)
5183 * without having to perform any computation.
5185 * Note that we know that
5189 * If c1 + c2 were 0, then we would have detected an equality during
5190 * simplification. If c1 + c2 were negative, then we would have detected
5193 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5194 __isl_take isl_map
*map
)
5202 isl_basic_map
*hull
;
5204 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5205 dim
= isl_map_dim(map
, isl_dim_out
);
5206 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5207 if (dim
< 0 || total
< 0)
5211 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5213 for (d
= 0; d
< dim
; ++d
) {
5214 for (i
= 0; i
< n
; ++i
) {
5215 if (!is_potential_div_constraint(hull
->ineq
[i
],
5216 offset
, d
, 1 + total
))
5218 for (j
= i
+ 1; j
< n
; ++j
) {
5219 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5220 hull
->ineq
[j
] + 1, total
))
5222 isl_int_add(sum
, hull
->ineq
[i
][0],
5224 if (isl_int_abs_lt(sum
,
5225 hull
->ineq
[i
][offset
+ d
]))
5232 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5234 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5238 isl_basic_map_free(hull
);
5239 return pw_multi_aff_from_map_base(map
);
5242 isl_basic_map_free(hull
);
5246 /* Given an affine expression
5248 * [A -> B] -> f(A,B)
5250 * construct an isl_multi_aff
5254 * such that dimension "d" in B' is set to "aff" and the remaining
5255 * dimensions are set equal to the corresponding dimensions in B.
5256 * "n_in" is the dimension of the space A.
5257 * "n_out" is the dimension of the space B.
5259 * If "is_set" is set, then the affine expression is of the form
5263 * and we construct an isl_multi_aff
5267 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5268 unsigned n_in
, unsigned n_out
, int is_set
)
5272 isl_space
*space
, *space2
;
5273 isl_local_space
*ls
;
5275 space
= isl_aff_get_domain_space(aff
);
5276 ls
= isl_local_space_from_space(isl_space_copy(space
));
5277 space2
= isl_space_copy(space
);
5279 space2
= isl_space_range(isl_space_unwrap(space2
));
5280 space
= isl_space_map_from_domain_and_range(space
, space2
);
5281 ma
= isl_multi_aff_alloc(space
);
5282 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5284 for (i
= 0; i
< n_out
; ++i
) {
5287 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5288 isl_dim_set
, n_in
+ i
);
5289 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5292 isl_local_space_free(ls
);
5297 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5298 * taking into account that the dimension at position "d" can be written as
5300 * x = m a + f(..) (1)
5302 * where m is equal to "gcd".
5303 * "i" is the index of the equality in "hull" that defines f(..).
5304 * In particular, the equality is of the form
5306 * f(..) - x + m g(existentials) = 0
5310 * -f(..) + x + m g(existentials) = 0
5312 * We basically plug (1) into "map", resulting in a map with "a"
5313 * in the range instead of "x". The corresponding isl_pw_multi_aff
5314 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5316 * Specifically, given the input map
5320 * We first wrap it into a set
5324 * and define (1) on top of the corresponding space, resulting in "aff".
5325 * We use this to create an isl_multi_aff that maps the output position "d"
5326 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5327 * We plug this into the wrapped map, unwrap the result and compute the
5328 * corresponding isl_pw_multi_aff.
5329 * The result is an expression
5337 * so that we can plug that into "aff", after extending the latter to
5343 * If "map" is actually a set, then there is no "A" space, meaning
5344 * that we do not need to perform any wrapping, and that the result
5345 * of the recursive call is of the form
5349 * which is plugged into a mapping of the form
5353 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5354 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5359 isl_local_space
*ls
;
5362 isl_pw_multi_aff
*pma
, *id
;
5368 is_set
= isl_map_is_set(map
);
5372 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5373 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5374 if (n_in
< 0 || n_out
< 0)
5376 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5381 set
= isl_map_wrap(map
);
5382 space
= isl_space_map_from_set(isl_set_get_space(set
));
5383 ma
= isl_multi_aff_identity(space
);
5384 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5385 aff
= isl_aff_alloc(ls
);
5387 isl_int_set_si(aff
->v
->el
[0], 1);
5388 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5389 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5392 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5394 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5396 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5397 set
= isl_set_preimage_multi_aff(set
, ma
);
5399 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5404 map
= isl_set_unwrap(set
);
5405 pma
= isl_pw_multi_aff_from_map(map
);
5408 space
= isl_pw_multi_aff_get_domain_space(pma
);
5409 space
= isl_space_map_from_set(space
);
5410 id
= isl_pw_multi_aff_identity(space
);
5411 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5413 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5414 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5416 isl_basic_map_free(hull
);
5420 isl_basic_map_free(hull
);
5424 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5425 * "hull" contains the equalities valid for "map".
5427 * Check if any of the output dimensions is "strided".
5428 * That is, we check if it can be written as
5432 * with m greater than 1, a some combination of existentially quantified
5433 * variables and f an expression in the parameters and input dimensions.
5434 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5436 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5439 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5440 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5449 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5450 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5451 if (n_div
< 0 || n_out
< 0)
5455 isl_basic_map_free(hull
);
5456 return pw_multi_aff_from_map_check_div(map
);
5461 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5462 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5464 for (i
= 0; i
< n_out
; ++i
) {
5465 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5466 isl_int
*eq
= hull
->eq
[j
];
5467 isl_pw_multi_aff
*res
;
5469 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5470 !isl_int_is_negone(eq
[o_out
+ i
]))
5472 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5474 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5475 n_out
- (i
+ 1)) != -1)
5477 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5478 if (isl_int_is_zero(gcd
))
5480 if (isl_int_is_one(gcd
))
5483 res
= pw_multi_aff_from_map_stride(map
, hull
,
5491 isl_basic_map_free(hull
);
5492 return pw_multi_aff_from_map_check_div(map
);
5495 isl_basic_map_free(hull
);
5499 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5501 * As a special case, we first check if all output dimensions are uniquely
5502 * defined in terms of the parameters and input dimensions over the entire
5503 * domain. If so, we extract the desired isl_pw_multi_aff directly
5504 * from the affine hull of "map" and its domain.
5506 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5509 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5513 isl_basic_map
*hull
;
5515 n
= isl_map_n_basic_map(map
);
5520 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5521 hull
= isl_basic_map_plain_affine_hull(hull
);
5522 sv
= isl_basic_map_plain_is_single_valued(hull
);
5524 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5526 isl_basic_map_free(hull
);
5528 map
= isl_map_detect_equalities(map
);
5529 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5530 sv
= isl_basic_map_plain_is_single_valued(hull
);
5532 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5534 return pw_multi_aff_from_map_check_strides(map
, hull
);
5535 isl_basic_map_free(hull
);
5541 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5542 * but is considered as a function on an isl_map when exported.
5544 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5546 return isl_pw_multi_aff_from_map(map
);
5549 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5551 return isl_pw_multi_aff_from_map(set
);
5554 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5555 * but is considered as a function on an isl_set when exported.
5557 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5559 return isl_pw_multi_aff_from_set(set
);
5562 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5565 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5567 isl_union_pw_multi_aff
**upma
= user
;
5568 isl_pw_multi_aff
*pma
;
5570 pma
= isl_pw_multi_aff_from_map(map
);
5571 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5573 return *upma
? isl_stat_ok
: isl_stat_error
;
5576 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5579 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5580 __isl_take isl_aff
*aff
)
5583 isl_pw_multi_aff
*pma
;
5585 ma
= isl_multi_aff_from_aff(aff
);
5586 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5587 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5590 /* Try and create an isl_union_pw_multi_aff that is equivalent
5591 * to the given isl_union_map.
5592 * The isl_union_map is required to be single-valued in each space.
5593 * Otherwise, an error is produced.
5595 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5596 __isl_take isl_union_map
*umap
)
5599 isl_union_pw_multi_aff
*upma
;
5601 space
= isl_union_map_get_space(umap
);
5602 upma
= isl_union_pw_multi_aff_empty(space
);
5603 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5604 upma
= isl_union_pw_multi_aff_free(upma
);
5605 isl_union_map_free(umap
);
5610 /* This function performs the same operation as
5611 * isl_union_pw_multi_aff_from_union_map,
5612 * but is considered as a function on an isl_union_map when exported.
5614 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5615 __isl_take isl_union_map
*umap
)
5617 return isl_union_pw_multi_aff_from_union_map(umap
);
5620 /* Try and create an isl_union_pw_multi_aff that is equivalent
5621 * to the given isl_union_set.
5622 * The isl_union_set is required to be a singleton in each space.
5623 * Otherwise, an error is produced.
5625 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5626 __isl_take isl_union_set
*uset
)
5628 return isl_union_pw_multi_aff_from_union_map(uset
);
5631 /* Return the piecewise affine expression "set ? 1 : 0".
5633 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5636 isl_space
*space
= isl_set_get_space(set
);
5637 isl_local_space
*ls
= isl_local_space_from_space(space
);
5638 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5639 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5641 one
= isl_aff_add_constant_si(one
, 1);
5642 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5643 set
= isl_set_complement(set
);
5644 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5649 /* Plug in "subs" for dimension "type", "pos" of "aff".
5651 * Let i be the dimension to replace and let "subs" be of the form
5655 * and "aff" of the form
5661 * (a f + d g')/(m d)
5663 * where g' is the result of plugging in "subs" in each of the integer
5666 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5667 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5673 aff
= isl_aff_cow(aff
);
5675 return isl_aff_free(aff
);
5677 ctx
= isl_aff_get_ctx(aff
);
5678 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5679 isl_die(ctx
, isl_error_invalid
,
5680 "spaces don't match", return isl_aff_free(aff
));
5681 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5683 return isl_aff_free(aff
);
5685 isl_die(ctx
, isl_error_unsupported
,
5686 "cannot handle divs yet", return isl_aff_free(aff
));
5688 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5690 return isl_aff_free(aff
);
5692 aff
->v
= isl_vec_cow(aff
->v
);
5694 return isl_aff_free(aff
);
5696 pos
+= isl_local_space_offset(aff
->ls
, type
);
5699 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5700 aff
->v
->size
, subs
->v
->size
, v
);
5706 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5707 * expressions in "maff".
5709 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5710 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5711 __isl_keep isl_aff
*subs
)
5715 maff
= isl_multi_aff_cow(maff
);
5717 return isl_multi_aff_free(maff
);
5719 if (type
== isl_dim_in
)
5722 for (i
= 0; i
< maff
->n
; ++i
) {
5723 maff
->u
.p
[i
] = isl_aff_substitute(maff
->u
.p
[i
],
5726 return isl_multi_aff_free(maff
);
5732 /* Plug in "subs" for input dimension "pos" of "pma".
5734 * pma is of the form
5738 * while subs is of the form
5740 * v' = B_j(v) -> S_j
5742 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5743 * has a contribution in the result, in particular
5745 * C_ij(S_j) -> M_i(S_j)
5747 * Note that plugging in S_j in C_ij may also result in an empty set
5748 * and this contribution should simply be discarded.
5750 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5751 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5752 __isl_keep isl_pw_aff
*subs
)
5755 isl_pw_multi_aff
*res
;
5758 return isl_pw_multi_aff_free(pma
);
5760 n
= pma
->n
* subs
->n
;
5761 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5763 for (i
= 0; i
< pma
->n
; ++i
) {
5764 for (j
= 0; j
< subs
->n
; ++j
) {
5766 isl_multi_aff
*res_ij
;
5769 common
= isl_set_intersect(
5770 isl_set_copy(pma
->p
[i
].set
),
5771 isl_set_copy(subs
->p
[j
].set
));
5772 common
= isl_set_substitute(common
,
5773 pos
, subs
->p
[j
].aff
);
5774 empty
= isl_set_plain_is_empty(common
);
5775 if (empty
< 0 || empty
) {
5776 isl_set_free(common
);
5782 res_ij
= isl_multi_aff_substitute(
5783 isl_multi_aff_copy(pma
->p
[i
].maff
),
5784 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5786 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5790 isl_pw_multi_aff_free(pma
);
5793 isl_pw_multi_aff_free(pma
);
5794 isl_pw_multi_aff_free(res
);
5798 /* Compute the preimage of a range of dimensions in the affine expression "src"
5799 * under "ma" and put the result in "dst". The number of dimensions in "src"
5800 * that precede the range is given by "n_before". The number of dimensions
5801 * in the range is given by the number of output dimensions of "ma".
5802 * The number of dimensions that follow the range is given by "n_after".
5803 * If "has_denom" is set (to one),
5804 * then "src" and "dst" have an extra initial denominator.
5805 * "n_div_ma" is the number of existentials in "ma"
5806 * "n_div_bset" is the number of existentials in "src"
5807 * The resulting "dst" (which is assumed to have been allocated by
5808 * the caller) contains coefficients for both sets of existentials,
5809 * first those in "ma" and then those in "src".
5810 * f, c1, c2 and g are temporary objects that have been initialized
5813 * Let src represent the expression
5815 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5817 * and let ma represent the expressions
5819 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5821 * We start out with the following expression for dst:
5823 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5825 * with the multiplication factor f initially equal to 1
5826 * and f \sum_i b_i v_i kept separately.
5827 * For each x_i that we substitute, we multiply the numerator
5828 * (and denominator) of dst by c_1 = m_i and add the numerator
5829 * of the x_i expression multiplied by c_2 = f b_i,
5830 * after removing the common factors of c_1 and c_2.
5831 * The multiplication factor f also needs to be multiplied by c_1
5832 * for the next x_j, j > i.
5834 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5835 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5836 int n_div_ma
, int n_div_bmap
,
5837 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5840 isl_size n_param
, n_in
, n_out
;
5843 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5844 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5845 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5846 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5847 return isl_stat_error
;
5849 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5850 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5851 isl_seq_clr(dst
+ o_dst
, n_in
);
5854 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5857 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5859 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5861 isl_int_set_si(f
, 1);
5863 for (i
= 0; i
< n_out
; ++i
) {
5864 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5866 if (isl_int_is_zero(src
[offset
]))
5868 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5869 isl_int_mul(c2
, f
, src
[offset
]);
5870 isl_int_gcd(g
, c1
, c2
);
5871 isl_int_divexact(c1
, c1
, g
);
5872 isl_int_divexact(c2
, c2
, g
);
5874 isl_int_mul(f
, f
, c1
);
5877 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5878 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5879 o_dst
+= 1 + n_param
;
5880 o_src
+= 1 + n_param
;
5881 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5883 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5884 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
5887 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5889 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5890 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
5893 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5895 isl_int_mul(dst
[0], dst
[0], c1
);
5901 /* Compute the pullback of "aff" by the function represented by "ma".
5902 * In other words, plug in "ma" in "aff". The result is an affine expression
5903 * defined over the domain space of "ma".
5905 * If "aff" is represented by
5907 * (a(p) + b x + c(divs))/d
5909 * and ma is represented by
5911 * x = D(p) + F(y) + G(divs')
5913 * then the result is
5915 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5917 * The divs in the local space of the input are similarly adjusted
5918 * through a call to isl_local_space_preimage_multi_aff.
5920 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5921 __isl_take isl_multi_aff
*ma
)
5923 isl_aff
*res
= NULL
;
5924 isl_local_space
*ls
;
5925 isl_size n_div_aff
, n_div_ma
;
5926 isl_int f
, c1
, c2
, g
;
5928 ma
= isl_multi_aff_align_divs(ma
);
5932 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5933 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
5934 if (n_div_aff
< 0 || n_div_ma
< 0)
5937 ls
= isl_aff_get_domain_local_space(aff
);
5938 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5939 res
= isl_aff_alloc(ls
);
5948 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
5949 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
5950 res
= isl_aff_free(res
);
5958 isl_multi_aff_free(ma
);
5959 res
= isl_aff_normalize(res
);
5963 isl_multi_aff_free(ma
);
5968 /* Compute the pullback of "aff1" by the function represented by "aff2".
5969 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5970 * defined over the domain space of "aff1".
5972 * The domain of "aff1" should match the range of "aff2", which means
5973 * that it should be single-dimensional.
5975 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5976 __isl_take isl_aff
*aff2
)
5980 ma
= isl_multi_aff_from_aff(aff2
);
5981 return isl_aff_pullback_multi_aff(aff1
, ma
);
5984 /* Compute the pullback of "ma1" by the function represented by "ma2".
5985 * In other words, plug in "ma2" in "ma1".
5987 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5988 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5991 isl_space
*space
= NULL
;
5993 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
5994 ma2
= isl_multi_aff_align_divs(ma2
);
5995 ma1
= isl_multi_aff_cow(ma1
);
5999 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
6000 isl_multi_aff_get_space(ma1
));
6002 for (i
= 0; i
< ma1
->n
; ++i
) {
6003 ma1
->u
.p
[i
] = isl_aff_pullback_multi_aff(ma1
->u
.p
[i
],
6004 isl_multi_aff_copy(ma2
));
6009 ma1
= isl_multi_aff_reset_space(ma1
, space
);
6010 isl_multi_aff_free(ma2
);
6013 isl_space_free(space
);
6014 isl_multi_aff_free(ma2
);
6015 isl_multi_aff_free(ma1
);
6019 /* Extend the local space of "dst" to include the divs
6020 * in the local space of "src".
6022 * If "src" does not have any divs or if the local spaces of "dst" and
6023 * "src" are the same, then no extension is required.
6025 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6026 __isl_keep isl_aff
*src
)
6029 isl_size src_n_div
, dst_n_div
;
6036 return isl_aff_free(dst
);
6038 ctx
= isl_aff_get_ctx(src
);
6039 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6041 return isl_aff_free(dst
);
6043 isl_die(ctx
, isl_error_invalid
,
6044 "spaces don't match", goto error
);
6046 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6047 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6050 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6051 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6052 return isl_aff_free(dst
);
6056 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6057 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6058 if (!exp1
|| (dst_n_div
&& !exp2
))
6061 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6062 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6070 return isl_aff_free(dst
);
6073 /* Adjust the local spaces of the affine expressions in "maff"
6074 * such that they all have the save divs.
6076 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6077 __isl_take isl_multi_aff
*maff
)
6085 maff
= isl_multi_aff_cow(maff
);
6089 for (i
= 1; i
< maff
->n
; ++i
)
6090 maff
->u
.p
[0] = isl_aff_align_divs(maff
->u
.p
[0], maff
->u
.p
[i
]);
6091 for (i
= 1; i
< maff
->n
; ++i
) {
6092 maff
->u
.p
[i
] = isl_aff_align_divs(maff
->u
.p
[i
], maff
->u
.p
[0]);
6094 return isl_multi_aff_free(maff
);
6100 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6102 aff
= isl_aff_cow(aff
);
6106 aff
->ls
= isl_local_space_lift(aff
->ls
);
6108 return isl_aff_free(aff
);
6113 /* Lift "maff" to a space with extra dimensions such that the result
6114 * has no more existentially quantified variables.
6115 * If "ls" is not NULL, then *ls is assigned the local space that lies
6116 * at the basis of the lifting applied to "maff".
6118 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6119 __isl_give isl_local_space
**ls
)
6133 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6134 *ls
= isl_local_space_from_space(space
);
6136 return isl_multi_aff_free(maff
);
6141 maff
= isl_multi_aff_cow(maff
);
6142 maff
= isl_multi_aff_align_divs(maff
);
6146 n_div
= isl_aff_dim(maff
->u
.p
[0], isl_dim_div
);
6148 return isl_multi_aff_free(maff
);
6149 space
= isl_multi_aff_get_space(maff
);
6150 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6151 space
= isl_space_extend_domain_with_range(space
,
6152 isl_multi_aff_get_space(maff
));
6154 return isl_multi_aff_free(maff
);
6155 isl_space_free(maff
->space
);
6156 maff
->space
= space
;
6159 *ls
= isl_aff_get_domain_local_space(maff
->u
.p
[0]);
6161 return isl_multi_aff_free(maff
);
6164 for (i
= 0; i
< maff
->n
; ++i
) {
6165 maff
->u
.p
[i
] = isl_aff_lift(maff
->u
.p
[i
]);
6173 isl_local_space_free(*ls
);
6174 return isl_multi_aff_free(maff
);
6178 #define TYPE isl_pw_multi_aff
6180 #include "check_type_range_templ.c"
6182 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6184 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6185 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6192 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6195 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6199 space
= isl_pw_multi_aff_get_space(pma
);
6200 space
= isl_space_drop_dims(space
, isl_dim_out
,
6201 pos
+ 1, n_out
- pos
- 1);
6202 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6204 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6205 for (i
= 0; i
< pma
->n
; ++i
) {
6207 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6208 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6214 /* This is an alternative name for the function above.
6216 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6217 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6219 return isl_pw_multi_aff_get_at(pma
, pos
);
6222 /* Return an isl_pw_multi_aff with the given "set" as domain and
6223 * an unnamed zero-dimensional range.
6225 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6226 __isl_take isl_set
*set
)
6231 space
= isl_set_get_space(set
);
6232 space
= isl_space_from_domain(space
);
6233 ma
= isl_multi_aff_zero(space
);
6234 return isl_pw_multi_aff_alloc(set
, ma
);
6237 /* Add an isl_pw_multi_aff with the given "set" as domain and
6238 * an unnamed zero-dimensional range to *user.
6240 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6243 isl_union_pw_multi_aff
**upma
= user
;
6244 isl_pw_multi_aff
*pma
;
6246 pma
= isl_pw_multi_aff_from_domain(set
);
6247 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6252 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6253 * an unnamed zero-dimensional range.
6255 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6256 __isl_take isl_union_set
*uset
)
6259 isl_union_pw_multi_aff
*upma
;
6264 space
= isl_union_set_get_space(uset
);
6265 upma
= isl_union_pw_multi_aff_empty(space
);
6267 if (isl_union_set_foreach_set(uset
,
6268 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6271 isl_union_set_free(uset
);
6274 isl_union_set_free(uset
);
6275 isl_union_pw_multi_aff_free(upma
);
6279 /* Local data for bin_entry and the callback "fn".
6281 struct isl_union_pw_multi_aff_bin_data
{
6282 isl_union_pw_multi_aff
*upma2
;
6283 isl_union_pw_multi_aff
*res
;
6284 isl_pw_multi_aff
*pma
;
6285 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6288 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6289 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6291 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6293 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6297 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6299 isl_pw_multi_aff_free(pma
);
6304 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6305 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6306 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6307 * as *entry. The callback should adjust data->res if desired.
6309 static __isl_give isl_union_pw_multi_aff
*bin_op(
6310 __isl_take isl_union_pw_multi_aff
*upma1
,
6311 __isl_take isl_union_pw_multi_aff
*upma2
,
6312 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6315 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6317 space
= isl_union_pw_multi_aff_get_space(upma2
);
6318 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6319 space
= isl_union_pw_multi_aff_get_space(upma1
);
6320 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6322 if (!upma1
|| !upma2
)
6326 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6327 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6328 &bin_entry
, &data
) < 0)
6331 isl_union_pw_multi_aff_free(upma1
);
6332 isl_union_pw_multi_aff_free(upma2
);
6335 isl_union_pw_multi_aff_free(upma1
);
6336 isl_union_pw_multi_aff_free(upma2
);
6337 isl_union_pw_multi_aff_free(data
.res
);
6341 /* Given two isl_pw_multi_affs A -> B and C -> D,
6342 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6344 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6345 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6349 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6350 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6351 isl_pw_multi_aff_get_space(pma2
));
6352 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6353 &isl_multi_aff_range_product
);
6356 /* Given two isl_pw_multi_affs A -> B and C -> D,
6357 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6359 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6360 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6364 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6365 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6366 isl_pw_multi_aff_get_space(pma2
));
6367 space
= isl_space_flatten_range(space
);
6368 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6369 &isl_multi_aff_flat_range_product
);
6372 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6373 * to compute some form of range product and add the result to data->res.
6375 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6376 __isl_give isl_pw_multi_aff
*(*range_product
)(
6377 __isl_take isl_pw_multi_aff
*pma1
,
6378 __isl_take isl_pw_multi_aff
*pma2
),
6381 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6383 isl_space
*space1
, *space2
;
6385 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6386 space2
= isl_pw_multi_aff_peek_space(pma2
);
6387 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6388 space2
, isl_dim_in
);
6389 if (match
< 0 || !match
) {
6390 isl_pw_multi_aff_free(pma2
);
6391 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6394 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6396 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6401 /* If data->pma and "pma2" have the same domain space, then compute
6402 * their flat range product and add the result to data->res.
6404 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6407 return gen_range_product_entry(pma2
,
6408 &isl_pw_multi_aff_flat_range_product
, user
);
6411 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6412 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6414 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6415 __isl_take isl_union_pw_multi_aff
*upma1
,
6416 __isl_take isl_union_pw_multi_aff
*upma2
)
6418 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6421 /* If data->pma and "pma2" have the same domain space, then compute
6422 * their range product and add the result to data->res.
6424 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6427 return gen_range_product_entry(pma2
,
6428 &isl_pw_multi_aff_range_product
, user
);
6431 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6432 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6434 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6435 __isl_take isl_union_pw_multi_aff
*upma1
,
6436 __isl_take isl_union_pw_multi_aff
*upma2
)
6438 return bin_op(upma1
, upma2
, &range_product_entry
);
6441 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6442 * The parameters are assumed to have been aligned.
6444 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6445 * except that it works on two different isl_pw_* types.
6447 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6448 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6449 __isl_take isl_pw_aff
*pa
)
6452 isl_pw_multi_aff
*res
= NULL
;
6457 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6458 pa
->dim
, isl_dim_in
))
6459 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6460 "domains don't match", goto error
);
6461 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6465 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6467 for (i
= 0; i
< pma
->n
; ++i
) {
6468 for (j
= 0; j
< pa
->n
; ++j
) {
6470 isl_multi_aff
*res_ij
;
6473 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6474 isl_set_copy(pa
->p
[j
].set
));
6475 empty
= isl_set_plain_is_empty(common
);
6476 if (empty
< 0 || empty
) {
6477 isl_set_free(common
);
6483 res_ij
= isl_multi_aff_set_aff(
6484 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6485 isl_aff_copy(pa
->p
[j
].aff
));
6486 res_ij
= isl_multi_aff_gist(res_ij
,
6487 isl_set_copy(common
));
6489 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6493 isl_pw_multi_aff_free(pma
);
6494 isl_pw_aff_free(pa
);
6497 isl_pw_multi_aff_free(pma
);
6498 isl_pw_aff_free(pa
);
6499 return isl_pw_multi_aff_free(res
);
6502 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6504 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6505 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6506 __isl_take isl_pw_aff
*pa
)
6508 isl_bool equal_params
;
6512 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6513 if (equal_params
< 0)
6516 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6517 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6518 isl_pw_aff_check_named_params(pa
) < 0)
6520 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6521 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6522 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6524 isl_pw_multi_aff_free(pma
);
6525 isl_pw_aff_free(pa
);
6529 /* Do the parameters of "pa" match those of "space"?
6531 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6532 __isl_keep isl_space
*space
)
6534 isl_space
*pa_space
;
6538 return isl_bool_error
;
6540 pa_space
= isl_pw_aff_get_space(pa
);
6542 match
= isl_space_has_equal_params(space
, pa_space
);
6544 isl_space_free(pa_space
);
6548 /* Check that the domain space of "pa" matches "space".
6550 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6551 __isl_keep isl_space
*space
)
6553 isl_space
*pa_space
;
6557 return isl_stat_error
;
6559 pa_space
= isl_pw_aff_get_space(pa
);
6561 match
= isl_space_has_equal_params(space
, pa_space
);
6565 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6566 "parameters don't match", goto error
);
6567 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6568 pa_space
, isl_dim_in
);
6572 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6573 "domains don't match", goto error
);
6574 isl_space_free(pa_space
);
6577 isl_space_free(pa_space
);
6578 return isl_stat_error
;
6586 #include <isl_multi_explicit_domain.c>
6587 #include <isl_multi_pw_aff_explicit_domain.c>
6588 #include <isl_multi_templ.c>
6589 #include <isl_multi_add_constant_templ.c>
6590 #include <isl_multi_apply_set.c>
6591 #include <isl_multi_arith_templ.c>
6592 #include <isl_multi_bind_templ.c>
6593 #include <isl_multi_bind_domain_templ.c>
6594 #include <isl_multi_coalesce.c>
6595 #include <isl_multi_domain_templ.c>
6596 #include <isl_multi_dim_id_templ.c>
6597 #include <isl_multi_dims.c>
6598 #include <isl_multi_from_base_templ.c>
6599 #include <isl_multi_gist.c>
6600 #include <isl_multi_hash.c>
6601 #include <isl_multi_identity_templ.c>
6602 #include <isl_multi_align_set.c>
6603 #include <isl_multi_insert_domain_templ.c>
6604 #include <isl_multi_intersect.c>
6605 #include <isl_multi_min_max_templ.c>
6606 #include <isl_multi_move_dims_templ.c>
6607 #include <isl_multi_nan_templ.c>
6608 #include <isl_multi_param_templ.c>
6609 #include <isl_multi_product_templ.c>
6610 #include <isl_multi_splice_templ.c>
6611 #include <isl_multi_tuple_id_templ.c>
6612 #include <isl_multi_union_add_templ.c>
6613 #include <isl_multi_zero_templ.c>
6614 #include <isl_multi_unbind_params_templ.c>
6616 /* Is every element of "mpa" defined over a single universe domain?
6618 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6620 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6623 /* Given that every element of "mpa" is defined over a single universe domain,
6624 * return the corresponding base expressions.
6626 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6627 __isl_take isl_multi_pw_aff
*mpa
)
6633 n
= isl_multi_pw_aff_size(mpa
);
6635 mpa
= isl_multi_pw_aff_free(mpa
);
6636 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6637 for (i
= 0; i
< n
; ++i
) {
6640 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6641 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6643 isl_multi_pw_aff_free(mpa
);
6647 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6648 * with this explicit domain.
6650 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6651 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6655 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6658 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6659 map
= isl_map_intersect_domain(map
, dom
);
6664 /* Are all elements of "mpa" piecewise constants?
6666 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6668 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6671 /* Does "mpa" have a non-trivial explicit domain?
6673 * The explicit domain, if present, is trivial if it represents
6674 * an (obviously) universe set.
6676 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6677 __isl_keep isl_multi_pw_aff
*mpa
)
6680 return isl_bool_error
;
6681 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6682 return isl_bool_false
;
6683 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6689 #include "isl_opt_mpa_templ.c"
6691 /* Compute the minima of the set dimensions as a function of the
6692 * parameters, but independently of the other set dimensions.
6694 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6696 return set_opt_mpa(set
, &isl_set_dim_min
);
6699 /* Compute the maxima of the set dimensions as a function of the
6700 * parameters, but independently of the other set dimensions.
6702 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6704 return set_opt_mpa(set
, &isl_set_dim_max
);
6710 #include "isl_opt_mpa_templ.c"
6712 /* Compute the minima of the output dimensions as a function of the
6713 * parameters and input dimensions, but independently of
6714 * the other output dimensions.
6716 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6718 return map_opt_mpa(map
, &isl_map_dim_min
);
6721 /* Compute the maxima of the output dimensions as a function of the
6722 * parameters and input dimensions, but independently of
6723 * the other output dimensions.
6725 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6727 return map_opt_mpa(map
, &isl_map_dim_max
);
6730 /* Scale the elements of "pma" by the corresponding elements of "mv".
6732 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6733 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6736 isl_bool equal_params
;
6738 pma
= isl_pw_multi_aff_cow(pma
);
6741 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6742 mv
->space
, isl_dim_set
))
6743 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6744 "spaces don't match", goto error
);
6745 equal_params
= isl_space_has_equal_params(pma
->dim
, mv
->space
);
6746 if (equal_params
< 0)
6748 if (!equal_params
) {
6749 pma
= isl_pw_multi_aff_align_params(pma
,
6750 isl_multi_val_get_space(mv
));
6751 mv
= isl_multi_val_align_params(mv
,
6752 isl_pw_multi_aff_get_space(pma
));
6757 for (i
= 0; i
< pma
->n
; ++i
) {
6758 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6759 isl_multi_val_copy(mv
));
6760 if (!pma
->p
[i
].maff
)
6764 isl_multi_val_free(mv
);
6767 isl_multi_val_free(mv
);
6768 isl_pw_multi_aff_free(pma
);
6772 /* This function is called for each entry of an isl_union_pw_multi_aff.
6773 * If the space of the entry matches that of data->mv,
6774 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6775 * Otherwise, return an empty isl_pw_multi_aff.
6777 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6778 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6780 isl_multi_val
*mv
= user
;
6784 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6785 mv
->space
, isl_dim_set
)) {
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
);
7135 /* Compute the pullback of "mpa" by the function represented by "ma".
7136 * In other words, plug in "ma" in "mpa".
7138 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7140 * If "mpa" has an explicit domain, then it is this domain
7141 * that needs to undergo a pullback, i.e., a preimage.
7143 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
7144 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7147 isl_space
*space
= NULL
;
7149 mpa
= isl_multi_pw_aff_cow(mpa
);
7153 space
= isl_space_join(isl_multi_aff_get_space(ma
),
7154 isl_multi_pw_aff_get_space(mpa
));
7158 for (i
= 0; i
< mpa
->n
; ++i
) {
7159 mpa
->u
.p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->u
.p
[i
],
7160 isl_multi_aff_copy(ma
));
7164 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7165 mpa
->u
.dom
= isl_set_preimage_multi_aff(mpa
->u
.dom
,
7166 isl_multi_aff_copy(ma
));
7171 isl_multi_aff_free(ma
);
7172 isl_space_free(mpa
->space
);
7176 isl_space_free(space
);
7177 isl_multi_pw_aff_free(mpa
);
7178 isl_multi_aff_free(ma
);
7182 /* Compute the pullback of "mpa" by the function represented by "ma".
7183 * In other words, plug in "ma" in "mpa".
7185 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
7186 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
7188 isl_bool equal_params
;
7192 equal_params
= isl_space_has_equal_params(mpa
->space
, ma
->space
);
7193 if (equal_params
< 0)
7196 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7197 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
7198 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
7199 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
7201 isl_multi_pw_aff_free(mpa
);
7202 isl_multi_aff_free(ma
);
7206 /* Compute the pullback of "mpa" by the function represented by "pma".
7207 * In other words, plug in "pma" in "mpa".
7209 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7211 * If "mpa" has an explicit domain, then it is this domain
7212 * that needs to undergo a pullback, i.e., a preimage.
7214 static __isl_give isl_multi_pw_aff
*
7215 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7216 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7219 isl_space
*space
= NULL
;
7221 mpa
= isl_multi_pw_aff_cow(mpa
);
7225 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
7226 isl_multi_pw_aff_get_space(mpa
));
7228 for (i
= 0; i
< mpa
->n
; ++i
) {
7229 mpa
->u
.p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7230 mpa
->u
.p
[i
], isl_pw_multi_aff_copy(pma
));
7234 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7235 mpa
->u
.dom
= isl_set_preimage_pw_multi_aff(mpa
->u
.dom
,
7236 isl_pw_multi_aff_copy(pma
));
7241 isl_pw_multi_aff_free(pma
);
7242 isl_space_free(mpa
->space
);
7246 isl_space_free(space
);
7247 isl_multi_pw_aff_free(mpa
);
7248 isl_pw_multi_aff_free(pma
);
7252 /* Compute the pullback of "mpa" by the function represented by "pma".
7253 * In other words, plug in "pma" in "mpa".
7255 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
7256 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
7258 isl_bool equal_params
;
7262 equal_params
= isl_space_has_equal_params(mpa
->space
, pma
->dim
);
7263 if (equal_params
< 0)
7266 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7267 mpa
= isl_multi_pw_aff_align_params(mpa
,
7268 isl_pw_multi_aff_get_space(pma
));
7269 pma
= isl_pw_multi_aff_align_params(pma
,
7270 isl_multi_pw_aff_get_space(mpa
));
7271 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
7273 isl_multi_pw_aff_free(mpa
);
7274 isl_pw_multi_aff_free(pma
);
7278 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7279 * with the domain of "aff". The domain of the result is the same
7281 * "mpa" and "aff" are assumed to have been aligned.
7283 * We first extract the parametric constant from "aff", defined
7284 * over the correct domain.
7285 * Then we add the appropriate combinations of the members of "mpa".
7286 * Finally, we add the integer divisions through recursive calls.
7288 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7289 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7292 isl_size n_in
, n_div
, n_mpa_in
;
7298 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7299 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7300 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7301 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7304 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7305 tmp
= isl_aff_copy(aff
);
7306 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7307 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7308 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7309 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7310 pa
= isl_pw_aff_from_aff(tmp
);
7312 for (i
= 0; i
< n_in
; ++i
) {
7315 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7317 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7318 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7319 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7320 pa
= isl_pw_aff_add(pa
, pa_i
);
7323 for (i
= 0; i
< n_div
; ++i
) {
7327 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7329 div
= isl_aff_get_div(aff
, i
);
7330 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7331 isl_multi_pw_aff_copy(mpa
), div
);
7332 pa_i
= isl_pw_aff_floor(pa_i
);
7333 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7334 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7335 pa
= isl_pw_aff_add(pa
, pa_i
);
7338 isl_multi_pw_aff_free(mpa
);
7343 isl_multi_pw_aff_free(mpa
);
7348 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7349 * with the domain of "aff". The domain of the result is the same
7352 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7353 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7355 isl_bool equal_params
;
7359 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7360 if (equal_params
< 0)
7363 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7365 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7366 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7368 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7371 isl_multi_pw_aff_free(mpa
);
7375 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7376 * with the domain of "pa". The domain of the result is the same
7378 * "mpa" and "pa" are assumed to have been aligned.
7380 * We consider each piece in turn. Note that the domains of the
7381 * pieces are assumed to be disjoint and they remain disjoint
7382 * after taking the preimage (over the same function).
7384 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7385 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7394 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7395 isl_pw_aff_get_space(pa
));
7396 res
= isl_pw_aff_empty(space
);
7398 for (i
= 0; i
< pa
->n
; ++i
) {
7402 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7403 isl_multi_pw_aff_copy(mpa
),
7404 isl_aff_copy(pa
->p
[i
].aff
));
7405 domain
= isl_set_copy(pa
->p
[i
].set
);
7406 domain
= isl_set_preimage_multi_pw_aff(domain
,
7407 isl_multi_pw_aff_copy(mpa
));
7408 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7409 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7412 isl_pw_aff_free(pa
);
7413 isl_multi_pw_aff_free(mpa
);
7416 isl_pw_aff_free(pa
);
7417 isl_multi_pw_aff_free(mpa
);
7421 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7422 * with the domain of "pa". The domain of the result is the same
7425 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7426 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7428 isl_bool equal_params
;
7432 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7433 if (equal_params
< 0)
7436 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7438 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7439 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7441 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7443 isl_pw_aff_free(pa
);
7444 isl_multi_pw_aff_free(mpa
);
7448 /* Compute the pullback of "pa" by the function represented by "mpa".
7449 * In other words, plug in "mpa" in "pa".
7450 * "pa" and "mpa" are assumed to have been aligned.
7452 * The pullback is computed by applying "pa" to "mpa".
7454 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
7455 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7457 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7460 /* Compute the pullback of "pa" by the function represented by "mpa".
7461 * In other words, plug in "mpa" in "pa".
7463 * The pullback is computed by applying "pa" to "mpa".
7465 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7466 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7468 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7471 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7472 * In other words, plug in "mpa2" in "mpa1".
7474 * We pullback each member of "mpa1" in turn.
7476 * If "mpa1" has an explicit domain, then it is this domain
7477 * that needs to undergo a pullback instead, i.e., a preimage.
7479 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
7480 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7483 isl_space
*space
= NULL
;
7485 isl_multi_pw_aff_align_params_bin(&mpa1
, &mpa2
);
7486 mpa1
= isl_multi_pw_aff_cow(mpa1
);
7490 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
7491 isl_multi_pw_aff_get_space(mpa1
));
7493 for (i
= 0; i
< mpa1
->n
; ++i
) {
7494 mpa1
->u
.p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7495 mpa1
->u
.p
[i
], isl_multi_pw_aff_copy(mpa2
));
7500 if (isl_multi_pw_aff_has_explicit_domain(mpa1
)) {
7501 mpa1
->u
.dom
= isl_set_preimage_multi_pw_aff(mpa1
->u
.dom
,
7502 isl_multi_pw_aff_copy(mpa2
));
7506 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
7508 isl_multi_pw_aff_free(mpa2
);
7511 isl_space_free(space
);
7512 isl_multi_pw_aff_free(mpa1
);
7513 isl_multi_pw_aff_free(mpa2
);
7517 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7518 * of "mpa1" and "mpa2" live in the same space, construct map space
7519 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7520 * with this map space as extract argument.
7522 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7523 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7524 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7525 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7528 isl_space
*space1
, *space2
;
7531 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7532 isl_multi_pw_aff_get_space(mpa2
));
7533 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7534 isl_multi_pw_aff_get_space(mpa1
));
7537 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7538 mpa2
->space
, isl_dim_out
);
7542 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7543 "range spaces don't match", goto error
);
7544 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7545 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7546 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7548 res
= order(mpa1
, mpa2
, space1
);
7549 isl_multi_pw_aff_free(mpa1
);
7550 isl_multi_pw_aff_free(mpa2
);
7553 isl_multi_pw_aff_free(mpa1
);
7554 isl_multi_pw_aff_free(mpa2
);
7558 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7559 * where the function values are equal. "space" is the space of the result.
7560 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7562 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7563 * in the sequences are equal.
7565 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7566 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7567 __isl_take isl_space
*space
)
7573 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7575 space
= isl_space_free(space
);
7576 res
= isl_map_universe(space
);
7578 for (i
= 0; i
< n
; ++i
) {
7579 isl_pw_aff
*pa1
, *pa2
;
7582 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7583 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7584 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7585 res
= isl_map_intersect(res
, map
);
7591 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7592 * where the function values are equal.
7594 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7595 __isl_take isl_multi_pw_aff
*mpa2
)
7597 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7598 &isl_multi_pw_aff_eq_map_on_space
);
7601 /* Intersect "map" with the result of applying "order"
7602 * on two copies of "mpa".
7604 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7605 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7606 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7607 __isl_take isl_multi_pw_aff
*mpa2
))
7609 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7612 /* Return the subset of "map" where the domain and the range
7613 * have equal "mpa" values.
7615 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7616 __isl_take isl_multi_pw_aff
*mpa
)
7618 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7619 &isl_multi_pw_aff_eq_map
);
7622 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7623 * where the function values of "mpa1" lexicographically satisfies
7624 * "strict_base"/"base" compared to that of "mpa2".
7625 * "space" is the space of the result.
7626 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7628 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7629 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7630 * when compared to the i-th element of "mpa2" while all previous elements are
7632 * In particular, if i corresponds to the final elements
7633 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7634 * for other values of i.
7635 * If "base" is a strict order, then "base" and "strict_base" are the same.
7637 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7638 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7639 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7640 __isl_take isl_pw_aff
*pa2
),
7641 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7642 __isl_take isl_pw_aff
*pa2
),
7643 __isl_take isl_space
*space
)
7647 isl_map
*res
, *rest
;
7649 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7651 space
= isl_space_free(space
);
7652 res
= isl_map_empty(isl_space_copy(space
));
7653 rest
= isl_map_universe(space
);
7655 for (i
= 0; i
< n
; ++i
) {
7657 isl_pw_aff
*pa1
, *pa2
;
7662 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7663 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7664 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7665 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7666 res
= isl_map_union(res
, map
);
7671 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7672 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7673 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7674 rest
= isl_map_intersect(rest
, map
);
7684 #define STRICT_ORDER lt
7685 #include "isl_aff_lex_templ.c"
7690 #define STRICT_ORDER lt
7691 #include "isl_aff_lex_templ.c"
7696 #define STRICT_ORDER gt
7697 #include "isl_aff_lex_templ.c"
7702 #define STRICT_ORDER gt
7703 #include "isl_aff_lex_templ.c"
7705 /* Compare two isl_affs.
7707 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7708 * than "aff2" and 0 if they are equal.
7710 * The order is fairly arbitrary. We do consider expressions that only involve
7711 * earlier dimensions as "smaller".
7713 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7726 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7730 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7731 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7733 return last1
- last2
;
7735 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7738 /* Compare two isl_pw_affs.
7740 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7741 * than "pa2" and 0 if they are equal.
7743 * The order is fairly arbitrary. We do consider expressions that only involve
7744 * earlier dimensions as "smaller".
7746 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7747 __isl_keep isl_pw_aff
*pa2
)
7760 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7764 if (pa1
->n
!= pa2
->n
)
7765 return pa1
->n
- pa2
->n
;
7767 for (i
= 0; i
< pa1
->n
; ++i
) {
7768 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7771 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7779 /* Return a piecewise affine expression that is equal to "v" on "domain".
7781 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7782 __isl_take isl_val
*v
)
7785 isl_local_space
*ls
;
7788 space
= isl_set_get_space(domain
);
7789 ls
= isl_local_space_from_space(space
);
7790 aff
= isl_aff_val_on_domain(ls
, v
);
7792 return isl_pw_aff_alloc(domain
, aff
);
7795 /* Return a piecewise affine expression that is equal to the parameter
7796 * with identifier "id" on "domain".
7798 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7799 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7804 space
= isl_set_get_space(domain
);
7805 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7806 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7807 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7809 return isl_pw_aff_alloc(domain
, aff
);
7812 /* Return a multi affine expression that is equal to "mv" on domain
7815 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7816 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7821 isl_local_space
*ls
;
7824 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7825 if (!space
|| n
< 0)
7828 space2
= isl_multi_val_get_space(mv
);
7829 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7830 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7831 space
= isl_space_map_from_domain_and_range(space
, space2
);
7832 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7833 ls
= isl_local_space_from_space(isl_space_domain(space
));
7834 for (i
= 0; i
< n
; ++i
) {
7838 v
= isl_multi_val_get_val(mv
, i
);
7839 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7840 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7842 isl_local_space_free(ls
);
7844 isl_multi_val_free(mv
);
7847 isl_space_free(space
);
7848 isl_multi_val_free(mv
);
7852 /* This is an alternative name for the function above.
7854 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7855 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7857 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7860 /* This function performs the same operation as
7861 * isl_multi_aff_multi_val_on_domain_space,
7862 * but is considered as a function on an isl_space when exported.
7864 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7865 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7867 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7870 /* Return a piecewise multi-affine expression
7871 * that is equal to "mv" on "domain".
7873 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7874 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7879 space
= isl_set_get_space(domain
);
7880 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7882 return isl_pw_multi_aff_alloc(domain
, ma
);
7885 /* This function performs the same operation as
7886 * isl_pw_multi_aff_multi_val_on_domain,
7887 * but is considered as a function on an isl_set when exported.
7889 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7890 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7892 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7895 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7896 * mv is the value that should be attained on each domain set
7897 * res collects the results
7899 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7901 isl_union_pw_multi_aff
*res
;
7904 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7905 * and add it to data->res.
7907 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7910 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7911 isl_pw_multi_aff
*pma
;
7914 mv
= isl_multi_val_copy(data
->mv
);
7915 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7916 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7918 return data
->res
? isl_stat_ok
: isl_stat_error
;
7921 /* Return a union piecewise multi-affine expression
7922 * that is equal to "mv" on "domain".
7924 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7925 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7927 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7930 space
= isl_union_set_get_space(domain
);
7931 data
.res
= isl_union_pw_multi_aff_empty(space
);
7933 if (isl_union_set_foreach_set(domain
,
7934 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7935 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7936 isl_union_set_free(domain
);
7937 isl_multi_val_free(mv
);
7941 /* Compute the pullback of data->pma by the function represented by "pma2",
7942 * provided the spaces match, and add the results to data->res.
7944 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7946 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7948 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7949 pma2
->dim
, isl_dim_out
)) {
7950 isl_pw_multi_aff_free(pma2
);
7954 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7955 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7957 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7959 return isl_stat_error
;
7964 /* Compute the pullback of "upma1" by the function represented by "upma2".
7966 __isl_give isl_union_pw_multi_aff
*
7967 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7968 __isl_take isl_union_pw_multi_aff
*upma1
,
7969 __isl_take isl_union_pw_multi_aff
*upma2
)
7971 return bin_op(upma1
, upma2
, &pullback_entry
);
7974 /* Apply "upma2" to "upma1".
7976 * That is, compute the pullback of "upma2" by "upma1".
7978 __isl_give isl_union_pw_multi_aff
*
7979 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7980 __isl_take isl_union_pw_multi_aff
*upma1
,
7981 __isl_take isl_union_pw_multi_aff
*upma2
)
7983 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7987 #define TYPE isl_pw_multi_aff
7989 #include "isl_copy_tuple_id_templ.c"
7991 /* Given a function "pma1" of the form A[B -> C] -> D and
7992 * a function "pma2" of the form E -> B,
7993 * replace the domain of the wrapped relation inside the domain of "pma1"
7994 * by the preimage with respect to "pma2".
7995 * In other words, plug in "pma2" in this nested domain.
7996 * The result is of the form A[E -> C] -> D.
7998 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7999 * plug that into "pma1".
8001 __isl_give isl_pw_multi_aff
*
8002 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8003 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
8005 isl_space
*pma1_space
, *pma2_space
;
8007 isl_pw_multi_aff
*id
;
8009 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
8010 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8012 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
8014 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
8015 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
8018 space
= isl_space_domain(isl_space_copy(pma1_space
));
8019 space
= isl_space_range(isl_space_unwrap(space
));
8020 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
8021 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8023 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8024 pma1_space
, isl_dim_in
);
8025 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8026 pma1_space
, isl_dim_in
);
8028 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8030 isl_pw_multi_aff_free(pma1
);
8031 isl_pw_multi_aff_free(pma2
);
8035 /* If data->pma and "pma2" are such that
8036 * data->pma is of the form A[B -> C] -> D and
8037 * "pma2" is of the form E -> B,
8038 * then replace the domain of the wrapped relation
8039 * inside the domain of data->pma by the preimage with respect to "pma2" and
8040 * add the result to data->res.
8042 static isl_stat
preimage_domain_wrapped_domain_entry(
8043 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8045 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8046 isl_space
*pma1_space
, *pma2_space
;
8049 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8050 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8052 match
= isl_space_domain_is_wrapping(pma1_space
);
8053 if (match
>= 0 && match
)
8054 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8055 isl_dim_in
, pma2_space
, isl_dim_out
);
8056 if (match
< 0 || !match
) {
8057 isl_pw_multi_aff_free(pma2
);
8058 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8061 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8062 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8064 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8066 return isl_stat_non_null(data
->res
);
8069 /* For each pair of functions A[B -> C] -> D in "upma1" and
8070 * E -> B in "upma2",
8071 * replace the domain of the wrapped relation inside the domain of the first
8072 * by the preimage with respect to the second and collect the results.
8073 * In other words, plug in the second function in this nested domain.
8074 * The results are of the form A[E -> C] -> D.
8076 __isl_give isl_union_pw_multi_aff
*
8077 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8078 __isl_take isl_union_pw_multi_aff
*upma1
,
8079 __isl_take isl_union_pw_multi_aff
*upma2
)
8081 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8084 /* Check that the domain space of "upa" matches "space".
8086 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8087 * can in principle never fail since the space "space" is that
8088 * of the isl_multi_union_pw_aff and is a set space such that
8089 * there is no domain space to match.
8091 * We check the parameters and double-check that "space" is
8092 * indeed that of a set.
8094 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8095 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8097 isl_space
*upa_space
;
8101 return isl_stat_error
;
8103 match
= isl_space_is_set(space
);
8105 return isl_stat_error
;
8107 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8108 "expecting set space", return isl_stat_error
);
8110 upa_space
= isl_union_pw_aff_get_space(upa
);
8111 match
= isl_space_has_equal_params(space
, upa_space
);
8115 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8116 "parameters don't match", goto error
);
8118 isl_space_free(upa_space
);
8121 isl_space_free(upa_space
);
8122 return isl_stat_error
;
8125 /* Do the parameters of "upa" match those of "space"?
8127 static isl_bool
isl_union_pw_aff_matching_params(
8128 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8130 isl_space
*upa_space
;
8134 return isl_bool_error
;
8136 upa_space
= isl_union_pw_aff_get_space(upa
);
8138 match
= isl_space_has_equal_params(space
, upa_space
);
8140 isl_space_free(upa_space
);
8144 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8145 * space represents the new parameters.
8146 * res collects the results.
8148 struct isl_union_pw_aff_reset_params_data
{
8150 isl_union_pw_aff
*res
;
8153 /* Replace the parameters of "pa" by data->space and
8154 * add the result to data->res.
8156 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8158 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8161 space
= isl_pw_aff_get_space(pa
);
8162 space
= isl_space_replace_params(space
, data
->space
);
8163 pa
= isl_pw_aff_reset_space(pa
, space
);
8164 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8166 return data
->res
? isl_stat_ok
: isl_stat_error
;
8169 /* Replace the domain space of "upa" by "space".
8170 * Since a union expression does not have a (single) domain space,
8171 * "space" is necessarily a parameter space.
8173 * Since the order and the names of the parameters determine
8174 * the hash value, we need to create a new hash table.
8176 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8177 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8179 struct isl_union_pw_aff_reset_params_data data
= { space
};
8182 match
= isl_union_pw_aff_matching_params(upa
, space
);
8184 upa
= isl_union_pw_aff_free(upa
);
8186 isl_space_free(space
);
8190 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8191 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8192 data
.res
= isl_union_pw_aff_free(data
.res
);
8194 isl_union_pw_aff_free(upa
);
8195 isl_space_free(space
);
8199 /* Return the floor of "pa".
8201 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8203 return isl_pw_aff_floor(pa
);
8206 /* Given f, return floor(f).
8208 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8209 __isl_take isl_union_pw_aff
*upa
)
8211 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8216 * upa mod m = upa - m * floor(upa/m)
8218 * with m an integer value.
8220 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8221 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8223 isl_union_pw_aff
*res
;
8228 if (!isl_val_is_int(m
))
8229 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8230 "expecting integer modulo", goto error
);
8231 if (!isl_val_is_pos(m
))
8232 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8233 "expecting positive modulo", goto error
);
8235 res
= isl_union_pw_aff_copy(upa
);
8236 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8237 upa
= isl_union_pw_aff_floor(upa
);
8238 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8239 res
= isl_union_pw_aff_sub(res
, upa
);
8244 isl_union_pw_aff_free(upa
);
8248 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8249 * pos is the output position that needs to be extracted.
8250 * res collects the results.
8252 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8254 isl_union_pw_aff
*res
;
8257 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8258 * (assuming it has such a dimension) and add it to data->res.
8260 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8262 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8266 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8268 return isl_stat_error
;
8269 if (data
->pos
>= n_out
) {
8270 isl_pw_multi_aff_free(pma
);
8274 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8275 isl_pw_multi_aff_free(pma
);
8277 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8279 return data
->res
? isl_stat_ok
: isl_stat_error
;
8282 /* Extract an isl_union_pw_aff corresponding to
8283 * output dimension "pos" of "upma".
8285 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8286 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8288 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8295 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8296 "cannot extract at negative position", return NULL
);
8298 space
= isl_union_pw_multi_aff_get_space(upma
);
8299 data
.res
= isl_union_pw_aff_empty(space
);
8301 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8302 &get_union_pw_aff
, &data
) < 0)
8303 data
.res
= isl_union_pw_aff_free(data
.res
);
8308 /* Return a union piecewise affine expression
8309 * that is equal to "aff" on "domain".
8311 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8312 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8316 pa
= isl_pw_aff_from_aff(aff
);
8317 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8320 /* Return a union piecewise affine expression
8321 * that is equal to the parameter identified by "id" on "domain".
8323 * Make sure the parameter appears in the space passed to
8324 * isl_aff_param_on_domain_space_id.
8326 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8327 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8332 space
= isl_union_set_get_space(domain
);
8333 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8334 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8335 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8338 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8339 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8341 * "res" collects the results.
8343 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8345 isl_union_pw_aff
*res
;
8348 /* Construct a piecewise affine expression that is equal to data->pa
8349 * on "domain" and add the result to data->res.
8351 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8353 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8357 pa
= isl_pw_aff_copy(data
->pa
);
8358 dim
= isl_set_dim(domain
, isl_dim_set
);
8360 pa
= isl_pw_aff_free(pa
);
8361 pa
= isl_pw_aff_from_range(pa
);
8362 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8363 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8364 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8365 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8367 return data
->res
? isl_stat_ok
: isl_stat_error
;
8370 /* Return a union piecewise affine expression
8371 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8372 * have been aligned.
8374 * Construct an isl_pw_aff on each of the sets in "domain" and
8375 * collect the results.
8377 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8378 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8380 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8383 space
= isl_union_set_get_space(domain
);
8384 data
.res
= isl_union_pw_aff_empty(space
);
8386 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8387 data
.res
= isl_union_pw_aff_free(data
.res
);
8388 isl_union_set_free(domain
);
8389 isl_pw_aff_free(pa
);
8393 /* Return a union piecewise affine expression
8394 * that is equal to "pa" on "domain".
8396 * Check that "pa" is a parametric expression,
8397 * align the parameters if needed and call
8398 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8400 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8401 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8404 isl_bool equal_params
;
8405 isl_space
*domain_space
, *pa_space
;
8407 pa_space
= isl_pw_aff_peek_space(pa
);
8408 is_set
= isl_space_is_set(pa_space
);
8412 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8413 "expecting parametric expression", goto error
);
8415 domain_space
= isl_union_set_get_space(domain
);
8416 pa_space
= isl_pw_aff_get_space(pa
);
8417 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8418 if (equal_params
>= 0 && !equal_params
) {
8421 space
= isl_space_align_params(domain_space
, pa_space
);
8422 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8423 domain
= isl_union_set_align_params(domain
, space
);
8425 isl_space_free(domain_space
);
8426 isl_space_free(pa_space
);
8429 if (equal_params
< 0)
8431 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8433 isl_union_set_free(domain
);
8434 isl_pw_aff_free(pa
);
8438 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8439 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8440 * "res" collects the results.
8442 struct isl_union_pw_aff_val_on_domain_data
{
8444 isl_union_pw_aff
*res
;
8447 /* Construct a piecewise affine expression that is equal to data->v
8448 * on "domain" and add the result to data->res.
8450 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8452 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8456 v
= isl_val_copy(data
->v
);
8457 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8458 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8460 return data
->res
? isl_stat_ok
: isl_stat_error
;
8463 /* Return a union piecewise affine expression
8464 * that is equal to "v" on "domain".
8466 * Construct an isl_pw_aff on each of the sets in "domain" and
8467 * collect the results.
8469 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8470 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8472 struct isl_union_pw_aff_val_on_domain_data data
;
8475 space
= isl_union_set_get_space(domain
);
8476 data
.res
= isl_union_pw_aff_empty(space
);
8478 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8479 data
.res
= isl_union_pw_aff_free(data
.res
);
8480 isl_union_set_free(domain
);
8485 /* Construct a piecewise multi affine expression
8486 * that is equal to "pa" and add it to upma.
8488 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8491 isl_union_pw_multi_aff
**upma
= user
;
8492 isl_pw_multi_aff
*pma
;
8494 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8495 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8497 return *upma
? isl_stat_ok
: isl_stat_error
;
8500 /* Construct and return a union piecewise multi affine expression
8501 * that is equal to the given union piecewise affine expression.
8503 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8504 __isl_take isl_union_pw_aff
*upa
)
8507 isl_union_pw_multi_aff
*upma
;
8512 space
= isl_union_pw_aff_get_space(upa
);
8513 upma
= isl_union_pw_multi_aff_empty(space
);
8515 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8516 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8517 upma
= isl_union_pw_multi_aff_free(upma
);
8519 isl_union_pw_aff_free(upa
);
8523 /* Compute the set of elements in the domain of "pa" where it is zero and
8524 * add this set to "uset".
8526 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8528 isl_union_set
**uset
= (isl_union_set
**)user
;
8530 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8532 return *uset
? isl_stat_ok
: isl_stat_error
;
8535 /* Return a union set containing those elements in the domain
8536 * of "upa" where it is zero.
8538 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8539 __isl_take isl_union_pw_aff
*upa
)
8541 isl_union_set
*zero
;
8543 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8544 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8545 zero
= isl_union_set_free(zero
);
8547 isl_union_pw_aff_free(upa
);
8551 /* Internal data structure for isl_union_pw_aff_bind_id,
8552 * storing the parameter that needs to be bound and
8553 * the accumulated results.
8555 struct isl_bind_id_data
{
8557 isl_union_set
*bound
;
8560 /* Bind the piecewise affine function "pa" to the parameter data->id,
8561 * adding the resulting elements in the domain where the expression
8562 * is equal to the parameter to data->bound.
8564 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8566 struct isl_bind_id_data
*data
= user
;
8569 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8570 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8572 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8575 /* Bind the union piecewise affine function "upa" to the parameter "id",
8576 * returning the elements in the domain where the expression
8577 * is equal to the parameter.
8579 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8580 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8582 struct isl_bind_id_data data
= { id
};
8584 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8585 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8586 data
.bound
= isl_union_set_free(data
.bound
);
8588 isl_union_pw_aff_free(upa
);
8593 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8594 * upma is the function that is plugged in.
8595 * pa is the current part of the function in which upma is plugged in.
8596 * res collects the results.
8598 struct isl_union_pw_aff_pullback_upma_data
{
8599 isl_union_pw_multi_aff
*upma
;
8601 isl_union_pw_aff
*res
;
8604 /* Check if "pma" can be plugged into data->pa.
8605 * If so, perform the pullback and add the result to data->res.
8607 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8609 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8612 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8613 pma
->dim
, isl_dim_out
)) {
8614 isl_pw_multi_aff_free(pma
);
8618 pa
= isl_pw_aff_copy(data
->pa
);
8619 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8621 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8623 return data
->res
? isl_stat_ok
: isl_stat_error
;
8626 /* Check if any of the elements of data->upma can be plugged into pa,
8627 * add if so add the result to data->res.
8629 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8631 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8635 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8637 isl_pw_aff_free(pa
);
8642 /* Compute the pullback of "upa" by the function represented by "upma".
8643 * In other words, plug in "upma" in "upa". The result contains
8644 * expressions defined over the domain space of "upma".
8646 * Run over all pairs of elements in "upa" and "upma", perform
8647 * the pullback when appropriate and collect the results.
8648 * If the hash value were based on the domain space rather than
8649 * the function space, then we could run through all elements
8650 * of "upma" and directly pick out the corresponding element of "upa".
8652 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8653 __isl_take isl_union_pw_aff
*upa
,
8654 __isl_take isl_union_pw_multi_aff
*upma
)
8656 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8659 space
= isl_union_pw_multi_aff_get_space(upma
);
8660 upa
= isl_union_pw_aff_align_params(upa
, space
);
8661 space
= isl_union_pw_aff_get_space(upa
);
8662 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8668 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8669 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8670 data
.res
= isl_union_pw_aff_free(data
.res
);
8672 isl_union_pw_aff_free(upa
);
8673 isl_union_pw_multi_aff_free(upma
);
8676 isl_union_pw_aff_free(upa
);
8677 isl_union_pw_multi_aff_free(upma
);
8682 #define BASE union_pw_aff
8684 #define DOMBASE union_set
8686 #include <isl_multi_explicit_domain.c>
8687 #include <isl_multi_union_pw_aff_explicit_domain.c>
8688 #include <isl_multi_templ.c>
8689 #include <isl_multi_apply_set.c>
8690 #include <isl_multi_apply_union_set.c>
8691 #include <isl_multi_arith_templ.c>
8692 #include <isl_multi_bind_templ.c>
8693 #include <isl_multi_coalesce.c>
8694 #include <isl_multi_dim_id_templ.c>
8695 #include <isl_multi_floor.c>
8696 #include <isl_multi_from_base_templ.c>
8697 #include <isl_multi_gist.c>
8698 #include <isl_multi_align_set.c>
8699 #include <isl_multi_align_union_set.c>
8700 #include <isl_multi_intersect.c>
8701 #include <isl_multi_nan_templ.c>
8702 #include <isl_multi_tuple_id_templ.c>
8703 #include <isl_multi_union_add_templ.c>
8704 #include <isl_multi_zero_space_templ.c>
8706 /* Does "mupa" have a non-trivial explicit domain?
8708 * The explicit domain, if present, is trivial if it represents
8709 * an (obviously) universe parameter set.
8711 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8712 __isl_keep isl_multi_union_pw_aff
*mupa
)
8714 isl_bool is_params
, trivial
;
8718 return isl_bool_error
;
8719 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8720 return isl_bool_false
;
8721 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8722 if (is_params
< 0 || !is_params
)
8723 return isl_bool_not(is_params
);
8724 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8725 trivial
= isl_set_plain_is_universe(set
);
8727 return isl_bool_not(trivial
);
8730 /* Construct a multiple union piecewise affine expression
8731 * in the given space with value zero in each of the output dimensions.
8733 * Since there is no canonical zero value for
8734 * a union piecewise affine expression, we can only construct
8735 * a zero-dimensional "zero" value.
8737 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8738 __isl_take isl_space
*space
)
8746 params
= isl_space_is_params(space
);
8750 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8751 "expecting proper set space", goto error
);
8752 if (!isl_space_is_set(space
))
8753 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8754 "expecting set space", goto error
);
8755 dim
= isl_space_dim(space
, isl_dim_out
);
8759 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8760 "expecting 0D space", goto error
);
8762 return isl_multi_union_pw_aff_alloc(space
);
8764 isl_space_free(space
);
8768 /* Construct and return a multi union piecewise affine expression
8769 * that is equal to the given multi affine expression.
8771 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8772 __isl_take isl_multi_aff
*ma
)
8774 isl_multi_pw_aff
*mpa
;
8776 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8777 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8780 /* This function performs the same operation as
8781 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8782 * isl_multi_aff when exported.
8784 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8785 __isl_take isl_multi_aff
*ma
)
8787 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8790 /* Construct and return a multi union piecewise affine expression
8791 * that is equal to the given multi piecewise affine expression.
8793 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8794 __isl_take isl_multi_pw_aff
*mpa
)
8799 isl_multi_union_pw_aff
*mupa
;
8801 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8803 mpa
= isl_multi_pw_aff_free(mpa
);
8807 space
= isl_multi_pw_aff_get_space(mpa
);
8808 space
= isl_space_range(space
);
8809 mupa
= isl_multi_union_pw_aff_alloc(space
);
8811 for (i
= 0; i
< n
; ++i
) {
8813 isl_union_pw_aff
*upa
;
8815 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8816 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8817 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8820 isl_multi_pw_aff_free(mpa
);
8825 /* Extract the range space of "pma" and assign it to *space.
8826 * If *space has already been set (through a previous call to this function),
8827 * then check that the range space is the same.
8829 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8831 isl_space
**space
= user
;
8832 isl_space
*pma_space
;
8835 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8836 isl_pw_multi_aff_free(pma
);
8839 return isl_stat_error
;
8845 equal
= isl_space_is_equal(pma_space
, *space
);
8846 isl_space_free(pma_space
);
8849 return isl_stat_error
;
8851 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8852 "range spaces not the same", return isl_stat_error
);
8856 /* Construct and return a multi union piecewise affine expression
8857 * that is equal to the given union piecewise multi affine expression.
8859 * In order to be able to perform the conversion, the input
8860 * needs to be non-empty and may only involve a single range space.
8862 * If the resulting multi union piecewise affine expression has
8863 * an explicit domain, then assign it the domain of the input.
8864 * In other cases, the domain is stored in the individual elements.
8866 __isl_give isl_multi_union_pw_aff
*
8867 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8868 __isl_take isl_union_pw_multi_aff
*upma
)
8870 isl_space
*space
= NULL
;
8871 isl_multi_union_pw_aff
*mupa
;
8875 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8879 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8880 "cannot extract range space from empty input",
8882 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8889 n
= isl_space_dim(space
, isl_dim_set
);
8891 space
= isl_space_free(space
);
8892 mupa
= isl_multi_union_pw_aff_alloc(space
);
8894 for (i
= 0; i
< n
; ++i
) {
8895 isl_union_pw_aff
*upa
;
8897 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8898 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8900 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8902 isl_union_pw_multi_aff
*copy
;
8904 copy
= isl_union_pw_multi_aff_copy(upma
);
8905 dom
= isl_union_pw_multi_aff_domain(copy
);
8906 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8909 isl_union_pw_multi_aff_free(upma
);
8912 isl_space_free(space
);
8913 isl_union_pw_multi_aff_free(upma
);
8917 /* This function performs the same operation as
8918 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8919 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8921 __isl_give isl_multi_union_pw_aff
*
8922 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8923 __isl_take isl_union_pw_multi_aff
*upma
)
8925 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8928 /* Try and create an isl_multi_union_pw_aff that is equivalent
8929 * to the given isl_union_map.
8930 * The isl_union_map is required to be single-valued in each space.
8931 * Moreover, it cannot be empty and all range spaces need to be the same.
8932 * Otherwise, an error is produced.
8934 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8935 __isl_take isl_union_map
*umap
)
8937 isl_union_pw_multi_aff
*upma
;
8939 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8940 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8943 /* This function performs the same operation as
8944 * isl_multi_union_pw_aff_from_union_map,
8945 * but is considered as a function on an isl_union_map when exported.
8947 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8948 __isl_take isl_union_map
*umap
)
8950 return isl_multi_union_pw_aff_from_union_map(umap
);
8953 /* Return a multiple union piecewise affine expression
8954 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8955 * have been aligned.
8957 * If the resulting multi union piecewise affine expression has
8958 * an explicit domain, then assign it the input domain.
8959 * In other cases, the domain is stored in the individual elements.
8961 static __isl_give isl_multi_union_pw_aff
*
8962 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8963 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8968 isl_multi_union_pw_aff
*mupa
;
8970 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8971 if (!domain
|| n
< 0)
8974 space
= isl_multi_val_get_space(mv
);
8975 mupa
= isl_multi_union_pw_aff_alloc(space
);
8976 for (i
= 0; i
< n
; ++i
) {
8978 isl_union_pw_aff
*upa
;
8980 v
= isl_multi_val_get_val(mv
, i
);
8981 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8983 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8985 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8986 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8987 isl_union_set_copy(domain
));
8989 isl_union_set_free(domain
);
8990 isl_multi_val_free(mv
);
8993 isl_union_set_free(domain
);
8994 isl_multi_val_free(mv
);
8998 /* Return a multiple union piecewise affine expression
8999 * that is equal to "mv" on "domain".
9001 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
9002 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9004 isl_bool equal_params
;
9008 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
9009 if (equal_params
< 0)
9012 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9014 domain
= isl_union_set_align_params(domain
,
9015 isl_multi_val_get_space(mv
));
9016 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9017 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9019 isl_union_set_free(domain
);
9020 isl_multi_val_free(mv
);
9024 /* Return a multiple union piecewise affine expression
9025 * that is equal to "ma" on "domain".
9027 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9028 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9030 isl_pw_multi_aff
*pma
;
9032 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9033 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9036 /* Return a multiple union piecewise affine expression
9037 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9038 * have been aligned.
9040 * If the resulting multi union piecewise affine expression has
9041 * an explicit domain, then assign it the input domain.
9042 * In other cases, the domain is stored in the individual elements.
9044 static __isl_give isl_multi_union_pw_aff
*
9045 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9046 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9051 isl_multi_union_pw_aff
*mupa
;
9053 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9054 if (!domain
|| n
< 0)
9056 space
= isl_pw_multi_aff_get_space(pma
);
9057 mupa
= isl_multi_union_pw_aff_alloc(space
);
9058 for (i
= 0; i
< n
; ++i
) {
9060 isl_union_pw_aff
*upa
;
9062 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9063 upa
= isl_union_pw_aff_pw_aff_on_domain(
9064 isl_union_set_copy(domain
), pa
);
9065 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9067 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9068 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9069 isl_union_set_copy(domain
));
9071 isl_union_set_free(domain
);
9072 isl_pw_multi_aff_free(pma
);
9075 isl_union_set_free(domain
);
9076 isl_pw_multi_aff_free(pma
);
9080 /* Return a multiple union piecewise affine expression
9081 * that is equal to "pma" on "domain".
9083 __isl_give isl_multi_union_pw_aff
*
9084 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9085 __isl_take isl_pw_multi_aff
*pma
)
9087 isl_bool equal_params
;
9090 space
= isl_pw_multi_aff_peek_space(pma
);
9091 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9092 if (equal_params
< 0)
9095 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9097 domain
= isl_union_set_align_params(domain
,
9098 isl_pw_multi_aff_get_space(pma
));
9099 pma
= isl_pw_multi_aff_align_params(pma
,
9100 isl_union_set_get_space(domain
));
9101 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9104 isl_union_set_free(domain
);
9105 isl_pw_multi_aff_free(pma
);
9109 /* Return a union set containing those elements in the domains
9110 * of the elements of "mupa" where they are all zero.
9112 * If there are no elements, then simply return the entire domain.
9114 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9115 __isl_take isl_multi_union_pw_aff
*mupa
)
9119 isl_union_pw_aff
*upa
;
9120 isl_union_set
*zero
;
9122 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9124 mupa
= isl_multi_union_pw_aff_free(mupa
);
9129 return isl_multi_union_pw_aff_domain(mupa
);
9131 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9132 zero
= isl_union_pw_aff_zero_union_set(upa
);
9134 for (i
= 1; i
< n
; ++i
) {
9135 isl_union_set
*zero_i
;
9137 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9138 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9140 zero
= isl_union_set_intersect(zero
, zero_i
);
9143 isl_multi_union_pw_aff_free(mupa
);
9147 /* Construct a union map mapping the shared domain
9148 * of the union piecewise affine expressions to the range of "mupa"
9149 * in the special case of a 0D multi union piecewise affine expression.
9151 * Construct a map between the explicit domain of "mupa" and
9153 * Note that this assumes that the domain consists of explicit elements.
9155 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9156 __isl_take isl_multi_union_pw_aff
*mupa
)
9160 isl_union_set
*dom
, *ran
;
9162 space
= isl_multi_union_pw_aff_get_space(mupa
);
9163 dom
= isl_multi_union_pw_aff_domain(mupa
);
9164 ran
= isl_union_set_from_set(isl_set_universe(space
));
9166 is_params
= isl_union_set_is_params(dom
);
9168 dom
= isl_union_set_free(dom
);
9170 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9171 "cannot create union map from expression without "
9172 "explicit domain elements",
9173 dom
= isl_union_set_free(dom
));
9175 return isl_union_map_from_domain_and_range(dom
, ran
);
9178 /* Construct a union map mapping the shared domain
9179 * of the union piecewise affine expressions to the range of "mupa"
9180 * with each dimension in the range equated to the
9181 * corresponding union piecewise affine expression.
9183 * If the input is zero-dimensional, then construct a mapping
9184 * from its explicit domain.
9186 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9187 __isl_take isl_multi_union_pw_aff
*mupa
)
9192 isl_union_map
*umap
;
9193 isl_union_pw_aff
*upa
;
9195 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9197 mupa
= isl_multi_union_pw_aff_free(mupa
);
9202 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9204 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9205 umap
= isl_union_map_from_union_pw_aff(upa
);
9207 for (i
= 1; i
< n
; ++i
) {
9208 isl_union_map
*umap_i
;
9210 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9211 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9212 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9215 space
= isl_multi_union_pw_aff_get_space(mupa
);
9216 umap
= isl_union_map_reset_range_space(umap
, space
);
9218 isl_multi_union_pw_aff_free(mupa
);
9222 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9223 * "range" is the space from which to set the range space.
9224 * "res" collects the results.
9226 struct isl_union_pw_multi_aff_reset_range_space_data
{
9228 isl_union_pw_multi_aff
*res
;
9231 /* Replace the range space of "pma" by the range space of data->range and
9232 * add the result to data->res.
9234 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9236 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9239 space
= isl_pw_multi_aff_get_space(pma
);
9240 space
= isl_space_domain(space
);
9241 space
= isl_space_extend_domain_with_range(space
,
9242 isl_space_copy(data
->range
));
9243 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9244 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9246 return data
->res
? isl_stat_ok
: isl_stat_error
;
9249 /* Replace the range space of all the piecewise affine expressions in "upma" by
9250 * the range space of "space".
9252 * This assumes that all these expressions have the same output dimension.
9254 * Since the spaces of the expressions change, so do their hash values.
9255 * We therefore need to create a new isl_union_pw_multi_aff.
9256 * Note that the hash value is currently computed based on the entire
9257 * space even though there can only be a single expression with a given
9260 static __isl_give isl_union_pw_multi_aff
*
9261 isl_union_pw_multi_aff_reset_range_space(
9262 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9264 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9265 isl_space
*space_upma
;
9267 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9268 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9269 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9270 &reset_range_space
, &data
) < 0)
9271 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9273 isl_space_free(space
);
9274 isl_union_pw_multi_aff_free(upma
);
9278 /* Construct and return a union piecewise multi affine expression
9279 * that is equal to the given multi union piecewise affine expression,
9280 * in the special case of a 0D multi union piecewise affine expression.
9282 * Construct a union piecewise multi affine expression
9283 * on top of the explicit domain of the input.
9285 __isl_give isl_union_pw_multi_aff
*
9286 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9287 __isl_take isl_multi_union_pw_aff
*mupa
)
9291 isl_union_set
*domain
;
9293 space
= isl_multi_union_pw_aff_get_space(mupa
);
9294 mv
= isl_multi_val_zero(space
);
9295 domain
= isl_multi_union_pw_aff_domain(mupa
);
9296 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9299 /* Construct and return a union piecewise multi affine expression
9300 * that is equal to the given multi union piecewise affine expression.
9302 * If the input is zero-dimensional, then
9303 * construct a union piecewise multi affine expression
9304 * on top of the explicit domain of the input.
9306 __isl_give isl_union_pw_multi_aff
*
9307 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9308 __isl_take isl_multi_union_pw_aff
*mupa
)
9313 isl_union_pw_multi_aff
*upma
;
9314 isl_union_pw_aff
*upa
;
9316 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9318 mupa
= isl_multi_union_pw_aff_free(mupa
);
9323 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9325 space
= isl_multi_union_pw_aff_get_space(mupa
);
9326 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9327 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9329 for (i
= 1; i
< n
; ++i
) {
9330 isl_union_pw_multi_aff
*upma_i
;
9332 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9333 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9334 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9337 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9339 isl_multi_union_pw_aff_free(mupa
);
9343 /* Intersect the range of "mupa" with "range",
9344 * in the special case where "mupa" is 0D.
9346 * Intersect the domain of "mupa" with the constraints on the parameters
9349 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9350 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9352 range
= isl_set_params(range
);
9353 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9357 /* Intersect the range of "mupa" with "range".
9358 * That is, keep only those domain elements that have a function value
9361 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9362 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9364 isl_union_pw_multi_aff
*upma
;
9365 isl_union_set
*domain
;
9370 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9371 if (n
< 0 || !range
)
9374 space
= isl_set_get_space(range
);
9375 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9376 space
, isl_dim_set
);
9377 isl_space_free(space
);
9381 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9382 "space don't match", goto error
);
9384 return mupa_intersect_range_0D(mupa
, range
);
9386 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9387 isl_multi_union_pw_aff_copy(mupa
));
9388 domain
= isl_union_set_from_set(range
);
9389 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9390 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9394 isl_multi_union_pw_aff_free(mupa
);
9395 isl_set_free(range
);
9399 /* Return the shared domain of the elements of "mupa",
9400 * in the special case where "mupa" is zero-dimensional.
9402 * Return the explicit domain of "mupa".
9403 * Note that this domain may be a parameter set, either
9404 * because "mupa" is meant to live in a set space or
9405 * because no explicit domain has been set.
9407 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9408 __isl_take isl_multi_union_pw_aff
*mupa
)
9412 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9413 isl_multi_union_pw_aff_free(mupa
);
9418 /* Return the shared domain of the elements of "mupa".
9420 * If "mupa" is zero-dimensional, then return its explicit domain.
9422 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9423 __isl_take isl_multi_union_pw_aff
*mupa
)
9427 isl_union_pw_aff
*upa
;
9430 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9432 mupa
= isl_multi_union_pw_aff_free(mupa
);
9437 return isl_multi_union_pw_aff_domain_0D(mupa
);
9439 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9440 dom
= isl_union_pw_aff_domain(upa
);
9441 for (i
= 1; i
< n
; ++i
) {
9442 isl_union_set
*dom_i
;
9444 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9445 dom_i
= isl_union_pw_aff_domain(upa
);
9446 dom
= isl_union_set_intersect(dom
, dom_i
);
9449 isl_multi_union_pw_aff_free(mupa
);
9453 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9454 * In particular, the spaces have been aligned.
9455 * The result is defined over the shared domain of the elements of "mupa"
9457 * We first extract the parametric constant part of "aff" and
9458 * define that over the shared domain.
9459 * Then we iterate over all input dimensions of "aff" and add the corresponding
9460 * multiples of the elements of "mupa".
9461 * Finally, we consider the integer divisions, calling the function
9462 * recursively to obtain an isl_union_pw_aff corresponding to the
9463 * integer division argument.
9465 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9466 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9469 isl_size n_in
, n_div
;
9470 isl_union_pw_aff
*upa
;
9471 isl_union_set
*uset
;
9475 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9476 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9477 if (n_in
< 0 || n_div
< 0)
9480 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9481 cst
= isl_aff_copy(aff
);
9482 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9483 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9484 cst
= isl_aff_project_domain_on_params(cst
);
9485 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9487 for (i
= 0; i
< n_in
; ++i
) {
9488 isl_union_pw_aff
*upa_i
;
9490 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9492 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9493 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9494 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9495 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9498 for (i
= 0; i
< n_div
; ++i
) {
9500 isl_union_pw_aff
*upa_i
;
9502 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9504 div
= isl_aff_get_div(aff
, i
);
9505 upa_i
= multi_union_pw_aff_apply_aff(
9506 isl_multi_union_pw_aff_copy(mupa
), div
);
9507 upa_i
= isl_union_pw_aff_floor(upa_i
);
9508 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9509 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9510 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9513 isl_multi_union_pw_aff_free(mupa
);
9518 isl_multi_union_pw_aff_free(mupa
);
9523 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9524 * with the domain of "aff".
9525 * Furthermore, the dimension of this space needs to be greater than zero.
9526 * The result is defined over the shared domain of the elements of "mupa"
9528 * We perform these checks and then hand over control to
9529 * multi_union_pw_aff_apply_aff.
9531 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9532 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9535 isl_space
*space1
, *space2
;
9538 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9539 isl_aff_get_space(aff
));
9540 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9544 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9545 space2
= isl_aff_get_domain_space(aff
);
9546 equal
= isl_space_is_equal(space1
, space2
);
9547 isl_space_free(space1
);
9548 isl_space_free(space2
);
9552 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9553 "spaces don't match", goto error
);
9554 dim
= isl_aff_dim(aff
, isl_dim_in
);
9558 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9559 "cannot determine domains", goto error
);
9561 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9563 isl_multi_union_pw_aff_free(mupa
);
9568 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9569 * The space of "mupa" is known to be compatible with the domain of "ma".
9571 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9572 * on the domain of "mupa".
9574 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9575 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9579 dom
= isl_multi_union_pw_aff_domain(mupa
);
9580 ma
= isl_multi_aff_project_domain_on_params(ma
);
9582 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9585 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9586 * with the domain of "ma".
9587 * The result is defined over the shared domain of the elements of "mupa"
9589 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9590 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9592 isl_space
*space1
, *space2
;
9593 isl_multi_union_pw_aff
*res
;
9596 isl_size n_in
, n_out
;
9598 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9599 isl_multi_aff_get_space(ma
));
9600 ma
= isl_multi_aff_align_params(ma
,
9601 isl_multi_union_pw_aff_get_space(mupa
));
9602 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9603 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9604 if (!mupa
|| n_in
< 0 || n_out
< 0)
9607 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9608 space2
= isl_multi_aff_get_domain_space(ma
);
9609 equal
= isl_space_is_equal(space1
, space2
);
9610 isl_space_free(space1
);
9611 isl_space_free(space2
);
9615 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9616 "spaces don't match", goto error
);
9618 return mupa_apply_multi_aff_0D(mupa
, ma
);
9620 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9621 res
= isl_multi_union_pw_aff_alloc(space1
);
9623 for (i
= 0; i
< n_out
; ++i
) {
9625 isl_union_pw_aff
*upa
;
9627 aff
= isl_multi_aff_get_aff(ma
, i
);
9628 upa
= multi_union_pw_aff_apply_aff(
9629 isl_multi_union_pw_aff_copy(mupa
), aff
);
9630 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9633 isl_multi_aff_free(ma
);
9634 isl_multi_union_pw_aff_free(mupa
);
9637 isl_multi_union_pw_aff_free(mupa
);
9638 isl_multi_aff_free(ma
);
9642 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9643 * The space of "mupa" is known to be compatible with the domain of "pa".
9645 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9646 * on the domain of "mupa".
9648 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9649 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9653 dom
= isl_multi_union_pw_aff_domain(mupa
);
9654 pa
= isl_pw_aff_project_domain_on_params(pa
);
9656 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9659 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9660 * with the domain of "pa".
9661 * Furthermore, the dimension of this space needs to be greater than zero.
9662 * The result is defined over the shared domain of the elements of "mupa"
9664 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9665 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9670 isl_space
*space
, *space2
;
9671 isl_union_pw_aff
*upa
;
9673 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9674 isl_pw_aff_get_space(pa
));
9675 pa
= isl_pw_aff_align_params(pa
,
9676 isl_multi_union_pw_aff_get_space(mupa
));
9680 space
= isl_multi_union_pw_aff_get_space(mupa
);
9681 space2
= isl_pw_aff_get_domain_space(pa
);
9682 equal
= isl_space_is_equal(space
, space2
);
9683 isl_space_free(space
);
9684 isl_space_free(space2
);
9688 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9689 "spaces don't match", goto error
);
9690 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9694 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9696 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9697 upa
= isl_union_pw_aff_empty(space
);
9699 for (i
= 0; i
< pa
->n
; ++i
) {
9702 isl_multi_union_pw_aff
*mupa_i
;
9703 isl_union_pw_aff
*upa_i
;
9705 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9706 domain
= isl_set_copy(pa
->p
[i
].set
);
9707 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9708 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9709 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9710 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9713 isl_multi_union_pw_aff_free(mupa
);
9714 isl_pw_aff_free(pa
);
9717 isl_multi_union_pw_aff_free(mupa
);
9718 isl_pw_aff_free(pa
);
9722 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9723 * The space of "mupa" is known to be compatible with the domain of "pma".
9725 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9726 * on the domain of "mupa".
9728 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9729 __isl_take isl_multi_union_pw_aff
*mupa
,
9730 __isl_take isl_pw_multi_aff
*pma
)
9734 dom
= isl_multi_union_pw_aff_domain(mupa
);
9735 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9737 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9740 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9741 * with the domain of "pma".
9742 * The result is defined over the shared domain of the elements of "mupa"
9744 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9745 __isl_take isl_multi_union_pw_aff
*mupa
,
9746 __isl_take isl_pw_multi_aff
*pma
)
9748 isl_space
*space1
, *space2
;
9749 isl_multi_union_pw_aff
*res
;
9752 isl_size n_in
, n_out
;
9754 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9755 isl_pw_multi_aff_get_space(pma
));
9756 pma
= isl_pw_multi_aff_align_params(pma
,
9757 isl_multi_union_pw_aff_get_space(mupa
));
9761 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9762 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9763 equal
= isl_space_is_equal(space1
, space2
);
9764 isl_space_free(space1
);
9765 isl_space_free(space2
);
9769 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9770 "spaces don't match", goto error
);
9771 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9772 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9773 if (n_in
< 0 || n_out
< 0)
9776 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9778 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9779 res
= isl_multi_union_pw_aff_alloc(space1
);
9781 for (i
= 0; i
< n_out
; ++i
) {
9783 isl_union_pw_aff
*upa
;
9785 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9786 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9787 isl_multi_union_pw_aff_copy(mupa
), pa
);
9788 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9791 isl_pw_multi_aff_free(pma
);
9792 isl_multi_union_pw_aff_free(mupa
);
9795 isl_multi_union_pw_aff_free(mupa
);
9796 isl_pw_multi_aff_free(pma
);
9800 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9801 * If the explicit domain only keeps track of constraints on the parameters,
9802 * then only update those constraints.
9804 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9805 __isl_take isl_multi_union_pw_aff
*mupa
,
9806 __isl_keep isl_union_pw_multi_aff
*upma
)
9810 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9811 return isl_multi_union_pw_aff_free(mupa
);
9813 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9817 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9819 return isl_multi_union_pw_aff_free(mupa
);
9821 upma
= isl_union_pw_multi_aff_copy(upma
);
9823 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9824 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9826 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9829 return isl_multi_union_pw_aff_free(mupa
);
9833 /* Compute the pullback of "mupa" by the function represented by "upma".
9834 * In other words, plug in "upma" in "mupa". The result contains
9835 * expressions defined over the domain space of "upma".
9837 * Run over all elements of "mupa" and plug in "upma" in each of them.
9839 * If "mupa" has an explicit domain, then it is this domain
9840 * that needs to undergo a pullback instead, i.e., a preimage.
9842 __isl_give isl_multi_union_pw_aff
*
9843 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9844 __isl_take isl_multi_union_pw_aff
*mupa
,
9845 __isl_take isl_union_pw_multi_aff
*upma
)
9850 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9851 isl_union_pw_multi_aff_get_space(upma
));
9852 upma
= isl_union_pw_multi_aff_align_params(upma
,
9853 isl_multi_union_pw_aff_get_space(mupa
));
9854 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9855 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9859 for (i
= 0; i
< n
; ++i
) {
9860 isl_union_pw_aff
*upa
;
9862 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9863 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9864 isl_union_pw_multi_aff_copy(upma
));
9865 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9868 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9869 mupa
= preimage_explicit_domain(mupa
, upma
);
9871 isl_union_pw_multi_aff_free(upma
);
9874 isl_multi_union_pw_aff_free(mupa
);
9875 isl_union_pw_multi_aff_free(upma
);
9879 /* Extract the sequence of elements in "mupa" with domain space "space"
9880 * (ignoring parameters).
9882 * For the elements of "mupa" that are not defined on the specified space,
9883 * the corresponding element in the result is empty.
9885 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9886 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9890 isl_space
*space_mpa
;
9891 isl_multi_pw_aff
*mpa
;
9893 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9894 if (n
< 0 || !space
)
9897 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9898 space
= isl_space_replace_params(space
, space_mpa
);
9899 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9901 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9903 space
= isl_space_from_domain(space
);
9904 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9905 for (i
= 0; i
< n
; ++i
) {
9906 isl_union_pw_aff
*upa
;
9909 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9910 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9911 isl_space_copy(space
));
9912 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9913 isl_union_pw_aff_free(upa
);
9916 isl_space_free(space
);
9919 isl_space_free(space
);
9923 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9924 * should modify the base expressions in the input.
9926 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9927 * are taken into account.
9928 * "fn" is applied to each entry in the input.
9930 struct isl_union_pw_multi_aff_un_op_control
{
9931 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9932 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9935 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9936 * a second argument) for use as an isl_union_pw_multi_aff_transform
9937 * filter function (which does take a second argument).
9938 * Simply call control->filter without the second argument.
9940 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9941 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9943 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9945 return control
->filter(pma
);
9948 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9949 * a second argument) for use as an isl_union_pw_multi_aff_transform
9950 * base function (which does take a second argument).
9951 * Simply call control->fn without the second argument.
9953 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9954 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9956 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9958 return control
->fn(pma
);
9961 /* Construct an isl_union_pw_multi_aff that is obtained by
9962 * modifying "upma" according to "control".
9964 * isl_union_pw_multi_aff_transform performs essentially
9965 * the same operation, but takes a filter and a callback function
9966 * of a different form (with an extra argument).
9967 * Call isl_union_pw_multi_aff_transform with wrappers
9968 * that remove this extra argument.
9970 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9971 __isl_take isl_union_pw_multi_aff
*upma
,
9972 struct isl_union_pw_multi_aff_un_op_control
*control
)
9974 struct isl_union_pw_multi_aff_transform_control t_control
= {
9975 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9976 .filter_user
= control
,
9977 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9981 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9984 /* For each function in "upma" of the form A -> [B -> C],
9985 * extract the function A -> B and collect the results.
9987 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9988 __isl_take isl_union_pw_multi_aff
*upma
)
9990 struct isl_union_pw_multi_aff_un_op_control control
= {
9991 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9992 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9994 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9997 /* For each function in "upma" of the form A -> [B -> C],
9998 * extract the function A -> C and collect the results.
10000 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
10001 __isl_take isl_union_pw_multi_aff
*upma
)
10003 struct isl_union_pw_multi_aff_un_op_control control
= {
10004 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10005 .fn
= &isl_pw_multi_aff_range_factor_range
,
10007 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10010 /* Evaluate the affine function "aff" in the void point "pnt".
10011 * In particular, return the value NaN.
10013 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10014 __isl_take isl_point
*pnt
)
10018 ctx
= isl_point_get_ctx(pnt
);
10020 isl_point_free(pnt
);
10021 return isl_val_nan(ctx
);
10024 /* Evaluate the affine expression "aff"
10025 * in the coordinates (with denominator) "pnt".
10027 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10028 __isl_keep isl_vec
*pnt
)
10037 ctx
= isl_vec_get_ctx(aff
);
10040 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10041 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10042 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10043 v
= isl_val_normalize(v
);
10050 /* Check that the domain space of "aff" is equal to "space".
10052 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10053 __isl_keep isl_space
*space
)
10057 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10059 return isl_stat_error
;
10061 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10062 "incompatible spaces", return isl_stat_error
);
10063 return isl_stat_ok
;
10066 /* Evaluate the affine function "aff" in "pnt".
10068 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10069 __isl_take isl_point
*pnt
)
10073 isl_local_space
*ls
;
10075 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10077 is_void
= isl_point_is_void(pnt
);
10081 return eval_void(aff
, pnt
);
10083 ls
= isl_aff_get_domain_local_space(aff
);
10084 pnt
= isl_local_space_lift_point(ls
, pnt
);
10086 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10089 isl_point_free(pnt
);
10094 isl_point_free(pnt
);