2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
33 #include <isl_list_templ.c>
38 #include <isl_list_templ.c>
40 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
41 __isl_take isl_vec
*v
)
48 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
58 isl_local_space_free(ls
);
63 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
72 ctx
= isl_local_space_get_ctx(ls
);
73 if (!isl_local_space_divs_known(ls
))
74 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
76 if (!isl_local_space_is_set(ls
))
77 isl_die(ctx
, isl_error_invalid
,
78 "domain of affine expression should be a set",
81 total
= isl_local_space_dim(ls
, isl_dim_all
);
82 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
83 return isl_aff_alloc_vec(ls
, v
);
85 isl_local_space_free(ls
);
89 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
93 aff
= isl_aff_alloc(ls
);
97 isl_int_set_si(aff
->v
->el
[0], 1);
98 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
111 /* Return an affine expression that is equal to "val" on
112 * domain local space "ls".
114 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
115 __isl_take isl_val
*val
)
121 if (!isl_val_is_rat(val
))
122 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
123 "expecting rational value", goto error
);
125 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
129 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
130 isl_int_set(aff
->v
->el
[1], val
->n
);
131 isl_int_set(aff
->v
->el
[0], val
->d
);
133 isl_local_space_free(ls
);
137 isl_local_space_free(ls
);
142 /* Return an affine expression that is equal to the specified dimension
145 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
146 enum isl_dim_type type
, unsigned pos
)
154 space
= isl_local_space_get_space(ls
);
157 if (isl_space_is_map(space
))
158 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
159 "expecting (parameter) set space", goto error
);
160 if (pos
>= isl_local_space_dim(ls
, type
))
161 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
162 "position out of bounds", goto error
);
164 isl_space_free(space
);
165 aff
= isl_aff_alloc(ls
);
169 pos
+= isl_local_space_offset(aff
->ls
, type
);
171 isl_int_set_si(aff
->v
->el
[0], 1);
172 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
173 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
177 isl_local_space_free(ls
);
178 isl_space_free(space
);
182 /* Return a piecewise affine expression that is equal to
183 * the specified dimension in "ls".
185 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
186 enum isl_dim_type type
, unsigned pos
)
188 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
191 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
200 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
205 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
206 isl_vec_copy(aff
->v
));
209 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
217 return isl_aff_dup(aff
);
220 void *isl_aff_free(__isl_take isl_aff
*aff
)
228 isl_local_space_free(aff
->ls
);
229 isl_vec_free(aff
->v
);
236 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
238 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
241 /* Externally, an isl_aff has a map space, but internally, the
242 * ls field corresponds to the domain of that space.
244 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
248 if (type
== isl_dim_out
)
250 if (type
== isl_dim_in
)
252 return isl_local_space_dim(aff
->ls
, type
);
255 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
257 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
260 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
265 space
= isl_local_space_get_space(aff
->ls
);
266 space
= isl_space_from_domain(space
);
267 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
271 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
272 __isl_keep isl_aff
*aff
)
274 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
277 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
282 ls
= isl_local_space_copy(aff
->ls
);
283 ls
= isl_local_space_from_domain(ls
);
284 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
288 /* Externally, an isl_aff has a map space, but internally, the
289 * ls field corresponds to the domain of that space.
291 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
292 enum isl_dim_type type
, unsigned pos
)
296 if (type
== isl_dim_out
)
298 if (type
== isl_dim_in
)
300 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
303 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
304 __isl_take isl_space
*dim
)
306 aff
= isl_aff_cow(aff
);
310 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
312 return isl_aff_free(aff
);
321 /* Reset the space of "aff". This function is called from isl_pw_templ.c
322 * and doesn't know if the space of an element object is represented
323 * directly or through its domain. It therefore passes along both.
325 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
326 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
328 isl_space_free(space
);
329 return isl_aff_reset_domain_space(aff
, domain
);
332 /* Reorder the coefficients of the affine expression based
333 * on the given reodering.
334 * The reordering r is assumed to have been extended with the local
337 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
338 __isl_take isl_reordering
*r
, int n_div
)
346 res
= isl_vec_alloc(vec
->ctx
,
347 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
348 isl_seq_cpy(res
->el
, vec
->el
, 2);
349 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
350 for (i
= 0; i
< r
->len
; ++i
)
351 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
353 isl_reordering_free(r
);
358 isl_reordering_free(r
);
362 /* Reorder the dimensions of the domain of "aff" according
363 * to the given reordering.
365 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
366 __isl_take isl_reordering
*r
)
368 aff
= isl_aff_cow(aff
);
372 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
373 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
374 aff
->ls
->div
->n_row
);
375 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
377 if (!aff
->v
|| !aff
->ls
)
378 return isl_aff_free(aff
);
383 isl_reordering_free(r
);
387 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
388 __isl_take isl_space
*model
)
393 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
394 model
, isl_dim_param
)) {
397 model
= isl_space_drop_dims(model
, isl_dim_in
,
398 0, isl_space_dim(model
, isl_dim_in
));
399 model
= isl_space_drop_dims(model
, isl_dim_out
,
400 0, isl_space_dim(model
, isl_dim_out
));
401 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
402 exp
= isl_reordering_extend_space(exp
,
403 isl_aff_get_domain_space(aff
));
404 aff
= isl_aff_realign_domain(aff
, exp
);
407 isl_space_free(model
);
410 isl_space_free(model
);
415 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
420 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
423 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
430 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
431 if (equal
< 0 || !equal
)
434 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
437 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
441 isl_int_set(*v
, aff
->v
->el
[0]);
445 /* Return the common denominator of "aff".
447 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
454 ctx
= isl_aff_get_ctx(aff
);
455 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
458 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
462 isl_int_set(*v
, aff
->v
->el
[1]);
466 /* Return the constant term of "aff".
468 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
476 ctx
= isl_aff_get_ctx(aff
);
477 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
478 return isl_val_normalize(v
);
481 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
482 enum isl_dim_type type
, int pos
, isl_int
*v
)
487 if (type
== isl_dim_out
)
488 isl_die(aff
->v
->ctx
, isl_error_invalid
,
489 "output/set dimension does not have a coefficient",
491 if (type
== isl_dim_in
)
494 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
495 isl_die(aff
->v
->ctx
, isl_error_invalid
,
496 "position out of bounds", return -1);
498 pos
+= isl_local_space_offset(aff
->ls
, type
);
499 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
504 /* Return the coefficient of the variable of type "type" at position "pos"
507 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
508 enum isl_dim_type type
, int pos
)
516 ctx
= isl_aff_get_ctx(aff
);
517 if (type
== isl_dim_out
)
518 isl_die(ctx
, isl_error_invalid
,
519 "output/set dimension does not have a coefficient",
521 if (type
== isl_dim_in
)
524 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
525 isl_die(ctx
, isl_error_invalid
,
526 "position out of bounds", return NULL
);
528 pos
+= isl_local_space_offset(aff
->ls
, type
);
529 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
530 return isl_val_normalize(v
);
533 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
535 aff
= isl_aff_cow(aff
);
539 aff
->v
= isl_vec_cow(aff
->v
);
541 return isl_aff_free(aff
);
543 isl_int_set(aff
->v
->el
[0], v
);
548 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
550 aff
= isl_aff_cow(aff
);
554 aff
->v
= isl_vec_cow(aff
->v
);
556 return isl_aff_free(aff
);
558 isl_int_set(aff
->v
->el
[1], v
);
563 /* Replace the constant term of "aff" by "v".
565 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
566 __isl_take isl_val
*v
)
571 if (!isl_val_is_rat(v
))
572 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
573 "expecting rational value", goto error
);
575 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
576 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
581 aff
= isl_aff_cow(aff
);
584 aff
->v
= isl_vec_cow(aff
->v
);
588 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
589 isl_int_set(aff
->v
->el
[1], v
->n
);
590 } else if (isl_int_is_one(v
->d
)) {
591 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
593 isl_seq_scale(aff
->v
->el
+ 1,
594 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
595 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
596 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
597 aff
->v
= isl_vec_normalize(aff
->v
);
610 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
612 if (isl_int_is_zero(v
))
615 aff
= isl_aff_cow(aff
);
619 aff
->v
= isl_vec_cow(aff
->v
);
621 return isl_aff_free(aff
);
623 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
628 /* Add "v" to the constant term of "aff".
630 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
631 __isl_take isl_val
*v
)
636 if (isl_val_is_zero(v
)) {
641 if (!isl_val_is_rat(v
))
642 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
643 "expecting rational value", goto error
);
645 aff
= isl_aff_cow(aff
);
649 aff
->v
= isl_vec_cow(aff
->v
);
653 if (isl_int_is_one(v
->d
)) {
654 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
655 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
656 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
657 aff
->v
= isl_vec_normalize(aff
->v
);
661 isl_seq_scale(aff
->v
->el
+ 1,
662 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
663 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
664 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
665 aff
->v
= isl_vec_normalize(aff
->v
);
678 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
683 isl_int_set_si(t
, v
);
684 aff
= isl_aff_add_constant(aff
, t
);
690 /* Add "v" to the numerator of the constant term of "aff".
692 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
694 if (isl_int_is_zero(v
))
697 aff
= isl_aff_cow(aff
);
701 aff
->v
= isl_vec_cow(aff
->v
);
703 return isl_aff_free(aff
);
705 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
710 /* Add "v" to the numerator of the constant term of "aff".
712 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
720 isl_int_set_si(t
, v
);
721 aff
= isl_aff_add_constant_num(aff
, t
);
727 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
729 aff
= isl_aff_cow(aff
);
733 aff
->v
= isl_vec_cow(aff
->v
);
735 return isl_aff_free(aff
);
737 isl_int_set_si(aff
->v
->el
[1], v
);
742 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
743 enum isl_dim_type type
, int pos
, isl_int v
)
748 if (type
== isl_dim_out
)
749 isl_die(aff
->v
->ctx
, isl_error_invalid
,
750 "output/set dimension does not have a coefficient",
751 return isl_aff_free(aff
));
752 if (type
== isl_dim_in
)
755 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
756 isl_die(aff
->v
->ctx
, isl_error_invalid
,
757 "position out of bounds", return isl_aff_free(aff
));
759 aff
= isl_aff_cow(aff
);
763 aff
->v
= isl_vec_cow(aff
->v
);
765 return isl_aff_free(aff
);
767 pos
+= isl_local_space_offset(aff
->ls
, type
);
768 isl_int_set(aff
->v
->el
[1 + pos
], v
);
773 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
774 enum isl_dim_type type
, int pos
, int v
)
779 if (type
== isl_dim_out
)
780 isl_die(aff
->v
->ctx
, isl_error_invalid
,
781 "output/set dimension does not have a coefficient",
782 return isl_aff_free(aff
));
783 if (type
== isl_dim_in
)
786 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
787 isl_die(aff
->v
->ctx
, isl_error_invalid
,
788 "position out of bounds", return isl_aff_free(aff
));
790 aff
= isl_aff_cow(aff
);
794 aff
->v
= isl_vec_cow(aff
->v
);
796 return isl_aff_free(aff
);
798 pos
+= isl_local_space_offset(aff
->ls
, type
);
799 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
804 /* Replace the coefficient of the variable of type "type" at position "pos"
807 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
808 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
813 if (type
== isl_dim_out
)
814 isl_die(aff
->v
->ctx
, isl_error_invalid
,
815 "output/set dimension does not have a coefficient",
817 if (type
== isl_dim_in
)
820 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
821 isl_die(aff
->v
->ctx
, isl_error_invalid
,
822 "position out of bounds", goto error
);
824 if (!isl_val_is_rat(v
))
825 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
826 "expecting rational value", goto error
);
828 pos
+= isl_local_space_offset(aff
->ls
, type
);
829 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
830 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
835 aff
= isl_aff_cow(aff
);
838 aff
->v
= isl_vec_cow(aff
->v
);
842 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
843 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
844 } else if (isl_int_is_one(v
->d
)) {
845 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
847 isl_seq_scale(aff
->v
->el
+ 1,
848 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
849 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
850 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
851 aff
->v
= isl_vec_normalize(aff
->v
);
864 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
865 enum isl_dim_type type
, int pos
, isl_int v
)
870 if (type
== isl_dim_out
)
871 isl_die(aff
->v
->ctx
, isl_error_invalid
,
872 "output/set dimension does not have a coefficient",
873 return isl_aff_free(aff
));
874 if (type
== isl_dim_in
)
877 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
878 isl_die(aff
->v
->ctx
, isl_error_invalid
,
879 "position out of bounds", return isl_aff_free(aff
));
881 aff
= isl_aff_cow(aff
);
885 aff
->v
= isl_vec_cow(aff
->v
);
887 return isl_aff_free(aff
);
889 pos
+= isl_local_space_offset(aff
->ls
, type
);
890 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
895 /* Add "v" to the coefficient of the variable of type "type"
896 * at position "pos" of "aff".
898 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
899 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
904 if (isl_val_is_zero(v
)) {
909 if (type
== isl_dim_out
)
910 isl_die(aff
->v
->ctx
, isl_error_invalid
,
911 "output/set dimension does not have a coefficient",
913 if (type
== isl_dim_in
)
916 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
917 isl_die(aff
->v
->ctx
, isl_error_invalid
,
918 "position out of bounds", goto error
);
920 if (!isl_val_is_rat(v
))
921 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
922 "expecting rational value", goto error
);
924 aff
= isl_aff_cow(aff
);
928 aff
->v
= isl_vec_cow(aff
->v
);
932 pos
+= isl_local_space_offset(aff
->ls
, type
);
933 if (isl_int_is_one(v
->d
)) {
934 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
935 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
936 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
937 aff
->v
= isl_vec_normalize(aff
->v
);
941 isl_seq_scale(aff
->v
->el
+ 1,
942 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
943 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
944 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
945 aff
->v
= isl_vec_normalize(aff
->v
);
958 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
959 enum isl_dim_type type
, int pos
, int v
)
964 isl_int_set_si(t
, v
);
965 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
971 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
976 return isl_local_space_get_div(aff
->ls
, pos
);
979 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
981 aff
= isl_aff_cow(aff
);
984 aff
->v
= isl_vec_cow(aff
->v
);
986 return isl_aff_free(aff
);
988 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
993 /* Remove divs from the local space that do not appear in the affine
995 * We currently only remove divs at the end.
996 * Some intermediate divs may also not appear directly in the affine
997 * expression, but we would also need to check that no other divs are
998 * defined in terms of them.
1000 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1009 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1010 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1012 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1016 aff
= isl_aff_cow(aff
);
1020 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1021 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1022 if (!aff
->ls
|| !aff
->v
)
1023 return isl_aff_free(aff
);
1028 /* Given two affine expressions "p" of length p_len (including the
1029 * denominator and the constant term) and "subs" of length subs_len,
1030 * plug in "subs" for the variable at position "pos".
1031 * The variables of "subs" and "p" are assumed to match up to subs_len,
1032 * but "p" may have additional variables.
1033 * "v" is an initialized isl_int that can be used internally.
1035 * In particular, if "p" represents the expression
1039 * with i the variable at position "pos" and "subs" represents the expression
1043 * then the result represents the expression
1048 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1049 int p_len
, int subs_len
, isl_int v
)
1051 isl_int_set(v
, p
[1 + pos
]);
1052 isl_int_set_si(p
[1 + pos
], 0);
1053 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1054 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1055 isl_int_mul(p
[0], p
[0], subs
[0]);
1058 /* Look for any divs in the aff->ls with a denominator equal to one
1059 * and plug them into the affine expression and any subsequent divs
1060 * that may reference the div.
1062 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1068 isl_local_space
*ls
;
1074 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1076 for (i
= 0; i
< n
; ++i
) {
1077 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1079 ls
= isl_local_space_copy(aff
->ls
);
1080 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1081 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1082 vec
= isl_vec_copy(aff
->v
);
1083 vec
= isl_vec_cow(vec
);
1089 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1090 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1095 isl_vec_free(aff
->v
);
1097 isl_local_space_free(aff
->ls
);
1104 isl_local_space_free(ls
);
1105 return isl_aff_free(aff
);
1108 /* Look for any divs j that appear with a unit coefficient inside
1109 * the definitions of other divs i and plug them into the definitions
1112 * In particular, an expression of the form
1114 * floor((f(..) + floor(g(..)/n))/m)
1118 * floor((n * f(..) + g(..))/(n * m))
1120 * This simplification is correct because we can move the expression
1121 * f(..) into the inner floor in the original expression to obtain
1123 * floor(floor((n * f(..) + g(..))/n)/m)
1125 * from which we can derive the simplified expression.
1127 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1135 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1136 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1137 for (i
= 1; i
< n
; ++i
) {
1138 for (j
= 0; j
< i
; ++j
) {
1139 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1141 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1142 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1143 aff
->v
->size
, i
, 1);
1145 return isl_aff_free(aff
);
1152 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1154 * Even though this function is only called on isl_affs with a single
1155 * reference, we are careful to only change aff->v and aff->ls together.
1157 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1159 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1160 isl_local_space
*ls
;
1163 ls
= isl_local_space_copy(aff
->ls
);
1164 ls
= isl_local_space_swap_div(ls
, a
, b
);
1165 v
= isl_vec_copy(aff
->v
);
1170 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1171 isl_vec_free(aff
->v
);
1173 isl_local_space_free(aff
->ls
);
1179 isl_local_space_free(ls
);
1180 return isl_aff_free(aff
);
1183 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1185 * We currently do not actually remove div "b", but simply add its
1186 * coefficient to that of "a" and then zero it out.
1188 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1190 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1192 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1195 aff
->v
= isl_vec_cow(aff
->v
);
1197 return isl_aff_free(aff
);
1199 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1200 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1201 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1206 /* Sort the divs in the local space of "aff" according to
1207 * the comparison function "cmp_row" in isl_local_space.c,
1208 * combining the coefficients of identical divs.
1210 * Reordering divs does not change the semantics of "aff",
1211 * so there is no need to call isl_aff_cow.
1212 * Moreover, this function is currently only called on isl_affs
1213 * with a single reference.
1215 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1223 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1224 n
= isl_aff_dim(aff
, isl_dim_div
);
1225 for (i
= 1; i
< n
; ++i
) {
1226 for (j
= i
- 1; j
>= 0; --j
) {
1227 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1231 aff
= merge_divs(aff
, j
, j
+ 1);
1233 aff
= swap_div(aff
, j
, j
+ 1);
1242 /* Normalize the representation of "aff".
1244 * This function should only be called of "new" isl_affs, i.e.,
1245 * with only a single reference. We therefore do not need to
1246 * worry about affecting other instances.
1248 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1252 aff
->v
= isl_vec_normalize(aff
->v
);
1254 return isl_aff_free(aff
);
1255 aff
= plug_in_integral_divs(aff
);
1256 aff
= plug_in_unit_divs(aff
);
1257 aff
= sort_divs(aff
);
1258 aff
= isl_aff_remove_unused_divs(aff
);
1262 /* Given f, return floor(f).
1263 * If f is an integer expression, then just return f.
1264 * If f is a constant, then return the constant floor(f).
1265 * Otherwise, if f = g/m, write g = q m + r,
1266 * create a new div d = [r/m] and return the expression q + d.
1267 * The coefficients in r are taken to lie between -m/2 and m/2.
1269 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1279 if (isl_int_is_one(aff
->v
->el
[0]))
1282 aff
= isl_aff_cow(aff
);
1286 aff
->v
= isl_vec_cow(aff
->v
);
1288 return isl_aff_free(aff
);
1290 if (isl_aff_is_cst(aff
)) {
1291 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1292 isl_int_set_si(aff
->v
->el
[0], 1);
1296 div
= isl_vec_copy(aff
->v
);
1297 div
= isl_vec_cow(div
);
1299 return isl_aff_free(aff
);
1301 ctx
= isl_aff_get_ctx(aff
);
1302 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1303 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1304 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1305 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1306 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1307 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1308 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1312 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1314 return isl_aff_free(aff
);
1316 size
= aff
->v
->size
;
1317 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1319 return isl_aff_free(aff
);
1320 isl_int_set_si(aff
->v
->el
[0], 1);
1321 isl_int_set_si(aff
->v
->el
[size
], 1);
1323 aff
= isl_aff_normalize(aff
);
1330 * aff mod m = aff - m * floor(aff/m)
1332 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1336 res
= isl_aff_copy(aff
);
1337 aff
= isl_aff_scale_down(aff
, m
);
1338 aff
= isl_aff_floor(aff
);
1339 aff
= isl_aff_scale(aff
, m
);
1340 res
= isl_aff_sub(res
, aff
);
1347 * aff mod m = aff - m * floor(aff/m)
1349 * with m an integer value.
1351 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1352 __isl_take isl_val
*m
)
1359 if (!isl_val_is_int(m
))
1360 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1361 "expecting integer modulo", goto error
);
1363 res
= isl_aff_copy(aff
);
1364 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1365 aff
= isl_aff_floor(aff
);
1366 aff
= isl_aff_scale_val(aff
, m
);
1367 res
= isl_aff_sub(res
, aff
);
1378 * pwaff mod m = pwaff - m * floor(pwaff/m)
1380 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1384 res
= isl_pw_aff_copy(pwaff
);
1385 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1386 pwaff
= isl_pw_aff_floor(pwaff
);
1387 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1388 res
= isl_pw_aff_sub(res
, pwaff
);
1395 * pa mod m = pa - m * floor(pa/m)
1397 * with m an integer value.
1399 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1400 __isl_take isl_val
*m
)
1404 if (!isl_val_is_int(m
))
1405 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1406 "expecting integer modulo", goto error
);
1407 pa
= isl_pw_aff_mod(pa
, m
->n
);
1411 isl_pw_aff_free(pa
);
1416 /* Given f, return ceil(f).
1417 * If f is an integer expression, then just return f.
1418 * Otherwise, let f be the expression
1424 * floor((e + m - 1)/m)
1426 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1431 if (isl_int_is_one(aff
->v
->el
[0]))
1434 aff
= isl_aff_cow(aff
);
1437 aff
->v
= isl_vec_cow(aff
->v
);
1439 return isl_aff_free(aff
);
1441 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1442 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1443 aff
= isl_aff_floor(aff
);
1448 /* Apply the expansion computed by isl_merge_divs.
1449 * The expansion itself is given by "exp" while the resulting
1450 * list of divs is given by "div".
1452 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1453 __isl_take isl_mat
*div
, int *exp
)
1460 aff
= isl_aff_cow(aff
);
1464 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1465 new_n_div
= isl_mat_rows(div
);
1466 if (new_n_div
< old_n_div
)
1467 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1468 "not an expansion", goto error
);
1470 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1474 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1476 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1477 if (j
>= 0 && exp
[j
] == i
) {
1479 isl_int_swap(aff
->v
->el
[offset
+ i
],
1480 aff
->v
->el
[offset
+ j
]);
1483 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1486 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1497 /* Add two affine expressions that live in the same local space.
1499 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1500 __isl_take isl_aff
*aff2
)
1504 aff1
= isl_aff_cow(aff1
);
1508 aff1
->v
= isl_vec_cow(aff1
->v
);
1514 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1515 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1516 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1517 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1518 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1519 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1520 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1532 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1533 __isl_take isl_aff
*aff2
)
1544 ctx
= isl_aff_get_ctx(aff1
);
1545 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1546 isl_die(ctx
, isl_error_invalid
,
1547 "spaces don't match", goto error
);
1549 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1550 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1551 if (n_div1
== 0 && n_div2
== 0)
1552 return add_expanded(aff1
, aff2
);
1554 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1555 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1556 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1559 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1560 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1561 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1565 return add_expanded(aff1
, aff2
);
1574 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1575 __isl_take isl_aff
*aff2
)
1577 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1580 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1584 if (isl_int_is_one(f
))
1587 aff
= isl_aff_cow(aff
);
1590 aff
->v
= isl_vec_cow(aff
->v
);
1592 return isl_aff_free(aff
);
1594 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1595 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1600 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1601 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1602 isl_int_divexact(gcd
, f
, gcd
);
1603 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1609 /* Multiple "aff" by "v".
1611 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1612 __isl_take isl_val
*v
)
1617 if (isl_val_is_one(v
)) {
1622 if (!isl_val_is_rat(v
))
1623 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1624 "expecting rational factor", goto error
);
1626 aff
= isl_aff_scale(aff
, v
->n
);
1627 aff
= isl_aff_scale_down(aff
, v
->d
);
1637 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1641 if (isl_int_is_one(f
))
1644 aff
= isl_aff_cow(aff
);
1648 if (isl_int_is_zero(f
))
1649 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1650 "cannot scale down by zero", return isl_aff_free(aff
));
1652 aff
->v
= isl_vec_cow(aff
->v
);
1654 return isl_aff_free(aff
);
1657 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1658 isl_int_gcd(gcd
, gcd
, f
);
1659 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1660 isl_int_divexact(gcd
, f
, gcd
);
1661 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1667 /* Divide "aff" by "v".
1669 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1670 __isl_take isl_val
*v
)
1675 if (isl_val_is_one(v
)) {
1680 if (!isl_val_is_rat(v
))
1681 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1682 "expecting rational factor", goto error
);
1683 if (!isl_val_is_pos(v
))
1684 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1685 "factor needs to be positive", goto error
);
1687 aff
= isl_aff_scale(aff
, v
->d
);
1688 aff
= isl_aff_scale_down(aff
, v
->n
);
1698 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1706 isl_int_set_ui(v
, f
);
1707 aff
= isl_aff_scale_down(aff
, v
);
1713 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1714 enum isl_dim_type type
, unsigned pos
, const char *s
)
1716 aff
= isl_aff_cow(aff
);
1719 if (type
== isl_dim_out
)
1720 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1721 "cannot set name of output/set dimension",
1722 return isl_aff_free(aff
));
1723 if (type
== isl_dim_in
)
1725 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1727 return isl_aff_free(aff
);
1732 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1733 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1735 aff
= isl_aff_cow(aff
);
1737 return isl_id_free(id
);
1738 if (type
== isl_dim_out
)
1739 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1740 "cannot set name of output/set dimension",
1742 if (type
== isl_dim_in
)
1744 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1746 return isl_aff_free(aff
);
1755 /* Replace the identifier of the input tuple of "aff" by "id".
1756 * type is currently required to be equal to isl_dim_in
1758 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1759 enum isl_dim_type type
, __isl_take isl_id
*id
)
1761 aff
= isl_aff_cow(aff
);
1763 return isl_id_free(id
);
1764 if (type
!= isl_dim_out
)
1765 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1766 "cannot only set id of input tuple", goto error
);
1767 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
1769 return isl_aff_free(aff
);
1778 /* Exploit the equalities in "eq" to simplify the affine expression
1779 * and the expressions of the integer divisions in the local space.
1780 * The integer divisions in this local space are assumed to appear
1781 * as regular dimensions in "eq".
1783 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1784 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1792 if (eq
->n_eq
== 0) {
1793 isl_basic_set_free(eq
);
1797 aff
= isl_aff_cow(aff
);
1801 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1802 isl_basic_set_copy(eq
));
1803 aff
->v
= isl_vec_cow(aff
->v
);
1804 if (!aff
->ls
|| !aff
->v
)
1807 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1809 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1810 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1811 if (j
< 0 || j
== 0 || j
>= total
)
1814 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1818 isl_basic_set_free(eq
);
1819 aff
= isl_aff_normalize(aff
);
1822 isl_basic_set_free(eq
);
1827 /* Exploit the equalities in "eq" to simplify the affine expression
1828 * and the expressions of the integer divisions in the local space.
1830 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1831 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1837 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1839 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
1840 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1842 isl_basic_set_free(eq
);
1847 /* Look for equalities among the variables shared by context and aff
1848 * and the integer divisions of aff, if any.
1849 * The equalities are then used to eliminate coefficients and/or integer
1850 * divisions from aff.
1852 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1853 __isl_take isl_set
*context
)
1855 isl_basic_set
*hull
;
1860 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1862 isl_basic_set
*bset
;
1863 isl_local_space
*ls
;
1864 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1865 ls
= isl_aff_get_domain_local_space(aff
);
1866 bset
= isl_basic_set_from_local_space(ls
);
1867 bset
= isl_basic_set_lift(bset
);
1868 bset
= isl_basic_set_flatten(bset
);
1869 context
= isl_set_intersect(context
,
1870 isl_set_from_basic_set(bset
));
1873 hull
= isl_set_affine_hull(context
);
1874 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1877 isl_set_free(context
);
1881 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1882 __isl_take isl_set
*context
)
1884 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1885 dom_context
= isl_set_intersect_params(dom_context
, context
);
1886 return isl_aff_gist(aff
, dom_context
);
1889 /* Return a basic set containing those elements in the space
1890 * of aff where it is non-negative.
1891 * If "rational" is set, then return a rational basic set.
1893 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
1894 __isl_take isl_aff
*aff
, int rational
)
1896 isl_constraint
*ineq
;
1897 isl_basic_set
*bset
;
1899 ineq
= isl_inequality_from_aff(aff
);
1901 bset
= isl_basic_set_from_constraint(ineq
);
1903 bset
= isl_basic_set_set_rational(bset
);
1904 bset
= isl_basic_set_simplify(bset
);
1908 /* Return a basic set containing those elements in the space
1909 * of aff where it is non-negative.
1911 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1913 return aff_nonneg_basic_set(aff
, 0);
1916 /* Return a basic set containing those elements in the domain space
1917 * of aff where it is negative.
1919 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
1921 aff
= isl_aff_neg(aff
);
1922 aff
= isl_aff_add_constant_num_si(aff
, -1);
1923 return isl_aff_nonneg_basic_set(aff
);
1926 /* Return a basic set containing those elements in the space
1927 * of aff where it is zero.
1928 * If "rational" is set, then return a rational basic set.
1930 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
1933 isl_constraint
*ineq
;
1934 isl_basic_set
*bset
;
1936 ineq
= isl_equality_from_aff(aff
);
1938 bset
= isl_basic_set_from_constraint(ineq
);
1940 bset
= isl_basic_set_set_rational(bset
);
1941 bset
= isl_basic_set_simplify(bset
);
1945 /* Return a basic set containing those elements in the space
1946 * of aff where it is zero.
1948 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1950 return aff_zero_basic_set(aff
, 0);
1953 /* Return a basic set containing those elements in the shared space
1954 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1956 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1957 __isl_take isl_aff
*aff2
)
1959 aff1
= isl_aff_sub(aff1
, aff2
);
1961 return isl_aff_nonneg_basic_set(aff1
);
1964 /* Return a basic set containing those elements in the shared space
1965 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1967 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1968 __isl_take isl_aff
*aff2
)
1970 return isl_aff_ge_basic_set(aff2
, aff1
);
1973 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1974 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1976 aff1
= isl_aff_add(aff1
, aff2
);
1977 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1981 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1989 /* Check whether the given affine expression has non-zero coefficient
1990 * for any dimension in the given range or if any of these dimensions
1991 * appear with non-zero coefficients in any of the integer divisions
1992 * involved in the affine expression.
1994 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1995 enum isl_dim_type type
, unsigned first
, unsigned n
)
2007 ctx
= isl_aff_get_ctx(aff
);
2008 if (first
+ n
> isl_aff_dim(aff
, type
))
2009 isl_die(ctx
, isl_error_invalid
,
2010 "range out of bounds", return -1);
2012 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2016 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2017 for (i
= 0; i
< n
; ++i
)
2018 if (active
[first
+ i
]) {
2031 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2032 enum isl_dim_type type
, unsigned first
, unsigned n
)
2038 if (type
== isl_dim_out
)
2039 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2040 "cannot drop output/set dimension",
2041 return isl_aff_free(aff
));
2042 if (type
== isl_dim_in
)
2044 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2047 ctx
= isl_aff_get_ctx(aff
);
2048 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2049 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2050 return isl_aff_free(aff
));
2052 aff
= isl_aff_cow(aff
);
2056 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2058 return isl_aff_free(aff
);
2060 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2061 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2063 return isl_aff_free(aff
);
2068 /* Project the domain of the affine expression onto its parameter space.
2069 * The affine expression may not involve any of the domain dimensions.
2071 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2077 n
= isl_aff_dim(aff
, isl_dim_in
);
2078 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2080 return isl_aff_free(aff
);
2082 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2083 "affine expression involves some of the domain dimensions",
2084 return isl_aff_free(aff
));
2085 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2086 space
= isl_aff_get_domain_space(aff
);
2087 space
= isl_space_params(space
);
2088 aff
= isl_aff_reset_domain_space(aff
, space
);
2092 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2093 enum isl_dim_type type
, unsigned first
, unsigned n
)
2099 if (type
== isl_dim_out
)
2100 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2101 "cannot insert output/set dimensions",
2102 return isl_aff_free(aff
));
2103 if (type
== isl_dim_in
)
2105 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2108 ctx
= isl_aff_get_ctx(aff
);
2109 if (first
> isl_local_space_dim(aff
->ls
, type
))
2110 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2111 return isl_aff_free(aff
));
2113 aff
= isl_aff_cow(aff
);
2117 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2119 return isl_aff_free(aff
);
2121 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2122 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2124 return isl_aff_free(aff
);
2129 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2130 enum isl_dim_type type
, unsigned n
)
2134 pos
= isl_aff_dim(aff
, type
);
2136 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2139 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2140 enum isl_dim_type type
, unsigned n
)
2144 pos
= isl_pw_aff_dim(pwaff
, type
);
2146 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2149 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2150 * to dimensions of "dst_type" at "dst_pos".
2152 * We only support moving input dimensions to parameters and vice versa.
2154 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2155 enum isl_dim_type dst_type
, unsigned dst_pos
,
2156 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2164 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2165 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2168 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2169 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2170 "cannot move output/set dimension", isl_aff_free(aff
));
2171 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2172 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2173 "cannot move divs", isl_aff_free(aff
));
2174 if (dst_type
== isl_dim_in
)
2175 dst_type
= isl_dim_set
;
2176 if (src_type
== isl_dim_in
)
2177 src_type
= isl_dim_set
;
2179 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2180 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2181 "range out of bounds", isl_aff_free(aff
));
2182 if (dst_type
== src_type
)
2183 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2184 "moving dims within the same type not supported",
2187 aff
= isl_aff_cow(aff
);
2191 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2192 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2193 if (dst_type
> src_type
)
2196 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2197 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2198 src_type
, src_pos
, n
);
2199 if (!aff
->v
|| !aff
->ls
)
2200 return isl_aff_free(aff
);
2202 aff
= sort_divs(aff
);
2207 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2209 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2210 return isl_pw_aff_alloc(dom
, aff
);
2214 #define PW isl_pw_aff
2218 #define EL_IS_ZERO is_empty
2222 #define IS_ZERO is_empty
2225 #undef DEFAULT_IS_ZERO
2226 #define DEFAULT_IS_ZERO 0
2233 #include <isl_pw_templ.c>
2235 static __isl_give isl_set
*align_params_pw_pw_set_and(
2236 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2237 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2238 __isl_take isl_pw_aff
*pwaff2
))
2240 if (!pwaff1
|| !pwaff2
)
2242 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2243 pwaff2
->dim
, isl_dim_param
))
2244 return fn(pwaff1
, pwaff2
);
2245 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2246 !isl_space_has_named_params(pwaff2
->dim
))
2247 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2248 "unaligned unnamed parameters", goto error
);
2249 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2250 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2251 return fn(pwaff1
, pwaff2
);
2253 isl_pw_aff_free(pwaff1
);
2254 isl_pw_aff_free(pwaff2
);
2258 /* Compute a piecewise quasi-affine expression with a domain that
2259 * is the union of those of pwaff1 and pwaff2 and such that on each
2260 * cell, the quasi-affine expression is the better (according to cmp)
2261 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2262 * is defined on a given cell, then the associated expression
2263 * is the defined one.
2265 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2266 __isl_take isl_pw_aff
*pwaff2
,
2267 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2268 __isl_take isl_aff
*aff2
))
2275 if (!pwaff1
|| !pwaff2
)
2278 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2279 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2280 isl_die(ctx
, isl_error_invalid
,
2281 "arguments should live in same space", goto error
);
2283 if (isl_pw_aff_is_empty(pwaff1
)) {
2284 isl_pw_aff_free(pwaff1
);
2288 if (isl_pw_aff_is_empty(pwaff2
)) {
2289 isl_pw_aff_free(pwaff2
);
2293 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2294 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2296 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2297 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2298 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2299 struct isl_set
*common
;
2302 common
= isl_set_intersect(
2303 isl_set_copy(pwaff1
->p
[i
].set
),
2304 isl_set_copy(pwaff2
->p
[j
].set
));
2305 better
= isl_set_from_basic_set(cmp(
2306 isl_aff_copy(pwaff2
->p
[j
].aff
),
2307 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2308 better
= isl_set_intersect(common
, better
);
2309 if (isl_set_plain_is_empty(better
)) {
2310 isl_set_free(better
);
2313 set
= isl_set_subtract(set
, isl_set_copy(better
));
2315 res
= isl_pw_aff_add_piece(res
, better
,
2316 isl_aff_copy(pwaff2
->p
[j
].aff
));
2318 res
= isl_pw_aff_add_piece(res
, set
,
2319 isl_aff_copy(pwaff1
->p
[i
].aff
));
2322 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2323 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2324 for (i
= 0; i
< pwaff1
->n
; ++i
)
2325 set
= isl_set_subtract(set
,
2326 isl_set_copy(pwaff1
->p
[i
].set
));
2327 res
= isl_pw_aff_add_piece(res
, set
,
2328 isl_aff_copy(pwaff2
->p
[j
].aff
));
2331 isl_pw_aff_free(pwaff1
);
2332 isl_pw_aff_free(pwaff2
);
2336 isl_pw_aff_free(pwaff1
);
2337 isl_pw_aff_free(pwaff2
);
2341 /* Compute a piecewise quasi-affine expression with a domain that
2342 * is the union of those of pwaff1 and pwaff2 and such that on each
2343 * cell, the quasi-affine expression is the maximum of those of pwaff1
2344 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2345 * cell, then the associated expression is the defined one.
2347 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2348 __isl_take isl_pw_aff
*pwaff2
)
2350 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2353 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2354 __isl_take isl_pw_aff
*pwaff2
)
2356 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2360 /* Compute a piecewise quasi-affine expression with a domain that
2361 * is the union of those of pwaff1 and pwaff2 and such that on each
2362 * cell, the quasi-affine expression is the minimum of those of pwaff1
2363 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2364 * cell, then the associated expression is the defined one.
2366 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2367 __isl_take isl_pw_aff
*pwaff2
)
2369 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2372 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2373 __isl_take isl_pw_aff
*pwaff2
)
2375 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2379 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2380 __isl_take isl_pw_aff
*pwaff2
, int max
)
2383 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2385 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2388 /* Construct a map with as domain the domain of pwaff and
2389 * one-dimensional range corresponding to the affine expressions.
2391 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2400 dim
= isl_pw_aff_get_space(pwaff
);
2401 map
= isl_map_empty(dim
);
2403 for (i
= 0; i
< pwaff
->n
; ++i
) {
2404 isl_basic_map
*bmap
;
2407 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2408 map_i
= isl_map_from_basic_map(bmap
);
2409 map_i
= isl_map_intersect_domain(map_i
,
2410 isl_set_copy(pwaff
->p
[i
].set
));
2411 map
= isl_map_union_disjoint(map
, map_i
);
2414 isl_pw_aff_free(pwaff
);
2419 /* Construct a map with as domain the domain of pwaff and
2420 * one-dimensional range corresponding to the affine expressions.
2422 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2426 if (isl_space_is_set(pwaff
->dim
))
2427 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2428 "space of input is not a map",
2429 return isl_pw_aff_free(pwaff
));
2430 return map_from_pw_aff(pwaff
);
2433 /* Construct a one-dimensional set with as parameter domain
2434 * the domain of pwaff and the single set dimension
2435 * corresponding to the affine expressions.
2437 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2441 if (!isl_space_is_set(pwaff
->dim
))
2442 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2443 "space of input is not a set",
2444 return isl_pw_aff_free(pwaff
));
2445 return map_from_pw_aff(pwaff
);
2448 /* Return a set containing those elements in the domain
2449 * of pwaff where it is non-negative.
2451 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2459 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2461 for (i
= 0; i
< pwaff
->n
; ++i
) {
2462 isl_basic_set
*bset
;
2466 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2467 bset
= aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2469 set_i
= isl_set_from_basic_set(bset
);
2470 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
2471 set
= isl_set_union_disjoint(set
, set_i
);
2474 isl_pw_aff_free(pwaff
);
2479 /* Return a set containing those elements in the domain
2480 * of pwaff where it is zero (if complement is 0) or not zero
2481 * (if complement is 1).
2483 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
2492 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2494 for (i
= 0; i
< pwaff
->n
; ++i
) {
2495 isl_basic_set
*bset
;
2496 isl_set
*set_i
, *zero
;
2499 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2500 bset
= aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2502 zero
= isl_set_from_basic_set(bset
);
2503 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2505 set_i
= isl_set_subtract(set_i
, zero
);
2507 set_i
= isl_set_intersect(set_i
, zero
);
2508 set
= isl_set_union_disjoint(set
, set_i
);
2511 isl_pw_aff_free(pwaff
);
2516 /* Return a set containing those elements in the domain
2517 * of pwaff where it is zero.
2519 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2521 return pw_aff_zero_set(pwaff
, 0);
2524 /* Return a set containing those elements in the domain
2525 * of pwaff where it is not zero.
2527 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2529 return pw_aff_zero_set(pwaff
, 1);
2532 /* Return a set containing those elements in the shared domain
2533 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2535 * We compute the difference on the shared domain and then construct
2536 * the set of values where this difference is non-negative.
2537 * If strict is set, we first subtract 1 from the difference.
2538 * If equal is set, we only return the elements where pwaff1 and pwaff2
2541 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2542 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2544 isl_set
*set1
, *set2
;
2546 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2547 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2548 set1
= isl_set_intersect(set1
, set2
);
2549 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2550 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2551 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2554 isl_space
*dim
= isl_set_get_space(set1
);
2556 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2557 aff
= isl_aff_add_constant_si(aff
, -1);
2558 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2563 return isl_pw_aff_zero_set(pwaff1
);
2564 return isl_pw_aff_nonneg_set(pwaff1
);
2567 /* Return a set containing those elements in the shared domain
2568 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2570 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2571 __isl_take isl_pw_aff
*pwaff2
)
2573 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2576 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2577 __isl_take isl_pw_aff
*pwaff2
)
2579 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2582 /* Return a set containing those elements in the shared domain
2583 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2585 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2586 __isl_take isl_pw_aff
*pwaff2
)
2588 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2591 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2592 __isl_take isl_pw_aff
*pwaff2
)
2594 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2597 /* Return a set containing those elements in the shared domain
2598 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2600 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2601 __isl_take isl_pw_aff
*pwaff2
)
2603 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2606 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2607 __isl_take isl_pw_aff
*pwaff2
)
2609 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2612 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2613 __isl_take isl_pw_aff
*pwaff2
)
2615 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2618 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2619 __isl_take isl_pw_aff
*pwaff2
)
2621 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2624 /* Return a set containing those elements in the shared domain
2625 * of the elements of list1 and list2 where each element in list1
2626 * has the relation specified by "fn" with each element in list2.
2628 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2629 __isl_take isl_pw_aff_list
*list2
,
2630 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2631 __isl_take isl_pw_aff
*pwaff2
))
2637 if (!list1
|| !list2
)
2640 ctx
= isl_pw_aff_list_get_ctx(list1
);
2641 if (list1
->n
< 1 || list2
->n
< 1)
2642 isl_die(ctx
, isl_error_invalid
,
2643 "list should contain at least one element", goto error
);
2645 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2646 for (i
= 0; i
< list1
->n
; ++i
)
2647 for (j
= 0; j
< list2
->n
; ++j
) {
2650 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2651 isl_pw_aff_copy(list2
->p
[j
]));
2652 set
= isl_set_intersect(set
, set_ij
);
2655 isl_pw_aff_list_free(list1
);
2656 isl_pw_aff_list_free(list2
);
2659 isl_pw_aff_list_free(list1
);
2660 isl_pw_aff_list_free(list2
);
2664 /* Return a set containing those elements in the shared domain
2665 * of the elements of list1 and list2 where each element in list1
2666 * is equal to each element in list2.
2668 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2669 __isl_take isl_pw_aff_list
*list2
)
2671 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2674 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2675 __isl_take isl_pw_aff_list
*list2
)
2677 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2680 /* Return a set containing those elements in the shared domain
2681 * of the elements of list1 and list2 where each element in list1
2682 * is less than or equal to each element in list2.
2684 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2685 __isl_take isl_pw_aff_list
*list2
)
2687 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2690 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2691 __isl_take isl_pw_aff_list
*list2
)
2693 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2696 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2697 __isl_take isl_pw_aff_list
*list2
)
2699 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
2702 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
2703 __isl_take isl_pw_aff_list
*list2
)
2705 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
2709 /* Return a set containing those elements in the shared domain
2710 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2712 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2713 __isl_take isl_pw_aff
*pwaff2
)
2715 isl_set
*set_lt
, *set_gt
;
2717 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
2718 isl_pw_aff_copy(pwaff2
));
2719 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
2720 return isl_set_union_disjoint(set_lt
, set_gt
);
2723 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2724 __isl_take isl_pw_aff
*pwaff2
)
2726 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
2729 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
2734 if (isl_int_is_one(v
))
2736 if (!isl_int_is_pos(v
))
2737 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2738 "factor needs to be positive",
2739 return isl_pw_aff_free(pwaff
));
2740 pwaff
= isl_pw_aff_cow(pwaff
);
2746 for (i
= 0; i
< pwaff
->n
; ++i
) {
2747 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
2748 if (!pwaff
->p
[i
].aff
)
2749 return isl_pw_aff_free(pwaff
);
2755 /* Divide "pa" by "f".
2757 __isl_give isl_pw_aff
*isl_pw_aff_scale_down_val(__isl_take isl_pw_aff
*pa
,
2758 __isl_take isl_val
*f
)
2765 if (isl_val_is_one(f
)) {
2770 if (!isl_val_is_rat(f
))
2771 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2772 "expecting rational factor", goto error
);
2773 if (!isl_val_is_pos(f
))
2774 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2775 "factor needs to be positive", goto error
);
2777 pa
= isl_pw_aff_cow(pa
);
2783 for (i
= 0; i
< pa
->n
; ++i
) {
2784 pa
->p
[i
].aff
= isl_aff_scale_down_val(pa
->p
[i
].aff
,
2793 isl_pw_aff_free(pa
);
2798 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
2802 pwaff
= isl_pw_aff_cow(pwaff
);
2808 for (i
= 0; i
< pwaff
->n
; ++i
) {
2809 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
2810 if (!pwaff
->p
[i
].aff
)
2811 return isl_pw_aff_free(pwaff
);
2817 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
2821 pwaff
= isl_pw_aff_cow(pwaff
);
2827 for (i
= 0; i
< pwaff
->n
; ++i
) {
2828 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
2829 if (!pwaff
->p
[i
].aff
)
2830 return isl_pw_aff_free(pwaff
);
2836 /* Assuming that "cond1" and "cond2" are disjoint,
2837 * return an affine expression that is equal to pwaff1 on cond1
2838 * and to pwaff2 on cond2.
2840 static __isl_give isl_pw_aff
*isl_pw_aff_select(
2841 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
2842 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
2844 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
2845 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
2847 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
2850 /* Return an affine expression that is equal to pwaff_true for elements
2851 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2853 * That is, return cond ? pwaff_true : pwaff_false;
2855 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
2856 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
2858 isl_set
*cond_true
, *cond_false
;
2860 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
2861 cond_false
= isl_pw_aff_zero_set(cond
);
2862 return isl_pw_aff_select(cond_true
, pwaff_true
,
2863 cond_false
, pwaff_false
);
2866 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
2871 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
2874 /* Check whether pwaff is a piecewise constant.
2876 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
2883 for (i
= 0; i
< pwaff
->n
; ++i
) {
2884 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
2885 if (is_cst
< 0 || !is_cst
)
2892 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
2893 __isl_take isl_aff
*aff2
)
2895 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2896 return isl_aff_mul(aff2
, aff1
);
2898 if (!isl_aff_is_cst(aff2
))
2899 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2900 "at least one affine expression should be constant",
2903 aff1
= isl_aff_cow(aff1
);
2907 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2908 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2918 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2920 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
2921 __isl_take isl_aff
*aff2
)
2926 is_cst
= isl_aff_is_cst(aff2
);
2930 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
2931 "second argument should be a constant", goto error
);
2936 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
2938 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2939 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2942 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
2943 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
2946 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2947 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2958 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2959 __isl_take isl_pw_aff
*pwaff2
)
2961 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2964 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2965 __isl_take isl_pw_aff
*pwaff2
)
2967 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2970 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2971 __isl_take isl_pw_aff
*pwaff2
)
2973 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2976 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2977 __isl_take isl_pw_aff
*pwaff2
)
2979 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2982 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2983 __isl_take isl_pw_aff
*pwaff2
)
2985 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2988 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2989 __isl_take isl_pw_aff
*pa2
)
2991 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
2994 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2996 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2997 __isl_take isl_pw_aff
*pa2
)
3001 is_cst
= isl_pw_aff_is_cst(pa2
);
3005 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3006 "second argument should be a piecewise constant",
3008 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3010 isl_pw_aff_free(pa1
);
3011 isl_pw_aff_free(pa2
);
3015 /* Compute the quotient of the integer division of "pa1" by "pa2"
3016 * with rounding towards zero.
3017 * "pa2" is assumed to be a piecewise constant.
3019 * In particular, return
3021 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3024 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3025 __isl_take isl_pw_aff
*pa2
)
3031 is_cst
= isl_pw_aff_is_cst(pa2
);
3035 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3036 "second argument should be a piecewise constant",
3039 pa1
= isl_pw_aff_div(pa1
, pa2
);
3041 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3042 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3043 c
= isl_pw_aff_ceil(pa1
);
3044 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3046 isl_pw_aff_free(pa1
);
3047 isl_pw_aff_free(pa2
);
3051 /* Compute the remainder of the integer division of "pa1" by "pa2"
3052 * with rounding towards zero.
3053 * "pa2" is assumed to be a piecewise constant.
3055 * In particular, return
3057 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3060 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3061 __isl_take isl_pw_aff
*pa2
)
3066 is_cst
= isl_pw_aff_is_cst(pa2
);
3070 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3071 "second argument should be a piecewise constant",
3073 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3074 res
= isl_pw_aff_mul(pa2
, res
);
3075 res
= isl_pw_aff_sub(pa1
, res
);
3078 isl_pw_aff_free(pa1
);
3079 isl_pw_aff_free(pa2
);
3083 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3084 __isl_take isl_pw_aff
*pwaff2
)
3089 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3090 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3091 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3092 isl_pw_aff_copy(pwaff2
));
3093 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3094 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3097 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3098 __isl_take isl_pw_aff
*pwaff2
)
3100 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3103 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3104 __isl_take isl_pw_aff
*pwaff2
)
3109 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3110 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3111 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3112 isl_pw_aff_copy(pwaff2
));
3113 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3114 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3117 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3118 __isl_take isl_pw_aff
*pwaff2
)
3120 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3123 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3124 __isl_take isl_pw_aff_list
*list
,
3125 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3126 __isl_take isl_pw_aff
*pwaff2
))
3135 ctx
= isl_pw_aff_list_get_ctx(list
);
3137 isl_die(ctx
, isl_error_invalid
,
3138 "list should contain at least one element",
3139 return isl_pw_aff_list_free(list
));
3141 res
= isl_pw_aff_copy(list
->p
[0]);
3142 for (i
= 1; i
< list
->n
; ++i
)
3143 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3145 isl_pw_aff_list_free(list
);
3149 /* Return an isl_pw_aff that maps each element in the intersection of the
3150 * domains of the elements of list to the minimal corresponding affine
3153 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3155 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3158 /* Return an isl_pw_aff that maps each element in the intersection of the
3159 * domains of the elements of list to the maximal corresponding affine
3162 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3164 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3167 /* Mark the domains of "pwaff" as rational.
3169 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3173 pwaff
= isl_pw_aff_cow(pwaff
);
3179 for (i
= 0; i
< pwaff
->n
; ++i
) {
3180 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3181 if (!pwaff
->p
[i
].set
)
3182 return isl_pw_aff_free(pwaff
);
3188 /* Mark the domains of the elements of "list" as rational.
3190 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3191 __isl_take isl_pw_aff_list
*list
)
3201 for (i
= 0; i
< n
; ++i
) {
3204 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3205 pa
= isl_pw_aff_set_rational(pa
);
3206 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3212 /* Do the parameters of "aff" match those of "space"?
3214 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3215 __isl_keep isl_space
*space
)
3217 isl_space
*aff_space
;
3223 aff_space
= isl_aff_get_domain_space(aff
);
3225 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3227 isl_space_free(aff_space
);
3231 /* Check that the domain space of "aff" matches "space".
3233 * Return 0 on success and -1 on error.
3235 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3236 __isl_keep isl_space
*space
)
3238 isl_space
*aff_space
;
3244 aff_space
= isl_aff_get_domain_space(aff
);
3246 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3250 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3251 "parameters don't match", goto error
);
3252 match
= isl_space_tuple_match(space
, isl_dim_in
,
3253 aff_space
, isl_dim_set
);
3257 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3258 "domains don't match", goto error
);
3259 isl_space_free(aff_space
);
3262 isl_space_free(aff_space
);
3268 #define NO_INTERSECT_DOMAIN
3271 #include <isl_multi_templ.c>
3274 #undef NO_INTERSECT_DOMAIN
3276 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3277 * of the space to its domain.
3279 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3282 isl_local_space
*ls
;
3287 if (!isl_space_is_map(space
))
3288 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3289 "not a map space", goto error
);
3291 n_in
= isl_space_dim(space
, isl_dim_in
);
3292 space
= isl_space_domain_map(space
);
3294 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3296 isl_space_free(space
);
3300 space
= isl_space_domain(space
);
3301 ls
= isl_local_space_from_space(space
);
3302 for (i
= 0; i
< n_in
; ++i
) {
3305 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3307 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3309 isl_local_space_free(ls
);
3312 isl_space_free(space
);
3316 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3317 * of the space to its range.
3319 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3322 isl_local_space
*ls
;
3327 if (!isl_space_is_map(space
))
3328 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3329 "not a map space", goto error
);
3331 n_in
= isl_space_dim(space
, isl_dim_in
);
3332 n_out
= isl_space_dim(space
, isl_dim_out
);
3333 space
= isl_space_range_map(space
);
3335 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3337 isl_space_free(space
);
3341 space
= isl_space_domain(space
);
3342 ls
= isl_local_space_from_space(space
);
3343 for (i
= 0; i
< n_out
; ++i
) {
3346 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3347 isl_dim_set
, n_in
+ i
);
3348 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3350 isl_local_space_free(ls
);
3353 isl_space_free(space
);
3357 /* Given the space of a set and a range of set dimensions,
3358 * construct an isl_multi_aff that projects out those dimensions.
3360 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3361 __isl_take isl_space
*space
, enum isl_dim_type type
,
3362 unsigned first
, unsigned n
)
3365 isl_local_space
*ls
;
3370 if (!isl_space_is_set(space
))
3371 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3372 "expecting set space", goto error
);
3373 if (type
!= isl_dim_set
)
3374 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3375 "only set dimensions can be projected out", goto error
);
3377 dim
= isl_space_dim(space
, isl_dim_set
);
3378 if (first
+ n
> dim
)
3379 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3380 "range out of bounds", goto error
);
3382 space
= isl_space_from_domain(space
);
3383 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3386 return isl_multi_aff_alloc(space
);
3388 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3389 space
= isl_space_domain(space
);
3390 ls
= isl_local_space_from_space(space
);
3392 for (i
= 0; i
< first
; ++i
) {
3395 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3397 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3400 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3403 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3404 isl_dim_set
, first
+ n
+ i
);
3405 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3408 isl_local_space_free(ls
);
3411 isl_space_free(space
);
3415 /* Given the space of a set and a range of set dimensions,
3416 * construct an isl_pw_multi_aff that projects out those dimensions.
3418 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3419 __isl_take isl_space
*space
, enum isl_dim_type type
,
3420 unsigned first
, unsigned n
)
3424 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3425 return isl_pw_multi_aff_from_multi_aff(ma
);
3428 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3431 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3432 __isl_take isl_multi_aff
*ma
)
3434 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3435 return isl_pw_multi_aff_alloc(dom
, ma
);
3438 /* Create a piecewise multi-affine expression in the given space that maps each
3439 * input dimension to the corresponding output dimension.
3441 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3442 __isl_take isl_space
*space
)
3444 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3447 /* Add "ma2" to "ma1" and return the result.
3449 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3451 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3452 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3454 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3457 /* Add "ma2" to "ma1" and return the result.
3459 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3460 __isl_take isl_multi_aff
*ma2
)
3462 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3463 &isl_multi_aff_add_aligned
);
3466 /* Subtract "ma2" from "ma1" and return the result.
3468 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3470 static __isl_give isl_multi_aff
*isl_multi_aff_sub_aligned(
3471 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3473 return isl_multi_aff_bin_op(ma1
, ma2
, &isl_aff_sub
);
3476 /* Subtract "ma2" from "ma1" and return the result.
3478 __isl_give isl_multi_aff
*isl_multi_aff_sub(__isl_take isl_multi_aff
*ma1
,
3479 __isl_take isl_multi_aff
*ma2
)
3481 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3482 &isl_multi_aff_sub_aligned
);
3485 /* Exploit the equalities in "eq" to simplify the affine expressions.
3487 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3488 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3492 maff
= isl_multi_aff_cow(maff
);
3496 for (i
= 0; i
< maff
->n
; ++i
) {
3497 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3498 isl_basic_set_copy(eq
));
3503 isl_basic_set_free(eq
);
3506 isl_basic_set_free(eq
);
3507 isl_multi_aff_free(maff
);
3511 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3516 maff
= isl_multi_aff_cow(maff
);
3520 for (i
= 0; i
< maff
->n
; ++i
) {
3521 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3523 return isl_multi_aff_free(maff
);
3529 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3530 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3532 maff1
= isl_multi_aff_add(maff1
, maff2
);
3533 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3537 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3545 /* Return the set of domain elements where "ma1" is lexicographically
3546 * smaller than or equal to "ma2".
3548 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3549 __isl_take isl_multi_aff
*ma2
)
3551 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3554 /* Return the set of domain elements where "ma1" is lexicographically
3555 * greater than or equal to "ma2".
3557 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3558 __isl_take isl_multi_aff
*ma2
)
3561 isl_map
*map1
, *map2
;
3564 map1
= isl_map_from_multi_aff(ma1
);
3565 map2
= isl_map_from_multi_aff(ma2
);
3566 map
= isl_map_range_product(map1
, map2
);
3567 space
= isl_space_range(isl_map_get_space(map
));
3568 space
= isl_space_domain(isl_space_unwrap(space
));
3569 ge
= isl_map_lex_ge(space
);
3570 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3572 return isl_map_domain(map
);
3576 #define PW isl_pw_multi_aff
3578 #define EL isl_multi_aff
3580 #define EL_IS_ZERO is_empty
3584 #define IS_ZERO is_empty
3587 #undef DEFAULT_IS_ZERO
3588 #define DEFAULT_IS_ZERO 0
3593 #define NO_INVOLVES_DIMS
3594 #define NO_INSERT_DIMS
3598 #include <isl_pw_templ.c>
3601 #define UNION isl_union_pw_multi_aff
3603 #define PART isl_pw_multi_aff
3605 #define PARTS pw_multi_aff
3606 #define ALIGN_DOMAIN
3610 #include <isl_union_templ.c>
3612 /* Given a function "cmp" that returns the set of elements where
3613 * "ma1" is "better" than "ma2", return the intersection of this
3614 * set with "dom1" and "dom2".
3616 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
3617 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
3618 __isl_keep isl_multi_aff
*ma2
,
3619 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3620 __isl_take isl_multi_aff
*ma2
))
3626 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
3627 is_empty
= isl_set_plain_is_empty(common
);
3628 if (is_empty
>= 0 && is_empty
)
3631 return isl_set_free(common
);
3632 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
3633 better
= isl_set_intersect(common
, better
);
3638 /* Given a function "cmp" that returns the set of elements where
3639 * "ma1" is "better" than "ma2", return a piecewise multi affine
3640 * expression defined on the union of the definition domains
3641 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3642 * "pma2" on each cell. If only one of the two input functions
3643 * is defined on a given cell, then it is considered the best.
3645 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
3646 __isl_take isl_pw_multi_aff
*pma1
,
3647 __isl_take isl_pw_multi_aff
*pma2
,
3648 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3649 __isl_take isl_multi_aff
*ma2
))
3652 isl_pw_multi_aff
*res
= NULL
;
3654 isl_set
*set
= NULL
;
3659 ctx
= isl_space_get_ctx(pma1
->dim
);
3660 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
3661 isl_die(ctx
, isl_error_invalid
,
3662 "arguments should live in the same space", goto error
);
3664 if (isl_pw_multi_aff_is_empty(pma1
)) {
3665 isl_pw_multi_aff_free(pma1
);
3669 if (isl_pw_multi_aff_is_empty(pma2
)) {
3670 isl_pw_multi_aff_free(pma2
);
3674 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
3675 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
3677 for (i
= 0; i
< pma1
->n
; ++i
) {
3678 set
= isl_set_copy(pma1
->p
[i
].set
);
3679 for (j
= 0; j
< pma2
->n
; ++j
) {
3683 better
= shared_and_better(pma2
->p
[j
].set
,
3684 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
3685 pma1
->p
[i
].maff
, cmp
);
3686 is_empty
= isl_set_plain_is_empty(better
);
3687 if (is_empty
< 0 || is_empty
) {
3688 isl_set_free(better
);
3693 set
= isl_set_subtract(set
, isl_set_copy(better
));
3695 res
= isl_pw_multi_aff_add_piece(res
, better
,
3696 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3698 res
= isl_pw_multi_aff_add_piece(res
, set
,
3699 isl_multi_aff_copy(pma1
->p
[i
].maff
));
3702 for (j
= 0; j
< pma2
->n
; ++j
) {
3703 set
= isl_set_copy(pma2
->p
[j
].set
);
3704 for (i
= 0; i
< pma1
->n
; ++i
)
3705 set
= isl_set_subtract(set
,
3706 isl_set_copy(pma1
->p
[i
].set
));
3707 res
= isl_pw_multi_aff_add_piece(res
, set
,
3708 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3711 isl_pw_multi_aff_free(pma1
);
3712 isl_pw_multi_aff_free(pma2
);
3716 isl_pw_multi_aff_free(pma1
);
3717 isl_pw_multi_aff_free(pma2
);
3719 return isl_pw_multi_aff_free(res
);
3722 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
3723 __isl_take isl_pw_multi_aff
*pma1
,
3724 __isl_take isl_pw_multi_aff
*pma2
)
3726 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
3729 /* Given two piecewise multi affine expressions, return a piecewise
3730 * multi-affine expression defined on the union of the definition domains
3731 * of the inputs that is equal to the lexicographic maximum of the two
3732 * inputs on each cell. If only one of the two inputs is defined on
3733 * a given cell, then it is considered to be the maximum.
3735 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
3736 __isl_take isl_pw_multi_aff
*pma1
,
3737 __isl_take isl_pw_multi_aff
*pma2
)
3739 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3740 &pw_multi_aff_union_lexmax
);
3743 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
3744 __isl_take isl_pw_multi_aff
*pma1
,
3745 __isl_take isl_pw_multi_aff
*pma2
)
3747 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
3750 /* Given two piecewise multi affine expressions, return a piecewise
3751 * multi-affine expression defined on the union of the definition domains
3752 * of the inputs that is equal to the lexicographic minimum of the two
3753 * inputs on each cell. If only one of the two inputs is defined on
3754 * a given cell, then it is considered to be the minimum.
3756 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
3757 __isl_take isl_pw_multi_aff
*pma1
,
3758 __isl_take isl_pw_multi_aff
*pma2
)
3760 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3761 &pw_multi_aff_union_lexmin
);
3764 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
3765 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3767 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3768 &isl_multi_aff_add
);
3771 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
3772 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3774 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3778 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
3779 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3781 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3782 &isl_multi_aff_sub
);
3785 /* Subtract "pma2" from "pma1" and return the result.
3787 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
3788 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3790 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3794 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
3795 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3797 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
3800 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3801 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3803 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
3804 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3808 isl_pw_multi_aff
*res
;
3813 n
= pma1
->n
* pma2
->n
;
3814 space
= isl_space_product(isl_space_copy(pma1
->dim
),
3815 isl_space_copy(pma2
->dim
));
3816 res
= isl_pw_multi_aff_alloc_size(space
, n
);
3818 for (i
= 0; i
< pma1
->n
; ++i
) {
3819 for (j
= 0; j
< pma2
->n
; ++j
) {
3823 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
3824 isl_set_copy(pma2
->p
[j
].set
));
3825 ma
= isl_multi_aff_product(
3826 isl_multi_aff_copy(pma1
->p
[i
].maff
),
3827 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3828 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
3832 isl_pw_multi_aff_free(pma1
);
3833 isl_pw_multi_aff_free(pma2
);
3836 isl_pw_multi_aff_free(pma1
);
3837 isl_pw_multi_aff_free(pma2
);
3841 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
3842 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3844 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3845 &pw_multi_aff_product
);
3848 /* Construct a map mapping the domain of the piecewise multi-affine expression
3849 * to its range, with each dimension in the range equated to the
3850 * corresponding affine expression on its cell.
3852 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3860 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
3862 for (i
= 0; i
< pma
->n
; ++i
) {
3863 isl_multi_aff
*maff
;
3864 isl_basic_map
*bmap
;
3867 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
3868 bmap
= isl_basic_map_from_multi_aff(maff
);
3869 map_i
= isl_map_from_basic_map(bmap
);
3870 map_i
= isl_map_intersect_domain(map_i
,
3871 isl_set_copy(pma
->p
[i
].set
));
3872 map
= isl_map_union_disjoint(map
, map_i
);
3875 isl_pw_multi_aff_free(pma
);
3879 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3884 if (!isl_space_is_set(pma
->dim
))
3885 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3886 "isl_pw_multi_aff cannot be converted into an isl_set",
3887 return isl_pw_multi_aff_free(pma
));
3889 return isl_map_from_pw_multi_aff(pma
);
3892 /* Given a basic map with a single output dimension that is defined
3893 * in terms of the parameters and input dimensions using an equality,
3894 * extract an isl_aff that expresses the output dimension in terms
3895 * of the parameters and input dimensions.
3897 * Since some applications expect the result of isl_pw_multi_aff_from_map
3898 * to only contain integer affine expressions, we compute the floor
3899 * of the expression before returning.
3901 * This function shares some similarities with
3902 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3904 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
3905 __isl_take isl_basic_map
*bmap
)
3910 isl_local_space
*ls
;
3915 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
3916 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3917 "basic map should have a single output dimension",
3919 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
3920 total
= isl_basic_map_total_dim(bmap
);
3921 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3922 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
3924 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
3925 1 + total
- (offset
+ 1)) != -1)
3929 if (i
>= bmap
->n_eq
)
3930 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3931 "unable to find suitable equality", goto error
);
3932 ls
= isl_basic_map_get_local_space(bmap
);
3933 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
3936 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
3937 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3939 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3940 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
3941 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
3942 isl_basic_map_free(bmap
);
3944 aff
= isl_aff_remove_unused_divs(aff
);
3945 aff
= isl_aff_floor(aff
);
3948 isl_basic_map_free(bmap
);
3952 /* Given a basic map where each output dimension is defined
3953 * in terms of the parameters and input dimensions using an equality,
3954 * extract an isl_multi_aff that expresses the output dimensions in terms
3955 * of the parameters and input dimensions.
3957 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
3958 __isl_take isl_basic_map
*bmap
)
3967 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
3968 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3970 for (i
= 0; i
< n_out
; ++i
) {
3971 isl_basic_map
*bmap_i
;
3974 bmap_i
= isl_basic_map_copy(bmap
);
3975 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
3976 i
+ 1, n_out
- (1 + i
));
3977 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
3978 aff
= extract_isl_aff_from_basic_map(bmap_i
);
3979 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3982 isl_basic_map_free(bmap
);
3987 /* Create an isl_pw_multi_aff that is equivalent to
3988 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3989 * The given basic map is such that each output dimension is defined
3990 * in terms of the parameters and input dimensions using an equality.
3992 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
3993 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
3997 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
3998 return isl_pw_multi_aff_alloc(domain
, ma
);
4001 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4002 * This obviously only works if the input "map" is single-valued.
4003 * If so, we compute the lexicographic minimum of the image in the form
4004 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4005 * to its lexicographic minimum.
4006 * If the input is not single-valued, we produce an error.
4008 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4009 __isl_take isl_map
*map
)
4013 isl_pw_multi_aff
*pma
;
4015 sv
= isl_map_is_single_valued(map
);
4019 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4020 "map is not single-valued", goto error
);
4021 map
= isl_map_make_disjoint(map
);
4025 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4027 for (i
= 0; i
< map
->n
; ++i
) {
4028 isl_pw_multi_aff
*pma_i
;
4029 isl_basic_map
*bmap
;
4030 bmap
= isl_basic_map_copy(map
->p
[i
]);
4031 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4032 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4042 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4043 * taking into account that the output dimension at position "d"
4044 * can be represented as
4046 * x = floor((e(...) + c1) / m)
4048 * given that constraint "i" is of the form
4050 * e(...) + c1 - m x >= 0
4053 * Let "map" be of the form
4057 * We construct a mapping
4059 * A -> [A -> x = floor(...)]
4061 * apply that to the map, obtaining
4063 * [A -> x = floor(...)] -> B
4065 * and equate dimension "d" to x.
4066 * We then compute a isl_pw_multi_aff representation of the resulting map
4067 * and plug in the mapping above.
4069 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4070 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4074 isl_local_space
*ls
;
4082 isl_pw_multi_aff
*pma
;
4085 is_set
= isl_map_is_set(map
);
4087 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4088 ctx
= isl_map_get_ctx(map
);
4089 space
= isl_space_domain(isl_map_get_space(map
));
4090 n_in
= isl_space_dim(space
, isl_dim_set
);
4091 n
= isl_space_dim(space
, isl_dim_all
);
4093 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4095 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4096 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4098 isl_basic_map_free(hull
);
4100 ls
= isl_local_space_from_space(isl_space_copy(space
));
4101 aff
= isl_aff_alloc_vec(ls
, v
);
4102 aff
= isl_aff_floor(aff
);
4104 isl_space_free(space
);
4105 ma
= isl_multi_aff_from_aff(aff
);
4107 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4108 ma
= isl_multi_aff_range_product(ma
,
4109 isl_multi_aff_from_aff(aff
));
4112 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4113 map
= isl_map_apply_domain(map
, insert
);
4114 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4115 pma
= isl_pw_multi_aff_from_map(map
);
4116 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4121 /* Is constraint "c" of the form
4123 * e(...) + c1 - m x >= 0
4127 * -e(...) + c2 + m x >= 0
4129 * where m > 1 and e only depends on parameters and input dimemnsions?
4131 * "offset" is the offset of the output dimensions
4132 * "pos" is the position of output dimension x.
4134 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4136 if (isl_int_is_zero(c
[offset
+ d
]))
4138 if (isl_int_is_one(c
[offset
+ d
]))
4140 if (isl_int_is_negone(c
[offset
+ d
]))
4142 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4144 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4145 total
- (offset
+ d
+ 1)) != -1)
4150 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4152 * As a special case, we first check if there is any pair of constraints,
4153 * shared by all the basic maps in "map" that force a given dimension
4154 * to be equal to the floor of some affine combination of the input dimensions.
4156 * In particular, if we can find two constraints
4158 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4162 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4164 * where m > 1 and e only depends on parameters and input dimemnsions,
4167 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4169 * then we know that we can take
4171 * x = floor((e(...) + c1) / m)
4173 * without having to perform any computation.
4175 * Note that we know that
4179 * If c1 + c2 were 0, then we would have detected an equality during
4180 * simplification. If c1 + c2 were negative, then we would have detected
4183 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4184 __isl_take isl_map
*map
)
4190 isl_basic_map
*hull
;
4192 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4197 dim
= isl_map_dim(map
, isl_dim_out
);
4198 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4199 total
= 1 + isl_basic_map_total_dim(hull
);
4201 for (d
= 0; d
< dim
; ++d
) {
4202 for (i
= 0; i
< n
; ++i
) {
4203 if (!is_potential_div_constraint(hull
->ineq
[i
],
4206 for (j
= i
+ 1; j
< n
; ++j
) {
4207 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4208 hull
->ineq
[j
] + 1, total
- 1))
4210 isl_int_add(sum
, hull
->ineq
[i
][0],
4212 if (isl_int_abs_lt(sum
,
4213 hull
->ineq
[i
][offset
+ d
]))
4220 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4222 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4226 isl_basic_map_free(hull
);
4227 return pw_multi_aff_from_map_base(map
);
4230 isl_basic_map_free(hull
);
4234 /* Given an affine expression
4236 * [A -> B] -> f(A,B)
4238 * construct an isl_multi_aff
4242 * such that dimension "d" in B' is set to "aff" and the remaining
4243 * dimensions are set equal to the corresponding dimensions in B.
4244 * "n_in" is the dimension of the space A.
4245 * "n_out" is the dimension of the space B.
4247 * If "is_set" is set, then the affine expression is of the form
4251 * and we construct an isl_multi_aff
4255 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4256 unsigned n_in
, unsigned n_out
, int is_set
)
4260 isl_space
*space
, *space2
;
4261 isl_local_space
*ls
;
4263 space
= isl_aff_get_domain_space(aff
);
4264 ls
= isl_local_space_from_space(isl_space_copy(space
));
4265 space2
= isl_space_copy(space
);
4267 space2
= isl_space_range(isl_space_unwrap(space2
));
4268 space
= isl_space_map_from_domain_and_range(space
, space2
);
4269 ma
= isl_multi_aff_alloc(space
);
4270 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4272 for (i
= 0; i
< n_out
; ++i
) {
4275 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4276 isl_dim_set
, n_in
+ i
);
4277 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4280 isl_local_space_free(ls
);
4285 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4286 * taking into account that the dimension at position "d" can be written as
4288 * x = m a + f(..) (1)
4290 * where m is equal to "gcd".
4291 * "i" is the index of the equality in "hull" that defines f(..).
4292 * In particular, the equality is of the form
4294 * f(..) - x + m g(existentials) = 0
4298 * -f(..) + x + m g(existentials) = 0
4300 * We basically plug (1) into "map", resulting in a map with "a"
4301 * in the range instead of "x". The corresponding isl_pw_multi_aff
4302 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4304 * Specifically, given the input map
4308 * We first wrap it into a set
4312 * and define (1) on top of the corresponding space, resulting in "aff".
4313 * We use this to create an isl_multi_aff that maps the output position "d"
4314 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4315 * We plug this into the wrapped map, unwrap the result and compute the
4316 * corresponding isl_pw_multi_aff.
4317 * The result is an expression
4325 * so that we can plug that into "aff", after extending the latter to
4331 * If "map" is actually a set, then there is no "A" space, meaning
4332 * that we do not need to perform any wrapping, and that the result
4333 * of the recursive call is of the form
4337 * which is plugged into a mapping of the form
4341 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4342 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4347 isl_local_space
*ls
;
4350 isl_pw_multi_aff
*pma
, *id
;
4356 is_set
= isl_map_is_set(map
);
4358 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4359 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4360 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4365 set
= isl_map_wrap(map
);
4366 space
= isl_space_map_from_set(isl_set_get_space(set
));
4367 ma
= isl_multi_aff_identity(space
);
4368 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4369 aff
= isl_aff_alloc(ls
);
4371 isl_int_set_si(aff
->v
->el
[0], 1);
4372 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4373 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4376 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4378 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4380 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4381 set
= isl_set_preimage_multi_aff(set
, ma
);
4383 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4388 map
= isl_set_unwrap(set
);
4389 pma
= isl_pw_multi_aff_from_map(set
);
4392 space
= isl_pw_multi_aff_get_domain_space(pma
);
4393 space
= isl_space_map_from_set(space
);
4394 id
= isl_pw_multi_aff_identity(space
);
4395 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4397 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4398 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4400 isl_basic_map_free(hull
);
4404 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4406 * As a special case, we first check if all output dimensions are uniquely
4407 * defined in terms of the parameters and input dimensions over the entire
4408 * domain. If so, we extract the desired isl_pw_multi_aff directly
4409 * from the affine hull of "map" and its domain.
4411 * Otherwise, we check if any of the output dimensions is "strided".
4412 * That is, we check if can be written as
4416 * with m greater than 1, a some combination of existentiall quantified
4417 * variables and f and expression in the parameters and input dimensions.
4418 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4420 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4423 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4427 isl_basic_map
*hull
;
4437 hull
= isl_map_affine_hull(isl_map_copy(map
));
4438 sv
= isl_basic_map_plain_is_single_valued(hull
);
4440 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4442 hull
= isl_basic_map_free(hull
);
4446 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4447 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4450 isl_basic_map_free(hull
);
4451 return pw_multi_aff_from_map_check_div(map
);
4456 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4457 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4459 for (i
= 0; i
< n_out
; ++i
) {
4460 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4461 isl_int
*eq
= hull
->eq
[j
];
4462 isl_pw_multi_aff
*res
;
4464 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4465 !isl_int_is_negone(eq
[o_out
+ i
]))
4467 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4469 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4470 n_out
- (i
+ 1)) != -1)
4472 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4473 if (isl_int_is_zero(gcd
))
4475 if (isl_int_is_one(gcd
))
4478 res
= pw_multi_aff_from_map_stride(map
, hull
,
4486 isl_basic_map_free(hull
);
4487 return pw_multi_aff_from_map_check_div(map
);
4493 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4495 return isl_pw_multi_aff_from_map(set
);
4498 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4501 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4503 isl_union_pw_multi_aff
**upma
= user
;
4504 isl_pw_multi_aff
*pma
;
4506 pma
= isl_pw_multi_aff_from_map(map
);
4507 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4509 return *upma
? 0 : -1;
4512 /* Try and create an isl_union_pw_multi_aff that is equivalent
4513 * to the given isl_union_map.
4514 * The isl_union_map is required to be single-valued in each space.
4515 * Otherwise, an error is produced.
4517 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4518 __isl_take isl_union_map
*umap
)
4521 isl_union_pw_multi_aff
*upma
;
4523 space
= isl_union_map_get_space(umap
);
4524 upma
= isl_union_pw_multi_aff_empty(space
);
4525 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4526 upma
= isl_union_pw_multi_aff_free(upma
);
4527 isl_union_map_free(umap
);
4532 /* Try and create an isl_union_pw_multi_aff that is equivalent
4533 * to the given isl_union_set.
4534 * The isl_union_set is required to be a singleton in each space.
4535 * Otherwise, an error is produced.
4537 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4538 __isl_take isl_union_set
*uset
)
4540 return isl_union_pw_multi_aff_from_union_map(uset
);
4543 /* Return the piecewise affine expression "set ? 1 : 0".
4545 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4548 isl_space
*space
= isl_set_get_space(set
);
4549 isl_local_space
*ls
= isl_local_space_from_space(space
);
4550 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4551 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4553 one
= isl_aff_add_constant_si(one
, 1);
4554 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4555 set
= isl_set_complement(set
);
4556 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4561 /* Plug in "subs" for dimension "type", "pos" of "aff".
4563 * Let i be the dimension to replace and let "subs" be of the form
4567 * and "aff" of the form
4573 * (a f + d g')/(m d)
4575 * where g' is the result of plugging in "subs" in each of the integer
4578 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
4579 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
4584 aff
= isl_aff_cow(aff
);
4586 return isl_aff_free(aff
);
4588 ctx
= isl_aff_get_ctx(aff
);
4589 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
4590 isl_die(ctx
, isl_error_invalid
,
4591 "spaces don't match", return isl_aff_free(aff
));
4592 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
4593 isl_die(ctx
, isl_error_unsupported
,
4594 "cannot handle divs yet", return isl_aff_free(aff
));
4596 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
4598 return isl_aff_free(aff
);
4600 aff
->v
= isl_vec_cow(aff
->v
);
4602 return isl_aff_free(aff
);
4604 pos
+= isl_local_space_offset(aff
->ls
, type
);
4607 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
4608 aff
->v
->size
, subs
->v
->size
, v
);
4614 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4615 * expressions in "maff".
4617 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
4618 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
4619 __isl_keep isl_aff
*subs
)
4623 maff
= isl_multi_aff_cow(maff
);
4625 return isl_multi_aff_free(maff
);
4627 if (type
== isl_dim_in
)
4630 for (i
= 0; i
< maff
->n
; ++i
) {
4631 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
4633 return isl_multi_aff_free(maff
);
4639 /* Plug in "subs" for dimension "type", "pos" of "pma".
4641 * pma is of the form
4645 * while subs is of the form
4647 * v' = B_j(v) -> S_j
4649 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4650 * has a contribution in the result, in particular
4652 * C_ij(S_j) -> M_i(S_j)
4654 * Note that plugging in S_j in C_ij may also result in an empty set
4655 * and this contribution should simply be discarded.
4657 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
4658 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
4659 __isl_keep isl_pw_aff
*subs
)
4662 isl_pw_multi_aff
*res
;
4665 return isl_pw_multi_aff_free(pma
);
4667 n
= pma
->n
* subs
->n
;
4668 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
4670 for (i
= 0; i
< pma
->n
; ++i
) {
4671 for (j
= 0; j
< subs
->n
; ++j
) {
4673 isl_multi_aff
*res_ij
;
4676 common
= isl_set_intersect(
4677 isl_set_copy(pma
->p
[i
].set
),
4678 isl_set_copy(subs
->p
[j
].set
));
4679 common
= isl_set_substitute(common
,
4680 type
, pos
, subs
->p
[j
].aff
);
4681 empty
= isl_set_plain_is_empty(common
);
4682 if (empty
< 0 || empty
) {
4683 isl_set_free(common
);
4689 res_ij
= isl_multi_aff_substitute(
4690 isl_multi_aff_copy(pma
->p
[i
].maff
),
4691 type
, pos
, subs
->p
[j
].aff
);
4693 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
4697 isl_pw_multi_aff_free(pma
);
4700 isl_pw_multi_aff_free(pma
);
4701 isl_pw_multi_aff_free(res
);
4705 /* Compute the preimage of a range of dimensions in the affine expression "src"
4706 * under "ma" and put the result in "dst". The number of dimensions in "src"
4707 * that precede the range is given by "n_before". The number of dimensions
4708 * in the range is given by the number of output dimensions of "ma".
4709 * The number of dimensions that follow the range is given by "n_after".
4710 * If "has_denom" is set (to one),
4711 * then "src" and "dst" have an extra initial denominator.
4712 * "n_div_ma" is the number of existentials in "ma"
4713 * "n_div_bset" is the number of existentials in "src"
4714 * The resulting "dst" (which is assumed to have been allocated by
4715 * the caller) contains coefficients for both sets of existentials,
4716 * first those in "ma" and then those in "src".
4717 * f, c1, c2 and g are temporary objects that have been initialized
4720 * Let src represent the expression
4722 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4724 * and let ma represent the expressions
4726 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4728 * We start out with the following expression for dst:
4730 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4732 * with the multiplication factor f initially equal to 1
4733 * and f \sum_i b_i v_i kept separately.
4734 * For each x_i that we substitute, we multiply the numerator
4735 * (and denominator) of dst by c_1 = m_i and add the numerator
4736 * of the x_i expression multiplied by c_2 = f b_i,
4737 * after removing the common factors of c_1 and c_2.
4738 * The multiplication factor f also needs to be multiplied by c_1
4739 * for the next x_j, j > i.
4741 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
4742 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
4743 int n_div_ma
, int n_div_bmap
,
4744 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
4747 int n_param
, n_in
, n_out
;
4750 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
4751 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
4752 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
4754 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
4755 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
4756 isl_seq_clr(dst
+ o_dst
, n_in
);
4759 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
4762 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
4764 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
4766 isl_int_set_si(f
, 1);
4768 for (i
= 0; i
< n_out
; ++i
) {
4769 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
4771 if (isl_int_is_zero(src
[offset
]))
4773 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
4774 isl_int_mul(c2
, f
, src
[offset
]);
4775 isl_int_gcd(g
, c1
, c2
);
4776 isl_int_divexact(c1
, c1
, g
);
4777 isl_int_divexact(c2
, c2
, g
);
4779 isl_int_mul(f
, f
, c1
);
4782 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4783 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
4784 o_dst
+= 1 + n_param
;
4785 o_src
+= 1 + n_param
;
4786 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
4788 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4789 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
4792 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
4794 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4795 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
4798 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
4800 isl_int_mul(dst
[0], dst
[0], c1
);
4804 /* Compute the pullback of "aff" by the function represented by "ma".
4805 * In other words, plug in "ma" in "aff". The result is an affine expression
4806 * defined over the domain space of "ma".
4808 * If "aff" is represented by
4810 * (a(p) + b x + c(divs))/d
4812 * and ma is represented by
4814 * x = D(p) + F(y) + G(divs')
4816 * then the result is
4818 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4820 * The divs in the local space of the input are similarly adjusted
4821 * through a call to isl_local_space_preimage_multi_aff.
4823 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
4824 __isl_take isl_multi_aff
*ma
)
4826 isl_aff
*res
= NULL
;
4827 isl_local_space
*ls
;
4828 int n_div_aff
, n_div_ma
;
4829 isl_int f
, c1
, c2
, g
;
4831 ma
= isl_multi_aff_align_divs(ma
);
4835 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
4836 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
4838 ls
= isl_aff_get_domain_local_space(aff
);
4839 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
4840 res
= isl_aff_alloc(ls
);
4849 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
4858 isl_multi_aff_free(ma
);
4859 res
= isl_aff_normalize(res
);
4863 isl_multi_aff_free(ma
);
4868 /* Compute the pullback of "aff1" by the function represented by "aff2".
4869 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4870 * defined over the domain space of "aff1".
4872 * The domain of "aff1" should match the range of "aff2", which means
4873 * that it should be single-dimensional.
4875 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
4876 __isl_take isl_aff
*aff2
)
4880 ma
= isl_multi_aff_from_aff(aff2
);
4881 return isl_aff_pullback_multi_aff(aff1
, ma
);
4884 /* Compute the pullback of "ma1" by the function represented by "ma2".
4885 * In other words, plug in "ma2" in "ma1".
4887 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4889 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
4890 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
4893 isl_space
*space
= NULL
;
4895 ma2
= isl_multi_aff_align_divs(ma2
);
4896 ma1
= isl_multi_aff_cow(ma1
);
4900 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
4901 isl_multi_aff_get_space(ma1
));
4903 for (i
= 0; i
< ma1
->n
; ++i
) {
4904 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
4905 isl_multi_aff_copy(ma2
));
4910 ma1
= isl_multi_aff_reset_space(ma1
, space
);
4911 isl_multi_aff_free(ma2
);
4914 isl_space_free(space
);
4915 isl_multi_aff_free(ma2
);
4916 isl_multi_aff_free(ma1
);
4920 /* Compute the pullback of "ma1" by the function represented by "ma2".
4921 * In other words, plug in "ma2" in "ma1".
4923 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
4924 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
4926 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
4927 &isl_multi_aff_pullback_multi_aff_aligned
);
4930 /* Extend the local space of "dst" to include the divs
4931 * in the local space of "src".
4933 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
4934 __isl_keep isl_aff
*src
)
4942 return isl_aff_free(dst
);
4944 ctx
= isl_aff_get_ctx(src
);
4945 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
4946 isl_die(ctx
, isl_error_invalid
,
4947 "spaces don't match", goto error
);
4949 if (src
->ls
->div
->n_row
== 0)
4952 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
4953 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
4954 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
4957 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
4958 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
4966 return isl_aff_free(dst
);
4969 /* Adjust the local spaces of the affine expressions in "maff"
4970 * such that they all have the save divs.
4972 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
4973 __isl_take isl_multi_aff
*maff
)
4981 maff
= isl_multi_aff_cow(maff
);
4985 for (i
= 1; i
< maff
->n
; ++i
)
4986 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
4987 for (i
= 1; i
< maff
->n
; ++i
) {
4988 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
4990 return isl_multi_aff_free(maff
);
4996 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
4998 aff
= isl_aff_cow(aff
);
5002 aff
->ls
= isl_local_space_lift(aff
->ls
);
5004 return isl_aff_free(aff
);
5009 /* Lift "maff" to a space with extra dimensions such that the result
5010 * has no more existentially quantified variables.
5011 * If "ls" is not NULL, then *ls is assigned the local space that lies
5012 * at the basis of the lifting applied to "maff".
5014 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5015 __isl_give isl_local_space
**ls
)
5029 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5030 *ls
= isl_local_space_from_space(space
);
5032 return isl_multi_aff_free(maff
);
5037 maff
= isl_multi_aff_cow(maff
);
5038 maff
= isl_multi_aff_align_divs(maff
);
5042 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5043 space
= isl_multi_aff_get_space(maff
);
5044 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5045 space
= isl_space_extend_domain_with_range(space
,
5046 isl_multi_aff_get_space(maff
));
5048 return isl_multi_aff_free(maff
);
5049 isl_space_free(maff
->space
);
5050 maff
->space
= space
;
5053 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5055 return isl_multi_aff_free(maff
);
5058 for (i
= 0; i
< maff
->n
; ++i
) {
5059 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5067 isl_local_space_free(*ls
);
5068 return isl_multi_aff_free(maff
);
5072 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5074 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5075 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5085 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5086 if (pos
< 0 || pos
>= n_out
)
5087 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5088 "index out of bounds", return NULL
);
5090 space
= isl_pw_multi_aff_get_space(pma
);
5091 space
= isl_space_drop_dims(space
, isl_dim_out
,
5092 pos
+ 1, n_out
- pos
- 1);
5093 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5095 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5096 for (i
= 0; i
< pma
->n
; ++i
) {
5098 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5099 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5105 /* Return an isl_pw_multi_aff with the given "set" as domain and
5106 * an unnamed zero-dimensional range.
5108 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5109 __isl_take isl_set
*set
)
5114 space
= isl_set_get_space(set
);
5115 space
= isl_space_from_domain(space
);
5116 ma
= isl_multi_aff_zero(space
);
5117 return isl_pw_multi_aff_alloc(set
, ma
);
5120 /* Add an isl_pw_multi_aff with the given "set" as domain and
5121 * an unnamed zero-dimensional range to *user.
5123 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5125 isl_union_pw_multi_aff
**upma
= user
;
5126 isl_pw_multi_aff
*pma
;
5128 pma
= isl_pw_multi_aff_from_domain(set
);
5129 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5134 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5135 * an unnamed zero-dimensional range.
5137 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5138 __isl_take isl_union_set
*uset
)
5141 isl_union_pw_multi_aff
*upma
;
5146 space
= isl_union_set_get_space(uset
);
5147 upma
= isl_union_pw_multi_aff_empty(space
);
5149 if (isl_union_set_foreach_set(uset
,
5150 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5153 isl_union_set_free(uset
);
5156 isl_union_set_free(uset
);
5157 isl_union_pw_multi_aff_free(upma
);
5161 /* Convert "pma" to an isl_map and add it to *umap.
5163 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5165 isl_union_map
**umap
= user
;
5168 map
= isl_map_from_pw_multi_aff(pma
);
5169 *umap
= isl_union_map_add_map(*umap
, map
);
5174 /* Construct a union map mapping the domain of the union
5175 * piecewise multi-affine expression to its range, with each dimension
5176 * in the range equated to the corresponding affine expression on its cell.
5178 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5179 __isl_take isl_union_pw_multi_aff
*upma
)
5182 isl_union_map
*umap
;
5187 space
= isl_union_pw_multi_aff_get_space(upma
);
5188 umap
= isl_union_map_empty(space
);
5190 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5191 &map_from_pw_multi_aff
, &umap
) < 0)
5194 isl_union_pw_multi_aff_free(upma
);
5197 isl_union_pw_multi_aff_free(upma
);
5198 isl_union_map_free(umap
);
5202 /* Local data for bin_entry and the callback "fn".
5204 struct isl_union_pw_multi_aff_bin_data
{
5205 isl_union_pw_multi_aff
*upma2
;
5206 isl_union_pw_multi_aff
*res
;
5207 isl_pw_multi_aff
*pma
;
5208 int (*fn
)(void **entry
, void *user
);
5211 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5212 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5214 static int bin_entry(void **entry
, void *user
)
5216 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5217 isl_pw_multi_aff
*pma
= *entry
;
5220 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
5221 data
->fn
, data
) < 0)
5227 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5228 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5229 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5230 * as *entry. The callback should adjust data->res if desired.
5232 static __isl_give isl_union_pw_multi_aff
*bin_op(
5233 __isl_take isl_union_pw_multi_aff
*upma1
,
5234 __isl_take isl_union_pw_multi_aff
*upma2
,
5235 int (*fn
)(void **entry
, void *user
))
5238 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5240 space
= isl_union_pw_multi_aff_get_space(upma2
);
5241 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5242 space
= isl_union_pw_multi_aff_get_space(upma1
);
5243 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5245 if (!upma1
|| !upma2
)
5249 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
5251 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
5252 &bin_entry
, &data
) < 0)
5255 isl_union_pw_multi_aff_free(upma1
);
5256 isl_union_pw_multi_aff_free(upma2
);
5259 isl_union_pw_multi_aff_free(upma1
);
5260 isl_union_pw_multi_aff_free(upma2
);
5261 isl_union_pw_multi_aff_free(data
.res
);
5265 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5266 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5268 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5269 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5273 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5274 isl_pw_multi_aff_get_space(pma2
));
5275 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5276 &isl_multi_aff_range_product
);
5279 /* Given two isl_pw_multi_affs A -> B and C -> D,
5280 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5282 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5283 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5285 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5286 &pw_multi_aff_range_product
);
5289 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5290 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5292 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5293 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5297 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5298 isl_pw_multi_aff_get_space(pma2
));
5299 space
= isl_space_flatten_range(space
);
5300 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5301 &isl_multi_aff_flat_range_product
);
5304 /* Given two isl_pw_multi_affs A -> B and C -> D,
5305 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5307 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5308 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5310 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5311 &pw_multi_aff_flat_range_product
);
5314 /* If data->pma and *entry have the same domain space, then compute
5315 * their flat range product and the result to data->res.
5317 static int flat_range_product_entry(void **entry
, void *user
)
5319 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5320 isl_pw_multi_aff
*pma2
= *entry
;
5322 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
5323 pma2
->dim
, isl_dim_in
))
5326 pma2
= isl_pw_multi_aff_flat_range_product(
5327 isl_pw_multi_aff_copy(data
->pma
),
5328 isl_pw_multi_aff_copy(pma2
));
5330 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5335 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5336 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5338 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5339 __isl_take isl_union_pw_multi_aff
*upma1
,
5340 __isl_take isl_union_pw_multi_aff
*upma2
)
5342 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5345 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5346 * The parameters are assumed to have been aligned.
5348 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5349 * except that it works on two different isl_pw_* types.
5351 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5352 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5353 __isl_take isl_pw_aff
*pa
)
5356 isl_pw_multi_aff
*res
= NULL
;
5361 if (!isl_space_tuple_match(pma
->dim
, isl_dim_in
, pa
->dim
, isl_dim_in
))
5362 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5363 "domains don't match", goto error
);
5364 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5365 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5366 "index out of bounds", goto error
);
5369 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5371 for (i
= 0; i
< pma
->n
; ++i
) {
5372 for (j
= 0; j
< pa
->n
; ++j
) {
5374 isl_multi_aff
*res_ij
;
5377 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5378 isl_set_copy(pa
->p
[j
].set
));
5379 empty
= isl_set_plain_is_empty(common
);
5380 if (empty
< 0 || empty
) {
5381 isl_set_free(common
);
5387 res_ij
= isl_multi_aff_set_aff(
5388 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5389 isl_aff_copy(pa
->p
[j
].aff
));
5390 res_ij
= isl_multi_aff_gist(res_ij
,
5391 isl_set_copy(common
));
5393 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5397 isl_pw_multi_aff_free(pma
);
5398 isl_pw_aff_free(pa
);
5401 isl_pw_multi_aff_free(pma
);
5402 isl_pw_aff_free(pa
);
5403 return isl_pw_multi_aff_free(res
);
5406 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5408 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5409 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5410 __isl_take isl_pw_aff
*pa
)
5414 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5415 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5416 if (!isl_space_has_named_params(pma
->dim
) ||
5417 !isl_space_has_named_params(pa
->dim
))
5418 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5419 "unaligned unnamed parameters", goto error
);
5420 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5421 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5422 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5424 isl_pw_multi_aff_free(pma
);
5425 isl_pw_aff_free(pa
);
5429 /* Do the parameters of "pa" match those of "space"?
5431 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5432 __isl_keep isl_space
*space
)
5434 isl_space
*pa_space
;
5440 pa_space
= isl_pw_aff_get_space(pa
);
5442 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5444 isl_space_free(pa_space
);
5448 /* Check that the domain space of "pa" matches "space".
5450 * Return 0 on success and -1 on error.
5452 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5453 __isl_keep isl_space
*space
)
5455 isl_space
*pa_space
;
5461 pa_space
= isl_pw_aff_get_space(pa
);
5463 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5467 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5468 "parameters don't match", goto error
);
5469 match
= isl_space_tuple_match(space
, isl_dim_in
, pa_space
, isl_dim_in
);
5473 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5474 "domains don't match", goto error
);
5475 isl_space_free(pa_space
);
5478 isl_space_free(pa_space
);
5485 #include <isl_multi_templ.c>
5487 /* Scale the elements of "pma" by the corresponding elements of "mv".
5489 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5490 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5494 pma
= isl_pw_multi_aff_cow(pma
);
5497 if (!isl_space_tuple_match(pma
->dim
, isl_dim_out
,
5498 mv
->space
, isl_dim_set
))
5499 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5500 "spaces don't match", goto error
);
5501 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5502 mv
->space
, isl_dim_param
)) {
5503 pma
= isl_pw_multi_aff_align_params(pma
,
5504 isl_multi_val_get_space(mv
));
5505 mv
= isl_multi_val_align_params(mv
,
5506 isl_pw_multi_aff_get_space(pma
));
5511 for (i
= 0; i
< pma
->n
; ++i
) {
5512 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5513 isl_multi_val_copy(mv
));
5514 if (!pma
->p
[i
].maff
)
5518 isl_multi_val_free(mv
);
5521 isl_multi_val_free(mv
);
5522 isl_pw_multi_aff_free(pma
);
5526 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5527 * mv contains the mv argument.
5528 * res collects the results.
5530 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5532 isl_union_pw_multi_aff
*res
;
5535 /* This function is called for each entry of an isl_union_pw_multi_aff.
5536 * If the space of the entry matches that of data->mv,
5537 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5540 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5542 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5543 isl_pw_multi_aff
*pma
= *entry
;
5547 if (!isl_space_tuple_match(pma
->dim
, isl_dim_out
,
5548 data
->mv
->space
, isl_dim_set
))
5551 pma
= isl_pw_multi_aff_copy(pma
);
5552 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5553 isl_multi_val_copy(data
->mv
));
5554 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
5561 /* Scale the elements of "upma" by the corresponding elements of "mv",
5562 * for those entries that match the space of "mv".
5564 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
5565 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
5567 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
5569 upma
= isl_union_pw_multi_aff_align_params(upma
,
5570 isl_multi_val_get_space(mv
));
5571 mv
= isl_multi_val_align_params(mv
,
5572 isl_union_pw_multi_aff_get_space(upma
));
5577 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->dim
),
5579 if (isl_hash_table_foreach(upma
->dim
->ctx
, &upma
->table
,
5580 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
5583 isl_multi_val_free(mv
);
5584 isl_union_pw_multi_aff_free(upma
);
5587 isl_multi_val_free(mv
);
5588 isl_union_pw_multi_aff_free(upma
);
5592 /* Construct and return a piecewise multi affine expression
5593 * in the given space with value zero in each of the output dimensions and
5594 * a universe domain.
5596 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
5598 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
5601 /* Construct and return a piecewise multi affine expression
5602 * that is equal to the given piecewise affine expression.
5604 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
5605 __isl_take isl_pw_aff
*pa
)
5609 isl_pw_multi_aff
*pma
;
5614 space
= isl_pw_aff_get_space(pa
);
5615 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
5617 for (i
= 0; i
< pa
->n
; ++i
) {
5621 set
= isl_set_copy(pa
->p
[i
].set
);
5622 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
5623 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
5626 isl_pw_aff_free(pa
);
5630 /* Construct a set or map mapping the shared (parameter) domain
5631 * of the piecewise affine expressions to the range of "mpa"
5632 * with each dimension in the range equated to the
5633 * corresponding piecewise affine expression.
5635 static __isl_give isl_map
*map_from_multi_pw_aff(
5636 __isl_take isl_multi_pw_aff
*mpa
)
5645 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
5646 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5647 "invalid space", return isl_multi_pw_aff_free(mpa
));
5649 space
= isl_multi_pw_aff_get_domain_space(mpa
);
5650 map
= isl_map_universe(isl_space_from_domain(space
));
5652 for (i
= 0; i
< mpa
->n
; ++i
) {
5656 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
5657 map_i
= map_from_pw_aff(pa
);
5659 map
= isl_map_flat_range_product(map
, map_i
);
5662 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
5664 isl_multi_pw_aff_free(mpa
);
5668 /* Construct a map mapping the shared domain
5669 * of the piecewise affine expressions to the range of "mpa"
5670 * with each dimension in the range equated to the
5671 * corresponding piecewise affine expression.
5673 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
5677 if (isl_space_is_set(mpa
->space
))
5678 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5679 "space of input is not a map", goto error
);
5681 return map_from_multi_pw_aff(mpa
);
5683 isl_multi_pw_aff_free(mpa
);
5687 /* Construct a set mapping the shared parameter domain
5688 * of the piecewise affine expressions to the space of "mpa"
5689 * with each dimension in the range equated to the
5690 * corresponding piecewise affine expression.
5692 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
5696 if (!isl_space_is_set(mpa
->space
))
5697 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5698 "space of input is not a set", goto error
);
5700 return map_from_multi_pw_aff(mpa
);
5702 isl_multi_pw_aff_free(mpa
);
5706 /* Construct and return a piecewise multi affine expression
5707 * that is equal to the given multi piecewise affine expression
5708 * on the shared domain of the piecewise affine expressions.
5710 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
5711 __isl_take isl_multi_pw_aff
*mpa
)
5716 isl_pw_multi_aff
*pma
;
5721 space
= isl_multi_pw_aff_get_space(mpa
);
5724 isl_multi_pw_aff_free(mpa
);
5725 return isl_pw_multi_aff_zero(space
);
5728 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
5729 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
5731 for (i
= 1; i
< mpa
->n
; ++i
) {
5732 isl_pw_multi_aff
*pma_i
;
5734 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
5735 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
5736 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
5739 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
5741 isl_multi_pw_aff_free(mpa
);
5745 /* Construct and return a multi piecewise affine expression
5746 * that is equal to the given multi affine expression.
5748 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
5749 __isl_take isl_multi_aff
*ma
)
5752 isl_multi_pw_aff
*mpa
;
5757 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
5758 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
5760 for (i
= 0; i
< n
; ++i
) {
5763 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
5764 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
5767 isl_multi_aff_free(ma
);
5771 /* Construct and return a multi piecewise affine expression
5772 * that is equal to the given piecewise multi affine expression.
5774 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
5775 __isl_take isl_pw_multi_aff
*pma
)
5779 isl_multi_pw_aff
*mpa
;
5784 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5785 space
= isl_pw_multi_aff_get_space(pma
);
5786 mpa
= isl_multi_pw_aff_alloc(space
);
5788 for (i
= 0; i
< n
; ++i
) {
5791 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
5792 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
5795 isl_pw_multi_aff_free(pma
);
5799 /* Do "pa1" and "pa2" represent the same function?
5801 * We first check if they are obviously equal.
5802 * If not, we convert them to maps and check if those are equal.
5804 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
5807 isl_map
*map1
, *map2
;
5812 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
5813 if (equal
< 0 || equal
)
5816 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
5817 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
5818 equal
= isl_map_is_equal(map1
, map2
);
5825 /* Do "mpa1" and "mpa2" represent the same function?
5827 * Note that we cannot convert the entire isl_multi_pw_aff
5828 * to a map because the domains of the piecewise affine expressions
5829 * may not be the same.
5831 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
5832 __isl_keep isl_multi_pw_aff
*mpa2
)
5840 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
5841 mpa2
->space
, isl_dim_param
)) {
5842 if (!isl_space_has_named_params(mpa1
->space
))
5844 if (!isl_space_has_named_params(mpa2
->space
))
5846 mpa1
= isl_multi_pw_aff_copy(mpa1
);
5847 mpa2
= isl_multi_pw_aff_copy(mpa2
);
5848 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
5849 isl_multi_pw_aff_get_space(mpa2
));
5850 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
5851 isl_multi_pw_aff_get_space(mpa1
));
5852 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
5853 isl_multi_pw_aff_free(mpa1
);
5854 isl_multi_pw_aff_free(mpa2
);
5858 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
5859 if (equal
< 0 || !equal
)
5862 for (i
= 0; i
< mpa1
->n
; ++i
) {
5863 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
5864 if (equal
< 0 || !equal
)
5871 /* Coalesce the elements of "mpa".
5873 * Note that such coalescing does not change the meaning of "mpa"
5874 * so there is no need to cow. We do need to be careful not to
5875 * destroy any other copies of "mpa" in case of failure.
5877 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
5878 __isl_take isl_multi_pw_aff
*mpa
)
5885 for (i
= 0; i
< mpa
->n
; ++i
) {
5886 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
5887 pa
= isl_pw_aff_coalesce(pa
);
5889 return isl_multi_pw_aff_free(mpa
);
5890 isl_pw_aff_free(mpa
->p
[i
]);
5897 /* Compute the pullback of "mpa" by the function represented by "ma".
5898 * In other words, plug in "ma" in "mpa".
5900 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5902 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
5903 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
5906 isl_space
*space
= NULL
;
5908 mpa
= isl_multi_pw_aff_cow(mpa
);
5912 space
= isl_space_join(isl_multi_aff_get_space(ma
),
5913 isl_multi_pw_aff_get_space(mpa
));
5917 for (i
= 0; i
< mpa
->n
; ++i
) {
5918 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
5919 isl_multi_aff_copy(ma
));
5924 isl_multi_aff_free(ma
);
5925 isl_space_free(mpa
->space
);
5929 isl_space_free(space
);
5930 isl_multi_pw_aff_free(mpa
);
5931 isl_multi_aff_free(ma
);
5935 /* Compute the pullback of "mpa" by the function represented by "ma".
5936 * In other words, plug in "ma" in "mpa".
5938 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
5939 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
5943 if (isl_space_match(mpa
->space
, isl_dim_param
,
5944 ma
->space
, isl_dim_param
))
5945 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
5946 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
5947 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
5948 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
5950 isl_multi_pw_aff_free(mpa
);
5951 isl_multi_aff_free(ma
);
5955 /* Compute the pullback of "mpa" by the function represented by "pma".
5956 * In other words, plug in "pma" in "mpa".
5958 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5960 static __isl_give isl_multi_pw_aff
*
5961 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5962 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
5965 isl_space
*space
= NULL
;
5967 mpa
= isl_multi_pw_aff_cow(mpa
);
5971 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
5972 isl_multi_pw_aff_get_space(mpa
));
5974 for (i
= 0; i
< mpa
->n
; ++i
) {
5975 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
5976 isl_pw_multi_aff_copy(pma
));
5981 isl_pw_multi_aff_free(pma
);
5982 isl_space_free(mpa
->space
);
5986 isl_space_free(space
);
5987 isl_multi_pw_aff_free(mpa
);
5988 isl_pw_multi_aff_free(pma
);
5992 /* Compute the pullback of "mpa" by the function represented by "pma".
5993 * In other words, plug in "pma" in "mpa".
5995 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
5996 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6000 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6001 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6002 mpa
= isl_multi_pw_aff_align_params(mpa
,
6003 isl_pw_multi_aff_get_space(pma
));
6004 pma
= isl_pw_multi_aff_align_params(pma
,
6005 isl_multi_pw_aff_get_space(mpa
));
6006 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6008 isl_multi_pw_aff_free(mpa
);
6009 isl_pw_multi_aff_free(pma
);
6013 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6014 * In other words, plug in "mpa2" in "mpa1".
6016 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6018 static __isl_give isl_multi_pw_aff
*
6019 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6020 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6022 isl_pw_multi_aff
*pma
;
6024 pma
= isl_pw_multi_aff_from_multi_pw_aff(mpa2
);
6025 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa1
, pma
);
6028 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6029 * In other words, plug in "mpa2" in "mpa1".
6031 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6032 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6034 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6035 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);