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
1471 * We currently only remove divs at the end.
1472 * Some intermediate divs may also not appear directly in the affine
1473 * expression, but we would also need to check that no other divs are
1474 * defined in terms of them.
1476 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1482 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1483 v_div
= isl_aff_domain_var_offset(aff
, isl_dim_div
);
1484 if (n
< 0 || v_div
< 0)
1485 return isl_aff_free(aff
);
1487 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + 1 + v_div
, n
) + 1;
1489 aff
= isl_aff_drop_dims(aff
, isl_dim_div
, pos
, n
- pos
);
1494 /* Look for any divs in the aff->ls with a denominator equal to one
1495 * and plug them into the affine expression and any subsequent divs
1496 * that may reference the div.
1498 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1505 isl_local_space
*ls
;
1508 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1509 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1510 if (n
< 0 || off
< 0)
1511 return isl_aff_free(aff
);
1513 for (i
= 0; i
< n
; ++i
) {
1514 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1516 ls
= isl_local_space_copy(aff
->ls
);
1517 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1518 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1519 vec
= isl_vec_copy(aff
->v
);
1520 vec
= isl_vec_cow(vec
);
1526 isl_seq_substitute(vec
->el
, off
+ i
, aff
->ls
->div
->row
[i
],
1531 isl_vec_free(aff
->v
);
1533 isl_local_space_free(aff
->ls
);
1540 isl_local_space_free(ls
);
1541 return isl_aff_free(aff
);
1544 /* Look for any divs j that appear with a unit coefficient inside
1545 * the definitions of other divs i and plug them into the definitions
1548 * In particular, an expression of the form
1550 * floor((f(..) + floor(g(..)/n))/m)
1554 * floor((n * f(..) + g(..))/(n * m))
1556 * This simplification is correct because we can move the expression
1557 * f(..) into the inner floor in the original expression to obtain
1559 * floor(floor((n * f(..) + g(..))/n)/m)
1561 * from which we can derive the simplified expression.
1563 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1569 n
= isl_aff_domain_dim(aff
, isl_dim_div
);
1570 off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1571 if (n
< 0 || off
< 0)
1572 return isl_aff_free(aff
);
1573 for (i
= 1; i
< n
; ++i
) {
1574 for (j
= 0; j
< i
; ++j
) {
1575 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1577 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1578 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1579 aff
->v
->size
, i
, 1);
1581 return isl_aff_free(aff
);
1588 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1590 * Even though this function is only called on isl_affs with a single
1591 * reference, we are careful to only change aff->v and aff->ls together.
1593 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1595 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1596 isl_local_space
*ls
;
1600 return isl_aff_free(aff
);
1602 ls
= isl_local_space_copy(aff
->ls
);
1603 ls
= isl_local_space_swap_div(ls
, a
, b
);
1604 v
= isl_vec_copy(aff
->v
);
1609 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1610 isl_vec_free(aff
->v
);
1612 isl_local_space_free(aff
->ls
);
1618 isl_local_space_free(ls
);
1619 return isl_aff_free(aff
);
1622 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1624 * We currently do not actually remove div "b", but simply add its
1625 * coefficient to that of "a" and then zero it out.
1627 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1629 isl_size off
= isl_aff_domain_offset(aff
, isl_dim_div
);
1632 return isl_aff_free(aff
);
1634 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1637 aff
->v
= isl_vec_cow(aff
->v
);
1639 return isl_aff_free(aff
);
1641 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1642 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1643 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1648 /* Sort the divs in the local space of "aff" according to
1649 * the comparison function "cmp_row" in isl_local_space.c,
1650 * combining the coefficients of identical divs.
1652 * Reordering divs does not change the semantics of "aff",
1653 * so there is no need to call isl_aff_cow.
1654 * Moreover, this function is currently only called on isl_affs
1655 * with a single reference.
1657 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1662 n
= isl_aff_dim(aff
, isl_dim_div
);
1664 return isl_aff_free(aff
);
1665 for (i
= 1; i
< n
; ++i
) {
1666 for (j
= i
- 1; j
>= 0; --j
) {
1667 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1671 aff
= merge_divs(aff
, j
, j
+ 1);
1673 aff
= swap_div(aff
, j
, j
+ 1);
1682 /* Normalize the representation of "aff".
1684 * This function should only be called on "new" isl_affs, i.e.,
1685 * with only a single reference. We therefore do not need to
1686 * worry about affecting other instances.
1688 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1692 aff
->v
= isl_vec_normalize(aff
->v
);
1694 return isl_aff_free(aff
);
1695 aff
= plug_in_integral_divs(aff
);
1696 aff
= plug_in_unit_divs(aff
);
1697 aff
= sort_divs(aff
);
1698 aff
= isl_aff_remove_unused_divs(aff
);
1702 /* Given f, return floor(f).
1703 * If f is an integer expression, then just return f.
1704 * If f is a constant, then return the constant floor(f).
1705 * Otherwise, if f = g/m, write g = q m + r,
1706 * create a new div d = [r/m] and return the expression q + d.
1707 * The coefficients in r are taken to lie between -m/2 and m/2.
1709 * reduce_div_coefficients performs the same normalization.
1711 * As a special case, floor(NaN) = NaN.
1713 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1723 if (isl_aff_is_nan(aff
))
1725 if (isl_int_is_one(aff
->v
->el
[0]))
1728 aff
= isl_aff_cow(aff
);
1732 aff
->v
= isl_vec_cow(aff
->v
);
1734 return isl_aff_free(aff
);
1736 if (isl_aff_is_cst(aff
)) {
1737 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1738 isl_int_set_si(aff
->v
->el
[0], 1);
1742 div
= isl_vec_copy(aff
->v
);
1743 div
= isl_vec_cow(div
);
1745 return isl_aff_free(aff
);
1747 ctx
= isl_aff_get_ctx(aff
);
1748 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1749 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1750 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1751 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1752 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1753 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1754 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1758 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1760 return isl_aff_free(aff
);
1762 size
= aff
->v
->size
;
1763 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1765 return isl_aff_free(aff
);
1766 isl_int_set_si(aff
->v
->el
[0], 1);
1767 isl_int_set_si(aff
->v
->el
[size
], 1);
1769 aff
= isl_aff_normalize(aff
);
1776 * aff mod m = aff - m * floor(aff/m)
1778 * with m an integer value.
1780 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1781 __isl_take isl_val
*m
)
1788 if (!isl_val_is_int(m
))
1789 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1790 "expecting integer modulo", goto error
);
1792 res
= isl_aff_copy(aff
);
1793 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1794 aff
= isl_aff_floor(aff
);
1795 aff
= isl_aff_scale_val(aff
, m
);
1796 res
= isl_aff_sub(res
, aff
);
1807 * pwaff mod m = pwaff - m * floor(pwaff/m)
1809 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1813 res
= isl_pw_aff_copy(pwaff
);
1814 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1815 pwaff
= isl_pw_aff_floor(pwaff
);
1816 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1817 res
= isl_pw_aff_sub(res
, pwaff
);
1824 * pa mod m = pa - m * floor(pa/m)
1826 * with m an integer value.
1828 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1829 __isl_take isl_val
*m
)
1833 if (!isl_val_is_int(m
))
1834 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1835 "expecting integer modulo", goto error
);
1836 pa
= isl_pw_aff_mod(pa
, m
->n
);
1840 isl_pw_aff_free(pa
);
1845 /* Given f, return ceil(f).
1846 * If f is an integer expression, then just return f.
1847 * Otherwise, let f be the expression
1853 * floor((e + m - 1)/m)
1855 * As a special case, ceil(NaN) = NaN.
1857 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1862 if (isl_aff_is_nan(aff
))
1864 if (isl_int_is_one(aff
->v
->el
[0]))
1867 aff
= isl_aff_cow(aff
);
1870 aff
->v
= isl_vec_cow(aff
->v
);
1872 return isl_aff_free(aff
);
1874 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1875 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1876 aff
= isl_aff_floor(aff
);
1881 /* Apply the expansion computed by isl_merge_divs.
1882 * The expansion itself is given by "exp" while the resulting
1883 * list of divs is given by "div".
1885 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1886 __isl_take isl_mat
*div
, int *exp
)
1892 aff
= isl_aff_cow(aff
);
1894 offset
= isl_aff_domain_offset(aff
, isl_dim_div
);
1895 old_n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
1896 new_n_div
= isl_mat_rows(div
);
1897 if (offset
< 0 || old_n_div
< 0 || new_n_div
< 0)
1900 aff
->v
= isl_vec_expand(aff
->v
, 1 + offset
, old_n_div
, exp
, new_n_div
);
1901 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1902 if (!aff
->v
|| !aff
->ls
)
1903 return isl_aff_free(aff
);
1911 /* Add two affine expressions that live in the same local space.
1913 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1914 __isl_take isl_aff
*aff2
)
1918 aff1
= isl_aff_cow(aff1
);
1922 aff1
->v
= isl_vec_cow(aff1
->v
);
1928 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1929 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1930 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1931 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1932 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1933 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1934 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1939 aff1
= isl_aff_normalize(aff1
);
1947 /* Replace one of the arguments by a NaN and free the other one.
1949 static __isl_give isl_aff
*set_nan_free(__isl_take isl_aff
*aff1
,
1950 __isl_take isl_aff
*aff2
)
1953 return isl_aff_set_nan(aff1
);
1956 /* Return the sum of "aff1" and "aff2".
1958 * If either of the two is NaN, then the result is NaN.
1960 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1961 __isl_take isl_aff
*aff2
)
1967 isl_size n_div1
, n_div2
;
1972 ctx
= isl_aff_get_ctx(aff1
);
1973 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1974 isl_die(ctx
, isl_error_invalid
,
1975 "spaces don't match", goto error
);
1977 if (isl_aff_is_nan(aff1
)) {
1981 if (isl_aff_is_nan(aff2
)) {
1986 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1987 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1988 if (n_div1
< 0 || n_div2
< 0)
1990 if (n_div1
== 0 && n_div2
== 0)
1991 return add_expanded(aff1
, aff2
);
1993 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1994 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1995 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1998 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1999 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
2000 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
2004 return add_expanded(aff1
, aff2
);
2013 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
2014 __isl_take isl_aff
*aff2
)
2016 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
2019 /* Return the result of scaling "aff" by a factor of "f".
2021 * As a special case, f * NaN = NaN.
2023 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
2029 if (isl_aff_is_nan(aff
))
2032 if (isl_int_is_one(f
))
2035 aff
= isl_aff_cow(aff
);
2038 aff
->v
= isl_vec_cow(aff
->v
);
2040 return isl_aff_free(aff
);
2042 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
2043 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
2048 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
2049 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2050 isl_int_divexact(gcd
, f
, gcd
);
2051 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2057 /* Multiple "aff" by "v".
2059 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
2060 __isl_take isl_val
*v
)
2065 if (isl_val_is_one(v
)) {
2070 if (!isl_val_is_rat(v
))
2071 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2072 "expecting rational factor", goto error
);
2074 aff
= isl_aff_scale(aff
, v
->n
);
2075 aff
= isl_aff_scale_down(aff
, v
->d
);
2085 /* Return the result of scaling "aff" down by a factor of "f".
2087 * As a special case, NaN/f = NaN.
2089 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
2095 if (isl_aff_is_nan(aff
))
2098 if (isl_int_is_one(f
))
2101 aff
= isl_aff_cow(aff
);
2105 if (isl_int_is_zero(f
))
2106 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2107 "cannot scale down by zero", return isl_aff_free(aff
));
2109 aff
->v
= isl_vec_cow(aff
->v
);
2111 return isl_aff_free(aff
);
2114 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
2115 isl_int_gcd(gcd
, gcd
, f
);
2116 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
2117 isl_int_divexact(gcd
, f
, gcd
);
2118 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
2124 /* Divide "aff" by "v".
2126 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
2127 __isl_take isl_val
*v
)
2132 if (isl_val_is_one(v
)) {
2137 if (!isl_val_is_rat(v
))
2138 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2139 "expecting rational factor", goto error
);
2140 if (!isl_val_is_pos(v
))
2141 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2142 "factor needs to be positive", goto error
);
2144 aff
= isl_aff_scale(aff
, v
->d
);
2145 aff
= isl_aff_scale_down(aff
, v
->n
);
2155 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2163 isl_int_set_ui(v
, f
);
2164 aff
= isl_aff_scale_down(aff
, v
);
2170 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2171 enum isl_dim_type type
, unsigned pos
, const char *s
)
2173 aff
= isl_aff_cow(aff
);
2176 if (type
== isl_dim_out
)
2177 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2178 "cannot set name of output/set dimension",
2179 return isl_aff_free(aff
));
2180 if (type
== isl_dim_in
)
2182 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2184 return isl_aff_free(aff
);
2189 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2190 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2192 aff
= isl_aff_cow(aff
);
2195 if (type
== isl_dim_out
)
2196 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2197 "cannot set name of output/set dimension",
2199 if (type
== isl_dim_in
)
2201 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2203 return isl_aff_free(aff
);
2212 /* Replace the identifier of the input tuple of "aff" by "id".
2213 * type is currently required to be equal to isl_dim_in
2215 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2216 enum isl_dim_type type
, __isl_take isl_id
*id
)
2218 aff
= isl_aff_cow(aff
);
2221 if (type
!= isl_dim_in
)
2222 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2223 "cannot only set id of input tuple", goto error
);
2224 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2226 return isl_aff_free(aff
);
2235 /* Exploit the equalities in "eq" to simplify the affine expression
2236 * and the expressions of the integer divisions in the local space.
2237 * The integer divisions in this local space are assumed to appear
2238 * as regular dimensions in "eq".
2240 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2241 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2249 if (eq
->n_eq
== 0) {
2250 isl_basic_set_free(eq
);
2254 aff
= isl_aff_cow(aff
);
2258 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2259 isl_basic_set_copy(eq
));
2260 aff
->v
= isl_vec_cow(aff
->v
);
2261 if (!aff
->ls
|| !aff
->v
)
2264 o_div
= isl_basic_set_offset(eq
, isl_dim_div
);
2266 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2267 j
= isl_seq_last_non_zero(eq
->eq
[i
], o_div
+ n_div
);
2268 if (j
< 0 || j
== 0 || j
>= o_div
)
2271 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, o_div
,
2275 isl_basic_set_free(eq
);
2276 aff
= isl_aff_normalize(aff
);
2279 isl_basic_set_free(eq
);
2284 /* Exploit the equalities in "eq" to simplify the affine expression
2285 * and the expressions of the integer divisions in the local space.
2287 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2288 __isl_take isl_basic_set
*eq
)
2292 n_div
= isl_aff_domain_dim(aff
, isl_dim_div
);
2296 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2297 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2299 isl_basic_set_free(eq
);
2304 /* Look for equalities among the variables shared by context and aff
2305 * and the integer divisions of aff, if any.
2306 * The equalities are then used to eliminate coefficients and/or integer
2307 * divisions from aff.
2309 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2310 __isl_take isl_set
*context
)
2312 isl_local_space
*ls
;
2313 isl_basic_set
*hull
;
2315 ls
= isl_aff_get_domain_local_space(aff
);
2316 context
= isl_local_space_lift_set(ls
, context
);
2318 hull
= isl_set_affine_hull(context
);
2319 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2322 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2323 __isl_take isl_set
*context
)
2325 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2326 dom_context
= isl_set_intersect_params(dom_context
, context
);
2327 return isl_aff_gist(aff
, dom_context
);
2330 /* Return a basic set containing those elements in the space
2331 * of aff where it is positive. "rational" should not be set.
2333 * If "aff" is NaN, then it is not positive.
2335 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2336 int rational
, void *user
)
2338 isl_constraint
*ineq
;
2339 isl_basic_set
*bset
;
2344 if (isl_aff_is_nan(aff
)) {
2345 isl_space
*space
= isl_aff_get_domain_space(aff
);
2347 return isl_basic_set_empty(space
);
2350 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2351 "rational sets not supported", goto error
);
2353 ineq
= isl_inequality_from_aff(aff
);
2354 c
= isl_constraint_get_constant_val(ineq
);
2355 c
= isl_val_sub_ui(c
, 1);
2356 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2358 bset
= isl_basic_set_from_constraint(ineq
);
2359 bset
= isl_basic_set_simplify(bset
);
2366 /* Return a basic set containing those elements in the space
2367 * of aff where it is non-negative.
2368 * If "rational" is set, then return a rational basic set.
2370 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2372 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2373 __isl_take isl_aff
*aff
, int rational
, void *user
)
2375 isl_constraint
*ineq
;
2376 isl_basic_set
*bset
;
2380 if (isl_aff_is_nan(aff
)) {
2381 isl_space
*space
= isl_aff_get_domain_space(aff
);
2383 return isl_basic_set_empty(space
);
2386 ineq
= isl_inequality_from_aff(aff
);
2388 bset
= isl_basic_set_from_constraint(ineq
);
2390 bset
= isl_basic_set_set_rational(bset
);
2391 bset
= isl_basic_set_simplify(bset
);
2395 /* Return a basic set containing those elements in the space
2396 * of aff where it is non-negative.
2398 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2400 return aff_nonneg_basic_set(aff
, 0, NULL
);
2403 /* Return a basic set containing those elements in the domain space
2404 * of "aff" where it is positive.
2406 __isl_give isl_basic_set
*isl_aff_pos_basic_set(__isl_take isl_aff
*aff
)
2408 aff
= isl_aff_add_constant_num_si(aff
, -1);
2409 return isl_aff_nonneg_basic_set(aff
);
2412 /* Return a basic set containing those elements in the domain space
2413 * of aff where it is negative.
2415 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2417 aff
= isl_aff_neg(aff
);
2418 return isl_aff_pos_basic_set(aff
);
2421 /* Return a basic set containing those elements in the space
2422 * of aff where it is zero.
2423 * If "rational" is set, then return a rational basic set.
2425 * If "aff" is NaN, then it is not zero.
2427 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2428 int rational
, void *user
)
2430 isl_constraint
*ineq
;
2431 isl_basic_set
*bset
;
2435 if (isl_aff_is_nan(aff
)) {
2436 isl_space
*space
= isl_aff_get_domain_space(aff
);
2438 return isl_basic_set_empty(space
);
2441 ineq
= isl_equality_from_aff(aff
);
2443 bset
= isl_basic_set_from_constraint(ineq
);
2445 bset
= isl_basic_set_set_rational(bset
);
2446 bset
= isl_basic_set_simplify(bset
);
2450 /* Return a basic set containing those elements in the space
2451 * of aff where it is zero.
2453 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2455 return aff_zero_basic_set(aff
, 0, NULL
);
2458 /* Return a basic set containing those elements in the shared space
2459 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2461 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2462 __isl_take isl_aff
*aff2
)
2464 aff1
= isl_aff_sub(aff1
, aff2
);
2466 return isl_aff_nonneg_basic_set(aff1
);
2469 /* Return a basic set containing those elements in the shared domain space
2470 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2472 __isl_give isl_basic_set
*isl_aff_gt_basic_set(__isl_take isl_aff
*aff1
,
2473 __isl_take isl_aff
*aff2
)
2475 aff1
= isl_aff_sub(aff1
, aff2
);
2477 return isl_aff_pos_basic_set(aff1
);
2480 /* Return a set containing those elements in the shared space
2481 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2483 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2484 __isl_take isl_aff
*aff2
)
2486 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2489 /* Return a set containing those elements in the shared domain space
2490 * of aff1 and aff2 where aff1 is greater than aff2.
2492 * If either of the two inputs is NaN, then the result is empty,
2493 * as comparisons with NaN always return false.
2495 __isl_give isl_set
*isl_aff_gt_set(__isl_take isl_aff
*aff1
,
2496 __isl_take isl_aff
*aff2
)
2498 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1
, aff2
));
2501 /* Return a basic set containing those elements in the shared space
2502 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2504 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2505 __isl_take isl_aff
*aff2
)
2507 return isl_aff_ge_basic_set(aff2
, aff1
);
2510 /* Return a basic set containing those elements in the shared domain space
2511 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2513 __isl_give isl_basic_set
*isl_aff_lt_basic_set(__isl_take isl_aff
*aff1
,
2514 __isl_take isl_aff
*aff2
)
2516 return isl_aff_gt_basic_set(aff2
, aff1
);
2519 /* Return a set containing those elements in the shared space
2520 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2522 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2523 __isl_take isl_aff
*aff2
)
2525 return isl_aff_ge_set(aff2
, aff1
);
2528 /* Return a set containing those elements in the shared domain space
2529 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2531 __isl_give isl_set
*isl_aff_lt_set(__isl_take isl_aff
*aff1
,
2532 __isl_take isl_aff
*aff2
)
2534 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1
, aff2
));
2537 /* Return a basic set containing those elements in the shared space
2538 * of aff1 and aff2 where aff1 and aff2 are equal.
2540 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2541 __isl_take isl_aff
*aff2
)
2543 aff1
= isl_aff_sub(aff1
, aff2
);
2545 return isl_aff_zero_basic_set(aff1
);
2548 /* Return a set containing those elements in the shared space
2549 * of aff1 and aff2 where aff1 and aff2 are equal.
2551 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2552 __isl_take isl_aff
*aff2
)
2554 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2557 /* Return a set containing those elements in the shared domain space
2558 * of aff1 and aff2 where aff1 and aff2 are not equal.
2560 * If either of the two inputs is NaN, then the result is empty,
2561 * as comparisons with NaN always return false.
2563 __isl_give isl_set
*isl_aff_ne_set(__isl_take isl_aff
*aff1
,
2564 __isl_take isl_aff
*aff2
)
2566 isl_set
*set_lt
, *set_gt
;
2568 set_lt
= isl_aff_lt_set(isl_aff_copy(aff1
),
2569 isl_aff_copy(aff2
));
2570 set_gt
= isl_aff_gt_set(aff1
, aff2
);
2571 return isl_set_union_disjoint(set_lt
, set_gt
);
2574 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2575 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2577 aff1
= isl_aff_add(aff1
, aff2
);
2578 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2582 isl_bool
isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2585 return isl_bool_error
;
2587 return isl_bool_false
;
2591 #define TYPE isl_aff
2593 #include "check_type_range_templ.c"
2595 /* Check whether the given affine expression has non-zero coefficient
2596 * for any dimension in the given range or if any of these dimensions
2597 * appear with non-zero coefficients in any of the integer divisions
2598 * involved in the affine expression.
2600 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2601 enum isl_dim_type type
, unsigned first
, unsigned n
)
2605 isl_bool involves
= isl_bool_false
;
2608 return isl_bool_error
;
2610 return isl_bool_false
;
2611 if (isl_aff_check_range(aff
, type
, first
, n
) < 0)
2612 return isl_bool_error
;
2614 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2618 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2619 for (i
= 0; i
< n
; ++i
)
2620 if (active
[first
+ i
]) {
2621 involves
= isl_bool_true
;
2630 return isl_bool_error
;
2633 /* Does "aff" involve any local variables, i.e., integer divisions?
2635 isl_bool
isl_aff_involves_locals(__isl_keep isl_aff
*aff
)
2639 n
= isl_aff_dim(aff
, isl_dim_div
);
2641 return isl_bool_error
;
2642 return isl_bool_ok(n
> 0);
2645 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2646 enum isl_dim_type type
, unsigned first
, unsigned n
)
2650 if (type
== isl_dim_out
)
2651 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2652 "cannot drop output/set dimension",
2653 return isl_aff_free(aff
));
2654 if (type
== isl_dim_in
)
2656 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2659 if (isl_local_space_check_range(aff
->ls
, type
, first
, n
) < 0)
2660 return isl_aff_free(aff
);
2662 aff
= isl_aff_cow(aff
);
2666 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2668 return isl_aff_free(aff
);
2670 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2671 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2673 return isl_aff_free(aff
);
2678 /* Is the domain of "aff" a product?
2680 static isl_bool
isl_aff_domain_is_product(__isl_keep isl_aff
*aff
)
2682 return isl_space_is_product(isl_aff_peek_domain_space(aff
));
2686 #define TYPE isl_aff
2687 #include <isl_domain_factor_templ.c>
2689 /* Project the domain of the affine expression onto its parameter space.
2690 * The affine expression may not involve any of the domain dimensions.
2692 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2697 n
= isl_aff_dim(aff
, isl_dim_in
);
2699 return isl_aff_free(aff
);
2700 aff
= isl_aff_drop_domain(aff
, 0, n
);
2701 space
= isl_aff_get_domain_space(aff
);
2702 space
= isl_space_params(space
);
2703 aff
= isl_aff_reset_domain_space(aff
, space
);
2707 /* Convert an affine expression defined over a parameter domain
2708 * into one that is defined over a zero-dimensional set.
2710 __isl_give isl_aff
*isl_aff_from_range(__isl_take isl_aff
*aff
)
2712 isl_local_space
*ls
;
2714 ls
= isl_aff_take_domain_local_space(aff
);
2715 ls
= isl_local_space_set_from_params(ls
);
2716 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2721 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2722 enum isl_dim_type type
, unsigned first
, unsigned n
)
2726 if (type
== isl_dim_out
)
2727 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2728 "cannot insert output/set dimensions",
2729 return isl_aff_free(aff
));
2730 if (type
== isl_dim_in
)
2732 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2735 if (isl_local_space_check_range(aff
->ls
, type
, first
, 0) < 0)
2736 return isl_aff_free(aff
);
2738 aff
= isl_aff_cow(aff
);
2742 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2744 return isl_aff_free(aff
);
2746 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2747 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2749 return isl_aff_free(aff
);
2754 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2755 enum isl_dim_type type
, unsigned n
)
2759 pos
= isl_aff_dim(aff
, type
);
2761 return isl_aff_free(aff
);
2763 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2766 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2767 * to dimensions of "dst_type" at "dst_pos".
2769 * We only support moving input dimensions to parameters and vice versa.
2771 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2772 enum isl_dim_type dst_type
, unsigned dst_pos
,
2773 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2777 isl_size src_off
, dst_off
;
2782 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2783 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2786 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2787 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2788 "cannot move output/set dimension",
2789 return isl_aff_free(aff
));
2790 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2791 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2792 "cannot move divs", return isl_aff_free(aff
));
2793 if (dst_type
== isl_dim_in
)
2794 dst_type
= isl_dim_set
;
2795 if (src_type
== isl_dim_in
)
2796 src_type
= isl_dim_set
;
2798 if (isl_local_space_check_range(aff
->ls
, src_type
, src_pos
, n
) < 0)
2799 return isl_aff_free(aff
);
2800 if (dst_type
== src_type
)
2801 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2802 "moving dims within the same type not supported",
2803 return isl_aff_free(aff
));
2805 aff
= isl_aff_cow(aff
);
2806 src_off
= isl_aff_domain_offset(aff
, src_type
);
2807 dst_off
= isl_aff_domain_offset(aff
, dst_type
);
2808 if (src_off
< 0 || dst_off
< 0)
2809 return isl_aff_free(aff
);
2811 g_src_pos
= 1 + src_off
+ src_pos
;
2812 g_dst_pos
= 1 + dst_off
+ dst_pos
;
2813 if (dst_type
> src_type
)
2816 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2817 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2818 src_type
, src_pos
, n
);
2819 if (!aff
->v
|| !aff
->ls
)
2820 return isl_aff_free(aff
);
2822 aff
= sort_divs(aff
);
2827 /* Given an affine function on a domain (A -> B),
2828 * interchange A and B in the wrapped domain
2829 * to obtain a function on the domain (B -> A).
2831 * Since this may change the position of some variables,
2832 * it may also change the normalized order of the local variables.
2833 * Restore this order. Since sort_divs assumes the input
2834 * has a single reference, an explicit isl_aff_cow is required.
2836 __isl_give isl_aff
*isl_aff_domain_reverse(__isl_take isl_aff
*aff
)
2839 isl_local_space
*ls
;
2841 isl_size n_in
, n_out
;
2844 space
= isl_aff_peek_domain_space(aff
);
2845 offset
= isl_space_offset(space
, isl_dim_set
);
2846 n_in
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_in
);
2847 n_out
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_out
);
2848 if (offset
< 0 || n_in
< 0 || n_out
< 0)
2849 return isl_aff_free(aff
);
2851 v
= isl_aff_take_rat_aff(aff
);
2852 v
= isl_vec_move_els(v
, 1 + 1 + offset
, 1 + 1 + offset
+ n_in
, n_out
);
2853 aff
= isl_aff_restore_rat_aff(aff
, v
);
2855 ls
= isl_aff_take_domain_local_space(aff
);
2856 ls
= isl_local_space_wrapped_reverse(ls
);
2857 aff
= isl_aff_restore_domain_local_space(aff
, ls
);
2859 aff
= isl_aff_cow(aff
);
2860 aff
= sort_divs(aff
);
2865 /* Return a zero isl_aff in the given space.
2867 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2868 * interface over all piecewise types.
2870 static __isl_give isl_aff
*isl_aff_zero_in_space(__isl_take isl_space
*space
)
2872 isl_local_space
*ls
;
2874 ls
= isl_local_space_from_space(isl_space_domain(space
));
2875 return isl_aff_zero_on_domain(ls
);
2878 #define isl_aff_involves_nan isl_aff_is_nan
2881 #define PW isl_pw_aff
2885 #define EL_IS_ZERO is_empty
2889 #define IS_ZERO is_empty
2892 #undef DEFAULT_IS_ZERO
2893 #define DEFAULT_IS_ZERO 0
2895 #include <isl_pw_templ.c>
2896 #include <isl_pw_un_op_templ.c>
2897 #include <isl_pw_add_constant_val_templ.c>
2898 #include <isl_pw_add_disjoint_templ.c>
2899 #include <isl_pw_bind_domain_templ.c>
2900 #include <isl_pw_domain_reverse_templ.c>
2901 #include <isl_pw_eval.c>
2902 #include <isl_pw_hash.c>
2903 #include <isl_pw_fix_templ.c>
2904 #include <isl_pw_from_range_templ.c>
2905 #include <isl_pw_insert_dims_templ.c>
2906 #include <isl_pw_insert_domain_templ.c>
2907 #include <isl_pw_move_dims_templ.c>
2908 #include <isl_pw_neg_templ.c>
2909 #include <isl_pw_pullback_templ.c>
2910 #include <isl_pw_scale_templ.c>
2911 #include <isl_pw_sub_templ.c>
2912 #include <isl_pw_union_opt.c>
2917 #include <isl_union_single.c>
2918 #include <isl_union_neg.c>
2919 #include <isl_union_sub_templ.c>
2924 #include <isl_union_pw_templ.c>
2926 /* Compute a piecewise quasi-affine expression with a domain that
2927 * is the union of those of pwaff1 and pwaff2 and such that on each
2928 * cell, the quasi-affine expression is the maximum of those of pwaff1
2929 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2930 * cell, then the associated expression is the defined one.
2932 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2933 __isl_take isl_pw_aff
*pwaff2
)
2935 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2936 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2939 /* Compute a piecewise quasi-affine expression with a domain that
2940 * is the union of those of pwaff1 and pwaff2 and such that on each
2941 * cell, the quasi-affine expression is the minimum of those of pwaff1
2942 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2943 * cell, then the associated expression is the defined one.
2945 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2946 __isl_take isl_pw_aff
*pwaff2
)
2948 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
2949 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2952 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2953 __isl_take isl_pw_aff
*pwaff2
, int max
)
2956 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2958 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2961 /* Is the domain of "pa" a product?
2963 static isl_bool
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff
*pa
)
2965 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa
));
2969 #define TYPE isl_pw_aff
2970 #include <isl_domain_factor_templ.c>
2972 /* Return a set containing those elements in the domain
2973 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2974 * does not satisfy "fn" (if complement is 1).
2976 * The pieces with a NaN never belong to the result since
2977 * NaN does not satisfy any property.
2979 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2980 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
,
2982 int complement
, void *user
)
2990 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2992 for (i
= 0; i
< pwaff
->n
; ++i
) {
2993 isl_basic_set
*bset
;
2994 isl_set
*set_i
, *locus
;
2997 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
3000 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
3001 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
, user
);
3002 locus
= isl_set_from_basic_set(bset
);
3003 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
3005 set_i
= isl_set_subtract(set_i
, locus
);
3007 set_i
= isl_set_intersect(set_i
, locus
);
3008 set
= isl_set_union_disjoint(set
, set_i
);
3011 isl_pw_aff_free(pwaff
);
3016 /* Return a set containing those elements in the domain
3017 * of "pa" where it is positive.
3019 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
3021 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0, NULL
);
3024 /* Return a set containing those elements in the domain
3025 * of pwaff where it is non-negative.
3027 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
3029 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0, NULL
);
3032 /* Return a set containing those elements in the domain
3033 * of pwaff where it is zero.
3035 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
3037 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0, NULL
);
3040 /* Return a set containing those elements in the domain
3041 * of pwaff where it is not zero.
3043 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
3045 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1, NULL
);
3048 /* Bind the affine function "aff" to the parameter "id",
3049 * returning the elements in the domain where the affine expression
3050 * is equal to the parameter.
3052 __isl_give isl_basic_set
*isl_aff_bind_id(__isl_take isl_aff
*aff
,
3053 __isl_take isl_id
*id
)
3058 space
= isl_aff_get_domain_space(aff
);
3059 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
3061 aff
= isl_aff_align_params(aff
, isl_space_copy(space
));
3062 aff_id
= isl_aff_param_on_domain_space_id(space
, id
);
3064 return isl_aff_eq_basic_set(aff
, aff_id
);
3067 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
3068 * "rational" should not be set.
3070 static __isl_give isl_basic_set
*aff_bind_id(__isl_take isl_aff
*aff
,
3071 int rational
, void *user
)
3078 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
3079 "rational binding not supported", goto error
);
3080 return isl_aff_bind_id(aff
, isl_id_copy(id
));
3086 /* Bind the piecewise affine function "pa" to the parameter "id",
3087 * returning the elements in the domain where the expression
3088 * is equal to the parameter.
3090 __isl_give isl_set
*isl_pw_aff_bind_id(__isl_take isl_pw_aff
*pa
,
3091 __isl_take isl_id
*id
)
3095 bound
= pw_aff_locus(pa
, &aff_bind_id
, 0, id
);
3101 /* Return a set containing those elements in the shared domain
3102 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3104 * We compute the difference on the shared domain and then construct
3105 * the set of values where this difference is non-negative.
3106 * If strict is set, we first subtract 1 from the difference.
3107 * If equal is set, we only return the elements where pwaff1 and pwaff2
3110 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
3111 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
3113 isl_set
*set1
, *set2
;
3115 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
3116 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
3117 set1
= isl_set_intersect(set1
, set2
);
3118 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
3119 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
3120 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
3123 isl_space
*space
= isl_set_get_space(set1
);
3125 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
3126 aff
= isl_aff_add_constant_si(aff
, -1);
3127 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
3132 return isl_pw_aff_zero_set(pwaff1
);
3133 return isl_pw_aff_nonneg_set(pwaff1
);
3136 /* Return a set containing those elements in the shared domain
3137 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3139 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
3140 __isl_take isl_pw_aff
*pwaff2
)
3142 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3143 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
3146 /* Return a set containing those elements in the shared domain
3147 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3149 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
3150 __isl_take isl_pw_aff
*pwaff2
)
3152 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3153 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
3156 /* Return a set containing those elements in the shared domain
3157 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3159 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
3160 __isl_take isl_pw_aff
*pwaff2
)
3162 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3163 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
3166 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
3167 __isl_take isl_pw_aff
*pwaff2
)
3169 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
3172 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
3173 __isl_take isl_pw_aff
*pwaff2
)
3175 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
3178 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3179 * where the function values are ordered in the same way as "order",
3180 * which returns a set in the shared domain of its two arguments.
3182 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3183 * We first pull back the two functions such that they are defined on
3184 * the domain [A -> B]. Then we apply "order", resulting in a set
3185 * in the space [A -> B]. Finally, we unwrap this set to obtain
3186 * a map in the space A -> B.
3188 static __isl_give isl_map
*isl_pw_aff_order_map(
3189 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
3190 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
3191 __isl_take isl_pw_aff
*pa2
))
3193 isl_space
*space1
, *space2
;
3197 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3198 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
3199 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
3200 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
3201 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
3202 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
3203 ma
= isl_multi_aff_range_map(space1
);
3204 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
3205 set
= order(pa1
, pa2
);
3207 return isl_set_unwrap(set
);
3210 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3211 * where the function values are equal.
3213 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
3214 __isl_take isl_pw_aff
*pa2
)
3216 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_eq_set
);
3219 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3220 * where the function value of "pa1" is less than or equal to
3221 * the function value of "pa2".
3223 __isl_give isl_map
*isl_pw_aff_le_map(__isl_take isl_pw_aff
*pa1
,
3224 __isl_take isl_pw_aff
*pa2
)
3226 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_le_set
);
3229 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3230 * where the function value of "pa1" is less than the function value of "pa2".
3232 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3233 __isl_take isl_pw_aff
*pa2
)
3235 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_lt_set
);
3238 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3239 * where the function value of "pa1" is greater than or equal to
3240 * the function value of "pa2".
3242 __isl_give isl_map
*isl_pw_aff_ge_map(__isl_take isl_pw_aff
*pa1
,
3243 __isl_take isl_pw_aff
*pa2
)
3245 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_ge_set
);
3248 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3249 * where the function value of "pa1" is greater than the function value
3252 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3253 __isl_take isl_pw_aff
*pa2
)
3255 return isl_pw_aff_order_map(pa1
, pa2
, &isl_pw_aff_gt_set
);
3258 /* Return a set containing those elements in the shared domain
3259 * of the elements of list1 and list2 where each element in list1
3260 * has the relation specified by "fn" with each element in list2.
3262 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3263 __isl_take isl_pw_aff_list
*list2
,
3264 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3265 __isl_take isl_pw_aff
*pwaff2
))
3271 if (!list1
|| !list2
)
3274 ctx
= isl_pw_aff_list_get_ctx(list1
);
3275 if (list1
->n
< 1 || list2
->n
< 1)
3276 isl_die(ctx
, isl_error_invalid
,
3277 "list should contain at least one element", goto error
);
3279 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3280 for (i
= 0; i
< list1
->n
; ++i
)
3281 for (j
= 0; j
< list2
->n
; ++j
) {
3284 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3285 isl_pw_aff_copy(list2
->p
[j
]));
3286 set
= isl_set_intersect(set
, set_ij
);
3289 isl_pw_aff_list_free(list1
);
3290 isl_pw_aff_list_free(list2
);
3293 isl_pw_aff_list_free(list1
);
3294 isl_pw_aff_list_free(list2
);
3298 /* Return a set containing those elements in the shared domain
3299 * of the elements of list1 and list2 where each element in list1
3300 * is equal to each element in list2.
3302 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3303 __isl_take isl_pw_aff_list
*list2
)
3305 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3308 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3309 __isl_take isl_pw_aff_list
*list2
)
3311 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
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 less than or equal to each element in list2.
3318 __isl_give isl_set
*isl_pw_aff_list_le_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_le_set
);
3324 __isl_give isl_set
*isl_pw_aff_list_lt_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_lt_set
);
3330 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3331 __isl_take isl_pw_aff_list
*list2
)
3333 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3336 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3337 __isl_take isl_pw_aff_list
*list2
)
3339 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3343 /* Return a set containing those elements in the shared domain
3344 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3346 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3347 __isl_take isl_pw_aff
*pwaff2
)
3349 isl_set
*set_lt
, *set_gt
;
3351 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3352 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3353 isl_pw_aff_copy(pwaff2
));
3354 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3355 return isl_set_union_disjoint(set_lt
, set_gt
);
3358 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3363 if (isl_int_is_one(v
))
3365 if (!isl_int_is_pos(v
))
3366 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3367 "factor needs to be positive",
3368 return isl_pw_aff_free(pwaff
));
3369 pwaff
= isl_pw_aff_cow(pwaff
);
3375 for (i
= 0; i
< pwaff
->n
; ++i
) {
3376 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3377 if (!pwaff
->p
[i
].aff
)
3378 return isl_pw_aff_free(pwaff
);
3384 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3386 struct isl_pw_aff_un_op_control control
= { .fn_base
= &isl_aff_floor
};
3387 return isl_pw_aff_un_op(pwaff
, &control
);
3390 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3392 struct isl_pw_aff_un_op_control control
= { .fn_base
= &isl_aff_ceil
};
3393 return isl_pw_aff_un_op(pwaff
, &control
);
3396 /* Assuming that "cond1" and "cond2" are disjoint,
3397 * return an affine expression that is equal to pwaff1 on cond1
3398 * and to pwaff2 on cond2.
3400 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3401 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3402 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3404 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3405 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3407 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3410 /* Return an affine expression that is equal to pwaff_true for elements
3411 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3413 * That is, return cond ? pwaff_true : pwaff_false;
3415 * If "cond" involves and NaN, then we conservatively return a NaN
3416 * on its entire domain. In principle, we could consider the pieces
3417 * where it is NaN separately from those where it is not.
3419 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3420 * then only use the domain of "cond" to restrict the domain.
3422 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3423 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3425 isl_set
*cond_true
, *cond_false
;
3430 if (isl_pw_aff_involves_nan(cond
)) {
3431 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3432 isl_local_space
*ls
= isl_local_space_from_space(space
);
3433 isl_pw_aff_free(cond
);
3434 isl_pw_aff_free(pwaff_true
);
3435 isl_pw_aff_free(pwaff_false
);
3436 return isl_pw_aff_nan_on_domain(ls
);
3439 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3440 isl_pw_aff_get_space(pwaff_false
));
3441 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3442 isl_pw_aff_get_space(pwaff_true
));
3443 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3449 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3450 isl_pw_aff_free(pwaff_false
);
3451 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3454 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3455 cond_false
= isl_pw_aff_zero_set(cond
);
3456 return isl_pw_aff_select(cond_true
, pwaff_true
,
3457 cond_false
, pwaff_false
);
3459 isl_pw_aff_free(cond
);
3460 isl_pw_aff_free(pwaff_true
);
3461 isl_pw_aff_free(pwaff_false
);
3465 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3470 return isl_bool_error
;
3472 pos
= isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2);
3473 return isl_bool_ok(pos
== -1);
3476 /* Check whether pwaff is a piecewise constant.
3478 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3483 return isl_bool_error
;
3485 for (i
= 0; i
< pwaff
->n
; ++i
) {
3486 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3487 if (is_cst
< 0 || !is_cst
)
3491 return isl_bool_true
;
3494 /* Return the product of "aff1" and "aff2".
3496 * If either of the two is NaN, then the result is NaN.
3498 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3500 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3501 __isl_take isl_aff
*aff2
)
3506 if (isl_aff_is_nan(aff1
)) {
3510 if (isl_aff_is_nan(aff2
)) {
3515 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3516 return isl_aff_mul(aff2
, aff1
);
3518 if (!isl_aff_is_cst(aff2
))
3519 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3520 "at least one affine expression should be constant",
3523 aff1
= isl_aff_cow(aff1
);
3527 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3528 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3538 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3540 * If either of the two is NaN, then the result is NaN.
3541 * A division by zero also results in NaN.
3543 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3544 __isl_take isl_aff
*aff2
)
3546 isl_bool is_cst
, is_zero
;
3552 if (isl_aff_is_nan(aff1
)) {
3556 if (isl_aff_is_nan(aff2
)) {
3561 is_cst
= isl_aff_is_cst(aff2
);
3565 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3566 "second argument should be a constant", goto error
);
3567 is_zero
= isl_aff_plain_is_zero(aff2
);
3571 return set_nan_free(aff1
, aff2
);
3573 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3575 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3576 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3579 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3580 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3583 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3584 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3595 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3596 __isl_take isl_pw_aff
*pwaff2
)
3598 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3599 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3602 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3603 __isl_take isl_pw_aff
*pwaff2
)
3605 isl_pw_aff_align_params_bin(&pwaff1
, &pwaff2
);
3606 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3609 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3611 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3612 __isl_take isl_pw_aff
*pa2
)
3616 is_cst
= isl_pw_aff_is_cst(pa2
);
3620 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3621 "second argument should be a piecewise constant",
3623 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3624 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3626 isl_pw_aff_free(pa1
);
3627 isl_pw_aff_free(pa2
);
3631 /* Compute the quotient of the integer division of "pa1" by "pa2"
3632 * with rounding towards zero.
3633 * "pa2" is assumed to be a piecewise constant.
3635 * In particular, return
3637 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3640 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3641 __isl_take isl_pw_aff
*pa2
)
3647 is_cst
= isl_pw_aff_is_cst(pa2
);
3651 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3652 "second argument should be a piecewise constant",
3655 pa1
= isl_pw_aff_div(pa1
, pa2
);
3657 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3658 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3659 c
= isl_pw_aff_ceil(pa1
);
3660 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3662 isl_pw_aff_free(pa1
);
3663 isl_pw_aff_free(pa2
);
3667 /* Compute the remainder of the integer division of "pa1" by "pa2"
3668 * with rounding towards zero.
3669 * "pa2" is assumed to be a piecewise constant.
3671 * In particular, return
3673 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3676 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3677 __isl_take isl_pw_aff
*pa2
)
3682 is_cst
= isl_pw_aff_is_cst(pa2
);
3686 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3687 "second argument should be a piecewise constant",
3689 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3690 res
= isl_pw_aff_mul(pa2
, res
);
3691 res
= isl_pw_aff_sub(pa1
, res
);
3694 isl_pw_aff_free(pa1
);
3695 isl_pw_aff_free(pa2
);
3699 /* Does either of "pa1" or "pa2" involve any NaN?
3701 static isl_bool
either_involves_nan(__isl_keep isl_pw_aff
*pa1
,
3702 __isl_keep isl_pw_aff
*pa2
)
3706 has_nan
= isl_pw_aff_involves_nan(pa1
);
3707 if (has_nan
< 0 || has_nan
)
3709 return isl_pw_aff_involves_nan(pa2
);
3712 /* Return a piecewise affine expression defined on the specified domain
3713 * that represents NaN.
3715 static __isl_give isl_pw_aff
*nan_on_domain_set(__isl_take isl_set
*dom
)
3717 isl_local_space
*ls
;
3720 ls
= isl_local_space_from_space(isl_set_get_space(dom
));
3721 pa
= isl_pw_aff_nan_on_domain(ls
);
3722 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3727 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3728 * by a NaN on their shared domain.
3730 * In principle, the result could be refined to only being NaN
3731 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3733 static __isl_give isl_pw_aff
*replace_by_nan(__isl_take isl_pw_aff
*pa1
,
3734 __isl_take isl_pw_aff
*pa2
)
3738 dom
= isl_set_intersect(isl_pw_aff_domain(pa1
), isl_pw_aff_domain(pa2
));
3739 return nan_on_domain_set(dom
);
3742 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3743 __isl_take isl_pw_aff
*pwaff2
)
3748 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3749 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3750 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3751 isl_pw_aff_copy(pwaff2
));
3752 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3753 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3756 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3757 __isl_take isl_pw_aff
*pwaff2
)
3762 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3763 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3764 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3765 isl_pw_aff_copy(pwaff2
));
3766 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3767 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3770 /* Return an expression for the minimum (if "max" is not set) or
3771 * the maximum (if "max" is set) of "pa1" and "pa2".
3772 * If either expression involves any NaN, then return a NaN
3773 * on the shared domain as result.
3775 static __isl_give isl_pw_aff
*pw_aff_min_max(__isl_take isl_pw_aff
*pa1
,
3776 __isl_take isl_pw_aff
*pa2
, int max
)
3780 has_nan
= either_involves_nan(pa1
, pa2
);
3782 pa1
= isl_pw_aff_free(pa1
);
3784 return replace_by_nan(pa1
, pa2
);
3786 isl_pw_aff_align_params_bin(&pa1
, &pa2
);
3788 return pw_aff_max(pa1
, pa2
);
3790 return pw_aff_min(pa1
, pa2
);
3793 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3795 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3796 __isl_take isl_pw_aff
*pwaff2
)
3798 return pw_aff_min_max(pwaff1
, pwaff2
, 0);
3801 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3803 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3804 __isl_take isl_pw_aff
*pwaff2
)
3806 return pw_aff_min_max(pwaff1
, pwaff2
, 1);
3809 /* Does "pa" not involve any NaN?
3811 static isl_bool
pw_aff_no_nan(__isl_keep isl_pw_aff
*pa
, void *user
)
3813 return isl_bool_not(isl_pw_aff_involves_nan(pa
));
3816 /* Does any element of "list" involve any NaN?
3818 * That is, is it not the case that every element does not involve any NaN?
3820 static isl_bool
isl_pw_aff_list_involves_nan(__isl_keep isl_pw_aff_list
*list
)
3822 return isl_bool_not(isl_pw_aff_list_every(list
, &pw_aff_no_nan
, NULL
));
3825 /* Replace "list" (consisting of "n" elements, of which
3826 * at least one element involves a NaN)
3827 * by a NaN on the shared domain of the elements.
3829 * In principle, the result could be refined to only being NaN
3830 * on the parts of this domain where at least one of the elements is NaN.
3832 static __isl_give isl_pw_aff
*replace_list_by_nan(
3833 __isl_take isl_pw_aff_list
*list
, int n
)
3838 dom
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, 0));
3839 for (i
= 1; i
< n
; ++i
) {
3842 dom_i
= isl_pw_aff_domain(isl_pw_aff_list_get_at(list
, i
));
3843 dom
= isl_set_intersect(dom
, dom_i
);
3846 isl_pw_aff_list_free(list
);
3847 return nan_on_domain_set(dom
);
3850 /* Return the set where the element at "pos1" of "list" is less than or
3851 * equal to the element at "pos2".
3852 * Equality is only allowed if "pos1" is smaller than "pos2".
3854 static __isl_give isl_set
*less(__isl_keep isl_pw_aff_list
*list
,
3857 isl_pw_aff
*pa1
, *pa2
;
3859 pa1
= isl_pw_aff_list_get_at(list
, pos1
);
3860 pa2
= isl_pw_aff_list_get_at(list
, pos2
);
3863 return isl_pw_aff_le_set(pa1
, pa2
);
3865 return isl_pw_aff_lt_set(pa1
, pa2
);
3868 /* Return an isl_pw_aff that maps each element in the intersection of the
3869 * domains of the piecewise affine expressions in "list"
3870 * to the maximal (if "max" is set) or minimal (if "max" is not set)
3871 * expression in "list" at that element.
3872 * If any expression involves any NaN, then return a NaN
3873 * on the shared domain as result.
3875 * If "list" has n elements, then the result consists of n pieces,
3876 * where, in the case of a minimum, each piece has as value expression
3877 * the value expression of one of the elements and as domain
3878 * the set of elements where that value expression
3879 * is less than (or equal) to the other value expressions.
3880 * In the case of a maximum, the condition is
3881 * that all the other value expressions are less than (or equal)
3882 * to the given value expression.
3884 * In order to produce disjoint pieces, a pair of elements
3885 * in the original domain is only allowed to be equal to each other
3886 * on exactly one of the two pieces corresponding to the two elements.
3887 * The position in the list is used to break ties.
3888 * In particular, in the case of a minimum,
3889 * in the piece corresponding to a given element,
3890 * this element is allowed to be equal to any later element in the list,
3891 * but not to any earlier element in the list.
3893 static __isl_give isl_pw_aff
*isl_pw_aff_list_opt(
3894 __isl_take isl_pw_aff_list
*list
, int max
)
3900 isl_pw_aff
*pa
, *res
;
3902 n
= isl_pw_aff_list_size(list
);
3906 isl_die(isl_pw_aff_list_get_ctx(list
), isl_error_invalid
,
3907 "list should contain at least one element", goto error
);
3909 has_nan
= isl_pw_aff_list_involves_nan(list
);
3913 return replace_list_by_nan(list
, n
);
3915 pa
= isl_pw_aff_list_get_at(list
, 0);
3916 space
= isl_pw_aff_get_space(pa
);
3917 isl_pw_aff_free(pa
);
3918 res
= isl_pw_aff_empty(space
);
3920 for (i
= 0; i
< n
; ++i
) {
3921 pa
= isl_pw_aff_list_get_at(list
, i
);
3922 for (j
= 0; j
< n
; ++j
) {
3928 dom
= less(list
, j
, i
);
3930 dom
= less(list
, i
, j
);
3932 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
3934 res
= isl_pw_aff_add_disjoint(res
, pa
);
3937 isl_pw_aff_list_free(list
);
3940 isl_pw_aff_list_free(list
);
3944 /* Return an isl_pw_aff that maps each element in the intersection of the
3945 * domains of the elements of list to the minimal corresponding affine
3948 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3950 return isl_pw_aff_list_opt(list
, 0);
3953 /* Return an isl_pw_aff that maps each element in the intersection of the
3954 * domains of the elements of list to the maximal corresponding affine
3957 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3959 return isl_pw_aff_list_opt(list
, 1);
3962 /* Mark the domains of "pwaff" as rational.
3964 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3968 pwaff
= isl_pw_aff_cow(pwaff
);
3974 for (i
= 0; i
< pwaff
->n
; ++i
) {
3975 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3976 if (!pwaff
->p
[i
].set
)
3977 return isl_pw_aff_free(pwaff
);
3983 /* Mark the domains of the elements of "list" as rational.
3985 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3986 __isl_take isl_pw_aff_list
*list
)
3996 for (i
= 0; i
< n
; ++i
) {
3999 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
4000 pa
= isl_pw_aff_set_rational(pa
);
4001 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
4007 /* Do the parameters of "aff" match those of "space"?
4009 isl_bool
isl_aff_matching_params(__isl_keep isl_aff
*aff
,
4010 __isl_keep isl_space
*space
)
4012 isl_space
*aff_space
;
4016 return isl_bool_error
;
4018 aff_space
= isl_aff_get_domain_space(aff
);
4020 match
= isl_space_has_equal_params(space
, aff_space
);
4022 isl_space_free(aff_space
);
4026 /* Check that the domain space of "aff" matches "space".
4028 isl_stat
isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
4029 __isl_keep isl_space
*space
)
4031 isl_space
*aff_space
;
4035 return isl_stat_error
;
4037 aff_space
= isl_aff_get_domain_space(aff
);
4039 match
= isl_space_has_equal_params(space
, aff_space
);
4043 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4044 "parameters don't match", goto error
);
4045 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
4046 aff_space
, isl_dim_set
);
4050 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
4051 "domains don't match", goto error
);
4052 isl_space_free(aff_space
);
4055 isl_space_free(aff_space
);
4056 return isl_stat_error
;
4059 /* Return the shared (universe) domain of the elements of "ma".
4061 * Since an isl_multi_aff (and an isl_aff) is always total,
4062 * the domain is always the universe set in its domain space.
4063 * This is a helper function for use in the generic isl_multi_*_bind.
4065 static __isl_give isl_basic_set
*isl_multi_aff_domain(
4066 __isl_take isl_multi_aff
*ma
)
4070 space
= isl_multi_aff_get_space(ma
);
4071 isl_multi_aff_free(ma
);
4073 return isl_basic_set_universe(isl_space_domain(space
));
4079 #include <isl_multi_no_explicit_domain.c>
4080 #include <isl_multi_templ.c>
4081 #include <isl_multi_un_op_templ.c>
4082 #include <isl_multi_bin_val_templ.c>
4083 #include <isl_multi_add_constant_templ.c>
4084 #include <isl_multi_align_set.c>
4085 #include <isl_multi_arith_templ.c>
4086 #include <isl_multi_bind_domain_templ.c>
4087 #include <isl_multi_cmp.c>
4088 #include <isl_multi_dim_id_templ.c>
4089 #include <isl_multi_dims.c>
4090 #include <isl_multi_domain_reverse_templ.c>
4091 #include <isl_multi_floor.c>
4092 #include <isl_multi_from_base_templ.c>
4093 #include <isl_multi_identity_templ.c>
4094 #include <isl_multi_insert_domain_templ.c>
4095 #include <isl_multi_locals_templ.c>
4096 #include <isl_multi_move_dims_templ.c>
4097 #include <isl_multi_nan_templ.c>
4098 #include <isl_multi_product_templ.c>
4099 #include <isl_multi_splice_templ.c>
4100 #include <isl_multi_tuple_id_templ.c>
4101 #include <isl_multi_unbind_params_templ.c>
4102 #include <isl_multi_zero_templ.c>
4106 #include <isl_multi_check_domain_templ.c>
4107 #include <isl_multi_apply_set_no_explicit_domain_templ.c>
4108 #include <isl_multi_gist.c>
4111 #define DOMBASE basic_set
4112 #include <isl_multi_bind_templ.c>
4114 /* Construct an isl_multi_aff living in "space" that corresponds
4115 * to the affine transformation matrix "mat".
4117 __isl_give isl_multi_aff
*isl_multi_aff_from_aff_mat(
4118 __isl_take isl_space
*space
, __isl_take isl_mat
*mat
)
4121 isl_local_space
*ls
= NULL
;
4122 isl_multi_aff
*ma
= NULL
;
4123 isl_size n_row
, n_col
, n_out
, total
;
4129 ctx
= isl_mat_get_ctx(mat
);
4131 n_row
= isl_mat_rows(mat
);
4132 n_col
= isl_mat_cols(mat
);
4133 n_out
= isl_space_dim(space
, isl_dim_out
);
4134 total
= isl_space_dim(space
, isl_dim_all
);
4135 if (n_row
< 0 || n_col
< 0 || n_out
< 0 || total
< 0)
4138 isl_die(ctx
, isl_error_invalid
,
4139 "insufficient number of rows", goto error
);
4141 isl_die(ctx
, isl_error_invalid
,
4142 "insufficient number of columns", goto error
);
4143 if (1 + n_out
!= n_row
|| 2 + total
!= n_row
+ n_col
)
4144 isl_die(ctx
, isl_error_invalid
,
4145 "dimension mismatch", goto error
);
4147 ma
= isl_multi_aff_zero(isl_space_copy(space
));
4148 space
= isl_space_domain(space
);
4149 ls
= isl_local_space_from_space(isl_space_copy(space
));
4151 for (i
= 0; i
< n_row
- 1; ++i
) {
4155 v
= isl_vec_alloc(ctx
, 1 + n_col
);
4158 isl_int_set(v
->el
[0], mat
->row
[0][0]);
4159 isl_seq_cpy(v
->el
+ 1, mat
->row
[1 + i
], n_col
);
4160 v
= isl_vec_normalize(v
);
4161 aff
= isl_aff_alloc_vec_validated(isl_local_space_copy(ls
), v
);
4162 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4165 isl_space_free(space
);
4166 isl_local_space_free(ls
);
4170 isl_space_free(space
);
4171 isl_local_space_free(ls
);
4173 isl_multi_aff_free(ma
);
4177 /* Return the constant terms of the affine expressions of "ma".
4179 __isl_give isl_multi_val
*isl_multi_aff_get_constant_multi_val(
4180 __isl_keep isl_multi_aff
*ma
)
4187 n
= isl_multi_aff_size(ma
);
4190 space
= isl_space_range(isl_multi_aff_get_space(ma
));
4191 space
= isl_space_drop_all_params(space
);
4192 mv
= isl_multi_val_zero(space
);
4194 for (i
= 0; i
< n
; ++i
) {
4198 aff
= isl_multi_aff_get_at(ma
, i
);
4199 val
= isl_aff_get_constant_val(aff
);
4201 mv
= isl_multi_val_set_at(mv
, i
, val
);
4207 /* Remove any internal structure of the domain of "ma".
4208 * If there is any such internal structure in the input,
4209 * then the name of the corresponding space is also removed.
4211 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
4212 __isl_take isl_multi_aff
*ma
)
4219 if (!ma
->space
->nested
[0])
4222 space
= isl_multi_aff_get_space(ma
);
4223 space
= isl_space_flatten_domain(space
);
4224 ma
= isl_multi_aff_reset_space(ma
, space
);
4229 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4230 * of the space to its domain.
4232 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
4236 isl_local_space
*ls
;
4241 if (!isl_space_is_map(space
))
4242 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4243 "not a map space", goto error
);
4245 n_in
= isl_space_dim(space
, isl_dim_in
);
4248 space
= isl_space_domain_map(space
);
4250 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4252 isl_space_free(space
);
4256 space
= isl_space_domain(space
);
4257 ls
= isl_local_space_from_space(space
);
4258 for (i
= 0; i
< n_in
; ++i
) {
4261 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4263 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4265 isl_local_space_free(ls
);
4268 isl_space_free(space
);
4272 /* This function performs the same operation as isl_multi_aff_domain_map,
4273 * but is considered as a function on an isl_space when exported.
4275 __isl_give isl_multi_aff
*isl_space_domain_map_multi_aff(
4276 __isl_take isl_space
*space
)
4278 return isl_multi_aff_domain_map(space
);
4281 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4282 * of the space to its range.
4284 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
4287 isl_size n_in
, n_out
;
4288 isl_local_space
*ls
;
4293 if (!isl_space_is_map(space
))
4294 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4295 "not a map space", goto error
);
4297 n_in
= isl_space_dim(space
, isl_dim_in
);
4298 n_out
= isl_space_dim(space
, isl_dim_out
);
4299 if (n_in
< 0 || n_out
< 0)
4301 space
= isl_space_range_map(space
);
4303 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4305 isl_space_free(space
);
4309 space
= isl_space_domain(space
);
4310 ls
= isl_local_space_from_space(space
);
4311 for (i
= 0; i
< n_out
; ++i
) {
4314 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4315 isl_dim_set
, n_in
+ i
);
4316 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4318 isl_local_space_free(ls
);
4321 isl_space_free(space
);
4325 /* This function performs the same operation as isl_multi_aff_range_map,
4326 * but is considered as a function on an isl_space when exported.
4328 __isl_give isl_multi_aff
*isl_space_range_map_multi_aff(
4329 __isl_take isl_space
*space
)
4331 return isl_multi_aff_range_map(space
);
4334 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4335 * of the space to its domain.
4337 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_domain_map(
4338 __isl_take isl_space
*space
)
4340 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space
));
4343 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4344 * but is considered as a function on an isl_space when exported.
4346 __isl_give isl_pw_multi_aff
*isl_space_domain_map_pw_multi_aff(
4347 __isl_take isl_space
*space
)
4349 return isl_pw_multi_aff_domain_map(space
);
4352 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4353 * of the space to its range.
4355 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
4356 __isl_take isl_space
*space
)
4358 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
4361 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4362 * but is considered as a function on an isl_space when exported.
4364 __isl_give isl_pw_multi_aff
*isl_space_range_map_pw_multi_aff(
4365 __isl_take isl_space
*space
)
4367 return isl_pw_multi_aff_range_map(space
);
4370 /* Given the space of a set and a range of set dimensions,
4371 * construct an isl_multi_aff that projects out those dimensions.
4373 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
4374 __isl_take isl_space
*space
, enum isl_dim_type type
,
4375 unsigned first
, unsigned n
)
4379 isl_local_space
*ls
;
4384 if (!isl_space_is_set(space
))
4385 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
4386 "expecting set space", goto error
);
4387 if (type
!= isl_dim_set
)
4388 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
4389 "only set dimensions can be projected out", goto error
);
4390 if (isl_space_check_range(space
, type
, first
, n
) < 0)
4393 dim
= isl_space_dim(space
, isl_dim_set
);
4397 space
= isl_space_from_domain(space
);
4398 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
4401 return isl_multi_aff_alloc(space
);
4403 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
4404 space
= isl_space_domain(space
);
4405 ls
= isl_local_space_from_space(space
);
4407 for (i
= 0; i
< first
; ++i
) {
4410 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4412 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4415 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
4418 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4419 isl_dim_set
, first
+ n
+ i
);
4420 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
4423 isl_local_space_free(ls
);
4426 isl_space_free(space
);
4430 /* Given the space of a set and a range of set dimensions,
4431 * construct an isl_pw_multi_aff that projects out those dimensions.
4433 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
4434 __isl_take isl_space
*space
, enum isl_dim_type type
,
4435 unsigned first
, unsigned n
)
4439 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
4440 return isl_pw_multi_aff_from_multi_aff(ma
);
4443 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4444 * but is considered as a function on an isl_multi_aff when exported.
4446 __isl_give isl_pw_multi_aff
*isl_multi_aff_to_pw_multi_aff(
4447 __isl_take isl_multi_aff
*ma
)
4449 return isl_pw_multi_aff_from_multi_aff(ma
);
4452 /* Create a piecewise multi-affine expression in the given space that maps each
4453 * input dimension to the corresponding output dimension.
4455 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
4456 __isl_take isl_space
*space
)
4458 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
4461 /* Create a piecewise multi expression that maps elements in the given space
4464 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity_on_domain_space(
4465 __isl_take isl_space
*space
)
4469 ma
= isl_multi_aff_identity_on_domain_space(space
);
4470 return isl_pw_multi_aff_from_multi_aff(ma
);
4473 /* This function performs the same operation as
4474 * isl_pw_multi_aff_identity_on_domain_space,
4475 * but is considered as a function on an isl_space when exported.
4477 __isl_give isl_pw_multi_aff
*isl_space_identity_pw_multi_aff_on_domain(
4478 __isl_take isl_space
*space
)
4480 return isl_pw_multi_aff_identity_on_domain_space(space
);
4483 /* Exploit the equalities in "eq" to simplify the affine expressions.
4485 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
4486 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
4491 n
= isl_multi_aff_size(maff
);
4495 for (i
= 0; i
< n
; ++i
) {
4498 aff
= isl_multi_aff_take_at(maff
, i
);
4499 aff
= isl_aff_substitute_equalities(aff
,
4500 isl_basic_set_copy(eq
));
4501 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4504 isl_basic_set_free(eq
);
4507 isl_basic_set_free(eq
);
4508 isl_multi_aff_free(maff
);
4512 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
4518 n
= isl_multi_aff_size(maff
);
4520 return isl_multi_aff_free(maff
);
4522 for (i
= 0; i
< n
; ++i
) {
4525 aff
= isl_multi_aff_take_at(maff
, i
);
4526 aff
= isl_aff_scale(aff
, f
);
4527 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
4533 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
4534 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
4536 maff1
= isl_multi_aff_add(maff1
, maff2
);
4537 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4541 isl_bool
isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4544 return isl_bool_error
;
4546 return isl_bool_false
;
4549 /* Return the set of domain elements where "ma1" is lexicographically
4550 * smaller than or equal to "ma2".
4552 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4553 __isl_take isl_multi_aff
*ma2
)
4555 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4558 /* Return the set of domain elements where "ma1" is lexicographically
4559 * smaller than "ma2".
4561 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4562 __isl_take isl_multi_aff
*ma2
)
4564 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4567 /* Return the set of domain elements where "ma1" is lexicographically
4568 * greater than to "ma2". If "equal" is set, then include the domain
4569 * elements where they are equal.
4570 * Do this for the case where there are no entries.
4571 * In this case, "ma1" cannot be greater than "ma2",
4572 * but it is (greater than or) equal to "ma2".
4574 static __isl_give isl_set
*isl_multi_aff_lex_gte_set_0d(
4575 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4579 space
= isl_multi_aff_get_domain_space(ma1
);
4581 isl_multi_aff_free(ma1
);
4582 isl_multi_aff_free(ma2
);
4585 return isl_set_universe(space
);
4587 return isl_set_empty(space
);
4590 /* Return the set where entry "i" of "ma1" and "ma2"
4591 * satisfy the relation prescribed by "cmp".
4593 static __isl_give isl_set
*isl_multi_aff_order_at(__isl_keep isl_multi_aff
*ma1
,
4594 __isl_keep isl_multi_aff
*ma2
, int i
,
4595 __isl_give isl_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
4596 __isl_take isl_aff
*aff2
))
4598 isl_aff
*aff1
, *aff2
;
4600 aff1
= isl_multi_aff_get_at(ma1
, i
);
4601 aff2
= isl_multi_aff_get_at(ma2
, i
);
4602 return cmp(aff1
, aff2
);
4605 /* Return the set of domain elements where "ma1" is lexicographically
4606 * greater than to "ma2". If "equal" is set, then include the domain
4607 * elements where they are equal.
4609 * In particular, for all but the final entry,
4610 * include the set of elements where this entry is strictly greater in "ma1"
4611 * and all previous entries are equal.
4612 * The final entry is also allowed to be equal in the two functions
4613 * if "equal" is set.
4615 * The case where there are no entries is handled separately.
4617 static __isl_give isl_set
*isl_multi_aff_lex_gte_set(
4618 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
, int equal
)
4627 if (isl_multi_aff_check_equal_space(ma1
, ma2
) < 0)
4629 n
= isl_multi_aff_size(ma1
);
4633 return isl_multi_aff_lex_gte_set_0d(ma1
, ma2
, equal
);
4635 space
= isl_multi_aff_get_domain_space(ma1
);
4636 res
= isl_set_empty(isl_space_copy(space
));
4637 equal_set
= isl_set_universe(space
);
4639 for (i
= 0; i
+ 1 < n
; ++i
) {
4643 gt
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_gt_set
);
4644 gt
= isl_set_intersect(gt
, isl_set_copy(equal_set
));
4645 res
= isl_set_union(res
, gt
);
4646 eq
= isl_multi_aff_order_at(ma1
, ma2
, i
, &isl_aff_eq_set
);
4647 equal_set
= isl_set_intersect(equal_set
, eq
);
4649 empty
= isl_set_is_empty(equal_set
);
4650 if (empty
>= 0 && empty
)
4655 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_ge_set
);
4657 gte
= isl_multi_aff_order_at(ma1
, ma2
, n
- 1, &isl_aff_gt_set
);
4658 isl_multi_aff_free(ma1
);
4659 isl_multi_aff_free(ma2
);
4661 gte
= isl_set_intersect(gte
, equal_set
);
4662 return isl_set_union(res
, gte
);
4664 isl_multi_aff_free(ma1
);
4665 isl_multi_aff_free(ma2
);
4669 /* Return the set of domain elements where "ma1" is lexicographically
4670 * greater than or equal to "ma2".
4672 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4673 __isl_take isl_multi_aff
*ma2
)
4675 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 1);
4678 /* Return the set of domain elements where "ma1" is lexicographically
4679 * greater than "ma2".
4681 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4682 __isl_take isl_multi_aff
*ma2
)
4684 return isl_multi_aff_lex_gte_set(ma1
, ma2
, 0);
4687 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4690 #define PW isl_pw_multi_aff
4692 #define BASE multi_aff
4694 #define EL_IS_ZERO is_empty
4698 #define IS_ZERO is_empty
4701 #undef DEFAULT_IS_ZERO
4702 #define DEFAULT_IS_ZERO 0
4704 #include <isl_pw_templ.c>
4705 #include <isl_pw_un_op_templ.c>
4706 #include <isl_pw_add_constant_multi_val_templ.c>
4707 #include <isl_pw_add_constant_val_templ.c>
4708 #include <isl_pw_add_disjoint_templ.c>
4709 #include <isl_pw_bind_domain_templ.c>
4710 #include <isl_pw_domain_reverse_templ.c>
4711 #include <isl_pw_fix_templ.c>
4712 #include <isl_pw_from_range_templ.c>
4713 #include <isl_pw_insert_dims_templ.c>
4714 #include <isl_pw_insert_domain_templ.c>
4715 #include <isl_pw_locals_templ.c>
4716 #include <isl_pw_move_dims_templ.c>
4717 #include <isl_pw_neg_templ.c>
4718 #include <isl_pw_pullback_templ.c>
4719 #include <isl_pw_range_tuple_id_templ.c>
4720 #include <isl_pw_union_opt.c>
4723 #define BASE pw_multi_aff
4725 #include <isl_union_multi.c>
4726 #include "isl_union_locals_templ.c"
4727 #include <isl_union_neg.c>
4728 #include <isl_union_sub_templ.c>
4731 #define BASE multi_aff
4733 #include <isl_union_pw_templ.c>
4735 /* Generic function for extracting a factor from a product "pma".
4736 * "check_space" checks that the space is that of the right kind of product.
4737 * "space_factor" extracts the factor from the space.
4738 * "multi_aff_factor" extracts the factor from the constituent functions.
4740 static __isl_give isl_pw_multi_aff
*pw_multi_aff_factor(
4741 __isl_take isl_pw_multi_aff
*pma
,
4742 isl_stat (*check_space
)(__isl_keep isl_pw_multi_aff
*pma
),
4743 __isl_give isl_space
*(*space_factor
)(__isl_take isl_space
*space
),
4744 __isl_give isl_multi_aff
*(*multi_aff_factor
)(
4745 __isl_take isl_multi_aff
*ma
))
4750 if (check_space(pma
) < 0)
4751 return isl_pw_multi_aff_free(pma
);
4753 space
= isl_pw_multi_aff_take_space(pma
);
4754 space
= space_factor(space
);
4756 for (i
= 0; pma
&& i
< pma
->n
; ++i
) {
4759 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
4760 ma
= multi_aff_factor(ma
);
4761 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
4764 pma
= isl_pw_multi_aff_restore_space(pma
, space
);
4769 /* Is the range of "pma" a wrapped relation?
4771 static isl_bool
isl_pw_multi_aff_range_is_wrapping(
4772 __isl_keep isl_pw_multi_aff
*pma
)
4774 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma
));
4777 /* Check that the range of "pma" is a product.
4779 static isl_stat
pw_multi_aff_check_range_product(
4780 __isl_keep isl_pw_multi_aff
*pma
)
4784 wraps
= isl_pw_multi_aff_range_is_wrapping(pma
);
4786 return isl_stat_error
;
4788 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4789 "range is not a product", return isl_stat_error
);
4793 /* Given a function A -> [B -> C], extract the function A -> B.
4795 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_domain(
4796 __isl_take isl_pw_multi_aff
*pma
)
4798 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4799 &isl_space_range_factor_domain
,
4800 &isl_multi_aff_range_factor_domain
);
4803 /* Given a function A -> [B -> C], extract the function A -> C.
4805 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_factor_range(
4806 __isl_take isl_pw_multi_aff
*pma
)
4808 return pw_multi_aff_factor(pma
, &pw_multi_aff_check_range_product
,
4809 &isl_space_range_factor_range
,
4810 &isl_multi_aff_range_factor_range
);
4813 /* Given two piecewise multi affine expressions, return a piecewise
4814 * multi-affine expression defined on the union of the definition domains
4815 * of the inputs that is equal to the lexicographic maximum of the two
4816 * inputs on each cell. If only one of the two inputs is defined on
4817 * a given cell, then it is considered to be the maximum.
4819 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4820 __isl_take isl_pw_multi_aff
*pma1
,
4821 __isl_take isl_pw_multi_aff
*pma2
)
4823 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4824 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4825 &isl_multi_aff_lex_ge_set
);
4828 /* Given two piecewise multi affine expressions, return a piecewise
4829 * multi-affine expression defined on the union of the definition domains
4830 * of the inputs that is equal to the lexicographic minimum of the two
4831 * inputs on each cell. If only one of the two inputs is defined on
4832 * a given cell, then it is considered to be the minimum.
4834 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4835 __isl_take isl_pw_multi_aff
*pma1
,
4836 __isl_take isl_pw_multi_aff
*pma2
)
4838 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4839 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4840 &isl_multi_aff_lex_le_set
);
4843 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4844 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4846 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4847 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4848 &isl_multi_aff_add
);
4851 /* Subtract "pma2" from "pma1" and return the result.
4853 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4854 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4856 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
4857 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4858 &isl_multi_aff_sub
);
4861 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4862 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4864 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4865 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4869 isl_pw_multi_aff
*res
;
4871 if (isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
) < 0)
4874 n
= pma1
->n
* pma2
->n
;
4875 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4876 isl_space_copy(pma2
->dim
));
4877 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4879 for (i
= 0; i
< pma1
->n
; ++i
) {
4880 for (j
= 0; j
< pma2
->n
; ++j
) {
4884 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4885 isl_set_copy(pma2
->p
[j
].set
));
4886 ma
= isl_multi_aff_product(
4887 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4888 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4889 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4893 isl_pw_multi_aff_free(pma1
);
4894 isl_pw_multi_aff_free(pma2
);
4897 isl_pw_multi_aff_free(pma1
);
4898 isl_pw_multi_aff_free(pma2
);
4902 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4903 * denominator "denom".
4904 * "denom" is allowed to be negative, in which case the actual denominator
4905 * is -denom and the expressions are added instead.
4907 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4908 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4914 first
= isl_seq_first_non_zero(c
, n
);
4918 sign
= isl_int_sgn(denom
);
4920 isl_int_abs(d
, denom
);
4921 for (i
= first
; i
< n
; ++i
) {
4924 if (isl_int_is_zero(c
[i
]))
4926 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4927 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4928 aff_i
= isl_aff_scale_down(aff_i
, d
);
4930 aff
= isl_aff_sub(aff
, aff_i
);
4932 aff
= isl_aff_add(aff
, aff_i
);
4939 /* Extract an affine expression that expresses the output dimension "pos"
4940 * of "bmap" in terms of the parameters and input dimensions from
4942 * Note that this expression may involve integer divisions defined
4943 * in terms of parameters and input dimensions.
4944 * The equality may also involve references to earlier (but not later)
4945 * output dimensions. These are replaced by the corresponding elements
4948 * If the equality is of the form
4950 * f(i) + h(j) + a x + g(i) = 0,
4952 * with f(i) a linear combinations of the parameters and input dimensions,
4953 * g(i) a linear combination of integer divisions defined in terms of the same
4954 * and h(j) a linear combinations of earlier output dimensions,
4955 * then the affine expression is
4957 * (-f(i) - g(i))/a - h(j)/a
4959 * If the equality is of the form
4961 * f(i) + h(j) - a x + g(i) = 0,
4963 * then the affine expression is
4965 * (f(i) + g(i))/a - h(j)/(-a)
4968 * If "div" refers to an integer division (i.e., it is smaller than
4969 * the number of integer divisions), then the equality constraint
4970 * does involve an integer division (the one at position "div") that
4971 * is defined in terms of output dimensions. However, this integer
4972 * division can be eliminated by exploiting a pair of constraints
4973 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4974 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4976 * In particular, let
4978 * x = e(i) + m floor(...)
4980 * with e(i) the expression derived above and floor(...) the integer
4981 * division involving output dimensions.
4992 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4993 * = (e(i) - l) mod m
4997 * x - l = (e(i) - l) mod m
5001 * x = ((e(i) - l) mod m) + l
5003 * The variable "shift" below contains the expression -l, which may
5004 * also involve a linear combination of earlier output dimensions.
5006 static __isl_give isl_aff
*extract_aff_from_equality(
5007 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
5008 __isl_keep isl_multi_aff
*ma
)
5011 isl_size n_div
, n_out
;
5013 isl_local_space
*ls
;
5014 isl_aff
*aff
, *shift
;
5017 ctx
= isl_basic_map_get_ctx(bmap
);
5018 ls
= isl_basic_map_get_local_space(bmap
);
5019 ls
= isl_local_space_domain(ls
);
5020 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
5023 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
5024 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5025 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5026 if (n_out
< 0 || n_div
< 0)
5028 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
5029 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
5030 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
5031 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
5033 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
5034 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
5035 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
5038 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
5039 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
5040 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
5041 bmap
->eq
[eq
][o_out
+ pos
]);
5043 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
5046 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
5047 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
5048 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
5049 isl_int_set_si(shift
->v
->el
[0], 1);
5050 shift
= subtract_initial(shift
, ma
, pos
,
5051 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
5052 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
5053 mod
= isl_val_int_from_isl_int(ctx
,
5054 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
5055 mod
= isl_val_abs(mod
);
5056 aff
= isl_aff_mod_val(aff
, mod
);
5057 aff
= isl_aff_sub(aff
, shift
);
5060 isl_local_space_free(ls
);
5063 isl_local_space_free(ls
);
5068 /* Given a basic map with output dimensions defined
5069 * in terms of the parameters input dimensions and earlier
5070 * output dimensions using an equality (and possibly a pair on inequalities),
5071 * extract an isl_aff that expresses output dimension "pos" in terms
5072 * of the parameters and input dimensions.
5073 * Note that this expression may involve integer divisions defined
5074 * in terms of parameters and input dimensions.
5075 * "ma" contains the expressions corresponding to earlier output dimensions.
5077 * This function shares some similarities with
5078 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
5080 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
5081 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
5088 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
5089 if (eq
>= bmap
->n_eq
)
5090 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
5091 "unable to find suitable equality", return NULL
);
5092 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
5094 aff
= isl_aff_remove_unused_divs(aff
);
5098 /* Given a basic map where each output dimension is defined
5099 * in terms of the parameters and input dimensions using an equality,
5100 * extract an isl_multi_aff that expresses the output dimensions in terms
5101 * of the parameters and input dimensions.
5103 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
5104 __isl_take isl_basic_map
*bmap
)
5113 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
5114 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
5116 ma
= isl_multi_aff_free(ma
);
5118 for (i
= 0; i
< n_out
; ++i
) {
5121 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
5122 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5125 isl_basic_map_free(bmap
);
5130 /* Given a basic set where each set dimension is defined
5131 * in terms of the parameters using an equality,
5132 * extract an isl_multi_aff that expresses the set dimensions in terms
5133 * of the parameters.
5135 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
5136 __isl_take isl_basic_set
*bset
)
5138 return extract_isl_multi_aff_from_basic_map(bset
);
5141 /* Create an isl_pw_multi_aff that is equivalent to
5142 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
5143 * The given basic map is such that each output dimension is defined
5144 * in terms of the parameters and input dimensions using an equality.
5146 * Since some applications expect the result of isl_pw_multi_aff_from_map
5147 * to only contain integer affine expressions, we compute the floor
5148 * of the expression before returning.
5150 * Remove all constraints involving local variables without
5151 * an explicit representation (resulting in the removal of those
5152 * local variables) prior to the actual extraction to ensure
5153 * that the local spaces in which the resulting affine expressions
5154 * are created do not contain any unknown local variables.
5155 * Removing such constraints is safe because constraints involving
5156 * unknown local variables are not used to determine whether
5157 * a basic map is obviously single-valued.
5159 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
5160 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
5164 bmap
= isl_basic_map_drop_constraints_involving_unknown_divs(bmap
);
5165 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
5166 ma
= isl_multi_aff_floor(ma
);
5167 return isl_pw_multi_aff_alloc(domain
, ma
);
5170 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5171 * This obviously only works if the input "map" is single-valued.
5172 * If so, we compute the lexicographic minimum of the image in the form
5173 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5174 * to its lexicographic minimum.
5175 * If the input is not single-valued, we produce an error.
5177 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
5178 __isl_take isl_map
*map
)
5182 isl_pw_multi_aff
*pma
;
5184 sv
= isl_map_is_single_valued(map
);
5188 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
5189 "map is not single-valued", goto error
);
5190 map
= isl_map_make_disjoint(map
);
5194 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
5196 for (i
= 0; i
< map
->n
; ++i
) {
5197 isl_pw_multi_aff
*pma_i
;
5198 isl_basic_map
*bmap
;
5199 bmap
= isl_basic_map_copy(map
->p
[i
]);
5200 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
5201 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
5211 /* Construct an isl_aff from the given domain local space "ls" and
5212 * coefficients "v", where the local space may involve
5213 * local variables without a known expression, as long as these
5214 * do not have a non-zero coefficient in "v".
5215 * These need to be pruned away first since an isl_aff cannot
5216 * reference any local variables without a known expression.
5217 * For simplicity, remove all local variables that have a zero coefficient and
5218 * that are not used in other local variables with a non-zero coefficient.
5220 static __isl_give isl_aff
*isl_aff_alloc_vec_prune(
5221 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
5224 isl_size n_div
, v_div
;
5226 n_div
= isl_local_space_dim(ls
, isl_dim_div
);
5227 v_div
= isl_local_space_var_offset(ls
, isl_dim_div
);
5228 if (n_div
< 0 || v_div
< 0 || !v
)
5230 for (i
= n_div
- 1; i
>= 0; --i
) {
5233 if (!isl_int_is_zero(v
->el
[1 + 1 + v_div
+ i
]))
5235 involves
= isl_local_space_involves_dims(ls
, isl_dim_div
, i
, 1);
5240 ls
= isl_local_space_drop_dims(ls
, isl_dim_div
, i
, 1);
5241 v
= isl_vec_drop_els(v
, 1 + 1 + v_div
+ i
, 1);
5246 return isl_aff_alloc_vec(ls
, v
);
5248 isl_local_space_free(ls
);
5253 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5254 * taking into account that the output dimension at position "d"
5255 * can be represented as
5257 * x = floor((e(...) + c1) / m)
5259 * given that constraint "i" is of the form
5261 * e(...) + c1 - m x >= 0
5263 * with e(...) an expression that does not involve any other output dimensions.
5266 * Let "map" be of the form
5270 * We construct a mapping
5272 * A -> [A -> x = floor(...)]
5274 * apply that to the map, obtaining
5276 * [A -> x = floor(...)] -> B
5278 * and equate dimension "d" to x.
5279 * We then compute a isl_pw_multi_aff representation of the resulting map
5280 * and plug in the mapping above.
5282 * The constraint "i" is guaranteed by the caller not to involve
5283 * any local variables without a known expression, but such local variables
5284 * may appear in other constraints. They therefore need to be removed
5285 * during the construction of the affine expression.
5287 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
5288 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
5290 isl_space
*space
= NULL
;
5291 isl_local_space
*ls
;
5297 isl_pw_multi_aff
*pma
;
5300 is_set
= isl_map_is_set(map
);
5304 space
= isl_space_domain(isl_map_get_space(map
));
5305 n_in
= isl_space_dim(space
, isl_dim_set
);
5309 ls
= isl_basic_map_get_local_space(hull
);
5311 ls
= isl_local_space_wrap(ls
);
5312 v
= isl_basic_map_inequality_extract_output_upper_bound(hull
, i
, d
);
5313 isl_basic_map_free(hull
);
5315 aff
= isl_aff_alloc_vec_prune(ls
, v
);
5316 aff
= isl_aff_floor(aff
);
5318 aff
= isl_aff_project_domain_on_params(aff
);
5319 isl_space_free(space
);
5320 ma
= isl_multi_aff_from_aff(aff
);
5322 aff
= isl_aff_domain_factor_domain(aff
);
5323 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
5324 ma
= isl_multi_aff_range_product(ma
,
5325 isl_multi_aff_from_aff(aff
));
5328 insert
= isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma
));
5329 map
= isl_map_apply_domain(map
, insert
);
5330 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
5331 pma
= isl_pw_multi_aff_from_map(map
);
5332 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
5336 isl_space_free(space
);
5338 isl_basic_map_free(hull
);
5342 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5344 * As a special case, we first check if there is any pair of constraints,
5345 * shared by all the basic maps in "map" that force a given dimension
5346 * to be equal to the floor of some affine combination of the input dimensions.
5348 * In particular, if we can find two constraints
5350 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5354 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5356 * where m > 1 and e only depends on parameters and input dimensions,
5359 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5361 * then we know that we can take
5363 * x = floor((e(...) + c1) / m)
5365 * without having to perform any computation.
5367 * Note that we know that
5371 * If c1 + c2 were 0, then we would have detected an equality during
5372 * simplification. If c1 + c2 were negative, then we would have detected
5375 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
5376 __isl_take isl_map
*map
)
5382 isl_basic_map
*hull
;
5384 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5385 dim
= isl_map_dim(map
, isl_dim_out
);
5386 n_ineq
= isl_basic_map_n_inequality(hull
);
5387 if (dim
< 0 || n_ineq
< 0)
5390 dim
= isl_map_dim(map
, isl_dim_out
);
5391 for (d
= 0; d
< dim
; ++d
) {
5392 i
= isl_basic_map_find_output_upper_div_constraint(hull
, d
);
5397 return pw_multi_aff_from_map_div(map
, hull
, d
, i
);
5399 isl_basic_map_free(hull
);
5400 return pw_multi_aff_from_map_base(map
);
5403 isl_basic_map_free(hull
);
5407 /* Given an affine expression
5409 * [A -> B] -> f(A,B)
5411 * construct an isl_multi_aff
5415 * such that dimension "d" in B' is set to "aff" and the remaining
5416 * dimensions are set equal to the corresponding dimensions in B.
5417 * "n_in" is the dimension of the space A.
5418 * "n_out" is the dimension of the space B.
5420 * If "is_set" is set, then the affine expression is of the form
5424 * and we construct an isl_multi_aff
5428 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
5429 unsigned n_in
, unsigned n_out
, int is_set
)
5433 isl_space
*space
, *space2
;
5434 isl_local_space
*ls
;
5436 space
= isl_aff_get_domain_space(aff
);
5437 ls
= isl_local_space_from_space(isl_space_copy(space
));
5438 space2
= isl_space_copy(space
);
5440 space2
= isl_space_range(isl_space_unwrap(space2
));
5441 space
= isl_space_map_from_domain_and_range(space
, space2
);
5442 ma
= isl_multi_aff_alloc(space
);
5443 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
5445 for (i
= 0; i
< n_out
; ++i
) {
5448 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
5449 isl_dim_set
, n_in
+ i
);
5450 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
5453 isl_local_space_free(ls
);
5458 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5459 * taking into account that the dimension at position "d" can be written as
5461 * x = m a + f(..) (1)
5463 * where m is equal to "gcd".
5464 * "i" is the index of the equality in "hull" that defines f(..).
5465 * In particular, the equality is of the form
5467 * f(..) - x + m g(existentials) = 0
5471 * -f(..) + x + m g(existentials) = 0
5473 * We basically plug (1) into "map", resulting in a map with "a"
5474 * in the range instead of "x". The corresponding isl_pw_multi_aff
5475 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5477 * Specifically, given the input map
5481 * We first wrap it into a set
5485 * and define (1) on top of the corresponding space, resulting in "aff".
5486 * We use this to create an isl_multi_aff that maps the output position "d"
5487 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5488 * We plug this into the wrapped map, unwrap the result and compute the
5489 * corresponding isl_pw_multi_aff.
5490 * The result is an expression
5498 * so that we can plug that into "aff", after extending the latter to
5504 * If "map" is actually a set, then there is no "A" space, meaning
5505 * that we do not need to perform any wrapping, and that the result
5506 * of the recursive call is of the form
5510 * which is plugged into a mapping of the form
5514 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
5515 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
5520 isl_local_space
*ls
;
5523 isl_pw_multi_aff
*pma
, *id
;
5529 is_set
= isl_map_is_set(map
);
5533 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
5534 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5535 if (n_in
< 0 || n_out
< 0)
5537 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5542 set
= isl_map_wrap(map
);
5543 space
= isl_space_map_from_set(isl_set_get_space(set
));
5544 ma
= isl_multi_aff_identity(space
);
5545 ls
= isl_local_space_from_space(isl_set_get_space(set
));
5546 aff
= isl_aff_alloc(ls
);
5548 isl_int_set_si(aff
->v
->el
[0], 1);
5549 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
5550 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
5553 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
5555 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
5557 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
5558 set
= isl_set_preimage_multi_aff(set
, ma
);
5560 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
5565 map
= isl_set_unwrap(set
);
5566 pma
= isl_pw_multi_aff_from_map(map
);
5569 space
= isl_pw_multi_aff_get_domain_space(pma
);
5570 space
= isl_space_map_from_set(space
);
5571 id
= isl_pw_multi_aff_identity(space
);
5572 pma
= isl_pw_multi_aff_range_product(id
, pma
);
5574 id
= isl_pw_multi_aff_from_multi_aff(ma
);
5575 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
5577 isl_basic_map_free(hull
);
5581 isl_basic_map_free(hull
);
5585 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5586 * "hull" contains the equalities valid for "map".
5588 * Check if any of the output dimensions is "strided".
5589 * That is, we check if it can be written as
5593 * with m greater than 1, a some combination of existentially quantified
5594 * variables and f an expression in the parameters and input dimensions.
5595 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5597 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5600 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
5601 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5610 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5611 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5612 if (n_div
< 0 || n_out
< 0)
5616 isl_basic_map_free(hull
);
5617 return pw_multi_aff_from_map_check_div(map
);
5622 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5623 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5625 for (i
= 0; i
< n_out
; ++i
) {
5626 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5627 isl_int
*eq
= hull
->eq
[j
];
5628 isl_pw_multi_aff
*res
;
5630 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5631 !isl_int_is_negone(eq
[o_out
+ i
]))
5633 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5635 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5636 n_out
- (i
+ 1)) != -1)
5638 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5639 if (isl_int_is_zero(gcd
))
5641 if (isl_int_is_one(gcd
))
5644 res
= pw_multi_aff_from_map_stride(map
, hull
,
5652 isl_basic_map_free(hull
);
5653 return pw_multi_aff_from_map_check_div(map
);
5656 isl_basic_map_free(hull
);
5660 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5662 * As a special case, we first check if all output dimensions are uniquely
5663 * defined in terms of the parameters and input dimensions over the entire
5664 * domain. If so, we extract the desired isl_pw_multi_aff directly
5665 * from the affine hull of "map" and its domain.
5667 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5670 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5674 isl_basic_map
*hull
;
5676 n
= isl_map_n_basic_map(map
);
5681 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5682 hull
= isl_basic_map_plain_affine_hull(hull
);
5683 sv
= isl_basic_map_plain_is_single_valued(hull
);
5685 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5687 isl_basic_map_free(hull
);
5689 map
= isl_map_detect_equalities(map
);
5690 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5691 sv
= isl_basic_map_plain_is_single_valued(hull
);
5693 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5695 return pw_multi_aff_from_map_check_strides(map
, hull
);
5696 isl_basic_map_free(hull
);
5702 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5703 * but is considered as a function on an isl_map when exported.
5705 __isl_give isl_pw_multi_aff
*isl_map_as_pw_multi_aff(__isl_take isl_map
*map
)
5707 return isl_pw_multi_aff_from_map(map
);
5710 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5712 return isl_pw_multi_aff_from_map(set
);
5715 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5716 * but is considered as a function on an isl_set when exported.
5718 __isl_give isl_pw_multi_aff
*isl_set_as_pw_multi_aff(__isl_take isl_set
*set
)
5720 return isl_pw_multi_aff_from_set(set
);
5723 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5726 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5728 isl_union_pw_multi_aff
**upma
= user
;
5729 isl_pw_multi_aff
*pma
;
5731 pma
= isl_pw_multi_aff_from_map(map
);
5732 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5734 return *upma
? isl_stat_ok
: isl_stat_error
;
5737 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5740 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5741 __isl_take isl_aff
*aff
)
5744 isl_pw_multi_aff
*pma
;
5746 ma
= isl_multi_aff_from_aff(aff
);
5747 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5748 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5751 /* Try and create an isl_union_pw_multi_aff that is equivalent
5752 * to the given isl_union_map.
5753 * The isl_union_map is required to be single-valued in each space.
5754 * Otherwise, an error is produced.
5756 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5757 __isl_take isl_union_map
*umap
)
5760 isl_union_pw_multi_aff
*upma
;
5762 space
= isl_union_map_get_space(umap
);
5763 upma
= isl_union_pw_multi_aff_empty(space
);
5764 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5765 upma
= isl_union_pw_multi_aff_free(upma
);
5766 isl_union_map_free(umap
);
5771 /* This function performs the same operation as
5772 * isl_union_pw_multi_aff_from_union_map,
5773 * but is considered as a function on an isl_union_map when exported.
5775 __isl_give isl_union_pw_multi_aff
*isl_union_map_as_union_pw_multi_aff(
5776 __isl_take isl_union_map
*umap
)
5778 return isl_union_pw_multi_aff_from_union_map(umap
);
5781 /* Try and create an isl_union_pw_multi_aff that is equivalent
5782 * to the given isl_union_set.
5783 * The isl_union_set is required to be a singleton in each space.
5784 * Otherwise, an error is produced.
5786 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5787 __isl_take isl_union_set
*uset
)
5789 return isl_union_pw_multi_aff_from_union_map(uset
);
5792 /* Return the piecewise affine expression "set ? 1 : 0".
5794 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5797 isl_space
*space
= isl_set_get_space(set
);
5798 isl_local_space
*ls
= isl_local_space_from_space(space
);
5799 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5800 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5802 one
= isl_aff_add_constant_si(one
, 1);
5803 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5804 set
= isl_set_complement(set
);
5805 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5810 /* Plug in "subs" for dimension "type", "pos" of "aff".
5812 * Let i be the dimension to replace and let "subs" be of the form
5816 * and "aff" of the form
5822 * (a f + d g')/(m d)
5824 * where g' is the result of plugging in "subs" in each of the integer
5827 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5828 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5834 aff
= isl_aff_cow(aff
);
5836 return isl_aff_free(aff
);
5838 ctx
= isl_aff_get_ctx(aff
);
5839 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5840 isl_die(ctx
, isl_error_invalid
,
5841 "spaces don't match", return isl_aff_free(aff
));
5842 n_div
= isl_aff_domain_dim(subs
, isl_dim_div
);
5844 return isl_aff_free(aff
);
5846 isl_die(ctx
, isl_error_unsupported
,
5847 "cannot handle divs yet", return isl_aff_free(aff
));
5849 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5851 return isl_aff_free(aff
);
5853 aff
->v
= isl_vec_cow(aff
->v
);
5855 return isl_aff_free(aff
);
5857 pos
+= isl_local_space_offset(aff
->ls
, type
);
5860 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5861 aff
->v
->size
, subs
->v
->size
, v
);
5867 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5868 * expressions in "maff".
5870 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5871 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5872 __isl_keep isl_aff
*subs
)
5877 n
= isl_multi_aff_size(maff
);
5879 return isl_multi_aff_free(maff
);
5881 if (type
== isl_dim_in
)
5884 for (i
= 0; i
< n
; ++i
) {
5887 aff
= isl_multi_aff_take_at(maff
, i
);
5888 aff
= isl_aff_substitute(aff
, type
, pos
, subs
);
5889 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
5895 /* Plug in "subs" for input dimension "pos" of "pma".
5897 * pma is of the form
5901 * while subs is of the form
5903 * v' = B_j(v) -> S_j
5905 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5906 * has a contribution in the result, in particular
5908 * C_ij(S_j) -> M_i(S_j)
5910 * Note that plugging in S_j in C_ij may also result in an empty set
5911 * and this contribution should simply be discarded.
5913 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5914 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5915 __isl_keep isl_pw_aff
*subs
)
5918 isl_pw_multi_aff
*res
;
5921 return isl_pw_multi_aff_free(pma
);
5923 n
= pma
->n
* subs
->n
;
5924 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5926 for (i
= 0; i
< pma
->n
; ++i
) {
5927 for (j
= 0; j
< subs
->n
; ++j
) {
5929 isl_multi_aff
*res_ij
;
5932 common
= isl_set_intersect(
5933 isl_set_copy(pma
->p
[i
].set
),
5934 isl_set_copy(subs
->p
[j
].set
));
5935 common
= isl_set_substitute(common
,
5936 pos
, subs
->p
[j
].aff
);
5937 empty
= isl_set_plain_is_empty(common
);
5938 if (empty
< 0 || empty
) {
5939 isl_set_free(common
);
5945 res_ij
= isl_multi_aff_substitute(
5946 isl_multi_aff_copy(pma
->p
[i
].maff
),
5947 isl_dim_in
, pos
, subs
->p
[j
].aff
);
5949 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5953 isl_pw_multi_aff_free(pma
);
5956 isl_pw_multi_aff_free(pma
);
5957 isl_pw_multi_aff_free(res
);
5961 /* Compute the preimage of a range of dimensions in the affine expression "src"
5962 * under "ma" and put the result in "dst". The number of dimensions in "src"
5963 * that precede the range is given by "n_before". The number of dimensions
5964 * in the range is given by the number of output dimensions of "ma".
5965 * The number of dimensions that follow the range is given by "n_after".
5966 * If "has_denom" is set (to one),
5967 * then "src" and "dst" have an extra initial denominator.
5968 * "n_div_ma" is the number of existentials in "ma"
5969 * "n_div_bset" is the number of existentials in "src"
5970 * The resulting "dst" (which is assumed to have been allocated by
5971 * the caller) contains coefficients for both sets of existentials,
5972 * first those in "ma" and then those in "src".
5973 * f, c1, c2 and g are temporary objects that have been initialized
5976 * Let src represent the expression
5978 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5980 * and let ma represent the expressions
5982 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5984 * We start out with the following expression for dst:
5986 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5988 * with the multiplication factor f initially equal to 1
5989 * and f \sum_i b_i v_i kept separately.
5990 * For each x_i that we substitute, we multiply the numerator
5991 * (and denominator) of dst by c_1 = m_i and add the numerator
5992 * of the x_i expression multiplied by c_2 = f b_i,
5993 * after removing the common factors of c_1 and c_2.
5994 * The multiplication factor f also needs to be multiplied by c_1
5995 * for the next x_j, j > i.
5997 isl_stat
isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5998 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5999 int n_div_ma
, int n_div_bmap
,
6000 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
6003 isl_size n_param
, n_in
, n_out
;
6006 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
6007 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
6008 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
6009 if (n_param
< 0 || n_in
< 0 || n_out
< 0)
6010 return isl_stat_error
;
6012 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
6013 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
6014 isl_seq_clr(dst
+ o_dst
, n_in
);
6017 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
6020 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
6022 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
6024 isl_int_set_si(f
, 1);
6026 for (i
= 0; i
< n_out
; ++i
) {
6027 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
6029 if (isl_int_is_zero(src
[offset
]))
6031 isl_int_set(c1
, ma
->u
.p
[i
]->v
->el
[0]);
6032 isl_int_mul(c2
, f
, src
[offset
]);
6033 isl_int_gcd(g
, c1
, c2
);
6034 isl_int_divexact(c1
, c1
, g
);
6035 isl_int_divexact(c2
, c2
, g
);
6037 isl_int_mul(f
, f
, c1
);
6040 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6041 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, 1 + n_param
);
6042 o_dst
+= 1 + n_param
;
6043 o_src
+= 1 + n_param
;
6044 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
6046 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6047 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_in
);
6050 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
6052 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
6053 c2
, ma
->u
.p
[i
]->v
->el
+ o_src
, n_div_ma
);
6056 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
6058 isl_int_mul(dst
[0], dst
[0], c1
);
6064 /* Compute the pullback of "aff" by the function represented by "ma".
6065 * In other words, plug in "ma" in "aff". The result is an affine expression
6066 * defined over the domain space of "ma".
6068 * If "aff" is represented by
6070 * (a(p) + b x + c(divs))/d
6072 * and ma is represented by
6074 * x = D(p) + F(y) + G(divs')
6076 * then the result is
6078 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
6080 * The divs in the local space of the input are similarly adjusted
6081 * through a call to isl_local_space_preimage_multi_aff.
6083 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
6084 __isl_take isl_multi_aff
*ma
)
6086 isl_aff
*res
= NULL
;
6087 isl_local_space
*ls
;
6088 isl_size n_div_aff
, n_div_ma
;
6089 isl_int f
, c1
, c2
, g
;
6091 ma
= isl_multi_aff_align_divs(ma
);
6095 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
6096 n_div_ma
= ma
->n
? isl_aff_dim(ma
->u
.p
[0], isl_dim_div
) : 0;
6097 if (n_div_aff
< 0 || n_div_ma
< 0)
6100 ls
= isl_aff_get_domain_local_space(aff
);
6101 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
6102 res
= isl_aff_alloc(ls
);
6111 if (isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0,
6112 n_div_ma
, n_div_aff
, f
, c1
, c2
, g
, 1) < 0)
6113 res
= isl_aff_free(res
);
6121 isl_multi_aff_free(ma
);
6122 res
= isl_aff_normalize(res
);
6126 isl_multi_aff_free(ma
);
6131 /* Compute the pullback of "aff1" by the function represented by "aff2".
6132 * In other words, plug in "aff2" in "aff1". The result is an affine expression
6133 * defined over the domain space of "aff1".
6135 * The domain of "aff1" should match the range of "aff2", which means
6136 * that it should be single-dimensional.
6138 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
6139 __isl_take isl_aff
*aff2
)
6143 ma
= isl_multi_aff_from_aff(aff2
);
6144 return isl_aff_pullback_multi_aff(aff1
, ma
);
6147 /* Compute the pullback of "ma1" by the function represented by "ma2".
6148 * In other words, plug in "ma2" in "ma1".
6150 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
6151 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
6155 isl_space
*space
= NULL
;
6157 isl_multi_aff_align_params_bin(&ma1
, &ma2
);
6158 ma2
= isl_multi_aff_align_divs(ma2
);
6159 n
= isl_multi_aff_size(ma1
);
6163 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
6164 isl_multi_aff_get_space(ma1
));
6166 for (i
= 0; i
< n
; ++i
) {
6169 aff
= isl_multi_aff_take_at(ma1
, i
);
6170 aff
= isl_aff_pullback_multi_aff(aff
, isl_multi_aff_copy(ma2
));
6171 ma1
= isl_multi_aff_restore_at(ma1
, i
, aff
);
6174 ma1
= isl_multi_aff_reset_space(ma1
, space
);
6175 isl_multi_aff_free(ma2
);
6178 isl_space_free(space
);
6179 isl_multi_aff_free(ma2
);
6180 isl_multi_aff_free(ma1
);
6184 /* Extend the local space of "dst" to include the divs
6185 * in the local space of "src".
6187 * If "src" does not have any divs or if the local spaces of "dst" and
6188 * "src" are the same, then no extension is required.
6190 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
6191 __isl_keep isl_aff
*src
)
6194 isl_size src_n_div
, dst_n_div
;
6201 return isl_aff_free(dst
);
6203 ctx
= isl_aff_get_ctx(src
);
6204 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
6206 return isl_aff_free(dst
);
6208 isl_die(ctx
, isl_error_invalid
,
6209 "spaces don't match", goto error
);
6211 src_n_div
= isl_aff_domain_dim(src
, isl_dim_div
);
6212 dst_n_div
= isl_aff_domain_dim(dst
, isl_dim_div
);
6215 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
6216 if (equal
< 0 || src_n_div
< 0 || dst_n_div
< 0)
6217 return isl_aff_free(dst
);
6221 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
6222 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
6223 if (!exp1
|| (dst_n_div
&& !exp2
))
6226 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
6227 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
6235 return isl_aff_free(dst
);
6238 /* Adjust the local spaces of the affine expressions in "maff"
6239 * such that they all have the save divs.
6241 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
6242 __isl_take isl_multi_aff
*maff
)
6248 n
= isl_multi_aff_size(maff
);
6250 return isl_multi_aff_free(maff
);
6254 aff_0
= isl_multi_aff_take_at(maff
, 0);
6255 for (i
= 1; i
< n
; ++i
) {
6258 aff_i
= isl_multi_aff_peek_at(maff
, i
);
6259 aff_0
= isl_aff_align_divs(aff_0
, aff_i
);
6261 maff
= isl_multi_aff_restore_at(maff
, 0, aff_0
);
6263 aff_0
= isl_multi_aff_peek_at(maff
, 0);
6264 for (i
= 1; i
< n
; ++i
) {
6267 aff_i
= isl_multi_aff_take_at(maff
, i
);
6268 aff_i
= isl_aff_align_divs(aff_i
, aff_0
);
6269 maff
= isl_multi_aff_restore_at(maff
, i
, aff_i
);
6275 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
6277 aff
= isl_aff_cow(aff
);
6281 aff
->ls
= isl_local_space_lift(aff
->ls
);
6283 return isl_aff_free(aff
);
6288 /* Lift "maff" to a space with extra dimensions such that the result
6289 * has no more existentially quantified variables.
6290 * If "ls" is not NULL, then *ls is assigned the local space that lies
6291 * at the basis of the lifting applied to "maff".
6293 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
6294 __isl_give isl_local_space
**ls
)
6304 n
= isl_multi_aff_size(maff
);
6306 return isl_multi_aff_free(maff
);
6310 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
6311 *ls
= isl_local_space_from_space(space
);
6313 return isl_multi_aff_free(maff
);
6318 maff
= isl_multi_aff_align_divs(maff
);
6320 aff
= isl_multi_aff_peek_at(maff
, 0);
6321 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6323 return isl_multi_aff_free(maff
);
6324 space
= isl_multi_aff_get_space(maff
);
6325 space
= isl_space_lift(isl_space_domain(space
), n_div
);
6326 space
= isl_space_extend_domain_with_range(space
,
6327 isl_multi_aff_get_space(maff
));
6328 maff
= isl_multi_aff_restore_space(maff
, space
);
6331 aff
= isl_multi_aff_peek_at(maff
, 0);
6332 *ls
= isl_aff_get_domain_local_space(aff
);
6334 return isl_multi_aff_free(maff
);
6337 for (i
= 0; i
< n
; ++i
) {
6338 aff
= isl_multi_aff_take_at(maff
, i
);
6339 aff
= isl_aff_lift(aff
);
6340 maff
= isl_multi_aff_restore_at(maff
, i
, aff
);
6347 #define TYPE isl_pw_multi_aff
6349 #include "check_type_range_templ.c"
6351 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6353 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_at(
6354 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6361 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6364 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6368 space
= isl_pw_multi_aff_get_space(pma
);
6369 space
= isl_space_drop_dims(space
, isl_dim_out
,
6370 pos
+ 1, n_out
- pos
- 1);
6371 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
6373 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
6374 for (i
= 0; i
< pma
->n
; ++i
) {
6376 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
6377 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
6383 /* This is an alternative name for the function above.
6385 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
6386 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
6388 return isl_pw_multi_aff_get_at(pma
, pos
);
6391 /* Return an isl_pw_multi_aff with the given "set" as domain and
6392 * an unnamed zero-dimensional range.
6394 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
6395 __isl_take isl_set
*set
)
6400 space
= isl_set_get_space(set
);
6401 space
= isl_space_from_domain(space
);
6402 ma
= isl_multi_aff_zero(space
);
6403 return isl_pw_multi_aff_alloc(set
, ma
);
6406 /* Add an isl_pw_multi_aff with the given "set" as domain and
6407 * an unnamed zero-dimensional range to *user.
6409 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
6412 isl_union_pw_multi_aff
**upma
= user
;
6413 isl_pw_multi_aff
*pma
;
6415 pma
= isl_pw_multi_aff_from_domain(set
);
6416 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
6421 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6422 * an unnamed zero-dimensional range.
6424 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
6425 __isl_take isl_union_set
*uset
)
6428 isl_union_pw_multi_aff
*upma
;
6433 space
= isl_union_set_get_space(uset
);
6434 upma
= isl_union_pw_multi_aff_empty(space
);
6436 if (isl_union_set_foreach_set(uset
,
6437 &add_pw_multi_aff_from_domain
, &upma
) < 0)
6440 isl_union_set_free(uset
);
6443 isl_union_set_free(uset
);
6444 isl_union_pw_multi_aff_free(upma
);
6448 /* Local data for bin_entry and the callback "fn".
6450 struct isl_union_pw_multi_aff_bin_data
{
6451 isl_union_pw_multi_aff
*upma2
;
6452 isl_union_pw_multi_aff
*res
;
6453 isl_pw_multi_aff
*pma
;
6454 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
6457 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6458 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6460 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
6462 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6466 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
6468 isl_pw_multi_aff_free(pma
);
6473 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6474 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6475 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6476 * as *entry. The callback should adjust data->res if desired.
6478 static __isl_give isl_union_pw_multi_aff
*bin_op(
6479 __isl_take isl_union_pw_multi_aff
*upma1
,
6480 __isl_take isl_union_pw_multi_aff
*upma2
,
6481 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
6484 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
6486 space
= isl_union_pw_multi_aff_get_space(upma2
);
6487 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
6488 space
= isl_union_pw_multi_aff_get_space(upma1
);
6489 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
6491 if (!upma1
|| !upma2
)
6495 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
6496 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
6497 &bin_entry
, &data
) < 0)
6500 isl_union_pw_multi_aff_free(upma1
);
6501 isl_union_pw_multi_aff_free(upma2
);
6504 isl_union_pw_multi_aff_free(upma1
);
6505 isl_union_pw_multi_aff_free(upma2
);
6506 isl_union_pw_multi_aff_free(data
.res
);
6510 /* Given two isl_pw_multi_affs A -> B and C -> D,
6511 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6513 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
6514 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6518 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6519 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6520 isl_pw_multi_aff_get_space(pma2
));
6521 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6522 &isl_multi_aff_range_product
);
6525 /* Given two isl_pw_multi_affs A -> B and C -> D,
6526 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6528 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
6529 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
6533 isl_pw_multi_aff_align_params_bin(&pma1
, &pma2
);
6534 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
6535 isl_pw_multi_aff_get_space(pma2
));
6536 space
= isl_space_flatten_range(space
);
6537 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
6538 &isl_multi_aff_flat_range_product
);
6541 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6542 * to compute some form of range product and add the result to data->res.
6544 static isl_stat
gen_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6545 __isl_give isl_pw_multi_aff
*(*range_product
)(
6546 __isl_take isl_pw_multi_aff
*pma1
,
6547 __isl_take isl_pw_multi_aff
*pma2
),
6550 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6552 isl_space
*space1
, *space2
;
6554 space1
= isl_pw_multi_aff_peek_space(data
->pma
);
6555 space2
= isl_pw_multi_aff_peek_space(pma2
);
6556 match
= isl_space_tuple_is_equal(space1
, isl_dim_in
,
6557 space2
, isl_dim_in
);
6558 if (match
< 0 || !match
) {
6559 isl_pw_multi_aff_free(pma2
);
6560 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
6563 pma2
= range_product(isl_pw_multi_aff_copy(data
->pma
), pma2
);
6565 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6570 /* If data->pma and "pma2" have the same domain space, then compute
6571 * their flat range product and add the result to data->res.
6573 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6576 return gen_range_product_entry(pma2
,
6577 &isl_pw_multi_aff_flat_range_product
, user
);
6580 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6581 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6583 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
6584 __isl_take isl_union_pw_multi_aff
*upma1
,
6585 __isl_take isl_union_pw_multi_aff
*upma2
)
6587 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
6590 /* If data->pma and "pma2" have the same domain space, then compute
6591 * their range product and add the result to data->res.
6593 static isl_stat
range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
6596 return gen_range_product_entry(pma2
,
6597 &isl_pw_multi_aff_range_product
, user
);
6600 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6601 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6603 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_product(
6604 __isl_take isl_union_pw_multi_aff
*upma1
,
6605 __isl_take isl_union_pw_multi_aff
*upma2
)
6607 return bin_op(upma1
, upma2
, &range_product_entry
);
6610 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6611 * The parameters are assumed to have been aligned.
6613 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6614 * except that it works on two different isl_pw_* types.
6616 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
6617 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6618 __isl_take isl_pw_aff
*pa
)
6621 isl_pw_multi_aff
*res
= NULL
;
6626 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
6627 pa
->dim
, isl_dim_in
))
6628 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6629 "domains don't match", goto error
);
6630 if (isl_pw_multi_aff_check_range(pma
, isl_dim_out
, pos
, 1) < 0)
6634 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
6636 for (i
= 0; i
< pma
->n
; ++i
) {
6637 for (j
= 0; j
< pa
->n
; ++j
) {
6639 isl_multi_aff
*res_ij
;
6642 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6643 isl_set_copy(pa
->p
[j
].set
));
6644 empty
= isl_set_plain_is_empty(common
);
6645 if (empty
< 0 || empty
) {
6646 isl_set_free(common
);
6652 res_ij
= isl_multi_aff_set_aff(
6653 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6654 isl_aff_copy(pa
->p
[j
].aff
));
6655 res_ij
= isl_multi_aff_gist(res_ij
,
6656 isl_set_copy(common
));
6658 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6662 isl_pw_multi_aff_free(pma
);
6663 isl_pw_aff_free(pa
);
6666 isl_pw_multi_aff_free(pma
);
6667 isl_pw_aff_free(pa
);
6668 return isl_pw_multi_aff_free(res
);
6671 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6673 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6674 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6675 __isl_take isl_pw_aff
*pa
)
6677 isl_bool equal_params
;
6681 equal_params
= isl_space_has_equal_params(pma
->dim
, pa
->dim
);
6682 if (equal_params
< 0)
6685 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6686 if (isl_pw_multi_aff_check_named_params(pma
) < 0 ||
6687 isl_pw_aff_check_named_params(pa
) < 0)
6689 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6690 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6691 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6693 isl_pw_multi_aff_free(pma
);
6694 isl_pw_aff_free(pa
);
6698 /* Do the parameters of "pa" match those of "space"?
6700 isl_bool
isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6701 __isl_keep isl_space
*space
)
6703 isl_space
*pa_space
;
6707 return isl_bool_error
;
6709 pa_space
= isl_pw_aff_get_space(pa
);
6711 match
= isl_space_has_equal_params(space
, pa_space
);
6713 isl_space_free(pa_space
);
6717 /* Check that the domain space of "pa" matches "space".
6719 isl_stat
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6720 __isl_keep isl_space
*space
)
6722 isl_space
*pa_space
;
6726 return isl_stat_error
;
6728 pa_space
= isl_pw_aff_get_space(pa
);
6730 match
= isl_space_has_equal_params(space
, pa_space
);
6734 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6735 "parameters don't match", goto error
);
6736 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6737 pa_space
, isl_dim_in
);
6741 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6742 "domains don't match", goto error
);
6743 isl_space_free(pa_space
);
6746 isl_space_free(pa_space
);
6747 return isl_stat_error
;
6755 #include <isl_multi_explicit_domain.c>
6756 #include <isl_multi_pw_aff_explicit_domain.c>
6757 #include <isl_multi_templ.c>
6758 #include <isl_multi_un_op_templ.c>
6759 #include <isl_multi_bin_val_templ.c>
6760 #include <isl_multi_add_constant_templ.c>
6761 #include <isl_multi_align_set.c>
6762 #include <isl_multi_apply_set_explicit_domain_templ.c>
6763 #include <isl_multi_arith_templ.c>
6764 #include <isl_multi_bind_templ.c>
6765 #include <isl_multi_bind_domain_templ.c>
6766 #include <isl_multi_coalesce.c>
6767 #include <isl_multi_domain_templ.c>
6768 #include <isl_multi_domain_reverse_templ.c>
6769 #include <isl_multi_dim_id_templ.c>
6770 #include <isl_multi_dims.c>
6771 #include <isl_multi_from_base_templ.c>
6772 #include <isl_multi_check_domain_templ.c>
6773 #include <isl_multi_gist.c>
6774 #include <isl_multi_hash.c>
6775 #include <isl_multi_identity_templ.c>
6776 #include <isl_multi_insert_domain_templ.c>
6777 #include <isl_multi_intersect.c>
6778 #include <isl_multi_min_max_templ.c>
6779 #include <isl_multi_move_dims_templ.c>
6780 #include <isl_multi_nan_templ.c>
6781 #include <isl_multi_param_templ.c>
6782 #include <isl_multi_product_templ.c>
6783 #include <isl_multi_splice_templ.c>
6784 #include <isl_multi_tuple_id_templ.c>
6785 #include <isl_multi_union_add_templ.c>
6786 #include <isl_multi_zero_templ.c>
6787 #include <isl_multi_unbind_params_templ.c>
6789 /* Is every element of "mpa" defined over a single universe domain?
6791 isl_bool
isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff
*mpa
)
6793 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_isa_aff
);
6796 /* Given that every element of "mpa" is defined over a single universe domain,
6797 * return the corresponding base expressions.
6799 __isl_give isl_multi_aff
*isl_multi_pw_aff_as_multi_aff(
6800 __isl_take isl_multi_pw_aff
*mpa
)
6806 n
= isl_multi_pw_aff_size(mpa
);
6808 mpa
= isl_multi_pw_aff_free(mpa
);
6809 ma
= isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa
));
6810 for (i
= 0; i
< n
; ++i
) {
6813 aff
= isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa
, i
));
6814 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6816 isl_multi_pw_aff_free(mpa
);
6820 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6821 * with this explicit domain.
6823 __isl_give isl_map
*isl_map_intersect_multi_pw_aff_explicit_domain(
6824 __isl_take isl_map
*map
, __isl_keep isl_multi_pw_aff
*mpa
)
6828 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6831 dom
= isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa
));
6832 map
= isl_map_intersect_domain(map
, dom
);
6837 /* Are all elements of "mpa" piecewise constants?
6839 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
6841 return isl_multi_pw_aff_every(mpa
, &isl_pw_aff_is_cst
);
6844 /* Does "mpa" have a non-trivial explicit domain?
6846 * The explicit domain, if present, is trivial if it represents
6847 * an (obviously) universe set.
6849 isl_bool
isl_multi_pw_aff_has_non_trivial_domain(
6850 __isl_keep isl_multi_pw_aff
*mpa
)
6853 return isl_bool_error
;
6854 if (!isl_multi_pw_aff_has_explicit_domain(mpa
))
6855 return isl_bool_false
;
6856 return isl_bool_not(isl_set_plain_is_universe(mpa
->u
.dom
));
6862 #include "isl_opt_mpa_templ.c"
6864 /* Compute the minima of the set dimensions as a function of the
6865 * parameters, but independently of the other set dimensions.
6867 __isl_give isl_multi_pw_aff
*isl_set_min_multi_pw_aff(__isl_take isl_set
*set
)
6869 return set_opt_mpa(set
, &isl_set_dim_min
);
6872 /* Compute the maxima of the set dimensions as a function of the
6873 * parameters, but independently of the other set dimensions.
6875 __isl_give isl_multi_pw_aff
*isl_set_max_multi_pw_aff(__isl_take isl_set
*set
)
6877 return set_opt_mpa(set
, &isl_set_dim_max
);
6883 #include "isl_opt_mpa_templ.c"
6885 /* Compute the minima of the output dimensions as a function of the
6886 * parameters and input dimensions, but independently of
6887 * the other output dimensions.
6889 __isl_give isl_multi_pw_aff
*isl_map_min_multi_pw_aff(__isl_take isl_map
*map
)
6891 return map_opt_mpa(map
, &isl_map_dim_min
);
6894 /* Compute the maxima of the output dimensions as a function of the
6895 * parameters and input dimensions, but independently of
6896 * the other output dimensions.
6898 __isl_give isl_multi_pw_aff
*isl_map_max_multi_pw_aff(__isl_take isl_map
*map
)
6900 return map_opt_mpa(map
, &isl_map_dim_max
);
6904 #define TYPE isl_pw_multi_aff
6905 #include "isl_type_check_match_range_multi_val.c"
6907 /* Apply "fn" to the base expressions of "pma" and "mv".
6909 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_op_multi_val(
6910 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
,
6911 __isl_give isl_multi_aff
*(*fn
)(__isl_take isl_multi_aff
*ma
,
6912 __isl_take isl_multi_val
*mv
))
6917 if (isl_pw_multi_aff_check_match_range_multi_val(pma
, mv
) < 0)
6920 n
= isl_pw_multi_aff_n_piece(pma
);
6924 for (i
= 0; i
< n
; ++i
) {
6927 ma
= isl_pw_multi_aff_take_base_at(pma
, i
);
6928 ma
= fn(ma
, isl_multi_val_copy(mv
));
6929 pma
= isl_pw_multi_aff_restore_base_at(pma
, i
, ma
);
6932 isl_multi_val_free(mv
);
6935 isl_multi_val_free(mv
);
6936 isl_pw_multi_aff_free(pma
);
6940 /* Scale the elements of "pma" by the corresponding elements of "mv".
6942 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6943 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6945 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6946 &isl_multi_aff_scale_multi_val
);
6949 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6951 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_down_multi_val(
6952 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6954 return isl_pw_multi_aff_op_multi_val(pma
, mv
,
6955 &isl_multi_aff_scale_down_multi_val
);
6958 /* This function is called for each entry of an isl_union_pw_multi_aff.
6959 * If the space of the entry matches that of data->mv,
6960 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6961 * Otherwise, return an empty isl_pw_multi_aff.
6963 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6964 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6967 isl_multi_val
*mv
= user
;
6969 equal
= isl_pw_multi_aff_match_range_multi_val(pma
, mv
);
6971 return isl_pw_multi_aff_free(pma
);
6973 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6974 isl_pw_multi_aff_free(pma
);
6975 return isl_pw_multi_aff_empty(space
);
6978 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6981 /* Scale the elements of "upma" by the corresponding elements of "mv",
6982 * for those entries that match the space of "mv".
6984 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6985 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6987 struct isl_union_pw_multi_aff_transform_control control
= {
6988 .fn
= &union_pw_multi_aff_scale_multi_val_entry
,
6992 upma
= isl_union_pw_multi_aff_align_params(upma
,
6993 isl_multi_val_get_space(mv
));
6994 mv
= isl_multi_val_align_params(mv
,
6995 isl_union_pw_multi_aff_get_space(upma
));
6999 return isl_union_pw_multi_aff_transform(upma
, &control
);
7001 isl_multi_val_free(mv
);
7004 isl_multi_val_free(mv
);
7005 isl_union_pw_multi_aff_free(upma
);
7009 /* Construct and return a piecewise multi affine expression
7010 * in the given space with value zero in each of the output dimensions and
7011 * a universe domain.
7013 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
7015 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
7018 /* Construct and return a piecewise multi affine expression
7019 * that is equal to the given piecewise affine expression.
7021 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
7022 __isl_take isl_pw_aff
*pa
)
7026 isl_pw_multi_aff
*pma
;
7031 space
= isl_pw_aff_get_space(pa
);
7032 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
7034 for (i
= 0; i
< pa
->n
; ++i
) {
7038 set
= isl_set_copy(pa
->p
[i
].set
);
7039 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
7040 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
7043 isl_pw_aff_free(pa
);
7047 /* Construct and return a piecewise multi affine expression
7048 * that is equal to the given multi piecewise affine expression
7049 * on the shared domain of the piecewise affine expressions,
7050 * in the special case of a 0D multi piecewise affine expression.
7052 * Create a piecewise multi affine expression with the explicit domain of
7053 * the 0D multi piecewise affine expression as domain.
7055 static __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff_0D(
7056 __isl_take isl_multi_pw_aff
*mpa
)
7062 space
= isl_multi_pw_aff_get_space(mpa
);
7063 dom
= isl_multi_pw_aff_get_explicit_domain(mpa
);
7064 isl_multi_pw_aff_free(mpa
);
7066 ma
= isl_multi_aff_zero(space
);
7067 return isl_pw_multi_aff_alloc(dom
, ma
);
7070 /* Construct and return a piecewise multi affine expression
7071 * that is equal to the given multi piecewise affine expression
7072 * on the shared domain of the piecewise affine expressions.
7074 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
7075 __isl_take isl_multi_pw_aff
*mpa
)
7080 isl_pw_multi_aff
*pma
;
7086 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa
);
7088 space
= isl_multi_pw_aff_get_space(mpa
);
7089 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
7090 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7092 for (i
= 1; i
< mpa
->n
; ++i
) {
7093 isl_pw_multi_aff
*pma_i
;
7095 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7096 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
7097 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
7100 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
7102 isl_multi_pw_aff_free(mpa
);
7106 /* Convenience function that constructs an isl_multi_pw_aff
7107 * directly from an isl_aff.
7109 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_aff(__isl_take isl_aff
*aff
)
7111 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
7114 /* Construct and return a multi piecewise affine expression
7115 * that is equal to the given multi affine expression.
7117 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
7118 __isl_take isl_multi_aff
*ma
)
7122 isl_multi_pw_aff
*mpa
;
7124 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
7126 ma
= isl_multi_aff_free(ma
);
7130 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
7132 for (i
= 0; i
< n
; ++i
) {
7135 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
7136 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7139 isl_multi_aff_free(ma
);
7143 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
7144 * but is considered as a function on an isl_multi_aff when exported.
7146 __isl_give isl_multi_pw_aff
*isl_multi_aff_to_multi_pw_aff(
7147 __isl_take isl_multi_aff
*ma
)
7149 return isl_multi_pw_aff_from_multi_aff(ma
);
7152 /* Construct and return a multi piecewise affine expression
7153 * that is equal to the given piecewise multi affine expression.
7155 * If the resulting multi piecewise affine expression has
7156 * an explicit domain, then assign it the domain of the input.
7157 * In other cases, the domain is stored in the individual elements.
7159 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
7160 __isl_take isl_pw_multi_aff
*pma
)
7165 isl_multi_pw_aff
*mpa
;
7167 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7169 pma
= isl_pw_multi_aff_free(pma
);
7170 space
= isl_pw_multi_aff_get_space(pma
);
7171 mpa
= isl_multi_pw_aff_alloc(space
);
7173 for (i
= 0; i
< n
; ++i
) {
7176 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
7177 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
7179 if (isl_multi_pw_aff_has_explicit_domain(mpa
)) {
7182 dom
= isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma
));
7183 mpa
= isl_multi_pw_aff_intersect_domain(mpa
, dom
);
7186 isl_pw_multi_aff_free(pma
);
7190 /* This function performs the same operation as
7191 * isl_multi_pw_aff_from_pw_multi_aff,
7192 * but is considered as a function on an isl_pw_multi_aff when exported.
7194 __isl_give isl_multi_pw_aff
*isl_pw_multi_aff_to_multi_pw_aff(
7195 __isl_take isl_pw_multi_aff
*pma
)
7197 return isl_multi_pw_aff_from_pw_multi_aff(pma
);
7200 /* Do "pa1" and "pa2" represent the same function?
7202 * We first check if they are obviously equal.
7203 * If not, we convert them to maps and check if those are equal.
7205 * If "pa1" or "pa2" contain any NaNs, then they are considered
7206 * not to be the same. A NaN is not equal to anything, not even
7209 isl_bool
isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
,
7210 __isl_keep isl_pw_aff
*pa2
)
7214 isl_map
*map1
, *map2
;
7217 return isl_bool_error
;
7219 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
7220 if (equal
< 0 || equal
)
7222 has_nan
= either_involves_nan(pa1
, pa2
);
7224 return isl_bool_error
;
7226 return isl_bool_false
;
7228 map1
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1
));
7229 map2
= isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2
));
7230 equal
= isl_map_is_equal(map1
, map2
);
7237 /* Do "mpa1" and "mpa2" represent the same function?
7239 * Note that we cannot convert the entire isl_multi_pw_aff
7240 * to a map because the domains of the piecewise affine expressions
7241 * may not be the same.
7243 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
7244 __isl_keep isl_multi_pw_aff
*mpa2
)
7247 isl_bool equal
, equal_params
;
7250 return isl_bool_error
;
7252 equal_params
= isl_space_has_equal_params(mpa1
->space
, mpa2
->space
);
7253 if (equal_params
< 0)
7254 return isl_bool_error
;
7255 if (!equal_params
) {
7256 if (!isl_space_has_named_params(mpa1
->space
))
7257 return isl_bool_false
;
7258 if (!isl_space_has_named_params(mpa2
->space
))
7259 return isl_bool_false
;
7260 mpa1
= isl_multi_pw_aff_copy(mpa1
);
7261 mpa2
= isl_multi_pw_aff_copy(mpa2
);
7262 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7263 isl_multi_pw_aff_get_space(mpa2
));
7264 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7265 isl_multi_pw_aff_get_space(mpa1
));
7266 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
7267 isl_multi_pw_aff_free(mpa1
);
7268 isl_multi_pw_aff_free(mpa2
);
7272 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
7273 if (equal
< 0 || !equal
)
7276 for (i
= 0; i
< mpa1
->n
; ++i
) {
7277 equal
= isl_pw_aff_is_equal(mpa1
->u
.p
[i
], mpa2
->u
.p
[i
]);
7278 if (equal
< 0 || !equal
)
7282 return isl_bool_true
;
7285 /* Do "pma1" and "pma2" represent the same function?
7287 * First check if they are obviously equal.
7288 * If not, then convert them to maps and check if those are equal.
7290 * If "pa1" or "pa2" contain any NaNs, then they are considered
7291 * not to be the same. A NaN is not equal to anything, not even
7294 isl_bool
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff
*pma1
,
7295 __isl_keep isl_pw_multi_aff
*pma2
)
7299 isl_map
*map1
, *map2
;
7302 return isl_bool_error
;
7304 equal
= isl_pw_multi_aff_plain_is_equal(pma1
, pma2
);
7305 if (equal
< 0 || equal
)
7307 has_nan
= isl_pw_multi_aff_involves_nan(pma1
);
7308 if (has_nan
>= 0 && !has_nan
)
7309 has_nan
= isl_pw_multi_aff_involves_nan(pma2
);
7310 if (has_nan
< 0 || has_nan
)
7311 return isl_bool_not(has_nan
);
7313 map1
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1
));
7314 map2
= isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2
));
7315 equal
= isl_map_is_equal(map1
, map2
);
7323 #define BASE multi_aff
7325 #include "isl_multi_pw_aff_pullback_templ.c"
7328 #define BASE pw_multi_aff
7330 #include "isl_multi_pw_aff_pullback_templ.c"
7332 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7333 * with the domain of "aff". The domain of the result is the same
7335 * "mpa" and "aff" are assumed to have been aligned.
7337 * We first extract the parametric constant from "aff", defined
7338 * over the correct domain.
7339 * Then we add the appropriate combinations of the members of "mpa".
7340 * Finally, we add the integer divisions through recursive calls.
7342 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
7343 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7346 isl_size n_in
, n_div
, n_mpa_in
;
7352 n_in
= isl_aff_dim(aff
, isl_dim_in
);
7353 n_div
= isl_aff_dim(aff
, isl_dim_div
);
7354 n_mpa_in
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
7355 if (n_in
< 0 || n_div
< 0 || n_mpa_in
< 0)
7358 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
7359 tmp
= isl_aff_copy(aff
);
7360 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
7361 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
7362 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
, n_mpa_in
);
7363 tmp
= isl_aff_reset_domain_space(tmp
, space
);
7364 pa
= isl_pw_aff_from_aff(tmp
);
7366 for (i
= 0; i
< n_in
; ++i
) {
7369 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
7371 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
7372 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7373 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7374 pa
= isl_pw_aff_add(pa
, pa_i
);
7377 for (i
= 0; i
< n_div
; ++i
) {
7381 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
7383 div
= isl_aff_get_div(aff
, i
);
7384 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7385 isl_multi_pw_aff_copy(mpa
), div
);
7386 pa_i
= isl_pw_aff_floor(pa_i
);
7387 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
7388 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
7389 pa
= isl_pw_aff_add(pa
, pa_i
);
7392 isl_multi_pw_aff_free(mpa
);
7397 isl_multi_pw_aff_free(mpa
);
7402 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7403 * with the domain of "aff". The domain of the result is the same
7406 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
7407 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
7409 isl_bool equal_params
;
7413 equal_params
= isl_space_has_equal_params(aff
->ls
->dim
, mpa
->space
);
7414 if (equal_params
< 0)
7417 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7419 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
7420 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
7422 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
7425 isl_multi_pw_aff_free(mpa
);
7429 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7430 * with the domain of "pa". The domain of the result is the same
7432 * "mpa" and "pa" are assumed to have been aligned.
7434 * We consider each piece in turn. Note that the domains of the
7435 * pieces are assumed to be disjoint and they remain disjoint
7436 * after taking the preimage (over the same function).
7438 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
7439 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7448 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
7449 isl_pw_aff_get_space(pa
));
7450 res
= isl_pw_aff_empty(space
);
7452 for (i
= 0; i
< pa
->n
; ++i
) {
7456 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
7457 isl_multi_pw_aff_copy(mpa
),
7458 isl_aff_copy(pa
->p
[i
].aff
));
7459 domain
= isl_set_copy(pa
->p
[i
].set
);
7460 domain
= isl_set_preimage_multi_pw_aff(domain
,
7461 isl_multi_pw_aff_copy(mpa
));
7462 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
7463 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
7466 isl_pw_aff_free(pa
);
7467 isl_multi_pw_aff_free(mpa
);
7470 isl_pw_aff_free(pa
);
7471 isl_multi_pw_aff_free(mpa
);
7475 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7476 * with the domain of "pa". The domain of the result is the same
7479 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
7480 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
7482 isl_bool equal_params
;
7486 equal_params
= isl_space_has_equal_params(pa
->dim
, mpa
->space
);
7487 if (equal_params
< 0)
7490 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7492 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
7493 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
7495 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
7497 isl_pw_aff_free(pa
);
7498 isl_multi_pw_aff_free(mpa
);
7502 /* Compute the pullback of "pa" by the function represented by "mpa".
7503 * In other words, plug in "mpa" in "pa".
7505 * The pullback is computed by applying "pa" to "mpa".
7507 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
7508 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
7510 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
7514 #define BASE multi_pw_aff
7516 #include "isl_multi_pw_aff_pullback_templ.c"
7518 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7519 * of "mpa1" and "mpa2" live in the same space, construct map space
7520 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7521 * with this map space as extract argument.
7523 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
7524 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7525 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
7526 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
7529 isl_space
*space1
, *space2
;
7532 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
7533 isl_multi_pw_aff_get_space(mpa2
));
7534 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
7535 isl_multi_pw_aff_get_space(mpa1
));
7538 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
7539 mpa2
->space
, isl_dim_out
);
7543 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
7544 "range spaces don't match", goto error
);
7545 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
7546 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
7547 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
7549 res
= order(mpa1
, mpa2
, space1
);
7550 isl_multi_pw_aff_free(mpa1
);
7551 isl_multi_pw_aff_free(mpa2
);
7554 isl_multi_pw_aff_free(mpa1
);
7555 isl_multi_pw_aff_free(mpa2
);
7559 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7560 * where the function values are equal. "space" is the space of the result.
7561 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7563 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7564 * in the sequences are equal.
7566 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
7567 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7568 __isl_take isl_space
*space
)
7574 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7576 space
= isl_space_free(space
);
7577 res
= isl_map_universe(space
);
7579 for (i
= 0; i
< n
; ++i
) {
7580 isl_pw_aff
*pa1
, *pa2
;
7583 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7584 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7585 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7586 res
= isl_map_intersect(res
, map
);
7592 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7593 * where the function values are equal.
7595 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
7596 __isl_take isl_multi_pw_aff
*mpa2
)
7598 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7599 &isl_multi_pw_aff_eq_map_on_space
);
7602 /* Intersect "map" with the result of applying "order"
7603 * on two copies of "mpa".
7605 static __isl_give isl_map
*isl_map_order_at_multi_pw_aff(
7606 __isl_take isl_map
*map
, __isl_take isl_multi_pw_aff
*mpa
,
7607 __isl_give isl_map
*(*order
)(__isl_take isl_multi_pw_aff
*mpa1
,
7608 __isl_take isl_multi_pw_aff
*mpa2
))
7610 return isl_map_intersect(map
, order(mpa
, isl_multi_pw_aff_copy(mpa
)));
7613 /* Return the subset of "map" where the domain and the range
7614 * have equal "mpa" values.
7616 __isl_give isl_map
*isl_map_eq_at_multi_pw_aff(__isl_take isl_map
*map
,
7617 __isl_take isl_multi_pw_aff
*mpa
)
7619 return isl_map_order_at_multi_pw_aff(map
, mpa
,
7620 &isl_multi_pw_aff_eq_map
);
7623 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7624 * where the function values of "mpa1" lexicographically satisfies
7625 * "strict_base"/"base" compared to that of "mpa2".
7626 * "space" is the space of the result.
7627 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7629 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7630 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7631 * when compared to the i-th element of "mpa2" while all previous elements are
7633 * In particular, if i corresponds to the final elements
7634 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7635 * for other values of i.
7636 * If "base" is a strict order, then "base" and "strict_base" are the same.
7638 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
7639 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
7640 __isl_give isl_map
*(*strict_base
)(__isl_take isl_pw_aff
*pa1
,
7641 __isl_take isl_pw_aff
*pa2
),
7642 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
7643 __isl_take isl_pw_aff
*pa2
),
7644 __isl_take isl_space
*space
)
7648 isl_map
*res
, *rest
;
7650 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
7652 space
= isl_space_free(space
);
7653 res
= isl_map_empty(isl_space_copy(space
));
7654 rest
= isl_map_universe(space
);
7656 for (i
= 0; i
< n
; ++i
) {
7658 isl_pw_aff
*pa1
, *pa2
;
7663 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7664 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7665 map
= last
? base(pa1
, pa2
) : strict_base(pa1
, pa2
);
7666 map
= isl_map_intersect(map
, isl_map_copy(rest
));
7667 res
= isl_map_union(res
, map
);
7672 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
7673 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
7674 map
= isl_pw_aff_eq_map(pa1
, pa2
);
7675 rest
= isl_map_intersect(rest
, map
);
7685 #define STRICT_ORDER lt
7686 #include "isl_aff_lex_templ.c"
7691 #define STRICT_ORDER lt
7692 #include "isl_aff_lex_templ.c"
7697 #define STRICT_ORDER gt
7698 #include "isl_aff_lex_templ.c"
7703 #define STRICT_ORDER gt
7704 #include "isl_aff_lex_templ.c"
7706 /* Compare two isl_affs.
7708 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7709 * than "aff2" and 0 if they are equal.
7711 * The order is fairly arbitrary. We do consider expressions that only involve
7712 * earlier dimensions as "smaller".
7714 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7727 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7731 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7732 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7734 return last1
- last2
;
7736 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7739 /* Compare two isl_pw_affs.
7741 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7742 * than "pa2" and 0 if they are equal.
7744 * The order is fairly arbitrary. We do consider expressions that only involve
7745 * earlier dimensions as "smaller".
7747 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7748 __isl_keep isl_pw_aff
*pa2
)
7761 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7765 if (pa1
->n
!= pa2
->n
)
7766 return pa1
->n
- pa2
->n
;
7768 for (i
= 0; i
< pa1
->n
; ++i
) {
7769 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7772 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7780 /* Return a piecewise affine expression that is equal to "v" on "domain".
7782 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7783 __isl_take isl_val
*v
)
7786 isl_local_space
*ls
;
7789 space
= isl_set_get_space(domain
);
7790 ls
= isl_local_space_from_space(space
);
7791 aff
= isl_aff_val_on_domain(ls
, v
);
7793 return isl_pw_aff_alloc(domain
, aff
);
7796 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7797 * but is considered as a function on an isl_set when exported.
7799 __isl_give isl_pw_aff
*isl_set_pw_aff_on_domain_val(__isl_take isl_set
*domain
,
7800 __isl_take isl_val
*v
)
7802 return isl_pw_aff_val_on_domain(domain
, v
);
7805 /* Return a piecewise affine expression that is equal to the parameter
7806 * with identifier "id" on "domain".
7808 __isl_give isl_pw_aff
*isl_pw_aff_param_on_domain_id(
7809 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7814 space
= isl_set_get_space(domain
);
7815 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
7816 domain
= isl_set_align_params(domain
, isl_space_copy(space
));
7817 aff
= isl_aff_param_on_domain_space_id(space
, id
);
7819 return isl_pw_aff_alloc(domain
, aff
);
7822 /* This function performs the same operation as
7823 * isl_pw_aff_param_on_domain_id,
7824 * but is considered as a function on an isl_set when exported.
7826 __isl_give isl_pw_aff
*isl_set_param_pw_aff_on_domain_id(
7827 __isl_take isl_set
*domain
, __isl_take isl_id
*id
)
7829 return isl_pw_aff_param_on_domain_id(domain
, id
);
7832 /* Return a multi affine expression that is equal to "mv" on domain
7835 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_domain_space(
7836 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7841 isl_local_space
*ls
;
7844 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7845 if (!space
|| n
< 0)
7848 space2
= isl_multi_val_get_space(mv
);
7849 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7850 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7851 space
= isl_space_map_from_domain_and_range(space
, space2
);
7852 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7853 ls
= isl_local_space_from_space(isl_space_domain(space
));
7854 for (i
= 0; i
< n
; ++i
) {
7858 v
= isl_multi_val_get_val(mv
, i
);
7859 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7860 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7862 isl_local_space_free(ls
);
7864 isl_multi_val_free(mv
);
7867 isl_space_free(space
);
7868 isl_multi_val_free(mv
);
7872 /* This is an alternative name for the function above.
7874 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7875 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7877 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7880 /* This function performs the same operation as
7881 * isl_multi_aff_multi_val_on_domain_space,
7882 * but is considered as a function on an isl_space when exported.
7884 __isl_give isl_multi_aff
*isl_space_multi_aff_on_domain_multi_val(
7885 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7887 return isl_multi_aff_multi_val_on_domain_space(space
, mv
);
7890 /* Return a piecewise multi-affine expression
7891 * that is equal to "mv" on "domain".
7893 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7894 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7899 space
= isl_set_get_space(domain
);
7900 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7902 return isl_pw_multi_aff_alloc(domain
, ma
);
7905 /* This function performs the same operation as
7906 * isl_pw_multi_aff_multi_val_on_domain,
7907 * but is considered as a function on an isl_set when exported.
7909 __isl_give isl_pw_multi_aff
*isl_set_pw_multi_aff_on_domain_multi_val(
7910 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7912 return isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7915 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7916 * mv is the value that should be attained on each domain set
7917 * res collects the results
7919 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7921 isl_union_pw_multi_aff
*res
;
7924 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7925 * and add it to data->res.
7927 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7930 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7931 isl_pw_multi_aff
*pma
;
7934 mv
= isl_multi_val_copy(data
->mv
);
7935 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7936 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7938 return data
->res
? isl_stat_ok
: isl_stat_error
;
7941 /* Return a union piecewise multi-affine expression
7942 * that is equal to "mv" on "domain".
7944 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7945 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7947 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7950 space
= isl_union_set_get_space(domain
);
7951 data
.res
= isl_union_pw_multi_aff_empty(space
);
7953 if (isl_union_set_foreach_set(domain
,
7954 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7955 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7956 isl_union_set_free(domain
);
7957 isl_multi_val_free(mv
);
7961 /* Compute the pullback of data->pma by the function represented by "pma2",
7962 * provided the spaces match, and add the results to data->res.
7964 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7966 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7968 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7969 pma2
->dim
, isl_dim_out
)) {
7970 isl_pw_multi_aff_free(pma2
);
7974 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7975 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7977 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7979 return isl_stat_error
;
7984 /* Compute the pullback of "upma1" by the function represented by "upma2".
7986 __isl_give isl_union_pw_multi_aff
*
7987 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7988 __isl_take isl_union_pw_multi_aff
*upma1
,
7989 __isl_take isl_union_pw_multi_aff
*upma2
)
7991 return bin_op(upma1
, upma2
, &pullback_entry
);
7994 /* Apply "upma2" to "upma1".
7996 * That is, compute the pullback of "upma2" by "upma1".
7998 __isl_give isl_union_pw_multi_aff
*
7999 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
8000 __isl_take isl_union_pw_multi_aff
*upma1
,
8001 __isl_take isl_union_pw_multi_aff
*upma2
)
8003 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2
, upma1
);
8007 #define BASE pw_multi_aff
8009 #include "isl_copy_tuple_id_templ.c"
8011 /* Given a function "pma1" of the form A[B -> C] -> D and
8012 * a function "pma2" of the form E -> B,
8013 * replace the domain of the wrapped relation inside the domain of "pma1"
8014 * by the preimage with respect to "pma2".
8015 * In other words, plug in "pma2" in this nested domain.
8016 * The result is of the form A[E -> C] -> D.
8018 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
8019 * plug that into "pma1".
8021 __isl_give isl_pw_multi_aff
*
8022 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8023 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
8025 isl_space
*pma1_space
, *pma2_space
;
8027 isl_pw_multi_aff
*id
;
8029 pma1_space
= isl_pw_multi_aff_peek_space(pma1
);
8030 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8032 if (isl_space_check_domain_is_wrapping(pma1_space
) < 0)
8034 if (isl_space_check_wrapped_tuple_is_equal(pma1_space
,
8035 isl_dim_in
, isl_dim_in
, pma2_space
, isl_dim_out
) < 0)
8038 space
= isl_space_domain(isl_space_copy(pma1_space
));
8039 space
= isl_space_range(isl_space_unwrap(space
));
8040 id
= isl_pw_multi_aff_identity_on_domain_space(space
);
8041 pma2
= isl_pw_multi_aff_product(pma2
, id
);
8043 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_in
,
8044 pma1_space
, isl_dim_in
);
8045 pma2
= isl_pw_multi_aff_copy_tuple_id(pma2
, isl_dim_out
,
8046 pma1_space
, isl_dim_in
);
8048 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1
, pma2
);
8050 isl_pw_multi_aff_free(pma1
);
8051 isl_pw_multi_aff_free(pma2
);
8055 /* If data->pma and "pma2" are such that
8056 * data->pma is of the form A[B -> C] -> D and
8057 * "pma2" is of the form E -> B,
8058 * then replace the domain of the wrapped relation
8059 * inside the domain of data->pma by the preimage with respect to "pma2" and
8060 * add the result to data->res.
8062 static isl_stat
preimage_domain_wrapped_domain_entry(
8063 __isl_take isl_pw_multi_aff
*pma2
, void *user
)
8065 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
8066 isl_space
*pma1_space
, *pma2_space
;
8069 pma1_space
= isl_pw_multi_aff_peek_space(data
->pma
);
8070 pma2_space
= isl_pw_multi_aff_peek_space(pma2
);
8072 match
= isl_space_domain_is_wrapping(pma1_space
);
8073 if (match
>= 0 && match
)
8074 match
= isl_space_wrapped_tuple_is_equal(pma1_space
, isl_dim_in
,
8075 isl_dim_in
, pma2_space
, isl_dim_out
);
8076 if (match
< 0 || !match
) {
8077 isl_pw_multi_aff_free(pma2
);
8078 return match
< 0 ? isl_stat_error
: isl_stat_ok
;
8081 pma2
= isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8082 isl_pw_multi_aff_copy(data
->pma
), pma2
);
8084 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
8086 return isl_stat_non_null(data
->res
);
8089 /* For each pair of functions A[B -> C] -> D in "upma1" and
8090 * E -> B in "upma2",
8091 * replace the domain of the wrapped relation inside the domain of the first
8092 * by the preimage with respect to the second and collect the results.
8093 * In other words, plug in the second function in this nested domain.
8094 * The results are of the form A[E -> C] -> D.
8096 __isl_give isl_union_pw_multi_aff
*
8097 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8098 __isl_take isl_union_pw_multi_aff
*upma1
,
8099 __isl_take isl_union_pw_multi_aff
*upma2
)
8101 return bin_op(upma1
, upma2
, &preimage_domain_wrapped_domain_entry
);
8104 /* Check that the domain space of "upa" matches "space".
8106 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8107 * can in principle never fail since the space "space" is that
8108 * of the isl_multi_union_pw_aff and is a set space such that
8109 * there is no domain space to match.
8111 * We check the parameters and double-check that "space" is
8112 * indeed that of a set.
8114 static isl_stat
isl_union_pw_aff_check_match_domain_space(
8115 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8117 isl_space
*upa_space
;
8121 return isl_stat_error
;
8123 match
= isl_space_is_set(space
);
8125 return isl_stat_error
;
8127 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8128 "expecting set space", return isl_stat_error
);
8130 upa_space
= isl_union_pw_aff_get_space(upa
);
8131 match
= isl_space_has_equal_params(space
, upa_space
);
8135 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8136 "parameters don't match", goto error
);
8138 isl_space_free(upa_space
);
8141 isl_space_free(upa_space
);
8142 return isl_stat_error
;
8145 /* Do the parameters of "upa" match those of "space"?
8147 static isl_bool
isl_union_pw_aff_matching_params(
8148 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
8150 isl_space
*upa_space
;
8154 return isl_bool_error
;
8156 upa_space
= isl_union_pw_aff_get_space(upa
);
8158 match
= isl_space_has_equal_params(space
, upa_space
);
8160 isl_space_free(upa_space
);
8164 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8165 * space represents the new parameters.
8166 * res collects the results.
8168 struct isl_union_pw_aff_reset_params_data
{
8170 isl_union_pw_aff
*res
;
8173 /* Replace the parameters of "pa" by data->space and
8174 * add the result to data->res.
8176 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
8178 struct isl_union_pw_aff_reset_params_data
*data
= user
;
8181 space
= isl_pw_aff_get_space(pa
);
8182 space
= isl_space_replace_params(space
, data
->space
);
8183 pa
= isl_pw_aff_reset_space(pa
, space
);
8184 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8186 return data
->res
? isl_stat_ok
: isl_stat_error
;
8189 /* Replace the domain space of "upa" by "space".
8190 * Since a union expression does not have a (single) domain space,
8191 * "space" is necessarily a parameter space.
8193 * Since the order and the names of the parameters determine
8194 * the hash value, we need to create a new hash table.
8196 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
8197 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
8199 struct isl_union_pw_aff_reset_params_data data
= { space
};
8202 match
= isl_union_pw_aff_matching_params(upa
, space
);
8204 upa
= isl_union_pw_aff_free(upa
);
8206 isl_space_free(space
);
8210 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
8211 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
8212 data
.res
= isl_union_pw_aff_free(data
.res
);
8214 isl_union_pw_aff_free(upa
);
8215 isl_space_free(space
);
8219 /* Return the floor of "pa".
8221 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
8223 return isl_pw_aff_floor(pa
);
8226 /* Given f, return floor(f).
8228 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
8229 __isl_take isl_union_pw_aff
*upa
)
8231 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
8236 * upa mod m = upa - m * floor(upa/m)
8238 * with m an integer value.
8240 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
8241 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
8243 isl_union_pw_aff
*res
;
8248 if (!isl_val_is_int(m
))
8249 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8250 "expecting integer modulo", goto error
);
8251 if (!isl_val_is_pos(m
))
8252 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
8253 "expecting positive modulo", goto error
);
8255 res
= isl_union_pw_aff_copy(upa
);
8256 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
8257 upa
= isl_union_pw_aff_floor(upa
);
8258 upa
= isl_union_pw_aff_scale_val(upa
, m
);
8259 res
= isl_union_pw_aff_sub(res
, upa
);
8264 isl_union_pw_aff_free(upa
);
8268 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8269 * pos is the output position that needs to be extracted.
8270 * res collects the results.
8272 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
8274 isl_union_pw_aff
*res
;
8277 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8278 * (assuming it has such a dimension) and add it to data->res.
8280 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8282 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
8286 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8288 return isl_stat_error
;
8289 if (data
->pos
>= n_out
) {
8290 isl_pw_multi_aff_free(pma
);
8294 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
8295 isl_pw_multi_aff_free(pma
);
8297 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8299 return data
->res
? isl_stat_ok
: isl_stat_error
;
8302 /* Extract an isl_union_pw_aff corresponding to
8303 * output dimension "pos" of "upma".
8305 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
8306 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
8308 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
8315 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8316 "cannot extract at negative position", return NULL
);
8318 space
= isl_union_pw_multi_aff_get_space(upma
);
8319 data
.res
= isl_union_pw_aff_empty(space
);
8321 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8322 &get_union_pw_aff
, &data
) < 0)
8323 data
.res
= isl_union_pw_aff_free(data
.res
);
8328 /* Return a union piecewise affine expression
8329 * that is equal to "aff" on "domain".
8331 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
8332 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
8336 pa
= isl_pw_aff_from_aff(aff
);
8337 return isl_union_pw_aff_pw_aff_on_domain(domain
, pa
);
8340 /* Return a union piecewise affine expression
8341 * that is equal to the parameter identified by "id" on "domain".
8343 * Make sure the parameter appears in the space passed to
8344 * isl_aff_param_on_domain_space_id.
8346 __isl_give isl_union_pw_aff
*isl_union_pw_aff_param_on_domain_id(
8347 __isl_take isl_union_set
*domain
, __isl_take isl_id
*id
)
8352 space
= isl_union_set_get_space(domain
);
8353 space
= isl_space_add_param_id(space
, isl_id_copy(id
));
8354 aff
= isl_aff_param_on_domain_space_id(space
, id
);
8355 return isl_union_pw_aff_aff_on_domain(domain
, aff
);
8358 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8359 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8361 * "res" collects the results.
8363 struct isl_union_pw_aff_pw_aff_on_domain_data
{
8365 isl_union_pw_aff
*res
;
8368 /* Construct a piecewise affine expression that is equal to data->pa
8369 * on "domain" and add the result to data->res.
8371 static isl_stat
pw_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
8373 struct isl_union_pw_aff_pw_aff_on_domain_data
*data
= user
;
8377 pa
= isl_pw_aff_copy(data
->pa
);
8378 dim
= isl_set_dim(domain
, isl_dim_set
);
8380 pa
= isl_pw_aff_free(pa
);
8381 pa
= isl_pw_aff_from_range(pa
);
8382 pa
= isl_pw_aff_add_dims(pa
, isl_dim_in
, dim
);
8383 pa
= isl_pw_aff_reset_domain_space(pa
, isl_set_get_space(domain
));
8384 pa
= isl_pw_aff_intersect_domain(pa
, domain
);
8385 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8387 return data
->res
? isl_stat_ok
: isl_stat_error
;
8390 /* Return a union piecewise affine expression
8391 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8392 * have been aligned.
8394 * Construct an isl_pw_aff on each of the sets in "domain" and
8395 * collect the results.
8397 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain_aligned(
8398 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8400 struct isl_union_pw_aff_pw_aff_on_domain_data data
;
8403 space
= isl_union_set_get_space(domain
);
8404 data
.res
= isl_union_pw_aff_empty(space
);
8406 if (isl_union_set_foreach_set(domain
, &pw_aff_on_domain
, &data
) < 0)
8407 data
.res
= isl_union_pw_aff_free(data
.res
);
8408 isl_union_set_free(domain
);
8409 isl_pw_aff_free(pa
);
8413 /* Return a union piecewise affine expression
8414 * that is equal to "pa" on "domain".
8416 * Check that "pa" is a parametric expression,
8417 * align the parameters if needed and call
8418 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8420 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pw_aff_on_domain(
8421 __isl_take isl_union_set
*domain
, __isl_take isl_pw_aff
*pa
)
8424 isl_bool equal_params
;
8425 isl_space
*domain_space
, *pa_space
;
8427 pa_space
= isl_pw_aff_peek_space(pa
);
8428 is_set
= isl_space_is_set(pa_space
);
8432 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8433 "expecting parametric expression", goto error
);
8435 domain_space
= isl_union_set_get_space(domain
);
8436 pa_space
= isl_pw_aff_get_space(pa
);
8437 equal_params
= isl_space_has_equal_params(domain_space
, pa_space
);
8438 if (equal_params
>= 0 && !equal_params
) {
8441 space
= isl_space_align_params(domain_space
, pa_space
);
8442 pa
= isl_pw_aff_align_params(pa
, isl_space_copy(space
));
8443 domain
= isl_union_set_align_params(domain
, space
);
8445 isl_space_free(domain_space
);
8446 isl_space_free(pa_space
);
8449 if (equal_params
< 0)
8451 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain
, pa
);
8453 isl_union_set_free(domain
);
8454 isl_pw_aff_free(pa
);
8458 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8459 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8460 * "res" collects the results.
8462 struct isl_union_pw_aff_val_on_domain_data
{
8464 isl_union_pw_aff
*res
;
8467 /* Construct a piecewise affine expression that is equal to data->v
8468 * on "domain" and add the result to data->res.
8470 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
8472 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
8476 v
= isl_val_copy(data
->v
);
8477 pa
= isl_pw_aff_val_on_domain(domain
, v
);
8478 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8480 return data
->res
? isl_stat_ok
: isl_stat_error
;
8483 /* Return a union piecewise affine expression
8484 * that is equal to "v" on "domain".
8486 * Construct an isl_pw_aff on each of the sets in "domain" and
8487 * collect the results.
8489 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
8490 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
8492 struct isl_union_pw_aff_val_on_domain_data data
;
8495 space
= isl_union_set_get_space(domain
);
8496 data
.res
= isl_union_pw_aff_empty(space
);
8498 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
8499 data
.res
= isl_union_pw_aff_free(data
.res
);
8500 isl_union_set_free(domain
);
8505 /* Construct a piecewise multi affine expression
8506 * that is equal to "pa" and add it to upma.
8508 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
8511 isl_union_pw_multi_aff
**upma
= user
;
8512 isl_pw_multi_aff
*pma
;
8514 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
8515 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
8517 return *upma
? isl_stat_ok
: isl_stat_error
;
8520 /* Construct and return a union piecewise multi affine expression
8521 * that is equal to the given union piecewise affine expression.
8523 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
8524 __isl_take isl_union_pw_aff
*upa
)
8527 isl_union_pw_multi_aff
*upma
;
8532 space
= isl_union_pw_aff_get_space(upa
);
8533 upma
= isl_union_pw_multi_aff_empty(space
);
8535 if (isl_union_pw_aff_foreach_pw_aff(upa
,
8536 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
8537 upma
= isl_union_pw_multi_aff_free(upma
);
8539 isl_union_pw_aff_free(upa
);
8543 /* Compute the set of elements in the domain of "pa" where it is zero and
8544 * add this set to "uset".
8546 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
8548 isl_union_set
**uset
= (isl_union_set
**)user
;
8550 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
8552 return *uset
? isl_stat_ok
: isl_stat_error
;
8555 /* Return a union set containing those elements in the domain
8556 * of "upa" where it is zero.
8558 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
8559 __isl_take isl_union_pw_aff
*upa
)
8561 isl_union_set
*zero
;
8563 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8564 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
8565 zero
= isl_union_set_free(zero
);
8567 isl_union_pw_aff_free(upa
);
8571 /* Internal data structure for isl_union_pw_aff_bind_id,
8572 * storing the parameter that needs to be bound and
8573 * the accumulated results.
8575 struct isl_bind_id_data
{
8577 isl_union_set
*bound
;
8580 /* Bind the piecewise affine function "pa" to the parameter data->id,
8581 * adding the resulting elements in the domain where the expression
8582 * is equal to the parameter to data->bound.
8584 static isl_stat
bind_id(__isl_take isl_pw_aff
*pa
, void *user
)
8586 struct isl_bind_id_data
*data
= user
;
8589 bound
= isl_pw_aff_bind_id(pa
, isl_id_copy(data
->id
));
8590 data
->bound
= isl_union_set_add_set(data
->bound
, bound
);
8592 return data
->bound
? isl_stat_ok
: isl_stat_error
;
8595 /* Bind the union piecewise affine function "upa" to the parameter "id",
8596 * returning the elements in the domain where the expression
8597 * is equal to the parameter.
8599 __isl_give isl_union_set
*isl_union_pw_aff_bind_id(
8600 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_id
*id
)
8602 struct isl_bind_id_data data
= { id
};
8604 data
.bound
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
8605 if (isl_union_pw_aff_foreach_pw_aff(upa
, &bind_id
, &data
) < 0)
8606 data
.bound
= isl_union_set_free(data
.bound
);
8608 isl_union_pw_aff_free(upa
);
8613 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8614 * upma is the function that is plugged in.
8615 * pa is the current part of the function in which upma is plugged in.
8616 * res collects the results.
8618 struct isl_union_pw_aff_pullback_upma_data
{
8619 isl_union_pw_multi_aff
*upma
;
8621 isl_union_pw_aff
*res
;
8624 /* Check if "pma" can be plugged into data->pa.
8625 * If so, perform the pullback and add the result to data->res.
8627 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8629 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8632 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
8633 pma
->dim
, isl_dim_out
)) {
8634 isl_pw_multi_aff_free(pma
);
8638 pa
= isl_pw_aff_copy(data
->pa
);
8639 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
8641 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
8643 return data
->res
? isl_stat_ok
: isl_stat_error
;
8646 /* Check if any of the elements of data->upma can be plugged into pa,
8647 * add if so add the result to data->res.
8649 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
8651 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
8655 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
8657 isl_pw_aff_free(pa
);
8662 /* Compute the pullback of "upa" by the function represented by "upma".
8663 * In other words, plug in "upma" in "upa". The result contains
8664 * expressions defined over the domain space of "upma".
8666 * Run over all pairs of elements in "upa" and "upma", perform
8667 * the pullback when appropriate and collect the results.
8668 * If the hash value were based on the domain space rather than
8669 * the function space, then we could run through all elements
8670 * of "upma" and directly pick out the corresponding element of "upa".
8672 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
8673 __isl_take isl_union_pw_aff
*upa
,
8674 __isl_take isl_union_pw_multi_aff
*upma
)
8676 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
8679 space
= isl_union_pw_multi_aff_get_space(upma
);
8680 upa
= isl_union_pw_aff_align_params(upa
, space
);
8681 space
= isl_union_pw_aff_get_space(upa
);
8682 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
8688 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
8689 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
8690 data
.res
= isl_union_pw_aff_free(data
.res
);
8692 isl_union_pw_aff_free(upa
);
8693 isl_union_pw_multi_aff_free(upma
);
8696 isl_union_pw_aff_free(upa
);
8697 isl_union_pw_multi_aff_free(upma
);
8702 #define BASE union_pw_aff
8704 #define DOMBASE union_set
8706 #include <isl_multi_explicit_domain.c>
8707 #include <isl_multi_union_pw_aff_explicit_domain.c>
8708 #include <isl_multi_templ.c>
8709 #include <isl_multi_un_op_templ.c>
8710 #include <isl_multi_bin_val_templ.c>
8711 #include <isl_multi_align_set.c>
8712 #include <isl_multi_align_union_set.c>
8713 #include <isl_multi_apply_set_explicit_domain_templ.c>
8714 #include <isl_multi_apply_union_set_explicit_domain_templ.c>
8715 #include <isl_multi_arith_templ.c>
8716 #include <isl_multi_bind_templ.c>
8717 #include <isl_multi_coalesce.c>
8718 #include <isl_multi_dim_id_templ.c>
8719 #include <isl_multi_floor.c>
8720 #include <isl_multi_from_base_templ.c>
8721 #include <isl_multi_check_domain_templ.c>
8722 #include <isl_multi_gist.c>
8723 #include <isl_multi_intersect.c>
8724 #include <isl_multi_nan_templ.c>
8725 #include <isl_multi_tuple_id_templ.c>
8726 #include <isl_multi_union_add_templ.c>
8727 #include <isl_multi_zero_space_templ.c>
8729 /* Does "mupa" have a non-trivial explicit domain?
8731 * The explicit domain, if present, is trivial if it represents
8732 * an (obviously) universe parameter set.
8734 isl_bool
isl_multi_union_pw_aff_has_non_trivial_domain(
8735 __isl_keep isl_multi_union_pw_aff
*mupa
)
8737 isl_bool is_params
, trivial
;
8741 return isl_bool_error
;
8742 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa
))
8743 return isl_bool_false
;
8744 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
8745 if (is_params
< 0 || !is_params
)
8746 return isl_bool_not(is_params
);
8747 set
= isl_set_from_union_set(isl_union_set_copy(mupa
->u
.dom
));
8748 trivial
= isl_set_plain_is_universe(set
);
8750 return isl_bool_not(trivial
);
8753 /* Construct a multiple union piecewise affine expression
8754 * in the given space with value zero in each of the output dimensions.
8756 * Since there is no canonical zero value for
8757 * a union piecewise affine expression, we can only construct
8758 * a zero-dimensional "zero" value.
8760 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
8761 __isl_take isl_space
*space
)
8769 params
= isl_space_is_params(space
);
8773 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8774 "expecting proper set space", goto error
);
8775 if (!isl_space_is_set(space
))
8776 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8777 "expecting set space", goto error
);
8778 dim
= isl_space_dim(space
, isl_dim_out
);
8782 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
8783 "expecting 0D space", goto error
);
8785 return isl_multi_union_pw_aff_alloc(space
);
8787 isl_space_free(space
);
8791 /* Construct and return a multi union piecewise affine expression
8792 * that is equal to the given multi affine expression.
8794 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
8795 __isl_take isl_multi_aff
*ma
)
8797 isl_multi_pw_aff
*mpa
;
8799 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
8800 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
8803 /* This function performs the same operation as
8804 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8805 * isl_multi_aff when exported.
8807 __isl_give isl_multi_union_pw_aff
*isl_multi_aff_to_multi_union_pw_aff(
8808 __isl_take isl_multi_aff
*ma
)
8810 return isl_multi_union_pw_aff_from_multi_aff(ma
);
8813 /* Construct and return a multi union piecewise affine expression
8814 * that is equal to the given multi piecewise affine expression.
8816 * If the resulting multi union piecewise affine expression has
8817 * an explicit domain, then assign it the domain of the input.
8818 * In other cases, the domain is stored in the individual elements.
8820 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
8821 __isl_take isl_multi_pw_aff
*mpa
)
8826 isl_multi_union_pw_aff
*mupa
;
8828 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
8830 mpa
= isl_multi_pw_aff_free(mpa
);
8834 space
= isl_multi_pw_aff_get_space(mpa
);
8835 space
= isl_space_range(space
);
8836 mupa
= isl_multi_union_pw_aff_alloc(space
);
8838 for (i
= 0; i
< n
; ++i
) {
8840 isl_union_pw_aff
*upa
;
8842 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
8843 upa
= isl_union_pw_aff_from_pw_aff(pa
);
8844 mupa
= isl_multi_union_pw_aff_restore_check_space(mupa
, i
, upa
);
8846 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8848 isl_multi_pw_aff
*copy
;
8850 copy
= isl_multi_pw_aff_copy(mpa
);
8851 dom
= isl_union_set_from_set(isl_multi_pw_aff_domain(copy
));
8852 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8855 isl_multi_pw_aff_free(mpa
);
8860 /* Extract the range space of "pma" and assign it to *space.
8861 * If *space has already been set (through a previous call to this function),
8862 * then check that the range space is the same.
8864 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8866 isl_space
**space
= user
;
8867 isl_space
*pma_space
;
8870 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8871 isl_pw_multi_aff_free(pma
);
8874 return isl_stat_error
;
8880 equal
= isl_space_is_equal(pma_space
, *space
);
8881 isl_space_free(pma_space
);
8884 return isl_stat_error
;
8886 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
8887 "range spaces not the same", return isl_stat_error
);
8891 /* Construct and return a multi union piecewise affine expression
8892 * that is equal to the given union piecewise multi affine expression.
8894 * In order to be able to perform the conversion, the input
8895 * needs to be non-empty and may only involve a single range space.
8897 * If the resulting multi union piecewise affine expression has
8898 * an explicit domain, then assign it the domain of the input.
8899 * In other cases, the domain is stored in the individual elements.
8901 __isl_give isl_multi_union_pw_aff
*
8902 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8903 __isl_take isl_union_pw_multi_aff
*upma
)
8905 isl_space
*space
= NULL
;
8906 isl_multi_union_pw_aff
*mupa
;
8910 n
= isl_union_pw_multi_aff_n_pw_multi_aff(upma
);
8914 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
8915 "cannot extract range space from empty input",
8917 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
8924 n
= isl_space_dim(space
, isl_dim_set
);
8926 space
= isl_space_free(space
);
8927 mupa
= isl_multi_union_pw_aff_alloc(space
);
8929 for (i
= 0; i
< n
; ++i
) {
8930 isl_union_pw_aff
*upa
;
8932 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
8933 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8935 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
)) {
8937 isl_union_pw_multi_aff
*copy
;
8939 copy
= isl_union_pw_multi_aff_copy(upma
);
8940 dom
= isl_union_pw_multi_aff_domain(copy
);
8941 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, dom
);
8944 isl_union_pw_multi_aff_free(upma
);
8947 isl_space_free(space
);
8948 isl_union_pw_multi_aff_free(upma
);
8952 /* This function performs the same operation as
8953 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8954 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8956 __isl_give isl_multi_union_pw_aff
*
8957 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8958 __isl_take isl_union_pw_multi_aff
*upma
)
8960 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8963 /* Try and create an isl_multi_union_pw_aff that is equivalent
8964 * to the given isl_union_map.
8965 * The isl_union_map is required to be single-valued in each space.
8966 * Moreover, it cannot be empty and all range spaces need to be the same.
8967 * Otherwise, an error is produced.
8969 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
8970 __isl_take isl_union_map
*umap
)
8972 isl_union_pw_multi_aff
*upma
;
8974 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
8975 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
8978 /* This function performs the same operation as
8979 * isl_multi_union_pw_aff_from_union_map,
8980 * but is considered as a function on an isl_union_map when exported.
8982 __isl_give isl_multi_union_pw_aff
*isl_union_map_as_multi_union_pw_aff(
8983 __isl_take isl_union_map
*umap
)
8985 return isl_multi_union_pw_aff_from_union_map(umap
);
8988 /* Return a multiple union piecewise affine expression
8989 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8990 * have been aligned.
8992 * If the resulting multi union piecewise affine expression has
8993 * an explicit domain, then assign it the input domain.
8994 * In other cases, the domain is stored in the individual elements.
8996 static __isl_give isl_multi_union_pw_aff
*
8997 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8998 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9003 isl_multi_union_pw_aff
*mupa
;
9005 n
= isl_multi_val_dim(mv
, isl_dim_set
);
9006 if (!domain
|| n
< 0)
9009 space
= isl_multi_val_get_space(mv
);
9010 mupa
= isl_multi_union_pw_aff_alloc(space
);
9011 for (i
= 0; i
< n
; ++i
) {
9013 isl_union_pw_aff
*upa
;
9015 v
= isl_multi_val_get_val(mv
, i
);
9016 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
9018 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9020 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9021 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9022 isl_union_set_copy(domain
));
9024 isl_union_set_free(domain
);
9025 isl_multi_val_free(mv
);
9028 isl_union_set_free(domain
);
9029 isl_multi_val_free(mv
);
9033 /* Return a multiple union piecewise affine expression
9034 * that is equal to "mv" on "domain".
9036 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
9037 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
9039 isl_bool equal_params
;
9043 equal_params
= isl_space_has_equal_params(domain
->dim
, mv
->space
);
9044 if (equal_params
< 0)
9047 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9049 domain
= isl_union_set_align_params(domain
,
9050 isl_multi_val_get_space(mv
));
9051 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
9052 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
9054 isl_union_set_free(domain
);
9055 isl_multi_val_free(mv
);
9059 /* Return a multiple union piecewise affine expression
9060 * that is equal to "ma" on "domain".
9062 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
9063 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
9065 isl_pw_multi_aff
*pma
;
9067 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
9068 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain
, pma
);
9071 /* Return a multiple union piecewise affine expression
9072 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9073 * have been aligned.
9075 * If the resulting multi union piecewise affine expression has
9076 * an explicit domain, then assign it the input domain.
9077 * In other cases, the domain is stored in the individual elements.
9079 static __isl_give isl_multi_union_pw_aff
*
9080 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9081 __isl_take isl_union_set
*domain
, __isl_take isl_pw_multi_aff
*pma
)
9086 isl_multi_union_pw_aff
*mupa
;
9088 n
= isl_pw_multi_aff_dim(pma
, isl_dim_set
);
9089 if (!domain
|| n
< 0)
9091 space
= isl_pw_multi_aff_get_space(pma
);
9092 mupa
= isl_multi_union_pw_aff_alloc(space
);
9093 for (i
= 0; i
< n
; ++i
) {
9095 isl_union_pw_aff
*upa
;
9097 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9098 upa
= isl_union_pw_aff_pw_aff_on_domain(
9099 isl_union_set_copy(domain
), pa
);
9100 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9102 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9103 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
,
9104 isl_union_set_copy(domain
));
9106 isl_union_set_free(domain
);
9107 isl_pw_multi_aff_free(pma
);
9110 isl_union_set_free(domain
);
9111 isl_pw_multi_aff_free(pma
);
9115 /* Return a multiple union piecewise affine expression
9116 * that is equal to "pma" on "domain".
9118 __isl_give isl_multi_union_pw_aff
*
9119 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set
*domain
,
9120 __isl_take isl_pw_multi_aff
*pma
)
9122 isl_bool equal_params
;
9125 space
= isl_pw_multi_aff_peek_space(pma
);
9126 equal_params
= isl_union_set_space_has_equal_params(domain
, space
);
9127 if (equal_params
< 0)
9130 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9132 domain
= isl_union_set_align_params(domain
,
9133 isl_pw_multi_aff_get_space(pma
));
9134 pma
= isl_pw_multi_aff_align_params(pma
,
9135 isl_union_set_get_space(domain
));
9136 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain
,
9139 isl_union_set_free(domain
);
9140 isl_pw_multi_aff_free(pma
);
9144 /* Return a union set containing those elements in the domains
9145 * of the elements of "mupa" where they are all zero.
9147 * If there are no elements, then simply return the entire domain.
9149 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
9150 __isl_take isl_multi_union_pw_aff
*mupa
)
9154 isl_union_pw_aff
*upa
;
9155 isl_union_set
*zero
;
9157 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9159 mupa
= isl_multi_union_pw_aff_free(mupa
);
9164 return isl_multi_union_pw_aff_domain(mupa
);
9166 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9167 zero
= isl_union_pw_aff_zero_union_set(upa
);
9169 for (i
= 1; i
< n
; ++i
) {
9170 isl_union_set
*zero_i
;
9172 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9173 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
9175 zero
= isl_union_set_intersect(zero
, zero_i
);
9178 isl_multi_union_pw_aff_free(mupa
);
9182 /* Construct a union map mapping the shared domain
9183 * of the union piecewise affine expressions to the range of "mupa"
9184 * in the special case of a 0D multi union piecewise affine expression.
9186 * Construct a map between the explicit domain of "mupa" and
9188 * Note that this assumes that the domain consists of explicit elements.
9190 static __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff_0D(
9191 __isl_take isl_multi_union_pw_aff
*mupa
)
9195 isl_union_set
*dom
, *ran
;
9197 space
= isl_multi_union_pw_aff_get_space(mupa
);
9198 dom
= isl_multi_union_pw_aff_domain(mupa
);
9199 ran
= isl_union_set_from_set(isl_set_universe(space
));
9201 is_params
= isl_union_set_is_params(dom
);
9203 dom
= isl_union_set_free(dom
);
9205 isl_die(isl_union_set_get_ctx(dom
), isl_error_invalid
,
9206 "cannot create union map from expression without "
9207 "explicit domain elements",
9208 dom
= isl_union_set_free(dom
));
9210 return isl_union_map_from_domain_and_range(dom
, ran
);
9213 /* Construct a union map mapping the shared domain
9214 * of the union piecewise affine expressions to the range of "mupa"
9215 * with each dimension in the range equated to the
9216 * corresponding union piecewise affine expression.
9218 * If the input is zero-dimensional, then construct a mapping
9219 * from its explicit domain.
9221 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
9222 __isl_take isl_multi_union_pw_aff
*mupa
)
9227 isl_union_map
*umap
;
9228 isl_union_pw_aff
*upa
;
9230 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9232 mupa
= isl_multi_union_pw_aff_free(mupa
);
9237 return isl_union_map_from_multi_union_pw_aff_0D(mupa
);
9239 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9240 umap
= isl_union_map_from_union_pw_aff(upa
);
9242 for (i
= 1; i
< n
; ++i
) {
9243 isl_union_map
*umap_i
;
9245 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9246 umap_i
= isl_union_map_from_union_pw_aff(upa
);
9247 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
9250 space
= isl_multi_union_pw_aff_get_space(mupa
);
9251 umap
= isl_union_map_reset_range_space(umap
, space
);
9253 isl_multi_union_pw_aff_free(mupa
);
9257 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9258 * "range" is the space from which to set the range space.
9259 * "res" collects the results.
9261 struct isl_union_pw_multi_aff_reset_range_space_data
{
9263 isl_union_pw_multi_aff
*res
;
9266 /* Replace the range space of "pma" by the range space of data->range and
9267 * add the result to data->res.
9269 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
9271 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
9274 space
= isl_pw_multi_aff_get_space(pma
);
9275 space
= isl_space_domain(space
);
9276 space
= isl_space_extend_domain_with_range(space
,
9277 isl_space_copy(data
->range
));
9278 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
9279 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
9281 return data
->res
? isl_stat_ok
: isl_stat_error
;
9284 /* Replace the range space of all the piecewise affine expressions in "upma" by
9285 * the range space of "space".
9287 * This assumes that all these expressions have the same output dimension.
9289 * Since the spaces of the expressions change, so do their hash values.
9290 * We therefore need to create a new isl_union_pw_multi_aff.
9291 * Note that the hash value is currently computed based on the entire
9292 * space even though there can only be a single expression with a given
9295 static __isl_give isl_union_pw_multi_aff
*
9296 isl_union_pw_multi_aff_reset_range_space(
9297 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
9299 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
9300 isl_space
*space_upma
;
9302 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
9303 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
9304 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
9305 &reset_range_space
, &data
) < 0)
9306 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
9308 isl_space_free(space
);
9309 isl_union_pw_multi_aff_free(upma
);
9313 /* Construct and return a union piecewise multi affine expression
9314 * that is equal to the given multi union piecewise affine expression,
9315 * in the special case of a 0D multi union piecewise affine expression.
9317 * Construct a union piecewise multi affine expression
9318 * on top of the explicit domain of the input.
9320 __isl_give isl_union_pw_multi_aff
*
9321 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9322 __isl_take isl_multi_union_pw_aff
*mupa
)
9326 isl_union_set
*domain
;
9328 space
= isl_multi_union_pw_aff_get_space(mupa
);
9329 mv
= isl_multi_val_zero(space
);
9330 domain
= isl_multi_union_pw_aff_domain(mupa
);
9331 return isl_union_pw_multi_aff_multi_val_on_domain(domain
, mv
);
9334 /* Construct and return a union piecewise multi affine expression
9335 * that is equal to the given multi union piecewise affine expression.
9337 * If the input is zero-dimensional, then
9338 * construct a union piecewise multi affine expression
9339 * on top of the explicit domain of the input.
9341 __isl_give isl_union_pw_multi_aff
*
9342 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9343 __isl_take isl_multi_union_pw_aff
*mupa
)
9348 isl_union_pw_multi_aff
*upma
;
9349 isl_union_pw_aff
*upa
;
9351 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9353 mupa
= isl_multi_union_pw_aff_free(mupa
);
9358 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa
);
9360 space
= isl_multi_union_pw_aff_get_space(mupa
);
9361 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9362 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9364 for (i
= 1; i
< n
; ++i
) {
9365 isl_union_pw_multi_aff
*upma_i
;
9367 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9368 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
9369 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
9372 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
9374 isl_multi_union_pw_aff_free(mupa
);
9378 /* Intersect the range of "mupa" with "range",
9379 * in the special case where "mupa" is 0D.
9381 * Intersect the domain of "mupa" with the constraints on the parameters
9384 static __isl_give isl_multi_union_pw_aff
*mupa_intersect_range_0D(
9385 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9387 range
= isl_set_params(range
);
9388 mupa
= isl_multi_union_pw_aff_intersect_params(mupa
, range
);
9392 /* Intersect the range of "mupa" with "range".
9393 * That is, keep only those domain elements that have a function value
9396 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
9397 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
9399 isl_union_pw_multi_aff
*upma
;
9400 isl_union_set
*domain
;
9405 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9406 if (n
< 0 || !range
)
9409 space
= isl_set_get_space(range
);
9410 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
9411 space
, isl_dim_set
);
9412 isl_space_free(space
);
9416 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
9417 "space don't match", goto error
);
9419 return mupa_intersect_range_0D(mupa
, range
);
9421 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
9422 isl_multi_union_pw_aff_copy(mupa
));
9423 domain
= isl_union_set_from_set(range
);
9424 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
9425 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
9429 isl_multi_union_pw_aff_free(mupa
);
9430 isl_set_free(range
);
9434 /* Return the shared domain of the elements of "mupa",
9435 * in the special case where "mupa" is zero-dimensional.
9437 * Return the explicit domain of "mupa".
9438 * Note that this domain may be a parameter set, either
9439 * because "mupa" is meant to live in a set space or
9440 * because no explicit domain has been set.
9442 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain_0D(
9443 __isl_take isl_multi_union_pw_aff
*mupa
)
9447 dom
= isl_multi_union_pw_aff_get_explicit_domain(mupa
);
9448 isl_multi_union_pw_aff_free(mupa
);
9453 /* Return the shared domain of the elements of "mupa".
9455 * If "mupa" is zero-dimensional, then return its explicit domain.
9457 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
9458 __isl_take isl_multi_union_pw_aff
*mupa
)
9462 isl_union_pw_aff
*upa
;
9465 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9467 mupa
= isl_multi_union_pw_aff_free(mupa
);
9472 return isl_multi_union_pw_aff_domain_0D(mupa
);
9474 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
9475 dom
= isl_union_pw_aff_domain(upa
);
9476 for (i
= 1; i
< n
; ++i
) {
9477 isl_union_set
*dom_i
;
9479 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9480 dom_i
= isl_union_pw_aff_domain(upa
);
9481 dom
= isl_union_set_intersect(dom
, dom_i
);
9484 isl_multi_union_pw_aff_free(mupa
);
9488 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9489 * In particular, the spaces have been aligned.
9490 * The result is defined over the shared domain of the elements of "mupa"
9492 * We first extract the parametric constant part of "aff" and
9493 * define that over the shared domain.
9494 * Then we iterate over all input dimensions of "aff" and add the corresponding
9495 * multiples of the elements of "mupa".
9496 * Finally, we consider the integer divisions, calling the function
9497 * recursively to obtain an isl_union_pw_aff corresponding to the
9498 * integer division argument.
9500 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
9501 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9504 isl_size n_in
, n_div
;
9505 isl_union_pw_aff
*upa
;
9506 isl_union_set
*uset
;
9510 n_in
= isl_aff_dim(aff
, isl_dim_in
);
9511 n_div
= isl_aff_dim(aff
, isl_dim_div
);
9512 if (n_in
< 0 || n_div
< 0)
9515 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
9516 cst
= isl_aff_copy(aff
);
9517 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
9518 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
9519 cst
= isl_aff_project_domain_on_params(cst
);
9520 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
9522 for (i
= 0; i
< n_in
; ++i
) {
9523 isl_union_pw_aff
*upa_i
;
9525 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
9527 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
9528 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9529 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9530 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9533 for (i
= 0; i
< n_div
; ++i
) {
9535 isl_union_pw_aff
*upa_i
;
9537 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
9539 div
= isl_aff_get_div(aff
, i
);
9540 upa_i
= multi_union_pw_aff_apply_aff(
9541 isl_multi_union_pw_aff_copy(mupa
), div
);
9542 upa_i
= isl_union_pw_aff_floor(upa_i
);
9543 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
9544 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
9545 upa
= isl_union_pw_aff_add(upa
, upa_i
);
9548 isl_multi_union_pw_aff_free(mupa
);
9553 isl_multi_union_pw_aff_free(mupa
);
9558 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9559 * with the domain of "aff".
9560 * Furthermore, the dimension of this space needs to be greater than zero.
9561 * The result is defined over the shared domain of the elements of "mupa"
9563 * We perform these checks and then hand over control to
9564 * multi_union_pw_aff_apply_aff.
9566 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
9567 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
9570 isl_space
*space1
, *space2
;
9573 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9574 isl_aff_get_space(aff
));
9575 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
9579 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9580 space2
= isl_aff_get_domain_space(aff
);
9581 equal
= isl_space_is_equal(space1
, space2
);
9582 isl_space_free(space1
);
9583 isl_space_free(space2
);
9587 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9588 "spaces don't match", goto error
);
9589 dim
= isl_aff_dim(aff
, isl_dim_in
);
9593 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
9594 "cannot determine domains", goto error
);
9596 return multi_union_pw_aff_apply_aff(mupa
, aff
);
9598 isl_multi_union_pw_aff_free(mupa
);
9603 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9604 * The space of "mupa" is known to be compatible with the domain of "ma".
9606 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9607 * on the domain of "mupa".
9609 static __isl_give isl_multi_union_pw_aff
*mupa_apply_multi_aff_0D(
9610 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9614 dom
= isl_multi_union_pw_aff_domain(mupa
);
9615 ma
= isl_multi_aff_project_domain_on_params(ma
);
9617 return isl_multi_union_pw_aff_multi_aff_on_domain(dom
, ma
);
9620 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9621 * with the domain of "ma".
9622 * The result is defined over the shared domain of the elements of "mupa"
9624 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
9625 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
9627 isl_space
*space1
, *space2
;
9628 isl_multi_union_pw_aff
*res
;
9631 isl_size n_in
, n_out
;
9633 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9634 isl_multi_aff_get_space(ma
));
9635 ma
= isl_multi_aff_align_params(ma
,
9636 isl_multi_union_pw_aff_get_space(mupa
));
9637 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
9638 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
9639 if (!mupa
|| n_in
< 0 || n_out
< 0)
9642 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9643 space2
= isl_multi_aff_get_domain_space(ma
);
9644 equal
= isl_space_is_equal(space1
, space2
);
9645 isl_space_free(space1
);
9646 isl_space_free(space2
);
9650 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
9651 "spaces don't match", goto error
);
9653 return mupa_apply_multi_aff_0D(mupa
, ma
);
9655 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
9656 res
= isl_multi_union_pw_aff_alloc(space1
);
9658 for (i
= 0; i
< n_out
; ++i
) {
9660 isl_union_pw_aff
*upa
;
9662 aff
= isl_multi_aff_get_aff(ma
, i
);
9663 upa
= multi_union_pw_aff_apply_aff(
9664 isl_multi_union_pw_aff_copy(mupa
), aff
);
9665 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9668 isl_multi_aff_free(ma
);
9669 isl_multi_union_pw_aff_free(mupa
);
9672 isl_multi_union_pw_aff_free(mupa
);
9673 isl_multi_aff_free(ma
);
9677 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9678 * The space of "mupa" is known to be compatible with the domain of "pa".
9680 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9681 * on the domain of "mupa".
9683 static __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff_0D(
9684 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9688 dom
= isl_multi_union_pw_aff_domain(mupa
);
9689 pa
= isl_pw_aff_project_domain_on_params(pa
);
9691 return isl_union_pw_aff_pw_aff_on_domain(dom
, pa
);
9694 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9695 * with the domain of "pa".
9696 * Furthermore, the dimension of this space needs to be greater than zero.
9697 * The result is defined over the shared domain of the elements of "mupa"
9699 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
9700 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
9705 isl_space
*space
, *space2
;
9706 isl_union_pw_aff
*upa
;
9708 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9709 isl_pw_aff_get_space(pa
));
9710 pa
= isl_pw_aff_align_params(pa
,
9711 isl_multi_union_pw_aff_get_space(mupa
));
9715 space
= isl_multi_union_pw_aff_get_space(mupa
);
9716 space2
= isl_pw_aff_get_domain_space(pa
);
9717 equal
= isl_space_is_equal(space
, space2
);
9718 isl_space_free(space
);
9719 isl_space_free(space2
);
9723 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
9724 "spaces don't match", goto error
);
9725 n_in
= isl_pw_aff_dim(pa
, isl_dim_in
);
9729 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa
, pa
);
9731 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
9732 upa
= isl_union_pw_aff_empty(space
);
9734 for (i
= 0; i
< pa
->n
; ++i
) {
9737 isl_multi_union_pw_aff
*mupa_i
;
9738 isl_union_pw_aff
*upa_i
;
9740 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
9741 domain
= isl_set_copy(pa
->p
[i
].set
);
9742 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
9743 aff
= isl_aff_copy(pa
->p
[i
].aff
);
9744 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
9745 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
9748 isl_multi_union_pw_aff_free(mupa
);
9749 isl_pw_aff_free(pa
);
9752 isl_multi_union_pw_aff_free(mupa
);
9753 isl_pw_aff_free(pa
);
9757 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9758 * The space of "mupa" is known to be compatible with the domain of "pma".
9760 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9761 * on the domain of "mupa".
9763 static __isl_give isl_multi_union_pw_aff
*mupa_apply_pw_multi_aff_0D(
9764 __isl_take isl_multi_union_pw_aff
*mupa
,
9765 __isl_take isl_pw_multi_aff
*pma
)
9769 dom
= isl_multi_union_pw_aff_domain(mupa
);
9770 pma
= isl_pw_multi_aff_project_domain_on_params(pma
);
9772 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom
, pma
);
9775 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9776 * with the domain of "pma".
9777 * The result is defined over the shared domain of the elements of "mupa"
9779 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
9780 __isl_take isl_multi_union_pw_aff
*mupa
,
9781 __isl_take isl_pw_multi_aff
*pma
)
9783 isl_space
*space1
, *space2
;
9784 isl_multi_union_pw_aff
*res
;
9787 isl_size n_in
, n_out
;
9789 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9790 isl_pw_multi_aff_get_space(pma
));
9791 pma
= isl_pw_multi_aff_align_params(pma
,
9792 isl_multi_union_pw_aff_get_space(mupa
));
9796 space1
= isl_multi_union_pw_aff_get_space(mupa
);
9797 space2
= isl_pw_multi_aff_get_domain_space(pma
);
9798 equal
= isl_space_is_equal(space1
, space2
);
9799 isl_space_free(space1
);
9800 isl_space_free(space2
);
9804 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
9805 "spaces don't match", goto error
);
9806 n_in
= isl_pw_multi_aff_dim(pma
, isl_dim_in
);
9807 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
9808 if (n_in
< 0 || n_out
< 0)
9811 return mupa_apply_pw_multi_aff_0D(mupa
, pma
);
9813 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
9814 res
= isl_multi_union_pw_aff_alloc(space1
);
9816 for (i
= 0; i
< n_out
; ++i
) {
9818 isl_union_pw_aff
*upa
;
9820 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
9821 upa
= isl_multi_union_pw_aff_apply_pw_aff(
9822 isl_multi_union_pw_aff_copy(mupa
), pa
);
9823 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
9826 isl_pw_multi_aff_free(pma
);
9827 isl_multi_union_pw_aff_free(mupa
);
9830 isl_multi_union_pw_aff_free(mupa
);
9831 isl_pw_multi_aff_free(pma
);
9835 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9836 * If the explicit domain only keeps track of constraints on the parameters,
9837 * then only update those constraints.
9839 static __isl_give isl_multi_union_pw_aff
*preimage_explicit_domain(
9840 __isl_take isl_multi_union_pw_aff
*mupa
,
9841 __isl_keep isl_union_pw_multi_aff
*upma
)
9845 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa
) < 0)
9846 return isl_multi_union_pw_aff_free(mupa
);
9848 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9852 is_params
= isl_union_set_is_params(mupa
->u
.dom
);
9854 return isl_multi_union_pw_aff_free(mupa
);
9856 upma
= isl_union_pw_multi_aff_copy(upma
);
9858 mupa
->u
.dom
= isl_union_set_intersect_params(mupa
->u
.dom
,
9859 isl_union_set_params(isl_union_pw_multi_aff_domain(upma
)));
9861 mupa
->u
.dom
= isl_union_set_preimage_union_pw_multi_aff(
9864 return isl_multi_union_pw_aff_free(mupa
);
9868 /* Compute the pullback of "mupa" by the function represented by "upma".
9869 * In other words, plug in "upma" in "mupa". The result contains
9870 * expressions defined over the domain space of "upma".
9872 * Run over all elements of "mupa" and plug in "upma" in each of them.
9874 * If "mupa" has an explicit domain, then it is this domain
9875 * that needs to undergo a pullback instead, i.e., a preimage.
9877 __isl_give isl_multi_union_pw_aff
*
9878 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9879 __isl_take isl_multi_union_pw_aff
*mupa
,
9880 __isl_take isl_union_pw_multi_aff
*upma
)
9885 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
9886 isl_union_pw_multi_aff_get_space(upma
));
9887 upma
= isl_union_pw_multi_aff_align_params(upma
,
9888 isl_multi_union_pw_aff_get_space(mupa
));
9889 mupa
= isl_multi_union_pw_aff_cow(mupa
);
9890 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9894 for (i
= 0; i
< n
; ++i
) {
9895 isl_union_pw_aff
*upa
;
9897 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9898 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
9899 isl_union_pw_multi_aff_copy(upma
));
9900 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
9903 if (isl_multi_union_pw_aff_has_explicit_domain(mupa
))
9904 mupa
= preimage_explicit_domain(mupa
, upma
);
9906 isl_union_pw_multi_aff_free(upma
);
9909 isl_multi_union_pw_aff_free(mupa
);
9910 isl_union_pw_multi_aff_free(upma
);
9914 /* Extract the sequence of elements in "mupa" with domain space "space"
9915 * (ignoring parameters).
9917 * For the elements of "mupa" that are not defined on the specified space,
9918 * the corresponding element in the result is empty.
9920 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
9921 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
9925 isl_space
*space_mpa
;
9926 isl_multi_pw_aff
*mpa
;
9928 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
9929 if (n
< 0 || !space
)
9932 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
9933 space
= isl_space_replace_params(space
, space_mpa
);
9934 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
9936 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
9938 space
= isl_space_from_domain(space
);
9939 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
9940 for (i
= 0; i
< n
; ++i
) {
9941 isl_union_pw_aff
*upa
;
9944 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
9945 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
9946 isl_space_copy(space
));
9947 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
9948 isl_union_pw_aff_free(upa
);
9951 isl_space_free(space
);
9954 isl_space_free(space
);
9958 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9959 * should modify the base expressions in the input.
9961 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9962 * are taken into account.
9963 * "fn" is applied to each entry in the input.
9965 struct isl_union_pw_multi_aff_un_op_control
{
9966 isl_bool (*filter
)(__isl_keep isl_pw_multi_aff
*part
);
9967 __isl_give isl_pw_multi_aff
*(*fn
)(__isl_take isl_pw_multi_aff
*pma
);
9970 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9971 * a second argument) for use as an isl_union_pw_multi_aff_transform
9972 * filter function (which does take a second argument).
9973 * Simply call control->filter without the second argument.
9975 static isl_bool
isl_union_pw_multi_aff_un_op_filter_drop_user(
9976 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9978 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9980 return control
->filter(pma
);
9983 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9984 * a second argument) for use as an isl_union_pw_multi_aff_transform
9985 * base function (which does take a second argument).
9986 * Simply call control->fn without the second argument.
9988 static __isl_give isl_pw_multi_aff
*isl_union_pw_multi_aff_un_op_drop_user(
9989 __isl_take isl_pw_multi_aff
*pma
, void *user
)
9991 struct isl_union_pw_multi_aff_un_op_control
*control
= user
;
9993 return control
->fn(pma
);
9996 /* Construct an isl_union_pw_multi_aff that is obtained by
9997 * modifying "upma" according to "control".
9999 * isl_union_pw_multi_aff_transform performs essentially
10000 * the same operation, but takes a filter and a callback function
10001 * of a different form (with an extra argument).
10002 * Call isl_union_pw_multi_aff_transform with wrappers
10003 * that remove this extra argument.
10005 static __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_un_op(
10006 __isl_take isl_union_pw_multi_aff
*upma
,
10007 struct isl_union_pw_multi_aff_un_op_control
*control
)
10009 struct isl_union_pw_multi_aff_transform_control t_control
= {
10010 .filter
= &isl_union_pw_multi_aff_un_op_filter_drop_user
,
10011 .filter_user
= control
,
10012 .fn
= &isl_union_pw_multi_aff_un_op_drop_user
,
10013 .fn_user
= control
,
10016 return isl_union_pw_multi_aff_transform(upma
, &t_control
);
10019 /* For each function in "upma" of the form A -> [B -> C],
10020 * extract the function A -> B and collect the results.
10022 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_domain(
10023 __isl_take isl_union_pw_multi_aff
*upma
)
10025 struct isl_union_pw_multi_aff_un_op_control control
= {
10026 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10027 .fn
= &isl_pw_multi_aff_range_factor_domain
,
10029 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10032 /* For each function in "upma" of the form A -> [B -> C],
10033 * extract the function A -> C and collect the results.
10035 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_range_factor_range(
10036 __isl_take isl_union_pw_multi_aff
*upma
)
10038 struct isl_union_pw_multi_aff_un_op_control control
= {
10039 .filter
= &isl_pw_multi_aff_range_is_wrapping
,
10040 .fn
= &isl_pw_multi_aff_range_factor_range
,
10042 return isl_union_pw_multi_aff_un_op(upma
, &control
);
10045 /* Evaluate the affine function "aff" in the void point "pnt".
10046 * In particular, return the value NaN.
10048 static __isl_give isl_val
*eval_void(__isl_take isl_aff
*aff
,
10049 __isl_take isl_point
*pnt
)
10053 ctx
= isl_point_get_ctx(pnt
);
10055 isl_point_free(pnt
);
10056 return isl_val_nan(ctx
);
10059 /* Evaluate the affine expression "aff"
10060 * in the coordinates (with denominator) "pnt".
10062 static __isl_give isl_val
*eval(__isl_keep isl_vec
*aff
,
10063 __isl_keep isl_vec
*pnt
)
10072 ctx
= isl_vec_get_ctx(aff
);
10075 isl_seq_inner_product(aff
->el
+ 1, pnt
->el
, pnt
->size
, &n
);
10076 isl_int_mul(d
, aff
->el
[0], pnt
->el
[0]);
10077 v
= isl_val_rat_from_isl_int(ctx
, n
, d
);
10078 v
= isl_val_normalize(v
);
10085 /* Check that the domain space of "aff" is equal to "space".
10087 static isl_stat
isl_aff_check_has_domain_space(__isl_keep isl_aff
*aff
,
10088 __isl_keep isl_space
*space
)
10092 ok
= isl_space_is_equal(isl_aff_peek_domain_space(aff
), space
);
10094 return isl_stat_error
;
10096 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
10097 "incompatible spaces", return isl_stat_error
);
10098 return isl_stat_ok
;
10101 /* Evaluate the affine function "aff" in "pnt".
10103 __isl_give isl_val
*isl_aff_eval(__isl_take isl_aff
*aff
,
10104 __isl_take isl_point
*pnt
)
10108 isl_local_space
*ls
;
10110 if (isl_aff_check_has_domain_space(aff
, isl_point_peek_space(pnt
)) < 0)
10112 is_void
= isl_point_is_void(pnt
);
10116 return eval_void(aff
, pnt
);
10118 ls
= isl_aff_get_domain_local_space(aff
);
10119 pnt
= isl_local_space_lift_point(ls
, pnt
);
10121 v
= eval(aff
->v
, isl_point_peek_vec(pnt
));
10124 isl_point_free(pnt
);
10129 isl_point_free(pnt
);