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 __isl_null isl_aff
*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
);
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
);
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", goto error
);
2432 return map_from_pw_aff(pwaff
);
2434 isl_pw_aff_free(pwaff
);
2438 /* Construct a one-dimensional set with as parameter domain
2439 * the domain of pwaff and the single set dimension
2440 * corresponding to the affine expressions.
2442 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2446 if (!isl_space_is_set(pwaff
->dim
))
2447 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2448 "space of input is not a set", goto error
);
2449 return map_from_pw_aff(pwaff
);
2451 isl_pw_aff_free(pwaff
);
2455 /* Return a set containing those elements in the domain
2456 * of pwaff where it is non-negative.
2458 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2466 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2468 for (i
= 0; i
< pwaff
->n
; ++i
) {
2469 isl_basic_set
*bset
;
2473 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2474 bset
= aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2476 set_i
= isl_set_from_basic_set(bset
);
2477 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
2478 set
= isl_set_union_disjoint(set
, set_i
);
2481 isl_pw_aff_free(pwaff
);
2486 /* Return a set containing those elements in the domain
2487 * of pwaff where it is zero (if complement is 0) or not zero
2488 * (if complement is 1).
2490 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
2499 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2501 for (i
= 0; i
< pwaff
->n
; ++i
) {
2502 isl_basic_set
*bset
;
2503 isl_set
*set_i
, *zero
;
2506 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2507 bset
= aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2509 zero
= isl_set_from_basic_set(bset
);
2510 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2512 set_i
= isl_set_subtract(set_i
, zero
);
2514 set_i
= isl_set_intersect(set_i
, zero
);
2515 set
= isl_set_union_disjoint(set
, set_i
);
2518 isl_pw_aff_free(pwaff
);
2523 /* Return a set containing those elements in the domain
2524 * of pwaff where it is zero.
2526 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2528 return pw_aff_zero_set(pwaff
, 0);
2531 /* Return a set containing those elements in the domain
2532 * of pwaff where it is not zero.
2534 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2536 return pw_aff_zero_set(pwaff
, 1);
2539 /* Return a set containing those elements in the shared domain
2540 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2542 * We compute the difference on the shared domain and then construct
2543 * the set of values where this difference is non-negative.
2544 * If strict is set, we first subtract 1 from the difference.
2545 * If equal is set, we only return the elements where pwaff1 and pwaff2
2548 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2549 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2551 isl_set
*set1
, *set2
;
2553 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2554 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2555 set1
= isl_set_intersect(set1
, set2
);
2556 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2557 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2558 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2561 isl_space
*dim
= isl_set_get_space(set1
);
2563 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2564 aff
= isl_aff_add_constant_si(aff
, -1);
2565 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2570 return isl_pw_aff_zero_set(pwaff1
);
2571 return isl_pw_aff_nonneg_set(pwaff1
);
2574 /* Return a set containing those elements in the shared domain
2575 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2577 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2578 __isl_take isl_pw_aff
*pwaff2
)
2580 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2583 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2584 __isl_take isl_pw_aff
*pwaff2
)
2586 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2589 /* Return a set containing those elements in the shared domain
2590 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2592 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2593 __isl_take isl_pw_aff
*pwaff2
)
2595 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2598 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2599 __isl_take isl_pw_aff
*pwaff2
)
2601 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2604 /* Return a set containing those elements in the shared domain
2605 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2607 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2608 __isl_take isl_pw_aff
*pwaff2
)
2610 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2613 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2614 __isl_take isl_pw_aff
*pwaff2
)
2616 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2619 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2620 __isl_take isl_pw_aff
*pwaff2
)
2622 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2625 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2626 __isl_take isl_pw_aff
*pwaff2
)
2628 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2631 /* Return a set containing those elements in the shared domain
2632 * of the elements of list1 and list2 where each element in list1
2633 * has the relation specified by "fn" with each element in list2.
2635 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2636 __isl_take isl_pw_aff_list
*list2
,
2637 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2638 __isl_take isl_pw_aff
*pwaff2
))
2644 if (!list1
|| !list2
)
2647 ctx
= isl_pw_aff_list_get_ctx(list1
);
2648 if (list1
->n
< 1 || list2
->n
< 1)
2649 isl_die(ctx
, isl_error_invalid
,
2650 "list should contain at least one element", goto error
);
2652 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2653 for (i
= 0; i
< list1
->n
; ++i
)
2654 for (j
= 0; j
< list2
->n
; ++j
) {
2657 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2658 isl_pw_aff_copy(list2
->p
[j
]));
2659 set
= isl_set_intersect(set
, set_ij
);
2662 isl_pw_aff_list_free(list1
);
2663 isl_pw_aff_list_free(list2
);
2666 isl_pw_aff_list_free(list1
);
2667 isl_pw_aff_list_free(list2
);
2671 /* Return a set containing those elements in the shared domain
2672 * of the elements of list1 and list2 where each element in list1
2673 * is equal to each element in list2.
2675 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2676 __isl_take isl_pw_aff_list
*list2
)
2678 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2681 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2682 __isl_take isl_pw_aff_list
*list2
)
2684 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2687 /* Return a set containing those elements in the shared domain
2688 * of the elements of list1 and list2 where each element in list1
2689 * is less than or equal to each element in list2.
2691 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2692 __isl_take isl_pw_aff_list
*list2
)
2694 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2697 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2698 __isl_take isl_pw_aff_list
*list2
)
2700 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2703 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2704 __isl_take isl_pw_aff_list
*list2
)
2706 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
2709 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
2710 __isl_take isl_pw_aff_list
*list2
)
2712 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
2716 /* Return a set containing those elements in the shared domain
2717 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2719 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2720 __isl_take isl_pw_aff
*pwaff2
)
2722 isl_set
*set_lt
, *set_gt
;
2724 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
2725 isl_pw_aff_copy(pwaff2
));
2726 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
2727 return isl_set_union_disjoint(set_lt
, set_gt
);
2730 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2731 __isl_take isl_pw_aff
*pwaff2
)
2733 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
2736 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
2741 if (isl_int_is_one(v
))
2743 if (!isl_int_is_pos(v
))
2744 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2745 "factor needs to be positive",
2746 return isl_pw_aff_free(pwaff
));
2747 pwaff
= isl_pw_aff_cow(pwaff
);
2753 for (i
= 0; i
< pwaff
->n
; ++i
) {
2754 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
2755 if (!pwaff
->p
[i
].aff
)
2756 return isl_pw_aff_free(pwaff
);
2762 /* Divide "pa" by "f".
2764 __isl_give isl_pw_aff
*isl_pw_aff_scale_down_val(__isl_take isl_pw_aff
*pa
,
2765 __isl_take isl_val
*f
)
2772 if (isl_val_is_one(f
)) {
2777 if (!isl_val_is_rat(f
))
2778 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2779 "expecting rational factor", goto error
);
2780 if (!isl_val_is_pos(f
))
2781 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
2782 "factor needs to be positive", goto error
);
2784 pa
= isl_pw_aff_cow(pa
);
2790 for (i
= 0; i
< pa
->n
; ++i
) {
2791 pa
->p
[i
].aff
= isl_aff_scale_down_val(pa
->p
[i
].aff
,
2800 isl_pw_aff_free(pa
);
2805 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
2809 pwaff
= isl_pw_aff_cow(pwaff
);
2815 for (i
= 0; i
< pwaff
->n
; ++i
) {
2816 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
2817 if (!pwaff
->p
[i
].aff
)
2818 return isl_pw_aff_free(pwaff
);
2824 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
2828 pwaff
= isl_pw_aff_cow(pwaff
);
2834 for (i
= 0; i
< pwaff
->n
; ++i
) {
2835 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
2836 if (!pwaff
->p
[i
].aff
)
2837 return isl_pw_aff_free(pwaff
);
2843 /* Assuming that "cond1" and "cond2" are disjoint,
2844 * return an affine expression that is equal to pwaff1 on cond1
2845 * and to pwaff2 on cond2.
2847 static __isl_give isl_pw_aff
*isl_pw_aff_select(
2848 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
2849 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
2851 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
2852 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
2854 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
2857 /* Return an affine expression that is equal to pwaff_true for elements
2858 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2860 * That is, return cond ? pwaff_true : pwaff_false;
2862 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
2863 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
2865 isl_set
*cond_true
, *cond_false
;
2867 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
2868 cond_false
= isl_pw_aff_zero_set(cond
);
2869 return isl_pw_aff_select(cond_true
, pwaff_true
,
2870 cond_false
, pwaff_false
);
2873 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
2878 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
2881 /* Check whether pwaff is a piecewise constant.
2883 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
2890 for (i
= 0; i
< pwaff
->n
; ++i
) {
2891 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
2892 if (is_cst
< 0 || !is_cst
)
2899 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
2900 __isl_take isl_aff
*aff2
)
2902 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2903 return isl_aff_mul(aff2
, aff1
);
2905 if (!isl_aff_is_cst(aff2
))
2906 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2907 "at least one affine expression should be constant",
2910 aff1
= isl_aff_cow(aff1
);
2914 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2915 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2925 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
2927 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
2928 __isl_take isl_aff
*aff2
)
2933 is_cst
= isl_aff_is_cst(aff2
);
2937 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
2938 "second argument should be a constant", goto error
);
2943 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
2945 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2946 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2949 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
2950 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
2953 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2954 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2965 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2966 __isl_take isl_pw_aff
*pwaff2
)
2968 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2971 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2972 __isl_take isl_pw_aff
*pwaff2
)
2974 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2977 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2978 __isl_take isl_pw_aff
*pwaff2
)
2980 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2983 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2984 __isl_take isl_pw_aff
*pwaff2
)
2986 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2989 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2990 __isl_take isl_pw_aff
*pwaff2
)
2992 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2995 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2996 __isl_take isl_pw_aff
*pa2
)
2998 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3001 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3003 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3004 __isl_take isl_pw_aff
*pa2
)
3008 is_cst
= isl_pw_aff_is_cst(pa2
);
3012 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3013 "second argument should be a piecewise constant",
3015 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3017 isl_pw_aff_free(pa1
);
3018 isl_pw_aff_free(pa2
);
3022 /* Compute the quotient of the integer division of "pa1" by "pa2"
3023 * with rounding towards zero.
3024 * "pa2" is assumed to be a piecewise constant.
3026 * In particular, return
3028 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3031 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3032 __isl_take isl_pw_aff
*pa2
)
3038 is_cst
= isl_pw_aff_is_cst(pa2
);
3042 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3043 "second argument should be a piecewise constant",
3046 pa1
= isl_pw_aff_div(pa1
, pa2
);
3048 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3049 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3050 c
= isl_pw_aff_ceil(pa1
);
3051 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3053 isl_pw_aff_free(pa1
);
3054 isl_pw_aff_free(pa2
);
3058 /* Compute the remainder of the integer division of "pa1" by "pa2"
3059 * with rounding towards zero.
3060 * "pa2" is assumed to be a piecewise constant.
3062 * In particular, return
3064 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3067 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3068 __isl_take isl_pw_aff
*pa2
)
3073 is_cst
= isl_pw_aff_is_cst(pa2
);
3077 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3078 "second argument should be a piecewise constant",
3080 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3081 res
= isl_pw_aff_mul(pa2
, res
);
3082 res
= isl_pw_aff_sub(pa1
, res
);
3085 isl_pw_aff_free(pa1
);
3086 isl_pw_aff_free(pa2
);
3090 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3091 __isl_take isl_pw_aff
*pwaff2
)
3096 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3097 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3098 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3099 isl_pw_aff_copy(pwaff2
));
3100 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3101 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3104 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3105 __isl_take isl_pw_aff
*pwaff2
)
3107 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3110 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3111 __isl_take isl_pw_aff
*pwaff2
)
3116 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3117 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3118 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3119 isl_pw_aff_copy(pwaff2
));
3120 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3121 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3124 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3125 __isl_take isl_pw_aff
*pwaff2
)
3127 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3130 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3131 __isl_take isl_pw_aff_list
*list
,
3132 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3133 __isl_take isl_pw_aff
*pwaff2
))
3142 ctx
= isl_pw_aff_list_get_ctx(list
);
3144 isl_die(ctx
, isl_error_invalid
,
3145 "list should contain at least one element", goto error
);
3147 res
= isl_pw_aff_copy(list
->p
[0]);
3148 for (i
= 1; i
< list
->n
; ++i
)
3149 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3151 isl_pw_aff_list_free(list
);
3154 isl_pw_aff_list_free(list
);
3158 /* Return an isl_pw_aff that maps each element in the intersection of the
3159 * domains of the elements of list to the minimal corresponding affine
3162 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3164 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3167 /* Return an isl_pw_aff that maps each element in the intersection of the
3168 * domains of the elements of list to the maximal corresponding affine
3171 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3173 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3176 /* Mark the domains of "pwaff" as rational.
3178 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3182 pwaff
= isl_pw_aff_cow(pwaff
);
3188 for (i
= 0; i
< pwaff
->n
; ++i
) {
3189 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3190 if (!pwaff
->p
[i
].set
)
3191 return isl_pw_aff_free(pwaff
);
3197 /* Mark the domains of the elements of "list" as rational.
3199 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3200 __isl_take isl_pw_aff_list
*list
)
3210 for (i
= 0; i
< n
; ++i
) {
3213 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3214 pa
= isl_pw_aff_set_rational(pa
);
3215 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3221 /* Do the parameters of "aff" match those of "space"?
3223 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3224 __isl_keep isl_space
*space
)
3226 isl_space
*aff_space
;
3232 aff_space
= isl_aff_get_domain_space(aff
);
3234 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3236 isl_space_free(aff_space
);
3240 /* Check that the domain space of "aff" matches "space".
3242 * Return 0 on success and -1 on error.
3244 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3245 __isl_keep isl_space
*space
)
3247 isl_space
*aff_space
;
3253 aff_space
= isl_aff_get_domain_space(aff
);
3255 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3259 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3260 "parameters don't match", goto error
);
3261 match
= isl_space_tuple_match(space
, isl_dim_in
,
3262 aff_space
, isl_dim_set
);
3266 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3267 "domains don't match", goto error
);
3268 isl_space_free(aff_space
);
3271 isl_space_free(aff_space
);
3277 #define NO_INTERSECT_DOMAIN
3280 #include <isl_multi_templ.c>
3283 #undef NO_INTERSECT_DOMAIN
3285 /* Remove any internal structure of the domain of "ma".
3286 * If there is any such internal structure in the input,
3287 * then the name of the corresponding space is also removed.
3289 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3290 __isl_take isl_multi_aff
*ma
)
3297 if (!ma
->space
->nested
[0])
3300 space
= isl_multi_aff_get_space(ma
);
3301 space
= isl_space_flatten_domain(space
);
3302 ma
= isl_multi_aff_reset_space(ma
, space
);
3307 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3308 * of the space to its domain.
3310 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3313 isl_local_space
*ls
;
3318 if (!isl_space_is_map(space
))
3319 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3320 "not a map space", goto error
);
3322 n_in
= isl_space_dim(space
, isl_dim_in
);
3323 space
= isl_space_domain_map(space
);
3325 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3327 isl_space_free(space
);
3331 space
= isl_space_domain(space
);
3332 ls
= isl_local_space_from_space(space
);
3333 for (i
= 0; i
< n_in
; ++i
) {
3336 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3338 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3340 isl_local_space_free(ls
);
3343 isl_space_free(space
);
3347 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3348 * of the space to its range.
3350 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3353 isl_local_space
*ls
;
3358 if (!isl_space_is_map(space
))
3359 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3360 "not a map space", goto error
);
3362 n_in
= isl_space_dim(space
, isl_dim_in
);
3363 n_out
= isl_space_dim(space
, isl_dim_out
);
3364 space
= isl_space_range_map(space
);
3366 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3368 isl_space_free(space
);
3372 space
= isl_space_domain(space
);
3373 ls
= isl_local_space_from_space(space
);
3374 for (i
= 0; i
< n_out
; ++i
) {
3377 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3378 isl_dim_set
, n_in
+ i
);
3379 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3381 isl_local_space_free(ls
);
3384 isl_space_free(space
);
3388 /* Given the space of a set and a range of set dimensions,
3389 * construct an isl_multi_aff that projects out those dimensions.
3391 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3392 __isl_take isl_space
*space
, enum isl_dim_type type
,
3393 unsigned first
, unsigned n
)
3396 isl_local_space
*ls
;
3401 if (!isl_space_is_set(space
))
3402 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3403 "expecting set space", goto error
);
3404 if (type
!= isl_dim_set
)
3405 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3406 "only set dimensions can be projected out", goto error
);
3408 dim
= isl_space_dim(space
, isl_dim_set
);
3409 if (first
+ n
> dim
)
3410 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3411 "range out of bounds", goto error
);
3413 space
= isl_space_from_domain(space
);
3414 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3417 return isl_multi_aff_alloc(space
);
3419 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3420 space
= isl_space_domain(space
);
3421 ls
= isl_local_space_from_space(space
);
3423 for (i
= 0; i
< first
; ++i
) {
3426 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3428 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3431 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3434 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3435 isl_dim_set
, first
+ n
+ i
);
3436 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3439 isl_local_space_free(ls
);
3442 isl_space_free(space
);
3446 /* Given the space of a set and a range of set dimensions,
3447 * construct an isl_pw_multi_aff that projects out those dimensions.
3449 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3450 __isl_take isl_space
*space
, enum isl_dim_type type
,
3451 unsigned first
, unsigned n
)
3455 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3456 return isl_pw_multi_aff_from_multi_aff(ma
);
3459 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3462 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3463 __isl_take isl_multi_aff
*ma
)
3465 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3466 return isl_pw_multi_aff_alloc(dom
, ma
);
3469 /* Create a piecewise multi-affine expression in the given space that maps each
3470 * input dimension to the corresponding output dimension.
3472 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3473 __isl_take isl_space
*space
)
3475 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3478 /* Add "ma2" to "ma1" and return the result.
3480 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3482 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3483 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3485 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3488 /* Add "ma2" to "ma1" and return the result.
3490 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3491 __isl_take isl_multi_aff
*ma2
)
3493 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3494 &isl_multi_aff_add_aligned
);
3497 /* Subtract "ma2" from "ma1" and return the result.
3499 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3501 static __isl_give isl_multi_aff
*isl_multi_aff_sub_aligned(
3502 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3504 return isl_multi_aff_bin_op(ma1
, ma2
, &isl_aff_sub
);
3507 /* Subtract "ma2" from "ma1" and return the result.
3509 __isl_give isl_multi_aff
*isl_multi_aff_sub(__isl_take isl_multi_aff
*ma1
,
3510 __isl_take isl_multi_aff
*ma2
)
3512 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3513 &isl_multi_aff_sub_aligned
);
3516 /* Exploit the equalities in "eq" to simplify the affine expressions.
3518 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3519 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3523 maff
= isl_multi_aff_cow(maff
);
3527 for (i
= 0; i
< maff
->n
; ++i
) {
3528 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3529 isl_basic_set_copy(eq
));
3534 isl_basic_set_free(eq
);
3537 isl_basic_set_free(eq
);
3538 isl_multi_aff_free(maff
);
3542 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3547 maff
= isl_multi_aff_cow(maff
);
3551 for (i
= 0; i
< maff
->n
; ++i
) {
3552 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3554 return isl_multi_aff_free(maff
);
3560 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3561 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3563 maff1
= isl_multi_aff_add(maff1
, maff2
);
3564 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3568 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3576 /* Return the set of domain elements where "ma1" is lexicographically
3577 * smaller than or equal to "ma2".
3579 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3580 __isl_take isl_multi_aff
*ma2
)
3582 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3585 /* Return the set of domain elements where "ma1" is lexicographically
3586 * greater than or equal to "ma2".
3588 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3589 __isl_take isl_multi_aff
*ma2
)
3592 isl_map
*map1
, *map2
;
3595 map1
= isl_map_from_multi_aff(ma1
);
3596 map2
= isl_map_from_multi_aff(ma2
);
3597 map
= isl_map_range_product(map1
, map2
);
3598 space
= isl_space_range(isl_map_get_space(map
));
3599 space
= isl_space_domain(isl_space_unwrap(space
));
3600 ge
= isl_map_lex_ge(space
);
3601 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3603 return isl_map_domain(map
);
3607 #define PW isl_pw_multi_aff
3609 #define EL isl_multi_aff
3611 #define EL_IS_ZERO is_empty
3615 #define IS_ZERO is_empty
3618 #undef DEFAULT_IS_ZERO
3619 #define DEFAULT_IS_ZERO 0
3624 #define NO_INVOLVES_DIMS
3625 #define NO_INSERT_DIMS
3629 #include <isl_pw_templ.c>
3632 #define UNION isl_union_pw_multi_aff
3634 #define PART isl_pw_multi_aff
3636 #define PARTS pw_multi_aff
3637 #define ALIGN_DOMAIN
3641 #include <isl_union_templ.c>
3643 /* Given a function "cmp" that returns the set of elements where
3644 * "ma1" is "better" than "ma2", return the intersection of this
3645 * set with "dom1" and "dom2".
3647 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
3648 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
3649 __isl_keep isl_multi_aff
*ma2
,
3650 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3651 __isl_take isl_multi_aff
*ma2
))
3657 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
3658 is_empty
= isl_set_plain_is_empty(common
);
3659 if (is_empty
>= 0 && is_empty
)
3662 return isl_set_free(common
);
3663 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
3664 better
= isl_set_intersect(common
, better
);
3669 /* Given a function "cmp" that returns the set of elements where
3670 * "ma1" is "better" than "ma2", return a piecewise multi affine
3671 * expression defined on the union of the definition domains
3672 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3673 * "pma2" on each cell. If only one of the two input functions
3674 * is defined on a given cell, then it is considered the best.
3676 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
3677 __isl_take isl_pw_multi_aff
*pma1
,
3678 __isl_take isl_pw_multi_aff
*pma2
,
3679 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3680 __isl_take isl_multi_aff
*ma2
))
3683 isl_pw_multi_aff
*res
= NULL
;
3685 isl_set
*set
= NULL
;
3690 ctx
= isl_space_get_ctx(pma1
->dim
);
3691 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
3692 isl_die(ctx
, isl_error_invalid
,
3693 "arguments should live in the same space", goto error
);
3695 if (isl_pw_multi_aff_is_empty(pma1
)) {
3696 isl_pw_multi_aff_free(pma1
);
3700 if (isl_pw_multi_aff_is_empty(pma2
)) {
3701 isl_pw_multi_aff_free(pma2
);
3705 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
3706 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
3708 for (i
= 0; i
< pma1
->n
; ++i
) {
3709 set
= isl_set_copy(pma1
->p
[i
].set
);
3710 for (j
= 0; j
< pma2
->n
; ++j
) {
3714 better
= shared_and_better(pma2
->p
[j
].set
,
3715 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
3716 pma1
->p
[i
].maff
, cmp
);
3717 is_empty
= isl_set_plain_is_empty(better
);
3718 if (is_empty
< 0 || is_empty
) {
3719 isl_set_free(better
);
3724 set
= isl_set_subtract(set
, isl_set_copy(better
));
3726 res
= isl_pw_multi_aff_add_piece(res
, better
,
3727 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3729 res
= isl_pw_multi_aff_add_piece(res
, set
,
3730 isl_multi_aff_copy(pma1
->p
[i
].maff
));
3733 for (j
= 0; j
< pma2
->n
; ++j
) {
3734 set
= isl_set_copy(pma2
->p
[j
].set
);
3735 for (i
= 0; i
< pma1
->n
; ++i
)
3736 set
= isl_set_subtract(set
,
3737 isl_set_copy(pma1
->p
[i
].set
));
3738 res
= isl_pw_multi_aff_add_piece(res
, set
,
3739 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3742 isl_pw_multi_aff_free(pma1
);
3743 isl_pw_multi_aff_free(pma2
);
3747 isl_pw_multi_aff_free(pma1
);
3748 isl_pw_multi_aff_free(pma2
);
3750 return isl_pw_multi_aff_free(res
);
3753 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
3754 __isl_take isl_pw_multi_aff
*pma1
,
3755 __isl_take isl_pw_multi_aff
*pma2
)
3757 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
3760 /* Given two piecewise multi affine expressions, return a piecewise
3761 * multi-affine expression defined on the union of the definition domains
3762 * of the inputs that is equal to the lexicographic maximum of the two
3763 * inputs on each cell. If only one of the two inputs is defined on
3764 * a given cell, then it is considered to be the maximum.
3766 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
3767 __isl_take isl_pw_multi_aff
*pma1
,
3768 __isl_take isl_pw_multi_aff
*pma2
)
3770 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3771 &pw_multi_aff_union_lexmax
);
3774 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
3775 __isl_take isl_pw_multi_aff
*pma1
,
3776 __isl_take isl_pw_multi_aff
*pma2
)
3778 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
3781 /* Given two piecewise multi affine expressions, return a piecewise
3782 * multi-affine expression defined on the union of the definition domains
3783 * of the inputs that is equal to the lexicographic minimum of the two
3784 * inputs on each cell. If only one of the two inputs is defined on
3785 * a given cell, then it is considered to be the minimum.
3787 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
3788 __isl_take isl_pw_multi_aff
*pma1
,
3789 __isl_take isl_pw_multi_aff
*pma2
)
3791 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3792 &pw_multi_aff_union_lexmin
);
3795 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
3796 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3798 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3799 &isl_multi_aff_add
);
3802 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
3803 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3805 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3809 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
3810 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3812 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3813 &isl_multi_aff_sub
);
3816 /* Subtract "pma2" from "pma1" and return the result.
3818 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
3819 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3821 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3825 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
3826 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3828 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
3831 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3832 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3834 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
3835 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3839 isl_pw_multi_aff
*res
;
3844 n
= pma1
->n
* pma2
->n
;
3845 space
= isl_space_product(isl_space_copy(pma1
->dim
),
3846 isl_space_copy(pma2
->dim
));
3847 res
= isl_pw_multi_aff_alloc_size(space
, n
);
3849 for (i
= 0; i
< pma1
->n
; ++i
) {
3850 for (j
= 0; j
< pma2
->n
; ++j
) {
3854 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
3855 isl_set_copy(pma2
->p
[j
].set
));
3856 ma
= isl_multi_aff_product(
3857 isl_multi_aff_copy(pma1
->p
[i
].maff
),
3858 isl_multi_aff_copy(pma2
->p
[j
].maff
));
3859 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
3863 isl_pw_multi_aff_free(pma1
);
3864 isl_pw_multi_aff_free(pma2
);
3867 isl_pw_multi_aff_free(pma1
);
3868 isl_pw_multi_aff_free(pma2
);
3872 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
3873 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3875 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3876 &pw_multi_aff_product
);
3879 /* Construct a map mapping the domain of the piecewise multi-affine expression
3880 * to its range, with each dimension in the range equated to the
3881 * corresponding affine expression on its cell.
3883 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3891 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
3893 for (i
= 0; i
< pma
->n
; ++i
) {
3894 isl_multi_aff
*maff
;
3895 isl_basic_map
*bmap
;
3898 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
3899 bmap
= isl_basic_map_from_multi_aff(maff
);
3900 map_i
= isl_map_from_basic_map(bmap
);
3901 map_i
= isl_map_intersect_domain(map_i
,
3902 isl_set_copy(pma
->p
[i
].set
));
3903 map
= isl_map_union_disjoint(map
, map_i
);
3906 isl_pw_multi_aff_free(pma
);
3910 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3915 if (!isl_space_is_set(pma
->dim
))
3916 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3917 "isl_pw_multi_aff cannot be converted into an isl_set",
3920 return isl_map_from_pw_multi_aff(pma
);
3922 isl_pw_multi_aff_free(pma
);
3926 /* Given a basic map with a single output dimension that is defined
3927 * in terms of the parameters and input dimensions using an equality,
3928 * extract an isl_aff that expresses the output dimension in terms
3929 * of the parameters and input dimensions.
3931 * Since some applications expect the result of isl_pw_multi_aff_from_map
3932 * to only contain integer affine expressions, we compute the floor
3933 * of the expression before returning.
3935 * This function shares some similarities with
3936 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3938 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
3939 __isl_take isl_basic_map
*bmap
)
3944 isl_local_space
*ls
;
3949 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
3950 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3951 "basic map should have a single output dimension",
3953 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
3954 total
= isl_basic_map_total_dim(bmap
);
3955 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3956 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
3958 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
3959 1 + total
- (offset
+ 1)) != -1)
3963 if (i
>= bmap
->n_eq
)
3964 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3965 "unable to find suitable equality", goto error
);
3966 ls
= isl_basic_map_get_local_space(bmap
);
3967 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
3970 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
3971 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3973 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3974 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
3975 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
3976 isl_basic_map_free(bmap
);
3978 aff
= isl_aff_remove_unused_divs(aff
);
3979 aff
= isl_aff_floor(aff
);
3982 isl_basic_map_free(bmap
);
3986 /* Given a basic map where each output dimension is defined
3987 * in terms of the parameters and input dimensions using an equality,
3988 * extract an isl_multi_aff that expresses the output dimensions in terms
3989 * of the parameters and input dimensions.
3991 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
3992 __isl_take isl_basic_map
*bmap
)
4001 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4002 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4004 for (i
= 0; i
< n_out
; ++i
) {
4005 isl_basic_map
*bmap_i
;
4008 bmap_i
= isl_basic_map_copy(bmap
);
4009 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4010 i
+ 1, n_out
- (1 + i
));
4011 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4012 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4013 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4016 isl_basic_map_free(bmap
);
4021 /* Create an isl_pw_multi_aff that is equivalent to
4022 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4023 * The given basic map is such that each output dimension is defined
4024 * in terms of the parameters and input dimensions using an equality.
4026 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4027 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4031 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4032 return isl_pw_multi_aff_alloc(domain
, ma
);
4035 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4036 * This obviously only works if the input "map" is single-valued.
4037 * If so, we compute the lexicographic minimum of the image in the form
4038 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4039 * to its lexicographic minimum.
4040 * If the input is not single-valued, we produce an error.
4042 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4043 __isl_take isl_map
*map
)
4047 isl_pw_multi_aff
*pma
;
4049 sv
= isl_map_is_single_valued(map
);
4053 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4054 "map is not single-valued", goto error
);
4055 map
= isl_map_make_disjoint(map
);
4059 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4061 for (i
= 0; i
< map
->n
; ++i
) {
4062 isl_pw_multi_aff
*pma_i
;
4063 isl_basic_map
*bmap
;
4064 bmap
= isl_basic_map_copy(map
->p
[i
]);
4065 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4066 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4076 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4077 * taking into account that the output dimension at position "d"
4078 * can be represented as
4080 * x = floor((e(...) + c1) / m)
4082 * given that constraint "i" is of the form
4084 * e(...) + c1 - m x >= 0
4087 * Let "map" be of the form
4091 * We construct a mapping
4093 * A -> [A -> x = floor(...)]
4095 * apply that to the map, obtaining
4097 * [A -> x = floor(...)] -> B
4099 * and equate dimension "d" to x.
4100 * We then compute a isl_pw_multi_aff representation of the resulting map
4101 * and plug in the mapping above.
4103 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4104 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4108 isl_local_space
*ls
;
4116 isl_pw_multi_aff
*pma
;
4119 is_set
= isl_map_is_set(map
);
4121 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4122 ctx
= isl_map_get_ctx(map
);
4123 space
= isl_space_domain(isl_map_get_space(map
));
4124 n_in
= isl_space_dim(space
, isl_dim_set
);
4125 n
= isl_space_dim(space
, isl_dim_all
);
4127 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4129 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4130 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4132 isl_basic_map_free(hull
);
4134 ls
= isl_local_space_from_space(isl_space_copy(space
));
4135 aff
= isl_aff_alloc_vec(ls
, v
);
4136 aff
= isl_aff_floor(aff
);
4138 isl_space_free(space
);
4139 ma
= isl_multi_aff_from_aff(aff
);
4141 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4142 ma
= isl_multi_aff_range_product(ma
,
4143 isl_multi_aff_from_aff(aff
));
4146 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4147 map
= isl_map_apply_domain(map
, insert
);
4148 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4149 pma
= isl_pw_multi_aff_from_map(map
);
4150 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4155 /* Is constraint "c" of the form
4157 * e(...) + c1 - m x >= 0
4161 * -e(...) + c2 + m x >= 0
4163 * where m > 1 and e only depends on parameters and input dimemnsions?
4165 * "offset" is the offset of the output dimensions
4166 * "pos" is the position of output dimension x.
4168 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4170 if (isl_int_is_zero(c
[offset
+ d
]))
4172 if (isl_int_is_one(c
[offset
+ d
]))
4174 if (isl_int_is_negone(c
[offset
+ d
]))
4176 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4178 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4179 total
- (offset
+ d
+ 1)) != -1)
4184 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4186 * As a special case, we first check if there is any pair of constraints,
4187 * shared by all the basic maps in "map" that force a given dimension
4188 * to be equal to the floor of some affine combination of the input dimensions.
4190 * In particular, if we can find two constraints
4192 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4196 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4198 * where m > 1 and e only depends on parameters and input dimemnsions,
4201 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4203 * then we know that we can take
4205 * x = floor((e(...) + c1) / m)
4207 * without having to perform any computation.
4209 * Note that we know that
4213 * If c1 + c2 were 0, then we would have detected an equality during
4214 * simplification. If c1 + c2 were negative, then we would have detected
4217 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4218 __isl_take isl_map
*map
)
4224 isl_basic_map
*hull
;
4226 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4231 dim
= isl_map_dim(map
, isl_dim_out
);
4232 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4233 total
= 1 + isl_basic_map_total_dim(hull
);
4235 for (d
= 0; d
< dim
; ++d
) {
4236 for (i
= 0; i
< n
; ++i
) {
4237 if (!is_potential_div_constraint(hull
->ineq
[i
],
4240 for (j
= i
+ 1; j
< n
; ++j
) {
4241 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4242 hull
->ineq
[j
] + 1, total
- 1))
4244 isl_int_add(sum
, hull
->ineq
[i
][0],
4246 if (isl_int_abs_lt(sum
,
4247 hull
->ineq
[i
][offset
+ d
]))
4254 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4256 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4260 isl_basic_map_free(hull
);
4261 return pw_multi_aff_from_map_base(map
);
4264 isl_basic_map_free(hull
);
4268 /* Given an affine expression
4270 * [A -> B] -> f(A,B)
4272 * construct an isl_multi_aff
4276 * such that dimension "d" in B' is set to "aff" and the remaining
4277 * dimensions are set equal to the corresponding dimensions in B.
4278 * "n_in" is the dimension of the space A.
4279 * "n_out" is the dimension of the space B.
4281 * If "is_set" is set, then the affine expression is of the form
4285 * and we construct an isl_multi_aff
4289 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4290 unsigned n_in
, unsigned n_out
, int is_set
)
4294 isl_space
*space
, *space2
;
4295 isl_local_space
*ls
;
4297 space
= isl_aff_get_domain_space(aff
);
4298 ls
= isl_local_space_from_space(isl_space_copy(space
));
4299 space2
= isl_space_copy(space
);
4301 space2
= isl_space_range(isl_space_unwrap(space2
));
4302 space
= isl_space_map_from_domain_and_range(space
, space2
);
4303 ma
= isl_multi_aff_alloc(space
);
4304 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4306 for (i
= 0; i
< n_out
; ++i
) {
4309 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4310 isl_dim_set
, n_in
+ i
);
4311 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4314 isl_local_space_free(ls
);
4319 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4320 * taking into account that the dimension at position "d" can be written as
4322 * x = m a + f(..) (1)
4324 * where m is equal to "gcd".
4325 * "i" is the index of the equality in "hull" that defines f(..).
4326 * In particular, the equality is of the form
4328 * f(..) - x + m g(existentials) = 0
4332 * -f(..) + x + m g(existentials) = 0
4334 * We basically plug (1) into "map", resulting in a map with "a"
4335 * in the range instead of "x". The corresponding isl_pw_multi_aff
4336 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4338 * Specifically, given the input map
4342 * We first wrap it into a set
4346 * and define (1) on top of the corresponding space, resulting in "aff".
4347 * We use this to create an isl_multi_aff that maps the output position "d"
4348 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4349 * We plug this into the wrapped map, unwrap the result and compute the
4350 * corresponding isl_pw_multi_aff.
4351 * The result is an expression
4359 * so that we can plug that into "aff", after extending the latter to
4365 * If "map" is actually a set, then there is no "A" space, meaning
4366 * that we do not need to perform any wrapping, and that the result
4367 * of the recursive call is of the form
4371 * which is plugged into a mapping of the form
4375 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4376 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4381 isl_local_space
*ls
;
4384 isl_pw_multi_aff
*pma
, *id
;
4390 is_set
= isl_map_is_set(map
);
4392 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4393 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4394 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4399 set
= isl_map_wrap(map
);
4400 space
= isl_space_map_from_set(isl_set_get_space(set
));
4401 ma
= isl_multi_aff_identity(space
);
4402 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4403 aff
= isl_aff_alloc(ls
);
4405 isl_int_set_si(aff
->v
->el
[0], 1);
4406 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4407 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4410 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4412 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4414 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4415 set
= isl_set_preimage_multi_aff(set
, ma
);
4417 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4422 map
= isl_set_unwrap(set
);
4423 pma
= isl_pw_multi_aff_from_map(set
);
4426 space
= isl_pw_multi_aff_get_domain_space(pma
);
4427 space
= isl_space_map_from_set(space
);
4428 id
= isl_pw_multi_aff_identity(space
);
4429 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4431 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4432 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4434 isl_basic_map_free(hull
);
4438 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4440 * As a special case, we first check if all output dimensions are uniquely
4441 * defined in terms of the parameters and input dimensions over the entire
4442 * domain. If so, we extract the desired isl_pw_multi_aff directly
4443 * from the affine hull of "map" and its domain.
4445 * Otherwise, we check if any of the output dimensions is "strided".
4446 * That is, we check if can be written as
4450 * with m greater than 1, a some combination of existentiall quantified
4451 * variables and f and expression in the parameters and input dimensions.
4452 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4454 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4457 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4461 isl_basic_map
*hull
;
4471 hull
= isl_map_affine_hull(isl_map_copy(map
));
4472 sv
= isl_basic_map_plain_is_single_valued(hull
);
4474 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4476 hull
= isl_basic_map_free(hull
);
4480 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4481 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4484 isl_basic_map_free(hull
);
4485 return pw_multi_aff_from_map_check_div(map
);
4490 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4491 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4493 for (i
= 0; i
< n_out
; ++i
) {
4494 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4495 isl_int
*eq
= hull
->eq
[j
];
4496 isl_pw_multi_aff
*res
;
4498 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4499 !isl_int_is_negone(eq
[o_out
+ i
]))
4501 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4503 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4504 n_out
- (i
+ 1)) != -1)
4506 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4507 if (isl_int_is_zero(gcd
))
4509 if (isl_int_is_one(gcd
))
4512 res
= pw_multi_aff_from_map_stride(map
, hull
,
4520 isl_basic_map_free(hull
);
4521 return pw_multi_aff_from_map_check_div(map
);
4527 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4529 return isl_pw_multi_aff_from_map(set
);
4532 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4535 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4537 isl_union_pw_multi_aff
**upma
= user
;
4538 isl_pw_multi_aff
*pma
;
4540 pma
= isl_pw_multi_aff_from_map(map
);
4541 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4543 return *upma
? 0 : -1;
4546 /* Try and create an isl_union_pw_multi_aff that is equivalent
4547 * to the given isl_union_map.
4548 * The isl_union_map is required to be single-valued in each space.
4549 * Otherwise, an error is produced.
4551 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4552 __isl_take isl_union_map
*umap
)
4555 isl_union_pw_multi_aff
*upma
;
4557 space
= isl_union_map_get_space(umap
);
4558 upma
= isl_union_pw_multi_aff_empty(space
);
4559 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4560 upma
= isl_union_pw_multi_aff_free(upma
);
4561 isl_union_map_free(umap
);
4566 /* Try and create an isl_union_pw_multi_aff that is equivalent
4567 * to the given isl_union_set.
4568 * The isl_union_set is required to be a singleton in each space.
4569 * Otherwise, an error is produced.
4571 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4572 __isl_take isl_union_set
*uset
)
4574 return isl_union_pw_multi_aff_from_union_map(uset
);
4577 /* Return the piecewise affine expression "set ? 1 : 0".
4579 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4582 isl_space
*space
= isl_set_get_space(set
);
4583 isl_local_space
*ls
= isl_local_space_from_space(space
);
4584 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4585 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4587 one
= isl_aff_add_constant_si(one
, 1);
4588 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4589 set
= isl_set_complement(set
);
4590 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4595 /* Plug in "subs" for dimension "type", "pos" of "aff".
4597 * Let i be the dimension to replace and let "subs" be of the form
4601 * and "aff" of the form
4607 * (a f + d g')/(m d)
4609 * where g' is the result of plugging in "subs" in each of the integer
4612 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
4613 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
4618 aff
= isl_aff_cow(aff
);
4620 return isl_aff_free(aff
);
4622 ctx
= isl_aff_get_ctx(aff
);
4623 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
4624 isl_die(ctx
, isl_error_invalid
,
4625 "spaces don't match", return isl_aff_free(aff
));
4626 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
4627 isl_die(ctx
, isl_error_unsupported
,
4628 "cannot handle divs yet", return isl_aff_free(aff
));
4630 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
4632 return isl_aff_free(aff
);
4634 aff
->v
= isl_vec_cow(aff
->v
);
4636 return isl_aff_free(aff
);
4638 pos
+= isl_local_space_offset(aff
->ls
, type
);
4641 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
4642 aff
->v
->size
, subs
->v
->size
, v
);
4648 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4649 * expressions in "maff".
4651 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
4652 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
4653 __isl_keep isl_aff
*subs
)
4657 maff
= isl_multi_aff_cow(maff
);
4659 return isl_multi_aff_free(maff
);
4661 if (type
== isl_dim_in
)
4664 for (i
= 0; i
< maff
->n
; ++i
) {
4665 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
4667 return isl_multi_aff_free(maff
);
4673 /* Plug in "subs" for dimension "type", "pos" of "pma".
4675 * pma is of the form
4679 * while subs is of the form
4681 * v' = B_j(v) -> S_j
4683 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4684 * has a contribution in the result, in particular
4686 * C_ij(S_j) -> M_i(S_j)
4688 * Note that plugging in S_j in C_ij may also result in an empty set
4689 * and this contribution should simply be discarded.
4691 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
4692 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
4693 __isl_keep isl_pw_aff
*subs
)
4696 isl_pw_multi_aff
*res
;
4699 return isl_pw_multi_aff_free(pma
);
4701 n
= pma
->n
* subs
->n
;
4702 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
4704 for (i
= 0; i
< pma
->n
; ++i
) {
4705 for (j
= 0; j
< subs
->n
; ++j
) {
4707 isl_multi_aff
*res_ij
;
4710 common
= isl_set_intersect(
4711 isl_set_copy(pma
->p
[i
].set
),
4712 isl_set_copy(subs
->p
[j
].set
));
4713 common
= isl_set_substitute(common
,
4714 type
, pos
, subs
->p
[j
].aff
);
4715 empty
= isl_set_plain_is_empty(common
);
4716 if (empty
< 0 || empty
) {
4717 isl_set_free(common
);
4723 res_ij
= isl_multi_aff_substitute(
4724 isl_multi_aff_copy(pma
->p
[i
].maff
),
4725 type
, pos
, subs
->p
[j
].aff
);
4727 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
4731 isl_pw_multi_aff_free(pma
);
4734 isl_pw_multi_aff_free(pma
);
4735 isl_pw_multi_aff_free(res
);
4739 /* Compute the preimage of a range of dimensions in the affine expression "src"
4740 * under "ma" and put the result in "dst". The number of dimensions in "src"
4741 * that precede the range is given by "n_before". The number of dimensions
4742 * in the range is given by the number of output dimensions of "ma".
4743 * The number of dimensions that follow the range is given by "n_after".
4744 * If "has_denom" is set (to one),
4745 * then "src" and "dst" have an extra initial denominator.
4746 * "n_div_ma" is the number of existentials in "ma"
4747 * "n_div_bset" is the number of existentials in "src"
4748 * The resulting "dst" (which is assumed to have been allocated by
4749 * the caller) contains coefficients for both sets of existentials,
4750 * first those in "ma" and then those in "src".
4751 * f, c1, c2 and g are temporary objects that have been initialized
4754 * Let src represent the expression
4756 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4758 * and let ma represent the expressions
4760 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4762 * We start out with the following expression for dst:
4764 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4766 * with the multiplication factor f initially equal to 1
4767 * and f \sum_i b_i v_i kept separately.
4768 * For each x_i that we substitute, we multiply the numerator
4769 * (and denominator) of dst by c_1 = m_i and add the numerator
4770 * of the x_i expression multiplied by c_2 = f b_i,
4771 * after removing the common factors of c_1 and c_2.
4772 * The multiplication factor f also needs to be multiplied by c_1
4773 * for the next x_j, j > i.
4775 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
4776 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
4777 int n_div_ma
, int n_div_bmap
,
4778 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
4781 int n_param
, n_in
, n_out
;
4784 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
4785 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
4786 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
4788 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
4789 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
4790 isl_seq_clr(dst
+ o_dst
, n_in
);
4793 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
4796 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
4798 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
4800 isl_int_set_si(f
, 1);
4802 for (i
= 0; i
< n_out
; ++i
) {
4803 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
4805 if (isl_int_is_zero(src
[offset
]))
4807 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
4808 isl_int_mul(c2
, f
, src
[offset
]);
4809 isl_int_gcd(g
, c1
, c2
);
4810 isl_int_divexact(c1
, c1
, g
);
4811 isl_int_divexact(c2
, c2
, g
);
4813 isl_int_mul(f
, f
, c1
);
4816 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4817 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
4818 o_dst
+= 1 + n_param
;
4819 o_src
+= 1 + n_param
;
4820 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
4822 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4823 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
4826 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
4828 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
4829 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
4832 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
4834 isl_int_mul(dst
[0], dst
[0], c1
);
4838 /* Compute the pullback of "aff" by the function represented by "ma".
4839 * In other words, plug in "ma" in "aff". The result is an affine expression
4840 * defined over the domain space of "ma".
4842 * If "aff" is represented by
4844 * (a(p) + b x + c(divs))/d
4846 * and ma is represented by
4848 * x = D(p) + F(y) + G(divs')
4850 * then the result is
4852 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4854 * The divs in the local space of the input are similarly adjusted
4855 * through a call to isl_local_space_preimage_multi_aff.
4857 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
4858 __isl_take isl_multi_aff
*ma
)
4860 isl_aff
*res
= NULL
;
4861 isl_local_space
*ls
;
4862 int n_div_aff
, n_div_ma
;
4863 isl_int f
, c1
, c2
, g
;
4865 ma
= isl_multi_aff_align_divs(ma
);
4869 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
4870 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
4872 ls
= isl_aff_get_domain_local_space(aff
);
4873 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
4874 res
= isl_aff_alloc(ls
);
4883 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
4892 isl_multi_aff_free(ma
);
4893 res
= isl_aff_normalize(res
);
4897 isl_multi_aff_free(ma
);
4902 /* Compute the pullback of "aff1" by the function represented by "aff2".
4903 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4904 * defined over the domain space of "aff1".
4906 * The domain of "aff1" should match the range of "aff2", which means
4907 * that it should be single-dimensional.
4909 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
4910 __isl_take isl_aff
*aff2
)
4914 ma
= isl_multi_aff_from_aff(aff2
);
4915 return isl_aff_pullback_multi_aff(aff1
, ma
);
4918 /* Compute the pullback of "ma1" by the function represented by "ma2".
4919 * In other words, plug in "ma2" in "ma1".
4921 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4923 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
4924 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
4927 isl_space
*space
= NULL
;
4929 ma2
= isl_multi_aff_align_divs(ma2
);
4930 ma1
= isl_multi_aff_cow(ma1
);
4934 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
4935 isl_multi_aff_get_space(ma1
));
4937 for (i
= 0; i
< ma1
->n
; ++i
) {
4938 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
4939 isl_multi_aff_copy(ma2
));
4944 ma1
= isl_multi_aff_reset_space(ma1
, space
);
4945 isl_multi_aff_free(ma2
);
4948 isl_space_free(space
);
4949 isl_multi_aff_free(ma2
);
4950 isl_multi_aff_free(ma1
);
4954 /* Compute the pullback of "ma1" by the function represented by "ma2".
4955 * In other words, plug in "ma2" in "ma1".
4957 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
4958 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
4960 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
4961 &isl_multi_aff_pullback_multi_aff_aligned
);
4964 /* Extend the local space of "dst" to include the divs
4965 * in the local space of "src".
4967 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
4968 __isl_keep isl_aff
*src
)
4976 return isl_aff_free(dst
);
4978 ctx
= isl_aff_get_ctx(src
);
4979 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
4980 isl_die(ctx
, isl_error_invalid
,
4981 "spaces don't match", goto error
);
4983 if (src
->ls
->div
->n_row
== 0)
4986 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
4987 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
4988 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
4991 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
4992 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5000 return isl_aff_free(dst
);
5003 /* Adjust the local spaces of the affine expressions in "maff"
5004 * such that they all have the save divs.
5006 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5007 __isl_take isl_multi_aff
*maff
)
5015 maff
= isl_multi_aff_cow(maff
);
5019 for (i
= 1; i
< maff
->n
; ++i
)
5020 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5021 for (i
= 1; i
< maff
->n
; ++i
) {
5022 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5024 return isl_multi_aff_free(maff
);
5030 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5032 aff
= isl_aff_cow(aff
);
5036 aff
->ls
= isl_local_space_lift(aff
->ls
);
5038 return isl_aff_free(aff
);
5043 /* Lift "maff" to a space with extra dimensions such that the result
5044 * has no more existentially quantified variables.
5045 * If "ls" is not NULL, then *ls is assigned the local space that lies
5046 * at the basis of the lifting applied to "maff".
5048 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5049 __isl_give isl_local_space
**ls
)
5063 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5064 *ls
= isl_local_space_from_space(space
);
5066 return isl_multi_aff_free(maff
);
5071 maff
= isl_multi_aff_cow(maff
);
5072 maff
= isl_multi_aff_align_divs(maff
);
5076 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5077 space
= isl_multi_aff_get_space(maff
);
5078 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5079 space
= isl_space_extend_domain_with_range(space
,
5080 isl_multi_aff_get_space(maff
));
5082 return isl_multi_aff_free(maff
);
5083 isl_space_free(maff
->space
);
5084 maff
->space
= space
;
5087 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5089 return isl_multi_aff_free(maff
);
5092 for (i
= 0; i
< maff
->n
; ++i
) {
5093 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5101 isl_local_space_free(*ls
);
5102 return isl_multi_aff_free(maff
);
5106 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5108 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5109 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5119 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5120 if (pos
< 0 || pos
>= n_out
)
5121 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5122 "index out of bounds", return NULL
);
5124 space
= isl_pw_multi_aff_get_space(pma
);
5125 space
= isl_space_drop_dims(space
, isl_dim_out
,
5126 pos
+ 1, n_out
- pos
- 1);
5127 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5129 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5130 for (i
= 0; i
< pma
->n
; ++i
) {
5132 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5133 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5139 /* Return an isl_pw_multi_aff with the given "set" as domain and
5140 * an unnamed zero-dimensional range.
5142 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5143 __isl_take isl_set
*set
)
5148 space
= isl_set_get_space(set
);
5149 space
= isl_space_from_domain(space
);
5150 ma
= isl_multi_aff_zero(space
);
5151 return isl_pw_multi_aff_alloc(set
, ma
);
5154 /* Add an isl_pw_multi_aff with the given "set" as domain and
5155 * an unnamed zero-dimensional range to *user.
5157 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5159 isl_union_pw_multi_aff
**upma
= user
;
5160 isl_pw_multi_aff
*pma
;
5162 pma
= isl_pw_multi_aff_from_domain(set
);
5163 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5168 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5169 * an unnamed zero-dimensional range.
5171 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5172 __isl_take isl_union_set
*uset
)
5175 isl_union_pw_multi_aff
*upma
;
5180 space
= isl_union_set_get_space(uset
);
5181 upma
= isl_union_pw_multi_aff_empty(space
);
5183 if (isl_union_set_foreach_set(uset
,
5184 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5187 isl_union_set_free(uset
);
5190 isl_union_set_free(uset
);
5191 isl_union_pw_multi_aff_free(upma
);
5195 /* Convert "pma" to an isl_map and add it to *umap.
5197 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5199 isl_union_map
**umap
= user
;
5202 map
= isl_map_from_pw_multi_aff(pma
);
5203 *umap
= isl_union_map_add_map(*umap
, map
);
5208 /* Construct a union map mapping the domain of the union
5209 * piecewise multi-affine expression to its range, with each dimension
5210 * in the range equated to the corresponding affine expression on its cell.
5212 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5213 __isl_take isl_union_pw_multi_aff
*upma
)
5216 isl_union_map
*umap
;
5221 space
= isl_union_pw_multi_aff_get_space(upma
);
5222 umap
= isl_union_map_empty(space
);
5224 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5225 &map_from_pw_multi_aff
, &umap
) < 0)
5228 isl_union_pw_multi_aff_free(upma
);
5231 isl_union_pw_multi_aff_free(upma
);
5232 isl_union_map_free(umap
);
5236 /* Local data for bin_entry and the callback "fn".
5238 struct isl_union_pw_multi_aff_bin_data
{
5239 isl_union_pw_multi_aff
*upma2
;
5240 isl_union_pw_multi_aff
*res
;
5241 isl_pw_multi_aff
*pma
;
5242 int (*fn
)(void **entry
, void *user
);
5245 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5246 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5248 static int bin_entry(void **entry
, void *user
)
5250 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5251 isl_pw_multi_aff
*pma
= *entry
;
5254 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
5255 data
->fn
, data
) < 0)
5261 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5262 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5263 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5264 * as *entry. The callback should adjust data->res if desired.
5266 static __isl_give isl_union_pw_multi_aff
*bin_op(
5267 __isl_take isl_union_pw_multi_aff
*upma1
,
5268 __isl_take isl_union_pw_multi_aff
*upma2
,
5269 int (*fn
)(void **entry
, void *user
))
5272 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5274 space
= isl_union_pw_multi_aff_get_space(upma2
);
5275 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5276 space
= isl_union_pw_multi_aff_get_space(upma1
);
5277 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5279 if (!upma1
|| !upma2
)
5283 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
5285 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
5286 &bin_entry
, &data
) < 0)
5289 isl_union_pw_multi_aff_free(upma1
);
5290 isl_union_pw_multi_aff_free(upma2
);
5293 isl_union_pw_multi_aff_free(upma1
);
5294 isl_union_pw_multi_aff_free(upma2
);
5295 isl_union_pw_multi_aff_free(data
.res
);
5299 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5300 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5302 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5303 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5307 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5308 isl_pw_multi_aff_get_space(pma2
));
5309 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5310 &isl_multi_aff_range_product
);
5313 /* Given two isl_pw_multi_affs A -> B and C -> D,
5314 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5316 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5317 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5320 &pw_multi_aff_range_product
);
5323 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5324 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5326 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5327 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5331 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5332 isl_pw_multi_aff_get_space(pma2
));
5333 space
= isl_space_flatten_range(space
);
5334 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5335 &isl_multi_aff_flat_range_product
);
5338 /* Given two isl_pw_multi_affs A -> B and C -> D,
5339 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5341 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5342 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5344 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5345 &pw_multi_aff_flat_range_product
);
5348 /* If data->pma and *entry have the same domain space, then compute
5349 * their flat range product and the result to data->res.
5351 static int flat_range_product_entry(void **entry
, void *user
)
5353 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5354 isl_pw_multi_aff
*pma2
= *entry
;
5356 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
5357 pma2
->dim
, isl_dim_in
))
5360 pma2
= isl_pw_multi_aff_flat_range_product(
5361 isl_pw_multi_aff_copy(data
->pma
),
5362 isl_pw_multi_aff_copy(pma2
));
5364 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5369 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5370 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5372 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5373 __isl_take isl_union_pw_multi_aff
*upma1
,
5374 __isl_take isl_union_pw_multi_aff
*upma2
)
5376 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5379 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5380 * The parameters are assumed to have been aligned.
5382 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5383 * except that it works on two different isl_pw_* types.
5385 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5386 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5387 __isl_take isl_pw_aff
*pa
)
5390 isl_pw_multi_aff
*res
= NULL
;
5395 if (!isl_space_tuple_match(pma
->dim
, isl_dim_in
, pa
->dim
, isl_dim_in
))
5396 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5397 "domains don't match", goto error
);
5398 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5399 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5400 "index out of bounds", goto error
);
5403 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5405 for (i
= 0; i
< pma
->n
; ++i
) {
5406 for (j
= 0; j
< pa
->n
; ++j
) {
5408 isl_multi_aff
*res_ij
;
5411 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5412 isl_set_copy(pa
->p
[j
].set
));
5413 empty
= isl_set_plain_is_empty(common
);
5414 if (empty
< 0 || empty
) {
5415 isl_set_free(common
);
5421 res_ij
= isl_multi_aff_set_aff(
5422 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5423 isl_aff_copy(pa
->p
[j
].aff
));
5424 res_ij
= isl_multi_aff_gist(res_ij
,
5425 isl_set_copy(common
));
5427 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5431 isl_pw_multi_aff_free(pma
);
5432 isl_pw_aff_free(pa
);
5435 isl_pw_multi_aff_free(pma
);
5436 isl_pw_aff_free(pa
);
5437 return isl_pw_multi_aff_free(res
);
5440 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5442 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5443 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5444 __isl_take isl_pw_aff
*pa
)
5448 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5449 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5450 if (!isl_space_has_named_params(pma
->dim
) ||
5451 !isl_space_has_named_params(pa
->dim
))
5452 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5453 "unaligned unnamed parameters", goto error
);
5454 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5455 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5456 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5458 isl_pw_multi_aff_free(pma
);
5459 isl_pw_aff_free(pa
);
5463 /* Do the parameters of "pa" match those of "space"?
5465 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5466 __isl_keep isl_space
*space
)
5468 isl_space
*pa_space
;
5474 pa_space
= isl_pw_aff_get_space(pa
);
5476 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5478 isl_space_free(pa_space
);
5482 /* Check that the domain space of "pa" matches "space".
5484 * Return 0 on success and -1 on error.
5486 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5487 __isl_keep isl_space
*space
)
5489 isl_space
*pa_space
;
5495 pa_space
= isl_pw_aff_get_space(pa
);
5497 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5501 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5502 "parameters don't match", goto error
);
5503 match
= isl_space_tuple_match(space
, isl_dim_in
, pa_space
, isl_dim_in
);
5507 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5508 "domains don't match", goto error
);
5509 isl_space_free(pa_space
);
5512 isl_space_free(pa_space
);
5519 #include <isl_multi_templ.c>
5521 /* Scale the elements of "pma" by the corresponding elements of "mv".
5523 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5524 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5528 pma
= isl_pw_multi_aff_cow(pma
);
5531 if (!isl_space_tuple_match(pma
->dim
, isl_dim_out
,
5532 mv
->space
, isl_dim_set
))
5533 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5534 "spaces don't match", goto error
);
5535 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5536 mv
->space
, isl_dim_param
)) {
5537 pma
= isl_pw_multi_aff_align_params(pma
,
5538 isl_multi_val_get_space(mv
));
5539 mv
= isl_multi_val_align_params(mv
,
5540 isl_pw_multi_aff_get_space(pma
));
5545 for (i
= 0; i
< pma
->n
; ++i
) {
5546 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5547 isl_multi_val_copy(mv
));
5548 if (!pma
->p
[i
].maff
)
5552 isl_multi_val_free(mv
);
5555 isl_multi_val_free(mv
);
5556 isl_pw_multi_aff_free(pma
);
5560 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5561 * mv contains the mv argument.
5562 * res collects the results.
5564 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5566 isl_union_pw_multi_aff
*res
;
5569 /* This function is called for each entry of an isl_union_pw_multi_aff.
5570 * If the space of the entry matches that of data->mv,
5571 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5574 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5576 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5577 isl_pw_multi_aff
*pma
= *entry
;
5581 if (!isl_space_tuple_match(pma
->dim
, isl_dim_out
,
5582 data
->mv
->space
, isl_dim_set
))
5585 pma
= isl_pw_multi_aff_copy(pma
);
5586 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5587 isl_multi_val_copy(data
->mv
));
5588 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
5595 /* Scale the elements of "upma" by the corresponding elements of "mv",
5596 * for those entries that match the space of "mv".
5598 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
5599 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
5601 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
5603 upma
= isl_union_pw_multi_aff_align_params(upma
,
5604 isl_multi_val_get_space(mv
));
5605 mv
= isl_multi_val_align_params(mv
,
5606 isl_union_pw_multi_aff_get_space(upma
));
5611 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->dim
),
5613 if (isl_hash_table_foreach(upma
->dim
->ctx
, &upma
->table
,
5614 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
5617 isl_multi_val_free(mv
);
5618 isl_union_pw_multi_aff_free(upma
);
5621 isl_multi_val_free(mv
);
5622 isl_union_pw_multi_aff_free(upma
);
5626 /* Construct and return a piecewise multi affine expression
5627 * in the given space with value zero in each of the output dimensions and
5628 * a universe domain.
5630 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
5632 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
5635 /* Construct and return a piecewise multi affine expression
5636 * that is equal to the given piecewise affine expression.
5638 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
5639 __isl_take isl_pw_aff
*pa
)
5643 isl_pw_multi_aff
*pma
;
5648 space
= isl_pw_aff_get_space(pa
);
5649 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
5651 for (i
= 0; i
< pa
->n
; ++i
) {
5655 set
= isl_set_copy(pa
->p
[i
].set
);
5656 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
5657 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
5660 isl_pw_aff_free(pa
);
5664 /* Construct a set or map mapping the shared (parameter) domain
5665 * of the piecewise affine expressions to the range of "mpa"
5666 * with each dimension in the range equated to the
5667 * corresponding piecewise affine expression.
5669 static __isl_give isl_map
*map_from_multi_pw_aff(
5670 __isl_take isl_multi_pw_aff
*mpa
)
5679 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
5680 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5681 "invalid space", goto error
);
5683 space
= isl_multi_pw_aff_get_domain_space(mpa
);
5684 map
= isl_map_universe(isl_space_from_domain(space
));
5686 for (i
= 0; i
< mpa
->n
; ++i
) {
5690 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
5691 map_i
= map_from_pw_aff(pa
);
5693 map
= isl_map_flat_range_product(map
, map_i
);
5696 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
5698 isl_multi_pw_aff_free(mpa
);
5701 isl_multi_pw_aff_free(mpa
);
5705 /* Construct a map mapping the shared domain
5706 * of the piecewise affine expressions to the range of "mpa"
5707 * with each dimension in the range equated to the
5708 * corresponding piecewise affine expression.
5710 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
5714 if (isl_space_is_set(mpa
->space
))
5715 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5716 "space of input is not a map", goto error
);
5718 return map_from_multi_pw_aff(mpa
);
5720 isl_multi_pw_aff_free(mpa
);
5724 /* Construct a set mapping the shared parameter domain
5725 * of the piecewise affine expressions to the space of "mpa"
5726 * with each dimension in the range equated to the
5727 * corresponding piecewise affine expression.
5729 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
5733 if (!isl_space_is_set(mpa
->space
))
5734 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
5735 "space of input is not a set", goto error
);
5737 return map_from_multi_pw_aff(mpa
);
5739 isl_multi_pw_aff_free(mpa
);
5743 /* Construct and return a piecewise multi affine expression
5744 * that is equal to the given multi piecewise affine expression
5745 * on the shared domain of the piecewise affine expressions.
5747 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
5748 __isl_take isl_multi_pw_aff
*mpa
)
5753 isl_pw_multi_aff
*pma
;
5758 space
= isl_multi_pw_aff_get_space(mpa
);
5761 isl_multi_pw_aff_free(mpa
);
5762 return isl_pw_multi_aff_zero(space
);
5765 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
5766 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
5768 for (i
= 1; i
< mpa
->n
; ++i
) {
5769 isl_pw_multi_aff
*pma_i
;
5771 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
5772 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
5773 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
5776 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
5778 isl_multi_pw_aff_free(mpa
);
5782 /* Construct and return a multi piecewise affine expression
5783 * that is equal to the given multi affine expression.
5785 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
5786 __isl_take isl_multi_aff
*ma
)
5789 isl_multi_pw_aff
*mpa
;
5794 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
5795 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
5797 for (i
= 0; i
< n
; ++i
) {
5800 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
5801 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
5804 isl_multi_aff_free(ma
);
5808 /* Construct and return a multi piecewise affine expression
5809 * that is equal to the given piecewise multi affine expression.
5811 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
5812 __isl_take isl_pw_multi_aff
*pma
)
5816 isl_multi_pw_aff
*mpa
;
5821 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5822 space
= isl_pw_multi_aff_get_space(pma
);
5823 mpa
= isl_multi_pw_aff_alloc(space
);
5825 for (i
= 0; i
< n
; ++i
) {
5828 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
5829 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
5832 isl_pw_multi_aff_free(pma
);
5836 /* Do "pa1" and "pa2" represent the same function?
5838 * We first check if they are obviously equal.
5839 * If not, we convert them to maps and check if those are equal.
5841 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
5844 isl_map
*map1
, *map2
;
5849 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
5850 if (equal
< 0 || equal
)
5853 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
5854 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
5855 equal
= isl_map_is_equal(map1
, map2
);
5862 /* Do "mpa1" and "mpa2" represent the same function?
5864 * Note that we cannot convert the entire isl_multi_pw_aff
5865 * to a map because the domains of the piecewise affine expressions
5866 * may not be the same.
5868 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
5869 __isl_keep isl_multi_pw_aff
*mpa2
)
5877 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
5878 mpa2
->space
, isl_dim_param
)) {
5879 if (!isl_space_has_named_params(mpa1
->space
))
5881 if (!isl_space_has_named_params(mpa2
->space
))
5883 mpa1
= isl_multi_pw_aff_copy(mpa1
);
5884 mpa2
= isl_multi_pw_aff_copy(mpa2
);
5885 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
5886 isl_multi_pw_aff_get_space(mpa2
));
5887 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
5888 isl_multi_pw_aff_get_space(mpa1
));
5889 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
5890 isl_multi_pw_aff_free(mpa1
);
5891 isl_multi_pw_aff_free(mpa2
);
5895 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
5896 if (equal
< 0 || !equal
)
5899 for (i
= 0; i
< mpa1
->n
; ++i
) {
5900 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
5901 if (equal
< 0 || !equal
)
5908 /* Coalesce the elements of "mpa".
5910 * Note that such coalescing does not change the meaning of "mpa"
5911 * so there is no need to cow. We do need to be careful not to
5912 * destroy any other copies of "mpa" in case of failure.
5914 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
5915 __isl_take isl_multi_pw_aff
*mpa
)
5922 for (i
= 0; i
< mpa
->n
; ++i
) {
5923 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
5924 pa
= isl_pw_aff_coalesce(pa
);
5926 return isl_multi_pw_aff_free(mpa
);
5927 isl_pw_aff_free(mpa
->p
[i
]);
5934 /* Compute the pullback of "mpa" by the function represented by "ma".
5935 * In other words, plug in "ma" in "mpa".
5937 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5939 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
5940 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
5943 isl_space
*space
= NULL
;
5945 mpa
= isl_multi_pw_aff_cow(mpa
);
5949 space
= isl_space_join(isl_multi_aff_get_space(ma
),
5950 isl_multi_pw_aff_get_space(mpa
));
5954 for (i
= 0; i
< mpa
->n
; ++i
) {
5955 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
5956 isl_multi_aff_copy(ma
));
5961 isl_multi_aff_free(ma
);
5962 isl_space_free(mpa
->space
);
5966 isl_space_free(space
);
5967 isl_multi_pw_aff_free(mpa
);
5968 isl_multi_aff_free(ma
);
5972 /* Compute the pullback of "mpa" by the function represented by "ma".
5973 * In other words, plug in "ma" in "mpa".
5975 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
5976 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
5980 if (isl_space_match(mpa
->space
, isl_dim_param
,
5981 ma
->space
, isl_dim_param
))
5982 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
5983 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
5984 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
5985 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
5987 isl_multi_pw_aff_free(mpa
);
5988 isl_multi_aff_free(ma
);
5992 /* Compute the pullback of "mpa" by the function represented by "pma".
5993 * In other words, plug in "pma" in "mpa".
5995 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5997 static __isl_give isl_multi_pw_aff
*
5998 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5999 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6002 isl_space
*space
= NULL
;
6004 mpa
= isl_multi_pw_aff_cow(mpa
);
6008 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6009 isl_multi_pw_aff_get_space(mpa
));
6011 for (i
= 0; i
< mpa
->n
; ++i
) {
6012 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6013 isl_pw_multi_aff_copy(pma
));
6018 isl_pw_multi_aff_free(pma
);
6019 isl_space_free(mpa
->space
);
6023 isl_space_free(space
);
6024 isl_multi_pw_aff_free(mpa
);
6025 isl_pw_multi_aff_free(pma
);
6029 /* Compute the pullback of "mpa" by the function represented by "pma".
6030 * In other words, plug in "pma" in "mpa".
6032 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6033 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6037 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6038 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6039 mpa
= isl_multi_pw_aff_align_params(mpa
,
6040 isl_pw_multi_aff_get_space(pma
));
6041 pma
= isl_pw_multi_aff_align_params(pma
,
6042 isl_multi_pw_aff_get_space(mpa
));
6043 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6045 isl_multi_pw_aff_free(mpa
);
6046 isl_pw_multi_aff_free(pma
);
6050 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6051 * with the domain of "aff". The domain of the result is the same
6053 * "mpa" and "aff" are assumed to have been aligned.
6055 * We first extract the parametric constant from "aff", defined
6056 * over the correct domain.
6057 * Then we add the appropriate combinations of the members of "mpa".
6058 * Finally, we add the integer divisions through recursive calls.
6060 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6061 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6063 int i
, n_param
, n_in
, n_div
;
6069 n_param
= isl_aff_dim(aff
, isl_dim_param
);
6070 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6071 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6073 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6074 tmp
= isl_aff_copy(aff
);
6075 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6076 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6077 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6078 isl_space_dim(space
, isl_dim_set
));
6079 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6080 pa
= isl_pw_aff_from_aff(tmp
);
6082 for (i
= 0; i
< n_in
; ++i
) {
6085 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6087 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6088 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6089 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6090 pa
= isl_pw_aff_add(pa
, pa_i
);
6093 for (i
= 0; i
< n_div
; ++i
) {
6097 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6099 div
= isl_aff_get_div(aff
, i
);
6100 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6101 isl_multi_pw_aff_copy(mpa
), div
);
6102 pa_i
= isl_pw_aff_floor(pa_i
);
6103 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6104 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6105 pa
= isl_pw_aff_add(pa
, pa_i
);
6108 isl_multi_pw_aff_free(mpa
);
6114 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6115 * with the domain of "aff". The domain of the result is the same
6118 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6119 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6123 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6124 mpa
->space
, isl_dim_param
))
6125 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6127 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6128 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6130 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6133 isl_multi_pw_aff_free(mpa
);
6137 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6138 * with the domain of "pa". The domain of the result is the same
6140 * "mpa" and "pa" are assumed to have been aligned.
6142 * We consider each piece in turn. Note that the domains of the
6143 * pieces are assumed to be disjoint and they remain disjoint
6144 * after taking the preimage (over the same function).
6146 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6147 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6156 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6157 isl_pw_aff_get_space(pa
));
6158 res
= isl_pw_aff_empty(space
);
6160 for (i
= 0; i
< pa
->n
; ++i
) {
6164 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6165 isl_multi_pw_aff_copy(mpa
),
6166 isl_aff_copy(pa
->p
[i
].aff
));
6167 domain
= isl_set_copy(pa
->p
[i
].set
);
6168 domain
= isl_set_preimage_multi_pw_aff(domain
,
6169 isl_multi_pw_aff_copy(mpa
));
6170 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6171 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6174 isl_pw_aff_free(pa
);
6175 isl_multi_pw_aff_free(mpa
);
6178 isl_pw_aff_free(pa
);
6179 isl_multi_pw_aff_free(mpa
);
6183 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6184 * with the domain of "pa". The domain of the result is the same
6187 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6188 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6192 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6193 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6195 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6196 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6198 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6200 isl_pw_aff_free(pa
);
6201 isl_multi_pw_aff_free(mpa
);
6205 /* Compute the pullback of "pa" by the function represented by "mpa".
6206 * In other words, plug in "mpa" in "pa".
6207 * "pa" and "mpa" are assumed to have been aligned.
6209 * The pullback is computed by applying "pa" to "mpa".
6211 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6212 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6214 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6217 /* Compute the pullback of "pa" by the function represented by "mpa".
6218 * In other words, plug in "mpa" in "pa".
6220 * The pullback is computed by applying "pa" to "mpa".
6222 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6223 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6225 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6228 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6229 * In other words, plug in "mpa2" in "mpa1".
6231 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6233 * We pullback each member of "mpa1" in turn.
6235 static __isl_give isl_multi_pw_aff
*
6236 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6237 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6240 isl_space
*space
= NULL
;
6242 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6246 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6247 isl_multi_pw_aff_get_space(mpa1
));
6249 for (i
= 0; i
< mpa1
->n
; ++i
) {
6250 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6251 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6256 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6258 isl_multi_pw_aff_free(mpa2
);
6261 isl_space_free(space
);
6262 isl_multi_pw_aff_free(mpa1
);
6263 isl_multi_pw_aff_free(mpa2
);
6267 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6268 * In other words, plug in "mpa2" in "mpa1".
6270 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6271 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6273 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6274 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6277 /* Compare two isl_affs.
6279 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6280 * than "aff2" and 0 if they are equal.
6282 * The order is fairly arbitrary. We do consider expressions that only involve
6283 * earlier dimensions as "smaller".
6285 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6298 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6302 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6303 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6305 return last1
- last2
;
6307 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
6310 /* Compare two isl_pw_affs.
6312 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6313 * than "pa2" and 0 if they are equal.
6315 * The order is fairly arbitrary. We do consider expressions that only involve
6316 * earlier dimensions as "smaller".
6318 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
6319 __isl_keep isl_pw_aff
*pa2
)
6332 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
6336 if (pa1
->n
!= pa2
->n
)
6337 return pa1
->n
- pa2
->n
;
6339 for (i
= 0; i
< pa1
->n
; ++i
) {
6340 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
6343 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);