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 /* Return a piecewise affine expression that is equal to
132 * the specified dimension in "ls".
134 __isl_give isl_pw_aff
*isl_pw_aff_var_on_domain(__isl_take isl_local_space
*ls
,
135 enum isl_dim_type type
, unsigned pos
)
137 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls
, type
, pos
));
140 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
149 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
154 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
155 isl_vec_copy(aff
->v
));
158 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
166 return isl_aff_dup(aff
);
169 void *isl_aff_free(__isl_take isl_aff
*aff
)
177 isl_local_space_free(aff
->ls
);
178 isl_vec_free(aff
->v
);
185 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
187 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
190 /* Externally, an isl_aff has a map space, but internally, the
191 * ls field corresponds to the domain of that space.
193 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
197 if (type
== isl_dim_out
)
199 if (type
== isl_dim_in
)
201 return isl_local_space_dim(aff
->ls
, type
);
204 __isl_give isl_space
*isl_aff_get_domain_space(__isl_keep isl_aff
*aff
)
206 return aff
? isl_local_space_get_space(aff
->ls
) : NULL
;
209 __isl_give isl_space
*isl_aff_get_space(__isl_keep isl_aff
*aff
)
214 space
= isl_local_space_get_space(aff
->ls
);
215 space
= isl_space_from_domain(space
);
216 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
220 __isl_give isl_local_space
*isl_aff_get_domain_local_space(
221 __isl_keep isl_aff
*aff
)
223 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
226 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
231 ls
= isl_local_space_copy(aff
->ls
);
232 ls
= isl_local_space_from_domain(ls
);
233 ls
= isl_local_space_add_dims(ls
, isl_dim_out
, 1);
237 /* Externally, an isl_aff has a map space, but internally, the
238 * ls field corresponds to the domain of that space.
240 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
241 enum isl_dim_type type
, unsigned pos
)
245 if (type
== isl_dim_out
)
247 if (type
== isl_dim_in
)
249 return isl_local_space_get_dim_name(aff
->ls
, type
, pos
);
252 __isl_give isl_aff
*isl_aff_reset_domain_space(__isl_take isl_aff
*aff
,
253 __isl_take isl_space
*dim
)
255 aff
= isl_aff_cow(aff
);
259 aff
->ls
= isl_local_space_reset_space(aff
->ls
, dim
);
261 return isl_aff_free(aff
);
270 /* Reset the space of "aff". This function is called from isl_pw_templ.c
271 * and doesn't know if the space of an element object is represented
272 * directly or through its domain. It therefore passes along both.
274 __isl_give isl_aff
*isl_aff_reset_space_and_domain(__isl_take isl_aff
*aff
,
275 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
277 isl_space_free(space
);
278 return isl_aff_reset_domain_space(aff
, domain
);
281 /* Reorder the coefficients of the affine expression based
282 * on the given reodering.
283 * The reordering r is assumed to have been extended with the local
286 static __isl_give isl_vec
*vec_reorder(__isl_take isl_vec
*vec
,
287 __isl_take isl_reordering
*r
, int n_div
)
295 res
= isl_vec_alloc(vec
->ctx
,
296 2 + isl_space_dim(r
->dim
, isl_dim_all
) + n_div
);
297 isl_seq_cpy(res
->el
, vec
->el
, 2);
298 isl_seq_clr(res
->el
+ 2, res
->size
- 2);
299 for (i
= 0; i
< r
->len
; ++i
)
300 isl_int_set(res
->el
[2 + r
->pos
[i
]], vec
->el
[2 + i
]);
302 isl_reordering_free(r
);
307 isl_reordering_free(r
);
311 /* Reorder the dimensions of the domain of "aff" according
312 * to the given reordering.
314 __isl_give isl_aff
*isl_aff_realign_domain(__isl_take isl_aff
*aff
,
315 __isl_take isl_reordering
*r
)
317 aff
= isl_aff_cow(aff
);
321 r
= isl_reordering_extend(r
, aff
->ls
->div
->n_row
);
322 aff
->v
= vec_reorder(aff
->v
, isl_reordering_copy(r
),
323 aff
->ls
->div
->n_row
);
324 aff
->ls
= isl_local_space_realign(aff
->ls
, r
);
326 if (!aff
->v
|| !aff
->ls
)
327 return isl_aff_free(aff
);
332 isl_reordering_free(r
);
336 __isl_give isl_aff
*isl_aff_align_params(__isl_take isl_aff
*aff
,
337 __isl_take isl_space
*model
)
342 if (!isl_space_match(aff
->ls
->dim
, isl_dim_param
,
343 model
, isl_dim_param
)) {
346 model
= isl_space_drop_dims(model
, isl_dim_in
,
347 0, isl_space_dim(model
, isl_dim_in
));
348 model
= isl_space_drop_dims(model
, isl_dim_out
,
349 0, isl_space_dim(model
, isl_dim_out
));
350 exp
= isl_parameter_alignment_reordering(aff
->ls
->dim
, model
);
351 exp
= isl_reordering_extend_space(exp
,
352 isl_aff_get_domain_space(aff
));
353 aff
= isl_aff_realign_domain(aff
, exp
);
356 isl_space_free(model
);
359 isl_space_free(model
);
364 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
369 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
372 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
379 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
380 if (equal
< 0 || !equal
)
383 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
386 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
390 isl_int_set(*v
, aff
->v
->el
[0]);
394 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
398 isl_int_set(*v
, aff
->v
->el
[1]);
402 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
403 enum isl_dim_type type
, int pos
, isl_int
*v
)
408 if (type
== isl_dim_out
)
409 isl_die(aff
->v
->ctx
, isl_error_invalid
,
410 "output/set dimension does not have a coefficient",
412 if (type
== isl_dim_in
)
415 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
416 isl_die(aff
->v
->ctx
, isl_error_invalid
,
417 "position out of bounds", return -1);
419 pos
+= isl_local_space_offset(aff
->ls
, type
);
420 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
425 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
427 aff
= isl_aff_cow(aff
);
431 aff
->v
= isl_vec_cow(aff
->v
);
433 return isl_aff_free(aff
);
435 isl_int_set(aff
->v
->el
[0], v
);
440 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
442 aff
= isl_aff_cow(aff
);
446 aff
->v
= isl_vec_cow(aff
->v
);
448 return isl_aff_free(aff
);
450 isl_int_set(aff
->v
->el
[1], v
);
455 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
457 if (isl_int_is_zero(v
))
460 aff
= isl_aff_cow(aff
);
464 aff
->v
= isl_vec_cow(aff
->v
);
466 return isl_aff_free(aff
);
468 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
473 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
478 isl_int_set_si(t
, v
);
479 aff
= isl_aff_add_constant(aff
, t
);
485 /* Add "v" to the numerator of the constant term of "aff".
487 __isl_give isl_aff
*isl_aff_add_constant_num(__isl_take isl_aff
*aff
, isl_int v
)
489 if (isl_int_is_zero(v
))
492 aff
= isl_aff_cow(aff
);
496 aff
->v
= isl_vec_cow(aff
->v
);
498 return isl_aff_free(aff
);
500 isl_int_add(aff
->v
->el
[1], aff
->v
->el
[1], v
);
505 /* Add "v" to the numerator of the constant term of "aff".
507 __isl_give isl_aff
*isl_aff_add_constant_num_si(__isl_take isl_aff
*aff
, int v
)
515 isl_int_set_si(t
, v
);
516 aff
= isl_aff_add_constant_num(aff
, t
);
522 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
524 aff
= isl_aff_cow(aff
);
528 aff
->v
= isl_vec_cow(aff
->v
);
530 return isl_aff_free(aff
);
532 isl_int_set_si(aff
->v
->el
[1], v
);
537 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
538 enum isl_dim_type type
, int pos
, isl_int v
)
543 if (type
== isl_dim_out
)
544 isl_die(aff
->v
->ctx
, isl_error_invalid
,
545 "output/set dimension does not have a coefficient",
546 return isl_aff_free(aff
));
547 if (type
== isl_dim_in
)
550 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
551 isl_die(aff
->v
->ctx
, isl_error_invalid
,
552 "position out of bounds", return isl_aff_free(aff
));
554 aff
= isl_aff_cow(aff
);
558 aff
->v
= isl_vec_cow(aff
->v
);
560 return isl_aff_free(aff
);
562 pos
+= isl_local_space_offset(aff
->ls
, type
);
563 isl_int_set(aff
->v
->el
[1 + pos
], v
);
568 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
569 enum isl_dim_type type
, int pos
, int v
)
574 if (type
== isl_dim_out
)
575 isl_die(aff
->v
->ctx
, isl_error_invalid
,
576 "output/set dimension does not have a coefficient",
577 return isl_aff_free(aff
));
578 if (type
== isl_dim_in
)
581 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
582 isl_die(aff
->v
->ctx
, isl_error_invalid
,
583 "position out of bounds", return isl_aff_free(aff
));
585 aff
= isl_aff_cow(aff
);
589 aff
->v
= isl_vec_cow(aff
->v
);
591 return isl_aff_free(aff
);
593 pos
+= isl_local_space_offset(aff
->ls
, type
);
594 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
599 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
600 enum isl_dim_type type
, int pos
, isl_int v
)
605 if (type
== isl_dim_out
)
606 isl_die(aff
->v
->ctx
, isl_error_invalid
,
607 "output/set dimension does not have a coefficient",
608 return isl_aff_free(aff
));
609 if (type
== isl_dim_in
)
612 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
613 isl_die(aff
->v
->ctx
, isl_error_invalid
,
614 "position out of bounds", return isl_aff_free(aff
));
616 aff
= isl_aff_cow(aff
);
620 aff
->v
= isl_vec_cow(aff
->v
);
622 return isl_aff_free(aff
);
624 pos
+= isl_local_space_offset(aff
->ls
, type
);
625 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
630 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
631 enum isl_dim_type type
, int pos
, int v
)
636 isl_int_set_si(t
, v
);
637 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
643 __isl_give isl_aff
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
648 return isl_local_space_get_div(aff
->ls
, pos
);
651 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
653 aff
= isl_aff_cow(aff
);
656 aff
->v
= isl_vec_cow(aff
->v
);
658 return isl_aff_free(aff
);
660 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
665 /* Remove divs from the local space that do not appear in the affine
667 * We currently only remove divs at the end.
668 * Some intermediate divs may also not appear directly in the affine
669 * expression, but we would also need to check that no other divs are
670 * defined in terms of them.
672 __isl_give isl_aff
*isl_aff_remove_unused_divs( __isl_take isl_aff
*aff
)
681 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
682 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
684 pos
= isl_seq_last_non_zero(aff
->v
->el
+ 1 + off
, n
) + 1;
688 aff
= isl_aff_cow(aff
);
692 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, isl_dim_div
, pos
, n
- pos
);
693 aff
->v
= isl_vec_drop_els(aff
->v
, 1 + off
+ pos
, n
- pos
);
694 if (!aff
->ls
|| !aff
->v
)
695 return isl_aff_free(aff
);
700 /* Given two affine expressions "p" of length p_len (including the
701 * denominator and the constant term) and "subs" of length subs_len,
702 * plug in "subs" for the variable at position "pos".
703 * The variables of "subs" and "p" are assumed to match up to subs_len,
704 * but "p" may have additional variables.
705 * "v" is an initialized isl_int that can be used internally.
707 * In particular, if "p" represents the expression
711 * with i the variable at position "pos" and "subs" represents the expression
715 * then the result represents the expression
720 void isl_seq_substitute(isl_int
*p
, int pos
, isl_int
*subs
,
721 int p_len
, int subs_len
, isl_int v
)
723 isl_int_set(v
, p
[1 + pos
]);
724 isl_int_set_si(p
[1 + pos
], 0);
725 isl_seq_combine(p
+ 1, subs
[0], p
+ 1, v
, subs
+ 1, subs_len
- 1);
726 isl_seq_scale(p
+ subs_len
, p
+ subs_len
, subs
[0], p_len
- subs_len
);
727 isl_int_mul(p
[0], p
[0], subs
[0]);
730 /* Look for any divs in the aff->ls with a denominator equal to one
731 * and plug them into the affine expression and any subsequent divs
732 * that may reference the div.
734 static __isl_give isl_aff
*plug_in_integral_divs(__isl_take isl_aff
*aff
)
746 n
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
748 for (i
= 0; i
< n
; ++i
) {
749 if (!isl_int_is_one(aff
->ls
->div
->row
[i
][0]))
751 ls
= isl_local_space_copy(aff
->ls
);
752 ls
= isl_local_space_substitute_seq(ls
, isl_dim_div
, i
,
753 aff
->ls
->div
->row
[i
], len
, i
+ 1);
754 vec
= isl_vec_copy(aff
->v
);
755 vec
= isl_vec_cow(vec
);
761 pos
= isl_local_space_offset(aff
->ls
, isl_dim_div
) + i
;
762 isl_seq_substitute(vec
->el
, pos
, aff
->ls
->div
->row
[i
],
767 isl_vec_free(aff
->v
);
769 isl_local_space_free(aff
->ls
);
776 isl_local_space_free(ls
);
777 return isl_aff_free(aff
);
780 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
782 * Even though this function is only called on isl_affs with a single
783 * reference, we are careful to only change aff->v and aff->ls together.
785 static __isl_give isl_aff
*swap_div(__isl_take isl_aff
*aff
, int a
, int b
)
787 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
791 ls
= isl_local_space_copy(aff
->ls
);
792 ls
= isl_local_space_swap_div(ls
, a
, b
);
793 v
= isl_vec_copy(aff
->v
);
798 isl_int_swap(v
->el
[1 + off
+ a
], v
->el
[1 + off
+ b
]);
799 isl_vec_free(aff
->v
);
801 isl_local_space_free(aff
->ls
);
807 isl_local_space_free(ls
);
808 return isl_aff_free(aff
);
811 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
813 * We currently do not actually remove div "b", but simply add its
814 * coefficient to that of "a" and then zero it out.
816 static __isl_give isl_aff
*merge_divs(__isl_take isl_aff
*aff
, int a
, int b
)
818 unsigned off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
820 if (isl_int_is_zero(aff
->v
->el
[1 + off
+ b
]))
823 aff
->v
= isl_vec_cow(aff
->v
);
825 return isl_aff_free(aff
);
827 isl_int_add(aff
->v
->el
[1 + off
+ a
],
828 aff
->v
->el
[1 + off
+ a
], aff
->v
->el
[1 + off
+ b
]);
829 isl_int_set_si(aff
->v
->el
[1 + off
+ b
], 0);
834 /* Sort the divs in the local space of "aff" according to
835 * the comparison function "cmp_row" in isl_local_space.c,
836 * combining the coefficients of identical divs.
838 * Reordering divs does not change the semantics of "aff",
839 * so there is no need to call isl_aff_cow.
840 * Moreover, this function is currently only called on isl_affs
841 * with a single reference.
843 static __isl_give isl_aff
*sort_divs(__isl_take isl_aff
*aff
)
851 off
= isl_local_space_offset(aff
->ls
, isl_dim_div
);
852 n
= isl_aff_dim(aff
, isl_dim_div
);
853 for (i
= 1; i
< n
; ++i
) {
854 for (j
= i
- 1; j
>= 0; --j
) {
855 int cmp
= isl_mat_cmp_div(aff
->ls
->div
, j
, j
+ 1);
859 aff
= merge_divs(aff
, j
, j
+ 1);
861 aff
= swap_div(aff
, j
, j
+ 1);
870 /* Normalize the representation of "aff".
872 * This function should only be called of "new" isl_affs, i.e.,
873 * with only a single reference. We therefore do not need to
874 * worry about affecting other instances.
876 __isl_give isl_aff
*isl_aff_normalize(__isl_take isl_aff
*aff
)
880 aff
->v
= isl_vec_normalize(aff
->v
);
882 return isl_aff_free(aff
);
883 aff
= plug_in_integral_divs(aff
);
884 aff
= sort_divs(aff
);
885 aff
= isl_aff_remove_unused_divs(aff
);
889 /* Given f, return floor(f).
890 * If f is an integer expression, then just return f.
891 * If f is a constant, then return the constant floor(f).
892 * Otherwise, if f = g/m, write g = q m + r,
893 * create a new div d = [r/m] and return the expression q + d.
894 * The coefficients in r are taken to lie between -m/2 and m/2.
896 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
906 if (isl_int_is_one(aff
->v
->el
[0]))
909 aff
= isl_aff_cow(aff
);
913 aff
->v
= isl_vec_cow(aff
->v
);
915 return isl_aff_free(aff
);
917 if (isl_aff_is_cst(aff
)) {
918 isl_int_fdiv_q(aff
->v
->el
[1], aff
->v
->el
[1], aff
->v
->el
[0]);
919 isl_int_set_si(aff
->v
->el
[0], 1);
923 div
= isl_vec_copy(aff
->v
);
924 div
= isl_vec_cow(div
);
926 return isl_aff_free(aff
);
928 ctx
= isl_aff_get_ctx(aff
);
929 isl_int_fdiv_q(aff
->v
->el
[0], aff
->v
->el
[0], ctx
->two
);
930 for (i
= 1; i
< aff
->v
->size
; ++i
) {
931 isl_int_fdiv_r(div
->el
[i
], div
->el
[i
], div
->el
[0]);
932 isl_int_fdiv_q(aff
->v
->el
[i
], aff
->v
->el
[i
], div
->el
[0]);
933 if (isl_int_gt(div
->el
[i
], aff
->v
->el
[0])) {
934 isl_int_sub(div
->el
[i
], div
->el
[i
], div
->el
[0]);
935 isl_int_add_ui(aff
->v
->el
[i
], aff
->v
->el
[i
], 1);
939 aff
->ls
= isl_local_space_add_div(aff
->ls
, div
);
941 return isl_aff_free(aff
);
944 aff
->v
= isl_vec_extend(aff
->v
, size
+ 1);
946 return isl_aff_free(aff
);
947 isl_int_set_si(aff
->v
->el
[0], 1);
948 isl_int_set_si(aff
->v
->el
[size
], 1);
955 * aff mod m = aff - m * floor(aff/m)
957 __isl_give isl_aff
*isl_aff_mod(__isl_take isl_aff
*aff
, isl_int m
)
961 res
= isl_aff_copy(aff
);
962 aff
= isl_aff_scale_down(aff
, m
);
963 aff
= isl_aff_floor(aff
);
964 aff
= isl_aff_scale(aff
, m
);
965 res
= isl_aff_sub(res
, aff
);
972 * pwaff mod m = pwaff - m * floor(pwaff/m)
974 __isl_give isl_pw_aff
*isl_pw_aff_mod(__isl_take isl_pw_aff
*pwaff
, isl_int m
)
978 res
= isl_pw_aff_copy(pwaff
);
979 pwaff
= isl_pw_aff_scale_down(pwaff
, m
);
980 pwaff
= isl_pw_aff_floor(pwaff
);
981 pwaff
= isl_pw_aff_scale(pwaff
, m
);
982 res
= isl_pw_aff_sub(res
, pwaff
);
987 /* Given f, return ceil(f).
988 * If f is an integer expression, then just return f.
989 * Otherwise, create a new div d = [-f] and return the expression -d.
991 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
996 if (isl_int_is_one(aff
->v
->el
[0]))
999 aff
= isl_aff_neg(aff
);
1000 aff
= isl_aff_floor(aff
);
1001 aff
= isl_aff_neg(aff
);
1006 /* Apply the expansion computed by isl_merge_divs.
1007 * The expansion itself is given by "exp" while the resulting
1008 * list of divs is given by "div".
1010 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
1011 __isl_take isl_mat
*div
, int *exp
)
1018 aff
= isl_aff_cow(aff
);
1022 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1023 new_n_div
= isl_mat_rows(div
);
1024 if (new_n_div
< old_n_div
)
1025 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
1026 "not an expansion", goto error
);
1028 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
1032 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
1034 for (i
= new_n_div
- 1; i
>= 0; --i
) {
1035 if (j
>= 0 && exp
[j
] == i
) {
1037 isl_int_swap(aff
->v
->el
[offset
+ i
],
1038 aff
->v
->el
[offset
+ j
]);
1041 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
1044 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
1055 /* Add two affine expressions that live in the same local space.
1057 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
1058 __isl_take isl_aff
*aff2
)
1062 aff1
= isl_aff_cow(aff1
);
1066 aff1
->v
= isl_vec_cow(aff1
->v
);
1072 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
1073 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1074 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
1075 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
1076 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
1077 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
1078 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
1090 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
1091 __isl_take isl_aff
*aff2
)
1101 ctx
= isl_aff_get_ctx(aff1
);
1102 if (!isl_space_is_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
1103 isl_die(ctx
, isl_error_invalid
,
1104 "spaces don't match", goto error
);
1106 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
1107 return add_expanded(aff1
, aff2
);
1109 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
1110 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
1114 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
1115 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
1116 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
1120 return add_expanded(aff1
, aff2
);
1129 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
1130 __isl_take isl_aff
*aff2
)
1132 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
1135 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
1139 if (isl_int_is_one(f
))
1142 aff
= isl_aff_cow(aff
);
1145 aff
->v
= isl_vec_cow(aff
->v
);
1147 return isl_aff_free(aff
);
1150 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
1151 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1152 isl_int_divexact(gcd
, f
, gcd
);
1153 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1159 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
1163 if (isl_int_is_one(f
))
1166 aff
= isl_aff_cow(aff
);
1170 if (isl_int_is_zero(f
))
1171 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1172 "cannot scale down by zero", return isl_aff_free(aff
));
1174 aff
->v
= isl_vec_cow(aff
->v
);
1176 return isl_aff_free(aff
);
1179 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
1180 isl_int_gcd(gcd
, gcd
, f
);
1181 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
1182 isl_int_divexact(gcd
, f
, gcd
);
1183 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
1189 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
1197 isl_int_set_ui(v
, f
);
1198 aff
= isl_aff_scale_down(aff
, v
);
1204 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
1205 enum isl_dim_type type
, unsigned pos
, const char *s
)
1207 aff
= isl_aff_cow(aff
);
1210 if (type
== isl_dim_out
)
1211 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1212 "cannot set name of output/set dimension",
1213 return isl_aff_free(aff
));
1214 if (type
== isl_dim_in
)
1216 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
1218 return isl_aff_free(aff
);
1223 __isl_give isl_aff
*isl_aff_set_dim_id(__isl_take isl_aff
*aff
,
1224 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
1226 aff
= isl_aff_cow(aff
);
1228 return isl_id_free(id
);
1229 if (type
== isl_dim_out
)
1230 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1231 "cannot set name of output/set dimension",
1233 if (type
== isl_dim_in
)
1235 aff
->ls
= isl_local_space_set_dim_id(aff
->ls
, type
, pos
, id
);
1237 return isl_aff_free(aff
);
1246 /* Exploit the equalities in "eq" to simplify the affine expression
1247 * and the expressions of the integer divisions in the local space.
1248 * The integer divisions in this local space are assumed to appear
1249 * as regular dimensions in "eq".
1251 static __isl_give isl_aff
*isl_aff_substitute_equalities_lifted(
1252 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1260 if (eq
->n_eq
== 0) {
1261 isl_basic_set_free(eq
);
1265 aff
= isl_aff_cow(aff
);
1269 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
1270 isl_basic_set_copy(eq
));
1274 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
1276 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1277 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
1278 if (j
< 0 || j
== 0 || j
>= total
)
1281 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
1285 isl_basic_set_free(eq
);
1286 aff
= isl_aff_normalize(aff
);
1289 isl_basic_set_free(eq
);
1294 /* Exploit the equalities in "eq" to simplify the affine expression
1295 * and the expressions of the integer divisions in the local space.
1297 static __isl_give isl_aff
*isl_aff_substitute_equalities(
1298 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
1304 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1306 eq
= isl_basic_set_add(eq
, isl_dim_set
, n_div
);
1307 return isl_aff_substitute_equalities_lifted(aff
, eq
);
1309 isl_basic_set_free(eq
);
1314 /* Look for equalities among the variables shared by context and aff
1315 * and the integer divisions of aff, if any.
1316 * The equalities are then used to eliminate coefficients and/or integer
1317 * divisions from aff.
1319 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
1320 __isl_take isl_set
*context
)
1322 isl_basic_set
*hull
;
1327 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
1329 isl_basic_set
*bset
;
1330 isl_local_space
*ls
;
1331 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
1332 ls
= isl_aff_get_domain_local_space(aff
);
1333 bset
= isl_basic_set_from_local_space(ls
);
1334 bset
= isl_basic_set_lift(bset
);
1335 bset
= isl_basic_set_flatten(bset
);
1336 context
= isl_set_intersect(context
,
1337 isl_set_from_basic_set(bset
));
1340 hull
= isl_set_affine_hull(context
);
1341 return isl_aff_substitute_equalities_lifted(aff
, hull
);
1344 isl_set_free(context
);
1348 __isl_give isl_aff
*isl_aff_gist_params(__isl_take isl_aff
*aff
,
1349 __isl_take isl_set
*context
)
1351 isl_set
*dom_context
= isl_set_universe(isl_aff_get_domain_space(aff
));
1352 dom_context
= isl_set_intersect_params(dom_context
, context
);
1353 return isl_aff_gist(aff
, dom_context
);
1356 /* Return a basic set containing those elements in the space
1357 * of aff where it is non-negative.
1359 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
1361 isl_constraint
*ineq
;
1362 isl_basic_set
*bset
;
1364 ineq
= isl_inequality_from_aff(aff
);
1366 bset
= isl_basic_set_from_constraint(ineq
);
1367 bset
= isl_basic_set_simplify(bset
);
1371 /* Return a basic set containing those elements in the domain space
1372 * of aff where it is negative.
1374 __isl_give isl_basic_set
*isl_aff_neg_basic_set(__isl_take isl_aff
*aff
)
1376 aff
= isl_aff_neg(aff
);
1377 aff
= isl_aff_add_constant_num_si(aff
, -1);
1378 return isl_aff_nonneg_basic_set(aff
);
1381 /* Return a basic set containing those elements in the space
1382 * of aff where it is zero.
1384 __isl_give isl_basic_set
*isl_aff_zero_basic_set(__isl_take isl_aff
*aff
)
1386 isl_constraint
*ineq
;
1387 isl_basic_set
*bset
;
1389 ineq
= isl_equality_from_aff(aff
);
1391 bset
= isl_basic_set_from_constraint(ineq
);
1392 bset
= isl_basic_set_simplify(bset
);
1396 /* Return a basic set containing those elements in the shared space
1397 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1399 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
1400 __isl_take isl_aff
*aff2
)
1402 aff1
= isl_aff_sub(aff1
, aff2
);
1404 return isl_aff_nonneg_basic_set(aff1
);
1407 /* Return a basic set containing those elements in the shared space
1408 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1410 __isl_give isl_basic_set
*isl_aff_le_basic_set(__isl_take isl_aff
*aff1
,
1411 __isl_take isl_aff
*aff2
)
1413 return isl_aff_ge_basic_set(aff2
, aff1
);
1416 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
1417 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
1419 aff1
= isl_aff_add(aff1
, aff2
);
1420 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
1424 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
1432 /* Check whether the given affine expression has non-zero coefficient
1433 * for any dimension in the given range or if any of these dimensions
1434 * appear with non-zero coefficients in any of the integer divisions
1435 * involved in the affine expression.
1437 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
1438 enum isl_dim_type type
, unsigned first
, unsigned n
)
1450 ctx
= isl_aff_get_ctx(aff
);
1451 if (first
+ n
> isl_aff_dim(aff
, type
))
1452 isl_die(ctx
, isl_error_invalid
,
1453 "range out of bounds", return -1);
1455 active
= isl_local_space_get_active(aff
->ls
, aff
->v
->el
+ 2);
1459 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
1460 for (i
= 0; i
< n
; ++i
)
1461 if (active
[first
+ i
]) {
1474 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
1475 enum isl_dim_type type
, unsigned first
, unsigned n
)
1481 if (type
== isl_dim_out
)
1482 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1483 "cannot drop output/set dimension",
1484 return isl_aff_free(aff
));
1485 if (type
== isl_dim_in
)
1487 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1490 ctx
= isl_aff_get_ctx(aff
);
1491 if (first
+ n
> isl_local_space_dim(aff
->ls
, type
))
1492 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
1493 return isl_aff_free(aff
));
1495 aff
= isl_aff_cow(aff
);
1499 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
1501 return isl_aff_free(aff
);
1503 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1504 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
1506 return isl_aff_free(aff
);
1511 /* Project the domain of the affine expression onto its parameter space.
1512 * The affine expression may not involve any of the domain dimensions.
1514 __isl_give isl_aff
*isl_aff_project_domain_on_params(__isl_take isl_aff
*aff
)
1520 n
= isl_aff_dim(aff
, isl_dim_in
);
1521 involves
= isl_aff_involves_dims(aff
, isl_dim_in
, 0, n
);
1523 return isl_aff_free(aff
);
1525 isl_die(isl_aff_get_ctx(aff
), isl_error_invalid
,
1526 "affine expression involves some of the domain dimensions",
1527 return isl_aff_free(aff
));
1528 aff
= isl_aff_drop_dims(aff
, isl_dim_in
, 0, n
);
1529 space
= isl_aff_get_domain_space(aff
);
1530 space
= isl_space_params(space
);
1531 aff
= isl_aff_reset_domain_space(aff
, space
);
1535 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
1536 enum isl_dim_type type
, unsigned first
, unsigned n
)
1542 if (type
== isl_dim_out
)
1543 isl_die(aff
->v
->ctx
, isl_error_invalid
,
1544 "cannot insert output/set dimensions",
1545 return isl_aff_free(aff
));
1546 if (type
== isl_dim_in
)
1548 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
1551 ctx
= isl_aff_get_ctx(aff
);
1552 if (first
> isl_local_space_dim(aff
->ls
, type
))
1553 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
1554 return isl_aff_free(aff
));
1556 aff
= isl_aff_cow(aff
);
1560 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
1562 return isl_aff_free(aff
);
1564 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
1565 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
1567 return isl_aff_free(aff
);
1572 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
1573 enum isl_dim_type type
, unsigned n
)
1577 pos
= isl_aff_dim(aff
, type
);
1579 return isl_aff_insert_dims(aff
, type
, pos
, n
);
1582 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
1583 enum isl_dim_type type
, unsigned n
)
1587 pos
= isl_pw_aff_dim(pwaff
, type
);
1589 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
1592 __isl_give isl_pw_aff
*isl_pw_aff_from_aff(__isl_take isl_aff
*aff
)
1594 isl_set
*dom
= isl_set_universe(isl_aff_get_domain_space(aff
));
1595 return isl_pw_aff_alloc(dom
, aff
);
1599 #define PW isl_pw_aff
1603 #define EL_IS_ZERO is_empty
1607 #define IS_ZERO is_empty
1610 #undef DEFAULT_IS_ZERO
1611 #define DEFAULT_IS_ZERO 0
1615 #define NO_MOVE_DIMS
1619 #include <isl_pw_templ.c>
1621 static __isl_give isl_set
*align_params_pw_pw_set_and(
1622 __isl_take isl_pw_aff
*pwaff1
, __isl_take isl_pw_aff
*pwaff2
,
1623 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
1624 __isl_take isl_pw_aff
*pwaff2
))
1626 if (!pwaff1
|| !pwaff2
)
1628 if (isl_space_match(pwaff1
->dim
, isl_dim_param
,
1629 pwaff2
->dim
, isl_dim_param
))
1630 return fn(pwaff1
, pwaff2
);
1631 if (!isl_space_has_named_params(pwaff1
->dim
) ||
1632 !isl_space_has_named_params(pwaff2
->dim
))
1633 isl_die(isl_pw_aff_get_ctx(pwaff1
), isl_error_invalid
,
1634 "unaligned unnamed parameters", goto error
);
1635 pwaff1
= isl_pw_aff_align_params(pwaff1
, isl_pw_aff_get_space(pwaff2
));
1636 pwaff2
= isl_pw_aff_align_params(pwaff2
, isl_pw_aff_get_space(pwaff1
));
1637 return fn(pwaff1
, pwaff2
);
1639 isl_pw_aff_free(pwaff1
);
1640 isl_pw_aff_free(pwaff2
);
1644 /* Compute a piecewise quasi-affine expression with a domain that
1645 * is the union of those of pwaff1 and pwaff2 and such that on each
1646 * cell, the quasi-affine expression is the better (according to cmp)
1647 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1648 * is defined on a given cell, then the associated expression
1649 * is the defined one.
1651 static __isl_give isl_pw_aff
*pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1652 __isl_take isl_pw_aff
*pwaff2
,
1653 __isl_give isl_basic_set
*(*cmp
)(__isl_take isl_aff
*aff1
,
1654 __isl_take isl_aff
*aff2
))
1661 if (!pwaff1
|| !pwaff2
)
1664 ctx
= isl_space_get_ctx(pwaff1
->dim
);
1665 if (!isl_space_is_equal(pwaff1
->dim
, pwaff2
->dim
))
1666 isl_die(ctx
, isl_error_invalid
,
1667 "arguments should live in same space", goto error
);
1669 if (isl_pw_aff_is_empty(pwaff1
)) {
1670 isl_pw_aff_free(pwaff1
);
1674 if (isl_pw_aff_is_empty(pwaff2
)) {
1675 isl_pw_aff_free(pwaff2
);
1679 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
1680 res
= isl_pw_aff_alloc_size(isl_space_copy(pwaff1
->dim
), n
);
1682 for (i
= 0; i
< pwaff1
->n
; ++i
) {
1683 set
= isl_set_copy(pwaff1
->p
[i
].set
);
1684 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1685 struct isl_set
*common
;
1688 common
= isl_set_intersect(
1689 isl_set_copy(pwaff1
->p
[i
].set
),
1690 isl_set_copy(pwaff2
->p
[j
].set
));
1691 better
= isl_set_from_basic_set(cmp(
1692 isl_aff_copy(pwaff2
->p
[j
].aff
),
1693 isl_aff_copy(pwaff1
->p
[i
].aff
)));
1694 better
= isl_set_intersect(common
, better
);
1695 if (isl_set_plain_is_empty(better
)) {
1696 isl_set_free(better
);
1699 set
= isl_set_subtract(set
, isl_set_copy(better
));
1701 res
= isl_pw_aff_add_piece(res
, better
,
1702 isl_aff_copy(pwaff2
->p
[j
].aff
));
1704 res
= isl_pw_aff_add_piece(res
, set
,
1705 isl_aff_copy(pwaff1
->p
[i
].aff
));
1708 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1709 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1710 for (i
= 0; i
< pwaff1
->n
; ++i
)
1711 set
= isl_set_subtract(set
,
1712 isl_set_copy(pwaff1
->p
[i
].set
));
1713 res
= isl_pw_aff_add_piece(res
, set
,
1714 isl_aff_copy(pwaff2
->p
[j
].aff
));
1717 isl_pw_aff_free(pwaff1
);
1718 isl_pw_aff_free(pwaff2
);
1722 isl_pw_aff_free(pwaff1
);
1723 isl_pw_aff_free(pwaff2
);
1727 /* Compute a piecewise quasi-affine expression with a domain that
1728 * is the union of those of pwaff1 and pwaff2 and such that on each
1729 * cell, the quasi-affine expression is the maximum of those of pwaff1
1730 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1731 * cell, then the associated expression is the defined one.
1733 static __isl_give isl_pw_aff
*pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1734 __isl_take isl_pw_aff
*pwaff2
)
1736 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_ge_basic_set
);
1739 __isl_give isl_pw_aff
*isl_pw_aff_union_max(__isl_take isl_pw_aff
*pwaff1
,
1740 __isl_take isl_pw_aff
*pwaff2
)
1742 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1746 /* Compute a piecewise quasi-affine expression with a domain that
1747 * is the union of those of pwaff1 and pwaff2 and such that on each
1748 * cell, the quasi-affine expression is the minimum of those of pwaff1
1749 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1750 * cell, then the associated expression is the defined one.
1752 static __isl_give isl_pw_aff
*pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1753 __isl_take isl_pw_aff
*pwaff2
)
1755 return pw_aff_union_opt(pwaff1
, pwaff2
, &isl_aff_le_basic_set
);
1758 __isl_give isl_pw_aff
*isl_pw_aff_union_min(__isl_take isl_pw_aff
*pwaff1
,
1759 __isl_take isl_pw_aff
*pwaff2
)
1761 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
,
1765 __isl_give isl_pw_aff
*isl_pw_aff_union_opt(__isl_take isl_pw_aff
*pwaff1
,
1766 __isl_take isl_pw_aff
*pwaff2
, int max
)
1769 return isl_pw_aff_union_max(pwaff1
, pwaff2
);
1771 return isl_pw_aff_union_min(pwaff1
, pwaff2
);
1774 /* Construct a map with as domain the domain of pwaff and
1775 * one-dimensional range corresponding to the affine expressions.
1777 static __isl_give isl_map
*map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1786 dim
= isl_pw_aff_get_space(pwaff
);
1787 map
= isl_map_empty(dim
);
1789 for (i
= 0; i
< pwaff
->n
; ++i
) {
1790 isl_basic_map
*bmap
;
1793 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1794 map_i
= isl_map_from_basic_map(bmap
);
1795 map_i
= isl_map_intersect_domain(map_i
,
1796 isl_set_copy(pwaff
->p
[i
].set
));
1797 map
= isl_map_union_disjoint(map
, map_i
);
1800 isl_pw_aff_free(pwaff
);
1805 /* Construct a map with as domain the domain of pwaff and
1806 * one-dimensional range corresponding to the affine expressions.
1808 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1812 if (isl_space_is_set(pwaff
->dim
))
1813 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1814 "space of input is not a map",
1815 return isl_pw_aff_free(pwaff
));
1816 return map_from_pw_aff(pwaff
);
1819 /* Construct a one-dimensional set with as parameter domain
1820 * the domain of pwaff and the single set dimension
1821 * corresponding to the affine expressions.
1823 __isl_give isl_set
*isl_set_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1827 if (!isl_space_is_set(pwaff
->dim
))
1828 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
1829 "space of input is not a set",
1830 return isl_pw_aff_free(pwaff
));
1831 return map_from_pw_aff(pwaff
);
1834 /* Return a set containing those elements in the domain
1835 * of pwaff where it is non-negative.
1837 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1845 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1847 for (i
= 0; i
< pwaff
->n
; ++i
) {
1848 isl_basic_set
*bset
;
1851 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1852 set_i
= isl_set_from_basic_set(bset
);
1853 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1854 set
= isl_set_union_disjoint(set
, set_i
);
1857 isl_pw_aff_free(pwaff
);
1862 /* Return a set containing those elements in the domain
1863 * of pwaff where it is zero (if complement is 0) or not zero
1864 * (if complement is 1).
1866 static __isl_give isl_set
*pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
,
1875 set
= isl_set_empty(isl_pw_aff_get_domain_space(pwaff
));
1877 for (i
= 0; i
< pwaff
->n
; ++i
) {
1878 isl_basic_set
*bset
;
1879 isl_set
*set_i
, *zero
;
1881 bset
= isl_aff_zero_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1882 zero
= isl_set_from_basic_set(bset
);
1883 set_i
= isl_set_copy(pwaff
->p
[i
].set
);
1885 set_i
= isl_set_subtract(set_i
, zero
);
1887 set_i
= isl_set_intersect(set_i
, zero
);
1888 set
= isl_set_union_disjoint(set
, set_i
);
1891 isl_pw_aff_free(pwaff
);
1896 /* Return a set containing those elements in the domain
1897 * of pwaff where it is zero.
1899 __isl_give isl_set
*isl_pw_aff_zero_set(__isl_take isl_pw_aff
*pwaff
)
1901 return pw_aff_zero_set(pwaff
, 0);
1904 /* Return a set containing those elements in the domain
1905 * of pwaff where it is not zero.
1907 __isl_give isl_set
*isl_pw_aff_non_zero_set(__isl_take isl_pw_aff
*pwaff
)
1909 return pw_aff_zero_set(pwaff
, 1);
1912 /* Return a set containing those elements in the shared domain
1913 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1915 * We compute the difference on the shared domain and then construct
1916 * the set of values where this difference is non-negative.
1917 * If strict is set, we first subtract 1 from the difference.
1918 * If equal is set, we only return the elements where pwaff1 and pwaff2
1921 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1922 __isl_take isl_pw_aff
*pwaff2
, int strict
, int equal
)
1924 isl_set
*set1
, *set2
;
1926 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1927 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1928 set1
= isl_set_intersect(set1
, set2
);
1929 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1930 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1931 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1934 isl_space
*dim
= isl_set_get_space(set1
);
1936 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1937 aff
= isl_aff_add_constant_si(aff
, -1);
1938 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1943 return isl_pw_aff_zero_set(pwaff1
);
1944 return isl_pw_aff_nonneg_set(pwaff1
);
1947 /* Return a set containing those elements in the shared domain
1948 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1950 static __isl_give isl_set
*pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1951 __isl_take isl_pw_aff
*pwaff2
)
1953 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 1);
1956 __isl_give isl_set
*isl_pw_aff_eq_set(__isl_take isl_pw_aff
*pwaff1
,
1957 __isl_take isl_pw_aff
*pwaff2
)
1959 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_eq_set
);
1962 /* Return a set containing those elements in the shared domain
1963 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1965 static __isl_give isl_set
*pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1966 __isl_take isl_pw_aff
*pwaff2
)
1968 return pw_aff_gte_set(pwaff1
, pwaff2
, 0, 0);
1971 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1972 __isl_take isl_pw_aff
*pwaff2
)
1974 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ge_set
);
1977 /* Return a set containing those elements in the shared domain
1978 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1980 static __isl_give isl_set
*pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1981 __isl_take isl_pw_aff
*pwaff2
)
1983 return pw_aff_gte_set(pwaff1
, pwaff2
, 1, 0);
1986 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1987 __isl_take isl_pw_aff
*pwaff2
)
1989 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_gt_set
);
1992 __isl_give isl_set
*isl_pw_aff_le_set(__isl_take isl_pw_aff
*pwaff1
,
1993 __isl_take isl_pw_aff
*pwaff2
)
1995 return isl_pw_aff_ge_set(pwaff2
, pwaff1
);
1998 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1999 __isl_take isl_pw_aff
*pwaff2
)
2001 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);
2004 /* Return a set containing those elements in the shared domain
2005 * of the elements of list1 and list2 where each element in list1
2006 * has the relation specified by "fn" with each element in list2.
2008 static __isl_give isl_set
*pw_aff_list_set(__isl_take isl_pw_aff_list
*list1
,
2009 __isl_take isl_pw_aff_list
*list2
,
2010 __isl_give isl_set
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2011 __isl_take isl_pw_aff
*pwaff2
))
2017 if (!list1
|| !list2
)
2020 ctx
= isl_pw_aff_list_get_ctx(list1
);
2021 if (list1
->n
< 1 || list2
->n
< 1)
2022 isl_die(ctx
, isl_error_invalid
,
2023 "list should contain at least one element", goto error
);
2025 set
= isl_set_universe(isl_pw_aff_get_domain_space(list1
->p
[0]));
2026 for (i
= 0; i
< list1
->n
; ++i
)
2027 for (j
= 0; j
< list2
->n
; ++j
) {
2030 set_ij
= fn(isl_pw_aff_copy(list1
->p
[i
]),
2031 isl_pw_aff_copy(list2
->p
[j
]));
2032 set
= isl_set_intersect(set
, set_ij
);
2035 isl_pw_aff_list_free(list1
);
2036 isl_pw_aff_list_free(list2
);
2039 isl_pw_aff_list_free(list1
);
2040 isl_pw_aff_list_free(list2
);
2044 /* Return a set containing those elements in the shared domain
2045 * of the elements of list1 and list2 where each element in list1
2046 * is equal to each element in list2.
2048 __isl_give isl_set
*isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list
*list1
,
2049 __isl_take isl_pw_aff_list
*list2
)
2051 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_eq_set
);
2054 __isl_give isl_set
*isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list
*list1
,
2055 __isl_take isl_pw_aff_list
*list2
)
2057 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ne_set
);
2060 /* Return a set containing those elements in the shared domain
2061 * of the elements of list1 and list2 where each element in list1
2062 * is less than or equal to each element in list2.
2064 __isl_give isl_set
*isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list
*list1
,
2065 __isl_take isl_pw_aff_list
*list2
)
2067 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_le_set
);
2070 __isl_give isl_set
*isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list
*list1
,
2071 __isl_take isl_pw_aff_list
*list2
)
2073 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_lt_set
);
2076 __isl_give isl_set
*isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list
*list1
,
2077 __isl_take isl_pw_aff_list
*list2
)
2079 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_ge_set
);
2082 __isl_give isl_set
*isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list
*list1
,
2083 __isl_take isl_pw_aff_list
*list2
)
2085 return pw_aff_list_set(list1
, list2
, &isl_pw_aff_gt_set
);
2089 /* Return a set containing those elements in the shared domain
2090 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2092 static __isl_give isl_set
*pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2093 __isl_take isl_pw_aff
*pwaff2
)
2095 isl_set
*set_lt
, *set_gt
;
2097 set_lt
= isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1
),
2098 isl_pw_aff_copy(pwaff2
));
2099 set_gt
= isl_pw_aff_gt_set(pwaff1
, pwaff2
);
2100 return isl_set_union_disjoint(set_lt
, set_gt
);
2103 __isl_give isl_set
*isl_pw_aff_ne_set(__isl_take isl_pw_aff
*pwaff1
,
2104 __isl_take isl_pw_aff
*pwaff2
)
2106 return align_params_pw_pw_set_and(pwaff1
, pwaff2
, &pw_aff_ne_set
);
2109 __isl_give isl_pw_aff
*isl_pw_aff_scale_down(__isl_take isl_pw_aff
*pwaff
,
2114 if (isl_int_is_one(v
))
2116 if (!isl_int_is_pos(v
))
2117 isl_die(isl_pw_aff_get_ctx(pwaff
), isl_error_invalid
,
2118 "factor needs to be positive",
2119 return isl_pw_aff_free(pwaff
));
2120 pwaff
= isl_pw_aff_cow(pwaff
);
2126 for (i
= 0; i
< pwaff
->n
; ++i
) {
2127 pwaff
->p
[i
].aff
= isl_aff_scale_down(pwaff
->p
[i
].aff
, v
);
2128 if (!pwaff
->p
[i
].aff
)
2129 return isl_pw_aff_free(pwaff
);
2135 __isl_give isl_pw_aff
*isl_pw_aff_floor(__isl_take isl_pw_aff
*pwaff
)
2139 pwaff
= isl_pw_aff_cow(pwaff
);
2145 for (i
= 0; i
< pwaff
->n
; ++i
) {
2146 pwaff
->p
[i
].aff
= isl_aff_floor(pwaff
->p
[i
].aff
);
2147 if (!pwaff
->p
[i
].aff
)
2148 return isl_pw_aff_free(pwaff
);
2154 __isl_give isl_pw_aff
*isl_pw_aff_ceil(__isl_take isl_pw_aff
*pwaff
)
2158 pwaff
= isl_pw_aff_cow(pwaff
);
2164 for (i
= 0; i
< pwaff
->n
; ++i
) {
2165 pwaff
->p
[i
].aff
= isl_aff_ceil(pwaff
->p
[i
].aff
);
2166 if (!pwaff
->p
[i
].aff
)
2167 return isl_pw_aff_free(pwaff
);
2173 /* Assuming that "cond1" and "cond2" are disjoint,
2174 * return an affine expression that is equal to pwaff1 on cond1
2175 * and to pwaff2 on cond2.
2177 static __isl_give isl_pw_aff
*isl_pw_aff_select(
2178 __isl_take isl_set
*cond1
, __isl_take isl_pw_aff
*pwaff1
,
2179 __isl_take isl_set
*cond2
, __isl_take isl_pw_aff
*pwaff2
)
2181 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, cond1
);
2182 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, cond2
);
2184 return isl_pw_aff_add_disjoint(pwaff1
, pwaff2
);
2187 /* Return an affine expression that is equal to pwaff_true for elements
2188 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2190 * That is, return cond ? pwaff_true : pwaff_false;
2192 __isl_give isl_pw_aff
*isl_pw_aff_cond(__isl_take isl_pw_aff
*cond
,
2193 __isl_take isl_pw_aff
*pwaff_true
, __isl_take isl_pw_aff
*pwaff_false
)
2195 isl_set
*cond_true
, *cond_false
;
2197 cond_true
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond
));
2198 cond_false
= isl_pw_aff_zero_set(cond
);
2199 return isl_pw_aff_select(cond_true
, pwaff_true
,
2200 cond_false
, pwaff_false
);
2203 int isl_aff_is_cst(__isl_keep isl_aff
*aff
)
2208 return isl_seq_first_non_zero(aff
->v
->el
+ 2, aff
->v
->size
- 2) == -1;
2211 /* Check whether pwaff is a piecewise constant.
2213 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff
*pwaff
)
2220 for (i
= 0; i
< pwaff
->n
; ++i
) {
2221 int is_cst
= isl_aff_is_cst(pwaff
->p
[i
].aff
);
2222 if (is_cst
< 0 || !is_cst
)
2229 __isl_give isl_aff
*isl_aff_mul(__isl_take isl_aff
*aff1
,
2230 __isl_take isl_aff
*aff2
)
2232 if (!isl_aff_is_cst(aff2
) && isl_aff_is_cst(aff1
))
2233 return isl_aff_mul(aff2
, aff1
);
2235 if (!isl_aff_is_cst(aff2
))
2236 isl_die(isl_aff_get_ctx(aff1
), isl_error_invalid
,
2237 "at least one affine expression should be constant",
2240 aff1
= isl_aff_cow(aff1
);
2244 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[1]);
2245 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[0]);
2255 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2257 __isl_give isl_aff
*isl_aff_div(__isl_take isl_aff
*aff1
,
2258 __isl_take isl_aff
*aff2
)
2263 is_cst
= isl_aff_is_cst(aff2
);
2267 isl_die(isl_aff_get_ctx(aff2
), isl_error_invalid
,
2268 "second argument should be a constant", goto error
);
2273 neg
= isl_int_is_neg(aff2
->v
->el
[1]);
2275 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2276 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2279 aff1
= isl_aff_scale(aff1
, aff2
->v
->el
[0]);
2280 aff1
= isl_aff_scale_down(aff1
, aff2
->v
->el
[1]);
2283 isl_int_neg(aff2
->v
->el
[0], aff2
->v
->el
[0]);
2284 isl_int_neg(aff2
->v
->el
[1], aff2
->v
->el
[1]);
2295 static __isl_give isl_pw_aff
*pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2296 __isl_take isl_pw_aff
*pwaff2
)
2298 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_add
);
2301 __isl_give isl_pw_aff
*isl_pw_aff_add(__isl_take isl_pw_aff
*pwaff1
,
2302 __isl_take isl_pw_aff
*pwaff2
)
2304 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_add
);
2307 __isl_give isl_pw_aff
*isl_pw_aff_union_add(__isl_take isl_pw_aff
*pwaff1
,
2308 __isl_take isl_pw_aff
*pwaff2
)
2310 return isl_pw_aff_union_add_(pwaff1
, pwaff2
);
2313 static __isl_give isl_pw_aff
*pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2314 __isl_take isl_pw_aff
*pwaff2
)
2316 return isl_pw_aff_on_shared_domain(pwaff1
, pwaff2
, &isl_aff_mul
);
2319 __isl_give isl_pw_aff
*isl_pw_aff_mul(__isl_take isl_pw_aff
*pwaff1
,
2320 __isl_take isl_pw_aff
*pwaff2
)
2322 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_mul
);
2325 static __isl_give isl_pw_aff
*pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2326 __isl_take isl_pw_aff
*pa2
)
2328 return isl_pw_aff_on_shared_domain(pa1
, pa2
, &isl_aff_div
);
2331 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2333 __isl_give isl_pw_aff
*isl_pw_aff_div(__isl_take isl_pw_aff
*pa1
,
2334 __isl_take isl_pw_aff
*pa2
)
2338 is_cst
= isl_pw_aff_is_cst(pa2
);
2342 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
2343 "second argument should be a piecewise constant",
2345 return isl_pw_aff_align_params_pw_pw_and(pa1
, pa2
, &pw_aff_div
);
2347 isl_pw_aff_free(pa1
);
2348 isl_pw_aff_free(pa2
);
2352 /* Compute the quotient of the integer division of "pa1" by "pa2"
2353 * with rounding towards zero.
2354 * "pa2" is assumed to be a piecewise constant.
2356 * In particular, return
2358 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2361 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_q(__isl_take isl_pw_aff
*pa1
,
2362 __isl_take isl_pw_aff
*pa2
)
2368 is_cst
= isl_pw_aff_is_cst(pa2
);
2372 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
2373 "second argument should be a piecewise constant",
2376 pa1
= isl_pw_aff_div(pa1
, pa2
);
2378 cond
= isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1
));
2379 f
= isl_pw_aff_floor(isl_pw_aff_copy(pa1
));
2380 c
= isl_pw_aff_ceil(pa1
);
2381 return isl_pw_aff_cond(isl_set_indicator_function(cond
), f
, c
);
2383 isl_pw_aff_free(pa1
);
2384 isl_pw_aff_free(pa2
);
2388 /* Compute the remainder of the integer division of "pa1" by "pa2"
2389 * with rounding towards zero.
2390 * "pa2" is assumed to be a piecewise constant.
2392 * In particular, return
2394 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2397 __isl_give isl_pw_aff
*isl_pw_aff_tdiv_r(__isl_take isl_pw_aff
*pa1
,
2398 __isl_take isl_pw_aff
*pa2
)
2403 is_cst
= isl_pw_aff_is_cst(pa2
);
2407 isl_die(isl_pw_aff_get_ctx(pa2
), isl_error_invalid
,
2408 "second argument should be a piecewise constant",
2410 res
= isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1
), isl_pw_aff_copy(pa2
));
2411 res
= isl_pw_aff_mul(pa2
, res
);
2412 res
= isl_pw_aff_sub(pa1
, res
);
2415 isl_pw_aff_free(pa1
);
2416 isl_pw_aff_free(pa2
);
2420 static __isl_give isl_pw_aff
*pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2421 __isl_take isl_pw_aff
*pwaff2
)
2426 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2427 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2428 le
= isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1
),
2429 isl_pw_aff_copy(pwaff2
));
2430 dom
= isl_set_subtract(dom
, isl_set_copy(le
));
2431 return isl_pw_aff_select(le
, pwaff1
, dom
, pwaff2
);
2434 __isl_give isl_pw_aff
*isl_pw_aff_min(__isl_take isl_pw_aff
*pwaff1
,
2435 __isl_take isl_pw_aff
*pwaff2
)
2437 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_min
);
2440 static __isl_give isl_pw_aff
*pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2441 __isl_take isl_pw_aff
*pwaff2
)
2446 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
)),
2447 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
)));
2448 ge
= isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1
),
2449 isl_pw_aff_copy(pwaff2
));
2450 dom
= isl_set_subtract(dom
, isl_set_copy(ge
));
2451 return isl_pw_aff_select(ge
, pwaff1
, dom
, pwaff2
);
2454 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
2455 __isl_take isl_pw_aff
*pwaff2
)
2457 return isl_pw_aff_align_params_pw_pw_and(pwaff1
, pwaff2
, &pw_aff_max
);
2460 static __isl_give isl_pw_aff
*pw_aff_list_reduce(
2461 __isl_take isl_pw_aff_list
*list
,
2462 __isl_give isl_pw_aff
*(*fn
)(__isl_take isl_pw_aff
*pwaff1
,
2463 __isl_take isl_pw_aff
*pwaff2
))
2472 ctx
= isl_pw_aff_list_get_ctx(list
);
2474 isl_die(ctx
, isl_error_invalid
,
2475 "list should contain at least one element",
2476 return isl_pw_aff_list_free(list
));
2478 res
= isl_pw_aff_copy(list
->p
[0]);
2479 for (i
= 1; i
< list
->n
; ++i
)
2480 res
= fn(res
, isl_pw_aff_copy(list
->p
[i
]));
2482 isl_pw_aff_list_free(list
);
2486 /* Return an isl_pw_aff that maps each element in the intersection of the
2487 * domains of the elements of list to the minimal corresponding affine
2490 __isl_give isl_pw_aff
*isl_pw_aff_list_min(__isl_take isl_pw_aff_list
*list
)
2492 return pw_aff_list_reduce(list
, &isl_pw_aff_min
);
2495 /* Return an isl_pw_aff that maps each element in the intersection of the
2496 * domains of the elements of list to the maximal corresponding affine
2499 __isl_give isl_pw_aff
*isl_pw_aff_list_max(__isl_take isl_pw_aff_list
*list
)
2501 return pw_aff_list_reduce(list
, &isl_pw_aff_max
);
2507 #include <isl_multi_templ.c>
2509 /* Construct an isl_multi_aff in the given space with value zero in
2510 * each of the output dimensions.
2512 __isl_give isl_multi_aff
*isl_multi_aff_zero(__isl_take isl_space
*space
)
2520 n
= isl_space_dim(space
, isl_dim_out
);
2521 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2524 isl_space_free(space
);
2527 isl_local_space
*ls
;
2530 space
= isl_space_domain(space
);
2531 ls
= isl_local_space_from_space(space
);
2532 aff
= isl_aff_zero_on_domain(ls
);
2534 for (i
= 0; i
< n
; ++i
)
2535 ma
= isl_multi_aff_set_aff(ma
, i
, isl_aff_copy(aff
));
2543 /* Create an isl_multi_aff in the given space that maps each
2544 * input dimension to the corresponding output dimension.
2546 __isl_give isl_multi_aff
*isl_multi_aff_identity(__isl_take isl_space
*space
)
2554 if (isl_space_is_set(space
))
2555 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2556 "expecting map space", goto error
);
2558 n
= isl_space_dim(space
, isl_dim_out
);
2559 if (n
!= isl_space_dim(space
, isl_dim_in
))
2560 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2561 "number of input and output dimensions needs to be "
2562 "the same", goto error
);
2564 ma
= isl_multi_aff_alloc(isl_space_copy(space
));
2567 isl_space_free(space
);
2570 isl_local_space
*ls
;
2573 space
= isl_space_domain(space
);
2574 ls
= isl_local_space_from_space(space
);
2575 aff
= isl_aff_zero_on_domain(ls
);
2577 for (i
= 0; i
< n
; ++i
) {
2579 aff_i
= isl_aff_copy(aff
);
2580 aff_i
= isl_aff_add_coefficient_si(aff_i
,
2582 ma
= isl_multi_aff_set_aff(ma
, i
, aff_i
);
2590 isl_space_free(space
);
2594 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2597 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_multi_aff(
2598 __isl_take isl_multi_aff
*ma
)
2600 isl_set
*dom
= isl_set_universe(isl_multi_aff_get_domain_space(ma
));
2601 return isl_pw_multi_aff_alloc(dom
, ma
);
2604 /* Create a piecewise multi-affine expression in the given space that maps each
2605 * input dimension to the corresponding output dimension.
2607 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_identity(
2608 __isl_take isl_space
*space
)
2610 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space
));
2613 __isl_give isl_multi_aff
*isl_multi_aff_add(__isl_take isl_multi_aff
*maff1
,
2614 __isl_take isl_multi_aff
*maff2
)
2619 maff1
= isl_multi_aff_cow(maff1
);
2620 if (!maff1
|| !maff2
)
2623 ctx
= isl_multi_aff_get_ctx(maff1
);
2624 if (!isl_space_is_equal(maff1
->space
, maff2
->space
))
2625 isl_die(ctx
, isl_error_invalid
,
2626 "spaces don't match", goto error
);
2628 for (i
= 0; i
< maff1
->n
; ++i
) {
2629 maff1
->p
[i
] = isl_aff_add(maff1
->p
[i
],
2630 isl_aff_copy(maff2
->p
[i
]));
2635 isl_multi_aff_free(maff2
);
2638 isl_multi_aff_free(maff1
);
2639 isl_multi_aff_free(maff2
);
2643 /* Given two multi-affine expressions A -> B and C -> D,
2644 * construct a multi-affine expression [A -> C] -> [B -> D].
2646 __isl_give isl_multi_aff
*isl_multi_aff_product(
2647 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
2653 int in1
, in2
, out1
, out2
;
2655 in1
= isl_multi_aff_dim(ma1
, isl_dim_in
);
2656 in2
= isl_multi_aff_dim(ma2
, isl_dim_in
);
2657 out1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
2658 out2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
2659 space
= isl_space_product(isl_multi_aff_get_space(ma1
),
2660 isl_multi_aff_get_space(ma2
));
2661 res
= isl_multi_aff_alloc(isl_space_copy(space
));
2662 space
= isl_space_domain(space
);
2664 for (i
= 0; i
< out1
; ++i
) {
2665 aff
= isl_multi_aff_get_aff(ma1
, i
);
2666 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, in1
, in2
);
2667 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2668 res
= isl_multi_aff_set_aff(res
, i
, aff
);
2671 for (i
= 0; i
< out2
; ++i
) {
2672 aff
= isl_multi_aff_get_aff(ma2
, i
);
2673 aff
= isl_aff_insert_dims(aff
, isl_dim_in
, 0, in1
);
2674 aff
= isl_aff_reset_domain_space(aff
, isl_space_copy(space
));
2675 res
= isl_multi_aff_set_aff(res
, out1
+ i
, aff
);
2678 isl_space_free(space
);
2679 isl_multi_aff_free(ma1
);
2680 isl_multi_aff_free(ma2
);
2684 /* Exploit the equalities in "eq" to simplify the affine expressions.
2686 static __isl_give isl_multi_aff
*isl_multi_aff_substitute_equalities(
2687 __isl_take isl_multi_aff
*maff
, __isl_take isl_basic_set
*eq
)
2691 maff
= isl_multi_aff_cow(maff
);
2695 for (i
= 0; i
< maff
->n
; ++i
) {
2696 maff
->p
[i
] = isl_aff_substitute_equalities(maff
->p
[i
],
2697 isl_basic_set_copy(eq
));
2702 isl_basic_set_free(eq
);
2705 isl_basic_set_free(eq
);
2706 isl_multi_aff_free(maff
);
2710 __isl_give isl_multi_aff
*isl_multi_aff_scale(__isl_take isl_multi_aff
*maff
,
2715 maff
= isl_multi_aff_cow(maff
);
2719 for (i
= 0; i
< maff
->n
; ++i
) {
2720 maff
->p
[i
] = isl_aff_scale(maff
->p
[i
], f
);
2722 return isl_multi_aff_free(maff
);
2728 __isl_give isl_multi_aff
*isl_multi_aff_add_on_domain(__isl_keep isl_set
*dom
,
2729 __isl_take isl_multi_aff
*maff1
, __isl_take isl_multi_aff
*maff2
)
2731 maff1
= isl_multi_aff_add(maff1
, maff2
);
2732 maff1
= isl_multi_aff_gist(maff1
, isl_set_copy(dom
));
2736 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff
*maff
)
2744 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff
*maff1
,
2745 __isl_keep isl_multi_aff
*maff2
)
2750 if (!maff1
|| !maff2
)
2752 if (maff1
->n
!= maff2
->n
)
2754 equal
= isl_space_is_equal(maff1
->space
, maff2
->space
);
2755 if (equal
< 0 || !equal
)
2758 for (i
= 0; i
< maff1
->n
; ++i
) {
2759 equal
= isl_aff_plain_is_equal(maff1
->p
[i
], maff2
->p
[i
]);
2760 if (equal
< 0 || !equal
)
2767 __isl_give isl_multi_aff
*isl_multi_aff_set_dim_name(
2768 __isl_take isl_multi_aff
*maff
,
2769 enum isl_dim_type type
, unsigned pos
, const char *s
)
2773 maff
= isl_multi_aff_cow(maff
);
2777 maff
->space
= isl_space_set_dim_name(maff
->space
, type
, pos
, s
);
2779 return isl_multi_aff_free(maff
);
2781 if (type
== isl_dim_out
)
2783 for (i
= 0; i
< maff
->n
; ++i
) {
2784 maff
->p
[i
] = isl_aff_set_dim_name(maff
->p
[i
], type
, pos
, s
);
2786 return isl_multi_aff_free(maff
);
2792 __isl_give isl_multi_aff
*isl_multi_aff_drop_dims(__isl_take isl_multi_aff
*maff
,
2793 enum isl_dim_type type
, unsigned first
, unsigned n
)
2797 maff
= isl_multi_aff_cow(maff
);
2801 maff
->space
= isl_space_drop_dims(maff
->space
, type
, first
, n
);
2803 return isl_multi_aff_free(maff
);
2805 if (type
== isl_dim_out
) {
2806 for (i
= 0; i
< n
; ++i
)
2807 isl_aff_free(maff
->p
[first
+ i
]);
2808 for (i
= first
; i
+ n
< maff
->n
; ++i
)
2809 maff
->p
[i
] = maff
->p
[i
+ n
];
2814 for (i
= 0; i
< maff
->n
; ++i
) {
2815 maff
->p
[i
] = isl_aff_drop_dims(maff
->p
[i
], type
, first
, n
);
2817 return isl_multi_aff_free(maff
);
2823 /* Return the set of domain elements where "ma1" is lexicographically
2824 * smaller than or equal to "ma2".
2826 __isl_give isl_set
*isl_multi_aff_lex_le_set(__isl_take isl_multi_aff
*ma1
,
2827 __isl_take isl_multi_aff
*ma2
)
2829 return isl_multi_aff_lex_ge_set(ma2
, ma1
);
2832 /* Return the set of domain elements where "ma1" is lexicographically
2833 * greater than or equal to "ma2".
2835 __isl_give isl_set
*isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff
*ma1
,
2836 __isl_take isl_multi_aff
*ma2
)
2839 isl_map
*map1
, *map2
;
2842 map1
= isl_map_from_multi_aff(ma1
);
2843 map2
= isl_map_from_multi_aff(ma2
);
2844 map
= isl_map_range_product(map1
, map2
);
2845 space
= isl_space_range(isl_map_get_space(map
));
2846 space
= isl_space_domain(isl_space_unwrap(space
));
2847 ge
= isl_map_lex_ge(space
);
2848 map
= isl_map_intersect_range(map
, isl_map_wrap(ge
));
2850 return isl_map_domain(map
);
2854 #define PW isl_pw_multi_aff
2856 #define EL isl_multi_aff
2858 #define EL_IS_ZERO is_empty
2862 #define IS_ZERO is_empty
2865 #undef DEFAULT_IS_ZERO
2866 #define DEFAULT_IS_ZERO 0
2871 #define NO_INVOLVES_DIMS
2872 #define NO_MOVE_DIMS
2873 #define NO_INSERT_DIMS
2877 #include <isl_pw_templ.c>
2880 #define UNION isl_union_pw_multi_aff
2882 #define PART isl_pw_multi_aff
2884 #define PARTS pw_multi_aff
2885 #define ALIGN_DOMAIN
2889 #include <isl_union_templ.c>
2891 /* Given a function "cmp" that returns the set of elements where
2892 * "ma1" is "better" than "ma2", return the intersection of this
2893 * set with "dom1" and "dom2".
2895 static __isl_give isl_set
*shared_and_better(__isl_keep isl_set
*dom1
,
2896 __isl_keep isl_set
*dom2
, __isl_keep isl_multi_aff
*ma1
,
2897 __isl_keep isl_multi_aff
*ma2
,
2898 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2899 __isl_take isl_multi_aff
*ma2
))
2905 common
= isl_set_intersect(isl_set_copy(dom1
), isl_set_copy(dom2
));
2906 is_empty
= isl_set_plain_is_empty(common
);
2907 if (is_empty
>= 0 && is_empty
)
2910 return isl_set_free(common
);
2911 better
= cmp(isl_multi_aff_copy(ma1
), isl_multi_aff_copy(ma2
));
2912 better
= isl_set_intersect(common
, better
);
2917 /* Given a function "cmp" that returns the set of elements where
2918 * "ma1" is "better" than "ma2", return a piecewise multi affine
2919 * expression defined on the union of the definition domains
2920 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2921 * "pma2" on each cell. If only one of the two input functions
2922 * is defined on a given cell, then it is considered the best.
2924 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_opt(
2925 __isl_take isl_pw_multi_aff
*pma1
,
2926 __isl_take isl_pw_multi_aff
*pma2
,
2927 __isl_give isl_set
*(*cmp
)(__isl_take isl_multi_aff
*ma1
,
2928 __isl_take isl_multi_aff
*ma2
))
2931 isl_pw_multi_aff
*res
= NULL
;
2933 isl_set
*set
= NULL
;
2938 ctx
= isl_space_get_ctx(pma1
->dim
);
2939 if (!isl_space_is_equal(pma1
->dim
, pma2
->dim
))
2940 isl_die(ctx
, isl_error_invalid
,
2941 "arguments should live in the same space", goto error
);
2943 if (isl_pw_multi_aff_is_empty(pma1
)) {
2944 isl_pw_multi_aff_free(pma1
);
2948 if (isl_pw_multi_aff_is_empty(pma2
)) {
2949 isl_pw_multi_aff_free(pma2
);
2953 n
= 2 * (pma1
->n
+ 1) * (pma2
->n
+ 1);
2954 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma1
->dim
), n
);
2956 for (i
= 0; i
< pma1
->n
; ++i
) {
2957 set
= isl_set_copy(pma1
->p
[i
].set
);
2958 for (j
= 0; j
< pma2
->n
; ++j
) {
2962 better
= shared_and_better(pma2
->p
[j
].set
,
2963 pma1
->p
[i
].set
, pma2
->p
[j
].maff
,
2964 pma1
->p
[i
].maff
, cmp
);
2965 is_empty
= isl_set_plain_is_empty(better
);
2966 if (is_empty
< 0 || is_empty
) {
2967 isl_set_free(better
);
2972 set
= isl_set_subtract(set
, isl_set_copy(better
));
2974 res
= isl_pw_multi_aff_add_piece(res
, better
,
2975 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2977 res
= isl_pw_multi_aff_add_piece(res
, set
,
2978 isl_multi_aff_copy(pma1
->p
[i
].maff
));
2981 for (j
= 0; j
< pma2
->n
; ++j
) {
2982 set
= isl_set_copy(pma2
->p
[j
].set
);
2983 for (i
= 0; i
< pma1
->n
; ++i
)
2984 set
= isl_set_subtract(set
,
2985 isl_set_copy(pma1
->p
[i
].set
));
2986 res
= isl_pw_multi_aff_add_piece(res
, set
,
2987 isl_multi_aff_copy(pma2
->p
[j
].maff
));
2990 isl_pw_multi_aff_free(pma1
);
2991 isl_pw_multi_aff_free(pma2
);
2995 isl_pw_multi_aff_free(pma1
);
2996 isl_pw_multi_aff_free(pma2
);
2998 return isl_pw_multi_aff_free(res
);
3001 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmax(
3002 __isl_take isl_pw_multi_aff
*pma1
,
3003 __isl_take isl_pw_multi_aff
*pma2
)
3005 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_ge_set
);
3008 /* Given two piecewise multi affine expressions, return a piecewise
3009 * multi-affine expression defined on the union of the definition domains
3010 * of the inputs that is equal to the lexicographic maximum of the two
3011 * inputs on each cell. If only one of the two inputs is defined on
3012 * a given cell, then it is considered to be the maximum.
3014 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmax(
3015 __isl_take isl_pw_multi_aff
*pma1
,
3016 __isl_take isl_pw_multi_aff
*pma2
)
3018 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3019 &pw_multi_aff_union_lexmax
);
3022 static __isl_give isl_pw_multi_aff
*pw_multi_aff_union_lexmin(
3023 __isl_take isl_pw_multi_aff
*pma1
,
3024 __isl_take isl_pw_multi_aff
*pma2
)
3026 return pw_multi_aff_union_opt(pma1
, pma2
, &isl_multi_aff_lex_le_set
);
3029 /* Given two piecewise multi affine expressions, return a piecewise
3030 * multi-affine expression defined on the union of the definition domains
3031 * of the inputs that is equal to the lexicographic minimum of the two
3032 * inputs on each cell. If only one of the two inputs is defined on
3033 * a given cell, then it is considered to be the minimum.
3035 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_lexmin(
3036 __isl_take isl_pw_multi_aff
*pma1
,
3037 __isl_take isl_pw_multi_aff
*pma2
)
3039 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3040 &pw_multi_aff_union_lexmin
);
3043 static __isl_give isl_pw_multi_aff
*pw_multi_aff_add(
3044 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3046 return isl_pw_multi_aff_on_shared_domain(pma1
, pma2
,
3047 &isl_multi_aff_add
);
3050 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_add(
3051 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3053 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3057 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_union_add(
3058 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3060 return isl_pw_multi_aff_union_add_(pma1
, pma2
);
3063 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3064 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3066 static __isl_give isl_pw_multi_aff
*pw_multi_aff_product(
3067 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3071 isl_pw_multi_aff
*res
;
3076 n
= pma1
->n
* pma2
->n
;
3077 space
= isl_space_product(isl_space_copy(pma1
->dim
),
3078 isl_space_copy(pma2
->dim
));
3079 res
= isl_pw_multi_aff_alloc_size(space
, n
);
3081 for (i
= 0; i
< pma1
->n
; ++i
) {
3082 for (j
= 0; j
< pma2
->n
; ++j
) {
3086 domain
= isl_set_product(isl_set_copy(pma1
->p
[i
].set
),
3087 isl_set_copy(pma2
->p
[j
].set
));
3088 ma
= isl_multi_aff_product(
3089 isl_multi_aff_copy(pma1
->p
[i
].maff
),
3090 isl_multi_aff_copy(pma2
->p
[i
].maff
));
3091 res
= isl_pw_multi_aff_add_piece(res
, domain
, ma
);
3095 isl_pw_multi_aff_free(pma1
);
3096 isl_pw_multi_aff_free(pma2
);
3099 isl_pw_multi_aff_free(pma1
);
3100 isl_pw_multi_aff_free(pma2
);
3104 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_product(
3105 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3107 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3108 &pw_multi_aff_product
);
3111 /* Construct a map mapping the domain of the piecewise multi-affine expression
3112 * to its range, with each dimension in the range equated to the
3113 * corresponding affine expression on its cell.
3115 __isl_give isl_map
*isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3123 map
= isl_map_empty(isl_pw_multi_aff_get_space(pma
));
3125 for (i
= 0; i
< pma
->n
; ++i
) {
3126 isl_multi_aff
*maff
;
3127 isl_basic_map
*bmap
;
3130 maff
= isl_multi_aff_copy(pma
->p
[i
].maff
);
3131 bmap
= isl_basic_map_from_multi_aff(maff
);
3132 map_i
= isl_map_from_basic_map(bmap
);
3133 map_i
= isl_map_intersect_domain(map_i
,
3134 isl_set_copy(pma
->p
[i
].set
));
3135 map
= isl_map_union_disjoint(map
, map_i
);
3138 isl_pw_multi_aff_free(pma
);
3142 __isl_give isl_set
*isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
)
3147 if (!isl_space_is_set(pma
->dim
))
3148 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3149 "isl_pw_multi_aff cannot be converted into an isl_set",
3150 return isl_pw_multi_aff_free(pma
));
3152 return isl_map_from_pw_multi_aff(pma
);
3155 /* Given a basic map with a single output dimension that is defined
3156 * in terms of the parameters and input dimensions using an equality,
3157 * extract an isl_aff that expresses the output dimension in terms
3158 * of the parameters and input dimensions.
3160 * Since some applications expect the result of isl_pw_multi_aff_from_map
3161 * to only contain integer affine expressions, we compute the floor
3162 * of the expression before returning.
3164 * This function shares some similarities with
3165 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3167 static __isl_give isl_aff
*extract_isl_aff_from_basic_map(
3168 __isl_take isl_basic_map
*bmap
)
3173 isl_local_space
*ls
;
3178 if (isl_basic_map_dim(bmap
, isl_dim_out
) != 1)
3179 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3180 "basic map should have a single output dimension",
3182 offset
= isl_basic_map_offset(bmap
, isl_dim_out
);
3183 total
= isl_basic_map_total_dim(bmap
);
3184 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
3185 if (isl_int_is_zero(bmap
->eq
[i
][offset
]))
3187 if (isl_seq_first_non_zero(bmap
->eq
[i
] + offset
+ 1,
3188 1 + total
- (offset
+ 1)) != -1)
3192 if (i
>= bmap
->n_eq
)
3193 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3194 "unable to find suitable equality", goto error
);
3195 ls
= isl_basic_map_get_local_space(bmap
);
3196 aff
= isl_aff_alloc(isl_local_space_domain(ls
));
3199 if (isl_int_is_neg(bmap
->eq
[i
][offset
]))
3200 isl_seq_cpy(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3202 isl_seq_neg(aff
->v
->el
+ 1, bmap
->eq
[i
], offset
);
3203 isl_seq_clr(aff
->v
->el
+ 1 + offset
, aff
->v
->size
- (1 + offset
));
3204 isl_int_abs(aff
->v
->el
[0], bmap
->eq
[i
][offset
]);
3205 isl_basic_map_free(bmap
);
3207 aff
= isl_aff_remove_unused_divs(aff
);
3208 aff
= isl_aff_floor(aff
);
3211 isl_basic_map_free(bmap
);
3215 /* Given a basic map where each output dimension is defined
3216 * in terms of the parameters and input dimensions using an equality,
3217 * extract an isl_multi_aff that expresses the output dimensions in terms
3218 * of the parameters and input dimensions.
3220 static __isl_give isl_multi_aff
*extract_isl_multi_aff_from_basic_map(
3221 __isl_take isl_basic_map
*bmap
)
3230 ma
= isl_multi_aff_alloc(isl_basic_map_get_space(bmap
));
3231 n_out
= isl_basic_map_dim(bmap
, isl_dim_out
);
3233 for (i
= 0; i
< n_out
; ++i
) {
3234 isl_basic_map
*bmap_i
;
3237 bmap_i
= isl_basic_map_copy(bmap
);
3238 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
,
3239 i
+ 1, n_out
- (1 + i
));
3240 bmap_i
= isl_basic_map_project_out(bmap_i
, isl_dim_out
, 0, i
);
3241 aff
= extract_isl_aff_from_basic_map(bmap_i
);
3242 ma
= isl_multi_aff_set_aff(ma
, i
, aff
);
3245 isl_basic_map_free(bmap
);
3250 /* Create an isl_pw_multi_aff that is equivalent to
3251 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3252 * The given basic map is such that each output dimension is defined
3253 * in terms of the parameters and input dimensions using an equality.
3255 static __isl_give isl_pw_multi_aff
*plain_pw_multi_aff_from_map(
3256 __isl_take isl_set
*domain
, __isl_take isl_basic_map
*bmap
)
3260 ma
= extract_isl_multi_aff_from_basic_map(bmap
);
3261 return isl_pw_multi_aff_alloc(domain
, ma
);
3264 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3265 * This obviously only works if the input "map" is single-valued.
3266 * If so, we compute the lexicographic minimum of the image in the form
3267 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3268 * to its lexicographic minimum.
3269 * If the input is not single-valued, we produce an error.
3271 * As a special case, we first check if all output dimensions are uniquely
3272 * defined in terms of the parameters and input dimensions over the entire
3273 * domain. If so, we extract the desired isl_pw_multi_aff directly
3274 * from the affine hull of "map" and its domain.
3276 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_map(__isl_take isl_map
*map
)
3280 isl_pw_multi_aff
*pma
;
3281 isl_basic_map
*hull
;
3286 hull
= isl_map_affine_hull(isl_map_copy(map
));
3287 sv
= isl_basic_map_plain_is_single_valued(hull
);
3289 return plain_pw_multi_aff_from_map(isl_map_domain(map
), hull
);
3290 isl_basic_map_free(hull
);
3294 sv
= isl_map_is_single_valued(map
);
3298 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3299 "map is not single-valued", goto error
);
3300 map
= isl_map_make_disjoint(map
);
3304 pma
= isl_pw_multi_aff_empty(isl_map_get_space(map
));
3306 for (i
= 0; i
< map
->n
; ++i
) {
3307 isl_pw_multi_aff
*pma_i
;
3308 isl_basic_map
*bmap
;
3309 bmap
= isl_basic_map_copy(map
->p
[i
]);
3310 pma_i
= isl_basic_map_lexmin_pw_multi_aff(bmap
);
3311 pma
= isl_pw_multi_aff_add_disjoint(pma
, pma_i
);
3321 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_set(__isl_take isl_set
*set
)
3323 return isl_pw_multi_aff_from_map(set
);
3326 /* Return the piecewise affine expression "set ? 1 : 0".
3328 __isl_give isl_pw_aff
*isl_set_indicator_function(__isl_take isl_set
*set
)
3331 isl_space
*space
= isl_set_get_space(set
);
3332 isl_local_space
*ls
= isl_local_space_from_space(space
);
3333 isl_aff
*zero
= isl_aff_zero_on_domain(isl_local_space_copy(ls
));
3334 isl_aff
*one
= isl_aff_zero_on_domain(ls
);
3336 one
= isl_aff_add_constant_si(one
, 1);
3337 pa
= isl_pw_aff_alloc(isl_set_copy(set
), one
);
3338 set
= isl_set_complement(set
);
3339 pa
= isl_pw_aff_add_disjoint(pa
, isl_pw_aff_alloc(set
, zero
));
3344 /* Plug in "subs" for dimension "type", "pos" of "aff".
3346 * Let i be the dimension to replace and let "subs" be of the form
3350 * and "aff" of the form
3356 * (a f + d g')/(m d)
3358 * where g' is the result of plugging in "subs" in each of the integer
3361 __isl_give isl_aff
*isl_aff_substitute(__isl_take isl_aff
*aff
,
3362 enum isl_dim_type type
, unsigned pos
, __isl_keep isl_aff
*subs
)
3367 aff
= isl_aff_cow(aff
);
3369 return isl_aff_free(aff
);
3371 ctx
= isl_aff_get_ctx(aff
);
3372 if (!isl_space_is_equal(aff
->ls
->dim
, subs
->ls
->dim
))
3373 isl_die(ctx
, isl_error_invalid
,
3374 "spaces don't match", return isl_aff_free(aff
));
3375 if (isl_local_space_dim(subs
->ls
, isl_dim_div
) != 0)
3376 isl_die(ctx
, isl_error_unsupported
,
3377 "cannot handle divs yet", return isl_aff_free(aff
));
3379 aff
->ls
= isl_local_space_substitute(aff
->ls
, type
, pos
, subs
);
3381 return isl_aff_free(aff
);
3383 aff
->v
= isl_vec_cow(aff
->v
);
3385 return isl_aff_free(aff
);
3387 pos
+= isl_local_space_offset(aff
->ls
, type
);
3390 isl_seq_substitute(aff
->v
->el
, pos
, subs
->v
->el
,
3391 aff
->v
->size
, subs
->v
->size
, v
);
3397 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3398 * expressions in "maff".
3400 __isl_give isl_multi_aff
*isl_multi_aff_substitute(
3401 __isl_take isl_multi_aff
*maff
, enum isl_dim_type type
, unsigned pos
,
3402 __isl_keep isl_aff
*subs
)
3406 maff
= isl_multi_aff_cow(maff
);
3408 return isl_multi_aff_free(maff
);
3410 if (type
== isl_dim_in
)
3413 for (i
= 0; i
< maff
->n
; ++i
) {
3414 maff
->p
[i
] = isl_aff_substitute(maff
->p
[i
], type
, pos
, subs
);
3416 return isl_multi_aff_free(maff
);
3422 /* Plug in "subs" for dimension "type", "pos" of "pma".
3424 * pma is of the form
3428 * while subs is of the form
3430 * v' = B_j(v) -> S_j
3432 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3433 * has a contribution in the result, in particular
3435 * C_ij(S_j) -> M_i(S_j)
3437 * Note that plugging in S_j in C_ij may also result in an empty set
3438 * and this contribution should simply be discarded.
3440 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_substitute(
3441 __isl_take isl_pw_multi_aff
*pma
, enum isl_dim_type type
, unsigned pos
,
3442 __isl_keep isl_pw_aff
*subs
)
3445 isl_pw_multi_aff
*res
;
3448 return isl_pw_multi_aff_free(pma
);
3450 n
= pma
->n
* subs
->n
;
3451 res
= isl_pw_multi_aff_alloc_size(isl_space_copy(pma
->dim
), n
);
3453 for (i
= 0; i
< pma
->n
; ++i
) {
3454 for (j
= 0; j
< subs
->n
; ++j
) {
3456 isl_multi_aff
*res_ij
;
3457 common
= isl_set_intersect(
3458 isl_set_copy(pma
->p
[i
].set
),
3459 isl_set_copy(subs
->p
[j
].set
));
3460 common
= isl_set_substitute(common
,
3461 type
, pos
, subs
->p
[j
].aff
);
3462 if (isl_set_plain_is_empty(common
)) {
3463 isl_set_free(common
);
3467 res_ij
= isl_multi_aff_substitute(
3468 isl_multi_aff_copy(pma
->p
[i
].maff
),
3469 type
, pos
, subs
->p
[j
].aff
);
3471 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
3475 isl_pw_multi_aff_free(pma
);
3479 /* Extend the local space of "dst" to include the divs
3480 * in the local space of "src".
3482 __isl_give isl_aff
*isl_aff_align_divs(__isl_take isl_aff
*dst
,
3483 __isl_keep isl_aff
*src
)
3491 return isl_aff_free(dst
);
3493 ctx
= isl_aff_get_ctx(src
);
3494 if (!isl_space_is_equal(src
->ls
->dim
, dst
->ls
->dim
))
3495 isl_die(ctx
, isl_error_invalid
,
3496 "spaces don't match", goto error
);
3498 if (src
->ls
->div
->n_row
== 0)
3501 exp1
= isl_alloc_array(ctx
, int, src
->ls
->div
->n_row
);
3502 exp2
= isl_alloc_array(ctx
, int, dst
->ls
->div
->n_row
);
3506 div
= isl_merge_divs(src
->ls
->div
, dst
->ls
->div
, exp1
, exp2
);
3507 dst
= isl_aff_expand_divs(dst
, div
, exp2
);
3515 return isl_aff_free(dst
);
3518 /* Adjust the local spaces of the affine expressions in "maff"
3519 * such that they all have the save divs.
3521 __isl_give isl_multi_aff
*isl_multi_aff_align_divs(
3522 __isl_take isl_multi_aff
*maff
)
3530 maff
= isl_multi_aff_cow(maff
);
3534 for (i
= 1; i
< maff
->n
; ++i
)
3535 maff
->p
[0] = isl_aff_align_divs(maff
->p
[0], maff
->p
[i
]);
3536 for (i
= 1; i
< maff
->n
; ++i
) {
3537 maff
->p
[i
] = isl_aff_align_divs(maff
->p
[i
], maff
->p
[0]);
3539 return isl_multi_aff_free(maff
);
3545 __isl_give isl_aff
*isl_aff_lift(__isl_take isl_aff
*aff
)
3547 aff
= isl_aff_cow(aff
);
3551 aff
->ls
= isl_local_space_lift(aff
->ls
);
3553 return isl_aff_free(aff
);
3558 /* Lift "maff" to a space with extra dimensions such that the result
3559 * has no more existentially quantified variables.
3560 * If "ls" is not NULL, then *ls is assigned the local space that lies
3561 * at the basis of the lifting applied to "maff".
3563 __isl_give isl_multi_aff
*isl_multi_aff_lift(__isl_take isl_multi_aff
*maff
,
3564 __isl_give isl_local_space
**ls
)
3578 isl_space
*space
= isl_multi_aff_get_domain_space(maff
);
3579 *ls
= isl_local_space_from_space(space
);
3581 return isl_multi_aff_free(maff
);
3586 maff
= isl_multi_aff_cow(maff
);
3587 maff
= isl_multi_aff_align_divs(maff
);
3591 n_div
= isl_aff_dim(maff
->p
[0], isl_dim_div
);
3592 space
= isl_multi_aff_get_space(maff
);
3593 space
= isl_space_lift(isl_space_domain(space
), n_div
);
3594 space
= isl_space_extend_domain_with_range(space
,
3595 isl_multi_aff_get_space(maff
));
3597 return isl_multi_aff_free(maff
);
3598 isl_space_free(maff
->space
);
3599 maff
->space
= space
;
3602 *ls
= isl_aff_get_domain_local_space(maff
->p
[0]);
3604 return isl_multi_aff_free(maff
);
3607 for (i
= 0; i
< maff
->n
; ++i
) {
3608 maff
->p
[i
] = isl_aff_lift(maff
->p
[i
]);
3616 isl_local_space_free(*ls
);
3617 return isl_multi_aff_free(maff
);
3621 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
3623 __isl_give isl_pw_aff
*isl_pw_multi_aff_get_pw_aff(
3624 __isl_keep isl_pw_multi_aff
*pma
, int pos
)
3634 n_out
= isl_pw_multi_aff_dim(pma
, isl_dim_out
);
3635 if (pos
< 0 || pos
>= n_out
)
3636 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3637 "index out of bounds", return NULL
);
3639 space
= isl_pw_multi_aff_get_space(pma
);
3640 space
= isl_space_drop_dims(space
, isl_dim_out
,
3641 pos
+ 1, n_out
- pos
- 1);
3642 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, pos
);
3644 pa
= isl_pw_aff_alloc_size(space
, pma
->n
);
3645 for (i
= 0; i
< pma
->n
; ++i
) {
3647 aff
= isl_multi_aff_get_aff(pma
->p
[i
].maff
, pos
);
3648 pa
= isl_pw_aff_add_piece(pa
, isl_set_copy(pma
->p
[i
].set
), aff
);
3654 /* Return an isl_pw_multi_aff with the given "set" as domain and
3655 * an unnamed zero-dimensional range.
3657 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_from_domain(
3658 __isl_take isl_set
*set
)
3663 space
= isl_set_get_space(set
);
3664 space
= isl_space_from_domain(space
);
3665 ma
= isl_multi_aff_zero(space
);
3666 return isl_pw_multi_aff_alloc(set
, ma
);
3669 /* Add an isl_pw_multi_aff with the given "set" as domain and
3670 * an unnamed zero-dimensional range to *user.
3672 static int add_pw_multi_aff_from_domain(__isl_take isl_set
*set
, void *user
)
3674 isl_union_pw_multi_aff
**upma
= user
;
3675 isl_pw_multi_aff
*pma
;
3677 pma
= isl_pw_multi_aff_from_domain(set
);
3678 *upma
= isl_union_pw_multi_aff_add_pw_multi_aff(*upma
, pma
);
3683 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
3684 * an unnamed zero-dimensional range.
3686 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_from_domain(
3687 __isl_take isl_union_set
*uset
)
3690 isl_union_pw_multi_aff
*upma
;
3695 space
= isl_union_set_get_space(uset
);
3696 upma
= isl_union_pw_multi_aff_empty(space
);
3698 if (isl_union_set_foreach_set(uset
,
3699 &add_pw_multi_aff_from_domain
, &upma
) < 0)
3702 isl_union_set_free(uset
);
3705 isl_union_set_free(uset
);
3706 isl_union_pw_multi_aff_free(upma
);
3710 /* Convert "pma" to an isl_map and add it to *umap.
3712 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff
*pma
, void *user
)
3714 isl_union_map
**umap
= user
;
3717 map
= isl_map_from_pw_multi_aff(pma
);
3718 *umap
= isl_union_map_add_map(*umap
, map
);
3723 /* Construct a union map mapping the domain of the union
3724 * piecewise multi-affine expression to its range, with each dimension
3725 * in the range equated to the corresponding affine expression on its cell.
3727 __isl_give isl_union_map
*isl_union_map_from_union_pw_multi_aff(
3728 __isl_take isl_union_pw_multi_aff
*upma
)
3731 isl_union_map
*umap
;
3736 space
= isl_union_pw_multi_aff_get_space(upma
);
3737 umap
= isl_union_map_empty(space
);
3739 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma
,
3740 &map_from_pw_multi_aff
, &umap
) < 0)
3743 isl_union_pw_multi_aff_free(upma
);
3746 isl_union_pw_multi_aff_free(upma
);
3747 isl_union_map_free(umap
);
3751 /* Local data for bin_entry and the callback "fn".
3753 struct isl_union_pw_multi_aff_bin_data
{
3754 isl_union_pw_multi_aff
*upma2
;
3755 isl_union_pw_multi_aff
*res
;
3756 isl_pw_multi_aff
*pma
;
3757 int (*fn
)(void **entry
, void *user
);
3760 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
3761 * and call data->fn for each isl_pw_multi_aff in data->upma2.
3763 static int bin_entry(void **entry
, void *user
)
3765 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3766 isl_pw_multi_aff
*pma
= *entry
;
3769 if (isl_hash_table_foreach(data
->upma2
->dim
->ctx
, &data
->upma2
->table
,
3770 data
->fn
, data
) < 0)
3776 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
3777 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
3778 * passed as user field) and the isl_pw_multi_aff from upma2 is available
3779 * as *entry. The callback should adjust data->res if desired.
3781 static __isl_give isl_union_pw_multi_aff
*bin_op(
3782 __isl_take isl_union_pw_multi_aff
*upma1
,
3783 __isl_take isl_union_pw_multi_aff
*upma2
,
3784 int (*fn
)(void **entry
, void *user
))
3787 struct isl_union_pw_multi_aff_bin_data data
= { NULL
, NULL
, NULL
, fn
};
3789 space
= isl_union_pw_multi_aff_get_space(upma2
);
3790 upma1
= isl_union_pw_multi_aff_align_params(upma1
, space
);
3791 space
= isl_union_pw_multi_aff_get_space(upma1
);
3792 upma2
= isl_union_pw_multi_aff_align_params(upma2
, space
);
3794 if (!upma1
|| !upma2
)
3798 data
.res
= isl_union_pw_multi_aff_alloc(isl_space_copy(upma1
->dim
),
3800 if (isl_hash_table_foreach(upma1
->dim
->ctx
, &upma1
->table
,
3801 &bin_entry
, &data
) < 0)
3804 isl_union_pw_multi_aff_free(upma1
);
3805 isl_union_pw_multi_aff_free(upma2
);
3808 isl_union_pw_multi_aff_free(upma1
);
3809 isl_union_pw_multi_aff_free(upma2
);
3810 isl_union_pw_multi_aff_free(data
.res
);
3814 /* Given two isl_multi_affs A -> B and C -> D,
3815 * construct an isl_multi_aff (A * C) -> (B, D).
3817 __isl_give isl_multi_aff
*isl_multi_aff_flat_range_product(
3818 __isl_take isl_multi_aff
*ma1
, __isl_take isl_multi_aff
*ma2
)
3828 space
= isl_space_range_product(isl_multi_aff_get_space(ma1
),
3829 isl_multi_aff_get_space(ma2
));
3830 space
= isl_space_flatten_range(space
);
3831 res
= isl_multi_aff_alloc(space
);
3833 n1
= isl_multi_aff_dim(ma1
, isl_dim_out
);
3834 n2
= isl_multi_aff_dim(ma2
, isl_dim_out
);
3836 for (i
= 0; i
< n1
; ++i
) {
3837 aff
= isl_multi_aff_get_aff(ma1
, i
);
3838 res
= isl_multi_aff_set_aff(res
, i
, aff
);
3841 for (i
= 0; i
< n2
; ++i
) {
3842 aff
= isl_multi_aff_get_aff(ma2
, i
);
3843 res
= isl_multi_aff_set_aff(res
, n1
+ i
, aff
);
3846 isl_multi_aff_free(ma1
);
3847 isl_multi_aff_free(ma2
);
3850 isl_multi_aff_free(ma1
);
3851 isl_multi_aff_free(ma2
);
3855 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
3856 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3858 static __isl_give isl_pw_multi_aff
*pw_multi_aff_flat_range_product(
3859 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3863 space
= isl_space_range_product(isl_pw_multi_aff_get_space(pma1
),
3864 isl_pw_multi_aff_get_space(pma2
));
3865 space
= isl_space_flatten_range(space
);
3866 return isl_pw_multi_aff_on_shared_domain_in(pma1
, pma2
, space
,
3867 &isl_multi_aff_flat_range_product
);
3870 /* Given two isl_pw_multi_affs A -> B and C -> D,
3871 * construct an isl_pw_multi_aff (A * C) -> (B, D).
3873 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_flat_range_product(
3874 __isl_take isl_pw_multi_aff
*pma1
, __isl_take isl_pw_multi_aff
*pma2
)
3876 return isl_pw_multi_aff_align_params_pw_pw_and(pma1
, pma2
,
3877 &pw_multi_aff_flat_range_product
);
3880 /* If data->pma and *entry have the same domain space, then compute
3881 * their flat range product and the result to data->res.
3883 static int flat_range_product_entry(void **entry
, void *user
)
3885 struct isl_union_pw_multi_aff_bin_data
*data
= user
;
3886 isl_pw_multi_aff
*pma2
= *entry
;
3888 if (!isl_space_tuple_match(data
->pma
->dim
, isl_dim_in
,
3889 pma2
->dim
, isl_dim_in
))
3892 pma2
= isl_pw_multi_aff_flat_range_product(
3893 isl_pw_multi_aff_copy(data
->pma
),
3894 isl_pw_multi_aff_copy(pma2
));
3896 data
->res
= isl_union_pw_multi_aff_add_pw_multi_aff(data
->res
, pma2
);
3901 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
3902 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
3904 __isl_give isl_union_pw_multi_aff
*isl_union_pw_multi_aff_flat_range_product(
3905 __isl_take isl_union_pw_multi_aff
*upma1
,
3906 __isl_take isl_union_pw_multi_aff
*upma2
)
3908 return bin_op(upma1
, upma2
, &flat_range_product_entry
);
3911 /* Replace the affine expressions at position "pos" in "pma" by "pa".
3912 * The parameters are assumed to have been aligned.
3914 * The implementation essentially performs an isl_pw_*_on_shared_domain,
3915 * except that it works on two different isl_pw_* types.
3917 static __isl_give isl_pw_multi_aff
*pw_multi_aff_set_pw_aff(
3918 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
3919 __isl_take isl_pw_aff
*pa
)
3922 isl_pw_multi_aff
*res
= NULL
;
3927 if (!isl_space_tuple_match(pma
->dim
, isl_dim_in
, pa
->dim
, isl_dim_in
))
3928 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3929 "domains don't match", goto error
);
3930 if (pos
>= isl_pw_multi_aff_dim(pma
, isl_dim_out
))
3931 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3932 "index out of bounds", goto error
);
3935 res
= isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma
), n
);
3937 for (i
= 0; i
< pma
->n
; ++i
) {
3938 for (j
= 0; j
< pa
->n
; ++j
) {
3940 isl_multi_aff
*res_ij
;
3943 common
= isl_set_intersect(isl_set_copy(pma
->p
[i
].set
),
3944 isl_set_copy(pa
->p
[j
].set
));
3945 empty
= isl_set_plain_is_empty(common
);
3946 if (empty
< 0 || empty
) {
3947 isl_set_free(common
);
3953 res_ij
= isl_multi_aff_set_aff(
3954 isl_multi_aff_copy(pma
->p
[i
].maff
), pos
,
3955 isl_aff_copy(pa
->p
[j
].aff
));
3956 res_ij
= isl_multi_aff_gist(res_ij
,
3957 isl_set_copy(common
));
3959 res
= isl_pw_multi_aff_add_piece(res
, common
, res_ij
);
3963 isl_pw_multi_aff_free(pma
);
3964 isl_pw_aff_free(pa
);
3967 isl_pw_multi_aff_free(pma
);
3968 isl_pw_aff_free(pa
);
3969 return isl_pw_multi_aff_free(res
);
3972 /* Replace the affine expressions at position "pos" in "pma" by "pa".
3974 __isl_give isl_pw_multi_aff
*isl_pw_multi_aff_set_pw_aff(
3975 __isl_take isl_pw_multi_aff
*pma
, unsigned pos
,
3976 __isl_take isl_pw_aff
*pa
)
3980 if (isl_space_match(pma
->dim
, isl_dim_param
, pa
->dim
, isl_dim_param
))
3981 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
3982 if (!isl_space_has_named_params(pma
->dim
) ||
3983 !isl_space_has_named_params(pa
->dim
))
3984 isl_die(isl_pw_multi_aff_get_ctx(pma
), isl_error_invalid
,
3985 "unaligned unnamed parameters", goto error
);
3986 pma
= isl_pw_multi_aff_align_params(pma
, isl_pw_aff_get_space(pa
));
3987 pa
= isl_pw_aff_align_params(pa
, isl_pw_multi_aff_get_space(pma
));
3988 return pw_multi_aff_set_pw_aff(pma
, pos
, pa
);
3990 isl_pw_multi_aff_free(pma
);
3991 isl_pw_aff_free(pa
);