isl_schedule_node.c: collect_filter_prefix: allow caller to initialize filter
[isl.git] / isl_aff.c
blob28cd15745d64a0bec1460a1b0fd918ec0a740b00
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
230 if (!aff)
231 return NULL;
233 aff->ref++;
234 return aff;
237 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
239 if (!aff)
240 return NULL;
242 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
243 isl_vec_copy(aff->v));
246 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
248 if (!aff)
249 return NULL;
251 if (aff->ref == 1)
252 return aff;
253 aff->ref--;
254 return isl_aff_dup(aff);
257 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
259 if (!aff)
260 return NULL;
262 if (--aff->ref > 0)
263 return NULL;
265 isl_local_space_free(aff->ls);
266 isl_vec_free(aff->v);
268 free(aff);
270 return NULL;
273 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
275 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
278 /* Externally, an isl_aff has a map space, but internally, the
279 * ls field corresponds to the domain of that space.
281 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
283 if (!aff)
284 return 0;
285 if (type == isl_dim_out)
286 return 1;
287 if (type == isl_dim_in)
288 type = isl_dim_set;
289 return isl_local_space_dim(aff->ls, type);
292 /* Return the position of the dimension of the given type and name
293 * in "aff".
294 * Return -1 if no such dimension can be found.
296 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
297 const char *name)
299 if (!aff)
300 return -1;
301 if (type == isl_dim_out)
302 return -1;
303 if (type == isl_dim_in)
304 type = isl_dim_set;
305 return isl_local_space_find_dim_by_name(aff->ls, type, name);
308 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
310 return aff ? isl_local_space_get_space(aff->ls) : NULL;
313 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
315 isl_space *space;
316 if (!aff)
317 return NULL;
318 space = isl_local_space_get_space(aff->ls);
319 space = isl_space_from_domain(space);
320 space = isl_space_add_dims(space, isl_dim_out, 1);
321 return space;
324 __isl_give isl_local_space *isl_aff_get_domain_local_space(
325 __isl_keep isl_aff *aff)
327 return aff ? isl_local_space_copy(aff->ls) : NULL;
330 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
332 isl_local_space *ls;
333 if (!aff)
334 return NULL;
335 ls = isl_local_space_copy(aff->ls);
336 ls = isl_local_space_from_domain(ls);
337 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
338 return ls;
341 /* Externally, an isl_aff has a map space, but internally, the
342 * ls field corresponds to the domain of that space.
344 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
345 enum isl_dim_type type, unsigned pos)
347 if (!aff)
348 return NULL;
349 if (type == isl_dim_out)
350 return NULL;
351 if (type == isl_dim_in)
352 type = isl_dim_set;
353 return isl_local_space_get_dim_name(aff->ls, type, pos);
356 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
357 __isl_take isl_space *dim)
359 aff = isl_aff_cow(aff);
360 if (!aff || !dim)
361 goto error;
363 aff->ls = isl_local_space_reset_space(aff->ls, dim);
364 if (!aff->ls)
365 return isl_aff_free(aff);
367 return aff;
368 error:
369 isl_aff_free(aff);
370 isl_space_free(dim);
371 return NULL;
374 /* Reset the space of "aff". This function is called from isl_pw_templ.c
375 * and doesn't know if the space of an element object is represented
376 * directly or through its domain. It therefore passes along both.
378 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
379 __isl_take isl_space *space, __isl_take isl_space *domain)
381 isl_space_free(space);
382 return isl_aff_reset_domain_space(aff, domain);
385 /* Reorder the coefficients of the affine expression based
386 * on the given reodering.
387 * The reordering r is assumed to have been extended with the local
388 * variables.
390 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
391 __isl_take isl_reordering *r, int n_div)
393 isl_vec *res;
394 int i;
396 if (!vec || !r)
397 goto error;
399 res = isl_vec_alloc(vec->ctx,
400 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
401 isl_seq_cpy(res->el, vec->el, 2);
402 isl_seq_clr(res->el + 2, res->size - 2);
403 for (i = 0; i < r->len; ++i)
404 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
406 isl_reordering_free(r);
407 isl_vec_free(vec);
408 return res;
409 error:
410 isl_vec_free(vec);
411 isl_reordering_free(r);
412 return NULL;
415 /* Reorder the dimensions of the domain of "aff" according
416 * to the given reordering.
418 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
419 __isl_take isl_reordering *r)
421 aff = isl_aff_cow(aff);
422 if (!aff)
423 goto error;
425 r = isl_reordering_extend(r, aff->ls->div->n_row);
426 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
427 aff->ls->div->n_row);
428 aff->ls = isl_local_space_realign(aff->ls, r);
430 if (!aff->v || !aff->ls)
431 return isl_aff_free(aff);
433 return aff;
434 error:
435 isl_aff_free(aff);
436 isl_reordering_free(r);
437 return NULL;
440 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
441 __isl_take isl_space *model)
443 if (!aff || !model)
444 goto error;
446 if (!isl_space_match(aff->ls->dim, isl_dim_param,
447 model, isl_dim_param)) {
448 isl_reordering *exp;
450 model = isl_space_drop_dims(model, isl_dim_in,
451 0, isl_space_dim(model, isl_dim_in));
452 model = isl_space_drop_dims(model, isl_dim_out,
453 0, isl_space_dim(model, isl_dim_out));
454 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
455 exp = isl_reordering_extend_space(exp,
456 isl_aff_get_domain_space(aff));
457 aff = isl_aff_realign_domain(aff, exp);
460 isl_space_free(model);
461 return aff;
462 error:
463 isl_space_free(model);
464 isl_aff_free(aff);
465 return NULL;
468 /* Is "aff" obviously equal to zero?
470 * If the denominator is zero, then "aff" is not equal to zero.
472 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
474 if (!aff)
475 return -1;
477 if (isl_int_is_zero(aff->v->el[0]))
478 return 0;
479 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
482 /* Does "aff" represent NaN?
484 int isl_aff_is_nan(__isl_keep isl_aff *aff)
486 if (!aff)
487 return -1;
489 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
492 /* Does "pa" involve any NaNs?
494 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
496 int i;
498 if (!pa)
499 return -1;
500 if (pa->n == 0)
501 return 0;
503 for (i = 0; i < pa->n; ++i) {
504 int is_nan = isl_aff_is_nan(pa->p[i].aff);
505 if (is_nan < 0 || is_nan)
506 return is_nan;
509 return 0;
512 /* Are "aff1" and "aff2" obviously equal?
514 * NaN is not equal to anything, not even to another NaN.
516 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
518 int equal;
520 if (!aff1 || !aff2)
521 return -1;
523 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
524 return 0;
526 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
527 if (equal < 0 || !equal)
528 return equal;
530 return isl_vec_is_equal(aff1->v, aff2->v);
533 /* Return the common denominator of "aff" in "v".
535 * We cannot return anything meaningful in case of a NaN.
537 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
539 if (!aff)
540 return -1;
541 if (isl_aff_is_nan(aff))
542 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
543 "cannot get denominator of NaN", return -1);
544 isl_int_set(*v, aff->v->el[0]);
545 return 0;
548 /* Return the common denominator of "aff".
550 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
552 isl_ctx *ctx;
554 if (!aff)
555 return NULL;
557 ctx = isl_aff_get_ctx(aff);
558 if (isl_aff_is_nan(aff))
559 return isl_val_nan(ctx);
560 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
563 /* Return the constant term of "aff" in "v".
565 * We cannot return anything meaningful in case of a NaN.
567 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
569 if (!aff)
570 return -1;
571 if (isl_aff_is_nan(aff))
572 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
573 "cannot get constant term of NaN", return -1);
574 isl_int_set(*v, aff->v->el[1]);
575 return 0;
578 /* Return the constant term of "aff".
580 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
582 isl_ctx *ctx;
583 isl_val *v;
585 if (!aff)
586 return NULL;
588 ctx = isl_aff_get_ctx(aff);
589 if (isl_aff_is_nan(aff))
590 return isl_val_nan(ctx);
591 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
592 return isl_val_normalize(v);
595 /* Return the coefficient of the variable of type "type" at position "pos"
596 * of "aff" in "v".
598 * We cannot return anything meaningful in case of a NaN.
600 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
601 enum isl_dim_type type, int pos, isl_int *v)
603 if (!aff)
604 return -1;
606 if (type == isl_dim_out)
607 isl_die(aff->v->ctx, isl_error_invalid,
608 "output/set dimension does not have a coefficient",
609 return -1);
610 if (type == isl_dim_in)
611 type = isl_dim_set;
613 if (pos >= isl_local_space_dim(aff->ls, type))
614 isl_die(aff->v->ctx, isl_error_invalid,
615 "position out of bounds", return -1);
617 if (isl_aff_is_nan(aff))
618 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
619 "cannot get coefficient of NaN", return -1);
620 pos += isl_local_space_offset(aff->ls, type);
621 isl_int_set(*v, aff->v->el[1 + pos]);
623 return 0;
626 /* Return the coefficient of the variable of type "type" at position "pos"
627 * of "aff".
629 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
630 enum isl_dim_type type, int pos)
632 isl_ctx *ctx;
633 isl_val *v;
635 if (!aff)
636 return NULL;
638 ctx = isl_aff_get_ctx(aff);
639 if (type == isl_dim_out)
640 isl_die(ctx, isl_error_invalid,
641 "output/set dimension does not have a coefficient",
642 return NULL);
643 if (type == isl_dim_in)
644 type = isl_dim_set;
646 if (pos >= isl_local_space_dim(aff->ls, type))
647 isl_die(ctx, isl_error_invalid,
648 "position out of bounds", return NULL);
650 if (isl_aff_is_nan(aff))
651 return isl_val_nan(ctx);
652 pos += isl_local_space_offset(aff->ls, type);
653 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
654 return isl_val_normalize(v);
657 /* Return the sign of the coefficient of the variable of type "type"
658 * at position "pos" of "aff".
660 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
661 int pos)
663 isl_ctx *ctx;
665 if (!aff)
666 return 0;
668 ctx = isl_aff_get_ctx(aff);
669 if (type == isl_dim_out)
670 isl_die(ctx, isl_error_invalid,
671 "output/set dimension does not have a coefficient",
672 return 0);
673 if (type == isl_dim_in)
674 type = isl_dim_set;
676 if (pos >= isl_local_space_dim(aff->ls, type))
677 isl_die(ctx, isl_error_invalid,
678 "position out of bounds", return 0);
680 pos += isl_local_space_offset(aff->ls, type);
681 return isl_int_sgn(aff->v->el[1 + pos]);
684 /* Replace the denominator of "aff" by "v".
686 * A NaN is unaffected by this operation.
688 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
690 if (!aff)
691 return NULL;
692 if (isl_aff_is_nan(aff))
693 return aff;
694 aff = isl_aff_cow(aff);
695 if (!aff)
696 return NULL;
698 aff->v = isl_vec_cow(aff->v);
699 if (!aff->v)
700 return isl_aff_free(aff);
702 isl_int_set(aff->v->el[0], v);
704 return aff;
707 /* Replace the numerator of the constant term of "aff" by "v".
709 * A NaN is unaffected by this operation.
711 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
713 if (!aff)
714 return NULL;
715 if (isl_aff_is_nan(aff))
716 return aff;
717 aff = isl_aff_cow(aff);
718 if (!aff)
719 return NULL;
721 aff->v = isl_vec_cow(aff->v);
722 if (!aff->v)
723 return isl_aff_free(aff);
725 isl_int_set(aff->v->el[1], v);
727 return aff;
730 /* Replace the constant term of "aff" by "v".
732 * A NaN is unaffected by this operation.
734 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
735 __isl_take isl_val *v)
737 if (!aff || !v)
738 goto error;
740 if (isl_aff_is_nan(aff)) {
741 isl_val_free(v);
742 return aff;
745 if (!isl_val_is_rat(v))
746 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
747 "expecting rational value", goto error);
749 if (isl_int_eq(aff->v->el[1], v->n) &&
750 isl_int_eq(aff->v->el[0], v->d)) {
751 isl_val_free(v);
752 return aff;
755 aff = isl_aff_cow(aff);
756 if (!aff)
757 goto error;
758 aff->v = isl_vec_cow(aff->v);
759 if (!aff->v)
760 goto error;
762 if (isl_int_eq(aff->v->el[0], v->d)) {
763 isl_int_set(aff->v->el[1], v->n);
764 } else if (isl_int_is_one(v->d)) {
765 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
766 } else {
767 isl_seq_scale(aff->v->el + 1,
768 aff->v->el + 1, v->d, aff->v->size - 1);
769 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
770 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
771 aff->v = isl_vec_normalize(aff->v);
772 if (!aff->v)
773 goto error;
776 isl_val_free(v);
777 return aff;
778 error:
779 isl_aff_free(aff);
780 isl_val_free(v);
781 return NULL;
784 /* Add "v" to the constant term of "aff".
786 * A NaN is unaffected by this operation.
788 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
790 if (isl_int_is_zero(v))
791 return aff;
793 if (!aff)
794 return NULL;
795 if (isl_aff_is_nan(aff))
796 return aff;
797 aff = isl_aff_cow(aff);
798 if (!aff)
799 return NULL;
801 aff->v = isl_vec_cow(aff->v);
802 if (!aff->v)
803 return isl_aff_free(aff);
805 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
807 return aff;
810 /* Add "v" to the constant term of "aff".
812 * A NaN is unaffected by this operation.
814 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
815 __isl_take isl_val *v)
817 if (!aff || !v)
818 goto error;
820 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
821 isl_val_free(v);
822 return aff;
825 if (!isl_val_is_rat(v))
826 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
827 "expecting rational value", goto error);
829 aff = isl_aff_cow(aff);
830 if (!aff)
831 goto error;
833 aff->v = isl_vec_cow(aff->v);
834 if (!aff->v)
835 goto error;
837 if (isl_int_is_one(v->d)) {
838 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
839 } else if (isl_int_eq(aff->v->el[0], v->d)) {
840 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
841 aff->v = isl_vec_normalize(aff->v);
842 if (!aff->v)
843 goto error;
844 } else {
845 isl_seq_scale(aff->v->el + 1,
846 aff->v->el + 1, v->d, aff->v->size - 1);
847 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
848 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
849 aff->v = isl_vec_normalize(aff->v);
850 if (!aff->v)
851 goto error;
854 isl_val_free(v);
855 return aff;
856 error:
857 isl_aff_free(aff);
858 isl_val_free(v);
859 return NULL;
862 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
864 isl_int t;
866 isl_int_init(t);
867 isl_int_set_si(t, v);
868 aff = isl_aff_add_constant(aff, t);
869 isl_int_clear(t);
871 return aff;
874 /* Add "v" to the numerator of the constant term of "aff".
876 * A NaN is unaffected by this operation.
878 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
880 if (isl_int_is_zero(v))
881 return aff;
883 if (!aff)
884 return NULL;
885 if (isl_aff_is_nan(aff))
886 return aff;
887 aff = isl_aff_cow(aff);
888 if (!aff)
889 return NULL;
891 aff->v = isl_vec_cow(aff->v);
892 if (!aff->v)
893 return isl_aff_free(aff);
895 isl_int_add(aff->v->el[1], aff->v->el[1], v);
897 return aff;
900 /* Add "v" to the numerator of the constant term of "aff".
902 * A NaN is unaffected by this operation.
904 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
906 isl_int t;
908 if (v == 0)
909 return aff;
911 isl_int_init(t);
912 isl_int_set_si(t, v);
913 aff = isl_aff_add_constant_num(aff, t);
914 isl_int_clear(t);
916 return aff;
919 /* Replace the numerator of the constant term of "aff" by "v".
921 * A NaN is unaffected by this operation.
923 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
925 if (!aff)
926 return NULL;
927 if (isl_aff_is_nan(aff))
928 return aff;
929 aff = isl_aff_cow(aff);
930 if (!aff)
931 return NULL;
933 aff->v = isl_vec_cow(aff->v);
934 if (!aff->v)
935 return isl_aff_free(aff);
937 isl_int_set_si(aff->v->el[1], v);
939 return aff;
942 /* Replace the numerator of the coefficient of the variable of type "type"
943 * at position "pos" of "aff" by "v".
945 * A NaN is unaffected by this operation.
947 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
948 enum isl_dim_type type, int pos, isl_int v)
950 if (!aff)
951 return NULL;
953 if (type == isl_dim_out)
954 isl_die(aff->v->ctx, isl_error_invalid,
955 "output/set dimension does not have a coefficient",
956 return isl_aff_free(aff));
957 if (type == isl_dim_in)
958 type = isl_dim_set;
960 if (pos >= isl_local_space_dim(aff->ls, type))
961 isl_die(aff->v->ctx, isl_error_invalid,
962 "position out of bounds", return isl_aff_free(aff));
964 if (isl_aff_is_nan(aff))
965 return aff;
966 aff = isl_aff_cow(aff);
967 if (!aff)
968 return NULL;
970 aff->v = isl_vec_cow(aff->v);
971 if (!aff->v)
972 return isl_aff_free(aff);
974 pos += isl_local_space_offset(aff->ls, type);
975 isl_int_set(aff->v->el[1 + pos], v);
977 return aff;
980 /* Replace the numerator of the coefficient of the variable of type "type"
981 * at position "pos" of "aff" by "v".
983 * A NaN is unaffected by this operation.
985 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
986 enum isl_dim_type type, int pos, int v)
988 if (!aff)
989 return NULL;
991 if (type == isl_dim_out)
992 isl_die(aff->v->ctx, isl_error_invalid,
993 "output/set dimension does not have a coefficient",
994 return isl_aff_free(aff));
995 if (type == isl_dim_in)
996 type = isl_dim_set;
998 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
999 isl_die(aff->v->ctx, isl_error_invalid,
1000 "position out of bounds", return isl_aff_free(aff));
1002 if (isl_aff_is_nan(aff))
1003 return aff;
1004 pos += isl_local_space_offset(aff->ls, type);
1005 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1006 return aff;
1008 aff = isl_aff_cow(aff);
1009 if (!aff)
1010 return NULL;
1012 aff->v = isl_vec_cow(aff->v);
1013 if (!aff->v)
1014 return isl_aff_free(aff);
1016 isl_int_set_si(aff->v->el[1 + pos], v);
1018 return aff;
1021 /* Replace the coefficient of the variable of type "type" at position "pos"
1022 * of "aff" by "v".
1024 * A NaN is unaffected by this operation.
1026 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1027 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1029 if (!aff || !v)
1030 goto error;
1032 if (type == isl_dim_out)
1033 isl_die(aff->v->ctx, isl_error_invalid,
1034 "output/set dimension does not have a coefficient",
1035 goto error);
1036 if (type == isl_dim_in)
1037 type = isl_dim_set;
1039 if (pos >= isl_local_space_dim(aff->ls, type))
1040 isl_die(aff->v->ctx, isl_error_invalid,
1041 "position out of bounds", goto error);
1043 if (isl_aff_is_nan(aff)) {
1044 isl_val_free(v);
1045 return aff;
1047 if (!isl_val_is_rat(v))
1048 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1049 "expecting rational value", goto error);
1051 pos += isl_local_space_offset(aff->ls, type);
1052 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1053 isl_int_eq(aff->v->el[0], v->d)) {
1054 isl_val_free(v);
1055 return aff;
1058 aff = isl_aff_cow(aff);
1059 if (!aff)
1060 goto error;
1061 aff->v = isl_vec_cow(aff->v);
1062 if (!aff->v)
1063 goto error;
1065 if (isl_int_eq(aff->v->el[0], v->d)) {
1066 isl_int_set(aff->v->el[1 + pos], v->n);
1067 } else if (isl_int_is_one(v->d)) {
1068 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1069 } else {
1070 isl_seq_scale(aff->v->el + 1,
1071 aff->v->el + 1, v->d, aff->v->size - 1);
1072 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1073 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1074 aff->v = isl_vec_normalize(aff->v);
1075 if (!aff->v)
1076 goto error;
1079 isl_val_free(v);
1080 return aff;
1081 error:
1082 isl_aff_free(aff);
1083 isl_val_free(v);
1084 return NULL;
1087 /* Add "v" to the coefficient of the variable of type "type"
1088 * at position "pos" of "aff".
1090 * A NaN is unaffected by this operation.
1092 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1093 enum isl_dim_type type, int pos, isl_int v)
1095 if (!aff)
1096 return NULL;
1098 if (type == isl_dim_out)
1099 isl_die(aff->v->ctx, isl_error_invalid,
1100 "output/set dimension does not have a coefficient",
1101 return isl_aff_free(aff));
1102 if (type == isl_dim_in)
1103 type = isl_dim_set;
1105 if (pos >= isl_local_space_dim(aff->ls, type))
1106 isl_die(aff->v->ctx, isl_error_invalid,
1107 "position out of bounds", return isl_aff_free(aff));
1109 if (isl_aff_is_nan(aff))
1110 return aff;
1111 aff = isl_aff_cow(aff);
1112 if (!aff)
1113 return NULL;
1115 aff->v = isl_vec_cow(aff->v);
1116 if (!aff->v)
1117 return isl_aff_free(aff);
1119 pos += isl_local_space_offset(aff->ls, type);
1120 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1122 return aff;
1125 /* Add "v" to the coefficient of the variable of type "type"
1126 * at position "pos" of "aff".
1128 * A NaN is unaffected by this operation.
1130 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1131 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1133 if (!aff || !v)
1134 goto error;
1136 if (isl_val_is_zero(v)) {
1137 isl_val_free(v);
1138 return aff;
1141 if (type == isl_dim_out)
1142 isl_die(aff->v->ctx, isl_error_invalid,
1143 "output/set dimension does not have a coefficient",
1144 goto error);
1145 if (type == isl_dim_in)
1146 type = isl_dim_set;
1148 if (pos >= isl_local_space_dim(aff->ls, type))
1149 isl_die(aff->v->ctx, isl_error_invalid,
1150 "position out of bounds", goto error);
1152 if (isl_aff_is_nan(aff)) {
1153 isl_val_free(v);
1154 return aff;
1156 if (!isl_val_is_rat(v))
1157 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1158 "expecting rational value", goto error);
1160 aff = isl_aff_cow(aff);
1161 if (!aff)
1162 goto error;
1164 aff->v = isl_vec_cow(aff->v);
1165 if (!aff->v)
1166 goto error;
1168 pos += isl_local_space_offset(aff->ls, type);
1169 if (isl_int_is_one(v->d)) {
1170 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1171 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1172 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1173 aff->v = isl_vec_normalize(aff->v);
1174 if (!aff->v)
1175 goto error;
1176 } else {
1177 isl_seq_scale(aff->v->el + 1,
1178 aff->v->el + 1, v->d, aff->v->size - 1);
1179 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1180 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1181 aff->v = isl_vec_normalize(aff->v);
1182 if (!aff->v)
1183 goto error;
1186 isl_val_free(v);
1187 return aff;
1188 error:
1189 isl_aff_free(aff);
1190 isl_val_free(v);
1191 return NULL;
1194 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1195 enum isl_dim_type type, int pos, int v)
1197 isl_int t;
1199 isl_int_init(t);
1200 isl_int_set_si(t, v);
1201 aff = isl_aff_add_coefficient(aff, type, pos, t);
1202 isl_int_clear(t);
1204 return aff;
1207 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1209 if (!aff)
1210 return NULL;
1212 return isl_local_space_get_div(aff->ls, pos);
1215 /* Return the negation of "aff".
1217 * As a special case, -NaN = NaN.
1219 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1221 if (!aff)
1222 return NULL;
1223 if (isl_aff_is_nan(aff))
1224 return aff;
1225 aff = isl_aff_cow(aff);
1226 if (!aff)
1227 return NULL;
1228 aff->v = isl_vec_cow(aff->v);
1229 if (!aff->v)
1230 return isl_aff_free(aff);
1232 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1234 return aff;
1237 /* Remove divs from the local space that do not appear in the affine
1238 * expression.
1239 * We currently only remove divs at the end.
1240 * Some intermediate divs may also not appear directly in the affine
1241 * expression, but we would also need to check that no other divs are
1242 * defined in terms of them.
1244 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1246 int pos;
1247 int off;
1248 int n;
1250 if (!aff)
1251 return NULL;
1253 n = isl_local_space_dim(aff->ls, isl_dim_div);
1254 off = isl_local_space_offset(aff->ls, isl_dim_div);
1256 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1257 if (pos == n)
1258 return aff;
1260 aff = isl_aff_cow(aff);
1261 if (!aff)
1262 return NULL;
1264 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1265 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1266 if (!aff->ls || !aff->v)
1267 return isl_aff_free(aff);
1269 return aff;
1272 /* Given two affine expressions "p" of length p_len (including the
1273 * denominator and the constant term) and "subs" of length subs_len,
1274 * plug in "subs" for the variable at position "pos".
1275 * The variables of "subs" and "p" are assumed to match up to subs_len,
1276 * but "p" may have additional variables.
1277 * "v" is an initialized isl_int that can be used internally.
1279 * In particular, if "p" represents the expression
1281 * (a i + g)/m
1283 * with i the variable at position "pos" and "subs" represents the expression
1285 * f/d
1287 * then the result represents the expression
1289 * (a f + d g)/(m d)
1292 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1293 int p_len, int subs_len, isl_int v)
1295 isl_int_set(v, p[1 + pos]);
1296 isl_int_set_si(p[1 + pos], 0);
1297 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1298 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1299 isl_int_mul(p[0], p[0], subs[0]);
1302 /* Look for any divs in the aff->ls with a denominator equal to one
1303 * and plug them into the affine expression and any subsequent divs
1304 * that may reference the div.
1306 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1308 int i, n;
1309 int len;
1310 isl_int v;
1311 isl_vec *vec;
1312 isl_local_space *ls;
1313 unsigned pos;
1315 if (!aff)
1316 return NULL;
1318 n = isl_local_space_dim(aff->ls, isl_dim_div);
1319 len = aff->v->size;
1320 for (i = 0; i < n; ++i) {
1321 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1322 continue;
1323 ls = isl_local_space_copy(aff->ls);
1324 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1325 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1326 vec = isl_vec_copy(aff->v);
1327 vec = isl_vec_cow(vec);
1328 if (!ls || !vec)
1329 goto error;
1331 isl_int_init(v);
1333 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1334 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1335 len, len, v);
1337 isl_int_clear(v);
1339 isl_vec_free(aff->v);
1340 aff->v = vec;
1341 isl_local_space_free(aff->ls);
1342 aff->ls = ls;
1345 return aff;
1346 error:
1347 isl_vec_free(vec);
1348 isl_local_space_free(ls);
1349 return isl_aff_free(aff);
1352 /* Look for any divs j that appear with a unit coefficient inside
1353 * the definitions of other divs i and plug them into the definitions
1354 * of the divs i.
1356 * In particular, an expression of the form
1358 * floor((f(..) + floor(g(..)/n))/m)
1360 * is simplified to
1362 * floor((n * f(..) + g(..))/(n * m))
1364 * This simplification is correct because we can move the expression
1365 * f(..) into the inner floor in the original expression to obtain
1367 * floor(floor((n * f(..) + g(..))/n)/m)
1369 * from which we can derive the simplified expression.
1371 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1373 int i, j, n;
1374 int off;
1376 if (!aff)
1377 return NULL;
1379 n = isl_local_space_dim(aff->ls, isl_dim_div);
1380 off = isl_local_space_offset(aff->ls, isl_dim_div);
1381 for (i = 1; i < n; ++i) {
1382 for (j = 0; j < i; ++j) {
1383 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1384 continue;
1385 aff->ls = isl_local_space_substitute_seq(aff->ls,
1386 isl_dim_div, j, aff->ls->div->row[j],
1387 aff->v->size, i, 1);
1388 if (!aff->ls)
1389 return isl_aff_free(aff);
1393 return aff;
1396 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1398 * Even though this function is only called on isl_affs with a single
1399 * reference, we are careful to only change aff->v and aff->ls together.
1401 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1403 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1404 isl_local_space *ls;
1405 isl_vec *v;
1407 ls = isl_local_space_copy(aff->ls);
1408 ls = isl_local_space_swap_div(ls, a, b);
1409 v = isl_vec_copy(aff->v);
1410 v = isl_vec_cow(v);
1411 if (!ls || !v)
1412 goto error;
1414 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1415 isl_vec_free(aff->v);
1416 aff->v = v;
1417 isl_local_space_free(aff->ls);
1418 aff->ls = ls;
1420 return aff;
1421 error:
1422 isl_vec_free(v);
1423 isl_local_space_free(ls);
1424 return isl_aff_free(aff);
1427 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1429 * We currently do not actually remove div "b", but simply add its
1430 * coefficient to that of "a" and then zero it out.
1432 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1434 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1436 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1437 return aff;
1439 aff->v = isl_vec_cow(aff->v);
1440 if (!aff->v)
1441 return isl_aff_free(aff);
1443 isl_int_add(aff->v->el[1 + off + a],
1444 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1445 isl_int_set_si(aff->v->el[1 + off + b], 0);
1447 return aff;
1450 /* Sort the divs in the local space of "aff" according to
1451 * the comparison function "cmp_row" in isl_local_space.c,
1452 * combining the coefficients of identical divs.
1454 * Reordering divs does not change the semantics of "aff",
1455 * so there is no need to call isl_aff_cow.
1456 * Moreover, this function is currently only called on isl_affs
1457 * with a single reference.
1459 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1461 int i, j, n;
1463 if (!aff)
1464 return NULL;
1466 n = isl_aff_dim(aff, isl_dim_div);
1467 for (i = 1; i < n; ++i) {
1468 for (j = i - 1; j >= 0; --j) {
1469 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1470 if (cmp < 0)
1471 break;
1472 if (cmp == 0)
1473 aff = merge_divs(aff, j, j + 1);
1474 else
1475 aff = swap_div(aff, j, j + 1);
1476 if (!aff)
1477 return NULL;
1481 return aff;
1484 /* Normalize the representation of "aff".
1486 * This function should only be called of "new" isl_affs, i.e.,
1487 * with only a single reference. We therefore do not need to
1488 * worry about affecting other instances.
1490 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1492 if (!aff)
1493 return NULL;
1494 aff->v = isl_vec_normalize(aff->v);
1495 if (!aff->v)
1496 return isl_aff_free(aff);
1497 aff = plug_in_integral_divs(aff);
1498 aff = plug_in_unit_divs(aff);
1499 aff = sort_divs(aff);
1500 aff = isl_aff_remove_unused_divs(aff);
1501 return aff;
1504 /* Given f, return floor(f).
1505 * If f is an integer expression, then just return f.
1506 * If f is a constant, then return the constant floor(f).
1507 * Otherwise, if f = g/m, write g = q m + r,
1508 * create a new div d = [r/m] and return the expression q + d.
1509 * The coefficients in r are taken to lie between -m/2 and m/2.
1511 * As a special case, floor(NaN) = NaN.
1513 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1515 int i;
1516 int size;
1517 isl_ctx *ctx;
1518 isl_vec *div;
1520 if (!aff)
1521 return NULL;
1523 if (isl_aff_is_nan(aff))
1524 return aff;
1525 if (isl_int_is_one(aff->v->el[0]))
1526 return aff;
1528 aff = isl_aff_cow(aff);
1529 if (!aff)
1530 return NULL;
1532 aff->v = isl_vec_cow(aff->v);
1533 if (!aff->v)
1534 return isl_aff_free(aff);
1536 if (isl_aff_is_cst(aff)) {
1537 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1538 isl_int_set_si(aff->v->el[0], 1);
1539 return aff;
1542 div = isl_vec_copy(aff->v);
1543 div = isl_vec_cow(div);
1544 if (!div)
1545 return isl_aff_free(aff);
1547 ctx = isl_aff_get_ctx(aff);
1548 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1549 for (i = 1; i < aff->v->size; ++i) {
1550 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1551 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1552 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1553 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1554 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1558 aff->ls = isl_local_space_add_div(aff->ls, div);
1559 if (!aff->ls)
1560 return isl_aff_free(aff);
1562 size = aff->v->size;
1563 aff->v = isl_vec_extend(aff->v, size + 1);
1564 if (!aff->v)
1565 return isl_aff_free(aff);
1566 isl_int_set_si(aff->v->el[0], 1);
1567 isl_int_set_si(aff->v->el[size], 1);
1569 aff = isl_aff_normalize(aff);
1571 return aff;
1574 /* Compute
1576 * aff mod m = aff - m * floor(aff/m)
1578 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1580 isl_aff *res;
1582 res = isl_aff_copy(aff);
1583 aff = isl_aff_scale_down(aff, m);
1584 aff = isl_aff_floor(aff);
1585 aff = isl_aff_scale(aff, m);
1586 res = isl_aff_sub(res, aff);
1588 return res;
1591 /* Compute
1593 * aff mod m = aff - m * floor(aff/m)
1595 * with m an integer value.
1597 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1598 __isl_take isl_val *m)
1600 isl_aff *res;
1602 if (!aff || !m)
1603 goto error;
1605 if (!isl_val_is_int(m))
1606 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1607 "expecting integer modulo", goto error);
1609 res = isl_aff_copy(aff);
1610 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1611 aff = isl_aff_floor(aff);
1612 aff = isl_aff_scale_val(aff, m);
1613 res = isl_aff_sub(res, aff);
1615 return res;
1616 error:
1617 isl_aff_free(aff);
1618 isl_val_free(m);
1619 return NULL;
1622 /* Compute
1624 * pwaff mod m = pwaff - m * floor(pwaff/m)
1626 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1628 isl_pw_aff *res;
1630 res = isl_pw_aff_copy(pwaff);
1631 pwaff = isl_pw_aff_scale_down(pwaff, m);
1632 pwaff = isl_pw_aff_floor(pwaff);
1633 pwaff = isl_pw_aff_scale(pwaff, m);
1634 res = isl_pw_aff_sub(res, pwaff);
1636 return res;
1639 /* Compute
1641 * pa mod m = pa - m * floor(pa/m)
1643 * with m an integer value.
1645 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1646 __isl_take isl_val *m)
1648 if (!pa || !m)
1649 goto error;
1650 if (!isl_val_is_int(m))
1651 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1652 "expecting integer modulo", goto error);
1653 pa = isl_pw_aff_mod(pa, m->n);
1654 isl_val_free(m);
1655 return pa;
1656 error:
1657 isl_pw_aff_free(pa);
1658 isl_val_free(m);
1659 return NULL;
1662 /* Given f, return ceil(f).
1663 * If f is an integer expression, then just return f.
1664 * Otherwise, let f be the expression
1666 * e/m
1668 * then return
1670 * floor((e + m - 1)/m)
1672 * As a special case, ceil(NaN) = NaN.
1674 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1676 if (!aff)
1677 return NULL;
1679 if (isl_aff_is_nan(aff))
1680 return aff;
1681 if (isl_int_is_one(aff->v->el[0]))
1682 return aff;
1684 aff = isl_aff_cow(aff);
1685 if (!aff)
1686 return NULL;
1687 aff->v = isl_vec_cow(aff->v);
1688 if (!aff->v)
1689 return isl_aff_free(aff);
1691 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1692 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1693 aff = isl_aff_floor(aff);
1695 return aff;
1698 /* Apply the expansion computed by isl_merge_divs.
1699 * The expansion itself is given by "exp" while the resulting
1700 * list of divs is given by "div".
1702 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1703 __isl_take isl_mat *div, int *exp)
1705 int i, j;
1706 int old_n_div;
1707 int new_n_div;
1708 int offset;
1710 aff = isl_aff_cow(aff);
1711 if (!aff || !div)
1712 goto error;
1714 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1715 new_n_div = isl_mat_rows(div);
1716 if (new_n_div < old_n_div)
1717 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1718 "not an expansion", goto error);
1720 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1721 if (!aff->v)
1722 goto error;
1724 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1725 j = old_n_div - 1;
1726 for (i = new_n_div - 1; i >= 0; --i) {
1727 if (j >= 0 && exp[j] == i) {
1728 if (i != j)
1729 isl_int_swap(aff->v->el[offset + i],
1730 aff->v->el[offset + j]);
1731 j--;
1732 } else
1733 isl_int_set_si(aff->v->el[offset + i], 0);
1736 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1737 if (!aff->ls)
1738 goto error;
1739 isl_mat_free(div);
1740 return aff;
1741 error:
1742 isl_aff_free(aff);
1743 isl_mat_free(div);
1744 return NULL;
1747 /* Add two affine expressions that live in the same local space.
1749 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1750 __isl_take isl_aff *aff2)
1752 isl_int gcd, f;
1754 aff1 = isl_aff_cow(aff1);
1755 if (!aff1 || !aff2)
1756 goto error;
1758 aff1->v = isl_vec_cow(aff1->v);
1759 if (!aff1->v)
1760 goto error;
1762 isl_int_init(gcd);
1763 isl_int_init(f);
1764 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1765 isl_int_divexact(f, aff2->v->el[0], gcd);
1766 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1767 isl_int_divexact(f, aff1->v->el[0], gcd);
1768 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1769 isl_int_divexact(f, aff2->v->el[0], gcd);
1770 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1771 isl_int_clear(f);
1772 isl_int_clear(gcd);
1774 isl_aff_free(aff2);
1775 return aff1;
1776 error:
1777 isl_aff_free(aff1);
1778 isl_aff_free(aff2);
1779 return NULL;
1782 /* Return the sum of "aff1" and "aff2".
1784 * If either of the two is NaN, then the result is NaN.
1786 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1787 __isl_take isl_aff *aff2)
1789 isl_ctx *ctx;
1790 int *exp1 = NULL;
1791 int *exp2 = NULL;
1792 isl_mat *div;
1793 int n_div1, n_div2;
1795 if (!aff1 || !aff2)
1796 goto error;
1798 ctx = isl_aff_get_ctx(aff1);
1799 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1800 isl_die(ctx, isl_error_invalid,
1801 "spaces don't match", goto error);
1803 if (isl_aff_is_nan(aff1)) {
1804 isl_aff_free(aff2);
1805 return aff1;
1807 if (isl_aff_is_nan(aff2)) {
1808 isl_aff_free(aff1);
1809 return aff2;
1812 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1813 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1814 if (n_div1 == 0 && n_div2 == 0)
1815 return add_expanded(aff1, aff2);
1817 exp1 = isl_alloc_array(ctx, int, n_div1);
1818 exp2 = isl_alloc_array(ctx, int, n_div2);
1819 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1820 goto error;
1822 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1823 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1824 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1825 free(exp1);
1826 free(exp2);
1828 return add_expanded(aff1, aff2);
1829 error:
1830 free(exp1);
1831 free(exp2);
1832 isl_aff_free(aff1);
1833 isl_aff_free(aff2);
1834 return NULL;
1837 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1838 __isl_take isl_aff *aff2)
1840 return isl_aff_add(aff1, isl_aff_neg(aff2));
1843 /* Return the result of scaling "aff" by a factor of "f".
1845 * As a special case, f * NaN = NaN.
1847 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1849 isl_int gcd;
1851 if (!aff)
1852 return NULL;
1853 if (isl_aff_is_nan(aff))
1854 return aff;
1856 if (isl_int_is_one(f))
1857 return aff;
1859 aff = isl_aff_cow(aff);
1860 if (!aff)
1861 return NULL;
1862 aff->v = isl_vec_cow(aff->v);
1863 if (!aff->v)
1864 return isl_aff_free(aff);
1866 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1867 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1868 return aff;
1871 isl_int_init(gcd);
1872 isl_int_gcd(gcd, aff->v->el[0], f);
1873 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1874 isl_int_divexact(gcd, f, gcd);
1875 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1876 isl_int_clear(gcd);
1878 return aff;
1881 /* Multiple "aff" by "v".
1883 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1884 __isl_take isl_val *v)
1886 if (!aff || !v)
1887 goto error;
1889 if (isl_val_is_one(v)) {
1890 isl_val_free(v);
1891 return aff;
1894 if (!isl_val_is_rat(v))
1895 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1896 "expecting rational factor", goto error);
1898 aff = isl_aff_scale(aff, v->n);
1899 aff = isl_aff_scale_down(aff, v->d);
1901 isl_val_free(v);
1902 return aff;
1903 error:
1904 isl_aff_free(aff);
1905 isl_val_free(v);
1906 return NULL;
1909 /* Return the result of scaling "aff" down by a factor of "f".
1911 * As a special case, NaN/f = NaN.
1913 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1915 isl_int gcd;
1917 if (!aff)
1918 return NULL;
1919 if (isl_aff_is_nan(aff))
1920 return aff;
1922 if (isl_int_is_one(f))
1923 return aff;
1925 aff = isl_aff_cow(aff);
1926 if (!aff)
1927 return NULL;
1929 if (isl_int_is_zero(f))
1930 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1931 "cannot scale down by zero", return isl_aff_free(aff));
1933 aff->v = isl_vec_cow(aff->v);
1934 if (!aff->v)
1935 return isl_aff_free(aff);
1937 isl_int_init(gcd);
1938 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1939 isl_int_gcd(gcd, gcd, f);
1940 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1941 isl_int_divexact(gcd, f, gcd);
1942 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1943 isl_int_clear(gcd);
1945 return aff;
1948 /* Divide "aff" by "v".
1950 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1951 __isl_take isl_val *v)
1953 if (!aff || !v)
1954 goto error;
1956 if (isl_val_is_one(v)) {
1957 isl_val_free(v);
1958 return aff;
1961 if (!isl_val_is_rat(v))
1962 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1963 "expecting rational factor", goto error);
1964 if (!isl_val_is_pos(v))
1965 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1966 "factor needs to be positive", goto error);
1968 aff = isl_aff_scale(aff, v->d);
1969 aff = isl_aff_scale_down(aff, v->n);
1971 isl_val_free(v);
1972 return aff;
1973 error:
1974 isl_aff_free(aff);
1975 isl_val_free(v);
1976 return NULL;
1979 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1981 isl_int v;
1983 if (f == 1)
1984 return aff;
1986 isl_int_init(v);
1987 isl_int_set_ui(v, f);
1988 aff = isl_aff_scale_down(aff, v);
1989 isl_int_clear(v);
1991 return aff;
1994 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1995 enum isl_dim_type type, unsigned pos, const char *s)
1997 aff = isl_aff_cow(aff);
1998 if (!aff)
1999 return NULL;
2000 if (type == isl_dim_out)
2001 isl_die(aff->v->ctx, isl_error_invalid,
2002 "cannot set name of output/set dimension",
2003 return isl_aff_free(aff));
2004 if (type == isl_dim_in)
2005 type = isl_dim_set;
2006 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2007 if (!aff->ls)
2008 return isl_aff_free(aff);
2010 return aff;
2013 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2014 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2016 aff = isl_aff_cow(aff);
2017 if (!aff)
2018 goto error;
2019 if (type == isl_dim_out)
2020 isl_die(aff->v->ctx, isl_error_invalid,
2021 "cannot set name of output/set dimension",
2022 goto error);
2023 if (type == isl_dim_in)
2024 type = isl_dim_set;
2025 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2026 if (!aff->ls)
2027 return isl_aff_free(aff);
2029 return aff;
2030 error:
2031 isl_id_free(id);
2032 isl_aff_free(aff);
2033 return NULL;
2036 /* Replace the identifier of the input tuple of "aff" by "id".
2037 * type is currently required to be equal to isl_dim_in
2039 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2040 enum isl_dim_type type, __isl_take isl_id *id)
2042 aff = isl_aff_cow(aff);
2043 if (!aff)
2044 goto error;
2045 if (type != isl_dim_out)
2046 isl_die(aff->v->ctx, isl_error_invalid,
2047 "cannot only set id of input tuple", goto error);
2048 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2049 if (!aff->ls)
2050 return isl_aff_free(aff);
2052 return aff;
2053 error:
2054 isl_id_free(id);
2055 isl_aff_free(aff);
2056 return NULL;
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2061 * The integer divisions in this local space are assumed to appear
2062 * as regular dimensions in "eq".
2064 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2065 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2067 int i, j;
2068 unsigned total;
2069 unsigned n_div;
2071 if (!eq)
2072 goto error;
2073 if (eq->n_eq == 0) {
2074 isl_basic_set_free(eq);
2075 return aff;
2078 aff = isl_aff_cow(aff);
2079 if (!aff)
2080 goto error;
2082 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2083 isl_basic_set_copy(eq));
2084 aff->v = isl_vec_cow(aff->v);
2085 if (!aff->ls || !aff->v)
2086 goto error;
2088 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2089 n_div = eq->n_div;
2090 for (i = 0; i < eq->n_eq; ++i) {
2091 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2092 if (j < 0 || j == 0 || j >= total)
2093 continue;
2095 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2096 &aff->v->el[0]);
2099 isl_basic_set_free(eq);
2100 aff = isl_aff_normalize(aff);
2101 return aff;
2102 error:
2103 isl_basic_set_free(eq);
2104 isl_aff_free(aff);
2105 return NULL;
2108 /* Exploit the equalities in "eq" to simplify the affine expression
2109 * and the expressions of the integer divisions in the local space.
2111 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2112 __isl_take isl_basic_set *eq)
2114 int n_div;
2116 if (!aff || !eq)
2117 goto error;
2118 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2119 if (n_div > 0)
2120 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2121 return isl_aff_substitute_equalities_lifted(aff, eq);
2122 error:
2123 isl_basic_set_free(eq);
2124 isl_aff_free(aff);
2125 return NULL;
2128 /* Look for equalities among the variables shared by context and aff
2129 * and the integer divisions of aff, if any.
2130 * The equalities are then used to eliminate coefficients and/or integer
2131 * divisions from aff.
2133 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2134 __isl_take isl_set *context)
2136 isl_basic_set *hull;
2137 int n_div;
2139 if (!aff)
2140 goto error;
2141 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2142 if (n_div > 0) {
2143 isl_basic_set *bset;
2144 isl_local_space *ls;
2145 context = isl_set_add_dims(context, isl_dim_set, n_div);
2146 ls = isl_aff_get_domain_local_space(aff);
2147 bset = isl_basic_set_from_local_space(ls);
2148 bset = isl_basic_set_lift(bset);
2149 bset = isl_basic_set_flatten(bset);
2150 context = isl_set_intersect(context,
2151 isl_set_from_basic_set(bset));
2154 hull = isl_set_affine_hull(context);
2155 return isl_aff_substitute_equalities_lifted(aff, hull);
2156 error:
2157 isl_aff_free(aff);
2158 isl_set_free(context);
2159 return NULL;
2162 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2163 __isl_take isl_set *context)
2165 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2166 dom_context = isl_set_intersect_params(dom_context, context);
2167 return isl_aff_gist(aff, dom_context);
2170 /* Return a basic set containing those elements in the space
2171 * of aff where it is positive. "rational" should not be set.
2173 * If "aff" is NaN, then it is not positive.
2175 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2176 int rational)
2178 isl_constraint *ineq;
2179 isl_basic_set *bset;
2180 isl_val *c;
2182 if (!aff)
2183 return NULL;
2184 if (isl_aff_is_nan(aff)) {
2185 isl_space *space = isl_aff_get_domain_space(aff);
2186 isl_aff_free(aff);
2187 return isl_basic_set_empty(space);
2189 if (rational)
2190 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2191 "rational sets not supported", goto error);
2193 ineq = isl_inequality_from_aff(aff);
2194 c = isl_constraint_get_constant_val(ineq);
2195 c = isl_val_sub_ui(c, 1);
2196 ineq = isl_constraint_set_constant_val(ineq, c);
2198 bset = isl_basic_set_from_constraint(ineq);
2199 bset = isl_basic_set_simplify(bset);
2200 return bset;
2201 error:
2202 isl_aff_free(aff);
2203 return NULL;
2206 /* Return a basic set containing those elements in the space
2207 * of aff where it is non-negative.
2208 * If "rational" is set, then return a rational basic set.
2210 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2212 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2213 __isl_take isl_aff *aff, int rational)
2215 isl_constraint *ineq;
2216 isl_basic_set *bset;
2218 if (!aff)
2219 return NULL;
2220 if (isl_aff_is_nan(aff)) {
2221 isl_space *space = isl_aff_get_domain_space(aff);
2222 isl_aff_free(aff);
2223 return isl_basic_set_empty(space);
2226 ineq = isl_inequality_from_aff(aff);
2228 bset = isl_basic_set_from_constraint(ineq);
2229 if (rational)
2230 bset = isl_basic_set_set_rational(bset);
2231 bset = isl_basic_set_simplify(bset);
2232 return bset;
2235 /* Return a basic set containing those elements in the space
2236 * of aff where it is non-negative.
2238 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2240 return aff_nonneg_basic_set(aff, 0);
2243 /* Return a basic set containing those elements in the domain space
2244 * of aff where it is negative.
2246 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2248 aff = isl_aff_neg(aff);
2249 aff = isl_aff_add_constant_num_si(aff, -1);
2250 return isl_aff_nonneg_basic_set(aff);
2253 /* Return a basic set containing those elements in the space
2254 * of aff where it is zero.
2255 * If "rational" is set, then return a rational basic set.
2257 * If "aff" is NaN, then it is not zero.
2259 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2260 int rational)
2262 isl_constraint *ineq;
2263 isl_basic_set *bset;
2265 if (!aff)
2266 return NULL;
2267 if (isl_aff_is_nan(aff)) {
2268 isl_space *space = isl_aff_get_domain_space(aff);
2269 isl_aff_free(aff);
2270 return isl_basic_set_empty(space);
2273 ineq = isl_equality_from_aff(aff);
2275 bset = isl_basic_set_from_constraint(ineq);
2276 if (rational)
2277 bset = isl_basic_set_set_rational(bset);
2278 bset = isl_basic_set_simplify(bset);
2279 return bset;
2282 /* Return a basic set containing those elements in the space
2283 * of aff where it is zero.
2285 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2287 return aff_zero_basic_set(aff, 0);
2290 /* Return a basic set containing those elements in the shared space
2291 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2293 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2294 __isl_take isl_aff *aff2)
2296 aff1 = isl_aff_sub(aff1, aff2);
2298 return isl_aff_nonneg_basic_set(aff1);
2301 /* Return a basic set containing those elements in the shared space
2302 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2304 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2305 __isl_take isl_aff *aff2)
2307 return isl_aff_ge_basic_set(aff2, aff1);
2310 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2311 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2313 aff1 = isl_aff_add(aff1, aff2);
2314 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2315 return aff1;
2318 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2320 if (!aff)
2321 return -1;
2323 return 0;
2326 /* Check whether the given affine expression has non-zero coefficient
2327 * for any dimension in the given range or if any of these dimensions
2328 * appear with non-zero coefficients in any of the integer divisions
2329 * involved in the affine expression.
2331 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2332 enum isl_dim_type type, unsigned first, unsigned n)
2334 int i;
2335 isl_ctx *ctx;
2336 int *active = NULL;
2337 int involves = 0;
2339 if (!aff)
2340 return -1;
2341 if (n == 0)
2342 return 0;
2344 ctx = isl_aff_get_ctx(aff);
2345 if (first + n > isl_aff_dim(aff, type))
2346 isl_die(ctx, isl_error_invalid,
2347 "range out of bounds", return -1);
2349 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2350 if (!active)
2351 goto error;
2353 first += isl_local_space_offset(aff->ls, type) - 1;
2354 for (i = 0; i < n; ++i)
2355 if (active[first + i]) {
2356 involves = 1;
2357 break;
2360 free(active);
2362 return involves;
2363 error:
2364 free(active);
2365 return -1;
2368 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2369 enum isl_dim_type type, unsigned first, unsigned n)
2371 isl_ctx *ctx;
2373 if (!aff)
2374 return NULL;
2375 if (type == isl_dim_out)
2376 isl_die(aff->v->ctx, isl_error_invalid,
2377 "cannot drop output/set dimension",
2378 return isl_aff_free(aff));
2379 if (type == isl_dim_in)
2380 type = isl_dim_set;
2381 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2382 return aff;
2384 ctx = isl_aff_get_ctx(aff);
2385 if (first + n > isl_local_space_dim(aff->ls, type))
2386 isl_die(ctx, isl_error_invalid, "range out of bounds",
2387 return isl_aff_free(aff));
2389 aff = isl_aff_cow(aff);
2390 if (!aff)
2391 return NULL;
2393 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2394 if (!aff->ls)
2395 return isl_aff_free(aff);
2397 first += 1 + isl_local_space_offset(aff->ls, type);
2398 aff->v = isl_vec_drop_els(aff->v, first, n);
2399 if (!aff->v)
2400 return isl_aff_free(aff);
2402 return aff;
2405 /* Project the domain of the affine expression onto its parameter space.
2406 * The affine expression may not involve any of the domain dimensions.
2408 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2410 isl_space *space;
2411 unsigned n;
2412 int involves;
2414 n = isl_aff_dim(aff, isl_dim_in);
2415 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2416 if (involves < 0)
2417 return isl_aff_free(aff);
2418 if (involves)
2419 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2420 "affine expression involves some of the domain dimensions",
2421 return isl_aff_free(aff));
2422 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2423 space = isl_aff_get_domain_space(aff);
2424 space = isl_space_params(space);
2425 aff = isl_aff_reset_domain_space(aff, space);
2426 return aff;
2429 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2430 enum isl_dim_type type, unsigned first, unsigned n)
2432 isl_ctx *ctx;
2434 if (!aff)
2435 return NULL;
2436 if (type == isl_dim_out)
2437 isl_die(aff->v->ctx, isl_error_invalid,
2438 "cannot insert output/set dimensions",
2439 return isl_aff_free(aff));
2440 if (type == isl_dim_in)
2441 type = isl_dim_set;
2442 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2443 return aff;
2445 ctx = isl_aff_get_ctx(aff);
2446 if (first > isl_local_space_dim(aff->ls, type))
2447 isl_die(ctx, isl_error_invalid, "position out of bounds",
2448 return isl_aff_free(aff));
2450 aff = isl_aff_cow(aff);
2451 if (!aff)
2452 return NULL;
2454 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2455 if (!aff->ls)
2456 return isl_aff_free(aff);
2458 first += 1 + isl_local_space_offset(aff->ls, type);
2459 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2460 if (!aff->v)
2461 return isl_aff_free(aff);
2463 return aff;
2466 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2467 enum isl_dim_type type, unsigned n)
2469 unsigned pos;
2471 pos = isl_aff_dim(aff, type);
2473 return isl_aff_insert_dims(aff, type, pos, n);
2476 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2477 enum isl_dim_type type, unsigned n)
2479 unsigned pos;
2481 pos = isl_pw_aff_dim(pwaff, type);
2483 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2486 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2487 * to dimensions of "dst_type" at "dst_pos".
2489 * We only support moving input dimensions to parameters and vice versa.
2491 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2492 enum isl_dim_type dst_type, unsigned dst_pos,
2493 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2495 unsigned g_dst_pos;
2496 unsigned g_src_pos;
2498 if (!aff)
2499 return NULL;
2500 if (n == 0 &&
2501 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2502 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2503 return aff;
2505 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2506 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2507 "cannot move output/set dimension", isl_aff_free(aff));
2508 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2509 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2510 "cannot move divs", isl_aff_free(aff));
2511 if (dst_type == isl_dim_in)
2512 dst_type = isl_dim_set;
2513 if (src_type == isl_dim_in)
2514 src_type = isl_dim_set;
2516 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2517 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2518 "range out of bounds", isl_aff_free(aff));
2519 if (dst_type == src_type)
2520 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2521 "moving dims within the same type not supported",
2522 isl_aff_free(aff));
2524 aff = isl_aff_cow(aff);
2525 if (!aff)
2526 return NULL;
2528 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2529 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2530 if (dst_type > src_type)
2531 g_dst_pos -= n;
2533 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2534 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2535 src_type, src_pos, n);
2536 if (!aff->v || !aff->ls)
2537 return isl_aff_free(aff);
2539 aff = sort_divs(aff);
2541 return aff;
2544 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2546 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2547 return isl_pw_aff_alloc(dom, aff);
2550 #undef PW
2551 #define PW isl_pw_aff
2552 #undef EL
2553 #define EL isl_aff
2554 #undef EL_IS_ZERO
2555 #define EL_IS_ZERO is_empty
2556 #undef ZERO
2557 #define ZERO empty
2558 #undef IS_ZERO
2559 #define IS_ZERO is_empty
2560 #undef FIELD
2561 #define FIELD aff
2562 #undef DEFAULT_IS_ZERO
2563 #define DEFAULT_IS_ZERO 0
2565 #define NO_EVAL
2566 #define NO_OPT
2567 #define NO_LIFT
2568 #define NO_MORPH
2570 #include <isl_pw_templ.c>
2572 #undef UNION
2573 #define UNION isl_union_pw_aff
2574 #undef PART
2575 #define PART isl_pw_aff
2576 #undef PARTS
2577 #define PARTS pw_aff
2579 #define NO_EVAL
2581 #include <isl_union_templ.c>
2583 static __isl_give isl_set *align_params_pw_pw_set_and(
2584 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2585 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2586 __isl_take isl_pw_aff *pwaff2))
2588 if (!pwaff1 || !pwaff2)
2589 goto error;
2590 if (isl_space_match(pwaff1->dim, isl_dim_param,
2591 pwaff2->dim, isl_dim_param))
2592 return fn(pwaff1, pwaff2);
2593 if (!isl_space_has_named_params(pwaff1->dim) ||
2594 !isl_space_has_named_params(pwaff2->dim))
2595 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2596 "unaligned unnamed parameters", goto error);
2597 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2598 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2599 return fn(pwaff1, pwaff2);
2600 error:
2601 isl_pw_aff_free(pwaff1);
2602 isl_pw_aff_free(pwaff2);
2603 return NULL;
2606 /* Align the parameters of the to isl_pw_aff arguments and
2607 * then apply a function "fn" on them that returns an isl_map.
2609 static __isl_give isl_map *align_params_pw_pw_map_and(
2610 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2611 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2612 __isl_take isl_pw_aff *pa2))
2614 if (!pa1 || !pa2)
2615 goto error;
2616 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2617 return fn(pa1, pa2);
2618 if (!isl_space_has_named_params(pa1->dim) ||
2619 !isl_space_has_named_params(pa2->dim))
2620 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2621 "unaligned unnamed parameters", goto error);
2622 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2623 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2624 return fn(pa1, pa2);
2625 error:
2626 isl_pw_aff_free(pa1);
2627 isl_pw_aff_free(pa2);
2628 return NULL;
2631 /* Compute a piecewise quasi-affine expression with a domain that
2632 * is the union of those of pwaff1 and pwaff2 and such that on each
2633 * cell, the quasi-affine expression is the better (according to cmp)
2634 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2635 * is defined on a given cell, then the associated expression
2636 * is the defined one.
2638 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2639 __isl_take isl_pw_aff *pwaff2,
2640 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2641 __isl_take isl_aff *aff2))
2643 int i, j, n;
2644 isl_pw_aff *res;
2645 isl_ctx *ctx;
2646 isl_set *set;
2648 if (!pwaff1 || !pwaff2)
2649 goto error;
2651 ctx = isl_space_get_ctx(pwaff1->dim);
2652 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2653 isl_die(ctx, isl_error_invalid,
2654 "arguments should live in same space", goto error);
2656 if (isl_pw_aff_is_empty(pwaff1)) {
2657 isl_pw_aff_free(pwaff1);
2658 return pwaff2;
2661 if (isl_pw_aff_is_empty(pwaff2)) {
2662 isl_pw_aff_free(pwaff2);
2663 return pwaff1;
2666 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2667 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2669 for (i = 0; i < pwaff1->n; ++i) {
2670 set = isl_set_copy(pwaff1->p[i].set);
2671 for (j = 0; j < pwaff2->n; ++j) {
2672 struct isl_set *common;
2673 isl_set *better;
2675 common = isl_set_intersect(
2676 isl_set_copy(pwaff1->p[i].set),
2677 isl_set_copy(pwaff2->p[j].set));
2678 better = isl_set_from_basic_set(cmp(
2679 isl_aff_copy(pwaff2->p[j].aff),
2680 isl_aff_copy(pwaff1->p[i].aff)));
2681 better = isl_set_intersect(common, better);
2682 if (isl_set_plain_is_empty(better)) {
2683 isl_set_free(better);
2684 continue;
2686 set = isl_set_subtract(set, isl_set_copy(better));
2688 res = isl_pw_aff_add_piece(res, better,
2689 isl_aff_copy(pwaff2->p[j].aff));
2691 res = isl_pw_aff_add_piece(res, set,
2692 isl_aff_copy(pwaff1->p[i].aff));
2695 for (j = 0; j < pwaff2->n; ++j) {
2696 set = isl_set_copy(pwaff2->p[j].set);
2697 for (i = 0; i < pwaff1->n; ++i)
2698 set = isl_set_subtract(set,
2699 isl_set_copy(pwaff1->p[i].set));
2700 res = isl_pw_aff_add_piece(res, set,
2701 isl_aff_copy(pwaff2->p[j].aff));
2704 isl_pw_aff_free(pwaff1);
2705 isl_pw_aff_free(pwaff2);
2707 return res;
2708 error:
2709 isl_pw_aff_free(pwaff1);
2710 isl_pw_aff_free(pwaff2);
2711 return NULL;
2714 /* Compute a piecewise quasi-affine expression with a domain that
2715 * is the union of those of pwaff1 and pwaff2 and such that on each
2716 * cell, the quasi-affine expression is the maximum of those of pwaff1
2717 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2718 * cell, then the associated expression is the defined one.
2720 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2721 __isl_take isl_pw_aff *pwaff2)
2723 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2726 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2727 __isl_take isl_pw_aff *pwaff2)
2729 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2730 &pw_aff_union_max);
2733 /* Compute a piecewise quasi-affine expression with a domain that
2734 * is the union of those of pwaff1 and pwaff2 and such that on each
2735 * cell, the quasi-affine expression is the minimum of those of pwaff1
2736 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2737 * cell, then the associated expression is the defined one.
2739 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2740 __isl_take isl_pw_aff *pwaff2)
2742 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2745 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2746 __isl_take isl_pw_aff *pwaff2)
2748 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2749 &pw_aff_union_min);
2752 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2753 __isl_take isl_pw_aff *pwaff2, int max)
2755 if (max)
2756 return isl_pw_aff_union_max(pwaff1, pwaff2);
2757 else
2758 return isl_pw_aff_union_min(pwaff1, pwaff2);
2761 /* Construct a map with as domain the domain of pwaff and
2762 * one-dimensional range corresponding to the affine expressions.
2764 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2766 int i;
2767 isl_space *dim;
2768 isl_map *map;
2770 if (!pwaff)
2771 return NULL;
2773 dim = isl_pw_aff_get_space(pwaff);
2774 map = isl_map_empty(dim);
2776 for (i = 0; i < pwaff->n; ++i) {
2777 isl_basic_map *bmap;
2778 isl_map *map_i;
2780 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2781 map_i = isl_map_from_basic_map(bmap);
2782 map_i = isl_map_intersect_domain(map_i,
2783 isl_set_copy(pwaff->p[i].set));
2784 map = isl_map_union_disjoint(map, map_i);
2787 isl_pw_aff_free(pwaff);
2789 return map;
2792 /* Construct a map with as domain the domain of pwaff and
2793 * one-dimensional range corresponding to the affine expressions.
2795 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2797 if (!pwaff)
2798 return NULL;
2799 if (isl_space_is_set(pwaff->dim))
2800 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2801 "space of input is not a map", goto error);
2802 return map_from_pw_aff(pwaff);
2803 error:
2804 isl_pw_aff_free(pwaff);
2805 return NULL;
2808 /* Construct a one-dimensional set with as parameter domain
2809 * the domain of pwaff and the single set dimension
2810 * corresponding to the affine expressions.
2812 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2814 if (!pwaff)
2815 return NULL;
2816 if (!isl_space_is_set(pwaff->dim))
2817 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2818 "space of input is not a set", goto error);
2819 return map_from_pw_aff(pwaff);
2820 error:
2821 isl_pw_aff_free(pwaff);
2822 return NULL;
2825 /* Return a set containing those elements in the domain
2826 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2827 * does not satisfy "fn" (if complement is 1).
2829 * The pieces with a NaN never belong to the result since
2830 * NaN does not satisfy any property.
2832 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2833 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2834 int complement)
2836 int i;
2837 isl_set *set;
2839 if (!pwaff)
2840 return NULL;
2842 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2844 for (i = 0; i < pwaff->n; ++i) {
2845 isl_basic_set *bset;
2846 isl_set *set_i, *locus;
2847 int rational;
2849 if (isl_aff_is_nan(pwaff->p[i].aff))
2850 continue;
2852 rational = isl_set_has_rational(pwaff->p[i].set);
2853 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2854 locus = isl_set_from_basic_set(bset);
2855 set_i = isl_set_copy(pwaff->p[i].set);
2856 if (complement)
2857 set_i = isl_set_subtract(set_i, locus);
2858 else
2859 set_i = isl_set_intersect(set_i, locus);
2860 set = isl_set_union_disjoint(set, set_i);
2863 isl_pw_aff_free(pwaff);
2865 return set;
2868 /* Return a set containing those elements in the domain
2869 * of "pa" where it is positive.
2871 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2873 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2876 /* Return a set containing those elements in the domain
2877 * of pwaff where it is non-negative.
2879 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2881 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2884 /* Return a set containing those elements in the domain
2885 * of pwaff where it is zero.
2887 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2889 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2892 /* Return a set containing those elements in the domain
2893 * of pwaff where it is not zero.
2895 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2897 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2900 /* Return a set containing those elements in the shared domain
2901 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2903 * We compute the difference on the shared domain and then construct
2904 * the set of values where this difference is non-negative.
2905 * If strict is set, we first subtract 1 from the difference.
2906 * If equal is set, we only return the elements where pwaff1 and pwaff2
2907 * are equal.
2909 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2910 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2912 isl_set *set1, *set2;
2914 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2915 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2916 set1 = isl_set_intersect(set1, set2);
2917 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2918 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2919 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2921 if (strict) {
2922 isl_space *dim = isl_set_get_space(set1);
2923 isl_aff *aff;
2924 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2925 aff = isl_aff_add_constant_si(aff, -1);
2926 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2927 } else
2928 isl_set_free(set1);
2930 if (equal)
2931 return isl_pw_aff_zero_set(pwaff1);
2932 return isl_pw_aff_nonneg_set(pwaff1);
2935 /* Return a set containing those elements in the shared domain
2936 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2938 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2939 __isl_take isl_pw_aff *pwaff2)
2941 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2944 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2945 __isl_take isl_pw_aff *pwaff2)
2947 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2950 /* Return a set containing those elements in the shared domain
2951 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2953 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2954 __isl_take isl_pw_aff *pwaff2)
2956 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2959 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2960 __isl_take isl_pw_aff *pwaff2)
2962 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2965 /* Return a set containing those elements in the shared domain
2966 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2968 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2969 __isl_take isl_pw_aff *pwaff2)
2971 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2974 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2975 __isl_take isl_pw_aff *pwaff2)
2977 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2980 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2981 __isl_take isl_pw_aff *pwaff2)
2983 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2986 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2987 __isl_take isl_pw_aff *pwaff2)
2989 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2992 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2993 * where the function values are ordered in the same way as "order",
2994 * which returns a set in the shared domain of its two arguments.
2995 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2997 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2998 * We first pull back the two functions such that they are defined on
2999 * the domain [A -> B]. Then we apply "order", resulting in a set
3000 * in the space [A -> B]. Finally, we unwrap this set to obtain
3001 * a map in the space A -> B.
3003 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3004 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3005 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3006 __isl_take isl_pw_aff *pa2))
3008 isl_space *space1, *space2;
3009 isl_multi_aff *ma;
3010 isl_set *set;
3012 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3013 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3014 space1 = isl_space_map_from_domain_and_range(space1, space2);
3015 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3016 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3017 ma = isl_multi_aff_range_map(space1);
3018 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3019 set = order(pa1, pa2);
3021 return isl_set_unwrap(set);
3024 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3025 * where the function values are equal.
3026 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3028 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3029 __isl_take isl_pw_aff *pa2)
3031 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function values are equal.
3037 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3038 __isl_take isl_pw_aff *pa2)
3040 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3043 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3044 * where the function value of "pa1" is less than the function value of "pa2".
3045 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3047 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3048 __isl_take isl_pw_aff *pa2)
3050 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3053 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3054 * where the function value of "pa1" is less than the function value of "pa2".
3056 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3057 __isl_take isl_pw_aff *pa2)
3059 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3062 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3063 * where the function value of "pa1" is greater than the function value
3064 * of "pa2".
3065 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3067 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3068 __isl_take isl_pw_aff *pa2)
3070 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3073 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3074 * where the function value of "pa1" is greater than the function value
3075 * of "pa2".
3077 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3078 __isl_take isl_pw_aff *pa2)
3080 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3083 /* Return a set containing those elements in the shared domain
3084 * of the elements of list1 and list2 where each element in list1
3085 * has the relation specified by "fn" with each element in list2.
3087 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3088 __isl_take isl_pw_aff_list *list2,
3089 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3090 __isl_take isl_pw_aff *pwaff2))
3092 int i, j;
3093 isl_ctx *ctx;
3094 isl_set *set;
3096 if (!list1 || !list2)
3097 goto error;
3099 ctx = isl_pw_aff_list_get_ctx(list1);
3100 if (list1->n < 1 || list2->n < 1)
3101 isl_die(ctx, isl_error_invalid,
3102 "list should contain at least one element", goto error);
3104 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3105 for (i = 0; i < list1->n; ++i)
3106 for (j = 0; j < list2->n; ++j) {
3107 isl_set *set_ij;
3109 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3110 isl_pw_aff_copy(list2->p[j]));
3111 set = isl_set_intersect(set, set_ij);
3114 isl_pw_aff_list_free(list1);
3115 isl_pw_aff_list_free(list2);
3116 return set;
3117 error:
3118 isl_pw_aff_list_free(list1);
3119 isl_pw_aff_list_free(list2);
3120 return NULL;
3123 /* Return a set containing those elements in the shared domain
3124 * of the elements of list1 and list2 where each element in list1
3125 * is equal to each element in list2.
3127 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3128 __isl_take isl_pw_aff_list *list2)
3130 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3133 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3134 __isl_take isl_pw_aff_list *list2)
3136 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3139 /* Return a set containing those elements in the shared domain
3140 * of the elements of list1 and list2 where each element in list1
3141 * is less than or equal to each element in list2.
3143 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3144 __isl_take isl_pw_aff_list *list2)
3146 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3149 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3150 __isl_take isl_pw_aff_list *list2)
3152 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3155 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3156 __isl_take isl_pw_aff_list *list2)
3158 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3161 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3162 __isl_take isl_pw_aff_list *list2)
3164 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3168 /* Return a set containing those elements in the shared domain
3169 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3171 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3172 __isl_take isl_pw_aff *pwaff2)
3174 isl_set *set_lt, *set_gt;
3176 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3177 isl_pw_aff_copy(pwaff2));
3178 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3179 return isl_set_union_disjoint(set_lt, set_gt);
3182 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3183 __isl_take isl_pw_aff *pwaff2)
3185 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3188 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3189 isl_int v)
3191 int i;
3193 if (isl_int_is_one(v))
3194 return pwaff;
3195 if (!isl_int_is_pos(v))
3196 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3197 "factor needs to be positive",
3198 return isl_pw_aff_free(pwaff));
3199 pwaff = isl_pw_aff_cow(pwaff);
3200 if (!pwaff)
3201 return NULL;
3202 if (pwaff->n == 0)
3203 return pwaff;
3205 for (i = 0; i < pwaff->n; ++i) {
3206 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3207 if (!pwaff->p[i].aff)
3208 return isl_pw_aff_free(pwaff);
3211 return pwaff;
3214 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3216 int i;
3218 pwaff = isl_pw_aff_cow(pwaff);
3219 if (!pwaff)
3220 return NULL;
3221 if (pwaff->n == 0)
3222 return pwaff;
3224 for (i = 0; i < pwaff->n; ++i) {
3225 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3226 if (!pwaff->p[i].aff)
3227 return isl_pw_aff_free(pwaff);
3230 return pwaff;
3233 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3235 int i;
3237 pwaff = isl_pw_aff_cow(pwaff);
3238 if (!pwaff)
3239 return NULL;
3240 if (pwaff->n == 0)
3241 return pwaff;
3243 for (i = 0; i < pwaff->n; ++i) {
3244 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3245 if (!pwaff->p[i].aff)
3246 return isl_pw_aff_free(pwaff);
3249 return pwaff;
3252 /* Assuming that "cond1" and "cond2" are disjoint,
3253 * return an affine expression that is equal to pwaff1 on cond1
3254 * and to pwaff2 on cond2.
3256 static __isl_give isl_pw_aff *isl_pw_aff_select(
3257 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3258 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3260 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3261 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3263 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3266 /* Return an affine expression that is equal to pwaff_true for elements
3267 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3268 * is zero.
3269 * That is, return cond ? pwaff_true : pwaff_false;
3271 * If "cond" involves and NaN, then we conservatively return a NaN
3272 * on its entire domain. In principle, we could consider the pieces
3273 * where it is NaN separately from those where it is not.
3275 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3276 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3278 isl_set *cond_true, *cond_false;
3280 if (!cond)
3281 goto error;
3282 if (isl_pw_aff_involves_nan(cond)) {
3283 isl_space *space = isl_pw_aff_get_domain_space(cond);
3284 isl_local_space *ls = isl_local_space_from_space(space);
3285 isl_pw_aff_free(cond);
3286 isl_pw_aff_free(pwaff_true);
3287 isl_pw_aff_free(pwaff_false);
3288 return isl_pw_aff_nan_on_domain(ls);
3291 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3292 cond_false = isl_pw_aff_zero_set(cond);
3293 return isl_pw_aff_select(cond_true, pwaff_true,
3294 cond_false, pwaff_false);
3295 error:
3296 isl_pw_aff_free(cond);
3297 isl_pw_aff_free(pwaff_true);
3298 isl_pw_aff_free(pwaff_false);
3299 return NULL;
3302 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3304 if (!aff)
3305 return -1;
3307 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3310 /* Check whether pwaff is a piecewise constant.
3312 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3314 int i;
3316 if (!pwaff)
3317 return -1;
3319 for (i = 0; i < pwaff->n; ++i) {
3320 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3321 if (is_cst < 0 || !is_cst)
3322 return is_cst;
3325 return 1;
3328 /* Return the product of "aff1" and "aff2".
3330 * If either of the two is NaN, then the result is NaN.
3332 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3334 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3335 __isl_take isl_aff *aff2)
3337 if (!aff1 || !aff2)
3338 goto error;
3340 if (isl_aff_is_nan(aff1)) {
3341 isl_aff_free(aff2);
3342 return aff1;
3344 if (isl_aff_is_nan(aff2)) {
3345 isl_aff_free(aff1);
3346 return aff2;
3349 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3350 return isl_aff_mul(aff2, aff1);
3352 if (!isl_aff_is_cst(aff2))
3353 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3354 "at least one affine expression should be constant",
3355 goto error);
3357 aff1 = isl_aff_cow(aff1);
3358 if (!aff1 || !aff2)
3359 goto error;
3361 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3362 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3364 isl_aff_free(aff2);
3365 return aff1;
3366 error:
3367 isl_aff_free(aff1);
3368 isl_aff_free(aff2);
3369 return NULL;
3372 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3374 * If either of the two is NaN, then the result is NaN.
3376 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3377 __isl_take isl_aff *aff2)
3379 int is_cst;
3380 int neg;
3382 if (!aff1 || !aff2)
3383 goto error;
3385 if (isl_aff_is_nan(aff1)) {
3386 isl_aff_free(aff2);
3387 return aff1;
3389 if (isl_aff_is_nan(aff2)) {
3390 isl_aff_free(aff1);
3391 return aff2;
3394 is_cst = isl_aff_is_cst(aff2);
3395 if (is_cst < 0)
3396 goto error;
3397 if (!is_cst)
3398 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3399 "second argument should be a constant", goto error);
3401 if (!aff2)
3402 goto error;
3404 neg = isl_int_is_neg(aff2->v->el[1]);
3405 if (neg) {
3406 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3407 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3410 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3411 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3413 if (neg) {
3414 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3415 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3418 isl_aff_free(aff2);
3419 return aff1;
3420 error:
3421 isl_aff_free(aff1);
3422 isl_aff_free(aff2);
3423 return NULL;
3426 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3427 __isl_take isl_pw_aff *pwaff2)
3429 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3432 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3433 __isl_take isl_pw_aff *pwaff2)
3435 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3438 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3439 __isl_take isl_pw_aff *pwaff2)
3441 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3444 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3445 __isl_take isl_pw_aff *pwaff2)
3447 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3450 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3451 __isl_take isl_pw_aff *pwaff2)
3453 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3456 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3457 __isl_take isl_pw_aff *pa2)
3459 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3462 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3464 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3465 __isl_take isl_pw_aff *pa2)
3467 int is_cst;
3469 is_cst = isl_pw_aff_is_cst(pa2);
3470 if (is_cst < 0)
3471 goto error;
3472 if (!is_cst)
3473 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3474 "second argument should be a piecewise constant",
3475 goto error);
3476 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3477 error:
3478 isl_pw_aff_free(pa1);
3479 isl_pw_aff_free(pa2);
3480 return NULL;
3483 /* Compute the quotient of the integer division of "pa1" by "pa2"
3484 * with rounding towards zero.
3485 * "pa2" is assumed to be a piecewise constant.
3487 * In particular, return
3489 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3492 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3493 __isl_take isl_pw_aff *pa2)
3495 int is_cst;
3496 isl_set *cond;
3497 isl_pw_aff *f, *c;
3499 is_cst = isl_pw_aff_is_cst(pa2);
3500 if (is_cst < 0)
3501 goto error;
3502 if (!is_cst)
3503 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3504 "second argument should be a piecewise constant",
3505 goto error);
3507 pa1 = isl_pw_aff_div(pa1, pa2);
3509 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3510 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3511 c = isl_pw_aff_ceil(pa1);
3512 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3513 error:
3514 isl_pw_aff_free(pa1);
3515 isl_pw_aff_free(pa2);
3516 return NULL;
3519 /* Compute the remainder of the integer division of "pa1" by "pa2"
3520 * with rounding towards zero.
3521 * "pa2" is assumed to be a piecewise constant.
3523 * In particular, return
3525 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3528 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3529 __isl_take isl_pw_aff *pa2)
3531 int is_cst;
3532 isl_pw_aff *res;
3534 is_cst = isl_pw_aff_is_cst(pa2);
3535 if (is_cst < 0)
3536 goto error;
3537 if (!is_cst)
3538 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3539 "second argument should be a piecewise constant",
3540 goto error);
3541 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3542 res = isl_pw_aff_mul(pa2, res);
3543 res = isl_pw_aff_sub(pa1, res);
3544 return res;
3545 error:
3546 isl_pw_aff_free(pa1);
3547 isl_pw_aff_free(pa2);
3548 return NULL;
3551 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3552 __isl_take isl_pw_aff *pwaff2)
3554 isl_set *le;
3555 isl_set *dom;
3557 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3558 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3559 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3560 isl_pw_aff_copy(pwaff2));
3561 dom = isl_set_subtract(dom, isl_set_copy(le));
3562 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3565 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3566 __isl_take isl_pw_aff *pwaff2)
3568 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3571 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3572 __isl_take isl_pw_aff *pwaff2)
3574 isl_set *ge;
3575 isl_set *dom;
3577 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3578 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3579 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3580 isl_pw_aff_copy(pwaff2));
3581 dom = isl_set_subtract(dom, isl_set_copy(ge));
3582 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3585 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3586 __isl_take isl_pw_aff *pwaff2)
3588 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3591 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3592 __isl_take isl_pw_aff_list *list,
3593 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3594 __isl_take isl_pw_aff *pwaff2))
3596 int i;
3597 isl_ctx *ctx;
3598 isl_pw_aff *res;
3600 if (!list)
3601 return NULL;
3603 ctx = isl_pw_aff_list_get_ctx(list);
3604 if (list->n < 1)
3605 isl_die(ctx, isl_error_invalid,
3606 "list should contain at least one element", goto error);
3608 res = isl_pw_aff_copy(list->p[0]);
3609 for (i = 1; i < list->n; ++i)
3610 res = fn(res, isl_pw_aff_copy(list->p[i]));
3612 isl_pw_aff_list_free(list);
3613 return res;
3614 error:
3615 isl_pw_aff_list_free(list);
3616 return NULL;
3619 /* Return an isl_pw_aff that maps each element in the intersection of the
3620 * domains of the elements of list to the minimal corresponding affine
3621 * expression.
3623 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3625 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3628 /* Return an isl_pw_aff that maps each element in the intersection of the
3629 * domains of the elements of list to the maximal corresponding affine
3630 * expression.
3632 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3634 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3637 /* Mark the domains of "pwaff" as rational.
3639 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3641 int i;
3643 pwaff = isl_pw_aff_cow(pwaff);
3644 if (!pwaff)
3645 return NULL;
3646 if (pwaff->n == 0)
3647 return pwaff;
3649 for (i = 0; i < pwaff->n; ++i) {
3650 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3651 if (!pwaff->p[i].set)
3652 return isl_pw_aff_free(pwaff);
3655 return pwaff;
3658 /* Mark the domains of the elements of "list" as rational.
3660 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3661 __isl_take isl_pw_aff_list *list)
3663 int i, n;
3665 if (!list)
3666 return NULL;
3667 if (list->n == 0)
3668 return list;
3670 n = list->n;
3671 for (i = 0; i < n; ++i) {
3672 isl_pw_aff *pa;
3674 pa = isl_pw_aff_list_get_pw_aff(list, i);
3675 pa = isl_pw_aff_set_rational(pa);
3676 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3679 return list;
3682 /* Do the parameters of "aff" match those of "space"?
3684 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3685 __isl_keep isl_space *space)
3687 isl_space *aff_space;
3688 int match;
3690 if (!aff || !space)
3691 return -1;
3693 aff_space = isl_aff_get_domain_space(aff);
3695 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3697 isl_space_free(aff_space);
3698 return match;
3701 /* Check that the domain space of "aff" matches "space".
3703 * Return 0 on success and -1 on error.
3705 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3706 __isl_keep isl_space *space)
3708 isl_space *aff_space;
3709 int match;
3711 if (!aff || !space)
3712 return -1;
3714 aff_space = isl_aff_get_domain_space(aff);
3716 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3717 if (match < 0)
3718 goto error;
3719 if (!match)
3720 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3721 "parameters don't match", goto error);
3722 match = isl_space_tuple_is_equal(space, isl_dim_in,
3723 aff_space, isl_dim_set);
3724 if (match < 0)
3725 goto error;
3726 if (!match)
3727 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3728 "domains don't match", goto error);
3729 isl_space_free(aff_space);
3730 return 0;
3731 error:
3732 isl_space_free(aff_space);
3733 return -1;
3736 #undef BASE
3737 #define BASE aff
3738 #undef DOMBASE
3739 #define DOMBASE set
3740 #define NO_DOMAIN
3742 #include <isl_multi_templ.c>
3743 #include <isl_multi_apply_set.c>
3744 #include <isl_multi_floor.c>
3745 #include <isl_multi_gist.c>
3747 #undef NO_DOMAIN
3749 /* Remove any internal structure of the domain of "ma".
3750 * If there is any such internal structure in the input,
3751 * then the name of the corresponding space is also removed.
3753 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3754 __isl_take isl_multi_aff *ma)
3756 isl_space *space;
3758 if (!ma)
3759 return NULL;
3761 if (!ma->space->nested[0])
3762 return ma;
3764 space = isl_multi_aff_get_space(ma);
3765 space = isl_space_flatten_domain(space);
3766 ma = isl_multi_aff_reset_space(ma, space);
3768 return ma;
3771 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3772 * of the space to its domain.
3774 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3776 int i, n_in;
3777 isl_local_space *ls;
3778 isl_multi_aff *ma;
3780 if (!space)
3781 return NULL;
3782 if (!isl_space_is_map(space))
3783 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3784 "not a map space", goto error);
3786 n_in = isl_space_dim(space, isl_dim_in);
3787 space = isl_space_domain_map(space);
3789 ma = isl_multi_aff_alloc(isl_space_copy(space));
3790 if (n_in == 0) {
3791 isl_space_free(space);
3792 return ma;
3795 space = isl_space_domain(space);
3796 ls = isl_local_space_from_space(space);
3797 for (i = 0; i < n_in; ++i) {
3798 isl_aff *aff;
3800 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3801 isl_dim_set, i);
3802 ma = isl_multi_aff_set_aff(ma, i, aff);
3804 isl_local_space_free(ls);
3805 return ma;
3806 error:
3807 isl_space_free(space);
3808 return NULL;
3811 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3812 * of the space to its range.
3814 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3816 int i, n_in, n_out;
3817 isl_local_space *ls;
3818 isl_multi_aff *ma;
3820 if (!space)
3821 return NULL;
3822 if (!isl_space_is_map(space))
3823 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3824 "not a map space", goto error);
3826 n_in = isl_space_dim(space, isl_dim_in);
3827 n_out = isl_space_dim(space, isl_dim_out);
3828 space = isl_space_range_map(space);
3830 ma = isl_multi_aff_alloc(isl_space_copy(space));
3831 if (n_out == 0) {
3832 isl_space_free(space);
3833 return ma;
3836 space = isl_space_domain(space);
3837 ls = isl_local_space_from_space(space);
3838 for (i = 0; i < n_out; ++i) {
3839 isl_aff *aff;
3841 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3842 isl_dim_set, n_in + i);
3843 ma = isl_multi_aff_set_aff(ma, i, aff);
3845 isl_local_space_free(ls);
3846 return ma;
3847 error:
3848 isl_space_free(space);
3849 return NULL;
3852 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3853 * of the space to its range.
3855 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3856 __isl_take isl_space *space)
3858 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3861 /* Given the space of a set and a range of set dimensions,
3862 * construct an isl_multi_aff that projects out those dimensions.
3864 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3865 __isl_take isl_space *space, enum isl_dim_type type,
3866 unsigned first, unsigned n)
3868 int i, dim;
3869 isl_local_space *ls;
3870 isl_multi_aff *ma;
3872 if (!space)
3873 return NULL;
3874 if (!isl_space_is_set(space))
3875 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3876 "expecting set space", goto error);
3877 if (type != isl_dim_set)
3878 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3879 "only set dimensions can be projected out", goto error);
3881 dim = isl_space_dim(space, isl_dim_set);
3882 if (first + n > dim)
3883 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3884 "range out of bounds", goto error);
3886 space = isl_space_from_domain(space);
3887 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3889 if (dim == n)
3890 return isl_multi_aff_alloc(space);
3892 ma = isl_multi_aff_alloc(isl_space_copy(space));
3893 space = isl_space_domain(space);
3894 ls = isl_local_space_from_space(space);
3896 for (i = 0; i < first; ++i) {
3897 isl_aff *aff;
3899 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3900 isl_dim_set, i);
3901 ma = isl_multi_aff_set_aff(ma, i, aff);
3904 for (i = 0; i < dim - (first + n); ++i) {
3905 isl_aff *aff;
3907 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3908 isl_dim_set, first + n + i);
3909 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3912 isl_local_space_free(ls);
3913 return ma;
3914 error:
3915 isl_space_free(space);
3916 return NULL;
3919 /* Given the space of a set and a range of set dimensions,
3920 * construct an isl_pw_multi_aff that projects out those dimensions.
3922 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3923 __isl_take isl_space *space, enum isl_dim_type type,
3924 unsigned first, unsigned n)
3926 isl_multi_aff *ma;
3928 ma = isl_multi_aff_project_out_map(space, type, first, n);
3929 return isl_pw_multi_aff_from_multi_aff(ma);
3932 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3933 * domain.
3935 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3936 __isl_take isl_multi_aff *ma)
3938 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3939 return isl_pw_multi_aff_alloc(dom, ma);
3942 /* Create a piecewise multi-affine expression in the given space that maps each
3943 * input dimension to the corresponding output dimension.
3945 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3946 __isl_take isl_space *space)
3948 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3951 /* Add "ma2" to "ma1" and return the result.
3953 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3955 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3956 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3958 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3961 /* Add "ma2" to "ma1" and return the result.
3963 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3964 __isl_take isl_multi_aff *ma2)
3966 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3967 &isl_multi_aff_add_aligned);
3970 /* Exploit the equalities in "eq" to simplify the affine expressions.
3972 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3973 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3975 int i;
3977 maff = isl_multi_aff_cow(maff);
3978 if (!maff || !eq)
3979 goto error;
3981 for (i = 0; i < maff->n; ++i) {
3982 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3983 isl_basic_set_copy(eq));
3984 if (!maff->p[i])
3985 goto error;
3988 isl_basic_set_free(eq);
3989 return maff;
3990 error:
3991 isl_basic_set_free(eq);
3992 isl_multi_aff_free(maff);
3993 return NULL;
3996 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3997 isl_int f)
3999 int i;
4001 maff = isl_multi_aff_cow(maff);
4002 if (!maff)
4003 return NULL;
4005 for (i = 0; i < maff->n; ++i) {
4006 maff->p[i] = isl_aff_scale(maff->p[i], f);
4007 if (!maff->p[i])
4008 return isl_multi_aff_free(maff);
4011 return maff;
4014 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4015 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4017 maff1 = isl_multi_aff_add(maff1, maff2);
4018 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4019 return maff1;
4022 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4024 if (!maff)
4025 return -1;
4027 return 0;
4030 /* Return the set of domain elements where "ma1" is lexicographically
4031 * smaller than or equal to "ma2".
4033 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4034 __isl_take isl_multi_aff *ma2)
4036 return isl_multi_aff_lex_ge_set(ma2, ma1);
4039 /* Return the set of domain elements where "ma1" is lexicographically
4040 * greater than or equal to "ma2".
4042 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4043 __isl_take isl_multi_aff *ma2)
4045 isl_space *space;
4046 isl_map *map1, *map2;
4047 isl_map *map, *ge;
4049 map1 = isl_map_from_multi_aff(ma1);
4050 map2 = isl_map_from_multi_aff(ma2);
4051 map = isl_map_range_product(map1, map2);
4052 space = isl_space_range(isl_map_get_space(map));
4053 space = isl_space_domain(isl_space_unwrap(space));
4054 ge = isl_map_lex_ge(space);
4055 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4057 return isl_map_domain(map);
4060 #undef PW
4061 #define PW isl_pw_multi_aff
4062 #undef EL
4063 #define EL isl_multi_aff
4064 #undef EL_IS_ZERO
4065 #define EL_IS_ZERO is_empty
4066 #undef ZERO
4067 #define ZERO empty
4068 #undef IS_ZERO
4069 #define IS_ZERO is_empty
4070 #undef FIELD
4071 #define FIELD maff
4072 #undef DEFAULT_IS_ZERO
4073 #define DEFAULT_IS_ZERO 0
4075 #define NO_SUB
4076 #define NO_EVAL
4077 #define NO_OPT
4078 #define NO_INVOLVES_DIMS
4079 #define NO_INSERT_DIMS
4080 #define NO_LIFT
4081 #define NO_MORPH
4083 #include <isl_pw_templ.c>
4085 #undef NO_SUB
4087 #undef UNION
4088 #define UNION isl_union_pw_multi_aff
4089 #undef PART
4090 #define PART isl_pw_multi_aff
4091 #undef PARTS
4092 #define PARTS pw_multi_aff
4094 #define NO_EVAL
4096 #include <isl_union_templ.c>
4098 /* Given a function "cmp" that returns the set of elements where
4099 * "ma1" is "better" than "ma2", return the intersection of this
4100 * set with "dom1" and "dom2".
4102 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
4103 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
4104 __isl_keep isl_multi_aff *ma2,
4105 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4106 __isl_take isl_multi_aff *ma2))
4108 isl_set *common;
4109 isl_set *better;
4110 int is_empty;
4112 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
4113 is_empty = isl_set_plain_is_empty(common);
4114 if (is_empty >= 0 && is_empty)
4115 return common;
4116 if (is_empty < 0)
4117 return isl_set_free(common);
4118 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
4119 better = isl_set_intersect(common, better);
4121 return better;
4124 /* Given a function "cmp" that returns the set of elements where
4125 * "ma1" is "better" than "ma2", return a piecewise multi affine
4126 * expression defined on the union of the definition domains
4127 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4128 * "pma2" on each cell. If only one of the two input functions
4129 * is defined on a given cell, then it is considered the best.
4131 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4132 __isl_take isl_pw_multi_aff *pma1,
4133 __isl_take isl_pw_multi_aff *pma2,
4134 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4135 __isl_take isl_multi_aff *ma2))
4137 int i, j, n;
4138 isl_pw_multi_aff *res = NULL;
4139 isl_ctx *ctx;
4140 isl_set *set = NULL;
4142 if (!pma1 || !pma2)
4143 goto error;
4145 ctx = isl_space_get_ctx(pma1->dim);
4146 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4147 isl_die(ctx, isl_error_invalid,
4148 "arguments should live in the same space", goto error);
4150 if (isl_pw_multi_aff_is_empty(pma1)) {
4151 isl_pw_multi_aff_free(pma1);
4152 return pma2;
4155 if (isl_pw_multi_aff_is_empty(pma2)) {
4156 isl_pw_multi_aff_free(pma2);
4157 return pma1;
4160 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4161 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4163 for (i = 0; i < pma1->n; ++i) {
4164 set = isl_set_copy(pma1->p[i].set);
4165 for (j = 0; j < pma2->n; ++j) {
4166 isl_set *better;
4167 int is_empty;
4169 better = shared_and_better(pma2->p[j].set,
4170 pma1->p[i].set, pma2->p[j].maff,
4171 pma1->p[i].maff, cmp);
4172 is_empty = isl_set_plain_is_empty(better);
4173 if (is_empty < 0 || is_empty) {
4174 isl_set_free(better);
4175 if (is_empty < 0)
4176 goto error;
4177 continue;
4179 set = isl_set_subtract(set, isl_set_copy(better));
4181 res = isl_pw_multi_aff_add_piece(res, better,
4182 isl_multi_aff_copy(pma2->p[j].maff));
4184 res = isl_pw_multi_aff_add_piece(res, set,
4185 isl_multi_aff_copy(pma1->p[i].maff));
4188 for (j = 0; j < pma2->n; ++j) {
4189 set = isl_set_copy(pma2->p[j].set);
4190 for (i = 0; i < pma1->n; ++i)
4191 set = isl_set_subtract(set,
4192 isl_set_copy(pma1->p[i].set));
4193 res = isl_pw_multi_aff_add_piece(res, set,
4194 isl_multi_aff_copy(pma2->p[j].maff));
4197 isl_pw_multi_aff_free(pma1);
4198 isl_pw_multi_aff_free(pma2);
4200 return res;
4201 error:
4202 isl_pw_multi_aff_free(pma1);
4203 isl_pw_multi_aff_free(pma2);
4204 isl_set_free(set);
4205 return isl_pw_multi_aff_free(res);
4208 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4209 __isl_take isl_pw_multi_aff *pma1,
4210 __isl_take isl_pw_multi_aff *pma2)
4212 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4215 /* Given two piecewise multi affine expressions, return a piecewise
4216 * multi-affine expression defined on the union of the definition domains
4217 * of the inputs that is equal to the lexicographic maximum of the two
4218 * inputs on each cell. If only one of the two inputs is defined on
4219 * a given cell, then it is considered to be the maximum.
4221 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4222 __isl_take isl_pw_multi_aff *pma1,
4223 __isl_take isl_pw_multi_aff *pma2)
4225 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4226 &pw_multi_aff_union_lexmax);
4229 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4230 __isl_take isl_pw_multi_aff *pma1,
4231 __isl_take isl_pw_multi_aff *pma2)
4233 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4236 /* Given two piecewise multi affine expressions, return a piecewise
4237 * multi-affine expression defined on the union of the definition domains
4238 * of the inputs that is equal to the lexicographic minimum of the two
4239 * inputs on each cell. If only one of the two inputs is defined on
4240 * a given cell, then it is considered to be the minimum.
4242 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4243 __isl_take isl_pw_multi_aff *pma1,
4244 __isl_take isl_pw_multi_aff *pma2)
4246 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4247 &pw_multi_aff_union_lexmin);
4250 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4251 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4253 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4254 &isl_multi_aff_add);
4257 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4258 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4260 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4261 &pw_multi_aff_add);
4264 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4265 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4267 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4268 &isl_multi_aff_sub);
4271 /* Subtract "pma2" from "pma1" and return the result.
4273 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4274 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4276 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4277 &pw_multi_aff_sub);
4280 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4281 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4283 return isl_pw_multi_aff_union_add_(pma1, pma2);
4286 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4287 * with the actual sum on the shared domain and
4288 * the defined expression on the symmetric difference of the domains.
4290 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4291 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4293 return isl_union_pw_aff_union_add_(upa1, upa2);
4296 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4297 * with the actual sum on the shared domain and
4298 * the defined expression on the symmetric difference of the domains.
4300 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4301 __isl_take isl_union_pw_multi_aff *upma1,
4302 __isl_take isl_union_pw_multi_aff *upma2)
4304 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4307 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4308 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4310 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4311 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4313 int i, j, n;
4314 isl_space *space;
4315 isl_pw_multi_aff *res;
4317 if (!pma1 || !pma2)
4318 goto error;
4320 n = pma1->n * pma2->n;
4321 space = isl_space_product(isl_space_copy(pma1->dim),
4322 isl_space_copy(pma2->dim));
4323 res = isl_pw_multi_aff_alloc_size(space, n);
4325 for (i = 0; i < pma1->n; ++i) {
4326 for (j = 0; j < pma2->n; ++j) {
4327 isl_set *domain;
4328 isl_multi_aff *ma;
4330 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4331 isl_set_copy(pma2->p[j].set));
4332 ma = isl_multi_aff_product(
4333 isl_multi_aff_copy(pma1->p[i].maff),
4334 isl_multi_aff_copy(pma2->p[j].maff));
4335 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4339 isl_pw_multi_aff_free(pma1);
4340 isl_pw_multi_aff_free(pma2);
4341 return res;
4342 error:
4343 isl_pw_multi_aff_free(pma1);
4344 isl_pw_multi_aff_free(pma2);
4345 return NULL;
4348 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4349 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4351 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4352 &pw_multi_aff_product);
4355 /* Construct a map mapping the domain of the piecewise multi-affine expression
4356 * to its range, with each dimension in the range equated to the
4357 * corresponding affine expression on its cell.
4359 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4361 int i;
4362 isl_map *map;
4364 if (!pma)
4365 return NULL;
4367 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4369 for (i = 0; i < pma->n; ++i) {
4370 isl_multi_aff *maff;
4371 isl_basic_map *bmap;
4372 isl_map *map_i;
4374 maff = isl_multi_aff_copy(pma->p[i].maff);
4375 bmap = isl_basic_map_from_multi_aff(maff);
4376 map_i = isl_map_from_basic_map(bmap);
4377 map_i = isl_map_intersect_domain(map_i,
4378 isl_set_copy(pma->p[i].set));
4379 map = isl_map_union_disjoint(map, map_i);
4382 isl_pw_multi_aff_free(pma);
4383 return map;
4386 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4388 if (!pma)
4389 return NULL;
4391 if (!isl_space_is_set(pma->dim))
4392 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4393 "isl_pw_multi_aff cannot be converted into an isl_set",
4394 goto error);
4396 return isl_map_from_pw_multi_aff(pma);
4397 error:
4398 isl_pw_multi_aff_free(pma);
4399 return NULL;
4402 /* Given a basic map with a single output dimension that is defined
4403 * in terms of the parameters and input dimensions using an equality,
4404 * extract an isl_aff that expresses the output dimension in terms
4405 * of the parameters and input dimensions.
4406 * Note that this expression may involve integer divisions defined
4407 * in terms of parameters and input dimensions.
4409 * This function shares some similarities with
4410 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4412 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4413 __isl_take isl_basic_map *bmap)
4415 int eq;
4416 unsigned offset;
4417 unsigned n_div;
4418 isl_local_space *ls;
4419 isl_aff *aff;
4421 if (!bmap)
4422 return NULL;
4423 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4424 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4425 "basic map should have a single output dimension",
4426 goto error);
4427 eq = isl_basic_map_output_defining_equality(bmap, 0);
4428 if (eq >= bmap->n_eq)
4429 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4430 "unable to find suitable equality", goto error);
4431 ls = isl_basic_map_get_local_space(bmap);
4432 aff = isl_aff_alloc(isl_local_space_domain(ls));
4433 if (!aff)
4434 goto error;
4435 offset = isl_basic_map_offset(bmap, isl_dim_out);
4436 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4437 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4438 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4439 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4440 n_div);
4441 } else {
4442 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4443 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4444 n_div);
4446 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4447 isl_basic_map_free(bmap);
4449 aff = isl_aff_remove_unused_divs(aff);
4450 return aff;
4451 error:
4452 isl_basic_map_free(bmap);
4453 return NULL;
4456 /* Given a basic map where each output dimension is defined
4457 * in terms of the parameters and input dimensions using an equality,
4458 * extract an isl_multi_aff that expresses the output dimensions in terms
4459 * of the parameters and input dimensions.
4461 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4462 __isl_take isl_basic_map *bmap)
4464 int i;
4465 unsigned n_out;
4466 isl_multi_aff *ma;
4468 if (!bmap)
4469 return NULL;
4471 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4472 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4474 for (i = 0; i < n_out; ++i) {
4475 isl_basic_map *bmap_i;
4476 isl_aff *aff;
4478 bmap_i = isl_basic_map_copy(bmap);
4479 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4480 i + 1, n_out - (1 + i));
4481 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4482 aff = extract_isl_aff_from_basic_map(bmap_i);
4483 ma = isl_multi_aff_set_aff(ma, i, aff);
4486 isl_basic_map_free(bmap);
4488 return ma;
4491 /* Given a basic set where each set dimension is defined
4492 * in terms of the parameters using an equality,
4493 * extract an isl_multi_aff that expresses the set dimensions in terms
4494 * of the parameters.
4496 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4497 __isl_take isl_basic_set *bset)
4499 return extract_isl_multi_aff_from_basic_map(bset);
4502 /* Create an isl_pw_multi_aff that is equivalent to
4503 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4504 * The given basic map is such that each output dimension is defined
4505 * in terms of the parameters and input dimensions using an equality.
4507 * Since some applications expect the result of isl_pw_multi_aff_from_map
4508 * to only contain integer affine expressions, we compute the floor
4509 * of the expression before returning.
4511 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4512 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4514 isl_multi_aff *ma;
4516 ma = extract_isl_multi_aff_from_basic_map(bmap);
4517 ma = isl_multi_aff_floor(ma);
4518 return isl_pw_multi_aff_alloc(domain, ma);
4521 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4522 * This obviously only works if the input "map" is single-valued.
4523 * If so, we compute the lexicographic minimum of the image in the form
4524 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4525 * to its lexicographic minimum.
4526 * If the input is not single-valued, we produce an error.
4528 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4529 __isl_take isl_map *map)
4531 int i;
4532 int sv;
4533 isl_pw_multi_aff *pma;
4535 sv = isl_map_is_single_valued(map);
4536 if (sv < 0)
4537 goto error;
4538 if (!sv)
4539 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4540 "map is not single-valued", goto error);
4541 map = isl_map_make_disjoint(map);
4542 if (!map)
4543 return NULL;
4545 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4547 for (i = 0; i < map->n; ++i) {
4548 isl_pw_multi_aff *pma_i;
4549 isl_basic_map *bmap;
4550 bmap = isl_basic_map_copy(map->p[i]);
4551 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4552 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4555 isl_map_free(map);
4556 return pma;
4557 error:
4558 isl_map_free(map);
4559 return NULL;
4562 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4563 * taking into account that the output dimension at position "d"
4564 * can be represented as
4566 * x = floor((e(...) + c1) / m)
4568 * given that constraint "i" is of the form
4570 * e(...) + c1 - m x >= 0
4573 * Let "map" be of the form
4575 * A -> B
4577 * We construct a mapping
4579 * A -> [A -> x = floor(...)]
4581 * apply that to the map, obtaining
4583 * [A -> x = floor(...)] -> B
4585 * and equate dimension "d" to x.
4586 * We then compute a isl_pw_multi_aff representation of the resulting map
4587 * and plug in the mapping above.
4589 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4590 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4592 isl_ctx *ctx;
4593 isl_space *space;
4594 isl_local_space *ls;
4595 isl_multi_aff *ma;
4596 isl_aff *aff;
4597 isl_vec *v;
4598 isl_map *insert;
4599 int offset;
4600 int n;
4601 int n_in;
4602 isl_pw_multi_aff *pma;
4603 int is_set;
4605 is_set = isl_map_is_set(map);
4607 offset = isl_basic_map_offset(hull, isl_dim_out);
4608 ctx = isl_map_get_ctx(map);
4609 space = isl_space_domain(isl_map_get_space(map));
4610 n_in = isl_space_dim(space, isl_dim_set);
4611 n = isl_space_dim(space, isl_dim_all);
4613 v = isl_vec_alloc(ctx, 1 + 1 + n);
4614 if (v) {
4615 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4616 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4618 isl_basic_map_free(hull);
4620 ls = isl_local_space_from_space(isl_space_copy(space));
4621 aff = isl_aff_alloc_vec(ls, v);
4622 aff = isl_aff_floor(aff);
4623 if (is_set) {
4624 isl_space_free(space);
4625 ma = isl_multi_aff_from_aff(aff);
4626 } else {
4627 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4628 ma = isl_multi_aff_range_product(ma,
4629 isl_multi_aff_from_aff(aff));
4632 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4633 map = isl_map_apply_domain(map, insert);
4634 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4635 pma = isl_pw_multi_aff_from_map(map);
4636 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4638 return pma;
4641 /* Is constraint "c" of the form
4643 * e(...) + c1 - m x >= 0
4645 * or
4647 * -e(...) + c2 + m x >= 0
4649 * where m > 1 and e only depends on parameters and input dimemnsions?
4651 * "offset" is the offset of the output dimensions
4652 * "pos" is the position of output dimension x.
4654 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4656 if (isl_int_is_zero(c[offset + d]))
4657 return 0;
4658 if (isl_int_is_one(c[offset + d]))
4659 return 0;
4660 if (isl_int_is_negone(c[offset + d]))
4661 return 0;
4662 if (isl_seq_first_non_zero(c + offset, d) != -1)
4663 return 0;
4664 if (isl_seq_first_non_zero(c + offset + d + 1,
4665 total - (offset + d + 1)) != -1)
4666 return 0;
4667 return 1;
4670 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4672 * As a special case, we first check if there is any pair of constraints,
4673 * shared by all the basic maps in "map" that force a given dimension
4674 * to be equal to the floor of some affine combination of the input dimensions.
4676 * In particular, if we can find two constraints
4678 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4680 * and
4682 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4684 * where m > 1 and e only depends on parameters and input dimemnsions,
4685 * and such that
4687 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4689 * then we know that we can take
4691 * x = floor((e(...) + c1) / m)
4693 * without having to perform any computation.
4695 * Note that we know that
4697 * c1 + c2 >= 1
4699 * If c1 + c2 were 0, then we would have detected an equality during
4700 * simplification. If c1 + c2 were negative, then we would have detected
4701 * a contradiction.
4703 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4704 __isl_take isl_map *map)
4706 int d, dim;
4707 int i, j, n;
4708 int offset, total;
4709 isl_int sum;
4710 isl_basic_map *hull;
4712 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4713 if (!hull)
4714 goto error;
4716 isl_int_init(sum);
4717 dim = isl_map_dim(map, isl_dim_out);
4718 offset = isl_basic_map_offset(hull, isl_dim_out);
4719 total = 1 + isl_basic_map_total_dim(hull);
4720 n = hull->n_ineq;
4721 for (d = 0; d < dim; ++d) {
4722 for (i = 0; i < n; ++i) {
4723 if (!is_potential_div_constraint(hull->ineq[i],
4724 offset, d, total))
4725 continue;
4726 for (j = i + 1; j < n; ++j) {
4727 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4728 hull->ineq[j] + 1, total - 1))
4729 continue;
4730 isl_int_add(sum, hull->ineq[i][0],
4731 hull->ineq[j][0]);
4732 if (isl_int_abs_lt(sum,
4733 hull->ineq[i][offset + d]))
4734 break;
4737 if (j >= n)
4738 continue;
4739 isl_int_clear(sum);
4740 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4741 j = i;
4742 return pw_multi_aff_from_map_div(map, hull, d, j);
4745 isl_int_clear(sum);
4746 isl_basic_map_free(hull);
4747 return pw_multi_aff_from_map_base(map);
4748 error:
4749 isl_map_free(map);
4750 isl_basic_map_free(hull);
4751 return NULL;
4754 /* Given an affine expression
4756 * [A -> B] -> f(A,B)
4758 * construct an isl_multi_aff
4760 * [A -> B] -> B'
4762 * such that dimension "d" in B' is set to "aff" and the remaining
4763 * dimensions are set equal to the corresponding dimensions in B.
4764 * "n_in" is the dimension of the space A.
4765 * "n_out" is the dimension of the space B.
4767 * If "is_set" is set, then the affine expression is of the form
4769 * [B] -> f(B)
4771 * and we construct an isl_multi_aff
4773 * B -> B'
4775 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4776 unsigned n_in, unsigned n_out, int is_set)
4778 int i;
4779 isl_multi_aff *ma;
4780 isl_space *space, *space2;
4781 isl_local_space *ls;
4783 space = isl_aff_get_domain_space(aff);
4784 ls = isl_local_space_from_space(isl_space_copy(space));
4785 space2 = isl_space_copy(space);
4786 if (!is_set)
4787 space2 = isl_space_range(isl_space_unwrap(space2));
4788 space = isl_space_map_from_domain_and_range(space, space2);
4789 ma = isl_multi_aff_alloc(space);
4790 ma = isl_multi_aff_set_aff(ma, d, aff);
4792 for (i = 0; i < n_out; ++i) {
4793 if (i == d)
4794 continue;
4795 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4796 isl_dim_set, n_in + i);
4797 ma = isl_multi_aff_set_aff(ma, i, aff);
4800 isl_local_space_free(ls);
4802 return ma;
4805 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4806 * taking into account that the dimension at position "d" can be written as
4808 * x = m a + f(..) (1)
4810 * where m is equal to "gcd".
4811 * "i" is the index of the equality in "hull" that defines f(..).
4812 * In particular, the equality is of the form
4814 * f(..) - x + m g(existentials) = 0
4816 * or
4818 * -f(..) + x + m g(existentials) = 0
4820 * We basically plug (1) into "map", resulting in a map with "a"
4821 * in the range instead of "x". The corresponding isl_pw_multi_aff
4822 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4824 * Specifically, given the input map
4826 * A -> B
4828 * We first wrap it into a set
4830 * [A -> B]
4832 * and define (1) on top of the corresponding space, resulting in "aff".
4833 * We use this to create an isl_multi_aff that maps the output position "d"
4834 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4835 * We plug this into the wrapped map, unwrap the result and compute the
4836 * corresponding isl_pw_multi_aff.
4837 * The result is an expression
4839 * A -> T(A)
4841 * We adjust that to
4843 * A -> [A -> T(A)]
4845 * so that we can plug that into "aff", after extending the latter to
4846 * a mapping
4848 * [A -> B] -> B'
4851 * If "map" is actually a set, then there is no "A" space, meaning
4852 * that we do not need to perform any wrapping, and that the result
4853 * of the recursive call is of the form
4855 * [T]
4857 * which is plugged into a mapping of the form
4859 * B -> B'
4861 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4862 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4863 isl_int gcd)
4865 isl_set *set;
4866 isl_space *space;
4867 isl_local_space *ls;
4868 isl_aff *aff;
4869 isl_multi_aff *ma;
4870 isl_pw_multi_aff *pma, *id;
4871 unsigned n_in;
4872 unsigned o_out;
4873 unsigned n_out;
4874 int is_set;
4876 is_set = isl_map_is_set(map);
4878 n_in = isl_basic_map_dim(hull, isl_dim_in);
4879 n_out = isl_basic_map_dim(hull, isl_dim_out);
4880 o_out = isl_basic_map_offset(hull, isl_dim_out);
4882 if (is_set)
4883 set = map;
4884 else
4885 set = isl_map_wrap(map);
4886 space = isl_space_map_from_set(isl_set_get_space(set));
4887 ma = isl_multi_aff_identity(space);
4888 ls = isl_local_space_from_space(isl_set_get_space(set));
4889 aff = isl_aff_alloc(ls);
4890 if (aff) {
4891 isl_int_set_si(aff->v->el[0], 1);
4892 if (isl_int_is_one(hull->eq[i][o_out + d]))
4893 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4894 aff->v->size - 1);
4895 else
4896 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4897 aff->v->size - 1);
4898 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4900 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4901 set = isl_set_preimage_multi_aff(set, ma);
4903 ma = range_map(aff, d, n_in, n_out, is_set);
4905 if (is_set)
4906 map = set;
4907 else
4908 map = isl_set_unwrap(set);
4909 pma = isl_pw_multi_aff_from_map(map);
4911 if (!is_set) {
4912 space = isl_pw_multi_aff_get_domain_space(pma);
4913 space = isl_space_map_from_set(space);
4914 id = isl_pw_multi_aff_identity(space);
4915 pma = isl_pw_multi_aff_range_product(id, pma);
4917 id = isl_pw_multi_aff_from_multi_aff(ma);
4918 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4920 isl_basic_map_free(hull);
4921 return pma;
4924 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4926 * As a special case, we first check if all output dimensions are uniquely
4927 * defined in terms of the parameters and input dimensions over the entire
4928 * domain. If so, we extract the desired isl_pw_multi_aff directly
4929 * from the affine hull of "map" and its domain.
4931 * Otherwise, we check if any of the output dimensions is "strided".
4932 * That is, we check if can be written as
4934 * x = m a + f(..)
4936 * with m greater than 1, a some combination of existentiall quantified
4937 * variables and f and expression in the parameters and input dimensions.
4938 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4940 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4941 * special case.
4943 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4945 int i, j;
4946 int sv;
4947 isl_basic_map *hull;
4948 unsigned n_out;
4949 unsigned o_out;
4950 unsigned n_div;
4951 unsigned o_div;
4952 isl_int gcd;
4954 if (!map)
4955 return NULL;
4957 hull = isl_map_affine_hull(isl_map_copy(map));
4958 sv = isl_basic_map_plain_is_single_valued(hull);
4959 if (sv >= 0 && sv)
4960 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4961 if (sv < 0)
4962 hull = isl_basic_map_free(hull);
4963 if (!hull)
4964 goto error;
4966 n_div = isl_basic_map_dim(hull, isl_dim_div);
4967 o_div = isl_basic_map_offset(hull, isl_dim_div);
4969 if (n_div == 0) {
4970 isl_basic_map_free(hull);
4971 return pw_multi_aff_from_map_check_div(map);
4974 isl_int_init(gcd);
4976 n_out = isl_basic_map_dim(hull, isl_dim_out);
4977 o_out = isl_basic_map_offset(hull, isl_dim_out);
4979 for (i = 0; i < n_out; ++i) {
4980 for (j = 0; j < hull->n_eq; ++j) {
4981 isl_int *eq = hull->eq[j];
4982 isl_pw_multi_aff *res;
4984 if (!isl_int_is_one(eq[o_out + i]) &&
4985 !isl_int_is_negone(eq[o_out + i]))
4986 continue;
4987 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4988 continue;
4989 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4990 n_out - (i + 1)) != -1)
4991 continue;
4992 isl_seq_gcd(eq + o_div, n_div, &gcd);
4993 if (isl_int_is_zero(gcd))
4994 continue;
4995 if (isl_int_is_one(gcd))
4996 continue;
4998 res = pw_multi_aff_from_map_stride(map, hull,
4999 i, j, gcd);
5000 isl_int_clear(gcd);
5001 return res;
5005 isl_int_clear(gcd);
5006 isl_basic_map_free(hull);
5007 return pw_multi_aff_from_map_check_div(map);
5008 error:
5009 isl_map_free(map);
5010 return NULL;
5013 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5015 return isl_pw_multi_aff_from_map(set);
5018 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5019 * add it to *user.
5021 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5023 isl_union_pw_multi_aff **upma = user;
5024 isl_pw_multi_aff *pma;
5026 pma = isl_pw_multi_aff_from_map(map);
5027 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5029 return *upma ? 0 : -1;
5032 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5033 * domain.
5035 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5036 __isl_take isl_aff *aff)
5038 isl_multi_aff *ma;
5039 isl_pw_multi_aff *pma;
5041 ma = isl_multi_aff_from_aff(aff);
5042 pma = isl_pw_multi_aff_from_multi_aff(ma);
5043 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5046 /* Try and create an isl_union_pw_multi_aff that is equivalent
5047 * to the given isl_union_map.
5048 * The isl_union_map is required to be single-valued in each space.
5049 * Otherwise, an error is produced.
5051 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5052 __isl_take isl_union_map *umap)
5054 isl_space *space;
5055 isl_union_pw_multi_aff *upma;
5057 space = isl_union_map_get_space(umap);
5058 upma = isl_union_pw_multi_aff_empty(space);
5059 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5060 upma = isl_union_pw_multi_aff_free(upma);
5061 isl_union_map_free(umap);
5063 return upma;
5066 /* Try and create an isl_union_pw_multi_aff that is equivalent
5067 * to the given isl_union_set.
5068 * The isl_union_set is required to be a singleton in each space.
5069 * Otherwise, an error is produced.
5071 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5072 __isl_take isl_union_set *uset)
5074 return isl_union_pw_multi_aff_from_union_map(uset);
5077 /* Return the piecewise affine expression "set ? 1 : 0".
5079 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5081 isl_pw_aff *pa;
5082 isl_space *space = isl_set_get_space(set);
5083 isl_local_space *ls = isl_local_space_from_space(space);
5084 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5085 isl_aff *one = isl_aff_zero_on_domain(ls);
5087 one = isl_aff_add_constant_si(one, 1);
5088 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5089 set = isl_set_complement(set);
5090 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5092 return pa;
5095 /* Plug in "subs" for dimension "type", "pos" of "aff".
5097 * Let i be the dimension to replace and let "subs" be of the form
5099 * f/d
5101 * and "aff" of the form
5103 * (a i + g)/m
5105 * The result is
5107 * (a f + d g')/(m d)
5109 * where g' is the result of plugging in "subs" in each of the integer
5110 * divisions in g.
5112 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5113 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5115 isl_ctx *ctx;
5116 isl_int v;
5118 aff = isl_aff_cow(aff);
5119 if (!aff || !subs)
5120 return isl_aff_free(aff);
5122 ctx = isl_aff_get_ctx(aff);
5123 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5124 isl_die(ctx, isl_error_invalid,
5125 "spaces don't match", return isl_aff_free(aff));
5126 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5127 isl_die(ctx, isl_error_unsupported,
5128 "cannot handle divs yet", return isl_aff_free(aff));
5130 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5131 if (!aff->ls)
5132 return isl_aff_free(aff);
5134 aff->v = isl_vec_cow(aff->v);
5135 if (!aff->v)
5136 return isl_aff_free(aff);
5138 pos += isl_local_space_offset(aff->ls, type);
5140 isl_int_init(v);
5141 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5142 aff->v->size, subs->v->size, v);
5143 isl_int_clear(v);
5145 return aff;
5148 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5149 * expressions in "maff".
5151 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5152 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5153 __isl_keep isl_aff *subs)
5155 int i;
5157 maff = isl_multi_aff_cow(maff);
5158 if (!maff || !subs)
5159 return isl_multi_aff_free(maff);
5161 if (type == isl_dim_in)
5162 type = isl_dim_set;
5164 for (i = 0; i < maff->n; ++i) {
5165 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5166 if (!maff->p[i])
5167 return isl_multi_aff_free(maff);
5170 return maff;
5173 /* Plug in "subs" for dimension "type", "pos" of "pma".
5175 * pma is of the form
5177 * A_i(v) -> M_i(v)
5179 * while subs is of the form
5181 * v' = B_j(v) -> S_j
5183 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5184 * has a contribution in the result, in particular
5186 * C_ij(S_j) -> M_i(S_j)
5188 * Note that plugging in S_j in C_ij may also result in an empty set
5189 * and this contribution should simply be discarded.
5191 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5192 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5193 __isl_keep isl_pw_aff *subs)
5195 int i, j, n;
5196 isl_pw_multi_aff *res;
5198 if (!pma || !subs)
5199 return isl_pw_multi_aff_free(pma);
5201 n = pma->n * subs->n;
5202 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5204 for (i = 0; i < pma->n; ++i) {
5205 for (j = 0; j < subs->n; ++j) {
5206 isl_set *common;
5207 isl_multi_aff *res_ij;
5208 int empty;
5210 common = isl_set_intersect(
5211 isl_set_copy(pma->p[i].set),
5212 isl_set_copy(subs->p[j].set));
5213 common = isl_set_substitute(common,
5214 type, pos, subs->p[j].aff);
5215 empty = isl_set_plain_is_empty(common);
5216 if (empty < 0 || empty) {
5217 isl_set_free(common);
5218 if (empty < 0)
5219 goto error;
5220 continue;
5223 res_ij = isl_multi_aff_substitute(
5224 isl_multi_aff_copy(pma->p[i].maff),
5225 type, pos, subs->p[j].aff);
5227 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5231 isl_pw_multi_aff_free(pma);
5232 return res;
5233 error:
5234 isl_pw_multi_aff_free(pma);
5235 isl_pw_multi_aff_free(res);
5236 return NULL;
5239 /* Compute the preimage of a range of dimensions in the affine expression "src"
5240 * under "ma" and put the result in "dst". The number of dimensions in "src"
5241 * that precede the range is given by "n_before". The number of dimensions
5242 * in the range is given by the number of output dimensions of "ma".
5243 * The number of dimensions that follow the range is given by "n_after".
5244 * If "has_denom" is set (to one),
5245 * then "src" and "dst" have an extra initial denominator.
5246 * "n_div_ma" is the number of existentials in "ma"
5247 * "n_div_bset" is the number of existentials in "src"
5248 * The resulting "dst" (which is assumed to have been allocated by
5249 * the caller) contains coefficients for both sets of existentials,
5250 * first those in "ma" and then those in "src".
5251 * f, c1, c2 and g are temporary objects that have been initialized
5252 * by the caller.
5254 * Let src represent the expression
5256 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5258 * and let ma represent the expressions
5260 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5262 * We start out with the following expression for dst:
5264 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5266 * with the multiplication factor f initially equal to 1
5267 * and f \sum_i b_i v_i kept separately.
5268 * For each x_i that we substitute, we multiply the numerator
5269 * (and denominator) of dst by c_1 = m_i and add the numerator
5270 * of the x_i expression multiplied by c_2 = f b_i,
5271 * after removing the common factors of c_1 and c_2.
5272 * The multiplication factor f also needs to be multiplied by c_1
5273 * for the next x_j, j > i.
5275 void isl_seq_preimage(isl_int *dst, isl_int *src,
5276 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5277 int n_div_ma, int n_div_bmap,
5278 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5280 int i;
5281 int n_param, n_in, n_out;
5282 int o_dst, o_src;
5284 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5285 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5286 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5288 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5289 o_dst = o_src = has_denom + 1 + n_param + n_before;
5290 isl_seq_clr(dst + o_dst, n_in);
5291 o_dst += n_in;
5292 o_src += n_out;
5293 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5294 o_dst += n_after;
5295 o_src += n_after;
5296 isl_seq_clr(dst + o_dst, n_div_ma);
5297 o_dst += n_div_ma;
5298 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5300 isl_int_set_si(f, 1);
5302 for (i = 0; i < n_out; ++i) {
5303 int offset = has_denom + 1 + n_param + n_before + i;
5305 if (isl_int_is_zero(src[offset]))
5306 continue;
5307 isl_int_set(c1, ma->p[i]->v->el[0]);
5308 isl_int_mul(c2, f, src[offset]);
5309 isl_int_gcd(g, c1, c2);
5310 isl_int_divexact(c1, c1, g);
5311 isl_int_divexact(c2, c2, g);
5313 isl_int_mul(f, f, c1);
5314 o_dst = has_denom;
5315 o_src = 1;
5316 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5317 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5318 o_dst += 1 + n_param;
5319 o_src += 1 + n_param;
5320 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5321 o_dst += n_before;
5322 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5323 c2, ma->p[i]->v->el + o_src, n_in);
5324 o_dst += n_in;
5325 o_src += n_in;
5326 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5327 o_dst += n_after;
5328 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5329 c2, ma->p[i]->v->el + o_src, n_div_ma);
5330 o_dst += n_div_ma;
5331 o_src += n_div_ma;
5332 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5333 if (has_denom)
5334 isl_int_mul(dst[0], dst[0], c1);
5338 /* Compute the pullback of "aff" by the function represented by "ma".
5339 * In other words, plug in "ma" in "aff". The result is an affine expression
5340 * defined over the domain space of "ma".
5342 * If "aff" is represented by
5344 * (a(p) + b x + c(divs))/d
5346 * and ma is represented by
5348 * x = D(p) + F(y) + G(divs')
5350 * then the result is
5352 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5354 * The divs in the local space of the input are similarly adjusted
5355 * through a call to isl_local_space_preimage_multi_aff.
5357 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5358 __isl_take isl_multi_aff *ma)
5360 isl_aff *res = NULL;
5361 isl_local_space *ls;
5362 int n_div_aff, n_div_ma;
5363 isl_int f, c1, c2, g;
5365 ma = isl_multi_aff_align_divs(ma);
5366 if (!aff || !ma)
5367 goto error;
5369 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5370 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5372 ls = isl_aff_get_domain_local_space(aff);
5373 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5374 res = isl_aff_alloc(ls);
5375 if (!res)
5376 goto error;
5378 isl_int_init(f);
5379 isl_int_init(c1);
5380 isl_int_init(c2);
5381 isl_int_init(g);
5383 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5384 f, c1, c2, g, 1);
5386 isl_int_clear(f);
5387 isl_int_clear(c1);
5388 isl_int_clear(c2);
5389 isl_int_clear(g);
5391 isl_aff_free(aff);
5392 isl_multi_aff_free(ma);
5393 res = isl_aff_normalize(res);
5394 return res;
5395 error:
5396 isl_aff_free(aff);
5397 isl_multi_aff_free(ma);
5398 isl_aff_free(res);
5399 return NULL;
5402 /* Compute the pullback of "aff1" by the function represented by "aff2".
5403 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5404 * defined over the domain space of "aff1".
5406 * The domain of "aff1" should match the range of "aff2", which means
5407 * that it should be single-dimensional.
5409 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5410 __isl_take isl_aff *aff2)
5412 isl_multi_aff *ma;
5414 ma = isl_multi_aff_from_aff(aff2);
5415 return isl_aff_pullback_multi_aff(aff1, ma);
5418 /* Compute the pullback of "ma1" by the function represented by "ma2".
5419 * In other words, plug in "ma2" in "ma1".
5421 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5423 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5424 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5426 int i;
5427 isl_space *space = NULL;
5429 ma2 = isl_multi_aff_align_divs(ma2);
5430 ma1 = isl_multi_aff_cow(ma1);
5431 if (!ma1 || !ma2)
5432 goto error;
5434 space = isl_space_join(isl_multi_aff_get_space(ma2),
5435 isl_multi_aff_get_space(ma1));
5437 for (i = 0; i < ma1->n; ++i) {
5438 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5439 isl_multi_aff_copy(ma2));
5440 if (!ma1->p[i])
5441 goto error;
5444 ma1 = isl_multi_aff_reset_space(ma1, space);
5445 isl_multi_aff_free(ma2);
5446 return ma1;
5447 error:
5448 isl_space_free(space);
5449 isl_multi_aff_free(ma2);
5450 isl_multi_aff_free(ma1);
5451 return NULL;
5454 /* Compute the pullback of "ma1" by the function represented by "ma2".
5455 * In other words, plug in "ma2" in "ma1".
5457 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5458 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5460 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5461 &isl_multi_aff_pullback_multi_aff_aligned);
5464 /* Extend the local space of "dst" to include the divs
5465 * in the local space of "src".
5467 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5468 __isl_keep isl_aff *src)
5470 isl_ctx *ctx;
5471 int *exp1 = NULL;
5472 int *exp2 = NULL;
5473 isl_mat *div;
5475 if (!src || !dst)
5476 return isl_aff_free(dst);
5478 ctx = isl_aff_get_ctx(src);
5479 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5480 isl_die(ctx, isl_error_invalid,
5481 "spaces don't match", goto error);
5483 if (src->ls->div->n_row == 0)
5484 return dst;
5486 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5487 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5488 if (!exp1 || (dst->ls->div->n_row && !exp2))
5489 goto error;
5491 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5492 dst = isl_aff_expand_divs(dst, div, exp2);
5493 free(exp1);
5494 free(exp2);
5496 return dst;
5497 error:
5498 free(exp1);
5499 free(exp2);
5500 return isl_aff_free(dst);
5503 /* Adjust the local spaces of the affine expressions in "maff"
5504 * such that they all have the save divs.
5506 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5507 __isl_take isl_multi_aff *maff)
5509 int i;
5511 if (!maff)
5512 return NULL;
5513 if (maff->n == 0)
5514 return maff;
5515 maff = isl_multi_aff_cow(maff);
5516 if (!maff)
5517 return NULL;
5519 for (i = 1; i < maff->n; ++i)
5520 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5521 for (i = 1; i < maff->n; ++i) {
5522 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5523 if (!maff->p[i])
5524 return isl_multi_aff_free(maff);
5527 return maff;
5530 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5532 aff = isl_aff_cow(aff);
5533 if (!aff)
5534 return NULL;
5536 aff->ls = isl_local_space_lift(aff->ls);
5537 if (!aff->ls)
5538 return isl_aff_free(aff);
5540 return aff;
5543 /* Lift "maff" to a space with extra dimensions such that the result
5544 * has no more existentially quantified variables.
5545 * If "ls" is not NULL, then *ls is assigned the local space that lies
5546 * at the basis of the lifting applied to "maff".
5548 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5549 __isl_give isl_local_space **ls)
5551 int i;
5552 isl_space *space;
5553 unsigned n_div;
5555 if (ls)
5556 *ls = NULL;
5558 if (!maff)
5559 return NULL;
5561 if (maff->n == 0) {
5562 if (ls) {
5563 isl_space *space = isl_multi_aff_get_domain_space(maff);
5564 *ls = isl_local_space_from_space(space);
5565 if (!*ls)
5566 return isl_multi_aff_free(maff);
5568 return maff;
5571 maff = isl_multi_aff_cow(maff);
5572 maff = isl_multi_aff_align_divs(maff);
5573 if (!maff)
5574 return NULL;
5576 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5577 space = isl_multi_aff_get_space(maff);
5578 space = isl_space_lift(isl_space_domain(space), n_div);
5579 space = isl_space_extend_domain_with_range(space,
5580 isl_multi_aff_get_space(maff));
5581 if (!space)
5582 return isl_multi_aff_free(maff);
5583 isl_space_free(maff->space);
5584 maff->space = space;
5586 if (ls) {
5587 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5588 if (!*ls)
5589 return isl_multi_aff_free(maff);
5592 for (i = 0; i < maff->n; ++i) {
5593 maff->p[i] = isl_aff_lift(maff->p[i]);
5594 if (!maff->p[i])
5595 goto error;
5598 return maff;
5599 error:
5600 if (ls)
5601 isl_local_space_free(*ls);
5602 return isl_multi_aff_free(maff);
5606 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5608 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5609 __isl_keep isl_pw_multi_aff *pma, int pos)
5611 int i;
5612 int n_out;
5613 isl_space *space;
5614 isl_pw_aff *pa;
5616 if (!pma)
5617 return NULL;
5619 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5620 if (pos < 0 || pos >= n_out)
5621 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5622 "index out of bounds", return NULL);
5624 space = isl_pw_multi_aff_get_space(pma);
5625 space = isl_space_drop_dims(space, isl_dim_out,
5626 pos + 1, n_out - pos - 1);
5627 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5629 pa = isl_pw_aff_alloc_size(space, pma->n);
5630 for (i = 0; i < pma->n; ++i) {
5631 isl_aff *aff;
5632 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5633 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5636 return pa;
5639 /* Return an isl_pw_multi_aff with the given "set" as domain and
5640 * an unnamed zero-dimensional range.
5642 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5643 __isl_take isl_set *set)
5645 isl_multi_aff *ma;
5646 isl_space *space;
5648 space = isl_set_get_space(set);
5649 space = isl_space_from_domain(space);
5650 ma = isl_multi_aff_zero(space);
5651 return isl_pw_multi_aff_alloc(set, ma);
5654 /* Add an isl_pw_multi_aff with the given "set" as domain and
5655 * an unnamed zero-dimensional range to *user.
5657 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5659 isl_union_pw_multi_aff **upma = user;
5660 isl_pw_multi_aff *pma;
5662 pma = isl_pw_multi_aff_from_domain(set);
5663 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5665 return 0;
5668 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5669 * an unnamed zero-dimensional range.
5671 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5672 __isl_take isl_union_set *uset)
5674 isl_space *space;
5675 isl_union_pw_multi_aff *upma;
5677 if (!uset)
5678 return NULL;
5680 space = isl_union_set_get_space(uset);
5681 upma = isl_union_pw_multi_aff_empty(space);
5683 if (isl_union_set_foreach_set(uset,
5684 &add_pw_multi_aff_from_domain, &upma) < 0)
5685 goto error;
5687 isl_union_set_free(uset);
5688 return upma;
5689 error:
5690 isl_union_set_free(uset);
5691 isl_union_pw_multi_aff_free(upma);
5692 return NULL;
5695 /* Convert "pma" to an isl_map and add it to *umap.
5697 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5699 isl_union_map **umap = user;
5700 isl_map *map;
5702 map = isl_map_from_pw_multi_aff(pma);
5703 *umap = isl_union_map_add_map(*umap, map);
5705 return 0;
5708 /* Construct a union map mapping the domain of the union
5709 * piecewise multi-affine expression to its range, with each dimension
5710 * in the range equated to the corresponding affine expression on its cell.
5712 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5713 __isl_take isl_union_pw_multi_aff *upma)
5715 isl_space *space;
5716 isl_union_map *umap;
5718 if (!upma)
5719 return NULL;
5721 space = isl_union_pw_multi_aff_get_space(upma);
5722 umap = isl_union_map_empty(space);
5724 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5725 &map_from_pw_multi_aff, &umap) < 0)
5726 goto error;
5728 isl_union_pw_multi_aff_free(upma);
5729 return umap;
5730 error:
5731 isl_union_pw_multi_aff_free(upma);
5732 isl_union_map_free(umap);
5733 return NULL;
5736 /* Local data for bin_entry and the callback "fn".
5738 struct isl_union_pw_multi_aff_bin_data {
5739 isl_union_pw_multi_aff *upma2;
5740 isl_union_pw_multi_aff *res;
5741 isl_pw_multi_aff *pma;
5742 int (*fn)(void **entry, void *user);
5745 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5746 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5748 static int bin_entry(void **entry, void *user)
5750 struct isl_union_pw_multi_aff_bin_data *data = user;
5751 isl_pw_multi_aff *pma = *entry;
5753 data->pma = pma;
5754 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5755 data->fn, data) < 0)
5756 return -1;
5758 return 0;
5761 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5762 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5763 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5764 * as *entry. The callback should adjust data->res if desired.
5766 static __isl_give isl_union_pw_multi_aff *bin_op(
5767 __isl_take isl_union_pw_multi_aff *upma1,
5768 __isl_take isl_union_pw_multi_aff *upma2,
5769 int (*fn)(void **entry, void *user))
5771 isl_space *space;
5772 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5774 space = isl_union_pw_multi_aff_get_space(upma2);
5775 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5776 space = isl_union_pw_multi_aff_get_space(upma1);
5777 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5779 if (!upma1 || !upma2)
5780 goto error;
5782 data.upma2 = upma2;
5783 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5784 upma1->table.n);
5785 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5786 &bin_entry, &data) < 0)
5787 goto error;
5789 isl_union_pw_multi_aff_free(upma1);
5790 isl_union_pw_multi_aff_free(upma2);
5791 return data.res;
5792 error:
5793 isl_union_pw_multi_aff_free(upma1);
5794 isl_union_pw_multi_aff_free(upma2);
5795 isl_union_pw_multi_aff_free(data.res);
5796 return NULL;
5799 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5800 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5802 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5803 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5805 isl_space *space;
5807 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5808 isl_pw_multi_aff_get_space(pma2));
5809 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5810 &isl_multi_aff_range_product);
5813 /* Given two isl_pw_multi_affs A -> B and C -> D,
5814 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5816 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5817 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5819 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5820 &pw_multi_aff_range_product);
5823 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5824 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5826 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5827 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5829 isl_space *space;
5831 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5832 isl_pw_multi_aff_get_space(pma2));
5833 space = isl_space_flatten_range(space);
5834 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5835 &isl_multi_aff_flat_range_product);
5838 /* Given two isl_pw_multi_affs A -> B and C -> D,
5839 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5841 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5842 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5844 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5845 &pw_multi_aff_flat_range_product);
5848 /* If data->pma and *entry have the same domain space, then compute
5849 * their flat range product and the result to data->res.
5851 static int flat_range_product_entry(void **entry, void *user)
5853 struct isl_union_pw_multi_aff_bin_data *data = user;
5854 isl_pw_multi_aff *pma2 = *entry;
5856 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5857 pma2->dim, isl_dim_in))
5858 return 0;
5860 pma2 = isl_pw_multi_aff_flat_range_product(
5861 isl_pw_multi_aff_copy(data->pma),
5862 isl_pw_multi_aff_copy(pma2));
5864 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5866 return 0;
5869 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5870 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5872 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5873 __isl_take isl_union_pw_multi_aff *upma1,
5874 __isl_take isl_union_pw_multi_aff *upma2)
5876 return bin_op(upma1, upma2, &flat_range_product_entry);
5879 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5880 * The parameters are assumed to have been aligned.
5882 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5883 * except that it works on two different isl_pw_* types.
5885 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5886 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5887 __isl_take isl_pw_aff *pa)
5889 int i, j, n;
5890 isl_pw_multi_aff *res = NULL;
5892 if (!pma || !pa)
5893 goto error;
5895 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5896 pa->dim, isl_dim_in))
5897 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5898 "domains don't match", goto error);
5899 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5900 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5901 "index out of bounds", goto error);
5903 n = pma->n * pa->n;
5904 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5906 for (i = 0; i < pma->n; ++i) {
5907 for (j = 0; j < pa->n; ++j) {
5908 isl_set *common;
5909 isl_multi_aff *res_ij;
5910 int empty;
5912 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5913 isl_set_copy(pa->p[j].set));
5914 empty = isl_set_plain_is_empty(common);
5915 if (empty < 0 || empty) {
5916 isl_set_free(common);
5917 if (empty < 0)
5918 goto error;
5919 continue;
5922 res_ij = isl_multi_aff_set_aff(
5923 isl_multi_aff_copy(pma->p[i].maff), pos,
5924 isl_aff_copy(pa->p[j].aff));
5925 res_ij = isl_multi_aff_gist(res_ij,
5926 isl_set_copy(common));
5928 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5932 isl_pw_multi_aff_free(pma);
5933 isl_pw_aff_free(pa);
5934 return res;
5935 error:
5936 isl_pw_multi_aff_free(pma);
5937 isl_pw_aff_free(pa);
5938 return isl_pw_multi_aff_free(res);
5941 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5943 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5944 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5945 __isl_take isl_pw_aff *pa)
5947 if (!pma || !pa)
5948 goto error;
5949 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5950 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5951 if (!isl_space_has_named_params(pma->dim) ||
5952 !isl_space_has_named_params(pa->dim))
5953 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5954 "unaligned unnamed parameters", goto error);
5955 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5956 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5957 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5958 error:
5959 isl_pw_multi_aff_free(pma);
5960 isl_pw_aff_free(pa);
5961 return NULL;
5964 /* Do the parameters of "pa" match those of "space"?
5966 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5967 __isl_keep isl_space *space)
5969 isl_space *pa_space;
5970 int match;
5972 if (!pa || !space)
5973 return -1;
5975 pa_space = isl_pw_aff_get_space(pa);
5977 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5979 isl_space_free(pa_space);
5980 return match;
5983 /* Check that the domain space of "pa" matches "space".
5985 * Return 0 on success and -1 on error.
5987 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5988 __isl_keep isl_space *space)
5990 isl_space *pa_space;
5991 int match;
5993 if (!pa || !space)
5994 return -1;
5996 pa_space = isl_pw_aff_get_space(pa);
5998 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5999 if (match < 0)
6000 goto error;
6001 if (!match)
6002 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6003 "parameters don't match", goto error);
6004 match = isl_space_tuple_is_equal(space, isl_dim_in,
6005 pa_space, isl_dim_in);
6006 if (match < 0)
6007 goto error;
6008 if (!match)
6009 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6010 "domains don't match", goto error);
6011 isl_space_free(pa_space);
6012 return 0;
6013 error:
6014 isl_space_free(pa_space);
6015 return -1;
6018 #undef BASE
6019 #define BASE pw_aff
6020 #undef DOMBASE
6021 #define DOMBASE set
6023 #include <isl_multi_templ.c>
6024 #include <isl_multi_apply_set.c>
6025 #include <isl_multi_gist.c>
6026 #include <isl_multi_intersect.c>
6028 /* Scale the elements of "pma" by the corresponding elements of "mv".
6030 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6031 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6033 int i;
6035 pma = isl_pw_multi_aff_cow(pma);
6036 if (!pma || !mv)
6037 goto error;
6038 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6039 mv->space, isl_dim_set))
6040 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6041 "spaces don't match", goto error);
6042 if (!isl_space_match(pma->dim, isl_dim_param,
6043 mv->space, isl_dim_param)) {
6044 pma = isl_pw_multi_aff_align_params(pma,
6045 isl_multi_val_get_space(mv));
6046 mv = isl_multi_val_align_params(mv,
6047 isl_pw_multi_aff_get_space(pma));
6048 if (!pma || !mv)
6049 goto error;
6052 for (i = 0; i < pma->n; ++i) {
6053 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6054 isl_multi_val_copy(mv));
6055 if (!pma->p[i].maff)
6056 goto error;
6059 isl_multi_val_free(mv);
6060 return pma;
6061 error:
6062 isl_multi_val_free(mv);
6063 isl_pw_multi_aff_free(pma);
6064 return NULL;
6067 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
6068 * mv contains the mv argument.
6069 * res collects the results.
6071 struct isl_union_pw_multi_aff_scale_multi_val_data {
6072 isl_multi_val *mv;
6073 isl_union_pw_multi_aff *res;
6076 /* This function is called for each entry of an isl_union_pw_multi_aff.
6077 * If the space of the entry matches that of data->mv,
6078 * then apply isl_pw_multi_aff_scale_multi_val and add the result
6079 * to data->res.
6081 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
6083 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
6084 isl_pw_multi_aff *pma = *entry;
6086 if (!pma)
6087 return -1;
6088 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6089 data->mv->space, isl_dim_set))
6090 return 0;
6092 pma = isl_pw_multi_aff_copy(pma);
6093 pma = isl_pw_multi_aff_scale_multi_val(pma,
6094 isl_multi_val_copy(data->mv));
6095 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6096 if (!data->res)
6097 return -1;
6099 return 0;
6102 /* Scale the elements of "upma" by the corresponding elements of "mv",
6103 * for those entries that match the space of "mv".
6105 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6106 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6108 struct isl_union_pw_multi_aff_scale_multi_val_data data;
6110 upma = isl_union_pw_multi_aff_align_params(upma,
6111 isl_multi_val_get_space(mv));
6112 mv = isl_multi_val_align_params(mv,
6113 isl_union_pw_multi_aff_get_space(upma));
6114 if (!upma || !mv)
6115 goto error;
6117 data.mv = mv;
6118 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
6119 upma->table.n);
6120 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
6121 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
6122 goto error;
6124 isl_multi_val_free(mv);
6125 isl_union_pw_multi_aff_free(upma);
6126 return data.res;
6127 error:
6128 isl_multi_val_free(mv);
6129 isl_union_pw_multi_aff_free(upma);
6130 return NULL;
6133 /* Construct and return a piecewise multi affine expression
6134 * in the given space with value zero in each of the output dimensions and
6135 * a universe domain.
6137 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6139 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6142 /* Construct and return a piecewise multi affine expression
6143 * that is equal to the given piecewise affine expression.
6145 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6146 __isl_take isl_pw_aff *pa)
6148 int i;
6149 isl_space *space;
6150 isl_pw_multi_aff *pma;
6152 if (!pa)
6153 return NULL;
6155 space = isl_pw_aff_get_space(pa);
6156 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6158 for (i = 0; i < pa->n; ++i) {
6159 isl_set *set;
6160 isl_multi_aff *ma;
6162 set = isl_set_copy(pa->p[i].set);
6163 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6164 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6167 isl_pw_aff_free(pa);
6168 return pma;
6171 /* Construct a set or map mapping the shared (parameter) domain
6172 * of the piecewise affine expressions to the range of "mpa"
6173 * with each dimension in the range equated to the
6174 * corresponding piecewise affine expression.
6176 static __isl_give isl_map *map_from_multi_pw_aff(
6177 __isl_take isl_multi_pw_aff *mpa)
6179 int i;
6180 isl_space *space;
6181 isl_map *map;
6183 if (!mpa)
6184 return NULL;
6186 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6187 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6188 "invalid space", goto error);
6190 space = isl_multi_pw_aff_get_domain_space(mpa);
6191 map = isl_map_universe(isl_space_from_domain(space));
6193 for (i = 0; i < mpa->n; ++i) {
6194 isl_pw_aff *pa;
6195 isl_map *map_i;
6197 pa = isl_pw_aff_copy(mpa->p[i]);
6198 map_i = map_from_pw_aff(pa);
6200 map = isl_map_flat_range_product(map, map_i);
6203 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6205 isl_multi_pw_aff_free(mpa);
6206 return map;
6207 error:
6208 isl_multi_pw_aff_free(mpa);
6209 return NULL;
6212 /* Construct a map mapping the shared domain
6213 * of the piecewise affine expressions to the range of "mpa"
6214 * with each dimension in the range equated to the
6215 * corresponding piecewise affine expression.
6217 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6219 if (!mpa)
6220 return NULL;
6221 if (isl_space_is_set(mpa->space))
6222 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6223 "space of input is not a map", goto error);
6225 return map_from_multi_pw_aff(mpa);
6226 error:
6227 isl_multi_pw_aff_free(mpa);
6228 return NULL;
6231 /* Construct a set mapping the shared parameter domain
6232 * of the piecewise affine expressions to the space of "mpa"
6233 * with each dimension in the range equated to the
6234 * corresponding piecewise affine expression.
6236 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6238 if (!mpa)
6239 return NULL;
6240 if (!isl_space_is_set(mpa->space))
6241 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6242 "space of input is not a set", goto error);
6244 return map_from_multi_pw_aff(mpa);
6245 error:
6246 isl_multi_pw_aff_free(mpa);
6247 return NULL;
6250 /* Construct and return a piecewise multi affine expression
6251 * that is equal to the given multi piecewise affine expression
6252 * on the shared domain of the piecewise affine expressions.
6254 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6255 __isl_take isl_multi_pw_aff *mpa)
6257 int i;
6258 isl_space *space;
6259 isl_pw_aff *pa;
6260 isl_pw_multi_aff *pma;
6262 if (!mpa)
6263 return NULL;
6265 space = isl_multi_pw_aff_get_space(mpa);
6267 if (mpa->n == 0) {
6268 isl_multi_pw_aff_free(mpa);
6269 return isl_pw_multi_aff_zero(space);
6272 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6273 pma = isl_pw_multi_aff_from_pw_aff(pa);
6275 for (i = 1; i < mpa->n; ++i) {
6276 isl_pw_multi_aff *pma_i;
6278 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6279 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6280 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6283 pma = isl_pw_multi_aff_reset_space(pma, space);
6285 isl_multi_pw_aff_free(mpa);
6286 return pma;
6289 /* Construct and return a multi piecewise affine expression
6290 * that is equal to the given multi affine expression.
6292 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6293 __isl_take isl_multi_aff *ma)
6295 int i, n;
6296 isl_multi_pw_aff *mpa;
6298 if (!ma)
6299 return NULL;
6301 n = isl_multi_aff_dim(ma, isl_dim_out);
6302 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6304 for (i = 0; i < n; ++i) {
6305 isl_pw_aff *pa;
6307 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6308 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6311 isl_multi_aff_free(ma);
6312 return mpa;
6315 /* Construct and return a multi piecewise affine expression
6316 * that is equal to the given piecewise multi affine expression.
6318 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6319 __isl_take isl_pw_multi_aff *pma)
6321 int i, n;
6322 isl_space *space;
6323 isl_multi_pw_aff *mpa;
6325 if (!pma)
6326 return NULL;
6328 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6329 space = isl_pw_multi_aff_get_space(pma);
6330 mpa = isl_multi_pw_aff_alloc(space);
6332 for (i = 0; i < n; ++i) {
6333 isl_pw_aff *pa;
6335 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6336 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6339 isl_pw_multi_aff_free(pma);
6340 return mpa;
6343 /* Do "pa1" and "pa2" represent the same function?
6345 * We first check if they are obviously equal.
6346 * If not, we convert them to maps and check if those are equal.
6348 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6350 int equal;
6351 isl_map *map1, *map2;
6353 if (!pa1 || !pa2)
6354 return -1;
6356 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6357 if (equal < 0 || equal)
6358 return equal;
6360 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6361 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6362 equal = isl_map_is_equal(map1, map2);
6363 isl_map_free(map1);
6364 isl_map_free(map2);
6366 return equal;
6369 /* Do "mpa1" and "mpa2" represent the same function?
6371 * Note that we cannot convert the entire isl_multi_pw_aff
6372 * to a map because the domains of the piecewise affine expressions
6373 * may not be the same.
6375 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6376 __isl_keep isl_multi_pw_aff *mpa2)
6378 int i;
6379 int equal;
6381 if (!mpa1 || !mpa2)
6382 return -1;
6384 if (!isl_space_match(mpa1->space, isl_dim_param,
6385 mpa2->space, isl_dim_param)) {
6386 if (!isl_space_has_named_params(mpa1->space))
6387 return 0;
6388 if (!isl_space_has_named_params(mpa2->space))
6389 return 0;
6390 mpa1 = isl_multi_pw_aff_copy(mpa1);
6391 mpa2 = isl_multi_pw_aff_copy(mpa2);
6392 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6393 isl_multi_pw_aff_get_space(mpa2));
6394 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6395 isl_multi_pw_aff_get_space(mpa1));
6396 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6397 isl_multi_pw_aff_free(mpa1);
6398 isl_multi_pw_aff_free(mpa2);
6399 return equal;
6402 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6403 if (equal < 0 || !equal)
6404 return equal;
6406 for (i = 0; i < mpa1->n; ++i) {
6407 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6408 if (equal < 0 || !equal)
6409 return equal;
6412 return 1;
6415 /* Coalesce the elements of "mpa".
6417 * Note that such coalescing does not change the meaning of "mpa"
6418 * so there is no need to cow. We do need to be careful not to
6419 * destroy any other copies of "mpa" in case of failure.
6421 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6422 __isl_take isl_multi_pw_aff *mpa)
6424 int i;
6426 if (!mpa)
6427 return NULL;
6429 for (i = 0; i < mpa->n; ++i) {
6430 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6431 pa = isl_pw_aff_coalesce(pa);
6432 if (!pa)
6433 return isl_multi_pw_aff_free(mpa);
6434 isl_pw_aff_free(mpa->p[i]);
6435 mpa->p[i] = pa;
6438 return mpa;
6441 /* Compute the pullback of "mpa" by the function represented by "ma".
6442 * In other words, plug in "ma" in "mpa".
6444 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6446 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6447 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6449 int i;
6450 isl_space *space = NULL;
6452 mpa = isl_multi_pw_aff_cow(mpa);
6453 if (!mpa || !ma)
6454 goto error;
6456 space = isl_space_join(isl_multi_aff_get_space(ma),
6457 isl_multi_pw_aff_get_space(mpa));
6458 if (!space)
6459 goto error;
6461 for (i = 0; i < mpa->n; ++i) {
6462 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6463 isl_multi_aff_copy(ma));
6464 if (!mpa->p[i])
6465 goto error;
6468 isl_multi_aff_free(ma);
6469 isl_space_free(mpa->space);
6470 mpa->space = space;
6471 return mpa;
6472 error:
6473 isl_space_free(space);
6474 isl_multi_pw_aff_free(mpa);
6475 isl_multi_aff_free(ma);
6476 return NULL;
6479 /* Compute the pullback of "mpa" by the function represented by "ma".
6480 * In other words, plug in "ma" in "mpa".
6482 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6483 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6485 if (!mpa || !ma)
6486 goto error;
6487 if (isl_space_match(mpa->space, isl_dim_param,
6488 ma->space, isl_dim_param))
6489 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6490 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6491 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6492 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6493 error:
6494 isl_multi_pw_aff_free(mpa);
6495 isl_multi_aff_free(ma);
6496 return NULL;
6499 /* Compute the pullback of "mpa" by the function represented by "pma".
6500 * In other words, plug in "pma" in "mpa".
6502 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6504 static __isl_give isl_multi_pw_aff *
6505 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6506 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6508 int i;
6509 isl_space *space = NULL;
6511 mpa = isl_multi_pw_aff_cow(mpa);
6512 if (!mpa || !pma)
6513 goto error;
6515 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6516 isl_multi_pw_aff_get_space(mpa));
6518 for (i = 0; i < mpa->n; ++i) {
6519 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6520 isl_pw_multi_aff_copy(pma));
6521 if (!mpa->p[i])
6522 goto error;
6525 isl_pw_multi_aff_free(pma);
6526 isl_space_free(mpa->space);
6527 mpa->space = space;
6528 return mpa;
6529 error:
6530 isl_space_free(space);
6531 isl_multi_pw_aff_free(mpa);
6532 isl_pw_multi_aff_free(pma);
6533 return NULL;
6536 /* Compute the pullback of "mpa" by the function represented by "pma".
6537 * In other words, plug in "pma" in "mpa".
6539 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6540 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6542 if (!mpa || !pma)
6543 goto error;
6544 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6545 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6546 mpa = isl_multi_pw_aff_align_params(mpa,
6547 isl_pw_multi_aff_get_space(pma));
6548 pma = isl_pw_multi_aff_align_params(pma,
6549 isl_multi_pw_aff_get_space(mpa));
6550 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6551 error:
6552 isl_multi_pw_aff_free(mpa);
6553 isl_pw_multi_aff_free(pma);
6554 return NULL;
6557 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6558 * with the domain of "aff". The domain of the result is the same
6559 * as that of "mpa".
6560 * "mpa" and "aff" are assumed to have been aligned.
6562 * We first extract the parametric constant from "aff", defined
6563 * over the correct domain.
6564 * Then we add the appropriate combinations of the members of "mpa".
6565 * Finally, we add the integer divisions through recursive calls.
6567 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6568 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6570 int i, n_in, n_div;
6571 isl_space *space;
6572 isl_val *v;
6573 isl_pw_aff *pa;
6574 isl_aff *tmp;
6576 n_in = isl_aff_dim(aff, isl_dim_in);
6577 n_div = isl_aff_dim(aff, isl_dim_div);
6579 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6580 tmp = isl_aff_copy(aff);
6581 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6582 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6583 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6584 isl_space_dim(space, isl_dim_set));
6585 tmp = isl_aff_reset_domain_space(tmp, space);
6586 pa = isl_pw_aff_from_aff(tmp);
6588 for (i = 0; i < n_in; ++i) {
6589 isl_pw_aff *pa_i;
6591 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6592 continue;
6593 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6594 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6595 pa_i = isl_pw_aff_scale_val(pa_i, v);
6596 pa = isl_pw_aff_add(pa, pa_i);
6599 for (i = 0; i < n_div; ++i) {
6600 isl_aff *div;
6601 isl_pw_aff *pa_i;
6603 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6604 continue;
6605 div = isl_aff_get_div(aff, i);
6606 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6607 isl_multi_pw_aff_copy(mpa), div);
6608 pa_i = isl_pw_aff_floor(pa_i);
6609 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6610 pa_i = isl_pw_aff_scale_val(pa_i, v);
6611 pa = isl_pw_aff_add(pa, pa_i);
6614 isl_multi_pw_aff_free(mpa);
6615 isl_aff_free(aff);
6617 return pa;
6620 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6621 * with the domain of "aff". The domain of the result is the same
6622 * as that of "mpa".
6624 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6625 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6627 if (!aff || !mpa)
6628 goto error;
6629 if (isl_space_match(aff->ls->dim, isl_dim_param,
6630 mpa->space, isl_dim_param))
6631 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6633 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6634 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6636 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6637 error:
6638 isl_aff_free(aff);
6639 isl_multi_pw_aff_free(mpa);
6640 return NULL;
6643 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6644 * with the domain of "pa". The domain of the result is the same
6645 * as that of "mpa".
6646 * "mpa" and "pa" are assumed to have been aligned.
6648 * We consider each piece in turn. Note that the domains of the
6649 * pieces are assumed to be disjoint and they remain disjoint
6650 * after taking the preimage (over the same function).
6652 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6653 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6655 isl_space *space;
6656 isl_pw_aff *res;
6657 int i;
6659 if (!mpa || !pa)
6660 goto error;
6662 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6663 isl_pw_aff_get_space(pa));
6664 res = isl_pw_aff_empty(space);
6666 for (i = 0; i < pa->n; ++i) {
6667 isl_pw_aff *pa_i;
6668 isl_set *domain;
6670 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6671 isl_multi_pw_aff_copy(mpa),
6672 isl_aff_copy(pa->p[i].aff));
6673 domain = isl_set_copy(pa->p[i].set);
6674 domain = isl_set_preimage_multi_pw_aff(domain,
6675 isl_multi_pw_aff_copy(mpa));
6676 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6677 res = isl_pw_aff_add_disjoint(res, pa_i);
6680 isl_pw_aff_free(pa);
6681 isl_multi_pw_aff_free(mpa);
6682 return res;
6683 error:
6684 isl_pw_aff_free(pa);
6685 isl_multi_pw_aff_free(mpa);
6686 return NULL;
6689 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6690 * with the domain of "pa". The domain of the result is the same
6691 * as that of "mpa".
6693 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6694 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6696 if (!pa || !mpa)
6697 goto error;
6698 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6699 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6701 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6702 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6704 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6705 error:
6706 isl_pw_aff_free(pa);
6707 isl_multi_pw_aff_free(mpa);
6708 return NULL;
6711 /* Compute the pullback of "pa" by the function represented by "mpa".
6712 * In other words, plug in "mpa" in "pa".
6713 * "pa" and "mpa" are assumed to have been aligned.
6715 * The pullback is computed by applying "pa" to "mpa".
6717 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6718 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6720 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6723 /* Compute the pullback of "pa" by the function represented by "mpa".
6724 * In other words, plug in "mpa" in "pa".
6726 * The pullback is computed by applying "pa" to "mpa".
6728 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6729 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6731 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6734 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6735 * In other words, plug in "mpa2" in "mpa1".
6737 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6739 * We pullback each member of "mpa1" in turn.
6741 static __isl_give isl_multi_pw_aff *
6742 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6743 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6745 int i;
6746 isl_space *space = NULL;
6748 mpa1 = isl_multi_pw_aff_cow(mpa1);
6749 if (!mpa1 || !mpa2)
6750 goto error;
6752 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6753 isl_multi_pw_aff_get_space(mpa1));
6755 for (i = 0; i < mpa1->n; ++i) {
6756 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6757 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6758 if (!mpa1->p[i])
6759 goto error;
6762 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6764 isl_multi_pw_aff_free(mpa2);
6765 return mpa1;
6766 error:
6767 isl_space_free(space);
6768 isl_multi_pw_aff_free(mpa1);
6769 isl_multi_pw_aff_free(mpa2);
6770 return NULL;
6773 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6774 * In other words, plug in "mpa2" in "mpa1".
6776 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6777 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6779 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6780 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6783 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6784 * of "mpa1" and "mpa2" live in the same space, construct map space
6785 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6786 * with this map space as extract argument.
6788 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6789 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6790 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6791 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6793 int match;
6794 isl_space *space1, *space2;
6795 isl_map *res;
6797 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6798 isl_multi_pw_aff_get_space(mpa2));
6799 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6800 isl_multi_pw_aff_get_space(mpa1));
6801 if (!mpa1 || !mpa2)
6802 goto error;
6803 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6804 mpa2->space, isl_dim_out);
6805 if (match < 0)
6806 goto error;
6807 if (!match)
6808 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6809 "range spaces don't match", goto error);
6810 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6811 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6812 space1 = isl_space_map_from_domain_and_range(space1, space2);
6814 res = order(mpa1, mpa2, space1);
6815 isl_multi_pw_aff_free(mpa1);
6816 isl_multi_pw_aff_free(mpa2);
6817 return res;
6818 error:
6819 isl_multi_pw_aff_free(mpa1);
6820 isl_multi_pw_aff_free(mpa2);
6821 return NULL;
6824 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6825 * where the function values are equal. "space" is the space of the result.
6826 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6828 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6829 * in the sequences are equal.
6831 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6832 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6833 __isl_take isl_space *space)
6835 int i, n;
6836 isl_map *res;
6838 res = isl_map_universe(space);
6840 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6841 for (i = 0; i < n; ++i) {
6842 isl_pw_aff *pa1, *pa2;
6843 isl_map *map;
6845 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6846 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6847 map = isl_pw_aff_eq_map(pa1, pa2);
6848 res = isl_map_intersect(res, map);
6851 return res;
6854 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6855 * where the function values are equal.
6857 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6858 __isl_take isl_multi_pw_aff *mpa2)
6860 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6861 &isl_multi_pw_aff_eq_map_on_space);
6864 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6865 * where the function values of "mpa1" is lexicographically satisfies "base"
6866 * compared to that of "mpa2". "space" is the space of the result.
6867 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6869 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6870 * if its i-th element satisfies "base" when compared to
6871 * the i-th element of "mpa2" while all previous elements are
6872 * pairwise equal.
6874 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6875 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6876 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6877 __isl_take isl_pw_aff *pa2),
6878 __isl_take isl_space *space)
6880 int i, n;
6881 isl_map *res, *rest;
6883 res = isl_map_empty(isl_space_copy(space));
6884 rest = isl_map_universe(space);
6886 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6887 for (i = 0; i < n; ++i) {
6888 isl_pw_aff *pa1, *pa2;
6889 isl_map *map;
6891 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6892 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6893 map = base(pa1, pa2);
6894 map = isl_map_intersect(map, isl_map_copy(rest));
6895 res = isl_map_union(res, map);
6897 if (i == n - 1)
6898 continue;
6900 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6901 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6902 map = isl_pw_aff_eq_map(pa1, pa2);
6903 rest = isl_map_intersect(rest, map);
6906 isl_map_free(rest);
6907 return res;
6910 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6911 * where the function value of "mpa1" is lexicographically less than that
6912 * of "mpa2". "space" is the space of the result.
6913 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6915 * "mpa1" is less than "mpa2" if its i-th element is smaller
6916 * than the i-th element of "mpa2" while all previous elements are
6917 * pairwise equal.
6919 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6920 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6921 __isl_take isl_space *space)
6923 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6924 &isl_pw_aff_lt_map, space);
6927 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6928 * where the function value of "mpa1" is lexicographically less than that
6929 * of "mpa2".
6931 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6932 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6934 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6935 &isl_multi_pw_aff_lex_lt_map_on_space);
6938 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6939 * where the function value of "mpa1" is lexicographically greater than that
6940 * of "mpa2". "space" is the space of the result.
6941 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6943 * "mpa1" is greater than "mpa2" if its i-th element is greater
6944 * than the i-th element of "mpa2" while all previous elements are
6945 * pairwise equal.
6947 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6948 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6949 __isl_take isl_space *space)
6951 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6952 &isl_pw_aff_gt_map, space);
6955 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6956 * where the function value of "mpa1" is lexicographically greater than that
6957 * of "mpa2".
6959 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
6960 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6962 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6963 &isl_multi_pw_aff_lex_gt_map_on_space);
6966 /* Compare two isl_affs.
6968 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6969 * than "aff2" and 0 if they are equal.
6971 * The order is fairly arbitrary. We do consider expressions that only involve
6972 * earlier dimensions as "smaller".
6974 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6976 int cmp;
6977 int last1, last2;
6979 if (aff1 == aff2)
6980 return 0;
6982 if (!aff1)
6983 return -1;
6984 if (!aff2)
6985 return 1;
6987 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6988 if (cmp != 0)
6989 return cmp;
6991 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6992 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6993 if (last1 != last2)
6994 return last1 - last2;
6996 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6999 /* Compare two isl_pw_affs.
7001 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7002 * than "pa2" and 0 if they are equal.
7004 * The order is fairly arbitrary. We do consider expressions that only involve
7005 * earlier dimensions as "smaller".
7007 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7008 __isl_keep isl_pw_aff *pa2)
7010 int i;
7011 int cmp;
7013 if (pa1 == pa2)
7014 return 0;
7016 if (!pa1)
7017 return -1;
7018 if (!pa2)
7019 return 1;
7021 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7022 if (cmp != 0)
7023 return cmp;
7025 if (pa1->n != pa2->n)
7026 return pa1->n - pa2->n;
7028 for (i = 0; i < pa1->n; ++i) {
7029 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7030 if (cmp != 0)
7031 return cmp;
7032 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7033 if (cmp != 0)
7034 return cmp;
7037 return 0;
7040 /* Return a piecewise affine expression that is equal to "v" on "domain".
7042 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7043 __isl_take isl_val *v)
7045 isl_space *space;
7046 isl_local_space *ls;
7047 isl_aff *aff;
7049 space = isl_set_get_space(domain);
7050 ls = isl_local_space_from_space(space);
7051 aff = isl_aff_val_on_domain(ls, v);
7053 return isl_pw_aff_alloc(domain, aff);
7056 /* Return a multi affine expression that is equal to "mv" on domain
7057 * space "space".
7059 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7060 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7062 int i, n;
7063 isl_space *space2;
7064 isl_local_space *ls;
7065 isl_multi_aff *ma;
7067 if (!space || !mv)
7068 goto error;
7070 n = isl_multi_val_dim(mv, isl_dim_set);
7071 space2 = isl_multi_val_get_space(mv);
7072 space2 = isl_space_align_params(space2, isl_space_copy(space));
7073 space = isl_space_align_params(space, isl_space_copy(space2));
7074 space = isl_space_map_from_domain_and_range(space, space2);
7075 ma = isl_multi_aff_alloc(isl_space_copy(space));
7076 ls = isl_local_space_from_space(isl_space_domain(space));
7077 for (i = 0; i < n; ++i) {
7078 isl_val *v;
7079 isl_aff *aff;
7081 v = isl_multi_val_get_val(mv, i);
7082 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7083 ma = isl_multi_aff_set_aff(ma, i, aff);
7085 isl_local_space_free(ls);
7087 isl_multi_val_free(mv);
7088 return ma;
7089 error:
7090 isl_space_free(space);
7091 isl_multi_val_free(mv);
7092 return NULL;
7095 /* Return a piecewise multi-affine expression
7096 * that is equal to "mv" on "domain".
7098 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7099 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7101 isl_space *space;
7102 isl_multi_aff *ma;
7104 space = isl_set_get_space(domain);
7105 ma = isl_multi_aff_multi_val_on_space(space, mv);
7107 return isl_pw_multi_aff_alloc(domain, ma);
7110 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7111 * mv is the value that should be attained on each domain set
7112 * res collects the results
7114 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7115 isl_multi_val *mv;
7116 isl_union_pw_multi_aff *res;
7119 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7120 * and add it to data->res.
7122 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7123 void *user)
7125 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7126 isl_pw_multi_aff *pma;
7127 isl_multi_val *mv;
7129 mv = isl_multi_val_copy(data->mv);
7130 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7131 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7133 return data->res ? 0 : -1;
7136 /* Return a union piecewise multi-affine expression
7137 * that is equal to "mv" on "domain".
7139 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7140 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7142 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7143 isl_space *space;
7145 space = isl_union_set_get_space(domain);
7146 data.res = isl_union_pw_multi_aff_empty(space);
7147 data.mv = mv;
7148 if (isl_union_set_foreach_set(domain,
7149 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7150 data.res = isl_union_pw_multi_aff_free(data.res);
7151 isl_union_set_free(domain);
7152 isl_multi_val_free(mv);
7153 return data.res;
7156 /* Compute the pullback of data->pma by the function represented by "pma2",
7157 * provided the spaces match, and add the results to data->res.
7159 static int pullback_entry(void **entry, void *user)
7161 struct isl_union_pw_multi_aff_bin_data *data = user;
7162 isl_pw_multi_aff *pma2 = *entry;
7164 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7165 pma2->dim, isl_dim_out))
7166 return 0;
7168 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7169 isl_pw_multi_aff_copy(data->pma),
7170 isl_pw_multi_aff_copy(pma2));
7172 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7173 if (!data->res)
7174 return -1;
7176 return 0;
7179 /* Compute the pullback of "upma1" by the function represented by "upma2".
7181 __isl_give isl_union_pw_multi_aff *
7182 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7183 __isl_take isl_union_pw_multi_aff *upma1,
7184 __isl_take isl_union_pw_multi_aff *upma2)
7186 return bin_op(upma1, upma2, &pullback_entry);
7189 /* Check that the domain space of "upa" matches "space".
7191 * Return 0 on success and -1 on error.
7193 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7194 * can in principle never fail since the space "space" is that
7195 * of the isl_multi_union_pw_aff and is a set space such that
7196 * there is no domain space to match.
7198 * We check the parameters and double-check that "space" is
7199 * indeed that of a set.
7201 static int isl_union_pw_aff_check_match_domain_space(
7202 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7204 isl_space *upa_space;
7205 int match;
7207 if (!upa || !space)
7208 return -1;
7210 match = isl_space_is_set(space);
7211 if (match < 0)
7212 return -1;
7213 if (!match)
7214 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7215 "expecting set space", return -1);
7217 upa_space = isl_union_pw_aff_get_space(upa);
7218 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7219 if (match < 0)
7220 goto error;
7221 if (!match)
7222 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7223 "parameters don't match", goto error);
7225 isl_space_free(upa_space);
7226 return 0;
7227 error:
7228 isl_space_free(upa_space);
7229 return -1;
7232 /* Do the parameters of "upa" match those of "space"?
7234 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7235 __isl_keep isl_space *space)
7237 isl_space *upa_space;
7238 int match;
7240 if (!upa || !space)
7241 return -1;
7243 upa_space = isl_union_pw_aff_get_space(upa);
7245 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7247 isl_space_free(upa_space);
7248 return match;
7251 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7252 * space represents the new parameters.
7253 * res collects the results.
7255 struct isl_union_pw_aff_reset_params_data {
7256 isl_space *space;
7257 isl_union_pw_aff *res;
7260 /* Replace the parameters of "pa" by data->space and
7261 * add the result to data->res.
7263 static int reset_params(__isl_take isl_pw_aff *pa, void *user)
7265 struct isl_union_pw_aff_reset_params_data *data = user;
7266 isl_space *space;
7268 space = isl_pw_aff_get_space(pa);
7269 space = isl_space_replace(space, isl_dim_param, data->space);
7270 pa = isl_pw_aff_reset_space(pa, space);
7271 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7273 return data->res ? 0 : -1;
7276 /* Replace the domain space of "upa" by "space".
7277 * Since a union expression does not have a (single) domain space,
7278 * "space" is necessarily a parameter space.
7280 * Since the order and the names of the parameters determine
7281 * the hash value, we need to create a new hash table.
7283 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7284 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7286 struct isl_union_pw_aff_reset_params_data data = { space };
7287 int match;
7289 match = isl_union_pw_aff_matching_params(upa, space);
7290 if (match < 0)
7291 upa = isl_union_pw_aff_free(upa);
7292 else if (match) {
7293 isl_space_free(space);
7294 return upa;
7297 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7298 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7299 data.res = isl_union_pw_aff_free(data.res);
7301 isl_union_pw_aff_free(upa);
7302 isl_space_free(space);
7303 return data.res;
7306 /* Replace the entry of isl_union_pw_aff to which "entry" points
7307 * by its floor.
7309 static int floor_entry(void **entry, void *user)
7311 isl_pw_aff **pa = (isl_pw_aff **) entry;
7313 *pa = isl_pw_aff_floor(*pa);
7314 if (!*pa)
7315 return -1;
7317 return 0;
7320 /* Given f, return floor(f).
7322 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7323 __isl_take isl_union_pw_aff *upa)
7325 isl_ctx *ctx;
7327 upa = isl_union_pw_aff_cow(upa);
7328 if (!upa)
7329 return NULL;
7331 ctx = isl_union_pw_aff_get_ctx(upa);
7332 if (isl_hash_table_foreach(ctx, &upa->table, &floor_entry, NULL) < 0)
7333 upa = isl_union_pw_aff_free(upa);
7335 return upa;
7338 /* Compute
7340 * upa mod m = upa - m * floor(upa/m)
7342 * with m an integer value.
7344 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7345 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7347 isl_union_pw_aff *res;
7349 if (!upa || !m)
7350 goto error;
7352 if (!isl_val_is_int(m))
7353 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7354 "expecting integer modulo", goto error);
7355 if (!isl_val_is_pos(m))
7356 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7357 "expecting positive modulo", goto error);
7359 res = isl_union_pw_aff_copy(upa);
7360 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7361 upa = isl_union_pw_aff_floor(upa);
7362 upa = isl_union_pw_aff_scale_val(upa, m);
7363 res = isl_union_pw_aff_sub(res, upa);
7365 return res;
7366 error:
7367 isl_val_free(m);
7368 isl_union_pw_aff_free(upa);
7369 return NULL;
7372 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7373 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7374 * needs to attain.
7375 * "res" collects the results.
7377 struct isl_union_pw_aff_aff_on_domain_data {
7378 isl_aff *aff;
7379 isl_union_pw_aff *res;
7382 /* Construct a piecewise affine expression that is equal to data->aff
7383 * on "domain" and add the result to data->res.
7385 static int pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7387 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7388 isl_pw_aff *pa;
7389 isl_aff *aff;
7390 int dim;
7392 aff = isl_aff_copy(data->aff);
7393 dim = isl_set_dim(domain, isl_dim_set);
7394 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7395 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7396 pa = isl_pw_aff_alloc(domain, aff);
7397 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7399 return data->res ? 0 : -1;
7402 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7403 * pos is the output position that needs to be extracted.
7404 * res collects the results.
7406 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7407 int pos;
7408 isl_union_pw_aff *res;
7411 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7412 * (assuming it has such a dimension) and add it to data->res.
7414 static int get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7416 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7417 int n_out;
7418 isl_pw_aff *pa;
7420 if (!pma)
7421 return -1;
7423 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7424 if (data->pos >= n_out) {
7425 isl_pw_multi_aff_free(pma);
7426 return 0;
7429 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7430 isl_pw_multi_aff_free(pma);
7432 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7434 return data->res ? 0 : -1;
7437 /* Extract an isl_union_pw_aff corresponding to
7438 * output dimension "pos" of "upma".
7440 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7441 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7443 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7444 isl_space *space;
7446 if (!upma)
7447 return NULL;
7449 if (pos < 0)
7450 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7451 "cannot extract at negative position", return NULL);
7453 space = isl_union_pw_multi_aff_get_space(upma);
7454 data.res = isl_union_pw_aff_empty(space);
7455 data.pos = pos;
7456 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7457 &get_union_pw_aff, &data) < 0)
7458 data.res = isl_union_pw_aff_free(data.res);
7460 return data.res;
7463 /* Return a union piecewise affine expression
7464 * that is equal to "aff" on "domain".
7466 * Construct an isl_pw_aff on each of the sets in "domain" and
7467 * collect the results.
7469 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7470 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7472 struct isl_union_pw_aff_aff_on_domain_data data;
7473 isl_space *space;
7475 if (!domain || !aff)
7476 goto error;
7477 if (!isl_local_space_is_params(aff->ls))
7478 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7479 "expecting parametric expression", goto error);
7481 space = isl_union_set_get_space(domain);
7482 data.res = isl_union_pw_aff_empty(space);
7483 data.aff = aff;
7484 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7485 data.res = isl_union_pw_aff_free(data.res);
7486 isl_union_set_free(domain);
7487 isl_aff_free(aff);
7488 return data.res;
7489 error:
7490 isl_union_set_free(domain);
7491 isl_aff_free(aff);
7492 return NULL;
7495 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7496 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7497 * "res" collects the results.
7499 struct isl_union_pw_aff_val_on_domain_data {
7500 isl_val *v;
7501 isl_union_pw_aff *res;
7504 /* Construct a piecewise affine expression that is equal to data->v
7505 * on "domain" and add the result to data->res.
7507 static int pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7509 struct isl_union_pw_aff_val_on_domain_data *data = user;
7510 isl_pw_aff *pa;
7511 isl_val *v;
7513 v = isl_val_copy(data->v);
7514 pa = isl_pw_aff_val_on_domain(domain, v);
7515 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7517 return data->res ? 0 : -1;
7520 /* Return a union piecewise affine expression
7521 * that is equal to "v" on "domain".
7523 * Construct an isl_pw_aff on each of the sets in "domain" and
7524 * collect the results.
7526 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7527 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7529 struct isl_union_pw_aff_val_on_domain_data data;
7530 isl_space *space;
7532 space = isl_union_set_get_space(domain);
7533 data.res = isl_union_pw_aff_empty(space);
7534 data.v = v;
7535 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7536 data.res = isl_union_pw_aff_free(data.res);
7537 isl_union_set_free(domain);
7538 isl_val_free(v);
7539 return data.res;
7542 /* Construct a piecewise multi affine expression
7543 * that is equal to "pa" and add it to upma.
7545 static int pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7547 isl_union_pw_multi_aff **upma = user;
7548 isl_pw_multi_aff *pma;
7550 pma = isl_pw_multi_aff_from_pw_aff(pa);
7551 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7553 return *upma ? 0 : -1;
7556 /* Construct and return a union piecewise multi affine expression
7557 * that is equal to the given union piecewise affine expression.
7559 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7560 __isl_take isl_union_pw_aff *upa)
7562 isl_space *space;
7563 isl_union_pw_multi_aff *upma;
7565 if (!upa)
7566 return NULL;
7568 space = isl_union_pw_aff_get_space(upa);
7569 upma = isl_union_pw_multi_aff_empty(space);
7571 if (isl_union_pw_aff_foreach_pw_aff(upa,
7572 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7573 upma = isl_union_pw_multi_aff_free(upma);
7575 isl_union_pw_aff_free(upa);
7576 return upma;
7579 /* Compute the set of elements in the domain of "pa" where it is zero and
7580 * add this set to "uset".
7582 static int zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7584 isl_union_set **uset = (isl_union_set **)user;
7586 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7588 return *uset ? 0 : -1;
7591 /* Return a union set containing those elements in the domain
7592 * of "upa" where it is zero.
7594 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7595 __isl_take isl_union_pw_aff *upa)
7597 isl_union_set *zero;
7599 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7600 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7601 zero = isl_union_set_free(zero);
7603 isl_union_pw_aff_free(upa);
7604 return zero;
7607 /* Convert "pa" to an isl_map and add it to *umap.
7609 static int map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7611 isl_union_map **umap = user;
7612 isl_map *map;
7614 map = isl_map_from_pw_aff(pa);
7615 *umap = isl_union_map_add_map(*umap, map);
7617 return *umap ? 0 : -1;
7620 /* Construct a union map mapping the domain of the union
7621 * piecewise affine expression to its range, with the single output dimension
7622 * equated to the corresponding affine expressions on their cells.
7624 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7625 __isl_take isl_union_pw_aff *upa)
7627 isl_space *space;
7628 isl_union_map *umap;
7630 if (!upa)
7631 return NULL;
7633 space = isl_union_pw_aff_get_space(upa);
7634 umap = isl_union_map_empty(space);
7636 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7637 &umap) < 0)
7638 umap = isl_union_map_free(umap);
7640 isl_union_pw_aff_free(upa);
7641 return umap;
7644 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7645 * upma is the function that is plugged in.
7646 * pa is the current part of the function in which upma is plugged in.
7647 * res collects the results.
7649 struct isl_union_pw_aff_pullback_upma_data {
7650 isl_union_pw_multi_aff *upma;
7651 isl_pw_aff *pa;
7652 isl_union_pw_aff *res;
7655 /* Check if "pma" can be plugged into data->pa.
7656 * If so, perform the pullback and add the result to data->res.
7658 static int pa_pb_pma(void **entry, void *user)
7660 struct isl_union_pw_aff_pullback_upma_data *data = user;
7661 isl_pw_multi_aff *pma = *entry;
7662 isl_pw_aff *pa;
7664 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7665 pma->dim, isl_dim_out))
7666 return 0;
7668 pma = isl_pw_multi_aff_copy(pma);
7669 pa = isl_pw_aff_copy(data->pa);
7670 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7672 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7674 return data->res ? 0 : -1;
7677 /* Check if any of the elements of data->upma can be plugged into pa,
7678 * add if so add the result to data->res.
7680 static int upa_pb_upma(void **entry, void *user)
7682 struct isl_union_pw_aff_pullback_upma_data *data = user;
7683 isl_ctx *ctx;
7684 isl_pw_aff *pa = *entry;
7686 data->pa = pa;
7687 ctx = isl_union_pw_multi_aff_get_ctx(data->upma);
7688 if (isl_hash_table_foreach(ctx, &data->upma->table,
7689 &pa_pb_pma, data) < 0)
7690 return -1;
7692 return 0;
7695 /* Compute the pullback of "upa" by the function represented by "upma".
7696 * In other words, plug in "upma" in "upa". The result contains
7697 * expressions defined over the domain space of "upma".
7699 * Run over all pairs of elements in "upa" and "upma", perform
7700 * the pullback when appropriate and collect the results.
7701 * If the hash value were based on the domain space rather than
7702 * the function space, then we could run through all elements
7703 * of "upma" and directly pick out the corresponding element of "upa".
7705 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7706 __isl_take isl_union_pw_aff *upa,
7707 __isl_take isl_union_pw_multi_aff *upma)
7709 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7710 isl_ctx *ctx;
7711 isl_space *space;
7713 space = isl_union_pw_multi_aff_get_space(upma);
7714 upa = isl_union_pw_aff_align_params(upa, space);
7715 space = isl_union_pw_aff_get_space(upa);
7716 upma = isl_union_pw_multi_aff_align_params(upma, space);
7718 if (!upa || !upma)
7719 goto error;
7721 ctx = isl_union_pw_aff_get_ctx(upa);
7722 data.upma = upma;
7723 space = isl_union_pw_aff_get_space(upa);
7724 data.res = isl_union_pw_aff_alloc(space, upa->table.n);
7725 if (isl_hash_table_foreach(ctx, &upa->table, &upa_pb_upma, &data) < 0)
7726 data.res = isl_union_pw_aff_free(data.res);
7728 isl_union_pw_aff_free(upa);
7729 isl_union_pw_multi_aff_free(upma);
7730 return data.res;
7731 error:
7732 isl_union_pw_aff_free(upa);
7733 isl_union_pw_multi_aff_free(upma);
7734 return NULL;
7737 #undef BASE
7738 #define BASE union_pw_aff
7739 #undef DOMBASE
7740 #define DOMBASE union_set
7742 #define NO_MOVE_DIMS
7743 #define NO_DIMS
7744 #define NO_DOMAIN
7745 #define NO_PRODUCT
7746 #define NO_SPLICE
7747 #define NO_ZERO
7748 #define NO_IDENTITY
7749 #define NO_GIST
7751 #include <isl_multi_templ.c>
7752 #include <isl_multi_apply_set.c>
7753 #include <isl_multi_apply_union_set.c>
7754 #include <isl_multi_floor.c>
7755 #include <isl_multi_gist.c>
7756 #include <isl_multi_intersect.c>
7758 /* Construct a multiple union piecewise affine expression
7759 * in the given space with value zero in each of the output dimensions.
7761 * Since there is no canonical zero value for
7762 * a union piecewise affine expression, we can only construct
7763 * zero-dimensional "zero" value.
7765 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7766 __isl_take isl_space *space)
7768 if (!space)
7769 return NULL;
7771 if (!isl_space_is_set(space))
7772 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7773 "expecting set space", goto error);
7774 if (isl_space_dim(space , isl_dim_out) != 0)
7775 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7776 "expecting 0D space", goto error);
7778 return isl_multi_union_pw_aff_alloc(space);
7779 error:
7780 isl_space_free(space);
7781 return NULL;
7784 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7785 * with the actual sum on the shared domain and
7786 * the defined expression on the symmetric difference of the domains.
7788 * We simply iterate over the elements in both arguments and
7789 * call isl_union_pw_aff_union_add on each of them.
7791 static __isl_give isl_multi_union_pw_aff *
7792 isl_multi_union_pw_aff_union_add_aligned(
7793 __isl_take isl_multi_union_pw_aff *mupa1,
7794 __isl_take isl_multi_union_pw_aff *mupa2)
7796 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7797 &isl_union_pw_aff_union_add);
7800 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7801 * with the actual sum on the shared domain and
7802 * the defined expression on the symmetric difference of the domains.
7804 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7805 __isl_take isl_multi_union_pw_aff *mupa1,
7806 __isl_take isl_multi_union_pw_aff *mupa2)
7808 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7809 &isl_multi_union_pw_aff_union_add_aligned);
7812 /* Construct and return a multi union piecewise affine expression
7813 * that is equal to the given multi affine expression.
7815 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7816 __isl_take isl_multi_aff *ma)
7818 isl_multi_pw_aff *mpa;
7820 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7821 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7824 /* Construct and return a multi union piecewise affine expression
7825 * that is equal to the given multi piecewise affine expression.
7827 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7828 __isl_take isl_multi_pw_aff *mpa)
7830 int i, n;
7831 isl_space *space;
7832 isl_multi_union_pw_aff *mupa;
7834 if (!mpa)
7835 return NULL;
7837 space = isl_multi_pw_aff_get_space(mpa);
7838 space = isl_space_range(space);
7839 mupa = isl_multi_union_pw_aff_alloc(space);
7841 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7842 for (i = 0; i < n; ++i) {
7843 isl_pw_aff *pa;
7844 isl_union_pw_aff *upa;
7846 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7847 upa = isl_union_pw_aff_from_pw_aff(pa);
7848 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7851 isl_multi_pw_aff_free(mpa);
7853 return mupa;
7856 /* Extract the range space of "pma" and assign it to *space.
7857 * If *space has already been set (through a previous call to this function),
7858 * then check that the range space is the same.
7860 static int extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7862 isl_space **space = user;
7863 isl_space *pma_space;
7864 int equal;
7866 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7867 isl_pw_multi_aff_free(pma);
7869 if (!pma_space)
7870 return -1;
7871 if (!*space) {
7872 *space = pma_space;
7873 return 0;
7876 equal = isl_space_is_equal(pma_space, *space);
7877 isl_space_free(pma_space);
7879 if (equal < 0)
7880 return -1;
7881 if (!equal)
7882 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7883 "range spaces not the same", return -1);
7884 return 0;
7887 /* Construct and return a multi union piecewise affine expression
7888 * that is equal to the given union piecewise multi affine expression.
7890 * In order to be able to perform the conversion, the input
7891 * needs to be non-empty and may only involve a single range space.
7893 __isl_give isl_multi_union_pw_aff *
7894 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7895 __isl_take isl_union_pw_multi_aff *upma)
7897 isl_space *space = NULL;
7898 isl_multi_union_pw_aff *mupa;
7899 int i, n;
7901 if (!upma)
7902 return NULL;
7903 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7904 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7905 "cannot extract range space from empty input",
7906 goto error);
7907 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7908 &space) < 0)
7909 goto error;
7911 if (!space)
7912 goto error;
7914 n = isl_space_dim(space, isl_dim_set);
7915 mupa = isl_multi_union_pw_aff_alloc(space);
7917 for (i = 0; i < n; ++i) {
7918 isl_union_pw_aff *upa;
7920 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7921 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7924 isl_union_pw_multi_aff_free(upma);
7925 return mupa;
7926 error:
7927 isl_space_free(space);
7928 isl_union_pw_multi_aff_free(upma);
7929 return NULL;
7932 /* Try and create an isl_multi_union_pw_aff that is equivalent
7933 * to the given isl_union_map.
7934 * The isl_union_map is required to be single-valued in each space.
7935 * Moreover, it cannot be empty and all range spaces need to be the same.
7936 * Otherwise, an error is produced.
7938 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7939 __isl_take isl_union_map *umap)
7941 isl_union_pw_multi_aff *upma;
7943 upma = isl_union_pw_multi_aff_from_union_map(umap);
7944 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7947 /* Return a multiple union piecewise affine expression
7948 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7949 * have been aligned.
7951 static __isl_give isl_multi_union_pw_aff *
7952 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7953 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7955 int i, n;
7956 isl_space *space;
7957 isl_multi_union_pw_aff *mupa;
7959 if (!domain || !mv)
7960 goto error;
7962 n = isl_multi_val_dim(mv, isl_dim_set);
7963 space = isl_multi_val_get_space(mv);
7964 mupa = isl_multi_union_pw_aff_alloc(space);
7965 for (i = 0; i < n; ++i) {
7966 isl_val *v;
7967 isl_union_pw_aff *upa;
7969 v = isl_multi_val_get_val(mv, i);
7970 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
7972 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7975 isl_union_set_free(domain);
7976 isl_multi_val_free(mv);
7977 return mupa;
7978 error:
7979 isl_union_set_free(domain);
7980 isl_multi_val_free(mv);
7981 return NULL;
7984 /* Return a multiple union piecewise affine expression
7985 * that is equal to "mv" on "domain".
7987 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
7988 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7990 if (!domain || !mv)
7991 goto error;
7992 if (isl_space_match(domain->dim, isl_dim_param,
7993 mv->space, isl_dim_param))
7994 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7995 domain, mv);
7996 domain = isl_union_set_align_params(domain,
7997 isl_multi_val_get_space(mv));
7998 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
7999 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8000 error:
8001 isl_union_set_free(domain);
8002 isl_multi_val_free(mv);
8003 return NULL;
8006 /* Return a multiple union piecewise affine expression
8007 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8008 * have been aligned.
8010 static __isl_give isl_multi_union_pw_aff *
8011 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8012 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8014 int i, n;
8015 isl_space *space;
8016 isl_multi_union_pw_aff *mupa;
8018 if (!domain || !ma)
8019 goto error;
8021 n = isl_multi_aff_dim(ma, isl_dim_set);
8022 space = isl_multi_aff_get_space(ma);
8023 mupa = isl_multi_union_pw_aff_alloc(space);
8024 for (i = 0; i < n; ++i) {
8025 isl_aff *aff;
8026 isl_union_pw_aff *upa;
8028 aff = isl_multi_aff_get_aff(ma, i);
8029 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8030 aff);
8031 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8034 isl_union_set_free(domain);
8035 isl_multi_aff_free(ma);
8036 return mupa;
8037 error:
8038 isl_union_set_free(domain);
8039 isl_multi_aff_free(ma);
8040 return NULL;
8043 /* Return a multiple union piecewise affine expression
8044 * that is equal to "ma" on "domain".
8046 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8047 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8049 if (!domain || !ma)
8050 goto error;
8051 if (isl_space_match(domain->dim, isl_dim_param,
8052 ma->space, isl_dim_param))
8053 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8054 domain, ma);
8055 domain = isl_union_set_align_params(domain,
8056 isl_multi_aff_get_space(ma));
8057 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8058 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8059 error:
8060 isl_union_set_free(domain);
8061 isl_multi_aff_free(ma);
8062 return NULL;
8065 /* Return a union set containing those elements in the domains
8066 * of the elements of "mupa" where they are all zero.
8068 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8069 __isl_take isl_multi_union_pw_aff *mupa)
8071 int i, n;
8072 isl_union_pw_aff *upa;
8073 isl_union_set *zero;
8075 if (!mupa)
8076 return NULL;
8078 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8079 if (n == 0)
8080 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8081 "cannot determine zero set "
8082 "of zero-dimensional function", goto error);
8084 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8085 zero = isl_union_pw_aff_zero_union_set(upa);
8087 for (i = 1; i < n; ++i) {
8088 isl_union_set *zero_i;
8090 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8091 zero_i = isl_union_pw_aff_zero_union_set(upa);
8093 zero = isl_union_set_intersect(zero, zero_i);
8096 isl_multi_union_pw_aff_free(mupa);
8097 return zero;
8098 error:
8099 isl_multi_union_pw_aff_free(mupa);
8100 return NULL;
8103 /* Construct a union map mapping the shared domain
8104 * of the union piecewise affine expressions to the range of "mupa"
8105 * with each dimension in the range equated to the
8106 * corresponding union piecewise affine expression.
8108 * The input cannot be zero-dimensional as there is
8109 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8111 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8112 __isl_take isl_multi_union_pw_aff *mupa)
8114 int i, n;
8115 isl_space *space;
8116 isl_union_map *umap;
8117 isl_union_pw_aff *upa;
8119 if (!mupa)
8120 return NULL;
8122 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8123 if (n == 0)
8124 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8125 "cannot determine domain of zero-dimensional "
8126 "isl_multi_union_pw_aff", goto error);
8128 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8129 umap = isl_union_map_from_union_pw_aff(upa);
8131 for (i = 1; i < n; ++i) {
8132 isl_union_map *umap_i;
8134 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8135 umap_i = isl_union_map_from_union_pw_aff(upa);
8136 umap = isl_union_map_flat_range_product(umap, umap_i);
8139 space = isl_multi_union_pw_aff_get_space(mupa);
8140 umap = isl_union_map_reset_range_space(umap, space);
8142 isl_multi_union_pw_aff_free(mupa);
8143 return umap;
8144 error:
8145 isl_multi_union_pw_aff_free(mupa);
8146 return NULL;
8149 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8150 * "range" is the space from which to set the range space.
8151 * "res" collects the results.
8153 struct isl_union_pw_multi_aff_reset_range_space_data {
8154 isl_space *range;
8155 isl_union_pw_multi_aff *res;
8158 /* Replace the range space of "pma" by the range space of data->range and
8159 * add the result to data->res.
8161 static int reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8163 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8164 isl_space *space;
8166 space = isl_pw_multi_aff_get_space(pma);
8167 space = isl_space_domain(space);
8168 space = isl_space_extend_domain_with_range(space,
8169 isl_space_copy(data->range));
8170 pma = isl_pw_multi_aff_reset_space(pma, space);
8171 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8173 return data->res ? 0 : -1;
8176 /* Replace the range space of all the piecewise affine expressions in "upma" by
8177 * the range space of "space".
8179 * This assumes that all these expressions have the same output dimension.
8181 * Since the spaces of the expressions change, so do their hash values.
8182 * We therefore need to create a new isl_union_pw_multi_aff.
8183 * Note that the hash value is currently computed based on the entire
8184 * space even though there can only be a single expression with a given
8185 * domain space.
8187 static __isl_give isl_union_pw_multi_aff *
8188 isl_union_pw_multi_aff_reset_range_space(
8189 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8191 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8192 isl_space *space_upma;
8194 space_upma = isl_union_pw_multi_aff_get_space(upma);
8195 data.res = isl_union_pw_multi_aff_empty(space_upma);
8196 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8197 &reset_range_space, &data) < 0)
8198 data.res = isl_union_pw_multi_aff_free(data.res);
8200 isl_space_free(space);
8201 isl_union_pw_multi_aff_free(upma);
8202 return data.res;
8205 /* Construct and return a union piecewise multi affine expression
8206 * that is equal to the given multi union piecewise affine expression.
8208 * In order to be able to perform the conversion, the input
8209 * needs to have a least one output dimension.
8211 __isl_give isl_union_pw_multi_aff *
8212 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8213 __isl_take isl_multi_union_pw_aff *mupa)
8215 int i, n;
8216 isl_space *space;
8217 isl_union_pw_multi_aff *upma;
8218 isl_union_pw_aff *upa;
8220 if (!mupa)
8221 return NULL;
8223 space = isl_multi_union_pw_aff_get_space(mupa);
8225 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8226 if (n == 0)
8227 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8228 "cannot determine domain of zero-dimensional "
8229 "isl_multi_union_pw_aff", goto error);
8231 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8232 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8234 for (i = 1; i < n; ++i) {
8235 isl_union_pw_multi_aff *upma_i;
8237 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8238 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8239 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8242 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8244 isl_multi_union_pw_aff_free(mupa);
8245 return upma;
8246 error:
8247 isl_multi_union_pw_aff_free(mupa);
8248 return NULL;
8251 /* Intersect the range of "mupa" with "range".
8252 * That is, keep only those domain elements that have a function value
8253 * in "range".
8255 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8256 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8258 isl_union_pw_multi_aff *upma;
8259 isl_union_set *domain;
8260 isl_space *space;
8261 int n;
8262 int match;
8264 if (!mupa || !range)
8265 goto error;
8267 space = isl_set_get_space(range);
8268 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8269 space, isl_dim_set);
8270 isl_space_free(space);
8271 if (match < 0)
8272 goto error;
8273 if (!match)
8274 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8275 "space don't match", goto error);
8276 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8277 if (n == 0)
8278 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8279 "cannot intersect range of zero-dimensional "
8280 "isl_multi_union_pw_aff", goto error);
8282 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8283 isl_multi_union_pw_aff_copy(mupa));
8284 domain = isl_union_set_from_set(range);
8285 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8286 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8288 return mupa;
8289 error:
8290 isl_multi_union_pw_aff_free(mupa);
8291 isl_set_free(range);
8292 return NULL;
8295 /* Return the shared domain of the elements of "mupa".
8297 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8298 __isl_take isl_multi_union_pw_aff *mupa)
8300 int i, n;
8301 isl_union_pw_aff *upa;
8302 isl_union_set *dom;
8304 if (!mupa)
8305 return NULL;
8307 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8308 if (n == 0)
8309 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8310 "cannot determine domain", goto error);
8312 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8313 dom = isl_union_pw_aff_domain(upa);
8314 for (i = 1; i < n; ++i) {
8315 isl_union_set *dom_i;
8317 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8318 dom_i = isl_union_pw_aff_domain(upa);
8319 dom = isl_union_set_intersect(dom, dom_i);
8322 isl_multi_union_pw_aff_free(mupa);
8323 return dom;
8324 error:
8325 isl_multi_union_pw_aff_free(mupa);
8326 return NULL;
8329 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8330 * In particular, the spaces have been aligned.
8331 * The result is defined over the shared domain of the elements of "mupa"
8333 * We first extract the parametric constant part of "aff" and
8334 * define that over the shared domain.
8335 * Then we iterate over all input dimensions of "aff" and add the corresponding
8336 * multiples of the elements of "mupa".
8337 * Finally, we consider the integer divisions, calling the function
8338 * recursively to obtain an isl_union_pw_aff corresponding to the
8339 * integer division argument.
8341 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8342 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8344 int i, n_in, n_div;
8345 isl_union_pw_aff *upa;
8346 isl_union_set *uset;
8347 isl_val *v;
8348 isl_aff *cst;
8350 n_in = isl_aff_dim(aff, isl_dim_in);
8351 n_div = isl_aff_dim(aff, isl_dim_div);
8353 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8354 cst = isl_aff_copy(aff);
8355 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8356 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8357 cst = isl_aff_project_domain_on_params(cst);
8358 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8360 for (i = 0; i < n_in; ++i) {
8361 isl_union_pw_aff *upa_i;
8363 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8364 continue;
8365 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8366 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8367 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8368 upa = isl_union_pw_aff_add(upa, upa_i);
8371 for (i = 0; i < n_div; ++i) {
8372 isl_aff *div;
8373 isl_union_pw_aff *upa_i;
8375 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8376 continue;
8377 div = isl_aff_get_div(aff, i);
8378 upa_i = multi_union_pw_aff_apply_aff(
8379 isl_multi_union_pw_aff_copy(mupa), div);
8380 upa_i = isl_union_pw_aff_floor(upa_i);
8381 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8382 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8383 upa = isl_union_pw_aff_add(upa, upa_i);
8386 isl_multi_union_pw_aff_free(mupa);
8387 isl_aff_free(aff);
8389 return upa;
8392 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8393 * with the domain of "aff".
8394 * Furthermore, the dimension of this space needs to be greater than zero.
8395 * The result is defined over the shared domain of the elements of "mupa"
8397 * We perform these checks and then hand over control to
8398 * multi_union_pw_aff_apply_aff.
8400 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8401 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8403 isl_space *space1, *space2;
8404 int equal;
8406 mupa = isl_multi_union_pw_aff_align_params(mupa,
8407 isl_aff_get_space(aff));
8408 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8409 if (!mupa || !aff)
8410 goto error;
8412 space1 = isl_multi_union_pw_aff_get_space(mupa);
8413 space2 = isl_aff_get_domain_space(aff);
8414 equal = isl_space_is_equal(space1, space2);
8415 isl_space_free(space1);
8416 isl_space_free(space2);
8417 if (equal < 0)
8418 goto error;
8419 if (!equal)
8420 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8421 "spaces don't match", goto error);
8422 if (isl_aff_dim(aff, isl_dim_in) == 0)
8423 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8424 "cannot determine domains", goto error);
8426 return multi_union_pw_aff_apply_aff(mupa, aff);
8427 error:
8428 isl_multi_union_pw_aff_free(mupa);
8429 isl_aff_free(aff);
8430 return NULL;
8433 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8434 * with the domain of "ma".
8435 * Furthermore, the dimension of this space needs to be greater than zero,
8436 * unless the dimension of the target space of "ma" is also zero.
8437 * The result is defined over the shared domain of the elements of "mupa"
8439 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8440 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8442 isl_space *space1, *space2;
8443 isl_multi_union_pw_aff *res;
8444 int equal;
8445 int i, n_out;
8447 mupa = isl_multi_union_pw_aff_align_params(mupa,
8448 isl_multi_aff_get_space(ma));
8449 ma = isl_multi_aff_align_params(ma,
8450 isl_multi_union_pw_aff_get_space(mupa));
8451 if (!mupa || !ma)
8452 goto error;
8454 space1 = isl_multi_union_pw_aff_get_space(mupa);
8455 space2 = isl_multi_aff_get_domain_space(ma);
8456 equal = isl_space_is_equal(space1, space2);
8457 isl_space_free(space1);
8458 isl_space_free(space2);
8459 if (equal < 0)
8460 goto error;
8461 if (!equal)
8462 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8463 "spaces don't match", goto error);
8464 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8465 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8466 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8467 "cannot determine domains", goto error);
8469 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8470 res = isl_multi_union_pw_aff_alloc(space1);
8472 for (i = 0; i < n_out; ++i) {
8473 isl_aff *aff;
8474 isl_union_pw_aff *upa;
8476 aff = isl_multi_aff_get_aff(ma, i);
8477 upa = multi_union_pw_aff_apply_aff(
8478 isl_multi_union_pw_aff_copy(mupa), aff);
8479 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8482 isl_multi_aff_free(ma);
8483 isl_multi_union_pw_aff_free(mupa);
8484 return res;
8485 error:
8486 isl_multi_union_pw_aff_free(mupa);
8487 isl_multi_aff_free(ma);
8488 return NULL;
8491 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8492 * with the domain of "pa".
8493 * Furthermore, the dimension of this space needs to be greater than zero.
8494 * The result is defined over the shared domain of the elements of "mupa"
8496 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8497 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8499 int i;
8500 int equal;
8501 isl_space *space, *space2;
8502 isl_union_pw_aff *upa;
8504 mupa = isl_multi_union_pw_aff_align_params(mupa,
8505 isl_pw_aff_get_space(pa));
8506 pa = isl_pw_aff_align_params(pa,
8507 isl_multi_union_pw_aff_get_space(mupa));
8508 if (!mupa || !pa)
8509 goto error;
8511 space = isl_multi_union_pw_aff_get_space(mupa);
8512 space2 = isl_pw_aff_get_domain_space(pa);
8513 equal = isl_space_is_equal(space, space2);
8514 isl_space_free(space);
8515 isl_space_free(space2);
8516 if (equal < 0)
8517 goto error;
8518 if (!equal)
8519 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8520 "spaces don't match", goto error);
8521 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8522 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8523 "cannot determine domains", goto error);
8525 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8526 upa = isl_union_pw_aff_empty(space);
8528 for (i = 0; i < pa->n; ++i) {
8529 isl_aff *aff;
8530 isl_set *domain;
8531 isl_multi_union_pw_aff *mupa_i;
8532 isl_union_pw_aff *upa_i;
8534 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8535 domain = isl_set_copy(pa->p[i].set);
8536 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8537 aff = isl_aff_copy(pa->p[i].aff);
8538 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8539 upa = isl_union_pw_aff_union_add(upa, upa_i);
8542 isl_multi_union_pw_aff_free(mupa);
8543 isl_pw_aff_free(pa);
8544 return upa;
8545 error:
8546 isl_multi_union_pw_aff_free(mupa);
8547 isl_pw_aff_free(pa);
8548 return NULL;
8551 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8552 * with the domain of "pma".
8553 * Furthermore, the dimension of this space needs to be greater than zero,
8554 * unless the dimension of the target space of "pma" is also zero.
8555 * The result is defined over the shared domain of the elements of "mupa"
8557 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8558 __isl_take isl_multi_union_pw_aff *mupa,
8559 __isl_take isl_pw_multi_aff *pma)
8561 isl_space *space1, *space2;
8562 isl_multi_union_pw_aff *res;
8563 int equal;
8564 int i, n_out;
8566 mupa = isl_multi_union_pw_aff_align_params(mupa,
8567 isl_pw_multi_aff_get_space(pma));
8568 pma = isl_pw_multi_aff_align_params(pma,
8569 isl_multi_union_pw_aff_get_space(mupa));
8570 if (!mupa || !pma)
8571 goto error;
8573 space1 = isl_multi_union_pw_aff_get_space(mupa);
8574 space2 = isl_pw_multi_aff_get_domain_space(pma);
8575 equal = isl_space_is_equal(space1, space2);
8576 isl_space_free(space1);
8577 isl_space_free(space2);
8578 if (equal < 0)
8579 goto error;
8580 if (!equal)
8581 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8582 "spaces don't match", goto error);
8583 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8584 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8585 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8586 "cannot determine domains", goto error);
8588 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8589 res = isl_multi_union_pw_aff_alloc(space1);
8591 for (i = 0; i < n_out; ++i) {
8592 isl_pw_aff *pa;
8593 isl_union_pw_aff *upa;
8595 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8596 upa = isl_multi_union_pw_aff_apply_pw_aff(
8597 isl_multi_union_pw_aff_copy(mupa), pa);
8598 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8601 isl_pw_multi_aff_free(pma);
8602 isl_multi_union_pw_aff_free(mupa);
8603 return res;
8604 error:
8605 isl_multi_union_pw_aff_free(mupa);
8606 isl_pw_multi_aff_free(pma);
8607 return NULL;
8610 /* Compute the pullback of "mupa" by the function represented by "upma".
8611 * In other words, plug in "upma" in "mupa". The result contains
8612 * expressions defined over the domain space of "upma".
8614 * Run over all elements of "mupa" and plug in "upma" in each of them.
8616 __isl_give isl_multi_union_pw_aff *
8617 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8618 __isl_take isl_multi_union_pw_aff *mupa,
8619 __isl_take isl_union_pw_multi_aff *upma)
8621 int i, n;
8623 mupa = isl_multi_union_pw_aff_align_params(mupa,
8624 isl_union_pw_multi_aff_get_space(upma));
8625 upma = isl_union_pw_multi_aff_align_params(upma,
8626 isl_multi_union_pw_aff_get_space(mupa));
8627 if (!mupa || !upma)
8628 goto error;
8630 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8631 for (i = 0; i < n; ++i) {
8632 isl_union_pw_aff *upa;
8634 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8635 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8636 isl_union_pw_multi_aff_copy(upma));
8637 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8640 isl_union_pw_multi_aff_free(upma);
8641 return mupa;
8642 error:
8643 isl_multi_union_pw_aff_free(mupa);
8644 isl_union_pw_multi_aff_free(upma);
8645 return NULL;
8648 /* Extract the sequence of elements in "mupa" with domain space "space"
8649 * (ignoring parameters).
8651 * For the elements of "mupa" that are not defined on the specified space,
8652 * the corresponding element in the result is empty.
8654 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8655 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8657 int i, n;
8658 isl_space *space_mpa = NULL;
8659 isl_multi_pw_aff *mpa;
8661 if (!mupa || !space)
8662 goto error;
8664 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8665 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8666 space = isl_space_drop_dims(space, isl_dim_param,
8667 0, isl_space_dim(space, isl_dim_param));
8668 space = isl_space_align_params(space,
8669 isl_space_copy(space_mpa));
8670 if (!space)
8671 goto error;
8673 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8674 space_mpa);
8675 mpa = isl_multi_pw_aff_alloc(space_mpa);
8677 space = isl_space_from_domain(space);
8678 space = isl_space_add_dims(space, isl_dim_out, 1);
8679 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8680 for (i = 0; i < n; ++i) {
8681 isl_union_pw_aff *upa;
8682 isl_pw_aff *pa;
8684 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8685 pa = isl_union_pw_aff_extract_pw_aff(upa,
8686 isl_space_copy(space));
8687 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8688 isl_union_pw_aff_free(upa);
8691 isl_space_free(space);
8692 return mpa;
8693 error:
8694 isl_space_free(space_mpa);
8695 isl_space_free(space);
8696 return NULL;