2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 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 pos
+= isl_local_space_offset(aff
->ls
, type
);
791 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
794 aff
= isl_aff_cow(aff
);
798 aff
->v
= isl_vec_cow(aff
->v
);
800 return isl_aff_free(aff
);
802 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
807 /* Replace the coefficient of the variable of type "type" at position "pos"
810 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
811 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
816 if (type
== isl_dim_out
)
817 isl_die(aff
->v
->ctx
, isl_error_invalid
,
818 "output/set dimension does not have a coefficient",
820 if (type
== isl_dim_in
)
823 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
824 isl_die(aff
->v
->ctx
, isl_error_invalid
,
825 "position out of bounds", goto error
);
827 if (!isl_val_is_rat(v
))
828 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
829 "expecting rational value", goto error
);
831 pos
+= isl_local_space_offset(aff
->ls
, type
);
832 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
833 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
838 aff
= isl_aff_cow(aff
);
841 aff
->v
= isl_vec_cow(aff
->v
);
845 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
846 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
847 } else if (isl_int_is_one(v
->d
)) {
848 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
850 isl_seq_scale(aff
->v
->el
+ 1,
851 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
852 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
853 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
854 aff
->v
= isl_vec_normalize(aff
->v
);
867 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
868 enum isl_dim_type type
, int pos
, isl_int v
)
873 if (type
== isl_dim_out
)
874 isl_die(aff
->v
->ctx
, isl_error_invalid
,
875 "output/set dimension does not have a coefficient",
876 return isl_aff_free(aff
));
877 if (type
== isl_dim_in
)
880 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
881 isl_die(aff
->v
->ctx
, isl_error_invalid
,
882 "position out of bounds", return isl_aff_free(aff
));
884 aff
= isl_aff_cow(aff
);
888 aff
->v
= isl_vec_cow(aff
->v
);
890 return isl_aff_free(aff
);
892 pos
+= isl_local_space_offset(aff
->ls
, type
);
893 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
898 /* Add "v" to the coefficient of the variable of type "type"
899 * at position "pos" of "aff".
901 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
902 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
907 if (isl_val_is_zero(v
)) {
912 if (type
== isl_dim_out
)
913 isl_die(aff
->v
->ctx
, isl_error_invalid
,
914 "output/set dimension does not have a coefficient",
916 if (type
== isl_dim_in
)
919 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
920 isl_die(aff
->v
->ctx
, isl_error_invalid
,
921 "position out of bounds", goto error
);
923 if (!isl_val_is_rat(v
))
924 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
925 "expecting rational value", goto error
);
927 aff
= isl_aff_cow(aff
);
931 aff
->v
= isl_vec_cow(aff
->v
);
935 pos
+= isl_local_space_offset(aff
->ls
, type
);
936 if (isl_int_is_one(v
->d
)) {
937 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
938 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
939 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
940 aff
->v
= isl_vec_normalize(aff
->v
);
944 isl_seq_scale(aff
->v
->el
+ 1,
945 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
946 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
947 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
948 aff
->v
= isl_vec_normalize(aff
->v
);
961 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
962 enum isl_dim_type type
, int pos
, int v
)
967 isl_int_set_si(t
, v
);
968 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
974 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
979 return isl_local_space_get_div(aff
->ls
, pos
);
982 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
984 aff
= isl_aff_cow(aff
);
987 aff
->v
= isl_vec_cow(aff
->v
);
989 return isl_aff_free(aff
);
991 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
996 /* Remove divs from the local space that do not appear in the affine
998 * We currently only remove divs at the end.
999 * Some intermediate divs may also not appear directly in the affine
1000 * expression, but we would also need to check that no other divs are
1001 * defined in terms of them.
1003 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1012 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1013 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1015 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1019 aff
= isl_aff_cow(aff
);
1023 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1024 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1025 if (!aff
->ls
|| !aff
->v
)
1026 return isl_aff_free(aff
);
1031 /* Given two affine expressions "p" of length p_len (including the
1032 * denominator and the constant term) and "subs" of length subs_len,
1033 * plug in "subs" for the variable at position "pos".
1034 * The variables of "subs" and "p" are assumed to match up to subs_len,
1035 * but "p" may have additional variables.
1036 * "v" is an initialized isl_int that can be used internally.
1038 * In particular, if "p" represents the expression
1042 * with i the variable at position "pos" and "subs" represents the expression
1046 * then the result represents the expression
1051 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1052 int p_len
, int subs_len
, isl_int v
)
1054 isl_int_set(v
, p
[1 + pos
]);
1055 isl_int_set_si(p
[1 + pos
], 0);
1056 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1057 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1058 isl_int_mul(p
[0], p
[0], subs
[0]);
1061 /* Look for any divs in the aff->ls with a denominator equal to one
1062 * and plug them into the affine expression and any subsequent divs
1063 * that may reference the div.
1065 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1071 isl_local_space
*ls
;
1077 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1079 for (i
= 0; i
< n
; ++i
) {
1080 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1082 ls
= isl_local_space_copy(aff
->ls
);
1083 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1084 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1085 vec
= isl_vec_copy(aff
->v
);
1086 vec
= isl_vec_cow(vec
);
1092 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1093 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1098 isl_vec_free(aff
->v
);
1100 isl_local_space_free(aff
->ls
);
1107 isl_local_space_free(ls
);
1108 return isl_aff_free(aff
);
1111 /* Look for any divs j that appear with a unit coefficient inside
1112 * the definitions of other divs i and plug them into the definitions
1115 * In particular, an expression of the form
1117 * floor((f(..) + floor(g(..)/n))/m)
1121 * floor((n * f(..) + g(..))/(n * m))
1123 * This simplification is correct because we can move the expression
1124 * f(..) into the inner floor in the original expression to obtain
1126 * floor(floor((n * f(..) + g(..))/n)/m)
1128 * from which we can derive the simplified expression.
1130 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1138 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1139 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1140 for (i
= 1; i
< n
; ++i
) {
1141 for (j
= 0; j
< i
; ++j
) {
1142 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1144 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1145 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1146 aff
->v
->size
, i
, 1);
1148 return isl_aff_free(aff
);
1155 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1157 * Even though this function is only called on isl_affs with a single
1158 * reference, we are careful to only change aff->v and aff->ls together.
1160 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1162 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1163 isl_local_space
*ls
;
1166 ls
= isl_local_space_copy(aff
->ls
);
1167 ls
= isl_local_space_swap_div(ls
, a
, b
);
1168 v
= isl_vec_copy(aff
->v
);
1173 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1174 isl_vec_free(aff
->v
);
1176 isl_local_space_free(aff
->ls
);
1182 isl_local_space_free(ls
);
1183 return isl_aff_free(aff
);
1186 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1188 * We currently do not actually remove div "b", but simply add its
1189 * coefficient to that of "a" and then zero it out.
1191 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1193 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1195 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1198 aff
->v
= isl_vec_cow(aff
->v
);
1200 return isl_aff_free(aff
);
1202 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1203 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1204 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1209 /* Sort the divs in the local space of "aff" according to
1210 * the comparison function "cmp_row" in isl_local_space.c,
1211 * combining the coefficients of identical divs.
1213 * Reordering divs does not change the semantics of "aff",
1214 * so there is no need to call isl_aff_cow.
1215 * Moreover, this function is currently only called on isl_affs
1216 * with a single reference.
1218 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1226 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1227 n
= isl_aff_dim(aff
, isl_dim_div
);
1228 for (i
= 1; i
< n
; ++i
) {
1229 for (j
= i
- 1; j
>= 0; --j
) {
1230 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1234 aff
= merge_divs(aff
, j
, j
+ 1);
1236 aff
= swap_div(aff
, j
, j
+ 1);
1245 /* Normalize the representation of "aff".
1247 * This function should only be called of "new" isl_affs, i.e.,
1248 * with only a single reference. We therefore do not need to
1249 * worry about affecting other instances.
1251 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1255 aff
->v
= isl_vec_normalize(aff
->v
);
1257 return isl_aff_free(aff
);
1258 aff
= plug_in_integral_divs(aff
);
1259 aff
= plug_in_unit_divs(aff
);
1260 aff
= sort_divs(aff
);
1261 aff
= isl_aff_remove_unused_divs(aff
);
1265 /* Given f, return floor(f).
1266 * If f is an integer expression, then just return f.
1267 * If f is a constant, then return the constant floor(f).
1268 * Otherwise, if f = g/m, write g = q m + r,
1269 * create a new div d = [r/m] and return the expression q + d.
1270 * The coefficients in r are taken to lie between -m/2 and m/2.
1272 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1282 if (isl_int_is_one(aff
->v
->el
[0]))
1285 aff
= isl_aff_cow(aff
);
1289 aff
->v
= isl_vec_cow(aff
->v
);
1291 return isl_aff_free(aff
);
1293 if (isl_aff_is_cst(aff
)) {
1294 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1295 isl_int_set_si(aff
->v
->el
[0], 1);
1299 div
= isl_vec_copy(aff
->v
);
1300 div
= isl_vec_cow(div
);
1302 return isl_aff_free(aff
);
1304 ctx
= isl_aff_get_ctx(aff
);
1305 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1306 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1307 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1308 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1309 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1310 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1311 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1315 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1317 return isl_aff_free(aff
);
1319 size
= aff
->v
->size
;
1320 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1322 return isl_aff_free(aff
);
1323 isl_int_set_si(aff
->v
->el
[0], 1);
1324 isl_int_set_si(aff
->v
->el
[size
], 1);
1326 aff
= isl_aff_normalize(aff
);
1333 * aff mod m = aff - m * floor(aff/m)
1335 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1339 res
= isl_aff_copy(aff
);
1340 aff
= isl_aff_scale_down(aff
, m
);
1341 aff
= isl_aff_floor(aff
);
1342 aff
= isl_aff_scale(aff
, m
);
1343 res
= isl_aff_sub(res
, aff
);
1350 * aff mod m = aff - m * floor(aff/m)
1352 * with m an integer value.
1354 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1355 __isl_take isl_val
*m
)
1362 if (!isl_val_is_int(m
))
1363 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1364 "expecting integer modulo", goto error
);
1366 res
= isl_aff_copy(aff
);
1367 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1368 aff
= isl_aff_floor(aff
);
1369 aff
= isl_aff_scale_val(aff
, m
);
1370 res
= isl_aff_sub(res
, aff
);
1381 * pwaff mod m = pwaff - m * floor(pwaff/m)
1383 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1387 res
= isl_pw_aff_copy(pwaff
);
1388 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1389 pwaff
= isl_pw_aff_floor(pwaff
);
1390 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1391 res
= isl_pw_aff_sub(res
, pwaff
);
1398 * pa mod m = pa - m * floor(pa/m)
1400 * with m an integer value.
1402 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1403 __isl_take isl_val
*m
)
1407 if (!isl_val_is_int(m
))
1408 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1409 "expecting integer modulo", goto error
);
1410 pa
= isl_pw_aff_mod(pa
, m
->n
);
1414 isl_pw_aff_free(pa
);
1419 /* Given f, return ceil(f).
1420 * If f is an integer expression, then just return f.
1421 * Otherwise, let f be the expression
1427 * floor((e + m - 1)/m)
1429 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1434 if (isl_int_is_one(aff
->v
->el
[0]))
1437 aff
= isl_aff_cow(aff
);
1440 aff
->v
= isl_vec_cow(aff
->v
);
1442 return isl_aff_free(aff
);
1444 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1445 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1446 aff
= isl_aff_floor(aff
);
1451 /* Apply the expansion computed by isl_merge_divs.
1452 * The expansion itself is given by "exp" while the resulting
1453 * list of divs is given by "div".
1455 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1456 __isl_take isl_mat
*div
, int *exp
)
1463 aff
= isl_aff_cow(aff
);
1467 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1468 new_n_div
= isl_mat_rows(div
);
1469 if (new_n_div
< old_n_div
)
1470 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1471 "not an expansion", goto error
);
1473 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1477 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1479 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1480 if (j
>= 0 && exp
[j
] == i
) {
1482 isl_int_swap(aff
->v
->el
[offset
+ i
],
1483 aff
->v
->el
[offset
+ j
]);
1486 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1489 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1500 /* Add two affine expressions that live in the same local space.
1502 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1503 __isl_take isl_aff
*aff2
)
1507 aff1
= isl_aff_cow(aff1
);
1511 aff1
->v
= isl_vec_cow(aff1
->v
);
1517 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1518 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1519 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1520 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1521 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1522 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1523 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1535 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1536 __isl_take isl_aff
*aff2
)
1547 ctx
= isl_aff_get_ctx(aff1
);
1548 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1549 isl_die(ctx
, isl_error_invalid
,
1550 "spaces don't match", goto error
);
1552 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1553 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1554 if (n_div1
== 0 && n_div2
== 0)
1555 return add_expanded(aff1
, aff2
);
1557 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1558 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1559 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1562 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1563 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1564 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1568 return add_expanded(aff1
, aff2
);
1577 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1578 __isl_take isl_aff
*aff2
)
1580 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1583 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1587 if (isl_int_is_one(f
))
1590 aff
= isl_aff_cow(aff
);
1593 aff
->v
= isl_vec_cow(aff
->v
);
1595 return isl_aff_free(aff
);
1597 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1598 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1603 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1604 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1605 isl_int_divexact(gcd
, f
, gcd
);
1606 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1612 /* Multiple "aff" by "v".
1614 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1615 __isl_take isl_val
*v
)
1620 if (isl_val_is_one(v
)) {
1625 if (!isl_val_is_rat(v
))
1626 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1627 "expecting rational factor", goto error
);
1629 aff
= isl_aff_scale(aff
, v
->n
);
1630 aff
= isl_aff_scale_down(aff
, v
->d
);
1640 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1644 if (isl_int_is_one(f
))
1647 aff
= isl_aff_cow(aff
);
1651 if (isl_int_is_zero(f
))
1652 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1653 "cannot scale down by zero", return isl_aff_free(aff
));
1655 aff
->v
= isl_vec_cow(aff
->v
);
1657 return isl_aff_free(aff
);
1660 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1661 isl_int_gcd(gcd
, gcd
, f
);
1662 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1663 isl_int_divexact(gcd
, f
, gcd
);
1664 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1670 /* Divide "aff" by "v".
1672 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1673 __isl_take isl_val
*v
)
1678 if (isl_val_is_one(v
)) {
1683 if (!isl_val_is_rat(v
))
1684 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1685 "expecting rational factor", goto error
);
1686 if (!isl_val_is_pos(v
))
1687 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1688 "factor needs to be positive", goto error
);
1690 aff
= isl_aff_scale(aff
, v
->d
);
1691 aff
= isl_aff_scale_down(aff
, v
->n
);
1701 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1709 isl_int_set_ui(v
, f
);
1710 aff
= isl_aff_scale_down(aff
, v
);
1716 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1717 enum isl_dim_type type
, unsigned pos
, const char *s
)
1719 aff
= isl_aff_cow(aff
);
1722 if (type
== isl_dim_out
)
1723 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1724 "cannot set name of output/set dimension",
1725 return isl_aff_free(aff
));
1726 if (type
== isl_dim_in
)
1728 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1730 return isl_aff_free(aff
);
1735 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1736 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1738 aff
= isl_aff_cow(aff
);
1740 return isl_id_free(id
);
1741 if (type
== isl_dim_out
)
1742 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1743 "cannot set name of output/set dimension",
1745 if (type
== isl_dim_in
)
1747 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1749 return isl_aff_free(aff
);
1758 /* Replace the identifier of the input tuple of "aff" by "id".
1759 * type is currently required to be equal to isl_dim_in
1761 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
1762 enum isl_dim_type type
, __isl_take isl_id
*id
)
1764 aff
= isl_aff_cow(aff
);
1766 return isl_id_free(id
);
1767 if (type
!= isl_dim_out
)
1768 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1769 "cannot only set id of input tuple", goto error
);
1770 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
1772 return isl_aff_free(aff
);
1781 /* Exploit the equalities in "eq" to simplify the affine expression
1782 * and the expressions of the integer divisions in the local space.
1783 * The integer divisions in this local space are assumed to appear
1784 * as regular dimensions in "eq".
1786 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1787 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1795 if (eq
->n_eq
== 0) {
1796 isl_basic_set_free(eq
);
1800 aff
= isl_aff_cow(aff
);
1804 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1805 isl_basic_set_copy(eq
));
1806 aff
->v
= isl_vec_cow(aff
->v
);
1807 if (!aff
->ls
|| !aff
->v
)
1810 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1812 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1813 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1814 if (j
< 0 || j
== 0 || j
>= total
)
1817 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1821 isl_basic_set_free(eq
);
1822 aff
= isl_aff_normalize(aff
);
1825 isl_basic_set_free(eq
);
1830 /* Exploit the equalities in "eq" to simplify the affine expression
1831 * and the expressions of the integer divisions in the local space.
1833 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1834 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1840 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1842 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
1843 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1845 isl_basic_set_free(eq
);
1850 /* Look for equalities among the variables shared by context and aff
1851 * and the integer divisions of aff, if any.
1852 * The equalities are then used to eliminate coefficients and/or integer
1853 * divisions from aff.
1855 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1856 __isl_take isl_set
*context
)
1858 isl_basic_set
*hull
;
1863 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1865 isl_basic_set
*bset
;
1866 isl_local_space
*ls
;
1867 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1868 ls
= isl_aff_get_domain_local_space(aff
);
1869 bset
= isl_basic_set_from_local_space(ls
);
1870 bset
= isl_basic_set_lift(bset
);
1871 bset
= isl_basic_set_flatten(bset
);
1872 context
= isl_set_intersect(context
,
1873 isl_set_from_basic_set(bset
));
1876 hull
= isl_set_affine_hull(context
);
1877 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1880 isl_set_free(context
);
1884 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1885 __isl_take isl_set
*context
)
1887 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1888 dom_context
= isl_set_intersect_params(dom_context
, context
);
1889 return isl_aff_gist(aff
, dom_context
);
1892 /* Return a basic set containing those elements in the space
1893 * of aff where it is non-negative.
1894 * If "rational" is set, then return a rational basic set.
1896 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
1897 __isl_take isl_aff
*aff
, int rational
)
1899 isl_constraint
*ineq
;
1900 isl_basic_set
*bset
;
1902 ineq
= isl_inequality_from_aff(aff
);
1904 bset
= isl_basic_set_from_constraint(ineq
);
1906 bset
= isl_basic_set_set_rational(bset
);
1907 bset
= isl_basic_set_simplify(bset
);
1911 /* Return a basic set containing those elements in the space
1912 * of aff where it is non-negative.
1914 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1916 return aff_nonneg_basic_set(aff
, 0);
1919 /* Return a basic set containing those elements in the domain space
1920 * of aff where it is negative.
1922 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
1924 aff
= isl_aff_neg(aff
);
1925 aff
= isl_aff_add_constant_num_si(aff
, -1);
1926 return isl_aff_nonneg_basic_set(aff
);
1929 /* Return a basic set containing those elements in the space
1930 * of aff where it is zero.
1931 * If "rational" is set, then return a rational basic set.
1933 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
1936 isl_constraint
*ineq
;
1937 isl_basic_set
*bset
;
1939 ineq
= isl_equality_from_aff(aff
);
1941 bset
= isl_basic_set_from_constraint(ineq
);
1943 bset
= isl_basic_set_set_rational(bset
);
1944 bset
= isl_basic_set_simplify(bset
);
1948 /* Return a basic set containing those elements in the space
1949 * of aff where it is zero.
1951 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1953 return aff_zero_basic_set(aff
, 0);
1956 /* Return a basic set containing those elements in the shared space
1957 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1959 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1960 __isl_take isl_aff
*aff2
)
1962 aff1
= isl_aff_sub(aff1
, aff2
);
1964 return isl_aff_nonneg_basic_set(aff1
);
1967 /* Return a basic set containing those elements in the shared space
1968 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1970 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1971 __isl_take isl_aff
*aff2
)
1973 return isl_aff_ge_basic_set(aff2
, aff1
);
1976 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1977 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1979 aff1
= isl_aff_add(aff1
, aff2
);
1980 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1984 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1992 /* Check whether the given affine expression has non-zero coefficient
1993 * for any dimension in the given range or if any of these dimensions
1994 * appear with non-zero coefficients in any of the integer divisions
1995 * involved in the affine expression.
1997 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1998 enum isl_dim_type type
, unsigned first
, unsigned n
)
2010 ctx
= isl_aff_get_ctx(aff
);
2011 if (first
+ n
> isl_aff_dim(aff
, type
))
2012 isl_die(ctx
, isl_error_invalid
,
2013 "range out of bounds", return -1);
2015 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2019 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2020 for (i
= 0; i
< n
; ++i
)
2021 if (active
[first
+ i
]) {
2034 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2035 enum isl_dim_type type
, unsigned first
, unsigned n
)
2041 if (type
== isl_dim_out
)
2042 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2043 "cannot drop output/set dimension",
2044 return isl_aff_free(aff
));
2045 if (type
== isl_dim_in
)
2047 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2050 ctx
= isl_aff_get_ctx(aff
);
2051 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2052 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2053 return isl_aff_free(aff
));
2055 aff
= isl_aff_cow(aff
);
2059 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2061 return isl_aff_free(aff
);
2063 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2064 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2066 return isl_aff_free(aff
);
2071 /* Project the domain of the affine expression onto its parameter space.
2072 * The affine expression may not involve any of the domain dimensions.
2074 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2080 n
= isl_aff_dim(aff
, isl_dim_in
);
2081 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2083 return isl_aff_free(aff
);
2085 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2086 "affine expression involves some of the domain dimensions",
2087 return isl_aff_free(aff
));
2088 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2089 space
= isl_aff_get_domain_space(aff
);
2090 space
= isl_space_params(space
);
2091 aff
= isl_aff_reset_domain_space(aff
, space
);
2095 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2096 enum isl_dim_type type
, unsigned first
, unsigned n
)
2102 if (type
== isl_dim_out
)
2103 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2104 "cannot insert output/set dimensions",
2105 return isl_aff_free(aff
));
2106 if (type
== isl_dim_in
)
2108 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2111 ctx
= isl_aff_get_ctx(aff
);
2112 if (first
> isl_local_space_dim(aff
->ls
, type
))
2113 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2114 return isl_aff_free(aff
));
2116 aff
= isl_aff_cow(aff
);
2120 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2122 return isl_aff_free(aff
);
2124 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2125 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2127 return isl_aff_free(aff
);
2132 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2133 enum isl_dim_type type
, unsigned n
)
2137 pos
= isl_aff_dim(aff
, type
);
2139 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2142 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2143 enum isl_dim_type type
, unsigned n
)
2147 pos
= isl_pw_aff_dim(pwaff
, type
);
2149 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2152 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2153 * to dimensions of "dst_type" at "dst_pos".
2155 * We only support moving input dimensions to parameters and vice versa.
2157 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2158 enum isl_dim_type dst_type
, unsigned dst_pos
,
2159 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2167 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2168 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2171 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2172 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2173 "cannot move output/set dimension", isl_aff_free(aff
));
2174 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2175 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2176 "cannot move divs", isl_aff_free(aff
));
2177 if (dst_type
== isl_dim_in
)
2178 dst_type
= isl_dim_set
;
2179 if (src_type
== isl_dim_in
)
2180 src_type
= isl_dim_set
;
2182 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2183 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2184 "range out of bounds", isl_aff_free(aff
));
2185 if (dst_type
== src_type
)
2186 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2187 "moving dims within the same type not supported",
2190 aff
= isl_aff_cow(aff
);
2194 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2195 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2196 if (dst_type
> src_type
)
2199 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2200 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2201 src_type
, src_pos
, n
);
2202 if (!aff
->v
|| !aff
->ls
)
2203 return isl_aff_free(aff
);
2205 aff
= sort_divs(aff
);
2210 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2212 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2213 return isl_pw_aff_alloc(dom
, aff
);
2217 #define PW isl_pw_aff
2221 #define EL_IS_ZERO is_empty
2225 #define IS_ZERO is_empty
2228 #undef DEFAULT_IS_ZERO
2229 #define DEFAULT_IS_ZERO 0
2236 #include <isl_pw_templ.c>
2238 static __isl_give isl_set
*align_params_pw_pw_set_and(
2239 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2240 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2241 __isl_take isl_pw_aff
*pwaff2
))
2243 if (!pwaff1
|| !pwaff2
)
2245 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2246 pwaff2
->dim
, isl_dim_param
))
2247 return fn(pwaff1
, pwaff2
);
2248 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2249 !isl_space_has_named_params(pwaff2
->dim
))
2250 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2251 "unaligned unnamed parameters", goto error
);
2252 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2253 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2254 return fn(pwaff1
, pwaff2
);
2256 isl_pw_aff_free(pwaff1
);
2257 isl_pw_aff_free(pwaff2
);
2261 /* Compute a piecewise quasi-affine expression with a domain that
2262 * is the union of those of pwaff1 and pwaff2 and such that on each
2263 * cell, the quasi-affine expression is the better (according to cmp)
2264 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2265 * is defined on a given cell, then the associated expression
2266 * is the defined one.
2268 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2269 __isl_take isl_pw_aff
*pwaff2
,
2270 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2271 __isl_take isl_aff
*aff2
))
2278 if (!pwaff1
|| !pwaff2
)
2281 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2282 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2283 isl_die(ctx
, isl_error_invalid
,
2284 "arguments should live in same space", goto error
);
2286 if (isl_pw_aff_is_empty(pwaff1
)) {
2287 isl_pw_aff_free(pwaff1
);
2291 if (isl_pw_aff_is_empty(pwaff2
)) {
2292 isl_pw_aff_free(pwaff2
);
2296 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2297 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2299 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2300 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2301 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2302 struct isl_set
*common
;
2305 common
= isl_set_intersect(
2306 isl_set_copy(pwaff1
->p
[i
].set
),
2307 isl_set_copy(pwaff2
->p
[j
].set
));
2308 better
= isl_set_from_basic_set(cmp(
2309 isl_aff_copy(pwaff2
->p
[j
].aff
),
2310 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2311 better
= isl_set_intersect(common
, better
);
2312 if (isl_set_plain_is_empty(better
)) {
2313 isl_set_free(better
);
2316 set
= isl_set_subtract(set
, isl_set_copy(better
));
2318 res
= isl_pw_aff_add_piece(res
, better
,
2319 isl_aff_copy(pwaff2
->p
[j
].aff
));
2321 res
= isl_pw_aff_add_piece(res
, set
,
2322 isl_aff_copy(pwaff1
->p
[i
].aff
));
2325 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2326 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2327 for (i
= 0; i
< pwaff1
->n
; ++i
)
2328 set
= isl_set_subtract(set
,
2329 isl_set_copy(pwaff1
->p
[i
].set
));
2330 res
= isl_pw_aff_add_piece(res
, set
,
2331 isl_aff_copy(pwaff2
->p
[j
].aff
));
2334 isl_pw_aff_free(pwaff1
);
2335 isl_pw_aff_free(pwaff2
);
2339 isl_pw_aff_free(pwaff1
);
2340 isl_pw_aff_free(pwaff2
);
2344 /* Compute a piecewise quasi-affine expression with a domain that
2345 * is the union of those of pwaff1 and pwaff2 and such that on each
2346 * cell, the quasi-affine expression is the maximum of those of pwaff1
2347 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2348 * cell, then the associated expression is the defined one.
2350 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2351 __isl_take isl_pw_aff
*pwaff2
)
2353 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2356 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2357 __isl_take isl_pw_aff
*pwaff2
)
2359 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2363 /* Compute a piecewise quasi-affine expression with a domain that
2364 * is the union of those of pwaff1 and pwaff2 and such that on each
2365 * cell, the quasi-affine expression is the minimum of those of pwaff1
2366 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2367 * cell, then the associated expression is the defined one.
2369 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2370 __isl_take isl_pw_aff
*pwaff2
)
2372 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2375 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2376 __isl_take isl_pw_aff
*pwaff2
)
2378 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2382 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2383 __isl_take isl_pw_aff
*pwaff2
, int max
)
2386 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2388 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2391 /* Construct a map with as domain the domain of pwaff and
2392 * one-dimensional range corresponding to the affine expressions.
2394 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2403 dim
= isl_pw_aff_get_space(pwaff
);
2404 map
= isl_map_empty(dim
);
2406 for (i
= 0; i
< pwaff
->n
; ++i
) {
2407 isl_basic_map
*bmap
;
2410 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2411 map_i
= isl_map_from_basic_map(bmap
);
2412 map_i
= isl_map_intersect_domain(map_i
,
2413 isl_set_copy(pwaff
->p
[i
].set
));
2414 map
= isl_map_union_disjoint(map
, map_i
);
2417 isl_pw_aff_free(pwaff
);
2422 /* Construct a map with as domain the domain of pwaff and
2423 * one-dimensional range corresponding to the affine expressions.
2425 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2429 if (isl_space_is_set(pwaff
->dim
))
2430 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2431 "space of input is not a map",
2432 return isl_pw_aff_free(pwaff
));
2433 return map_from_pw_aff(pwaff
);
2436 /* Construct a one-dimensional set with as parameter domain
2437 * the domain of pwaff and the single set dimension
2438 * corresponding to the affine expressions.
2440 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2444 if (!isl_space_is_set(pwaff
->dim
))
2445 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2446 "space of input is not a set",
2447 return isl_pw_aff_free(pwaff
));
2448 return map_from_pw_aff(pwaff
);
2451 /* Return a set containing those elements in the domain
2452 * of pwaff where it is non-negative.
2454 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2462 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2464 for (i
= 0; i
< pwaff
->n
; ++i
) {
2465 isl_basic_set
*bset
;
2469 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2470 bset
= aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2472 set_i
= isl_set_from_basic_set(bset
);
2473 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
2474 set
= isl_set_union_disjoint(set
, set_i
);
2477 isl_pw_aff_free(pwaff
);
2482 /* Return a set containing those elements in the domain
2483 * of pwaff where it is zero (if complement is 0) or not zero
2484 * (if complement is 1).
2486 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
2495 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2497 for (i
= 0; i
< pwaff
->n
; ++i
) {
2498 isl_basic_set
*bset
;
2499 isl_set
*set_i
, *zero
;
2502 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2503 bset
= aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2505 zero
= isl_set_from_basic_set(bset
);
2506 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2508 set_i
= isl_set_subtract(set_i
, zero
);
2510 set_i
= isl_set_intersect(set_i
, zero
);
2511 set
= isl_set_union_disjoint(set
, set_i
);
2514 isl_pw_aff_free(pwaff
);
2519 /* Return a set containing those elements in the domain
2520 * of pwaff where it is zero.
2522 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2524 return pw_aff_zero_set(pwaff
, 0);
2527 /* Return a set containing those elements in the domain
2528 * of pwaff where it is not zero.
2530 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2532 return pw_aff_zero_set(pwaff
, 1);
2535 /* Return a set containing those elements in the shared domain
2536 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2538 * We compute the difference on the shared domain and then construct
2539 * the set of values where this difference is non-negative.
2540 * If strict is set, we first subtract 1 from the difference.
2541 * If equal is set, we only return the elements where pwaff1 and pwaff2
2544 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2545 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2547 isl_set
*set1
, *set2
;
2549 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2550 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2551 set1
= isl_set_intersect(set1
, set2
);
2552 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2553 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2554 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2557 isl_space
*dim
= isl_set_get_space(set1
);
2559 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2560 aff
= isl_aff_add_constant_si(aff
, -1);
2561 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2566 return isl_pw_aff_zero_set(pwaff1
);
2567 return isl_pw_aff_nonneg_set(pwaff1
);
2570 /* Return a set containing those elements in the shared domain
2571 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2573 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2574 __isl_take isl_pw_aff
*pwaff2
)
2576 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2579 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2580 __isl_take isl_pw_aff
*pwaff2
)
2582 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2585 /* Return a set containing those elements in the shared domain
2586 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2588 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2589 __isl_take isl_pw_aff
*pwaff2
)
2591 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2594 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2595 __isl_take isl_pw_aff
*pwaff2
)
2597 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2600 /* Return a set containing those elements in the shared domain
2601 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2603 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2604 __isl_take isl_pw_aff
*pwaff2
)
2606 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2609 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2610 __isl_take isl_pw_aff
*pwaff2
)
2612 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2615 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2616 __isl_take isl_pw_aff
*pwaff2
)
2618 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2621 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2622 __isl_take isl_pw_aff
*pwaff2
)
2624 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2627 /* Return a set containing those elements in the shared domain
2628 * of the elements of list1 and list2 where each element in list1
2629 * has the relation specified by "fn" with each element in list2.
2631 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2632 __isl_take isl_pw_aff_list
*list2
,
2633 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2634 __isl_take isl_pw_aff
*pwaff2
))
2640 if (!list1
|| !list2
)
2643 ctx
= isl_pw_aff_list_get_ctx(list1
);
2644 if (list1
->n
< 1 || list2
->n
< 1)
2645 isl_die(ctx
, isl_error_invalid
,
2646 "list should contain at least one element", goto error
);
2648 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2649 for (i
= 0; i
< list1
->n
; ++i
)
2650 for (j
= 0; j
< list2
->n
; ++j
) {
2653 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2654 isl_pw_aff_copy(list2
->p
[j
]));
2655 set
= isl_set_intersect(set
, set_ij
);
2658 isl_pw_aff_list_free(list1
);
2659 isl_pw_aff_list_free(list2
);
2662 isl_pw_aff_list_free(list1
);
2663 isl_pw_aff_list_free(list2
);
2667 /* Return a set containing those elements in the shared domain
2668 * of the elements of list1 and list2 where each element in list1
2669 * is equal to each element in list2.
2671 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2672 __isl_take isl_pw_aff_list
*list2
)
2674 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2677 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2678 __isl_take isl_pw_aff_list
*list2
)
2680 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2683 /* Return a set containing those elements in the shared domain
2684 * of the elements of list1 and list2 where each element in list1
2685 * is less than or equal to each element in list2.
2687 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2688 __isl_take isl_pw_aff_list
*list2
)
2690 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2693 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2694 __isl_take isl_pw_aff_list
*list2
)
2696 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2699 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2700 __isl_take isl_pw_aff_list
*list2
)
2702 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
2705 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
2706 __isl_take isl_pw_aff_list
*list2
)
2708 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
2712 /* Return a set containing those elements in the shared domain
2713 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2715 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2716 __isl_take isl_pw_aff
*pwaff2
)
2718 isl_set
*set_lt
, *set_gt
;
2720 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
2721 isl_pw_aff_copy(pwaff2
));
2722 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
2723 return isl_set_union_disjoint(set_lt
, set_gt
);
2726 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2727 __isl_take isl_pw_aff
*pwaff2
)
2729 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
2732 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
2737 if (isl_int_is_one(v
))
2739 if (!isl_int_is_pos(v
))
2740 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2741 "factor needs to be positive",
2742 return isl_pw_aff_free(pwaff
));
2743 pwaff
= isl_pw_aff_cow(pwaff
);
2749 for (i
= 0; i
< pwaff
->n
; ++i
) {
2750 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
2751 if (!pwaff
->p
[i
].aff
)
2752 return isl_pw_aff_free(pwaff
);
2758 /* Divide "pa" by "f".
2760 __isl_give isl_pw_aff
*isl_pw_aff_scale_down_val(__isl_take isl_pw_aff
*pa
,
2761 __isl_take isl_val
*f
)
2768 if (isl_val_is_one(f
)) {
2773 if (!isl_val_is_rat(f
))
2774 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2775 "expecting rational factor", goto error
);
2776 if (!isl_val_is_pos(f
))
2777 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2778 "factor needs to be positive", goto error
);
2780 pa
= isl_pw_aff_cow(pa
);
2786 for (i
= 0; i
< pa
->n
; ++i
) {
2787 pa
->p
[i
].aff
= isl_aff_scale_down_val(pa
->p
[i
].aff
,
2796 isl_pw_aff_free(pa
);
2801 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
2805 pwaff
= isl_pw_aff_cow(pwaff
);
2811 for (i
= 0; i
< pwaff
->n
; ++i
) {
2812 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
2813 if (!pwaff
->p
[i
].aff
)
2814 return isl_pw_aff_free(pwaff
);
2820 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
2824 pwaff
= isl_pw_aff_cow(pwaff
);
2830 for (i
= 0; i
< pwaff
->n
; ++i
) {
2831 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
2832 if (!pwaff
->p
[i
].aff
)
2833 return isl_pw_aff_free(pwaff
);
2839 /* Assuming that "cond1" and "cond2" are disjoint,
2840 * return an affine expression that is equal to pwaff1 on cond1
2841 * and to pwaff2 on cond2.
2843 static __isl_give isl_pw_aff
*isl_pw_aff_select(
2844 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
2845 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
2847 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
2848 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
2850 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
2853 /* Return an affine expression that is equal to pwaff_true for elements
2854 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2856 * That is, return cond ? pwaff_true : pwaff_false;
2858 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
2859 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
2861 isl_set
*cond_true
, *cond_false
;
2863 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
2864 cond_false
= isl_pw_aff_zero_set(cond
);
2865 return isl_pw_aff_select(cond_true
, pwaff_true
,
2866 cond_false
, pwaff_false
);
2869 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
2874 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
2877 /* Check whether pwaff is a piecewise constant.
2879 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
2886 for (i
= 0; i
< pwaff
->n
; ++i
) {
2887 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
2888 if (is_cst
< 0 || !is_cst
)
2895 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
2896 __isl_take isl_aff
*aff2
)
2898 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2899 return isl_aff_mul(aff2
, aff1
);
2901 if (!isl_aff_is_cst(aff2
))
2902 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2903 "at least one affine expression should be constant",
2906 aff1
= isl_aff_cow(aff1
);
2910 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2911 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2921 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2923 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
2924 __isl_take isl_aff
*aff2
)
2929 is_cst
= isl_aff_is_cst(aff2
);
2933 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
2934 "second argument should be a constant", goto error
);
2939 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
2941 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2942 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2945 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
2946 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
2949 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2950 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2961 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2962 __isl_take isl_pw_aff
*pwaff2
)
2964 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2967 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2968 __isl_take isl_pw_aff
*pwaff2
)
2970 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2973 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2974 __isl_take isl_pw_aff
*pwaff2
)
2976 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2979 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2980 __isl_take isl_pw_aff
*pwaff2
)
2982 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2985 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2986 __isl_take isl_pw_aff
*pwaff2
)
2988 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2991 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2992 __isl_take isl_pw_aff
*pa2
)
2994 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
2997 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2999 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3000 __isl_take isl_pw_aff
*pa2
)
3004 is_cst
= isl_pw_aff_is_cst(pa2
);
3008 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3009 "second argument should be a piecewise constant",
3011 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3013 isl_pw_aff_free(pa1
);
3014 isl_pw_aff_free(pa2
);
3018 /* Compute the quotient of the integer division of "pa1" by "pa2"
3019 * with rounding towards zero.
3020 * "pa2" is assumed to be a piecewise constant.
3022 * In particular, return
3024 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3027 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3028 __isl_take isl_pw_aff
*pa2
)
3034 is_cst
= isl_pw_aff_is_cst(pa2
);
3038 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3039 "second argument should be a piecewise constant",
3042 pa1
= isl_pw_aff_div(pa1
, pa2
);
3044 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3045 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3046 c
= isl_pw_aff_ceil(pa1
);
3047 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3049 isl_pw_aff_free(pa1
);
3050 isl_pw_aff_free(pa2
);
3054 /* Compute the remainder of the integer division of "pa1" by "pa2"
3055 * with rounding towards zero.
3056 * "pa2" is assumed to be a piecewise constant.
3058 * In particular, return
3060 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3063 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3064 __isl_take isl_pw_aff
*pa2
)
3069 is_cst
= isl_pw_aff_is_cst(pa2
);
3073 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3074 "second argument should be a piecewise constant",
3076 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3077 res
= isl_pw_aff_mul(pa2
, res
);
3078 res
= isl_pw_aff_sub(pa1
, res
);
3081 isl_pw_aff_free(pa1
);
3082 isl_pw_aff_free(pa2
);
3086 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3087 __isl_take isl_pw_aff
*pwaff2
)
3092 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3093 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3094 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3095 isl_pw_aff_copy(pwaff2
));
3096 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3097 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3100 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3101 __isl_take isl_pw_aff
*pwaff2
)
3103 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3106 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3107 __isl_take isl_pw_aff
*pwaff2
)
3112 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3113 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3114 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3115 isl_pw_aff_copy(pwaff2
));
3116 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3117 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3120 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3121 __isl_take isl_pw_aff
*pwaff2
)
3123 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3126 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3127 __isl_take isl_pw_aff_list
*list
,
3128 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3129 __isl_take isl_pw_aff
*pwaff2
))
3138 ctx
= isl_pw_aff_list_get_ctx(list
);
3140 isl_die(ctx
, isl_error_invalid
,
3141 "list should contain at least one element",
3142 return isl_pw_aff_list_free(list
));
3144 res
= isl_pw_aff_copy(list
->p
[0]);
3145 for (i
= 1; i
< list
->n
; ++i
)
3146 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3148 isl_pw_aff_list_free(list
);
3152 /* Return an isl_pw_aff that maps each element in the intersection of the
3153 * domains of the elements of list to the minimal corresponding affine
3156 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3158 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3161 /* Return an isl_pw_aff that maps each element in the intersection of the
3162 * domains of the elements of list to the maximal corresponding affine
3165 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3167 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3170 /* Mark the domains of "pwaff" as rational.
3172 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3176 pwaff
= isl_pw_aff_cow(pwaff
);
3182 for (i
= 0; i
< pwaff
->n
; ++i
) {
3183 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3184 if (!pwaff
->p
[i
].set
)
3185 return isl_pw_aff_free(pwaff
);
3191 /* Mark the domains of the elements of "list" as rational.
3193 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3194 __isl_take isl_pw_aff_list
*list
)
3204 for (i
= 0; i
< n
; ++i
) {
3207 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3208 pa
= isl_pw_aff_set_rational(pa
);
3209 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3215 /* Do the parameters of "aff" match those of "space"?
3217 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3218 __isl_keep isl_space
*space
)
3220 isl_space
*aff_space
;
3226 aff_space
= isl_aff_get_domain_space(aff
);
3228 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3230 isl_space_free(aff_space
);
3234 /* Check that the domain space of "aff" matches "space".
3236 * Return 0 on success and -1 on error.
3238 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3239 __isl_keep isl_space
*space
)
3241 isl_space
*aff_space
;
3247 aff_space
= isl_aff_get_domain_space(aff
);
3249 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3253 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3254 "parameters don't match", goto error
);
3255 match
= isl_space_tuple_match(space
, isl_dim_in
,
3256 aff_space
, isl_dim_set
);
3260 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3261 "domains don't match", goto error
);
3262 isl_space_free(aff_space
);
3265 isl_space_free(aff_space
);
3271 #define NO_INTERSECT_DOMAIN
3274 #include <isl_multi_templ.c>
3277 #undef NO_INTERSECT_DOMAIN
3279 /* Remove any internal structure of the domain of "ma".
3280 * If there is any such internal structure in the input,
3281 * then the name of the corresponding space is also removed.
3283 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3284 __isl_take isl_multi_aff
*ma
)
3291 if (!ma
->space
->nested
[0])
3294 space
= isl_multi_aff_get_space(ma
);
3295 space
= isl_space_flatten_domain(space
);
3296 ma
= isl_multi_aff_reset_space(ma
, space
);
3301 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3302 * of the space to its domain.
3304 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3307 isl_local_space
*ls
;
3312 if (!isl_space_is_map(space
))
3313 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3314 "not a map space", goto error
);
3316 n_in
= isl_space_dim(space
, isl_dim_in
);
3317 space
= isl_space_domain_map(space
);
3319 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3321 isl_space_free(space
);
3325 space
= isl_space_domain(space
);
3326 ls
= isl_local_space_from_space(space
);
3327 for (i
= 0; i
< n_in
; ++i
) {
3330 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3332 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3334 isl_local_space_free(ls
);
3337 isl_space_free(space
);
3341 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3342 * of the space to its range.
3344 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3347 isl_local_space
*ls
;
3352 if (!isl_space_is_map(space
))
3353 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3354 "not a map space", goto error
);
3356 n_in
= isl_space_dim(space
, isl_dim_in
);
3357 n_out
= isl_space_dim(space
, isl_dim_out
);
3358 space
= isl_space_range_map(space
);
3360 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3362 isl_space_free(space
);
3366 space
= isl_space_domain(space
);
3367 ls
= isl_local_space_from_space(space
);
3368 for (i
= 0; i
< n_out
; ++i
) {
3371 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3372 isl_dim_set
, n_in
+ i
);
3373 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3375 isl_local_space_free(ls
);
3378 isl_space_free(space
);
3382 /* Given the space of a set and a range of set dimensions,
3383 * construct an isl_multi_aff that projects out those dimensions.
3385 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3386 __isl_take isl_space
*space
, enum isl_dim_type type
,
3387 unsigned first
, unsigned n
)
3390 isl_local_space
*ls
;
3395 if (!isl_space_is_set(space
))
3396 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3397 "expecting set space", goto error
);
3398 if (type
!= isl_dim_set
)
3399 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3400 "only set dimensions can be projected out", goto error
);
3402 dim
= isl_space_dim(space
, isl_dim_set
);
3403 if (first
+ n
> dim
)
3404 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3405 "range out of bounds", goto error
);
3407 space
= isl_space_from_domain(space
);
3408 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3411 return isl_multi_aff_alloc(space
);
3413 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3414 space
= isl_space_domain(space
);
3415 ls
= isl_local_space_from_space(space
);
3417 for (i
= 0; i
< first
; ++i
) {
3420 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3422 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3425 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3428 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3429 isl_dim_set
, first
+ n
+ i
);
3430 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3433 isl_local_space_free(ls
);
3436 isl_space_free(space
);
3440 /* Given the space of a set and a range of set dimensions,
3441 * construct an isl_pw_multi_aff that projects out those dimensions.
3443 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3444 __isl_take isl_space
*space
, enum isl_dim_type type
,
3445 unsigned first
, unsigned n
)
3449 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3450 return isl_pw_multi_aff_from_multi_aff(ma
);
3453 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3456 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3457 __isl_take isl_multi_aff
*ma
)
3459 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3460 return isl_pw_multi_aff_alloc(dom
, ma
);
3463 /* Create a piecewise multi-affine expression in the given space that maps each
3464 * input dimension to the corresponding output dimension.
3466 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3467 __isl_take isl_space
*space
)
3469 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3472 /* Add "ma2" to "ma1" and return the result.
3474 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3476 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3477 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3479 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3482 /* Add "ma2" to "ma1" and return the result.
3484 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3485 __isl_take isl_multi_aff
*ma2
)
3487 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3488 &isl_multi_aff_add_aligned
);
3491 /* Subtract "ma2" from "ma1" and return the result.
3493 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3495 static __isl_give isl_multi_aff
*isl_multi_aff_sub_aligned(
3496 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3498 return isl_multi_aff_bin_op(ma1
, ma2
, &isl_aff_sub
);
3501 /* Subtract "ma2" from "ma1" and return the result.
3503 __isl_give isl_multi_aff
*isl_multi_aff_sub(__isl_take isl_multi_aff
*ma1
,
3504 __isl_take isl_multi_aff
*ma2
)
3506 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3507 &isl_multi_aff_sub_aligned
);
3510 /* Exploit the equalities in "eq" to simplify the affine expressions.
3512 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3513 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3517 maff
= isl_multi_aff_cow(maff
);
3521 for (i
= 0; i
< maff
->n
; ++i
) {
3522 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3523 isl_basic_set_copy(eq
));
3528 isl_basic_set_free(eq
);
3531 isl_basic_set_free(eq
);
3532 isl_multi_aff_free(maff
);
3536 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3541 maff
= isl_multi_aff_cow(maff
);
3545 for (i
= 0; i
< maff
->n
; ++i
) {
3546 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3548 return isl_multi_aff_free(maff
);
3554 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3555 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3557 maff1
= isl_multi_aff_add(maff1
, maff2
);
3558 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3562 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3570 /* Return the set of domain elements where "ma1" is lexicographically
3571 * smaller than or equal to "ma2".
3573 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3574 __isl_take isl_multi_aff
*ma2
)
3576 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3579 /* Return the set of domain elements where "ma1" is lexicographically
3580 * greater than or equal to "ma2".
3582 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3583 __isl_take isl_multi_aff
*ma2
)
3586 isl_map
*map1
, *map2
;
3589 map1
= isl_map_from_multi_aff(ma1
);
3590 map2
= isl_map_from_multi_aff(ma2
);
3591 map
= isl_map_range_product(map1
, map2
);
3592 space
= isl_space_range(isl_map_get_space(map
));
3593 space
= isl_space_domain(isl_space_unwrap(space
));
3594 ge
= isl_map_lex_ge(space
);
3595 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3597 return isl_map_domain(map
);
3601 #define PW isl_pw_multi_aff
3603 #define EL isl_multi_aff
3605 #define EL_IS_ZERO is_empty
3609 #define IS_ZERO is_empty
3612 #undef DEFAULT_IS_ZERO
3613 #define DEFAULT_IS_ZERO 0
3618 #define NO_INVOLVES_DIMS
3619 #define NO_INSERT_DIMS
3623 #include <isl_pw_templ.c>
3626 #define UNION isl_union_pw_multi_aff
3628 #define PART isl_pw_multi_aff
3630 #define PARTS pw_multi_aff
3631 #define ALIGN_DOMAIN
3635 #include <isl_union_templ.c>
3637 /* Given a function "cmp" that returns the set of elements where
3638 * "ma1" is "better" than "ma2", return the intersection of this
3639 * set with "dom1" and "dom2".
3641 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
3642 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
3643 __isl_keep isl_multi_aff
*ma2
,
3644 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3645 __isl_take isl_multi_aff
*ma2
))
3651 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
3652 is_empty
= isl_set_plain_is_empty(common
);
3653 if (is_empty
>= 0 && is_empty
)
3656 return isl_set_free(common
);
3657 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
3658 better
= isl_set_intersect(common
, better
);
3663 /* Given a function "cmp" that returns the set of elements where
3664 * "ma1" is "better" than "ma2", return a piecewise multi affine
3665 * expression defined on the union of the definition domains
3666 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3667 * "pma2" on each cell. If only one of the two input functions
3668 * is defined on a given cell, then it is considered the best.
3670 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
3671 __isl_take isl_pw_multi_aff
*pma1
,
3672 __isl_take isl_pw_multi_aff
*pma2
,
3673 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3674 __isl_take isl_multi_aff
*ma2
))
3677 isl_pw_multi_aff
*res
= NULL
;
3679 isl_set
*set
= NULL
;
3684 ctx
= isl_space_get_ctx(pma1
->dim
);
3685 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
3686 isl_die(ctx
, isl_error_invalid
,
3687 "arguments should live in the same space", goto error
);
3689 if (isl_pw_multi_aff_is_empty(pma1
)) {
3690 isl_pw_multi_aff_free(pma1
);
3694 if (isl_pw_multi_aff_is_empty(pma2
)) {
3695 isl_pw_multi_aff_free(pma2
);
3699 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
3700 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
3702 for (i
= 0; i
< pma1
->n
; ++i
) {
3703 set
= isl_set_copy(pma1
->p
[i
].set
);
3704 for (j
= 0; j
< pma2
->n
; ++j
) {
3708 better
= shared_and_better(pma2
->p
[j
].set
,
3709 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
3710 pma1
->p
[i
].maff
, cmp
);
3711 is_empty
= isl_set_plain_is_empty(better
);
3712 if (is_empty
< 0 || is_empty
) {
3713 isl_set_free(better
);
3718 set
= isl_set_subtract(set
, isl_set_copy(better
));
3720 res
= isl_pw_multi_aff_add_piece(res
, better
,
3721 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3723 res
= isl_pw_multi_aff_add_piece(res
, set
,
3724 isl_multi_aff_copy(pma1
->p
[i
].maff
));
3727 for (j
= 0; j
< pma2
->n
; ++j
) {
3728 set
= isl_set_copy(pma2
->p
[j
].set
);
3729 for (i
= 0; i
< pma1
->n
; ++i
)
3730 set
= isl_set_subtract(set
,
3731 isl_set_copy(pma1
->p
[i
].set
));
3732 res
= isl_pw_multi_aff_add_piece(res
, set
,
3733 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3736 isl_pw_multi_aff_free(pma1
);
3737 isl_pw_multi_aff_free(pma2
);
3741 isl_pw_multi_aff_free(pma1
);
3742 isl_pw_multi_aff_free(pma2
);
3744 return isl_pw_multi_aff_free(res
);
3747 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
3748 __isl_take isl_pw_multi_aff
*pma1
,
3749 __isl_take isl_pw_multi_aff
*pma2
)
3751 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
3754 /* Given two piecewise multi affine expressions, return a piecewise
3755 * multi-affine expression defined on the union of the definition domains
3756 * of the inputs that is equal to the lexicographic maximum of the two
3757 * inputs on each cell. If only one of the two inputs is defined on
3758 * a given cell, then it is considered to be the maximum.
3760 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
3761 __isl_take isl_pw_multi_aff
*pma1
,
3762 __isl_take isl_pw_multi_aff
*pma2
)
3764 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3765 &pw_multi_aff_union_lexmax
);
3768 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
3769 __isl_take isl_pw_multi_aff
*pma1
,
3770 __isl_take isl_pw_multi_aff
*pma2
)
3772 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
3775 /* Given two piecewise multi affine expressions, return a piecewise
3776 * multi-affine expression defined on the union of the definition domains
3777 * of the inputs that is equal to the lexicographic minimum of the two
3778 * inputs on each cell. If only one of the two inputs is defined on
3779 * a given cell, then it is considered to be the minimum.
3781 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
3782 __isl_take isl_pw_multi_aff
*pma1
,
3783 __isl_take isl_pw_multi_aff
*pma2
)
3785 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3786 &pw_multi_aff_union_lexmin
);
3789 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
3790 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3792 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3793 &isl_multi_aff_add
);
3796 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
3797 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3799 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3803 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
3804 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3806 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3807 &isl_multi_aff_sub
);
3810 /* Subtract "pma2" from "pma1" and return the result.
3812 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
3813 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3815 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3819 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
3820 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3822 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
3825 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3826 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3828 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
3829 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3833 isl_pw_multi_aff
*res
;
3838 n
= pma1
->n
* pma2
->n
;
3839 space
= isl_space_product(isl_space_copy(pma1
->dim
),
3840 isl_space_copy(pma2
->dim
));
3841 res
= isl_pw_multi_aff_alloc_size(space
, n
);
3843 for (i
= 0; i
< pma1
->n
; ++i
) {
3844 for (j
= 0; j
< pma2
->n
; ++j
) {
3848 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
3849 isl_set_copy(pma2
->p
[j
].set
));
3850 ma
= isl_multi_aff_product(
3851 isl_multi_aff_copy(pma1
->p
[i
].maff
),
3852 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3853 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
3857 isl_pw_multi_aff_free(pma1
);
3858 isl_pw_multi_aff_free(pma2
);
3861 isl_pw_multi_aff_free(pma1
);
3862 isl_pw_multi_aff_free(pma2
);
3866 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
3867 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3869 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3870 &pw_multi_aff_product
);
3873 /* Construct a map mapping the domain of the piecewise multi-affine expression
3874 * to its range, with each dimension in the range equated to the
3875 * corresponding affine expression on its cell.
3877 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3885 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
3887 for (i
= 0; i
< pma
->n
; ++i
) {
3888 isl_multi_aff
*maff
;
3889 isl_basic_map
*bmap
;
3892 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
3893 bmap
= isl_basic_map_from_multi_aff(maff
);
3894 map_i
= isl_map_from_basic_map(bmap
);
3895 map_i
= isl_map_intersect_domain(map_i
,
3896 isl_set_copy(pma
->p
[i
].set
));
3897 map
= isl_map_union_disjoint(map
, map_i
);
3900 isl_pw_multi_aff_free(pma
);
3904 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3909 if (!isl_space_is_set(pma
->dim
))
3910 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3911 "isl_pw_multi_aff cannot be converted into an isl_set",
3912 return isl_pw_multi_aff_free(pma
));
3914 return isl_map_from_pw_multi_aff(pma
);
3917 /* Given a basic map with a single output dimension that is defined
3918 * in terms of the parameters and input dimensions using an equality,
3919 * extract an isl_aff that expresses the output dimension in terms
3920 * of the parameters and input dimensions.
3922 * Since some applications expect the result of isl_pw_multi_aff_from_map
3923 * to only contain integer affine expressions, we compute the floor
3924 * of the expression before returning.
3926 * This function shares some similarities with
3927 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3929 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
3930 __isl_take isl_basic_map
*bmap
)
3935 isl_local_space
*ls
;
3940 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
3941 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3942 "basic map should have a single output dimension",
3944 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
3945 total
= isl_basic_map_total_dim(bmap
);
3946 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3947 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
3949 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
3950 1 + total
- (offset
+ 1)) != -1)
3954 if (i
>= bmap
->n_eq
)
3955 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3956 "unable to find suitable equality", goto error
);
3957 ls
= isl_basic_map_get_local_space(bmap
);
3958 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
3961 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
3962 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3964 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3965 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
3966 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
3967 isl_basic_map_free(bmap
);
3969 aff
= isl_aff_remove_unused_divs(aff
);
3970 aff
= isl_aff_floor(aff
);
3973 isl_basic_map_free(bmap
);
3977 /* Given a basic map where each output dimension is defined
3978 * in terms of the parameters and input dimensions using an equality,
3979 * extract an isl_multi_aff that expresses the output dimensions in terms
3980 * of the parameters and input dimensions.
3982 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
3983 __isl_take isl_basic_map
*bmap
)
3992 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
3993 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3995 for (i
= 0; i
< n_out
; ++i
) {
3996 isl_basic_map
*bmap_i
;
3999 bmap_i
= isl_basic_map_copy(bmap
);
4000 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4001 i
+ 1, n_out
- (1 + i
));
4002 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4003 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4004 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4007 isl_basic_map_free(bmap
);
4012 /* Create an isl_pw_multi_aff that is equivalent to
4013 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4014 * The given basic map is such that each output dimension is defined
4015 * in terms of the parameters and input dimensions using an equality.
4017 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4018 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4022 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4023 return isl_pw_multi_aff_alloc(domain
, ma
);
4026 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4027 * This obviously only works if the input "map" is single-valued.
4028 * If so, we compute the lexicographic minimum of the image in the form
4029 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4030 * to its lexicographic minimum.
4031 * If the input is not single-valued, we produce an error.
4033 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4034 __isl_take isl_map
*map
)
4038 isl_pw_multi_aff
*pma
;
4040 sv
= isl_map_is_single_valued(map
);
4044 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4045 "map is not single-valued", goto error
);
4046 map
= isl_map_make_disjoint(map
);
4050 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4052 for (i
= 0; i
< map
->n
; ++i
) {
4053 isl_pw_multi_aff
*pma_i
;
4054 isl_basic_map
*bmap
;
4055 bmap
= isl_basic_map_copy(map
->p
[i
]);
4056 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4057 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4067 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4068 * taking into account that the output dimension at position "d"
4069 * can be represented as
4071 * x = floor((e(...) + c1) / m)
4073 * given that constraint "i" is of the form
4075 * e(...) + c1 - m x >= 0
4078 * Let "map" be of the form
4082 * We construct a mapping
4084 * A -> [A -> x = floor(...)]
4086 * apply that to the map, obtaining
4088 * [A -> x = floor(...)] -> B
4090 * and equate dimension "d" to x.
4091 * We then compute a isl_pw_multi_aff representation of the resulting map
4092 * and plug in the mapping above.
4094 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4095 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4099 isl_local_space
*ls
;
4107 isl_pw_multi_aff
*pma
;
4110 is_set
= isl_map_is_set(map
);
4112 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4113 ctx
= isl_map_get_ctx(map
);
4114 space
= isl_space_domain(isl_map_get_space(map
));
4115 n_in
= isl_space_dim(space
, isl_dim_set
);
4116 n
= isl_space_dim(space
, isl_dim_all
);
4118 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4120 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4121 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4123 isl_basic_map_free(hull
);
4125 ls
= isl_local_space_from_space(isl_space_copy(space
));
4126 aff
= isl_aff_alloc_vec(ls
, v
);
4127 aff
= isl_aff_floor(aff
);
4129 isl_space_free(space
);
4130 ma
= isl_multi_aff_from_aff(aff
);
4132 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4133 ma
= isl_multi_aff_range_product(ma
,
4134 isl_multi_aff_from_aff(aff
));
4137 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4138 map
= isl_map_apply_domain(map
, insert
);
4139 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4140 pma
= isl_pw_multi_aff_from_map(map
);
4141 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4146 /* Is constraint "c" of the form
4148 * e(...) + c1 - m x >= 0
4152 * -e(...) + c2 + m x >= 0
4154 * where m > 1 and e only depends on parameters and input dimemnsions?
4156 * "offset" is the offset of the output dimensions
4157 * "pos" is the position of output dimension x.
4159 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4161 if (isl_int_is_zero(c
[offset
+ d
]))
4163 if (isl_int_is_one(c
[offset
+ d
]))
4165 if (isl_int_is_negone(c
[offset
+ d
]))
4167 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4169 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4170 total
- (offset
+ d
+ 1)) != -1)
4175 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4177 * As a special case, we first check if there is any pair of constraints,
4178 * shared by all the basic maps in "map" that force a given dimension
4179 * to be equal to the floor of some affine combination of the input dimensions.
4181 * In particular, if we can find two constraints
4183 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4187 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4189 * where m > 1 and e only depends on parameters and input dimemnsions,
4192 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4194 * then we know that we can take
4196 * x = floor((e(...) + c1) / m)
4198 * without having to perform any computation.
4200 * Note that we know that
4204 * If c1 + c2 were 0, then we would have detected an equality during
4205 * simplification. If c1 + c2 were negative, then we would have detected
4208 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4209 __isl_take isl_map
*map
)
4215 isl_basic_map
*hull
;
4217 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4222 dim
= isl_map_dim(map
, isl_dim_out
);
4223 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4224 total
= 1 + isl_basic_map_total_dim(hull
);
4226 for (d
= 0; d
< dim
; ++d
) {
4227 for (i
= 0; i
< n
; ++i
) {
4228 if (!is_potential_div_constraint(hull
->ineq
[i
],
4231 for (j
= i
+ 1; j
< n
; ++j
) {
4232 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4233 hull
->ineq
[j
] + 1, total
- 1))
4235 isl_int_add(sum
, hull
->ineq
[i
][0],
4237 if (isl_int_abs_lt(sum
,
4238 hull
->ineq
[i
][offset
+ d
]))
4245 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4247 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4251 isl_basic_map_free(hull
);
4252 return pw_multi_aff_from_map_base(map
);
4255 isl_basic_map_free(hull
);
4259 /* Given an affine expression
4261 * [A -> B] -> f(A,B)
4263 * construct an isl_multi_aff
4267 * such that dimension "d" in B' is set to "aff" and the remaining
4268 * dimensions are set equal to the corresponding dimensions in B.
4269 * "n_in" is the dimension of the space A.
4270 * "n_out" is the dimension of the space B.
4272 * If "is_set" is set, then the affine expression is of the form
4276 * and we construct an isl_multi_aff
4280 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4281 unsigned n_in
, unsigned n_out
, int is_set
)
4285 isl_space
*space
, *space2
;
4286 isl_local_space
*ls
;
4288 space
= isl_aff_get_domain_space(aff
);
4289 ls
= isl_local_space_from_space(isl_space_copy(space
));
4290 space2
= isl_space_copy(space
);
4292 space2
= isl_space_range(isl_space_unwrap(space2
));
4293 space
= isl_space_map_from_domain_and_range(space
, space2
);
4294 ma
= isl_multi_aff_alloc(space
);
4295 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4297 for (i
= 0; i
< n_out
; ++i
) {
4300 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4301 isl_dim_set
, n_in
+ i
);
4302 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4305 isl_local_space_free(ls
);
4310 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4311 * taking into account that the dimension at position "d" can be written as
4313 * x = m a + f(..) (1)
4315 * where m is equal to "gcd".
4316 * "i" is the index of the equality in "hull" that defines f(..).
4317 * In particular, the equality is of the form
4319 * f(..) - x + m g(existentials) = 0
4323 * -f(..) + x + m g(existentials) = 0
4325 * We basically plug (1) into "map", resulting in a map with "a"
4326 * in the range instead of "x". The corresponding isl_pw_multi_aff
4327 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4329 * Specifically, given the input map
4333 * We first wrap it into a set
4337 * and define (1) on top of the corresponding space, resulting in "aff".
4338 * We use this to create an isl_multi_aff that maps the output position "d"
4339 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4340 * We plug this into the wrapped map, unwrap the result and compute the
4341 * corresponding isl_pw_multi_aff.
4342 * The result is an expression
4350 * so that we can plug that into "aff", after extending the latter to
4356 * If "map" is actually a set, then there is no "A" space, meaning
4357 * that we do not need to perform any wrapping, and that the result
4358 * of the recursive call is of the form
4362 * which is plugged into a mapping of the form
4366 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4367 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4372 isl_local_space
*ls
;
4375 isl_pw_multi_aff
*pma
, *id
;
4381 is_set
= isl_map_is_set(map
);
4383 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4384 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4385 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4390 set
= isl_map_wrap(map
);
4391 space
= isl_space_map_from_set(isl_set_get_space(set
));
4392 ma
= isl_multi_aff_identity(space
);
4393 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4394 aff
= isl_aff_alloc(ls
);
4396 isl_int_set_si(aff
->v
->el
[0], 1);
4397 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4398 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4401 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4403 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4405 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4406 set
= isl_set_preimage_multi_aff(set
, ma
);
4408 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4413 map
= isl_set_unwrap(set
);
4414 pma
= isl_pw_multi_aff_from_map(set
);
4417 space
= isl_pw_multi_aff_get_domain_space(pma
);
4418 space
= isl_space_map_from_set(space
);
4419 id
= isl_pw_multi_aff_identity(space
);
4420 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4422 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4423 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4425 isl_basic_map_free(hull
);
4429 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4431 * As a special case, we first check if all output dimensions are uniquely
4432 * defined in terms of the parameters and input dimensions over the entire
4433 * domain. If so, we extract the desired isl_pw_multi_aff directly
4434 * from the affine hull of "map" and its domain.
4436 * Otherwise, we check if any of the output dimensions is "strided".
4437 * That is, we check if can be written as
4441 * with m greater than 1, a some combination of existentiall quantified
4442 * variables and f and expression in the parameters and input dimensions.
4443 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4445 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4448 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4452 isl_basic_map
*hull
;
4462 hull
= isl_map_affine_hull(isl_map_copy(map
));
4463 sv
= isl_basic_map_plain_is_single_valued(hull
);
4465 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4467 hull
= isl_basic_map_free(hull
);
4471 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4472 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4475 isl_basic_map_free(hull
);
4476 return pw_multi_aff_from_map_check_div(map
);
4481 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4482 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4484 for (i
= 0; i
< n_out
; ++i
) {
4485 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4486 isl_int
*eq
= hull
->eq
[j
];
4487 isl_pw_multi_aff
*res
;
4489 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4490 !isl_int_is_negone(eq
[o_out
+ i
]))
4492 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4494 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4495 n_out
- (i
+ 1)) != -1)
4497 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4498 if (isl_int_is_zero(gcd
))
4500 if (isl_int_is_one(gcd
))
4503 res
= pw_multi_aff_from_map_stride(map
, hull
,
4511 isl_basic_map_free(hull
);
4512 return pw_multi_aff_from_map_check_div(map
);
4518 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4520 return isl_pw_multi_aff_from_map(set
);
4523 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4526 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4528 isl_union_pw_multi_aff
**upma
= user
;
4529 isl_pw_multi_aff
*pma
;
4531 pma
= isl_pw_multi_aff_from_map(map
);
4532 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4534 return *upma
? 0 : -1;
4537 /* Try and create an isl_union_pw_multi_aff that is equivalent
4538 * to the given isl_union_map.
4539 * The isl_union_map is required to be single-valued in each space.
4540 * Otherwise, an error is produced.
4542 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4543 __isl_take isl_union_map
*umap
)
4546 isl_union_pw_multi_aff
*upma
;
4548 space
= isl_union_map_get_space(umap
);
4549 upma
= isl_union_pw_multi_aff_empty(space
);
4550 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4551 upma
= isl_union_pw_multi_aff_free(upma
);
4552 isl_union_map_free(umap
);
4557 /* Try and create an isl_union_pw_multi_aff that is equivalent
4558 * to the given isl_union_set.
4559 * The isl_union_set is required to be a singleton in each space.
4560 * Otherwise, an error is produced.
4562 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4563 __isl_take isl_union_set
*uset
)
4565 return isl_union_pw_multi_aff_from_union_map(uset
);
4568 /* Return the piecewise affine expression "set ? 1 : 0".
4570 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4573 isl_space
*space
= isl_set_get_space(set
);
4574 isl_local_space
*ls
= isl_local_space_from_space(space
);
4575 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4576 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4578 one
= isl_aff_add_constant_si(one
, 1);
4579 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4580 set
= isl_set_complement(set
);
4581 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4586 /* Plug in "subs" for dimension "type", "pos" of "aff".
4588 * Let i be the dimension to replace and let "subs" be of the form
4592 * and "aff" of the form
4598 * (a f + d g')/(m d)
4600 * where g' is the result of plugging in "subs" in each of the integer
4603 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
4604 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
4609 aff
= isl_aff_cow(aff
);
4611 return isl_aff_free(aff
);
4613 ctx
= isl_aff_get_ctx(aff
);
4614 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
4615 isl_die(ctx
, isl_error_invalid
,
4616 "spaces don't match", return isl_aff_free(aff
));
4617 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
4618 isl_die(ctx
, isl_error_unsupported
,
4619 "cannot handle divs yet", return isl_aff_free(aff
));
4621 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
4623 return isl_aff_free(aff
);
4625 aff
->v
= isl_vec_cow(aff
->v
);
4627 return isl_aff_free(aff
);
4629 pos
+= isl_local_space_offset(aff
->ls
, type
);
4632 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
4633 aff
->v
->size
, subs
->v
->size
, v
);
4639 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4640 * expressions in "maff".
4642 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
4643 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
4644 __isl_keep isl_aff
*subs
)
4648 maff
= isl_multi_aff_cow(maff
);
4650 return isl_multi_aff_free(maff
);
4652 if (type
== isl_dim_in
)
4655 for (i
= 0; i
< maff
->n
; ++i
) {
4656 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
4658 return isl_multi_aff_free(maff
);
4664 /* Plug in "subs" for dimension "type", "pos" of "pma".
4666 * pma is of the form
4670 * while subs is of the form
4672 * v' = B_j(v) -> S_j
4674 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4675 * has a contribution in the result, in particular
4677 * C_ij(S_j) -> M_i(S_j)
4679 * Note that plugging in S_j in C_ij may also result in an empty set
4680 * and this contribution should simply be discarded.
4682 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
4683 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
4684 __isl_keep isl_pw_aff
*subs
)
4687 isl_pw_multi_aff
*res
;
4690 return isl_pw_multi_aff_free(pma
);
4692 n
= pma
->n
* subs
->n
;
4693 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
4695 for (i
= 0; i
< pma
->n
; ++i
) {
4696 for (j
= 0; j
< subs
->n
; ++j
) {
4698 isl_multi_aff
*res_ij
;
4701 common
= isl_set_intersect(
4702 isl_set_copy(pma
->p
[i
].set
),
4703 isl_set_copy(subs
->p
[j
].set
));
4704 common
= isl_set_substitute(common
,
4705 type
, pos
, subs
->p
[j
].aff
);
4706 empty
= isl_set_plain_is_empty(common
);
4707 if (empty
< 0 || empty
) {
4708 isl_set_free(common
);
4714 res_ij
= isl_multi_aff_substitute(
4715 isl_multi_aff_copy(pma
->p
[i
].maff
),
4716 type
, pos
, subs
->p
[j
].aff
);
4718 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
4722 isl_pw_multi_aff_free(pma
);
4725 isl_pw_multi_aff_free(pma
);
4726 isl_pw_multi_aff_free(res
);
4730 /* Compute the preimage of a range of dimensions in the affine expression "src"
4731 * under "ma" and put the result in "dst". The number of dimensions in "src"
4732 * that precede the range is given by "n_before". The number of dimensions
4733 * in the range is given by the number of output dimensions of "ma".
4734 * The number of dimensions that follow the range is given by "n_after".
4735 * If "has_denom" is set (to one),
4736 * then "src" and "dst" have an extra initial denominator.
4737 * "n_div_ma" is the number of existentials in "ma"
4738 * "n_div_bset" is the number of existentials in "src"
4739 * The resulting "dst" (which is assumed to have been allocated by
4740 * the caller) contains coefficients for both sets of existentials,
4741 * first those in "ma" and then those in "src".
4742 * f, c1, c2 and g are temporary objects that have been initialized
4745 * Let src represent the expression
4747 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4749 * and let ma represent the expressions
4751 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4753 * We start out with the following expression for dst:
4755 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4757 * with the multiplication factor f initially equal to 1
4758 * and f \sum_i b_i v_i kept separately.
4759 * For each x_i that we substitute, we multiply the numerator
4760 * (and denominator) of dst by c_1 = m_i and add the numerator
4761 * of the x_i expression multiplied by c_2 = f b_i,
4762 * after removing the common factors of c_1 and c_2.
4763 * The multiplication factor f also needs to be multiplied by c_1
4764 * for the next x_j, j > i.
4766 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
4767 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
4768 int n_div_ma
, int n_div_bmap
,
4769 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
4772 int n_param
, n_in
, n_out
;
4775 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
4776 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
4777 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
4779 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
4780 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
4781 isl_seq_clr(dst
+ o_dst
, n_in
);
4784 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
4787 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
4789 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
4791 isl_int_set_si(f
, 1);
4793 for (i
= 0; i
< n_out
; ++i
) {
4794 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
4796 if (isl_int_is_zero(src
[offset
]))
4798 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
4799 isl_int_mul(c2
, f
, src
[offset
]);
4800 isl_int_gcd(g
, c1
, c2
);
4801 isl_int_divexact(c1
, c1
, g
);
4802 isl_int_divexact(c2
, c2
, g
);
4804 isl_int_mul(f
, f
, c1
);
4807 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4808 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
4809 o_dst
+= 1 + n_param
;
4810 o_src
+= 1 + n_param
;
4811 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
4813 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4814 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
4817 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
4819 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4820 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
4823 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
4825 isl_int_mul(dst
[0], dst
[0], c1
);
4829 /* Compute the pullback of "aff" by the function represented by "ma".
4830 * In other words, plug in "ma" in "aff". The result is an affine expression
4831 * defined over the domain space of "ma".
4833 * If "aff" is represented by
4835 * (a(p) + b x + c(divs))/d
4837 * and ma is represented by
4839 * x = D(p) + F(y) + G(divs')
4841 * then the result is
4843 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4845 * The divs in the local space of the input are similarly adjusted
4846 * through a call to isl_local_space_preimage_multi_aff.
4848 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
4849 __isl_take isl_multi_aff
*ma
)
4851 isl_aff
*res
= NULL
;
4852 isl_local_space
*ls
;
4853 int n_div_aff
, n_div_ma
;
4854 isl_int f
, c1
, c2
, g
;
4856 ma
= isl_multi_aff_align_divs(ma
);
4860 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
4861 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
4863 ls
= isl_aff_get_domain_local_space(aff
);
4864 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
4865 res
= isl_aff_alloc(ls
);
4874 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
4883 isl_multi_aff_free(ma
);
4884 res
= isl_aff_normalize(res
);
4888 isl_multi_aff_free(ma
);
4893 /* Compute the pullback of "aff1" by the function represented by "aff2".
4894 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4895 * defined over the domain space of "aff1".
4897 * The domain of "aff1" should match the range of "aff2", which means
4898 * that it should be single-dimensional.
4900 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
4901 __isl_take isl_aff
*aff2
)
4905 ma
= isl_multi_aff_from_aff(aff2
);
4906 return isl_aff_pullback_multi_aff(aff1
, ma
);
4909 /* Compute the pullback of "ma1" by the function represented by "ma2".
4910 * In other words, plug in "ma2" in "ma1".
4912 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4914 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
4915 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
4918 isl_space
*space
= NULL
;
4920 ma2
= isl_multi_aff_align_divs(ma2
);
4921 ma1
= isl_multi_aff_cow(ma1
);
4925 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
4926 isl_multi_aff_get_space(ma1
));
4928 for (i
= 0; i
< ma1
->n
; ++i
) {
4929 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
4930 isl_multi_aff_copy(ma2
));
4935 ma1
= isl_multi_aff_reset_space(ma1
, space
);
4936 isl_multi_aff_free(ma2
);
4939 isl_space_free(space
);
4940 isl_multi_aff_free(ma2
);
4941 isl_multi_aff_free(ma1
);
4945 /* Compute the pullback of "ma1" by the function represented by "ma2".
4946 * In other words, plug in "ma2" in "ma1".
4948 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
4949 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
4951 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
4952 &isl_multi_aff_pullback_multi_aff_aligned
);
4955 /* Extend the local space of "dst" to include the divs
4956 * in the local space of "src".
4958 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
4959 __isl_keep isl_aff
*src
)
4967 return isl_aff_free(dst
);
4969 ctx
= isl_aff_get_ctx(src
);
4970 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
4971 isl_die(ctx
, isl_error_invalid
,
4972 "spaces don't match", goto error
);
4974 if (src
->ls
->div
->n_row
== 0)
4977 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
4978 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
4979 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
4982 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
4983 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
4991 return isl_aff_free(dst
);
4994 /* Adjust the local spaces of the affine expressions in "maff"
4995 * such that they all have the save divs.
4997 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
4998 __isl_take isl_multi_aff
*maff
)
5006 maff
= isl_multi_aff_cow(maff
);
5010 for (i
= 1; i
< maff
->n
; ++i
)
5011 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5012 for (i
= 1; i
< maff
->n
; ++i
) {
5013 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5015 return isl_multi_aff_free(maff
);
5021 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5023 aff
= isl_aff_cow(aff
);
5027 aff
->ls
= isl_local_space_lift(aff
->ls
);
5029 return isl_aff_free(aff
);
5034 /* Lift "maff" to a space with extra dimensions such that the result
5035 * has no more existentially quantified variables.
5036 * If "ls" is not NULL, then *ls is assigned the local space that lies
5037 * at the basis of the lifting applied to "maff".
5039 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5040 __isl_give isl_local_space
**ls
)
5054 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5055 *ls
= isl_local_space_from_space(space
);
5057 return isl_multi_aff_free(maff
);
5062 maff
= isl_multi_aff_cow(maff
);
5063 maff
= isl_multi_aff_align_divs(maff
);
5067 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5068 space
= isl_multi_aff_get_space(maff
);
5069 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5070 space
= isl_space_extend_domain_with_range(space
,
5071 isl_multi_aff_get_space(maff
));
5073 return isl_multi_aff_free(maff
);
5074 isl_space_free(maff
->space
);
5075 maff
->space
= space
;
5078 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5080 return isl_multi_aff_free(maff
);
5083 for (i
= 0; i
< maff
->n
; ++i
) {
5084 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5092 isl_local_space_free(*ls
);
5093 return isl_multi_aff_free(maff
);
5097 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5099 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5100 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5110 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5111 if (pos
< 0 || pos
>= n_out
)
5112 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5113 "index out of bounds", return NULL
);
5115 space
= isl_pw_multi_aff_get_space(pma
);
5116 space
= isl_space_drop_dims(space
, isl_dim_out
,
5117 pos
+ 1, n_out
- pos
- 1);
5118 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5120 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5121 for (i
= 0; i
< pma
->n
; ++i
) {
5123 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5124 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5130 /* Return an isl_pw_multi_aff with the given "set" as domain and
5131 * an unnamed zero-dimensional range.
5133 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5134 __isl_take isl_set
*set
)
5139 space
= isl_set_get_space(set
);
5140 space
= isl_space_from_domain(space
);
5141 ma
= isl_multi_aff_zero(space
);
5142 return isl_pw_multi_aff_alloc(set
, ma
);
5145 /* Add an isl_pw_multi_aff with the given "set" as domain and
5146 * an unnamed zero-dimensional range to *user.
5148 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5150 isl_union_pw_multi_aff
**upma
= user
;
5151 isl_pw_multi_aff
*pma
;
5153 pma
= isl_pw_multi_aff_from_domain(set
);
5154 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5159 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5160 * an unnamed zero-dimensional range.
5162 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5163 __isl_take isl_union_set
*uset
)
5166 isl_union_pw_multi_aff
*upma
;
5171 space
= isl_union_set_get_space(uset
);
5172 upma
= isl_union_pw_multi_aff_empty(space
);
5174 if (isl_union_set_foreach_set(uset
,
5175 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5178 isl_union_set_free(uset
);
5181 isl_union_set_free(uset
);
5182 isl_union_pw_multi_aff_free(upma
);
5186 /* Convert "pma" to an isl_map and add it to *umap.
5188 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5190 isl_union_map
**umap
= user
;
5193 map
= isl_map_from_pw_multi_aff(pma
);
5194 *umap
= isl_union_map_add_map(*umap
, map
);
5199 /* Construct a union map mapping the domain of the union
5200 * piecewise multi-affine expression to its range, with each dimension
5201 * in the range equated to the corresponding affine expression on its cell.
5203 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5204 __isl_take isl_union_pw_multi_aff
*upma
)
5207 isl_union_map
*umap
;
5212 space
= isl_union_pw_multi_aff_get_space(upma
);
5213 umap
= isl_union_map_empty(space
);
5215 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5216 &map_from_pw_multi_aff
, &umap
) < 0)
5219 isl_union_pw_multi_aff_free(upma
);
5222 isl_union_pw_multi_aff_free(upma
);
5223 isl_union_map_free(umap
);
5227 /* Local data for bin_entry and the callback "fn".
5229 struct isl_union_pw_multi_aff_bin_data
{
5230 isl_union_pw_multi_aff
*upma2
;
5231 isl_union_pw_multi_aff
*res
;
5232 isl_pw_multi_aff
*pma
;
5233 int (*fn
)(void **entry
, void *user
);
5236 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5237 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5239 static int bin_entry(void **entry
, void *user
)
5241 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5242 isl_pw_multi_aff
*pma
= *entry
;
5245 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
5246 data
->fn
, data
) < 0)
5252 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5253 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5254 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5255 * as *entry. The callback should adjust data->res if desired.
5257 static __isl_give isl_union_pw_multi_aff
*bin_op(
5258 __isl_take isl_union_pw_multi_aff
*upma1
,
5259 __isl_take isl_union_pw_multi_aff
*upma2
,
5260 int (*fn
)(void **entry
, void *user
))
5263 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5265 space
= isl_union_pw_multi_aff_get_space(upma2
);
5266 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5267 space
= isl_union_pw_multi_aff_get_space(upma1
);
5268 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5270 if (!upma1
|| !upma2
)
5274 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
5276 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
5277 &bin_entry
, &data
) < 0)
5280 isl_union_pw_multi_aff_free(upma1
);
5281 isl_union_pw_multi_aff_free(upma2
);
5284 isl_union_pw_multi_aff_free(upma1
);
5285 isl_union_pw_multi_aff_free(upma2
);
5286 isl_union_pw_multi_aff_free(data
.res
);
5290 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5291 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5293 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5294 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5298 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5299 isl_pw_multi_aff_get_space(pma2
));
5300 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5301 &isl_multi_aff_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_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_range_product
);
5314 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5315 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5317 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5318 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5322 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5323 isl_pw_multi_aff_get_space(pma2
));
5324 space
= isl_space_flatten_range(space
);
5325 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5326 &isl_multi_aff_flat_range_product
);
5329 /* Given two isl_pw_multi_affs A -> B and C -> D,
5330 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5332 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5333 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5335 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5336 &pw_multi_aff_flat_range_product
);
5339 /* If data->pma and *entry have the same domain space, then compute
5340 * their flat range product and the result to data->res.
5342 static int flat_range_product_entry(void **entry
, void *user
)
5344 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5345 isl_pw_multi_aff
*pma2
= *entry
;
5347 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
5348 pma2
->dim
, isl_dim_in
))
5351 pma2
= isl_pw_multi_aff_flat_range_product(
5352 isl_pw_multi_aff_copy(data
->pma
),
5353 isl_pw_multi_aff_copy(pma2
));
5355 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5360 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5361 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5363 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5364 __isl_take isl_union_pw_multi_aff
*upma1
,
5365 __isl_take isl_union_pw_multi_aff
*upma2
)
5367 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5370 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5371 * The parameters are assumed to have been aligned.
5373 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5374 * except that it works on two different isl_pw_* types.
5376 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5377 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5378 __isl_take isl_pw_aff
*pa
)
5381 isl_pw_multi_aff
*res
= NULL
;
5386 if (!isl_space_tuple_match(pma
->dim
, isl_dim_in
, pa
->dim
, isl_dim_in
))
5387 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5388 "domains don't match", goto error
);
5389 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5390 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5391 "index out of bounds", goto error
);
5394 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5396 for (i
= 0; i
< pma
->n
; ++i
) {
5397 for (j
= 0; j
< pa
->n
; ++j
) {
5399 isl_multi_aff
*res_ij
;
5402 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5403 isl_set_copy(pa
->p
[j
].set
));
5404 empty
= isl_set_plain_is_empty(common
);
5405 if (empty
< 0 || empty
) {
5406 isl_set_free(common
);
5412 res_ij
= isl_multi_aff_set_aff(
5413 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5414 isl_aff_copy(pa
->p
[j
].aff
));
5415 res_ij
= isl_multi_aff_gist(res_ij
,
5416 isl_set_copy(common
));
5418 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5422 isl_pw_multi_aff_free(pma
);
5423 isl_pw_aff_free(pa
);
5426 isl_pw_multi_aff_free(pma
);
5427 isl_pw_aff_free(pa
);
5428 return isl_pw_multi_aff_free(res
);
5431 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5433 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5434 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5435 __isl_take isl_pw_aff
*pa
)
5439 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5440 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5441 if (!isl_space_has_named_params(pma
->dim
) ||
5442 !isl_space_has_named_params(pa
->dim
))
5443 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5444 "unaligned unnamed parameters", goto error
);
5445 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5446 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5447 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5449 isl_pw_multi_aff_free(pma
);
5450 isl_pw_aff_free(pa
);
5454 /* Do the parameters of "pa" match those of "space"?
5456 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5457 __isl_keep isl_space
*space
)
5459 isl_space
*pa_space
;
5465 pa_space
= isl_pw_aff_get_space(pa
);
5467 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5469 isl_space_free(pa_space
);
5473 /* Check that the domain space of "pa" matches "space".
5475 * Return 0 on success and -1 on error.
5477 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5478 __isl_keep isl_space
*space
)
5480 isl_space
*pa_space
;
5486 pa_space
= isl_pw_aff_get_space(pa
);
5488 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5492 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5493 "parameters don't match", goto error
);
5494 match
= isl_space_tuple_match(space
, isl_dim_in
, pa_space
, isl_dim_in
);
5498 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5499 "domains don't match", goto error
);
5500 isl_space_free(pa_space
);
5503 isl_space_free(pa_space
);
5510 #include <isl_multi_templ.c>
5512 /* Scale the elements of "pma" by the corresponding elements of "mv".
5514 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5515 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5519 pma
= isl_pw_multi_aff_cow(pma
);
5522 if (!isl_space_tuple_match(pma
->dim
, isl_dim_out
,
5523 mv
->space
, isl_dim_set
))
5524 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5525 "spaces don't match", goto error
);
5526 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5527 mv
->space
, isl_dim_param
)) {
5528 pma
= isl_pw_multi_aff_align_params(pma
,
5529 isl_multi_val_get_space(mv
));
5530 mv
= isl_multi_val_align_params(mv
,
5531 isl_pw_multi_aff_get_space(pma
));
5536 for (i
= 0; i
< pma
->n
; ++i
) {
5537 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5538 isl_multi_val_copy(mv
));
5539 if (!pma
->p
[i
].maff
)
5543 isl_multi_val_free(mv
);
5546 isl_multi_val_free(mv
);
5547 isl_pw_multi_aff_free(pma
);
5551 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5552 * mv contains the mv argument.
5553 * res collects the results.
5555 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5557 isl_union_pw_multi_aff
*res
;
5560 /* This function is called for each entry of an isl_union_pw_multi_aff.
5561 * If the space of the entry matches that of data->mv,
5562 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5565 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5567 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5568 isl_pw_multi_aff
*pma
= *entry
;
5572 if (!isl_space_tuple_match(pma
->dim
, isl_dim_out
,
5573 data
->mv
->space
, isl_dim_set
))
5576 pma
= isl_pw_multi_aff_copy(pma
);
5577 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5578 isl_multi_val_copy(data
->mv
));
5579 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
5586 /* Scale the elements of "upma" by the corresponding elements of "mv",
5587 * for those entries that match the space of "mv".
5589 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
5590 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
5592 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
5594 upma
= isl_union_pw_multi_aff_align_params(upma
,
5595 isl_multi_val_get_space(mv
));
5596 mv
= isl_multi_val_align_params(mv
,
5597 isl_union_pw_multi_aff_get_space(upma
));
5602 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->dim
),
5604 if (isl_hash_table_foreach(upma
->dim
->ctx
, &upma
->table
,
5605 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
5608 isl_multi_val_free(mv
);
5609 isl_union_pw_multi_aff_free(upma
);
5612 isl_multi_val_free(mv
);
5613 isl_union_pw_multi_aff_free(upma
);
5617 /* Construct and return a piecewise multi affine expression
5618 * in the given space with value zero in each of the output dimensions and
5619 * a universe domain.
5621 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
5623 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
5626 /* Construct and return a piecewise multi affine expression
5627 * that is equal to the given piecewise affine expression.
5629 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
5630 __isl_take isl_pw_aff
*pa
)
5634 isl_pw_multi_aff
*pma
;
5639 space
= isl_pw_aff_get_space(pa
);
5640 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
5642 for (i
= 0; i
< pa
->n
; ++i
) {
5646 set
= isl_set_copy(pa
->p
[i
].set
);
5647 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
5648 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
5651 isl_pw_aff_free(pa
);
5655 /* Construct a set or map mapping the shared (parameter) domain
5656 * of the piecewise affine expressions to the range of "mpa"
5657 * with each dimension in the range equated to the
5658 * corresponding piecewise affine expression.
5660 static __isl_give isl_map
*map_from_multi_pw_aff(
5661 __isl_take isl_multi_pw_aff
*mpa
)
5670 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
5671 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5672 "invalid space", return isl_multi_pw_aff_free(mpa
));
5674 space
= isl_multi_pw_aff_get_domain_space(mpa
);
5675 map
= isl_map_universe(isl_space_from_domain(space
));
5677 for (i
= 0; i
< mpa
->n
; ++i
) {
5681 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
5682 map_i
= map_from_pw_aff(pa
);
5684 map
= isl_map_flat_range_product(map
, map_i
);
5687 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
5689 isl_multi_pw_aff_free(mpa
);
5693 /* Construct a map mapping the shared domain
5694 * of the piecewise affine expressions to the range of "mpa"
5695 * with each dimension in the range equated to the
5696 * corresponding piecewise affine expression.
5698 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
5702 if (isl_space_is_set(mpa
->space
))
5703 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5704 "space of input is not a map", goto error
);
5706 return map_from_multi_pw_aff(mpa
);
5708 isl_multi_pw_aff_free(mpa
);
5712 /* Construct a set mapping the shared parameter domain
5713 * of the piecewise affine expressions to the space of "mpa"
5714 * with each dimension in the range equated to the
5715 * corresponding piecewise affine expression.
5717 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
5721 if (!isl_space_is_set(mpa
->space
))
5722 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5723 "space of input is not a set", goto error
);
5725 return map_from_multi_pw_aff(mpa
);
5727 isl_multi_pw_aff_free(mpa
);
5731 /* Construct and return a piecewise multi affine expression
5732 * that is equal to the given multi piecewise affine expression
5733 * on the shared domain of the piecewise affine expressions.
5735 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
5736 __isl_take isl_multi_pw_aff
*mpa
)
5741 isl_pw_multi_aff
*pma
;
5746 space
= isl_multi_pw_aff_get_space(mpa
);
5749 isl_multi_pw_aff_free(mpa
);
5750 return isl_pw_multi_aff_zero(space
);
5753 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
5754 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
5756 for (i
= 1; i
< mpa
->n
; ++i
) {
5757 isl_pw_multi_aff
*pma_i
;
5759 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
5760 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
5761 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
5764 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
5766 isl_multi_pw_aff_free(mpa
);
5770 /* Construct and return a multi piecewise affine expression
5771 * that is equal to the given multi affine expression.
5773 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
5774 __isl_take isl_multi_aff
*ma
)
5777 isl_multi_pw_aff
*mpa
;
5782 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
5783 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
5785 for (i
= 0; i
< n
; ++i
) {
5788 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
5789 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
5792 isl_multi_aff_free(ma
);
5796 /* Construct and return a multi piecewise affine expression
5797 * that is equal to the given piecewise multi affine expression.
5799 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
5800 __isl_take isl_pw_multi_aff
*pma
)
5804 isl_multi_pw_aff
*mpa
;
5809 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5810 space
= isl_pw_multi_aff_get_space(pma
);
5811 mpa
= isl_multi_pw_aff_alloc(space
);
5813 for (i
= 0; i
< n
; ++i
) {
5816 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
5817 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
5820 isl_pw_multi_aff_free(pma
);
5824 /* Do "pa1" and "pa2" represent the same function?
5826 * We first check if they are obviously equal.
5827 * If not, we convert them to maps and check if those are equal.
5829 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
5832 isl_map
*map1
, *map2
;
5837 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
5838 if (equal
< 0 || equal
)
5841 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
5842 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
5843 equal
= isl_map_is_equal(map1
, map2
);
5850 /* Do "mpa1" and "mpa2" represent the same function?
5852 * Note that we cannot convert the entire isl_multi_pw_aff
5853 * to a map because the domains of the piecewise affine expressions
5854 * may not be the same.
5856 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
5857 __isl_keep isl_multi_pw_aff
*mpa2
)
5865 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
5866 mpa2
->space
, isl_dim_param
)) {
5867 if (!isl_space_has_named_params(mpa1
->space
))
5869 if (!isl_space_has_named_params(mpa2
->space
))
5871 mpa1
= isl_multi_pw_aff_copy(mpa1
);
5872 mpa2
= isl_multi_pw_aff_copy(mpa2
);
5873 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
5874 isl_multi_pw_aff_get_space(mpa2
));
5875 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
5876 isl_multi_pw_aff_get_space(mpa1
));
5877 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
5878 isl_multi_pw_aff_free(mpa1
);
5879 isl_multi_pw_aff_free(mpa2
);
5883 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
5884 if (equal
< 0 || !equal
)
5887 for (i
= 0; i
< mpa1
->n
; ++i
) {
5888 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
5889 if (equal
< 0 || !equal
)
5896 /* Coalesce the elements of "mpa".
5898 * Note that such coalescing does not change the meaning of "mpa"
5899 * so there is no need to cow. We do need to be careful not to
5900 * destroy any other copies of "mpa" in case of failure.
5902 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
5903 __isl_take isl_multi_pw_aff
*mpa
)
5910 for (i
= 0; i
< mpa
->n
; ++i
) {
5911 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
5912 pa
= isl_pw_aff_coalesce(pa
);
5914 return isl_multi_pw_aff_free(mpa
);
5915 isl_pw_aff_free(mpa
->p
[i
]);
5922 /* Compute the pullback of "mpa" by the function represented by "ma".
5923 * In other words, plug in "ma" in "mpa".
5925 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5927 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
5928 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
5931 isl_space
*space
= NULL
;
5933 mpa
= isl_multi_pw_aff_cow(mpa
);
5937 space
= isl_space_join(isl_multi_aff_get_space(ma
),
5938 isl_multi_pw_aff_get_space(mpa
));
5942 for (i
= 0; i
< mpa
->n
; ++i
) {
5943 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
5944 isl_multi_aff_copy(ma
));
5949 isl_multi_aff_free(ma
);
5950 isl_space_free(mpa
->space
);
5954 isl_space_free(space
);
5955 isl_multi_pw_aff_free(mpa
);
5956 isl_multi_aff_free(ma
);
5960 /* Compute the pullback of "mpa" by the function represented by "ma".
5961 * In other words, plug in "ma" in "mpa".
5963 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
5964 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
5968 if (isl_space_match(mpa
->space
, isl_dim_param
,
5969 ma
->space
, isl_dim_param
))
5970 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
5971 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
5972 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
5973 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
5975 isl_multi_pw_aff_free(mpa
);
5976 isl_multi_aff_free(ma
);
5980 /* Compute the pullback of "mpa" by the function represented by "pma".
5981 * In other words, plug in "pma" in "mpa".
5983 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5985 static __isl_give isl_multi_pw_aff
*
5986 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5987 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
5990 isl_space
*space
= NULL
;
5992 mpa
= isl_multi_pw_aff_cow(mpa
);
5996 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
5997 isl_multi_pw_aff_get_space(mpa
));
5999 for (i
= 0; i
< mpa
->n
; ++i
) {
6000 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6001 isl_pw_multi_aff_copy(pma
));
6006 isl_pw_multi_aff_free(pma
);
6007 isl_space_free(mpa
->space
);
6011 isl_space_free(space
);
6012 isl_multi_pw_aff_free(mpa
);
6013 isl_pw_multi_aff_free(pma
);
6017 /* Compute the pullback of "mpa" by the function represented by "pma".
6018 * In other words, plug in "pma" in "mpa".
6020 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6021 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6025 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6026 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6027 mpa
= isl_multi_pw_aff_align_params(mpa
,
6028 isl_pw_multi_aff_get_space(pma
));
6029 pma
= isl_pw_multi_aff_align_params(pma
,
6030 isl_multi_pw_aff_get_space(mpa
));
6031 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6033 isl_multi_pw_aff_free(mpa
);
6034 isl_pw_multi_aff_free(pma
);
6038 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6039 * with the domain of "aff". The domain of the result is the same
6041 * "mpa" and "aff" are assumed to have been aligned.
6043 * We first extract the parametric constant from "aff", defined
6044 * over the correct domain.
6045 * Then we add the appropriate combinations of the members of "mpa".
6046 * Finally, we add the integer divisions through recursive calls.
6048 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6049 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6051 int i
, n_param
, n_in
, n_div
;
6057 n_param
= isl_aff_dim(aff
, isl_dim_param
);
6058 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6059 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6061 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6062 tmp
= isl_aff_copy(aff
);
6063 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6064 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6065 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6066 isl_space_dim(space
, isl_dim_set
));
6067 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6068 pa
= isl_pw_aff_from_aff(tmp
);
6070 for (i
= 0; i
< n_in
; ++i
) {
6073 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6075 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6076 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6077 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6078 pa
= isl_pw_aff_add(pa
, pa_i
);
6081 for (i
= 0; i
< n_div
; ++i
) {
6085 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6087 div
= isl_aff_get_div(aff
, i
);
6088 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6089 isl_multi_pw_aff_copy(mpa
), div
);
6090 pa_i
= isl_pw_aff_floor(pa_i
);
6091 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6092 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6093 pa
= isl_pw_aff_add(pa
, pa_i
);
6096 isl_multi_pw_aff_free(mpa
);
6102 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6103 * with the domain of "aff". The domain of the result is the same
6106 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6107 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6111 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6112 mpa
->space
, isl_dim_param
))
6113 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6115 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6116 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6118 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6121 isl_multi_pw_aff_free(mpa
);
6125 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6126 * with the domain of "pa". The domain of the result is the same
6128 * "mpa" and "pa" are assumed to have been aligned.
6130 * We consider each piece in turn. Note that the domains of the
6131 * pieces are assumed to be disjoint and they remain disjoint
6132 * after taking the preimage (over the same function).
6134 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6135 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6144 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6145 isl_pw_aff_get_space(pa
));
6146 res
= isl_pw_aff_empty(space
);
6148 for (i
= 0; i
< pa
->n
; ++i
) {
6152 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6153 isl_multi_pw_aff_copy(mpa
),
6154 isl_aff_copy(pa
->p
[i
].aff
));
6155 domain
= isl_set_copy(pa
->p
[i
].set
);
6156 domain
= isl_set_preimage_multi_pw_aff(domain
,
6157 isl_multi_pw_aff_copy(mpa
));
6158 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6159 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6162 isl_pw_aff_free(pa
);
6163 isl_multi_pw_aff_free(mpa
);
6166 isl_pw_aff_free(pa
);
6167 isl_multi_pw_aff_free(mpa
);
6171 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6172 * with the domain of "pa". The domain of the result is the same
6175 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6176 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6180 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6181 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6183 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6184 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6186 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6188 isl_pw_aff_free(pa
);
6189 isl_multi_pw_aff_free(mpa
);
6193 /* Compute the pullback of "pa" by the function represented by "mpa".
6194 * In other words, plug in "mpa" in "pa".
6195 * "pa" and "mpa" are assumed to have been aligned.
6197 * The pullback is computed by applying "pa" to "mpa".
6199 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6200 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6202 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6205 /* Compute the pullback of "pa" by the function represented by "mpa".
6206 * In other words, plug in "mpa" in "pa".
6208 * The pullback is computed by applying "pa" to "mpa".
6210 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6211 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6213 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6216 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6217 * In other words, plug in "mpa2" in "mpa1".
6219 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6221 * We pullback each member of "mpa1" in turn.
6223 static __isl_give isl_multi_pw_aff
*
6224 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6225 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6228 isl_space
*space
= NULL
;
6230 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6234 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6235 isl_multi_pw_aff_get_space(mpa1
));
6237 for (i
= 0; i
< mpa1
->n
; ++i
) {
6238 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6239 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6244 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6246 isl_multi_pw_aff_free(mpa2
);
6249 isl_space_free(space
);
6250 isl_multi_pw_aff_free(mpa1
);
6251 isl_multi_pw_aff_free(mpa2
);
6255 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6256 * In other words, plug in "mpa2" in "mpa1".
6258 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6259 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6261 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6262 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);