2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 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_mat_private.h>
22 #include <isl_list_private.h>
23 #include <isl/constraint.h>
26 #include <isl_config.h>
28 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
29 __isl_take isl_vec
*v
)
36 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
46 isl_local_space_free(ls
);
51 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
60 ctx
= isl_local_space_get_ctx(ls
);
61 if (!isl_local_space_divs_known(ls
))
62 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
64 if (!isl_local_space_is_set(ls
))
65 isl_die(ctx
, isl_error_invalid
,
66 "domain of affine expression should be a set",
69 total
= isl_local_space_dim(ls
, isl_dim_all
);
70 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
71 return isl_aff_alloc_vec(ls
, v
);
73 isl_local_space_free(ls
);
77 __isl_give isl_aff
*isl_aff_zero_on_domain(__isl_take isl_local_space
*ls
)
81 aff
= isl_aff_alloc(ls
);
85 isl_int_set_si(aff
->v
->el
[0], 1);
86 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
91 /* Return an affine expression that is equal to the specified dimension
94 __isl_give isl_aff
*isl_aff_var_on_domain(__isl_take isl_local_space
*ls
,
95 enum isl_dim_type type
, unsigned pos
)
103 space
= isl_local_space_get_space(ls
);
106 if (isl_space_is_map(space
))
107 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
108 "expecting (parameter) set space", goto error
);
109 if (pos
>= isl_local_space_dim(ls
, type
))
110 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
111 "position out of bounds", goto error
);
113 isl_space_free(space
);
114 aff
= isl_aff_alloc(ls
);
118 pos
+= isl_local_space_offset(aff
->ls
, type
);
120 isl_int_set_si(aff
->v
->el
[0], 1);
121 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
122 isl_int_set_si(aff
->v
->el
[1 + pos
], 1);
126 isl_local_space_free(ls
);
127 isl_space_free(space
);
131 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
140 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
145 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
146 isl_vec_copy(aff
->v
));
149 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
157 return isl_aff_dup(aff
);
160 void *isl_aff_free(__isl_take isl_aff
*aff
)
168 isl_local_space_free(aff
->ls
);
169 isl_vec_free(aff
->v
);
176 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
178 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
181 /* Externally, an isl_aff has a map space, but internally, the
182 * ls field corresponds to the domain of that space.
184 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
188 if (type
== isl_dim_out
)
190 if (type
== isl_dim_in
)
192 return isl_local_space_dim(aff
->ls
, type
);
195 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
197 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
200 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
205 space
= isl_local_space_get_space(aff
->ls
);
206 space
= isl_space_from_domain(space
);
207 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
211 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
212 __isl_keep isl_aff
*aff
)
214 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
217 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
222 ls
= isl_local_space_copy(aff
->ls
);
223 ls
= isl_local_space_from_domain(ls
);
224 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
228 /* Externally, an isl_aff has a map space, but internally, the
229 * ls field corresponds to the domain of that space.
231 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
232 enum isl_dim_type type
, unsigned pos
)
236 if (type
== isl_dim_out
)
238 if (type
== isl_dim_in
)
240 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
243 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
244 __isl_take isl_space
*dim
)
246 aff
= isl_aff_cow(aff
);
250 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
252 return isl_aff_free(aff
);
261 /* Reset the space of "aff". This function is called from isl_pw_templ.c
262 * and doesn't know if the space of an element object is represented
263 * directly or through its domain. It therefore passes along both.
265 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
266 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
268 isl_space_free(space
);
269 return isl_aff_reset_domain_space(aff
, domain
);
272 /* Reorder the coefficients of the affine expression based
273 * on the given reodering.
274 * The reordering r is assumed to have been extended with the local
277 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
278 __isl_take isl_reordering
*r
, int n_div
)
286 res
= isl_vec_alloc(vec
->ctx
,
287 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
288 isl_seq_cpy(res
->el
, vec
->el
, 2);
289 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
290 for (i
= 0; i
< r
->len
; ++i
)
291 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
293 isl_reordering_free(r
);
298 isl_reordering_free(r
);
302 /* Reorder the dimensions of the domain of "aff" according
303 * to the given reordering.
305 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
306 __isl_take isl_reordering
*r
)
308 aff
= isl_aff_cow(aff
);
312 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
313 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
314 aff
->ls
->div
->n_row
);
315 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
317 if (!aff
->v
|| !aff
->ls
)
318 return isl_aff_free(aff
);
323 isl_reordering_free(r
);
327 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
328 __isl_take isl_space
*model
)
333 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
334 model
, isl_dim_param
)) {
337 model
= isl_space_drop_dims(model
, isl_dim_in
,
338 0, isl_space_dim(model
, isl_dim_in
));
339 model
= isl_space_drop_dims(model
, isl_dim_out
,
340 0, isl_space_dim(model
, isl_dim_out
));
341 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
342 exp
= isl_reordering_extend_space(exp
,
343 isl_aff_get_domain_space(aff
));
344 aff
= isl_aff_realign_domain(aff
, exp
);
347 isl_space_free(model
);
350 isl_space_free(model
);
355 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
360 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
363 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
370 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
371 if (equal
< 0 || !equal
)
374 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
377 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
381 isl_int_set(*v
, aff
->v
->el
[0]);
385 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
389 isl_int_set(*v
, aff
->v
->el
[1]);
393 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
394 enum isl_dim_type type
, int pos
, isl_int
*v
)
399 if (type
== isl_dim_out
)
400 isl_die(aff
->v
->ctx
, isl_error_invalid
,
401 "output/set dimension does not have a coefficient",
403 if (type
== isl_dim_in
)
406 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
407 isl_die(aff
->v
->ctx
, isl_error_invalid
,
408 "position out of bounds", return -1);
410 pos
+= isl_local_space_offset(aff
->ls
, type
);
411 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
416 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
418 aff
= isl_aff_cow(aff
);
422 aff
->v
= isl_vec_cow(aff
->v
);
424 return isl_aff_free(aff
);
426 isl_int_set(aff
->v
->el
[0], v
);
431 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
433 aff
= isl_aff_cow(aff
);
437 aff
->v
= isl_vec_cow(aff
->v
);
439 return isl_aff_free(aff
);
441 isl_int_set(aff
->v
->el
[1], v
);
446 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
448 if (isl_int_is_zero(v
))
451 aff
= isl_aff_cow(aff
);
455 aff
->v
= isl_vec_cow(aff
->v
);
457 return isl_aff_free(aff
);
459 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
464 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
469 isl_int_set_si(t
, v
);
470 aff
= isl_aff_add_constant(aff
, t
);
476 /* Add "v" to the numerator of the constant term of "aff".
478 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
480 if (isl_int_is_zero(v
))
483 aff
= isl_aff_cow(aff
);
487 aff
->v
= isl_vec_cow(aff
->v
);
489 return isl_aff_free(aff
);
491 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
496 /* Add "v" to the numerator of the constant term of "aff".
498 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
506 isl_int_set_si(t
, v
);
507 aff
= isl_aff_add_constant_num(aff
, t
);
513 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
515 aff
= isl_aff_cow(aff
);
519 aff
->v
= isl_vec_cow(aff
->v
);
521 return isl_aff_free(aff
);
523 isl_int_set_si(aff
->v
->el
[1], v
);
528 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
529 enum isl_dim_type type
, int pos
, isl_int v
)
534 if (type
== isl_dim_out
)
535 isl_die(aff
->v
->ctx
, isl_error_invalid
,
536 "output/set dimension does not have a coefficient",
537 return isl_aff_free(aff
));
538 if (type
== isl_dim_in
)
541 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
542 isl_die(aff
->v
->ctx
, isl_error_invalid
,
543 "position out of bounds", return isl_aff_free(aff
));
545 aff
= isl_aff_cow(aff
);
549 aff
->v
= isl_vec_cow(aff
->v
);
551 return isl_aff_free(aff
);
553 pos
+= isl_local_space_offset(aff
->ls
, type
);
554 isl_int_set(aff
->v
->el
[1 + pos
], v
);
559 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
560 enum isl_dim_type type
, int pos
, int v
)
565 if (type
== isl_dim_out
)
566 isl_die(aff
->v
->ctx
, isl_error_invalid
,
567 "output/set dimension does not have a coefficient",
568 return isl_aff_free(aff
));
569 if (type
== isl_dim_in
)
572 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
573 isl_die(aff
->v
->ctx
, isl_error_invalid
,
574 "position out of bounds", return isl_aff_free(aff
));
576 aff
= isl_aff_cow(aff
);
580 aff
->v
= isl_vec_cow(aff
->v
);
582 return isl_aff_free(aff
);
584 pos
+= isl_local_space_offset(aff
->ls
, type
);
585 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
590 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
591 enum isl_dim_type type
, int pos
, isl_int v
)
596 if (type
== isl_dim_out
)
597 isl_die(aff
->v
->ctx
, isl_error_invalid
,
598 "output/set dimension does not have a coefficient",
599 return isl_aff_free(aff
));
600 if (type
== isl_dim_in
)
603 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
604 isl_die(aff
->v
->ctx
, isl_error_invalid
,
605 "position out of bounds", return isl_aff_free(aff
));
607 aff
= isl_aff_cow(aff
);
611 aff
->v
= isl_vec_cow(aff
->v
);
613 return isl_aff_free(aff
);
615 pos
+= isl_local_space_offset(aff
->ls
, type
);
616 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
621 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
622 enum isl_dim_type type
, int pos
, int v
)
627 isl_int_set_si(t
, v
);
628 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
634 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
639 return isl_local_space_get_div(aff
->ls
, pos
);
642 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
644 aff
= isl_aff_cow(aff
);
647 aff
->v
= isl_vec_cow(aff
->v
);
649 return isl_aff_free(aff
);
651 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
656 /* Remove divs from the local space that do not appear in the affine
658 * We currently only remove divs at the end.
659 * Some intermediate divs may also not appear directly in the affine
660 * expression, but we would also need to check that no other divs are
661 * defined in terms of them.
663 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
672 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
673 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
675 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
679 aff
= isl_aff_cow(aff
);
683 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
684 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
685 if (!aff
->ls
|| !aff
->v
)
686 return isl_aff_free(aff
);
691 /* Given two affine expressions "p" of length p_len (including the
692 * denominator and the constant term) and "subs" of length subs_len,
693 * plug in "subs" for the variable at position "pos".
694 * The variables of "subs" and "p" are assumed to match up to subs_len,
695 * but "p" may have additional variables.
696 * "v" is an initialized isl_int that can be used internally.
698 * In particular, if "p" represents the expression
702 * with i the variable at position "pos" and "subs" represents the expression
706 * then the result represents the expression
711 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
712 int p_len
, int subs_len
, isl_int v
)
714 isl_int_set(v
, p
[1 + pos
]);
715 isl_int_set_si(p
[1 + pos
], 0);
716 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
717 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
718 isl_int_mul(p
[0], p
[0], subs
[0]);
721 /* Look for any divs in the aff->ls with a denominator equal to one
722 * and plug them into the affine expression and any subsequent divs
723 * that may reference the div.
725 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
737 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
739 for (i
= 0; i
< n
; ++i
) {
740 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
742 ls
= isl_local_space_copy(aff
->ls
);
743 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
744 aff
->ls
->div
->row
[i
], len
, i
+ 1);
745 vec
= isl_vec_copy(aff
->v
);
746 vec
= isl_vec_cow(vec
);
752 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
753 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
758 isl_vec_free(aff
->v
);
760 isl_local_space_free(aff
->ls
);
767 isl_local_space_free(ls
);
768 return isl_aff_free(aff
);
771 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
773 * Even though this function is only called on isl_affs with a single
774 * reference, we are careful to only change aff->v and aff->ls together.
776 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
778 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
782 ls
= isl_local_space_copy(aff
->ls
);
783 ls
= isl_local_space_swap_div(ls
, a
, b
);
784 v
= isl_vec_copy(aff
->v
);
789 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
790 isl_vec_free(aff
->v
);
792 isl_local_space_free(aff
->ls
);
798 isl_local_space_free(ls
);
799 return isl_aff_free(aff
);
802 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
804 * We currently do not actually remove div "b", but simply add its
805 * coefficient to that of "a" and then zero it out.
807 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
809 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
811 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
814 aff
->v
= isl_vec_cow(aff
->v
);
816 return isl_aff_free(aff
);
818 isl_int_add(aff
->v
->el
[1 + off
+ a
],
819 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
820 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
825 /* Sort the divs in the local space of "aff" according to
826 * the comparison function "cmp_row" in isl_local_space.c,
827 * combining the coefficients of identical divs.
829 * Reordering divs does not change the semantics of "aff",
830 * so there is no need to call isl_aff_cow.
831 * Moreover, this function is currently only called on isl_affs
832 * with a single reference.
834 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
842 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
843 n
= isl_aff_dim(aff
, isl_dim_div
);
844 for (i
= 1; i
< n
; ++i
) {
845 for (j
= i
- 1; j
>= 0; --j
) {
846 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
850 aff
= merge_divs(aff
, j
, j
+ 1);
852 aff
= swap_div(aff
, j
, j
+ 1);
861 /* Normalize the representation of "aff".
863 * This function should only be called of "new" isl_affs, i.e.,
864 * with only a single reference. We therefore do not need to
865 * worry about affecting other instances.
867 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
871 aff
->v
= isl_vec_normalize(aff
->v
);
873 return isl_aff_free(aff
);
874 aff
= plug_in_integral_divs(aff
);
875 aff
= sort_divs(aff
);
876 aff
= isl_aff_remove_unused_divs(aff
);
880 /* Given f, return floor(f).
881 * If f is an integer expression, then just return f.
882 * If f is a constant, then return the constant floor(f).
883 * Otherwise, if f = g/m, write g = q m + r,
884 * create a new div d = [r/m] and return the expression q + d.
885 * The coefficients in r are taken to lie between -m/2 and m/2.
887 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
897 if (isl_int_is_one(aff
->v
->el
[0]))
900 aff
= isl_aff_cow(aff
);
904 aff
->v
= isl_vec_cow(aff
->v
);
906 return isl_aff_free(aff
);
908 if (isl_aff_is_cst(aff
)) {
909 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
910 isl_int_set_si(aff
->v
->el
[0], 1);
914 div
= isl_vec_copy(aff
->v
);
915 div
= isl_vec_cow(div
);
917 return isl_aff_free(aff
);
919 ctx
= isl_aff_get_ctx(aff
);
920 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
921 for (i
= 1; i
< aff
->v
->size
; ++i
) {
922 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
923 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
924 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
925 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
926 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
930 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
932 return isl_aff_free(aff
);
935 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
937 return isl_aff_free(aff
);
938 isl_int_set_si(aff
->v
->el
[0], 1);
939 isl_int_set_si(aff
->v
->el
[size
], 1);
946 * aff mod m = aff - m * floor(aff/m)
948 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
952 res
= isl_aff_copy(aff
);
953 aff
= isl_aff_scale_down(aff
, m
);
954 aff
= isl_aff_floor(aff
);
955 aff
= isl_aff_scale(aff
, m
);
956 res
= isl_aff_sub(res
, aff
);
963 * pwaff mod m = pwaff - m * floor(pwaff/m)
965 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
969 res
= isl_pw_aff_copy(pwaff
);
970 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
971 pwaff
= isl_pw_aff_floor(pwaff
);
972 pwaff
= isl_pw_aff_scale(pwaff
, m
);
973 res
= isl_pw_aff_sub(res
, pwaff
);
978 /* Given f, return ceil(f).
979 * If f is an integer expression, then just return f.
980 * Otherwise, create a new div d = [-f] and return the expression -d.
982 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
987 if (isl_int_is_one(aff
->v
->el
[0]))
990 aff
= isl_aff_neg(aff
);
991 aff
= isl_aff_floor(aff
);
992 aff
= isl_aff_neg(aff
);
997 /* Apply the expansion computed by isl_merge_divs.
998 * The expansion itself is given by "exp" while the resulting
999 * list of divs is given by "div".
1001 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1002 __isl_take isl_mat
*div
, int *exp
)
1009 aff
= isl_aff_cow(aff
);
1013 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1014 new_n_div
= isl_mat_rows(div
);
1015 if (new_n_div
< old_n_div
)
1016 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1017 "not an expansion", goto error
);
1019 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1023 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1025 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1026 if (j
>= 0 && exp
[j
] == i
) {
1028 isl_int_swap(aff
->v
->el
[offset
+ i
],
1029 aff
->v
->el
[offset
+ j
]);
1032 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1035 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1046 /* Add two affine expressions that live in the same local space.
1048 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1049 __isl_take isl_aff
*aff2
)
1053 aff1
= isl_aff_cow(aff1
);
1057 aff1
->v
= isl_vec_cow(aff1
->v
);
1063 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1064 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1065 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1066 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1067 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1068 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1069 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1081 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1082 __isl_take isl_aff
*aff2
)
1092 ctx
= isl_aff_get_ctx(aff1
);
1093 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1094 isl_die(ctx
, isl_error_invalid
,
1095 "spaces don't match", goto error
);
1097 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
1098 return add_expanded(aff1
, aff2
);
1100 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
1101 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
1105 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1106 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1107 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1111 return add_expanded(aff1
, aff2
);
1120 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1121 __isl_take isl_aff
*aff2
)
1123 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1126 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1130 if (isl_int_is_one(f
))
1133 aff
= isl_aff_cow(aff
);
1136 aff
->v
= isl_vec_cow(aff
->v
);
1138 return isl_aff_free(aff
);
1141 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1142 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1143 isl_int_divexact(gcd
, f
, gcd
);
1144 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1150 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1154 if (isl_int_is_one(f
))
1157 aff
= isl_aff_cow(aff
);
1161 if (isl_int_is_zero(f
))
1162 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1163 "cannot scale down by zero", return isl_aff_free(aff
));
1165 aff
->v
= isl_vec_cow(aff
->v
);
1167 return isl_aff_free(aff
);
1170 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1171 isl_int_gcd(gcd
, gcd
, f
);
1172 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1173 isl_int_divexact(gcd
, f
, gcd
);
1174 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1180 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1188 isl_int_set_ui(v
, f
);
1189 aff
= isl_aff_scale_down(aff
, v
);
1195 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1196 enum isl_dim_type type
, unsigned pos
, const char *s
)
1198 aff
= isl_aff_cow(aff
);
1201 if (type
== isl_dim_out
)
1202 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1203 "cannot set name of output/set dimension",
1204 return isl_aff_free(aff
));
1205 if (type
== isl_dim_in
)
1207 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1209 return isl_aff_free(aff
);
1214 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1215 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1217 aff
= isl_aff_cow(aff
);
1219 return isl_id_free(id
);
1220 if (type
== isl_dim_out
)
1221 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1222 "cannot set name of output/set dimension",
1224 if (type
== isl_dim_in
)
1226 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1228 return isl_aff_free(aff
);
1237 /* Exploit the equalities in "eq" to simplify the affine expression
1238 * and the expressions of the integer divisions in the local space.
1239 * The integer divisions in this local space are assumed to appear
1240 * as regular dimensions in "eq".
1242 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1243 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1251 if (eq
->n_eq
== 0) {
1252 isl_basic_set_free(eq
);
1256 aff
= isl_aff_cow(aff
);
1260 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1261 isl_basic_set_copy(eq
));
1265 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1267 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1268 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1269 if (j
< 0 || j
== 0 || j
>= total
)
1272 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1276 isl_basic_set_free(eq
);
1277 aff
= isl_aff_normalize(aff
);
1280 isl_basic_set_free(eq
);
1285 /* Exploit the equalities in "eq" to simplify the affine expression
1286 * and the expressions of the integer divisions in the local space.
1288 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1289 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1295 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1297 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
1298 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1300 isl_basic_set_free(eq
);
1305 /* Look for equalities among the variables shared by context and aff
1306 * and the integer divisions of aff, if any.
1307 * The equalities are then used to eliminate coefficients and/or integer
1308 * divisions from aff.
1310 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1311 __isl_take isl_set
*context
)
1313 isl_basic_set
*hull
;
1318 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1320 isl_basic_set
*bset
;
1321 isl_local_space
*ls
;
1322 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1323 ls
= isl_aff_get_domain_local_space(aff
);
1324 bset
= isl_basic_set_from_local_space(ls
);
1325 bset
= isl_basic_set_lift(bset
);
1326 bset
= isl_basic_set_flatten(bset
);
1327 context
= isl_set_intersect(context
,
1328 isl_set_from_basic_set(bset
));
1331 hull
= isl_set_affine_hull(context
);
1332 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1335 isl_set_free(context
);
1339 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1340 __isl_take isl_set
*context
)
1342 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1343 dom_context
= isl_set_intersect_params(dom_context
, context
);
1344 return isl_aff_gist(aff
, dom_context
);
1347 /* Return a basic set containing those elements in the space
1348 * of aff where it is non-negative.
1350 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1352 isl_constraint
*ineq
;
1353 isl_basic_set
*bset
;
1355 ineq
= isl_inequality_from_aff(aff
);
1357 bset
= isl_basic_set_from_constraint(ineq
);
1358 bset
= isl_basic_set_simplify(bset
);
1362 /* Return a basic set containing those elements in the domain space
1363 * of aff where it is negative.
1365 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
1367 aff
= isl_aff_neg(aff
);
1368 aff
= isl_aff_add_constant_num_si(aff
, -1);
1369 return isl_aff_nonneg_basic_set(aff
);
1372 /* Return a basic set containing those elements in the space
1373 * of aff where it is zero.
1375 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1377 isl_constraint
*ineq
;
1378 isl_basic_set
*bset
;
1380 ineq
= isl_equality_from_aff(aff
);
1382 bset
= isl_basic_set_from_constraint(ineq
);
1383 bset
= isl_basic_set_simplify(bset
);
1387 /* Return a basic set containing those elements in the shared space
1388 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1390 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1391 __isl_take isl_aff
*aff2
)
1393 aff1
= isl_aff_sub(aff1
, aff2
);
1395 return isl_aff_nonneg_basic_set(aff1
);
1398 /* Return a basic set containing those elements in the shared space
1399 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1401 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1402 __isl_take isl_aff
*aff2
)
1404 return isl_aff_ge_basic_set(aff2
, aff1
);
1407 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1408 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1410 aff1
= isl_aff_add(aff1
, aff2
);
1411 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1415 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1423 /* Check whether the given affine expression has non-zero coefficient
1424 * for any dimension in the given range or if any of these dimensions
1425 * appear with non-zero coefficients in any of the integer divisions
1426 * involved in the affine expression.
1428 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1429 enum isl_dim_type type
, unsigned first
, unsigned n
)
1441 ctx
= isl_aff_get_ctx(aff
);
1442 if (first
+ n
> isl_aff_dim(aff
, type
))
1443 isl_die(ctx
, isl_error_invalid
,
1444 "range out of bounds", return -1);
1446 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1450 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1451 for (i
= 0; i
< n
; ++i
)
1452 if (active
[first
+ i
]) {
1465 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1466 enum isl_dim_type type
, unsigned first
, unsigned n
)
1472 if (type
== isl_dim_out
)
1473 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1474 "cannot drop output/set dimension",
1475 return isl_aff_free(aff
));
1476 if (type
== isl_dim_in
)
1478 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1481 ctx
= isl_aff_get_ctx(aff
);
1482 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1483 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1484 return isl_aff_free(aff
));
1486 aff
= isl_aff_cow(aff
);
1490 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1492 return isl_aff_free(aff
);
1494 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1495 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1497 return isl_aff_free(aff
);
1502 /* Project the domain of the affine expression onto its parameter space.
1503 * The affine expression may not involve any of the domain dimensions.
1505 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
1511 n
= isl_aff_dim(aff
, isl_dim_in
);
1512 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
1514 return isl_aff_free(aff
);
1516 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1517 "affine expression involves some of the domain dimensions",
1518 return isl_aff_free(aff
));
1519 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
1520 space
= isl_aff_get_domain_space(aff
);
1521 space
= isl_space_params(space
);
1522 aff
= isl_aff_reset_domain_space(aff
, space
);
1526 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1527 enum isl_dim_type type
, unsigned first
, unsigned n
)
1533 if (type
== isl_dim_out
)
1534 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1535 "cannot insert output/set dimensions",
1536 return isl_aff_free(aff
));
1537 if (type
== isl_dim_in
)
1539 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1542 ctx
= isl_aff_get_ctx(aff
);
1543 if (first
> isl_local_space_dim(aff
->ls
, type
))
1544 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1545 return isl_aff_free(aff
));
1547 aff
= isl_aff_cow(aff
);
1551 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1553 return isl_aff_free(aff
);
1555 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1556 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1558 return isl_aff_free(aff
);
1563 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1564 enum isl_dim_type type
, unsigned n
)
1568 pos
= isl_aff_dim(aff
, type
);
1570 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1573 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1574 enum isl_dim_type type
, unsigned n
)
1578 pos
= isl_pw_aff_dim(pwaff
, type
);
1580 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1583 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1585 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1586 return isl_pw_aff_alloc(dom
, aff
);
1590 #define PW isl_pw_aff
1594 #define EL_IS_ZERO is_empty
1598 #define IS_ZERO is_empty
1601 #undef DEFAULT_IS_ZERO
1602 #define DEFAULT_IS_ZERO 0
1606 #define NO_MOVE_DIMS
1610 #include <isl_pw_templ.c>
1612 static __isl_give isl_set
*align_params_pw_pw_set_and(
1613 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1614 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1615 __isl_take isl_pw_aff
*pwaff2
))
1617 if (!pwaff1
|| !pwaff2
)
1619 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1620 pwaff2
->dim
, isl_dim_param
))
1621 return fn(pwaff1
, pwaff2
);
1622 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1623 !isl_space_has_named_params(pwaff2
->dim
))
1624 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1625 "unaligned unnamed parameters", goto error
);
1626 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1627 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1628 return fn(pwaff1
, pwaff2
);
1630 isl_pw_aff_free(pwaff1
);
1631 isl_pw_aff_free(pwaff2
);
1635 /* Compute a piecewise quasi-affine expression with a domain that
1636 * is the union of those of pwaff1 and pwaff2 and such that on each
1637 * cell, the quasi-affine expression is the better (according to cmp)
1638 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1639 * is defined on a given cell, then the associated expression
1640 * is the defined one.
1642 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1643 __isl_take isl_pw_aff
*pwaff2
,
1644 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1645 __isl_take isl_aff
*aff2
))
1652 if (!pwaff1
|| !pwaff2
)
1655 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1656 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1657 isl_die(ctx
, isl_error_invalid
,
1658 "arguments should live in same space", goto error
);
1660 if (isl_pw_aff_is_empty(pwaff1
)) {
1661 isl_pw_aff_free(pwaff1
);
1665 if (isl_pw_aff_is_empty(pwaff2
)) {
1666 isl_pw_aff_free(pwaff2
);
1670 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1671 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1673 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1674 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1675 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1676 struct isl_set
*common
;
1679 common
= isl_set_intersect(
1680 isl_set_copy(pwaff1
->p
[i
].set
),
1681 isl_set_copy(pwaff2
->p
[j
].set
));
1682 better
= isl_set_from_basic_set(cmp(
1683 isl_aff_copy(pwaff2
->p
[j
].aff
),
1684 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1685 better
= isl_set_intersect(common
, better
);
1686 if (isl_set_plain_is_empty(better
)) {
1687 isl_set_free(better
);
1690 set
= isl_set_subtract(set
, isl_set_copy(better
));
1692 res
= isl_pw_aff_add_piece(res
, better
,
1693 isl_aff_copy(pwaff2
->p
[j
].aff
));
1695 res
= isl_pw_aff_add_piece(res
, set
,
1696 isl_aff_copy(pwaff1
->p
[i
].aff
));
1699 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1700 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1701 for (i
= 0; i
< pwaff1
->n
; ++i
)
1702 set
= isl_set_subtract(set
,
1703 isl_set_copy(pwaff1
->p
[i
].set
));
1704 res
= isl_pw_aff_add_piece(res
, set
,
1705 isl_aff_copy(pwaff2
->p
[j
].aff
));
1708 isl_pw_aff_free(pwaff1
);
1709 isl_pw_aff_free(pwaff2
);
1713 isl_pw_aff_free(pwaff1
);
1714 isl_pw_aff_free(pwaff2
);
1718 /* Compute a piecewise quasi-affine expression with a domain that
1719 * is the union of those of pwaff1 and pwaff2 and such that on each
1720 * cell, the quasi-affine expression is the maximum of those of pwaff1
1721 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1722 * cell, then the associated expression is the defined one.
1724 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1725 __isl_take isl_pw_aff
*pwaff2
)
1727 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1730 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1731 __isl_take isl_pw_aff
*pwaff2
)
1733 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1737 /* Compute a piecewise quasi-affine expression with a domain that
1738 * is the union of those of pwaff1 and pwaff2 and such that on each
1739 * cell, the quasi-affine expression is the minimum of those of pwaff1
1740 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1741 * cell, then the associated expression is the defined one.
1743 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1744 __isl_take isl_pw_aff
*pwaff2
)
1746 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1749 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1750 __isl_take isl_pw_aff
*pwaff2
)
1752 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1756 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1757 __isl_take isl_pw_aff
*pwaff2
, int max
)
1760 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1762 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1765 /* Construct a map with as domain the domain of pwaff and
1766 * one-dimensional range corresponding to the affine expressions.
1768 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1777 dim
= isl_pw_aff_get_space(pwaff
);
1778 map
= isl_map_empty(dim
);
1780 for (i
= 0; i
< pwaff
->n
; ++i
) {
1781 isl_basic_map
*bmap
;
1784 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1785 map_i
= isl_map_from_basic_map(bmap
);
1786 map_i
= isl_map_intersect_domain(map_i
,
1787 isl_set_copy(pwaff
->p
[i
].set
));
1788 map
= isl_map_union_disjoint(map
, map_i
);
1791 isl_pw_aff_free(pwaff
);
1796 /* Construct a map with as domain the domain of pwaff and
1797 * one-dimensional range corresponding to the affine expressions.
1799 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1803 if (isl_space_is_set(pwaff
->dim
))
1804 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1805 "space of input is not a map",
1806 return isl_pw_aff_free(pwaff
));
1807 return map_from_pw_aff(pwaff
);
1810 /* Construct a one-dimensional set with as parameter domain
1811 * the domain of pwaff and the single set dimension
1812 * corresponding to the affine expressions.
1814 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1818 if (!isl_space_is_set(pwaff
->dim
))
1819 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1820 "space of input is not a set",
1821 return isl_pw_aff_free(pwaff
));
1822 return map_from_pw_aff(pwaff
);
1825 /* Return a set containing those elements in the domain
1826 * of pwaff where it is non-negative.
1828 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1836 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1838 for (i
= 0; i
< pwaff
->n
; ++i
) {
1839 isl_basic_set
*bset
;
1842 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1843 set_i
= isl_set_from_basic_set(bset
);
1844 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1845 set
= isl_set_union_disjoint(set
, set_i
);
1848 isl_pw_aff_free(pwaff
);
1853 /* Return a set containing those elements in the domain
1854 * of pwaff where it is zero (if complement is 0) or not zero
1855 * (if complement is 1).
1857 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
1866 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1868 for (i
= 0; i
< pwaff
->n
; ++i
) {
1869 isl_basic_set
*bset
;
1870 isl_set
*set_i
, *zero
;
1872 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1873 zero
= isl_set_from_basic_set(bset
);
1874 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
1876 set_i
= isl_set_subtract(set_i
, zero
);
1878 set_i
= isl_set_intersect(set_i
, zero
);
1879 set
= isl_set_union_disjoint(set
, set_i
);
1882 isl_pw_aff_free(pwaff
);
1887 /* Return a set containing those elements in the domain
1888 * of pwaff where it is zero.
1890 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1892 return pw_aff_zero_set(pwaff
, 0);
1895 /* Return a set containing those elements in the domain
1896 * of pwaff where it is not zero.
1898 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1900 return pw_aff_zero_set(pwaff
, 1);
1903 /* Return a set containing those elements in the shared domain
1904 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1906 * We compute the difference on the shared domain and then construct
1907 * the set of values where this difference is non-negative.
1908 * If strict is set, we first subtract 1 from the difference.
1909 * If equal is set, we only return the elements where pwaff1 and pwaff2
1912 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1913 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1915 isl_set
*set1
, *set2
;
1917 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1918 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1919 set1
= isl_set_intersect(set1
, set2
);
1920 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1921 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1922 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1925 isl_space
*dim
= isl_set_get_space(set1
);
1927 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1928 aff
= isl_aff_add_constant_si(aff
, -1);
1929 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1934 return isl_pw_aff_zero_set(pwaff1
);
1935 return isl_pw_aff_nonneg_set(pwaff1
);
1938 /* Return a set containing those elements in the shared domain
1939 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1941 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1942 __isl_take isl_pw_aff
*pwaff2
)
1944 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1947 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1948 __isl_take isl_pw_aff
*pwaff2
)
1950 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1953 /* Return a set containing those elements in the shared domain
1954 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1956 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1957 __isl_take isl_pw_aff
*pwaff2
)
1959 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1962 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1963 __isl_take isl_pw_aff
*pwaff2
)
1965 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1968 /* Return a set containing those elements in the shared domain
1969 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1971 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1972 __isl_take isl_pw_aff
*pwaff2
)
1974 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1977 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1978 __isl_take isl_pw_aff
*pwaff2
)
1980 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1983 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1984 __isl_take isl_pw_aff
*pwaff2
)
1986 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1989 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1990 __isl_take isl_pw_aff
*pwaff2
)
1992 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
1995 /* Return a set containing those elements in the shared domain
1996 * of the elements of list1 and list2 where each element in list1
1997 * has the relation specified by "fn" with each element in list2.
1999 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2000 __isl_take isl_pw_aff_list
*list2
,
2001 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2002 __isl_take isl_pw_aff
*pwaff2
))
2008 if (!list1
|| !list2
)
2011 ctx
= isl_pw_aff_list_get_ctx(list1
);
2012 if (list1
->n
< 1 || list2
->n
< 1)
2013 isl_die(ctx
, isl_error_invalid
,
2014 "list should contain at least one element", goto error
);
2016 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2017 for (i
= 0; i
< list1
->n
; ++i
)
2018 for (j
= 0; j
< list2
->n
; ++j
) {
2021 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2022 isl_pw_aff_copy(list2
->p
[j
]));
2023 set
= isl_set_intersect(set
, set_ij
);
2026 isl_pw_aff_list_free(list1
);
2027 isl_pw_aff_list_free(list2
);
2030 isl_pw_aff_list_free(list1
);
2031 isl_pw_aff_list_free(list2
);
2035 /* Return a set containing those elements in the shared domain
2036 * of the elements of list1 and list2 where each element in list1
2037 * is equal to each element in list2.
2039 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2040 __isl_take isl_pw_aff_list
*list2
)
2042 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2045 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2046 __isl_take isl_pw_aff_list
*list2
)
2048 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2051 /* Return a set containing those elements in the shared domain
2052 * of the elements of list1 and list2 where each element in list1
2053 * is less than or equal to each element in list2.
2055 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2056 __isl_take isl_pw_aff_list
*list2
)
2058 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2061 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2062 __isl_take isl_pw_aff_list
*list2
)
2064 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2067 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2068 __isl_take isl_pw_aff_list
*list2
)
2070 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
2073 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
2074 __isl_take isl_pw_aff_list
*list2
)
2076 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
2080 /* Return a set containing those elements in the shared domain
2081 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2083 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2084 __isl_take isl_pw_aff
*pwaff2
)
2086 isl_set
*set_lt
, *set_gt
;
2088 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
2089 isl_pw_aff_copy(pwaff2
));
2090 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
2091 return isl_set_union_disjoint(set_lt
, set_gt
);
2094 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2095 __isl_take isl_pw_aff
*pwaff2
)
2097 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
2100 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
2105 if (isl_int_is_one(v
))
2107 if (!isl_int_is_pos(v
))
2108 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2109 "factor needs to be positive",
2110 return isl_pw_aff_free(pwaff
));
2111 pwaff
= isl_pw_aff_cow(pwaff
);
2117 for (i
= 0; i
< pwaff
->n
; ++i
) {
2118 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
2119 if (!pwaff
->p
[i
].aff
)
2120 return isl_pw_aff_free(pwaff
);
2126 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
2130 pwaff
= isl_pw_aff_cow(pwaff
);
2136 for (i
= 0; i
< pwaff
->n
; ++i
) {
2137 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
2138 if (!pwaff
->p
[i
].aff
)
2139 return isl_pw_aff_free(pwaff
);
2145 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
2149 pwaff
= isl_pw_aff_cow(pwaff
);
2155 for (i
= 0; i
< pwaff
->n
; ++i
) {
2156 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
2157 if (!pwaff
->p
[i
].aff
)
2158 return isl_pw_aff_free(pwaff
);
2164 /* Assuming that "cond1" and "cond2" are disjoint,
2165 * return an affine expression that is equal to pwaff1 on cond1
2166 * and to pwaff2 on cond2.
2168 static __isl_give isl_pw_aff
*isl_pw_aff_select(
2169 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
2170 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
2172 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
2173 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
2175 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
2178 /* Return an affine expression that is equal to pwaff_true for elements
2179 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2181 * That is, return cond ? pwaff_true : pwaff_false;
2183 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
2184 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
2186 isl_set
*cond_true
, *cond_false
;
2188 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
2189 cond_false
= isl_pw_aff_zero_set(cond
);
2190 return isl_pw_aff_select(cond_true
, pwaff_true
,
2191 cond_false
, pwaff_false
);
2194 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
2199 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
2202 /* Check whether pwaff is a piecewise constant.
2204 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
2211 for (i
= 0; i
< pwaff
->n
; ++i
) {
2212 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
2213 if (is_cst
< 0 || !is_cst
)
2220 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
2221 __isl_take isl_aff
*aff2
)
2223 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2224 return isl_aff_mul(aff2
, aff1
);
2226 if (!isl_aff_is_cst(aff2
))
2227 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2228 "at least one affine expression should be constant",
2231 aff1
= isl_aff_cow(aff1
);
2235 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2236 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2246 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2248 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
2249 __isl_take isl_aff
*aff2
)
2254 is_cst
= isl_aff_is_cst(aff2
);
2258 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
2259 "second argument should be a constant", goto error
);
2264 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
2266 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2267 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2270 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
2271 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
2274 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2275 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2286 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2287 __isl_take isl_pw_aff
*pwaff2
)
2289 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2292 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2293 __isl_take isl_pw_aff
*pwaff2
)
2295 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2298 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2299 __isl_take isl_pw_aff
*pwaff2
)
2301 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2304 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2305 __isl_take isl_pw_aff
*pwaff2
)
2307 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2310 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2311 __isl_take isl_pw_aff
*pwaff2
)
2313 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2316 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2317 __isl_take isl_pw_aff
*pa2
)
2319 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
2322 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2324 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2325 __isl_take isl_pw_aff
*pa2
)
2329 is_cst
= isl_pw_aff_is_cst(pa2
);
2333 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
2334 "second argument should be a piecewise constant",
2336 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
2338 isl_pw_aff_free(pa1
);
2339 isl_pw_aff_free(pa2
);
2343 /* Compute the quotient of the integer division of "pa1" by "pa2"
2344 * with rounding towards zero.
2345 * "pa2" is assumed to be a piecewise constant.
2347 * In particular, return
2349 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2352 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
2353 __isl_take isl_pw_aff
*pa2
)
2359 is_cst
= isl_pw_aff_is_cst(pa2
);
2363 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
2364 "second argument should be a piecewise constant",
2367 pa1
= isl_pw_aff_div(pa1
, pa2
);
2369 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
2370 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
2371 c
= isl_pw_aff_ceil(pa1
);
2372 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
2374 isl_pw_aff_free(pa1
);
2375 isl_pw_aff_free(pa2
);
2379 /* Compute the remainder of the integer division of "pa1" by "pa2"
2380 * with rounding towards zero.
2381 * "pa2" is assumed to be a piecewise constant.
2383 * In particular, return
2385 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2388 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
2389 __isl_take isl_pw_aff
*pa2
)
2394 is_cst
= isl_pw_aff_is_cst(pa2
);
2398 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
2399 "second argument should be a piecewise constant",
2401 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
2402 res
= isl_pw_aff_mul(pa2
, res
);
2403 res
= isl_pw_aff_sub(pa1
, res
);
2406 isl_pw_aff_free(pa1
);
2407 isl_pw_aff_free(pa2
);
2411 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2412 __isl_take isl_pw_aff
*pwaff2
)
2417 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2418 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2419 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
2420 isl_pw_aff_copy(pwaff2
));
2421 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
2422 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
2425 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2426 __isl_take isl_pw_aff
*pwaff2
)
2428 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
2431 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2432 __isl_take isl_pw_aff
*pwaff2
)
2437 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2438 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2439 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2440 isl_pw_aff_copy(pwaff2
));
2441 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
2442 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
2445 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2446 __isl_take isl_pw_aff
*pwaff2
)
2448 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2451 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2452 __isl_take isl_pw_aff_list
*list
,
2453 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2454 __isl_take isl_pw_aff
*pwaff2
))
2463 ctx
= isl_pw_aff_list_get_ctx(list
);
2465 isl_die(ctx
, isl_error_invalid
,
2466 "list should contain at least one element",
2467 return isl_pw_aff_list_free(list
));
2469 res
= isl_pw_aff_copy(list
->p
[0]);
2470 for (i
= 1; i
< list
->n
; ++i
)
2471 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2473 isl_pw_aff_list_free(list
);
2477 /* Return an isl_pw_aff that maps each element in the intersection of the
2478 * domains of the elements of list to the minimal corresponding affine
2481 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2483 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2486 /* Return an isl_pw_aff that maps each element in the intersection of the
2487 * domains of the elements of list to the maximal corresponding affine
2490 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2492 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2498 #include <isl_multi_templ.c>
2500 /* Construct an isl_multi_aff in the given space with value zero in
2501 * each of the output dimensions.
2503 __isl_give isl_multi_aff
*isl_multi_aff_zero(__isl_take isl_space
*space
)
2511 n
= isl_space_dim(space
, isl_dim_out
);
2512 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2515 isl_space_free(space
);
2518 isl_local_space
*ls
;
2521 space
= isl_space_domain(space
);
2522 ls
= isl_local_space_from_space(space
);
2523 aff
= isl_aff_zero_on_domain(ls
);
2525 for (i
= 0; i
< n
; ++i
)
2526 ma
= isl_multi_aff_set_aff(ma
, i
, isl_aff_copy(aff
));
2534 /* Create an isl_multi_aff in the given space that maps each
2535 * input dimension to the corresponding output dimension.
2537 __isl_give isl_multi_aff
*isl_multi_aff_identity(__isl_take isl_space
*space
)
2545 if (isl_space_is_set(space
))
2546 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2547 "expecting map space", goto error
);
2549 n
= isl_space_dim(space
, isl_dim_out
);
2550 if (n
!= isl_space_dim(space
, isl_dim_in
))
2551 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2552 "number of input and output dimensions needs to be "
2553 "the same", goto error
);
2555 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2558 isl_space_free(space
);
2561 isl_local_space
*ls
;
2564 space
= isl_space_domain(space
);
2565 ls
= isl_local_space_from_space(space
);
2566 aff
= isl_aff_zero_on_domain(ls
);
2568 for (i
= 0; i
< n
; ++i
) {
2570 aff_i
= isl_aff_copy(aff
);
2571 aff_i
= isl_aff_add_coefficient_si(aff_i
,
2573 ma
= isl_multi_aff_set_aff(ma
, i
, aff_i
);
2581 isl_space_free(space
);
2585 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2588 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
2589 __isl_take isl_multi_aff
*ma
)
2591 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
2592 return isl_pw_multi_aff_alloc(dom
, ma
);
2595 /* Create a piecewise multi-affine expression in the given space that maps each
2596 * input dimension to the corresponding output dimension.
2598 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
2599 __isl_take isl_space
*space
)
2601 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
2604 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2605 __isl_take isl_multi_aff
*maff2
)
2610 maff1
= isl_multi_aff_cow(maff1
);
2611 if (!maff1
|| !maff2
)
2614 ctx
= isl_multi_aff_get_ctx(maff1
);
2615 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2616 isl_die(ctx
, isl_error_invalid
,
2617 "spaces don't match", goto error
);
2619 for (i
= 0; i
< maff1
->n
; ++i
) {
2620 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2621 isl_aff_copy(maff2
->p
[i
]));
2626 isl_multi_aff_free(maff2
);
2629 isl_multi_aff_free(maff1
);
2630 isl_multi_aff_free(maff2
);
2634 /* Given two multi-affine expressions A -> B and C -> D,
2635 * construct a multi-affine expression [A -> C] -> [B -> D].
2637 __isl_give isl_multi_aff
*isl_multi_aff_product(
2638 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
2644 int in1
, in2
, out1
, out2
;
2646 in1
= isl_multi_aff_dim(ma1
, isl_dim_in
);
2647 in2
= isl_multi_aff_dim(ma2
, isl_dim_in
);
2648 out1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
2649 out2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
2650 space
= isl_space_product(isl_multi_aff_get_space(ma1
),
2651 isl_multi_aff_get_space(ma2
));
2652 res
= isl_multi_aff_alloc(isl_space_copy(space
));
2653 space
= isl_space_domain(space
);
2655 for (i
= 0; i
< out1
; ++i
) {
2656 aff
= isl_multi_aff_get_aff(ma1
, i
);
2657 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, in1
, in2
);
2658 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2659 res
= isl_multi_aff_set_aff(res
, i
, aff
);
2662 for (i
= 0; i
< out2
; ++i
) {
2663 aff
= isl_multi_aff_get_aff(ma2
, i
);
2664 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, 0, in1
);
2665 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2666 res
= isl_multi_aff_set_aff(res
, out1
+ i
, aff
);
2669 isl_space_free(space
);
2670 isl_multi_aff_free(ma1
);
2671 isl_multi_aff_free(ma2
);
2675 /* Exploit the equalities in "eq" to simplify the affine expressions.
2677 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2678 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2682 maff
= isl_multi_aff_cow(maff
);
2686 for (i
= 0; i
< maff
->n
; ++i
) {
2687 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2688 isl_basic_set_copy(eq
));
2693 isl_basic_set_free(eq
);
2696 isl_basic_set_free(eq
);
2697 isl_multi_aff_free(maff
);
2701 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2706 maff
= isl_multi_aff_cow(maff
);
2710 for (i
= 0; i
< maff
->n
; ++i
) {
2711 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2713 return isl_multi_aff_free(maff
);
2719 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2720 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2722 maff1
= isl_multi_aff_add(maff1
, maff2
);
2723 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2727 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2735 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2736 __isl_keep isl_multi_aff
*maff2
)
2741 if (!maff1
|| !maff2
)
2743 if (maff1
->n
!= maff2
->n
)
2745 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2746 if (equal
< 0 || !equal
)
2749 for (i
= 0; i
< maff1
->n
; ++i
) {
2750 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2751 if (equal
< 0 || !equal
)
2758 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2759 __isl_take isl_multi_aff
*maff
,
2760 enum isl_dim_type type
, unsigned pos
, const char *s
)
2764 maff
= isl_multi_aff_cow(maff
);
2768 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2770 return isl_multi_aff_free(maff
);
2772 if (type
== isl_dim_out
)
2774 for (i
= 0; i
< maff
->n
; ++i
) {
2775 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2777 return isl_multi_aff_free(maff
);
2783 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2784 enum isl_dim_type type
, unsigned first
, unsigned n
)
2788 maff
= isl_multi_aff_cow(maff
);
2792 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2794 return isl_multi_aff_free(maff
);
2796 if (type
== isl_dim_out
) {
2797 for (i
= 0; i
< n
; ++i
)
2798 isl_aff_free(maff
->p
[first
+ i
]);
2799 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2800 maff
->p
[i
] = maff
->p
[i
+ n
];
2805 for (i
= 0; i
< maff
->n
; ++i
) {
2806 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2808 return isl_multi_aff_free(maff
);
2814 /* Return the set of domain elements where "ma1" is lexicographically
2815 * smaller than or equal to "ma2".
2817 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
2818 __isl_take isl_multi_aff
*ma2
)
2820 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
2823 /* Return the set of domain elements where "ma1" is lexicographically
2824 * greater than or equal to "ma2".
2826 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
2827 __isl_take isl_multi_aff
*ma2
)
2830 isl_map
*map1
, *map2
;
2833 map1
= isl_map_from_multi_aff(ma1
);
2834 map2
= isl_map_from_multi_aff(ma2
);
2835 map
= isl_map_range_product(map1
, map2
);
2836 space
= isl_space_range(isl_map_get_space(map
));
2837 space
= isl_space_domain(isl_space_unwrap(space
));
2838 ge
= isl_map_lex_ge(space
);
2839 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
2841 return isl_map_domain(map
);
2845 #define PW isl_pw_multi_aff
2847 #define EL isl_multi_aff
2849 #define EL_IS_ZERO is_empty
2853 #define IS_ZERO is_empty
2856 #undef DEFAULT_IS_ZERO
2857 #define DEFAULT_IS_ZERO 0
2862 #define NO_INVOLVES_DIMS
2863 #define NO_MOVE_DIMS
2864 #define NO_INSERT_DIMS
2868 #include <isl_pw_templ.c>
2871 #define UNION isl_union_pw_multi_aff
2873 #define PART isl_pw_multi_aff
2875 #define PARTS pw_multi_aff
2876 #define ALIGN_DOMAIN
2880 #include <isl_union_templ.c>
2882 /* Given a function "cmp" that returns the set of elements where
2883 * "ma1" is "better" than "ma2", return the intersection of this
2884 * set with "dom1" and "dom2".
2886 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
2887 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
2888 __isl_keep isl_multi_aff
*ma2
,
2889 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2890 __isl_take isl_multi_aff
*ma2
))
2896 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
2897 is_empty
= isl_set_plain_is_empty(common
);
2898 if (is_empty
>= 0 && is_empty
)
2901 return isl_set_free(common
);
2902 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
2903 better
= isl_set_intersect(common
, better
);
2908 /* Given a function "cmp" that returns the set of elements where
2909 * "ma1" is "better" than "ma2", return a piecewise multi affine
2910 * expression defined on the union of the definition domains
2911 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2912 * "pma2" on each cell. If only one of the two input functions
2913 * is defined on a given cell, then it is considered the best.
2915 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
2916 __isl_take isl_pw_multi_aff
*pma1
,
2917 __isl_take isl_pw_multi_aff
*pma2
,
2918 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2919 __isl_take isl_multi_aff
*ma2
))
2922 isl_pw_multi_aff
*res
= NULL
;
2924 isl_set
*set
= NULL
;
2929 ctx
= isl_space_get_ctx(pma1
->dim
);
2930 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
2931 isl_die(ctx
, isl_error_invalid
,
2932 "arguments should live in the same space", goto error
);
2934 if (isl_pw_multi_aff_is_empty(pma1
)) {
2935 isl_pw_multi_aff_free(pma1
);
2939 if (isl_pw_multi_aff_is_empty(pma2
)) {
2940 isl_pw_multi_aff_free(pma2
);
2944 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
2945 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
2947 for (i
= 0; i
< pma1
->n
; ++i
) {
2948 set
= isl_set_copy(pma1
->p
[i
].set
);
2949 for (j
= 0; j
< pma2
->n
; ++j
) {
2953 better
= shared_and_better(pma2
->p
[j
].set
,
2954 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
2955 pma1
->p
[i
].maff
, cmp
);
2956 is_empty
= isl_set_plain_is_empty(better
);
2957 if (is_empty
< 0 || is_empty
) {
2958 isl_set_free(better
);
2963 set
= isl_set_subtract(set
, isl_set_copy(better
));
2965 res
= isl_pw_multi_aff_add_piece(res
, better
,
2966 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2968 res
= isl_pw_multi_aff_add_piece(res
, set
,
2969 isl_multi_aff_copy(pma1
->p
[i
].maff
));
2972 for (j
= 0; j
< pma2
->n
; ++j
) {
2973 set
= isl_set_copy(pma2
->p
[j
].set
);
2974 for (i
= 0; i
< pma1
->n
; ++i
)
2975 set
= isl_set_subtract(set
,
2976 isl_set_copy(pma1
->p
[i
].set
));
2977 res
= isl_pw_multi_aff_add_piece(res
, set
,
2978 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2981 isl_pw_multi_aff_free(pma1
);
2982 isl_pw_multi_aff_free(pma2
);
2986 isl_pw_multi_aff_free(pma1
);
2987 isl_pw_multi_aff_free(pma2
);
2989 return isl_pw_multi_aff_free(res
);
2992 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
2993 __isl_take isl_pw_multi_aff
*pma1
,
2994 __isl_take isl_pw_multi_aff
*pma2
)
2996 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
2999 /* Given two piecewise multi affine expressions, return a piecewise
3000 * multi-affine expression defined on the union of the definition domains
3001 * of the inputs that is equal to the lexicographic maximum of the two
3002 * inputs on each cell. If only one of the two inputs is defined on
3003 * a given cell, then it is considered to be the maximum.
3005 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
3006 __isl_take isl_pw_multi_aff
*pma1
,
3007 __isl_take isl_pw_multi_aff
*pma2
)
3009 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3010 &pw_multi_aff_union_lexmax
);
3013 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
3014 __isl_take isl_pw_multi_aff
*pma1
,
3015 __isl_take isl_pw_multi_aff
*pma2
)
3017 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
3020 /* Given two piecewise multi affine expressions, return a piecewise
3021 * multi-affine expression defined on the union of the definition domains
3022 * of the inputs that is equal to the lexicographic minimum of the two
3023 * inputs on each cell. If only one of the two inputs is defined on
3024 * a given cell, then it is considered to be the minimum.
3026 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
3027 __isl_take isl_pw_multi_aff
*pma1
,
3028 __isl_take isl_pw_multi_aff
*pma2
)
3030 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3031 &pw_multi_aff_union_lexmin
);
3034 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
3035 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3037 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3038 &isl_multi_aff_add
);
3041 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
3042 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3044 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3048 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
3049 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3051 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
3054 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3055 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3057 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
3058 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3062 isl_pw_multi_aff
*res
;
3067 n
= pma1
->n
* pma2
->n
;
3068 space
= isl_space_product(isl_space_copy(pma1
->dim
),
3069 isl_space_copy(pma2
->dim
));
3070 res
= isl_pw_multi_aff_alloc_size(space
, n
);
3072 for (i
= 0; i
< pma1
->n
; ++i
) {
3073 for (j
= 0; j
< pma2
->n
; ++j
) {
3077 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
3078 isl_set_copy(pma2
->p
[j
].set
));
3079 ma
= isl_multi_aff_product(
3080 isl_multi_aff_copy(pma1
->p
[i
].maff
),
3081 isl_multi_aff_copy(pma2
->p
[i
].maff
));
3082 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
3086 isl_pw_multi_aff_free(pma1
);
3087 isl_pw_multi_aff_free(pma2
);
3090 isl_pw_multi_aff_free(pma1
);
3091 isl_pw_multi_aff_free(pma2
);
3095 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
3096 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3098 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3099 &pw_multi_aff_product
);
3102 /* Construct a map mapping the domain of the piecewise multi-affine expression
3103 * to its range, with each dimension in the range equated to the
3104 * corresponding affine expression on its cell.
3106 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3114 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
3116 for (i
= 0; i
< pma
->n
; ++i
) {
3117 isl_multi_aff
*maff
;
3118 isl_basic_map
*bmap
;
3121 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
3122 bmap
= isl_basic_map_from_multi_aff(maff
);
3123 map_i
= isl_map_from_basic_map(bmap
);
3124 map_i
= isl_map_intersect_domain(map_i
,
3125 isl_set_copy(pma
->p
[i
].set
));
3126 map
= isl_map_union_disjoint(map
, map_i
);
3129 isl_pw_multi_aff_free(pma
);
3133 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3138 if (!isl_space_is_set(pma
->dim
))
3139 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3140 "isl_pw_multi_aff cannot be converted into an isl_set",
3141 return isl_pw_multi_aff_free(pma
));
3143 return isl_map_from_pw_multi_aff(pma
);
3146 /* Given a basic map with a single output dimension that is defined
3147 * in terms of the parameters and input dimensions using an equality,
3148 * extract an isl_aff that expresses the output dimension in terms
3149 * of the parameters and input dimensions.
3151 * Since some applications expect the result of isl_pw_multi_aff_from_map
3152 * to only contain integer affine expressions, we compute the floor
3153 * of the expression before returning.
3155 * This function shares some similarities with
3156 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3158 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
3159 __isl_take isl_basic_map
*bmap
)
3164 isl_local_space
*ls
;
3169 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
3170 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3171 "basic map should have a single output dimension",
3173 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
3174 total
= isl_basic_map_total_dim(bmap
);
3175 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3176 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
3178 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
3179 1 + total
- (offset
+ 1)) != -1)
3183 if (i
>= bmap
->n_eq
)
3184 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3185 "unable to find suitable equality", goto error
);
3186 ls
= isl_basic_map_get_local_space(bmap
);
3187 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
3190 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
3191 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3193 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3194 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
3195 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
3196 isl_basic_map_free(bmap
);
3198 aff
= isl_aff_remove_unused_divs(aff
);
3199 aff
= isl_aff_floor(aff
);
3202 isl_basic_map_free(bmap
);
3206 /* Given a basic map where each output dimension is defined
3207 * in terms of the parameters and input dimensions using an equality,
3208 * extract an isl_multi_aff that expresses the output dimensions in terms
3209 * of the parameters and input dimensions.
3211 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
3212 __isl_take isl_basic_map
*bmap
)
3221 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
3222 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3224 for (i
= 0; i
< n_out
; ++i
) {
3225 isl_basic_map
*bmap_i
;
3228 bmap_i
= isl_basic_map_copy(bmap
);
3229 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
3230 i
+ 1, n_out
- (1 + i
));
3231 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
3232 aff
= extract_isl_aff_from_basic_map(bmap_i
);
3233 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3236 isl_basic_map_free(bmap
);
3241 /* Create an isl_pw_multi_aff that is equivalent to
3242 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3243 * The given basic map is such that each output dimension is defined
3244 * in terms of the parameters and input dimensions using an equality.
3246 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
3247 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
3251 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
3252 return isl_pw_multi_aff_alloc(domain
, ma
);
3255 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3256 * This obviously only works if the input "map" is single-valued.
3257 * If so, we compute the lexicographic minimum of the image in the form
3258 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3259 * to its lexicographic minimum.
3260 * If the input is not single-valued, we produce an error.
3262 * As a special case, we first check if all output dimensions are uniquely
3263 * defined in terms of the parameters and input dimensions over the entire
3264 * domain. If so, we extract the desired isl_pw_multi_aff directly
3265 * from the affine hull of "map" and its domain.
3267 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
3271 isl_pw_multi_aff
*pma
;
3272 isl_basic_map
*hull
;
3277 hull
= isl_map_affine_hull(isl_map_copy(map
));
3278 sv
= isl_basic_map_plain_is_single_valued(hull
);
3280 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
3281 isl_basic_map_free(hull
);
3285 sv
= isl_map_is_single_valued(map
);
3289 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3290 "map is not single-valued", goto error
);
3291 map
= isl_map_make_disjoint(map
);
3295 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
3297 for (i
= 0; i
< map
->n
; ++i
) {
3298 isl_pw_multi_aff
*pma_i
;
3299 isl_basic_map
*bmap
;
3300 bmap
= isl_basic_map_copy(map
->p
[i
]);
3301 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
3302 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
3312 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
3314 return isl_pw_multi_aff_from_map(set
);
3317 /* Return the piecewise affine expression "set ? 1 : 0".
3319 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
3322 isl_space
*space
= isl_set_get_space(set
);
3323 isl_local_space
*ls
= isl_local_space_from_space(space
);
3324 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
3325 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
3327 one
= isl_aff_add_constant_si(one
, 1);
3328 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
3329 set
= isl_set_complement(set
);
3330 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
3335 /* Plug in "subs" for dimension "type", "pos" of "aff".
3337 * Let i be the dimension to replace and let "subs" be of the form
3341 * and "aff" of the form
3347 * (a f + d g')/(m d)
3349 * where g' is the result of plugging in "subs" in each of the integer
3352 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
3353 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
3358 aff
= isl_aff_cow(aff
);
3360 return isl_aff_free(aff
);
3362 ctx
= isl_aff_get_ctx(aff
);
3363 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
3364 isl_die(ctx
, isl_error_invalid
,
3365 "spaces don't match", return isl_aff_free(aff
));
3366 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
3367 isl_die(ctx
, isl_error_unsupported
,
3368 "cannot handle divs yet", return isl_aff_free(aff
));
3370 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
3372 return isl_aff_free(aff
);
3374 aff
->v
= isl_vec_cow(aff
->v
);
3376 return isl_aff_free(aff
);
3378 pos
+= isl_local_space_offset(aff
->ls
, type
);
3381 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
3382 aff
->v
->size
, subs
->v
->size
, v
);
3388 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3389 * expressions in "maff".
3391 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
3392 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
3393 __isl_keep isl_aff
*subs
)
3397 maff
= isl_multi_aff_cow(maff
);
3399 return isl_multi_aff_free(maff
);
3401 if (type
== isl_dim_in
)
3404 for (i
= 0; i
< maff
->n
; ++i
) {
3405 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
3407 return isl_multi_aff_free(maff
);
3413 /* Plug in "subs" for dimension "type", "pos" of "pma".
3415 * pma is of the form
3419 * while subs is of the form
3421 * v' = B_j(v) -> S_j
3423 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3424 * has a contribution in the result, in particular
3426 * C_ij(S_j) -> M_i(S_j)
3428 * Note that plugging in S_j in C_ij may also result in an empty set
3429 * and this contribution should simply be discarded.
3431 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
3432 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
3433 __isl_keep isl_pw_aff
*subs
)
3436 isl_pw_multi_aff
*res
;
3439 return isl_pw_multi_aff_free(pma
);
3441 n
= pma
->n
* subs
->n
;
3442 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
3444 for (i
= 0; i
< pma
->n
; ++i
) {
3445 for (j
= 0; j
< subs
->n
; ++j
) {
3447 isl_multi_aff
*res_ij
;
3448 common
= isl_set_intersect(
3449 isl_set_copy(pma
->p
[i
].set
),
3450 isl_set_copy(subs
->p
[j
].set
));
3451 common
= isl_set_substitute(common
,
3452 type
, pos
, subs
->p
[j
].aff
);
3453 if (isl_set_plain_is_empty(common
)) {
3454 isl_set_free(common
);
3458 res_ij
= isl_multi_aff_substitute(
3459 isl_multi_aff_copy(pma
->p
[i
].maff
),
3460 type
, pos
, subs
->p
[j
].aff
);
3462 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
3466 isl_pw_multi_aff_free(pma
);
3470 /* Extend the local space of "dst" to include the divs
3471 * in the local space of "src".
3473 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
3474 __isl_keep isl_aff
*src
)
3482 return isl_aff_free(dst
);
3484 ctx
= isl_aff_get_ctx(src
);
3485 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
3486 isl_die(ctx
, isl_error_invalid
,
3487 "spaces don't match", goto error
);
3489 if (src
->ls
->div
->n_row
== 0)
3492 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
3493 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
3497 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
3498 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
3506 return isl_aff_free(dst
);
3509 /* Adjust the local spaces of the affine expressions in "maff"
3510 * such that they all have the save divs.
3512 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
3513 __isl_take isl_multi_aff
*maff
)
3521 maff
= isl_multi_aff_cow(maff
);
3525 for (i
= 1; i
< maff
->n
; ++i
)
3526 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
3527 for (i
= 1; i
< maff
->n
; ++i
) {
3528 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
3530 return isl_multi_aff_free(maff
);
3536 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
3538 aff
= isl_aff_cow(aff
);
3542 aff
->ls
= isl_local_space_lift(aff
->ls
);
3544 return isl_aff_free(aff
);
3549 /* Lift "maff" to a space with extra dimensions such that the result
3550 * has no more existentially quantified variables.
3551 * If "ls" is not NULL, then *ls is assigned the local space that lies
3552 * at the basis of the lifting applied to "maff".
3554 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
3555 __isl_give isl_local_space
**ls
)
3569 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
3570 *ls
= isl_local_space_from_space(space
);
3572 return isl_multi_aff_free(maff
);
3577 maff
= isl_multi_aff_cow(maff
);
3578 maff
= isl_multi_aff_align_divs(maff
);
3582 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
3583 space
= isl_multi_aff_get_space(maff
);
3584 space
= isl_space_lift(isl_space_domain(space
), n_div
);
3585 space
= isl_space_extend_domain_with_range(space
,
3586 isl_multi_aff_get_space(maff
));
3588 return isl_multi_aff_free(maff
);
3589 isl_space_free(maff
->space
);
3590 maff
->space
= space
;
3593 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
3595 return isl_multi_aff_free(maff
);
3598 for (i
= 0; i
< maff
->n
; ++i
) {
3599 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
3607 isl_local_space_free(*ls
);
3608 return isl_multi_aff_free(maff
);
3612 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
3614 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
3615 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
3625 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
3626 if (pos
< 0 || pos
>= n_out
)
3627 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3628 "index out of bounds", return NULL
);
3630 space
= isl_pw_multi_aff_get_space(pma
);
3631 space
= isl_space_drop_dims(space
, isl_dim_out
,
3632 pos
+ 1, n_out
- pos
- 1);
3633 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
3635 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
3636 for (i
= 0; i
< pma
->n
; ++i
) {
3638 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
3639 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
3645 /* Return an isl_pw_multi_aff with the given "set" as domain and
3646 * an unnamed zero-dimensional range.
3648 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
3649 __isl_take isl_set
*set
)
3654 space
= isl_set_get_space(set
);
3655 space
= isl_space_from_domain(space
);
3656 ma
= isl_multi_aff_zero(space
);
3657 return isl_pw_multi_aff_alloc(set
, ma
);
3660 /* Add an isl_pw_multi_aff with the given "set" as domain and
3661 * an unnamed zero-dimensional range to *user.
3663 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
3665 isl_union_pw_multi_aff
**upma
= user
;
3666 isl_pw_multi_aff
*pma
;
3668 pma
= isl_pw_multi_aff_from_domain(set
);
3669 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
3674 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
3675 * an unnamed zero-dimensional range.
3677 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
3678 __isl_take isl_union_set
*uset
)
3681 isl_union_pw_multi_aff
*upma
;
3686 space
= isl_union_set_get_space(uset
);
3687 upma
= isl_union_pw_multi_aff_empty(space
);
3689 if (isl_union_set_foreach_set(uset
,
3690 &add_pw_multi_aff_from_domain
, &upma
) < 0)
3693 isl_union_set_free(uset
);
3696 isl_union_set_free(uset
);
3697 isl_union_pw_multi_aff_free(upma
);
3701 /* Convert "pma" to an isl_map and add it to *umap.
3703 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3705 isl_union_map
**umap
= user
;
3708 map
= isl_map_from_pw_multi_aff(pma
);
3709 *umap
= isl_union_map_add_map(*umap
, map
);
3714 /* Construct a union map mapping the domain of the union
3715 * piecewise multi-affine expression to its range, with each dimension
3716 * in the range equated to the corresponding affine expression on its cell.
3718 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
3719 __isl_take isl_union_pw_multi_aff
*upma
)
3722 isl_union_map
*umap
;
3727 space
= isl_union_pw_multi_aff_get_space(upma
);
3728 umap
= isl_union_map_empty(space
);
3730 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3731 &map_from_pw_multi_aff
, &umap
) < 0)
3734 isl_union_pw_multi_aff_free(upma
);
3737 isl_union_pw_multi_aff_free(upma
);
3738 isl_union_map_free(umap
);
3742 /* Local data for bin_entry and the callback "fn".
3744 struct isl_union_pw_multi_aff_bin_data
{
3745 isl_union_pw_multi_aff
*upma2
;
3746 isl_union_pw_multi_aff
*res
;
3747 isl_pw_multi_aff
*pma
;
3748 int (*fn
)(void **entry
, void *user
);
3751 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3752 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3754 static int bin_entry(void **entry
, void *user
)
3756 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3757 isl_pw_multi_aff
*pma
= *entry
;
3760 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
3761 data
->fn
, data
) < 0)
3767 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3768 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3769 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3770 * as *entry. The callback should adjust data->res if desired.
3772 static __isl_give isl_union_pw_multi_aff
*bin_op(
3773 __isl_take isl_union_pw_multi_aff
*upma1
,
3774 __isl_take isl_union_pw_multi_aff
*upma2
,
3775 int (*fn
)(void **entry
, void *user
))
3778 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
3780 space
= isl_union_pw_multi_aff_get_space(upma2
);
3781 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
3782 space
= isl_union_pw_multi_aff_get_space(upma1
);
3783 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
3785 if (!upma1
|| !upma2
)
3789 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
3791 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
3792 &bin_entry
, &data
) < 0)
3795 isl_union_pw_multi_aff_free(upma1
);
3796 isl_union_pw_multi_aff_free(upma2
);
3799 isl_union_pw_multi_aff_free(upma1
);
3800 isl_union_pw_multi_aff_free(upma2
);
3801 isl_union_pw_multi_aff_free(data
.res
);
3805 /* Given two isl_multi_affs A -> B and C -> D,
3806 * construct an isl_multi_aff (A * C) -> (B, D).
3808 __isl_give isl_multi_aff
*isl_multi_aff_flat_range_product(
3809 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3819 space
= isl_space_range_product(isl_multi_aff_get_space(ma1
),
3820 isl_multi_aff_get_space(ma2
));
3821 space
= isl_space_flatten_range(space
);
3822 res
= isl_multi_aff_alloc(space
);
3824 n1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
3825 n2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
3827 for (i
= 0; i
< n1
; ++i
) {
3828 aff
= isl_multi_aff_get_aff(ma1
, i
);
3829 res
= isl_multi_aff_set_aff(res
, i
, aff
);
3832 for (i
= 0; i
< n2
; ++i
) {
3833 aff
= isl_multi_aff_get_aff(ma2
, i
);
3834 res
= isl_multi_aff_set_aff(res
, n1
+ i
, aff
);
3837 isl_multi_aff_free(ma1
);
3838 isl_multi_aff_free(ma2
);
3841 isl_multi_aff_free(ma1
);
3842 isl_multi_aff_free(ma2
);
3846 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3847 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3849 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
3850 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3854 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
3855 isl_pw_multi_aff_get_space(pma2
));
3856 space
= isl_space_flatten_range(space
);
3857 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
3858 &isl_multi_aff_flat_range_product
);
3861 /* Given two isl_pw_multi_affs A -> B and C -> D,
3862 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3864 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
3865 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3867 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3868 &pw_multi_aff_flat_range_product
);
3871 /* If data->pma and *entry have the same domain space, then compute
3872 * their flat range product and the result to data->res.
3874 static int flat_range_product_entry(void **entry
, void *user
)
3876 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3877 isl_pw_multi_aff
*pma2
= *entry
;
3879 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
3880 pma2
->dim
, isl_dim_in
))
3883 pma2
= isl_pw_multi_aff_flat_range_product(
3884 isl_pw_multi_aff_copy(data
->pma
),
3885 isl_pw_multi_aff_copy(pma2
));
3887 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
3892 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3893 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3895 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
3896 __isl_take isl_union_pw_multi_aff
*upma1
,
3897 __isl_take isl_union_pw_multi_aff
*upma2
)
3899 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
3902 /* Replace the affine expressions at position "pos" in "pma" by "pa".
3903 * The parameters are assumed to have been aligned.
3905 * The implementation essentially performs an isl_pw_*_on_shared_domain,
3906 * except that it works on two different isl_pw_* types.
3908 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
3909 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
3910 __isl_take isl_pw_aff
*pa
)
3913 isl_pw_multi_aff
*res
= NULL
;
3918 if (!isl_space_tuple_match(pma
->dim
, isl_dim_in
, pa
->dim
, isl_dim_in
))
3919 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3920 "domains don't match", goto error
);
3921 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
3922 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3923 "index out of bounds", goto error
);
3926 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
3928 for (i
= 0; i
< pma
->n
; ++i
) {
3929 for (j
= 0; j
< pa
->n
; ++j
) {
3931 isl_multi_aff
*res_ij
;
3934 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
3935 isl_set_copy(pa
->p
[j
].set
));
3936 empty
= isl_set_plain_is_empty(common
);
3937 if (empty
< 0 || empty
) {
3938 isl_set_free(common
);
3944 res_ij
= isl_multi_aff_set_aff(
3945 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
3946 isl_aff_copy(pa
->p
[j
].aff
));
3947 res_ij
= isl_multi_aff_gist(res_ij
,
3948 isl_set_copy(common
));
3950 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
3954 isl_pw_multi_aff_free(pma
);
3955 isl_pw_aff_free(pa
);
3958 isl_pw_multi_aff_free(pma
);
3959 isl_pw_aff_free(pa
);
3960 return isl_pw_multi_aff_free(res
);
3963 /* Replace the affine expressions at position "pos" in "pma" by "pa".
3965 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
3966 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
3967 __isl_take isl_pw_aff
*pa
)
3971 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
3972 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
3973 if (!isl_space_has_named_params(pma
->dim
) ||
3974 !isl_space_has_named_params(pa
->dim
))
3975 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3976 "unaligned unnamed parameters", goto error
);
3977 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
3978 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
3979 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
3981 isl_pw_multi_aff_free(pma
);
3982 isl_pw_aff_free(pa
);