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
)
1728 aff
= isl_aff_cow(aff
);
1732 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1733 new_n_div
= isl_mat_rows(div
);
1734 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1736 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1737 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1738 if (!aff
->v
|| !aff
->ls
)
1748 /* Add two affine expressions that live in the same local space.
1750 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1751 __isl_take isl_aff
*aff2
)
1755 aff1
= isl_aff_cow(aff1
);
1759 aff1
->v
= isl_vec_cow(aff1
->v
);
1765 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1766 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1767 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1768 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1769 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1770 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1771 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1783 /* Return the sum of "aff1" and "aff2".
1785 * If either of the two is NaN, then the result is NaN.
1787 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1788 __isl_take isl_aff
*aff2
)
1799 ctx
= isl_aff_get_ctx(aff1
);
1800 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1801 isl_die(ctx
, isl_error_invalid
,
1802 "spaces don't match", goto error
);
1804 if (isl_aff_is_nan(aff1
)) {
1808 if (isl_aff_is_nan(aff2
)) {
1813 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1814 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1815 if (n_div1
== 0 && n_div2
== 0)
1816 return add_expanded(aff1
, aff2
);
1818 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1819 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1820 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1823 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1824 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1825 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1829 return add_expanded(aff1
, aff2
);
1838 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1839 __isl_take isl_aff
*aff2
)
1841 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1844 /* Return the result of scaling "aff" by a factor of "f".
1846 * As a special case, f * NaN = NaN.
1848 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1854 if (isl_aff_is_nan(aff
))
1857 if (isl_int_is_one(f
))
1860 aff
= isl_aff_cow(aff
);
1863 aff
->v
= isl_vec_cow(aff
->v
);
1865 return isl_aff_free(aff
);
1867 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1868 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1873 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1874 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1875 isl_int_divexact(gcd
, f
, gcd
);
1876 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1882 /* Multiple "aff" by "v".
1884 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1885 __isl_take isl_val
*v
)
1890 if (isl_val_is_one(v
)) {
1895 if (!isl_val_is_rat(v
))
1896 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1897 "expecting rational factor", goto error
);
1899 aff
= isl_aff_scale(aff
, v
->n
);
1900 aff
= isl_aff_scale_down(aff
, v
->d
);
1910 /* Return the result of scaling "aff" down by a factor of "f".
1912 * As a special case, NaN/f = NaN.
1914 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1920 if (isl_aff_is_nan(aff
))
1923 if (isl_int_is_one(f
))
1926 aff
= isl_aff_cow(aff
);
1930 if (isl_int_is_zero(f
))
1931 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1932 "cannot scale down by zero", return isl_aff_free(aff
));
1934 aff
->v
= isl_vec_cow(aff
->v
);
1936 return isl_aff_free(aff
);
1939 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1940 isl_int_gcd(gcd
, gcd
, f
);
1941 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1942 isl_int_divexact(gcd
, f
, gcd
);
1943 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1949 /* Divide "aff" by "v".
1951 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1952 __isl_take isl_val
*v
)
1957 if (isl_val_is_one(v
)) {
1962 if (!isl_val_is_rat(v
))
1963 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1964 "expecting rational factor", goto error
);
1965 if (!isl_val_is_pos(v
))
1966 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1967 "factor needs to be positive", goto error
);
1969 aff
= isl_aff_scale(aff
, v
->d
);
1970 aff
= isl_aff_scale_down(aff
, v
->n
);
1980 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1988 isl_int_set_ui(v
, f
);
1989 aff
= isl_aff_scale_down(aff
, v
);
1995 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1996 enum isl_dim_type type
, unsigned pos
, const char *s
)
1998 aff
= isl_aff_cow(aff
);
2001 if (type
== isl_dim_out
)
2002 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2003 "cannot set name of output/set dimension",
2004 return isl_aff_free(aff
));
2005 if (type
== isl_dim_in
)
2007 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2009 return isl_aff_free(aff
);
2014 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2015 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2017 aff
= isl_aff_cow(aff
);
2020 if (type
== isl_dim_out
)
2021 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2022 "cannot set name of output/set dimension",
2024 if (type
== isl_dim_in
)
2026 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2028 return isl_aff_free(aff
);
2037 /* Replace the identifier of the input tuple of "aff" by "id".
2038 * type is currently required to be equal to isl_dim_in
2040 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2041 enum isl_dim_type type
, __isl_take isl_id
*id
)
2043 aff
= isl_aff_cow(aff
);
2046 if (type
!= isl_dim_out
)
2047 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2048 "cannot only set id of input tuple", goto error
);
2049 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2051 return isl_aff_free(aff
);
2060 /* Exploit the equalities in "eq" to simplify the affine expression
2061 * and the expressions of the integer divisions in the local space.
2062 * The integer divisions in this local space are assumed to appear
2063 * as regular dimensions in "eq".
2065 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2066 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2074 if (eq
->n_eq
== 0) {
2075 isl_basic_set_free(eq
);
2079 aff
= isl_aff_cow(aff
);
2083 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2084 isl_basic_set_copy(eq
));
2085 aff
->v
= isl_vec_cow(aff
->v
);
2086 if (!aff
->ls
|| !aff
->v
)
2089 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2091 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2092 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2093 if (j
< 0 || j
== 0 || j
>= total
)
2096 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2100 isl_basic_set_free(eq
);
2101 aff
= isl_aff_normalize(aff
);
2104 isl_basic_set_free(eq
);
2109 /* Exploit the equalities in "eq" to simplify the affine expression
2110 * and the expressions of the integer divisions in the local space.
2112 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2113 __isl_take isl_basic_set
*eq
)
2119 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2121 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2122 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2124 isl_basic_set_free(eq
);
2129 /* Look for equalities among the variables shared by context and aff
2130 * and the integer divisions of aff, if any.
2131 * The equalities are then used to eliminate coefficients and/or integer
2132 * divisions from aff.
2134 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2135 __isl_take isl_set
*context
)
2137 isl_basic_set
*hull
;
2142 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2144 isl_basic_set
*bset
;
2145 isl_local_space
*ls
;
2146 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2147 ls
= isl_aff_get_domain_local_space(aff
);
2148 bset
= isl_basic_set_from_local_space(ls
);
2149 bset
= isl_basic_set_lift(bset
);
2150 bset
= isl_basic_set_flatten(bset
);
2151 context
= isl_set_intersect(context
,
2152 isl_set_from_basic_set(bset
));
2155 hull
= isl_set_affine_hull(context
);
2156 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2159 isl_set_free(context
);
2163 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2164 __isl_take isl_set
*context
)
2166 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2167 dom_context
= isl_set_intersect_params(dom_context
, context
);
2168 return isl_aff_gist(aff
, dom_context
);
2171 /* Return a basic set containing those elements in the space
2172 * of aff where it is positive. "rational" should not be set.
2174 * If "aff" is NaN, then it is not positive.
2176 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2179 isl_constraint
*ineq
;
2180 isl_basic_set
*bset
;
2185 if (isl_aff_is_nan(aff
)) {
2186 isl_space
*space
= isl_aff_get_domain_space(aff
);
2188 return isl_basic_set_empty(space
);
2191 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2192 "rational sets not supported", goto error
);
2194 ineq
= isl_inequality_from_aff(aff
);
2195 c
= isl_constraint_get_constant_val(ineq
);
2196 c
= isl_val_sub_ui(c
, 1);
2197 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2199 bset
= isl_basic_set_from_constraint(ineq
);
2200 bset
= isl_basic_set_simplify(bset
);
2207 /* Return a basic set containing those elements in the space
2208 * of aff where it is non-negative.
2209 * If "rational" is set, then return a rational basic set.
2211 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2213 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2214 __isl_take isl_aff
*aff
, int rational
)
2216 isl_constraint
*ineq
;
2217 isl_basic_set
*bset
;
2221 if (isl_aff_is_nan(aff
)) {
2222 isl_space
*space
= isl_aff_get_domain_space(aff
);
2224 return isl_basic_set_empty(space
);
2227 ineq
= isl_inequality_from_aff(aff
);
2229 bset
= isl_basic_set_from_constraint(ineq
);
2231 bset
= isl_basic_set_set_rational(bset
);
2232 bset
= isl_basic_set_simplify(bset
);
2236 /* Return a basic set containing those elements in the space
2237 * of aff where it is non-negative.
2239 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2241 return aff_nonneg_basic_set(aff
, 0);
2244 /* Return a basic set containing those elements in the domain space
2245 * of aff where it is negative.
2247 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2249 aff
= isl_aff_neg(aff
);
2250 aff
= isl_aff_add_constant_num_si(aff
, -1);
2251 return isl_aff_nonneg_basic_set(aff
);
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is zero.
2256 * If "rational" is set, then return a rational basic set.
2258 * If "aff" is NaN, then it is not zero.
2260 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2263 isl_constraint
*ineq
;
2264 isl_basic_set
*bset
;
2268 if (isl_aff_is_nan(aff
)) {
2269 isl_space
*space
= isl_aff_get_domain_space(aff
);
2271 return isl_basic_set_empty(space
);
2274 ineq
= isl_equality_from_aff(aff
);
2276 bset
= isl_basic_set_from_constraint(ineq
);
2278 bset
= isl_basic_set_set_rational(bset
);
2279 bset
= isl_basic_set_simplify(bset
);
2283 /* Return a basic set containing those elements in the space
2284 * of aff where it is zero.
2286 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2288 return aff_zero_basic_set(aff
, 0);
2291 /* Return a basic set containing those elements in the shared space
2292 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2294 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2295 __isl_take isl_aff
*aff2
)
2297 aff1
= isl_aff_sub(aff1
, aff2
);
2299 return isl_aff_nonneg_basic_set(aff1
);
2302 /* Return a set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2305 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2306 __isl_take isl_aff
*aff2
)
2308 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2311 /* Return a basic set containing those elements in the shared space
2312 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2314 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2315 __isl_take isl_aff
*aff2
)
2317 return isl_aff_ge_basic_set(aff2
, aff1
);
2320 /* Return a set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2323 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2324 __isl_take isl_aff
*aff2
)
2326 return isl_aff_ge_set(aff2
, aff1
);
2329 /* Return a basic set containing those elements in the shared space
2330 * of aff1 and aff2 where aff1 and aff2 are equal.
2332 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2333 __isl_take isl_aff
*aff2
)
2335 aff1
= isl_aff_sub(aff1
, aff2
);
2337 return isl_aff_zero_basic_set(aff1
);
2340 /* Return a set containing those elements in the shared space
2341 * of aff1 and aff2 where aff1 and aff2 are equal.
2343 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2344 __isl_take isl_aff
*aff2
)
2346 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2349 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2350 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2352 aff1
= isl_aff_add(aff1
, aff2
);
2353 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2357 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2365 /* Check whether the given affine expression has non-zero coefficient
2366 * for any dimension in the given range or if any of these dimensions
2367 * appear with non-zero coefficients in any of the integer divisions
2368 * involved in the affine expression.
2370 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2371 enum isl_dim_type type
, unsigned first
, unsigned n
)
2376 isl_bool involves
= isl_bool_false
;
2379 return isl_bool_error
;
2381 return isl_bool_false
;
2383 ctx
= isl_aff_get_ctx(aff
);
2384 if (first
+ n
> isl_aff_dim(aff
, type
))
2385 isl_die(ctx
, isl_error_invalid
,
2386 "range out of bounds", return isl_bool_error
);
2388 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2392 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2393 for (i
= 0; i
< n
; ++i
)
2394 if (active
[first
+ i
]) {
2395 involves
= isl_bool_true
;
2404 return isl_bool_error
;
2407 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2408 enum isl_dim_type type
, unsigned first
, unsigned n
)
2414 if (type
== isl_dim_out
)
2415 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2416 "cannot drop output/set dimension",
2417 return isl_aff_free(aff
));
2418 if (type
== isl_dim_in
)
2420 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2423 ctx
= isl_aff_get_ctx(aff
);
2424 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2425 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2426 return isl_aff_free(aff
));
2428 aff
= isl_aff_cow(aff
);
2432 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2434 return isl_aff_free(aff
);
2436 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2437 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2439 return isl_aff_free(aff
);
2444 /* Project the domain of the affine expression onto its parameter space.
2445 * The affine expression may not involve any of the domain dimensions.
2447 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2453 n
= isl_aff_dim(aff
, isl_dim_in
);
2454 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2456 return isl_aff_free(aff
);
2458 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2459 "affine expression involves some of the domain dimensions",
2460 return isl_aff_free(aff
));
2461 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2462 space
= isl_aff_get_domain_space(aff
);
2463 space
= isl_space_params(space
);
2464 aff
= isl_aff_reset_domain_space(aff
, space
);
2468 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2469 enum isl_dim_type type
, unsigned first
, unsigned n
)
2475 if (type
== isl_dim_out
)
2476 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2477 "cannot insert output/set dimensions",
2478 return isl_aff_free(aff
));
2479 if (type
== isl_dim_in
)
2481 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2484 ctx
= isl_aff_get_ctx(aff
);
2485 if (first
> isl_local_space_dim(aff
->ls
, type
))
2486 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2487 return isl_aff_free(aff
));
2489 aff
= isl_aff_cow(aff
);
2493 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2495 return isl_aff_free(aff
);
2497 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2498 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2500 return isl_aff_free(aff
);
2505 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2506 enum isl_dim_type type
, unsigned n
)
2510 pos
= isl_aff_dim(aff
, type
);
2512 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2515 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2516 enum isl_dim_type type
, unsigned n
)
2520 pos
= isl_pw_aff_dim(pwaff
, type
);
2522 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2525 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2526 * to dimensions of "dst_type" at "dst_pos".
2528 * We only support moving input dimensions to parameters and vice versa.
2530 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2531 enum isl_dim_type dst_type
, unsigned dst_pos
,
2532 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2540 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2541 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2544 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2545 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2546 "cannot move output/set dimension",
2547 return isl_aff_free(aff
));
2548 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2549 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2550 "cannot move divs", return isl_aff_free(aff
));
2551 if (dst_type
== isl_dim_in
)
2552 dst_type
= isl_dim_set
;
2553 if (src_type
== isl_dim_in
)
2554 src_type
= isl_dim_set
;
2556 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2557 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2558 "range out of bounds", return isl_aff_free(aff
));
2559 if (dst_type
== src_type
)
2560 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2561 "moving dims within the same type not supported",
2562 return isl_aff_free(aff
));
2564 aff
= isl_aff_cow(aff
);
2568 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2569 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2570 if (dst_type
> src_type
)
2573 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2574 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2575 src_type
, src_pos
, n
);
2576 if (!aff
->v
|| !aff
->ls
)
2577 return isl_aff_free(aff
);
2579 aff
= sort_divs(aff
);
2584 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2586 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2587 return isl_pw_aff_alloc(dom
, aff
);
2591 #define PW isl_pw_aff
2595 #define EL_IS_ZERO is_empty
2599 #define IS_ZERO is_empty
2602 #undef DEFAULT_IS_ZERO
2603 #define DEFAULT_IS_ZERO 0
2610 #include <isl_pw_templ.c>
2611 #include <isl_pw_hash.c>
2612 #include <isl_pw_union_opt.c>
2615 #define UNION isl_union_pw_aff
2617 #define PART isl_pw_aff
2619 #define PARTS pw_aff
2621 #include <isl_union_single.c>
2622 #include <isl_union_neg.c>
2624 static __isl_give isl_set
*align_params_pw_pw_set_and(
2625 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2626 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2627 __isl_take isl_pw_aff
*pwaff2
))
2629 if (!pwaff1
|| !pwaff2
)
2631 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2632 pwaff2
->dim
, isl_dim_param
))
2633 return fn(pwaff1
, pwaff2
);
2634 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2635 !isl_space_has_named_params(pwaff2
->dim
))
2636 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2637 "unaligned unnamed parameters", goto error
);
2638 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2639 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2640 return fn(pwaff1
, pwaff2
);
2642 isl_pw_aff_free(pwaff1
);
2643 isl_pw_aff_free(pwaff2
);
2647 /* Align the parameters of the to isl_pw_aff arguments and
2648 * then apply a function "fn" on them that returns an isl_map.
2650 static __isl_give isl_map
*align_params_pw_pw_map_and(
2651 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2652 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2653 __isl_take isl_pw_aff
*pa2
))
2657 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2658 return fn(pa1
, pa2
);
2659 if (!isl_space_has_named_params(pa1
->dim
) ||
2660 !isl_space_has_named_params(pa2
->dim
))
2661 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2662 "unaligned unnamed parameters", goto error
);
2663 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2664 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2665 return fn(pa1
, pa2
);
2667 isl_pw_aff_free(pa1
);
2668 isl_pw_aff_free(pa2
);
2672 /* Compute a piecewise quasi-affine expression with a domain that
2673 * is the union of those of pwaff1 and pwaff2 and such that on each
2674 * cell, the quasi-affine expression is the maximum of those of pwaff1
2675 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2676 * cell, then the associated expression is the defined one.
2678 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2679 __isl_take isl_pw_aff
*pwaff2
)
2681 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2684 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2685 __isl_take isl_pw_aff
*pwaff2
)
2687 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2691 /* Compute a piecewise quasi-affine expression with a domain that
2692 * is the union of those of pwaff1 and pwaff2 and such that on each
2693 * cell, the quasi-affine expression is the minimum of those of pwaff1
2694 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2695 * cell, then the associated expression is the defined one.
2697 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2698 __isl_take isl_pw_aff
*pwaff2
)
2700 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2703 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2704 __isl_take isl_pw_aff
*pwaff2
)
2706 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2710 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2711 __isl_take isl_pw_aff
*pwaff2
, int max
)
2714 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2716 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2719 /* Construct a map with as domain the domain of pwaff and
2720 * one-dimensional range corresponding to the affine expressions.
2722 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2731 dim
= isl_pw_aff_get_space(pwaff
);
2732 map
= isl_map_empty(dim
);
2734 for (i
= 0; i
< pwaff
->n
; ++i
) {
2735 isl_basic_map
*bmap
;
2738 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2739 map_i
= isl_map_from_basic_map(bmap
);
2740 map_i
= isl_map_intersect_domain(map_i
,
2741 isl_set_copy(pwaff
->p
[i
].set
));
2742 map
= isl_map_union_disjoint(map
, map_i
);
2745 isl_pw_aff_free(pwaff
);
2750 /* Construct a map with as domain the domain of pwaff and
2751 * one-dimensional range corresponding to the affine expressions.
2753 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2757 if (isl_space_is_set(pwaff
->dim
))
2758 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2759 "space of input is not a map", goto error
);
2760 return map_from_pw_aff(pwaff
);
2762 isl_pw_aff_free(pwaff
);
2766 /* Construct a one-dimensional set with as parameter domain
2767 * the domain of pwaff and the single set dimension
2768 * corresponding to the affine expressions.
2770 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2774 if (!isl_space_is_set(pwaff
->dim
))
2775 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2776 "space of input is not a set", goto error
);
2777 return map_from_pw_aff(pwaff
);
2779 isl_pw_aff_free(pwaff
);
2783 /* Return a set containing those elements in the domain
2784 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2785 * does not satisfy "fn" (if complement is 1).
2787 * The pieces with a NaN never belong to the result since
2788 * NaN does not satisfy any property.
2790 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2791 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2800 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2802 for (i
= 0; i
< pwaff
->n
; ++i
) {
2803 isl_basic_set
*bset
;
2804 isl_set
*set_i
, *locus
;
2807 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2810 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2811 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2812 locus
= isl_set_from_basic_set(bset
);
2813 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2815 set_i
= isl_set_subtract(set_i
, locus
);
2817 set_i
= isl_set_intersect(set_i
, locus
);
2818 set
= isl_set_union_disjoint(set
, set_i
);
2821 isl_pw_aff_free(pwaff
);
2826 /* Return a set containing those elements in the domain
2827 * of "pa" where it is positive.
2829 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2831 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2834 /* Return a set containing those elements in the domain
2835 * of pwaff where it is non-negative.
2837 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2839 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2842 /* Return a set containing those elements in the domain
2843 * of pwaff where it is zero.
2845 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2847 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2850 /* Return a set containing those elements in the domain
2851 * of pwaff where it is not zero.
2853 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2855 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2858 /* Return a set containing those elements in the shared domain
2859 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2861 * We compute the difference on the shared domain and then construct
2862 * the set of values where this difference is non-negative.
2863 * If strict is set, we first subtract 1 from the difference.
2864 * If equal is set, we only return the elements where pwaff1 and pwaff2
2867 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2868 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2870 isl_set
*set1
, *set2
;
2872 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2873 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2874 set1
= isl_set_intersect(set1
, set2
);
2875 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2876 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2877 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2880 isl_space
*dim
= isl_set_get_space(set1
);
2882 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2883 aff
= isl_aff_add_constant_si(aff
, -1);
2884 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2889 return isl_pw_aff_zero_set(pwaff1
);
2890 return isl_pw_aff_nonneg_set(pwaff1
);
2893 /* Return a set containing those elements in the shared domain
2894 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2896 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2897 __isl_take isl_pw_aff
*pwaff2
)
2899 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2902 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2903 __isl_take isl_pw_aff
*pwaff2
)
2905 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2908 /* Return a set containing those elements in the shared domain
2909 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2911 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2912 __isl_take isl_pw_aff
*pwaff2
)
2914 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2917 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2918 __isl_take isl_pw_aff
*pwaff2
)
2920 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2923 /* Return a set containing those elements in the shared domain
2924 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2926 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2927 __isl_take isl_pw_aff
*pwaff2
)
2929 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2932 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2933 __isl_take isl_pw_aff
*pwaff2
)
2935 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2938 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2939 __isl_take isl_pw_aff
*pwaff2
)
2941 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2944 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2945 __isl_take isl_pw_aff
*pwaff2
)
2947 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2950 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2951 * where the function values are ordered in the same way as "order",
2952 * which returns a set in the shared domain of its two arguments.
2953 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2955 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2956 * We first pull back the two functions such that they are defined on
2957 * the domain [A -> B]. Then we apply "order", resulting in a set
2958 * in the space [A -> B]. Finally, we unwrap this set to obtain
2959 * a map in the space A -> B.
2961 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2962 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2963 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2964 __isl_take isl_pw_aff
*pa2
))
2966 isl_space
*space1
, *space2
;
2970 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2971 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2972 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2973 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2974 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2975 ma
= isl_multi_aff_range_map(space1
);
2976 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2977 set
= order(pa1
, pa2
);
2979 return isl_set_unwrap(set
);
2982 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2983 * where the function values are equal.
2984 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2986 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2987 __isl_take isl_pw_aff
*pa2
)
2989 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2992 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2993 * where the function values are equal.
2995 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2996 __isl_take isl_pw_aff
*pa2
)
2998 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3001 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3002 * where the function value of "pa1" is less than the function value of "pa2".
3003 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3005 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3006 __isl_take isl_pw_aff
*pa2
)
3008 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3011 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3012 * where the function value of "pa1" is less than the function value of "pa2".
3014 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3015 __isl_take isl_pw_aff
*pa2
)
3017 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function value of "pa1" is greater than the function value
3023 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3025 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3026 __isl_take isl_pw_aff
*pa2
)
3028 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3031 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3032 * where the function value of "pa1" is greater than the function value
3035 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3036 __isl_take isl_pw_aff
*pa2
)
3038 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3041 /* Return a set containing those elements in the shared domain
3042 * of the elements of list1 and list2 where each element in list1
3043 * has the relation specified by "fn" with each element in list2.
3045 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3046 __isl_take isl_pw_aff_list
*list2
,
3047 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3048 __isl_take isl_pw_aff
*pwaff2
))
3054 if (!list1
|| !list2
)
3057 ctx
= isl_pw_aff_list_get_ctx(list1
);
3058 if (list1
->n
< 1 || list2
->n
< 1)
3059 isl_die(ctx
, isl_error_invalid
,
3060 "list should contain at least one element", goto error
);
3062 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3063 for (i
= 0; i
< list1
->n
; ++i
)
3064 for (j
= 0; j
< list2
->n
; ++j
) {
3067 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3068 isl_pw_aff_copy(list2
->p
[j
]));
3069 set
= isl_set_intersect(set
, set_ij
);
3072 isl_pw_aff_list_free(list1
);
3073 isl_pw_aff_list_free(list2
);
3076 isl_pw_aff_list_free(list1
);
3077 isl_pw_aff_list_free(list2
);
3081 /* Return a set containing those elements in the shared domain
3082 * of the elements of list1 and list2 where each element in list1
3083 * is equal to each element in list2.
3085 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3086 __isl_take isl_pw_aff_list
*list2
)
3088 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3091 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3092 __isl_take isl_pw_aff_list
*list2
)
3094 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3097 /* Return a set containing those elements in the shared domain
3098 * of the elements of list1 and list2 where each element in list1
3099 * is less than or equal to each element in list2.
3101 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3102 __isl_take isl_pw_aff_list
*list2
)
3104 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3107 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3108 __isl_take isl_pw_aff_list
*list2
)
3110 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3113 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3114 __isl_take isl_pw_aff_list
*list2
)
3116 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3119 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3120 __isl_take isl_pw_aff_list
*list2
)
3122 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3126 /* Return a set containing those elements in the shared domain
3127 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3129 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3130 __isl_take isl_pw_aff
*pwaff2
)
3132 isl_set
*set_lt
, *set_gt
;
3134 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3135 isl_pw_aff_copy(pwaff2
));
3136 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3137 return isl_set_union_disjoint(set_lt
, set_gt
);
3140 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3141 __isl_take isl_pw_aff
*pwaff2
)
3143 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3146 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3151 if (isl_int_is_one(v
))
3153 if (!isl_int_is_pos(v
))
3154 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3155 "factor needs to be positive",
3156 return isl_pw_aff_free(pwaff
));
3157 pwaff
= isl_pw_aff_cow(pwaff
);
3163 for (i
= 0; i
< pwaff
->n
; ++i
) {
3164 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3165 if (!pwaff
->p
[i
].aff
)
3166 return isl_pw_aff_free(pwaff
);
3172 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3176 pwaff
= isl_pw_aff_cow(pwaff
);
3182 for (i
= 0; i
< pwaff
->n
; ++i
) {
3183 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3184 if (!pwaff
->p
[i
].aff
)
3185 return isl_pw_aff_free(pwaff
);
3191 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3195 pwaff
= isl_pw_aff_cow(pwaff
);
3201 for (i
= 0; i
< pwaff
->n
; ++i
) {
3202 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3203 if (!pwaff
->p
[i
].aff
)
3204 return isl_pw_aff_free(pwaff
);
3210 /* Assuming that "cond1" and "cond2" are disjoint,
3211 * return an affine expression that is equal to pwaff1 on cond1
3212 * and to pwaff2 on cond2.
3214 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3215 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3216 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3218 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3219 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3221 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3224 /* Return an affine expression that is equal to pwaff_true for elements
3225 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3227 * That is, return cond ? pwaff_true : pwaff_false;
3229 * If "cond" involves and NaN, then we conservatively return a NaN
3230 * on its entire domain. In principle, we could consider the pieces
3231 * where it is NaN separately from those where it is not.
3233 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3234 * then only use the domain of "cond" to restrict the domain.
3236 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3237 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3239 isl_set
*cond_true
, *cond_false
;
3244 if (isl_pw_aff_involves_nan(cond
)) {
3245 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3246 isl_local_space
*ls
= isl_local_space_from_space(space
);
3247 isl_pw_aff_free(cond
);
3248 isl_pw_aff_free(pwaff_true
);
3249 isl_pw_aff_free(pwaff_false
);
3250 return isl_pw_aff_nan_on_domain(ls
);
3253 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3254 isl_pw_aff_get_space(pwaff_false
));
3255 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3256 isl_pw_aff_get_space(pwaff_true
));
3257 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3263 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3264 isl_pw_aff_free(pwaff_false
);
3265 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3268 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3269 cond_false
= isl_pw_aff_zero_set(cond
);
3270 return isl_pw_aff_select(cond_true
, pwaff_true
,
3271 cond_false
, pwaff_false
);
3273 isl_pw_aff_free(cond
);
3274 isl_pw_aff_free(pwaff_true
);
3275 isl_pw_aff_free(pwaff_false
);
3279 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3282 return isl_bool_error
;
3284 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3287 /* Check whether pwaff is a piecewise constant.
3289 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3294 return isl_bool_error
;
3296 for (i
= 0; i
< pwaff
->n
; ++i
) {
3297 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3298 if (is_cst
< 0 || !is_cst
)
3302 return isl_bool_true
;
3305 /* Are all elements of "mpa" piecewise constants?
3307 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3312 return isl_bool_error
;
3314 for (i
= 0; i
< mpa
->n
; ++i
) {
3315 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3316 if (is_cst
< 0 || !is_cst
)
3320 return isl_bool_true
;
3323 /* Return the product of "aff1" and "aff2".
3325 * If either of the two is NaN, then the result is NaN.
3327 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3329 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3330 __isl_take isl_aff
*aff2
)
3335 if (isl_aff_is_nan(aff1
)) {
3339 if (isl_aff_is_nan(aff2
)) {
3344 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3345 return isl_aff_mul(aff2
, aff1
);
3347 if (!isl_aff_is_cst(aff2
))
3348 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3349 "at least one affine expression should be constant",
3352 aff1
= isl_aff_cow(aff1
);
3356 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3357 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3367 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3369 * If either of the two is NaN, then the result is NaN.
3371 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3372 __isl_take isl_aff
*aff2
)
3380 if (isl_aff_is_nan(aff1
)) {
3384 if (isl_aff_is_nan(aff2
)) {
3389 is_cst
= isl_aff_is_cst(aff2
);
3393 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3394 "second argument should be a constant", goto error
);
3399 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3401 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3402 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3405 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3406 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3409 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3410 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3421 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3422 __isl_take isl_pw_aff
*pwaff2
)
3424 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3427 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3428 __isl_take isl_pw_aff
*pwaff2
)
3430 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3433 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3434 __isl_take isl_pw_aff
*pwaff2
)
3436 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3439 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3440 __isl_take isl_pw_aff
*pwaff2
)
3442 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3445 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3446 __isl_take isl_pw_aff
*pwaff2
)
3448 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3451 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3452 __isl_take isl_pw_aff
*pa2
)
3454 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3457 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3459 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3460 __isl_take isl_pw_aff
*pa2
)
3464 is_cst
= isl_pw_aff_is_cst(pa2
);
3468 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3469 "second argument should be a piecewise constant",
3471 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3473 isl_pw_aff_free(pa1
);
3474 isl_pw_aff_free(pa2
);
3478 /* Compute the quotient of the integer division of "pa1" by "pa2"
3479 * with rounding towards zero.
3480 * "pa2" is assumed to be a piecewise constant.
3482 * In particular, return
3484 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3487 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3488 __isl_take isl_pw_aff
*pa2
)
3494 is_cst
= isl_pw_aff_is_cst(pa2
);
3498 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3499 "second argument should be a piecewise constant",
3502 pa1
= isl_pw_aff_div(pa1
, pa2
);
3504 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3505 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3506 c
= isl_pw_aff_ceil(pa1
);
3507 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3509 isl_pw_aff_free(pa1
);
3510 isl_pw_aff_free(pa2
);
3514 /* Compute the remainder of the integer division of "pa1" by "pa2"
3515 * with rounding towards zero.
3516 * "pa2" is assumed to be a piecewise constant.
3518 * In particular, return
3520 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3523 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3524 __isl_take isl_pw_aff
*pa2
)
3529 is_cst
= isl_pw_aff_is_cst(pa2
);
3533 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3534 "second argument should be a piecewise constant",
3536 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3537 res
= isl_pw_aff_mul(pa2
, res
);
3538 res
= isl_pw_aff_sub(pa1
, res
);
3541 isl_pw_aff_free(pa1
);
3542 isl_pw_aff_free(pa2
);
3546 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3547 __isl_take isl_pw_aff
*pwaff2
)
3552 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3553 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3554 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3555 isl_pw_aff_copy(pwaff2
));
3556 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3557 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3560 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3561 __isl_take isl_pw_aff
*pwaff2
)
3563 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3566 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3567 __isl_take isl_pw_aff
*pwaff2
)
3572 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3573 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3574 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3575 isl_pw_aff_copy(pwaff2
));
3576 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3577 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3580 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3581 __isl_take isl_pw_aff
*pwaff2
)
3583 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3586 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3587 __isl_take isl_pw_aff_list
*list
,
3588 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3589 __isl_take isl_pw_aff
*pwaff2
))
3598 ctx
= isl_pw_aff_list_get_ctx(list
);
3600 isl_die(ctx
, isl_error_invalid
,
3601 "list should contain at least one element", goto error
);
3603 res
= isl_pw_aff_copy(list
->p
[0]);
3604 for (i
= 1; i
< list
->n
; ++i
)
3605 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3607 isl_pw_aff_list_free(list
);
3610 isl_pw_aff_list_free(list
);
3614 /* Return an isl_pw_aff that maps each element in the intersection of the
3615 * domains of the elements of list to the minimal corresponding affine
3618 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3620 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3623 /* Return an isl_pw_aff that maps each element in the intersection of the
3624 * domains of the elements of list to the maximal corresponding affine
3627 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3629 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3632 /* Mark the domains of "pwaff" as rational.
3634 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3638 pwaff
= isl_pw_aff_cow(pwaff
);
3644 for (i
= 0; i
< pwaff
->n
; ++i
) {
3645 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3646 if (!pwaff
->p
[i
].set
)
3647 return isl_pw_aff_free(pwaff
);
3653 /* Mark the domains of the elements of "list" as rational.
3655 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3656 __isl_take isl_pw_aff_list
*list
)
3666 for (i
= 0; i
< n
; ++i
) {
3669 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3670 pa
= isl_pw_aff_set_rational(pa
);
3671 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3677 /* Do the parameters of "aff" match those of "space"?
3679 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3680 __isl_keep isl_space
*space
)
3682 isl_space
*aff_space
;
3688 aff_space
= isl_aff_get_domain_space(aff
);
3690 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3692 isl_space_free(aff_space
);
3696 /* Check that the domain space of "aff" matches "space".
3698 * Return 0 on success and -1 on error.
3700 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3701 __isl_keep isl_space
*space
)
3703 isl_space
*aff_space
;
3709 aff_space
= isl_aff_get_domain_space(aff
);
3711 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3715 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3716 "parameters don't match", goto error
);
3717 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3718 aff_space
, isl_dim_set
);
3722 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3723 "domains don't match", goto error
);
3724 isl_space_free(aff_space
);
3727 isl_space_free(aff_space
);
3737 #include <isl_multi_templ.c>
3738 #include <isl_multi_apply_set.c>
3739 #include <isl_multi_cmp.c>
3740 #include <isl_multi_floor.c>
3741 #include <isl_multi_gist.c>
3745 /* Remove any internal structure of the domain of "ma".
3746 * If there is any such internal structure in the input,
3747 * then the name of the corresponding space is also removed.
3749 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3750 __isl_take isl_multi_aff
*ma
)
3757 if (!ma
->space
->nested
[0])
3760 space
= isl_multi_aff_get_space(ma
);
3761 space
= isl_space_flatten_domain(space
);
3762 ma
= isl_multi_aff_reset_space(ma
, space
);
3767 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3768 * of the space to its domain.
3770 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3773 isl_local_space
*ls
;
3778 if (!isl_space_is_map(space
))
3779 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3780 "not a map space", goto error
);
3782 n_in
= isl_space_dim(space
, isl_dim_in
);
3783 space
= isl_space_domain_map(space
);
3785 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3787 isl_space_free(space
);
3791 space
= isl_space_domain(space
);
3792 ls
= isl_local_space_from_space(space
);
3793 for (i
= 0; i
< n_in
; ++i
) {
3796 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3798 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3800 isl_local_space_free(ls
);
3803 isl_space_free(space
);
3807 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3808 * of the space to its range.
3810 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3813 isl_local_space
*ls
;
3818 if (!isl_space_is_map(space
))
3819 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3820 "not a map space", goto error
);
3822 n_in
= isl_space_dim(space
, isl_dim_in
);
3823 n_out
= isl_space_dim(space
, isl_dim_out
);
3824 space
= isl_space_range_map(space
);
3826 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3828 isl_space_free(space
);
3832 space
= isl_space_domain(space
);
3833 ls
= isl_local_space_from_space(space
);
3834 for (i
= 0; i
< n_out
; ++i
) {
3837 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3838 isl_dim_set
, n_in
+ i
);
3839 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3841 isl_local_space_free(ls
);
3844 isl_space_free(space
);
3848 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3849 * of the space to its range.
3851 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3852 __isl_take isl_space
*space
)
3854 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3857 /* Given the space of a set and a range of set dimensions,
3858 * construct an isl_multi_aff that projects out those dimensions.
3860 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3861 __isl_take isl_space
*space
, enum isl_dim_type type
,
3862 unsigned first
, unsigned n
)
3865 isl_local_space
*ls
;
3870 if (!isl_space_is_set(space
))
3871 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3872 "expecting set space", goto error
);
3873 if (type
!= isl_dim_set
)
3874 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3875 "only set dimensions can be projected out", goto error
);
3877 dim
= isl_space_dim(space
, isl_dim_set
);
3878 if (first
+ n
> dim
)
3879 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3880 "range out of bounds", goto error
);
3882 space
= isl_space_from_domain(space
);
3883 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3886 return isl_multi_aff_alloc(space
);
3888 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3889 space
= isl_space_domain(space
);
3890 ls
= isl_local_space_from_space(space
);
3892 for (i
= 0; i
< first
; ++i
) {
3895 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3897 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3900 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3903 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3904 isl_dim_set
, first
+ n
+ i
);
3905 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3908 isl_local_space_free(ls
);
3911 isl_space_free(space
);
3915 /* Given the space of a set and a range of set dimensions,
3916 * construct an isl_pw_multi_aff that projects out those dimensions.
3918 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3919 __isl_take isl_space
*space
, enum isl_dim_type type
,
3920 unsigned first
, unsigned n
)
3924 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3925 return isl_pw_multi_aff_from_multi_aff(ma
);
3928 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3931 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3932 __isl_take isl_multi_aff
*ma
)
3934 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3935 return isl_pw_multi_aff_alloc(dom
, ma
);
3938 /* Create a piecewise multi-affine expression in the given space that maps each
3939 * input dimension to the corresponding output dimension.
3941 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3942 __isl_take isl_space
*space
)
3944 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3947 /* Exploit the equalities in "eq" to simplify the affine expressions.
3949 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3950 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3954 maff
= isl_multi_aff_cow(maff
);
3958 for (i
= 0; i
< maff
->n
; ++i
) {
3959 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3960 isl_basic_set_copy(eq
));
3965 isl_basic_set_free(eq
);
3968 isl_basic_set_free(eq
);
3969 isl_multi_aff_free(maff
);
3973 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3978 maff
= isl_multi_aff_cow(maff
);
3982 for (i
= 0; i
< maff
->n
; ++i
) {
3983 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3985 return isl_multi_aff_free(maff
);
3991 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3992 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3994 maff1
= isl_multi_aff_add(maff1
, maff2
);
3995 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3999 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4007 /* Return the set of domain elements where "ma1" is lexicographically
4008 * smaller than or equal to "ma2".
4010 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4011 __isl_take isl_multi_aff
*ma2
)
4013 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4016 /* Return the set of domain elements where "ma1" is lexicographically
4017 * smaller than "ma2".
4019 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4020 __isl_take isl_multi_aff
*ma2
)
4022 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4025 /* Return the set of domain elements where "ma1" and "ma2"
4028 static __isl_give isl_set
*isl_multi_aff_order_set(
4029 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4030 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4033 isl_map
*map1
, *map2
;
4036 map1
= isl_map_from_multi_aff(ma1
);
4037 map2
= isl_map_from_multi_aff(ma2
);
4038 map
= isl_map_range_product(map1
, map2
);
4039 space
= isl_space_range(isl_map_get_space(map
));
4040 space
= isl_space_domain(isl_space_unwrap(space
));
4042 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4044 return isl_map_domain(map
);
4047 /* Return the set of domain elements where "ma1" is lexicographically
4048 * greater than or equal to "ma2".
4050 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4051 __isl_take isl_multi_aff
*ma2
)
4053 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4056 /* Return the set of domain elements where "ma1" is lexicographically
4057 * greater than "ma2".
4059 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4060 __isl_take isl_multi_aff
*ma2
)
4062 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4066 #define PW isl_pw_multi_aff
4068 #define EL isl_multi_aff
4070 #define EL_IS_ZERO is_empty
4074 #define IS_ZERO is_empty
4077 #undef DEFAULT_IS_ZERO
4078 #define DEFAULT_IS_ZERO 0
4083 #define NO_INVOLVES_DIMS
4084 #define NO_INSERT_DIMS
4088 #include <isl_pw_templ.c>
4089 #include <isl_pw_union_opt.c>
4094 #define UNION isl_union_pw_multi_aff
4096 #define PART isl_pw_multi_aff
4098 #define PARTS pw_multi_aff
4100 #include <isl_union_multi.c>
4101 #include <isl_union_neg.c>
4103 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4104 __isl_take isl_pw_multi_aff
*pma1
,
4105 __isl_take isl_pw_multi_aff
*pma2
)
4107 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4108 &isl_multi_aff_lex_ge_set
);
4111 /* Given two piecewise multi affine expressions, return a piecewise
4112 * multi-affine expression defined on the union of the definition domains
4113 * of the inputs that is equal to the lexicographic maximum of the two
4114 * inputs on each cell. If only one of the two inputs is defined on
4115 * a given cell, then it is considered to be the maximum.
4117 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4118 __isl_take isl_pw_multi_aff
*pma1
,
4119 __isl_take isl_pw_multi_aff
*pma2
)
4121 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4122 &pw_multi_aff_union_lexmax
);
4125 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4126 __isl_take isl_pw_multi_aff
*pma1
,
4127 __isl_take isl_pw_multi_aff
*pma2
)
4129 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4130 &isl_multi_aff_lex_le_set
);
4133 /* Given two piecewise multi affine expressions, return a piecewise
4134 * multi-affine expression defined on the union of the definition domains
4135 * of the inputs that is equal to the lexicographic minimum of the two
4136 * inputs on each cell. If only one of the two inputs is defined on
4137 * a given cell, then it is considered to be the minimum.
4139 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4140 __isl_take isl_pw_multi_aff
*pma1
,
4141 __isl_take isl_pw_multi_aff
*pma2
)
4143 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4144 &pw_multi_aff_union_lexmin
);
4147 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4148 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4150 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4151 &isl_multi_aff_add
);
4154 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4155 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4157 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4161 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4162 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4164 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4165 &isl_multi_aff_sub
);
4168 /* Subtract "pma2" from "pma1" and return the result.
4170 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4171 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4173 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4177 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4178 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4180 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4183 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4184 * with the actual sum on the shared domain and
4185 * the defined expression on the symmetric difference of the domains.
4187 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4188 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4190 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4193 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4194 * with the actual sum on the shared domain and
4195 * the defined expression on the symmetric difference of the domains.
4197 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4198 __isl_take isl_union_pw_multi_aff
*upma1
,
4199 __isl_take isl_union_pw_multi_aff
*upma2
)
4201 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4204 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4205 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4207 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4208 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4212 isl_pw_multi_aff
*res
;
4217 n
= pma1
->n
* pma2
->n
;
4218 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4219 isl_space_copy(pma2
->dim
));
4220 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4222 for (i
= 0; i
< pma1
->n
; ++i
) {
4223 for (j
= 0; j
< pma2
->n
; ++j
) {
4227 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4228 isl_set_copy(pma2
->p
[j
].set
));
4229 ma
= isl_multi_aff_product(
4230 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4231 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4232 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4236 isl_pw_multi_aff_free(pma1
);
4237 isl_pw_multi_aff_free(pma2
);
4240 isl_pw_multi_aff_free(pma1
);
4241 isl_pw_multi_aff_free(pma2
);
4245 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4246 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4248 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4249 &pw_multi_aff_product
);
4252 /* Construct a map mapping the domain of the piecewise multi-affine expression
4253 * to its range, with each dimension in the range equated to the
4254 * corresponding affine expression on its cell.
4256 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4264 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4266 for (i
= 0; i
< pma
->n
; ++i
) {
4267 isl_multi_aff
*maff
;
4268 isl_basic_map
*bmap
;
4271 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4272 bmap
= isl_basic_map_from_multi_aff(maff
);
4273 map_i
= isl_map_from_basic_map(bmap
);
4274 map_i
= isl_map_intersect_domain(map_i
,
4275 isl_set_copy(pma
->p
[i
].set
));
4276 map
= isl_map_union_disjoint(map
, map_i
);
4279 isl_pw_multi_aff_free(pma
);
4283 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4288 if (!isl_space_is_set(pma
->dim
))
4289 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4290 "isl_pw_multi_aff cannot be converted into an isl_set",
4293 return isl_map_from_pw_multi_aff(pma
);
4295 isl_pw_multi_aff_free(pma
);
4299 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4300 * denominator "denom".
4301 * "denom" is allowed to be negative, in which case the actual denominator
4302 * is -denom and the expressions are added instead.
4304 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4305 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4311 first
= isl_seq_first_non_zero(c
, n
);
4315 sign
= isl_int_sgn(denom
);
4317 isl_int_abs(d
, denom
);
4318 for (i
= first
; i
< n
; ++i
) {
4321 if (isl_int_is_zero(c
[i
]))
4323 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4324 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4325 aff_i
= isl_aff_scale_down(aff_i
, d
);
4327 aff
= isl_aff_sub(aff
, aff_i
);
4329 aff
= isl_aff_add(aff
, aff_i
);
4336 /* Extract an affine expression that expresses the output dimension "pos"
4337 * of "bmap" in terms of the parameters and input dimensions from
4339 * Note that this expression may involve integer divisions defined
4340 * in terms of parameters and input dimensions.
4341 * The equality may also involve references to earlier (but not later)
4342 * output dimensions. These are replaced by the corresponding elements
4345 * If the equality is of the form
4347 * f(i) + h(j) + a x + g(i) = 0,
4349 * with f(i) a linear combinations of the parameters and input dimensions,
4350 * g(i) a linear combination of integer divisions defined in terms of the same
4351 * and h(j) a linear combinations of earlier output dimensions,
4352 * then the affine expression is
4354 * (-f(i) - g(i))/a - h(j)/a
4356 * If the equality is of the form
4358 * f(i) + h(j) - a x + g(i) = 0,
4360 * then the affine expression is
4362 * (f(i) + g(i))/a - h(j)/(-a)
4365 * If "div" refers to an integer division (i.e., it is smaller than
4366 * the number of integer divisions), then the equality constraint
4367 * does involve an integer division (the one at position "div") that
4368 * is defined in terms of output dimensions. However, this integer
4369 * division can be eliminated by exploiting a pair of constraints
4370 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4371 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4373 * In particular, let
4375 * x = e(i) + m floor(...)
4377 * with e(i) the expression derived above and floor(...) the integer
4378 * division involving output dimensions.
4389 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4390 * = (e(i) - l) mod m
4394 * x - l = (e(i) - l) mod m
4398 * x = ((e(i) - l) mod m) + l
4400 * The variable "shift" below contains the expression -l, which may
4401 * also involve a linear combination of earlier output dimensions.
4403 static __isl_give isl_aff
*extract_aff_from_equality(
4404 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4405 __isl_keep isl_multi_aff
*ma
)
4408 unsigned n_div
, n_out
;
4410 isl_local_space
*ls
;
4411 isl_aff
*aff
, *shift
;
4414 ctx
= isl_basic_map_get_ctx(bmap
);
4415 ls
= isl_basic_map_get_local_space(bmap
);
4416 ls
= isl_local_space_domain(ls
);
4417 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4420 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4421 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4422 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4423 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4424 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4425 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4426 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4428 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4429 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4430 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4433 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4434 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4435 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4436 bmap
->eq
[eq
][o_out
+ pos
]);
4438 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4441 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4442 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4443 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4444 isl_int_set_si(shift
->v
->el
[0], 1);
4445 shift
= subtract_initial(shift
, ma
, pos
,
4446 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4447 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4448 mod
= isl_val_int_from_isl_int(ctx
,
4449 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4450 mod
= isl_val_abs(mod
);
4451 aff
= isl_aff_mod_val(aff
, mod
);
4452 aff
= isl_aff_sub(aff
, shift
);
4455 isl_local_space_free(ls
);
4458 isl_local_space_free(ls
);
4463 /* Given a basic map with output dimensions defined
4464 * in terms of the parameters input dimensions and earlier
4465 * output dimensions using an equality (and possibly a pair on inequalities),
4466 * extract an isl_aff that expresses output dimension "pos" in terms
4467 * of the parameters and input dimensions.
4468 * Note that this expression may involve integer divisions defined
4469 * in terms of parameters and input dimensions.
4470 * "ma" contains the expressions corresponding to earlier output dimensions.
4472 * This function shares some similarities with
4473 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4475 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4476 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4483 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4484 if (eq
>= bmap
->n_eq
)
4485 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4486 "unable to find suitable equality", return NULL
);
4487 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4489 aff
= isl_aff_remove_unused_divs(aff
);
4493 /* Given a basic map where each output dimension is defined
4494 * in terms of the parameters and input dimensions using an equality,
4495 * extract an isl_multi_aff that expresses the output dimensions in terms
4496 * of the parameters and input dimensions.
4498 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4499 __isl_take isl_basic_map
*bmap
)
4508 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4509 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4511 for (i
= 0; i
< n_out
; ++i
) {
4514 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4515 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4518 isl_basic_map_free(bmap
);
4523 /* Given a basic set where each set dimension is defined
4524 * in terms of the parameters using an equality,
4525 * extract an isl_multi_aff that expresses the set dimensions in terms
4526 * of the parameters.
4528 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4529 __isl_take isl_basic_set
*bset
)
4531 return extract_isl_multi_aff_from_basic_map(bset
);
4534 /* Create an isl_pw_multi_aff that is equivalent to
4535 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4536 * The given basic map is such that each output dimension is defined
4537 * in terms of the parameters and input dimensions using an equality.
4539 * Since some applications expect the result of isl_pw_multi_aff_from_map
4540 * to only contain integer affine expressions, we compute the floor
4541 * of the expression before returning.
4543 * Remove all constraints involving local variables without
4544 * an explicit representation (resulting in the removal of those
4545 * local variables) prior to the actual extraction to ensure
4546 * that the local spaces in which the resulting affine expressions
4547 * are created do not contain any unknown local variables.
4548 * Removing such constraints is safe because constraints involving
4549 * unknown local variables are not used to determine whether
4550 * a basic map is obviously single-valued.
4552 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4553 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4557 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4558 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4559 ma
= isl_multi_aff_floor(ma
);
4560 return isl_pw_multi_aff_alloc(domain
, ma
);
4563 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4564 * This obviously only works if the input "map" is single-valued.
4565 * If so, we compute the lexicographic minimum of the image in the form
4566 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4567 * to its lexicographic minimum.
4568 * If the input is not single-valued, we produce an error.
4570 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4571 __isl_take isl_map
*map
)
4575 isl_pw_multi_aff
*pma
;
4577 sv
= isl_map_is_single_valued(map
);
4581 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4582 "map is not single-valued", goto error
);
4583 map
= isl_map_make_disjoint(map
);
4587 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4589 for (i
= 0; i
< map
->n
; ++i
) {
4590 isl_pw_multi_aff
*pma_i
;
4591 isl_basic_map
*bmap
;
4592 bmap
= isl_basic_map_copy(map
->p
[i
]);
4593 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4594 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4604 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4605 * taking into account that the output dimension at position "d"
4606 * can be represented as
4608 * x = floor((e(...) + c1) / m)
4610 * given that constraint "i" is of the form
4612 * e(...) + c1 - m x >= 0
4615 * Let "map" be of the form
4619 * We construct a mapping
4621 * A -> [A -> x = floor(...)]
4623 * apply that to the map, obtaining
4625 * [A -> x = floor(...)] -> B
4627 * and equate dimension "d" to x.
4628 * We then compute a isl_pw_multi_aff representation of the resulting map
4629 * and plug in the mapping above.
4631 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4632 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4636 isl_local_space
*ls
;
4644 isl_pw_multi_aff
*pma
;
4647 is_set
= isl_map_is_set(map
);
4649 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4650 ctx
= isl_map_get_ctx(map
);
4651 space
= isl_space_domain(isl_map_get_space(map
));
4652 n_in
= isl_space_dim(space
, isl_dim_set
);
4653 n
= isl_space_dim(space
, isl_dim_all
);
4655 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4657 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4658 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4660 isl_basic_map_free(hull
);
4662 ls
= isl_local_space_from_space(isl_space_copy(space
));
4663 aff
= isl_aff_alloc_vec(ls
, v
);
4664 aff
= isl_aff_floor(aff
);
4666 isl_space_free(space
);
4667 ma
= isl_multi_aff_from_aff(aff
);
4669 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4670 ma
= isl_multi_aff_range_product(ma
,
4671 isl_multi_aff_from_aff(aff
));
4674 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4675 map
= isl_map_apply_domain(map
, insert
);
4676 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4677 pma
= isl_pw_multi_aff_from_map(map
);
4678 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4683 /* Is constraint "c" of the form
4685 * e(...) + c1 - m x >= 0
4689 * -e(...) + c2 + m x >= 0
4691 * where m > 1 and e only depends on parameters and input dimemnsions?
4693 * "offset" is the offset of the output dimensions
4694 * "pos" is the position of output dimension x.
4696 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4698 if (isl_int_is_zero(c
[offset
+ d
]))
4700 if (isl_int_is_one(c
[offset
+ d
]))
4702 if (isl_int_is_negone(c
[offset
+ d
]))
4704 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4706 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4707 total
- (offset
+ d
+ 1)) != -1)
4712 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4714 * As a special case, we first check if there is any pair of constraints,
4715 * shared by all the basic maps in "map" that force a given dimension
4716 * to be equal to the floor of some affine combination of the input dimensions.
4718 * In particular, if we can find two constraints
4720 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4724 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4726 * where m > 1 and e only depends on parameters and input dimemnsions,
4729 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4731 * then we know that we can take
4733 * x = floor((e(...) + c1) / m)
4735 * without having to perform any computation.
4737 * Note that we know that
4741 * If c1 + c2 were 0, then we would have detected an equality during
4742 * simplification. If c1 + c2 were negative, then we would have detected
4745 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4746 __isl_take isl_map
*map
)
4752 isl_basic_map
*hull
;
4754 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4759 dim
= isl_map_dim(map
, isl_dim_out
);
4760 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4761 total
= 1 + isl_basic_map_total_dim(hull
);
4763 for (d
= 0; d
< dim
; ++d
) {
4764 for (i
= 0; i
< n
; ++i
) {
4765 if (!is_potential_div_constraint(hull
->ineq
[i
],
4768 for (j
= i
+ 1; j
< n
; ++j
) {
4769 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4770 hull
->ineq
[j
] + 1, total
- 1))
4772 isl_int_add(sum
, hull
->ineq
[i
][0],
4774 if (isl_int_abs_lt(sum
,
4775 hull
->ineq
[i
][offset
+ d
]))
4782 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4784 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4788 isl_basic_map_free(hull
);
4789 return pw_multi_aff_from_map_base(map
);
4792 isl_basic_map_free(hull
);
4796 /* Given an affine expression
4798 * [A -> B] -> f(A,B)
4800 * construct an isl_multi_aff
4804 * such that dimension "d" in B' is set to "aff" and the remaining
4805 * dimensions are set equal to the corresponding dimensions in B.
4806 * "n_in" is the dimension of the space A.
4807 * "n_out" is the dimension of the space B.
4809 * If "is_set" is set, then the affine expression is of the form
4813 * and we construct an isl_multi_aff
4817 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4818 unsigned n_in
, unsigned n_out
, int is_set
)
4822 isl_space
*space
, *space2
;
4823 isl_local_space
*ls
;
4825 space
= isl_aff_get_domain_space(aff
);
4826 ls
= isl_local_space_from_space(isl_space_copy(space
));
4827 space2
= isl_space_copy(space
);
4829 space2
= isl_space_range(isl_space_unwrap(space2
));
4830 space
= isl_space_map_from_domain_and_range(space
, space2
);
4831 ma
= isl_multi_aff_alloc(space
);
4832 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4834 for (i
= 0; i
< n_out
; ++i
) {
4837 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4838 isl_dim_set
, n_in
+ i
);
4839 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4842 isl_local_space_free(ls
);
4847 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4848 * taking into account that the dimension at position "d" can be written as
4850 * x = m a + f(..) (1)
4852 * where m is equal to "gcd".
4853 * "i" is the index of the equality in "hull" that defines f(..).
4854 * In particular, the equality is of the form
4856 * f(..) - x + m g(existentials) = 0
4860 * -f(..) + x + m g(existentials) = 0
4862 * We basically plug (1) into "map", resulting in a map with "a"
4863 * in the range instead of "x". The corresponding isl_pw_multi_aff
4864 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4866 * Specifically, given the input map
4870 * We first wrap it into a set
4874 * and define (1) on top of the corresponding space, resulting in "aff".
4875 * We use this to create an isl_multi_aff that maps the output position "d"
4876 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4877 * We plug this into the wrapped map, unwrap the result and compute the
4878 * corresponding isl_pw_multi_aff.
4879 * The result is an expression
4887 * so that we can plug that into "aff", after extending the latter to
4893 * If "map" is actually a set, then there is no "A" space, meaning
4894 * that we do not need to perform any wrapping, and that the result
4895 * of the recursive call is of the form
4899 * which is plugged into a mapping of the form
4903 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4904 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4909 isl_local_space
*ls
;
4912 isl_pw_multi_aff
*pma
, *id
;
4918 is_set
= isl_map_is_set(map
);
4920 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4921 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4922 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4927 set
= isl_map_wrap(map
);
4928 space
= isl_space_map_from_set(isl_set_get_space(set
));
4929 ma
= isl_multi_aff_identity(space
);
4930 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4931 aff
= isl_aff_alloc(ls
);
4933 isl_int_set_si(aff
->v
->el
[0], 1);
4934 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4935 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4938 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4940 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4942 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4943 set
= isl_set_preimage_multi_aff(set
, ma
);
4945 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4950 map
= isl_set_unwrap(set
);
4951 pma
= isl_pw_multi_aff_from_map(map
);
4954 space
= isl_pw_multi_aff_get_domain_space(pma
);
4955 space
= isl_space_map_from_set(space
);
4956 id
= isl_pw_multi_aff_identity(space
);
4957 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4959 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4960 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4962 isl_basic_map_free(hull
);
4966 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4968 * As a special case, we first check if all output dimensions are uniquely
4969 * defined in terms of the parameters and input dimensions over the entire
4970 * domain. If so, we extract the desired isl_pw_multi_aff directly
4971 * from the affine hull of "map" and its domain.
4973 * Otherwise, we check if any of the output dimensions is "strided".
4974 * That is, we check if can be written as
4978 * with m greater than 1, a some combination of existentially quantified
4979 * variables and f an expression in the parameters and input dimensions.
4980 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4982 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4985 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4989 isl_basic_map
*hull
;
4999 map
= isl_map_detect_equalities(map
);
5000 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5001 sv
= isl_basic_map_plain_is_single_valued(hull
);
5003 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5005 hull
= isl_basic_map_free(hull
);
5009 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5010 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5013 isl_basic_map_free(hull
);
5014 return pw_multi_aff_from_map_check_div(map
);
5019 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5020 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5022 for (i
= 0; i
< n_out
; ++i
) {
5023 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5024 isl_int
*eq
= hull
->eq
[j
];
5025 isl_pw_multi_aff
*res
;
5027 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5028 !isl_int_is_negone(eq
[o_out
+ i
]))
5030 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5032 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5033 n_out
- (i
+ 1)) != -1)
5035 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5036 if (isl_int_is_zero(gcd
))
5038 if (isl_int_is_one(gcd
))
5041 res
= pw_multi_aff_from_map_stride(map
, hull
,
5049 isl_basic_map_free(hull
);
5050 return pw_multi_aff_from_map_check_div(map
);
5056 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5058 return isl_pw_multi_aff_from_map(set
);
5061 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5064 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5066 isl_union_pw_multi_aff
**upma
= user
;
5067 isl_pw_multi_aff
*pma
;
5069 pma
= isl_pw_multi_aff_from_map(map
);
5070 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5072 return *upma
? isl_stat_ok
: isl_stat_error
;
5075 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5078 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5079 __isl_take isl_aff
*aff
)
5082 isl_pw_multi_aff
*pma
;
5084 ma
= isl_multi_aff_from_aff(aff
);
5085 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5086 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5089 /* Try and create an isl_union_pw_multi_aff that is equivalent
5090 * to the given isl_union_map.
5091 * The isl_union_map is required to be single-valued in each space.
5092 * Otherwise, an error is produced.
5094 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5095 __isl_take isl_union_map
*umap
)
5098 isl_union_pw_multi_aff
*upma
;
5100 space
= isl_union_map_get_space(umap
);
5101 upma
= isl_union_pw_multi_aff_empty(space
);
5102 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5103 upma
= isl_union_pw_multi_aff_free(upma
);
5104 isl_union_map_free(umap
);
5109 /* Try and create an isl_union_pw_multi_aff that is equivalent
5110 * to the given isl_union_set.
5111 * The isl_union_set is required to be a singleton in each space.
5112 * Otherwise, an error is produced.
5114 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5115 __isl_take isl_union_set
*uset
)
5117 return isl_union_pw_multi_aff_from_union_map(uset
);
5120 /* Return the piecewise affine expression "set ? 1 : 0".
5122 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5125 isl_space
*space
= isl_set_get_space(set
);
5126 isl_local_space
*ls
= isl_local_space_from_space(space
);
5127 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5128 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5130 one
= isl_aff_add_constant_si(one
, 1);
5131 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5132 set
= isl_set_complement(set
);
5133 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5138 /* Plug in "subs" for dimension "type", "pos" of "aff".
5140 * Let i be the dimension to replace and let "subs" be of the form
5144 * and "aff" of the form
5150 * (a f + d g')/(m d)
5152 * where g' is the result of plugging in "subs" in each of the integer
5155 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5156 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5161 aff
= isl_aff_cow(aff
);
5163 return isl_aff_free(aff
);
5165 ctx
= isl_aff_get_ctx(aff
);
5166 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5167 isl_die(ctx
, isl_error_invalid
,
5168 "spaces don't match", return isl_aff_free(aff
));
5169 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5170 isl_die(ctx
, isl_error_unsupported
,
5171 "cannot handle divs yet", return isl_aff_free(aff
));
5173 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5175 return isl_aff_free(aff
);
5177 aff
->v
= isl_vec_cow(aff
->v
);
5179 return isl_aff_free(aff
);
5181 pos
+= isl_local_space_offset(aff
->ls
, type
);
5184 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5185 aff
->v
->size
, subs
->v
->size
, v
);
5191 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5192 * expressions in "maff".
5194 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5195 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5196 __isl_keep isl_aff
*subs
)
5200 maff
= isl_multi_aff_cow(maff
);
5202 return isl_multi_aff_free(maff
);
5204 if (type
== isl_dim_in
)
5207 for (i
= 0; i
< maff
->n
; ++i
) {
5208 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5210 return isl_multi_aff_free(maff
);
5216 /* Plug in "subs" for dimension "type", "pos" of "pma".
5218 * pma is of the form
5222 * while subs is of the form
5224 * v' = B_j(v) -> S_j
5226 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5227 * has a contribution in the result, in particular
5229 * C_ij(S_j) -> M_i(S_j)
5231 * Note that plugging in S_j in C_ij may also result in an empty set
5232 * and this contribution should simply be discarded.
5234 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5235 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5236 __isl_keep isl_pw_aff
*subs
)
5239 isl_pw_multi_aff
*res
;
5242 return isl_pw_multi_aff_free(pma
);
5244 n
= pma
->n
* subs
->n
;
5245 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5247 for (i
= 0; i
< pma
->n
; ++i
) {
5248 for (j
= 0; j
< subs
->n
; ++j
) {
5250 isl_multi_aff
*res_ij
;
5253 common
= isl_set_intersect(
5254 isl_set_copy(pma
->p
[i
].set
),
5255 isl_set_copy(subs
->p
[j
].set
));
5256 common
= isl_set_substitute(common
,
5257 type
, pos
, subs
->p
[j
].aff
);
5258 empty
= isl_set_plain_is_empty(common
);
5259 if (empty
< 0 || empty
) {
5260 isl_set_free(common
);
5266 res_ij
= isl_multi_aff_substitute(
5267 isl_multi_aff_copy(pma
->p
[i
].maff
),
5268 type
, pos
, subs
->p
[j
].aff
);
5270 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5274 isl_pw_multi_aff_free(pma
);
5277 isl_pw_multi_aff_free(pma
);
5278 isl_pw_multi_aff_free(res
);
5282 /* Compute the preimage of a range of dimensions in the affine expression "src"
5283 * under "ma" and put the result in "dst". The number of dimensions in "src"
5284 * that precede the range is given by "n_before". The number of dimensions
5285 * in the range is given by the number of output dimensions of "ma".
5286 * The number of dimensions that follow the range is given by "n_after".
5287 * If "has_denom" is set (to one),
5288 * then "src" and "dst" have an extra initial denominator.
5289 * "n_div_ma" is the number of existentials in "ma"
5290 * "n_div_bset" is the number of existentials in "src"
5291 * The resulting "dst" (which is assumed to have been allocated by
5292 * the caller) contains coefficients for both sets of existentials,
5293 * first those in "ma" and then those in "src".
5294 * f, c1, c2 and g are temporary objects that have been initialized
5297 * Let src represent the expression
5299 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5301 * and let ma represent the expressions
5303 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5305 * We start out with the following expression for dst:
5307 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5309 * with the multiplication factor f initially equal to 1
5310 * and f \sum_i b_i v_i kept separately.
5311 * For each x_i that we substitute, we multiply the numerator
5312 * (and denominator) of dst by c_1 = m_i and add the numerator
5313 * of the x_i expression multiplied by c_2 = f b_i,
5314 * after removing the common factors of c_1 and c_2.
5315 * The multiplication factor f also needs to be multiplied by c_1
5316 * for the next x_j, j > i.
5318 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5319 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5320 int n_div_ma
, int n_div_bmap
,
5321 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5324 int n_param
, n_in
, n_out
;
5327 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5328 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5329 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5331 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5332 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5333 isl_seq_clr(dst
+ o_dst
, n_in
);
5336 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5339 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5341 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5343 isl_int_set_si(f
, 1);
5345 for (i
= 0; i
< n_out
; ++i
) {
5346 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5348 if (isl_int_is_zero(src
[offset
]))
5350 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5351 isl_int_mul(c2
, f
, src
[offset
]);
5352 isl_int_gcd(g
, c1
, c2
);
5353 isl_int_divexact(c1
, c1
, g
);
5354 isl_int_divexact(c2
, c2
, g
);
5356 isl_int_mul(f
, f
, c1
);
5359 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5360 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5361 o_dst
+= 1 + n_param
;
5362 o_src
+= 1 + n_param
;
5363 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5365 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5366 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5369 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5371 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5372 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5375 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5377 isl_int_mul(dst
[0], dst
[0], c1
);
5381 /* Compute the pullback of "aff" by the function represented by "ma".
5382 * In other words, plug in "ma" in "aff". The result is an affine expression
5383 * defined over the domain space of "ma".
5385 * If "aff" is represented by
5387 * (a(p) + b x + c(divs))/d
5389 * and ma is represented by
5391 * x = D(p) + F(y) + G(divs')
5393 * then the result is
5395 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5397 * The divs in the local space of the input are similarly adjusted
5398 * through a call to isl_local_space_preimage_multi_aff.
5400 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5401 __isl_take isl_multi_aff
*ma
)
5403 isl_aff
*res
= NULL
;
5404 isl_local_space
*ls
;
5405 int n_div_aff
, n_div_ma
;
5406 isl_int f
, c1
, c2
, g
;
5408 ma
= isl_multi_aff_align_divs(ma
);
5412 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5413 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5415 ls
= isl_aff_get_domain_local_space(aff
);
5416 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5417 res
= isl_aff_alloc(ls
);
5426 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5435 isl_multi_aff_free(ma
);
5436 res
= isl_aff_normalize(res
);
5440 isl_multi_aff_free(ma
);
5445 /* Compute the pullback of "aff1" by the function represented by "aff2".
5446 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5447 * defined over the domain space of "aff1".
5449 * The domain of "aff1" should match the range of "aff2", which means
5450 * that it should be single-dimensional.
5452 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5453 __isl_take isl_aff
*aff2
)
5457 ma
= isl_multi_aff_from_aff(aff2
);
5458 return isl_aff_pullback_multi_aff(aff1
, ma
);
5461 /* Compute the pullback of "ma1" by the function represented by "ma2".
5462 * In other words, plug in "ma2" in "ma1".
5464 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5466 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5467 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5470 isl_space
*space
= NULL
;
5472 ma2
= isl_multi_aff_align_divs(ma2
);
5473 ma1
= isl_multi_aff_cow(ma1
);
5477 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5478 isl_multi_aff_get_space(ma1
));
5480 for (i
= 0; i
< ma1
->n
; ++i
) {
5481 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5482 isl_multi_aff_copy(ma2
));
5487 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5488 isl_multi_aff_free(ma2
);
5491 isl_space_free(space
);
5492 isl_multi_aff_free(ma2
);
5493 isl_multi_aff_free(ma1
);
5497 /* Compute the pullback of "ma1" by the function represented by "ma2".
5498 * In other words, plug in "ma2" in "ma1".
5500 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5501 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5503 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5504 &isl_multi_aff_pullback_multi_aff_aligned
);
5507 /* Extend the local space of "dst" to include the divs
5508 * in the local space of "src".
5510 * If "src" does not have any divs or if the local spaces of "dst" and
5511 * "src" are the same, then no extension is required.
5513 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5514 __isl_keep isl_aff
*src
)
5517 int src_n_div
, dst_n_div
;
5524 return isl_aff_free(dst
);
5526 ctx
= isl_aff_get_ctx(src
);
5527 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5529 return isl_aff_free(dst
);
5531 isl_die(ctx
, isl_error_invalid
,
5532 "spaces don't match", goto error
);
5534 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5537 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5539 return isl_aff_free(dst
);
5543 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5544 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5545 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5546 if (!exp1
|| (dst_n_div
&& !exp2
))
5549 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5550 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5558 return isl_aff_free(dst
);
5561 /* Adjust the local spaces of the affine expressions in "maff"
5562 * such that they all have the save divs.
5564 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5565 __isl_take isl_multi_aff
*maff
)
5573 maff
= isl_multi_aff_cow(maff
);
5577 for (i
= 1; i
< maff
->n
; ++i
)
5578 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5579 for (i
= 1; i
< maff
->n
; ++i
) {
5580 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5582 return isl_multi_aff_free(maff
);
5588 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5590 aff
= isl_aff_cow(aff
);
5594 aff
->ls
= isl_local_space_lift(aff
->ls
);
5596 return isl_aff_free(aff
);
5601 /* Lift "maff" to a space with extra dimensions such that the result
5602 * has no more existentially quantified variables.
5603 * If "ls" is not NULL, then *ls is assigned the local space that lies
5604 * at the basis of the lifting applied to "maff".
5606 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5607 __isl_give isl_local_space
**ls
)
5621 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5622 *ls
= isl_local_space_from_space(space
);
5624 return isl_multi_aff_free(maff
);
5629 maff
= isl_multi_aff_cow(maff
);
5630 maff
= isl_multi_aff_align_divs(maff
);
5634 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5635 space
= isl_multi_aff_get_space(maff
);
5636 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5637 space
= isl_space_extend_domain_with_range(space
,
5638 isl_multi_aff_get_space(maff
));
5640 return isl_multi_aff_free(maff
);
5641 isl_space_free(maff
->space
);
5642 maff
->space
= space
;
5645 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5647 return isl_multi_aff_free(maff
);
5650 for (i
= 0; i
< maff
->n
; ++i
) {
5651 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5659 isl_local_space_free(*ls
);
5660 return isl_multi_aff_free(maff
);
5664 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5666 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5667 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5677 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5678 if (pos
< 0 || pos
>= n_out
)
5679 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5680 "index out of bounds", return NULL
);
5682 space
= isl_pw_multi_aff_get_space(pma
);
5683 space
= isl_space_drop_dims(space
, isl_dim_out
,
5684 pos
+ 1, n_out
- pos
- 1);
5685 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5687 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5688 for (i
= 0; i
< pma
->n
; ++i
) {
5690 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5691 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5697 /* Return an isl_pw_multi_aff with the given "set" as domain and
5698 * an unnamed zero-dimensional range.
5700 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5701 __isl_take isl_set
*set
)
5706 space
= isl_set_get_space(set
);
5707 space
= isl_space_from_domain(space
);
5708 ma
= isl_multi_aff_zero(space
);
5709 return isl_pw_multi_aff_alloc(set
, ma
);
5712 /* Add an isl_pw_multi_aff with the given "set" as domain and
5713 * an unnamed zero-dimensional range to *user.
5715 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5718 isl_union_pw_multi_aff
**upma
= user
;
5719 isl_pw_multi_aff
*pma
;
5721 pma
= isl_pw_multi_aff_from_domain(set
);
5722 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5727 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5728 * an unnamed zero-dimensional range.
5730 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5731 __isl_take isl_union_set
*uset
)
5734 isl_union_pw_multi_aff
*upma
;
5739 space
= isl_union_set_get_space(uset
);
5740 upma
= isl_union_pw_multi_aff_empty(space
);
5742 if (isl_union_set_foreach_set(uset
,
5743 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5746 isl_union_set_free(uset
);
5749 isl_union_set_free(uset
);
5750 isl_union_pw_multi_aff_free(upma
);
5754 /* Convert "pma" to an isl_map and add it to *umap.
5756 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5759 isl_union_map
**umap
= user
;
5762 map
= isl_map_from_pw_multi_aff(pma
);
5763 *umap
= isl_union_map_add_map(*umap
, map
);
5768 /* Construct a union map mapping the domain of the union
5769 * piecewise multi-affine expression to its range, with each dimension
5770 * in the range equated to the corresponding affine expression on its cell.
5772 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5773 __isl_take isl_union_pw_multi_aff
*upma
)
5776 isl_union_map
*umap
;
5781 space
= isl_union_pw_multi_aff_get_space(upma
);
5782 umap
= isl_union_map_empty(space
);
5784 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5785 &map_from_pw_multi_aff
, &umap
) < 0)
5788 isl_union_pw_multi_aff_free(upma
);
5791 isl_union_pw_multi_aff_free(upma
);
5792 isl_union_map_free(umap
);
5796 /* Local data for bin_entry and the callback "fn".
5798 struct isl_union_pw_multi_aff_bin_data
{
5799 isl_union_pw_multi_aff
*upma2
;
5800 isl_union_pw_multi_aff
*res
;
5801 isl_pw_multi_aff
*pma
;
5802 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5805 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5806 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5808 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5810 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5814 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5816 isl_pw_multi_aff_free(pma
);
5821 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5822 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5823 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5824 * as *entry. The callback should adjust data->res if desired.
5826 static __isl_give isl_union_pw_multi_aff
*bin_op(
5827 __isl_take isl_union_pw_multi_aff
*upma1
,
5828 __isl_take isl_union_pw_multi_aff
*upma2
,
5829 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5832 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5834 space
= isl_union_pw_multi_aff_get_space(upma2
);
5835 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5836 space
= isl_union_pw_multi_aff_get_space(upma1
);
5837 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5839 if (!upma1
|| !upma2
)
5843 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5844 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5845 &bin_entry
, &data
) < 0)
5848 isl_union_pw_multi_aff_free(upma1
);
5849 isl_union_pw_multi_aff_free(upma2
);
5852 isl_union_pw_multi_aff_free(upma1
);
5853 isl_union_pw_multi_aff_free(upma2
);
5854 isl_union_pw_multi_aff_free(data
.res
);
5858 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5859 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5861 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5862 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5866 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5867 isl_pw_multi_aff_get_space(pma2
));
5868 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5869 &isl_multi_aff_range_product
);
5872 /* Given two isl_pw_multi_affs A -> B and C -> D,
5873 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5875 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5876 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5878 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5879 &pw_multi_aff_range_product
);
5882 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5883 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5885 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5886 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5890 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5891 isl_pw_multi_aff_get_space(pma2
));
5892 space
= isl_space_flatten_range(space
);
5893 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5894 &isl_multi_aff_flat_range_product
);
5897 /* Given two isl_pw_multi_affs A -> B and C -> D,
5898 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5900 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5901 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5903 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5904 &pw_multi_aff_flat_range_product
);
5907 /* If data->pma and "pma2" have the same domain space, then compute
5908 * their flat range product and the result to data->res.
5910 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5913 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5915 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5916 pma2
->dim
, isl_dim_in
)) {
5917 isl_pw_multi_aff_free(pma2
);
5921 pma2
= isl_pw_multi_aff_flat_range_product(
5922 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5924 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5929 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5930 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5932 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5933 __isl_take isl_union_pw_multi_aff
*upma1
,
5934 __isl_take isl_union_pw_multi_aff
*upma2
)
5936 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5939 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5940 * The parameters are assumed to have been aligned.
5942 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5943 * except that it works on two different isl_pw_* types.
5945 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5946 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5947 __isl_take isl_pw_aff
*pa
)
5950 isl_pw_multi_aff
*res
= NULL
;
5955 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5956 pa
->dim
, isl_dim_in
))
5957 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5958 "domains don't match", goto error
);
5959 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5960 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5961 "index out of bounds", goto error
);
5964 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5966 for (i
= 0; i
< pma
->n
; ++i
) {
5967 for (j
= 0; j
< pa
->n
; ++j
) {
5969 isl_multi_aff
*res_ij
;
5972 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5973 isl_set_copy(pa
->p
[j
].set
));
5974 empty
= isl_set_plain_is_empty(common
);
5975 if (empty
< 0 || empty
) {
5976 isl_set_free(common
);
5982 res_ij
= isl_multi_aff_set_aff(
5983 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5984 isl_aff_copy(pa
->p
[j
].aff
));
5985 res_ij
= isl_multi_aff_gist(res_ij
,
5986 isl_set_copy(common
));
5988 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5992 isl_pw_multi_aff_free(pma
);
5993 isl_pw_aff_free(pa
);
5996 isl_pw_multi_aff_free(pma
);
5997 isl_pw_aff_free(pa
);
5998 return isl_pw_multi_aff_free(res
);
6001 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6003 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6004 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6005 __isl_take isl_pw_aff
*pa
)
6009 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
6010 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6011 if (!isl_space_has_named_params(pma
->dim
) ||
6012 !isl_space_has_named_params(pa
->dim
))
6013 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6014 "unaligned unnamed parameters", goto error
);
6015 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6016 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6017 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6019 isl_pw_multi_aff_free(pma
);
6020 isl_pw_aff_free(pa
);
6024 /* Do the parameters of "pa" match those of "space"?
6026 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6027 __isl_keep isl_space
*space
)
6029 isl_space
*pa_space
;
6035 pa_space
= isl_pw_aff_get_space(pa
);
6037 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6039 isl_space_free(pa_space
);
6043 /* Check that the domain space of "pa" matches "space".
6045 * Return 0 on success and -1 on error.
6047 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6048 __isl_keep isl_space
*space
)
6050 isl_space
*pa_space
;
6056 pa_space
= isl_pw_aff_get_space(pa
);
6058 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6062 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6063 "parameters don't match", goto error
);
6064 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6065 pa_space
, isl_dim_in
);
6069 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6070 "domains don't match", goto error
);
6071 isl_space_free(pa_space
);
6074 isl_space_free(pa_space
);
6083 #include <isl_multi_templ.c>
6084 #include <isl_multi_apply_set.c>
6085 #include <isl_multi_coalesce.c>
6086 #include <isl_multi_gist.c>
6087 #include <isl_multi_hash.c>
6088 #include <isl_multi_intersect.c>
6090 /* Scale the elements of "pma" by the corresponding elements of "mv".
6092 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6093 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6097 pma
= isl_pw_multi_aff_cow(pma
);
6100 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6101 mv
->space
, isl_dim_set
))
6102 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6103 "spaces don't match", goto error
);
6104 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6105 mv
->space
, isl_dim_param
)) {
6106 pma
= isl_pw_multi_aff_align_params(pma
,
6107 isl_multi_val_get_space(mv
));
6108 mv
= isl_multi_val_align_params(mv
,
6109 isl_pw_multi_aff_get_space(pma
));
6114 for (i
= 0; i
< pma
->n
; ++i
) {
6115 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6116 isl_multi_val_copy(mv
));
6117 if (!pma
->p
[i
].maff
)
6121 isl_multi_val_free(mv
);
6124 isl_multi_val_free(mv
);
6125 isl_pw_multi_aff_free(pma
);
6129 /* This function is called for each entry of an isl_union_pw_multi_aff.
6130 * If the space of the entry matches that of data->mv,
6131 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6132 * Otherwise, return an empty isl_pw_multi_aff.
6134 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6135 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6137 isl_multi_val
*mv
= user
;
6141 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6142 mv
->space
, isl_dim_set
)) {
6143 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6144 isl_pw_multi_aff_free(pma
);
6145 return isl_pw_multi_aff_empty(space
);
6148 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6151 /* Scale the elements of "upma" by the corresponding elements of "mv",
6152 * for those entries that match the space of "mv".
6154 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6155 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6157 upma
= isl_union_pw_multi_aff_align_params(upma
,
6158 isl_multi_val_get_space(mv
));
6159 mv
= isl_multi_val_align_params(mv
,
6160 isl_union_pw_multi_aff_get_space(upma
));
6164 return isl_union_pw_multi_aff_transform(upma
,
6165 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6167 isl_multi_val_free(mv
);
6170 isl_multi_val_free(mv
);
6171 isl_union_pw_multi_aff_free(upma
);
6175 /* Construct and return a piecewise multi affine expression
6176 * in the given space with value zero in each of the output dimensions and
6177 * a universe domain.
6179 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6181 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6184 /* Construct and return a piecewise multi affine expression
6185 * that is equal to the given piecewise affine expression.
6187 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6188 __isl_take isl_pw_aff
*pa
)
6192 isl_pw_multi_aff
*pma
;
6197 space
= isl_pw_aff_get_space(pa
);
6198 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6200 for (i
= 0; i
< pa
->n
; ++i
) {
6204 set
= isl_set_copy(pa
->p
[i
].set
);
6205 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6206 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6209 isl_pw_aff_free(pa
);
6213 /* Construct a set or map mapping the shared (parameter) domain
6214 * of the piecewise affine expressions to the range of "mpa"
6215 * with each dimension in the range equated to the
6216 * corresponding piecewise affine expression.
6218 static __isl_give isl_map
*map_from_multi_pw_aff(
6219 __isl_take isl_multi_pw_aff
*mpa
)
6228 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6229 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6230 "invalid space", goto error
);
6232 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6233 map
= isl_map_universe(isl_space_from_domain(space
));
6235 for (i
= 0; i
< mpa
->n
; ++i
) {
6239 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6240 map_i
= map_from_pw_aff(pa
);
6242 map
= isl_map_flat_range_product(map
, map_i
);
6245 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6247 isl_multi_pw_aff_free(mpa
);
6250 isl_multi_pw_aff_free(mpa
);
6254 /* Construct a map mapping the shared domain
6255 * of the piecewise affine expressions to the range of "mpa"
6256 * with each dimension in the range equated to the
6257 * corresponding piecewise affine expression.
6259 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6263 if (isl_space_is_set(mpa
->space
))
6264 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6265 "space of input is not a map", goto error
);
6267 return map_from_multi_pw_aff(mpa
);
6269 isl_multi_pw_aff_free(mpa
);
6273 /* Construct a set mapping the shared parameter domain
6274 * of the piecewise affine expressions to the space of "mpa"
6275 * with each dimension in the range equated to the
6276 * corresponding piecewise affine expression.
6278 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6282 if (!isl_space_is_set(mpa
->space
))
6283 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6284 "space of input is not a set", goto error
);
6286 return map_from_multi_pw_aff(mpa
);
6288 isl_multi_pw_aff_free(mpa
);
6292 /* Construct and return a piecewise multi affine expression
6293 * that is equal to the given multi piecewise affine expression
6294 * on the shared domain of the piecewise affine expressions.
6296 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6297 __isl_take isl_multi_pw_aff
*mpa
)
6302 isl_pw_multi_aff
*pma
;
6307 space
= isl_multi_pw_aff_get_space(mpa
);
6310 isl_multi_pw_aff_free(mpa
);
6311 return isl_pw_multi_aff_zero(space
);
6314 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6315 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6317 for (i
= 1; i
< mpa
->n
; ++i
) {
6318 isl_pw_multi_aff
*pma_i
;
6320 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6321 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6322 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6325 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6327 isl_multi_pw_aff_free(mpa
);
6331 /* Construct and return a multi piecewise affine expression
6332 * that is equal to the given multi affine expression.
6334 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6335 __isl_take isl_multi_aff
*ma
)
6338 isl_multi_pw_aff
*mpa
;
6343 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6344 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6346 for (i
= 0; i
< n
; ++i
) {
6349 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6350 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6353 isl_multi_aff_free(ma
);
6357 /* Construct and return a multi piecewise affine expression
6358 * that is equal to the given piecewise multi affine expression.
6360 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6361 __isl_take isl_pw_multi_aff
*pma
)
6365 isl_multi_pw_aff
*mpa
;
6370 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6371 space
= isl_pw_multi_aff_get_space(pma
);
6372 mpa
= isl_multi_pw_aff_alloc(space
);
6374 for (i
= 0; i
< n
; ++i
) {
6377 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6378 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6381 isl_pw_multi_aff_free(pma
);
6385 /* Do "pa1" and "pa2" represent the same function?
6387 * We first check if they are obviously equal.
6388 * If not, we convert them to maps and check if those are equal.
6390 * If "pa1" or "pa2" contain any NaNs, then they are considered
6391 * not to be the same. A NaN is not equal to anything, not even
6394 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6398 isl_map
*map1
, *map2
;
6403 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6404 if (equal
< 0 || equal
)
6406 has_nan
= isl_pw_aff_involves_nan(pa1
);
6407 if (has_nan
>= 0 && !has_nan
)
6408 has_nan
= isl_pw_aff_involves_nan(pa2
);
6414 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6415 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6416 equal
= isl_map_is_equal(map1
, map2
);
6423 /* Do "mpa1" and "mpa2" represent the same function?
6425 * Note that we cannot convert the entire isl_multi_pw_aff
6426 * to a map because the domains of the piecewise affine expressions
6427 * may not be the same.
6429 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6430 __isl_keep isl_multi_pw_aff
*mpa2
)
6436 return isl_bool_error
;
6438 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6439 mpa2
->space
, isl_dim_param
)) {
6440 if (!isl_space_has_named_params(mpa1
->space
))
6441 return isl_bool_false
;
6442 if (!isl_space_has_named_params(mpa2
->space
))
6443 return isl_bool_false
;
6444 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6445 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6446 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6447 isl_multi_pw_aff_get_space(mpa2
));
6448 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6449 isl_multi_pw_aff_get_space(mpa1
));
6450 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6451 isl_multi_pw_aff_free(mpa1
);
6452 isl_multi_pw_aff_free(mpa2
);
6456 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6457 if (equal
< 0 || !equal
)
6460 for (i
= 0; i
< mpa1
->n
; ++i
) {
6461 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6462 if (equal
< 0 || !equal
)
6466 return isl_bool_true
;
6469 /* Compute the pullback of "mpa" by the function represented by "ma".
6470 * In other words, plug in "ma" in "mpa".
6472 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6474 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6475 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6478 isl_space
*space
= NULL
;
6480 mpa
= isl_multi_pw_aff_cow(mpa
);
6484 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6485 isl_multi_pw_aff_get_space(mpa
));
6489 for (i
= 0; i
< mpa
->n
; ++i
) {
6490 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6491 isl_multi_aff_copy(ma
));
6496 isl_multi_aff_free(ma
);
6497 isl_space_free(mpa
->space
);
6501 isl_space_free(space
);
6502 isl_multi_pw_aff_free(mpa
);
6503 isl_multi_aff_free(ma
);
6507 /* Compute the pullback of "mpa" by the function represented by "ma".
6508 * In other words, plug in "ma" in "mpa".
6510 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6511 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6515 if (isl_space_match(mpa
->space
, isl_dim_param
,
6516 ma
->space
, isl_dim_param
))
6517 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6518 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6519 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6520 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6522 isl_multi_pw_aff_free(mpa
);
6523 isl_multi_aff_free(ma
);
6527 /* Compute the pullback of "mpa" by the function represented by "pma".
6528 * In other words, plug in "pma" in "mpa".
6530 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6532 static __isl_give isl_multi_pw_aff
*
6533 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6534 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6537 isl_space
*space
= NULL
;
6539 mpa
= isl_multi_pw_aff_cow(mpa
);
6543 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6544 isl_multi_pw_aff_get_space(mpa
));
6546 for (i
= 0; i
< mpa
->n
; ++i
) {
6547 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6548 isl_pw_multi_aff_copy(pma
));
6553 isl_pw_multi_aff_free(pma
);
6554 isl_space_free(mpa
->space
);
6558 isl_space_free(space
);
6559 isl_multi_pw_aff_free(mpa
);
6560 isl_pw_multi_aff_free(pma
);
6564 /* Compute the pullback of "mpa" by the function represented by "pma".
6565 * In other words, plug in "pma" in "mpa".
6567 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6568 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6572 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6573 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6574 mpa
= isl_multi_pw_aff_align_params(mpa
,
6575 isl_pw_multi_aff_get_space(pma
));
6576 pma
= isl_pw_multi_aff_align_params(pma
,
6577 isl_multi_pw_aff_get_space(mpa
));
6578 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6580 isl_multi_pw_aff_free(mpa
);
6581 isl_pw_multi_aff_free(pma
);
6585 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6586 * with the domain of "aff". The domain of the result is the same
6588 * "mpa" and "aff" are assumed to have been aligned.
6590 * We first extract the parametric constant from "aff", defined
6591 * over the correct domain.
6592 * Then we add the appropriate combinations of the members of "mpa".
6593 * Finally, we add the integer divisions through recursive calls.
6595 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6596 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6604 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6605 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6607 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6608 tmp
= isl_aff_copy(aff
);
6609 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6610 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6611 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6612 isl_space_dim(space
, isl_dim_set
));
6613 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6614 pa
= isl_pw_aff_from_aff(tmp
);
6616 for (i
= 0; i
< n_in
; ++i
) {
6619 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6621 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6622 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6623 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6624 pa
= isl_pw_aff_add(pa
, pa_i
);
6627 for (i
= 0; i
< n_div
; ++i
) {
6631 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6633 div
= isl_aff_get_div(aff
, i
);
6634 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6635 isl_multi_pw_aff_copy(mpa
), div
);
6636 pa_i
= isl_pw_aff_floor(pa_i
);
6637 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6638 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6639 pa
= isl_pw_aff_add(pa
, pa_i
);
6642 isl_multi_pw_aff_free(mpa
);
6648 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6649 * with the domain of "aff". The domain of the result is the same
6652 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6653 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6657 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6658 mpa
->space
, isl_dim_param
))
6659 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6661 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6662 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6664 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6667 isl_multi_pw_aff_free(mpa
);
6671 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6672 * with the domain of "pa". The domain of the result is the same
6674 * "mpa" and "pa" are assumed to have been aligned.
6676 * We consider each piece in turn. Note that the domains of the
6677 * pieces are assumed to be disjoint and they remain disjoint
6678 * after taking the preimage (over the same function).
6680 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6681 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6690 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6691 isl_pw_aff_get_space(pa
));
6692 res
= isl_pw_aff_empty(space
);
6694 for (i
= 0; i
< pa
->n
; ++i
) {
6698 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6699 isl_multi_pw_aff_copy(mpa
),
6700 isl_aff_copy(pa
->p
[i
].aff
));
6701 domain
= isl_set_copy(pa
->p
[i
].set
);
6702 domain
= isl_set_preimage_multi_pw_aff(domain
,
6703 isl_multi_pw_aff_copy(mpa
));
6704 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6705 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6708 isl_pw_aff_free(pa
);
6709 isl_multi_pw_aff_free(mpa
);
6712 isl_pw_aff_free(pa
);
6713 isl_multi_pw_aff_free(mpa
);
6717 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6718 * with the domain of "pa". The domain of the result is the same
6721 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6722 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6726 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6727 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6729 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6730 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6732 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6734 isl_pw_aff_free(pa
);
6735 isl_multi_pw_aff_free(mpa
);
6739 /* Compute the pullback of "pa" by the function represented by "mpa".
6740 * In other words, plug in "mpa" in "pa".
6741 * "pa" and "mpa" are assumed to have been aligned.
6743 * The pullback is computed by applying "pa" to "mpa".
6745 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6746 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6748 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6751 /* Compute the pullback of "pa" by the function represented by "mpa".
6752 * In other words, plug in "mpa" in "pa".
6754 * The pullback is computed by applying "pa" to "mpa".
6756 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6757 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6759 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6762 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6763 * In other words, plug in "mpa2" in "mpa1".
6765 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6767 * We pullback each member of "mpa1" in turn.
6769 static __isl_give isl_multi_pw_aff
*
6770 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6771 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6774 isl_space
*space
= NULL
;
6776 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6780 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6781 isl_multi_pw_aff_get_space(mpa1
));
6783 for (i
= 0; i
< mpa1
->n
; ++i
) {
6784 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6785 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6790 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6792 isl_multi_pw_aff_free(mpa2
);
6795 isl_space_free(space
);
6796 isl_multi_pw_aff_free(mpa1
);
6797 isl_multi_pw_aff_free(mpa2
);
6801 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6802 * In other words, plug in "mpa2" in "mpa1".
6804 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6805 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6807 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6808 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6811 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6812 * of "mpa1" and "mpa2" live in the same space, construct map space
6813 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6814 * with this map space as extract argument.
6816 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6817 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6818 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6819 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6822 isl_space
*space1
, *space2
;
6825 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6826 isl_multi_pw_aff_get_space(mpa2
));
6827 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6828 isl_multi_pw_aff_get_space(mpa1
));
6831 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6832 mpa2
->space
, isl_dim_out
);
6836 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6837 "range spaces don't match", goto error
);
6838 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6839 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6840 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6842 res
= order(mpa1
, mpa2
, space1
);
6843 isl_multi_pw_aff_free(mpa1
);
6844 isl_multi_pw_aff_free(mpa2
);
6847 isl_multi_pw_aff_free(mpa1
);
6848 isl_multi_pw_aff_free(mpa2
);
6852 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6853 * where the function values are equal. "space" is the space of the result.
6854 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6856 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6857 * in the sequences are equal.
6859 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6860 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6861 __isl_take isl_space
*space
)
6866 res
= isl_map_universe(space
);
6868 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6869 for (i
= 0; i
< n
; ++i
) {
6870 isl_pw_aff
*pa1
, *pa2
;
6873 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6874 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6875 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6876 res
= isl_map_intersect(res
, map
);
6882 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6883 * where the function values are equal.
6885 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6886 __isl_take isl_multi_pw_aff
*mpa2
)
6888 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6889 &isl_multi_pw_aff_eq_map_on_space
);
6892 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6893 * where the function values of "mpa1" is lexicographically satisfies "base"
6894 * compared to that of "mpa2". "space" is the space of the result.
6895 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6897 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6898 * if its i-th element satisfies "base" when compared to
6899 * the i-th element of "mpa2" while all previous elements are
6902 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6903 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6904 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6905 __isl_take isl_pw_aff
*pa2
),
6906 __isl_take isl_space
*space
)
6909 isl_map
*res
, *rest
;
6911 res
= isl_map_empty(isl_space_copy(space
));
6912 rest
= isl_map_universe(space
);
6914 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6915 for (i
= 0; i
< n
; ++i
) {
6916 isl_pw_aff
*pa1
, *pa2
;
6919 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6920 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6921 map
= base(pa1
, pa2
);
6922 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6923 res
= isl_map_union(res
, map
);
6928 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6929 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6930 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6931 rest
= isl_map_intersect(rest
, map
);
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
6940 * of "mpa2". "space" is the space of the result.
6941 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6943 * "mpa1" is less than "mpa2" if its i-th element is smaller
6944 * than the i-th element of "mpa2" while all previous elements are
6947 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
6948 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6949 __isl_take isl_space
*space
)
6951 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6952 &isl_pw_aff_lt_map
, space
);
6955 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6956 * where the function value of "mpa1" is lexicographically less than that
6959 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
6960 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6962 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6963 &isl_multi_pw_aff_lex_lt_map_on_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
6968 * of "mpa2". "space" is the space of the result.
6969 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6971 * "mpa1" is greater than "mpa2" if its i-th element is greater
6972 * than the i-th element of "mpa2" while all previous elements are
6975 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
6976 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6977 __isl_take isl_space
*space
)
6979 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6980 &isl_pw_aff_gt_map
, space
);
6983 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6984 * where the function value of "mpa1" is lexicographically greater than that
6987 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
6988 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6990 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6991 &isl_multi_pw_aff_lex_gt_map_on_space
);
6994 /* Compare two isl_affs.
6996 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6997 * than "aff2" and 0 if they are equal.
6999 * The order is fairly arbitrary. We do consider expressions that only involve
7000 * earlier dimensions as "smaller".
7002 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7015 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7019 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7020 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7022 return last1
- last2
;
7024 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7027 /* Compare two isl_pw_affs.
7029 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7030 * than "pa2" and 0 if they are equal.
7032 * The order is fairly arbitrary. We do consider expressions that only involve
7033 * earlier dimensions as "smaller".
7035 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7036 __isl_keep isl_pw_aff
*pa2
)
7049 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7053 if (pa1
->n
!= pa2
->n
)
7054 return pa1
->n
- pa2
->n
;
7056 for (i
= 0; i
< pa1
->n
; ++i
) {
7057 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7060 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7068 /* Return a piecewise affine expression that is equal to "v" on "domain".
7070 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7071 __isl_take isl_val
*v
)
7074 isl_local_space
*ls
;
7077 space
= isl_set_get_space(domain
);
7078 ls
= isl_local_space_from_space(space
);
7079 aff
= isl_aff_val_on_domain(ls
, v
);
7081 return isl_pw_aff_alloc(domain
, aff
);
7084 /* Return a multi affine expression that is equal to "mv" on domain
7087 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7088 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7092 isl_local_space
*ls
;
7098 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7099 space2
= isl_multi_val_get_space(mv
);
7100 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7101 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7102 space
= isl_space_map_from_domain_and_range(space
, space2
);
7103 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7104 ls
= isl_local_space_from_space(isl_space_domain(space
));
7105 for (i
= 0; i
< n
; ++i
) {
7109 v
= isl_multi_val_get_val(mv
, i
);
7110 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7111 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7113 isl_local_space_free(ls
);
7115 isl_multi_val_free(mv
);
7118 isl_space_free(space
);
7119 isl_multi_val_free(mv
);
7123 /* Return a piecewise multi-affine expression
7124 * that is equal to "mv" on "domain".
7126 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7127 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7132 space
= isl_set_get_space(domain
);
7133 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7135 return isl_pw_multi_aff_alloc(domain
, ma
);
7138 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7139 * mv is the value that should be attained on each domain set
7140 * res collects the results
7142 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7144 isl_union_pw_multi_aff
*res
;
7147 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7148 * and add it to data->res.
7150 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7153 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7154 isl_pw_multi_aff
*pma
;
7157 mv
= isl_multi_val_copy(data
->mv
);
7158 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7159 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7161 return data
->res
? isl_stat_ok
: isl_stat_error
;
7164 /* Return a union piecewise multi-affine expression
7165 * that is equal to "mv" on "domain".
7167 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7168 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7170 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7173 space
= isl_union_set_get_space(domain
);
7174 data
.res
= isl_union_pw_multi_aff_empty(space
);
7176 if (isl_union_set_foreach_set(domain
,
7177 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7178 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7179 isl_union_set_free(domain
);
7180 isl_multi_val_free(mv
);
7184 /* Compute the pullback of data->pma by the function represented by "pma2",
7185 * provided the spaces match, and add the results to data->res.
7187 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7189 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7191 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7192 pma2
->dim
, isl_dim_out
)) {
7193 isl_pw_multi_aff_free(pma2
);
7197 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7198 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7200 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7202 return isl_stat_error
;
7207 /* Compute the pullback of "upma1" by the function represented by "upma2".
7209 __isl_give isl_union_pw_multi_aff
*
7210 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7211 __isl_take isl_union_pw_multi_aff
*upma1
,
7212 __isl_take isl_union_pw_multi_aff
*upma2
)
7214 return bin_op(upma1
, upma2
, &pullback_entry
);
7217 /* Check that the domain space of "upa" matches "space".
7219 * Return 0 on success and -1 on error.
7221 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7222 * can in principle never fail since the space "space" is that
7223 * of the isl_multi_union_pw_aff and is a set space such that
7224 * there is no domain space to match.
7226 * We check the parameters and double-check that "space" is
7227 * indeed that of a set.
7229 static int isl_union_pw_aff_check_match_domain_space(
7230 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7232 isl_space
*upa_space
;
7238 match
= isl_space_is_set(space
);
7242 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7243 "expecting set space", return -1);
7245 upa_space
= isl_union_pw_aff_get_space(upa
);
7246 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7250 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7251 "parameters don't match", goto error
);
7253 isl_space_free(upa_space
);
7256 isl_space_free(upa_space
);
7260 /* Do the parameters of "upa" match those of "space"?
7262 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7263 __isl_keep isl_space
*space
)
7265 isl_space
*upa_space
;
7271 upa_space
= isl_union_pw_aff_get_space(upa
);
7273 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7275 isl_space_free(upa_space
);
7279 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7280 * space represents the new parameters.
7281 * res collects the results.
7283 struct isl_union_pw_aff_reset_params_data
{
7285 isl_union_pw_aff
*res
;
7288 /* Replace the parameters of "pa" by data->space and
7289 * add the result to data->res.
7291 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7293 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7296 space
= isl_pw_aff_get_space(pa
);
7297 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7298 pa
= isl_pw_aff_reset_space(pa
, space
);
7299 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7301 return data
->res
? isl_stat_ok
: isl_stat_error
;
7304 /* Replace the domain space of "upa" by "space".
7305 * Since a union expression does not have a (single) domain space,
7306 * "space" is necessarily a parameter space.
7308 * Since the order and the names of the parameters determine
7309 * the hash value, we need to create a new hash table.
7311 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7312 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7314 struct isl_union_pw_aff_reset_params_data data
= { space
};
7317 match
= isl_union_pw_aff_matching_params(upa
, space
);
7319 upa
= isl_union_pw_aff_free(upa
);
7321 isl_space_free(space
);
7325 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7326 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7327 data
.res
= isl_union_pw_aff_free(data
.res
);
7329 isl_union_pw_aff_free(upa
);
7330 isl_space_free(space
);
7334 /* Return the floor of "pa".
7336 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7338 return isl_pw_aff_floor(pa
);
7341 /* Given f, return floor(f).
7343 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7344 __isl_take isl_union_pw_aff
*upa
)
7346 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7351 * upa mod m = upa - m * floor(upa/m)
7353 * with m an integer value.
7355 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7356 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7358 isl_union_pw_aff
*res
;
7363 if (!isl_val_is_int(m
))
7364 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7365 "expecting integer modulo", goto error
);
7366 if (!isl_val_is_pos(m
))
7367 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7368 "expecting positive modulo", goto error
);
7370 res
= isl_union_pw_aff_copy(upa
);
7371 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7372 upa
= isl_union_pw_aff_floor(upa
);
7373 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7374 res
= isl_union_pw_aff_sub(res
, upa
);
7379 isl_union_pw_aff_free(upa
);
7383 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7384 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7386 * "res" collects the results.
7388 struct isl_union_pw_aff_aff_on_domain_data
{
7390 isl_union_pw_aff
*res
;
7393 /* Construct a piecewise affine expression that is equal to data->aff
7394 * on "domain" and add the result to data->res.
7396 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7398 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7403 aff
= isl_aff_copy(data
->aff
);
7404 dim
= isl_set_dim(domain
, isl_dim_set
);
7405 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7406 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7407 pa
= isl_pw_aff_alloc(domain
, aff
);
7408 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7410 return data
->res
? isl_stat_ok
: isl_stat_error
;
7413 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7414 * pos is the output position that needs to be extracted.
7415 * res collects the results.
7417 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7419 isl_union_pw_aff
*res
;
7422 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7423 * (assuming it has such a dimension) and add it to data->res.
7425 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7427 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7432 return isl_stat_error
;
7434 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7435 if (data
->pos
>= n_out
) {
7436 isl_pw_multi_aff_free(pma
);
7440 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7441 isl_pw_multi_aff_free(pma
);
7443 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7445 return data
->res
? isl_stat_ok
: isl_stat_error
;
7448 /* Extract an isl_union_pw_aff corresponding to
7449 * output dimension "pos" of "upma".
7451 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7452 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7454 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7461 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7462 "cannot extract at negative position", return NULL
);
7464 space
= isl_union_pw_multi_aff_get_space(upma
);
7465 data
.res
= isl_union_pw_aff_empty(space
);
7467 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7468 &get_union_pw_aff
, &data
) < 0)
7469 data
.res
= isl_union_pw_aff_free(data
.res
);
7474 /* Return a union piecewise affine expression
7475 * that is equal to "aff" on "domain".
7477 * Construct an isl_pw_aff on each of the sets in "domain" and
7478 * collect the results.
7480 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7481 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7483 struct isl_union_pw_aff_aff_on_domain_data data
;
7486 if (!domain
|| !aff
)
7488 if (!isl_local_space_is_params(aff
->ls
))
7489 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7490 "expecting parametric expression", goto error
);
7492 space
= isl_union_set_get_space(domain
);
7493 data
.res
= isl_union_pw_aff_empty(space
);
7495 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7496 data
.res
= isl_union_pw_aff_free(data
.res
);
7497 isl_union_set_free(domain
);
7501 isl_union_set_free(domain
);
7506 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7507 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7508 * "res" collects the results.
7510 struct isl_union_pw_aff_val_on_domain_data
{
7512 isl_union_pw_aff
*res
;
7515 /* Construct a piecewise affine expression that is equal to data->v
7516 * on "domain" and add the result to data->res.
7518 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7520 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7524 v
= isl_val_copy(data
->v
);
7525 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7526 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7528 return data
->res
? isl_stat_ok
: isl_stat_error
;
7531 /* Return a union piecewise affine expression
7532 * that is equal to "v" on "domain".
7534 * Construct an isl_pw_aff on each of the sets in "domain" and
7535 * collect the results.
7537 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7538 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7540 struct isl_union_pw_aff_val_on_domain_data data
;
7543 space
= isl_union_set_get_space(domain
);
7544 data
.res
= isl_union_pw_aff_empty(space
);
7546 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7547 data
.res
= isl_union_pw_aff_free(data
.res
);
7548 isl_union_set_free(domain
);
7553 /* Construct a piecewise multi affine expression
7554 * that is equal to "pa" and add it to upma.
7556 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7559 isl_union_pw_multi_aff
**upma
= user
;
7560 isl_pw_multi_aff
*pma
;
7562 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7563 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7565 return *upma
? isl_stat_ok
: isl_stat_error
;
7568 /* Construct and return a union piecewise multi affine expression
7569 * that is equal to the given union piecewise affine expression.
7571 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7572 __isl_take isl_union_pw_aff
*upa
)
7575 isl_union_pw_multi_aff
*upma
;
7580 space
= isl_union_pw_aff_get_space(upa
);
7581 upma
= isl_union_pw_multi_aff_empty(space
);
7583 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7584 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7585 upma
= isl_union_pw_multi_aff_free(upma
);
7587 isl_union_pw_aff_free(upa
);
7591 /* Compute the set of elements in the domain of "pa" where it is zero and
7592 * add this set to "uset".
7594 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7596 isl_union_set
**uset
= (isl_union_set
**)user
;
7598 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7600 return *uset
? isl_stat_ok
: isl_stat_error
;
7603 /* Return a union set containing those elements in the domain
7604 * of "upa" where it is zero.
7606 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7607 __isl_take isl_union_pw_aff
*upa
)
7609 isl_union_set
*zero
;
7611 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7612 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7613 zero
= isl_union_set_free(zero
);
7615 isl_union_pw_aff_free(upa
);
7619 /* Convert "pa" to an isl_map and add it to *umap.
7621 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7623 isl_union_map
**umap
= user
;
7626 map
= isl_map_from_pw_aff(pa
);
7627 *umap
= isl_union_map_add_map(*umap
, map
);
7629 return *umap
? isl_stat_ok
: isl_stat_error
;
7632 /* Construct a union map mapping the domain of the union
7633 * piecewise affine expression to its range, with the single output dimension
7634 * equated to the corresponding affine expressions on their cells.
7636 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7637 __isl_take isl_union_pw_aff
*upa
)
7640 isl_union_map
*umap
;
7645 space
= isl_union_pw_aff_get_space(upa
);
7646 umap
= isl_union_map_empty(space
);
7648 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7650 umap
= isl_union_map_free(umap
);
7652 isl_union_pw_aff_free(upa
);
7656 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7657 * upma is the function that is plugged in.
7658 * pa is the current part of the function in which upma is plugged in.
7659 * res collects the results.
7661 struct isl_union_pw_aff_pullback_upma_data
{
7662 isl_union_pw_multi_aff
*upma
;
7664 isl_union_pw_aff
*res
;
7667 /* Check if "pma" can be plugged into data->pa.
7668 * If so, perform the pullback and add the result to data->res.
7670 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7672 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7675 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7676 pma
->dim
, isl_dim_out
)) {
7677 isl_pw_multi_aff_free(pma
);
7681 pa
= isl_pw_aff_copy(data
->pa
);
7682 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7684 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7686 return data
->res
? isl_stat_ok
: isl_stat_error
;
7689 /* Check if any of the elements of data->upma can be plugged into pa,
7690 * add if so add the result to data->res.
7692 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7694 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7698 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7700 isl_pw_aff_free(pa
);
7705 /* Compute the pullback of "upa" by the function represented by "upma".
7706 * In other words, plug in "upma" in "upa". The result contains
7707 * expressions defined over the domain space of "upma".
7709 * Run over all pairs of elements in "upa" and "upma", perform
7710 * the pullback when appropriate and collect the results.
7711 * If the hash value were based on the domain space rather than
7712 * the function space, then we could run through all elements
7713 * of "upma" and directly pick out the corresponding element of "upa".
7715 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7716 __isl_take isl_union_pw_aff
*upa
,
7717 __isl_take isl_union_pw_multi_aff
*upma
)
7719 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7722 space
= isl_union_pw_multi_aff_get_space(upma
);
7723 upa
= isl_union_pw_aff_align_params(upa
, space
);
7724 space
= isl_union_pw_aff_get_space(upa
);
7725 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7731 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7732 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7733 data
.res
= isl_union_pw_aff_free(data
.res
);
7735 isl_union_pw_aff_free(upa
);
7736 isl_union_pw_multi_aff_free(upma
);
7739 isl_union_pw_aff_free(upa
);
7740 isl_union_pw_multi_aff_free(upma
);
7745 #define BASE union_pw_aff
7747 #define DOMBASE union_set
7749 #define NO_MOVE_DIMS
7758 #include <isl_multi_templ.c>
7759 #include <isl_multi_apply_set.c>
7760 #include <isl_multi_apply_union_set.c>
7761 #include <isl_multi_coalesce.c>
7762 #include <isl_multi_floor.c>
7763 #include <isl_multi_gist.c>
7764 #include <isl_multi_intersect.c>
7766 /* Construct a multiple union piecewise affine expression
7767 * in the given space with value zero in each of the output dimensions.
7769 * Since there is no canonical zero value for
7770 * a union piecewise affine expression, we can only construct
7771 * zero-dimensional "zero" value.
7773 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7774 __isl_take isl_space
*space
)
7779 if (!isl_space_is_set(space
))
7780 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7781 "expecting set space", goto error
);
7782 if (isl_space_dim(space
, isl_dim_out
) != 0)
7783 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7784 "expecting 0D space", goto error
);
7786 return isl_multi_union_pw_aff_alloc(space
);
7788 isl_space_free(space
);
7792 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7793 * with the actual sum on the shared domain and
7794 * the defined expression on the symmetric difference of the domains.
7796 * We simply iterate over the elements in both arguments and
7797 * call isl_union_pw_aff_union_add on each of them.
7799 static __isl_give isl_multi_union_pw_aff
*
7800 isl_multi_union_pw_aff_union_add_aligned(
7801 __isl_take isl_multi_union_pw_aff
*mupa1
,
7802 __isl_take isl_multi_union_pw_aff
*mupa2
)
7804 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7805 &isl_union_pw_aff_union_add
);
7808 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7809 * with the actual sum on the shared domain and
7810 * the defined expression on the symmetric difference of the domains.
7812 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7813 __isl_take isl_multi_union_pw_aff
*mupa1
,
7814 __isl_take isl_multi_union_pw_aff
*mupa2
)
7816 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7817 &isl_multi_union_pw_aff_union_add_aligned
);
7820 /* Construct and return a multi union piecewise affine expression
7821 * that is equal to the given multi affine expression.
7823 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7824 __isl_take isl_multi_aff
*ma
)
7826 isl_multi_pw_aff
*mpa
;
7828 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7829 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7832 /* Construct and return a multi union piecewise affine expression
7833 * that is equal to the given multi piecewise affine expression.
7835 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7836 __isl_take isl_multi_pw_aff
*mpa
)
7840 isl_multi_union_pw_aff
*mupa
;
7845 space
= isl_multi_pw_aff_get_space(mpa
);
7846 space
= isl_space_range(space
);
7847 mupa
= isl_multi_union_pw_aff_alloc(space
);
7849 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7850 for (i
= 0; i
< n
; ++i
) {
7852 isl_union_pw_aff
*upa
;
7854 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7855 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7856 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7859 isl_multi_pw_aff_free(mpa
);
7864 /* Extract the range space of "pma" and assign it to *space.
7865 * If *space has already been set (through a previous call to this function),
7866 * then check that the range space is the same.
7868 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7870 isl_space
**space
= user
;
7871 isl_space
*pma_space
;
7874 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7875 isl_pw_multi_aff_free(pma
);
7878 return isl_stat_error
;
7884 equal
= isl_space_is_equal(pma_space
, *space
);
7885 isl_space_free(pma_space
);
7888 return isl_stat_error
;
7890 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7891 "range spaces not the same", return isl_stat_error
);
7895 /* Construct and return a multi union piecewise affine expression
7896 * that is equal to the given union piecewise multi affine expression.
7898 * In order to be able to perform the conversion, the input
7899 * needs to be non-empty and may only involve a single range space.
7901 __isl_give isl_multi_union_pw_aff
*
7902 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7903 __isl_take isl_union_pw_multi_aff
*upma
)
7905 isl_space
*space
= NULL
;
7906 isl_multi_union_pw_aff
*mupa
;
7911 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7912 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7913 "cannot extract range space from empty input",
7915 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7922 n
= isl_space_dim(space
, isl_dim_set
);
7923 mupa
= isl_multi_union_pw_aff_alloc(space
);
7925 for (i
= 0; i
< n
; ++i
) {
7926 isl_union_pw_aff
*upa
;
7928 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7929 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7932 isl_union_pw_multi_aff_free(upma
);
7935 isl_space_free(space
);
7936 isl_union_pw_multi_aff_free(upma
);
7940 /* Try and create an isl_multi_union_pw_aff that is equivalent
7941 * to the given isl_union_map.
7942 * The isl_union_map is required to be single-valued in each space.
7943 * Moreover, it cannot be empty and all range spaces need to be the same.
7944 * Otherwise, an error is produced.
7946 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7947 __isl_take isl_union_map
*umap
)
7949 isl_union_pw_multi_aff
*upma
;
7951 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7952 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7955 /* Return a multiple union piecewise affine expression
7956 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7957 * have been aligned.
7959 static __isl_give isl_multi_union_pw_aff
*
7960 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7961 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7965 isl_multi_union_pw_aff
*mupa
;
7970 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7971 space
= isl_multi_val_get_space(mv
);
7972 mupa
= isl_multi_union_pw_aff_alloc(space
);
7973 for (i
= 0; i
< n
; ++i
) {
7975 isl_union_pw_aff
*upa
;
7977 v
= isl_multi_val_get_val(mv
, i
);
7978 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
7980 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7983 isl_union_set_free(domain
);
7984 isl_multi_val_free(mv
);
7987 isl_union_set_free(domain
);
7988 isl_multi_val_free(mv
);
7992 /* Return a multiple union piecewise affine expression
7993 * that is equal to "mv" on "domain".
7995 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
7996 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8000 if (isl_space_match(domain
->dim
, isl_dim_param
,
8001 mv
->space
, isl_dim_param
))
8002 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8004 domain
= isl_union_set_align_params(domain
,
8005 isl_multi_val_get_space(mv
));
8006 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8007 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8009 isl_union_set_free(domain
);
8010 isl_multi_val_free(mv
);
8014 /* Return a multiple union piecewise affine expression
8015 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8016 * have been aligned.
8018 static __isl_give isl_multi_union_pw_aff
*
8019 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8020 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8024 isl_multi_union_pw_aff
*mupa
;
8029 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8030 space
= isl_multi_aff_get_space(ma
);
8031 mupa
= isl_multi_union_pw_aff_alloc(space
);
8032 for (i
= 0; i
< n
; ++i
) {
8034 isl_union_pw_aff
*upa
;
8036 aff
= isl_multi_aff_get_aff(ma
, i
);
8037 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8039 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8042 isl_union_set_free(domain
);
8043 isl_multi_aff_free(ma
);
8046 isl_union_set_free(domain
);
8047 isl_multi_aff_free(ma
);
8051 /* Return a multiple union piecewise affine expression
8052 * that is equal to "ma" on "domain".
8054 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8055 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8059 if (isl_space_match(domain
->dim
, isl_dim_param
,
8060 ma
->space
, isl_dim_param
))
8061 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8063 domain
= isl_union_set_align_params(domain
,
8064 isl_multi_aff_get_space(ma
));
8065 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8066 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8068 isl_union_set_free(domain
);
8069 isl_multi_aff_free(ma
);
8073 /* Return a union set containing those elements in the domains
8074 * of the elements of "mupa" where they are all zero.
8076 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8077 __isl_take isl_multi_union_pw_aff
*mupa
)
8080 isl_union_pw_aff
*upa
;
8081 isl_union_set
*zero
;
8086 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8088 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8089 "cannot determine zero set "
8090 "of zero-dimensional function", goto error
);
8092 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8093 zero
= isl_union_pw_aff_zero_union_set(upa
);
8095 for (i
= 1; i
< n
; ++i
) {
8096 isl_union_set
*zero_i
;
8098 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8099 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8101 zero
= isl_union_set_intersect(zero
, zero_i
);
8104 isl_multi_union_pw_aff_free(mupa
);
8107 isl_multi_union_pw_aff_free(mupa
);
8111 /* Construct a union map mapping the shared domain
8112 * of the union piecewise affine expressions to the range of "mupa"
8113 * with each dimension in the range equated to the
8114 * corresponding union piecewise affine expression.
8116 * The input cannot be zero-dimensional as there is
8117 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8119 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8120 __isl_take isl_multi_union_pw_aff
*mupa
)
8124 isl_union_map
*umap
;
8125 isl_union_pw_aff
*upa
;
8130 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8132 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8133 "cannot determine domain of zero-dimensional "
8134 "isl_multi_union_pw_aff", goto error
);
8136 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8137 umap
= isl_union_map_from_union_pw_aff(upa
);
8139 for (i
= 1; i
< n
; ++i
) {
8140 isl_union_map
*umap_i
;
8142 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8143 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8144 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8147 space
= isl_multi_union_pw_aff_get_space(mupa
);
8148 umap
= isl_union_map_reset_range_space(umap
, space
);
8150 isl_multi_union_pw_aff_free(mupa
);
8153 isl_multi_union_pw_aff_free(mupa
);
8157 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8158 * "range" is the space from which to set the range space.
8159 * "res" collects the results.
8161 struct isl_union_pw_multi_aff_reset_range_space_data
{
8163 isl_union_pw_multi_aff
*res
;
8166 /* Replace the range space of "pma" by the range space of data->range and
8167 * add the result to data->res.
8169 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8171 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8174 space
= isl_pw_multi_aff_get_space(pma
);
8175 space
= isl_space_domain(space
);
8176 space
= isl_space_extend_domain_with_range(space
,
8177 isl_space_copy(data
->range
));
8178 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8179 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8181 return data
->res
? isl_stat_ok
: isl_stat_error
;
8184 /* Replace the range space of all the piecewise affine expressions in "upma" by
8185 * the range space of "space".
8187 * This assumes that all these expressions have the same output dimension.
8189 * Since the spaces of the expressions change, so do their hash values.
8190 * We therefore need to create a new isl_union_pw_multi_aff.
8191 * Note that the hash value is currently computed based on the entire
8192 * space even though there can only be a single expression with a given
8195 static __isl_give isl_union_pw_multi_aff
*
8196 isl_union_pw_multi_aff_reset_range_space(
8197 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8199 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8200 isl_space
*space_upma
;
8202 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8203 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8204 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8205 &reset_range_space
, &data
) < 0)
8206 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8208 isl_space_free(space
);
8209 isl_union_pw_multi_aff_free(upma
);
8213 /* Construct and return a union piecewise multi affine expression
8214 * that is equal to the given multi union piecewise affine expression.
8216 * In order to be able to perform the conversion, the input
8217 * needs to have a least one output dimension.
8219 __isl_give isl_union_pw_multi_aff
*
8220 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8221 __isl_take isl_multi_union_pw_aff
*mupa
)
8225 isl_union_pw_multi_aff
*upma
;
8226 isl_union_pw_aff
*upa
;
8231 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8233 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8234 "cannot determine domain of zero-dimensional "
8235 "isl_multi_union_pw_aff", goto error
);
8237 space
= isl_multi_union_pw_aff_get_space(mupa
);
8238 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8239 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8241 for (i
= 1; i
< n
; ++i
) {
8242 isl_union_pw_multi_aff
*upma_i
;
8244 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8245 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8246 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8249 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8251 isl_multi_union_pw_aff_free(mupa
);
8254 isl_multi_union_pw_aff_free(mupa
);
8258 /* Intersect the range of "mupa" with "range".
8259 * That is, keep only those domain elements that have a function value
8262 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8263 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8265 isl_union_pw_multi_aff
*upma
;
8266 isl_union_set
*domain
;
8271 if (!mupa
|| !range
)
8274 space
= isl_set_get_space(range
);
8275 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8276 space
, isl_dim_set
);
8277 isl_space_free(space
);
8281 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8282 "space don't match", goto error
);
8283 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8285 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8286 "cannot intersect range of zero-dimensional "
8287 "isl_multi_union_pw_aff", goto error
);
8289 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8290 isl_multi_union_pw_aff_copy(mupa
));
8291 domain
= isl_union_set_from_set(range
);
8292 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8293 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8297 isl_multi_union_pw_aff_free(mupa
);
8298 isl_set_free(range
);
8302 /* Return the shared domain of the elements of "mupa".
8304 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8305 __isl_take isl_multi_union_pw_aff
*mupa
)
8308 isl_union_pw_aff
*upa
;
8314 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8316 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8317 "cannot determine domain", goto error
);
8319 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8320 dom
= isl_union_pw_aff_domain(upa
);
8321 for (i
= 1; i
< n
; ++i
) {
8322 isl_union_set
*dom_i
;
8324 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8325 dom_i
= isl_union_pw_aff_domain(upa
);
8326 dom
= isl_union_set_intersect(dom
, dom_i
);
8329 isl_multi_union_pw_aff_free(mupa
);
8332 isl_multi_union_pw_aff_free(mupa
);
8336 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8337 * In particular, the spaces have been aligned.
8338 * The result is defined over the shared domain of the elements of "mupa"
8340 * We first extract the parametric constant part of "aff" and
8341 * define that over the shared domain.
8342 * Then we iterate over all input dimensions of "aff" and add the corresponding
8343 * multiples of the elements of "mupa".
8344 * Finally, we consider the integer divisions, calling the function
8345 * recursively to obtain an isl_union_pw_aff corresponding to the
8346 * integer division argument.
8348 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8349 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8352 isl_union_pw_aff
*upa
;
8353 isl_union_set
*uset
;
8357 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8358 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8360 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8361 cst
= isl_aff_copy(aff
);
8362 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8363 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8364 cst
= isl_aff_project_domain_on_params(cst
);
8365 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8367 for (i
= 0; i
< n_in
; ++i
) {
8368 isl_union_pw_aff
*upa_i
;
8370 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8372 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8373 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8374 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8375 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8378 for (i
= 0; i
< n_div
; ++i
) {
8380 isl_union_pw_aff
*upa_i
;
8382 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8384 div
= isl_aff_get_div(aff
, i
);
8385 upa_i
= multi_union_pw_aff_apply_aff(
8386 isl_multi_union_pw_aff_copy(mupa
), div
);
8387 upa_i
= isl_union_pw_aff_floor(upa_i
);
8388 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8389 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8390 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8393 isl_multi_union_pw_aff_free(mupa
);
8399 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8400 * with the domain of "aff".
8401 * Furthermore, the dimension of this space needs to be greater than zero.
8402 * The result is defined over the shared domain of the elements of "mupa"
8404 * We perform these checks and then hand over control to
8405 * multi_union_pw_aff_apply_aff.
8407 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8408 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8410 isl_space
*space1
, *space2
;
8413 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8414 isl_aff_get_space(aff
));
8415 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8419 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8420 space2
= isl_aff_get_domain_space(aff
);
8421 equal
= isl_space_is_equal(space1
, space2
);
8422 isl_space_free(space1
);
8423 isl_space_free(space2
);
8427 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8428 "spaces don't match", goto error
);
8429 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8430 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8431 "cannot determine domains", goto error
);
8433 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8435 isl_multi_union_pw_aff_free(mupa
);
8440 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8441 * with the domain of "ma".
8442 * Furthermore, the dimension of this space needs to be greater than zero,
8443 * unless the dimension of the target space of "ma" is also zero.
8444 * The result is defined over the shared domain of the elements of "mupa"
8446 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8447 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8449 isl_space
*space1
, *space2
;
8450 isl_multi_union_pw_aff
*res
;
8454 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8455 isl_multi_aff_get_space(ma
));
8456 ma
= isl_multi_aff_align_params(ma
,
8457 isl_multi_union_pw_aff_get_space(mupa
));
8461 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8462 space2
= isl_multi_aff_get_domain_space(ma
);
8463 equal
= isl_space_is_equal(space1
, space2
);
8464 isl_space_free(space1
);
8465 isl_space_free(space2
);
8469 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8470 "spaces don't match", goto error
);
8471 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8472 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8473 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8474 "cannot determine domains", goto error
);
8476 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8477 res
= isl_multi_union_pw_aff_alloc(space1
);
8479 for (i
= 0; i
< n_out
; ++i
) {
8481 isl_union_pw_aff
*upa
;
8483 aff
= isl_multi_aff_get_aff(ma
, i
);
8484 upa
= multi_union_pw_aff_apply_aff(
8485 isl_multi_union_pw_aff_copy(mupa
), aff
);
8486 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8489 isl_multi_aff_free(ma
);
8490 isl_multi_union_pw_aff_free(mupa
);
8493 isl_multi_union_pw_aff_free(mupa
);
8494 isl_multi_aff_free(ma
);
8498 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8499 * with the domain of "pa".
8500 * Furthermore, the dimension of this space needs to be greater than zero.
8501 * The result is defined over the shared domain of the elements of "mupa"
8503 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8504 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8508 isl_space
*space
, *space2
;
8509 isl_union_pw_aff
*upa
;
8511 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8512 isl_pw_aff_get_space(pa
));
8513 pa
= isl_pw_aff_align_params(pa
,
8514 isl_multi_union_pw_aff_get_space(mupa
));
8518 space
= isl_multi_union_pw_aff_get_space(mupa
);
8519 space2
= isl_pw_aff_get_domain_space(pa
);
8520 equal
= isl_space_is_equal(space
, space2
);
8521 isl_space_free(space
);
8522 isl_space_free(space2
);
8526 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8527 "spaces don't match", goto error
);
8528 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8529 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8530 "cannot determine domains", goto error
);
8532 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8533 upa
= isl_union_pw_aff_empty(space
);
8535 for (i
= 0; i
< pa
->n
; ++i
) {
8538 isl_multi_union_pw_aff
*mupa_i
;
8539 isl_union_pw_aff
*upa_i
;
8541 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8542 domain
= isl_set_copy(pa
->p
[i
].set
);
8543 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8544 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8545 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8546 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8549 isl_multi_union_pw_aff_free(mupa
);
8550 isl_pw_aff_free(pa
);
8553 isl_multi_union_pw_aff_free(mupa
);
8554 isl_pw_aff_free(pa
);
8558 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8559 * with the domain of "pma".
8560 * Furthermore, the dimension of this space needs to be greater than zero,
8561 * unless the dimension of the target space of "pma" is also zero.
8562 * The result is defined over the shared domain of the elements of "mupa"
8564 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8565 __isl_take isl_multi_union_pw_aff
*mupa
,
8566 __isl_take isl_pw_multi_aff
*pma
)
8568 isl_space
*space1
, *space2
;
8569 isl_multi_union_pw_aff
*res
;
8573 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8574 isl_pw_multi_aff_get_space(pma
));
8575 pma
= isl_pw_multi_aff_align_params(pma
,
8576 isl_multi_union_pw_aff_get_space(mupa
));
8580 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8581 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8582 equal
= isl_space_is_equal(space1
, space2
);
8583 isl_space_free(space1
);
8584 isl_space_free(space2
);
8588 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8589 "spaces don't match", goto error
);
8590 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8591 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8592 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8593 "cannot determine domains", goto error
);
8595 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8596 res
= isl_multi_union_pw_aff_alloc(space1
);
8598 for (i
= 0; i
< n_out
; ++i
) {
8600 isl_union_pw_aff
*upa
;
8602 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8603 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8604 isl_multi_union_pw_aff_copy(mupa
), pa
);
8605 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8608 isl_pw_multi_aff_free(pma
);
8609 isl_multi_union_pw_aff_free(mupa
);
8612 isl_multi_union_pw_aff_free(mupa
);
8613 isl_pw_multi_aff_free(pma
);
8617 /* Compute the pullback of "mupa" by the function represented by "upma".
8618 * In other words, plug in "upma" in "mupa". The result contains
8619 * expressions defined over the domain space of "upma".
8621 * Run over all elements of "mupa" and plug in "upma" in each of them.
8623 __isl_give isl_multi_union_pw_aff
*
8624 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8625 __isl_take isl_multi_union_pw_aff
*mupa
,
8626 __isl_take isl_union_pw_multi_aff
*upma
)
8630 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8631 isl_union_pw_multi_aff_get_space(upma
));
8632 upma
= isl_union_pw_multi_aff_align_params(upma
,
8633 isl_multi_union_pw_aff_get_space(mupa
));
8637 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8638 for (i
= 0; i
< n
; ++i
) {
8639 isl_union_pw_aff
*upa
;
8641 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8642 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8643 isl_union_pw_multi_aff_copy(upma
));
8644 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8647 isl_union_pw_multi_aff_free(upma
);
8650 isl_multi_union_pw_aff_free(mupa
);
8651 isl_union_pw_multi_aff_free(upma
);
8655 /* Extract the sequence of elements in "mupa" with domain space "space"
8656 * (ignoring parameters).
8658 * For the elements of "mupa" that are not defined on the specified space,
8659 * the corresponding element in the result is empty.
8661 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8662 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8665 isl_space
*space_mpa
= NULL
;
8666 isl_multi_pw_aff
*mpa
;
8668 if (!mupa
|| !space
)
8671 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8672 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8673 space
= isl_space_drop_dims(space
, isl_dim_param
,
8674 0, isl_space_dim(space
, isl_dim_param
));
8675 space
= isl_space_align_params(space
,
8676 isl_space_copy(space_mpa
));
8680 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8682 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8684 space
= isl_space_from_domain(space
);
8685 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8686 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8687 for (i
= 0; i
< n
; ++i
) {
8688 isl_union_pw_aff
*upa
;
8691 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8692 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8693 isl_space_copy(space
));
8694 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8695 isl_union_pw_aff_free(upa
);
8698 isl_space_free(space
);
8701 isl_space_free(space_mpa
);
8702 isl_space_free(space
);