2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
36 #include <isl_list_templ.c>
41 #include <isl_list_templ.c>
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
54 __isl_take isl_vec
*v
)
61 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
71 isl_local_space_free(ls
);
76 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
85 ctx
= isl_local_space_get_ctx(ls
);
86 if (!isl_local_space_divs_known(ls
))
87 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
89 if (!isl_local_space_is_set(ls
))
90 isl_die(ctx
, isl_error_invalid
,
91 "domain of affine expression should be a set",
94 total
= isl_local_space_dim(ls
, isl_dim_all
);
95 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
96 return isl_aff_alloc_vec(ls
, v
);
98 isl_local_space_free(ls
);
102 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
106 aff
= isl_aff_alloc(ls
);
110 isl_int_set_si(aff
->v
->el
[0], 1);
111 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
131 aff
= isl_aff_alloc(ls
);
135 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
152 __isl_take isl_val
*val
)
158 if (!isl_val_is_rat(val
))
159 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
160 "expecting rational value", goto error
);
162 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
166 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
167 isl_int_set(aff
->v
->el
[1], val
->n
);
168 isl_int_set(aff
->v
->el
[0], val
->d
);
170 isl_local_space_free(ls
);
174 isl_local_space_free(ls
);
179 /* Return an affine expression that is equal to the specified dimension
182 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
183 enum isl_dim_type type
, unsigned pos
)
191 space
= isl_local_space_get_space(ls
);
194 if (isl_space_is_map(space
))
195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
196 "expecting (parameter) set space", goto error
);
197 if (pos
>= isl_local_space_dim(ls
, type
))
198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
199 "position out of bounds", goto error
);
201 isl_space_free(space
);
202 aff
= isl_aff_alloc(ls
);
206 pos
+= isl_local_space_offset(aff
->ls
, type
);
208 isl_int_set_si(aff
->v
->el
[0], 1);
209 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
210 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
214 isl_local_space_free(ls
);
215 isl_space_free(space
);
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
223 enum isl_dim_type type
, unsigned pos
)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
228 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
237 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
242 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
243 isl_vec_copy(aff
->v
));
246 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
254 return isl_aff_dup(aff
);
257 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
265 isl_local_space_free(aff
->ls
);
266 isl_vec_free(aff
->v
);
273 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
275 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
278 /* Return a hash value that digests "aff".
280 uint32_t isl_aff_get_hash(__isl_keep isl_aff
*aff
)
282 uint32_t hash
, ls_hash
, v_hash
;
287 hash
= isl_hash_init();
288 ls_hash
= isl_local_space_get_hash(aff
->ls
);
289 isl_hash_hash(hash
, ls_hash
);
290 v_hash
= isl_vec_get_hash(aff
->v
);
291 isl_hash_hash(hash
, v_hash
);
296 /* Externally, an isl_aff has a map space, but internally, the
297 * ls field corresponds to the domain of that space.
299 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
303 if (type
== isl_dim_out
)
305 if (type
== isl_dim_in
)
307 return isl_local_space_dim(aff
->ls
, type
);
310 /* Return the position of the dimension of the given type and name
312 * Return -1 if no such dimension can be found.
314 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
319 if (type
== isl_dim_out
)
321 if (type
== isl_dim_in
)
323 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
326 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
328 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
331 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
336 space
= isl_local_space_get_space(aff
->ls
);
337 space
= isl_space_from_domain(space
);
338 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
342 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
343 __isl_keep isl_aff
*aff
)
345 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
348 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
353 ls
= isl_local_space_copy(aff
->ls
);
354 ls
= isl_local_space_from_domain(ls
);
355 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
359 /* Externally, an isl_aff has a map space, but internally, the
360 * ls field corresponds to the domain of that space.
362 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
363 enum isl_dim_type type
, unsigned pos
)
367 if (type
== isl_dim_out
)
369 if (type
== isl_dim_in
)
371 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
374 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
375 __isl_take isl_space
*dim
)
377 aff
= isl_aff_cow(aff
);
381 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
383 return isl_aff_free(aff
);
392 /* Reset the space of "aff". This function is called from isl_pw_templ.c
393 * and doesn't know if the space of an element object is represented
394 * directly or through its domain. It therefore passes along both.
396 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
397 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
399 isl_space_free(space
);
400 return isl_aff_reset_domain_space(aff
, domain
);
403 /* Reorder the coefficients of the affine expression based
404 * on the given reodering.
405 * The reordering r is assumed to have been extended with the local
408 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
409 __isl_take isl_reordering
*r
, int n_div
)
417 res
= isl_vec_alloc(vec
->ctx
,
418 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
419 isl_seq_cpy(res
->el
, vec
->el
, 2);
420 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
421 for (i
= 0; i
< r
->len
; ++i
)
422 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
424 isl_reordering_free(r
);
429 isl_reordering_free(r
);
433 /* Reorder the dimensions of the domain of "aff" according
434 * to the given reordering.
436 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
437 __isl_take isl_reordering
*r
)
439 aff
= isl_aff_cow(aff
);
443 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
444 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
445 aff
->ls
->div
->n_row
);
446 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
448 if (!aff
->v
|| !aff
->ls
)
449 return isl_aff_free(aff
);
454 isl_reordering_free(r
);
458 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
459 __isl_take isl_space
*model
)
464 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
465 model
, isl_dim_param
)) {
468 model
= isl_space_drop_dims(model
, isl_dim_in
,
469 0, isl_space_dim(model
, isl_dim_in
));
470 model
= isl_space_drop_dims(model
, isl_dim_out
,
471 0, isl_space_dim(model
, isl_dim_out
));
472 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
473 exp
= isl_reordering_extend_space(exp
,
474 isl_aff_get_domain_space(aff
));
475 aff
= isl_aff_realign_domain(aff
, exp
);
478 isl_space_free(model
);
481 isl_space_free(model
);
486 /* Is "aff" obviously equal to zero?
488 * If the denominator is zero, then "aff" is not equal to zero.
490 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
493 return isl_bool_error
;
495 if (isl_int_is_zero(aff
->v
->el
[0]))
496 return isl_bool_false
;
497 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
500 /* Does "aff" represent NaN?
502 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
505 return isl_bool_error
;
507 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
510 /* Does "pa" involve any NaNs?
512 isl_bool
isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
517 return isl_bool_error
;
519 return isl_bool_false
;
521 for (i
= 0; i
< pa
->n
; ++i
) {
522 isl_bool is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
523 if (is_nan
< 0 || is_nan
)
527 return isl_bool_false
;
530 /* Are "aff1" and "aff2" obviously equal?
532 * NaN is not equal to anything, not even to another NaN.
534 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
535 __isl_keep isl_aff
*aff2
)
540 return isl_bool_error
;
542 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
543 return isl_bool_false
;
545 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
546 if (equal
< 0 || !equal
)
549 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
552 /* Return the common denominator of "aff" in "v".
554 * We cannot return anything meaningful in case of a NaN.
556 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
560 if (isl_aff_is_nan(aff
))
561 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
562 "cannot get denominator of NaN", return -1);
563 isl_int_set(*v
, aff
->v
->el
[0]);
567 /* Return the common denominator of "aff".
569 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
576 ctx
= isl_aff_get_ctx(aff
);
577 if (isl_aff_is_nan(aff
))
578 return isl_val_nan(ctx
);
579 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
582 /* Return the constant term of "aff" in "v".
584 * We cannot return anything meaningful in case of a NaN.
586 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
590 if (isl_aff_is_nan(aff
))
591 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
592 "cannot get constant term of NaN", return -1);
593 isl_int_set(*v
, aff
->v
->el
[1]);
597 /* Return the constant term of "aff".
599 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
607 ctx
= isl_aff_get_ctx(aff
);
608 if (isl_aff_is_nan(aff
))
609 return isl_val_nan(ctx
);
610 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
611 return isl_val_normalize(v
);
614 /* Return the coefficient of the variable of type "type" at position "pos"
617 * We cannot return anything meaningful in case of a NaN.
619 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
620 enum isl_dim_type type
, int pos
, isl_int
*v
)
625 if (type
== isl_dim_out
)
626 isl_die(aff
->v
->ctx
, isl_error_invalid
,
627 "output/set dimension does not have a coefficient",
629 if (type
== isl_dim_in
)
632 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
633 isl_die(aff
->v
->ctx
, isl_error_invalid
,
634 "position out of bounds", return -1);
636 if (isl_aff_is_nan(aff
))
637 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
638 "cannot get coefficient of NaN", return -1);
639 pos
+= isl_local_space_offset(aff
->ls
, type
);
640 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
645 /* Return the coefficient of the variable of type "type" at position "pos"
648 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
649 enum isl_dim_type type
, int pos
)
657 ctx
= isl_aff_get_ctx(aff
);
658 if (type
== isl_dim_out
)
659 isl_die(ctx
, isl_error_invalid
,
660 "output/set dimension does not have a coefficient",
662 if (type
== isl_dim_in
)
665 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
666 isl_die(ctx
, isl_error_invalid
,
667 "position out of bounds", return NULL
);
669 if (isl_aff_is_nan(aff
))
670 return isl_val_nan(ctx
);
671 pos
+= isl_local_space_offset(aff
->ls
, type
);
672 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
673 return isl_val_normalize(v
);
676 /* Return the sign of the coefficient of the variable of type "type"
677 * at position "pos" of "aff".
679 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
687 ctx
= isl_aff_get_ctx(aff
);
688 if (type
== isl_dim_out
)
689 isl_die(ctx
, isl_error_invalid
,
690 "output/set dimension does not have a coefficient",
692 if (type
== isl_dim_in
)
695 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
696 isl_die(ctx
, isl_error_invalid
,
697 "position out of bounds", return 0);
699 pos
+= isl_local_space_offset(aff
->ls
, type
);
700 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
703 /* Replace the denominator of "aff" by "v".
705 * A NaN is unaffected by this operation.
707 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
711 if (isl_aff_is_nan(aff
))
713 aff
= isl_aff_cow(aff
);
717 aff
->v
= isl_vec_cow(aff
->v
);
719 return isl_aff_free(aff
);
721 isl_int_set(aff
->v
->el
[0], v
);
726 /* Replace the numerator of the constant term of "aff" by "v".
728 * A NaN is unaffected by this operation.
730 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
734 if (isl_aff_is_nan(aff
))
736 aff
= isl_aff_cow(aff
);
740 aff
->v
= isl_vec_cow(aff
->v
);
742 return isl_aff_free(aff
);
744 isl_int_set(aff
->v
->el
[1], v
);
749 /* Replace the constant term of "aff" by "v".
751 * A NaN is unaffected by this operation.
753 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
754 __isl_take isl_val
*v
)
759 if (isl_aff_is_nan(aff
)) {
764 if (!isl_val_is_rat(v
))
765 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
766 "expecting rational value", goto error
);
768 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
769 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
774 aff
= isl_aff_cow(aff
);
777 aff
->v
= isl_vec_cow(aff
->v
);
781 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
782 isl_int_set(aff
->v
->el
[1], v
->n
);
783 } else if (isl_int_is_one(v
->d
)) {
784 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
786 isl_seq_scale(aff
->v
->el
+ 1,
787 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
788 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
789 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
790 aff
->v
= isl_vec_normalize(aff
->v
);
803 /* Add "v" to the constant term of "aff".
805 * A NaN is unaffected by this operation.
807 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
809 if (isl_int_is_zero(v
))
814 if (isl_aff_is_nan(aff
))
816 aff
= isl_aff_cow(aff
);
820 aff
->v
= isl_vec_cow(aff
->v
);
822 return isl_aff_free(aff
);
824 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
829 /* Add "v" to the constant term of "aff".
831 * A NaN is unaffected by this operation.
833 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
834 __isl_take isl_val
*v
)
839 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
844 if (!isl_val_is_rat(v
))
845 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
846 "expecting rational value", goto error
);
848 aff
= isl_aff_cow(aff
);
852 aff
->v
= isl_vec_cow(aff
->v
);
856 if (isl_int_is_one(v
->d
)) {
857 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
858 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
859 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
860 aff
->v
= isl_vec_normalize(aff
->v
);
864 isl_seq_scale(aff
->v
->el
+ 1,
865 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
866 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
867 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
868 aff
->v
= isl_vec_normalize(aff
->v
);
881 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
886 isl_int_set_si(t
, v
);
887 aff
= isl_aff_add_constant(aff
, t
);
893 /* Add "v" to the numerator of the constant term of "aff".
895 * A NaN is unaffected by this operation.
897 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
899 if (isl_int_is_zero(v
))
904 if (isl_aff_is_nan(aff
))
906 aff
= isl_aff_cow(aff
);
910 aff
->v
= isl_vec_cow(aff
->v
);
912 return isl_aff_free(aff
);
914 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
919 /* Add "v" to the numerator of the constant term of "aff".
921 * A NaN is unaffected by this operation.
923 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
931 isl_int_set_si(t
, v
);
932 aff
= isl_aff_add_constant_num(aff
, t
);
938 /* Replace the numerator of the constant term of "aff" by "v".
940 * A NaN is unaffected by this operation.
942 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
946 if (isl_aff_is_nan(aff
))
948 aff
= isl_aff_cow(aff
);
952 aff
->v
= isl_vec_cow(aff
->v
);
954 return isl_aff_free(aff
);
956 isl_int_set_si(aff
->v
->el
[1], v
);
961 /* Replace the numerator of the coefficient of the variable of type "type"
962 * at position "pos" of "aff" by "v".
964 * A NaN is unaffected by this operation.
966 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
967 enum isl_dim_type type
, int pos
, isl_int v
)
972 if (type
== isl_dim_out
)
973 isl_die(aff
->v
->ctx
, isl_error_invalid
,
974 "output/set dimension does not have a coefficient",
975 return isl_aff_free(aff
));
976 if (type
== isl_dim_in
)
979 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
980 isl_die(aff
->v
->ctx
, isl_error_invalid
,
981 "position out of bounds", return isl_aff_free(aff
));
983 if (isl_aff_is_nan(aff
))
985 aff
= isl_aff_cow(aff
);
989 aff
->v
= isl_vec_cow(aff
->v
);
991 return isl_aff_free(aff
);
993 pos
+= isl_local_space_offset(aff
->ls
, type
);
994 isl_int_set(aff
->v
->el
[1 + pos
], v
);
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1005 enum isl_dim_type type
, int pos
, int v
)
1010 if (type
== isl_dim_out
)
1011 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff
));
1014 if (type
== isl_dim_in
)
1017 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1018 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1019 "position out of bounds", return isl_aff_free(aff
));
1021 if (isl_aff_is_nan(aff
))
1023 pos
+= isl_local_space_offset(aff
->ls
, type
);
1024 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1027 aff
= isl_aff_cow(aff
);
1031 aff
->v
= isl_vec_cow(aff
->v
);
1033 return isl_aff_free(aff
);
1035 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1040 /* Replace the coefficient of the variable of type "type" at position "pos"
1043 * A NaN is unaffected by this operation.
1045 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1046 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1051 if (type
== isl_dim_out
)
1052 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1053 "output/set dimension does not have a coefficient",
1055 if (type
== isl_dim_in
)
1058 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1059 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1060 "position out of bounds", goto error
);
1062 if (isl_aff_is_nan(aff
)) {
1066 if (!isl_val_is_rat(v
))
1067 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1068 "expecting rational value", goto error
);
1070 pos
+= isl_local_space_offset(aff
->ls
, type
);
1071 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1072 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1077 aff
= isl_aff_cow(aff
);
1080 aff
->v
= isl_vec_cow(aff
->v
);
1084 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1085 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1086 } else if (isl_int_is_one(v
->d
)) {
1087 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1089 isl_seq_scale(aff
->v
->el
+ 1,
1090 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1091 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1092 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1093 aff
->v
= isl_vec_normalize(aff
->v
);
1106 /* Add "v" to the coefficient of the variable of type "type"
1107 * at position "pos" of "aff".
1109 * A NaN is unaffected by this operation.
1111 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1112 enum isl_dim_type type
, int pos
, isl_int v
)
1117 if (type
== isl_dim_out
)
1118 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1119 "output/set dimension does not have a coefficient",
1120 return isl_aff_free(aff
));
1121 if (type
== isl_dim_in
)
1124 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1125 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1126 "position out of bounds", return isl_aff_free(aff
));
1128 if (isl_aff_is_nan(aff
))
1130 aff
= isl_aff_cow(aff
);
1134 aff
->v
= isl_vec_cow(aff
->v
);
1136 return isl_aff_free(aff
);
1138 pos
+= isl_local_space_offset(aff
->ls
, type
);
1139 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1144 /* Add "v" to the coefficient of the variable of type "type"
1145 * at position "pos" of "aff".
1147 * A NaN is unaffected by this operation.
1149 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1150 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1155 if (isl_val_is_zero(v
)) {
1160 if (type
== isl_dim_out
)
1161 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1162 "output/set dimension does not have a coefficient",
1164 if (type
== isl_dim_in
)
1167 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1168 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1169 "position out of bounds", goto error
);
1171 if (isl_aff_is_nan(aff
)) {
1175 if (!isl_val_is_rat(v
))
1176 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1177 "expecting rational value", goto error
);
1179 aff
= isl_aff_cow(aff
);
1183 aff
->v
= isl_vec_cow(aff
->v
);
1187 pos
+= isl_local_space_offset(aff
->ls
, type
);
1188 if (isl_int_is_one(v
->d
)) {
1189 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1190 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1191 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1192 aff
->v
= isl_vec_normalize(aff
->v
);
1196 isl_seq_scale(aff
->v
->el
+ 1,
1197 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1198 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1199 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1200 aff
->v
= isl_vec_normalize(aff
->v
);
1213 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1214 enum isl_dim_type type
, int pos
, int v
)
1219 isl_int_set_si(t
, v
);
1220 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1226 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1231 return isl_local_space_get_div(aff
->ls
, pos
);
1234 /* Return the negation of "aff".
1236 * As a special case, -NaN = NaN.
1238 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1242 if (isl_aff_is_nan(aff
))
1244 aff
= isl_aff_cow(aff
);
1247 aff
->v
= isl_vec_cow(aff
->v
);
1249 return isl_aff_free(aff
);
1251 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1256 /* Remove divs from the local space that do not appear in the affine
1258 * We currently only remove divs at the end.
1259 * Some intermediate divs may also not appear directly in the affine
1260 * expression, but we would also need to check that no other divs are
1261 * defined in terms of them.
1263 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1272 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1273 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1275 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1279 aff
= isl_aff_cow(aff
);
1283 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1284 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1285 if (!aff
->ls
|| !aff
->v
)
1286 return isl_aff_free(aff
);
1291 /* Given two affine expressions "p" of length p_len (including the
1292 * denominator and the constant term) and "subs" of length subs_len,
1293 * plug in "subs" for the variable at position "pos".
1294 * The variables of "subs" and "p" are assumed to match up to subs_len,
1295 * but "p" may have additional variables.
1296 * "v" is an initialized isl_int that can be used internally.
1298 * In particular, if "p" represents the expression
1302 * with i the variable at position "pos" and "subs" represents the expression
1306 * then the result represents the expression
1311 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1312 int p_len
, int subs_len
, isl_int v
)
1314 isl_int_set(v
, p
[1 + pos
]);
1315 isl_int_set_si(p
[1 + pos
], 0);
1316 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1317 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1318 isl_int_mul(p
[0], p
[0], subs
[0]);
1321 /* Look for any divs in the aff->ls with a denominator equal to one
1322 * and plug them into the affine expression and any subsequent divs
1323 * that may reference the div.
1325 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1331 isl_local_space
*ls
;
1337 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1339 for (i
= 0; i
< n
; ++i
) {
1340 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1342 ls
= isl_local_space_copy(aff
->ls
);
1343 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1344 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1345 vec
= isl_vec_copy(aff
->v
);
1346 vec
= isl_vec_cow(vec
);
1352 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1353 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1358 isl_vec_free(aff
->v
);
1360 isl_local_space_free(aff
->ls
);
1367 isl_local_space_free(ls
);
1368 return isl_aff_free(aff
);
1371 /* Look for any divs j that appear with a unit coefficient inside
1372 * the definitions of other divs i and plug them into the definitions
1375 * In particular, an expression of the form
1377 * floor((f(..) + floor(g(..)/n))/m)
1381 * floor((n * f(..) + g(..))/(n * m))
1383 * This simplification is correct because we can move the expression
1384 * f(..) into the inner floor in the original expression to obtain
1386 * floor(floor((n * f(..) + g(..))/n)/m)
1388 * from which we can derive the simplified expression.
1390 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1398 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1399 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1400 for (i
= 1; i
< n
; ++i
) {
1401 for (j
= 0; j
< i
; ++j
) {
1402 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1404 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1405 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1406 aff
->v
->size
, i
, 1);
1408 return isl_aff_free(aff
);
1415 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1417 * Even though this function is only called on isl_affs with a single
1418 * reference, we are careful to only change aff->v and aff->ls together.
1420 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1422 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1423 isl_local_space
*ls
;
1426 ls
= isl_local_space_copy(aff
->ls
);
1427 ls
= isl_local_space_swap_div(ls
, a
, b
);
1428 v
= isl_vec_copy(aff
->v
);
1433 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1434 isl_vec_free(aff
->v
);
1436 isl_local_space_free(aff
->ls
);
1442 isl_local_space_free(ls
);
1443 return isl_aff_free(aff
);
1446 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1448 * We currently do not actually remove div "b", but simply add its
1449 * coefficient to that of "a" and then zero it out.
1451 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1453 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1455 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1458 aff
->v
= isl_vec_cow(aff
->v
);
1460 return isl_aff_free(aff
);
1462 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1463 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1464 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1469 /* Sort the divs in the local space of "aff" according to
1470 * the comparison function "cmp_row" in isl_local_space.c,
1471 * combining the coefficients of identical divs.
1473 * Reordering divs does not change the semantics of "aff",
1474 * so there is no need to call isl_aff_cow.
1475 * Moreover, this function is currently only called on isl_affs
1476 * with a single reference.
1478 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1485 n
= isl_aff_dim(aff
, isl_dim_div
);
1486 for (i
= 1; i
< n
; ++i
) {
1487 for (j
= i
- 1; j
>= 0; --j
) {
1488 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1492 aff
= merge_divs(aff
, j
, j
+ 1);
1494 aff
= swap_div(aff
, j
, j
+ 1);
1503 /* Normalize the representation of "aff".
1505 * This function should only be called of "new" isl_affs, i.e.,
1506 * with only a single reference. We therefore do not need to
1507 * worry about affecting other instances.
1509 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1513 aff
->v
= isl_vec_normalize(aff
->v
);
1515 return isl_aff_free(aff
);
1516 aff
= plug_in_integral_divs(aff
);
1517 aff
= plug_in_unit_divs(aff
);
1518 aff
= sort_divs(aff
);
1519 aff
= isl_aff_remove_unused_divs(aff
);
1523 /* Given f, return floor(f).
1524 * If f is an integer expression, then just return f.
1525 * If f is a constant, then return the constant floor(f).
1526 * Otherwise, if f = g/m, write g = q m + r,
1527 * create a new div d = [r/m] and return the expression q + d.
1528 * The coefficients in r are taken to lie between -m/2 and m/2.
1530 * As a special case, floor(NaN) = NaN.
1532 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1542 if (isl_aff_is_nan(aff
))
1544 if (isl_int_is_one(aff
->v
->el
[0]))
1547 aff
= isl_aff_cow(aff
);
1551 aff
->v
= isl_vec_cow(aff
->v
);
1553 return isl_aff_free(aff
);
1555 if (isl_aff_is_cst(aff
)) {
1556 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1557 isl_int_set_si(aff
->v
->el
[0], 1);
1561 div
= isl_vec_copy(aff
->v
);
1562 div
= isl_vec_cow(div
);
1564 return isl_aff_free(aff
);
1566 ctx
= isl_aff_get_ctx(aff
);
1567 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1568 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1569 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1570 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1571 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1572 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1573 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1577 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1579 return isl_aff_free(aff
);
1581 size
= aff
->v
->size
;
1582 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1584 return isl_aff_free(aff
);
1585 isl_int_set_si(aff
->v
->el
[0], 1);
1586 isl_int_set_si(aff
->v
->el
[size
], 1);
1588 aff
= isl_aff_normalize(aff
);
1595 * aff mod m = aff - m * floor(aff/m)
1597 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1601 res
= isl_aff_copy(aff
);
1602 aff
= isl_aff_scale_down(aff
, m
);
1603 aff
= isl_aff_floor(aff
);
1604 aff
= isl_aff_scale(aff
, m
);
1605 res
= isl_aff_sub(res
, aff
);
1612 * aff mod m = aff - m * floor(aff/m)
1614 * with m an integer value.
1616 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1617 __isl_take isl_val
*m
)
1624 if (!isl_val_is_int(m
))
1625 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1626 "expecting integer modulo", goto error
);
1628 res
= isl_aff_copy(aff
);
1629 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1630 aff
= isl_aff_floor(aff
);
1631 aff
= isl_aff_scale_val(aff
, m
);
1632 res
= isl_aff_sub(res
, aff
);
1643 * pwaff mod m = pwaff - m * floor(pwaff/m)
1645 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1649 res
= isl_pw_aff_copy(pwaff
);
1650 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1651 pwaff
= isl_pw_aff_floor(pwaff
);
1652 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1653 res
= isl_pw_aff_sub(res
, pwaff
);
1660 * pa mod m = pa - m * floor(pa/m)
1662 * with m an integer value.
1664 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1665 __isl_take isl_val
*m
)
1669 if (!isl_val_is_int(m
))
1670 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1671 "expecting integer modulo", goto error
);
1672 pa
= isl_pw_aff_mod(pa
, m
->n
);
1676 isl_pw_aff_free(pa
);
1681 /* Given f, return ceil(f).
1682 * If f is an integer expression, then just return f.
1683 * Otherwise, let f be the expression
1689 * floor((e + m - 1)/m)
1691 * As a special case, ceil(NaN) = NaN.
1693 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1698 if (isl_aff_is_nan(aff
))
1700 if (isl_int_is_one(aff
->v
->el
[0]))
1703 aff
= isl_aff_cow(aff
);
1706 aff
->v
= isl_vec_cow(aff
->v
);
1708 return isl_aff_free(aff
);
1710 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1711 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1712 aff
= isl_aff_floor(aff
);
1717 /* Apply the expansion computed by isl_merge_divs.
1718 * The expansion itself is given by "exp" while the resulting
1719 * list of divs is given by "div".
1721 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1722 __isl_take isl_mat
*div
, int *exp
)
1729 aff
= isl_aff_cow(aff
);
1733 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1734 new_n_div
= isl_mat_rows(div
);
1735 if (new_n_div
< old_n_div
)
1736 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1737 "not an expansion", goto error
);
1739 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1743 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1745 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1746 if (j
>= 0 && exp
[j
] == i
) {
1748 isl_int_swap(aff
->v
->el
[offset
+ i
],
1749 aff
->v
->el
[offset
+ j
]);
1752 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1755 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1766 /* Add two affine expressions that live in the same local space.
1768 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1769 __isl_take isl_aff
*aff2
)
1773 aff1
= isl_aff_cow(aff1
);
1777 aff1
->v
= isl_vec_cow(aff1
->v
);
1783 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1784 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1785 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1786 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1787 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1788 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1789 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1801 /* Return the sum of "aff1" and "aff2".
1803 * If either of the two is NaN, then the result is NaN.
1805 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1806 __isl_take isl_aff
*aff2
)
1817 ctx
= isl_aff_get_ctx(aff1
);
1818 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1819 isl_die(ctx
, isl_error_invalid
,
1820 "spaces don't match", goto error
);
1822 if (isl_aff_is_nan(aff1
)) {
1826 if (isl_aff_is_nan(aff2
)) {
1831 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1832 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1833 if (n_div1
== 0 && n_div2
== 0)
1834 return add_expanded(aff1
, aff2
);
1836 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1837 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1838 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1841 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1842 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1843 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1847 return add_expanded(aff1
, aff2
);
1856 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1857 __isl_take isl_aff
*aff2
)
1859 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1862 /* Return the result of scaling "aff" by a factor of "f".
1864 * As a special case, f * NaN = NaN.
1866 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1872 if (isl_aff_is_nan(aff
))
1875 if (isl_int_is_one(f
))
1878 aff
= isl_aff_cow(aff
);
1881 aff
->v
= isl_vec_cow(aff
->v
);
1883 return isl_aff_free(aff
);
1885 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1886 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1891 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1892 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1893 isl_int_divexact(gcd
, f
, gcd
);
1894 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1900 /* Multiple "aff" by "v".
1902 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1903 __isl_take isl_val
*v
)
1908 if (isl_val_is_one(v
)) {
1913 if (!isl_val_is_rat(v
))
1914 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1915 "expecting rational factor", goto error
);
1917 aff
= isl_aff_scale(aff
, v
->n
);
1918 aff
= isl_aff_scale_down(aff
, v
->d
);
1928 /* Return the result of scaling "aff" down by a factor of "f".
1930 * As a special case, NaN/f = NaN.
1932 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1938 if (isl_aff_is_nan(aff
))
1941 if (isl_int_is_one(f
))
1944 aff
= isl_aff_cow(aff
);
1948 if (isl_int_is_zero(f
))
1949 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1950 "cannot scale down by zero", return isl_aff_free(aff
));
1952 aff
->v
= isl_vec_cow(aff
->v
);
1954 return isl_aff_free(aff
);
1957 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1958 isl_int_gcd(gcd
, gcd
, f
);
1959 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1960 isl_int_divexact(gcd
, f
, gcd
);
1961 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1967 /* Divide "aff" by "v".
1969 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1970 __isl_take isl_val
*v
)
1975 if (isl_val_is_one(v
)) {
1980 if (!isl_val_is_rat(v
))
1981 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1982 "expecting rational factor", goto error
);
1983 if (!isl_val_is_pos(v
))
1984 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1985 "factor needs to be positive", goto error
);
1987 aff
= isl_aff_scale(aff
, v
->d
);
1988 aff
= isl_aff_scale_down(aff
, v
->n
);
1998 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
2006 isl_int_set_ui(v
, f
);
2007 aff
= isl_aff_scale_down(aff
, v
);
2013 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
2014 enum isl_dim_type type
, unsigned pos
, const char *s
)
2016 aff
= isl_aff_cow(aff
);
2019 if (type
== isl_dim_out
)
2020 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2021 "cannot set name of output/set dimension",
2022 return isl_aff_free(aff
));
2023 if (type
== isl_dim_in
)
2025 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2027 return isl_aff_free(aff
);
2032 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2033 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2035 aff
= isl_aff_cow(aff
);
2038 if (type
== isl_dim_out
)
2039 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2040 "cannot set name of output/set dimension",
2042 if (type
== isl_dim_in
)
2044 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2046 return isl_aff_free(aff
);
2055 /* Replace the identifier of the input tuple of "aff" by "id".
2056 * type is currently required to be equal to isl_dim_in
2058 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2059 enum isl_dim_type type
, __isl_take isl_id
*id
)
2061 aff
= isl_aff_cow(aff
);
2064 if (type
!= isl_dim_out
)
2065 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2066 "cannot only set id of input tuple", goto error
);
2067 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2069 return isl_aff_free(aff
);
2078 /* Exploit the equalities in "eq" to simplify the affine expression
2079 * and the expressions of the integer divisions in the local space.
2080 * The integer divisions in this local space are assumed to appear
2081 * as regular dimensions in "eq".
2083 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2084 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2092 if (eq
->n_eq
== 0) {
2093 isl_basic_set_free(eq
);
2097 aff
= isl_aff_cow(aff
);
2101 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2102 isl_basic_set_copy(eq
));
2103 aff
->v
= isl_vec_cow(aff
->v
);
2104 if (!aff
->ls
|| !aff
->v
)
2107 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2109 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2110 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2111 if (j
< 0 || j
== 0 || j
>= total
)
2114 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2118 isl_basic_set_free(eq
);
2119 aff
= isl_aff_normalize(aff
);
2122 isl_basic_set_free(eq
);
2127 /* Exploit the equalities in "eq" to simplify the affine expression
2128 * and the expressions of the integer divisions in the local space.
2130 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2131 __isl_take isl_basic_set
*eq
)
2137 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2139 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2140 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2142 isl_basic_set_free(eq
);
2147 /* Look for equalities among the variables shared by context and aff
2148 * and the integer divisions of aff, if any.
2149 * The equalities are then used to eliminate coefficients and/or integer
2150 * divisions from aff.
2152 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2153 __isl_take isl_set
*context
)
2155 isl_basic_set
*hull
;
2160 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2162 isl_basic_set
*bset
;
2163 isl_local_space
*ls
;
2164 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2165 ls
= isl_aff_get_domain_local_space(aff
);
2166 bset
= isl_basic_set_from_local_space(ls
);
2167 bset
= isl_basic_set_lift(bset
);
2168 bset
= isl_basic_set_flatten(bset
);
2169 context
= isl_set_intersect(context
,
2170 isl_set_from_basic_set(bset
));
2173 hull
= isl_set_affine_hull(context
);
2174 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2177 isl_set_free(context
);
2181 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2182 __isl_take isl_set
*context
)
2184 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2185 dom_context
= isl_set_intersect_params(dom_context
, context
);
2186 return isl_aff_gist(aff
, dom_context
);
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is positive. "rational" should not be set.
2192 * If "aff" is NaN, then it is not positive.
2194 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2197 isl_constraint
*ineq
;
2198 isl_basic_set
*bset
;
2203 if (isl_aff_is_nan(aff
)) {
2204 isl_space
*space
= isl_aff_get_domain_space(aff
);
2206 return isl_basic_set_empty(space
);
2209 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2210 "rational sets not supported", goto error
);
2212 ineq
= isl_inequality_from_aff(aff
);
2213 c
= isl_constraint_get_constant_val(ineq
);
2214 c
= isl_val_sub_ui(c
, 1);
2215 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2217 bset
= isl_basic_set_from_constraint(ineq
);
2218 bset
= isl_basic_set_simplify(bset
);
2225 /* Return a basic set containing those elements in the space
2226 * of aff where it is non-negative.
2227 * If "rational" is set, then return a rational basic set.
2229 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2231 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2232 __isl_take isl_aff
*aff
, int rational
)
2234 isl_constraint
*ineq
;
2235 isl_basic_set
*bset
;
2239 if (isl_aff_is_nan(aff
)) {
2240 isl_space
*space
= isl_aff_get_domain_space(aff
);
2242 return isl_basic_set_empty(space
);
2245 ineq
= isl_inequality_from_aff(aff
);
2247 bset
= isl_basic_set_from_constraint(ineq
);
2249 bset
= isl_basic_set_set_rational(bset
);
2250 bset
= isl_basic_set_simplify(bset
);
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is non-negative.
2257 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2259 return aff_nonneg_basic_set(aff
, 0);
2262 /* Return a basic set containing those elements in the domain space
2263 * of aff where it is negative.
2265 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2267 aff
= isl_aff_neg(aff
);
2268 aff
= isl_aff_add_constant_num_si(aff
, -1);
2269 return isl_aff_nonneg_basic_set(aff
);
2272 /* Return a basic set containing those elements in the space
2273 * of aff where it is zero.
2274 * If "rational" is set, then return a rational basic set.
2276 * If "aff" is NaN, then it is not zero.
2278 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2281 isl_constraint
*ineq
;
2282 isl_basic_set
*bset
;
2286 if (isl_aff_is_nan(aff
)) {
2287 isl_space
*space
= isl_aff_get_domain_space(aff
);
2289 return isl_basic_set_empty(space
);
2292 ineq
= isl_equality_from_aff(aff
);
2294 bset
= isl_basic_set_from_constraint(ineq
);
2296 bset
= isl_basic_set_set_rational(bset
);
2297 bset
= isl_basic_set_simplify(bset
);
2301 /* Return a basic set containing those elements in the space
2302 * of aff where it is zero.
2304 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2306 return aff_zero_basic_set(aff
, 0);
2309 /* Return a basic set containing those elements in the shared space
2310 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2312 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2313 __isl_take isl_aff
*aff2
)
2315 aff1
= isl_aff_sub(aff1
, aff2
);
2317 return isl_aff_nonneg_basic_set(aff1
);
2320 /* Return a set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2323 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2324 __isl_take isl_aff
*aff2
)
2326 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2329 /* Return a basic set containing those elements in the shared space
2330 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2332 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2333 __isl_take isl_aff
*aff2
)
2335 return isl_aff_ge_basic_set(aff2
, aff1
);
2338 /* Return a set containing those elements in the shared space
2339 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2341 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2342 __isl_take isl_aff
*aff2
)
2344 return isl_aff_ge_set(aff2
, aff1
);
2347 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2348 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2350 aff1
= isl_aff_add(aff1
, aff2
);
2351 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2355 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2363 /* Check whether the given affine expression has non-zero coefficient
2364 * for any dimension in the given range or if any of these dimensions
2365 * appear with non-zero coefficients in any of the integer divisions
2366 * involved in the affine expression.
2368 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2369 enum isl_dim_type type
, unsigned first
, unsigned n
)
2374 isl_bool involves
= isl_bool_false
;
2377 return isl_bool_error
;
2379 return isl_bool_false
;
2381 ctx
= isl_aff_get_ctx(aff
);
2382 if (first
+ n
> isl_aff_dim(aff
, type
))
2383 isl_die(ctx
, isl_error_invalid
,
2384 "range out of bounds", return isl_bool_error
);
2386 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2390 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2391 for (i
= 0; i
< n
; ++i
)
2392 if (active
[first
+ i
]) {
2393 involves
= isl_bool_true
;
2402 return isl_bool_error
;
2405 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2406 enum isl_dim_type type
, unsigned first
, unsigned n
)
2412 if (type
== isl_dim_out
)
2413 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2414 "cannot drop output/set dimension",
2415 return isl_aff_free(aff
));
2416 if (type
== isl_dim_in
)
2418 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2421 ctx
= isl_aff_get_ctx(aff
);
2422 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2423 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2424 return isl_aff_free(aff
));
2426 aff
= isl_aff_cow(aff
);
2430 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2432 return isl_aff_free(aff
);
2434 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2435 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2437 return isl_aff_free(aff
);
2442 /* Project the domain of the affine expression onto its parameter space.
2443 * The affine expression may not involve any of the domain dimensions.
2445 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2451 n
= isl_aff_dim(aff
, isl_dim_in
);
2452 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2454 return isl_aff_free(aff
);
2456 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2457 "affine expression involves some of the domain dimensions",
2458 return isl_aff_free(aff
));
2459 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2460 space
= isl_aff_get_domain_space(aff
);
2461 space
= isl_space_params(space
);
2462 aff
= isl_aff_reset_domain_space(aff
, space
);
2466 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2467 enum isl_dim_type type
, unsigned first
, unsigned n
)
2473 if (type
== isl_dim_out
)
2474 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2475 "cannot insert output/set dimensions",
2476 return isl_aff_free(aff
));
2477 if (type
== isl_dim_in
)
2479 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2482 ctx
= isl_aff_get_ctx(aff
);
2483 if (first
> isl_local_space_dim(aff
->ls
, type
))
2484 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2485 return isl_aff_free(aff
));
2487 aff
= isl_aff_cow(aff
);
2491 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2493 return isl_aff_free(aff
);
2495 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2496 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2498 return isl_aff_free(aff
);
2503 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2504 enum isl_dim_type type
, unsigned n
)
2508 pos
= isl_aff_dim(aff
, type
);
2510 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2513 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2514 enum isl_dim_type type
, unsigned n
)
2518 pos
= isl_pw_aff_dim(pwaff
, type
);
2520 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2523 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2524 * to dimensions of "dst_type" at "dst_pos".
2526 * We only support moving input dimensions to parameters and vice versa.
2528 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2529 enum isl_dim_type dst_type
, unsigned dst_pos
,
2530 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2538 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2539 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2542 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2543 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2544 "cannot move output/set dimension",
2545 return isl_aff_free(aff
));
2546 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2547 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2548 "cannot move divs", return isl_aff_free(aff
));
2549 if (dst_type
== isl_dim_in
)
2550 dst_type
= isl_dim_set
;
2551 if (src_type
== isl_dim_in
)
2552 src_type
= isl_dim_set
;
2554 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2555 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2556 "range out of bounds", return isl_aff_free(aff
));
2557 if (dst_type
== src_type
)
2558 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2559 "moving dims within the same type not supported",
2560 return isl_aff_free(aff
));
2562 aff
= isl_aff_cow(aff
);
2566 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2567 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2568 if (dst_type
> src_type
)
2571 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2572 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2573 src_type
, src_pos
, n
);
2574 if (!aff
->v
|| !aff
->ls
)
2575 return isl_aff_free(aff
);
2577 aff
= sort_divs(aff
);
2582 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2584 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2585 return isl_pw_aff_alloc(dom
, aff
);
2589 #define PW isl_pw_aff
2593 #define EL_IS_ZERO is_empty
2597 #define IS_ZERO is_empty
2600 #undef DEFAULT_IS_ZERO
2601 #define DEFAULT_IS_ZERO 0
2608 #include <isl_pw_templ.c>
2609 #include <isl_pw_hash.c>
2610 #include <isl_pw_union_opt.c>
2613 #define UNION isl_union_pw_aff
2615 #define PART isl_pw_aff
2617 #define PARTS pw_aff
2619 #include <isl_union_single.c>
2620 #include <isl_union_neg.c>
2622 static __isl_give isl_set
*align_params_pw_pw_set_and(
2623 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2624 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2625 __isl_take isl_pw_aff
*pwaff2
))
2627 if (!pwaff1
|| !pwaff2
)
2629 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2630 pwaff2
->dim
, isl_dim_param
))
2631 return fn(pwaff1
, pwaff2
);
2632 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2633 !isl_space_has_named_params(pwaff2
->dim
))
2634 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2635 "unaligned unnamed parameters", goto error
);
2636 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2637 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2638 return fn(pwaff1
, pwaff2
);
2640 isl_pw_aff_free(pwaff1
);
2641 isl_pw_aff_free(pwaff2
);
2645 /* Align the parameters of the to isl_pw_aff arguments and
2646 * then apply a function "fn" on them that returns an isl_map.
2648 static __isl_give isl_map
*align_params_pw_pw_map_and(
2649 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2650 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2651 __isl_take isl_pw_aff
*pa2
))
2655 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2656 return fn(pa1
, pa2
);
2657 if (!isl_space_has_named_params(pa1
->dim
) ||
2658 !isl_space_has_named_params(pa2
->dim
))
2659 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2660 "unaligned unnamed parameters", goto error
);
2661 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2662 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2663 return fn(pa1
, pa2
);
2665 isl_pw_aff_free(pa1
);
2666 isl_pw_aff_free(pa2
);
2670 /* Compute a piecewise quasi-affine expression with a domain that
2671 * is the union of those of pwaff1 and pwaff2 and such that on each
2672 * cell, the quasi-affine expression is the maximum of those of pwaff1
2673 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2674 * cell, then the associated expression is the defined one.
2676 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2677 __isl_take isl_pw_aff
*pwaff2
)
2679 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2682 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2683 __isl_take isl_pw_aff
*pwaff2
)
2685 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2689 /* Compute a piecewise quasi-affine expression with a domain that
2690 * is the union of those of pwaff1 and pwaff2 and such that on each
2691 * cell, the quasi-affine expression is the minimum of those of pwaff1
2692 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2693 * cell, then the associated expression is the defined one.
2695 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2696 __isl_take isl_pw_aff
*pwaff2
)
2698 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2701 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2702 __isl_take isl_pw_aff
*pwaff2
)
2704 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2708 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2709 __isl_take isl_pw_aff
*pwaff2
, int max
)
2712 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2714 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2717 /* Construct a map with as domain the domain of pwaff and
2718 * one-dimensional range corresponding to the affine expressions.
2720 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2729 dim
= isl_pw_aff_get_space(pwaff
);
2730 map
= isl_map_empty(dim
);
2732 for (i
= 0; i
< pwaff
->n
; ++i
) {
2733 isl_basic_map
*bmap
;
2736 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2737 map_i
= isl_map_from_basic_map(bmap
);
2738 map_i
= isl_map_intersect_domain(map_i
,
2739 isl_set_copy(pwaff
->p
[i
].set
));
2740 map
= isl_map_union_disjoint(map
, map_i
);
2743 isl_pw_aff_free(pwaff
);
2748 /* Construct a map with as domain the domain of pwaff and
2749 * one-dimensional range corresponding to the affine expressions.
2751 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2755 if (isl_space_is_set(pwaff
->dim
))
2756 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2757 "space of input is not a map", goto error
);
2758 return map_from_pw_aff(pwaff
);
2760 isl_pw_aff_free(pwaff
);
2764 /* Construct a one-dimensional set with as parameter domain
2765 * the domain of pwaff and the single set dimension
2766 * corresponding to the affine expressions.
2768 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2772 if (!isl_space_is_set(pwaff
->dim
))
2773 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2774 "space of input is not a set", goto error
);
2775 return map_from_pw_aff(pwaff
);
2777 isl_pw_aff_free(pwaff
);
2781 /* Return a set containing those elements in the domain
2782 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2783 * does not satisfy "fn" (if complement is 1).
2785 * The pieces with a NaN never belong to the result since
2786 * NaN does not satisfy any property.
2788 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2789 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2798 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2800 for (i
= 0; i
< pwaff
->n
; ++i
) {
2801 isl_basic_set
*bset
;
2802 isl_set
*set_i
, *locus
;
2805 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2808 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2809 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2810 locus
= isl_set_from_basic_set(bset
);
2811 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2813 set_i
= isl_set_subtract(set_i
, locus
);
2815 set_i
= isl_set_intersect(set_i
, locus
);
2816 set
= isl_set_union_disjoint(set
, set_i
);
2819 isl_pw_aff_free(pwaff
);
2824 /* Return a set containing those elements in the domain
2825 * of "pa" where it is positive.
2827 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2829 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2832 /* Return a set containing those elements in the domain
2833 * of pwaff where it is non-negative.
2835 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2837 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2840 /* Return a set containing those elements in the domain
2841 * of pwaff where it is zero.
2843 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2845 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2848 /* Return a set containing those elements in the domain
2849 * of pwaff where it is not zero.
2851 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2853 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2856 /* Return a set containing those elements in the shared domain
2857 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2859 * We compute the difference on the shared domain and then construct
2860 * the set of values where this difference is non-negative.
2861 * If strict is set, we first subtract 1 from the difference.
2862 * If equal is set, we only return the elements where pwaff1 and pwaff2
2865 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2866 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2868 isl_set
*set1
, *set2
;
2870 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2871 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2872 set1
= isl_set_intersect(set1
, set2
);
2873 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2874 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2875 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2878 isl_space
*dim
= isl_set_get_space(set1
);
2880 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2881 aff
= isl_aff_add_constant_si(aff
, -1);
2882 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2887 return isl_pw_aff_zero_set(pwaff1
);
2888 return isl_pw_aff_nonneg_set(pwaff1
);
2891 /* Return a set containing those elements in the shared domain
2892 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2894 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2895 __isl_take isl_pw_aff
*pwaff2
)
2897 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2900 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2901 __isl_take isl_pw_aff
*pwaff2
)
2903 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2906 /* Return a set containing those elements in the shared domain
2907 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2909 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2910 __isl_take isl_pw_aff
*pwaff2
)
2912 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2915 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2916 __isl_take isl_pw_aff
*pwaff2
)
2918 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2921 /* Return a set containing those elements in the shared domain
2922 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2924 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2925 __isl_take isl_pw_aff
*pwaff2
)
2927 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2930 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2931 __isl_take isl_pw_aff
*pwaff2
)
2933 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2936 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2937 __isl_take isl_pw_aff
*pwaff2
)
2939 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2942 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2943 __isl_take isl_pw_aff
*pwaff2
)
2945 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2948 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2949 * where the function values are ordered in the same way as "order",
2950 * which returns a set in the shared domain of its two arguments.
2951 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2953 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2954 * We first pull back the two functions such that they are defined on
2955 * the domain [A -> B]. Then we apply "order", resulting in a set
2956 * in the space [A -> B]. Finally, we unwrap this set to obtain
2957 * a map in the space A -> B.
2959 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2960 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2961 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2962 __isl_take isl_pw_aff
*pa2
))
2964 isl_space
*space1
, *space2
;
2968 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2969 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2970 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2971 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2972 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2973 ma
= isl_multi_aff_range_map(space1
);
2974 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2975 set
= order(pa1
, pa2
);
2977 return isl_set_unwrap(set
);
2980 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2981 * where the function values are equal.
2982 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2984 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2985 __isl_take isl_pw_aff
*pa2
)
2987 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2990 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2991 * where the function values are equal.
2993 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2994 __isl_take isl_pw_aff
*pa2
)
2996 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
2999 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3000 * where the function value of "pa1" is less than the function value of "pa2".
3001 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3003 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3004 __isl_take isl_pw_aff
*pa2
)
3006 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3009 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3010 * where the function value of "pa1" is less than the function value of "pa2".
3012 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3013 __isl_take isl_pw_aff
*pa2
)
3015 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3018 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3019 * where the function value of "pa1" is greater than the function value
3021 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3023 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3024 __isl_take isl_pw_aff
*pa2
)
3026 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is greater than the function value
3033 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3034 __isl_take isl_pw_aff
*pa2
)
3036 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3039 /* Return a set containing those elements in the shared domain
3040 * of the elements of list1 and list2 where each element in list1
3041 * has the relation specified by "fn" with each element in list2.
3043 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3044 __isl_take isl_pw_aff_list
*list2
,
3045 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3046 __isl_take isl_pw_aff
*pwaff2
))
3052 if (!list1
|| !list2
)
3055 ctx
= isl_pw_aff_list_get_ctx(list1
);
3056 if (list1
->n
< 1 || list2
->n
< 1)
3057 isl_die(ctx
, isl_error_invalid
,
3058 "list should contain at least one element", goto error
);
3060 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3061 for (i
= 0; i
< list1
->n
; ++i
)
3062 for (j
= 0; j
< list2
->n
; ++j
) {
3065 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3066 isl_pw_aff_copy(list2
->p
[j
]));
3067 set
= isl_set_intersect(set
, set_ij
);
3070 isl_pw_aff_list_free(list1
);
3071 isl_pw_aff_list_free(list2
);
3074 isl_pw_aff_list_free(list1
);
3075 isl_pw_aff_list_free(list2
);
3079 /* Return a set containing those elements in the shared domain
3080 * of the elements of list1 and list2 where each element in list1
3081 * is equal to each element in list2.
3083 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3084 __isl_take isl_pw_aff_list
*list2
)
3086 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3089 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3090 __isl_take isl_pw_aff_list
*list2
)
3092 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3095 /* Return a set containing those elements in the shared domain
3096 * of the elements of list1 and list2 where each element in list1
3097 * is less than or equal to each element in list2.
3099 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3100 __isl_take isl_pw_aff_list
*list2
)
3102 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3105 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3106 __isl_take isl_pw_aff_list
*list2
)
3108 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3111 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3112 __isl_take isl_pw_aff_list
*list2
)
3114 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3117 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3118 __isl_take isl_pw_aff_list
*list2
)
3120 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3124 /* Return a set containing those elements in the shared domain
3125 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3127 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3128 __isl_take isl_pw_aff
*pwaff2
)
3130 isl_set
*set_lt
, *set_gt
;
3132 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3133 isl_pw_aff_copy(pwaff2
));
3134 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3135 return isl_set_union_disjoint(set_lt
, set_gt
);
3138 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3139 __isl_take isl_pw_aff
*pwaff2
)
3141 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3144 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3149 if (isl_int_is_one(v
))
3151 if (!isl_int_is_pos(v
))
3152 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3153 "factor needs to be positive",
3154 return isl_pw_aff_free(pwaff
));
3155 pwaff
= isl_pw_aff_cow(pwaff
);
3161 for (i
= 0; i
< pwaff
->n
; ++i
) {
3162 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3163 if (!pwaff
->p
[i
].aff
)
3164 return isl_pw_aff_free(pwaff
);
3170 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3174 pwaff
= isl_pw_aff_cow(pwaff
);
3180 for (i
= 0; i
< pwaff
->n
; ++i
) {
3181 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3182 if (!pwaff
->p
[i
].aff
)
3183 return isl_pw_aff_free(pwaff
);
3189 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3193 pwaff
= isl_pw_aff_cow(pwaff
);
3199 for (i
= 0; i
< pwaff
->n
; ++i
) {
3200 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3201 if (!pwaff
->p
[i
].aff
)
3202 return isl_pw_aff_free(pwaff
);
3208 /* Assuming that "cond1" and "cond2" are disjoint,
3209 * return an affine expression that is equal to pwaff1 on cond1
3210 * and to pwaff2 on cond2.
3212 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3213 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3214 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3216 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3217 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3219 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3222 /* Return an affine expression that is equal to pwaff_true for elements
3223 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3225 * That is, return cond ? pwaff_true : pwaff_false;
3227 * If "cond" involves and NaN, then we conservatively return a NaN
3228 * on its entire domain. In principle, we could consider the pieces
3229 * where it is NaN separately from those where it is not.
3231 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3232 * then only use the domain of "cond" to restrict the domain.
3234 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3235 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3237 isl_set
*cond_true
, *cond_false
;
3242 if (isl_pw_aff_involves_nan(cond
)) {
3243 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3244 isl_local_space
*ls
= isl_local_space_from_space(space
);
3245 isl_pw_aff_free(cond
);
3246 isl_pw_aff_free(pwaff_true
);
3247 isl_pw_aff_free(pwaff_false
);
3248 return isl_pw_aff_nan_on_domain(ls
);
3251 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3252 isl_pw_aff_get_space(pwaff_false
));
3253 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3254 isl_pw_aff_get_space(pwaff_true
));
3255 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3261 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3262 isl_pw_aff_free(pwaff_false
);
3263 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3266 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3267 cond_false
= isl_pw_aff_zero_set(cond
);
3268 return isl_pw_aff_select(cond_true
, pwaff_true
,
3269 cond_false
, pwaff_false
);
3271 isl_pw_aff_free(cond
);
3272 isl_pw_aff_free(pwaff_true
);
3273 isl_pw_aff_free(pwaff_false
);
3277 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3280 return isl_bool_error
;
3282 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3285 /* Check whether pwaff is a piecewise constant.
3287 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3292 return isl_bool_error
;
3294 for (i
= 0; i
< pwaff
->n
; ++i
) {
3295 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3296 if (is_cst
< 0 || !is_cst
)
3300 return isl_bool_true
;
3303 /* Are all elements of "mpa" piecewise constants?
3305 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3310 return isl_bool_error
;
3312 for (i
= 0; i
< mpa
->n
; ++i
) {
3313 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3314 if (is_cst
< 0 || !is_cst
)
3318 return isl_bool_true
;
3321 /* Return the product of "aff1" and "aff2".
3323 * If either of the two is NaN, then the result is NaN.
3325 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3327 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3328 __isl_take isl_aff
*aff2
)
3333 if (isl_aff_is_nan(aff1
)) {
3337 if (isl_aff_is_nan(aff2
)) {
3342 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3343 return isl_aff_mul(aff2
, aff1
);
3345 if (!isl_aff_is_cst(aff2
))
3346 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3347 "at least one affine expression should be constant",
3350 aff1
= isl_aff_cow(aff1
);
3354 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3355 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3365 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3367 * If either of the two is NaN, then the result is NaN.
3369 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3370 __isl_take isl_aff
*aff2
)
3378 if (isl_aff_is_nan(aff1
)) {
3382 if (isl_aff_is_nan(aff2
)) {
3387 is_cst
= isl_aff_is_cst(aff2
);
3391 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3392 "second argument should be a constant", goto error
);
3397 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3399 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3400 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3403 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3404 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3407 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3408 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3419 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3420 __isl_take isl_pw_aff
*pwaff2
)
3422 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3425 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3426 __isl_take isl_pw_aff
*pwaff2
)
3428 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3431 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3432 __isl_take isl_pw_aff
*pwaff2
)
3434 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3437 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3438 __isl_take isl_pw_aff
*pwaff2
)
3440 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3443 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3444 __isl_take isl_pw_aff
*pwaff2
)
3446 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3449 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3450 __isl_take isl_pw_aff
*pa2
)
3452 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3455 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3457 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3458 __isl_take isl_pw_aff
*pa2
)
3462 is_cst
= isl_pw_aff_is_cst(pa2
);
3466 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3467 "second argument should be a piecewise constant",
3469 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3471 isl_pw_aff_free(pa1
);
3472 isl_pw_aff_free(pa2
);
3476 /* Compute the quotient of the integer division of "pa1" by "pa2"
3477 * with rounding towards zero.
3478 * "pa2" is assumed to be a piecewise constant.
3480 * In particular, return
3482 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3485 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3486 __isl_take isl_pw_aff
*pa2
)
3492 is_cst
= isl_pw_aff_is_cst(pa2
);
3496 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3497 "second argument should be a piecewise constant",
3500 pa1
= isl_pw_aff_div(pa1
, pa2
);
3502 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3503 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3504 c
= isl_pw_aff_ceil(pa1
);
3505 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3507 isl_pw_aff_free(pa1
);
3508 isl_pw_aff_free(pa2
);
3512 /* Compute the remainder of the integer division of "pa1" by "pa2"
3513 * with rounding towards zero.
3514 * "pa2" is assumed to be a piecewise constant.
3516 * In particular, return
3518 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3521 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3522 __isl_take isl_pw_aff
*pa2
)
3527 is_cst
= isl_pw_aff_is_cst(pa2
);
3531 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3532 "second argument should be a piecewise constant",
3534 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3535 res
= isl_pw_aff_mul(pa2
, res
);
3536 res
= isl_pw_aff_sub(pa1
, res
);
3539 isl_pw_aff_free(pa1
);
3540 isl_pw_aff_free(pa2
);
3544 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3545 __isl_take isl_pw_aff
*pwaff2
)
3550 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3551 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3552 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3553 isl_pw_aff_copy(pwaff2
));
3554 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3555 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3558 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3559 __isl_take isl_pw_aff
*pwaff2
)
3561 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3564 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3565 __isl_take isl_pw_aff
*pwaff2
)
3570 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3571 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3572 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3573 isl_pw_aff_copy(pwaff2
));
3574 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3575 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3578 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3579 __isl_take isl_pw_aff
*pwaff2
)
3581 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3584 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3585 __isl_take isl_pw_aff_list
*list
,
3586 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3587 __isl_take isl_pw_aff
*pwaff2
))
3596 ctx
= isl_pw_aff_list_get_ctx(list
);
3598 isl_die(ctx
, isl_error_invalid
,
3599 "list should contain at least one element", goto error
);
3601 res
= isl_pw_aff_copy(list
->p
[0]);
3602 for (i
= 1; i
< list
->n
; ++i
)
3603 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3605 isl_pw_aff_list_free(list
);
3608 isl_pw_aff_list_free(list
);
3612 /* Return an isl_pw_aff that maps each element in the intersection of the
3613 * domains of the elements of list to the minimal corresponding affine
3616 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3618 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the maximal corresponding affine
3625 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3627 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3630 /* Mark the domains of "pwaff" as rational.
3632 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3636 pwaff
= isl_pw_aff_cow(pwaff
);
3642 for (i
= 0; i
< pwaff
->n
; ++i
) {
3643 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3644 if (!pwaff
->p
[i
].set
)
3645 return isl_pw_aff_free(pwaff
);
3651 /* Mark the domains of the elements of "list" as rational.
3653 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3654 __isl_take isl_pw_aff_list
*list
)
3664 for (i
= 0; i
< n
; ++i
) {
3667 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3668 pa
= isl_pw_aff_set_rational(pa
);
3669 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3675 /* Do the parameters of "aff" match those of "space"?
3677 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3678 __isl_keep isl_space
*space
)
3680 isl_space
*aff_space
;
3686 aff_space
= isl_aff_get_domain_space(aff
);
3688 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3690 isl_space_free(aff_space
);
3694 /* Check that the domain space of "aff" matches "space".
3696 * Return 0 on success and -1 on error.
3698 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3699 __isl_keep isl_space
*space
)
3701 isl_space
*aff_space
;
3707 aff_space
= isl_aff_get_domain_space(aff
);
3709 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3713 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3714 "parameters don't match", goto error
);
3715 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3716 aff_space
, isl_dim_set
);
3720 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3721 "domains don't match", goto error
);
3722 isl_space_free(aff_space
);
3725 isl_space_free(aff_space
);
3735 #include <isl_multi_templ.c>
3736 #include <isl_multi_apply_set.c>
3737 #include <isl_multi_cmp.c>
3738 #include <isl_multi_floor.c>
3739 #include <isl_multi_gist.c>
3743 /* Remove any internal structure of the domain of "ma".
3744 * If there is any such internal structure in the input,
3745 * then the name of the corresponding space is also removed.
3747 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3748 __isl_take isl_multi_aff
*ma
)
3755 if (!ma
->space
->nested
[0])
3758 space
= isl_multi_aff_get_space(ma
);
3759 space
= isl_space_flatten_domain(space
);
3760 ma
= isl_multi_aff_reset_space(ma
, space
);
3765 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3766 * of the space to its domain.
3768 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3771 isl_local_space
*ls
;
3776 if (!isl_space_is_map(space
))
3777 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3778 "not a map space", goto error
);
3780 n_in
= isl_space_dim(space
, isl_dim_in
);
3781 space
= isl_space_domain_map(space
);
3783 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3785 isl_space_free(space
);
3789 space
= isl_space_domain(space
);
3790 ls
= isl_local_space_from_space(space
);
3791 for (i
= 0; i
< n_in
; ++i
) {
3794 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3796 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3798 isl_local_space_free(ls
);
3801 isl_space_free(space
);
3805 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3806 * of the space to its range.
3808 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3811 isl_local_space
*ls
;
3816 if (!isl_space_is_map(space
))
3817 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3818 "not a map space", goto error
);
3820 n_in
= isl_space_dim(space
, isl_dim_in
);
3821 n_out
= isl_space_dim(space
, isl_dim_out
);
3822 space
= isl_space_range_map(space
);
3824 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3826 isl_space_free(space
);
3830 space
= isl_space_domain(space
);
3831 ls
= isl_local_space_from_space(space
);
3832 for (i
= 0; i
< n_out
; ++i
) {
3835 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3836 isl_dim_set
, n_in
+ i
);
3837 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3839 isl_local_space_free(ls
);
3842 isl_space_free(space
);
3846 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3847 * of the space to its range.
3849 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3850 __isl_take isl_space
*space
)
3852 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3855 /* Given the space of a set and a range of set dimensions,
3856 * construct an isl_multi_aff that projects out those dimensions.
3858 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3859 __isl_take isl_space
*space
, enum isl_dim_type type
,
3860 unsigned first
, unsigned n
)
3863 isl_local_space
*ls
;
3868 if (!isl_space_is_set(space
))
3869 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3870 "expecting set space", goto error
);
3871 if (type
!= isl_dim_set
)
3872 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3873 "only set dimensions can be projected out", goto error
);
3875 dim
= isl_space_dim(space
, isl_dim_set
);
3876 if (first
+ n
> dim
)
3877 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3878 "range out of bounds", goto error
);
3880 space
= isl_space_from_domain(space
);
3881 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3884 return isl_multi_aff_alloc(space
);
3886 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3887 space
= isl_space_domain(space
);
3888 ls
= isl_local_space_from_space(space
);
3890 for (i
= 0; i
< first
; ++i
) {
3893 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3895 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3898 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3901 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3902 isl_dim_set
, first
+ n
+ i
);
3903 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3906 isl_local_space_free(ls
);
3909 isl_space_free(space
);
3913 /* Given the space of a set and a range of set dimensions,
3914 * construct an isl_pw_multi_aff that projects out those dimensions.
3916 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3917 __isl_take isl_space
*space
, enum isl_dim_type type
,
3918 unsigned first
, unsigned n
)
3922 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3923 return isl_pw_multi_aff_from_multi_aff(ma
);
3926 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3929 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3930 __isl_take isl_multi_aff
*ma
)
3932 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3933 return isl_pw_multi_aff_alloc(dom
, ma
);
3936 /* Create a piecewise multi-affine expression in the given space that maps each
3937 * input dimension to the corresponding output dimension.
3939 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3940 __isl_take isl_space
*space
)
3942 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3945 /* Exploit the equalities in "eq" to simplify the affine expressions.
3947 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3948 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3952 maff
= isl_multi_aff_cow(maff
);
3956 for (i
= 0; i
< maff
->n
; ++i
) {
3957 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3958 isl_basic_set_copy(eq
));
3963 isl_basic_set_free(eq
);
3966 isl_basic_set_free(eq
);
3967 isl_multi_aff_free(maff
);
3971 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3976 maff
= isl_multi_aff_cow(maff
);
3980 for (i
= 0; i
< maff
->n
; ++i
) {
3981 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3983 return isl_multi_aff_free(maff
);
3989 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3990 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3992 maff1
= isl_multi_aff_add(maff1
, maff2
);
3993 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3997 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4005 /* Return the set of domain elements where "ma1" is lexicographically
4006 * smaller than or equal to "ma2".
4008 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4009 __isl_take isl_multi_aff
*ma2
)
4011 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4014 /* Return the set of domain elements where "ma1" is lexicographically
4015 * smaller than "ma2".
4017 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4018 __isl_take isl_multi_aff
*ma2
)
4020 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4023 /* Return the set of domain elements where "ma1" and "ma2"
4026 static __isl_give isl_set
*isl_multi_aff_order_set(
4027 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4028 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4031 isl_map
*map1
, *map2
;
4034 map1
= isl_map_from_multi_aff(ma1
);
4035 map2
= isl_map_from_multi_aff(ma2
);
4036 map
= isl_map_range_product(map1
, map2
);
4037 space
= isl_space_range(isl_map_get_space(map
));
4038 space
= isl_space_domain(isl_space_unwrap(space
));
4040 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4042 return isl_map_domain(map
);
4045 /* Return the set of domain elements where "ma1" is lexicographically
4046 * greater than or equal to "ma2".
4048 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4049 __isl_take isl_multi_aff
*ma2
)
4051 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4054 /* Return the set of domain elements where "ma1" is lexicographically
4055 * greater than "ma2".
4057 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4058 __isl_take isl_multi_aff
*ma2
)
4060 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4064 #define PW isl_pw_multi_aff
4066 #define EL isl_multi_aff
4068 #define EL_IS_ZERO is_empty
4072 #define IS_ZERO is_empty
4075 #undef DEFAULT_IS_ZERO
4076 #define DEFAULT_IS_ZERO 0
4081 #define NO_INVOLVES_DIMS
4082 #define NO_INSERT_DIMS
4086 #include <isl_pw_templ.c>
4087 #include <isl_pw_union_opt.c>
4092 #define UNION isl_union_pw_multi_aff
4094 #define PART isl_pw_multi_aff
4096 #define PARTS pw_multi_aff
4098 #include <isl_union_multi.c>
4099 #include <isl_union_neg.c>
4101 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4102 __isl_take isl_pw_multi_aff
*pma1
,
4103 __isl_take isl_pw_multi_aff
*pma2
)
4105 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4106 &isl_multi_aff_lex_ge_set
);
4109 /* Given two piecewise multi affine expressions, return a piecewise
4110 * multi-affine expression defined on the union of the definition domains
4111 * of the inputs that is equal to the lexicographic maximum of the two
4112 * inputs on each cell. If only one of the two inputs is defined on
4113 * a given cell, then it is considered to be the maximum.
4115 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4116 __isl_take isl_pw_multi_aff
*pma1
,
4117 __isl_take isl_pw_multi_aff
*pma2
)
4119 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4120 &pw_multi_aff_union_lexmax
);
4123 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4124 __isl_take isl_pw_multi_aff
*pma1
,
4125 __isl_take isl_pw_multi_aff
*pma2
)
4127 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4128 &isl_multi_aff_lex_le_set
);
4131 /* Given two piecewise multi affine expressions, return a piecewise
4132 * multi-affine expression defined on the union of the definition domains
4133 * of the inputs that is equal to the lexicographic minimum of the two
4134 * inputs on each cell. If only one of the two inputs is defined on
4135 * a given cell, then it is considered to be the minimum.
4137 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4138 __isl_take isl_pw_multi_aff
*pma1
,
4139 __isl_take isl_pw_multi_aff
*pma2
)
4141 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4142 &pw_multi_aff_union_lexmin
);
4145 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4146 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4148 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4149 &isl_multi_aff_add
);
4152 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4153 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4155 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4159 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4160 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4162 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4163 &isl_multi_aff_sub
);
4166 /* Subtract "pma2" from "pma1" and return the result.
4168 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4169 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4171 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4175 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4176 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4178 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4181 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4182 * with the actual sum on the shared domain and
4183 * the defined expression on the symmetric difference of the domains.
4185 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4186 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4188 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4191 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4192 * with the actual sum on the shared domain and
4193 * the defined expression on the symmetric difference of the domains.
4195 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4196 __isl_take isl_union_pw_multi_aff
*upma1
,
4197 __isl_take isl_union_pw_multi_aff
*upma2
)
4199 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4202 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4203 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4205 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4206 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4210 isl_pw_multi_aff
*res
;
4215 n
= pma1
->n
* pma2
->n
;
4216 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4217 isl_space_copy(pma2
->dim
));
4218 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4220 for (i
= 0; i
< pma1
->n
; ++i
) {
4221 for (j
= 0; j
< pma2
->n
; ++j
) {
4225 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4226 isl_set_copy(pma2
->p
[j
].set
));
4227 ma
= isl_multi_aff_product(
4228 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4229 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4230 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4234 isl_pw_multi_aff_free(pma1
);
4235 isl_pw_multi_aff_free(pma2
);
4238 isl_pw_multi_aff_free(pma1
);
4239 isl_pw_multi_aff_free(pma2
);
4243 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4244 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4246 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4247 &pw_multi_aff_product
);
4250 /* Construct a map mapping the domain of the piecewise multi-affine expression
4251 * to its range, with each dimension in the range equated to the
4252 * corresponding affine expression on its cell.
4254 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4262 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4264 for (i
= 0; i
< pma
->n
; ++i
) {
4265 isl_multi_aff
*maff
;
4266 isl_basic_map
*bmap
;
4269 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4270 bmap
= isl_basic_map_from_multi_aff(maff
);
4271 map_i
= isl_map_from_basic_map(bmap
);
4272 map_i
= isl_map_intersect_domain(map_i
,
4273 isl_set_copy(pma
->p
[i
].set
));
4274 map
= isl_map_union_disjoint(map
, map_i
);
4277 isl_pw_multi_aff_free(pma
);
4281 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4286 if (!isl_space_is_set(pma
->dim
))
4287 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4288 "isl_pw_multi_aff cannot be converted into an isl_set",
4291 return isl_map_from_pw_multi_aff(pma
);
4293 isl_pw_multi_aff_free(pma
);
4297 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4298 * denominator "denom".
4299 * "denom" is allowed to be negative, in which case the actual denominator
4300 * is -denom and the expressions are added instead.
4302 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4303 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4309 first
= isl_seq_first_non_zero(c
, n
);
4313 sign
= isl_int_sgn(denom
);
4315 isl_int_abs(d
, denom
);
4316 for (i
= first
; i
< n
; ++i
) {
4319 if (isl_int_is_zero(c
[i
]))
4321 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4322 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4323 aff_i
= isl_aff_scale_down(aff_i
, d
);
4325 aff
= isl_aff_sub(aff
, aff_i
);
4327 aff
= isl_aff_add(aff
, aff_i
);
4334 /* Extract an affine expression that expresses the output dimension "pos"
4335 * of "bmap" in terms of the parameters and input dimensions from
4337 * Note that this expression may involve integer divisions defined
4338 * in terms of parameters and input dimensions.
4339 * The equality may also involve references to earlier (but not later)
4340 * output dimensions. These are replaced by the corresponding elements
4343 * If the equality is of the form
4345 * f(i) + h(j) + a x + g(i) = 0,
4347 * with f(i) a linear combinations of the parameters and input dimensions,
4348 * g(i) a linear combination of integer divisions defined in terms of the same
4349 * and h(j) a linear combinations of earlier output dimensions,
4350 * then the affine expression is
4352 * (-f(i) - g(i))/a - h(j)/a
4354 * If the equality is of the form
4356 * f(i) + h(j) - a x + g(i) = 0,
4358 * then the affine expression is
4360 * (f(i) + g(i))/a - h(j)/(-a)
4363 * If "div" refers to an integer division (i.e., it is smaller than
4364 * the number of integer divisions), then the equality constraint
4365 * does involve an integer division (the one at position "div") that
4366 * is defined in terms of output dimensions. However, this integer
4367 * division can be eliminated by exploiting a pair of constraints
4368 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4369 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4371 * In particular, let
4373 * x = e(i) + m floor(...)
4375 * with e(i) the expression derived above and floor(...) the integer
4376 * division involving output dimensions.
4387 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4388 * = (e(i) - l) mod m
4392 * x - l = (e(i) - l) mod m
4396 * x = ((e(i) - l) mod m) + l
4398 * The variable "shift" below contains the expression -l, which may
4399 * also involve a linear combination of earlier output dimensions.
4401 static __isl_give isl_aff
*extract_aff_from_equality(
4402 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4403 __isl_keep isl_multi_aff
*ma
)
4406 unsigned n_div
, n_out
;
4408 isl_local_space
*ls
;
4409 isl_aff
*aff
, *shift
;
4412 ctx
= isl_basic_map_get_ctx(bmap
);
4413 ls
= isl_basic_map_get_local_space(bmap
);
4414 ls
= isl_local_space_domain(ls
);
4415 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4418 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4419 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4420 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4421 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4422 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4423 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4424 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4426 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4427 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4428 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4431 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4432 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4433 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4434 bmap
->eq
[eq
][o_out
+ pos
]);
4436 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4439 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4440 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4441 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4442 isl_int_set_si(shift
->v
->el
[0], 1);
4443 shift
= subtract_initial(shift
, ma
, pos
,
4444 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4445 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4446 mod
= isl_val_int_from_isl_int(ctx
,
4447 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4448 mod
= isl_val_abs(mod
);
4449 aff
= isl_aff_mod_val(aff
, mod
);
4450 aff
= isl_aff_sub(aff
, shift
);
4453 isl_local_space_free(ls
);
4456 isl_local_space_free(ls
);
4461 /* Given a basic map with output dimensions defined
4462 * in terms of the parameters input dimensions and earlier
4463 * output dimensions using an equality (and possibly a pair on inequalities),
4464 * extract an isl_aff that expresses output dimension "pos" in terms
4465 * of the parameters and input dimensions.
4466 * Note that this expression may involve integer divisions defined
4467 * in terms of parameters and input dimensions.
4468 * "ma" contains the expressions corresponding to earlier output dimensions.
4470 * This function shares some similarities with
4471 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4473 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4474 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4481 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4482 if (eq
>= bmap
->n_eq
)
4483 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4484 "unable to find suitable equality", return NULL
);
4485 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4487 aff
= isl_aff_remove_unused_divs(aff
);
4491 /* Given a basic map where each output dimension is defined
4492 * in terms of the parameters and input dimensions using an equality,
4493 * extract an isl_multi_aff that expresses the output dimensions in terms
4494 * of the parameters and input dimensions.
4496 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4497 __isl_take isl_basic_map
*bmap
)
4506 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4507 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4509 for (i
= 0; i
< n_out
; ++i
) {
4512 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4513 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4516 isl_basic_map_free(bmap
);
4521 /* Given a basic set where each set dimension is defined
4522 * in terms of the parameters using an equality,
4523 * extract an isl_multi_aff that expresses the set dimensions in terms
4524 * of the parameters.
4526 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4527 __isl_take isl_basic_set
*bset
)
4529 return extract_isl_multi_aff_from_basic_map(bset
);
4532 /* Create an isl_pw_multi_aff that is equivalent to
4533 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4534 * The given basic map is such that each output dimension is defined
4535 * in terms of the parameters and input dimensions using an equality.
4537 * Since some applications expect the result of isl_pw_multi_aff_from_map
4538 * to only contain integer affine expressions, we compute the floor
4539 * of the expression before returning.
4541 * Remove all constraints involving local variables without
4542 * an explicit representation (resulting in the removal of those
4543 * local variables) prior to the actual extraction to ensure
4544 * that the local spaces in which the resulting affine expressions
4545 * are created do not contain any unknown local variables.
4546 * Removing such constraints is safe because constraints involving
4547 * unknown local variables are not used to determine whether
4548 * a basic map is obviously single-valued.
4550 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4551 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4555 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4556 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4557 ma
= isl_multi_aff_floor(ma
);
4558 return isl_pw_multi_aff_alloc(domain
, ma
);
4561 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4562 * This obviously only works if the input "map" is single-valued.
4563 * If so, we compute the lexicographic minimum of the image in the form
4564 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4565 * to its lexicographic minimum.
4566 * If the input is not single-valued, we produce an error.
4568 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4569 __isl_take isl_map
*map
)
4573 isl_pw_multi_aff
*pma
;
4575 sv
= isl_map_is_single_valued(map
);
4579 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4580 "map is not single-valued", goto error
);
4581 map
= isl_map_make_disjoint(map
);
4585 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4587 for (i
= 0; i
< map
->n
; ++i
) {
4588 isl_pw_multi_aff
*pma_i
;
4589 isl_basic_map
*bmap
;
4590 bmap
= isl_basic_map_copy(map
->p
[i
]);
4591 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4592 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4602 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4603 * taking into account that the output dimension at position "d"
4604 * can be represented as
4606 * x = floor((e(...) + c1) / m)
4608 * given that constraint "i" is of the form
4610 * e(...) + c1 - m x >= 0
4613 * Let "map" be of the form
4617 * We construct a mapping
4619 * A -> [A -> x = floor(...)]
4621 * apply that to the map, obtaining
4623 * [A -> x = floor(...)] -> B
4625 * and equate dimension "d" to x.
4626 * We then compute a isl_pw_multi_aff representation of the resulting map
4627 * and plug in the mapping above.
4629 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4630 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4634 isl_local_space
*ls
;
4642 isl_pw_multi_aff
*pma
;
4645 is_set
= isl_map_is_set(map
);
4647 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4648 ctx
= isl_map_get_ctx(map
);
4649 space
= isl_space_domain(isl_map_get_space(map
));
4650 n_in
= isl_space_dim(space
, isl_dim_set
);
4651 n
= isl_space_dim(space
, isl_dim_all
);
4653 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4655 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4656 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4658 isl_basic_map_free(hull
);
4660 ls
= isl_local_space_from_space(isl_space_copy(space
));
4661 aff
= isl_aff_alloc_vec(ls
, v
);
4662 aff
= isl_aff_floor(aff
);
4664 isl_space_free(space
);
4665 ma
= isl_multi_aff_from_aff(aff
);
4667 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4668 ma
= isl_multi_aff_range_product(ma
,
4669 isl_multi_aff_from_aff(aff
));
4672 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4673 map
= isl_map_apply_domain(map
, insert
);
4674 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4675 pma
= isl_pw_multi_aff_from_map(map
);
4676 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4681 /* Is constraint "c" of the form
4683 * e(...) + c1 - m x >= 0
4687 * -e(...) + c2 + m x >= 0
4689 * where m > 1 and e only depends on parameters and input dimemnsions?
4691 * "offset" is the offset of the output dimensions
4692 * "pos" is the position of output dimension x.
4694 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4696 if (isl_int_is_zero(c
[offset
+ d
]))
4698 if (isl_int_is_one(c
[offset
+ d
]))
4700 if (isl_int_is_negone(c
[offset
+ d
]))
4702 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4704 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4705 total
- (offset
+ d
+ 1)) != -1)
4710 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4712 * As a special case, we first check if there is any pair of constraints,
4713 * shared by all the basic maps in "map" that force a given dimension
4714 * to be equal to the floor of some affine combination of the input dimensions.
4716 * In particular, if we can find two constraints
4718 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4722 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4724 * where m > 1 and e only depends on parameters and input dimemnsions,
4727 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4729 * then we know that we can take
4731 * x = floor((e(...) + c1) / m)
4733 * without having to perform any computation.
4735 * Note that we know that
4739 * If c1 + c2 were 0, then we would have detected an equality during
4740 * simplification. If c1 + c2 were negative, then we would have detected
4743 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4744 __isl_take isl_map
*map
)
4750 isl_basic_map
*hull
;
4752 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4757 dim
= isl_map_dim(map
, isl_dim_out
);
4758 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4759 total
= 1 + isl_basic_map_total_dim(hull
);
4761 for (d
= 0; d
< dim
; ++d
) {
4762 for (i
= 0; i
< n
; ++i
) {
4763 if (!is_potential_div_constraint(hull
->ineq
[i
],
4766 for (j
= i
+ 1; j
< n
; ++j
) {
4767 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4768 hull
->ineq
[j
] + 1, total
- 1))
4770 isl_int_add(sum
, hull
->ineq
[i
][0],
4772 if (isl_int_abs_lt(sum
,
4773 hull
->ineq
[i
][offset
+ d
]))
4780 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4782 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4786 isl_basic_map_free(hull
);
4787 return pw_multi_aff_from_map_base(map
);
4790 isl_basic_map_free(hull
);
4794 /* Given an affine expression
4796 * [A -> B] -> f(A,B)
4798 * construct an isl_multi_aff
4802 * such that dimension "d" in B' is set to "aff" and the remaining
4803 * dimensions are set equal to the corresponding dimensions in B.
4804 * "n_in" is the dimension of the space A.
4805 * "n_out" is the dimension of the space B.
4807 * If "is_set" is set, then the affine expression is of the form
4811 * and we construct an isl_multi_aff
4815 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4816 unsigned n_in
, unsigned n_out
, int is_set
)
4820 isl_space
*space
, *space2
;
4821 isl_local_space
*ls
;
4823 space
= isl_aff_get_domain_space(aff
);
4824 ls
= isl_local_space_from_space(isl_space_copy(space
));
4825 space2
= isl_space_copy(space
);
4827 space2
= isl_space_range(isl_space_unwrap(space2
));
4828 space
= isl_space_map_from_domain_and_range(space
, space2
);
4829 ma
= isl_multi_aff_alloc(space
);
4830 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4832 for (i
= 0; i
< n_out
; ++i
) {
4835 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4836 isl_dim_set
, n_in
+ i
);
4837 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4840 isl_local_space_free(ls
);
4845 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4846 * taking into account that the dimension at position "d" can be written as
4848 * x = m a + f(..) (1)
4850 * where m is equal to "gcd".
4851 * "i" is the index of the equality in "hull" that defines f(..).
4852 * In particular, the equality is of the form
4854 * f(..) - x + m g(existentials) = 0
4858 * -f(..) + x + m g(existentials) = 0
4860 * We basically plug (1) into "map", resulting in a map with "a"
4861 * in the range instead of "x". The corresponding isl_pw_multi_aff
4862 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4864 * Specifically, given the input map
4868 * We first wrap it into a set
4872 * and define (1) on top of the corresponding space, resulting in "aff".
4873 * We use this to create an isl_multi_aff that maps the output position "d"
4874 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4875 * We plug this into the wrapped map, unwrap the result and compute the
4876 * corresponding isl_pw_multi_aff.
4877 * The result is an expression
4885 * so that we can plug that into "aff", after extending the latter to
4891 * If "map" is actually a set, then there is no "A" space, meaning
4892 * that we do not need to perform any wrapping, and that the result
4893 * of the recursive call is of the form
4897 * which is plugged into a mapping of the form
4901 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4902 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4907 isl_local_space
*ls
;
4910 isl_pw_multi_aff
*pma
, *id
;
4916 is_set
= isl_map_is_set(map
);
4918 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4919 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4920 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4925 set
= isl_map_wrap(map
);
4926 space
= isl_space_map_from_set(isl_set_get_space(set
));
4927 ma
= isl_multi_aff_identity(space
);
4928 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4929 aff
= isl_aff_alloc(ls
);
4931 isl_int_set_si(aff
->v
->el
[0], 1);
4932 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4933 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4936 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4938 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4940 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4941 set
= isl_set_preimage_multi_aff(set
, ma
);
4943 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4948 map
= isl_set_unwrap(set
);
4949 pma
= isl_pw_multi_aff_from_map(map
);
4952 space
= isl_pw_multi_aff_get_domain_space(pma
);
4953 space
= isl_space_map_from_set(space
);
4954 id
= isl_pw_multi_aff_identity(space
);
4955 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4957 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4958 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4960 isl_basic_map_free(hull
);
4964 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4966 * As a special case, we first check if all output dimensions are uniquely
4967 * defined in terms of the parameters and input dimensions over the entire
4968 * domain. If so, we extract the desired isl_pw_multi_aff directly
4969 * from the affine hull of "map" and its domain.
4971 * Otherwise, we check if any of the output dimensions is "strided".
4972 * That is, we check if can be written as
4976 * with m greater than 1, a some combination of existentially quantified
4977 * variables and f an expression in the parameters and input dimensions.
4978 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4980 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4983 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4987 isl_basic_map
*hull
;
4997 map
= isl_map_detect_equalities(map
);
4998 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4999 sv
= isl_basic_map_plain_is_single_valued(hull
);
5001 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5003 hull
= isl_basic_map_free(hull
);
5007 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5008 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5011 isl_basic_map_free(hull
);
5012 return pw_multi_aff_from_map_check_div(map
);
5017 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5018 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5020 for (i
= 0; i
< n_out
; ++i
) {
5021 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5022 isl_int
*eq
= hull
->eq
[j
];
5023 isl_pw_multi_aff
*res
;
5025 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5026 !isl_int_is_negone(eq
[o_out
+ i
]))
5028 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5030 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5031 n_out
- (i
+ 1)) != -1)
5033 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5034 if (isl_int_is_zero(gcd
))
5036 if (isl_int_is_one(gcd
))
5039 res
= pw_multi_aff_from_map_stride(map
, hull
,
5047 isl_basic_map_free(hull
);
5048 return pw_multi_aff_from_map_check_div(map
);
5054 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5056 return isl_pw_multi_aff_from_map(set
);
5059 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5062 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5064 isl_union_pw_multi_aff
**upma
= user
;
5065 isl_pw_multi_aff
*pma
;
5067 pma
= isl_pw_multi_aff_from_map(map
);
5068 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5070 return *upma
? isl_stat_ok
: isl_stat_error
;
5073 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5076 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5077 __isl_take isl_aff
*aff
)
5080 isl_pw_multi_aff
*pma
;
5082 ma
= isl_multi_aff_from_aff(aff
);
5083 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5084 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5087 /* Try and create an isl_union_pw_multi_aff that is equivalent
5088 * to the given isl_union_map.
5089 * The isl_union_map is required to be single-valued in each space.
5090 * Otherwise, an error is produced.
5092 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5093 __isl_take isl_union_map
*umap
)
5096 isl_union_pw_multi_aff
*upma
;
5098 space
= isl_union_map_get_space(umap
);
5099 upma
= isl_union_pw_multi_aff_empty(space
);
5100 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5101 upma
= isl_union_pw_multi_aff_free(upma
);
5102 isl_union_map_free(umap
);
5107 /* Try and create an isl_union_pw_multi_aff that is equivalent
5108 * to the given isl_union_set.
5109 * The isl_union_set is required to be a singleton in each space.
5110 * Otherwise, an error is produced.
5112 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5113 __isl_take isl_union_set
*uset
)
5115 return isl_union_pw_multi_aff_from_union_map(uset
);
5118 /* Return the piecewise affine expression "set ? 1 : 0".
5120 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5123 isl_space
*space
= isl_set_get_space(set
);
5124 isl_local_space
*ls
= isl_local_space_from_space(space
);
5125 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5126 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5128 one
= isl_aff_add_constant_si(one
, 1);
5129 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5130 set
= isl_set_complement(set
);
5131 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5136 /* Plug in "subs" for dimension "type", "pos" of "aff".
5138 * Let i be the dimension to replace and let "subs" be of the form
5142 * and "aff" of the form
5148 * (a f + d g')/(m d)
5150 * where g' is the result of plugging in "subs" in each of the integer
5153 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5154 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5159 aff
= isl_aff_cow(aff
);
5161 return isl_aff_free(aff
);
5163 ctx
= isl_aff_get_ctx(aff
);
5164 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5165 isl_die(ctx
, isl_error_invalid
,
5166 "spaces don't match", return isl_aff_free(aff
));
5167 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5168 isl_die(ctx
, isl_error_unsupported
,
5169 "cannot handle divs yet", return isl_aff_free(aff
));
5171 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5173 return isl_aff_free(aff
);
5175 aff
->v
= isl_vec_cow(aff
->v
);
5177 return isl_aff_free(aff
);
5179 pos
+= isl_local_space_offset(aff
->ls
, type
);
5182 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5183 aff
->v
->size
, subs
->v
->size
, v
);
5189 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5190 * expressions in "maff".
5192 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5193 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5194 __isl_keep isl_aff
*subs
)
5198 maff
= isl_multi_aff_cow(maff
);
5200 return isl_multi_aff_free(maff
);
5202 if (type
== isl_dim_in
)
5205 for (i
= 0; i
< maff
->n
; ++i
) {
5206 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5208 return isl_multi_aff_free(maff
);
5214 /* Plug in "subs" for dimension "type", "pos" of "pma".
5216 * pma is of the form
5220 * while subs is of the form
5222 * v' = B_j(v) -> S_j
5224 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5225 * has a contribution in the result, in particular
5227 * C_ij(S_j) -> M_i(S_j)
5229 * Note that plugging in S_j in C_ij may also result in an empty set
5230 * and this contribution should simply be discarded.
5232 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5233 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5234 __isl_keep isl_pw_aff
*subs
)
5237 isl_pw_multi_aff
*res
;
5240 return isl_pw_multi_aff_free(pma
);
5242 n
= pma
->n
* subs
->n
;
5243 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5245 for (i
= 0; i
< pma
->n
; ++i
) {
5246 for (j
= 0; j
< subs
->n
; ++j
) {
5248 isl_multi_aff
*res_ij
;
5251 common
= isl_set_intersect(
5252 isl_set_copy(pma
->p
[i
].set
),
5253 isl_set_copy(subs
->p
[j
].set
));
5254 common
= isl_set_substitute(common
,
5255 type
, pos
, subs
->p
[j
].aff
);
5256 empty
= isl_set_plain_is_empty(common
);
5257 if (empty
< 0 || empty
) {
5258 isl_set_free(common
);
5264 res_ij
= isl_multi_aff_substitute(
5265 isl_multi_aff_copy(pma
->p
[i
].maff
),
5266 type
, pos
, subs
->p
[j
].aff
);
5268 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5272 isl_pw_multi_aff_free(pma
);
5275 isl_pw_multi_aff_free(pma
);
5276 isl_pw_multi_aff_free(res
);
5280 /* Compute the preimage of a range of dimensions in the affine expression "src"
5281 * under "ma" and put the result in "dst". The number of dimensions in "src"
5282 * that precede the range is given by "n_before". The number of dimensions
5283 * in the range is given by the number of output dimensions of "ma".
5284 * The number of dimensions that follow the range is given by "n_after".
5285 * If "has_denom" is set (to one),
5286 * then "src" and "dst" have an extra initial denominator.
5287 * "n_div_ma" is the number of existentials in "ma"
5288 * "n_div_bset" is the number of existentials in "src"
5289 * The resulting "dst" (which is assumed to have been allocated by
5290 * the caller) contains coefficients for both sets of existentials,
5291 * first those in "ma" and then those in "src".
5292 * f, c1, c2 and g are temporary objects that have been initialized
5295 * Let src represent the expression
5297 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5299 * and let ma represent the expressions
5301 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5303 * We start out with the following expression for dst:
5305 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5307 * with the multiplication factor f initially equal to 1
5308 * and f \sum_i b_i v_i kept separately.
5309 * For each x_i that we substitute, we multiply the numerator
5310 * (and denominator) of dst by c_1 = m_i and add the numerator
5311 * of the x_i expression multiplied by c_2 = f b_i,
5312 * after removing the common factors of c_1 and c_2.
5313 * The multiplication factor f also needs to be multiplied by c_1
5314 * for the next x_j, j > i.
5316 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5317 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5318 int n_div_ma
, int n_div_bmap
,
5319 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5322 int n_param
, n_in
, n_out
;
5325 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5326 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5327 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5329 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5330 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5331 isl_seq_clr(dst
+ o_dst
, n_in
);
5334 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5337 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5339 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5341 isl_int_set_si(f
, 1);
5343 for (i
= 0; i
< n_out
; ++i
) {
5344 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5346 if (isl_int_is_zero(src
[offset
]))
5348 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5349 isl_int_mul(c2
, f
, src
[offset
]);
5350 isl_int_gcd(g
, c1
, c2
);
5351 isl_int_divexact(c1
, c1
, g
);
5352 isl_int_divexact(c2
, c2
, g
);
5354 isl_int_mul(f
, f
, c1
);
5357 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5358 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5359 o_dst
+= 1 + n_param
;
5360 o_src
+= 1 + n_param
;
5361 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5363 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5364 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5367 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5369 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5370 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5373 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5375 isl_int_mul(dst
[0], dst
[0], c1
);
5379 /* Compute the pullback of "aff" by the function represented by "ma".
5380 * In other words, plug in "ma" in "aff". The result is an affine expression
5381 * defined over the domain space of "ma".
5383 * If "aff" is represented by
5385 * (a(p) + b x + c(divs))/d
5387 * and ma is represented by
5389 * x = D(p) + F(y) + G(divs')
5391 * then the result is
5393 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5395 * The divs in the local space of the input are similarly adjusted
5396 * through a call to isl_local_space_preimage_multi_aff.
5398 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5399 __isl_take isl_multi_aff
*ma
)
5401 isl_aff
*res
= NULL
;
5402 isl_local_space
*ls
;
5403 int n_div_aff
, n_div_ma
;
5404 isl_int f
, c1
, c2
, g
;
5406 ma
= isl_multi_aff_align_divs(ma
);
5410 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5411 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5413 ls
= isl_aff_get_domain_local_space(aff
);
5414 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5415 res
= isl_aff_alloc(ls
);
5424 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5433 isl_multi_aff_free(ma
);
5434 res
= isl_aff_normalize(res
);
5438 isl_multi_aff_free(ma
);
5443 /* Compute the pullback of "aff1" by the function represented by "aff2".
5444 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5445 * defined over the domain space of "aff1".
5447 * The domain of "aff1" should match the range of "aff2", which means
5448 * that it should be single-dimensional.
5450 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5451 __isl_take isl_aff
*aff2
)
5455 ma
= isl_multi_aff_from_aff(aff2
);
5456 return isl_aff_pullback_multi_aff(aff1
, ma
);
5459 /* Compute the pullback of "ma1" by the function represented by "ma2".
5460 * In other words, plug in "ma2" in "ma1".
5462 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5464 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5465 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5468 isl_space
*space
= NULL
;
5470 ma2
= isl_multi_aff_align_divs(ma2
);
5471 ma1
= isl_multi_aff_cow(ma1
);
5475 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5476 isl_multi_aff_get_space(ma1
));
5478 for (i
= 0; i
< ma1
->n
; ++i
) {
5479 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5480 isl_multi_aff_copy(ma2
));
5485 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5486 isl_multi_aff_free(ma2
);
5489 isl_space_free(space
);
5490 isl_multi_aff_free(ma2
);
5491 isl_multi_aff_free(ma1
);
5495 /* Compute the pullback of "ma1" by the function represented by "ma2".
5496 * In other words, plug in "ma2" in "ma1".
5498 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5499 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5501 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5502 &isl_multi_aff_pullback_multi_aff_aligned
);
5505 /* Extend the local space of "dst" to include the divs
5506 * in the local space of "src".
5508 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5509 __isl_keep isl_aff
*src
)
5517 return isl_aff_free(dst
);
5519 ctx
= isl_aff_get_ctx(src
);
5520 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5521 isl_die(ctx
, isl_error_invalid
,
5522 "spaces don't match", goto error
);
5524 if (src
->ls
->div
->n_row
== 0)
5527 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5528 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5529 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5532 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5533 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5541 return isl_aff_free(dst
);
5544 /* Adjust the local spaces of the affine expressions in "maff"
5545 * such that they all have the save divs.
5547 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5548 __isl_take isl_multi_aff
*maff
)
5556 maff
= isl_multi_aff_cow(maff
);
5560 for (i
= 1; i
< maff
->n
; ++i
)
5561 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5562 for (i
= 1; i
< maff
->n
; ++i
) {
5563 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5565 return isl_multi_aff_free(maff
);
5571 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5573 aff
= isl_aff_cow(aff
);
5577 aff
->ls
= isl_local_space_lift(aff
->ls
);
5579 return isl_aff_free(aff
);
5584 /* Lift "maff" to a space with extra dimensions such that the result
5585 * has no more existentially quantified variables.
5586 * If "ls" is not NULL, then *ls is assigned the local space that lies
5587 * at the basis of the lifting applied to "maff".
5589 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5590 __isl_give isl_local_space
**ls
)
5604 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5605 *ls
= isl_local_space_from_space(space
);
5607 return isl_multi_aff_free(maff
);
5612 maff
= isl_multi_aff_cow(maff
);
5613 maff
= isl_multi_aff_align_divs(maff
);
5617 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5618 space
= isl_multi_aff_get_space(maff
);
5619 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5620 space
= isl_space_extend_domain_with_range(space
,
5621 isl_multi_aff_get_space(maff
));
5623 return isl_multi_aff_free(maff
);
5624 isl_space_free(maff
->space
);
5625 maff
->space
= space
;
5628 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5630 return isl_multi_aff_free(maff
);
5633 for (i
= 0; i
< maff
->n
; ++i
) {
5634 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5642 isl_local_space_free(*ls
);
5643 return isl_multi_aff_free(maff
);
5647 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5649 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5650 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5660 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5661 if (pos
< 0 || pos
>= n_out
)
5662 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5663 "index out of bounds", return NULL
);
5665 space
= isl_pw_multi_aff_get_space(pma
);
5666 space
= isl_space_drop_dims(space
, isl_dim_out
,
5667 pos
+ 1, n_out
- pos
- 1);
5668 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5670 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5671 for (i
= 0; i
< pma
->n
; ++i
) {
5673 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5674 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5680 /* Return an isl_pw_multi_aff with the given "set" as domain and
5681 * an unnamed zero-dimensional range.
5683 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5684 __isl_take isl_set
*set
)
5689 space
= isl_set_get_space(set
);
5690 space
= isl_space_from_domain(space
);
5691 ma
= isl_multi_aff_zero(space
);
5692 return isl_pw_multi_aff_alloc(set
, ma
);
5695 /* Add an isl_pw_multi_aff with the given "set" as domain and
5696 * an unnamed zero-dimensional range to *user.
5698 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5701 isl_union_pw_multi_aff
**upma
= user
;
5702 isl_pw_multi_aff
*pma
;
5704 pma
= isl_pw_multi_aff_from_domain(set
);
5705 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5710 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5711 * an unnamed zero-dimensional range.
5713 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5714 __isl_take isl_union_set
*uset
)
5717 isl_union_pw_multi_aff
*upma
;
5722 space
= isl_union_set_get_space(uset
);
5723 upma
= isl_union_pw_multi_aff_empty(space
);
5725 if (isl_union_set_foreach_set(uset
,
5726 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5729 isl_union_set_free(uset
);
5732 isl_union_set_free(uset
);
5733 isl_union_pw_multi_aff_free(upma
);
5737 /* Convert "pma" to an isl_map and add it to *umap.
5739 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5742 isl_union_map
**umap
= user
;
5745 map
= isl_map_from_pw_multi_aff(pma
);
5746 *umap
= isl_union_map_add_map(*umap
, map
);
5751 /* Construct a union map mapping the domain of the union
5752 * piecewise multi-affine expression to its range, with each dimension
5753 * in the range equated to the corresponding affine expression on its cell.
5755 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5756 __isl_take isl_union_pw_multi_aff
*upma
)
5759 isl_union_map
*umap
;
5764 space
= isl_union_pw_multi_aff_get_space(upma
);
5765 umap
= isl_union_map_empty(space
);
5767 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5768 &map_from_pw_multi_aff
, &umap
) < 0)
5771 isl_union_pw_multi_aff_free(upma
);
5774 isl_union_pw_multi_aff_free(upma
);
5775 isl_union_map_free(umap
);
5779 /* Local data for bin_entry and the callback "fn".
5781 struct isl_union_pw_multi_aff_bin_data
{
5782 isl_union_pw_multi_aff
*upma2
;
5783 isl_union_pw_multi_aff
*res
;
5784 isl_pw_multi_aff
*pma
;
5785 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5788 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5789 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5791 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5793 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5797 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5799 isl_pw_multi_aff_free(pma
);
5804 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5805 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5806 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5807 * as *entry. The callback should adjust data->res if desired.
5809 static __isl_give isl_union_pw_multi_aff
*bin_op(
5810 __isl_take isl_union_pw_multi_aff
*upma1
,
5811 __isl_take isl_union_pw_multi_aff
*upma2
,
5812 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5815 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5817 space
= isl_union_pw_multi_aff_get_space(upma2
);
5818 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5819 space
= isl_union_pw_multi_aff_get_space(upma1
);
5820 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5822 if (!upma1
|| !upma2
)
5826 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5827 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5828 &bin_entry
, &data
) < 0)
5831 isl_union_pw_multi_aff_free(upma1
);
5832 isl_union_pw_multi_aff_free(upma2
);
5835 isl_union_pw_multi_aff_free(upma1
);
5836 isl_union_pw_multi_aff_free(upma2
);
5837 isl_union_pw_multi_aff_free(data
.res
);
5841 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5842 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5844 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5845 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5849 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5850 isl_pw_multi_aff_get_space(pma2
));
5851 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5852 &isl_multi_aff_range_product
);
5855 /* Given two isl_pw_multi_affs A -> B and C -> D,
5856 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5858 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5859 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5861 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5862 &pw_multi_aff_range_product
);
5865 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5866 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5868 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5869 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5873 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5874 isl_pw_multi_aff_get_space(pma2
));
5875 space
= isl_space_flatten_range(space
);
5876 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5877 &isl_multi_aff_flat_range_product
);
5880 /* Given two isl_pw_multi_affs A -> B and C -> D,
5881 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5883 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5884 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5886 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5887 &pw_multi_aff_flat_range_product
);
5890 /* If data->pma and "pma2" have the same domain space, then compute
5891 * their flat range product and the result to data->res.
5893 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5896 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5898 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5899 pma2
->dim
, isl_dim_in
)) {
5900 isl_pw_multi_aff_free(pma2
);
5904 pma2
= isl_pw_multi_aff_flat_range_product(
5905 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5907 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5912 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5913 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5915 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5916 __isl_take isl_union_pw_multi_aff
*upma1
,
5917 __isl_take isl_union_pw_multi_aff
*upma2
)
5919 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5922 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5923 * The parameters are assumed to have been aligned.
5925 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5926 * except that it works on two different isl_pw_* types.
5928 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5929 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5930 __isl_take isl_pw_aff
*pa
)
5933 isl_pw_multi_aff
*res
= NULL
;
5938 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5939 pa
->dim
, isl_dim_in
))
5940 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5941 "domains don't match", goto error
);
5942 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5943 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5944 "index out of bounds", goto error
);
5947 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5949 for (i
= 0; i
< pma
->n
; ++i
) {
5950 for (j
= 0; j
< pa
->n
; ++j
) {
5952 isl_multi_aff
*res_ij
;
5955 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5956 isl_set_copy(pa
->p
[j
].set
));
5957 empty
= isl_set_plain_is_empty(common
);
5958 if (empty
< 0 || empty
) {
5959 isl_set_free(common
);
5965 res_ij
= isl_multi_aff_set_aff(
5966 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5967 isl_aff_copy(pa
->p
[j
].aff
));
5968 res_ij
= isl_multi_aff_gist(res_ij
,
5969 isl_set_copy(common
));
5971 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5975 isl_pw_multi_aff_free(pma
);
5976 isl_pw_aff_free(pa
);
5979 isl_pw_multi_aff_free(pma
);
5980 isl_pw_aff_free(pa
);
5981 return isl_pw_multi_aff_free(res
);
5984 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5986 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5987 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5988 __isl_take isl_pw_aff
*pa
)
5992 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5993 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5994 if (!isl_space_has_named_params(pma
->dim
) ||
5995 !isl_space_has_named_params(pa
->dim
))
5996 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5997 "unaligned unnamed parameters", goto error
);
5998 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5999 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6000 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6002 isl_pw_multi_aff_free(pma
);
6003 isl_pw_aff_free(pa
);
6007 /* Do the parameters of "pa" match those of "space"?
6009 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6010 __isl_keep isl_space
*space
)
6012 isl_space
*pa_space
;
6018 pa_space
= isl_pw_aff_get_space(pa
);
6020 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6022 isl_space_free(pa_space
);
6026 /* Check that the domain space of "pa" matches "space".
6028 * Return 0 on success and -1 on error.
6030 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6031 __isl_keep isl_space
*space
)
6033 isl_space
*pa_space
;
6039 pa_space
= isl_pw_aff_get_space(pa
);
6041 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6045 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6046 "parameters don't match", goto error
);
6047 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6048 pa_space
, isl_dim_in
);
6052 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6053 "domains don't match", goto error
);
6054 isl_space_free(pa_space
);
6057 isl_space_free(pa_space
);
6066 #include <isl_multi_templ.c>
6067 #include <isl_multi_apply_set.c>
6068 #include <isl_multi_coalesce.c>
6069 #include <isl_multi_gist.c>
6070 #include <isl_multi_hash.c>
6071 #include <isl_multi_intersect.c>
6073 /* Scale the elements of "pma" by the corresponding elements of "mv".
6075 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6076 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6080 pma
= isl_pw_multi_aff_cow(pma
);
6083 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6084 mv
->space
, isl_dim_set
))
6085 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6086 "spaces don't match", goto error
);
6087 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6088 mv
->space
, isl_dim_param
)) {
6089 pma
= isl_pw_multi_aff_align_params(pma
,
6090 isl_multi_val_get_space(mv
));
6091 mv
= isl_multi_val_align_params(mv
,
6092 isl_pw_multi_aff_get_space(pma
));
6097 for (i
= 0; i
< pma
->n
; ++i
) {
6098 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6099 isl_multi_val_copy(mv
));
6100 if (!pma
->p
[i
].maff
)
6104 isl_multi_val_free(mv
);
6107 isl_multi_val_free(mv
);
6108 isl_pw_multi_aff_free(pma
);
6112 /* This function is called for each entry of an isl_union_pw_multi_aff.
6113 * If the space of the entry matches that of data->mv,
6114 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6115 * Otherwise, return an empty isl_pw_multi_aff.
6117 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6118 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6120 isl_multi_val
*mv
= user
;
6124 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6125 mv
->space
, isl_dim_set
)) {
6126 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6127 isl_pw_multi_aff_free(pma
);
6128 return isl_pw_multi_aff_empty(space
);
6131 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6134 /* Scale the elements of "upma" by the corresponding elements of "mv",
6135 * for those entries that match the space of "mv".
6137 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6138 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6140 upma
= isl_union_pw_multi_aff_align_params(upma
,
6141 isl_multi_val_get_space(mv
));
6142 mv
= isl_multi_val_align_params(mv
,
6143 isl_union_pw_multi_aff_get_space(upma
));
6147 return isl_union_pw_multi_aff_transform(upma
,
6148 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6150 isl_multi_val_free(mv
);
6153 isl_multi_val_free(mv
);
6154 isl_union_pw_multi_aff_free(upma
);
6158 /* Construct and return a piecewise multi affine expression
6159 * in the given space with value zero in each of the output dimensions and
6160 * a universe domain.
6162 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6164 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6167 /* Construct and return a piecewise multi affine expression
6168 * that is equal to the given piecewise affine expression.
6170 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6171 __isl_take isl_pw_aff
*pa
)
6175 isl_pw_multi_aff
*pma
;
6180 space
= isl_pw_aff_get_space(pa
);
6181 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6183 for (i
= 0; i
< pa
->n
; ++i
) {
6187 set
= isl_set_copy(pa
->p
[i
].set
);
6188 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6189 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6192 isl_pw_aff_free(pa
);
6196 /* Construct a set or map mapping the shared (parameter) domain
6197 * of the piecewise affine expressions to the range of "mpa"
6198 * with each dimension in the range equated to the
6199 * corresponding piecewise affine expression.
6201 static __isl_give isl_map
*map_from_multi_pw_aff(
6202 __isl_take isl_multi_pw_aff
*mpa
)
6211 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6212 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6213 "invalid space", goto error
);
6215 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6216 map
= isl_map_universe(isl_space_from_domain(space
));
6218 for (i
= 0; i
< mpa
->n
; ++i
) {
6222 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6223 map_i
= map_from_pw_aff(pa
);
6225 map
= isl_map_flat_range_product(map
, map_i
);
6228 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6230 isl_multi_pw_aff_free(mpa
);
6233 isl_multi_pw_aff_free(mpa
);
6237 /* Construct a map mapping the shared domain
6238 * of the piecewise affine expressions to the range of "mpa"
6239 * with each dimension in the range equated to the
6240 * corresponding piecewise affine expression.
6242 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6246 if (isl_space_is_set(mpa
->space
))
6247 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6248 "space of input is not a map", goto error
);
6250 return map_from_multi_pw_aff(mpa
);
6252 isl_multi_pw_aff_free(mpa
);
6256 /* Construct a set mapping the shared parameter domain
6257 * of the piecewise affine expressions to the space of "mpa"
6258 * with each dimension in the range equated to the
6259 * corresponding piecewise affine expression.
6261 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6265 if (!isl_space_is_set(mpa
->space
))
6266 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6267 "space of input is not a set", goto error
);
6269 return map_from_multi_pw_aff(mpa
);
6271 isl_multi_pw_aff_free(mpa
);
6275 /* Construct and return a piecewise multi affine expression
6276 * that is equal to the given multi piecewise affine expression
6277 * on the shared domain of the piecewise affine expressions.
6279 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6280 __isl_take isl_multi_pw_aff
*mpa
)
6285 isl_pw_multi_aff
*pma
;
6290 space
= isl_multi_pw_aff_get_space(mpa
);
6293 isl_multi_pw_aff_free(mpa
);
6294 return isl_pw_multi_aff_zero(space
);
6297 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6298 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6300 for (i
= 1; i
< mpa
->n
; ++i
) {
6301 isl_pw_multi_aff
*pma_i
;
6303 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6304 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6305 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6308 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6310 isl_multi_pw_aff_free(mpa
);
6314 /* Construct and return a multi piecewise affine expression
6315 * that is equal to the given multi affine expression.
6317 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6318 __isl_take isl_multi_aff
*ma
)
6321 isl_multi_pw_aff
*mpa
;
6326 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6327 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6329 for (i
= 0; i
< n
; ++i
) {
6332 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6333 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6336 isl_multi_aff_free(ma
);
6340 /* Construct and return a multi piecewise affine expression
6341 * that is equal to the given piecewise multi affine expression.
6343 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6344 __isl_take isl_pw_multi_aff
*pma
)
6348 isl_multi_pw_aff
*mpa
;
6353 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6354 space
= isl_pw_multi_aff_get_space(pma
);
6355 mpa
= isl_multi_pw_aff_alloc(space
);
6357 for (i
= 0; i
< n
; ++i
) {
6360 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6361 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6364 isl_pw_multi_aff_free(pma
);
6368 /* Do "pa1" and "pa2" represent the same function?
6370 * We first check if they are obviously equal.
6371 * If not, we convert them to maps and check if those are equal.
6373 * If "pa1" or "pa2" contain any NaNs, then they are considered
6374 * not to be the same. A NaN is not equal to anything, not even
6377 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6381 isl_map
*map1
, *map2
;
6386 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6387 if (equal
< 0 || equal
)
6389 has_nan
= isl_pw_aff_involves_nan(pa1
);
6390 if (has_nan
>= 0 && !has_nan
)
6391 has_nan
= isl_pw_aff_involves_nan(pa2
);
6397 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6398 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6399 equal
= isl_map_is_equal(map1
, map2
);
6406 /* Do "mpa1" and "mpa2" represent the same function?
6408 * Note that we cannot convert the entire isl_multi_pw_aff
6409 * to a map because the domains of the piecewise affine expressions
6410 * may not be the same.
6412 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6413 __isl_keep isl_multi_pw_aff
*mpa2
)
6419 return isl_bool_error
;
6421 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6422 mpa2
->space
, isl_dim_param
)) {
6423 if (!isl_space_has_named_params(mpa1
->space
))
6424 return isl_bool_false
;
6425 if (!isl_space_has_named_params(mpa2
->space
))
6426 return isl_bool_false
;
6427 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6428 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6429 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6430 isl_multi_pw_aff_get_space(mpa2
));
6431 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6432 isl_multi_pw_aff_get_space(mpa1
));
6433 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6434 isl_multi_pw_aff_free(mpa1
);
6435 isl_multi_pw_aff_free(mpa2
);
6439 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6440 if (equal
< 0 || !equal
)
6443 for (i
= 0; i
< mpa1
->n
; ++i
) {
6444 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6445 if (equal
< 0 || !equal
)
6449 return isl_bool_true
;
6452 /* Compute the pullback of "mpa" by the function represented by "ma".
6453 * In other words, plug in "ma" in "mpa".
6455 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6457 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6458 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6461 isl_space
*space
= NULL
;
6463 mpa
= isl_multi_pw_aff_cow(mpa
);
6467 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6468 isl_multi_pw_aff_get_space(mpa
));
6472 for (i
= 0; i
< mpa
->n
; ++i
) {
6473 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6474 isl_multi_aff_copy(ma
));
6479 isl_multi_aff_free(ma
);
6480 isl_space_free(mpa
->space
);
6484 isl_space_free(space
);
6485 isl_multi_pw_aff_free(mpa
);
6486 isl_multi_aff_free(ma
);
6490 /* Compute the pullback of "mpa" by the function represented by "ma".
6491 * In other words, plug in "ma" in "mpa".
6493 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6494 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6498 if (isl_space_match(mpa
->space
, isl_dim_param
,
6499 ma
->space
, isl_dim_param
))
6500 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6501 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6502 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6503 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6505 isl_multi_pw_aff_free(mpa
);
6506 isl_multi_aff_free(ma
);
6510 /* Compute the pullback of "mpa" by the function represented by "pma".
6511 * In other words, plug in "pma" in "mpa".
6513 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6515 static __isl_give isl_multi_pw_aff
*
6516 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6517 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6520 isl_space
*space
= NULL
;
6522 mpa
= isl_multi_pw_aff_cow(mpa
);
6526 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6527 isl_multi_pw_aff_get_space(mpa
));
6529 for (i
= 0; i
< mpa
->n
; ++i
) {
6530 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6531 isl_pw_multi_aff_copy(pma
));
6536 isl_pw_multi_aff_free(pma
);
6537 isl_space_free(mpa
->space
);
6541 isl_space_free(space
);
6542 isl_multi_pw_aff_free(mpa
);
6543 isl_pw_multi_aff_free(pma
);
6547 /* Compute the pullback of "mpa" by the function represented by "pma".
6548 * In other words, plug in "pma" in "mpa".
6550 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6551 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6555 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6556 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6557 mpa
= isl_multi_pw_aff_align_params(mpa
,
6558 isl_pw_multi_aff_get_space(pma
));
6559 pma
= isl_pw_multi_aff_align_params(pma
,
6560 isl_multi_pw_aff_get_space(mpa
));
6561 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6563 isl_multi_pw_aff_free(mpa
);
6564 isl_pw_multi_aff_free(pma
);
6568 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6569 * with the domain of "aff". The domain of the result is the same
6571 * "mpa" and "aff" are assumed to have been aligned.
6573 * We first extract the parametric constant from "aff", defined
6574 * over the correct domain.
6575 * Then we add the appropriate combinations of the members of "mpa".
6576 * Finally, we add the integer divisions through recursive calls.
6578 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6579 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6587 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6588 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6590 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6591 tmp
= isl_aff_copy(aff
);
6592 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6593 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6594 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6595 isl_space_dim(space
, isl_dim_set
));
6596 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6597 pa
= isl_pw_aff_from_aff(tmp
);
6599 for (i
= 0; i
< n_in
; ++i
) {
6602 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6604 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6605 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6606 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6607 pa
= isl_pw_aff_add(pa
, pa_i
);
6610 for (i
= 0; i
< n_div
; ++i
) {
6614 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6616 div
= isl_aff_get_div(aff
, i
);
6617 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6618 isl_multi_pw_aff_copy(mpa
), div
);
6619 pa_i
= isl_pw_aff_floor(pa_i
);
6620 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6621 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6622 pa
= isl_pw_aff_add(pa
, pa_i
);
6625 isl_multi_pw_aff_free(mpa
);
6631 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6632 * with the domain of "aff". The domain of the result is the same
6635 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6636 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6640 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6641 mpa
->space
, isl_dim_param
))
6642 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6644 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6645 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6647 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6650 isl_multi_pw_aff_free(mpa
);
6654 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6655 * with the domain of "pa". The domain of the result is the same
6657 * "mpa" and "pa" are assumed to have been aligned.
6659 * We consider each piece in turn. Note that the domains of the
6660 * pieces are assumed to be disjoint and they remain disjoint
6661 * after taking the preimage (over the same function).
6663 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6664 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6673 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6674 isl_pw_aff_get_space(pa
));
6675 res
= isl_pw_aff_empty(space
);
6677 for (i
= 0; i
< pa
->n
; ++i
) {
6681 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6682 isl_multi_pw_aff_copy(mpa
),
6683 isl_aff_copy(pa
->p
[i
].aff
));
6684 domain
= isl_set_copy(pa
->p
[i
].set
);
6685 domain
= isl_set_preimage_multi_pw_aff(domain
,
6686 isl_multi_pw_aff_copy(mpa
));
6687 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6688 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6691 isl_pw_aff_free(pa
);
6692 isl_multi_pw_aff_free(mpa
);
6695 isl_pw_aff_free(pa
);
6696 isl_multi_pw_aff_free(mpa
);
6700 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6701 * with the domain of "pa". The domain of the result is the same
6704 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6705 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6709 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6710 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6712 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6713 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6715 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6717 isl_pw_aff_free(pa
);
6718 isl_multi_pw_aff_free(mpa
);
6722 /* Compute the pullback of "pa" by the function represented by "mpa".
6723 * In other words, plug in "mpa" in "pa".
6724 * "pa" and "mpa" are assumed to have been aligned.
6726 * The pullback is computed by applying "pa" to "mpa".
6728 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6729 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6731 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6734 /* Compute the pullback of "pa" by the function represented by "mpa".
6735 * In other words, plug in "mpa" in "pa".
6737 * The pullback is computed by applying "pa" to "mpa".
6739 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6740 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6742 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6745 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6746 * In other words, plug in "mpa2" in "mpa1".
6748 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6750 * We pullback each member of "mpa1" in turn.
6752 static __isl_give isl_multi_pw_aff
*
6753 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6754 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6757 isl_space
*space
= NULL
;
6759 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6763 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6764 isl_multi_pw_aff_get_space(mpa1
));
6766 for (i
= 0; i
< mpa1
->n
; ++i
) {
6767 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6768 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6773 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6775 isl_multi_pw_aff_free(mpa2
);
6778 isl_space_free(space
);
6779 isl_multi_pw_aff_free(mpa1
);
6780 isl_multi_pw_aff_free(mpa2
);
6784 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6785 * In other words, plug in "mpa2" in "mpa1".
6787 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6788 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6790 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6791 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6794 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6795 * of "mpa1" and "mpa2" live in the same space, construct map space
6796 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6797 * with this map space as extract argument.
6799 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6800 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6801 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6802 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6805 isl_space
*space1
, *space2
;
6808 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6809 isl_multi_pw_aff_get_space(mpa2
));
6810 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6811 isl_multi_pw_aff_get_space(mpa1
));
6814 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6815 mpa2
->space
, isl_dim_out
);
6819 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6820 "range spaces don't match", goto error
);
6821 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6822 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6823 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6825 res
= order(mpa1
, mpa2
, space1
);
6826 isl_multi_pw_aff_free(mpa1
);
6827 isl_multi_pw_aff_free(mpa2
);
6830 isl_multi_pw_aff_free(mpa1
);
6831 isl_multi_pw_aff_free(mpa2
);
6835 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6836 * where the function values are equal. "space" is the space of the result.
6837 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6839 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6840 * in the sequences are equal.
6842 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6843 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6844 __isl_take isl_space
*space
)
6849 res
= isl_map_universe(space
);
6851 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6852 for (i
= 0; i
< n
; ++i
) {
6853 isl_pw_aff
*pa1
, *pa2
;
6856 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6857 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6858 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6859 res
= isl_map_intersect(res
, map
);
6865 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6866 * where the function values are equal.
6868 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6869 __isl_take isl_multi_pw_aff
*mpa2
)
6871 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6872 &isl_multi_pw_aff_eq_map_on_space
);
6875 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6876 * where the function values of "mpa1" is lexicographically satisfies "base"
6877 * compared to that of "mpa2". "space" is the space of the result.
6878 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6880 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6881 * if its i-th element satisfies "base" when compared to
6882 * the i-th element of "mpa2" while all previous elements are
6885 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6886 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6887 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6888 __isl_take isl_pw_aff
*pa2
),
6889 __isl_take isl_space
*space
)
6892 isl_map
*res
, *rest
;
6894 res
= isl_map_empty(isl_space_copy(space
));
6895 rest
= isl_map_universe(space
);
6897 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6898 for (i
= 0; i
< n
; ++i
) {
6899 isl_pw_aff
*pa1
, *pa2
;
6902 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6903 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6904 map
= base(pa1
, pa2
);
6905 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6906 res
= isl_map_union(res
, map
);
6911 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6912 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6913 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6914 rest
= isl_map_intersect(rest
, map
);
6921 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6922 * where the function value of "mpa1" is lexicographically less than that
6923 * of "mpa2". "space" is the space of the result.
6924 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6926 * "mpa1" is less than "mpa2" if its i-th element is smaller
6927 * than the i-th element of "mpa2" while all previous elements are
6930 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
6931 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6932 __isl_take isl_space
*space
)
6934 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6935 &isl_pw_aff_lt_map
, space
);
6938 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6939 * where the function value of "mpa1" is lexicographically less than that
6942 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
6943 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6945 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6946 &isl_multi_pw_aff_lex_lt_map_on_space
);
6949 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6950 * where the function value of "mpa1" is lexicographically greater than that
6951 * of "mpa2". "space" is the space of the result.
6952 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6954 * "mpa1" is greater than "mpa2" if its i-th element is greater
6955 * than the i-th element of "mpa2" while all previous elements are
6958 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
6959 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6960 __isl_take isl_space
*space
)
6962 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6963 &isl_pw_aff_gt_map
, space
);
6966 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6967 * where the function value of "mpa1" is lexicographically greater than that
6970 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
6971 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6973 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6974 &isl_multi_pw_aff_lex_gt_map_on_space
);
6977 /* Compare two isl_affs.
6979 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6980 * than "aff2" and 0 if they are equal.
6982 * The order is fairly arbitrary. We do consider expressions that only involve
6983 * earlier dimensions as "smaller".
6985 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6998 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7002 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7003 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7005 return last1
- last2
;
7007 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7010 /* Compare two isl_pw_affs.
7012 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7013 * than "pa2" and 0 if they are equal.
7015 * The order is fairly arbitrary. We do consider expressions that only involve
7016 * earlier dimensions as "smaller".
7018 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7019 __isl_keep isl_pw_aff
*pa2
)
7032 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7036 if (pa1
->n
!= pa2
->n
)
7037 return pa1
->n
- pa2
->n
;
7039 for (i
= 0; i
< pa1
->n
; ++i
) {
7040 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7043 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7051 /* Return a piecewise affine expression that is equal to "v" on "domain".
7053 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7054 __isl_take isl_val
*v
)
7057 isl_local_space
*ls
;
7060 space
= isl_set_get_space(domain
);
7061 ls
= isl_local_space_from_space(space
);
7062 aff
= isl_aff_val_on_domain(ls
, v
);
7064 return isl_pw_aff_alloc(domain
, aff
);
7067 /* Return a multi affine expression that is equal to "mv" on domain
7070 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7071 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7075 isl_local_space
*ls
;
7081 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7082 space2
= isl_multi_val_get_space(mv
);
7083 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7084 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7085 space
= isl_space_map_from_domain_and_range(space
, space2
);
7086 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7087 ls
= isl_local_space_from_space(isl_space_domain(space
));
7088 for (i
= 0; i
< n
; ++i
) {
7092 v
= isl_multi_val_get_val(mv
, i
);
7093 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7094 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7096 isl_local_space_free(ls
);
7098 isl_multi_val_free(mv
);
7101 isl_space_free(space
);
7102 isl_multi_val_free(mv
);
7106 /* Return a piecewise multi-affine expression
7107 * that is equal to "mv" on "domain".
7109 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7110 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7115 space
= isl_set_get_space(domain
);
7116 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7118 return isl_pw_multi_aff_alloc(domain
, ma
);
7121 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7122 * mv is the value that should be attained on each domain set
7123 * res collects the results
7125 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7127 isl_union_pw_multi_aff
*res
;
7130 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7131 * and add it to data->res.
7133 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7136 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7137 isl_pw_multi_aff
*pma
;
7140 mv
= isl_multi_val_copy(data
->mv
);
7141 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7142 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7144 return data
->res
? isl_stat_ok
: isl_stat_error
;
7147 /* Return a union piecewise multi-affine expression
7148 * that is equal to "mv" on "domain".
7150 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7151 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7153 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7156 space
= isl_union_set_get_space(domain
);
7157 data
.res
= isl_union_pw_multi_aff_empty(space
);
7159 if (isl_union_set_foreach_set(domain
,
7160 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7161 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7162 isl_union_set_free(domain
);
7163 isl_multi_val_free(mv
);
7167 /* Compute the pullback of data->pma by the function represented by "pma2",
7168 * provided the spaces match, and add the results to data->res.
7170 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7172 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7174 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7175 pma2
->dim
, isl_dim_out
)) {
7176 isl_pw_multi_aff_free(pma2
);
7180 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7181 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7183 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7185 return isl_stat_error
;
7190 /* Compute the pullback of "upma1" by the function represented by "upma2".
7192 __isl_give isl_union_pw_multi_aff
*
7193 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7194 __isl_take isl_union_pw_multi_aff
*upma1
,
7195 __isl_take isl_union_pw_multi_aff
*upma2
)
7197 return bin_op(upma1
, upma2
, &pullback_entry
);
7200 /* Check that the domain space of "upa" matches "space".
7202 * Return 0 on success and -1 on error.
7204 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7205 * can in principle never fail since the space "space" is that
7206 * of the isl_multi_union_pw_aff and is a set space such that
7207 * there is no domain space to match.
7209 * We check the parameters and double-check that "space" is
7210 * indeed that of a set.
7212 static int isl_union_pw_aff_check_match_domain_space(
7213 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7215 isl_space
*upa_space
;
7221 match
= isl_space_is_set(space
);
7225 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7226 "expecting set space", return -1);
7228 upa_space
= isl_union_pw_aff_get_space(upa
);
7229 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7233 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7234 "parameters don't match", goto error
);
7236 isl_space_free(upa_space
);
7239 isl_space_free(upa_space
);
7243 /* Do the parameters of "upa" match those of "space"?
7245 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7246 __isl_keep isl_space
*space
)
7248 isl_space
*upa_space
;
7254 upa_space
= isl_union_pw_aff_get_space(upa
);
7256 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7258 isl_space_free(upa_space
);
7262 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7263 * space represents the new parameters.
7264 * res collects the results.
7266 struct isl_union_pw_aff_reset_params_data
{
7268 isl_union_pw_aff
*res
;
7271 /* Replace the parameters of "pa" by data->space and
7272 * add the result to data->res.
7274 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7276 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7279 space
= isl_pw_aff_get_space(pa
);
7280 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7281 pa
= isl_pw_aff_reset_space(pa
, space
);
7282 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7284 return data
->res
? isl_stat_ok
: isl_stat_error
;
7287 /* Replace the domain space of "upa" by "space".
7288 * Since a union expression does not have a (single) domain space,
7289 * "space" is necessarily a parameter space.
7291 * Since the order and the names of the parameters determine
7292 * the hash value, we need to create a new hash table.
7294 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7295 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7297 struct isl_union_pw_aff_reset_params_data data
= { space
};
7300 match
= isl_union_pw_aff_matching_params(upa
, space
);
7302 upa
= isl_union_pw_aff_free(upa
);
7304 isl_space_free(space
);
7308 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7309 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7310 data
.res
= isl_union_pw_aff_free(data
.res
);
7312 isl_union_pw_aff_free(upa
);
7313 isl_space_free(space
);
7317 /* Return the floor of "pa".
7319 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7321 return isl_pw_aff_floor(pa
);
7324 /* Given f, return floor(f).
7326 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7327 __isl_take isl_union_pw_aff
*upa
)
7329 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7334 * upa mod m = upa - m * floor(upa/m)
7336 * with m an integer value.
7338 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7339 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7341 isl_union_pw_aff
*res
;
7346 if (!isl_val_is_int(m
))
7347 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7348 "expecting integer modulo", goto error
);
7349 if (!isl_val_is_pos(m
))
7350 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7351 "expecting positive modulo", goto error
);
7353 res
= isl_union_pw_aff_copy(upa
);
7354 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7355 upa
= isl_union_pw_aff_floor(upa
);
7356 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7357 res
= isl_union_pw_aff_sub(res
, upa
);
7362 isl_union_pw_aff_free(upa
);
7366 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7367 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7369 * "res" collects the results.
7371 struct isl_union_pw_aff_aff_on_domain_data
{
7373 isl_union_pw_aff
*res
;
7376 /* Construct a piecewise affine expression that is equal to data->aff
7377 * on "domain" and add the result to data->res.
7379 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7381 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7386 aff
= isl_aff_copy(data
->aff
);
7387 dim
= isl_set_dim(domain
, isl_dim_set
);
7388 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7389 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7390 pa
= isl_pw_aff_alloc(domain
, aff
);
7391 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7393 return data
->res
? isl_stat_ok
: isl_stat_error
;
7396 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7397 * pos is the output position that needs to be extracted.
7398 * res collects the results.
7400 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7402 isl_union_pw_aff
*res
;
7405 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7406 * (assuming it has such a dimension) and add it to data->res.
7408 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7410 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7415 return isl_stat_error
;
7417 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7418 if (data
->pos
>= n_out
) {
7419 isl_pw_multi_aff_free(pma
);
7423 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7424 isl_pw_multi_aff_free(pma
);
7426 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7428 return data
->res
? isl_stat_ok
: isl_stat_error
;
7431 /* Extract an isl_union_pw_aff corresponding to
7432 * output dimension "pos" of "upma".
7434 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7435 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7437 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7444 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7445 "cannot extract at negative position", return NULL
);
7447 space
= isl_union_pw_multi_aff_get_space(upma
);
7448 data
.res
= isl_union_pw_aff_empty(space
);
7450 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7451 &get_union_pw_aff
, &data
) < 0)
7452 data
.res
= isl_union_pw_aff_free(data
.res
);
7457 /* Return a union piecewise affine expression
7458 * that is equal to "aff" on "domain".
7460 * Construct an isl_pw_aff on each of the sets in "domain" and
7461 * collect the results.
7463 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7464 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7466 struct isl_union_pw_aff_aff_on_domain_data data
;
7469 if (!domain
|| !aff
)
7471 if (!isl_local_space_is_params(aff
->ls
))
7472 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7473 "expecting parametric expression", goto error
);
7475 space
= isl_union_set_get_space(domain
);
7476 data
.res
= isl_union_pw_aff_empty(space
);
7478 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7479 data
.res
= isl_union_pw_aff_free(data
.res
);
7480 isl_union_set_free(domain
);
7484 isl_union_set_free(domain
);
7489 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7490 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7491 * "res" collects the results.
7493 struct isl_union_pw_aff_val_on_domain_data
{
7495 isl_union_pw_aff
*res
;
7498 /* Construct a piecewise affine expression that is equal to data->v
7499 * on "domain" and add the result to data->res.
7501 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7503 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7507 v
= isl_val_copy(data
->v
);
7508 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7509 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7511 return data
->res
? isl_stat_ok
: isl_stat_error
;
7514 /* Return a union piecewise affine expression
7515 * that is equal to "v" on "domain".
7517 * Construct an isl_pw_aff on each of the sets in "domain" and
7518 * collect the results.
7520 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7521 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7523 struct isl_union_pw_aff_val_on_domain_data data
;
7526 space
= isl_union_set_get_space(domain
);
7527 data
.res
= isl_union_pw_aff_empty(space
);
7529 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7530 data
.res
= isl_union_pw_aff_free(data
.res
);
7531 isl_union_set_free(domain
);
7536 /* Construct a piecewise multi affine expression
7537 * that is equal to "pa" and add it to upma.
7539 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7542 isl_union_pw_multi_aff
**upma
= user
;
7543 isl_pw_multi_aff
*pma
;
7545 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7546 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7548 return *upma
? isl_stat_ok
: isl_stat_error
;
7551 /* Construct and return a union piecewise multi affine expression
7552 * that is equal to the given union piecewise affine expression.
7554 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7555 __isl_take isl_union_pw_aff
*upa
)
7558 isl_union_pw_multi_aff
*upma
;
7563 space
= isl_union_pw_aff_get_space(upa
);
7564 upma
= isl_union_pw_multi_aff_empty(space
);
7566 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7567 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7568 upma
= isl_union_pw_multi_aff_free(upma
);
7570 isl_union_pw_aff_free(upa
);
7574 /* Compute the set of elements in the domain of "pa" where it is zero and
7575 * add this set to "uset".
7577 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7579 isl_union_set
**uset
= (isl_union_set
**)user
;
7581 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7583 return *uset
? isl_stat_ok
: isl_stat_error
;
7586 /* Return a union set containing those elements in the domain
7587 * of "upa" where it is zero.
7589 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7590 __isl_take isl_union_pw_aff
*upa
)
7592 isl_union_set
*zero
;
7594 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7595 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7596 zero
= isl_union_set_free(zero
);
7598 isl_union_pw_aff_free(upa
);
7602 /* Convert "pa" to an isl_map and add it to *umap.
7604 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7606 isl_union_map
**umap
= user
;
7609 map
= isl_map_from_pw_aff(pa
);
7610 *umap
= isl_union_map_add_map(*umap
, map
);
7612 return *umap
? isl_stat_ok
: isl_stat_error
;
7615 /* Construct a union map mapping the domain of the union
7616 * piecewise affine expression to its range, with the single output dimension
7617 * equated to the corresponding affine expressions on their cells.
7619 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7620 __isl_take isl_union_pw_aff
*upa
)
7623 isl_union_map
*umap
;
7628 space
= isl_union_pw_aff_get_space(upa
);
7629 umap
= isl_union_map_empty(space
);
7631 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7633 umap
= isl_union_map_free(umap
);
7635 isl_union_pw_aff_free(upa
);
7639 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7640 * upma is the function that is plugged in.
7641 * pa is the current part of the function in which upma is plugged in.
7642 * res collects the results.
7644 struct isl_union_pw_aff_pullback_upma_data
{
7645 isl_union_pw_multi_aff
*upma
;
7647 isl_union_pw_aff
*res
;
7650 /* Check if "pma" can be plugged into data->pa.
7651 * If so, perform the pullback and add the result to data->res.
7653 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7655 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7658 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7659 pma
->dim
, isl_dim_out
)) {
7660 isl_pw_multi_aff_free(pma
);
7664 pa
= isl_pw_aff_copy(data
->pa
);
7665 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7667 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7669 return data
->res
? isl_stat_ok
: isl_stat_error
;
7672 /* Check if any of the elements of data->upma can be plugged into pa,
7673 * add if so add the result to data->res.
7675 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7677 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7681 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7683 isl_pw_aff_free(pa
);
7688 /* Compute the pullback of "upa" by the function represented by "upma".
7689 * In other words, plug in "upma" in "upa". The result contains
7690 * expressions defined over the domain space of "upma".
7692 * Run over all pairs of elements in "upa" and "upma", perform
7693 * the pullback when appropriate and collect the results.
7694 * If the hash value were based on the domain space rather than
7695 * the function space, then we could run through all elements
7696 * of "upma" and directly pick out the corresponding element of "upa".
7698 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7699 __isl_take isl_union_pw_aff
*upa
,
7700 __isl_take isl_union_pw_multi_aff
*upma
)
7702 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7705 space
= isl_union_pw_multi_aff_get_space(upma
);
7706 upa
= isl_union_pw_aff_align_params(upa
, space
);
7707 space
= isl_union_pw_aff_get_space(upa
);
7708 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7714 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7715 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7716 data
.res
= isl_union_pw_aff_free(data
.res
);
7718 isl_union_pw_aff_free(upa
);
7719 isl_union_pw_multi_aff_free(upma
);
7722 isl_union_pw_aff_free(upa
);
7723 isl_union_pw_multi_aff_free(upma
);
7728 #define BASE union_pw_aff
7730 #define DOMBASE union_set
7732 #define NO_MOVE_DIMS
7741 #include <isl_multi_templ.c>
7742 #include <isl_multi_apply_set.c>
7743 #include <isl_multi_apply_union_set.c>
7744 #include <isl_multi_coalesce.c>
7745 #include <isl_multi_floor.c>
7746 #include <isl_multi_gist.c>
7747 #include <isl_multi_intersect.c>
7749 /* Construct a multiple union piecewise affine expression
7750 * in the given space with value zero in each of the output dimensions.
7752 * Since there is no canonical zero value for
7753 * a union piecewise affine expression, we can only construct
7754 * zero-dimensional "zero" value.
7756 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7757 __isl_take isl_space
*space
)
7762 if (!isl_space_is_set(space
))
7763 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7764 "expecting set space", goto error
);
7765 if (isl_space_dim(space
, isl_dim_out
) != 0)
7766 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7767 "expecting 0D space", goto error
);
7769 return isl_multi_union_pw_aff_alloc(space
);
7771 isl_space_free(space
);
7775 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7776 * with the actual sum on the shared domain and
7777 * the defined expression on the symmetric difference of the domains.
7779 * We simply iterate over the elements in both arguments and
7780 * call isl_union_pw_aff_union_add on each of them.
7782 static __isl_give isl_multi_union_pw_aff
*
7783 isl_multi_union_pw_aff_union_add_aligned(
7784 __isl_take isl_multi_union_pw_aff
*mupa1
,
7785 __isl_take isl_multi_union_pw_aff
*mupa2
)
7787 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7788 &isl_union_pw_aff_union_add
);
7791 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7792 * with the actual sum on the shared domain and
7793 * the defined expression on the symmetric difference of the domains.
7795 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7796 __isl_take isl_multi_union_pw_aff
*mupa1
,
7797 __isl_take isl_multi_union_pw_aff
*mupa2
)
7799 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7800 &isl_multi_union_pw_aff_union_add_aligned
);
7803 /* Construct and return a multi union piecewise affine expression
7804 * that is equal to the given multi affine expression.
7806 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7807 __isl_take isl_multi_aff
*ma
)
7809 isl_multi_pw_aff
*mpa
;
7811 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7812 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7815 /* Construct and return a multi union piecewise affine expression
7816 * that is equal to the given multi piecewise affine expression.
7818 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7819 __isl_take isl_multi_pw_aff
*mpa
)
7823 isl_multi_union_pw_aff
*mupa
;
7828 space
= isl_multi_pw_aff_get_space(mpa
);
7829 space
= isl_space_range(space
);
7830 mupa
= isl_multi_union_pw_aff_alloc(space
);
7832 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7833 for (i
= 0; i
< n
; ++i
) {
7835 isl_union_pw_aff
*upa
;
7837 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7838 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7839 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7842 isl_multi_pw_aff_free(mpa
);
7847 /* Extract the range space of "pma" and assign it to *space.
7848 * If *space has already been set (through a previous call to this function),
7849 * then check that the range space is the same.
7851 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7853 isl_space
**space
= user
;
7854 isl_space
*pma_space
;
7857 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7858 isl_pw_multi_aff_free(pma
);
7861 return isl_stat_error
;
7867 equal
= isl_space_is_equal(pma_space
, *space
);
7868 isl_space_free(pma_space
);
7871 return isl_stat_error
;
7873 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7874 "range spaces not the same", return isl_stat_error
);
7878 /* Construct and return a multi union piecewise affine expression
7879 * that is equal to the given union piecewise multi affine expression.
7881 * In order to be able to perform the conversion, the input
7882 * needs to be non-empty and may only involve a single range space.
7884 __isl_give isl_multi_union_pw_aff
*
7885 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7886 __isl_take isl_union_pw_multi_aff
*upma
)
7888 isl_space
*space
= NULL
;
7889 isl_multi_union_pw_aff
*mupa
;
7894 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7895 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7896 "cannot extract range space from empty input",
7898 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7905 n
= isl_space_dim(space
, isl_dim_set
);
7906 mupa
= isl_multi_union_pw_aff_alloc(space
);
7908 for (i
= 0; i
< n
; ++i
) {
7909 isl_union_pw_aff
*upa
;
7911 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7912 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7915 isl_union_pw_multi_aff_free(upma
);
7918 isl_space_free(space
);
7919 isl_union_pw_multi_aff_free(upma
);
7923 /* Try and create an isl_multi_union_pw_aff that is equivalent
7924 * to the given isl_union_map.
7925 * The isl_union_map is required to be single-valued in each space.
7926 * Moreover, it cannot be empty and all range spaces need to be the same.
7927 * Otherwise, an error is produced.
7929 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7930 __isl_take isl_union_map
*umap
)
7932 isl_union_pw_multi_aff
*upma
;
7934 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7935 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7938 /* Return a multiple union piecewise affine expression
7939 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7940 * have been aligned.
7942 static __isl_give isl_multi_union_pw_aff
*
7943 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7944 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7948 isl_multi_union_pw_aff
*mupa
;
7953 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7954 space
= isl_multi_val_get_space(mv
);
7955 mupa
= isl_multi_union_pw_aff_alloc(space
);
7956 for (i
= 0; i
< n
; ++i
) {
7958 isl_union_pw_aff
*upa
;
7960 v
= isl_multi_val_get_val(mv
, i
);
7961 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
7963 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7966 isl_union_set_free(domain
);
7967 isl_multi_val_free(mv
);
7970 isl_union_set_free(domain
);
7971 isl_multi_val_free(mv
);
7975 /* Return a multiple union piecewise affine expression
7976 * that is equal to "mv" on "domain".
7978 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
7979 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7983 if (isl_space_match(domain
->dim
, isl_dim_param
,
7984 mv
->space
, isl_dim_param
))
7985 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7987 domain
= isl_union_set_align_params(domain
,
7988 isl_multi_val_get_space(mv
));
7989 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
7990 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
7992 isl_union_set_free(domain
);
7993 isl_multi_val_free(mv
);
7997 /* Return a multiple union piecewise affine expression
7998 * that is equal to "ma" on "domain", assuming "domain" and "ma"
7999 * have been aligned.
8001 static __isl_give isl_multi_union_pw_aff
*
8002 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8003 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8007 isl_multi_union_pw_aff
*mupa
;
8012 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8013 space
= isl_multi_aff_get_space(ma
);
8014 mupa
= isl_multi_union_pw_aff_alloc(space
);
8015 for (i
= 0; i
< n
; ++i
) {
8017 isl_union_pw_aff
*upa
;
8019 aff
= isl_multi_aff_get_aff(ma
, i
);
8020 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8022 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8025 isl_union_set_free(domain
);
8026 isl_multi_aff_free(ma
);
8029 isl_union_set_free(domain
);
8030 isl_multi_aff_free(ma
);
8034 /* Return a multiple union piecewise affine expression
8035 * that is equal to "ma" on "domain".
8037 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8038 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8042 if (isl_space_match(domain
->dim
, isl_dim_param
,
8043 ma
->space
, isl_dim_param
))
8044 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8046 domain
= isl_union_set_align_params(domain
,
8047 isl_multi_aff_get_space(ma
));
8048 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8049 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8051 isl_union_set_free(domain
);
8052 isl_multi_aff_free(ma
);
8056 /* Return a union set containing those elements in the domains
8057 * of the elements of "mupa" where they are all zero.
8059 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8060 __isl_take isl_multi_union_pw_aff
*mupa
)
8063 isl_union_pw_aff
*upa
;
8064 isl_union_set
*zero
;
8069 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8071 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8072 "cannot determine zero set "
8073 "of zero-dimensional function", goto error
);
8075 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8076 zero
= isl_union_pw_aff_zero_union_set(upa
);
8078 for (i
= 1; i
< n
; ++i
) {
8079 isl_union_set
*zero_i
;
8081 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8082 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8084 zero
= isl_union_set_intersect(zero
, zero_i
);
8087 isl_multi_union_pw_aff_free(mupa
);
8090 isl_multi_union_pw_aff_free(mupa
);
8094 /* Construct a union map mapping the shared domain
8095 * of the union piecewise affine expressions to the range of "mupa"
8096 * with each dimension in the range equated to the
8097 * corresponding union piecewise affine expression.
8099 * The input cannot be zero-dimensional as there is
8100 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8102 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8103 __isl_take isl_multi_union_pw_aff
*mupa
)
8107 isl_union_map
*umap
;
8108 isl_union_pw_aff
*upa
;
8113 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8115 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8116 "cannot determine domain of zero-dimensional "
8117 "isl_multi_union_pw_aff", goto error
);
8119 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8120 umap
= isl_union_map_from_union_pw_aff(upa
);
8122 for (i
= 1; i
< n
; ++i
) {
8123 isl_union_map
*umap_i
;
8125 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8126 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8127 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8130 space
= isl_multi_union_pw_aff_get_space(mupa
);
8131 umap
= isl_union_map_reset_range_space(umap
, space
);
8133 isl_multi_union_pw_aff_free(mupa
);
8136 isl_multi_union_pw_aff_free(mupa
);
8140 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8141 * "range" is the space from which to set the range space.
8142 * "res" collects the results.
8144 struct isl_union_pw_multi_aff_reset_range_space_data
{
8146 isl_union_pw_multi_aff
*res
;
8149 /* Replace the range space of "pma" by the range space of data->range and
8150 * add the result to data->res.
8152 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8154 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8157 space
= isl_pw_multi_aff_get_space(pma
);
8158 space
= isl_space_domain(space
);
8159 space
= isl_space_extend_domain_with_range(space
,
8160 isl_space_copy(data
->range
));
8161 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8162 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8164 return data
->res
? isl_stat_ok
: isl_stat_error
;
8167 /* Replace the range space of all the piecewise affine expressions in "upma" by
8168 * the range space of "space".
8170 * This assumes that all these expressions have the same output dimension.
8172 * Since the spaces of the expressions change, so do their hash values.
8173 * We therefore need to create a new isl_union_pw_multi_aff.
8174 * Note that the hash value is currently computed based on the entire
8175 * space even though there can only be a single expression with a given
8178 static __isl_give isl_union_pw_multi_aff
*
8179 isl_union_pw_multi_aff_reset_range_space(
8180 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8182 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8183 isl_space
*space_upma
;
8185 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8186 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8187 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8188 &reset_range_space
, &data
) < 0)
8189 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8191 isl_space_free(space
);
8192 isl_union_pw_multi_aff_free(upma
);
8196 /* Construct and return a union piecewise multi affine expression
8197 * that is equal to the given multi union piecewise affine expression.
8199 * In order to be able to perform the conversion, the input
8200 * needs to have a least one output dimension.
8202 __isl_give isl_union_pw_multi_aff
*
8203 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8204 __isl_take isl_multi_union_pw_aff
*mupa
)
8208 isl_union_pw_multi_aff
*upma
;
8209 isl_union_pw_aff
*upa
;
8214 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8216 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8217 "cannot determine domain of zero-dimensional "
8218 "isl_multi_union_pw_aff", goto error
);
8220 space
= isl_multi_union_pw_aff_get_space(mupa
);
8221 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8222 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8224 for (i
= 1; i
< n
; ++i
) {
8225 isl_union_pw_multi_aff
*upma_i
;
8227 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8228 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8229 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8232 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8234 isl_multi_union_pw_aff_free(mupa
);
8237 isl_multi_union_pw_aff_free(mupa
);
8241 /* Intersect the range of "mupa" with "range".
8242 * That is, keep only those domain elements that have a function value
8245 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8246 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8248 isl_union_pw_multi_aff
*upma
;
8249 isl_union_set
*domain
;
8254 if (!mupa
|| !range
)
8257 space
= isl_set_get_space(range
);
8258 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8259 space
, isl_dim_set
);
8260 isl_space_free(space
);
8264 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8265 "space don't match", goto error
);
8266 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8268 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8269 "cannot intersect range of zero-dimensional "
8270 "isl_multi_union_pw_aff", goto error
);
8272 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8273 isl_multi_union_pw_aff_copy(mupa
));
8274 domain
= isl_union_set_from_set(range
);
8275 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8276 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8280 isl_multi_union_pw_aff_free(mupa
);
8281 isl_set_free(range
);
8285 /* Return the shared domain of the elements of "mupa".
8287 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8288 __isl_take isl_multi_union_pw_aff
*mupa
)
8291 isl_union_pw_aff
*upa
;
8297 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8299 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8300 "cannot determine domain", goto error
);
8302 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8303 dom
= isl_union_pw_aff_domain(upa
);
8304 for (i
= 1; i
< n
; ++i
) {
8305 isl_union_set
*dom_i
;
8307 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8308 dom_i
= isl_union_pw_aff_domain(upa
);
8309 dom
= isl_union_set_intersect(dom
, dom_i
);
8312 isl_multi_union_pw_aff_free(mupa
);
8315 isl_multi_union_pw_aff_free(mupa
);
8319 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8320 * In particular, the spaces have been aligned.
8321 * The result is defined over the shared domain of the elements of "mupa"
8323 * We first extract the parametric constant part of "aff" and
8324 * define that over the shared domain.
8325 * Then we iterate over all input dimensions of "aff" and add the corresponding
8326 * multiples of the elements of "mupa".
8327 * Finally, we consider the integer divisions, calling the function
8328 * recursively to obtain an isl_union_pw_aff corresponding to the
8329 * integer division argument.
8331 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8332 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8335 isl_union_pw_aff
*upa
;
8336 isl_union_set
*uset
;
8340 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8341 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8343 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8344 cst
= isl_aff_copy(aff
);
8345 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8346 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8347 cst
= isl_aff_project_domain_on_params(cst
);
8348 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8350 for (i
= 0; i
< n_in
; ++i
) {
8351 isl_union_pw_aff
*upa_i
;
8353 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8355 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8356 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8357 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8358 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8361 for (i
= 0; i
< n_div
; ++i
) {
8363 isl_union_pw_aff
*upa_i
;
8365 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8367 div
= isl_aff_get_div(aff
, i
);
8368 upa_i
= multi_union_pw_aff_apply_aff(
8369 isl_multi_union_pw_aff_copy(mupa
), div
);
8370 upa_i
= isl_union_pw_aff_floor(upa_i
);
8371 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8372 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8373 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8376 isl_multi_union_pw_aff_free(mupa
);
8382 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8383 * with the domain of "aff".
8384 * Furthermore, the dimension of this space needs to be greater than zero.
8385 * The result is defined over the shared domain of the elements of "mupa"
8387 * We perform these checks and then hand over control to
8388 * multi_union_pw_aff_apply_aff.
8390 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8391 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8393 isl_space
*space1
, *space2
;
8396 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8397 isl_aff_get_space(aff
));
8398 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8402 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8403 space2
= isl_aff_get_domain_space(aff
);
8404 equal
= isl_space_is_equal(space1
, space2
);
8405 isl_space_free(space1
);
8406 isl_space_free(space2
);
8410 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8411 "spaces don't match", goto error
);
8412 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8413 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8414 "cannot determine domains", goto error
);
8416 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8418 isl_multi_union_pw_aff_free(mupa
);
8423 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8424 * with the domain of "ma".
8425 * Furthermore, the dimension of this space needs to be greater than zero,
8426 * unless the dimension of the target space of "ma" is also zero.
8427 * The result is defined over the shared domain of the elements of "mupa"
8429 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8430 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8432 isl_space
*space1
, *space2
;
8433 isl_multi_union_pw_aff
*res
;
8437 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8438 isl_multi_aff_get_space(ma
));
8439 ma
= isl_multi_aff_align_params(ma
,
8440 isl_multi_union_pw_aff_get_space(mupa
));
8444 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8445 space2
= isl_multi_aff_get_domain_space(ma
);
8446 equal
= isl_space_is_equal(space1
, space2
);
8447 isl_space_free(space1
);
8448 isl_space_free(space2
);
8452 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8453 "spaces don't match", goto error
);
8454 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8455 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8456 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8457 "cannot determine domains", goto error
);
8459 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8460 res
= isl_multi_union_pw_aff_alloc(space1
);
8462 for (i
= 0; i
< n_out
; ++i
) {
8464 isl_union_pw_aff
*upa
;
8466 aff
= isl_multi_aff_get_aff(ma
, i
);
8467 upa
= multi_union_pw_aff_apply_aff(
8468 isl_multi_union_pw_aff_copy(mupa
), aff
);
8469 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8472 isl_multi_aff_free(ma
);
8473 isl_multi_union_pw_aff_free(mupa
);
8476 isl_multi_union_pw_aff_free(mupa
);
8477 isl_multi_aff_free(ma
);
8481 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8482 * with the domain of "pa".
8483 * Furthermore, the dimension of this space needs to be greater than zero.
8484 * The result is defined over the shared domain of the elements of "mupa"
8486 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8487 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8491 isl_space
*space
, *space2
;
8492 isl_union_pw_aff
*upa
;
8494 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8495 isl_pw_aff_get_space(pa
));
8496 pa
= isl_pw_aff_align_params(pa
,
8497 isl_multi_union_pw_aff_get_space(mupa
));
8501 space
= isl_multi_union_pw_aff_get_space(mupa
);
8502 space2
= isl_pw_aff_get_domain_space(pa
);
8503 equal
= isl_space_is_equal(space
, space2
);
8504 isl_space_free(space
);
8505 isl_space_free(space2
);
8509 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8510 "spaces don't match", goto error
);
8511 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8512 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8513 "cannot determine domains", goto error
);
8515 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8516 upa
= isl_union_pw_aff_empty(space
);
8518 for (i
= 0; i
< pa
->n
; ++i
) {
8521 isl_multi_union_pw_aff
*mupa_i
;
8522 isl_union_pw_aff
*upa_i
;
8524 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8525 domain
= isl_set_copy(pa
->p
[i
].set
);
8526 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8527 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8528 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8529 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8532 isl_multi_union_pw_aff_free(mupa
);
8533 isl_pw_aff_free(pa
);
8536 isl_multi_union_pw_aff_free(mupa
);
8537 isl_pw_aff_free(pa
);
8541 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8542 * with the domain of "pma".
8543 * Furthermore, the dimension of this space needs to be greater than zero,
8544 * unless the dimension of the target space of "pma" is also zero.
8545 * The result is defined over the shared domain of the elements of "mupa"
8547 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8548 __isl_take isl_multi_union_pw_aff
*mupa
,
8549 __isl_take isl_pw_multi_aff
*pma
)
8551 isl_space
*space1
, *space2
;
8552 isl_multi_union_pw_aff
*res
;
8556 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8557 isl_pw_multi_aff_get_space(pma
));
8558 pma
= isl_pw_multi_aff_align_params(pma
,
8559 isl_multi_union_pw_aff_get_space(mupa
));
8563 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8564 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8565 equal
= isl_space_is_equal(space1
, space2
);
8566 isl_space_free(space1
);
8567 isl_space_free(space2
);
8571 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8572 "spaces don't match", goto error
);
8573 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8574 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8575 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8576 "cannot determine domains", goto error
);
8578 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8579 res
= isl_multi_union_pw_aff_alloc(space1
);
8581 for (i
= 0; i
< n_out
; ++i
) {
8583 isl_union_pw_aff
*upa
;
8585 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8586 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8587 isl_multi_union_pw_aff_copy(mupa
), pa
);
8588 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8591 isl_pw_multi_aff_free(pma
);
8592 isl_multi_union_pw_aff_free(mupa
);
8595 isl_multi_union_pw_aff_free(mupa
);
8596 isl_pw_multi_aff_free(pma
);
8600 /* Compute the pullback of "mupa" by the function represented by "upma".
8601 * In other words, plug in "upma" in "mupa". The result contains
8602 * expressions defined over the domain space of "upma".
8604 * Run over all elements of "mupa" and plug in "upma" in each of them.
8606 __isl_give isl_multi_union_pw_aff
*
8607 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8608 __isl_take isl_multi_union_pw_aff
*mupa
,
8609 __isl_take isl_union_pw_multi_aff
*upma
)
8613 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8614 isl_union_pw_multi_aff_get_space(upma
));
8615 upma
= isl_union_pw_multi_aff_align_params(upma
,
8616 isl_multi_union_pw_aff_get_space(mupa
));
8620 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8621 for (i
= 0; i
< n
; ++i
) {
8622 isl_union_pw_aff
*upa
;
8624 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8625 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8626 isl_union_pw_multi_aff_copy(upma
));
8627 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8630 isl_union_pw_multi_aff_free(upma
);
8633 isl_multi_union_pw_aff_free(mupa
);
8634 isl_union_pw_multi_aff_free(upma
);
8638 /* Extract the sequence of elements in "mupa" with domain space "space"
8639 * (ignoring parameters).
8641 * For the elements of "mupa" that are not defined on the specified space,
8642 * the corresponding element in the result is empty.
8644 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8645 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8648 isl_space
*space_mpa
= NULL
;
8649 isl_multi_pw_aff
*mpa
;
8651 if (!mupa
|| !space
)
8654 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8655 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8656 space
= isl_space_drop_dims(space
, isl_dim_param
,
8657 0, isl_space_dim(space
, isl_dim_param
));
8658 space
= isl_space_align_params(space
,
8659 isl_space_copy(space_mpa
));
8663 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8665 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8667 space
= isl_space_from_domain(space
);
8668 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8669 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8670 for (i
= 0; i
< n
; ++i
) {
8671 isl_union_pw_aff
*upa
;
8674 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8675 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8676 isl_space_copy(space
));
8677 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8678 isl_union_pw_aff_free(upa
);
8681 isl_space_free(space
);
8684 isl_space_free(space_mpa
);
8685 isl_space_free(space
);