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 * reduce_div_coefficients performs the same normalization.
1534 * As a special case, floor(NaN) = NaN.
1536 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1546 if (isl_aff_is_nan(aff
))
1548 if (isl_int_is_one(aff
->v
->el
[0]))
1551 aff
= isl_aff_cow(aff
);
1555 aff
->v
= isl_vec_cow(aff
->v
);
1557 return isl_aff_free(aff
);
1559 if (isl_aff_is_cst(aff
)) {
1560 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1561 isl_int_set_si(aff
->v
->el
[0], 1);
1565 div
= isl_vec_copy(aff
->v
);
1566 div
= isl_vec_cow(div
);
1568 return isl_aff_free(aff
);
1570 ctx
= isl_aff_get_ctx(aff
);
1571 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1572 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1573 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1574 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1575 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1576 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1577 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1581 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1583 return isl_aff_free(aff
);
1585 size
= aff
->v
->size
;
1586 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1588 return isl_aff_free(aff
);
1589 isl_int_set_si(aff
->v
->el
[0], 1);
1590 isl_int_set_si(aff
->v
->el
[size
], 1);
1592 aff
= isl_aff_normalize(aff
);
1599 * aff mod m = aff - m * floor(aff/m)
1601 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1605 res
= isl_aff_copy(aff
);
1606 aff
= isl_aff_scale_down(aff
, m
);
1607 aff
= isl_aff_floor(aff
);
1608 aff
= isl_aff_scale(aff
, m
);
1609 res
= isl_aff_sub(res
, aff
);
1616 * aff mod m = aff - m * floor(aff/m)
1618 * with m an integer value.
1620 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1621 __isl_take isl_val
*m
)
1628 if (!isl_val_is_int(m
))
1629 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1630 "expecting integer modulo", goto error
);
1632 res
= isl_aff_copy(aff
);
1633 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1634 aff
= isl_aff_floor(aff
);
1635 aff
= isl_aff_scale_val(aff
, m
);
1636 res
= isl_aff_sub(res
, aff
);
1647 * pwaff mod m = pwaff - m * floor(pwaff/m)
1649 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1653 res
= isl_pw_aff_copy(pwaff
);
1654 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1655 pwaff
= isl_pw_aff_floor(pwaff
);
1656 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1657 res
= isl_pw_aff_sub(res
, pwaff
);
1664 * pa mod m = pa - m * floor(pa/m)
1666 * with m an integer value.
1668 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1669 __isl_take isl_val
*m
)
1673 if (!isl_val_is_int(m
))
1674 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1675 "expecting integer modulo", goto error
);
1676 pa
= isl_pw_aff_mod(pa
, m
->n
);
1680 isl_pw_aff_free(pa
);
1685 /* Given f, return ceil(f).
1686 * If f is an integer expression, then just return f.
1687 * Otherwise, let f be the expression
1693 * floor((e + m - 1)/m)
1695 * As a special case, ceil(NaN) = NaN.
1697 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1702 if (isl_aff_is_nan(aff
))
1704 if (isl_int_is_one(aff
->v
->el
[0]))
1707 aff
= isl_aff_cow(aff
);
1710 aff
->v
= isl_vec_cow(aff
->v
);
1712 return isl_aff_free(aff
);
1714 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1715 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1716 aff
= isl_aff_floor(aff
);
1721 /* Apply the expansion computed by isl_merge_divs.
1722 * The expansion itself is given by "exp" while the resulting
1723 * list of divs is given by "div".
1725 __isl_give isl_aff
*isl_aff_expand_divs(__isl_take isl_aff
*aff
,
1726 __isl_take isl_mat
*div
, int *exp
)
1732 aff
= isl_aff_cow(aff
);
1736 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1737 new_n_div
= isl_mat_rows(div
);
1738 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1740 aff
->v
= isl_vec_expand(aff
->v
, offset
, old_n_div
, exp
, new_n_div
);
1741 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, div
);
1742 if (!aff
->v
|| !aff
->ls
)
1743 return isl_aff_free(aff
);
1751 /* Add two affine expressions that live in the same local space.
1753 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1754 __isl_take isl_aff
*aff2
)
1758 aff1
= isl_aff_cow(aff1
);
1762 aff1
->v
= isl_vec_cow(aff1
->v
);
1768 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1769 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1770 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1771 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1772 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1773 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1774 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1786 /* Return the sum of "aff1" and "aff2".
1788 * If either of the two is NaN, then the result is NaN.
1790 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1791 __isl_take isl_aff
*aff2
)
1802 ctx
= isl_aff_get_ctx(aff1
);
1803 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1804 isl_die(ctx
, isl_error_invalid
,
1805 "spaces don't match", goto error
);
1807 if (isl_aff_is_nan(aff1
)) {
1811 if (isl_aff_is_nan(aff2
)) {
1816 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1817 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1818 if (n_div1
== 0 && n_div2
== 0)
1819 return add_expanded(aff1
, aff2
);
1821 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1822 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1823 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1826 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1827 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1828 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1832 return add_expanded(aff1
, aff2
);
1841 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1842 __isl_take isl_aff
*aff2
)
1844 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1847 /* Return the result of scaling "aff" by a factor of "f".
1849 * As a special case, f * NaN = NaN.
1851 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1857 if (isl_aff_is_nan(aff
))
1860 if (isl_int_is_one(f
))
1863 aff
= isl_aff_cow(aff
);
1866 aff
->v
= isl_vec_cow(aff
->v
);
1868 return isl_aff_free(aff
);
1870 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1871 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1876 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1877 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1878 isl_int_divexact(gcd
, f
, gcd
);
1879 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1885 /* Multiple "aff" by "v".
1887 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1888 __isl_take isl_val
*v
)
1893 if (isl_val_is_one(v
)) {
1898 if (!isl_val_is_rat(v
))
1899 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1900 "expecting rational factor", goto error
);
1902 aff
= isl_aff_scale(aff
, v
->n
);
1903 aff
= isl_aff_scale_down(aff
, v
->d
);
1913 /* Return the result of scaling "aff" down by a factor of "f".
1915 * As a special case, NaN/f = NaN.
1917 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1923 if (isl_aff_is_nan(aff
))
1926 if (isl_int_is_one(f
))
1929 aff
= isl_aff_cow(aff
);
1933 if (isl_int_is_zero(f
))
1934 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1935 "cannot scale down by zero", return isl_aff_free(aff
));
1937 aff
->v
= isl_vec_cow(aff
->v
);
1939 return isl_aff_free(aff
);
1942 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1943 isl_int_gcd(gcd
, gcd
, f
);
1944 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1945 isl_int_divexact(gcd
, f
, gcd
);
1946 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1952 /* Divide "aff" by "v".
1954 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1955 __isl_take isl_val
*v
)
1960 if (isl_val_is_one(v
)) {
1965 if (!isl_val_is_rat(v
))
1966 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1967 "expecting rational factor", goto error
);
1968 if (!isl_val_is_pos(v
))
1969 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1970 "factor needs to be positive", goto error
);
1972 aff
= isl_aff_scale(aff
, v
->d
);
1973 aff
= isl_aff_scale_down(aff
, v
->n
);
1983 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1991 isl_int_set_ui(v
, f
);
1992 aff
= isl_aff_scale_down(aff
, v
);
1998 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1999 enum isl_dim_type type
, unsigned pos
, const char *s
)
2001 aff
= isl_aff_cow(aff
);
2004 if (type
== isl_dim_out
)
2005 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2006 "cannot set name of output/set dimension",
2007 return isl_aff_free(aff
));
2008 if (type
== isl_dim_in
)
2010 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2012 return isl_aff_free(aff
);
2017 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2018 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2020 aff
= isl_aff_cow(aff
);
2023 if (type
== isl_dim_out
)
2024 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2025 "cannot set name of output/set dimension",
2027 if (type
== isl_dim_in
)
2029 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2031 return isl_aff_free(aff
);
2040 /* Replace the identifier of the input tuple of "aff" by "id".
2041 * type is currently required to be equal to isl_dim_in
2043 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2044 enum isl_dim_type type
, __isl_take isl_id
*id
)
2046 aff
= isl_aff_cow(aff
);
2049 if (type
!= isl_dim_out
)
2050 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2051 "cannot only set id of input tuple", goto error
);
2052 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2054 return isl_aff_free(aff
);
2063 /* Exploit the equalities in "eq" to simplify the affine expression
2064 * and the expressions of the integer divisions in the local space.
2065 * The integer divisions in this local space are assumed to appear
2066 * as regular dimensions in "eq".
2068 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2069 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2077 if (eq
->n_eq
== 0) {
2078 isl_basic_set_free(eq
);
2082 aff
= isl_aff_cow(aff
);
2086 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2087 isl_basic_set_copy(eq
));
2088 aff
->v
= isl_vec_cow(aff
->v
);
2089 if (!aff
->ls
|| !aff
->v
)
2092 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2094 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2095 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2096 if (j
< 0 || j
== 0 || j
>= total
)
2099 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2103 isl_basic_set_free(eq
);
2104 aff
= isl_aff_normalize(aff
);
2107 isl_basic_set_free(eq
);
2112 /* Exploit the equalities in "eq" to simplify the affine expression
2113 * and the expressions of the integer divisions in the local space.
2115 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2116 __isl_take isl_basic_set
*eq
)
2122 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2124 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2125 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2127 isl_basic_set_free(eq
);
2132 /* Look for equalities among the variables shared by context and aff
2133 * and the integer divisions of aff, if any.
2134 * The equalities are then used to eliminate coefficients and/or integer
2135 * divisions from aff.
2137 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2138 __isl_take isl_set
*context
)
2140 isl_basic_set
*hull
;
2145 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2147 isl_basic_set
*bset
;
2148 isl_local_space
*ls
;
2149 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2150 ls
= isl_aff_get_domain_local_space(aff
);
2151 bset
= isl_basic_set_from_local_space(ls
);
2152 bset
= isl_basic_set_lift(bset
);
2153 bset
= isl_basic_set_flatten(bset
);
2154 context
= isl_set_intersect(context
,
2155 isl_set_from_basic_set(bset
));
2158 hull
= isl_set_affine_hull(context
);
2159 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2162 isl_set_free(context
);
2166 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2167 __isl_take isl_set
*context
)
2169 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2170 dom_context
= isl_set_intersect_params(dom_context
, context
);
2171 return isl_aff_gist(aff
, dom_context
);
2174 /* Return a basic set containing those elements in the space
2175 * of aff where it is positive. "rational" should not be set.
2177 * If "aff" is NaN, then it is not positive.
2179 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2182 isl_constraint
*ineq
;
2183 isl_basic_set
*bset
;
2188 if (isl_aff_is_nan(aff
)) {
2189 isl_space
*space
= isl_aff_get_domain_space(aff
);
2191 return isl_basic_set_empty(space
);
2194 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2195 "rational sets not supported", goto error
);
2197 ineq
= isl_inequality_from_aff(aff
);
2198 c
= isl_constraint_get_constant_val(ineq
);
2199 c
= isl_val_sub_ui(c
, 1);
2200 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2202 bset
= isl_basic_set_from_constraint(ineq
);
2203 bset
= isl_basic_set_simplify(bset
);
2210 /* Return a basic set containing those elements in the space
2211 * of aff where it is non-negative.
2212 * If "rational" is set, then return a rational basic set.
2214 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2216 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2217 __isl_take isl_aff
*aff
, int rational
)
2219 isl_constraint
*ineq
;
2220 isl_basic_set
*bset
;
2224 if (isl_aff_is_nan(aff
)) {
2225 isl_space
*space
= isl_aff_get_domain_space(aff
);
2227 return isl_basic_set_empty(space
);
2230 ineq
= isl_inequality_from_aff(aff
);
2232 bset
= isl_basic_set_from_constraint(ineq
);
2234 bset
= isl_basic_set_set_rational(bset
);
2235 bset
= isl_basic_set_simplify(bset
);
2239 /* Return a basic set containing those elements in the space
2240 * of aff where it is non-negative.
2242 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2244 return aff_nonneg_basic_set(aff
, 0);
2247 /* Return a basic set containing those elements in the domain space
2248 * of aff where it is negative.
2250 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2252 aff
= isl_aff_neg(aff
);
2253 aff
= isl_aff_add_constant_num_si(aff
, -1);
2254 return isl_aff_nonneg_basic_set(aff
);
2257 /* Return a basic set containing those elements in the space
2258 * of aff where it is zero.
2259 * If "rational" is set, then return a rational basic set.
2261 * If "aff" is NaN, then it is not zero.
2263 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2266 isl_constraint
*ineq
;
2267 isl_basic_set
*bset
;
2271 if (isl_aff_is_nan(aff
)) {
2272 isl_space
*space
= isl_aff_get_domain_space(aff
);
2274 return isl_basic_set_empty(space
);
2277 ineq
= isl_equality_from_aff(aff
);
2279 bset
= isl_basic_set_from_constraint(ineq
);
2281 bset
= isl_basic_set_set_rational(bset
);
2282 bset
= isl_basic_set_simplify(bset
);
2286 /* Return a basic set containing those elements in the space
2287 * of aff where it is zero.
2289 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2291 return aff_zero_basic_set(aff
, 0);
2294 /* Return a basic set containing those elements in the shared space
2295 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2297 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2298 __isl_take isl_aff
*aff2
)
2300 aff1
= isl_aff_sub(aff1
, aff2
);
2302 return isl_aff_nonneg_basic_set(aff1
);
2305 /* Return a set containing those elements in the shared space
2306 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2308 __isl_give isl_set
*isl_aff_ge_set(__isl_take isl_aff
*aff1
,
2309 __isl_take isl_aff
*aff2
)
2311 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1
, aff2
));
2314 /* Return a basic set containing those elements in the shared space
2315 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2317 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2318 __isl_take isl_aff
*aff2
)
2320 return isl_aff_ge_basic_set(aff2
, aff1
);
2323 /* Return a set containing those elements in the shared space
2324 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2326 __isl_give isl_set
*isl_aff_le_set(__isl_take isl_aff
*aff1
,
2327 __isl_take isl_aff
*aff2
)
2329 return isl_aff_ge_set(aff2
, aff1
);
2332 /* Return a basic set containing those elements in the shared space
2333 * of aff1 and aff2 where aff1 and aff2 are equal.
2335 __isl_give isl_basic_set
*isl_aff_eq_basic_set(__isl_take isl_aff
*aff1
,
2336 __isl_take isl_aff
*aff2
)
2338 aff1
= isl_aff_sub(aff1
, aff2
);
2340 return isl_aff_zero_basic_set(aff1
);
2343 /* Return a set containing those elements in the shared space
2344 * of aff1 and aff2 where aff1 and aff2 are equal.
2346 __isl_give isl_set
*isl_aff_eq_set(__isl_take isl_aff
*aff1
,
2347 __isl_take isl_aff
*aff2
)
2349 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1
, aff2
));
2352 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2353 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2355 aff1
= isl_aff_add(aff1
, aff2
);
2356 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2360 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2368 /* Check whether the given affine expression has non-zero coefficient
2369 * for any dimension in the given range or if any of these dimensions
2370 * appear with non-zero coefficients in any of the integer divisions
2371 * involved in the affine expression.
2373 isl_bool
isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2374 enum isl_dim_type type
, unsigned first
, unsigned n
)
2379 isl_bool involves
= isl_bool_false
;
2382 return isl_bool_error
;
2384 return isl_bool_false
;
2386 ctx
= isl_aff_get_ctx(aff
);
2387 if (first
+ n
> isl_aff_dim(aff
, type
))
2388 isl_die(ctx
, isl_error_invalid
,
2389 "range out of bounds", return isl_bool_error
);
2391 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2395 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2396 for (i
= 0; i
< n
; ++i
)
2397 if (active
[first
+ i
]) {
2398 involves
= isl_bool_true
;
2407 return isl_bool_error
;
2410 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2411 enum isl_dim_type type
, unsigned first
, unsigned n
)
2417 if (type
== isl_dim_out
)
2418 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2419 "cannot drop output/set dimension",
2420 return isl_aff_free(aff
));
2421 if (type
== isl_dim_in
)
2423 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2426 ctx
= isl_aff_get_ctx(aff
);
2427 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2428 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2429 return isl_aff_free(aff
));
2431 aff
= isl_aff_cow(aff
);
2435 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2437 return isl_aff_free(aff
);
2439 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2440 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2442 return isl_aff_free(aff
);
2447 /* Project the domain of the affine expression onto its parameter space.
2448 * The affine expression may not involve any of the domain dimensions.
2450 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2456 n
= isl_aff_dim(aff
, isl_dim_in
);
2457 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2459 return isl_aff_free(aff
);
2461 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2462 "affine expression involves some of the domain dimensions",
2463 return isl_aff_free(aff
));
2464 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2465 space
= isl_aff_get_domain_space(aff
);
2466 space
= isl_space_params(space
);
2467 aff
= isl_aff_reset_domain_space(aff
, space
);
2471 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2472 enum isl_dim_type type
, unsigned first
, unsigned n
)
2478 if (type
== isl_dim_out
)
2479 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2480 "cannot insert output/set dimensions",
2481 return isl_aff_free(aff
));
2482 if (type
== isl_dim_in
)
2484 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2487 ctx
= isl_aff_get_ctx(aff
);
2488 if (first
> isl_local_space_dim(aff
->ls
, type
))
2489 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2490 return isl_aff_free(aff
));
2492 aff
= isl_aff_cow(aff
);
2496 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2498 return isl_aff_free(aff
);
2500 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2501 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2503 return isl_aff_free(aff
);
2508 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2509 enum isl_dim_type type
, unsigned n
)
2513 pos
= isl_aff_dim(aff
, type
);
2515 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2518 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2519 enum isl_dim_type type
, unsigned n
)
2523 pos
= isl_pw_aff_dim(pwaff
, type
);
2525 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2528 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2529 * to dimensions of "dst_type" at "dst_pos".
2531 * We only support moving input dimensions to parameters and vice versa.
2533 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2534 enum isl_dim_type dst_type
, unsigned dst_pos
,
2535 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2543 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2544 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2547 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2548 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2549 "cannot move output/set dimension",
2550 return isl_aff_free(aff
));
2551 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2552 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2553 "cannot move divs", return isl_aff_free(aff
));
2554 if (dst_type
== isl_dim_in
)
2555 dst_type
= isl_dim_set
;
2556 if (src_type
== isl_dim_in
)
2557 src_type
= isl_dim_set
;
2559 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2560 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2561 "range out of bounds", return isl_aff_free(aff
));
2562 if (dst_type
== src_type
)
2563 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2564 "moving dims within the same type not supported",
2565 return isl_aff_free(aff
));
2567 aff
= isl_aff_cow(aff
);
2571 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2572 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2573 if (dst_type
> src_type
)
2576 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2577 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2578 src_type
, src_pos
, n
);
2579 if (!aff
->v
|| !aff
->ls
)
2580 return isl_aff_free(aff
);
2582 aff
= sort_divs(aff
);
2587 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2589 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2590 return isl_pw_aff_alloc(dom
, aff
);
2594 #define PW isl_pw_aff
2598 #define EL_IS_ZERO is_empty
2602 #define IS_ZERO is_empty
2605 #undef DEFAULT_IS_ZERO
2606 #define DEFAULT_IS_ZERO 0
2613 #include <isl_pw_templ.c>
2614 #include <isl_pw_hash.c>
2615 #include <isl_pw_union_opt.c>
2618 #define UNION isl_union_pw_aff
2620 #define PART isl_pw_aff
2622 #define PARTS pw_aff
2624 #include <isl_union_single.c>
2625 #include <isl_union_neg.c>
2627 static __isl_give isl_set
*align_params_pw_pw_set_and(
2628 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2629 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2630 __isl_take isl_pw_aff
*pwaff2
))
2632 if (!pwaff1
|| !pwaff2
)
2634 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2635 pwaff2
->dim
, isl_dim_param
))
2636 return fn(pwaff1
, pwaff2
);
2637 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2638 !isl_space_has_named_params(pwaff2
->dim
))
2639 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2640 "unaligned unnamed parameters", goto error
);
2641 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2642 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2643 return fn(pwaff1
, pwaff2
);
2645 isl_pw_aff_free(pwaff1
);
2646 isl_pw_aff_free(pwaff2
);
2650 /* Align the parameters of the to isl_pw_aff arguments and
2651 * then apply a function "fn" on them that returns an isl_map.
2653 static __isl_give isl_map
*align_params_pw_pw_map_and(
2654 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2655 __isl_give isl_map
*(*fn
)(__isl_take isl_pw_aff
*pa1
,
2656 __isl_take isl_pw_aff
*pa2
))
2660 if (isl_space_match(pa1
->dim
, isl_dim_param
, pa2
->dim
, isl_dim_param
))
2661 return fn(pa1
, pa2
);
2662 if (!isl_space_has_named_params(pa1
->dim
) ||
2663 !isl_space_has_named_params(pa2
->dim
))
2664 isl_die(isl_pw_aff_get_ctx(pa1
), isl_error_invalid
,
2665 "unaligned unnamed parameters", goto error
);
2666 pa1
= isl_pw_aff_align_params(pa1
, isl_pw_aff_get_space(pa2
));
2667 pa2
= isl_pw_aff_align_params(pa2
, isl_pw_aff_get_space(pa1
));
2668 return fn(pa1
, pa2
);
2670 isl_pw_aff_free(pa1
);
2671 isl_pw_aff_free(pa2
);
2675 /* Compute a piecewise quasi-affine expression with a domain that
2676 * is the union of those of pwaff1 and pwaff2 and such that on each
2677 * cell, the quasi-affine expression is the maximum of those of pwaff1
2678 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2679 * cell, then the associated expression is the defined one.
2681 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2682 __isl_take isl_pw_aff
*pwaff2
)
2684 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_ge_set
);
2687 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2688 __isl_take isl_pw_aff
*pwaff2
)
2690 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2694 /* Compute a piecewise quasi-affine expression with a domain that
2695 * is the union of those of pwaff1 and pwaff2 and such that on each
2696 * cell, the quasi-affine expression is the minimum of those of pwaff1
2697 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2698 * cell, then the associated expression is the defined one.
2700 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2701 __isl_take isl_pw_aff
*pwaff2
)
2703 return isl_pw_aff_union_opt_cmp(pwaff1
, pwaff2
, &isl_aff_le_set
);
2706 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2707 __isl_take isl_pw_aff
*pwaff2
)
2709 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2713 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2714 __isl_take isl_pw_aff
*pwaff2
, int max
)
2717 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2719 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2722 /* Construct a map with as domain the domain of pwaff and
2723 * one-dimensional range corresponding to the affine expressions.
2725 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2734 dim
= isl_pw_aff_get_space(pwaff
);
2735 map
= isl_map_empty(dim
);
2737 for (i
= 0; i
< pwaff
->n
; ++i
) {
2738 isl_basic_map
*bmap
;
2741 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2742 map_i
= isl_map_from_basic_map(bmap
);
2743 map_i
= isl_map_intersect_domain(map_i
,
2744 isl_set_copy(pwaff
->p
[i
].set
));
2745 map
= isl_map_union_disjoint(map
, map_i
);
2748 isl_pw_aff_free(pwaff
);
2753 /* Construct a map with as domain the domain of pwaff and
2754 * one-dimensional range corresponding to the affine expressions.
2756 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2760 if (isl_space_is_set(pwaff
->dim
))
2761 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2762 "space of input is not a map", goto error
);
2763 return map_from_pw_aff(pwaff
);
2765 isl_pw_aff_free(pwaff
);
2769 /* Construct a one-dimensional set with as parameter domain
2770 * the domain of pwaff and the single set dimension
2771 * corresponding to the affine expressions.
2773 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2777 if (!isl_space_is_set(pwaff
->dim
))
2778 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2779 "space of input is not a set", goto error
);
2780 return map_from_pw_aff(pwaff
);
2782 isl_pw_aff_free(pwaff
);
2786 /* Return a set containing those elements in the domain
2787 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2788 * does not satisfy "fn" (if complement is 1).
2790 * The pieces with a NaN never belong to the result since
2791 * NaN does not satisfy any property.
2793 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2794 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2803 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2805 for (i
= 0; i
< pwaff
->n
; ++i
) {
2806 isl_basic_set
*bset
;
2807 isl_set
*set_i
, *locus
;
2810 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2813 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2814 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2815 locus
= isl_set_from_basic_set(bset
);
2816 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2818 set_i
= isl_set_subtract(set_i
, locus
);
2820 set_i
= isl_set_intersect(set_i
, locus
);
2821 set
= isl_set_union_disjoint(set
, set_i
);
2824 isl_pw_aff_free(pwaff
);
2829 /* Return a set containing those elements in the domain
2830 * of "pa" where it is positive.
2832 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2834 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2837 /* Return a set containing those elements in the domain
2838 * of pwaff where it is non-negative.
2840 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2842 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2845 /* Return a set containing those elements in the domain
2846 * of pwaff where it is zero.
2848 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2850 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2853 /* Return a set containing those elements in the domain
2854 * of pwaff where it is not zero.
2856 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2858 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2861 /* Return a set containing those elements in the shared domain
2862 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2864 * We compute the difference on the shared domain and then construct
2865 * the set of values where this difference is non-negative.
2866 * If strict is set, we first subtract 1 from the difference.
2867 * If equal is set, we only return the elements where pwaff1 and pwaff2
2870 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2871 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2873 isl_set
*set1
, *set2
;
2875 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2876 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2877 set1
= isl_set_intersect(set1
, set2
);
2878 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2879 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2880 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2883 isl_space
*dim
= isl_set_get_space(set1
);
2885 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2886 aff
= isl_aff_add_constant_si(aff
, -1);
2887 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2892 return isl_pw_aff_zero_set(pwaff1
);
2893 return isl_pw_aff_nonneg_set(pwaff1
);
2896 /* Return a set containing those elements in the shared domain
2897 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2899 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2900 __isl_take isl_pw_aff
*pwaff2
)
2902 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2905 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2906 __isl_take isl_pw_aff
*pwaff2
)
2908 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2911 /* Return a set containing those elements in the shared domain
2912 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2914 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2915 __isl_take isl_pw_aff
*pwaff2
)
2917 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2920 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2921 __isl_take isl_pw_aff
*pwaff2
)
2923 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2926 /* Return a set containing those elements in the shared domain
2927 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2929 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2930 __isl_take isl_pw_aff
*pwaff2
)
2932 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2935 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2936 __isl_take isl_pw_aff
*pwaff2
)
2938 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2941 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2942 __isl_take isl_pw_aff
*pwaff2
)
2944 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2947 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2948 __isl_take isl_pw_aff
*pwaff2
)
2950 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2953 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2954 * where the function values are ordered in the same way as "order",
2955 * which returns a set in the shared domain of its two arguments.
2956 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2958 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2959 * We first pull back the two functions such that they are defined on
2960 * the domain [A -> B]. Then we apply "order", resulting in a set
2961 * in the space [A -> B]. Finally, we unwrap this set to obtain
2962 * a map in the space A -> B.
2964 static __isl_give isl_map
*isl_pw_aff_order_map_aligned(
2965 __isl_take isl_pw_aff
*pa1
, __isl_take isl_pw_aff
*pa2
,
2966 __isl_give isl_set
*(*order
)(__isl_take isl_pw_aff
*pa1
,
2967 __isl_take isl_pw_aff
*pa2
))
2969 isl_space
*space1
, *space2
;
2973 space1
= isl_space_domain(isl_pw_aff_get_space(pa1
));
2974 space2
= isl_space_domain(isl_pw_aff_get_space(pa2
));
2975 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
2976 ma
= isl_multi_aff_domain_map(isl_space_copy(space1
));
2977 pa1
= isl_pw_aff_pullback_multi_aff(pa1
, ma
);
2978 ma
= isl_multi_aff_range_map(space1
);
2979 pa2
= isl_pw_aff_pullback_multi_aff(pa2
, ma
);
2980 set
= order(pa1
, pa2
);
2982 return isl_set_unwrap(set
);
2985 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2986 * where the function values are equal.
2987 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2989 static __isl_give isl_map
*isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff
*pa1
,
2990 __isl_take isl_pw_aff
*pa2
)
2992 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_eq_set
);
2995 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2996 * where the function values are equal.
2998 __isl_give isl_map
*isl_pw_aff_eq_map(__isl_take isl_pw_aff
*pa1
,
2999 __isl_take isl_pw_aff
*pa2
)
3001 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_eq_map_aligned
);
3004 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3005 * where the function value of "pa1" is less than the function value of "pa2".
3006 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3008 static __isl_give isl_map
*isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3009 __isl_take isl_pw_aff
*pa2
)
3011 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_lt_set
);
3014 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3015 * where the function value of "pa1" is less than the function value of "pa2".
3017 __isl_give isl_map
*isl_pw_aff_lt_map(__isl_take isl_pw_aff
*pa1
,
3018 __isl_take isl_pw_aff
*pa2
)
3020 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_lt_map_aligned
);
3023 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3024 * where the function value of "pa1" is greater than the function value
3026 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3028 static __isl_give isl_map
*isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff
*pa1
,
3029 __isl_take isl_pw_aff
*pa2
)
3031 return isl_pw_aff_order_map_aligned(pa1
, pa2
, &isl_pw_aff_gt_set
);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function value of "pa1" is greater than the function value
3038 __isl_give isl_map
*isl_pw_aff_gt_map(__isl_take isl_pw_aff
*pa1
,
3039 __isl_take isl_pw_aff
*pa2
)
3041 return align_params_pw_pw_map_and(pa1
, pa2
, &isl_pw_aff_gt_map_aligned
);
3044 /* Return a set containing those elements in the shared domain
3045 * of the elements of list1 and list2 where each element in list1
3046 * has the relation specified by "fn" with each element in list2.
3048 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
3049 __isl_take isl_pw_aff_list
*list2
,
3050 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3051 __isl_take isl_pw_aff
*pwaff2
))
3057 if (!list1
|| !list2
)
3060 ctx
= isl_pw_aff_list_get_ctx(list1
);
3061 if (list1
->n
< 1 || list2
->n
< 1)
3062 isl_die(ctx
, isl_error_invalid
,
3063 "list should contain at least one element", goto error
);
3065 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
3066 for (i
= 0; i
< list1
->n
; ++i
)
3067 for (j
= 0; j
< list2
->n
; ++j
) {
3070 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
3071 isl_pw_aff_copy(list2
->p
[j
]));
3072 set
= isl_set_intersect(set
, set_ij
);
3075 isl_pw_aff_list_free(list1
);
3076 isl_pw_aff_list_free(list2
);
3079 isl_pw_aff_list_free(list1
);
3080 isl_pw_aff_list_free(list2
);
3084 /* Return a set containing those elements in the shared domain
3085 * of the elements of list1 and list2 where each element in list1
3086 * is equal to each element in list2.
3088 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3089 __isl_take isl_pw_aff_list
*list2
)
3091 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3094 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3095 __isl_take isl_pw_aff_list
*list2
)
3097 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3100 /* Return a set containing those elements in the shared domain
3101 * of the elements of list1 and list2 where each element in list1
3102 * is less than or equal to each element in list2.
3104 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3105 __isl_take isl_pw_aff_list
*list2
)
3107 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3110 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3111 __isl_take isl_pw_aff_list
*list2
)
3113 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3116 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3117 __isl_take isl_pw_aff_list
*list2
)
3119 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3122 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3123 __isl_take isl_pw_aff_list
*list2
)
3125 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3129 /* Return a set containing those elements in the shared domain
3130 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3132 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3133 __isl_take isl_pw_aff
*pwaff2
)
3135 isl_set
*set_lt
, *set_gt
;
3137 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3138 isl_pw_aff_copy(pwaff2
));
3139 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3140 return isl_set_union_disjoint(set_lt
, set_gt
);
3143 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3144 __isl_take isl_pw_aff
*pwaff2
)
3146 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3149 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3154 if (isl_int_is_one(v
))
3156 if (!isl_int_is_pos(v
))
3157 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3158 "factor needs to be positive",
3159 return isl_pw_aff_free(pwaff
));
3160 pwaff
= isl_pw_aff_cow(pwaff
);
3166 for (i
= 0; i
< pwaff
->n
; ++i
) {
3167 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3168 if (!pwaff
->p
[i
].aff
)
3169 return isl_pw_aff_free(pwaff
);
3175 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3179 pwaff
= isl_pw_aff_cow(pwaff
);
3185 for (i
= 0; i
< pwaff
->n
; ++i
) {
3186 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3187 if (!pwaff
->p
[i
].aff
)
3188 return isl_pw_aff_free(pwaff
);
3194 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3198 pwaff
= isl_pw_aff_cow(pwaff
);
3204 for (i
= 0; i
< pwaff
->n
; ++i
) {
3205 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3206 if (!pwaff
->p
[i
].aff
)
3207 return isl_pw_aff_free(pwaff
);
3213 /* Assuming that "cond1" and "cond2" are disjoint,
3214 * return an affine expression that is equal to pwaff1 on cond1
3215 * and to pwaff2 on cond2.
3217 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3218 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3219 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3221 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3222 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3224 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3227 /* Return an affine expression that is equal to pwaff_true for elements
3228 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3230 * That is, return cond ? pwaff_true : pwaff_false;
3232 * If "cond" involves and NaN, then we conservatively return a NaN
3233 * on its entire domain. In principle, we could consider the pieces
3234 * where it is NaN separately from those where it is not.
3236 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3237 * then only use the domain of "cond" to restrict the domain.
3239 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3240 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3242 isl_set
*cond_true
, *cond_false
;
3247 if (isl_pw_aff_involves_nan(cond
)) {
3248 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3249 isl_local_space
*ls
= isl_local_space_from_space(space
);
3250 isl_pw_aff_free(cond
);
3251 isl_pw_aff_free(pwaff_true
);
3252 isl_pw_aff_free(pwaff_false
);
3253 return isl_pw_aff_nan_on_domain(ls
);
3256 pwaff_true
= isl_pw_aff_align_params(pwaff_true
,
3257 isl_pw_aff_get_space(pwaff_false
));
3258 pwaff_false
= isl_pw_aff_align_params(pwaff_false
,
3259 isl_pw_aff_get_space(pwaff_true
));
3260 equal
= isl_pw_aff_plain_is_equal(pwaff_true
, pwaff_false
);
3266 dom
= isl_set_coalesce(isl_pw_aff_domain(cond
));
3267 isl_pw_aff_free(pwaff_false
);
3268 return isl_pw_aff_intersect_domain(pwaff_true
, dom
);
3271 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3272 cond_false
= isl_pw_aff_zero_set(cond
);
3273 return isl_pw_aff_select(cond_true
, pwaff_true
,
3274 cond_false
, pwaff_false
);
3276 isl_pw_aff_free(cond
);
3277 isl_pw_aff_free(pwaff_true
);
3278 isl_pw_aff_free(pwaff_false
);
3282 isl_bool
isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3285 return isl_bool_error
;
3287 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3290 /* Check whether pwaff is a piecewise constant.
3292 isl_bool
isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3297 return isl_bool_error
;
3299 for (i
= 0; i
< pwaff
->n
; ++i
) {
3300 isl_bool is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3301 if (is_cst
< 0 || !is_cst
)
3305 return isl_bool_true
;
3308 /* Are all elements of "mpa" piecewise constants?
3310 isl_bool
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff
*mpa
)
3315 return isl_bool_error
;
3317 for (i
= 0; i
< mpa
->n
; ++i
) {
3318 isl_bool is_cst
= isl_pw_aff_is_cst(mpa
->p
[i
]);
3319 if (is_cst
< 0 || !is_cst
)
3323 return isl_bool_true
;
3326 /* Return the product of "aff1" and "aff2".
3328 * If either of the two is NaN, then the result is NaN.
3330 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3332 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3333 __isl_take isl_aff
*aff2
)
3338 if (isl_aff_is_nan(aff1
)) {
3342 if (isl_aff_is_nan(aff2
)) {
3347 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3348 return isl_aff_mul(aff2
, aff1
);
3350 if (!isl_aff_is_cst(aff2
))
3351 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3352 "at least one affine expression should be constant",
3355 aff1
= isl_aff_cow(aff1
);
3359 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3360 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3370 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3372 * If either of the two is NaN, then the result is NaN.
3374 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3375 __isl_take isl_aff
*aff2
)
3383 if (isl_aff_is_nan(aff1
)) {
3387 if (isl_aff_is_nan(aff2
)) {
3392 is_cst
= isl_aff_is_cst(aff2
);
3396 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3397 "second argument should be a constant", goto error
);
3402 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3404 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3405 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3408 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3409 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3412 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3413 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3424 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3425 __isl_take isl_pw_aff
*pwaff2
)
3427 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3430 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3431 __isl_take isl_pw_aff
*pwaff2
)
3433 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3436 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3437 __isl_take isl_pw_aff
*pwaff2
)
3439 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3442 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3443 __isl_take isl_pw_aff
*pwaff2
)
3445 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3448 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3449 __isl_take isl_pw_aff
*pwaff2
)
3451 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3454 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3455 __isl_take isl_pw_aff
*pa2
)
3457 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3460 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3462 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3463 __isl_take isl_pw_aff
*pa2
)
3467 is_cst
= isl_pw_aff_is_cst(pa2
);
3471 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3472 "second argument should be a piecewise constant",
3474 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3476 isl_pw_aff_free(pa1
);
3477 isl_pw_aff_free(pa2
);
3481 /* Compute the quotient of the integer division of "pa1" by "pa2"
3482 * with rounding towards zero.
3483 * "pa2" is assumed to be a piecewise constant.
3485 * In particular, return
3487 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3490 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3491 __isl_take isl_pw_aff
*pa2
)
3497 is_cst
= isl_pw_aff_is_cst(pa2
);
3501 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3502 "second argument should be a piecewise constant",
3505 pa1
= isl_pw_aff_div(pa1
, pa2
);
3507 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3508 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3509 c
= isl_pw_aff_ceil(pa1
);
3510 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3512 isl_pw_aff_free(pa1
);
3513 isl_pw_aff_free(pa2
);
3517 /* Compute the remainder of the integer division of "pa1" by "pa2"
3518 * with rounding towards zero.
3519 * "pa2" is assumed to be a piecewise constant.
3521 * In particular, return
3523 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3526 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3527 __isl_take isl_pw_aff
*pa2
)
3532 is_cst
= isl_pw_aff_is_cst(pa2
);
3536 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3537 "second argument should be a piecewise constant",
3539 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3540 res
= isl_pw_aff_mul(pa2
, res
);
3541 res
= isl_pw_aff_sub(pa1
, res
);
3544 isl_pw_aff_free(pa1
);
3545 isl_pw_aff_free(pa2
);
3549 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3550 __isl_take isl_pw_aff
*pwaff2
)
3555 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3556 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3557 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3558 isl_pw_aff_copy(pwaff2
));
3559 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3560 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3563 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3564 __isl_take isl_pw_aff
*pwaff2
)
3566 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3569 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3570 __isl_take isl_pw_aff
*pwaff2
)
3575 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3576 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3577 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3578 isl_pw_aff_copy(pwaff2
));
3579 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3580 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3583 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3584 __isl_take isl_pw_aff
*pwaff2
)
3586 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3589 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3590 __isl_take isl_pw_aff_list
*list
,
3591 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3592 __isl_take isl_pw_aff
*pwaff2
))
3601 ctx
= isl_pw_aff_list_get_ctx(list
);
3603 isl_die(ctx
, isl_error_invalid
,
3604 "list should contain at least one element", goto error
);
3606 res
= isl_pw_aff_copy(list
->p
[0]);
3607 for (i
= 1; i
< list
->n
; ++i
)
3608 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3610 isl_pw_aff_list_free(list
);
3613 isl_pw_aff_list_free(list
);
3617 /* Return an isl_pw_aff that maps each element in the intersection of the
3618 * domains of the elements of list to the minimal corresponding affine
3621 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3623 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3626 /* Return an isl_pw_aff that maps each element in the intersection of the
3627 * domains of the elements of list to the maximal corresponding affine
3630 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3632 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3635 /* Mark the domains of "pwaff" as rational.
3637 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3641 pwaff
= isl_pw_aff_cow(pwaff
);
3647 for (i
= 0; i
< pwaff
->n
; ++i
) {
3648 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3649 if (!pwaff
->p
[i
].set
)
3650 return isl_pw_aff_free(pwaff
);
3656 /* Mark the domains of the elements of "list" as rational.
3658 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3659 __isl_take isl_pw_aff_list
*list
)
3669 for (i
= 0; i
< n
; ++i
) {
3672 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3673 pa
= isl_pw_aff_set_rational(pa
);
3674 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3680 /* Do the parameters of "aff" match those of "space"?
3682 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3683 __isl_keep isl_space
*space
)
3685 isl_space
*aff_space
;
3691 aff_space
= isl_aff_get_domain_space(aff
);
3693 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3695 isl_space_free(aff_space
);
3699 /* Check that the domain space of "aff" matches "space".
3701 * Return 0 on success and -1 on error.
3703 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3704 __isl_keep isl_space
*space
)
3706 isl_space
*aff_space
;
3712 aff_space
= isl_aff_get_domain_space(aff
);
3714 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3718 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3719 "parameters don't match", goto error
);
3720 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3721 aff_space
, isl_dim_set
);
3725 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3726 "domains don't match", goto error
);
3727 isl_space_free(aff_space
);
3730 isl_space_free(aff_space
);
3740 #include <isl_multi_templ.c>
3741 #include <isl_multi_apply_set.c>
3742 #include <isl_multi_cmp.c>
3743 #include <isl_multi_floor.c>
3744 #include <isl_multi_gist.c>
3748 /* Remove any internal structure of the domain of "ma".
3749 * If there is any such internal structure in the input,
3750 * then the name of the corresponding space is also removed.
3752 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3753 __isl_take isl_multi_aff
*ma
)
3760 if (!ma
->space
->nested
[0])
3763 space
= isl_multi_aff_get_space(ma
);
3764 space
= isl_space_flatten_domain(space
);
3765 ma
= isl_multi_aff_reset_space(ma
, space
);
3770 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3771 * of the space to its domain.
3773 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3776 isl_local_space
*ls
;
3781 if (!isl_space_is_map(space
))
3782 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3783 "not a map space", goto error
);
3785 n_in
= isl_space_dim(space
, isl_dim_in
);
3786 space
= isl_space_domain_map(space
);
3788 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3790 isl_space_free(space
);
3794 space
= isl_space_domain(space
);
3795 ls
= isl_local_space_from_space(space
);
3796 for (i
= 0; i
< n_in
; ++i
) {
3799 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3801 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3803 isl_local_space_free(ls
);
3806 isl_space_free(space
);
3810 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3811 * of the space to its range.
3813 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3816 isl_local_space
*ls
;
3821 if (!isl_space_is_map(space
))
3822 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3823 "not a map space", goto error
);
3825 n_in
= isl_space_dim(space
, isl_dim_in
);
3826 n_out
= isl_space_dim(space
, isl_dim_out
);
3827 space
= isl_space_range_map(space
);
3829 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3831 isl_space_free(space
);
3835 space
= isl_space_domain(space
);
3836 ls
= isl_local_space_from_space(space
);
3837 for (i
= 0; i
< n_out
; ++i
) {
3840 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3841 isl_dim_set
, n_in
+ i
);
3842 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3844 isl_local_space_free(ls
);
3847 isl_space_free(space
);
3851 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3852 * of the space to its range.
3854 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3855 __isl_take isl_space
*space
)
3857 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3860 /* Given the space of a set and a range of set dimensions,
3861 * construct an isl_multi_aff that projects out those dimensions.
3863 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3864 __isl_take isl_space
*space
, enum isl_dim_type type
,
3865 unsigned first
, unsigned n
)
3868 isl_local_space
*ls
;
3873 if (!isl_space_is_set(space
))
3874 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3875 "expecting set space", goto error
);
3876 if (type
!= isl_dim_set
)
3877 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3878 "only set dimensions can be projected out", goto error
);
3880 dim
= isl_space_dim(space
, isl_dim_set
);
3881 if (first
+ n
> dim
)
3882 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3883 "range out of bounds", goto error
);
3885 space
= isl_space_from_domain(space
);
3886 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3889 return isl_multi_aff_alloc(space
);
3891 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3892 space
= isl_space_domain(space
);
3893 ls
= isl_local_space_from_space(space
);
3895 for (i
= 0; i
< first
; ++i
) {
3898 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3900 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3903 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3906 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3907 isl_dim_set
, first
+ n
+ i
);
3908 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3911 isl_local_space_free(ls
);
3914 isl_space_free(space
);
3918 /* Given the space of a set and a range of set dimensions,
3919 * construct an isl_pw_multi_aff that projects out those dimensions.
3921 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3922 __isl_take isl_space
*space
, enum isl_dim_type type
,
3923 unsigned first
, unsigned n
)
3927 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3928 return isl_pw_multi_aff_from_multi_aff(ma
);
3931 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3934 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3935 __isl_take isl_multi_aff
*ma
)
3937 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3938 return isl_pw_multi_aff_alloc(dom
, ma
);
3941 /* Create a piecewise multi-affine expression in the given space that maps each
3942 * input dimension to the corresponding output dimension.
3944 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3945 __isl_take isl_space
*space
)
3947 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3950 /* Exploit the equalities in "eq" to simplify the affine expressions.
3952 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3953 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3957 maff
= isl_multi_aff_cow(maff
);
3961 for (i
= 0; i
< maff
->n
; ++i
) {
3962 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3963 isl_basic_set_copy(eq
));
3968 isl_basic_set_free(eq
);
3971 isl_basic_set_free(eq
);
3972 isl_multi_aff_free(maff
);
3976 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3981 maff
= isl_multi_aff_cow(maff
);
3985 for (i
= 0; i
< maff
->n
; ++i
) {
3986 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3988 return isl_multi_aff_free(maff
);
3994 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3995 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3997 maff1
= isl_multi_aff_add(maff1
, maff2
);
3998 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
4002 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
4010 /* Return the set of domain elements where "ma1" is lexicographically
4011 * smaller than or equal to "ma2".
4013 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
4014 __isl_take isl_multi_aff
*ma2
)
4016 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
4019 /* Return the set of domain elements where "ma1" is lexicographically
4020 * smaller than "ma2".
4022 __isl_give isl_set
*isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff
*ma1
,
4023 __isl_take isl_multi_aff
*ma2
)
4025 return isl_multi_aff_lex_gt_set(ma2
, ma1
);
4028 /* Return the set of domain elements where "ma1" and "ma2"
4031 static __isl_give isl_set
*isl_multi_aff_order_set(
4032 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
,
4033 __isl_give isl_map
*order(__isl_take isl_space
*set_space
))
4036 isl_map
*map1
, *map2
;
4039 map1
= isl_map_from_multi_aff(ma1
);
4040 map2
= isl_map_from_multi_aff(ma2
);
4041 map
= isl_map_range_product(map1
, map2
);
4042 space
= isl_space_range(isl_map_get_space(map
));
4043 space
= isl_space_domain(isl_space_unwrap(space
));
4045 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
4047 return isl_map_domain(map
);
4050 /* Return the set of domain elements where "ma1" is lexicographically
4051 * greater than or equal to "ma2".
4053 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
4054 __isl_take isl_multi_aff
*ma2
)
4056 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_ge
);
4059 /* Return the set of domain elements where "ma1" is lexicographically
4060 * greater than "ma2".
4062 __isl_give isl_set
*isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff
*ma1
,
4063 __isl_take isl_multi_aff
*ma2
)
4065 return isl_multi_aff_order_set(ma1
, ma2
, &isl_map_lex_gt
);
4069 #define PW isl_pw_multi_aff
4071 #define EL isl_multi_aff
4073 #define EL_IS_ZERO is_empty
4077 #define IS_ZERO is_empty
4080 #undef DEFAULT_IS_ZERO
4081 #define DEFAULT_IS_ZERO 0
4086 #define NO_INVOLVES_DIMS
4087 #define NO_INSERT_DIMS
4091 #include <isl_pw_templ.c>
4092 #include <isl_pw_union_opt.c>
4097 #define UNION isl_union_pw_multi_aff
4099 #define PART isl_pw_multi_aff
4101 #define PARTS pw_multi_aff
4103 #include <isl_union_multi.c>
4104 #include <isl_union_neg.c>
4106 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4107 __isl_take isl_pw_multi_aff
*pma1
,
4108 __isl_take isl_pw_multi_aff
*pma2
)
4110 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4111 &isl_multi_aff_lex_ge_set
);
4114 /* Given two piecewise multi affine expressions, return a piecewise
4115 * multi-affine expression defined on the union of the definition domains
4116 * of the inputs that is equal to the lexicographic maximum of the two
4117 * inputs on each cell. If only one of the two inputs is defined on
4118 * a given cell, then it is considered to be the maximum.
4120 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4121 __isl_take isl_pw_multi_aff
*pma1
,
4122 __isl_take isl_pw_multi_aff
*pma2
)
4124 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4125 &pw_multi_aff_union_lexmax
);
4128 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4129 __isl_take isl_pw_multi_aff
*pma1
,
4130 __isl_take isl_pw_multi_aff
*pma2
)
4132 return isl_pw_multi_aff_union_opt_cmp(pma1
, pma2
,
4133 &isl_multi_aff_lex_le_set
);
4136 /* Given two piecewise multi affine expressions, return a piecewise
4137 * multi-affine expression defined on the union of the definition domains
4138 * of the inputs that is equal to the lexicographic minimum of the two
4139 * inputs on each cell. If only one of the two inputs is defined on
4140 * a given cell, then it is considered to be the minimum.
4142 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4143 __isl_take isl_pw_multi_aff
*pma1
,
4144 __isl_take isl_pw_multi_aff
*pma2
)
4146 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4147 &pw_multi_aff_union_lexmin
);
4150 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4151 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4153 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4154 &isl_multi_aff_add
);
4157 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4158 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4160 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4164 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4165 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4167 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4168 &isl_multi_aff_sub
);
4171 /* Subtract "pma2" from "pma1" and return the result.
4173 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4174 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4176 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4180 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4181 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4183 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4186 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4187 * with the actual sum on the shared domain and
4188 * the defined expression on the symmetric difference of the domains.
4190 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4191 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4193 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4196 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4197 * with the actual sum on the shared domain and
4198 * the defined expression on the symmetric difference of the domains.
4200 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4201 __isl_take isl_union_pw_multi_aff
*upma1
,
4202 __isl_take isl_union_pw_multi_aff
*upma2
)
4204 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4207 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4208 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4210 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4211 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4215 isl_pw_multi_aff
*res
;
4220 n
= pma1
->n
* pma2
->n
;
4221 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4222 isl_space_copy(pma2
->dim
));
4223 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4225 for (i
= 0; i
< pma1
->n
; ++i
) {
4226 for (j
= 0; j
< pma2
->n
; ++j
) {
4230 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4231 isl_set_copy(pma2
->p
[j
].set
));
4232 ma
= isl_multi_aff_product(
4233 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4234 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4235 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4239 isl_pw_multi_aff_free(pma1
);
4240 isl_pw_multi_aff_free(pma2
);
4243 isl_pw_multi_aff_free(pma1
);
4244 isl_pw_multi_aff_free(pma2
);
4248 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4249 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4251 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4252 &pw_multi_aff_product
);
4255 /* Construct a map mapping the domain of the piecewise multi-affine expression
4256 * to its range, with each dimension in the range equated to the
4257 * corresponding affine expression on its cell.
4259 * If the domain of "pma" is rational, then so is the constructed "map".
4261 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4269 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4271 for (i
= 0; i
< pma
->n
; ++i
) {
4273 isl_multi_aff
*maff
;
4274 isl_basic_map
*bmap
;
4277 rational
= isl_set_is_rational(pma
->p
[i
].set
);
4279 map
= isl_map_free(map
);
4280 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4281 bmap
= isl_basic_map_from_multi_aff2(maff
, rational
);
4282 map_i
= isl_map_from_basic_map(bmap
);
4283 map_i
= isl_map_intersect_domain(map_i
,
4284 isl_set_copy(pma
->p
[i
].set
));
4285 map
= isl_map_union_disjoint(map
, map_i
);
4288 isl_pw_multi_aff_free(pma
);
4292 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4297 if (!isl_space_is_set(pma
->dim
))
4298 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4299 "isl_pw_multi_aff cannot be converted into an isl_set",
4302 return isl_map_from_pw_multi_aff(pma
);
4304 isl_pw_multi_aff_free(pma
);
4308 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4309 * denominator "denom".
4310 * "denom" is allowed to be negative, in which case the actual denominator
4311 * is -denom and the expressions are added instead.
4313 static __isl_give isl_aff
*subtract_initial(__isl_take isl_aff
*aff
,
4314 __isl_keep isl_multi_aff
*ma
, int n
, isl_int
*c
, isl_int denom
)
4320 first
= isl_seq_first_non_zero(c
, n
);
4324 sign
= isl_int_sgn(denom
);
4326 isl_int_abs(d
, denom
);
4327 for (i
= first
; i
< n
; ++i
) {
4330 if (isl_int_is_zero(c
[i
]))
4332 aff_i
= isl_multi_aff_get_aff(ma
, i
);
4333 aff_i
= isl_aff_scale(aff_i
, c
[i
]);
4334 aff_i
= isl_aff_scale_down(aff_i
, d
);
4336 aff
= isl_aff_sub(aff
, aff_i
);
4338 aff
= isl_aff_add(aff
, aff_i
);
4345 /* Extract an affine expression that expresses the output dimension "pos"
4346 * of "bmap" in terms of the parameters and input dimensions from
4348 * Note that this expression may involve integer divisions defined
4349 * in terms of parameters and input dimensions.
4350 * The equality may also involve references to earlier (but not later)
4351 * output dimensions. These are replaced by the corresponding elements
4354 * If the equality is of the form
4356 * f(i) + h(j) + a x + g(i) = 0,
4358 * with f(i) a linear combinations of the parameters and input dimensions,
4359 * g(i) a linear combination of integer divisions defined in terms of the same
4360 * and h(j) a linear combinations of earlier output dimensions,
4361 * then the affine expression is
4363 * (-f(i) - g(i))/a - h(j)/a
4365 * If the equality is of the form
4367 * f(i) + h(j) - a x + g(i) = 0,
4369 * then the affine expression is
4371 * (f(i) + g(i))/a - h(j)/(-a)
4374 * If "div" refers to an integer division (i.e., it is smaller than
4375 * the number of integer divisions), then the equality constraint
4376 * does involve an integer division (the one at position "div") that
4377 * is defined in terms of output dimensions. However, this integer
4378 * division can be eliminated by exploiting a pair of constraints
4379 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4380 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4382 * In particular, let
4384 * x = e(i) + m floor(...)
4386 * with e(i) the expression derived above and floor(...) the integer
4387 * division involving output dimensions.
4398 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4399 * = (e(i) - l) mod m
4403 * x - l = (e(i) - l) mod m
4407 * x = ((e(i) - l) mod m) + l
4409 * The variable "shift" below contains the expression -l, which may
4410 * also involve a linear combination of earlier output dimensions.
4412 static __isl_give isl_aff
*extract_aff_from_equality(
4413 __isl_keep isl_basic_map
*bmap
, int pos
, int eq
, int div
, int ineq
,
4414 __isl_keep isl_multi_aff
*ma
)
4417 unsigned n_div
, n_out
;
4419 isl_local_space
*ls
;
4420 isl_aff
*aff
, *shift
;
4423 ctx
= isl_basic_map_get_ctx(bmap
);
4424 ls
= isl_basic_map_get_local_space(bmap
);
4425 ls
= isl_local_space_domain(ls
);
4426 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
4429 o_out
= isl_basic_map_offset(bmap
, isl_dim_out
);
4430 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4431 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4432 if (isl_int_is_neg(bmap
->eq
[eq
][o_out
+ pos
])) {
4433 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4434 isl_seq_cpy(aff
->v
->el
+ 1 + o_out
,
4435 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4437 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], o_out
);
4438 isl_seq_neg(aff
->v
->el
+ 1 + o_out
,
4439 bmap
->eq
[eq
] + o_out
+ n_out
, n_div
);
4442 isl_int_set_si(aff
->v
->el
[1 + o_out
+ div
], 0);
4443 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][o_out
+ pos
]);
4444 aff
= subtract_initial(aff
, ma
, pos
, bmap
->eq
[eq
] + o_out
,
4445 bmap
->eq
[eq
][o_out
+ pos
]);
4447 shift
= isl_aff_alloc(isl_local_space_copy(ls
));
4450 isl_seq_cpy(shift
->v
->el
+ 1, bmap
->ineq
[ineq
], o_out
);
4451 isl_seq_cpy(shift
->v
->el
+ 1 + o_out
,
4452 bmap
->ineq
[ineq
] + o_out
+ n_out
, n_div
);
4453 isl_int_set_si(shift
->v
->el
[0], 1);
4454 shift
= subtract_initial(shift
, ma
, pos
,
4455 bmap
->ineq
[ineq
] + o_out
, ctx
->negone
);
4456 aff
= isl_aff_add(aff
, isl_aff_copy(shift
));
4457 mod
= isl_val_int_from_isl_int(ctx
,
4458 bmap
->eq
[eq
][o_out
+ n_out
+ div
]);
4459 mod
= isl_val_abs(mod
);
4460 aff
= isl_aff_mod_val(aff
, mod
);
4461 aff
= isl_aff_sub(aff
, shift
);
4464 isl_local_space_free(ls
);
4467 isl_local_space_free(ls
);
4472 /* Given a basic map with output dimensions defined
4473 * in terms of the parameters input dimensions and earlier
4474 * output dimensions using an equality (and possibly a pair on inequalities),
4475 * extract an isl_aff that expresses output dimension "pos" in terms
4476 * of the parameters and input dimensions.
4477 * Note that this expression may involve integer divisions defined
4478 * in terms of parameters and input dimensions.
4479 * "ma" contains the expressions corresponding to earlier output dimensions.
4481 * This function shares some similarities with
4482 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4484 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4485 __isl_keep isl_basic_map
*bmap
, int pos
, __isl_keep isl_multi_aff
*ma
)
4492 eq
= isl_basic_map_output_defining_equality(bmap
, pos
, &div
, &ineq
);
4493 if (eq
>= bmap
->n_eq
)
4494 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4495 "unable to find suitable equality", return NULL
);
4496 aff
= extract_aff_from_equality(bmap
, pos
, eq
, div
, ineq
, ma
);
4498 aff
= isl_aff_remove_unused_divs(aff
);
4502 /* Given a basic map where each output dimension is defined
4503 * in terms of the parameters and input dimensions using an equality,
4504 * extract an isl_multi_aff that expresses the output dimensions in terms
4505 * of the parameters and input dimensions.
4507 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4508 __isl_take isl_basic_map
*bmap
)
4517 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4518 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4520 for (i
= 0; i
< n_out
; ++i
) {
4523 aff
= extract_isl_aff_from_basic_map(bmap
, i
, ma
);
4524 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4527 isl_basic_map_free(bmap
);
4532 /* Given a basic set where each set dimension is defined
4533 * in terms of the parameters using an equality,
4534 * extract an isl_multi_aff that expresses the set dimensions in terms
4535 * of the parameters.
4537 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4538 __isl_take isl_basic_set
*bset
)
4540 return extract_isl_multi_aff_from_basic_map(bset
);
4543 /* Create an isl_pw_multi_aff that is equivalent to
4544 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4545 * The given basic map is such that each output dimension is defined
4546 * in terms of the parameters and input dimensions using an equality.
4548 * Since some applications expect the result of isl_pw_multi_aff_from_map
4549 * to only contain integer affine expressions, we compute the floor
4550 * of the expression before returning.
4552 * Remove all constraints involving local variables without
4553 * an explicit representation (resulting in the removal of those
4554 * local variables) prior to the actual extraction to ensure
4555 * that the local spaces in which the resulting affine expressions
4556 * are created do not contain any unknown local variables.
4557 * Removing such constraints is safe because constraints involving
4558 * unknown local variables are not used to determine whether
4559 * a basic map is obviously single-valued.
4561 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4562 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4566 bmap
= isl_basic_map_drop_constraint_involving_unknown_divs(bmap
);
4567 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4568 ma
= isl_multi_aff_floor(ma
);
4569 return isl_pw_multi_aff_alloc(domain
, ma
);
4572 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4573 * This obviously only works if the input "map" is single-valued.
4574 * If so, we compute the lexicographic minimum of the image in the form
4575 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4576 * to its lexicographic minimum.
4577 * If the input is not single-valued, we produce an error.
4579 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4580 __isl_take isl_map
*map
)
4584 isl_pw_multi_aff
*pma
;
4586 sv
= isl_map_is_single_valued(map
);
4590 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4591 "map is not single-valued", goto error
);
4592 map
= isl_map_make_disjoint(map
);
4596 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4598 for (i
= 0; i
< map
->n
; ++i
) {
4599 isl_pw_multi_aff
*pma_i
;
4600 isl_basic_map
*bmap
;
4601 bmap
= isl_basic_map_copy(map
->p
[i
]);
4602 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4603 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4613 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4614 * taking into account that the output dimension at position "d"
4615 * can be represented as
4617 * x = floor((e(...) + c1) / m)
4619 * given that constraint "i" is of the form
4621 * e(...) + c1 - m x >= 0
4624 * Let "map" be of the form
4628 * We construct a mapping
4630 * A -> [A -> x = floor(...)]
4632 * apply that to the map, obtaining
4634 * [A -> x = floor(...)] -> B
4636 * and equate dimension "d" to x.
4637 * We then compute a isl_pw_multi_aff representation of the resulting map
4638 * and plug in the mapping above.
4640 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4641 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4645 isl_local_space
*ls
;
4653 isl_pw_multi_aff
*pma
;
4656 is_set
= isl_map_is_set(map
);
4658 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4659 ctx
= isl_map_get_ctx(map
);
4660 space
= isl_space_domain(isl_map_get_space(map
));
4661 n_in
= isl_space_dim(space
, isl_dim_set
);
4662 n
= isl_space_dim(space
, isl_dim_all
);
4664 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4666 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4667 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4669 isl_basic_map_free(hull
);
4671 ls
= isl_local_space_from_space(isl_space_copy(space
));
4672 aff
= isl_aff_alloc_vec(ls
, v
);
4673 aff
= isl_aff_floor(aff
);
4675 isl_space_free(space
);
4676 ma
= isl_multi_aff_from_aff(aff
);
4678 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4679 ma
= isl_multi_aff_range_product(ma
,
4680 isl_multi_aff_from_aff(aff
));
4683 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4684 map
= isl_map_apply_domain(map
, insert
);
4685 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4686 pma
= isl_pw_multi_aff_from_map(map
);
4687 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4692 /* Is constraint "c" of the form
4694 * e(...) + c1 - m x >= 0
4698 * -e(...) + c2 + m x >= 0
4700 * where m > 1 and e only depends on parameters and input dimemnsions?
4702 * "offset" is the offset of the output dimensions
4703 * "pos" is the position of output dimension x.
4705 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4707 if (isl_int_is_zero(c
[offset
+ d
]))
4709 if (isl_int_is_one(c
[offset
+ d
]))
4711 if (isl_int_is_negone(c
[offset
+ d
]))
4713 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4715 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4716 total
- (offset
+ d
+ 1)) != -1)
4721 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4723 * As a special case, we first check if there is any pair of constraints,
4724 * shared by all the basic maps in "map" that force a given dimension
4725 * to be equal to the floor of some affine combination of the input dimensions.
4727 * In particular, if we can find two constraints
4729 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4733 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4735 * where m > 1 and e only depends on parameters and input dimemnsions,
4738 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4740 * then we know that we can take
4742 * x = floor((e(...) + c1) / m)
4744 * without having to perform any computation.
4746 * Note that we know that
4750 * If c1 + c2 were 0, then we would have detected an equality during
4751 * simplification. If c1 + c2 were negative, then we would have detected
4754 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4755 __isl_take isl_map
*map
)
4761 isl_basic_map
*hull
;
4763 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4768 dim
= isl_map_dim(map
, isl_dim_out
);
4769 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4770 total
= 1 + isl_basic_map_total_dim(hull
);
4772 for (d
= 0; d
< dim
; ++d
) {
4773 for (i
= 0; i
< n
; ++i
) {
4774 if (!is_potential_div_constraint(hull
->ineq
[i
],
4777 for (j
= i
+ 1; j
< n
; ++j
) {
4778 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4779 hull
->ineq
[j
] + 1, total
- 1))
4781 isl_int_add(sum
, hull
->ineq
[i
][0],
4783 if (isl_int_abs_lt(sum
,
4784 hull
->ineq
[i
][offset
+ d
]))
4791 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4793 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4797 isl_basic_map_free(hull
);
4798 return pw_multi_aff_from_map_base(map
);
4801 isl_basic_map_free(hull
);
4805 /* Given an affine expression
4807 * [A -> B] -> f(A,B)
4809 * construct an isl_multi_aff
4813 * such that dimension "d" in B' is set to "aff" and the remaining
4814 * dimensions are set equal to the corresponding dimensions in B.
4815 * "n_in" is the dimension of the space A.
4816 * "n_out" is the dimension of the space B.
4818 * If "is_set" is set, then the affine expression is of the form
4822 * and we construct an isl_multi_aff
4826 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4827 unsigned n_in
, unsigned n_out
, int is_set
)
4831 isl_space
*space
, *space2
;
4832 isl_local_space
*ls
;
4834 space
= isl_aff_get_domain_space(aff
);
4835 ls
= isl_local_space_from_space(isl_space_copy(space
));
4836 space2
= isl_space_copy(space
);
4838 space2
= isl_space_range(isl_space_unwrap(space2
));
4839 space
= isl_space_map_from_domain_and_range(space
, space2
);
4840 ma
= isl_multi_aff_alloc(space
);
4841 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4843 for (i
= 0; i
< n_out
; ++i
) {
4846 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4847 isl_dim_set
, n_in
+ i
);
4848 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4851 isl_local_space_free(ls
);
4856 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4857 * taking into account that the dimension at position "d" can be written as
4859 * x = m a + f(..) (1)
4861 * where m is equal to "gcd".
4862 * "i" is the index of the equality in "hull" that defines f(..).
4863 * In particular, the equality is of the form
4865 * f(..) - x + m g(existentials) = 0
4869 * -f(..) + x + m g(existentials) = 0
4871 * We basically plug (1) into "map", resulting in a map with "a"
4872 * in the range instead of "x". The corresponding isl_pw_multi_aff
4873 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4875 * Specifically, given the input map
4879 * We first wrap it into a set
4883 * and define (1) on top of the corresponding space, resulting in "aff".
4884 * We use this to create an isl_multi_aff that maps the output position "d"
4885 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4886 * We plug this into the wrapped map, unwrap the result and compute the
4887 * corresponding isl_pw_multi_aff.
4888 * The result is an expression
4896 * so that we can plug that into "aff", after extending the latter to
4902 * If "map" is actually a set, then there is no "A" space, meaning
4903 * that we do not need to perform any wrapping, and that the result
4904 * of the recursive call is of the form
4908 * which is plugged into a mapping of the form
4912 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4913 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4918 isl_local_space
*ls
;
4921 isl_pw_multi_aff
*pma
, *id
;
4927 is_set
= isl_map_is_set(map
);
4929 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4930 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4931 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4936 set
= isl_map_wrap(map
);
4937 space
= isl_space_map_from_set(isl_set_get_space(set
));
4938 ma
= isl_multi_aff_identity(space
);
4939 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4940 aff
= isl_aff_alloc(ls
);
4942 isl_int_set_si(aff
->v
->el
[0], 1);
4943 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4944 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4947 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4949 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4951 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4952 set
= isl_set_preimage_multi_aff(set
, ma
);
4954 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4959 map
= isl_set_unwrap(set
);
4960 pma
= isl_pw_multi_aff_from_map(map
);
4963 space
= isl_pw_multi_aff_get_domain_space(pma
);
4964 space
= isl_space_map_from_set(space
);
4965 id
= isl_pw_multi_aff_identity(space
);
4966 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4968 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4969 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4971 isl_basic_map_free(hull
);
4975 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4976 * "hull" contains the equalities valid for "map".
4978 * Check if any of the output dimensions is "strided".
4979 * That is, we check if it can be written as
4983 * with m greater than 1, a some combination of existentially quantified
4984 * variables and f an expression in the parameters and input dimensions.
4985 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4987 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4990 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_strides(
4991 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
)
5000 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
5001 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
5004 isl_basic_map_free(hull
);
5005 return pw_multi_aff_from_map_check_div(map
);
5010 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
5011 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
5013 for (i
= 0; i
< n_out
; ++i
) {
5014 for (j
= 0; j
< hull
->n_eq
; ++j
) {
5015 isl_int
*eq
= hull
->eq
[j
];
5016 isl_pw_multi_aff
*res
;
5018 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
5019 !isl_int_is_negone(eq
[o_out
+ i
]))
5021 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
5023 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
5024 n_out
- (i
+ 1)) != -1)
5026 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
5027 if (isl_int_is_zero(gcd
))
5029 if (isl_int_is_one(gcd
))
5032 res
= pw_multi_aff_from_map_stride(map
, hull
,
5040 isl_basic_map_free(hull
);
5041 return pw_multi_aff_from_map_check_div(map
);
5044 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5046 * As a special case, we first check if all output dimensions are uniquely
5047 * defined in terms of the parameters and input dimensions over the entire
5048 * domain. If so, we extract the desired isl_pw_multi_aff directly
5049 * from the affine hull of "map" and its domain.
5051 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5054 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
5057 isl_basic_map
*hull
;
5062 if (isl_map_n_basic_map(map
) == 1) {
5063 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5064 hull
= isl_basic_map_plain_affine_hull(hull
);
5065 sv
= isl_basic_map_plain_is_single_valued(hull
);
5067 return plain_pw_multi_aff_from_map(isl_map_domain(map
),
5069 isl_basic_map_free(hull
);
5071 map
= isl_map_detect_equalities(map
);
5072 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
5073 sv
= isl_basic_map_plain_is_single_valued(hull
);
5075 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
5077 return pw_multi_aff_from_map_check_strides(map
, hull
);
5078 isl_basic_map_free(hull
);
5083 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
5085 return isl_pw_multi_aff_from_map(set
);
5088 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5091 static isl_stat
pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
5093 isl_union_pw_multi_aff
**upma
= user
;
5094 isl_pw_multi_aff
*pma
;
5096 pma
= isl_pw_multi_aff_from_map(map
);
5097 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5099 return *upma
? isl_stat_ok
: isl_stat_error
;
5102 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5105 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
5106 __isl_take isl_aff
*aff
)
5109 isl_pw_multi_aff
*pma
;
5111 ma
= isl_multi_aff_from_aff(aff
);
5112 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
5113 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
5116 /* Try and create an isl_union_pw_multi_aff that is equivalent
5117 * to the given isl_union_map.
5118 * The isl_union_map is required to be single-valued in each space.
5119 * Otherwise, an error is produced.
5121 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
5122 __isl_take isl_union_map
*umap
)
5125 isl_union_pw_multi_aff
*upma
;
5127 space
= isl_union_map_get_space(umap
);
5128 upma
= isl_union_pw_multi_aff_empty(space
);
5129 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
5130 upma
= isl_union_pw_multi_aff_free(upma
);
5131 isl_union_map_free(umap
);
5136 /* Try and create an isl_union_pw_multi_aff that is equivalent
5137 * to the given isl_union_set.
5138 * The isl_union_set is required to be a singleton in each space.
5139 * Otherwise, an error is produced.
5141 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
5142 __isl_take isl_union_set
*uset
)
5144 return isl_union_pw_multi_aff_from_union_map(uset
);
5147 /* Return the piecewise affine expression "set ? 1 : 0".
5149 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
5152 isl_space
*space
= isl_set_get_space(set
);
5153 isl_local_space
*ls
= isl_local_space_from_space(space
);
5154 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
5155 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
5157 one
= isl_aff_add_constant_si(one
, 1);
5158 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
5159 set
= isl_set_complement(set
);
5160 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
5165 /* Plug in "subs" for dimension "type", "pos" of "aff".
5167 * Let i be the dimension to replace and let "subs" be of the form
5171 * and "aff" of the form
5177 * (a f + d g')/(m d)
5179 * where g' is the result of plugging in "subs" in each of the integer
5182 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5183 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5188 aff
= isl_aff_cow(aff
);
5190 return isl_aff_free(aff
);
5192 ctx
= isl_aff_get_ctx(aff
);
5193 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5194 isl_die(ctx
, isl_error_invalid
,
5195 "spaces don't match", return isl_aff_free(aff
));
5196 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5197 isl_die(ctx
, isl_error_unsupported
,
5198 "cannot handle divs yet", return isl_aff_free(aff
));
5200 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5202 return isl_aff_free(aff
);
5204 aff
->v
= isl_vec_cow(aff
->v
);
5206 return isl_aff_free(aff
);
5208 pos
+= isl_local_space_offset(aff
->ls
, type
);
5211 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5212 aff
->v
->size
, subs
->v
->size
, v
);
5218 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5219 * expressions in "maff".
5221 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5222 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5223 __isl_keep isl_aff
*subs
)
5227 maff
= isl_multi_aff_cow(maff
);
5229 return isl_multi_aff_free(maff
);
5231 if (type
== isl_dim_in
)
5234 for (i
= 0; i
< maff
->n
; ++i
) {
5235 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5237 return isl_multi_aff_free(maff
);
5243 /* Plug in "subs" for dimension "type", "pos" of "pma".
5245 * pma is of the form
5249 * while subs is of the form
5251 * v' = B_j(v) -> S_j
5253 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5254 * has a contribution in the result, in particular
5256 * C_ij(S_j) -> M_i(S_j)
5258 * Note that plugging in S_j in C_ij may also result in an empty set
5259 * and this contribution should simply be discarded.
5261 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5262 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5263 __isl_keep isl_pw_aff
*subs
)
5266 isl_pw_multi_aff
*res
;
5269 return isl_pw_multi_aff_free(pma
);
5271 n
= pma
->n
* subs
->n
;
5272 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5274 for (i
= 0; i
< pma
->n
; ++i
) {
5275 for (j
= 0; j
< subs
->n
; ++j
) {
5277 isl_multi_aff
*res_ij
;
5280 common
= isl_set_intersect(
5281 isl_set_copy(pma
->p
[i
].set
),
5282 isl_set_copy(subs
->p
[j
].set
));
5283 common
= isl_set_substitute(common
,
5284 type
, pos
, subs
->p
[j
].aff
);
5285 empty
= isl_set_plain_is_empty(common
);
5286 if (empty
< 0 || empty
) {
5287 isl_set_free(common
);
5293 res_ij
= isl_multi_aff_substitute(
5294 isl_multi_aff_copy(pma
->p
[i
].maff
),
5295 type
, pos
, subs
->p
[j
].aff
);
5297 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5301 isl_pw_multi_aff_free(pma
);
5304 isl_pw_multi_aff_free(pma
);
5305 isl_pw_multi_aff_free(res
);
5309 /* Compute the preimage of a range of dimensions in the affine expression "src"
5310 * under "ma" and put the result in "dst". The number of dimensions in "src"
5311 * that precede the range is given by "n_before". The number of dimensions
5312 * in the range is given by the number of output dimensions of "ma".
5313 * The number of dimensions that follow the range is given by "n_after".
5314 * If "has_denom" is set (to one),
5315 * then "src" and "dst" have an extra initial denominator.
5316 * "n_div_ma" is the number of existentials in "ma"
5317 * "n_div_bset" is the number of existentials in "src"
5318 * The resulting "dst" (which is assumed to have been allocated by
5319 * the caller) contains coefficients for both sets of existentials,
5320 * first those in "ma" and then those in "src".
5321 * f, c1, c2 and g are temporary objects that have been initialized
5324 * Let src represent the expression
5326 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5328 * and let ma represent the expressions
5330 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5332 * We start out with the following expression for dst:
5334 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5336 * with the multiplication factor f initially equal to 1
5337 * and f \sum_i b_i v_i kept separately.
5338 * For each x_i that we substitute, we multiply the numerator
5339 * (and denominator) of dst by c_1 = m_i and add the numerator
5340 * of the x_i expression multiplied by c_2 = f b_i,
5341 * after removing the common factors of c_1 and c_2.
5342 * The multiplication factor f also needs to be multiplied by c_1
5343 * for the next x_j, j > i.
5345 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5346 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5347 int n_div_ma
, int n_div_bmap
,
5348 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5351 int n_param
, n_in
, n_out
;
5354 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5355 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5356 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5358 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5359 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5360 isl_seq_clr(dst
+ o_dst
, n_in
);
5363 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5366 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5368 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5370 isl_int_set_si(f
, 1);
5372 for (i
= 0; i
< n_out
; ++i
) {
5373 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5375 if (isl_int_is_zero(src
[offset
]))
5377 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5378 isl_int_mul(c2
, f
, src
[offset
]);
5379 isl_int_gcd(g
, c1
, c2
);
5380 isl_int_divexact(c1
, c1
, g
);
5381 isl_int_divexact(c2
, c2
, g
);
5383 isl_int_mul(f
, f
, c1
);
5386 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5387 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5388 o_dst
+= 1 + n_param
;
5389 o_src
+= 1 + n_param
;
5390 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5392 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5393 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5396 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5398 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5399 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5402 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5404 isl_int_mul(dst
[0], dst
[0], c1
);
5408 /* Compute the pullback of "aff" by the function represented by "ma".
5409 * In other words, plug in "ma" in "aff". The result is an affine expression
5410 * defined over the domain space of "ma".
5412 * If "aff" is represented by
5414 * (a(p) + b x + c(divs))/d
5416 * and ma is represented by
5418 * x = D(p) + F(y) + G(divs')
5420 * then the result is
5422 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5424 * The divs in the local space of the input are similarly adjusted
5425 * through a call to isl_local_space_preimage_multi_aff.
5427 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5428 __isl_take isl_multi_aff
*ma
)
5430 isl_aff
*res
= NULL
;
5431 isl_local_space
*ls
;
5432 int n_div_aff
, n_div_ma
;
5433 isl_int f
, c1
, c2
, g
;
5435 ma
= isl_multi_aff_align_divs(ma
);
5439 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5440 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5442 ls
= isl_aff_get_domain_local_space(aff
);
5443 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5444 res
= isl_aff_alloc(ls
);
5453 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5462 isl_multi_aff_free(ma
);
5463 res
= isl_aff_normalize(res
);
5467 isl_multi_aff_free(ma
);
5472 /* Compute the pullback of "aff1" by the function represented by "aff2".
5473 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5474 * defined over the domain space of "aff1".
5476 * The domain of "aff1" should match the range of "aff2", which means
5477 * that it should be single-dimensional.
5479 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5480 __isl_take isl_aff
*aff2
)
5484 ma
= isl_multi_aff_from_aff(aff2
);
5485 return isl_aff_pullback_multi_aff(aff1
, ma
);
5488 /* Compute the pullback of "ma1" by the function represented by "ma2".
5489 * In other words, plug in "ma2" in "ma1".
5491 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5493 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5494 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5497 isl_space
*space
= NULL
;
5499 ma2
= isl_multi_aff_align_divs(ma2
);
5500 ma1
= isl_multi_aff_cow(ma1
);
5504 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5505 isl_multi_aff_get_space(ma1
));
5507 for (i
= 0; i
< ma1
->n
; ++i
) {
5508 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5509 isl_multi_aff_copy(ma2
));
5514 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5515 isl_multi_aff_free(ma2
);
5518 isl_space_free(space
);
5519 isl_multi_aff_free(ma2
);
5520 isl_multi_aff_free(ma1
);
5524 /* Compute the pullback of "ma1" by the function represented by "ma2".
5525 * In other words, plug in "ma2" in "ma1".
5527 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5528 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5530 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5531 &isl_multi_aff_pullback_multi_aff_aligned
);
5534 /* Extend the local space of "dst" to include the divs
5535 * in the local space of "src".
5537 * If "src" does not have any divs or if the local spaces of "dst" and
5538 * "src" are the same, then no extension is required.
5540 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5541 __isl_keep isl_aff
*src
)
5544 int src_n_div
, dst_n_div
;
5551 return isl_aff_free(dst
);
5553 ctx
= isl_aff_get_ctx(src
);
5554 equal
= isl_local_space_has_equal_space(src
->ls
, dst
->ls
);
5556 return isl_aff_free(dst
);
5558 isl_die(ctx
, isl_error_invalid
,
5559 "spaces don't match", goto error
);
5561 src_n_div
= isl_local_space_dim(src
->ls
, isl_dim_div
);
5564 equal
= isl_local_space_is_equal(src
->ls
, dst
->ls
);
5566 return isl_aff_free(dst
);
5570 dst_n_div
= isl_local_space_dim(dst
->ls
, isl_dim_div
);
5571 exp1
= isl_alloc_array(ctx
, int, src_n_div
);
5572 exp2
= isl_alloc_array(ctx
, int, dst_n_div
);
5573 if (!exp1
|| (dst_n_div
&& !exp2
))
5576 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5577 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5585 return isl_aff_free(dst
);
5588 /* Adjust the local spaces of the affine expressions in "maff"
5589 * such that they all have the save divs.
5591 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5592 __isl_take isl_multi_aff
*maff
)
5600 maff
= isl_multi_aff_cow(maff
);
5604 for (i
= 1; i
< maff
->n
; ++i
)
5605 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5606 for (i
= 1; i
< maff
->n
; ++i
) {
5607 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5609 return isl_multi_aff_free(maff
);
5615 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5617 aff
= isl_aff_cow(aff
);
5621 aff
->ls
= isl_local_space_lift(aff
->ls
);
5623 return isl_aff_free(aff
);
5628 /* Lift "maff" to a space with extra dimensions such that the result
5629 * has no more existentially quantified variables.
5630 * If "ls" is not NULL, then *ls is assigned the local space that lies
5631 * at the basis of the lifting applied to "maff".
5633 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5634 __isl_give isl_local_space
**ls
)
5648 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5649 *ls
= isl_local_space_from_space(space
);
5651 return isl_multi_aff_free(maff
);
5656 maff
= isl_multi_aff_cow(maff
);
5657 maff
= isl_multi_aff_align_divs(maff
);
5661 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5662 space
= isl_multi_aff_get_space(maff
);
5663 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5664 space
= isl_space_extend_domain_with_range(space
,
5665 isl_multi_aff_get_space(maff
));
5667 return isl_multi_aff_free(maff
);
5668 isl_space_free(maff
->space
);
5669 maff
->space
= space
;
5672 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5674 return isl_multi_aff_free(maff
);
5677 for (i
= 0; i
< maff
->n
; ++i
) {
5678 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5686 isl_local_space_free(*ls
);
5687 return isl_multi_aff_free(maff
);
5691 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5693 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5694 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5704 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5705 if (pos
< 0 || pos
>= n_out
)
5706 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5707 "index out of bounds", return NULL
);
5709 space
= isl_pw_multi_aff_get_space(pma
);
5710 space
= isl_space_drop_dims(space
, isl_dim_out
,
5711 pos
+ 1, n_out
- pos
- 1);
5712 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5714 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5715 for (i
= 0; i
< pma
->n
; ++i
) {
5717 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5718 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5724 /* Return an isl_pw_multi_aff with the given "set" as domain and
5725 * an unnamed zero-dimensional range.
5727 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5728 __isl_take isl_set
*set
)
5733 space
= isl_set_get_space(set
);
5734 space
= isl_space_from_domain(space
);
5735 ma
= isl_multi_aff_zero(space
);
5736 return isl_pw_multi_aff_alloc(set
, ma
);
5739 /* Add an isl_pw_multi_aff with the given "set" as domain and
5740 * an unnamed zero-dimensional range to *user.
5742 static isl_stat
add_pw_multi_aff_from_domain(__isl_take isl_set
*set
,
5745 isl_union_pw_multi_aff
**upma
= user
;
5746 isl_pw_multi_aff
*pma
;
5748 pma
= isl_pw_multi_aff_from_domain(set
);
5749 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5754 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5755 * an unnamed zero-dimensional range.
5757 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5758 __isl_take isl_union_set
*uset
)
5761 isl_union_pw_multi_aff
*upma
;
5766 space
= isl_union_set_get_space(uset
);
5767 upma
= isl_union_pw_multi_aff_empty(space
);
5769 if (isl_union_set_foreach_set(uset
,
5770 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5773 isl_union_set_free(uset
);
5776 isl_union_set_free(uset
);
5777 isl_union_pw_multi_aff_free(upma
);
5781 /* Convert "pma" to an isl_map and add it to *umap.
5783 static isl_stat
map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
,
5786 isl_union_map
**umap
= user
;
5789 map
= isl_map_from_pw_multi_aff(pma
);
5790 *umap
= isl_union_map_add_map(*umap
, map
);
5795 /* Construct a union map mapping the domain of the union
5796 * piecewise multi-affine expression to its range, with each dimension
5797 * in the range equated to the corresponding affine expression on its cell.
5799 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5800 __isl_take isl_union_pw_multi_aff
*upma
)
5803 isl_union_map
*umap
;
5808 space
= isl_union_pw_multi_aff_get_space(upma
);
5809 umap
= isl_union_map_empty(space
);
5811 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5812 &map_from_pw_multi_aff
, &umap
) < 0)
5815 isl_union_pw_multi_aff_free(upma
);
5818 isl_union_pw_multi_aff_free(upma
);
5819 isl_union_map_free(umap
);
5823 /* Local data for bin_entry and the callback "fn".
5825 struct isl_union_pw_multi_aff_bin_data
{
5826 isl_union_pw_multi_aff
*upma2
;
5827 isl_union_pw_multi_aff
*res
;
5828 isl_pw_multi_aff
*pma
;
5829 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
);
5832 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5833 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5835 static isl_stat
bin_entry(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5837 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5841 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma2
,
5843 isl_pw_multi_aff_free(pma
);
5848 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5849 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5850 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5851 * as *entry. The callback should adjust data->res if desired.
5853 static __isl_give isl_union_pw_multi_aff
*bin_op(
5854 __isl_take isl_union_pw_multi_aff
*upma1
,
5855 __isl_take isl_union_pw_multi_aff
*upma2
,
5856 isl_stat (*fn
)(__isl_take isl_pw_multi_aff
*pma
, void *user
))
5859 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5861 space
= isl_union_pw_multi_aff_get_space(upma2
);
5862 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5863 space
= isl_union_pw_multi_aff_get_space(upma1
);
5864 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5866 if (!upma1
|| !upma2
)
5870 data
.res
= isl_union_pw_multi_aff_alloc_same_size(upma1
);
5871 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1
,
5872 &bin_entry
, &data
) < 0)
5875 isl_union_pw_multi_aff_free(upma1
);
5876 isl_union_pw_multi_aff_free(upma2
);
5879 isl_union_pw_multi_aff_free(upma1
);
5880 isl_union_pw_multi_aff_free(upma2
);
5881 isl_union_pw_multi_aff_free(data
.res
);
5885 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5886 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5888 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5889 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5893 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5894 isl_pw_multi_aff_get_space(pma2
));
5895 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5896 &isl_multi_aff_range_product
);
5899 /* Given two isl_pw_multi_affs A -> B and C -> D,
5900 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5902 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5903 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5905 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5906 &pw_multi_aff_range_product
);
5909 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5910 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5912 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5913 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5917 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5918 isl_pw_multi_aff_get_space(pma2
));
5919 space
= isl_space_flatten_range(space
);
5920 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5921 &isl_multi_aff_flat_range_product
);
5924 /* Given two isl_pw_multi_affs A -> B and C -> D,
5925 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5927 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5928 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5930 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5931 &pw_multi_aff_flat_range_product
);
5934 /* If data->pma and "pma2" have the same domain space, then compute
5935 * their flat range product and the result to data->res.
5937 static isl_stat
flat_range_product_entry(__isl_take isl_pw_multi_aff
*pma2
,
5940 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5942 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5943 pma2
->dim
, isl_dim_in
)) {
5944 isl_pw_multi_aff_free(pma2
);
5948 pma2
= isl_pw_multi_aff_flat_range_product(
5949 isl_pw_multi_aff_copy(data
->pma
), pma2
);
5951 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5956 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5957 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5959 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5960 __isl_take isl_union_pw_multi_aff
*upma1
,
5961 __isl_take isl_union_pw_multi_aff
*upma2
)
5963 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5966 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5967 * The parameters are assumed to have been aligned.
5969 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5970 * except that it works on two different isl_pw_* types.
5972 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5973 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5974 __isl_take isl_pw_aff
*pa
)
5977 isl_pw_multi_aff
*res
= NULL
;
5982 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5983 pa
->dim
, isl_dim_in
))
5984 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5985 "domains don't match", goto error
);
5986 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5987 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5988 "index out of bounds", goto error
);
5991 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5993 for (i
= 0; i
< pma
->n
; ++i
) {
5994 for (j
= 0; j
< pa
->n
; ++j
) {
5996 isl_multi_aff
*res_ij
;
5999 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
6000 isl_set_copy(pa
->p
[j
].set
));
6001 empty
= isl_set_plain_is_empty(common
);
6002 if (empty
< 0 || empty
) {
6003 isl_set_free(common
);
6009 res_ij
= isl_multi_aff_set_aff(
6010 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
6011 isl_aff_copy(pa
->p
[j
].aff
));
6012 res_ij
= isl_multi_aff_gist(res_ij
,
6013 isl_set_copy(common
));
6015 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
6019 isl_pw_multi_aff_free(pma
);
6020 isl_pw_aff_free(pa
);
6023 isl_pw_multi_aff_free(pma
);
6024 isl_pw_aff_free(pa
);
6025 return isl_pw_multi_aff_free(res
);
6028 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6030 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
6031 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
6032 __isl_take isl_pw_aff
*pa
)
6036 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
6037 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6038 if (!isl_space_has_named_params(pma
->dim
) ||
6039 !isl_space_has_named_params(pa
->dim
))
6040 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6041 "unaligned unnamed parameters", goto error
);
6042 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
6043 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
6044 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
6046 isl_pw_multi_aff_free(pma
);
6047 isl_pw_aff_free(pa
);
6051 /* Do the parameters of "pa" match those of "space"?
6053 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
6054 __isl_keep isl_space
*space
)
6056 isl_space
*pa_space
;
6062 pa_space
= isl_pw_aff_get_space(pa
);
6064 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6066 isl_space_free(pa_space
);
6070 /* Check that the domain space of "pa" matches "space".
6072 * Return 0 on success and -1 on error.
6074 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
6075 __isl_keep isl_space
*space
)
6077 isl_space
*pa_space
;
6083 pa_space
= isl_pw_aff_get_space(pa
);
6085 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
6089 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6090 "parameters don't match", goto error
);
6091 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
6092 pa_space
, isl_dim_in
);
6096 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
6097 "domains don't match", goto error
);
6098 isl_space_free(pa_space
);
6101 isl_space_free(pa_space
);
6110 #include <isl_multi_templ.c>
6111 #include <isl_multi_apply_set.c>
6112 #include <isl_multi_coalesce.c>
6113 #include <isl_multi_gist.c>
6114 #include <isl_multi_hash.c>
6115 #include <isl_multi_intersect.c>
6117 /* Scale the elements of "pma" by the corresponding elements of "mv".
6119 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
6120 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
6124 pma
= isl_pw_multi_aff_cow(pma
);
6127 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6128 mv
->space
, isl_dim_set
))
6129 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
6130 "spaces don't match", goto error
);
6131 if (!isl_space_match(pma
->dim
, isl_dim_param
,
6132 mv
->space
, isl_dim_param
)) {
6133 pma
= isl_pw_multi_aff_align_params(pma
,
6134 isl_multi_val_get_space(mv
));
6135 mv
= isl_multi_val_align_params(mv
,
6136 isl_pw_multi_aff_get_space(pma
));
6141 for (i
= 0; i
< pma
->n
; ++i
) {
6142 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
6143 isl_multi_val_copy(mv
));
6144 if (!pma
->p
[i
].maff
)
6148 isl_multi_val_free(mv
);
6151 isl_multi_val_free(mv
);
6152 isl_pw_multi_aff_free(pma
);
6156 /* This function is called for each entry of an isl_union_pw_multi_aff.
6157 * If the space of the entry matches that of data->mv,
6158 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6159 * Otherwise, return an empty isl_pw_multi_aff.
6161 static __isl_give isl_pw_multi_aff
*union_pw_multi_aff_scale_multi_val_entry(
6162 __isl_take isl_pw_multi_aff
*pma
, void *user
)
6164 isl_multi_val
*mv
= user
;
6168 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
6169 mv
->space
, isl_dim_set
)) {
6170 isl_space
*space
= isl_pw_multi_aff_get_space(pma
);
6171 isl_pw_multi_aff_free(pma
);
6172 return isl_pw_multi_aff_empty(space
);
6175 return isl_pw_multi_aff_scale_multi_val(pma
, isl_multi_val_copy(mv
));
6178 /* Scale the elements of "upma" by the corresponding elements of "mv",
6179 * for those entries that match the space of "mv".
6181 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6182 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6184 upma
= isl_union_pw_multi_aff_align_params(upma
,
6185 isl_multi_val_get_space(mv
));
6186 mv
= isl_multi_val_align_params(mv
,
6187 isl_union_pw_multi_aff_get_space(upma
));
6191 return isl_union_pw_multi_aff_transform(upma
,
6192 &union_pw_multi_aff_scale_multi_val_entry
, mv
);
6194 isl_multi_val_free(mv
);
6197 isl_multi_val_free(mv
);
6198 isl_union_pw_multi_aff_free(upma
);
6202 /* Construct and return a piecewise multi affine expression
6203 * in the given space with value zero in each of the output dimensions and
6204 * a universe domain.
6206 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6208 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6211 /* Construct and return a piecewise multi affine expression
6212 * that is equal to the given piecewise affine expression.
6214 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6215 __isl_take isl_pw_aff
*pa
)
6219 isl_pw_multi_aff
*pma
;
6224 space
= isl_pw_aff_get_space(pa
);
6225 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6227 for (i
= 0; i
< pa
->n
; ++i
) {
6231 set
= isl_set_copy(pa
->p
[i
].set
);
6232 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6233 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6236 isl_pw_aff_free(pa
);
6240 /* Construct a set or map mapping the shared (parameter) domain
6241 * of the piecewise affine expressions to the range of "mpa"
6242 * with each dimension in the range equated to the
6243 * corresponding piecewise affine expression.
6245 static __isl_give isl_map
*map_from_multi_pw_aff(
6246 __isl_take isl_multi_pw_aff
*mpa
)
6255 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6256 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6257 "invalid space", goto error
);
6259 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6260 map
= isl_map_universe(isl_space_from_domain(space
));
6262 for (i
= 0; i
< mpa
->n
; ++i
) {
6266 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6267 map_i
= map_from_pw_aff(pa
);
6269 map
= isl_map_flat_range_product(map
, map_i
);
6272 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6274 isl_multi_pw_aff_free(mpa
);
6277 isl_multi_pw_aff_free(mpa
);
6281 /* Construct a map mapping the shared domain
6282 * of the piecewise affine expressions to the range of "mpa"
6283 * with each dimension in the range equated to the
6284 * corresponding piecewise affine expression.
6286 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6290 if (isl_space_is_set(mpa
->space
))
6291 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6292 "space of input is not a map", goto error
);
6294 return map_from_multi_pw_aff(mpa
);
6296 isl_multi_pw_aff_free(mpa
);
6300 /* Construct a set mapping the shared parameter domain
6301 * of the piecewise affine expressions to the space of "mpa"
6302 * with each dimension in the range equated to the
6303 * corresponding piecewise affine expression.
6305 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6309 if (!isl_space_is_set(mpa
->space
))
6310 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6311 "space of input is not a set", goto error
);
6313 return map_from_multi_pw_aff(mpa
);
6315 isl_multi_pw_aff_free(mpa
);
6319 /* Construct and return a piecewise multi affine expression
6320 * that is equal to the given multi piecewise affine expression
6321 * on the shared domain of the piecewise affine expressions.
6323 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6324 __isl_take isl_multi_pw_aff
*mpa
)
6329 isl_pw_multi_aff
*pma
;
6334 space
= isl_multi_pw_aff_get_space(mpa
);
6337 isl_multi_pw_aff_free(mpa
);
6338 return isl_pw_multi_aff_zero(space
);
6341 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6342 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6344 for (i
= 1; i
< mpa
->n
; ++i
) {
6345 isl_pw_multi_aff
*pma_i
;
6347 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6348 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6349 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6352 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6354 isl_multi_pw_aff_free(mpa
);
6358 /* Construct and return a multi piecewise affine expression
6359 * that is equal to the given multi affine expression.
6361 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6362 __isl_take isl_multi_aff
*ma
)
6365 isl_multi_pw_aff
*mpa
;
6370 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6371 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6373 for (i
= 0; i
< n
; ++i
) {
6376 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6377 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6380 isl_multi_aff_free(ma
);
6384 /* Construct and return a multi piecewise affine expression
6385 * that is equal to the given piecewise multi affine expression.
6387 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6388 __isl_take isl_pw_multi_aff
*pma
)
6392 isl_multi_pw_aff
*mpa
;
6397 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6398 space
= isl_pw_multi_aff_get_space(pma
);
6399 mpa
= isl_multi_pw_aff_alloc(space
);
6401 for (i
= 0; i
< n
; ++i
) {
6404 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6405 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6408 isl_pw_multi_aff_free(pma
);
6412 /* Do "pa1" and "pa2" represent the same function?
6414 * We first check if they are obviously equal.
6415 * If not, we convert them to maps and check if those are equal.
6417 * If "pa1" or "pa2" contain any NaNs, then they are considered
6418 * not to be the same. A NaN is not equal to anything, not even
6421 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6425 isl_map
*map1
, *map2
;
6430 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6431 if (equal
< 0 || equal
)
6433 has_nan
= isl_pw_aff_involves_nan(pa1
);
6434 if (has_nan
>= 0 && !has_nan
)
6435 has_nan
= isl_pw_aff_involves_nan(pa2
);
6441 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6442 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6443 equal
= isl_map_is_equal(map1
, map2
);
6450 /* Do "mpa1" and "mpa2" represent the same function?
6452 * Note that we cannot convert the entire isl_multi_pw_aff
6453 * to a map because the domains of the piecewise affine expressions
6454 * may not be the same.
6456 isl_bool
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6457 __isl_keep isl_multi_pw_aff
*mpa2
)
6463 return isl_bool_error
;
6465 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6466 mpa2
->space
, isl_dim_param
)) {
6467 if (!isl_space_has_named_params(mpa1
->space
))
6468 return isl_bool_false
;
6469 if (!isl_space_has_named_params(mpa2
->space
))
6470 return isl_bool_false
;
6471 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6472 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6473 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6474 isl_multi_pw_aff_get_space(mpa2
));
6475 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6476 isl_multi_pw_aff_get_space(mpa1
));
6477 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6478 isl_multi_pw_aff_free(mpa1
);
6479 isl_multi_pw_aff_free(mpa2
);
6483 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6484 if (equal
< 0 || !equal
)
6487 for (i
= 0; i
< mpa1
->n
; ++i
) {
6488 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6489 if (equal
< 0 || !equal
)
6493 return isl_bool_true
;
6496 /* Compute the pullback of "mpa" by the function represented by "ma".
6497 * In other words, plug in "ma" in "mpa".
6499 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6501 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6502 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6505 isl_space
*space
= NULL
;
6507 mpa
= isl_multi_pw_aff_cow(mpa
);
6511 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6512 isl_multi_pw_aff_get_space(mpa
));
6516 for (i
= 0; i
< mpa
->n
; ++i
) {
6517 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6518 isl_multi_aff_copy(ma
));
6523 isl_multi_aff_free(ma
);
6524 isl_space_free(mpa
->space
);
6528 isl_space_free(space
);
6529 isl_multi_pw_aff_free(mpa
);
6530 isl_multi_aff_free(ma
);
6534 /* Compute the pullback of "mpa" by the function represented by "ma".
6535 * In other words, plug in "ma" in "mpa".
6537 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6538 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6542 if (isl_space_match(mpa
->space
, isl_dim_param
,
6543 ma
->space
, isl_dim_param
))
6544 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6545 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6546 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6547 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6549 isl_multi_pw_aff_free(mpa
);
6550 isl_multi_aff_free(ma
);
6554 /* Compute the pullback of "mpa" by the function represented by "pma".
6555 * In other words, plug in "pma" in "mpa".
6557 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6559 static __isl_give isl_multi_pw_aff
*
6560 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6561 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6564 isl_space
*space
= NULL
;
6566 mpa
= isl_multi_pw_aff_cow(mpa
);
6570 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6571 isl_multi_pw_aff_get_space(mpa
));
6573 for (i
= 0; i
< mpa
->n
; ++i
) {
6574 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6575 isl_pw_multi_aff_copy(pma
));
6580 isl_pw_multi_aff_free(pma
);
6581 isl_space_free(mpa
->space
);
6585 isl_space_free(space
);
6586 isl_multi_pw_aff_free(mpa
);
6587 isl_pw_multi_aff_free(pma
);
6591 /* Compute the pullback of "mpa" by the function represented by "pma".
6592 * In other words, plug in "pma" in "mpa".
6594 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6595 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6599 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6600 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6601 mpa
= isl_multi_pw_aff_align_params(mpa
,
6602 isl_pw_multi_aff_get_space(pma
));
6603 pma
= isl_pw_multi_aff_align_params(pma
,
6604 isl_multi_pw_aff_get_space(mpa
));
6605 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6607 isl_multi_pw_aff_free(mpa
);
6608 isl_pw_multi_aff_free(pma
);
6612 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6613 * with the domain of "aff". The domain of the result is the same
6615 * "mpa" and "aff" are assumed to have been aligned.
6617 * We first extract the parametric constant from "aff", defined
6618 * over the correct domain.
6619 * Then we add the appropriate combinations of the members of "mpa".
6620 * Finally, we add the integer divisions through recursive calls.
6622 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6623 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6631 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6632 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6634 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6635 tmp
= isl_aff_copy(aff
);
6636 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6637 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6638 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6639 isl_space_dim(space
, isl_dim_set
));
6640 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6641 pa
= isl_pw_aff_from_aff(tmp
);
6643 for (i
= 0; i
< n_in
; ++i
) {
6646 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6648 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6649 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6650 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6651 pa
= isl_pw_aff_add(pa
, pa_i
);
6654 for (i
= 0; i
< n_div
; ++i
) {
6658 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6660 div
= isl_aff_get_div(aff
, i
);
6661 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6662 isl_multi_pw_aff_copy(mpa
), div
);
6663 pa_i
= isl_pw_aff_floor(pa_i
);
6664 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6665 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6666 pa
= isl_pw_aff_add(pa
, pa_i
);
6669 isl_multi_pw_aff_free(mpa
);
6675 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6676 * with the domain of "aff". The domain of the result is the same
6679 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6680 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6684 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6685 mpa
->space
, isl_dim_param
))
6686 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6688 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6689 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6691 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6694 isl_multi_pw_aff_free(mpa
);
6698 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6699 * with the domain of "pa". The domain of the result is the same
6701 * "mpa" and "pa" are assumed to have been aligned.
6703 * We consider each piece in turn. Note that the domains of the
6704 * pieces are assumed to be disjoint and they remain disjoint
6705 * after taking the preimage (over the same function).
6707 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6708 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6717 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6718 isl_pw_aff_get_space(pa
));
6719 res
= isl_pw_aff_empty(space
);
6721 for (i
= 0; i
< pa
->n
; ++i
) {
6725 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6726 isl_multi_pw_aff_copy(mpa
),
6727 isl_aff_copy(pa
->p
[i
].aff
));
6728 domain
= isl_set_copy(pa
->p
[i
].set
);
6729 domain
= isl_set_preimage_multi_pw_aff(domain
,
6730 isl_multi_pw_aff_copy(mpa
));
6731 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6732 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6735 isl_pw_aff_free(pa
);
6736 isl_multi_pw_aff_free(mpa
);
6739 isl_pw_aff_free(pa
);
6740 isl_multi_pw_aff_free(mpa
);
6744 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6745 * with the domain of "pa". The domain of the result is the same
6748 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6749 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6753 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6754 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6756 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6757 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6759 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6761 isl_pw_aff_free(pa
);
6762 isl_multi_pw_aff_free(mpa
);
6766 /* Compute the pullback of "pa" by the function represented by "mpa".
6767 * In other words, plug in "mpa" in "pa".
6768 * "pa" and "mpa" are assumed to have been aligned.
6770 * The pullback is computed by applying "pa" to "mpa".
6772 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6773 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6775 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6778 /* Compute the pullback of "pa" by the function represented by "mpa".
6779 * In other words, plug in "mpa" in "pa".
6781 * The pullback is computed by applying "pa" to "mpa".
6783 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6784 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6786 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6789 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6790 * In other words, plug in "mpa2" in "mpa1".
6792 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6794 * We pullback each member of "mpa1" in turn.
6796 static __isl_give isl_multi_pw_aff
*
6797 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6798 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6801 isl_space
*space
= NULL
;
6803 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6807 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6808 isl_multi_pw_aff_get_space(mpa1
));
6810 for (i
= 0; i
< mpa1
->n
; ++i
) {
6811 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6812 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6817 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6819 isl_multi_pw_aff_free(mpa2
);
6822 isl_space_free(space
);
6823 isl_multi_pw_aff_free(mpa1
);
6824 isl_multi_pw_aff_free(mpa2
);
6828 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6829 * In other words, plug in "mpa2" in "mpa1".
6831 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6832 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6834 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6835 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6838 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6839 * of "mpa1" and "mpa2" live in the same space, construct map space
6840 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6841 * with this map space as extract argument.
6843 static __isl_give isl_map
*isl_multi_pw_aff_order_map(
6844 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6845 __isl_give isl_map
*(*order
)(__isl_keep isl_multi_pw_aff
*mpa1
,
6846 __isl_keep isl_multi_pw_aff
*mpa2
, __isl_take isl_space
*space
))
6849 isl_space
*space1
, *space2
;
6852 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6853 isl_multi_pw_aff_get_space(mpa2
));
6854 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6855 isl_multi_pw_aff_get_space(mpa1
));
6858 match
= isl_space_tuple_is_equal(mpa1
->space
, isl_dim_out
,
6859 mpa2
->space
, isl_dim_out
);
6863 isl_die(isl_multi_pw_aff_get_ctx(mpa1
), isl_error_invalid
,
6864 "range spaces don't match", goto error
);
6865 space1
= isl_space_domain(isl_multi_pw_aff_get_space(mpa1
));
6866 space2
= isl_space_domain(isl_multi_pw_aff_get_space(mpa2
));
6867 space1
= isl_space_map_from_domain_and_range(space1
, space2
);
6869 res
= order(mpa1
, mpa2
, space1
);
6870 isl_multi_pw_aff_free(mpa1
);
6871 isl_multi_pw_aff_free(mpa2
);
6874 isl_multi_pw_aff_free(mpa1
);
6875 isl_multi_pw_aff_free(mpa2
);
6879 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6880 * where the function values are equal. "space" is the space of the result.
6881 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6883 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6884 * in the sequences are equal.
6886 static __isl_give isl_map
*isl_multi_pw_aff_eq_map_on_space(
6887 __isl_keep isl_multi_pw_aff
*mpa1
, __isl_keep isl_multi_pw_aff
*mpa2
,
6888 __isl_take isl_space
*space
)
6893 res
= isl_map_universe(space
);
6895 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6896 for (i
= 0; i
< n
; ++i
) {
6897 isl_pw_aff
*pa1
, *pa2
;
6900 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6901 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6902 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6903 res
= isl_map_intersect(res
, map
);
6909 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6910 * where the function values are equal.
6912 __isl_give isl_map
*isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff
*mpa1
,
6913 __isl_take isl_multi_pw_aff
*mpa2
)
6915 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6916 &isl_multi_pw_aff_eq_map_on_space
);
6919 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6920 * where the function values of "mpa1" is lexicographically satisfies "base"
6921 * compared to that of "mpa2". "space" is the space of the result.
6922 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6924 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6925 * if its i-th element satisfies "base" when compared to
6926 * the i-th element of "mpa2" while all previous elements are
6929 static __isl_give isl_map
*isl_multi_pw_aff_lex_map_on_space(
6930 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6931 __isl_give isl_map
*(*base
)(__isl_take isl_pw_aff
*pa1
,
6932 __isl_take isl_pw_aff
*pa2
),
6933 __isl_take isl_space
*space
)
6936 isl_map
*res
, *rest
;
6938 res
= isl_map_empty(isl_space_copy(space
));
6939 rest
= isl_map_universe(space
);
6941 n
= isl_multi_pw_aff_dim(mpa1
, isl_dim_out
);
6942 for (i
= 0; i
< n
; ++i
) {
6943 isl_pw_aff
*pa1
, *pa2
;
6946 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6947 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6948 map
= base(pa1
, pa2
);
6949 map
= isl_map_intersect(map
, isl_map_copy(rest
));
6950 res
= isl_map_union(res
, map
);
6955 pa1
= isl_multi_pw_aff_get_pw_aff(mpa1
, i
);
6956 pa2
= isl_multi_pw_aff_get_pw_aff(mpa2
, i
);
6957 map
= isl_pw_aff_eq_map(pa1
, pa2
);
6958 rest
= isl_map_intersect(rest
, map
);
6965 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6966 * where the function value of "mpa1" is lexicographically less than that
6967 * of "mpa2". "space" is the space of the result.
6968 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6970 * "mpa1" is less than "mpa2" if its i-th element is smaller
6971 * than the i-th element of "mpa2" while all previous elements are
6974 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map_on_space(
6975 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
6976 __isl_take isl_space
*space
)
6978 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
6979 &isl_pw_aff_lt_map
, space
);
6982 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6983 * where the function value of "mpa1" is lexicographically less than that
6986 __isl_give isl_map
*isl_multi_pw_aff_lex_lt_map(
6987 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6989 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
6990 &isl_multi_pw_aff_lex_lt_map_on_space
);
6993 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6994 * where the function value of "mpa1" is lexicographically greater than that
6995 * of "mpa2". "space" is the space of the result.
6996 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6998 * "mpa1" is greater than "mpa2" if its i-th element is greater
6999 * than the i-th element of "mpa2" while all previous elements are
7002 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map_on_space(
7003 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
,
7004 __isl_take isl_space
*space
)
7006 return isl_multi_pw_aff_lex_map_on_space(mpa1
, mpa2
,
7007 &isl_pw_aff_gt_map
, space
);
7010 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7011 * where the function value of "mpa1" is lexicographically greater than that
7014 __isl_give isl_map
*isl_multi_pw_aff_lex_gt_map(
7015 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
7017 return isl_multi_pw_aff_order_map(mpa1
, mpa2
,
7018 &isl_multi_pw_aff_lex_gt_map_on_space
);
7021 /* Compare two isl_affs.
7023 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7024 * than "aff2" and 0 if they are equal.
7026 * The order is fairly arbitrary. We do consider expressions that only involve
7027 * earlier dimensions as "smaller".
7029 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
7042 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
7046 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
7047 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
7049 return last1
- last2
;
7051 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
7054 /* Compare two isl_pw_affs.
7056 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7057 * than "pa2" and 0 if they are equal.
7059 * The order is fairly arbitrary. We do consider expressions that only involve
7060 * earlier dimensions as "smaller".
7062 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
7063 __isl_keep isl_pw_aff
*pa2
)
7076 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
7080 if (pa1
->n
!= pa2
->n
)
7081 return pa1
->n
- pa2
->n
;
7083 for (i
= 0; i
< pa1
->n
; ++i
) {
7084 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
7087 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
7095 /* Return a piecewise affine expression that is equal to "v" on "domain".
7097 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
7098 __isl_take isl_val
*v
)
7101 isl_local_space
*ls
;
7104 space
= isl_set_get_space(domain
);
7105 ls
= isl_local_space_from_space(space
);
7106 aff
= isl_aff_val_on_domain(ls
, v
);
7108 return isl_pw_aff_alloc(domain
, aff
);
7111 /* Return a multi affine expression that is equal to "mv" on domain
7114 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
7115 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
7119 isl_local_space
*ls
;
7125 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7126 space2
= isl_multi_val_get_space(mv
);
7127 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
7128 space
= isl_space_align_params(space
, isl_space_copy(space2
));
7129 space
= isl_space_map_from_domain_and_range(space
, space2
);
7130 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
7131 ls
= isl_local_space_from_space(isl_space_domain(space
));
7132 for (i
= 0; i
< n
; ++i
) {
7136 v
= isl_multi_val_get_val(mv
, i
);
7137 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
7138 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
7140 isl_local_space_free(ls
);
7142 isl_multi_val_free(mv
);
7145 isl_space_free(space
);
7146 isl_multi_val_free(mv
);
7150 /* Return a piecewise multi-affine expression
7151 * that is equal to "mv" on "domain".
7153 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
7154 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
7159 space
= isl_set_get_space(domain
);
7160 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
7162 return isl_pw_multi_aff_alloc(domain
, ma
);
7165 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7166 * mv is the value that should be attained on each domain set
7167 * res collects the results
7169 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
7171 isl_union_pw_multi_aff
*res
;
7174 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7175 * and add it to data->res.
7177 static isl_stat
pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
7180 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
7181 isl_pw_multi_aff
*pma
;
7184 mv
= isl_multi_val_copy(data
->mv
);
7185 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
7186 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
7188 return data
->res
? isl_stat_ok
: isl_stat_error
;
7191 /* Return a union piecewise multi-affine expression
7192 * that is equal to "mv" on "domain".
7194 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
7195 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7197 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
7200 space
= isl_union_set_get_space(domain
);
7201 data
.res
= isl_union_pw_multi_aff_empty(space
);
7203 if (isl_union_set_foreach_set(domain
,
7204 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
7205 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
7206 isl_union_set_free(domain
);
7207 isl_multi_val_free(mv
);
7211 /* Compute the pullback of data->pma by the function represented by "pma2",
7212 * provided the spaces match, and add the results to data->res.
7214 static isl_stat
pullback_entry(__isl_take isl_pw_multi_aff
*pma2
, void *user
)
7216 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
7218 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
7219 pma2
->dim
, isl_dim_out
)) {
7220 isl_pw_multi_aff_free(pma2
);
7224 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
7225 isl_pw_multi_aff_copy(data
->pma
), pma2
);
7227 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
7229 return isl_stat_error
;
7234 /* Compute the pullback of "upma1" by the function represented by "upma2".
7236 __isl_give isl_union_pw_multi_aff
*
7237 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7238 __isl_take isl_union_pw_multi_aff
*upma1
,
7239 __isl_take isl_union_pw_multi_aff
*upma2
)
7241 return bin_op(upma1
, upma2
, &pullback_entry
);
7244 /* Check that the domain space of "upa" matches "space".
7246 * Return 0 on success and -1 on error.
7248 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7249 * can in principle never fail since the space "space" is that
7250 * of the isl_multi_union_pw_aff and is a set space such that
7251 * there is no domain space to match.
7253 * We check the parameters and double-check that "space" is
7254 * indeed that of a set.
7256 static int isl_union_pw_aff_check_match_domain_space(
7257 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
7259 isl_space
*upa_space
;
7265 match
= isl_space_is_set(space
);
7269 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7270 "expecting set space", return -1);
7272 upa_space
= isl_union_pw_aff_get_space(upa
);
7273 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7277 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7278 "parameters don't match", goto error
);
7280 isl_space_free(upa_space
);
7283 isl_space_free(upa_space
);
7287 /* Do the parameters of "upa" match those of "space"?
7289 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
7290 __isl_keep isl_space
*space
)
7292 isl_space
*upa_space
;
7298 upa_space
= isl_union_pw_aff_get_space(upa
);
7300 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
7302 isl_space_free(upa_space
);
7306 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7307 * space represents the new parameters.
7308 * res collects the results.
7310 struct isl_union_pw_aff_reset_params_data
{
7312 isl_union_pw_aff
*res
;
7315 /* Replace the parameters of "pa" by data->space and
7316 * add the result to data->res.
7318 static isl_stat
reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
7320 struct isl_union_pw_aff_reset_params_data
*data
= user
;
7323 space
= isl_pw_aff_get_space(pa
);
7324 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
7325 pa
= isl_pw_aff_reset_space(pa
, space
);
7326 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7328 return data
->res
? isl_stat_ok
: isl_stat_error
;
7331 /* Replace the domain space of "upa" by "space".
7332 * Since a union expression does not have a (single) domain space,
7333 * "space" is necessarily a parameter space.
7335 * Since the order and the names of the parameters determine
7336 * the hash value, we need to create a new hash table.
7338 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7339 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7341 struct isl_union_pw_aff_reset_params_data data
= { space
};
7344 match
= isl_union_pw_aff_matching_params(upa
, space
);
7346 upa
= isl_union_pw_aff_free(upa
);
7348 isl_space_free(space
);
7352 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7353 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7354 data
.res
= isl_union_pw_aff_free(data
.res
);
7356 isl_union_pw_aff_free(upa
);
7357 isl_space_free(space
);
7361 /* Return the floor of "pa".
7363 static __isl_give isl_pw_aff
*floor_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7365 return isl_pw_aff_floor(pa
);
7368 /* Given f, return floor(f).
7370 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7371 __isl_take isl_union_pw_aff
*upa
)
7373 return isl_union_pw_aff_transform_inplace(upa
, &floor_entry
, NULL
);
7378 * upa mod m = upa - m * floor(upa/m)
7380 * with m an integer value.
7382 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7383 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7385 isl_union_pw_aff
*res
;
7390 if (!isl_val_is_int(m
))
7391 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7392 "expecting integer modulo", goto error
);
7393 if (!isl_val_is_pos(m
))
7394 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7395 "expecting positive modulo", goto error
);
7397 res
= isl_union_pw_aff_copy(upa
);
7398 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7399 upa
= isl_union_pw_aff_floor(upa
);
7400 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7401 res
= isl_union_pw_aff_sub(res
, upa
);
7406 isl_union_pw_aff_free(upa
);
7410 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7411 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7413 * "res" collects the results.
7415 struct isl_union_pw_aff_aff_on_domain_data
{
7417 isl_union_pw_aff
*res
;
7420 /* Construct a piecewise affine expression that is equal to data->aff
7421 * on "domain" and add the result to data->res.
7423 static isl_stat
pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7425 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7430 aff
= isl_aff_copy(data
->aff
);
7431 dim
= isl_set_dim(domain
, isl_dim_set
);
7432 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7433 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7434 pa
= isl_pw_aff_alloc(domain
, aff
);
7435 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7437 return data
->res
? isl_stat_ok
: isl_stat_error
;
7440 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7441 * pos is the output position that needs to be extracted.
7442 * res collects the results.
7444 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7446 isl_union_pw_aff
*res
;
7449 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7450 * (assuming it has such a dimension) and add it to data->res.
7452 static isl_stat
get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7454 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7459 return isl_stat_error
;
7461 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7462 if (data
->pos
>= n_out
) {
7463 isl_pw_multi_aff_free(pma
);
7467 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7468 isl_pw_multi_aff_free(pma
);
7470 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7472 return data
->res
? isl_stat_ok
: isl_stat_error
;
7475 /* Extract an isl_union_pw_aff corresponding to
7476 * output dimension "pos" of "upma".
7478 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7479 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7481 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7488 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7489 "cannot extract at negative position", return NULL
);
7491 space
= isl_union_pw_multi_aff_get_space(upma
);
7492 data
.res
= isl_union_pw_aff_empty(space
);
7494 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7495 &get_union_pw_aff
, &data
) < 0)
7496 data
.res
= isl_union_pw_aff_free(data
.res
);
7501 /* Return a union piecewise affine expression
7502 * that is equal to "aff" on "domain".
7504 * Construct an isl_pw_aff on each of the sets in "domain" and
7505 * collect the results.
7507 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7508 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7510 struct isl_union_pw_aff_aff_on_domain_data data
;
7513 if (!domain
|| !aff
)
7515 if (!isl_local_space_is_params(aff
->ls
))
7516 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7517 "expecting parametric expression", goto error
);
7519 space
= isl_union_set_get_space(domain
);
7520 data
.res
= isl_union_pw_aff_empty(space
);
7522 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7523 data
.res
= isl_union_pw_aff_free(data
.res
);
7524 isl_union_set_free(domain
);
7528 isl_union_set_free(domain
);
7533 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7534 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7535 * "res" collects the results.
7537 struct isl_union_pw_aff_val_on_domain_data
{
7539 isl_union_pw_aff
*res
;
7542 /* Construct a piecewise affine expression that is equal to data->v
7543 * on "domain" and add the result to data->res.
7545 static isl_stat
pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7547 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7551 v
= isl_val_copy(data
->v
);
7552 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7553 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7555 return data
->res
? isl_stat_ok
: isl_stat_error
;
7558 /* Return a union piecewise affine expression
7559 * that is equal to "v" on "domain".
7561 * Construct an isl_pw_aff on each of the sets in "domain" and
7562 * collect the results.
7564 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7565 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7567 struct isl_union_pw_aff_val_on_domain_data data
;
7570 space
= isl_union_set_get_space(domain
);
7571 data
.res
= isl_union_pw_aff_empty(space
);
7573 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7574 data
.res
= isl_union_pw_aff_free(data
.res
);
7575 isl_union_set_free(domain
);
7580 /* Construct a piecewise multi affine expression
7581 * that is equal to "pa" and add it to upma.
7583 static isl_stat
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
,
7586 isl_union_pw_multi_aff
**upma
= user
;
7587 isl_pw_multi_aff
*pma
;
7589 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7590 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7592 return *upma
? isl_stat_ok
: isl_stat_error
;
7595 /* Construct and return a union piecewise multi affine expression
7596 * that is equal to the given union piecewise affine expression.
7598 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7599 __isl_take isl_union_pw_aff
*upa
)
7602 isl_union_pw_multi_aff
*upma
;
7607 space
= isl_union_pw_aff_get_space(upa
);
7608 upma
= isl_union_pw_multi_aff_empty(space
);
7610 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7611 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7612 upma
= isl_union_pw_multi_aff_free(upma
);
7614 isl_union_pw_aff_free(upa
);
7618 /* Compute the set of elements in the domain of "pa" where it is zero and
7619 * add this set to "uset".
7621 static isl_stat
zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7623 isl_union_set
**uset
= (isl_union_set
**)user
;
7625 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7627 return *uset
? isl_stat_ok
: isl_stat_error
;
7630 /* Return a union set containing those elements in the domain
7631 * of "upa" where it is zero.
7633 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7634 __isl_take isl_union_pw_aff
*upa
)
7636 isl_union_set
*zero
;
7638 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7639 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7640 zero
= isl_union_set_free(zero
);
7642 isl_union_pw_aff_free(upa
);
7646 /* Convert "pa" to an isl_map and add it to *umap.
7648 static isl_stat
map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7650 isl_union_map
**umap
= user
;
7653 map
= isl_map_from_pw_aff(pa
);
7654 *umap
= isl_union_map_add_map(*umap
, map
);
7656 return *umap
? isl_stat_ok
: isl_stat_error
;
7659 /* Construct a union map mapping the domain of the union
7660 * piecewise affine expression to its range, with the single output dimension
7661 * equated to the corresponding affine expressions on their cells.
7663 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7664 __isl_take isl_union_pw_aff
*upa
)
7667 isl_union_map
*umap
;
7672 space
= isl_union_pw_aff_get_space(upa
);
7673 umap
= isl_union_map_empty(space
);
7675 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7677 umap
= isl_union_map_free(umap
);
7679 isl_union_pw_aff_free(upa
);
7683 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7684 * upma is the function that is plugged in.
7685 * pa is the current part of the function in which upma is plugged in.
7686 * res collects the results.
7688 struct isl_union_pw_aff_pullback_upma_data
{
7689 isl_union_pw_multi_aff
*upma
;
7691 isl_union_pw_aff
*res
;
7694 /* Check if "pma" can be plugged into data->pa.
7695 * If so, perform the pullback and add the result to data->res.
7697 static isl_stat
pa_pb_pma(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7699 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7702 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7703 pma
->dim
, isl_dim_out
)) {
7704 isl_pw_multi_aff_free(pma
);
7708 pa
= isl_pw_aff_copy(data
->pa
);
7709 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7711 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7713 return data
->res
? isl_stat_ok
: isl_stat_error
;
7716 /* Check if any of the elements of data->upma can be plugged into pa,
7717 * add if so add the result to data->res.
7719 static isl_stat
upa_pb_upma(__isl_take isl_pw_aff
*pa
, void *user
)
7721 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7725 r
= isl_union_pw_multi_aff_foreach_pw_multi_aff(data
->upma
,
7727 isl_pw_aff_free(pa
);
7732 /* Compute the pullback of "upa" by the function represented by "upma".
7733 * In other words, plug in "upma" in "upa". The result contains
7734 * expressions defined over the domain space of "upma".
7736 * Run over all pairs of elements in "upa" and "upma", perform
7737 * the pullback when appropriate and collect the results.
7738 * If the hash value were based on the domain space rather than
7739 * the function space, then we could run through all elements
7740 * of "upma" and directly pick out the corresponding element of "upa".
7742 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7743 __isl_take isl_union_pw_aff
*upa
,
7744 __isl_take isl_union_pw_multi_aff
*upma
)
7746 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7749 space
= isl_union_pw_multi_aff_get_space(upma
);
7750 upa
= isl_union_pw_aff_align_params(upa
, space
);
7751 space
= isl_union_pw_aff_get_space(upa
);
7752 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7758 data
.res
= isl_union_pw_aff_alloc_same_size(upa
);
7759 if (isl_union_pw_aff_foreach_pw_aff(upa
, &upa_pb_upma
, &data
) < 0)
7760 data
.res
= isl_union_pw_aff_free(data
.res
);
7762 isl_union_pw_aff_free(upa
);
7763 isl_union_pw_multi_aff_free(upma
);
7766 isl_union_pw_aff_free(upa
);
7767 isl_union_pw_multi_aff_free(upma
);
7772 #define BASE union_pw_aff
7774 #define DOMBASE union_set
7776 #define NO_MOVE_DIMS
7785 #include <isl_multi_templ.c>
7786 #include <isl_multi_apply_set.c>
7787 #include <isl_multi_apply_union_set.c>
7788 #include <isl_multi_coalesce.c>
7789 #include <isl_multi_floor.c>
7790 #include <isl_multi_gist.c>
7791 #include <isl_multi_intersect.c>
7793 /* Construct a multiple union piecewise affine expression
7794 * in the given space with value zero in each of the output dimensions.
7796 * Since there is no canonical zero value for
7797 * a union piecewise affine expression, we can only construct
7798 * zero-dimensional "zero" value.
7800 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7801 __isl_take isl_space
*space
)
7806 if (!isl_space_is_set(space
))
7807 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7808 "expecting set space", goto error
);
7809 if (isl_space_dim(space
, isl_dim_out
) != 0)
7810 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7811 "expecting 0D space", goto error
);
7813 return isl_multi_union_pw_aff_alloc(space
);
7815 isl_space_free(space
);
7819 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7820 * with the actual sum on the shared domain and
7821 * the defined expression on the symmetric difference of the domains.
7823 * We simply iterate over the elements in both arguments and
7824 * call isl_union_pw_aff_union_add on each of them.
7826 static __isl_give isl_multi_union_pw_aff
*
7827 isl_multi_union_pw_aff_union_add_aligned(
7828 __isl_take isl_multi_union_pw_aff
*mupa1
,
7829 __isl_take isl_multi_union_pw_aff
*mupa2
)
7831 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7832 &isl_union_pw_aff_union_add
);
7835 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7836 * with the actual sum on the shared domain and
7837 * the defined expression on the symmetric difference of the domains.
7839 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7840 __isl_take isl_multi_union_pw_aff
*mupa1
,
7841 __isl_take isl_multi_union_pw_aff
*mupa2
)
7843 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7844 &isl_multi_union_pw_aff_union_add_aligned
);
7847 /* Construct and return a multi union piecewise affine expression
7848 * that is equal to the given multi affine expression.
7850 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7851 __isl_take isl_multi_aff
*ma
)
7853 isl_multi_pw_aff
*mpa
;
7855 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7856 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7859 /* Construct and return a multi union piecewise affine expression
7860 * that is equal to the given multi piecewise affine expression.
7862 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7863 __isl_take isl_multi_pw_aff
*mpa
)
7867 isl_multi_union_pw_aff
*mupa
;
7872 space
= isl_multi_pw_aff_get_space(mpa
);
7873 space
= isl_space_range(space
);
7874 mupa
= isl_multi_union_pw_aff_alloc(space
);
7876 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7877 for (i
= 0; i
< n
; ++i
) {
7879 isl_union_pw_aff
*upa
;
7881 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7882 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7883 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7886 isl_multi_pw_aff_free(mpa
);
7891 /* Extract the range space of "pma" and assign it to *space.
7892 * If *space has already been set (through a previous call to this function),
7893 * then check that the range space is the same.
7895 static isl_stat
extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7897 isl_space
**space
= user
;
7898 isl_space
*pma_space
;
7901 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7902 isl_pw_multi_aff_free(pma
);
7905 return isl_stat_error
;
7911 equal
= isl_space_is_equal(pma_space
, *space
);
7912 isl_space_free(pma_space
);
7915 return isl_stat_error
;
7917 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7918 "range spaces not the same", return isl_stat_error
);
7922 /* Construct and return a multi union piecewise affine expression
7923 * that is equal to the given union piecewise multi affine expression.
7925 * In order to be able to perform the conversion, the input
7926 * needs to be non-empty and may only involve a single range space.
7928 __isl_give isl_multi_union_pw_aff
*
7929 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7930 __isl_take isl_union_pw_multi_aff
*upma
)
7932 isl_space
*space
= NULL
;
7933 isl_multi_union_pw_aff
*mupa
;
7938 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7939 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7940 "cannot extract range space from empty input",
7942 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7949 n
= isl_space_dim(space
, isl_dim_set
);
7950 mupa
= isl_multi_union_pw_aff_alloc(space
);
7952 for (i
= 0; i
< n
; ++i
) {
7953 isl_union_pw_aff
*upa
;
7955 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7956 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7959 isl_union_pw_multi_aff_free(upma
);
7962 isl_space_free(space
);
7963 isl_union_pw_multi_aff_free(upma
);
7967 /* Try and create an isl_multi_union_pw_aff that is equivalent
7968 * to the given isl_union_map.
7969 * The isl_union_map is required to be single-valued in each space.
7970 * Moreover, it cannot be empty and all range spaces need to be the same.
7971 * Otherwise, an error is produced.
7973 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7974 __isl_take isl_union_map
*umap
)
7976 isl_union_pw_multi_aff
*upma
;
7978 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7979 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7982 /* Return a multiple union piecewise affine expression
7983 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7984 * have been aligned.
7986 static __isl_give isl_multi_union_pw_aff
*
7987 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7988 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7992 isl_multi_union_pw_aff
*mupa
;
7997 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7998 space
= isl_multi_val_get_space(mv
);
7999 mupa
= isl_multi_union_pw_aff_alloc(space
);
8000 for (i
= 0; i
< n
; ++i
) {
8002 isl_union_pw_aff
*upa
;
8004 v
= isl_multi_val_get_val(mv
, i
);
8005 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
8007 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8010 isl_union_set_free(domain
);
8011 isl_multi_val_free(mv
);
8014 isl_union_set_free(domain
);
8015 isl_multi_val_free(mv
);
8019 /* Return a multiple union piecewise affine expression
8020 * that is equal to "mv" on "domain".
8022 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
8023 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
8027 if (isl_space_match(domain
->dim
, isl_dim_param
,
8028 mv
->space
, isl_dim_param
))
8029 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8031 domain
= isl_union_set_align_params(domain
,
8032 isl_multi_val_get_space(mv
));
8033 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
8034 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
8036 isl_union_set_free(domain
);
8037 isl_multi_val_free(mv
);
8041 /* Return a multiple union piecewise affine expression
8042 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8043 * have been aligned.
8045 static __isl_give isl_multi_union_pw_aff
*
8046 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8047 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8051 isl_multi_union_pw_aff
*mupa
;
8056 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
8057 space
= isl_multi_aff_get_space(ma
);
8058 mupa
= isl_multi_union_pw_aff_alloc(space
);
8059 for (i
= 0; i
< n
; ++i
) {
8061 isl_union_pw_aff
*upa
;
8063 aff
= isl_multi_aff_get_aff(ma
, i
);
8064 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
8066 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8069 isl_union_set_free(domain
);
8070 isl_multi_aff_free(ma
);
8073 isl_union_set_free(domain
);
8074 isl_multi_aff_free(ma
);
8078 /* Return a multiple union piecewise affine expression
8079 * that is equal to "ma" on "domain".
8081 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
8082 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
8086 if (isl_space_match(domain
->dim
, isl_dim_param
,
8087 ma
->space
, isl_dim_param
))
8088 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8090 domain
= isl_union_set_align_params(domain
,
8091 isl_multi_aff_get_space(ma
));
8092 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
8093 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
8095 isl_union_set_free(domain
);
8096 isl_multi_aff_free(ma
);
8100 /* Return a union set containing those elements in the domains
8101 * of the elements of "mupa" where they are all zero.
8103 __isl_give isl_union_set
*isl_multi_union_pw_aff_zero_union_set(
8104 __isl_take isl_multi_union_pw_aff
*mupa
)
8107 isl_union_pw_aff
*upa
;
8108 isl_union_set
*zero
;
8113 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8115 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8116 "cannot determine zero set "
8117 "of zero-dimensional function", goto error
);
8119 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8120 zero
= isl_union_pw_aff_zero_union_set(upa
);
8122 for (i
= 1; i
< n
; ++i
) {
8123 isl_union_set
*zero_i
;
8125 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8126 zero_i
= isl_union_pw_aff_zero_union_set(upa
);
8128 zero
= isl_union_set_intersect(zero
, zero_i
);
8131 isl_multi_union_pw_aff_free(mupa
);
8134 isl_multi_union_pw_aff_free(mupa
);
8138 /* Construct a union map mapping the shared domain
8139 * of the union piecewise affine expressions to the range of "mupa"
8140 * with each dimension in the range equated to the
8141 * corresponding union piecewise affine expression.
8143 * The input cannot be zero-dimensional as there is
8144 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8146 __isl_give isl_union_map
*isl_union_map_from_multi_union_pw_aff(
8147 __isl_take isl_multi_union_pw_aff
*mupa
)
8151 isl_union_map
*umap
;
8152 isl_union_pw_aff
*upa
;
8157 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8159 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8160 "cannot determine domain of zero-dimensional "
8161 "isl_multi_union_pw_aff", goto error
);
8163 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8164 umap
= isl_union_map_from_union_pw_aff(upa
);
8166 for (i
= 1; i
< n
; ++i
) {
8167 isl_union_map
*umap_i
;
8169 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8170 umap_i
= isl_union_map_from_union_pw_aff(upa
);
8171 umap
= isl_union_map_flat_range_product(umap
, umap_i
);
8174 space
= isl_multi_union_pw_aff_get_space(mupa
);
8175 umap
= isl_union_map_reset_range_space(umap
, space
);
8177 isl_multi_union_pw_aff_free(mupa
);
8180 isl_multi_union_pw_aff_free(mupa
);
8184 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8185 * "range" is the space from which to set the range space.
8186 * "res" collects the results.
8188 struct isl_union_pw_multi_aff_reset_range_space_data
{
8190 isl_union_pw_multi_aff
*res
;
8193 /* Replace the range space of "pma" by the range space of data->range and
8194 * add the result to data->res.
8196 static isl_stat
reset_range_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
8198 struct isl_union_pw_multi_aff_reset_range_space_data
*data
= user
;
8201 space
= isl_pw_multi_aff_get_space(pma
);
8202 space
= isl_space_domain(space
);
8203 space
= isl_space_extend_domain_with_range(space
,
8204 isl_space_copy(data
->range
));
8205 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
8206 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
8208 return data
->res
? isl_stat_ok
: isl_stat_error
;
8211 /* Replace the range space of all the piecewise affine expressions in "upma" by
8212 * the range space of "space".
8214 * This assumes that all these expressions have the same output dimension.
8216 * Since the spaces of the expressions change, so do their hash values.
8217 * We therefore need to create a new isl_union_pw_multi_aff.
8218 * Note that the hash value is currently computed based on the entire
8219 * space even though there can only be a single expression with a given
8222 static __isl_give isl_union_pw_multi_aff
*
8223 isl_union_pw_multi_aff_reset_range_space(
8224 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_space
*space
)
8226 struct isl_union_pw_multi_aff_reset_range_space_data data
= { space
};
8227 isl_space
*space_upma
;
8229 space_upma
= isl_union_pw_multi_aff_get_space(upma
);
8230 data
.res
= isl_union_pw_multi_aff_empty(space_upma
);
8231 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
8232 &reset_range_space
, &data
) < 0)
8233 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
8235 isl_space_free(space
);
8236 isl_union_pw_multi_aff_free(upma
);
8240 /* Construct and return a union piecewise multi affine expression
8241 * that is equal to the given multi union piecewise affine expression.
8243 * In order to be able to perform the conversion, the input
8244 * needs to have a least one output dimension.
8246 __isl_give isl_union_pw_multi_aff
*
8247 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8248 __isl_take isl_multi_union_pw_aff
*mupa
)
8252 isl_union_pw_multi_aff
*upma
;
8253 isl_union_pw_aff
*upa
;
8258 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8260 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8261 "cannot determine domain of zero-dimensional "
8262 "isl_multi_union_pw_aff", goto error
);
8264 space
= isl_multi_union_pw_aff_get_space(mupa
);
8265 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8266 upma
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8268 for (i
= 1; i
< n
; ++i
) {
8269 isl_union_pw_multi_aff
*upma_i
;
8271 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8272 upma_i
= isl_union_pw_multi_aff_from_union_pw_aff(upa
);
8273 upma
= isl_union_pw_multi_aff_flat_range_product(upma
, upma_i
);
8276 upma
= isl_union_pw_multi_aff_reset_range_space(upma
, space
);
8278 isl_multi_union_pw_aff_free(mupa
);
8281 isl_multi_union_pw_aff_free(mupa
);
8285 /* Intersect the range of "mupa" with "range".
8286 * That is, keep only those domain elements that have a function value
8289 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_intersect_range(
8290 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_set
*range
)
8292 isl_union_pw_multi_aff
*upma
;
8293 isl_union_set
*domain
;
8298 if (!mupa
|| !range
)
8301 space
= isl_set_get_space(range
);
8302 match
= isl_space_tuple_is_equal(mupa
->space
, isl_dim_set
,
8303 space
, isl_dim_set
);
8304 isl_space_free(space
);
8308 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8309 "space don't match", goto error
);
8310 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8312 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8313 "cannot intersect range of zero-dimensional "
8314 "isl_multi_union_pw_aff", goto error
);
8316 upma
= isl_union_pw_multi_aff_from_multi_union_pw_aff(
8317 isl_multi_union_pw_aff_copy(mupa
));
8318 domain
= isl_union_set_from_set(range
);
8319 domain
= isl_union_set_preimage_union_pw_multi_aff(domain
, upma
);
8320 mupa
= isl_multi_union_pw_aff_intersect_domain(mupa
, domain
);
8324 isl_multi_union_pw_aff_free(mupa
);
8325 isl_set_free(range
);
8329 /* Return the shared domain of the elements of "mupa".
8331 __isl_give isl_union_set
*isl_multi_union_pw_aff_domain(
8332 __isl_take isl_multi_union_pw_aff
*mupa
)
8335 isl_union_pw_aff
*upa
;
8341 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8343 isl_die(isl_multi_union_pw_aff_get_ctx(mupa
), isl_error_invalid
,
8344 "cannot determine domain", goto error
);
8346 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, 0);
8347 dom
= isl_union_pw_aff_domain(upa
);
8348 for (i
= 1; i
< n
; ++i
) {
8349 isl_union_set
*dom_i
;
8351 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8352 dom_i
= isl_union_pw_aff_domain(upa
);
8353 dom
= isl_union_set_intersect(dom
, dom_i
);
8356 isl_multi_union_pw_aff_free(mupa
);
8359 isl_multi_union_pw_aff_free(mupa
);
8363 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8364 * In particular, the spaces have been aligned.
8365 * The result is defined over the shared domain of the elements of "mupa"
8367 * We first extract the parametric constant part of "aff" and
8368 * define that over the shared domain.
8369 * Then we iterate over all input dimensions of "aff" and add the corresponding
8370 * multiples of the elements of "mupa".
8371 * Finally, we consider the integer divisions, calling the function
8372 * recursively to obtain an isl_union_pw_aff corresponding to the
8373 * integer division argument.
8375 static __isl_give isl_union_pw_aff
*multi_union_pw_aff_apply_aff(
8376 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8379 isl_union_pw_aff
*upa
;
8380 isl_union_set
*uset
;
8384 n_in
= isl_aff_dim(aff
, isl_dim_in
);
8385 n_div
= isl_aff_dim(aff
, isl_dim_div
);
8387 uset
= isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa
));
8388 cst
= isl_aff_copy(aff
);
8389 cst
= isl_aff_drop_dims(cst
, isl_dim_div
, 0, n_div
);
8390 cst
= isl_aff_drop_dims(cst
, isl_dim_in
, 0, n_in
);
8391 cst
= isl_aff_project_domain_on_params(cst
);
8392 upa
= isl_union_pw_aff_aff_on_domain(uset
, cst
);
8394 for (i
= 0; i
< n_in
; ++i
) {
8395 isl_union_pw_aff
*upa_i
;
8397 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
8399 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
8400 upa_i
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8401 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8402 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8405 for (i
= 0; i
< n_div
; ++i
) {
8407 isl_union_pw_aff
*upa_i
;
8409 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
8411 div
= isl_aff_get_div(aff
, i
);
8412 upa_i
= multi_union_pw_aff_apply_aff(
8413 isl_multi_union_pw_aff_copy(mupa
), div
);
8414 upa_i
= isl_union_pw_aff_floor(upa_i
);
8415 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
8416 upa_i
= isl_union_pw_aff_scale_val(upa_i
, v
);
8417 upa
= isl_union_pw_aff_add(upa
, upa_i
);
8420 isl_multi_union_pw_aff_free(mupa
);
8426 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8427 * with the domain of "aff".
8428 * Furthermore, the dimension of this space needs to be greater than zero.
8429 * The result is defined over the shared domain of the elements of "mupa"
8431 * We perform these checks and then hand over control to
8432 * multi_union_pw_aff_apply_aff.
8434 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_aff(
8435 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_aff
*aff
)
8437 isl_space
*space1
, *space2
;
8440 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8441 isl_aff_get_space(aff
));
8442 aff
= isl_aff_align_params(aff
, isl_multi_union_pw_aff_get_space(mupa
));
8446 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8447 space2
= isl_aff_get_domain_space(aff
);
8448 equal
= isl_space_is_equal(space1
, space2
);
8449 isl_space_free(space1
);
8450 isl_space_free(space2
);
8454 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8455 "spaces don't match", goto error
);
8456 if (isl_aff_dim(aff
, isl_dim_in
) == 0)
8457 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
8458 "cannot determine domains", goto error
);
8460 return multi_union_pw_aff_apply_aff(mupa
, aff
);
8462 isl_multi_union_pw_aff_free(mupa
);
8467 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8468 * with the domain of "ma".
8469 * Furthermore, the dimension of this space needs to be greater than zero,
8470 * unless the dimension of the target space of "ma" is also zero.
8471 * The result is defined over the shared domain of the elements of "mupa"
8473 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_multi_aff(
8474 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_multi_aff
*ma
)
8476 isl_space
*space1
, *space2
;
8477 isl_multi_union_pw_aff
*res
;
8481 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8482 isl_multi_aff_get_space(ma
));
8483 ma
= isl_multi_aff_align_params(ma
,
8484 isl_multi_union_pw_aff_get_space(mupa
));
8488 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8489 space2
= isl_multi_aff_get_domain_space(ma
);
8490 equal
= isl_space_is_equal(space1
, space2
);
8491 isl_space_free(space1
);
8492 isl_space_free(space2
);
8496 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8497 "spaces don't match", goto error
);
8498 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
8499 if (isl_multi_aff_dim(ma
, isl_dim_in
) == 0 && n_out
!= 0)
8500 isl_die(isl_multi_aff_get_ctx(ma
), isl_error_invalid
,
8501 "cannot determine domains", goto error
);
8503 space1
= isl_space_range(isl_multi_aff_get_space(ma
));
8504 res
= isl_multi_union_pw_aff_alloc(space1
);
8506 for (i
= 0; i
< n_out
; ++i
) {
8508 isl_union_pw_aff
*upa
;
8510 aff
= isl_multi_aff_get_aff(ma
, i
);
8511 upa
= multi_union_pw_aff_apply_aff(
8512 isl_multi_union_pw_aff_copy(mupa
), aff
);
8513 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8516 isl_multi_aff_free(ma
);
8517 isl_multi_union_pw_aff_free(mupa
);
8520 isl_multi_union_pw_aff_free(mupa
);
8521 isl_multi_aff_free(ma
);
8525 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8526 * with the domain of "pa".
8527 * Furthermore, the dimension of this space needs to be greater than zero.
8528 * The result is defined over the shared domain of the elements of "mupa"
8530 __isl_give isl_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_aff(
8531 __isl_take isl_multi_union_pw_aff
*mupa
, __isl_take isl_pw_aff
*pa
)
8535 isl_space
*space
, *space2
;
8536 isl_union_pw_aff
*upa
;
8538 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8539 isl_pw_aff_get_space(pa
));
8540 pa
= isl_pw_aff_align_params(pa
,
8541 isl_multi_union_pw_aff_get_space(mupa
));
8545 space
= isl_multi_union_pw_aff_get_space(mupa
);
8546 space2
= isl_pw_aff_get_domain_space(pa
);
8547 equal
= isl_space_is_equal(space
, space2
);
8548 isl_space_free(space
);
8549 isl_space_free(space2
);
8553 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8554 "spaces don't match", goto error
);
8555 if (isl_pw_aff_dim(pa
, isl_dim_in
) == 0)
8556 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
8557 "cannot determine domains", goto error
);
8559 space
= isl_space_params(isl_multi_union_pw_aff_get_space(mupa
));
8560 upa
= isl_union_pw_aff_empty(space
);
8562 for (i
= 0; i
< pa
->n
; ++i
) {
8565 isl_multi_union_pw_aff
*mupa_i
;
8566 isl_union_pw_aff
*upa_i
;
8568 mupa_i
= isl_multi_union_pw_aff_copy(mupa
);
8569 domain
= isl_set_copy(pa
->p
[i
].set
);
8570 mupa_i
= isl_multi_union_pw_aff_intersect_range(mupa_i
, domain
);
8571 aff
= isl_aff_copy(pa
->p
[i
].aff
);
8572 upa_i
= multi_union_pw_aff_apply_aff(mupa_i
, aff
);
8573 upa
= isl_union_pw_aff_union_add(upa
, upa_i
);
8576 isl_multi_union_pw_aff_free(mupa
);
8577 isl_pw_aff_free(pa
);
8580 isl_multi_union_pw_aff_free(mupa
);
8581 isl_pw_aff_free(pa
);
8585 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8586 * with the domain of "pma".
8587 * Furthermore, the dimension of this space needs to be greater than zero,
8588 * unless the dimension of the target space of "pma" is also zero.
8589 * The result is defined over the shared domain of the elements of "mupa"
8591 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_apply_pw_multi_aff(
8592 __isl_take isl_multi_union_pw_aff
*mupa
,
8593 __isl_take isl_pw_multi_aff
*pma
)
8595 isl_space
*space1
, *space2
;
8596 isl_multi_union_pw_aff
*res
;
8600 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8601 isl_pw_multi_aff_get_space(pma
));
8602 pma
= isl_pw_multi_aff_align_params(pma
,
8603 isl_multi_union_pw_aff_get_space(mupa
));
8607 space1
= isl_multi_union_pw_aff_get_space(mupa
);
8608 space2
= isl_pw_multi_aff_get_domain_space(pma
);
8609 equal
= isl_space_is_equal(space1
, space2
);
8610 isl_space_free(space1
);
8611 isl_space_free(space2
);
8615 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8616 "spaces don't match", goto error
);
8617 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
8618 if (isl_pw_multi_aff_dim(pma
, isl_dim_in
) == 0 && n_out
!= 0)
8619 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
8620 "cannot determine domains", goto error
);
8622 space1
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
8623 res
= isl_multi_union_pw_aff_alloc(space1
);
8625 for (i
= 0; i
< n_out
; ++i
) {
8627 isl_union_pw_aff
*upa
;
8629 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
8630 upa
= isl_multi_union_pw_aff_apply_pw_aff(
8631 isl_multi_union_pw_aff_copy(mupa
), pa
);
8632 res
= isl_multi_union_pw_aff_set_union_pw_aff(res
, i
, upa
);
8635 isl_pw_multi_aff_free(pma
);
8636 isl_multi_union_pw_aff_free(mupa
);
8639 isl_multi_union_pw_aff_free(mupa
);
8640 isl_pw_multi_aff_free(pma
);
8644 /* Compute the pullback of "mupa" by the function represented by "upma".
8645 * In other words, plug in "upma" in "mupa". The result contains
8646 * expressions defined over the domain space of "upma".
8648 * Run over all elements of "mupa" and plug in "upma" in each of them.
8650 __isl_give isl_multi_union_pw_aff
*
8651 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8652 __isl_take isl_multi_union_pw_aff
*mupa
,
8653 __isl_take isl_union_pw_multi_aff
*upma
)
8657 mupa
= isl_multi_union_pw_aff_align_params(mupa
,
8658 isl_union_pw_multi_aff_get_space(upma
));
8659 upma
= isl_union_pw_multi_aff_align_params(upma
,
8660 isl_multi_union_pw_aff_get_space(mupa
));
8664 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8665 for (i
= 0; i
< n
; ++i
) {
8666 isl_union_pw_aff
*upa
;
8668 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8669 upa
= isl_union_pw_aff_pullback_union_pw_multi_aff(upa
,
8670 isl_union_pw_multi_aff_copy(upma
));
8671 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
8674 isl_union_pw_multi_aff_free(upma
);
8677 isl_multi_union_pw_aff_free(mupa
);
8678 isl_union_pw_multi_aff_free(upma
);
8682 /* Extract the sequence of elements in "mupa" with domain space "space"
8683 * (ignoring parameters).
8685 * For the elements of "mupa" that are not defined on the specified space,
8686 * the corresponding element in the result is empty.
8688 __isl_give isl_multi_pw_aff
*isl_multi_union_pw_aff_extract_multi_pw_aff(
8689 __isl_keep isl_multi_union_pw_aff
*mupa
, __isl_take isl_space
*space
)
8692 isl_space
*space_mpa
= NULL
;
8693 isl_multi_pw_aff
*mpa
;
8695 if (!mupa
|| !space
)
8698 space_mpa
= isl_multi_union_pw_aff_get_space(mupa
);
8699 if (!isl_space_match(space_mpa
, isl_dim_param
, space
, isl_dim_param
)) {
8700 space
= isl_space_drop_dims(space
, isl_dim_param
,
8701 0, isl_space_dim(space
, isl_dim_param
));
8702 space
= isl_space_align_params(space
,
8703 isl_space_copy(space_mpa
));
8707 space_mpa
= isl_space_map_from_domain_and_range(isl_space_copy(space
),
8709 mpa
= isl_multi_pw_aff_alloc(space_mpa
);
8711 space
= isl_space_from_domain(space
);
8712 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
8713 n
= isl_multi_union_pw_aff_dim(mupa
, isl_dim_set
);
8714 for (i
= 0; i
< n
; ++i
) {
8715 isl_union_pw_aff
*upa
;
8718 upa
= isl_multi_union_pw_aff_get_union_pw_aff(mupa
, i
);
8719 pa
= isl_union_pw_aff_extract_pw_aff(upa
,
8720 isl_space_copy(space
));
8721 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
8722 isl_union_pw_aff_free(upa
);
8725 isl_space_free(space
);
8728 isl_space_free(space_mpa
);
8729 isl_space_free(space
);