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>
41 #define BASE union_pw_aff
43 #include <isl_list_templ.c>
46 #define BASE union_pw_multi_aff
48 #include <isl_list_templ.c>
50 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
51 __isl_take isl_vec
*v
)
58 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
68 isl_local_space_free(ls
);
73 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
82 ctx
= isl_local_space_get_ctx(ls
);
83 if (!isl_local_space_divs_known(ls
))
84 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
86 if (!isl_local_space_is_set(ls
))
87 isl_die(ctx
, isl_error_invalid
,
88 "domain of affine expression should be a set",
91 total
= isl_local_space_dim(ls
, isl_dim_all
);
92 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
93 return isl_aff_alloc_vec(ls
, v
);
95 isl_local_space_free(ls
);
99 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
103 aff
= isl_aff_alloc(ls
);
107 isl_int_set_si(aff
->v
->el
[0], 1);
108 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
113 /* Return a piecewise affine expression defined on the specified domain
114 * that is equal to zero.
116 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
118 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
121 /* Return an affine expression defined on the specified domain
122 * that represents NaN.
124 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
128 aff
= isl_aff_alloc(ls
);
132 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
137 /* Return a piecewise affine expression defined on the specified domain
138 * that represents NaN.
140 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
142 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
145 /* Return an affine expression that is equal to "val" on
146 * domain local space "ls".
148 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
149 __isl_take isl_val
*val
)
155 if (!isl_val_is_rat(val
))
156 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
157 "expecting rational value", goto error
);
159 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
163 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
164 isl_int_set(aff
->v
->el
[1], val
->n
);
165 isl_int_set(aff
->v
->el
[0], val
->d
);
167 isl_local_space_free(ls
);
171 isl_local_space_free(ls
);
176 /* Return an affine expression that is equal to the specified dimension
179 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
180 enum isl_dim_type type
, unsigned pos
)
188 space
= isl_local_space_get_space(ls
);
191 if (isl_space_is_map(space
))
192 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
193 "expecting (parameter) set space", goto error
);
194 if (pos
>= isl_local_space_dim(ls
, type
))
195 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
196 "position out of bounds", goto error
);
198 isl_space_free(space
);
199 aff
= isl_aff_alloc(ls
);
203 pos
+= isl_local_space_offset(aff
->ls
, type
);
205 isl_int_set_si(aff
->v
->el
[0], 1);
206 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
207 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
211 isl_local_space_free(ls
);
212 isl_space_free(space
);
216 /* Return a piecewise affine expression that is equal to
217 * the specified dimension in "ls".
219 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
220 enum isl_dim_type type
, unsigned pos
)
222 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
225 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
234 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
239 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
240 isl_vec_copy(aff
->v
));
243 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
251 return isl_aff_dup(aff
);
254 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
262 isl_local_space_free(aff
->ls
);
263 isl_vec_free(aff
->v
);
270 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
272 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
275 /* Externally, an isl_aff has a map space, but internally, the
276 * ls field corresponds to the domain of that space.
278 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
282 if (type
== isl_dim_out
)
284 if (type
== isl_dim_in
)
286 return isl_local_space_dim(aff
->ls
, type
);
289 /* Return the position of the dimension of the given type and name
291 * Return -1 if no such dimension can be found.
293 int isl_aff_find_dim_by_name(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
298 if (type
== isl_dim_out
)
300 if (type
== isl_dim_in
)
302 return isl_local_space_find_dim_by_name(aff
->ls
, type
, name
);
305 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
307 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
310 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
315 space
= isl_local_space_get_space(aff
->ls
);
316 space
= isl_space_from_domain(space
);
317 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
321 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
322 __isl_keep isl_aff
*aff
)
324 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
327 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
332 ls
= isl_local_space_copy(aff
->ls
);
333 ls
= isl_local_space_from_domain(ls
);
334 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
338 /* Externally, an isl_aff has a map space, but internally, the
339 * ls field corresponds to the domain of that space.
341 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
342 enum isl_dim_type type
, unsigned pos
)
346 if (type
== isl_dim_out
)
348 if (type
== isl_dim_in
)
350 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
353 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
354 __isl_take isl_space
*dim
)
356 aff
= isl_aff_cow(aff
);
360 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
362 return isl_aff_free(aff
);
371 /* Reset the space of "aff". This function is called from isl_pw_templ.c
372 * and doesn't know if the space of an element object is represented
373 * directly or through its domain. It therefore passes along both.
375 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
376 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
378 isl_space_free(space
);
379 return isl_aff_reset_domain_space(aff
, domain
);
382 /* Reorder the coefficients of the affine expression based
383 * on the given reodering.
384 * The reordering r is assumed to have been extended with the local
387 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
388 __isl_take isl_reordering
*r
, int n_div
)
396 res
= isl_vec_alloc(vec
->ctx
,
397 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
398 isl_seq_cpy(res
->el
, vec
->el
, 2);
399 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
400 for (i
= 0; i
< r
->len
; ++i
)
401 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
403 isl_reordering_free(r
);
408 isl_reordering_free(r
);
412 /* Reorder the dimensions of the domain of "aff" according
413 * to the given reordering.
415 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
416 __isl_take isl_reordering
*r
)
418 aff
= isl_aff_cow(aff
);
422 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
423 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
424 aff
->ls
->div
->n_row
);
425 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
427 if (!aff
->v
|| !aff
->ls
)
428 return isl_aff_free(aff
);
433 isl_reordering_free(r
);
437 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
438 __isl_take isl_space
*model
)
443 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
444 model
, isl_dim_param
)) {
447 model
= isl_space_drop_dims(model
, isl_dim_in
,
448 0, isl_space_dim(model
, isl_dim_in
));
449 model
= isl_space_drop_dims(model
, isl_dim_out
,
450 0, isl_space_dim(model
, isl_dim_out
));
451 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
452 exp
= isl_reordering_extend_space(exp
,
453 isl_aff_get_domain_space(aff
));
454 aff
= isl_aff_realign_domain(aff
, exp
);
457 isl_space_free(model
);
460 isl_space_free(model
);
465 /* Is "aff" obviously equal to zero?
467 * If the denominator is zero, then "aff" is not equal to zero.
469 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
474 if (isl_int_is_zero(aff
->v
->el
[0]))
476 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
479 /* Does "aff" represent NaN?
481 int isl_aff_is_nan(__isl_keep isl_aff
*aff
)
486 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
489 /* Does "pa" involve any NaNs?
491 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
500 for (i
= 0; i
< pa
->n
; ++i
) {
501 int is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
502 if (is_nan
< 0 || is_nan
)
509 /* Are "aff1" and "aff2" obviously equal?
511 * NaN is not equal to anything, not even to another NaN.
513 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
520 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
523 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
524 if (equal
< 0 || !equal
)
527 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
530 /* Return the common denominator of "aff" in "v".
532 * We cannot return anything meaningful in case of a NaN.
534 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
538 if (isl_aff_is_nan(aff
))
539 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
540 "cannot get denominator of NaN", return -1);
541 isl_int_set(*v
, aff
->v
->el
[0]);
545 /* Return the common denominator of "aff".
547 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
554 ctx
= isl_aff_get_ctx(aff
);
555 if (isl_aff_is_nan(aff
))
556 return isl_val_nan(ctx
);
557 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
560 /* Return the constant term of "aff" in "v".
562 * We cannot return anything meaningful in case of a NaN.
564 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
568 if (isl_aff_is_nan(aff
))
569 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
570 "cannot get constant term of NaN", return -1);
571 isl_int_set(*v
, aff
->v
->el
[1]);
575 /* Return the constant term of "aff".
577 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
585 ctx
= isl_aff_get_ctx(aff
);
586 if (isl_aff_is_nan(aff
))
587 return isl_val_nan(ctx
);
588 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
589 return isl_val_normalize(v
);
592 /* Return the coefficient of the variable of type "type" at position "pos"
595 * We cannot return anything meaningful in case of a NaN.
597 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
598 enum isl_dim_type type
, int pos
, isl_int
*v
)
603 if (type
== isl_dim_out
)
604 isl_die(aff
->v
->ctx
, isl_error_invalid
,
605 "output/set dimension does not have a coefficient",
607 if (type
== isl_dim_in
)
610 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
611 isl_die(aff
->v
->ctx
, isl_error_invalid
,
612 "position out of bounds", return -1);
614 if (isl_aff_is_nan(aff
))
615 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
616 "cannot get coefficient of NaN", return -1);
617 pos
+= isl_local_space_offset(aff
->ls
, type
);
618 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
623 /* Return the coefficient of the variable of type "type" at position "pos"
626 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
627 enum isl_dim_type type
, int pos
)
635 ctx
= isl_aff_get_ctx(aff
);
636 if (type
== isl_dim_out
)
637 isl_die(ctx
, isl_error_invalid
,
638 "output/set dimension does not have a coefficient",
640 if (type
== isl_dim_in
)
643 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
644 isl_die(ctx
, isl_error_invalid
,
645 "position out of bounds", return NULL
);
647 if (isl_aff_is_nan(aff
))
648 return isl_val_nan(ctx
);
649 pos
+= isl_local_space_offset(aff
->ls
, type
);
650 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
651 return isl_val_normalize(v
);
654 /* Return the sign of the coefficient of the variable of type "type"
655 * at position "pos" of "aff".
657 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
665 ctx
= isl_aff_get_ctx(aff
);
666 if (type
== isl_dim_out
)
667 isl_die(ctx
, isl_error_invalid
,
668 "output/set dimension does not have a coefficient",
670 if (type
== isl_dim_in
)
673 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
674 isl_die(ctx
, isl_error_invalid
,
675 "position out of bounds", return 0);
677 pos
+= isl_local_space_offset(aff
->ls
, type
);
678 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
681 /* Replace the denominator of "aff" by "v".
683 * A NaN is unaffected by this operation.
685 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
689 if (isl_aff_is_nan(aff
))
691 aff
= isl_aff_cow(aff
);
695 aff
->v
= isl_vec_cow(aff
->v
);
697 return isl_aff_free(aff
);
699 isl_int_set(aff
->v
->el
[0], v
);
704 /* Replace the numerator of the constant term of "aff" by "v".
706 * A NaN is unaffected by this operation.
708 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
712 if (isl_aff_is_nan(aff
))
714 aff
= isl_aff_cow(aff
);
718 aff
->v
= isl_vec_cow(aff
->v
);
720 return isl_aff_free(aff
);
722 isl_int_set(aff
->v
->el
[1], v
);
727 /* Replace the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
732 __isl_take isl_val
*v
)
737 if (isl_aff_is_nan(aff
)) {
742 if (!isl_val_is_rat(v
))
743 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
744 "expecting rational value", goto error
);
746 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
747 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
752 aff
= isl_aff_cow(aff
);
755 aff
->v
= isl_vec_cow(aff
->v
);
759 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
760 isl_int_set(aff
->v
->el
[1], v
->n
);
761 } else if (isl_int_is_one(v
->d
)) {
762 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
764 isl_seq_scale(aff
->v
->el
+ 1,
765 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
766 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
767 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
768 aff
->v
= isl_vec_normalize(aff
->v
);
781 /* Add "v" to the constant term of "aff".
783 * A NaN is unaffected by this operation.
785 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
787 if (isl_int_is_zero(v
))
792 if (isl_aff_is_nan(aff
))
794 aff
= isl_aff_cow(aff
);
798 aff
->v
= isl_vec_cow(aff
->v
);
800 return isl_aff_free(aff
);
802 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
807 /* Add "v" to the constant term of "aff".
809 * A NaN is unaffected by this operation.
811 __isl_give isl_aff
*isl_aff_add_constant_val(__isl_take isl_aff
*aff
,
812 __isl_take isl_val
*v
)
817 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
822 if (!isl_val_is_rat(v
))
823 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
824 "expecting rational value", goto error
);
826 aff
= isl_aff_cow(aff
);
830 aff
->v
= isl_vec_cow(aff
->v
);
834 if (isl_int_is_one(v
->d
)) {
835 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
836 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
837 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
838 aff
->v
= isl_vec_normalize(aff
->v
);
842 isl_seq_scale(aff
->v
->el
+ 1,
843 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
844 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
845 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
846 aff
->v
= isl_vec_normalize(aff
->v
);
859 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
864 isl_int_set_si(t
, v
);
865 aff
= isl_aff_add_constant(aff
, t
);
871 /* Add "v" to the numerator of the constant term of "aff".
873 * A NaN is unaffected by this operation.
875 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
877 if (isl_int_is_zero(v
))
882 if (isl_aff_is_nan(aff
))
884 aff
= isl_aff_cow(aff
);
888 aff
->v
= isl_vec_cow(aff
->v
);
890 return isl_aff_free(aff
);
892 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
897 /* Add "v" to the numerator of the constant term of "aff".
899 * A NaN is unaffected by this operation.
901 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
909 isl_int_set_si(t
, v
);
910 aff
= isl_aff_add_constant_num(aff
, t
);
916 /* Replace the numerator of the constant term of "aff" by "v".
918 * A NaN is unaffected by this operation.
920 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
924 if (isl_aff_is_nan(aff
))
926 aff
= isl_aff_cow(aff
);
930 aff
->v
= isl_vec_cow(aff
->v
);
932 return isl_aff_free(aff
);
934 isl_int_set_si(aff
->v
->el
[1], v
);
939 /* Replace the numerator of the coefficient of the variable of type "type"
940 * at position "pos" of "aff" by "v".
942 * A NaN is unaffected by this operation.
944 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
945 enum isl_dim_type type
, int pos
, isl_int v
)
950 if (type
== isl_dim_out
)
951 isl_die(aff
->v
->ctx
, isl_error_invalid
,
952 "output/set dimension does not have a coefficient",
953 return isl_aff_free(aff
));
954 if (type
== isl_dim_in
)
957 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
958 isl_die(aff
->v
->ctx
, isl_error_invalid
,
959 "position out of bounds", return isl_aff_free(aff
));
961 if (isl_aff_is_nan(aff
))
963 aff
= isl_aff_cow(aff
);
967 aff
->v
= isl_vec_cow(aff
->v
);
969 return isl_aff_free(aff
);
971 pos
+= isl_local_space_offset(aff
->ls
, type
);
972 isl_int_set(aff
->v
->el
[1 + pos
], v
);
977 /* Replace the numerator of the coefficient of the variable of type "type"
978 * at position "pos" of "aff" by "v".
980 * A NaN is unaffected by this operation.
982 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
983 enum isl_dim_type type
, int pos
, int v
)
988 if (type
== isl_dim_out
)
989 isl_die(aff
->v
->ctx
, isl_error_invalid
,
990 "output/set dimension does not have a coefficient",
991 return isl_aff_free(aff
));
992 if (type
== isl_dim_in
)
995 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
996 isl_die(aff
->v
->ctx
, isl_error_invalid
,
997 "position out of bounds", return isl_aff_free(aff
));
999 if (isl_aff_is_nan(aff
))
1001 pos
+= isl_local_space_offset(aff
->ls
, type
);
1002 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
1005 aff
= isl_aff_cow(aff
);
1009 aff
->v
= isl_vec_cow(aff
->v
);
1011 return isl_aff_free(aff
);
1013 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
1018 /* Replace the coefficient of the variable of type "type" at position "pos"
1021 * A NaN is unaffected by this operation.
1023 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
1024 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1029 if (type
== isl_dim_out
)
1030 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1031 "output/set dimension does not have a coefficient",
1033 if (type
== isl_dim_in
)
1036 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1037 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1038 "position out of bounds", goto error
);
1040 if (isl_aff_is_nan(aff
)) {
1044 if (!isl_val_is_rat(v
))
1045 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1046 "expecting rational value", goto error
);
1048 pos
+= isl_local_space_offset(aff
->ls
, type
);
1049 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1050 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1055 aff
= isl_aff_cow(aff
);
1058 aff
->v
= isl_vec_cow(aff
->v
);
1062 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1063 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1064 } else if (isl_int_is_one(v
->d
)) {
1065 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1067 isl_seq_scale(aff
->v
->el
+ 1,
1068 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1069 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1070 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1071 aff
->v
= isl_vec_normalize(aff
->v
);
1084 /* Add "v" to the coefficient of the variable of type "type"
1085 * at position "pos" of "aff".
1087 * A NaN is unaffected by this operation.
1089 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1090 enum isl_dim_type type
, int pos
, isl_int v
)
1095 if (type
== isl_dim_out
)
1096 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1097 "output/set dimension does not have a coefficient",
1098 return isl_aff_free(aff
));
1099 if (type
== isl_dim_in
)
1102 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1103 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1104 "position out of bounds", return isl_aff_free(aff
));
1106 if (isl_aff_is_nan(aff
))
1108 aff
= isl_aff_cow(aff
);
1112 aff
->v
= isl_vec_cow(aff
->v
);
1114 return isl_aff_free(aff
);
1116 pos
+= isl_local_space_offset(aff
->ls
, type
);
1117 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1122 /* Add "v" to the coefficient of the variable of type "type"
1123 * at position "pos" of "aff".
1125 * A NaN is unaffected by this operation.
1127 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1128 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1133 if (isl_val_is_zero(v
)) {
1138 if (type
== isl_dim_out
)
1139 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1140 "output/set dimension does not have a coefficient",
1142 if (type
== isl_dim_in
)
1145 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1146 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1147 "position out of bounds", goto error
);
1149 if (isl_aff_is_nan(aff
)) {
1153 if (!isl_val_is_rat(v
))
1154 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1155 "expecting rational value", goto error
);
1157 aff
= isl_aff_cow(aff
);
1161 aff
->v
= isl_vec_cow(aff
->v
);
1165 pos
+= isl_local_space_offset(aff
->ls
, type
);
1166 if (isl_int_is_one(v
->d
)) {
1167 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1168 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1169 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1170 aff
->v
= isl_vec_normalize(aff
->v
);
1174 isl_seq_scale(aff
->v
->el
+ 1,
1175 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1176 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1177 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1178 aff
->v
= isl_vec_normalize(aff
->v
);
1191 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1192 enum isl_dim_type type
, int pos
, int v
)
1197 isl_int_set_si(t
, v
);
1198 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1204 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1209 return isl_local_space_get_div(aff
->ls
, pos
);
1212 /* Return the negation of "aff".
1214 * As a special case, -NaN = NaN.
1216 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1220 if (isl_aff_is_nan(aff
))
1222 aff
= isl_aff_cow(aff
);
1225 aff
->v
= isl_vec_cow(aff
->v
);
1227 return isl_aff_free(aff
);
1229 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1234 /* Remove divs from the local space that do not appear in the affine
1236 * We currently only remove divs at the end.
1237 * Some intermediate divs may also not appear directly in the affine
1238 * expression, but we would also need to check that no other divs are
1239 * defined in terms of them.
1241 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1250 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1251 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1253 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1257 aff
= isl_aff_cow(aff
);
1261 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1262 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1263 if (!aff
->ls
|| !aff
->v
)
1264 return isl_aff_free(aff
);
1269 /* Given two affine expressions "p" of length p_len (including the
1270 * denominator and the constant term) and "subs" of length subs_len,
1271 * plug in "subs" for the variable at position "pos".
1272 * The variables of "subs" and "p" are assumed to match up to subs_len,
1273 * but "p" may have additional variables.
1274 * "v" is an initialized isl_int that can be used internally.
1276 * In particular, if "p" represents the expression
1280 * with i the variable at position "pos" and "subs" represents the expression
1284 * then the result represents the expression
1289 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1290 int p_len
, int subs_len
, isl_int v
)
1292 isl_int_set(v
, p
[1 + pos
]);
1293 isl_int_set_si(p
[1 + pos
], 0);
1294 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1295 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1296 isl_int_mul(p
[0], p
[0], subs
[0]);
1299 /* Look for any divs in the aff->ls with a denominator equal to one
1300 * and plug them into the affine expression and any subsequent divs
1301 * that may reference the div.
1303 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1309 isl_local_space
*ls
;
1315 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1317 for (i
= 0; i
< n
; ++i
) {
1318 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1320 ls
= isl_local_space_copy(aff
->ls
);
1321 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1322 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1323 vec
= isl_vec_copy(aff
->v
);
1324 vec
= isl_vec_cow(vec
);
1330 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1331 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1336 isl_vec_free(aff
->v
);
1338 isl_local_space_free(aff
->ls
);
1345 isl_local_space_free(ls
);
1346 return isl_aff_free(aff
);
1349 /* Look for any divs j that appear with a unit coefficient inside
1350 * the definitions of other divs i and plug them into the definitions
1353 * In particular, an expression of the form
1355 * floor((f(..) + floor(g(..)/n))/m)
1359 * floor((n * f(..) + g(..))/(n * m))
1361 * This simplification is correct because we can move the expression
1362 * f(..) into the inner floor in the original expression to obtain
1364 * floor(floor((n * f(..) + g(..))/n)/m)
1366 * from which we can derive the simplified expression.
1368 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1376 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1377 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1378 for (i
= 1; i
< n
; ++i
) {
1379 for (j
= 0; j
< i
; ++j
) {
1380 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1382 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1383 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1384 aff
->v
->size
, i
, 1);
1386 return isl_aff_free(aff
);
1393 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1395 * Even though this function is only called on isl_affs with a single
1396 * reference, we are careful to only change aff->v and aff->ls together.
1398 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1400 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1401 isl_local_space
*ls
;
1404 ls
= isl_local_space_copy(aff
->ls
);
1405 ls
= isl_local_space_swap_div(ls
, a
, b
);
1406 v
= isl_vec_copy(aff
->v
);
1411 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1412 isl_vec_free(aff
->v
);
1414 isl_local_space_free(aff
->ls
);
1420 isl_local_space_free(ls
);
1421 return isl_aff_free(aff
);
1424 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1426 * We currently do not actually remove div "b", but simply add its
1427 * coefficient to that of "a" and then zero it out.
1429 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1431 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1433 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1436 aff
->v
= isl_vec_cow(aff
->v
);
1438 return isl_aff_free(aff
);
1440 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1441 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1442 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1447 /* Sort the divs in the local space of "aff" according to
1448 * the comparison function "cmp_row" in isl_local_space.c,
1449 * combining the coefficients of identical divs.
1451 * Reordering divs does not change the semantics of "aff",
1452 * so there is no need to call isl_aff_cow.
1453 * Moreover, this function is currently only called on isl_affs
1454 * with a single reference.
1456 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1464 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1465 n
= isl_aff_dim(aff
, isl_dim_div
);
1466 for (i
= 1; i
< n
; ++i
) {
1467 for (j
= i
- 1; j
>= 0; --j
) {
1468 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1472 aff
= merge_divs(aff
, j
, j
+ 1);
1474 aff
= swap_div(aff
, j
, j
+ 1);
1483 /* Normalize the representation of "aff".
1485 * This function should only be called of "new" isl_affs, i.e.,
1486 * with only a single reference. We therefore do not need to
1487 * worry about affecting other instances.
1489 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1493 aff
->v
= isl_vec_normalize(aff
->v
);
1495 return isl_aff_free(aff
);
1496 aff
= plug_in_integral_divs(aff
);
1497 aff
= plug_in_unit_divs(aff
);
1498 aff
= sort_divs(aff
);
1499 aff
= isl_aff_remove_unused_divs(aff
);
1503 /* Given f, return floor(f).
1504 * If f is an integer expression, then just return f.
1505 * If f is a constant, then return the constant floor(f).
1506 * Otherwise, if f = g/m, write g = q m + r,
1507 * create a new div d = [r/m] and return the expression q + d.
1508 * The coefficients in r are taken to lie between -m/2 and m/2.
1510 * As a special case, floor(NaN) = NaN.
1512 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1522 if (isl_aff_is_nan(aff
))
1524 if (isl_int_is_one(aff
->v
->el
[0]))
1527 aff
= isl_aff_cow(aff
);
1531 aff
->v
= isl_vec_cow(aff
->v
);
1533 return isl_aff_free(aff
);
1535 if (isl_aff_is_cst(aff
)) {
1536 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1537 isl_int_set_si(aff
->v
->el
[0], 1);
1541 div
= isl_vec_copy(aff
->v
);
1542 div
= isl_vec_cow(div
);
1544 return isl_aff_free(aff
);
1546 ctx
= isl_aff_get_ctx(aff
);
1547 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1548 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1549 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1550 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1551 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1552 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1553 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1557 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1559 return isl_aff_free(aff
);
1561 size
= aff
->v
->size
;
1562 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1564 return isl_aff_free(aff
);
1565 isl_int_set_si(aff
->v
->el
[0], 1);
1566 isl_int_set_si(aff
->v
->el
[size
], 1);
1568 aff
= isl_aff_normalize(aff
);
1575 * aff mod m = aff - m * floor(aff/m)
1577 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1581 res
= isl_aff_copy(aff
);
1582 aff
= isl_aff_scale_down(aff
, m
);
1583 aff
= isl_aff_floor(aff
);
1584 aff
= isl_aff_scale(aff
, m
);
1585 res
= isl_aff_sub(res
, aff
);
1592 * aff mod m = aff - m * floor(aff/m)
1594 * with m an integer value.
1596 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1597 __isl_take isl_val
*m
)
1604 if (!isl_val_is_int(m
))
1605 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1606 "expecting integer modulo", goto error
);
1608 res
= isl_aff_copy(aff
);
1609 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1610 aff
= isl_aff_floor(aff
);
1611 aff
= isl_aff_scale_val(aff
, m
);
1612 res
= isl_aff_sub(res
, aff
);
1623 * pwaff mod m = pwaff - m * floor(pwaff/m)
1625 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1629 res
= isl_pw_aff_copy(pwaff
);
1630 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1631 pwaff
= isl_pw_aff_floor(pwaff
);
1632 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1633 res
= isl_pw_aff_sub(res
, pwaff
);
1640 * pa mod m = pa - m * floor(pa/m)
1642 * with m an integer value.
1644 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1645 __isl_take isl_val
*m
)
1649 if (!isl_val_is_int(m
))
1650 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1651 "expecting integer modulo", goto error
);
1652 pa
= isl_pw_aff_mod(pa
, m
->n
);
1656 isl_pw_aff_free(pa
);
1661 /* Given f, return ceil(f).
1662 * If f is an integer expression, then just return f.
1663 * Otherwise, let f be the expression
1669 * floor((e + m - 1)/m)
1671 * As a special case, ceil(NaN) = NaN.
1673 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1678 if (isl_aff_is_nan(aff
))
1680 if (isl_int_is_one(aff
->v
->el
[0]))
1683 aff
= isl_aff_cow(aff
);
1686 aff
->v
= isl_vec_cow(aff
->v
);
1688 return isl_aff_free(aff
);
1690 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1691 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1692 aff
= isl_aff_floor(aff
);
1697 /* Apply the expansion computed by isl_merge_divs.
1698 * The expansion itself is given by "exp" while the resulting
1699 * list of divs is given by "div".
1701 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1702 __isl_take isl_mat
*div
, int *exp
)
1709 aff
= isl_aff_cow(aff
);
1713 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1714 new_n_div
= isl_mat_rows(div
);
1715 if (new_n_div
< old_n_div
)
1716 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1717 "not an expansion", goto error
);
1719 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1723 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1725 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1726 if (j
>= 0 && exp
[j
] == i
) {
1728 isl_int_swap(aff
->v
->el
[offset
+ i
],
1729 aff
->v
->el
[offset
+ j
]);
1732 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1735 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1746 /* Add two affine expressions that live in the same local space.
1748 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1749 __isl_take isl_aff
*aff2
)
1753 aff1
= isl_aff_cow(aff1
);
1757 aff1
->v
= isl_vec_cow(aff1
->v
);
1763 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1764 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1765 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1766 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1767 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1768 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1769 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1781 /* Return the sum of "aff1" and "aff2".
1783 * If either of the two is NaN, then the result is NaN.
1785 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1786 __isl_take isl_aff
*aff2
)
1797 ctx
= isl_aff_get_ctx(aff1
);
1798 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1799 isl_die(ctx
, isl_error_invalid
,
1800 "spaces don't match", goto error
);
1802 if (isl_aff_is_nan(aff1
)) {
1806 if (isl_aff_is_nan(aff2
)) {
1811 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1812 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1813 if (n_div1
== 0 && n_div2
== 0)
1814 return add_expanded(aff1
, aff2
);
1816 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1817 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1818 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1821 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1822 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1823 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1827 return add_expanded(aff1
, aff2
);
1836 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1837 __isl_take isl_aff
*aff2
)
1839 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1842 /* Return the result of scaling "aff" by a factor of "f".
1844 * As a special case, f * NaN = NaN.
1846 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1852 if (isl_aff_is_nan(aff
))
1855 if (isl_int_is_one(f
))
1858 aff
= isl_aff_cow(aff
);
1861 aff
->v
= isl_vec_cow(aff
->v
);
1863 return isl_aff_free(aff
);
1865 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1866 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1871 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1872 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1873 isl_int_divexact(gcd
, f
, gcd
);
1874 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1880 /* Multiple "aff" by "v".
1882 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1883 __isl_take isl_val
*v
)
1888 if (isl_val_is_one(v
)) {
1893 if (!isl_val_is_rat(v
))
1894 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1895 "expecting rational factor", goto error
);
1897 aff
= isl_aff_scale(aff
, v
->n
);
1898 aff
= isl_aff_scale_down(aff
, v
->d
);
1908 /* Return the result of scaling "aff" down by a factor of "f".
1910 * As a special case, NaN/f = NaN.
1912 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1918 if (isl_aff_is_nan(aff
))
1921 if (isl_int_is_one(f
))
1924 aff
= isl_aff_cow(aff
);
1928 if (isl_int_is_zero(f
))
1929 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1930 "cannot scale down by zero", return isl_aff_free(aff
));
1932 aff
->v
= isl_vec_cow(aff
->v
);
1934 return isl_aff_free(aff
);
1937 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1938 isl_int_gcd(gcd
, gcd
, f
);
1939 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1940 isl_int_divexact(gcd
, f
, gcd
);
1941 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1947 /* Divide "aff" by "v".
1949 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1950 __isl_take isl_val
*v
)
1955 if (isl_val_is_one(v
)) {
1960 if (!isl_val_is_rat(v
))
1961 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1962 "expecting rational factor", goto error
);
1963 if (!isl_val_is_pos(v
))
1964 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1965 "factor needs to be positive", goto error
);
1967 aff
= isl_aff_scale(aff
, v
->d
);
1968 aff
= isl_aff_scale_down(aff
, v
->n
);
1978 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1986 isl_int_set_ui(v
, f
);
1987 aff
= isl_aff_scale_down(aff
, v
);
1993 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1994 enum isl_dim_type type
, unsigned pos
, const char *s
)
1996 aff
= isl_aff_cow(aff
);
1999 if (type
== isl_dim_out
)
2000 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2001 "cannot set name of output/set dimension",
2002 return isl_aff_free(aff
));
2003 if (type
== isl_dim_in
)
2005 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
2007 return isl_aff_free(aff
);
2012 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
2013 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
2015 aff
= isl_aff_cow(aff
);
2018 if (type
== isl_dim_out
)
2019 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2020 "cannot set name of output/set dimension",
2022 if (type
== isl_dim_in
)
2024 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2026 return isl_aff_free(aff
);
2035 /* Replace the identifier of the input tuple of "aff" by "id".
2036 * type is currently required to be equal to isl_dim_in
2038 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2039 enum isl_dim_type type
, __isl_take isl_id
*id
)
2041 aff
= isl_aff_cow(aff
);
2044 if (type
!= isl_dim_out
)
2045 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2046 "cannot only set id of input tuple", goto error
);
2047 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2049 return isl_aff_free(aff
);
2058 /* Exploit the equalities in "eq" to simplify the affine expression
2059 * and the expressions of the integer divisions in the local space.
2060 * The integer divisions in this local space are assumed to appear
2061 * as regular dimensions in "eq".
2063 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2064 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2072 if (eq
->n_eq
== 0) {
2073 isl_basic_set_free(eq
);
2077 aff
= isl_aff_cow(aff
);
2081 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2082 isl_basic_set_copy(eq
));
2083 aff
->v
= isl_vec_cow(aff
->v
);
2084 if (!aff
->ls
|| !aff
->v
)
2087 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2089 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2090 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2091 if (j
< 0 || j
== 0 || j
>= total
)
2094 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2098 isl_basic_set_free(eq
);
2099 aff
= isl_aff_normalize(aff
);
2102 isl_basic_set_free(eq
);
2107 /* Exploit the equalities in "eq" to simplify the affine expression
2108 * and the expressions of the integer divisions in the local space.
2110 __isl_give isl_aff
*isl_aff_substitute_equalities(__isl_take isl_aff
*aff
,
2111 __isl_take isl_basic_set
*eq
)
2117 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2119 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2120 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2122 isl_basic_set_free(eq
);
2127 /* Look for equalities among the variables shared by context and aff
2128 * and the integer divisions of aff, if any.
2129 * The equalities are then used to eliminate coefficients and/or integer
2130 * divisions from aff.
2132 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2133 __isl_take isl_set
*context
)
2135 isl_basic_set
*hull
;
2140 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2142 isl_basic_set
*bset
;
2143 isl_local_space
*ls
;
2144 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2145 ls
= isl_aff_get_domain_local_space(aff
);
2146 bset
= isl_basic_set_from_local_space(ls
);
2147 bset
= isl_basic_set_lift(bset
);
2148 bset
= isl_basic_set_flatten(bset
);
2149 context
= isl_set_intersect(context
,
2150 isl_set_from_basic_set(bset
));
2153 hull
= isl_set_affine_hull(context
);
2154 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2157 isl_set_free(context
);
2161 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2162 __isl_take isl_set
*context
)
2164 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2165 dom_context
= isl_set_intersect_params(dom_context
, context
);
2166 return isl_aff_gist(aff
, dom_context
);
2169 /* Return a basic set containing those elements in the space
2170 * of aff where it is positive. "rational" should not be set.
2172 * If "aff" is NaN, then it is not positive.
2174 static __isl_give isl_basic_set
*aff_pos_basic_set(__isl_take isl_aff
*aff
,
2177 isl_constraint
*ineq
;
2178 isl_basic_set
*bset
;
2183 if (isl_aff_is_nan(aff
)) {
2184 isl_space
*space
= isl_aff_get_domain_space(aff
);
2186 return isl_basic_set_empty(space
);
2189 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2190 "rational sets not supported", goto error
);
2192 ineq
= isl_inequality_from_aff(aff
);
2193 c
= isl_constraint_get_constant_val(ineq
);
2194 c
= isl_val_sub_ui(c
, 1);
2195 ineq
= isl_constraint_set_constant_val(ineq
, c
);
2197 bset
= isl_basic_set_from_constraint(ineq
);
2198 bset
= isl_basic_set_simplify(bset
);
2205 /* Return a basic set containing those elements in the space
2206 * of aff where it is non-negative.
2207 * If "rational" is set, then return a rational basic set.
2209 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2211 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2212 __isl_take isl_aff
*aff
, int rational
)
2214 isl_constraint
*ineq
;
2215 isl_basic_set
*bset
;
2219 if (isl_aff_is_nan(aff
)) {
2220 isl_space
*space
= isl_aff_get_domain_space(aff
);
2222 return isl_basic_set_empty(space
);
2225 ineq
= isl_inequality_from_aff(aff
);
2227 bset
= isl_basic_set_from_constraint(ineq
);
2229 bset
= isl_basic_set_set_rational(bset
);
2230 bset
= isl_basic_set_simplify(bset
);
2234 /* Return a basic set containing those elements in the space
2235 * of aff where it is non-negative.
2237 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2239 return aff_nonneg_basic_set(aff
, 0);
2242 /* Return a basic set containing those elements in the domain space
2243 * of aff where it is negative.
2245 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2247 aff
= isl_aff_neg(aff
);
2248 aff
= isl_aff_add_constant_num_si(aff
, -1);
2249 return isl_aff_nonneg_basic_set(aff
);
2252 /* Return a basic set containing those elements in the space
2253 * of aff where it is zero.
2254 * If "rational" is set, then return a rational basic set.
2256 * If "aff" is NaN, then it is not zero.
2258 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2261 isl_constraint
*ineq
;
2262 isl_basic_set
*bset
;
2266 if (isl_aff_is_nan(aff
)) {
2267 isl_space
*space
= isl_aff_get_domain_space(aff
);
2269 return isl_basic_set_empty(space
);
2272 ineq
= isl_equality_from_aff(aff
);
2274 bset
= isl_basic_set_from_constraint(ineq
);
2276 bset
= isl_basic_set_set_rational(bset
);
2277 bset
= isl_basic_set_simplify(bset
);
2281 /* Return a basic set containing those elements in the space
2282 * of aff where it is zero.
2284 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2286 return aff_zero_basic_set(aff
, 0);
2289 /* Return a basic set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2292 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2293 __isl_take isl_aff
*aff2
)
2295 aff1
= isl_aff_sub(aff1
, aff2
);
2297 return isl_aff_nonneg_basic_set(aff1
);
2300 /* Return a basic set containing those elements in the shared space
2301 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2303 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2304 __isl_take isl_aff
*aff2
)
2306 return isl_aff_ge_basic_set(aff2
, aff1
);
2309 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2310 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2312 aff1
= isl_aff_add(aff1
, aff2
);
2313 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2317 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2325 /* Check whether the given affine expression has non-zero coefficient
2326 * for any dimension in the given range or if any of these dimensions
2327 * appear with non-zero coefficients in any of the integer divisions
2328 * involved in the affine expression.
2330 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2331 enum isl_dim_type type
, unsigned first
, unsigned n
)
2343 ctx
= isl_aff_get_ctx(aff
);
2344 if (first
+ n
> isl_aff_dim(aff
, type
))
2345 isl_die(ctx
, isl_error_invalid
,
2346 "range out of bounds", return -1);
2348 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2352 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2353 for (i
= 0; i
< n
; ++i
)
2354 if (active
[first
+ i
]) {
2367 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2368 enum isl_dim_type type
, unsigned first
, unsigned n
)
2374 if (type
== isl_dim_out
)
2375 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2376 "cannot drop output/set dimension",
2377 return isl_aff_free(aff
));
2378 if (type
== isl_dim_in
)
2380 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2383 ctx
= isl_aff_get_ctx(aff
);
2384 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2385 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2386 return isl_aff_free(aff
));
2388 aff
= isl_aff_cow(aff
);
2392 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2394 return isl_aff_free(aff
);
2396 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2397 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2399 return isl_aff_free(aff
);
2404 /* Project the domain of the affine expression onto its parameter space.
2405 * The affine expression may not involve any of the domain dimensions.
2407 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2413 n
= isl_aff_dim(aff
, isl_dim_in
);
2414 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2416 return isl_aff_free(aff
);
2418 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2419 "affine expression involves some of the domain dimensions",
2420 return isl_aff_free(aff
));
2421 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2422 space
= isl_aff_get_domain_space(aff
);
2423 space
= isl_space_params(space
);
2424 aff
= isl_aff_reset_domain_space(aff
, space
);
2428 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2429 enum isl_dim_type type
, unsigned first
, unsigned n
)
2435 if (type
== isl_dim_out
)
2436 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2437 "cannot insert output/set dimensions",
2438 return isl_aff_free(aff
));
2439 if (type
== isl_dim_in
)
2441 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2444 ctx
= isl_aff_get_ctx(aff
);
2445 if (first
> isl_local_space_dim(aff
->ls
, type
))
2446 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2447 return isl_aff_free(aff
));
2449 aff
= isl_aff_cow(aff
);
2453 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2455 return isl_aff_free(aff
);
2457 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2458 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2460 return isl_aff_free(aff
);
2465 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2466 enum isl_dim_type type
, unsigned n
)
2470 pos
= isl_aff_dim(aff
, type
);
2472 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2475 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2476 enum isl_dim_type type
, unsigned n
)
2480 pos
= isl_pw_aff_dim(pwaff
, type
);
2482 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2485 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2486 * to dimensions of "dst_type" at "dst_pos".
2488 * We only support moving input dimensions to parameters and vice versa.
2490 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2491 enum isl_dim_type dst_type
, unsigned dst_pos
,
2492 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2500 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2501 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2504 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2505 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2506 "cannot move output/set dimension", isl_aff_free(aff
));
2507 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2508 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2509 "cannot move divs", isl_aff_free(aff
));
2510 if (dst_type
== isl_dim_in
)
2511 dst_type
= isl_dim_set
;
2512 if (src_type
== isl_dim_in
)
2513 src_type
= isl_dim_set
;
2515 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2516 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2517 "range out of bounds", isl_aff_free(aff
));
2518 if (dst_type
== src_type
)
2519 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2520 "moving dims within the same type not supported",
2523 aff
= isl_aff_cow(aff
);
2527 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2528 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2529 if (dst_type
> src_type
)
2532 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2533 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2534 src_type
, src_pos
, n
);
2535 if (!aff
->v
|| !aff
->ls
)
2536 return isl_aff_free(aff
);
2538 aff
= sort_divs(aff
);
2543 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2545 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2546 return isl_pw_aff_alloc(dom
, aff
);
2550 #define PW isl_pw_aff
2554 #define EL_IS_ZERO is_empty
2558 #define IS_ZERO is_empty
2561 #undef DEFAULT_IS_ZERO
2562 #define DEFAULT_IS_ZERO 0
2569 #include <isl_pw_templ.c>
2572 #define UNION isl_union_pw_aff
2574 #define PART isl_pw_aff
2576 #define PARTS pw_aff
2580 #include <isl_union_templ.c>
2582 static __isl_give isl_set
*align_params_pw_pw_set_and(
2583 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2584 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2585 __isl_take isl_pw_aff
*pwaff2
))
2587 if (!pwaff1
|| !pwaff2
)
2589 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2590 pwaff2
->dim
, isl_dim_param
))
2591 return fn(pwaff1
, pwaff2
);
2592 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2593 !isl_space_has_named_params(pwaff2
->dim
))
2594 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2595 "unaligned unnamed parameters", goto error
);
2596 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2597 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2598 return fn(pwaff1
, pwaff2
);
2600 isl_pw_aff_free(pwaff1
);
2601 isl_pw_aff_free(pwaff2
);
2605 /* Compute a piecewise quasi-affine expression with a domain that
2606 * is the union of those of pwaff1 and pwaff2 and such that on each
2607 * cell, the quasi-affine expression is the better (according to cmp)
2608 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2609 * is defined on a given cell, then the associated expression
2610 * is the defined one.
2612 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2613 __isl_take isl_pw_aff
*pwaff2
,
2614 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2615 __isl_take isl_aff
*aff2
))
2622 if (!pwaff1
|| !pwaff2
)
2625 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2626 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2627 isl_die(ctx
, isl_error_invalid
,
2628 "arguments should live in same space", goto error
);
2630 if (isl_pw_aff_is_empty(pwaff1
)) {
2631 isl_pw_aff_free(pwaff1
);
2635 if (isl_pw_aff_is_empty(pwaff2
)) {
2636 isl_pw_aff_free(pwaff2
);
2640 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2641 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2643 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2644 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2645 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2646 struct isl_set
*common
;
2649 common
= isl_set_intersect(
2650 isl_set_copy(pwaff1
->p
[i
].set
),
2651 isl_set_copy(pwaff2
->p
[j
].set
));
2652 better
= isl_set_from_basic_set(cmp(
2653 isl_aff_copy(pwaff2
->p
[j
].aff
),
2654 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2655 better
= isl_set_intersect(common
, better
);
2656 if (isl_set_plain_is_empty(better
)) {
2657 isl_set_free(better
);
2660 set
= isl_set_subtract(set
, isl_set_copy(better
));
2662 res
= isl_pw_aff_add_piece(res
, better
,
2663 isl_aff_copy(pwaff2
->p
[j
].aff
));
2665 res
= isl_pw_aff_add_piece(res
, set
,
2666 isl_aff_copy(pwaff1
->p
[i
].aff
));
2669 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2670 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2671 for (i
= 0; i
< pwaff1
->n
; ++i
)
2672 set
= isl_set_subtract(set
,
2673 isl_set_copy(pwaff1
->p
[i
].set
));
2674 res
= isl_pw_aff_add_piece(res
, set
,
2675 isl_aff_copy(pwaff2
->p
[j
].aff
));
2678 isl_pw_aff_free(pwaff1
);
2679 isl_pw_aff_free(pwaff2
);
2683 isl_pw_aff_free(pwaff1
);
2684 isl_pw_aff_free(pwaff2
);
2688 /* Compute a piecewise quasi-affine expression with a domain that
2689 * is the union of those of pwaff1 and pwaff2 and such that on each
2690 * cell, the quasi-affine expression is the maximum of those of pwaff1
2691 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2692 * cell, then the associated expression is the defined one.
2694 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2695 __isl_take isl_pw_aff
*pwaff2
)
2697 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2700 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2701 __isl_take isl_pw_aff
*pwaff2
)
2703 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2707 /* Compute a piecewise quasi-affine expression with a domain that
2708 * is the union of those of pwaff1 and pwaff2 and such that on each
2709 * cell, the quasi-affine expression is the minimum of those of pwaff1
2710 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2711 * cell, then the associated expression is the defined one.
2713 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2714 __isl_take isl_pw_aff
*pwaff2
)
2716 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2719 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2720 __isl_take isl_pw_aff
*pwaff2
)
2722 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2726 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2727 __isl_take isl_pw_aff
*pwaff2
, int max
)
2730 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2732 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2735 /* Construct a map with as domain the domain of pwaff and
2736 * one-dimensional range corresponding to the affine expressions.
2738 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2747 dim
= isl_pw_aff_get_space(pwaff
);
2748 map
= isl_map_empty(dim
);
2750 for (i
= 0; i
< pwaff
->n
; ++i
) {
2751 isl_basic_map
*bmap
;
2754 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2755 map_i
= isl_map_from_basic_map(bmap
);
2756 map_i
= isl_map_intersect_domain(map_i
,
2757 isl_set_copy(pwaff
->p
[i
].set
));
2758 map
= isl_map_union_disjoint(map
, map_i
);
2761 isl_pw_aff_free(pwaff
);
2766 /* Construct a map with as domain the domain of pwaff and
2767 * one-dimensional range corresponding to the affine expressions.
2769 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2773 if (isl_space_is_set(pwaff
->dim
))
2774 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2775 "space of input is not a map", goto error
);
2776 return map_from_pw_aff(pwaff
);
2778 isl_pw_aff_free(pwaff
);
2782 /* Construct a one-dimensional set with as parameter domain
2783 * the domain of pwaff and the single set dimension
2784 * corresponding to the affine expressions.
2786 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2790 if (!isl_space_is_set(pwaff
->dim
))
2791 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2792 "space of input is not a set", goto error
);
2793 return map_from_pw_aff(pwaff
);
2795 isl_pw_aff_free(pwaff
);
2799 /* Return a set containing those elements in the domain
2800 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2801 * does not satisfy "fn" (if complement is 1).
2803 * The pieces with a NaN never belong to the result since
2804 * NaN does not satisfy any property.
2806 static __isl_give isl_set
*pw_aff_locus(__isl_take isl_pw_aff
*pwaff
,
2807 __isl_give isl_basic_set
*(*fn
)(__isl_take isl_aff
*aff
, int rational
),
2816 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2818 for (i
= 0; i
< pwaff
->n
; ++i
) {
2819 isl_basic_set
*bset
;
2820 isl_set
*set_i
, *locus
;
2823 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2826 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2827 bset
= fn(isl_aff_copy(pwaff
->p
[i
].aff
), rational
);
2828 locus
= isl_set_from_basic_set(bset
);
2829 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2831 set_i
= isl_set_subtract(set_i
, locus
);
2833 set_i
= isl_set_intersect(set_i
, locus
);
2834 set
= isl_set_union_disjoint(set
, set_i
);
2837 isl_pw_aff_free(pwaff
);
2842 /* Return a set containing those elements in the domain
2843 * of "pa" where it is positive.
2845 __isl_give isl_set
*isl_pw_aff_pos_set(__isl_take isl_pw_aff
*pa
)
2847 return pw_aff_locus(pa
, &aff_pos_basic_set
, 0);
2850 /* Return a set containing those elements in the domain
2851 * of pwaff where it is non-negative.
2853 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2855 return pw_aff_locus(pwaff
, &aff_nonneg_basic_set
, 0);
2858 /* Return a set containing those elements in the domain
2859 * of pwaff where it is zero.
2861 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2863 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 0);
2866 /* Return a set containing those elements in the domain
2867 * of pwaff where it is not zero.
2869 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2871 return pw_aff_locus(pwaff
, &aff_zero_basic_set
, 1);
2874 /* Return a set containing those elements in the shared domain
2875 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2877 * We compute the difference on the shared domain and then construct
2878 * the set of values where this difference is non-negative.
2879 * If strict is set, we first subtract 1 from the difference.
2880 * If equal is set, we only return the elements where pwaff1 and pwaff2
2883 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2884 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2886 isl_set
*set1
, *set2
;
2888 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2889 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2890 set1
= isl_set_intersect(set1
, set2
);
2891 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2892 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2893 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2896 isl_space
*dim
= isl_set_get_space(set1
);
2898 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2899 aff
= isl_aff_add_constant_si(aff
, -1);
2900 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2905 return isl_pw_aff_zero_set(pwaff1
);
2906 return isl_pw_aff_nonneg_set(pwaff1
);
2909 /* Return a set containing those elements in the shared domain
2910 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2912 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2913 __isl_take isl_pw_aff
*pwaff2
)
2915 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2918 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2919 __isl_take isl_pw_aff
*pwaff2
)
2921 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2924 /* Return a set containing those elements in the shared domain
2925 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2927 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2928 __isl_take isl_pw_aff
*pwaff2
)
2930 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2933 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2934 __isl_take isl_pw_aff
*pwaff2
)
2936 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2939 /* Return a set containing those elements in the shared domain
2940 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2942 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2943 __isl_take isl_pw_aff
*pwaff2
)
2945 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2948 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2949 __isl_take isl_pw_aff
*pwaff2
)
2951 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2954 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2955 __isl_take isl_pw_aff
*pwaff2
)
2957 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2960 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2961 __isl_take isl_pw_aff
*pwaff2
)
2963 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2966 /* Return a set containing those elements in the shared domain
2967 * of the elements of list1 and list2 where each element in list1
2968 * has the relation specified by "fn" with each element in list2.
2970 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2971 __isl_take isl_pw_aff_list
*list2
,
2972 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2973 __isl_take isl_pw_aff
*pwaff2
))
2979 if (!list1
|| !list2
)
2982 ctx
= isl_pw_aff_list_get_ctx(list1
);
2983 if (list1
->n
< 1 || list2
->n
< 1)
2984 isl_die(ctx
, isl_error_invalid
,
2985 "list should contain at least one element", goto error
);
2987 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2988 for (i
= 0; i
< list1
->n
; ++i
)
2989 for (j
= 0; j
< list2
->n
; ++j
) {
2992 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2993 isl_pw_aff_copy(list2
->p
[j
]));
2994 set
= isl_set_intersect(set
, set_ij
);
2997 isl_pw_aff_list_free(list1
);
2998 isl_pw_aff_list_free(list2
);
3001 isl_pw_aff_list_free(list1
);
3002 isl_pw_aff_list_free(list2
);
3006 /* Return a set containing those elements in the shared domain
3007 * of the elements of list1 and list2 where each element in list1
3008 * is equal to each element in list2.
3010 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
3011 __isl_take isl_pw_aff_list
*list2
)
3013 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
3016 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
3017 __isl_take isl_pw_aff_list
*list2
)
3019 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
3022 /* Return a set containing those elements in the shared domain
3023 * of the elements of list1 and list2 where each element in list1
3024 * is less than or equal to each element in list2.
3026 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
3027 __isl_take isl_pw_aff_list
*list2
)
3029 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
3032 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
3033 __isl_take isl_pw_aff_list
*list2
)
3035 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
3038 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
3039 __isl_take isl_pw_aff_list
*list2
)
3041 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
3044 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
3045 __isl_take isl_pw_aff_list
*list2
)
3047 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
3051 /* Return a set containing those elements in the shared domain
3052 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3054 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3055 __isl_take isl_pw_aff
*pwaff2
)
3057 isl_set
*set_lt
, *set_gt
;
3059 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3060 isl_pw_aff_copy(pwaff2
));
3061 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3062 return isl_set_union_disjoint(set_lt
, set_gt
);
3065 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3066 __isl_take isl_pw_aff
*pwaff2
)
3068 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3071 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3076 if (isl_int_is_one(v
))
3078 if (!isl_int_is_pos(v
))
3079 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3080 "factor needs to be positive",
3081 return isl_pw_aff_free(pwaff
));
3082 pwaff
= isl_pw_aff_cow(pwaff
);
3088 for (i
= 0; i
< pwaff
->n
; ++i
) {
3089 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3090 if (!pwaff
->p
[i
].aff
)
3091 return isl_pw_aff_free(pwaff
);
3097 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3101 pwaff
= isl_pw_aff_cow(pwaff
);
3107 for (i
= 0; i
< pwaff
->n
; ++i
) {
3108 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3109 if (!pwaff
->p
[i
].aff
)
3110 return isl_pw_aff_free(pwaff
);
3116 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3120 pwaff
= isl_pw_aff_cow(pwaff
);
3126 for (i
= 0; i
< pwaff
->n
; ++i
) {
3127 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3128 if (!pwaff
->p
[i
].aff
)
3129 return isl_pw_aff_free(pwaff
);
3135 /* Assuming that "cond1" and "cond2" are disjoint,
3136 * return an affine expression that is equal to pwaff1 on cond1
3137 * and to pwaff2 on cond2.
3139 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3140 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3141 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3143 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3144 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3146 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3149 /* Return an affine expression that is equal to pwaff_true for elements
3150 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3152 * That is, return cond ? pwaff_true : pwaff_false;
3154 * If "cond" involves and NaN, then we conservatively return a NaN
3155 * on its entire domain. In principle, we could consider the pieces
3156 * where it is NaN separately from those where it is not.
3158 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3159 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3161 isl_set
*cond_true
, *cond_false
;
3165 if (isl_pw_aff_involves_nan(cond
)) {
3166 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3167 isl_local_space
*ls
= isl_local_space_from_space(space
);
3168 isl_pw_aff_free(cond
);
3169 isl_pw_aff_free(pwaff_true
);
3170 isl_pw_aff_free(pwaff_false
);
3171 return isl_pw_aff_nan_on_domain(ls
);
3174 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3175 cond_false
= isl_pw_aff_zero_set(cond
);
3176 return isl_pw_aff_select(cond_true
, pwaff_true
,
3177 cond_false
, pwaff_false
);
3179 isl_pw_aff_free(cond
);
3180 isl_pw_aff_free(pwaff_true
);
3181 isl_pw_aff_free(pwaff_false
);
3185 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3190 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3193 /* Check whether pwaff is a piecewise constant.
3195 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3202 for (i
= 0; i
< pwaff
->n
; ++i
) {
3203 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3204 if (is_cst
< 0 || !is_cst
)
3211 /* Return the product of "aff1" and "aff2".
3213 * If either of the two is NaN, then the result is NaN.
3215 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3217 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3218 __isl_take isl_aff
*aff2
)
3223 if (isl_aff_is_nan(aff1
)) {
3227 if (isl_aff_is_nan(aff2
)) {
3232 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3233 return isl_aff_mul(aff2
, aff1
);
3235 if (!isl_aff_is_cst(aff2
))
3236 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3237 "at least one affine expression should be constant",
3240 aff1
= isl_aff_cow(aff1
);
3244 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3245 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3255 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3257 * If either of the two is NaN, then the result is NaN.
3259 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3260 __isl_take isl_aff
*aff2
)
3268 if (isl_aff_is_nan(aff1
)) {
3272 if (isl_aff_is_nan(aff2
)) {
3277 is_cst
= isl_aff_is_cst(aff2
);
3281 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3282 "second argument should be a constant", goto error
);
3287 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3289 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3290 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3293 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3294 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3297 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3298 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3309 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3310 __isl_take isl_pw_aff
*pwaff2
)
3312 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3315 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3316 __isl_take isl_pw_aff
*pwaff2
)
3318 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3321 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3322 __isl_take isl_pw_aff
*pwaff2
)
3324 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3327 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3328 __isl_take isl_pw_aff
*pwaff2
)
3330 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3333 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3334 __isl_take isl_pw_aff
*pwaff2
)
3336 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3339 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3340 __isl_take isl_pw_aff
*pa2
)
3342 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3345 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3347 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3348 __isl_take isl_pw_aff
*pa2
)
3352 is_cst
= isl_pw_aff_is_cst(pa2
);
3356 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3357 "second argument should be a piecewise constant",
3359 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3361 isl_pw_aff_free(pa1
);
3362 isl_pw_aff_free(pa2
);
3366 /* Compute the quotient of the integer division of "pa1" by "pa2"
3367 * with rounding towards zero.
3368 * "pa2" is assumed to be a piecewise constant.
3370 * In particular, return
3372 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3375 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3376 __isl_take isl_pw_aff
*pa2
)
3382 is_cst
= isl_pw_aff_is_cst(pa2
);
3386 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3387 "second argument should be a piecewise constant",
3390 pa1
= isl_pw_aff_div(pa1
, pa2
);
3392 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3393 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3394 c
= isl_pw_aff_ceil(pa1
);
3395 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3397 isl_pw_aff_free(pa1
);
3398 isl_pw_aff_free(pa2
);
3402 /* Compute the remainder of the integer division of "pa1" by "pa2"
3403 * with rounding towards zero.
3404 * "pa2" is assumed to be a piecewise constant.
3406 * In particular, return
3408 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3411 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3412 __isl_take isl_pw_aff
*pa2
)
3417 is_cst
= isl_pw_aff_is_cst(pa2
);
3421 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3422 "second argument should be a piecewise constant",
3424 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3425 res
= isl_pw_aff_mul(pa2
, res
);
3426 res
= isl_pw_aff_sub(pa1
, res
);
3429 isl_pw_aff_free(pa1
);
3430 isl_pw_aff_free(pa2
);
3434 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3435 __isl_take isl_pw_aff
*pwaff2
)
3440 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3441 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3442 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3443 isl_pw_aff_copy(pwaff2
));
3444 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3445 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3448 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3449 __isl_take isl_pw_aff
*pwaff2
)
3451 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3454 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3455 __isl_take isl_pw_aff
*pwaff2
)
3460 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3461 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3462 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3463 isl_pw_aff_copy(pwaff2
));
3464 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3465 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3468 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3469 __isl_take isl_pw_aff
*pwaff2
)
3471 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3474 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3475 __isl_take isl_pw_aff_list
*list
,
3476 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3477 __isl_take isl_pw_aff
*pwaff2
))
3486 ctx
= isl_pw_aff_list_get_ctx(list
);
3488 isl_die(ctx
, isl_error_invalid
,
3489 "list should contain at least one element", goto error
);
3491 res
= isl_pw_aff_copy(list
->p
[0]);
3492 for (i
= 1; i
< list
->n
; ++i
)
3493 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3495 isl_pw_aff_list_free(list
);
3498 isl_pw_aff_list_free(list
);
3502 /* Return an isl_pw_aff that maps each element in the intersection of the
3503 * domains of the elements of list to the minimal corresponding affine
3506 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3508 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3511 /* Return an isl_pw_aff that maps each element in the intersection of the
3512 * domains of the elements of list to the maximal corresponding affine
3515 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3517 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3520 /* Mark the domains of "pwaff" as rational.
3522 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3526 pwaff
= isl_pw_aff_cow(pwaff
);
3532 for (i
= 0; i
< pwaff
->n
; ++i
) {
3533 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3534 if (!pwaff
->p
[i
].set
)
3535 return isl_pw_aff_free(pwaff
);
3541 /* Mark the domains of the elements of "list" as rational.
3543 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3544 __isl_take isl_pw_aff_list
*list
)
3554 for (i
= 0; i
< n
; ++i
) {
3557 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3558 pa
= isl_pw_aff_set_rational(pa
);
3559 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3565 /* Do the parameters of "aff" match those of "space"?
3567 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3568 __isl_keep isl_space
*space
)
3570 isl_space
*aff_space
;
3576 aff_space
= isl_aff_get_domain_space(aff
);
3578 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3580 isl_space_free(aff_space
);
3584 /* Check that the domain space of "aff" matches "space".
3586 * Return 0 on success and -1 on error.
3588 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3589 __isl_keep isl_space
*space
)
3591 isl_space
*aff_space
;
3597 aff_space
= isl_aff_get_domain_space(aff
);
3599 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3603 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3604 "parameters don't match", goto error
);
3605 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3606 aff_space
, isl_dim_set
);
3610 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3611 "domains don't match", goto error
);
3612 isl_space_free(aff_space
);
3615 isl_space_free(aff_space
);
3625 #include <isl_multi_templ.c>
3626 #include <isl_multi_apply_set.c>
3627 #include <isl_multi_gist.c>
3631 /* Remove any internal structure of the domain of "ma".
3632 * If there is any such internal structure in the input,
3633 * then the name of the corresponding space is also removed.
3635 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3636 __isl_take isl_multi_aff
*ma
)
3643 if (!ma
->space
->nested
[0])
3646 space
= isl_multi_aff_get_space(ma
);
3647 space
= isl_space_flatten_domain(space
);
3648 ma
= isl_multi_aff_reset_space(ma
, space
);
3653 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3654 * of the space to its domain.
3656 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3659 isl_local_space
*ls
;
3664 if (!isl_space_is_map(space
))
3665 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3666 "not a map space", goto error
);
3668 n_in
= isl_space_dim(space
, isl_dim_in
);
3669 space
= isl_space_domain_map(space
);
3671 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3673 isl_space_free(space
);
3677 space
= isl_space_domain(space
);
3678 ls
= isl_local_space_from_space(space
);
3679 for (i
= 0; i
< n_in
; ++i
) {
3682 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3684 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3686 isl_local_space_free(ls
);
3689 isl_space_free(space
);
3693 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3694 * of the space to its range.
3696 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3699 isl_local_space
*ls
;
3704 if (!isl_space_is_map(space
))
3705 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3706 "not a map space", goto error
);
3708 n_in
= isl_space_dim(space
, isl_dim_in
);
3709 n_out
= isl_space_dim(space
, isl_dim_out
);
3710 space
= isl_space_range_map(space
);
3712 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3714 isl_space_free(space
);
3718 space
= isl_space_domain(space
);
3719 ls
= isl_local_space_from_space(space
);
3720 for (i
= 0; i
< n_out
; ++i
) {
3723 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3724 isl_dim_set
, n_in
+ i
);
3725 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3727 isl_local_space_free(ls
);
3730 isl_space_free(space
);
3734 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3735 * of the space to its range.
3737 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_map(
3738 __isl_take isl_space
*space
)
3740 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space
));
3743 /* Given the space of a set and a range of set dimensions,
3744 * construct an isl_multi_aff that projects out those dimensions.
3746 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3747 __isl_take isl_space
*space
, enum isl_dim_type type
,
3748 unsigned first
, unsigned n
)
3751 isl_local_space
*ls
;
3756 if (!isl_space_is_set(space
))
3757 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3758 "expecting set space", goto error
);
3759 if (type
!= isl_dim_set
)
3760 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3761 "only set dimensions can be projected out", goto error
);
3763 dim
= isl_space_dim(space
, isl_dim_set
);
3764 if (first
+ n
> dim
)
3765 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3766 "range out of bounds", goto error
);
3768 space
= isl_space_from_domain(space
);
3769 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3772 return isl_multi_aff_alloc(space
);
3774 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3775 space
= isl_space_domain(space
);
3776 ls
= isl_local_space_from_space(space
);
3778 for (i
= 0; i
< first
; ++i
) {
3781 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3783 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3786 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3789 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3790 isl_dim_set
, first
+ n
+ i
);
3791 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3794 isl_local_space_free(ls
);
3797 isl_space_free(space
);
3801 /* Given the space of a set and a range of set dimensions,
3802 * construct an isl_pw_multi_aff that projects out those dimensions.
3804 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3805 __isl_take isl_space
*space
, enum isl_dim_type type
,
3806 unsigned first
, unsigned n
)
3810 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3811 return isl_pw_multi_aff_from_multi_aff(ma
);
3814 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3817 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3818 __isl_take isl_multi_aff
*ma
)
3820 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3821 return isl_pw_multi_aff_alloc(dom
, ma
);
3824 /* Create a piecewise multi-affine expression in the given space that maps each
3825 * input dimension to the corresponding output dimension.
3827 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3828 __isl_take isl_space
*space
)
3830 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3833 /* Add "ma2" to "ma1" and return the result.
3835 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3837 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3838 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3840 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3843 /* Add "ma2" to "ma1" and return the result.
3845 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3846 __isl_take isl_multi_aff
*ma2
)
3848 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3849 &isl_multi_aff_add_aligned
);
3852 /* Exploit the equalities in "eq" to simplify the affine expressions.
3854 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3855 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3859 maff
= isl_multi_aff_cow(maff
);
3863 for (i
= 0; i
< maff
->n
; ++i
) {
3864 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3865 isl_basic_set_copy(eq
));
3870 isl_basic_set_free(eq
);
3873 isl_basic_set_free(eq
);
3874 isl_multi_aff_free(maff
);
3878 /* Given f, return floor(f).
3880 __isl_give isl_multi_aff
*isl_multi_aff_floor(__isl_take isl_multi_aff
*ma
)
3884 ma
= isl_multi_aff_cow(ma
);
3888 for (i
= 0; i
< ma
->n
; ++i
) {
3889 ma
->p
[i
] = isl_aff_floor(ma
->p
[i
]);
3891 return isl_multi_aff_free(ma
);
3897 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3902 maff
= isl_multi_aff_cow(maff
);
3906 for (i
= 0; i
< maff
->n
; ++i
) {
3907 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3909 return isl_multi_aff_free(maff
);
3915 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3916 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3918 maff1
= isl_multi_aff_add(maff1
, maff2
);
3919 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3923 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3931 /* Return the set of domain elements where "ma1" is lexicographically
3932 * smaller than or equal to "ma2".
3934 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3935 __isl_take isl_multi_aff
*ma2
)
3937 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3940 /* Return the set of domain elements where "ma1" is lexicographically
3941 * greater than or equal to "ma2".
3943 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3944 __isl_take isl_multi_aff
*ma2
)
3947 isl_map
*map1
, *map2
;
3950 map1
= isl_map_from_multi_aff(ma1
);
3951 map2
= isl_map_from_multi_aff(ma2
);
3952 map
= isl_map_range_product(map1
, map2
);
3953 space
= isl_space_range(isl_map_get_space(map
));
3954 space
= isl_space_domain(isl_space_unwrap(space
));
3955 ge
= isl_map_lex_ge(space
);
3956 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3958 return isl_map_domain(map
);
3962 #define PW isl_pw_multi_aff
3964 #define EL isl_multi_aff
3966 #define EL_IS_ZERO is_empty
3970 #define IS_ZERO is_empty
3973 #undef DEFAULT_IS_ZERO
3974 #define DEFAULT_IS_ZERO 0
3979 #define NO_INVOLVES_DIMS
3980 #define NO_INSERT_DIMS
3984 #include <isl_pw_templ.c>
3989 #define UNION isl_union_pw_multi_aff
3991 #define PART isl_pw_multi_aff
3993 #define PARTS pw_multi_aff
3997 #include <isl_union_templ.c>
3999 /* Given a function "cmp" that returns the set of elements where
4000 * "ma1" is "better" than "ma2", return the intersection of this
4001 * set with "dom1" and "dom2".
4003 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
4004 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
4005 __isl_keep isl_multi_aff
*ma2
,
4006 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4007 __isl_take isl_multi_aff
*ma2
))
4013 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
4014 is_empty
= isl_set_plain_is_empty(common
);
4015 if (is_empty
>= 0 && is_empty
)
4018 return isl_set_free(common
);
4019 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
4020 better
= isl_set_intersect(common
, better
);
4025 /* Given a function "cmp" that returns the set of elements where
4026 * "ma1" is "better" than "ma2", return a piecewise multi affine
4027 * expression defined on the union of the definition domains
4028 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4029 * "pma2" on each cell. If only one of the two input functions
4030 * is defined on a given cell, then it is considered the best.
4032 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
4033 __isl_take isl_pw_multi_aff
*pma1
,
4034 __isl_take isl_pw_multi_aff
*pma2
,
4035 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4036 __isl_take isl_multi_aff
*ma2
))
4039 isl_pw_multi_aff
*res
= NULL
;
4041 isl_set
*set
= NULL
;
4046 ctx
= isl_space_get_ctx(pma1
->dim
);
4047 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
4048 isl_die(ctx
, isl_error_invalid
,
4049 "arguments should live in the same space", goto error
);
4051 if (isl_pw_multi_aff_is_empty(pma1
)) {
4052 isl_pw_multi_aff_free(pma1
);
4056 if (isl_pw_multi_aff_is_empty(pma2
)) {
4057 isl_pw_multi_aff_free(pma2
);
4061 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4062 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4064 for (i
= 0; i
< pma1
->n
; ++i
) {
4065 set
= isl_set_copy(pma1
->p
[i
].set
);
4066 for (j
= 0; j
< pma2
->n
; ++j
) {
4070 better
= shared_and_better(pma2
->p
[j
].set
,
4071 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4072 pma1
->p
[i
].maff
, cmp
);
4073 is_empty
= isl_set_plain_is_empty(better
);
4074 if (is_empty
< 0 || is_empty
) {
4075 isl_set_free(better
);
4080 set
= isl_set_subtract(set
, isl_set_copy(better
));
4082 res
= isl_pw_multi_aff_add_piece(res
, better
,
4083 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4085 res
= isl_pw_multi_aff_add_piece(res
, set
,
4086 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4089 for (j
= 0; j
< pma2
->n
; ++j
) {
4090 set
= isl_set_copy(pma2
->p
[j
].set
);
4091 for (i
= 0; i
< pma1
->n
; ++i
)
4092 set
= isl_set_subtract(set
,
4093 isl_set_copy(pma1
->p
[i
].set
));
4094 res
= isl_pw_multi_aff_add_piece(res
, set
,
4095 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4098 isl_pw_multi_aff_free(pma1
);
4099 isl_pw_multi_aff_free(pma2
);
4103 isl_pw_multi_aff_free(pma1
);
4104 isl_pw_multi_aff_free(pma2
);
4106 return isl_pw_multi_aff_free(res
);
4109 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4110 __isl_take isl_pw_multi_aff
*pma1
,
4111 __isl_take isl_pw_multi_aff
*pma2
)
4113 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4116 /* Given two piecewise multi affine expressions, return a piecewise
4117 * multi-affine expression defined on the union of the definition domains
4118 * of the inputs that is equal to the lexicographic maximum of the two
4119 * inputs on each cell. If only one of the two inputs is defined on
4120 * a given cell, then it is considered to be the maximum.
4122 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4123 __isl_take isl_pw_multi_aff
*pma1
,
4124 __isl_take isl_pw_multi_aff
*pma2
)
4126 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4127 &pw_multi_aff_union_lexmax
);
4130 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4131 __isl_take isl_pw_multi_aff
*pma1
,
4132 __isl_take isl_pw_multi_aff
*pma2
)
4134 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4137 /* Given two piecewise multi affine expressions, return a piecewise
4138 * multi-affine expression defined on the union of the definition domains
4139 * of the inputs that is equal to the lexicographic minimum of the two
4140 * inputs on each cell. If only one of the two inputs is defined on
4141 * a given cell, then it is considered to be the minimum.
4143 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4144 __isl_take isl_pw_multi_aff
*pma1
,
4145 __isl_take isl_pw_multi_aff
*pma2
)
4147 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4148 &pw_multi_aff_union_lexmin
);
4151 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4152 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4154 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4155 &isl_multi_aff_add
);
4158 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4159 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4161 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4165 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4166 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4168 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4169 &isl_multi_aff_sub
);
4172 /* Subtract "pma2" from "pma1" and return the result.
4174 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4175 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4177 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4181 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4182 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4184 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4187 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4188 * with the actual sum on the shared domain and
4189 * the defined expression on the symmetric difference of the domains.
4191 __isl_give isl_union_pw_aff
*isl_union_pw_aff_union_add(
4192 __isl_take isl_union_pw_aff
*upa1
, __isl_take isl_union_pw_aff
*upa2
)
4194 return isl_union_pw_aff_union_add_(upa1
, upa2
);
4197 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4198 * with the actual sum on the shared domain and
4199 * the defined expression on the symmetric difference of the domains.
4201 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4202 __isl_take isl_union_pw_multi_aff
*upma1
,
4203 __isl_take isl_union_pw_multi_aff
*upma2
)
4205 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4208 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4209 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4211 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4212 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4216 isl_pw_multi_aff
*res
;
4221 n
= pma1
->n
* pma2
->n
;
4222 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4223 isl_space_copy(pma2
->dim
));
4224 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4226 for (i
= 0; i
< pma1
->n
; ++i
) {
4227 for (j
= 0; j
< pma2
->n
; ++j
) {
4231 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4232 isl_set_copy(pma2
->p
[j
].set
));
4233 ma
= isl_multi_aff_product(
4234 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4235 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4236 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4240 isl_pw_multi_aff_free(pma1
);
4241 isl_pw_multi_aff_free(pma2
);
4244 isl_pw_multi_aff_free(pma1
);
4245 isl_pw_multi_aff_free(pma2
);
4249 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4250 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4252 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4253 &pw_multi_aff_product
);
4256 /* Construct a map mapping the domain of the piecewise multi-affine expression
4257 * to its range, with each dimension in the range equated to the
4258 * corresponding affine expression on its cell.
4260 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4268 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4270 for (i
= 0; i
< pma
->n
; ++i
) {
4271 isl_multi_aff
*maff
;
4272 isl_basic_map
*bmap
;
4275 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4276 bmap
= isl_basic_map_from_multi_aff(maff
);
4277 map_i
= isl_map_from_basic_map(bmap
);
4278 map_i
= isl_map_intersect_domain(map_i
,
4279 isl_set_copy(pma
->p
[i
].set
));
4280 map
= isl_map_union_disjoint(map
, map_i
);
4283 isl_pw_multi_aff_free(pma
);
4287 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4292 if (!isl_space_is_set(pma
->dim
))
4293 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4294 "isl_pw_multi_aff cannot be converted into an isl_set",
4297 return isl_map_from_pw_multi_aff(pma
);
4299 isl_pw_multi_aff_free(pma
);
4303 /* Given a basic map with a single output dimension that is defined
4304 * in terms of the parameters and input dimensions using an equality,
4305 * extract an isl_aff that expresses the output dimension in terms
4306 * of the parameters and input dimensions.
4307 * Note that this expression may involve integer divisions defined
4308 * in terms of parameters and input dimensions.
4310 * This function shares some similarities with
4311 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4313 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4314 __isl_take isl_basic_map
*bmap
)
4319 isl_local_space
*ls
;
4324 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
4325 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4326 "basic map should have a single output dimension",
4328 eq
= isl_basic_map_output_defining_equality(bmap
, 0);
4329 if (eq
>= bmap
->n_eq
)
4330 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4331 "unable to find suitable equality", goto error
);
4332 ls
= isl_basic_map_get_local_space(bmap
);
4333 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
4336 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
4337 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4338 if (isl_int_is_neg(bmap
->eq
[eq
][offset
])) {
4339 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4340 isl_seq_cpy(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4343 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4344 isl_seq_neg(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4347 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][offset
]);
4348 isl_basic_map_free(bmap
);
4350 aff
= isl_aff_remove_unused_divs(aff
);
4353 isl_basic_map_free(bmap
);
4357 /* Given a basic map where each output dimension is defined
4358 * in terms of the parameters and input dimensions using an equality,
4359 * extract an isl_multi_aff that expresses the output dimensions in terms
4360 * of the parameters and input dimensions.
4362 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4363 __isl_take isl_basic_map
*bmap
)
4372 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4373 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4375 for (i
= 0; i
< n_out
; ++i
) {
4376 isl_basic_map
*bmap_i
;
4379 bmap_i
= isl_basic_map_copy(bmap
);
4380 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4381 i
+ 1, n_out
- (1 + i
));
4382 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4383 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4384 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4387 isl_basic_map_free(bmap
);
4392 /* Given a basic set where each set dimension is defined
4393 * in terms of the parameters using an equality,
4394 * extract an isl_multi_aff that expresses the set dimensions in terms
4395 * of the parameters.
4397 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4398 __isl_take isl_basic_set
*bset
)
4400 return extract_isl_multi_aff_from_basic_map(bset
);
4403 /* Create an isl_pw_multi_aff that is equivalent to
4404 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4405 * The given basic map is such that each output dimension is defined
4406 * in terms of the parameters and input dimensions using an equality.
4408 * Since some applications expect the result of isl_pw_multi_aff_from_map
4409 * to only contain integer affine expressions, we compute the floor
4410 * of the expression before returning.
4412 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4413 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4417 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4418 ma
= isl_multi_aff_floor(ma
);
4419 return isl_pw_multi_aff_alloc(domain
, ma
);
4422 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4423 * This obviously only works if the input "map" is single-valued.
4424 * If so, we compute the lexicographic minimum of the image in the form
4425 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4426 * to its lexicographic minimum.
4427 * If the input is not single-valued, we produce an error.
4429 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4430 __isl_take isl_map
*map
)
4434 isl_pw_multi_aff
*pma
;
4436 sv
= isl_map_is_single_valued(map
);
4440 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4441 "map is not single-valued", goto error
);
4442 map
= isl_map_make_disjoint(map
);
4446 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4448 for (i
= 0; i
< map
->n
; ++i
) {
4449 isl_pw_multi_aff
*pma_i
;
4450 isl_basic_map
*bmap
;
4451 bmap
= isl_basic_map_copy(map
->p
[i
]);
4452 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4453 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4463 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4464 * taking into account that the output dimension at position "d"
4465 * can be represented as
4467 * x = floor((e(...) + c1) / m)
4469 * given that constraint "i" is of the form
4471 * e(...) + c1 - m x >= 0
4474 * Let "map" be of the form
4478 * We construct a mapping
4480 * A -> [A -> x = floor(...)]
4482 * apply that to the map, obtaining
4484 * [A -> x = floor(...)] -> B
4486 * and equate dimension "d" to x.
4487 * We then compute a isl_pw_multi_aff representation of the resulting map
4488 * and plug in the mapping above.
4490 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4491 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4495 isl_local_space
*ls
;
4503 isl_pw_multi_aff
*pma
;
4506 is_set
= isl_map_is_set(map
);
4508 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4509 ctx
= isl_map_get_ctx(map
);
4510 space
= isl_space_domain(isl_map_get_space(map
));
4511 n_in
= isl_space_dim(space
, isl_dim_set
);
4512 n
= isl_space_dim(space
, isl_dim_all
);
4514 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4516 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4517 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4519 isl_basic_map_free(hull
);
4521 ls
= isl_local_space_from_space(isl_space_copy(space
));
4522 aff
= isl_aff_alloc_vec(ls
, v
);
4523 aff
= isl_aff_floor(aff
);
4525 isl_space_free(space
);
4526 ma
= isl_multi_aff_from_aff(aff
);
4528 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4529 ma
= isl_multi_aff_range_product(ma
,
4530 isl_multi_aff_from_aff(aff
));
4533 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4534 map
= isl_map_apply_domain(map
, insert
);
4535 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4536 pma
= isl_pw_multi_aff_from_map(map
);
4537 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4542 /* Is constraint "c" of the form
4544 * e(...) + c1 - m x >= 0
4548 * -e(...) + c2 + m x >= 0
4550 * where m > 1 and e only depends on parameters and input dimemnsions?
4552 * "offset" is the offset of the output dimensions
4553 * "pos" is the position of output dimension x.
4555 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4557 if (isl_int_is_zero(c
[offset
+ d
]))
4559 if (isl_int_is_one(c
[offset
+ d
]))
4561 if (isl_int_is_negone(c
[offset
+ d
]))
4563 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4565 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4566 total
- (offset
+ d
+ 1)) != -1)
4571 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4573 * As a special case, we first check if there is any pair of constraints,
4574 * shared by all the basic maps in "map" that force a given dimension
4575 * to be equal to the floor of some affine combination of the input dimensions.
4577 * In particular, if we can find two constraints
4579 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4583 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4585 * where m > 1 and e only depends on parameters and input dimemnsions,
4588 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4590 * then we know that we can take
4592 * x = floor((e(...) + c1) / m)
4594 * without having to perform any computation.
4596 * Note that we know that
4600 * If c1 + c2 were 0, then we would have detected an equality during
4601 * simplification. If c1 + c2 were negative, then we would have detected
4604 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4605 __isl_take isl_map
*map
)
4611 isl_basic_map
*hull
;
4613 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4618 dim
= isl_map_dim(map
, isl_dim_out
);
4619 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4620 total
= 1 + isl_basic_map_total_dim(hull
);
4622 for (d
= 0; d
< dim
; ++d
) {
4623 for (i
= 0; i
< n
; ++i
) {
4624 if (!is_potential_div_constraint(hull
->ineq
[i
],
4627 for (j
= i
+ 1; j
< n
; ++j
) {
4628 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4629 hull
->ineq
[j
] + 1, total
- 1))
4631 isl_int_add(sum
, hull
->ineq
[i
][0],
4633 if (isl_int_abs_lt(sum
,
4634 hull
->ineq
[i
][offset
+ d
]))
4641 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4643 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4647 isl_basic_map_free(hull
);
4648 return pw_multi_aff_from_map_base(map
);
4651 isl_basic_map_free(hull
);
4655 /* Given an affine expression
4657 * [A -> B] -> f(A,B)
4659 * construct an isl_multi_aff
4663 * such that dimension "d" in B' is set to "aff" and the remaining
4664 * dimensions are set equal to the corresponding dimensions in B.
4665 * "n_in" is the dimension of the space A.
4666 * "n_out" is the dimension of the space B.
4668 * If "is_set" is set, then the affine expression is of the form
4672 * and we construct an isl_multi_aff
4676 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4677 unsigned n_in
, unsigned n_out
, int is_set
)
4681 isl_space
*space
, *space2
;
4682 isl_local_space
*ls
;
4684 space
= isl_aff_get_domain_space(aff
);
4685 ls
= isl_local_space_from_space(isl_space_copy(space
));
4686 space2
= isl_space_copy(space
);
4688 space2
= isl_space_range(isl_space_unwrap(space2
));
4689 space
= isl_space_map_from_domain_and_range(space
, space2
);
4690 ma
= isl_multi_aff_alloc(space
);
4691 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4693 for (i
= 0; i
< n_out
; ++i
) {
4696 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4697 isl_dim_set
, n_in
+ i
);
4698 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4701 isl_local_space_free(ls
);
4706 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4707 * taking into account that the dimension at position "d" can be written as
4709 * x = m a + f(..) (1)
4711 * where m is equal to "gcd".
4712 * "i" is the index of the equality in "hull" that defines f(..).
4713 * In particular, the equality is of the form
4715 * f(..) - x + m g(existentials) = 0
4719 * -f(..) + x + m g(existentials) = 0
4721 * We basically plug (1) into "map", resulting in a map with "a"
4722 * in the range instead of "x". The corresponding isl_pw_multi_aff
4723 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4725 * Specifically, given the input map
4729 * We first wrap it into a set
4733 * and define (1) on top of the corresponding space, resulting in "aff".
4734 * We use this to create an isl_multi_aff that maps the output position "d"
4735 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4736 * We plug this into the wrapped map, unwrap the result and compute the
4737 * corresponding isl_pw_multi_aff.
4738 * The result is an expression
4746 * so that we can plug that into "aff", after extending the latter to
4752 * If "map" is actually a set, then there is no "A" space, meaning
4753 * that we do not need to perform any wrapping, and that the result
4754 * of the recursive call is of the form
4758 * which is plugged into a mapping of the form
4762 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4763 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4768 isl_local_space
*ls
;
4771 isl_pw_multi_aff
*pma
, *id
;
4777 is_set
= isl_map_is_set(map
);
4779 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4780 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4781 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4786 set
= isl_map_wrap(map
);
4787 space
= isl_space_map_from_set(isl_set_get_space(set
));
4788 ma
= isl_multi_aff_identity(space
);
4789 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4790 aff
= isl_aff_alloc(ls
);
4792 isl_int_set_si(aff
->v
->el
[0], 1);
4793 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4794 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4797 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4799 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4801 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4802 set
= isl_set_preimage_multi_aff(set
, ma
);
4804 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4809 map
= isl_set_unwrap(set
);
4810 pma
= isl_pw_multi_aff_from_map(set
);
4813 space
= isl_pw_multi_aff_get_domain_space(pma
);
4814 space
= isl_space_map_from_set(space
);
4815 id
= isl_pw_multi_aff_identity(space
);
4816 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4818 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4819 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4821 isl_basic_map_free(hull
);
4825 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4827 * As a special case, we first check if all output dimensions are uniquely
4828 * defined in terms of the parameters and input dimensions over the entire
4829 * domain. If so, we extract the desired isl_pw_multi_aff directly
4830 * from the affine hull of "map" and its domain.
4832 * Otherwise, we check if any of the output dimensions is "strided".
4833 * That is, we check if can be written as
4837 * with m greater than 1, a some combination of existentiall quantified
4838 * variables and f and expression in the parameters and input dimensions.
4839 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4841 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4844 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4848 isl_basic_map
*hull
;
4858 hull
= isl_map_affine_hull(isl_map_copy(map
));
4859 sv
= isl_basic_map_plain_is_single_valued(hull
);
4861 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4863 hull
= isl_basic_map_free(hull
);
4867 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4868 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4871 isl_basic_map_free(hull
);
4872 return pw_multi_aff_from_map_check_div(map
);
4877 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4878 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4880 for (i
= 0; i
< n_out
; ++i
) {
4881 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4882 isl_int
*eq
= hull
->eq
[j
];
4883 isl_pw_multi_aff
*res
;
4885 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4886 !isl_int_is_negone(eq
[o_out
+ i
]))
4888 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4890 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4891 n_out
- (i
+ 1)) != -1)
4893 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4894 if (isl_int_is_zero(gcd
))
4896 if (isl_int_is_one(gcd
))
4899 res
= pw_multi_aff_from_map_stride(map
, hull
,
4907 isl_basic_map_free(hull
);
4908 return pw_multi_aff_from_map_check_div(map
);
4914 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4916 return isl_pw_multi_aff_from_map(set
);
4919 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4922 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4924 isl_union_pw_multi_aff
**upma
= user
;
4925 isl_pw_multi_aff
*pma
;
4927 pma
= isl_pw_multi_aff_from_map(map
);
4928 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4930 return *upma
? 0 : -1;
4933 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
4936 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_aff(
4937 __isl_take isl_aff
*aff
)
4940 isl_pw_multi_aff
*pma
;
4942 ma
= isl_multi_aff_from_aff(aff
);
4943 pma
= isl_pw_multi_aff_from_multi_aff(ma
);
4944 return isl_union_pw_multi_aff_from_pw_multi_aff(pma
);
4947 /* Try and create an isl_union_pw_multi_aff that is equivalent
4948 * to the given isl_union_map.
4949 * The isl_union_map is required to be single-valued in each space.
4950 * Otherwise, an error is produced.
4952 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4953 __isl_take isl_union_map
*umap
)
4956 isl_union_pw_multi_aff
*upma
;
4958 space
= isl_union_map_get_space(umap
);
4959 upma
= isl_union_pw_multi_aff_empty(space
);
4960 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4961 upma
= isl_union_pw_multi_aff_free(upma
);
4962 isl_union_map_free(umap
);
4967 /* Try and create an isl_union_pw_multi_aff that is equivalent
4968 * to the given isl_union_set.
4969 * The isl_union_set is required to be a singleton in each space.
4970 * Otherwise, an error is produced.
4972 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4973 __isl_take isl_union_set
*uset
)
4975 return isl_union_pw_multi_aff_from_union_map(uset
);
4978 /* Return the piecewise affine expression "set ? 1 : 0".
4980 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4983 isl_space
*space
= isl_set_get_space(set
);
4984 isl_local_space
*ls
= isl_local_space_from_space(space
);
4985 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4986 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4988 one
= isl_aff_add_constant_si(one
, 1);
4989 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4990 set
= isl_set_complement(set
);
4991 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4996 /* Plug in "subs" for dimension "type", "pos" of "aff".
4998 * Let i be the dimension to replace and let "subs" be of the form
5002 * and "aff" of the form
5008 * (a f + d g')/(m d)
5010 * where g' is the result of plugging in "subs" in each of the integer
5013 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
5014 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
5019 aff
= isl_aff_cow(aff
);
5021 return isl_aff_free(aff
);
5023 ctx
= isl_aff_get_ctx(aff
);
5024 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
5025 isl_die(ctx
, isl_error_invalid
,
5026 "spaces don't match", return isl_aff_free(aff
));
5027 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
5028 isl_die(ctx
, isl_error_unsupported
,
5029 "cannot handle divs yet", return isl_aff_free(aff
));
5031 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5033 return isl_aff_free(aff
);
5035 aff
->v
= isl_vec_cow(aff
->v
);
5037 return isl_aff_free(aff
);
5039 pos
+= isl_local_space_offset(aff
->ls
, type
);
5042 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5043 aff
->v
->size
, subs
->v
->size
, v
);
5049 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5050 * expressions in "maff".
5052 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5053 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5054 __isl_keep isl_aff
*subs
)
5058 maff
= isl_multi_aff_cow(maff
);
5060 return isl_multi_aff_free(maff
);
5062 if (type
== isl_dim_in
)
5065 for (i
= 0; i
< maff
->n
; ++i
) {
5066 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5068 return isl_multi_aff_free(maff
);
5074 /* Plug in "subs" for dimension "type", "pos" of "pma".
5076 * pma is of the form
5080 * while subs is of the form
5082 * v' = B_j(v) -> S_j
5084 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5085 * has a contribution in the result, in particular
5087 * C_ij(S_j) -> M_i(S_j)
5089 * Note that plugging in S_j in C_ij may also result in an empty set
5090 * and this contribution should simply be discarded.
5092 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5093 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5094 __isl_keep isl_pw_aff
*subs
)
5097 isl_pw_multi_aff
*res
;
5100 return isl_pw_multi_aff_free(pma
);
5102 n
= pma
->n
* subs
->n
;
5103 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5105 for (i
= 0; i
< pma
->n
; ++i
) {
5106 for (j
= 0; j
< subs
->n
; ++j
) {
5108 isl_multi_aff
*res_ij
;
5111 common
= isl_set_intersect(
5112 isl_set_copy(pma
->p
[i
].set
),
5113 isl_set_copy(subs
->p
[j
].set
));
5114 common
= isl_set_substitute(common
,
5115 type
, pos
, subs
->p
[j
].aff
);
5116 empty
= isl_set_plain_is_empty(common
);
5117 if (empty
< 0 || empty
) {
5118 isl_set_free(common
);
5124 res_ij
= isl_multi_aff_substitute(
5125 isl_multi_aff_copy(pma
->p
[i
].maff
),
5126 type
, pos
, subs
->p
[j
].aff
);
5128 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5132 isl_pw_multi_aff_free(pma
);
5135 isl_pw_multi_aff_free(pma
);
5136 isl_pw_multi_aff_free(res
);
5140 /* Compute the preimage of a range of dimensions in the affine expression "src"
5141 * under "ma" and put the result in "dst". The number of dimensions in "src"
5142 * that precede the range is given by "n_before". The number of dimensions
5143 * in the range is given by the number of output dimensions of "ma".
5144 * The number of dimensions that follow the range is given by "n_after".
5145 * If "has_denom" is set (to one),
5146 * then "src" and "dst" have an extra initial denominator.
5147 * "n_div_ma" is the number of existentials in "ma"
5148 * "n_div_bset" is the number of existentials in "src"
5149 * The resulting "dst" (which is assumed to have been allocated by
5150 * the caller) contains coefficients for both sets of existentials,
5151 * first those in "ma" and then those in "src".
5152 * f, c1, c2 and g are temporary objects that have been initialized
5155 * Let src represent the expression
5157 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5159 * and let ma represent the expressions
5161 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5163 * We start out with the following expression for dst:
5165 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5167 * with the multiplication factor f initially equal to 1
5168 * and f \sum_i b_i v_i kept separately.
5169 * For each x_i that we substitute, we multiply the numerator
5170 * (and denominator) of dst by c_1 = m_i and add the numerator
5171 * of the x_i expression multiplied by c_2 = f b_i,
5172 * after removing the common factors of c_1 and c_2.
5173 * The multiplication factor f also needs to be multiplied by c_1
5174 * for the next x_j, j > i.
5176 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5177 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5178 int n_div_ma
, int n_div_bmap
,
5179 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5182 int n_param
, n_in
, n_out
;
5185 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5186 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5187 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5189 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5190 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5191 isl_seq_clr(dst
+ o_dst
, n_in
);
5194 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5197 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5199 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5201 isl_int_set_si(f
, 1);
5203 for (i
= 0; i
< n_out
; ++i
) {
5204 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5206 if (isl_int_is_zero(src
[offset
]))
5208 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5209 isl_int_mul(c2
, f
, src
[offset
]);
5210 isl_int_gcd(g
, c1
, c2
);
5211 isl_int_divexact(c1
, c1
, g
);
5212 isl_int_divexact(c2
, c2
, g
);
5214 isl_int_mul(f
, f
, c1
);
5217 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5218 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5219 o_dst
+= 1 + n_param
;
5220 o_src
+= 1 + n_param
;
5221 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5223 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5224 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5227 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5229 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5230 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5233 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5235 isl_int_mul(dst
[0], dst
[0], c1
);
5239 /* Compute the pullback of "aff" by the function represented by "ma".
5240 * In other words, plug in "ma" in "aff". The result is an affine expression
5241 * defined over the domain space of "ma".
5243 * If "aff" is represented by
5245 * (a(p) + b x + c(divs))/d
5247 * and ma is represented by
5249 * x = D(p) + F(y) + G(divs')
5251 * then the result is
5253 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5255 * The divs in the local space of the input are similarly adjusted
5256 * through a call to isl_local_space_preimage_multi_aff.
5258 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5259 __isl_take isl_multi_aff
*ma
)
5261 isl_aff
*res
= NULL
;
5262 isl_local_space
*ls
;
5263 int n_div_aff
, n_div_ma
;
5264 isl_int f
, c1
, c2
, g
;
5266 ma
= isl_multi_aff_align_divs(ma
);
5270 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5271 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5273 ls
= isl_aff_get_domain_local_space(aff
);
5274 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5275 res
= isl_aff_alloc(ls
);
5284 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5293 isl_multi_aff_free(ma
);
5294 res
= isl_aff_normalize(res
);
5298 isl_multi_aff_free(ma
);
5303 /* Compute the pullback of "aff1" by the function represented by "aff2".
5304 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5305 * defined over the domain space of "aff1".
5307 * The domain of "aff1" should match the range of "aff2", which means
5308 * that it should be single-dimensional.
5310 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5311 __isl_take isl_aff
*aff2
)
5315 ma
= isl_multi_aff_from_aff(aff2
);
5316 return isl_aff_pullback_multi_aff(aff1
, ma
);
5319 /* Compute the pullback of "ma1" by the function represented by "ma2".
5320 * In other words, plug in "ma2" in "ma1".
5322 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5324 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5325 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5328 isl_space
*space
= NULL
;
5330 ma2
= isl_multi_aff_align_divs(ma2
);
5331 ma1
= isl_multi_aff_cow(ma1
);
5335 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5336 isl_multi_aff_get_space(ma1
));
5338 for (i
= 0; i
< ma1
->n
; ++i
) {
5339 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5340 isl_multi_aff_copy(ma2
));
5345 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5346 isl_multi_aff_free(ma2
);
5349 isl_space_free(space
);
5350 isl_multi_aff_free(ma2
);
5351 isl_multi_aff_free(ma1
);
5355 /* Compute the pullback of "ma1" by the function represented by "ma2".
5356 * In other words, plug in "ma2" in "ma1".
5358 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5359 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5361 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5362 &isl_multi_aff_pullback_multi_aff_aligned
);
5365 /* Extend the local space of "dst" to include the divs
5366 * in the local space of "src".
5368 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5369 __isl_keep isl_aff
*src
)
5377 return isl_aff_free(dst
);
5379 ctx
= isl_aff_get_ctx(src
);
5380 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5381 isl_die(ctx
, isl_error_invalid
,
5382 "spaces don't match", goto error
);
5384 if (src
->ls
->div
->n_row
== 0)
5387 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5388 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5389 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5392 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5393 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5401 return isl_aff_free(dst
);
5404 /* Adjust the local spaces of the affine expressions in "maff"
5405 * such that they all have the save divs.
5407 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5408 __isl_take isl_multi_aff
*maff
)
5416 maff
= isl_multi_aff_cow(maff
);
5420 for (i
= 1; i
< maff
->n
; ++i
)
5421 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5422 for (i
= 1; i
< maff
->n
; ++i
) {
5423 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5425 return isl_multi_aff_free(maff
);
5431 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5433 aff
= isl_aff_cow(aff
);
5437 aff
->ls
= isl_local_space_lift(aff
->ls
);
5439 return isl_aff_free(aff
);
5444 /* Lift "maff" to a space with extra dimensions such that the result
5445 * has no more existentially quantified variables.
5446 * If "ls" is not NULL, then *ls is assigned the local space that lies
5447 * at the basis of the lifting applied to "maff".
5449 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5450 __isl_give isl_local_space
**ls
)
5464 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5465 *ls
= isl_local_space_from_space(space
);
5467 return isl_multi_aff_free(maff
);
5472 maff
= isl_multi_aff_cow(maff
);
5473 maff
= isl_multi_aff_align_divs(maff
);
5477 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5478 space
= isl_multi_aff_get_space(maff
);
5479 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5480 space
= isl_space_extend_domain_with_range(space
,
5481 isl_multi_aff_get_space(maff
));
5483 return isl_multi_aff_free(maff
);
5484 isl_space_free(maff
->space
);
5485 maff
->space
= space
;
5488 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5490 return isl_multi_aff_free(maff
);
5493 for (i
= 0; i
< maff
->n
; ++i
) {
5494 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5502 isl_local_space_free(*ls
);
5503 return isl_multi_aff_free(maff
);
5507 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5509 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5510 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5520 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5521 if (pos
< 0 || pos
>= n_out
)
5522 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5523 "index out of bounds", return NULL
);
5525 space
= isl_pw_multi_aff_get_space(pma
);
5526 space
= isl_space_drop_dims(space
, isl_dim_out
,
5527 pos
+ 1, n_out
- pos
- 1);
5528 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5530 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5531 for (i
= 0; i
< pma
->n
; ++i
) {
5533 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5534 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5540 /* Return an isl_pw_multi_aff with the given "set" as domain and
5541 * an unnamed zero-dimensional range.
5543 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5544 __isl_take isl_set
*set
)
5549 space
= isl_set_get_space(set
);
5550 space
= isl_space_from_domain(space
);
5551 ma
= isl_multi_aff_zero(space
);
5552 return isl_pw_multi_aff_alloc(set
, ma
);
5555 /* Add an isl_pw_multi_aff with the given "set" as domain and
5556 * an unnamed zero-dimensional range to *user.
5558 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5560 isl_union_pw_multi_aff
**upma
= user
;
5561 isl_pw_multi_aff
*pma
;
5563 pma
= isl_pw_multi_aff_from_domain(set
);
5564 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5569 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5570 * an unnamed zero-dimensional range.
5572 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5573 __isl_take isl_union_set
*uset
)
5576 isl_union_pw_multi_aff
*upma
;
5581 space
= isl_union_set_get_space(uset
);
5582 upma
= isl_union_pw_multi_aff_empty(space
);
5584 if (isl_union_set_foreach_set(uset
,
5585 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5588 isl_union_set_free(uset
);
5591 isl_union_set_free(uset
);
5592 isl_union_pw_multi_aff_free(upma
);
5596 /* Convert "pma" to an isl_map and add it to *umap.
5598 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5600 isl_union_map
**umap
= user
;
5603 map
= isl_map_from_pw_multi_aff(pma
);
5604 *umap
= isl_union_map_add_map(*umap
, map
);
5609 /* Construct a union map mapping the domain of the union
5610 * piecewise multi-affine expression to its range, with each dimension
5611 * in the range equated to the corresponding affine expression on its cell.
5613 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5614 __isl_take isl_union_pw_multi_aff
*upma
)
5617 isl_union_map
*umap
;
5622 space
= isl_union_pw_multi_aff_get_space(upma
);
5623 umap
= isl_union_map_empty(space
);
5625 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5626 &map_from_pw_multi_aff
, &umap
) < 0)
5629 isl_union_pw_multi_aff_free(upma
);
5632 isl_union_pw_multi_aff_free(upma
);
5633 isl_union_map_free(umap
);
5637 /* Local data for bin_entry and the callback "fn".
5639 struct isl_union_pw_multi_aff_bin_data
{
5640 isl_union_pw_multi_aff
*upma2
;
5641 isl_union_pw_multi_aff
*res
;
5642 isl_pw_multi_aff
*pma
;
5643 int (*fn
)(void **entry
, void *user
);
5646 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5647 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5649 static int bin_entry(void **entry
, void *user
)
5651 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5652 isl_pw_multi_aff
*pma
= *entry
;
5655 if (isl_hash_table_foreach(data
->upma2
->space
->ctx
, &data
->upma2
->table
,
5656 data
->fn
, data
) < 0)
5662 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5663 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5664 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5665 * as *entry. The callback should adjust data->res if desired.
5667 static __isl_give isl_union_pw_multi_aff
*bin_op(
5668 __isl_take isl_union_pw_multi_aff
*upma1
,
5669 __isl_take isl_union_pw_multi_aff
*upma2
,
5670 int (*fn
)(void **entry
, void *user
))
5673 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5675 space
= isl_union_pw_multi_aff_get_space(upma2
);
5676 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5677 space
= isl_union_pw_multi_aff_get_space(upma1
);
5678 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5680 if (!upma1
|| !upma2
)
5684 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->space
),
5686 if (isl_hash_table_foreach(upma1
->space
->ctx
, &upma1
->table
,
5687 &bin_entry
, &data
) < 0)
5690 isl_union_pw_multi_aff_free(upma1
);
5691 isl_union_pw_multi_aff_free(upma2
);
5694 isl_union_pw_multi_aff_free(upma1
);
5695 isl_union_pw_multi_aff_free(upma2
);
5696 isl_union_pw_multi_aff_free(data
.res
);
5700 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5701 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5703 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5704 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5708 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5709 isl_pw_multi_aff_get_space(pma2
));
5710 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5711 &isl_multi_aff_range_product
);
5714 /* Given two isl_pw_multi_affs A -> B and C -> D,
5715 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5717 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5718 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5720 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5721 &pw_multi_aff_range_product
);
5724 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5725 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5727 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5728 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5732 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5733 isl_pw_multi_aff_get_space(pma2
));
5734 space
= isl_space_flatten_range(space
);
5735 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5736 &isl_multi_aff_flat_range_product
);
5739 /* Given two isl_pw_multi_affs A -> B and C -> D,
5740 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5742 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5743 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5745 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5746 &pw_multi_aff_flat_range_product
);
5749 /* If data->pma and *entry have the same domain space, then compute
5750 * their flat range product and the result to data->res.
5752 static int flat_range_product_entry(void **entry
, void *user
)
5754 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5755 isl_pw_multi_aff
*pma2
= *entry
;
5757 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5758 pma2
->dim
, isl_dim_in
))
5761 pma2
= isl_pw_multi_aff_flat_range_product(
5762 isl_pw_multi_aff_copy(data
->pma
),
5763 isl_pw_multi_aff_copy(pma2
));
5765 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5770 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5771 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5773 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5774 __isl_take isl_union_pw_multi_aff
*upma1
,
5775 __isl_take isl_union_pw_multi_aff
*upma2
)
5777 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5780 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5781 * The parameters are assumed to have been aligned.
5783 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5784 * except that it works on two different isl_pw_* types.
5786 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5787 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5788 __isl_take isl_pw_aff
*pa
)
5791 isl_pw_multi_aff
*res
= NULL
;
5796 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5797 pa
->dim
, isl_dim_in
))
5798 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5799 "domains don't match", goto error
);
5800 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5801 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5802 "index out of bounds", goto error
);
5805 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5807 for (i
= 0; i
< pma
->n
; ++i
) {
5808 for (j
= 0; j
< pa
->n
; ++j
) {
5810 isl_multi_aff
*res_ij
;
5813 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5814 isl_set_copy(pa
->p
[j
].set
));
5815 empty
= isl_set_plain_is_empty(common
);
5816 if (empty
< 0 || empty
) {
5817 isl_set_free(common
);
5823 res_ij
= isl_multi_aff_set_aff(
5824 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5825 isl_aff_copy(pa
->p
[j
].aff
));
5826 res_ij
= isl_multi_aff_gist(res_ij
,
5827 isl_set_copy(common
));
5829 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5833 isl_pw_multi_aff_free(pma
);
5834 isl_pw_aff_free(pa
);
5837 isl_pw_multi_aff_free(pma
);
5838 isl_pw_aff_free(pa
);
5839 return isl_pw_multi_aff_free(res
);
5842 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5844 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5845 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5846 __isl_take isl_pw_aff
*pa
)
5850 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5851 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5852 if (!isl_space_has_named_params(pma
->dim
) ||
5853 !isl_space_has_named_params(pa
->dim
))
5854 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5855 "unaligned unnamed parameters", goto error
);
5856 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5857 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5858 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5860 isl_pw_multi_aff_free(pma
);
5861 isl_pw_aff_free(pa
);
5865 /* Do the parameters of "pa" match those of "space"?
5867 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5868 __isl_keep isl_space
*space
)
5870 isl_space
*pa_space
;
5876 pa_space
= isl_pw_aff_get_space(pa
);
5878 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5880 isl_space_free(pa_space
);
5884 /* Check that the domain space of "pa" matches "space".
5886 * Return 0 on success and -1 on error.
5888 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5889 __isl_keep isl_space
*space
)
5891 isl_space
*pa_space
;
5897 pa_space
= isl_pw_aff_get_space(pa
);
5899 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5903 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5904 "parameters don't match", goto error
);
5905 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
5906 pa_space
, isl_dim_in
);
5910 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5911 "domains don't match", goto error
);
5912 isl_space_free(pa_space
);
5915 isl_space_free(pa_space
);
5924 #include <isl_multi_templ.c>
5925 #include <isl_multi_apply_set.c>
5926 #include <isl_multi_gist.c>
5927 #include <isl_multi_intersect.c>
5929 /* Scale the elements of "pma" by the corresponding elements of "mv".
5931 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5932 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5936 pma
= isl_pw_multi_aff_cow(pma
);
5939 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5940 mv
->space
, isl_dim_set
))
5941 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5942 "spaces don't match", goto error
);
5943 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5944 mv
->space
, isl_dim_param
)) {
5945 pma
= isl_pw_multi_aff_align_params(pma
,
5946 isl_multi_val_get_space(mv
));
5947 mv
= isl_multi_val_align_params(mv
,
5948 isl_pw_multi_aff_get_space(pma
));
5953 for (i
= 0; i
< pma
->n
; ++i
) {
5954 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5955 isl_multi_val_copy(mv
));
5956 if (!pma
->p
[i
].maff
)
5960 isl_multi_val_free(mv
);
5963 isl_multi_val_free(mv
);
5964 isl_pw_multi_aff_free(pma
);
5968 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5969 * mv contains the mv argument.
5970 * res collects the results.
5972 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5974 isl_union_pw_multi_aff
*res
;
5977 /* This function is called for each entry of an isl_union_pw_multi_aff.
5978 * If the space of the entry matches that of data->mv,
5979 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5982 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5984 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5985 isl_pw_multi_aff
*pma
= *entry
;
5989 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5990 data
->mv
->space
, isl_dim_set
))
5993 pma
= isl_pw_multi_aff_copy(pma
);
5994 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5995 isl_multi_val_copy(data
->mv
));
5996 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
6003 /* Scale the elements of "upma" by the corresponding elements of "mv",
6004 * for those entries that match the space of "mv".
6006 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
6007 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
6009 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
6011 upma
= isl_union_pw_multi_aff_align_params(upma
,
6012 isl_multi_val_get_space(mv
));
6013 mv
= isl_multi_val_align_params(mv
,
6014 isl_union_pw_multi_aff_get_space(upma
));
6019 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->space
),
6021 if (isl_hash_table_foreach(upma
->space
->ctx
, &upma
->table
,
6022 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
6025 isl_multi_val_free(mv
);
6026 isl_union_pw_multi_aff_free(upma
);
6029 isl_multi_val_free(mv
);
6030 isl_union_pw_multi_aff_free(upma
);
6034 /* Construct and return a piecewise multi affine expression
6035 * in the given space with value zero in each of the output dimensions and
6036 * a universe domain.
6038 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6040 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6043 /* Construct and return a piecewise multi affine expression
6044 * that is equal to the given piecewise affine expression.
6046 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6047 __isl_take isl_pw_aff
*pa
)
6051 isl_pw_multi_aff
*pma
;
6056 space
= isl_pw_aff_get_space(pa
);
6057 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6059 for (i
= 0; i
< pa
->n
; ++i
) {
6063 set
= isl_set_copy(pa
->p
[i
].set
);
6064 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6065 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6068 isl_pw_aff_free(pa
);
6072 /* Construct a set or map mapping the shared (parameter) domain
6073 * of the piecewise affine expressions to the range of "mpa"
6074 * with each dimension in the range equated to the
6075 * corresponding piecewise affine expression.
6077 static __isl_give isl_map
*map_from_multi_pw_aff(
6078 __isl_take isl_multi_pw_aff
*mpa
)
6087 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6088 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6089 "invalid space", goto error
);
6091 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6092 map
= isl_map_universe(isl_space_from_domain(space
));
6094 for (i
= 0; i
< mpa
->n
; ++i
) {
6098 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6099 map_i
= map_from_pw_aff(pa
);
6101 map
= isl_map_flat_range_product(map
, map_i
);
6104 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6106 isl_multi_pw_aff_free(mpa
);
6109 isl_multi_pw_aff_free(mpa
);
6113 /* Construct a map mapping the shared domain
6114 * of the piecewise affine expressions to the range of "mpa"
6115 * with each dimension in the range equated to the
6116 * corresponding piecewise affine expression.
6118 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6122 if (isl_space_is_set(mpa
->space
))
6123 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6124 "space of input is not a map", goto error
);
6126 return map_from_multi_pw_aff(mpa
);
6128 isl_multi_pw_aff_free(mpa
);
6132 /* Construct a set mapping the shared parameter domain
6133 * of the piecewise affine expressions to the space of "mpa"
6134 * with each dimension in the range equated to the
6135 * corresponding piecewise affine expression.
6137 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6141 if (!isl_space_is_set(mpa
->space
))
6142 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6143 "space of input is not a set", goto error
);
6145 return map_from_multi_pw_aff(mpa
);
6147 isl_multi_pw_aff_free(mpa
);
6151 /* Construct and return a piecewise multi affine expression
6152 * that is equal to the given multi piecewise affine expression
6153 * on the shared domain of the piecewise affine expressions.
6155 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6156 __isl_take isl_multi_pw_aff
*mpa
)
6161 isl_pw_multi_aff
*pma
;
6166 space
= isl_multi_pw_aff_get_space(mpa
);
6169 isl_multi_pw_aff_free(mpa
);
6170 return isl_pw_multi_aff_zero(space
);
6173 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6174 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6176 for (i
= 1; i
< mpa
->n
; ++i
) {
6177 isl_pw_multi_aff
*pma_i
;
6179 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6180 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6181 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6184 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6186 isl_multi_pw_aff_free(mpa
);
6190 /* Construct and return a multi piecewise affine expression
6191 * that is equal to the given multi affine expression.
6193 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6194 __isl_take isl_multi_aff
*ma
)
6197 isl_multi_pw_aff
*mpa
;
6202 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6203 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6205 for (i
= 0; i
< n
; ++i
) {
6208 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6209 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6212 isl_multi_aff_free(ma
);
6216 /* Construct and return a multi piecewise affine expression
6217 * that is equal to the given piecewise multi affine expression.
6219 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6220 __isl_take isl_pw_multi_aff
*pma
)
6224 isl_multi_pw_aff
*mpa
;
6229 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6230 space
= isl_pw_multi_aff_get_space(pma
);
6231 mpa
= isl_multi_pw_aff_alloc(space
);
6233 for (i
= 0; i
< n
; ++i
) {
6236 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6237 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6240 isl_pw_multi_aff_free(pma
);
6244 /* Do "pa1" and "pa2" represent the same function?
6246 * We first check if they are obviously equal.
6247 * If not, we convert them to maps and check if those are equal.
6249 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6252 isl_map
*map1
, *map2
;
6257 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6258 if (equal
< 0 || equal
)
6261 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6262 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6263 equal
= isl_map_is_equal(map1
, map2
);
6270 /* Do "mpa1" and "mpa2" represent the same function?
6272 * Note that we cannot convert the entire isl_multi_pw_aff
6273 * to a map because the domains of the piecewise affine expressions
6274 * may not be the same.
6276 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6277 __isl_keep isl_multi_pw_aff
*mpa2
)
6285 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6286 mpa2
->space
, isl_dim_param
)) {
6287 if (!isl_space_has_named_params(mpa1
->space
))
6289 if (!isl_space_has_named_params(mpa2
->space
))
6291 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6292 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6293 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6294 isl_multi_pw_aff_get_space(mpa2
));
6295 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6296 isl_multi_pw_aff_get_space(mpa1
));
6297 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6298 isl_multi_pw_aff_free(mpa1
);
6299 isl_multi_pw_aff_free(mpa2
);
6303 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6304 if (equal
< 0 || !equal
)
6307 for (i
= 0; i
< mpa1
->n
; ++i
) {
6308 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6309 if (equal
< 0 || !equal
)
6316 /* Coalesce the elements of "mpa".
6318 * Note that such coalescing does not change the meaning of "mpa"
6319 * so there is no need to cow. We do need to be careful not to
6320 * destroy any other copies of "mpa" in case of failure.
6322 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
6323 __isl_take isl_multi_pw_aff
*mpa
)
6330 for (i
= 0; i
< mpa
->n
; ++i
) {
6331 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6332 pa
= isl_pw_aff_coalesce(pa
);
6334 return isl_multi_pw_aff_free(mpa
);
6335 isl_pw_aff_free(mpa
->p
[i
]);
6342 /* Compute the pullback of "mpa" by the function represented by "ma".
6343 * In other words, plug in "ma" in "mpa".
6345 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6347 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6348 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6351 isl_space
*space
= NULL
;
6353 mpa
= isl_multi_pw_aff_cow(mpa
);
6357 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6358 isl_multi_pw_aff_get_space(mpa
));
6362 for (i
= 0; i
< mpa
->n
; ++i
) {
6363 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6364 isl_multi_aff_copy(ma
));
6369 isl_multi_aff_free(ma
);
6370 isl_space_free(mpa
->space
);
6374 isl_space_free(space
);
6375 isl_multi_pw_aff_free(mpa
);
6376 isl_multi_aff_free(ma
);
6380 /* Compute the pullback of "mpa" by the function represented by "ma".
6381 * In other words, plug in "ma" in "mpa".
6383 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6384 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6388 if (isl_space_match(mpa
->space
, isl_dim_param
,
6389 ma
->space
, isl_dim_param
))
6390 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6391 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6392 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6393 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6395 isl_multi_pw_aff_free(mpa
);
6396 isl_multi_aff_free(ma
);
6400 /* Compute the pullback of "mpa" by the function represented by "pma".
6401 * In other words, plug in "pma" in "mpa".
6403 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6405 static __isl_give isl_multi_pw_aff
*
6406 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6407 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6410 isl_space
*space
= NULL
;
6412 mpa
= isl_multi_pw_aff_cow(mpa
);
6416 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6417 isl_multi_pw_aff_get_space(mpa
));
6419 for (i
= 0; i
< mpa
->n
; ++i
) {
6420 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6421 isl_pw_multi_aff_copy(pma
));
6426 isl_pw_multi_aff_free(pma
);
6427 isl_space_free(mpa
->space
);
6431 isl_space_free(space
);
6432 isl_multi_pw_aff_free(mpa
);
6433 isl_pw_multi_aff_free(pma
);
6437 /* Compute the pullback of "mpa" by the function represented by "pma".
6438 * In other words, plug in "pma" in "mpa".
6440 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6441 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6445 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6446 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6447 mpa
= isl_multi_pw_aff_align_params(mpa
,
6448 isl_pw_multi_aff_get_space(pma
));
6449 pma
= isl_pw_multi_aff_align_params(pma
,
6450 isl_multi_pw_aff_get_space(mpa
));
6451 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6453 isl_multi_pw_aff_free(mpa
);
6454 isl_pw_multi_aff_free(pma
);
6458 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6459 * with the domain of "aff". The domain of the result is the same
6461 * "mpa" and "aff" are assumed to have been aligned.
6463 * We first extract the parametric constant from "aff", defined
6464 * over the correct domain.
6465 * Then we add the appropriate combinations of the members of "mpa".
6466 * Finally, we add the integer divisions through recursive calls.
6468 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6469 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6471 int i
, n_param
, n_in
, n_div
;
6477 n_param
= isl_aff_dim(aff
, isl_dim_param
);
6478 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6479 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6481 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6482 tmp
= isl_aff_copy(aff
);
6483 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6484 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6485 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6486 isl_space_dim(space
, isl_dim_set
));
6487 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6488 pa
= isl_pw_aff_from_aff(tmp
);
6490 for (i
= 0; i
< n_in
; ++i
) {
6493 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6495 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6496 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6497 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6498 pa
= isl_pw_aff_add(pa
, pa_i
);
6501 for (i
= 0; i
< n_div
; ++i
) {
6505 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6507 div
= isl_aff_get_div(aff
, i
);
6508 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6509 isl_multi_pw_aff_copy(mpa
), div
);
6510 pa_i
= isl_pw_aff_floor(pa_i
);
6511 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6512 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6513 pa
= isl_pw_aff_add(pa
, pa_i
);
6516 isl_multi_pw_aff_free(mpa
);
6522 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6523 * with the domain of "aff". The domain of the result is the same
6526 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6527 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6531 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6532 mpa
->space
, isl_dim_param
))
6533 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6535 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6536 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6538 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6541 isl_multi_pw_aff_free(mpa
);
6545 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6546 * with the domain of "pa". The domain of the result is the same
6548 * "mpa" and "pa" are assumed to have been aligned.
6550 * We consider each piece in turn. Note that the domains of the
6551 * pieces are assumed to be disjoint and they remain disjoint
6552 * after taking the preimage (over the same function).
6554 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6555 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6564 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6565 isl_pw_aff_get_space(pa
));
6566 res
= isl_pw_aff_empty(space
);
6568 for (i
= 0; i
< pa
->n
; ++i
) {
6572 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6573 isl_multi_pw_aff_copy(mpa
),
6574 isl_aff_copy(pa
->p
[i
].aff
));
6575 domain
= isl_set_copy(pa
->p
[i
].set
);
6576 domain
= isl_set_preimage_multi_pw_aff(domain
,
6577 isl_multi_pw_aff_copy(mpa
));
6578 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6579 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6582 isl_pw_aff_free(pa
);
6583 isl_multi_pw_aff_free(mpa
);
6586 isl_pw_aff_free(pa
);
6587 isl_multi_pw_aff_free(mpa
);
6591 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6592 * with the domain of "pa". The domain of the result is the same
6595 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6596 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6600 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6601 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6603 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6604 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6606 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6608 isl_pw_aff_free(pa
);
6609 isl_multi_pw_aff_free(mpa
);
6613 /* Compute the pullback of "pa" by the function represented by "mpa".
6614 * In other words, plug in "mpa" in "pa".
6615 * "pa" and "mpa" are assumed to have been aligned.
6617 * The pullback is computed by applying "pa" to "mpa".
6619 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6620 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6622 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6625 /* Compute the pullback of "pa" by the function represented by "mpa".
6626 * In other words, plug in "mpa" in "pa".
6628 * The pullback is computed by applying "pa" to "mpa".
6630 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6631 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6633 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6636 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6637 * In other words, plug in "mpa2" in "mpa1".
6639 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6641 * We pullback each member of "mpa1" in turn.
6643 static __isl_give isl_multi_pw_aff
*
6644 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6645 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6648 isl_space
*space
= NULL
;
6650 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6654 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6655 isl_multi_pw_aff_get_space(mpa1
));
6657 for (i
= 0; i
< mpa1
->n
; ++i
) {
6658 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6659 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6664 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6666 isl_multi_pw_aff_free(mpa2
);
6669 isl_space_free(space
);
6670 isl_multi_pw_aff_free(mpa1
);
6671 isl_multi_pw_aff_free(mpa2
);
6675 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6676 * In other words, plug in "mpa2" in "mpa1".
6678 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6679 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6681 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6682 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6685 /* Compare two isl_affs.
6687 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6688 * than "aff2" and 0 if they are equal.
6690 * The order is fairly arbitrary. We do consider expressions that only involve
6691 * earlier dimensions as "smaller".
6693 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6706 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6710 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6711 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6713 return last1
- last2
;
6715 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
6718 /* Compare two isl_pw_affs.
6720 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6721 * than "pa2" and 0 if they are equal.
6723 * The order is fairly arbitrary. We do consider expressions that only involve
6724 * earlier dimensions as "smaller".
6726 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
6727 __isl_keep isl_pw_aff
*pa2
)
6740 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
6744 if (pa1
->n
!= pa2
->n
)
6745 return pa1
->n
- pa2
->n
;
6747 for (i
= 0; i
< pa1
->n
; ++i
) {
6748 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
6751 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);
6759 /* Return a piecewise affine expression that is equal to "v" on "domain".
6761 __isl_give isl_pw_aff
*isl_pw_aff_val_on_domain(__isl_take isl_set
*domain
,
6762 __isl_take isl_val
*v
)
6765 isl_local_space
*ls
;
6768 space
= isl_set_get_space(domain
);
6769 ls
= isl_local_space_from_space(space
);
6770 aff
= isl_aff_val_on_domain(ls
, v
);
6772 return isl_pw_aff_alloc(domain
, aff
);
6775 /* Return a multi affine expression that is equal to "mv" on domain
6778 __isl_give isl_multi_aff
*isl_multi_aff_multi_val_on_space(
6779 __isl_take isl_space
*space
, __isl_take isl_multi_val
*mv
)
6783 isl_local_space
*ls
;
6789 n
= isl_multi_val_dim(mv
, isl_dim_set
);
6790 space2
= isl_multi_val_get_space(mv
);
6791 space2
= isl_space_align_params(space2
, isl_space_copy(space
));
6792 space
= isl_space_align_params(space
, isl_space_copy(space2
));
6793 space
= isl_space_map_from_domain_and_range(space
, space2
);
6794 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
6795 ls
= isl_local_space_from_space(isl_space_domain(space
));
6796 for (i
= 0; i
< n
; ++i
) {
6800 v
= isl_multi_val_get_val(mv
, i
);
6801 aff
= isl_aff_val_on_domain(isl_local_space_copy(ls
), v
);
6802 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
6804 isl_local_space_free(ls
);
6806 isl_multi_val_free(mv
);
6809 isl_space_free(space
);
6810 isl_multi_val_free(mv
);
6814 /* Return a piecewise multi-affine expression
6815 * that is equal to "mv" on "domain".
6817 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_multi_val_on_domain(
6818 __isl_take isl_set
*domain
, __isl_take isl_multi_val
*mv
)
6823 space
= isl_set_get_space(domain
);
6824 ma
= isl_multi_aff_multi_val_on_space(space
, mv
);
6826 return isl_pw_multi_aff_alloc(domain
, ma
);
6829 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6830 * mv is the value that should be attained on each domain set
6831 * res collects the results
6833 struct isl_union_pw_multi_aff_multi_val_on_domain_data
{
6835 isl_union_pw_multi_aff
*res
;
6838 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6839 * and add it to data->res.
6841 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set
*domain
,
6844 struct isl_union_pw_multi_aff_multi_val_on_domain_data
*data
= user
;
6845 isl_pw_multi_aff
*pma
;
6848 mv
= isl_multi_val_copy(data
->mv
);
6849 pma
= isl_pw_multi_aff_multi_val_on_domain(domain
, mv
);
6850 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
6852 return data
->res
? 0 : -1;
6855 /* Return a union piecewise multi-affine expression
6856 * that is equal to "mv" on "domain".
6858 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_multi_val_on_domain(
6859 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
6861 struct isl_union_pw_multi_aff_multi_val_on_domain_data data
;
6864 space
= isl_union_set_get_space(domain
);
6865 data
.res
= isl_union_pw_multi_aff_empty(space
);
6867 if (isl_union_set_foreach_set(domain
,
6868 &pw_multi_aff_multi_val_on_domain
, &data
) < 0)
6869 data
.res
= isl_union_pw_multi_aff_free(data
.res
);
6870 isl_union_set_free(domain
);
6871 isl_multi_val_free(mv
);
6875 /* Compute the pullback of data->pma by the function represented by "pma2",
6876 * provided the spaces match, and add the results to data->res.
6878 static int pullback_entry(void **entry
, void *user
)
6880 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
6881 isl_pw_multi_aff
*pma2
= *entry
;
6883 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
6884 pma2
->dim
, isl_dim_out
))
6887 pma2
= isl_pw_multi_aff_pullback_pw_multi_aff(
6888 isl_pw_multi_aff_copy(data
->pma
),
6889 isl_pw_multi_aff_copy(pma2
));
6891 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
6898 /* Compute the pullback of "upma1" by the function represented by "upma2".
6900 __isl_give isl_union_pw_multi_aff
*
6901 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
6902 __isl_take isl_union_pw_multi_aff
*upma1
,
6903 __isl_take isl_union_pw_multi_aff
*upma2
)
6905 return bin_op(upma1
, upma2
, &pullback_entry
);
6908 /* Check that the domain space of "upa" matches "space".
6910 * Return 0 on success and -1 on error.
6912 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
6913 * can in principle never fail since the space "space" is that
6914 * of the isl_multi_union_pw_aff and is a set space such that
6915 * there is no domain space to match.
6917 * We check the parameters and double-check that "space" is
6918 * indeed that of a set.
6920 static int isl_union_pw_aff_check_match_domain_space(
6921 __isl_keep isl_union_pw_aff
*upa
, __isl_keep isl_space
*space
)
6923 isl_space
*upa_space
;
6929 match
= isl_space_is_set(space
);
6933 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
6934 "expecting set space", return -1);
6936 upa_space
= isl_union_pw_aff_get_space(upa
);
6937 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
6941 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
6942 "parameters don't match", goto error
);
6944 isl_space_free(upa_space
);
6947 isl_space_free(upa_space
);
6951 /* Do the parameters of "upa" match those of "space"?
6953 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff
*upa
,
6954 __isl_keep isl_space
*space
)
6956 isl_space
*upa_space
;
6962 upa_space
= isl_union_pw_aff_get_space(upa
);
6964 match
= isl_space_match(space
, isl_dim_param
, upa_space
, isl_dim_param
);
6966 isl_space_free(upa_space
);
6970 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
6971 * space represents the new parameters.
6972 * res collects the results.
6974 struct isl_union_pw_aff_reset_params_data
{
6976 isl_union_pw_aff
*res
;
6979 /* Replace the parameters of "pa" by data->space and
6980 * add the result to data->res.
6982 static int reset_params(__isl_take isl_pw_aff
*pa
, void *user
)
6984 struct isl_union_pw_aff_reset_params_data
*data
= user
;
6987 space
= isl_pw_aff_get_space(pa
);
6988 space
= isl_space_replace(space
, isl_dim_param
, data
->space
);
6989 pa
= isl_pw_aff_reset_space(pa
, space
);
6990 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
6992 return data
->res
? 0 : -1;
6995 /* Replace the domain space of "upa" by "space".
6996 * Since a union expression does not have a (single) domain space,
6997 * "space" is necessarily a parameter space.
6999 * Since the order and the names of the parameters determine
7000 * the hash value, we need to create a new hash table.
7002 static __isl_give isl_union_pw_aff
*isl_union_pw_aff_reset_domain_space(
7003 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_space
*space
)
7005 struct isl_union_pw_aff_reset_params_data data
= { space
};
7008 match
= isl_union_pw_aff_matching_params(upa
, space
);
7010 upa
= isl_union_pw_aff_free(upa
);
7012 isl_space_free(space
);
7016 data
.res
= isl_union_pw_aff_empty(isl_space_copy(space
));
7017 if (isl_union_pw_aff_foreach_pw_aff(upa
, &reset_params
, &data
) < 0)
7018 data
.res
= isl_union_pw_aff_free(data
.res
);
7020 isl_union_pw_aff_free(upa
);
7021 isl_space_free(space
);
7025 /* Replace the entry of isl_union_pw_aff to which "entry" points
7028 static int floor_entry(void **entry
, void *user
)
7030 isl_pw_aff
**pa
= (isl_pw_aff
**) entry
;
7032 *pa
= isl_pw_aff_floor(*pa
);
7039 /* Given f, return floor(f).
7041 __isl_give isl_union_pw_aff
*isl_union_pw_aff_floor(
7042 __isl_take isl_union_pw_aff
*upa
)
7046 upa
= isl_union_pw_aff_cow(upa
);
7050 ctx
= isl_union_pw_aff_get_ctx(upa
);
7051 if (isl_hash_table_foreach(ctx
, &upa
->table
, &floor_entry
, NULL
) < 0)
7052 upa
= isl_union_pw_aff_free(upa
);
7059 * upa mod m = upa - m * floor(upa/m)
7061 * with m an integer value.
7063 __isl_give isl_union_pw_aff
*isl_union_pw_aff_mod_val(
7064 __isl_take isl_union_pw_aff
*upa
, __isl_take isl_val
*m
)
7066 isl_union_pw_aff
*res
;
7071 if (!isl_val_is_int(m
))
7072 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7073 "expecting integer modulo", goto error
);
7074 if (!isl_val_is_pos(m
))
7075 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
7076 "expecting positive modulo", goto error
);
7078 res
= isl_union_pw_aff_copy(upa
);
7079 upa
= isl_union_pw_aff_scale_down_val(upa
, isl_val_copy(m
));
7080 upa
= isl_union_pw_aff_floor(upa
);
7081 upa
= isl_union_pw_aff_scale_val(upa
, m
);
7082 res
= isl_union_pw_aff_sub(res
, upa
);
7087 isl_union_pw_aff_free(upa
);
7091 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7092 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7094 * "res" collects the results.
7096 struct isl_union_pw_aff_aff_on_domain_data
{
7098 isl_union_pw_aff
*res
;
7101 /* Construct a piecewise affine expression that is equal to data->aff
7102 * on "domain" and add the result to data->res.
7104 static int pw_aff_aff_on_domain(__isl_take isl_set
*domain
, void *user
)
7106 struct isl_union_pw_aff_aff_on_domain_data
*data
= user
;
7111 aff
= isl_aff_copy(data
->aff
);
7112 dim
= isl_set_dim(domain
, isl_dim_set
);
7113 aff
= isl_aff_add_dims(aff
, isl_dim_in
, dim
);
7114 aff
= isl_aff_reset_domain_space(aff
, isl_set_get_space(domain
));
7115 pa
= isl_pw_aff_alloc(domain
, aff
);
7116 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7118 return data
->res
? 0 : -1;
7121 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7122 * pos is the output position that needs to be extracted.
7123 * res collects the results.
7125 struct isl_union_pw_multi_aff_get_union_pw_aff_data
{
7127 isl_union_pw_aff
*res
;
7130 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7131 * (assuming it has such a dimension) and add it to data->res.
7133 static int get_union_pw_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7135 struct isl_union_pw_multi_aff_get_union_pw_aff_data
*data
= user
;
7142 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
7143 if (data
->pos
>= n_out
) {
7144 isl_pw_multi_aff_free(pma
);
7148 pa
= isl_pw_multi_aff_get_pw_aff(pma
, data
->pos
);
7149 isl_pw_multi_aff_free(pma
);
7151 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7153 return data
->res
? 0 : -1;
7156 /* Extract an isl_union_pw_aff corresponding to
7157 * output dimension "pos" of "upma".
7159 __isl_give isl_union_pw_aff
*isl_union_pw_multi_aff_get_union_pw_aff(
7160 __isl_keep isl_union_pw_multi_aff
*upma
, int pos
)
7162 struct isl_union_pw_multi_aff_get_union_pw_aff_data data
;
7169 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7170 "cannot extract at negative position", return NULL
);
7172 space
= isl_union_pw_multi_aff_get_space(upma
);
7173 data
.res
= isl_union_pw_aff_empty(space
);
7175 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
7176 &get_union_pw_aff
, &data
) < 0)
7177 data
.res
= isl_union_pw_aff_free(data
.res
);
7182 /* Return a union piecewise affine expression
7183 * that is equal to "aff" on "domain".
7185 * Construct an isl_pw_aff on each of the sets in "domain" and
7186 * collect the results.
7188 __isl_give isl_union_pw_aff
*isl_union_pw_aff_aff_on_domain(
7189 __isl_take isl_union_set
*domain
, __isl_take isl_aff
*aff
)
7191 struct isl_union_pw_aff_aff_on_domain_data data
;
7194 if (!domain
|| !aff
)
7196 if (!isl_local_space_is_params(aff
->ls
))
7197 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
7198 "expecting parametric expression", goto error
);
7200 space
= isl_union_set_get_space(domain
);
7201 data
.res
= isl_union_pw_aff_empty(space
);
7203 if (isl_union_set_foreach_set(domain
, &pw_aff_aff_on_domain
, &data
) < 0)
7204 data
.res
= isl_union_pw_aff_free(data
.res
);
7205 isl_union_set_free(domain
);
7209 isl_union_set_free(domain
);
7214 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7215 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7216 * "res" collects the results.
7218 struct isl_union_pw_aff_val_on_domain_data
{
7220 isl_union_pw_aff
*res
;
7223 /* Construct a piecewise affine expression that is equal to data->v
7224 * on "domain" and add the result to data->res.
7226 static int pw_aff_val_on_domain(__isl_take isl_set
*domain
, void *user
)
7228 struct isl_union_pw_aff_val_on_domain_data
*data
= user
;
7232 v
= isl_val_copy(data
->v
);
7233 pa
= isl_pw_aff_val_on_domain(domain
, v
);
7234 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7236 return data
->res
? 0 : -1;
7239 /* Return a union piecewise affine expression
7240 * that is equal to "v" on "domain".
7242 * Construct an isl_pw_aff on each of the sets in "domain" and
7243 * collect the results.
7245 __isl_give isl_union_pw_aff
*isl_union_pw_aff_val_on_domain(
7246 __isl_take isl_union_set
*domain
, __isl_take isl_val
*v
)
7248 struct isl_union_pw_aff_val_on_domain_data data
;
7251 space
= isl_union_set_get_space(domain
);
7252 data
.res
= isl_union_pw_aff_empty(space
);
7254 if (isl_union_set_foreach_set(domain
, &pw_aff_val_on_domain
, &data
) < 0)
7255 data
.res
= isl_union_pw_aff_free(data
.res
);
7256 isl_union_set_free(domain
);
7261 /* Construct a piecewise multi affine expression
7262 * that is equal to "pa" and add it to upma.
7264 static int pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7266 isl_union_pw_multi_aff
**upma
= user
;
7267 isl_pw_multi_aff
*pma
;
7269 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
7270 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
7272 return *upma
? 0 : -1;
7275 /* Construct and return a union piecewise multi affine expression
7276 * that is equal to the given union piecewise affine expression.
7278 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_pw_aff(
7279 __isl_take isl_union_pw_aff
*upa
)
7282 isl_union_pw_multi_aff
*upma
;
7287 space
= isl_union_pw_aff_get_space(upa
);
7288 upma
= isl_union_pw_multi_aff_empty(space
);
7290 if (isl_union_pw_aff_foreach_pw_aff(upa
,
7291 &pw_multi_aff_from_pw_aff_entry
, &upma
) < 0)
7292 upma
= isl_union_pw_multi_aff_free(upma
);
7294 isl_union_pw_aff_free(upa
);
7298 /* Compute the set of elements in the domain of "pa" where it is zero and
7299 * add this set to "uset".
7301 static int zero_union_set(__isl_take isl_pw_aff
*pa
, void *user
)
7303 isl_union_set
**uset
= (isl_union_set
**)user
;
7305 *uset
= isl_union_set_add_set(*uset
, isl_pw_aff_zero_set(pa
));
7307 return *uset
? 0 : -1;
7310 /* Return a union set containing those elements in the domain
7311 * of "upa" where it is zero.
7313 __isl_give isl_union_set
*isl_union_pw_aff_zero_union_set(
7314 __isl_take isl_union_pw_aff
*upa
)
7316 isl_union_set
*zero
;
7318 zero
= isl_union_set_empty(isl_union_pw_aff_get_space(upa
));
7319 if (isl_union_pw_aff_foreach_pw_aff(upa
, &zero_union_set
, &zero
) < 0)
7320 zero
= isl_union_set_free(zero
);
7322 isl_union_pw_aff_free(upa
);
7326 /* Convert "pa" to an isl_map and add it to *umap.
7328 static int map_from_pw_aff_entry(__isl_take isl_pw_aff
*pa
, void *user
)
7330 isl_union_map
**umap
= user
;
7333 map
= isl_map_from_pw_aff(pa
);
7334 *umap
= isl_union_map_add_map(*umap
, map
);
7336 return *umap
? 0 : -1;
7339 /* Construct a union map mapping the domain of the union
7340 * piecewise affine expression to its range, with the single output dimension
7341 * equated to the corresponding affine expressions on their cells.
7343 __isl_give isl_union_map
*isl_union_map_from_union_pw_aff(
7344 __isl_take isl_union_pw_aff
*upa
)
7347 isl_union_map
*umap
;
7352 space
= isl_union_pw_aff_get_space(upa
);
7353 umap
= isl_union_map_empty(space
);
7355 if (isl_union_pw_aff_foreach_pw_aff(upa
, &map_from_pw_aff_entry
,
7357 umap
= isl_union_map_free(umap
);
7359 isl_union_pw_aff_free(upa
);
7363 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7364 * upma is the function that is plugged in.
7365 * pa is the current part of the function in which upma is plugged in.
7366 * res collects the results.
7368 struct isl_union_pw_aff_pullback_upma_data
{
7369 isl_union_pw_multi_aff
*upma
;
7371 isl_union_pw_aff
*res
;
7374 /* Check if "pma" can be plugged into data->pa.
7375 * If so, perform the pullback and add the result to data->res.
7377 static int pa_pb_pma(void **entry
, void *user
)
7379 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7380 isl_pw_multi_aff
*pma
= *entry
;
7383 if (!isl_space_tuple_is_equal(data
->pa
->dim
, isl_dim_in
,
7384 pma
->dim
, isl_dim_out
))
7387 pma
= isl_pw_multi_aff_copy(pma
);
7388 pa
= isl_pw_aff_copy(data
->pa
);
7389 pa
= isl_pw_aff_pullback_pw_multi_aff(pa
, pma
);
7391 data
->res
= isl_union_pw_aff_add_pw_aff(data
->res
, pa
);
7393 return data
->res
? 0 : -1;
7396 /* Check if any of the elements of data->upma can be plugged into pa,
7397 * add if so add the result to data->res.
7399 static int upa_pb_upma(void **entry
, void *user
)
7401 struct isl_union_pw_aff_pullback_upma_data
*data
= user
;
7403 isl_pw_aff
*pa
= *entry
;
7406 ctx
= isl_union_pw_multi_aff_get_ctx(data
->upma
);
7407 if (isl_hash_table_foreach(ctx
, &data
->upma
->table
,
7408 &pa_pb_pma
, data
) < 0)
7414 /* Compute the pullback of "upa" by the function represented by "upma".
7415 * In other words, plug in "upma" in "upa". The result contains
7416 * expressions defined over the domain space of "upma".
7418 * Run over all pairs of elements in "upa" and "upma", perform
7419 * the pullback when appropriate and collect the results.
7420 * If the hash value were based on the domain space rather than
7421 * the function space, then we could run through all elements
7422 * of "upma" and directly pick out the corresponding element of "upa".
7424 __isl_give isl_union_pw_aff
*isl_union_pw_aff_pullback_union_pw_multi_aff(
7425 __isl_take isl_union_pw_aff
*upa
,
7426 __isl_take isl_union_pw_multi_aff
*upma
)
7428 struct isl_union_pw_aff_pullback_upma_data data
= { NULL
, NULL
};
7432 space
= isl_union_pw_multi_aff_get_space(upma
);
7433 upa
= isl_union_pw_aff_align_params(upa
, space
);
7434 space
= isl_union_pw_aff_get_space(upa
);
7435 upma
= isl_union_pw_multi_aff_align_params(upma
, space
);
7440 ctx
= isl_union_pw_aff_get_ctx(upa
);
7442 space
= isl_union_pw_aff_get_space(upa
);
7443 data
.res
= isl_union_pw_aff_alloc(space
, upa
->table
.n
);
7444 if (isl_hash_table_foreach(ctx
, &upa
->table
, &upa_pb_upma
, &data
) < 0)
7445 data
.res
= isl_union_pw_aff_free(data
.res
);
7447 isl_union_pw_aff_free(upa
);
7448 isl_union_pw_multi_aff_free(upma
);
7451 isl_union_pw_aff_free(upa
);
7452 isl_union_pw_multi_aff_free(upma
);
7457 #define BASE union_pw_aff
7459 #define DOMBASE union_set
7461 #define NO_MOVE_DIMS
7470 #include <isl_multi_templ.c>
7471 #include <isl_multi_apply_set.c>
7472 #include <isl_multi_apply_union_set.c>
7473 #include <isl_multi_gist.c>
7474 #include <isl_multi_intersect.c>
7476 /* Construct a multiple union piecewise affine expression
7477 * in the given space with value zero in each of the output dimensions.
7479 * Since there is no canonical zero value for
7480 * a union piecewise affine expression, we can only construct
7481 * zero-dimensional "zero" value.
7483 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_zero(
7484 __isl_take isl_space
*space
)
7489 if (!isl_space_is_set(space
))
7490 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7491 "expecting set space", goto error
);
7492 if (isl_space_dim(space
, isl_dim_out
) != 0)
7493 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
7494 "expecting 0D space", goto error
);
7496 return isl_multi_union_pw_aff_alloc(space
);
7498 isl_space_free(space
);
7502 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7503 * with the actual sum on the shared domain and
7504 * the defined expression on the symmetric difference of the domains.
7506 * We simply iterate over the elements in both arguments and
7507 * call isl_union_pw_aff_union_add on each of them.
7509 static __isl_give isl_multi_union_pw_aff
*
7510 isl_multi_union_pw_aff_union_add_aligned(
7511 __isl_take isl_multi_union_pw_aff
*mupa1
,
7512 __isl_take isl_multi_union_pw_aff
*mupa2
)
7514 return isl_multi_union_pw_aff_bin_op(mupa1
, mupa2
,
7515 &isl_union_pw_aff_union_add
);
7518 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7519 * with the actual sum on the shared domain and
7520 * the defined expression on the symmetric difference of the domains.
7522 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_union_add(
7523 __isl_take isl_multi_union_pw_aff
*mupa1
,
7524 __isl_take isl_multi_union_pw_aff
*mupa2
)
7526 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1
, mupa2
,
7527 &isl_multi_union_pw_aff_union_add_aligned
);
7530 /* Construct and return a multi union piecewise affine expression
7531 * that is equal to the given multi affine expression.
7533 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_aff(
7534 __isl_take isl_multi_aff
*ma
)
7536 isl_multi_pw_aff
*mpa
;
7538 mpa
= isl_multi_pw_aff_from_multi_aff(ma
);
7539 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa
);
7542 /* Construct and return a multi union piecewise affine expression
7543 * that is equal to the given multi piecewise affine expression.
7545 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_multi_pw_aff(
7546 __isl_take isl_multi_pw_aff
*mpa
)
7550 isl_multi_union_pw_aff
*mupa
;
7555 space
= isl_multi_pw_aff_get_space(mpa
);
7556 space
= isl_space_range(space
);
7557 mupa
= isl_multi_union_pw_aff_alloc(space
);
7559 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
7560 for (i
= 0; i
< n
; ++i
) {
7562 isl_union_pw_aff
*upa
;
7564 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
7565 upa
= isl_union_pw_aff_from_pw_aff(pa
);
7566 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7569 isl_multi_pw_aff_free(mpa
);
7574 /* Extract the range space of "pma" and assign it to *space.
7575 * If *space has already been set (through a previous call to this function),
7576 * then check that the range space is the same.
7578 static int extract_space(__isl_take isl_pw_multi_aff
*pma
, void *user
)
7580 isl_space
**space
= user
;
7581 isl_space
*pma_space
;
7584 pma_space
= isl_space_range(isl_pw_multi_aff_get_space(pma
));
7585 isl_pw_multi_aff_free(pma
);
7594 equal
= isl_space_is_equal(pma_space
, *space
);
7595 isl_space_free(pma_space
);
7600 isl_die(isl_space_get_ctx(*space
), isl_error_invalid
,
7601 "range spaces not the same", return -1);
7605 /* Construct and return a multi union piecewise affine expression
7606 * that is equal to the given union piecewise multi affine expression.
7608 * In order to be able to perform the conversion, the input
7609 * needs to be non-empty and may only involve a single range space.
7611 __isl_give isl_multi_union_pw_aff
*
7612 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7613 __isl_take isl_union_pw_multi_aff
*upma
)
7615 isl_space
*space
= NULL
;
7616 isl_multi_union_pw_aff
*mupa
;
7621 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma
) == 0)
7622 isl_die(isl_union_pw_multi_aff_get_ctx(upma
), isl_error_invalid
,
7623 "cannot extract range space from empty input",
7625 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
, &extract_space
,
7632 n
= isl_space_dim(space
, isl_dim_set
);
7633 mupa
= isl_multi_union_pw_aff_alloc(space
);
7635 for (i
= 0; i
< n
; ++i
) {
7636 isl_union_pw_aff
*upa
;
7638 upa
= isl_union_pw_multi_aff_get_union_pw_aff(upma
, i
);
7639 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7642 isl_union_pw_multi_aff_free(upma
);
7645 isl_space_free(space
);
7646 isl_union_pw_multi_aff_free(upma
);
7650 /* Try and create an isl_multi_union_pw_aff that is equivalent
7651 * to the given isl_union_map.
7652 * The isl_union_map is required to be single-valued in each space.
7653 * Moreover, it cannot be empty and all range spaces need to be the same.
7654 * Otherwise, an error is produced.
7656 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_from_union_map(
7657 __isl_take isl_union_map
*umap
)
7659 isl_union_pw_multi_aff
*upma
;
7661 upma
= isl_union_pw_multi_aff_from_union_map(umap
);
7662 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma
);
7665 /* Return a multiple union piecewise affine expression
7666 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7667 * have been aligned.
7669 static __isl_give isl_multi_union_pw_aff
*
7670 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7671 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7675 isl_multi_union_pw_aff
*mupa
;
7680 n
= isl_multi_val_dim(mv
, isl_dim_set
);
7681 space
= isl_multi_val_get_space(mv
);
7682 mupa
= isl_multi_union_pw_aff_alloc(space
);
7683 for (i
= 0; i
< n
; ++i
) {
7685 isl_union_pw_aff
*upa
;
7687 v
= isl_multi_val_get_val(mv
, i
);
7688 upa
= isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain
),
7690 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7693 isl_union_set_free(domain
);
7694 isl_multi_val_free(mv
);
7697 isl_union_set_free(domain
);
7698 isl_multi_val_free(mv
);
7702 /* Return a multiple union piecewise affine expression
7703 * that is equal to "mv" on "domain".
7705 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_val_on_domain(
7706 __isl_take isl_union_set
*domain
, __isl_take isl_multi_val
*mv
)
7710 if (isl_space_match(domain
->dim
, isl_dim_param
,
7711 mv
->space
, isl_dim_param
))
7712 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7714 domain
= isl_union_set_align_params(domain
,
7715 isl_multi_val_get_space(mv
));
7716 mv
= isl_multi_val_align_params(mv
, isl_union_set_get_space(domain
));
7717 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain
, mv
);
7719 isl_union_set_free(domain
);
7720 isl_multi_val_free(mv
);
7724 /* Return a multiple union piecewise affine expression
7725 * that is equal to "ma" on "domain", assuming "domain" and "ma"
7726 * have been aligned.
7728 static __isl_give isl_multi_union_pw_aff
*
7729 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
7730 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
7734 isl_multi_union_pw_aff
*mupa
;
7739 n
= isl_multi_aff_dim(ma
, isl_dim_set
);
7740 space
= isl_multi_aff_get_space(ma
);
7741 mupa
= isl_multi_union_pw_aff_alloc(space
);
7742 for (i
= 0; i
< n
; ++i
) {
7744 isl_union_pw_aff
*upa
;
7746 aff
= isl_multi_aff_get_aff(ma
, i
);
7747 upa
= isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain
),
7749 mupa
= isl_multi_union_pw_aff_set_union_pw_aff(mupa
, i
, upa
);
7752 isl_union_set_free(domain
);
7753 isl_multi_aff_free(ma
);
7756 isl_union_set_free(domain
);
7757 isl_multi_aff_free(ma
);
7761 /* Return a multiple union piecewise affine expression
7762 * that is equal to "ma" on "domain".
7764 __isl_give isl_multi_union_pw_aff
*isl_multi_union_pw_aff_multi_aff_on_domain(
7765 __isl_take isl_union_set
*domain
, __isl_take isl_multi_aff
*ma
)
7769 if (isl_space_match(domain
->dim
, isl_dim_param
,
7770 ma
->space
, isl_dim_param
))
7771 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
7773 domain
= isl_union_set_align_params(domain
,
7774 isl_multi_aff_get_space(ma
));
7775 ma
= isl_multi_aff_align_params(ma
, isl_union_set_get_space(domain
));
7776 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain
, ma
);
7778 isl_union_set_free(domain
);
7779 isl_multi_aff_free(ma
);