2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
33 #include <isl_list_templ.c>
38 #include <isl_list_templ.c>
40 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
41 __isl_take isl_vec
*v
)
48 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
58 isl_local_space_free(ls
);
63 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
72 ctx
= isl_local_space_get_ctx(ls
);
73 if (!isl_local_space_divs_known(ls
))
74 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
76 if (!isl_local_space_is_set(ls
))
77 isl_die(ctx
, isl_error_invalid
,
78 "domain of affine expression should be a set",
81 total
= isl_local_space_dim(ls
, isl_dim_all
);
82 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
83 return isl_aff_alloc_vec(ls
, v
);
85 isl_local_space_free(ls
);
89 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
93 aff
= isl_aff_alloc(ls
);
97 isl_int_set_si(aff
->v
->el
[0], 1);
98 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff
*isl_pw_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls
));
111 /* Return an affine expression defined on the specified domain
112 * that represents NaN.
114 __isl_give isl_aff
*isl_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
118 aff
= isl_aff_alloc(ls
);
122 isl_seq_clr(aff
->v
->el
, aff
->v
->size
);
127 /* Return a piecewise affine expression defined on the specified domain
128 * that represents NaN.
130 __isl_give isl_pw_aff
*isl_pw_aff_nan_on_domain(__isl_take isl_local_space
*ls
)
132 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls
));
135 /* Return an affine expression that is equal to "val" on
136 * domain local space "ls".
138 __isl_give isl_aff
*isl_aff_val_on_domain(__isl_take isl_local_space
*ls
,
139 __isl_take isl_val
*val
)
145 if (!isl_val_is_rat(val
))
146 isl_die(isl_val_get_ctx(val
), isl_error_invalid
,
147 "expecting rational value", goto error
);
149 aff
= isl_aff_alloc(isl_local_space_copy(ls
));
153 isl_seq_clr(aff
->v
->el
+ 2, aff
->v
->size
- 2);
154 isl_int_set(aff
->v
->el
[1], val
->n
);
155 isl_int_set(aff
->v
->el
[0], val
->d
);
157 isl_local_space_free(ls
);
161 isl_local_space_free(ls
);
166 /* Return an affine expression that is equal to the specified dimension
169 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
170 enum isl_dim_type type
, unsigned pos
)
178 space
= isl_local_space_get_space(ls
);
181 if (isl_space_is_map(space
))
182 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
183 "expecting (parameter) set space", goto error
);
184 if (pos
>= isl_local_space_dim(ls
, type
))
185 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
186 "position out of bounds", goto error
);
188 isl_space_free(space
);
189 aff
= isl_aff_alloc(ls
);
193 pos
+= isl_local_space_offset(aff
->ls
, type
);
195 isl_int_set_si(aff
->v
->el
[0], 1);
196 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
197 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
201 isl_local_space_free(ls
);
202 isl_space_free(space
);
206 /* Return a piecewise affine expression that is equal to
207 * the specified dimension in "ls".
209 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
210 enum isl_dim_type type
, unsigned pos
)
212 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
215 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
224 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
229 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
230 isl_vec_copy(aff
->v
));
233 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
241 return isl_aff_dup(aff
);
244 __isl_null isl_aff
*isl_aff_free(__isl_take isl_aff
*aff
)
252 isl_local_space_free(aff
->ls
);
253 isl_vec_free(aff
->v
);
260 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
262 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
265 /* Externally, an isl_aff has a map space, but internally, the
266 * ls field corresponds to the domain of that space.
268 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
272 if (type
== isl_dim_out
)
274 if (type
== isl_dim_in
)
276 return isl_local_space_dim(aff
->ls
, type
);
279 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
281 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
284 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
289 space
= isl_local_space_get_space(aff
->ls
);
290 space
= isl_space_from_domain(space
);
291 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
295 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
296 __isl_keep isl_aff
*aff
)
298 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
301 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
306 ls
= isl_local_space_copy(aff
->ls
);
307 ls
= isl_local_space_from_domain(ls
);
308 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
312 /* Externally, an isl_aff has a map space, but internally, the
313 * ls field corresponds to the domain of that space.
315 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
316 enum isl_dim_type type
, unsigned pos
)
320 if (type
== isl_dim_out
)
322 if (type
== isl_dim_in
)
324 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
327 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
328 __isl_take isl_space
*dim
)
330 aff
= isl_aff_cow(aff
);
334 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
336 return isl_aff_free(aff
);
345 /* Reset the space of "aff". This function is called from isl_pw_templ.c
346 * and doesn't know if the space of an element object is represented
347 * directly or through its domain. It therefore passes along both.
349 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
350 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
352 isl_space_free(space
);
353 return isl_aff_reset_domain_space(aff
, domain
);
356 /* Reorder the coefficients of the affine expression based
357 * on the given reodering.
358 * The reordering r is assumed to have been extended with the local
361 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
362 __isl_take isl_reordering
*r
, int n_div
)
370 res
= isl_vec_alloc(vec
->ctx
,
371 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
372 isl_seq_cpy(res
->el
, vec
->el
, 2);
373 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
374 for (i
= 0; i
< r
->len
; ++i
)
375 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
377 isl_reordering_free(r
);
382 isl_reordering_free(r
);
386 /* Reorder the dimensions of the domain of "aff" according
387 * to the given reordering.
389 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
390 __isl_take isl_reordering
*r
)
392 aff
= isl_aff_cow(aff
);
396 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
397 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
398 aff
->ls
->div
->n_row
);
399 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
401 if (!aff
->v
|| !aff
->ls
)
402 return isl_aff_free(aff
);
407 isl_reordering_free(r
);
411 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
412 __isl_take isl_space
*model
)
417 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
418 model
, isl_dim_param
)) {
421 model
= isl_space_drop_dims(model
, isl_dim_in
,
422 0, isl_space_dim(model
, isl_dim_in
));
423 model
= isl_space_drop_dims(model
, isl_dim_out
,
424 0, isl_space_dim(model
, isl_dim_out
));
425 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
426 exp
= isl_reordering_extend_space(exp
,
427 isl_aff_get_domain_space(aff
));
428 aff
= isl_aff_realign_domain(aff
, exp
);
431 isl_space_free(model
);
434 isl_space_free(model
);
439 /* Is "aff" obviously equal to zero?
441 * If the denominator is zero, then "aff" is not equal to zero.
443 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
448 if (isl_int_is_zero(aff
->v
->el
[0]))
450 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
453 /* Does "aff" represent NaN?
455 int isl_aff_is_nan(__isl_keep isl_aff
*aff
)
460 return isl_seq_first_non_zero(aff
->v
->el
, 2) < 0;
463 /* Does "pa" involve any NaNs?
465 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff
*pa
)
474 for (i
= 0; i
< pa
->n
; ++i
) {
475 int is_nan
= isl_aff_is_nan(pa
->p
[i
].aff
);
476 if (is_nan
< 0 || is_nan
)
483 /* Are "aff1" and "aff2" obviously equal?
485 * NaN is not equal to anything, not even to another NaN.
487 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
494 if (isl_aff_is_nan(aff1
) || isl_aff_is_nan(aff2
))
497 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
498 if (equal
< 0 || !equal
)
501 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
504 /* Return the common denominator of "aff" in "v".
506 * We cannot return anything meaningful in case of a NaN.
508 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
512 if (isl_aff_is_nan(aff
))
513 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
514 "cannot get denominator of NaN", return -1);
515 isl_int_set(*v
, aff
->v
->el
[0]);
519 /* Return the common denominator of "aff".
521 __isl_give isl_val
*isl_aff_get_denominator_val(__isl_keep isl_aff
*aff
)
528 ctx
= isl_aff_get_ctx(aff
);
529 if (isl_aff_is_nan(aff
))
530 return isl_val_nan(ctx
);
531 return isl_val_int_from_isl_int(ctx
, aff
->v
->el
[0]);
534 /* Return the constant term of "aff" in "v".
536 * We cannot return anything meaningful in case of a NaN.
538 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
542 if (isl_aff_is_nan(aff
))
543 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
544 "cannot get constant term of NaN", return -1);
545 isl_int_set(*v
, aff
->v
->el
[1]);
549 /* Return the constant term of "aff".
551 __isl_give isl_val
*isl_aff_get_constant_val(__isl_keep isl_aff
*aff
)
559 ctx
= isl_aff_get_ctx(aff
);
560 if (isl_aff_is_nan(aff
))
561 return isl_val_nan(ctx
);
562 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1], aff
->v
->el
[0]);
563 return isl_val_normalize(v
);
566 /* Return the coefficient of the variable of type "type" at position "pos"
569 * We cannot return anything meaningful in case of a NaN.
571 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
572 enum isl_dim_type type
, int pos
, isl_int
*v
)
577 if (type
== isl_dim_out
)
578 isl_die(aff
->v
->ctx
, isl_error_invalid
,
579 "output/set dimension does not have a coefficient",
581 if (type
== isl_dim_in
)
584 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
585 isl_die(aff
->v
->ctx
, isl_error_invalid
,
586 "position out of bounds", return -1);
588 if (isl_aff_is_nan(aff
))
589 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
590 "cannot get coefficient of NaN", return -1);
591 pos
+= isl_local_space_offset(aff
->ls
, type
);
592 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
597 /* Return the coefficient of the variable of type "type" at position "pos"
600 __isl_give isl_val
*isl_aff_get_coefficient_val(__isl_keep isl_aff
*aff
,
601 enum isl_dim_type type
, int pos
)
609 ctx
= isl_aff_get_ctx(aff
);
610 if (type
== isl_dim_out
)
611 isl_die(ctx
, isl_error_invalid
,
612 "output/set dimension does not have a coefficient",
614 if (type
== isl_dim_in
)
617 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
618 isl_die(ctx
, isl_error_invalid
,
619 "position out of bounds", return NULL
);
621 if (isl_aff_is_nan(aff
))
622 return isl_val_nan(ctx
);
623 pos
+= isl_local_space_offset(aff
->ls
, type
);
624 v
= isl_val_rat_from_isl_int(ctx
, aff
->v
->el
[1 + pos
], aff
->v
->el
[0]);
625 return isl_val_normalize(v
);
628 /* Return the sign of the coefficient of the variable of type "type"
629 * at position "pos" of "aff".
631 int isl_aff_coefficient_sgn(__isl_keep isl_aff
*aff
, enum isl_dim_type type
,
639 ctx
= isl_aff_get_ctx(aff
);
640 if (type
== isl_dim_out
)
641 isl_die(ctx
, isl_error_invalid
,
642 "output/set dimension does not have a coefficient",
644 if (type
== isl_dim_in
)
647 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
648 isl_die(ctx
, isl_error_invalid
,
649 "position out of bounds", return 0);
651 pos
+= isl_local_space_offset(aff
->ls
, type
);
652 return isl_int_sgn(aff
->v
->el
[1 + pos
]);
655 /* Replace the denominator of "aff" by "v".
657 * A NaN is unaffected by this operation.
659 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
663 if (isl_aff_is_nan(aff
))
665 aff
= isl_aff_cow(aff
);
669 aff
->v
= isl_vec_cow(aff
->v
);
671 return isl_aff_free(aff
);
673 isl_int_set(aff
->v
->el
[0], v
);
678 /* Replace the numerator of the constant term of "aff" by "v".
680 * A NaN is unaffected by this operation.
682 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
686 if (isl_aff_is_nan(aff
))
688 aff
= isl_aff_cow(aff
);
692 aff
->v
= isl_vec_cow(aff
->v
);
694 return isl_aff_free(aff
);
696 isl_int_set(aff
->v
->el
[1], v
);
701 /* Replace the constant term of "aff" by "v".
703 * A NaN is unaffected by this operation.
705 __isl_give isl_aff
*isl_aff_set_constant_val(__isl_take isl_aff
*aff
,
706 __isl_take isl_val
*v
)
711 if (isl_aff_is_nan(aff
)) {
716 if (!isl_val_is_rat(v
))
717 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
718 "expecting rational value", goto error
);
720 if (isl_int_eq(aff
->v
->el
[1], v
->n
) &&
721 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
726 aff
= isl_aff_cow(aff
);
729 aff
->v
= isl_vec_cow(aff
->v
);
733 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
734 isl_int_set(aff
->v
->el
[1], v
->n
);
735 } else if (isl_int_is_one(v
->d
)) {
736 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
738 isl_seq_scale(aff
->v
->el
+ 1,
739 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
740 isl_int_mul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
741 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
742 aff
->v
= isl_vec_normalize(aff
->v
);
755 /* Add "v" to the constant term of "aff".
757 * A NaN is unaffected by this operation.
759 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
761 if (isl_int_is_zero(v
))
766 if (isl_aff_is_nan(aff
))
768 aff
= isl_aff_cow(aff
);
772 aff
->v
= isl_vec_cow(aff
->v
);
774 return isl_aff_free(aff
);
776 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], 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_val(__isl_take isl_aff
*aff
,
786 __isl_take isl_val
*v
)
791 if (isl_aff_is_nan(aff
) || isl_val_is_zero(v
)) {
796 if (!isl_val_is_rat(v
))
797 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
798 "expecting rational value", goto error
);
800 aff
= isl_aff_cow(aff
);
804 aff
->v
= isl_vec_cow(aff
->v
);
808 if (isl_int_is_one(v
->d
)) {
809 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
810 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
811 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
->n
);
812 aff
->v
= isl_vec_normalize(aff
->v
);
816 isl_seq_scale(aff
->v
->el
+ 1,
817 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
818 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
->n
);
819 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
820 aff
->v
= isl_vec_normalize(aff
->v
);
833 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
838 isl_int_set_si(t
, v
);
839 aff
= isl_aff_add_constant(aff
, t
);
845 /* Add "v" to the numerator of the constant term of "aff".
847 * A NaN is unaffected by this operation.
849 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
851 if (isl_int_is_zero(v
))
856 if (isl_aff_is_nan(aff
))
858 aff
= isl_aff_cow(aff
);
862 aff
->v
= isl_vec_cow(aff
->v
);
864 return isl_aff_free(aff
);
866 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
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_si(__isl_take isl_aff
*aff
, int v
)
883 isl_int_set_si(t
, v
);
884 aff
= isl_aff_add_constant_num(aff
, t
);
890 /* Replace the numerator of the constant term of "aff" by "v".
892 * A NaN is unaffected by this operation.
894 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
898 if (isl_aff_is_nan(aff
))
900 aff
= isl_aff_cow(aff
);
904 aff
->v
= isl_vec_cow(aff
->v
);
906 return isl_aff_free(aff
);
908 isl_int_set_si(aff
->v
->el
[1], v
);
913 /* Replace the numerator of the coefficient of the variable of type "type"
914 * at position "pos" of "aff" by "v".
916 * A NaN is unaffected by this operation.
918 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
919 enum isl_dim_type type
, int pos
, isl_int v
)
924 if (type
== isl_dim_out
)
925 isl_die(aff
->v
->ctx
, isl_error_invalid
,
926 "output/set dimension does not have a coefficient",
927 return isl_aff_free(aff
));
928 if (type
== isl_dim_in
)
931 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
932 isl_die(aff
->v
->ctx
, isl_error_invalid
,
933 "position out of bounds", return isl_aff_free(aff
));
935 if (isl_aff_is_nan(aff
))
937 aff
= isl_aff_cow(aff
);
941 aff
->v
= isl_vec_cow(aff
->v
);
943 return isl_aff_free(aff
);
945 pos
+= isl_local_space_offset(aff
->ls
, type
);
946 isl_int_set(aff
->v
->el
[1 + pos
], v
);
951 /* Replace the numerator of the coefficient of the variable of type "type"
952 * at position "pos" of "aff" by "v".
954 * A NaN is unaffected by this operation.
956 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
957 enum isl_dim_type type
, int pos
, int v
)
962 if (type
== isl_dim_out
)
963 isl_die(aff
->v
->ctx
, isl_error_invalid
,
964 "output/set dimension does not have a coefficient",
965 return isl_aff_free(aff
));
966 if (type
== isl_dim_in
)
969 if (pos
< 0 || pos
>= isl_local_space_dim(aff
->ls
, type
))
970 isl_die(aff
->v
->ctx
, isl_error_invalid
,
971 "position out of bounds", return isl_aff_free(aff
));
973 if (isl_aff_is_nan(aff
))
975 pos
+= isl_local_space_offset(aff
->ls
, type
);
976 if (isl_int_cmp_si(aff
->v
->el
[1 + pos
], v
) == 0)
979 aff
= isl_aff_cow(aff
);
983 aff
->v
= isl_vec_cow(aff
->v
);
985 return isl_aff_free(aff
);
987 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
992 /* Replace the coefficient of the variable of type "type" at position "pos"
995 * A NaN is unaffected by this operation.
997 __isl_give isl_aff
*isl_aff_set_coefficient_val(__isl_take isl_aff
*aff
,
998 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1003 if (type
== isl_dim_out
)
1004 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1005 "output/set dimension does not have a coefficient",
1007 if (type
== isl_dim_in
)
1010 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1011 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1012 "position out of bounds", goto error
);
1014 if (isl_aff_is_nan(aff
)) {
1018 if (!isl_val_is_rat(v
))
1019 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1020 "expecting rational value", goto error
);
1022 pos
+= isl_local_space_offset(aff
->ls
, type
);
1023 if (isl_int_eq(aff
->v
->el
[1 + pos
], v
->n
) &&
1024 isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1029 aff
= isl_aff_cow(aff
);
1032 aff
->v
= isl_vec_cow(aff
->v
);
1036 if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1037 isl_int_set(aff
->v
->el
[1 + pos
], v
->n
);
1038 } else if (isl_int_is_one(v
->d
)) {
1039 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1041 isl_seq_scale(aff
->v
->el
+ 1,
1042 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1043 isl_int_mul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1044 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1045 aff
->v
= isl_vec_normalize(aff
->v
);
1058 /* Add "v" to the coefficient of the variable of type "type"
1059 * at position "pos" of "aff".
1061 * A NaN is unaffected by this operation.
1063 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
1064 enum isl_dim_type type
, int pos
, isl_int v
)
1069 if (type
== isl_dim_out
)
1070 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1071 "output/set dimension does not have a coefficient",
1072 return isl_aff_free(aff
));
1073 if (type
== isl_dim_in
)
1076 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1077 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1078 "position out of bounds", return isl_aff_free(aff
));
1080 if (isl_aff_is_nan(aff
))
1082 aff
= isl_aff_cow(aff
);
1086 aff
->v
= isl_vec_cow(aff
->v
);
1088 return isl_aff_free(aff
);
1090 pos
+= isl_local_space_offset(aff
->ls
, type
);
1091 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
1096 /* Add "v" to the coefficient of the variable of type "type"
1097 * at position "pos" of "aff".
1099 * A NaN is unaffected by this operation.
1101 __isl_give isl_aff
*isl_aff_add_coefficient_val(__isl_take isl_aff
*aff
,
1102 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
1107 if (isl_val_is_zero(v
)) {
1112 if (type
== isl_dim_out
)
1113 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1114 "output/set dimension does not have a coefficient",
1116 if (type
== isl_dim_in
)
1119 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
1120 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1121 "position out of bounds", goto error
);
1123 if (isl_aff_is_nan(aff
)) {
1127 if (!isl_val_is_rat(v
))
1128 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1129 "expecting rational value", goto error
);
1131 aff
= isl_aff_cow(aff
);
1135 aff
->v
= isl_vec_cow(aff
->v
);
1139 pos
+= isl_local_space_offset(aff
->ls
, type
);
1140 if (isl_int_is_one(v
->d
)) {
1141 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1142 } else if (isl_int_eq(aff
->v
->el
[0], v
->d
)) {
1143 isl_int_add(aff
->v
->el
[1 + pos
], aff
->v
->el
[1 + pos
], v
->n
);
1144 aff
->v
= isl_vec_normalize(aff
->v
);
1148 isl_seq_scale(aff
->v
->el
+ 1,
1149 aff
->v
->el
+ 1, v
->d
, aff
->v
->size
- 1);
1150 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
->n
);
1151 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], v
->d
);
1152 aff
->v
= isl_vec_normalize(aff
->v
);
1165 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
1166 enum isl_dim_type type
, int pos
, int v
)
1171 isl_int_set_si(t
, v
);
1172 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
1178 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
1183 return isl_local_space_get_div(aff
->ls
, pos
);
1186 /* Return the negation of "aff".
1188 * As a special case, -NaN = NaN.
1190 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
1194 if (isl_aff_is_nan(aff
))
1196 aff
= isl_aff_cow(aff
);
1199 aff
->v
= isl_vec_cow(aff
->v
);
1201 return isl_aff_free(aff
);
1203 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
1208 /* Remove divs from the local space that do not appear in the affine
1210 * We currently only remove divs at the end.
1211 * Some intermediate divs may also not appear directly in the affine
1212 * expression, but we would also need to check that no other divs are
1213 * defined in terms of them.
1215 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
1224 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1225 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1227 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
1231 aff
= isl_aff_cow(aff
);
1235 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
1236 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
1237 if (!aff
->ls
|| !aff
->v
)
1238 return isl_aff_free(aff
);
1243 /* Given two affine expressions "p" of length p_len (including the
1244 * denominator and the constant term) and "subs" of length subs_len,
1245 * plug in "subs" for the variable at position "pos".
1246 * The variables of "subs" and "p" are assumed to match up to subs_len,
1247 * but "p" may have additional variables.
1248 * "v" is an initialized isl_int that can be used internally.
1250 * In particular, if "p" represents the expression
1254 * with i the variable at position "pos" and "subs" represents the expression
1258 * then the result represents the expression
1263 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
1264 int p_len
, int subs_len
, isl_int v
)
1266 isl_int_set(v
, p
[1 + pos
]);
1267 isl_int_set_si(p
[1 + pos
], 0);
1268 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
1269 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
1270 isl_int_mul(p
[0], p
[0], subs
[0]);
1273 /* Look for any divs in the aff->ls with a denominator equal to one
1274 * and plug them into the affine expression and any subsequent divs
1275 * that may reference the div.
1277 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
1283 isl_local_space
*ls
;
1289 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1291 for (i
= 0; i
< n
; ++i
) {
1292 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
1294 ls
= isl_local_space_copy(aff
->ls
);
1295 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
1296 aff
->ls
->div
->row
[i
], len
, i
+ 1, n
- (i
+ 1));
1297 vec
= isl_vec_copy(aff
->v
);
1298 vec
= isl_vec_cow(vec
);
1304 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
1305 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
1310 isl_vec_free(aff
->v
);
1312 isl_local_space_free(aff
->ls
);
1319 isl_local_space_free(ls
);
1320 return isl_aff_free(aff
);
1323 /* Look for any divs j that appear with a unit coefficient inside
1324 * the definitions of other divs i and plug them into the definitions
1327 * In particular, an expression of the form
1329 * floor((f(..) + floor(g(..)/n))/m)
1333 * floor((n * f(..) + g(..))/(n * m))
1335 * This simplification is correct because we can move the expression
1336 * f(..) into the inner floor in the original expression to obtain
1338 * floor(floor((n * f(..) + g(..))/n)/m)
1340 * from which we can derive the simplified expression.
1342 static __isl_give isl_aff
*plug_in_unit_divs(__isl_take isl_aff
*aff
)
1350 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1351 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1352 for (i
= 1; i
< n
; ++i
) {
1353 for (j
= 0; j
< i
; ++j
) {
1354 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][1 + off
+ j
]))
1356 aff
->ls
= isl_local_space_substitute_seq(aff
->ls
,
1357 isl_dim_div
, j
, aff
->ls
->div
->row
[j
],
1358 aff
->v
->size
, i
, 1);
1360 return isl_aff_free(aff
);
1367 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1369 * Even though this function is only called on isl_affs with a single
1370 * reference, we are careful to only change aff->v and aff->ls together.
1372 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
1374 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1375 isl_local_space
*ls
;
1378 ls
= isl_local_space_copy(aff
->ls
);
1379 ls
= isl_local_space_swap_div(ls
, a
, b
);
1380 v
= isl_vec_copy(aff
->v
);
1385 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
1386 isl_vec_free(aff
->v
);
1388 isl_local_space_free(aff
->ls
);
1394 isl_local_space_free(ls
);
1395 return isl_aff_free(aff
);
1398 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1400 * We currently do not actually remove div "b", but simply add its
1401 * coefficient to that of "a" and then zero it out.
1403 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
1405 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1407 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
1410 aff
->v
= isl_vec_cow(aff
->v
);
1412 return isl_aff_free(aff
);
1414 isl_int_add(aff
->v
->el
[1 + off
+ a
],
1415 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
1416 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
1421 /* Sort the divs in the local space of "aff" according to
1422 * the comparison function "cmp_row" in isl_local_space.c,
1423 * combining the coefficients of identical divs.
1425 * Reordering divs does not change the semantics of "aff",
1426 * so there is no need to call isl_aff_cow.
1427 * Moreover, this function is currently only called on isl_affs
1428 * with a single reference.
1430 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
1438 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
1439 n
= isl_aff_dim(aff
, isl_dim_div
);
1440 for (i
= 1; i
< n
; ++i
) {
1441 for (j
= i
- 1; j
>= 0; --j
) {
1442 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
1446 aff
= merge_divs(aff
, j
, j
+ 1);
1448 aff
= swap_div(aff
, j
, j
+ 1);
1457 /* Normalize the representation of "aff".
1459 * This function should only be called of "new" isl_affs, i.e.,
1460 * with only a single reference. We therefore do not need to
1461 * worry about affecting other instances.
1463 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
1467 aff
->v
= isl_vec_normalize(aff
->v
);
1469 return isl_aff_free(aff
);
1470 aff
= plug_in_integral_divs(aff
);
1471 aff
= plug_in_unit_divs(aff
);
1472 aff
= sort_divs(aff
);
1473 aff
= isl_aff_remove_unused_divs(aff
);
1477 /* Given f, return floor(f).
1478 * If f is an integer expression, then just return f.
1479 * If f is a constant, then return the constant floor(f).
1480 * Otherwise, if f = g/m, write g = q m + r,
1481 * create a new div d = [r/m] and return the expression q + d.
1482 * The coefficients in r are taken to lie between -m/2 and m/2.
1484 * As a special case, floor(NaN) = NaN.
1486 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
1496 if (isl_aff_is_nan(aff
))
1498 if (isl_int_is_one(aff
->v
->el
[0]))
1501 aff
= isl_aff_cow(aff
);
1505 aff
->v
= isl_vec_cow(aff
->v
);
1507 return isl_aff_free(aff
);
1509 if (isl_aff_is_cst(aff
)) {
1510 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1511 isl_int_set_si(aff
->v
->el
[0], 1);
1515 div
= isl_vec_copy(aff
->v
);
1516 div
= isl_vec_cow(div
);
1518 return isl_aff_free(aff
);
1520 ctx
= isl_aff_get_ctx(aff
);
1521 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
1522 for (i
= 1; i
< aff
->v
->size
; ++i
) {
1523 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1524 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
1525 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
1526 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
1527 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
1531 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
1533 return isl_aff_free(aff
);
1535 size
= aff
->v
->size
;
1536 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
1538 return isl_aff_free(aff
);
1539 isl_int_set_si(aff
->v
->el
[0], 1);
1540 isl_int_set_si(aff
->v
->el
[size
], 1);
1542 aff
= isl_aff_normalize(aff
);
1549 * aff mod m = aff - m * floor(aff/m)
1551 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
1555 res
= isl_aff_copy(aff
);
1556 aff
= isl_aff_scale_down(aff
, m
);
1557 aff
= isl_aff_floor(aff
);
1558 aff
= isl_aff_scale(aff
, m
);
1559 res
= isl_aff_sub(res
, aff
);
1566 * aff mod m = aff - m * floor(aff/m)
1568 * with m an integer value.
1570 __isl_give isl_aff
*isl_aff_mod_val(__isl_take isl_aff
*aff
,
1571 __isl_take isl_val
*m
)
1578 if (!isl_val_is_int(m
))
1579 isl_die(isl_val_get_ctx(m
), isl_error_invalid
,
1580 "expecting integer modulo", goto error
);
1582 res
= isl_aff_copy(aff
);
1583 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(m
));
1584 aff
= isl_aff_floor(aff
);
1585 aff
= isl_aff_scale_val(aff
, m
);
1586 res
= isl_aff_sub(res
, aff
);
1597 * pwaff mod m = pwaff - m * floor(pwaff/m)
1599 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
1603 res
= isl_pw_aff_copy(pwaff
);
1604 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
1605 pwaff
= isl_pw_aff_floor(pwaff
);
1606 pwaff
= isl_pw_aff_scale(pwaff
, m
);
1607 res
= isl_pw_aff_sub(res
, pwaff
);
1614 * pa mod m = pa - m * floor(pa/m)
1616 * with m an integer value.
1618 __isl_give isl_pw_aff
*isl_pw_aff_mod_val(__isl_take isl_pw_aff
*pa
,
1619 __isl_take isl_val
*m
)
1623 if (!isl_val_is_int(m
))
1624 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
1625 "expecting integer modulo", goto error
);
1626 pa
= isl_pw_aff_mod(pa
, m
->n
);
1630 isl_pw_aff_free(pa
);
1635 /* Given f, return ceil(f).
1636 * If f is an integer expression, then just return f.
1637 * Otherwise, let f be the expression
1643 * floor((e + m - 1)/m)
1645 * As a special case, ceil(NaN) = NaN.
1647 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
1652 if (isl_aff_is_nan(aff
))
1654 if (isl_int_is_one(aff
->v
->el
[0]))
1657 aff
= isl_aff_cow(aff
);
1660 aff
->v
= isl_vec_cow(aff
->v
);
1662 return isl_aff_free(aff
);
1664 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
1665 isl_int_sub_ui(aff
->v
->el
[1], aff
->v
->el
[1], 1);
1666 aff
= isl_aff_floor(aff
);
1671 /* Apply the expansion computed by isl_merge_divs.
1672 * The expansion itself is given by "exp" while the resulting
1673 * list of divs is given by "div".
1675 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1676 __isl_take isl_mat
*div
, int *exp
)
1683 aff
= isl_aff_cow(aff
);
1687 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1688 new_n_div
= isl_mat_rows(div
);
1689 if (new_n_div
< old_n_div
)
1690 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1691 "not an expansion", goto error
);
1693 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1697 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1699 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1700 if (j
>= 0 && exp
[j
] == i
) {
1702 isl_int_swap(aff
->v
->el
[offset
+ i
],
1703 aff
->v
->el
[offset
+ j
]);
1706 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1709 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1720 /* Add two affine expressions that live in the same local space.
1722 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1723 __isl_take isl_aff
*aff2
)
1727 aff1
= isl_aff_cow(aff1
);
1731 aff1
->v
= isl_vec_cow(aff1
->v
);
1737 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1738 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1739 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1740 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1741 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1742 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1743 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1755 /* Return the sum of "aff1" and "aff2".
1757 * If either of the two is NaN, then the result is NaN.
1759 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1760 __isl_take isl_aff
*aff2
)
1771 ctx
= isl_aff_get_ctx(aff1
);
1772 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1773 isl_die(ctx
, isl_error_invalid
,
1774 "spaces don't match", goto error
);
1776 if (isl_aff_is_nan(aff1
)) {
1780 if (isl_aff_is_nan(aff2
)) {
1785 n_div1
= isl_aff_dim(aff1
, isl_dim_div
);
1786 n_div2
= isl_aff_dim(aff2
, isl_dim_div
);
1787 if (n_div1
== 0 && n_div2
== 0)
1788 return add_expanded(aff1
, aff2
);
1790 exp1
= isl_alloc_array(ctx
, int, n_div1
);
1791 exp2
= isl_alloc_array(ctx
, int, n_div2
);
1792 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1795 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1796 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1797 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1801 return add_expanded(aff1
, aff2
);
1810 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1811 __isl_take isl_aff
*aff2
)
1813 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1816 /* Return the result of scaling "aff" by a factor of "f".
1818 * As a special case, f * NaN = NaN.
1820 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1826 if (isl_aff_is_nan(aff
))
1829 if (isl_int_is_one(f
))
1832 aff
= isl_aff_cow(aff
);
1835 aff
->v
= isl_vec_cow(aff
->v
);
1837 return isl_aff_free(aff
);
1839 if (isl_int_is_pos(f
) && isl_int_is_divisible_by(aff
->v
->el
[0], f
)) {
1840 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], f
);
1845 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1846 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1847 isl_int_divexact(gcd
, f
, gcd
);
1848 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1854 /* Multiple "aff" by "v".
1856 __isl_give isl_aff
*isl_aff_scale_val(__isl_take isl_aff
*aff
,
1857 __isl_take isl_val
*v
)
1862 if (isl_val_is_one(v
)) {
1867 if (!isl_val_is_rat(v
))
1868 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1869 "expecting rational factor", goto error
);
1871 aff
= isl_aff_scale(aff
, v
->n
);
1872 aff
= isl_aff_scale_down(aff
, v
->d
);
1882 /* Return the result of scaling "aff" down by a factor of "f".
1884 * As a special case, NaN/f = NaN.
1886 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1892 if (isl_aff_is_nan(aff
))
1895 if (isl_int_is_one(f
))
1898 aff
= isl_aff_cow(aff
);
1902 if (isl_int_is_zero(f
))
1903 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1904 "cannot scale down by zero", return isl_aff_free(aff
));
1906 aff
->v
= isl_vec_cow(aff
->v
);
1908 return isl_aff_free(aff
);
1911 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1912 isl_int_gcd(gcd
, gcd
, f
);
1913 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1914 isl_int_divexact(gcd
, f
, gcd
);
1915 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1921 /* Divide "aff" by "v".
1923 __isl_give isl_aff
*isl_aff_scale_down_val(__isl_take isl_aff
*aff
,
1924 __isl_take isl_val
*v
)
1929 if (isl_val_is_one(v
)) {
1934 if (!isl_val_is_rat(v
))
1935 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1936 "expecting rational factor", goto error
);
1937 if (!isl_val_is_pos(v
))
1938 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1939 "factor needs to be positive", goto error
);
1941 aff
= isl_aff_scale(aff
, v
->d
);
1942 aff
= isl_aff_scale_down(aff
, v
->n
);
1952 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1960 isl_int_set_ui(v
, f
);
1961 aff
= isl_aff_scale_down(aff
, v
);
1967 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1968 enum isl_dim_type type
, unsigned pos
, const char *s
)
1970 aff
= isl_aff_cow(aff
);
1973 if (type
== isl_dim_out
)
1974 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1975 "cannot set name of output/set dimension",
1976 return isl_aff_free(aff
));
1977 if (type
== isl_dim_in
)
1979 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1981 return isl_aff_free(aff
);
1986 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1987 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1989 aff
= isl_aff_cow(aff
);
1992 if (type
== isl_dim_out
)
1993 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1994 "cannot set name of output/set dimension",
1996 if (type
== isl_dim_in
)
1998 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
2000 return isl_aff_free(aff
);
2009 /* Replace the identifier of the input tuple of "aff" by "id".
2010 * type is currently required to be equal to isl_dim_in
2012 __isl_give isl_aff
*isl_aff_set_tuple_id(__isl_take isl_aff
*aff
,
2013 enum isl_dim_type type
, __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 only set id of input tuple", goto error
);
2021 aff
->ls
= isl_local_space_set_tuple_id(aff
->ls
, isl_dim_set
, id
);
2023 return isl_aff_free(aff
);
2032 /* Exploit the equalities in "eq" to simplify the affine expression
2033 * and the expressions of the integer divisions in the local space.
2034 * The integer divisions in this local space are assumed to appear
2035 * as regular dimensions in "eq".
2037 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
2038 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2046 if (eq
->n_eq
== 0) {
2047 isl_basic_set_free(eq
);
2051 aff
= isl_aff_cow(aff
);
2055 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
2056 isl_basic_set_copy(eq
));
2057 aff
->v
= isl_vec_cow(aff
->v
);
2058 if (!aff
->ls
|| !aff
->v
)
2061 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2063 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2064 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2065 if (j
< 0 || j
== 0 || j
>= total
)
2068 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
2072 isl_basic_set_free(eq
);
2073 aff
= isl_aff_normalize(aff
);
2076 isl_basic_set_free(eq
);
2081 /* Exploit the equalities in "eq" to simplify the affine expression
2082 * and the expressions of the integer divisions in the local space.
2084 static __isl_give isl_aff
*isl_aff_substitute_equalities(
2085 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
2091 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2093 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, n_div
);
2094 return isl_aff_substitute_equalities_lifted(aff
, eq
);
2096 isl_basic_set_free(eq
);
2101 /* Look for equalities among the variables shared by context and aff
2102 * and the integer divisions of aff, if any.
2103 * The equalities are then used to eliminate coefficients and/or integer
2104 * divisions from aff.
2106 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
2107 __isl_take isl_set
*context
)
2109 isl_basic_set
*hull
;
2114 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
2116 isl_basic_set
*bset
;
2117 isl_local_space
*ls
;
2118 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
2119 ls
= isl_aff_get_domain_local_space(aff
);
2120 bset
= isl_basic_set_from_local_space(ls
);
2121 bset
= isl_basic_set_lift(bset
);
2122 bset
= isl_basic_set_flatten(bset
);
2123 context
= isl_set_intersect(context
,
2124 isl_set_from_basic_set(bset
));
2127 hull
= isl_set_affine_hull(context
);
2128 return isl_aff_substitute_equalities_lifted(aff
, hull
);
2131 isl_set_free(context
);
2135 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
2136 __isl_take isl_set
*context
)
2138 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
2139 dom_context
= isl_set_intersect_params(dom_context
, context
);
2140 return isl_aff_gist(aff
, dom_context
);
2143 /* Return a basic set containing those elements in the space
2144 * of aff where it is non-negative.
2145 * If "rational" is set, then return a rational basic set.
2147 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2149 static __isl_give isl_basic_set
*aff_nonneg_basic_set(
2150 __isl_take isl_aff
*aff
, int rational
)
2152 isl_constraint
*ineq
;
2153 isl_basic_set
*bset
;
2157 if (isl_aff_is_nan(aff
)) {
2158 isl_space
*space
= isl_aff_get_domain_space(aff
);
2160 return isl_basic_set_empty(space
);
2163 ineq
= isl_inequality_from_aff(aff
);
2165 bset
= isl_basic_set_from_constraint(ineq
);
2167 bset
= isl_basic_set_set_rational(bset
);
2168 bset
= isl_basic_set_simplify(bset
);
2172 /* Return a basic set containing those elements in the space
2173 * of aff where it is non-negative.
2175 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
2177 return aff_nonneg_basic_set(aff
, 0);
2180 /* Return a basic set containing those elements in the domain space
2181 * of aff where it is negative.
2183 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
2185 aff
= isl_aff_neg(aff
);
2186 aff
= isl_aff_add_constant_num_si(aff
, -1);
2187 return isl_aff_nonneg_basic_set(aff
);
2190 /* Return a basic set containing those elements in the space
2191 * of aff where it is zero.
2192 * If "rational" is set, then return a rational basic set.
2194 * If "aff" is NaN, then it is not zero.
2196 static __isl_give isl_basic_set
*aff_zero_basic_set(__isl_take isl_aff
*aff
,
2199 isl_constraint
*ineq
;
2200 isl_basic_set
*bset
;
2204 if (isl_aff_is_nan(aff
)) {
2205 isl_space
*space
= isl_aff_get_domain_space(aff
);
2207 return isl_basic_set_empty(space
);
2210 ineq
= isl_equality_from_aff(aff
);
2212 bset
= isl_basic_set_from_constraint(ineq
);
2214 bset
= isl_basic_set_set_rational(bset
);
2215 bset
= isl_basic_set_simplify(bset
);
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is zero.
2222 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
2224 return aff_zero_basic_set(aff
, 0);
2227 /* Return a basic set containing those elements in the shared space
2228 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2230 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
2231 __isl_take isl_aff
*aff2
)
2233 aff1
= isl_aff_sub(aff1
, aff2
);
2235 return isl_aff_nonneg_basic_set(aff1
);
2238 /* Return a basic set containing those elements in the shared space
2239 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2241 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
2242 __isl_take isl_aff
*aff2
)
2244 return isl_aff_ge_basic_set(aff2
, aff1
);
2247 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
2248 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
2250 aff1
= isl_aff_add(aff1
, aff2
);
2251 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
2255 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
2263 /* Check whether the given affine expression has non-zero coefficient
2264 * for any dimension in the given range or if any of these dimensions
2265 * appear with non-zero coefficients in any of the integer divisions
2266 * involved in the affine expression.
2268 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
2269 enum isl_dim_type type
, unsigned first
, unsigned n
)
2281 ctx
= isl_aff_get_ctx(aff
);
2282 if (first
+ n
> isl_aff_dim(aff
, type
))
2283 isl_die(ctx
, isl_error_invalid
,
2284 "range out of bounds", return -1);
2286 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
2290 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
2291 for (i
= 0; i
< n
; ++i
)
2292 if (active
[first
+ i
]) {
2305 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
2306 enum isl_dim_type type
, unsigned first
, unsigned n
)
2312 if (type
== isl_dim_out
)
2313 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2314 "cannot drop output/set dimension",
2315 return isl_aff_free(aff
));
2316 if (type
== isl_dim_in
)
2318 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2321 ctx
= isl_aff_get_ctx(aff
);
2322 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
2323 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
2324 return isl_aff_free(aff
));
2326 aff
= isl_aff_cow(aff
);
2330 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
2332 return isl_aff_free(aff
);
2334 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2335 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
2337 return isl_aff_free(aff
);
2342 /* Project the domain of the affine expression onto its parameter space.
2343 * The affine expression may not involve any of the domain dimensions.
2345 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
2351 n
= isl_aff_dim(aff
, isl_dim_in
);
2352 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
2354 return isl_aff_free(aff
);
2356 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2357 "affine expression involves some of the domain dimensions",
2358 return isl_aff_free(aff
));
2359 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
2360 space
= isl_aff_get_domain_space(aff
);
2361 space
= isl_space_params(space
);
2362 aff
= isl_aff_reset_domain_space(aff
, space
);
2366 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
2367 enum isl_dim_type type
, unsigned first
, unsigned n
)
2373 if (type
== isl_dim_out
)
2374 isl_die(aff
->v
->ctx
, isl_error_invalid
,
2375 "cannot insert output/set dimensions",
2376 return isl_aff_free(aff
));
2377 if (type
== isl_dim_in
)
2379 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
2382 ctx
= isl_aff_get_ctx(aff
);
2383 if (first
> isl_local_space_dim(aff
->ls
, type
))
2384 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
2385 return isl_aff_free(aff
));
2387 aff
= isl_aff_cow(aff
);
2391 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
2393 return isl_aff_free(aff
);
2395 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
2396 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
2398 return isl_aff_free(aff
);
2403 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
2404 enum isl_dim_type type
, unsigned n
)
2408 pos
= isl_aff_dim(aff
, type
);
2410 return isl_aff_insert_dims(aff
, type
, pos
, n
);
2413 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
2414 enum isl_dim_type type
, unsigned n
)
2418 pos
= isl_pw_aff_dim(pwaff
, type
);
2420 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
2423 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2424 * to dimensions of "dst_type" at "dst_pos".
2426 * We only support moving input dimensions to parameters and vice versa.
2428 __isl_give isl_aff
*isl_aff_move_dims(__isl_take isl_aff
*aff
,
2429 enum isl_dim_type dst_type
, unsigned dst_pos
,
2430 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2438 !isl_local_space_is_named_or_nested(aff
->ls
, src_type
) &&
2439 !isl_local_space_is_named_or_nested(aff
->ls
, dst_type
))
2442 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
2443 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2444 "cannot move output/set dimension", isl_aff_free(aff
));
2445 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
2446 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2447 "cannot move divs", isl_aff_free(aff
));
2448 if (dst_type
== isl_dim_in
)
2449 dst_type
= isl_dim_set
;
2450 if (src_type
== isl_dim_in
)
2451 src_type
= isl_dim_set
;
2453 if (src_pos
+ n
> isl_local_space_dim(aff
->ls
, src_type
))
2454 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
2455 "range out of bounds", isl_aff_free(aff
));
2456 if (dst_type
== src_type
)
2457 isl_die(isl_aff_get_ctx(aff
), isl_error_unsupported
,
2458 "moving dims within the same type not supported",
2461 aff
= isl_aff_cow(aff
);
2465 g_src_pos
= 1 + isl_local_space_offset(aff
->ls
, src_type
) + src_pos
;
2466 g_dst_pos
= 1 + isl_local_space_offset(aff
->ls
, dst_type
) + dst_pos
;
2467 if (dst_type
> src_type
)
2470 aff
->v
= isl_vec_move_els(aff
->v
, g_dst_pos
, g_src_pos
, n
);
2471 aff
->ls
= isl_local_space_move_dims(aff
->ls
, dst_type
, dst_pos
,
2472 src_type
, src_pos
, n
);
2473 if (!aff
->v
|| !aff
->ls
)
2474 return isl_aff_free(aff
);
2476 aff
= sort_divs(aff
);
2481 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
2483 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
2484 return isl_pw_aff_alloc(dom
, aff
);
2488 #define PW isl_pw_aff
2492 #define EL_IS_ZERO is_empty
2496 #define IS_ZERO is_empty
2499 #undef DEFAULT_IS_ZERO
2500 #define DEFAULT_IS_ZERO 0
2507 #include <isl_pw_templ.c>
2509 static __isl_give isl_set
*align_params_pw_pw_set_and(
2510 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
2511 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2512 __isl_take isl_pw_aff
*pwaff2
))
2514 if (!pwaff1
|| !pwaff2
)
2516 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
2517 pwaff2
->dim
, isl_dim_param
))
2518 return fn(pwaff1
, pwaff2
);
2519 if (!isl_space_has_named_params(pwaff1
->dim
) ||
2520 !isl_space_has_named_params(pwaff2
->dim
))
2521 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
2522 "unaligned unnamed parameters", goto error
);
2523 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
2524 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
2525 return fn(pwaff1
, pwaff2
);
2527 isl_pw_aff_free(pwaff1
);
2528 isl_pw_aff_free(pwaff2
);
2532 /* Compute a piecewise quasi-affine expression with a domain that
2533 * is the union of those of pwaff1 and pwaff2 and such that on each
2534 * cell, the quasi-affine expression is the better (according to cmp)
2535 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2536 * is defined on a given cell, then the associated expression
2537 * is the defined one.
2539 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2540 __isl_take isl_pw_aff
*pwaff2
,
2541 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
2542 __isl_take isl_aff
*aff2
))
2549 if (!pwaff1
|| !pwaff2
)
2552 ctx
= isl_space_get_ctx(pwaff1
->dim
);
2553 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
2554 isl_die(ctx
, isl_error_invalid
,
2555 "arguments should live in same space", goto error
);
2557 if (isl_pw_aff_is_empty(pwaff1
)) {
2558 isl_pw_aff_free(pwaff1
);
2562 if (isl_pw_aff_is_empty(pwaff2
)) {
2563 isl_pw_aff_free(pwaff2
);
2567 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
2568 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
2570 for (i
= 0; i
< pwaff1
->n
; ++i
) {
2571 set
= isl_set_copy(pwaff1
->p
[i
].set
);
2572 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2573 struct isl_set
*common
;
2576 common
= isl_set_intersect(
2577 isl_set_copy(pwaff1
->p
[i
].set
),
2578 isl_set_copy(pwaff2
->p
[j
].set
));
2579 better
= isl_set_from_basic_set(cmp(
2580 isl_aff_copy(pwaff2
->p
[j
].aff
),
2581 isl_aff_copy(pwaff1
->p
[i
].aff
)));
2582 better
= isl_set_intersect(common
, better
);
2583 if (isl_set_plain_is_empty(better
)) {
2584 isl_set_free(better
);
2587 set
= isl_set_subtract(set
, isl_set_copy(better
));
2589 res
= isl_pw_aff_add_piece(res
, better
,
2590 isl_aff_copy(pwaff2
->p
[j
].aff
));
2592 res
= isl_pw_aff_add_piece(res
, set
,
2593 isl_aff_copy(pwaff1
->p
[i
].aff
));
2596 for (j
= 0; j
< pwaff2
->n
; ++j
) {
2597 set
= isl_set_copy(pwaff2
->p
[j
].set
);
2598 for (i
= 0; i
< pwaff1
->n
; ++i
)
2599 set
= isl_set_subtract(set
,
2600 isl_set_copy(pwaff1
->p
[i
].set
));
2601 res
= isl_pw_aff_add_piece(res
, set
,
2602 isl_aff_copy(pwaff2
->p
[j
].aff
));
2605 isl_pw_aff_free(pwaff1
);
2606 isl_pw_aff_free(pwaff2
);
2610 isl_pw_aff_free(pwaff1
);
2611 isl_pw_aff_free(pwaff2
);
2615 /* Compute a piecewise quasi-affine expression with a domain that
2616 * is the union of those of pwaff1 and pwaff2 and such that on each
2617 * cell, the quasi-affine expression is the maximum of those of pwaff1
2618 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2619 * cell, then the associated expression is the defined one.
2621 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2622 __isl_take isl_pw_aff
*pwaff2
)
2624 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
2627 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
2628 __isl_take isl_pw_aff
*pwaff2
)
2630 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2634 /* Compute a piecewise quasi-affine expression with a domain that
2635 * is the union of those of pwaff1 and pwaff2 and such that on each
2636 * cell, the quasi-affine expression is the minimum of those of pwaff1
2637 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2638 * cell, then the associated expression is the defined one.
2640 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2641 __isl_take isl_pw_aff
*pwaff2
)
2643 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
2646 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
2647 __isl_take isl_pw_aff
*pwaff2
)
2649 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
2653 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
2654 __isl_take isl_pw_aff
*pwaff2
, int max
)
2657 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
2659 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
2662 /* Construct a map with as domain the domain of pwaff and
2663 * one-dimensional range corresponding to the affine expressions.
2665 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2674 dim
= isl_pw_aff_get_space(pwaff
);
2675 map
= isl_map_empty(dim
);
2677 for (i
= 0; i
< pwaff
->n
; ++i
) {
2678 isl_basic_map
*bmap
;
2681 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
2682 map_i
= isl_map_from_basic_map(bmap
);
2683 map_i
= isl_map_intersect_domain(map_i
,
2684 isl_set_copy(pwaff
->p
[i
].set
));
2685 map
= isl_map_union_disjoint(map
, map_i
);
2688 isl_pw_aff_free(pwaff
);
2693 /* Construct a map with as domain the domain of pwaff and
2694 * one-dimensional range corresponding to the affine expressions.
2696 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2700 if (isl_space_is_set(pwaff
->dim
))
2701 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2702 "space of input is not a map", goto error
);
2703 return map_from_pw_aff(pwaff
);
2705 isl_pw_aff_free(pwaff
);
2709 /* Construct a one-dimensional set with as parameter domain
2710 * the domain of pwaff and the single set dimension
2711 * corresponding to the affine expressions.
2713 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
2717 if (!isl_space_is_set(pwaff
->dim
))
2718 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2719 "space of input is not a set", goto error
);
2720 return map_from_pw_aff(pwaff
);
2722 isl_pw_aff_free(pwaff
);
2726 /* Return a set containing those elements in the domain
2727 * of pwaff where it is non-negative.
2729 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
2737 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2739 for (i
= 0; i
< pwaff
->n
; ++i
) {
2740 isl_basic_set
*bset
;
2744 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2745 bset
= aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2747 set_i
= isl_set_from_basic_set(bset
);
2748 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
2749 set
= isl_set_union_disjoint(set
, set_i
);
2752 isl_pw_aff_free(pwaff
);
2757 /* Return a set containing those elements in the domain
2758 * of pwaff where it is zero (if complement is 0) or not zero
2759 * (if complement is 1).
2761 * The pieces with a NaN never belong to the result since
2762 * NaN is neither zero nor non-zero.
2764 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
2773 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
2775 for (i
= 0; i
< pwaff
->n
; ++i
) {
2776 isl_basic_set
*bset
;
2777 isl_set
*set_i
, *zero
;
2780 if (isl_aff_is_nan(pwaff
->p
[i
].aff
))
2783 rational
= isl_set_has_rational(pwaff
->p
[i
].set
);
2784 bset
= aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
),
2786 zero
= isl_set_from_basic_set(bset
);
2787 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
2789 set_i
= isl_set_subtract(set_i
, zero
);
2791 set_i
= isl_set_intersect(set_i
, zero
);
2792 set
= isl_set_union_disjoint(set
, set_i
);
2795 isl_pw_aff_free(pwaff
);
2800 /* Return a set containing those elements in the domain
2801 * of pwaff where it is zero.
2803 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
2805 return pw_aff_zero_set(pwaff
, 0);
2808 /* Return a set containing those elements in the domain
2809 * of pwaff where it is not zero.
2811 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
2813 return pw_aff_zero_set(pwaff
, 1);
2816 /* Return a set containing those elements in the shared domain
2817 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2819 * We compute the difference on the shared domain and then construct
2820 * the set of values where this difference is non-negative.
2821 * If strict is set, we first subtract 1 from the difference.
2822 * If equal is set, we only return the elements where pwaff1 and pwaff2
2825 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
2826 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
2828 isl_set
*set1
, *set2
;
2830 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
2831 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
2832 set1
= isl_set_intersect(set1
, set2
);
2833 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
2834 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
2835 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
2838 isl_space
*dim
= isl_set_get_space(set1
);
2840 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2841 aff
= isl_aff_add_constant_si(aff
, -1);
2842 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
2847 return isl_pw_aff_zero_set(pwaff1
);
2848 return isl_pw_aff_nonneg_set(pwaff1
);
2851 /* Return a set containing those elements in the shared domain
2852 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2854 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2855 __isl_take isl_pw_aff
*pwaff2
)
2857 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
2860 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
2861 __isl_take isl_pw_aff
*pwaff2
)
2863 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
2866 /* Return a set containing those elements in the shared domain
2867 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2869 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2870 __isl_take isl_pw_aff
*pwaff2
)
2872 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
2875 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
2876 __isl_take isl_pw_aff
*pwaff2
)
2878 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
2881 /* Return a set containing those elements in the shared domain
2882 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2884 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2885 __isl_take isl_pw_aff
*pwaff2
)
2887 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
2890 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
2891 __isl_take isl_pw_aff
*pwaff2
)
2893 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
2896 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
2897 __isl_take isl_pw_aff
*pwaff2
)
2899 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
2902 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
2903 __isl_take isl_pw_aff
*pwaff2
)
2905 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2908 /* Return a set containing those elements in the shared domain
2909 * of the elements of list1 and list2 where each element in list1
2910 * has the relation specified by "fn" with each element in list2.
2912 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2913 __isl_take isl_pw_aff_list
*list2
,
2914 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2915 __isl_take isl_pw_aff
*pwaff2
))
2921 if (!list1
|| !list2
)
2924 ctx
= isl_pw_aff_list_get_ctx(list1
);
2925 if (list1
->n
< 1 || list2
->n
< 1)
2926 isl_die(ctx
, isl_error_invalid
,
2927 "list should contain at least one element", goto error
);
2929 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2930 for (i
= 0; i
< list1
->n
; ++i
)
2931 for (j
= 0; j
< list2
->n
; ++j
) {
2934 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2935 isl_pw_aff_copy(list2
->p
[j
]));
2936 set
= isl_set_intersect(set
, set_ij
);
2939 isl_pw_aff_list_free(list1
);
2940 isl_pw_aff_list_free(list2
);
2943 isl_pw_aff_list_free(list1
);
2944 isl_pw_aff_list_free(list2
);
2948 /* Return a set containing those elements in the shared domain
2949 * of the elements of list1 and list2 where each element in list1
2950 * is equal to each element in list2.
2952 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2953 __isl_take isl_pw_aff_list
*list2
)
2955 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2958 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2959 __isl_take isl_pw_aff_list
*list2
)
2961 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2964 /* Return a set containing those elements in the shared domain
2965 * of the elements of list1 and list2 where each element in list1
2966 * is less than or equal to each element in list2.
2968 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2969 __isl_take isl_pw_aff_list
*list2
)
2971 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2974 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2975 __isl_take isl_pw_aff_list
*list2
)
2977 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2980 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2981 __isl_take isl_pw_aff_list
*list2
)
2983 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
2986 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
2987 __isl_take isl_pw_aff_list
*list2
)
2989 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
2993 /* Return a set containing those elements in the shared domain
2994 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2996 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2997 __isl_take isl_pw_aff
*pwaff2
)
2999 isl_set
*set_lt
, *set_gt
;
3001 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
3002 isl_pw_aff_copy(pwaff2
));
3003 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
3004 return isl_set_union_disjoint(set_lt
, set_gt
);
3007 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
3008 __isl_take isl_pw_aff
*pwaff2
)
3010 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
3013 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
3018 if (isl_int_is_one(v
))
3020 if (!isl_int_is_pos(v
))
3021 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
3022 "factor needs to be positive",
3023 return isl_pw_aff_free(pwaff
));
3024 pwaff
= isl_pw_aff_cow(pwaff
);
3030 for (i
= 0; i
< pwaff
->n
; ++i
) {
3031 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
3032 if (!pwaff
->p
[i
].aff
)
3033 return isl_pw_aff_free(pwaff
);
3039 /* Divide "pa" by "f".
3041 __isl_give isl_pw_aff
*isl_pw_aff_scale_down_val(__isl_take isl_pw_aff
*pa
,
3042 __isl_take isl_val
*f
)
3049 if (isl_val_is_one(f
)) {
3054 if (!isl_val_is_rat(f
))
3055 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3056 "expecting rational factor", goto error
);
3057 if (!isl_val_is_pos(f
))
3058 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
3059 "factor needs to be positive", goto error
);
3061 pa
= isl_pw_aff_cow(pa
);
3067 for (i
= 0; i
< pa
->n
; ++i
) {
3068 pa
->p
[i
].aff
= isl_aff_scale_down_val(pa
->p
[i
].aff
,
3077 isl_pw_aff_free(pa
);
3082 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
3086 pwaff
= isl_pw_aff_cow(pwaff
);
3092 for (i
= 0; i
< pwaff
->n
; ++i
) {
3093 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
3094 if (!pwaff
->p
[i
].aff
)
3095 return isl_pw_aff_free(pwaff
);
3101 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
3105 pwaff
= isl_pw_aff_cow(pwaff
);
3111 for (i
= 0; i
< pwaff
->n
; ++i
) {
3112 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
3113 if (!pwaff
->p
[i
].aff
)
3114 return isl_pw_aff_free(pwaff
);
3120 /* Assuming that "cond1" and "cond2" are disjoint,
3121 * return an affine expression that is equal to pwaff1 on cond1
3122 * and to pwaff2 on cond2.
3124 static __isl_give isl_pw_aff
*isl_pw_aff_select(
3125 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
3126 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
3128 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
3129 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
3131 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
3134 /* Return an affine expression that is equal to pwaff_true for elements
3135 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3137 * That is, return cond ? pwaff_true : pwaff_false;
3139 * If "cond" involves and NaN, then we conservatively return a NaN
3140 * on its entire domain. In principle, we could consider the pieces
3141 * where it is NaN separately from those where it is not.
3143 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
3144 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
3146 isl_set
*cond_true
, *cond_false
;
3150 if (isl_pw_aff_involves_nan(cond
)) {
3151 isl_space
*space
= isl_pw_aff_get_domain_space(cond
);
3152 isl_local_space
*ls
= isl_local_space_from_space(space
);
3153 isl_pw_aff_free(cond
);
3154 isl_pw_aff_free(pwaff_true
);
3155 isl_pw_aff_free(pwaff_false
);
3156 return isl_pw_aff_nan_on_domain(ls
);
3159 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
3160 cond_false
= isl_pw_aff_zero_set(cond
);
3161 return isl_pw_aff_select(cond_true
, pwaff_true
,
3162 cond_false
, pwaff_false
);
3164 isl_pw_aff_free(cond
);
3165 isl_pw_aff_free(pwaff_true
);
3166 isl_pw_aff_free(pwaff_false
);
3170 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
3175 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
3178 /* Check whether pwaff is a piecewise constant.
3180 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
3187 for (i
= 0; i
< pwaff
->n
; ++i
) {
3188 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
3189 if (is_cst
< 0 || !is_cst
)
3196 /* Return the product of "aff1" and "aff2".
3198 * If either of the two is NaN, then the result is NaN.
3200 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3202 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
3203 __isl_take isl_aff
*aff2
)
3208 if (isl_aff_is_nan(aff1
)) {
3212 if (isl_aff_is_nan(aff2
)) {
3217 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
3218 return isl_aff_mul(aff2
, aff1
);
3220 if (!isl_aff_is_cst(aff2
))
3221 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
3222 "at least one affine expression should be constant",
3225 aff1
= isl_aff_cow(aff1
);
3229 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
3230 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
3240 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3242 * If either of the two is NaN, then the result is NaN.
3244 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
3245 __isl_take isl_aff
*aff2
)
3253 if (isl_aff_is_nan(aff1
)) {
3257 if (isl_aff_is_nan(aff2
)) {
3262 is_cst
= isl_aff_is_cst(aff2
);
3266 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
3267 "second argument should be a constant", goto error
);
3272 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
3274 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3275 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3278 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
3279 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
3282 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
3283 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
3294 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3295 __isl_take isl_pw_aff
*pwaff2
)
3297 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
3300 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
3301 __isl_take isl_pw_aff
*pwaff2
)
3303 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
3306 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
3307 __isl_take isl_pw_aff
*pwaff2
)
3309 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
3312 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3313 __isl_take isl_pw_aff
*pwaff2
)
3315 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
3318 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
3319 __isl_take isl_pw_aff
*pwaff2
)
3321 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
3324 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3325 __isl_take isl_pw_aff
*pa2
)
3327 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
3330 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3332 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
3333 __isl_take isl_pw_aff
*pa2
)
3337 is_cst
= isl_pw_aff_is_cst(pa2
);
3341 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3342 "second argument should be a piecewise constant",
3344 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
3346 isl_pw_aff_free(pa1
);
3347 isl_pw_aff_free(pa2
);
3351 /* Compute the quotient of the integer division of "pa1" by "pa2"
3352 * with rounding towards zero.
3353 * "pa2" is assumed to be a piecewise constant.
3355 * In particular, return
3357 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3360 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
3361 __isl_take isl_pw_aff
*pa2
)
3367 is_cst
= isl_pw_aff_is_cst(pa2
);
3371 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3372 "second argument should be a piecewise constant",
3375 pa1
= isl_pw_aff_div(pa1
, pa2
);
3377 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
3378 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
3379 c
= isl_pw_aff_ceil(pa1
);
3380 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
3382 isl_pw_aff_free(pa1
);
3383 isl_pw_aff_free(pa2
);
3387 /* Compute the remainder of the integer division of "pa1" by "pa2"
3388 * with rounding towards zero.
3389 * "pa2" is assumed to be a piecewise constant.
3391 * In particular, return
3393 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3396 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
3397 __isl_take isl_pw_aff
*pa2
)
3402 is_cst
= isl_pw_aff_is_cst(pa2
);
3406 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
3407 "second argument should be a piecewise constant",
3409 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
3410 res
= isl_pw_aff_mul(pa2
, res
);
3411 res
= isl_pw_aff_sub(pa1
, res
);
3414 isl_pw_aff_free(pa1
);
3415 isl_pw_aff_free(pa2
);
3419 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3420 __isl_take isl_pw_aff
*pwaff2
)
3425 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3426 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3427 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
3428 isl_pw_aff_copy(pwaff2
));
3429 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
3430 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
3433 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
3434 __isl_take isl_pw_aff
*pwaff2
)
3436 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
3439 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3440 __isl_take isl_pw_aff
*pwaff2
)
3445 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
3446 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
3447 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
3448 isl_pw_aff_copy(pwaff2
));
3449 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
3450 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
3453 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
3454 __isl_take isl_pw_aff
*pwaff2
)
3456 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
3459 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
3460 __isl_take isl_pw_aff_list
*list
,
3461 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
3462 __isl_take isl_pw_aff
*pwaff2
))
3471 ctx
= isl_pw_aff_list_get_ctx(list
);
3473 isl_die(ctx
, isl_error_invalid
,
3474 "list should contain at least one element", goto error
);
3476 res
= isl_pw_aff_copy(list
->p
[0]);
3477 for (i
= 1; i
< list
->n
; ++i
)
3478 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
3480 isl_pw_aff_list_free(list
);
3483 isl_pw_aff_list_free(list
);
3487 /* Return an isl_pw_aff that maps each element in the intersection of the
3488 * domains of the elements of list to the minimal corresponding affine
3491 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
3493 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
3496 /* Return an isl_pw_aff that maps each element in the intersection of the
3497 * domains of the elements of list to the maximal corresponding affine
3500 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
3502 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
3505 /* Mark the domains of "pwaff" as rational.
3507 __isl_give isl_pw_aff
*isl_pw_aff_set_rational(__isl_take isl_pw_aff
*pwaff
)
3511 pwaff
= isl_pw_aff_cow(pwaff
);
3517 for (i
= 0; i
< pwaff
->n
; ++i
) {
3518 pwaff
->p
[i
].set
= isl_set_set_rational(pwaff
->p
[i
].set
);
3519 if (!pwaff
->p
[i
].set
)
3520 return isl_pw_aff_free(pwaff
);
3526 /* Mark the domains of the elements of "list" as rational.
3528 __isl_give isl_pw_aff_list
*isl_pw_aff_list_set_rational(
3529 __isl_take isl_pw_aff_list
*list
)
3539 for (i
= 0; i
< n
; ++i
) {
3542 pa
= isl_pw_aff_list_get_pw_aff(list
, i
);
3543 pa
= isl_pw_aff_set_rational(pa
);
3544 list
= isl_pw_aff_list_set_pw_aff(list
, i
, pa
);
3550 /* Do the parameters of "aff" match those of "space"?
3552 int isl_aff_matching_params(__isl_keep isl_aff
*aff
,
3553 __isl_keep isl_space
*space
)
3555 isl_space
*aff_space
;
3561 aff_space
= isl_aff_get_domain_space(aff
);
3563 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3565 isl_space_free(aff_space
);
3569 /* Check that the domain space of "aff" matches "space".
3571 * Return 0 on success and -1 on error.
3573 int isl_aff_check_match_domain_space(__isl_keep isl_aff
*aff
,
3574 __isl_keep isl_space
*space
)
3576 isl_space
*aff_space
;
3582 aff_space
= isl_aff_get_domain_space(aff
);
3584 match
= isl_space_match(space
, isl_dim_param
, aff_space
, isl_dim_param
);
3588 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3589 "parameters don't match", goto error
);
3590 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
3591 aff_space
, isl_dim_set
);
3595 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
3596 "domains don't match", goto error
);
3597 isl_space_free(aff_space
);
3600 isl_space_free(aff_space
);
3606 #define NO_INTERSECT_DOMAIN
3609 #include <isl_multi_templ.c>
3612 #undef NO_INTERSECT_DOMAIN
3614 /* Remove any internal structure of the domain of "ma".
3615 * If there is any such internal structure in the input,
3616 * then the name of the corresponding space is also removed.
3618 __isl_give isl_multi_aff
*isl_multi_aff_flatten_domain(
3619 __isl_take isl_multi_aff
*ma
)
3626 if (!ma
->space
->nested
[0])
3629 space
= isl_multi_aff_get_space(ma
);
3630 space
= isl_space_flatten_domain(space
);
3631 ma
= isl_multi_aff_reset_space(ma
, space
);
3636 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3637 * of the space to its domain.
3639 __isl_give isl_multi_aff
*isl_multi_aff_domain_map(__isl_take isl_space
*space
)
3642 isl_local_space
*ls
;
3647 if (!isl_space_is_map(space
))
3648 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3649 "not a map space", goto error
);
3651 n_in
= isl_space_dim(space
, isl_dim_in
);
3652 space
= isl_space_domain_map(space
);
3654 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3656 isl_space_free(space
);
3660 space
= isl_space_domain(space
);
3661 ls
= isl_local_space_from_space(space
);
3662 for (i
= 0; i
< n_in
; ++i
) {
3665 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3667 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3669 isl_local_space_free(ls
);
3672 isl_space_free(space
);
3676 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3677 * of the space to its range.
3679 __isl_give isl_multi_aff
*isl_multi_aff_range_map(__isl_take isl_space
*space
)
3682 isl_local_space
*ls
;
3687 if (!isl_space_is_map(space
))
3688 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3689 "not a map space", goto error
);
3691 n_in
= isl_space_dim(space
, isl_dim_in
);
3692 n_out
= isl_space_dim(space
, isl_dim_out
);
3693 space
= isl_space_range_map(space
);
3695 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3697 isl_space_free(space
);
3701 space
= isl_space_domain(space
);
3702 ls
= isl_local_space_from_space(space
);
3703 for (i
= 0; i
< n_out
; ++i
) {
3706 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3707 isl_dim_set
, n_in
+ i
);
3708 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3710 isl_local_space_free(ls
);
3713 isl_space_free(space
);
3717 /* Given the space of a set and a range of set dimensions,
3718 * construct an isl_multi_aff that projects out those dimensions.
3720 __isl_give isl_multi_aff
*isl_multi_aff_project_out_map(
3721 __isl_take isl_space
*space
, enum isl_dim_type type
,
3722 unsigned first
, unsigned n
)
3725 isl_local_space
*ls
;
3730 if (!isl_space_is_set(space
))
3731 isl_die(isl_space_get_ctx(space
), isl_error_unsupported
,
3732 "expecting set space", goto error
);
3733 if (type
!= isl_dim_set
)
3734 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3735 "only set dimensions can be projected out", goto error
);
3737 dim
= isl_space_dim(space
, isl_dim_set
);
3738 if (first
+ n
> dim
)
3739 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3740 "range out of bounds", goto error
);
3742 space
= isl_space_from_domain(space
);
3743 space
= isl_space_add_dims(space
, isl_dim_out
, dim
- n
);
3746 return isl_multi_aff_alloc(space
);
3748 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
3749 space
= isl_space_domain(space
);
3750 ls
= isl_local_space_from_space(space
);
3752 for (i
= 0; i
< first
; ++i
) {
3755 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3757 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3760 for (i
= 0; i
< dim
- (first
+ n
); ++i
) {
3763 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
3764 isl_dim_set
, first
+ n
+ i
);
3765 ma
= isl_multi_aff_set_aff(ma
, first
+ i
, aff
);
3768 isl_local_space_free(ls
);
3771 isl_space_free(space
);
3775 /* Given the space of a set and a range of set dimensions,
3776 * construct an isl_pw_multi_aff that projects out those dimensions.
3778 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_project_out_map(
3779 __isl_take isl_space
*space
, enum isl_dim_type type
,
3780 unsigned first
, unsigned n
)
3784 ma
= isl_multi_aff_project_out_map(space
, type
, first
, n
);
3785 return isl_pw_multi_aff_from_multi_aff(ma
);
3788 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3791 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
3792 __isl_take isl_multi_aff
*ma
)
3794 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
3795 return isl_pw_multi_aff_alloc(dom
, ma
);
3798 /* Create a piecewise multi-affine expression in the given space that maps each
3799 * input dimension to the corresponding output dimension.
3801 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
3802 __isl_take isl_space
*space
)
3804 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
3807 /* Add "ma2" to "ma1" and return the result.
3809 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3811 static __isl_give isl_multi_aff
*isl_multi_aff_add_aligned(
3812 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3814 return isl_multi_aff_bin_op(maff1
, maff2
, &isl_aff_add
);
3817 /* Add "ma2" to "ma1" and return the result.
3819 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*ma1
,
3820 __isl_take isl_multi_aff
*ma2
)
3822 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3823 &isl_multi_aff_add_aligned
);
3826 /* Subtract "ma2" from "ma1" and return the result.
3828 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3830 static __isl_give isl_multi_aff
*isl_multi_aff_sub_aligned(
3831 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3833 return isl_multi_aff_bin_op(ma1
, ma2
, &isl_aff_sub
);
3836 /* Subtract "ma2" from "ma1" and return the result.
3838 __isl_give isl_multi_aff
*isl_multi_aff_sub(__isl_take isl_multi_aff
*ma1
,
3839 __isl_take isl_multi_aff
*ma2
)
3841 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
3842 &isl_multi_aff_sub_aligned
);
3845 /* Exploit the equalities in "eq" to simplify the affine expressions.
3847 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
3848 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
3852 maff
= isl_multi_aff_cow(maff
);
3856 for (i
= 0; i
< maff
->n
; ++i
) {
3857 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
3858 isl_basic_set_copy(eq
));
3863 isl_basic_set_free(eq
);
3866 isl_basic_set_free(eq
);
3867 isl_multi_aff_free(maff
);
3871 /* Given f, return floor(f).
3873 __isl_give isl_multi_aff
*isl_multi_aff_floor(__isl_take isl_multi_aff
*ma
)
3877 ma
= isl_multi_aff_cow(ma
);
3881 for (i
= 0; i
< ma
->n
; ++i
) {
3882 ma
->p
[i
] = isl_aff_floor(ma
->p
[i
]);
3884 return isl_multi_aff_free(ma
);
3890 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
3895 maff
= isl_multi_aff_cow(maff
);
3899 for (i
= 0; i
< maff
->n
; ++i
) {
3900 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
3902 return isl_multi_aff_free(maff
);
3908 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
3909 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
3911 maff1
= isl_multi_aff_add(maff1
, maff2
);
3912 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
3916 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
3924 /* Return the set of domain elements where "ma1" is lexicographically
3925 * smaller than or equal to "ma2".
3927 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
3928 __isl_take isl_multi_aff
*ma2
)
3930 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
3933 /* Return the set of domain elements where "ma1" is lexicographically
3934 * greater than or equal to "ma2".
3936 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
3937 __isl_take isl_multi_aff
*ma2
)
3940 isl_map
*map1
, *map2
;
3943 map1
= isl_map_from_multi_aff(ma1
);
3944 map2
= isl_map_from_multi_aff(ma2
);
3945 map
= isl_map_range_product(map1
, map2
);
3946 space
= isl_space_range(isl_map_get_space(map
));
3947 space
= isl_space_domain(isl_space_unwrap(space
));
3948 ge
= isl_map_lex_ge(space
);
3949 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
3951 return isl_map_domain(map
);
3955 #define PW isl_pw_multi_aff
3957 #define EL isl_multi_aff
3959 #define EL_IS_ZERO is_empty
3963 #define IS_ZERO is_empty
3966 #undef DEFAULT_IS_ZERO
3967 #define DEFAULT_IS_ZERO 0
3972 #define NO_INVOLVES_DIMS
3973 #define NO_INSERT_DIMS
3977 #include <isl_pw_templ.c>
3980 #define UNION isl_union_pw_multi_aff
3982 #define PART isl_pw_multi_aff
3984 #define PARTS pw_multi_aff
3985 #define ALIGN_DOMAIN
3989 #include <isl_union_templ.c>
3991 /* Given a function "cmp" that returns the set of elements where
3992 * "ma1" is "better" than "ma2", return the intersection of this
3993 * set with "dom1" and "dom2".
3995 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
3996 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
3997 __isl_keep isl_multi_aff
*ma2
,
3998 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
3999 __isl_take isl_multi_aff
*ma2
))
4005 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
4006 is_empty
= isl_set_plain_is_empty(common
);
4007 if (is_empty
>= 0 && is_empty
)
4010 return isl_set_free(common
);
4011 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
4012 better
= isl_set_intersect(common
, better
);
4017 /* Given a function "cmp" that returns the set of elements where
4018 * "ma1" is "better" than "ma2", return a piecewise multi affine
4019 * expression defined on the union of the definition domains
4020 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4021 * "pma2" on each cell. If only one of the two input functions
4022 * is defined on a given cell, then it is considered the best.
4024 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
4025 __isl_take isl_pw_multi_aff
*pma1
,
4026 __isl_take isl_pw_multi_aff
*pma2
,
4027 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
4028 __isl_take isl_multi_aff
*ma2
))
4031 isl_pw_multi_aff
*res
= NULL
;
4033 isl_set
*set
= NULL
;
4038 ctx
= isl_space_get_ctx(pma1
->dim
);
4039 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
4040 isl_die(ctx
, isl_error_invalid
,
4041 "arguments should live in the same space", goto error
);
4043 if (isl_pw_multi_aff_is_empty(pma1
)) {
4044 isl_pw_multi_aff_free(pma1
);
4048 if (isl_pw_multi_aff_is_empty(pma2
)) {
4049 isl_pw_multi_aff_free(pma2
);
4053 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
4054 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
4056 for (i
= 0; i
< pma1
->n
; ++i
) {
4057 set
= isl_set_copy(pma1
->p
[i
].set
);
4058 for (j
= 0; j
< pma2
->n
; ++j
) {
4062 better
= shared_and_better(pma2
->p
[j
].set
,
4063 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
4064 pma1
->p
[i
].maff
, cmp
);
4065 is_empty
= isl_set_plain_is_empty(better
);
4066 if (is_empty
< 0 || is_empty
) {
4067 isl_set_free(better
);
4072 set
= isl_set_subtract(set
, isl_set_copy(better
));
4074 res
= isl_pw_multi_aff_add_piece(res
, better
,
4075 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4077 res
= isl_pw_multi_aff_add_piece(res
, set
,
4078 isl_multi_aff_copy(pma1
->p
[i
].maff
));
4081 for (j
= 0; j
< pma2
->n
; ++j
) {
4082 set
= isl_set_copy(pma2
->p
[j
].set
);
4083 for (i
= 0; i
< pma1
->n
; ++i
)
4084 set
= isl_set_subtract(set
,
4085 isl_set_copy(pma1
->p
[i
].set
));
4086 res
= isl_pw_multi_aff_add_piece(res
, set
,
4087 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4090 isl_pw_multi_aff_free(pma1
);
4091 isl_pw_multi_aff_free(pma2
);
4095 isl_pw_multi_aff_free(pma1
);
4096 isl_pw_multi_aff_free(pma2
);
4098 return isl_pw_multi_aff_free(res
);
4101 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
4102 __isl_take isl_pw_multi_aff
*pma1
,
4103 __isl_take isl_pw_multi_aff
*pma2
)
4105 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
4108 /* Given two piecewise multi affine expressions, return a piecewise
4109 * multi-affine expression defined on the union of the definition domains
4110 * of the inputs that is equal to the lexicographic maximum of the two
4111 * inputs on each cell. If only one of the two inputs is defined on
4112 * a given cell, then it is considered to be the maximum.
4114 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
4115 __isl_take isl_pw_multi_aff
*pma1
,
4116 __isl_take isl_pw_multi_aff
*pma2
)
4118 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4119 &pw_multi_aff_union_lexmax
);
4122 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
4123 __isl_take isl_pw_multi_aff
*pma1
,
4124 __isl_take isl_pw_multi_aff
*pma2
)
4126 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
4129 /* Given two piecewise multi affine expressions, return a piecewise
4130 * multi-affine expression defined on the union of the definition domains
4131 * of the inputs that is equal to the lexicographic minimum of the two
4132 * inputs on each cell. If only one of the two inputs is defined on
4133 * a given cell, then it is considered to be the minimum.
4135 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
4136 __isl_take isl_pw_multi_aff
*pma1
,
4137 __isl_take isl_pw_multi_aff
*pma2
)
4139 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4140 &pw_multi_aff_union_lexmin
);
4143 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
4144 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4146 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4147 &isl_multi_aff_add
);
4150 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
4151 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4153 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4157 static __isl_give isl_pw_multi_aff
*pw_multi_aff_sub(
4158 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4160 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
4161 &isl_multi_aff_sub
);
4164 /* Subtract "pma2" from "pma1" and return the result.
4166 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_sub(
4167 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4169 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4173 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
4174 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4176 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
4179 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4180 * with the actual sum on the shared domain and
4181 * the defined expression on the symmetric difference of the domains.
4183 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_union_add(
4184 __isl_take isl_union_pw_multi_aff
*upma1
,
4185 __isl_take isl_union_pw_multi_aff
*upma2
)
4187 return isl_union_pw_multi_aff_union_add_(upma1
, upma2
);
4190 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4191 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4193 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
4194 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4198 isl_pw_multi_aff
*res
;
4203 n
= pma1
->n
* pma2
->n
;
4204 space
= isl_space_product(isl_space_copy(pma1
->dim
),
4205 isl_space_copy(pma2
->dim
));
4206 res
= isl_pw_multi_aff_alloc_size(space
, n
);
4208 for (i
= 0; i
< pma1
->n
; ++i
) {
4209 for (j
= 0; j
< pma2
->n
; ++j
) {
4213 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
4214 isl_set_copy(pma2
->p
[j
].set
));
4215 ma
= isl_multi_aff_product(
4216 isl_multi_aff_copy(pma1
->p
[i
].maff
),
4217 isl_multi_aff_copy(pma2
->p
[j
].maff
));
4218 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
4222 isl_pw_multi_aff_free(pma1
);
4223 isl_pw_multi_aff_free(pma2
);
4226 isl_pw_multi_aff_free(pma1
);
4227 isl_pw_multi_aff_free(pma2
);
4231 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
4232 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
4234 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
4235 &pw_multi_aff_product
);
4238 /* Construct a map mapping the domain of the piecewise multi-affine expression
4239 * to its range, with each dimension in the range equated to the
4240 * corresponding affine expression on its cell.
4242 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4250 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
4252 for (i
= 0; i
< pma
->n
; ++i
) {
4253 isl_multi_aff
*maff
;
4254 isl_basic_map
*bmap
;
4257 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
4258 bmap
= isl_basic_map_from_multi_aff(maff
);
4259 map_i
= isl_map_from_basic_map(bmap
);
4260 map_i
= isl_map_intersect_domain(map_i
,
4261 isl_set_copy(pma
->p
[i
].set
));
4262 map
= isl_map_union_disjoint(map
, map_i
);
4265 isl_pw_multi_aff_free(pma
);
4269 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
4274 if (!isl_space_is_set(pma
->dim
))
4275 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
4276 "isl_pw_multi_aff cannot be converted into an isl_set",
4279 return isl_map_from_pw_multi_aff(pma
);
4281 isl_pw_multi_aff_free(pma
);
4285 /* Given a basic map with a single output dimension that is defined
4286 * in terms of the parameters and input dimensions using an equality,
4287 * extract an isl_aff that expresses the output dimension in terms
4288 * of the parameters and input dimensions.
4289 * Note that this expression may involve integer divisions defined
4290 * in terms of parameters and input dimensions.
4292 * This function shares some similarities with
4293 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4295 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
4296 __isl_take isl_basic_map
*bmap
)
4301 isl_local_space
*ls
;
4306 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
4307 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4308 "basic map should have a single output dimension",
4310 eq
= isl_basic_map_output_defining_equality(bmap
, 0);
4311 if (eq
>= bmap
->n_eq
)
4312 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
4313 "unable to find suitable equality", goto error
);
4314 ls
= isl_basic_map_get_local_space(bmap
);
4315 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
4318 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
4319 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4320 if (isl_int_is_neg(bmap
->eq
[eq
][offset
])) {
4321 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4322 isl_seq_cpy(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4325 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[eq
], offset
);
4326 isl_seq_neg(aff
->v
->el
+ 1 + offset
, bmap
->eq
[eq
] + offset
+ 1,
4329 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[eq
][offset
]);
4330 isl_basic_map_free(bmap
);
4332 aff
= isl_aff_remove_unused_divs(aff
);
4335 isl_basic_map_free(bmap
);
4339 /* Given a basic map where each output dimension is defined
4340 * in terms of the parameters and input dimensions using an equality,
4341 * extract an isl_multi_aff that expresses the output dimensions in terms
4342 * of the parameters and input dimensions.
4344 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
4345 __isl_take isl_basic_map
*bmap
)
4354 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
4355 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
4357 for (i
= 0; i
< n_out
; ++i
) {
4358 isl_basic_map
*bmap_i
;
4361 bmap_i
= isl_basic_map_copy(bmap
);
4362 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
4363 i
+ 1, n_out
- (1 + i
));
4364 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
4365 aff
= extract_isl_aff_from_basic_map(bmap_i
);
4366 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4369 isl_basic_map_free(bmap
);
4374 /* Given a basic set where each set dimension is defined
4375 * in terms of the parameters using an equality,
4376 * extract an isl_multi_aff that expresses the set dimensions in terms
4377 * of the parameters.
4379 __isl_give isl_multi_aff
*isl_multi_aff_from_basic_set_equalities(
4380 __isl_take isl_basic_set
*bset
)
4382 return extract_isl_multi_aff_from_basic_map(bset
);
4385 /* Create an isl_pw_multi_aff that is equivalent to
4386 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4387 * The given basic map is such that each output dimension is defined
4388 * in terms of the parameters and input dimensions using an equality.
4390 * Since some applications expect the result of isl_pw_multi_aff_from_map
4391 * to only contain integer affine expressions, we compute the floor
4392 * of the expression before returning.
4394 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
4395 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
4399 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
4400 ma
= isl_multi_aff_floor(ma
);
4401 return isl_pw_multi_aff_alloc(domain
, ma
);
4404 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4405 * This obviously only works if the input "map" is single-valued.
4406 * If so, we compute the lexicographic minimum of the image in the form
4407 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4408 * to its lexicographic minimum.
4409 * If the input is not single-valued, we produce an error.
4411 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_base(
4412 __isl_take isl_map
*map
)
4416 isl_pw_multi_aff
*pma
;
4418 sv
= isl_map_is_single_valued(map
);
4422 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
4423 "map is not single-valued", goto error
);
4424 map
= isl_map_make_disjoint(map
);
4428 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
4430 for (i
= 0; i
< map
->n
; ++i
) {
4431 isl_pw_multi_aff
*pma_i
;
4432 isl_basic_map
*bmap
;
4433 bmap
= isl_basic_map_copy(map
->p
[i
]);
4434 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
4435 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
4445 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4446 * taking into account that the output dimension at position "d"
4447 * can be represented as
4449 * x = floor((e(...) + c1) / m)
4451 * given that constraint "i" is of the form
4453 * e(...) + c1 - m x >= 0
4456 * Let "map" be of the form
4460 * We construct a mapping
4462 * A -> [A -> x = floor(...)]
4464 * apply that to the map, obtaining
4466 * [A -> x = floor(...)] -> B
4468 * and equate dimension "d" to x.
4469 * We then compute a isl_pw_multi_aff representation of the resulting map
4470 * and plug in the mapping above.
4472 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_div(
4473 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
)
4477 isl_local_space
*ls
;
4485 isl_pw_multi_aff
*pma
;
4488 is_set
= isl_map_is_set(map
);
4490 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4491 ctx
= isl_map_get_ctx(map
);
4492 space
= isl_space_domain(isl_map_get_space(map
));
4493 n_in
= isl_space_dim(space
, isl_dim_set
);
4494 n
= isl_space_dim(space
, isl_dim_all
);
4496 v
= isl_vec_alloc(ctx
, 1 + 1 + n
);
4498 isl_int_neg(v
->el
[0], hull
->ineq
[i
][offset
+ d
]);
4499 isl_seq_cpy(v
->el
+ 1, hull
->ineq
[i
], 1 + n
);
4501 isl_basic_map_free(hull
);
4503 ls
= isl_local_space_from_space(isl_space_copy(space
));
4504 aff
= isl_aff_alloc_vec(ls
, v
);
4505 aff
= isl_aff_floor(aff
);
4507 isl_space_free(space
);
4508 ma
= isl_multi_aff_from_aff(aff
);
4510 ma
= isl_multi_aff_identity(isl_space_map_from_set(space
));
4511 ma
= isl_multi_aff_range_product(ma
,
4512 isl_multi_aff_from_aff(aff
));
4515 insert
= isl_map_from_multi_aff(isl_multi_aff_copy(ma
));
4516 map
= isl_map_apply_domain(map
, insert
);
4517 map
= isl_map_equate(map
, isl_dim_in
, n_in
, isl_dim_out
, d
);
4518 pma
= isl_pw_multi_aff_from_map(map
);
4519 pma
= isl_pw_multi_aff_pullback_multi_aff(pma
, ma
);
4524 /* Is constraint "c" of the form
4526 * e(...) + c1 - m x >= 0
4530 * -e(...) + c2 + m x >= 0
4532 * where m > 1 and e only depends on parameters and input dimemnsions?
4534 * "offset" is the offset of the output dimensions
4535 * "pos" is the position of output dimension x.
4537 static int is_potential_div_constraint(isl_int
*c
, int offset
, int d
, int total
)
4539 if (isl_int_is_zero(c
[offset
+ d
]))
4541 if (isl_int_is_one(c
[offset
+ d
]))
4543 if (isl_int_is_negone(c
[offset
+ d
]))
4545 if (isl_seq_first_non_zero(c
+ offset
, d
) != -1)
4547 if (isl_seq_first_non_zero(c
+ offset
+ d
+ 1,
4548 total
- (offset
+ d
+ 1)) != -1)
4553 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4555 * As a special case, we first check if there is any pair of constraints,
4556 * shared by all the basic maps in "map" that force a given dimension
4557 * to be equal to the floor of some affine combination of the input dimensions.
4559 * In particular, if we can find two constraints
4561 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4565 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4567 * where m > 1 and e only depends on parameters and input dimemnsions,
4570 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4572 * then we know that we can take
4574 * x = floor((e(...) + c1) / m)
4576 * without having to perform any computation.
4578 * Note that we know that
4582 * If c1 + c2 were 0, then we would have detected an equality during
4583 * simplification. If c1 + c2 were negative, then we would have detected
4586 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_check_div(
4587 __isl_take isl_map
*map
)
4593 isl_basic_map
*hull
;
4595 hull
= isl_map_unshifted_simple_hull(isl_map_copy(map
));
4600 dim
= isl_map_dim(map
, isl_dim_out
);
4601 offset
= isl_basic_map_offset(hull
, isl_dim_out
);
4602 total
= 1 + isl_basic_map_total_dim(hull
);
4604 for (d
= 0; d
< dim
; ++d
) {
4605 for (i
= 0; i
< n
; ++i
) {
4606 if (!is_potential_div_constraint(hull
->ineq
[i
],
4609 for (j
= i
+ 1; j
< n
; ++j
) {
4610 if (!isl_seq_is_neg(hull
->ineq
[i
] + 1,
4611 hull
->ineq
[j
] + 1, total
- 1))
4613 isl_int_add(sum
, hull
->ineq
[i
][0],
4615 if (isl_int_abs_lt(sum
,
4616 hull
->ineq
[i
][offset
+ d
]))
4623 if (isl_int_is_pos(hull
->ineq
[j
][offset
+ d
]))
4625 return pw_multi_aff_from_map_div(map
, hull
, d
, j
);
4629 isl_basic_map_free(hull
);
4630 return pw_multi_aff_from_map_base(map
);
4633 isl_basic_map_free(hull
);
4637 /* Given an affine expression
4639 * [A -> B] -> f(A,B)
4641 * construct an isl_multi_aff
4645 * such that dimension "d" in B' is set to "aff" and the remaining
4646 * dimensions are set equal to the corresponding dimensions in B.
4647 * "n_in" is the dimension of the space A.
4648 * "n_out" is the dimension of the space B.
4650 * If "is_set" is set, then the affine expression is of the form
4654 * and we construct an isl_multi_aff
4658 static __isl_give isl_multi_aff
*range_map(__isl_take isl_aff
*aff
, int d
,
4659 unsigned n_in
, unsigned n_out
, int is_set
)
4663 isl_space
*space
, *space2
;
4664 isl_local_space
*ls
;
4666 space
= isl_aff_get_domain_space(aff
);
4667 ls
= isl_local_space_from_space(isl_space_copy(space
));
4668 space2
= isl_space_copy(space
);
4670 space2
= isl_space_range(isl_space_unwrap(space2
));
4671 space
= isl_space_map_from_domain_and_range(space
, space2
);
4672 ma
= isl_multi_aff_alloc(space
);
4673 ma
= isl_multi_aff_set_aff(ma
, d
, aff
);
4675 for (i
= 0; i
< n_out
; ++i
) {
4678 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
4679 isl_dim_set
, n_in
+ i
);
4680 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
4683 isl_local_space_free(ls
);
4688 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4689 * taking into account that the dimension at position "d" can be written as
4691 * x = m a + f(..) (1)
4693 * where m is equal to "gcd".
4694 * "i" is the index of the equality in "hull" that defines f(..).
4695 * In particular, the equality is of the form
4697 * f(..) - x + m g(existentials) = 0
4701 * -f(..) + x + m g(existentials) = 0
4703 * We basically plug (1) into "map", resulting in a map with "a"
4704 * in the range instead of "x". The corresponding isl_pw_multi_aff
4705 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4707 * Specifically, given the input map
4711 * We first wrap it into a set
4715 * and define (1) on top of the corresponding space, resulting in "aff".
4716 * We use this to create an isl_multi_aff that maps the output position "d"
4717 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4718 * We plug this into the wrapped map, unwrap the result and compute the
4719 * corresponding isl_pw_multi_aff.
4720 * The result is an expression
4728 * so that we can plug that into "aff", after extending the latter to
4734 * If "map" is actually a set, then there is no "A" space, meaning
4735 * that we do not need to perform any wrapping, and that the result
4736 * of the recursive call is of the form
4740 * which is plugged into a mapping of the form
4744 static __isl_give isl_pw_multi_aff
*pw_multi_aff_from_map_stride(
4745 __isl_take isl_map
*map
, __isl_take isl_basic_map
*hull
, int d
, int i
,
4750 isl_local_space
*ls
;
4753 isl_pw_multi_aff
*pma
, *id
;
4759 is_set
= isl_map_is_set(map
);
4761 n_in
= isl_basic_map_dim(hull
, isl_dim_in
);
4762 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4763 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4768 set
= isl_map_wrap(map
);
4769 space
= isl_space_map_from_set(isl_set_get_space(set
));
4770 ma
= isl_multi_aff_identity(space
);
4771 ls
= isl_local_space_from_space(isl_set_get_space(set
));
4772 aff
= isl_aff_alloc(ls
);
4774 isl_int_set_si(aff
->v
->el
[0], 1);
4775 if (isl_int_is_one(hull
->eq
[i
][o_out
+ d
]))
4776 isl_seq_neg(aff
->v
->el
+ 1, hull
->eq
[i
],
4779 isl_seq_cpy(aff
->v
->el
+ 1, hull
->eq
[i
],
4781 isl_int_set(aff
->v
->el
[1 + o_out
+ d
], gcd
);
4783 ma
= isl_multi_aff_set_aff(ma
, n_in
+ d
, isl_aff_copy(aff
));
4784 set
= isl_set_preimage_multi_aff(set
, ma
);
4786 ma
= range_map(aff
, d
, n_in
, n_out
, is_set
);
4791 map
= isl_set_unwrap(set
);
4792 pma
= isl_pw_multi_aff_from_map(set
);
4795 space
= isl_pw_multi_aff_get_domain_space(pma
);
4796 space
= isl_space_map_from_set(space
);
4797 id
= isl_pw_multi_aff_identity(space
);
4798 pma
= isl_pw_multi_aff_range_product(id
, pma
);
4800 id
= isl_pw_multi_aff_from_multi_aff(ma
);
4801 pma
= isl_pw_multi_aff_pullback_pw_multi_aff(id
, pma
);
4803 isl_basic_map_free(hull
);
4807 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4809 * As a special case, we first check if all output dimensions are uniquely
4810 * defined in terms of the parameters and input dimensions over the entire
4811 * domain. If so, we extract the desired isl_pw_multi_aff directly
4812 * from the affine hull of "map" and its domain.
4814 * Otherwise, we check if any of the output dimensions is "strided".
4815 * That is, we check if can be written as
4819 * with m greater than 1, a some combination of existentiall quantified
4820 * variables and f and expression in the parameters and input dimensions.
4821 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4823 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4826 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
4830 isl_basic_map
*hull
;
4840 hull
= isl_map_affine_hull(isl_map_copy(map
));
4841 sv
= isl_basic_map_plain_is_single_valued(hull
);
4843 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
4845 hull
= isl_basic_map_free(hull
);
4849 n_div
= isl_basic_map_dim(hull
, isl_dim_div
);
4850 o_div
= isl_basic_map_offset(hull
, isl_dim_div
);
4853 isl_basic_map_free(hull
);
4854 return pw_multi_aff_from_map_check_div(map
);
4859 n_out
= isl_basic_map_dim(hull
, isl_dim_out
);
4860 o_out
= isl_basic_map_offset(hull
, isl_dim_out
);
4862 for (i
= 0; i
< n_out
; ++i
) {
4863 for (j
= 0; j
< hull
->n_eq
; ++j
) {
4864 isl_int
*eq
= hull
->eq
[j
];
4865 isl_pw_multi_aff
*res
;
4867 if (!isl_int_is_one(eq
[o_out
+ i
]) &&
4868 !isl_int_is_negone(eq
[o_out
+ i
]))
4870 if (isl_seq_first_non_zero(eq
+ o_out
, i
) != -1)
4872 if (isl_seq_first_non_zero(eq
+ o_out
+ i
+ 1,
4873 n_out
- (i
+ 1)) != -1)
4875 isl_seq_gcd(eq
+ o_div
, n_div
, &gcd
);
4876 if (isl_int_is_zero(gcd
))
4878 if (isl_int_is_one(gcd
))
4881 res
= pw_multi_aff_from_map_stride(map
, hull
,
4889 isl_basic_map_free(hull
);
4890 return pw_multi_aff_from_map_check_div(map
);
4896 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
4898 return isl_pw_multi_aff_from_map(set
);
4901 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4904 static int pw_multi_aff_from_map(__isl_take isl_map
*map
, void *user
)
4906 isl_union_pw_multi_aff
**upma
= user
;
4907 isl_pw_multi_aff
*pma
;
4909 pma
= isl_pw_multi_aff_from_map(map
);
4910 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
4912 return *upma
? 0 : -1;
4915 /* Try and create an isl_union_pw_multi_aff that is equivalent
4916 * to the given isl_union_map.
4917 * The isl_union_map is required to be single-valued in each space.
4918 * Otherwise, an error is produced.
4920 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_map(
4921 __isl_take isl_union_map
*umap
)
4924 isl_union_pw_multi_aff
*upma
;
4926 space
= isl_union_map_get_space(umap
);
4927 upma
= isl_union_pw_multi_aff_empty(space
);
4928 if (isl_union_map_foreach_map(umap
, &pw_multi_aff_from_map
, &upma
) < 0)
4929 upma
= isl_union_pw_multi_aff_free(upma
);
4930 isl_union_map_free(umap
);
4935 /* Try and create an isl_union_pw_multi_aff that is equivalent
4936 * to the given isl_union_set.
4937 * The isl_union_set is required to be a singleton in each space.
4938 * Otherwise, an error is produced.
4940 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_union_set(
4941 __isl_take isl_union_set
*uset
)
4943 return isl_union_pw_multi_aff_from_union_map(uset
);
4946 /* Return the piecewise affine expression "set ? 1 : 0".
4948 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
4951 isl_space
*space
= isl_set_get_space(set
);
4952 isl_local_space
*ls
= isl_local_space_from_space(space
);
4953 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
4954 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
4956 one
= isl_aff_add_constant_si(one
, 1);
4957 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
4958 set
= isl_set_complement(set
);
4959 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
4964 /* Plug in "subs" for dimension "type", "pos" of "aff".
4966 * Let i be the dimension to replace and let "subs" be of the form
4970 * and "aff" of the form
4976 * (a f + d g')/(m d)
4978 * where g' is the result of plugging in "subs" in each of the integer
4981 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
4982 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
4987 aff
= isl_aff_cow(aff
);
4989 return isl_aff_free(aff
);
4991 ctx
= isl_aff_get_ctx(aff
);
4992 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
4993 isl_die(ctx
, isl_error_invalid
,
4994 "spaces don't match", return isl_aff_free(aff
));
4995 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
4996 isl_die(ctx
, isl_error_unsupported
,
4997 "cannot handle divs yet", return isl_aff_free(aff
));
4999 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
5001 return isl_aff_free(aff
);
5003 aff
->v
= isl_vec_cow(aff
->v
);
5005 return isl_aff_free(aff
);
5007 pos
+= isl_local_space_offset(aff
->ls
, type
);
5010 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
5011 aff
->v
->size
, subs
->v
->size
, v
);
5017 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5018 * expressions in "maff".
5020 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
5021 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
5022 __isl_keep isl_aff
*subs
)
5026 maff
= isl_multi_aff_cow(maff
);
5028 return isl_multi_aff_free(maff
);
5030 if (type
== isl_dim_in
)
5033 for (i
= 0; i
< maff
->n
; ++i
) {
5034 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
5036 return isl_multi_aff_free(maff
);
5042 /* Plug in "subs" for dimension "type", "pos" of "pma".
5044 * pma is of the form
5048 * while subs is of the form
5050 * v' = B_j(v) -> S_j
5052 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5053 * has a contribution in the result, in particular
5055 * C_ij(S_j) -> M_i(S_j)
5057 * Note that plugging in S_j in C_ij may also result in an empty set
5058 * and this contribution should simply be discarded.
5060 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
5061 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
5062 __isl_keep isl_pw_aff
*subs
)
5065 isl_pw_multi_aff
*res
;
5068 return isl_pw_multi_aff_free(pma
);
5070 n
= pma
->n
* subs
->n
;
5071 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
5073 for (i
= 0; i
< pma
->n
; ++i
) {
5074 for (j
= 0; j
< subs
->n
; ++j
) {
5076 isl_multi_aff
*res_ij
;
5079 common
= isl_set_intersect(
5080 isl_set_copy(pma
->p
[i
].set
),
5081 isl_set_copy(subs
->p
[j
].set
));
5082 common
= isl_set_substitute(common
,
5083 type
, pos
, subs
->p
[j
].aff
);
5084 empty
= isl_set_plain_is_empty(common
);
5085 if (empty
< 0 || empty
) {
5086 isl_set_free(common
);
5092 res_ij
= isl_multi_aff_substitute(
5093 isl_multi_aff_copy(pma
->p
[i
].maff
),
5094 type
, pos
, subs
->p
[j
].aff
);
5096 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5100 isl_pw_multi_aff_free(pma
);
5103 isl_pw_multi_aff_free(pma
);
5104 isl_pw_multi_aff_free(res
);
5108 /* Compute the preimage of a range of dimensions in the affine expression "src"
5109 * under "ma" and put the result in "dst". The number of dimensions in "src"
5110 * that precede the range is given by "n_before". The number of dimensions
5111 * in the range is given by the number of output dimensions of "ma".
5112 * The number of dimensions that follow the range is given by "n_after".
5113 * If "has_denom" is set (to one),
5114 * then "src" and "dst" have an extra initial denominator.
5115 * "n_div_ma" is the number of existentials in "ma"
5116 * "n_div_bset" is the number of existentials in "src"
5117 * The resulting "dst" (which is assumed to have been allocated by
5118 * the caller) contains coefficients for both sets of existentials,
5119 * first those in "ma" and then those in "src".
5120 * f, c1, c2 and g are temporary objects that have been initialized
5123 * Let src represent the expression
5125 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5127 * and let ma represent the expressions
5129 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5131 * We start out with the following expression for dst:
5133 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5135 * with the multiplication factor f initially equal to 1
5136 * and f \sum_i b_i v_i kept separately.
5137 * For each x_i that we substitute, we multiply the numerator
5138 * (and denominator) of dst by c_1 = m_i and add the numerator
5139 * of the x_i expression multiplied by c_2 = f b_i,
5140 * after removing the common factors of c_1 and c_2.
5141 * The multiplication factor f also needs to be multiplied by c_1
5142 * for the next x_j, j > i.
5144 void isl_seq_preimage(isl_int
*dst
, isl_int
*src
,
5145 __isl_keep isl_multi_aff
*ma
, int n_before
, int n_after
,
5146 int n_div_ma
, int n_div_bmap
,
5147 isl_int f
, isl_int c1
, isl_int c2
, isl_int g
, int has_denom
)
5150 int n_param
, n_in
, n_out
;
5153 n_param
= isl_multi_aff_dim(ma
, isl_dim_param
);
5154 n_in
= isl_multi_aff_dim(ma
, isl_dim_in
);
5155 n_out
= isl_multi_aff_dim(ma
, isl_dim_out
);
5157 isl_seq_cpy(dst
, src
, has_denom
+ 1 + n_param
+ n_before
);
5158 o_dst
= o_src
= has_denom
+ 1 + n_param
+ n_before
;
5159 isl_seq_clr(dst
+ o_dst
, n_in
);
5162 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_after
);
5165 isl_seq_clr(dst
+ o_dst
, n_div_ma
);
5167 isl_seq_cpy(dst
+ o_dst
, src
+ o_src
, n_div_bmap
);
5169 isl_int_set_si(f
, 1);
5171 for (i
= 0; i
< n_out
; ++i
) {
5172 int offset
= has_denom
+ 1 + n_param
+ n_before
+ i
;
5174 if (isl_int_is_zero(src
[offset
]))
5176 isl_int_set(c1
, ma
->p
[i
]->v
->el
[0]);
5177 isl_int_mul(c2
, f
, src
[offset
]);
5178 isl_int_gcd(g
, c1
, c2
);
5179 isl_int_divexact(c1
, c1
, g
);
5180 isl_int_divexact(c2
, c2
, g
);
5182 isl_int_mul(f
, f
, c1
);
5185 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5186 c2
, ma
->p
[i
]->v
->el
+ o_src
, 1 + n_param
);
5187 o_dst
+= 1 + n_param
;
5188 o_src
+= 1 + n_param
;
5189 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_before
);
5191 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5192 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_in
);
5195 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_after
);
5197 isl_seq_combine(dst
+ o_dst
, c1
, dst
+ o_dst
,
5198 c2
, ma
->p
[i
]->v
->el
+ o_src
, n_div_ma
);
5201 isl_seq_scale(dst
+ o_dst
, dst
+ o_dst
, c1
, n_div_bmap
);
5203 isl_int_mul(dst
[0], dst
[0], c1
);
5207 /* Compute the pullback of "aff" by the function represented by "ma".
5208 * In other words, plug in "ma" in "aff". The result is an affine expression
5209 * defined over the domain space of "ma".
5211 * If "aff" is represented by
5213 * (a(p) + b x + c(divs))/d
5215 * and ma is represented by
5217 * x = D(p) + F(y) + G(divs')
5219 * then the result is
5221 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5223 * The divs in the local space of the input are similarly adjusted
5224 * through a call to isl_local_space_preimage_multi_aff.
5226 __isl_give isl_aff
*isl_aff_pullback_multi_aff(__isl_take isl_aff
*aff
,
5227 __isl_take isl_multi_aff
*ma
)
5229 isl_aff
*res
= NULL
;
5230 isl_local_space
*ls
;
5231 int n_div_aff
, n_div_ma
;
5232 isl_int f
, c1
, c2
, g
;
5234 ma
= isl_multi_aff_align_divs(ma
);
5238 n_div_aff
= isl_aff_dim(aff
, isl_dim_div
);
5239 n_div_ma
= ma
->n
? isl_aff_dim(ma
->p
[0], isl_dim_div
) : 0;
5241 ls
= isl_aff_get_domain_local_space(aff
);
5242 ls
= isl_local_space_preimage_multi_aff(ls
, isl_multi_aff_copy(ma
));
5243 res
= isl_aff_alloc(ls
);
5252 isl_seq_preimage(res
->v
->el
, aff
->v
->el
, ma
, 0, 0, n_div_ma
, n_div_aff
,
5261 isl_multi_aff_free(ma
);
5262 res
= isl_aff_normalize(res
);
5266 isl_multi_aff_free(ma
);
5271 /* Compute the pullback of "aff1" by the function represented by "aff2".
5272 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5273 * defined over the domain space of "aff1".
5275 * The domain of "aff1" should match the range of "aff2", which means
5276 * that it should be single-dimensional.
5278 __isl_give isl_aff
*isl_aff_pullback_aff(__isl_take isl_aff
*aff1
,
5279 __isl_take isl_aff
*aff2
)
5283 ma
= isl_multi_aff_from_aff(aff2
);
5284 return isl_aff_pullback_multi_aff(aff1
, ma
);
5287 /* Compute the pullback of "ma1" by the function represented by "ma2".
5288 * In other words, plug in "ma2" in "ma1".
5290 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5292 static __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff_aligned(
5293 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5296 isl_space
*space
= NULL
;
5298 ma2
= isl_multi_aff_align_divs(ma2
);
5299 ma1
= isl_multi_aff_cow(ma1
);
5303 space
= isl_space_join(isl_multi_aff_get_space(ma2
),
5304 isl_multi_aff_get_space(ma1
));
5306 for (i
= 0; i
< ma1
->n
; ++i
) {
5307 ma1
->p
[i
] = isl_aff_pullback_multi_aff(ma1
->p
[i
],
5308 isl_multi_aff_copy(ma2
));
5313 ma1
= isl_multi_aff_reset_space(ma1
, space
);
5314 isl_multi_aff_free(ma2
);
5317 isl_space_free(space
);
5318 isl_multi_aff_free(ma2
);
5319 isl_multi_aff_free(ma1
);
5323 /* Compute the pullback of "ma1" by the function represented by "ma2".
5324 * In other words, plug in "ma2" in "ma1".
5326 __isl_give isl_multi_aff
*isl_multi_aff_pullback_multi_aff(
5327 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
5329 return isl_multi_aff_align_params_multi_multi_and(ma1
, ma2
,
5330 &isl_multi_aff_pullback_multi_aff_aligned
);
5333 /* Extend the local space of "dst" to include the divs
5334 * in the local space of "src".
5336 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
5337 __isl_keep isl_aff
*src
)
5345 return isl_aff_free(dst
);
5347 ctx
= isl_aff_get_ctx(src
);
5348 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
5349 isl_die(ctx
, isl_error_invalid
,
5350 "spaces don't match", goto error
);
5352 if (src
->ls
->div
->n_row
== 0)
5355 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
5356 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
5357 if (!exp1
|| (dst
->ls
->div
->n_row
&& !exp2
))
5360 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
5361 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
5369 return isl_aff_free(dst
);
5372 /* Adjust the local spaces of the affine expressions in "maff"
5373 * such that they all have the save divs.
5375 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
5376 __isl_take isl_multi_aff
*maff
)
5384 maff
= isl_multi_aff_cow(maff
);
5388 for (i
= 1; i
< maff
->n
; ++i
)
5389 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
5390 for (i
= 1; i
< maff
->n
; ++i
) {
5391 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
5393 return isl_multi_aff_free(maff
);
5399 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
5401 aff
= isl_aff_cow(aff
);
5405 aff
->ls
= isl_local_space_lift(aff
->ls
);
5407 return isl_aff_free(aff
);
5412 /* Lift "maff" to a space with extra dimensions such that the result
5413 * has no more existentially quantified variables.
5414 * If "ls" is not NULL, then *ls is assigned the local space that lies
5415 * at the basis of the lifting applied to "maff".
5417 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
5418 __isl_give isl_local_space
**ls
)
5432 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
5433 *ls
= isl_local_space_from_space(space
);
5435 return isl_multi_aff_free(maff
);
5440 maff
= isl_multi_aff_cow(maff
);
5441 maff
= isl_multi_aff_align_divs(maff
);
5445 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
5446 space
= isl_multi_aff_get_space(maff
);
5447 space
= isl_space_lift(isl_space_domain(space
), n_div
);
5448 space
= isl_space_extend_domain_with_range(space
,
5449 isl_multi_aff_get_space(maff
));
5451 return isl_multi_aff_free(maff
);
5452 isl_space_free(maff
->space
);
5453 maff
->space
= space
;
5456 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
5458 return isl_multi_aff_free(maff
);
5461 for (i
= 0; i
< maff
->n
; ++i
) {
5462 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
5470 isl_local_space_free(*ls
);
5471 return isl_multi_aff_free(maff
);
5475 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5477 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
5478 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
5488 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
5489 if (pos
< 0 || pos
>= n_out
)
5490 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5491 "index out of bounds", return NULL
);
5493 space
= isl_pw_multi_aff_get_space(pma
);
5494 space
= isl_space_drop_dims(space
, isl_dim_out
,
5495 pos
+ 1, n_out
- pos
- 1);
5496 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
5498 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
5499 for (i
= 0; i
< pma
->n
; ++i
) {
5501 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
5502 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
5508 /* Return an isl_pw_multi_aff with the given "set" as domain and
5509 * an unnamed zero-dimensional range.
5511 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
5512 __isl_take isl_set
*set
)
5517 space
= isl_set_get_space(set
);
5518 space
= isl_space_from_domain(space
);
5519 ma
= isl_multi_aff_zero(space
);
5520 return isl_pw_multi_aff_alloc(set
, ma
);
5523 /* Add an isl_pw_multi_aff with the given "set" as domain and
5524 * an unnamed zero-dimensional range to *user.
5526 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
5528 isl_union_pw_multi_aff
**upma
= user
;
5529 isl_pw_multi_aff
*pma
;
5531 pma
= isl_pw_multi_aff_from_domain(set
);
5532 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
5537 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5538 * an unnamed zero-dimensional range.
5540 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
5541 __isl_take isl_union_set
*uset
)
5544 isl_union_pw_multi_aff
*upma
;
5549 space
= isl_union_set_get_space(uset
);
5550 upma
= isl_union_pw_multi_aff_empty(space
);
5552 if (isl_union_set_foreach_set(uset
,
5553 &add_pw_multi_aff_from_domain
, &upma
) < 0)
5556 isl_union_set_free(uset
);
5559 isl_union_set_free(uset
);
5560 isl_union_pw_multi_aff_free(upma
);
5564 /* Convert "pma" to an isl_map and add it to *umap.
5566 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
5568 isl_union_map
**umap
= user
;
5571 map
= isl_map_from_pw_multi_aff(pma
);
5572 *umap
= isl_union_map_add_map(*umap
, map
);
5577 /* Construct a union map mapping the domain of the union
5578 * piecewise multi-affine expression to its range, with each dimension
5579 * in the range equated to the corresponding affine expression on its cell.
5581 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
5582 __isl_take isl_union_pw_multi_aff
*upma
)
5585 isl_union_map
*umap
;
5590 space
= isl_union_pw_multi_aff_get_space(upma
);
5591 umap
= isl_union_map_empty(space
);
5593 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
5594 &map_from_pw_multi_aff
, &umap
) < 0)
5597 isl_union_pw_multi_aff_free(upma
);
5600 isl_union_pw_multi_aff_free(upma
);
5601 isl_union_map_free(umap
);
5605 /* Local data for bin_entry and the callback "fn".
5607 struct isl_union_pw_multi_aff_bin_data
{
5608 isl_union_pw_multi_aff
*upma2
;
5609 isl_union_pw_multi_aff
*res
;
5610 isl_pw_multi_aff
*pma
;
5611 int (*fn
)(void **entry
, void *user
);
5614 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5615 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5617 static int bin_entry(void **entry
, void *user
)
5619 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5620 isl_pw_multi_aff
*pma
= *entry
;
5623 if (isl_hash_table_foreach(data
->upma2
->space
->ctx
, &data
->upma2
->table
,
5624 data
->fn
, data
) < 0)
5630 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5631 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5632 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5633 * as *entry. The callback should adjust data->res if desired.
5635 static __isl_give isl_union_pw_multi_aff
*bin_op(
5636 __isl_take isl_union_pw_multi_aff
*upma1
,
5637 __isl_take isl_union_pw_multi_aff
*upma2
,
5638 int (*fn
)(void **entry
, void *user
))
5641 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
5643 space
= isl_union_pw_multi_aff_get_space(upma2
);
5644 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
5645 space
= isl_union_pw_multi_aff_get_space(upma1
);
5646 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
5648 if (!upma1
|| !upma2
)
5652 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->space
),
5654 if (isl_hash_table_foreach(upma1
->space
->ctx
, &upma1
->table
,
5655 &bin_entry
, &data
) < 0)
5658 isl_union_pw_multi_aff_free(upma1
);
5659 isl_union_pw_multi_aff_free(upma2
);
5662 isl_union_pw_multi_aff_free(upma1
);
5663 isl_union_pw_multi_aff_free(upma2
);
5664 isl_union_pw_multi_aff_free(data
.res
);
5668 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5669 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5671 static __isl_give isl_pw_multi_aff
*pw_multi_aff_range_product(
5672 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5676 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5677 isl_pw_multi_aff_get_space(pma2
));
5678 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5679 &isl_multi_aff_range_product
);
5682 /* Given two isl_pw_multi_affs A -> B and C -> D,
5683 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5685 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_range_product(
5686 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5688 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5689 &pw_multi_aff_range_product
);
5692 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5693 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5695 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
5696 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5700 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
5701 isl_pw_multi_aff_get_space(pma2
));
5702 space
= isl_space_flatten_range(space
);
5703 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
5704 &isl_multi_aff_flat_range_product
);
5707 /* Given two isl_pw_multi_affs A -> B and C -> D,
5708 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5710 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
5711 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
5713 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
5714 &pw_multi_aff_flat_range_product
);
5717 /* If data->pma and *entry have the same domain space, then compute
5718 * their flat range product and the result to data->res.
5720 static int flat_range_product_entry(void **entry
, void *user
)
5722 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
5723 isl_pw_multi_aff
*pma2
= *entry
;
5725 if (!isl_space_tuple_is_equal(data
->pma
->dim
, isl_dim_in
,
5726 pma2
->dim
, isl_dim_in
))
5729 pma2
= isl_pw_multi_aff_flat_range_product(
5730 isl_pw_multi_aff_copy(data
->pma
),
5731 isl_pw_multi_aff_copy(pma2
));
5733 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
5738 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5739 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5741 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
5742 __isl_take isl_union_pw_multi_aff
*upma1
,
5743 __isl_take isl_union_pw_multi_aff
*upma2
)
5745 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
5748 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5749 * The parameters are assumed to have been aligned.
5751 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5752 * except that it works on two different isl_pw_* types.
5754 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
5755 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5756 __isl_take isl_pw_aff
*pa
)
5759 isl_pw_multi_aff
*res
= NULL
;
5764 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_in
,
5765 pa
->dim
, isl_dim_in
))
5766 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5767 "domains don't match", goto error
);
5768 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
5769 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5770 "index out of bounds", goto error
);
5773 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
5775 for (i
= 0; i
< pma
->n
; ++i
) {
5776 for (j
= 0; j
< pa
->n
; ++j
) {
5778 isl_multi_aff
*res_ij
;
5781 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
5782 isl_set_copy(pa
->p
[j
].set
));
5783 empty
= isl_set_plain_is_empty(common
);
5784 if (empty
< 0 || empty
) {
5785 isl_set_free(common
);
5791 res_ij
= isl_multi_aff_set_aff(
5792 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
5793 isl_aff_copy(pa
->p
[j
].aff
));
5794 res_ij
= isl_multi_aff_gist(res_ij
,
5795 isl_set_copy(common
));
5797 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
5801 isl_pw_multi_aff_free(pma
);
5802 isl_pw_aff_free(pa
);
5805 isl_pw_multi_aff_free(pma
);
5806 isl_pw_aff_free(pa
);
5807 return isl_pw_multi_aff_free(res
);
5810 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5812 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
5813 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
5814 __isl_take isl_pw_aff
*pa
)
5818 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
5819 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5820 if (!isl_space_has_named_params(pma
->dim
) ||
5821 !isl_space_has_named_params(pa
->dim
))
5822 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5823 "unaligned unnamed parameters", goto error
);
5824 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
5825 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
5826 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
5828 isl_pw_multi_aff_free(pma
);
5829 isl_pw_aff_free(pa
);
5833 /* Do the parameters of "pa" match those of "space"?
5835 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff
*pa
,
5836 __isl_keep isl_space
*space
)
5838 isl_space
*pa_space
;
5844 pa_space
= isl_pw_aff_get_space(pa
);
5846 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5848 isl_space_free(pa_space
);
5852 /* Check that the domain space of "pa" matches "space".
5854 * Return 0 on success and -1 on error.
5856 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff
*pa
,
5857 __isl_keep isl_space
*space
)
5859 isl_space
*pa_space
;
5865 pa_space
= isl_pw_aff_get_space(pa
);
5867 match
= isl_space_match(space
, isl_dim_param
, pa_space
, isl_dim_param
);
5871 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5872 "parameters don't match", goto error
);
5873 match
= isl_space_tuple_is_equal(space
, isl_dim_in
,
5874 pa_space
, isl_dim_in
);
5878 isl_die(isl_pw_aff_get_ctx(pa
), isl_error_invalid
,
5879 "domains don't match", goto error
);
5880 isl_space_free(pa_space
);
5883 isl_space_free(pa_space
);
5890 #include <isl_multi_templ.c>
5892 /* Scale the elements of "pma" by the corresponding elements of "mv".
5894 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_scale_multi_val(
5895 __isl_take isl_pw_multi_aff
*pma
, __isl_take isl_multi_val
*mv
)
5899 pma
= isl_pw_multi_aff_cow(pma
);
5902 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5903 mv
->space
, isl_dim_set
))
5904 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
5905 "spaces don't match", goto error
);
5906 if (!isl_space_match(pma
->dim
, isl_dim_param
,
5907 mv
->space
, isl_dim_param
)) {
5908 pma
= isl_pw_multi_aff_align_params(pma
,
5909 isl_multi_val_get_space(mv
));
5910 mv
= isl_multi_val_align_params(mv
,
5911 isl_pw_multi_aff_get_space(pma
));
5916 for (i
= 0; i
< pma
->n
; ++i
) {
5917 pma
->p
[i
].maff
= isl_multi_aff_scale_multi_val(pma
->p
[i
].maff
,
5918 isl_multi_val_copy(mv
));
5919 if (!pma
->p
[i
].maff
)
5923 isl_multi_val_free(mv
);
5926 isl_multi_val_free(mv
);
5927 isl_pw_multi_aff_free(pma
);
5931 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5932 * mv contains the mv argument.
5933 * res collects the results.
5935 struct isl_union_pw_multi_aff_scale_multi_val_data
{
5937 isl_union_pw_multi_aff
*res
;
5940 /* This function is called for each entry of an isl_union_pw_multi_aff.
5941 * If the space of the entry matches that of data->mv,
5942 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5945 static int union_pw_multi_aff_scale_multi_val_entry(void **entry
, void *user
)
5947 struct isl_union_pw_multi_aff_scale_multi_val_data
*data
= user
;
5948 isl_pw_multi_aff
*pma
= *entry
;
5952 if (!isl_space_tuple_is_equal(pma
->dim
, isl_dim_out
,
5953 data
->mv
->space
, isl_dim_set
))
5956 pma
= isl_pw_multi_aff_copy(pma
);
5957 pma
= isl_pw_multi_aff_scale_multi_val(pma
,
5958 isl_multi_val_copy(data
->mv
));
5959 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma
);
5966 /* Scale the elements of "upma" by the corresponding elements of "mv",
5967 * for those entries that match the space of "mv".
5969 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_scale_multi_val(
5970 __isl_take isl_union_pw_multi_aff
*upma
, __isl_take isl_multi_val
*mv
)
5972 struct isl_union_pw_multi_aff_scale_multi_val_data data
;
5974 upma
= isl_union_pw_multi_aff_align_params(upma
,
5975 isl_multi_val_get_space(mv
));
5976 mv
= isl_multi_val_align_params(mv
,
5977 isl_union_pw_multi_aff_get_space(upma
));
5982 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma
->space
),
5984 if (isl_hash_table_foreach(upma
->space
->ctx
, &upma
->table
,
5985 &union_pw_multi_aff_scale_multi_val_entry
, &data
) < 0)
5988 isl_multi_val_free(mv
);
5989 isl_union_pw_multi_aff_free(upma
);
5992 isl_multi_val_free(mv
);
5993 isl_union_pw_multi_aff_free(upma
);
5997 /* Construct and return a piecewise multi affine expression
5998 * in the given space with value zero in each of the output dimensions and
5999 * a universe domain.
6001 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_zero(__isl_take isl_space
*space
)
6003 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space
));
6006 /* Construct and return a piecewise multi affine expression
6007 * that is equal to the given piecewise affine expression.
6009 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_pw_aff(
6010 __isl_take isl_pw_aff
*pa
)
6014 isl_pw_multi_aff
*pma
;
6019 space
= isl_pw_aff_get_space(pa
);
6020 pma
= isl_pw_multi_aff_alloc_size(space
, pa
->n
);
6022 for (i
= 0; i
< pa
->n
; ++i
) {
6026 set
= isl_set_copy(pa
->p
[i
].set
);
6027 ma
= isl_multi_aff_from_aff(isl_aff_copy(pa
->p
[i
].aff
));
6028 pma
= isl_pw_multi_aff_add_piece(pma
, set
, ma
);
6031 isl_pw_aff_free(pa
);
6035 /* Construct a set or map mapping the shared (parameter) domain
6036 * of the piecewise affine expressions to the range of "mpa"
6037 * with each dimension in the range equated to the
6038 * corresponding piecewise affine expression.
6040 static __isl_give isl_map
*map_from_multi_pw_aff(
6041 __isl_take isl_multi_pw_aff
*mpa
)
6050 if (isl_space_dim(mpa
->space
, isl_dim_out
) != mpa
->n
)
6051 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6052 "invalid space", goto error
);
6054 space
= isl_multi_pw_aff_get_domain_space(mpa
);
6055 map
= isl_map_universe(isl_space_from_domain(space
));
6057 for (i
= 0; i
< mpa
->n
; ++i
) {
6061 pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6062 map_i
= map_from_pw_aff(pa
);
6064 map
= isl_map_flat_range_product(map
, map_i
);
6067 map
= isl_map_reset_space(map
, isl_multi_pw_aff_get_space(mpa
));
6069 isl_multi_pw_aff_free(mpa
);
6072 isl_multi_pw_aff_free(mpa
);
6076 /* Construct a map mapping the shared domain
6077 * of the piecewise affine expressions to the range of "mpa"
6078 * with each dimension in the range equated to the
6079 * corresponding piecewise affine expression.
6081 __isl_give isl_map
*isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6085 if (isl_space_is_set(mpa
->space
))
6086 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6087 "space of input is not a map", goto error
);
6089 return map_from_multi_pw_aff(mpa
);
6091 isl_multi_pw_aff_free(mpa
);
6095 /* Construct a set mapping the shared parameter domain
6096 * of the piecewise affine expressions to the space of "mpa"
6097 * with each dimension in the range equated to the
6098 * corresponding piecewise affine expression.
6100 __isl_give isl_set
*isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff
*mpa
)
6104 if (!isl_space_is_set(mpa
->space
))
6105 isl_die(isl_multi_pw_aff_get_ctx(mpa
), isl_error_internal
,
6106 "space of input is not a set", goto error
);
6108 return map_from_multi_pw_aff(mpa
);
6110 isl_multi_pw_aff_free(mpa
);
6114 /* Construct and return a piecewise multi affine expression
6115 * that is equal to the given multi piecewise affine expression
6116 * on the shared domain of the piecewise affine expressions.
6118 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_pw_aff(
6119 __isl_take isl_multi_pw_aff
*mpa
)
6124 isl_pw_multi_aff
*pma
;
6129 space
= isl_multi_pw_aff_get_space(mpa
);
6132 isl_multi_pw_aff_free(mpa
);
6133 return isl_pw_multi_aff_zero(space
);
6136 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, 0);
6137 pma
= isl_pw_multi_aff_from_pw_aff(pa
);
6139 for (i
= 1; i
< mpa
->n
; ++i
) {
6140 isl_pw_multi_aff
*pma_i
;
6142 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6143 pma_i
= isl_pw_multi_aff_from_pw_aff(pa
);
6144 pma
= isl_pw_multi_aff_range_product(pma
, pma_i
);
6147 pma
= isl_pw_multi_aff_reset_space(pma
, space
);
6149 isl_multi_pw_aff_free(mpa
);
6153 /* Construct and return a multi piecewise affine expression
6154 * that is equal to the given multi affine expression.
6156 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_multi_aff(
6157 __isl_take isl_multi_aff
*ma
)
6160 isl_multi_pw_aff
*mpa
;
6165 n
= isl_multi_aff_dim(ma
, isl_dim_out
);
6166 mpa
= isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma
));
6168 for (i
= 0; i
< n
; ++i
) {
6171 pa
= isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma
, i
));
6172 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6175 isl_multi_aff_free(ma
);
6179 /* Construct and return a multi piecewise affine expression
6180 * that is equal to the given piecewise multi affine expression.
6182 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_from_pw_multi_aff(
6183 __isl_take isl_pw_multi_aff
*pma
)
6187 isl_multi_pw_aff
*mpa
;
6192 n
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
6193 space
= isl_pw_multi_aff_get_space(pma
);
6194 mpa
= isl_multi_pw_aff_alloc(space
);
6196 for (i
= 0; i
< n
; ++i
) {
6199 pa
= isl_pw_multi_aff_get_pw_aff(pma
, i
);
6200 mpa
= isl_multi_pw_aff_set_pw_aff(mpa
, i
, pa
);
6203 isl_pw_multi_aff_free(pma
);
6207 /* Do "pa1" and "pa2" represent the same function?
6209 * We first check if they are obviously equal.
6210 * If not, we convert them to maps and check if those are equal.
6212 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff
*pa1
, __isl_keep isl_pw_aff
*pa2
)
6215 isl_map
*map1
, *map2
;
6220 equal
= isl_pw_aff_plain_is_equal(pa1
, pa2
);
6221 if (equal
< 0 || equal
)
6224 map1
= map_from_pw_aff(isl_pw_aff_copy(pa1
));
6225 map2
= map_from_pw_aff(isl_pw_aff_copy(pa2
));
6226 equal
= isl_map_is_equal(map1
, map2
);
6233 /* Do "mpa1" and "mpa2" represent the same function?
6235 * Note that we cannot convert the entire isl_multi_pw_aff
6236 * to a map because the domains of the piecewise affine expressions
6237 * may not be the same.
6239 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff
*mpa1
,
6240 __isl_keep isl_multi_pw_aff
*mpa2
)
6248 if (!isl_space_match(mpa1
->space
, isl_dim_param
,
6249 mpa2
->space
, isl_dim_param
)) {
6250 if (!isl_space_has_named_params(mpa1
->space
))
6252 if (!isl_space_has_named_params(mpa2
->space
))
6254 mpa1
= isl_multi_pw_aff_copy(mpa1
);
6255 mpa2
= isl_multi_pw_aff_copy(mpa2
);
6256 mpa1
= isl_multi_pw_aff_align_params(mpa1
,
6257 isl_multi_pw_aff_get_space(mpa2
));
6258 mpa2
= isl_multi_pw_aff_align_params(mpa2
,
6259 isl_multi_pw_aff_get_space(mpa1
));
6260 equal
= isl_multi_pw_aff_is_equal(mpa1
, mpa2
);
6261 isl_multi_pw_aff_free(mpa1
);
6262 isl_multi_pw_aff_free(mpa2
);
6266 equal
= isl_space_is_equal(mpa1
->space
, mpa2
->space
);
6267 if (equal
< 0 || !equal
)
6270 for (i
= 0; i
< mpa1
->n
; ++i
) {
6271 equal
= isl_pw_aff_is_equal(mpa1
->p
[i
], mpa2
->p
[i
]);
6272 if (equal
< 0 || !equal
)
6279 /* Coalesce the elements of "mpa".
6281 * Note that such coalescing does not change the meaning of "mpa"
6282 * so there is no need to cow. We do need to be careful not to
6283 * destroy any other copies of "mpa" in case of failure.
6285 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_coalesce(
6286 __isl_take isl_multi_pw_aff
*mpa
)
6293 for (i
= 0; i
< mpa
->n
; ++i
) {
6294 isl_pw_aff
*pa
= isl_pw_aff_copy(mpa
->p
[i
]);
6295 pa
= isl_pw_aff_coalesce(pa
);
6297 return isl_multi_pw_aff_free(mpa
);
6298 isl_pw_aff_free(mpa
->p
[i
]);
6305 /* Compute the pullback of "mpa" by the function represented by "ma".
6306 * In other words, plug in "ma" in "mpa".
6308 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6310 static __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff_aligned(
6311 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6314 isl_space
*space
= NULL
;
6316 mpa
= isl_multi_pw_aff_cow(mpa
);
6320 space
= isl_space_join(isl_multi_aff_get_space(ma
),
6321 isl_multi_pw_aff_get_space(mpa
));
6325 for (i
= 0; i
< mpa
->n
; ++i
) {
6326 mpa
->p
[i
] = isl_pw_aff_pullback_multi_aff(mpa
->p
[i
],
6327 isl_multi_aff_copy(ma
));
6332 isl_multi_aff_free(ma
);
6333 isl_space_free(mpa
->space
);
6337 isl_space_free(space
);
6338 isl_multi_pw_aff_free(mpa
);
6339 isl_multi_aff_free(ma
);
6343 /* Compute the pullback of "mpa" by the function represented by "ma".
6344 * In other words, plug in "ma" in "mpa".
6346 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_aff(
6347 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_multi_aff
*ma
)
6351 if (isl_space_match(mpa
->space
, isl_dim_param
,
6352 ma
->space
, isl_dim_param
))
6353 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6354 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_multi_aff_get_space(ma
));
6355 ma
= isl_multi_aff_align_params(ma
, isl_multi_pw_aff_get_space(mpa
));
6356 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa
, ma
);
6358 isl_multi_pw_aff_free(mpa
);
6359 isl_multi_aff_free(ma
);
6363 /* Compute the pullback of "mpa" by the function represented by "pma".
6364 * In other words, plug in "pma" in "mpa".
6366 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6368 static __isl_give isl_multi_pw_aff
*
6369 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6370 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6373 isl_space
*space
= NULL
;
6375 mpa
= isl_multi_pw_aff_cow(mpa
);
6379 space
= isl_space_join(isl_pw_multi_aff_get_space(pma
),
6380 isl_multi_pw_aff_get_space(mpa
));
6382 for (i
= 0; i
< mpa
->n
; ++i
) {
6383 mpa
->p
[i
] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa
->p
[i
],
6384 isl_pw_multi_aff_copy(pma
));
6389 isl_pw_multi_aff_free(pma
);
6390 isl_space_free(mpa
->space
);
6394 isl_space_free(space
);
6395 isl_multi_pw_aff_free(mpa
);
6396 isl_pw_multi_aff_free(pma
);
6400 /* Compute the pullback of "mpa" by the function represented by "pma".
6401 * In other words, plug in "pma" in "mpa".
6403 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_pw_multi_aff(
6404 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_multi_aff
*pma
)
6408 if (isl_space_match(mpa
->space
, isl_dim_param
, pma
->dim
, isl_dim_param
))
6409 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6410 mpa
= isl_multi_pw_aff_align_params(mpa
,
6411 isl_pw_multi_aff_get_space(pma
));
6412 pma
= isl_pw_multi_aff_align_params(pma
,
6413 isl_multi_pw_aff_get_space(mpa
));
6414 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa
, pma
);
6416 isl_multi_pw_aff_free(mpa
);
6417 isl_pw_multi_aff_free(pma
);
6421 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6422 * with the domain of "aff". The domain of the result is the same
6424 * "mpa" and "aff" are assumed to have been aligned.
6426 * We first extract the parametric constant from "aff", defined
6427 * over the correct domain.
6428 * Then we add the appropriate combinations of the members of "mpa".
6429 * Finally, we add the integer divisions through recursive calls.
6431 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff_aligned(
6432 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6434 int i
, n_param
, n_in
, n_div
;
6440 n_param
= isl_aff_dim(aff
, isl_dim_param
);
6441 n_in
= isl_aff_dim(aff
, isl_dim_in
);
6442 n_div
= isl_aff_dim(aff
, isl_dim_div
);
6444 space
= isl_space_domain(isl_multi_pw_aff_get_space(mpa
));
6445 tmp
= isl_aff_copy(aff
);
6446 tmp
= isl_aff_drop_dims(tmp
, isl_dim_div
, 0, n_div
);
6447 tmp
= isl_aff_drop_dims(tmp
, isl_dim_in
, 0, n_in
);
6448 tmp
= isl_aff_add_dims(tmp
, isl_dim_in
,
6449 isl_space_dim(space
, isl_dim_set
));
6450 tmp
= isl_aff_reset_domain_space(tmp
, space
);
6451 pa
= isl_pw_aff_from_aff(tmp
);
6453 for (i
= 0; i
< n_in
; ++i
) {
6456 if (!isl_aff_involves_dims(aff
, isl_dim_in
, i
, 1))
6458 v
= isl_aff_get_coefficient_val(aff
, isl_dim_in
, i
);
6459 pa_i
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
6460 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6461 pa
= isl_pw_aff_add(pa
, pa_i
);
6464 for (i
= 0; i
< n_div
; ++i
) {
6468 if (!isl_aff_involves_dims(aff
, isl_dim_div
, i
, 1))
6470 div
= isl_aff_get_div(aff
, i
);
6471 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6472 isl_multi_pw_aff_copy(mpa
), div
);
6473 pa_i
= isl_pw_aff_floor(pa_i
);
6474 v
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, i
);
6475 pa_i
= isl_pw_aff_scale_val(pa_i
, v
);
6476 pa
= isl_pw_aff_add(pa
, pa_i
);
6479 isl_multi_pw_aff_free(mpa
);
6485 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6486 * with the domain of "aff". The domain of the result is the same
6489 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_aff(
6490 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_aff
*aff
)
6494 if (isl_space_match(aff
->ls
->dim
, isl_dim_param
,
6495 mpa
->space
, isl_dim_param
))
6496 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6498 aff
= isl_aff_align_params(aff
, isl_multi_pw_aff_get_space(mpa
));
6499 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_aff_get_space(aff
));
6501 return isl_multi_pw_aff_apply_aff_aligned(mpa
, aff
);
6504 isl_multi_pw_aff_free(mpa
);
6508 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6509 * with the domain of "pa". The domain of the result is the same
6511 * "mpa" and "pa" are assumed to have been aligned.
6513 * We consider each piece in turn. Note that the domains of the
6514 * pieces are assumed to be disjoint and they remain disjoint
6515 * after taking the preimage (over the same function).
6517 static __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff_aligned(
6518 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6527 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa
),
6528 isl_pw_aff_get_space(pa
));
6529 res
= isl_pw_aff_empty(space
);
6531 for (i
= 0; i
< pa
->n
; ++i
) {
6535 pa_i
= isl_multi_pw_aff_apply_aff_aligned(
6536 isl_multi_pw_aff_copy(mpa
),
6537 isl_aff_copy(pa
->p
[i
].aff
));
6538 domain
= isl_set_copy(pa
->p
[i
].set
);
6539 domain
= isl_set_preimage_multi_pw_aff(domain
,
6540 isl_multi_pw_aff_copy(mpa
));
6541 pa_i
= isl_pw_aff_intersect_domain(pa_i
, domain
);
6542 res
= isl_pw_aff_add_disjoint(res
, pa_i
);
6545 isl_pw_aff_free(pa
);
6546 isl_multi_pw_aff_free(mpa
);
6549 isl_pw_aff_free(pa
);
6550 isl_multi_pw_aff_free(mpa
);
6554 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6555 * with the domain of "pa". The domain of the result is the same
6558 __isl_give isl_pw_aff
*isl_multi_pw_aff_apply_pw_aff(
6559 __isl_take isl_multi_pw_aff
*mpa
, __isl_take isl_pw_aff
*pa
)
6563 if (isl_space_match(pa
->dim
, isl_dim_param
, mpa
->space
, isl_dim_param
))
6564 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6566 pa
= isl_pw_aff_align_params(pa
, isl_multi_pw_aff_get_space(mpa
));
6567 mpa
= isl_multi_pw_aff_align_params(mpa
, isl_pw_aff_get_space(pa
));
6569 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6571 isl_pw_aff_free(pa
);
6572 isl_multi_pw_aff_free(mpa
);
6576 /* Compute the pullback of "pa" by the function represented by "mpa".
6577 * In other words, plug in "mpa" in "pa".
6578 * "pa" and "mpa" are assumed to have been aligned.
6580 * The pullback is computed by applying "pa" to "mpa".
6582 static __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff_aligned(
6583 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6585 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa
, pa
);
6588 /* Compute the pullback of "pa" by the function represented by "mpa".
6589 * In other words, plug in "mpa" in "pa".
6591 * The pullback is computed by applying "pa" to "mpa".
6593 __isl_give isl_pw_aff
*isl_pw_aff_pullback_multi_pw_aff(
6594 __isl_take isl_pw_aff
*pa
, __isl_take isl_multi_pw_aff
*mpa
)
6596 return isl_multi_pw_aff_apply_pw_aff(mpa
, pa
);
6599 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6600 * In other words, plug in "mpa2" in "mpa1".
6602 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6604 * We pullback each member of "mpa1" in turn.
6606 static __isl_give isl_multi_pw_aff
*
6607 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6608 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6611 isl_space
*space
= NULL
;
6613 mpa1
= isl_multi_pw_aff_cow(mpa1
);
6617 space
= isl_space_join(isl_multi_pw_aff_get_space(mpa2
),
6618 isl_multi_pw_aff_get_space(mpa1
));
6620 for (i
= 0; i
< mpa1
->n
; ++i
) {
6621 mpa1
->p
[i
] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6622 mpa1
->p
[i
], isl_multi_pw_aff_copy(mpa2
));
6627 mpa1
= isl_multi_pw_aff_reset_space(mpa1
, space
);
6629 isl_multi_pw_aff_free(mpa2
);
6632 isl_space_free(space
);
6633 isl_multi_pw_aff_free(mpa1
);
6634 isl_multi_pw_aff_free(mpa2
);
6638 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6639 * In other words, plug in "mpa2" in "mpa1".
6641 __isl_give isl_multi_pw_aff
*isl_multi_pw_aff_pullback_multi_pw_aff(
6642 __isl_take isl_multi_pw_aff
*mpa1
, __isl_take isl_multi_pw_aff
*mpa2
)
6644 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1
, mpa2
,
6645 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned
);
6648 /* Compare two isl_affs.
6650 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6651 * than "aff2" and 0 if they are equal.
6653 * The order is fairly arbitrary. We do consider expressions that only involve
6654 * earlier dimensions as "smaller".
6656 int isl_aff_plain_cmp(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
6669 cmp
= isl_local_space_cmp(aff1
->ls
, aff2
->ls
);
6673 last1
= isl_seq_last_non_zero(aff1
->v
->el
+ 1, aff1
->v
->size
- 1);
6674 last2
= isl_seq_last_non_zero(aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
6676 return last1
- last2
;
6678 return isl_seq_cmp(aff1
->v
->el
, aff2
->v
->el
, aff1
->v
->size
);
6681 /* Compare two isl_pw_affs.
6683 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6684 * than "pa2" and 0 if they are equal.
6686 * The order is fairly arbitrary. We do consider expressions that only involve
6687 * earlier dimensions as "smaller".
6689 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff
*pa1
,
6690 __isl_keep isl_pw_aff
*pa2
)
6703 cmp
= isl_space_cmp(pa1
->dim
, pa2
->dim
);
6707 if (pa1
->n
!= pa2
->n
)
6708 return pa1
->n
- pa2
->n
;
6710 for (i
= 0; i
< pa1
->n
; ++i
) {
6711 cmp
= isl_set_plain_cmp(pa1
->p
[i
].set
, pa2
->p
[i
].set
);
6714 cmp
= isl_aff_plain_cmp(pa1
->p
[i
].aff
, pa2
->p
[i
].aff
);