2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl_list_private.h>
23 #include <isl/constraint.h>
26 #include <isl_config.h>
28 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
29 __isl_take isl_vec
*v
)
36 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
46 isl_local_space_free(ls
);
51 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
60 ctx
= isl_local_space_get_ctx(ls
);
61 if (!isl_local_space_divs_known(ls
))
62 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
64 if (!isl_local_space_is_set(ls
))
65 isl_die(ctx
, isl_error_invalid
,
66 "domain of affine expression should be a set",
69 total
= isl_local_space_dim(ls
, isl_dim_all
);
70 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
71 return isl_aff_alloc_vec(ls
, v
);
73 isl_local_space_free(ls
);
77 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
81 aff
= isl_aff_alloc(ls
);
85 isl_int_set_si(aff
->v
->el
[0], 1);
86 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
91 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
100 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
105 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
106 isl_vec_copy(aff
->v
));
109 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
117 return isl_aff_dup(aff
);
120 void *isl_aff_free(__isl_take isl_aff
*aff
)
128 isl_local_space_free(aff
->ls
);
129 isl_vec_free(aff
->v
);
136 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
138 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
141 /* Externally, an isl_aff has a map space, but internally, the
142 * ls field corresponds to the domain of that space.
144 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
148 if (type
== isl_dim_out
)
150 if (type
== isl_dim_in
)
152 return isl_local_space_dim(aff
->ls
, type
);
155 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
157 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
160 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
165 space
= isl_local_space_get_space(aff
->ls
);
166 space
= isl_space_from_domain(space
);
167 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
171 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
172 __isl_keep isl_aff
*aff
)
174 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
177 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
182 ls
= isl_local_space_copy(aff
->ls
);
183 ls
= isl_local_space_from_domain(ls
);
184 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
188 /* Externally, an isl_aff has a map space, but internally, the
189 * ls field corresponds to the domain of that space.
191 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
192 enum isl_dim_type type
, unsigned pos
)
196 if (type
== isl_dim_out
)
198 if (type
== isl_dim_in
)
200 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
203 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
204 __isl_take isl_space
*dim
)
206 aff
= isl_aff_cow(aff
);
210 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
212 return isl_aff_free(aff
);
221 /* Reset the space of "aff". This function is called from isl_pw_templ.c
222 * and doesn't know if the space of an element object is represented
223 * directly or through its domain. It therefore passes along both.
225 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
226 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
228 isl_space_free(space
);
229 return isl_aff_reset_domain_space(aff
, domain
);
232 /* Reorder the coefficients of the affine expression based
233 * on the given reodering.
234 * The reordering r is assumed to have been extended with the local
237 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
238 __isl_take isl_reordering
*r
, int n_div
)
246 res
= isl_vec_alloc(vec
->ctx
,
247 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
248 isl_seq_cpy(res
->el
, vec
->el
, 2);
249 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
250 for (i
= 0; i
< r
->len
; ++i
)
251 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
253 isl_reordering_free(r
);
258 isl_reordering_free(r
);
262 /* Reorder the dimensions of the domain of "aff" according
263 * to the given reordering.
265 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
266 __isl_take isl_reordering
*r
)
268 aff
= isl_aff_cow(aff
);
272 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
273 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
274 aff
->ls
->div
->n_row
);
275 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
277 if (!aff
->v
|| !aff
->ls
)
278 return isl_aff_free(aff
);
283 isl_reordering_free(r
);
287 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
288 __isl_take isl_space
*model
)
293 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
294 model
, isl_dim_param
)) {
297 model
= isl_space_drop_dims(model
, isl_dim_in
,
298 0, isl_space_dim(model
, isl_dim_in
));
299 model
= isl_space_drop_dims(model
, isl_dim_out
,
300 0, isl_space_dim(model
, isl_dim_out
));
301 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
302 exp
= isl_reordering_extend_space(exp
,
303 isl_aff_get_domain_space(aff
));
304 aff
= isl_aff_realign_domain(aff
, exp
);
307 isl_space_free(model
);
310 isl_space_free(model
);
315 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
320 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
323 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
330 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
331 if (equal
< 0 || !equal
)
334 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
337 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
341 isl_int_set(*v
, aff
->v
->el
[0]);
345 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
349 isl_int_set(*v
, aff
->v
->el
[1]);
353 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
354 enum isl_dim_type type
, int pos
, isl_int
*v
)
359 if (type
== isl_dim_out
)
360 isl_die(aff
->v
->ctx
, isl_error_invalid
,
361 "output/set dimension does not have a coefficient",
363 if (type
== isl_dim_in
)
366 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
367 isl_die(aff
->v
->ctx
, isl_error_invalid
,
368 "position out of bounds", return -1);
370 pos
+= isl_local_space_offset(aff
->ls
, type
);
371 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
376 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
378 aff
= isl_aff_cow(aff
);
382 aff
->v
= isl_vec_cow(aff
->v
);
384 return isl_aff_free(aff
);
386 isl_int_set(aff
->v
->el
[0], v
);
391 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
393 aff
= isl_aff_cow(aff
);
397 aff
->v
= isl_vec_cow(aff
->v
);
399 return isl_aff_free(aff
);
401 isl_int_set(aff
->v
->el
[1], v
);
406 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
408 if (isl_int_is_zero(v
))
411 aff
= isl_aff_cow(aff
);
415 aff
->v
= isl_vec_cow(aff
->v
);
417 return isl_aff_free(aff
);
419 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
424 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
429 isl_int_set_si(t
, v
);
430 aff
= isl_aff_add_constant(aff
, t
);
436 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
438 aff
= isl_aff_cow(aff
);
442 aff
->v
= isl_vec_cow(aff
->v
);
444 return isl_aff_free(aff
);
446 isl_int_set_si(aff
->v
->el
[1], v
);
451 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
452 enum isl_dim_type type
, int pos
, isl_int v
)
457 if (type
== isl_dim_out
)
458 isl_die(aff
->v
->ctx
, isl_error_invalid
,
459 "output/set dimension does not have a coefficient",
460 return isl_aff_free(aff
));
461 if (type
== isl_dim_in
)
464 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
465 isl_die(aff
->v
->ctx
, isl_error_invalid
,
466 "position out of bounds", return isl_aff_free(aff
));
468 aff
= isl_aff_cow(aff
);
472 aff
->v
= isl_vec_cow(aff
->v
);
474 return isl_aff_free(aff
);
476 pos
+= isl_local_space_offset(aff
->ls
, type
);
477 isl_int_set(aff
->v
->el
[1 + pos
], v
);
482 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
483 enum isl_dim_type type
, int pos
, int v
)
488 if (type
== isl_dim_out
)
489 isl_die(aff
->v
->ctx
, isl_error_invalid
,
490 "output/set dimension does not have a coefficient",
491 return isl_aff_free(aff
));
492 if (type
== isl_dim_in
)
495 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
496 isl_die(aff
->v
->ctx
, isl_error_invalid
,
497 "position out of bounds", return isl_aff_free(aff
));
499 aff
= isl_aff_cow(aff
);
503 aff
->v
= isl_vec_cow(aff
->v
);
505 return isl_aff_free(aff
);
507 pos
+= isl_local_space_offset(aff
->ls
, type
);
508 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
513 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
514 enum isl_dim_type type
, int pos
, isl_int v
)
519 if (type
== isl_dim_out
)
520 isl_die(aff
->v
->ctx
, isl_error_invalid
,
521 "output/set dimension does not have a coefficient",
522 return isl_aff_free(aff
));
523 if (type
== isl_dim_in
)
526 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
527 isl_die(aff
->v
->ctx
, isl_error_invalid
,
528 "position out of bounds", return isl_aff_free(aff
));
530 aff
= isl_aff_cow(aff
);
534 aff
->v
= isl_vec_cow(aff
->v
);
536 return isl_aff_free(aff
);
538 pos
+= isl_local_space_offset(aff
->ls
, type
);
539 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
544 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
545 enum isl_dim_type type
, int pos
, int v
)
550 isl_int_set_si(t
, v
);
551 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
557 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
562 return isl_local_space_get_div(aff
->ls
, pos
);
565 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
567 aff
= isl_aff_cow(aff
);
570 aff
->v
= isl_vec_cow(aff
->v
);
572 return isl_aff_free(aff
);
574 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
579 /* Remove divs from the local space that do not appear in the affine
581 * We currently only remove divs at the end.
582 * Some intermediate divs may also not appear directly in the affine
583 * expression, but we would also need to check that no other divs are
584 * defined in terms of them.
586 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
595 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
596 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
598 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
602 aff
= isl_aff_cow(aff
);
606 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
607 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
608 if (!aff
->ls
|| !aff
->v
)
609 return isl_aff_free(aff
);
614 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
618 aff
->v
= isl_vec_normalize(aff
->v
);
620 return isl_aff_free(aff
);
621 aff
= isl_aff_remove_unused_divs(aff
);
625 /* Given f, return floor(f).
626 * If f is an integer expression, then just return f.
627 * Otherwise, if f = g/m, write g = q m + r,
628 * create a new div d = [r/m] and return the expression q + d.
629 * The coefficients in r are taken to lie between -m/2 and m/2.
631 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
641 if (isl_int_is_one(aff
->v
->el
[0]))
644 aff
= isl_aff_cow(aff
);
648 aff
->v
= isl_vec_cow(aff
->v
);
649 div
= isl_vec_copy(aff
->v
);
650 div
= isl_vec_cow(div
);
652 return isl_aff_free(aff
);
654 ctx
= isl_aff_get_ctx(aff
);
655 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
656 for (i
= 1; i
< aff
->v
->size
; ++i
) {
657 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
658 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
659 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
660 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
661 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
665 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
667 return isl_aff_free(aff
);
670 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
672 return isl_aff_free(aff
);
673 isl_int_set_si(aff
->v
->el
[0], 1);
674 isl_int_set_si(aff
->v
->el
[size
], 1);
681 * aff mod m = aff - m * floor(aff/m)
683 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
687 res
= isl_aff_copy(aff
);
688 aff
= isl_aff_scale_down(aff
, m
);
689 aff
= isl_aff_floor(aff
);
690 aff
= isl_aff_scale(aff
, m
);
691 res
= isl_aff_sub(res
, aff
);
698 * pwaff mod m = pwaff - m * floor(pwaff/m)
700 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
704 res
= isl_pw_aff_copy(pwaff
);
705 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
706 pwaff
= isl_pw_aff_floor(pwaff
);
707 pwaff
= isl_pw_aff_scale(pwaff
, m
);
708 res
= isl_pw_aff_sub(res
, pwaff
);
713 /* Given f, return ceil(f).
714 * If f is an integer expression, then just return f.
715 * Otherwise, create a new div d = [-f] and return the expression -d.
717 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
722 if (isl_int_is_one(aff
->v
->el
[0]))
725 aff
= isl_aff_neg(aff
);
726 aff
= isl_aff_floor(aff
);
727 aff
= isl_aff_neg(aff
);
732 /* Apply the expansion computed by isl_merge_divs.
733 * The expansion itself is given by "exp" while the resulting
734 * list of divs is given by "div".
736 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
737 __isl_take isl_mat
*div
, int *exp
)
744 aff
= isl_aff_cow(aff
);
748 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
749 new_n_div
= isl_mat_rows(div
);
750 if (new_n_div
< old_n_div
)
751 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
752 "not an expansion", goto error
);
754 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
758 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
760 for (i
= new_n_div
- 1; i
>= 0; --i
) {
761 if (j
>= 0 && exp
[j
] == i
) {
763 isl_int_swap(aff
->v
->el
[offset
+ i
],
764 aff
->v
->el
[offset
+ j
]);
767 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
770 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
781 /* Add two affine expressions that live in the same local space.
783 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
784 __isl_take isl_aff
*aff2
)
788 aff1
= isl_aff_cow(aff1
);
792 aff1
->v
= isl_vec_cow(aff1
->v
);
798 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
799 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
800 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
801 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
802 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
803 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
804 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
816 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
817 __isl_take isl_aff
*aff2
)
827 ctx
= isl_aff_get_ctx(aff1
);
828 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
829 isl_die(ctx
, isl_error_invalid
,
830 "spaces don't match", goto error
);
832 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
833 return add_expanded(aff1
, aff2
);
835 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
836 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
840 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
841 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
842 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
846 return add_expanded(aff1
, aff2
);
855 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
856 __isl_take isl_aff
*aff2
)
858 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
861 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
865 if (isl_int_is_one(f
))
868 aff
= isl_aff_cow(aff
);
871 aff
->v
= isl_vec_cow(aff
->v
);
873 return isl_aff_free(aff
);
876 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
877 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
878 isl_int_divexact(gcd
, f
, gcd
);
879 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
885 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
889 if (isl_int_is_one(f
))
892 aff
= isl_aff_cow(aff
);
895 aff
->v
= isl_vec_cow(aff
->v
);
897 return isl_aff_free(aff
);
900 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
901 isl_int_gcd(gcd
, gcd
, f
);
902 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
903 isl_int_divexact(gcd
, f
, gcd
);
904 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
910 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
918 isl_int_set_ui(v
, f
);
919 aff
= isl_aff_scale_down(aff
, v
);
925 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
926 enum isl_dim_type type
, unsigned pos
, const char *s
)
928 aff
= isl_aff_cow(aff
);
931 if (type
== isl_dim_out
)
932 isl_die(aff
->v
->ctx
, isl_error_invalid
,
933 "cannot set name of output/set dimension",
934 return isl_aff_free(aff
));
935 if (type
== isl_dim_in
)
937 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
939 return isl_aff_free(aff
);
944 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
945 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
947 aff
= isl_aff_cow(aff
);
949 return isl_id_free(id
);
950 if (type
== isl_dim_out
)
951 isl_die(aff
->v
->ctx
, isl_error_invalid
,
952 "cannot set name of output/set dimension",
954 if (type
== isl_dim_in
)
956 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
958 return isl_aff_free(aff
);
967 /* Exploit the equalities in "eq" to simplify the affine expression
968 * and the expressions of the integer divisions in the local space.
969 * The integer divisions in this local space are assumed to appear
970 * as regular dimensions in "eq".
972 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
973 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
982 isl_basic_set_free(eq
);
986 aff
= isl_aff_cow(aff
);
990 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
991 isl_basic_set_copy(eq
));
995 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
997 for (i
= 0; i
< eq
->n_eq
; ++i
) {
998 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
999 if (j
< 0 || j
== 0 || j
>= total
)
1002 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1006 isl_basic_set_free(eq
);
1007 aff
= isl_aff_normalize(aff
);
1010 isl_basic_set_free(eq
);
1015 /* Exploit the equalities in "eq" to simplify the affine expression
1016 * and the expressions of the integer divisions in the local space.
1018 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1019 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1025 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1027 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
1028 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1030 isl_basic_set_free(eq
);
1035 /* Look for equalities among the variables shared by context and aff
1036 * and the integer divisions of aff, if any.
1037 * The equalities are then used to eliminate coefficients and/or integer
1038 * divisions from aff.
1040 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1041 __isl_take isl_set
*context
)
1043 isl_basic_set
*hull
;
1048 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1050 isl_basic_set
*bset
;
1051 isl_local_space
*ls
;
1052 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1053 ls
= isl_aff_get_domain_local_space(aff
);
1054 bset
= isl_basic_set_from_local_space(ls
);
1055 bset
= isl_basic_set_lift(bset
);
1056 bset
= isl_basic_set_flatten(bset
);
1057 context
= isl_set_intersect(context
,
1058 isl_set_from_basic_set(bset
));
1061 hull
= isl_set_affine_hull(context
);
1062 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1065 isl_set_free(context
);
1069 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1070 __isl_take isl_set
*context
)
1072 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1073 dom_context
= isl_set_intersect_params(dom_context
, context
);
1074 return isl_aff_gist(aff
, dom_context
);
1077 /* Return a basic set containing those elements in the space
1078 * of aff where it is non-negative.
1080 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1082 isl_constraint
*ineq
;
1083 isl_basic_set
*bset
;
1085 ineq
= isl_inequality_from_aff(aff
);
1087 bset
= isl_basic_set_from_constraint(ineq
);
1088 bset
= isl_basic_set_simplify(bset
);
1092 /* Return a basic set containing those elements in the space
1093 * of aff where it is zero.
1095 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1097 isl_constraint
*ineq
;
1098 isl_basic_set
*bset
;
1100 ineq
= isl_equality_from_aff(aff
);
1102 bset
= isl_basic_set_from_constraint(ineq
);
1103 bset
= isl_basic_set_simplify(bset
);
1107 /* Return a basic set containing those elements in the shared space
1108 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1110 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1111 __isl_take isl_aff
*aff2
)
1113 aff1
= isl_aff_sub(aff1
, aff2
);
1115 return isl_aff_nonneg_basic_set(aff1
);
1118 /* Return a basic set containing those elements in the shared space
1119 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1121 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1122 __isl_take isl_aff
*aff2
)
1124 return isl_aff_ge_basic_set(aff2
, aff1
);
1127 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1128 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1130 aff1
= isl_aff_add(aff1
, aff2
);
1131 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1135 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1143 /* Check whether the given affine expression has non-zero coefficient
1144 * for any dimension in the given range or if any of these dimensions
1145 * appear with non-zero coefficients in any of the integer divisions
1146 * involved in the affine expression.
1148 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1149 enum isl_dim_type type
, unsigned first
, unsigned n
)
1161 ctx
= isl_aff_get_ctx(aff
);
1162 if (first
+ n
> isl_aff_dim(aff
, type
))
1163 isl_die(ctx
, isl_error_invalid
,
1164 "range out of bounds", return -1);
1166 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1170 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1171 for (i
= 0; i
< n
; ++i
)
1172 if (active
[first
+ i
]) {
1185 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1186 enum isl_dim_type type
, unsigned first
, unsigned n
)
1192 if (type
== isl_dim_out
)
1193 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1194 "cannot drop output/set dimension",
1195 return isl_aff_free(aff
));
1196 if (type
== isl_dim_in
)
1198 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1201 ctx
= isl_aff_get_ctx(aff
);
1202 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1203 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1204 return isl_aff_free(aff
));
1206 aff
= isl_aff_cow(aff
);
1210 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1212 return isl_aff_free(aff
);
1214 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1215 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1217 return isl_aff_free(aff
);
1222 /* Project the domain of the affine expression onto its parameter space.
1223 * The affine expression may not involve any of the domain dimensions.
1225 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
1231 n
= isl_aff_dim(aff
, isl_dim_in
);
1232 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
1234 return isl_aff_free(aff
);
1236 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1237 "affine expression involves some of the domain dimensions",
1238 return isl_aff_free(aff
));
1239 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
1240 space
= isl_aff_get_domain_space(aff
);
1241 space
= isl_space_params(space
);
1242 aff
= isl_aff_reset_domain_space(aff
, space
);
1246 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1247 enum isl_dim_type type
, unsigned first
, unsigned n
)
1253 if (type
== isl_dim_out
)
1254 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1255 "cannot insert output/set dimensions",
1256 return isl_aff_free(aff
));
1257 if (type
== isl_dim_in
)
1259 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1262 ctx
= isl_aff_get_ctx(aff
);
1263 if (first
> isl_local_space_dim(aff
->ls
, type
))
1264 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1265 return isl_aff_free(aff
));
1267 aff
= isl_aff_cow(aff
);
1271 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1273 return isl_aff_free(aff
);
1275 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1276 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1278 return isl_aff_free(aff
);
1283 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1284 enum isl_dim_type type
, unsigned n
)
1288 pos
= isl_aff_dim(aff
, type
);
1290 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1293 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1294 enum isl_dim_type type
, unsigned n
)
1298 pos
= isl_pw_aff_dim(pwaff
, type
);
1300 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1303 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1305 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1306 return isl_pw_aff_alloc(dom
, aff
);
1310 #define PW isl_pw_aff
1314 #define EL_IS_ZERO is_empty
1318 #define IS_ZERO is_empty
1321 #undef DEFAULT_IS_ZERO
1322 #define DEFAULT_IS_ZERO 0
1326 #define NO_MOVE_DIMS
1330 #include <isl_pw_templ.c>
1332 static __isl_give isl_set
*align_params_pw_pw_set_and(
1333 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1334 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1335 __isl_take isl_pw_aff
*pwaff2
))
1337 if (!pwaff1
|| !pwaff2
)
1339 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1340 pwaff2
->dim
, isl_dim_param
))
1341 return fn(pwaff1
, pwaff2
);
1342 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1343 !isl_space_has_named_params(pwaff2
->dim
))
1344 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1345 "unaligned unnamed parameters", goto error
);
1346 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1347 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1348 return fn(pwaff1
, pwaff2
);
1350 isl_pw_aff_free(pwaff1
);
1351 isl_pw_aff_free(pwaff2
);
1355 /* Compute a piecewise quasi-affine expression with a domain that
1356 * is the union of those of pwaff1 and pwaff2 and such that on each
1357 * cell, the quasi-affine expression is the better (according to cmp)
1358 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1359 * is defined on a given cell, then the associated expression
1360 * is the defined one.
1362 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1363 __isl_take isl_pw_aff
*pwaff2
,
1364 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1365 __isl_take isl_aff
*aff2
))
1372 if (!pwaff1
|| !pwaff2
)
1375 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1376 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1377 isl_die(ctx
, isl_error_invalid
,
1378 "arguments should live in same space", goto error
);
1380 if (isl_pw_aff_is_empty(pwaff1
)) {
1381 isl_pw_aff_free(pwaff1
);
1385 if (isl_pw_aff_is_empty(pwaff2
)) {
1386 isl_pw_aff_free(pwaff2
);
1390 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1391 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1393 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1394 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1395 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1396 struct isl_set
*common
;
1399 common
= isl_set_intersect(
1400 isl_set_copy(pwaff1
->p
[i
].set
),
1401 isl_set_copy(pwaff2
->p
[j
].set
));
1402 better
= isl_set_from_basic_set(cmp(
1403 isl_aff_copy(pwaff2
->p
[j
].aff
),
1404 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1405 better
= isl_set_intersect(common
, better
);
1406 if (isl_set_plain_is_empty(better
)) {
1407 isl_set_free(better
);
1410 set
= isl_set_subtract(set
, isl_set_copy(better
));
1412 res
= isl_pw_aff_add_piece(res
, better
,
1413 isl_aff_copy(pwaff2
->p
[j
].aff
));
1415 res
= isl_pw_aff_add_piece(res
, set
,
1416 isl_aff_copy(pwaff1
->p
[i
].aff
));
1419 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1420 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1421 for (i
= 0; i
< pwaff1
->n
; ++i
)
1422 set
= isl_set_subtract(set
,
1423 isl_set_copy(pwaff1
->p
[i
].set
));
1424 res
= isl_pw_aff_add_piece(res
, set
,
1425 isl_aff_copy(pwaff2
->p
[j
].aff
));
1428 isl_pw_aff_free(pwaff1
);
1429 isl_pw_aff_free(pwaff2
);
1433 isl_pw_aff_free(pwaff1
);
1434 isl_pw_aff_free(pwaff2
);
1438 /* Compute a piecewise quasi-affine expression with a domain that
1439 * is the union of those of pwaff1 and pwaff2 and such that on each
1440 * cell, the quasi-affine expression is the maximum of those of pwaff1
1441 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1442 * cell, then the associated expression is the defined one.
1444 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1445 __isl_take isl_pw_aff
*pwaff2
)
1447 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1450 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1451 __isl_take isl_pw_aff
*pwaff2
)
1453 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1457 /* Compute a piecewise quasi-affine expression with a domain that
1458 * is the union of those of pwaff1 and pwaff2 and such that on each
1459 * cell, the quasi-affine expression is the minimum of those of pwaff1
1460 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1461 * cell, then the associated expression is the defined one.
1463 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1464 __isl_take isl_pw_aff
*pwaff2
)
1466 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1469 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1470 __isl_take isl_pw_aff
*pwaff2
)
1472 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1476 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1477 __isl_take isl_pw_aff
*pwaff2
, int max
)
1480 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1482 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1485 /* Construct a map with as domain the domain of pwaff and
1486 * one-dimensional range corresponding to the affine expressions.
1488 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1497 dim
= isl_pw_aff_get_space(pwaff
);
1498 map
= isl_map_empty(dim
);
1500 for (i
= 0; i
< pwaff
->n
; ++i
) {
1501 isl_basic_map
*bmap
;
1504 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1505 map_i
= isl_map_from_basic_map(bmap
);
1506 map_i
= isl_map_intersect_domain(map_i
,
1507 isl_set_copy(pwaff
->p
[i
].set
));
1508 map
= isl_map_union_disjoint(map
, map_i
);
1511 isl_pw_aff_free(pwaff
);
1516 /* Construct a map with as domain the domain of pwaff and
1517 * one-dimensional range corresponding to the affine expressions.
1519 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1523 if (isl_space_is_set(pwaff
->dim
))
1524 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1525 "space of input is not a map",
1526 return isl_pw_aff_free(pwaff
));
1527 return map_from_pw_aff(pwaff
);
1530 /* Construct a one-dimensional set with as parameter domain
1531 * the domain of pwaff and the single set dimension
1532 * corresponding to the affine expressions.
1534 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1538 if (!isl_space_is_set(pwaff
->dim
))
1539 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1540 "space of input is not a set",
1541 return isl_pw_aff_free(pwaff
));
1542 return map_from_pw_aff(pwaff
);
1545 /* Return a set containing those elements in the domain
1546 * of pwaff where it is non-negative.
1548 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1556 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1558 for (i
= 0; i
< pwaff
->n
; ++i
) {
1559 isl_basic_set
*bset
;
1562 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1563 set_i
= isl_set_from_basic_set(bset
);
1564 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1565 set
= isl_set_union_disjoint(set
, set_i
);
1568 isl_pw_aff_free(pwaff
);
1573 /* Return a set containing those elements in the domain
1574 * of pwaff where it is zero (if complement is 0) or not zero
1575 * (if complement is 1).
1577 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
1586 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1588 for (i
= 0; i
< pwaff
->n
; ++i
) {
1589 isl_basic_set
*bset
;
1590 isl_set
*set_i
, *zero
;
1592 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1593 zero
= isl_set_from_basic_set(bset
);
1594 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
1596 set_i
= isl_set_subtract(set_i
, zero
);
1598 set_i
= isl_set_intersect(set_i
, zero
);
1599 set
= isl_set_union_disjoint(set
, set_i
);
1602 isl_pw_aff_free(pwaff
);
1607 /* Return a set containing those elements in the domain
1608 * of pwaff where it is zero.
1610 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1612 return pw_aff_zero_set(pwaff
, 0);
1615 /* Return a set containing those elements in the domain
1616 * of pwaff where it is not zero.
1618 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1620 return pw_aff_zero_set(pwaff
, 1);
1623 /* Return a set containing those elements in the shared domain
1624 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1626 * We compute the difference on the shared domain and then construct
1627 * the set of values where this difference is non-negative.
1628 * If strict is set, we first subtract 1 from the difference.
1629 * If equal is set, we only return the elements where pwaff1 and pwaff2
1632 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1633 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1635 isl_set
*set1
, *set2
;
1637 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1638 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1639 set1
= isl_set_intersect(set1
, set2
);
1640 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1641 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1642 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1645 isl_space
*dim
= isl_set_get_space(set1
);
1647 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1648 aff
= isl_aff_add_constant_si(aff
, -1);
1649 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1654 return isl_pw_aff_zero_set(pwaff1
);
1655 return isl_pw_aff_nonneg_set(pwaff1
);
1658 /* Return a set containing those elements in the shared domain
1659 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1661 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1662 __isl_take isl_pw_aff
*pwaff2
)
1664 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1667 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1668 __isl_take isl_pw_aff
*pwaff2
)
1670 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1673 /* Return a set containing those elements in the shared domain
1674 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1676 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1677 __isl_take isl_pw_aff
*pwaff2
)
1679 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1682 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1683 __isl_take isl_pw_aff
*pwaff2
)
1685 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1688 /* Return a set containing those elements in the shared domain
1689 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1691 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1692 __isl_take isl_pw_aff
*pwaff2
)
1694 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1697 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1698 __isl_take isl_pw_aff
*pwaff2
)
1700 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1703 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1704 __isl_take isl_pw_aff
*pwaff2
)
1706 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1709 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1710 __isl_take isl_pw_aff
*pwaff2
)
1712 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1715 /* Return a set containing those elements in the shared domain
1716 * of the elements of list1 and list2 where each element in list1
1717 * has the relation specified by "fn" with each element in list2.
1719 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1720 __isl_take isl_pw_aff_list
*list2
,
1721 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1722 __isl_take isl_pw_aff
*pwaff2
))
1728 if (!list1
|| !list2
)
1731 ctx
= isl_pw_aff_list_get_ctx(list1
);
1732 if (list1
->n
< 1 || list2
->n
< 1)
1733 isl_die(ctx
, isl_error_invalid
,
1734 "list should contain at least one element", goto error
);
1736 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1737 for (i
= 0; i
< list1
->n
; ++i
)
1738 for (j
= 0; j
< list2
->n
; ++j
) {
1741 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1742 isl_pw_aff_copy(list2
->p
[j
]));
1743 set
= isl_set_intersect(set
, set_ij
);
1746 isl_pw_aff_list_free(list1
);
1747 isl_pw_aff_list_free(list2
);
1750 isl_pw_aff_list_free(list1
);
1751 isl_pw_aff_list_free(list2
);
1755 /* Return a set containing those elements in the shared domain
1756 * of the elements of list1 and list2 where each element in list1
1757 * is equal to each element in list2.
1759 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1760 __isl_take isl_pw_aff_list
*list2
)
1762 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1765 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1766 __isl_take isl_pw_aff_list
*list2
)
1768 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1771 /* Return a set containing those elements in the shared domain
1772 * of the elements of list1 and list2 where each element in list1
1773 * is less than or equal to each element in list2.
1775 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1776 __isl_take isl_pw_aff_list
*list2
)
1778 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1781 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1782 __isl_take isl_pw_aff_list
*list2
)
1784 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1787 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1788 __isl_take isl_pw_aff_list
*list2
)
1790 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1793 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1794 __isl_take isl_pw_aff_list
*list2
)
1796 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1800 /* Return a set containing those elements in the shared domain
1801 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1803 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1804 __isl_take isl_pw_aff
*pwaff2
)
1806 isl_set
*set_lt
, *set_gt
;
1808 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1809 isl_pw_aff_copy(pwaff2
));
1810 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1811 return isl_set_union_disjoint(set_lt
, set_gt
);
1814 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1815 __isl_take isl_pw_aff
*pwaff2
)
1817 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1820 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1825 if (isl_int_is_one(v
))
1827 if (!isl_int_is_pos(v
))
1828 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1829 "factor needs to be positive",
1830 return isl_pw_aff_free(pwaff
));
1831 pwaff
= isl_pw_aff_cow(pwaff
);
1837 for (i
= 0; i
< pwaff
->n
; ++i
) {
1838 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1839 if (!pwaff
->p
[i
].aff
)
1840 return isl_pw_aff_free(pwaff
);
1846 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1850 pwaff
= isl_pw_aff_cow(pwaff
);
1856 for (i
= 0; i
< pwaff
->n
; ++i
) {
1857 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1858 if (!pwaff
->p
[i
].aff
)
1859 return isl_pw_aff_free(pwaff
);
1865 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1869 pwaff
= isl_pw_aff_cow(pwaff
);
1875 for (i
= 0; i
< pwaff
->n
; ++i
) {
1876 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1877 if (!pwaff
->p
[i
].aff
)
1878 return isl_pw_aff_free(pwaff
);
1884 /* Assuming that "cond1" and "cond2" are disjoint,
1885 * return an affine expression that is equal to pwaff1 on cond1
1886 * and to pwaff2 on cond2.
1888 static __isl_give isl_pw_aff
*isl_pw_aff_select(
1889 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
1890 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
1892 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
1893 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
1895 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
1898 /* Return an affine expression that is equal to pwaff_true for elements
1899 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1901 * That is, return cond ? pwaff_true : pwaff_false;
1903 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
1904 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1906 isl_set
*cond_true
, *cond_false
;
1908 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1909 cond_false
= isl_pw_aff_zero_set(cond
);
1910 return isl_pw_aff_select(cond_true
, pwaff_true
,
1911 cond_false
, pwaff_false
);
1914 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1919 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1922 /* Check whether pwaff is a piecewise constant.
1924 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1931 for (i
= 0; i
< pwaff
->n
; ++i
) {
1932 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1933 if (is_cst
< 0 || !is_cst
)
1940 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1941 __isl_take isl_aff
*aff2
)
1943 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1944 return isl_aff_mul(aff2
, aff1
);
1946 if (!isl_aff_is_cst(aff2
))
1947 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1948 "at least one affine expression should be constant",
1951 aff1
= isl_aff_cow(aff1
);
1955 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1956 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1966 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1967 __isl_take isl_pw_aff
*pwaff2
)
1969 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
1972 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1973 __isl_take isl_pw_aff
*pwaff2
)
1975 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
1978 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
1979 __isl_take isl_pw_aff
*pwaff2
)
1981 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
1984 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1985 __isl_take isl_pw_aff
*pwaff2
)
1987 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
1990 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1991 __isl_take isl_pw_aff
*pwaff2
)
1993 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1996 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1997 __isl_take isl_pw_aff
*pwaff2
)
2002 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2003 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2004 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
2005 isl_pw_aff_copy(pwaff2
));
2006 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
2007 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
2010 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2011 __isl_take isl_pw_aff
*pwaff2
)
2013 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
2016 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2017 __isl_take isl_pw_aff
*pwaff2
)
2022 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2023 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2024 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2025 isl_pw_aff_copy(pwaff2
));
2026 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
2027 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
2030 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2031 __isl_take isl_pw_aff
*pwaff2
)
2033 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2036 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2037 __isl_take isl_pw_aff_list
*list
,
2038 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2039 __isl_take isl_pw_aff
*pwaff2
))
2048 ctx
= isl_pw_aff_list_get_ctx(list
);
2050 isl_die(ctx
, isl_error_invalid
,
2051 "list should contain at least one element",
2052 return isl_pw_aff_list_free(list
));
2054 res
= isl_pw_aff_copy(list
->p
[0]);
2055 for (i
= 1; i
< list
->n
; ++i
)
2056 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2058 isl_pw_aff_list_free(list
);
2062 /* Return an isl_pw_aff that maps each element in the intersection of the
2063 * domains of the elements of list to the minimal corresponding affine
2066 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2068 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2071 /* Return an isl_pw_aff that maps each element in the intersection of the
2072 * domains of the elements of list to the maximal corresponding affine
2075 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2077 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2083 #include <isl_multi_templ.c>
2085 /* Construct an isl_multi_aff in the given space with value zero in
2086 * each of the output dimensions.
2088 __isl_give isl_multi_aff
*isl_multi_aff_zero(__isl_take isl_space
*space
)
2096 n
= isl_space_dim(space
, isl_dim_out
);
2097 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2100 isl_space_free(space
);
2103 isl_local_space
*ls
;
2106 space
= isl_space_domain(space
);
2107 ls
= isl_local_space_from_space(space
);
2108 aff
= isl_aff_zero_on_domain(ls
);
2110 for (i
= 0; i
< n
; ++i
)
2111 ma
= isl_multi_aff_set_aff(ma
, i
, isl_aff_copy(aff
));
2119 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2122 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
2123 __isl_take isl_multi_aff
*ma
)
2125 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
2126 return isl_pw_multi_aff_alloc(dom
, ma
);
2129 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2130 __isl_take isl_multi_aff
*maff2
)
2135 maff1
= isl_multi_aff_cow(maff1
);
2136 if (!maff1
|| !maff2
)
2139 ctx
= isl_multi_aff_get_ctx(maff1
);
2140 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2141 isl_die(ctx
, isl_error_invalid
,
2142 "spaces don't match", goto error
);
2144 for (i
= 0; i
< maff1
->n
; ++i
) {
2145 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2146 isl_aff_copy(maff2
->p
[i
]));
2151 isl_multi_aff_free(maff2
);
2154 isl_multi_aff_free(maff1
);
2155 isl_multi_aff_free(maff2
);
2159 /* Exploit the equalities in "eq" to simplify the affine expressions.
2161 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2162 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2166 maff
= isl_multi_aff_cow(maff
);
2170 for (i
= 0; i
< maff
->n
; ++i
) {
2171 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2172 isl_basic_set_copy(eq
));
2177 isl_basic_set_free(eq
);
2180 isl_basic_set_free(eq
);
2181 isl_multi_aff_free(maff
);
2185 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2190 maff
= isl_multi_aff_cow(maff
);
2194 for (i
= 0; i
< maff
->n
; ++i
) {
2195 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2197 return isl_multi_aff_free(maff
);
2203 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2204 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2206 maff1
= isl_multi_aff_add(maff1
, maff2
);
2207 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2211 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2219 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2220 __isl_keep isl_multi_aff
*maff2
)
2225 if (!maff1
|| !maff2
)
2227 if (maff1
->n
!= maff2
->n
)
2229 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2230 if (equal
< 0 || !equal
)
2233 for (i
= 0; i
< maff1
->n
; ++i
) {
2234 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2235 if (equal
< 0 || !equal
)
2242 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2243 __isl_take isl_multi_aff
*maff
,
2244 enum isl_dim_type type
, unsigned pos
, const char *s
)
2248 maff
= isl_multi_aff_cow(maff
);
2252 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2254 return isl_multi_aff_free(maff
);
2255 for (i
= 0; i
< maff
->n
; ++i
) {
2256 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2258 return isl_multi_aff_free(maff
);
2264 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2265 enum isl_dim_type type
, unsigned first
, unsigned n
)
2269 maff
= isl_multi_aff_cow(maff
);
2273 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2275 return isl_multi_aff_free(maff
);
2277 if (type
== isl_dim_out
) {
2278 for (i
= 0; i
< n
; ++i
)
2279 isl_aff_free(maff
->p
[first
+ i
]);
2280 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2281 maff
->p
[i
] = maff
->p
[i
+ n
];
2286 for (i
= 0; i
< maff
->n
; ++i
) {
2287 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2289 return isl_multi_aff_free(maff
);
2296 #define PW isl_pw_multi_aff
2298 #define EL isl_multi_aff
2300 #define EL_IS_ZERO is_empty
2304 #define IS_ZERO is_empty
2307 #undef DEFAULT_IS_ZERO
2308 #define DEFAULT_IS_ZERO 0
2313 #define NO_INVOLVES_DIMS
2314 #define NO_MOVE_DIMS
2315 #define NO_INSERT_DIMS
2319 #include <isl_pw_templ.c>
2322 #define UNION isl_union_pw_multi_aff
2324 #define PART isl_pw_multi_aff
2326 #define PARTS pw_multi_aff
2327 #define ALIGN_DOMAIN
2331 #include <isl_union_templ.c>
2333 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2334 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2336 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2337 &isl_multi_aff_add
);
2340 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2341 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2343 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2347 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2348 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2350 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2353 /* Construct a map mapping the domain the piecewise multi-affine expression
2354 * to its range, with each dimension in the range equated to the
2355 * corresponding affine expression on its cell.
2357 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2365 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2367 for (i
= 0; i
< pma
->n
; ++i
) {
2368 isl_multi_aff
*maff
;
2369 isl_basic_map
*bmap
;
2372 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2373 bmap
= isl_basic_map_from_multi_aff(maff
);
2374 map_i
= isl_map_from_basic_map(bmap
);
2375 map_i
= isl_map_intersect_domain(map_i
,
2376 isl_set_copy(pma
->p
[i
].set
));
2377 map
= isl_map_union_disjoint(map
, map_i
);
2380 isl_pw_multi_aff_free(pma
);
2384 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2386 if (!isl_space_is_set(pma
->dim
))
2387 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2388 "isl_pw_multi_aff cannot be converted into an isl_set",
2389 return isl_pw_multi_aff_free(pma
));
2391 return isl_map_from_pw_multi_aff(pma
);
2394 /* Given a basic map with a single output dimension that is defined
2395 * in terms of the parameters and input dimensions using an equality,
2396 * extract an isl_aff that expresses the output dimension in terms
2397 * of the parameters and input dimensions.
2399 * Since some applications expect the result of isl_pw_multi_aff_from_map
2400 * to only contain integer affine expressions, we compute the floor
2401 * of the expression before returning.
2403 * This function shares some similarities with
2404 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2406 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
2407 __isl_take isl_basic_map
*bmap
)
2412 isl_local_space
*ls
;
2417 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
2418 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2419 "basic map should have a single output dimension",
2421 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
2422 total
= isl_basic_map_total_dim(bmap
);
2423 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2424 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
2426 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
2427 1 + total
- (offset
+ 1)) != -1)
2431 if (i
>= bmap
->n_eq
)
2432 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2433 "unable to find suitable equality", goto error
);
2434 ls
= isl_basic_map_get_local_space(bmap
);
2435 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
2438 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
2439 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2441 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2442 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
2443 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
2444 isl_basic_map_free(bmap
);
2446 aff
= isl_aff_remove_unused_divs(aff
);
2447 aff
= isl_aff_floor(aff
);
2450 isl_basic_map_free(bmap
);
2454 /* Given a basic map where each output dimension is defined
2455 * in terms of the parameters and input dimensions using an equality,
2456 * extract an isl_multi_aff that expresses the output dimensions in terms
2457 * of the parameters and input dimensions.
2459 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
2460 __isl_take isl_basic_map
*bmap
)
2469 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
2470 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2472 for (i
= 0; i
< n_out
; ++i
) {
2473 isl_basic_map
*bmap_i
;
2476 bmap_i
= isl_basic_map_copy(bmap
);
2477 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
2478 i
+ 1, n_out
- (1 + i
));
2479 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
2480 aff
= extract_isl_aff_from_basic_map(bmap_i
);
2481 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2484 isl_basic_map_free(bmap
);
2489 /* Create an isl_pw_multi_aff that is equivalent to
2490 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2491 * The given basic map is such that each output dimension is defined
2492 * in terms of the parameters and input dimensions using an equality.
2494 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
2495 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
2499 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
2500 return isl_pw_multi_aff_alloc(domain
, ma
);
2503 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2504 * This obivously only works if the input "map" is single-valued.
2505 * If so, we compute the lexicographic minimum of the image in the form
2506 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2507 * to its lexicographic minimum.
2508 * If the input is not single-valued, we produce an error.
2510 * As a special case, we first check if all output dimensions are uniquely
2511 * defined in terms of the parameters and input dimensions over the entire
2512 * domain. If so, we extract the desired isl_pw_multi_aff directly
2513 * from the affine hull of "map" and its domain.
2515 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2519 isl_pw_multi_aff
*pma
;
2520 isl_basic_map
*hull
;
2525 hull
= isl_map_affine_hull(isl_map_copy(map
));
2526 sv
= isl_basic_map_plain_is_single_valued(hull
);
2528 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
2529 isl_basic_map_free(hull
);
2533 sv
= isl_map_is_single_valued(map
);
2537 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2538 "map is not single-valued", goto error
);
2539 map
= isl_map_make_disjoint(map
);
2543 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2545 for (i
= 0; i
< map
->n
; ++i
) {
2546 isl_pw_multi_aff
*pma_i
;
2547 isl_basic_map
*bmap
;
2548 bmap
= isl_basic_map_copy(map
->p
[i
]);
2549 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2550 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2560 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2562 return isl_pw_multi_aff_from_map(set
);
2565 /* Return the piecewise affine expression "set ? 1 : 0".
2567 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
2570 isl_space
*space
= isl_set_get_space(set
);
2571 isl_local_space
*ls
= isl_local_space_from_space(space
);
2572 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
2573 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
2575 one
= isl_aff_add_constant_si(one
, 1);
2576 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
2577 set
= isl_set_complement(set
);
2578 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
2583 /* Plug in "subs" for dimension "type", "pos" of "aff".
2585 * Let i be the dimension to replace and let "subs" be of the form
2589 * and "aff" of the form
2595 * floor((a f + d g')/(m d))
2597 * where g' is the result of plugging in "subs" in each of the integer
2600 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2601 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2606 aff
= isl_aff_cow(aff
);
2608 return isl_aff_free(aff
);
2610 ctx
= isl_aff_get_ctx(aff
);
2611 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2612 isl_die(ctx
, isl_error_invalid
,
2613 "spaces don't match", return isl_aff_free(aff
));
2614 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2615 isl_die(ctx
, isl_error_unsupported
,
2616 "cannot handle divs yet", return isl_aff_free(aff
));
2618 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
2620 return isl_aff_free(aff
);
2622 aff
->v
= isl_vec_cow(aff
->v
);
2624 return isl_aff_free(aff
);
2626 pos
+= isl_local_space_offset(aff
->ls
, type
);
2629 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
2630 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
2631 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
2632 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
2633 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
2639 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2640 * expressions in "maff".
2642 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
2643 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
2644 __isl_keep isl_aff
*subs
)
2648 maff
= isl_multi_aff_cow(maff
);
2650 return isl_multi_aff_free(maff
);
2652 if (type
== isl_dim_in
)
2655 for (i
= 0; i
< maff
->n
; ++i
) {
2656 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
2658 return isl_multi_aff_free(maff
);
2664 /* Plug in "subs" for dimension "type", "pos" of "pma".
2666 * pma is of the form
2670 * while subs is of the form
2672 * v' = B_j(v) -> S_j
2674 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2675 * has a contribution in the result, in particular
2677 * C_ij(S_j) -> M_i(S_j)
2679 * Note that plugging in S_j in C_ij may also result in an empty set
2680 * and this contribution should simply be discarded.
2682 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
2683 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
2684 __isl_keep isl_pw_aff
*subs
)
2687 isl_pw_multi_aff
*res
;
2690 return isl_pw_multi_aff_free(pma
);
2692 n
= pma
->n
* subs
->n
;
2693 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
2695 for (i
= 0; i
< pma
->n
; ++i
) {
2696 for (j
= 0; j
< subs
->n
; ++j
) {
2698 isl_multi_aff
*res_ij
;
2699 common
= isl_set_intersect(
2700 isl_set_copy(pma
->p
[i
].set
),
2701 isl_set_copy(subs
->p
[j
].set
));
2702 common
= isl_set_substitute(common
,
2703 type
, pos
, subs
->p
[j
].aff
);
2704 if (isl_set_plain_is_empty(common
)) {
2705 isl_set_free(common
);
2709 res_ij
= isl_multi_aff_substitute(
2710 isl_multi_aff_copy(pma
->p
[i
].maff
),
2711 type
, pos
, subs
->p
[j
].aff
);
2713 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
2717 isl_pw_multi_aff_free(pma
);
2721 /* Extend the local space of "dst" to include the divs
2722 * in the local space of "src".
2724 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
2725 __isl_keep isl_aff
*src
)
2733 return isl_aff_free(dst
);
2735 ctx
= isl_aff_get_ctx(src
);
2736 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
2737 isl_die(ctx
, isl_error_invalid
,
2738 "spaces don't match", goto error
);
2740 if (src
->ls
->div
->n_row
== 0)
2743 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
2744 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
2748 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
2749 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
2757 return isl_aff_free(dst
);
2760 /* Adjust the local spaces of the affine expressions in "maff"
2761 * such that they all have the save divs.
2763 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
2764 __isl_take isl_multi_aff
*maff
)
2772 maff
= isl_multi_aff_cow(maff
);
2776 for (i
= 1; i
< maff
->n
; ++i
)
2777 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
2778 for (i
= 1; i
< maff
->n
; ++i
) {
2779 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
2781 return isl_multi_aff_free(maff
);
2787 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
2789 aff
= isl_aff_cow(aff
);
2793 aff
->ls
= isl_local_space_lift(aff
->ls
);
2795 return isl_aff_free(aff
);
2800 /* Lift "maff" to a space with extra dimensions such that the result
2801 * has no more existentially quantified variables.
2802 * If "ls" is not NULL, then *ls is assigned the local space that lies
2803 * at the basis of the lifting applied to "maff".
2805 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
2806 __isl_give isl_local_space
**ls
)
2820 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
2821 *ls
= isl_local_space_from_space(space
);
2823 return isl_multi_aff_free(maff
);
2828 maff
= isl_multi_aff_cow(maff
);
2829 maff
= isl_multi_aff_align_divs(maff
);
2833 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
2834 space
= isl_multi_aff_get_space(maff
);
2835 space
= isl_space_lift(isl_space_domain(space
), n_div
);
2836 space
= isl_space_extend_domain_with_range(space
,
2837 isl_multi_aff_get_space(maff
));
2839 return isl_multi_aff_free(maff
);
2840 isl_space_free(maff
->space
);
2841 maff
->space
= space
;
2844 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
2846 return isl_multi_aff_free(maff
);
2849 for (i
= 0; i
< maff
->n
; ++i
) {
2850 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
2858 isl_local_space_free(*ls
);
2859 return isl_multi_aff_free(maff
);
2863 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2865 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
2866 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
2876 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
2877 if (pos
< 0 || pos
>= n_out
)
2878 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2879 "index out of bounds", return NULL
);
2881 space
= isl_pw_multi_aff_get_space(pma
);
2882 space
= isl_space_drop_dims(space
, isl_dim_out
,
2883 pos
+ 1, n_out
- pos
- 1);
2884 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
2886 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
2887 for (i
= 0; i
< pma
->n
; ++i
) {
2889 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
2890 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);