2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
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,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_space_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_list_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_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
139 return aff
? isl_local_space_get_space(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_space(__isl_take isl_aff
*aff
,
154 __isl_take isl_space
*dim
)
156 aff
= isl_aff_cow(aff
);
160 aff
->ls
= isl_local_space_reset_space(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
,
186 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
187 isl_seq_cpy(res
->el
, vec
->el
, 2);
188 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
189 for (i
= 0; i
< r
->len
; ++i
)
190 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
192 isl_reordering_free(r
);
197 isl_reordering_free(r
);
201 /* Reorder the dimensions of "aff" according to the given reordering.
203 __isl_give isl_aff
*isl_aff_realign(__isl_take isl_aff
*aff
,
204 __isl_take isl_reordering
*r
)
206 aff
= isl_aff_cow(aff
);
210 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
211 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
212 aff
->ls
->div
->n_row
);
213 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
215 if (!aff
->v
|| !aff
->ls
)
216 return isl_aff_free(aff
);
221 isl_reordering_free(r
);
225 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
230 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
233 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
240 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
241 if (equal
< 0 || !equal
)
244 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
247 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
251 isl_int_set(*v
, aff
->v
->el
[0]);
255 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
259 isl_int_set(*v
, aff
->v
->el
[1]);
263 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
264 enum isl_dim_type type
, int pos
, isl_int
*v
)
269 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
270 isl_die(aff
->v
->ctx
, isl_error_invalid
,
271 "position out of bounds", return -1);
273 pos
+= isl_local_space_offset(aff
->ls
, type
);
274 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
279 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
281 aff
= isl_aff_cow(aff
);
285 aff
->v
= isl_vec_cow(aff
->v
);
287 return isl_aff_free(aff
);
289 isl_int_set(aff
->v
->el
[0], v
);
294 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
296 aff
= isl_aff_cow(aff
);
300 aff
->v
= isl_vec_cow(aff
->v
);
302 return isl_aff_free(aff
);
304 isl_int_set(aff
->v
->el
[1], v
);
309 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
311 if (isl_int_is_zero(v
))
314 aff
= isl_aff_cow(aff
);
318 aff
->v
= isl_vec_cow(aff
->v
);
320 return isl_aff_free(aff
);
322 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
327 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
332 isl_int_set_si(t
, v
);
333 aff
= isl_aff_add_constant(aff
, t
);
339 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
341 aff
= isl_aff_cow(aff
);
345 aff
->v
= isl_vec_cow(aff
->v
);
347 return isl_aff_free(aff
);
349 isl_int_set_si(aff
->v
->el
[1], v
);
354 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
355 enum isl_dim_type type
, int pos
, isl_int v
)
360 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
361 isl_die(aff
->v
->ctx
, isl_error_invalid
,
362 "position out of bounds", return isl_aff_free(aff
));
364 aff
= isl_aff_cow(aff
);
368 aff
->v
= isl_vec_cow(aff
->v
);
370 return isl_aff_free(aff
);
372 pos
+= isl_local_space_offset(aff
->ls
, type
);
373 isl_int_set(aff
->v
->el
[1 + pos
], v
);
378 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
379 enum isl_dim_type type
, int pos
, int v
)
384 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
385 isl_die(aff
->v
->ctx
, isl_error_invalid
,
386 "position out of bounds", return isl_aff_free(aff
));
388 aff
= isl_aff_cow(aff
);
392 aff
->v
= isl_vec_cow(aff
->v
);
394 return isl_aff_free(aff
);
396 pos
+= isl_local_space_offset(aff
->ls
, type
);
397 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
402 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
403 enum isl_dim_type type
, int pos
, isl_int v
)
408 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
409 isl_die(aff
->v
->ctx
, isl_error_invalid
,
410 "position out of bounds", return isl_aff_free(aff
));
412 aff
= isl_aff_cow(aff
);
416 aff
->v
= isl_vec_cow(aff
->v
);
418 return isl_aff_free(aff
);
420 pos
+= isl_local_space_offset(aff
->ls
, type
);
421 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
426 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
427 enum isl_dim_type type
, int pos
, int v
)
432 isl_int_set_si(t
, v
);
433 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
439 __isl_give isl_div
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
444 return isl_local_space_get_div(aff
->ls
, pos
);
447 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
449 aff
= isl_aff_cow(aff
);
452 aff
->v
= isl_vec_cow(aff
->v
);
454 return isl_aff_free(aff
);
456 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
461 /* Given f, return floor(f).
462 * If f is an integer expression, then just return f.
463 * Otherwise, if f = g/m, write g = q m + r,
464 * create a new div d = [r/m] and return the expression q + d.
465 * The coefficients in r are taken to lie between -m/2 and m/2.
467 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
477 if (isl_int_is_one(aff
->v
->el
[0]))
480 aff
= isl_aff_cow(aff
);
484 aff
->v
= isl_vec_cow(aff
->v
);
485 div
= isl_vec_copy(aff
->v
);
486 div
= isl_vec_cow(div
);
488 return isl_aff_free(aff
);
490 ctx
= isl_aff_get_ctx(aff
);
491 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
492 for (i
= 1; i
< aff
->v
->size
; ++i
) {
493 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
494 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
495 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
496 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
497 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
501 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
503 return isl_aff_free(aff
);
506 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
508 return isl_aff_free(aff
);
509 isl_int_set_si(aff
->v
->el
[0], 1);
510 isl_int_set_si(aff
->v
->el
[size
], 1);
517 * aff mod m = aff - m * floor(aff/m)
519 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
523 res
= isl_aff_copy(aff
);
524 aff
= isl_aff_scale_down(aff
, m
);
525 aff
= isl_aff_floor(aff
);
526 aff
= isl_aff_scale(aff
, m
);
527 res
= isl_aff_sub(res
, aff
);
534 * pwaff mod m = pwaff - m * floor(pwaff/m)
536 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
540 res
= isl_pw_aff_copy(pwaff
);
541 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
542 pwaff
= isl_pw_aff_floor(pwaff
);
543 pwaff
= isl_pw_aff_scale(pwaff
, m
);
544 res
= isl_pw_aff_sub(res
, pwaff
);
549 /* Given f, return ceil(f).
550 * If f is an integer expression, then just return f.
551 * Otherwise, create a new div d = [-f] and return the expression -d.
553 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
558 if (isl_int_is_one(aff
->v
->el
[0]))
561 aff
= isl_aff_neg(aff
);
562 aff
= isl_aff_floor(aff
);
563 aff
= isl_aff_neg(aff
);
568 /* Apply the expansion computed by isl_merge_divs.
569 * The expansion itself is given by "exp" while the resulting
570 * list of divs is given by "div".
572 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
573 __isl_take isl_mat
*div
, int *exp
)
580 aff
= isl_aff_cow(aff
);
584 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
585 new_n_div
= isl_mat_rows(div
);
586 if (new_n_div
< old_n_div
)
587 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
588 "not an expansion", goto error
);
590 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
594 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
596 for (i
= new_n_div
- 1; i
>= 0; --i
) {
597 if (j
>= 0 && exp
[j
] == i
) {
599 isl_int_swap(aff
->v
->el
[offset
+ i
],
600 aff
->v
->el
[offset
+ j
]);
603 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
606 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
617 /* Add two affine expressions that live in the same local space.
619 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
620 __isl_take isl_aff
*aff2
)
624 aff1
= isl_aff_cow(aff1
);
628 aff1
->v
= isl_vec_cow(aff1
->v
);
634 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
635 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
636 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
637 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
638 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
639 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
640 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
652 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
653 __isl_take isl_aff
*aff2
)
663 ctx
= isl_aff_get_ctx(aff1
);
664 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
665 isl_die(ctx
, isl_error_invalid
,
666 "spaces don't match", goto error
);
668 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
669 return add_expanded(aff1
, aff2
);
671 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
672 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
676 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
677 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
678 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
682 return add_expanded(aff1
, aff2
);
691 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
692 __isl_take isl_aff
*aff2
)
694 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
697 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
701 if (isl_int_is_one(f
))
704 aff
= isl_aff_cow(aff
);
707 aff
->v
= isl_vec_cow(aff
->v
);
709 return isl_aff_free(aff
);
712 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
713 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
714 isl_int_divexact(gcd
, f
, gcd
);
715 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
721 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
725 if (isl_int_is_one(f
))
728 aff
= isl_aff_cow(aff
);
731 aff
->v
= isl_vec_cow(aff
->v
);
733 return isl_aff_free(aff
);
736 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
737 isl_int_gcd(gcd
, gcd
, f
);
738 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
739 isl_int_divexact(gcd
, f
, gcd
);
740 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
746 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
754 isl_int_set_ui(v
, f
);
755 aff
= isl_aff_scale_down(aff
, v
);
761 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
762 enum isl_dim_type type
, unsigned pos
, const char *s
)
764 aff
= isl_aff_cow(aff
);
767 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
769 return isl_aff_free(aff
);
774 /* Exploit the equalities in "eq" to simplify the affine expression
775 * and the expressions of the integer divisions in the local space.
776 * The integer divisions in this local space are assumed to appear
777 * as regular dimensions in "eq".
779 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
780 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
789 isl_basic_set_free(eq
);
793 aff
= isl_aff_cow(aff
);
797 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
798 isl_basic_set_copy(eq
));
802 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
804 for (i
= 0; i
< eq
->n_eq
; ++i
) {
805 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
806 if (j
< 0 || j
== 0 || j
>= total
)
809 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
813 isl_basic_set_free(eq
);
816 isl_basic_set_free(eq
);
821 /* Exploit the equalities in "eq" to simplify the affine expression
822 * and the expressions of the integer divisions in the local space.
824 static __isl_give isl_aff
*isl_aff_substitute_equalities(
825 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
831 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
833 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
834 return isl_aff_substitute_equalities_lifted(aff
, eq
);
836 isl_basic_set_free(eq
);
841 /* Look for equalities among the variables shared by context and aff
842 * and the integer divisions of aff, if any.
843 * The equalities are then used to eliminate coefficients and/or integer
844 * divisions from aff.
846 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
847 __isl_take isl_set
*context
)
854 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
857 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
858 bset
= isl_basic_set_from_local_space(
859 isl_aff_get_local_space(aff
));
860 bset
= isl_basic_set_lift(bset
);
861 bset
= isl_basic_set_flatten(bset
);
862 context
= isl_set_intersect(context
,
863 isl_set_from_basic_set(bset
));
866 hull
= isl_set_affine_hull(context
);
867 return isl_aff_substitute_equalities_lifted(aff
, hull
);
870 isl_set_free(context
);
874 /* Return a basic set containing those elements in the space
875 * of aff where it is non-negative.
877 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
879 isl_constraint
*ineq
;
881 ineq
= isl_inequality_from_aff(aff
);
883 return isl_basic_set_from_constraint(ineq
);
886 /* Return a basic set containing those elements in the space
887 * of aff where it is zero.
889 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
891 isl_constraint
*ineq
;
893 ineq
= isl_equality_from_aff(aff
);
895 return isl_basic_set_from_constraint(ineq
);
898 /* Return a basic set containing those elements in the shared space
899 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
901 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
902 __isl_take isl_aff
*aff2
)
904 aff1
= isl_aff_sub(aff1
, aff2
);
906 return isl_aff_nonneg_basic_set(aff1
);
909 /* Return a basic set containing those elements in the shared space
910 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
912 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
913 __isl_take isl_aff
*aff2
)
915 return isl_aff_ge_basic_set(aff2
, aff1
);
918 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
919 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
921 aff1
= isl_aff_add(aff1
, aff2
);
922 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
926 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
934 /* Set active[i] to 1 if the dimension at position i is involved
935 * in the affine expression.
937 static int set_active(__isl_keep isl_aff
*aff
, int *active
)
946 total
= aff
->v
->size
- 2;
947 for (i
= 0; i
< total
; ++i
)
948 active
[i
] = !isl_int_is_zero(aff
->v
->el
[2 + i
]);
950 offset
= isl_local_space_offset(aff
->ls
, isl_dim_div
) - 1;
951 for (i
= aff
->ls
->div
->n_row
- 1; i
>= 0; --i
) {
952 if (!active
[offset
+ i
])
954 for (j
= 0; j
< total
; ++j
)
956 !isl_int_is_zero(aff
->ls
->div
->row
[i
][2 + j
]);
962 /* Check whether the given affine expression has non-zero coefficient
963 * for any dimension in the given range or if any of these dimensions
964 * appear with non-zero coefficients in any of the integer divisions
965 * involved in the affine expression.
967 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
968 enum isl_dim_type type
, unsigned first
, unsigned n
)
980 ctx
= isl_aff_get_ctx(aff
);
981 if (first
+ n
> isl_aff_dim(aff
, type
))
982 isl_die(ctx
, isl_error_invalid
,
983 "range out of bounds", return -1);
985 active
= isl_calloc_array(ctx
, int,
986 isl_local_space_dim(aff
->ls
, isl_dim_all
));
987 if (set_active(aff
, active
) < 0)
990 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
991 for (i
= 0; i
< n
; ++i
)
992 if (active
[first
+ i
]) {
1005 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1006 enum isl_dim_type type
, unsigned first
, unsigned n
)
1012 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1015 ctx
= isl_aff_get_ctx(aff
);
1016 if (first
+ n
> isl_aff_dim(aff
, type
))
1017 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1018 return isl_aff_free(aff
));
1020 aff
= isl_aff_cow(aff
);
1024 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1026 return isl_aff_free(aff
);
1028 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1029 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1031 return isl_aff_free(aff
);
1036 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1037 enum isl_dim_type type
, unsigned first
, unsigned n
)
1043 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1046 ctx
= isl_aff_get_ctx(aff
);
1047 if (first
> isl_aff_dim(aff
, type
))
1048 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1049 return isl_aff_free(aff
));
1051 aff
= isl_aff_cow(aff
);
1055 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1057 return isl_aff_free(aff
);
1059 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1060 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1062 return isl_aff_free(aff
);
1067 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1068 enum isl_dim_type type
, unsigned n
)
1072 pos
= isl_aff_dim(aff
, type
);
1074 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1077 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1078 enum isl_dim_type type
, unsigned n
)
1082 pos
= isl_pw_aff_dim(pwaff
, type
);
1084 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1087 __isl_give isl_pw_aff
*isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff
*pwaff
,
1088 __isl_take isl_id
*id
)
1092 dim
= isl_pw_aff_get_space(pwaff
);
1093 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
1095 return isl_pw_aff_reset_space(pwaff
, dim
);
1098 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1100 isl_set
*dom
= isl_set_universe(isl_aff_get_space(aff
));
1101 return isl_pw_aff_alloc(dom
, aff
);
1105 #define PW isl_pw_aff
1109 #define EL_IS_ZERO is_empty
1113 #define IS_ZERO is_empty
1119 #define NO_MOVE_DIMS
1123 #include <isl_pw_templ.c>
1125 static __isl_give isl_set
*align_params_pw_pw_set_and(
1126 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1127 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1128 __isl_take isl_pw_aff
*pwaff2
))
1130 if (!pwaff1
|| !pwaff2
)
1132 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1133 pwaff2
->dim
, isl_dim_param
))
1134 return fn(pwaff1
, pwaff2
);
1135 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1136 !isl_space_has_named_params(pwaff2
->dim
))
1137 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1138 "unaligned unnamed parameters", goto error
);
1139 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1140 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1141 return fn(pwaff1
, pwaff2
);
1143 isl_pw_aff_free(pwaff1
);
1144 isl_pw_aff_free(pwaff2
);
1148 /* Compute a piecewise quasi-affine expression with a domain that
1149 * is the union of those of pwaff1 and pwaff2 and such that on each
1150 * cell, the quasi-affine expression is the better (according to cmp)
1151 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1152 * is defined on a given cell, then the associated expression
1153 * is the defined one.
1155 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1156 __isl_take isl_pw_aff
*pwaff2
,
1157 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1158 __isl_take isl_aff
*aff2
))
1165 if (!pwaff1
|| !pwaff2
)
1168 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1169 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1170 isl_die(ctx
, isl_error_invalid
,
1171 "arguments should live in same space", goto error
);
1173 if (isl_pw_aff_is_empty(pwaff1
)) {
1174 isl_pw_aff_free(pwaff1
);
1178 if (isl_pw_aff_is_empty(pwaff2
)) {
1179 isl_pw_aff_free(pwaff2
);
1183 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1184 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1186 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1187 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1188 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1189 struct isl_set
*common
;
1192 common
= isl_set_intersect(
1193 isl_set_copy(pwaff1
->p
[i
].set
),
1194 isl_set_copy(pwaff2
->p
[j
].set
));
1195 better
= isl_set_from_basic_set(cmp(
1196 isl_aff_copy(pwaff2
->p
[j
].aff
),
1197 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1198 better
= isl_set_intersect(common
, better
);
1199 if (isl_set_plain_is_empty(better
)) {
1200 isl_set_free(better
);
1203 set
= isl_set_subtract(set
, isl_set_copy(better
));
1205 res
= isl_pw_aff_add_piece(res
, better
,
1206 isl_aff_copy(pwaff2
->p
[j
].aff
));
1208 res
= isl_pw_aff_add_piece(res
, set
,
1209 isl_aff_copy(pwaff1
->p
[i
].aff
));
1212 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1213 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1214 for (i
= 0; i
< pwaff1
->n
; ++i
)
1215 set
= isl_set_subtract(set
,
1216 isl_set_copy(pwaff1
->p
[i
].set
));
1217 res
= isl_pw_aff_add_piece(res
, set
,
1218 isl_aff_copy(pwaff2
->p
[j
].aff
));
1221 isl_pw_aff_free(pwaff1
);
1222 isl_pw_aff_free(pwaff2
);
1226 isl_pw_aff_free(pwaff1
);
1227 isl_pw_aff_free(pwaff2
);
1231 /* Compute a piecewise quasi-affine expression with a domain that
1232 * is the union of those of pwaff1 and pwaff2 and such that on each
1233 * cell, the quasi-affine expression is the maximum of those of pwaff1
1234 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1235 * cell, then the associated expression is the defined one.
1237 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1238 __isl_take isl_pw_aff
*pwaff2
)
1240 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1243 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1244 __isl_take isl_pw_aff
*pwaff2
)
1246 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_union_max
);
1249 /* Compute a piecewise quasi-affine expression with a domain that
1250 * is the union of those of pwaff1 and pwaff2 and such that on each
1251 * cell, the quasi-affine expression is the minimum of those of pwaff1
1252 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1253 * cell, then the associated expression is the defined one.
1255 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1256 __isl_take isl_pw_aff
*pwaff2
)
1258 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1261 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1262 __isl_take isl_pw_aff
*pwaff2
)
1264 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_union_min
);
1267 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1268 __isl_take isl_pw_aff
*pwaff2
, int max
)
1271 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1273 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1276 /* Construct a map with as domain the domain of pwaff and
1277 * one-dimensional range corresponding to the affine expressions.
1279 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1288 dim
= isl_pw_aff_get_space(pwaff
);
1289 dim
= isl_space_from_domain(dim
);
1290 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
1291 map
= isl_map_empty(dim
);
1293 for (i
= 0; i
< pwaff
->n
; ++i
) {
1294 isl_basic_map
*bmap
;
1297 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1298 map_i
= isl_map_from_basic_map(bmap
);
1299 map_i
= isl_map_intersect_domain(map_i
,
1300 isl_set_copy(pwaff
->p
[i
].set
));
1301 map
= isl_map_union_disjoint(map
, map_i
);
1304 isl_pw_aff_free(pwaff
);
1309 /* Construct a map with as domain the domain of pwaff and
1310 * one-dimensional range corresponding to the affine expressions.
1312 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1314 if (isl_space_is_params(pwaff
->dim
))
1315 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1316 "space of input is not a map",
1317 return isl_pw_aff_free(pwaff
));
1318 return map_from_pw_aff(pwaff
);
1321 /* Construct a one-dimensional set with as parameter domain
1322 * the domain of pwaff and the single set dimension
1323 * corresponding to the affine expressions.
1325 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1327 if (!isl_space_is_params(pwaff
->dim
))
1328 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1329 "space of input is not a set",
1330 return isl_pw_aff_free(pwaff
));
1331 return map_from_pw_aff(pwaff
);
1334 /* Return a set containing those elements in the domain
1335 * of pwaff where it is non-negative.
1337 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1345 set
= isl_set_empty(isl_pw_aff_get_space(pwaff
));
1347 for (i
= 0; i
< pwaff
->n
; ++i
) {
1348 isl_basic_set
*bset
;
1351 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1352 set_i
= isl_set_from_basic_set(bset
);
1353 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1354 set
= isl_set_union_disjoint(set
, set_i
);
1357 isl_pw_aff_free(pwaff
);
1362 /* Return a set containing those elements in the domain
1363 * of pwaff where it is zero.
1365 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1373 set
= isl_set_empty(isl_pw_aff_get_space(pwaff
));
1375 for (i
= 0; i
< pwaff
->n
; ++i
) {
1376 isl_basic_set
*bset
;
1379 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1380 set_i
= isl_set_from_basic_set(bset
);
1381 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1382 set
= isl_set_union_disjoint(set
, set_i
);
1385 isl_pw_aff_free(pwaff
);
1390 /* Return a set containing those elements in the domain
1391 * of pwaff where it is not zero.
1393 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1395 return isl_set_complement(isl_pw_aff_zero_set(pwaff
));
1398 /* Return a set containing those elements in the shared domain
1399 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1401 * We compute the difference on the shared domain and then construct
1402 * the set of values where this difference is non-negative.
1403 * If strict is set, we first subtract 1 from the difference.
1404 * If equal is set, we only return the elements where pwaff1 and pwaff2
1407 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1408 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1410 isl_set
*set1
, *set2
;
1412 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1413 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1414 set1
= isl_set_intersect(set1
, set2
);
1415 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1416 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1417 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1420 isl_space
*dim
= isl_set_get_space(set1
);
1422 aff
= isl_aff_zero(isl_local_space_from_space(dim
));
1423 aff
= isl_aff_add_constant_si(aff
, -1);
1424 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1429 return isl_pw_aff_zero_set(pwaff1
);
1430 return isl_pw_aff_nonneg_set(pwaff1
);
1433 /* Return a set containing those elements in the shared domain
1434 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1436 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1437 __isl_take isl_pw_aff
*pwaff2
)
1439 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1442 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1443 __isl_take isl_pw_aff
*pwaff2
)
1445 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1448 /* Return a set containing those elements in the shared domain
1449 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1451 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1452 __isl_take isl_pw_aff
*pwaff2
)
1454 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1457 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1458 __isl_take isl_pw_aff
*pwaff2
)
1460 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1463 /* Return a set containing those elements in the shared domain
1464 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1466 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1467 __isl_take isl_pw_aff
*pwaff2
)
1469 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1472 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1473 __isl_take isl_pw_aff
*pwaff2
)
1475 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1478 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1479 __isl_take isl_pw_aff
*pwaff2
)
1481 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1484 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1485 __isl_take isl_pw_aff
*pwaff2
)
1487 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1490 /* Return a set containing those elements in the shared domain
1491 * of the elements of list1 and list2 where each element in list1
1492 * has the relation specified by "fn" with each element in list2.
1494 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1495 __isl_take isl_pw_aff_list
*list2
,
1496 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1497 __isl_take isl_pw_aff
*pwaff2
))
1503 if (!list1
|| !list2
)
1506 ctx
= isl_pw_aff_list_get_ctx(list1
);
1507 if (list1
->n
< 1 || list2
->n
< 1)
1508 isl_die(ctx
, isl_error_invalid
,
1509 "list should contain at least one element", goto error
);
1511 set
= isl_set_universe(isl_pw_aff_get_space(list1
->p
[0]));
1512 for (i
= 0; i
< list1
->n
; ++i
)
1513 for (j
= 0; j
< list2
->n
; ++j
) {
1516 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1517 isl_pw_aff_copy(list2
->p
[j
]));
1518 set
= isl_set_intersect(set
, set_ij
);
1521 isl_pw_aff_list_free(list1
);
1522 isl_pw_aff_list_free(list2
);
1525 isl_pw_aff_list_free(list1
);
1526 isl_pw_aff_list_free(list2
);
1530 /* Return a set containing those elements in the shared domain
1531 * of the elements of list1 and list2 where each element in list1
1532 * is equal to each element in list2.
1534 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1535 __isl_take isl_pw_aff_list
*list2
)
1537 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1540 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1541 __isl_take isl_pw_aff_list
*list2
)
1543 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1546 /* Return a set containing those elements in the shared domain
1547 * of the elements of list1 and list2 where each element in list1
1548 * is less than or equal to each element in list2.
1550 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1551 __isl_take isl_pw_aff_list
*list2
)
1553 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1556 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1557 __isl_take isl_pw_aff_list
*list2
)
1559 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1562 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1563 __isl_take isl_pw_aff_list
*list2
)
1565 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1568 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1569 __isl_take isl_pw_aff_list
*list2
)
1571 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1575 /* Return a set containing those elements in the shared domain
1576 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1578 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1579 __isl_take isl_pw_aff
*pwaff2
)
1581 isl_set
*set_lt
, *set_gt
;
1583 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1584 isl_pw_aff_copy(pwaff2
));
1585 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1586 return isl_set_union_disjoint(set_lt
, set_gt
);
1589 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1590 __isl_take isl_pw_aff
*pwaff2
)
1592 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1595 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1600 if (isl_int_is_one(v
))
1602 if (!isl_int_is_pos(v
))
1603 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1604 "factor needs to be positive",
1605 return isl_pw_aff_free(pwaff
));
1606 pwaff
= isl_pw_aff_cow(pwaff
);
1612 for (i
= 0; i
< pwaff
->n
; ++i
) {
1613 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1614 if (!pwaff
->p
[i
].aff
)
1615 return isl_pw_aff_free(pwaff
);
1621 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1625 pwaff
= isl_pw_aff_cow(pwaff
);
1631 for (i
= 0; i
< pwaff
->n
; ++i
) {
1632 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1633 if (!pwaff
->p
[i
].aff
)
1634 return isl_pw_aff_free(pwaff
);
1640 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1644 pwaff
= isl_pw_aff_cow(pwaff
);
1650 for (i
= 0; i
< pwaff
->n
; ++i
) {
1651 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1652 if (!pwaff
->p
[i
].aff
)
1653 return isl_pw_aff_free(pwaff
);
1659 /* Return an affine expression that is equal to pwaff_true for elements
1660 * in "cond" and to pwaff_false for elements not in "cond".
1661 * That is, return cond ? pwaff_true : pwaff_false;
1663 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_set
*cond
,
1664 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1668 comp
= isl_set_complement(isl_set_copy(cond
));
1669 pwaff_true
= isl_pw_aff_intersect_domain(pwaff_true
, cond
);
1670 pwaff_false
= isl_pw_aff_intersect_domain(pwaff_false
, comp
);
1672 return isl_pw_aff_add_disjoint(pwaff_true
, pwaff_false
);
1675 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1680 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1683 /* Check whether pwaff is a piecewise constant.
1685 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1692 for (i
= 0; i
< pwaff
->n
; ++i
) {
1693 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1694 if (is_cst
< 0 || !is_cst
)
1701 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1702 __isl_take isl_aff
*aff2
)
1704 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1705 return isl_aff_mul(aff2
, aff1
);
1707 if (!isl_aff_is_cst(aff2
))
1708 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1709 "at least one affine expression should be constant",
1712 aff1
= isl_aff_cow(aff1
);
1716 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1717 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1727 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1728 __isl_take isl_pw_aff
*pwaff2
)
1733 if (!pwaff1
|| !pwaff2
)
1736 n
= pwaff1
->n
* pwaff2
->n
;
1737 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1739 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1740 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1743 common
= isl_set_intersect(
1744 isl_set_copy(pwaff1
->p
[i
].set
),
1745 isl_set_copy(pwaff2
->p
[j
].set
));
1746 if (isl_set_plain_is_empty(common
)) {
1747 isl_set_free(common
);
1751 prod
= isl_aff_mul(isl_aff_copy(pwaff1
->p
[i
].aff
),
1752 isl_aff_copy(pwaff2
->p
[j
].aff
));
1754 res
= isl_pw_aff_add_piece(res
, common
, prod
);
1758 isl_pw_aff_free(pwaff1
);
1759 isl_pw_aff_free(pwaff2
);
1762 isl_pw_aff_free(pwaff1
);
1763 isl_pw_aff_free(pwaff2
);
1767 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1768 __isl_take isl_pw_aff
*pwaff2
)
1770 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1773 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1774 __isl_take isl_pw_aff
*pwaff2
)
1778 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
1779 isl_pw_aff_copy(pwaff2
));
1780 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1783 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1784 __isl_take isl_pw_aff
*pwaff2
)
1786 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
1789 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1790 __isl_take isl_pw_aff
*pwaff2
)
1794 le
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
1795 isl_pw_aff_copy(pwaff2
));
1796 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1799 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1800 __isl_take isl_pw_aff
*pwaff2
)
1802 return align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
1805 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
1806 __isl_take isl_pw_aff_list
*list
,
1807 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1808 __isl_take isl_pw_aff
*pwaff2
))
1817 ctx
= isl_pw_aff_list_get_ctx(list
);
1819 isl_die(ctx
, isl_error_invalid
,
1820 "list should contain at least one element",
1821 return isl_pw_aff_list_free(list
));
1823 res
= isl_pw_aff_copy(list
->p
[0]);
1824 for (i
= 1; i
< list
->n
; ++i
)
1825 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
1827 isl_pw_aff_list_free(list
);
1831 /* Return an isl_pw_aff that maps each element in the intersection of the
1832 * domains of the elements of list to the minimal corresponding affine
1835 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
1837 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
1840 /* Return an isl_pw_aff that maps each element in the intersection of the
1841 * domains of the elements of list to the maximal corresponding affine
1844 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
1846 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);