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 variable of type "type" within
521 * the variables of the domain of "aff".
523 static isl_size
isl_aff_domain_var_offset(__isl_keep isl_aff
*aff
,
524 enum isl_dim_type type
)
528 ls
= isl_aff_peek_domain_local_space(aff
);
529 return isl_local_space_var_offset(ls
, type
);
532 /* Return the offset of the first coefficient of type "type" in
533 * the domain of "aff".
535 isl_size
isl_aff_domain_offset(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
539 offset
= isl_aff_domain_var_offset(aff
, type
);
541 return isl_size_error
;
545 /* Return the position of the dimension of the given type and name
547 * Return -1 if no such dimension can be found.
549 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
554 if (type
== isl_dim_out
)
556 if (type
== isl_dim_in
)
558 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
561 /* Return the domain space of "aff".
563 static __isl_keep isl_space
*isl_aff_peek_domain_space(__isl_keep isl_aff
*aff
)
565 return aff
? isl_local_space_peek_space(aff
->ls
) : NULL
;
568 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
570 return isl_space_copy(isl_aff_peek_domain_space(aff
));
573 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
578 space
= isl_local_space_get_space(aff
->ls
);
579 space
= isl_space_from_domain(space
);
580 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
584 /* Return a copy of the domain space of "aff".
586 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
587 __isl_keep isl_aff
*aff
)
589 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff
));
592 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
597 ls
= isl_local_space_copy(aff
->ls
);
598 ls
= isl_local_space_from_domain(ls
);
599 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
603 /* Return the local space of the domain of "aff".
604 * This may be either a copy or the local space itself
605 * if there is only one reference to "aff".
606 * This allows the local space to be modified inplace
607 * if both the expression and its local space have only a single reference.
608 * The caller is not allowed to modify "aff" between this call and
609 * a subsequent call to isl_aff_restore_domain_local_space.
610 * The only exception is that isl_aff_free can be called instead.
612 __isl_give isl_local_space
*isl_aff_take_domain_local_space(
613 __isl_keep isl_aff
*aff
)
620 return isl_aff_get_domain_local_space(aff
);
626 /* Set the local space of the domain of "aff" to "ls",
627 * where the local space of "aff" may be missing
628 * due to a preceding call to isl_aff_take_domain_local_space.
629 * However, in this case, "aff" only has a single reference and
630 * then the call to isl_aff_cow has no effect.
632 __isl_give isl_aff
*isl_aff_restore_domain_local_space(
633 __isl_keep isl_aff
*aff
, __isl_take isl_local_space
*ls
)
639 isl_local_space_free(ls
);
643 aff
= isl_aff_cow(aff
);
646 isl_local_space_free(aff
->ls
);
652 isl_local_space_free(ls
);
656 /* Externally, an isl_aff has a map space, but internally, the
657 * ls field corresponds to the domain of that space.
659 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
660 enum isl_dim_type type
, unsigned pos
)
664 if (type
== isl_dim_out
)
666 if (type
== isl_dim_in
)
668 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
671 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
672 __isl_take isl_space
*space
)
674 aff
= isl_aff_cow(aff
);
678 aff
->ls
= isl_local_space_reset_space(aff
->ls
, space
);
680 return isl_aff_free(aff
);
685 isl_space_free(space
);
689 /* Reset the space of "aff". This function is called from isl_pw_templ.c
690 * and doesn't know if the space of an element object is represented
691 * directly or through its domain. It therefore passes along both.
693 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
694 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
696 isl_space_free(space
);
697 return isl_aff_reset_domain_space(aff
, domain
);
700 /* Reorder the dimensions of the domain of "aff" according
701 * to the given reordering.
703 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
704 __isl_take isl_reordering
*r
)
706 aff
= isl_aff_cow(aff
);
710 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
711 aff
->v
= isl_vec_reorder(aff
->v
, 2, isl_reordering_copy(r
));
712 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
714 if (!aff
->v
|| !aff
->ls
)
715 return isl_aff_free(aff
);
720 isl_reordering_free(r
);
724 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
725 __isl_take isl_space
*model
)
727 isl_space
*domain_space
;
728 isl_bool equal_params
;
730 domain_space
= isl_aff_peek_domain_space(aff
);
731 equal_params
= isl_space_has_equal_params(domain_space
, model
);
732 if (equal_params
< 0)
737 exp
= isl_parameter_alignment_reordering(domain_space
, model
);
738 aff
= isl_aff_realign_domain(aff
, exp
);
741 isl_space_free(model
);
744 isl_space_free(model
);
751 #include "isl_unbind_params_templ.c"
753 /* Is "aff" obviously equal to zero?
755 * If the denominator is zero, then "aff" is not equal to zero.
757 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
762 return isl_bool_error
;
764 if (isl_int_is_zero(aff
->v
->el
[0]))
765 return isl_bool_false
;
766 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1);
767 return isl_bool_ok(pos
< 0);
770 /* Does "aff" represent NaN?
772 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
775 return isl_bool_error
;
777 return isl_bool_ok(isl_seq_first_non_zero(aff
->v
->el
, 2) < 0);
780 /* Are "aff1" and "aff2" obviously equal?
782 * NaN is not equal to anything, not even to another NaN.
784 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
785 __isl_keep isl_aff
*aff2
)
790 return isl_bool_error
;
792 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
793 return isl_bool_false
;
795 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
796 if (equal
< 0 || !equal
)
799 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
802 /* Return the common denominator of "aff" in "v".
804 * We cannot return anything meaningful in case of a NaN.
806 isl_stat
isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
809 return isl_stat_error
;
810 if (isl_aff_is_nan(aff
))
811 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
812 "cannot get denominator of NaN", return isl_stat_error
);
813 isl_int_set(*v
, aff
->v
->el
[0]);
817 /* Return the common denominator of "aff".
819 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
826 ctx
= isl_aff_get_ctx(aff
);
827 if (isl_aff_is_nan(aff
))
828 return isl_val_nan(ctx
);
829 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
832 /* Return the constant term of "aff".
834 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
842 ctx
= isl_aff_get_ctx(aff
);
843 if (isl_aff_is_nan(aff
))
844 return isl_val_nan(ctx
);
845 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
846 return isl_val_normalize(v
);
849 /* Return the coefficient of the variable of type "type" at position "pos"
852 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
853 enum isl_dim_type type
, int pos
)
861 ctx
= isl_aff_get_ctx(aff
);
862 if (type
== isl_dim_out
)
863 isl_die(ctx
, isl_error_invalid
,
864 "output/set dimension does not have a coefficient",
866 if (type
== isl_dim_in
)
869 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
872 if (isl_aff_is_nan(aff
))
873 return isl_val_nan(ctx
);
874 pos
+= isl_local_space_offset(aff
->ls
, type
);
875 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
876 return isl_val_normalize(v
);
879 /* Return the sign of the coefficient of the variable of type "type"
880 * at position "pos" of "aff".
882 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
890 ctx
= isl_aff_get_ctx(aff
);
891 if (type
== isl_dim_out
)
892 isl_die(ctx
, isl_error_invalid
,
893 "output/set dimension does not have a coefficient",
895 if (type
== isl_dim_in
)
898 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
901 pos
+= isl_local_space_offset(aff
->ls
, type
);
902 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
905 /* Replace the numerator of the constant term of "aff" by "v".
907 * A NaN is unaffected by this operation.
909 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
913 if (isl_aff_is_nan(aff
))
915 aff
= isl_aff_cow(aff
);
919 aff
->v
= isl_vec_cow(aff
->v
);
921 return isl_aff_free(aff
);
923 isl_int_set(aff
->v
->el
[1], v
);
928 /* Replace the constant term of "aff" by "v".
930 * A NaN is unaffected by this operation.
932 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
933 __isl_take isl_val
*v
)
938 if (isl_aff_is_nan(aff
)) {
943 if (!isl_val_is_rat(v
))
944 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
945 "expecting rational value", goto error
);
947 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
948 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
953 aff
= isl_aff_cow(aff
);
956 aff
->v
= isl_vec_cow(aff
->v
);
960 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
961 isl_int_set(aff
->v
->el
[1], v
->n
);
962 } else if (isl_int_is_one(v
->d
)) {
963 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
965 isl_seq_scale(aff
->v
->el
+ 1,
966 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
967 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
968 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
969 aff
->v
= isl_vec_normalize(aff
->v
);
982 /* Add "v" to the constant term of "aff".
984 * A NaN is unaffected by this operation.
986 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
988 if (isl_int_is_zero(v
))
993 if (isl_aff_is_nan(aff
))
995 aff
= isl_aff_cow(aff
);
999 aff
->v
= isl_vec_cow(aff
->v
);
1001 return isl_aff_free(aff
);
1003 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
1008 /* Add "v" to the constant term of "aff",
1009 * in case "aff" is a rational expression.
1011 static __isl_give isl_aff
*isl_aff_add_rat_constant_val(__isl_take isl_aff
*aff
,
1012 __isl_take isl_val
*v
)
1014 aff
= isl_aff_cow(aff
);
1018 aff
->v
= isl_vec_cow(aff
->v
);
1022 if (isl_int_is_one(v
->d
)) {
1023 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
1024 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1025 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
1026 aff
->v
= isl_vec_normalize(aff
->v
);
1030 isl_seq_scale(aff
->v
->el
+ 1,
1031 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1032 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
1033 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1034 aff
->v
= isl_vec_normalize(aff
->v
);
1047 /* Return the first argument and free the second.
1049 static __isl_give isl_aff
*pick_free(__isl_take isl_aff
*aff
,
1050 __isl_take isl_val
*v
)
1056 /* Replace the first argument by NaN and free the second argument.
1058 static __isl_give isl_aff
*set_nan_free_val(__isl_take isl_aff
*aff
,
1059 __isl_take isl_val
*v
)
1062 return isl_aff_set_nan(aff
);
1065 /* Add "v" to the constant term of "aff".
1067 * A NaN is unaffected by this operation.
1068 * Conversely, adding a NaN turns "aff" into a NaN.
1070 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
1071 __isl_take isl_val
*v
)
1073 isl_bool is_nan
, is_zero
, is_rat
;
1075 is_nan
= isl_aff_is_nan(aff
);
1076 is_zero
= isl_val_is_zero(v
);
1077 if (is_nan
< 0 || is_zero
< 0)
1079 if (is_nan
|| is_zero
)
1080 return pick_free(aff
, v
);
1082 is_nan
= isl_val_is_nan(v
);
1083 is_rat
= isl_val_is_rat(v
);
1084 if (is_nan
< 0 || is_rat
< 0)
1087 return set_nan_free_val(aff
, v
);
1089 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1090 "expecting rational value or NaN", goto error
);
1092 return isl_aff_add_rat_constant_val(aff
, v
);
1099 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
1104 isl_int_set_si(t
, v
);
1105 aff
= isl_aff_add_constant(aff
, t
);
1111 /* Add "v" to the numerator of the constant term of "aff".
1113 * A NaN is unaffected by this operation.
1115 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
1117 if (isl_int_is_zero(v
))
1122 if (isl_aff_is_nan(aff
))
1124 aff
= isl_aff_cow(aff
);
1128 aff
->v
= isl_vec_cow(aff
->v
);
1130 return isl_aff_free(aff
);
1132 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
1137 /* Add "v" to the numerator of the constant term of "aff".
1139 * A NaN is unaffected by this operation.
1141 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
1149 isl_int_set_si(t
, v
);
1150 aff
= isl_aff_add_constant_num(aff
, t
);
1156 /* Replace the numerator of the constant term of "aff" by "v".
1158 * A NaN is unaffected by this operation.
1160 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
1164 if (isl_aff_is_nan(aff
))
1166 aff
= isl_aff_cow(aff
);
1170 aff
->v
= isl_vec_cow(aff
->v
);
1172 return isl_aff_free(aff
);
1174 isl_int_set_si(aff
->v
->el
[1], v
);
1179 /* Replace the numerator of the coefficient of the variable of type "type"
1180 * at position "pos" of "aff" by "v".
1182 * A NaN is unaffected by this operation.
1184 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
1185 enum isl_dim_type type
, int pos
, isl_int v
)
1190 if (type
== isl_dim_out
)
1191 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1192 "output/set dimension does not have a coefficient",
1193 return isl_aff_free(aff
));
1194 if (type
== isl_dim_in
)
1197 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1198 return isl_aff_free(aff
);
1200 if (isl_aff_is_nan(aff
))
1202 aff
= isl_aff_cow(aff
);
1206 aff
->v
= isl_vec_cow(aff
->v
);
1208 return isl_aff_free(aff
);
1210 pos
+= isl_local_space_offset(aff
->ls
, type
);
1211 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1216 /* Replace the numerator of the coefficient of the variable of type "type"
1217 * at position "pos" of "aff" by "v".
1219 * A NaN is unaffected by this operation.
1221 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1222 enum isl_dim_type type
, int pos
, int v
)
1227 if (type
== isl_dim_out
)
1228 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1229 "output/set dimension does not have a coefficient",
1230 return isl_aff_free(aff
));
1231 if (type
== isl_dim_in
)
1234 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1235 return isl_aff_free(aff
);
1237 if (isl_aff_is_nan(aff
))
1239 pos
+= isl_local_space_offset(aff
->ls
, type
);
1240 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1243 aff
= isl_aff_cow(aff
);
1247 aff
->v
= isl_vec_cow(aff
->v
);
1249 return isl_aff_free(aff
);
1251 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1256 /* Replace the coefficient of the variable of type "type" at position "pos"
1259 * A NaN is unaffected by this operation.
1261 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1262 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1267 if (type
== isl_dim_out
)
1268 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1269 "output/set dimension does not have a coefficient",
1271 if (type
== isl_dim_in
)
1274 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1275 return isl_aff_free(aff
);
1277 if (isl_aff_is_nan(aff
)) {
1281 if (!isl_val_is_rat(v
))
1282 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1283 "expecting rational value", goto error
);
1285 pos
+= isl_local_space_offset(aff
->ls
, type
);
1286 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1287 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1292 aff
= isl_aff_cow(aff
);
1295 aff
->v
= isl_vec_cow(aff
->v
);
1299 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1300 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1301 } else if (isl_int_is_one(v
->d
)) {
1302 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1304 isl_seq_scale(aff
->v
->el
+ 1,
1305 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1306 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1307 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1308 aff
->v
= isl_vec_normalize(aff
->v
);
1321 /* Add "v" to the coefficient of the variable of type "type"
1322 * at position "pos" of "aff".
1324 * A NaN is unaffected by this operation.
1326 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1327 enum isl_dim_type type
, int pos
, isl_int v
)
1332 if (type
== isl_dim_out
)
1333 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1334 "output/set dimension does not have a coefficient",
1335 return isl_aff_free(aff
));
1336 if (type
== isl_dim_in
)
1339 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1340 return isl_aff_free(aff
);
1342 if (isl_aff_is_nan(aff
))
1344 aff
= isl_aff_cow(aff
);
1348 aff
->v
= isl_vec_cow(aff
->v
);
1350 return isl_aff_free(aff
);
1352 pos
+= isl_local_space_offset(aff
->ls
, type
);
1353 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1358 /* Add "v" to the coefficient of the variable of type "type"
1359 * at position "pos" of "aff".
1361 * A NaN is unaffected by this operation.
1363 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1364 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1369 if (isl_val_is_zero(v
)) {
1374 if (type
== isl_dim_out
)
1375 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1376 "output/set dimension does not have a coefficient",
1378 if (type
== isl_dim_in
)
1381 if (isl_local_space_check_range(aff
->ls
, type
, pos
, 1) < 0)
1384 if (isl_aff_is_nan(aff
)) {
1388 if (!isl_val_is_rat(v
))
1389 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1390 "expecting rational value", goto error
);
1392 aff
= isl_aff_cow(aff
);
1396 aff
->v
= isl_vec_cow(aff
->v
);
1400 pos
+= isl_local_space_offset(aff
->ls
, type
);
1401 if (isl_int_is_one(v
->d
)) {
1402 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1403 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1404 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1405 aff
->v
= isl_vec_normalize(aff
->v
);
1409 isl_seq_scale(aff
->v
->el
+ 1,
1410 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1411 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1412 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1413 aff
->v
= isl_vec_normalize(aff
->v
);
1426 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1427 enum isl_dim_type type
, int pos
, int v
)
1432 isl_int_set_si(t
, v
);
1433 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1439 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1444 return isl_local_space_get_div(aff
->ls
, pos
);
1447 /* Return the negation of "aff".
1449 * As a special case, -NaN = NaN.
1451 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1455 if (isl_aff_is_nan(aff
))
1457 aff
= isl_aff_cow(aff
);
1460 aff
->v
= isl_vec_cow(aff
->v
);
1462 return isl_aff_free(aff
);
1464 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1469 /* Remove divs from the local space that do not appear in the affine
1472 * First remove any unused local variables at the end.
1473 * Then look for other unused local variables. These need some extra care
1474 * because a local variable that does not appear in the affine expression
1475 * may still appear in the definition of some later local variable.
1477 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1483 isl_local_space
*ls
;
1485 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1486 v_div
= isl_aff_domain_var_offset(aff
, isl_dim_div
);
1487 if (n
< 0 || v_div
< 0)
1488 return isl_aff_free(aff
);
1490 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + 1 + v_div
, n
) + 1;
1492 aff
= isl_aff_drop_dims(aff
, isl_dim_div
, pos
, n
- pos
);
1493 if (pos
<= 1 || !aff
)
1496 ls
= isl_aff_peek_domain_local_space(aff
);
1497 active
= isl_local_space_get_active(ls
, aff
->v
->el
+ 2);
1499 return isl_aff_free(aff
);
1500 for (pos
= pos
- 2; pos
>= 0; pos
--) {
1501 if (active
[v_div
+ pos
])
1503 aff
= isl_aff_drop_dims(aff
, isl_dim_div
, pos
, 1);
1510 /* Look for any divs in the aff->ls with a denominator equal to one
1511 * and plug them into the affine expression and any subsequent divs
1512 * that may reference the div.
1514 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1521 isl_local_space
*ls
;
1524 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1525 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1526 if (n
< 0 || off
< 0)
1527 return isl_aff_free(aff
);
1529 for (i
= 0; i
< n
; ++i
) {
1530 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1532 ls
= isl_local_space_copy(aff
->ls
);
1533 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1534 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1535 vec
= isl_vec_copy(aff
->v
);
1536 vec
= isl_vec_cow(vec
);
1542 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1547 isl_vec_free(aff
->v
);
1549 isl_local_space_free(aff
->ls
);
1556 isl_local_space_free(ls
);
1557 return isl_aff_free(aff
);
1560 /* Look for any divs j that appear with a unit coefficient inside
1561 * the definitions of other divs i and plug them into the definitions
1564 * In particular, an expression of the form
1566 * floor((f(..) + floor(g(..)/n))/m)
1570 * floor((n * f(..) + g(..))/(n * m))
1572 * This simplification is correct because we can move the expression
1573 * f(..) into the inner floor in the original expression to obtain
1575 * floor(floor((n * f(..) + g(..))/n)/m)
1577 * from which we can derive the simplified expression.
1579 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1585 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1586 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1587 if (n
< 0 || off
< 0)
1588 return isl_aff_free(aff
);
1589 for (i
= 1; i
< n
; ++i
) {
1590 for (j
= 0; j
< i
; ++j
) {
1591 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1593 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1594 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1595 aff
->v
->size
, i
, 1);
1597 return isl_aff_free(aff
);
1604 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1606 * Even though this function is only called on isl_affs with a single
1607 * reference, we are careful to only change aff->v and aff->ls together.
1609 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1611 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1612 isl_local_space
*ls
;
1616 return isl_aff_free(aff
);
1618 ls
= isl_local_space_copy(aff
->ls
);
1619 ls
= isl_local_space_swap_div(ls
, a
, b
);
1620 v
= isl_vec_copy(aff
->v
);
1625 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1626 isl_vec_free(aff
->v
);
1628 isl_local_space_free(aff
->ls
);
1634 isl_local_space_free(ls
);
1635 return isl_aff_free(aff
);
1638 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1640 * We currently do not actually remove div "b", but simply add its
1641 * coefficient to that of "a" and then zero it out.
1643 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1645 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1648 return isl_aff_free(aff
);
1650 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1653 aff
->v
= isl_vec_cow(aff
->v
);
1655 return isl_aff_free(aff
);
1657 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1658 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1659 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1664 /* Sort the divs in the local space of "aff" according to
1665 * the comparison function "cmp_row" in isl_local_space.c,
1666 * combining the coefficients of identical divs.
1668 * Reordering divs does not change the semantics of "aff",
1669 * so there is no need to call isl_aff_cow.
1670 * Moreover, this function is currently only called on isl_affs
1671 * with a single reference.
1673 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1678 n
= isl_aff_dim(aff
, isl_dim_div
);
1680 return isl_aff_free(aff
);
1681 for (i
= 1; i
< n
; ++i
) {
1682 for (j
= i
- 1; j
>= 0; --j
) {
1683 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1687 aff
= merge_divs(aff
, j
, j
+ 1);
1689 aff
= swap_div(aff
, j
, j
+ 1);
1698 /* Normalize the representation of "aff".
1700 * This function should only be called on "new" isl_affs, i.e.,
1701 * with only a single reference. We therefore do not need to
1702 * worry about affecting other instances.
1704 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1708 aff
->v
= isl_vec_normalize(aff
->v
);
1710 return isl_aff_free(aff
);
1711 aff
= plug_in_integral_divs(aff
);
1712 aff
= plug_in_unit_divs(aff
);
1713 aff
= sort_divs(aff
);
1714 aff
= isl_aff_remove_unused_divs(aff
);
1718 /* Given f, return floor(f).
1719 * If f is an integer expression, then just return f.
1720 * If f is a constant, then return the constant floor(f).
1721 * Otherwise, if f = g/m, write g = q m + r,
1722 * create a new div d = [r/m] and return the expression q + d.
1723 * The coefficients in r are taken to lie between -m/2 and m/2.
1725 * reduce_div_coefficients performs the same normalization.
1727 * As a special case, floor(NaN) = NaN.
1729 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1739 if (isl_aff_is_nan(aff
))
1741 if (isl_int_is_one(aff
->v
->el
[0]))
1744 aff
= isl_aff_cow(aff
);
1748 aff
->v
= isl_vec_cow(aff
->v
);
1750 return isl_aff_free(aff
);
1752 if (isl_aff_is_cst(aff
)) {
1753 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1754 isl_int_set_si(aff
->v
->el
[0], 1);
1758 div
= isl_vec_copy(aff
->v
);
1759 div
= isl_vec_cow(div
);
1761 return isl_aff_free(aff
);
1763 ctx
= isl_aff_get_ctx(aff
);
1764 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1765 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1766 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1767 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1768 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1769 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1770 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1774 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1776 return isl_aff_free(aff
);
1778 size
= aff
->v
->size
;
1779 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1781 return isl_aff_free(aff
);
1782 isl_int_set_si(aff
->v
->el
[0], 1);
1783 isl_int_set_si(aff
->v
->el
[size
], 1);
1785 aff
= isl_aff_normalize(aff
);
1792 * aff mod m = aff - m * floor(aff/m)
1794 * with m an integer value.
1796 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1797 __isl_take isl_val
*m
)
1804 if (!isl_val_is_int(m
))
1805 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1806 "expecting integer modulo", goto error
);
1808 res
= isl_aff_copy(aff
);
1809 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1810 aff
= isl_aff_floor(aff
);
1811 aff
= isl_aff_scale_val(aff
, m
);
1812 res
= isl_aff_sub(res
, aff
);
1823 * pwaff mod m = pwaff - m * floor(pwaff/m)
1825 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1829 res
= isl_pw_aff_copy(pwaff
);
1830 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1831 pwaff
= isl_pw_aff_floor(pwaff
);
1832 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1833 res
= isl_pw_aff_sub(res
, pwaff
);
1840 * pa mod m = pa - m * floor(pa/m)
1842 * with m an integer value.
1844 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1845 __isl_take isl_val
*m
)
1849 if (!isl_val_is_int(m
))
1850 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1851 "expecting integer modulo", goto error
);
1852 pa
= isl_pw_aff_mod(pa
, m
->n
);
1856 isl_pw_aff_free(pa
);
1861 /* Given f, return ceil(f).
1862 * If f is an integer expression, then just return f.
1863 * Otherwise, let f be the expression
1869 * floor((e + m - 1)/m)
1871 * As a special case, ceil(NaN) = NaN.
1873 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1878 if (isl_aff_is_nan(aff
))
1880 if (isl_int_is_one(aff
->v
->el
[0]))
1883 aff
= isl_aff_cow(aff
);
1886 aff
->v
= isl_vec_cow(aff
->v
);
1888 return isl_aff_free(aff
);
1890 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1891 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1892 aff
= isl_aff_floor(aff
);
1897 /* Apply the expansion computed by isl_merge_divs.
1898 * The expansion itself is given by "exp" while the resulting
1899 * list of divs is given by "div".
1901 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1902 __isl_take isl_mat
*div
, int *exp
)
1908 aff
= isl_aff_cow(aff
);
1910 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1911 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1912 new_n_div
= isl_mat_rows(div
);
1913 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1916 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1917 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1918 if (!aff
->v
|| !aff
->ls
)
1919 return isl_aff_free(aff
);
1927 /* Add two affine expressions that live in the same local space.
1929 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1930 __isl_take isl_aff
*aff2
)
1934 aff1
= isl_aff_cow(aff1
);
1938 aff1
->v
= isl_vec_cow(aff1
->v
);
1944 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1945 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1946 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1947 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1948 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1949 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1950 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1955 aff1
= isl_aff_normalize(aff1
);
1963 /* Replace one of the arguments by a NaN and free the other one.
1965 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1966 __isl_take isl_aff
*aff2
)
1969 return isl_aff_set_nan(aff1
);
1972 /* Return the sum of "aff1" and "aff2".
1974 * If either of the two is NaN, then the result is NaN.
1976 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1977 __isl_take isl_aff
*aff2
)
1983 isl_size n_div1
, n_div2
;
1988 ctx
= isl_aff_get_ctx(aff1
);
1989 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1990 isl_die(ctx
, isl_error_invalid
,
1991 "spaces don't match", goto error
);
1993 if (isl_aff_is_nan(aff1
)) {
1997 if (isl_aff_is_nan(aff2
)) {
2002 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
2003 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
2004 if (n_div1
< 0 || n_div2
< 0)
2006 if (n_div1
== 0 && n_div2
== 0)
2007 return add_expanded(aff1
, aff2
);
2009 exp1
= isl_alloc_array(ctx
, int, n_div1
);
2010 exp2
= isl_alloc_array(ctx
, int, n_div2
);
2011 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
2014 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
2015 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
2016 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
2020 return add_expanded(aff1
, aff2
);
2029 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
2030 __isl_take isl_aff
*aff2
)
2032 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
2035 /* Return the result of scaling "aff" by a factor of "f".
2037 * As a special case, f * NaN = NaN.
2039 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
2045 if (isl_aff_is_nan(aff
))
2048 if (isl_int_is_one(f
))
2051 aff
= isl_aff_cow(aff
);
2054 aff
->v
= isl_vec_cow(aff
->v
);
2056 return isl_aff_free(aff
);
2058 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
2059 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
2064 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
2065 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2066 isl_int_divexact(gcd
, f
, gcd
);
2067 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2073 /* Multiple "aff" by "v".
2075 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
2076 __isl_take isl_val
*v
)
2081 if (isl_val_is_one(v
)) {
2086 if (!isl_val_is_rat(v
))
2087 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2088 "expecting rational factor", goto error
);
2090 aff
= isl_aff_scale(aff
, v
->n
);
2091 aff
= isl_aff_scale_down(aff
, v
->d
);
2101 /* Return the result of scaling "aff" down by a factor of "f".
2103 * As a special case, NaN/f = NaN.
2105 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2111 if (isl_aff_is_nan(aff
))
2114 if (isl_int_is_one(f
))
2117 aff
= isl_aff_cow(aff
);
2121 if (isl_int_is_zero(f
))
2122 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2123 "cannot scale down by zero", return isl_aff_free(aff
));
2125 aff
->v
= isl_vec_cow(aff
->v
);
2127 return isl_aff_free(aff
);
2130 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2131 isl_int_gcd(gcd
, gcd
, f
);
2132 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2133 isl_int_divexact(gcd
, f
, gcd
);
2134 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2140 /* Divide "aff" by "v".
2142 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2143 __isl_take isl_val
*v
)
2148 if (isl_val_is_one(v
)) {
2153 if (!isl_val_is_rat(v
))
2154 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2155 "expecting rational factor", goto error
);
2156 if (!isl_val_is_pos(v
))
2157 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2158 "factor needs to be positive", goto error
);
2160 aff
= isl_aff_scale(aff
, v
->d
);
2161 aff
= isl_aff_scale_down(aff
, v
->n
);
2171 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2179 isl_int_set_ui(v
, f
);
2180 aff
= isl_aff_scale_down(aff
, v
);
2186 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2187 enum isl_dim_type type
, unsigned pos
, const char *s
)
2189 aff
= isl_aff_cow(aff
);
2192 if (type
== isl_dim_out
)
2193 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2194 "cannot set name of output/set dimension",
2195 return isl_aff_free(aff
));
2196 if (type
== isl_dim_in
)
2198 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2200 return isl_aff_free(aff
);
2205 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2206 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2208 aff
= isl_aff_cow(aff
);
2211 if (type
== isl_dim_out
)
2212 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2213 "cannot set name of output/set dimension",
2215 if (type
== isl_dim_in
)
2217 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2219 return isl_aff_free(aff
);
2228 /* Replace the identifier of the input tuple of "aff" by "id".
2229 * type is currently required to be equal to isl_dim_in
2231 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2232 enum isl_dim_type type
, __isl_take isl_id
*id
)
2234 aff
= isl_aff_cow(aff
);
2237 if (type
!= isl_dim_in
)
2238 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2239 "cannot only set id of input tuple", goto error
);
2240 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2242 return isl_aff_free(aff
);
2251 /* Exploit the equalities in "eq" to simplify the affine expression
2252 * and the expressions of the integer divisions in the local space.
2253 * The integer divisions in this local space are assumed to appear
2254 * as regular dimensions in "eq".
2256 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2257 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2265 if (eq
->n_eq
== 0) {
2266 isl_basic_set_free(eq
);
2270 aff
= isl_aff_cow(aff
);
2274 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2275 isl_basic_set_copy(eq
));
2276 aff
->v
= isl_vec_cow(aff
->v
);
2277 if (!aff
->ls
|| !aff
->v
)
2280 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2282 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2283 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2284 if (j
< 0 || j
== 0 || j
>= o_div
)
2287 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2291 isl_basic_set_free(eq
);
2292 aff
= isl_aff_normalize(aff
);
2295 isl_basic_set_free(eq
);
2300 /* Exploit the equalities in "eq" to simplify the affine expression
2301 * and the expressions of the integer divisions in the local space.
2303 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2304 __isl_take isl_basic_set
*eq
)
2308 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2312 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2313 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2315 isl_basic_set_free(eq
);
2320 /* Look for equalities among the variables shared by context and aff
2321 * and the integer divisions of aff, if any.
2322 * The equalities are then used to eliminate coefficients and/or integer
2323 * divisions from aff.
2325 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2326 __isl_take isl_set
*context
)
2328 isl_local_space
*ls
;
2329 isl_basic_set
*hull
;
2331 ls
= isl_aff_get_domain_local_space(aff
);
2332 context
= isl_local_space_lift_set(ls
, context
);
2334 hull
= isl_set_affine_hull(context
);
2335 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2338 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2339 __isl_take isl_set
*context
)
2341 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2342 dom_context
= isl_set_intersect_params(dom_context
, context
);
2343 return isl_aff_gist(aff
, dom_context
);
2346 /* Return a basic set containing those elements in the space
2347 * of aff where it is positive. "rational" should not be set.
2349 * If "aff" is NaN, then it is not positive.
2351 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2352 int rational
, void *user
)
2354 isl_constraint
*ineq
;
2355 isl_basic_set
*bset
;
2360 if (isl_aff_is_nan(aff
)) {
2361 isl_space
*space
= isl_aff_get_domain_space(aff
);
2363 return isl_basic_set_empty(space
);
2366 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2367 "rational sets not supported", goto error
);
2369 ineq
= isl_inequality_from_aff(aff
);
2370 c
= isl_constraint_get_constant_val(ineq
);
2371 c
= isl_val_sub_ui(c
, 1);
2372 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2374 bset
= isl_basic_set_from_constraint(ineq
);
2375 bset
= isl_basic_set_simplify(bset
);
2382 /* Return a basic set containing those elements in the space
2383 * of aff where it is non-negative.
2384 * If "rational" is set, then return a rational basic set.
2386 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2388 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2389 __isl_take isl_aff
*aff
, int rational
, void *user
)
2391 isl_constraint
*ineq
;
2392 isl_basic_set
*bset
;
2396 if (isl_aff_is_nan(aff
)) {
2397 isl_space
*space
= isl_aff_get_domain_space(aff
);
2399 return isl_basic_set_empty(space
);
2402 ineq
= isl_inequality_from_aff(aff
);
2404 bset
= isl_basic_set_from_constraint(ineq
);
2406 bset
= isl_basic_set_set_rational(bset
);
2407 bset
= isl_basic_set_simplify(bset
);
2411 /* Return a basic set containing those elements in the space
2412 * of aff where it is non-negative.
2414 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2416 return aff_nonneg_basic_set(aff
, 0, NULL
);
2419 /* Return a basic set containing those elements in the domain space
2420 * of "aff" where it is positive.
2422 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2424 aff
= isl_aff_add_constant_num_si(aff
, -1);
2425 return isl_aff_nonneg_basic_set(aff
);
2428 /* Return a basic set containing those elements in the domain space
2429 * of aff where it is negative.
2431 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2433 aff
= isl_aff_neg(aff
);
2434 return isl_aff_pos_basic_set(aff
);
2437 /* Return a basic set containing those elements in the space
2438 * of aff where it is zero.
2439 * If "rational" is set, then return a rational basic set.
2441 * If "aff" is NaN, then it is not zero.
2443 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2444 int rational
, void *user
)
2446 isl_constraint
*ineq
;
2447 isl_basic_set
*bset
;
2451 if (isl_aff_is_nan(aff
)) {
2452 isl_space
*space
= isl_aff_get_domain_space(aff
);
2454 return isl_basic_set_empty(space
);
2457 ineq
= isl_equality_from_aff(aff
);
2459 bset
= isl_basic_set_from_constraint(ineq
);
2461 bset
= isl_basic_set_set_rational(bset
);
2462 bset
= isl_basic_set_simplify(bset
);
2466 /* Return a basic set containing those elements in the space
2467 * of aff where it is zero.
2469 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2471 return aff_zero_basic_set(aff
, 0, NULL
);
2474 /* Return a basic set containing those elements in the shared space
2475 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2477 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2478 __isl_take isl_aff
*aff2
)
2480 aff1
= isl_aff_sub(aff1
, aff2
);
2482 return isl_aff_nonneg_basic_set(aff1
);
2485 /* Return a basic set containing those elements in the shared domain space
2486 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2488 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2489 __isl_take isl_aff
*aff2
)
2491 aff1
= isl_aff_sub(aff1
, aff2
);
2493 return isl_aff_pos_basic_set(aff1
);
2496 /* Return a set containing those elements in the shared space
2497 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2499 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2500 __isl_take isl_aff
*aff2
)
2502 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2505 /* Return a set containing those elements in the shared domain space
2506 * of aff1 and aff2 where aff1 is greater than aff2.
2508 * If either of the two inputs is NaN, then the result is empty,
2509 * as comparisons with NaN always return false.
2511 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2512 __isl_take isl_aff
*aff2
)
2514 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2517 /* Return a basic set containing those elements in the shared space
2518 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2520 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2521 __isl_take isl_aff
*aff2
)
2523 return isl_aff_ge_basic_set(aff2
, aff1
);
2526 /* Return a basic set containing those elements in the shared domain space
2527 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2529 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2530 __isl_take isl_aff
*aff2
)
2532 return isl_aff_gt_basic_set(aff2
, aff1
);
2535 /* Return a set containing those elements in the shared space
2536 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2538 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2539 __isl_take isl_aff
*aff2
)
2541 return isl_aff_ge_set(aff2
, aff1
);
2544 /* Return a set containing those elements in the shared domain space
2545 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2547 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2548 __isl_take isl_aff
*aff2
)
2550 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2553 /* Return a basic set containing those elements in the shared space
2554 * of aff1 and aff2 where aff1 and aff2 are equal.
2556 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2557 __isl_take isl_aff
*aff2
)
2559 aff1
= isl_aff_sub(aff1
, aff2
);
2561 return isl_aff_zero_basic_set(aff1
);
2564 /* Return a set containing those elements in the shared space
2565 * of aff1 and aff2 where aff1 and aff2 are equal.
2567 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2568 __isl_take isl_aff
*aff2
)
2570 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2573 /* Return a set containing those elements in the shared domain space
2574 * of aff1 and aff2 where aff1 and aff2 are not equal.
2576 * If either of the two inputs is NaN, then the result is empty,
2577 * as comparisons with NaN always return false.
2579 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2580 __isl_take isl_aff
*aff2
)
2582 isl_set
*set_lt
, *set_gt
;
2584 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2585 isl_aff_copy(aff2
));
2586 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2587 return isl_set_union_disjoint(set_lt
, set_gt
);
2590 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2591 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2593 aff1
= isl_aff_add(aff1
, aff2
);
2594 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2598 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2601 return isl_bool_error
;
2603 return isl_bool_false
;
2607 #define TYPE isl_aff
2609 #include "check_type_range_templ.c"
2611 /* Check whether the given affine expression has non-zero coefficient
2612 * for any dimension in the given range or if any of these dimensions
2613 * appear with non-zero coefficients in any of the integer divisions
2614 * involved in the affine expression.
2616 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2617 enum isl_dim_type type
, unsigned first
, unsigned n
)
2621 isl_bool involves
= isl_bool_false
;
2624 return isl_bool_error
;
2626 return isl_bool_false
;
2627 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2628 return isl_bool_error
;
2630 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2634 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2635 for (i
= 0; i
< n
; ++i
)
2636 if (active
[first
+ i
]) {
2637 involves
= isl_bool_true
;
2646 return isl_bool_error
;
2649 /* Does "aff" involve any local variables, i.e., integer divisions?
2651 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2655 n
= isl_aff_dim(aff
, isl_dim_div
);
2657 return isl_bool_error
;
2658 return isl_bool_ok(n
> 0);
2661 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2662 enum isl_dim_type type
, unsigned first
, unsigned n
)
2666 if (type
== isl_dim_out
)
2667 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2668 "cannot drop output/set dimension",
2669 return isl_aff_free(aff
));
2670 if (type
== isl_dim_in
)
2672 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2675 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2676 return isl_aff_free(aff
);
2678 aff
= isl_aff_cow(aff
);
2682 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2684 return isl_aff_free(aff
);
2686 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2687 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2689 return isl_aff_free(aff
);
2694 /* Is the domain of "aff" a product?
2696 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2698 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2702 #define TYPE isl_aff
2703 #include <isl_domain_factor_templ.c>
2705 /* Project the domain of the affine expression onto its parameter space.
2706 * The affine expression may not involve any of the domain dimensions.
2708 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2713 n
= isl_aff_dim(aff
, isl_dim_in
);
2715 return isl_aff_free(aff
);
2716 aff
= isl_aff_drop_domain(aff
, 0, n
);
2717 space
= isl_aff_get_domain_space(aff
);
2718 space
= isl_space_params(space
);
2719 aff
= isl_aff_reset_domain_space(aff
, space
);
2723 /* Convert an affine expression defined over a parameter domain
2724 * into one that is defined over a zero-dimensional set.
2726 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2728 isl_local_space
*ls
;
2730 ls
= isl_aff_take_domain_local_space(aff
);
2731 ls
= isl_local_space_set_from_params(ls
);
2732 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2737 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2738 enum isl_dim_type type
, unsigned first
, unsigned n
)
2742 if (type
== isl_dim_out
)
2743 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2744 "cannot insert output/set dimensions",
2745 return isl_aff_free(aff
));
2746 if (type
== isl_dim_in
)
2748 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2751 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2752 return isl_aff_free(aff
);
2754 aff
= isl_aff_cow(aff
);
2758 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2760 return isl_aff_free(aff
);
2762 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2763 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2765 return isl_aff_free(aff
);
2770 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2771 enum isl_dim_type type
, unsigned n
)
2775 pos
= isl_aff_dim(aff
, type
);
2777 return isl_aff_free(aff
);
2779 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2782 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2783 * to dimensions of "dst_type" at "dst_pos".
2785 * We only support moving input dimensions to parameters and vice versa.
2787 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2788 enum isl_dim_type dst_type
, unsigned dst_pos
,
2789 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2793 isl_size src_off
, dst_off
;
2798 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2799 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2802 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2803 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2804 "cannot move output/set dimension",
2805 return isl_aff_free(aff
));
2806 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2807 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2808 "cannot move divs", return isl_aff_free(aff
));
2809 if (dst_type
== isl_dim_in
)
2810 dst_type
= isl_dim_set
;
2811 if (src_type
== isl_dim_in
)
2812 src_type
= isl_dim_set
;
2814 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2815 return isl_aff_free(aff
);
2816 if (dst_type
== src_type
)
2817 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2818 "moving dims within the same type not supported",
2819 return isl_aff_free(aff
));
2821 aff
= isl_aff_cow(aff
);
2822 src_off
= isl_aff_domain_offset(aff
, src_type
);
2823 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2824 if (src_off
< 0 || dst_off
< 0)
2825 return isl_aff_free(aff
);
2827 g_src_pos
= 1 + src_off
+ src_pos
;
2828 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2829 if (dst_type
> src_type
)
2832 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2833 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2834 src_type
, src_pos
, n
);
2835 if (!aff
->v
|| !aff
->ls
)
2836 return isl_aff_free(aff
);
2838 aff
= sort_divs(aff
);
2843 /* Given an affine function on a domain (A -> B),
2844 * interchange A and B in the wrapped domain
2845 * to obtain a function on the domain (B -> A).
2847 * Since this may change the position of some variables,
2848 * it may also change the normalized order of the local variables.
2849 * Restore this order. Since sort_divs assumes the input
2850 * has a single reference, an explicit isl_aff_cow is required.
2852 __isl_give isl_aff
*isl_aff_domain_reverse(__isl_take isl_aff
*aff
)
2855 isl_local_space
*ls
;
2857 isl_size n_in
, n_out
;
2860 space
= isl_aff_peek_domain_space(aff
);
2861 offset
= isl_space_offset(space
, isl_dim_set
);
2862 n_in
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_in
);
2863 n_out
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_out
);
2864 if (offset
< 0 || n_in
< 0 || n_out
< 0)
2865 return isl_aff_free(aff
);
2867 v
= isl_aff_take_rat_aff(aff
);
2868 v
= isl_vec_move_els(v
, 1 + 1 + offset
, 1 + 1 + offset
+ n_in
, n_out
);
2869 aff
= isl_aff_restore_rat_aff(aff
, v
);
2871 ls
= isl_aff_take_domain_local_space(aff
);
2872 ls
= isl_local_space_wrapped_reverse(ls
);
2873 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2875 aff
= isl_aff_cow(aff
);
2876 aff
= sort_divs(aff
);
2881 /* Return a zero isl_aff in the given space.
2883 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2884 * interface over all piecewise types.
2886 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2888 isl_local_space
*ls
;
2890 ls
= isl_local_space_from_space(isl_space_domain(space
));
2891 return isl_aff_zero_on_domain(ls
);
2894 #define isl_aff_involves_nan isl_aff_is_nan
2897 #define PW isl_pw_aff
2901 #define EL_IS_ZERO is_empty
2905 #define IS_ZERO is_empty
2908 #undef DEFAULT_IS_ZERO
2909 #define DEFAULT_IS_ZERO 0
2911 #include <isl_pw_templ.c>
2912 #include <isl_pw_un_op_templ.c>
2913 #include <isl_pw_add_constant_val_templ.c>
2914 #include <isl_pw_add_disjoint_templ.c>
2915 #include <isl_pw_bind_domain_templ.c>
2916 #include <isl_pw_domain_reverse_templ.c>
2917 #include <isl_pw_eval.c>
2918 #include <isl_pw_hash.c>
2919 #include <isl_pw_fix_templ.c>
2920 #include <isl_pw_from_range_templ.c>
2921 #include <isl_pw_insert_dims_templ.c>
2922 #include <isl_pw_insert_domain_templ.c>
2923 #include <isl_pw_move_dims_templ.c>
2924 #include <isl_pw_neg_templ.c>
2925 #include <isl_pw_pullback_templ.c>
2926 #include <isl_pw_scale_templ.c>
2927 #include <isl_pw_sub_templ.c>
2928 #include <isl_pw_union_opt.c>
2933 #include <isl_union_single.c>
2934 #include <isl_union_neg.c>
2935 #include <isl_union_sub_templ.c>
2940 #include <isl_union_pw_templ.c>
2942 /* Compute a piecewise quasi-affine expression with a domain that
2943 * is the union of those of pwaff1 and pwaff2 and such that on each
2944 * cell, the quasi-affine expression is the maximum of those of pwaff1
2945 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2946 * cell, then the associated expression is the defined one.
2948 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2949 __isl_take isl_pw_aff
*pwaff2
)
2951 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2952 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2955 /* Compute a piecewise quasi-affine expression with a domain that
2956 * is the union of those of pwaff1 and pwaff2 and such that on each
2957 * cell, the quasi-affine expression is the minimum of those of pwaff1
2958 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2959 * cell, then the associated expression is the defined one.
2961 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2962 __isl_take isl_pw_aff
*pwaff2
)
2964 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2965 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2968 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2969 __isl_take isl_pw_aff
*pwaff2
, int max
)
2972 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2974 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2977 /* Is the domain of "pa" a product?
2979 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2981 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2985 #define TYPE isl_pw_aff
2986 #include <isl_domain_factor_templ.c>
2988 /* Return a set containing those elements in the domain
2989 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2990 * does not satisfy "fn" (if complement is 1).
2992 * The pieces with a NaN never belong to the result since
2993 * NaN does not satisfy any property.
2995 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2996 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2998 int complement
, void *user
)
3006 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
3008 for (i
= 0; i
< pwaff
->n
; ++i
) {
3009 isl_basic_set
*bset
;
3010 isl_set
*set_i
, *locus
;
3013 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
3016 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
3017 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
3018 locus
= isl_set_from_basic_set(bset
);
3019 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
3021 set_i
= isl_set_subtract(set_i
, locus
);
3023 set_i
= isl_set_intersect(set_i
, locus
);
3024 set
= isl_set_union_disjoint(set
, set_i
);
3027 isl_pw_aff_free(pwaff
);
3032 /* Return a set containing those elements in the domain
3033 * of "pa" where it is positive.
3035 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
3037 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
3040 /* Return a set containing those elements in the domain
3041 * of pwaff where it is non-negative.
3043 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
3045 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
3048 /* Return a set containing those elements in the domain
3049 * of pwaff where it is zero.
3051 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
3053 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
3056 /* Return a set containing those elements in the domain
3057 * of pwaff where it is not zero.
3059 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
3061 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
3064 /* Bind the affine function "aff" to the parameter "id",
3065 * returning the elements in the domain where the affine expression
3066 * is equal to the parameter.
3068 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
3069 __isl_take isl_id
*id
)
3074 space
= isl_aff_get_domain_space(aff
);
3075 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
3077 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
3078 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
3080 return isl_aff_eq_basic_set(aff
, aff_id
);
3083 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
3084 * "rational" should not be set.
3086 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
3087 int rational
, void *user
)
3094 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
3095 "rational binding not supported", goto error
);
3096 return isl_aff_bind_id(aff
, isl_id_copy(id
));
3102 /* Bind the piecewise affine function "pa" to the parameter "id",
3103 * returning the elements in the domain where the expression
3104 * is equal to the parameter.
3106 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
3107 __isl_take isl_id
*id
)
3111 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3117 /* Return a set containing those elements in the shared domain
3118 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3120 * We compute the difference on the shared domain and then construct
3121 * the set of values where this difference is non-negative.
3122 * If strict is set, we first subtract 1 from the difference.
3123 * If equal is set, we only return the elements where pwaff1 and pwaff2
3126 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3127 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3129 isl_set
*set1
, *set2
;
3131 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3132 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3133 set1
= isl_set_intersect(set1
, set2
);
3134 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3135 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3136 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3139 isl_space
*space
= isl_set_get_space(set1
);
3141 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3142 aff
= isl_aff_add_constant_si(aff
, -1);
3143 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3148 return isl_pw_aff_zero_set(pwaff1
);
3149 return isl_pw_aff_nonneg_set(pwaff1
);
3152 /* Return a set containing those elements in the shared domain
3153 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3155 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3156 __isl_take isl_pw_aff
*pwaff2
)
3158 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3159 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3162 /* Return a set containing those elements in the shared domain
3163 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3165 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3166 __isl_take isl_pw_aff
*pwaff2
)
3168 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3169 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3172 /* Return a set containing those elements in the shared domain
3173 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3175 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3176 __isl_take isl_pw_aff
*pwaff2
)
3178 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3179 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3182 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3183 __isl_take isl_pw_aff
*pwaff2
)
3185 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3188 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3189 __isl_take isl_pw_aff
*pwaff2
)
3191 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3194 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3195 * where the function values are ordered in the same way as "order",
3196 * which returns a set in the shared domain of its two arguments.
3198 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3199 * We first pull back the two functions such that they are defined on
3200 * the domain [A -> B]. Then we apply "order", resulting in a set
3201 * in the space [A -> B]. Finally, we unwrap this set to obtain
3202 * a map in the space A -> B.
3204 static __isl_give isl_map
*isl_pw_aff_order_map(
3205 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3206 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3207 __isl_take isl_pw_aff
*pa2
))
3209 isl_space
*space1
, *space2
;
3213 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3214 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3215 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3216 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3217 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3218 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3219 ma
= isl_multi_aff_range_map(space1
);
3220 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3221 set
= order(pa1
, pa2
);
3223 return isl_set_unwrap(set
);
3226 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3227 * where the function values are equal.
3229 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3230 __isl_take isl_pw_aff
*pa2
)
3232 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3235 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3236 * where the function value of "pa1" is less than or equal to
3237 * the function value of "pa2".
3239 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3240 __isl_take isl_pw_aff
*pa2
)
3242 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3245 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3246 * where the function value of "pa1" is less than the function value of "pa2".
3248 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3249 __isl_take isl_pw_aff
*pa2
)
3251 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3254 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3255 * where the function value of "pa1" is greater than or equal to
3256 * the function value of "pa2".
3258 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3259 __isl_take isl_pw_aff
*pa2
)
3261 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3264 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3265 * where the function value of "pa1" is greater than the function value
3268 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3269 __isl_take isl_pw_aff
*pa2
)
3271 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3274 /* Return a set containing those elements in the shared domain
3275 * of the elements of list1 and list2 where each element in list1
3276 * has the relation specified by "fn" with each element in list2.
3278 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3279 __isl_take isl_pw_aff_list
*list2
,
3280 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3281 __isl_take isl_pw_aff
*pwaff2
))
3287 if (!list1
|| !list2
)
3290 ctx
= isl_pw_aff_list_get_ctx(list1
);
3291 if (list1
->n
< 1 || list2
->n
< 1)
3292 isl_die(ctx
, isl_error_invalid
,
3293 "list should contain at least one element", goto error
);
3295 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3296 for (i
= 0; i
< list1
->n
; ++i
)
3297 for (j
= 0; j
< list2
->n
; ++j
) {
3300 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3301 isl_pw_aff_copy(list2
->p
[j
]));
3302 set
= isl_set_intersect(set
, set_ij
);
3305 isl_pw_aff_list_free(list1
);
3306 isl_pw_aff_list_free(list2
);
3309 isl_pw_aff_list_free(list1
);
3310 isl_pw_aff_list_free(list2
);
3314 /* Return a set containing those elements in the shared domain
3315 * of the elements of list1 and list2 where each element in list1
3316 * is equal to each element in list2.
3318 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3319 __isl_take isl_pw_aff_list
*list2
)
3321 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3324 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3325 __isl_take isl_pw_aff_list
*list2
)
3327 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3330 /* Return a set containing those elements in the shared domain
3331 * of the elements of list1 and list2 where each element in list1
3332 * is less than or equal to each element in list2.
3334 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3335 __isl_take isl_pw_aff_list
*list2
)
3337 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3340 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3341 __isl_take isl_pw_aff_list
*list2
)
3343 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3346 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3347 __isl_take isl_pw_aff_list
*list2
)
3349 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3352 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3353 __isl_take isl_pw_aff_list
*list2
)
3355 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3359 /* Return a set containing those elements in the shared domain
3360 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3362 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3363 __isl_take isl_pw_aff
*pwaff2
)
3365 isl_set
*set_lt
, *set_gt
;
3367 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3368 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3369 isl_pw_aff_copy(pwaff2
));
3370 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3371 return isl_set_union_disjoint(set_lt
, set_gt
);
3374 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3379 if (isl_int_is_one(v
))
3381 if (!isl_int_is_pos(v
))
3382 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3383 "factor needs to be positive",
3384 return isl_pw_aff_free(pwaff
));
3385 pwaff
= isl_pw_aff_cow(pwaff
);
3391 for (i
= 0; i
< pwaff
->n
; ++i
) {
3392 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3393 if (!pwaff
->p
[i
].aff
)
3394 return isl_pw_aff_free(pwaff
);
3400 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3402 struct isl_pw_aff_un_op_control control
= { .fn_base
= &isl_aff_floor
};
3403 return isl_pw_aff_un_op(pwaff
, &control
);
3406 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3408 struct isl_pw_aff_un_op_control control
= { .fn_base
= &isl_aff_ceil
};
3409 return isl_pw_aff_un_op(pwaff
, &control
);
3412 /* Assuming that "cond1" and "cond2" are disjoint,
3413 * return an affine expression that is equal to pwaff1 on cond1
3414 * and to pwaff2 on cond2.
3416 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3417 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3418 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3420 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3421 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3423 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3426 /* Return an affine expression that is equal to pwaff_true for elements
3427 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3429 * That is, return cond ? pwaff_true : pwaff_false;
3431 * If "cond" involves and NaN, then we conservatively return a NaN
3432 * on its entire domain. In principle, we could consider the pieces
3433 * where it is NaN separately from those where it is not.
3435 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3436 * then only use the domain of "cond" to restrict the domain.
3438 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3439 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3441 isl_set
*cond_true
, *cond_false
;
3446 if (isl_pw_aff_involves_nan(cond
)) {
3447 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3448 isl_local_space
*ls
= isl_local_space_from_space(space
);
3449 isl_pw_aff_free(cond
);
3450 isl_pw_aff_free(pwaff_true
);
3451 isl_pw_aff_free(pwaff_false
);
3452 return isl_pw_aff_nan_on_domain(ls
);
3455 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3456 isl_pw_aff_get_space(pwaff_false
));
3457 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3458 isl_pw_aff_get_space(pwaff_true
));
3459 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3465 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3466 isl_pw_aff_free(pwaff_false
);
3467 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3470 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3471 cond_false
= isl_pw_aff_zero_set(cond
);
3472 return isl_pw_aff_select(cond_true
, pwaff_true
,
3473 cond_false
, pwaff_false
);
3475 isl_pw_aff_free(cond
);
3476 isl_pw_aff_free(pwaff_true
);
3477 isl_pw_aff_free(pwaff_false
);
3481 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3486 return isl_bool_error
;
3488 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3489 return isl_bool_ok(pos
== -1);
3492 /* Check whether pwaff is a piecewise constant.
3494 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3499 return isl_bool_error
;
3501 for (i
= 0; i
< pwaff
->n
; ++i
) {
3502 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3503 if (is_cst
< 0 || !is_cst
)
3507 return isl_bool_true
;
3510 /* Return the product of "aff1" and "aff2".
3512 * If either of the two is NaN, then the result is NaN.
3514 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3516 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3517 __isl_take isl_aff
*aff2
)
3522 if (isl_aff_is_nan(aff1
)) {
3526 if (isl_aff_is_nan(aff2
)) {
3531 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3532 return isl_aff_mul(aff2
, aff1
);
3534 if (!isl_aff_is_cst(aff2
))
3535 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3536 "at least one affine expression should be constant",
3539 aff1
= isl_aff_cow(aff1
);
3543 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3544 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3554 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3556 * If either of the two is NaN, then the result is NaN.
3557 * A division by zero also results in NaN.
3559 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3560 __isl_take isl_aff
*aff2
)
3562 isl_bool is_cst
, is_zero
;
3568 if (isl_aff_is_nan(aff1
)) {
3572 if (isl_aff_is_nan(aff2
)) {
3577 is_cst
= isl_aff_is_cst(aff2
);
3581 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3582 "second argument should be a constant", goto error
);
3583 is_zero
= isl_aff_plain_is_zero(aff2
);
3587 return set_nan_free(aff1
, aff2
);
3589 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3591 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3592 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3595 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3596 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3599 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3600 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3611 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3612 __isl_take isl_pw_aff
*pwaff2
)
3614 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3615 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3618 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3619 __isl_take isl_pw_aff
*pwaff2
)
3621 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3622 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3625 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3627 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3628 __isl_take isl_pw_aff
*pa2
)
3632 is_cst
= isl_pw_aff_is_cst(pa2
);
3636 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3637 "second argument should be a piecewise constant",
3639 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3640 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3642 isl_pw_aff_free(pa1
);
3643 isl_pw_aff_free(pa2
);
3647 /* Compute the quotient of the integer division of "pa1" by "pa2"
3648 * with rounding towards zero.
3649 * "pa2" is assumed to be a piecewise constant.
3651 * In particular, return
3653 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3656 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3657 __isl_take isl_pw_aff
*pa2
)
3663 is_cst
= isl_pw_aff_is_cst(pa2
);
3667 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3668 "second argument should be a piecewise constant",
3671 pa1
= isl_pw_aff_div(pa1
, pa2
);
3673 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3674 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3675 c
= isl_pw_aff_ceil(pa1
);
3676 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3678 isl_pw_aff_free(pa1
);
3679 isl_pw_aff_free(pa2
);
3683 /* Compute the remainder of the integer division of "pa1" by "pa2"
3684 * with rounding towards zero.
3685 * "pa2" is assumed to be a piecewise constant.
3687 * In particular, return
3689 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3692 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3693 __isl_take isl_pw_aff
*pa2
)
3698 is_cst
= isl_pw_aff_is_cst(pa2
);
3702 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3703 "second argument should be a piecewise constant",
3705 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3706 res
= isl_pw_aff_mul(pa2
, res
);
3707 res
= isl_pw_aff_sub(pa1
, res
);
3710 isl_pw_aff_free(pa1
);
3711 isl_pw_aff_free(pa2
);
3715 /* Does either of "pa1" or "pa2" involve any NaN?
3717 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3718 __isl_keep isl_pw_aff
*pa2
)
3722 has_nan
= isl_pw_aff_involves_nan(pa1
);
3723 if (has_nan
< 0 || has_nan
)
3725 return isl_pw_aff_involves_nan(pa2
);
3728 /* Return a piecewise affine expression defined on the specified domain
3729 * that represents NaN.
3731 static __isl_give isl_pw_aff
*nan_on_domain_set(__isl_take isl_set
*dom
)
3733 isl_local_space
*ls
;
3736 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3737 pa
= isl_pw_aff_nan_on_domain(ls
);
3738 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3743 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3744 * by a NaN on their shared domain.
3746 * In principle, the result could be refined to only being NaN
3747 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3749 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3750 __isl_take isl_pw_aff
*pa2
)
3754 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3755 return nan_on_domain_set(dom
);
3758 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3759 __isl_take isl_pw_aff
*pwaff2
)
3764 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3765 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3766 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3767 isl_pw_aff_copy(pwaff2
));
3768 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3769 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3772 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3773 __isl_take isl_pw_aff
*pwaff2
)
3778 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3779 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3780 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3781 isl_pw_aff_copy(pwaff2
));
3782 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3783 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3786 /* Return an expression for the minimum (if "max" is not set) or
3787 * the maximum (if "max" is set) of "pa1" and "pa2".
3788 * If either expression involves any NaN, then return a NaN
3789 * on the shared domain as result.
3791 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3792 __isl_take isl_pw_aff
*pa2
, int max
)
3796 has_nan
= either_involves_nan(pa1
, pa2
);
3798 pa1
= isl_pw_aff_free(pa1
);
3800 return replace_by_nan(pa1
, pa2
);
3802 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3804 return pw_aff_max(pa1
, pa2
);
3806 return pw_aff_min(pa1
, pa2
);
3809 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3811 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3812 __isl_take isl_pw_aff
*pwaff2
)
3814 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3817 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3819 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3820 __isl_take isl_pw_aff
*pwaff2
)
3822 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3825 /* Does "pa" not involve any NaN?
3827 static isl_bool
pw_aff_no_nan(__isl_keep isl_pw_aff
*pa
, void *user
)
3829 return isl_bool_not(isl_pw_aff_involves_nan(pa
));
3832 /* Does any element of "list" involve any NaN?
3834 * That is, is it not the case that every element does not involve any NaN?
3836 static isl_bool
isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list
*list
)
3838 return isl_bool_not(isl_pw_aff_list_every(list
, &pw_aff_no_nan
, NULL
));
3841 /* Replace "list" (consisting of "n" elements, of which
3842 * at least one element involves a NaN)
3843 * by a NaN on the shared domain of the elements.
3845 * In principle, the result could be refined to only being NaN
3846 * on the parts of this domain where at least one of the elements is NaN.
3848 static __isl_give isl_pw_aff
*replace_list_by_nan(
3849 __isl_take isl_pw_aff_list
*list
, int n
)
3854 dom
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, 0));
3855 for (i
= 1; i
< n
; ++i
) {
3858 dom_i
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, i
));
3859 dom
= isl_set_intersect(dom
, dom_i
);
3862 isl_pw_aff_list_free(list
);
3863 return nan_on_domain_set(dom
);
3866 /* Return the set where the element at "pos1" of "list" is less than or
3867 * equal to the element at "pos2".
3868 * Equality is only allowed if "pos1" is smaller than "pos2".
3870 static __isl_give isl_set
*less(__isl_keep isl_pw_aff_list
*list
,
3873 isl_pw_aff
*pa1
, *pa2
;
3875 pa1
= isl_pw_aff_list_get_at(list
, pos1
);
3876 pa2
= isl_pw_aff_list_get_at(list
, pos2
);
3879 return isl_pw_aff_le_set(pa1
, pa2
);
3881 return isl_pw_aff_lt_set(pa1
, pa2
);
3884 /* Return an isl_pw_aff that maps each element in the intersection of the
3885 * domains of the piecewise affine expressions in "list"
3886 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3887 * expression in "list" at that element.
3888 * If any expression involves any NaN, then return a NaN
3889 * on the shared domain as result.
3891 * If "list" has n elements, then the result consists of n pieces,
3892 * where, in the case of a minimum, each piece has as value expression
3893 * the value expression of one of the elements and as domain
3894 * the set of elements where that value expression
3895 * is less than (or equal) to the other value expressions.
3896 * In the case of a maximum, the condition is
3897 * that all the other value expressions are less than (or equal)
3898 * to the given value expression.
3900 * In order to produce disjoint pieces, a pair of elements
3901 * in the original domain is only allowed to be equal to each other
3902 * on exactly one of the two pieces corresponding to the two elements.
3903 * The position in the list is used to break ties.
3904 * In particular, in the case of a minimum,
3905 * in the piece corresponding to a given element,
3906 * this element is allowed to be equal to any later element in the list,
3907 * but not to any earlier element in the list.
3909 static __isl_give isl_pw_aff
*isl_pw_aff_list_opt(
3910 __isl_take isl_pw_aff_list
*list
, int max
)
3916 isl_pw_aff
*pa
, *res
;
3918 n
= isl_pw_aff_list_size(list
);
3922 isl_die(isl_pw_aff_list_get_ctx(list
), isl_error_invalid
,
3923 "list should contain at least one element", goto error
);
3925 has_nan
= isl_pw_aff_list_involves_nan(list
);
3929 return replace_list_by_nan(list
, n
);
3931 pa
= isl_pw_aff_list_get_at(list
, 0);
3932 space
= isl_pw_aff_get_space(pa
);
3933 isl_pw_aff_free(pa
);
3934 res
= isl_pw_aff_empty(space
);
3936 for (i
= 0; i
< n
; ++i
) {
3937 pa
= isl_pw_aff_list_get_at(list
, i
);
3938 for (j
= 0; j
< n
; ++j
) {
3944 dom
= less(list
, j
, i
);
3946 dom
= less(list
, i
, j
);
3948 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3950 res
= isl_pw_aff_add_disjoint(res
, pa
);
3953 isl_pw_aff_list_free(list
);
3956 isl_pw_aff_list_free(list
);
3960 /* Return an isl_pw_aff that maps each element in the intersection of the
3961 * domains of the elements of list to the minimal corresponding affine
3964 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3966 return isl_pw_aff_list_opt(list
, 0);
3969 /* Return an isl_pw_aff that maps each element in the intersection of the
3970 * domains of the elements of list to the maximal corresponding affine
3973 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3975 return isl_pw_aff_list_opt(list
, 1);
3978 /* Mark the domains of "pwaff" as rational.
3980 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3984 pwaff
= isl_pw_aff_cow(pwaff
);
3990 for (i
= 0; i
< pwaff
->n
; ++i
) {
3991 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3992 if (!pwaff
->p
[i
].set
)
3993 return isl_pw_aff_free(pwaff
);
3999 /* Mark the domains of the elements of "list" as rational.
4001 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
4002 __isl_take isl_pw_aff_list
*list
)
4012 for (i
= 0; i
< n
; ++i
) {
4015 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
4016 pa
= isl_pw_aff_set_rational(pa
);
4017 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
4023 /* Do the parameters of "aff" match those of "space"?
4025 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
4026 __isl_keep isl_space
*space
)
4028 isl_space
*aff_space
;
4032 return isl_bool_error
;
4034 aff_space
= isl_aff_get_domain_space(aff
);
4036 match
= isl_space_has_equal_params(space
, aff_space
);
4038 isl_space_free(aff_space
);
4042 /* Check that the domain space of "aff" matches "space".
4044 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
4045 __isl_keep isl_space
*space
)
4047 isl_space
*aff_space
;
4051 return isl_stat_error
;
4053 aff_space
= isl_aff_get_domain_space(aff
);
4055 match
= isl_space_has_equal_params(space
, aff_space
);
4059 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4060 "parameters don't match", goto error
);
4061 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
4062 aff_space
, isl_dim_set
);
4066 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4067 "domains don't match", goto error
);
4068 isl_space_free(aff_space
);
4071 isl_space_free(aff_space
);
4072 return isl_stat_error
;
4075 /* Return the shared (universe) domain of the elements of "ma".
4077 * Since an isl_multi_aff (and an isl_aff) is always total,
4078 * the domain is always the universe set in its domain space.
4079 * This is a helper function for use in the generic isl_multi_*_bind.
4081 static __isl_give isl_basic_set
*isl_multi_aff_domain(
4082 __isl_take isl_multi_aff
*ma
)
4086 space
= isl_multi_aff_get_space(ma
);
4087 isl_multi_aff_free(ma
);
4089 return isl_basic_set_universe(isl_space_domain(space
));
4095 #include <isl_multi_no_explicit_domain.c>
4096 #include <isl_multi_templ.c>
4097 #include <isl_multi_un_op_templ.c>
4098 #include <isl_multi_bin_val_templ.c>
4099 #include <isl_multi_add_constant_templ.c>
4100 #include <isl_multi_align_set.c>
4101 #include <isl_multi_arith_templ.c>
4102 #include <isl_multi_bind_domain_templ.c>
4103 #include <isl_multi_cmp.c>
4104 #include <isl_multi_dim_id_templ.c>
4105 #include <isl_multi_dims.c>
4106 #include <isl_multi_domain_reverse_templ.c>
4107 #include <isl_multi_floor.c>
4108 #include <isl_multi_from_base_templ.c>
4109 #include <isl_multi_identity_templ.c>
4110 #include <isl_multi_insert_domain_templ.c>
4111 #include <isl_multi_locals_templ.c>
4112 #include <isl_multi_move_dims_templ.c>
4113 #include <isl_multi_nan_templ.c>
4114 #include <isl_multi_product_templ.c>
4115 #include <isl_multi_splice_templ.c>
4116 #include <isl_multi_tuple_id_templ.c>
4117 #include <isl_multi_unbind_params_templ.c>
4118 #include <isl_multi_zero_templ.c>
4122 #include <isl_multi_check_domain_templ.c>
4123 #include <isl_multi_apply_set_no_explicit_domain_templ.c>
4124 #include <isl_multi_gist.c>
4127 #define DOMBASE basic_set
4128 #include <isl_multi_bind_templ.c>
4130 /* Construct an isl_multi_aff living in "space" that corresponds
4131 * to the affine transformation matrix "mat".
4133 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
4134 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
4137 isl_local_space
*ls
= NULL
;
4138 isl_multi_aff
*ma
= NULL
;
4139 isl_size n_row
, n_col
, n_out
, total
;
4145 ctx
= isl_mat_get_ctx(mat
);
4147 n_row
= isl_mat_rows(mat
);
4148 n_col
= isl_mat_cols(mat
);
4149 n_out
= isl_space_dim(space
, isl_dim_out
);
4150 total
= isl_space_dim(space
, isl_dim_all
);
4151 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
4154 isl_die(ctx
, isl_error_invalid
,
4155 "insufficient number of rows", goto error
);
4157 isl_die(ctx
, isl_error_invalid
,
4158 "insufficient number of columns", goto error
);
4159 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
4160 isl_die(ctx
, isl_error_invalid
,
4161 "dimension mismatch", goto error
);
4163 ma
= isl_multi_aff_zero(isl_space_copy(space
));
4164 space
= isl_space_domain(space
);
4165 ls
= isl_local_space_from_space(isl_space_copy(space
));
4167 for (i
= 0; i
< n_row
- 1; ++i
) {
4171 v
= isl_vec_alloc(ctx
, 1 + n_col
);
4174 isl_int_set(v
->el
[0], mat
->row
[0][0]);
4175 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
4176 v
= isl_vec_normalize(v
);
4177 aff
= isl_aff_alloc_vec_validated(isl_local_space_copy(ls
), v
);
4178 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4181 isl_space_free(space
);
4182 isl_local_space_free(ls
);
4186 isl_space_free(space
);
4187 isl_local_space_free(ls
);
4189 isl_multi_aff_free(ma
);
4193 /* Return the constant terms of the affine expressions of "ma".
4195 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
4196 __isl_keep isl_multi_aff
*ma
)
4203 n
= isl_multi_aff_size(ma
);
4206 space
= isl_space_range(isl_multi_aff_get_space(ma
));
4207 space
= isl_space_drop_all_params(space
);
4208 mv
= isl_multi_val_zero(space
);
4210 for (i
= 0; i
< n
; ++i
) {
4214 aff
= isl_multi_aff_get_at(ma
, i
);
4215 val
= isl_aff_get_constant_val(aff
);
4217 mv
= isl_multi_val_set_at(mv
, i
, val
);
4223 /* Remove any internal structure of the domain of "ma".
4224 * If there is any such internal structure in the input,
4225 * then the name of the corresponding space is also removed.
4227 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4228 __isl_take isl_multi_aff
*ma
)
4235 if (!ma
->space
->nested
[0])
4238 space
= isl_multi_aff_get_space(ma
);
4239 space
= isl_space_flatten_domain(space
);
4240 ma
= isl_multi_aff_reset_space(ma
, space
);
4245 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4246 * of the space to its domain.
4248 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4252 isl_local_space
*ls
;
4257 if (!isl_space_is_map(space
))
4258 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4259 "not a map space", goto error
);
4261 n_in
= isl_space_dim(space
, isl_dim_in
);
4264 space
= isl_space_domain_map(space
);
4266 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4268 isl_space_free(space
);
4272 space
= isl_space_domain(space
);
4273 ls
= isl_local_space_from_space(space
);
4274 for (i
= 0; i
< n_in
; ++i
) {
4277 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4279 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4281 isl_local_space_free(ls
);
4284 isl_space_free(space
);
4288 /* This function performs the same operation as isl_multi_aff_domain_map,
4289 * but is considered as a function on an isl_space when exported.
4291 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4292 __isl_take isl_space
*space
)
4294 return isl_multi_aff_domain_map(space
);
4297 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4298 * of the space to its range.
4300 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4303 isl_size n_in
, n_out
;
4304 isl_local_space
*ls
;
4309 if (!isl_space_is_map(space
))
4310 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4311 "not a map space", goto error
);
4313 n_in
= isl_space_dim(space
, isl_dim_in
);
4314 n_out
= isl_space_dim(space
, isl_dim_out
);
4315 if (n_in
< 0 || n_out
< 0)
4317 space
= isl_space_range_map(space
);
4319 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4321 isl_space_free(space
);
4325 space
= isl_space_domain(space
);
4326 ls
= isl_local_space_from_space(space
);
4327 for (i
= 0; i
< n_out
; ++i
) {
4330 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4331 isl_dim_set
, n_in
+ i
);
4332 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4334 isl_local_space_free(ls
);
4337 isl_space_free(space
);
4341 /* This function performs the same operation as isl_multi_aff_range_map,
4342 * but is considered as a function on an isl_space when exported.
4344 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4345 __isl_take isl_space
*space
)
4347 return isl_multi_aff_range_map(space
);
4350 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4351 * of the space to its domain.
4353 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4354 __isl_take isl_space
*space
)
4356 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4359 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4360 * but is considered as a function on an isl_space when exported.
4362 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4363 __isl_take isl_space
*space
)
4365 return isl_pw_multi_aff_domain_map(space
);
4368 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4369 * of the space to its range.
4371 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4372 __isl_take isl_space
*space
)
4374 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4377 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4378 * but is considered as a function on an isl_space when exported.
4380 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4381 __isl_take isl_space
*space
)
4383 return isl_pw_multi_aff_range_map(space
);
4386 /* Given the space of a set and a range of set dimensions,
4387 * construct an isl_multi_aff that projects out those dimensions.
4389 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4390 __isl_take isl_space
*space
, enum isl_dim_type type
,
4391 unsigned first
, unsigned n
)
4395 isl_local_space
*ls
;
4400 if (!isl_space_is_set(space
))
4401 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4402 "expecting set space", goto error
);
4403 if (type
!= isl_dim_set
)
4404 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4405 "only set dimensions can be projected out", goto error
);
4406 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4409 dim
= isl_space_dim(space
, isl_dim_set
);
4413 space
= isl_space_from_domain(space
);
4414 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4417 return isl_multi_aff_alloc(space
);
4419 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4420 space
= isl_space_domain(space
);
4421 ls
= isl_local_space_from_space(space
);
4423 for (i
= 0; i
< first
; ++i
) {
4426 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4428 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4431 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4434 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4435 isl_dim_set
, first
+ n
+ i
);
4436 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4439 isl_local_space_free(ls
);
4442 isl_space_free(space
);
4446 /* Given the space of a set and a range of set dimensions,
4447 * construct an isl_pw_multi_aff that projects out those dimensions.
4449 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4450 __isl_take isl_space
*space
, enum isl_dim_type type
,
4451 unsigned first
, unsigned n
)
4455 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4456 return isl_pw_multi_aff_from_multi_aff(ma
);
4459 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4460 * but is considered as a function on an isl_multi_aff when exported.
4462 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4463 __isl_take isl_multi_aff
*ma
)
4465 return isl_pw_multi_aff_from_multi_aff(ma
);
4468 /* Create a piecewise multi-affine expression in the given space that maps each
4469 * input dimension to the corresponding output dimension.
4471 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4472 __isl_take isl_space
*space
)
4474 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4477 /* Create a piecewise multi expression that maps elements in the given space
4480 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4481 __isl_take isl_space
*space
)
4485 ma
= isl_multi_aff_identity_on_domain_space(space
);
4486 return isl_pw_multi_aff_from_multi_aff(ma
);
4489 /* This function performs the same operation as
4490 * isl_pw_multi_aff_identity_on_domain_space,
4491 * but is considered as a function on an isl_space when exported.
4493 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4494 __isl_take isl_space
*space
)
4496 return isl_pw_multi_aff_identity_on_domain_space(space
);
4499 /* Exploit the equalities in "eq" to simplify the affine expressions.
4501 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4502 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4507 n
= isl_multi_aff_size(maff
);
4511 for (i
= 0; i
< n
; ++i
) {
4514 aff
= isl_multi_aff_take_at(maff
, i
);
4515 aff
= isl_aff_substitute_equalities(aff
,
4516 isl_basic_set_copy(eq
));
4517 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4520 isl_basic_set_free(eq
);
4523 isl_basic_set_free(eq
);
4524 isl_multi_aff_free(maff
);
4528 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4534 n
= isl_multi_aff_size(maff
);
4536 return isl_multi_aff_free(maff
);
4538 for (i
= 0; i
< n
; ++i
) {
4541 aff
= isl_multi_aff_take_at(maff
, i
);
4542 aff
= isl_aff_scale(aff
, f
);
4543 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4549 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4550 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4552 maff1
= isl_multi_aff_add(maff1
, maff2
);
4553 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4557 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4560 return isl_bool_error
;
4562 return isl_bool_false
;
4565 /* Return the set of domain elements where "ma1" is lexicographically
4566 * smaller than or equal to "ma2".
4568 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4569 __isl_take isl_multi_aff
*ma2
)
4571 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4574 /* Return the set of domain elements where "ma1" is lexicographically
4575 * smaller than "ma2".
4577 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4578 __isl_take isl_multi_aff
*ma2
)
4580 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4583 /* Return the set of domain elements where "ma1" is lexicographically
4584 * greater than to "ma2". If "equal" is set, then include the domain
4585 * elements where they are equal.
4586 * Do this for the case where there are no entries.
4587 * In this case, "ma1" cannot be greater than "ma2",
4588 * but it is (greater than or) equal to "ma2".
4590 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4591 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4595 space
= isl_multi_aff_get_domain_space(ma1
);
4597 isl_multi_aff_free(ma1
);
4598 isl_multi_aff_free(ma2
);
4601 return isl_set_universe(space
);
4603 return isl_set_empty(space
);
4606 /* Return the set where entry "i" of "ma1" and "ma2"
4607 * satisfy the relation prescribed by "cmp".
4609 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4610 __isl_keep isl_multi_aff
*ma2
, int i
,
4611 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4612 __isl_take isl_aff
*aff2
))
4614 isl_aff
*aff1
, *aff2
;
4616 aff1
= isl_multi_aff_get_at(ma1
, i
);
4617 aff2
= isl_multi_aff_get_at(ma2
, i
);
4618 return cmp(aff1
, aff2
);
4621 /* Return the set of domain elements where "ma1" is lexicographically
4622 * greater than to "ma2". If "equal" is set, then include the domain
4623 * elements where they are equal.
4625 * In particular, for all but the final entry,
4626 * include the set of elements where this entry is strictly greater in "ma1"
4627 * and all previous entries are equal.
4628 * The final entry is also allowed to be equal in the two functions
4629 * if "equal" is set.
4631 * The case where there are no entries is handled separately.
4633 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4634 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4643 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4645 n
= isl_multi_aff_size(ma1
);
4649 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4651 space
= isl_multi_aff_get_domain_space(ma1
);
4652 res
= isl_set_empty(isl_space_copy(space
));
4653 equal_set
= isl_set_universe(space
);
4655 for (i
= 0; i
+ 1 < n
; ++i
) {
4659 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4660 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4661 res
= isl_set_union(res
, gt
);
4662 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4663 equal_set
= isl_set_intersect(equal_set
, eq
);
4665 empty
= isl_set_is_empty(equal_set
);
4666 if (empty
>= 0 && empty
)
4671 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4673 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4674 isl_multi_aff_free(ma1
);
4675 isl_multi_aff_free(ma2
);
4677 gte
= isl_set_intersect(gte
, equal_set
);
4678 return isl_set_union(res
, gte
);
4680 isl_multi_aff_free(ma1
);
4681 isl_multi_aff_free(ma2
);
4685 /* Return the set of domain elements where "ma1" is lexicographically
4686 * greater than or equal to "ma2".
4688 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4689 __isl_take isl_multi_aff
*ma2
)
4691 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4694 /* Return the set of domain elements where "ma1" is lexicographically
4695 * greater than "ma2".
4697 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4698 __isl_take isl_multi_aff
*ma2
)
4700 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4703 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4706 #define PW isl_pw_multi_aff
4708 #define BASE multi_aff
4710 #define EL_IS_ZERO is_empty
4714 #define IS_ZERO is_empty
4717 #undef DEFAULT_IS_ZERO
4718 #define DEFAULT_IS_ZERO 0
4720 #include <isl_pw_templ.c>
4721 #include <isl_pw_un_op_templ.c>
4722 #include <isl_pw_add_constant_multi_val_templ.c>
4723 #include <isl_pw_add_constant_val_templ.c>
4724 #include <isl_pw_add_disjoint_templ.c>
4725 #include <isl_pw_bind_domain_templ.c>
4726 #include <isl_pw_domain_reverse_templ.c>
4727 #include <isl_pw_fix_templ.c>
4728 #include <isl_pw_from_range_templ.c>
4729 #include <isl_pw_insert_dims_templ.c>
4730 #include <isl_pw_insert_domain_templ.c>
4731 #include <isl_pw_locals_templ.c>
4732 #include <isl_pw_move_dims_templ.c>
4733 #include <isl_pw_neg_templ.c>
4734 #include <isl_pw_pullback_templ.c>
4735 #include <isl_pw_range_tuple_id_templ.c>
4736 #include <isl_pw_union_opt.c>
4739 #define BASE pw_multi_aff
4741 #include <isl_union_multi.c>
4742 #include "isl_union_locals_templ.c"
4743 #include <isl_union_neg.c>
4744 #include <isl_union_sub_templ.c>
4747 #define BASE multi_aff
4749 #include <isl_union_pw_templ.c>
4751 /* Generic function for extracting a factor from a product "pma".
4752 * "check_space" checks that the space is that of the right kind of product.
4753 * "space_factor" extracts the factor from the space.
4754 * "multi_aff_factor" extracts the factor from the constituent functions.
4756 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4757 __isl_take isl_pw_multi_aff
*pma
,
4758 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4759 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4760 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4761 __isl_take isl_multi_aff
*ma
))
4766 if (check_space(pma
) < 0)
4767 return isl_pw_multi_aff_free(pma
);
4769 space
= isl_pw_multi_aff_take_space(pma
);
4770 space
= space_factor(space
);
4772 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4775 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4776 ma
= multi_aff_factor(ma
);
4777 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4780 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4785 /* Is the range of "pma" a wrapped relation?
4787 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4788 __isl_keep isl_pw_multi_aff
*pma
)
4790 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4793 /* Check that the range of "pma" is a product.
4795 static isl_stat
pw_multi_aff_check_range_product(
4796 __isl_keep isl_pw_multi_aff
*pma
)
4800 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4802 return isl_stat_error
;
4804 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4805 "range is not a product", return isl_stat_error
);
4809 /* Given a function A -> [B -> C], extract the function A -> B.
4811 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4812 __isl_take isl_pw_multi_aff
*pma
)
4814 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4815 &isl_space_range_factor_domain
,
4816 &isl_multi_aff_range_factor_domain
);
4819 /* Given a function A -> [B -> C], extract the function A -> C.
4821 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4822 __isl_take isl_pw_multi_aff
*pma
)
4824 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4825 &isl_space_range_factor_range
,
4826 &isl_multi_aff_range_factor_range
);
4829 /* Given two piecewise multi affine expressions, return a piecewise
4830 * multi-affine expression defined on the union of the definition domains
4831 * of the inputs that is equal to the lexicographic maximum of the two
4832 * inputs on each cell. If only one of the two inputs is defined on
4833 * a given cell, then it is considered to be the maximum.
4835 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4836 __isl_take isl_pw_multi_aff
*pma1
,
4837 __isl_take isl_pw_multi_aff
*pma2
)
4839 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4840 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4841 &isl_multi_aff_lex_ge_set
);
4844 /* Given two piecewise multi affine expressions, return a piecewise
4845 * multi-affine expression defined on the union of the definition domains
4846 * of the inputs that is equal to the lexicographic minimum of the two
4847 * inputs on each cell. If only one of the two inputs is defined on
4848 * a given cell, then it is considered to be the minimum.
4850 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4851 __isl_take isl_pw_multi_aff
*pma1
,
4852 __isl_take isl_pw_multi_aff
*pma2
)
4854 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4855 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4856 &isl_multi_aff_lex_le_set
);
4859 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4860 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4862 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4863 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4864 &isl_multi_aff_add
);
4867 /* Subtract "pma2" from "pma1" and return the result.
4869 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4870 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4872 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4873 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4874 &isl_multi_aff_sub
);
4877 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4878 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4880 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4881 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4885 isl_pw_multi_aff
*res
;
4887 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4890 n
= pma1
->n
* pma2
->n
;
4891 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4892 isl_space_copy(pma2
->dim
));
4893 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4895 for (i
= 0; i
< pma1
->n
; ++i
) {
4896 for (j
= 0; j
< pma2
->n
; ++j
) {
4900 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4901 isl_set_copy(pma2
->p
[j
].set
));
4902 ma
= isl_multi_aff_product(
4903 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4904 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4905 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4909 isl_pw_multi_aff_free(pma1
);
4910 isl_pw_multi_aff_free(pma2
);
4913 isl_pw_multi_aff_free(pma1
);
4914 isl_pw_multi_aff_free(pma2
);
4918 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4919 * denominator "denom".
4920 * "denom" is allowed to be negative, in which case the actual denominator
4921 * is -denom and the expressions are added instead.
4923 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4924 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4930 first
= isl_seq_first_non_zero(c
, n
);
4934 sign
= isl_int_sgn(denom
);
4936 isl_int_abs(d
, denom
);
4937 for (i
= first
; i
< n
; ++i
) {
4940 if (isl_int_is_zero(c
[i
]))
4942 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4943 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4944 aff_i
= isl_aff_scale_down(aff_i
, d
);
4946 aff
= isl_aff_sub(aff
, aff_i
);
4948 aff
= isl_aff_add(aff
, aff_i
);
4955 /* Extract an affine expression that expresses the output dimension "pos"
4956 * of "bmap" in terms of the parameters and input dimensions from
4958 * Note that this expression may involve integer divisions defined
4959 * in terms of parameters and input dimensions.
4960 * The equality may also involve references to earlier (but not later)
4961 * output dimensions. These are replaced by the corresponding elements
4964 * If the equality is of the form
4966 * f(i) + h(j) + a x + g(i) = 0,
4968 * with f(i) a linear combinations of the parameters and input dimensions,
4969 * g(i) a linear combination of integer divisions defined in terms of the same
4970 * and h(j) a linear combinations of earlier output dimensions,
4971 * then the affine expression is
4973 * (-f(i) - g(i))/a - h(j)/a
4975 * If the equality is of the form
4977 * f(i) + h(j) - a x + g(i) = 0,
4979 * then the affine expression is
4981 * (f(i) + g(i))/a - h(j)/(-a)
4984 * If "div" refers to an integer division (i.e., it is smaller than
4985 * the number of integer divisions), then the equality constraint
4986 * does involve an integer division (the one at position "div") that
4987 * is defined in terms of output dimensions. However, this integer
4988 * division can be eliminated by exploiting a pair of constraints
4989 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4990 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4992 * In particular, let
4994 * x = e(i) + m floor(...)
4996 * with e(i) the expression derived above and floor(...) the integer
4997 * division involving output dimensions.
5008 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
5009 * = (e(i) - l) mod m
5013 * x - l = (e(i) - l) mod m
5017 * x = ((e(i) - l) mod m) + l
5019 * The variable "shift" below contains the expression -l, which may
5020 * also involve a linear combination of earlier output dimensions.
5022 static __isl_give isl_aff
*extract_aff_from_equality(
5023 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
5024 __isl_keep isl_multi_aff
*ma
)
5027 isl_size n_div
, n_out
;
5029 isl_local_space
*ls
;
5030 isl_aff
*aff
, *shift
;
5033 ctx
= isl_basic_map_get_ctx(bmap
);
5034 ls
= isl_basic_map_get_local_space(bmap
);
5035 ls
= isl_local_space_domain(ls
);
5036 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5039 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
5040 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5041 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5042 if (n_out
< 0 || n_div
< 0)
5044 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
5045 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
5046 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
5047 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
5049 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
5050 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
5051 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
5054 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
5055 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
5056 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
5057 bmap
->eq
[eq
][o_out
+ pos
]);
5059 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
5062 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
5063 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
5064 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
5065 isl_int_set_si(shift
->v
->el
[0], 1);
5066 shift
= subtract_initial(shift
, ma
, pos
,
5067 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
5068 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
5069 mod
= isl_val_int_from_isl_int(ctx
,
5070 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
5071 mod
= isl_val_abs(mod
);
5072 aff
= isl_aff_mod_val(aff
, mod
);
5073 aff
= isl_aff_sub(aff
, shift
);
5076 isl_local_space_free(ls
);
5079 isl_local_space_free(ls
);
5084 /* Given a basic map with output dimensions defined
5085 * in terms of the parameters input dimensions and earlier
5086 * output dimensions using an equality (and possibly a pair on inequalities),
5087 * extract an isl_aff that expresses output dimension "pos" in terms
5088 * of the parameters and input dimensions.
5089 * Note that this expression may involve integer divisions defined
5090 * in terms of parameters and input dimensions.
5091 * "ma" contains the expressions corresponding to earlier output dimensions.
5093 * This function shares some similarities with
5094 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
5096 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
5097 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
5104 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
5105 if (eq
>= bmap
->n_eq
)
5106 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5107 "unable to find suitable equality", return NULL
);
5108 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
5110 aff
= isl_aff_remove_unused_divs(aff
);
5114 /* Given a basic map where each output dimension is defined
5115 * in terms of the parameters and input dimensions using an equality,
5116 * extract an isl_multi_aff that expresses the output dimensions in terms
5117 * of the parameters and input dimensions.
5119 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
5120 __isl_take isl_basic_map
*bmap
)
5129 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
5130 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5132 ma
= isl_multi_aff_free(ma
);
5134 for (i
= 0; i
< n_out
; ++i
) {
5137 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
5138 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5141 isl_basic_map_free(bmap
);
5146 /* Given a basic set where each set dimension is defined
5147 * in terms of the parameters using an equality,
5148 * extract an isl_multi_aff that expresses the set dimensions in terms
5149 * of the parameters.
5151 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
5152 __isl_take isl_basic_set
*bset
)
5154 return extract_isl_multi_aff_from_basic_map(bset
);
5157 /* Create an isl_pw_multi_aff that is equivalent to
5158 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5159 * The given basic map is such that each output dimension is defined
5160 * in terms of the parameters and input dimensions using an equality.
5162 * Since some applications expect the result of isl_pw_multi_aff_from_map
5163 * to only contain integer affine expressions, we compute the floor
5164 * of the expression before returning.
5166 * Remove all constraints involving local variables without
5167 * an explicit representation (resulting in the removal of those
5168 * local variables) prior to the actual extraction to ensure
5169 * that the local spaces in which the resulting affine expressions
5170 * are created do not contain any unknown local variables.
5171 * Removing such constraints is safe because constraints involving
5172 * unknown local variables are not used to determine whether
5173 * a basic map is obviously single-valued.
5175 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
5176 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
5180 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
5181 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
5182 ma
= isl_multi_aff_floor(ma
);
5183 return isl_pw_multi_aff_alloc(domain
, ma
);
5186 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5187 * This obviously only works if the input "map" is single-valued.
5188 * If so, we compute the lexicographic minimum of the image in the form
5189 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5190 * to its lexicographic minimum.
5191 * If the input is not single-valued, we produce an error.
5193 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
5194 __isl_take isl_map
*map
)
5198 isl_pw_multi_aff
*pma
;
5200 sv
= isl_map_is_single_valued(map
);
5204 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5205 "map is not single-valued", goto error
);
5206 map
= isl_map_make_disjoint(map
);
5210 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5212 for (i
= 0; i
< map
->n
; ++i
) {
5213 isl_pw_multi_aff
*pma_i
;
5214 isl_basic_map
*bmap
;
5215 bmap
= isl_basic_map_copy(map
->p
[i
]);
5216 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5217 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5227 /* Construct an isl_aff from the given domain local space "ls" and
5228 * coefficients "v", where the local space may involve
5229 * local variables without a known expression, as long as these
5230 * do not have a non-zero coefficient in "v".
5231 * These need to be pruned away first since an isl_aff cannot
5232 * reference any local variables without a known expression.
5233 * For simplicity, remove all local variables that have a zero coefficient and
5234 * that are not used in other local variables with a non-zero coefficient.
5236 static __isl_give isl_aff
*isl_aff_alloc_vec_prune(
5237 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
5240 isl_size n_div
, v_div
;
5242 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
5243 v_div
= isl_local_space_var_offset(ls
, isl_dim_div
);
5244 if (n_div
< 0 || v_div
< 0 || !v
)
5246 for (i
= n_div
- 1; i
>= 0; --i
) {
5249 if (!isl_int_is_zero(v
->el
[1 + 1 + v_div
+ i
]))
5251 involves
= isl_local_space_involves_dims(ls
, isl_dim_div
, i
, 1);
5256 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
5257 v
= isl_vec_drop_els(v
, 1 + 1 + v_div
+ i
, 1);
5262 return isl_aff_alloc_vec(ls
, v
);
5264 isl_local_space_free(ls
);
5269 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5270 * taking into account that the output dimension at position "d"
5271 * is equal to some expression f in the parameters and input dimensions
5272 * represented by "aff".
5274 * Let "map" be of the form
5278 * Construct a mapping
5282 * apply that to the map, obtaining
5286 * and equate dimension "d" to x.
5287 * An isl_pw_multi_aff representation of this map is then computed and
5288 * the above expression is plugged in in the result.
5290 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_plug_in(
5291 __isl_take isl_map
*map
, int d
, __isl_take isl_aff
*aff
)
5296 isl_pw_multi_aff
*pma
;
5299 is_set
= isl_map_is_set(map
);
5303 n_in
= is_set
? 0 : isl_map_dim(map
, isl_dim_in
);
5308 ma
= isl_multi_aff_from_aff(aff
);
5312 space
= isl_space_domain(isl_map_get_space(map
));
5313 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5314 ma
= isl_multi_aff_range_product(ma
,
5315 isl_multi_aff_from_aff(aff
));
5318 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5319 map
= isl_map_apply_domain(map
, insert
);
5320 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5321 pma
= isl_pw_multi_aff_from_map(map
);
5322 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5331 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5332 * taking into account that the output dimension at position "d"
5333 * can be represented as
5335 * x = floor((e(...) + c1) / m)
5337 * given that constraint "i" is of the form
5339 * e(...) + c1 - m x >= 0
5341 * with e(...) an expression that does not involve any other output dimensions.
5344 * Let "map" be of the form
5348 * Create an expression
5350 * A -> x = floor(...)
5352 * and continue with output dimension "d" equated to this expression,
5353 * plugging it back in afterwards.
5355 * The constraint "i" is guaranteed by the caller not to involve
5356 * any local variables without a known expression, but such local variables
5357 * may appear in other constraints. They therefore need to be removed
5358 * during the construction of the affine expression.
5360 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5361 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5363 isl_local_space
*ls
;
5368 is_set
= isl_map_is_set(map
);
5370 hull
= isl_basic_map_free(hull
);
5372 ls
= isl_basic_map_get_local_space(hull
);
5374 ls
= isl_local_space_wrap(ls
);
5375 v
= isl_basic_map_inequality_extract_output_upper_bound(hull
, i
, d
);
5376 isl_basic_map_free(hull
);
5378 aff
= isl_aff_alloc_vec_prune(ls
, v
);
5379 aff
= isl_aff_floor(aff
);
5382 aff
= isl_aff_project_domain_on_params(aff
);
5384 aff
= isl_aff_domain_factor_domain(aff
);
5386 return pw_multi_aff_from_map_plug_in(map
, d
, aff
);
5389 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5391 * As a special case, we first check if there is any pair of constraints,
5392 * shared by all the basic maps in "map" that force a given dimension
5393 * to be equal to the floor of some affine combination of the input dimensions.
5395 * Sort the constraints first to make it easier to find such pairs
5398 * In particular, if we can find two constraints
5400 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5404 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5406 * where m > 1 and e only depends on parameters and input dimensions,
5409 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5411 * then we know that we can take
5413 * x = floor((e(...) + c1) / m)
5415 * without having to perform any computation.
5417 * Note that we know that
5421 * If c1 + c2 were 0, then we would have detected an equality during
5422 * simplification. If c1 + c2 were negative, then we would have detected
5425 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5426 __isl_take isl_map
*map
)
5432 isl_basic_map
*hull
;
5434 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5435 hull
= isl_basic_map_sort_constraints(hull
);
5436 dim
= isl_map_dim(map
, isl_dim_out
);
5437 n_ineq
= isl_basic_map_n_inequality(hull
);
5438 if (dim
< 0 || n_ineq
< 0)
5441 dim
= isl_map_dim(map
, isl_dim_out
);
5442 for (d
= 0; d
< dim
; ++d
) {
5443 i
= isl_basic_map_find_output_upper_div_constraint(hull
, d
);
5448 return pw_multi_aff_from_map_div(map
, hull
, d
, i
);
5450 isl_basic_map_free(hull
);
5451 return pw_multi_aff_from_map_base(map
);
5454 isl_basic_map_free(hull
);
5458 /* Given an affine expression
5460 * [A -> B] -> f(A,B)
5462 * construct an isl_multi_aff
5466 * such that dimension "d" in B' is set to "aff" and the remaining
5467 * dimensions are set equal to the corresponding dimensions in B.
5468 * "n_in" is the dimension of the space A.
5469 * "n_out" is the dimension of the space B.
5471 * If "is_set" is set, then the affine expression is of the form
5475 * and we construct an isl_multi_aff
5479 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5480 unsigned n_in
, unsigned n_out
, int is_set
)
5484 isl_space
*space
, *space2
;
5485 isl_local_space
*ls
;
5487 space
= isl_aff_get_domain_space(aff
);
5488 ls
= isl_local_space_from_space(isl_space_copy(space
));
5489 space2
= isl_space_copy(space
);
5491 space2
= isl_space_range(isl_space_unwrap(space2
));
5492 space
= isl_space_map_from_domain_and_range(space
, space2
);
5493 ma
= isl_multi_aff_alloc(space
);
5494 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5496 for (i
= 0; i
< n_out
; ++i
) {
5499 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5500 isl_dim_set
, n_in
+ i
);
5501 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5504 isl_local_space_free(ls
);
5509 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5510 * taking into account that the dimension at position "d" can be written as
5512 * x = m a + f(..) (1)
5514 * where m is equal to "gcd".
5515 * "i" is the index of the equality in "hull" that defines f(..).
5516 * In particular, the equality is of the form
5518 * f(..) - x + m g(existentials) = 0
5522 * -f(..) + x + m g(existentials) = 0
5524 * We basically plug (1) into "map", resulting in a map with "a"
5525 * in the range instead of "x". The corresponding isl_pw_multi_aff
5526 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5528 * Specifically, given the input map
5532 * We first wrap it into a set
5536 * and define (1) on top of the corresponding space, resulting in "aff".
5537 * We use this to create an isl_multi_aff that maps the output position "d"
5538 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5539 * We plug this into the wrapped map, unwrap the result and compute the
5540 * corresponding isl_pw_multi_aff.
5541 * The result is an expression
5549 * so that we can plug that into "aff", after extending the latter to
5555 * If "map" is actually a set, then there is no "A" space, meaning
5556 * that we do not need to perform any wrapping, and that the result
5557 * of the recursive call is of the form
5561 * which is plugged into a mapping of the form
5565 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5566 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5571 isl_local_space
*ls
;
5574 isl_pw_multi_aff
*pma
, *id
;
5580 is_set
= isl_map_is_set(map
);
5584 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5585 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5586 if (n_in
< 0 || n_out
< 0)
5588 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5593 set
= isl_map_wrap(map
);
5594 space
= isl_space_map_from_set(isl_set_get_space(set
));
5595 ma
= isl_multi_aff_identity(space
);
5596 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5597 aff
= isl_aff_alloc(ls
);
5599 isl_int_set_si(aff
->v
->el
[0], 1);
5600 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5601 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5604 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5606 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5608 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5609 set
= isl_set_preimage_multi_aff(set
, ma
);
5611 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5616 map
= isl_set_unwrap(set
);
5617 pma
= isl_pw_multi_aff_from_map(map
);
5620 space
= isl_pw_multi_aff_get_domain_space(pma
);
5621 space
= isl_space_map_from_set(space
);
5622 id
= isl_pw_multi_aff_identity(space
);
5623 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5625 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5626 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5628 isl_basic_map_free(hull
);
5632 isl_basic_map_free(hull
);
5636 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5637 * "hull" contains the equalities valid for "map".
5639 * Check if any of the output dimensions is "strided".
5640 * That is, we check if it can be written as
5644 * with m greater than 1, a some combination of existentially quantified
5645 * variables and f an expression in the parameters and input dimensions.
5646 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5648 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5651 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5652 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5661 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5662 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5663 if (n_div
< 0 || n_out
< 0)
5667 isl_basic_map_free(hull
);
5668 return pw_multi_aff_from_map_check_div(map
);
5673 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5674 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5676 for (i
= 0; i
< n_out
; ++i
) {
5677 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5678 isl_int
*eq
= hull
->eq
[j
];
5679 isl_pw_multi_aff
*res
;
5681 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5682 !isl_int_is_negone(eq
[o_out
+ i
]))
5684 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5686 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5687 n_out
- (i
+ 1)) != -1)
5689 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5690 if (isl_int_is_zero(gcd
))
5692 if (isl_int_is_one(gcd
))
5695 res
= pw_multi_aff_from_map_stride(map
, hull
,
5703 isl_basic_map_free(hull
);
5704 return pw_multi_aff_from_map_check_div(map
);
5707 isl_basic_map_free(hull
);
5711 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5713 * As a special case, we first check if all output dimensions are uniquely
5714 * defined in terms of the parameters and input dimensions over the entire
5715 * domain. If so, we extract the desired isl_pw_multi_aff directly
5716 * from the affine hull of "map" and its domain.
5718 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5721 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5725 isl_basic_map
*hull
;
5727 n
= isl_map_n_basic_map(map
);
5732 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5733 hull
= isl_basic_map_plain_affine_hull(hull
);
5734 sv
= isl_basic_map_plain_is_single_valued(hull
);
5736 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5738 isl_basic_map_free(hull
);
5740 map
= isl_map_detect_equalities(map
);
5741 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5742 sv
= isl_basic_map_plain_is_single_valued(hull
);
5744 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5746 return pw_multi_aff_from_map_check_strides(map
, hull
);
5747 isl_basic_map_free(hull
);
5753 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5754 * but is considered as a function on an isl_map when exported.
5756 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5758 return isl_pw_multi_aff_from_map(map
);
5761 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5763 return isl_pw_multi_aff_from_map(set
);
5766 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5767 * but is considered as a function on an isl_set when exported.
5769 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5771 return isl_pw_multi_aff_from_set(set
);
5774 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5777 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5779 isl_union_pw_multi_aff
**upma
= user
;
5780 isl_pw_multi_aff
*pma
;
5782 pma
= isl_pw_multi_aff_from_map(map
);
5783 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5785 return *upma
? isl_stat_ok
: isl_stat_error
;
5788 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5791 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5792 __isl_take isl_aff
*aff
)
5795 isl_pw_multi_aff
*pma
;
5797 ma
= isl_multi_aff_from_aff(aff
);
5798 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5799 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5802 /* Try and create an isl_union_pw_multi_aff that is equivalent
5803 * to the given isl_union_map.
5804 * The isl_union_map is required to be single-valued in each space.
5805 * Otherwise, an error is produced.
5807 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5808 __isl_take isl_union_map
*umap
)
5811 isl_union_pw_multi_aff
*upma
;
5813 space
= isl_union_map_get_space(umap
);
5814 upma
= isl_union_pw_multi_aff_empty(space
);
5815 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5816 upma
= isl_union_pw_multi_aff_free(upma
);
5817 isl_union_map_free(umap
);
5822 /* This function performs the same operation as
5823 * isl_union_pw_multi_aff_from_union_map,
5824 * but is considered as a function on an isl_union_map when exported.
5826 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5827 __isl_take isl_union_map
*umap
)
5829 return isl_union_pw_multi_aff_from_union_map(umap
);
5832 /* Try and create an isl_union_pw_multi_aff that is equivalent
5833 * to the given isl_union_set.
5834 * The isl_union_set is required to be a singleton in each space.
5835 * Otherwise, an error is produced.
5837 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5838 __isl_take isl_union_set
*uset
)
5840 return isl_union_pw_multi_aff_from_union_map(uset
);
5843 /* Return the piecewise affine expression "set ? 1 : 0".
5845 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5848 isl_space
*space
= isl_set_get_space(set
);
5849 isl_local_space
*ls
= isl_local_space_from_space(space
);
5850 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5851 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5853 one
= isl_aff_add_constant_si(one
, 1);
5854 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5855 set
= isl_set_complement(set
);
5856 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5861 /* Plug in "subs" for dimension "type", "pos" of "aff".
5863 * Let i be the dimension to replace and let "subs" be of the form
5867 * and "aff" of the form
5873 * (a f + d g')/(m d)
5875 * where g' is the result of plugging in "subs" in each of the integer
5878 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5879 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5885 aff
= isl_aff_cow(aff
);
5887 return isl_aff_free(aff
);
5889 ctx
= isl_aff_get_ctx(aff
);
5890 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5891 isl_die(ctx
, isl_error_invalid
,
5892 "spaces don't match", return isl_aff_free(aff
));
5893 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5895 return isl_aff_free(aff
);
5897 isl_die(ctx
, isl_error_unsupported
,
5898 "cannot handle divs yet", return isl_aff_free(aff
));
5900 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5902 return isl_aff_free(aff
);
5904 aff
->v
= isl_vec_cow(aff
->v
);
5906 return isl_aff_free(aff
);
5908 pos
+= isl_local_space_offset(aff
->ls
, type
);
5911 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5912 aff
->v
->size
, subs
->v
->size
, v
);
5918 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5919 * expressions in "maff".
5921 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5922 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5923 __isl_keep isl_aff
*subs
)
5928 n
= isl_multi_aff_size(maff
);
5930 return isl_multi_aff_free(maff
);
5932 if (type
== isl_dim_in
)
5935 for (i
= 0; i
< n
; ++i
) {
5938 aff
= isl_multi_aff_take_at(maff
, i
);
5939 aff
= isl_aff_substitute(aff
, type
, pos
, subs
);
5940 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
5946 /* Plug in "subs" for input dimension "pos" of "pma".
5948 * pma is of the form
5952 * while subs is of the form
5954 * v' = B_j(v) -> S_j
5956 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5957 * has a contribution in the result, in particular
5959 * C_ij(S_j) -> M_i(S_j)
5961 * Note that plugging in S_j in C_ij may also result in an empty set
5962 * and this contribution should simply be discarded.
5964 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5965 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5966 __isl_keep isl_pw_aff
*subs
)
5969 isl_pw_multi_aff
*res
;
5972 return isl_pw_multi_aff_free(pma
);
5974 n
= pma
->n
* subs
->n
;
5975 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5977 for (i
= 0; i
< pma
->n
; ++i
) {
5978 for (j
= 0; j
< subs
->n
; ++j
) {
5980 isl_multi_aff
*res_ij
;
5983 common
= isl_set_intersect(
5984 isl_set_copy(pma
->p
[i
].set
),
5985 isl_set_copy(subs
->p
[j
].set
));
5986 common
= isl_set_substitute(common
,
5987 pos
, subs
->p
[j
].aff
);
5988 empty
= isl_set_plain_is_empty(common
);
5989 if (empty
< 0 || empty
) {
5990 isl_set_free(common
);
5996 res_ij
= isl_multi_aff_substitute(
5997 isl_multi_aff_copy(pma
->p
[i
].maff
),
5998 isl_dim_in
, pos
, subs
->p
[j
].aff
);
6000 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6004 isl_pw_multi_aff_free(pma
);
6007 isl_pw_multi_aff_free(pma
);
6008 isl_pw_multi_aff_free(res
);
6012 /* Compute the preimage of a range of dimensions in the affine expression "src"
6013 * under "ma" and put the result in "dst". The number of dimensions in "src"
6014 * that precede the range is given by "n_before". The number of dimensions
6015 * in the range is given by the number of output dimensions of "ma".
6016 * The number of dimensions that follow the range is given by "n_after".
6017 * If "has_denom" is set (to one),
6018 * then "src" and "dst" have an extra initial denominator.
6019 * "n_div_ma" is the number of existentials in "ma"
6020 * "n_div_bset" is the number of existentials in "src"
6021 * The resulting "dst" (which is assumed to have been allocated by
6022 * the caller) contains coefficients for both sets of existentials,
6023 * first those in "ma" and then those in "src".
6024 * f, c1, c2 and g are temporary objects that have been initialized
6027 * Let src represent the expression
6029 * (a(p) + f_u u + b v + f_w w + c(divs))/d
6031 * and let ma represent the expressions
6033 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
6035 * We start out with the following expression for dst:
6037 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
6039 * with the multiplication factor f initially equal to 1
6040 * and f \sum_i b_i v_i kept separately.
6041 * For each x_i that we substitute, we multiply the numerator
6042 * (and denominator) of dst by c_1 = m_i and add the numerator
6043 * of the x_i expression multiplied by c_2 = f b_i,
6044 * after removing the common factors of c_1 and c_2.
6045 * The multiplication factor f also needs to be multiplied by c_1
6046 * for the next x_j, j > i.
6048 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
6049 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
6050 int n_div_ma
, int n_div_bmap
,
6051 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
6054 isl_size n_param
, n_in
, n_out
;
6057 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
6058 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
6059 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
6060 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
6061 return isl_stat_error
;
6063 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
6064 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
6065 isl_seq_clr(dst
+ o_dst
, n_in
);
6068 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
6071 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
6073 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
6075 isl_int_set_si(f
, 1);
6077 for (i
= 0; i
< n_out
; ++i
) {
6078 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
6080 if (isl_int_is_zero(src
[offset
]))
6082 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
6083 isl_int_mul(c2
, f
, src
[offset
]);
6084 isl_int_gcd(g
, c1
, c2
);
6085 isl_int_divexact(c1
, c1
, g
);
6086 isl_int_divexact(c2
, c2
, g
);
6088 isl_int_mul(f
, f
, c1
);
6091 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6092 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
6093 o_dst
+= 1 + n_param
;
6094 o_src
+= 1 + n_param
;
6095 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
6097 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6098 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
6101 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
6103 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6104 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
6107 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
6109 isl_int_mul(dst
[0], dst
[0], c1
);
6115 /* Compute the pullback of "aff" by the function represented by "ma".
6116 * In other words, plug in "ma" in "aff". The result is an affine expression
6117 * defined over the domain space of "ma".
6119 * If "aff" is represented by
6121 * (a(p) + b x + c(divs))/d
6123 * and ma is represented by
6125 * x = D(p) + F(y) + G(divs')
6127 * then the result is
6129 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
6131 * The divs in the local space of the input are similarly adjusted
6132 * through a call to isl_local_space_preimage_multi_aff.
6134 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
6135 __isl_take isl_multi_aff
*ma
)
6137 isl_aff
*res
= NULL
;
6138 isl_local_space
*ls
;
6139 isl_size n_div_aff
, n_div_ma
;
6140 isl_int f
, c1
, c2
, g
;
6142 ma
= isl_multi_aff_align_divs(ma
);
6146 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
6147 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
6148 if (n_div_aff
< 0 || n_div_ma
< 0)
6151 ls
= isl_aff_get_domain_local_space(aff
);
6152 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
6153 res
= isl_aff_alloc(ls
);
6162 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
6163 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
6164 res
= isl_aff_free(res
);
6172 isl_multi_aff_free(ma
);
6173 res
= isl_aff_normalize(res
);
6177 isl_multi_aff_free(ma
);
6182 /* Compute the pullback of "aff1" by the function represented by "aff2".
6183 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6184 * defined over the domain space of "aff1".
6186 * The domain of "aff1" should match the range of "aff2", which means
6187 * that it should be single-dimensional.
6189 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
6190 __isl_take isl_aff
*aff2
)
6194 ma
= isl_multi_aff_from_aff(aff2
);
6195 return isl_aff_pullback_multi_aff(aff1
, ma
);
6198 /* Compute the pullback of "ma1" by the function represented by "ma2".
6199 * In other words, plug in "ma2" in "ma1".
6201 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
6202 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
6206 isl_space
*space
= NULL
;
6208 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
6209 ma2
= isl_multi_aff_align_divs(ma2
);
6210 n
= isl_multi_aff_size(ma1
);
6214 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
6215 isl_multi_aff_get_space(ma1
));
6217 for (i
= 0; i
< n
; ++i
) {
6220 aff
= isl_multi_aff_take_at(ma1
, i
);
6221 aff
= isl_aff_pullback_multi_aff(aff
, isl_multi_aff_copy(ma2
));
6222 ma1
= isl_multi_aff_restore_at(ma1
, i
, aff
);
6225 ma1
= isl_multi_aff_reset_space(ma1
, space
);
6226 isl_multi_aff_free(ma2
);
6229 isl_space_free(space
);
6230 isl_multi_aff_free(ma2
);
6231 isl_multi_aff_free(ma1
);
6235 /* Extend the local space of "dst" to include the divs
6236 * in the local space of "src".
6238 * If "src" does not have any divs or if the local spaces of "dst" and
6239 * "src" are the same, then no extension is required.
6241 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6242 __isl_keep isl_aff
*src
)
6245 isl_size src_n_div
, dst_n_div
;
6252 return isl_aff_free(dst
);
6254 ctx
= isl_aff_get_ctx(src
);
6255 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6257 return isl_aff_free(dst
);
6259 isl_die(ctx
, isl_error_invalid
,
6260 "spaces don't match", goto error
);
6262 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6263 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6266 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6267 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6268 return isl_aff_free(dst
);
6272 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6273 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6274 if (!exp1
|| (dst_n_div
&& !exp2
))
6277 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6278 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6286 return isl_aff_free(dst
);
6289 /* Adjust the local spaces of the affine expressions in "maff"
6290 * such that they all have the save divs.
6292 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6293 __isl_take isl_multi_aff
*maff
)
6299 n
= isl_multi_aff_size(maff
);
6301 return isl_multi_aff_free(maff
);
6305 aff_0
= isl_multi_aff_take_at(maff
, 0);
6306 for (i
= 1; i
< n
; ++i
) {
6309 aff_i
= isl_multi_aff_peek_at(maff
, i
);
6310 aff_0
= isl_aff_align_divs(aff_0
, aff_i
);
6312 maff
= isl_multi_aff_restore_at(maff
, 0, aff_0
);
6314 aff_0
= isl_multi_aff_peek_at(maff
, 0);
6315 for (i
= 1; i
< n
; ++i
) {
6318 aff_i
= isl_multi_aff_take_at(maff
, i
);
6319 aff_i
= isl_aff_align_divs(aff_i
, aff_0
);
6320 maff
= isl_multi_aff_restore_at(maff
, i
, aff_i
);
6326 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6328 aff
= isl_aff_cow(aff
);
6332 aff
->ls
= isl_local_space_lift(aff
->ls
);
6334 return isl_aff_free(aff
);
6339 /* Lift "maff" to a space with extra dimensions such that the result
6340 * has no more existentially quantified variables.
6341 * If "ls" is not NULL, then *ls is assigned the local space that lies
6342 * at the basis of the lifting applied to "maff".
6344 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6345 __isl_give isl_local_space
**ls
)
6355 n
= isl_multi_aff_size(maff
);
6357 return isl_multi_aff_free(maff
);
6361 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6362 *ls
= isl_local_space_from_space(space
);
6364 return isl_multi_aff_free(maff
);
6369 maff
= isl_multi_aff_align_divs(maff
);
6371 aff
= isl_multi_aff_peek_at(maff
, 0);
6372 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6374 return isl_multi_aff_free(maff
);
6375 space
= isl_multi_aff_get_space(maff
);
6376 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6377 space
= isl_space_extend_domain_with_range(space
,
6378 isl_multi_aff_get_space(maff
));
6379 maff
= isl_multi_aff_restore_space(maff
, space
);
6382 aff
= isl_multi_aff_peek_at(maff
, 0);
6383 *ls
= isl_aff_get_domain_local_space(aff
);
6385 return isl_multi_aff_free(maff
);
6388 for (i
= 0; i
< n
; ++i
) {
6389 aff
= isl_multi_aff_take_at(maff
, i
);
6390 aff
= isl_aff_lift(aff
);
6391 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
6398 #define TYPE isl_pw_multi_aff
6400 #include "check_type_range_templ.c"
6402 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6404 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6405 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6412 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6415 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6419 space
= isl_pw_multi_aff_get_space(pma
);
6420 space
= isl_space_drop_dims(space
, isl_dim_out
,
6421 pos
+ 1, n_out
- pos
- 1);
6422 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6424 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6425 for (i
= 0; i
< pma
->n
; ++i
) {
6427 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6428 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6434 /* This is an alternative name for the function above.
6436 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6437 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6439 return isl_pw_multi_aff_get_at(pma
, pos
);
6442 /* Return an isl_pw_multi_aff with the given "set" as domain and
6443 * an unnamed zero-dimensional range.
6445 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6446 __isl_take isl_set
*set
)
6451 space
= isl_set_get_space(set
);
6452 space
= isl_space_from_domain(space
);
6453 ma
= isl_multi_aff_zero(space
);
6454 return isl_pw_multi_aff_alloc(set
, ma
);
6457 /* Add an isl_pw_multi_aff with the given "set" as domain and
6458 * an unnamed zero-dimensional range to *user.
6460 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6463 isl_union_pw_multi_aff
**upma
= user
;
6464 isl_pw_multi_aff
*pma
;
6466 pma
= isl_pw_multi_aff_from_domain(set
);
6467 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6472 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6473 * an unnamed zero-dimensional range.
6475 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6476 __isl_take isl_union_set
*uset
)
6479 isl_union_pw_multi_aff
*upma
;
6484 space
= isl_union_set_get_space(uset
);
6485 upma
= isl_union_pw_multi_aff_empty(space
);
6487 if (isl_union_set_foreach_set(uset
,
6488 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6491 isl_union_set_free(uset
);
6494 isl_union_set_free(uset
);
6495 isl_union_pw_multi_aff_free(upma
);
6499 /* Local data for bin_entry and the callback "fn".
6501 struct isl_union_pw_multi_aff_bin_data
{
6502 isl_union_pw_multi_aff
*upma2
;
6503 isl_union_pw_multi_aff
*res
;
6504 isl_pw_multi_aff
*pma
;
6505 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6508 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6509 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6511 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6513 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6517 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6519 isl_pw_multi_aff_free(pma
);
6524 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6525 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6526 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6527 * as *entry. The callback should adjust data->res if desired.
6529 static __isl_give isl_union_pw_multi_aff
*bin_op(
6530 __isl_take isl_union_pw_multi_aff
*upma1
,
6531 __isl_take isl_union_pw_multi_aff
*upma2
,
6532 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6535 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6537 space
= isl_union_pw_multi_aff_get_space(upma2
);
6538 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6539 space
= isl_union_pw_multi_aff_get_space(upma1
);
6540 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6542 if (!upma1
|| !upma2
)
6546 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6547 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6548 &bin_entry
, &data
) < 0)
6551 isl_union_pw_multi_aff_free(upma1
);
6552 isl_union_pw_multi_aff_free(upma2
);
6555 isl_union_pw_multi_aff_free(upma1
);
6556 isl_union_pw_multi_aff_free(upma2
);
6557 isl_union_pw_multi_aff_free(data
.res
);
6561 /* Given two isl_pw_multi_affs A -> B and C -> D,
6562 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6564 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6565 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6569 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6570 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6571 isl_pw_multi_aff_get_space(pma2
));
6572 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6573 &isl_multi_aff_range_product
);
6576 /* Given two isl_pw_multi_affs A -> B and C -> D,
6577 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6579 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6580 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6584 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6585 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6586 isl_pw_multi_aff_get_space(pma2
));
6587 space
= isl_space_flatten_range(space
);
6588 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6589 &isl_multi_aff_flat_range_product
);
6592 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6593 * to compute some form of range product and add the result to data->res.
6595 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6596 __isl_give isl_pw_multi_aff
*(*range_product
)(
6597 __isl_take isl_pw_multi_aff
*pma1
,
6598 __isl_take isl_pw_multi_aff
*pma2
),
6601 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6603 isl_space
*space1
, *space2
;
6605 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6606 space2
= isl_pw_multi_aff_peek_space(pma2
);
6607 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6608 space2
, isl_dim_in
);
6609 if (match
< 0 || !match
) {
6610 isl_pw_multi_aff_free(pma2
);
6611 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6614 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6616 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6621 /* If data->pma and "pma2" have the same domain space, then compute
6622 * their flat range product and add the result to data->res.
6624 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6627 return gen_range_product_entry(pma2
,
6628 &isl_pw_multi_aff_flat_range_product
, user
);
6631 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6632 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6634 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6635 __isl_take isl_union_pw_multi_aff
*upma1
,
6636 __isl_take isl_union_pw_multi_aff
*upma2
)
6638 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6641 /* If data->pma and "pma2" have the same domain space, then compute
6642 * their range product and add the result to data->res.
6644 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6647 return gen_range_product_entry(pma2
,
6648 &isl_pw_multi_aff_range_product
, user
);
6651 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6652 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6654 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6655 __isl_take isl_union_pw_multi_aff
*upma1
,
6656 __isl_take isl_union_pw_multi_aff
*upma2
)
6658 return bin_op(upma1
, upma2
, &range_product_entry
);
6661 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6662 * The parameters are assumed to have been aligned.
6664 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6665 * except that it works on two different isl_pw_* types.
6667 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6668 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6669 __isl_take isl_pw_aff
*pa
)
6672 isl_pw_multi_aff
*res
= NULL
;
6677 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6678 pa
->dim
, isl_dim_in
))
6679 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6680 "domains don't match", goto error
);
6681 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6685 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6687 for (i
= 0; i
< pma
->n
; ++i
) {
6688 for (j
= 0; j
< pa
->n
; ++j
) {
6690 isl_multi_aff
*res_ij
;
6693 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6694 isl_set_copy(pa
->p
[j
].set
));
6695 empty
= isl_set_plain_is_empty(common
);
6696 if (empty
< 0 || empty
) {
6697 isl_set_free(common
);
6703 res_ij
= isl_multi_aff_set_aff(
6704 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6705 isl_aff_copy(pa
->p
[j
].aff
));
6706 res_ij
= isl_multi_aff_gist(res_ij
,
6707 isl_set_copy(common
));
6709 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6713 isl_pw_multi_aff_free(pma
);
6714 isl_pw_aff_free(pa
);
6717 isl_pw_multi_aff_free(pma
);
6718 isl_pw_aff_free(pa
);
6719 return isl_pw_multi_aff_free(res
);
6722 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6724 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6725 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6726 __isl_take isl_pw_aff
*pa
)
6728 isl_bool equal_params
;
6732 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6733 if (equal_params
< 0)
6736 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6737 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6738 isl_pw_aff_check_named_params(pa
) < 0)
6740 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6741 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6742 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6744 isl_pw_multi_aff_free(pma
);
6745 isl_pw_aff_free(pa
);
6749 /* Do the parameters of "pa" match those of "space"?
6751 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6752 __isl_keep isl_space
*space
)
6754 isl_space
*pa_space
;
6758 return isl_bool_error
;
6760 pa_space
= isl_pw_aff_get_space(pa
);
6762 match
= isl_space_has_equal_params(space
, pa_space
);
6764 isl_space_free(pa_space
);
6768 /* Check that the domain space of "pa" matches "space".
6770 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6771 __isl_keep isl_space
*space
)
6773 isl_space
*pa_space
;
6777 return isl_stat_error
;
6779 pa_space
= isl_pw_aff_get_space(pa
);
6781 match
= isl_space_has_equal_params(space
, pa_space
);
6785 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6786 "parameters don't match", goto error
);
6787 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6788 pa_space
, isl_dim_in
);
6792 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6793 "domains don't match", goto error
);
6794 isl_space_free(pa_space
);
6797 isl_space_free(pa_space
);
6798 return isl_stat_error
;
6806 #include <isl_multi_explicit_domain.c>
6807 #include <isl_multi_pw_aff_explicit_domain.c>
6808 #include <isl_multi_templ.c>
6809 #include <isl_multi_un_op_templ.c>
6810 #include <isl_multi_bin_val_templ.c>
6811 #include <isl_multi_add_constant_templ.c>
6812 #include <isl_multi_align_set.c>
6813 #include <isl_multi_apply_set_explicit_domain_templ.c>
6814 #include <isl_multi_arith_templ.c>
6815 #include <isl_multi_bind_templ.c>
6816 #include <isl_multi_bind_domain_templ.c>
6817 #include <isl_multi_coalesce.c>
6818 #include <isl_multi_domain_templ.c>
6819 #include <isl_multi_domain_reverse_templ.c>
6820 #include <isl_multi_dim_id_templ.c>
6821 #include <isl_multi_dims.c>
6822 #include <isl_multi_from_base_templ.c>
6823 #include <isl_multi_check_domain_templ.c>
6824 #include <isl_multi_gist.c>
6825 #include <isl_multi_hash.c>
6826 #include <isl_multi_identity_templ.c>
6827 #include <isl_multi_insert_domain_templ.c>
6828 #include <isl_multi_intersect.c>
6829 #include <isl_multi_min_max_templ.c>
6830 #include <isl_multi_move_dims_templ.c>
6831 #include <isl_multi_nan_templ.c>
6832 #include <isl_multi_param_templ.c>
6833 #include <isl_multi_product_templ.c>
6834 #include <isl_multi_splice_templ.c>
6835 #include <isl_multi_tuple_id_templ.c>
6836 #include <isl_multi_union_add_templ.c>
6837 #include <isl_multi_zero_templ.c>
6838 #include <isl_multi_unbind_params_templ.c>
6840 /* Is every element of "mpa" defined over a single universe domain?
6842 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6844 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6847 /* Given that every element of "mpa" is defined over a single universe domain,
6848 * return the corresponding base expressions.
6850 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6851 __isl_take isl_multi_pw_aff
*mpa
)
6857 n
= isl_multi_pw_aff_size(mpa
);
6859 mpa
= isl_multi_pw_aff_free(mpa
);
6860 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6861 for (i
= 0; i
< n
; ++i
) {
6864 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6865 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6867 isl_multi_pw_aff_free(mpa
);
6871 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6872 * with this explicit domain.
6874 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6875 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6879 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6882 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6883 map
= isl_map_intersect_domain(map
, dom
);
6888 /* Are all elements of "mpa" piecewise constants?
6890 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6892 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6895 /* Does "mpa" have a non-trivial explicit domain?
6897 * The explicit domain, if present, is trivial if it represents
6898 * an (obviously) universe set.
6900 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6901 __isl_keep isl_multi_pw_aff
*mpa
)
6904 return isl_bool_error
;
6905 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6906 return isl_bool_false
;
6907 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6913 #include "isl_opt_mpa_templ.c"
6915 /* Compute the minima of the set dimensions as a function of the
6916 * parameters, but independently of the other set dimensions.
6918 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6920 return set_opt_mpa(set
, &isl_set_dim_min
);
6923 /* Compute the maxima of the set dimensions as a function of the
6924 * parameters, but independently of the other set dimensions.
6926 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6928 return set_opt_mpa(set
, &isl_set_dim_max
);
6934 #include "isl_opt_mpa_templ.c"
6936 /* Compute the minima of the output dimensions as a function of the
6937 * parameters and input dimensions, but independently of
6938 * the other output dimensions.
6940 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6942 return map_opt_mpa(map
, &isl_map_dim_min
);
6945 /* Compute the maxima of the output dimensions as a function of the
6946 * parameters and input dimensions, but independently of
6947 * the other output dimensions.
6949 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6951 return map_opt_mpa(map
, &isl_map_dim_max
);
6955 #define TYPE isl_pw_multi_aff
6956 #include "isl_type_check_match_range_multi_val.c"
6958 /* Apply "fn" to the base expressions of "pma" and "mv".
6960 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6961 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6962 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6963 __isl_take isl_multi_val
*mv
))
6968 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6971 n
= isl_pw_multi_aff_n_piece(pma
);
6975 for (i
= 0; i
< n
; ++i
) {
6978 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6979 ma
= fn(ma
, isl_multi_val_copy(mv
));
6980 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6983 isl_multi_val_free(mv
);
6986 isl_multi_val_free(mv
);
6987 isl_pw_multi_aff_free(pma
);
6991 /* Scale the elements of "pma" by the corresponding elements of "mv".
6993 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6994 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6996 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6997 &isl_multi_aff_scale_multi_val
);
7000 /* Scale the elements of "pma" down by the corresponding elements of "mv".
7002 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
7003 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
7005 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
7006 &isl_multi_aff_scale_down_multi_val
);
7009 /* This function is called for each entry of an isl_union_pw_multi_aff.
7010 * If the space of the entry matches that of data->mv,
7011 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
7012 * Otherwise, return an empty isl_pw_multi_aff.
7014 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
7015 __isl_take isl_pw_multi_aff
*pma
, void *user
)
7018 isl_multi_val
*mv
= user
;
7020 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
7022 return isl_pw_multi_aff_free(pma
);
7024 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
7025 isl_pw_multi_aff_free(pma
);
7026 return isl_pw_multi_aff_empty(space
);
7029 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
7032 /* Scale the elements of "upma" by the corresponding elements of "mv",
7033 * for those entries that match the space of "mv".
7035 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
7036 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
7038 struct isl_union_pw_multi_aff_transform_control control
= {
7039 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
7043 upma
= isl_union_pw_multi_aff_align_params(upma
,
7044 isl_multi_val_get_space(mv
));
7045 mv
= isl_multi_val_align_params(mv
,
7046 isl_union_pw_multi_aff_get_space(upma
));
7050 return isl_union_pw_multi_aff_transform(upma
, &control
);
7052 isl_multi_val_free(mv
);
7055 isl_multi_val_free(mv
);
7056 isl_union_pw_multi_aff_free(upma
);
7060 /* Construct and return a piecewise multi affine expression
7061 * in the given space with value zero in each of the output dimensions and
7062 * a universe domain.
7064 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
7066 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
7069 /* Construct and return a piecewise multi affine expression
7070 * that is equal to the given piecewise affine expression.
7072 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
7073 __isl_take isl_pw_aff
*pa
)
7077 isl_pw_multi_aff
*pma
;
7082 space
= isl_pw_aff_get_space(pa
);
7083 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
7085 for (i
= 0; i
< pa
->n
; ++i
) {
7089 set
= isl_set_copy(pa
->p
[i
].set
);
7090 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
7091 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
7094 isl_pw_aff_free(pa
);
7098 /* Construct and return a piecewise multi affine expression
7099 * that is equal to the given multi piecewise affine expression
7100 * on the shared domain of the piecewise affine expressions,
7101 * in the special case of a 0D multi piecewise affine expression.
7103 * Create a piecewise multi affine expression with the explicit domain of
7104 * the 0D multi piecewise affine expression as domain.
7106 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
7107 __isl_take isl_multi_pw_aff
*mpa
)
7113 space
= isl_multi_pw_aff_get_space(mpa
);
7114 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
7115 isl_multi_pw_aff_free(mpa
);
7117 ma
= isl_multi_aff_zero(space
);
7118 return isl_pw_multi_aff_alloc(dom
, ma
);
7121 /* Construct and return a piecewise multi affine expression
7122 * that is equal to the given multi piecewise affine expression
7123 * on the shared domain of the piecewise affine expressions.
7125 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
7126 __isl_take isl_multi_pw_aff
*mpa
)
7131 isl_pw_multi_aff
*pma
;
7137 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
7139 space
= isl_multi_pw_aff_get_space(mpa
);
7140 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
7141 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7143 for (i
= 1; i
< mpa
->n
; ++i
) {
7144 isl_pw_multi_aff
*pma_i
;
7146 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7147 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
7148 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
7151 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
7153 isl_multi_pw_aff_free(mpa
);
7157 /* Convenience function that constructs an isl_multi_pw_aff
7158 * directly from an isl_aff.
7160 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
7162 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
7165 /* Construct and return a multi piecewise affine expression
7166 * that is equal to the given multi affine expression.
7168 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
7169 __isl_take isl_multi_aff
*ma
)
7173 isl_multi_pw_aff
*mpa
;
7175 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
7177 ma
= isl_multi_aff_free(ma
);
7181 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
7183 for (i
= 0; i
< n
; ++i
) {
7186 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
7187 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7190 isl_multi_aff_free(ma
);
7194 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7195 * but is considered as a function on an isl_multi_aff when exported.
7197 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
7198 __isl_take isl_multi_aff
*ma
)
7200 return isl_multi_pw_aff_from_multi_aff(ma
);
7203 /* Construct and return a multi piecewise affine expression
7204 * that is equal to the given piecewise multi affine expression.
7206 * If the resulting multi piecewise affine expression has
7207 * an explicit domain, then assign it the domain of the input.
7208 * In other cases, the domain is stored in the individual elements.
7210 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
7211 __isl_take isl_pw_multi_aff
*pma
)
7216 isl_multi_pw_aff
*mpa
;
7218 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7220 pma
= isl_pw_multi_aff_free(pma
);
7221 space
= isl_pw_multi_aff_get_space(pma
);
7222 mpa
= isl_multi_pw_aff_alloc(space
);
7224 for (i
= 0; i
< n
; ++i
) {
7227 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
7228 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7230 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7233 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
7234 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
7237 isl_pw_multi_aff_free(pma
);
7241 /* This function performs the same operation as
7242 * isl_multi_pw_aff_from_pw_multi_aff,
7243 * but is considered as a function on an isl_pw_multi_aff when exported.
7245 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
7246 __isl_take isl_pw_multi_aff
*pma
)
7248 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7251 /* Do "pa1" and "pa2" represent the same function?
7253 * We first check if they are obviously equal.
7254 * If not, we convert them to maps and check if those are equal.
7256 * If "pa1" or "pa2" contain any NaNs, then they are considered
7257 * not to be the same. A NaN is not equal to anything, not even
7260 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7261 __isl_keep isl_pw_aff
*pa2
)
7265 isl_map
*map1
, *map2
;
7268 return isl_bool_error
;
7270 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7271 if (equal
< 0 || equal
)
7273 has_nan
= either_involves_nan(pa1
, pa2
);
7275 return isl_bool_error
;
7277 return isl_bool_false
;
7279 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7280 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7281 equal
= isl_map_is_equal(map1
, map2
);
7288 /* Do "mpa1" and "mpa2" represent the same function?
7290 * Note that we cannot convert the entire isl_multi_pw_aff
7291 * to a map because the domains of the piecewise affine expressions
7292 * may not be the same.
7294 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7295 __isl_keep isl_multi_pw_aff
*mpa2
)
7298 isl_bool equal
, equal_params
;
7301 return isl_bool_error
;
7303 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7304 if (equal_params
< 0)
7305 return isl_bool_error
;
7306 if (!equal_params
) {
7307 if (!isl_space_has_named_params(mpa1
->space
))
7308 return isl_bool_false
;
7309 if (!isl_space_has_named_params(mpa2
->space
))
7310 return isl_bool_false
;
7311 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7312 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7313 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7314 isl_multi_pw_aff_get_space(mpa2
));
7315 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7316 isl_multi_pw_aff_get_space(mpa1
));
7317 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7318 isl_multi_pw_aff_free(mpa1
);
7319 isl_multi_pw_aff_free(mpa2
);
7323 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7324 if (equal
< 0 || !equal
)
7327 for (i
= 0; i
< mpa1
->n
; ++i
) {
7328 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7329 if (equal
< 0 || !equal
)
7333 return isl_bool_true
;
7336 /* Do "pma1" and "pma2" represent the same function?
7338 * First check if they are obviously equal.
7339 * If not, then convert them to maps and check if those are equal.
7341 * If "pa1" or "pa2" contain any NaNs, then they are considered
7342 * not to be the same. A NaN is not equal to anything, not even
7345 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7346 __isl_keep isl_pw_multi_aff
*pma2
)
7350 isl_map
*map1
, *map2
;
7353 return isl_bool_error
;
7355 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7356 if (equal
< 0 || equal
)
7358 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7359 if (has_nan
>= 0 && !has_nan
)
7360 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7361 if (has_nan
< 0 || has_nan
)
7362 return isl_bool_not(has_nan
);
7364 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7365 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7366 equal
= isl_map_is_equal(map1
, map2
);
7374 #define BASE multi_aff
7376 #include "isl_multi_pw_aff_pullback_templ.c"
7379 #define BASE pw_multi_aff
7381 #include "isl_multi_pw_aff_pullback_templ.c"
7383 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7384 * with the domain of "aff". The domain of the result is the same
7386 * "mpa" and "aff" are assumed to have been aligned.
7388 * We first extract the parametric constant from "aff", defined
7389 * over the correct domain.
7390 * Then we add the appropriate combinations of the members of "mpa".
7391 * Finally, we add the integer divisions through recursive calls.
7393 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7394 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7397 isl_size n_in
, n_div
, n_mpa_in
;
7403 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7404 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7405 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7406 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7409 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7410 tmp
= isl_aff_copy(aff
);
7411 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7412 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7413 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7414 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7415 pa
= isl_pw_aff_from_aff(tmp
);
7417 for (i
= 0; i
< n_in
; ++i
) {
7420 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7422 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7423 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7424 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7425 pa
= isl_pw_aff_add(pa
, pa_i
);
7428 for (i
= 0; i
< n_div
; ++i
) {
7432 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7434 div
= isl_aff_get_div(aff
, i
);
7435 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7436 isl_multi_pw_aff_copy(mpa
), div
);
7437 pa_i
= isl_pw_aff_floor(pa_i
);
7438 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7439 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7440 pa
= isl_pw_aff_add(pa
, pa_i
);
7443 isl_multi_pw_aff_free(mpa
);
7448 isl_multi_pw_aff_free(mpa
);
7453 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7454 * with the domain of "aff". The domain of the result is the same
7457 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7458 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7460 isl_bool equal_params
;
7464 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7465 if (equal_params
< 0)
7468 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7470 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7471 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7473 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7476 isl_multi_pw_aff_free(mpa
);
7480 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7481 * with the domain of "pa". The domain of the result is the same
7483 * "mpa" and "pa" are assumed to have been aligned.
7485 * We consider each piece in turn. Note that the domains of the
7486 * pieces are assumed to be disjoint and they remain disjoint
7487 * after taking the preimage (over the same function).
7489 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7490 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7499 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7500 isl_pw_aff_get_space(pa
));
7501 res
= isl_pw_aff_empty(space
);
7503 for (i
= 0; i
< pa
->n
; ++i
) {
7507 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7508 isl_multi_pw_aff_copy(mpa
),
7509 isl_aff_copy(pa
->p
[i
].aff
));
7510 domain
= isl_set_copy(pa
->p
[i
].set
);
7511 domain
= isl_set_preimage_multi_pw_aff(domain
,
7512 isl_multi_pw_aff_copy(mpa
));
7513 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7514 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7517 isl_pw_aff_free(pa
);
7518 isl_multi_pw_aff_free(mpa
);
7521 isl_pw_aff_free(pa
);
7522 isl_multi_pw_aff_free(mpa
);
7526 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7527 * with the domain of "pa". The domain of the result is the same
7530 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7531 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7533 isl_bool equal_params
;
7537 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7538 if (equal_params
< 0)
7541 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7543 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7544 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7546 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7548 isl_pw_aff_free(pa
);
7549 isl_multi_pw_aff_free(mpa
);
7553 /* Compute the pullback of "pa" by the function represented by "mpa".
7554 * In other words, plug in "mpa" in "pa".
7556 * The pullback is computed by applying "pa" to "mpa".
7558 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7559 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7561 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7565 #define BASE multi_pw_aff
7567 #include "isl_multi_pw_aff_pullback_templ.c"
7569 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7570 * of "mpa1" and "mpa2" live in the same space, construct map space
7571 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7572 * with this map space as extract argument.
7574 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7575 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7576 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7577 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7580 isl_space
*space1
, *space2
;
7583 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7584 isl_multi_pw_aff_get_space(mpa2
));
7585 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7586 isl_multi_pw_aff_get_space(mpa1
));
7589 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7590 mpa2
->space
, isl_dim_out
);
7594 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7595 "range spaces don't match", goto error
);
7596 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7597 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7598 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7600 res
= order(mpa1
, mpa2
, space1
);
7601 isl_multi_pw_aff_free(mpa1
);
7602 isl_multi_pw_aff_free(mpa2
);
7605 isl_multi_pw_aff_free(mpa1
);
7606 isl_multi_pw_aff_free(mpa2
);
7610 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7611 * where the function values are equal. "space" is the space of the result.
7612 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7614 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7615 * in the sequences are equal.
7617 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7618 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7619 __isl_take isl_space
*space
)
7625 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7627 space
= isl_space_free(space
);
7628 res
= isl_map_universe(space
);
7630 for (i
= 0; i
< n
; ++i
) {
7631 isl_pw_aff
*pa1
, *pa2
;
7634 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7635 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7636 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7637 res
= isl_map_intersect(res
, map
);
7643 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7644 * where the function values are equal.
7646 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7647 __isl_take isl_multi_pw_aff
*mpa2
)
7649 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7650 &isl_multi_pw_aff_eq_map_on_space
);
7653 /* Intersect "map" with the result of applying "order"
7654 * on two copies of "mpa".
7656 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7657 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7658 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7659 __isl_take isl_multi_pw_aff
*mpa2
))
7661 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7664 /* Return the subset of "map" where the domain and the range
7665 * have equal "mpa" values.
7667 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7668 __isl_take isl_multi_pw_aff
*mpa
)
7670 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7671 &isl_multi_pw_aff_eq_map
);
7674 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7675 * where the function values of "mpa1" lexicographically satisfies
7676 * "strict_base"/"base" compared to that of "mpa2".
7677 * "space" is the space of the result.
7678 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7680 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7681 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7682 * when compared to the i-th element of "mpa2" while all previous elements are
7684 * In particular, if i corresponds to the final elements
7685 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7686 * for other values of i.
7687 * If "base" is a strict order, then "base" and "strict_base" are the same.
7689 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7690 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7691 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7692 __isl_take isl_pw_aff
*pa2
),
7693 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7694 __isl_take isl_pw_aff
*pa2
),
7695 __isl_take isl_space
*space
)
7699 isl_map
*res
, *rest
;
7701 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7703 space
= isl_space_free(space
);
7704 res
= isl_map_empty(isl_space_copy(space
));
7705 rest
= isl_map_universe(space
);
7707 for (i
= 0; i
< n
; ++i
) {
7709 isl_pw_aff
*pa1
, *pa2
;
7714 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7715 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7716 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7717 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7718 res
= isl_map_union(res
, map
);
7723 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7724 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7725 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7726 rest
= isl_map_intersect(rest
, map
);
7736 #define STRICT_ORDER lt
7737 #include "isl_aff_lex_templ.c"
7742 #define STRICT_ORDER lt
7743 #include "isl_aff_lex_templ.c"
7748 #define STRICT_ORDER gt
7749 #include "isl_aff_lex_templ.c"
7754 #define STRICT_ORDER gt
7755 #include "isl_aff_lex_templ.c"
7757 /* Compare two isl_affs.
7759 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7760 * than "aff2" and 0 if they are equal.
7762 * The order is fairly arbitrary. We do consider expressions that only involve
7763 * earlier dimensions as "smaller".
7765 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7778 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7782 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7783 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7785 return last1
- last2
;
7787 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7790 /* Compare two isl_pw_affs.
7792 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7793 * than "pa2" and 0 if they are equal.
7795 * The order is fairly arbitrary. We do consider expressions that only involve
7796 * earlier dimensions as "smaller".
7798 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7799 __isl_keep isl_pw_aff
*pa2
)
7812 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7816 if (pa1
->n
!= pa2
->n
)
7817 return pa1
->n
- pa2
->n
;
7819 for (i
= 0; i
< pa1
->n
; ++i
) {
7820 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7823 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7831 /* Return a piecewise affine expression that is equal to "v" on "domain".
7833 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7834 __isl_take isl_val
*v
)
7837 isl_local_space
*ls
;
7840 space
= isl_set_get_space(domain
);
7841 ls
= isl_local_space_from_space(space
);
7842 aff
= isl_aff_val_on_domain(ls
, v
);
7844 return isl_pw_aff_alloc(domain
, aff
);
7847 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7848 * but is considered as a function on an isl_set when exported.
7850 __isl_give isl_pw_aff
*isl_set_pw_aff_on_domain_val(__isl_take isl_set
*domain
,
7851 __isl_take isl_val
*v
)
7853 return isl_pw_aff_val_on_domain(domain
, v
);
7856 /* Return a piecewise affine expression that is equal to the parameter
7857 * with identifier "id" on "domain".
7859 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7860 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7865 space
= isl_set_get_space(domain
);
7866 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7867 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7868 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7870 return isl_pw_aff_alloc(domain
, aff
);
7873 /* This function performs the same operation as
7874 * isl_pw_aff_param_on_domain_id,
7875 * but is considered as a function on an isl_set when exported.
7877 __isl_give isl_pw_aff
*isl_set_param_pw_aff_on_domain_id(
7878 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7880 return isl_pw_aff_param_on_domain_id(domain
, id
);
7883 /* Return a multi affine expression that is equal to "mv" on domain
7886 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7887 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7892 isl_local_space
*ls
;
7895 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7896 if (!space
|| n
< 0)
7899 space2
= isl_multi_val_get_space(mv
);
7900 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7901 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7902 space
= isl_space_map_from_domain_and_range(space
, space2
);
7903 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7904 ls
= isl_local_space_from_space(isl_space_domain(space
));
7905 for (i
= 0; i
< n
; ++i
) {
7909 v
= isl_multi_val_get_val(mv
, i
);
7910 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7911 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7913 isl_local_space_free(ls
);
7915 isl_multi_val_free(mv
);
7918 isl_space_free(space
);
7919 isl_multi_val_free(mv
);
7923 /* This is an alternative name for the function above.
7925 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7926 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7928 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7931 /* This function performs the same operation as
7932 * isl_multi_aff_multi_val_on_domain_space,
7933 * but is considered as a function on an isl_space when exported.
7935 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7936 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7938 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7941 /* Return a piecewise multi-affine expression
7942 * that is equal to "mv" on "domain".
7944 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7945 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7950 space
= isl_set_get_space(domain
);
7951 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7953 return isl_pw_multi_aff_alloc(domain
, ma
);
7956 /* This function performs the same operation as
7957 * isl_pw_multi_aff_multi_val_on_domain,
7958 * but is considered as a function on an isl_set when exported.
7960 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7961 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7963 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7966 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7967 * mv is the value that should be attained on each domain set
7968 * res collects the results
7970 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7972 isl_union_pw_multi_aff
*res
;
7975 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7976 * and add it to data->res.
7978 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7981 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7982 isl_pw_multi_aff
*pma
;
7985 mv
= isl_multi_val_copy(data
->mv
);
7986 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7987 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7989 return data
->res
? isl_stat_ok
: isl_stat_error
;
7992 /* Return a union piecewise multi-affine expression
7993 * that is equal to "mv" on "domain".
7995 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7996 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7998 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
8001 space
= isl_union_set_get_space(domain
);
8002 data
.res
= isl_union_pw_multi_aff_empty(space
);
8004 if (isl_union_set_foreach_set(domain
,
8005 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
8006 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8007 isl_union_set_free(domain
);
8008 isl_multi_val_free(mv
);
8012 /* Compute the pullback of data->pma by the function represented by "pma2",
8013 * provided the spaces match, and add the results to data->res.
8015 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
8017 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8019 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
8020 pma2
->dim
, isl_dim_out
)) {
8021 isl_pw_multi_aff_free(pma2
);
8025 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
8026 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8028 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8030 return isl_stat_error
;
8035 /* Compute the pullback of "upma1" by the function represented by "upma2".
8037 __isl_give isl_union_pw_multi_aff
*
8038 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
8039 __isl_take isl_union_pw_multi_aff
*upma1
,
8040 __isl_take isl_union_pw_multi_aff
*upma2
)
8042 return bin_op(upma1
, upma2
, &pullback_entry
);
8045 /* Apply "upma2" to "upma1".
8047 * That is, compute the pullback of "upma2" by "upma1".
8049 __isl_give isl_union_pw_multi_aff
*
8050 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
8051 __isl_take isl_union_pw_multi_aff
*upma1
,
8052 __isl_take isl_union_pw_multi_aff
*upma2
)
8054 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
8058 #define BASE pw_multi_aff
8060 #include "isl_copy_tuple_id_templ.c"
8062 /* Given a function "pma1" of the form A[B -> C] -> D and
8063 * a function "pma2" of the form E -> B,
8064 * replace the domain of the wrapped relation inside the domain of "pma1"
8065 * by the preimage with respect to "pma2".
8066 * In other words, plug in "pma2" in this nested domain.
8067 * The result is of the form A[E -> C] -> D.
8069 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
8070 * plug that into "pma1".
8072 __isl_give isl_pw_multi_aff
*
8073 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8074 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
8076 isl_space
*pma1_space
, *pma2_space
;
8078 isl_pw_multi_aff
*id
;
8080 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
8081 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8083 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
8085 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
8086 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
8089 space
= isl_space_domain(isl_space_copy(pma1_space
));
8090 space
= isl_space_range(isl_space_unwrap(space
));
8091 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
8092 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8094 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8095 pma1_space
, isl_dim_in
);
8096 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8097 pma1_space
, isl_dim_in
);
8099 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8101 isl_pw_multi_aff_free(pma1
);
8102 isl_pw_multi_aff_free(pma2
);
8106 /* If data->pma and "pma2" are such that
8107 * data->pma is of the form A[B -> C] -> D and
8108 * "pma2" is of the form E -> B,
8109 * then replace the domain of the wrapped relation
8110 * inside the domain of data->pma by the preimage with respect to "pma2" and
8111 * add the result to data->res.
8113 static isl_stat
preimage_domain_wrapped_domain_entry(
8114 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8116 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8117 isl_space
*pma1_space
, *pma2_space
;
8120 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8121 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8123 match
= isl_space_domain_is_wrapping(pma1_space
);
8124 if (match
>= 0 && match
)
8125 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8126 isl_dim_in
, pma2_space
, isl_dim_out
);
8127 if (match
< 0 || !match
) {
8128 isl_pw_multi_aff_free(pma2
);
8129 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8132 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8133 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8135 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8137 return isl_stat_non_null(data
->res
);
8140 /* For each pair of functions A[B -> C] -> D in "upma1" and
8141 * E -> B in "upma2",
8142 * replace the domain of the wrapped relation inside the domain of the first
8143 * by the preimage with respect to the second and collect the results.
8144 * In other words, plug in the second function in this nested domain.
8145 * The results are of the form A[E -> C] -> D.
8147 __isl_give isl_union_pw_multi_aff
*
8148 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8149 __isl_take isl_union_pw_multi_aff
*upma1
,
8150 __isl_take isl_union_pw_multi_aff
*upma2
)
8152 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8155 /* Check that the domain space of "upa" matches "space".
8157 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8158 * can in principle never fail since the space "space" is that
8159 * of the isl_multi_union_pw_aff and is a set space such that
8160 * there is no domain space to match.
8162 * We check the parameters and double-check that "space" is
8163 * indeed that of a set.
8165 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8166 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8168 isl_space
*upa_space
;
8172 return isl_stat_error
;
8174 match
= isl_space_is_set(space
);
8176 return isl_stat_error
;
8178 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8179 "expecting set space", return isl_stat_error
);
8181 upa_space
= isl_union_pw_aff_get_space(upa
);
8182 match
= isl_space_has_equal_params(space
, upa_space
);
8186 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8187 "parameters don't match", goto error
);
8189 isl_space_free(upa_space
);
8192 isl_space_free(upa_space
);
8193 return isl_stat_error
;
8196 /* Do the parameters of "upa" match those of "space"?
8198 static isl_bool
isl_union_pw_aff_matching_params(
8199 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8201 isl_space
*upa_space
;
8205 return isl_bool_error
;
8207 upa_space
= isl_union_pw_aff_get_space(upa
);
8209 match
= isl_space_has_equal_params(space
, upa_space
);
8211 isl_space_free(upa_space
);
8215 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8216 * space represents the new parameters.
8217 * res collects the results.
8219 struct isl_union_pw_aff_reset_params_data
{
8221 isl_union_pw_aff
*res
;
8224 /* Replace the parameters of "pa" by data->space and
8225 * add the result to data->res.
8227 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8229 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8232 space
= isl_pw_aff_get_space(pa
);
8233 space
= isl_space_replace_params(space
, data
->space
);
8234 pa
= isl_pw_aff_reset_space(pa
, space
);
8235 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8237 return data
->res
? isl_stat_ok
: isl_stat_error
;
8240 /* Replace the domain space of "upa" by "space".
8241 * Since a union expression does not have a (single) domain space,
8242 * "space" is necessarily a parameter space.
8244 * Since the order and the names of the parameters determine
8245 * the hash value, we need to create a new hash table.
8247 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8248 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8250 struct isl_union_pw_aff_reset_params_data data
= { space
};
8253 match
= isl_union_pw_aff_matching_params(upa
, space
);
8255 upa
= isl_union_pw_aff_free(upa
);
8257 isl_space_free(space
);
8261 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8262 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8263 data
.res
= isl_union_pw_aff_free(data
.res
);
8265 isl_union_pw_aff_free(upa
);
8266 isl_space_free(space
);
8270 /* Return the floor of "pa".
8272 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8274 return isl_pw_aff_floor(pa
);
8277 /* Given f, return floor(f).
8279 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8280 __isl_take isl_union_pw_aff
*upa
)
8282 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8287 * upa mod m = upa - m * floor(upa/m)
8289 * with m an integer value.
8291 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8292 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8294 isl_union_pw_aff
*res
;
8299 if (!isl_val_is_int(m
))
8300 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8301 "expecting integer modulo", goto error
);
8302 if (!isl_val_is_pos(m
))
8303 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8304 "expecting positive modulo", goto error
);
8306 res
= isl_union_pw_aff_copy(upa
);
8307 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8308 upa
= isl_union_pw_aff_floor(upa
);
8309 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8310 res
= isl_union_pw_aff_sub(res
, upa
);
8315 isl_union_pw_aff_free(upa
);
8319 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8320 * pos is the output position that needs to be extracted.
8321 * res collects the results.
8323 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8325 isl_union_pw_aff
*res
;
8328 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8329 * (assuming it has such a dimension) and add it to data->res.
8331 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8333 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8337 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8339 return isl_stat_error
;
8340 if (data
->pos
>= n_out
) {
8341 isl_pw_multi_aff_free(pma
);
8345 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8346 isl_pw_multi_aff_free(pma
);
8348 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8350 return data
->res
? isl_stat_ok
: isl_stat_error
;
8353 /* Extract an isl_union_pw_aff corresponding to
8354 * output dimension "pos" of "upma".
8356 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8357 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8359 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8366 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8367 "cannot extract at negative position", return NULL
);
8369 space
= isl_union_pw_multi_aff_get_space(upma
);
8370 data
.res
= isl_union_pw_aff_empty(space
);
8372 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8373 &get_union_pw_aff
, &data
) < 0)
8374 data
.res
= isl_union_pw_aff_free(data
.res
);
8379 /* Return a union piecewise affine expression
8380 * that is equal to "aff" on "domain".
8382 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8383 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8387 pa
= isl_pw_aff_from_aff(aff
);
8388 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8391 /* Return a union piecewise affine expression
8392 * that is equal to the parameter identified by "id" on "domain".
8394 * Make sure the parameter appears in the space passed to
8395 * isl_aff_param_on_domain_space_id.
8397 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8398 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8403 space
= isl_union_set_get_space(domain
);
8404 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8405 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8406 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8409 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8410 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8412 * "res" collects the results.
8414 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8416 isl_union_pw_aff
*res
;
8419 /* Construct a piecewise affine expression that is equal to data->pa
8420 * on "domain" and add the result to data->res.
8422 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8424 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8428 pa
= isl_pw_aff_copy(data
->pa
);
8429 dim
= isl_set_dim(domain
, isl_dim_set
);
8431 pa
= isl_pw_aff_free(pa
);
8432 pa
= isl_pw_aff_from_range(pa
);
8433 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8434 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8435 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8436 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8438 return data
->res
? isl_stat_ok
: isl_stat_error
;
8441 /* Return a union piecewise affine expression
8442 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8443 * have been aligned.
8445 * Construct an isl_pw_aff on each of the sets in "domain" and
8446 * collect the results.
8448 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8449 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8451 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8454 space
= isl_union_set_get_space(domain
);
8455 data
.res
= isl_union_pw_aff_empty(space
);
8457 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8458 data
.res
= isl_union_pw_aff_free(data
.res
);
8459 isl_union_set_free(domain
);
8460 isl_pw_aff_free(pa
);
8464 /* Return a union piecewise affine expression
8465 * that is equal to "pa" on "domain".
8467 * Check that "pa" is a parametric expression,
8468 * align the parameters if needed and call
8469 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8471 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8472 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8475 isl_bool equal_params
;
8476 isl_space
*domain_space
, *pa_space
;
8478 pa_space
= isl_pw_aff_peek_space(pa
);
8479 is_set
= isl_space_is_set(pa_space
);
8483 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8484 "expecting parametric expression", goto error
);
8486 domain_space
= isl_union_set_get_space(domain
);
8487 pa_space
= isl_pw_aff_get_space(pa
);
8488 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8489 if (equal_params
>= 0 && !equal_params
) {
8492 space
= isl_space_align_params(domain_space
, pa_space
);
8493 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8494 domain
= isl_union_set_align_params(domain
, space
);
8496 isl_space_free(domain_space
);
8497 isl_space_free(pa_space
);
8500 if (equal_params
< 0)
8502 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8504 isl_union_set_free(domain
);
8505 isl_pw_aff_free(pa
);
8509 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8510 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8511 * "res" collects the results.
8513 struct isl_union_pw_aff_val_on_domain_data
{
8515 isl_union_pw_aff
*res
;
8518 /* Construct a piecewise affine expression that is equal to data->v
8519 * on "domain" and add the result to data->res.
8521 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8523 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8527 v
= isl_val_copy(data
->v
);
8528 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8529 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8531 return data
->res
? isl_stat_ok
: isl_stat_error
;
8534 /* Return a union piecewise affine expression
8535 * that is equal to "v" on "domain".
8537 * Construct an isl_pw_aff on each of the sets in "domain" and
8538 * collect the results.
8540 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8541 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8543 struct isl_union_pw_aff_val_on_domain_data data
;
8546 space
= isl_union_set_get_space(domain
);
8547 data
.res
= isl_union_pw_aff_empty(space
);
8549 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8550 data
.res
= isl_union_pw_aff_free(data
.res
);
8551 isl_union_set_free(domain
);
8556 /* Construct a piecewise multi affine expression
8557 * that is equal to "pa" and add it to upma.
8559 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8562 isl_union_pw_multi_aff
**upma
= user
;
8563 isl_pw_multi_aff
*pma
;
8565 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8566 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8568 return *upma
? isl_stat_ok
: isl_stat_error
;
8571 /* Construct and return a union piecewise multi affine expression
8572 * that is equal to the given union piecewise affine expression.
8574 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8575 __isl_take isl_union_pw_aff
*upa
)
8578 isl_union_pw_multi_aff
*upma
;
8583 space
= isl_union_pw_aff_get_space(upa
);
8584 upma
= isl_union_pw_multi_aff_empty(space
);
8586 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8587 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8588 upma
= isl_union_pw_multi_aff_free(upma
);
8590 isl_union_pw_aff_free(upa
);
8594 /* Compute the set of elements in the domain of "pa" where it is zero and
8595 * add this set to "uset".
8597 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8599 isl_union_set
**uset
= (isl_union_set
**)user
;
8601 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8603 return *uset
? isl_stat_ok
: isl_stat_error
;
8606 /* Return a union set containing those elements in the domain
8607 * of "upa" where it is zero.
8609 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8610 __isl_take isl_union_pw_aff
*upa
)
8612 isl_union_set
*zero
;
8614 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8615 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8616 zero
= isl_union_set_free(zero
);
8618 isl_union_pw_aff_free(upa
);
8622 /* Internal data structure for isl_union_pw_aff_bind_id,
8623 * storing the parameter that needs to be bound and
8624 * the accumulated results.
8626 struct isl_bind_id_data
{
8628 isl_union_set
*bound
;
8631 /* Bind the piecewise affine function "pa" to the parameter data->id,
8632 * adding the resulting elements in the domain where the expression
8633 * is equal to the parameter to data->bound.
8635 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8637 struct isl_bind_id_data
*data
= user
;
8640 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8641 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8643 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8646 /* Bind the union piecewise affine function "upa" to the parameter "id",
8647 * returning the elements in the domain where the expression
8648 * is equal to the parameter.
8650 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8651 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8653 struct isl_bind_id_data data
= { id
};
8655 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8656 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8657 data
.bound
= isl_union_set_free(data
.bound
);
8659 isl_union_pw_aff_free(upa
);
8664 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8665 * upma is the function that is plugged in.
8666 * pa is the current part of the function in which upma is plugged in.
8667 * res collects the results.
8669 struct isl_union_pw_aff_pullback_upma_data
{
8670 isl_union_pw_multi_aff
*upma
;
8672 isl_union_pw_aff
*res
;
8675 /* Check if "pma" can be plugged into data->pa.
8676 * If so, perform the pullback and add the result to data->res.
8678 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8680 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8683 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8684 pma
->dim
, isl_dim_out
)) {
8685 isl_pw_multi_aff_free(pma
);
8689 pa
= isl_pw_aff_copy(data
->pa
);
8690 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8692 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8694 return data
->res
? isl_stat_ok
: isl_stat_error
;
8697 /* Check if any of the elements of data->upma can be plugged into pa,
8698 * add if so add the result to data->res.
8700 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8702 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8706 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8708 isl_pw_aff_free(pa
);
8713 /* Compute the pullback of "upa" by the function represented by "upma".
8714 * In other words, plug in "upma" in "upa". The result contains
8715 * expressions defined over the domain space of "upma".
8717 * Run over all pairs of elements in "upa" and "upma", perform
8718 * the pullback when appropriate and collect the results.
8719 * If the hash value were based on the domain space rather than
8720 * the function space, then we could run through all elements
8721 * of "upma" and directly pick out the corresponding element of "upa".
8723 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8724 __isl_take isl_union_pw_aff
*upa
,
8725 __isl_take isl_union_pw_multi_aff
*upma
)
8727 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8730 space
= isl_union_pw_multi_aff_get_space(upma
);
8731 upa
= isl_union_pw_aff_align_params(upa
, space
);
8732 space
= isl_union_pw_aff_get_space(upa
);
8733 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8739 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8740 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8741 data
.res
= isl_union_pw_aff_free(data
.res
);
8743 isl_union_pw_aff_free(upa
);
8744 isl_union_pw_multi_aff_free(upma
);
8747 isl_union_pw_aff_free(upa
);
8748 isl_union_pw_multi_aff_free(upma
);
8753 #define BASE union_pw_aff
8755 #define DOMBASE union_set
8757 #include <isl_multi_explicit_domain.c>
8758 #include <isl_multi_union_pw_aff_explicit_domain.c>
8759 #include <isl_multi_templ.c>
8760 #include <isl_multi_un_op_templ.c>
8761 #include <isl_multi_bin_val_templ.c>
8762 #include <isl_multi_align_set.c>
8763 #include <isl_multi_align_union_set.c>
8764 #include <isl_multi_apply_set_explicit_domain_templ.c>
8765 #include <isl_multi_apply_union_set_explicit_domain_templ.c>
8766 #include <isl_multi_arith_templ.c>
8767 #include <isl_multi_bind_templ.c>
8768 #include <isl_multi_coalesce.c>
8769 #include <isl_multi_dim_id_templ.c>
8770 #include <isl_multi_floor.c>
8771 #include <isl_multi_from_base_templ.c>
8772 #include <isl_multi_check_domain_templ.c>
8773 #include <isl_multi_gist.c>
8774 #include <isl_multi_intersect.c>
8775 #include <isl_multi_nan_templ.c>
8776 #include <isl_multi_tuple_id_templ.c>
8777 #include <isl_multi_union_add_templ.c>
8778 #include <isl_multi_zero_space_templ.c>
8780 /* Does "mupa" have a non-trivial explicit domain?
8782 * The explicit domain, if present, is trivial if it represents
8783 * an (obviously) universe parameter set.
8785 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8786 __isl_keep isl_multi_union_pw_aff
*mupa
)
8788 isl_bool is_params
, trivial
;
8792 return isl_bool_error
;
8793 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8794 return isl_bool_false
;
8795 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8796 if (is_params
< 0 || !is_params
)
8797 return isl_bool_not(is_params
);
8798 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8799 trivial
= isl_set_plain_is_universe(set
);
8801 return isl_bool_not(trivial
);
8804 /* Construct a multiple union piecewise affine expression
8805 * in the given space with value zero in each of the output dimensions.
8807 * Since there is no canonical zero value for
8808 * a union piecewise affine expression, we can only construct
8809 * a zero-dimensional "zero" value.
8811 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8812 __isl_take isl_space
*space
)
8820 params
= isl_space_is_params(space
);
8824 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8825 "expecting proper set space", goto error
);
8826 if (!isl_space_is_set(space
))
8827 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8828 "expecting set space", goto error
);
8829 dim
= isl_space_dim(space
, isl_dim_out
);
8833 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8834 "expecting 0D space", goto error
);
8836 return isl_multi_union_pw_aff_alloc(space
);
8838 isl_space_free(space
);
8842 /* Construct and return a multi union piecewise affine expression
8843 * that is equal to the given multi affine expression.
8845 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8846 __isl_take isl_multi_aff
*ma
)
8848 isl_multi_pw_aff
*mpa
;
8850 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8851 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8854 /* This function performs the same operation as
8855 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8856 * isl_multi_aff when exported.
8858 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8859 __isl_take isl_multi_aff
*ma
)
8861 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8864 /* Construct and return a multi union piecewise affine expression
8865 * that is equal to the given multi piecewise affine expression.
8867 * If the resulting multi union piecewise affine expression has
8868 * an explicit domain, then assign it the domain of the input.
8869 * In other cases, the domain is stored in the individual elements.
8871 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8872 __isl_take isl_multi_pw_aff
*mpa
)
8877 isl_multi_union_pw_aff
*mupa
;
8879 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8881 mpa
= isl_multi_pw_aff_free(mpa
);
8885 space
= isl_multi_pw_aff_get_space(mpa
);
8886 space
= isl_space_range(space
);
8887 mupa
= isl_multi_union_pw_aff_alloc(space
);
8889 for (i
= 0; i
< n
; ++i
) {
8891 isl_union_pw_aff
*upa
;
8893 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8894 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8895 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8897 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8899 isl_multi_pw_aff
*copy
;
8901 copy
= isl_multi_pw_aff_copy(mpa
);
8902 dom
= isl_union_set_from_set(isl_multi_pw_aff_domain(copy
));
8903 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8906 isl_multi_pw_aff_free(mpa
);
8911 /* Extract the range space of "pma" and assign it to *space.
8912 * If *space has already been set (through a previous call to this function),
8913 * then check that the range space is the same.
8915 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8917 isl_space
**space
= user
;
8918 isl_space
*pma_space
;
8921 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8922 isl_pw_multi_aff_free(pma
);
8925 return isl_stat_error
;
8931 equal
= isl_space_is_equal(pma_space
, *space
);
8932 isl_space_free(pma_space
);
8935 return isl_stat_error
;
8937 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8938 "range spaces not the same", return isl_stat_error
);
8942 /* Construct and return a multi union piecewise affine expression
8943 * that is equal to the given union piecewise multi affine expression.
8945 * In order to be able to perform the conversion, the input
8946 * needs to be non-empty and may only involve a single range space.
8948 * If the resulting multi union piecewise affine expression has
8949 * an explicit domain, then assign it the domain of the input.
8950 * In other cases, the domain is stored in the individual elements.
8952 __isl_give isl_multi_union_pw_aff
*
8953 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8954 __isl_take isl_union_pw_multi_aff
*upma
)
8956 isl_space
*space
= NULL
;
8957 isl_multi_union_pw_aff
*mupa
;
8961 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8965 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8966 "cannot extract range space from empty input",
8968 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8975 n
= isl_space_dim(space
, isl_dim_set
);
8977 space
= isl_space_free(space
);
8978 mupa
= isl_multi_union_pw_aff_alloc(space
);
8980 for (i
= 0; i
< n
; ++i
) {
8981 isl_union_pw_aff
*upa
;
8983 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8984 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8986 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8988 isl_union_pw_multi_aff
*copy
;
8990 copy
= isl_union_pw_multi_aff_copy(upma
);
8991 dom
= isl_union_pw_multi_aff_domain(copy
);
8992 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8995 isl_union_pw_multi_aff_free(upma
);
8998 isl_space_free(space
);
8999 isl_union_pw_multi_aff_free(upma
);
9003 /* This function performs the same operation as
9004 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
9005 * but is considered as a function on an isl_union_pw_multi_aff when exported.
9007 __isl_give isl_multi_union_pw_aff
*
9008 isl_union_pw_multi_aff_as_multi_union_pw_aff(
9009 __isl_take isl_union_pw_multi_aff
*upma
)
9011 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
9014 /* Try and create an isl_multi_union_pw_aff that is equivalent
9015 * to the given isl_union_map.
9016 * The isl_union_map is required to be single-valued in each space.
9017 * Moreover, it cannot be empty and all range spaces need to be the same.
9018 * Otherwise, an error is produced.
9020 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
9021 __isl_take isl_union_map
*umap
)
9023 isl_union_pw_multi_aff
*upma
;
9025 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
9026 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
9029 /* This function performs the same operation as
9030 * isl_multi_union_pw_aff_from_union_map,
9031 * but is considered as a function on an isl_union_map when exported.
9033 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
9034 __isl_take isl_union_map
*umap
)
9036 return isl_multi_union_pw_aff_from_union_map(umap
);
9039 /* Return a multiple union piecewise affine expression
9040 * that is equal to "mv" on "domain", assuming "domain" and "mv"
9041 * have been aligned.
9043 * If the resulting multi union piecewise affine expression has
9044 * an explicit domain, then assign it the input domain.
9045 * In other cases, the domain is stored in the individual elements.
9047 static __isl_give isl_multi_union_pw_aff
*
9048 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9049 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9054 isl_multi_union_pw_aff
*mupa
;
9056 n
= isl_multi_val_dim(mv
, isl_dim_set
);
9057 if (!domain
|| n
< 0)
9060 space
= isl_multi_val_get_space(mv
);
9061 mupa
= isl_multi_union_pw_aff_alloc(space
);
9062 for (i
= 0; i
< n
; ++i
) {
9064 isl_union_pw_aff
*upa
;
9066 v
= isl_multi_val_get_val(mv
, i
);
9067 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
9069 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9071 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9072 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9073 isl_union_set_copy(domain
));
9075 isl_union_set_free(domain
);
9076 isl_multi_val_free(mv
);
9079 isl_union_set_free(domain
);
9080 isl_multi_val_free(mv
);
9084 /* Return a multiple union piecewise affine expression
9085 * that is equal to "mv" on "domain".
9087 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
9088 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9090 isl_bool equal_params
;
9094 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
9095 if (equal_params
< 0)
9098 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9100 domain
= isl_union_set_align_params(domain
,
9101 isl_multi_val_get_space(mv
));
9102 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9103 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9105 isl_union_set_free(domain
);
9106 isl_multi_val_free(mv
);
9110 /* Return a multiple union piecewise affine expression
9111 * that is equal to "ma" on "domain".
9113 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9114 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9116 isl_pw_multi_aff
*pma
;
9118 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9119 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9122 /* Return a multiple union piecewise affine expression
9123 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9124 * have been aligned.
9126 * If the resulting multi union piecewise affine expression has
9127 * an explicit domain, then assign it the input domain.
9128 * In other cases, the domain is stored in the individual elements.
9130 static __isl_give isl_multi_union_pw_aff
*
9131 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9132 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9137 isl_multi_union_pw_aff
*mupa
;
9139 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9140 if (!domain
|| n
< 0)
9142 space
= isl_pw_multi_aff_get_space(pma
);
9143 mupa
= isl_multi_union_pw_aff_alloc(space
);
9144 for (i
= 0; i
< n
; ++i
) {
9146 isl_union_pw_aff
*upa
;
9148 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9149 upa
= isl_union_pw_aff_pw_aff_on_domain(
9150 isl_union_set_copy(domain
), pa
);
9151 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9153 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9154 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9155 isl_union_set_copy(domain
));
9157 isl_union_set_free(domain
);
9158 isl_pw_multi_aff_free(pma
);
9161 isl_union_set_free(domain
);
9162 isl_pw_multi_aff_free(pma
);
9166 /* Return a multiple union piecewise affine expression
9167 * that is equal to "pma" on "domain".
9169 __isl_give isl_multi_union_pw_aff
*
9170 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9171 __isl_take isl_pw_multi_aff
*pma
)
9173 isl_bool equal_params
;
9176 space
= isl_pw_multi_aff_peek_space(pma
);
9177 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9178 if (equal_params
< 0)
9181 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9183 domain
= isl_union_set_align_params(domain
,
9184 isl_pw_multi_aff_get_space(pma
));
9185 pma
= isl_pw_multi_aff_align_params(pma
,
9186 isl_union_set_get_space(domain
));
9187 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9190 isl_union_set_free(domain
);
9191 isl_pw_multi_aff_free(pma
);
9195 /* Return a union set containing those elements in the domains
9196 * of the elements of "mupa" where they are all zero.
9198 * If there are no elements, then simply return the entire domain.
9200 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9201 __isl_take isl_multi_union_pw_aff
*mupa
)
9205 isl_union_pw_aff
*upa
;
9206 isl_union_set
*zero
;
9208 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9210 mupa
= isl_multi_union_pw_aff_free(mupa
);
9215 return isl_multi_union_pw_aff_domain(mupa
);
9217 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9218 zero
= isl_union_pw_aff_zero_union_set(upa
);
9220 for (i
= 1; i
< n
; ++i
) {
9221 isl_union_set
*zero_i
;
9223 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9224 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9226 zero
= isl_union_set_intersect(zero
, zero_i
);
9229 isl_multi_union_pw_aff_free(mupa
);
9233 /* Construct a union map mapping the shared domain
9234 * of the union piecewise affine expressions to the range of "mupa"
9235 * in the special case of a 0D multi union piecewise affine expression.
9237 * Construct a map between the explicit domain of "mupa" and
9239 * Note that this assumes that the domain consists of explicit elements.
9241 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9242 __isl_take isl_multi_union_pw_aff
*mupa
)
9246 isl_union_set
*dom
, *ran
;
9248 space
= isl_multi_union_pw_aff_get_space(mupa
);
9249 dom
= isl_multi_union_pw_aff_domain(mupa
);
9250 ran
= isl_union_set_from_set(isl_set_universe(space
));
9252 is_params
= isl_union_set_is_params(dom
);
9254 dom
= isl_union_set_free(dom
);
9256 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9257 "cannot create union map from expression without "
9258 "explicit domain elements",
9259 dom
= isl_union_set_free(dom
));
9261 return isl_union_map_from_domain_and_range(dom
, ran
);
9264 /* Construct a union map mapping the shared domain
9265 * of the union piecewise affine expressions to the range of "mupa"
9266 * with each dimension in the range equated to the
9267 * corresponding union piecewise affine expression.
9269 * If the input is zero-dimensional, then construct a mapping
9270 * from its explicit domain.
9272 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9273 __isl_take isl_multi_union_pw_aff
*mupa
)
9278 isl_union_map
*umap
;
9279 isl_union_pw_aff
*upa
;
9281 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9283 mupa
= isl_multi_union_pw_aff_free(mupa
);
9288 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9290 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9291 umap
= isl_union_map_from_union_pw_aff(upa
);
9293 for (i
= 1; i
< n
; ++i
) {
9294 isl_union_map
*umap_i
;
9296 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9297 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9298 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9301 space
= isl_multi_union_pw_aff_get_space(mupa
);
9302 umap
= isl_union_map_reset_range_space(umap
, space
);
9304 isl_multi_union_pw_aff_free(mupa
);
9308 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9309 * "range" is the space from which to set the range space.
9310 * "res" collects the results.
9312 struct isl_union_pw_multi_aff_reset_range_space_data
{
9314 isl_union_pw_multi_aff
*res
;
9317 /* Replace the range space of "pma" by the range space of data->range and
9318 * add the result to data->res.
9320 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9322 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9325 space
= isl_pw_multi_aff_get_space(pma
);
9326 space
= isl_space_domain(space
);
9327 space
= isl_space_extend_domain_with_range(space
,
9328 isl_space_copy(data
->range
));
9329 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9330 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9332 return data
->res
? isl_stat_ok
: isl_stat_error
;
9335 /* Replace the range space of all the piecewise affine expressions in "upma" by
9336 * the range space of "space".
9338 * This assumes that all these expressions have the same output dimension.
9340 * Since the spaces of the expressions change, so do their hash values.
9341 * We therefore need to create a new isl_union_pw_multi_aff.
9342 * Note that the hash value is currently computed based on the entire
9343 * space even though there can only be a single expression with a given
9346 static __isl_give isl_union_pw_multi_aff
*
9347 isl_union_pw_multi_aff_reset_range_space(
9348 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9350 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9351 isl_space
*space_upma
;
9353 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9354 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9355 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9356 &reset_range_space
, &data
) < 0)
9357 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9359 isl_space_free(space
);
9360 isl_union_pw_multi_aff_free(upma
);
9364 /* Construct and return a union piecewise multi affine expression
9365 * that is equal to the given multi union piecewise affine expression,
9366 * in the special case of a 0D multi union piecewise affine expression.
9368 * Construct a union piecewise multi affine expression
9369 * on top of the explicit domain of the input.
9371 __isl_give isl_union_pw_multi_aff
*
9372 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9373 __isl_take isl_multi_union_pw_aff
*mupa
)
9377 isl_union_set
*domain
;
9379 space
= isl_multi_union_pw_aff_get_space(mupa
);
9380 mv
= isl_multi_val_zero(space
);
9381 domain
= isl_multi_union_pw_aff_domain(mupa
);
9382 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9385 /* Construct and return a union piecewise multi affine expression
9386 * that is equal to the given multi union piecewise affine expression.
9388 * If the input is zero-dimensional, then
9389 * construct a union piecewise multi affine expression
9390 * on top of the explicit domain of the input.
9392 __isl_give isl_union_pw_multi_aff
*
9393 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9394 __isl_take isl_multi_union_pw_aff
*mupa
)
9399 isl_union_pw_multi_aff
*upma
;
9400 isl_union_pw_aff
*upa
;
9402 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9404 mupa
= isl_multi_union_pw_aff_free(mupa
);
9409 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9411 space
= isl_multi_union_pw_aff_get_space(mupa
);
9412 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9413 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9415 for (i
= 1; i
< n
; ++i
) {
9416 isl_union_pw_multi_aff
*upma_i
;
9418 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9419 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9420 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9423 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9425 isl_multi_union_pw_aff_free(mupa
);
9429 /* Intersect the range of "mupa" with "range",
9430 * in the special case where "mupa" is 0D.
9432 * Intersect the domain of "mupa" with the constraints on the parameters
9435 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9436 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9438 range
= isl_set_params(range
);
9439 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9443 /* Intersect the range of "mupa" with "range".
9444 * That is, keep only those domain elements that have a function value
9447 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9448 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9450 isl_union_pw_multi_aff
*upma
;
9451 isl_union_set
*domain
;
9456 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9457 if (n
< 0 || !range
)
9460 space
= isl_set_get_space(range
);
9461 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9462 space
, isl_dim_set
);
9463 isl_space_free(space
);
9467 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9468 "space don't match", goto error
);
9470 return mupa_intersect_range_0D(mupa
, range
);
9472 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9473 isl_multi_union_pw_aff_copy(mupa
));
9474 domain
= isl_union_set_from_set(range
);
9475 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9476 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9480 isl_multi_union_pw_aff_free(mupa
);
9481 isl_set_free(range
);
9485 /* Return the shared domain of the elements of "mupa",
9486 * in the special case where "mupa" is zero-dimensional.
9488 * Return the explicit domain of "mupa".
9489 * Note that this domain may be a parameter set, either
9490 * because "mupa" is meant to live in a set space or
9491 * because no explicit domain has been set.
9493 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9494 __isl_take isl_multi_union_pw_aff
*mupa
)
9498 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9499 isl_multi_union_pw_aff_free(mupa
);
9504 /* Return the shared domain of the elements of "mupa".
9506 * If "mupa" is zero-dimensional, then return its explicit domain.
9508 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9509 __isl_take isl_multi_union_pw_aff
*mupa
)
9513 isl_union_pw_aff
*upa
;
9516 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9518 mupa
= isl_multi_union_pw_aff_free(mupa
);
9523 return isl_multi_union_pw_aff_domain_0D(mupa
);
9525 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9526 dom
= isl_union_pw_aff_domain(upa
);
9527 for (i
= 1; i
< n
; ++i
) {
9528 isl_union_set
*dom_i
;
9530 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9531 dom_i
= isl_union_pw_aff_domain(upa
);
9532 dom
= isl_union_set_intersect(dom
, dom_i
);
9535 isl_multi_union_pw_aff_free(mupa
);
9539 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9540 * In particular, the spaces have been aligned.
9541 * The result is defined over the shared domain of the elements of "mupa"
9543 * We first extract the parametric constant part of "aff" and
9544 * define that over the shared domain.
9545 * Then we iterate over all input dimensions of "aff" and add the corresponding
9546 * multiples of the elements of "mupa".
9547 * Finally, we consider the integer divisions, calling the function
9548 * recursively to obtain an isl_union_pw_aff corresponding to the
9549 * integer division argument.
9551 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9552 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9555 isl_size n_in
, n_div
;
9556 isl_union_pw_aff
*upa
;
9557 isl_union_set
*uset
;
9561 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9562 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9563 if (n_in
< 0 || n_div
< 0)
9566 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9567 cst
= isl_aff_copy(aff
);
9568 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9569 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9570 cst
= isl_aff_project_domain_on_params(cst
);
9571 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9573 for (i
= 0; i
< n_in
; ++i
) {
9574 isl_union_pw_aff
*upa_i
;
9576 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9578 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9579 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9580 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9581 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9584 for (i
= 0; i
< n_div
; ++i
) {
9586 isl_union_pw_aff
*upa_i
;
9588 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9590 div
= isl_aff_get_div(aff
, i
);
9591 upa_i
= multi_union_pw_aff_apply_aff(
9592 isl_multi_union_pw_aff_copy(mupa
), div
);
9593 upa_i
= isl_union_pw_aff_floor(upa_i
);
9594 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9595 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9596 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9599 isl_multi_union_pw_aff_free(mupa
);
9604 isl_multi_union_pw_aff_free(mupa
);
9609 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9610 * with the domain of "aff".
9611 * Furthermore, the dimension of this space needs to be greater than zero.
9612 * The result is defined over the shared domain of the elements of "mupa"
9614 * We perform these checks and then hand over control to
9615 * multi_union_pw_aff_apply_aff.
9617 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9618 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9621 isl_space
*space1
, *space2
;
9624 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9625 isl_aff_get_space(aff
));
9626 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9630 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9631 space2
= isl_aff_get_domain_space(aff
);
9632 equal
= isl_space_is_equal(space1
, space2
);
9633 isl_space_free(space1
);
9634 isl_space_free(space2
);
9638 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9639 "spaces don't match", goto error
);
9640 dim
= isl_aff_dim(aff
, isl_dim_in
);
9644 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9645 "cannot determine domains", goto error
);
9647 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9649 isl_multi_union_pw_aff_free(mupa
);
9654 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9655 * The space of "mupa" is known to be compatible with the domain of "ma".
9657 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9658 * on the domain of "mupa".
9660 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9661 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9665 dom
= isl_multi_union_pw_aff_domain(mupa
);
9666 ma
= isl_multi_aff_project_domain_on_params(ma
);
9668 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9671 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9672 * with the domain of "ma".
9673 * The result is defined over the shared domain of the elements of "mupa"
9675 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9676 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9678 isl_space
*space1
, *space2
;
9679 isl_multi_union_pw_aff
*res
;
9682 isl_size n_in
, n_out
;
9684 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9685 isl_multi_aff_get_space(ma
));
9686 ma
= isl_multi_aff_align_params(ma
,
9687 isl_multi_union_pw_aff_get_space(mupa
));
9688 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9689 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9690 if (!mupa
|| n_in
< 0 || n_out
< 0)
9693 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9694 space2
= isl_multi_aff_get_domain_space(ma
);
9695 equal
= isl_space_is_equal(space1
, space2
);
9696 isl_space_free(space1
);
9697 isl_space_free(space2
);
9701 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9702 "spaces don't match", goto error
);
9704 return mupa_apply_multi_aff_0D(mupa
, ma
);
9706 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9707 res
= isl_multi_union_pw_aff_alloc(space1
);
9709 for (i
= 0; i
< n_out
; ++i
) {
9711 isl_union_pw_aff
*upa
;
9713 aff
= isl_multi_aff_get_aff(ma
, i
);
9714 upa
= multi_union_pw_aff_apply_aff(
9715 isl_multi_union_pw_aff_copy(mupa
), aff
);
9716 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9719 isl_multi_aff_free(ma
);
9720 isl_multi_union_pw_aff_free(mupa
);
9723 isl_multi_union_pw_aff_free(mupa
);
9724 isl_multi_aff_free(ma
);
9728 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9729 * The space of "mupa" is known to be compatible with the domain of "pa".
9731 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9732 * on the domain of "mupa".
9734 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9735 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9739 dom
= isl_multi_union_pw_aff_domain(mupa
);
9740 pa
= isl_pw_aff_project_domain_on_params(pa
);
9742 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9745 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9746 * with the domain of "pa".
9747 * Furthermore, the dimension of this space needs to be greater than zero.
9748 * The result is defined over the shared domain of the elements of "mupa"
9750 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9751 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9756 isl_space
*space
, *space2
;
9757 isl_union_pw_aff
*upa
;
9759 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9760 isl_pw_aff_get_space(pa
));
9761 pa
= isl_pw_aff_align_params(pa
,
9762 isl_multi_union_pw_aff_get_space(mupa
));
9766 space
= isl_multi_union_pw_aff_get_space(mupa
);
9767 space2
= isl_pw_aff_get_domain_space(pa
);
9768 equal
= isl_space_is_equal(space
, space2
);
9769 isl_space_free(space
);
9770 isl_space_free(space2
);
9774 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9775 "spaces don't match", goto error
);
9776 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9780 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9782 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9783 upa
= isl_union_pw_aff_empty(space
);
9785 for (i
= 0; i
< pa
->n
; ++i
) {
9788 isl_multi_union_pw_aff
*mupa_i
;
9789 isl_union_pw_aff
*upa_i
;
9791 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9792 domain
= isl_set_copy(pa
->p
[i
].set
);
9793 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9794 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9795 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9796 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9799 isl_multi_union_pw_aff_free(mupa
);
9800 isl_pw_aff_free(pa
);
9803 isl_multi_union_pw_aff_free(mupa
);
9804 isl_pw_aff_free(pa
);
9808 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9809 * The space of "mupa" is known to be compatible with the domain of "pma".
9811 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9812 * on the domain of "mupa".
9814 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9815 __isl_take isl_multi_union_pw_aff
*mupa
,
9816 __isl_take isl_pw_multi_aff
*pma
)
9820 dom
= isl_multi_union_pw_aff_domain(mupa
);
9821 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9823 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9826 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9827 * with the domain of "pma".
9828 * The result is defined over the shared domain of the elements of "mupa"
9830 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9831 __isl_take isl_multi_union_pw_aff
*mupa
,
9832 __isl_take isl_pw_multi_aff
*pma
)
9834 isl_space
*space1
, *space2
;
9835 isl_multi_union_pw_aff
*res
;
9838 isl_size n_in
, n_out
;
9840 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9841 isl_pw_multi_aff_get_space(pma
));
9842 pma
= isl_pw_multi_aff_align_params(pma
,
9843 isl_multi_union_pw_aff_get_space(mupa
));
9847 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9848 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9849 equal
= isl_space_is_equal(space1
, space2
);
9850 isl_space_free(space1
);
9851 isl_space_free(space2
);
9855 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9856 "spaces don't match", goto error
);
9857 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9858 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9859 if (n_in
< 0 || n_out
< 0)
9862 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9864 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9865 res
= isl_multi_union_pw_aff_alloc(space1
);
9867 for (i
= 0; i
< n_out
; ++i
) {
9869 isl_union_pw_aff
*upa
;
9871 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9872 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9873 isl_multi_union_pw_aff_copy(mupa
), pa
);
9874 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9877 isl_pw_multi_aff_free(pma
);
9878 isl_multi_union_pw_aff_free(mupa
);
9881 isl_multi_union_pw_aff_free(mupa
);
9882 isl_pw_multi_aff_free(pma
);
9886 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9887 * If the explicit domain only keeps track of constraints on the parameters,
9888 * then only update those constraints.
9890 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9891 __isl_take isl_multi_union_pw_aff
*mupa
,
9892 __isl_keep isl_union_pw_multi_aff
*upma
)
9896 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9897 return isl_multi_union_pw_aff_free(mupa
);
9899 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9903 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9905 return isl_multi_union_pw_aff_free(mupa
);
9907 upma
= isl_union_pw_multi_aff_copy(upma
);
9909 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9910 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9912 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9915 return isl_multi_union_pw_aff_free(mupa
);
9919 /* Compute the pullback of "mupa" by the function represented by "upma".
9920 * In other words, plug in "upma" in "mupa". The result contains
9921 * expressions defined over the domain space of "upma".
9923 * Run over all elements of "mupa" and plug in "upma" in each of them.
9925 * If "mupa" has an explicit domain, then it is this domain
9926 * that needs to undergo a pullback instead, i.e., a preimage.
9928 __isl_give isl_multi_union_pw_aff
*
9929 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9930 __isl_take isl_multi_union_pw_aff
*mupa
,
9931 __isl_take isl_union_pw_multi_aff
*upma
)
9936 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9937 isl_union_pw_multi_aff_get_space(upma
));
9938 upma
= isl_union_pw_multi_aff_align_params(upma
,
9939 isl_multi_union_pw_aff_get_space(mupa
));
9940 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9941 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9945 for (i
= 0; i
< n
; ++i
) {
9946 isl_union_pw_aff
*upa
;
9948 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9949 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9950 isl_union_pw_multi_aff_copy(upma
));
9951 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9954 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9955 mupa
= preimage_explicit_domain(mupa
, upma
);
9957 isl_union_pw_multi_aff_free(upma
);
9960 isl_multi_union_pw_aff_free(mupa
);
9961 isl_union_pw_multi_aff_free(upma
);
9965 /* Extract the sequence of elements in "mupa" with domain space "space"
9966 * (ignoring parameters).
9968 * For the elements of "mupa" that are not defined on the specified space,
9969 * the corresponding element in the result is empty.
9971 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9972 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9976 isl_space
*space_mpa
;
9977 isl_multi_pw_aff
*mpa
;
9979 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9980 if (n
< 0 || !space
)
9983 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9984 space
= isl_space_replace_params(space
, space_mpa
);
9985 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9987 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9989 space
= isl_space_from_domain(space
);
9990 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9991 for (i
= 0; i
< n
; ++i
) {
9992 isl_union_pw_aff
*upa
;
9995 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9996 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9997 isl_space_copy(space
));
9998 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9999 isl_union_pw_aff_free(upa
);
10002 isl_space_free(space
);
10005 isl_space_free(space
);
10009 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
10010 * should modify the base expressions in the input.
10012 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
10013 * are taken into account.
10014 * "fn" is applied to each entry in the input.
10016 struct isl_union_pw_multi_aff_un_op_control
{
10017 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
10018 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
10021 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
10022 * a second argument) for use as an isl_union_pw_multi_aff_transform
10023 * filter function (which does take a second argument).
10024 * Simply call control->filter without the second argument.
10026 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
10027 __isl_take isl_pw_multi_aff
*pma
, void *user
)
10029 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
10031 return control
->filter(pma
);
10034 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
10035 * a second argument) for use as an isl_union_pw_multi_aff_transform
10036 * base function (which does take a second argument).
10037 * Simply call control->fn without the second argument.
10039 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
10040 __isl_take isl_pw_multi_aff
*pma
, void *user
)
10042 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
10044 return control
->fn(pma
);
10047 /* Construct an isl_union_pw_multi_aff that is obtained by
10048 * modifying "upma" according to "control".
10050 * isl_union_pw_multi_aff_transform performs essentially
10051 * the same operation, but takes a filter and a callback function
10052 * of a different form (with an extra argument).
10053 * Call isl_union_pw_multi_aff_transform with wrappers
10054 * that remove this extra argument.
10056 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
10057 __isl_take isl_union_pw_multi_aff
*upma
,
10058 struct isl_union_pw_multi_aff_un_op_control
*control
)
10060 struct isl_union_pw_multi_aff_transform_control t_control
= {
10061 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
10062 .filter_user
= control
,
10063 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
10064 .fn_user
= control
,
10067 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
10070 /* For each function in "upma" of the form A -> [B -> C],
10071 * extract the function A -> B and collect the results.
10073 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
10074 __isl_take isl_union_pw_multi_aff
*upma
)
10076 struct isl_union_pw_multi_aff_un_op_control control
= {
10077 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10078 .fn
= &isl_pw_multi_aff_range_factor_domain
,
10080 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10083 /* For each function in "upma" of the form A -> [B -> C],
10084 * extract the function A -> C and collect the results.
10086 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
10087 __isl_take isl_union_pw_multi_aff
*upma
)
10089 struct isl_union_pw_multi_aff_un_op_control control
= {
10090 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10091 .fn
= &isl_pw_multi_aff_range_factor_range
,
10093 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10096 /* Evaluate the affine function "aff" in the void point "pnt".
10097 * In particular, return the value NaN.
10099 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10100 __isl_take isl_point
*pnt
)
10104 ctx
= isl_point_get_ctx(pnt
);
10106 isl_point_free(pnt
);
10107 return isl_val_nan(ctx
);
10110 /* Evaluate the affine expression "aff"
10111 * in the coordinates (with denominator) "pnt".
10113 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10114 __isl_keep isl_vec
*pnt
)
10123 ctx
= isl_vec_get_ctx(aff
);
10126 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10127 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10128 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10129 v
= isl_val_normalize(v
);
10136 /* Check that the domain space of "aff" is equal to "space".
10138 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10139 __isl_keep isl_space
*space
)
10143 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10145 return isl_stat_error
;
10147 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10148 "incompatible spaces", return isl_stat_error
);
10149 return isl_stat_ok
;
10152 /* Evaluate the affine function "aff" in "pnt".
10154 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10155 __isl_take isl_point
*pnt
)
10159 isl_local_space
*ls
;
10161 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10163 is_void
= isl_point_is_void(pnt
);
10167 return eval_void(aff
, pnt
);
10169 ls
= isl_aff_get_domain_local_space(aff
);
10170 pnt
= isl_local_space_lift_point(ls
, pnt
);
10172 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10175 isl_point_free(pnt
);
10180 isl_point_free(pnt
);