2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Universiteit Leiden
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_local_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl/constraint.h>
23 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
24 __isl_take isl_vec
*v
)
31 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
41 isl_local_space_free(ls
);
46 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
55 ctx
= isl_local_space_get_ctx(ls
);
56 if (!isl_local_space_divs_known(ls
))
57 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
60 total
= isl_local_space_dim(ls
, isl_dim_all
);
61 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
62 return isl_aff_alloc_vec(ls
, v
);
64 isl_local_space_free(ls
);
68 __isl_give isl_aff
*isl_aff_zero(__isl_take isl_local_space
*ls
)
72 aff
= isl_aff_alloc(ls
);
76 isl_int_set_si(aff
->v
->el
[0], 1);
77 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
82 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
91 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
96 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
97 isl_vec_copy(aff
->v
));
100 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
108 return isl_aff_dup(aff
);
111 void *isl_aff_free(__isl_take isl_aff
*aff
)
119 isl_local_space_free(aff
->ls
);
120 isl_vec_free(aff
->v
);
127 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
129 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
132 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
134 return aff
? isl_local_space_dim(aff
->ls
, type
) : 0;
137 __isl_give isl_dim
*isl_aff_get_dim(__isl_keep isl_aff
*aff
)
139 return aff
? isl_local_space_get_dim(aff
->ls
) : NULL
;
142 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
144 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
147 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
148 enum isl_dim_type type
, unsigned pos
)
150 return aff
? isl_local_space_get_dim_name(aff
->ls
, type
, pos
) : 0;
153 __isl_give isl_aff
*isl_aff_reset_dim(__isl_take isl_aff
*aff
,
154 __isl_take isl_dim
*dim
)
156 aff
= isl_aff_cow(aff
);
160 aff
->ls
= isl_local_space_reset_dim(aff
->ls
, dim
);
162 return isl_aff_free(aff
);
171 /* Reorder the coefficients of the affine expression based
172 * on the given reodering.
173 * The reordering r is assumed to have been extended with the local
176 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
177 __isl_take isl_reordering
*r
, int n_div
)
185 res
= isl_vec_alloc(vec
->ctx
, 2 + isl_dim_total(r
->dim
) + n_div
);
186 isl_seq_cpy(res
->el
, vec
->el
, 2);
187 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
188 for (i
= 0; i
< r
->len
; ++i
)
189 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
191 isl_reordering_free(r
);
196 isl_reordering_free(r
);
200 /* Reorder the dimensions of "aff" according to the given reordering.
202 __isl_give isl_aff
*isl_aff_realign(__isl_take isl_aff
*aff
,
203 __isl_take isl_reordering
*r
)
205 aff
= isl_aff_cow(aff
);
209 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
210 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
211 aff
->ls
->div
->n_row
);
212 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
214 if (!aff
->v
|| !aff
->ls
)
215 return isl_aff_free(aff
);
220 isl_reordering_free(r
);
224 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
229 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
232 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
239 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
240 if (equal
< 0 || !equal
)
243 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
246 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
250 isl_int_set(*v
, aff
->v
->el
[0]);
254 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
258 isl_int_set(*v
, aff
->v
->el
[1]);
262 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
263 enum isl_dim_type type
, int pos
, isl_int
*v
)
268 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
269 isl_die(aff
->v
->ctx
, isl_error_invalid
,
270 "position out of bounds", return -1);
272 pos
+= isl_local_space_offset(aff
->ls
, type
);
273 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
278 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
280 aff
= isl_aff_cow(aff
);
284 aff
->v
= isl_vec_cow(aff
->v
);
286 return isl_aff_free(aff
);
288 isl_int_set(aff
->v
->el
[0], v
);
293 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
295 aff
= isl_aff_cow(aff
);
299 aff
->v
= isl_vec_cow(aff
->v
);
301 return isl_aff_free(aff
);
303 isl_int_set(aff
->v
->el
[1], v
);
308 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
310 if (isl_int_is_zero(v
))
313 aff
= isl_aff_cow(aff
);
317 aff
->v
= isl_vec_cow(aff
->v
);
319 return isl_aff_free(aff
);
321 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
326 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
331 isl_int_set_si(t
, v
);
332 aff
= isl_aff_add_constant(aff
, t
);
338 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
340 aff
= isl_aff_cow(aff
);
344 aff
->v
= isl_vec_cow(aff
->v
);
346 return isl_aff_free(aff
);
348 isl_int_set_si(aff
->v
->el
[1], v
);
353 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
354 enum isl_dim_type type
, int pos
, isl_int v
)
359 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
360 isl_die(aff
->v
->ctx
, isl_error_invalid
,
361 "position out of bounds", return isl_aff_free(aff
));
363 aff
= isl_aff_cow(aff
);
367 aff
->v
= isl_vec_cow(aff
->v
);
369 return isl_aff_free(aff
);
371 pos
+= isl_local_space_offset(aff
->ls
, type
);
372 isl_int_set(aff
->v
->el
[1 + pos
], v
);
377 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
378 enum isl_dim_type type
, int pos
, int v
)
383 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
384 isl_die(aff
->v
->ctx
, isl_error_invalid
,
385 "position out of bounds", return isl_aff_free(aff
));
387 aff
= isl_aff_cow(aff
);
391 aff
->v
= isl_vec_cow(aff
->v
);
393 return isl_aff_free(aff
);
395 pos
+= isl_local_space_offset(aff
->ls
, type
);
396 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
401 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
402 enum isl_dim_type type
, int pos
, isl_int v
)
407 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
408 isl_die(aff
->v
->ctx
, isl_error_invalid
,
409 "position out of bounds", return isl_aff_free(aff
));
411 aff
= isl_aff_cow(aff
);
415 aff
->v
= isl_vec_cow(aff
->v
);
417 return isl_aff_free(aff
);
419 pos
+= isl_local_space_offset(aff
->ls
, type
);
420 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
425 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
426 enum isl_dim_type type
, int pos
, int v
)
431 isl_int_set_si(t
, v
);
432 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
438 __isl_give isl_div
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
443 return isl_local_space_get_div(aff
->ls
, pos
);
446 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
448 aff
= isl_aff_cow(aff
);
451 aff
->v
= isl_vec_cow(aff
->v
);
453 return isl_aff_free(aff
);
455 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
460 /* Given f, return floor(f).
461 * If f is an integer expression, then just return f.
462 * Otherwise, create a new div d = [f] and return the expression d.
464 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
472 if (isl_int_is_one(aff
->v
->el
[0]))
475 aff
= isl_aff_cow(aff
);
479 aff
->ls
= isl_local_space_add_div(aff
->ls
, isl_vec_copy(aff
->v
));
481 return isl_aff_free(aff
);
483 ctx
= isl_aff_get_ctx(aff
);
485 isl_vec_free(aff
->v
);
486 aff
->v
= isl_vec_alloc(ctx
, size
+ 1);
487 aff
->v
= isl_vec_clr(aff
->v
);
489 return isl_aff_free(aff
);
490 isl_int_set_si(aff
->v
->el
[0], 1);
491 isl_int_set_si(aff
->v
->el
[size
], 1);
496 /* Given f, return ceil(f).
497 * If f is an integer expression, then just return f.
498 * Otherwise, create a new div d = [-f] and return the expression -d.
500 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
505 if (isl_int_is_one(aff
->v
->el
[0]))
508 aff
= isl_aff_neg(aff
);
509 aff
= isl_aff_floor(aff
);
510 aff
= isl_aff_neg(aff
);
515 /* Apply the expansion computed by isl_merge_divs.
516 * The expansion itself is given by "exp" while the resulting
517 * list of divs is given by "div".
519 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
520 __isl_take isl_mat
*div
, int *exp
)
527 aff
= isl_aff_cow(aff
);
531 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
532 new_n_div
= isl_mat_rows(div
);
533 if (new_n_div
< old_n_div
)
534 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
535 "not an expansion", goto error
);
537 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
541 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
543 for (i
= new_n_div
- 1; i
>= 0; --i
) {
544 if (j
>= 0 && exp
[j
] == i
) {
546 isl_int_swap(aff
->v
->el
[offset
+ i
],
547 aff
->v
->el
[offset
+ j
]);
550 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
553 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
564 /* Add two affine expressions that live in the same local space.
566 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
567 __isl_take isl_aff
*aff2
)
571 aff1
= isl_aff_cow(aff1
);
575 aff1
->v
= isl_vec_cow(aff1
->v
);
581 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
582 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
583 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
584 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
585 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
586 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
587 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
599 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
600 __isl_take isl_aff
*aff2
)
610 ctx
= isl_aff_get_ctx(aff1
);
611 if (!isl_dim_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
612 isl_die(ctx
, isl_error_invalid
,
613 "spaces don't match", goto error
);
615 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
616 return add_expanded(aff1
, aff2
);
618 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
619 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
623 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
624 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
625 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
629 return add_expanded(aff1
, aff2
);
638 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
639 __isl_take isl_aff
*aff2
)
641 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
644 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
648 if (isl_int_is_one(f
))
651 aff
= isl_aff_cow(aff
);
654 aff
->v
= isl_vec_cow(aff
->v
);
656 return isl_aff_free(aff
);
659 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
660 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
661 isl_int_divexact(gcd
, f
, gcd
);
662 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
668 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
672 if (isl_int_is_one(f
))
675 aff
= isl_aff_cow(aff
);
678 aff
->v
= isl_vec_cow(aff
->v
);
680 return isl_aff_free(aff
);
683 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
684 isl_int_gcd(gcd
, gcd
, f
);
685 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
686 isl_int_divexact(gcd
, f
, gcd
);
687 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
693 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
701 isl_int_set_ui(v
, f
);
702 aff
= isl_aff_scale_down(aff
, v
);
708 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
709 enum isl_dim_type type
, unsigned pos
, const char *s
)
711 aff
= isl_aff_cow(aff
);
714 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
716 return isl_aff_free(aff
);
721 /* Exploit the equalities in "eq" to simplify the affine expression
722 * and the expressions of the integer divisions in the local space.
723 * The integer divisions in this local space are assumed to appear
724 * as regular dimensions in "eq".
726 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
727 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
736 isl_basic_set_free(eq
);
740 aff
= isl_aff_cow(aff
);
744 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
745 isl_basic_set_copy(eq
));
749 total
= 1 + isl_dim_total(eq
->dim
);
751 for (i
= 0; i
< eq
->n_eq
; ++i
) {
752 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
753 if (j
< 0 || j
== 0 || j
>= total
)
756 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
760 isl_basic_set_free(eq
);
763 isl_basic_set_free(eq
);
768 /* Exploit the equalities in "eq" to simplify the affine expression
769 * and the expressions of the integer divisions in the local space.
771 static __isl_give isl_aff
*isl_aff_substitute_equalities(
772 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
778 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
780 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
781 return isl_aff_substitute_equalities_lifted(aff
, eq
);
783 isl_basic_set_free(eq
);
788 /* Look for equalities among the variables shared by context and aff
789 * and the integer divisions of aff, if any.
790 * The equalities are then used to eliminate coefficients and/or integer
791 * divisions from aff.
793 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
794 __isl_take isl_set
*context
)
801 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
804 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
805 bset
= isl_basic_set_from_local_space(
806 isl_aff_get_local_space(aff
));
807 bset
= isl_basic_set_lift(bset
);
808 bset
= isl_basic_set_flatten(bset
);
809 context
= isl_set_intersect(context
,
810 isl_set_from_basic_set(bset
));
813 hull
= isl_set_affine_hull(context
);
814 return isl_aff_substitute_equalities_lifted(aff
, hull
);
817 isl_set_free(context
);
821 /* Return a basic set containing those elements in the space
822 * of aff where it is non-negative.
824 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
826 isl_constraint
*ineq
;
828 ineq
= isl_inequality_from_aff(aff
);
830 return isl_basic_set_from_constraint(ineq
);
833 /* Return a basic set containing those elements in the space
834 * of aff where it is zero.
836 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
838 isl_constraint
*ineq
;
840 ineq
= isl_equality_from_aff(aff
);
842 return isl_basic_set_from_constraint(ineq
);
845 /* Return a basic set containing those elements in the shared space
846 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
848 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
849 __isl_take isl_aff
*aff2
)
851 aff1
= isl_aff_sub(aff1
, aff2
);
853 return isl_aff_nonneg_basic_set(aff1
);
856 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
857 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
859 aff1
= isl_aff_add(aff1
, aff2
);
860 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
864 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
872 /* Set active[i] to 1 if the dimension at position i is involved
873 * in the affine expression.
875 static int set_active(__isl_keep isl_aff
*aff
, int *active
)
884 total
= aff
->v
->size
- 2;
885 for (i
= 0; i
< total
; ++i
)
886 active
[i
] = !isl_int_is_zero(aff
->v
->el
[2 + i
]);
888 offset
= isl_local_space_offset(aff
->ls
, isl_dim_div
) - 1;
889 for (i
= aff
->ls
->div
->n_row
- 1; i
>= 0; --i
) {
890 if (!active
[offset
+ i
])
892 for (j
= 0; j
< total
; ++j
)
894 !isl_int_is_zero(aff
->ls
->div
->row
[i
][2 + j
]);
900 /* Check whether the given affine expression has non-zero coefficient
901 * for any dimension in the given range or if any of these dimensions
902 * appear with non-zero coefficients in any of the integer divisions
903 * involved in the affine expression.
905 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
906 enum isl_dim_type type
, unsigned first
, unsigned n
)
918 ctx
= isl_aff_get_ctx(aff
);
919 if (first
+ n
> isl_aff_dim(aff
, type
))
920 isl_die(ctx
, isl_error_invalid
,
921 "range out of bounds", return -1);
923 active
= isl_calloc_array(ctx
, int,
924 isl_local_space_dim(aff
->ls
, isl_dim_all
));
925 if (set_active(aff
, active
) < 0)
928 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
929 for (i
= 0; i
< n
; ++i
)
930 if (active
[first
+ i
]) {
943 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
944 enum isl_dim_type type
, unsigned first
, unsigned n
)
950 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
953 ctx
= isl_aff_get_ctx(aff
);
954 if (first
+ n
> isl_aff_dim(aff
, type
))
955 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
956 return isl_aff_free(aff
));
958 aff
= isl_aff_cow(aff
);
962 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
964 return isl_aff_free(aff
);
966 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
967 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
969 return isl_aff_free(aff
);
974 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
975 enum isl_dim_type type
, unsigned first
, unsigned n
)
981 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
984 ctx
= isl_aff_get_ctx(aff
);
985 if (first
> isl_aff_dim(aff
, type
))
986 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
987 return isl_aff_free(aff
));
989 aff
= isl_aff_cow(aff
);
993 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
995 return isl_aff_free(aff
);
997 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
998 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1000 return isl_aff_free(aff
);
1005 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1006 enum isl_dim_type type
, unsigned n
)
1010 pos
= isl_aff_dim(aff
, type
);
1012 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1015 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1016 enum isl_dim_type type
, unsigned n
)
1020 pos
= isl_pw_aff_dim(pwaff
, type
);
1022 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1025 __isl_give isl_pw_aff
*isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff
*pwaff
,
1026 __isl_take isl_id
*id
)
1030 dim
= isl_pw_aff_get_dim(pwaff
);
1031 dim
= isl_dim_set_tuple_id(dim
, isl_dim_set
, id
);
1033 return isl_pw_aff_reset_dim(pwaff
, dim
);
1036 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1038 isl_set
*dom
= isl_set_universe(isl_aff_get_dim(aff
));
1039 return isl_pw_aff_alloc(dom
, aff
);
1043 #define PW isl_pw_aff
1047 #define EL_IS_ZERO is_empty
1051 #define IS_ZERO is_empty
1057 #define NO_MOVE_DIMS
1061 #include <isl_pw_templ.c>
1063 static __isl_give isl_set
*align_params_pw_pw_set_and(
1064 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1065 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1066 __isl_take isl_pw_aff
*pwaff2
))
1068 if (!pwaff1
|| !pwaff2
)
1070 if (isl_dim_match(pwaff1
->dim
, isl_dim_param
,
1071 pwaff2
->dim
, isl_dim_param
))
1072 return fn(pwaff1
, pwaff2
);
1073 if (!isl_dim_has_named_params(pwaff1
->dim
) ||
1074 !isl_dim_has_named_params(pwaff2
->dim
))
1075 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1076 "unaligned unnamed parameters", goto error
);
1077 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_dim(pwaff2
));
1078 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_dim(pwaff1
));
1079 return fn(pwaff1
, pwaff2
);
1081 isl_pw_aff_free(pwaff1
);
1082 isl_pw_aff_free(pwaff2
);
1086 /* Compute a piecewise quasi-affine expression with a domain that
1087 * is the union of those of pwaff1 and pwaff2 and such that on each
1088 * cell, the quasi-affine expression is the maximum of those of pwaff1
1089 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1090 * cell, then the associated expression is the defined one.
1092 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1093 __isl_take isl_pw_aff
*pwaff2
)
1100 if (!pwaff1
|| !pwaff2
)
1103 ctx
= isl_dim_get_ctx(pwaff1
->dim
);
1104 if (!isl_dim_equal(pwaff1
->dim
, pwaff2
->dim
))
1105 isl_die(ctx
, isl_error_invalid
,
1106 "arguments should live in same space", goto error
);
1108 if (isl_pw_aff_is_empty(pwaff1
)) {
1109 isl_pw_aff_free(pwaff1
);
1113 if (isl_pw_aff_is_empty(pwaff2
)) {
1114 isl_pw_aff_free(pwaff2
);
1118 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1119 res
= isl_pw_aff_alloc_(isl_dim_copy(pwaff1
->dim
), n
);
1121 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1122 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1123 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1124 struct isl_set
*common
;
1127 common
= isl_set_intersect(
1128 isl_set_copy(pwaff1
->p
[i
].set
),
1129 isl_set_copy(pwaff2
->p
[j
].set
));
1130 ge
= isl_set_from_basic_set(isl_aff_ge_basic_set(
1131 isl_aff_copy(pwaff2
->p
[j
].aff
),
1132 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1133 ge
= isl_set_intersect(common
, ge
);
1134 if (isl_set_plain_is_empty(ge
)) {
1138 set
= isl_set_subtract(set
, isl_set_copy(ge
));
1140 res
= isl_pw_aff_add_piece(res
, ge
,
1141 isl_aff_copy(pwaff2
->p
[j
].aff
));
1143 res
= isl_pw_aff_add_piece(res
, set
,
1144 isl_aff_copy(pwaff1
->p
[i
].aff
));
1147 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1148 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1149 for (i
= 0; i
< pwaff1
->n
; ++i
)
1150 set
= isl_set_subtract(set
,
1151 isl_set_copy(pwaff1
->p
[i
].set
));
1152 res
= isl_pw_aff_add_piece(res
, set
,
1153 isl_aff_copy(pwaff2
->p
[j
].aff
));
1156 isl_pw_aff_free(pwaff1
);
1157 isl_pw_aff_free(pwaff2
);
1161 isl_pw_aff_free(pwaff1
);
1162 isl_pw_aff_free(pwaff2
);
1166 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1167 __isl_take isl_pw_aff
*pwaff2
)
1169 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_union_max
);
1172 /* Construct a map with as domain the domain of pwaff and
1173 * one-dimensional range corresponding to the affine expressions.
1175 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1184 dim
= isl_pw_aff_get_dim(pwaff
);
1185 dim
= isl_dim_from_domain(dim
);
1186 dim
= isl_dim_add(dim
, isl_dim_out
, 1);
1187 map
= isl_map_empty(dim
);
1189 for (i
= 0; i
< pwaff
->n
; ++i
) {
1190 isl_basic_map
*bmap
;
1193 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1194 map_i
= isl_map_from_basic_map(bmap
);
1195 map_i
= isl_map_intersect_domain(map_i
,
1196 isl_set_copy(pwaff
->p
[i
].set
));
1197 map
= isl_map_union_disjoint(map
, map_i
);
1200 isl_pw_aff_free(pwaff
);
1205 /* Return a set containing those elements in the domain
1206 * of pwaff where it is non-negative.
1208 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1216 set
= isl_set_empty(isl_pw_aff_get_dim(pwaff
));
1218 for (i
= 0; i
< pwaff
->n
; ++i
) {
1219 isl_basic_set
*bset
;
1222 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1223 set_i
= isl_set_from_basic_set(bset
);
1224 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1225 set
= isl_set_union_disjoint(set
, set_i
);
1228 isl_pw_aff_free(pwaff
);
1233 /* Return a set containing those elements in the domain
1234 * of pwaff where it is zero.
1236 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1244 set
= isl_set_empty(isl_pw_aff_get_dim(pwaff
));
1246 for (i
= 0; i
< pwaff
->n
; ++i
) {
1247 isl_basic_set
*bset
;
1250 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1251 set_i
= isl_set_from_basic_set(bset
);
1252 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1253 set
= isl_set_union_disjoint(set
, set_i
);
1256 isl_pw_aff_free(pwaff
);
1261 /* Return a set containing those elements in the domain
1262 * of pwaff where it is not zero.
1264 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1266 return isl_set_complement(isl_pw_aff_zero_set(pwaff
));
1269 /* Return a set containing those elements in the shared domain
1270 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1272 * We compute the difference on the shared domain and then construct
1273 * the set of values where this difference is non-negative.
1274 * If strict is set, we first subtract 1 from the difference.
1275 * If equal is set, we only return the elements where pwaff1 and pwaff2
1278 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1279 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1281 isl_set
*set1
, *set2
;
1283 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1284 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1285 set1
= isl_set_intersect(set1
, set2
);
1286 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1287 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1288 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1291 isl_dim
*dim
= isl_set_get_dim(set1
);
1293 aff
= isl_aff_zero(isl_local_space_from_dim(dim
));
1294 aff
= isl_aff_add_constant_si(aff
, -1);
1295 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1300 return isl_pw_aff_zero_set(pwaff1
);
1301 return isl_pw_aff_nonneg_set(pwaff1
);
1304 /* Return a set containing those elements in the shared domain
1305 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1307 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1308 __isl_take isl_pw_aff
*pwaff2
)
1310 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1313 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1314 __isl_take isl_pw_aff
*pwaff2
)
1316 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1319 /* Return a set containing those elements in the shared domain
1320 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1322 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1323 __isl_take isl_pw_aff
*pwaff2
)
1325 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1328 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1329 __isl_take isl_pw_aff
*pwaff2
)
1331 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1334 /* Return a set containing those elements in the shared domain
1335 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1337 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1338 __isl_take isl_pw_aff
*pwaff2
)
1340 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1343 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1344 __isl_take isl_pw_aff
*pwaff2
)
1346 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1349 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1350 __isl_take isl_pw_aff
*pwaff2
)
1352 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1355 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1356 __isl_take isl_pw_aff
*pwaff2
)
1358 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1361 /* Return a set containing those elements in the shared domain
1362 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1364 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1365 __isl_take isl_pw_aff
*pwaff2
)
1367 isl_set
*set_lt
, *set_gt
;
1369 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1370 isl_pw_aff_copy(pwaff2
));
1371 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1372 return isl_set_union_disjoint(set_lt
, set_gt
);
1375 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1376 __isl_take isl_pw_aff
*pwaff2
)
1378 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1381 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1386 if (isl_int_is_one(v
))
1388 if (!isl_int_is_pos(v
))
1389 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1390 "factor needs to be positive",
1391 return isl_pw_aff_free(pwaff
));
1392 pwaff
= isl_pw_aff_cow(pwaff
);
1398 for (i
= 0; i
< pwaff
->n
; ++i
) {
1399 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1400 if (!pwaff
->p
[i
].aff
)
1401 return isl_pw_aff_free(pwaff
);
1407 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1411 pwaff
= isl_pw_aff_cow(pwaff
);
1417 for (i
= 0; i
< pwaff
->n
; ++i
) {
1418 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1419 if (!pwaff
->p
[i
].aff
)
1420 return isl_pw_aff_free(pwaff
);
1426 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1430 pwaff
= isl_pw_aff_cow(pwaff
);
1436 for (i
= 0; i
< pwaff
->n
; ++i
) {
1437 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1438 if (!pwaff
->p
[i
].aff
)
1439 return isl_pw_aff_free(pwaff
);
1445 /* Return an affine expression that is equal to pwaff_true for elements
1446 * in "cond" and to pwaff_false for elements not in "cond".
1447 * That is, return cond ? pwaff_true : pwaff_false;
1449 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_set
*cond
,
1450 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1454 comp
= isl_set_complement(isl_set_copy(cond
));
1455 pwaff_true
= isl_pw_aff_intersect_domain(pwaff_true
, cond
);
1456 pwaff_false
= isl_pw_aff_intersect_domain(pwaff_false
, comp
);
1458 return isl_pw_aff_add_disjoint(pwaff_true
, pwaff_false
);
1461 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1466 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1469 /* Check whether pwaff is a piecewise constant.
1471 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1478 for (i
= 0; i
< pwaff
->n
; ++i
) {
1479 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1480 if (is_cst
< 0 || !is_cst
)
1487 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1488 __isl_take isl_aff
*aff2
)
1490 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1491 return isl_aff_mul(aff2
, aff1
);
1493 if (!isl_aff_is_cst(aff2
))
1494 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1495 "at least one affine expression should be constant",
1498 aff1
= isl_aff_cow(aff1
);
1502 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1503 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1513 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1514 __isl_take isl_pw_aff
*pwaff2
)
1519 if (!pwaff1
|| !pwaff2
)
1522 n
= pwaff1
->n
* pwaff2
->n
;
1523 res
= isl_pw_aff_alloc_(isl_dim_copy(pwaff1
->dim
), n
);
1525 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1526 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1529 common
= isl_set_intersect(
1530 isl_set_copy(pwaff1
->p
[i
].set
),
1531 isl_set_copy(pwaff2
->p
[j
].set
));
1532 if (isl_set_plain_is_empty(common
)) {
1533 isl_set_free(common
);
1537 prod
= isl_aff_mul(isl_aff_copy(pwaff1
->p
[i
].aff
),
1538 isl_aff_copy(pwaff2
->p
[j
].aff
));
1540 res
= isl_pw_aff_add_piece(res
, common
, prod
);
1544 isl_pw_aff_free(pwaff1
);
1545 isl_pw_aff_free(pwaff2
);
1548 isl_pw_aff_free(pwaff1
);
1549 isl_pw_aff_free(pwaff2
);
1553 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1554 __isl_take isl_pw_aff
*pwaff2
)
1556 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1559 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1560 __isl_take isl_pw_aff
*pwaff2
)
1564 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
1565 isl_pw_aff_copy(pwaff2
));
1566 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1569 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1570 __isl_take isl_pw_aff
*pwaff2
)
1572 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
1575 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1576 __isl_take isl_pw_aff
*pwaff2
)
1580 le
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
1581 isl_pw_aff_copy(pwaff2
));
1582 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1585 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1586 __isl_take isl_pw_aff
*pwaff2
)
1588 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);