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 /* Given an affine function on a domain (A -> B),
2823 * interchange A and B in the wrapped domain
2824 * to obtain a function on the domain (B -> A).
2826 * Since this may change the position of some variables,
2827 * it may also change the normalized order of the local variables.
2828 * Restore this order. Since sort_divs assumes the input
2829 * has a single reference, an explicit isl_aff_cow is required.
2831 __isl_give isl_aff
*isl_aff_domain_reverse(__isl_take isl_aff
*aff
)
2834 isl_local_space
*ls
;
2836 isl_size n_in
, n_out
;
2839 space
= isl_aff_peek_domain_space(aff
);
2840 offset
= isl_space_offset(space
, isl_dim_set
);
2841 n_in
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_in
);
2842 n_out
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_out
);
2843 if (offset
< 0 || n_in
< 0 || n_out
< 0)
2844 return isl_aff_free(aff
);
2846 v
= isl_aff_take_rat_aff(aff
);
2847 v
= isl_vec_move_els(v
, 1 + 1 + offset
, 1 + 1 + offset
+ n_in
, n_out
);
2848 aff
= isl_aff_restore_rat_aff(aff
, v
);
2850 ls
= isl_aff_take_domain_local_space(aff
);
2851 ls
= isl_local_space_wrapped_reverse(ls
);
2852 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2854 aff
= isl_aff_cow(aff
);
2855 aff
= sort_divs(aff
);
2860 /* Return a zero isl_aff in the given space.
2862 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2863 * interface over all piecewise types.
2865 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2867 isl_local_space
*ls
;
2869 ls
= isl_local_space_from_space(isl_space_domain(space
));
2870 return isl_aff_zero_on_domain(ls
);
2873 #define isl_aff_involves_nan isl_aff_is_nan
2876 #define PW isl_pw_aff
2880 #define EL_IS_ZERO is_empty
2884 #define IS_ZERO is_empty
2887 #undef DEFAULT_IS_ZERO
2888 #define DEFAULT_IS_ZERO 0
2890 #include <isl_pw_templ.c>
2891 #include <isl_pw_un_op_templ.c>
2892 #include <isl_pw_add_constant_val_templ.c>
2893 #include <isl_pw_add_disjoint_templ.c>
2894 #include <isl_pw_bind_domain_templ.c>
2895 #include <isl_pw_domain_reverse_templ.c>
2896 #include <isl_pw_eval.c>
2897 #include <isl_pw_hash.c>
2898 #include <isl_pw_fix_templ.c>
2899 #include <isl_pw_from_range_templ.c>
2900 #include <isl_pw_insert_dims_templ.c>
2901 #include <isl_pw_insert_domain_templ.c>
2902 #include <isl_pw_move_dims_templ.c>
2903 #include <isl_pw_neg_templ.c>
2904 #include <isl_pw_pullback_templ.c>
2905 #include <isl_pw_scale_templ.c>
2906 #include <isl_pw_sub_templ.c>
2907 #include <isl_pw_union_opt.c>
2912 #include <isl_union_single.c>
2913 #include <isl_union_neg.c>
2914 #include <isl_union_sub_templ.c>
2919 #include <isl_union_pw_templ.c>
2921 /* Compute a piecewise quasi-affine expression with a domain that
2922 * is the union of those of pwaff1 and pwaff2 and such that on each
2923 * cell, the quasi-affine expression is the maximum of those of pwaff1
2924 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2925 * cell, then the associated expression is the defined one.
2927 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2928 __isl_take isl_pw_aff
*pwaff2
)
2930 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2931 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2934 /* Compute a piecewise quasi-affine expression with a domain that
2935 * is the union of those of pwaff1 and pwaff2 and such that on each
2936 * cell, the quasi-affine expression is the minimum of those of pwaff1
2937 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2938 * cell, then the associated expression is the defined one.
2940 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2941 __isl_take isl_pw_aff
*pwaff2
)
2943 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2944 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2947 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2948 __isl_take isl_pw_aff
*pwaff2
, int max
)
2951 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2953 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2956 /* Is the domain of "pa" a product?
2958 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2960 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2964 #define TYPE isl_pw_aff
2965 #include <isl_domain_factor_templ.c>
2967 /* Return a set containing those elements in the domain
2968 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2969 * does not satisfy "fn" (if complement is 1).
2971 * The pieces with a NaN never belong to the result since
2972 * NaN does not satisfy any property.
2974 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2975 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2977 int complement
, void *user
)
2985 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2987 for (i
= 0; i
< pwaff
->n
; ++i
) {
2988 isl_basic_set
*bset
;
2989 isl_set
*set_i
, *locus
;
2992 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2995 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2996 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
2997 locus
= isl_set_from_basic_set(bset
);
2998 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
3000 set_i
= isl_set_subtract(set_i
, locus
);
3002 set_i
= isl_set_intersect(set_i
, locus
);
3003 set
= isl_set_union_disjoint(set
, set_i
);
3006 isl_pw_aff_free(pwaff
);
3011 /* Return a set containing those elements in the domain
3012 * of "pa" where it is positive.
3014 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
3016 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
3019 /* Return a set containing those elements in the domain
3020 * of pwaff where it is non-negative.
3022 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
3024 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
3027 /* Return a set containing those elements in the domain
3028 * of pwaff where it is zero.
3030 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
3032 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
3035 /* Return a set containing those elements in the domain
3036 * of pwaff where it is not zero.
3038 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
3040 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
3043 /* Bind the affine function "aff" to the parameter "id",
3044 * returning the elements in the domain where the affine expression
3045 * is equal to the parameter.
3047 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
3048 __isl_take isl_id
*id
)
3053 space
= isl_aff_get_domain_space(aff
);
3054 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
3056 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
3057 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
3059 return isl_aff_eq_basic_set(aff
, aff_id
);
3062 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
3063 * "rational" should not be set.
3065 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
3066 int rational
, void *user
)
3073 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
3074 "rational binding not supported", goto error
);
3075 return isl_aff_bind_id(aff
, isl_id_copy(id
));
3081 /* Bind the piecewise affine function "pa" to the parameter "id",
3082 * returning the elements in the domain where the expression
3083 * is equal to the parameter.
3085 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
3086 __isl_take isl_id
*id
)
3090 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3096 /* Return a set containing those elements in the shared domain
3097 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3099 * We compute the difference on the shared domain and then construct
3100 * the set of values where this difference is non-negative.
3101 * If strict is set, we first subtract 1 from the difference.
3102 * If equal is set, we only return the elements where pwaff1 and pwaff2
3105 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3106 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3108 isl_set
*set1
, *set2
;
3110 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3111 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3112 set1
= isl_set_intersect(set1
, set2
);
3113 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3114 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3115 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3118 isl_space
*space
= isl_set_get_space(set1
);
3120 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3121 aff
= isl_aff_add_constant_si(aff
, -1);
3122 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3127 return isl_pw_aff_zero_set(pwaff1
);
3128 return isl_pw_aff_nonneg_set(pwaff1
);
3131 /* Return a set containing those elements in the shared domain
3132 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3134 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3135 __isl_take isl_pw_aff
*pwaff2
)
3137 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3138 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3141 /* Return a set containing those elements in the shared domain
3142 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3144 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3145 __isl_take isl_pw_aff
*pwaff2
)
3147 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3148 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3151 /* Return a set containing those elements in the shared domain
3152 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3154 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3155 __isl_take isl_pw_aff
*pwaff2
)
3157 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3158 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3161 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3162 __isl_take isl_pw_aff
*pwaff2
)
3164 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3167 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3168 __isl_take isl_pw_aff
*pwaff2
)
3170 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3173 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3174 * where the function values are ordered in the same way as "order",
3175 * which returns a set in the shared domain of its two arguments.
3177 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3178 * We first pull back the two functions such that they are defined on
3179 * the domain [A -> B]. Then we apply "order", resulting in a set
3180 * in the space [A -> B]. Finally, we unwrap this set to obtain
3181 * a map in the space A -> B.
3183 static __isl_give isl_map
*isl_pw_aff_order_map(
3184 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3185 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3186 __isl_take isl_pw_aff
*pa2
))
3188 isl_space
*space1
, *space2
;
3192 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3193 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3194 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3195 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3196 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3197 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3198 ma
= isl_multi_aff_range_map(space1
);
3199 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3200 set
= order(pa1
, pa2
);
3202 return isl_set_unwrap(set
);
3205 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3206 * where the function values are equal.
3208 __isl_give isl_map
*isl_pw_aff_eq_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_eq_set
);
3214 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3215 * where the function value of "pa1" is less than or equal to
3216 * the function value of "pa2".
3218 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3219 __isl_take isl_pw_aff
*pa2
)
3221 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3224 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3225 * where the function value of "pa1" is less than the function value of "pa2".
3227 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3228 __isl_take isl_pw_aff
*pa2
)
3230 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3233 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3234 * where the function value of "pa1" is greater than or equal to
3235 * the function value of "pa2".
3237 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3238 __isl_take isl_pw_aff
*pa2
)
3240 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3243 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3244 * where the function value of "pa1" is greater than the function value
3247 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3248 __isl_take isl_pw_aff
*pa2
)
3250 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3253 /* Return a set containing those elements in the shared domain
3254 * of the elements of list1 and list2 where each element in list1
3255 * has the relation specified by "fn" with each element in list2.
3257 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3258 __isl_take isl_pw_aff_list
*list2
,
3259 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3260 __isl_take isl_pw_aff
*pwaff2
))
3266 if (!list1
|| !list2
)
3269 ctx
= isl_pw_aff_list_get_ctx(list1
);
3270 if (list1
->n
< 1 || list2
->n
< 1)
3271 isl_die(ctx
, isl_error_invalid
,
3272 "list should contain at least one element", goto error
);
3274 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3275 for (i
= 0; i
< list1
->n
; ++i
)
3276 for (j
= 0; j
< list2
->n
; ++j
) {
3279 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3280 isl_pw_aff_copy(list2
->p
[j
]));
3281 set
= isl_set_intersect(set
, set_ij
);
3284 isl_pw_aff_list_free(list1
);
3285 isl_pw_aff_list_free(list2
);
3288 isl_pw_aff_list_free(list1
);
3289 isl_pw_aff_list_free(list2
);
3293 /* Return a set containing those elements in the shared domain
3294 * of the elements of list1 and list2 where each element in list1
3295 * is equal to each element in list2.
3297 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3298 __isl_take isl_pw_aff_list
*list2
)
3300 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3303 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3304 __isl_take isl_pw_aff_list
*list2
)
3306 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3309 /* Return a set containing those elements in the shared domain
3310 * of the elements of list1 and list2 where each element in list1
3311 * is less than or equal to each element in list2.
3313 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3314 __isl_take isl_pw_aff_list
*list2
)
3316 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3319 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3320 __isl_take isl_pw_aff_list
*list2
)
3322 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3325 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3326 __isl_take isl_pw_aff_list
*list2
)
3328 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3331 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3332 __isl_take isl_pw_aff_list
*list2
)
3334 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3338 /* Return a set containing those elements in the shared domain
3339 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3341 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3342 __isl_take isl_pw_aff
*pwaff2
)
3344 isl_set
*set_lt
, *set_gt
;
3346 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3347 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3348 isl_pw_aff_copy(pwaff2
));
3349 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3350 return isl_set_union_disjoint(set_lt
, set_gt
);
3353 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3358 if (isl_int_is_one(v
))
3360 if (!isl_int_is_pos(v
))
3361 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3362 "factor needs to be positive",
3363 return isl_pw_aff_free(pwaff
));
3364 pwaff
= isl_pw_aff_cow(pwaff
);
3370 for (i
= 0; i
< pwaff
->n
; ++i
) {
3371 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3372 if (!pwaff
->p
[i
].aff
)
3373 return isl_pw_aff_free(pwaff
);
3379 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3381 struct isl_pw_aff_un_op_control control
= { .fn_base
= &isl_aff_floor
};
3382 return isl_pw_aff_un_op(pwaff
, &control
);
3385 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3387 struct isl_pw_aff_un_op_control control
= { .fn_base
= &isl_aff_ceil
};
3388 return isl_pw_aff_un_op(pwaff
, &control
);
3391 /* Assuming that "cond1" and "cond2" are disjoint,
3392 * return an affine expression that is equal to pwaff1 on cond1
3393 * and to pwaff2 on cond2.
3395 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3396 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3397 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3399 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3400 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3402 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3405 /* Return an affine expression that is equal to pwaff_true for elements
3406 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3408 * That is, return cond ? pwaff_true : pwaff_false;
3410 * If "cond" involves and NaN, then we conservatively return a NaN
3411 * on its entire domain. In principle, we could consider the pieces
3412 * where it is NaN separately from those where it is not.
3414 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3415 * then only use the domain of "cond" to restrict the domain.
3417 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3418 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3420 isl_set
*cond_true
, *cond_false
;
3425 if (isl_pw_aff_involves_nan(cond
)) {
3426 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3427 isl_local_space
*ls
= isl_local_space_from_space(space
);
3428 isl_pw_aff_free(cond
);
3429 isl_pw_aff_free(pwaff_true
);
3430 isl_pw_aff_free(pwaff_false
);
3431 return isl_pw_aff_nan_on_domain(ls
);
3434 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3435 isl_pw_aff_get_space(pwaff_false
));
3436 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3437 isl_pw_aff_get_space(pwaff_true
));
3438 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3444 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3445 isl_pw_aff_free(pwaff_false
);
3446 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3449 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3450 cond_false
= isl_pw_aff_zero_set(cond
);
3451 return isl_pw_aff_select(cond_true
, pwaff_true
,
3452 cond_false
, pwaff_false
);
3454 isl_pw_aff_free(cond
);
3455 isl_pw_aff_free(pwaff_true
);
3456 isl_pw_aff_free(pwaff_false
);
3460 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3465 return isl_bool_error
;
3467 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3468 return isl_bool_ok(pos
== -1);
3471 /* Check whether pwaff is a piecewise constant.
3473 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3478 return isl_bool_error
;
3480 for (i
= 0; i
< pwaff
->n
; ++i
) {
3481 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3482 if (is_cst
< 0 || !is_cst
)
3486 return isl_bool_true
;
3489 /* Return the product of "aff1" and "aff2".
3491 * If either of the two is NaN, then the result is NaN.
3493 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3495 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3496 __isl_take isl_aff
*aff2
)
3501 if (isl_aff_is_nan(aff1
)) {
3505 if (isl_aff_is_nan(aff2
)) {
3510 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3511 return isl_aff_mul(aff2
, aff1
);
3513 if (!isl_aff_is_cst(aff2
))
3514 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3515 "at least one affine expression should be constant",
3518 aff1
= isl_aff_cow(aff1
);
3522 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3523 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3533 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3535 * If either of the two is NaN, then the result is NaN.
3536 * A division by zero also results in NaN.
3538 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3539 __isl_take isl_aff
*aff2
)
3541 isl_bool is_cst
, is_zero
;
3547 if (isl_aff_is_nan(aff1
)) {
3551 if (isl_aff_is_nan(aff2
)) {
3556 is_cst
= isl_aff_is_cst(aff2
);
3560 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3561 "second argument should be a constant", goto error
);
3562 is_zero
= isl_aff_plain_is_zero(aff2
);
3566 return set_nan_free(aff1
, aff2
);
3568 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3570 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3571 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3574 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3575 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3578 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3579 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3590 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3591 __isl_take isl_pw_aff
*pwaff2
)
3593 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3594 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3597 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3598 __isl_take isl_pw_aff
*pwaff2
)
3600 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3601 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3604 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3606 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3607 __isl_take isl_pw_aff
*pa2
)
3611 is_cst
= isl_pw_aff_is_cst(pa2
);
3615 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3616 "second argument should be a piecewise constant",
3618 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3619 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3621 isl_pw_aff_free(pa1
);
3622 isl_pw_aff_free(pa2
);
3626 /* Compute the quotient of the integer division of "pa1" by "pa2"
3627 * with rounding towards zero.
3628 * "pa2" is assumed to be a piecewise constant.
3630 * In particular, return
3632 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3635 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3636 __isl_take isl_pw_aff
*pa2
)
3642 is_cst
= isl_pw_aff_is_cst(pa2
);
3646 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3647 "second argument should be a piecewise constant",
3650 pa1
= isl_pw_aff_div(pa1
, pa2
);
3652 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3653 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3654 c
= isl_pw_aff_ceil(pa1
);
3655 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3657 isl_pw_aff_free(pa1
);
3658 isl_pw_aff_free(pa2
);
3662 /* Compute the remainder of the integer division of "pa1" by "pa2"
3663 * with rounding towards zero.
3664 * "pa2" is assumed to be a piecewise constant.
3666 * In particular, return
3668 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3671 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3672 __isl_take isl_pw_aff
*pa2
)
3677 is_cst
= isl_pw_aff_is_cst(pa2
);
3681 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3682 "second argument should be a piecewise constant",
3684 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3685 res
= isl_pw_aff_mul(pa2
, res
);
3686 res
= isl_pw_aff_sub(pa1
, res
);
3689 isl_pw_aff_free(pa1
);
3690 isl_pw_aff_free(pa2
);
3694 /* Does either of "pa1" or "pa2" involve any NaN?
3696 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3697 __isl_keep isl_pw_aff
*pa2
)
3701 has_nan
= isl_pw_aff_involves_nan(pa1
);
3702 if (has_nan
< 0 || has_nan
)
3704 return isl_pw_aff_involves_nan(pa2
);
3707 /* Return a piecewise affine expression defined on the specified domain
3708 * that represents NaN.
3710 static __isl_give isl_pw_aff
*nan_on_domain_set(__isl_take isl_set
*dom
)
3712 isl_local_space
*ls
;
3715 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3716 pa
= isl_pw_aff_nan_on_domain(ls
);
3717 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3722 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3723 * by a NaN on their shared domain.
3725 * In principle, the result could be refined to only being NaN
3726 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3728 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3729 __isl_take isl_pw_aff
*pa2
)
3733 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3734 return nan_on_domain_set(dom
);
3737 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3738 __isl_take isl_pw_aff
*pwaff2
)
3743 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3744 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3745 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3746 isl_pw_aff_copy(pwaff2
));
3747 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3748 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3751 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3752 __isl_take isl_pw_aff
*pwaff2
)
3757 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3758 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3759 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3760 isl_pw_aff_copy(pwaff2
));
3761 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3762 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3765 /* Return an expression for the minimum (if "max" is not set) or
3766 * the maximum (if "max" is set) of "pa1" and "pa2".
3767 * If either expression involves any NaN, then return a NaN
3768 * on the shared domain as result.
3770 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3771 __isl_take isl_pw_aff
*pa2
, int max
)
3775 has_nan
= either_involves_nan(pa1
, pa2
);
3777 pa1
= isl_pw_aff_free(pa1
);
3779 return replace_by_nan(pa1
, pa2
);
3781 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3783 return pw_aff_max(pa1
, pa2
);
3785 return pw_aff_min(pa1
, pa2
);
3788 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3790 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3791 __isl_take isl_pw_aff
*pwaff2
)
3793 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3796 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3798 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3799 __isl_take isl_pw_aff
*pwaff2
)
3801 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3804 /* Does "pa" not involve any NaN?
3806 static isl_bool
pw_aff_no_nan(__isl_keep isl_pw_aff
*pa
, void *user
)
3808 return isl_bool_not(isl_pw_aff_involves_nan(pa
));
3811 /* Does any element of "list" involve any NaN?
3813 * That is, is it not the case that every element does not involve any NaN?
3815 static isl_bool
isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list
*list
)
3817 return isl_bool_not(isl_pw_aff_list_every(list
, &pw_aff_no_nan
, NULL
));
3820 /* Replace "list" (consisting of "n" elements, of which
3821 * at least one element involves a NaN)
3822 * by a NaN on the shared domain of the elements.
3824 * In principle, the result could be refined to only being NaN
3825 * on the parts of this domain where at least one of the elements is NaN.
3827 static __isl_give isl_pw_aff
*replace_list_by_nan(
3828 __isl_take isl_pw_aff_list
*list
, int n
)
3833 dom
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, 0));
3834 for (i
= 1; i
< n
; ++i
) {
3837 dom_i
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, i
));
3838 dom
= isl_set_intersect(dom
, dom_i
);
3841 isl_pw_aff_list_free(list
);
3842 return nan_on_domain_set(dom
);
3845 /* Return the set where the element at "pos1" of "list" is less than or
3846 * equal to the element at "pos2".
3847 * Equality is only allowed if "pos1" is smaller than "pos2".
3849 static __isl_give isl_set
*less(__isl_keep isl_pw_aff_list
*list
,
3852 isl_pw_aff
*pa1
, *pa2
;
3854 pa1
= isl_pw_aff_list_get_at(list
, pos1
);
3855 pa2
= isl_pw_aff_list_get_at(list
, pos2
);
3858 return isl_pw_aff_le_set(pa1
, pa2
);
3860 return isl_pw_aff_lt_set(pa1
, pa2
);
3863 /* Return an isl_pw_aff that maps each element in the intersection of the
3864 * domains of the piecewise affine expressions in "list"
3865 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3866 * expression in "list" at that element.
3867 * If any expression involves any NaN, then return a NaN
3868 * on the shared domain as result.
3870 * If "list" has n elements, then the result consists of n pieces,
3871 * where, in the case of a minimum, each piece has as value expression
3872 * the value expression of one of the elements and as domain
3873 * the set of elements where that value expression
3874 * is less than (or equal) to the other value expressions.
3875 * In the case of a maximum, the condition is
3876 * that all the other value expressions are less than (or equal)
3877 * to the given value expression.
3879 * In order to produce disjoint pieces, a pair of elements
3880 * in the original domain is only allowed to be equal to each other
3881 * on exactly one of the two pieces corresponding to the two elements.
3882 * The position in the list is used to break ties.
3883 * In particular, in the case of a minimum,
3884 * in the piece corresponding to a given element,
3885 * this element is allowed to be equal to any later element in the list,
3886 * but not to any earlier element in the list.
3888 static __isl_give isl_pw_aff
*isl_pw_aff_list_opt(
3889 __isl_take isl_pw_aff_list
*list
, int max
)
3895 isl_pw_aff
*pa
, *res
;
3897 n
= isl_pw_aff_list_size(list
);
3901 isl_die(isl_pw_aff_list_get_ctx(list
), isl_error_invalid
,
3902 "list should contain at least one element", goto error
);
3904 has_nan
= isl_pw_aff_list_involves_nan(list
);
3908 return replace_list_by_nan(list
, n
);
3910 pa
= isl_pw_aff_list_get_at(list
, 0);
3911 space
= isl_pw_aff_get_space(pa
);
3912 isl_pw_aff_free(pa
);
3913 res
= isl_pw_aff_empty(space
);
3915 for (i
= 0; i
< n
; ++i
) {
3916 pa
= isl_pw_aff_list_get_at(list
, i
);
3917 for (j
= 0; j
< n
; ++j
) {
3923 dom
= less(list
, j
, i
);
3925 dom
= less(list
, i
, j
);
3927 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3929 res
= isl_pw_aff_add_disjoint(res
, pa
);
3932 isl_pw_aff_list_free(list
);
3935 isl_pw_aff_list_free(list
);
3939 /* Return an isl_pw_aff that maps each element in the intersection of the
3940 * domains of the elements of list to the minimal corresponding affine
3943 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3945 return isl_pw_aff_list_opt(list
, 0);
3948 /* Return an isl_pw_aff that maps each element in the intersection of the
3949 * domains of the elements of list to the maximal corresponding affine
3952 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3954 return isl_pw_aff_list_opt(list
, 1);
3957 /* Mark the domains of "pwaff" as rational.
3959 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3963 pwaff
= isl_pw_aff_cow(pwaff
);
3969 for (i
= 0; i
< pwaff
->n
; ++i
) {
3970 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3971 if (!pwaff
->p
[i
].set
)
3972 return isl_pw_aff_free(pwaff
);
3978 /* Mark the domains of the elements of "list" as rational.
3980 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3981 __isl_take isl_pw_aff_list
*list
)
3991 for (i
= 0; i
< n
; ++i
) {
3994 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3995 pa
= isl_pw_aff_set_rational(pa
);
3996 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
4002 /* Do the parameters of "aff" match those of "space"?
4004 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
4005 __isl_keep isl_space
*space
)
4007 isl_space
*aff_space
;
4011 return isl_bool_error
;
4013 aff_space
= isl_aff_get_domain_space(aff
);
4015 match
= isl_space_has_equal_params(space
, aff_space
);
4017 isl_space_free(aff_space
);
4021 /* Check that the domain space of "aff" matches "space".
4023 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
4024 __isl_keep isl_space
*space
)
4026 isl_space
*aff_space
;
4030 return isl_stat_error
;
4032 aff_space
= isl_aff_get_domain_space(aff
);
4034 match
= isl_space_has_equal_params(space
, aff_space
);
4038 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4039 "parameters don't match", goto error
);
4040 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
4041 aff_space
, isl_dim_set
);
4045 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4046 "domains don't match", goto error
);
4047 isl_space_free(aff_space
);
4050 isl_space_free(aff_space
);
4051 return isl_stat_error
;
4054 /* Return the shared (universe) domain of the elements of "ma".
4056 * Since an isl_multi_aff (and an isl_aff) is always total,
4057 * the domain is always the universe set in its domain space.
4058 * This is a helper function for use in the generic isl_multi_*_bind.
4060 static __isl_give isl_basic_set
*isl_multi_aff_domain(
4061 __isl_take isl_multi_aff
*ma
)
4065 space
= isl_multi_aff_get_space(ma
);
4066 isl_multi_aff_free(ma
);
4068 return isl_basic_set_universe(isl_space_domain(space
));
4074 #include <isl_multi_no_explicit_domain.c>
4075 #include <isl_multi_templ.c>
4076 #include <isl_multi_un_op_templ.c>
4077 #include <isl_multi_bin_val_templ.c>
4078 #include <isl_multi_add_constant_templ.c>
4079 #include <isl_multi_align_set.c>
4080 #include <isl_multi_arith_templ.c>
4081 #include <isl_multi_bind_domain_templ.c>
4082 #include <isl_multi_cmp.c>
4083 #include <isl_multi_dim_id_templ.c>
4084 #include <isl_multi_dims.c>
4085 #include <isl_multi_domain_reverse_templ.c>
4086 #include <isl_multi_floor.c>
4087 #include <isl_multi_from_base_templ.c>
4088 #include <isl_multi_identity_templ.c>
4089 #include <isl_multi_insert_domain_templ.c>
4090 #include <isl_multi_locals_templ.c>
4091 #include <isl_multi_move_dims_templ.c>
4092 #include <isl_multi_nan_templ.c>
4093 #include <isl_multi_product_templ.c>
4094 #include <isl_multi_splice_templ.c>
4095 #include <isl_multi_tuple_id_templ.c>
4096 #include <isl_multi_unbind_params_templ.c>
4097 #include <isl_multi_zero_templ.c>
4101 #include <isl_multi_check_domain_templ.c>
4102 #include <isl_multi_apply_set_no_explicit_domain_templ.c>
4103 #include <isl_multi_gist.c>
4106 #define DOMBASE basic_set
4107 #include <isl_multi_bind_templ.c>
4109 /* Construct an isl_multi_aff living in "space" that corresponds
4110 * to the affine transformation matrix "mat".
4112 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
4113 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
4116 isl_local_space
*ls
= NULL
;
4117 isl_multi_aff
*ma
= NULL
;
4118 isl_size n_row
, n_col
, n_out
, total
;
4124 ctx
= isl_mat_get_ctx(mat
);
4126 n_row
= isl_mat_rows(mat
);
4127 n_col
= isl_mat_cols(mat
);
4128 n_out
= isl_space_dim(space
, isl_dim_out
);
4129 total
= isl_space_dim(space
, isl_dim_all
);
4130 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
4133 isl_die(ctx
, isl_error_invalid
,
4134 "insufficient number of rows", goto error
);
4136 isl_die(ctx
, isl_error_invalid
,
4137 "insufficient number of columns", goto error
);
4138 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
4139 isl_die(ctx
, isl_error_invalid
,
4140 "dimension mismatch", goto error
);
4142 ma
= isl_multi_aff_zero(isl_space_copy(space
));
4143 space
= isl_space_domain(space
);
4144 ls
= isl_local_space_from_space(isl_space_copy(space
));
4146 for (i
= 0; i
< n_row
- 1; ++i
) {
4150 v
= isl_vec_alloc(ctx
, 1 + n_col
);
4153 isl_int_set(v
->el
[0], mat
->row
[0][0]);
4154 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
4155 v
= isl_vec_normalize(v
);
4156 aff
= isl_aff_alloc_vec_validated(isl_local_space_copy(ls
), v
);
4157 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4160 isl_space_free(space
);
4161 isl_local_space_free(ls
);
4165 isl_space_free(space
);
4166 isl_local_space_free(ls
);
4168 isl_multi_aff_free(ma
);
4172 /* Return the constant terms of the affine expressions of "ma".
4174 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
4175 __isl_keep isl_multi_aff
*ma
)
4182 n
= isl_multi_aff_size(ma
);
4185 space
= isl_space_range(isl_multi_aff_get_space(ma
));
4186 space
= isl_space_drop_all_params(space
);
4187 mv
= isl_multi_val_zero(space
);
4189 for (i
= 0; i
< n
; ++i
) {
4193 aff
= isl_multi_aff_get_at(ma
, i
);
4194 val
= isl_aff_get_constant_val(aff
);
4196 mv
= isl_multi_val_set_at(mv
, i
, val
);
4202 /* Remove any internal structure of the domain of "ma".
4203 * If there is any such internal structure in the input,
4204 * then the name of the corresponding space is also removed.
4206 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4207 __isl_take isl_multi_aff
*ma
)
4214 if (!ma
->space
->nested
[0])
4217 space
= isl_multi_aff_get_space(ma
);
4218 space
= isl_space_flatten_domain(space
);
4219 ma
= isl_multi_aff_reset_space(ma
, space
);
4224 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4225 * of the space to its domain.
4227 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4231 isl_local_space
*ls
;
4236 if (!isl_space_is_map(space
))
4237 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4238 "not a map space", goto error
);
4240 n_in
= isl_space_dim(space
, isl_dim_in
);
4243 space
= isl_space_domain_map(space
);
4245 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4247 isl_space_free(space
);
4251 space
= isl_space_domain(space
);
4252 ls
= isl_local_space_from_space(space
);
4253 for (i
= 0; i
< n_in
; ++i
) {
4256 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4258 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4260 isl_local_space_free(ls
);
4263 isl_space_free(space
);
4267 /* This function performs the same operation as isl_multi_aff_domain_map,
4268 * but is considered as a function on an isl_space when exported.
4270 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4271 __isl_take isl_space
*space
)
4273 return isl_multi_aff_domain_map(space
);
4276 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4277 * of the space to its range.
4279 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4282 isl_size n_in
, n_out
;
4283 isl_local_space
*ls
;
4288 if (!isl_space_is_map(space
))
4289 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4290 "not a map space", goto error
);
4292 n_in
= isl_space_dim(space
, isl_dim_in
);
4293 n_out
= isl_space_dim(space
, isl_dim_out
);
4294 if (n_in
< 0 || n_out
< 0)
4296 space
= isl_space_range_map(space
);
4298 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4300 isl_space_free(space
);
4304 space
= isl_space_domain(space
);
4305 ls
= isl_local_space_from_space(space
);
4306 for (i
= 0; i
< n_out
; ++i
) {
4309 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4310 isl_dim_set
, n_in
+ i
);
4311 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4313 isl_local_space_free(ls
);
4316 isl_space_free(space
);
4320 /* This function performs the same operation as isl_multi_aff_range_map,
4321 * but is considered as a function on an isl_space when exported.
4323 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4324 __isl_take isl_space
*space
)
4326 return isl_multi_aff_range_map(space
);
4329 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4330 * of the space to its domain.
4332 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4333 __isl_take isl_space
*space
)
4335 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4338 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4339 * but is considered as a function on an isl_space when exported.
4341 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4342 __isl_take isl_space
*space
)
4344 return isl_pw_multi_aff_domain_map(space
);
4347 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4348 * of the space to its range.
4350 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4351 __isl_take isl_space
*space
)
4353 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4356 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4357 * but is considered as a function on an isl_space when exported.
4359 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4360 __isl_take isl_space
*space
)
4362 return isl_pw_multi_aff_range_map(space
);
4365 /* Given the space of a set and a range of set dimensions,
4366 * construct an isl_multi_aff that projects out those dimensions.
4368 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4369 __isl_take isl_space
*space
, enum isl_dim_type type
,
4370 unsigned first
, unsigned n
)
4374 isl_local_space
*ls
;
4379 if (!isl_space_is_set(space
))
4380 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4381 "expecting set space", goto error
);
4382 if (type
!= isl_dim_set
)
4383 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4384 "only set dimensions can be projected out", goto error
);
4385 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4388 dim
= isl_space_dim(space
, isl_dim_set
);
4392 space
= isl_space_from_domain(space
);
4393 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4396 return isl_multi_aff_alloc(space
);
4398 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4399 space
= isl_space_domain(space
);
4400 ls
= isl_local_space_from_space(space
);
4402 for (i
= 0; i
< first
; ++i
) {
4405 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4407 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4410 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4413 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4414 isl_dim_set
, first
+ n
+ i
);
4415 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4418 isl_local_space_free(ls
);
4421 isl_space_free(space
);
4425 /* Given the space of a set and a range of set dimensions,
4426 * construct an isl_pw_multi_aff that projects out those dimensions.
4428 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4429 __isl_take isl_space
*space
, enum isl_dim_type type
,
4430 unsigned first
, unsigned n
)
4434 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4435 return isl_pw_multi_aff_from_multi_aff(ma
);
4438 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4439 * but is considered as a function on an isl_multi_aff when exported.
4441 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4442 __isl_take isl_multi_aff
*ma
)
4444 return isl_pw_multi_aff_from_multi_aff(ma
);
4447 /* Create a piecewise multi-affine expression in the given space that maps each
4448 * input dimension to the corresponding output dimension.
4450 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4451 __isl_take isl_space
*space
)
4453 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4456 /* Create a piecewise multi expression that maps elements in the given space
4459 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4460 __isl_take isl_space
*space
)
4464 ma
= isl_multi_aff_identity_on_domain_space(space
);
4465 return isl_pw_multi_aff_from_multi_aff(ma
);
4468 /* This function performs the same operation as
4469 * isl_pw_multi_aff_identity_on_domain_space,
4470 * but is considered as a function on an isl_space when exported.
4472 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4473 __isl_take isl_space
*space
)
4475 return isl_pw_multi_aff_identity_on_domain_space(space
);
4478 /* Exploit the equalities in "eq" to simplify the affine expressions.
4480 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4481 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4486 n
= isl_multi_aff_size(maff
);
4490 for (i
= 0; i
< n
; ++i
) {
4493 aff
= isl_multi_aff_take_at(maff
, i
);
4494 aff
= isl_aff_substitute_equalities(aff
,
4495 isl_basic_set_copy(eq
));
4496 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4499 isl_basic_set_free(eq
);
4502 isl_basic_set_free(eq
);
4503 isl_multi_aff_free(maff
);
4507 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4513 n
= isl_multi_aff_size(maff
);
4515 return isl_multi_aff_free(maff
);
4517 for (i
= 0; i
< n
; ++i
) {
4520 aff
= isl_multi_aff_take_at(maff
, i
);
4521 aff
= isl_aff_scale(aff
, f
);
4522 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4528 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4529 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4531 maff1
= isl_multi_aff_add(maff1
, maff2
);
4532 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4536 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4539 return isl_bool_error
;
4541 return isl_bool_false
;
4544 /* Return the set of domain elements where "ma1" is lexicographically
4545 * smaller than or equal to "ma2".
4547 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4548 __isl_take isl_multi_aff
*ma2
)
4550 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4553 /* Return the set of domain elements where "ma1" is lexicographically
4554 * smaller than "ma2".
4556 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4557 __isl_take isl_multi_aff
*ma2
)
4559 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4562 /* Return the set of domain elements where "ma1" is lexicographically
4563 * greater than to "ma2". If "equal" is set, then include the domain
4564 * elements where they are equal.
4565 * Do this for the case where there are no entries.
4566 * In this case, "ma1" cannot be greater than "ma2",
4567 * but it is (greater than or) equal to "ma2".
4569 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4570 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4574 space
= isl_multi_aff_get_domain_space(ma1
);
4576 isl_multi_aff_free(ma1
);
4577 isl_multi_aff_free(ma2
);
4580 return isl_set_universe(space
);
4582 return isl_set_empty(space
);
4585 /* Return the set where entry "i" of "ma1" and "ma2"
4586 * satisfy the relation prescribed by "cmp".
4588 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4589 __isl_keep isl_multi_aff
*ma2
, int i
,
4590 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4591 __isl_take isl_aff
*aff2
))
4593 isl_aff
*aff1
, *aff2
;
4595 aff1
= isl_multi_aff_get_at(ma1
, i
);
4596 aff2
= isl_multi_aff_get_at(ma2
, i
);
4597 return cmp(aff1
, aff2
);
4600 /* Return the set of domain elements where "ma1" is lexicographically
4601 * greater than to "ma2". If "equal" is set, then include the domain
4602 * elements where they are equal.
4604 * In particular, for all but the final entry,
4605 * include the set of elements where this entry is strictly greater in "ma1"
4606 * and all previous entries are equal.
4607 * The final entry is also allowed to be equal in the two functions
4608 * if "equal" is set.
4610 * The case where there are no entries is handled separately.
4612 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4613 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4622 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4624 n
= isl_multi_aff_size(ma1
);
4628 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4630 space
= isl_multi_aff_get_domain_space(ma1
);
4631 res
= isl_set_empty(isl_space_copy(space
));
4632 equal_set
= isl_set_universe(space
);
4634 for (i
= 0; i
+ 1 < n
; ++i
) {
4638 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4639 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4640 res
= isl_set_union(res
, gt
);
4641 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4642 equal_set
= isl_set_intersect(equal_set
, eq
);
4644 empty
= isl_set_is_empty(equal_set
);
4645 if (empty
>= 0 && empty
)
4650 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4652 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4653 isl_multi_aff_free(ma1
);
4654 isl_multi_aff_free(ma2
);
4656 gte
= isl_set_intersect(gte
, equal_set
);
4657 return isl_set_union(res
, gte
);
4659 isl_multi_aff_free(ma1
);
4660 isl_multi_aff_free(ma2
);
4664 /* Return the set of domain elements where "ma1" is lexicographically
4665 * greater than or equal to "ma2".
4667 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4668 __isl_take isl_multi_aff
*ma2
)
4670 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4673 /* Return the set of domain elements where "ma1" is lexicographically
4674 * greater than "ma2".
4676 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4677 __isl_take isl_multi_aff
*ma2
)
4679 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4682 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4685 #define PW isl_pw_multi_aff
4687 #define BASE multi_aff
4689 #define EL_IS_ZERO is_empty
4693 #define IS_ZERO is_empty
4696 #undef DEFAULT_IS_ZERO
4697 #define DEFAULT_IS_ZERO 0
4699 #include <isl_pw_templ.c>
4700 #include <isl_pw_un_op_templ.c>
4701 #include <isl_pw_add_constant_multi_val_templ.c>
4702 #include <isl_pw_add_constant_val_templ.c>
4703 #include <isl_pw_add_disjoint_templ.c>
4704 #include <isl_pw_bind_domain_templ.c>
4705 #include <isl_pw_domain_reverse_templ.c>
4706 #include <isl_pw_fix_templ.c>
4707 #include <isl_pw_from_range_templ.c>
4708 #include <isl_pw_insert_dims_templ.c>
4709 #include <isl_pw_insert_domain_templ.c>
4710 #include <isl_pw_locals_templ.c>
4711 #include <isl_pw_move_dims_templ.c>
4712 #include <isl_pw_neg_templ.c>
4713 #include <isl_pw_pullback_templ.c>
4714 #include <isl_pw_range_tuple_id_templ.c>
4715 #include <isl_pw_union_opt.c>
4718 #define BASE pw_multi_aff
4720 #include <isl_union_multi.c>
4721 #include "isl_union_locals_templ.c"
4722 #include <isl_union_neg.c>
4723 #include <isl_union_sub_templ.c>
4726 #define BASE multi_aff
4728 #include <isl_union_pw_templ.c>
4730 /* Generic function for extracting a factor from a product "pma".
4731 * "check_space" checks that the space is that of the right kind of product.
4732 * "space_factor" extracts the factor from the space.
4733 * "multi_aff_factor" extracts the factor from the constituent functions.
4735 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4736 __isl_take isl_pw_multi_aff
*pma
,
4737 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4738 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4739 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4740 __isl_take isl_multi_aff
*ma
))
4745 if (check_space(pma
) < 0)
4746 return isl_pw_multi_aff_free(pma
);
4748 space
= isl_pw_multi_aff_take_space(pma
);
4749 space
= space_factor(space
);
4751 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4754 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4755 ma
= multi_aff_factor(ma
);
4756 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4759 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4764 /* Is the range of "pma" a wrapped relation?
4766 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4767 __isl_keep isl_pw_multi_aff
*pma
)
4769 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4772 /* Check that the range of "pma" is a product.
4774 static isl_stat
pw_multi_aff_check_range_product(
4775 __isl_keep isl_pw_multi_aff
*pma
)
4779 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4781 return isl_stat_error
;
4783 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4784 "range is not a product", return isl_stat_error
);
4788 /* Given a function A -> [B -> C], extract the function A -> B.
4790 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4791 __isl_take isl_pw_multi_aff
*pma
)
4793 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4794 &isl_space_range_factor_domain
,
4795 &isl_multi_aff_range_factor_domain
);
4798 /* Given a function A -> [B -> C], extract the function A -> C.
4800 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4801 __isl_take isl_pw_multi_aff
*pma
)
4803 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4804 &isl_space_range_factor_range
,
4805 &isl_multi_aff_range_factor_range
);
4808 /* Given two piecewise multi affine expressions, return a piecewise
4809 * multi-affine expression defined on the union of the definition domains
4810 * of the inputs that is equal to the lexicographic maximum of the two
4811 * inputs on each cell. If only one of the two inputs is defined on
4812 * a given cell, then it is considered to be the maximum.
4814 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4815 __isl_take isl_pw_multi_aff
*pma1
,
4816 __isl_take isl_pw_multi_aff
*pma2
)
4818 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4819 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4820 &isl_multi_aff_lex_ge_set
);
4823 /* Given two piecewise multi affine expressions, return a piecewise
4824 * multi-affine expression defined on the union of the definition domains
4825 * of the inputs that is equal to the lexicographic minimum of the two
4826 * inputs on each cell. If only one of the two inputs is defined on
4827 * a given cell, then it is considered to be the minimum.
4829 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4830 __isl_take isl_pw_multi_aff
*pma1
,
4831 __isl_take isl_pw_multi_aff
*pma2
)
4833 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4834 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4835 &isl_multi_aff_lex_le_set
);
4838 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4839 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4841 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4842 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4843 &isl_multi_aff_add
);
4846 /* Subtract "pma2" from "pma1" and return the result.
4848 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4849 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4851 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4852 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4853 &isl_multi_aff_sub
);
4856 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4857 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4859 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4860 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4864 isl_pw_multi_aff
*res
;
4866 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4869 n
= pma1
->n
* pma2
->n
;
4870 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4871 isl_space_copy(pma2
->dim
));
4872 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4874 for (i
= 0; i
< pma1
->n
; ++i
) {
4875 for (j
= 0; j
< pma2
->n
; ++j
) {
4879 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4880 isl_set_copy(pma2
->p
[j
].set
));
4881 ma
= isl_multi_aff_product(
4882 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4883 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4884 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4888 isl_pw_multi_aff_free(pma1
);
4889 isl_pw_multi_aff_free(pma2
);
4892 isl_pw_multi_aff_free(pma1
);
4893 isl_pw_multi_aff_free(pma2
);
4897 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4898 * denominator "denom".
4899 * "denom" is allowed to be negative, in which case the actual denominator
4900 * is -denom and the expressions are added instead.
4902 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4903 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4909 first
= isl_seq_first_non_zero(c
, n
);
4913 sign
= isl_int_sgn(denom
);
4915 isl_int_abs(d
, denom
);
4916 for (i
= first
; i
< n
; ++i
) {
4919 if (isl_int_is_zero(c
[i
]))
4921 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4922 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4923 aff_i
= isl_aff_scale_down(aff_i
, d
);
4925 aff
= isl_aff_sub(aff
, aff_i
);
4927 aff
= isl_aff_add(aff
, aff_i
);
4934 /* Extract an affine expression that expresses the output dimension "pos"
4935 * of "bmap" in terms of the parameters and input dimensions from
4937 * Note that this expression may involve integer divisions defined
4938 * in terms of parameters and input dimensions.
4939 * The equality may also involve references to earlier (but not later)
4940 * output dimensions. These are replaced by the corresponding elements
4943 * If the equality is of the form
4945 * f(i) + h(j) + a x + g(i) = 0,
4947 * with f(i) a linear combinations of the parameters and input dimensions,
4948 * g(i) a linear combination of integer divisions defined in terms of the same
4949 * and h(j) a linear combinations of earlier output dimensions,
4950 * then the affine expression is
4952 * (-f(i) - g(i))/a - h(j)/a
4954 * If the equality is of the form
4956 * f(i) + h(j) - a x + g(i) = 0,
4958 * then the affine expression is
4960 * (f(i) + g(i))/a - h(j)/(-a)
4963 * If "div" refers to an integer division (i.e., it is smaller than
4964 * the number of integer divisions), then the equality constraint
4965 * does involve an integer division (the one at position "div") that
4966 * is defined in terms of output dimensions. However, this integer
4967 * division can be eliminated by exploiting a pair of constraints
4968 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4969 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4971 * In particular, let
4973 * x = e(i) + m floor(...)
4975 * with e(i) the expression derived above and floor(...) the integer
4976 * division involving output dimensions.
4987 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4988 * = (e(i) - l) mod m
4992 * x - l = (e(i) - l) mod m
4996 * x = ((e(i) - l) mod m) + l
4998 * The variable "shift" below contains the expression -l, which may
4999 * also involve a linear combination of earlier output dimensions.
5001 static __isl_give isl_aff
*extract_aff_from_equality(
5002 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
5003 __isl_keep isl_multi_aff
*ma
)
5006 isl_size n_div
, n_out
;
5008 isl_local_space
*ls
;
5009 isl_aff
*aff
, *shift
;
5012 ctx
= isl_basic_map_get_ctx(bmap
);
5013 ls
= isl_basic_map_get_local_space(bmap
);
5014 ls
= isl_local_space_domain(ls
);
5015 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5018 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
5019 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5020 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5021 if (n_out
< 0 || n_div
< 0)
5023 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
5024 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
5025 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
5026 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
5028 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
5029 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
5030 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
5033 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
5034 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
5035 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
5036 bmap
->eq
[eq
][o_out
+ pos
]);
5038 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
5041 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
5042 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
5043 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
5044 isl_int_set_si(shift
->v
->el
[0], 1);
5045 shift
= subtract_initial(shift
, ma
, pos
,
5046 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
5047 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
5048 mod
= isl_val_int_from_isl_int(ctx
,
5049 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
5050 mod
= isl_val_abs(mod
);
5051 aff
= isl_aff_mod_val(aff
, mod
);
5052 aff
= isl_aff_sub(aff
, shift
);
5055 isl_local_space_free(ls
);
5058 isl_local_space_free(ls
);
5063 /* Given a basic map with output dimensions defined
5064 * in terms of the parameters input dimensions and earlier
5065 * output dimensions using an equality (and possibly a pair on inequalities),
5066 * extract an isl_aff that expresses output dimension "pos" in terms
5067 * of the parameters and input dimensions.
5068 * Note that this expression may involve integer divisions defined
5069 * in terms of parameters and input dimensions.
5070 * "ma" contains the expressions corresponding to earlier output dimensions.
5072 * This function shares some similarities with
5073 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
5075 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
5076 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
5083 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
5084 if (eq
>= bmap
->n_eq
)
5085 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5086 "unable to find suitable equality", return NULL
);
5087 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
5089 aff
= isl_aff_remove_unused_divs(aff
);
5093 /* Given a basic map where each output dimension is defined
5094 * in terms of the parameters and input dimensions using an equality,
5095 * extract an isl_multi_aff that expresses the output dimensions in terms
5096 * of the parameters and input dimensions.
5098 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
5099 __isl_take isl_basic_map
*bmap
)
5108 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
5109 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5111 ma
= isl_multi_aff_free(ma
);
5113 for (i
= 0; i
< n_out
; ++i
) {
5116 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
5117 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5120 isl_basic_map_free(bmap
);
5125 /* Given a basic set where each set dimension is defined
5126 * in terms of the parameters using an equality,
5127 * extract an isl_multi_aff that expresses the set dimensions in terms
5128 * of the parameters.
5130 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
5131 __isl_take isl_basic_set
*bset
)
5133 return extract_isl_multi_aff_from_basic_map(bset
);
5136 /* Create an isl_pw_multi_aff that is equivalent to
5137 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5138 * The given basic map is such that each output dimension is defined
5139 * in terms of the parameters and input dimensions using an equality.
5141 * Since some applications expect the result of isl_pw_multi_aff_from_map
5142 * to only contain integer affine expressions, we compute the floor
5143 * of the expression before returning.
5145 * Remove all constraints involving local variables without
5146 * an explicit representation (resulting in the removal of those
5147 * local variables) prior to the actual extraction to ensure
5148 * that the local spaces in which the resulting affine expressions
5149 * are created do not contain any unknown local variables.
5150 * Removing such constraints is safe because constraints involving
5151 * unknown local variables are not used to determine whether
5152 * a basic map is obviously single-valued.
5154 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
5155 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
5159 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
5160 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
5161 ma
= isl_multi_aff_floor(ma
);
5162 return isl_pw_multi_aff_alloc(domain
, ma
);
5165 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5166 * This obviously only works if the input "map" is single-valued.
5167 * If so, we compute the lexicographic minimum of the image in the form
5168 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5169 * to its lexicographic minimum.
5170 * If the input is not single-valued, we produce an error.
5172 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
5173 __isl_take isl_map
*map
)
5177 isl_pw_multi_aff
*pma
;
5179 sv
= isl_map_is_single_valued(map
);
5183 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5184 "map is not single-valued", goto error
);
5185 map
= isl_map_make_disjoint(map
);
5189 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5191 for (i
= 0; i
< map
->n
; ++i
) {
5192 isl_pw_multi_aff
*pma_i
;
5193 isl_basic_map
*bmap
;
5194 bmap
= isl_basic_map_copy(map
->p
[i
]);
5195 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5196 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5206 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5207 * taking into account that the output dimension at position "d"
5208 * can be represented as
5210 * x = floor((e(...) + c1) / m)
5212 * given that constraint "i" is of the form
5214 * e(...) + c1 - m x >= 0
5217 * Let "map" be of the form
5221 * We construct a mapping
5223 * A -> [A -> x = floor(...)]
5225 * apply that to the map, obtaining
5227 * [A -> x = floor(...)] -> B
5229 * and equate dimension "d" to x.
5230 * We then compute a isl_pw_multi_aff representation of the resulting map
5231 * and plug in the mapping above.
5233 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5234 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5237 isl_space
*space
= NULL
;
5238 isl_local_space
*ls
;
5246 isl_pw_multi_aff
*pma
;
5249 is_set
= isl_map_is_set(map
);
5253 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5254 ctx
= isl_map_get_ctx(map
);
5255 space
= isl_space_domain(isl_map_get_space(map
));
5256 n_in
= isl_space_dim(space
, isl_dim_set
);
5257 n
= isl_space_dim(space
, isl_dim_all
);
5258 if (n_in
< 0 || n
< 0)
5261 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
5263 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
5264 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
5266 isl_basic_map_free(hull
);
5268 ls
= isl_local_space_from_space(isl_space_copy(space
));
5269 aff
= isl_aff_alloc_vec_validated(ls
, v
);
5270 aff
= isl_aff_floor(aff
);
5272 isl_space_free(space
);
5273 ma
= isl_multi_aff_from_aff(aff
);
5275 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5276 ma
= isl_multi_aff_range_product(ma
,
5277 isl_multi_aff_from_aff(aff
));
5280 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5281 map
= isl_map_apply_domain(map
, insert
);
5282 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5283 pma
= isl_pw_multi_aff_from_map(map
);
5284 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5288 isl_space_free(space
);
5290 isl_basic_map_free(hull
);
5294 /* Is constraint "c" of the form
5296 * e(...) + c1 - m x >= 0
5300 * -e(...) + c2 + m x >= 0
5302 * where m > 1 and e only depends on parameters and input dimensions?
5304 * "offset" is the offset of the output dimensions
5305 * "pos" is the position of output dimension x.
5307 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
5309 if (isl_int_is_zero(c
[offset
+ d
]))
5311 if (isl_int_is_one(c
[offset
+ d
]))
5313 if (isl_int_is_negone(c
[offset
+ d
]))
5315 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
5317 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
5318 total
- (offset
+ d
+ 1)) != -1)
5323 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5325 * As a special case, we first check if there is any pair of constraints,
5326 * shared by all the basic maps in "map" that force a given dimension
5327 * to be equal to the floor of some affine combination of the input dimensions.
5329 * In particular, if we can find two constraints
5331 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5335 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5337 * where m > 1 and e only depends on parameters and input dimensions,
5340 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5342 * then we know that we can take
5344 * x = floor((e(...) + c1) / m)
5346 * without having to perform any computation.
5348 * Note that we know that
5352 * If c1 + c2 were 0, then we would have detected an equality during
5353 * simplification. If c1 + c2 were negative, then we would have detected
5356 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5357 __isl_take isl_map
*map
)
5365 isl_basic_map
*hull
;
5367 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5368 dim
= isl_map_dim(map
, isl_dim_out
);
5369 total
= isl_basic_map_dim(hull
, isl_dim_all
);
5370 if (dim
< 0 || total
< 0)
5374 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
5376 for (d
= 0; d
< dim
; ++d
) {
5377 for (i
= 0; i
< n
; ++i
) {
5378 if (!is_potential_div_constraint(hull
->ineq
[i
],
5379 offset
, d
, 1 + total
))
5381 for (j
= i
+ 1; j
< n
; ++j
) {
5382 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
5383 hull
->ineq
[j
] + 1, total
))
5385 isl_int_add(sum
, hull
->ineq
[i
][0],
5387 if (isl_int_abs_lt(sum
,
5388 hull
->ineq
[i
][offset
+ d
]))
5395 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
5397 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
5401 isl_basic_map_free(hull
);
5402 return pw_multi_aff_from_map_base(map
);
5405 isl_basic_map_free(hull
);
5409 /* Given an affine expression
5411 * [A -> B] -> f(A,B)
5413 * construct an isl_multi_aff
5417 * such that dimension "d" in B' is set to "aff" and the remaining
5418 * dimensions are set equal to the corresponding dimensions in B.
5419 * "n_in" is the dimension of the space A.
5420 * "n_out" is the dimension of the space B.
5422 * If "is_set" is set, then the affine expression is of the form
5426 * and we construct an isl_multi_aff
5430 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5431 unsigned n_in
, unsigned n_out
, int is_set
)
5435 isl_space
*space
, *space2
;
5436 isl_local_space
*ls
;
5438 space
= isl_aff_get_domain_space(aff
);
5439 ls
= isl_local_space_from_space(isl_space_copy(space
));
5440 space2
= isl_space_copy(space
);
5442 space2
= isl_space_range(isl_space_unwrap(space2
));
5443 space
= isl_space_map_from_domain_and_range(space
, space2
);
5444 ma
= isl_multi_aff_alloc(space
);
5445 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5447 for (i
= 0; i
< n_out
; ++i
) {
5450 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5451 isl_dim_set
, n_in
+ i
);
5452 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5455 isl_local_space_free(ls
);
5460 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5461 * taking into account that the dimension at position "d" can be written as
5463 * x = m a + f(..) (1)
5465 * where m is equal to "gcd".
5466 * "i" is the index of the equality in "hull" that defines f(..).
5467 * In particular, the equality is of the form
5469 * f(..) - x + m g(existentials) = 0
5473 * -f(..) + x + m g(existentials) = 0
5475 * We basically plug (1) into "map", resulting in a map with "a"
5476 * in the range instead of "x". The corresponding isl_pw_multi_aff
5477 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5479 * Specifically, given the input map
5483 * We first wrap it into a set
5487 * and define (1) on top of the corresponding space, resulting in "aff".
5488 * We use this to create an isl_multi_aff that maps the output position "d"
5489 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5490 * We plug this into the wrapped map, unwrap the result and compute the
5491 * corresponding isl_pw_multi_aff.
5492 * The result is an expression
5500 * so that we can plug that into "aff", after extending the latter to
5506 * If "map" is actually a set, then there is no "A" space, meaning
5507 * that we do not need to perform any wrapping, and that the result
5508 * of the recursive call is of the form
5512 * which is plugged into a mapping of the form
5516 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5517 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5522 isl_local_space
*ls
;
5525 isl_pw_multi_aff
*pma
, *id
;
5531 is_set
= isl_map_is_set(map
);
5535 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5536 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5537 if (n_in
< 0 || n_out
< 0)
5539 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5544 set
= isl_map_wrap(map
);
5545 space
= isl_space_map_from_set(isl_set_get_space(set
));
5546 ma
= isl_multi_aff_identity(space
);
5547 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5548 aff
= isl_aff_alloc(ls
);
5550 isl_int_set_si(aff
->v
->el
[0], 1);
5551 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5552 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5555 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5557 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5559 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5560 set
= isl_set_preimage_multi_aff(set
, ma
);
5562 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5567 map
= isl_set_unwrap(set
);
5568 pma
= isl_pw_multi_aff_from_map(map
);
5571 space
= isl_pw_multi_aff_get_domain_space(pma
);
5572 space
= isl_space_map_from_set(space
);
5573 id
= isl_pw_multi_aff_identity(space
);
5574 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5576 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5577 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5579 isl_basic_map_free(hull
);
5583 isl_basic_map_free(hull
);
5587 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5588 * "hull" contains the equalities valid for "map".
5590 * Check if any of the output dimensions is "strided".
5591 * That is, we check if it can be written as
5595 * with m greater than 1, a some combination of existentially quantified
5596 * variables and f an expression in the parameters and input dimensions.
5597 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5599 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5602 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5603 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5612 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5613 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5614 if (n_div
< 0 || n_out
< 0)
5618 isl_basic_map_free(hull
);
5619 return pw_multi_aff_from_map_check_div(map
);
5624 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5625 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5627 for (i
= 0; i
< n_out
; ++i
) {
5628 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5629 isl_int
*eq
= hull
->eq
[j
];
5630 isl_pw_multi_aff
*res
;
5632 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5633 !isl_int_is_negone(eq
[o_out
+ i
]))
5635 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5637 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5638 n_out
- (i
+ 1)) != -1)
5640 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5641 if (isl_int_is_zero(gcd
))
5643 if (isl_int_is_one(gcd
))
5646 res
= pw_multi_aff_from_map_stride(map
, hull
,
5654 isl_basic_map_free(hull
);
5655 return pw_multi_aff_from_map_check_div(map
);
5658 isl_basic_map_free(hull
);
5662 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5664 * As a special case, we first check if all output dimensions are uniquely
5665 * defined in terms of the parameters and input dimensions over the entire
5666 * domain. If so, we extract the desired isl_pw_multi_aff directly
5667 * from the affine hull of "map" and its domain.
5669 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5672 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5676 isl_basic_map
*hull
;
5678 n
= isl_map_n_basic_map(map
);
5683 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5684 hull
= isl_basic_map_plain_affine_hull(hull
);
5685 sv
= isl_basic_map_plain_is_single_valued(hull
);
5687 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5689 isl_basic_map_free(hull
);
5691 map
= isl_map_detect_equalities(map
);
5692 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5693 sv
= isl_basic_map_plain_is_single_valued(hull
);
5695 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5697 return pw_multi_aff_from_map_check_strides(map
, hull
);
5698 isl_basic_map_free(hull
);
5704 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5705 * but is considered as a function on an isl_map when exported.
5707 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5709 return isl_pw_multi_aff_from_map(map
);
5712 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5714 return isl_pw_multi_aff_from_map(set
);
5717 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5718 * but is considered as a function on an isl_set when exported.
5720 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5722 return isl_pw_multi_aff_from_set(set
);
5725 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5728 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5730 isl_union_pw_multi_aff
**upma
= user
;
5731 isl_pw_multi_aff
*pma
;
5733 pma
= isl_pw_multi_aff_from_map(map
);
5734 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5736 return *upma
? isl_stat_ok
: isl_stat_error
;
5739 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5742 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5743 __isl_take isl_aff
*aff
)
5746 isl_pw_multi_aff
*pma
;
5748 ma
= isl_multi_aff_from_aff(aff
);
5749 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5750 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5753 /* Try and create an isl_union_pw_multi_aff that is equivalent
5754 * to the given isl_union_map.
5755 * The isl_union_map is required to be single-valued in each space.
5756 * Otherwise, an error is produced.
5758 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5759 __isl_take isl_union_map
*umap
)
5762 isl_union_pw_multi_aff
*upma
;
5764 space
= isl_union_map_get_space(umap
);
5765 upma
= isl_union_pw_multi_aff_empty(space
);
5766 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5767 upma
= isl_union_pw_multi_aff_free(upma
);
5768 isl_union_map_free(umap
);
5773 /* This function performs the same operation as
5774 * isl_union_pw_multi_aff_from_union_map,
5775 * but is considered as a function on an isl_union_map when exported.
5777 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5778 __isl_take isl_union_map
*umap
)
5780 return isl_union_pw_multi_aff_from_union_map(umap
);
5783 /* Try and create an isl_union_pw_multi_aff that is equivalent
5784 * to the given isl_union_set.
5785 * The isl_union_set is required to be a singleton in each space.
5786 * Otherwise, an error is produced.
5788 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5789 __isl_take isl_union_set
*uset
)
5791 return isl_union_pw_multi_aff_from_union_map(uset
);
5794 /* Return the piecewise affine expression "set ? 1 : 0".
5796 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5799 isl_space
*space
= isl_set_get_space(set
);
5800 isl_local_space
*ls
= isl_local_space_from_space(space
);
5801 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5802 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5804 one
= isl_aff_add_constant_si(one
, 1);
5805 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5806 set
= isl_set_complement(set
);
5807 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5812 /* Plug in "subs" for dimension "type", "pos" of "aff".
5814 * Let i be the dimension to replace and let "subs" be of the form
5818 * and "aff" of the form
5824 * (a f + d g')/(m d)
5826 * where g' is the result of plugging in "subs" in each of the integer
5829 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5830 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5836 aff
= isl_aff_cow(aff
);
5838 return isl_aff_free(aff
);
5840 ctx
= isl_aff_get_ctx(aff
);
5841 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5842 isl_die(ctx
, isl_error_invalid
,
5843 "spaces don't match", return isl_aff_free(aff
));
5844 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5846 return isl_aff_free(aff
);
5848 isl_die(ctx
, isl_error_unsupported
,
5849 "cannot handle divs yet", return isl_aff_free(aff
));
5851 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5853 return isl_aff_free(aff
);
5855 aff
->v
= isl_vec_cow(aff
->v
);
5857 return isl_aff_free(aff
);
5859 pos
+= isl_local_space_offset(aff
->ls
, type
);
5862 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5863 aff
->v
->size
, subs
->v
->size
, v
);
5869 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5870 * expressions in "maff".
5872 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5873 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5874 __isl_keep isl_aff
*subs
)
5879 n
= isl_multi_aff_size(maff
);
5881 return isl_multi_aff_free(maff
);
5883 if (type
== isl_dim_in
)
5886 for (i
= 0; i
< n
; ++i
) {
5889 aff
= isl_multi_aff_take_at(maff
, i
);
5890 aff
= isl_aff_substitute(aff
, type
, pos
, subs
);
5891 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
5897 /* Plug in "subs" for input dimension "pos" of "pma".
5899 * pma is of the form
5903 * while subs is of the form
5905 * v' = B_j(v) -> S_j
5907 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5908 * has a contribution in the result, in particular
5910 * C_ij(S_j) -> M_i(S_j)
5912 * Note that plugging in S_j in C_ij may also result in an empty set
5913 * and this contribution should simply be discarded.
5915 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5916 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5917 __isl_keep isl_pw_aff
*subs
)
5920 isl_pw_multi_aff
*res
;
5923 return isl_pw_multi_aff_free(pma
);
5925 n
= pma
->n
* subs
->n
;
5926 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5928 for (i
= 0; i
< pma
->n
; ++i
) {
5929 for (j
= 0; j
< subs
->n
; ++j
) {
5931 isl_multi_aff
*res_ij
;
5934 common
= isl_set_intersect(
5935 isl_set_copy(pma
->p
[i
].set
),
5936 isl_set_copy(subs
->p
[j
].set
));
5937 common
= isl_set_substitute(common
,
5938 pos
, subs
->p
[j
].aff
);
5939 empty
= isl_set_plain_is_empty(common
);
5940 if (empty
< 0 || empty
) {
5941 isl_set_free(common
);
5947 res_ij
= isl_multi_aff_substitute(
5948 isl_multi_aff_copy(pma
->p
[i
].maff
),
5949 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5951 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5955 isl_pw_multi_aff_free(pma
);
5958 isl_pw_multi_aff_free(pma
);
5959 isl_pw_multi_aff_free(res
);
5963 /* Compute the preimage of a range of dimensions in the affine expression "src"
5964 * under "ma" and put the result in "dst". The number of dimensions in "src"
5965 * that precede the range is given by "n_before". The number of dimensions
5966 * in the range is given by the number of output dimensions of "ma".
5967 * The number of dimensions that follow the range is given by "n_after".
5968 * If "has_denom" is set (to one),
5969 * then "src" and "dst" have an extra initial denominator.
5970 * "n_div_ma" is the number of existentials in "ma"
5971 * "n_div_bset" is the number of existentials in "src"
5972 * The resulting "dst" (which is assumed to have been allocated by
5973 * the caller) contains coefficients for both sets of existentials,
5974 * first those in "ma" and then those in "src".
5975 * f, c1, c2 and g are temporary objects that have been initialized
5978 * Let src represent the expression
5980 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5982 * and let ma represent the expressions
5984 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5986 * We start out with the following expression for dst:
5988 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5990 * with the multiplication factor f initially equal to 1
5991 * and f \sum_i b_i v_i kept separately.
5992 * For each x_i that we substitute, we multiply the numerator
5993 * (and denominator) of dst by c_1 = m_i and add the numerator
5994 * of the x_i expression multiplied by c_2 = f b_i,
5995 * after removing the common factors of c_1 and c_2.
5996 * The multiplication factor f also needs to be multiplied by c_1
5997 * for the next x_j, j > i.
5999 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
6000 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
6001 int n_div_ma
, int n_div_bmap
,
6002 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
6005 isl_size n_param
, n_in
, n_out
;
6008 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
6009 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
6010 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
6011 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
6012 return isl_stat_error
;
6014 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
6015 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
6016 isl_seq_clr(dst
+ o_dst
, n_in
);
6019 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
6022 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
6024 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
6026 isl_int_set_si(f
, 1);
6028 for (i
= 0; i
< n_out
; ++i
) {
6029 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
6031 if (isl_int_is_zero(src
[offset
]))
6033 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
6034 isl_int_mul(c2
, f
, src
[offset
]);
6035 isl_int_gcd(g
, c1
, c2
);
6036 isl_int_divexact(c1
, c1
, g
);
6037 isl_int_divexact(c2
, c2
, g
);
6039 isl_int_mul(f
, f
, c1
);
6042 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6043 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
6044 o_dst
+= 1 + n_param
;
6045 o_src
+= 1 + n_param
;
6046 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
6048 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6049 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
6052 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
6054 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6055 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
6058 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
6060 isl_int_mul(dst
[0], dst
[0], c1
);
6066 /* Compute the pullback of "aff" by the function represented by "ma".
6067 * In other words, plug in "ma" in "aff". The result is an affine expression
6068 * defined over the domain space of "ma".
6070 * If "aff" is represented by
6072 * (a(p) + b x + c(divs))/d
6074 * and ma is represented by
6076 * x = D(p) + F(y) + G(divs')
6078 * then the result is
6080 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
6082 * The divs in the local space of the input are similarly adjusted
6083 * through a call to isl_local_space_preimage_multi_aff.
6085 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
6086 __isl_take isl_multi_aff
*ma
)
6088 isl_aff
*res
= NULL
;
6089 isl_local_space
*ls
;
6090 isl_size n_div_aff
, n_div_ma
;
6091 isl_int f
, c1
, c2
, g
;
6093 ma
= isl_multi_aff_align_divs(ma
);
6097 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
6098 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
6099 if (n_div_aff
< 0 || n_div_ma
< 0)
6102 ls
= isl_aff_get_domain_local_space(aff
);
6103 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
6104 res
= isl_aff_alloc(ls
);
6113 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
6114 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
6115 res
= isl_aff_free(res
);
6123 isl_multi_aff_free(ma
);
6124 res
= isl_aff_normalize(res
);
6128 isl_multi_aff_free(ma
);
6133 /* Compute the pullback of "aff1" by the function represented by "aff2".
6134 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6135 * defined over the domain space of "aff1".
6137 * The domain of "aff1" should match the range of "aff2", which means
6138 * that it should be single-dimensional.
6140 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
6141 __isl_take isl_aff
*aff2
)
6145 ma
= isl_multi_aff_from_aff(aff2
);
6146 return isl_aff_pullback_multi_aff(aff1
, ma
);
6149 /* Compute the pullback of "ma1" by the function represented by "ma2".
6150 * In other words, plug in "ma2" in "ma1".
6152 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
6153 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
6157 isl_space
*space
= NULL
;
6159 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
6160 ma2
= isl_multi_aff_align_divs(ma2
);
6161 n
= isl_multi_aff_size(ma1
);
6165 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
6166 isl_multi_aff_get_space(ma1
));
6168 for (i
= 0; i
< n
; ++i
) {
6171 aff
= isl_multi_aff_take_at(ma1
, i
);
6172 aff
= isl_aff_pullback_multi_aff(aff
, isl_multi_aff_copy(ma2
));
6173 ma1
= isl_multi_aff_restore_at(ma1
, i
, aff
);
6176 ma1
= isl_multi_aff_reset_space(ma1
, space
);
6177 isl_multi_aff_free(ma2
);
6180 isl_space_free(space
);
6181 isl_multi_aff_free(ma2
);
6182 isl_multi_aff_free(ma1
);
6186 /* Extend the local space of "dst" to include the divs
6187 * in the local space of "src".
6189 * If "src" does not have any divs or if the local spaces of "dst" and
6190 * "src" are the same, then no extension is required.
6192 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6193 __isl_keep isl_aff
*src
)
6196 isl_size src_n_div
, dst_n_div
;
6203 return isl_aff_free(dst
);
6205 ctx
= isl_aff_get_ctx(src
);
6206 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6208 return isl_aff_free(dst
);
6210 isl_die(ctx
, isl_error_invalid
,
6211 "spaces don't match", goto error
);
6213 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6214 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6217 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6218 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6219 return isl_aff_free(dst
);
6223 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6224 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6225 if (!exp1
|| (dst_n_div
&& !exp2
))
6228 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6229 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6237 return isl_aff_free(dst
);
6240 /* Adjust the local spaces of the affine expressions in "maff"
6241 * such that they all have the save divs.
6243 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6244 __isl_take isl_multi_aff
*maff
)
6250 n
= isl_multi_aff_size(maff
);
6252 return isl_multi_aff_free(maff
);
6256 aff_0
= isl_multi_aff_take_at(maff
, 0);
6257 for (i
= 1; i
< n
; ++i
) {
6260 aff_i
= isl_multi_aff_peek_at(maff
, i
);
6261 aff_0
= isl_aff_align_divs(aff_0
, aff_i
);
6263 maff
= isl_multi_aff_restore_at(maff
, 0, aff_0
);
6265 aff_0
= isl_multi_aff_peek_at(maff
, 0);
6266 for (i
= 1; i
< n
; ++i
) {
6269 aff_i
= isl_multi_aff_take_at(maff
, i
);
6270 aff_i
= isl_aff_align_divs(aff_i
, aff_0
);
6271 maff
= isl_multi_aff_restore_at(maff
, i
, aff_i
);
6277 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6279 aff
= isl_aff_cow(aff
);
6283 aff
->ls
= isl_local_space_lift(aff
->ls
);
6285 return isl_aff_free(aff
);
6290 /* Lift "maff" to a space with extra dimensions such that the result
6291 * has no more existentially quantified variables.
6292 * If "ls" is not NULL, then *ls is assigned the local space that lies
6293 * at the basis of the lifting applied to "maff".
6295 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6296 __isl_give isl_local_space
**ls
)
6306 n
= isl_multi_aff_size(maff
);
6308 return isl_multi_aff_free(maff
);
6312 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6313 *ls
= isl_local_space_from_space(space
);
6315 return isl_multi_aff_free(maff
);
6320 maff
= isl_multi_aff_align_divs(maff
);
6322 aff
= isl_multi_aff_peek_at(maff
, 0);
6323 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6325 return isl_multi_aff_free(maff
);
6326 space
= isl_multi_aff_get_space(maff
);
6327 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6328 space
= isl_space_extend_domain_with_range(space
,
6329 isl_multi_aff_get_space(maff
));
6330 maff
= isl_multi_aff_restore_space(maff
, space
);
6333 aff
= isl_multi_aff_peek_at(maff
, 0);
6334 *ls
= isl_aff_get_domain_local_space(aff
);
6336 return isl_multi_aff_free(maff
);
6339 for (i
= 0; i
< n
; ++i
) {
6340 aff
= isl_multi_aff_take_at(maff
, i
);
6341 aff
= isl_aff_lift(aff
);
6342 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
6349 #define TYPE isl_pw_multi_aff
6351 #include "check_type_range_templ.c"
6353 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6355 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6356 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6363 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6366 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6370 space
= isl_pw_multi_aff_get_space(pma
);
6371 space
= isl_space_drop_dims(space
, isl_dim_out
,
6372 pos
+ 1, n_out
- pos
- 1);
6373 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6375 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6376 for (i
= 0; i
< pma
->n
; ++i
) {
6378 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6379 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6385 /* This is an alternative name for the function above.
6387 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6388 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6390 return isl_pw_multi_aff_get_at(pma
, pos
);
6393 /* Return an isl_pw_multi_aff with the given "set" as domain and
6394 * an unnamed zero-dimensional range.
6396 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6397 __isl_take isl_set
*set
)
6402 space
= isl_set_get_space(set
);
6403 space
= isl_space_from_domain(space
);
6404 ma
= isl_multi_aff_zero(space
);
6405 return isl_pw_multi_aff_alloc(set
, ma
);
6408 /* Add an isl_pw_multi_aff with the given "set" as domain and
6409 * an unnamed zero-dimensional range to *user.
6411 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6414 isl_union_pw_multi_aff
**upma
= user
;
6415 isl_pw_multi_aff
*pma
;
6417 pma
= isl_pw_multi_aff_from_domain(set
);
6418 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6423 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6424 * an unnamed zero-dimensional range.
6426 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6427 __isl_take isl_union_set
*uset
)
6430 isl_union_pw_multi_aff
*upma
;
6435 space
= isl_union_set_get_space(uset
);
6436 upma
= isl_union_pw_multi_aff_empty(space
);
6438 if (isl_union_set_foreach_set(uset
,
6439 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6442 isl_union_set_free(uset
);
6445 isl_union_set_free(uset
);
6446 isl_union_pw_multi_aff_free(upma
);
6450 /* Local data for bin_entry and the callback "fn".
6452 struct isl_union_pw_multi_aff_bin_data
{
6453 isl_union_pw_multi_aff
*upma2
;
6454 isl_union_pw_multi_aff
*res
;
6455 isl_pw_multi_aff
*pma
;
6456 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6459 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6460 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6462 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6464 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6468 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6470 isl_pw_multi_aff_free(pma
);
6475 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6476 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6477 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6478 * as *entry. The callback should adjust data->res if desired.
6480 static __isl_give isl_union_pw_multi_aff
*bin_op(
6481 __isl_take isl_union_pw_multi_aff
*upma1
,
6482 __isl_take isl_union_pw_multi_aff
*upma2
,
6483 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6486 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6488 space
= isl_union_pw_multi_aff_get_space(upma2
);
6489 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6490 space
= isl_union_pw_multi_aff_get_space(upma1
);
6491 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6493 if (!upma1
|| !upma2
)
6497 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6498 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6499 &bin_entry
, &data
) < 0)
6502 isl_union_pw_multi_aff_free(upma1
);
6503 isl_union_pw_multi_aff_free(upma2
);
6506 isl_union_pw_multi_aff_free(upma1
);
6507 isl_union_pw_multi_aff_free(upma2
);
6508 isl_union_pw_multi_aff_free(data
.res
);
6512 /* Given two isl_pw_multi_affs A -> B and C -> D,
6513 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6515 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6516 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6520 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6521 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6522 isl_pw_multi_aff_get_space(pma2
));
6523 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6524 &isl_multi_aff_range_product
);
6527 /* Given two isl_pw_multi_affs A -> B and C -> D,
6528 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6530 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6531 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6535 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6536 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6537 isl_pw_multi_aff_get_space(pma2
));
6538 space
= isl_space_flatten_range(space
);
6539 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6540 &isl_multi_aff_flat_range_product
);
6543 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6544 * to compute some form of range product and add the result to data->res.
6546 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6547 __isl_give isl_pw_multi_aff
*(*range_product
)(
6548 __isl_take isl_pw_multi_aff
*pma1
,
6549 __isl_take isl_pw_multi_aff
*pma2
),
6552 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6554 isl_space
*space1
, *space2
;
6556 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6557 space2
= isl_pw_multi_aff_peek_space(pma2
);
6558 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6559 space2
, isl_dim_in
);
6560 if (match
< 0 || !match
) {
6561 isl_pw_multi_aff_free(pma2
);
6562 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6565 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6567 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6572 /* If data->pma and "pma2" have the same domain space, then compute
6573 * their flat range product and add the result to data->res.
6575 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6578 return gen_range_product_entry(pma2
,
6579 &isl_pw_multi_aff_flat_range_product
, user
);
6582 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6583 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6585 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6586 __isl_take isl_union_pw_multi_aff
*upma1
,
6587 __isl_take isl_union_pw_multi_aff
*upma2
)
6589 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6592 /* If data->pma and "pma2" have the same domain space, then compute
6593 * their range product and add the result to data->res.
6595 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6598 return gen_range_product_entry(pma2
,
6599 &isl_pw_multi_aff_range_product
, user
);
6602 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6603 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6605 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6606 __isl_take isl_union_pw_multi_aff
*upma1
,
6607 __isl_take isl_union_pw_multi_aff
*upma2
)
6609 return bin_op(upma1
, upma2
, &range_product_entry
);
6612 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6613 * The parameters are assumed to have been aligned.
6615 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6616 * except that it works on two different isl_pw_* types.
6618 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6619 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6620 __isl_take isl_pw_aff
*pa
)
6623 isl_pw_multi_aff
*res
= NULL
;
6628 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6629 pa
->dim
, isl_dim_in
))
6630 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6631 "domains don't match", goto error
);
6632 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6636 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6638 for (i
= 0; i
< pma
->n
; ++i
) {
6639 for (j
= 0; j
< pa
->n
; ++j
) {
6641 isl_multi_aff
*res_ij
;
6644 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6645 isl_set_copy(pa
->p
[j
].set
));
6646 empty
= isl_set_plain_is_empty(common
);
6647 if (empty
< 0 || empty
) {
6648 isl_set_free(common
);
6654 res_ij
= isl_multi_aff_set_aff(
6655 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6656 isl_aff_copy(pa
->p
[j
].aff
));
6657 res_ij
= isl_multi_aff_gist(res_ij
,
6658 isl_set_copy(common
));
6660 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6664 isl_pw_multi_aff_free(pma
);
6665 isl_pw_aff_free(pa
);
6668 isl_pw_multi_aff_free(pma
);
6669 isl_pw_aff_free(pa
);
6670 return isl_pw_multi_aff_free(res
);
6673 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6675 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6676 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6677 __isl_take isl_pw_aff
*pa
)
6679 isl_bool equal_params
;
6683 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6684 if (equal_params
< 0)
6687 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6688 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6689 isl_pw_aff_check_named_params(pa
) < 0)
6691 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6692 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6693 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6695 isl_pw_multi_aff_free(pma
);
6696 isl_pw_aff_free(pa
);
6700 /* Do the parameters of "pa" match those of "space"?
6702 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6703 __isl_keep isl_space
*space
)
6705 isl_space
*pa_space
;
6709 return isl_bool_error
;
6711 pa_space
= isl_pw_aff_get_space(pa
);
6713 match
= isl_space_has_equal_params(space
, pa_space
);
6715 isl_space_free(pa_space
);
6719 /* Check that the domain space of "pa" matches "space".
6721 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6722 __isl_keep isl_space
*space
)
6724 isl_space
*pa_space
;
6728 return isl_stat_error
;
6730 pa_space
= isl_pw_aff_get_space(pa
);
6732 match
= isl_space_has_equal_params(space
, pa_space
);
6736 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6737 "parameters don't match", goto error
);
6738 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6739 pa_space
, isl_dim_in
);
6743 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6744 "domains don't match", goto error
);
6745 isl_space_free(pa_space
);
6748 isl_space_free(pa_space
);
6749 return isl_stat_error
;
6757 #include <isl_multi_explicit_domain.c>
6758 #include <isl_multi_pw_aff_explicit_domain.c>
6759 #include <isl_multi_templ.c>
6760 #include <isl_multi_un_op_templ.c>
6761 #include <isl_multi_bin_val_templ.c>
6762 #include <isl_multi_add_constant_templ.c>
6763 #include <isl_multi_align_set.c>
6764 #include <isl_multi_apply_set_explicit_domain_templ.c>
6765 #include <isl_multi_arith_templ.c>
6766 #include <isl_multi_bind_templ.c>
6767 #include <isl_multi_bind_domain_templ.c>
6768 #include <isl_multi_coalesce.c>
6769 #include <isl_multi_domain_templ.c>
6770 #include <isl_multi_domain_reverse_templ.c>
6771 #include <isl_multi_dim_id_templ.c>
6772 #include <isl_multi_dims.c>
6773 #include <isl_multi_from_base_templ.c>
6774 #include <isl_multi_check_domain_templ.c>
6775 #include <isl_multi_gist.c>
6776 #include <isl_multi_hash.c>
6777 #include <isl_multi_identity_templ.c>
6778 #include <isl_multi_insert_domain_templ.c>
6779 #include <isl_multi_intersect.c>
6780 #include <isl_multi_min_max_templ.c>
6781 #include <isl_multi_move_dims_templ.c>
6782 #include <isl_multi_nan_templ.c>
6783 #include <isl_multi_param_templ.c>
6784 #include <isl_multi_product_templ.c>
6785 #include <isl_multi_splice_templ.c>
6786 #include <isl_multi_tuple_id_templ.c>
6787 #include <isl_multi_union_add_templ.c>
6788 #include <isl_multi_zero_templ.c>
6789 #include <isl_multi_unbind_params_templ.c>
6791 /* Is every element of "mpa" defined over a single universe domain?
6793 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6795 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6798 /* Given that every element of "mpa" is defined over a single universe domain,
6799 * return the corresponding base expressions.
6801 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6802 __isl_take isl_multi_pw_aff
*mpa
)
6808 n
= isl_multi_pw_aff_size(mpa
);
6810 mpa
= isl_multi_pw_aff_free(mpa
);
6811 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6812 for (i
= 0; i
< n
; ++i
) {
6815 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6816 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6818 isl_multi_pw_aff_free(mpa
);
6822 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6823 * with this explicit domain.
6825 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6826 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6830 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6833 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6834 map
= isl_map_intersect_domain(map
, dom
);
6839 /* Are all elements of "mpa" piecewise constants?
6841 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6843 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6846 /* Does "mpa" have a non-trivial explicit domain?
6848 * The explicit domain, if present, is trivial if it represents
6849 * an (obviously) universe set.
6851 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6852 __isl_keep isl_multi_pw_aff
*mpa
)
6855 return isl_bool_error
;
6856 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6857 return isl_bool_false
;
6858 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6864 #include "isl_opt_mpa_templ.c"
6866 /* Compute the minima of the set dimensions as a function of the
6867 * parameters, but independently of the other set dimensions.
6869 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6871 return set_opt_mpa(set
, &isl_set_dim_min
);
6874 /* Compute the maxima of the set dimensions as a function of the
6875 * parameters, but independently of the other set dimensions.
6877 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6879 return set_opt_mpa(set
, &isl_set_dim_max
);
6885 #include "isl_opt_mpa_templ.c"
6887 /* Compute the minima of the output dimensions as a function of the
6888 * parameters and input dimensions, but independently of
6889 * the other output dimensions.
6891 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6893 return map_opt_mpa(map
, &isl_map_dim_min
);
6896 /* Compute the maxima of the output dimensions as a function of the
6897 * parameters and input dimensions, but independently of
6898 * the other output dimensions.
6900 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6902 return map_opt_mpa(map
, &isl_map_dim_max
);
6906 #define TYPE isl_pw_multi_aff
6907 #include "isl_type_check_match_range_multi_val.c"
6909 /* Apply "fn" to the base expressions of "pma" and "mv".
6911 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6912 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6913 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6914 __isl_take isl_multi_val
*mv
))
6919 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6922 n
= isl_pw_multi_aff_n_piece(pma
);
6926 for (i
= 0; i
< n
; ++i
) {
6929 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6930 ma
= fn(ma
, isl_multi_val_copy(mv
));
6931 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6934 isl_multi_val_free(mv
);
6937 isl_multi_val_free(mv
);
6938 isl_pw_multi_aff_free(pma
);
6942 /* Scale the elements of "pma" by the corresponding elements of "mv".
6944 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6945 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6947 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6948 &isl_multi_aff_scale_multi_val
);
6951 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6953 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
6954 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6956 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6957 &isl_multi_aff_scale_down_multi_val
);
6960 /* This function is called for each entry of an isl_union_pw_multi_aff.
6961 * If the space of the entry matches that of data->mv,
6962 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6963 * Otherwise, return an empty isl_pw_multi_aff.
6965 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6966 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6969 isl_multi_val
*mv
= user
;
6971 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
6973 return isl_pw_multi_aff_free(pma
);
6975 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6976 isl_pw_multi_aff_free(pma
);
6977 return isl_pw_multi_aff_empty(space
);
6980 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6983 /* Scale the elements of "upma" by the corresponding elements of "mv",
6984 * for those entries that match the space of "mv".
6986 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6987 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6989 struct isl_union_pw_multi_aff_transform_control control
= {
6990 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6994 upma
= isl_union_pw_multi_aff_align_params(upma
,
6995 isl_multi_val_get_space(mv
));
6996 mv
= isl_multi_val_align_params(mv
,
6997 isl_union_pw_multi_aff_get_space(upma
));
7001 return isl_union_pw_multi_aff_transform(upma
, &control
);
7003 isl_multi_val_free(mv
);
7006 isl_multi_val_free(mv
);
7007 isl_union_pw_multi_aff_free(upma
);
7011 /* Construct and return a piecewise multi affine expression
7012 * in the given space with value zero in each of the output dimensions and
7013 * a universe domain.
7015 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
7017 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
7020 /* Construct and return a piecewise multi affine expression
7021 * that is equal to the given piecewise affine expression.
7023 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
7024 __isl_take isl_pw_aff
*pa
)
7028 isl_pw_multi_aff
*pma
;
7033 space
= isl_pw_aff_get_space(pa
);
7034 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
7036 for (i
= 0; i
< pa
->n
; ++i
) {
7040 set
= isl_set_copy(pa
->p
[i
].set
);
7041 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
7042 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
7045 isl_pw_aff_free(pa
);
7049 /* Construct and return a piecewise multi affine expression
7050 * that is equal to the given multi piecewise affine expression
7051 * on the shared domain of the piecewise affine expressions,
7052 * in the special case of a 0D multi piecewise affine expression.
7054 * Create a piecewise multi affine expression with the explicit domain of
7055 * the 0D multi piecewise affine expression as domain.
7057 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
7058 __isl_take isl_multi_pw_aff
*mpa
)
7064 space
= isl_multi_pw_aff_get_space(mpa
);
7065 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
7066 isl_multi_pw_aff_free(mpa
);
7068 ma
= isl_multi_aff_zero(space
);
7069 return isl_pw_multi_aff_alloc(dom
, ma
);
7072 /* Construct and return a piecewise multi affine expression
7073 * that is equal to the given multi piecewise affine expression
7074 * on the shared domain of the piecewise affine expressions.
7076 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
7077 __isl_take isl_multi_pw_aff
*mpa
)
7082 isl_pw_multi_aff
*pma
;
7088 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
7090 space
= isl_multi_pw_aff_get_space(mpa
);
7091 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
7092 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7094 for (i
= 1; i
< mpa
->n
; ++i
) {
7095 isl_pw_multi_aff
*pma_i
;
7097 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7098 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
7099 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
7102 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
7104 isl_multi_pw_aff_free(mpa
);
7108 /* Convenience function that constructs an isl_multi_pw_aff
7109 * directly from an isl_aff.
7111 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
7113 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
7116 /* Construct and return a multi piecewise affine expression
7117 * that is equal to the given multi affine expression.
7119 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
7120 __isl_take isl_multi_aff
*ma
)
7124 isl_multi_pw_aff
*mpa
;
7126 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
7128 ma
= isl_multi_aff_free(ma
);
7132 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
7134 for (i
= 0; i
< n
; ++i
) {
7137 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
7138 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7141 isl_multi_aff_free(ma
);
7145 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7146 * but is considered as a function on an isl_multi_aff when exported.
7148 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
7149 __isl_take isl_multi_aff
*ma
)
7151 return isl_multi_pw_aff_from_multi_aff(ma
);
7154 /* Construct and return a multi piecewise affine expression
7155 * that is equal to the given piecewise multi affine expression.
7157 * If the resulting multi piecewise affine expression has
7158 * an explicit domain, then assign it the domain of the input.
7159 * In other cases, the domain is stored in the individual elements.
7161 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
7162 __isl_take isl_pw_multi_aff
*pma
)
7167 isl_multi_pw_aff
*mpa
;
7169 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7171 pma
= isl_pw_multi_aff_free(pma
);
7172 space
= isl_pw_multi_aff_get_space(pma
);
7173 mpa
= isl_multi_pw_aff_alloc(space
);
7175 for (i
= 0; i
< n
; ++i
) {
7178 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
7179 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7181 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7184 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
7185 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
7188 isl_pw_multi_aff_free(pma
);
7192 /* This function performs the same operation as
7193 * isl_multi_pw_aff_from_pw_multi_aff,
7194 * but is considered as a function on an isl_pw_multi_aff when exported.
7196 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
7197 __isl_take isl_pw_multi_aff
*pma
)
7199 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7202 /* Do "pa1" and "pa2" represent the same function?
7204 * We first check if they are obviously equal.
7205 * If not, we convert them to maps and check if those are equal.
7207 * If "pa1" or "pa2" contain any NaNs, then they are considered
7208 * not to be the same. A NaN is not equal to anything, not even
7211 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7212 __isl_keep isl_pw_aff
*pa2
)
7216 isl_map
*map1
, *map2
;
7219 return isl_bool_error
;
7221 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7222 if (equal
< 0 || equal
)
7224 has_nan
= either_involves_nan(pa1
, pa2
);
7226 return isl_bool_error
;
7228 return isl_bool_false
;
7230 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7231 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7232 equal
= isl_map_is_equal(map1
, map2
);
7239 /* Do "mpa1" and "mpa2" represent the same function?
7241 * Note that we cannot convert the entire isl_multi_pw_aff
7242 * to a map because the domains of the piecewise affine expressions
7243 * may not be the same.
7245 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7246 __isl_keep isl_multi_pw_aff
*mpa2
)
7249 isl_bool equal
, equal_params
;
7252 return isl_bool_error
;
7254 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7255 if (equal_params
< 0)
7256 return isl_bool_error
;
7257 if (!equal_params
) {
7258 if (!isl_space_has_named_params(mpa1
->space
))
7259 return isl_bool_false
;
7260 if (!isl_space_has_named_params(mpa2
->space
))
7261 return isl_bool_false
;
7262 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7263 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7264 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7265 isl_multi_pw_aff_get_space(mpa2
));
7266 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7267 isl_multi_pw_aff_get_space(mpa1
));
7268 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7269 isl_multi_pw_aff_free(mpa1
);
7270 isl_multi_pw_aff_free(mpa2
);
7274 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7275 if (equal
< 0 || !equal
)
7278 for (i
= 0; i
< mpa1
->n
; ++i
) {
7279 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7280 if (equal
< 0 || !equal
)
7284 return isl_bool_true
;
7287 /* Do "pma1" and "pma2" represent the same function?
7289 * First check if they are obviously equal.
7290 * If not, then convert them to maps and check if those are equal.
7292 * If "pa1" or "pa2" contain any NaNs, then they are considered
7293 * not to be the same. A NaN is not equal to anything, not even
7296 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7297 __isl_keep isl_pw_multi_aff
*pma2
)
7301 isl_map
*map1
, *map2
;
7304 return isl_bool_error
;
7306 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7307 if (equal
< 0 || equal
)
7309 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7310 if (has_nan
>= 0 && !has_nan
)
7311 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7312 if (has_nan
< 0 || has_nan
)
7313 return isl_bool_not(has_nan
);
7315 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7316 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7317 equal
= isl_map_is_equal(map1
, map2
);
7325 #define BASE multi_aff
7327 #include "isl_multi_pw_aff_pullback_templ.c"
7330 #define BASE pw_multi_aff
7332 #include "isl_multi_pw_aff_pullback_templ.c"
7334 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7335 * with the domain of "aff". The domain of the result is the same
7337 * "mpa" and "aff" are assumed to have been aligned.
7339 * We first extract the parametric constant from "aff", defined
7340 * over the correct domain.
7341 * Then we add the appropriate combinations of the members of "mpa".
7342 * Finally, we add the integer divisions through recursive calls.
7344 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7345 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7348 isl_size n_in
, n_div
, n_mpa_in
;
7354 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7355 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7356 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7357 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7360 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7361 tmp
= isl_aff_copy(aff
);
7362 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7363 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7364 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7365 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7366 pa
= isl_pw_aff_from_aff(tmp
);
7368 for (i
= 0; i
< n_in
; ++i
) {
7371 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7373 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7374 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7375 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7376 pa
= isl_pw_aff_add(pa
, pa_i
);
7379 for (i
= 0; i
< n_div
; ++i
) {
7383 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7385 div
= isl_aff_get_div(aff
, i
);
7386 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7387 isl_multi_pw_aff_copy(mpa
), div
);
7388 pa_i
= isl_pw_aff_floor(pa_i
);
7389 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7390 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7391 pa
= isl_pw_aff_add(pa
, pa_i
);
7394 isl_multi_pw_aff_free(mpa
);
7399 isl_multi_pw_aff_free(mpa
);
7404 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7405 * with the domain of "aff". The domain of the result is the same
7408 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7409 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7411 isl_bool equal_params
;
7415 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7416 if (equal_params
< 0)
7419 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7421 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7422 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7424 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7427 isl_multi_pw_aff_free(mpa
);
7431 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7432 * with the domain of "pa". The domain of the result is the same
7434 * "mpa" and "pa" are assumed to have been aligned.
7436 * We consider each piece in turn. Note that the domains of the
7437 * pieces are assumed to be disjoint and they remain disjoint
7438 * after taking the preimage (over the same function).
7440 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7441 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7450 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7451 isl_pw_aff_get_space(pa
));
7452 res
= isl_pw_aff_empty(space
);
7454 for (i
= 0; i
< pa
->n
; ++i
) {
7458 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7459 isl_multi_pw_aff_copy(mpa
),
7460 isl_aff_copy(pa
->p
[i
].aff
));
7461 domain
= isl_set_copy(pa
->p
[i
].set
);
7462 domain
= isl_set_preimage_multi_pw_aff(domain
,
7463 isl_multi_pw_aff_copy(mpa
));
7464 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7465 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7468 isl_pw_aff_free(pa
);
7469 isl_multi_pw_aff_free(mpa
);
7472 isl_pw_aff_free(pa
);
7473 isl_multi_pw_aff_free(mpa
);
7477 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7478 * with the domain of "pa". The domain of the result is the same
7481 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7482 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7484 isl_bool equal_params
;
7488 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7489 if (equal_params
< 0)
7492 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7494 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7495 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7497 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7499 isl_pw_aff_free(pa
);
7500 isl_multi_pw_aff_free(mpa
);
7504 /* Compute the pullback of "pa" by the function represented by "mpa".
7505 * In other words, plug in "mpa" in "pa".
7507 * The pullback is computed by applying "pa" to "mpa".
7509 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7510 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7512 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7516 #define BASE multi_pw_aff
7518 #include "isl_multi_pw_aff_pullback_templ.c"
7520 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7521 * of "mpa1" and "mpa2" live in the same space, construct map space
7522 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7523 * with this map space as extract argument.
7525 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7526 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7527 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7528 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7531 isl_space
*space1
, *space2
;
7534 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7535 isl_multi_pw_aff_get_space(mpa2
));
7536 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7537 isl_multi_pw_aff_get_space(mpa1
));
7540 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7541 mpa2
->space
, isl_dim_out
);
7545 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7546 "range spaces don't match", goto error
);
7547 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7548 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7549 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7551 res
= order(mpa1
, mpa2
, space1
);
7552 isl_multi_pw_aff_free(mpa1
);
7553 isl_multi_pw_aff_free(mpa2
);
7556 isl_multi_pw_aff_free(mpa1
);
7557 isl_multi_pw_aff_free(mpa2
);
7561 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7562 * where the function values are equal. "space" is the space of the result.
7563 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7565 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7566 * in the sequences are equal.
7568 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7569 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7570 __isl_take isl_space
*space
)
7576 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7578 space
= isl_space_free(space
);
7579 res
= isl_map_universe(space
);
7581 for (i
= 0; i
< n
; ++i
) {
7582 isl_pw_aff
*pa1
, *pa2
;
7585 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7586 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7587 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7588 res
= isl_map_intersect(res
, map
);
7594 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7595 * where the function values are equal.
7597 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7598 __isl_take isl_multi_pw_aff
*mpa2
)
7600 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7601 &isl_multi_pw_aff_eq_map_on_space
);
7604 /* Intersect "map" with the result of applying "order"
7605 * on two copies of "mpa".
7607 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7608 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7609 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7610 __isl_take isl_multi_pw_aff
*mpa2
))
7612 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7615 /* Return the subset of "map" where the domain and the range
7616 * have equal "mpa" values.
7618 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7619 __isl_take isl_multi_pw_aff
*mpa
)
7621 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7622 &isl_multi_pw_aff_eq_map
);
7625 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7626 * where the function values of "mpa1" lexicographically satisfies
7627 * "strict_base"/"base" compared to that of "mpa2".
7628 * "space" is the space of the result.
7629 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7631 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7632 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7633 * when compared to the i-th element of "mpa2" while all previous elements are
7635 * In particular, if i corresponds to the final elements
7636 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7637 * for other values of i.
7638 * If "base" is a strict order, then "base" and "strict_base" are the same.
7640 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7641 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7642 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7643 __isl_take isl_pw_aff
*pa2
),
7644 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7645 __isl_take isl_pw_aff
*pa2
),
7646 __isl_take isl_space
*space
)
7650 isl_map
*res
, *rest
;
7652 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7654 space
= isl_space_free(space
);
7655 res
= isl_map_empty(isl_space_copy(space
));
7656 rest
= isl_map_universe(space
);
7658 for (i
= 0; i
< n
; ++i
) {
7660 isl_pw_aff
*pa1
, *pa2
;
7665 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7666 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7667 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7668 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7669 res
= isl_map_union(res
, map
);
7674 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7675 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7676 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7677 rest
= isl_map_intersect(rest
, map
);
7687 #define STRICT_ORDER lt
7688 #include "isl_aff_lex_templ.c"
7693 #define STRICT_ORDER lt
7694 #include "isl_aff_lex_templ.c"
7699 #define STRICT_ORDER gt
7700 #include "isl_aff_lex_templ.c"
7705 #define STRICT_ORDER gt
7706 #include "isl_aff_lex_templ.c"
7708 /* Compare two isl_affs.
7710 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7711 * than "aff2" and 0 if they are equal.
7713 * The order is fairly arbitrary. We do consider expressions that only involve
7714 * earlier dimensions as "smaller".
7716 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7729 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7733 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7734 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7736 return last1
- last2
;
7738 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7741 /* Compare two isl_pw_affs.
7743 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7744 * than "pa2" and 0 if they are equal.
7746 * The order is fairly arbitrary. We do consider expressions that only involve
7747 * earlier dimensions as "smaller".
7749 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7750 __isl_keep isl_pw_aff
*pa2
)
7763 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7767 if (pa1
->n
!= pa2
->n
)
7768 return pa1
->n
- pa2
->n
;
7770 for (i
= 0; i
< pa1
->n
; ++i
) {
7771 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7774 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7782 /* Return a piecewise affine expression that is equal to "v" on "domain".
7784 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7785 __isl_take isl_val
*v
)
7788 isl_local_space
*ls
;
7791 space
= isl_set_get_space(domain
);
7792 ls
= isl_local_space_from_space(space
);
7793 aff
= isl_aff_val_on_domain(ls
, v
);
7795 return isl_pw_aff_alloc(domain
, aff
);
7798 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7799 * but is considered as a function on an isl_set when exported.
7801 __isl_give isl_pw_aff
*isl_set_pw_aff_on_domain_val(__isl_take isl_set
*domain
,
7802 __isl_take isl_val
*v
)
7804 return isl_pw_aff_val_on_domain(domain
, v
);
7807 /* Return a piecewise affine expression that is equal to the parameter
7808 * with identifier "id" on "domain".
7810 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7811 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7816 space
= isl_set_get_space(domain
);
7817 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7818 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7819 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7821 return isl_pw_aff_alloc(domain
, aff
);
7824 /* This function performs the same operation as
7825 * isl_pw_aff_param_on_domain_id,
7826 * but is considered as a function on an isl_set when exported.
7828 __isl_give isl_pw_aff
*isl_set_param_pw_aff_on_domain_id(
7829 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7831 return isl_pw_aff_param_on_domain_id(domain
, id
);
7834 /* Return a multi affine expression that is equal to "mv" on domain
7837 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7838 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7843 isl_local_space
*ls
;
7846 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7847 if (!space
|| n
< 0)
7850 space2
= isl_multi_val_get_space(mv
);
7851 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7852 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7853 space
= isl_space_map_from_domain_and_range(space
, space2
);
7854 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7855 ls
= isl_local_space_from_space(isl_space_domain(space
));
7856 for (i
= 0; i
< n
; ++i
) {
7860 v
= isl_multi_val_get_val(mv
, i
);
7861 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7862 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7864 isl_local_space_free(ls
);
7866 isl_multi_val_free(mv
);
7869 isl_space_free(space
);
7870 isl_multi_val_free(mv
);
7874 /* This is an alternative name for the function above.
7876 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7877 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7879 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7882 /* This function performs the same operation as
7883 * isl_multi_aff_multi_val_on_domain_space,
7884 * but is considered as a function on an isl_space when exported.
7886 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7887 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7889 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7892 /* Return a piecewise multi-affine expression
7893 * that is equal to "mv" on "domain".
7895 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7896 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7901 space
= isl_set_get_space(domain
);
7902 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7904 return isl_pw_multi_aff_alloc(domain
, ma
);
7907 /* This function performs the same operation as
7908 * isl_pw_multi_aff_multi_val_on_domain,
7909 * but is considered as a function on an isl_set when exported.
7911 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7912 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7914 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7917 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7918 * mv is the value that should be attained on each domain set
7919 * res collects the results
7921 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7923 isl_union_pw_multi_aff
*res
;
7926 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7927 * and add it to data->res.
7929 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7932 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7933 isl_pw_multi_aff
*pma
;
7936 mv
= isl_multi_val_copy(data
->mv
);
7937 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7938 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7940 return data
->res
? isl_stat_ok
: isl_stat_error
;
7943 /* Return a union piecewise multi-affine expression
7944 * that is equal to "mv" on "domain".
7946 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7947 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7949 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7952 space
= isl_union_set_get_space(domain
);
7953 data
.res
= isl_union_pw_multi_aff_empty(space
);
7955 if (isl_union_set_foreach_set(domain
,
7956 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7957 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7958 isl_union_set_free(domain
);
7959 isl_multi_val_free(mv
);
7963 /* Compute the pullback of data->pma by the function represented by "pma2",
7964 * provided the spaces match, and add the results to data->res.
7966 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7968 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7970 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7971 pma2
->dim
, isl_dim_out
)) {
7972 isl_pw_multi_aff_free(pma2
);
7976 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7977 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7979 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7981 return isl_stat_error
;
7986 /* Compute the pullback of "upma1" by the function represented by "upma2".
7988 __isl_give isl_union_pw_multi_aff
*
7989 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7990 __isl_take isl_union_pw_multi_aff
*upma1
,
7991 __isl_take isl_union_pw_multi_aff
*upma2
)
7993 return bin_op(upma1
, upma2
, &pullback_entry
);
7996 /* Apply "upma2" to "upma1".
7998 * That is, compute the pullback of "upma2" by "upma1".
8000 __isl_give isl_union_pw_multi_aff
*
8001 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
8002 __isl_take isl_union_pw_multi_aff
*upma1
,
8003 __isl_take isl_union_pw_multi_aff
*upma2
)
8005 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
8009 #define BASE pw_multi_aff
8011 #include "isl_copy_tuple_id_templ.c"
8013 /* Given a function "pma1" of the form A[B -> C] -> D and
8014 * a function "pma2" of the form E -> B,
8015 * replace the domain of the wrapped relation inside the domain of "pma1"
8016 * by the preimage with respect to "pma2".
8017 * In other words, plug in "pma2" in this nested domain.
8018 * The result is of the form A[E -> C] -> D.
8020 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
8021 * plug that into "pma1".
8023 __isl_give isl_pw_multi_aff
*
8024 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8025 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
8027 isl_space
*pma1_space
, *pma2_space
;
8029 isl_pw_multi_aff
*id
;
8031 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
8032 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8034 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
8036 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
8037 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
8040 space
= isl_space_domain(isl_space_copy(pma1_space
));
8041 space
= isl_space_range(isl_space_unwrap(space
));
8042 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
8043 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8045 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8046 pma1_space
, isl_dim_in
);
8047 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8048 pma1_space
, isl_dim_in
);
8050 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8052 isl_pw_multi_aff_free(pma1
);
8053 isl_pw_multi_aff_free(pma2
);
8057 /* If data->pma and "pma2" are such that
8058 * data->pma is of the form A[B -> C] -> D and
8059 * "pma2" is of the form E -> B,
8060 * then replace the domain of the wrapped relation
8061 * inside the domain of data->pma by the preimage with respect to "pma2" and
8062 * add the result to data->res.
8064 static isl_stat
preimage_domain_wrapped_domain_entry(
8065 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8067 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8068 isl_space
*pma1_space
, *pma2_space
;
8071 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8072 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8074 match
= isl_space_domain_is_wrapping(pma1_space
);
8075 if (match
>= 0 && match
)
8076 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8077 isl_dim_in
, pma2_space
, isl_dim_out
);
8078 if (match
< 0 || !match
) {
8079 isl_pw_multi_aff_free(pma2
);
8080 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8083 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8084 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8086 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8088 return isl_stat_non_null(data
->res
);
8091 /* For each pair of functions A[B -> C] -> D in "upma1" and
8092 * E -> B in "upma2",
8093 * replace the domain of the wrapped relation inside the domain of the first
8094 * by the preimage with respect to the second and collect the results.
8095 * In other words, plug in the second function in this nested domain.
8096 * The results are of the form A[E -> C] -> D.
8098 __isl_give isl_union_pw_multi_aff
*
8099 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8100 __isl_take isl_union_pw_multi_aff
*upma1
,
8101 __isl_take isl_union_pw_multi_aff
*upma2
)
8103 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8106 /* Check that the domain space of "upa" matches "space".
8108 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8109 * can in principle never fail since the space "space" is that
8110 * of the isl_multi_union_pw_aff and is a set space such that
8111 * there is no domain space to match.
8113 * We check the parameters and double-check that "space" is
8114 * indeed that of a set.
8116 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8117 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8119 isl_space
*upa_space
;
8123 return isl_stat_error
;
8125 match
= isl_space_is_set(space
);
8127 return isl_stat_error
;
8129 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8130 "expecting set space", return isl_stat_error
);
8132 upa_space
= isl_union_pw_aff_get_space(upa
);
8133 match
= isl_space_has_equal_params(space
, upa_space
);
8137 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8138 "parameters don't match", goto error
);
8140 isl_space_free(upa_space
);
8143 isl_space_free(upa_space
);
8144 return isl_stat_error
;
8147 /* Do the parameters of "upa" match those of "space"?
8149 static isl_bool
isl_union_pw_aff_matching_params(
8150 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8152 isl_space
*upa_space
;
8156 return isl_bool_error
;
8158 upa_space
= isl_union_pw_aff_get_space(upa
);
8160 match
= isl_space_has_equal_params(space
, upa_space
);
8162 isl_space_free(upa_space
);
8166 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8167 * space represents the new parameters.
8168 * res collects the results.
8170 struct isl_union_pw_aff_reset_params_data
{
8172 isl_union_pw_aff
*res
;
8175 /* Replace the parameters of "pa" by data->space and
8176 * add the result to data->res.
8178 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8180 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8183 space
= isl_pw_aff_get_space(pa
);
8184 space
= isl_space_replace_params(space
, data
->space
);
8185 pa
= isl_pw_aff_reset_space(pa
, space
);
8186 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8188 return data
->res
? isl_stat_ok
: isl_stat_error
;
8191 /* Replace the domain space of "upa" by "space".
8192 * Since a union expression does not have a (single) domain space,
8193 * "space" is necessarily a parameter space.
8195 * Since the order and the names of the parameters determine
8196 * the hash value, we need to create a new hash table.
8198 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8199 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8201 struct isl_union_pw_aff_reset_params_data data
= { space
};
8204 match
= isl_union_pw_aff_matching_params(upa
, space
);
8206 upa
= isl_union_pw_aff_free(upa
);
8208 isl_space_free(space
);
8212 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8213 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8214 data
.res
= isl_union_pw_aff_free(data
.res
);
8216 isl_union_pw_aff_free(upa
);
8217 isl_space_free(space
);
8221 /* Return the floor of "pa".
8223 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8225 return isl_pw_aff_floor(pa
);
8228 /* Given f, return floor(f).
8230 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8231 __isl_take isl_union_pw_aff
*upa
)
8233 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8238 * upa mod m = upa - m * floor(upa/m)
8240 * with m an integer value.
8242 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8243 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8245 isl_union_pw_aff
*res
;
8250 if (!isl_val_is_int(m
))
8251 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8252 "expecting integer modulo", goto error
);
8253 if (!isl_val_is_pos(m
))
8254 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8255 "expecting positive modulo", goto error
);
8257 res
= isl_union_pw_aff_copy(upa
);
8258 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8259 upa
= isl_union_pw_aff_floor(upa
);
8260 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8261 res
= isl_union_pw_aff_sub(res
, upa
);
8266 isl_union_pw_aff_free(upa
);
8270 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8271 * pos is the output position that needs to be extracted.
8272 * res collects the results.
8274 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8276 isl_union_pw_aff
*res
;
8279 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8280 * (assuming it has such a dimension) and add it to data->res.
8282 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8284 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8288 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8290 return isl_stat_error
;
8291 if (data
->pos
>= n_out
) {
8292 isl_pw_multi_aff_free(pma
);
8296 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8297 isl_pw_multi_aff_free(pma
);
8299 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8301 return data
->res
? isl_stat_ok
: isl_stat_error
;
8304 /* Extract an isl_union_pw_aff corresponding to
8305 * output dimension "pos" of "upma".
8307 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8308 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8310 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8317 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8318 "cannot extract at negative position", return NULL
);
8320 space
= isl_union_pw_multi_aff_get_space(upma
);
8321 data
.res
= isl_union_pw_aff_empty(space
);
8323 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8324 &get_union_pw_aff
, &data
) < 0)
8325 data
.res
= isl_union_pw_aff_free(data
.res
);
8330 /* Return a union piecewise affine expression
8331 * that is equal to "aff" on "domain".
8333 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8334 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8338 pa
= isl_pw_aff_from_aff(aff
);
8339 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8342 /* Return a union piecewise affine expression
8343 * that is equal to the parameter identified by "id" on "domain".
8345 * Make sure the parameter appears in the space passed to
8346 * isl_aff_param_on_domain_space_id.
8348 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8349 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8354 space
= isl_union_set_get_space(domain
);
8355 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8356 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8357 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8360 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8361 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8363 * "res" collects the results.
8365 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8367 isl_union_pw_aff
*res
;
8370 /* Construct a piecewise affine expression that is equal to data->pa
8371 * on "domain" and add the result to data->res.
8373 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8375 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8379 pa
= isl_pw_aff_copy(data
->pa
);
8380 dim
= isl_set_dim(domain
, isl_dim_set
);
8382 pa
= isl_pw_aff_free(pa
);
8383 pa
= isl_pw_aff_from_range(pa
);
8384 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8385 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8386 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8387 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8389 return data
->res
? isl_stat_ok
: isl_stat_error
;
8392 /* Return a union piecewise affine expression
8393 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8394 * have been aligned.
8396 * Construct an isl_pw_aff on each of the sets in "domain" and
8397 * collect the results.
8399 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8400 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8402 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8405 space
= isl_union_set_get_space(domain
);
8406 data
.res
= isl_union_pw_aff_empty(space
);
8408 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8409 data
.res
= isl_union_pw_aff_free(data
.res
);
8410 isl_union_set_free(domain
);
8411 isl_pw_aff_free(pa
);
8415 /* Return a union piecewise affine expression
8416 * that is equal to "pa" on "domain".
8418 * Check that "pa" is a parametric expression,
8419 * align the parameters if needed and call
8420 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8422 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8423 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8426 isl_bool equal_params
;
8427 isl_space
*domain_space
, *pa_space
;
8429 pa_space
= isl_pw_aff_peek_space(pa
);
8430 is_set
= isl_space_is_set(pa_space
);
8434 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8435 "expecting parametric expression", goto error
);
8437 domain_space
= isl_union_set_get_space(domain
);
8438 pa_space
= isl_pw_aff_get_space(pa
);
8439 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8440 if (equal_params
>= 0 && !equal_params
) {
8443 space
= isl_space_align_params(domain_space
, pa_space
);
8444 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8445 domain
= isl_union_set_align_params(domain
, space
);
8447 isl_space_free(domain_space
);
8448 isl_space_free(pa_space
);
8451 if (equal_params
< 0)
8453 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8455 isl_union_set_free(domain
);
8456 isl_pw_aff_free(pa
);
8460 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8461 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8462 * "res" collects the results.
8464 struct isl_union_pw_aff_val_on_domain_data
{
8466 isl_union_pw_aff
*res
;
8469 /* Construct a piecewise affine expression that is equal to data->v
8470 * on "domain" and add the result to data->res.
8472 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8474 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8478 v
= isl_val_copy(data
->v
);
8479 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8480 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8482 return data
->res
? isl_stat_ok
: isl_stat_error
;
8485 /* Return a union piecewise affine expression
8486 * that is equal to "v" on "domain".
8488 * Construct an isl_pw_aff on each of the sets in "domain" and
8489 * collect the results.
8491 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8492 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8494 struct isl_union_pw_aff_val_on_domain_data data
;
8497 space
= isl_union_set_get_space(domain
);
8498 data
.res
= isl_union_pw_aff_empty(space
);
8500 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8501 data
.res
= isl_union_pw_aff_free(data
.res
);
8502 isl_union_set_free(domain
);
8507 /* Construct a piecewise multi affine expression
8508 * that is equal to "pa" and add it to upma.
8510 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8513 isl_union_pw_multi_aff
**upma
= user
;
8514 isl_pw_multi_aff
*pma
;
8516 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8517 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8519 return *upma
? isl_stat_ok
: isl_stat_error
;
8522 /* Construct and return a union piecewise multi affine expression
8523 * that is equal to the given union piecewise affine expression.
8525 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8526 __isl_take isl_union_pw_aff
*upa
)
8529 isl_union_pw_multi_aff
*upma
;
8534 space
= isl_union_pw_aff_get_space(upa
);
8535 upma
= isl_union_pw_multi_aff_empty(space
);
8537 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8538 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8539 upma
= isl_union_pw_multi_aff_free(upma
);
8541 isl_union_pw_aff_free(upa
);
8545 /* Compute the set of elements in the domain of "pa" where it is zero and
8546 * add this set to "uset".
8548 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8550 isl_union_set
**uset
= (isl_union_set
**)user
;
8552 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8554 return *uset
? isl_stat_ok
: isl_stat_error
;
8557 /* Return a union set containing those elements in the domain
8558 * of "upa" where it is zero.
8560 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8561 __isl_take isl_union_pw_aff
*upa
)
8563 isl_union_set
*zero
;
8565 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8566 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8567 zero
= isl_union_set_free(zero
);
8569 isl_union_pw_aff_free(upa
);
8573 /* Internal data structure for isl_union_pw_aff_bind_id,
8574 * storing the parameter that needs to be bound and
8575 * the accumulated results.
8577 struct isl_bind_id_data
{
8579 isl_union_set
*bound
;
8582 /* Bind the piecewise affine function "pa" to the parameter data->id,
8583 * adding the resulting elements in the domain where the expression
8584 * is equal to the parameter to data->bound.
8586 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8588 struct isl_bind_id_data
*data
= user
;
8591 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8592 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8594 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8597 /* Bind the union piecewise affine function "upa" to the parameter "id",
8598 * returning the elements in the domain where the expression
8599 * is equal to the parameter.
8601 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8602 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8604 struct isl_bind_id_data data
= { id
};
8606 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8607 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8608 data
.bound
= isl_union_set_free(data
.bound
);
8610 isl_union_pw_aff_free(upa
);
8615 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8616 * upma is the function that is plugged in.
8617 * pa is the current part of the function in which upma is plugged in.
8618 * res collects the results.
8620 struct isl_union_pw_aff_pullback_upma_data
{
8621 isl_union_pw_multi_aff
*upma
;
8623 isl_union_pw_aff
*res
;
8626 /* Check if "pma" can be plugged into data->pa.
8627 * If so, perform the pullback and add the result to data->res.
8629 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8631 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8634 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8635 pma
->dim
, isl_dim_out
)) {
8636 isl_pw_multi_aff_free(pma
);
8640 pa
= isl_pw_aff_copy(data
->pa
);
8641 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8643 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8645 return data
->res
? isl_stat_ok
: isl_stat_error
;
8648 /* Check if any of the elements of data->upma can be plugged into pa,
8649 * add if so add the result to data->res.
8651 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8653 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8657 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8659 isl_pw_aff_free(pa
);
8664 /* Compute the pullback of "upa" by the function represented by "upma".
8665 * In other words, plug in "upma" in "upa". The result contains
8666 * expressions defined over the domain space of "upma".
8668 * Run over all pairs of elements in "upa" and "upma", perform
8669 * the pullback when appropriate and collect the results.
8670 * If the hash value were based on the domain space rather than
8671 * the function space, then we could run through all elements
8672 * of "upma" and directly pick out the corresponding element of "upa".
8674 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8675 __isl_take isl_union_pw_aff
*upa
,
8676 __isl_take isl_union_pw_multi_aff
*upma
)
8678 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8681 space
= isl_union_pw_multi_aff_get_space(upma
);
8682 upa
= isl_union_pw_aff_align_params(upa
, space
);
8683 space
= isl_union_pw_aff_get_space(upa
);
8684 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8690 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8691 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8692 data
.res
= isl_union_pw_aff_free(data
.res
);
8694 isl_union_pw_aff_free(upa
);
8695 isl_union_pw_multi_aff_free(upma
);
8698 isl_union_pw_aff_free(upa
);
8699 isl_union_pw_multi_aff_free(upma
);
8704 #define BASE union_pw_aff
8706 #define DOMBASE union_set
8708 #include <isl_multi_explicit_domain.c>
8709 #include <isl_multi_union_pw_aff_explicit_domain.c>
8710 #include <isl_multi_templ.c>
8711 #include <isl_multi_un_op_templ.c>
8712 #include <isl_multi_bin_val_templ.c>
8713 #include <isl_multi_align_set.c>
8714 #include <isl_multi_align_union_set.c>
8715 #include <isl_multi_apply_set_explicit_domain_templ.c>
8716 #include <isl_multi_apply_union_set_explicit_domain_templ.c>
8717 #include <isl_multi_arith_templ.c>
8718 #include <isl_multi_bind_templ.c>
8719 #include <isl_multi_coalesce.c>
8720 #include <isl_multi_dim_id_templ.c>
8721 #include <isl_multi_floor.c>
8722 #include <isl_multi_from_base_templ.c>
8723 #include <isl_multi_check_domain_templ.c>
8724 #include <isl_multi_gist.c>
8725 #include <isl_multi_intersect.c>
8726 #include <isl_multi_nan_templ.c>
8727 #include <isl_multi_tuple_id_templ.c>
8728 #include <isl_multi_union_add_templ.c>
8729 #include <isl_multi_zero_space_templ.c>
8731 /* Does "mupa" have a non-trivial explicit domain?
8733 * The explicit domain, if present, is trivial if it represents
8734 * an (obviously) universe parameter set.
8736 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8737 __isl_keep isl_multi_union_pw_aff
*mupa
)
8739 isl_bool is_params
, trivial
;
8743 return isl_bool_error
;
8744 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8745 return isl_bool_false
;
8746 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8747 if (is_params
< 0 || !is_params
)
8748 return isl_bool_not(is_params
);
8749 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8750 trivial
= isl_set_plain_is_universe(set
);
8752 return isl_bool_not(trivial
);
8755 /* Construct a multiple union piecewise affine expression
8756 * in the given space with value zero in each of the output dimensions.
8758 * Since there is no canonical zero value for
8759 * a union piecewise affine expression, we can only construct
8760 * a zero-dimensional "zero" value.
8762 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8763 __isl_take isl_space
*space
)
8771 params
= isl_space_is_params(space
);
8775 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8776 "expecting proper set space", goto error
);
8777 if (!isl_space_is_set(space
))
8778 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8779 "expecting set space", goto error
);
8780 dim
= isl_space_dim(space
, isl_dim_out
);
8784 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8785 "expecting 0D space", goto error
);
8787 return isl_multi_union_pw_aff_alloc(space
);
8789 isl_space_free(space
);
8793 /* Construct and return a multi union piecewise affine expression
8794 * that is equal to the given multi affine expression.
8796 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8797 __isl_take isl_multi_aff
*ma
)
8799 isl_multi_pw_aff
*mpa
;
8801 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8802 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8805 /* This function performs the same operation as
8806 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8807 * isl_multi_aff when exported.
8809 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8810 __isl_take isl_multi_aff
*ma
)
8812 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8815 /* Construct and return a multi union piecewise affine expression
8816 * that is equal to the given multi piecewise affine expression.
8818 * If the resulting multi union piecewise affine expression has
8819 * an explicit domain, then assign it the domain of the input.
8820 * In other cases, the domain is stored in the individual elements.
8822 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8823 __isl_take isl_multi_pw_aff
*mpa
)
8828 isl_multi_union_pw_aff
*mupa
;
8830 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8832 mpa
= isl_multi_pw_aff_free(mpa
);
8836 space
= isl_multi_pw_aff_get_space(mpa
);
8837 space
= isl_space_range(space
);
8838 mupa
= isl_multi_union_pw_aff_alloc(space
);
8840 for (i
= 0; i
< n
; ++i
) {
8842 isl_union_pw_aff
*upa
;
8844 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8845 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8846 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8848 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8850 isl_multi_pw_aff
*copy
;
8852 copy
= isl_multi_pw_aff_copy(mpa
);
8853 dom
= isl_union_set_from_set(isl_multi_pw_aff_domain(copy
));
8854 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8857 isl_multi_pw_aff_free(mpa
);
8862 /* Extract the range space of "pma" and assign it to *space.
8863 * If *space has already been set (through a previous call to this function),
8864 * then check that the range space is the same.
8866 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8868 isl_space
**space
= user
;
8869 isl_space
*pma_space
;
8872 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8873 isl_pw_multi_aff_free(pma
);
8876 return isl_stat_error
;
8882 equal
= isl_space_is_equal(pma_space
, *space
);
8883 isl_space_free(pma_space
);
8886 return isl_stat_error
;
8888 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8889 "range spaces not the same", return isl_stat_error
);
8893 /* Construct and return a multi union piecewise affine expression
8894 * that is equal to the given union piecewise multi affine expression.
8896 * In order to be able to perform the conversion, the input
8897 * needs to be non-empty and may only involve a single range space.
8899 * If the resulting multi union piecewise affine expression has
8900 * an explicit domain, then assign it the domain of the input.
8901 * In other cases, the domain is stored in the individual elements.
8903 __isl_give isl_multi_union_pw_aff
*
8904 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8905 __isl_take isl_union_pw_multi_aff
*upma
)
8907 isl_space
*space
= NULL
;
8908 isl_multi_union_pw_aff
*mupa
;
8912 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8916 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8917 "cannot extract range space from empty input",
8919 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8926 n
= isl_space_dim(space
, isl_dim_set
);
8928 space
= isl_space_free(space
);
8929 mupa
= isl_multi_union_pw_aff_alloc(space
);
8931 for (i
= 0; i
< n
; ++i
) {
8932 isl_union_pw_aff
*upa
;
8934 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8935 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8937 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8939 isl_union_pw_multi_aff
*copy
;
8941 copy
= isl_union_pw_multi_aff_copy(upma
);
8942 dom
= isl_union_pw_multi_aff_domain(copy
);
8943 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8946 isl_union_pw_multi_aff_free(upma
);
8949 isl_space_free(space
);
8950 isl_union_pw_multi_aff_free(upma
);
8954 /* This function performs the same operation as
8955 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8956 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8958 __isl_give isl_multi_union_pw_aff
*
8959 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8960 __isl_take isl_union_pw_multi_aff
*upma
)
8962 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8965 /* Try and create an isl_multi_union_pw_aff that is equivalent
8966 * to the given isl_union_map.
8967 * The isl_union_map is required to be single-valued in each space.
8968 * Moreover, it cannot be empty and all range spaces need to be the same.
8969 * Otherwise, an error is produced.
8971 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8972 __isl_take isl_union_map
*umap
)
8974 isl_union_pw_multi_aff
*upma
;
8976 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8977 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8980 /* This function performs the same operation as
8981 * isl_multi_union_pw_aff_from_union_map,
8982 * but is considered as a function on an isl_union_map when exported.
8984 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8985 __isl_take isl_union_map
*umap
)
8987 return isl_multi_union_pw_aff_from_union_map(umap
);
8990 /* Return a multiple union piecewise affine expression
8991 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8992 * have been aligned.
8994 * If the resulting multi union piecewise affine expression has
8995 * an explicit domain, then assign it the input domain.
8996 * In other cases, the domain is stored in the individual elements.
8998 static __isl_give isl_multi_union_pw_aff
*
8999 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9000 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9005 isl_multi_union_pw_aff
*mupa
;
9007 n
= isl_multi_val_dim(mv
, isl_dim_set
);
9008 if (!domain
|| n
< 0)
9011 space
= isl_multi_val_get_space(mv
);
9012 mupa
= isl_multi_union_pw_aff_alloc(space
);
9013 for (i
= 0; i
< n
; ++i
) {
9015 isl_union_pw_aff
*upa
;
9017 v
= isl_multi_val_get_val(mv
, i
);
9018 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
9020 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9022 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9023 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9024 isl_union_set_copy(domain
));
9026 isl_union_set_free(domain
);
9027 isl_multi_val_free(mv
);
9030 isl_union_set_free(domain
);
9031 isl_multi_val_free(mv
);
9035 /* Return a multiple union piecewise affine expression
9036 * that is equal to "mv" on "domain".
9038 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
9039 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9041 isl_bool equal_params
;
9045 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
9046 if (equal_params
< 0)
9049 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9051 domain
= isl_union_set_align_params(domain
,
9052 isl_multi_val_get_space(mv
));
9053 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9054 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9056 isl_union_set_free(domain
);
9057 isl_multi_val_free(mv
);
9061 /* Return a multiple union piecewise affine expression
9062 * that is equal to "ma" on "domain".
9064 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9065 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9067 isl_pw_multi_aff
*pma
;
9069 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9070 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9073 /* Return a multiple union piecewise affine expression
9074 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9075 * have been aligned.
9077 * If the resulting multi union piecewise affine expression has
9078 * an explicit domain, then assign it the input domain.
9079 * In other cases, the domain is stored in the individual elements.
9081 static __isl_give isl_multi_union_pw_aff
*
9082 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9083 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9088 isl_multi_union_pw_aff
*mupa
;
9090 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9091 if (!domain
|| n
< 0)
9093 space
= isl_pw_multi_aff_get_space(pma
);
9094 mupa
= isl_multi_union_pw_aff_alloc(space
);
9095 for (i
= 0; i
< n
; ++i
) {
9097 isl_union_pw_aff
*upa
;
9099 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9100 upa
= isl_union_pw_aff_pw_aff_on_domain(
9101 isl_union_set_copy(domain
), pa
);
9102 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9104 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9105 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9106 isl_union_set_copy(domain
));
9108 isl_union_set_free(domain
);
9109 isl_pw_multi_aff_free(pma
);
9112 isl_union_set_free(domain
);
9113 isl_pw_multi_aff_free(pma
);
9117 /* Return a multiple union piecewise affine expression
9118 * that is equal to "pma" on "domain".
9120 __isl_give isl_multi_union_pw_aff
*
9121 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9122 __isl_take isl_pw_multi_aff
*pma
)
9124 isl_bool equal_params
;
9127 space
= isl_pw_multi_aff_peek_space(pma
);
9128 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9129 if (equal_params
< 0)
9132 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9134 domain
= isl_union_set_align_params(domain
,
9135 isl_pw_multi_aff_get_space(pma
));
9136 pma
= isl_pw_multi_aff_align_params(pma
,
9137 isl_union_set_get_space(domain
));
9138 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9141 isl_union_set_free(domain
);
9142 isl_pw_multi_aff_free(pma
);
9146 /* Return a union set containing those elements in the domains
9147 * of the elements of "mupa" where they are all zero.
9149 * If there are no elements, then simply return the entire domain.
9151 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9152 __isl_take isl_multi_union_pw_aff
*mupa
)
9156 isl_union_pw_aff
*upa
;
9157 isl_union_set
*zero
;
9159 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9161 mupa
= isl_multi_union_pw_aff_free(mupa
);
9166 return isl_multi_union_pw_aff_domain(mupa
);
9168 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9169 zero
= isl_union_pw_aff_zero_union_set(upa
);
9171 for (i
= 1; i
< n
; ++i
) {
9172 isl_union_set
*zero_i
;
9174 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9175 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9177 zero
= isl_union_set_intersect(zero
, zero_i
);
9180 isl_multi_union_pw_aff_free(mupa
);
9184 /* Construct a union map mapping the shared domain
9185 * of the union piecewise affine expressions to the range of "mupa"
9186 * in the special case of a 0D multi union piecewise affine expression.
9188 * Construct a map between the explicit domain of "mupa" and
9190 * Note that this assumes that the domain consists of explicit elements.
9192 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9193 __isl_take isl_multi_union_pw_aff
*mupa
)
9197 isl_union_set
*dom
, *ran
;
9199 space
= isl_multi_union_pw_aff_get_space(mupa
);
9200 dom
= isl_multi_union_pw_aff_domain(mupa
);
9201 ran
= isl_union_set_from_set(isl_set_universe(space
));
9203 is_params
= isl_union_set_is_params(dom
);
9205 dom
= isl_union_set_free(dom
);
9207 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9208 "cannot create union map from expression without "
9209 "explicit domain elements",
9210 dom
= isl_union_set_free(dom
));
9212 return isl_union_map_from_domain_and_range(dom
, ran
);
9215 /* Construct a union map mapping the shared domain
9216 * of the union piecewise affine expressions to the range of "mupa"
9217 * with each dimension in the range equated to the
9218 * corresponding union piecewise affine expression.
9220 * If the input is zero-dimensional, then construct a mapping
9221 * from its explicit domain.
9223 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9224 __isl_take isl_multi_union_pw_aff
*mupa
)
9229 isl_union_map
*umap
;
9230 isl_union_pw_aff
*upa
;
9232 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9234 mupa
= isl_multi_union_pw_aff_free(mupa
);
9239 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9241 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9242 umap
= isl_union_map_from_union_pw_aff(upa
);
9244 for (i
= 1; i
< n
; ++i
) {
9245 isl_union_map
*umap_i
;
9247 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9248 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9249 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9252 space
= isl_multi_union_pw_aff_get_space(mupa
);
9253 umap
= isl_union_map_reset_range_space(umap
, space
);
9255 isl_multi_union_pw_aff_free(mupa
);
9259 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9260 * "range" is the space from which to set the range space.
9261 * "res" collects the results.
9263 struct isl_union_pw_multi_aff_reset_range_space_data
{
9265 isl_union_pw_multi_aff
*res
;
9268 /* Replace the range space of "pma" by the range space of data->range and
9269 * add the result to data->res.
9271 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9273 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9276 space
= isl_pw_multi_aff_get_space(pma
);
9277 space
= isl_space_domain(space
);
9278 space
= isl_space_extend_domain_with_range(space
,
9279 isl_space_copy(data
->range
));
9280 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9281 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9283 return data
->res
? isl_stat_ok
: isl_stat_error
;
9286 /* Replace the range space of all the piecewise affine expressions in "upma" by
9287 * the range space of "space".
9289 * This assumes that all these expressions have the same output dimension.
9291 * Since the spaces of the expressions change, so do their hash values.
9292 * We therefore need to create a new isl_union_pw_multi_aff.
9293 * Note that the hash value is currently computed based on the entire
9294 * space even though there can only be a single expression with a given
9297 static __isl_give isl_union_pw_multi_aff
*
9298 isl_union_pw_multi_aff_reset_range_space(
9299 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9301 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9302 isl_space
*space_upma
;
9304 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9305 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9306 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9307 &reset_range_space
, &data
) < 0)
9308 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9310 isl_space_free(space
);
9311 isl_union_pw_multi_aff_free(upma
);
9315 /* Construct and return a union piecewise multi affine expression
9316 * that is equal to the given multi union piecewise affine expression,
9317 * in the special case of a 0D multi union piecewise affine expression.
9319 * Construct a union piecewise multi affine expression
9320 * on top of the explicit domain of the input.
9322 __isl_give isl_union_pw_multi_aff
*
9323 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9324 __isl_take isl_multi_union_pw_aff
*mupa
)
9328 isl_union_set
*domain
;
9330 space
= isl_multi_union_pw_aff_get_space(mupa
);
9331 mv
= isl_multi_val_zero(space
);
9332 domain
= isl_multi_union_pw_aff_domain(mupa
);
9333 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9336 /* Construct and return a union piecewise multi affine expression
9337 * that is equal to the given multi union piecewise affine expression.
9339 * If the input is zero-dimensional, then
9340 * construct a union piecewise multi affine expression
9341 * on top of the explicit domain of the input.
9343 __isl_give isl_union_pw_multi_aff
*
9344 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9345 __isl_take isl_multi_union_pw_aff
*mupa
)
9350 isl_union_pw_multi_aff
*upma
;
9351 isl_union_pw_aff
*upa
;
9353 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9355 mupa
= isl_multi_union_pw_aff_free(mupa
);
9360 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9362 space
= isl_multi_union_pw_aff_get_space(mupa
);
9363 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9364 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9366 for (i
= 1; i
< n
; ++i
) {
9367 isl_union_pw_multi_aff
*upma_i
;
9369 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9370 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9371 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9374 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9376 isl_multi_union_pw_aff_free(mupa
);
9380 /* Intersect the range of "mupa" with "range",
9381 * in the special case where "mupa" is 0D.
9383 * Intersect the domain of "mupa" with the constraints on the parameters
9386 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9387 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9389 range
= isl_set_params(range
);
9390 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9394 /* Intersect the range of "mupa" with "range".
9395 * That is, keep only those domain elements that have a function value
9398 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9399 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9401 isl_union_pw_multi_aff
*upma
;
9402 isl_union_set
*domain
;
9407 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9408 if (n
< 0 || !range
)
9411 space
= isl_set_get_space(range
);
9412 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9413 space
, isl_dim_set
);
9414 isl_space_free(space
);
9418 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9419 "space don't match", goto error
);
9421 return mupa_intersect_range_0D(mupa
, range
);
9423 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9424 isl_multi_union_pw_aff_copy(mupa
));
9425 domain
= isl_union_set_from_set(range
);
9426 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9427 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9431 isl_multi_union_pw_aff_free(mupa
);
9432 isl_set_free(range
);
9436 /* Return the shared domain of the elements of "mupa",
9437 * in the special case where "mupa" is zero-dimensional.
9439 * Return the explicit domain of "mupa".
9440 * Note that this domain may be a parameter set, either
9441 * because "mupa" is meant to live in a set space or
9442 * because no explicit domain has been set.
9444 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9445 __isl_take isl_multi_union_pw_aff
*mupa
)
9449 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9450 isl_multi_union_pw_aff_free(mupa
);
9455 /* Return the shared domain of the elements of "mupa".
9457 * If "mupa" is zero-dimensional, then return its explicit domain.
9459 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9460 __isl_take isl_multi_union_pw_aff
*mupa
)
9464 isl_union_pw_aff
*upa
;
9467 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9469 mupa
= isl_multi_union_pw_aff_free(mupa
);
9474 return isl_multi_union_pw_aff_domain_0D(mupa
);
9476 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9477 dom
= isl_union_pw_aff_domain(upa
);
9478 for (i
= 1; i
< n
; ++i
) {
9479 isl_union_set
*dom_i
;
9481 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9482 dom_i
= isl_union_pw_aff_domain(upa
);
9483 dom
= isl_union_set_intersect(dom
, dom_i
);
9486 isl_multi_union_pw_aff_free(mupa
);
9490 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9491 * In particular, the spaces have been aligned.
9492 * The result is defined over the shared domain of the elements of "mupa"
9494 * We first extract the parametric constant part of "aff" and
9495 * define that over the shared domain.
9496 * Then we iterate over all input dimensions of "aff" and add the corresponding
9497 * multiples of the elements of "mupa".
9498 * Finally, we consider the integer divisions, calling the function
9499 * recursively to obtain an isl_union_pw_aff corresponding to the
9500 * integer division argument.
9502 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9503 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9506 isl_size n_in
, n_div
;
9507 isl_union_pw_aff
*upa
;
9508 isl_union_set
*uset
;
9512 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9513 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9514 if (n_in
< 0 || n_div
< 0)
9517 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9518 cst
= isl_aff_copy(aff
);
9519 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9520 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9521 cst
= isl_aff_project_domain_on_params(cst
);
9522 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9524 for (i
= 0; i
< n_in
; ++i
) {
9525 isl_union_pw_aff
*upa_i
;
9527 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9529 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9530 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9531 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9532 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9535 for (i
= 0; i
< n_div
; ++i
) {
9537 isl_union_pw_aff
*upa_i
;
9539 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9541 div
= isl_aff_get_div(aff
, i
);
9542 upa_i
= multi_union_pw_aff_apply_aff(
9543 isl_multi_union_pw_aff_copy(mupa
), div
);
9544 upa_i
= isl_union_pw_aff_floor(upa_i
);
9545 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9546 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9547 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9550 isl_multi_union_pw_aff_free(mupa
);
9555 isl_multi_union_pw_aff_free(mupa
);
9560 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9561 * with the domain of "aff".
9562 * Furthermore, the dimension of this space needs to be greater than zero.
9563 * The result is defined over the shared domain of the elements of "mupa"
9565 * We perform these checks and then hand over control to
9566 * multi_union_pw_aff_apply_aff.
9568 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9569 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9572 isl_space
*space1
, *space2
;
9575 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9576 isl_aff_get_space(aff
));
9577 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9581 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9582 space2
= isl_aff_get_domain_space(aff
);
9583 equal
= isl_space_is_equal(space1
, space2
);
9584 isl_space_free(space1
);
9585 isl_space_free(space2
);
9589 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9590 "spaces don't match", goto error
);
9591 dim
= isl_aff_dim(aff
, isl_dim_in
);
9595 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9596 "cannot determine domains", goto error
);
9598 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9600 isl_multi_union_pw_aff_free(mupa
);
9605 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9606 * The space of "mupa" is known to be compatible with the domain of "ma".
9608 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9609 * on the domain of "mupa".
9611 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9612 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9616 dom
= isl_multi_union_pw_aff_domain(mupa
);
9617 ma
= isl_multi_aff_project_domain_on_params(ma
);
9619 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9622 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9623 * with the domain of "ma".
9624 * The result is defined over the shared domain of the elements of "mupa"
9626 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9627 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9629 isl_space
*space1
, *space2
;
9630 isl_multi_union_pw_aff
*res
;
9633 isl_size n_in
, n_out
;
9635 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9636 isl_multi_aff_get_space(ma
));
9637 ma
= isl_multi_aff_align_params(ma
,
9638 isl_multi_union_pw_aff_get_space(mupa
));
9639 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9640 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9641 if (!mupa
|| n_in
< 0 || n_out
< 0)
9644 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9645 space2
= isl_multi_aff_get_domain_space(ma
);
9646 equal
= isl_space_is_equal(space1
, space2
);
9647 isl_space_free(space1
);
9648 isl_space_free(space2
);
9652 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9653 "spaces don't match", goto error
);
9655 return mupa_apply_multi_aff_0D(mupa
, ma
);
9657 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9658 res
= isl_multi_union_pw_aff_alloc(space1
);
9660 for (i
= 0; i
< n_out
; ++i
) {
9662 isl_union_pw_aff
*upa
;
9664 aff
= isl_multi_aff_get_aff(ma
, i
);
9665 upa
= multi_union_pw_aff_apply_aff(
9666 isl_multi_union_pw_aff_copy(mupa
), aff
);
9667 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9670 isl_multi_aff_free(ma
);
9671 isl_multi_union_pw_aff_free(mupa
);
9674 isl_multi_union_pw_aff_free(mupa
);
9675 isl_multi_aff_free(ma
);
9679 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9680 * The space of "mupa" is known to be compatible with the domain of "pa".
9682 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9683 * on the domain of "mupa".
9685 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9686 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9690 dom
= isl_multi_union_pw_aff_domain(mupa
);
9691 pa
= isl_pw_aff_project_domain_on_params(pa
);
9693 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9696 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9697 * with the domain of "pa".
9698 * Furthermore, the dimension of this space needs to be greater than zero.
9699 * The result is defined over the shared domain of the elements of "mupa"
9701 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9702 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9707 isl_space
*space
, *space2
;
9708 isl_union_pw_aff
*upa
;
9710 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9711 isl_pw_aff_get_space(pa
));
9712 pa
= isl_pw_aff_align_params(pa
,
9713 isl_multi_union_pw_aff_get_space(mupa
));
9717 space
= isl_multi_union_pw_aff_get_space(mupa
);
9718 space2
= isl_pw_aff_get_domain_space(pa
);
9719 equal
= isl_space_is_equal(space
, space2
);
9720 isl_space_free(space
);
9721 isl_space_free(space2
);
9725 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9726 "spaces don't match", goto error
);
9727 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9731 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9733 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9734 upa
= isl_union_pw_aff_empty(space
);
9736 for (i
= 0; i
< pa
->n
; ++i
) {
9739 isl_multi_union_pw_aff
*mupa_i
;
9740 isl_union_pw_aff
*upa_i
;
9742 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9743 domain
= isl_set_copy(pa
->p
[i
].set
);
9744 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9745 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9746 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9747 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9750 isl_multi_union_pw_aff_free(mupa
);
9751 isl_pw_aff_free(pa
);
9754 isl_multi_union_pw_aff_free(mupa
);
9755 isl_pw_aff_free(pa
);
9759 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9760 * The space of "mupa" is known to be compatible with the domain of "pma".
9762 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9763 * on the domain of "mupa".
9765 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9766 __isl_take isl_multi_union_pw_aff
*mupa
,
9767 __isl_take isl_pw_multi_aff
*pma
)
9771 dom
= isl_multi_union_pw_aff_domain(mupa
);
9772 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9774 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9777 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9778 * with the domain of "pma".
9779 * The result is defined over the shared domain of the elements of "mupa"
9781 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9782 __isl_take isl_multi_union_pw_aff
*mupa
,
9783 __isl_take isl_pw_multi_aff
*pma
)
9785 isl_space
*space1
, *space2
;
9786 isl_multi_union_pw_aff
*res
;
9789 isl_size n_in
, n_out
;
9791 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9792 isl_pw_multi_aff_get_space(pma
));
9793 pma
= isl_pw_multi_aff_align_params(pma
,
9794 isl_multi_union_pw_aff_get_space(mupa
));
9798 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9799 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9800 equal
= isl_space_is_equal(space1
, space2
);
9801 isl_space_free(space1
);
9802 isl_space_free(space2
);
9806 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9807 "spaces don't match", goto error
);
9808 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9809 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9810 if (n_in
< 0 || n_out
< 0)
9813 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9815 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9816 res
= isl_multi_union_pw_aff_alloc(space1
);
9818 for (i
= 0; i
< n_out
; ++i
) {
9820 isl_union_pw_aff
*upa
;
9822 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9823 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9824 isl_multi_union_pw_aff_copy(mupa
), pa
);
9825 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9828 isl_pw_multi_aff_free(pma
);
9829 isl_multi_union_pw_aff_free(mupa
);
9832 isl_multi_union_pw_aff_free(mupa
);
9833 isl_pw_multi_aff_free(pma
);
9837 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9838 * If the explicit domain only keeps track of constraints on the parameters,
9839 * then only update those constraints.
9841 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9842 __isl_take isl_multi_union_pw_aff
*mupa
,
9843 __isl_keep isl_union_pw_multi_aff
*upma
)
9847 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9848 return isl_multi_union_pw_aff_free(mupa
);
9850 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9854 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9856 return isl_multi_union_pw_aff_free(mupa
);
9858 upma
= isl_union_pw_multi_aff_copy(upma
);
9860 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9861 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9863 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9866 return isl_multi_union_pw_aff_free(mupa
);
9870 /* Compute the pullback of "mupa" by the function represented by "upma".
9871 * In other words, plug in "upma" in "mupa". The result contains
9872 * expressions defined over the domain space of "upma".
9874 * Run over all elements of "mupa" and plug in "upma" in each of them.
9876 * If "mupa" has an explicit domain, then it is this domain
9877 * that needs to undergo a pullback instead, i.e., a preimage.
9879 __isl_give isl_multi_union_pw_aff
*
9880 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9881 __isl_take isl_multi_union_pw_aff
*mupa
,
9882 __isl_take isl_union_pw_multi_aff
*upma
)
9887 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9888 isl_union_pw_multi_aff_get_space(upma
));
9889 upma
= isl_union_pw_multi_aff_align_params(upma
,
9890 isl_multi_union_pw_aff_get_space(mupa
));
9891 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9892 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9896 for (i
= 0; i
< n
; ++i
) {
9897 isl_union_pw_aff
*upa
;
9899 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9900 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9901 isl_union_pw_multi_aff_copy(upma
));
9902 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9905 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9906 mupa
= preimage_explicit_domain(mupa
, upma
);
9908 isl_union_pw_multi_aff_free(upma
);
9911 isl_multi_union_pw_aff_free(mupa
);
9912 isl_union_pw_multi_aff_free(upma
);
9916 /* Extract the sequence of elements in "mupa" with domain space "space"
9917 * (ignoring parameters).
9919 * For the elements of "mupa" that are not defined on the specified space,
9920 * the corresponding element in the result is empty.
9922 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9923 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9927 isl_space
*space_mpa
;
9928 isl_multi_pw_aff
*mpa
;
9930 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9931 if (n
< 0 || !space
)
9934 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9935 space
= isl_space_replace_params(space
, space_mpa
);
9936 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9938 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9940 space
= isl_space_from_domain(space
);
9941 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9942 for (i
= 0; i
< n
; ++i
) {
9943 isl_union_pw_aff
*upa
;
9946 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9947 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9948 isl_space_copy(space
));
9949 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9950 isl_union_pw_aff_free(upa
);
9953 isl_space_free(space
);
9956 isl_space_free(space
);
9960 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9961 * should modify the base expressions in the input.
9963 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9964 * are taken into account.
9965 * "fn" is applied to each entry in the input.
9967 struct isl_union_pw_multi_aff_un_op_control
{
9968 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9969 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9972 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9973 * a second argument) for use as an isl_union_pw_multi_aff_transform
9974 * filter function (which does take a second argument).
9975 * Simply call control->filter without the second argument.
9977 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9978 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9980 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9982 return control
->filter(pma
);
9985 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9986 * a second argument) for use as an isl_union_pw_multi_aff_transform
9987 * base function (which does take a second argument).
9988 * Simply call control->fn without the second argument.
9990 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9991 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9993 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9995 return control
->fn(pma
);
9998 /* Construct an isl_union_pw_multi_aff that is obtained by
9999 * modifying "upma" according to "control".
10001 * isl_union_pw_multi_aff_transform performs essentially
10002 * the same operation, but takes a filter and a callback function
10003 * of a different form (with an extra argument).
10004 * Call isl_union_pw_multi_aff_transform with wrappers
10005 * that remove this extra argument.
10007 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
10008 __isl_take isl_union_pw_multi_aff
*upma
,
10009 struct isl_union_pw_multi_aff_un_op_control
*control
)
10011 struct isl_union_pw_multi_aff_transform_control t_control
= {
10012 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
10013 .filter_user
= control
,
10014 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
10015 .fn_user
= control
,
10018 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
10021 /* For each function in "upma" of the form A -> [B -> C],
10022 * extract the function A -> B and collect the results.
10024 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
10025 __isl_take isl_union_pw_multi_aff
*upma
)
10027 struct isl_union_pw_multi_aff_un_op_control control
= {
10028 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10029 .fn
= &isl_pw_multi_aff_range_factor_domain
,
10031 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10034 /* For each function in "upma" of the form A -> [B -> C],
10035 * extract the function A -> C and collect the results.
10037 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
10038 __isl_take isl_union_pw_multi_aff
*upma
)
10040 struct isl_union_pw_multi_aff_un_op_control control
= {
10041 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10042 .fn
= &isl_pw_multi_aff_range_factor_range
,
10044 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10047 /* Evaluate the affine function "aff" in the void point "pnt".
10048 * In particular, return the value NaN.
10050 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10051 __isl_take isl_point
*pnt
)
10055 ctx
= isl_point_get_ctx(pnt
);
10057 isl_point_free(pnt
);
10058 return isl_val_nan(ctx
);
10061 /* Evaluate the affine expression "aff"
10062 * in the coordinates (with denominator) "pnt".
10064 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10065 __isl_keep isl_vec
*pnt
)
10074 ctx
= isl_vec_get_ctx(aff
);
10077 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10078 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10079 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10080 v
= isl_val_normalize(v
);
10087 /* Check that the domain space of "aff" is equal to "space".
10089 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10090 __isl_keep isl_space
*space
)
10094 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10096 return isl_stat_error
;
10098 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10099 "incompatible spaces", return isl_stat_error
);
10100 return isl_stat_ok
;
10103 /* Evaluate the affine function "aff" in "pnt".
10105 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10106 __isl_take isl_point
*pnt
)
10110 isl_local_space
*ls
;
10112 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10114 is_void
= isl_point_is_void(pnt
);
10118 return eval_void(aff
, pnt
);
10120 ls
= isl_aff_get_domain_local_space(aff
);
10121 pnt
= isl_local_space_lift_point(ls
, pnt
);
10123 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10126 isl_point_free(pnt
);
10131 isl_point_free(pnt
);