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 /* Return an affine expression that is equal to pwaff_true for elements
1883 * in "cond" and to pwaff_false for elements not in "cond".
1884 * That is, return cond ? pwaff_true : pwaff_false;
1886 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_set
*cond
,
1887 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
1891 comp
= isl_set_complement(isl_set_copy(cond
));
1892 pwaff_true
= isl_pw_aff_intersect_domain(pwaff_true
, cond
);
1893 pwaff_false
= isl_pw_aff_intersect_domain(pwaff_false
, comp
);
1895 return isl_pw_aff_add_disjoint(pwaff_true
, pwaff_false
);
1898 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
1903 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
1906 /* Check whether pwaff is a piecewise constant.
1908 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
1915 for (i
= 0; i
< pwaff
->n
; ++i
) {
1916 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
1917 if (is_cst
< 0 || !is_cst
)
1924 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
1925 __isl_take isl_aff
*aff2
)
1927 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
1928 return isl_aff_mul(aff2
, aff1
);
1930 if (!isl_aff_is_cst(aff2
))
1931 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
1932 "at least one affine expression should be constant",
1935 aff1
= isl_aff_cow(aff1
);
1939 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
1940 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
1950 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1951 __isl_take isl_pw_aff
*pwaff2
)
1953 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
1956 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
1957 __isl_take isl_pw_aff
*pwaff2
)
1959 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
1962 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
1963 __isl_take isl_pw_aff
*pwaff2
)
1965 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
1968 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1969 __isl_take isl_pw_aff
*pwaff2
)
1971 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
1974 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
1975 __isl_take isl_pw_aff
*pwaff2
)
1977 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
1980 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1981 __isl_take isl_pw_aff
*pwaff2
)
1985 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
1986 isl_pw_aff_copy(pwaff2
));
1987 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
1990 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
1991 __isl_take isl_pw_aff
*pwaff2
)
1993 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
1996 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
1997 __isl_take isl_pw_aff
*pwaff2
)
2001 le
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2002 isl_pw_aff_copy(pwaff2
));
2003 return isl_pw_aff_cond(le
, pwaff1
, pwaff2
);
2006 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2007 __isl_take isl_pw_aff
*pwaff2
)
2009 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2012 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2013 __isl_take isl_pw_aff_list
*list
,
2014 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2015 __isl_take isl_pw_aff
*pwaff2
))
2024 ctx
= isl_pw_aff_list_get_ctx(list
);
2026 isl_die(ctx
, isl_error_invalid
,
2027 "list should contain at least one element",
2028 return isl_pw_aff_list_free(list
));
2030 res
= isl_pw_aff_copy(list
->p
[0]);
2031 for (i
= 1; i
< list
->n
; ++i
)
2032 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2034 isl_pw_aff_list_free(list
);
2038 /* Return an isl_pw_aff that maps each element in the intersection of the
2039 * domains of the elements of list to the minimal corresponding affine
2042 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2044 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2047 /* Return an isl_pw_aff that maps each element in the intersection of the
2048 * domains of the elements of list to the maximal corresponding affine
2051 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2053 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2059 #include <isl_multi_templ.c>
2061 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2062 __isl_take isl_multi_aff
*maff2
)
2067 maff1
= isl_multi_aff_cow(maff1
);
2068 if (!maff1
|| !maff2
)
2071 ctx
= isl_multi_aff_get_ctx(maff1
);
2072 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2073 isl_die(ctx
, isl_error_invalid
,
2074 "spaces don't match", goto error
);
2076 for (i
= 0; i
< maff1
->n
; ++i
) {
2077 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2078 isl_aff_copy(maff2
->p
[i
]));
2083 isl_multi_aff_free(maff2
);
2086 isl_multi_aff_free(maff1
);
2087 isl_multi_aff_free(maff2
);
2091 /* Exploit the equalities in "eq" to simplify the affine expressions.
2093 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2094 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2098 maff
= isl_multi_aff_cow(maff
);
2102 for (i
= 0; i
< maff
->n
; ++i
) {
2103 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2104 isl_basic_set_copy(eq
));
2109 isl_basic_set_free(eq
);
2112 isl_basic_set_free(eq
);
2113 isl_multi_aff_free(maff
);
2117 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2122 maff
= isl_multi_aff_cow(maff
);
2126 for (i
= 0; i
< maff
->n
; ++i
) {
2127 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2129 return isl_multi_aff_free(maff
);
2135 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2136 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2138 maff1
= isl_multi_aff_add(maff1
, maff2
);
2139 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2143 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2151 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2152 __isl_keep isl_multi_aff
*maff2
)
2157 if (!maff1
|| !maff2
)
2159 if (maff1
->n
!= maff2
->n
)
2161 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2162 if (equal
< 0 || !equal
)
2165 for (i
= 0; i
< maff1
->n
; ++i
) {
2166 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2167 if (equal
< 0 || !equal
)
2174 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2175 __isl_take isl_multi_aff
*maff
,
2176 enum isl_dim_type type
, unsigned pos
, const char *s
)
2180 maff
= isl_multi_aff_cow(maff
);
2184 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2186 return isl_multi_aff_free(maff
);
2187 for (i
= 0; i
< maff
->n
; ++i
) {
2188 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2190 return isl_multi_aff_free(maff
);
2196 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2197 enum isl_dim_type type
, unsigned first
, unsigned n
)
2201 maff
= isl_multi_aff_cow(maff
);
2205 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2207 return isl_multi_aff_free(maff
);
2209 if (type
== isl_dim_out
) {
2210 for (i
= 0; i
< n
; ++i
)
2211 isl_aff_free(maff
->p
[first
+ i
]);
2212 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2213 maff
->p
[i
] = maff
->p
[i
+ n
];
2218 for (i
= 0; i
< maff
->n
; ++i
) {
2219 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2221 return isl_multi_aff_free(maff
);
2228 #define PW isl_pw_multi_aff
2230 #define EL isl_multi_aff
2232 #define EL_IS_ZERO is_empty
2236 #define IS_ZERO is_empty
2239 #undef DEFAULT_IS_ZERO
2240 #define DEFAULT_IS_ZERO 0
2245 #define NO_INVOLVES_DIMS
2246 #define NO_MOVE_DIMS
2247 #define NO_INSERT_DIMS
2251 #include <isl_pw_templ.c>
2253 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
2254 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2256 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
2257 &isl_multi_aff_add
);
2260 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
2261 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2263 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
2267 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
2268 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
2270 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
2273 /* Construct a map mapping the domain the piecewise multi-affine expression
2274 * to its range, with each dimension in the range equated to the
2275 * corresponding affine expression on its cell.
2277 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2285 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
2287 for (i
= 0; i
< pma
->n
; ++i
) {
2288 isl_multi_aff
*maff
;
2289 isl_basic_map
*bmap
;
2292 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
2293 bmap
= isl_basic_map_from_multi_aff(maff
);
2294 map_i
= isl_map_from_basic_map(bmap
);
2295 map_i
= isl_map_intersect_domain(map_i
,
2296 isl_set_copy(pma
->p
[i
].set
));
2297 map
= isl_map_union_disjoint(map
, map_i
);
2300 isl_pw_multi_aff_free(pma
);
2304 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
2306 if (!isl_space_is_set(pma
->dim
))
2307 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2308 "isl_pw_multi_aff cannot be converted into an isl_set",
2309 return isl_pw_multi_aff_free(pma
));
2311 return isl_map_from_pw_multi_aff(pma
);
2314 /* Given a basic map with a single output dimension that is defined
2315 * in terms of the parameters and input dimensions using an equality,
2316 * extract an isl_aff that expresses the output dimension in terms
2317 * of the parameters and input dimensions.
2319 * Since some applications expect the result of isl_pw_multi_aff_from_map
2320 * to only contain integer affine expressions, we compute the floor
2321 * of the expression before returning.
2323 * This function shares some similarities with
2324 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2326 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
2327 __isl_take isl_basic_map
*bmap
)
2332 isl_local_space
*ls
;
2337 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
2338 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2339 "basic map should have a single output dimension",
2341 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
2342 total
= isl_basic_map_total_dim(bmap
);
2343 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
2344 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
2346 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
2347 1 + total
- (offset
+ 1)) != -1)
2351 if (i
>= bmap
->n_eq
)
2352 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
2353 "unable to find suitable equality", goto error
);
2354 ls
= isl_basic_map_get_local_space(bmap
);
2355 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
2358 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
2359 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2361 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
2362 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
2363 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
2364 isl_basic_map_free(bmap
);
2366 aff
= isl_aff_remove_unused_divs(aff
);
2367 aff
= isl_aff_floor(aff
);
2370 isl_basic_map_free(bmap
);
2374 /* Given a basic map where each output dimension is defined
2375 * in terms of the parameters and input dimensions using an equality,
2376 * extract an isl_multi_aff that expresses the output dimensions in terms
2377 * of the parameters and input dimensions.
2379 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
2380 __isl_take isl_basic_map
*bmap
)
2389 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
2390 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
2392 for (i
= 0; i
< n_out
; ++i
) {
2393 isl_basic_map
*bmap_i
;
2396 bmap_i
= isl_basic_map_copy(bmap
);
2397 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
2398 i
+ 1, n_out
- (1 + i
));
2399 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
2400 aff
= extract_isl_aff_from_basic_map(bmap_i
);
2401 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
2404 isl_basic_map_free(bmap
);
2409 /* Create an isl_pw_multi_aff that is equivalent to
2410 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2411 * The given basic map is such that each output dimension is defined
2412 * in terms of the parameters and input dimensions using an equality.
2414 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
2415 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
2419 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
2420 return isl_pw_multi_aff_alloc(domain
, ma
);
2423 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2424 * This obivously only works if the input "map" is single-valued.
2425 * If so, we compute the lexicographic minimum of the image in the form
2426 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2427 * to its lexicographic minimum.
2428 * If the input is not single-valued, we produce an error.
2430 * As a special case, we first check if all output dimensions are uniquely
2431 * defined in terms of the parameters and input dimensions over the entire
2432 * domain. If so, we extract the desired isl_pw_multi_aff directly
2433 * from the affine hull of "map" and its domain.
2435 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
2439 isl_pw_multi_aff
*pma
;
2440 isl_basic_map
*hull
;
2445 hull
= isl_map_affine_hull(isl_map_copy(map
));
2446 sv
= isl_basic_map_plain_is_single_valued(hull
);
2448 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
2449 isl_basic_map_free(hull
);
2453 sv
= isl_map_is_single_valued(map
);
2457 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
2458 "map is not single-valued", goto error
);
2459 map
= isl_map_make_disjoint(map
);
2463 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
2465 for (i
= 0; i
< map
->n
; ++i
) {
2466 isl_pw_multi_aff
*pma_i
;
2467 isl_basic_map
*bmap
;
2468 bmap
= isl_basic_map_copy(map
->p
[i
]);
2469 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
2470 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
2480 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
2482 return isl_pw_multi_aff_from_map(set
);
2485 /* Plug in "subs" for dimension "type", "pos" of "aff".
2487 * Let i be the dimension to replace and let "subs" be of the form
2491 * and "aff" of the form
2497 * floor((a f + d g')/(m d))
2499 * where g' is the result of plugging in "subs" in each of the integer
2502 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
2503 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
2508 aff
= isl_aff_cow(aff
);
2510 return isl_aff_free(aff
);
2512 ctx
= isl_aff_get_ctx(aff
);
2513 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
2514 isl_die(ctx
, isl_error_invalid
,
2515 "spaces don't match", return isl_aff_free(aff
));
2516 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
2517 isl_die(ctx
, isl_error_unsupported
,
2518 "cannot handle divs yet", return isl_aff_free(aff
));
2520 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
2522 return isl_aff_free(aff
);
2524 aff
->v
= isl_vec_cow(aff
->v
);
2526 return isl_aff_free(aff
);
2528 pos
+= isl_local_space_offset(aff
->ls
, type
);
2531 isl_int_set(v
, aff
->v
->el
[1 + pos
]);
2532 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
2533 isl_seq_combine(aff
->v
->el
+ 1, subs
->v
->el
[0], aff
->v
->el
+ 1,
2534 v
, subs
->v
->el
+ 1, subs
->v
->size
- 1);
2535 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], subs
->v
->el
[0]);
2541 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2542 * expressions in "maff".
2544 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
2545 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
2546 __isl_keep isl_aff
*subs
)
2550 maff
= isl_multi_aff_cow(maff
);
2552 return isl_multi_aff_free(maff
);
2554 if (type
== isl_dim_in
)
2557 for (i
= 0; i
< maff
->n
; ++i
) {
2558 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
2560 return isl_multi_aff_free(maff
);
2566 /* Plug in "subs" for dimension "type", "pos" of "pma".
2568 * pma is of the form
2572 * while subs is of the form
2574 * v' = B_j(v) -> S_j
2576 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2577 * has a contribution in the result, in particular
2579 * C_ij(S_j) -> M_i(S_j)
2581 * Note that plugging in S_j in C_ij may also result in an empty set
2582 * and this contribution should simply be discarded.
2584 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
2585 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
2586 __isl_keep isl_pw_aff
*subs
)
2589 isl_pw_multi_aff
*res
;
2592 return isl_pw_multi_aff_free(pma
);
2594 n
= pma
->n
* subs
->n
;
2595 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
2597 for (i
= 0; i
< pma
->n
; ++i
) {
2598 for (j
= 0; j
< subs
->n
; ++j
) {
2600 isl_multi_aff
*res_ij
;
2601 common
= isl_set_intersect(
2602 isl_set_copy(pma
->p
[i
].set
),
2603 isl_set_copy(subs
->p
[j
].set
));
2604 common
= isl_set_substitute(common
,
2605 type
, pos
, subs
->p
[j
].aff
);
2606 if (isl_set_plain_is_empty(common
)) {
2607 isl_set_free(common
);
2611 res_ij
= isl_multi_aff_substitute(
2612 isl_multi_aff_copy(pma
->p
[i
].maff
),
2613 type
, pos
, subs
->p
[j
].aff
);
2615 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
2619 isl_pw_multi_aff_free(pma
);
2623 /* Extend the local space of "dst" to include the divs
2624 * in the local space of "src".
2626 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
2627 __isl_keep isl_aff
*src
)
2635 return isl_aff_free(dst
);
2637 ctx
= isl_aff_get_ctx(src
);
2638 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
2639 isl_die(ctx
, isl_error_invalid
,
2640 "spaces don't match", goto error
);
2642 if (src
->ls
->div
->n_row
== 0)
2645 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
2646 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
2650 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
2651 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
2659 return isl_aff_free(dst
);
2662 /* Adjust the local spaces of the affine expressions in "maff"
2663 * such that they all have the save divs.
2665 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
2666 __isl_take isl_multi_aff
*maff
)
2674 maff
= isl_multi_aff_cow(maff
);
2678 for (i
= 1; i
< maff
->n
; ++i
)
2679 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
2680 for (i
= 1; i
< maff
->n
; ++i
) {
2681 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
2683 return isl_multi_aff_free(maff
);
2689 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
2691 aff
= isl_aff_cow(aff
);
2695 aff
->ls
= isl_local_space_lift(aff
->ls
);
2697 return isl_aff_free(aff
);
2702 /* Lift "maff" to a space with extra dimensions such that the result
2703 * has no more existentially quantified variables.
2704 * If "ls" is not NULL, then *ls is assigned the local space that lies
2705 * at the basis of the lifting applied to "maff".
2707 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
2708 __isl_give isl_local_space
**ls
)
2722 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
2723 *ls
= isl_local_space_from_space(space
);
2725 return isl_multi_aff_free(maff
);
2730 maff
= isl_multi_aff_cow(maff
);
2731 maff
= isl_multi_aff_align_divs(maff
);
2735 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
2736 space
= isl_multi_aff_get_space(maff
);
2737 space
= isl_space_lift(isl_space_domain(space
), n_div
);
2738 space
= isl_space_extend_domain_with_range(space
,
2739 isl_multi_aff_get_space(maff
));
2741 return isl_multi_aff_free(maff
);
2742 isl_space_free(maff
->space
);
2743 maff
->space
= space
;
2746 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
2748 return isl_multi_aff_free(maff
);
2751 for (i
= 0; i
< maff
->n
; ++i
) {
2752 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
2760 isl_local_space_free(*ls
);
2761 return isl_multi_aff_free(maff
);
2765 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2767 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
2768 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
2778 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
2779 if (pos
< 0 || pos
>= n_out
)
2780 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
2781 "index out of bounds", return NULL
);
2783 space
= isl_pw_multi_aff_get_space(pma
);
2784 space
= isl_space_drop_dims(space
, isl_dim_out
,
2785 pos
+ 1, n_out
- pos
- 1);
2786 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
2788 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
2789 for (i
= 0; i
< pma
->n
; ++i
) {
2791 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
2792 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);