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 MIT 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 /* Add "v" to the numerator of the constant term of "aff".
438 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
440 if (isl_int_is_zero(v
))
443 aff
= isl_aff_cow(aff
);
447 aff
->v
= isl_vec_cow(aff
->v
);
449 return isl_aff_free(aff
);
451 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
456 /* Add "v" to the numerator of the constant term of "aff".
458 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
466 isl_int_set_si(t
, v
);
467 aff
= isl_aff_add_constant_num(aff
, t
);
473 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
475 aff
= isl_aff_cow(aff
);
479 aff
->v
= isl_vec_cow(aff
->v
);
481 return isl_aff_free(aff
);
483 isl_int_set_si(aff
->v
->el
[1], v
);
488 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
489 enum isl_dim_type type
, int pos
, isl_int v
)
494 if (type
== isl_dim_out
)
495 isl_die(aff
->v
->ctx
, isl_error_invalid
,
496 "output/set dimension does not have a coefficient",
497 return isl_aff_free(aff
));
498 if (type
== isl_dim_in
)
501 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
502 isl_die(aff
->v
->ctx
, isl_error_invalid
,
503 "position out of bounds", return isl_aff_free(aff
));
505 aff
= isl_aff_cow(aff
);
509 aff
->v
= isl_vec_cow(aff
->v
);
511 return isl_aff_free(aff
);
513 pos
+= isl_local_space_offset(aff
->ls
, type
);
514 isl_int_set(aff
->v
->el
[1 + pos
], v
);
519 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
520 enum isl_dim_type type
, int pos
, int v
)
525 if (type
== isl_dim_out
)
526 isl_die(aff
->v
->ctx
, isl_error_invalid
,
527 "output/set dimension does not have a coefficient",
528 return isl_aff_free(aff
));
529 if (type
== isl_dim_in
)
532 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
533 isl_die(aff
->v
->ctx
, isl_error_invalid
,
534 "position out of bounds", return isl_aff_free(aff
));
536 aff
= isl_aff_cow(aff
);
540 aff
->v
= isl_vec_cow(aff
->v
);
542 return isl_aff_free(aff
);
544 pos
+= isl_local_space_offset(aff
->ls
, type
);
545 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
550 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
551 enum isl_dim_type type
, int pos
, isl_int v
)
556 if (type
== isl_dim_out
)
557 isl_die(aff
->v
->ctx
, isl_error_invalid
,
558 "output/set dimension does not have a coefficient",
559 return isl_aff_free(aff
));
560 if (type
== isl_dim_in
)
563 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
564 isl_die(aff
->v
->ctx
, isl_error_invalid
,
565 "position out of bounds", return isl_aff_free(aff
));
567 aff
= isl_aff_cow(aff
);
571 aff
->v
= isl_vec_cow(aff
->v
);
573 return isl_aff_free(aff
);
575 pos
+= isl_local_space_offset(aff
->ls
, type
);
576 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
581 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
582 enum isl_dim_type type
, int pos
, int v
)
587 isl_int_set_si(t
, v
);
588 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
594 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
599 return isl_local_space_get_div(aff
->ls
, pos
);
602 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
604 aff
= isl_aff_cow(aff
);
607 aff
->v
= isl_vec_cow(aff
->v
);
609 return isl_aff_free(aff
);
611 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
616 /* Remove divs from the local space that do not appear in the affine
618 * We currently only remove divs at the end.
619 * Some intermediate divs may also not appear directly in the affine
620 * expression, but we would also need to check that no other divs are
621 * defined in terms of them.
623 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
632 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
633 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
635 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
639 aff
= isl_aff_cow(aff
);
643 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
644 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
645 if (!aff
->ls
|| !aff
->v
)
646 return isl_aff_free(aff
);
651 /* Normalize the representation of "aff".
653 * This function should only be called of "new" isl_affs, i.e.,
654 * with only a single reference. We therefore do not need to
655 * worry about affecting other instances.
657 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
661 aff
->v
= isl_vec_normalize(aff
->v
);
663 return isl_aff_free(aff
);
664 aff
= isl_aff_remove_unused_divs(aff
);
668 /* Given f, return floor(f).
669 * If f is an integer expression, then just return f.
670 * If f is a constant, then return the constant floor(f).
671 * Otherwise, if f = g/m, write g = q m + r,
672 * create a new div d = [r/m] and return the expression q + d.
673 * The coefficients in r are taken to lie between -m/2 and m/2.
675 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
685 if (isl_int_is_one(aff
->v
->el
[0]))
688 aff
= isl_aff_cow(aff
);
692 aff
->v
= isl_vec_cow(aff
->v
);
694 return isl_aff_free(aff
);
696 if (isl_aff_is_cst(aff
)) {
697 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
698 isl_int_set_si(aff
->v
->el
[0], 1);
702 div
= isl_vec_copy(aff
->v
);
703 div
= isl_vec_cow(div
);
705 return isl_aff_free(aff
);
707 ctx
= isl_aff_get_ctx(aff
);
708 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
709 for (i
= 1; i
< aff
->v
->size
; ++i
) {
710 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
711 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
712 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
713 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
714 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
718 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
720 return isl_aff_free(aff
);
723 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
725 return isl_aff_free(aff
);
726 isl_int_set_si(aff
->v
->el
[0], 1);
727 isl_int_set_si(aff
->v
->el
[size
], 1);
734 * aff mod m = aff - m * floor(aff/m)
736 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
740 res
= isl_aff_copy(aff
);
741 aff
= isl_aff_scale_down(aff
, m
);
742 aff
= isl_aff_floor(aff
);
743 aff
= isl_aff_scale(aff
, m
);
744 res
= isl_aff_sub(res
, aff
);
751 * pwaff mod m = pwaff - m * floor(pwaff/m)
753 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
757 res
= isl_pw_aff_copy(pwaff
);
758 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
759 pwaff
= isl_pw_aff_floor(pwaff
);
760 pwaff
= isl_pw_aff_scale(pwaff
, m
);
761 res
= isl_pw_aff_sub(res
, pwaff
);
766 /* Given f, return ceil(f).
767 * If f is an integer expression, then just return f.
768 * Otherwise, create a new div d = [-f] and return the expression -d.
770 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
775 if (isl_int_is_one(aff
->v
->el
[0]))
778 aff
= isl_aff_neg(aff
);
779 aff
= isl_aff_floor(aff
);
780 aff
= isl_aff_neg(aff
);
785 /* Apply the expansion computed by isl_merge_divs.
786 * The expansion itself is given by "exp" while the resulting
787 * list of divs is given by "div".
789 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
790 __isl_take isl_mat
*div
, int *exp
)
797 aff
= isl_aff_cow(aff
);
801 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
802 new_n_div
= isl_mat_rows(div
);
803 if (new_n_div
< old_n_div
)
804 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
805 "not an expansion", goto error
);
807 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
811 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
813 for (i
= new_n_div
- 1; i
>= 0; --i
) {
814 if (j
>= 0 && exp
[j
] == i
) {
816 isl_int_swap(aff
->v
->el
[offset
+ i
],
817 aff
->v
->el
[offset
+ j
]);
820 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
823 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
834 /* Add two affine expressions that live in the same local space.
836 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
837 __isl_take isl_aff
*aff2
)
841 aff1
= isl_aff_cow(aff1
);
845 aff1
->v
= isl_vec_cow(aff1
->v
);
851 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
852 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
853 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
854 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
855 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
856 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
857 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
869 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
870 __isl_take isl_aff
*aff2
)
880 ctx
= isl_aff_get_ctx(aff1
);
881 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
882 isl_die(ctx
, isl_error_invalid
,
883 "spaces don't match", goto error
);
885 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
886 return add_expanded(aff1
, aff2
);
888 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
889 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
893 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
894 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
895 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
899 return add_expanded(aff1
, aff2
);
908 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
909 __isl_take isl_aff
*aff2
)
911 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
914 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
918 if (isl_int_is_one(f
))
921 aff
= isl_aff_cow(aff
);
924 aff
->v
= isl_vec_cow(aff
->v
);
926 return isl_aff_free(aff
);
929 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
930 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
931 isl_int_divexact(gcd
, f
, gcd
);
932 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
938 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
942 if (isl_int_is_one(f
))
945 aff
= isl_aff_cow(aff
);
949 if (isl_int_is_zero(f
))
950 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
951 "cannot scale down by zero", return isl_aff_free(aff
));
953 aff
->v
= isl_vec_cow(aff
->v
);
955 return isl_aff_free(aff
);
958 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
959 isl_int_gcd(gcd
, gcd
, f
);
960 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
961 isl_int_divexact(gcd
, f
, gcd
);
962 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
968 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
976 isl_int_set_ui(v
, f
);
977 aff
= isl_aff_scale_down(aff
, v
);
983 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
984 enum isl_dim_type type
, unsigned pos
, const char *s
)
986 aff
= isl_aff_cow(aff
);
989 if (type
== isl_dim_out
)
990 isl_die(aff
->v
->ctx
, isl_error_invalid
,
991 "cannot set name of output/set dimension",
992 return isl_aff_free(aff
));
993 if (type
== isl_dim_in
)
995 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
997 return isl_aff_free(aff
);
1002 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1003 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1005 aff
= isl_aff_cow(aff
);
1007 return isl_id_free(id
);
1008 if (type
== isl_dim_out
)
1009 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1010 "cannot set name of output/set dimension",
1012 if (type
== isl_dim_in
)
1014 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1016 return isl_aff_free(aff
);
1025 /* Exploit the equalities in "eq" to simplify the affine expression
1026 * and the expressions of the integer divisions in the local space.
1027 * The integer divisions in this local space are assumed to appear
1028 * as regular dimensions in "eq".
1030 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1031 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1039 if (eq
->n_eq
== 0) {
1040 isl_basic_set_free(eq
);
1044 aff
= isl_aff_cow(aff
);
1048 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1049 isl_basic_set_copy(eq
));
1053 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1055 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1056 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1057 if (j
< 0 || j
== 0 || j
>= total
)
1060 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1064 isl_basic_set_free(eq
);
1065 aff
= isl_aff_normalize(aff
);
1068 isl_basic_set_free(eq
);
1073 /* Exploit the equalities in "eq" to simplify the affine expression
1074 * and the expressions of the integer divisions in the local space.
1076 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1077 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1083 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1085 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
1086 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1088 isl_basic_set_free(eq
);
1093 /* Look for equalities among the variables shared by context and aff
1094 * and the integer divisions of aff, if any.
1095 * The equalities are then used to eliminate coefficients and/or integer
1096 * divisions from aff.
1098 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1099 __isl_take isl_set
*context
)
1101 isl_basic_set
*hull
;
1106 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1108 isl_basic_set
*bset
;
1109 isl_local_space
*ls
;
1110 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1111 ls
= isl_aff_get_domain_local_space(aff
);
1112 bset
= isl_basic_set_from_local_space(ls
);
1113 bset
= isl_basic_set_lift(bset
);
1114 bset
= isl_basic_set_flatten(bset
);
1115 context
= isl_set_intersect(context
,
1116 isl_set_from_basic_set(bset
));
1119 hull
= isl_set_affine_hull(context
);
1120 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1123 isl_set_free(context
);
1127 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1128 __isl_take isl_set
*context
)
1130 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1131 dom_context
= isl_set_intersect_params(dom_context
, context
);
1132 return isl_aff_gist(aff
, dom_context
);
1135 /* Return a basic set containing those elements in the space
1136 * of aff where it is non-negative.
1138 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1140 isl_constraint
*ineq
;
1141 isl_basic_set
*bset
;
1143 ineq
= isl_inequality_from_aff(aff
);
1145 bset
= isl_basic_set_from_constraint(ineq
);
1146 bset
= isl_basic_set_simplify(bset
);
1150 /* Return a basic set containing those elements in the domain space
1151 * of aff where it is negative.
1153 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
1155 aff
= isl_aff_neg(aff
);
1156 aff
= isl_aff_add_constant_num_si(aff
, -1);
1157 return isl_aff_nonneg_basic_set(aff
);
1160 /* Return a basic set containing those elements in the space
1161 * of aff where it is zero.
1163 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1165 isl_constraint
*ineq
;
1166 isl_basic_set
*bset
;
1168 ineq
= isl_equality_from_aff(aff
);
1170 bset
= isl_basic_set_from_constraint(ineq
);
1171 bset
= isl_basic_set_simplify(bset
);
1175 /* Return a basic set containing those elements in the shared space
1176 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1178 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1179 __isl_take isl_aff
*aff2
)
1181 aff1
= isl_aff_sub(aff1
, aff2
);
1183 return isl_aff_nonneg_basic_set(aff1
);
1186 /* Return a basic set containing those elements in the shared space
1187 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1189 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1190 __isl_take isl_aff
*aff2
)
1192 return isl_aff_ge_basic_set(aff2
, aff1
);
1195 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1196 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1198 aff1
= isl_aff_add(aff1
, aff2
);
1199 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1203 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1211 /* Check whether the given affine expression has non-zero coefficient
1212 * for any dimension in the given range or if any of these dimensions
1213 * appear with non-zero coefficients in any of the integer divisions
1214 * involved in the affine expression.
1216 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1217 enum isl_dim_type type
, unsigned first
, unsigned n
)
1229 ctx
= isl_aff_get_ctx(aff
);
1230 if (first
+ n
> isl_aff_dim(aff
, type
))
1231 isl_die(ctx
, isl_error_invalid
,
1232 "range out of bounds", return -1);
1234 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1238 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1239 for (i
= 0; i
< n
; ++i
)
1240 if (active
[first
+ i
]) {
1253 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1254 enum isl_dim_type type
, unsigned first
, unsigned n
)
1260 if (type
== isl_dim_out
)
1261 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1262 "cannot drop output/set dimension",
1263 return isl_aff_free(aff
));
1264 if (type
== isl_dim_in
)
1266 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1269 ctx
= isl_aff_get_ctx(aff
);
1270 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1271 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1272 return isl_aff_free(aff
));
1274 aff
= isl_aff_cow(aff
);
1278 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1280 return isl_aff_free(aff
);
1282 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1283 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1285 return isl_aff_free(aff
);
1290 /* Project the domain of the affine expression onto its parameter space.
1291 * The affine expression may not involve any of the domain dimensions.
1293 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
1299 n
= isl_aff_dim(aff
, isl_dim_in
);
1300 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
1302 return isl_aff_free(aff
);
1304 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1305 "affine expression involves some of the domain dimensions",
1306 return isl_aff_free(aff
));
1307 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
1308 space
= isl_aff_get_domain_space(aff
);
1309 space
= isl_space_params(space
);
1310 aff
= isl_aff_reset_domain_space(aff
, space
);
1314 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1315 enum isl_dim_type type
, unsigned first
, unsigned n
)
1321 if (type
== isl_dim_out
)
1322 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1323 "cannot insert output/set dimensions",
1324 return isl_aff_free(aff
));
1325 if (type
== isl_dim_in
)
1327 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1330 ctx
= isl_aff_get_ctx(aff
);
1331 if (first
> isl_local_space_dim(aff
->ls
, type
))
1332 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1333 return isl_aff_free(aff
));
1335 aff
= isl_aff_cow(aff
);
1339 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1341 return isl_aff_free(aff
);
1343 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1344 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1346 return isl_aff_free(aff
);
1351 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1352 enum isl_dim_type type
, unsigned n
)
1356 pos
= isl_aff_dim(aff
, type
);
1358 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1361 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1362 enum isl_dim_type type
, unsigned n
)
1366 pos
= isl_pw_aff_dim(pwaff
, type
);
1368 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1371 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1373 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1374 return isl_pw_aff_alloc(dom
, aff
);
1378 #define PW isl_pw_aff
1382 #define EL_IS_ZERO is_empty
1386 #define IS_ZERO is_empty
1389 #undef DEFAULT_IS_ZERO
1390 #define DEFAULT_IS_ZERO 0
1394 #define NO_MOVE_DIMS
1398 #include <isl_pw_templ.c>
1400 static __isl_give isl_set
*align_params_pw_pw_set_and(
1401 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1402 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1403 __isl_take isl_pw_aff
*pwaff2
))
1405 if (!pwaff1
|| !pwaff2
)
1407 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1408 pwaff2
->dim
, isl_dim_param
))
1409 return fn(pwaff1
, pwaff2
);
1410 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1411 !isl_space_has_named_params(pwaff2
->dim
))
1412 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1413 "unaligned unnamed parameters", goto error
);
1414 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1415 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1416 return fn(pwaff1
, pwaff2
);
1418 isl_pw_aff_free(pwaff1
);
1419 isl_pw_aff_free(pwaff2
);
1423 /* Compute a piecewise quasi-affine expression with a domain that
1424 * is the union of those of pwaff1 and pwaff2 and such that on each
1425 * cell, the quasi-affine expression is the better (according to cmp)
1426 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1427 * is defined on a given cell, then the associated expression
1428 * is the defined one.
1430 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1431 __isl_take isl_pw_aff
*pwaff2
,
1432 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1433 __isl_take isl_aff
*aff2
))
1440 if (!pwaff1
|| !pwaff2
)
1443 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1444 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1445 isl_die(ctx
, isl_error_invalid
,
1446 "arguments should live in same space", goto error
);
1448 if (isl_pw_aff_is_empty(pwaff1
)) {
1449 isl_pw_aff_free(pwaff1
);
1453 if (isl_pw_aff_is_empty(pwaff2
)) {
1454 isl_pw_aff_free(pwaff2
);
1458 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1459 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1461 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1462 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1463 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1464 struct isl_set
*common
;
1467 common
= isl_set_intersect(
1468 isl_set_copy(pwaff1
->p
[i
].set
),
1469 isl_set_copy(pwaff2
->p
[j
].set
));
1470 better
= isl_set_from_basic_set(cmp(
1471 isl_aff_copy(pwaff2
->p
[j
].aff
),
1472 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1473 better
= isl_set_intersect(common
, better
);
1474 if (isl_set_plain_is_empty(better
)) {
1475 isl_set_free(better
);
1478 set
= isl_set_subtract(set
, isl_set_copy(better
));
1480 res
= isl_pw_aff_add_piece(res
, better
,
1481 isl_aff_copy(pwaff2
->p
[j
].aff
));
1483 res
= isl_pw_aff_add_piece(res
, set
,
1484 isl_aff_copy(pwaff1
->p
[i
].aff
));
1487 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1488 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1489 for (i
= 0; i
< pwaff1
->n
; ++i
)
1490 set
= isl_set_subtract(set
,
1491 isl_set_copy(pwaff1
->p
[i
].set
));
1492 res
= isl_pw_aff_add_piece(res
, set
,
1493 isl_aff_copy(pwaff2
->p
[j
].aff
));
1496 isl_pw_aff_free(pwaff1
);
1497 isl_pw_aff_free(pwaff2
);
1501 isl_pw_aff_free(pwaff1
);
1502 isl_pw_aff_free(pwaff2
);
1506 /* Compute a piecewise quasi-affine expression with a domain that
1507 * is the union of those of pwaff1 and pwaff2 and such that on each
1508 * cell, the quasi-affine expression is the maximum of those of pwaff1
1509 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1510 * cell, then the associated expression is the defined one.
1512 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1513 __isl_take isl_pw_aff
*pwaff2
)
1515 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1518 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1519 __isl_take isl_pw_aff
*pwaff2
)
1521 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1525 /* Compute a piecewise quasi-affine expression with a domain that
1526 * is the union of those of pwaff1 and pwaff2 and such that on each
1527 * cell, the quasi-affine expression is the minimum of those of pwaff1
1528 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1529 * cell, then the associated expression is the defined one.
1531 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1532 __isl_take isl_pw_aff
*pwaff2
)
1534 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1537 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1538 __isl_take isl_pw_aff
*pwaff2
)
1540 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1544 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1545 __isl_take isl_pw_aff
*pwaff2
, int max
)
1548 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1550 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1553 /* Construct a map with as domain the domain of pwaff and
1554 * one-dimensional range corresponding to the affine expressions.
1556 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1565 dim
= isl_pw_aff_get_space(pwaff
);
1566 map
= isl_map_empty(dim
);
1568 for (i
= 0; i
< pwaff
->n
; ++i
) {
1569 isl_basic_map
*bmap
;
1572 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1573 map_i
= isl_map_from_basic_map(bmap
);
1574 map_i
= isl_map_intersect_domain(map_i
,
1575 isl_set_copy(pwaff
->p
[i
].set
));
1576 map
= isl_map_union_disjoint(map
, map_i
);
1579 isl_pw_aff_free(pwaff
);
1584 /* Construct a map with as domain the domain of pwaff and
1585 * one-dimensional range corresponding to the affine expressions.
1587 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1591 if (isl_space_is_set(pwaff
->dim
))
1592 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1593 "space of input is not a map",
1594 return isl_pw_aff_free(pwaff
));
1595 return map_from_pw_aff(pwaff
);
1598 /* Construct a one-dimensional set with as parameter domain
1599 * the domain of pwaff and the single set dimension
1600 * corresponding to the affine expressions.
1602 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1606 if (!isl_space_is_set(pwaff
->dim
))
1607 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1608 "space of input is not a set",
1609 return isl_pw_aff_free(pwaff
));
1610 return map_from_pw_aff(pwaff
);
1613 /* Return a set containing those elements in the domain
1614 * of pwaff where it is non-negative.
1616 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1624 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1626 for (i
= 0; i
< pwaff
->n
; ++i
) {
1627 isl_basic_set
*bset
;
1630 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1631 set_i
= isl_set_from_basic_set(bset
);
1632 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1633 set
= isl_set_union_disjoint(set
, set_i
);
1636 isl_pw_aff_free(pwaff
);
1641 /* Return a set containing those elements in the domain
1642 * of pwaff where it is zero (if complement is 0) or not zero
1643 * (if complement is 1).
1645 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
1654 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1656 for (i
= 0; i
< pwaff
->n
; ++i
) {
1657 isl_basic_set
*bset
;
1658 isl_set
*set_i
, *zero
;
1660 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1661 zero
= isl_set_from_basic_set(bset
);
1662 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
1664 set_i
= isl_set_subtract(set_i
, zero
);
1666 set_i
= isl_set_intersect(set_i
, zero
);
1667 set
= isl_set_union_disjoint(set
, set_i
);
1670 isl_pw_aff_free(pwaff
);
1675 /* Return a set containing those elements in the domain
1676 * of pwaff where it is zero.
1678 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1680 return pw_aff_zero_set(pwaff
, 0);
1683 /* Return a set containing those elements in the domain
1684 * of pwaff where it is not zero.
1686 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1688 return pw_aff_zero_set(pwaff
, 1);
1691 /* Return a set containing those elements in the shared domain
1692 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1694 * We compute the difference on the shared domain and then construct
1695 * the set of values where this difference is non-negative.
1696 * If strict is set, we first subtract 1 from the difference.
1697 * If equal is set, we only return the elements where pwaff1 and pwaff2
1700 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1701 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1703 isl_set
*set1
, *set2
;
1705 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1706 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1707 set1
= isl_set_intersect(set1
, set2
);
1708 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1709 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1710 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1713 isl_space
*dim
= isl_set_get_space(set1
);
1715 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1716 aff
= isl_aff_add_constant_si(aff
, -1);
1717 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1722 return isl_pw_aff_zero_set(pwaff1
);
1723 return isl_pw_aff_nonneg_set(pwaff1
);
1726 /* Return a set containing those elements in the shared domain
1727 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1729 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1730 __isl_take isl_pw_aff
*pwaff2
)
1732 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1735 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1736 __isl_take isl_pw_aff
*pwaff2
)
1738 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1741 /* Return a set containing those elements in the shared domain
1742 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1744 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1745 __isl_take isl_pw_aff
*pwaff2
)
1747 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1750 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1751 __isl_take isl_pw_aff
*pwaff2
)
1753 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1756 /* Return a set containing those elements in the shared domain
1757 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1759 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1760 __isl_take isl_pw_aff
*pwaff2
)
1762 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1765 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1766 __isl_take isl_pw_aff
*pwaff2
)
1768 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1771 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1772 __isl_take isl_pw_aff
*pwaff2
)
1774 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1777 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1778 __isl_take isl_pw_aff
*pwaff2
)
1780 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1783 /* Return a set containing those elements in the shared domain
1784 * of the elements of list1 and list2 where each element in list1
1785 * has the relation specified by "fn" with each element in list2.
1787 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1788 __isl_take isl_pw_aff_list
*list2
,
1789 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1790 __isl_take isl_pw_aff
*pwaff2
))
1796 if (!list1
|| !list2
)
1799 ctx
= isl_pw_aff_list_get_ctx(list1
);
1800 if (list1
->n
< 1 || list2
->n
< 1)
1801 isl_die(ctx
, isl_error_invalid
,
1802 "list should contain at least one element", goto error
);
1804 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1805 for (i
= 0; i
< list1
->n
; ++i
)
1806 for (j
= 0; j
< list2
->n
; ++j
) {
1809 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1810 isl_pw_aff_copy(list2
->p
[j
]));
1811 set
= isl_set_intersect(set
, set_ij
);
1814 isl_pw_aff_list_free(list1
);
1815 isl_pw_aff_list_free(list2
);
1818 isl_pw_aff_list_free(list1
);
1819 isl_pw_aff_list_free(list2
);
1823 /* Return a set containing those elements in the shared domain
1824 * of the elements of list1 and list2 where each element in list1
1825 * is equal to each element in list2.
1827 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1828 __isl_take isl_pw_aff_list
*list2
)
1830 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1833 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1834 __isl_take isl_pw_aff_list
*list2
)
1836 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1839 /* Return a set containing those elements in the shared domain
1840 * of the elements of list1 and list2 where each element in list1
1841 * is less than or equal to each element in list2.
1843 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1844 __isl_take isl_pw_aff_list
*list2
)
1846 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1849 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1850 __isl_take isl_pw_aff_list
*list2
)
1852 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1855 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1856 __isl_take isl_pw_aff_list
*list2
)
1858 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1861 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1862 __isl_take isl_pw_aff_list
*list2
)
1864 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1868 /* Return a set containing those elements in the shared domain
1869 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1871 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1872 __isl_take isl_pw_aff
*pwaff2
)
1874 isl_set
*set_lt
, *set_gt
;
1876 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1877 isl_pw_aff_copy(pwaff2
));
1878 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1879 return isl_set_union_disjoint(set_lt
, set_gt
);
1882 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1883 __isl_take isl_pw_aff
*pwaff2
)
1885 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1888 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1893 if (isl_int_is_one(v
))
1895 if (!isl_int_is_pos(v
))
1896 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1897 "factor needs to be positive",
1898 return isl_pw_aff_free(pwaff
));
1899 pwaff
= isl_pw_aff_cow(pwaff
);
1905 for (i
= 0; i
< pwaff
->n
; ++i
) {
1906 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1907 if (!pwaff
->p
[i
].aff
)
1908 return isl_pw_aff_free(pwaff
);
1914 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1918 pwaff
= isl_pw_aff_cow(pwaff
);
1924 for (i
= 0; i
< pwaff
->n
; ++i
) {
1925 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1926 if (!pwaff
->p
[i
].aff
)
1927 return isl_pw_aff_free(pwaff
);
1933 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1937 pwaff
= isl_pw_aff_cow(pwaff
);
1943 for (i
= 0; i
< pwaff
->n
; ++i
) {
1944 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1945 if (!pwaff
->p
[i
].aff
)
1946 return isl_pw_aff_free(pwaff
);
1952 /* Assuming that "cond1" and "cond2" are disjoint,
1953 * return an affine expression that is equal to pwaff1 on cond1
1954 * and to pwaff2 on cond2.
1956 static __isl_give isl_pw_aff
*isl_pw_aff_select(
1957 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
1958 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
1960 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
1961 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
1963 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
1966 /* Return an affine expression that is equal to pwaff_true for elements
1967 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1969 * That is, return cond ? pwaff_true : pwaff_false;
1971 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
1972 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1974 isl_set
*cond_true
, *cond_false
;
1976 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1977 cond_false
= isl_pw_aff_zero_set(cond
);
1978 return isl_pw_aff_select(cond_true
, pwaff_true
,
1979 cond_false
, pwaff_false
);
1982 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1987 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1990 /* Check whether pwaff is a piecewise constant.
1992 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1999 for (i
= 0; i
< pwaff
->n
; ++i
) {
2000 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
2001 if (is_cst
< 0 || !is_cst
)
2008 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
2009 __isl_take isl_aff
*aff2
)
2011 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2012 return isl_aff_mul(aff2
, aff1
);
2014 if (!isl_aff_is_cst(aff2
))
2015 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2016 "at least one affine expression should be constant",
2019 aff1
= isl_aff_cow(aff1
);
2023 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2024 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2034 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2035 __isl_take isl_pw_aff
*pwaff2
)
2037 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2040 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2041 __isl_take isl_pw_aff
*pwaff2
)
2043 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2046 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2047 __isl_take isl_pw_aff
*pwaff2
)
2049 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2052 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2053 __isl_take isl_pw_aff
*pwaff2
)
2055 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2058 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2059 __isl_take isl_pw_aff
*pwaff2
)
2061 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2064 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2065 __isl_take isl_pw_aff
*pwaff2
)
2070 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2071 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2072 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
2073 isl_pw_aff_copy(pwaff2
));
2074 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
2075 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
2078 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2079 __isl_take isl_pw_aff
*pwaff2
)
2081 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
2084 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2085 __isl_take isl_pw_aff
*pwaff2
)
2090 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2091 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2092 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2093 isl_pw_aff_copy(pwaff2
));
2094 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
2095 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
2098 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2099 __isl_take isl_pw_aff
*pwaff2
)
2101 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2104 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2105 __isl_take isl_pw_aff_list
*list
,
2106 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2107 __isl_take isl_pw_aff
*pwaff2
))
2116 ctx
= isl_pw_aff_list_get_ctx(list
);
2118 isl_die(ctx
, isl_error_invalid
,
2119 "list should contain at least one element",
2120 return isl_pw_aff_list_free(list
));
2122 res
= isl_pw_aff_copy(list
->p
[0]);
2123 for (i
= 1; i
< list
->n
; ++i
)
2124 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2126 isl_pw_aff_list_free(list
);
2130 /* Return an isl_pw_aff that maps each element in the intersection of the
2131 * domains of the elements of list to the minimal corresponding affine
2134 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2136 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2139 /* Return an isl_pw_aff that maps each element in the intersection of the
2140 * domains of the elements of list to the maximal corresponding affine
2143 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2145 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2151 #include <isl_multi_templ.c>
2153 /* Construct an isl_multi_aff in the given space with value zero in
2154 * each of the output dimensions.
2156 __isl_give isl_multi_aff
*isl_multi_aff_zero(__isl_take isl_space
*space
)
2164 n
= isl_space_dim(space
, isl_dim_out
);
2165 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2168 isl_space_free(space
);
2171 isl_local_space
*ls
;
2174 space
= isl_space_domain(space
);
2175 ls
= isl_local_space_from_space(space
);
2176 aff
= isl_aff_zero_on_domain(ls
);
2178 for (i
= 0; i
< n
; ++i
)
2179 ma
= isl_multi_aff_set_aff(ma
, i
, isl_aff_copy(aff
));
2187 /* Create an isl_multi_aff in the given space that maps each
2188 * input dimension to the corresponding output dimension.
2190 __isl_give isl_multi_aff
*isl_multi_aff_identity(__isl_take isl_space
*space
)
2198 if (isl_space_is_set(space
))
2199 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2200 "expecting map space", goto error
);
2202 n
= isl_space_dim(space
, isl_dim_out
);
2203 if (n
!= isl_space_dim(space
, isl_dim_in
))
2204 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2205 "number of input and output dimensions needs to be "
2206 "the same", goto error
);
2208 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2211 isl_space_free(space
);
2214 isl_local_space
*ls
;
2217 space
= isl_space_domain(space
);
2218 ls
= isl_local_space_from_space(space
);
2219 aff
= isl_aff_zero_on_domain(ls
);
2221 for (i
= 0; i
< n
; ++i
) {
2223 aff_i
= isl_aff_copy(aff
);
2224 aff_i
= isl_aff_add_coefficient_si(aff_i
,
2226 ma
= isl_multi_aff_set_aff(ma
, i
, aff_i
);
2234 isl_space_free(space
);
2238 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2241 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
2242 __isl_take isl_multi_aff
*ma
)
2244 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
2245 return isl_pw_multi_aff_alloc(dom
, ma
);
2248 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2249 __isl_take isl_multi_aff
*maff2
)
2254 maff1
= isl_multi_aff_cow(maff1
);
2255 if (!maff1
|| !maff2
)
2258 ctx
= isl_multi_aff_get_ctx(maff1
);
2259 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2260 isl_die(ctx
, isl_error_invalid
,
2261 "spaces don't match", goto error
);
2263 for (i
= 0; i
< maff1
->n
; ++i
) {
2264 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2265 isl_aff_copy(maff2
->p
[i
]));
2270 isl_multi_aff_free(maff2
);
2273 isl_multi_aff_free(maff1
);
2274 isl_multi_aff_free(maff2
);
2278 /* Given two multi-affine expressions A -> B and C -> D,
2279 * construct a multi-affine expression [A -> C] -> [B -> D].
2281 __isl_give isl_multi_aff
*isl_multi_aff_product(
2282 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
2288 int in1
, in2
, out1
, out2
;
2290 in1
= isl_multi_aff_dim(ma1
, isl_dim_in
);
2291 in2
= isl_multi_aff_dim(ma2
, isl_dim_in
);
2292 out1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
2293 out2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
2294 space
= isl_space_product(isl_multi_aff_get_space(ma1
),
2295 isl_multi_aff_get_space(ma2
));
2296 res
= isl_multi_aff_alloc(isl_space_copy(space
));
2297 space
= isl_space_domain(space
);
2299 for (i
= 0; i
< out1
; ++i
) {
2300 aff
= isl_multi_aff_get_aff(ma1
, i
);
2301 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, in1
, in2
);
2302 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2303 res
= isl_multi_aff_set_aff(res
, i
, aff
);
2306 for (i
= 0; i
< out2
; ++i
) {
2307 aff
= isl_multi_aff_get_aff(ma2
, i
);
2308 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, 0, in1
);
2309 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2310 res
= isl_multi_aff_set_aff(res
, out1
+ i
, aff
);
2313 isl_space_free(space
);
2314 isl_multi_aff_free(ma1
);
2315 isl_multi_aff_free(ma2
);
2319 /* Exploit the equalities in "eq" to simplify the affine expressions.
2321 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2322 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2326 maff
= isl_multi_aff_cow(maff
);
2330 for (i
= 0; i
< maff
->n
; ++i
) {
2331 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2332 isl_basic_set_copy(eq
));
2337 isl_basic_set_free(eq
);
2340 isl_basic_set_free(eq
);
2341 isl_multi_aff_free(maff
);
2345 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2350 maff
= isl_multi_aff_cow(maff
);
2354 for (i
= 0; i
< maff
->n
; ++i
) {
2355 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2357 return isl_multi_aff_free(maff
);
2363 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2364 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2366 maff1
= isl_multi_aff_add(maff1
, maff2
);
2367 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2371 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2379 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2380 __isl_keep isl_multi_aff
*maff2
)
2385 if (!maff1
|| !maff2
)
2387 if (maff1
->n
!= maff2
->n
)
2389 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2390 if (equal
< 0 || !equal
)
2393 for (i
= 0; i
< maff1
->n
; ++i
) {
2394 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2395 if (equal
< 0 || !equal
)
2402 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2403 __isl_take isl_multi_aff
*maff
,
2404 enum isl_dim_type type
, unsigned pos
, const char *s
)
2408 maff
= isl_multi_aff_cow(maff
);
2412 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2414 return isl_multi_aff_free(maff
);
2416 if (type
== isl_dim_out
)
2418 for (i
= 0; i
< maff
->n
; ++i
) {
2419 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2421 return isl_multi_aff_free(maff
);
2427 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2428 enum isl_dim_type type
, unsigned first
, unsigned n
)
2432 maff
= isl_multi_aff_cow(maff
);
2436 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2438 return isl_multi_aff_free(maff
);
2440 if (type
== isl_dim_out
) {
2441 for (i
= 0; i
< n
; ++i
)
2442 isl_aff_free(maff
->p
[first
+ i
]);
2443 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2444 maff
->p
[i
] = maff
->p
[i
+ n
];
2449 for (i
= 0; i
< maff
->n
; ++i
) {
2450 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2452 return isl_multi_aff_free(maff
);
2458 /* Return the set of domain elements where "ma1" is lexicographically
2459 * smaller than or equal to "ma2".
2461 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
2462 __isl_take isl_multi_aff
*ma2
)
2464 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
2467 /* Return the set of domain elements where "ma1" is lexicographically
2468 * greater than or equal to "ma2".
2470 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
2471 __isl_take isl_multi_aff
*ma2
)
2474 isl_map
*map1
, *map2
;
2477 map1
= isl_map_from_multi_aff(ma1
);
2478 map2
= isl_map_from_multi_aff(ma2
);
2479 map
= isl_map_range_product(map1
, map2
);
2480 space
= isl_space_range(isl_map_get_space(map
));
2481 space
= isl_space_domain(isl_space_unwrap(space
));
2482 ge
= isl_map_lex_ge(space
);
2483 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
2485 return isl_map_domain(map
);
2489 #define PW isl_pw_multi_aff
2491 #define EL isl_multi_aff
2493 #define EL_IS_ZERO is_empty
2497 #define IS_ZERO is_empty
2500 #undef DEFAULT_IS_ZERO
2501 #define DEFAULT_IS_ZERO 0
2506 #define NO_INVOLVES_DIMS
2507 #define NO_MOVE_DIMS
2508 #define NO_INSERT_DIMS
2512 #include <isl_pw_templ.c>
2515 #define UNION isl_union_pw_multi_aff
2517 #define PART isl_pw_multi_aff
2519 #define PARTS pw_multi_aff
2520 #define ALIGN_DOMAIN
2524 #include <isl_union_templ.c>
2526 /* Given a function "cmp" that returns the set of elements where
2527 * "ma1" is "better" than "ma2", return the intersection of this
2528 * set with "dom1" and "dom2".
2530 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
2531 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
2532 __isl_keep isl_multi_aff
*ma2
,
2533 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2534 __isl_take isl_multi_aff
*ma2
))
2540 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
2541 is_empty
= isl_set_plain_is_empty(common
);
2542 if (is_empty
>= 0 && is_empty
)
2545 return isl_set_free(common
);
2546 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
2547 better
= isl_set_intersect(common
, better
);
2552 /* Given a function "cmp" that returns the set of elements where
2553 * "ma1" is "better" than "ma2", return a piecewise multi affine
2554 * expression defined on the union of the definition domains
2555 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2556 * "pma2" on each cell. If only one of the two input functions
2557 * is defined on a given cell, then it is considered the best.
2559 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
2560 __isl_take isl_pw_multi_aff
*pma1
,
2561 __isl_take isl_pw_multi_aff
*pma2
,
2562 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2563 __isl_take isl_multi_aff
*ma2
))
2566 isl_pw_multi_aff
*res
= NULL
;
2568 isl_set
*set
= NULL
;
2573 ctx
= isl_space_get_ctx(pma1
->dim
);
2574 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
2575 isl_die(ctx
, isl_error_invalid
,
2576 "arguments should live in the same space", goto error
);
2578 if (isl_pw_multi_aff_is_empty(pma1
)) {
2579 isl_pw_multi_aff_free(pma1
);
2583 if (isl_pw_multi_aff_is_empty(pma2
)) {
2584 isl_pw_multi_aff_free(pma2
);
2588 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
2589 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
2591 for (i
= 0; i
< pma1
->n
; ++i
) {
2592 set
= isl_set_copy(pma1
->p
[i
].set
);
2593 for (j
= 0; j
< pma2
->n
; ++j
) {
2597 better
= shared_and_better(pma2
->p
[j
].set
,
2598 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
2599 pma1
->p
[i
].maff
, cmp
);
2600 is_empty
= isl_set_plain_is_empty(better
);
2601 if (is_empty
< 0 || is_empty
) {
2602 isl_set_free(better
);
2607 set
= isl_set_subtract(set
, isl_set_copy(better
));
2609 res
= isl_pw_multi_aff_add_piece(res
, better
,
2610 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2612 res
= isl_pw_multi_aff_add_piece(res
, set
,
2613 isl_multi_aff_copy(pma1
->p
[i
].maff
));
2616 for (j
= 0; j
< pma2
->n
; ++j
) {
2617 set
= isl_set_copy(pma2
->p
[j
].set
);
2618 for (i
= 0; i
< pma1
->n
; ++i
)
2619 set
= isl_set_subtract(set
,
2620 isl_set_copy(pma1
->p
[i
].set
));
2621 res
= isl_pw_multi_aff_add_piece(res
, set
,
2622 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2625 isl_pw_multi_aff_free(pma1
);
2626 isl_pw_multi_aff_free(pma2
);
2630 isl_pw_multi_aff_free(pma1
);
2631 isl_pw_multi_aff_free(pma2
);
2633 return isl_pw_multi_aff_free(res
);
2636 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
2637 __isl_take isl_pw_multi_aff
*pma1
,
2638 __isl_take isl_pw_multi_aff
*pma2
)
2640 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
2643 /* Given two piecewise multi affine expressions, return a piecewise
2644 * multi-affine expression defined on the union of the definition domains
2645 * of the inputs that is equal to the lexicographic maximum of the two
2646 * inputs on each cell. If only one of the two inputs is defined on
2647 * a given cell, then it is considered to be the maximum.
2649 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
2650 __isl_take isl_pw_multi_aff
*pma1
,
2651 __isl_take isl_pw_multi_aff
*pma2
)
2653 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2654 &pw_multi_aff_union_lexmax
);
2657 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
2658 __isl_take isl_pw_multi_aff
*pma1
,
2659 __isl_take isl_pw_multi_aff
*pma2
)
2661 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
2664 /* Given two piecewise multi affine expressions, return a piecewise
2665 * multi-affine expression defined on the union of the definition domains
2666 * of the inputs that is equal to the lexicographic minimum of the two
2667 * inputs on each cell. If only one of the two inputs is defined on
2668 * a given cell, then it is considered to be the minimum.
2670 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
2671 __isl_take isl_pw_multi_aff
*pma1
,
2672 __isl_take isl_pw_multi_aff
*pma2
)
2674 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2675 &pw_multi_aff_union_lexmin
);
2678 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2679 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2681 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2682 &isl_multi_aff_add
);
2685 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2686 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2688 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2692 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2693 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2695 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2698 /* Given two piecewise multi-affine expressions A -> B and C -> D,
2699 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
2701 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
2702 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2706 isl_pw_multi_aff
*res
;
2711 n
= pma1
->n
* pma2
->n
;
2712 space
= isl_space_product(isl_space_copy(pma1
->dim
),
2713 isl_space_copy(pma2
->dim
));
2714 res
= isl_pw_multi_aff_alloc_size(space
, n
);
2716 for (i
= 0; i
< pma1
->n
; ++i
) {
2717 for (j
= 0; j
< pma2
->n
; ++j
) {
2721 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
2722 isl_set_copy(pma2
->p
[j
].set
));
2723 ma
= isl_multi_aff_product(
2724 isl_multi_aff_copy(pma1
->p
[i
].maff
),
2725 isl_multi_aff_copy(pma2
->p
[i
].maff
));
2726 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
2730 isl_pw_multi_aff_free(pma1
);
2731 isl_pw_multi_aff_free(pma2
);
2734 isl_pw_multi_aff_free(pma1
);
2735 isl_pw_multi_aff_free(pma2
);
2739 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
2740 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2742 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2743 &pw_multi_aff_product
);
2746 /* Construct a map mapping the domain of the piecewise multi-affine expression
2747 * to its range, with each dimension in the range equated to the
2748 * corresponding affine expression on its cell.
2750 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2758 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2760 for (i
= 0; i
< pma
->n
; ++i
) {
2761 isl_multi_aff
*maff
;
2762 isl_basic_map
*bmap
;
2765 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2766 bmap
= isl_basic_map_from_multi_aff(maff
);
2767 map_i
= isl_map_from_basic_map(bmap
);
2768 map_i
= isl_map_intersect_domain(map_i
,
2769 isl_set_copy(pma
->p
[i
].set
));
2770 map
= isl_map_union_disjoint(map
, map_i
);
2773 isl_pw_multi_aff_free(pma
);
2777 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2779 if (!isl_space_is_set(pma
->dim
))
2780 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2781 "isl_pw_multi_aff cannot be converted into an isl_set",
2782 return isl_pw_multi_aff_free(pma
));
2784 return isl_map_from_pw_multi_aff(pma
);
2787 /* Given a basic map with a single output dimension that is defined
2788 * in terms of the parameters and input dimensions using an equality,
2789 * extract an isl_aff that expresses the output dimension in terms
2790 * of the parameters and input dimensions.
2792 * Since some applications expect the result of isl_pw_multi_aff_from_map
2793 * to only contain integer affine expressions, we compute the floor
2794 * of the expression before returning.
2796 * This function shares some similarities with
2797 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2799 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
2800 __isl_take isl_basic_map
*bmap
)
2805 isl_local_space
*ls
;
2810 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
2811 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2812 "basic map should have a single output dimension",
2814 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
2815 total
= isl_basic_map_total_dim(bmap
);
2816 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2817 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
2819 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
2820 1 + total
- (offset
+ 1)) != -1)
2824 if (i
>= bmap
->n_eq
)
2825 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2826 "unable to find suitable equality", goto error
);
2827 ls
= isl_basic_map_get_local_space(bmap
);
2828 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
2831 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
2832 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2834 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2835 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
2836 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
2837 isl_basic_map_free(bmap
);
2839 aff
= isl_aff_remove_unused_divs(aff
);
2840 aff
= isl_aff_floor(aff
);
2843 isl_basic_map_free(bmap
);
2847 /* Given a basic map where each output dimension is defined
2848 * in terms of the parameters and input dimensions using an equality,
2849 * extract an isl_multi_aff that expresses the output dimensions in terms
2850 * of the parameters and input dimensions.
2852 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
2853 __isl_take isl_basic_map
*bmap
)
2862 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
2863 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2865 for (i
= 0; i
< n_out
; ++i
) {
2866 isl_basic_map
*bmap_i
;
2869 bmap_i
= isl_basic_map_copy(bmap
);
2870 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
2871 i
+ 1, n_out
- (1 + i
));
2872 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
2873 aff
= extract_isl_aff_from_basic_map(bmap_i
);
2874 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2877 isl_basic_map_free(bmap
);
2882 /* Create an isl_pw_multi_aff that is equivalent to
2883 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2884 * The given basic map is such that each output dimension is defined
2885 * in terms of the parameters and input dimensions using an equality.
2887 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
2888 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
2892 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
2893 return isl_pw_multi_aff_alloc(domain
, ma
);
2896 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2897 * This obivously only works if the input "map" is single-valued.
2898 * If so, we compute the lexicographic minimum of the image in the form
2899 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2900 * to its lexicographic minimum.
2901 * If the input is not single-valued, we produce an error.
2903 * As a special case, we first check if all output dimensions are uniquely
2904 * defined in terms of the parameters and input dimensions over the entire
2905 * domain. If so, we extract the desired isl_pw_multi_aff directly
2906 * from the affine hull of "map" and its domain.
2908 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2912 isl_pw_multi_aff
*pma
;
2913 isl_basic_map
*hull
;
2918 hull
= isl_map_affine_hull(isl_map_copy(map
));
2919 sv
= isl_basic_map_plain_is_single_valued(hull
);
2921 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
2922 isl_basic_map_free(hull
);
2926 sv
= isl_map_is_single_valued(map
);
2930 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2931 "map is not single-valued", goto error
);
2932 map
= isl_map_make_disjoint(map
);
2936 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2938 for (i
= 0; i
< map
->n
; ++i
) {
2939 isl_pw_multi_aff
*pma_i
;
2940 isl_basic_map
*bmap
;
2941 bmap
= isl_basic_map_copy(map
->p
[i
]);
2942 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2943 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2953 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2955 return isl_pw_multi_aff_from_map(set
);
2958 /* Return the piecewise affine expression "set ? 1 : 0".
2960 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
2963 isl_space
*space
= isl_set_get_space(set
);
2964 isl_local_space
*ls
= isl_local_space_from_space(space
);
2965 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
2966 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
2968 one
= isl_aff_add_constant_si(one
, 1);
2969 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
2970 set
= isl_set_complement(set
);
2971 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
2976 /* Plug in "subs" for dimension "type", "pos" of "aff".
2978 * Let i be the dimension to replace and let "subs" be of the form
2982 * and "aff" of the form
2988 * (a f + d g')/(m d)
2990 * where g' is the result of plugging in "subs" in each of the integer
2993 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2994 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2999 aff
= isl_aff_cow(aff
);
3001 return isl_aff_free(aff
);
3003 ctx
= isl_aff_get_ctx(aff
);
3004 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
3005 isl_die(ctx
, isl_error_invalid
,
3006 "spaces don't match", return isl_aff_free(aff
));
3007 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
3008 isl_die(ctx
, isl_error_unsupported
,
3009 "cannot handle divs yet", return isl_aff_free(aff
));
3011 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
3013 return isl_aff_free(aff
);
3015 aff
->v
= isl_vec_cow(aff
->v
);
3017 return isl_aff_free(aff
);
3019 pos
+= isl_local_space_offset(aff
->ls
, type
);
3022 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
3023 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
3024 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
3025 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
3026 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
3032 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3033 * expressions in "maff".
3035 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
3036 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
3037 __isl_keep isl_aff
*subs
)
3041 maff
= isl_multi_aff_cow(maff
);
3043 return isl_multi_aff_free(maff
);
3045 if (type
== isl_dim_in
)
3048 for (i
= 0; i
< maff
->n
; ++i
) {
3049 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
3051 return isl_multi_aff_free(maff
);
3057 /* Plug in "subs" for dimension "type", "pos" of "pma".
3059 * pma is of the form
3063 * while subs is of the form
3065 * v' = B_j(v) -> S_j
3067 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3068 * has a contribution in the result, in particular
3070 * C_ij(S_j) -> M_i(S_j)
3072 * Note that plugging in S_j in C_ij may also result in an empty set
3073 * and this contribution should simply be discarded.
3075 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
3076 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
3077 __isl_keep isl_pw_aff
*subs
)
3080 isl_pw_multi_aff
*res
;
3083 return isl_pw_multi_aff_free(pma
);
3085 n
= pma
->n
* subs
->n
;
3086 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
3088 for (i
= 0; i
< pma
->n
; ++i
) {
3089 for (j
= 0; j
< subs
->n
; ++j
) {
3091 isl_multi_aff
*res_ij
;
3092 common
= isl_set_intersect(
3093 isl_set_copy(pma
->p
[i
].set
),
3094 isl_set_copy(subs
->p
[j
].set
));
3095 common
= isl_set_substitute(common
,
3096 type
, pos
, subs
->p
[j
].aff
);
3097 if (isl_set_plain_is_empty(common
)) {
3098 isl_set_free(common
);
3102 res_ij
= isl_multi_aff_substitute(
3103 isl_multi_aff_copy(pma
->p
[i
].maff
),
3104 type
, pos
, subs
->p
[j
].aff
);
3106 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
3110 isl_pw_multi_aff_free(pma
);
3114 /* Extend the local space of "dst" to include the divs
3115 * in the local space of "src".
3117 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
3118 __isl_keep isl_aff
*src
)
3126 return isl_aff_free(dst
);
3128 ctx
= isl_aff_get_ctx(src
);
3129 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
3130 isl_die(ctx
, isl_error_invalid
,
3131 "spaces don't match", goto error
);
3133 if (src
->ls
->div
->n_row
== 0)
3136 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
3137 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
3141 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
3142 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
3150 return isl_aff_free(dst
);
3153 /* Adjust the local spaces of the affine expressions in "maff"
3154 * such that they all have the save divs.
3156 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
3157 __isl_take isl_multi_aff
*maff
)
3165 maff
= isl_multi_aff_cow(maff
);
3169 for (i
= 1; i
< maff
->n
; ++i
)
3170 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
3171 for (i
= 1; i
< maff
->n
; ++i
) {
3172 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
3174 return isl_multi_aff_free(maff
);
3180 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
3182 aff
= isl_aff_cow(aff
);
3186 aff
->ls
= isl_local_space_lift(aff
->ls
);
3188 return isl_aff_free(aff
);
3193 /* Lift "maff" to a space with extra dimensions such that the result
3194 * has no more existentially quantified variables.
3195 * If "ls" is not NULL, then *ls is assigned the local space that lies
3196 * at the basis of the lifting applied to "maff".
3198 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
3199 __isl_give isl_local_space
**ls
)
3213 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
3214 *ls
= isl_local_space_from_space(space
);
3216 return isl_multi_aff_free(maff
);
3221 maff
= isl_multi_aff_cow(maff
);
3222 maff
= isl_multi_aff_align_divs(maff
);
3226 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
3227 space
= isl_multi_aff_get_space(maff
);
3228 space
= isl_space_lift(isl_space_domain(space
), n_div
);
3229 space
= isl_space_extend_domain_with_range(space
,
3230 isl_multi_aff_get_space(maff
));
3232 return isl_multi_aff_free(maff
);
3233 isl_space_free(maff
->space
);
3234 maff
->space
= space
;
3237 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
3239 return isl_multi_aff_free(maff
);
3242 for (i
= 0; i
< maff
->n
; ++i
) {
3243 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
3251 isl_local_space_free(*ls
);
3252 return isl_multi_aff_free(maff
);
3256 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
3258 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
3259 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
3269 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
3270 if (pos
< 0 || pos
>= n_out
)
3271 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3272 "index out of bounds", return NULL
);
3274 space
= isl_pw_multi_aff_get_space(pma
);
3275 space
= isl_space_drop_dims(space
, isl_dim_out
,
3276 pos
+ 1, n_out
- pos
- 1);
3277 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
3279 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
3280 for (i
= 0; i
< pma
->n
; ++i
) {
3282 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
3283 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
3289 /* Return an isl_pw_multi_aff with the given "set" as domain and
3290 * an unnamed zero-dimensional range.
3292 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
3293 __isl_take isl_set
*set
)
3298 space
= isl_set_get_space(set
);
3299 space
= isl_space_from_domain(space
);
3300 ma
= isl_multi_aff_zero(space
);
3301 return isl_pw_multi_aff_alloc(set
, ma
);
3304 /* Add an isl_pw_multi_aff with the given "set" as domain and
3305 * an unnamed zero-dimensional range to *user.
3307 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
3309 isl_union_pw_multi_aff
**upma
= user
;
3310 isl_pw_multi_aff
*pma
;
3312 pma
= isl_pw_multi_aff_from_domain(set
);
3313 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
3318 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
3319 * an unnamed zero-dimensional range.
3321 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
3322 __isl_take isl_union_set
*uset
)
3325 isl_union_pw_multi_aff
*upma
;
3330 space
= isl_union_set_get_space(uset
);
3331 upma
= isl_union_pw_multi_aff_empty(space
);
3333 if (isl_union_set_foreach_set(uset
,
3334 &add_pw_multi_aff_from_domain
, &upma
) < 0)
3337 isl_union_set_free(uset
);
3340 isl_union_set_free(uset
);
3341 isl_union_pw_multi_aff_free(upma
);
3345 /* Convert "pma" to an isl_map and add it to *umap.
3347 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3349 isl_union_map
**umap
= user
;
3352 map
= isl_map_from_pw_multi_aff(pma
);
3353 *umap
= isl_union_map_add_map(*umap
, map
);
3358 /* Construct a union map mapping the domain of the union
3359 * piecewise multi-affine expression to its range, with each dimension
3360 * in the range equated to the corresponding affine expression on its cell.
3362 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
3363 __isl_take isl_union_pw_multi_aff
*upma
)
3366 isl_union_map
*umap
;
3371 space
= isl_union_pw_multi_aff_get_space(upma
);
3372 umap
= isl_union_map_empty(space
);
3374 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3375 &map_from_pw_multi_aff
, &umap
) < 0)
3378 isl_union_pw_multi_aff_free(upma
);
3381 isl_union_pw_multi_aff_free(upma
);
3382 isl_union_map_free(umap
);
3386 /* Local data for bin_entry and the callback "fn".
3388 struct isl_union_pw_multi_aff_bin_data
{
3389 isl_union_pw_multi_aff
*upma2
;
3390 isl_union_pw_multi_aff
*res
;
3391 isl_pw_multi_aff
*pma
;
3392 int (*fn
)(void **entry
, void *user
);
3395 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3396 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3398 static int bin_entry(void **entry
, void *user
)
3400 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3401 isl_pw_multi_aff
*pma
= *entry
;
3404 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
3405 data
->fn
, data
) < 0)
3411 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3412 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3413 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3414 * as *entry. The callback should adjust data->res if desired.
3416 static __isl_give isl_union_pw_multi_aff
*bin_op(
3417 __isl_take isl_union_pw_multi_aff
*upma1
,
3418 __isl_take isl_union_pw_multi_aff
*upma2
,
3419 int (*fn
)(void **entry
, void *user
))
3422 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
3424 space
= isl_union_pw_multi_aff_get_space(upma2
);
3425 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
3426 space
= isl_union_pw_multi_aff_get_space(upma1
);
3427 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
3429 if (!upma1
|| !upma2
)
3433 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
3435 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
3436 &bin_entry
, &data
) < 0)
3439 isl_union_pw_multi_aff_free(upma1
);
3440 isl_union_pw_multi_aff_free(upma2
);
3443 isl_union_pw_multi_aff_free(upma1
);
3444 isl_union_pw_multi_aff_free(upma2
);
3445 isl_union_pw_multi_aff_free(data
.res
);
3449 /* Given two isl_multi_affs A -> B and C -> D,
3450 * construct an isl_multi_aff (A * C) -> (B, D).
3452 __isl_give isl_multi_aff
*isl_multi_aff_flat_range_product(
3453 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3463 space
= isl_space_range_product(isl_multi_aff_get_space(ma1
),
3464 isl_multi_aff_get_space(ma2
));
3465 space
= isl_space_flatten_range(space
);
3466 res
= isl_multi_aff_alloc(space
);
3468 n1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
3469 n2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
3471 for (i
= 0; i
< n1
; ++i
) {
3472 aff
= isl_multi_aff_get_aff(ma1
, i
);
3473 res
= isl_multi_aff_set_aff(res
, i
, aff
);
3476 for (i
= 0; i
< n2
; ++i
) {
3477 aff
= isl_multi_aff_get_aff(ma2
, i
);
3478 res
= isl_multi_aff_set_aff(res
, n1
+ i
, aff
);
3481 isl_multi_aff_free(ma1
);
3482 isl_multi_aff_free(ma2
);
3485 isl_multi_aff_free(ma1
);
3486 isl_multi_aff_free(ma2
);
3490 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3491 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3493 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
3494 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3498 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
3499 isl_pw_multi_aff_get_space(pma2
));
3500 space
= isl_space_flatten_range(space
);
3501 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
3502 &isl_multi_aff_flat_range_product
);
3505 /* Given two isl_pw_multi_affs A -> B and C -> D,
3506 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3508 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
3509 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3511 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3512 &pw_multi_aff_flat_range_product
);
3515 /* If data->pma and *entry have the same domain space, then compute
3516 * their flat range product and the result to data->res.
3518 static int flat_range_product_entry(void **entry
, void *user
)
3520 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3521 isl_pw_multi_aff
*pma2
= *entry
;
3523 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
3524 pma2
->dim
, isl_dim_in
))
3527 pma2
= isl_pw_multi_aff_flat_range_product(
3528 isl_pw_multi_aff_copy(data
->pma
),
3529 isl_pw_multi_aff_copy(pma2
));
3531 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
3536 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3537 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3539 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
3540 __isl_take isl_union_pw_multi_aff
*upma1
,
3541 __isl_take isl_union_pw_multi_aff
*upma2
)
3543 return bin_op(upma1
, upma2
, &flat_range_product_entry
);