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
9 * Copyright 2022 Cerebras Systems
11 * Use of this software is governed by the MIT license
13 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
14 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_union_map_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_space_private.h>
28 #include <isl_local_space_private.h>
29 #include <isl_vec_private.h>
30 #include <isl_mat_private.h>
31 #include <isl_id_private.h>
32 #include <isl/constraint.h>
35 #include <isl_val_private.h>
36 #include <isl_point_private.h>
37 #include <isl_config.h>
42 #include <isl_list_templ.c>
43 #include <isl_list_read_templ.c>
46 #define EL_BASE pw_aff
48 #include <isl_list_templ.c>
49 #include <isl_list_read_templ.c>
52 #define EL_BASE pw_multi_aff
54 #include <isl_list_templ.c>
55 #include <isl_list_read_templ.c>
58 #define EL_BASE union_pw_aff
60 #include <isl_list_templ.c>
61 #include <isl_list_read_templ.c>
64 #define EL_BASE union_pw_multi_aff
66 #include <isl_list_templ.c>
68 /* Construct an isl_aff from the given domain local space "ls" and
69 * coefficients "v", where the local space is known to be valid
70 * for an affine expression.
72 static __isl_give isl_aff
*isl_aff_alloc_vec_validated(
73 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
80 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
90 isl_local_space_free(ls
);
95 /* Construct an isl_aff from the given domain local space "ls" and
98 * First check that "ls" is a valid domain local space
99 * for an affine expression.
101 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
102 __isl_take isl_vec
*v
)
109 ctx
= isl_local_space_get_ctx(ls
);
110 if (!isl_local_space_divs_known(ls
))
111 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
113 if (!isl_local_space_is_set(ls
))
114 isl_die(ctx
, isl_error_invalid
,
115 "domain of affine expression should be a set",
117 return isl_aff_alloc_vec_validated(ls
, v
);
119 isl_local_space_free(ls
);
124 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
133 ctx
= isl_local_space_get_ctx(ls
);
135 total
= isl_local_space_dim(ls
, isl_dim_all
);
138 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
139 return isl_aff_alloc_vec(ls
, v
);
141 isl_local_space_free(ls
);
145 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
154 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
159 return isl_aff_alloc_vec_validated(isl_local_space_copy(aff
->ls
),
160 isl_vec_copy(aff
->v
));
163 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
171 return isl_aff_dup(aff
);
174 /* Return a copy of the rational affine expression of "aff".
176 static __isl_give isl_vec
*isl_aff_get_rat_aff(__isl_keep isl_aff
*aff
)
180 return isl_vec_copy(aff
->v
);
183 /* Return the rational affine expression of "aff".
184 * This may be either a copy or the expression itself
185 * if there is only one reference to "aff".
186 * This allows the expression to be modified inplace
187 * if both the "aff" and its expression have only a single reference.
188 * The caller is not allowed to modify "aff" between this call and
189 * a subsequent call to isl_aff_restore_rat_aff.
190 * The only exception is that isl_aff_free can be called instead.
192 static __isl_give isl_vec
*isl_aff_take_rat_aff(__isl_keep isl_aff
*aff
)
199 return isl_aff_get_rat_aff(aff
);
205 /* Set the rational affine expression of "aff" to "v",
206 * where the rational affine expression of "aff" may be missing
207 * due to a preceding call to isl_aff_take_rat_aff.
208 * However, in this case, "aff" only has a single reference and
209 * then the call to isl_aff_cow has no effect.
211 static __isl_give isl_aff
*isl_aff_restore_rat_aff(__isl_keep isl_aff
*aff
,
212 __isl_take isl_vec
*v
)
222 aff
= isl_aff_cow(aff
);
225 isl_vec_free(aff
->v
);
235 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
239 aff
= isl_aff_alloc(ls
);
243 isl_int_set_si(aff
->v
->el
[0], 1);
244 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
249 /* Return an affine expression that is equal to zero on domain space "space".
251 __isl_give isl_aff
*isl_aff_zero_on_domain_space(__isl_take isl_space
*space
)
253 return isl_aff_zero_on_domain(isl_local_space_from_space(space
));
256 /* This function performs the same operation as isl_aff_zero_on_domain_space,
257 * but is considered as a function on an isl_space when exported.
259 __isl_give isl_aff
*isl_space_zero_aff_on_domain(__isl_take isl_space
*space
)
261 return isl_aff_zero_on_domain_space(space
);
264 /* Return a piecewise affine expression defined on the specified domain
265 * that is equal to zero.
267 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
269 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
272 /* Change "aff" into a NaN.
274 * Note that this function gets called from isl_aff_nan_on_domain,
275 * so "aff" may not have been initialized yet.
277 static __isl_give isl_aff
*isl_aff_set_nan(__isl_take isl_aff
*aff
)
281 v
= isl_aff_take_rat_aff(aff
);
283 aff
= isl_aff_restore_rat_aff(aff
, v
);
288 /* Return an affine expression defined on the specified domain
289 * that represents NaN.
291 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
295 aff
= isl_aff_alloc(ls
);
296 return isl_aff_set_nan(aff
);
299 /* Return an affine expression defined on the specified domain space
300 * that represents NaN.
302 __isl_give isl_aff
*isl_aff_nan_on_domain_space(__isl_take isl_space
*space
)
304 return isl_aff_nan_on_domain(isl_local_space_from_space(space
));
307 /* Return a piecewise affine expression defined on the specified domain space
308 * that represents NaN.
310 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain_space(
311 __isl_take isl_space
*space
)
313 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space
));
316 /* Return a piecewise affine expression defined on the specified domain
317 * that represents NaN.
319 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
321 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
324 /* Return an affine expression that is equal to "val" on
325 * domain local space "ls".
327 * Note that the encoding for the special value NaN
328 * is the same in isl_val and isl_aff, so this does not need
329 * to be treated in any special way.
331 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
332 __isl_take isl_val
*val
)
338 if (!isl_val_is_rat(val
) && !isl_val_is_nan(val
))
339 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
340 "expecting rational value or NaN", goto error
);
342 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
346 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
347 isl_int_set(aff
->v
->el
[1], val
->n
);
348 isl_int_set(aff
->v
->el
[0], val
->d
);
350 isl_local_space_free(ls
);
354 isl_local_space_free(ls
);
359 /* Return an affine expression that is equal to "val" on domain space "space".
361 __isl_give isl_aff
*isl_aff_val_on_domain_space(__isl_take isl_space
*space
,
362 __isl_take isl_val
*val
)
364 return isl_aff_val_on_domain(isl_local_space_from_space(space
), val
);
367 /* Return an affine expression that is equal to the specified dimension
370 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
371 enum isl_dim_type type
, unsigned pos
)
379 space
= isl_local_space_get_space(ls
);
382 if (isl_space_is_map(space
))
383 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
384 "expecting (parameter) set space", goto error
);
385 if (isl_local_space_check_range(ls
, type
, pos
, 1) < 0)
388 isl_space_free(space
);
389 aff
= isl_aff_alloc(ls
);
393 pos
+= isl_local_space_offset(aff
->ls
, type
);
395 isl_int_set_si(aff
->v
->el
[0], 1);
396 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
397 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
401 isl_local_space_free(ls
);
402 isl_space_free(space
);
406 /* Return a piecewise affine expression that is equal to
407 * the specified dimension in "ls".
409 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
410 enum isl_dim_type type
, unsigned pos
)
412 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
415 /* Return an affine expression that is equal to the parameter
416 * in the domain space "space" with identifier "id".
418 __isl_give isl_aff
*isl_aff_param_on_domain_space_id(
419 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
426 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
428 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
429 "parameter not found in space", goto error
);
431 ls
= isl_local_space_from_space(space
);
432 return isl_aff_var_on_domain(ls
, isl_dim_param
, pos
);
434 isl_space_free(space
);
439 /* This function performs the same operation as
440 * isl_aff_param_on_domain_space_id,
441 * but is considered as a function on an isl_space when exported.
443 __isl_give isl_aff
*isl_space_param_aff_on_domain_id(
444 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
446 return isl_aff_param_on_domain_space_id(space
, id
);
449 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
457 isl_local_space_free(aff
->ls
);
458 isl_vec_free(aff
->v
);
465 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
467 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
470 /* Return a hash value that digests "aff".
472 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
474 uint32_t hash
, ls_hash
, v_hash
;
479 hash
= isl_hash_init();
480 ls_hash
= isl_local_space_get_hash(aff
->ls
);
481 isl_hash_hash(hash
, ls_hash
);
482 v_hash
= isl_vec_get_hash(aff
->v
);
483 isl_hash_hash(hash
, v_hash
);
488 /* Return the domain local space of "aff".
490 static __isl_keep isl_local_space
*isl_aff_peek_domain_local_space(
491 __isl_keep isl_aff
*aff
)
493 return aff
? aff
->ls
: NULL
;
496 /* Return the number of variables of the given type in the domain of "aff".
498 isl_size
isl_aff_domain_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
502 ls
= isl_aff_peek_domain_local_space(aff
);
503 return isl_local_space_dim(ls
, type
);
506 /* Externally, an isl_aff has a map space, but internally, the
507 * ls field corresponds to the domain of that space.
509 isl_size
isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
512 return isl_size_error
;
513 if (type
== isl_dim_out
)
515 if (type
== isl_dim_in
)
517 return isl_aff_domain_dim(aff
, type
);
520 /* Return the offset of the first coefficient of type "type" in
521 * the domain of "aff".
523 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
527 ls
= isl_aff_peek_domain_local_space(aff
);
528 return isl_local_space_offset(ls
, type
);
531 /* Return the position of the dimension of the given type and name
533 * Return -1 if no such dimension can be found.
535 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
540 if (type
== isl_dim_out
)
542 if (type
== isl_dim_in
)
544 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
547 /* Return the domain space of "aff".
549 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
551 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
554 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
556 return isl_space_copy(isl_aff_peek_domain_space(aff
));
559 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
564 space
= isl_local_space_get_space(aff
->ls
);
565 space
= isl_space_from_domain(space
);
566 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
570 /* Return a copy of the domain space of "aff".
572 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
573 __isl_keep isl_aff
*aff
)
575 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
578 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
583 ls
= isl_local_space_copy(aff
->ls
);
584 ls
= isl_local_space_from_domain(ls
);
585 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
589 /* Return the local space of the domain of "aff".
590 * This may be either a copy or the local space itself
591 * if there is only one reference to "aff".
592 * This allows the local space to be modified inplace
593 * if both the expression and its local space have only a single reference.
594 * The caller is not allowed to modify "aff" between this call and
595 * a subsequent call to isl_aff_restore_domain_local_space.
596 * The only exception is that isl_aff_free can be called instead.
598 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
599 __isl_keep isl_aff
*aff
)
606 return isl_aff_get_domain_local_space(aff
);
612 /* Set the local space of the domain of "aff" to "ls",
613 * where the local space of "aff" may be missing
614 * due to a preceding call to isl_aff_take_domain_local_space.
615 * However, in this case, "aff" only has a single reference and
616 * then the call to isl_aff_cow has no effect.
618 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
619 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
625 isl_local_space_free(ls
);
629 aff
= isl_aff_cow(aff
);
632 isl_local_space_free(aff
->ls
);
638 isl_local_space_free(ls
);
642 /* Externally, an isl_aff has a map space, but internally, the
643 * ls field corresponds to the domain of that space.
645 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
646 enum isl_dim_type type
, unsigned pos
)
650 if (type
== isl_dim_out
)
652 if (type
== isl_dim_in
)
654 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
657 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
658 __isl_take isl_space
*space
)
660 aff
= isl_aff_cow(aff
);
664 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
666 return isl_aff_free(aff
);
671 isl_space_free(space
);
675 /* Reset the space of "aff". This function is called from isl_pw_templ.c
676 * and doesn't know if the space of an element object is represented
677 * directly or through its domain. It therefore passes along both.
679 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
680 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
682 isl_space_free(space
);
683 return isl_aff_reset_domain_space(aff
, domain
);
686 /* Reorder the dimensions of the domain of "aff" according
687 * to the given reordering.
689 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
690 __isl_take isl_reordering
*r
)
692 aff
= isl_aff_cow(aff
);
696 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
697 aff
->v
= isl_vec_reorder(aff
->v
, 2, isl_reordering_copy(r
));
698 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
700 if (!aff
->v
|| !aff
->ls
)
701 return isl_aff_free(aff
);
706 isl_reordering_free(r
);
710 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
711 __isl_take isl_space
*model
)
713 isl_space
*domain_space
;
714 isl_bool equal_params
;
716 domain_space
= isl_aff_peek_domain_space(aff
);
717 equal_params
= isl_space_has_equal_params(domain_space
, model
);
718 if (equal_params
< 0)
723 exp
= isl_parameter_alignment_reordering(domain_space
, model
);
724 aff
= isl_aff_realign_domain(aff
, exp
);
727 isl_space_free(model
);
730 isl_space_free(model
);
737 #include "isl_unbind_params_templ.c"
739 /* Is "aff" obviously equal to zero?
741 * If the denominator is zero, then "aff" is not equal to zero.
743 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
748 return isl_bool_error
;
750 if (isl_int_is_zero(aff
->v
->el
[0]))
751 return isl_bool_false
;
752 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
753 return isl_bool_ok(pos
< 0);
756 /* Does "aff" represent NaN?
758 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
761 return isl_bool_error
;
763 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
766 /* Are "aff1" and "aff2" obviously equal?
768 * NaN is not equal to anything, not even to another NaN.
770 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
771 __isl_keep isl_aff
*aff2
)
776 return isl_bool_error
;
778 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
779 return isl_bool_false
;
781 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
782 if (equal
< 0 || !equal
)
785 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
788 /* Return the common denominator of "aff" in "v".
790 * We cannot return anything meaningful in case of a NaN.
792 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
795 return isl_stat_error
;
796 if (isl_aff_is_nan(aff
))
797 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
798 "cannot get denominator of NaN", return isl_stat_error
);
799 isl_int_set(*v
, aff
->v
->el
[0]);
803 /* Return the common denominator of "aff".
805 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
812 ctx
= isl_aff_get_ctx(aff
);
813 if (isl_aff_is_nan(aff
))
814 return isl_val_nan(ctx
);
815 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
818 /* Return the constant term of "aff".
820 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
828 ctx
= isl_aff_get_ctx(aff
);
829 if (isl_aff_is_nan(aff
))
830 return isl_val_nan(ctx
);
831 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
832 return isl_val_normalize(v
);
835 /* Return the coefficient of the variable of type "type" at position "pos"
838 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
839 enum isl_dim_type type
, int pos
)
847 ctx
= isl_aff_get_ctx(aff
);
848 if (type
== isl_dim_out
)
849 isl_die(ctx
, isl_error_invalid
,
850 "output/set dimension does not have a coefficient",
852 if (type
== isl_dim_in
)
855 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
858 if (isl_aff_is_nan(aff
))
859 return isl_val_nan(ctx
);
860 pos
+= isl_local_space_offset(aff
->ls
, type
);
861 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
862 return isl_val_normalize(v
);
865 /* Return the sign of the coefficient of the variable of type "type"
866 * at position "pos" of "aff".
868 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
876 ctx
= isl_aff_get_ctx(aff
);
877 if (type
== isl_dim_out
)
878 isl_die(ctx
, isl_error_invalid
,
879 "output/set dimension does not have a coefficient",
881 if (type
== isl_dim_in
)
884 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
887 pos
+= isl_local_space_offset(aff
->ls
, type
);
888 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
891 /* Replace the numerator of the constant term of "aff" by "v".
893 * A NaN is unaffected by this operation.
895 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
899 if (isl_aff_is_nan(aff
))
901 aff
= isl_aff_cow(aff
);
905 aff
->v
= isl_vec_cow(aff
->v
);
907 return isl_aff_free(aff
);
909 isl_int_set(aff
->v
->el
[1], v
);
914 /* Replace the constant term of "aff" by "v".
916 * A NaN is unaffected by this operation.
918 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
919 __isl_take isl_val
*v
)
924 if (isl_aff_is_nan(aff
)) {
929 if (!isl_val_is_rat(v
))
930 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
931 "expecting rational value", goto error
);
933 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
934 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
939 aff
= isl_aff_cow(aff
);
942 aff
->v
= isl_vec_cow(aff
->v
);
946 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
947 isl_int_set(aff
->v
->el
[1], v
->n
);
948 } else if (isl_int_is_one(v
->d
)) {
949 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
951 isl_seq_scale(aff
->v
->el
+ 1,
952 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
953 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
954 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
955 aff
->v
= isl_vec_normalize(aff
->v
);
968 /* Add "v" to the constant term of "aff".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
974 if (isl_int_is_zero(v
))
979 if (isl_aff_is_nan(aff
))
981 aff
= isl_aff_cow(aff
);
985 aff
->v
= isl_vec_cow(aff
->v
);
987 return isl_aff_free(aff
);
989 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
994 /* Add "v" to the constant term of "aff",
995 * in case "aff" is a rational expression.
997 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
998 __isl_take isl_val
*v
)
1000 aff
= isl_aff_cow(aff
);
1004 aff
->v
= isl_vec_cow(aff
->v
);
1008 if (isl_int_is_one(v
->d
)) {
1009 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
1010 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1011 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
1012 aff
->v
= isl_vec_normalize(aff
->v
);
1016 isl_seq_scale(aff
->v
->el
+ 1,
1017 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1018 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
1019 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1020 aff
->v
= isl_vec_normalize(aff
->v
);
1033 /* Return the first argument and free the second.
1035 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
1036 __isl_take isl_val
*v
)
1042 /* Replace the first argument by NaN and free the second argument.
1044 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
1045 __isl_take isl_val
*v
)
1048 return isl_aff_set_nan(aff
);
1051 /* Add "v" to the constant term of "aff".
1053 * A NaN is unaffected by this operation.
1054 * Conversely, adding a NaN turns "aff" into a NaN.
1056 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
1057 __isl_take isl_val
*v
)
1059 isl_bool is_nan
, is_zero
, is_rat
;
1061 is_nan
= isl_aff_is_nan(aff
);
1062 is_zero
= isl_val_is_zero(v
);
1063 if (is_nan
< 0 || is_zero
< 0)
1065 if (is_nan
|| is_zero
)
1066 return pick_free(aff
, v
);
1068 is_nan
= isl_val_is_nan(v
);
1069 is_rat
= isl_val_is_rat(v
);
1070 if (is_nan
< 0 || is_rat
< 0)
1073 return set_nan_free_val(aff
, v
);
1075 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1076 "expecting rational value or NaN", goto error
);
1078 return isl_aff_add_rat_constant_val(aff
, v
);
1085 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1090 isl_int_set_si(t
, v
);
1091 aff
= isl_aff_add_constant(aff
, t
);
1097 /* Add "v" to the numerator of the constant term of "aff".
1099 * A NaN is unaffected by this operation.
1101 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1103 if (isl_int_is_zero(v
))
1108 if (isl_aff_is_nan(aff
))
1110 aff
= isl_aff_cow(aff
);
1114 aff
->v
= isl_vec_cow(aff
->v
);
1116 return isl_aff_free(aff
);
1118 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1123 /* Add "v" to the numerator of the constant term of "aff".
1125 * A NaN is unaffected by this operation.
1127 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1135 isl_int_set_si(t
, v
);
1136 aff
= isl_aff_add_constant_num(aff
, t
);
1142 /* Replace the numerator of the constant term of "aff" by "v".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1150 if (isl_aff_is_nan(aff
))
1152 aff
= isl_aff_cow(aff
);
1156 aff
->v
= isl_vec_cow(aff
->v
);
1158 return isl_aff_free(aff
);
1160 isl_int_set_si(aff
->v
->el
[1], v
);
1165 /* Replace the numerator of the coefficient of the variable of type "type"
1166 * at position "pos" of "aff" by "v".
1168 * A NaN is unaffected by this operation.
1170 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1171 enum isl_dim_type type
, int pos
, isl_int v
)
1176 if (type
== isl_dim_out
)
1177 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1178 "output/set dimension does not have a coefficient",
1179 return isl_aff_free(aff
));
1180 if (type
== isl_dim_in
)
1183 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1184 return isl_aff_free(aff
);
1186 if (isl_aff_is_nan(aff
))
1188 aff
= isl_aff_cow(aff
);
1192 aff
->v
= isl_vec_cow(aff
->v
);
1194 return isl_aff_free(aff
);
1196 pos
+= isl_local_space_offset(aff
->ls
, type
);
1197 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1202 /* Replace the numerator of the coefficient of the variable of type "type"
1203 * at position "pos" of "aff" by "v".
1205 * A NaN is unaffected by this operation.
1207 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1208 enum isl_dim_type type
, int pos
, int v
)
1213 if (type
== isl_dim_out
)
1214 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1215 "output/set dimension does not have a coefficient",
1216 return isl_aff_free(aff
));
1217 if (type
== isl_dim_in
)
1220 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1221 return isl_aff_free(aff
);
1223 if (isl_aff_is_nan(aff
))
1225 pos
+= isl_local_space_offset(aff
->ls
, type
);
1226 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1229 aff
= isl_aff_cow(aff
);
1233 aff
->v
= isl_vec_cow(aff
->v
);
1235 return isl_aff_free(aff
);
1237 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1242 /* Replace the coefficient of the variable of type "type" at position "pos"
1245 * A NaN is unaffected by this operation.
1247 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1248 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1253 if (type
== isl_dim_out
)
1254 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1255 "output/set dimension does not have a coefficient",
1257 if (type
== isl_dim_in
)
1260 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1261 return isl_aff_free(aff
);
1263 if (isl_aff_is_nan(aff
)) {
1267 if (!isl_val_is_rat(v
))
1268 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1269 "expecting rational value", goto error
);
1271 pos
+= isl_local_space_offset(aff
->ls
, type
);
1272 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1273 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1278 aff
= isl_aff_cow(aff
);
1281 aff
->v
= isl_vec_cow(aff
->v
);
1285 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1286 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1287 } else if (isl_int_is_one(v
->d
)) {
1288 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1290 isl_seq_scale(aff
->v
->el
+ 1,
1291 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1292 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1293 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1294 aff
->v
= isl_vec_normalize(aff
->v
);
1307 /* Add "v" to the coefficient of the variable of type "type"
1308 * at position "pos" of "aff".
1310 * A NaN is unaffected by this operation.
1312 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1313 enum isl_dim_type type
, int pos
, isl_int v
)
1318 if (type
== isl_dim_out
)
1319 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1320 "output/set dimension does not have a coefficient",
1321 return isl_aff_free(aff
));
1322 if (type
== isl_dim_in
)
1325 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1326 return isl_aff_free(aff
);
1328 if (isl_aff_is_nan(aff
))
1330 aff
= isl_aff_cow(aff
);
1334 aff
->v
= isl_vec_cow(aff
->v
);
1336 return isl_aff_free(aff
);
1338 pos
+= isl_local_space_offset(aff
->ls
, type
);
1339 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1344 /* Add "v" to the coefficient of the variable of type "type"
1345 * at position "pos" of "aff".
1347 * A NaN is unaffected by this operation.
1349 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1350 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1355 if (isl_val_is_zero(v
)) {
1360 if (type
== isl_dim_out
)
1361 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1362 "output/set dimension does not have a coefficient",
1364 if (type
== isl_dim_in
)
1367 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1370 if (isl_aff_is_nan(aff
)) {
1374 if (!isl_val_is_rat(v
))
1375 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1376 "expecting rational value", goto error
);
1378 aff
= isl_aff_cow(aff
);
1382 aff
->v
= isl_vec_cow(aff
->v
);
1386 pos
+= isl_local_space_offset(aff
->ls
, type
);
1387 if (isl_int_is_one(v
->d
)) {
1388 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1389 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1390 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1391 aff
->v
= isl_vec_normalize(aff
->v
);
1395 isl_seq_scale(aff
->v
->el
+ 1,
1396 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1397 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1398 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1399 aff
->v
= isl_vec_normalize(aff
->v
);
1412 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1413 enum isl_dim_type type
, int pos
, int v
)
1418 isl_int_set_si(t
, v
);
1419 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1425 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1430 return isl_local_space_get_div(aff
->ls
, pos
);
1433 /* Return the negation of "aff".
1435 * As a special case, -NaN = NaN.
1437 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1441 if (isl_aff_is_nan(aff
))
1443 aff
= isl_aff_cow(aff
);
1446 aff
->v
= isl_vec_cow(aff
->v
);
1448 return isl_aff_free(aff
);
1450 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1455 /* Remove divs from the local space that do not appear in the affine
1457 * We currently only remove divs at the end.
1458 * Some intermediate divs may also not appear directly in the affine
1459 * expression, but we would also need to check that no other divs are
1460 * defined in terms of them.
1462 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1468 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1469 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1470 if (n
< 0 || off
< 0)
1471 return isl_aff_free(aff
);
1473 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1477 aff
= isl_aff_cow(aff
);
1481 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1482 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1483 if (!aff
->ls
|| !aff
->v
)
1484 return isl_aff_free(aff
);
1489 /* Look for any divs in the aff->ls with a denominator equal to one
1490 * and plug them into the affine expression and any subsequent divs
1491 * that may reference the div.
1493 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1500 isl_local_space
*ls
;
1503 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1504 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1505 if (n
< 0 || off
< 0)
1506 return isl_aff_free(aff
);
1508 for (i
= 0; i
< n
; ++i
) {
1509 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1511 ls
= isl_local_space_copy(aff
->ls
);
1512 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1513 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1514 vec
= isl_vec_copy(aff
->v
);
1515 vec
= isl_vec_cow(vec
);
1521 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1526 isl_vec_free(aff
->v
);
1528 isl_local_space_free(aff
->ls
);
1535 isl_local_space_free(ls
);
1536 return isl_aff_free(aff
);
1539 /* Look for any divs j that appear with a unit coefficient inside
1540 * the definitions of other divs i and plug them into the definitions
1543 * In particular, an expression of the form
1545 * floor((f(..) + floor(g(..)/n))/m)
1549 * floor((n * f(..) + g(..))/(n * m))
1551 * This simplification is correct because we can move the expression
1552 * f(..) into the inner floor in the original expression to obtain
1554 * floor(floor((n * f(..) + g(..))/n)/m)
1556 * from which we can derive the simplified expression.
1558 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1564 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1565 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1566 if (n
< 0 || off
< 0)
1567 return isl_aff_free(aff
);
1568 for (i
= 1; i
< n
; ++i
) {
1569 for (j
= 0; j
< i
; ++j
) {
1570 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1572 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1573 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1574 aff
->v
->size
, i
, 1);
1576 return isl_aff_free(aff
);
1583 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1585 * Even though this function is only called on isl_affs with a single
1586 * reference, we are careful to only change aff->v and aff->ls together.
1588 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1590 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1591 isl_local_space
*ls
;
1595 return isl_aff_free(aff
);
1597 ls
= isl_local_space_copy(aff
->ls
);
1598 ls
= isl_local_space_swap_div(ls
, a
, b
);
1599 v
= isl_vec_copy(aff
->v
);
1604 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1605 isl_vec_free(aff
->v
);
1607 isl_local_space_free(aff
->ls
);
1613 isl_local_space_free(ls
);
1614 return isl_aff_free(aff
);
1617 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1619 * We currently do not actually remove div "b", but simply add its
1620 * coefficient to that of "a" and then zero it out.
1622 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1624 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1627 return isl_aff_free(aff
);
1629 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1632 aff
->v
= isl_vec_cow(aff
->v
);
1634 return isl_aff_free(aff
);
1636 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1637 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1638 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1643 /* Sort the divs in the local space of "aff" according to
1644 * the comparison function "cmp_row" in isl_local_space.c,
1645 * combining the coefficients of identical divs.
1647 * Reordering divs does not change the semantics of "aff",
1648 * so there is no need to call isl_aff_cow.
1649 * Moreover, this function is currently only called on isl_affs
1650 * with a single reference.
1652 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1657 n
= isl_aff_dim(aff
, isl_dim_div
);
1659 return isl_aff_free(aff
);
1660 for (i
= 1; i
< n
; ++i
) {
1661 for (j
= i
- 1; j
>= 0; --j
) {
1662 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1666 aff
= merge_divs(aff
, j
, j
+ 1);
1668 aff
= swap_div(aff
, j
, j
+ 1);
1677 /* Normalize the representation of "aff".
1679 * This function should only be called on "new" isl_affs, i.e.,
1680 * with only a single reference. We therefore do not need to
1681 * worry about affecting other instances.
1683 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1687 aff
->v
= isl_vec_normalize(aff
->v
);
1689 return isl_aff_free(aff
);
1690 aff
= plug_in_integral_divs(aff
);
1691 aff
= plug_in_unit_divs(aff
);
1692 aff
= sort_divs(aff
);
1693 aff
= isl_aff_remove_unused_divs(aff
);
1697 /* Given f, return floor(f).
1698 * If f is an integer expression, then just return f.
1699 * If f is a constant, then return the constant floor(f).
1700 * Otherwise, if f = g/m, write g = q m + r,
1701 * create a new div d = [r/m] and return the expression q + d.
1702 * The coefficients in r are taken to lie between -m/2 and m/2.
1704 * reduce_div_coefficients performs the same normalization.
1706 * As a special case, floor(NaN) = NaN.
1708 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1718 if (isl_aff_is_nan(aff
))
1720 if (isl_int_is_one(aff
->v
->el
[0]))
1723 aff
= isl_aff_cow(aff
);
1727 aff
->v
= isl_vec_cow(aff
->v
);
1729 return isl_aff_free(aff
);
1731 if (isl_aff_is_cst(aff
)) {
1732 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1733 isl_int_set_si(aff
->v
->el
[0], 1);
1737 div
= isl_vec_copy(aff
->v
);
1738 div
= isl_vec_cow(div
);
1740 return isl_aff_free(aff
);
1742 ctx
= isl_aff_get_ctx(aff
);
1743 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1744 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1745 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1746 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1747 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1748 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1749 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1753 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1755 return isl_aff_free(aff
);
1757 size
= aff
->v
->size
;
1758 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1760 return isl_aff_free(aff
);
1761 isl_int_set_si(aff
->v
->el
[0], 1);
1762 isl_int_set_si(aff
->v
->el
[size
], 1);
1764 aff
= isl_aff_normalize(aff
);
1771 * aff mod m = aff - m * floor(aff/m)
1773 * with m an integer value.
1775 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1776 __isl_take isl_val
*m
)
1783 if (!isl_val_is_int(m
))
1784 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1785 "expecting integer modulo", goto error
);
1787 res
= isl_aff_copy(aff
);
1788 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1789 aff
= isl_aff_floor(aff
);
1790 aff
= isl_aff_scale_val(aff
, m
);
1791 res
= isl_aff_sub(res
, aff
);
1802 * pwaff mod m = pwaff - m * floor(pwaff/m)
1804 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1808 res
= isl_pw_aff_copy(pwaff
);
1809 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1810 pwaff
= isl_pw_aff_floor(pwaff
);
1811 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1812 res
= isl_pw_aff_sub(res
, pwaff
);
1819 * pa mod m = pa - m * floor(pa/m)
1821 * with m an integer value.
1823 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1824 __isl_take isl_val
*m
)
1828 if (!isl_val_is_int(m
))
1829 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1830 "expecting integer modulo", goto error
);
1831 pa
= isl_pw_aff_mod(pa
, m
->n
);
1835 isl_pw_aff_free(pa
);
1840 /* Given f, return ceil(f).
1841 * If f is an integer expression, then just return f.
1842 * Otherwise, let f be the expression
1848 * floor((e + m - 1)/m)
1850 * As a special case, ceil(NaN) = NaN.
1852 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1857 if (isl_aff_is_nan(aff
))
1859 if (isl_int_is_one(aff
->v
->el
[0]))
1862 aff
= isl_aff_cow(aff
);
1865 aff
->v
= isl_vec_cow(aff
->v
);
1867 return isl_aff_free(aff
);
1869 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1870 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1871 aff
= isl_aff_floor(aff
);
1876 /* Apply the expansion computed by isl_merge_divs.
1877 * The expansion itself is given by "exp" while the resulting
1878 * list of divs is given by "div".
1880 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1881 __isl_take isl_mat
*div
, int *exp
)
1887 aff
= isl_aff_cow(aff
);
1889 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1890 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1891 new_n_div
= isl_mat_rows(div
);
1892 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1895 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1896 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1897 if (!aff
->v
|| !aff
->ls
)
1898 return isl_aff_free(aff
);
1906 /* Add two affine expressions that live in the same local space.
1908 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1909 __isl_take isl_aff
*aff2
)
1913 aff1
= isl_aff_cow(aff1
);
1917 aff1
->v
= isl_vec_cow(aff1
->v
);
1923 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1924 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1925 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1926 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1927 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1928 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1929 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1934 aff1
= isl_aff_normalize(aff1
);
1942 /* Replace one of the arguments by a NaN and free the other one.
1944 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1945 __isl_take isl_aff
*aff2
)
1948 return isl_aff_set_nan(aff1
);
1951 /* Return the sum of "aff1" and "aff2".
1953 * If either of the two is NaN, then the result is NaN.
1955 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1956 __isl_take isl_aff
*aff2
)
1962 isl_size n_div1
, n_div2
;
1967 ctx
= isl_aff_get_ctx(aff1
);
1968 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1969 isl_die(ctx
, isl_error_invalid
,
1970 "spaces don't match", goto error
);
1972 if (isl_aff_is_nan(aff1
)) {
1976 if (isl_aff_is_nan(aff2
)) {
1981 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1982 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1983 if (n_div1
< 0 || n_div2
< 0)
1985 if (n_div1
== 0 && n_div2
== 0)
1986 return add_expanded(aff1
, aff2
);
1988 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1989 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1990 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1993 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1994 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1995 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1999 return add_expanded(aff1
, aff2
);
2008 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
2009 __isl_take isl_aff
*aff2
)
2011 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
2014 /* Return the result of scaling "aff" by a factor of "f".
2016 * As a special case, f * NaN = NaN.
2018 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
2024 if (isl_aff_is_nan(aff
))
2027 if (isl_int_is_one(f
))
2030 aff
= isl_aff_cow(aff
);
2033 aff
->v
= isl_vec_cow(aff
->v
);
2035 return isl_aff_free(aff
);
2037 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
2038 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
2043 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
2044 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2045 isl_int_divexact(gcd
, f
, gcd
);
2046 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2052 /* Multiple "aff" by "v".
2054 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
2055 __isl_take isl_val
*v
)
2060 if (isl_val_is_one(v
)) {
2065 if (!isl_val_is_rat(v
))
2066 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2067 "expecting rational factor", goto error
);
2069 aff
= isl_aff_scale(aff
, v
->n
);
2070 aff
= isl_aff_scale_down(aff
, v
->d
);
2080 /* Return the result of scaling "aff" down by a factor of "f".
2082 * As a special case, NaN/f = NaN.
2084 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2090 if (isl_aff_is_nan(aff
))
2093 if (isl_int_is_one(f
))
2096 aff
= isl_aff_cow(aff
);
2100 if (isl_int_is_zero(f
))
2101 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2102 "cannot scale down by zero", return isl_aff_free(aff
));
2104 aff
->v
= isl_vec_cow(aff
->v
);
2106 return isl_aff_free(aff
);
2109 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2110 isl_int_gcd(gcd
, gcd
, f
);
2111 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2112 isl_int_divexact(gcd
, f
, gcd
);
2113 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2119 /* Divide "aff" by "v".
2121 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2122 __isl_take isl_val
*v
)
2127 if (isl_val_is_one(v
)) {
2132 if (!isl_val_is_rat(v
))
2133 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2134 "expecting rational factor", goto error
);
2135 if (!isl_val_is_pos(v
))
2136 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2137 "factor needs to be positive", goto error
);
2139 aff
= isl_aff_scale(aff
, v
->d
);
2140 aff
= isl_aff_scale_down(aff
, v
->n
);
2150 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2158 isl_int_set_ui(v
, f
);
2159 aff
= isl_aff_scale_down(aff
, v
);
2165 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2166 enum isl_dim_type type
, unsigned pos
, const char *s
)
2168 aff
= isl_aff_cow(aff
);
2171 if (type
== isl_dim_out
)
2172 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2173 "cannot set name of output/set dimension",
2174 return isl_aff_free(aff
));
2175 if (type
== isl_dim_in
)
2177 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2179 return isl_aff_free(aff
);
2184 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2185 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2187 aff
= isl_aff_cow(aff
);
2190 if (type
== isl_dim_out
)
2191 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2192 "cannot set name of output/set dimension",
2194 if (type
== isl_dim_in
)
2196 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2198 return isl_aff_free(aff
);
2207 /* Replace the identifier of the input tuple of "aff" by "id".
2208 * type is currently required to be equal to isl_dim_in
2210 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2211 enum isl_dim_type type
, __isl_take isl_id
*id
)
2213 aff
= isl_aff_cow(aff
);
2216 if (type
!= isl_dim_in
)
2217 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2218 "cannot only set id of input tuple", goto error
);
2219 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2221 return isl_aff_free(aff
);
2230 /* Exploit the equalities in "eq" to simplify the affine expression
2231 * and the expressions of the integer divisions in the local space.
2232 * The integer divisions in this local space are assumed to appear
2233 * as regular dimensions in "eq".
2235 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2236 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2244 if (eq
->n_eq
== 0) {
2245 isl_basic_set_free(eq
);
2249 aff
= isl_aff_cow(aff
);
2253 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2254 isl_basic_set_copy(eq
));
2255 aff
->v
= isl_vec_cow(aff
->v
);
2256 if (!aff
->ls
|| !aff
->v
)
2259 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2261 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2262 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2263 if (j
< 0 || j
== 0 || j
>= o_div
)
2266 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2270 isl_basic_set_free(eq
);
2271 aff
= isl_aff_normalize(aff
);
2274 isl_basic_set_free(eq
);
2279 /* Exploit the equalities in "eq" to simplify the affine expression
2280 * and the expressions of the integer divisions in the local space.
2282 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2283 __isl_take isl_basic_set
*eq
)
2287 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2291 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2292 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2294 isl_basic_set_free(eq
);
2299 /* Look for equalities among the variables shared by context and aff
2300 * and the integer divisions of aff, if any.
2301 * The equalities are then used to eliminate coefficients and/or integer
2302 * divisions from aff.
2304 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2305 __isl_take isl_set
*context
)
2307 isl_local_space
*ls
;
2308 isl_basic_set
*hull
;
2310 ls
= isl_aff_get_domain_local_space(aff
);
2311 context
= isl_local_space_lift_set(ls
, context
);
2313 hull
= isl_set_affine_hull(context
);
2314 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2317 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2318 __isl_take isl_set
*context
)
2320 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2321 dom_context
= isl_set_intersect_params(dom_context
, context
);
2322 return isl_aff_gist(aff
, dom_context
);
2325 /* Return a basic set containing those elements in the space
2326 * of aff where it is positive. "rational" should not be set.
2328 * If "aff" is NaN, then it is not positive.
2330 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2331 int rational
, void *user
)
2333 isl_constraint
*ineq
;
2334 isl_basic_set
*bset
;
2339 if (isl_aff_is_nan(aff
)) {
2340 isl_space
*space
= isl_aff_get_domain_space(aff
);
2342 return isl_basic_set_empty(space
);
2345 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2346 "rational sets not supported", goto error
);
2348 ineq
= isl_inequality_from_aff(aff
);
2349 c
= isl_constraint_get_constant_val(ineq
);
2350 c
= isl_val_sub_ui(c
, 1);
2351 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2353 bset
= isl_basic_set_from_constraint(ineq
);
2354 bset
= isl_basic_set_simplify(bset
);
2361 /* Return a basic set containing those elements in the space
2362 * of aff where it is non-negative.
2363 * If "rational" is set, then return a rational basic set.
2365 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2367 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2368 __isl_take isl_aff
*aff
, int rational
, void *user
)
2370 isl_constraint
*ineq
;
2371 isl_basic_set
*bset
;
2375 if (isl_aff_is_nan(aff
)) {
2376 isl_space
*space
= isl_aff_get_domain_space(aff
);
2378 return isl_basic_set_empty(space
);
2381 ineq
= isl_inequality_from_aff(aff
);
2383 bset
= isl_basic_set_from_constraint(ineq
);
2385 bset
= isl_basic_set_set_rational(bset
);
2386 bset
= isl_basic_set_simplify(bset
);
2390 /* Return a basic set containing those elements in the space
2391 * of aff where it is non-negative.
2393 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2395 return aff_nonneg_basic_set(aff
, 0, NULL
);
2398 /* Return a basic set containing those elements in the domain space
2399 * of "aff" where it is positive.
2401 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2403 aff
= isl_aff_add_constant_num_si(aff
, -1);
2404 return isl_aff_nonneg_basic_set(aff
);
2407 /* Return a basic set containing those elements in the domain space
2408 * of aff where it is negative.
2410 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2412 aff
= isl_aff_neg(aff
);
2413 return isl_aff_pos_basic_set(aff
);
2416 /* Return a basic set containing those elements in the space
2417 * of aff where it is zero.
2418 * If "rational" is set, then return a rational basic set.
2420 * If "aff" is NaN, then it is not zero.
2422 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2423 int rational
, void *user
)
2425 isl_constraint
*ineq
;
2426 isl_basic_set
*bset
;
2430 if (isl_aff_is_nan(aff
)) {
2431 isl_space
*space
= isl_aff_get_domain_space(aff
);
2433 return isl_basic_set_empty(space
);
2436 ineq
= isl_equality_from_aff(aff
);
2438 bset
= isl_basic_set_from_constraint(ineq
);
2440 bset
= isl_basic_set_set_rational(bset
);
2441 bset
= isl_basic_set_simplify(bset
);
2445 /* Return a basic set containing those elements in the space
2446 * of aff where it is zero.
2448 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2450 return aff_zero_basic_set(aff
, 0, NULL
);
2453 /* Return a basic set containing those elements in the shared space
2454 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2456 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2457 __isl_take isl_aff
*aff2
)
2459 aff1
= isl_aff_sub(aff1
, aff2
);
2461 return isl_aff_nonneg_basic_set(aff1
);
2464 /* Return a basic set containing those elements in the shared domain space
2465 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2467 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2468 __isl_take isl_aff
*aff2
)
2470 aff1
= isl_aff_sub(aff1
, aff2
);
2472 return isl_aff_pos_basic_set(aff1
);
2475 /* Return a set containing those elements in the shared space
2476 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2478 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2479 __isl_take isl_aff
*aff2
)
2481 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2484 /* Return a set containing those elements in the shared domain space
2485 * of aff1 and aff2 where aff1 is greater than aff2.
2487 * If either of the two inputs is NaN, then the result is empty,
2488 * as comparisons with NaN always return false.
2490 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2491 __isl_take isl_aff
*aff2
)
2493 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2496 /* Return a basic set containing those elements in the shared space
2497 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2499 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2500 __isl_take isl_aff
*aff2
)
2502 return isl_aff_ge_basic_set(aff2
, aff1
);
2505 /* Return a basic set containing those elements in the shared domain space
2506 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2508 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2509 __isl_take isl_aff
*aff2
)
2511 return isl_aff_gt_basic_set(aff2
, aff1
);
2514 /* Return a set containing those elements in the shared space
2515 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2517 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2518 __isl_take isl_aff
*aff2
)
2520 return isl_aff_ge_set(aff2
, aff1
);
2523 /* Return a set containing those elements in the shared domain space
2524 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2526 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2527 __isl_take isl_aff
*aff2
)
2529 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2532 /* Return a basic set containing those elements in the shared space
2533 * of aff1 and aff2 where aff1 and aff2 are equal.
2535 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2536 __isl_take isl_aff
*aff2
)
2538 aff1
= isl_aff_sub(aff1
, aff2
);
2540 return isl_aff_zero_basic_set(aff1
);
2543 /* Return a set containing those elements in the shared space
2544 * of aff1 and aff2 where aff1 and aff2 are equal.
2546 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2547 __isl_take isl_aff
*aff2
)
2549 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2552 /* Return a set containing those elements in the shared domain space
2553 * of aff1 and aff2 where aff1 and aff2 are not equal.
2555 * If either of the two inputs is NaN, then the result is empty,
2556 * as comparisons with NaN always return false.
2558 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2559 __isl_take isl_aff
*aff2
)
2561 isl_set
*set_lt
, *set_gt
;
2563 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2564 isl_aff_copy(aff2
));
2565 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2566 return isl_set_union_disjoint(set_lt
, set_gt
);
2569 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2570 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2572 aff1
= isl_aff_add(aff1
, aff2
);
2573 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2577 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2580 return isl_bool_error
;
2582 return isl_bool_false
;
2586 #define TYPE isl_aff
2588 #include "check_type_range_templ.c"
2590 /* Check whether the given affine expression has non-zero coefficient
2591 * for any dimension in the given range or if any of these dimensions
2592 * appear with non-zero coefficients in any of the integer divisions
2593 * involved in the affine expression.
2595 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2596 enum isl_dim_type type
, unsigned first
, unsigned n
)
2600 isl_bool involves
= isl_bool_false
;
2603 return isl_bool_error
;
2605 return isl_bool_false
;
2606 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2607 return isl_bool_error
;
2609 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2613 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2614 for (i
= 0; i
< n
; ++i
)
2615 if (active
[first
+ i
]) {
2616 involves
= isl_bool_true
;
2625 return isl_bool_error
;
2628 /* Does "aff" involve any local variables, i.e., integer divisions?
2630 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2634 n
= isl_aff_dim(aff
, isl_dim_div
);
2636 return isl_bool_error
;
2637 return isl_bool_ok(n
> 0);
2640 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2641 enum isl_dim_type type
, unsigned first
, unsigned n
)
2645 if (type
== isl_dim_out
)
2646 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2647 "cannot drop output/set dimension",
2648 return isl_aff_free(aff
));
2649 if (type
== isl_dim_in
)
2651 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2654 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2655 return isl_aff_free(aff
);
2657 aff
= isl_aff_cow(aff
);
2661 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2663 return isl_aff_free(aff
);
2665 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2666 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2668 return isl_aff_free(aff
);
2673 /* Is the domain of "aff" a product?
2675 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2677 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2681 #define TYPE isl_aff
2682 #include <isl_domain_factor_templ.c>
2684 /* Project the domain of the affine expression onto its parameter space.
2685 * The affine expression may not involve any of the domain dimensions.
2687 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2692 n
= isl_aff_dim(aff
, isl_dim_in
);
2694 return isl_aff_free(aff
);
2695 aff
= isl_aff_drop_domain(aff
, 0, n
);
2696 space
= isl_aff_get_domain_space(aff
);
2697 space
= isl_space_params(space
);
2698 aff
= isl_aff_reset_domain_space(aff
, space
);
2702 /* Convert an affine expression defined over a parameter domain
2703 * into one that is defined over a zero-dimensional set.
2705 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2707 isl_local_space
*ls
;
2709 ls
= isl_aff_take_domain_local_space(aff
);
2710 ls
= isl_local_space_set_from_params(ls
);
2711 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2716 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2717 enum isl_dim_type type
, unsigned first
, unsigned n
)
2721 if (type
== isl_dim_out
)
2722 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2723 "cannot insert output/set dimensions",
2724 return isl_aff_free(aff
));
2725 if (type
== isl_dim_in
)
2727 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2730 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2731 return isl_aff_free(aff
);
2733 aff
= isl_aff_cow(aff
);
2737 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2739 return isl_aff_free(aff
);
2741 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2742 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2744 return isl_aff_free(aff
);
2749 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2750 enum isl_dim_type type
, unsigned n
)
2754 pos
= isl_aff_dim(aff
, type
);
2756 return isl_aff_free(aff
);
2758 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2761 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2762 * to dimensions of "dst_type" at "dst_pos".
2764 * We only support moving input dimensions to parameters and vice versa.
2766 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2767 enum isl_dim_type dst_type
, unsigned dst_pos
,
2768 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2772 isl_size src_off
, dst_off
;
2777 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2778 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2781 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2782 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2783 "cannot move output/set dimension",
2784 return isl_aff_free(aff
));
2785 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2786 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2787 "cannot move divs", return isl_aff_free(aff
));
2788 if (dst_type
== isl_dim_in
)
2789 dst_type
= isl_dim_set
;
2790 if (src_type
== isl_dim_in
)
2791 src_type
= isl_dim_set
;
2793 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2794 return isl_aff_free(aff
);
2795 if (dst_type
== src_type
)
2796 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2797 "moving dims within the same type not supported",
2798 return isl_aff_free(aff
));
2800 aff
= isl_aff_cow(aff
);
2801 src_off
= isl_aff_domain_offset(aff
, src_type
);
2802 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2803 if (src_off
< 0 || dst_off
< 0)
2804 return isl_aff_free(aff
);
2806 g_src_pos
= 1 + src_off
+ src_pos
;
2807 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2808 if (dst_type
> src_type
)
2811 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2812 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2813 src_type
, src_pos
, n
);
2814 if (!aff
->v
|| !aff
->ls
)
2815 return isl_aff_free(aff
);
2817 aff
= sort_divs(aff
);
2822 /* Return a zero isl_aff in the given space.
2824 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2825 * interface over all piecewise types.
2827 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2829 isl_local_space
*ls
;
2831 ls
= isl_local_space_from_space(isl_space_domain(space
));
2832 return isl_aff_zero_on_domain(ls
);
2835 #define isl_aff_involves_nan isl_aff_is_nan
2838 #define PW isl_pw_aff
2842 #define EL_IS_ZERO is_empty
2846 #define IS_ZERO is_empty
2849 #undef DEFAULT_IS_ZERO
2850 #define DEFAULT_IS_ZERO 0
2852 #include <isl_pw_templ.c>
2853 #include <isl_pw_un_op_templ.c>
2854 #include <isl_pw_add_constant_val_templ.c>
2855 #include <isl_pw_add_disjoint_templ.c>
2856 #include <isl_pw_bind_domain_templ.c>
2857 #include <isl_pw_eval.c>
2858 #include <isl_pw_hash.c>
2859 #include <isl_pw_fix_templ.c>
2860 #include <isl_pw_from_range_templ.c>
2861 #include <isl_pw_insert_dims_templ.c>
2862 #include <isl_pw_insert_domain_templ.c>
2863 #include <isl_pw_move_dims_templ.c>
2864 #include <isl_pw_neg_templ.c>
2865 #include <isl_pw_pullback_templ.c>
2866 #include <isl_pw_scale_templ.c>
2867 #include <isl_pw_sub_templ.c>
2868 #include <isl_pw_union_opt.c>
2873 #include <isl_union_single.c>
2874 #include <isl_union_neg.c>
2875 #include <isl_union_sub_templ.c>
2880 #include <isl_union_pw_templ.c>
2882 /* Compute a piecewise quasi-affine expression with a domain that
2883 * is the union of those of pwaff1 and pwaff2 and such that on each
2884 * cell, the quasi-affine expression is the maximum of those of pwaff1
2885 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2886 * cell, then the associated expression is the defined one.
2888 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2889 __isl_take isl_pw_aff
*pwaff2
)
2891 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2892 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2895 /* Compute a piecewise quasi-affine expression with a domain that
2896 * is the union of those of pwaff1 and pwaff2 and such that on each
2897 * cell, the quasi-affine expression is the minimum of those of pwaff1
2898 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2899 * cell, then the associated expression is the defined one.
2901 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2902 __isl_take isl_pw_aff
*pwaff2
)
2904 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2905 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2908 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2909 __isl_take isl_pw_aff
*pwaff2
, int max
)
2912 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2914 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2917 /* Is the domain of "pa" a product?
2919 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2921 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2925 #define TYPE isl_pw_aff
2926 #include <isl_domain_factor_templ.c>
2928 /* Return a set containing those elements in the domain
2929 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2930 * does not satisfy "fn" (if complement is 1).
2932 * The pieces with a NaN never belong to the result since
2933 * NaN does not satisfy any property.
2935 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2936 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2938 int complement
, void *user
)
2946 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2948 for (i
= 0; i
< pwaff
->n
; ++i
) {
2949 isl_basic_set
*bset
;
2950 isl_set
*set_i
, *locus
;
2953 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2956 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2957 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2958 locus
= isl_set_from_basic_set(bset
);
2959 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2961 set_i
= isl_set_subtract(set_i
, locus
);
2963 set_i
= isl_set_intersect(set_i
, locus
);
2964 set
= isl_set_union_disjoint(set
, set_i
);
2967 isl_pw_aff_free(pwaff
);
2972 /* Return a set containing those elements in the domain
2973 * of "pa" where it is positive.
2975 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2977 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
2980 /* Return a set containing those elements in the domain
2981 * of pwaff where it is non-negative.
2983 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2985 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
2988 /* Return a set containing those elements in the domain
2989 * of pwaff where it is zero.
2991 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2993 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
2996 /* Return a set containing those elements in the domain
2997 * of pwaff where it is not zero.
2999 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
3001 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
3004 /* Bind the affine function "aff" to the parameter "id",
3005 * returning the elements in the domain where the affine expression
3006 * is equal to the parameter.
3008 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
3009 __isl_take isl_id
*id
)
3014 space
= isl_aff_get_domain_space(aff
);
3015 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
3017 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
3018 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
3020 return isl_aff_eq_basic_set(aff
, aff_id
);
3023 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
3024 * "rational" should not be set.
3026 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
3027 int rational
, void *user
)
3034 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
3035 "rational binding not supported", goto error
);
3036 return isl_aff_bind_id(aff
, isl_id_copy(id
));
3042 /* Bind the piecewise affine function "pa" to the parameter "id",
3043 * returning the elements in the domain where the expression
3044 * is equal to the parameter.
3046 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
3047 __isl_take isl_id
*id
)
3051 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3057 /* Return a set containing those elements in the shared domain
3058 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3060 * We compute the difference on the shared domain and then construct
3061 * the set of values where this difference is non-negative.
3062 * If strict is set, we first subtract 1 from the difference.
3063 * If equal is set, we only return the elements where pwaff1 and pwaff2
3066 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3067 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3069 isl_set
*set1
, *set2
;
3071 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3072 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3073 set1
= isl_set_intersect(set1
, set2
);
3074 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3075 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3076 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3079 isl_space
*space
= isl_set_get_space(set1
);
3081 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3082 aff
= isl_aff_add_constant_si(aff
, -1);
3083 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3088 return isl_pw_aff_zero_set(pwaff1
);
3089 return isl_pw_aff_nonneg_set(pwaff1
);
3092 /* Return a set containing those elements in the shared domain
3093 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3095 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3096 __isl_take isl_pw_aff
*pwaff2
)
3098 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3099 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3102 /* Return a set containing those elements in the shared domain
3103 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3105 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3106 __isl_take isl_pw_aff
*pwaff2
)
3108 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3109 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3112 /* Return a set containing those elements in the shared domain
3113 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3115 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3116 __isl_take isl_pw_aff
*pwaff2
)
3118 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3119 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3122 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3123 __isl_take isl_pw_aff
*pwaff2
)
3125 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3128 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3129 __isl_take isl_pw_aff
*pwaff2
)
3131 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3134 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3135 * where the function values are ordered in the same way as "order",
3136 * which returns a set in the shared domain of its two arguments.
3138 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3139 * We first pull back the two functions such that they are defined on
3140 * the domain [A -> B]. Then we apply "order", resulting in a set
3141 * in the space [A -> B]. Finally, we unwrap this set to obtain
3142 * a map in the space A -> B.
3144 static __isl_give isl_map
*isl_pw_aff_order_map(
3145 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3146 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3147 __isl_take isl_pw_aff
*pa2
))
3149 isl_space
*space1
, *space2
;
3153 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3154 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3155 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3156 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3157 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3158 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3159 ma
= isl_multi_aff_range_map(space1
);
3160 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3161 set
= order(pa1
, pa2
);
3163 return isl_set_unwrap(set
);
3166 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3167 * where the function values are equal.
3169 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3170 __isl_take isl_pw_aff
*pa2
)
3172 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3175 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3176 * where the function value of "pa1" is less than or equal to
3177 * the function value of "pa2".
3179 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3180 __isl_take isl_pw_aff
*pa2
)
3182 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3185 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3186 * where the function value of "pa1" is less than the function value of "pa2".
3188 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3189 __isl_take isl_pw_aff
*pa2
)
3191 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3194 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3195 * where the function value of "pa1" is greater than or equal to
3196 * the function value of "pa2".
3198 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3199 __isl_take isl_pw_aff
*pa2
)
3201 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3204 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3205 * where the function value of "pa1" is greater than the function value
3208 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3209 __isl_take isl_pw_aff
*pa2
)
3211 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3214 /* Return a set containing those elements in the shared domain
3215 * of the elements of list1 and list2 where each element in list1
3216 * has the relation specified by "fn" with each element in list2.
3218 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3219 __isl_take isl_pw_aff_list
*list2
,
3220 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3221 __isl_take isl_pw_aff
*pwaff2
))
3227 if (!list1
|| !list2
)
3230 ctx
= isl_pw_aff_list_get_ctx(list1
);
3231 if (list1
->n
< 1 || list2
->n
< 1)
3232 isl_die(ctx
, isl_error_invalid
,
3233 "list should contain at least one element", goto error
);
3235 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3236 for (i
= 0; i
< list1
->n
; ++i
)
3237 for (j
= 0; j
< list2
->n
; ++j
) {
3240 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3241 isl_pw_aff_copy(list2
->p
[j
]));
3242 set
= isl_set_intersect(set
, set_ij
);
3245 isl_pw_aff_list_free(list1
);
3246 isl_pw_aff_list_free(list2
);
3249 isl_pw_aff_list_free(list1
);
3250 isl_pw_aff_list_free(list2
);
3254 /* Return a set containing those elements in the shared domain
3255 * of the elements of list1 and list2 where each element in list1
3256 * is equal to each element in list2.
3258 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3259 __isl_take isl_pw_aff_list
*list2
)
3261 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3264 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3265 __isl_take isl_pw_aff_list
*list2
)
3267 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3270 /* Return a set containing those elements in the shared domain
3271 * of the elements of list1 and list2 where each element in list1
3272 * is less than or equal to each element in list2.
3274 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3275 __isl_take isl_pw_aff_list
*list2
)
3277 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3280 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3281 __isl_take isl_pw_aff_list
*list2
)
3283 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3286 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3287 __isl_take isl_pw_aff_list
*list2
)
3289 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3292 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3293 __isl_take isl_pw_aff_list
*list2
)
3295 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3299 /* Return a set containing those elements in the shared domain
3300 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3302 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3303 __isl_take isl_pw_aff
*pwaff2
)
3305 isl_set
*set_lt
, *set_gt
;
3307 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3308 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3309 isl_pw_aff_copy(pwaff2
));
3310 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3311 return isl_set_union_disjoint(set_lt
, set_gt
);
3314 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3319 if (isl_int_is_one(v
))
3321 if (!isl_int_is_pos(v
))
3322 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3323 "factor needs to be positive",
3324 return isl_pw_aff_free(pwaff
));
3325 pwaff
= isl_pw_aff_cow(pwaff
);
3331 for (i
= 0; i
< pwaff
->n
; ++i
) {
3332 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3333 if (!pwaff
->p
[i
].aff
)
3334 return isl_pw_aff_free(pwaff
);
3340 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3342 return isl_pw_aff_un_op(pwaff
, &isl_aff_floor
);
3345 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3347 return isl_pw_aff_un_op(pwaff
, &isl_aff_ceil
);
3350 /* Assuming that "cond1" and "cond2" are disjoint,
3351 * return an affine expression that is equal to pwaff1 on cond1
3352 * and to pwaff2 on cond2.
3354 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3355 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3356 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3358 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3359 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3361 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3364 /* Return an affine expression that is equal to pwaff_true for elements
3365 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3367 * That is, return cond ? pwaff_true : pwaff_false;
3369 * If "cond" involves and NaN, then we conservatively return a NaN
3370 * on its entire domain. In principle, we could consider the pieces
3371 * where it is NaN separately from those where it is not.
3373 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3374 * then only use the domain of "cond" to restrict the domain.
3376 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3377 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3379 isl_set
*cond_true
, *cond_false
;
3384 if (isl_pw_aff_involves_nan(cond
)) {
3385 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3386 isl_local_space
*ls
= isl_local_space_from_space(space
);
3387 isl_pw_aff_free(cond
);
3388 isl_pw_aff_free(pwaff_true
);
3389 isl_pw_aff_free(pwaff_false
);
3390 return isl_pw_aff_nan_on_domain(ls
);
3393 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3394 isl_pw_aff_get_space(pwaff_false
));
3395 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3396 isl_pw_aff_get_space(pwaff_true
));
3397 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3403 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3404 isl_pw_aff_free(pwaff_false
);
3405 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3408 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3409 cond_false
= isl_pw_aff_zero_set(cond
);
3410 return isl_pw_aff_select(cond_true
, pwaff_true
,
3411 cond_false
, pwaff_false
);
3413 isl_pw_aff_free(cond
);
3414 isl_pw_aff_free(pwaff_true
);
3415 isl_pw_aff_free(pwaff_false
);
3419 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3424 return isl_bool_error
;
3426 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3427 return isl_bool_ok(pos
== -1);
3430 /* Check whether pwaff is a piecewise constant.
3432 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3437 return isl_bool_error
;
3439 for (i
= 0; i
< pwaff
->n
; ++i
) {
3440 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3441 if (is_cst
< 0 || !is_cst
)
3445 return isl_bool_true
;
3448 /* Return the product of "aff1" and "aff2".
3450 * If either of the two is NaN, then the result is NaN.
3452 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3454 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3455 __isl_take isl_aff
*aff2
)
3460 if (isl_aff_is_nan(aff1
)) {
3464 if (isl_aff_is_nan(aff2
)) {
3469 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3470 return isl_aff_mul(aff2
, aff1
);
3472 if (!isl_aff_is_cst(aff2
))
3473 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3474 "at least one affine expression should be constant",
3477 aff1
= isl_aff_cow(aff1
);
3481 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3482 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3492 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3494 * If either of the two is NaN, then the result is NaN.
3495 * A division by zero also results in NaN.
3497 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3498 __isl_take isl_aff
*aff2
)
3500 isl_bool is_cst
, is_zero
;
3506 if (isl_aff_is_nan(aff1
)) {
3510 if (isl_aff_is_nan(aff2
)) {
3515 is_cst
= isl_aff_is_cst(aff2
);
3519 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3520 "second argument should be a constant", goto error
);
3521 is_zero
= isl_aff_plain_is_zero(aff2
);
3525 return set_nan_free(aff1
, aff2
);
3527 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3529 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3530 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3533 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3534 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3537 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3538 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3549 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3550 __isl_take isl_pw_aff
*pwaff2
)
3552 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3553 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3556 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3557 __isl_take isl_pw_aff
*pwaff2
)
3559 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3560 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3563 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3565 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3566 __isl_take isl_pw_aff
*pa2
)
3570 is_cst
= isl_pw_aff_is_cst(pa2
);
3574 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3575 "second argument should be a piecewise constant",
3577 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3578 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3580 isl_pw_aff_free(pa1
);
3581 isl_pw_aff_free(pa2
);
3585 /* Compute the quotient of the integer division of "pa1" by "pa2"
3586 * with rounding towards zero.
3587 * "pa2" is assumed to be a piecewise constant.
3589 * In particular, return
3591 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3594 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3595 __isl_take isl_pw_aff
*pa2
)
3601 is_cst
= isl_pw_aff_is_cst(pa2
);
3605 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3606 "second argument should be a piecewise constant",
3609 pa1
= isl_pw_aff_div(pa1
, pa2
);
3611 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3612 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3613 c
= isl_pw_aff_ceil(pa1
);
3614 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3616 isl_pw_aff_free(pa1
);
3617 isl_pw_aff_free(pa2
);
3621 /* Compute the remainder of the integer division of "pa1" by "pa2"
3622 * with rounding towards zero.
3623 * "pa2" is assumed to be a piecewise constant.
3625 * In particular, return
3627 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3630 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3631 __isl_take isl_pw_aff
*pa2
)
3636 is_cst
= isl_pw_aff_is_cst(pa2
);
3640 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3641 "second argument should be a piecewise constant",
3643 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3644 res
= isl_pw_aff_mul(pa2
, res
);
3645 res
= isl_pw_aff_sub(pa1
, res
);
3648 isl_pw_aff_free(pa1
);
3649 isl_pw_aff_free(pa2
);
3653 /* Does either of "pa1" or "pa2" involve any NaN?
3655 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3656 __isl_keep isl_pw_aff
*pa2
)
3660 has_nan
= isl_pw_aff_involves_nan(pa1
);
3661 if (has_nan
< 0 || has_nan
)
3663 return isl_pw_aff_involves_nan(pa2
);
3666 /* Return a piecewise affine expression defined on the specified domain
3667 * that represents NaN.
3669 static __isl_give isl_pw_aff
*nan_on_domain_set(__isl_take isl_set
*dom
)
3671 isl_local_space
*ls
;
3674 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3675 pa
= isl_pw_aff_nan_on_domain(ls
);
3676 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3681 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3682 * by a NaN on their shared domain.
3684 * In principle, the result could be refined to only being NaN
3685 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3687 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3688 __isl_take isl_pw_aff
*pa2
)
3692 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3693 return nan_on_domain_set(dom
);
3696 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3697 __isl_take isl_pw_aff
*pwaff2
)
3702 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3703 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3704 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3705 isl_pw_aff_copy(pwaff2
));
3706 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3707 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3710 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3711 __isl_take isl_pw_aff
*pwaff2
)
3716 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3717 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3718 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3719 isl_pw_aff_copy(pwaff2
));
3720 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3721 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3724 /* Return an expression for the minimum (if "max" is not set) or
3725 * the maximum (if "max" is set) of "pa1" and "pa2".
3726 * If either expression involves any NaN, then return a NaN
3727 * on the shared domain as result.
3729 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3730 __isl_take isl_pw_aff
*pa2
, int max
)
3734 has_nan
= either_involves_nan(pa1
, pa2
);
3736 pa1
= isl_pw_aff_free(pa1
);
3738 return replace_by_nan(pa1
, pa2
);
3740 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3742 return pw_aff_max(pa1
, pa2
);
3744 return pw_aff_min(pa1
, pa2
);
3747 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3749 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3750 __isl_take isl_pw_aff
*pwaff2
)
3752 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3755 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3757 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3758 __isl_take isl_pw_aff
*pwaff2
)
3760 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3763 /* Does "pa" not involve any NaN?
3765 static isl_bool
pw_aff_no_nan(__isl_keep isl_pw_aff
*pa
, void *user
)
3767 return isl_bool_not(isl_pw_aff_involves_nan(pa
));
3770 /* Does any element of "list" involve any NaN?
3772 * That is, is it not the case that every element does not involve any NaN?
3774 static isl_bool
isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list
*list
)
3776 return isl_bool_not(isl_pw_aff_list_every(list
, &pw_aff_no_nan
, NULL
));
3779 /* Replace "list" (consisting of "n" elements, of which
3780 * at least one element involves a NaN)
3781 * by a NaN on the shared domain of the elements.
3783 * In principle, the result could be refined to only being NaN
3784 * on the parts of this domain where at least one of the elements is NaN.
3786 static __isl_give isl_pw_aff
*replace_list_by_nan(
3787 __isl_take isl_pw_aff_list
*list
, int n
)
3792 dom
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, 0));
3793 for (i
= 1; i
< n
; ++i
) {
3796 dom_i
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, i
));
3797 dom
= isl_set_intersect(dom
, dom_i
);
3800 isl_pw_aff_list_free(list
);
3801 return nan_on_domain_set(dom
);
3804 /* Return the set where the element at "pos1" of "list" is less than or
3805 * equal to the element at "pos2".
3806 * Equality is only allowed if "pos1" is smaller than "pos2".
3808 static __isl_give isl_set
*less(__isl_keep isl_pw_aff_list
*list
,
3811 isl_pw_aff
*pa1
, *pa2
;
3813 pa1
= isl_pw_aff_list_get_at(list
, pos1
);
3814 pa2
= isl_pw_aff_list_get_at(list
, pos2
);
3817 return isl_pw_aff_le_set(pa1
, pa2
);
3819 return isl_pw_aff_lt_set(pa1
, pa2
);
3822 /* Return an isl_pw_aff that maps each element in the intersection of the
3823 * domains of the piecewise affine expressions in "list"
3824 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3825 * expression in "list" at that element.
3826 * If any expression involves any NaN, then return a NaN
3827 * on the shared domain as result.
3829 * If "list" has n elements, then the result consists of n pieces,
3830 * where, in the case of a minimum, each piece has as value expression
3831 * the value expression of one of the elements and as domain
3832 * the set of elements where that value expression
3833 * is less than (or equal) to the other value expressions.
3834 * In the case of a maximum, the condition is
3835 * that all the other value expressions are less than (or equal)
3836 * to the given value expression.
3838 * In order to produce disjoint pieces, a pair of elements
3839 * in the original domain is only allowed to be equal to each other
3840 * on exactly one of the two pieces corresponding to the two elements.
3841 * The position in the list is used to break ties.
3842 * In particular, in the case of a minimum,
3843 * in the piece corresponding to a given element,
3844 * this element is allowed to be equal to any later element in the list,
3845 * but not to any earlier element in the list.
3847 static __isl_give isl_pw_aff
*isl_pw_aff_list_opt(
3848 __isl_take isl_pw_aff_list
*list
, int max
)
3854 isl_pw_aff
*pa
, *res
;
3856 n
= isl_pw_aff_list_size(list
);
3860 isl_die(isl_pw_aff_list_get_ctx(list
), isl_error_invalid
,
3861 "list should contain at least one element", goto error
);
3863 has_nan
= isl_pw_aff_list_involves_nan(list
);
3867 return replace_list_by_nan(list
, n
);
3869 pa
= isl_pw_aff_list_get_at(list
, 0);
3870 space
= isl_pw_aff_get_space(pa
);
3871 isl_pw_aff_free(pa
);
3872 res
= isl_pw_aff_empty(space
);
3874 for (i
= 0; i
< n
; ++i
) {
3875 pa
= isl_pw_aff_list_get_at(list
, i
);
3876 for (j
= 0; j
< n
; ++j
) {
3882 dom
= less(list
, j
, i
);
3884 dom
= less(list
, i
, j
);
3886 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3888 res
= isl_pw_aff_add_disjoint(res
, pa
);
3891 isl_pw_aff_list_free(list
);
3894 isl_pw_aff_list_free(list
);
3898 /* Return an isl_pw_aff that maps each element in the intersection of the
3899 * domains of the elements of list to the minimal corresponding affine
3902 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3904 return isl_pw_aff_list_opt(list
, 0);
3907 /* Return an isl_pw_aff that maps each element in the intersection of the
3908 * domains of the elements of list to the maximal corresponding affine
3911 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3913 return isl_pw_aff_list_opt(list
, 1);
3916 /* Mark the domains of "pwaff" as rational.
3918 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3922 pwaff
= isl_pw_aff_cow(pwaff
);
3928 for (i
= 0; i
< pwaff
->n
; ++i
) {
3929 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3930 if (!pwaff
->p
[i
].set
)
3931 return isl_pw_aff_free(pwaff
);
3937 /* Mark the domains of the elements of "list" as rational.
3939 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3940 __isl_take isl_pw_aff_list
*list
)
3950 for (i
= 0; i
< n
; ++i
) {
3953 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3954 pa
= isl_pw_aff_set_rational(pa
);
3955 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3961 /* Do the parameters of "aff" match those of "space"?
3963 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3964 __isl_keep isl_space
*space
)
3966 isl_space
*aff_space
;
3970 return isl_bool_error
;
3972 aff_space
= isl_aff_get_domain_space(aff
);
3974 match
= isl_space_has_equal_params(space
, aff_space
);
3976 isl_space_free(aff_space
);
3980 /* Check that the domain space of "aff" matches "space".
3982 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3983 __isl_keep isl_space
*space
)
3985 isl_space
*aff_space
;
3989 return isl_stat_error
;
3991 aff_space
= isl_aff_get_domain_space(aff
);
3993 match
= isl_space_has_equal_params(space
, aff_space
);
3997 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3998 "parameters don't match", goto error
);
3999 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
4000 aff_space
, isl_dim_set
);
4004 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4005 "domains don't match", goto error
);
4006 isl_space_free(aff_space
);
4009 isl_space_free(aff_space
);
4010 return isl_stat_error
;
4013 /* Return the shared (universe) domain of the elements of "ma".
4015 * Since an isl_multi_aff (and an isl_aff) is always total,
4016 * the domain is always the universe set in its domain space.
4017 * This is a helper function for use in the generic isl_multi_*_bind.
4019 static __isl_give isl_basic_set
*isl_multi_aff_domain(
4020 __isl_take isl_multi_aff
*ma
)
4024 space
= isl_multi_aff_get_space(ma
);
4025 isl_multi_aff_free(ma
);
4027 return isl_basic_set_universe(isl_space_domain(space
));
4033 #include <isl_multi_no_explicit_domain.c>
4034 #include <isl_multi_templ.c>
4035 #include <isl_multi_un_op_templ.c>
4036 #include <isl_multi_bin_val_templ.c>
4037 #include <isl_multi_add_constant_templ.c>
4038 #include <isl_multi_align_set.c>
4039 #include <isl_multi_arith_templ.c>
4040 #include <isl_multi_bind_domain_templ.c>
4041 #include <isl_multi_cmp.c>
4042 #include <isl_multi_dim_id_templ.c>
4043 #include <isl_multi_dims.c>
4044 #include <isl_multi_floor.c>
4045 #include <isl_multi_from_base_templ.c>
4046 #include <isl_multi_identity_templ.c>
4047 #include <isl_multi_insert_domain_templ.c>
4048 #include <isl_multi_locals_templ.c>
4049 #include <isl_multi_move_dims_templ.c>
4050 #include <isl_multi_nan_templ.c>
4051 #include <isl_multi_product_templ.c>
4052 #include <isl_multi_splice_templ.c>
4053 #include <isl_multi_tuple_id_templ.c>
4054 #include <isl_multi_unbind_params_templ.c>
4055 #include <isl_multi_zero_templ.c>
4059 #include <isl_multi_check_domain_templ.c>
4060 #include <isl_multi_apply_set_no_explicit_domain_templ.c>
4061 #include <isl_multi_gist.c>
4064 #define DOMBASE basic_set
4065 #include <isl_multi_bind_templ.c>
4067 /* Construct an isl_multi_aff living in "space" that corresponds
4068 * to the affine transformation matrix "mat".
4070 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
4071 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
4074 isl_local_space
*ls
= NULL
;
4075 isl_multi_aff
*ma
= NULL
;
4076 isl_size n_row
, n_col
, n_out
, total
;
4082 ctx
= isl_mat_get_ctx(mat
);
4084 n_row
= isl_mat_rows(mat
);
4085 n_col
= isl_mat_cols(mat
);
4086 n_out
= isl_space_dim(space
, isl_dim_out
);
4087 total
= isl_space_dim(space
, isl_dim_all
);
4088 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
4091 isl_die(ctx
, isl_error_invalid
,
4092 "insufficient number of rows", goto error
);
4094 isl_die(ctx
, isl_error_invalid
,
4095 "insufficient number of columns", goto error
);
4096 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
4097 isl_die(ctx
, isl_error_invalid
,
4098 "dimension mismatch", goto error
);
4100 ma
= isl_multi_aff_zero(isl_space_copy(space
));
4101 space
= isl_space_domain(space
);
4102 ls
= isl_local_space_from_space(isl_space_copy(space
));
4104 for (i
= 0; i
< n_row
- 1; ++i
) {
4108 v
= isl_vec_alloc(ctx
, 1 + n_col
);
4111 isl_int_set(v
->el
[0], mat
->row
[0][0]);
4112 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
4113 v
= isl_vec_normalize(v
);
4114 aff
= isl_aff_alloc_vec_validated(isl_local_space_copy(ls
), v
);
4115 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4118 isl_space_free(space
);
4119 isl_local_space_free(ls
);
4123 isl_space_free(space
);
4124 isl_local_space_free(ls
);
4126 isl_multi_aff_free(ma
);
4130 /* Return the constant terms of the affine expressions of "ma".
4132 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
4133 __isl_keep isl_multi_aff
*ma
)
4140 n
= isl_multi_aff_size(ma
);
4143 space
= isl_space_range(isl_multi_aff_get_space(ma
));
4144 space
= isl_space_drop_all_params(space
);
4145 mv
= isl_multi_val_zero(space
);
4147 for (i
= 0; i
< n
; ++i
) {
4151 aff
= isl_multi_aff_get_at(ma
, i
);
4152 val
= isl_aff_get_constant_val(aff
);
4154 mv
= isl_multi_val_set_at(mv
, i
, val
);
4160 /* Remove any internal structure of the domain of "ma".
4161 * If there is any such internal structure in the input,
4162 * then the name of the corresponding space is also removed.
4164 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4165 __isl_take isl_multi_aff
*ma
)
4172 if (!ma
->space
->nested
[0])
4175 space
= isl_multi_aff_get_space(ma
);
4176 space
= isl_space_flatten_domain(space
);
4177 ma
= isl_multi_aff_reset_space(ma
, space
);
4182 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4183 * of the space to its domain.
4185 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4189 isl_local_space
*ls
;
4194 if (!isl_space_is_map(space
))
4195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4196 "not a map space", goto error
);
4198 n_in
= isl_space_dim(space
, isl_dim_in
);
4201 space
= isl_space_domain_map(space
);
4203 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4205 isl_space_free(space
);
4209 space
= isl_space_domain(space
);
4210 ls
= isl_local_space_from_space(space
);
4211 for (i
= 0; i
< n_in
; ++i
) {
4214 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4216 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4218 isl_local_space_free(ls
);
4221 isl_space_free(space
);
4225 /* This function performs the same operation as isl_multi_aff_domain_map,
4226 * but is considered as a function on an isl_space when exported.
4228 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4229 __isl_take isl_space
*space
)
4231 return isl_multi_aff_domain_map(space
);
4234 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4235 * of the space to its range.
4237 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4240 isl_size n_in
, n_out
;
4241 isl_local_space
*ls
;
4246 if (!isl_space_is_map(space
))
4247 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4248 "not a map space", goto error
);
4250 n_in
= isl_space_dim(space
, isl_dim_in
);
4251 n_out
= isl_space_dim(space
, isl_dim_out
);
4252 if (n_in
< 0 || n_out
< 0)
4254 space
= isl_space_range_map(space
);
4256 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4258 isl_space_free(space
);
4262 space
= isl_space_domain(space
);
4263 ls
= isl_local_space_from_space(space
);
4264 for (i
= 0; i
< n_out
; ++i
) {
4267 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4268 isl_dim_set
, n_in
+ i
);
4269 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4271 isl_local_space_free(ls
);
4274 isl_space_free(space
);
4278 /* This function performs the same operation as isl_multi_aff_range_map,
4279 * but is considered as a function on an isl_space when exported.
4281 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4282 __isl_take isl_space
*space
)
4284 return isl_multi_aff_range_map(space
);
4287 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4288 * of the space to its domain.
4290 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4291 __isl_take isl_space
*space
)
4293 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4296 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4297 * but is considered as a function on an isl_space when exported.
4299 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4300 __isl_take isl_space
*space
)
4302 return isl_pw_multi_aff_domain_map(space
);
4305 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4306 * of the space to its range.
4308 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4309 __isl_take isl_space
*space
)
4311 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4314 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4315 * but is considered as a function on an isl_space when exported.
4317 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4318 __isl_take isl_space
*space
)
4320 return isl_pw_multi_aff_range_map(space
);
4323 /* Given the space of a set and a range of set dimensions,
4324 * construct an isl_multi_aff that projects out those dimensions.
4326 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4327 __isl_take isl_space
*space
, enum isl_dim_type type
,
4328 unsigned first
, unsigned n
)
4332 isl_local_space
*ls
;
4337 if (!isl_space_is_set(space
))
4338 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4339 "expecting set space", goto error
);
4340 if (type
!= isl_dim_set
)
4341 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4342 "only set dimensions can be projected out", goto error
);
4343 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4346 dim
= isl_space_dim(space
, isl_dim_set
);
4350 space
= isl_space_from_domain(space
);
4351 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4354 return isl_multi_aff_alloc(space
);
4356 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4357 space
= isl_space_domain(space
);
4358 ls
= isl_local_space_from_space(space
);
4360 for (i
= 0; i
< first
; ++i
) {
4363 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4365 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4368 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4371 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4372 isl_dim_set
, first
+ n
+ i
);
4373 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4376 isl_local_space_free(ls
);
4379 isl_space_free(space
);
4383 /* Given the space of a set and a range of set dimensions,
4384 * construct an isl_pw_multi_aff that projects out those dimensions.
4386 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4387 __isl_take isl_space
*space
, enum isl_dim_type type
,
4388 unsigned first
, unsigned n
)
4392 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4393 return isl_pw_multi_aff_from_multi_aff(ma
);
4396 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4397 * but is considered as a function on an isl_multi_aff when exported.
4399 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4400 __isl_take isl_multi_aff
*ma
)
4402 return isl_pw_multi_aff_from_multi_aff(ma
);
4405 /* Create a piecewise multi-affine expression in the given space that maps each
4406 * input dimension to the corresponding output dimension.
4408 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4409 __isl_take isl_space
*space
)
4411 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4414 /* Create a piecewise multi expression that maps elements in the given space
4417 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4418 __isl_take isl_space
*space
)
4422 ma
= isl_multi_aff_identity_on_domain_space(space
);
4423 return isl_pw_multi_aff_from_multi_aff(ma
);
4426 /* This function performs the same operation as
4427 * isl_pw_multi_aff_identity_on_domain_space,
4428 * but is considered as a function on an isl_space when exported.
4430 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4431 __isl_take isl_space
*space
)
4433 return isl_pw_multi_aff_identity_on_domain_space(space
);
4436 /* Exploit the equalities in "eq" to simplify the affine expressions.
4438 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4439 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4444 n
= isl_multi_aff_size(maff
);
4448 for (i
= 0; i
< n
; ++i
) {
4451 aff
= isl_multi_aff_take_at(maff
, i
);
4452 aff
= isl_aff_substitute_equalities(aff
,
4453 isl_basic_set_copy(eq
));
4454 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4457 isl_basic_set_free(eq
);
4460 isl_basic_set_free(eq
);
4461 isl_multi_aff_free(maff
);
4465 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4471 n
= isl_multi_aff_size(maff
);
4473 return isl_multi_aff_free(maff
);
4475 for (i
= 0; i
< n
; ++i
) {
4478 aff
= isl_multi_aff_take_at(maff
, i
);
4479 aff
= isl_aff_scale(aff
, f
);
4480 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4486 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4487 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4489 maff1
= isl_multi_aff_add(maff1
, maff2
);
4490 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4494 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4497 return isl_bool_error
;
4499 return isl_bool_false
;
4502 /* Return the set of domain elements where "ma1" is lexicographically
4503 * smaller than or equal to "ma2".
4505 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4506 __isl_take isl_multi_aff
*ma2
)
4508 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4511 /* Return the set of domain elements where "ma1" is lexicographically
4512 * smaller than "ma2".
4514 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4515 __isl_take isl_multi_aff
*ma2
)
4517 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4520 /* Return the set of domain elements where "ma1" is lexicographically
4521 * greater than to "ma2". If "equal" is set, then include the domain
4522 * elements where they are equal.
4523 * Do this for the case where there are no entries.
4524 * In this case, "ma1" cannot be greater than "ma2",
4525 * but it is (greater than or) equal to "ma2".
4527 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4528 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4532 space
= isl_multi_aff_get_domain_space(ma1
);
4534 isl_multi_aff_free(ma1
);
4535 isl_multi_aff_free(ma2
);
4538 return isl_set_universe(space
);
4540 return isl_set_empty(space
);
4543 /* Return the set where entry "i" of "ma1" and "ma2"
4544 * satisfy the relation prescribed by "cmp".
4546 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4547 __isl_keep isl_multi_aff
*ma2
, int i
,
4548 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4549 __isl_take isl_aff
*aff2
))
4551 isl_aff
*aff1
, *aff2
;
4553 aff1
= isl_multi_aff_get_at(ma1
, i
);
4554 aff2
= isl_multi_aff_get_at(ma2
, i
);
4555 return cmp(aff1
, aff2
);
4558 /* Return the set of domain elements where "ma1" is lexicographically
4559 * greater than to "ma2". If "equal" is set, then include the domain
4560 * elements where they are equal.
4562 * In particular, for all but the final entry,
4563 * include the set of elements where this entry is strictly greater in "ma1"
4564 * and all previous entries are equal.
4565 * The final entry is also allowed to be equal in the two functions
4566 * if "equal" is set.
4568 * The case where there are no entries is handled separately.
4570 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4571 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4580 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4582 n
= isl_multi_aff_size(ma1
);
4586 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4588 space
= isl_multi_aff_get_domain_space(ma1
);
4589 res
= isl_set_empty(isl_space_copy(space
));
4590 equal_set
= isl_set_universe(space
);
4592 for (i
= 0; i
+ 1 < n
; ++i
) {
4596 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4597 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4598 res
= isl_set_union(res
, gt
);
4599 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4600 equal_set
= isl_set_intersect(equal_set
, eq
);
4602 empty
= isl_set_is_empty(equal_set
);
4603 if (empty
>= 0 && empty
)
4608 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4610 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4611 isl_multi_aff_free(ma1
);
4612 isl_multi_aff_free(ma2
);
4614 gte
= isl_set_intersect(gte
, equal_set
);
4615 return isl_set_union(res
, gte
);
4617 isl_multi_aff_free(ma1
);
4618 isl_multi_aff_free(ma2
);
4622 /* Return the set of domain elements where "ma1" is lexicographically
4623 * greater than or equal to "ma2".
4625 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4626 __isl_take isl_multi_aff
*ma2
)
4628 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4631 /* Return the set of domain elements where "ma1" is lexicographically
4632 * greater than "ma2".
4634 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4635 __isl_take isl_multi_aff
*ma2
)
4637 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4640 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4643 #define PW isl_pw_multi_aff
4645 #define BASE multi_aff
4647 #define EL_IS_ZERO is_empty
4651 #define IS_ZERO is_empty
4654 #undef DEFAULT_IS_ZERO
4655 #define DEFAULT_IS_ZERO 0
4657 #include <isl_pw_templ.c>
4658 #include <isl_pw_un_op_templ.c>
4659 #include <isl_pw_add_constant_multi_val_templ.c>
4660 #include <isl_pw_add_constant_val_templ.c>
4661 #include <isl_pw_add_disjoint_templ.c>
4662 #include <isl_pw_bind_domain_templ.c>
4663 #include <isl_pw_fix_templ.c>
4664 #include <isl_pw_from_range_templ.c>
4665 #include <isl_pw_insert_dims_templ.c>
4666 #include <isl_pw_insert_domain_templ.c>
4667 #include <isl_pw_locals_templ.c>
4668 #include <isl_pw_move_dims_templ.c>
4669 #include <isl_pw_neg_templ.c>
4670 #include <isl_pw_pullback_templ.c>
4671 #include <isl_pw_range_tuple_id_templ.c>
4672 #include <isl_pw_union_opt.c>
4675 #define BASE pw_multi_aff
4677 #include <isl_union_multi.c>
4678 #include "isl_union_locals_templ.c"
4679 #include <isl_union_neg.c>
4680 #include <isl_union_sub_templ.c>
4683 #define BASE multi_aff
4685 #include <isl_union_pw_templ.c>
4687 /* Generic function for extracting a factor from a product "pma".
4688 * "check_space" checks that the space is that of the right kind of product.
4689 * "space_factor" extracts the factor from the space.
4690 * "multi_aff_factor" extracts the factor from the constituent functions.
4692 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4693 __isl_take isl_pw_multi_aff
*pma
,
4694 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4695 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4696 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4697 __isl_take isl_multi_aff
*ma
))
4702 if (check_space(pma
) < 0)
4703 return isl_pw_multi_aff_free(pma
);
4705 space
= isl_pw_multi_aff_take_space(pma
);
4706 space
= space_factor(space
);
4708 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4711 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4712 ma
= multi_aff_factor(ma
);
4713 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4716 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4721 /* Is the range of "pma" a wrapped relation?
4723 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4724 __isl_keep isl_pw_multi_aff
*pma
)
4726 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4729 /* Check that the range of "pma" is a product.
4731 static isl_stat
pw_multi_aff_check_range_product(
4732 __isl_keep isl_pw_multi_aff
*pma
)
4736 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4738 return isl_stat_error
;
4740 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4741 "range is not a product", return isl_stat_error
);
4745 /* Given a function A -> [B -> C], extract the function A -> B.
4747 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4748 __isl_take isl_pw_multi_aff
*pma
)
4750 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4751 &isl_space_range_factor_domain
,
4752 &isl_multi_aff_range_factor_domain
);
4755 /* Given a function A -> [B -> C], extract the function A -> C.
4757 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4758 __isl_take isl_pw_multi_aff
*pma
)
4760 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4761 &isl_space_range_factor_range
,
4762 &isl_multi_aff_range_factor_range
);
4765 /* Given two piecewise multi affine expressions, return a piecewise
4766 * multi-affine expression defined on the union of the definition domains
4767 * of the inputs that is equal to the lexicographic maximum of the two
4768 * inputs on each cell. If only one of the two inputs is defined on
4769 * a given cell, then it is considered to be the maximum.
4771 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4772 __isl_take isl_pw_multi_aff
*pma1
,
4773 __isl_take isl_pw_multi_aff
*pma2
)
4775 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4776 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4777 &isl_multi_aff_lex_ge_set
);
4780 /* Given two piecewise multi affine expressions, return a piecewise
4781 * multi-affine expression defined on the union of the definition domains
4782 * of the inputs that is equal to the lexicographic minimum of the two
4783 * inputs on each cell. If only one of the two inputs is defined on
4784 * a given cell, then it is considered to be the minimum.
4786 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4787 __isl_take isl_pw_multi_aff
*pma1
,
4788 __isl_take isl_pw_multi_aff
*pma2
)
4790 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4791 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4792 &isl_multi_aff_lex_le_set
);
4795 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4796 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4798 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4799 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4800 &isl_multi_aff_add
);
4803 /* Subtract "pma2" from "pma1" and return the result.
4805 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4806 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4808 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4809 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4810 &isl_multi_aff_sub
);
4813 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4814 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4816 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4817 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4821 isl_pw_multi_aff
*res
;
4823 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4826 n
= pma1
->n
* pma2
->n
;
4827 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4828 isl_space_copy(pma2
->dim
));
4829 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4831 for (i
= 0; i
< pma1
->n
; ++i
) {
4832 for (j
= 0; j
< pma2
->n
; ++j
) {
4836 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4837 isl_set_copy(pma2
->p
[j
].set
));
4838 ma
= isl_multi_aff_product(
4839 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4840 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4841 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4845 isl_pw_multi_aff_free(pma1
);
4846 isl_pw_multi_aff_free(pma2
);
4849 isl_pw_multi_aff_free(pma1
);
4850 isl_pw_multi_aff_free(pma2
);
4854 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4855 * denominator "denom".
4856 * "denom" is allowed to be negative, in which case the actual denominator
4857 * is -denom and the expressions are added instead.
4859 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4860 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4866 first
= isl_seq_first_non_zero(c
, n
);
4870 sign
= isl_int_sgn(denom
);
4872 isl_int_abs(d
, denom
);
4873 for (i
= first
; i
< n
; ++i
) {
4876 if (isl_int_is_zero(c
[i
]))
4878 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4879 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4880 aff_i
= isl_aff_scale_down(aff_i
, d
);
4882 aff
= isl_aff_sub(aff
, aff_i
);
4884 aff
= isl_aff_add(aff
, aff_i
);
4891 /* Extract an affine expression that expresses the output dimension "pos"
4892 * of "bmap" in terms of the parameters and input dimensions from
4894 * Note that this expression may involve integer divisions defined
4895 * in terms of parameters and input dimensions.
4896 * The equality may also involve references to earlier (but not later)
4897 * output dimensions. These are replaced by the corresponding elements
4900 * If the equality is of the form
4902 * f(i) + h(j) + a x + g(i) = 0,
4904 * with f(i) a linear combinations of the parameters and input dimensions,
4905 * g(i) a linear combination of integer divisions defined in terms of the same
4906 * and h(j) a linear combinations of earlier output dimensions,
4907 * then the affine expression is
4909 * (-f(i) - g(i))/a - h(j)/a
4911 * If the equality is of the form
4913 * f(i) + h(j) - a x + g(i) = 0,
4915 * then the affine expression is
4917 * (f(i) + g(i))/a - h(j)/(-a)
4920 * If "div" refers to an integer division (i.e., it is smaller than
4921 * the number of integer divisions), then the equality constraint
4922 * does involve an integer division (the one at position "div") that
4923 * is defined in terms of output dimensions. However, this integer
4924 * division can be eliminated by exploiting a pair of constraints
4925 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4926 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4928 * In particular, let
4930 * x = e(i) + m floor(...)
4932 * with e(i) the expression derived above and floor(...) the integer
4933 * division involving output dimensions.
4944 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4945 * = (e(i) - l) mod m
4949 * x - l = (e(i) - l) mod m
4953 * x = ((e(i) - l) mod m) + l
4955 * The variable "shift" below contains the expression -l, which may
4956 * also involve a linear combination of earlier output dimensions.
4958 static __isl_give isl_aff
*extract_aff_from_equality(
4959 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4960 __isl_keep isl_multi_aff
*ma
)
4963 isl_size n_div
, n_out
;
4965 isl_local_space
*ls
;
4966 isl_aff
*aff
, *shift
;
4969 ctx
= isl_basic_map_get_ctx(bmap
);
4970 ls
= isl_basic_map_get_local_space(bmap
);
4971 ls
= isl_local_space_domain(ls
);
4972 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4975 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4976 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4977 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4978 if (n_out
< 0 || n_div
< 0)
4980 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4981 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4982 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4983 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4985 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4986 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4987 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4990 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4991 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4992 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4993 bmap
->eq
[eq
][o_out
+ pos
]);
4995 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4998 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4999 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
5000 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
5001 isl_int_set_si(shift
->v
->el
[0], 1);
5002 shift
= subtract_initial(shift
, ma
, pos
,
5003 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
5004 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
5005 mod
= isl_val_int_from_isl_int(ctx
,
5006 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
5007 mod
= isl_val_abs(mod
);
5008 aff
= isl_aff_mod_val(aff
, mod
);
5009 aff
= isl_aff_sub(aff
, shift
);
5012 isl_local_space_free(ls
);
5015 isl_local_space_free(ls
);
5020 /* Given a basic map with output dimensions defined
5021 * in terms of the parameters input dimensions and earlier
5022 * output dimensions using an equality (and possibly a pair on inequalities),
5023 * extract an isl_aff that expresses output dimension "pos" in terms
5024 * of the parameters and input dimensions.
5025 * Note that this expression may involve integer divisions defined
5026 * in terms of parameters and input dimensions.
5027 * "ma" contains the expressions corresponding to earlier output dimensions.
5029 * This function shares some similarities with
5030 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
5032 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
5033 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
5040 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
5041 if (eq
>= bmap
->n_eq
)
5042 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5043 "unable to find suitable equality", return NULL
);
5044 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
5046 aff
= isl_aff_remove_unused_divs(aff
);
5050 /* Given a basic map where each output dimension is defined
5051 * in terms of the parameters and input dimensions using an equality,
5052 * extract an isl_multi_aff that expresses the output dimensions in terms
5053 * of the parameters and input dimensions.
5055 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
5056 __isl_take isl_basic_map
*bmap
)
5065 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
5066 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5068 ma
= isl_multi_aff_free(ma
);
5070 for (i
= 0; i
< n_out
; ++i
) {
5073 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
5074 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5077 isl_basic_map_free(bmap
);
5082 /* Given a basic set where each set dimension is defined
5083 * in terms of the parameters using an equality,
5084 * extract an isl_multi_aff that expresses the set dimensions in terms
5085 * of the parameters.
5087 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
5088 __isl_take isl_basic_set
*bset
)
5090 return extract_isl_multi_aff_from_basic_map(bset
);
5093 /* Create an isl_pw_multi_aff that is equivalent to
5094 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5095 * The given basic map is such that each output dimension is defined
5096 * in terms of the parameters and input dimensions using an equality.
5098 * Since some applications expect the result of isl_pw_multi_aff_from_map
5099 * to only contain integer affine expressions, we compute the floor
5100 * of the expression before returning.
5102 * Remove all constraints involving local variables without
5103 * an explicit representation (resulting in the removal of those
5104 * local variables) prior to the actual extraction to ensure
5105 * that the local spaces in which the resulting affine expressions
5106 * are created do not contain any unknown local variables.
5107 * Removing such constraints is safe because constraints involving
5108 * unknown local variables are not used to determine whether
5109 * a basic map is obviously single-valued.
5111 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
5112 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
5116 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
5117 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
5118 ma
= isl_multi_aff_floor(ma
);
5119 return isl_pw_multi_aff_alloc(domain
, ma
);
5122 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5123 * This obviously only works if the input "map" is single-valued.
5124 * If so, we compute the lexicographic minimum of the image in the form
5125 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5126 * to its lexicographic minimum.
5127 * If the input is not single-valued, we produce an error.
5129 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
5130 __isl_take isl_map
*map
)
5134 isl_pw_multi_aff
*pma
;
5136 sv
= isl_map_is_single_valued(map
);
5140 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5141 "map is not single-valued", goto error
);
5142 map
= isl_map_make_disjoint(map
);
5146 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5148 for (i
= 0; i
< map
->n
; ++i
) {
5149 isl_pw_multi_aff
*pma_i
;
5150 isl_basic_map
*bmap
;
5151 bmap
= isl_basic_map_copy(map
->p
[i
]);
5152 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5153 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5163 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5164 * taking into account that the output dimension at position "d"
5165 * can be represented as
5167 * x = floor((e(...) + c1) / m)
5169 * given that constraint "i" is of the form
5171 * e(...) + c1 - m x >= 0
5174 * Let "map" be of the form
5178 * We construct a mapping
5180 * A -> [A -> x = floor(...)]
5182 * apply that to the map, obtaining
5184 * [A -> x = floor(...)] -> B
5186 * and equate dimension "d" to x.
5187 * We then compute a isl_pw_multi_aff representation of the resulting map
5188 * and plug in the mapping above.
5190 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5191 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5194 isl_space
*space
= NULL
;
5195 isl_local_space
*ls
;
5203 isl_pw_multi_aff
*pma
;
5206 is_set
= isl_map_is_set(map
);
5210 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5211 ctx
= isl_map_get_ctx(map
);
5212 space
= isl_space_domain(isl_map_get_space(map
));
5213 n_in
= isl_space_dim(space
, isl_dim_set
);
5214 n
= isl_space_dim(space
, isl_dim_all
);
5215 if (n_in
< 0 || n
< 0)
5218 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5220 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5221 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5223 isl_basic_map_free(hull
);
5225 ls
= isl_local_space_from_space(isl_space_copy(space
));
5226 aff
= isl_aff_alloc_vec_validated(ls
, v
);
5227 aff
= isl_aff_floor(aff
);
5229 isl_space_free(space
);
5230 ma
= isl_multi_aff_from_aff(aff
);
5232 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5233 ma
= isl_multi_aff_range_product(ma
,
5234 isl_multi_aff_from_aff(aff
));
5237 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5238 map
= isl_map_apply_domain(map
, insert
);
5239 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5240 pma
= isl_pw_multi_aff_from_map(map
);
5241 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5245 isl_space_free(space
);
5247 isl_basic_map_free(hull
);
5251 /* Is constraint "c" of the form
5253 * e(...) + c1 - m x >= 0
5257 * -e(...) + c2 + m x >= 0
5259 * where m > 1 and e only depends on parameters and input dimensions?
5261 * "offset" is the offset of the output dimensions
5262 * "pos" is the position of output dimension x.
5264 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5266 if (isl_int_is_zero(c
[offset
+ d
]))
5268 if (isl_int_is_one(c
[offset
+ d
]))
5270 if (isl_int_is_negone(c
[offset
+ d
]))
5272 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5274 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5275 total
- (offset
+ d
+ 1)) != -1)
5280 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5282 * As a special case, we first check if there is any pair of constraints,
5283 * shared by all the basic maps in "map" that force a given dimension
5284 * to be equal to the floor of some affine combination of the input dimensions.
5286 * In particular, if we can find two constraints
5288 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5292 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5294 * where m > 1 and e only depends on parameters and input dimensions,
5297 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5299 * then we know that we can take
5301 * x = floor((e(...) + c1) / m)
5303 * without having to perform any computation.
5305 * Note that we know that
5309 * If c1 + c2 were 0, then we would have detected an equality during
5310 * simplification. If c1 + c2 were negative, then we would have detected
5313 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5314 __isl_take isl_map
*map
)
5322 isl_basic_map
*hull
;
5324 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5325 dim
= isl_map_dim(map
, isl_dim_out
);
5326 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5327 if (dim
< 0 || total
< 0)
5331 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5333 for (d
= 0; d
< dim
; ++d
) {
5334 for (i
= 0; i
< n
; ++i
) {
5335 if (!is_potential_div_constraint(hull
->ineq
[i
],
5336 offset
, d
, 1 + total
))
5338 for (j
= i
+ 1; j
< n
; ++j
) {
5339 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5340 hull
->ineq
[j
] + 1, total
))
5342 isl_int_add(sum
, hull
->ineq
[i
][0],
5344 if (isl_int_abs_lt(sum
,
5345 hull
->ineq
[i
][offset
+ d
]))
5352 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5354 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5358 isl_basic_map_free(hull
);
5359 return pw_multi_aff_from_map_base(map
);
5362 isl_basic_map_free(hull
);
5366 /* Given an affine expression
5368 * [A -> B] -> f(A,B)
5370 * construct an isl_multi_aff
5374 * such that dimension "d" in B' is set to "aff" and the remaining
5375 * dimensions are set equal to the corresponding dimensions in B.
5376 * "n_in" is the dimension of the space A.
5377 * "n_out" is the dimension of the space B.
5379 * If "is_set" is set, then the affine expression is of the form
5383 * and we construct an isl_multi_aff
5387 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5388 unsigned n_in
, unsigned n_out
, int is_set
)
5392 isl_space
*space
, *space2
;
5393 isl_local_space
*ls
;
5395 space
= isl_aff_get_domain_space(aff
);
5396 ls
= isl_local_space_from_space(isl_space_copy(space
));
5397 space2
= isl_space_copy(space
);
5399 space2
= isl_space_range(isl_space_unwrap(space2
));
5400 space
= isl_space_map_from_domain_and_range(space
, space2
);
5401 ma
= isl_multi_aff_alloc(space
);
5402 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5404 for (i
= 0; i
< n_out
; ++i
) {
5407 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5408 isl_dim_set
, n_in
+ i
);
5409 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5412 isl_local_space_free(ls
);
5417 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5418 * taking into account that the dimension at position "d" can be written as
5420 * x = m a + f(..) (1)
5422 * where m is equal to "gcd".
5423 * "i" is the index of the equality in "hull" that defines f(..).
5424 * In particular, the equality is of the form
5426 * f(..) - x + m g(existentials) = 0
5430 * -f(..) + x + m g(existentials) = 0
5432 * We basically plug (1) into "map", resulting in a map with "a"
5433 * in the range instead of "x". The corresponding isl_pw_multi_aff
5434 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5436 * Specifically, given the input map
5440 * We first wrap it into a set
5444 * and define (1) on top of the corresponding space, resulting in "aff".
5445 * We use this to create an isl_multi_aff that maps the output position "d"
5446 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5447 * We plug this into the wrapped map, unwrap the result and compute the
5448 * corresponding isl_pw_multi_aff.
5449 * The result is an expression
5457 * so that we can plug that into "aff", after extending the latter to
5463 * If "map" is actually a set, then there is no "A" space, meaning
5464 * that we do not need to perform any wrapping, and that the result
5465 * of the recursive call is of the form
5469 * which is plugged into a mapping of the form
5473 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5474 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5479 isl_local_space
*ls
;
5482 isl_pw_multi_aff
*pma
, *id
;
5488 is_set
= isl_map_is_set(map
);
5492 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5493 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5494 if (n_in
< 0 || n_out
< 0)
5496 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5501 set
= isl_map_wrap(map
);
5502 space
= isl_space_map_from_set(isl_set_get_space(set
));
5503 ma
= isl_multi_aff_identity(space
);
5504 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5505 aff
= isl_aff_alloc(ls
);
5507 isl_int_set_si(aff
->v
->el
[0], 1);
5508 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5509 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5512 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5514 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5516 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5517 set
= isl_set_preimage_multi_aff(set
, ma
);
5519 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5524 map
= isl_set_unwrap(set
);
5525 pma
= isl_pw_multi_aff_from_map(map
);
5528 space
= isl_pw_multi_aff_get_domain_space(pma
);
5529 space
= isl_space_map_from_set(space
);
5530 id
= isl_pw_multi_aff_identity(space
);
5531 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5533 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5534 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5536 isl_basic_map_free(hull
);
5540 isl_basic_map_free(hull
);
5544 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5545 * "hull" contains the equalities valid for "map".
5547 * Check if any of the output dimensions is "strided".
5548 * That is, we check if it can be written as
5552 * with m greater than 1, a some combination of existentially quantified
5553 * variables and f an expression in the parameters and input dimensions.
5554 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5556 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5559 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5560 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5569 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5570 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5571 if (n_div
< 0 || n_out
< 0)
5575 isl_basic_map_free(hull
);
5576 return pw_multi_aff_from_map_check_div(map
);
5581 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5582 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5584 for (i
= 0; i
< n_out
; ++i
) {
5585 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5586 isl_int
*eq
= hull
->eq
[j
];
5587 isl_pw_multi_aff
*res
;
5589 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5590 !isl_int_is_negone(eq
[o_out
+ i
]))
5592 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5594 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5595 n_out
- (i
+ 1)) != -1)
5597 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5598 if (isl_int_is_zero(gcd
))
5600 if (isl_int_is_one(gcd
))
5603 res
= pw_multi_aff_from_map_stride(map
, hull
,
5611 isl_basic_map_free(hull
);
5612 return pw_multi_aff_from_map_check_div(map
);
5615 isl_basic_map_free(hull
);
5619 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5621 * As a special case, we first check if all output dimensions are uniquely
5622 * defined in terms of the parameters and input dimensions over the entire
5623 * domain. If so, we extract the desired isl_pw_multi_aff directly
5624 * from the affine hull of "map" and its domain.
5626 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5629 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5633 isl_basic_map
*hull
;
5635 n
= isl_map_n_basic_map(map
);
5640 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5641 hull
= isl_basic_map_plain_affine_hull(hull
);
5642 sv
= isl_basic_map_plain_is_single_valued(hull
);
5644 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5646 isl_basic_map_free(hull
);
5648 map
= isl_map_detect_equalities(map
);
5649 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5650 sv
= isl_basic_map_plain_is_single_valued(hull
);
5652 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5654 return pw_multi_aff_from_map_check_strides(map
, hull
);
5655 isl_basic_map_free(hull
);
5661 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5662 * but is considered as a function on an isl_map when exported.
5664 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5666 return isl_pw_multi_aff_from_map(map
);
5669 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5671 return isl_pw_multi_aff_from_map(set
);
5674 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5675 * but is considered as a function on an isl_set when exported.
5677 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5679 return isl_pw_multi_aff_from_set(set
);
5682 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5685 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5687 isl_union_pw_multi_aff
**upma
= user
;
5688 isl_pw_multi_aff
*pma
;
5690 pma
= isl_pw_multi_aff_from_map(map
);
5691 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5693 return *upma
? isl_stat_ok
: isl_stat_error
;
5696 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5699 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5700 __isl_take isl_aff
*aff
)
5703 isl_pw_multi_aff
*pma
;
5705 ma
= isl_multi_aff_from_aff(aff
);
5706 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5707 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5710 /* Try and create an isl_union_pw_multi_aff that is equivalent
5711 * to the given isl_union_map.
5712 * The isl_union_map is required to be single-valued in each space.
5713 * Otherwise, an error is produced.
5715 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5716 __isl_take isl_union_map
*umap
)
5719 isl_union_pw_multi_aff
*upma
;
5721 space
= isl_union_map_get_space(umap
);
5722 upma
= isl_union_pw_multi_aff_empty(space
);
5723 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5724 upma
= isl_union_pw_multi_aff_free(upma
);
5725 isl_union_map_free(umap
);
5730 /* This function performs the same operation as
5731 * isl_union_pw_multi_aff_from_union_map,
5732 * but is considered as a function on an isl_union_map when exported.
5734 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5735 __isl_take isl_union_map
*umap
)
5737 return isl_union_pw_multi_aff_from_union_map(umap
);
5740 /* Try and create an isl_union_pw_multi_aff that is equivalent
5741 * to the given isl_union_set.
5742 * The isl_union_set is required to be a singleton in each space.
5743 * Otherwise, an error is produced.
5745 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5746 __isl_take isl_union_set
*uset
)
5748 return isl_union_pw_multi_aff_from_union_map(uset
);
5751 /* Return the piecewise affine expression "set ? 1 : 0".
5753 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5756 isl_space
*space
= isl_set_get_space(set
);
5757 isl_local_space
*ls
= isl_local_space_from_space(space
);
5758 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5759 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5761 one
= isl_aff_add_constant_si(one
, 1);
5762 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5763 set
= isl_set_complement(set
);
5764 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5769 /* Plug in "subs" for dimension "type", "pos" of "aff".
5771 * Let i be the dimension to replace and let "subs" be of the form
5775 * and "aff" of the form
5781 * (a f + d g')/(m d)
5783 * where g' is the result of plugging in "subs" in each of the integer
5786 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5787 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5793 aff
= isl_aff_cow(aff
);
5795 return isl_aff_free(aff
);
5797 ctx
= isl_aff_get_ctx(aff
);
5798 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5799 isl_die(ctx
, isl_error_invalid
,
5800 "spaces don't match", return isl_aff_free(aff
));
5801 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5803 return isl_aff_free(aff
);
5805 isl_die(ctx
, isl_error_unsupported
,
5806 "cannot handle divs yet", return isl_aff_free(aff
));
5808 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5810 return isl_aff_free(aff
);
5812 aff
->v
= isl_vec_cow(aff
->v
);
5814 return isl_aff_free(aff
);
5816 pos
+= isl_local_space_offset(aff
->ls
, type
);
5819 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5820 aff
->v
->size
, subs
->v
->size
, v
);
5826 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5827 * expressions in "maff".
5829 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5830 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5831 __isl_keep isl_aff
*subs
)
5836 n
= isl_multi_aff_size(maff
);
5838 return isl_multi_aff_free(maff
);
5840 if (type
== isl_dim_in
)
5843 for (i
= 0; i
< n
; ++i
) {
5846 aff
= isl_multi_aff_take_at(maff
, i
);
5847 aff
= isl_aff_substitute(aff
, type
, pos
, subs
);
5848 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
5854 /* Plug in "subs" for input dimension "pos" of "pma".
5856 * pma is of the form
5860 * while subs is of the form
5862 * v' = B_j(v) -> S_j
5864 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5865 * has a contribution in the result, in particular
5867 * C_ij(S_j) -> M_i(S_j)
5869 * Note that plugging in S_j in C_ij may also result in an empty set
5870 * and this contribution should simply be discarded.
5872 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5873 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5874 __isl_keep isl_pw_aff
*subs
)
5877 isl_pw_multi_aff
*res
;
5880 return isl_pw_multi_aff_free(pma
);
5882 n
= pma
->n
* subs
->n
;
5883 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5885 for (i
= 0; i
< pma
->n
; ++i
) {
5886 for (j
= 0; j
< subs
->n
; ++j
) {
5888 isl_multi_aff
*res_ij
;
5891 common
= isl_set_intersect(
5892 isl_set_copy(pma
->p
[i
].set
),
5893 isl_set_copy(subs
->p
[j
].set
));
5894 common
= isl_set_substitute(common
,
5895 pos
, subs
->p
[j
].aff
);
5896 empty
= isl_set_plain_is_empty(common
);
5897 if (empty
< 0 || empty
) {
5898 isl_set_free(common
);
5904 res_ij
= isl_multi_aff_substitute(
5905 isl_multi_aff_copy(pma
->p
[i
].maff
),
5906 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5908 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5912 isl_pw_multi_aff_free(pma
);
5915 isl_pw_multi_aff_free(pma
);
5916 isl_pw_multi_aff_free(res
);
5920 /* Compute the preimage of a range of dimensions in the affine expression "src"
5921 * under "ma" and put the result in "dst". The number of dimensions in "src"
5922 * that precede the range is given by "n_before". The number of dimensions
5923 * in the range is given by the number of output dimensions of "ma".
5924 * The number of dimensions that follow the range is given by "n_after".
5925 * If "has_denom" is set (to one),
5926 * then "src" and "dst" have an extra initial denominator.
5927 * "n_div_ma" is the number of existentials in "ma"
5928 * "n_div_bset" is the number of existentials in "src"
5929 * The resulting "dst" (which is assumed to have been allocated by
5930 * the caller) contains coefficients for both sets of existentials,
5931 * first those in "ma" and then those in "src".
5932 * f, c1, c2 and g are temporary objects that have been initialized
5935 * Let src represent the expression
5937 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5939 * and let ma represent the expressions
5941 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5943 * We start out with the following expression for dst:
5945 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5947 * with the multiplication factor f initially equal to 1
5948 * and f \sum_i b_i v_i kept separately.
5949 * For each x_i that we substitute, we multiply the numerator
5950 * (and denominator) of dst by c_1 = m_i and add the numerator
5951 * of the x_i expression multiplied by c_2 = f b_i,
5952 * after removing the common factors of c_1 and c_2.
5953 * The multiplication factor f also needs to be multiplied by c_1
5954 * for the next x_j, j > i.
5956 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5957 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5958 int n_div_ma
, int n_div_bmap
,
5959 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5962 isl_size n_param
, n_in
, n_out
;
5965 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5966 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5967 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5968 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
5969 return isl_stat_error
;
5971 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5972 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5973 isl_seq_clr(dst
+ o_dst
, n_in
);
5976 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5979 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5981 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5983 isl_int_set_si(f
, 1);
5985 for (i
= 0; i
< n_out
; ++i
) {
5986 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5988 if (isl_int_is_zero(src
[offset
]))
5990 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
5991 isl_int_mul(c2
, f
, src
[offset
]);
5992 isl_int_gcd(g
, c1
, c2
);
5993 isl_int_divexact(c1
, c1
, g
);
5994 isl_int_divexact(c2
, c2
, g
);
5996 isl_int_mul(f
, f
, c1
);
5999 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6000 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
6001 o_dst
+= 1 + n_param
;
6002 o_src
+= 1 + n_param
;
6003 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
6005 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6006 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
6009 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
6011 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6012 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
6015 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
6017 isl_int_mul(dst
[0], dst
[0], c1
);
6023 /* Compute the pullback of "aff" by the function represented by "ma".
6024 * In other words, plug in "ma" in "aff". The result is an affine expression
6025 * defined over the domain space of "ma".
6027 * If "aff" is represented by
6029 * (a(p) + b x + c(divs))/d
6031 * and ma is represented by
6033 * x = D(p) + F(y) + G(divs')
6035 * then the result is
6037 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
6039 * The divs in the local space of the input are similarly adjusted
6040 * through a call to isl_local_space_preimage_multi_aff.
6042 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
6043 __isl_take isl_multi_aff
*ma
)
6045 isl_aff
*res
= NULL
;
6046 isl_local_space
*ls
;
6047 isl_size n_div_aff
, n_div_ma
;
6048 isl_int f
, c1
, c2
, g
;
6050 ma
= isl_multi_aff_align_divs(ma
);
6054 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
6055 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
6056 if (n_div_aff
< 0 || n_div_ma
< 0)
6059 ls
= isl_aff_get_domain_local_space(aff
);
6060 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
6061 res
= isl_aff_alloc(ls
);
6070 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
6071 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
6072 res
= isl_aff_free(res
);
6080 isl_multi_aff_free(ma
);
6081 res
= isl_aff_normalize(res
);
6085 isl_multi_aff_free(ma
);
6090 /* Compute the pullback of "aff1" by the function represented by "aff2".
6091 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6092 * defined over the domain space of "aff1".
6094 * The domain of "aff1" should match the range of "aff2", which means
6095 * that it should be single-dimensional.
6097 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
6098 __isl_take isl_aff
*aff2
)
6102 ma
= isl_multi_aff_from_aff(aff2
);
6103 return isl_aff_pullback_multi_aff(aff1
, ma
);
6106 /* Compute the pullback of "ma1" by the function represented by "ma2".
6107 * In other words, plug in "ma2" in "ma1".
6109 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
6110 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
6114 isl_space
*space
= NULL
;
6116 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
6117 ma2
= isl_multi_aff_align_divs(ma2
);
6118 n
= isl_multi_aff_size(ma1
);
6122 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
6123 isl_multi_aff_get_space(ma1
));
6125 for (i
= 0; i
< n
; ++i
) {
6128 aff
= isl_multi_aff_take_at(ma1
, i
);
6129 aff
= isl_aff_pullback_multi_aff(aff
, isl_multi_aff_copy(ma2
));
6130 ma1
= isl_multi_aff_restore_at(ma1
, i
, aff
);
6133 ma1
= isl_multi_aff_reset_space(ma1
, space
);
6134 isl_multi_aff_free(ma2
);
6137 isl_space_free(space
);
6138 isl_multi_aff_free(ma2
);
6139 isl_multi_aff_free(ma1
);
6143 /* Extend the local space of "dst" to include the divs
6144 * in the local space of "src".
6146 * If "src" does not have any divs or if the local spaces of "dst" and
6147 * "src" are the same, then no extension is required.
6149 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6150 __isl_keep isl_aff
*src
)
6153 isl_size src_n_div
, dst_n_div
;
6160 return isl_aff_free(dst
);
6162 ctx
= isl_aff_get_ctx(src
);
6163 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6165 return isl_aff_free(dst
);
6167 isl_die(ctx
, isl_error_invalid
,
6168 "spaces don't match", goto error
);
6170 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6171 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6174 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6175 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6176 return isl_aff_free(dst
);
6180 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6181 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6182 if (!exp1
|| (dst_n_div
&& !exp2
))
6185 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6186 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6194 return isl_aff_free(dst
);
6197 /* Adjust the local spaces of the affine expressions in "maff"
6198 * such that they all have the save divs.
6200 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6201 __isl_take isl_multi_aff
*maff
)
6207 n
= isl_multi_aff_size(maff
);
6209 return isl_multi_aff_free(maff
);
6213 aff_0
= isl_multi_aff_take_at(maff
, 0);
6214 for (i
= 1; i
< n
; ++i
) {
6217 aff_i
= isl_multi_aff_peek_at(maff
, i
);
6218 aff_0
= isl_aff_align_divs(aff_0
, aff_i
);
6220 maff
= isl_multi_aff_restore_at(maff
, 0, aff_0
);
6222 aff_0
= isl_multi_aff_peek_at(maff
, 0);
6223 for (i
= 1; i
< n
; ++i
) {
6226 aff_i
= isl_multi_aff_take_at(maff
, i
);
6227 aff_i
= isl_aff_align_divs(aff_i
, aff_0
);
6228 maff
= isl_multi_aff_restore_at(maff
, i
, aff_i
);
6234 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6236 aff
= isl_aff_cow(aff
);
6240 aff
->ls
= isl_local_space_lift(aff
->ls
);
6242 return isl_aff_free(aff
);
6247 /* Lift "maff" to a space with extra dimensions such that the result
6248 * has no more existentially quantified variables.
6249 * If "ls" is not NULL, then *ls is assigned the local space that lies
6250 * at the basis of the lifting applied to "maff".
6252 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6253 __isl_give isl_local_space
**ls
)
6263 n
= isl_multi_aff_size(maff
);
6265 return isl_multi_aff_free(maff
);
6269 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6270 *ls
= isl_local_space_from_space(space
);
6272 return isl_multi_aff_free(maff
);
6277 maff
= isl_multi_aff_align_divs(maff
);
6279 aff
= isl_multi_aff_peek_at(maff
, 0);
6280 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6282 return isl_multi_aff_free(maff
);
6283 space
= isl_multi_aff_get_space(maff
);
6284 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6285 space
= isl_space_extend_domain_with_range(space
,
6286 isl_multi_aff_get_space(maff
));
6287 maff
= isl_multi_aff_restore_space(maff
, space
);
6290 aff
= isl_multi_aff_peek_at(maff
, 0);
6291 *ls
= isl_aff_get_domain_local_space(aff
);
6293 return isl_multi_aff_free(maff
);
6296 for (i
= 0; i
< n
; ++i
) {
6297 aff
= isl_multi_aff_take_at(maff
, i
);
6298 aff
= isl_aff_lift(aff
);
6299 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
6306 #define TYPE isl_pw_multi_aff
6308 #include "check_type_range_templ.c"
6310 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6312 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6313 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6320 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6323 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6327 space
= isl_pw_multi_aff_get_space(pma
);
6328 space
= isl_space_drop_dims(space
, isl_dim_out
,
6329 pos
+ 1, n_out
- pos
- 1);
6330 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6332 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6333 for (i
= 0; i
< pma
->n
; ++i
) {
6335 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6336 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6342 /* This is an alternative name for the function above.
6344 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6345 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6347 return isl_pw_multi_aff_get_at(pma
, pos
);
6350 /* Return an isl_pw_multi_aff with the given "set" as domain and
6351 * an unnamed zero-dimensional range.
6353 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6354 __isl_take isl_set
*set
)
6359 space
= isl_set_get_space(set
);
6360 space
= isl_space_from_domain(space
);
6361 ma
= isl_multi_aff_zero(space
);
6362 return isl_pw_multi_aff_alloc(set
, ma
);
6365 /* Add an isl_pw_multi_aff with the given "set" as domain and
6366 * an unnamed zero-dimensional range to *user.
6368 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6371 isl_union_pw_multi_aff
**upma
= user
;
6372 isl_pw_multi_aff
*pma
;
6374 pma
= isl_pw_multi_aff_from_domain(set
);
6375 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6380 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6381 * an unnamed zero-dimensional range.
6383 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6384 __isl_take isl_union_set
*uset
)
6387 isl_union_pw_multi_aff
*upma
;
6392 space
= isl_union_set_get_space(uset
);
6393 upma
= isl_union_pw_multi_aff_empty(space
);
6395 if (isl_union_set_foreach_set(uset
,
6396 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6399 isl_union_set_free(uset
);
6402 isl_union_set_free(uset
);
6403 isl_union_pw_multi_aff_free(upma
);
6407 /* Local data for bin_entry and the callback "fn".
6409 struct isl_union_pw_multi_aff_bin_data
{
6410 isl_union_pw_multi_aff
*upma2
;
6411 isl_union_pw_multi_aff
*res
;
6412 isl_pw_multi_aff
*pma
;
6413 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6416 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6417 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6419 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6421 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6425 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6427 isl_pw_multi_aff_free(pma
);
6432 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6433 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6434 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6435 * as *entry. The callback should adjust data->res if desired.
6437 static __isl_give isl_union_pw_multi_aff
*bin_op(
6438 __isl_take isl_union_pw_multi_aff
*upma1
,
6439 __isl_take isl_union_pw_multi_aff
*upma2
,
6440 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6443 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6445 space
= isl_union_pw_multi_aff_get_space(upma2
);
6446 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6447 space
= isl_union_pw_multi_aff_get_space(upma1
);
6448 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6450 if (!upma1
|| !upma2
)
6454 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6455 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6456 &bin_entry
, &data
) < 0)
6459 isl_union_pw_multi_aff_free(upma1
);
6460 isl_union_pw_multi_aff_free(upma2
);
6463 isl_union_pw_multi_aff_free(upma1
);
6464 isl_union_pw_multi_aff_free(upma2
);
6465 isl_union_pw_multi_aff_free(data
.res
);
6469 /* Given two isl_pw_multi_affs A -> B and C -> D,
6470 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6472 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6473 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6477 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6478 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6479 isl_pw_multi_aff_get_space(pma2
));
6480 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6481 &isl_multi_aff_range_product
);
6484 /* Given two isl_pw_multi_affs A -> B and C -> D,
6485 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6487 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6488 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6492 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6493 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6494 isl_pw_multi_aff_get_space(pma2
));
6495 space
= isl_space_flatten_range(space
);
6496 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6497 &isl_multi_aff_flat_range_product
);
6500 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6501 * to compute some form of range product and add the result to data->res.
6503 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6504 __isl_give isl_pw_multi_aff
*(*range_product
)(
6505 __isl_take isl_pw_multi_aff
*pma1
,
6506 __isl_take isl_pw_multi_aff
*pma2
),
6509 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6511 isl_space
*space1
, *space2
;
6513 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6514 space2
= isl_pw_multi_aff_peek_space(pma2
);
6515 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6516 space2
, isl_dim_in
);
6517 if (match
< 0 || !match
) {
6518 isl_pw_multi_aff_free(pma2
);
6519 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6522 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6524 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6529 /* If data->pma and "pma2" have the same domain space, then compute
6530 * their flat range product and add the result to data->res.
6532 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6535 return gen_range_product_entry(pma2
,
6536 &isl_pw_multi_aff_flat_range_product
, user
);
6539 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6540 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6542 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6543 __isl_take isl_union_pw_multi_aff
*upma1
,
6544 __isl_take isl_union_pw_multi_aff
*upma2
)
6546 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6549 /* If data->pma and "pma2" have the same domain space, then compute
6550 * their range product and add the result to data->res.
6552 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6555 return gen_range_product_entry(pma2
,
6556 &isl_pw_multi_aff_range_product
, user
);
6559 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6560 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6562 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6563 __isl_take isl_union_pw_multi_aff
*upma1
,
6564 __isl_take isl_union_pw_multi_aff
*upma2
)
6566 return bin_op(upma1
, upma2
, &range_product_entry
);
6569 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6570 * The parameters are assumed to have been aligned.
6572 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6573 * except that it works on two different isl_pw_* types.
6575 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6576 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6577 __isl_take isl_pw_aff
*pa
)
6580 isl_pw_multi_aff
*res
= NULL
;
6585 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6586 pa
->dim
, isl_dim_in
))
6587 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6588 "domains don't match", goto error
);
6589 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6593 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6595 for (i
= 0; i
< pma
->n
; ++i
) {
6596 for (j
= 0; j
< pa
->n
; ++j
) {
6598 isl_multi_aff
*res_ij
;
6601 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6602 isl_set_copy(pa
->p
[j
].set
));
6603 empty
= isl_set_plain_is_empty(common
);
6604 if (empty
< 0 || empty
) {
6605 isl_set_free(common
);
6611 res_ij
= isl_multi_aff_set_aff(
6612 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6613 isl_aff_copy(pa
->p
[j
].aff
));
6614 res_ij
= isl_multi_aff_gist(res_ij
,
6615 isl_set_copy(common
));
6617 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6621 isl_pw_multi_aff_free(pma
);
6622 isl_pw_aff_free(pa
);
6625 isl_pw_multi_aff_free(pma
);
6626 isl_pw_aff_free(pa
);
6627 return isl_pw_multi_aff_free(res
);
6630 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6632 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6633 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6634 __isl_take isl_pw_aff
*pa
)
6636 isl_bool equal_params
;
6640 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6641 if (equal_params
< 0)
6644 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6645 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6646 isl_pw_aff_check_named_params(pa
) < 0)
6648 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6649 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6650 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6652 isl_pw_multi_aff_free(pma
);
6653 isl_pw_aff_free(pa
);
6657 /* Do the parameters of "pa" match those of "space"?
6659 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6660 __isl_keep isl_space
*space
)
6662 isl_space
*pa_space
;
6666 return isl_bool_error
;
6668 pa_space
= isl_pw_aff_get_space(pa
);
6670 match
= isl_space_has_equal_params(space
, pa_space
);
6672 isl_space_free(pa_space
);
6676 /* Check that the domain space of "pa" matches "space".
6678 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6679 __isl_keep isl_space
*space
)
6681 isl_space
*pa_space
;
6685 return isl_stat_error
;
6687 pa_space
= isl_pw_aff_get_space(pa
);
6689 match
= isl_space_has_equal_params(space
, pa_space
);
6693 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6694 "parameters don't match", goto error
);
6695 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6696 pa_space
, isl_dim_in
);
6700 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6701 "domains don't match", goto error
);
6702 isl_space_free(pa_space
);
6705 isl_space_free(pa_space
);
6706 return isl_stat_error
;
6714 #include <isl_multi_explicit_domain.c>
6715 #include <isl_multi_pw_aff_explicit_domain.c>
6716 #include <isl_multi_templ.c>
6717 #include <isl_multi_un_op_templ.c>
6718 #include <isl_multi_bin_val_templ.c>
6719 #include <isl_multi_add_constant_templ.c>
6720 #include <isl_multi_align_set.c>
6721 #include <isl_multi_apply_set_explicit_domain_templ.c>
6722 #include <isl_multi_arith_templ.c>
6723 #include <isl_multi_bind_templ.c>
6724 #include <isl_multi_bind_domain_templ.c>
6725 #include <isl_multi_coalesce.c>
6726 #include <isl_multi_domain_templ.c>
6727 #include <isl_multi_dim_id_templ.c>
6728 #include <isl_multi_dims.c>
6729 #include <isl_multi_from_base_templ.c>
6730 #include <isl_multi_check_domain_templ.c>
6731 #include <isl_multi_gist.c>
6732 #include <isl_multi_hash.c>
6733 #include <isl_multi_identity_templ.c>
6734 #include <isl_multi_insert_domain_templ.c>
6735 #include <isl_multi_intersect.c>
6736 #include <isl_multi_min_max_templ.c>
6737 #include <isl_multi_move_dims_templ.c>
6738 #include <isl_multi_nan_templ.c>
6739 #include <isl_multi_param_templ.c>
6740 #include <isl_multi_product_templ.c>
6741 #include <isl_multi_splice_templ.c>
6742 #include <isl_multi_tuple_id_templ.c>
6743 #include <isl_multi_union_add_templ.c>
6744 #include <isl_multi_zero_templ.c>
6745 #include <isl_multi_unbind_params_templ.c>
6747 /* Is every element of "mpa" defined over a single universe domain?
6749 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6751 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6754 /* Given that every element of "mpa" is defined over a single universe domain,
6755 * return the corresponding base expressions.
6757 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6758 __isl_take isl_multi_pw_aff
*mpa
)
6764 n
= isl_multi_pw_aff_size(mpa
);
6766 mpa
= isl_multi_pw_aff_free(mpa
);
6767 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6768 for (i
= 0; i
< n
; ++i
) {
6771 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6772 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6774 isl_multi_pw_aff_free(mpa
);
6778 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6779 * with this explicit domain.
6781 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6782 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6786 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6789 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6790 map
= isl_map_intersect_domain(map
, dom
);
6795 /* Are all elements of "mpa" piecewise constants?
6797 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6799 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6802 /* Does "mpa" have a non-trivial explicit domain?
6804 * The explicit domain, if present, is trivial if it represents
6805 * an (obviously) universe set.
6807 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6808 __isl_keep isl_multi_pw_aff
*mpa
)
6811 return isl_bool_error
;
6812 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6813 return isl_bool_false
;
6814 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6820 #include "isl_opt_mpa_templ.c"
6822 /* Compute the minima of the set dimensions as a function of the
6823 * parameters, but independently of the other set dimensions.
6825 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6827 return set_opt_mpa(set
, &isl_set_dim_min
);
6830 /* Compute the maxima of the set dimensions as a function of the
6831 * parameters, but independently of the other set dimensions.
6833 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6835 return set_opt_mpa(set
, &isl_set_dim_max
);
6841 #include "isl_opt_mpa_templ.c"
6843 /* Compute the minima of the output dimensions as a function of the
6844 * parameters and input dimensions, but independently of
6845 * the other output dimensions.
6847 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6849 return map_opt_mpa(map
, &isl_map_dim_min
);
6852 /* Compute the maxima of the output dimensions as a function of the
6853 * parameters and input dimensions, but independently of
6854 * the other output dimensions.
6856 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6858 return map_opt_mpa(map
, &isl_map_dim_max
);
6862 #define TYPE isl_pw_multi_aff
6863 #include "isl_type_check_match_range_multi_val.c"
6865 /* Apply "fn" to the base expressions of "pma" and "mv".
6867 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6868 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6869 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6870 __isl_take isl_multi_val
*mv
))
6875 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6878 n
= isl_pw_multi_aff_n_piece(pma
);
6882 for (i
= 0; i
< n
; ++i
) {
6885 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6886 ma
= fn(ma
, isl_multi_val_copy(mv
));
6887 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6890 isl_multi_val_free(mv
);
6893 isl_multi_val_free(mv
);
6894 isl_pw_multi_aff_free(pma
);
6898 /* Scale the elements of "pma" by the corresponding elements of "mv".
6900 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6901 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6903 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6904 &isl_multi_aff_scale_multi_val
);
6907 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6909 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
6910 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6912 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6913 &isl_multi_aff_scale_down_multi_val
);
6916 /* This function is called for each entry of an isl_union_pw_multi_aff.
6917 * If the space of the entry matches that of data->mv,
6918 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6919 * Otherwise, return an empty isl_pw_multi_aff.
6921 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6922 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6925 isl_multi_val
*mv
= user
;
6927 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
6929 return isl_pw_multi_aff_free(pma
);
6931 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6932 isl_pw_multi_aff_free(pma
);
6933 return isl_pw_multi_aff_empty(space
);
6936 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6939 /* Scale the elements of "upma" by the corresponding elements of "mv",
6940 * for those entries that match the space of "mv".
6942 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6943 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6945 struct isl_union_pw_multi_aff_transform_control control
= {
6946 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6950 upma
= isl_union_pw_multi_aff_align_params(upma
,
6951 isl_multi_val_get_space(mv
));
6952 mv
= isl_multi_val_align_params(mv
,
6953 isl_union_pw_multi_aff_get_space(upma
));
6957 return isl_union_pw_multi_aff_transform(upma
, &control
);
6959 isl_multi_val_free(mv
);
6962 isl_multi_val_free(mv
);
6963 isl_union_pw_multi_aff_free(upma
);
6967 /* Construct and return a piecewise multi affine expression
6968 * in the given space with value zero in each of the output dimensions and
6969 * a universe domain.
6971 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6973 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6976 /* Construct and return a piecewise multi affine expression
6977 * that is equal to the given piecewise affine expression.
6979 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6980 __isl_take isl_pw_aff
*pa
)
6984 isl_pw_multi_aff
*pma
;
6989 space
= isl_pw_aff_get_space(pa
);
6990 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6992 for (i
= 0; i
< pa
->n
; ++i
) {
6996 set
= isl_set_copy(pa
->p
[i
].set
);
6997 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6998 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
7001 isl_pw_aff_free(pa
);
7005 /* Construct and return a piecewise multi affine expression
7006 * that is equal to the given multi piecewise affine expression
7007 * on the shared domain of the piecewise affine expressions,
7008 * in the special case of a 0D multi piecewise affine expression.
7010 * Create a piecewise multi affine expression with the explicit domain of
7011 * the 0D multi piecewise affine expression as domain.
7013 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
7014 __isl_take isl_multi_pw_aff
*mpa
)
7020 space
= isl_multi_pw_aff_get_space(mpa
);
7021 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
7022 isl_multi_pw_aff_free(mpa
);
7024 ma
= isl_multi_aff_zero(space
);
7025 return isl_pw_multi_aff_alloc(dom
, ma
);
7028 /* Construct and return a piecewise multi affine expression
7029 * that is equal to the given multi piecewise affine expression
7030 * on the shared domain of the piecewise affine expressions.
7032 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
7033 __isl_take isl_multi_pw_aff
*mpa
)
7038 isl_pw_multi_aff
*pma
;
7044 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
7046 space
= isl_multi_pw_aff_get_space(mpa
);
7047 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
7048 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7050 for (i
= 1; i
< mpa
->n
; ++i
) {
7051 isl_pw_multi_aff
*pma_i
;
7053 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7054 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
7055 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
7058 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
7060 isl_multi_pw_aff_free(mpa
);
7064 /* Convenience function that constructs an isl_multi_pw_aff
7065 * directly from an isl_aff.
7067 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
7069 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
7072 /* Construct and return a multi piecewise affine expression
7073 * that is equal to the given multi affine expression.
7075 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
7076 __isl_take isl_multi_aff
*ma
)
7080 isl_multi_pw_aff
*mpa
;
7082 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
7084 ma
= isl_multi_aff_free(ma
);
7088 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
7090 for (i
= 0; i
< n
; ++i
) {
7093 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
7094 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7097 isl_multi_aff_free(ma
);
7101 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7102 * but is considered as a function on an isl_multi_aff when exported.
7104 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
7105 __isl_take isl_multi_aff
*ma
)
7107 return isl_multi_pw_aff_from_multi_aff(ma
);
7110 /* Construct and return a multi piecewise affine expression
7111 * that is equal to the given piecewise multi affine expression.
7113 * If the resulting multi piecewise affine expression has
7114 * an explicit domain, then assign it the domain of the input.
7115 * In other cases, the domain is stored in the individual elements.
7117 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
7118 __isl_take isl_pw_multi_aff
*pma
)
7123 isl_multi_pw_aff
*mpa
;
7125 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7127 pma
= isl_pw_multi_aff_free(pma
);
7128 space
= isl_pw_multi_aff_get_space(pma
);
7129 mpa
= isl_multi_pw_aff_alloc(space
);
7131 for (i
= 0; i
< n
; ++i
) {
7134 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
7135 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7137 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7140 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
7141 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
7144 isl_pw_multi_aff_free(pma
);
7148 /* This function performs the same operation as
7149 * isl_multi_pw_aff_from_pw_multi_aff,
7150 * but is considered as a function on an isl_pw_multi_aff when exported.
7152 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
7153 __isl_take isl_pw_multi_aff
*pma
)
7155 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7158 /* Do "pa1" and "pa2" represent the same function?
7160 * We first check if they are obviously equal.
7161 * If not, we convert them to maps and check if those are equal.
7163 * If "pa1" or "pa2" contain any NaNs, then they are considered
7164 * not to be the same. A NaN is not equal to anything, not even
7167 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7168 __isl_keep isl_pw_aff
*pa2
)
7172 isl_map
*map1
, *map2
;
7175 return isl_bool_error
;
7177 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7178 if (equal
< 0 || equal
)
7180 has_nan
= either_involves_nan(pa1
, pa2
);
7182 return isl_bool_error
;
7184 return isl_bool_false
;
7186 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7187 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7188 equal
= isl_map_is_equal(map1
, map2
);
7195 /* Do "mpa1" and "mpa2" represent the same function?
7197 * Note that we cannot convert the entire isl_multi_pw_aff
7198 * to a map because the domains of the piecewise affine expressions
7199 * may not be the same.
7201 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7202 __isl_keep isl_multi_pw_aff
*mpa2
)
7205 isl_bool equal
, equal_params
;
7208 return isl_bool_error
;
7210 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7211 if (equal_params
< 0)
7212 return isl_bool_error
;
7213 if (!equal_params
) {
7214 if (!isl_space_has_named_params(mpa1
->space
))
7215 return isl_bool_false
;
7216 if (!isl_space_has_named_params(mpa2
->space
))
7217 return isl_bool_false
;
7218 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7219 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7220 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7221 isl_multi_pw_aff_get_space(mpa2
));
7222 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7223 isl_multi_pw_aff_get_space(mpa1
));
7224 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7225 isl_multi_pw_aff_free(mpa1
);
7226 isl_multi_pw_aff_free(mpa2
);
7230 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7231 if (equal
< 0 || !equal
)
7234 for (i
= 0; i
< mpa1
->n
; ++i
) {
7235 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7236 if (equal
< 0 || !equal
)
7240 return isl_bool_true
;
7243 /* Do "pma1" and "pma2" represent the same function?
7245 * First check if they are obviously equal.
7246 * If not, then convert them to maps and check if those are equal.
7248 * If "pa1" or "pa2" contain any NaNs, then they are considered
7249 * not to be the same. A NaN is not equal to anything, not even
7252 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7253 __isl_keep isl_pw_multi_aff
*pma2
)
7257 isl_map
*map1
, *map2
;
7260 return isl_bool_error
;
7262 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7263 if (equal
< 0 || equal
)
7265 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7266 if (has_nan
>= 0 && !has_nan
)
7267 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7268 if (has_nan
< 0 || has_nan
)
7269 return isl_bool_not(has_nan
);
7271 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7272 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7273 equal
= isl_map_is_equal(map1
, map2
);
7281 #define BASE multi_aff
7283 #include "isl_multi_pw_aff_pullback_templ.c"
7286 #define BASE pw_multi_aff
7288 #include "isl_multi_pw_aff_pullback_templ.c"
7290 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7291 * with the domain of "aff". The domain of the result is the same
7293 * "mpa" and "aff" are assumed to have been aligned.
7295 * We first extract the parametric constant from "aff", defined
7296 * over the correct domain.
7297 * Then we add the appropriate combinations of the members of "mpa".
7298 * Finally, we add the integer divisions through recursive calls.
7300 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7301 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7304 isl_size n_in
, n_div
, n_mpa_in
;
7310 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7311 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7312 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7313 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7316 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7317 tmp
= isl_aff_copy(aff
);
7318 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7319 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7320 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7321 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7322 pa
= isl_pw_aff_from_aff(tmp
);
7324 for (i
= 0; i
< n_in
; ++i
) {
7327 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7329 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7330 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7331 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7332 pa
= isl_pw_aff_add(pa
, pa_i
);
7335 for (i
= 0; i
< n_div
; ++i
) {
7339 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7341 div
= isl_aff_get_div(aff
, i
);
7342 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7343 isl_multi_pw_aff_copy(mpa
), div
);
7344 pa_i
= isl_pw_aff_floor(pa_i
);
7345 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7346 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7347 pa
= isl_pw_aff_add(pa
, pa_i
);
7350 isl_multi_pw_aff_free(mpa
);
7355 isl_multi_pw_aff_free(mpa
);
7360 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7361 * with the domain of "aff". The domain of the result is the same
7364 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7365 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7367 isl_bool equal_params
;
7371 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7372 if (equal_params
< 0)
7375 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7377 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7378 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7380 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7383 isl_multi_pw_aff_free(mpa
);
7387 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7388 * with the domain of "pa". The domain of the result is the same
7390 * "mpa" and "pa" are assumed to have been aligned.
7392 * We consider each piece in turn. Note that the domains of the
7393 * pieces are assumed to be disjoint and they remain disjoint
7394 * after taking the preimage (over the same function).
7396 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7397 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7406 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7407 isl_pw_aff_get_space(pa
));
7408 res
= isl_pw_aff_empty(space
);
7410 for (i
= 0; i
< pa
->n
; ++i
) {
7414 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7415 isl_multi_pw_aff_copy(mpa
),
7416 isl_aff_copy(pa
->p
[i
].aff
));
7417 domain
= isl_set_copy(pa
->p
[i
].set
);
7418 domain
= isl_set_preimage_multi_pw_aff(domain
,
7419 isl_multi_pw_aff_copy(mpa
));
7420 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7421 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7424 isl_pw_aff_free(pa
);
7425 isl_multi_pw_aff_free(mpa
);
7428 isl_pw_aff_free(pa
);
7429 isl_multi_pw_aff_free(mpa
);
7433 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7434 * with the domain of "pa". The domain of the result is the same
7437 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7438 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7440 isl_bool equal_params
;
7444 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7445 if (equal_params
< 0)
7448 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7450 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7451 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7453 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7455 isl_pw_aff_free(pa
);
7456 isl_multi_pw_aff_free(mpa
);
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
);
7472 #define BASE multi_pw_aff
7474 #include "isl_multi_pw_aff_pullback_templ.c"
7476 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7477 * of "mpa1" and "mpa2" live in the same space, construct map space
7478 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7479 * with this map space as extract argument.
7481 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7482 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7483 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7484 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7487 isl_space
*space1
, *space2
;
7490 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7491 isl_multi_pw_aff_get_space(mpa2
));
7492 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7493 isl_multi_pw_aff_get_space(mpa1
));
7496 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7497 mpa2
->space
, isl_dim_out
);
7501 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7502 "range spaces don't match", goto error
);
7503 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7504 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7505 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7507 res
= order(mpa1
, mpa2
, space1
);
7508 isl_multi_pw_aff_free(mpa1
);
7509 isl_multi_pw_aff_free(mpa2
);
7512 isl_multi_pw_aff_free(mpa1
);
7513 isl_multi_pw_aff_free(mpa2
);
7517 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7518 * where the function values are equal. "space" is the space of the result.
7519 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7521 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7522 * in the sequences are equal.
7524 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7525 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7526 __isl_take isl_space
*space
)
7532 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7534 space
= isl_space_free(space
);
7535 res
= isl_map_universe(space
);
7537 for (i
= 0; i
< n
; ++i
) {
7538 isl_pw_aff
*pa1
, *pa2
;
7541 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7542 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7543 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7544 res
= isl_map_intersect(res
, map
);
7550 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7551 * where the function values are equal.
7553 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7554 __isl_take isl_multi_pw_aff
*mpa2
)
7556 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7557 &isl_multi_pw_aff_eq_map_on_space
);
7560 /* Intersect "map" with the result of applying "order"
7561 * on two copies of "mpa".
7563 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7564 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7565 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7566 __isl_take isl_multi_pw_aff
*mpa2
))
7568 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7571 /* Return the subset of "map" where the domain and the range
7572 * have equal "mpa" values.
7574 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7575 __isl_take isl_multi_pw_aff
*mpa
)
7577 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7578 &isl_multi_pw_aff_eq_map
);
7581 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7582 * where the function values of "mpa1" lexicographically satisfies
7583 * "strict_base"/"base" compared to that of "mpa2".
7584 * "space" is the space of the result.
7585 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7587 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7588 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7589 * when compared to the i-th element of "mpa2" while all previous elements are
7591 * In particular, if i corresponds to the final elements
7592 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7593 * for other values of i.
7594 * If "base" is a strict order, then "base" and "strict_base" are the same.
7596 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7597 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7598 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7599 __isl_take isl_pw_aff
*pa2
),
7600 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7601 __isl_take isl_pw_aff
*pa2
),
7602 __isl_take isl_space
*space
)
7606 isl_map
*res
, *rest
;
7608 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7610 space
= isl_space_free(space
);
7611 res
= isl_map_empty(isl_space_copy(space
));
7612 rest
= isl_map_universe(space
);
7614 for (i
= 0; i
< n
; ++i
) {
7616 isl_pw_aff
*pa1
, *pa2
;
7621 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7622 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7623 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7624 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7625 res
= isl_map_union(res
, map
);
7630 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7631 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7632 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7633 rest
= isl_map_intersect(rest
, map
);
7643 #define STRICT_ORDER lt
7644 #include "isl_aff_lex_templ.c"
7649 #define STRICT_ORDER lt
7650 #include "isl_aff_lex_templ.c"
7655 #define STRICT_ORDER gt
7656 #include "isl_aff_lex_templ.c"
7661 #define STRICT_ORDER gt
7662 #include "isl_aff_lex_templ.c"
7664 /* Compare two isl_affs.
7666 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7667 * than "aff2" and 0 if they are equal.
7669 * The order is fairly arbitrary. We do consider expressions that only involve
7670 * earlier dimensions as "smaller".
7672 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7685 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7689 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7690 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7692 return last1
- last2
;
7694 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7697 /* Compare two isl_pw_affs.
7699 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7700 * than "pa2" and 0 if they are equal.
7702 * The order is fairly arbitrary. We do consider expressions that only involve
7703 * earlier dimensions as "smaller".
7705 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7706 __isl_keep isl_pw_aff
*pa2
)
7719 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7723 if (pa1
->n
!= pa2
->n
)
7724 return pa1
->n
- pa2
->n
;
7726 for (i
= 0; i
< pa1
->n
; ++i
) {
7727 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7730 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7738 /* Return a piecewise affine expression that is equal to "v" on "domain".
7740 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7741 __isl_take isl_val
*v
)
7744 isl_local_space
*ls
;
7747 space
= isl_set_get_space(domain
);
7748 ls
= isl_local_space_from_space(space
);
7749 aff
= isl_aff_val_on_domain(ls
, v
);
7751 return isl_pw_aff_alloc(domain
, aff
);
7754 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7755 * but is considered as a function on an isl_set when exported.
7757 __isl_give isl_pw_aff
*isl_set_pw_aff_on_domain_val(__isl_take isl_set
*domain
,
7758 __isl_take isl_val
*v
)
7760 return isl_pw_aff_val_on_domain(domain
, v
);
7763 /* Return a piecewise affine expression that is equal to the parameter
7764 * with identifier "id" on "domain".
7766 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7767 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7772 space
= isl_set_get_space(domain
);
7773 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7774 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7775 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7777 return isl_pw_aff_alloc(domain
, aff
);
7780 /* This function performs the same operation as
7781 * isl_pw_aff_param_on_domain_id,
7782 * but is considered as a function on an isl_set when exported.
7784 __isl_give isl_pw_aff
*isl_set_param_pw_aff_on_domain_id(
7785 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7787 return isl_pw_aff_param_on_domain_id(domain
, id
);
7790 /* Return a multi affine expression that is equal to "mv" on domain
7793 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7794 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7799 isl_local_space
*ls
;
7802 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7803 if (!space
|| n
< 0)
7806 space2
= isl_multi_val_get_space(mv
);
7807 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7808 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7809 space
= isl_space_map_from_domain_and_range(space
, space2
);
7810 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7811 ls
= isl_local_space_from_space(isl_space_domain(space
));
7812 for (i
= 0; i
< n
; ++i
) {
7816 v
= isl_multi_val_get_val(mv
, i
);
7817 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7818 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7820 isl_local_space_free(ls
);
7822 isl_multi_val_free(mv
);
7825 isl_space_free(space
);
7826 isl_multi_val_free(mv
);
7830 /* This is an alternative name for the function above.
7832 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7833 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7835 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7838 /* This function performs the same operation as
7839 * isl_multi_aff_multi_val_on_domain_space,
7840 * but is considered as a function on an isl_space when exported.
7842 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7843 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7845 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7848 /* Return a piecewise multi-affine expression
7849 * that is equal to "mv" on "domain".
7851 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7852 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7857 space
= isl_set_get_space(domain
);
7858 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7860 return isl_pw_multi_aff_alloc(domain
, ma
);
7863 /* This function performs the same operation as
7864 * isl_pw_multi_aff_multi_val_on_domain,
7865 * but is considered as a function on an isl_set when exported.
7867 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7868 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7870 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7873 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7874 * mv is the value that should be attained on each domain set
7875 * res collects the results
7877 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7879 isl_union_pw_multi_aff
*res
;
7882 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7883 * and add it to data->res.
7885 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7888 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7889 isl_pw_multi_aff
*pma
;
7892 mv
= isl_multi_val_copy(data
->mv
);
7893 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7894 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7896 return data
->res
? isl_stat_ok
: isl_stat_error
;
7899 /* Return a union piecewise multi-affine expression
7900 * that is equal to "mv" on "domain".
7902 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7903 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7905 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7908 space
= isl_union_set_get_space(domain
);
7909 data
.res
= isl_union_pw_multi_aff_empty(space
);
7911 if (isl_union_set_foreach_set(domain
,
7912 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7913 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7914 isl_union_set_free(domain
);
7915 isl_multi_val_free(mv
);
7919 /* Compute the pullback of data->pma by the function represented by "pma2",
7920 * provided the spaces match, and add the results to data->res.
7922 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7924 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7926 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7927 pma2
->dim
, isl_dim_out
)) {
7928 isl_pw_multi_aff_free(pma2
);
7932 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7933 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7935 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7937 return isl_stat_error
;
7942 /* Compute the pullback of "upma1" by the function represented by "upma2".
7944 __isl_give isl_union_pw_multi_aff
*
7945 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7946 __isl_take isl_union_pw_multi_aff
*upma1
,
7947 __isl_take isl_union_pw_multi_aff
*upma2
)
7949 return bin_op(upma1
, upma2
, &pullback_entry
);
7952 /* Apply "upma2" to "upma1".
7954 * That is, compute the pullback of "upma2" by "upma1".
7956 __isl_give isl_union_pw_multi_aff
*
7957 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7958 __isl_take isl_union_pw_multi_aff
*upma1
,
7959 __isl_take isl_union_pw_multi_aff
*upma2
)
7961 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
7965 #define TYPE isl_pw_multi_aff
7967 #include "isl_copy_tuple_id_templ.c"
7969 /* Given a function "pma1" of the form A[B -> C] -> D and
7970 * a function "pma2" of the form E -> B,
7971 * replace the domain of the wrapped relation inside the domain of "pma1"
7972 * by the preimage with respect to "pma2".
7973 * In other words, plug in "pma2" in this nested domain.
7974 * The result is of the form A[E -> C] -> D.
7976 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7977 * plug that into "pma1".
7979 __isl_give isl_pw_multi_aff
*
7980 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7981 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
7983 isl_space
*pma1_space
, *pma2_space
;
7985 isl_pw_multi_aff
*id
;
7987 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
7988 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
7990 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
7992 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
7993 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
7996 space
= isl_space_domain(isl_space_copy(pma1_space
));
7997 space
= isl_space_range(isl_space_unwrap(space
));
7998 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
7999 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8001 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8002 pma1_space
, isl_dim_in
);
8003 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8004 pma1_space
, isl_dim_in
);
8006 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8008 isl_pw_multi_aff_free(pma1
);
8009 isl_pw_multi_aff_free(pma2
);
8013 /* If data->pma and "pma2" are such that
8014 * data->pma is of the form A[B -> C] -> D and
8015 * "pma2" is of the form E -> B,
8016 * then replace the domain of the wrapped relation
8017 * inside the domain of data->pma by the preimage with respect to "pma2" and
8018 * add the result to data->res.
8020 static isl_stat
preimage_domain_wrapped_domain_entry(
8021 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8023 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8024 isl_space
*pma1_space
, *pma2_space
;
8027 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8028 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8030 match
= isl_space_domain_is_wrapping(pma1_space
);
8031 if (match
>= 0 && match
)
8032 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8033 isl_dim_in
, pma2_space
, isl_dim_out
);
8034 if (match
< 0 || !match
) {
8035 isl_pw_multi_aff_free(pma2
);
8036 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8039 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8040 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8042 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8044 return isl_stat_non_null(data
->res
);
8047 /* For each pair of functions A[B -> C] -> D in "upma1" and
8048 * E -> B in "upma2",
8049 * replace the domain of the wrapped relation inside the domain of the first
8050 * by the preimage with respect to the second and collect the results.
8051 * In other words, plug in the second function in this nested domain.
8052 * The results are of the form A[E -> C] -> D.
8054 __isl_give isl_union_pw_multi_aff
*
8055 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8056 __isl_take isl_union_pw_multi_aff
*upma1
,
8057 __isl_take isl_union_pw_multi_aff
*upma2
)
8059 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8062 /* Check that the domain space of "upa" matches "space".
8064 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8065 * can in principle never fail since the space "space" is that
8066 * of the isl_multi_union_pw_aff and is a set space such that
8067 * there is no domain space to match.
8069 * We check the parameters and double-check that "space" is
8070 * indeed that of a set.
8072 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8073 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8075 isl_space
*upa_space
;
8079 return isl_stat_error
;
8081 match
= isl_space_is_set(space
);
8083 return isl_stat_error
;
8085 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8086 "expecting set space", return isl_stat_error
);
8088 upa_space
= isl_union_pw_aff_get_space(upa
);
8089 match
= isl_space_has_equal_params(space
, upa_space
);
8093 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8094 "parameters don't match", goto error
);
8096 isl_space_free(upa_space
);
8099 isl_space_free(upa_space
);
8100 return isl_stat_error
;
8103 /* Do the parameters of "upa" match those of "space"?
8105 static isl_bool
isl_union_pw_aff_matching_params(
8106 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8108 isl_space
*upa_space
;
8112 return isl_bool_error
;
8114 upa_space
= isl_union_pw_aff_get_space(upa
);
8116 match
= isl_space_has_equal_params(space
, upa_space
);
8118 isl_space_free(upa_space
);
8122 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8123 * space represents the new parameters.
8124 * res collects the results.
8126 struct isl_union_pw_aff_reset_params_data
{
8128 isl_union_pw_aff
*res
;
8131 /* Replace the parameters of "pa" by data->space and
8132 * add the result to data->res.
8134 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8136 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8139 space
= isl_pw_aff_get_space(pa
);
8140 space
= isl_space_replace_params(space
, data
->space
);
8141 pa
= isl_pw_aff_reset_space(pa
, space
);
8142 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8144 return data
->res
? isl_stat_ok
: isl_stat_error
;
8147 /* Replace the domain space of "upa" by "space".
8148 * Since a union expression does not have a (single) domain space,
8149 * "space" is necessarily a parameter space.
8151 * Since the order and the names of the parameters determine
8152 * the hash value, we need to create a new hash table.
8154 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8155 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8157 struct isl_union_pw_aff_reset_params_data data
= { space
};
8160 match
= isl_union_pw_aff_matching_params(upa
, space
);
8162 upa
= isl_union_pw_aff_free(upa
);
8164 isl_space_free(space
);
8168 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8169 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8170 data
.res
= isl_union_pw_aff_free(data
.res
);
8172 isl_union_pw_aff_free(upa
);
8173 isl_space_free(space
);
8177 /* Return the floor of "pa".
8179 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8181 return isl_pw_aff_floor(pa
);
8184 /* Given f, return floor(f).
8186 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8187 __isl_take isl_union_pw_aff
*upa
)
8189 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8194 * upa mod m = upa - m * floor(upa/m)
8196 * with m an integer value.
8198 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8199 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8201 isl_union_pw_aff
*res
;
8206 if (!isl_val_is_int(m
))
8207 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8208 "expecting integer modulo", goto error
);
8209 if (!isl_val_is_pos(m
))
8210 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8211 "expecting positive modulo", goto error
);
8213 res
= isl_union_pw_aff_copy(upa
);
8214 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8215 upa
= isl_union_pw_aff_floor(upa
);
8216 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8217 res
= isl_union_pw_aff_sub(res
, upa
);
8222 isl_union_pw_aff_free(upa
);
8226 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8227 * pos is the output position that needs to be extracted.
8228 * res collects the results.
8230 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8232 isl_union_pw_aff
*res
;
8235 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8236 * (assuming it has such a dimension) and add it to data->res.
8238 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8240 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8244 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8246 return isl_stat_error
;
8247 if (data
->pos
>= n_out
) {
8248 isl_pw_multi_aff_free(pma
);
8252 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8253 isl_pw_multi_aff_free(pma
);
8255 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8257 return data
->res
? isl_stat_ok
: isl_stat_error
;
8260 /* Extract an isl_union_pw_aff corresponding to
8261 * output dimension "pos" of "upma".
8263 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8264 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8266 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8273 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8274 "cannot extract at negative position", return NULL
);
8276 space
= isl_union_pw_multi_aff_get_space(upma
);
8277 data
.res
= isl_union_pw_aff_empty(space
);
8279 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8280 &get_union_pw_aff
, &data
) < 0)
8281 data
.res
= isl_union_pw_aff_free(data
.res
);
8286 /* Return a union piecewise affine expression
8287 * that is equal to "aff" on "domain".
8289 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8290 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8294 pa
= isl_pw_aff_from_aff(aff
);
8295 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8298 /* Return a union piecewise affine expression
8299 * that is equal to the parameter identified by "id" on "domain".
8301 * Make sure the parameter appears in the space passed to
8302 * isl_aff_param_on_domain_space_id.
8304 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8305 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8310 space
= isl_union_set_get_space(domain
);
8311 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8312 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8313 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8316 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8317 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8319 * "res" collects the results.
8321 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8323 isl_union_pw_aff
*res
;
8326 /* Construct a piecewise affine expression that is equal to data->pa
8327 * on "domain" and add the result to data->res.
8329 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8331 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8335 pa
= isl_pw_aff_copy(data
->pa
);
8336 dim
= isl_set_dim(domain
, isl_dim_set
);
8338 pa
= isl_pw_aff_free(pa
);
8339 pa
= isl_pw_aff_from_range(pa
);
8340 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8341 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8342 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8343 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8345 return data
->res
? isl_stat_ok
: isl_stat_error
;
8348 /* Return a union piecewise affine expression
8349 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8350 * have been aligned.
8352 * Construct an isl_pw_aff on each of the sets in "domain" and
8353 * collect the results.
8355 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8356 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8358 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8361 space
= isl_union_set_get_space(domain
);
8362 data
.res
= isl_union_pw_aff_empty(space
);
8364 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8365 data
.res
= isl_union_pw_aff_free(data
.res
);
8366 isl_union_set_free(domain
);
8367 isl_pw_aff_free(pa
);
8371 /* Return a union piecewise affine expression
8372 * that is equal to "pa" on "domain".
8374 * Check that "pa" is a parametric expression,
8375 * align the parameters if needed and call
8376 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8378 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8379 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8382 isl_bool equal_params
;
8383 isl_space
*domain_space
, *pa_space
;
8385 pa_space
= isl_pw_aff_peek_space(pa
);
8386 is_set
= isl_space_is_set(pa_space
);
8390 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8391 "expecting parametric expression", goto error
);
8393 domain_space
= isl_union_set_get_space(domain
);
8394 pa_space
= isl_pw_aff_get_space(pa
);
8395 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8396 if (equal_params
>= 0 && !equal_params
) {
8399 space
= isl_space_align_params(domain_space
, pa_space
);
8400 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8401 domain
= isl_union_set_align_params(domain
, space
);
8403 isl_space_free(domain_space
);
8404 isl_space_free(pa_space
);
8407 if (equal_params
< 0)
8409 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8411 isl_union_set_free(domain
);
8412 isl_pw_aff_free(pa
);
8416 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8417 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8418 * "res" collects the results.
8420 struct isl_union_pw_aff_val_on_domain_data
{
8422 isl_union_pw_aff
*res
;
8425 /* Construct a piecewise affine expression that is equal to data->v
8426 * on "domain" and add the result to data->res.
8428 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8430 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8434 v
= isl_val_copy(data
->v
);
8435 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8436 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8438 return data
->res
? isl_stat_ok
: isl_stat_error
;
8441 /* Return a union piecewise affine expression
8442 * that is equal to "v" on "domain".
8444 * Construct an isl_pw_aff on each of the sets in "domain" and
8445 * collect the results.
8447 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8448 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8450 struct isl_union_pw_aff_val_on_domain_data data
;
8453 space
= isl_union_set_get_space(domain
);
8454 data
.res
= isl_union_pw_aff_empty(space
);
8456 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8457 data
.res
= isl_union_pw_aff_free(data
.res
);
8458 isl_union_set_free(domain
);
8463 /* Construct a piecewise multi affine expression
8464 * that is equal to "pa" and add it to upma.
8466 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8469 isl_union_pw_multi_aff
**upma
= user
;
8470 isl_pw_multi_aff
*pma
;
8472 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8473 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8475 return *upma
? isl_stat_ok
: isl_stat_error
;
8478 /* Construct and return a union piecewise multi affine expression
8479 * that is equal to the given union piecewise affine expression.
8481 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8482 __isl_take isl_union_pw_aff
*upa
)
8485 isl_union_pw_multi_aff
*upma
;
8490 space
= isl_union_pw_aff_get_space(upa
);
8491 upma
= isl_union_pw_multi_aff_empty(space
);
8493 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8494 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8495 upma
= isl_union_pw_multi_aff_free(upma
);
8497 isl_union_pw_aff_free(upa
);
8501 /* Compute the set of elements in the domain of "pa" where it is zero and
8502 * add this set to "uset".
8504 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8506 isl_union_set
**uset
= (isl_union_set
**)user
;
8508 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8510 return *uset
? isl_stat_ok
: isl_stat_error
;
8513 /* Return a union set containing those elements in the domain
8514 * of "upa" where it is zero.
8516 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8517 __isl_take isl_union_pw_aff
*upa
)
8519 isl_union_set
*zero
;
8521 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8522 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8523 zero
= isl_union_set_free(zero
);
8525 isl_union_pw_aff_free(upa
);
8529 /* Internal data structure for isl_union_pw_aff_bind_id,
8530 * storing the parameter that needs to be bound and
8531 * the accumulated results.
8533 struct isl_bind_id_data
{
8535 isl_union_set
*bound
;
8538 /* Bind the piecewise affine function "pa" to the parameter data->id,
8539 * adding the resulting elements in the domain where the expression
8540 * is equal to the parameter to data->bound.
8542 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8544 struct isl_bind_id_data
*data
= user
;
8547 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8548 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8550 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8553 /* Bind the union piecewise affine function "upa" to the parameter "id",
8554 * returning the elements in the domain where the expression
8555 * is equal to the parameter.
8557 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8558 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8560 struct isl_bind_id_data data
= { id
};
8562 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8563 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8564 data
.bound
= isl_union_set_free(data
.bound
);
8566 isl_union_pw_aff_free(upa
);
8571 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8572 * upma is the function that is plugged in.
8573 * pa is the current part of the function in which upma is plugged in.
8574 * res collects the results.
8576 struct isl_union_pw_aff_pullback_upma_data
{
8577 isl_union_pw_multi_aff
*upma
;
8579 isl_union_pw_aff
*res
;
8582 /* Check if "pma" can be plugged into data->pa.
8583 * If so, perform the pullback and add the result to data->res.
8585 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8587 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8590 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8591 pma
->dim
, isl_dim_out
)) {
8592 isl_pw_multi_aff_free(pma
);
8596 pa
= isl_pw_aff_copy(data
->pa
);
8597 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8599 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8601 return data
->res
? isl_stat_ok
: isl_stat_error
;
8604 /* Check if any of the elements of data->upma can be plugged into pa,
8605 * add if so add the result to data->res.
8607 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8609 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8613 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8615 isl_pw_aff_free(pa
);
8620 /* Compute the pullback of "upa" by the function represented by "upma".
8621 * In other words, plug in "upma" in "upa". The result contains
8622 * expressions defined over the domain space of "upma".
8624 * Run over all pairs of elements in "upa" and "upma", perform
8625 * the pullback when appropriate and collect the results.
8626 * If the hash value were based on the domain space rather than
8627 * the function space, then we could run through all elements
8628 * of "upma" and directly pick out the corresponding element of "upa".
8630 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8631 __isl_take isl_union_pw_aff
*upa
,
8632 __isl_take isl_union_pw_multi_aff
*upma
)
8634 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8637 space
= isl_union_pw_multi_aff_get_space(upma
);
8638 upa
= isl_union_pw_aff_align_params(upa
, space
);
8639 space
= isl_union_pw_aff_get_space(upa
);
8640 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8646 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8647 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8648 data
.res
= isl_union_pw_aff_free(data
.res
);
8650 isl_union_pw_aff_free(upa
);
8651 isl_union_pw_multi_aff_free(upma
);
8654 isl_union_pw_aff_free(upa
);
8655 isl_union_pw_multi_aff_free(upma
);
8660 #define BASE union_pw_aff
8662 #define DOMBASE union_set
8664 #include <isl_multi_explicit_domain.c>
8665 #include <isl_multi_union_pw_aff_explicit_domain.c>
8666 #include <isl_multi_templ.c>
8667 #include <isl_multi_un_op_templ.c>
8668 #include <isl_multi_bin_val_templ.c>
8669 #include <isl_multi_align_set.c>
8670 #include <isl_multi_align_union_set.c>
8671 #include <isl_multi_apply_set_explicit_domain_templ.c>
8672 #include <isl_multi_apply_union_set_explicit_domain_templ.c>
8673 #include <isl_multi_arith_templ.c>
8674 #include <isl_multi_bind_templ.c>
8675 #include <isl_multi_coalesce.c>
8676 #include <isl_multi_dim_id_templ.c>
8677 #include <isl_multi_floor.c>
8678 #include <isl_multi_from_base_templ.c>
8679 #include <isl_multi_check_domain_templ.c>
8680 #include <isl_multi_gist.c>
8681 #include <isl_multi_intersect.c>
8682 #include <isl_multi_nan_templ.c>
8683 #include <isl_multi_tuple_id_templ.c>
8684 #include <isl_multi_union_add_templ.c>
8685 #include <isl_multi_zero_space_templ.c>
8687 /* Does "mupa" have a non-trivial explicit domain?
8689 * The explicit domain, if present, is trivial if it represents
8690 * an (obviously) universe parameter set.
8692 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8693 __isl_keep isl_multi_union_pw_aff
*mupa
)
8695 isl_bool is_params
, trivial
;
8699 return isl_bool_error
;
8700 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8701 return isl_bool_false
;
8702 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8703 if (is_params
< 0 || !is_params
)
8704 return isl_bool_not(is_params
);
8705 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8706 trivial
= isl_set_plain_is_universe(set
);
8708 return isl_bool_not(trivial
);
8711 /* Construct a multiple union piecewise affine expression
8712 * in the given space with value zero in each of the output dimensions.
8714 * Since there is no canonical zero value for
8715 * a union piecewise affine expression, we can only construct
8716 * a zero-dimensional "zero" value.
8718 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8719 __isl_take isl_space
*space
)
8727 params
= isl_space_is_params(space
);
8731 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8732 "expecting proper set space", goto error
);
8733 if (!isl_space_is_set(space
))
8734 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8735 "expecting set space", goto error
);
8736 dim
= isl_space_dim(space
, isl_dim_out
);
8740 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8741 "expecting 0D space", goto error
);
8743 return isl_multi_union_pw_aff_alloc(space
);
8745 isl_space_free(space
);
8749 /* Construct and return a multi union piecewise affine expression
8750 * that is equal to the given multi affine expression.
8752 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8753 __isl_take isl_multi_aff
*ma
)
8755 isl_multi_pw_aff
*mpa
;
8757 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8758 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8761 /* This function performs the same operation as
8762 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8763 * isl_multi_aff when exported.
8765 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8766 __isl_take isl_multi_aff
*ma
)
8768 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8771 /* Construct and return a multi union piecewise affine expression
8772 * that is equal to the given multi piecewise affine expression.
8774 * If the resulting multi union piecewise affine expression has
8775 * an explicit domain, then assign it the domain of the input.
8776 * In other cases, the domain is stored in the individual elements.
8778 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8779 __isl_take isl_multi_pw_aff
*mpa
)
8784 isl_multi_union_pw_aff
*mupa
;
8786 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8788 mpa
= isl_multi_pw_aff_free(mpa
);
8792 space
= isl_multi_pw_aff_get_space(mpa
);
8793 space
= isl_space_range(space
);
8794 mupa
= isl_multi_union_pw_aff_alloc(space
);
8796 for (i
= 0; i
< n
; ++i
) {
8798 isl_union_pw_aff
*upa
;
8800 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8801 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8802 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8804 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8806 isl_multi_pw_aff
*copy
;
8808 copy
= isl_multi_pw_aff_copy(mpa
);
8809 dom
= isl_union_set_from_set(isl_multi_pw_aff_domain(copy
));
8810 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8813 isl_multi_pw_aff_free(mpa
);
8818 /* Extract the range space of "pma" and assign it to *space.
8819 * If *space has already been set (through a previous call to this function),
8820 * then check that the range space is the same.
8822 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8824 isl_space
**space
= user
;
8825 isl_space
*pma_space
;
8828 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8829 isl_pw_multi_aff_free(pma
);
8832 return isl_stat_error
;
8838 equal
= isl_space_is_equal(pma_space
, *space
);
8839 isl_space_free(pma_space
);
8842 return isl_stat_error
;
8844 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8845 "range spaces not the same", return isl_stat_error
);
8849 /* Construct and return a multi union piecewise affine expression
8850 * that is equal to the given union piecewise multi affine expression.
8852 * In order to be able to perform the conversion, the input
8853 * needs to be non-empty and may only involve a single range space.
8855 * If the resulting multi union piecewise affine expression has
8856 * an explicit domain, then assign it the domain of the input.
8857 * In other cases, the domain is stored in the individual elements.
8859 __isl_give isl_multi_union_pw_aff
*
8860 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8861 __isl_take isl_union_pw_multi_aff
*upma
)
8863 isl_space
*space
= NULL
;
8864 isl_multi_union_pw_aff
*mupa
;
8868 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8872 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8873 "cannot extract range space from empty input",
8875 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8882 n
= isl_space_dim(space
, isl_dim_set
);
8884 space
= isl_space_free(space
);
8885 mupa
= isl_multi_union_pw_aff_alloc(space
);
8887 for (i
= 0; i
< n
; ++i
) {
8888 isl_union_pw_aff
*upa
;
8890 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8891 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8893 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8895 isl_union_pw_multi_aff
*copy
;
8897 copy
= isl_union_pw_multi_aff_copy(upma
);
8898 dom
= isl_union_pw_multi_aff_domain(copy
);
8899 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8902 isl_union_pw_multi_aff_free(upma
);
8905 isl_space_free(space
);
8906 isl_union_pw_multi_aff_free(upma
);
8910 /* This function performs the same operation as
8911 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8912 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8914 __isl_give isl_multi_union_pw_aff
*
8915 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8916 __isl_take isl_union_pw_multi_aff
*upma
)
8918 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8921 /* Try and create an isl_multi_union_pw_aff that is equivalent
8922 * to the given isl_union_map.
8923 * The isl_union_map is required to be single-valued in each space.
8924 * Moreover, it cannot be empty and all range spaces need to be the same.
8925 * Otherwise, an error is produced.
8927 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8928 __isl_take isl_union_map
*umap
)
8930 isl_union_pw_multi_aff
*upma
;
8932 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8933 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8936 /* This function performs the same operation as
8937 * isl_multi_union_pw_aff_from_union_map,
8938 * but is considered as a function on an isl_union_map when exported.
8940 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8941 __isl_take isl_union_map
*umap
)
8943 return isl_multi_union_pw_aff_from_union_map(umap
);
8946 /* Return a multiple union piecewise affine expression
8947 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8948 * have been aligned.
8950 * If the resulting multi union piecewise affine expression has
8951 * an explicit domain, then assign it the input domain.
8952 * In other cases, the domain is stored in the individual elements.
8954 static __isl_give isl_multi_union_pw_aff
*
8955 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8956 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8961 isl_multi_union_pw_aff
*mupa
;
8963 n
= isl_multi_val_dim(mv
, isl_dim_set
);
8964 if (!domain
|| n
< 0)
8967 space
= isl_multi_val_get_space(mv
);
8968 mupa
= isl_multi_union_pw_aff_alloc(space
);
8969 for (i
= 0; i
< n
; ++i
) {
8971 isl_union_pw_aff
*upa
;
8973 v
= isl_multi_val_get_val(mv
, i
);
8974 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8976 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8978 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8979 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
8980 isl_union_set_copy(domain
));
8982 isl_union_set_free(domain
);
8983 isl_multi_val_free(mv
);
8986 isl_union_set_free(domain
);
8987 isl_multi_val_free(mv
);
8991 /* Return a multiple union piecewise affine expression
8992 * that is equal to "mv" on "domain".
8994 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8995 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8997 isl_bool equal_params
;
9001 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
9002 if (equal_params
< 0)
9005 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9007 domain
= isl_union_set_align_params(domain
,
9008 isl_multi_val_get_space(mv
));
9009 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9010 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9012 isl_union_set_free(domain
);
9013 isl_multi_val_free(mv
);
9017 /* Return a multiple union piecewise affine expression
9018 * that is equal to "ma" on "domain".
9020 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9021 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9023 isl_pw_multi_aff
*pma
;
9025 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9026 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9029 /* Return a multiple union piecewise affine expression
9030 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9031 * have been aligned.
9033 * If the resulting multi union piecewise affine expression has
9034 * an explicit domain, then assign it the input domain.
9035 * In other cases, the domain is stored in the individual elements.
9037 static __isl_give isl_multi_union_pw_aff
*
9038 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9039 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9044 isl_multi_union_pw_aff
*mupa
;
9046 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9047 if (!domain
|| n
< 0)
9049 space
= isl_pw_multi_aff_get_space(pma
);
9050 mupa
= isl_multi_union_pw_aff_alloc(space
);
9051 for (i
= 0; i
< n
; ++i
) {
9053 isl_union_pw_aff
*upa
;
9055 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9056 upa
= isl_union_pw_aff_pw_aff_on_domain(
9057 isl_union_set_copy(domain
), pa
);
9058 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9060 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9061 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9062 isl_union_set_copy(domain
));
9064 isl_union_set_free(domain
);
9065 isl_pw_multi_aff_free(pma
);
9068 isl_union_set_free(domain
);
9069 isl_pw_multi_aff_free(pma
);
9073 /* Return a multiple union piecewise affine expression
9074 * that is equal to "pma" on "domain".
9076 __isl_give isl_multi_union_pw_aff
*
9077 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9078 __isl_take isl_pw_multi_aff
*pma
)
9080 isl_bool equal_params
;
9083 space
= isl_pw_multi_aff_peek_space(pma
);
9084 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9085 if (equal_params
< 0)
9088 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9090 domain
= isl_union_set_align_params(domain
,
9091 isl_pw_multi_aff_get_space(pma
));
9092 pma
= isl_pw_multi_aff_align_params(pma
,
9093 isl_union_set_get_space(domain
));
9094 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9097 isl_union_set_free(domain
);
9098 isl_pw_multi_aff_free(pma
);
9102 /* Return a union set containing those elements in the domains
9103 * of the elements of "mupa" where they are all zero.
9105 * If there are no elements, then simply return the entire domain.
9107 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9108 __isl_take isl_multi_union_pw_aff
*mupa
)
9112 isl_union_pw_aff
*upa
;
9113 isl_union_set
*zero
;
9115 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9117 mupa
= isl_multi_union_pw_aff_free(mupa
);
9122 return isl_multi_union_pw_aff_domain(mupa
);
9124 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9125 zero
= isl_union_pw_aff_zero_union_set(upa
);
9127 for (i
= 1; i
< n
; ++i
) {
9128 isl_union_set
*zero_i
;
9130 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9131 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9133 zero
= isl_union_set_intersect(zero
, zero_i
);
9136 isl_multi_union_pw_aff_free(mupa
);
9140 /* Construct a union map mapping the shared domain
9141 * of the union piecewise affine expressions to the range of "mupa"
9142 * in the special case of a 0D multi union piecewise affine expression.
9144 * Construct a map between the explicit domain of "mupa" and
9146 * Note that this assumes that the domain consists of explicit elements.
9148 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9149 __isl_take isl_multi_union_pw_aff
*mupa
)
9153 isl_union_set
*dom
, *ran
;
9155 space
= isl_multi_union_pw_aff_get_space(mupa
);
9156 dom
= isl_multi_union_pw_aff_domain(mupa
);
9157 ran
= isl_union_set_from_set(isl_set_universe(space
));
9159 is_params
= isl_union_set_is_params(dom
);
9161 dom
= isl_union_set_free(dom
);
9163 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9164 "cannot create union map from expression without "
9165 "explicit domain elements",
9166 dom
= isl_union_set_free(dom
));
9168 return isl_union_map_from_domain_and_range(dom
, ran
);
9171 /* Construct a union map mapping the shared domain
9172 * of the union piecewise affine expressions to the range of "mupa"
9173 * with each dimension in the range equated to the
9174 * corresponding union piecewise affine expression.
9176 * If the input is zero-dimensional, then construct a mapping
9177 * from its explicit domain.
9179 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9180 __isl_take isl_multi_union_pw_aff
*mupa
)
9185 isl_union_map
*umap
;
9186 isl_union_pw_aff
*upa
;
9188 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9190 mupa
= isl_multi_union_pw_aff_free(mupa
);
9195 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9197 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9198 umap
= isl_union_map_from_union_pw_aff(upa
);
9200 for (i
= 1; i
< n
; ++i
) {
9201 isl_union_map
*umap_i
;
9203 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9204 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9205 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9208 space
= isl_multi_union_pw_aff_get_space(mupa
);
9209 umap
= isl_union_map_reset_range_space(umap
, space
);
9211 isl_multi_union_pw_aff_free(mupa
);
9215 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9216 * "range" is the space from which to set the range space.
9217 * "res" collects the results.
9219 struct isl_union_pw_multi_aff_reset_range_space_data
{
9221 isl_union_pw_multi_aff
*res
;
9224 /* Replace the range space of "pma" by the range space of data->range and
9225 * add the result to data->res.
9227 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9229 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9232 space
= isl_pw_multi_aff_get_space(pma
);
9233 space
= isl_space_domain(space
);
9234 space
= isl_space_extend_domain_with_range(space
,
9235 isl_space_copy(data
->range
));
9236 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9237 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9239 return data
->res
? isl_stat_ok
: isl_stat_error
;
9242 /* Replace the range space of all the piecewise affine expressions in "upma" by
9243 * the range space of "space".
9245 * This assumes that all these expressions have the same output dimension.
9247 * Since the spaces of the expressions change, so do their hash values.
9248 * We therefore need to create a new isl_union_pw_multi_aff.
9249 * Note that the hash value is currently computed based on the entire
9250 * space even though there can only be a single expression with a given
9253 static __isl_give isl_union_pw_multi_aff
*
9254 isl_union_pw_multi_aff_reset_range_space(
9255 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9257 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9258 isl_space
*space_upma
;
9260 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9261 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9262 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9263 &reset_range_space
, &data
) < 0)
9264 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9266 isl_space_free(space
);
9267 isl_union_pw_multi_aff_free(upma
);
9271 /* Construct and return a union piecewise multi affine expression
9272 * that is equal to the given multi union piecewise affine expression,
9273 * in the special case of a 0D multi union piecewise affine expression.
9275 * Construct a union piecewise multi affine expression
9276 * on top of the explicit domain of the input.
9278 __isl_give isl_union_pw_multi_aff
*
9279 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9280 __isl_take isl_multi_union_pw_aff
*mupa
)
9284 isl_union_set
*domain
;
9286 space
= isl_multi_union_pw_aff_get_space(mupa
);
9287 mv
= isl_multi_val_zero(space
);
9288 domain
= isl_multi_union_pw_aff_domain(mupa
);
9289 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9292 /* Construct and return a union piecewise multi affine expression
9293 * that is equal to the given multi union piecewise affine expression.
9295 * If the input is zero-dimensional, then
9296 * construct a union piecewise multi affine expression
9297 * on top of the explicit domain of the input.
9299 __isl_give isl_union_pw_multi_aff
*
9300 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9301 __isl_take isl_multi_union_pw_aff
*mupa
)
9306 isl_union_pw_multi_aff
*upma
;
9307 isl_union_pw_aff
*upa
;
9309 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9311 mupa
= isl_multi_union_pw_aff_free(mupa
);
9316 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9318 space
= isl_multi_union_pw_aff_get_space(mupa
);
9319 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9320 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9322 for (i
= 1; i
< n
; ++i
) {
9323 isl_union_pw_multi_aff
*upma_i
;
9325 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9326 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9327 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9330 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9332 isl_multi_union_pw_aff_free(mupa
);
9336 /* Intersect the range of "mupa" with "range",
9337 * in the special case where "mupa" is 0D.
9339 * Intersect the domain of "mupa" with the constraints on the parameters
9342 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9343 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9345 range
= isl_set_params(range
);
9346 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9350 /* Intersect the range of "mupa" with "range".
9351 * That is, keep only those domain elements that have a function value
9354 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9355 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9357 isl_union_pw_multi_aff
*upma
;
9358 isl_union_set
*domain
;
9363 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9364 if (n
< 0 || !range
)
9367 space
= isl_set_get_space(range
);
9368 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9369 space
, isl_dim_set
);
9370 isl_space_free(space
);
9374 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9375 "space don't match", goto error
);
9377 return mupa_intersect_range_0D(mupa
, range
);
9379 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9380 isl_multi_union_pw_aff_copy(mupa
));
9381 domain
= isl_union_set_from_set(range
);
9382 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9383 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9387 isl_multi_union_pw_aff_free(mupa
);
9388 isl_set_free(range
);
9392 /* Return the shared domain of the elements of "mupa",
9393 * in the special case where "mupa" is zero-dimensional.
9395 * Return the explicit domain of "mupa".
9396 * Note that this domain may be a parameter set, either
9397 * because "mupa" is meant to live in a set space or
9398 * because no explicit domain has been set.
9400 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9401 __isl_take isl_multi_union_pw_aff
*mupa
)
9405 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9406 isl_multi_union_pw_aff_free(mupa
);
9411 /* Return the shared domain of the elements of "mupa".
9413 * If "mupa" is zero-dimensional, then return its explicit domain.
9415 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9416 __isl_take isl_multi_union_pw_aff
*mupa
)
9420 isl_union_pw_aff
*upa
;
9423 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9425 mupa
= isl_multi_union_pw_aff_free(mupa
);
9430 return isl_multi_union_pw_aff_domain_0D(mupa
);
9432 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9433 dom
= isl_union_pw_aff_domain(upa
);
9434 for (i
= 1; i
< n
; ++i
) {
9435 isl_union_set
*dom_i
;
9437 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9438 dom_i
= isl_union_pw_aff_domain(upa
);
9439 dom
= isl_union_set_intersect(dom
, dom_i
);
9442 isl_multi_union_pw_aff_free(mupa
);
9446 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9447 * In particular, the spaces have been aligned.
9448 * The result is defined over the shared domain of the elements of "mupa"
9450 * We first extract the parametric constant part of "aff" and
9451 * define that over the shared domain.
9452 * Then we iterate over all input dimensions of "aff" and add the corresponding
9453 * multiples of the elements of "mupa".
9454 * Finally, we consider the integer divisions, calling the function
9455 * recursively to obtain an isl_union_pw_aff corresponding to the
9456 * integer division argument.
9458 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9459 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9462 isl_size n_in
, n_div
;
9463 isl_union_pw_aff
*upa
;
9464 isl_union_set
*uset
;
9468 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9469 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9470 if (n_in
< 0 || n_div
< 0)
9473 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9474 cst
= isl_aff_copy(aff
);
9475 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9476 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9477 cst
= isl_aff_project_domain_on_params(cst
);
9478 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9480 for (i
= 0; i
< n_in
; ++i
) {
9481 isl_union_pw_aff
*upa_i
;
9483 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9485 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9486 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9487 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9488 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9491 for (i
= 0; i
< n_div
; ++i
) {
9493 isl_union_pw_aff
*upa_i
;
9495 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9497 div
= isl_aff_get_div(aff
, i
);
9498 upa_i
= multi_union_pw_aff_apply_aff(
9499 isl_multi_union_pw_aff_copy(mupa
), div
);
9500 upa_i
= isl_union_pw_aff_floor(upa_i
);
9501 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9502 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9503 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9506 isl_multi_union_pw_aff_free(mupa
);
9511 isl_multi_union_pw_aff_free(mupa
);
9516 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9517 * with the domain of "aff".
9518 * Furthermore, the dimension of this space needs to be greater than zero.
9519 * The result is defined over the shared domain of the elements of "mupa"
9521 * We perform these checks and then hand over control to
9522 * multi_union_pw_aff_apply_aff.
9524 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9525 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9528 isl_space
*space1
, *space2
;
9531 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9532 isl_aff_get_space(aff
));
9533 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9537 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9538 space2
= isl_aff_get_domain_space(aff
);
9539 equal
= isl_space_is_equal(space1
, space2
);
9540 isl_space_free(space1
);
9541 isl_space_free(space2
);
9545 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9546 "spaces don't match", goto error
);
9547 dim
= isl_aff_dim(aff
, isl_dim_in
);
9551 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9552 "cannot determine domains", goto error
);
9554 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9556 isl_multi_union_pw_aff_free(mupa
);
9561 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9562 * The space of "mupa" is known to be compatible with the domain of "ma".
9564 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9565 * on the domain of "mupa".
9567 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9568 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9572 dom
= isl_multi_union_pw_aff_domain(mupa
);
9573 ma
= isl_multi_aff_project_domain_on_params(ma
);
9575 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9578 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9579 * with the domain of "ma".
9580 * The result is defined over the shared domain of the elements of "mupa"
9582 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9583 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9585 isl_space
*space1
, *space2
;
9586 isl_multi_union_pw_aff
*res
;
9589 isl_size n_in
, n_out
;
9591 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9592 isl_multi_aff_get_space(ma
));
9593 ma
= isl_multi_aff_align_params(ma
,
9594 isl_multi_union_pw_aff_get_space(mupa
));
9595 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9596 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9597 if (!mupa
|| n_in
< 0 || n_out
< 0)
9600 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9601 space2
= isl_multi_aff_get_domain_space(ma
);
9602 equal
= isl_space_is_equal(space1
, space2
);
9603 isl_space_free(space1
);
9604 isl_space_free(space2
);
9608 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9609 "spaces don't match", goto error
);
9611 return mupa_apply_multi_aff_0D(mupa
, ma
);
9613 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9614 res
= isl_multi_union_pw_aff_alloc(space1
);
9616 for (i
= 0; i
< n_out
; ++i
) {
9618 isl_union_pw_aff
*upa
;
9620 aff
= isl_multi_aff_get_aff(ma
, i
);
9621 upa
= multi_union_pw_aff_apply_aff(
9622 isl_multi_union_pw_aff_copy(mupa
), aff
);
9623 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9626 isl_multi_aff_free(ma
);
9627 isl_multi_union_pw_aff_free(mupa
);
9630 isl_multi_union_pw_aff_free(mupa
);
9631 isl_multi_aff_free(ma
);
9635 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9636 * The space of "mupa" is known to be compatible with the domain of "pa".
9638 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9639 * on the domain of "mupa".
9641 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9642 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9646 dom
= isl_multi_union_pw_aff_domain(mupa
);
9647 pa
= isl_pw_aff_project_domain_on_params(pa
);
9649 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9652 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9653 * with the domain of "pa".
9654 * Furthermore, the dimension of this space needs to be greater than zero.
9655 * The result is defined over the shared domain of the elements of "mupa"
9657 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9658 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9663 isl_space
*space
, *space2
;
9664 isl_union_pw_aff
*upa
;
9666 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9667 isl_pw_aff_get_space(pa
));
9668 pa
= isl_pw_aff_align_params(pa
,
9669 isl_multi_union_pw_aff_get_space(mupa
));
9673 space
= isl_multi_union_pw_aff_get_space(mupa
);
9674 space2
= isl_pw_aff_get_domain_space(pa
);
9675 equal
= isl_space_is_equal(space
, space2
);
9676 isl_space_free(space
);
9677 isl_space_free(space2
);
9681 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9682 "spaces don't match", goto error
);
9683 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9687 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9689 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9690 upa
= isl_union_pw_aff_empty(space
);
9692 for (i
= 0; i
< pa
->n
; ++i
) {
9695 isl_multi_union_pw_aff
*mupa_i
;
9696 isl_union_pw_aff
*upa_i
;
9698 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9699 domain
= isl_set_copy(pa
->p
[i
].set
);
9700 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9701 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9702 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9703 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9706 isl_multi_union_pw_aff_free(mupa
);
9707 isl_pw_aff_free(pa
);
9710 isl_multi_union_pw_aff_free(mupa
);
9711 isl_pw_aff_free(pa
);
9715 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9716 * The space of "mupa" is known to be compatible with the domain of "pma".
9718 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9719 * on the domain of "mupa".
9721 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9722 __isl_take isl_multi_union_pw_aff
*mupa
,
9723 __isl_take isl_pw_multi_aff
*pma
)
9727 dom
= isl_multi_union_pw_aff_domain(mupa
);
9728 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9730 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9733 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9734 * with the domain of "pma".
9735 * The result is defined over the shared domain of the elements of "mupa"
9737 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9738 __isl_take isl_multi_union_pw_aff
*mupa
,
9739 __isl_take isl_pw_multi_aff
*pma
)
9741 isl_space
*space1
, *space2
;
9742 isl_multi_union_pw_aff
*res
;
9745 isl_size n_in
, n_out
;
9747 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9748 isl_pw_multi_aff_get_space(pma
));
9749 pma
= isl_pw_multi_aff_align_params(pma
,
9750 isl_multi_union_pw_aff_get_space(mupa
));
9754 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9755 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9756 equal
= isl_space_is_equal(space1
, space2
);
9757 isl_space_free(space1
);
9758 isl_space_free(space2
);
9762 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9763 "spaces don't match", goto error
);
9764 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9765 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9766 if (n_in
< 0 || n_out
< 0)
9769 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9771 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9772 res
= isl_multi_union_pw_aff_alloc(space1
);
9774 for (i
= 0; i
< n_out
; ++i
) {
9776 isl_union_pw_aff
*upa
;
9778 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9779 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9780 isl_multi_union_pw_aff_copy(mupa
), pa
);
9781 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9784 isl_pw_multi_aff_free(pma
);
9785 isl_multi_union_pw_aff_free(mupa
);
9788 isl_multi_union_pw_aff_free(mupa
);
9789 isl_pw_multi_aff_free(pma
);
9793 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9794 * If the explicit domain only keeps track of constraints on the parameters,
9795 * then only update those constraints.
9797 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9798 __isl_take isl_multi_union_pw_aff
*mupa
,
9799 __isl_keep isl_union_pw_multi_aff
*upma
)
9803 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9804 return isl_multi_union_pw_aff_free(mupa
);
9806 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9810 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9812 return isl_multi_union_pw_aff_free(mupa
);
9814 upma
= isl_union_pw_multi_aff_copy(upma
);
9816 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9817 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9819 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9822 return isl_multi_union_pw_aff_free(mupa
);
9826 /* Compute the pullback of "mupa" by the function represented by "upma".
9827 * In other words, plug in "upma" in "mupa". The result contains
9828 * expressions defined over the domain space of "upma".
9830 * Run over all elements of "mupa" and plug in "upma" in each of them.
9832 * If "mupa" has an explicit domain, then it is this domain
9833 * that needs to undergo a pullback instead, i.e., a preimage.
9835 __isl_give isl_multi_union_pw_aff
*
9836 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9837 __isl_take isl_multi_union_pw_aff
*mupa
,
9838 __isl_take isl_union_pw_multi_aff
*upma
)
9843 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9844 isl_union_pw_multi_aff_get_space(upma
));
9845 upma
= isl_union_pw_multi_aff_align_params(upma
,
9846 isl_multi_union_pw_aff_get_space(mupa
));
9847 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9848 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9852 for (i
= 0; i
< n
; ++i
) {
9853 isl_union_pw_aff
*upa
;
9855 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9856 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9857 isl_union_pw_multi_aff_copy(upma
));
9858 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9861 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9862 mupa
= preimage_explicit_domain(mupa
, upma
);
9864 isl_union_pw_multi_aff_free(upma
);
9867 isl_multi_union_pw_aff_free(mupa
);
9868 isl_union_pw_multi_aff_free(upma
);
9872 /* Extract the sequence of elements in "mupa" with domain space "space"
9873 * (ignoring parameters).
9875 * For the elements of "mupa" that are not defined on the specified space,
9876 * the corresponding element in the result is empty.
9878 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9879 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9883 isl_space
*space_mpa
;
9884 isl_multi_pw_aff
*mpa
;
9886 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9887 if (n
< 0 || !space
)
9890 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9891 space
= isl_space_replace_params(space
, space_mpa
);
9892 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9894 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9896 space
= isl_space_from_domain(space
);
9897 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9898 for (i
= 0; i
< n
; ++i
) {
9899 isl_union_pw_aff
*upa
;
9902 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9903 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9904 isl_space_copy(space
));
9905 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9906 isl_union_pw_aff_free(upa
);
9909 isl_space_free(space
);
9912 isl_space_free(space
);
9916 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9917 * should modify the base expressions in the input.
9919 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9920 * are taken into account.
9921 * "fn" is applied to each entry in the input.
9923 struct isl_union_pw_multi_aff_un_op_control
{
9924 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9925 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9928 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9929 * a second argument) for use as an isl_union_pw_multi_aff_transform
9930 * filter function (which does take a second argument).
9931 * Simply call control->filter without the second argument.
9933 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9934 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9936 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9938 return control
->filter(pma
);
9941 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9942 * a second argument) for use as an isl_union_pw_multi_aff_transform
9943 * base function (which does take a second argument).
9944 * Simply call control->fn without the second argument.
9946 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9947 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9949 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9951 return control
->fn(pma
);
9954 /* Construct an isl_union_pw_multi_aff that is obtained by
9955 * modifying "upma" according to "control".
9957 * isl_union_pw_multi_aff_transform performs essentially
9958 * the same operation, but takes a filter and a callback function
9959 * of a different form (with an extra argument).
9960 * Call isl_union_pw_multi_aff_transform with wrappers
9961 * that remove this extra argument.
9963 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
9964 __isl_take isl_union_pw_multi_aff
*upma
,
9965 struct isl_union_pw_multi_aff_un_op_control
*control
)
9967 struct isl_union_pw_multi_aff_transform_control t_control
= {
9968 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
9969 .filter_user
= control
,
9970 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
9974 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
9977 /* For each function in "upma" of the form A -> [B -> C],
9978 * extract the function A -> B and collect the results.
9980 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
9981 __isl_take isl_union_pw_multi_aff
*upma
)
9983 struct isl_union_pw_multi_aff_un_op_control control
= {
9984 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9985 .fn
= &isl_pw_multi_aff_range_factor_domain
,
9987 return isl_union_pw_multi_aff_un_op(upma
, &control
);
9990 /* For each function in "upma" of the form A -> [B -> C],
9991 * extract the function A -> C and collect the results.
9993 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
9994 __isl_take isl_union_pw_multi_aff
*upma
)
9996 struct isl_union_pw_multi_aff_un_op_control control
= {
9997 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
9998 .fn
= &isl_pw_multi_aff_range_factor_range
,
10000 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10003 /* Evaluate the affine function "aff" in the void point "pnt".
10004 * In particular, return the value NaN.
10006 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10007 __isl_take isl_point
*pnt
)
10011 ctx
= isl_point_get_ctx(pnt
);
10013 isl_point_free(pnt
);
10014 return isl_val_nan(ctx
);
10017 /* Evaluate the affine expression "aff"
10018 * in the coordinates (with denominator) "pnt".
10020 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10021 __isl_keep isl_vec
*pnt
)
10030 ctx
= isl_vec_get_ctx(aff
);
10033 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10034 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10035 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10036 v
= isl_val_normalize(v
);
10043 /* Check that the domain space of "aff" is equal to "space".
10045 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10046 __isl_keep isl_space
*space
)
10050 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10052 return isl_stat_error
;
10054 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10055 "incompatible spaces", return isl_stat_error
);
10056 return isl_stat_ok
;
10059 /* Evaluate the affine function "aff" in "pnt".
10061 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10062 __isl_take isl_point
*pnt
)
10066 isl_local_space
*ls
;
10068 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10070 is_void
= isl_point_is_void(pnt
);
10074 return eval_void(aff
, pnt
);
10076 ls
= isl_aff_get_domain_local_space(aff
);
10077 pnt
= isl_local_space_lift_point(ls
, pnt
);
10079 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10082 isl_point_free(pnt
);
10087 isl_point_free(pnt
);