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
);
421 isl_seq_cpy(res
->el
, vec
->el
, 2);
422 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
423 for (i
= 0; i
< r
->len
; ++i
)
424 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
426 isl_reordering_free(r
);
431 isl_reordering_free(r
);
435 /* Reorder the dimensions of the domain of "aff" according
436 * to the given reordering.
438 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
439 __isl_take isl_reordering
*r
)
441 aff
= isl_aff_cow(aff
);
445 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
446 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
447 aff
->ls
->div
->n_row
);
448 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
450 if (!aff
->v
|| !aff
->ls
)
451 return isl_aff_free(aff
);
456 isl_reordering_free(r
);
460 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
461 __isl_take isl_space
*model
)
466 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
467 model
, isl_dim_param
)) {
470 model
= isl_space_drop_dims(model
, isl_dim_in
,
471 0, isl_space_dim(model
, isl_dim_in
));
472 model
= isl_space_drop_dims(model
, isl_dim_out
,
473 0, isl_space_dim(model
, isl_dim_out
));
474 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
475 exp
= isl_reordering_extend_space(exp
,
476 isl_aff_get_domain_space(aff
));
477 aff
= isl_aff_realign_domain(aff
, exp
);
480 isl_space_free(model
);
483 isl_space_free(model
);
488 /* Is "aff" obviously equal to zero?
490 * If the denominator is zero, then "aff" is not equal to zero.
492 isl_bool
isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
495 return isl_bool_error
;
497 if (isl_int_is_zero(aff
->v
->el
[0]))
498 return isl_bool_false
;
499 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
502 /* Does "aff" represent NaN?
504 isl_bool
isl_aff_is_nan(__isl_keep isl_aff
*aff
)
507 return isl_bool_error
;
509 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
512 /* Does "pa" involve any NaNs?
514 isl_bool
isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
519 return isl_bool_error
;
521 return isl_bool_false
;
523 for (i
= 0; i
< pa
->n
; ++i
) {
524 isl_bool is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
525 if (is_nan
< 0 || is_nan
)
529 return isl_bool_false
;
532 /* Are "aff1" and "aff2" obviously equal?
534 * NaN is not equal to anything, not even to another NaN.
536 isl_bool
isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
,
537 __isl_keep isl_aff
*aff2
)
542 return isl_bool_error
;
544 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
545 return isl_bool_false
;
547 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
548 if (equal
< 0 || !equal
)
551 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
554 /* Return the common denominator of "aff" in "v".
556 * We cannot return anything meaningful in case of a NaN.
558 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
562 if (isl_aff_is_nan(aff
))
563 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
564 "cannot get denominator of NaN", return -1);
565 isl_int_set(*v
, aff
->v
->el
[0]);
569 /* Return the common denominator of "aff".
571 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
578 ctx
= isl_aff_get_ctx(aff
);
579 if (isl_aff_is_nan(aff
))
580 return isl_val_nan(ctx
);
581 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
584 /* Return the constant term of "aff" in "v".
586 * We cannot return anything meaningful in case of a NaN.
588 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
592 if (isl_aff_is_nan(aff
))
593 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
594 "cannot get constant term of NaN", return -1);
595 isl_int_set(*v
, aff
->v
->el
[1]);
599 /* Return the constant term of "aff".
601 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
609 ctx
= isl_aff_get_ctx(aff
);
610 if (isl_aff_is_nan(aff
))
611 return isl_val_nan(ctx
);
612 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
613 return isl_val_normalize(v
);
616 /* Return the coefficient of the variable of type "type" at position "pos"
619 * We cannot return anything meaningful in case of a NaN.
621 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
622 enum isl_dim_type type
, int pos
, isl_int
*v
)
627 if (type
== isl_dim_out
)
628 isl_die(aff
->v
->ctx
, isl_error_invalid
,
629 "output/set dimension does not have a coefficient",
631 if (type
== isl_dim_in
)
634 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
635 isl_die(aff
->v
->ctx
, isl_error_invalid
,
636 "position out of bounds", return -1);
638 if (isl_aff_is_nan(aff
))
639 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
640 "cannot get coefficient of NaN", return -1);
641 pos
+= isl_local_space_offset(aff
->ls
, type
);
642 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
647 /* Return the coefficient of the variable of type "type" at position "pos"
650 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
651 enum isl_dim_type type
, int pos
)
659 ctx
= isl_aff_get_ctx(aff
);
660 if (type
== isl_dim_out
)
661 isl_die(ctx
, isl_error_invalid
,
662 "output/set dimension does not have a coefficient",
664 if (type
== isl_dim_in
)
667 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
668 isl_die(ctx
, isl_error_invalid
,
669 "position out of bounds", return NULL
);
671 if (isl_aff_is_nan(aff
))
672 return isl_val_nan(ctx
);
673 pos
+= isl_local_space_offset(aff
->ls
, type
);
674 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
675 return isl_val_normalize(v
);
678 /* Return the sign of the coefficient of the variable of type "type"
679 * at position "pos" of "aff".
681 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
689 ctx
= isl_aff_get_ctx(aff
);
690 if (type
== isl_dim_out
)
691 isl_die(ctx
, isl_error_invalid
,
692 "output/set dimension does not have a coefficient",
694 if (type
== isl_dim_in
)
697 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
698 isl_die(ctx
, isl_error_invalid
,
699 "position out of bounds", return 0);
701 pos
+= isl_local_space_offset(aff
->ls
, type
);
702 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
705 /* Replace the denominator of "aff" by "v".
707 * A NaN is unaffected by this operation.
709 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
713 if (isl_aff_is_nan(aff
))
715 aff
= isl_aff_cow(aff
);
719 aff
->v
= isl_vec_cow(aff
->v
);
721 return isl_aff_free(aff
);
723 isl_int_set(aff
->v
->el
[0], v
);
728 /* Replace the numerator of the constant term of "aff" by "v".
730 * A NaN is unaffected by this operation.
732 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
736 if (isl_aff_is_nan(aff
))
738 aff
= isl_aff_cow(aff
);
742 aff
->v
= isl_vec_cow(aff
->v
);
744 return isl_aff_free(aff
);
746 isl_int_set(aff
->v
->el
[1], v
);
751 /* Replace the constant term of "aff" by "v".
753 * A NaN is unaffected by this operation.
755 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
756 __isl_take isl_val
*v
)
761 if (isl_aff_is_nan(aff
)) {
766 if (!isl_val_is_rat(v
))
767 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
768 "expecting rational value", goto error
);
770 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
771 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
776 aff
= isl_aff_cow(aff
);
779 aff
->v
= isl_vec_cow(aff
->v
);
783 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
784 isl_int_set(aff
->v
->el
[1], v
->n
);
785 } else if (isl_int_is_one(v
->d
)) {
786 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
788 isl_seq_scale(aff
->v
->el
+ 1,
789 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
790 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
791 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
792 aff
->v
= isl_vec_normalize(aff
->v
);
805 /* Add "v" to the constant term of "aff".
807 * A NaN is unaffected by this operation.
809 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
811 if (isl_int_is_zero(v
))
816 if (isl_aff_is_nan(aff
))
818 aff
= isl_aff_cow(aff
);
822 aff
->v
= isl_vec_cow(aff
->v
);
824 return isl_aff_free(aff
);
826 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
831 /* Add "v" to the constant term of "aff".
833 * A NaN is unaffected by this operation.
835 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
836 __isl_take isl_val
*v
)
841 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
846 if (!isl_val_is_rat(v
))
847 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
848 "expecting rational value", goto error
);
850 aff
= isl_aff_cow(aff
);
854 aff
->v
= isl_vec_cow(aff
->v
);
858 if (isl_int_is_one(v
->d
)) {
859 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
860 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
861 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
862 aff
->v
= isl_vec_normalize(aff
->v
);
866 isl_seq_scale(aff
->v
->el
+ 1,
867 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
868 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
869 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
870 aff
->v
= isl_vec_normalize(aff
->v
);
883 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
888 isl_int_set_si(t
, v
);
889 aff
= isl_aff_add_constant(aff
, t
);
895 /* Add "v" to the numerator of the constant term of "aff".
897 * A NaN is unaffected by this operation.
899 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
901 if (isl_int_is_zero(v
))
906 if (isl_aff_is_nan(aff
))
908 aff
= isl_aff_cow(aff
);
912 aff
->v
= isl_vec_cow(aff
->v
);
914 return isl_aff_free(aff
);
916 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
921 /* Add "v" to the numerator of the constant term of "aff".
923 * A NaN is unaffected by this operation.
925 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
933 isl_int_set_si(t
, v
);
934 aff
= isl_aff_add_constant_num(aff
, t
);
940 /* Replace the numerator of the constant term of "aff" by "v".
942 * A NaN is unaffected by this operation.
944 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
948 if (isl_aff_is_nan(aff
))
950 aff
= isl_aff_cow(aff
);
954 aff
->v
= isl_vec_cow(aff
->v
);
956 return isl_aff_free(aff
);
958 isl_int_set_si(aff
->v
->el
[1], v
);
963 /* Replace the numerator of the coefficient of the variable of type "type"
964 * at position "pos" of "aff" by "v".
966 * A NaN is unaffected by this operation.
968 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
969 enum isl_dim_type type
, int pos
, isl_int v
)
974 if (type
== isl_dim_out
)
975 isl_die(aff
->v
->ctx
, isl_error_invalid
,
976 "output/set dimension does not have a coefficient",
977 return isl_aff_free(aff
));
978 if (type
== isl_dim_in
)
981 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
982 isl_die(aff
->v
->ctx
, isl_error_invalid
,
983 "position out of bounds", return isl_aff_free(aff
));
985 if (isl_aff_is_nan(aff
))
987 aff
= isl_aff_cow(aff
);
991 aff
->v
= isl_vec_cow(aff
->v
);
993 return isl_aff_free(aff
);
995 pos
+= isl_local_space_offset(aff
->ls
, type
);
996 isl_int_set(aff
->v
->el
[1 + pos
], v
);
1001 /* Replace the numerator of the coefficient of the variable of type "type"
1002 * at position "pos" of "aff" by "v".
1004 * A NaN is unaffected by this operation.
1006 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
1007 enum isl_dim_type type
, int pos
, int v
)
1012 if (type
== isl_dim_out
)
1013 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1014 "output/set dimension does not have a coefficient",
1015 return isl_aff_free(aff
));
1016 if (type
== isl_dim_in
)
1019 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
1020 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1021 "position out of bounds", return isl_aff_free(aff
));
1023 if (isl_aff_is_nan(aff
))
1025 pos
+= isl_local_space_offset(aff
->ls
, type
);
1026 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1029 aff
= isl_aff_cow(aff
);
1033 aff
->v
= isl_vec_cow(aff
->v
);
1035 return isl_aff_free(aff
);
1037 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1042 /* Replace the coefficient of the variable of type "type" at position "pos"
1045 * A NaN is unaffected by this operation.
1047 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1048 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1053 if (type
== isl_dim_out
)
1054 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1055 "output/set dimension does not have a coefficient",
1057 if (type
== isl_dim_in
)
1060 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1061 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1062 "position out of bounds", goto error
);
1064 if (isl_aff_is_nan(aff
)) {
1068 if (!isl_val_is_rat(v
))
1069 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1070 "expecting rational value", goto error
);
1072 pos
+= isl_local_space_offset(aff
->ls
, type
);
1073 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1074 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1079 aff
= isl_aff_cow(aff
);
1082 aff
->v
= isl_vec_cow(aff
->v
);
1086 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1087 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1088 } else if (isl_int_is_one(v
->d
)) {
1089 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1091 isl_seq_scale(aff
->v
->el
+ 1,
1092 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1093 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1094 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1095 aff
->v
= isl_vec_normalize(aff
->v
);
1108 /* Add "v" to the coefficient of the variable of type "type"
1109 * at position "pos" of "aff".
1111 * A NaN is unaffected by this operation.
1113 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1114 enum isl_dim_type type
, int pos
, isl_int v
)
1119 if (type
== isl_dim_out
)
1120 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1121 "output/set dimension does not have a coefficient",
1122 return isl_aff_free(aff
));
1123 if (type
== isl_dim_in
)
1126 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1127 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1128 "position out of bounds", return isl_aff_free(aff
));
1130 if (isl_aff_is_nan(aff
))
1132 aff
= isl_aff_cow(aff
);
1136 aff
->v
= isl_vec_cow(aff
->v
);
1138 return isl_aff_free(aff
);
1140 pos
+= isl_local_space_offset(aff
->ls
, type
);
1141 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1146 /* Add "v" to the coefficient of the variable of type "type"
1147 * at position "pos" of "aff".
1149 * A NaN is unaffected by this operation.
1151 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1152 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1157 if (isl_val_is_zero(v
)) {
1162 if (type
== isl_dim_out
)
1163 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1164 "output/set dimension does not have a coefficient",
1166 if (type
== isl_dim_in
)
1169 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1170 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1171 "position out of bounds", goto error
);
1173 if (isl_aff_is_nan(aff
)) {
1177 if (!isl_val_is_rat(v
))
1178 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1179 "expecting rational value", goto error
);
1181 aff
= isl_aff_cow(aff
);
1185 aff
->v
= isl_vec_cow(aff
->v
);
1189 pos
+= isl_local_space_offset(aff
->ls
, type
);
1190 if (isl_int_is_one(v
->d
)) {
1191 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1192 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1193 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1194 aff
->v
= isl_vec_normalize(aff
->v
);
1198 isl_seq_scale(aff
->v
->el
+ 1,
1199 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1200 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1201 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1202 aff
->v
= isl_vec_normalize(aff
->v
);
1215 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1216 enum isl_dim_type type
, int pos
, int v
)
1221 isl_int_set_si(t
, v
);
1222 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1228 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1233 return isl_local_space_get_div(aff
->ls
, pos
);
1236 /* Return the negation of "aff".
1238 * As a special case, -NaN = NaN.
1240 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1244 if (isl_aff_is_nan(aff
))
1246 aff
= isl_aff_cow(aff
);
1249 aff
->v
= isl_vec_cow(aff
->v
);
1251 return isl_aff_free(aff
);
1253 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1258 /* Remove divs from the local space that do not appear in the affine
1260 * We currently only remove divs at the end.
1261 * Some intermediate divs may also not appear directly in the affine
1262 * expression, but we would also need to check that no other divs are
1263 * defined in terms of them.
1265 __isl_give isl_aff
*isl_aff_remove_unused_divs(__isl_take isl_aff
*aff
)
1274 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1275 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1277 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1281 aff
= isl_aff_cow(aff
);
1285 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1286 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1287 if (!aff
->ls
|| !aff
->v
)
1288 return isl_aff_free(aff
);
1293 /* Given two affine expressions "p" of length p_len (including the
1294 * denominator and the constant term) and "subs" of length subs_len,
1295 * plug in "subs" for the variable at position "pos".
1296 * The variables of "subs" and "p" are assumed to match up to subs_len,
1297 * but "p" may have additional variables.
1298 * "v" is an initialized isl_int that can be used internally.
1300 * In particular, if "p" represents the expression
1304 * with i the variable at position "pos" and "subs" represents the expression
1308 * then the result represents the expression
1313 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1314 int p_len
, int subs_len
, isl_int v
)
1316 isl_int_set(v
, p
[1 + pos
]);
1317 isl_int_set_si(p
[1 + pos
], 0);
1318 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1319 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1320 isl_int_mul(p
[0], p
[0], subs
[0]);
1323 /* Look for any divs in the aff->ls with a denominator equal to one
1324 * and plug them into the affine expression and any subsequent divs
1325 * that may reference the div.
1327 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1333 isl_local_space
*ls
;
1339 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1341 for (i
= 0; i
< n
; ++i
) {
1342 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1344 ls
= isl_local_space_copy(aff
->ls
);
1345 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1346 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1347 vec
= isl_vec_copy(aff
->v
);
1348 vec
= isl_vec_cow(vec
);
1354 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1355 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1360 isl_vec_free(aff
->v
);
1362 isl_local_space_free(aff
->ls
);
1369 isl_local_space_free(ls
);
1370 return isl_aff_free(aff
);
1373 /* Look for any divs j that appear with a unit coefficient inside
1374 * the definitions of other divs i and plug them into the definitions
1377 * In particular, an expression of the form
1379 * floor((f(..) + floor(g(..)/n))/m)
1383 * floor((n * f(..) + g(..))/(n * m))
1385 * This simplification is correct because we can move the expression
1386 * f(..) into the inner floor in the original expression to obtain
1388 * floor(floor((n * f(..) + g(..))/n)/m)
1390 * from which we can derive the simplified expression.
1392 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1400 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1401 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1402 for (i
= 1; i
< n
; ++i
) {
1403 for (j
= 0; j
< i
; ++j
) {
1404 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1406 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1407 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1408 aff
->v
->size
, i
, 1);
1410 return isl_aff_free(aff
);
1417 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1419 * Even though this function is only called on isl_affs with a single
1420 * reference, we are careful to only change aff->v and aff->ls together.
1422 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1424 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1425 isl_local_space
*ls
;
1428 ls
= isl_local_space_copy(aff
->ls
);
1429 ls
= isl_local_space_swap_div(ls
, a
, b
);
1430 v
= isl_vec_copy(aff
->v
);
1435 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1436 isl_vec_free(aff
->v
);
1438 isl_local_space_free(aff
->ls
);
1444 isl_local_space_free(ls
);
1445 return isl_aff_free(aff
);
1448 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1450 * We currently do not actually remove div "b", but simply add its
1451 * coefficient to that of "a" and then zero it out.
1453 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1455 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1457 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1460 aff
->v
= isl_vec_cow(aff
->v
);
1462 return isl_aff_free(aff
);
1464 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1465 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1466 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1471 /* Sort the divs in the local space of "aff" according to
1472 * the comparison function "cmp_row" in isl_local_space.c,
1473 * combining the coefficients of identical divs.
1475 * Reordering divs does not change the semantics of "aff",
1476 * so there is no need to call isl_aff_cow.
1477 * Moreover, this function is currently only called on isl_affs
1478 * with a single reference.
1480 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1487 n
= isl_aff_dim(aff
, isl_dim_div
);
1488 for (i
= 1; i
< n
; ++i
) {
1489 for (j
= i
- 1; j
>= 0; --j
) {
1490 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1494 aff
= merge_divs(aff
, j
, j
+ 1);
1496 aff
= swap_div(aff
, j
, j
+ 1);
1505 /* Normalize the representation of "aff".
1507 * This function should only be called of "new" isl_affs, i.e.,
1508 * with only a single reference. We therefore do not need to
1509 * worry about affecting other instances.
1511 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1515 aff
->v
= isl_vec_normalize(aff
->v
);
1517 return isl_aff_free(aff
);
1518 aff
= plug_in_integral_divs(aff
);
1519 aff
= plug_in_unit_divs(aff
);
1520 aff
= sort_divs(aff
);
1521 aff
= isl_aff_remove_unused_divs(aff
);
1525 /* Given f, return floor(f).
1526 * If f is an integer expression, then just return f.
1527 * If f is a constant, then return the constant floor(f).
1528 * Otherwise, if f = g/m, write g = q m + r,
1529 * create a new div d = [r/m] and return the expression q + d.
1530 * The coefficients in r are taken to lie between -m/2 and m/2.
1532 * As a special case, floor(NaN) = NaN.
1534 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1544 if (isl_aff_is_nan(aff
))
1546 if (isl_int_is_one(aff
->v
->el
[0]))
1549 aff
= isl_aff_cow(aff
);
1553 aff
->v
= isl_vec_cow(aff
->v
);
1555 return isl_aff_free(aff
);
1557 if (isl_aff_is_cst(aff
)) {
1558 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1559 isl_int_set_si(aff
->v
->el
[0], 1);
1563 div
= isl_vec_copy(aff
->v
);
1564 div
= isl_vec_cow(div
);
1566 return isl_aff_free(aff
);
1568 ctx
= isl_aff_get_ctx(aff
);
1569 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1570 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1571 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1572 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1573 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1574 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1575 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1579 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1581 return isl_aff_free(aff
);
1583 size
= aff
->v
->size
;
1584 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1586 return isl_aff_free(aff
);
1587 isl_int_set_si(aff
->v
->el
[0], 1);
1588 isl_int_set_si(aff
->v
->el
[size
], 1);
1590 aff
= isl_aff_normalize(aff
);
1597 * aff mod m = aff - m * floor(aff/m)
1599 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1603 res
= isl_aff_copy(aff
);
1604 aff
= isl_aff_scale_down(aff
, m
);
1605 aff
= isl_aff_floor(aff
);
1606 aff
= isl_aff_scale(aff
, m
);
1607 res
= isl_aff_sub(res
, aff
);
1614 * aff mod m = aff - m * floor(aff/m)
1616 * with m an integer value.
1618 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1619 __isl_take isl_val
*m
)
1626 if (!isl_val_is_int(m
))
1627 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1628 "expecting integer modulo", goto error
);
1630 res
= isl_aff_copy(aff
);
1631 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1632 aff
= isl_aff_floor(aff
);
1633 aff
= isl_aff_scale_val(aff
, m
);
1634 res
= isl_aff_sub(res
, aff
);
1645 * pwaff mod m = pwaff - m * floor(pwaff/m)
1647 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1651 res
= isl_pw_aff_copy(pwaff
);
1652 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1653 pwaff
= isl_pw_aff_floor(pwaff
);
1654 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1655 res
= isl_pw_aff_sub(res
, pwaff
);
1662 * pa mod m = pa - m * floor(pa/m)
1664 * with m an integer value.
1666 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1667 __isl_take isl_val
*m
)
1671 if (!isl_val_is_int(m
))
1672 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1673 "expecting integer modulo", goto error
);
1674 pa
= isl_pw_aff_mod(pa
, m
->n
);
1678 isl_pw_aff_free(pa
);
1683 /* Given f, return ceil(f).
1684 * If f is an integer expression, then just return f.
1685 * Otherwise, let f be the expression
1691 * floor((e + m - 1)/m)
1693 * As a special case, ceil(NaN) = NaN.
1695 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1700 if (isl_aff_is_nan(aff
))
1702 if (isl_int_is_one(aff
->v
->el
[0]))
1705 aff
= isl_aff_cow(aff
);
1708 aff
->v
= isl_vec_cow(aff
->v
);
1710 return isl_aff_free(aff
);
1712 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1713 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1714 aff
= isl_aff_floor(aff
);
1719 /* Apply the expansion computed by isl_merge_divs.
1720 * The expansion itself is given by "exp" while the resulting
1721 * list of divs is given by "div".
1723 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1724 __isl_take isl_mat
*div
, int *exp
)
1730 aff
= isl_aff_cow(aff
);
1734 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1735 new_n_div
= isl_mat_rows(div
);
1736 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1738 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1739 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1740 if (!aff
->v
|| !aff
->ls
)
1741 return isl_aff_free(aff
);
1749 /* Add two affine expressions that live in the same local space.
1751 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1752 __isl_take isl_aff
*aff2
)
1756 aff1
= isl_aff_cow(aff1
);
1760 aff1
->v
= isl_vec_cow(aff1
->v
);
1766 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1767 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1768 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1769 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1770 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1771 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1772 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1784 /* Return the sum of "aff1" and "aff2".
1786 * If either of the two is NaN, then the result is NaN.
1788 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1789 __isl_take isl_aff
*aff2
)
1800 ctx
= isl_aff_get_ctx(aff1
);
1801 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1802 isl_die(ctx
, isl_error_invalid
,
1803 "spaces don't match", goto error
);
1805 if (isl_aff_is_nan(aff1
)) {
1809 if (isl_aff_is_nan(aff2
)) {
1814 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1815 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1816 if (n_div1
== 0 && n_div2
== 0)
1817 return add_expanded(aff1
, aff2
);
1819 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1820 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1821 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1824 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1825 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1826 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1830 return add_expanded(aff1
, aff2
);
1839 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1840 __isl_take isl_aff
*aff2
)
1842 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1845 /* Return the result of scaling "aff" by a factor of "f".
1847 * As a special case, f * NaN = NaN.
1849 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1855 if (isl_aff_is_nan(aff
))
1858 if (isl_int_is_one(f
))
1861 aff
= isl_aff_cow(aff
);
1864 aff
->v
= isl_vec_cow(aff
->v
);
1866 return isl_aff_free(aff
);
1868 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1869 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1874 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1875 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1876 isl_int_divexact(gcd
, f
, gcd
);
1877 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1883 /* Multiple "aff" by "v".
1885 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1886 __isl_take isl_val
*v
)
1891 if (isl_val_is_one(v
)) {
1896 if (!isl_val_is_rat(v
))
1897 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1898 "expecting rational factor", goto error
);
1900 aff
= isl_aff_scale(aff
, v
->n
);
1901 aff
= isl_aff_scale_down(aff
, v
->d
);
1911 /* Return the result of scaling "aff" down by a factor of "f".
1913 * As a special case, NaN/f = NaN.
1915 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1921 if (isl_aff_is_nan(aff
))
1924 if (isl_int_is_one(f
))
1927 aff
= isl_aff_cow(aff
);
1931 if (isl_int_is_zero(f
))
1932 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1933 "cannot scale down by zero", return isl_aff_free(aff
));
1935 aff
->v
= isl_vec_cow(aff
->v
);
1937 return isl_aff_free(aff
);
1940 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1941 isl_int_gcd(gcd
, gcd
, f
);
1942 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1943 isl_int_divexact(gcd
, f
, gcd
);
1944 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1950 /* Divide "aff" by "v".
1952 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1953 __isl_take isl_val
*v
)
1958 if (isl_val_is_one(v
)) {
1963 if (!isl_val_is_rat(v
))
1964 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1965 "expecting rational factor", goto error
);
1966 if (!isl_val_is_pos(v
))
1967 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1968 "factor needs to be positive", goto error
);
1970 aff
= isl_aff_scale(aff
, v
->d
);
1971 aff
= isl_aff_scale_down(aff
, v
->n
);
1981 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1989 isl_int_set_ui(v
, f
);
1990 aff
= isl_aff_scale_down(aff
, v
);
1996 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1997 enum isl_dim_type type
, unsigned pos
, const char *s
)
1999 aff
= isl_aff_cow(aff
);
2002 if (type
== isl_dim_out
)
2003 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2004 "cannot set name of output/set dimension",
2005 return isl_aff_free(aff
));
2006 if (type
== isl_dim_in
)
2008 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2010 return isl_aff_free(aff
);
2015 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2016 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2018 aff
= isl_aff_cow(aff
);
2021 if (type
== isl_dim_out
)
2022 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2023 "cannot set name of output/set dimension",
2025 if (type
== isl_dim_in
)
2027 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2029 return isl_aff_free(aff
);
2038 /* Replace the identifier of the input tuple of "aff" by "id".
2039 * type is currently required to be equal to isl_dim_in
2041 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2042 enum isl_dim_type type
, __isl_take isl_id
*id
)
2044 aff
= isl_aff_cow(aff
);
2047 if (type
!= isl_dim_out
)
2048 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2049 "cannot only set id of input tuple", goto error
);
2050 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2052 return isl_aff_free(aff
);
2061 /* Exploit the equalities in "eq" to simplify the affine expression
2062 * and the expressions of the integer divisions in the local space.
2063 * The integer divisions in this local space are assumed to appear
2064 * as regular dimensions in "eq".
2066 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2067 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2075 if (eq
->n_eq
== 0) {
2076 isl_basic_set_free(eq
);
2080 aff
= isl_aff_cow(aff
);
2084 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2085 isl_basic_set_copy(eq
));
2086 aff
->v
= isl_vec_cow(aff
->v
);
2087 if (!aff
->ls
|| !aff
->v
)
2090 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2092 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2093 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2094 if (j
< 0 || j
== 0 || j
>= total
)
2097 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2101 isl_basic_set_free(eq
);
2102 aff
= isl_aff_normalize(aff
);
2105 isl_basic_set_free(eq
);
2110 /* Exploit the equalities in "eq" to simplify the affine expression
2111 * and the expressions of the integer divisions in the local space.
2113 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2114 __isl_take isl_basic_set
*eq
)
2120 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2122 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2123 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2125 isl_basic_set_free(eq
);
2130 /* Look for equalities among the variables shared by context and aff
2131 * and the integer divisions of aff, if any.
2132 * The equalities are then used to eliminate coefficients and/or integer
2133 * divisions from aff.
2135 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2136 __isl_take isl_set
*context
)
2138 isl_basic_set
*hull
;
2143 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2145 isl_basic_set
*bset
;
2146 isl_local_space
*ls
;
2147 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2148 ls
= isl_aff_get_domain_local_space(aff
);
2149 bset
= isl_basic_set_from_local_space(ls
);
2150 bset
= isl_basic_set_lift(bset
);
2151 bset
= isl_basic_set_flatten(bset
);
2152 context
= isl_set_intersect(context
,
2153 isl_set_from_basic_set(bset
));
2156 hull
= isl_set_affine_hull(context
);
2157 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2160 isl_set_free(context
);
2164 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2165 __isl_take isl_set
*context
)
2167 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2168 dom_context
= isl_set_intersect_params(dom_context
, context
);
2169 return isl_aff_gist(aff
, dom_context
);
2172 /* Return a basic set containing those elements in the space
2173 * of aff where it is positive. "rational" should not be set.
2175 * If "aff" is NaN, then it is not positive.
2177 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2180 isl_constraint
*ineq
;
2181 isl_basic_set
*bset
;
2186 if (isl_aff_is_nan(aff
)) {
2187 isl_space
*space
= isl_aff_get_domain_space(aff
);
2189 return isl_basic_set_empty(space
);
2192 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2193 "rational sets not supported", goto error
);
2195 ineq
= isl_inequality_from_aff(aff
);
2196 c
= isl_constraint_get_constant_val(ineq
);
2197 c
= isl_val_sub_ui(c
, 1);
2198 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2200 bset
= isl_basic_set_from_constraint(ineq
);
2201 bset
= isl_basic_set_simplify(bset
);
2208 /* Return a basic set containing those elements in the space
2209 * of aff where it is non-negative.
2210 * If "rational" is set, then return a rational basic set.
2212 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2214 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2215 __isl_take isl_aff
*aff
, int rational
)
2217 isl_constraint
*ineq
;
2218 isl_basic_set
*bset
;
2222 if (isl_aff_is_nan(aff
)) {
2223 isl_space
*space
= isl_aff_get_domain_space(aff
);
2225 return isl_basic_set_empty(space
);
2228 ineq
= isl_inequality_from_aff(aff
);
2230 bset
= isl_basic_set_from_constraint(ineq
);
2232 bset
= isl_basic_set_set_rational(bset
);
2233 bset
= isl_basic_set_simplify(bset
);
2237 /* Return a basic set containing those elements in the space
2238 * of aff where it is non-negative.
2240 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2242 return aff_nonneg_basic_set(aff
, 0);
2245 /* Return a basic set containing those elements in the domain space
2246 * of aff where it is negative.
2248 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2250 aff
= isl_aff_neg(aff
);
2251 aff
= isl_aff_add_constant_num_si(aff
, -1);
2252 return isl_aff_nonneg_basic_set(aff
);
2255 /* Return a basic set containing those elements in the space
2256 * of aff where it is zero.
2257 * If "rational" is set, then return a rational basic set.
2259 * If "aff" is NaN, then it is not zero.
2261 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2264 isl_constraint
*ineq
;
2265 isl_basic_set
*bset
;
2269 if (isl_aff_is_nan(aff
)) {
2270 isl_space
*space
= isl_aff_get_domain_space(aff
);
2272 return isl_basic_set_empty(space
);
2275 ineq
= isl_equality_from_aff(aff
);
2277 bset
= isl_basic_set_from_constraint(ineq
);
2279 bset
= isl_basic_set_set_rational(bset
);
2280 bset
= isl_basic_set_simplify(bset
);
2284 /* Return a basic set containing those elements in the space
2285 * of aff where it is zero.
2287 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2289 return aff_zero_basic_set(aff
, 0);
2292 /* Return a basic set containing those elements in the shared space
2293 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2295 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2296 __isl_take isl_aff
*aff2
)
2298 aff1
= isl_aff_sub(aff1
, aff2
);
2300 return isl_aff_nonneg_basic_set(aff1
);
2303 /* Return a set containing those elements in the shared space
2304 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2306 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2307 __isl_take isl_aff
*aff2
)
2309 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2312 /* Return a basic set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2315 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2316 __isl_take isl_aff
*aff2
)
2318 return isl_aff_ge_basic_set(aff2
, aff1
);
2321 /* Return a set containing those elements in the shared space
2322 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2324 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2325 __isl_take isl_aff
*aff2
)
2327 return isl_aff_ge_set(aff2
, aff1
);
2330 /* Return a basic set containing those elements in the shared space
2331 * of aff1 and aff2 where aff1 and aff2 are equal.
2333 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2334 __isl_take isl_aff
*aff2
)
2336 aff1
= isl_aff_sub(aff1
, aff2
);
2338 return isl_aff_zero_basic_set(aff1
);
2341 /* Return a set containing those elements in the shared space
2342 * of aff1 and aff2 where aff1 and aff2 are equal.
2344 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2345 __isl_take isl_aff
*aff2
)
2347 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2350 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2351 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2353 aff1
= isl_aff_add(aff1
, aff2
);
2354 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2358 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2366 /* Check whether the given affine expression has non-zero coefficient
2367 * for any dimension in the given range or if any of these dimensions
2368 * appear with non-zero coefficients in any of the integer divisions
2369 * involved in the affine expression.
2371 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2372 enum isl_dim_type type
, unsigned first
, unsigned n
)
2377 isl_bool involves
= isl_bool_false
;
2380 return isl_bool_error
;
2382 return isl_bool_false
;
2384 ctx
= isl_aff_get_ctx(aff
);
2385 if (first
+ n
> isl_aff_dim(aff
, type
))
2386 isl_die(ctx
, isl_error_invalid
,
2387 "range out of bounds", return isl_bool_error
);
2389 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2393 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2394 for (i
= 0; i
< n
; ++i
)
2395 if (active
[first
+ i
]) {
2396 involves
= isl_bool_true
;
2405 return isl_bool_error
;
2408 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2409 enum isl_dim_type type
, unsigned first
, unsigned n
)
2415 if (type
== isl_dim_out
)
2416 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2417 "cannot drop output/set dimension",
2418 return isl_aff_free(aff
));
2419 if (type
== isl_dim_in
)
2421 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2424 ctx
= isl_aff_get_ctx(aff
);
2425 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2426 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2427 return isl_aff_free(aff
));
2429 aff
= isl_aff_cow(aff
);
2433 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2435 return isl_aff_free(aff
);
2437 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2438 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2440 return isl_aff_free(aff
);
2445 /* Project the domain of the affine expression onto its parameter space.
2446 * The affine expression may not involve any of the domain dimensions.
2448 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2454 n
= isl_aff_dim(aff
, isl_dim_in
);
2455 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2457 return isl_aff_free(aff
);
2459 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2460 "affine expression involves some of the domain dimensions",
2461 return isl_aff_free(aff
));
2462 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2463 space
= isl_aff_get_domain_space(aff
);
2464 space
= isl_space_params(space
);
2465 aff
= isl_aff_reset_domain_space(aff
, space
);
2469 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2470 enum isl_dim_type type
, unsigned first
, unsigned n
)
2476 if (type
== isl_dim_out
)
2477 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2478 "cannot insert output/set dimensions",
2479 return isl_aff_free(aff
));
2480 if (type
== isl_dim_in
)
2482 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2485 ctx
= isl_aff_get_ctx(aff
);
2486 if (first
> isl_local_space_dim(aff
->ls
, type
))
2487 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2488 return isl_aff_free(aff
));
2490 aff
= isl_aff_cow(aff
);
2494 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2496 return isl_aff_free(aff
);
2498 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2499 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2501 return isl_aff_free(aff
);
2506 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2507 enum isl_dim_type type
, unsigned n
)
2511 pos
= isl_aff_dim(aff
, type
);
2513 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2516 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2517 enum isl_dim_type type
, unsigned n
)
2521 pos
= isl_pw_aff_dim(pwaff
, type
);
2523 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2526 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2527 * to dimensions of "dst_type" at "dst_pos".
2529 * We only support moving input dimensions to parameters and vice versa.
2531 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2532 enum isl_dim_type dst_type
, unsigned dst_pos
,
2533 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2541 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2542 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2545 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2546 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2547 "cannot move output/set dimension",
2548 return isl_aff_free(aff
));
2549 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2550 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2551 "cannot move divs", return isl_aff_free(aff
));
2552 if (dst_type
== isl_dim_in
)
2553 dst_type
= isl_dim_set
;
2554 if (src_type
== isl_dim_in
)
2555 src_type
= isl_dim_set
;
2557 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2558 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2559 "range out of bounds", return isl_aff_free(aff
));
2560 if (dst_type
== src_type
)
2561 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2562 "moving dims within the same type not supported",
2563 return isl_aff_free(aff
));
2565 aff
= isl_aff_cow(aff
);
2569 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2570 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2571 if (dst_type
> src_type
)
2574 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2575 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2576 src_type
, src_pos
, n
);
2577 if (!aff
->v
|| !aff
->ls
)
2578 return isl_aff_free(aff
);
2580 aff
= sort_divs(aff
);
2585 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2587 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2588 return isl_pw_aff_alloc(dom
, aff
);
2592 #define PW isl_pw_aff
2596 #define EL_IS_ZERO is_empty
2600 #define IS_ZERO is_empty
2603 #undef DEFAULT_IS_ZERO
2604 #define DEFAULT_IS_ZERO 0
2611 #include <isl_pw_templ.c>
2612 #include <isl_pw_hash.c>
2613 #include <isl_pw_union_opt.c>
2616 #define UNION isl_union_pw_aff
2618 #define PART isl_pw_aff
2620 #define PARTS pw_aff
2622 #include <isl_union_single.c>
2623 #include <isl_union_neg.c>
2625 static __isl_give isl_set
*align_params_pw_pw_set_and(
2626 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2627 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2628 __isl_take isl_pw_aff
*pwaff2
))
2630 if (!pwaff1
|| !pwaff2
)
2632 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2633 pwaff2
->dim
, isl_dim_param
))
2634 return fn(pwaff1
, pwaff2
);
2635 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2636 !isl_space_has_named_params(pwaff2
->dim
))
2637 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2638 "unaligned unnamed parameters", goto error
);
2639 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2640 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2641 return fn(pwaff1
, pwaff2
);
2643 isl_pw_aff_free(pwaff1
);
2644 isl_pw_aff_free(pwaff2
);
2648 /* Align the parameters of the to isl_pw_aff arguments and
2649 * then apply a function "fn" on them that returns an isl_map.
2651 static __isl_give isl_map
*align_params_pw_pw_map_and(
2652 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2653 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2654 __isl_take isl_pw_aff
*pa2
))
2658 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2659 return fn(pa1
, pa2
);
2660 if (!isl_space_has_named_params(pa1
->dim
) ||
2661 !isl_space_has_named_params(pa2
->dim
))
2662 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2663 "unaligned unnamed parameters", goto error
);
2664 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2665 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2666 return fn(pa1
, pa2
);
2668 isl_pw_aff_free(pa1
);
2669 isl_pw_aff_free(pa2
);
2673 /* Compute a piecewise quasi-affine expression with a domain that
2674 * is the union of those of pwaff1 and pwaff2 and such that on each
2675 * cell, the quasi-affine expression is the maximum of those of pwaff1
2676 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2677 * cell, then the associated expression is the defined one.
2679 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2680 __isl_take isl_pw_aff
*pwaff2
)
2682 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2685 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2686 __isl_take isl_pw_aff
*pwaff2
)
2688 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2692 /* Compute a piecewise quasi-affine expression with a domain that
2693 * is the union of those of pwaff1 and pwaff2 and such that on each
2694 * cell, the quasi-affine expression is the minimum of those of pwaff1
2695 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2696 * cell, then the associated expression is the defined one.
2698 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2699 __isl_take isl_pw_aff
*pwaff2
)
2701 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2704 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2705 __isl_take isl_pw_aff
*pwaff2
)
2707 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2711 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2712 __isl_take isl_pw_aff
*pwaff2
, int max
)
2715 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2717 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2720 /* Construct a map with as domain the domain of pwaff and
2721 * one-dimensional range corresponding to the affine expressions.
2723 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2732 dim
= isl_pw_aff_get_space(pwaff
);
2733 map
= isl_map_empty(dim
);
2735 for (i
= 0; i
< pwaff
->n
; ++i
) {
2736 isl_basic_map
*bmap
;
2739 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2740 map_i
= isl_map_from_basic_map(bmap
);
2741 map_i
= isl_map_intersect_domain(map_i
,
2742 isl_set_copy(pwaff
->p
[i
].set
));
2743 map
= isl_map_union_disjoint(map
, map_i
);
2746 isl_pw_aff_free(pwaff
);
2751 /* Construct a map with as domain the domain of pwaff and
2752 * one-dimensional range corresponding to the affine expressions.
2754 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2758 if (isl_space_is_set(pwaff
->dim
))
2759 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2760 "space of input is not a map", goto error
);
2761 return map_from_pw_aff(pwaff
);
2763 isl_pw_aff_free(pwaff
);
2767 /* Construct a one-dimensional set with as parameter domain
2768 * the domain of pwaff and the single set dimension
2769 * corresponding to the affine expressions.
2771 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2775 if (!isl_space_is_set(pwaff
->dim
))
2776 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2777 "space of input is not a set", goto error
);
2778 return map_from_pw_aff(pwaff
);
2780 isl_pw_aff_free(pwaff
);
2784 /* Return a set containing those elements in the domain
2785 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2786 * does not satisfy "fn" (if complement is 1).
2788 * The pieces with a NaN never belong to the result since
2789 * NaN does not satisfy any property.
2791 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2792 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2801 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2803 for (i
= 0; i
< pwaff
->n
; ++i
) {
2804 isl_basic_set
*bset
;
2805 isl_set
*set_i
, *locus
;
2808 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2811 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2812 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2813 locus
= isl_set_from_basic_set(bset
);
2814 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2816 set_i
= isl_set_subtract(set_i
, locus
);
2818 set_i
= isl_set_intersect(set_i
, locus
);
2819 set
= isl_set_union_disjoint(set
, set_i
);
2822 isl_pw_aff_free(pwaff
);
2827 /* Return a set containing those elements in the domain
2828 * of "pa" where it is positive.
2830 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2832 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2835 /* Return a set containing those elements in the domain
2836 * of pwaff where it is non-negative.
2838 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2840 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2843 /* Return a set containing those elements in the domain
2844 * of pwaff where it is zero.
2846 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2848 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2851 /* Return a set containing those elements in the domain
2852 * of pwaff where it is not zero.
2854 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2856 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2859 /* Return a set containing those elements in the shared domain
2860 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2862 * We compute the difference on the shared domain and then construct
2863 * the set of values where this difference is non-negative.
2864 * If strict is set, we first subtract 1 from the difference.
2865 * If equal is set, we only return the elements where pwaff1 and pwaff2
2868 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2869 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2871 isl_set
*set1
, *set2
;
2873 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2874 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2875 set1
= isl_set_intersect(set1
, set2
);
2876 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2877 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2878 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2881 isl_space
*dim
= isl_set_get_space(set1
);
2883 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2884 aff
= isl_aff_add_constant_si(aff
, -1);
2885 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2890 return isl_pw_aff_zero_set(pwaff1
);
2891 return isl_pw_aff_nonneg_set(pwaff1
);
2894 /* Return a set containing those elements in the shared domain
2895 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2897 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2898 __isl_take isl_pw_aff
*pwaff2
)
2900 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2903 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2904 __isl_take isl_pw_aff
*pwaff2
)
2906 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2909 /* Return a set containing those elements in the shared domain
2910 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2912 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2913 __isl_take isl_pw_aff
*pwaff2
)
2915 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2918 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2919 __isl_take isl_pw_aff
*pwaff2
)
2921 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2924 /* Return a set containing those elements in the shared domain
2925 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2927 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2928 __isl_take isl_pw_aff
*pwaff2
)
2930 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2933 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2934 __isl_take isl_pw_aff
*pwaff2
)
2936 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2939 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2940 __isl_take isl_pw_aff
*pwaff2
)
2942 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2945 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2946 __isl_take isl_pw_aff
*pwaff2
)
2948 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2951 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2952 * where the function values are ordered in the same way as "order",
2953 * which returns a set in the shared domain of its two arguments.
2954 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2956 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2957 * We first pull back the two functions such that they are defined on
2958 * the domain [A -> B]. Then we apply "order", resulting in a set
2959 * in the space [A -> B]. Finally, we unwrap this set to obtain
2960 * a map in the space A -> B.
2962 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2963 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2964 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2965 __isl_take isl_pw_aff
*pa2
))
2967 isl_space
*space1
, *space2
;
2971 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2972 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2973 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2974 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2975 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2976 ma
= isl_multi_aff_range_map(space1
);
2977 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2978 set
= order(pa1
, pa2
);
2980 return isl_set_unwrap(set
);
2983 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2984 * where the function values are equal.
2985 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2987 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2988 __isl_take isl_pw_aff
*pa2
)
2990 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2993 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2994 * where the function values are equal.
2996 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2997 __isl_take isl_pw_aff
*pa2
)
2999 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3002 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3003 * where the function value of "pa1" is less than the function value of "pa2".
3004 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3006 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3007 __isl_take isl_pw_aff
*pa2
)
3009 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3012 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3013 * where the function value of "pa1" is less than the function value of "pa2".
3015 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3016 __isl_take isl_pw_aff
*pa2
)
3018 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3021 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3022 * where the function value of "pa1" is greater than the function value
3024 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3026 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3027 __isl_take isl_pw_aff
*pa2
)
3029 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3032 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3033 * where the function value of "pa1" is greater than the function value
3036 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3037 __isl_take isl_pw_aff
*pa2
)
3039 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3042 /* Return a set containing those elements in the shared domain
3043 * of the elements of list1 and list2 where each element in list1
3044 * has the relation specified by "fn" with each element in list2.
3046 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3047 __isl_take isl_pw_aff_list
*list2
,
3048 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3049 __isl_take isl_pw_aff
*pwaff2
))
3055 if (!list1
|| !list2
)
3058 ctx
= isl_pw_aff_list_get_ctx(list1
);
3059 if (list1
->n
< 1 || list2
->n
< 1)
3060 isl_die(ctx
, isl_error_invalid
,
3061 "list should contain at least one element", goto error
);
3063 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3064 for (i
= 0; i
< list1
->n
; ++i
)
3065 for (j
= 0; j
< list2
->n
; ++j
) {
3068 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3069 isl_pw_aff_copy(list2
->p
[j
]));
3070 set
= isl_set_intersect(set
, set_ij
);
3073 isl_pw_aff_list_free(list1
);
3074 isl_pw_aff_list_free(list2
);
3077 isl_pw_aff_list_free(list1
);
3078 isl_pw_aff_list_free(list2
);
3082 /* Return a set containing those elements in the shared domain
3083 * of the elements of list1 and list2 where each element in list1
3084 * is equal to each element in list2.
3086 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3087 __isl_take isl_pw_aff_list
*list2
)
3089 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3092 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3093 __isl_take isl_pw_aff_list
*list2
)
3095 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3098 /* Return a set containing those elements in the shared domain
3099 * of the elements of list1 and list2 where each element in list1
3100 * is less than or equal to each element in list2.
3102 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3103 __isl_take isl_pw_aff_list
*list2
)
3105 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3108 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3109 __isl_take isl_pw_aff_list
*list2
)
3111 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3114 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3115 __isl_take isl_pw_aff_list
*list2
)
3117 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3120 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3121 __isl_take isl_pw_aff_list
*list2
)
3123 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3127 /* Return a set containing those elements in the shared domain
3128 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3130 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3131 __isl_take isl_pw_aff
*pwaff2
)
3133 isl_set
*set_lt
, *set_gt
;
3135 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3136 isl_pw_aff_copy(pwaff2
));
3137 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3138 return isl_set_union_disjoint(set_lt
, set_gt
);
3141 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3142 __isl_take isl_pw_aff
*pwaff2
)
3144 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3147 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3152 if (isl_int_is_one(v
))
3154 if (!isl_int_is_pos(v
))
3155 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3156 "factor needs to be positive",
3157 return isl_pw_aff_free(pwaff
));
3158 pwaff
= isl_pw_aff_cow(pwaff
);
3164 for (i
= 0; i
< pwaff
->n
; ++i
) {
3165 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3166 if (!pwaff
->p
[i
].aff
)
3167 return isl_pw_aff_free(pwaff
);
3173 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3177 pwaff
= isl_pw_aff_cow(pwaff
);
3183 for (i
= 0; i
< pwaff
->n
; ++i
) {
3184 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3185 if (!pwaff
->p
[i
].aff
)
3186 return isl_pw_aff_free(pwaff
);
3192 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3196 pwaff
= isl_pw_aff_cow(pwaff
);
3202 for (i
= 0; i
< pwaff
->n
; ++i
) {
3203 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3204 if (!pwaff
->p
[i
].aff
)
3205 return isl_pw_aff_free(pwaff
);
3211 /* Assuming that "cond1" and "cond2" are disjoint,
3212 * return an affine expression that is equal to pwaff1 on cond1
3213 * and to pwaff2 on cond2.
3215 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3216 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3217 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3219 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3220 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3222 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3225 /* Return an affine expression that is equal to pwaff_true for elements
3226 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3228 * That is, return cond ? pwaff_true : pwaff_false;
3230 * If "cond" involves and NaN, then we conservatively return a NaN
3231 * on its entire domain. In principle, we could consider the pieces
3232 * where it is NaN separately from those where it is not.
3234 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3235 * then only use the domain of "cond" to restrict the domain.
3237 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3238 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3240 isl_set
*cond_true
, *cond_false
;
3245 if (isl_pw_aff_involves_nan(cond
)) {
3246 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3247 isl_local_space
*ls
= isl_local_space_from_space(space
);
3248 isl_pw_aff_free(cond
);
3249 isl_pw_aff_free(pwaff_true
);
3250 isl_pw_aff_free(pwaff_false
);
3251 return isl_pw_aff_nan_on_domain(ls
);
3254 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3255 isl_pw_aff_get_space(pwaff_false
));
3256 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3257 isl_pw_aff_get_space(pwaff_true
));
3258 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3264 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3265 isl_pw_aff_free(pwaff_false
);
3266 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3269 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3270 cond_false
= isl_pw_aff_zero_set(cond
);
3271 return isl_pw_aff_select(cond_true
, pwaff_true
,
3272 cond_false
, pwaff_false
);
3274 isl_pw_aff_free(cond
);
3275 isl_pw_aff_free(pwaff_true
);
3276 isl_pw_aff_free(pwaff_false
);
3280 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3283 return isl_bool_error
;
3285 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3288 /* Check whether pwaff is a piecewise constant.
3290 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3295 return isl_bool_error
;
3297 for (i
= 0; i
< pwaff
->n
; ++i
) {
3298 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3299 if (is_cst
< 0 || !is_cst
)
3303 return isl_bool_true
;
3306 /* Are all elements of "mpa" piecewise constants?
3308 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3313 return isl_bool_error
;
3315 for (i
= 0; i
< mpa
->n
; ++i
) {
3316 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3317 if (is_cst
< 0 || !is_cst
)
3321 return isl_bool_true
;
3324 /* Return the product of "aff1" and "aff2".
3326 * If either of the two is NaN, then the result is NaN.
3328 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3330 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3331 __isl_take isl_aff
*aff2
)
3336 if (isl_aff_is_nan(aff1
)) {
3340 if (isl_aff_is_nan(aff2
)) {
3345 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3346 return isl_aff_mul(aff2
, aff1
);
3348 if (!isl_aff_is_cst(aff2
))
3349 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3350 "at least one affine expression should be constant",
3353 aff1
= isl_aff_cow(aff1
);
3357 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3358 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3368 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3370 * If either of the two is NaN, then the result is NaN.
3372 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3373 __isl_take isl_aff
*aff2
)
3381 if (isl_aff_is_nan(aff1
)) {
3385 if (isl_aff_is_nan(aff2
)) {
3390 is_cst
= isl_aff_is_cst(aff2
);
3394 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3395 "second argument should be a constant", goto error
);
3400 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3402 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3403 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3406 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3407 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3410 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3411 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3422 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3423 __isl_take isl_pw_aff
*pwaff2
)
3425 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3428 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3429 __isl_take isl_pw_aff
*pwaff2
)
3431 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3434 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3435 __isl_take isl_pw_aff
*pwaff2
)
3437 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3440 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3441 __isl_take isl_pw_aff
*pwaff2
)
3443 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3446 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3447 __isl_take isl_pw_aff
*pwaff2
)
3449 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3452 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3453 __isl_take isl_pw_aff
*pa2
)
3455 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3458 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3460 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3461 __isl_take isl_pw_aff
*pa2
)
3465 is_cst
= isl_pw_aff_is_cst(pa2
);
3469 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3470 "second argument should be a piecewise constant",
3472 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3474 isl_pw_aff_free(pa1
);
3475 isl_pw_aff_free(pa2
);
3479 /* Compute the quotient of the integer division of "pa1" by "pa2"
3480 * with rounding towards zero.
3481 * "pa2" is assumed to be a piecewise constant.
3483 * In particular, return
3485 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3488 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3489 __isl_take isl_pw_aff
*pa2
)
3495 is_cst
= isl_pw_aff_is_cst(pa2
);
3499 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3500 "second argument should be a piecewise constant",
3503 pa1
= isl_pw_aff_div(pa1
, pa2
);
3505 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3506 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3507 c
= isl_pw_aff_ceil(pa1
);
3508 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3510 isl_pw_aff_free(pa1
);
3511 isl_pw_aff_free(pa2
);
3515 /* Compute the remainder of the integer division of "pa1" by "pa2"
3516 * with rounding towards zero.
3517 * "pa2" is assumed to be a piecewise constant.
3519 * In particular, return
3521 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3524 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3525 __isl_take isl_pw_aff
*pa2
)
3530 is_cst
= isl_pw_aff_is_cst(pa2
);
3534 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3535 "second argument should be a piecewise constant",
3537 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3538 res
= isl_pw_aff_mul(pa2
, res
);
3539 res
= isl_pw_aff_sub(pa1
, res
);
3542 isl_pw_aff_free(pa1
);
3543 isl_pw_aff_free(pa2
);
3547 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3548 __isl_take isl_pw_aff
*pwaff2
)
3553 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3554 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3555 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3556 isl_pw_aff_copy(pwaff2
));
3557 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3558 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3561 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3562 __isl_take isl_pw_aff
*pwaff2
)
3564 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3567 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3568 __isl_take isl_pw_aff
*pwaff2
)
3573 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3574 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3575 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3576 isl_pw_aff_copy(pwaff2
));
3577 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3578 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3581 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3582 __isl_take isl_pw_aff
*pwaff2
)
3584 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3587 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3588 __isl_take isl_pw_aff_list
*list
,
3589 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3590 __isl_take isl_pw_aff
*pwaff2
))
3599 ctx
= isl_pw_aff_list_get_ctx(list
);
3601 isl_die(ctx
, isl_error_invalid
,
3602 "list should contain at least one element", goto error
);
3604 res
= isl_pw_aff_copy(list
->p
[0]);
3605 for (i
= 1; i
< list
->n
; ++i
)
3606 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3608 isl_pw_aff_list_free(list
);
3611 isl_pw_aff_list_free(list
);
3615 /* Return an isl_pw_aff that maps each element in the intersection of the
3616 * domains of the elements of list to the minimal corresponding affine
3619 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3621 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3624 /* Return an isl_pw_aff that maps each element in the intersection of the
3625 * domains of the elements of list to the maximal corresponding affine
3628 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3630 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3633 /* Mark the domains of "pwaff" as rational.
3635 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3639 pwaff
= isl_pw_aff_cow(pwaff
);
3645 for (i
= 0; i
< pwaff
->n
; ++i
) {
3646 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3647 if (!pwaff
->p
[i
].set
)
3648 return isl_pw_aff_free(pwaff
);
3654 /* Mark the domains of the elements of "list" as rational.
3656 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3657 __isl_take isl_pw_aff_list
*list
)
3667 for (i
= 0; i
< n
; ++i
) {
3670 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3671 pa
= isl_pw_aff_set_rational(pa
);
3672 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3678 /* Do the parameters of "aff" match those of "space"?
3680 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3681 __isl_keep isl_space
*space
)
3683 isl_space
*aff_space
;
3689 aff_space
= isl_aff_get_domain_space(aff
);
3691 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3693 isl_space_free(aff_space
);
3697 /* Check that the domain space of "aff" matches "space".
3699 * Return 0 on success and -1 on error.
3701 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3702 __isl_keep isl_space
*space
)
3704 isl_space
*aff_space
;
3710 aff_space
= isl_aff_get_domain_space(aff
);
3712 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3716 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3717 "parameters don't match", goto error
);
3718 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3719 aff_space
, isl_dim_set
);
3723 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3724 "domains don't match", goto error
);
3725 isl_space_free(aff_space
);
3728 isl_space_free(aff_space
);
3738 #include <isl_multi_templ.c>
3739 #include <isl_multi_apply_set.c>
3740 #include <isl_multi_cmp.c>
3741 #include <isl_multi_floor.c>
3742 #include <isl_multi_gist.c>
3746 /* Remove any internal structure of the domain of "ma".
3747 * If there is any such internal structure in the input,
3748 * then the name of the corresponding space is also removed.
3750 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3751 __isl_take isl_multi_aff
*ma
)
3758 if (!ma
->space
->nested
[0])
3761 space
= isl_multi_aff_get_space(ma
);
3762 space
= isl_space_flatten_domain(space
);
3763 ma
= isl_multi_aff_reset_space(ma
, space
);
3768 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3769 * of the space to its domain.
3771 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3774 isl_local_space
*ls
;
3779 if (!isl_space_is_map(space
))
3780 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3781 "not a map space", goto error
);
3783 n_in
= isl_space_dim(space
, isl_dim_in
);
3784 space
= isl_space_domain_map(space
);
3786 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3788 isl_space_free(space
);
3792 space
= isl_space_domain(space
);
3793 ls
= isl_local_space_from_space(space
);
3794 for (i
= 0; i
< n_in
; ++i
) {
3797 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3799 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3801 isl_local_space_free(ls
);
3804 isl_space_free(space
);
3808 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3809 * of the space to its range.
3811 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3814 isl_local_space
*ls
;
3819 if (!isl_space_is_map(space
))
3820 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3821 "not a map space", goto error
);
3823 n_in
= isl_space_dim(space
, isl_dim_in
);
3824 n_out
= isl_space_dim(space
, isl_dim_out
);
3825 space
= isl_space_range_map(space
);
3827 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3829 isl_space_free(space
);
3833 space
= isl_space_domain(space
);
3834 ls
= isl_local_space_from_space(space
);
3835 for (i
= 0; i
< n_out
; ++i
) {
3838 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3839 isl_dim_set
, n_in
+ i
);
3840 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3842 isl_local_space_free(ls
);
3845 isl_space_free(space
);
3849 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3850 * of the space to its range.
3852 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3853 __isl_take isl_space
*space
)
3855 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3858 /* Given the space of a set and a range of set dimensions,
3859 * construct an isl_multi_aff that projects out those dimensions.
3861 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3862 __isl_take isl_space
*space
, enum isl_dim_type type
,
3863 unsigned first
, unsigned n
)
3866 isl_local_space
*ls
;
3871 if (!isl_space_is_set(space
))
3872 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3873 "expecting set space", goto error
);
3874 if (type
!= isl_dim_set
)
3875 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3876 "only set dimensions can be projected out", goto error
);
3878 dim
= isl_space_dim(space
, isl_dim_set
);
3879 if (first
+ n
> dim
)
3880 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3881 "range out of bounds", goto error
);
3883 space
= isl_space_from_domain(space
);
3884 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3887 return isl_multi_aff_alloc(space
);
3889 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3890 space
= isl_space_domain(space
);
3891 ls
= isl_local_space_from_space(space
);
3893 for (i
= 0; i
< first
; ++i
) {
3896 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3898 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3901 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3904 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3905 isl_dim_set
, first
+ n
+ i
);
3906 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3909 isl_local_space_free(ls
);
3912 isl_space_free(space
);
3916 /* Given the space of a set and a range of set dimensions,
3917 * construct an isl_pw_multi_aff that projects out those dimensions.
3919 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3920 __isl_take isl_space
*space
, enum isl_dim_type type
,
3921 unsigned first
, unsigned n
)
3925 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3926 return isl_pw_multi_aff_from_multi_aff(ma
);
3929 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3932 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3933 __isl_take isl_multi_aff
*ma
)
3935 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3936 return isl_pw_multi_aff_alloc(dom
, ma
);
3939 /* Create a piecewise multi-affine expression in the given space that maps each
3940 * input dimension to the corresponding output dimension.
3942 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3943 __isl_take isl_space
*space
)
3945 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3948 /* Exploit the equalities in "eq" to simplify the affine expressions.
3950 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3951 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3955 maff
= isl_multi_aff_cow(maff
);
3959 for (i
= 0; i
< maff
->n
; ++i
) {
3960 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3961 isl_basic_set_copy(eq
));
3966 isl_basic_set_free(eq
);
3969 isl_basic_set_free(eq
);
3970 isl_multi_aff_free(maff
);
3974 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3979 maff
= isl_multi_aff_cow(maff
);
3983 for (i
= 0; i
< maff
->n
; ++i
) {
3984 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3986 return isl_multi_aff_free(maff
);
3992 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3993 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3995 maff1
= isl_multi_aff_add(maff1
, maff2
);
3996 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4000 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4008 /* Return the set of domain elements where "ma1" is lexicographically
4009 * smaller than or equal to "ma2".
4011 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4012 __isl_take isl_multi_aff
*ma2
)
4014 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4017 /* Return the set of domain elements where "ma1" is lexicographically
4018 * smaller than "ma2".
4020 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4021 __isl_take isl_multi_aff
*ma2
)
4023 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4026 /* Return the set of domain elements where "ma1" and "ma2"
4029 static __isl_give isl_set
*isl_multi_aff_order_set(
4030 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4031 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4034 isl_map
*map1
, *map2
;
4037 map1
= isl_map_from_multi_aff(ma1
);
4038 map2
= isl_map_from_multi_aff(ma2
);
4039 map
= isl_map_range_product(map1
, map2
);
4040 space
= isl_space_range(isl_map_get_space(map
));
4041 space
= isl_space_domain(isl_space_unwrap(space
));
4043 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4045 return isl_map_domain(map
);
4048 /* Return the set of domain elements where "ma1" is lexicographically
4049 * greater than or equal to "ma2".
4051 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4052 __isl_take isl_multi_aff
*ma2
)
4054 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4057 /* Return the set of domain elements where "ma1" is lexicographically
4058 * greater than "ma2".
4060 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4061 __isl_take isl_multi_aff
*ma2
)
4063 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4067 #define PW isl_pw_multi_aff
4069 #define EL isl_multi_aff
4071 #define EL_IS_ZERO is_empty
4075 #define IS_ZERO is_empty
4078 #undef DEFAULT_IS_ZERO
4079 #define DEFAULT_IS_ZERO 0
4084 #define NO_INVOLVES_DIMS
4085 #define NO_INSERT_DIMS
4089 #include <isl_pw_templ.c>
4090 #include <isl_pw_union_opt.c>
4095 #define UNION isl_union_pw_multi_aff
4097 #define PART isl_pw_multi_aff
4099 #define PARTS pw_multi_aff
4101 #include <isl_union_multi.c>
4102 #include <isl_union_neg.c>
4104 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4105 __isl_take isl_pw_multi_aff
*pma1
,
4106 __isl_take isl_pw_multi_aff
*pma2
)
4108 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4109 &isl_multi_aff_lex_ge_set
);
4112 /* Given two piecewise multi affine expressions, return a piecewise
4113 * multi-affine expression defined on the union of the definition domains
4114 * of the inputs that is equal to the lexicographic maximum of the two
4115 * inputs on each cell. If only one of the two inputs is defined on
4116 * a given cell, then it is considered to be the maximum.
4118 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4119 __isl_take isl_pw_multi_aff
*pma1
,
4120 __isl_take isl_pw_multi_aff
*pma2
)
4122 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4123 &pw_multi_aff_union_lexmax
);
4126 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4127 __isl_take isl_pw_multi_aff
*pma1
,
4128 __isl_take isl_pw_multi_aff
*pma2
)
4130 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4131 &isl_multi_aff_lex_le_set
);
4134 /* Given two piecewise multi affine expressions, return a piecewise
4135 * multi-affine expression defined on the union of the definition domains
4136 * of the inputs that is equal to the lexicographic minimum of the two
4137 * inputs on each cell. If only one of the two inputs is defined on
4138 * a given cell, then it is considered to be the minimum.
4140 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4141 __isl_take isl_pw_multi_aff
*pma1
,
4142 __isl_take isl_pw_multi_aff
*pma2
)
4144 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4145 &pw_multi_aff_union_lexmin
);
4148 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4149 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4151 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4152 &isl_multi_aff_add
);
4155 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4156 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4158 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4162 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4163 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4165 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4166 &isl_multi_aff_sub
);
4169 /* Subtract "pma2" from "pma1" and return the result.
4171 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4172 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4174 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4178 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4179 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4181 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4184 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4185 * with the actual sum on the shared domain and
4186 * the defined expression on the symmetric difference of the domains.
4188 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4189 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4191 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4194 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4195 * with the actual sum on the shared domain and
4196 * the defined expression on the symmetric difference of the domains.
4198 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4199 __isl_take isl_union_pw_multi_aff
*upma1
,
4200 __isl_take isl_union_pw_multi_aff
*upma2
)
4202 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4205 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4206 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4208 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4209 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4213 isl_pw_multi_aff
*res
;
4218 n
= pma1
->n
* pma2
->n
;
4219 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4220 isl_space_copy(pma2
->dim
));
4221 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4223 for (i
= 0; i
< pma1
->n
; ++i
) {
4224 for (j
= 0; j
< pma2
->n
; ++j
) {
4228 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4229 isl_set_copy(pma2
->p
[j
].set
));
4230 ma
= isl_multi_aff_product(
4231 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4232 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4233 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4237 isl_pw_multi_aff_free(pma1
);
4238 isl_pw_multi_aff_free(pma2
);
4241 isl_pw_multi_aff_free(pma1
);
4242 isl_pw_multi_aff_free(pma2
);
4246 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4247 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4249 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4250 &pw_multi_aff_product
);
4253 /* Construct a map mapping the domain of the piecewise multi-affine expression
4254 * to its range, with each dimension in the range equated to the
4255 * corresponding affine expression on its cell.
4257 * If the domain of "pma" is rational, then so is the constructed "map".
4259 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4267 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4269 for (i
= 0; i
< pma
->n
; ++i
) {
4271 isl_multi_aff
*maff
;
4272 isl_basic_map
*bmap
;
4275 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4277 map
= isl_map_free(map
);
4278 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4279 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4280 map_i
= isl_map_from_basic_map(bmap
);
4281 map_i
= isl_map_intersect_domain(map_i
,
4282 isl_set_copy(pma
->p
[i
].set
));
4283 map
= isl_map_union_disjoint(map
, map_i
);
4286 isl_pw_multi_aff_free(pma
);
4290 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4295 if (!isl_space_is_set(pma
->dim
))
4296 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4297 "isl_pw_multi_aff cannot be converted into an isl_set",
4300 return isl_map_from_pw_multi_aff(pma
);
4302 isl_pw_multi_aff_free(pma
);
4306 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4307 * denominator "denom".
4308 * "denom" is allowed to be negative, in which case the actual denominator
4309 * is -denom and the expressions are added instead.
4311 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4312 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4318 first
= isl_seq_first_non_zero(c
, n
);
4322 sign
= isl_int_sgn(denom
);
4324 isl_int_abs(d
, denom
);
4325 for (i
= first
; i
< n
; ++i
) {
4328 if (isl_int_is_zero(c
[i
]))
4330 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4331 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4332 aff_i
= isl_aff_scale_down(aff_i
, d
);
4334 aff
= isl_aff_sub(aff
, aff_i
);
4336 aff
= isl_aff_add(aff
, aff_i
);
4343 /* Extract an affine expression that expresses the output dimension "pos"
4344 * of "bmap" in terms of the parameters and input dimensions from
4346 * Note that this expression may involve integer divisions defined
4347 * in terms of parameters and input dimensions.
4348 * The equality may also involve references to earlier (but not later)
4349 * output dimensions. These are replaced by the corresponding elements
4352 * If the equality is of the form
4354 * f(i) + h(j) + a x + g(i) = 0,
4356 * with f(i) a linear combinations of the parameters and input dimensions,
4357 * g(i) a linear combination of integer divisions defined in terms of the same
4358 * and h(j) a linear combinations of earlier output dimensions,
4359 * then the affine expression is
4361 * (-f(i) - g(i))/a - h(j)/a
4363 * If the equality is of the form
4365 * f(i) + h(j) - a x + g(i) = 0,
4367 * then the affine expression is
4369 * (f(i) + g(i))/a - h(j)/(-a)
4372 * If "div" refers to an integer division (i.e., it is smaller than
4373 * the number of integer divisions), then the equality constraint
4374 * does involve an integer division (the one at position "div") that
4375 * is defined in terms of output dimensions. However, this integer
4376 * division can be eliminated by exploiting a pair of constraints
4377 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4378 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4380 * In particular, let
4382 * x = e(i) + m floor(...)
4384 * with e(i) the expression derived above and floor(...) the integer
4385 * division involving output dimensions.
4396 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4397 * = (e(i) - l) mod m
4401 * x - l = (e(i) - l) mod m
4405 * x = ((e(i) - l) mod m) + l
4407 * The variable "shift" below contains the expression -l, which may
4408 * also involve a linear combination of earlier output dimensions.
4410 static __isl_give isl_aff
*extract_aff_from_equality(
4411 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4412 __isl_keep isl_multi_aff
*ma
)
4415 unsigned n_div
, n_out
;
4417 isl_local_space
*ls
;
4418 isl_aff
*aff
, *shift
;
4421 ctx
= isl_basic_map_get_ctx(bmap
);
4422 ls
= isl_basic_map_get_local_space(bmap
);
4423 ls
= isl_local_space_domain(ls
);
4424 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4427 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4428 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4429 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4430 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4431 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4432 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4433 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4435 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4436 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4437 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4440 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4441 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4442 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4443 bmap
->eq
[eq
][o_out
+ pos
]);
4445 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4448 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4449 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4450 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4451 isl_int_set_si(shift
->v
->el
[0], 1);
4452 shift
= subtract_initial(shift
, ma
, pos
,
4453 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4454 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4455 mod
= isl_val_int_from_isl_int(ctx
,
4456 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4457 mod
= isl_val_abs(mod
);
4458 aff
= isl_aff_mod_val(aff
, mod
);
4459 aff
= isl_aff_sub(aff
, shift
);
4462 isl_local_space_free(ls
);
4465 isl_local_space_free(ls
);
4470 /* Given a basic map with output dimensions defined
4471 * in terms of the parameters input dimensions and earlier
4472 * output dimensions using an equality (and possibly a pair on inequalities),
4473 * extract an isl_aff that expresses output dimension "pos" in terms
4474 * of the parameters and input dimensions.
4475 * Note that this expression may involve integer divisions defined
4476 * in terms of parameters and input dimensions.
4477 * "ma" contains the expressions corresponding to earlier output dimensions.
4479 * This function shares some similarities with
4480 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4482 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4483 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4490 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4491 if (eq
>= bmap
->n_eq
)
4492 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4493 "unable to find suitable equality", return NULL
);
4494 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4496 aff
= isl_aff_remove_unused_divs(aff
);
4500 /* Given a basic map where each output dimension is defined
4501 * in terms of the parameters and input dimensions using an equality,
4502 * extract an isl_multi_aff that expresses the output dimensions in terms
4503 * of the parameters and input dimensions.
4505 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4506 __isl_take isl_basic_map
*bmap
)
4515 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4516 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4518 for (i
= 0; i
< n_out
; ++i
) {
4521 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4522 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4525 isl_basic_map_free(bmap
);
4530 /* Given a basic set where each set dimension is defined
4531 * in terms of the parameters using an equality,
4532 * extract an isl_multi_aff that expresses the set dimensions in terms
4533 * of the parameters.
4535 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4536 __isl_take isl_basic_set
*bset
)
4538 return extract_isl_multi_aff_from_basic_map(bset
);
4541 /* Create an isl_pw_multi_aff that is equivalent to
4542 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4543 * The given basic map is such that each output dimension is defined
4544 * in terms of the parameters and input dimensions using an equality.
4546 * Since some applications expect the result of isl_pw_multi_aff_from_map
4547 * to only contain integer affine expressions, we compute the floor
4548 * of the expression before returning.
4550 * Remove all constraints involving local variables without
4551 * an explicit representation (resulting in the removal of those
4552 * local variables) prior to the actual extraction to ensure
4553 * that the local spaces in which the resulting affine expressions
4554 * are created do not contain any unknown local variables.
4555 * Removing such constraints is safe because constraints involving
4556 * unknown local variables are not used to determine whether
4557 * a basic map is obviously single-valued.
4559 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4560 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4564 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4565 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4566 ma
= isl_multi_aff_floor(ma
);
4567 return isl_pw_multi_aff_alloc(domain
, ma
);
4570 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4571 * This obviously only works if the input "map" is single-valued.
4572 * If so, we compute the lexicographic minimum of the image in the form
4573 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4574 * to its lexicographic minimum.
4575 * If the input is not single-valued, we produce an error.
4577 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4578 __isl_take isl_map
*map
)
4582 isl_pw_multi_aff
*pma
;
4584 sv
= isl_map_is_single_valued(map
);
4588 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4589 "map is not single-valued", goto error
);
4590 map
= isl_map_make_disjoint(map
);
4594 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4596 for (i
= 0; i
< map
->n
; ++i
) {
4597 isl_pw_multi_aff
*pma_i
;
4598 isl_basic_map
*bmap
;
4599 bmap
= isl_basic_map_copy(map
->p
[i
]);
4600 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4601 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4611 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4612 * taking into account that the output dimension at position "d"
4613 * can be represented as
4615 * x = floor((e(...) + c1) / m)
4617 * given that constraint "i" is of the form
4619 * e(...) + c1 - m x >= 0
4622 * Let "map" be of the form
4626 * We construct a mapping
4628 * A -> [A -> x = floor(...)]
4630 * apply that to the map, obtaining
4632 * [A -> x = floor(...)] -> B
4634 * and equate dimension "d" to x.
4635 * We then compute a isl_pw_multi_aff representation of the resulting map
4636 * and plug in the mapping above.
4638 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4639 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4643 isl_local_space
*ls
;
4651 isl_pw_multi_aff
*pma
;
4654 is_set
= isl_map_is_set(map
);
4656 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4657 ctx
= isl_map_get_ctx(map
);
4658 space
= isl_space_domain(isl_map_get_space(map
));
4659 n_in
= isl_space_dim(space
, isl_dim_set
);
4660 n
= isl_space_dim(space
, isl_dim_all
);
4662 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4664 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4665 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4667 isl_basic_map_free(hull
);
4669 ls
= isl_local_space_from_space(isl_space_copy(space
));
4670 aff
= isl_aff_alloc_vec(ls
, v
);
4671 aff
= isl_aff_floor(aff
);
4673 isl_space_free(space
);
4674 ma
= isl_multi_aff_from_aff(aff
);
4676 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4677 ma
= isl_multi_aff_range_product(ma
,
4678 isl_multi_aff_from_aff(aff
));
4681 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4682 map
= isl_map_apply_domain(map
, insert
);
4683 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4684 pma
= isl_pw_multi_aff_from_map(map
);
4685 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4690 /* Is constraint "c" of the form
4692 * e(...) + c1 - m x >= 0
4696 * -e(...) + c2 + m x >= 0
4698 * where m > 1 and e only depends on parameters and input dimemnsions?
4700 * "offset" is the offset of the output dimensions
4701 * "pos" is the position of output dimension x.
4703 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4705 if (isl_int_is_zero(c
[offset
+ d
]))
4707 if (isl_int_is_one(c
[offset
+ d
]))
4709 if (isl_int_is_negone(c
[offset
+ d
]))
4711 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4713 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4714 total
- (offset
+ d
+ 1)) != -1)
4719 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4721 * As a special case, we first check if there is any pair of constraints,
4722 * shared by all the basic maps in "map" that force a given dimension
4723 * to be equal to the floor of some affine combination of the input dimensions.
4725 * In particular, if we can find two constraints
4727 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4731 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4733 * where m > 1 and e only depends on parameters and input dimemnsions,
4736 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4738 * then we know that we can take
4740 * x = floor((e(...) + c1) / m)
4742 * without having to perform any computation.
4744 * Note that we know that
4748 * If c1 + c2 were 0, then we would have detected an equality during
4749 * simplification. If c1 + c2 were negative, then we would have detected
4752 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4753 __isl_take isl_map
*map
)
4759 isl_basic_map
*hull
;
4761 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4766 dim
= isl_map_dim(map
, isl_dim_out
);
4767 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4768 total
= 1 + isl_basic_map_total_dim(hull
);
4770 for (d
= 0; d
< dim
; ++d
) {
4771 for (i
= 0; i
< n
; ++i
) {
4772 if (!is_potential_div_constraint(hull
->ineq
[i
],
4775 for (j
= i
+ 1; j
< n
; ++j
) {
4776 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4777 hull
->ineq
[j
] + 1, total
- 1))
4779 isl_int_add(sum
, hull
->ineq
[i
][0],
4781 if (isl_int_abs_lt(sum
,
4782 hull
->ineq
[i
][offset
+ d
]))
4789 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4791 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4795 isl_basic_map_free(hull
);
4796 return pw_multi_aff_from_map_base(map
);
4799 isl_basic_map_free(hull
);
4803 /* Given an affine expression
4805 * [A -> B] -> f(A,B)
4807 * construct an isl_multi_aff
4811 * such that dimension "d" in B' is set to "aff" and the remaining
4812 * dimensions are set equal to the corresponding dimensions in B.
4813 * "n_in" is the dimension of the space A.
4814 * "n_out" is the dimension of the space B.
4816 * If "is_set" is set, then the affine expression is of the form
4820 * and we construct an isl_multi_aff
4824 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4825 unsigned n_in
, unsigned n_out
, int is_set
)
4829 isl_space
*space
, *space2
;
4830 isl_local_space
*ls
;
4832 space
= isl_aff_get_domain_space(aff
);
4833 ls
= isl_local_space_from_space(isl_space_copy(space
));
4834 space2
= isl_space_copy(space
);
4836 space2
= isl_space_range(isl_space_unwrap(space2
));
4837 space
= isl_space_map_from_domain_and_range(space
, space2
);
4838 ma
= isl_multi_aff_alloc(space
);
4839 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4841 for (i
= 0; i
< n_out
; ++i
) {
4844 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4845 isl_dim_set
, n_in
+ i
);
4846 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4849 isl_local_space_free(ls
);
4854 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4855 * taking into account that the dimension at position "d" can be written as
4857 * x = m a + f(..) (1)
4859 * where m is equal to "gcd".
4860 * "i" is the index of the equality in "hull" that defines f(..).
4861 * In particular, the equality is of the form
4863 * f(..) - x + m g(existentials) = 0
4867 * -f(..) + x + m g(existentials) = 0
4869 * We basically plug (1) into "map", resulting in a map with "a"
4870 * in the range instead of "x". The corresponding isl_pw_multi_aff
4871 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4873 * Specifically, given the input map
4877 * We first wrap it into a set
4881 * and define (1) on top of the corresponding space, resulting in "aff".
4882 * We use this to create an isl_multi_aff that maps the output position "d"
4883 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4884 * We plug this into the wrapped map, unwrap the result and compute the
4885 * corresponding isl_pw_multi_aff.
4886 * The result is an expression
4894 * so that we can plug that into "aff", after extending the latter to
4900 * If "map" is actually a set, then there is no "A" space, meaning
4901 * that we do not need to perform any wrapping, and that the result
4902 * of the recursive call is of the form
4906 * which is plugged into a mapping of the form
4910 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4911 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4916 isl_local_space
*ls
;
4919 isl_pw_multi_aff
*pma
, *id
;
4925 is_set
= isl_map_is_set(map
);
4927 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4928 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4929 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4934 set
= isl_map_wrap(map
);
4935 space
= isl_space_map_from_set(isl_set_get_space(set
));
4936 ma
= isl_multi_aff_identity(space
);
4937 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4938 aff
= isl_aff_alloc(ls
);
4940 isl_int_set_si(aff
->v
->el
[0], 1);
4941 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4942 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4945 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4947 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4949 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4950 set
= isl_set_preimage_multi_aff(set
, ma
);
4952 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4957 map
= isl_set_unwrap(set
);
4958 pma
= isl_pw_multi_aff_from_map(map
);
4961 space
= isl_pw_multi_aff_get_domain_space(pma
);
4962 space
= isl_space_map_from_set(space
);
4963 id
= isl_pw_multi_aff_identity(space
);
4964 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4966 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4967 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4969 isl_basic_map_free(hull
);
4973 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4974 * "hull" contains the equalities valid for "map".
4976 * Check if any of the output dimensions is "strided".
4977 * That is, we check if it can be written as
4981 * with m greater than 1, a some combination of existentially quantified
4982 * variables and f an expression in the parameters and input dimensions.
4983 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4985 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4988 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
4989 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
4998 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4999 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5002 isl_basic_map_free(hull
);
5003 return pw_multi_aff_from_map_check_div(map
);
5008 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5009 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5011 for (i
= 0; i
< n_out
; ++i
) {
5012 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5013 isl_int
*eq
= hull
->eq
[j
];
5014 isl_pw_multi_aff
*res
;
5016 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5017 !isl_int_is_negone(eq
[o_out
+ i
]))
5019 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5021 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5022 n_out
- (i
+ 1)) != -1)
5024 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5025 if (isl_int_is_zero(gcd
))
5027 if (isl_int_is_one(gcd
))
5030 res
= pw_multi_aff_from_map_stride(map
, hull
,
5038 isl_basic_map_free(hull
);
5039 return pw_multi_aff_from_map_check_div(map
);
5042 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5044 * As a special case, we first check if all output dimensions are uniquely
5045 * defined in terms of the parameters and input dimensions over the entire
5046 * domain. If so, we extract the desired isl_pw_multi_aff directly
5047 * from the affine hull of "map" and its domain.
5049 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5052 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5055 isl_basic_map
*hull
;
5060 if (isl_map_n_basic_map(map
) == 1) {
5061 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5062 hull
= isl_basic_map_plain_affine_hull(hull
);
5063 sv
= isl_basic_map_plain_is_single_valued(hull
);
5065 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5067 isl_basic_map_free(hull
);
5069 map
= isl_map_detect_equalities(map
);
5070 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5071 sv
= isl_basic_map_plain_is_single_valued(hull
);
5073 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5075 return pw_multi_aff_from_map_check_strides(map
, hull
);
5076 isl_basic_map_free(hull
);
5081 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5083 return isl_pw_multi_aff_from_map(set
);
5086 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5089 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5091 isl_union_pw_multi_aff
**upma
= user
;
5092 isl_pw_multi_aff
*pma
;
5094 pma
= isl_pw_multi_aff_from_map(map
);
5095 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5097 return *upma
? isl_stat_ok
: isl_stat_error
;
5100 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5103 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5104 __isl_take isl_aff
*aff
)
5107 isl_pw_multi_aff
*pma
;
5109 ma
= isl_multi_aff_from_aff(aff
);
5110 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5111 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5114 /* Try and create an isl_union_pw_multi_aff that is equivalent
5115 * to the given isl_union_map.
5116 * The isl_union_map is required to be single-valued in each space.
5117 * Otherwise, an error is produced.
5119 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5120 __isl_take isl_union_map
*umap
)
5123 isl_union_pw_multi_aff
*upma
;
5125 space
= isl_union_map_get_space(umap
);
5126 upma
= isl_union_pw_multi_aff_empty(space
);
5127 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5128 upma
= isl_union_pw_multi_aff_free(upma
);
5129 isl_union_map_free(umap
);
5134 /* Try and create an isl_union_pw_multi_aff that is equivalent
5135 * to the given isl_union_set.
5136 * The isl_union_set is required to be a singleton in each space.
5137 * Otherwise, an error is produced.
5139 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5140 __isl_take isl_union_set
*uset
)
5142 return isl_union_pw_multi_aff_from_union_map(uset
);
5145 /* Return the piecewise affine expression "set ? 1 : 0".
5147 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5150 isl_space
*space
= isl_set_get_space(set
);
5151 isl_local_space
*ls
= isl_local_space_from_space(space
);
5152 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5153 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5155 one
= isl_aff_add_constant_si(one
, 1);
5156 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5157 set
= isl_set_complement(set
);
5158 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5163 /* Plug in "subs" for dimension "type", "pos" of "aff".
5165 * Let i be the dimension to replace and let "subs" be of the form
5169 * and "aff" of the form
5175 * (a f + d g')/(m d)
5177 * where g' is the result of plugging in "subs" in each of the integer
5180 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5181 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5186 aff
= isl_aff_cow(aff
);
5188 return isl_aff_free(aff
);
5190 ctx
= isl_aff_get_ctx(aff
);
5191 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5192 isl_die(ctx
, isl_error_invalid
,
5193 "spaces don't match", return isl_aff_free(aff
));
5194 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5195 isl_die(ctx
, isl_error_unsupported
,
5196 "cannot handle divs yet", return isl_aff_free(aff
));
5198 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5200 return isl_aff_free(aff
);
5202 aff
->v
= isl_vec_cow(aff
->v
);
5204 return isl_aff_free(aff
);
5206 pos
+= isl_local_space_offset(aff
->ls
, type
);
5209 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5210 aff
->v
->size
, subs
->v
->size
, v
);
5216 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5217 * expressions in "maff".
5219 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5220 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5221 __isl_keep isl_aff
*subs
)
5225 maff
= isl_multi_aff_cow(maff
);
5227 return isl_multi_aff_free(maff
);
5229 if (type
== isl_dim_in
)
5232 for (i
= 0; i
< maff
->n
; ++i
) {
5233 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5235 return isl_multi_aff_free(maff
);
5241 /* Plug in "subs" for dimension "type", "pos" of "pma".
5243 * pma is of the form
5247 * while subs is of the form
5249 * v' = B_j(v) -> S_j
5251 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5252 * has a contribution in the result, in particular
5254 * C_ij(S_j) -> M_i(S_j)
5256 * Note that plugging in S_j in C_ij may also result in an empty set
5257 * and this contribution should simply be discarded.
5259 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5260 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5261 __isl_keep isl_pw_aff
*subs
)
5264 isl_pw_multi_aff
*res
;
5267 return isl_pw_multi_aff_free(pma
);
5269 n
= pma
->n
* subs
->n
;
5270 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5272 for (i
= 0; i
< pma
->n
; ++i
) {
5273 for (j
= 0; j
< subs
->n
; ++j
) {
5275 isl_multi_aff
*res_ij
;
5278 common
= isl_set_intersect(
5279 isl_set_copy(pma
->p
[i
].set
),
5280 isl_set_copy(subs
->p
[j
].set
));
5281 common
= isl_set_substitute(common
,
5282 type
, pos
, subs
->p
[j
].aff
);
5283 empty
= isl_set_plain_is_empty(common
);
5284 if (empty
< 0 || empty
) {
5285 isl_set_free(common
);
5291 res_ij
= isl_multi_aff_substitute(
5292 isl_multi_aff_copy(pma
->p
[i
].maff
),
5293 type
, pos
, subs
->p
[j
].aff
);
5295 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5299 isl_pw_multi_aff_free(pma
);
5302 isl_pw_multi_aff_free(pma
);
5303 isl_pw_multi_aff_free(res
);
5307 /* Compute the preimage of a range of dimensions in the affine expression "src"
5308 * under "ma" and put the result in "dst". The number of dimensions in "src"
5309 * that precede the range is given by "n_before". The number of dimensions
5310 * in the range is given by the number of output dimensions of "ma".
5311 * The number of dimensions that follow the range is given by "n_after".
5312 * If "has_denom" is set (to one),
5313 * then "src" and "dst" have an extra initial denominator.
5314 * "n_div_ma" is the number of existentials in "ma"
5315 * "n_div_bset" is the number of existentials in "src"
5316 * The resulting "dst" (which is assumed to have been allocated by
5317 * the caller) contains coefficients for both sets of existentials,
5318 * first those in "ma" and then those in "src".
5319 * f, c1, c2 and g are temporary objects that have been initialized
5322 * Let src represent the expression
5324 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5326 * and let ma represent the expressions
5328 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5330 * We start out with the following expression for dst:
5332 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5334 * with the multiplication factor f initially equal to 1
5335 * and f \sum_i b_i v_i kept separately.
5336 * For each x_i that we substitute, we multiply the numerator
5337 * (and denominator) of dst by c_1 = m_i and add the numerator
5338 * of the x_i expression multiplied by c_2 = f b_i,
5339 * after removing the common factors of c_1 and c_2.
5340 * The multiplication factor f also needs to be multiplied by c_1
5341 * for the next x_j, j > i.
5343 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5344 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5345 int n_div_ma
, int n_div_bmap
,
5346 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5349 int n_param
, n_in
, n_out
;
5352 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5353 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5354 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5356 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5357 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5358 isl_seq_clr(dst
+ o_dst
, n_in
);
5361 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5364 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5366 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5368 isl_int_set_si(f
, 1);
5370 for (i
= 0; i
< n_out
; ++i
) {
5371 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5373 if (isl_int_is_zero(src
[offset
]))
5375 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5376 isl_int_mul(c2
, f
, src
[offset
]);
5377 isl_int_gcd(g
, c1
, c2
);
5378 isl_int_divexact(c1
, c1
, g
);
5379 isl_int_divexact(c2
, c2
, g
);
5381 isl_int_mul(f
, f
, c1
);
5384 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5385 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5386 o_dst
+= 1 + n_param
;
5387 o_src
+= 1 + n_param
;
5388 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5390 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5391 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5394 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5396 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5397 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5400 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5402 isl_int_mul(dst
[0], dst
[0], c1
);
5406 /* Compute the pullback of "aff" by the function represented by "ma".
5407 * In other words, plug in "ma" in "aff". The result is an affine expression
5408 * defined over the domain space of "ma".
5410 * If "aff" is represented by
5412 * (a(p) + b x + c(divs))/d
5414 * and ma is represented by
5416 * x = D(p) + F(y) + G(divs')
5418 * then the result is
5420 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5422 * The divs in the local space of the input are similarly adjusted
5423 * through a call to isl_local_space_preimage_multi_aff.
5425 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5426 __isl_take isl_multi_aff
*ma
)
5428 isl_aff
*res
= NULL
;
5429 isl_local_space
*ls
;
5430 int n_div_aff
, n_div_ma
;
5431 isl_int f
, c1
, c2
, g
;
5433 ma
= isl_multi_aff_align_divs(ma
);
5437 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5438 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5440 ls
= isl_aff_get_domain_local_space(aff
);
5441 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5442 res
= isl_aff_alloc(ls
);
5451 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5460 isl_multi_aff_free(ma
);
5461 res
= isl_aff_normalize(res
);
5465 isl_multi_aff_free(ma
);
5470 /* Compute the pullback of "aff1" by the function represented by "aff2".
5471 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5472 * defined over the domain space of "aff1".
5474 * The domain of "aff1" should match the range of "aff2", which means
5475 * that it should be single-dimensional.
5477 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5478 __isl_take isl_aff
*aff2
)
5482 ma
= isl_multi_aff_from_aff(aff2
);
5483 return isl_aff_pullback_multi_aff(aff1
, ma
);
5486 /* Compute the pullback of "ma1" by the function represented by "ma2".
5487 * In other words, plug in "ma2" in "ma1".
5489 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5491 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5492 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5495 isl_space
*space
= NULL
;
5497 ma2
= isl_multi_aff_align_divs(ma2
);
5498 ma1
= isl_multi_aff_cow(ma1
);
5502 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5503 isl_multi_aff_get_space(ma1
));
5505 for (i
= 0; i
< ma1
->n
; ++i
) {
5506 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5507 isl_multi_aff_copy(ma2
));
5512 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5513 isl_multi_aff_free(ma2
);
5516 isl_space_free(space
);
5517 isl_multi_aff_free(ma2
);
5518 isl_multi_aff_free(ma1
);
5522 /* Compute the pullback of "ma1" by the function represented by "ma2".
5523 * In other words, plug in "ma2" in "ma1".
5525 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5526 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5528 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5529 &isl_multi_aff_pullback_multi_aff_aligned
);
5532 /* Extend the local space of "dst" to include the divs
5533 * in the local space of "src".
5535 * If "src" does not have any divs or if the local spaces of "dst" and
5536 * "src" are the same, then no extension is required.
5538 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5539 __isl_keep isl_aff
*src
)
5542 int src_n_div
, dst_n_div
;
5549 return isl_aff_free(dst
);
5551 ctx
= isl_aff_get_ctx(src
);
5552 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5554 return isl_aff_free(dst
);
5556 isl_die(ctx
, isl_error_invalid
,
5557 "spaces don't match", goto error
);
5559 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5562 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5564 return isl_aff_free(dst
);
5568 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5569 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5570 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5571 if (!exp1
|| (dst_n_div
&& !exp2
))
5574 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5575 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5583 return isl_aff_free(dst
);
5586 /* Adjust the local spaces of the affine expressions in "maff"
5587 * such that they all have the save divs.
5589 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5590 __isl_take isl_multi_aff
*maff
)
5598 maff
= isl_multi_aff_cow(maff
);
5602 for (i
= 1; i
< maff
->n
; ++i
)
5603 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5604 for (i
= 1; i
< maff
->n
; ++i
) {
5605 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5607 return isl_multi_aff_free(maff
);
5613 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5615 aff
= isl_aff_cow(aff
);
5619 aff
->ls
= isl_local_space_lift(aff
->ls
);
5621 return isl_aff_free(aff
);
5626 /* Lift "maff" to a space with extra dimensions such that the result
5627 * has no more existentially quantified variables.
5628 * If "ls" is not NULL, then *ls is assigned the local space that lies
5629 * at the basis of the lifting applied to "maff".
5631 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5632 __isl_give isl_local_space
**ls
)
5646 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5647 *ls
= isl_local_space_from_space(space
);
5649 return isl_multi_aff_free(maff
);
5654 maff
= isl_multi_aff_cow(maff
);
5655 maff
= isl_multi_aff_align_divs(maff
);
5659 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5660 space
= isl_multi_aff_get_space(maff
);
5661 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5662 space
= isl_space_extend_domain_with_range(space
,
5663 isl_multi_aff_get_space(maff
));
5665 return isl_multi_aff_free(maff
);
5666 isl_space_free(maff
->space
);
5667 maff
->space
= space
;
5670 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5672 return isl_multi_aff_free(maff
);
5675 for (i
= 0; i
< maff
->n
; ++i
) {
5676 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5684 isl_local_space_free(*ls
);
5685 return isl_multi_aff_free(maff
);
5689 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5691 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5692 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5702 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5703 if (pos
< 0 || pos
>= n_out
)
5704 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5705 "index out of bounds", return NULL
);
5707 space
= isl_pw_multi_aff_get_space(pma
);
5708 space
= isl_space_drop_dims(space
, isl_dim_out
,
5709 pos
+ 1, n_out
- pos
- 1);
5710 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5712 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5713 for (i
= 0; i
< pma
->n
; ++i
) {
5715 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5716 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5722 /* Return an isl_pw_multi_aff with the given "set" as domain and
5723 * an unnamed zero-dimensional range.
5725 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5726 __isl_take isl_set
*set
)
5731 space
= isl_set_get_space(set
);
5732 space
= isl_space_from_domain(space
);
5733 ma
= isl_multi_aff_zero(space
);
5734 return isl_pw_multi_aff_alloc(set
, ma
);
5737 /* Add an isl_pw_multi_aff with the given "set" as domain and
5738 * an unnamed zero-dimensional range to *user.
5740 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5743 isl_union_pw_multi_aff
**upma
= user
;
5744 isl_pw_multi_aff
*pma
;
5746 pma
= isl_pw_multi_aff_from_domain(set
);
5747 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5752 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5753 * an unnamed zero-dimensional range.
5755 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5756 __isl_take isl_union_set
*uset
)
5759 isl_union_pw_multi_aff
*upma
;
5764 space
= isl_union_set_get_space(uset
);
5765 upma
= isl_union_pw_multi_aff_empty(space
);
5767 if (isl_union_set_foreach_set(uset
,
5768 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5771 isl_union_set_free(uset
);
5774 isl_union_set_free(uset
);
5775 isl_union_pw_multi_aff_free(upma
);
5779 /* Convert "pma" to an isl_map and add it to *umap.
5781 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5784 isl_union_map
**umap
= user
;
5787 map
= isl_map_from_pw_multi_aff(pma
);
5788 *umap
= isl_union_map_add_map(*umap
, map
);
5793 /* Construct a union map mapping the domain of the union
5794 * piecewise multi-affine expression to its range, with each dimension
5795 * in the range equated to the corresponding affine expression on its cell.
5797 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5798 __isl_take isl_union_pw_multi_aff
*upma
)
5801 isl_union_map
*umap
;
5806 space
= isl_union_pw_multi_aff_get_space(upma
);
5807 umap
= isl_union_map_empty(space
);
5809 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5810 &map_from_pw_multi_aff
, &umap
) < 0)
5813 isl_union_pw_multi_aff_free(upma
);
5816 isl_union_pw_multi_aff_free(upma
);
5817 isl_union_map_free(umap
);
5821 /* Local data for bin_entry and the callback "fn".
5823 struct isl_union_pw_multi_aff_bin_data
{
5824 isl_union_pw_multi_aff
*upma2
;
5825 isl_union_pw_multi_aff
*res
;
5826 isl_pw_multi_aff
*pma
;
5827 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5830 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5831 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5833 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5835 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5839 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5841 isl_pw_multi_aff_free(pma
);
5846 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5847 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5848 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5849 * as *entry. The callback should adjust data->res if desired.
5851 static __isl_give isl_union_pw_multi_aff
*bin_op(
5852 __isl_take isl_union_pw_multi_aff
*upma1
,
5853 __isl_take isl_union_pw_multi_aff
*upma2
,
5854 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5857 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5859 space
= isl_union_pw_multi_aff_get_space(upma2
);
5860 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5861 space
= isl_union_pw_multi_aff_get_space(upma1
);
5862 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5864 if (!upma1
|| !upma2
)
5868 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5869 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5870 &bin_entry
, &data
) < 0)
5873 isl_union_pw_multi_aff_free(upma1
);
5874 isl_union_pw_multi_aff_free(upma2
);
5877 isl_union_pw_multi_aff_free(upma1
);
5878 isl_union_pw_multi_aff_free(upma2
);
5879 isl_union_pw_multi_aff_free(data
.res
);
5883 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5884 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5886 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5887 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5891 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5892 isl_pw_multi_aff_get_space(pma2
));
5893 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5894 &isl_multi_aff_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_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_range_product
);
5907 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5908 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5910 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5911 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5915 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5916 isl_pw_multi_aff_get_space(pma2
));
5917 space
= isl_space_flatten_range(space
);
5918 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5919 &isl_multi_aff_flat_range_product
);
5922 /* Given two isl_pw_multi_affs A -> B and C -> D,
5923 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5925 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5926 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5928 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5929 &pw_multi_aff_flat_range_product
);
5932 /* If data->pma and "pma2" have the same domain space, then compute
5933 * their flat range product and the result to data->res.
5935 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5938 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5940 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5941 pma2
->dim
, isl_dim_in
)) {
5942 isl_pw_multi_aff_free(pma2
);
5946 pma2
= isl_pw_multi_aff_flat_range_product(
5947 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5949 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5954 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5955 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5957 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5958 __isl_take isl_union_pw_multi_aff
*upma1
,
5959 __isl_take isl_union_pw_multi_aff
*upma2
)
5961 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5964 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5965 * The parameters are assumed to have been aligned.
5967 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5968 * except that it works on two different isl_pw_* types.
5970 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5971 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5972 __isl_take isl_pw_aff
*pa
)
5975 isl_pw_multi_aff
*res
= NULL
;
5980 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5981 pa
->dim
, isl_dim_in
))
5982 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5983 "domains don't match", goto error
);
5984 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5985 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5986 "index out of bounds", goto error
);
5989 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5991 for (i
= 0; i
< pma
->n
; ++i
) {
5992 for (j
= 0; j
< pa
->n
; ++j
) {
5994 isl_multi_aff
*res_ij
;
5997 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5998 isl_set_copy(pa
->p
[j
].set
));
5999 empty
= isl_set_plain_is_empty(common
);
6000 if (empty
< 0 || empty
) {
6001 isl_set_free(common
);
6007 res_ij
= isl_multi_aff_set_aff(
6008 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6009 isl_aff_copy(pa
->p
[j
].aff
));
6010 res_ij
= isl_multi_aff_gist(res_ij
,
6011 isl_set_copy(common
));
6013 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6017 isl_pw_multi_aff_free(pma
);
6018 isl_pw_aff_free(pa
);
6021 isl_pw_multi_aff_free(pma
);
6022 isl_pw_aff_free(pa
);
6023 return isl_pw_multi_aff_free(res
);
6026 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6028 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6029 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6030 __isl_take isl_pw_aff
*pa
)
6034 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
6035 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6036 if (!isl_space_has_named_params(pma
->dim
) ||
6037 !isl_space_has_named_params(pa
->dim
))
6038 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6039 "unaligned unnamed parameters", goto error
);
6040 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6041 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6042 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6044 isl_pw_multi_aff_free(pma
);
6045 isl_pw_aff_free(pa
);
6049 /* Do the parameters of "pa" match those of "space"?
6051 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6052 __isl_keep isl_space
*space
)
6054 isl_space
*pa_space
;
6060 pa_space
= isl_pw_aff_get_space(pa
);
6062 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6064 isl_space_free(pa_space
);
6068 /* Check that the domain space of "pa" matches "space".
6070 * Return 0 on success and -1 on error.
6072 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6073 __isl_keep isl_space
*space
)
6075 isl_space
*pa_space
;
6081 pa_space
= isl_pw_aff_get_space(pa
);
6083 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6087 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6088 "parameters don't match", goto error
);
6089 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6090 pa_space
, isl_dim_in
);
6094 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6095 "domains don't match", goto error
);
6096 isl_space_free(pa_space
);
6099 isl_space_free(pa_space
);
6108 #include <isl_multi_templ.c>
6109 #include <isl_multi_apply_set.c>
6110 #include <isl_multi_coalesce.c>
6111 #include <isl_multi_gist.c>
6112 #include <isl_multi_hash.c>
6113 #include <isl_multi_intersect.c>
6115 /* Scale the elements of "pma" by the corresponding elements of "mv".
6117 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6118 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6122 pma
= isl_pw_multi_aff_cow(pma
);
6125 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6126 mv
->space
, isl_dim_set
))
6127 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6128 "spaces don't match", goto error
);
6129 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6130 mv
->space
, isl_dim_param
)) {
6131 pma
= isl_pw_multi_aff_align_params(pma
,
6132 isl_multi_val_get_space(mv
));
6133 mv
= isl_multi_val_align_params(mv
,
6134 isl_pw_multi_aff_get_space(pma
));
6139 for (i
= 0; i
< pma
->n
; ++i
) {
6140 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6141 isl_multi_val_copy(mv
));
6142 if (!pma
->p
[i
].maff
)
6146 isl_multi_val_free(mv
);
6149 isl_multi_val_free(mv
);
6150 isl_pw_multi_aff_free(pma
);
6154 /* This function is called for each entry of an isl_union_pw_multi_aff.
6155 * If the space of the entry matches that of data->mv,
6156 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6157 * Otherwise, return an empty isl_pw_multi_aff.
6159 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6160 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6162 isl_multi_val
*mv
= user
;
6166 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6167 mv
->space
, isl_dim_set
)) {
6168 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6169 isl_pw_multi_aff_free(pma
);
6170 return isl_pw_multi_aff_empty(space
);
6173 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6176 /* Scale the elements of "upma" by the corresponding elements of "mv",
6177 * for those entries that match the space of "mv".
6179 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6180 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6182 upma
= isl_union_pw_multi_aff_align_params(upma
,
6183 isl_multi_val_get_space(mv
));
6184 mv
= isl_multi_val_align_params(mv
,
6185 isl_union_pw_multi_aff_get_space(upma
));
6189 return isl_union_pw_multi_aff_transform(upma
,
6190 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6192 isl_multi_val_free(mv
);
6195 isl_multi_val_free(mv
);
6196 isl_union_pw_multi_aff_free(upma
);
6200 /* Construct and return a piecewise multi affine expression
6201 * in the given space with value zero in each of the output dimensions and
6202 * a universe domain.
6204 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6206 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6209 /* Construct and return a piecewise multi affine expression
6210 * that is equal to the given piecewise affine expression.
6212 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6213 __isl_take isl_pw_aff
*pa
)
6217 isl_pw_multi_aff
*pma
;
6222 space
= isl_pw_aff_get_space(pa
);
6223 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6225 for (i
= 0; i
< pa
->n
; ++i
) {
6229 set
= isl_set_copy(pa
->p
[i
].set
);
6230 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6231 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6234 isl_pw_aff_free(pa
);
6238 /* Construct a set or map mapping the shared (parameter) domain
6239 * of the piecewise affine expressions to the range of "mpa"
6240 * with each dimension in the range equated to the
6241 * corresponding piecewise affine expression.
6243 static __isl_give isl_map
*map_from_multi_pw_aff(
6244 __isl_take isl_multi_pw_aff
*mpa
)
6253 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6254 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6255 "invalid space", goto error
);
6257 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6258 map
= isl_map_universe(isl_space_from_domain(space
));
6260 for (i
= 0; i
< mpa
->n
; ++i
) {
6264 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6265 map_i
= map_from_pw_aff(pa
);
6267 map
= isl_map_flat_range_product(map
, map_i
);
6270 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6272 isl_multi_pw_aff_free(mpa
);
6275 isl_multi_pw_aff_free(mpa
);
6279 /* Construct a map mapping the shared domain
6280 * of the piecewise affine expressions to the range of "mpa"
6281 * with each dimension in the range equated to the
6282 * corresponding piecewise affine expression.
6284 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6288 if (isl_space_is_set(mpa
->space
))
6289 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6290 "space of input is not a map", goto error
);
6292 return map_from_multi_pw_aff(mpa
);
6294 isl_multi_pw_aff_free(mpa
);
6298 /* Construct a set mapping the shared parameter domain
6299 * of the piecewise affine expressions to the space of "mpa"
6300 * with each dimension in the range equated to the
6301 * corresponding piecewise affine expression.
6303 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6307 if (!isl_space_is_set(mpa
->space
))
6308 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6309 "space of input is not a set", goto error
);
6311 return map_from_multi_pw_aff(mpa
);
6313 isl_multi_pw_aff_free(mpa
);
6317 /* Construct and return a piecewise multi affine expression
6318 * that is equal to the given multi piecewise affine expression
6319 * on the shared domain of the piecewise affine expressions.
6321 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6322 __isl_take isl_multi_pw_aff
*mpa
)
6327 isl_pw_multi_aff
*pma
;
6332 space
= isl_multi_pw_aff_get_space(mpa
);
6335 isl_multi_pw_aff_free(mpa
);
6336 return isl_pw_multi_aff_zero(space
);
6339 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6340 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6342 for (i
= 1; i
< mpa
->n
; ++i
) {
6343 isl_pw_multi_aff
*pma_i
;
6345 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6346 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6347 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6350 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6352 isl_multi_pw_aff_free(mpa
);
6356 /* Construct and return a multi piecewise affine expression
6357 * that is equal to the given multi affine expression.
6359 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6360 __isl_take isl_multi_aff
*ma
)
6363 isl_multi_pw_aff
*mpa
;
6368 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6369 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6371 for (i
= 0; i
< n
; ++i
) {
6374 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6375 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6378 isl_multi_aff_free(ma
);
6382 /* Construct and return a multi piecewise affine expression
6383 * that is equal to the given piecewise multi affine expression.
6385 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6386 __isl_take isl_pw_multi_aff
*pma
)
6390 isl_multi_pw_aff
*mpa
;
6395 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6396 space
= isl_pw_multi_aff_get_space(pma
);
6397 mpa
= isl_multi_pw_aff_alloc(space
);
6399 for (i
= 0; i
< n
; ++i
) {
6402 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6403 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6406 isl_pw_multi_aff_free(pma
);
6410 /* Do "pa1" and "pa2" represent the same function?
6412 * We first check if they are obviously equal.
6413 * If not, we convert them to maps and check if those are equal.
6415 * If "pa1" or "pa2" contain any NaNs, then they are considered
6416 * not to be the same. A NaN is not equal to anything, not even
6419 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6423 isl_map
*map1
, *map2
;
6428 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6429 if (equal
< 0 || equal
)
6431 has_nan
= isl_pw_aff_involves_nan(pa1
);
6432 if (has_nan
>= 0 && !has_nan
)
6433 has_nan
= isl_pw_aff_involves_nan(pa2
);
6439 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6440 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6441 equal
= isl_map_is_equal(map1
, map2
);
6448 /* Do "mpa1" and "mpa2" represent the same function?
6450 * Note that we cannot convert the entire isl_multi_pw_aff
6451 * to a map because the domains of the piecewise affine expressions
6452 * may not be the same.
6454 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6455 __isl_keep isl_multi_pw_aff
*mpa2
)
6461 return isl_bool_error
;
6463 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6464 mpa2
->space
, isl_dim_param
)) {
6465 if (!isl_space_has_named_params(mpa1
->space
))
6466 return isl_bool_false
;
6467 if (!isl_space_has_named_params(mpa2
->space
))
6468 return isl_bool_false
;
6469 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6470 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6471 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6472 isl_multi_pw_aff_get_space(mpa2
));
6473 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6474 isl_multi_pw_aff_get_space(mpa1
));
6475 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6476 isl_multi_pw_aff_free(mpa1
);
6477 isl_multi_pw_aff_free(mpa2
);
6481 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6482 if (equal
< 0 || !equal
)
6485 for (i
= 0; i
< mpa1
->n
; ++i
) {
6486 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6487 if (equal
< 0 || !equal
)
6491 return isl_bool_true
;
6494 /* Compute the pullback of "mpa" by the function represented by "ma".
6495 * In other words, plug in "ma" in "mpa".
6497 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6499 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6500 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6503 isl_space
*space
= NULL
;
6505 mpa
= isl_multi_pw_aff_cow(mpa
);
6509 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6510 isl_multi_pw_aff_get_space(mpa
));
6514 for (i
= 0; i
< mpa
->n
; ++i
) {
6515 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6516 isl_multi_aff_copy(ma
));
6521 isl_multi_aff_free(ma
);
6522 isl_space_free(mpa
->space
);
6526 isl_space_free(space
);
6527 isl_multi_pw_aff_free(mpa
);
6528 isl_multi_aff_free(ma
);
6532 /* Compute the pullback of "mpa" by the function represented by "ma".
6533 * In other words, plug in "ma" in "mpa".
6535 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6536 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6540 if (isl_space_match(mpa
->space
, isl_dim_param
,
6541 ma
->space
, isl_dim_param
))
6542 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6543 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6544 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6545 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6547 isl_multi_pw_aff_free(mpa
);
6548 isl_multi_aff_free(ma
);
6552 /* Compute the pullback of "mpa" by the function represented by "pma".
6553 * In other words, plug in "pma" in "mpa".
6555 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6557 static __isl_give isl_multi_pw_aff
*
6558 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6559 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6562 isl_space
*space
= NULL
;
6564 mpa
= isl_multi_pw_aff_cow(mpa
);
6568 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6569 isl_multi_pw_aff_get_space(mpa
));
6571 for (i
= 0; i
< mpa
->n
; ++i
) {
6572 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6573 isl_pw_multi_aff_copy(pma
));
6578 isl_pw_multi_aff_free(pma
);
6579 isl_space_free(mpa
->space
);
6583 isl_space_free(space
);
6584 isl_multi_pw_aff_free(mpa
);
6585 isl_pw_multi_aff_free(pma
);
6589 /* Compute the pullback of "mpa" by the function represented by "pma".
6590 * In other words, plug in "pma" in "mpa".
6592 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6593 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6597 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6598 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6599 mpa
= isl_multi_pw_aff_align_params(mpa
,
6600 isl_pw_multi_aff_get_space(pma
));
6601 pma
= isl_pw_multi_aff_align_params(pma
,
6602 isl_multi_pw_aff_get_space(mpa
));
6603 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6605 isl_multi_pw_aff_free(mpa
);
6606 isl_pw_multi_aff_free(pma
);
6610 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6611 * with the domain of "aff". The domain of the result is the same
6613 * "mpa" and "aff" are assumed to have been aligned.
6615 * We first extract the parametric constant from "aff", defined
6616 * over the correct domain.
6617 * Then we add the appropriate combinations of the members of "mpa".
6618 * Finally, we add the integer divisions through recursive calls.
6620 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6621 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6629 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6630 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6632 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6633 tmp
= isl_aff_copy(aff
);
6634 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6635 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6636 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6637 isl_space_dim(space
, isl_dim_set
));
6638 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6639 pa
= isl_pw_aff_from_aff(tmp
);
6641 for (i
= 0; i
< n_in
; ++i
) {
6644 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6646 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6647 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6648 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6649 pa
= isl_pw_aff_add(pa
, pa_i
);
6652 for (i
= 0; i
< n_div
; ++i
) {
6656 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6658 div
= isl_aff_get_div(aff
, i
);
6659 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6660 isl_multi_pw_aff_copy(mpa
), div
);
6661 pa_i
= isl_pw_aff_floor(pa_i
);
6662 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6663 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6664 pa
= isl_pw_aff_add(pa
, pa_i
);
6667 isl_multi_pw_aff_free(mpa
);
6673 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6674 * with the domain of "aff". The domain of the result is the same
6677 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6678 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6682 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6683 mpa
->space
, isl_dim_param
))
6684 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6686 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6687 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6689 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6692 isl_multi_pw_aff_free(mpa
);
6696 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6697 * with the domain of "pa". The domain of the result is the same
6699 * "mpa" and "pa" are assumed to have been aligned.
6701 * We consider each piece in turn. Note that the domains of the
6702 * pieces are assumed to be disjoint and they remain disjoint
6703 * after taking the preimage (over the same function).
6705 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6706 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6715 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6716 isl_pw_aff_get_space(pa
));
6717 res
= isl_pw_aff_empty(space
);
6719 for (i
= 0; i
< pa
->n
; ++i
) {
6723 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6724 isl_multi_pw_aff_copy(mpa
),
6725 isl_aff_copy(pa
->p
[i
].aff
));
6726 domain
= isl_set_copy(pa
->p
[i
].set
);
6727 domain
= isl_set_preimage_multi_pw_aff(domain
,
6728 isl_multi_pw_aff_copy(mpa
));
6729 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6730 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6733 isl_pw_aff_free(pa
);
6734 isl_multi_pw_aff_free(mpa
);
6737 isl_pw_aff_free(pa
);
6738 isl_multi_pw_aff_free(mpa
);
6742 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6743 * with the domain of "pa". The domain of the result is the same
6746 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6747 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6751 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6752 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6754 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6755 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6757 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6759 isl_pw_aff_free(pa
);
6760 isl_multi_pw_aff_free(mpa
);
6764 /* Compute the pullback of "pa" by the function represented by "mpa".
6765 * In other words, plug in "mpa" in "pa".
6766 * "pa" and "mpa" are assumed to have been aligned.
6768 * The pullback is computed by applying "pa" to "mpa".
6770 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6771 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6773 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6776 /* Compute the pullback of "pa" by the function represented by "mpa".
6777 * In other words, plug in "mpa" in "pa".
6779 * The pullback is computed by applying "pa" to "mpa".
6781 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6782 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6784 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6787 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6788 * In other words, plug in "mpa2" in "mpa1".
6790 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6792 * We pullback each member of "mpa1" in turn.
6794 static __isl_give isl_multi_pw_aff
*
6795 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6796 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6799 isl_space
*space
= NULL
;
6801 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6805 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6806 isl_multi_pw_aff_get_space(mpa1
));
6808 for (i
= 0; i
< mpa1
->n
; ++i
) {
6809 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6810 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6815 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6817 isl_multi_pw_aff_free(mpa2
);
6820 isl_space_free(space
);
6821 isl_multi_pw_aff_free(mpa1
);
6822 isl_multi_pw_aff_free(mpa2
);
6826 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6827 * In other words, plug in "mpa2" in "mpa1".
6829 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6830 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6832 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6833 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6836 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6837 * of "mpa1" and "mpa2" live in the same space, construct map space
6838 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6839 * with this map space as extract argument.
6841 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6842 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6843 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6844 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6847 isl_space
*space1
, *space2
;
6850 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6851 isl_multi_pw_aff_get_space(mpa2
));
6852 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6853 isl_multi_pw_aff_get_space(mpa1
));
6856 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6857 mpa2
->space
, isl_dim_out
);
6861 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6862 "range spaces don't match", goto error
);
6863 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6864 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6865 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6867 res
= order(mpa1
, mpa2
, space1
);
6868 isl_multi_pw_aff_free(mpa1
);
6869 isl_multi_pw_aff_free(mpa2
);
6872 isl_multi_pw_aff_free(mpa1
);
6873 isl_multi_pw_aff_free(mpa2
);
6877 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6878 * where the function values are equal. "space" is the space of the result.
6879 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6881 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6882 * in the sequences are equal.
6884 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6885 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6886 __isl_take isl_space
*space
)
6891 res
= isl_map_universe(space
);
6893 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6894 for (i
= 0; i
< n
; ++i
) {
6895 isl_pw_aff
*pa1
, *pa2
;
6898 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6899 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6900 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6901 res
= isl_map_intersect(res
, map
);
6907 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6908 * where the function values are equal.
6910 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6911 __isl_take isl_multi_pw_aff
*mpa2
)
6913 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6914 &isl_multi_pw_aff_eq_map_on_space
);
6917 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6918 * where the function values of "mpa1" is lexicographically satisfies "base"
6919 * compared to that of "mpa2". "space" is the space of the result.
6920 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6922 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6923 * if its i-th element satisfies "base" when compared to
6924 * the i-th element of "mpa2" while all previous elements are
6927 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6928 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6929 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6930 __isl_take isl_pw_aff
*pa2
),
6931 __isl_take isl_space
*space
)
6934 isl_map
*res
, *rest
;
6936 res
= isl_map_empty(isl_space_copy(space
));
6937 rest
= isl_map_universe(space
);
6939 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6940 for (i
= 0; i
< n
; ++i
) {
6941 isl_pw_aff
*pa1
, *pa2
;
6944 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6945 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6946 map
= base(pa1
, pa2
);
6947 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6948 res
= isl_map_union(res
, map
);
6953 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6954 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6955 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6956 rest
= isl_map_intersect(rest
, map
);
6963 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6964 * where the function value of "mpa1" is lexicographically less than that
6965 * of "mpa2". "space" is the space of the result.
6966 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6968 * "mpa1" is less than "mpa2" if its i-th element is smaller
6969 * than the i-th element of "mpa2" while all previous elements are
6972 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
6973 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6974 __isl_take isl_space
*space
)
6976 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6977 &isl_pw_aff_lt_map
, space
);
6980 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6981 * where the function value of "mpa1" is lexicographically less than that
6984 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
6985 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6987 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6988 &isl_multi_pw_aff_lex_lt_map_on_space
);
6991 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6992 * where the function value of "mpa1" is lexicographically greater than that
6993 * of "mpa2". "space" is the space of the result.
6994 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6996 * "mpa1" is greater than "mpa2" if its i-th element is greater
6997 * than the i-th element of "mpa2" while all previous elements are
7000 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7001 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7002 __isl_take isl_space
*space
)
7004 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7005 &isl_pw_aff_gt_map
, space
);
7008 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7009 * where the function value of "mpa1" is lexicographically greater than that
7012 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7013 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7015 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7016 &isl_multi_pw_aff_lex_gt_map_on_space
);
7019 /* Compare two isl_affs.
7021 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7022 * than "aff2" and 0 if they are equal.
7024 * The order is fairly arbitrary. We do consider expressions that only involve
7025 * earlier dimensions as "smaller".
7027 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7040 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7044 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7045 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7047 return last1
- last2
;
7049 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7052 /* Compare two isl_pw_affs.
7054 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7055 * than "pa2" and 0 if they are equal.
7057 * The order is fairly arbitrary. We do consider expressions that only involve
7058 * earlier dimensions as "smaller".
7060 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7061 __isl_keep isl_pw_aff
*pa2
)
7074 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7078 if (pa1
->n
!= pa2
->n
)
7079 return pa1
->n
- pa2
->n
;
7081 for (i
= 0; i
< pa1
->n
; ++i
) {
7082 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7085 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7093 /* Return a piecewise affine expression that is equal to "v" on "domain".
7095 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7096 __isl_take isl_val
*v
)
7099 isl_local_space
*ls
;
7102 space
= isl_set_get_space(domain
);
7103 ls
= isl_local_space_from_space(space
);
7104 aff
= isl_aff_val_on_domain(ls
, v
);
7106 return isl_pw_aff_alloc(domain
, aff
);
7109 /* Return a multi affine expression that is equal to "mv" on domain
7112 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7113 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7117 isl_local_space
*ls
;
7123 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7124 space2
= isl_multi_val_get_space(mv
);
7125 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7126 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7127 space
= isl_space_map_from_domain_and_range(space
, space2
);
7128 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7129 ls
= isl_local_space_from_space(isl_space_domain(space
));
7130 for (i
= 0; i
< n
; ++i
) {
7134 v
= isl_multi_val_get_val(mv
, i
);
7135 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7136 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7138 isl_local_space_free(ls
);
7140 isl_multi_val_free(mv
);
7143 isl_space_free(space
);
7144 isl_multi_val_free(mv
);
7148 /* Return a piecewise multi-affine expression
7149 * that is equal to "mv" on "domain".
7151 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7152 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7157 space
= isl_set_get_space(domain
);
7158 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7160 return isl_pw_multi_aff_alloc(domain
, ma
);
7163 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7164 * mv is the value that should be attained on each domain set
7165 * res collects the results
7167 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7169 isl_union_pw_multi_aff
*res
;
7172 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7173 * and add it to data->res.
7175 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7178 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7179 isl_pw_multi_aff
*pma
;
7182 mv
= isl_multi_val_copy(data
->mv
);
7183 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7184 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7186 return data
->res
? isl_stat_ok
: isl_stat_error
;
7189 /* Return a union piecewise multi-affine expression
7190 * that is equal to "mv" on "domain".
7192 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7193 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7195 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7198 space
= isl_union_set_get_space(domain
);
7199 data
.res
= isl_union_pw_multi_aff_empty(space
);
7201 if (isl_union_set_foreach_set(domain
,
7202 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7203 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7204 isl_union_set_free(domain
);
7205 isl_multi_val_free(mv
);
7209 /* Compute the pullback of data->pma by the function represented by "pma2",
7210 * provided the spaces match, and add the results to data->res.
7212 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7214 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7216 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7217 pma2
->dim
, isl_dim_out
)) {
7218 isl_pw_multi_aff_free(pma2
);
7222 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7223 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7225 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7227 return isl_stat_error
;
7232 /* Compute the pullback of "upma1" by the function represented by "upma2".
7234 __isl_give isl_union_pw_multi_aff
*
7235 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7236 __isl_take isl_union_pw_multi_aff
*upma1
,
7237 __isl_take isl_union_pw_multi_aff
*upma2
)
7239 return bin_op(upma1
, upma2
, &pullback_entry
);
7242 /* Check that the domain space of "upa" matches "space".
7244 * Return 0 on success and -1 on error.
7246 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7247 * can in principle never fail since the space "space" is that
7248 * of the isl_multi_union_pw_aff and is a set space such that
7249 * there is no domain space to match.
7251 * We check the parameters and double-check that "space" is
7252 * indeed that of a set.
7254 static int isl_union_pw_aff_check_match_domain_space(
7255 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7257 isl_space
*upa_space
;
7263 match
= isl_space_is_set(space
);
7267 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7268 "expecting set space", return -1);
7270 upa_space
= isl_union_pw_aff_get_space(upa
);
7271 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7275 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7276 "parameters don't match", goto error
);
7278 isl_space_free(upa_space
);
7281 isl_space_free(upa_space
);
7285 /* Do the parameters of "upa" match those of "space"?
7287 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7288 __isl_keep isl_space
*space
)
7290 isl_space
*upa_space
;
7296 upa_space
= isl_union_pw_aff_get_space(upa
);
7298 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7300 isl_space_free(upa_space
);
7304 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7305 * space represents the new parameters.
7306 * res collects the results.
7308 struct isl_union_pw_aff_reset_params_data
{
7310 isl_union_pw_aff
*res
;
7313 /* Replace the parameters of "pa" by data->space and
7314 * add the result to data->res.
7316 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7318 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7321 space
= isl_pw_aff_get_space(pa
);
7322 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7323 pa
= isl_pw_aff_reset_space(pa
, space
);
7324 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7326 return data
->res
? isl_stat_ok
: isl_stat_error
;
7329 /* Replace the domain space of "upa" by "space".
7330 * Since a union expression does not have a (single) domain space,
7331 * "space" is necessarily a parameter space.
7333 * Since the order and the names of the parameters determine
7334 * the hash value, we need to create a new hash table.
7336 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7337 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7339 struct isl_union_pw_aff_reset_params_data data
= { space
};
7342 match
= isl_union_pw_aff_matching_params(upa
, space
);
7344 upa
= isl_union_pw_aff_free(upa
);
7346 isl_space_free(space
);
7350 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7351 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7352 data
.res
= isl_union_pw_aff_free(data
.res
);
7354 isl_union_pw_aff_free(upa
);
7355 isl_space_free(space
);
7359 /* Return the floor of "pa".
7361 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7363 return isl_pw_aff_floor(pa
);
7366 /* Given f, return floor(f).
7368 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7369 __isl_take isl_union_pw_aff
*upa
)
7371 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7376 * upa mod m = upa - m * floor(upa/m)
7378 * with m an integer value.
7380 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7381 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7383 isl_union_pw_aff
*res
;
7388 if (!isl_val_is_int(m
))
7389 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7390 "expecting integer modulo", goto error
);
7391 if (!isl_val_is_pos(m
))
7392 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7393 "expecting positive modulo", goto error
);
7395 res
= isl_union_pw_aff_copy(upa
);
7396 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7397 upa
= isl_union_pw_aff_floor(upa
);
7398 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7399 res
= isl_union_pw_aff_sub(res
, upa
);
7404 isl_union_pw_aff_free(upa
);
7408 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7409 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7411 * "res" collects the results.
7413 struct isl_union_pw_aff_aff_on_domain_data
{
7415 isl_union_pw_aff
*res
;
7418 /* Construct a piecewise affine expression that is equal to data->aff
7419 * on "domain" and add the result to data->res.
7421 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7423 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7428 aff
= isl_aff_copy(data
->aff
);
7429 dim
= isl_set_dim(domain
, isl_dim_set
);
7430 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7431 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7432 pa
= isl_pw_aff_alloc(domain
, aff
);
7433 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7435 return data
->res
? isl_stat_ok
: isl_stat_error
;
7438 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7439 * pos is the output position that needs to be extracted.
7440 * res collects the results.
7442 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7444 isl_union_pw_aff
*res
;
7447 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7448 * (assuming it has such a dimension) and add it to data->res.
7450 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7452 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7457 return isl_stat_error
;
7459 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7460 if (data
->pos
>= n_out
) {
7461 isl_pw_multi_aff_free(pma
);
7465 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7466 isl_pw_multi_aff_free(pma
);
7468 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7470 return data
->res
? isl_stat_ok
: isl_stat_error
;
7473 /* Extract an isl_union_pw_aff corresponding to
7474 * output dimension "pos" of "upma".
7476 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7477 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7479 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7486 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7487 "cannot extract at negative position", return NULL
);
7489 space
= isl_union_pw_multi_aff_get_space(upma
);
7490 data
.res
= isl_union_pw_aff_empty(space
);
7492 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7493 &get_union_pw_aff
, &data
) < 0)
7494 data
.res
= isl_union_pw_aff_free(data
.res
);
7499 /* Return a union piecewise affine expression
7500 * that is equal to "aff" on "domain".
7502 * Construct an isl_pw_aff on each of the sets in "domain" and
7503 * collect the results.
7505 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7506 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7508 struct isl_union_pw_aff_aff_on_domain_data data
;
7511 if (!domain
|| !aff
)
7513 if (!isl_local_space_is_params(aff
->ls
))
7514 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7515 "expecting parametric expression", goto error
);
7517 space
= isl_union_set_get_space(domain
);
7518 data
.res
= isl_union_pw_aff_empty(space
);
7520 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7521 data
.res
= isl_union_pw_aff_free(data
.res
);
7522 isl_union_set_free(domain
);
7526 isl_union_set_free(domain
);
7531 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7532 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7533 * "res" collects the results.
7535 struct isl_union_pw_aff_val_on_domain_data
{
7537 isl_union_pw_aff
*res
;
7540 /* Construct a piecewise affine expression that is equal to data->v
7541 * on "domain" and add the result to data->res.
7543 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7545 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7549 v
= isl_val_copy(data
->v
);
7550 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7551 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7553 return data
->res
? isl_stat_ok
: isl_stat_error
;
7556 /* Return a union piecewise affine expression
7557 * that is equal to "v" on "domain".
7559 * Construct an isl_pw_aff on each of the sets in "domain" and
7560 * collect the results.
7562 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7563 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7565 struct isl_union_pw_aff_val_on_domain_data data
;
7568 space
= isl_union_set_get_space(domain
);
7569 data
.res
= isl_union_pw_aff_empty(space
);
7571 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7572 data
.res
= isl_union_pw_aff_free(data
.res
);
7573 isl_union_set_free(domain
);
7578 /* Construct a piecewise multi affine expression
7579 * that is equal to "pa" and add it to upma.
7581 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7584 isl_union_pw_multi_aff
**upma
= user
;
7585 isl_pw_multi_aff
*pma
;
7587 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7588 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7590 return *upma
? isl_stat_ok
: isl_stat_error
;
7593 /* Construct and return a union piecewise multi affine expression
7594 * that is equal to the given union piecewise affine expression.
7596 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7597 __isl_take isl_union_pw_aff
*upa
)
7600 isl_union_pw_multi_aff
*upma
;
7605 space
= isl_union_pw_aff_get_space(upa
);
7606 upma
= isl_union_pw_multi_aff_empty(space
);
7608 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7609 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7610 upma
= isl_union_pw_multi_aff_free(upma
);
7612 isl_union_pw_aff_free(upa
);
7616 /* Compute the set of elements in the domain of "pa" where it is zero and
7617 * add this set to "uset".
7619 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7621 isl_union_set
**uset
= (isl_union_set
**)user
;
7623 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7625 return *uset
? isl_stat_ok
: isl_stat_error
;
7628 /* Return a union set containing those elements in the domain
7629 * of "upa" where it is zero.
7631 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7632 __isl_take isl_union_pw_aff
*upa
)
7634 isl_union_set
*zero
;
7636 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7637 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7638 zero
= isl_union_set_free(zero
);
7640 isl_union_pw_aff_free(upa
);
7644 /* Convert "pa" to an isl_map and add it to *umap.
7646 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7648 isl_union_map
**umap
= user
;
7651 map
= isl_map_from_pw_aff(pa
);
7652 *umap
= isl_union_map_add_map(*umap
, map
);
7654 return *umap
? isl_stat_ok
: isl_stat_error
;
7657 /* Construct a union map mapping the domain of the union
7658 * piecewise affine expression to its range, with the single output dimension
7659 * equated to the corresponding affine expressions on their cells.
7661 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7662 __isl_take isl_union_pw_aff
*upa
)
7665 isl_union_map
*umap
;
7670 space
= isl_union_pw_aff_get_space(upa
);
7671 umap
= isl_union_map_empty(space
);
7673 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7675 umap
= isl_union_map_free(umap
);
7677 isl_union_pw_aff_free(upa
);
7681 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7682 * upma is the function that is plugged in.
7683 * pa is the current part of the function in which upma is plugged in.
7684 * res collects the results.
7686 struct isl_union_pw_aff_pullback_upma_data
{
7687 isl_union_pw_multi_aff
*upma
;
7689 isl_union_pw_aff
*res
;
7692 /* Check if "pma" can be plugged into data->pa.
7693 * If so, perform the pullback and add the result to data->res.
7695 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7697 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7700 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7701 pma
->dim
, isl_dim_out
)) {
7702 isl_pw_multi_aff_free(pma
);
7706 pa
= isl_pw_aff_copy(data
->pa
);
7707 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7709 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7711 return data
->res
? isl_stat_ok
: isl_stat_error
;
7714 /* Check if any of the elements of data->upma can be plugged into pa,
7715 * add if so add the result to data->res.
7717 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7719 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7723 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7725 isl_pw_aff_free(pa
);
7730 /* Compute the pullback of "upa" by the function represented by "upma".
7731 * In other words, plug in "upma" in "upa". The result contains
7732 * expressions defined over the domain space of "upma".
7734 * Run over all pairs of elements in "upa" and "upma", perform
7735 * the pullback when appropriate and collect the results.
7736 * If the hash value were based on the domain space rather than
7737 * the function space, then we could run through all elements
7738 * of "upma" and directly pick out the corresponding element of "upa".
7740 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7741 __isl_take isl_union_pw_aff
*upa
,
7742 __isl_take isl_union_pw_multi_aff
*upma
)
7744 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7747 space
= isl_union_pw_multi_aff_get_space(upma
);
7748 upa
= isl_union_pw_aff_align_params(upa
, space
);
7749 space
= isl_union_pw_aff_get_space(upa
);
7750 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7756 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7757 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7758 data
.res
= isl_union_pw_aff_free(data
.res
);
7760 isl_union_pw_aff_free(upa
);
7761 isl_union_pw_multi_aff_free(upma
);
7764 isl_union_pw_aff_free(upa
);
7765 isl_union_pw_multi_aff_free(upma
);
7770 #define BASE union_pw_aff
7772 #define DOMBASE union_set
7774 #define NO_MOVE_DIMS
7783 #include <isl_multi_templ.c>
7784 #include <isl_multi_apply_set.c>
7785 #include <isl_multi_apply_union_set.c>
7786 #include <isl_multi_coalesce.c>
7787 #include <isl_multi_floor.c>
7788 #include <isl_multi_gist.c>
7789 #include <isl_multi_intersect.c>
7791 /* Construct a multiple union piecewise affine expression
7792 * in the given space with value zero in each of the output dimensions.
7794 * Since there is no canonical zero value for
7795 * a union piecewise affine expression, we can only construct
7796 * zero-dimensional "zero" value.
7798 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7799 __isl_take isl_space
*space
)
7804 if (!isl_space_is_set(space
))
7805 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7806 "expecting set space", goto error
);
7807 if (isl_space_dim(space
, isl_dim_out
) != 0)
7808 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7809 "expecting 0D space", goto error
);
7811 return isl_multi_union_pw_aff_alloc(space
);
7813 isl_space_free(space
);
7817 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7818 * with the actual sum on the shared domain and
7819 * the defined expression on the symmetric difference of the domains.
7821 * We simply iterate over the elements in both arguments and
7822 * call isl_union_pw_aff_union_add on each of them.
7824 static __isl_give isl_multi_union_pw_aff
*
7825 isl_multi_union_pw_aff_union_add_aligned(
7826 __isl_take isl_multi_union_pw_aff
*mupa1
,
7827 __isl_take isl_multi_union_pw_aff
*mupa2
)
7829 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7830 &isl_union_pw_aff_union_add
);
7833 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7834 * with the actual sum on the shared domain and
7835 * the defined expression on the symmetric difference of the domains.
7837 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7838 __isl_take isl_multi_union_pw_aff
*mupa1
,
7839 __isl_take isl_multi_union_pw_aff
*mupa2
)
7841 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7842 &isl_multi_union_pw_aff_union_add_aligned
);
7845 /* Construct and return a multi union piecewise affine expression
7846 * that is equal to the given multi affine expression.
7848 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7849 __isl_take isl_multi_aff
*ma
)
7851 isl_multi_pw_aff
*mpa
;
7853 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7854 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7857 /* Construct and return a multi union piecewise affine expression
7858 * that is equal to the given multi piecewise affine expression.
7860 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7861 __isl_take isl_multi_pw_aff
*mpa
)
7865 isl_multi_union_pw_aff
*mupa
;
7870 space
= isl_multi_pw_aff_get_space(mpa
);
7871 space
= isl_space_range(space
);
7872 mupa
= isl_multi_union_pw_aff_alloc(space
);
7874 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7875 for (i
= 0; i
< n
; ++i
) {
7877 isl_union_pw_aff
*upa
;
7879 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7880 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7881 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7884 isl_multi_pw_aff_free(mpa
);
7889 /* Extract the range space of "pma" and assign it to *space.
7890 * If *space has already been set (through a previous call to this function),
7891 * then check that the range space is the same.
7893 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7895 isl_space
**space
= user
;
7896 isl_space
*pma_space
;
7899 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7900 isl_pw_multi_aff_free(pma
);
7903 return isl_stat_error
;
7909 equal
= isl_space_is_equal(pma_space
, *space
);
7910 isl_space_free(pma_space
);
7913 return isl_stat_error
;
7915 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7916 "range spaces not the same", return isl_stat_error
);
7920 /* Construct and return a multi union piecewise affine expression
7921 * that is equal to the given union piecewise multi affine expression.
7923 * In order to be able to perform the conversion, the input
7924 * needs to be non-empty and may only involve a single range space.
7926 __isl_give isl_multi_union_pw_aff
*
7927 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7928 __isl_take isl_union_pw_multi_aff
*upma
)
7930 isl_space
*space
= NULL
;
7931 isl_multi_union_pw_aff
*mupa
;
7936 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7937 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7938 "cannot extract range space from empty input",
7940 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7947 n
= isl_space_dim(space
, isl_dim_set
);
7948 mupa
= isl_multi_union_pw_aff_alloc(space
);
7950 for (i
= 0; i
< n
; ++i
) {
7951 isl_union_pw_aff
*upa
;
7953 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7954 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7957 isl_union_pw_multi_aff_free(upma
);
7960 isl_space_free(space
);
7961 isl_union_pw_multi_aff_free(upma
);
7965 /* Try and create an isl_multi_union_pw_aff that is equivalent
7966 * to the given isl_union_map.
7967 * The isl_union_map is required to be single-valued in each space.
7968 * Moreover, it cannot be empty and all range spaces need to be the same.
7969 * Otherwise, an error is produced.
7971 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7972 __isl_take isl_union_map
*umap
)
7974 isl_union_pw_multi_aff
*upma
;
7976 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7977 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7980 /* Return a multiple union piecewise affine expression
7981 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7982 * have been aligned.
7984 static __isl_give isl_multi_union_pw_aff
*
7985 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7986 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7990 isl_multi_union_pw_aff
*mupa
;
7995 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7996 space
= isl_multi_val_get_space(mv
);
7997 mupa
= isl_multi_union_pw_aff_alloc(space
);
7998 for (i
= 0; i
< n
; ++i
) {
8000 isl_union_pw_aff
*upa
;
8002 v
= isl_multi_val_get_val(mv
, i
);
8003 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8005 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8008 isl_union_set_free(domain
);
8009 isl_multi_val_free(mv
);
8012 isl_union_set_free(domain
);
8013 isl_multi_val_free(mv
);
8017 /* Return a multiple union piecewise affine expression
8018 * that is equal to "mv" on "domain".
8020 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8021 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8025 if (isl_space_match(domain
->dim
, isl_dim_param
,
8026 mv
->space
, isl_dim_param
))
8027 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8029 domain
= isl_union_set_align_params(domain
,
8030 isl_multi_val_get_space(mv
));
8031 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8032 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8034 isl_union_set_free(domain
);
8035 isl_multi_val_free(mv
);
8039 /* Return a multiple union piecewise affine expression
8040 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8041 * have been aligned.
8043 static __isl_give isl_multi_union_pw_aff
*
8044 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8045 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8049 isl_multi_union_pw_aff
*mupa
;
8054 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8055 space
= isl_multi_aff_get_space(ma
);
8056 mupa
= isl_multi_union_pw_aff_alloc(space
);
8057 for (i
= 0; i
< n
; ++i
) {
8059 isl_union_pw_aff
*upa
;
8061 aff
= isl_multi_aff_get_aff(ma
, i
);
8062 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8064 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8067 isl_union_set_free(domain
);
8068 isl_multi_aff_free(ma
);
8071 isl_union_set_free(domain
);
8072 isl_multi_aff_free(ma
);
8076 /* Return a multiple union piecewise affine expression
8077 * that is equal to "ma" on "domain".
8079 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8080 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8084 if (isl_space_match(domain
->dim
, isl_dim_param
,
8085 ma
->space
, isl_dim_param
))
8086 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8088 domain
= isl_union_set_align_params(domain
,
8089 isl_multi_aff_get_space(ma
));
8090 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8091 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8093 isl_union_set_free(domain
);
8094 isl_multi_aff_free(ma
);
8098 /* Return a union set containing those elements in the domains
8099 * of the elements of "mupa" where they are all zero.
8101 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8102 __isl_take isl_multi_union_pw_aff
*mupa
)
8105 isl_union_pw_aff
*upa
;
8106 isl_union_set
*zero
;
8111 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8113 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8114 "cannot determine zero set "
8115 "of zero-dimensional function", goto error
);
8117 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8118 zero
= isl_union_pw_aff_zero_union_set(upa
);
8120 for (i
= 1; i
< n
; ++i
) {
8121 isl_union_set
*zero_i
;
8123 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8124 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8126 zero
= isl_union_set_intersect(zero
, zero_i
);
8129 isl_multi_union_pw_aff_free(mupa
);
8132 isl_multi_union_pw_aff_free(mupa
);
8136 /* Construct a union map mapping the shared domain
8137 * of the union piecewise affine expressions to the range of "mupa"
8138 * with each dimension in the range equated to the
8139 * corresponding union piecewise affine expression.
8141 * The input cannot be zero-dimensional as there is
8142 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8144 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8145 __isl_take isl_multi_union_pw_aff
*mupa
)
8149 isl_union_map
*umap
;
8150 isl_union_pw_aff
*upa
;
8155 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8157 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8158 "cannot determine domain of zero-dimensional "
8159 "isl_multi_union_pw_aff", goto error
);
8161 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8162 umap
= isl_union_map_from_union_pw_aff(upa
);
8164 for (i
= 1; i
< n
; ++i
) {
8165 isl_union_map
*umap_i
;
8167 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8168 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8169 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8172 space
= isl_multi_union_pw_aff_get_space(mupa
);
8173 umap
= isl_union_map_reset_range_space(umap
, space
);
8175 isl_multi_union_pw_aff_free(mupa
);
8178 isl_multi_union_pw_aff_free(mupa
);
8182 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8183 * "range" is the space from which to set the range space.
8184 * "res" collects the results.
8186 struct isl_union_pw_multi_aff_reset_range_space_data
{
8188 isl_union_pw_multi_aff
*res
;
8191 /* Replace the range space of "pma" by the range space of data->range and
8192 * add the result to data->res.
8194 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8196 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8199 space
= isl_pw_multi_aff_get_space(pma
);
8200 space
= isl_space_domain(space
);
8201 space
= isl_space_extend_domain_with_range(space
,
8202 isl_space_copy(data
->range
));
8203 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8204 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8206 return data
->res
? isl_stat_ok
: isl_stat_error
;
8209 /* Replace the range space of all the piecewise affine expressions in "upma" by
8210 * the range space of "space".
8212 * This assumes that all these expressions have the same output dimension.
8214 * Since the spaces of the expressions change, so do their hash values.
8215 * We therefore need to create a new isl_union_pw_multi_aff.
8216 * Note that the hash value is currently computed based on the entire
8217 * space even though there can only be a single expression with a given
8220 static __isl_give isl_union_pw_multi_aff
*
8221 isl_union_pw_multi_aff_reset_range_space(
8222 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8224 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8225 isl_space
*space_upma
;
8227 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8228 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8229 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8230 &reset_range_space
, &data
) < 0)
8231 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8233 isl_space_free(space
);
8234 isl_union_pw_multi_aff_free(upma
);
8238 /* Construct and return a union piecewise multi affine expression
8239 * that is equal to the given multi union piecewise affine expression.
8241 * In order to be able to perform the conversion, the input
8242 * needs to have a least one output dimension.
8244 __isl_give isl_union_pw_multi_aff
*
8245 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8246 __isl_take isl_multi_union_pw_aff
*mupa
)
8250 isl_union_pw_multi_aff
*upma
;
8251 isl_union_pw_aff
*upa
;
8256 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8258 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8259 "cannot determine domain of zero-dimensional "
8260 "isl_multi_union_pw_aff", goto error
);
8262 space
= isl_multi_union_pw_aff_get_space(mupa
);
8263 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8264 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8266 for (i
= 1; i
< n
; ++i
) {
8267 isl_union_pw_multi_aff
*upma_i
;
8269 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8270 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8271 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8274 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8276 isl_multi_union_pw_aff_free(mupa
);
8279 isl_multi_union_pw_aff_free(mupa
);
8283 /* Intersect the range of "mupa" with "range".
8284 * That is, keep only those domain elements that have a function value
8287 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8288 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8290 isl_union_pw_multi_aff
*upma
;
8291 isl_union_set
*domain
;
8296 if (!mupa
|| !range
)
8299 space
= isl_set_get_space(range
);
8300 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8301 space
, isl_dim_set
);
8302 isl_space_free(space
);
8306 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8307 "space don't match", goto error
);
8308 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8310 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8311 "cannot intersect range of zero-dimensional "
8312 "isl_multi_union_pw_aff", goto error
);
8314 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8315 isl_multi_union_pw_aff_copy(mupa
));
8316 domain
= isl_union_set_from_set(range
);
8317 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8318 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8322 isl_multi_union_pw_aff_free(mupa
);
8323 isl_set_free(range
);
8327 /* Return the shared domain of the elements of "mupa".
8329 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8330 __isl_take isl_multi_union_pw_aff
*mupa
)
8333 isl_union_pw_aff
*upa
;
8339 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8341 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8342 "cannot determine domain", goto error
);
8344 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8345 dom
= isl_union_pw_aff_domain(upa
);
8346 for (i
= 1; i
< n
; ++i
) {
8347 isl_union_set
*dom_i
;
8349 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8350 dom_i
= isl_union_pw_aff_domain(upa
);
8351 dom
= isl_union_set_intersect(dom
, dom_i
);
8354 isl_multi_union_pw_aff_free(mupa
);
8357 isl_multi_union_pw_aff_free(mupa
);
8361 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8362 * In particular, the spaces have been aligned.
8363 * The result is defined over the shared domain of the elements of "mupa"
8365 * We first extract the parametric constant part of "aff" and
8366 * define that over the shared domain.
8367 * Then we iterate over all input dimensions of "aff" and add the corresponding
8368 * multiples of the elements of "mupa".
8369 * Finally, we consider the integer divisions, calling the function
8370 * recursively to obtain an isl_union_pw_aff corresponding to the
8371 * integer division argument.
8373 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8374 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8377 isl_union_pw_aff
*upa
;
8378 isl_union_set
*uset
;
8382 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8383 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8385 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8386 cst
= isl_aff_copy(aff
);
8387 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8388 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8389 cst
= isl_aff_project_domain_on_params(cst
);
8390 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8392 for (i
= 0; i
< n_in
; ++i
) {
8393 isl_union_pw_aff
*upa_i
;
8395 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8397 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8398 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8399 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8400 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8403 for (i
= 0; i
< n_div
; ++i
) {
8405 isl_union_pw_aff
*upa_i
;
8407 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8409 div
= isl_aff_get_div(aff
, i
);
8410 upa_i
= multi_union_pw_aff_apply_aff(
8411 isl_multi_union_pw_aff_copy(mupa
), div
);
8412 upa_i
= isl_union_pw_aff_floor(upa_i
);
8413 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8414 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8415 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8418 isl_multi_union_pw_aff_free(mupa
);
8424 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8425 * with the domain of "aff".
8426 * Furthermore, the dimension of this space needs to be greater than zero.
8427 * The result is defined over the shared domain of the elements of "mupa"
8429 * We perform these checks and then hand over control to
8430 * multi_union_pw_aff_apply_aff.
8432 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8433 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8435 isl_space
*space1
, *space2
;
8438 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8439 isl_aff_get_space(aff
));
8440 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8444 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8445 space2
= isl_aff_get_domain_space(aff
);
8446 equal
= isl_space_is_equal(space1
, space2
);
8447 isl_space_free(space1
);
8448 isl_space_free(space2
);
8452 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8453 "spaces don't match", goto error
);
8454 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8455 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8456 "cannot determine domains", goto error
);
8458 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8460 isl_multi_union_pw_aff_free(mupa
);
8465 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8466 * with the domain of "ma".
8467 * Furthermore, the dimension of this space needs to be greater than zero,
8468 * unless the dimension of the target space of "ma" is also zero.
8469 * The result is defined over the shared domain of the elements of "mupa"
8471 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8472 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8474 isl_space
*space1
, *space2
;
8475 isl_multi_union_pw_aff
*res
;
8479 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8480 isl_multi_aff_get_space(ma
));
8481 ma
= isl_multi_aff_align_params(ma
,
8482 isl_multi_union_pw_aff_get_space(mupa
));
8486 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8487 space2
= isl_multi_aff_get_domain_space(ma
);
8488 equal
= isl_space_is_equal(space1
, space2
);
8489 isl_space_free(space1
);
8490 isl_space_free(space2
);
8494 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8495 "spaces don't match", goto error
);
8496 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8497 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8498 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8499 "cannot determine domains", goto error
);
8501 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8502 res
= isl_multi_union_pw_aff_alloc(space1
);
8504 for (i
= 0; i
< n_out
; ++i
) {
8506 isl_union_pw_aff
*upa
;
8508 aff
= isl_multi_aff_get_aff(ma
, i
);
8509 upa
= multi_union_pw_aff_apply_aff(
8510 isl_multi_union_pw_aff_copy(mupa
), aff
);
8511 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8514 isl_multi_aff_free(ma
);
8515 isl_multi_union_pw_aff_free(mupa
);
8518 isl_multi_union_pw_aff_free(mupa
);
8519 isl_multi_aff_free(ma
);
8523 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8524 * with the domain of "pa".
8525 * Furthermore, the dimension of this space needs to be greater than zero.
8526 * The result is defined over the shared domain of the elements of "mupa"
8528 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8529 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8533 isl_space
*space
, *space2
;
8534 isl_union_pw_aff
*upa
;
8536 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8537 isl_pw_aff_get_space(pa
));
8538 pa
= isl_pw_aff_align_params(pa
,
8539 isl_multi_union_pw_aff_get_space(mupa
));
8543 space
= isl_multi_union_pw_aff_get_space(mupa
);
8544 space2
= isl_pw_aff_get_domain_space(pa
);
8545 equal
= isl_space_is_equal(space
, space2
);
8546 isl_space_free(space
);
8547 isl_space_free(space2
);
8551 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8552 "spaces don't match", goto error
);
8553 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8554 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8555 "cannot determine domains", goto error
);
8557 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8558 upa
= isl_union_pw_aff_empty(space
);
8560 for (i
= 0; i
< pa
->n
; ++i
) {
8563 isl_multi_union_pw_aff
*mupa_i
;
8564 isl_union_pw_aff
*upa_i
;
8566 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8567 domain
= isl_set_copy(pa
->p
[i
].set
);
8568 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8569 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8570 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8571 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8574 isl_multi_union_pw_aff_free(mupa
);
8575 isl_pw_aff_free(pa
);
8578 isl_multi_union_pw_aff_free(mupa
);
8579 isl_pw_aff_free(pa
);
8583 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8584 * with the domain of "pma".
8585 * Furthermore, the dimension of this space needs to be greater than zero,
8586 * unless the dimension of the target space of "pma" is also zero.
8587 * The result is defined over the shared domain of the elements of "mupa"
8589 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8590 __isl_take isl_multi_union_pw_aff
*mupa
,
8591 __isl_take isl_pw_multi_aff
*pma
)
8593 isl_space
*space1
, *space2
;
8594 isl_multi_union_pw_aff
*res
;
8598 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8599 isl_pw_multi_aff_get_space(pma
));
8600 pma
= isl_pw_multi_aff_align_params(pma
,
8601 isl_multi_union_pw_aff_get_space(mupa
));
8605 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8606 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8607 equal
= isl_space_is_equal(space1
, space2
);
8608 isl_space_free(space1
);
8609 isl_space_free(space2
);
8613 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8614 "spaces don't match", goto error
);
8615 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8616 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8617 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8618 "cannot determine domains", goto error
);
8620 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8621 res
= isl_multi_union_pw_aff_alloc(space1
);
8623 for (i
= 0; i
< n_out
; ++i
) {
8625 isl_union_pw_aff
*upa
;
8627 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8628 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8629 isl_multi_union_pw_aff_copy(mupa
), pa
);
8630 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8633 isl_pw_multi_aff_free(pma
);
8634 isl_multi_union_pw_aff_free(mupa
);
8637 isl_multi_union_pw_aff_free(mupa
);
8638 isl_pw_multi_aff_free(pma
);
8642 /* Compute the pullback of "mupa" by the function represented by "upma".
8643 * In other words, plug in "upma" in "mupa". The result contains
8644 * expressions defined over the domain space of "upma".
8646 * Run over all elements of "mupa" and plug in "upma" in each of them.
8648 __isl_give isl_multi_union_pw_aff
*
8649 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8650 __isl_take isl_multi_union_pw_aff
*mupa
,
8651 __isl_take isl_union_pw_multi_aff
*upma
)
8655 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8656 isl_union_pw_multi_aff_get_space(upma
));
8657 upma
= isl_union_pw_multi_aff_align_params(upma
,
8658 isl_multi_union_pw_aff_get_space(mupa
));
8662 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8663 for (i
= 0; i
< n
; ++i
) {
8664 isl_union_pw_aff
*upa
;
8666 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8667 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8668 isl_union_pw_multi_aff_copy(upma
));
8669 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8672 isl_union_pw_multi_aff_free(upma
);
8675 isl_multi_union_pw_aff_free(mupa
);
8676 isl_union_pw_multi_aff_free(upma
);
8680 /* Extract the sequence of elements in "mupa" with domain space "space"
8681 * (ignoring parameters).
8683 * For the elements of "mupa" that are not defined on the specified space,
8684 * the corresponding element in the result is empty.
8686 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8687 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8690 isl_space
*space_mpa
= NULL
;
8691 isl_multi_pw_aff
*mpa
;
8693 if (!mupa
|| !space
)
8696 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8697 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8698 space
= isl_space_drop_dims(space
, isl_dim_param
,
8699 0, isl_space_dim(space
, isl_dim_param
));
8700 space
= isl_space_align_params(space
,
8701 isl_space_copy(space_mpa
));
8705 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8707 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8709 space
= isl_space_from_domain(space
);
8710 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8711 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8712 for (i
= 0; i
< n
; ++i
) {
8713 isl_union_pw_aff
*upa
;
8716 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8717 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8718 isl_space_copy(space
));
8719 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8720 isl_union_pw_aff_free(upa
);
8723 isl_space_free(space
);
8726 isl_space_free(space_mpa
);
8727 isl_space_free(space
);