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>
15 #include <isl_map_private.h>
16 #include <isl_aff_private.h>
17 #include <isl_space_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_list_private.h>
21 #include <isl/constraint.h>
24 #include <isl_config.h>
26 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
27 __isl_take isl_vec
*v
)
34 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
44 isl_local_space_free(ls
);
49 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
58 ctx
= isl_local_space_get_ctx(ls
);
59 if (!isl_local_space_divs_known(ls
))
60 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
62 if (!isl_local_space_is_set(ls
))
63 isl_die(ctx
, isl_error_invalid
,
64 "domain of affine expression should be a set",
67 total
= isl_local_space_dim(ls
, isl_dim_all
);
68 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
69 return isl_aff_alloc_vec(ls
, v
);
71 isl_local_space_free(ls
);
75 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
79 aff
= isl_aff_alloc(ls
);
83 isl_int_set_si(aff
->v
->el
[0], 1);
84 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
89 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
98 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
103 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
104 isl_vec_copy(aff
->v
));
107 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
115 return isl_aff_dup(aff
);
118 void *isl_aff_free(__isl_take isl_aff
*aff
)
126 isl_local_space_free(aff
->ls
);
127 isl_vec_free(aff
->v
);
134 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
136 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
139 /* Externally, an isl_aff has a map space, but internally, the
140 * ls field corresponds to the domain of that space.
142 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
146 if (type
== isl_dim_out
)
148 if (type
== isl_dim_in
)
150 return isl_local_space_dim(aff
->ls
, type
);
153 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
155 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
158 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
163 space
= isl_local_space_get_space(aff
->ls
);
164 space
= isl_space_from_domain(space
);
165 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
169 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
170 __isl_keep isl_aff
*aff
)
172 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
175 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
180 ls
= isl_local_space_copy(aff
->ls
);
181 ls
= isl_local_space_from_domain(ls
);
182 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
186 /* Externally, an isl_aff has a map space, but internally, the
187 * ls field corresponds to the domain of that space.
189 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
190 enum isl_dim_type type
, unsigned pos
)
194 if (type
== isl_dim_out
)
196 if (type
== isl_dim_in
)
198 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
201 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
202 __isl_take isl_space
*dim
)
204 aff
= isl_aff_cow(aff
);
208 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
210 return isl_aff_free(aff
);
219 /* Reset the space of "aff". This function is called from isl_pw_templ.c
220 * and doesn't know if the space of an element object is represented
221 * directly or through its domain. It therefore passes along both.
223 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
224 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
226 isl_space_free(space
);
227 return isl_aff_reset_domain_space(aff
, domain
);
230 /* Reorder the coefficients of the affine expression based
231 * on the given reodering.
232 * The reordering r is assumed to have been extended with the local
235 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
236 __isl_take isl_reordering
*r
, int n_div
)
244 res
= isl_vec_alloc(vec
->ctx
,
245 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
246 isl_seq_cpy(res
->el
, vec
->el
, 2);
247 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
248 for (i
= 0; i
< r
->len
; ++i
)
249 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
251 isl_reordering_free(r
);
256 isl_reordering_free(r
);
260 /* Reorder the dimensions of the domain of "aff" according
261 * to the given reordering.
263 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
264 __isl_take isl_reordering
*r
)
266 aff
= isl_aff_cow(aff
);
270 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
271 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
272 aff
->ls
->div
->n_row
);
273 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
275 if (!aff
->v
|| !aff
->ls
)
276 return isl_aff_free(aff
);
281 isl_reordering_free(r
);
285 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
286 __isl_take isl_space
*model
)
291 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
292 model
, isl_dim_param
)) {
295 model
= isl_space_drop_dims(model
, isl_dim_in
,
296 0, isl_space_dim(model
, isl_dim_in
));
297 model
= isl_space_drop_dims(model
, isl_dim_out
,
298 0, isl_space_dim(model
, isl_dim_out
));
299 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
300 exp
= isl_reordering_extend_space(exp
,
301 isl_aff_get_domain_space(aff
));
302 aff
= isl_aff_realign_domain(aff
, exp
);
305 isl_space_free(model
);
308 isl_space_free(model
);
313 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
318 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
321 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
328 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
329 if (equal
< 0 || !equal
)
332 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
335 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
339 isl_int_set(*v
, aff
->v
->el
[0]);
343 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
347 isl_int_set(*v
, aff
->v
->el
[1]);
351 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
352 enum isl_dim_type type
, int pos
, isl_int
*v
)
357 if (type
== isl_dim_out
)
358 isl_die(aff
->v
->ctx
, isl_error_invalid
,
359 "output/set dimension does not have a coefficient",
361 if (type
== isl_dim_in
)
364 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
365 isl_die(aff
->v
->ctx
, isl_error_invalid
,
366 "position out of bounds", return -1);
368 pos
+= isl_local_space_offset(aff
->ls
, type
);
369 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
374 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
376 aff
= isl_aff_cow(aff
);
380 aff
->v
= isl_vec_cow(aff
->v
);
382 return isl_aff_free(aff
);
384 isl_int_set(aff
->v
->el
[0], v
);
389 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
391 aff
= isl_aff_cow(aff
);
395 aff
->v
= isl_vec_cow(aff
->v
);
397 return isl_aff_free(aff
);
399 isl_int_set(aff
->v
->el
[1], v
);
404 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
406 if (isl_int_is_zero(v
))
409 aff
= isl_aff_cow(aff
);
413 aff
->v
= isl_vec_cow(aff
->v
);
415 return isl_aff_free(aff
);
417 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
422 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
427 isl_int_set_si(t
, v
);
428 aff
= isl_aff_add_constant(aff
, t
);
434 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
436 aff
= isl_aff_cow(aff
);
440 aff
->v
= isl_vec_cow(aff
->v
);
442 return isl_aff_free(aff
);
444 isl_int_set_si(aff
->v
->el
[1], v
);
449 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
450 enum isl_dim_type type
, int pos
, isl_int v
)
455 if (type
== isl_dim_out
)
456 isl_die(aff
->v
->ctx
, isl_error_invalid
,
457 "output/set dimension does not have a coefficient",
458 return isl_aff_free(aff
));
459 if (type
== isl_dim_in
)
462 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
463 isl_die(aff
->v
->ctx
, isl_error_invalid
,
464 "position out of bounds", return isl_aff_free(aff
));
466 aff
= isl_aff_cow(aff
);
470 aff
->v
= isl_vec_cow(aff
->v
);
472 return isl_aff_free(aff
);
474 pos
+= isl_local_space_offset(aff
->ls
, type
);
475 isl_int_set(aff
->v
->el
[1 + pos
], v
);
480 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
481 enum isl_dim_type type
, int pos
, int v
)
486 if (type
== isl_dim_out
)
487 isl_die(aff
->v
->ctx
, isl_error_invalid
,
488 "output/set dimension does not have a coefficient",
489 return isl_aff_free(aff
));
490 if (type
== isl_dim_in
)
493 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
494 isl_die(aff
->v
->ctx
, isl_error_invalid
,
495 "position out of bounds", return isl_aff_free(aff
));
497 aff
= isl_aff_cow(aff
);
501 aff
->v
= isl_vec_cow(aff
->v
);
503 return isl_aff_free(aff
);
505 pos
+= isl_local_space_offset(aff
->ls
, type
);
506 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
511 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
512 enum isl_dim_type type
, int pos
, isl_int v
)
517 if (type
== isl_dim_out
)
518 isl_die(aff
->v
->ctx
, isl_error_invalid
,
519 "output/set dimension does not have a coefficient",
520 return isl_aff_free(aff
));
521 if (type
== isl_dim_in
)
524 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
525 isl_die(aff
->v
->ctx
, isl_error_invalid
,
526 "position out of bounds", return isl_aff_free(aff
));
528 aff
= isl_aff_cow(aff
);
532 aff
->v
= isl_vec_cow(aff
->v
);
534 return isl_aff_free(aff
);
536 pos
+= isl_local_space_offset(aff
->ls
, type
);
537 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
542 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
543 enum isl_dim_type type
, int pos
, int v
)
548 isl_int_set_si(t
, v
);
549 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
555 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
560 return isl_local_space_get_div(aff
->ls
, pos
);
563 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
565 aff
= isl_aff_cow(aff
);
568 aff
->v
= isl_vec_cow(aff
->v
);
570 return isl_aff_free(aff
);
572 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
577 /* Remove divs from the local space that do not appear in the affine
579 * We currently only remove divs at the end.
580 * Some intermediate divs may also not appear directly in the affine
581 * expression, but we would also need to check that no other divs are
582 * defined in terms of them.
584 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
593 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
594 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
596 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
600 aff
= isl_aff_cow(aff
);
604 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
605 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
606 if (!aff
->ls
|| !aff
->v
)
607 return isl_aff_free(aff
);
612 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
616 aff
->v
= isl_vec_normalize(aff
->v
);
618 return isl_aff_free(aff
);
619 aff
= isl_aff_remove_unused_divs(aff
);
623 /* Given f, return floor(f).
624 * If f is an integer expression, then just return f.
625 * Otherwise, if f = g/m, write g = q m + r,
626 * create a new div d = [r/m] and return the expression q + d.
627 * The coefficients in r are taken to lie between -m/2 and m/2.
629 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
639 if (isl_int_is_one(aff
->v
->el
[0]))
642 aff
= isl_aff_cow(aff
);
646 aff
->v
= isl_vec_cow(aff
->v
);
647 div
= isl_vec_copy(aff
->v
);
648 div
= isl_vec_cow(div
);
650 return isl_aff_free(aff
);
652 ctx
= isl_aff_get_ctx(aff
);
653 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
654 for (i
= 1; i
< aff
->v
->size
; ++i
) {
655 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
656 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
657 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
658 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
659 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
663 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
665 return isl_aff_free(aff
);
668 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
670 return isl_aff_free(aff
);
671 isl_int_set_si(aff
->v
->el
[0], 1);
672 isl_int_set_si(aff
->v
->el
[size
], 1);
679 * aff mod m = aff - m * floor(aff/m)
681 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
685 res
= isl_aff_copy(aff
);
686 aff
= isl_aff_scale_down(aff
, m
);
687 aff
= isl_aff_floor(aff
);
688 aff
= isl_aff_scale(aff
, m
);
689 res
= isl_aff_sub(res
, aff
);
696 * pwaff mod m = pwaff - m * floor(pwaff/m)
698 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
702 res
= isl_pw_aff_copy(pwaff
);
703 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
704 pwaff
= isl_pw_aff_floor(pwaff
);
705 pwaff
= isl_pw_aff_scale(pwaff
, m
);
706 res
= isl_pw_aff_sub(res
, pwaff
);
711 /* Given f, return ceil(f).
712 * If f is an integer expression, then just return f.
713 * Otherwise, create a new div d = [-f] and return the expression -d.
715 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
720 if (isl_int_is_one(aff
->v
->el
[0]))
723 aff
= isl_aff_neg(aff
);
724 aff
= isl_aff_floor(aff
);
725 aff
= isl_aff_neg(aff
);
730 /* Apply the expansion computed by isl_merge_divs.
731 * The expansion itself is given by "exp" while the resulting
732 * list of divs is given by "div".
734 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
735 __isl_take isl_mat
*div
, int *exp
)
742 aff
= isl_aff_cow(aff
);
746 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
747 new_n_div
= isl_mat_rows(div
);
748 if (new_n_div
< old_n_div
)
749 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
750 "not an expansion", goto error
);
752 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
756 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
758 for (i
= new_n_div
- 1; i
>= 0; --i
) {
759 if (j
>= 0 && exp
[j
] == i
) {
761 isl_int_swap(aff
->v
->el
[offset
+ i
],
762 aff
->v
->el
[offset
+ j
]);
765 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
768 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
779 /* Add two affine expressions that live in the same local space.
781 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
782 __isl_take isl_aff
*aff2
)
786 aff1
= isl_aff_cow(aff1
);
790 aff1
->v
= isl_vec_cow(aff1
->v
);
796 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
797 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
798 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
799 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
800 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
801 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
802 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
814 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
815 __isl_take isl_aff
*aff2
)
825 ctx
= isl_aff_get_ctx(aff1
);
826 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
827 isl_die(ctx
, isl_error_invalid
,
828 "spaces don't match", goto error
);
830 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
831 return add_expanded(aff1
, aff2
);
833 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
834 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
838 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
839 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
840 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
844 return add_expanded(aff1
, aff2
);
853 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
854 __isl_take isl_aff
*aff2
)
856 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
859 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
863 if (isl_int_is_one(f
))
866 aff
= isl_aff_cow(aff
);
869 aff
->v
= isl_vec_cow(aff
->v
);
871 return isl_aff_free(aff
);
874 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
875 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
876 isl_int_divexact(gcd
, f
, gcd
);
877 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
883 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
887 if (isl_int_is_one(f
))
890 aff
= isl_aff_cow(aff
);
893 aff
->v
= isl_vec_cow(aff
->v
);
895 return isl_aff_free(aff
);
898 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
899 isl_int_gcd(gcd
, gcd
, f
);
900 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
901 isl_int_divexact(gcd
, f
, gcd
);
902 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
908 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
916 isl_int_set_ui(v
, f
);
917 aff
= isl_aff_scale_down(aff
, v
);
923 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
924 enum isl_dim_type type
, unsigned pos
, const char *s
)
926 aff
= isl_aff_cow(aff
);
929 if (type
== isl_dim_out
)
930 isl_die(aff
->v
->ctx
, isl_error_invalid
,
931 "cannot set name of output/set dimension",
932 return isl_aff_free(aff
));
933 if (type
== isl_dim_in
)
935 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
937 return isl_aff_free(aff
);
942 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
943 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
945 aff
= isl_aff_cow(aff
);
947 return isl_id_free(id
);
948 if (type
== isl_dim_out
)
949 isl_die(aff
->v
->ctx
, isl_error_invalid
,
950 "cannot set name of output/set dimension",
952 if (type
== isl_dim_in
)
954 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
956 return isl_aff_free(aff
);
965 /* Exploit the equalities in "eq" to simplify the affine expression
966 * and the expressions of the integer divisions in the local space.
967 * The integer divisions in this local space are assumed to appear
968 * as regular dimensions in "eq".
970 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
971 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
980 isl_basic_set_free(eq
);
984 aff
= isl_aff_cow(aff
);
988 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
989 isl_basic_set_copy(eq
));
993 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
995 for (i
= 0; i
< eq
->n_eq
; ++i
) {
996 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
997 if (j
< 0 || j
== 0 || j
>= total
)
1000 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1004 isl_basic_set_free(eq
);
1005 aff
= isl_aff_normalize(aff
);
1008 isl_basic_set_free(eq
);
1013 /* Exploit the equalities in "eq" to simplify the affine expression
1014 * and the expressions of the integer divisions in the local space.
1016 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1017 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1023 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1025 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
1026 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1028 isl_basic_set_free(eq
);
1033 /* Look for equalities among the variables shared by context and aff
1034 * and the integer divisions of aff, if any.
1035 * The equalities are then used to eliminate coefficients and/or integer
1036 * divisions from aff.
1038 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1039 __isl_take isl_set
*context
)
1041 isl_basic_set
*hull
;
1046 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1048 isl_basic_set
*bset
;
1049 isl_local_space
*ls
;
1050 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1051 ls
= isl_aff_get_domain_local_space(aff
);
1052 bset
= isl_basic_set_from_local_space(ls
);
1053 bset
= isl_basic_set_lift(bset
);
1054 bset
= isl_basic_set_flatten(bset
);
1055 context
= isl_set_intersect(context
,
1056 isl_set_from_basic_set(bset
));
1059 hull
= isl_set_affine_hull(context
);
1060 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1063 isl_set_free(context
);
1067 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1068 __isl_take isl_set
*context
)
1070 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1071 dom_context
= isl_set_intersect_params(dom_context
, context
);
1072 return isl_aff_gist(aff
, dom_context
);
1075 /* Return a basic set containing those elements in the space
1076 * of aff where it is non-negative.
1078 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1080 isl_constraint
*ineq
;
1081 isl_basic_set
*bset
;
1083 ineq
= isl_inequality_from_aff(aff
);
1085 bset
= isl_basic_set_from_constraint(ineq
);
1086 bset
= isl_basic_set_simplify(bset
);
1090 /* Return a basic set containing those elements in the space
1091 * of aff where it is zero.
1093 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1095 isl_constraint
*ineq
;
1096 isl_basic_set
*bset
;
1098 ineq
= isl_equality_from_aff(aff
);
1100 bset
= isl_basic_set_from_constraint(ineq
);
1101 bset
= isl_basic_set_simplify(bset
);
1105 /* Return a basic set containing those elements in the shared space
1106 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1108 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1109 __isl_take isl_aff
*aff2
)
1111 aff1
= isl_aff_sub(aff1
, aff2
);
1113 return isl_aff_nonneg_basic_set(aff1
);
1116 /* Return a basic set containing those elements in the shared space
1117 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1119 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1120 __isl_take isl_aff
*aff2
)
1122 return isl_aff_ge_basic_set(aff2
, aff1
);
1125 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1126 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1128 aff1
= isl_aff_add(aff1
, aff2
);
1129 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1133 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1141 /* Check whether the given affine expression has non-zero coefficient
1142 * for any dimension in the given range or if any of these dimensions
1143 * appear with non-zero coefficients in any of the integer divisions
1144 * involved in the affine expression.
1146 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1147 enum isl_dim_type type
, unsigned first
, unsigned n
)
1159 ctx
= isl_aff_get_ctx(aff
);
1160 if (first
+ n
> isl_aff_dim(aff
, type
))
1161 isl_die(ctx
, isl_error_invalid
,
1162 "range out of bounds", return -1);
1164 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1168 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1169 for (i
= 0; i
< n
; ++i
)
1170 if (active
[first
+ i
]) {
1183 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1184 enum isl_dim_type type
, unsigned first
, unsigned n
)
1190 if (type
== isl_dim_out
)
1191 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1192 "cannot drop output/set dimension",
1193 return isl_aff_free(aff
));
1194 if (type
== isl_dim_in
)
1196 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1199 ctx
= isl_aff_get_ctx(aff
);
1200 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1201 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1202 return isl_aff_free(aff
));
1204 aff
= isl_aff_cow(aff
);
1208 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1210 return isl_aff_free(aff
);
1212 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1213 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1215 return isl_aff_free(aff
);
1220 /* Project the domain of the affine expression onto its parameter space.
1221 * The affine expression may not involve any of the domain dimensions.
1223 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
1229 n
= isl_aff_dim(aff
, isl_dim_in
);
1230 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
1232 return isl_aff_free(aff
);
1234 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1235 "affine expression involves some of the domain dimensions",
1236 return isl_aff_free(aff
));
1237 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
1238 space
= isl_aff_get_domain_space(aff
);
1239 space
= isl_space_params(space
);
1240 aff
= isl_aff_reset_domain_space(aff
, space
);
1244 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1245 enum isl_dim_type type
, unsigned first
, unsigned n
)
1251 if (type
== isl_dim_out
)
1252 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1253 "cannot insert output/set dimensions",
1254 return isl_aff_free(aff
));
1255 if (type
== isl_dim_in
)
1257 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1260 ctx
= isl_aff_get_ctx(aff
);
1261 if (first
> isl_local_space_dim(aff
->ls
, type
))
1262 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1263 return isl_aff_free(aff
));
1265 aff
= isl_aff_cow(aff
);
1269 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1271 return isl_aff_free(aff
);
1273 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1274 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1276 return isl_aff_free(aff
);
1281 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1282 enum isl_dim_type type
, unsigned n
)
1286 pos
= isl_aff_dim(aff
, type
);
1288 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1291 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1292 enum isl_dim_type type
, unsigned n
)
1296 pos
= isl_pw_aff_dim(pwaff
, type
);
1298 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1301 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1303 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1304 return isl_pw_aff_alloc(dom
, aff
);
1308 #define PW isl_pw_aff
1312 #define EL_IS_ZERO is_empty
1316 #define IS_ZERO is_empty
1319 #undef DEFAULT_IS_ZERO
1320 #define DEFAULT_IS_ZERO 0
1324 #define NO_MOVE_DIMS
1328 #include <isl_pw_templ.c>
1330 static __isl_give isl_set
*align_params_pw_pw_set_and(
1331 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1332 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1333 __isl_take isl_pw_aff
*pwaff2
))
1335 if (!pwaff1
|| !pwaff2
)
1337 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1338 pwaff2
->dim
, isl_dim_param
))
1339 return fn(pwaff1
, pwaff2
);
1340 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1341 !isl_space_has_named_params(pwaff2
->dim
))
1342 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1343 "unaligned unnamed parameters", goto error
);
1344 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1345 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1346 return fn(pwaff1
, pwaff2
);
1348 isl_pw_aff_free(pwaff1
);
1349 isl_pw_aff_free(pwaff2
);
1353 /* Compute a piecewise quasi-affine expression with a domain that
1354 * is the union of those of pwaff1 and pwaff2 and such that on each
1355 * cell, the quasi-affine expression is the better (according to cmp)
1356 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1357 * is defined on a given cell, then the associated expression
1358 * is the defined one.
1360 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1361 __isl_take isl_pw_aff
*pwaff2
,
1362 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1363 __isl_take isl_aff
*aff2
))
1370 if (!pwaff1
|| !pwaff2
)
1373 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1374 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1375 isl_die(ctx
, isl_error_invalid
,
1376 "arguments should live in same space", goto error
);
1378 if (isl_pw_aff_is_empty(pwaff1
)) {
1379 isl_pw_aff_free(pwaff1
);
1383 if (isl_pw_aff_is_empty(pwaff2
)) {
1384 isl_pw_aff_free(pwaff2
);
1388 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1389 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1391 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1392 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1393 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1394 struct isl_set
*common
;
1397 common
= isl_set_intersect(
1398 isl_set_copy(pwaff1
->p
[i
].set
),
1399 isl_set_copy(pwaff2
->p
[j
].set
));
1400 better
= isl_set_from_basic_set(cmp(
1401 isl_aff_copy(pwaff2
->p
[j
].aff
),
1402 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1403 better
= isl_set_intersect(common
, better
);
1404 if (isl_set_plain_is_empty(better
)) {
1405 isl_set_free(better
);
1408 set
= isl_set_subtract(set
, isl_set_copy(better
));
1410 res
= isl_pw_aff_add_piece(res
, better
,
1411 isl_aff_copy(pwaff2
->p
[j
].aff
));
1413 res
= isl_pw_aff_add_piece(res
, set
,
1414 isl_aff_copy(pwaff1
->p
[i
].aff
));
1417 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1418 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1419 for (i
= 0; i
< pwaff1
->n
; ++i
)
1420 set
= isl_set_subtract(set
,
1421 isl_set_copy(pwaff1
->p
[i
].set
));
1422 res
= isl_pw_aff_add_piece(res
, set
,
1423 isl_aff_copy(pwaff2
->p
[j
].aff
));
1426 isl_pw_aff_free(pwaff1
);
1427 isl_pw_aff_free(pwaff2
);
1431 isl_pw_aff_free(pwaff1
);
1432 isl_pw_aff_free(pwaff2
);
1436 /* Compute a piecewise quasi-affine expression with a domain that
1437 * is the union of those of pwaff1 and pwaff2 and such that on each
1438 * cell, the quasi-affine expression is the maximum of those of pwaff1
1439 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1440 * cell, then the associated expression is the defined one.
1442 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1443 __isl_take isl_pw_aff
*pwaff2
)
1445 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1448 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1449 __isl_take isl_pw_aff
*pwaff2
)
1451 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1455 /* Compute a piecewise quasi-affine expression with a domain that
1456 * is the union of those of pwaff1 and pwaff2 and such that on each
1457 * cell, the quasi-affine expression is the minimum of those of pwaff1
1458 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1459 * cell, then the associated expression is the defined one.
1461 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1462 __isl_take isl_pw_aff
*pwaff2
)
1464 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1467 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1468 __isl_take isl_pw_aff
*pwaff2
)
1470 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1474 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1475 __isl_take isl_pw_aff
*pwaff2
, int max
)
1478 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1480 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1483 /* Construct a map with as domain the domain of pwaff and
1484 * one-dimensional range corresponding to the affine expressions.
1486 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1495 dim
= isl_pw_aff_get_space(pwaff
);
1496 map
= isl_map_empty(dim
);
1498 for (i
= 0; i
< pwaff
->n
; ++i
) {
1499 isl_basic_map
*bmap
;
1502 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1503 map_i
= isl_map_from_basic_map(bmap
);
1504 map_i
= isl_map_intersect_domain(map_i
,
1505 isl_set_copy(pwaff
->p
[i
].set
));
1506 map
= isl_map_union_disjoint(map
, map_i
);
1509 isl_pw_aff_free(pwaff
);
1514 /* Construct a map with as domain the domain of pwaff and
1515 * one-dimensional range corresponding to the affine expressions.
1517 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1521 if (isl_space_is_set(pwaff
->dim
))
1522 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1523 "space of input is not a map",
1524 return isl_pw_aff_free(pwaff
));
1525 return map_from_pw_aff(pwaff
);
1528 /* Construct a one-dimensional set with as parameter domain
1529 * the domain of pwaff and the single set dimension
1530 * corresponding to the affine expressions.
1532 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1536 if (!isl_space_is_set(pwaff
->dim
))
1537 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1538 "space of input is not a set",
1539 return isl_pw_aff_free(pwaff
));
1540 return map_from_pw_aff(pwaff
);
1543 /* Return a set containing those elements in the domain
1544 * of pwaff where it is non-negative.
1546 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1554 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1556 for (i
= 0; i
< pwaff
->n
; ++i
) {
1557 isl_basic_set
*bset
;
1560 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1561 set_i
= isl_set_from_basic_set(bset
);
1562 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1563 set
= isl_set_union_disjoint(set
, set_i
);
1566 isl_pw_aff_free(pwaff
);
1571 /* Return a set containing those elements in the domain
1572 * of pwaff where it is zero (if complement is 0) or not zero
1573 * (if complement is 1).
1575 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
1584 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1586 for (i
= 0; i
< pwaff
->n
; ++i
) {
1587 isl_basic_set
*bset
;
1588 isl_set
*set_i
, *zero
;
1590 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1591 zero
= isl_set_from_basic_set(bset
);
1592 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
1594 set_i
= isl_set_subtract(set_i
, zero
);
1596 set_i
= isl_set_intersect(set_i
, zero
);
1597 set
= isl_set_union_disjoint(set
, set_i
);
1600 isl_pw_aff_free(pwaff
);
1605 /* Return a set containing those elements in the domain
1606 * of pwaff where it is zero.
1608 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1610 return pw_aff_zero_set(pwaff
, 0);
1613 /* Return a set containing those elements in the domain
1614 * of pwaff where it is not zero.
1616 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1618 return pw_aff_zero_set(pwaff
, 1);
1621 /* Return a set containing those elements in the shared domain
1622 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1624 * We compute the difference on the shared domain and then construct
1625 * the set of values where this difference is non-negative.
1626 * If strict is set, we first subtract 1 from the difference.
1627 * If equal is set, we only return the elements where pwaff1 and pwaff2
1630 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1631 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1633 isl_set
*set1
, *set2
;
1635 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1636 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1637 set1
= isl_set_intersect(set1
, set2
);
1638 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1639 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1640 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1643 isl_space
*dim
= isl_set_get_space(set1
);
1645 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1646 aff
= isl_aff_add_constant_si(aff
, -1);
1647 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1652 return isl_pw_aff_zero_set(pwaff1
);
1653 return isl_pw_aff_nonneg_set(pwaff1
);
1656 /* Return a set containing those elements in the shared domain
1657 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1659 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1660 __isl_take isl_pw_aff
*pwaff2
)
1662 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1665 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1666 __isl_take isl_pw_aff
*pwaff2
)
1668 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1671 /* Return a set containing those elements in the shared domain
1672 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1674 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1675 __isl_take isl_pw_aff
*pwaff2
)
1677 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1680 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1681 __isl_take isl_pw_aff
*pwaff2
)
1683 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1686 /* Return a set containing those elements in the shared domain
1687 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1689 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1690 __isl_take isl_pw_aff
*pwaff2
)
1692 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1695 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1696 __isl_take isl_pw_aff
*pwaff2
)
1698 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1701 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1702 __isl_take isl_pw_aff
*pwaff2
)
1704 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1707 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1708 __isl_take isl_pw_aff
*pwaff2
)
1710 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1713 /* Return a set containing those elements in the shared domain
1714 * of the elements of list1 and list2 where each element in list1
1715 * has the relation specified by "fn" with each element in list2.
1717 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
1718 __isl_take isl_pw_aff_list
*list2
,
1719 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1720 __isl_take isl_pw_aff
*pwaff2
))
1726 if (!list1
|| !list2
)
1729 ctx
= isl_pw_aff_list_get_ctx(list1
);
1730 if (list1
->n
< 1 || list2
->n
< 1)
1731 isl_die(ctx
, isl_error_invalid
,
1732 "list should contain at least one element", goto error
);
1734 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
1735 for (i
= 0; i
< list1
->n
; ++i
)
1736 for (j
= 0; j
< list2
->n
; ++j
) {
1739 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
1740 isl_pw_aff_copy(list2
->p
[j
]));
1741 set
= isl_set_intersect(set
, set_ij
);
1744 isl_pw_aff_list_free(list1
);
1745 isl_pw_aff_list_free(list2
);
1748 isl_pw_aff_list_free(list1
);
1749 isl_pw_aff_list_free(list2
);
1753 /* Return a set containing those elements in the shared domain
1754 * of the elements of list1 and list2 where each element in list1
1755 * is equal to each element in list2.
1757 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
1758 __isl_take isl_pw_aff_list
*list2
)
1760 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
1763 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
1764 __isl_take isl_pw_aff_list
*list2
)
1766 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
1769 /* Return a set containing those elements in the shared domain
1770 * of the elements of list1 and list2 where each element in list1
1771 * is less than or equal to each element in list2.
1773 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
1774 __isl_take isl_pw_aff_list
*list2
)
1776 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
1779 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
1780 __isl_take isl_pw_aff_list
*list2
)
1782 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
1785 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
1786 __isl_take isl_pw_aff_list
*list2
)
1788 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
1791 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
1792 __isl_take isl_pw_aff_list
*list2
)
1794 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
1798 /* Return a set containing those elements in the shared domain
1799 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1801 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1802 __isl_take isl_pw_aff
*pwaff2
)
1804 isl_set
*set_lt
, *set_gt
;
1806 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
1807 isl_pw_aff_copy(pwaff2
));
1808 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
1809 return isl_set_union_disjoint(set_lt
, set_gt
);
1812 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
1813 __isl_take isl_pw_aff
*pwaff2
)
1815 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
1818 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
1823 if (isl_int_is_one(v
))
1825 if (!isl_int_is_pos(v
))
1826 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1827 "factor needs to be positive",
1828 return isl_pw_aff_free(pwaff
));
1829 pwaff
= isl_pw_aff_cow(pwaff
);
1835 for (i
= 0; i
< pwaff
->n
; ++i
) {
1836 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
1837 if (!pwaff
->p
[i
].aff
)
1838 return isl_pw_aff_free(pwaff
);
1844 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
1848 pwaff
= isl_pw_aff_cow(pwaff
);
1854 for (i
= 0; i
< pwaff
->n
; ++i
) {
1855 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
1856 if (!pwaff
->p
[i
].aff
)
1857 return isl_pw_aff_free(pwaff
);
1863 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
1867 pwaff
= isl_pw_aff_cow(pwaff
);
1873 for (i
= 0; i
< pwaff
->n
; ++i
) {
1874 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
1875 if (!pwaff
->p
[i
].aff
)
1876 return isl_pw_aff_free(pwaff
);
1882 /* Assuming that "cond1" and "cond2" are disjoint,
1883 * return an affine expression that is equal to pwaff1 on cond1
1884 * and to pwaff2 on cond2.
1886 static __isl_give isl_pw_aff
*isl_pw_aff_select(
1887 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
1888 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
1890 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
1891 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
1893 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
1896 /* Return an affine expression that is equal to pwaff_true for elements
1897 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1899 * That is, return cond ? pwaff_true : pwaff_false;
1901 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
1902 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1904 isl_set
*cond_true
, *cond_false
;
1906 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
1907 cond_false
= isl_pw_aff_zero_set(cond
);
1908 return isl_pw_aff_select(cond_true
, pwaff_true
,
1909 cond_false
, pwaff_false
);
1912 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1917 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1920 /* Check whether pwaff is a piecewise constant.
1922 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1929 for (i
= 0; i
< pwaff
->n
; ++i
) {
1930 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1931 if (is_cst
< 0 || !is_cst
)
1938 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1939 __isl_take isl_aff
*aff2
)
1941 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1942 return isl_aff_mul(aff2
, aff1
);
1944 if (!isl_aff_is_cst(aff2
))
1945 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1946 "at least one affine expression should be constant",
1949 aff1
= isl_aff_cow(aff1
);
1953 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1954 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1964 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1965 __isl_take isl_pw_aff
*pwaff2
)
1967 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
1970 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1971 __isl_take isl_pw_aff
*pwaff2
)
1973 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
1976 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
1977 __isl_take isl_pw_aff
*pwaff2
)
1979 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
1982 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1983 __isl_take isl_pw_aff
*pwaff2
)
1985 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
1988 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1989 __isl_take isl_pw_aff
*pwaff2
)
1991 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1994 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1995 __isl_take isl_pw_aff
*pwaff2
)
2000 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2001 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2002 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
2003 isl_pw_aff_copy(pwaff2
));
2004 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
2005 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
2008 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2009 __isl_take isl_pw_aff
*pwaff2
)
2011 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
2014 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2015 __isl_take isl_pw_aff
*pwaff2
)
2020 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2021 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2022 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2023 isl_pw_aff_copy(pwaff2
));
2024 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
2025 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
2028 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2029 __isl_take isl_pw_aff
*pwaff2
)
2031 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2034 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2035 __isl_take isl_pw_aff_list
*list
,
2036 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2037 __isl_take isl_pw_aff
*pwaff2
))
2046 ctx
= isl_pw_aff_list_get_ctx(list
);
2048 isl_die(ctx
, isl_error_invalid
,
2049 "list should contain at least one element",
2050 return isl_pw_aff_list_free(list
));
2052 res
= isl_pw_aff_copy(list
->p
[0]);
2053 for (i
= 1; i
< list
->n
; ++i
)
2054 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2056 isl_pw_aff_list_free(list
);
2060 /* Return an isl_pw_aff that maps each element in the intersection of the
2061 * domains of the elements of list to the minimal corresponding affine
2064 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2066 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2069 /* Return an isl_pw_aff that maps each element in the intersection of the
2070 * domains of the elements of list to the maximal corresponding affine
2073 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2075 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2081 #include <isl_multi_templ.c>
2083 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2084 __isl_take isl_multi_aff
*maff2
)
2089 maff1
= isl_multi_aff_cow(maff1
);
2090 if (!maff1
|| !maff2
)
2093 ctx
= isl_multi_aff_get_ctx(maff1
);
2094 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2095 isl_die(ctx
, isl_error_invalid
,
2096 "spaces don't match", goto error
);
2098 for (i
= 0; i
< maff1
->n
; ++i
) {
2099 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2100 isl_aff_copy(maff2
->p
[i
]));
2105 isl_multi_aff_free(maff2
);
2108 isl_multi_aff_free(maff1
);
2109 isl_multi_aff_free(maff2
);
2113 /* Exploit the equalities in "eq" to simplify the affine expressions.
2115 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2116 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2120 maff
= isl_multi_aff_cow(maff
);
2124 for (i
= 0; i
< maff
->n
; ++i
) {
2125 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2126 isl_basic_set_copy(eq
));
2131 isl_basic_set_free(eq
);
2134 isl_basic_set_free(eq
);
2135 isl_multi_aff_free(maff
);
2139 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2144 maff
= isl_multi_aff_cow(maff
);
2148 for (i
= 0; i
< maff
->n
; ++i
) {
2149 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2151 return isl_multi_aff_free(maff
);
2157 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2158 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2160 maff1
= isl_multi_aff_add(maff1
, maff2
);
2161 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2165 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2173 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2174 __isl_keep isl_multi_aff
*maff2
)
2179 if (!maff1
|| !maff2
)
2181 if (maff1
->n
!= maff2
->n
)
2183 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2184 if (equal
< 0 || !equal
)
2187 for (i
= 0; i
< maff1
->n
; ++i
) {
2188 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2189 if (equal
< 0 || !equal
)
2196 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2197 __isl_take isl_multi_aff
*maff
,
2198 enum isl_dim_type type
, unsigned pos
, const char *s
)
2202 maff
= isl_multi_aff_cow(maff
);
2206 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2208 return isl_multi_aff_free(maff
);
2209 for (i
= 0; i
< maff
->n
; ++i
) {
2210 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2212 return isl_multi_aff_free(maff
);
2218 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2219 enum isl_dim_type type
, unsigned first
, unsigned n
)
2223 maff
= isl_multi_aff_cow(maff
);
2227 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2229 return isl_multi_aff_free(maff
);
2231 if (type
== isl_dim_out
) {
2232 for (i
= 0; i
< n
; ++i
)
2233 isl_aff_free(maff
->p
[first
+ i
]);
2234 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2235 maff
->p
[i
] = maff
->p
[i
+ n
];
2240 for (i
= 0; i
< maff
->n
; ++i
) {
2241 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2243 return isl_multi_aff_free(maff
);
2250 #define PW isl_pw_multi_aff
2252 #define EL isl_multi_aff
2254 #define EL_IS_ZERO is_empty
2258 #define IS_ZERO is_empty
2261 #undef DEFAULT_IS_ZERO
2262 #define DEFAULT_IS_ZERO 0
2267 #define NO_INVOLVES_DIMS
2268 #define NO_MOVE_DIMS
2269 #define NO_INSERT_DIMS
2273 #include <isl_pw_templ.c>
2275 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2276 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2278 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2279 &isl_multi_aff_add
);
2282 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2283 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2285 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2289 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2290 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2292 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2295 /* Construct a map mapping the domain the piecewise multi-affine expression
2296 * to its range, with each dimension in the range equated to the
2297 * corresponding affine expression on its cell.
2299 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2307 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2309 for (i
= 0; i
< pma
->n
; ++i
) {
2310 isl_multi_aff
*maff
;
2311 isl_basic_map
*bmap
;
2314 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2315 bmap
= isl_basic_map_from_multi_aff(maff
);
2316 map_i
= isl_map_from_basic_map(bmap
);
2317 map_i
= isl_map_intersect_domain(map_i
,
2318 isl_set_copy(pma
->p
[i
].set
));
2319 map
= isl_map_union_disjoint(map
, map_i
);
2322 isl_pw_multi_aff_free(pma
);
2326 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2328 if (!isl_space_is_set(pma
->dim
))
2329 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2330 "isl_pw_multi_aff cannot be converted into an isl_set",
2331 return isl_pw_multi_aff_free(pma
));
2333 return isl_map_from_pw_multi_aff(pma
);
2336 /* Given a basic map with a single output dimension that is defined
2337 * in terms of the parameters and input dimensions using an equality,
2338 * extract an isl_aff that expresses the output dimension in terms
2339 * of the parameters and input dimensions.
2341 * Since some applications expect the result of isl_pw_multi_aff_from_map
2342 * to only contain integer affine expressions, we compute the floor
2343 * of the expression before returning.
2345 * This function shares some similarities with
2346 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2348 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
2349 __isl_take isl_basic_map
*bmap
)
2354 isl_local_space
*ls
;
2359 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
2360 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2361 "basic map should have a single output dimension",
2363 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
2364 total
= isl_basic_map_total_dim(bmap
);
2365 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2366 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
2368 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
2369 1 + total
- (offset
+ 1)) != -1)
2373 if (i
>= bmap
->n_eq
)
2374 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2375 "unable to find suitable equality", goto error
);
2376 ls
= isl_basic_map_get_local_space(bmap
);
2377 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
2380 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
2381 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2383 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2384 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
2385 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
2386 isl_basic_map_free(bmap
);
2388 aff
= isl_aff_remove_unused_divs(aff
);
2389 aff
= isl_aff_floor(aff
);
2392 isl_basic_map_free(bmap
);
2396 /* Given a basic map where each output dimension is defined
2397 * in terms of the parameters and input dimensions using an equality,
2398 * extract an isl_multi_aff that expresses the output dimensions in terms
2399 * of the parameters and input dimensions.
2401 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
2402 __isl_take isl_basic_map
*bmap
)
2411 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
2412 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2414 for (i
= 0; i
< n_out
; ++i
) {
2415 isl_basic_map
*bmap_i
;
2418 bmap_i
= isl_basic_map_copy(bmap
);
2419 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
2420 i
+ 1, n_out
- (1 + i
));
2421 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
2422 aff
= extract_isl_aff_from_basic_map(bmap_i
);
2423 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2426 isl_basic_map_free(bmap
);
2431 /* Create an isl_pw_multi_aff that is equivalent to
2432 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2433 * The given basic map is such that each output dimension is defined
2434 * in terms of the parameters and input dimensions using an equality.
2436 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
2437 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
2441 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
2442 return isl_pw_multi_aff_alloc(domain
, ma
);
2445 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2446 * This obivously only works if the input "map" is single-valued.
2447 * If so, we compute the lexicographic minimum of the image in the form
2448 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2449 * to its lexicographic minimum.
2450 * If the input is not single-valued, we produce an error.
2452 * As a special case, we first check if all output dimensions are uniquely
2453 * defined in terms of the parameters and input dimensions over the entire
2454 * domain. If so, we extract the desired isl_pw_multi_aff directly
2455 * from the affine hull of "map" and its domain.
2457 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2461 isl_pw_multi_aff
*pma
;
2462 isl_basic_map
*hull
;
2467 hull
= isl_map_affine_hull(isl_map_copy(map
));
2468 sv
= isl_basic_map_plain_is_single_valued(hull
);
2470 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
2471 isl_basic_map_free(hull
);
2475 sv
= isl_map_is_single_valued(map
);
2479 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2480 "map is not single-valued", goto error
);
2481 map
= isl_map_make_disjoint(map
);
2485 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2487 for (i
= 0; i
< map
->n
; ++i
) {
2488 isl_pw_multi_aff
*pma_i
;
2489 isl_basic_map
*bmap
;
2490 bmap
= isl_basic_map_copy(map
->p
[i
]);
2491 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2492 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2502 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2504 return isl_pw_multi_aff_from_map(set
);
2507 /* Return the piecewise affine expression "set ? 1 : 0".
2509 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
2512 isl_space
*space
= isl_set_get_space(set
);
2513 isl_local_space
*ls
= isl_local_space_from_space(space
);
2514 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
2515 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
2517 one
= isl_aff_add_constant_si(one
, 1);
2518 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
2519 set
= isl_set_complement(set
);
2520 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
2525 /* Plug in "subs" for dimension "type", "pos" of "aff".
2527 * Let i be the dimension to replace and let "subs" be of the form
2531 * and "aff" of the form
2537 * floor((a f + d g')/(m d))
2539 * where g' is the result of plugging in "subs" in each of the integer
2542 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2543 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2548 aff
= isl_aff_cow(aff
);
2550 return isl_aff_free(aff
);
2552 ctx
= isl_aff_get_ctx(aff
);
2553 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2554 isl_die(ctx
, isl_error_invalid
,
2555 "spaces don't match", return isl_aff_free(aff
));
2556 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2557 isl_die(ctx
, isl_error_unsupported
,
2558 "cannot handle divs yet", return isl_aff_free(aff
));
2560 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
2562 return isl_aff_free(aff
);
2564 aff
->v
= isl_vec_cow(aff
->v
);
2566 return isl_aff_free(aff
);
2568 pos
+= isl_local_space_offset(aff
->ls
, type
);
2571 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
2572 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
2573 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
2574 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
2575 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
2581 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2582 * expressions in "maff".
2584 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
2585 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
2586 __isl_keep isl_aff
*subs
)
2590 maff
= isl_multi_aff_cow(maff
);
2592 return isl_multi_aff_free(maff
);
2594 if (type
== isl_dim_in
)
2597 for (i
= 0; i
< maff
->n
; ++i
) {
2598 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
2600 return isl_multi_aff_free(maff
);
2606 /* Plug in "subs" for dimension "type", "pos" of "pma".
2608 * pma is of the form
2612 * while subs is of the form
2614 * v' = B_j(v) -> S_j
2616 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2617 * has a contribution in the result, in particular
2619 * C_ij(S_j) -> M_i(S_j)
2621 * Note that plugging in S_j in C_ij may also result in an empty set
2622 * and this contribution should simply be discarded.
2624 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
2625 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
2626 __isl_keep isl_pw_aff
*subs
)
2629 isl_pw_multi_aff
*res
;
2632 return isl_pw_multi_aff_free(pma
);
2634 n
= pma
->n
* subs
->n
;
2635 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
2637 for (i
= 0; i
< pma
->n
; ++i
) {
2638 for (j
= 0; j
< subs
->n
; ++j
) {
2640 isl_multi_aff
*res_ij
;
2641 common
= isl_set_intersect(
2642 isl_set_copy(pma
->p
[i
].set
),
2643 isl_set_copy(subs
->p
[j
].set
));
2644 common
= isl_set_substitute(common
,
2645 type
, pos
, subs
->p
[j
].aff
);
2646 if (isl_set_plain_is_empty(common
)) {
2647 isl_set_free(common
);
2651 res_ij
= isl_multi_aff_substitute(
2652 isl_multi_aff_copy(pma
->p
[i
].maff
),
2653 type
, pos
, subs
->p
[j
].aff
);
2655 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
2659 isl_pw_multi_aff_free(pma
);
2663 /* Extend the local space of "dst" to include the divs
2664 * in the local space of "src".
2666 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
2667 __isl_keep isl_aff
*src
)
2675 return isl_aff_free(dst
);
2677 ctx
= isl_aff_get_ctx(src
);
2678 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
2679 isl_die(ctx
, isl_error_invalid
,
2680 "spaces don't match", goto error
);
2682 if (src
->ls
->div
->n_row
== 0)
2685 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
2686 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
2690 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
2691 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
2699 return isl_aff_free(dst
);
2702 /* Adjust the local spaces of the affine expressions in "maff"
2703 * such that they all have the save divs.
2705 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
2706 __isl_take isl_multi_aff
*maff
)
2714 maff
= isl_multi_aff_cow(maff
);
2718 for (i
= 1; i
< maff
->n
; ++i
)
2719 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
2720 for (i
= 1; i
< maff
->n
; ++i
) {
2721 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
2723 return isl_multi_aff_free(maff
);
2729 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
2731 aff
= isl_aff_cow(aff
);
2735 aff
->ls
= isl_local_space_lift(aff
->ls
);
2737 return isl_aff_free(aff
);
2742 /* Lift "maff" to a space with extra dimensions such that the result
2743 * has no more existentially quantified variables.
2744 * If "ls" is not NULL, then *ls is assigned the local space that lies
2745 * at the basis of the lifting applied to "maff".
2747 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
2748 __isl_give isl_local_space
**ls
)
2762 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
2763 *ls
= isl_local_space_from_space(space
);
2765 return isl_multi_aff_free(maff
);
2770 maff
= isl_multi_aff_cow(maff
);
2771 maff
= isl_multi_aff_align_divs(maff
);
2775 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
2776 space
= isl_multi_aff_get_space(maff
);
2777 space
= isl_space_lift(isl_space_domain(space
), n_div
);
2778 space
= isl_space_extend_domain_with_range(space
,
2779 isl_multi_aff_get_space(maff
));
2781 return isl_multi_aff_free(maff
);
2782 isl_space_free(maff
->space
);
2783 maff
->space
= space
;
2786 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
2788 return isl_multi_aff_free(maff
);
2791 for (i
= 0; i
< maff
->n
; ++i
) {
2792 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
2800 isl_local_space_free(*ls
);
2801 return isl_multi_aff_free(maff
);
2805 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2807 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
2808 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
2818 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
2819 if (pos
< 0 || pos
>= n_out
)
2820 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2821 "index out of bounds", return NULL
);
2823 space
= isl_pw_multi_aff_get_space(pma
);
2824 space
= isl_space_drop_dims(space
, isl_dim_out
,
2825 pos
+ 1, n_out
- pos
- 1);
2826 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
2828 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
2829 for (i
= 0; i
< pma
->n
; ++i
) {
2831 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
2832 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);