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 /* 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 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
655 aff
->v
= isl_vec_normalize(aff
->v
);
657 return isl_aff_free(aff
);
658 aff
= isl_aff_remove_unused_divs(aff
);
662 /* Given f, return floor(f).
663 * If f is an integer expression, then just return f.
664 * If f is a constant, then return the constant floor(f).
665 * Otherwise, if f = g/m, write g = q m + r,
666 * create a new div d = [r/m] and return the expression q + d.
667 * The coefficients in r are taken to lie between -m/2 and m/2.
669 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
679 if (isl_int_is_one(aff
->v
->el
[0]))
682 aff
= isl_aff_cow(aff
);
686 aff
->v
= isl_vec_cow(aff
->v
);
688 return isl_aff_free(aff
);
690 if (isl_aff_is_cst(aff
)) {
691 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
692 isl_int_set_si(aff
->v
->el
[0], 1);
696 div
= isl_vec_copy(aff
->v
);
697 div
= isl_vec_cow(div
);
699 return isl_aff_free(aff
);
701 ctx
= isl_aff_get_ctx(aff
);
702 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
703 for (i
= 1; i
< aff
->v
->size
; ++i
) {
704 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
705 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
706 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
707 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
708 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
712 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
714 return isl_aff_free(aff
);
717 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
719 return isl_aff_free(aff
);
720 isl_int_set_si(aff
->v
->el
[0], 1);
721 isl_int_set_si(aff
->v
->el
[size
], 1);
728 * aff mod m = aff - m * floor(aff/m)
730 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
734 res
= isl_aff_copy(aff
);
735 aff
= isl_aff_scale_down(aff
, m
);
736 aff
= isl_aff_floor(aff
);
737 aff
= isl_aff_scale(aff
, m
);
738 res
= isl_aff_sub(res
, aff
);
745 * pwaff mod m = pwaff - m * floor(pwaff/m)
747 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
751 res
= isl_pw_aff_copy(pwaff
);
752 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
753 pwaff
= isl_pw_aff_floor(pwaff
);
754 pwaff
= isl_pw_aff_scale(pwaff
, m
);
755 res
= isl_pw_aff_sub(res
, pwaff
);
760 /* Given f, return ceil(f).
761 * If f is an integer expression, then just return f.
762 * Otherwise, create a new div d = [-f] and return the expression -d.
764 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
769 if (isl_int_is_one(aff
->v
->el
[0]))
772 aff
= isl_aff_neg(aff
);
773 aff
= isl_aff_floor(aff
);
774 aff
= isl_aff_neg(aff
);
779 /* Apply the expansion computed by isl_merge_divs.
780 * The expansion itself is given by "exp" while the resulting
781 * list of divs is given by "div".
783 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
784 __isl_take isl_mat
*div
, int *exp
)
791 aff
= isl_aff_cow(aff
);
795 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
796 new_n_div
= isl_mat_rows(div
);
797 if (new_n_div
< old_n_div
)
798 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
799 "not an expansion", goto error
);
801 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
805 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
807 for (i
= new_n_div
- 1; i
>= 0; --i
) {
808 if (j
>= 0 && exp
[j
] == i
) {
810 isl_int_swap(aff
->v
->el
[offset
+ i
],
811 aff
->v
->el
[offset
+ j
]);
814 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
817 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
828 /* Add two affine expressions that live in the same local space.
830 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
831 __isl_take isl_aff
*aff2
)
835 aff1
= isl_aff_cow(aff1
);
839 aff1
->v
= isl_vec_cow(aff1
->v
);
845 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
846 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
847 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
848 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
849 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
850 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
851 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
863 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
864 __isl_take isl_aff
*aff2
)
874 ctx
= isl_aff_get_ctx(aff1
);
875 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
876 isl_die(ctx
, isl_error_invalid
,
877 "spaces don't match", goto error
);
879 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
880 return add_expanded(aff1
, aff2
);
882 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
883 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
887 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
888 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
889 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
893 return add_expanded(aff1
, aff2
);
902 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
903 __isl_take isl_aff
*aff2
)
905 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
908 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
912 if (isl_int_is_one(f
))
915 aff
= isl_aff_cow(aff
);
918 aff
->v
= isl_vec_cow(aff
->v
);
920 return isl_aff_free(aff
);
923 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
924 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
925 isl_int_divexact(gcd
, f
, gcd
);
926 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
932 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
936 if (isl_int_is_one(f
))
939 aff
= isl_aff_cow(aff
);
942 aff
->v
= isl_vec_cow(aff
->v
);
944 return isl_aff_free(aff
);
947 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
948 isl_int_gcd(gcd
, gcd
, f
);
949 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
950 isl_int_divexact(gcd
, f
, gcd
);
951 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
957 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
965 isl_int_set_ui(v
, f
);
966 aff
= isl_aff_scale_down(aff
, v
);
972 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
973 enum isl_dim_type type
, unsigned pos
, const char *s
)
975 aff
= isl_aff_cow(aff
);
978 if (type
== isl_dim_out
)
979 isl_die(aff
->v
->ctx
, isl_error_invalid
,
980 "cannot set name of output/set dimension",
981 return isl_aff_free(aff
));
982 if (type
== isl_dim_in
)
984 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
986 return isl_aff_free(aff
);
991 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
992 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
994 aff
= isl_aff_cow(aff
);
996 return isl_id_free(id
);
997 if (type
== isl_dim_out
)
998 isl_die(aff
->v
->ctx
, isl_error_invalid
,
999 "cannot set name of output/set dimension",
1001 if (type
== isl_dim_in
)
1003 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1005 return isl_aff_free(aff
);
1014 /* Exploit the equalities in "eq" to simplify the affine expression
1015 * and the expressions of the integer divisions in the local space.
1016 * The integer divisions in this local space are assumed to appear
1017 * as regular dimensions in "eq".
1019 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1020 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1028 if (eq
->n_eq
== 0) {
1029 isl_basic_set_free(eq
);
1033 aff
= isl_aff_cow(aff
);
1037 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1038 isl_basic_set_copy(eq
));
1042 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1044 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1045 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1046 if (j
< 0 || j
== 0 || j
>= total
)
1049 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1053 isl_basic_set_free(eq
);
1054 aff
= isl_aff_normalize(aff
);
1057 isl_basic_set_free(eq
);
1062 /* Exploit the equalities in "eq" to simplify the affine expression
1063 * and the expressions of the integer divisions in the local space.
1065 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1066 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1072 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1074 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
1075 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1077 isl_basic_set_free(eq
);
1082 /* Look for equalities among the variables shared by context and aff
1083 * and the integer divisions of aff, if any.
1084 * The equalities are then used to eliminate coefficients and/or integer
1085 * divisions from aff.
1087 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1088 __isl_take isl_set
*context
)
1090 isl_basic_set
*hull
;
1095 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1097 isl_basic_set
*bset
;
1098 isl_local_space
*ls
;
1099 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1100 ls
= isl_aff_get_domain_local_space(aff
);
1101 bset
= isl_basic_set_from_local_space(ls
);
1102 bset
= isl_basic_set_lift(bset
);
1103 bset
= isl_basic_set_flatten(bset
);
1104 context
= isl_set_intersect(context
,
1105 isl_set_from_basic_set(bset
));
1108 hull
= isl_set_affine_hull(context
);
1109 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1112 isl_set_free(context
);
1116 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1117 __isl_take isl_set
*context
)
1119 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1120 dom_context
= isl_set_intersect_params(dom_context
, context
);
1121 return isl_aff_gist(aff
, dom_context
);
1124 /* Return a basic set containing those elements in the space
1125 * of aff where it is non-negative.
1127 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1129 isl_constraint
*ineq
;
1130 isl_basic_set
*bset
;
1132 ineq
= isl_inequality_from_aff(aff
);
1134 bset
= isl_basic_set_from_constraint(ineq
);
1135 bset
= isl_basic_set_simplify(bset
);
1139 /* Return a basic set containing those elements in the domain space
1140 * of aff where it is negative.
1142 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
1144 aff
= isl_aff_neg(aff
);
1145 aff
= isl_aff_add_constant_num_si(aff
, -1);
1146 return isl_aff_nonneg_basic_set(aff
);
1149 /* Return a basic set containing those elements in the space
1150 * of aff where it is zero.
1152 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1154 isl_constraint
*ineq
;
1155 isl_basic_set
*bset
;
1157 ineq
= isl_equality_from_aff(aff
);
1159 bset
= isl_basic_set_from_constraint(ineq
);
1160 bset
= isl_basic_set_simplify(bset
);
1164 /* Return a basic set containing those elements in the shared space
1165 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1167 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1168 __isl_take isl_aff
*aff2
)
1170 aff1
= isl_aff_sub(aff1
, aff2
);
1172 return isl_aff_nonneg_basic_set(aff1
);
1175 /* Return a basic set containing those elements in the shared space
1176 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1178 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1179 __isl_take isl_aff
*aff2
)
1181 return isl_aff_ge_basic_set(aff2
, aff1
);
1184 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1185 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1187 aff1
= isl_aff_add(aff1
, aff2
);
1188 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1192 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1200 /* Check whether the given affine expression has non-zero coefficient
1201 * for any dimension in the given range or if any of these dimensions
1202 * appear with non-zero coefficients in any of the integer divisions
1203 * involved in the affine expression.
1205 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1206 enum isl_dim_type type
, unsigned first
, unsigned n
)
1218 ctx
= isl_aff_get_ctx(aff
);
1219 if (first
+ n
> isl_aff_dim(aff
, type
))
1220 isl_die(ctx
, isl_error_invalid
,
1221 "range out of bounds", return -1);
1223 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1227 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1228 for (i
= 0; i
< n
; ++i
)
1229 if (active
[first
+ i
]) {
1242 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1243 enum isl_dim_type type
, unsigned first
, unsigned n
)
1249 if (type
== isl_dim_out
)
1250 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1251 "cannot drop output/set dimension",
1252 return isl_aff_free(aff
));
1253 if (type
== isl_dim_in
)
1255 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1258 ctx
= isl_aff_get_ctx(aff
);
1259 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1260 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1261 return isl_aff_free(aff
));
1263 aff
= isl_aff_cow(aff
);
1267 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1269 return isl_aff_free(aff
);
1271 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1272 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1274 return isl_aff_free(aff
);
1279 /* Project the domain of the affine expression onto its parameter space.
1280 * The affine expression may not involve any of the domain dimensions.
1282 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
1288 n
= isl_aff_dim(aff
, isl_dim_in
);
1289 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
1291 return isl_aff_free(aff
);
1293 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1294 "affine expression involves some of the domain dimensions",
1295 return isl_aff_free(aff
));
1296 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
1297 space
= isl_aff_get_domain_space(aff
);
1298 space
= isl_space_params(space
);
1299 aff
= isl_aff_reset_domain_space(aff
, space
);
1303 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1304 enum isl_dim_type type
, unsigned first
, unsigned n
)
1310 if (type
== isl_dim_out
)
1311 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1312 "cannot insert output/set dimensions",
1313 return isl_aff_free(aff
));
1314 if (type
== isl_dim_in
)
1316 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1319 ctx
= isl_aff_get_ctx(aff
);
1320 if (first
> isl_local_space_dim(aff
->ls
, type
))
1321 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1322 return isl_aff_free(aff
));
1324 aff
= isl_aff_cow(aff
);
1328 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1330 return isl_aff_free(aff
);
1332 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1333 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1335 return isl_aff_free(aff
);
1340 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1341 enum isl_dim_type type
, unsigned n
)
1345 pos
= isl_aff_dim(aff
, type
);
1347 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1350 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1351 enum isl_dim_type type
, unsigned n
)
1355 pos
= isl_pw_aff_dim(pwaff
, type
);
1357 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1360 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1362 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1363 return isl_pw_aff_alloc(dom
, aff
);
1367 #define PW isl_pw_aff
1371 #define EL_IS_ZERO is_empty
1375 #define IS_ZERO is_empty
1378 #undef DEFAULT_IS_ZERO
1379 #define DEFAULT_IS_ZERO 0
1383 #define NO_MOVE_DIMS
1387 #include <isl_pw_templ.c>
1389 static __isl_give isl_set
*align_params_pw_pw_set_and(
1390 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1391 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1392 __isl_take isl_pw_aff
*pwaff2
))
1394 if (!pwaff1
|| !pwaff2
)
1396 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1397 pwaff2
->dim
, isl_dim_param
))
1398 return fn(pwaff1
, pwaff2
);
1399 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1400 !isl_space_has_named_params(pwaff2
->dim
))
1401 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1402 "unaligned unnamed parameters", goto error
);
1403 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1404 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1405 return fn(pwaff1
, pwaff2
);
1407 isl_pw_aff_free(pwaff1
);
1408 isl_pw_aff_free(pwaff2
);
1412 /* Compute a piecewise quasi-affine expression with a domain that
1413 * is the union of those of pwaff1 and pwaff2 and such that on each
1414 * cell, the quasi-affine expression is the better (according to cmp)
1415 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1416 * is defined on a given cell, then the associated expression
1417 * is the defined one.
1419 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1420 __isl_take isl_pw_aff
*pwaff2
,
1421 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1422 __isl_take isl_aff
*aff2
))
1429 if (!pwaff1
|| !pwaff2
)
1432 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1433 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1434 isl_die(ctx
, isl_error_invalid
,
1435 "arguments should live in same space", goto error
);
1437 if (isl_pw_aff_is_empty(pwaff1
)) {
1438 isl_pw_aff_free(pwaff1
);
1442 if (isl_pw_aff_is_empty(pwaff2
)) {
1443 isl_pw_aff_free(pwaff2
);
1447 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1448 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1450 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1451 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1452 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1453 struct isl_set
*common
;
1456 common
= isl_set_intersect(
1457 isl_set_copy(pwaff1
->p
[i
].set
),
1458 isl_set_copy(pwaff2
->p
[j
].set
));
1459 better
= isl_set_from_basic_set(cmp(
1460 isl_aff_copy(pwaff2
->p
[j
].aff
),
1461 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1462 better
= isl_set_intersect(common
, better
);
1463 if (isl_set_plain_is_empty(better
)) {
1464 isl_set_free(better
);
1467 set
= isl_set_subtract(set
, isl_set_copy(better
));
1469 res
= isl_pw_aff_add_piece(res
, better
,
1470 isl_aff_copy(pwaff2
->p
[j
].aff
));
1472 res
= isl_pw_aff_add_piece(res
, set
,
1473 isl_aff_copy(pwaff1
->p
[i
].aff
));
1476 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1477 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1478 for (i
= 0; i
< pwaff1
->n
; ++i
)
1479 set
= isl_set_subtract(set
,
1480 isl_set_copy(pwaff1
->p
[i
].set
));
1481 res
= isl_pw_aff_add_piece(res
, set
,
1482 isl_aff_copy(pwaff2
->p
[j
].aff
));
1485 isl_pw_aff_free(pwaff1
);
1486 isl_pw_aff_free(pwaff2
);
1490 isl_pw_aff_free(pwaff1
);
1491 isl_pw_aff_free(pwaff2
);
1495 /* Compute a piecewise quasi-affine expression with a domain that
1496 * is the union of those of pwaff1 and pwaff2 and such that on each
1497 * cell, the quasi-affine expression is the maximum of those of pwaff1
1498 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1499 * cell, then the associated expression is the defined one.
1501 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1502 __isl_take isl_pw_aff
*pwaff2
)
1504 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1507 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1508 __isl_take isl_pw_aff
*pwaff2
)
1510 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1514 /* Compute a piecewise quasi-affine expression with a domain that
1515 * is the union of those of pwaff1 and pwaff2 and such that on each
1516 * cell, the quasi-affine expression is the minimum of those of pwaff1
1517 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1518 * cell, then the associated expression is the defined one.
1520 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1521 __isl_take isl_pw_aff
*pwaff2
)
1523 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1526 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1527 __isl_take isl_pw_aff
*pwaff2
)
1529 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1533 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1534 __isl_take isl_pw_aff
*pwaff2
, int max
)
1537 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1539 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1542 /* Construct a map with as domain the domain of pwaff and
1543 * one-dimensional range corresponding to the affine expressions.
1545 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1554 dim
= isl_pw_aff_get_space(pwaff
);
1555 map
= isl_map_empty(dim
);
1557 for (i
= 0; i
< pwaff
->n
; ++i
) {
1558 isl_basic_map
*bmap
;
1561 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1562 map_i
= isl_map_from_basic_map(bmap
);
1563 map_i
= isl_map_intersect_domain(map_i
,
1564 isl_set_copy(pwaff
->p
[i
].set
));
1565 map
= isl_map_union_disjoint(map
, map_i
);
1568 isl_pw_aff_free(pwaff
);
1573 /* Construct a map with as domain the domain of pwaff and
1574 * one-dimensional range corresponding to the affine expressions.
1576 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1580 if (isl_space_is_set(pwaff
->dim
))
1581 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1582 "space of input is not a map",
1583 return isl_pw_aff_free(pwaff
));
1584 return map_from_pw_aff(pwaff
);
1587 /* Construct a one-dimensional set with as parameter domain
1588 * the domain of pwaff and the single set dimension
1589 * corresponding to the affine expressions.
1591 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1595 if (!isl_space_is_set(pwaff
->dim
))
1596 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1597 "space of input is not a set",
1598 return isl_pw_aff_free(pwaff
));
1599 return map_from_pw_aff(pwaff
);
1602 /* Return a set containing those elements in the domain
1603 * of pwaff where it is non-negative.
1605 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1613 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1615 for (i
= 0; i
< pwaff
->n
; ++i
) {
1616 isl_basic_set
*bset
;
1619 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1620 set_i
= isl_set_from_basic_set(bset
);
1621 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1622 set
= isl_set_union_disjoint(set
, set_i
);
1625 isl_pw_aff_free(pwaff
);
1630 /* Return a set containing those elements in the domain
1631 * of pwaff where it is zero (if complement is 0) or not zero
1632 * (if complement is 1).
1634 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
1643 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1645 for (i
= 0; i
< pwaff
->n
; ++i
) {
1646 isl_basic_set
*bset
;
1647 isl_set
*set_i
, *zero
;
1649 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1650 zero
= isl_set_from_basic_set(bset
);
1651 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
1653 set_i
= isl_set_subtract(set_i
, zero
);
1655 set_i
= isl_set_intersect(set_i
, zero
);
1656 set
= isl_set_union_disjoint(set
, set_i
);
1659 isl_pw_aff_free(pwaff
);
1664 /* Return a set containing those elements in the domain
1665 * of pwaff where it is zero.
1667 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1669 return pw_aff_zero_set(pwaff
, 0);
1672 /* Return a set containing those elements in the domain
1673 * of pwaff where it is not zero.
1675 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1677 return pw_aff_zero_set(pwaff
, 1);
1680 /* Return a set containing those elements in the shared domain
1681 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1683 * We compute the difference on the shared domain and then construct
1684 * the set of values where this difference is non-negative.
1685 * If strict is set, we first subtract 1 from the difference.
1686 * If equal is set, we only return the elements where pwaff1 and pwaff2
1689 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1690 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1692 isl_set
*set1
, *set2
;
1694 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1695 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1696 set1
= isl_set_intersect(set1
, set2
);
1697 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1698 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1699 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1702 isl_space
*dim
= isl_set_get_space(set1
);
1704 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1705 aff
= isl_aff_add_constant_si(aff
, -1);
1706 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1711 return isl_pw_aff_zero_set(pwaff1
);
1712 return isl_pw_aff_nonneg_set(pwaff1
);
1715 /* Return a set containing those elements in the shared domain
1716 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1718 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1719 __isl_take isl_pw_aff
*pwaff2
)
1721 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1724 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1725 __isl_take isl_pw_aff
*pwaff2
)
1727 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1730 /* Return a set containing those elements in the shared domain
1731 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1733 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1734 __isl_take isl_pw_aff
*pwaff2
)
1736 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1739 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1740 __isl_take isl_pw_aff
*pwaff2
)
1742 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1745 /* Return a set containing those elements in the shared domain
1746 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1748 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1749 __isl_take isl_pw_aff
*pwaff2
)
1751 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1754 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1755 __isl_take isl_pw_aff
*pwaff2
)
1757 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1760 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1761 __isl_take isl_pw_aff
*pwaff2
)
1763 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1766 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1767 __isl_take isl_pw_aff
*pwaff2
)
1769 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1772 /* Return a set containing those elements in the shared domain
1773 * of the elements of list1 and list2 where each element in list1
1774 * has the relation specified by "fn" with each element in list2.
1776 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1777 __isl_take isl_pw_aff_list
*list2
,
1778 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1779 __isl_take isl_pw_aff
*pwaff2
))
1785 if (!list1
|| !list2
)
1788 ctx
= isl_pw_aff_list_get_ctx(list1
);
1789 if (list1
->n
< 1 || list2
->n
< 1)
1790 isl_die(ctx
, isl_error_invalid
,
1791 "list should contain at least one element", goto error
);
1793 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1794 for (i
= 0; i
< list1
->n
; ++i
)
1795 for (j
= 0; j
< list2
->n
; ++j
) {
1798 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1799 isl_pw_aff_copy(list2
->p
[j
]));
1800 set
= isl_set_intersect(set
, set_ij
);
1803 isl_pw_aff_list_free(list1
);
1804 isl_pw_aff_list_free(list2
);
1807 isl_pw_aff_list_free(list1
);
1808 isl_pw_aff_list_free(list2
);
1812 /* Return a set containing those elements in the shared domain
1813 * of the elements of list1 and list2 where each element in list1
1814 * is equal to each element in list2.
1816 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1817 __isl_take isl_pw_aff_list
*list2
)
1819 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1822 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1823 __isl_take isl_pw_aff_list
*list2
)
1825 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1828 /* Return a set containing those elements in the shared domain
1829 * of the elements of list1 and list2 where each element in list1
1830 * is less than or equal to each element in list2.
1832 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1833 __isl_take isl_pw_aff_list
*list2
)
1835 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1838 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1839 __isl_take isl_pw_aff_list
*list2
)
1841 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1844 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1845 __isl_take isl_pw_aff_list
*list2
)
1847 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1850 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1851 __isl_take isl_pw_aff_list
*list2
)
1853 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1857 /* Return a set containing those elements in the shared domain
1858 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1860 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1861 __isl_take isl_pw_aff
*pwaff2
)
1863 isl_set
*set_lt
, *set_gt
;
1865 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1866 isl_pw_aff_copy(pwaff2
));
1867 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1868 return isl_set_union_disjoint(set_lt
, set_gt
);
1871 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1872 __isl_take isl_pw_aff
*pwaff2
)
1874 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1877 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1882 if (isl_int_is_one(v
))
1884 if (!isl_int_is_pos(v
))
1885 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1886 "factor needs to be positive",
1887 return isl_pw_aff_free(pwaff
));
1888 pwaff
= isl_pw_aff_cow(pwaff
);
1894 for (i
= 0; i
< pwaff
->n
; ++i
) {
1895 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1896 if (!pwaff
->p
[i
].aff
)
1897 return isl_pw_aff_free(pwaff
);
1903 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1907 pwaff
= isl_pw_aff_cow(pwaff
);
1913 for (i
= 0; i
< pwaff
->n
; ++i
) {
1914 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1915 if (!pwaff
->p
[i
].aff
)
1916 return isl_pw_aff_free(pwaff
);
1922 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1926 pwaff
= isl_pw_aff_cow(pwaff
);
1932 for (i
= 0; i
< pwaff
->n
; ++i
) {
1933 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1934 if (!pwaff
->p
[i
].aff
)
1935 return isl_pw_aff_free(pwaff
);
1941 /* Assuming that "cond1" and "cond2" are disjoint,
1942 * return an affine expression that is equal to pwaff1 on cond1
1943 * and to pwaff2 on cond2.
1945 static __isl_give isl_pw_aff
*isl_pw_aff_select(
1946 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
1947 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
1949 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
1950 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
1952 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
1955 /* Return an affine expression that is equal to pwaff_true for elements
1956 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1958 * That is, return cond ? pwaff_true : pwaff_false;
1960 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
1961 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1963 isl_set
*cond_true
, *cond_false
;
1965 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1966 cond_false
= isl_pw_aff_zero_set(cond
);
1967 return isl_pw_aff_select(cond_true
, pwaff_true
,
1968 cond_false
, pwaff_false
);
1971 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1976 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1979 /* Check whether pwaff is a piecewise constant.
1981 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1988 for (i
= 0; i
< pwaff
->n
; ++i
) {
1989 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1990 if (is_cst
< 0 || !is_cst
)
1997 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1998 __isl_take isl_aff
*aff2
)
2000 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2001 return isl_aff_mul(aff2
, aff1
);
2003 if (!isl_aff_is_cst(aff2
))
2004 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2005 "at least one affine expression should be constant",
2008 aff1
= isl_aff_cow(aff1
);
2012 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2013 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2023 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2024 __isl_take isl_pw_aff
*pwaff2
)
2026 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2029 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2030 __isl_take isl_pw_aff
*pwaff2
)
2032 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2035 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2036 __isl_take isl_pw_aff
*pwaff2
)
2038 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2041 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2042 __isl_take isl_pw_aff
*pwaff2
)
2044 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2047 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2048 __isl_take isl_pw_aff
*pwaff2
)
2050 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2053 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2054 __isl_take isl_pw_aff
*pwaff2
)
2059 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2060 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2061 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
2062 isl_pw_aff_copy(pwaff2
));
2063 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
2064 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
2067 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2068 __isl_take isl_pw_aff
*pwaff2
)
2070 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
2073 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2074 __isl_take isl_pw_aff
*pwaff2
)
2079 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2080 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2081 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2082 isl_pw_aff_copy(pwaff2
));
2083 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
2084 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
2087 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2088 __isl_take isl_pw_aff
*pwaff2
)
2090 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2093 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2094 __isl_take isl_pw_aff_list
*list
,
2095 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2096 __isl_take isl_pw_aff
*pwaff2
))
2105 ctx
= isl_pw_aff_list_get_ctx(list
);
2107 isl_die(ctx
, isl_error_invalid
,
2108 "list should contain at least one element",
2109 return isl_pw_aff_list_free(list
));
2111 res
= isl_pw_aff_copy(list
->p
[0]);
2112 for (i
= 1; i
< list
->n
; ++i
)
2113 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2115 isl_pw_aff_list_free(list
);
2119 /* Return an isl_pw_aff that maps each element in the intersection of the
2120 * domains of the elements of list to the minimal corresponding affine
2123 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2125 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2128 /* Return an isl_pw_aff that maps each element in the intersection of the
2129 * domains of the elements of list to the maximal corresponding affine
2132 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2134 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2140 #include <isl_multi_templ.c>
2142 /* Construct an isl_multi_aff in the given space with value zero in
2143 * each of the output dimensions.
2145 __isl_give isl_multi_aff
*isl_multi_aff_zero(__isl_take isl_space
*space
)
2153 n
= isl_space_dim(space
, isl_dim_out
);
2154 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2157 isl_space_free(space
);
2160 isl_local_space
*ls
;
2163 space
= isl_space_domain(space
);
2164 ls
= isl_local_space_from_space(space
);
2165 aff
= isl_aff_zero_on_domain(ls
);
2167 for (i
= 0; i
< n
; ++i
)
2168 ma
= isl_multi_aff_set_aff(ma
, i
, isl_aff_copy(aff
));
2176 /* Create an isl_multi_aff in the given space that maps each
2177 * input dimension to the corresponding output dimension.
2179 __isl_give isl_multi_aff
*isl_multi_aff_identity(__isl_take isl_space
*space
)
2187 if (isl_space_is_set(space
))
2188 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2189 "expecting map space", goto error
);
2191 n
= isl_space_dim(space
, isl_dim_out
);
2192 if (n
!= isl_space_dim(space
, isl_dim_in
))
2193 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2194 "number of input and output dimensions needs to be "
2195 "the same", goto error
);
2197 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2200 isl_space_free(space
);
2203 isl_local_space
*ls
;
2206 space
= isl_space_domain(space
);
2207 ls
= isl_local_space_from_space(space
);
2208 aff
= isl_aff_zero_on_domain(ls
);
2210 for (i
= 0; i
< n
; ++i
) {
2212 aff_i
= isl_aff_copy(aff
);
2213 aff_i
= isl_aff_add_coefficient_si(aff_i
,
2215 ma
= isl_multi_aff_set_aff(ma
, i
, aff_i
);
2223 isl_space_free(space
);
2227 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2230 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
2231 __isl_take isl_multi_aff
*ma
)
2233 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
2234 return isl_pw_multi_aff_alloc(dom
, ma
);
2237 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2238 __isl_take isl_multi_aff
*maff2
)
2243 maff1
= isl_multi_aff_cow(maff1
);
2244 if (!maff1
|| !maff2
)
2247 ctx
= isl_multi_aff_get_ctx(maff1
);
2248 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2249 isl_die(ctx
, isl_error_invalid
,
2250 "spaces don't match", goto error
);
2252 for (i
= 0; i
< maff1
->n
; ++i
) {
2253 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2254 isl_aff_copy(maff2
->p
[i
]));
2259 isl_multi_aff_free(maff2
);
2262 isl_multi_aff_free(maff1
);
2263 isl_multi_aff_free(maff2
);
2267 /* Given two multi-affine expressions A -> B and C -> D,
2268 * construct a multi-affine expression [A -> C] -> [B -> D].
2270 __isl_give isl_multi_aff
*isl_multi_aff_product(
2271 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
2277 int in1
, in2
, out1
, out2
;
2279 in1
= isl_multi_aff_dim(ma1
, isl_dim_in
);
2280 in2
= isl_multi_aff_dim(ma2
, isl_dim_in
);
2281 out1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
2282 out2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
2283 space
= isl_space_product(isl_multi_aff_get_space(ma1
),
2284 isl_multi_aff_get_space(ma2
));
2285 res
= isl_multi_aff_alloc(isl_space_copy(space
));
2286 space
= isl_space_domain(space
);
2288 for (i
= 0; i
< out1
; ++i
) {
2289 aff
= isl_multi_aff_get_aff(ma1
, i
);
2290 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, in1
, in2
);
2291 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2292 res
= isl_multi_aff_set_aff(res
, i
, aff
);
2295 for (i
= 0; i
< out2
; ++i
) {
2296 aff
= isl_multi_aff_get_aff(ma2
, i
);
2297 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, 0, in1
);
2298 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2299 res
= isl_multi_aff_set_aff(res
, out1
+ i
, aff
);
2302 isl_space_free(space
);
2303 isl_multi_aff_free(ma1
);
2304 isl_multi_aff_free(ma2
);
2308 /* Exploit the equalities in "eq" to simplify the affine expressions.
2310 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2311 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2315 maff
= isl_multi_aff_cow(maff
);
2319 for (i
= 0; i
< maff
->n
; ++i
) {
2320 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2321 isl_basic_set_copy(eq
));
2326 isl_basic_set_free(eq
);
2329 isl_basic_set_free(eq
);
2330 isl_multi_aff_free(maff
);
2334 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2339 maff
= isl_multi_aff_cow(maff
);
2343 for (i
= 0; i
< maff
->n
; ++i
) {
2344 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2346 return isl_multi_aff_free(maff
);
2352 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2353 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2355 maff1
= isl_multi_aff_add(maff1
, maff2
);
2356 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2360 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2368 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2369 __isl_keep isl_multi_aff
*maff2
)
2374 if (!maff1
|| !maff2
)
2376 if (maff1
->n
!= maff2
->n
)
2378 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2379 if (equal
< 0 || !equal
)
2382 for (i
= 0; i
< maff1
->n
; ++i
) {
2383 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2384 if (equal
< 0 || !equal
)
2391 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2392 __isl_take isl_multi_aff
*maff
,
2393 enum isl_dim_type type
, unsigned pos
, const char *s
)
2397 maff
= isl_multi_aff_cow(maff
);
2401 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2403 return isl_multi_aff_free(maff
);
2405 if (type
== isl_dim_out
)
2407 for (i
= 0; i
< maff
->n
; ++i
) {
2408 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2410 return isl_multi_aff_free(maff
);
2416 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2417 enum isl_dim_type type
, unsigned first
, unsigned n
)
2421 maff
= isl_multi_aff_cow(maff
);
2425 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2427 return isl_multi_aff_free(maff
);
2429 if (type
== isl_dim_out
) {
2430 for (i
= 0; i
< n
; ++i
)
2431 isl_aff_free(maff
->p
[first
+ i
]);
2432 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2433 maff
->p
[i
] = maff
->p
[i
+ n
];
2438 for (i
= 0; i
< maff
->n
; ++i
) {
2439 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2441 return isl_multi_aff_free(maff
);
2447 /* Return the set of domain elements where "ma1" is lexicographically
2448 * smaller than or equal to "ma2".
2450 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
2451 __isl_take isl_multi_aff
*ma2
)
2453 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
2456 /* Return the set of domain elements where "ma1" is lexicographically
2457 * greater than or equal to "ma2".
2459 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
2460 __isl_take isl_multi_aff
*ma2
)
2463 isl_map
*map1
, *map2
;
2466 map1
= isl_map_from_multi_aff(ma1
);
2467 map2
= isl_map_from_multi_aff(ma2
);
2468 map
= isl_map_range_product(map1
, map2
);
2469 space
= isl_space_range(isl_map_get_space(map
));
2470 space
= isl_space_domain(isl_space_unwrap(space
));
2471 ge
= isl_map_lex_ge(space
);
2472 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
2474 return isl_map_domain(map
);
2478 #define PW isl_pw_multi_aff
2480 #define EL isl_multi_aff
2482 #define EL_IS_ZERO is_empty
2486 #define IS_ZERO is_empty
2489 #undef DEFAULT_IS_ZERO
2490 #define DEFAULT_IS_ZERO 0
2495 #define NO_INVOLVES_DIMS
2496 #define NO_MOVE_DIMS
2497 #define NO_INSERT_DIMS
2501 #include <isl_pw_templ.c>
2504 #define UNION isl_union_pw_multi_aff
2506 #define PART isl_pw_multi_aff
2508 #define PARTS pw_multi_aff
2509 #define ALIGN_DOMAIN
2513 #include <isl_union_templ.c>
2515 /* Given a function "cmp" that returns the set of elements where
2516 * "ma1" is "better" than "ma2", return the intersection of this
2517 * set with "dom1" and "dom2".
2519 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
2520 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
2521 __isl_keep isl_multi_aff
*ma2
,
2522 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2523 __isl_take isl_multi_aff
*ma2
))
2529 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
2530 is_empty
= isl_set_plain_is_empty(common
);
2531 if (is_empty
>= 0 && is_empty
)
2534 return isl_set_free(common
);
2535 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
2536 better
= isl_set_intersect(common
, better
);
2541 /* Given a function "cmp" that returns the set of elements where
2542 * "ma1" is "better" than "ma2", return a piecewise multi affine
2543 * expression defined on the union of the definition domains
2544 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2545 * "pma2" on each cell. If only one of the two input functions
2546 * is defined on a given cell, then it is considered the best.
2548 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
2549 __isl_take isl_pw_multi_aff
*pma1
,
2550 __isl_take isl_pw_multi_aff
*pma2
,
2551 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2552 __isl_take isl_multi_aff
*ma2
))
2555 isl_pw_multi_aff
*res
= NULL
;
2557 isl_set
*set
= NULL
;
2562 ctx
= isl_space_get_ctx(pma1
->dim
);
2563 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
2564 isl_die(ctx
, isl_error_invalid
,
2565 "arguments should live in the same space", goto error
);
2567 if (isl_pw_multi_aff_is_empty(pma1
)) {
2568 isl_pw_multi_aff_free(pma1
);
2572 if (isl_pw_multi_aff_is_empty(pma2
)) {
2573 isl_pw_multi_aff_free(pma2
);
2577 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
2578 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
2580 for (i
= 0; i
< pma1
->n
; ++i
) {
2581 set
= isl_set_copy(pma1
->p
[i
].set
);
2582 for (j
= 0; j
< pma2
->n
; ++j
) {
2586 better
= shared_and_better(pma2
->p
[j
].set
,
2587 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
2588 pma1
->p
[i
].maff
, cmp
);
2589 is_empty
= isl_set_plain_is_empty(better
);
2590 if (is_empty
< 0 || is_empty
) {
2591 isl_set_free(better
);
2596 set
= isl_set_subtract(set
, isl_set_copy(better
));
2598 res
= isl_pw_multi_aff_add_piece(res
, better
,
2599 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2601 res
= isl_pw_multi_aff_add_piece(res
, set
,
2602 isl_multi_aff_copy(pma1
->p
[i
].maff
));
2605 for (j
= 0; j
< pma2
->n
; ++j
) {
2606 set
= isl_set_copy(pma2
->p
[j
].set
);
2607 for (i
= 0; i
< pma1
->n
; ++i
)
2608 set
= isl_set_subtract(set
,
2609 isl_set_copy(pma1
->p
[i
].set
));
2610 res
= isl_pw_multi_aff_add_piece(res
, set
,
2611 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2614 isl_pw_multi_aff_free(pma1
);
2615 isl_pw_multi_aff_free(pma2
);
2619 isl_pw_multi_aff_free(pma1
);
2620 isl_pw_multi_aff_free(pma2
);
2622 return isl_pw_multi_aff_free(res
);
2625 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
2626 __isl_take isl_pw_multi_aff
*pma1
,
2627 __isl_take isl_pw_multi_aff
*pma2
)
2629 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
2632 /* Given two piecewise multi affine expressions, return a piecewise
2633 * multi-affine expression defined on the union of the definition domains
2634 * of the inputs that is equal to the lexicographic maximum of the two
2635 * inputs on each cell. If only one of the two inputs is defined on
2636 * a given cell, then it is considered to be the maximum.
2638 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
2639 __isl_take isl_pw_multi_aff
*pma1
,
2640 __isl_take isl_pw_multi_aff
*pma2
)
2642 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2643 &pw_multi_aff_union_lexmax
);
2646 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
2647 __isl_take isl_pw_multi_aff
*pma1
,
2648 __isl_take isl_pw_multi_aff
*pma2
)
2650 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
2653 /* Given two piecewise multi affine expressions, return a piecewise
2654 * multi-affine expression defined on the union of the definition domains
2655 * of the inputs that is equal to the lexicographic minimum of the two
2656 * inputs on each cell. If only one of the two inputs is defined on
2657 * a given cell, then it is considered to be the minimum.
2659 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
2660 __isl_take isl_pw_multi_aff
*pma1
,
2661 __isl_take isl_pw_multi_aff
*pma2
)
2663 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2664 &pw_multi_aff_union_lexmin
);
2667 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2668 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2670 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2671 &isl_multi_aff_add
);
2674 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2675 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2677 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2681 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2682 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2684 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2687 /* Given two piecewise multi-affine expressions A -> B and C -> D,
2688 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
2690 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
2691 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2695 isl_pw_multi_aff
*res
;
2700 n
= pma1
->n
* pma2
->n
;
2701 space
= isl_space_product(isl_space_copy(pma1
->dim
),
2702 isl_space_copy(pma2
->dim
));
2703 res
= isl_pw_multi_aff_alloc_size(space
, n
);
2705 for (i
= 0; i
< pma1
->n
; ++i
) {
2706 for (j
= 0; j
< pma2
->n
; ++j
) {
2710 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
2711 isl_set_copy(pma2
->p
[j
].set
));
2712 ma
= isl_multi_aff_product(
2713 isl_multi_aff_copy(pma1
->p
[i
].maff
),
2714 isl_multi_aff_copy(pma2
->p
[i
].maff
));
2715 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
2719 isl_pw_multi_aff_free(pma1
);
2720 isl_pw_multi_aff_free(pma2
);
2723 isl_pw_multi_aff_free(pma1
);
2724 isl_pw_multi_aff_free(pma2
);
2728 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
2729 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2731 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2732 &pw_multi_aff_product
);
2735 /* Construct a map mapping the domain of the piecewise multi-affine expression
2736 * to its range, with each dimension in the range equated to the
2737 * corresponding affine expression on its cell.
2739 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2747 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2749 for (i
= 0; i
< pma
->n
; ++i
) {
2750 isl_multi_aff
*maff
;
2751 isl_basic_map
*bmap
;
2754 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2755 bmap
= isl_basic_map_from_multi_aff(maff
);
2756 map_i
= isl_map_from_basic_map(bmap
);
2757 map_i
= isl_map_intersect_domain(map_i
,
2758 isl_set_copy(pma
->p
[i
].set
));
2759 map
= isl_map_union_disjoint(map
, map_i
);
2762 isl_pw_multi_aff_free(pma
);
2766 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2768 if (!isl_space_is_set(pma
->dim
))
2769 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2770 "isl_pw_multi_aff cannot be converted into an isl_set",
2771 return isl_pw_multi_aff_free(pma
));
2773 return isl_map_from_pw_multi_aff(pma
);
2776 /* Given a basic map with a single output dimension that is defined
2777 * in terms of the parameters and input dimensions using an equality,
2778 * extract an isl_aff that expresses the output dimension in terms
2779 * of the parameters and input dimensions.
2781 * Since some applications expect the result of isl_pw_multi_aff_from_map
2782 * to only contain integer affine expressions, we compute the floor
2783 * of the expression before returning.
2785 * This function shares some similarities with
2786 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2788 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
2789 __isl_take isl_basic_map
*bmap
)
2794 isl_local_space
*ls
;
2799 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
2800 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2801 "basic map should have a single output dimension",
2803 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
2804 total
= isl_basic_map_total_dim(bmap
);
2805 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2806 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
2808 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
2809 1 + total
- (offset
+ 1)) != -1)
2813 if (i
>= bmap
->n_eq
)
2814 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2815 "unable to find suitable equality", goto error
);
2816 ls
= isl_basic_map_get_local_space(bmap
);
2817 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
2820 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
2821 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2823 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2824 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
2825 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
2826 isl_basic_map_free(bmap
);
2828 aff
= isl_aff_remove_unused_divs(aff
);
2829 aff
= isl_aff_floor(aff
);
2832 isl_basic_map_free(bmap
);
2836 /* Given a basic map where each output dimension is defined
2837 * in terms of the parameters and input dimensions using an equality,
2838 * extract an isl_multi_aff that expresses the output dimensions in terms
2839 * of the parameters and input dimensions.
2841 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
2842 __isl_take isl_basic_map
*bmap
)
2851 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
2852 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2854 for (i
= 0; i
< n_out
; ++i
) {
2855 isl_basic_map
*bmap_i
;
2858 bmap_i
= isl_basic_map_copy(bmap
);
2859 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
2860 i
+ 1, n_out
- (1 + i
));
2861 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
2862 aff
= extract_isl_aff_from_basic_map(bmap_i
);
2863 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2866 isl_basic_map_free(bmap
);
2871 /* Create an isl_pw_multi_aff that is equivalent to
2872 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2873 * The given basic map is such that each output dimension is defined
2874 * in terms of the parameters and input dimensions using an equality.
2876 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
2877 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
2881 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
2882 return isl_pw_multi_aff_alloc(domain
, ma
);
2885 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2886 * This obivously only works if the input "map" is single-valued.
2887 * If so, we compute the lexicographic minimum of the image in the form
2888 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2889 * to its lexicographic minimum.
2890 * If the input is not single-valued, we produce an error.
2892 * As a special case, we first check if all output dimensions are uniquely
2893 * defined in terms of the parameters and input dimensions over the entire
2894 * domain. If so, we extract the desired isl_pw_multi_aff directly
2895 * from the affine hull of "map" and its domain.
2897 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2901 isl_pw_multi_aff
*pma
;
2902 isl_basic_map
*hull
;
2907 hull
= isl_map_affine_hull(isl_map_copy(map
));
2908 sv
= isl_basic_map_plain_is_single_valued(hull
);
2910 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
2911 isl_basic_map_free(hull
);
2915 sv
= isl_map_is_single_valued(map
);
2919 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2920 "map is not single-valued", goto error
);
2921 map
= isl_map_make_disjoint(map
);
2925 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2927 for (i
= 0; i
< map
->n
; ++i
) {
2928 isl_pw_multi_aff
*pma_i
;
2929 isl_basic_map
*bmap
;
2930 bmap
= isl_basic_map_copy(map
->p
[i
]);
2931 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2932 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2942 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2944 return isl_pw_multi_aff_from_map(set
);
2947 /* Return the piecewise affine expression "set ? 1 : 0".
2949 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
2952 isl_space
*space
= isl_set_get_space(set
);
2953 isl_local_space
*ls
= isl_local_space_from_space(space
);
2954 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
2955 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
2957 one
= isl_aff_add_constant_si(one
, 1);
2958 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
2959 set
= isl_set_complement(set
);
2960 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
2965 /* Plug in "subs" for dimension "type", "pos" of "aff".
2967 * Let i be the dimension to replace and let "subs" be of the form
2971 * and "aff" of the form
2977 * floor((a f + d g')/(m d))
2979 * where g' is the result of plugging in "subs" in each of the integer
2982 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2983 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2988 aff
= isl_aff_cow(aff
);
2990 return isl_aff_free(aff
);
2992 ctx
= isl_aff_get_ctx(aff
);
2993 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2994 isl_die(ctx
, isl_error_invalid
,
2995 "spaces don't match", return isl_aff_free(aff
));
2996 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2997 isl_die(ctx
, isl_error_unsupported
,
2998 "cannot handle divs yet", return isl_aff_free(aff
));
3000 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
3002 return isl_aff_free(aff
);
3004 aff
->v
= isl_vec_cow(aff
->v
);
3006 return isl_aff_free(aff
);
3008 pos
+= isl_local_space_offset(aff
->ls
, type
);
3011 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
3012 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
3013 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
3014 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
3015 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
3021 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3022 * expressions in "maff".
3024 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
3025 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
3026 __isl_keep isl_aff
*subs
)
3030 maff
= isl_multi_aff_cow(maff
);
3032 return isl_multi_aff_free(maff
);
3034 if (type
== isl_dim_in
)
3037 for (i
= 0; i
< maff
->n
; ++i
) {
3038 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
3040 return isl_multi_aff_free(maff
);
3046 /* Plug in "subs" for dimension "type", "pos" of "pma".
3048 * pma is of the form
3052 * while subs is of the form
3054 * v' = B_j(v) -> S_j
3056 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3057 * has a contribution in the result, in particular
3059 * C_ij(S_j) -> M_i(S_j)
3061 * Note that plugging in S_j in C_ij may also result in an empty set
3062 * and this contribution should simply be discarded.
3064 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
3065 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
3066 __isl_keep isl_pw_aff
*subs
)
3069 isl_pw_multi_aff
*res
;
3072 return isl_pw_multi_aff_free(pma
);
3074 n
= pma
->n
* subs
->n
;
3075 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
3077 for (i
= 0; i
< pma
->n
; ++i
) {
3078 for (j
= 0; j
< subs
->n
; ++j
) {
3080 isl_multi_aff
*res_ij
;
3081 common
= isl_set_intersect(
3082 isl_set_copy(pma
->p
[i
].set
),
3083 isl_set_copy(subs
->p
[j
].set
));
3084 common
= isl_set_substitute(common
,
3085 type
, pos
, subs
->p
[j
].aff
);
3086 if (isl_set_plain_is_empty(common
)) {
3087 isl_set_free(common
);
3091 res_ij
= isl_multi_aff_substitute(
3092 isl_multi_aff_copy(pma
->p
[i
].maff
),
3093 type
, pos
, subs
->p
[j
].aff
);
3095 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
3099 isl_pw_multi_aff_free(pma
);
3103 /* Extend the local space of "dst" to include the divs
3104 * in the local space of "src".
3106 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
3107 __isl_keep isl_aff
*src
)
3115 return isl_aff_free(dst
);
3117 ctx
= isl_aff_get_ctx(src
);
3118 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
3119 isl_die(ctx
, isl_error_invalid
,
3120 "spaces don't match", goto error
);
3122 if (src
->ls
->div
->n_row
== 0)
3125 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
3126 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
3130 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
3131 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
3139 return isl_aff_free(dst
);
3142 /* Adjust the local spaces of the affine expressions in "maff"
3143 * such that they all have the save divs.
3145 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
3146 __isl_take isl_multi_aff
*maff
)
3154 maff
= isl_multi_aff_cow(maff
);
3158 for (i
= 1; i
< maff
->n
; ++i
)
3159 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
3160 for (i
= 1; i
< maff
->n
; ++i
) {
3161 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
3163 return isl_multi_aff_free(maff
);
3169 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
3171 aff
= isl_aff_cow(aff
);
3175 aff
->ls
= isl_local_space_lift(aff
->ls
);
3177 return isl_aff_free(aff
);
3182 /* Lift "maff" to a space with extra dimensions such that the result
3183 * has no more existentially quantified variables.
3184 * If "ls" is not NULL, then *ls is assigned the local space that lies
3185 * at the basis of the lifting applied to "maff".
3187 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
3188 __isl_give isl_local_space
**ls
)
3202 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
3203 *ls
= isl_local_space_from_space(space
);
3205 return isl_multi_aff_free(maff
);
3210 maff
= isl_multi_aff_cow(maff
);
3211 maff
= isl_multi_aff_align_divs(maff
);
3215 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
3216 space
= isl_multi_aff_get_space(maff
);
3217 space
= isl_space_lift(isl_space_domain(space
), n_div
);
3218 space
= isl_space_extend_domain_with_range(space
,
3219 isl_multi_aff_get_space(maff
));
3221 return isl_multi_aff_free(maff
);
3222 isl_space_free(maff
->space
);
3223 maff
->space
= space
;
3226 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
3228 return isl_multi_aff_free(maff
);
3231 for (i
= 0; i
< maff
->n
; ++i
) {
3232 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
3240 isl_local_space_free(*ls
);
3241 return isl_multi_aff_free(maff
);
3245 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
3247 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
3248 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
3258 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
3259 if (pos
< 0 || pos
>= n_out
)
3260 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3261 "index out of bounds", return NULL
);
3263 space
= isl_pw_multi_aff_get_space(pma
);
3264 space
= isl_space_drop_dims(space
, isl_dim_out
,
3265 pos
+ 1, n_out
- pos
- 1);
3266 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
3268 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
3269 for (i
= 0; i
< pma
->n
; ++i
) {
3271 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
3272 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
3278 /* Return an isl_pw_multi_aff with the given "set" as domain and
3279 * an unnamed zero-dimensional range.
3281 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
3282 __isl_take isl_set
*set
)
3287 space
= isl_set_get_space(set
);
3288 space
= isl_space_from_domain(space
);
3289 ma
= isl_multi_aff_zero(space
);
3290 return isl_pw_multi_aff_alloc(set
, ma
);
3293 /* Add an isl_pw_multi_aff with the given "set" as domain and
3294 * an unnamed zero-dimensional range to *user.
3296 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
3298 isl_union_pw_multi_aff
**upma
= user
;
3299 isl_pw_multi_aff
*pma
;
3301 pma
= isl_pw_multi_aff_from_domain(set
);
3302 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
3307 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
3308 * an unnamed zero-dimensional range.
3310 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
3311 __isl_take isl_union_set
*uset
)
3314 isl_union_pw_multi_aff
*upma
;
3319 space
= isl_union_set_get_space(uset
);
3320 upma
= isl_union_pw_multi_aff_empty(space
);
3322 if (isl_union_set_foreach_set(uset
,
3323 &add_pw_multi_aff_from_domain
, &upma
) < 0)
3326 isl_union_set_free(uset
);
3329 isl_union_set_free(uset
);
3330 isl_union_pw_multi_aff_free(upma
);
3334 /* Convert "pma" to an isl_map and add it to *umap.
3336 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3338 isl_union_map
**umap
= user
;
3341 map
= isl_map_from_pw_multi_aff(pma
);
3342 *umap
= isl_union_map_add_map(*umap
, map
);
3347 /* Construct a union map mapping the domain of the union
3348 * piecewise multi-affine expression to its range, with each dimension
3349 * in the range equated to the corresponding affine expression on its cell.
3351 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
3352 __isl_take isl_union_pw_multi_aff
*upma
)
3355 isl_union_map
*umap
;
3360 space
= isl_union_pw_multi_aff_get_space(upma
);
3361 umap
= isl_union_map_empty(space
);
3363 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3364 &map_from_pw_multi_aff
, &umap
) < 0)
3367 isl_union_pw_multi_aff_free(upma
);
3370 isl_union_pw_multi_aff_free(upma
);
3371 isl_union_map_free(umap
);
3375 /* Local data for bin_entry and the callback "fn".
3377 struct isl_union_pw_multi_aff_bin_data
{
3378 isl_union_pw_multi_aff
*upma2
;
3379 isl_union_pw_multi_aff
*res
;
3380 isl_pw_multi_aff
*pma
;
3381 int (*fn
)(void **entry
, void *user
);
3384 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3385 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3387 static int bin_entry(void **entry
, void *user
)
3389 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3390 isl_pw_multi_aff
*pma
= *entry
;
3393 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
3394 data
->fn
, data
) < 0)
3400 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3401 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3402 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3403 * as *entry. The callback should adjust data->res if desired.
3405 static __isl_give isl_union_pw_multi_aff
*bin_op(
3406 __isl_take isl_union_pw_multi_aff
*upma1
,
3407 __isl_take isl_union_pw_multi_aff
*upma2
,
3408 int (*fn
)(void **entry
, void *user
))
3411 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
3413 space
= isl_union_pw_multi_aff_get_space(upma2
);
3414 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
3415 space
= isl_union_pw_multi_aff_get_space(upma1
);
3416 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
3418 if (!upma1
|| !upma2
)
3422 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
3424 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
3425 &bin_entry
, &data
) < 0)
3428 isl_union_pw_multi_aff_free(upma1
);
3429 isl_union_pw_multi_aff_free(upma2
);
3432 isl_union_pw_multi_aff_free(upma1
);
3433 isl_union_pw_multi_aff_free(upma2
);
3434 isl_union_pw_multi_aff_free(data
.res
);
3438 /* Given two isl_multi_affs A -> B and C -> D,
3439 * construct an isl_multi_aff (A * C) -> (B, D).
3441 __isl_give isl_multi_aff
*isl_multi_aff_flat_range_product(
3442 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3452 space
= isl_space_range_product(isl_multi_aff_get_space(ma1
),
3453 isl_multi_aff_get_space(ma2
));
3454 space
= isl_space_flatten_range(space
);
3455 res
= isl_multi_aff_alloc(space
);
3457 n1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
3458 n2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
3460 for (i
= 0; i
< n1
; ++i
) {
3461 aff
= isl_multi_aff_get_aff(ma1
, i
);
3462 res
= isl_multi_aff_set_aff(res
, i
, aff
);
3465 for (i
= 0; i
< n2
; ++i
) {
3466 aff
= isl_multi_aff_get_aff(ma2
, i
);
3467 res
= isl_multi_aff_set_aff(res
, n1
+ i
, aff
);
3470 isl_multi_aff_free(ma1
);
3471 isl_multi_aff_free(ma2
);
3474 isl_multi_aff_free(ma1
);
3475 isl_multi_aff_free(ma2
);
3479 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3480 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3482 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
3483 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3487 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
3488 isl_pw_multi_aff_get_space(pma2
));
3489 space
= isl_space_flatten_range(space
);
3490 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
3491 &isl_multi_aff_flat_range_product
);
3494 /* Given two isl_pw_multi_affs A -> B and C -> D,
3495 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3497 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
3498 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3500 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3501 &pw_multi_aff_flat_range_product
);
3504 /* If data->pma and *entry have the same domain space, then compute
3505 * their flat range product and the result to data->res.
3507 static int flat_range_product_entry(void **entry
, void *user
)
3509 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3510 isl_pw_multi_aff
*pma2
= *entry
;
3512 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
3513 pma2
->dim
, isl_dim_in
))
3516 pma2
= isl_pw_multi_aff_flat_range_product(
3517 isl_pw_multi_aff_copy(data
->pma
),
3518 isl_pw_multi_aff_copy(pma2
));
3520 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
3525 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3526 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3528 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
3529 __isl_take isl_union_pw_multi_aff
*upma1
,
3530 __isl_take isl_union_pw_multi_aff
*upma2
)
3532 return bin_op(upma1
, upma2
, &flat_range_product_entry
);