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",
59 if (!isl_local_space_is_set(ls
))
60 isl_die(ctx
, isl_error_invalid
,
61 "domain of affine expression should be a set",
64 total
= isl_local_space_dim(ls
, isl_dim_all
);
65 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
66 return isl_aff_alloc_vec(ls
, v
);
68 isl_local_space_free(ls
);
72 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
76 aff
= isl_aff_alloc(ls
);
80 isl_int_set_si(aff
->v
->el
[0], 1);
81 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
86 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
95 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
100 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
101 isl_vec_copy(aff
->v
));
104 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
112 return isl_aff_dup(aff
);
115 void *isl_aff_free(__isl_take isl_aff
*aff
)
123 isl_local_space_free(aff
->ls
);
124 isl_vec_free(aff
->v
);
131 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
133 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
136 /* Externally, an isl_aff has a map space, but internally, the
137 * ls field corresponds to the domain of that space.
139 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
143 if (type
== isl_dim_out
)
145 if (type
== isl_dim_in
)
147 return isl_local_space_dim(aff
->ls
, type
);
150 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
152 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
155 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
160 space
= isl_local_space_get_space(aff
->ls
);
161 space
= isl_space_from_domain(space
);
162 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
166 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
167 __isl_keep isl_aff
*aff
)
169 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
172 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
177 ls
= isl_local_space_copy(aff
->ls
);
178 ls
= isl_local_space_from_domain(ls
);
179 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
183 /* Externally, an isl_aff has a map space, but internally, the
184 * ls field corresponds to the domain of that space.
186 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
187 enum isl_dim_type type
, unsigned pos
)
191 if (type
== isl_dim_out
)
193 if (type
== isl_dim_in
)
195 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
198 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
199 __isl_take isl_space
*dim
)
201 aff
= isl_aff_cow(aff
);
205 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
207 return isl_aff_free(aff
);
216 /* Reset the space of "aff". This function is called from isl_pw_templ.c
217 * and doesn't know if the space of an element object is represented
218 * directly or through its domain. It therefore passes along both.
220 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
221 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
223 isl_space_free(space
);
224 return isl_aff_reset_domain_space(aff
, domain
);
227 /* Reorder the coefficients of the affine expression based
228 * on the given reodering.
229 * The reordering r is assumed to have been extended with the local
232 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
233 __isl_take isl_reordering
*r
, int n_div
)
241 res
= isl_vec_alloc(vec
->ctx
,
242 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
243 isl_seq_cpy(res
->el
, vec
->el
, 2);
244 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
245 for (i
= 0; i
< r
->len
; ++i
)
246 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
248 isl_reordering_free(r
);
253 isl_reordering_free(r
);
257 /* Reorder the dimensions of the domain of "aff" according
258 * to the given reordering.
260 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
261 __isl_take isl_reordering
*r
)
263 aff
= isl_aff_cow(aff
);
267 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
268 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
269 aff
->ls
->div
->n_row
);
270 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
272 if (!aff
->v
|| !aff
->ls
)
273 return isl_aff_free(aff
);
278 isl_reordering_free(r
);
282 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
287 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
290 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
297 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
298 if (equal
< 0 || !equal
)
301 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
304 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
308 isl_int_set(*v
, aff
->v
->el
[0]);
312 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
316 isl_int_set(*v
, aff
->v
->el
[1]);
320 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
321 enum isl_dim_type type
, int pos
, isl_int
*v
)
326 if (type
== isl_dim_out
)
327 isl_die(aff
->v
->ctx
, isl_error_invalid
,
328 "output/set dimension does not have a coefficient",
330 if (type
== isl_dim_in
)
333 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
334 isl_die(aff
->v
->ctx
, isl_error_invalid
,
335 "position out of bounds", return -1);
337 pos
+= isl_local_space_offset(aff
->ls
, type
);
338 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
343 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
345 aff
= isl_aff_cow(aff
);
349 aff
->v
= isl_vec_cow(aff
->v
);
351 return isl_aff_free(aff
);
353 isl_int_set(aff
->v
->el
[0], v
);
358 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
360 aff
= isl_aff_cow(aff
);
364 aff
->v
= isl_vec_cow(aff
->v
);
366 return isl_aff_free(aff
);
368 isl_int_set(aff
->v
->el
[1], v
);
373 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
375 if (isl_int_is_zero(v
))
378 aff
= isl_aff_cow(aff
);
382 aff
->v
= isl_vec_cow(aff
->v
);
384 return isl_aff_free(aff
);
386 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
391 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
396 isl_int_set_si(t
, v
);
397 aff
= isl_aff_add_constant(aff
, t
);
403 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
405 aff
= isl_aff_cow(aff
);
409 aff
->v
= isl_vec_cow(aff
->v
);
411 return isl_aff_free(aff
);
413 isl_int_set_si(aff
->v
->el
[1], v
);
418 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
419 enum isl_dim_type type
, int pos
, isl_int v
)
424 if (type
== isl_dim_out
)
425 isl_die(aff
->v
->ctx
, isl_error_invalid
,
426 "output/set dimension does not have a coefficient",
427 return isl_aff_free(aff
));
428 if (type
== isl_dim_in
)
431 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
432 isl_die(aff
->v
->ctx
, isl_error_invalid
,
433 "position out of bounds", return isl_aff_free(aff
));
435 aff
= isl_aff_cow(aff
);
439 aff
->v
= isl_vec_cow(aff
->v
);
441 return isl_aff_free(aff
);
443 pos
+= isl_local_space_offset(aff
->ls
, type
);
444 isl_int_set(aff
->v
->el
[1 + pos
], v
);
449 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
450 enum isl_dim_type type
, int pos
, int v
)
455 if (type
== isl_dim_out
)
456 isl_die(aff
->v
->ctx
, isl_error_invalid
,
457 "output/set dimension does not have a coefficient",
458 return isl_aff_free(aff
));
459 if (type
== isl_dim_in
)
462 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
463 isl_die(aff
->v
->ctx
, isl_error_invalid
,
464 "position out of bounds", return isl_aff_free(aff
));
466 aff
= isl_aff_cow(aff
);
470 aff
->v
= isl_vec_cow(aff
->v
);
472 return isl_aff_free(aff
);
474 pos
+= isl_local_space_offset(aff
->ls
, type
);
475 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
480 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
481 enum isl_dim_type type
, int pos
, isl_int v
)
486 if (type
== isl_dim_out
)
487 isl_die(aff
->v
->ctx
, isl_error_invalid
,
488 "output/set dimension does not have a coefficient",
489 return isl_aff_free(aff
));
490 if (type
== isl_dim_in
)
493 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
494 isl_die(aff
->v
->ctx
, isl_error_invalid
,
495 "position out of bounds", return isl_aff_free(aff
));
497 aff
= isl_aff_cow(aff
);
501 aff
->v
= isl_vec_cow(aff
->v
);
503 return isl_aff_free(aff
);
505 pos
+= isl_local_space_offset(aff
->ls
, type
);
506 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
511 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
512 enum isl_dim_type type
, int pos
, int v
)
517 isl_int_set_si(t
, v
);
518 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
524 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
529 return isl_local_space_get_div(aff
->ls
, pos
);
532 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
534 aff
= isl_aff_cow(aff
);
537 aff
->v
= isl_vec_cow(aff
->v
);
539 return isl_aff_free(aff
);
541 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
546 /* Given f, return floor(f).
547 * If f is an integer expression, then just return f.
548 * Otherwise, if f = g/m, write g = q m + r,
549 * create a new div d = [r/m] and return the expression q + d.
550 * The coefficients in r are taken to lie between -m/2 and m/2.
552 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
562 if (isl_int_is_one(aff
->v
->el
[0]))
565 aff
= isl_aff_cow(aff
);
569 aff
->v
= isl_vec_cow(aff
->v
);
570 div
= isl_vec_copy(aff
->v
);
571 div
= isl_vec_cow(div
);
573 return isl_aff_free(aff
);
575 ctx
= isl_aff_get_ctx(aff
);
576 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
577 for (i
= 1; i
< aff
->v
->size
; ++i
) {
578 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
579 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
580 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
581 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
582 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
586 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
588 return isl_aff_free(aff
);
591 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
593 return isl_aff_free(aff
);
594 isl_int_set_si(aff
->v
->el
[0], 1);
595 isl_int_set_si(aff
->v
->el
[size
], 1);
602 * aff mod m = aff - m * floor(aff/m)
604 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
608 res
= isl_aff_copy(aff
);
609 aff
= isl_aff_scale_down(aff
, m
);
610 aff
= isl_aff_floor(aff
);
611 aff
= isl_aff_scale(aff
, m
);
612 res
= isl_aff_sub(res
, aff
);
619 * pwaff mod m = pwaff - m * floor(pwaff/m)
621 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
625 res
= isl_pw_aff_copy(pwaff
);
626 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
627 pwaff
= isl_pw_aff_floor(pwaff
);
628 pwaff
= isl_pw_aff_scale(pwaff
, m
);
629 res
= isl_pw_aff_sub(res
, pwaff
);
634 /* Given f, return ceil(f).
635 * If f is an integer expression, then just return f.
636 * Otherwise, create a new div d = [-f] and return the expression -d.
638 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
643 if (isl_int_is_one(aff
->v
->el
[0]))
646 aff
= isl_aff_neg(aff
);
647 aff
= isl_aff_floor(aff
);
648 aff
= isl_aff_neg(aff
);
653 /* Apply the expansion computed by isl_merge_divs.
654 * The expansion itself is given by "exp" while the resulting
655 * list of divs is given by "div".
657 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
658 __isl_take isl_mat
*div
, int *exp
)
665 aff
= isl_aff_cow(aff
);
669 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
670 new_n_div
= isl_mat_rows(div
);
671 if (new_n_div
< old_n_div
)
672 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
673 "not an expansion", goto error
);
675 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
679 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
681 for (i
= new_n_div
- 1; i
>= 0; --i
) {
682 if (j
>= 0 && exp
[j
] == i
) {
684 isl_int_swap(aff
->v
->el
[offset
+ i
],
685 aff
->v
->el
[offset
+ j
]);
688 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
691 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
702 /* Add two affine expressions that live in the same local space.
704 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
705 __isl_take isl_aff
*aff2
)
709 aff1
= isl_aff_cow(aff1
);
713 aff1
->v
= isl_vec_cow(aff1
->v
);
719 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
720 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
721 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
722 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
723 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
724 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
725 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
737 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
738 __isl_take isl_aff
*aff2
)
748 ctx
= isl_aff_get_ctx(aff1
);
749 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
750 isl_die(ctx
, isl_error_invalid
,
751 "spaces don't match", goto error
);
753 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
754 return add_expanded(aff1
, aff2
);
756 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
757 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
761 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
762 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
763 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
767 return add_expanded(aff1
, aff2
);
776 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
777 __isl_take isl_aff
*aff2
)
779 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
782 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
786 if (isl_int_is_one(f
))
789 aff
= isl_aff_cow(aff
);
792 aff
->v
= isl_vec_cow(aff
->v
);
794 return isl_aff_free(aff
);
797 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
798 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
799 isl_int_divexact(gcd
, f
, gcd
);
800 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
806 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
810 if (isl_int_is_one(f
))
813 aff
= isl_aff_cow(aff
);
816 aff
->v
= isl_vec_cow(aff
->v
);
818 return isl_aff_free(aff
);
821 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
822 isl_int_gcd(gcd
, gcd
, f
);
823 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
824 isl_int_divexact(gcd
, f
, gcd
);
825 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
831 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
839 isl_int_set_ui(v
, f
);
840 aff
= isl_aff_scale_down(aff
, v
);
846 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
847 enum isl_dim_type type
, unsigned pos
, const char *s
)
849 aff
= isl_aff_cow(aff
);
852 if (type
== isl_dim_out
)
853 isl_die(aff
->v
->ctx
, isl_error_invalid
,
854 "cannot set name of output/set dimension",
855 return isl_aff_free(aff
));
856 if (type
== isl_dim_in
)
858 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
860 return isl_aff_free(aff
);
865 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
866 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
868 aff
= isl_aff_cow(aff
);
870 return isl_id_free(id
);
871 if (type
== isl_dim_out
)
872 isl_die(aff
->v
->ctx
, isl_error_invalid
,
873 "cannot set name of output/set dimension",
875 if (type
== isl_dim_in
)
877 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
879 return isl_aff_free(aff
);
888 /* Exploit the equalities in "eq" to simplify the affine expression
889 * and the expressions of the integer divisions in the local space.
890 * The integer divisions in this local space are assumed to appear
891 * as regular dimensions in "eq".
893 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
894 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
903 isl_basic_set_free(eq
);
907 aff
= isl_aff_cow(aff
);
911 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
912 isl_basic_set_copy(eq
));
916 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
918 for (i
= 0; i
< eq
->n_eq
; ++i
) {
919 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
920 if (j
< 0 || j
== 0 || j
>= total
)
923 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
927 isl_basic_set_free(eq
);
930 isl_basic_set_free(eq
);
935 /* Exploit the equalities in "eq" to simplify the affine expression
936 * and the expressions of the integer divisions in the local space.
938 static __isl_give isl_aff
*isl_aff_substitute_equalities(
939 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
945 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
947 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
948 return isl_aff_substitute_equalities_lifted(aff
, eq
);
950 isl_basic_set_free(eq
);
955 /* Look for equalities among the variables shared by context and aff
956 * and the integer divisions of aff, if any.
957 * The equalities are then used to eliminate coefficients and/or integer
958 * divisions from aff.
960 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
961 __isl_take isl_set
*context
)
968 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
972 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
973 ls
= isl_aff_get_domain_local_space(aff
);
974 bset
= isl_basic_set_from_local_space(ls
);
975 bset
= isl_basic_set_lift(bset
);
976 bset
= isl_basic_set_flatten(bset
);
977 context
= isl_set_intersect(context
,
978 isl_set_from_basic_set(bset
));
981 hull
= isl_set_affine_hull(context
);
982 return isl_aff_substitute_equalities_lifted(aff
, hull
);
985 isl_set_free(context
);
989 /* Return a basic set containing those elements in the space
990 * of aff where it is non-negative.
992 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
994 isl_constraint
*ineq
;
996 ineq
= isl_inequality_from_aff(aff
);
998 return isl_basic_set_from_constraint(ineq
);
1001 /* Return a basic set containing those elements in the space
1002 * of aff where it is zero.
1004 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1006 isl_constraint
*ineq
;
1008 ineq
= isl_equality_from_aff(aff
);
1010 return isl_basic_set_from_constraint(ineq
);
1013 /* Return a basic set containing those elements in the shared space
1014 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1016 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1017 __isl_take isl_aff
*aff2
)
1019 aff1
= isl_aff_sub(aff1
, aff2
);
1021 return isl_aff_nonneg_basic_set(aff1
);
1024 /* Return a basic set containing those elements in the shared space
1025 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1027 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1028 __isl_take isl_aff
*aff2
)
1030 return isl_aff_ge_basic_set(aff2
, aff1
);
1033 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1034 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1036 aff1
= isl_aff_add(aff1
, aff2
);
1037 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1041 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1049 /* Check whether the given affine expression has non-zero coefficient
1050 * for any dimension in the given range or if any of these dimensions
1051 * appear with non-zero coefficients in any of the integer divisions
1052 * involved in the affine expression.
1054 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1055 enum isl_dim_type type
, unsigned first
, unsigned n
)
1067 ctx
= isl_aff_get_ctx(aff
);
1068 if (first
+ n
> isl_aff_dim(aff
, type
))
1069 isl_die(ctx
, isl_error_invalid
,
1070 "range out of bounds", return -1);
1072 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1076 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1077 for (i
= 0; i
< n
; ++i
)
1078 if (active
[first
+ i
]) {
1091 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1092 enum isl_dim_type type
, unsigned first
, unsigned n
)
1098 if (type
== isl_dim_out
)
1099 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1100 "cannot drop output/set dimension",
1101 return isl_aff_free(aff
));
1102 if (type
== isl_dim_in
)
1104 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1107 ctx
= isl_aff_get_ctx(aff
);
1108 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1109 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1110 return isl_aff_free(aff
));
1112 aff
= isl_aff_cow(aff
);
1116 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1118 return isl_aff_free(aff
);
1120 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1121 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1123 return isl_aff_free(aff
);
1128 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1129 enum isl_dim_type type
, unsigned first
, unsigned n
)
1135 if (type
== isl_dim_out
)
1136 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1137 "cannot insert output/set dimensions",
1138 return isl_aff_free(aff
));
1139 if (type
== isl_dim_in
)
1141 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1144 ctx
= isl_aff_get_ctx(aff
);
1145 if (first
> isl_local_space_dim(aff
->ls
, type
))
1146 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1147 return isl_aff_free(aff
));
1149 aff
= isl_aff_cow(aff
);
1153 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1155 return isl_aff_free(aff
);
1157 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1158 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1160 return isl_aff_free(aff
);
1165 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1166 enum isl_dim_type type
, unsigned n
)
1170 pos
= isl_aff_dim(aff
, type
);
1172 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1175 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1176 enum isl_dim_type type
, unsigned n
)
1180 pos
= isl_pw_aff_dim(pwaff
, type
);
1182 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1185 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1187 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1188 return isl_pw_aff_alloc(dom
, aff
);
1192 #define PW isl_pw_aff
1196 #define EL_IS_ZERO is_empty
1200 #define IS_ZERO is_empty
1206 #define NO_MOVE_DIMS
1210 #include <isl_pw_templ.c>
1212 static __isl_give isl_set
*align_params_pw_pw_set_and(
1213 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1214 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1215 __isl_take isl_pw_aff
*pwaff2
))
1217 if (!pwaff1
|| !pwaff2
)
1219 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1220 pwaff2
->dim
, isl_dim_param
))
1221 return fn(pwaff1
, pwaff2
);
1222 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1223 !isl_space_has_named_params(pwaff2
->dim
))
1224 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1225 "unaligned unnamed parameters", goto error
);
1226 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1227 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1228 return fn(pwaff1
, pwaff2
);
1230 isl_pw_aff_free(pwaff1
);
1231 isl_pw_aff_free(pwaff2
);
1235 /* Compute a piecewise quasi-affine expression with a domain that
1236 * is the union of those of pwaff1 and pwaff2 and such that on each
1237 * cell, the quasi-affine expression is the better (according to cmp)
1238 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1239 * is defined on a given cell, then the associated expression
1240 * is the defined one.
1242 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1243 __isl_take isl_pw_aff
*pwaff2
,
1244 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1245 __isl_take isl_aff
*aff2
))
1252 if (!pwaff1
|| !pwaff2
)
1255 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1256 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1257 isl_die(ctx
, isl_error_invalid
,
1258 "arguments should live in same space", goto error
);
1260 if (isl_pw_aff_is_empty(pwaff1
)) {
1261 isl_pw_aff_free(pwaff1
);
1265 if (isl_pw_aff_is_empty(pwaff2
)) {
1266 isl_pw_aff_free(pwaff2
);
1270 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1271 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1273 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1274 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1275 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1276 struct isl_set
*common
;
1279 common
= isl_set_intersect(
1280 isl_set_copy(pwaff1
->p
[i
].set
),
1281 isl_set_copy(pwaff2
->p
[j
].set
));
1282 better
= isl_set_from_basic_set(cmp(
1283 isl_aff_copy(pwaff2
->p
[j
].aff
),
1284 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1285 better
= isl_set_intersect(common
, better
);
1286 if (isl_set_plain_is_empty(better
)) {
1287 isl_set_free(better
);
1290 set
= isl_set_subtract(set
, isl_set_copy(better
));
1292 res
= isl_pw_aff_add_piece(res
, better
,
1293 isl_aff_copy(pwaff2
->p
[j
].aff
));
1295 res
= isl_pw_aff_add_piece(res
, set
,
1296 isl_aff_copy(pwaff1
->p
[i
].aff
));
1299 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1300 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1301 for (i
= 0; i
< pwaff1
->n
; ++i
)
1302 set
= isl_set_subtract(set
,
1303 isl_set_copy(pwaff1
->p
[i
].set
));
1304 res
= isl_pw_aff_add_piece(res
, set
,
1305 isl_aff_copy(pwaff2
->p
[j
].aff
));
1308 isl_pw_aff_free(pwaff1
);
1309 isl_pw_aff_free(pwaff2
);
1313 isl_pw_aff_free(pwaff1
);
1314 isl_pw_aff_free(pwaff2
);
1318 /* Compute a piecewise quasi-affine expression with a domain that
1319 * is the union of those of pwaff1 and pwaff2 and such that on each
1320 * cell, the quasi-affine expression is the maximum of those of pwaff1
1321 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1322 * cell, then the associated expression is the defined one.
1324 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1325 __isl_take isl_pw_aff
*pwaff2
)
1327 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1330 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1331 __isl_take isl_pw_aff
*pwaff2
)
1333 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1337 /* Compute a piecewise quasi-affine expression with a domain that
1338 * is the union of those of pwaff1 and pwaff2 and such that on each
1339 * cell, the quasi-affine expression is the minimum of those of pwaff1
1340 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1341 * cell, then the associated expression is the defined one.
1343 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1344 __isl_take isl_pw_aff
*pwaff2
)
1346 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1349 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1350 __isl_take isl_pw_aff
*pwaff2
)
1352 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1356 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1357 __isl_take isl_pw_aff
*pwaff2
, int max
)
1360 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1362 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1365 /* Construct a map with as domain the domain of pwaff and
1366 * one-dimensional range corresponding to the affine expressions.
1368 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1377 dim
= isl_pw_aff_get_space(pwaff
);
1378 map
= isl_map_empty(dim
);
1380 for (i
= 0; i
< pwaff
->n
; ++i
) {
1381 isl_basic_map
*bmap
;
1384 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1385 map_i
= isl_map_from_basic_map(bmap
);
1386 map_i
= isl_map_intersect_domain(map_i
,
1387 isl_set_copy(pwaff
->p
[i
].set
));
1388 map
= isl_map_union_disjoint(map
, map_i
);
1391 isl_pw_aff_free(pwaff
);
1396 /* Construct a map with as domain the domain of pwaff and
1397 * one-dimensional range corresponding to the affine expressions.
1399 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1401 if (isl_space_is_set(pwaff
->dim
))
1402 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1403 "space of input is not a map",
1404 return isl_pw_aff_free(pwaff
));
1405 return map_from_pw_aff(pwaff
);
1408 /* Construct a one-dimensional set with as parameter domain
1409 * the domain of pwaff and the single set dimension
1410 * corresponding to the affine expressions.
1412 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1414 if (!isl_space_is_set(pwaff
->dim
))
1415 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1416 "space of input is not a set",
1417 return isl_pw_aff_free(pwaff
));
1418 return map_from_pw_aff(pwaff
);
1421 /* Return a set containing those elements in the domain
1422 * of pwaff where it is non-negative.
1424 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1432 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1434 for (i
= 0; i
< pwaff
->n
; ++i
) {
1435 isl_basic_set
*bset
;
1438 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1439 set_i
= isl_set_from_basic_set(bset
);
1440 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1441 set
= isl_set_union_disjoint(set
, set_i
);
1444 isl_pw_aff_free(pwaff
);
1449 /* Return a set containing those elements in the domain
1450 * of pwaff where it is zero.
1452 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1460 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1462 for (i
= 0; i
< pwaff
->n
; ++i
) {
1463 isl_basic_set
*bset
;
1466 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1467 set_i
= isl_set_from_basic_set(bset
);
1468 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1469 set
= isl_set_union_disjoint(set
, set_i
);
1472 isl_pw_aff_free(pwaff
);
1477 /* Return a set containing those elements in the domain
1478 * of pwaff where it is not zero.
1480 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1482 return isl_set_complement(isl_pw_aff_zero_set(pwaff
));
1485 /* Return a set containing those elements in the shared domain
1486 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1488 * We compute the difference on the shared domain and then construct
1489 * the set of values where this difference is non-negative.
1490 * If strict is set, we first subtract 1 from the difference.
1491 * If equal is set, we only return the elements where pwaff1 and pwaff2
1494 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1495 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1497 isl_set
*set1
, *set2
;
1499 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1500 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1501 set1
= isl_set_intersect(set1
, set2
);
1502 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1503 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1504 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1507 isl_space
*dim
= isl_set_get_space(set1
);
1509 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1510 aff
= isl_aff_add_constant_si(aff
, -1);
1511 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1516 return isl_pw_aff_zero_set(pwaff1
);
1517 return isl_pw_aff_nonneg_set(pwaff1
);
1520 /* Return a set containing those elements in the shared domain
1521 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1523 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1524 __isl_take isl_pw_aff
*pwaff2
)
1526 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1529 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1530 __isl_take isl_pw_aff
*pwaff2
)
1532 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1535 /* Return a set containing those elements in the shared domain
1536 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1538 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1539 __isl_take isl_pw_aff
*pwaff2
)
1541 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1544 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1545 __isl_take isl_pw_aff
*pwaff2
)
1547 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1550 /* Return a set containing those elements in the shared domain
1551 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1553 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1554 __isl_take isl_pw_aff
*pwaff2
)
1556 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1559 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1560 __isl_take isl_pw_aff
*pwaff2
)
1562 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1565 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1566 __isl_take isl_pw_aff
*pwaff2
)
1568 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1571 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1572 __isl_take isl_pw_aff
*pwaff2
)
1574 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1577 /* Return a set containing those elements in the shared domain
1578 * of the elements of list1 and list2 where each element in list1
1579 * has the relation specified by "fn" with each element in list2.
1581 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1582 __isl_take isl_pw_aff_list
*list2
,
1583 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1584 __isl_take isl_pw_aff
*pwaff2
))
1590 if (!list1
|| !list2
)
1593 ctx
= isl_pw_aff_list_get_ctx(list1
);
1594 if (list1
->n
< 1 || list2
->n
< 1)
1595 isl_die(ctx
, isl_error_invalid
,
1596 "list should contain at least one element", goto error
);
1598 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1599 for (i
= 0; i
< list1
->n
; ++i
)
1600 for (j
= 0; j
< list2
->n
; ++j
) {
1603 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1604 isl_pw_aff_copy(list2
->p
[j
]));
1605 set
= isl_set_intersect(set
, set_ij
);
1608 isl_pw_aff_list_free(list1
);
1609 isl_pw_aff_list_free(list2
);
1612 isl_pw_aff_list_free(list1
);
1613 isl_pw_aff_list_free(list2
);
1617 /* Return a set containing those elements in the shared domain
1618 * of the elements of list1 and list2 where each element in list1
1619 * is equal to each element in list2.
1621 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1622 __isl_take isl_pw_aff_list
*list2
)
1624 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1627 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1628 __isl_take isl_pw_aff_list
*list2
)
1630 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1633 /* Return a set containing those elements in the shared domain
1634 * of the elements of list1 and list2 where each element in list1
1635 * is less than or equal to each element in list2.
1637 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1638 __isl_take isl_pw_aff_list
*list2
)
1640 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1643 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1644 __isl_take isl_pw_aff_list
*list2
)
1646 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1649 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1650 __isl_take isl_pw_aff_list
*list2
)
1652 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1655 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1656 __isl_take isl_pw_aff_list
*list2
)
1658 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1662 /* Return a set containing those elements in the shared domain
1663 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1665 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1666 __isl_take isl_pw_aff
*pwaff2
)
1668 isl_set
*set_lt
, *set_gt
;
1670 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1671 isl_pw_aff_copy(pwaff2
));
1672 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1673 return isl_set_union_disjoint(set_lt
, set_gt
);
1676 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1677 __isl_take isl_pw_aff
*pwaff2
)
1679 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1682 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1687 if (isl_int_is_one(v
))
1689 if (!isl_int_is_pos(v
))
1690 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1691 "factor needs to be positive",
1692 return isl_pw_aff_free(pwaff
));
1693 pwaff
= isl_pw_aff_cow(pwaff
);
1699 for (i
= 0; i
< pwaff
->n
; ++i
) {
1700 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1701 if (!pwaff
->p
[i
].aff
)
1702 return isl_pw_aff_free(pwaff
);
1708 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1712 pwaff
= isl_pw_aff_cow(pwaff
);
1718 for (i
= 0; i
< pwaff
->n
; ++i
) {
1719 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1720 if (!pwaff
->p
[i
].aff
)
1721 return isl_pw_aff_free(pwaff
);
1727 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1731 pwaff
= isl_pw_aff_cow(pwaff
);
1737 for (i
= 0; i
< pwaff
->n
; ++i
) {
1738 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1739 if (!pwaff
->p
[i
].aff
)
1740 return isl_pw_aff_free(pwaff
);
1746 /* Return an affine expression that is equal to pwaff_true for elements
1747 * in "cond" and to pwaff_false for elements not in "cond".
1748 * That is, return cond ? pwaff_true : pwaff_false;
1750 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_set
*cond
,
1751 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1755 comp
= isl_set_complement(isl_set_copy(cond
));
1756 pwaff_true
= isl_pw_aff_intersect_domain(pwaff_true
, cond
);
1757 pwaff_false
= isl_pw_aff_intersect_domain(pwaff_false
, comp
);
1759 return isl_pw_aff_add_disjoint(pwaff_true
, pwaff_false
);
1762 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1767 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1770 /* Check whether pwaff is a piecewise constant.
1772 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1779 for (i
= 0; i
< pwaff
->n
; ++i
) {
1780 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1781 if (is_cst
< 0 || !is_cst
)
1788 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1789 __isl_take isl_aff
*aff2
)
1791 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1792 return isl_aff_mul(aff2
, aff1
);
1794 if (!isl_aff_is_cst(aff2
))
1795 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1796 "at least one affine expression should be constant",
1799 aff1
= isl_aff_cow(aff1
);
1803 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1804 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1814 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1815 __isl_take isl_pw_aff
*pwaff2
)
1820 if (!pwaff1
|| !pwaff2
)
1823 n
= pwaff1
->n
* pwaff2
->n
;
1824 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1826 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1827 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1830 common
= isl_set_intersect(
1831 isl_set_copy(pwaff1
->p
[i
].set
),
1832 isl_set_copy(pwaff2
->p
[j
].set
));
1833 if (isl_set_plain_is_empty(common
)) {
1834 isl_set_free(common
);
1838 prod
= isl_aff_mul(isl_aff_copy(pwaff1
->p
[i
].aff
),
1839 isl_aff_copy(pwaff2
->p
[j
].aff
));
1841 res
= isl_pw_aff_add_piece(res
, common
, prod
);
1845 isl_pw_aff_free(pwaff1
);
1846 isl_pw_aff_free(pwaff2
);
1849 isl_pw_aff_free(pwaff1
);
1850 isl_pw_aff_free(pwaff2
);
1854 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1855 __isl_take isl_pw_aff
*pwaff2
)
1857 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1860 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1861 __isl_take isl_pw_aff
*pwaff2
)
1865 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
1866 isl_pw_aff_copy(pwaff2
));
1867 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1870 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1871 __isl_take isl_pw_aff
*pwaff2
)
1873 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
1876 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1877 __isl_take isl_pw_aff
*pwaff2
)
1881 le
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
1882 isl_pw_aff_copy(pwaff2
));
1883 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1886 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1887 __isl_take isl_pw_aff
*pwaff2
)
1889 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
1892 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
1893 __isl_take isl_pw_aff_list
*list
,
1894 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1895 __isl_take isl_pw_aff
*pwaff2
))
1904 ctx
= isl_pw_aff_list_get_ctx(list
);
1906 isl_die(ctx
, isl_error_invalid
,
1907 "list should contain at least one element",
1908 return isl_pw_aff_list_free(list
));
1910 res
= isl_pw_aff_copy(list
->p
[0]);
1911 for (i
= 1; i
< list
->n
; ++i
)
1912 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
1914 isl_pw_aff_list_free(list
);
1918 /* Return an isl_pw_aff that maps each element in the intersection of the
1919 * domains of the elements of list to the minimal corresponding affine
1922 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
1924 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
1927 /* Return an isl_pw_aff that maps each element in the intersection of the
1928 * domains of the elements of list to the maximal corresponding affine
1931 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
1933 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
1939 #include <isl_multi_templ.c>
1941 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
1942 __isl_take isl_multi_aff
*maff2
)
1947 maff1
= isl_multi_aff_cow(maff1
);
1948 if (!maff1
|| !maff2
)
1951 ctx
= isl_multi_aff_get_ctx(maff1
);
1952 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
1953 isl_die(ctx
, isl_error_invalid
,
1954 "spaces don't match", goto error
);
1956 for (i
= 0; i
< maff1
->n
; ++i
) {
1957 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
1958 isl_aff_copy(maff2
->p
[i
]));
1963 isl_multi_aff_free(maff2
);
1966 isl_multi_aff_free(maff1
);
1967 isl_multi_aff_free(maff2
);
1971 /* Exploit the equalities in "eq" to simplify the affine expressions.
1973 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
1974 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
1978 maff
= isl_multi_aff_cow(maff
);
1982 for (i
= 0; i
< maff
->n
; ++i
) {
1983 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
1984 isl_basic_set_copy(eq
));
1989 isl_basic_set_free(eq
);
1992 isl_basic_set_free(eq
);
1993 isl_multi_aff_free(maff
);
1997 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2002 maff
= isl_multi_aff_cow(maff
);
2006 for (i
= 0; i
< maff
->n
; ++i
) {
2007 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2009 return isl_multi_aff_free(maff
);
2015 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2016 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2018 maff1
= isl_multi_aff_add(maff1
, maff2
);
2019 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2023 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2031 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2032 __isl_keep isl_multi_aff
*maff2
)
2037 if (!maff1
|| !maff2
)
2039 if (maff1
->n
!= maff2
->n
)
2041 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2042 if (equal
< 0 || !equal
)
2045 for (i
= 0; i
< maff1
->n
; ++i
) {
2046 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2047 if (equal
< 0 || !equal
)
2054 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2055 __isl_take isl_multi_aff
*maff
,
2056 enum isl_dim_type type
, unsigned pos
, const char *s
)
2060 maff
= isl_multi_aff_cow(maff
);
2064 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2066 return isl_multi_aff_free(maff
);
2067 for (i
= 0; i
< maff
->n
; ++i
) {
2068 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2070 return isl_multi_aff_free(maff
);
2076 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2077 enum isl_dim_type type
, unsigned first
, unsigned n
)
2081 maff
= isl_multi_aff_cow(maff
);
2085 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2087 return isl_multi_aff_free(maff
);
2088 for (i
= 0; i
< maff
->n
; ++i
) {
2089 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2091 return isl_multi_aff_free(maff
);
2098 #define PW isl_pw_multi_aff
2100 #define EL isl_multi_aff
2102 #define EL_IS_ZERO is_empty
2106 #define IS_ZERO is_empty
2113 #define NO_INVOLVES_DIMS
2114 #define NO_MOVE_DIMS
2115 #define NO_INSERT_DIMS
2119 #include <isl_pw_templ.c>
2121 /* Construct a map mapping the domain the piecewise multi-affine expression
2122 * to its range, with each dimension in the range equated to the
2123 * corresponding affine expression on its cell.
2125 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2133 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2135 for (i
= 0; i
< pma
->n
; ++i
) {
2136 isl_multi_aff
*maff
;
2137 isl_basic_map
*bmap
;
2140 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2141 bmap
= isl_basic_map_from_multi_aff(maff
);
2142 map_i
= isl_map_from_basic_map(bmap
);
2143 map_i
= isl_map_intersect_domain(map_i
,
2144 isl_set_copy(pma
->p
[i
].set
));
2145 map
= isl_map_union_disjoint(map
, map_i
);
2148 isl_pw_multi_aff_free(pma
);
2152 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2154 if (!isl_space_is_set(pma
->dim
))
2155 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2156 "isl_pw_multi_aff cannot be converted into an isl_set",
2157 return isl_pw_multi_aff_free(pma
));
2159 return isl_map_from_pw_multi_aff(pma
);
2162 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2163 * This obivously only works if the input "map" is single-valued.
2164 * If so, we compute the lexicographic minimum of the image in the form
2165 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2166 * to its lexicographic minimum.
2167 * If the input is not single-valued, we produce an error.
2169 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2173 isl_pw_multi_aff
*pma
;
2178 sv
= isl_map_is_single_valued(map
);
2182 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2183 "map is not single-valued", goto error
);
2184 map
= isl_map_make_disjoint(map
);
2188 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2190 for (i
= 0; i
< map
->n
; ++i
) {
2191 isl_pw_multi_aff
*pma_i
;
2192 isl_basic_map
*bmap
;
2193 bmap
= isl_basic_map_copy(map
->p
[i
]);
2194 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2195 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2205 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2207 return isl_pw_multi_aff_from_map(set
);
2210 /* Plug in "subs" for dimension "type", "pos" of "aff".
2212 * Let i be the dimension to replace and let "subs" be of the form
2216 * and "aff" of the form
2222 * floor((a f + d g')/(m d))
2224 * where g' is the result of plugging in "subs" in each of the integer
2227 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2228 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2233 aff
= isl_aff_cow(aff
);
2235 return isl_aff_free(aff
);
2237 ctx
= isl_aff_get_ctx(aff
);
2238 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2239 isl_die(ctx
, isl_error_invalid
,
2240 "spaces don't match", return isl_aff_free(aff
));
2241 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2242 isl_die(ctx
, isl_error_unsupported
,
2243 "cannot handle divs yet", return isl_aff_free(aff
));
2245 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
2247 return isl_aff_free(aff
);
2249 aff
->v
= isl_vec_cow(aff
->v
);
2251 return isl_aff_free(aff
);
2253 pos
+= isl_local_space_offset(aff
->ls
, type
);
2256 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
2257 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
2258 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
2259 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
2260 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
2266 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2267 * expressions in "maff".
2269 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
2270 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
2271 __isl_keep isl_aff
*subs
)
2275 maff
= isl_multi_aff_cow(maff
);
2277 return isl_multi_aff_free(maff
);
2279 if (type
== isl_dim_in
)
2282 for (i
= 0; i
< maff
->n
; ++i
) {
2283 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
2285 return isl_multi_aff_free(maff
);
2291 /* Plug in "subs" for dimension "type", "pos" of "pma".
2293 * pma is of the form
2297 * while subs is of the form
2299 * v' = B_j(v) -> S_j
2301 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2302 * has a contribution in the result, in particular
2304 * C_ij(S_j) -> M_i(S_j)
2306 * Note that plugging in S_j in C_ij may also result in an empty set
2307 * and this contribution should simply be discarded.
2309 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
2310 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
2311 __isl_keep isl_pw_aff
*subs
)
2314 isl_pw_multi_aff
*res
;
2317 return isl_pw_multi_aff_free(pma
);
2319 n
= pma
->n
* subs
->n
;
2320 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
2322 for (i
= 0; i
< pma
->n
; ++i
) {
2323 for (j
= 0; j
< subs
->n
; ++j
) {
2325 isl_multi_aff
*res_ij
;
2326 common
= isl_set_intersect(
2327 isl_set_copy(pma
->p
[i
].set
),
2328 isl_set_copy(subs
->p
[j
].set
));
2329 common
= isl_set_substitute(common
,
2330 type
, pos
, subs
->p
[j
].aff
);
2331 if (isl_set_plain_is_empty(common
)) {
2332 isl_set_free(common
);
2336 res_ij
= isl_multi_aff_substitute(
2337 isl_multi_aff_copy(pma
->p
[i
].maff
),
2338 type
, pos
, subs
->p
[j
].aff
);
2340 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
2344 isl_pw_multi_aff_free(pma
);