isl_morph.c: extract out isl_mat_parameter_compression_ext
[isl.git] / isl_aff.c
blobb0a7f3311f9beca311c934141c2132e9644ee204
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2013 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,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #define ISL_DIM_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>
24 #include <isl/seq.h>
25 #include <isl/set.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)
31 isl_aff *aff;
33 if (!ls || !v)
34 goto error;
36 aff = isl_calloc_type(v->ctx, struct isl_aff);
37 if (!aff)
38 goto error;
40 aff->ref = 1;
41 aff->ls = ls;
42 aff->v = v;
44 return aff;
45 error:
46 isl_local_space_free(ls);
47 isl_vec_free(v);
48 return NULL;
51 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
53 isl_ctx *ctx;
54 isl_vec *v;
55 unsigned total;
57 if (!ls)
58 return NULL;
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",
63 goto error);
64 if (!isl_local_space_is_set(ls))
65 isl_die(ctx, isl_error_invalid,
66 "domain of affine expression should be a set",
67 goto error);
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);
72 error:
73 isl_local_space_free(ls);
74 return NULL;
77 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
79 isl_aff *aff;
81 aff = isl_aff_alloc(ls);
82 if (!aff)
83 return NULL;
85 isl_int_set_si(aff->v->el[0], 1);
86 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
88 return aff;
91 /* Return a piecewise affine expression defined on the specified domain
92 * that is equal to zero.
94 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
96 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
99 /* Return an affine expression that is equal to the specified dimension
100 * in "ls".
102 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
103 enum isl_dim_type type, unsigned pos)
105 isl_space *space;
106 isl_aff *aff;
108 if (!ls)
109 return NULL;
111 space = isl_local_space_get_space(ls);
112 if (!space)
113 goto error;
114 if (isl_space_is_map(space))
115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
116 "expecting (parameter) set space", goto error);
117 if (pos >= isl_local_space_dim(ls, type))
118 isl_die(isl_space_get_ctx(space), isl_error_invalid,
119 "position out of bounds", goto error);
121 isl_space_free(space);
122 aff = isl_aff_alloc(ls);
123 if (!aff)
124 return NULL;
126 pos += isl_local_space_offset(aff->ls, type);
128 isl_int_set_si(aff->v->el[0], 1);
129 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
130 isl_int_set_si(aff->v->el[1 + pos], 1);
132 return aff;
133 error:
134 isl_local_space_free(ls);
135 isl_space_free(space);
136 return NULL;
139 /* Return a piecewise affine expression that is equal to
140 * the specified dimension in "ls".
142 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
143 enum isl_dim_type type, unsigned pos)
145 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
148 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
150 if (!aff)
151 return NULL;
153 aff->ref++;
154 return aff;
157 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
159 if (!aff)
160 return NULL;
162 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
163 isl_vec_copy(aff->v));
166 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
168 if (!aff)
169 return NULL;
171 if (aff->ref == 1)
172 return aff;
173 aff->ref--;
174 return isl_aff_dup(aff);
177 void *isl_aff_free(__isl_take isl_aff *aff)
179 if (!aff)
180 return NULL;
182 if (--aff->ref > 0)
183 return NULL;
185 isl_local_space_free(aff->ls);
186 isl_vec_free(aff->v);
188 free(aff);
190 return NULL;
193 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
195 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
198 /* Externally, an isl_aff has a map space, but internally, the
199 * ls field corresponds to the domain of that space.
201 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
203 if (!aff)
204 return 0;
205 if (type == isl_dim_out)
206 return 1;
207 if (type == isl_dim_in)
208 type = isl_dim_set;
209 return isl_local_space_dim(aff->ls, type);
212 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
214 return aff ? isl_local_space_get_space(aff->ls) : NULL;
217 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
219 isl_space *space;
220 if (!aff)
221 return NULL;
222 space = isl_local_space_get_space(aff->ls);
223 space = isl_space_from_domain(space);
224 space = isl_space_add_dims(space, isl_dim_out, 1);
225 return space;
228 __isl_give isl_local_space *isl_aff_get_domain_local_space(
229 __isl_keep isl_aff *aff)
231 return aff ? isl_local_space_copy(aff->ls) : NULL;
234 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
236 isl_local_space *ls;
237 if (!aff)
238 return NULL;
239 ls = isl_local_space_copy(aff->ls);
240 ls = isl_local_space_from_domain(ls);
241 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
242 return ls;
245 /* Externally, an isl_aff has a map space, but internally, the
246 * ls field corresponds to the domain of that space.
248 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
249 enum isl_dim_type type, unsigned pos)
251 if (!aff)
252 return NULL;
253 if (type == isl_dim_out)
254 return NULL;
255 if (type == isl_dim_in)
256 type = isl_dim_set;
257 return isl_local_space_get_dim_name(aff->ls, type, pos);
260 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
261 __isl_take isl_space *dim)
263 aff = isl_aff_cow(aff);
264 if (!aff || !dim)
265 goto error;
267 aff->ls = isl_local_space_reset_space(aff->ls, dim);
268 if (!aff->ls)
269 return isl_aff_free(aff);
271 return aff;
272 error:
273 isl_aff_free(aff);
274 isl_space_free(dim);
275 return NULL;
278 /* Reset the space of "aff". This function is called from isl_pw_templ.c
279 * and doesn't know if the space of an element object is represented
280 * directly or through its domain. It therefore passes along both.
282 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
283 __isl_take isl_space *space, __isl_take isl_space *domain)
285 isl_space_free(space);
286 return isl_aff_reset_domain_space(aff, domain);
289 /* Reorder the coefficients of the affine expression based
290 * on the given reodering.
291 * The reordering r is assumed to have been extended with the local
292 * variables.
294 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
295 __isl_take isl_reordering *r, int n_div)
297 isl_vec *res;
298 int i;
300 if (!vec || !r)
301 goto error;
303 res = isl_vec_alloc(vec->ctx,
304 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
305 isl_seq_cpy(res->el, vec->el, 2);
306 isl_seq_clr(res->el + 2, res->size - 2);
307 for (i = 0; i < r->len; ++i)
308 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
310 isl_reordering_free(r);
311 isl_vec_free(vec);
312 return res;
313 error:
314 isl_vec_free(vec);
315 isl_reordering_free(r);
316 return NULL;
319 /* Reorder the dimensions of the domain of "aff" according
320 * to the given reordering.
322 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
323 __isl_take isl_reordering *r)
325 aff = isl_aff_cow(aff);
326 if (!aff)
327 goto error;
329 r = isl_reordering_extend(r, aff->ls->div->n_row);
330 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
331 aff->ls->div->n_row);
332 aff->ls = isl_local_space_realign(aff->ls, r);
334 if (!aff->v || !aff->ls)
335 return isl_aff_free(aff);
337 return aff;
338 error:
339 isl_aff_free(aff);
340 isl_reordering_free(r);
341 return NULL;
344 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
345 __isl_take isl_space *model)
347 if (!aff || !model)
348 goto error;
350 if (!isl_space_match(aff->ls->dim, isl_dim_param,
351 model, isl_dim_param)) {
352 isl_reordering *exp;
354 model = isl_space_drop_dims(model, isl_dim_in,
355 0, isl_space_dim(model, isl_dim_in));
356 model = isl_space_drop_dims(model, isl_dim_out,
357 0, isl_space_dim(model, isl_dim_out));
358 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
359 exp = isl_reordering_extend_space(exp,
360 isl_aff_get_domain_space(aff));
361 aff = isl_aff_realign_domain(aff, exp);
364 isl_space_free(model);
365 return aff;
366 error:
367 isl_space_free(model);
368 isl_aff_free(aff);
369 return NULL;
372 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
374 if (!aff)
375 return -1;
377 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
380 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
382 int equal;
384 if (!aff1 || !aff2)
385 return -1;
387 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
388 if (equal < 0 || !equal)
389 return equal;
391 return isl_vec_is_equal(aff1->v, aff2->v);
394 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
396 if (!aff)
397 return -1;
398 isl_int_set(*v, aff->v->el[0]);
399 return 0;
402 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
404 if (!aff)
405 return -1;
406 isl_int_set(*v, aff->v->el[1]);
407 return 0;
410 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
411 enum isl_dim_type type, int pos, isl_int *v)
413 if (!aff)
414 return -1;
416 if (type == isl_dim_out)
417 isl_die(aff->v->ctx, isl_error_invalid,
418 "output/set dimension does not have a coefficient",
419 return -1);
420 if (type == isl_dim_in)
421 type = isl_dim_set;
423 if (pos >= isl_local_space_dim(aff->ls, type))
424 isl_die(aff->v->ctx, isl_error_invalid,
425 "position out of bounds", return -1);
427 pos += isl_local_space_offset(aff->ls, type);
428 isl_int_set(*v, aff->v->el[1 + pos]);
430 return 0;
433 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
435 aff = isl_aff_cow(aff);
436 if (!aff)
437 return NULL;
439 aff->v = isl_vec_cow(aff->v);
440 if (!aff->v)
441 return isl_aff_free(aff);
443 isl_int_set(aff->v->el[0], v);
445 return aff;
448 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
450 aff = isl_aff_cow(aff);
451 if (!aff)
452 return NULL;
454 aff->v = isl_vec_cow(aff->v);
455 if (!aff->v)
456 return isl_aff_free(aff);
458 isl_int_set(aff->v->el[1], v);
460 return aff;
463 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
465 if (isl_int_is_zero(v))
466 return aff;
468 aff = isl_aff_cow(aff);
469 if (!aff)
470 return NULL;
472 aff->v = isl_vec_cow(aff->v);
473 if (!aff->v)
474 return isl_aff_free(aff);
476 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
478 return aff;
481 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
483 isl_int t;
485 isl_int_init(t);
486 isl_int_set_si(t, v);
487 aff = isl_aff_add_constant(aff, t);
488 isl_int_clear(t);
490 return aff;
493 /* Add "v" to the numerator of the constant term of "aff".
495 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
497 if (isl_int_is_zero(v))
498 return aff;
500 aff = isl_aff_cow(aff);
501 if (!aff)
502 return NULL;
504 aff->v = isl_vec_cow(aff->v);
505 if (!aff->v)
506 return isl_aff_free(aff);
508 isl_int_add(aff->v->el[1], aff->v->el[1], v);
510 return aff;
513 /* Add "v" to the numerator of the constant term of "aff".
515 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
517 isl_int t;
519 if (v == 0)
520 return aff;
522 isl_int_init(t);
523 isl_int_set_si(t, v);
524 aff = isl_aff_add_constant_num(aff, t);
525 isl_int_clear(t);
527 return aff;
530 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
532 aff = isl_aff_cow(aff);
533 if (!aff)
534 return NULL;
536 aff->v = isl_vec_cow(aff->v);
537 if (!aff->v)
538 return isl_aff_free(aff);
540 isl_int_set_si(aff->v->el[1], v);
542 return aff;
545 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
546 enum isl_dim_type type, int pos, isl_int v)
548 if (!aff)
549 return NULL;
551 if (type == isl_dim_out)
552 isl_die(aff->v->ctx, isl_error_invalid,
553 "output/set dimension does not have a coefficient",
554 return isl_aff_free(aff));
555 if (type == isl_dim_in)
556 type = isl_dim_set;
558 if (pos >= isl_local_space_dim(aff->ls, type))
559 isl_die(aff->v->ctx, isl_error_invalid,
560 "position out of bounds", return isl_aff_free(aff));
562 aff = isl_aff_cow(aff);
563 if (!aff)
564 return NULL;
566 aff->v = isl_vec_cow(aff->v);
567 if (!aff->v)
568 return isl_aff_free(aff);
570 pos += isl_local_space_offset(aff->ls, type);
571 isl_int_set(aff->v->el[1 + pos], v);
573 return aff;
576 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
577 enum isl_dim_type type, int pos, int v)
579 if (!aff)
580 return NULL;
582 if (type == isl_dim_out)
583 isl_die(aff->v->ctx, isl_error_invalid,
584 "output/set dimension does not have a coefficient",
585 return isl_aff_free(aff));
586 if (type == isl_dim_in)
587 type = isl_dim_set;
589 if (pos >= isl_local_space_dim(aff->ls, type))
590 isl_die(aff->v->ctx, isl_error_invalid,
591 "position out of bounds", return isl_aff_free(aff));
593 aff = isl_aff_cow(aff);
594 if (!aff)
595 return NULL;
597 aff->v = isl_vec_cow(aff->v);
598 if (!aff->v)
599 return isl_aff_free(aff);
601 pos += isl_local_space_offset(aff->ls, type);
602 isl_int_set_si(aff->v->el[1 + pos], v);
604 return aff;
607 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
608 enum isl_dim_type type, int pos, isl_int v)
610 if (!aff)
611 return NULL;
613 if (type == isl_dim_out)
614 isl_die(aff->v->ctx, isl_error_invalid,
615 "output/set dimension does not have a coefficient",
616 return isl_aff_free(aff));
617 if (type == isl_dim_in)
618 type = isl_dim_set;
620 if (pos >= isl_local_space_dim(aff->ls, type))
621 isl_die(aff->v->ctx, isl_error_invalid,
622 "position out of bounds", return isl_aff_free(aff));
624 aff = isl_aff_cow(aff);
625 if (!aff)
626 return NULL;
628 aff->v = isl_vec_cow(aff->v);
629 if (!aff->v)
630 return isl_aff_free(aff);
632 pos += isl_local_space_offset(aff->ls, type);
633 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
635 return aff;
638 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
639 enum isl_dim_type type, int pos, int v)
641 isl_int t;
643 isl_int_init(t);
644 isl_int_set_si(t, v);
645 aff = isl_aff_add_coefficient(aff, type, pos, t);
646 isl_int_clear(t);
648 return aff;
651 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
653 if (!aff)
654 return NULL;
656 return isl_local_space_get_div(aff->ls, pos);
659 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
661 aff = isl_aff_cow(aff);
662 if (!aff)
663 return NULL;
664 aff->v = isl_vec_cow(aff->v);
665 if (!aff->v)
666 return isl_aff_free(aff);
668 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
670 return aff;
673 /* Remove divs from the local space that do not appear in the affine
674 * expression.
675 * We currently only remove divs at the end.
676 * Some intermediate divs may also not appear directly in the affine
677 * expression, but we would also need to check that no other divs are
678 * defined in terms of them.
680 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
682 int pos;
683 int off;
684 int n;
686 if (!aff)
687 return NULL;
689 n = isl_local_space_dim(aff->ls, isl_dim_div);
690 off = isl_local_space_offset(aff->ls, isl_dim_div);
692 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
693 if (pos == n)
694 return aff;
696 aff = isl_aff_cow(aff);
697 if (!aff)
698 return NULL;
700 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
701 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
702 if (!aff->ls || !aff->v)
703 return isl_aff_free(aff);
705 return aff;
708 /* Given two affine expressions "p" of length p_len (including the
709 * denominator and the constant term) and "subs" of length subs_len,
710 * plug in "subs" for the variable at position "pos".
711 * The variables of "subs" and "p" are assumed to match up to subs_len,
712 * but "p" may have additional variables.
713 * "v" is an initialized isl_int that can be used internally.
715 * In particular, if "p" represents the expression
717 * (a i + g)/m
719 * with i the variable at position "pos" and "subs" represents the expression
721 * f/d
723 * then the result represents the expression
725 * (a f + d g)/(m d)
728 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
729 int p_len, int subs_len, isl_int v)
731 isl_int_set(v, p[1 + pos]);
732 isl_int_set_si(p[1 + pos], 0);
733 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
734 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
735 isl_int_mul(p[0], p[0], subs[0]);
738 /* Look for any divs in the aff->ls with a denominator equal to one
739 * and plug them into the affine expression and any subsequent divs
740 * that may reference the div.
742 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
744 int i, n;
745 int len;
746 isl_int v;
747 isl_vec *vec;
748 isl_local_space *ls;
749 unsigned pos;
751 if (!aff)
752 return NULL;
754 n = isl_local_space_dim(aff->ls, isl_dim_div);
755 len = aff->v->size;
756 for (i = 0; i < n; ++i) {
757 if (!isl_int_is_one(aff->ls->div->row[i][0]))
758 continue;
759 ls = isl_local_space_copy(aff->ls);
760 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
761 aff->ls->div->row[i], len, i + 1, n - (i + 1));
762 vec = isl_vec_copy(aff->v);
763 vec = isl_vec_cow(vec);
764 if (!ls || !vec)
765 goto error;
767 isl_int_init(v);
769 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
770 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
771 len, len, v);
773 isl_int_clear(v);
775 isl_vec_free(aff->v);
776 aff->v = vec;
777 isl_local_space_free(aff->ls);
778 aff->ls = ls;
781 return aff;
782 error:
783 isl_vec_free(vec);
784 isl_local_space_free(ls);
785 return isl_aff_free(aff);
788 /* Look for any divs j that appear with a unit coefficient inside
789 * the definitions of other divs i and plug them into the definitions
790 * of the divs i.
792 * In particular, an expression of the form
794 * floor((f(..) + floor(g(..)/n))/m)
796 * is simplified to
798 * floor((n * f(..) + g(..))/(n * m))
800 * This simplification is correct because we can move the expression
801 * f(..) into the inner floor in the original expression to obtain
803 * floor(floor((n * f(..) + g(..))/n)/m)
805 * from which we can derive the simplified expression.
807 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
809 int i, j, n;
810 int off;
812 if (!aff)
813 return NULL;
815 n = isl_local_space_dim(aff->ls, isl_dim_div);
816 off = isl_local_space_offset(aff->ls, isl_dim_div);
817 for (i = 1; i < n; ++i) {
818 for (j = 0; j < i; ++j) {
819 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
820 continue;
821 aff->ls = isl_local_space_substitute_seq(aff->ls,
822 isl_dim_div, j, aff->ls->div->row[j],
823 aff->v->size, i, 1);
824 if (!aff->ls)
825 return isl_aff_free(aff);
829 return aff;
832 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
834 * Even though this function is only called on isl_affs with a single
835 * reference, we are careful to only change aff->v and aff->ls together.
837 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
839 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
840 isl_local_space *ls;
841 isl_vec *v;
843 ls = isl_local_space_copy(aff->ls);
844 ls = isl_local_space_swap_div(ls, a, b);
845 v = isl_vec_copy(aff->v);
846 v = isl_vec_cow(v);
847 if (!ls || !v)
848 goto error;
850 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
851 isl_vec_free(aff->v);
852 aff->v = v;
853 isl_local_space_free(aff->ls);
854 aff->ls = ls;
856 return aff;
857 error:
858 isl_vec_free(v);
859 isl_local_space_free(ls);
860 return isl_aff_free(aff);
863 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
865 * We currently do not actually remove div "b", but simply add its
866 * coefficient to that of "a" and then zero it out.
868 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
870 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
872 if (isl_int_is_zero(aff->v->el[1 + off + b]))
873 return aff;
875 aff->v = isl_vec_cow(aff->v);
876 if (!aff->v)
877 return isl_aff_free(aff);
879 isl_int_add(aff->v->el[1 + off + a],
880 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
881 isl_int_set_si(aff->v->el[1 + off + b], 0);
883 return aff;
886 /* Sort the divs in the local space of "aff" according to
887 * the comparison function "cmp_row" in isl_local_space.c,
888 * combining the coefficients of identical divs.
890 * Reordering divs does not change the semantics of "aff",
891 * so there is no need to call isl_aff_cow.
892 * Moreover, this function is currently only called on isl_affs
893 * with a single reference.
895 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
897 int i, j, n;
898 unsigned off;
900 if (!aff)
901 return NULL;
903 off = isl_local_space_offset(aff->ls, isl_dim_div);
904 n = isl_aff_dim(aff, isl_dim_div);
905 for (i = 1; i < n; ++i) {
906 for (j = i - 1; j >= 0; --j) {
907 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
908 if (cmp < 0)
909 break;
910 if (cmp == 0)
911 aff = merge_divs(aff, j, j + 1);
912 else
913 aff = swap_div(aff, j, j + 1);
914 if (!aff)
915 return NULL;
919 return aff;
922 /* Normalize the representation of "aff".
924 * This function should only be called of "new" isl_affs, i.e.,
925 * with only a single reference. We therefore do not need to
926 * worry about affecting other instances.
928 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
930 if (!aff)
931 return NULL;
932 aff->v = isl_vec_normalize(aff->v);
933 if (!aff->v)
934 return isl_aff_free(aff);
935 aff = plug_in_integral_divs(aff);
936 aff = plug_in_unit_divs(aff);
937 aff = sort_divs(aff);
938 aff = isl_aff_remove_unused_divs(aff);
939 return aff;
942 /* Given f, return floor(f).
943 * If f is an integer expression, then just return f.
944 * If f is a constant, then return the constant floor(f).
945 * Otherwise, if f = g/m, write g = q m + r,
946 * create a new div d = [r/m] and return the expression q + d.
947 * The coefficients in r are taken to lie between -m/2 and m/2.
949 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
951 int i;
952 int size;
953 isl_ctx *ctx;
954 isl_vec *div;
956 if (!aff)
957 return NULL;
959 if (isl_int_is_one(aff->v->el[0]))
960 return aff;
962 aff = isl_aff_cow(aff);
963 if (!aff)
964 return NULL;
966 aff->v = isl_vec_cow(aff->v);
967 if (!aff->v)
968 return isl_aff_free(aff);
970 if (isl_aff_is_cst(aff)) {
971 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
972 isl_int_set_si(aff->v->el[0], 1);
973 return aff;
976 div = isl_vec_copy(aff->v);
977 div = isl_vec_cow(div);
978 if (!div)
979 return isl_aff_free(aff);
981 ctx = isl_aff_get_ctx(aff);
982 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
983 for (i = 1; i < aff->v->size; ++i) {
984 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
985 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
986 if (isl_int_gt(div->el[i], aff->v->el[0])) {
987 isl_int_sub(div->el[i], div->el[i], div->el[0]);
988 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
992 aff->ls = isl_local_space_add_div(aff->ls, div);
993 if (!aff->ls)
994 return isl_aff_free(aff);
996 size = aff->v->size;
997 aff->v = isl_vec_extend(aff->v, size + 1);
998 if (!aff->v)
999 return isl_aff_free(aff);
1000 isl_int_set_si(aff->v->el[0], 1);
1001 isl_int_set_si(aff->v->el[size], 1);
1003 aff = isl_aff_normalize(aff);
1005 return aff;
1008 /* Compute
1010 * aff mod m = aff - m * floor(aff/m)
1012 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1014 isl_aff *res;
1016 res = isl_aff_copy(aff);
1017 aff = isl_aff_scale_down(aff, m);
1018 aff = isl_aff_floor(aff);
1019 aff = isl_aff_scale(aff, m);
1020 res = isl_aff_sub(res, aff);
1022 return res;
1025 /* Compute
1027 * pwaff mod m = pwaff - m * floor(pwaff/m)
1029 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1031 isl_pw_aff *res;
1033 res = isl_pw_aff_copy(pwaff);
1034 pwaff = isl_pw_aff_scale_down(pwaff, m);
1035 pwaff = isl_pw_aff_floor(pwaff);
1036 pwaff = isl_pw_aff_scale(pwaff, m);
1037 res = isl_pw_aff_sub(res, pwaff);
1039 return res;
1042 /* Given f, return ceil(f).
1043 * If f is an integer expression, then just return f.
1044 * Otherwise, let f be the expression
1046 * e/m
1048 * then return
1050 * floor((e + m - 1)/m)
1052 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1054 if (!aff)
1055 return NULL;
1057 if (isl_int_is_one(aff->v->el[0]))
1058 return aff;
1060 aff = isl_aff_cow(aff);
1061 if (!aff)
1062 return NULL;
1063 aff->v = isl_vec_cow(aff->v);
1064 if (!aff->v)
1065 return isl_aff_free(aff);
1067 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1068 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1069 aff = isl_aff_floor(aff);
1071 return aff;
1074 /* Apply the expansion computed by isl_merge_divs.
1075 * The expansion itself is given by "exp" while the resulting
1076 * list of divs is given by "div".
1078 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1079 __isl_take isl_mat *div, int *exp)
1081 int i, j;
1082 int old_n_div;
1083 int new_n_div;
1084 int offset;
1086 aff = isl_aff_cow(aff);
1087 if (!aff || !div)
1088 goto error;
1090 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1091 new_n_div = isl_mat_rows(div);
1092 if (new_n_div < old_n_div)
1093 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1094 "not an expansion", goto error);
1096 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1097 if (!aff->v)
1098 goto error;
1100 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1101 j = old_n_div - 1;
1102 for (i = new_n_div - 1; i >= 0; --i) {
1103 if (j >= 0 && exp[j] == i) {
1104 if (i != j)
1105 isl_int_swap(aff->v->el[offset + i],
1106 aff->v->el[offset + j]);
1107 j--;
1108 } else
1109 isl_int_set_si(aff->v->el[offset + i], 0);
1112 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1113 if (!aff->ls)
1114 goto error;
1115 isl_mat_free(div);
1116 return aff;
1117 error:
1118 isl_aff_free(aff);
1119 isl_mat_free(div);
1120 return NULL;
1123 /* Add two affine expressions that live in the same local space.
1125 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1126 __isl_take isl_aff *aff2)
1128 isl_int gcd, f;
1130 aff1 = isl_aff_cow(aff1);
1131 if (!aff1 || !aff2)
1132 goto error;
1134 aff1->v = isl_vec_cow(aff1->v);
1135 if (!aff1->v)
1136 goto error;
1138 isl_int_init(gcd);
1139 isl_int_init(f);
1140 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1141 isl_int_divexact(f, aff2->v->el[0], gcd);
1142 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1143 isl_int_divexact(f, aff1->v->el[0], gcd);
1144 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1145 isl_int_divexact(f, aff2->v->el[0], gcd);
1146 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1147 isl_int_clear(f);
1148 isl_int_clear(gcd);
1150 isl_aff_free(aff2);
1151 return aff1;
1152 error:
1153 isl_aff_free(aff1);
1154 isl_aff_free(aff2);
1155 return NULL;
1158 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1159 __isl_take isl_aff *aff2)
1161 isl_ctx *ctx;
1162 int *exp1 = NULL;
1163 int *exp2 = NULL;
1164 isl_mat *div;
1166 if (!aff1 || !aff2)
1167 goto error;
1169 ctx = isl_aff_get_ctx(aff1);
1170 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1171 isl_die(ctx, isl_error_invalid,
1172 "spaces don't match", goto error);
1174 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1175 return add_expanded(aff1, aff2);
1177 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1178 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1179 if (!exp1 || !exp2)
1180 goto error;
1182 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1183 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1184 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1185 free(exp1);
1186 free(exp2);
1188 return add_expanded(aff1, aff2);
1189 error:
1190 free(exp1);
1191 free(exp2);
1192 isl_aff_free(aff1);
1193 isl_aff_free(aff2);
1194 return NULL;
1197 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1198 __isl_take isl_aff *aff2)
1200 return isl_aff_add(aff1, isl_aff_neg(aff2));
1203 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1205 isl_int gcd;
1207 if (isl_int_is_one(f))
1208 return aff;
1210 aff = isl_aff_cow(aff);
1211 if (!aff)
1212 return NULL;
1213 aff->v = isl_vec_cow(aff->v);
1214 if (!aff->v)
1215 return isl_aff_free(aff);
1217 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1218 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1219 return aff;
1222 isl_int_init(gcd);
1223 isl_int_gcd(gcd, aff->v->el[0], f);
1224 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1225 isl_int_divexact(gcd, f, gcd);
1226 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1227 isl_int_clear(gcd);
1229 return aff;
1232 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1234 isl_int gcd;
1236 if (isl_int_is_one(f))
1237 return aff;
1239 aff = isl_aff_cow(aff);
1240 if (!aff)
1241 return NULL;
1243 if (isl_int_is_zero(f))
1244 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1245 "cannot scale down by zero", return isl_aff_free(aff));
1247 aff->v = isl_vec_cow(aff->v);
1248 if (!aff->v)
1249 return isl_aff_free(aff);
1251 isl_int_init(gcd);
1252 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1253 isl_int_gcd(gcd, gcd, f);
1254 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1255 isl_int_divexact(gcd, f, gcd);
1256 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1257 isl_int_clear(gcd);
1259 return aff;
1262 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1264 isl_int v;
1266 if (f == 1)
1267 return aff;
1269 isl_int_init(v);
1270 isl_int_set_ui(v, f);
1271 aff = isl_aff_scale_down(aff, v);
1272 isl_int_clear(v);
1274 return aff;
1277 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1278 enum isl_dim_type type, unsigned pos, const char *s)
1280 aff = isl_aff_cow(aff);
1281 if (!aff)
1282 return NULL;
1283 if (type == isl_dim_out)
1284 isl_die(aff->v->ctx, isl_error_invalid,
1285 "cannot set name of output/set dimension",
1286 return isl_aff_free(aff));
1287 if (type == isl_dim_in)
1288 type = isl_dim_set;
1289 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1290 if (!aff->ls)
1291 return isl_aff_free(aff);
1293 return aff;
1296 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1297 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1299 aff = isl_aff_cow(aff);
1300 if (!aff)
1301 return isl_id_free(id);
1302 if (type == isl_dim_out)
1303 isl_die(aff->v->ctx, isl_error_invalid,
1304 "cannot set name of output/set dimension",
1305 goto error);
1306 if (type == isl_dim_in)
1307 type = isl_dim_set;
1308 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1309 if (!aff->ls)
1310 return isl_aff_free(aff);
1312 return aff;
1313 error:
1314 isl_id_free(id);
1315 isl_aff_free(aff);
1316 return NULL;
1319 /* Exploit the equalities in "eq" to simplify the affine expression
1320 * and the expressions of the integer divisions in the local space.
1321 * The integer divisions in this local space are assumed to appear
1322 * as regular dimensions in "eq".
1324 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1325 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1327 int i, j;
1328 unsigned total;
1329 unsigned n_div;
1331 if (!eq)
1332 goto error;
1333 if (eq->n_eq == 0) {
1334 isl_basic_set_free(eq);
1335 return aff;
1338 aff = isl_aff_cow(aff);
1339 if (!aff)
1340 goto error;
1342 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1343 isl_basic_set_copy(eq));
1344 aff->v = isl_vec_cow(aff->v);
1345 if (!aff->ls || !aff->v)
1346 goto error;
1348 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1349 n_div = eq->n_div;
1350 for (i = 0; i < eq->n_eq; ++i) {
1351 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1352 if (j < 0 || j == 0 || j >= total)
1353 continue;
1355 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1356 &aff->v->el[0]);
1359 isl_basic_set_free(eq);
1360 aff = isl_aff_normalize(aff);
1361 return aff;
1362 error:
1363 isl_basic_set_free(eq);
1364 isl_aff_free(aff);
1365 return NULL;
1368 /* Exploit the equalities in "eq" to simplify the affine expression
1369 * and the expressions of the integer divisions in the local space.
1371 static __isl_give isl_aff *isl_aff_substitute_equalities(
1372 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1374 int n_div;
1376 if (!aff || !eq)
1377 goto error;
1378 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1379 if (n_div > 0)
1380 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1381 return isl_aff_substitute_equalities_lifted(aff, eq);
1382 error:
1383 isl_basic_set_free(eq);
1384 isl_aff_free(aff);
1385 return NULL;
1388 /* Look for equalities among the variables shared by context and aff
1389 * and the integer divisions of aff, if any.
1390 * The equalities are then used to eliminate coefficients and/or integer
1391 * divisions from aff.
1393 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1394 __isl_take isl_set *context)
1396 isl_basic_set *hull;
1397 int n_div;
1399 if (!aff)
1400 goto error;
1401 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1402 if (n_div > 0) {
1403 isl_basic_set *bset;
1404 isl_local_space *ls;
1405 context = isl_set_add_dims(context, isl_dim_set, n_div);
1406 ls = isl_aff_get_domain_local_space(aff);
1407 bset = isl_basic_set_from_local_space(ls);
1408 bset = isl_basic_set_lift(bset);
1409 bset = isl_basic_set_flatten(bset);
1410 context = isl_set_intersect(context,
1411 isl_set_from_basic_set(bset));
1414 hull = isl_set_affine_hull(context);
1415 return isl_aff_substitute_equalities_lifted(aff, hull);
1416 error:
1417 isl_aff_free(aff);
1418 isl_set_free(context);
1419 return NULL;
1422 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1423 __isl_take isl_set *context)
1425 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1426 dom_context = isl_set_intersect_params(dom_context, context);
1427 return isl_aff_gist(aff, dom_context);
1430 /* Return a basic set containing those elements in the space
1431 * of aff where it is non-negative.
1432 * If "rational" is set, then return a rational basic set.
1434 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1435 __isl_take isl_aff *aff, int rational)
1437 isl_constraint *ineq;
1438 isl_basic_set *bset;
1440 ineq = isl_inequality_from_aff(aff);
1442 bset = isl_basic_set_from_constraint(ineq);
1443 if (rational)
1444 bset = isl_basic_set_set_rational(bset);
1445 bset = isl_basic_set_simplify(bset);
1446 return bset;
1449 /* Return a basic set containing those elements in the space
1450 * of aff where it is non-negative.
1452 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1454 return aff_nonneg_basic_set(aff, 0);
1457 /* Return a basic set containing those elements in the domain space
1458 * of aff where it is negative.
1460 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1462 aff = isl_aff_neg(aff);
1463 aff = isl_aff_add_constant_num_si(aff, -1);
1464 return isl_aff_nonneg_basic_set(aff);
1467 /* Return a basic set containing those elements in the space
1468 * of aff where it is zero.
1469 * If "rational" is set, then return a rational basic set.
1471 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1472 int rational)
1474 isl_constraint *ineq;
1475 isl_basic_set *bset;
1477 ineq = isl_equality_from_aff(aff);
1479 bset = isl_basic_set_from_constraint(ineq);
1480 if (rational)
1481 bset = isl_basic_set_set_rational(bset);
1482 bset = isl_basic_set_simplify(bset);
1483 return bset;
1486 /* Return a basic set containing those elements in the space
1487 * of aff where it is zero.
1489 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1491 return aff_zero_basic_set(aff, 0);
1494 /* Return a basic set containing those elements in the shared space
1495 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1497 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1498 __isl_take isl_aff *aff2)
1500 aff1 = isl_aff_sub(aff1, aff2);
1502 return isl_aff_nonneg_basic_set(aff1);
1505 /* Return a basic set containing those elements in the shared space
1506 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1508 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1509 __isl_take isl_aff *aff2)
1511 return isl_aff_ge_basic_set(aff2, aff1);
1514 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1515 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1517 aff1 = isl_aff_add(aff1, aff2);
1518 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1519 return aff1;
1522 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1524 if (!aff)
1525 return -1;
1527 return 0;
1530 /* Check whether the given affine expression has non-zero coefficient
1531 * for any dimension in the given range or if any of these dimensions
1532 * appear with non-zero coefficients in any of the integer divisions
1533 * involved in the affine expression.
1535 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1536 enum isl_dim_type type, unsigned first, unsigned n)
1538 int i;
1539 isl_ctx *ctx;
1540 int *active = NULL;
1541 int involves = 0;
1543 if (!aff)
1544 return -1;
1545 if (n == 0)
1546 return 0;
1548 ctx = isl_aff_get_ctx(aff);
1549 if (first + n > isl_aff_dim(aff, type))
1550 isl_die(ctx, isl_error_invalid,
1551 "range out of bounds", return -1);
1553 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1554 if (!active)
1555 goto error;
1557 first += isl_local_space_offset(aff->ls, type) - 1;
1558 for (i = 0; i < n; ++i)
1559 if (active[first + i]) {
1560 involves = 1;
1561 break;
1564 free(active);
1566 return involves;
1567 error:
1568 free(active);
1569 return -1;
1572 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1573 enum isl_dim_type type, unsigned first, unsigned n)
1575 isl_ctx *ctx;
1577 if (!aff)
1578 return NULL;
1579 if (type == isl_dim_out)
1580 isl_die(aff->v->ctx, isl_error_invalid,
1581 "cannot drop output/set dimension",
1582 return isl_aff_free(aff));
1583 if (type == isl_dim_in)
1584 type = isl_dim_set;
1585 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1586 return aff;
1588 ctx = isl_aff_get_ctx(aff);
1589 if (first + n > isl_local_space_dim(aff->ls, type))
1590 isl_die(ctx, isl_error_invalid, "range out of bounds",
1591 return isl_aff_free(aff));
1593 aff = isl_aff_cow(aff);
1594 if (!aff)
1595 return NULL;
1597 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1598 if (!aff->ls)
1599 return isl_aff_free(aff);
1601 first += 1 + isl_local_space_offset(aff->ls, type);
1602 aff->v = isl_vec_drop_els(aff->v, first, n);
1603 if (!aff->v)
1604 return isl_aff_free(aff);
1606 return aff;
1609 /* Project the domain of the affine expression onto its parameter space.
1610 * The affine expression may not involve any of the domain dimensions.
1612 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1614 isl_space *space;
1615 unsigned n;
1616 int involves;
1618 n = isl_aff_dim(aff, isl_dim_in);
1619 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1620 if (involves < 0)
1621 return isl_aff_free(aff);
1622 if (involves)
1623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1624 "affine expression involves some of the domain dimensions",
1625 return isl_aff_free(aff));
1626 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1627 space = isl_aff_get_domain_space(aff);
1628 space = isl_space_params(space);
1629 aff = isl_aff_reset_domain_space(aff, space);
1630 return aff;
1633 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1634 enum isl_dim_type type, unsigned first, unsigned n)
1636 isl_ctx *ctx;
1638 if (!aff)
1639 return NULL;
1640 if (type == isl_dim_out)
1641 isl_die(aff->v->ctx, isl_error_invalid,
1642 "cannot insert output/set dimensions",
1643 return isl_aff_free(aff));
1644 if (type == isl_dim_in)
1645 type = isl_dim_set;
1646 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1647 return aff;
1649 ctx = isl_aff_get_ctx(aff);
1650 if (first > isl_local_space_dim(aff->ls, type))
1651 isl_die(ctx, isl_error_invalid, "position out of bounds",
1652 return isl_aff_free(aff));
1654 aff = isl_aff_cow(aff);
1655 if (!aff)
1656 return NULL;
1658 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1659 if (!aff->ls)
1660 return isl_aff_free(aff);
1662 first += 1 + isl_local_space_offset(aff->ls, type);
1663 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1664 if (!aff->v)
1665 return isl_aff_free(aff);
1667 return aff;
1670 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1671 enum isl_dim_type type, unsigned n)
1673 unsigned pos;
1675 pos = isl_aff_dim(aff, type);
1677 return isl_aff_insert_dims(aff, type, pos, n);
1680 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1681 enum isl_dim_type type, unsigned n)
1683 unsigned pos;
1685 pos = isl_pw_aff_dim(pwaff, type);
1687 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1690 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1692 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1693 return isl_pw_aff_alloc(dom, aff);
1696 #undef PW
1697 #define PW isl_pw_aff
1698 #undef EL
1699 #define EL isl_aff
1700 #undef EL_IS_ZERO
1701 #define EL_IS_ZERO is_empty
1702 #undef ZERO
1703 #define ZERO empty
1704 #undef IS_ZERO
1705 #define IS_ZERO is_empty
1706 #undef FIELD
1707 #define FIELD aff
1708 #undef DEFAULT_IS_ZERO
1709 #define DEFAULT_IS_ZERO 0
1711 #define NO_EVAL
1712 #define NO_OPT
1713 #define NO_MOVE_DIMS
1714 #define NO_LIFT
1715 #define NO_MORPH
1717 #include <isl_pw_templ.c>
1719 static __isl_give isl_set *align_params_pw_pw_set_and(
1720 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1721 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1722 __isl_take isl_pw_aff *pwaff2))
1724 if (!pwaff1 || !pwaff2)
1725 goto error;
1726 if (isl_space_match(pwaff1->dim, isl_dim_param,
1727 pwaff2->dim, isl_dim_param))
1728 return fn(pwaff1, pwaff2);
1729 if (!isl_space_has_named_params(pwaff1->dim) ||
1730 !isl_space_has_named_params(pwaff2->dim))
1731 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1732 "unaligned unnamed parameters", goto error);
1733 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1734 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1735 return fn(pwaff1, pwaff2);
1736 error:
1737 isl_pw_aff_free(pwaff1);
1738 isl_pw_aff_free(pwaff2);
1739 return NULL;
1742 /* Compute a piecewise quasi-affine expression with a domain that
1743 * is the union of those of pwaff1 and pwaff2 and such that on each
1744 * cell, the quasi-affine expression is the better (according to cmp)
1745 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1746 * is defined on a given cell, then the associated expression
1747 * is the defined one.
1749 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1750 __isl_take isl_pw_aff *pwaff2,
1751 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1752 __isl_take isl_aff *aff2))
1754 int i, j, n;
1755 isl_pw_aff *res;
1756 isl_ctx *ctx;
1757 isl_set *set;
1759 if (!pwaff1 || !pwaff2)
1760 goto error;
1762 ctx = isl_space_get_ctx(pwaff1->dim);
1763 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1764 isl_die(ctx, isl_error_invalid,
1765 "arguments should live in same space", goto error);
1767 if (isl_pw_aff_is_empty(pwaff1)) {
1768 isl_pw_aff_free(pwaff1);
1769 return pwaff2;
1772 if (isl_pw_aff_is_empty(pwaff2)) {
1773 isl_pw_aff_free(pwaff2);
1774 return pwaff1;
1777 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1778 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1780 for (i = 0; i < pwaff1->n; ++i) {
1781 set = isl_set_copy(pwaff1->p[i].set);
1782 for (j = 0; j < pwaff2->n; ++j) {
1783 struct isl_set *common;
1784 isl_set *better;
1786 common = isl_set_intersect(
1787 isl_set_copy(pwaff1->p[i].set),
1788 isl_set_copy(pwaff2->p[j].set));
1789 better = isl_set_from_basic_set(cmp(
1790 isl_aff_copy(pwaff2->p[j].aff),
1791 isl_aff_copy(pwaff1->p[i].aff)));
1792 better = isl_set_intersect(common, better);
1793 if (isl_set_plain_is_empty(better)) {
1794 isl_set_free(better);
1795 continue;
1797 set = isl_set_subtract(set, isl_set_copy(better));
1799 res = isl_pw_aff_add_piece(res, better,
1800 isl_aff_copy(pwaff2->p[j].aff));
1802 res = isl_pw_aff_add_piece(res, set,
1803 isl_aff_copy(pwaff1->p[i].aff));
1806 for (j = 0; j < pwaff2->n; ++j) {
1807 set = isl_set_copy(pwaff2->p[j].set);
1808 for (i = 0; i < pwaff1->n; ++i)
1809 set = isl_set_subtract(set,
1810 isl_set_copy(pwaff1->p[i].set));
1811 res = isl_pw_aff_add_piece(res, set,
1812 isl_aff_copy(pwaff2->p[j].aff));
1815 isl_pw_aff_free(pwaff1);
1816 isl_pw_aff_free(pwaff2);
1818 return res;
1819 error:
1820 isl_pw_aff_free(pwaff1);
1821 isl_pw_aff_free(pwaff2);
1822 return NULL;
1825 /* Compute a piecewise quasi-affine expression with a domain that
1826 * is the union of those of pwaff1 and pwaff2 and such that on each
1827 * cell, the quasi-affine expression is the maximum of those of pwaff1
1828 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1829 * cell, then the associated expression is the defined one.
1831 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1832 __isl_take isl_pw_aff *pwaff2)
1834 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1837 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1838 __isl_take isl_pw_aff *pwaff2)
1840 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1841 &pw_aff_union_max);
1844 /* Compute a piecewise quasi-affine expression with a domain that
1845 * is the union of those of pwaff1 and pwaff2 and such that on each
1846 * cell, the quasi-affine expression is the minimum of those of pwaff1
1847 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1848 * cell, then the associated expression is the defined one.
1850 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1851 __isl_take isl_pw_aff *pwaff2)
1853 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1856 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1857 __isl_take isl_pw_aff *pwaff2)
1859 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1860 &pw_aff_union_min);
1863 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1864 __isl_take isl_pw_aff *pwaff2, int max)
1866 if (max)
1867 return isl_pw_aff_union_max(pwaff1, pwaff2);
1868 else
1869 return isl_pw_aff_union_min(pwaff1, pwaff2);
1872 /* Construct a map with as domain the domain of pwaff and
1873 * one-dimensional range corresponding to the affine expressions.
1875 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1877 int i;
1878 isl_space *dim;
1879 isl_map *map;
1881 if (!pwaff)
1882 return NULL;
1884 dim = isl_pw_aff_get_space(pwaff);
1885 map = isl_map_empty(dim);
1887 for (i = 0; i < pwaff->n; ++i) {
1888 isl_basic_map *bmap;
1889 isl_map *map_i;
1891 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1892 map_i = isl_map_from_basic_map(bmap);
1893 map_i = isl_map_intersect_domain(map_i,
1894 isl_set_copy(pwaff->p[i].set));
1895 map = isl_map_union_disjoint(map, map_i);
1898 isl_pw_aff_free(pwaff);
1900 return map;
1903 /* Construct a map with as domain the domain of pwaff and
1904 * one-dimensional range corresponding to the affine expressions.
1906 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1908 if (!pwaff)
1909 return NULL;
1910 if (isl_space_is_set(pwaff->dim))
1911 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1912 "space of input is not a map",
1913 return isl_pw_aff_free(pwaff));
1914 return map_from_pw_aff(pwaff);
1917 /* Construct a one-dimensional set with as parameter domain
1918 * the domain of pwaff and the single set dimension
1919 * corresponding to the affine expressions.
1921 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1923 if (!pwaff)
1924 return NULL;
1925 if (!isl_space_is_set(pwaff->dim))
1926 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1927 "space of input is not a set",
1928 return isl_pw_aff_free(pwaff));
1929 return map_from_pw_aff(pwaff);
1932 /* Return a set containing those elements in the domain
1933 * of pwaff where it is non-negative.
1935 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1937 int i;
1938 isl_set *set;
1940 if (!pwaff)
1941 return NULL;
1943 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1945 for (i = 0; i < pwaff->n; ++i) {
1946 isl_basic_set *bset;
1947 isl_set *set_i;
1948 int rational;
1950 rational = isl_set_has_rational(pwaff->p[i].set);
1951 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
1952 rational);
1953 set_i = isl_set_from_basic_set(bset);
1954 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1955 set = isl_set_union_disjoint(set, set_i);
1958 isl_pw_aff_free(pwaff);
1960 return set;
1963 /* Return a set containing those elements in the domain
1964 * of pwaff where it is zero (if complement is 0) or not zero
1965 * (if complement is 1).
1967 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1968 int complement)
1970 int i;
1971 isl_set *set;
1973 if (!pwaff)
1974 return NULL;
1976 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1978 for (i = 0; i < pwaff->n; ++i) {
1979 isl_basic_set *bset;
1980 isl_set *set_i, *zero;
1981 int rational;
1983 rational = isl_set_has_rational(pwaff->p[i].set);
1984 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
1985 rational);
1986 zero = isl_set_from_basic_set(bset);
1987 set_i = isl_set_copy(pwaff->p[i].set);
1988 if (complement)
1989 set_i = isl_set_subtract(set_i, zero);
1990 else
1991 set_i = isl_set_intersect(set_i, zero);
1992 set = isl_set_union_disjoint(set, set_i);
1995 isl_pw_aff_free(pwaff);
1997 return set;
2000 /* Return a set containing those elements in the domain
2001 * of pwaff where it is zero.
2003 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2005 return pw_aff_zero_set(pwaff, 0);
2008 /* Return a set containing those elements in the domain
2009 * of pwaff where it is not zero.
2011 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2013 return pw_aff_zero_set(pwaff, 1);
2016 /* Return a set containing those elements in the shared domain
2017 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2019 * We compute the difference on the shared domain and then construct
2020 * the set of values where this difference is non-negative.
2021 * If strict is set, we first subtract 1 from the difference.
2022 * If equal is set, we only return the elements where pwaff1 and pwaff2
2023 * are equal.
2025 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2026 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2028 isl_set *set1, *set2;
2030 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2031 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2032 set1 = isl_set_intersect(set1, set2);
2033 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2034 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2035 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2037 if (strict) {
2038 isl_space *dim = isl_set_get_space(set1);
2039 isl_aff *aff;
2040 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2041 aff = isl_aff_add_constant_si(aff, -1);
2042 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2043 } else
2044 isl_set_free(set1);
2046 if (equal)
2047 return isl_pw_aff_zero_set(pwaff1);
2048 return isl_pw_aff_nonneg_set(pwaff1);
2051 /* Return a set containing those elements in the shared domain
2052 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2054 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2055 __isl_take isl_pw_aff *pwaff2)
2057 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2060 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2061 __isl_take isl_pw_aff *pwaff2)
2063 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2066 /* Return a set containing those elements in the shared domain
2067 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2069 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2070 __isl_take isl_pw_aff *pwaff2)
2072 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2075 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2076 __isl_take isl_pw_aff *pwaff2)
2078 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2081 /* Return a set containing those elements in the shared domain
2082 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2084 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2085 __isl_take isl_pw_aff *pwaff2)
2087 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2090 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2091 __isl_take isl_pw_aff *pwaff2)
2093 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2096 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2097 __isl_take isl_pw_aff *pwaff2)
2099 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2102 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2103 __isl_take isl_pw_aff *pwaff2)
2105 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2108 /* Return a set containing those elements in the shared domain
2109 * of the elements of list1 and list2 where each element in list1
2110 * has the relation specified by "fn" with each element in list2.
2112 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2113 __isl_take isl_pw_aff_list *list2,
2114 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2115 __isl_take isl_pw_aff *pwaff2))
2117 int i, j;
2118 isl_ctx *ctx;
2119 isl_set *set;
2121 if (!list1 || !list2)
2122 goto error;
2124 ctx = isl_pw_aff_list_get_ctx(list1);
2125 if (list1->n < 1 || list2->n < 1)
2126 isl_die(ctx, isl_error_invalid,
2127 "list should contain at least one element", goto error);
2129 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2130 for (i = 0; i < list1->n; ++i)
2131 for (j = 0; j < list2->n; ++j) {
2132 isl_set *set_ij;
2134 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2135 isl_pw_aff_copy(list2->p[j]));
2136 set = isl_set_intersect(set, set_ij);
2139 isl_pw_aff_list_free(list1);
2140 isl_pw_aff_list_free(list2);
2141 return set;
2142 error:
2143 isl_pw_aff_list_free(list1);
2144 isl_pw_aff_list_free(list2);
2145 return NULL;
2148 /* Return a set containing those elements in the shared domain
2149 * of the elements of list1 and list2 where each element in list1
2150 * is equal to each element in list2.
2152 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2153 __isl_take isl_pw_aff_list *list2)
2155 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2158 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2159 __isl_take isl_pw_aff_list *list2)
2161 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2164 /* Return a set containing those elements in the shared domain
2165 * of the elements of list1 and list2 where each element in list1
2166 * is less than or equal to each element in list2.
2168 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2169 __isl_take isl_pw_aff_list *list2)
2171 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2174 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2175 __isl_take isl_pw_aff_list *list2)
2177 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2180 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2181 __isl_take isl_pw_aff_list *list2)
2183 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2186 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2187 __isl_take isl_pw_aff_list *list2)
2189 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2193 /* Return a set containing those elements in the shared domain
2194 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2196 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2197 __isl_take isl_pw_aff *pwaff2)
2199 isl_set *set_lt, *set_gt;
2201 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2202 isl_pw_aff_copy(pwaff2));
2203 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2204 return isl_set_union_disjoint(set_lt, set_gt);
2207 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2208 __isl_take isl_pw_aff *pwaff2)
2210 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2213 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2214 isl_int v)
2216 int i;
2218 if (isl_int_is_one(v))
2219 return pwaff;
2220 if (!isl_int_is_pos(v))
2221 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2222 "factor needs to be positive",
2223 return isl_pw_aff_free(pwaff));
2224 pwaff = isl_pw_aff_cow(pwaff);
2225 if (!pwaff)
2226 return NULL;
2227 if (pwaff->n == 0)
2228 return pwaff;
2230 for (i = 0; i < pwaff->n; ++i) {
2231 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2232 if (!pwaff->p[i].aff)
2233 return isl_pw_aff_free(pwaff);
2236 return pwaff;
2239 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2241 int i;
2243 pwaff = isl_pw_aff_cow(pwaff);
2244 if (!pwaff)
2245 return NULL;
2246 if (pwaff->n == 0)
2247 return pwaff;
2249 for (i = 0; i < pwaff->n; ++i) {
2250 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2251 if (!pwaff->p[i].aff)
2252 return isl_pw_aff_free(pwaff);
2255 return pwaff;
2258 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2260 int i;
2262 pwaff = isl_pw_aff_cow(pwaff);
2263 if (!pwaff)
2264 return NULL;
2265 if (pwaff->n == 0)
2266 return pwaff;
2268 for (i = 0; i < pwaff->n; ++i) {
2269 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2270 if (!pwaff->p[i].aff)
2271 return isl_pw_aff_free(pwaff);
2274 return pwaff;
2277 /* Assuming that "cond1" and "cond2" are disjoint,
2278 * return an affine expression that is equal to pwaff1 on cond1
2279 * and to pwaff2 on cond2.
2281 static __isl_give isl_pw_aff *isl_pw_aff_select(
2282 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2283 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2285 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2286 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2288 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2291 /* Return an affine expression that is equal to pwaff_true for elements
2292 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2293 * is zero.
2294 * That is, return cond ? pwaff_true : pwaff_false;
2296 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2297 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2299 isl_set *cond_true, *cond_false;
2301 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2302 cond_false = isl_pw_aff_zero_set(cond);
2303 return isl_pw_aff_select(cond_true, pwaff_true,
2304 cond_false, pwaff_false);
2307 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2309 if (!aff)
2310 return -1;
2312 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2315 /* Check whether pwaff is a piecewise constant.
2317 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2319 int i;
2321 if (!pwaff)
2322 return -1;
2324 for (i = 0; i < pwaff->n; ++i) {
2325 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2326 if (is_cst < 0 || !is_cst)
2327 return is_cst;
2330 return 1;
2333 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2334 __isl_take isl_aff *aff2)
2336 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2337 return isl_aff_mul(aff2, aff1);
2339 if (!isl_aff_is_cst(aff2))
2340 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2341 "at least one affine expression should be constant",
2342 goto error);
2344 aff1 = isl_aff_cow(aff1);
2345 if (!aff1 || !aff2)
2346 goto error;
2348 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2349 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2351 isl_aff_free(aff2);
2352 return aff1;
2353 error:
2354 isl_aff_free(aff1);
2355 isl_aff_free(aff2);
2356 return NULL;
2359 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2361 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2362 __isl_take isl_aff *aff2)
2364 int is_cst;
2365 int neg;
2367 is_cst = isl_aff_is_cst(aff2);
2368 if (is_cst < 0)
2369 goto error;
2370 if (!is_cst)
2371 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2372 "second argument should be a constant", goto error);
2374 if (!aff2)
2375 goto error;
2377 neg = isl_int_is_neg(aff2->v->el[1]);
2378 if (neg) {
2379 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2380 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2383 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2384 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2386 if (neg) {
2387 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2388 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2391 isl_aff_free(aff2);
2392 return aff1;
2393 error:
2394 isl_aff_free(aff1);
2395 isl_aff_free(aff2);
2396 return NULL;
2399 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2400 __isl_take isl_pw_aff *pwaff2)
2402 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2405 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2406 __isl_take isl_pw_aff *pwaff2)
2408 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2411 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2412 __isl_take isl_pw_aff *pwaff2)
2414 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2417 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2418 __isl_take isl_pw_aff *pwaff2)
2420 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2423 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2424 __isl_take isl_pw_aff *pwaff2)
2426 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2429 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2430 __isl_take isl_pw_aff *pa2)
2432 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2435 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2437 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2438 __isl_take isl_pw_aff *pa2)
2440 int is_cst;
2442 is_cst = isl_pw_aff_is_cst(pa2);
2443 if (is_cst < 0)
2444 goto error;
2445 if (!is_cst)
2446 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2447 "second argument should be a piecewise constant",
2448 goto error);
2449 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2450 error:
2451 isl_pw_aff_free(pa1);
2452 isl_pw_aff_free(pa2);
2453 return NULL;
2456 /* Compute the quotient of the integer division of "pa1" by "pa2"
2457 * with rounding towards zero.
2458 * "pa2" is assumed to be a piecewise constant.
2460 * In particular, return
2462 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2465 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2466 __isl_take isl_pw_aff *pa2)
2468 int is_cst;
2469 isl_set *cond;
2470 isl_pw_aff *f, *c;
2472 is_cst = isl_pw_aff_is_cst(pa2);
2473 if (is_cst < 0)
2474 goto error;
2475 if (!is_cst)
2476 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2477 "second argument should be a piecewise constant",
2478 goto error);
2480 pa1 = isl_pw_aff_div(pa1, pa2);
2482 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2483 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2484 c = isl_pw_aff_ceil(pa1);
2485 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2486 error:
2487 isl_pw_aff_free(pa1);
2488 isl_pw_aff_free(pa2);
2489 return NULL;
2492 /* Compute the remainder of the integer division of "pa1" by "pa2"
2493 * with rounding towards zero.
2494 * "pa2" is assumed to be a piecewise constant.
2496 * In particular, return
2498 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2501 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2502 __isl_take isl_pw_aff *pa2)
2504 int is_cst;
2505 isl_pw_aff *res;
2507 is_cst = isl_pw_aff_is_cst(pa2);
2508 if (is_cst < 0)
2509 goto error;
2510 if (!is_cst)
2511 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2512 "second argument should be a piecewise constant",
2513 goto error);
2514 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2515 res = isl_pw_aff_mul(pa2, res);
2516 res = isl_pw_aff_sub(pa1, res);
2517 return res;
2518 error:
2519 isl_pw_aff_free(pa1);
2520 isl_pw_aff_free(pa2);
2521 return NULL;
2524 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2525 __isl_take isl_pw_aff *pwaff2)
2527 isl_set *le;
2528 isl_set *dom;
2530 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2531 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2532 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2533 isl_pw_aff_copy(pwaff2));
2534 dom = isl_set_subtract(dom, isl_set_copy(le));
2535 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2538 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2539 __isl_take isl_pw_aff *pwaff2)
2541 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2544 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2545 __isl_take isl_pw_aff *pwaff2)
2547 isl_set *ge;
2548 isl_set *dom;
2550 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2551 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2552 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2553 isl_pw_aff_copy(pwaff2));
2554 dom = isl_set_subtract(dom, isl_set_copy(ge));
2555 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2558 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2559 __isl_take isl_pw_aff *pwaff2)
2561 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2564 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2565 __isl_take isl_pw_aff_list *list,
2566 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2567 __isl_take isl_pw_aff *pwaff2))
2569 int i;
2570 isl_ctx *ctx;
2571 isl_pw_aff *res;
2573 if (!list)
2574 return NULL;
2576 ctx = isl_pw_aff_list_get_ctx(list);
2577 if (list->n < 1)
2578 isl_die(ctx, isl_error_invalid,
2579 "list should contain at least one element",
2580 return isl_pw_aff_list_free(list));
2582 res = isl_pw_aff_copy(list->p[0]);
2583 for (i = 1; i < list->n; ++i)
2584 res = fn(res, isl_pw_aff_copy(list->p[i]));
2586 isl_pw_aff_list_free(list);
2587 return res;
2590 /* Return an isl_pw_aff that maps each element in the intersection of the
2591 * domains of the elements of list to the minimal corresponding affine
2592 * expression.
2594 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2596 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2599 /* Return an isl_pw_aff that maps each element in the intersection of the
2600 * domains of the elements of list to the maximal corresponding affine
2601 * expression.
2603 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2605 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2608 /* Mark the domains of "pwaff" as rational.
2610 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2612 int i;
2614 pwaff = isl_pw_aff_cow(pwaff);
2615 if (!pwaff)
2616 return NULL;
2617 if (pwaff->n == 0)
2618 return pwaff;
2620 for (i = 0; i < pwaff->n; ++i) {
2621 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2622 if (!pwaff->p[i].set)
2623 return isl_pw_aff_free(pwaff);
2626 return pwaff;
2629 /* Mark the domains of the elements of "list" as rational.
2631 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2632 __isl_take isl_pw_aff_list *list)
2634 int i, n;
2636 if (!list)
2637 return NULL;
2638 if (list->n == 0)
2639 return list;
2641 n = list->n;
2642 for (i = 0; i < n; ++i) {
2643 isl_pw_aff *pa;
2645 pa = isl_pw_aff_list_get_pw_aff(list, i);
2646 pa = isl_pw_aff_set_rational(pa);
2647 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2650 return list;
2653 #undef BASE
2654 #define BASE aff
2656 #include <isl_multi_templ.c>
2658 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2659 * domain.
2661 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2662 __isl_take isl_multi_aff *ma)
2664 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2665 return isl_pw_multi_aff_alloc(dom, ma);
2668 /* Create a piecewise multi-affine expression in the given space that maps each
2669 * input dimension to the corresponding output dimension.
2671 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2672 __isl_take isl_space *space)
2674 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2677 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2678 __isl_take isl_multi_aff *maff2)
2680 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2683 /* Subtract "ma2" from "ma1" and return the result.
2685 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2686 __isl_take isl_multi_aff *ma2)
2688 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2691 /* Given two multi-affine expressions A -> B and C -> D,
2692 * construct a multi-affine expression [A -> C] -> [B -> D].
2694 __isl_give isl_multi_aff *isl_multi_aff_product(
2695 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2697 int i;
2698 isl_aff *aff;
2699 isl_space *space;
2700 isl_multi_aff *res;
2701 int in1, in2, out1, out2;
2703 in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2704 in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2705 out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2706 out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2707 space = isl_space_product(isl_multi_aff_get_space(ma1),
2708 isl_multi_aff_get_space(ma2));
2709 res = isl_multi_aff_alloc(isl_space_copy(space));
2710 space = isl_space_domain(space);
2712 for (i = 0; i < out1; ++i) {
2713 aff = isl_multi_aff_get_aff(ma1, i);
2714 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2715 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2716 res = isl_multi_aff_set_aff(res, i, aff);
2719 for (i = 0; i < out2; ++i) {
2720 aff = isl_multi_aff_get_aff(ma2, i);
2721 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2722 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2723 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2726 isl_space_free(space);
2727 isl_multi_aff_free(ma1);
2728 isl_multi_aff_free(ma2);
2729 return res;
2732 /* Exploit the equalities in "eq" to simplify the affine expressions.
2734 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2735 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2737 int i;
2739 maff = isl_multi_aff_cow(maff);
2740 if (!maff || !eq)
2741 goto error;
2743 for (i = 0; i < maff->n; ++i) {
2744 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2745 isl_basic_set_copy(eq));
2746 if (!maff->p[i])
2747 goto error;
2750 isl_basic_set_free(eq);
2751 return maff;
2752 error:
2753 isl_basic_set_free(eq);
2754 isl_multi_aff_free(maff);
2755 return NULL;
2758 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2759 isl_int f)
2761 int i;
2763 maff = isl_multi_aff_cow(maff);
2764 if (!maff)
2765 return NULL;
2767 for (i = 0; i < maff->n; ++i) {
2768 maff->p[i] = isl_aff_scale(maff->p[i], f);
2769 if (!maff->p[i])
2770 return isl_multi_aff_free(maff);
2773 return maff;
2776 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2777 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2779 maff1 = isl_multi_aff_add(maff1, maff2);
2780 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2781 return maff1;
2784 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2786 if (!maff)
2787 return -1;
2789 return 0;
2792 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2793 __isl_keep isl_multi_aff *maff2)
2795 int i;
2796 int equal;
2798 if (!maff1 || !maff2)
2799 return -1;
2800 if (maff1->n != maff2->n)
2801 return 0;
2802 equal = isl_space_is_equal(maff1->space, maff2->space);
2803 if (equal < 0 || !equal)
2804 return equal;
2806 for (i = 0; i < maff1->n; ++i) {
2807 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2808 if (equal < 0 || !equal)
2809 return equal;
2812 return 1;
2815 /* Return the set of domain elements where "ma1" is lexicographically
2816 * smaller than or equal to "ma2".
2818 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2819 __isl_take isl_multi_aff *ma2)
2821 return isl_multi_aff_lex_ge_set(ma2, ma1);
2824 /* Return the set of domain elements where "ma1" is lexicographically
2825 * greater than or equal to "ma2".
2827 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2828 __isl_take isl_multi_aff *ma2)
2830 isl_space *space;
2831 isl_map *map1, *map2;
2832 isl_map *map, *ge;
2834 map1 = isl_map_from_multi_aff(ma1);
2835 map2 = isl_map_from_multi_aff(ma2);
2836 map = isl_map_range_product(map1, map2);
2837 space = isl_space_range(isl_map_get_space(map));
2838 space = isl_space_domain(isl_space_unwrap(space));
2839 ge = isl_map_lex_ge(space);
2840 map = isl_map_intersect_range(map, isl_map_wrap(ge));
2842 return isl_map_domain(map);
2845 #undef PW
2846 #define PW isl_pw_multi_aff
2847 #undef EL
2848 #define EL isl_multi_aff
2849 #undef EL_IS_ZERO
2850 #define EL_IS_ZERO is_empty
2851 #undef ZERO
2852 #define ZERO empty
2853 #undef IS_ZERO
2854 #define IS_ZERO is_empty
2855 #undef FIELD
2856 #define FIELD maff
2857 #undef DEFAULT_IS_ZERO
2858 #define DEFAULT_IS_ZERO 0
2860 #define NO_NEG
2861 #define NO_EVAL
2862 #define NO_OPT
2863 #define NO_INVOLVES_DIMS
2864 #define NO_MOVE_DIMS
2865 #define NO_INSERT_DIMS
2866 #define NO_LIFT
2867 #define NO_MORPH
2869 #include <isl_pw_templ.c>
2871 #undef UNION
2872 #define UNION isl_union_pw_multi_aff
2873 #undef PART
2874 #define PART isl_pw_multi_aff
2875 #undef PARTS
2876 #define PARTS pw_multi_aff
2877 #define ALIGN_DOMAIN
2879 #define NO_EVAL
2881 #include <isl_union_templ.c>
2883 /* Given a function "cmp" that returns the set of elements where
2884 * "ma1" is "better" than "ma2", return the intersection of this
2885 * set with "dom1" and "dom2".
2887 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2888 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2889 __isl_keep isl_multi_aff *ma2,
2890 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2891 __isl_take isl_multi_aff *ma2))
2893 isl_set *common;
2894 isl_set *better;
2895 int is_empty;
2897 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2898 is_empty = isl_set_plain_is_empty(common);
2899 if (is_empty >= 0 && is_empty)
2900 return common;
2901 if (is_empty < 0)
2902 return isl_set_free(common);
2903 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2904 better = isl_set_intersect(common, better);
2906 return better;
2909 /* Given a function "cmp" that returns the set of elements where
2910 * "ma1" is "better" than "ma2", return a piecewise multi affine
2911 * expression defined on the union of the definition domains
2912 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
2913 * "pma2" on each cell. If only one of the two input functions
2914 * is defined on a given cell, then it is considered the best.
2916 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
2917 __isl_take isl_pw_multi_aff *pma1,
2918 __isl_take isl_pw_multi_aff *pma2,
2919 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2920 __isl_take isl_multi_aff *ma2))
2922 int i, j, n;
2923 isl_pw_multi_aff *res = NULL;
2924 isl_ctx *ctx;
2925 isl_set *set = NULL;
2927 if (!pma1 || !pma2)
2928 goto error;
2930 ctx = isl_space_get_ctx(pma1->dim);
2931 if (!isl_space_is_equal(pma1->dim, pma2->dim))
2932 isl_die(ctx, isl_error_invalid,
2933 "arguments should live in the same space", goto error);
2935 if (isl_pw_multi_aff_is_empty(pma1)) {
2936 isl_pw_multi_aff_free(pma1);
2937 return pma2;
2940 if (isl_pw_multi_aff_is_empty(pma2)) {
2941 isl_pw_multi_aff_free(pma2);
2942 return pma1;
2945 n = 2 * (pma1->n + 1) * (pma2->n + 1);
2946 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
2948 for (i = 0; i < pma1->n; ++i) {
2949 set = isl_set_copy(pma1->p[i].set);
2950 for (j = 0; j < pma2->n; ++j) {
2951 isl_set *better;
2952 int is_empty;
2954 better = shared_and_better(pma2->p[j].set,
2955 pma1->p[i].set, pma2->p[j].maff,
2956 pma1->p[i].maff, cmp);
2957 is_empty = isl_set_plain_is_empty(better);
2958 if (is_empty < 0 || is_empty) {
2959 isl_set_free(better);
2960 if (is_empty < 0)
2961 goto error;
2962 continue;
2964 set = isl_set_subtract(set, isl_set_copy(better));
2966 res = isl_pw_multi_aff_add_piece(res, better,
2967 isl_multi_aff_copy(pma2->p[j].maff));
2969 res = isl_pw_multi_aff_add_piece(res, set,
2970 isl_multi_aff_copy(pma1->p[i].maff));
2973 for (j = 0; j < pma2->n; ++j) {
2974 set = isl_set_copy(pma2->p[j].set);
2975 for (i = 0; i < pma1->n; ++i)
2976 set = isl_set_subtract(set,
2977 isl_set_copy(pma1->p[i].set));
2978 res = isl_pw_multi_aff_add_piece(res, set,
2979 isl_multi_aff_copy(pma2->p[j].maff));
2982 isl_pw_multi_aff_free(pma1);
2983 isl_pw_multi_aff_free(pma2);
2985 return res;
2986 error:
2987 isl_pw_multi_aff_free(pma1);
2988 isl_pw_multi_aff_free(pma2);
2989 isl_set_free(set);
2990 return isl_pw_multi_aff_free(res);
2993 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
2994 __isl_take isl_pw_multi_aff *pma1,
2995 __isl_take isl_pw_multi_aff *pma2)
2997 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3000 /* Given two piecewise multi affine expressions, return a piecewise
3001 * multi-affine expression defined on the union of the definition domains
3002 * of the inputs that is equal to the lexicographic maximum of the two
3003 * inputs on each cell. If only one of the two inputs is defined on
3004 * a given cell, then it is considered to be the maximum.
3006 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3007 __isl_take isl_pw_multi_aff *pma1,
3008 __isl_take isl_pw_multi_aff *pma2)
3010 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3011 &pw_multi_aff_union_lexmax);
3014 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3015 __isl_take isl_pw_multi_aff *pma1,
3016 __isl_take isl_pw_multi_aff *pma2)
3018 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3021 /* Given two piecewise multi affine expressions, return a piecewise
3022 * multi-affine expression defined on the union of the definition domains
3023 * of the inputs that is equal to the lexicographic minimum of the two
3024 * inputs on each cell. If only one of the two inputs is defined on
3025 * a given cell, then it is considered to be the minimum.
3027 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3028 __isl_take isl_pw_multi_aff *pma1,
3029 __isl_take isl_pw_multi_aff *pma2)
3031 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3032 &pw_multi_aff_union_lexmin);
3035 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3036 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3038 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3039 &isl_multi_aff_add);
3042 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3043 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3045 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3046 &pw_multi_aff_add);
3049 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3050 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3052 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3053 &isl_multi_aff_sub);
3056 /* Subtract "pma2" from "pma1" and return the result.
3058 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3059 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3061 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3062 &pw_multi_aff_sub);
3065 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3066 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3068 return isl_pw_multi_aff_union_add_(pma1, pma2);
3071 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3072 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3074 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3075 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3077 int i, j, n;
3078 isl_space *space;
3079 isl_pw_multi_aff *res;
3081 if (!pma1 || !pma2)
3082 goto error;
3084 n = pma1->n * pma2->n;
3085 space = isl_space_product(isl_space_copy(pma1->dim),
3086 isl_space_copy(pma2->dim));
3087 res = isl_pw_multi_aff_alloc_size(space, n);
3089 for (i = 0; i < pma1->n; ++i) {
3090 for (j = 0; j < pma2->n; ++j) {
3091 isl_set *domain;
3092 isl_multi_aff *ma;
3094 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3095 isl_set_copy(pma2->p[j].set));
3096 ma = isl_multi_aff_product(
3097 isl_multi_aff_copy(pma1->p[i].maff),
3098 isl_multi_aff_copy(pma2->p[i].maff));
3099 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3103 isl_pw_multi_aff_free(pma1);
3104 isl_pw_multi_aff_free(pma2);
3105 return res;
3106 error:
3107 isl_pw_multi_aff_free(pma1);
3108 isl_pw_multi_aff_free(pma2);
3109 return NULL;
3112 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3113 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3115 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3116 &pw_multi_aff_product);
3119 /* Construct a map mapping the domain of the piecewise multi-affine expression
3120 * to its range, with each dimension in the range equated to the
3121 * corresponding affine expression on its cell.
3123 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3125 int i;
3126 isl_map *map;
3128 if (!pma)
3129 return NULL;
3131 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3133 for (i = 0; i < pma->n; ++i) {
3134 isl_multi_aff *maff;
3135 isl_basic_map *bmap;
3136 isl_map *map_i;
3138 maff = isl_multi_aff_copy(pma->p[i].maff);
3139 bmap = isl_basic_map_from_multi_aff(maff);
3140 map_i = isl_map_from_basic_map(bmap);
3141 map_i = isl_map_intersect_domain(map_i,
3142 isl_set_copy(pma->p[i].set));
3143 map = isl_map_union_disjoint(map, map_i);
3146 isl_pw_multi_aff_free(pma);
3147 return map;
3150 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3152 if (!pma)
3153 return NULL;
3155 if (!isl_space_is_set(pma->dim))
3156 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3157 "isl_pw_multi_aff cannot be converted into an isl_set",
3158 return isl_pw_multi_aff_free(pma));
3160 return isl_map_from_pw_multi_aff(pma);
3163 /* Given a basic map with a single output dimension that is defined
3164 * in terms of the parameters and input dimensions using an equality,
3165 * extract an isl_aff that expresses the output dimension in terms
3166 * of the parameters and input dimensions.
3168 * Since some applications expect the result of isl_pw_multi_aff_from_map
3169 * to only contain integer affine expressions, we compute the floor
3170 * of the expression before returning.
3172 * This function shares some similarities with
3173 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3175 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3176 __isl_take isl_basic_map *bmap)
3178 int i;
3179 unsigned offset;
3180 unsigned total;
3181 isl_local_space *ls;
3182 isl_aff *aff;
3184 if (!bmap)
3185 return NULL;
3186 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3187 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3188 "basic map should have a single output dimension",
3189 goto error);
3190 offset = isl_basic_map_offset(bmap, isl_dim_out);
3191 total = isl_basic_map_total_dim(bmap);
3192 for (i = 0; i < bmap->n_eq; ++i) {
3193 if (isl_int_is_zero(bmap->eq[i][offset]))
3194 continue;
3195 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3196 1 + total - (offset + 1)) != -1)
3197 continue;
3198 break;
3200 if (i >= bmap->n_eq)
3201 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3202 "unable to find suitable equality", goto error);
3203 ls = isl_basic_map_get_local_space(bmap);
3204 aff = isl_aff_alloc(isl_local_space_domain(ls));
3205 if (!aff)
3206 goto error;
3207 if (isl_int_is_neg(bmap->eq[i][offset]))
3208 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3209 else
3210 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3211 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3212 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3213 isl_basic_map_free(bmap);
3215 aff = isl_aff_remove_unused_divs(aff);
3216 aff = isl_aff_floor(aff);
3217 return aff;
3218 error:
3219 isl_basic_map_free(bmap);
3220 return NULL;
3223 /* Given a basic map where each output dimension is defined
3224 * in terms of the parameters and input dimensions using an equality,
3225 * extract an isl_multi_aff that expresses the output dimensions in terms
3226 * of the parameters and input dimensions.
3228 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3229 __isl_take isl_basic_map *bmap)
3231 int i;
3232 unsigned n_out;
3233 isl_multi_aff *ma;
3235 if (!bmap)
3236 return NULL;
3238 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3239 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3241 for (i = 0; i < n_out; ++i) {
3242 isl_basic_map *bmap_i;
3243 isl_aff *aff;
3245 bmap_i = isl_basic_map_copy(bmap);
3246 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3247 i + 1, n_out - (1 + i));
3248 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3249 aff = extract_isl_aff_from_basic_map(bmap_i);
3250 ma = isl_multi_aff_set_aff(ma, i, aff);
3253 isl_basic_map_free(bmap);
3255 return ma;
3258 /* Create an isl_pw_multi_aff that is equivalent to
3259 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3260 * The given basic map is such that each output dimension is defined
3261 * in terms of the parameters and input dimensions using an equality.
3263 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3264 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3266 isl_multi_aff *ma;
3268 ma = extract_isl_multi_aff_from_basic_map(bmap);
3269 return isl_pw_multi_aff_alloc(domain, ma);
3272 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3273 * This obviously only works if the input "map" is single-valued.
3274 * If so, we compute the lexicographic minimum of the image in the form
3275 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3276 * to its lexicographic minimum.
3277 * If the input is not single-valued, we produce an error.
3279 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3280 __isl_take isl_map *map)
3282 int i;
3283 int sv;
3284 isl_pw_multi_aff *pma;
3286 sv = isl_map_is_single_valued(map);
3287 if (sv < 0)
3288 goto error;
3289 if (!sv)
3290 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3291 "map is not single-valued", goto error);
3292 map = isl_map_make_disjoint(map);
3293 if (!map)
3294 return NULL;
3296 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3298 for (i = 0; i < map->n; ++i) {
3299 isl_pw_multi_aff *pma_i;
3300 isl_basic_map *bmap;
3301 bmap = isl_basic_map_copy(map->p[i]);
3302 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3303 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3306 isl_map_free(map);
3307 return pma;
3308 error:
3309 isl_map_free(map);
3310 return NULL;
3313 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3314 * taking into account that the output dimension at position "d"
3315 * can be represented as
3317 * x = floor((e(...) + c1) / m)
3319 * given that constraint "i" is of the form
3321 * e(...) + c1 - m x >= 0
3324 * Let "map" be of the form
3326 * A -> B
3328 * We construct a mapping
3330 * A -> [A -> x = floor(...)]
3332 * apply that to the map, obtaining
3334 * [A -> x = floor(...)] -> B
3336 * and equate dimension "d" to x.
3337 * We then compute a isl_pw_multi_aff representation of the resulting map
3338 * and plug in the mapping above.
3340 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3341 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3343 isl_ctx *ctx;
3344 isl_space *space;
3345 isl_local_space *ls;
3346 isl_multi_aff *ma;
3347 isl_aff *aff;
3348 isl_vec *v;
3349 isl_map *insert;
3350 int offset;
3351 int n;
3352 int n_in;
3353 isl_pw_multi_aff *pma;
3354 int is_set;
3356 is_set = isl_map_is_set(map);
3358 offset = isl_basic_map_offset(hull, isl_dim_out);
3359 ctx = isl_map_get_ctx(map);
3360 space = isl_space_domain(isl_map_get_space(map));
3361 n_in = isl_space_dim(space, isl_dim_set);
3362 n = isl_space_dim(space, isl_dim_all);
3364 v = isl_vec_alloc(ctx, 1 + 1 + n);
3365 if (v) {
3366 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3367 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3369 isl_basic_map_free(hull);
3371 ls = isl_local_space_from_space(isl_space_copy(space));
3372 aff = isl_aff_alloc_vec(ls, v);
3373 aff = isl_aff_floor(aff);
3374 if (is_set) {
3375 isl_space_free(space);
3376 ma = isl_multi_aff_from_aff(aff);
3377 } else {
3378 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3379 ma = isl_multi_aff_range_product(ma,
3380 isl_multi_aff_from_aff(aff));
3383 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3384 map = isl_map_apply_domain(map, insert);
3385 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3386 pma = isl_pw_multi_aff_from_map(map);
3387 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3389 return pma;
3392 /* Is constraint "c" of the form
3394 * e(...) + c1 - m x >= 0
3396 * or
3398 * -e(...) + c2 + m x >= 0
3400 * where m > 1 and e only depends on parameters and input dimemnsions?
3402 * "offset" is the offset of the output dimensions
3403 * "pos" is the position of output dimension x.
3405 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3407 if (isl_int_is_zero(c[offset + d]))
3408 return 0;
3409 if (isl_int_is_one(c[offset + d]))
3410 return 0;
3411 if (isl_int_is_negone(c[offset + d]))
3412 return 0;
3413 if (isl_seq_first_non_zero(c + offset, d) != -1)
3414 return 0;
3415 if (isl_seq_first_non_zero(c + offset + d + 1,
3416 total - (offset + d + 1)) != -1)
3417 return 0;
3418 return 1;
3421 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3423 * As a special case, we first check if there is any pair of constraints,
3424 * shared by all the basic maps in "map" that force a given dimension
3425 * to be equal to the floor of some affine combination of the input dimensions.
3427 * In particular, if we can find two constraints
3429 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
3431 * and
3433 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
3435 * where m > 1 and e only depends on parameters and input dimemnsions,
3436 * and such that
3438 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
3440 * then we know that we can take
3442 * x = floor((e(...) + c1) / m)
3444 * without having to perform any computation.
3446 * Note that we know that
3448 * c1 + c2 >= 1
3450 * If c1 + c2 were 0, then we would have detected an equality during
3451 * simplification. If c1 + c2 were negative, then we would have detected
3452 * a contradiction.
3454 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3455 __isl_take isl_map *map)
3457 int d, dim;
3458 int i, j, n;
3459 int offset, total;
3460 isl_int sum;
3461 isl_basic_map *hull;
3463 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3464 if (!hull)
3465 goto error;
3467 isl_int_init(sum);
3468 dim = isl_map_dim(map, isl_dim_out);
3469 offset = isl_basic_map_offset(hull, isl_dim_out);
3470 total = 1 + isl_basic_map_total_dim(hull);
3471 n = hull->n_ineq;
3472 for (d = 0; d < dim; ++d) {
3473 for (i = 0; i < n; ++i) {
3474 if (!is_potential_div_constraint(hull->ineq[i],
3475 offset, d, total))
3476 continue;
3477 for (j = i + 1; j < n; ++j) {
3478 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3479 hull->ineq[j] + 1, total - 1))
3480 continue;
3481 isl_int_add(sum, hull->ineq[i][0],
3482 hull->ineq[j][0]);
3483 if (isl_int_abs_lt(sum,
3484 hull->ineq[i][offset + d]))
3485 break;
3488 if (j >= n)
3489 continue;
3490 isl_int_clear(sum);
3491 if (isl_int_is_pos(hull->ineq[j][offset + d]))
3492 j = i;
3493 return pw_multi_aff_from_map_div(map, hull, d, j);
3496 isl_int_clear(sum);
3497 isl_basic_map_free(hull);
3498 return pw_multi_aff_from_map_base(map);
3499 error:
3500 isl_map_free(map);
3501 isl_basic_map_free(hull);
3502 return NULL;
3505 /* Given an affine expression
3507 * [A -> B] -> f(A,B)
3509 * construct an isl_multi_aff
3511 * [A -> B] -> B'
3513 * such that dimension "d" in B' is set to "aff" and the remaining
3514 * dimensions are set equal to the corresponding dimensions in B.
3515 * "n_in" is the dimension of the space A.
3516 * "n_out" is the dimension of the space B.
3518 * If "is_set" is set, then the affine expression is of the form
3520 * [B] -> f(B)
3522 * and we construct an isl_multi_aff
3524 * B -> B'
3526 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3527 unsigned n_in, unsigned n_out, int is_set)
3529 int i;
3530 isl_multi_aff *ma;
3531 isl_space *space, *space2;
3532 isl_local_space *ls;
3534 space = isl_aff_get_domain_space(aff);
3535 ls = isl_local_space_from_space(isl_space_copy(space));
3536 space2 = isl_space_copy(space);
3537 if (!is_set)
3538 space2 = isl_space_range(isl_space_unwrap(space2));
3539 space = isl_space_map_from_domain_and_range(space, space2);
3540 ma = isl_multi_aff_alloc(space);
3541 ma = isl_multi_aff_set_aff(ma, d, aff);
3543 for (i = 0; i < n_out; ++i) {
3544 if (i == d)
3545 continue;
3546 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3547 isl_dim_set, n_in + i);
3548 ma = isl_multi_aff_set_aff(ma, i, aff);
3551 isl_local_space_free(ls);
3553 return ma;
3556 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3557 * taking into account that the dimension at position "d" can be written as
3559 * x = m a + f(..) (1)
3561 * where m is equal to "gcd".
3562 * "i" is the index of the equality in "hull" that defines f(..).
3563 * In particular, the equality is of the form
3565 * f(..) - x + m g(existentials) = 0
3567 * or
3569 * -f(..) + x + m g(existentials) = 0
3571 * We basically plug (1) into "map", resulting in a map with "a"
3572 * in the range instead of "x". The corresponding isl_pw_multi_aff
3573 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3575 * Specifically, given the input map
3577 * A -> B
3579 * We first wrap it into a set
3581 * [A -> B]
3583 * and define (1) on top of the corresponding space, resulting in "aff".
3584 * We use this to create an isl_multi_aff that maps the output position "d"
3585 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3586 * We plug this into the wrapped map, unwrap the result and compute the
3587 * corresponding isl_pw_multi_aff.
3588 * The result is an expression
3590 * A -> T(A)
3592 * We adjust that to
3594 * A -> [A -> T(A)]
3596 * so that we can plug that into "aff", after extending the latter to
3597 * a mapping
3599 * [A -> B] -> B'
3602 * If "map" is actually a set, then there is no "A" space, meaning
3603 * that we do not need to perform any wrapping, and that the result
3604 * of the recursive call is of the form
3606 * [T]
3608 * which is plugged into a mapping of the form
3610 * B -> B'
3612 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3613 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3614 isl_int gcd)
3616 isl_set *set;
3617 isl_space *space;
3618 isl_local_space *ls;
3619 isl_aff *aff;
3620 isl_multi_aff *ma;
3621 isl_pw_multi_aff *pma, *id;
3622 unsigned n_in;
3623 unsigned o_out;
3624 unsigned n_out;
3625 int is_set;
3627 is_set = isl_map_is_set(map);
3629 n_in = isl_basic_map_dim(hull, isl_dim_in);
3630 n_out = isl_basic_map_dim(hull, isl_dim_out);
3631 o_out = isl_basic_map_offset(hull, isl_dim_out);
3633 if (is_set)
3634 set = map;
3635 else
3636 set = isl_map_wrap(map);
3637 space = isl_space_map_from_set(isl_set_get_space(set));
3638 ma = isl_multi_aff_identity(space);
3639 ls = isl_local_space_from_space(isl_set_get_space(set));
3640 aff = isl_aff_alloc(ls);
3641 if (aff) {
3642 isl_int_set_si(aff->v->el[0], 1);
3643 if (isl_int_is_one(hull->eq[i][o_out + d]))
3644 isl_seq_neg(aff->v->el + 1, hull->eq[i],
3645 aff->v->size - 1);
3646 else
3647 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3648 aff->v->size - 1);
3649 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3651 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3652 set = isl_set_preimage_multi_aff(set, ma);
3654 ma = range_map(aff, d, n_in, n_out, is_set);
3656 if (is_set)
3657 map = set;
3658 else
3659 map = isl_set_unwrap(set);
3660 pma = isl_pw_multi_aff_from_map(set);
3662 if (!is_set) {
3663 space = isl_pw_multi_aff_get_domain_space(pma);
3664 space = isl_space_map_from_set(space);
3665 id = isl_pw_multi_aff_identity(space);
3666 pma = isl_pw_multi_aff_range_product(id, pma);
3668 id = isl_pw_multi_aff_from_multi_aff(ma);
3669 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3671 isl_basic_map_free(hull);
3672 return pma;
3675 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3677 * As a special case, we first check if all output dimensions are uniquely
3678 * defined in terms of the parameters and input dimensions over the entire
3679 * domain. If so, we extract the desired isl_pw_multi_aff directly
3680 * from the affine hull of "map" and its domain.
3682 * Otherwise, we check if any of the output dimensions is "strided".
3683 * That is, we check if can be written as
3685 * x = m a + f(..)
3687 * with m greater than 1, a some combination of existentiall quantified
3688 * variables and f and expression in the parameters and input dimensions.
3689 * If so, we remove the stride in pw_multi_aff_from_map_stride.
3691 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3692 * special case.
3694 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3696 int i, j;
3697 int sv;
3698 isl_basic_map *hull;
3699 unsigned n_out;
3700 unsigned o_out;
3701 unsigned n_div;
3702 unsigned o_div;
3703 isl_int gcd;
3705 if (!map)
3706 return NULL;
3708 hull = isl_map_affine_hull(isl_map_copy(map));
3709 sv = isl_basic_map_plain_is_single_valued(hull);
3710 if (sv >= 0 && sv)
3711 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3712 if (sv < 0)
3713 hull = isl_basic_map_free(hull);
3714 if (!hull)
3715 goto error;
3717 n_div = isl_basic_map_dim(hull, isl_dim_div);
3718 o_div = isl_basic_map_offset(hull, isl_dim_div);
3720 if (n_div == 0) {
3721 isl_basic_map_free(hull);
3722 return pw_multi_aff_from_map_check_div(map);
3725 isl_int_init(gcd);
3727 n_out = isl_basic_map_dim(hull, isl_dim_out);
3728 o_out = isl_basic_map_offset(hull, isl_dim_out);
3730 for (i = 0; i < n_out; ++i) {
3731 for (j = 0; j < hull->n_eq; ++j) {
3732 isl_int *eq = hull->eq[j];
3733 isl_pw_multi_aff *res;
3735 if (!isl_int_is_one(eq[o_out + i]) &&
3736 !isl_int_is_negone(eq[o_out + i]))
3737 continue;
3738 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3739 continue;
3740 if (isl_seq_first_non_zero(eq + o_out + i + 1,
3741 n_out - (i + 1)) != -1)
3742 continue;
3743 isl_seq_gcd(eq + o_div, n_div, &gcd);
3744 if (isl_int_is_zero(gcd))
3745 continue;
3746 if (isl_int_is_one(gcd))
3747 continue;
3749 res = pw_multi_aff_from_map_stride(map, hull,
3750 i, j, gcd);
3751 isl_int_clear(gcd);
3752 return res;
3756 isl_int_clear(gcd);
3757 isl_basic_map_free(hull);
3758 return pw_multi_aff_from_map_check_div(map);
3759 error:
3760 isl_map_free(map);
3761 return NULL;
3764 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3766 return isl_pw_multi_aff_from_map(set);
3769 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3770 * add it to *user.
3772 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3774 isl_union_pw_multi_aff **upma = user;
3775 isl_pw_multi_aff *pma;
3777 pma = isl_pw_multi_aff_from_map(map);
3778 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3780 return *upma ? 0 : -1;
3783 /* Try and create an isl_union_pw_multi_aff that is equivalent
3784 * to the given isl_union_map.
3785 * The isl_union_map is required to be single-valued in each space.
3786 * Otherwise, an error is produced.
3788 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3789 __isl_take isl_union_map *umap)
3791 isl_space *space;
3792 isl_union_pw_multi_aff *upma;
3794 space = isl_union_map_get_space(umap);
3795 upma = isl_union_pw_multi_aff_empty(space);
3796 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3797 upma = isl_union_pw_multi_aff_free(upma);
3798 isl_union_map_free(umap);
3800 return upma;
3803 /* Try and create an isl_union_pw_multi_aff that is equivalent
3804 * to the given isl_union_set.
3805 * The isl_union_set is required to be a singleton in each space.
3806 * Otherwise, an error is produced.
3808 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
3809 __isl_take isl_union_set *uset)
3811 return isl_union_pw_multi_aff_from_union_map(uset);
3814 /* Return the piecewise affine expression "set ? 1 : 0".
3816 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3818 isl_pw_aff *pa;
3819 isl_space *space = isl_set_get_space(set);
3820 isl_local_space *ls = isl_local_space_from_space(space);
3821 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3822 isl_aff *one = isl_aff_zero_on_domain(ls);
3824 one = isl_aff_add_constant_si(one, 1);
3825 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3826 set = isl_set_complement(set);
3827 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3829 return pa;
3832 /* Plug in "subs" for dimension "type", "pos" of "aff".
3834 * Let i be the dimension to replace and let "subs" be of the form
3836 * f/d
3838 * and "aff" of the form
3840 * (a i + g)/m
3842 * The result is
3844 * (a f + d g')/(m d)
3846 * where g' is the result of plugging in "subs" in each of the integer
3847 * divisions in g.
3849 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3850 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3852 isl_ctx *ctx;
3853 isl_int v;
3855 aff = isl_aff_cow(aff);
3856 if (!aff || !subs)
3857 return isl_aff_free(aff);
3859 ctx = isl_aff_get_ctx(aff);
3860 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3861 isl_die(ctx, isl_error_invalid,
3862 "spaces don't match", return isl_aff_free(aff));
3863 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3864 isl_die(ctx, isl_error_unsupported,
3865 "cannot handle divs yet", return isl_aff_free(aff));
3867 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3868 if (!aff->ls)
3869 return isl_aff_free(aff);
3871 aff->v = isl_vec_cow(aff->v);
3872 if (!aff->v)
3873 return isl_aff_free(aff);
3875 pos += isl_local_space_offset(aff->ls, type);
3877 isl_int_init(v);
3878 isl_seq_substitute(aff->v->el, pos, subs->v->el,
3879 aff->v->size, subs->v->size, v);
3880 isl_int_clear(v);
3882 return aff;
3885 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3886 * expressions in "maff".
3888 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3889 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3890 __isl_keep isl_aff *subs)
3892 int i;
3894 maff = isl_multi_aff_cow(maff);
3895 if (!maff || !subs)
3896 return isl_multi_aff_free(maff);
3898 if (type == isl_dim_in)
3899 type = isl_dim_set;
3901 for (i = 0; i < maff->n; ++i) {
3902 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3903 if (!maff->p[i])
3904 return isl_multi_aff_free(maff);
3907 return maff;
3910 /* Plug in "subs" for dimension "type", "pos" of "pma".
3912 * pma is of the form
3914 * A_i(v) -> M_i(v)
3916 * while subs is of the form
3918 * v' = B_j(v) -> S_j
3920 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
3921 * has a contribution in the result, in particular
3923 * C_ij(S_j) -> M_i(S_j)
3925 * Note that plugging in S_j in C_ij may also result in an empty set
3926 * and this contribution should simply be discarded.
3928 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
3929 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
3930 __isl_keep isl_pw_aff *subs)
3932 int i, j, n;
3933 isl_pw_multi_aff *res;
3935 if (!pma || !subs)
3936 return isl_pw_multi_aff_free(pma);
3938 n = pma->n * subs->n;
3939 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
3941 for (i = 0; i < pma->n; ++i) {
3942 for (j = 0; j < subs->n; ++j) {
3943 isl_set *common;
3944 isl_multi_aff *res_ij;
3945 int empty;
3947 common = isl_set_intersect(
3948 isl_set_copy(pma->p[i].set),
3949 isl_set_copy(subs->p[j].set));
3950 common = isl_set_substitute(common,
3951 type, pos, subs->p[j].aff);
3952 empty = isl_set_plain_is_empty(common);
3953 if (empty < 0 || empty) {
3954 isl_set_free(common);
3955 if (empty < 0)
3956 goto error;
3957 continue;
3960 res_ij = isl_multi_aff_substitute(
3961 isl_multi_aff_copy(pma->p[i].maff),
3962 type, pos, subs->p[j].aff);
3964 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
3968 isl_pw_multi_aff_free(pma);
3969 return res;
3970 error:
3971 isl_pw_multi_aff_free(pma);
3972 isl_pw_multi_aff_free(res);
3973 return NULL;
3976 /* Compute the preimage of a range of dimensions in the affine expression "src"
3977 * under "ma" and put the result in "dst". The number of dimensions in "src"
3978 * that precede the range is given by "n_before". The number of dimensions
3979 * in the range is given by the number of output dimensions of "ma".
3980 * The number of dimensions that follow the range is given by "n_after".
3981 * If "has_denom" is set (to one),
3982 * then "src" and "dst" have an extra initial denominator.
3983 * "n_div_ma" is the number of existentials in "ma"
3984 * "n_div_bset" is the number of existentials in "src"
3985 * The resulting "dst" (which is assumed to have been allocated by
3986 * the caller) contains coefficients for both sets of existentials,
3987 * first those in "ma" and then those in "src".
3988 * f, c1, c2 and g are temporary objects that have been initialized
3989 * by the caller.
3991 * Let src represent the expression
3993 * (a(p) + f_u u + b v + f_w w + c(divs))/d
3995 * and let ma represent the expressions
3997 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
3999 * We start out with the following expression for dst:
4001 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4003 * with the multiplication factor f initially equal to 1
4004 * and f \sum_i b_i v_i kept separately.
4005 * For each x_i that we substitute, we multiply the numerator
4006 * (and denominator) of dst by c_1 = m_i and add the numerator
4007 * of the x_i expression multiplied by c_2 = f b_i,
4008 * after removing the common factors of c_1 and c_2.
4009 * The multiplication factor f also needs to be multiplied by c_1
4010 * for the next x_j, j > i.
4012 void isl_seq_preimage(isl_int *dst, isl_int *src,
4013 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4014 int n_div_ma, int n_div_bmap,
4015 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4017 int i;
4018 int n_param, n_in, n_out;
4019 int o_dst, o_src;
4021 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4022 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4023 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4025 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4026 o_dst = o_src = has_denom + 1 + n_param + n_before;
4027 isl_seq_clr(dst + o_dst, n_in);
4028 o_dst += n_in;
4029 o_src += n_out;
4030 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4031 o_dst += n_after;
4032 o_src += n_after;
4033 isl_seq_clr(dst + o_dst, n_div_ma);
4034 o_dst += n_div_ma;
4035 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4037 isl_int_set_si(f, 1);
4039 for (i = 0; i < n_out; ++i) {
4040 int offset = has_denom + 1 + n_param + n_before + i;
4042 if (isl_int_is_zero(src[offset]))
4043 continue;
4044 isl_int_set(c1, ma->p[i]->v->el[0]);
4045 isl_int_mul(c2, f, src[offset]);
4046 isl_int_gcd(g, c1, c2);
4047 isl_int_divexact(c1, c1, g);
4048 isl_int_divexact(c2, c2, g);
4050 isl_int_mul(f, f, c1);
4051 o_dst = has_denom;
4052 o_src = 1;
4053 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4054 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4055 o_dst += 1 + n_param;
4056 o_src += 1 + n_param;
4057 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4058 o_dst += n_before;
4059 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4060 c2, ma->p[i]->v->el + o_src, n_in);
4061 o_dst += n_in;
4062 o_src += n_in;
4063 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4064 o_dst += n_after;
4065 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4066 c2, ma->p[i]->v->el + o_src, n_div_ma);
4067 o_dst += n_div_ma;
4068 o_src += n_div_ma;
4069 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4070 if (has_denom)
4071 isl_int_mul(dst[0], dst[0], c1);
4075 /* Compute the pullback of "aff" by the function represented by "ma".
4076 * In other words, plug in "ma" in "aff". The result is an affine expression
4077 * defined over the domain space of "ma".
4079 * If "aff" is represented by
4081 * (a(p) + b x + c(divs))/d
4083 * and ma is represented by
4085 * x = D(p) + F(y) + G(divs')
4087 * then the result is
4089 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4091 * The divs in the local space of the input are similarly adjusted
4092 * through a call to isl_local_space_preimage_multi_aff.
4094 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4095 __isl_take isl_multi_aff *ma)
4097 isl_aff *res = NULL;
4098 isl_local_space *ls;
4099 int n_div_aff, n_div_ma;
4100 isl_int f, c1, c2, g;
4102 ma = isl_multi_aff_align_divs(ma);
4103 if (!aff || !ma)
4104 goto error;
4106 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4107 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4109 ls = isl_aff_get_domain_local_space(aff);
4110 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4111 res = isl_aff_alloc(ls);
4112 if (!res)
4113 goto error;
4115 isl_int_init(f);
4116 isl_int_init(c1);
4117 isl_int_init(c2);
4118 isl_int_init(g);
4120 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4121 f, c1, c2, g, 1);
4123 isl_int_clear(f);
4124 isl_int_clear(c1);
4125 isl_int_clear(c2);
4126 isl_int_clear(g);
4128 isl_aff_free(aff);
4129 isl_multi_aff_free(ma);
4130 res = isl_aff_normalize(res);
4131 return res;
4132 error:
4133 isl_aff_free(aff);
4134 isl_multi_aff_free(ma);
4135 isl_aff_free(res);
4136 return NULL;
4139 /* Compute the pullback of "ma1" by the function represented by "ma2".
4140 * In other words, plug in "ma2" in "ma1".
4142 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4143 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4145 int i;
4146 isl_space *space = NULL;
4148 ma2 = isl_multi_aff_align_divs(ma2);
4149 ma1 = isl_multi_aff_cow(ma1);
4150 if (!ma1 || !ma2)
4151 goto error;
4153 space = isl_space_join(isl_multi_aff_get_space(ma2),
4154 isl_multi_aff_get_space(ma1));
4156 for (i = 0; i < ma1->n; ++i) {
4157 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4158 isl_multi_aff_copy(ma2));
4159 if (!ma1->p[i])
4160 goto error;
4163 ma1 = isl_multi_aff_reset_space(ma1, space);
4164 isl_multi_aff_free(ma2);
4165 return ma1;
4166 error:
4167 isl_space_free(space);
4168 isl_multi_aff_free(ma2);
4169 isl_multi_aff_free(ma1);
4170 return NULL;
4173 /* Extend the local space of "dst" to include the divs
4174 * in the local space of "src".
4176 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4177 __isl_keep isl_aff *src)
4179 isl_ctx *ctx;
4180 int *exp1 = NULL;
4181 int *exp2 = NULL;
4182 isl_mat *div;
4184 if (!src || !dst)
4185 return isl_aff_free(dst);
4187 ctx = isl_aff_get_ctx(src);
4188 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4189 isl_die(ctx, isl_error_invalid,
4190 "spaces don't match", goto error);
4192 if (src->ls->div->n_row == 0)
4193 return dst;
4195 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4196 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4197 if (!exp1 || !exp2)
4198 goto error;
4200 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4201 dst = isl_aff_expand_divs(dst, div, exp2);
4202 free(exp1);
4203 free(exp2);
4205 return dst;
4206 error:
4207 free(exp1);
4208 free(exp2);
4209 return isl_aff_free(dst);
4212 /* Adjust the local spaces of the affine expressions in "maff"
4213 * such that they all have the save divs.
4215 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4216 __isl_take isl_multi_aff *maff)
4218 int i;
4220 if (!maff)
4221 return NULL;
4222 if (maff->n == 0)
4223 return maff;
4224 maff = isl_multi_aff_cow(maff);
4225 if (!maff)
4226 return NULL;
4228 for (i = 1; i < maff->n; ++i)
4229 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4230 for (i = 1; i < maff->n; ++i) {
4231 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4232 if (!maff->p[i])
4233 return isl_multi_aff_free(maff);
4236 return maff;
4239 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4241 aff = isl_aff_cow(aff);
4242 if (!aff)
4243 return NULL;
4245 aff->ls = isl_local_space_lift(aff->ls);
4246 if (!aff->ls)
4247 return isl_aff_free(aff);
4249 return aff;
4252 /* Lift "maff" to a space with extra dimensions such that the result
4253 * has no more existentially quantified variables.
4254 * If "ls" is not NULL, then *ls is assigned the local space that lies
4255 * at the basis of the lifting applied to "maff".
4257 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4258 __isl_give isl_local_space **ls)
4260 int i;
4261 isl_space *space;
4262 unsigned n_div;
4264 if (ls)
4265 *ls = NULL;
4267 if (!maff)
4268 return NULL;
4270 if (maff->n == 0) {
4271 if (ls) {
4272 isl_space *space = isl_multi_aff_get_domain_space(maff);
4273 *ls = isl_local_space_from_space(space);
4274 if (!*ls)
4275 return isl_multi_aff_free(maff);
4277 return maff;
4280 maff = isl_multi_aff_cow(maff);
4281 maff = isl_multi_aff_align_divs(maff);
4282 if (!maff)
4283 return NULL;
4285 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4286 space = isl_multi_aff_get_space(maff);
4287 space = isl_space_lift(isl_space_domain(space), n_div);
4288 space = isl_space_extend_domain_with_range(space,
4289 isl_multi_aff_get_space(maff));
4290 if (!space)
4291 return isl_multi_aff_free(maff);
4292 isl_space_free(maff->space);
4293 maff->space = space;
4295 if (ls) {
4296 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4297 if (!*ls)
4298 return isl_multi_aff_free(maff);
4301 for (i = 0; i < maff->n; ++i) {
4302 maff->p[i] = isl_aff_lift(maff->p[i]);
4303 if (!maff->p[i])
4304 goto error;
4307 return maff;
4308 error:
4309 if (ls)
4310 isl_local_space_free(*ls);
4311 return isl_multi_aff_free(maff);
4315 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4317 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4318 __isl_keep isl_pw_multi_aff *pma, int pos)
4320 int i;
4321 int n_out;
4322 isl_space *space;
4323 isl_pw_aff *pa;
4325 if (!pma)
4326 return NULL;
4328 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4329 if (pos < 0 || pos >= n_out)
4330 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4331 "index out of bounds", return NULL);
4333 space = isl_pw_multi_aff_get_space(pma);
4334 space = isl_space_drop_dims(space, isl_dim_out,
4335 pos + 1, n_out - pos - 1);
4336 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4338 pa = isl_pw_aff_alloc_size(space, pma->n);
4339 for (i = 0; i < pma->n; ++i) {
4340 isl_aff *aff;
4341 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4342 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4345 return pa;
4348 /* Return an isl_pw_multi_aff with the given "set" as domain and
4349 * an unnamed zero-dimensional range.
4351 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4352 __isl_take isl_set *set)
4354 isl_multi_aff *ma;
4355 isl_space *space;
4357 space = isl_set_get_space(set);
4358 space = isl_space_from_domain(space);
4359 ma = isl_multi_aff_zero(space);
4360 return isl_pw_multi_aff_alloc(set, ma);
4363 /* Add an isl_pw_multi_aff with the given "set" as domain and
4364 * an unnamed zero-dimensional range to *user.
4366 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4368 isl_union_pw_multi_aff **upma = user;
4369 isl_pw_multi_aff *pma;
4371 pma = isl_pw_multi_aff_from_domain(set);
4372 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4374 return 0;
4377 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4378 * an unnamed zero-dimensional range.
4380 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4381 __isl_take isl_union_set *uset)
4383 isl_space *space;
4384 isl_union_pw_multi_aff *upma;
4386 if (!uset)
4387 return NULL;
4389 space = isl_union_set_get_space(uset);
4390 upma = isl_union_pw_multi_aff_empty(space);
4392 if (isl_union_set_foreach_set(uset,
4393 &add_pw_multi_aff_from_domain, &upma) < 0)
4394 goto error;
4396 isl_union_set_free(uset);
4397 return upma;
4398 error:
4399 isl_union_set_free(uset);
4400 isl_union_pw_multi_aff_free(upma);
4401 return NULL;
4404 /* Convert "pma" to an isl_map and add it to *umap.
4406 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4408 isl_union_map **umap = user;
4409 isl_map *map;
4411 map = isl_map_from_pw_multi_aff(pma);
4412 *umap = isl_union_map_add_map(*umap, map);
4414 return 0;
4417 /* Construct a union map mapping the domain of the union
4418 * piecewise multi-affine expression to its range, with each dimension
4419 * in the range equated to the corresponding affine expression on its cell.
4421 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4422 __isl_take isl_union_pw_multi_aff *upma)
4424 isl_space *space;
4425 isl_union_map *umap;
4427 if (!upma)
4428 return NULL;
4430 space = isl_union_pw_multi_aff_get_space(upma);
4431 umap = isl_union_map_empty(space);
4433 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4434 &map_from_pw_multi_aff, &umap) < 0)
4435 goto error;
4437 isl_union_pw_multi_aff_free(upma);
4438 return umap;
4439 error:
4440 isl_union_pw_multi_aff_free(upma);
4441 isl_union_map_free(umap);
4442 return NULL;
4445 /* Local data for bin_entry and the callback "fn".
4447 struct isl_union_pw_multi_aff_bin_data {
4448 isl_union_pw_multi_aff *upma2;
4449 isl_union_pw_multi_aff *res;
4450 isl_pw_multi_aff *pma;
4451 int (*fn)(void **entry, void *user);
4454 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4455 * and call data->fn for each isl_pw_multi_aff in data->upma2.
4457 static int bin_entry(void **entry, void *user)
4459 struct isl_union_pw_multi_aff_bin_data *data = user;
4460 isl_pw_multi_aff *pma = *entry;
4462 data->pma = pma;
4463 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4464 data->fn, data) < 0)
4465 return -1;
4467 return 0;
4470 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4471 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4472 * passed as user field) and the isl_pw_multi_aff from upma2 is available
4473 * as *entry. The callback should adjust data->res if desired.
4475 static __isl_give isl_union_pw_multi_aff *bin_op(
4476 __isl_take isl_union_pw_multi_aff *upma1,
4477 __isl_take isl_union_pw_multi_aff *upma2,
4478 int (*fn)(void **entry, void *user))
4480 isl_space *space;
4481 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4483 space = isl_union_pw_multi_aff_get_space(upma2);
4484 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4485 space = isl_union_pw_multi_aff_get_space(upma1);
4486 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4488 if (!upma1 || !upma2)
4489 goto error;
4491 data.upma2 = upma2;
4492 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4493 upma1->table.n);
4494 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4495 &bin_entry, &data) < 0)
4496 goto error;
4498 isl_union_pw_multi_aff_free(upma1);
4499 isl_union_pw_multi_aff_free(upma2);
4500 return data.res;
4501 error:
4502 isl_union_pw_multi_aff_free(upma1);
4503 isl_union_pw_multi_aff_free(upma2);
4504 isl_union_pw_multi_aff_free(data.res);
4505 return NULL;
4508 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4509 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4511 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4512 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4514 isl_space *space;
4516 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4517 isl_pw_multi_aff_get_space(pma2));
4518 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4519 &isl_multi_aff_range_product);
4522 /* Given two isl_pw_multi_affs A -> B and C -> D,
4523 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4525 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4526 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4528 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4529 &pw_multi_aff_range_product);
4532 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4533 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4535 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4536 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4538 isl_space *space;
4540 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4541 isl_pw_multi_aff_get_space(pma2));
4542 space = isl_space_flatten_range(space);
4543 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4544 &isl_multi_aff_flat_range_product);
4547 /* Given two isl_pw_multi_affs A -> B and C -> D,
4548 * construct an isl_pw_multi_aff (A * C) -> (B, D).
4550 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4551 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4553 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4554 &pw_multi_aff_flat_range_product);
4557 /* If data->pma and *entry have the same domain space, then compute
4558 * their flat range product and the result to data->res.
4560 static int flat_range_product_entry(void **entry, void *user)
4562 struct isl_union_pw_multi_aff_bin_data *data = user;
4563 isl_pw_multi_aff *pma2 = *entry;
4565 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4566 pma2->dim, isl_dim_in))
4567 return 0;
4569 pma2 = isl_pw_multi_aff_flat_range_product(
4570 isl_pw_multi_aff_copy(data->pma),
4571 isl_pw_multi_aff_copy(pma2));
4573 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4575 return 0;
4578 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4579 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4581 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4582 __isl_take isl_union_pw_multi_aff *upma1,
4583 __isl_take isl_union_pw_multi_aff *upma2)
4585 return bin_op(upma1, upma2, &flat_range_product_entry);
4588 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4589 * The parameters are assumed to have been aligned.
4591 * The implementation essentially performs an isl_pw_*_on_shared_domain,
4592 * except that it works on two different isl_pw_* types.
4594 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4595 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4596 __isl_take isl_pw_aff *pa)
4598 int i, j, n;
4599 isl_pw_multi_aff *res = NULL;
4601 if (!pma || !pa)
4602 goto error;
4604 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4605 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4606 "domains don't match", goto error);
4607 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4608 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4609 "index out of bounds", goto error);
4611 n = pma->n * pa->n;
4612 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4614 for (i = 0; i < pma->n; ++i) {
4615 for (j = 0; j < pa->n; ++j) {
4616 isl_set *common;
4617 isl_multi_aff *res_ij;
4618 int empty;
4620 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4621 isl_set_copy(pa->p[j].set));
4622 empty = isl_set_plain_is_empty(common);
4623 if (empty < 0 || empty) {
4624 isl_set_free(common);
4625 if (empty < 0)
4626 goto error;
4627 continue;
4630 res_ij = isl_multi_aff_set_aff(
4631 isl_multi_aff_copy(pma->p[i].maff), pos,
4632 isl_aff_copy(pa->p[j].aff));
4633 res_ij = isl_multi_aff_gist(res_ij,
4634 isl_set_copy(common));
4636 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4640 isl_pw_multi_aff_free(pma);
4641 isl_pw_aff_free(pa);
4642 return res;
4643 error:
4644 isl_pw_multi_aff_free(pma);
4645 isl_pw_aff_free(pa);
4646 return isl_pw_multi_aff_free(res);
4649 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4651 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4652 __isl_take isl_pw_multi_aff *pma, unsigned pos,
4653 __isl_take isl_pw_aff *pa)
4655 if (!pma || !pa)
4656 goto error;
4657 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4658 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4659 if (!isl_space_has_named_params(pma->dim) ||
4660 !isl_space_has_named_params(pa->dim))
4661 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4662 "unaligned unnamed parameters", goto error);
4663 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4664 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4665 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4666 error:
4667 isl_pw_multi_aff_free(pma);
4668 isl_pw_aff_free(pa);
4669 return NULL;
4672 #undef BASE
4673 #define BASE pw_aff
4675 #include <isl_multi_templ.c>
4677 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4679 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4680 __isl_take isl_vec *vec)
4682 int i, n;
4683 isl_int v;
4685 if (!ma || !vec)
4686 goto error;
4688 n = isl_multi_aff_dim(ma, isl_dim_out);
4689 if (isl_vec_size(vec) < n)
4690 n = isl_vec_size(vec);
4692 isl_int_init(v);
4693 for (i = 0; i < n; ++i) {
4694 isl_aff *aff;
4696 isl_vec_get_element(vec, i, &v);
4698 aff = isl_multi_aff_get_aff(ma, i);
4699 aff = isl_aff_scale(aff, v);
4700 ma = isl_multi_aff_set_aff(ma, i, aff);
4702 isl_int_clear(v);
4704 isl_vec_free(vec);
4705 return ma;
4706 error:
4707 isl_vec_free(vec);
4708 isl_multi_aff_free(ma);
4709 return NULL;
4712 /* Scale the first elements of "pma" by the corresponding elements of "vec".
4714 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
4715 __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
4717 int i;
4719 pma = isl_pw_multi_aff_cow(pma);
4720 if (!pma || !v)
4721 goto error;
4723 for (i = 0; i < pma->n; ++i) {
4724 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
4725 isl_vec_copy(v));
4726 if (!pma->p[i].maff)
4727 goto error;
4730 isl_vec_free(v);
4731 return pma;
4732 error:
4733 isl_vec_free(v);
4734 isl_pw_multi_aff_free(pma);
4735 return NULL;
4738 /* This function is called for each entry of an isl_union_pw_multi_aff.
4739 * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
4740 * to the original entry with the isl_vec in "user" as extra argument.
4742 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
4744 isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
4745 isl_vec *v = user;
4747 *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
4748 if (!*pma)
4749 return -1;
4751 return 0;
4754 /* Scale the first elements of "upma" by the corresponding elements of "vec".
4756 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
4757 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
4759 upma = isl_union_pw_multi_aff_cow(upma);
4760 if (!upma || !v)
4761 goto error;
4763 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
4764 &union_pw_multi_aff_scale_vec_entry, v) < 0)
4765 goto error;
4767 isl_vec_free(v);
4768 return upma;
4769 error:
4770 isl_vec_free(v);
4771 isl_union_pw_multi_aff_free(upma);
4772 return NULL;