isl_sample.c: sample_bounded: use isl_basic_set_get_ctx
[isl.git] / isl_aff.c
blob0dff654edd69a4bb99618bef639430384b733768
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;
1462 unsigned off;
1464 if (!aff)
1465 return NULL;
1467 off = isl_local_space_offset(aff->ls, isl_dim_div);
1468 n = isl_aff_dim(aff, isl_dim_div);
1469 for (i = 1; i < n; ++i) {
1470 for (j = i - 1; j >= 0; --j) {
1471 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1472 if (cmp < 0)
1473 break;
1474 if (cmp == 0)
1475 aff = merge_divs(aff, j, j + 1);
1476 else
1477 aff = swap_div(aff, j, j + 1);
1478 if (!aff)
1479 return NULL;
1483 return aff;
1486 /* Normalize the representation of "aff".
1488 * This function should only be called of "new" isl_affs, i.e.,
1489 * with only a single reference. We therefore do not need to
1490 * worry about affecting other instances.
1492 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1494 if (!aff)
1495 return NULL;
1496 aff->v = isl_vec_normalize(aff->v);
1497 if (!aff->v)
1498 return isl_aff_free(aff);
1499 aff = plug_in_integral_divs(aff);
1500 aff = plug_in_unit_divs(aff);
1501 aff = sort_divs(aff);
1502 aff = isl_aff_remove_unused_divs(aff);
1503 return aff;
1506 /* Given f, return floor(f).
1507 * If f is an integer expression, then just return f.
1508 * If f is a constant, then return the constant floor(f).
1509 * Otherwise, if f = g/m, write g = q m + r,
1510 * create a new div d = [r/m] and return the expression q + d.
1511 * The coefficients in r are taken to lie between -m/2 and m/2.
1513 * As a special case, floor(NaN) = NaN.
1515 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1517 int i;
1518 int size;
1519 isl_ctx *ctx;
1520 isl_vec *div;
1522 if (!aff)
1523 return NULL;
1525 if (isl_aff_is_nan(aff))
1526 return aff;
1527 if (isl_int_is_one(aff->v->el[0]))
1528 return aff;
1530 aff = isl_aff_cow(aff);
1531 if (!aff)
1532 return NULL;
1534 aff->v = isl_vec_cow(aff->v);
1535 if (!aff->v)
1536 return isl_aff_free(aff);
1538 if (isl_aff_is_cst(aff)) {
1539 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1540 isl_int_set_si(aff->v->el[0], 1);
1541 return aff;
1544 div = isl_vec_copy(aff->v);
1545 div = isl_vec_cow(div);
1546 if (!div)
1547 return isl_aff_free(aff);
1549 ctx = isl_aff_get_ctx(aff);
1550 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1551 for (i = 1; i < aff->v->size; ++i) {
1552 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1553 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1554 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1555 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1556 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1560 aff->ls = isl_local_space_add_div(aff->ls, div);
1561 if (!aff->ls)
1562 return isl_aff_free(aff);
1564 size = aff->v->size;
1565 aff->v = isl_vec_extend(aff->v, size + 1);
1566 if (!aff->v)
1567 return isl_aff_free(aff);
1568 isl_int_set_si(aff->v->el[0], 1);
1569 isl_int_set_si(aff->v->el[size], 1);
1571 aff = isl_aff_normalize(aff);
1573 return aff;
1576 /* Compute
1578 * aff mod m = aff - m * floor(aff/m)
1580 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1582 isl_aff *res;
1584 res = isl_aff_copy(aff);
1585 aff = isl_aff_scale_down(aff, m);
1586 aff = isl_aff_floor(aff);
1587 aff = isl_aff_scale(aff, m);
1588 res = isl_aff_sub(res, aff);
1590 return res;
1593 /* Compute
1595 * aff mod m = aff - m * floor(aff/m)
1597 * with m an integer value.
1599 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1600 __isl_take isl_val *m)
1602 isl_aff *res;
1604 if (!aff || !m)
1605 goto error;
1607 if (!isl_val_is_int(m))
1608 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1609 "expecting integer modulo", goto error);
1611 res = isl_aff_copy(aff);
1612 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1613 aff = isl_aff_floor(aff);
1614 aff = isl_aff_scale_val(aff, m);
1615 res = isl_aff_sub(res, aff);
1617 return res;
1618 error:
1619 isl_aff_free(aff);
1620 isl_val_free(m);
1621 return NULL;
1624 /* Compute
1626 * pwaff mod m = pwaff - m * floor(pwaff/m)
1628 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1630 isl_pw_aff *res;
1632 res = isl_pw_aff_copy(pwaff);
1633 pwaff = isl_pw_aff_scale_down(pwaff, m);
1634 pwaff = isl_pw_aff_floor(pwaff);
1635 pwaff = isl_pw_aff_scale(pwaff, m);
1636 res = isl_pw_aff_sub(res, pwaff);
1638 return res;
1641 /* Compute
1643 * pa mod m = pa - m * floor(pa/m)
1645 * with m an integer value.
1647 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1648 __isl_take isl_val *m)
1650 if (!pa || !m)
1651 goto error;
1652 if (!isl_val_is_int(m))
1653 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1654 "expecting integer modulo", goto error);
1655 pa = isl_pw_aff_mod(pa, m->n);
1656 isl_val_free(m);
1657 return pa;
1658 error:
1659 isl_pw_aff_free(pa);
1660 isl_val_free(m);
1661 return NULL;
1664 /* Given f, return ceil(f).
1665 * If f is an integer expression, then just return f.
1666 * Otherwise, let f be the expression
1668 * e/m
1670 * then return
1672 * floor((e + m - 1)/m)
1674 * As a special case, ceil(NaN) = NaN.
1676 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1678 if (!aff)
1679 return NULL;
1681 if (isl_aff_is_nan(aff))
1682 return aff;
1683 if (isl_int_is_one(aff->v->el[0]))
1684 return aff;
1686 aff = isl_aff_cow(aff);
1687 if (!aff)
1688 return NULL;
1689 aff->v = isl_vec_cow(aff->v);
1690 if (!aff->v)
1691 return isl_aff_free(aff);
1693 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1694 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1695 aff = isl_aff_floor(aff);
1697 return aff;
1700 /* Apply the expansion computed by isl_merge_divs.
1701 * The expansion itself is given by "exp" while the resulting
1702 * list of divs is given by "div".
1704 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1705 __isl_take isl_mat *div, int *exp)
1707 int i, j;
1708 int old_n_div;
1709 int new_n_div;
1710 int offset;
1712 aff = isl_aff_cow(aff);
1713 if (!aff || !div)
1714 goto error;
1716 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1717 new_n_div = isl_mat_rows(div);
1718 if (new_n_div < old_n_div)
1719 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1720 "not an expansion", goto error);
1722 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1723 if (!aff->v)
1724 goto error;
1726 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1727 j = old_n_div - 1;
1728 for (i = new_n_div - 1; i >= 0; --i) {
1729 if (j >= 0 && exp[j] == i) {
1730 if (i != j)
1731 isl_int_swap(aff->v->el[offset + i],
1732 aff->v->el[offset + j]);
1733 j--;
1734 } else
1735 isl_int_set_si(aff->v->el[offset + i], 0);
1738 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1739 if (!aff->ls)
1740 goto error;
1741 isl_mat_free(div);
1742 return aff;
1743 error:
1744 isl_aff_free(aff);
1745 isl_mat_free(div);
1746 return NULL;
1749 /* Add two affine expressions that live in the same local space.
1751 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1752 __isl_take isl_aff *aff2)
1754 isl_int gcd, f;
1756 aff1 = isl_aff_cow(aff1);
1757 if (!aff1 || !aff2)
1758 goto error;
1760 aff1->v = isl_vec_cow(aff1->v);
1761 if (!aff1->v)
1762 goto error;
1764 isl_int_init(gcd);
1765 isl_int_init(f);
1766 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1767 isl_int_divexact(f, aff2->v->el[0], gcd);
1768 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1769 isl_int_divexact(f, aff1->v->el[0], gcd);
1770 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1771 isl_int_divexact(f, aff2->v->el[0], gcd);
1772 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1773 isl_int_clear(f);
1774 isl_int_clear(gcd);
1776 isl_aff_free(aff2);
1777 return aff1;
1778 error:
1779 isl_aff_free(aff1);
1780 isl_aff_free(aff2);
1781 return NULL;
1784 /* Return the sum of "aff1" and "aff2".
1786 * If either of the two is NaN, then the result is NaN.
1788 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1789 __isl_take isl_aff *aff2)
1791 isl_ctx *ctx;
1792 int *exp1 = NULL;
1793 int *exp2 = NULL;
1794 isl_mat *div;
1795 int n_div1, n_div2;
1797 if (!aff1 || !aff2)
1798 goto error;
1800 ctx = isl_aff_get_ctx(aff1);
1801 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1802 isl_die(ctx, isl_error_invalid,
1803 "spaces don't match", goto error);
1805 if (isl_aff_is_nan(aff1)) {
1806 isl_aff_free(aff2);
1807 return aff1;
1809 if (isl_aff_is_nan(aff2)) {
1810 isl_aff_free(aff1);
1811 return aff2;
1814 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1815 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1816 if (n_div1 == 0 && n_div2 == 0)
1817 return add_expanded(aff1, aff2);
1819 exp1 = isl_alloc_array(ctx, int, n_div1);
1820 exp2 = isl_alloc_array(ctx, int, n_div2);
1821 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1822 goto error;
1824 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1825 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1826 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1827 free(exp1);
1828 free(exp2);
1830 return add_expanded(aff1, aff2);
1831 error:
1832 free(exp1);
1833 free(exp2);
1834 isl_aff_free(aff1);
1835 isl_aff_free(aff2);
1836 return NULL;
1839 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1840 __isl_take isl_aff *aff2)
1842 return isl_aff_add(aff1, isl_aff_neg(aff2));
1845 /* Return the result of scaling "aff" by a factor of "f".
1847 * As a special case, f * NaN = NaN.
1849 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1851 isl_int gcd;
1853 if (!aff)
1854 return NULL;
1855 if (isl_aff_is_nan(aff))
1856 return aff;
1858 if (isl_int_is_one(f))
1859 return aff;
1861 aff = isl_aff_cow(aff);
1862 if (!aff)
1863 return NULL;
1864 aff->v = isl_vec_cow(aff->v);
1865 if (!aff->v)
1866 return isl_aff_free(aff);
1868 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1869 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1870 return aff;
1873 isl_int_init(gcd);
1874 isl_int_gcd(gcd, aff->v->el[0], f);
1875 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1876 isl_int_divexact(gcd, f, gcd);
1877 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1878 isl_int_clear(gcd);
1880 return aff;
1883 /* Multiple "aff" by "v".
1885 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1886 __isl_take isl_val *v)
1888 if (!aff || !v)
1889 goto error;
1891 if (isl_val_is_one(v)) {
1892 isl_val_free(v);
1893 return aff;
1896 if (!isl_val_is_rat(v))
1897 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1898 "expecting rational factor", goto error);
1900 aff = isl_aff_scale(aff, v->n);
1901 aff = isl_aff_scale_down(aff, v->d);
1903 isl_val_free(v);
1904 return aff;
1905 error:
1906 isl_aff_free(aff);
1907 isl_val_free(v);
1908 return NULL;
1911 /* Return the result of scaling "aff" down by a factor of "f".
1913 * As a special case, NaN/f = NaN.
1915 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1917 isl_int gcd;
1919 if (!aff)
1920 return NULL;
1921 if (isl_aff_is_nan(aff))
1922 return aff;
1924 if (isl_int_is_one(f))
1925 return aff;
1927 aff = isl_aff_cow(aff);
1928 if (!aff)
1929 return NULL;
1931 if (isl_int_is_zero(f))
1932 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1933 "cannot scale down by zero", return isl_aff_free(aff));
1935 aff->v = isl_vec_cow(aff->v);
1936 if (!aff->v)
1937 return isl_aff_free(aff);
1939 isl_int_init(gcd);
1940 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1941 isl_int_gcd(gcd, gcd, f);
1942 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1943 isl_int_divexact(gcd, f, gcd);
1944 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1945 isl_int_clear(gcd);
1947 return aff;
1950 /* Divide "aff" by "v".
1952 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1953 __isl_take isl_val *v)
1955 if (!aff || !v)
1956 goto error;
1958 if (isl_val_is_one(v)) {
1959 isl_val_free(v);
1960 return aff;
1963 if (!isl_val_is_rat(v))
1964 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1965 "expecting rational factor", goto error);
1966 if (!isl_val_is_pos(v))
1967 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1968 "factor needs to be positive", goto error);
1970 aff = isl_aff_scale(aff, v->d);
1971 aff = isl_aff_scale_down(aff, v->n);
1973 isl_val_free(v);
1974 return aff;
1975 error:
1976 isl_aff_free(aff);
1977 isl_val_free(v);
1978 return NULL;
1981 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1983 isl_int v;
1985 if (f == 1)
1986 return aff;
1988 isl_int_init(v);
1989 isl_int_set_ui(v, f);
1990 aff = isl_aff_scale_down(aff, v);
1991 isl_int_clear(v);
1993 return aff;
1996 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1997 enum isl_dim_type type, unsigned pos, const char *s)
1999 aff = isl_aff_cow(aff);
2000 if (!aff)
2001 return NULL;
2002 if (type == isl_dim_out)
2003 isl_die(aff->v->ctx, isl_error_invalid,
2004 "cannot set name of output/set dimension",
2005 return isl_aff_free(aff));
2006 if (type == isl_dim_in)
2007 type = isl_dim_set;
2008 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2009 if (!aff->ls)
2010 return isl_aff_free(aff);
2012 return aff;
2015 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2016 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2018 aff = isl_aff_cow(aff);
2019 if (!aff)
2020 goto error;
2021 if (type == isl_dim_out)
2022 isl_die(aff->v->ctx, isl_error_invalid,
2023 "cannot set name of output/set dimension",
2024 goto error);
2025 if (type == isl_dim_in)
2026 type = isl_dim_set;
2027 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2028 if (!aff->ls)
2029 return isl_aff_free(aff);
2031 return aff;
2032 error:
2033 isl_id_free(id);
2034 isl_aff_free(aff);
2035 return NULL;
2038 /* Replace the identifier of the input tuple of "aff" by "id".
2039 * type is currently required to be equal to isl_dim_in
2041 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2042 enum isl_dim_type type, __isl_take isl_id *id)
2044 aff = isl_aff_cow(aff);
2045 if (!aff)
2046 goto error;
2047 if (type != isl_dim_out)
2048 isl_die(aff->v->ctx, isl_error_invalid,
2049 "cannot only set id of input tuple", goto error);
2050 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2051 if (!aff->ls)
2052 return isl_aff_free(aff);
2054 return aff;
2055 error:
2056 isl_id_free(id);
2057 isl_aff_free(aff);
2058 return NULL;
2061 /* Exploit the equalities in "eq" to simplify the affine expression
2062 * and the expressions of the integer divisions in the local space.
2063 * The integer divisions in this local space are assumed to appear
2064 * as regular dimensions in "eq".
2066 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2067 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2069 int i, j;
2070 unsigned total;
2071 unsigned n_div;
2073 if (!eq)
2074 goto error;
2075 if (eq->n_eq == 0) {
2076 isl_basic_set_free(eq);
2077 return aff;
2080 aff = isl_aff_cow(aff);
2081 if (!aff)
2082 goto error;
2084 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2085 isl_basic_set_copy(eq));
2086 aff->v = isl_vec_cow(aff->v);
2087 if (!aff->ls || !aff->v)
2088 goto error;
2090 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2091 n_div = eq->n_div;
2092 for (i = 0; i < eq->n_eq; ++i) {
2093 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2094 if (j < 0 || j == 0 || j >= total)
2095 continue;
2097 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2098 &aff->v->el[0]);
2101 isl_basic_set_free(eq);
2102 aff = isl_aff_normalize(aff);
2103 return aff;
2104 error:
2105 isl_basic_set_free(eq);
2106 isl_aff_free(aff);
2107 return NULL;
2110 /* Exploit the equalities in "eq" to simplify the affine expression
2111 * and the expressions of the integer divisions in the local space.
2113 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2114 __isl_take isl_basic_set *eq)
2116 int n_div;
2118 if (!aff || !eq)
2119 goto error;
2120 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2121 if (n_div > 0)
2122 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2123 return isl_aff_substitute_equalities_lifted(aff, eq);
2124 error:
2125 isl_basic_set_free(eq);
2126 isl_aff_free(aff);
2127 return NULL;
2130 /* Look for equalities among the variables shared by context and aff
2131 * and the integer divisions of aff, if any.
2132 * The equalities are then used to eliminate coefficients and/or integer
2133 * divisions from aff.
2135 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2136 __isl_take isl_set *context)
2138 isl_basic_set *hull;
2139 int n_div;
2141 if (!aff)
2142 goto error;
2143 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2144 if (n_div > 0) {
2145 isl_basic_set *bset;
2146 isl_local_space *ls;
2147 context = isl_set_add_dims(context, isl_dim_set, n_div);
2148 ls = isl_aff_get_domain_local_space(aff);
2149 bset = isl_basic_set_from_local_space(ls);
2150 bset = isl_basic_set_lift(bset);
2151 bset = isl_basic_set_flatten(bset);
2152 context = isl_set_intersect(context,
2153 isl_set_from_basic_set(bset));
2156 hull = isl_set_affine_hull(context);
2157 return isl_aff_substitute_equalities_lifted(aff, hull);
2158 error:
2159 isl_aff_free(aff);
2160 isl_set_free(context);
2161 return NULL;
2164 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2165 __isl_take isl_set *context)
2167 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2168 dom_context = isl_set_intersect_params(dom_context, context);
2169 return isl_aff_gist(aff, dom_context);
2172 /* Return a basic set containing those elements in the space
2173 * of aff where it is positive. "rational" should not be set.
2175 * If "aff" is NaN, then it is not positive.
2177 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2178 int rational)
2180 isl_constraint *ineq;
2181 isl_basic_set *bset;
2182 isl_val *c;
2184 if (!aff)
2185 return NULL;
2186 if (isl_aff_is_nan(aff)) {
2187 isl_space *space = isl_aff_get_domain_space(aff);
2188 isl_aff_free(aff);
2189 return isl_basic_set_empty(space);
2191 if (rational)
2192 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2193 "rational sets not supported", goto error);
2195 ineq = isl_inequality_from_aff(aff);
2196 c = isl_constraint_get_constant_val(ineq);
2197 c = isl_val_sub_ui(c, 1);
2198 ineq = isl_constraint_set_constant_val(ineq, c);
2200 bset = isl_basic_set_from_constraint(ineq);
2201 bset = isl_basic_set_simplify(bset);
2202 return bset;
2203 error:
2204 isl_aff_free(aff);
2205 return NULL;
2208 /* Return a basic set containing those elements in the space
2209 * of aff where it is non-negative.
2210 * If "rational" is set, then return a rational basic set.
2212 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2214 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2215 __isl_take isl_aff *aff, int rational)
2217 isl_constraint *ineq;
2218 isl_basic_set *bset;
2220 if (!aff)
2221 return NULL;
2222 if (isl_aff_is_nan(aff)) {
2223 isl_space *space = isl_aff_get_domain_space(aff);
2224 isl_aff_free(aff);
2225 return isl_basic_set_empty(space);
2228 ineq = isl_inequality_from_aff(aff);
2230 bset = isl_basic_set_from_constraint(ineq);
2231 if (rational)
2232 bset = isl_basic_set_set_rational(bset);
2233 bset = isl_basic_set_simplify(bset);
2234 return bset;
2237 /* Return a basic set containing those elements in the space
2238 * of aff where it is non-negative.
2240 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2242 return aff_nonneg_basic_set(aff, 0);
2245 /* Return a basic set containing those elements in the domain space
2246 * of aff where it is negative.
2248 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2250 aff = isl_aff_neg(aff);
2251 aff = isl_aff_add_constant_num_si(aff, -1);
2252 return isl_aff_nonneg_basic_set(aff);
2255 /* Return a basic set containing those elements in the space
2256 * of aff where it is zero.
2257 * If "rational" is set, then return a rational basic set.
2259 * If "aff" is NaN, then it is not zero.
2261 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2262 int rational)
2264 isl_constraint *ineq;
2265 isl_basic_set *bset;
2267 if (!aff)
2268 return NULL;
2269 if (isl_aff_is_nan(aff)) {
2270 isl_space *space = isl_aff_get_domain_space(aff);
2271 isl_aff_free(aff);
2272 return isl_basic_set_empty(space);
2275 ineq = isl_equality_from_aff(aff);
2277 bset = isl_basic_set_from_constraint(ineq);
2278 if (rational)
2279 bset = isl_basic_set_set_rational(bset);
2280 bset = isl_basic_set_simplify(bset);
2281 return bset;
2284 /* Return a basic set containing those elements in the space
2285 * of aff where it is zero.
2287 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2289 return aff_zero_basic_set(aff, 0);
2292 /* Return a basic set containing those elements in the shared space
2293 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2295 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2296 __isl_take isl_aff *aff2)
2298 aff1 = isl_aff_sub(aff1, aff2);
2300 return isl_aff_nonneg_basic_set(aff1);
2303 /* Return a basic set containing those elements in the shared space
2304 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2306 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2307 __isl_take isl_aff *aff2)
2309 return isl_aff_ge_basic_set(aff2, aff1);
2312 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2313 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2315 aff1 = isl_aff_add(aff1, aff2);
2316 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2317 return aff1;
2320 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2322 if (!aff)
2323 return -1;
2325 return 0;
2328 /* Check whether the given affine expression has non-zero coefficient
2329 * for any dimension in the given range or if any of these dimensions
2330 * appear with non-zero coefficients in any of the integer divisions
2331 * involved in the affine expression.
2333 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2334 enum isl_dim_type type, unsigned first, unsigned n)
2336 int i;
2337 isl_ctx *ctx;
2338 int *active = NULL;
2339 int involves = 0;
2341 if (!aff)
2342 return -1;
2343 if (n == 0)
2344 return 0;
2346 ctx = isl_aff_get_ctx(aff);
2347 if (first + n > isl_aff_dim(aff, type))
2348 isl_die(ctx, isl_error_invalid,
2349 "range out of bounds", return -1);
2351 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2352 if (!active)
2353 goto error;
2355 first += isl_local_space_offset(aff->ls, type) - 1;
2356 for (i = 0; i < n; ++i)
2357 if (active[first + i]) {
2358 involves = 1;
2359 break;
2362 free(active);
2364 return involves;
2365 error:
2366 free(active);
2367 return -1;
2370 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2371 enum isl_dim_type type, unsigned first, unsigned n)
2373 isl_ctx *ctx;
2375 if (!aff)
2376 return NULL;
2377 if (type == isl_dim_out)
2378 isl_die(aff->v->ctx, isl_error_invalid,
2379 "cannot drop output/set dimension",
2380 return isl_aff_free(aff));
2381 if (type == isl_dim_in)
2382 type = isl_dim_set;
2383 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2384 return aff;
2386 ctx = isl_aff_get_ctx(aff);
2387 if (first + n > isl_local_space_dim(aff->ls, type))
2388 isl_die(ctx, isl_error_invalid, "range out of bounds",
2389 return isl_aff_free(aff));
2391 aff = isl_aff_cow(aff);
2392 if (!aff)
2393 return NULL;
2395 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2396 if (!aff->ls)
2397 return isl_aff_free(aff);
2399 first += 1 + isl_local_space_offset(aff->ls, type);
2400 aff->v = isl_vec_drop_els(aff->v, first, n);
2401 if (!aff->v)
2402 return isl_aff_free(aff);
2404 return aff;
2407 /* Project the domain of the affine expression onto its parameter space.
2408 * The affine expression may not involve any of the domain dimensions.
2410 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2412 isl_space *space;
2413 unsigned n;
2414 int involves;
2416 n = isl_aff_dim(aff, isl_dim_in);
2417 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2418 if (involves < 0)
2419 return isl_aff_free(aff);
2420 if (involves)
2421 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2422 "affine expression involves some of the domain dimensions",
2423 return isl_aff_free(aff));
2424 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2425 space = isl_aff_get_domain_space(aff);
2426 space = isl_space_params(space);
2427 aff = isl_aff_reset_domain_space(aff, space);
2428 return aff;
2431 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2432 enum isl_dim_type type, unsigned first, unsigned n)
2434 isl_ctx *ctx;
2436 if (!aff)
2437 return NULL;
2438 if (type == isl_dim_out)
2439 isl_die(aff->v->ctx, isl_error_invalid,
2440 "cannot insert output/set dimensions",
2441 return isl_aff_free(aff));
2442 if (type == isl_dim_in)
2443 type = isl_dim_set;
2444 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2445 return aff;
2447 ctx = isl_aff_get_ctx(aff);
2448 if (first > isl_local_space_dim(aff->ls, type))
2449 isl_die(ctx, isl_error_invalid, "position out of bounds",
2450 return isl_aff_free(aff));
2452 aff = isl_aff_cow(aff);
2453 if (!aff)
2454 return NULL;
2456 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2457 if (!aff->ls)
2458 return isl_aff_free(aff);
2460 first += 1 + isl_local_space_offset(aff->ls, type);
2461 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2462 if (!aff->v)
2463 return isl_aff_free(aff);
2465 return aff;
2468 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2469 enum isl_dim_type type, unsigned n)
2471 unsigned pos;
2473 pos = isl_aff_dim(aff, type);
2475 return isl_aff_insert_dims(aff, type, pos, n);
2478 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2479 enum isl_dim_type type, unsigned n)
2481 unsigned pos;
2483 pos = isl_pw_aff_dim(pwaff, type);
2485 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2488 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2489 * to dimensions of "dst_type" at "dst_pos".
2491 * We only support moving input dimensions to parameters and vice versa.
2493 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2494 enum isl_dim_type dst_type, unsigned dst_pos,
2495 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2497 unsigned g_dst_pos;
2498 unsigned g_src_pos;
2500 if (!aff)
2501 return NULL;
2502 if (n == 0 &&
2503 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2504 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2505 return aff;
2507 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2508 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2509 "cannot move output/set dimension", isl_aff_free(aff));
2510 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2511 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2512 "cannot move divs", isl_aff_free(aff));
2513 if (dst_type == isl_dim_in)
2514 dst_type = isl_dim_set;
2515 if (src_type == isl_dim_in)
2516 src_type = isl_dim_set;
2518 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2519 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2520 "range out of bounds", isl_aff_free(aff));
2521 if (dst_type == src_type)
2522 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2523 "moving dims within the same type not supported",
2524 isl_aff_free(aff));
2526 aff = isl_aff_cow(aff);
2527 if (!aff)
2528 return NULL;
2530 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2531 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2532 if (dst_type > src_type)
2533 g_dst_pos -= n;
2535 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2536 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2537 src_type, src_pos, n);
2538 if (!aff->v || !aff->ls)
2539 return isl_aff_free(aff);
2541 aff = sort_divs(aff);
2543 return aff;
2546 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2548 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2549 return isl_pw_aff_alloc(dom, aff);
2552 #undef PW
2553 #define PW isl_pw_aff
2554 #undef EL
2555 #define EL isl_aff
2556 #undef EL_IS_ZERO
2557 #define EL_IS_ZERO is_empty
2558 #undef ZERO
2559 #define ZERO empty
2560 #undef IS_ZERO
2561 #define IS_ZERO is_empty
2562 #undef FIELD
2563 #define FIELD aff
2564 #undef DEFAULT_IS_ZERO
2565 #define DEFAULT_IS_ZERO 0
2567 #define NO_EVAL
2568 #define NO_OPT
2569 #define NO_LIFT
2570 #define NO_MORPH
2572 #include <isl_pw_templ.c>
2574 #undef UNION
2575 #define UNION isl_union_pw_aff
2576 #undef PART
2577 #define PART isl_pw_aff
2578 #undef PARTS
2579 #define PARTS pw_aff
2581 #define NO_EVAL
2583 #include <isl_union_templ.c>
2585 static __isl_give isl_set *align_params_pw_pw_set_and(
2586 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2587 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2588 __isl_take isl_pw_aff *pwaff2))
2590 if (!pwaff1 || !pwaff2)
2591 goto error;
2592 if (isl_space_match(pwaff1->dim, isl_dim_param,
2593 pwaff2->dim, isl_dim_param))
2594 return fn(pwaff1, pwaff2);
2595 if (!isl_space_has_named_params(pwaff1->dim) ||
2596 !isl_space_has_named_params(pwaff2->dim))
2597 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2598 "unaligned unnamed parameters", goto error);
2599 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2600 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2601 return fn(pwaff1, pwaff2);
2602 error:
2603 isl_pw_aff_free(pwaff1);
2604 isl_pw_aff_free(pwaff2);
2605 return NULL;
2608 /* Align the parameters of the to isl_pw_aff arguments and
2609 * then apply a function "fn" on them that returns an isl_map.
2611 static __isl_give isl_map *align_params_pw_pw_map_and(
2612 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2613 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2614 __isl_take isl_pw_aff *pa2))
2616 if (!pa1 || !pa2)
2617 goto error;
2618 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2619 return fn(pa1, pa2);
2620 if (!isl_space_has_named_params(pa1->dim) ||
2621 !isl_space_has_named_params(pa2->dim))
2622 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2623 "unaligned unnamed parameters", goto error);
2624 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2625 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2626 return fn(pa1, pa2);
2627 error:
2628 isl_pw_aff_free(pa1);
2629 isl_pw_aff_free(pa2);
2630 return NULL;
2633 /* Compute a piecewise quasi-affine expression with a domain that
2634 * is the union of those of pwaff1 and pwaff2 and such that on each
2635 * cell, the quasi-affine expression is the better (according to cmp)
2636 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2637 * is defined on a given cell, then the associated expression
2638 * is the defined one.
2640 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2641 __isl_take isl_pw_aff *pwaff2,
2642 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2643 __isl_take isl_aff *aff2))
2645 int i, j, n;
2646 isl_pw_aff *res;
2647 isl_ctx *ctx;
2648 isl_set *set;
2650 if (!pwaff1 || !pwaff2)
2651 goto error;
2653 ctx = isl_space_get_ctx(pwaff1->dim);
2654 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2655 isl_die(ctx, isl_error_invalid,
2656 "arguments should live in same space", goto error);
2658 if (isl_pw_aff_is_empty(pwaff1)) {
2659 isl_pw_aff_free(pwaff1);
2660 return pwaff2;
2663 if (isl_pw_aff_is_empty(pwaff2)) {
2664 isl_pw_aff_free(pwaff2);
2665 return pwaff1;
2668 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2669 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2671 for (i = 0; i < pwaff1->n; ++i) {
2672 set = isl_set_copy(pwaff1->p[i].set);
2673 for (j = 0; j < pwaff2->n; ++j) {
2674 struct isl_set *common;
2675 isl_set *better;
2677 common = isl_set_intersect(
2678 isl_set_copy(pwaff1->p[i].set),
2679 isl_set_copy(pwaff2->p[j].set));
2680 better = isl_set_from_basic_set(cmp(
2681 isl_aff_copy(pwaff2->p[j].aff),
2682 isl_aff_copy(pwaff1->p[i].aff)));
2683 better = isl_set_intersect(common, better);
2684 if (isl_set_plain_is_empty(better)) {
2685 isl_set_free(better);
2686 continue;
2688 set = isl_set_subtract(set, isl_set_copy(better));
2690 res = isl_pw_aff_add_piece(res, better,
2691 isl_aff_copy(pwaff2->p[j].aff));
2693 res = isl_pw_aff_add_piece(res, set,
2694 isl_aff_copy(pwaff1->p[i].aff));
2697 for (j = 0; j < pwaff2->n; ++j) {
2698 set = isl_set_copy(pwaff2->p[j].set);
2699 for (i = 0; i < pwaff1->n; ++i)
2700 set = isl_set_subtract(set,
2701 isl_set_copy(pwaff1->p[i].set));
2702 res = isl_pw_aff_add_piece(res, set,
2703 isl_aff_copy(pwaff2->p[j].aff));
2706 isl_pw_aff_free(pwaff1);
2707 isl_pw_aff_free(pwaff2);
2709 return res;
2710 error:
2711 isl_pw_aff_free(pwaff1);
2712 isl_pw_aff_free(pwaff2);
2713 return NULL;
2716 /* Compute a piecewise quasi-affine expression with a domain that
2717 * is the union of those of pwaff1 and pwaff2 and such that on each
2718 * cell, the quasi-affine expression is the maximum of those of pwaff1
2719 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2720 * cell, then the associated expression is the defined one.
2722 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2723 __isl_take isl_pw_aff *pwaff2)
2725 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2728 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2729 __isl_take isl_pw_aff *pwaff2)
2731 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2732 &pw_aff_union_max);
2735 /* Compute a piecewise quasi-affine expression with a domain that
2736 * is the union of those of pwaff1 and pwaff2 and such that on each
2737 * cell, the quasi-affine expression is the minimum of those of pwaff1
2738 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2739 * cell, then the associated expression is the defined one.
2741 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2742 __isl_take isl_pw_aff *pwaff2)
2744 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2747 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2748 __isl_take isl_pw_aff *pwaff2)
2750 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2751 &pw_aff_union_min);
2754 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2755 __isl_take isl_pw_aff *pwaff2, int max)
2757 if (max)
2758 return isl_pw_aff_union_max(pwaff1, pwaff2);
2759 else
2760 return isl_pw_aff_union_min(pwaff1, pwaff2);
2763 /* Construct a map with as domain the domain of pwaff and
2764 * one-dimensional range corresponding to the affine expressions.
2766 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2768 int i;
2769 isl_space *dim;
2770 isl_map *map;
2772 if (!pwaff)
2773 return NULL;
2775 dim = isl_pw_aff_get_space(pwaff);
2776 map = isl_map_empty(dim);
2778 for (i = 0; i < pwaff->n; ++i) {
2779 isl_basic_map *bmap;
2780 isl_map *map_i;
2782 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2783 map_i = isl_map_from_basic_map(bmap);
2784 map_i = isl_map_intersect_domain(map_i,
2785 isl_set_copy(pwaff->p[i].set));
2786 map = isl_map_union_disjoint(map, map_i);
2789 isl_pw_aff_free(pwaff);
2791 return map;
2794 /* Construct a map with as domain the domain of pwaff and
2795 * one-dimensional range corresponding to the affine expressions.
2797 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2799 if (!pwaff)
2800 return NULL;
2801 if (isl_space_is_set(pwaff->dim))
2802 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2803 "space of input is not a map", goto error);
2804 return map_from_pw_aff(pwaff);
2805 error:
2806 isl_pw_aff_free(pwaff);
2807 return NULL;
2810 /* Construct a one-dimensional set with as parameter domain
2811 * the domain of pwaff and the single set dimension
2812 * corresponding to the affine expressions.
2814 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2816 if (!pwaff)
2817 return NULL;
2818 if (!isl_space_is_set(pwaff->dim))
2819 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2820 "space of input is not a set", goto error);
2821 return map_from_pw_aff(pwaff);
2822 error:
2823 isl_pw_aff_free(pwaff);
2824 return NULL;
2827 /* Return a set containing those elements in the domain
2828 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2829 * does not satisfy "fn" (if complement is 1).
2831 * The pieces with a NaN never belong to the result since
2832 * NaN does not satisfy any property.
2834 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2835 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2836 int complement)
2838 int i;
2839 isl_set *set;
2841 if (!pwaff)
2842 return NULL;
2844 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2846 for (i = 0; i < pwaff->n; ++i) {
2847 isl_basic_set *bset;
2848 isl_set *set_i, *locus;
2849 int rational;
2851 if (isl_aff_is_nan(pwaff->p[i].aff))
2852 continue;
2854 rational = isl_set_has_rational(pwaff->p[i].set);
2855 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2856 locus = isl_set_from_basic_set(bset);
2857 set_i = isl_set_copy(pwaff->p[i].set);
2858 if (complement)
2859 set_i = isl_set_subtract(set_i, locus);
2860 else
2861 set_i = isl_set_intersect(set_i, locus);
2862 set = isl_set_union_disjoint(set, set_i);
2865 isl_pw_aff_free(pwaff);
2867 return set;
2870 /* Return a set containing those elements in the domain
2871 * of "pa" where it is positive.
2873 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2875 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2878 /* Return a set containing those elements in the domain
2879 * of pwaff where it is non-negative.
2881 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2883 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2886 /* Return a set containing those elements in the domain
2887 * of pwaff where it is zero.
2889 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2891 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2894 /* Return a set containing those elements in the domain
2895 * of pwaff where it is not zero.
2897 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2899 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2902 /* Return a set containing those elements in the shared domain
2903 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2905 * We compute the difference on the shared domain and then construct
2906 * the set of values where this difference is non-negative.
2907 * If strict is set, we first subtract 1 from the difference.
2908 * If equal is set, we only return the elements where pwaff1 and pwaff2
2909 * are equal.
2911 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2912 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2914 isl_set *set1, *set2;
2916 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2917 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2918 set1 = isl_set_intersect(set1, set2);
2919 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2920 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2921 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2923 if (strict) {
2924 isl_space *dim = isl_set_get_space(set1);
2925 isl_aff *aff;
2926 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2927 aff = isl_aff_add_constant_si(aff, -1);
2928 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2929 } else
2930 isl_set_free(set1);
2932 if (equal)
2933 return isl_pw_aff_zero_set(pwaff1);
2934 return isl_pw_aff_nonneg_set(pwaff1);
2937 /* Return a set containing those elements in the shared domain
2938 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2940 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2941 __isl_take isl_pw_aff *pwaff2)
2943 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2946 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2947 __isl_take isl_pw_aff *pwaff2)
2949 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2952 /* Return a set containing those elements in the shared domain
2953 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2955 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2956 __isl_take isl_pw_aff *pwaff2)
2958 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2961 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2962 __isl_take isl_pw_aff *pwaff2)
2964 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2967 /* Return a set containing those elements in the shared domain
2968 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2970 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2971 __isl_take isl_pw_aff *pwaff2)
2973 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2976 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2979 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2982 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2983 __isl_take isl_pw_aff *pwaff2)
2985 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2988 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2989 __isl_take isl_pw_aff *pwaff2)
2991 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2994 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2995 * where the function values are ordered in the same way as "order",
2996 * which returns a set in the shared domain of its two arguments.
2997 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2999 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3000 * We first pull back the two functions such that they are defined on
3001 * the domain [A -> B]. Then we apply "order", resulting in a set
3002 * in the space [A -> B]. Finally, we unwrap this set to obtain
3003 * a map in the space A -> B.
3005 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3006 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3007 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3008 __isl_take isl_pw_aff *pa2))
3010 isl_space *space1, *space2;
3011 isl_multi_aff *ma;
3012 isl_set *set;
3014 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3015 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3016 space1 = isl_space_map_from_domain_and_range(space1, space2);
3017 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3018 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3019 ma = isl_multi_aff_range_map(space1);
3020 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3021 set = order(pa1, pa2);
3023 return isl_set_unwrap(set);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function values are equal.
3028 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3030 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3031 __isl_take isl_pw_aff *pa2)
3033 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function values are equal.
3039 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3040 __isl_take isl_pw_aff *pa2)
3042 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is less than the function value of "pa2".
3047 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3049 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3050 __isl_take isl_pw_aff *pa2)
3052 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3055 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3056 * where the function value of "pa1" is less than the function value of "pa2".
3058 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3059 __isl_take isl_pw_aff *pa2)
3061 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3064 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3065 * where the function value of "pa1" is greater than the function value
3066 * of "pa2".
3067 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3069 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3070 __isl_take isl_pw_aff *pa2)
3072 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3075 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function value of "pa1" is greater than the function value
3077 * of "pa2".
3079 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3080 __isl_take isl_pw_aff *pa2)
3082 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3085 /* Return a set containing those elements in the shared domain
3086 * of the elements of list1 and list2 where each element in list1
3087 * has the relation specified by "fn" with each element in list2.
3089 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3090 __isl_take isl_pw_aff_list *list2,
3091 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3092 __isl_take isl_pw_aff *pwaff2))
3094 int i, j;
3095 isl_ctx *ctx;
3096 isl_set *set;
3098 if (!list1 || !list2)
3099 goto error;
3101 ctx = isl_pw_aff_list_get_ctx(list1);
3102 if (list1->n < 1 || list2->n < 1)
3103 isl_die(ctx, isl_error_invalid,
3104 "list should contain at least one element", goto error);
3106 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3107 for (i = 0; i < list1->n; ++i)
3108 for (j = 0; j < list2->n; ++j) {
3109 isl_set *set_ij;
3111 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3112 isl_pw_aff_copy(list2->p[j]));
3113 set = isl_set_intersect(set, set_ij);
3116 isl_pw_aff_list_free(list1);
3117 isl_pw_aff_list_free(list2);
3118 return set;
3119 error:
3120 isl_pw_aff_list_free(list1);
3121 isl_pw_aff_list_free(list2);
3122 return NULL;
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is equal to each element in list2.
3129 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3135 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3136 __isl_take isl_pw_aff_list *list2)
3138 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * is less than or equal to each element in list2.
3145 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2)
3148 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3151 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3152 __isl_take isl_pw_aff_list *list2)
3154 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3157 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3158 __isl_take isl_pw_aff_list *list2)
3160 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3163 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3164 __isl_take isl_pw_aff_list *list2)
3166 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3170 /* Return a set containing those elements in the shared domain
3171 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3173 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3174 __isl_take isl_pw_aff *pwaff2)
3176 isl_set *set_lt, *set_gt;
3178 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3179 isl_pw_aff_copy(pwaff2));
3180 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3181 return isl_set_union_disjoint(set_lt, set_gt);
3184 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3185 __isl_take isl_pw_aff *pwaff2)
3187 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3190 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3191 isl_int v)
3193 int i;
3195 if (isl_int_is_one(v))
3196 return pwaff;
3197 if (!isl_int_is_pos(v))
3198 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3199 "factor needs to be positive",
3200 return isl_pw_aff_free(pwaff));
3201 pwaff = isl_pw_aff_cow(pwaff);
3202 if (!pwaff)
3203 return NULL;
3204 if (pwaff->n == 0)
3205 return pwaff;
3207 for (i = 0; i < pwaff->n; ++i) {
3208 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3209 if (!pwaff->p[i].aff)
3210 return isl_pw_aff_free(pwaff);
3213 return pwaff;
3216 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3218 int i;
3220 pwaff = isl_pw_aff_cow(pwaff);
3221 if (!pwaff)
3222 return NULL;
3223 if (pwaff->n == 0)
3224 return pwaff;
3226 for (i = 0; i < pwaff->n; ++i) {
3227 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3228 if (!pwaff->p[i].aff)
3229 return isl_pw_aff_free(pwaff);
3232 return pwaff;
3235 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3237 int i;
3239 pwaff = isl_pw_aff_cow(pwaff);
3240 if (!pwaff)
3241 return NULL;
3242 if (pwaff->n == 0)
3243 return pwaff;
3245 for (i = 0; i < pwaff->n; ++i) {
3246 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3247 if (!pwaff->p[i].aff)
3248 return isl_pw_aff_free(pwaff);
3251 return pwaff;
3254 /* Assuming that "cond1" and "cond2" are disjoint,
3255 * return an affine expression that is equal to pwaff1 on cond1
3256 * and to pwaff2 on cond2.
3258 static __isl_give isl_pw_aff *isl_pw_aff_select(
3259 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3260 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3262 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3263 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3265 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3268 /* Return an affine expression that is equal to pwaff_true for elements
3269 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3270 * is zero.
3271 * That is, return cond ? pwaff_true : pwaff_false;
3273 * If "cond" involves and NaN, then we conservatively return a NaN
3274 * on its entire domain. In principle, we could consider the pieces
3275 * where it is NaN separately from those where it is not.
3277 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3278 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3280 isl_set *cond_true, *cond_false;
3282 if (!cond)
3283 goto error;
3284 if (isl_pw_aff_involves_nan(cond)) {
3285 isl_space *space = isl_pw_aff_get_domain_space(cond);
3286 isl_local_space *ls = isl_local_space_from_space(space);
3287 isl_pw_aff_free(cond);
3288 isl_pw_aff_free(pwaff_true);
3289 isl_pw_aff_free(pwaff_false);
3290 return isl_pw_aff_nan_on_domain(ls);
3293 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3294 cond_false = isl_pw_aff_zero_set(cond);
3295 return isl_pw_aff_select(cond_true, pwaff_true,
3296 cond_false, pwaff_false);
3297 error:
3298 isl_pw_aff_free(cond);
3299 isl_pw_aff_free(pwaff_true);
3300 isl_pw_aff_free(pwaff_false);
3301 return NULL;
3304 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3306 if (!aff)
3307 return -1;
3309 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3312 /* Check whether pwaff is a piecewise constant.
3314 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3316 int i;
3318 if (!pwaff)
3319 return -1;
3321 for (i = 0; i < pwaff->n; ++i) {
3322 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3323 if (is_cst < 0 || !is_cst)
3324 return is_cst;
3327 return 1;
3330 /* Return the product of "aff1" and "aff2".
3332 * If either of the two is NaN, then the result is NaN.
3334 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3336 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3337 __isl_take isl_aff *aff2)
3339 if (!aff1 || !aff2)
3340 goto error;
3342 if (isl_aff_is_nan(aff1)) {
3343 isl_aff_free(aff2);
3344 return aff1;
3346 if (isl_aff_is_nan(aff2)) {
3347 isl_aff_free(aff1);
3348 return aff2;
3351 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3352 return isl_aff_mul(aff2, aff1);
3354 if (!isl_aff_is_cst(aff2))
3355 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3356 "at least one affine expression should be constant",
3357 goto error);
3359 aff1 = isl_aff_cow(aff1);
3360 if (!aff1 || !aff2)
3361 goto error;
3363 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3364 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3366 isl_aff_free(aff2);
3367 return aff1;
3368 error:
3369 isl_aff_free(aff1);
3370 isl_aff_free(aff2);
3371 return NULL;
3374 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3376 * If either of the two is NaN, then the result is NaN.
3378 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3379 __isl_take isl_aff *aff2)
3381 int is_cst;
3382 int neg;
3384 if (!aff1 || !aff2)
3385 goto error;
3387 if (isl_aff_is_nan(aff1)) {
3388 isl_aff_free(aff2);
3389 return aff1;
3391 if (isl_aff_is_nan(aff2)) {
3392 isl_aff_free(aff1);
3393 return aff2;
3396 is_cst = isl_aff_is_cst(aff2);
3397 if (is_cst < 0)
3398 goto error;
3399 if (!is_cst)
3400 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3401 "second argument should be a constant", goto error);
3403 if (!aff2)
3404 goto error;
3406 neg = isl_int_is_neg(aff2->v->el[1]);
3407 if (neg) {
3408 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3409 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3412 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3413 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3415 if (neg) {
3416 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3417 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3420 isl_aff_free(aff2);
3421 return aff1;
3422 error:
3423 isl_aff_free(aff1);
3424 isl_aff_free(aff2);
3425 return NULL;
3428 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3429 __isl_take isl_pw_aff *pwaff2)
3431 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3434 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3435 __isl_take isl_pw_aff *pwaff2)
3437 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3440 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3441 __isl_take isl_pw_aff *pwaff2)
3443 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3446 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3447 __isl_take isl_pw_aff *pwaff2)
3449 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3452 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3458 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3459 __isl_take isl_pw_aff *pa2)
3461 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3464 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3466 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3467 __isl_take isl_pw_aff *pa2)
3469 int is_cst;
3471 is_cst = isl_pw_aff_is_cst(pa2);
3472 if (is_cst < 0)
3473 goto error;
3474 if (!is_cst)
3475 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3476 "second argument should be a piecewise constant",
3477 goto error);
3478 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3479 error:
3480 isl_pw_aff_free(pa1);
3481 isl_pw_aff_free(pa2);
3482 return NULL;
3485 /* Compute the quotient of the integer division of "pa1" by "pa2"
3486 * with rounding towards zero.
3487 * "pa2" is assumed to be a piecewise constant.
3489 * In particular, return
3491 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3494 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3495 __isl_take isl_pw_aff *pa2)
3497 int is_cst;
3498 isl_set *cond;
3499 isl_pw_aff *f, *c;
3501 is_cst = isl_pw_aff_is_cst(pa2);
3502 if (is_cst < 0)
3503 goto error;
3504 if (!is_cst)
3505 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3506 "second argument should be a piecewise constant",
3507 goto error);
3509 pa1 = isl_pw_aff_div(pa1, pa2);
3511 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3512 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3513 c = isl_pw_aff_ceil(pa1);
3514 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3515 error:
3516 isl_pw_aff_free(pa1);
3517 isl_pw_aff_free(pa2);
3518 return NULL;
3521 /* Compute the remainder of the integer division of "pa1" by "pa2"
3522 * with rounding towards zero.
3523 * "pa2" is assumed to be a piecewise constant.
3525 * In particular, return
3527 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3530 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3531 __isl_take isl_pw_aff *pa2)
3533 int is_cst;
3534 isl_pw_aff *res;
3536 is_cst = isl_pw_aff_is_cst(pa2);
3537 if (is_cst < 0)
3538 goto error;
3539 if (!is_cst)
3540 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3541 "second argument should be a piecewise constant",
3542 goto error);
3543 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3544 res = isl_pw_aff_mul(pa2, res);
3545 res = isl_pw_aff_sub(pa1, res);
3546 return res;
3547 error:
3548 isl_pw_aff_free(pa1);
3549 isl_pw_aff_free(pa2);
3550 return NULL;
3553 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3554 __isl_take isl_pw_aff *pwaff2)
3556 isl_set *le;
3557 isl_set *dom;
3559 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3560 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3561 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3562 isl_pw_aff_copy(pwaff2));
3563 dom = isl_set_subtract(dom, isl_set_copy(le));
3564 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3567 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3568 __isl_take isl_pw_aff *pwaff2)
3570 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3573 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3574 __isl_take isl_pw_aff *pwaff2)
3576 isl_set *ge;
3577 isl_set *dom;
3579 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3580 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3581 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3582 isl_pw_aff_copy(pwaff2));
3583 dom = isl_set_subtract(dom, isl_set_copy(ge));
3584 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3587 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3588 __isl_take isl_pw_aff *pwaff2)
3590 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3593 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3594 __isl_take isl_pw_aff_list *list,
3595 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3596 __isl_take isl_pw_aff *pwaff2))
3598 int i;
3599 isl_ctx *ctx;
3600 isl_pw_aff *res;
3602 if (!list)
3603 return NULL;
3605 ctx = isl_pw_aff_list_get_ctx(list);
3606 if (list->n < 1)
3607 isl_die(ctx, isl_error_invalid,
3608 "list should contain at least one element", goto error);
3610 res = isl_pw_aff_copy(list->p[0]);
3611 for (i = 1; i < list->n; ++i)
3612 res = fn(res, isl_pw_aff_copy(list->p[i]));
3614 isl_pw_aff_list_free(list);
3615 return res;
3616 error:
3617 isl_pw_aff_list_free(list);
3618 return NULL;
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the minimal corresponding affine
3623 * expression.
3625 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3627 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3630 /* Return an isl_pw_aff that maps each element in the intersection of the
3631 * domains of the elements of list to the maximal corresponding affine
3632 * expression.
3634 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3636 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3639 /* Mark the domains of "pwaff" as rational.
3641 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3643 int i;
3645 pwaff = isl_pw_aff_cow(pwaff);
3646 if (!pwaff)
3647 return NULL;
3648 if (pwaff->n == 0)
3649 return pwaff;
3651 for (i = 0; i < pwaff->n; ++i) {
3652 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3653 if (!pwaff->p[i].set)
3654 return isl_pw_aff_free(pwaff);
3657 return pwaff;
3660 /* Mark the domains of the elements of "list" as rational.
3662 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3663 __isl_take isl_pw_aff_list *list)
3665 int i, n;
3667 if (!list)
3668 return NULL;
3669 if (list->n == 0)
3670 return list;
3672 n = list->n;
3673 for (i = 0; i < n; ++i) {
3674 isl_pw_aff *pa;
3676 pa = isl_pw_aff_list_get_pw_aff(list, i);
3677 pa = isl_pw_aff_set_rational(pa);
3678 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3681 return list;
3684 /* Do the parameters of "aff" match those of "space"?
3686 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3687 __isl_keep isl_space *space)
3689 isl_space *aff_space;
3690 int match;
3692 if (!aff || !space)
3693 return -1;
3695 aff_space = isl_aff_get_domain_space(aff);
3697 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3699 isl_space_free(aff_space);
3700 return match;
3703 /* Check that the domain space of "aff" matches "space".
3705 * Return 0 on success and -1 on error.
3707 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3708 __isl_keep isl_space *space)
3710 isl_space *aff_space;
3711 int match;
3713 if (!aff || !space)
3714 return -1;
3716 aff_space = isl_aff_get_domain_space(aff);
3718 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3719 if (match < 0)
3720 goto error;
3721 if (!match)
3722 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3723 "parameters don't match", goto error);
3724 match = isl_space_tuple_is_equal(space, isl_dim_in,
3725 aff_space, isl_dim_set);
3726 if (match < 0)
3727 goto error;
3728 if (!match)
3729 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3730 "domains don't match", goto error);
3731 isl_space_free(aff_space);
3732 return 0;
3733 error:
3734 isl_space_free(aff_space);
3735 return -1;
3738 #undef BASE
3739 #define BASE aff
3740 #undef DOMBASE
3741 #define DOMBASE set
3742 #define NO_DOMAIN
3744 #include <isl_multi_templ.c>
3745 #include <isl_multi_apply_set.c>
3746 #include <isl_multi_floor.c>
3747 #include <isl_multi_gist.c>
3749 #undef NO_DOMAIN
3751 /* Remove any internal structure of the domain of "ma".
3752 * If there is any such internal structure in the input,
3753 * then the name of the corresponding space is also removed.
3755 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3756 __isl_take isl_multi_aff *ma)
3758 isl_space *space;
3760 if (!ma)
3761 return NULL;
3763 if (!ma->space->nested[0])
3764 return ma;
3766 space = isl_multi_aff_get_space(ma);
3767 space = isl_space_flatten_domain(space);
3768 ma = isl_multi_aff_reset_space(ma, space);
3770 return ma;
3773 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3774 * of the space to its domain.
3776 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3778 int i, n_in;
3779 isl_local_space *ls;
3780 isl_multi_aff *ma;
3782 if (!space)
3783 return NULL;
3784 if (!isl_space_is_map(space))
3785 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3786 "not a map space", goto error);
3788 n_in = isl_space_dim(space, isl_dim_in);
3789 space = isl_space_domain_map(space);
3791 ma = isl_multi_aff_alloc(isl_space_copy(space));
3792 if (n_in == 0) {
3793 isl_space_free(space);
3794 return ma;
3797 space = isl_space_domain(space);
3798 ls = isl_local_space_from_space(space);
3799 for (i = 0; i < n_in; ++i) {
3800 isl_aff *aff;
3802 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3803 isl_dim_set, i);
3804 ma = isl_multi_aff_set_aff(ma, i, aff);
3806 isl_local_space_free(ls);
3807 return ma;
3808 error:
3809 isl_space_free(space);
3810 return NULL;
3813 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3814 * of the space to its range.
3816 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3818 int i, n_in, n_out;
3819 isl_local_space *ls;
3820 isl_multi_aff *ma;
3822 if (!space)
3823 return NULL;
3824 if (!isl_space_is_map(space))
3825 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3826 "not a map space", goto error);
3828 n_in = isl_space_dim(space, isl_dim_in);
3829 n_out = isl_space_dim(space, isl_dim_out);
3830 space = isl_space_range_map(space);
3832 ma = isl_multi_aff_alloc(isl_space_copy(space));
3833 if (n_out == 0) {
3834 isl_space_free(space);
3835 return ma;
3838 space = isl_space_domain(space);
3839 ls = isl_local_space_from_space(space);
3840 for (i = 0; i < n_out; ++i) {
3841 isl_aff *aff;
3843 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3844 isl_dim_set, n_in + i);
3845 ma = isl_multi_aff_set_aff(ma, i, aff);
3847 isl_local_space_free(ls);
3848 return ma;
3849 error:
3850 isl_space_free(space);
3851 return NULL;
3854 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3855 * of the space to its range.
3857 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3858 __isl_take isl_space *space)
3860 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3863 /* Given the space of a set and a range of set dimensions,
3864 * construct an isl_multi_aff that projects out those dimensions.
3866 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3867 __isl_take isl_space *space, enum isl_dim_type type,
3868 unsigned first, unsigned n)
3870 int i, dim;
3871 isl_local_space *ls;
3872 isl_multi_aff *ma;
3874 if (!space)
3875 return NULL;
3876 if (!isl_space_is_set(space))
3877 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3878 "expecting set space", goto error);
3879 if (type != isl_dim_set)
3880 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3881 "only set dimensions can be projected out", goto error);
3883 dim = isl_space_dim(space, isl_dim_set);
3884 if (first + n > dim)
3885 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3886 "range out of bounds", goto error);
3888 space = isl_space_from_domain(space);
3889 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3891 if (dim == n)
3892 return isl_multi_aff_alloc(space);
3894 ma = isl_multi_aff_alloc(isl_space_copy(space));
3895 space = isl_space_domain(space);
3896 ls = isl_local_space_from_space(space);
3898 for (i = 0; i < first; ++i) {
3899 isl_aff *aff;
3901 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3902 isl_dim_set, i);
3903 ma = isl_multi_aff_set_aff(ma, i, aff);
3906 for (i = 0; i < dim - (first + n); ++i) {
3907 isl_aff *aff;
3909 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3910 isl_dim_set, first + n + i);
3911 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3914 isl_local_space_free(ls);
3915 return ma;
3916 error:
3917 isl_space_free(space);
3918 return NULL;
3921 /* Given the space of a set and a range of set dimensions,
3922 * construct an isl_pw_multi_aff that projects out those dimensions.
3924 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3925 __isl_take isl_space *space, enum isl_dim_type type,
3926 unsigned first, unsigned n)
3928 isl_multi_aff *ma;
3930 ma = isl_multi_aff_project_out_map(space, type, first, n);
3931 return isl_pw_multi_aff_from_multi_aff(ma);
3934 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3935 * domain.
3937 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3938 __isl_take isl_multi_aff *ma)
3940 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3941 return isl_pw_multi_aff_alloc(dom, ma);
3944 /* Create a piecewise multi-affine expression in the given space that maps each
3945 * input dimension to the corresponding output dimension.
3947 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3948 __isl_take isl_space *space)
3950 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3953 /* Add "ma2" to "ma1" and return the result.
3955 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3957 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3958 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3960 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3963 /* Add "ma2" to "ma1" and return the result.
3965 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3966 __isl_take isl_multi_aff *ma2)
3968 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3969 &isl_multi_aff_add_aligned);
3972 /* Exploit the equalities in "eq" to simplify the affine expressions.
3974 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3975 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3977 int i;
3979 maff = isl_multi_aff_cow(maff);
3980 if (!maff || !eq)
3981 goto error;
3983 for (i = 0; i < maff->n; ++i) {
3984 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3985 isl_basic_set_copy(eq));
3986 if (!maff->p[i])
3987 goto error;
3990 isl_basic_set_free(eq);
3991 return maff;
3992 error:
3993 isl_basic_set_free(eq);
3994 isl_multi_aff_free(maff);
3995 return NULL;
3998 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3999 isl_int f)
4001 int i;
4003 maff = isl_multi_aff_cow(maff);
4004 if (!maff)
4005 return NULL;
4007 for (i = 0; i < maff->n; ++i) {
4008 maff->p[i] = isl_aff_scale(maff->p[i], f);
4009 if (!maff->p[i])
4010 return isl_multi_aff_free(maff);
4013 return maff;
4016 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4017 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4019 maff1 = isl_multi_aff_add(maff1, maff2);
4020 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4021 return maff1;
4024 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4026 if (!maff)
4027 return -1;
4029 return 0;
4032 /* Return the set of domain elements where "ma1" is lexicographically
4033 * smaller than or equal to "ma2".
4035 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4036 __isl_take isl_multi_aff *ma2)
4038 return isl_multi_aff_lex_ge_set(ma2, ma1);
4041 /* Return the set of domain elements where "ma1" is lexicographically
4042 * greater than or equal to "ma2".
4044 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4045 __isl_take isl_multi_aff *ma2)
4047 isl_space *space;
4048 isl_map *map1, *map2;
4049 isl_map *map, *ge;
4051 map1 = isl_map_from_multi_aff(ma1);
4052 map2 = isl_map_from_multi_aff(ma2);
4053 map = isl_map_range_product(map1, map2);
4054 space = isl_space_range(isl_map_get_space(map));
4055 space = isl_space_domain(isl_space_unwrap(space));
4056 ge = isl_map_lex_ge(space);
4057 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4059 return isl_map_domain(map);
4062 #undef PW
4063 #define PW isl_pw_multi_aff
4064 #undef EL
4065 #define EL isl_multi_aff
4066 #undef EL_IS_ZERO
4067 #define EL_IS_ZERO is_empty
4068 #undef ZERO
4069 #define ZERO empty
4070 #undef IS_ZERO
4071 #define IS_ZERO is_empty
4072 #undef FIELD
4073 #define FIELD maff
4074 #undef DEFAULT_IS_ZERO
4075 #define DEFAULT_IS_ZERO 0
4077 #define NO_SUB
4078 #define NO_EVAL
4079 #define NO_OPT
4080 #define NO_INVOLVES_DIMS
4081 #define NO_INSERT_DIMS
4082 #define NO_LIFT
4083 #define NO_MORPH
4085 #include <isl_pw_templ.c>
4087 #undef NO_SUB
4089 #undef UNION
4090 #define UNION isl_union_pw_multi_aff
4091 #undef PART
4092 #define PART isl_pw_multi_aff
4093 #undef PARTS
4094 #define PARTS pw_multi_aff
4096 #define NO_EVAL
4098 #include <isl_union_templ.c>
4100 /* Given a function "cmp" that returns the set of elements where
4101 * "ma1" is "better" than "ma2", return the intersection of this
4102 * set with "dom1" and "dom2".
4104 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
4105 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
4106 __isl_keep isl_multi_aff *ma2,
4107 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4108 __isl_take isl_multi_aff *ma2))
4110 isl_set *common;
4111 isl_set *better;
4112 int is_empty;
4114 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
4115 is_empty = isl_set_plain_is_empty(common);
4116 if (is_empty >= 0 && is_empty)
4117 return common;
4118 if (is_empty < 0)
4119 return isl_set_free(common);
4120 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
4121 better = isl_set_intersect(common, better);
4123 return better;
4126 /* Given a function "cmp" that returns the set of elements where
4127 * "ma1" is "better" than "ma2", return a piecewise multi affine
4128 * expression defined on the union of the definition domains
4129 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4130 * "pma2" on each cell. If only one of the two input functions
4131 * is defined on a given cell, then it is considered the best.
4133 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4134 __isl_take isl_pw_multi_aff *pma1,
4135 __isl_take isl_pw_multi_aff *pma2,
4136 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4137 __isl_take isl_multi_aff *ma2))
4139 int i, j, n;
4140 isl_pw_multi_aff *res = NULL;
4141 isl_ctx *ctx;
4142 isl_set *set = NULL;
4144 if (!pma1 || !pma2)
4145 goto error;
4147 ctx = isl_space_get_ctx(pma1->dim);
4148 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4149 isl_die(ctx, isl_error_invalid,
4150 "arguments should live in the same space", goto error);
4152 if (isl_pw_multi_aff_is_empty(pma1)) {
4153 isl_pw_multi_aff_free(pma1);
4154 return pma2;
4157 if (isl_pw_multi_aff_is_empty(pma2)) {
4158 isl_pw_multi_aff_free(pma2);
4159 return pma1;
4162 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4163 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4165 for (i = 0; i < pma1->n; ++i) {
4166 set = isl_set_copy(pma1->p[i].set);
4167 for (j = 0; j < pma2->n; ++j) {
4168 isl_set *better;
4169 int is_empty;
4171 better = shared_and_better(pma2->p[j].set,
4172 pma1->p[i].set, pma2->p[j].maff,
4173 pma1->p[i].maff, cmp);
4174 is_empty = isl_set_plain_is_empty(better);
4175 if (is_empty < 0 || is_empty) {
4176 isl_set_free(better);
4177 if (is_empty < 0)
4178 goto error;
4179 continue;
4181 set = isl_set_subtract(set, isl_set_copy(better));
4183 res = isl_pw_multi_aff_add_piece(res, better,
4184 isl_multi_aff_copy(pma2->p[j].maff));
4186 res = isl_pw_multi_aff_add_piece(res, set,
4187 isl_multi_aff_copy(pma1->p[i].maff));
4190 for (j = 0; j < pma2->n; ++j) {
4191 set = isl_set_copy(pma2->p[j].set);
4192 for (i = 0; i < pma1->n; ++i)
4193 set = isl_set_subtract(set,
4194 isl_set_copy(pma1->p[i].set));
4195 res = isl_pw_multi_aff_add_piece(res, set,
4196 isl_multi_aff_copy(pma2->p[j].maff));
4199 isl_pw_multi_aff_free(pma1);
4200 isl_pw_multi_aff_free(pma2);
4202 return res;
4203 error:
4204 isl_pw_multi_aff_free(pma1);
4205 isl_pw_multi_aff_free(pma2);
4206 isl_set_free(set);
4207 return isl_pw_multi_aff_free(res);
4210 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4211 __isl_take isl_pw_multi_aff *pma1,
4212 __isl_take isl_pw_multi_aff *pma2)
4214 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4217 /* Given two piecewise multi affine expressions, return a piecewise
4218 * multi-affine expression defined on the union of the definition domains
4219 * of the inputs that is equal to the lexicographic maximum of the two
4220 * inputs on each cell. If only one of the two inputs is defined on
4221 * a given cell, then it is considered to be the maximum.
4223 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4224 __isl_take isl_pw_multi_aff *pma1,
4225 __isl_take isl_pw_multi_aff *pma2)
4227 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4228 &pw_multi_aff_union_lexmax);
4231 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4232 __isl_take isl_pw_multi_aff *pma1,
4233 __isl_take isl_pw_multi_aff *pma2)
4235 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4238 /* Given two piecewise multi affine expressions, return a piecewise
4239 * multi-affine expression defined on the union of the definition domains
4240 * of the inputs that is equal to the lexicographic minimum of the two
4241 * inputs on each cell. If only one of the two inputs is defined on
4242 * a given cell, then it is considered to be the minimum.
4244 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4245 __isl_take isl_pw_multi_aff *pma1,
4246 __isl_take isl_pw_multi_aff *pma2)
4248 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4249 &pw_multi_aff_union_lexmin);
4252 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4253 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4255 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4256 &isl_multi_aff_add);
4259 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4260 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4262 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4263 &pw_multi_aff_add);
4266 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4267 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4269 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4270 &isl_multi_aff_sub);
4273 /* Subtract "pma2" from "pma1" and return the result.
4275 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4276 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4278 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4279 &pw_multi_aff_sub);
4282 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4283 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4285 return isl_pw_multi_aff_union_add_(pma1, pma2);
4288 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4289 * with the actual sum on the shared domain and
4290 * the defined expression on the symmetric difference of the domains.
4292 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4293 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4295 return isl_union_pw_aff_union_add_(upa1, upa2);
4298 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4299 * with the actual sum on the shared domain and
4300 * the defined expression on the symmetric difference of the domains.
4302 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4303 __isl_take isl_union_pw_multi_aff *upma1,
4304 __isl_take isl_union_pw_multi_aff *upma2)
4306 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4309 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4310 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4312 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4313 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4315 int i, j, n;
4316 isl_space *space;
4317 isl_pw_multi_aff *res;
4319 if (!pma1 || !pma2)
4320 goto error;
4322 n = pma1->n * pma2->n;
4323 space = isl_space_product(isl_space_copy(pma1->dim),
4324 isl_space_copy(pma2->dim));
4325 res = isl_pw_multi_aff_alloc_size(space, n);
4327 for (i = 0; i < pma1->n; ++i) {
4328 for (j = 0; j < pma2->n; ++j) {
4329 isl_set *domain;
4330 isl_multi_aff *ma;
4332 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4333 isl_set_copy(pma2->p[j].set));
4334 ma = isl_multi_aff_product(
4335 isl_multi_aff_copy(pma1->p[i].maff),
4336 isl_multi_aff_copy(pma2->p[j].maff));
4337 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4341 isl_pw_multi_aff_free(pma1);
4342 isl_pw_multi_aff_free(pma2);
4343 return res;
4344 error:
4345 isl_pw_multi_aff_free(pma1);
4346 isl_pw_multi_aff_free(pma2);
4347 return NULL;
4350 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4351 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4353 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4354 &pw_multi_aff_product);
4357 /* Construct a map mapping the domain of the piecewise multi-affine expression
4358 * to its range, with each dimension in the range equated to the
4359 * corresponding affine expression on its cell.
4361 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4363 int i;
4364 isl_map *map;
4366 if (!pma)
4367 return NULL;
4369 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4371 for (i = 0; i < pma->n; ++i) {
4372 isl_multi_aff *maff;
4373 isl_basic_map *bmap;
4374 isl_map *map_i;
4376 maff = isl_multi_aff_copy(pma->p[i].maff);
4377 bmap = isl_basic_map_from_multi_aff(maff);
4378 map_i = isl_map_from_basic_map(bmap);
4379 map_i = isl_map_intersect_domain(map_i,
4380 isl_set_copy(pma->p[i].set));
4381 map = isl_map_union_disjoint(map, map_i);
4384 isl_pw_multi_aff_free(pma);
4385 return map;
4388 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4390 if (!pma)
4391 return NULL;
4393 if (!isl_space_is_set(pma->dim))
4394 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4395 "isl_pw_multi_aff cannot be converted into an isl_set",
4396 goto error);
4398 return isl_map_from_pw_multi_aff(pma);
4399 error:
4400 isl_pw_multi_aff_free(pma);
4401 return NULL;
4404 /* Given a basic map with a single output dimension that is defined
4405 * in terms of the parameters and input dimensions using an equality,
4406 * extract an isl_aff that expresses the output dimension in terms
4407 * of the parameters and input dimensions.
4408 * Note that this expression may involve integer divisions defined
4409 * in terms of parameters and input dimensions.
4411 * This function shares some similarities with
4412 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4414 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4415 __isl_take isl_basic_map *bmap)
4417 int eq;
4418 unsigned offset;
4419 unsigned n_div;
4420 isl_local_space *ls;
4421 isl_aff *aff;
4423 if (!bmap)
4424 return NULL;
4425 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4426 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4427 "basic map should have a single output dimension",
4428 goto error);
4429 eq = isl_basic_map_output_defining_equality(bmap, 0);
4430 if (eq >= bmap->n_eq)
4431 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4432 "unable to find suitable equality", goto error);
4433 ls = isl_basic_map_get_local_space(bmap);
4434 aff = isl_aff_alloc(isl_local_space_domain(ls));
4435 if (!aff)
4436 goto error;
4437 offset = isl_basic_map_offset(bmap, isl_dim_out);
4438 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4439 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4440 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4441 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4442 n_div);
4443 } else {
4444 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4445 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4446 n_div);
4448 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4449 isl_basic_map_free(bmap);
4451 aff = isl_aff_remove_unused_divs(aff);
4452 return aff;
4453 error:
4454 isl_basic_map_free(bmap);
4455 return NULL;
4458 /* Given a basic map where each output dimension is defined
4459 * in terms of the parameters and input dimensions using an equality,
4460 * extract an isl_multi_aff that expresses the output dimensions in terms
4461 * of the parameters and input dimensions.
4463 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4464 __isl_take isl_basic_map *bmap)
4466 int i;
4467 unsigned n_out;
4468 isl_multi_aff *ma;
4470 if (!bmap)
4471 return NULL;
4473 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4474 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4476 for (i = 0; i < n_out; ++i) {
4477 isl_basic_map *bmap_i;
4478 isl_aff *aff;
4480 bmap_i = isl_basic_map_copy(bmap);
4481 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4482 i + 1, n_out - (1 + i));
4483 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4484 aff = extract_isl_aff_from_basic_map(bmap_i);
4485 ma = isl_multi_aff_set_aff(ma, i, aff);
4488 isl_basic_map_free(bmap);
4490 return ma;
4493 /* Given a basic set where each set dimension is defined
4494 * in terms of the parameters using an equality,
4495 * extract an isl_multi_aff that expresses the set dimensions in terms
4496 * of the parameters.
4498 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4499 __isl_take isl_basic_set *bset)
4501 return extract_isl_multi_aff_from_basic_map(bset);
4504 /* Create an isl_pw_multi_aff that is equivalent to
4505 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4506 * The given basic map is such that each output dimension is defined
4507 * in terms of the parameters and input dimensions using an equality.
4509 * Since some applications expect the result of isl_pw_multi_aff_from_map
4510 * to only contain integer affine expressions, we compute the floor
4511 * of the expression before returning.
4513 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4514 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4516 isl_multi_aff *ma;
4518 ma = extract_isl_multi_aff_from_basic_map(bmap);
4519 ma = isl_multi_aff_floor(ma);
4520 return isl_pw_multi_aff_alloc(domain, ma);
4523 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4524 * This obviously only works if the input "map" is single-valued.
4525 * If so, we compute the lexicographic minimum of the image in the form
4526 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4527 * to its lexicographic minimum.
4528 * If the input is not single-valued, we produce an error.
4530 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4531 __isl_take isl_map *map)
4533 int i;
4534 int sv;
4535 isl_pw_multi_aff *pma;
4537 sv = isl_map_is_single_valued(map);
4538 if (sv < 0)
4539 goto error;
4540 if (!sv)
4541 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4542 "map is not single-valued", goto error);
4543 map = isl_map_make_disjoint(map);
4544 if (!map)
4545 return NULL;
4547 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4549 for (i = 0; i < map->n; ++i) {
4550 isl_pw_multi_aff *pma_i;
4551 isl_basic_map *bmap;
4552 bmap = isl_basic_map_copy(map->p[i]);
4553 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4554 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4557 isl_map_free(map);
4558 return pma;
4559 error:
4560 isl_map_free(map);
4561 return NULL;
4564 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4565 * taking into account that the output dimension at position "d"
4566 * can be represented as
4568 * x = floor((e(...) + c1) / m)
4570 * given that constraint "i" is of the form
4572 * e(...) + c1 - m x >= 0
4575 * Let "map" be of the form
4577 * A -> B
4579 * We construct a mapping
4581 * A -> [A -> x = floor(...)]
4583 * apply that to the map, obtaining
4585 * [A -> x = floor(...)] -> B
4587 * and equate dimension "d" to x.
4588 * We then compute a isl_pw_multi_aff representation of the resulting map
4589 * and plug in the mapping above.
4591 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4592 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4594 isl_ctx *ctx;
4595 isl_space *space;
4596 isl_local_space *ls;
4597 isl_multi_aff *ma;
4598 isl_aff *aff;
4599 isl_vec *v;
4600 isl_map *insert;
4601 int offset;
4602 int n;
4603 int n_in;
4604 isl_pw_multi_aff *pma;
4605 int is_set;
4607 is_set = isl_map_is_set(map);
4609 offset = isl_basic_map_offset(hull, isl_dim_out);
4610 ctx = isl_map_get_ctx(map);
4611 space = isl_space_domain(isl_map_get_space(map));
4612 n_in = isl_space_dim(space, isl_dim_set);
4613 n = isl_space_dim(space, isl_dim_all);
4615 v = isl_vec_alloc(ctx, 1 + 1 + n);
4616 if (v) {
4617 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4618 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4620 isl_basic_map_free(hull);
4622 ls = isl_local_space_from_space(isl_space_copy(space));
4623 aff = isl_aff_alloc_vec(ls, v);
4624 aff = isl_aff_floor(aff);
4625 if (is_set) {
4626 isl_space_free(space);
4627 ma = isl_multi_aff_from_aff(aff);
4628 } else {
4629 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4630 ma = isl_multi_aff_range_product(ma,
4631 isl_multi_aff_from_aff(aff));
4634 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4635 map = isl_map_apply_domain(map, insert);
4636 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4637 pma = isl_pw_multi_aff_from_map(map);
4638 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4640 return pma;
4643 /* Is constraint "c" of the form
4645 * e(...) + c1 - m x >= 0
4647 * or
4649 * -e(...) + c2 + m x >= 0
4651 * where m > 1 and e only depends on parameters and input dimemnsions?
4653 * "offset" is the offset of the output dimensions
4654 * "pos" is the position of output dimension x.
4656 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4658 if (isl_int_is_zero(c[offset + d]))
4659 return 0;
4660 if (isl_int_is_one(c[offset + d]))
4661 return 0;
4662 if (isl_int_is_negone(c[offset + d]))
4663 return 0;
4664 if (isl_seq_first_non_zero(c + offset, d) != -1)
4665 return 0;
4666 if (isl_seq_first_non_zero(c + offset + d + 1,
4667 total - (offset + d + 1)) != -1)
4668 return 0;
4669 return 1;
4672 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4674 * As a special case, we first check if there is any pair of constraints,
4675 * shared by all the basic maps in "map" that force a given dimension
4676 * to be equal to the floor of some affine combination of the input dimensions.
4678 * In particular, if we can find two constraints
4680 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4682 * and
4684 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4686 * where m > 1 and e only depends on parameters and input dimemnsions,
4687 * and such that
4689 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4691 * then we know that we can take
4693 * x = floor((e(...) + c1) / m)
4695 * without having to perform any computation.
4697 * Note that we know that
4699 * c1 + c2 >= 1
4701 * If c1 + c2 were 0, then we would have detected an equality during
4702 * simplification. If c1 + c2 were negative, then we would have detected
4703 * a contradiction.
4705 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4706 __isl_take isl_map *map)
4708 int d, dim;
4709 int i, j, n;
4710 int offset, total;
4711 isl_int sum;
4712 isl_basic_map *hull;
4714 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4715 if (!hull)
4716 goto error;
4718 isl_int_init(sum);
4719 dim = isl_map_dim(map, isl_dim_out);
4720 offset = isl_basic_map_offset(hull, isl_dim_out);
4721 total = 1 + isl_basic_map_total_dim(hull);
4722 n = hull->n_ineq;
4723 for (d = 0; d < dim; ++d) {
4724 for (i = 0; i < n; ++i) {
4725 if (!is_potential_div_constraint(hull->ineq[i],
4726 offset, d, total))
4727 continue;
4728 for (j = i + 1; j < n; ++j) {
4729 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4730 hull->ineq[j] + 1, total - 1))
4731 continue;
4732 isl_int_add(sum, hull->ineq[i][0],
4733 hull->ineq[j][0]);
4734 if (isl_int_abs_lt(sum,
4735 hull->ineq[i][offset + d]))
4736 break;
4739 if (j >= n)
4740 continue;
4741 isl_int_clear(sum);
4742 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4743 j = i;
4744 return pw_multi_aff_from_map_div(map, hull, d, j);
4747 isl_int_clear(sum);
4748 isl_basic_map_free(hull);
4749 return pw_multi_aff_from_map_base(map);
4750 error:
4751 isl_map_free(map);
4752 isl_basic_map_free(hull);
4753 return NULL;
4756 /* Given an affine expression
4758 * [A -> B] -> f(A,B)
4760 * construct an isl_multi_aff
4762 * [A -> B] -> B'
4764 * such that dimension "d" in B' is set to "aff" and the remaining
4765 * dimensions are set equal to the corresponding dimensions in B.
4766 * "n_in" is the dimension of the space A.
4767 * "n_out" is the dimension of the space B.
4769 * If "is_set" is set, then the affine expression is of the form
4771 * [B] -> f(B)
4773 * and we construct an isl_multi_aff
4775 * B -> B'
4777 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4778 unsigned n_in, unsigned n_out, int is_set)
4780 int i;
4781 isl_multi_aff *ma;
4782 isl_space *space, *space2;
4783 isl_local_space *ls;
4785 space = isl_aff_get_domain_space(aff);
4786 ls = isl_local_space_from_space(isl_space_copy(space));
4787 space2 = isl_space_copy(space);
4788 if (!is_set)
4789 space2 = isl_space_range(isl_space_unwrap(space2));
4790 space = isl_space_map_from_domain_and_range(space, space2);
4791 ma = isl_multi_aff_alloc(space);
4792 ma = isl_multi_aff_set_aff(ma, d, aff);
4794 for (i = 0; i < n_out; ++i) {
4795 if (i == d)
4796 continue;
4797 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4798 isl_dim_set, n_in + i);
4799 ma = isl_multi_aff_set_aff(ma, i, aff);
4802 isl_local_space_free(ls);
4804 return ma;
4807 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4808 * taking into account that the dimension at position "d" can be written as
4810 * x = m a + f(..) (1)
4812 * where m is equal to "gcd".
4813 * "i" is the index of the equality in "hull" that defines f(..).
4814 * In particular, the equality is of the form
4816 * f(..) - x + m g(existentials) = 0
4818 * or
4820 * -f(..) + x + m g(existentials) = 0
4822 * We basically plug (1) into "map", resulting in a map with "a"
4823 * in the range instead of "x". The corresponding isl_pw_multi_aff
4824 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4826 * Specifically, given the input map
4828 * A -> B
4830 * We first wrap it into a set
4832 * [A -> B]
4834 * and define (1) on top of the corresponding space, resulting in "aff".
4835 * We use this to create an isl_multi_aff that maps the output position "d"
4836 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4837 * We plug this into the wrapped map, unwrap the result and compute the
4838 * corresponding isl_pw_multi_aff.
4839 * The result is an expression
4841 * A -> T(A)
4843 * We adjust that to
4845 * A -> [A -> T(A)]
4847 * so that we can plug that into "aff", after extending the latter to
4848 * a mapping
4850 * [A -> B] -> B'
4853 * If "map" is actually a set, then there is no "A" space, meaning
4854 * that we do not need to perform any wrapping, and that the result
4855 * of the recursive call is of the form
4857 * [T]
4859 * which is plugged into a mapping of the form
4861 * B -> B'
4863 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4864 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4865 isl_int gcd)
4867 isl_set *set;
4868 isl_space *space;
4869 isl_local_space *ls;
4870 isl_aff *aff;
4871 isl_multi_aff *ma;
4872 isl_pw_multi_aff *pma, *id;
4873 unsigned n_in;
4874 unsigned o_out;
4875 unsigned n_out;
4876 int is_set;
4878 is_set = isl_map_is_set(map);
4880 n_in = isl_basic_map_dim(hull, isl_dim_in);
4881 n_out = isl_basic_map_dim(hull, isl_dim_out);
4882 o_out = isl_basic_map_offset(hull, isl_dim_out);
4884 if (is_set)
4885 set = map;
4886 else
4887 set = isl_map_wrap(map);
4888 space = isl_space_map_from_set(isl_set_get_space(set));
4889 ma = isl_multi_aff_identity(space);
4890 ls = isl_local_space_from_space(isl_set_get_space(set));
4891 aff = isl_aff_alloc(ls);
4892 if (aff) {
4893 isl_int_set_si(aff->v->el[0], 1);
4894 if (isl_int_is_one(hull->eq[i][o_out + d]))
4895 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4896 aff->v->size - 1);
4897 else
4898 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4899 aff->v->size - 1);
4900 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4902 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4903 set = isl_set_preimage_multi_aff(set, ma);
4905 ma = range_map(aff, d, n_in, n_out, is_set);
4907 if (is_set)
4908 map = set;
4909 else
4910 map = isl_set_unwrap(set);
4911 pma = isl_pw_multi_aff_from_map(map);
4913 if (!is_set) {
4914 space = isl_pw_multi_aff_get_domain_space(pma);
4915 space = isl_space_map_from_set(space);
4916 id = isl_pw_multi_aff_identity(space);
4917 pma = isl_pw_multi_aff_range_product(id, pma);
4919 id = isl_pw_multi_aff_from_multi_aff(ma);
4920 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4922 isl_basic_map_free(hull);
4923 return pma;
4926 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4928 * As a special case, we first check if all output dimensions are uniquely
4929 * defined in terms of the parameters and input dimensions over the entire
4930 * domain. If so, we extract the desired isl_pw_multi_aff directly
4931 * from the affine hull of "map" and its domain.
4933 * Otherwise, we check if any of the output dimensions is "strided".
4934 * That is, we check if can be written as
4936 * x = m a + f(..)
4938 * with m greater than 1, a some combination of existentiall quantified
4939 * variables and f and expression in the parameters and input dimensions.
4940 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4942 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4943 * special case.
4945 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4947 int i, j;
4948 int sv;
4949 isl_basic_map *hull;
4950 unsigned n_out;
4951 unsigned o_out;
4952 unsigned n_div;
4953 unsigned o_div;
4954 isl_int gcd;
4956 if (!map)
4957 return NULL;
4959 hull = isl_map_affine_hull(isl_map_copy(map));
4960 sv = isl_basic_map_plain_is_single_valued(hull);
4961 if (sv >= 0 && sv)
4962 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4963 if (sv < 0)
4964 hull = isl_basic_map_free(hull);
4965 if (!hull)
4966 goto error;
4968 n_div = isl_basic_map_dim(hull, isl_dim_div);
4969 o_div = isl_basic_map_offset(hull, isl_dim_div);
4971 if (n_div == 0) {
4972 isl_basic_map_free(hull);
4973 return pw_multi_aff_from_map_check_div(map);
4976 isl_int_init(gcd);
4978 n_out = isl_basic_map_dim(hull, isl_dim_out);
4979 o_out = isl_basic_map_offset(hull, isl_dim_out);
4981 for (i = 0; i < n_out; ++i) {
4982 for (j = 0; j < hull->n_eq; ++j) {
4983 isl_int *eq = hull->eq[j];
4984 isl_pw_multi_aff *res;
4986 if (!isl_int_is_one(eq[o_out + i]) &&
4987 !isl_int_is_negone(eq[o_out + i]))
4988 continue;
4989 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4990 continue;
4991 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4992 n_out - (i + 1)) != -1)
4993 continue;
4994 isl_seq_gcd(eq + o_div, n_div, &gcd);
4995 if (isl_int_is_zero(gcd))
4996 continue;
4997 if (isl_int_is_one(gcd))
4998 continue;
5000 res = pw_multi_aff_from_map_stride(map, hull,
5001 i, j, gcd);
5002 isl_int_clear(gcd);
5003 return res;
5007 isl_int_clear(gcd);
5008 isl_basic_map_free(hull);
5009 return pw_multi_aff_from_map_check_div(map);
5010 error:
5011 isl_map_free(map);
5012 return NULL;
5015 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5017 return isl_pw_multi_aff_from_map(set);
5020 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5021 * add it to *user.
5023 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5025 isl_union_pw_multi_aff **upma = user;
5026 isl_pw_multi_aff *pma;
5028 pma = isl_pw_multi_aff_from_map(map);
5029 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5031 return *upma ? 0 : -1;
5034 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5035 * domain.
5037 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5038 __isl_take isl_aff *aff)
5040 isl_multi_aff *ma;
5041 isl_pw_multi_aff *pma;
5043 ma = isl_multi_aff_from_aff(aff);
5044 pma = isl_pw_multi_aff_from_multi_aff(ma);
5045 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5048 /* Try and create an isl_union_pw_multi_aff that is equivalent
5049 * to the given isl_union_map.
5050 * The isl_union_map is required to be single-valued in each space.
5051 * Otherwise, an error is produced.
5053 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5054 __isl_take isl_union_map *umap)
5056 isl_space *space;
5057 isl_union_pw_multi_aff *upma;
5059 space = isl_union_map_get_space(umap);
5060 upma = isl_union_pw_multi_aff_empty(space);
5061 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5062 upma = isl_union_pw_multi_aff_free(upma);
5063 isl_union_map_free(umap);
5065 return upma;
5068 /* Try and create an isl_union_pw_multi_aff that is equivalent
5069 * to the given isl_union_set.
5070 * The isl_union_set is required to be a singleton in each space.
5071 * Otherwise, an error is produced.
5073 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5074 __isl_take isl_union_set *uset)
5076 return isl_union_pw_multi_aff_from_union_map(uset);
5079 /* Return the piecewise affine expression "set ? 1 : 0".
5081 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5083 isl_pw_aff *pa;
5084 isl_space *space = isl_set_get_space(set);
5085 isl_local_space *ls = isl_local_space_from_space(space);
5086 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5087 isl_aff *one = isl_aff_zero_on_domain(ls);
5089 one = isl_aff_add_constant_si(one, 1);
5090 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5091 set = isl_set_complement(set);
5092 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5094 return pa;
5097 /* Plug in "subs" for dimension "type", "pos" of "aff".
5099 * Let i be the dimension to replace and let "subs" be of the form
5101 * f/d
5103 * and "aff" of the form
5105 * (a i + g)/m
5107 * The result is
5109 * (a f + d g')/(m d)
5111 * where g' is the result of plugging in "subs" in each of the integer
5112 * divisions in g.
5114 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5115 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5117 isl_ctx *ctx;
5118 isl_int v;
5120 aff = isl_aff_cow(aff);
5121 if (!aff || !subs)
5122 return isl_aff_free(aff);
5124 ctx = isl_aff_get_ctx(aff);
5125 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5126 isl_die(ctx, isl_error_invalid,
5127 "spaces don't match", return isl_aff_free(aff));
5128 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5129 isl_die(ctx, isl_error_unsupported,
5130 "cannot handle divs yet", return isl_aff_free(aff));
5132 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5133 if (!aff->ls)
5134 return isl_aff_free(aff);
5136 aff->v = isl_vec_cow(aff->v);
5137 if (!aff->v)
5138 return isl_aff_free(aff);
5140 pos += isl_local_space_offset(aff->ls, type);
5142 isl_int_init(v);
5143 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5144 aff->v->size, subs->v->size, v);
5145 isl_int_clear(v);
5147 return aff;
5150 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5151 * expressions in "maff".
5153 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5154 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5155 __isl_keep isl_aff *subs)
5157 int i;
5159 maff = isl_multi_aff_cow(maff);
5160 if (!maff || !subs)
5161 return isl_multi_aff_free(maff);
5163 if (type == isl_dim_in)
5164 type = isl_dim_set;
5166 for (i = 0; i < maff->n; ++i) {
5167 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5168 if (!maff->p[i])
5169 return isl_multi_aff_free(maff);
5172 return maff;
5175 /* Plug in "subs" for dimension "type", "pos" of "pma".
5177 * pma is of the form
5179 * A_i(v) -> M_i(v)
5181 * while subs is of the form
5183 * v' = B_j(v) -> S_j
5185 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5186 * has a contribution in the result, in particular
5188 * C_ij(S_j) -> M_i(S_j)
5190 * Note that plugging in S_j in C_ij may also result in an empty set
5191 * and this contribution should simply be discarded.
5193 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5194 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5195 __isl_keep isl_pw_aff *subs)
5197 int i, j, n;
5198 isl_pw_multi_aff *res;
5200 if (!pma || !subs)
5201 return isl_pw_multi_aff_free(pma);
5203 n = pma->n * subs->n;
5204 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5206 for (i = 0; i < pma->n; ++i) {
5207 for (j = 0; j < subs->n; ++j) {
5208 isl_set *common;
5209 isl_multi_aff *res_ij;
5210 int empty;
5212 common = isl_set_intersect(
5213 isl_set_copy(pma->p[i].set),
5214 isl_set_copy(subs->p[j].set));
5215 common = isl_set_substitute(common,
5216 type, pos, subs->p[j].aff);
5217 empty = isl_set_plain_is_empty(common);
5218 if (empty < 0 || empty) {
5219 isl_set_free(common);
5220 if (empty < 0)
5221 goto error;
5222 continue;
5225 res_ij = isl_multi_aff_substitute(
5226 isl_multi_aff_copy(pma->p[i].maff),
5227 type, pos, subs->p[j].aff);
5229 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5233 isl_pw_multi_aff_free(pma);
5234 return res;
5235 error:
5236 isl_pw_multi_aff_free(pma);
5237 isl_pw_multi_aff_free(res);
5238 return NULL;
5241 /* Compute the preimage of a range of dimensions in the affine expression "src"
5242 * under "ma" and put the result in "dst". The number of dimensions in "src"
5243 * that precede the range is given by "n_before". The number of dimensions
5244 * in the range is given by the number of output dimensions of "ma".
5245 * The number of dimensions that follow the range is given by "n_after".
5246 * If "has_denom" is set (to one),
5247 * then "src" and "dst" have an extra initial denominator.
5248 * "n_div_ma" is the number of existentials in "ma"
5249 * "n_div_bset" is the number of existentials in "src"
5250 * The resulting "dst" (which is assumed to have been allocated by
5251 * the caller) contains coefficients for both sets of existentials,
5252 * first those in "ma" and then those in "src".
5253 * f, c1, c2 and g are temporary objects that have been initialized
5254 * by the caller.
5256 * Let src represent the expression
5258 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5260 * and let ma represent the expressions
5262 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5264 * We start out with the following expression for dst:
5266 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5268 * with the multiplication factor f initially equal to 1
5269 * and f \sum_i b_i v_i kept separately.
5270 * For each x_i that we substitute, we multiply the numerator
5271 * (and denominator) of dst by c_1 = m_i and add the numerator
5272 * of the x_i expression multiplied by c_2 = f b_i,
5273 * after removing the common factors of c_1 and c_2.
5274 * The multiplication factor f also needs to be multiplied by c_1
5275 * for the next x_j, j > i.
5277 void isl_seq_preimage(isl_int *dst, isl_int *src,
5278 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5279 int n_div_ma, int n_div_bmap,
5280 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5282 int i;
5283 int n_param, n_in, n_out;
5284 int o_dst, o_src;
5286 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5287 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5288 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5290 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5291 o_dst = o_src = has_denom + 1 + n_param + n_before;
5292 isl_seq_clr(dst + o_dst, n_in);
5293 o_dst += n_in;
5294 o_src += n_out;
5295 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5296 o_dst += n_after;
5297 o_src += n_after;
5298 isl_seq_clr(dst + o_dst, n_div_ma);
5299 o_dst += n_div_ma;
5300 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5302 isl_int_set_si(f, 1);
5304 for (i = 0; i < n_out; ++i) {
5305 int offset = has_denom + 1 + n_param + n_before + i;
5307 if (isl_int_is_zero(src[offset]))
5308 continue;
5309 isl_int_set(c1, ma->p[i]->v->el[0]);
5310 isl_int_mul(c2, f, src[offset]);
5311 isl_int_gcd(g, c1, c2);
5312 isl_int_divexact(c1, c1, g);
5313 isl_int_divexact(c2, c2, g);
5315 isl_int_mul(f, f, c1);
5316 o_dst = has_denom;
5317 o_src = 1;
5318 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5319 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5320 o_dst += 1 + n_param;
5321 o_src += 1 + n_param;
5322 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5323 o_dst += n_before;
5324 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5325 c2, ma->p[i]->v->el + o_src, n_in);
5326 o_dst += n_in;
5327 o_src += n_in;
5328 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5329 o_dst += n_after;
5330 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5331 c2, ma->p[i]->v->el + o_src, n_div_ma);
5332 o_dst += n_div_ma;
5333 o_src += n_div_ma;
5334 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5335 if (has_denom)
5336 isl_int_mul(dst[0], dst[0], c1);
5340 /* Compute the pullback of "aff" by the function represented by "ma".
5341 * In other words, plug in "ma" in "aff". The result is an affine expression
5342 * defined over the domain space of "ma".
5344 * If "aff" is represented by
5346 * (a(p) + b x + c(divs))/d
5348 * and ma is represented by
5350 * x = D(p) + F(y) + G(divs')
5352 * then the result is
5354 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5356 * The divs in the local space of the input are similarly adjusted
5357 * through a call to isl_local_space_preimage_multi_aff.
5359 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5360 __isl_take isl_multi_aff *ma)
5362 isl_aff *res = NULL;
5363 isl_local_space *ls;
5364 int n_div_aff, n_div_ma;
5365 isl_int f, c1, c2, g;
5367 ma = isl_multi_aff_align_divs(ma);
5368 if (!aff || !ma)
5369 goto error;
5371 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5372 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5374 ls = isl_aff_get_domain_local_space(aff);
5375 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5376 res = isl_aff_alloc(ls);
5377 if (!res)
5378 goto error;
5380 isl_int_init(f);
5381 isl_int_init(c1);
5382 isl_int_init(c2);
5383 isl_int_init(g);
5385 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5386 f, c1, c2, g, 1);
5388 isl_int_clear(f);
5389 isl_int_clear(c1);
5390 isl_int_clear(c2);
5391 isl_int_clear(g);
5393 isl_aff_free(aff);
5394 isl_multi_aff_free(ma);
5395 res = isl_aff_normalize(res);
5396 return res;
5397 error:
5398 isl_aff_free(aff);
5399 isl_multi_aff_free(ma);
5400 isl_aff_free(res);
5401 return NULL;
5404 /* Compute the pullback of "aff1" by the function represented by "aff2".
5405 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5406 * defined over the domain space of "aff1".
5408 * The domain of "aff1" should match the range of "aff2", which means
5409 * that it should be single-dimensional.
5411 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5412 __isl_take isl_aff *aff2)
5414 isl_multi_aff *ma;
5416 ma = isl_multi_aff_from_aff(aff2);
5417 return isl_aff_pullback_multi_aff(aff1, ma);
5420 /* Compute the pullback of "ma1" by the function represented by "ma2".
5421 * In other words, plug in "ma2" in "ma1".
5423 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5425 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5426 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5428 int i;
5429 isl_space *space = NULL;
5431 ma2 = isl_multi_aff_align_divs(ma2);
5432 ma1 = isl_multi_aff_cow(ma1);
5433 if (!ma1 || !ma2)
5434 goto error;
5436 space = isl_space_join(isl_multi_aff_get_space(ma2),
5437 isl_multi_aff_get_space(ma1));
5439 for (i = 0; i < ma1->n; ++i) {
5440 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5441 isl_multi_aff_copy(ma2));
5442 if (!ma1->p[i])
5443 goto error;
5446 ma1 = isl_multi_aff_reset_space(ma1, space);
5447 isl_multi_aff_free(ma2);
5448 return ma1;
5449 error:
5450 isl_space_free(space);
5451 isl_multi_aff_free(ma2);
5452 isl_multi_aff_free(ma1);
5453 return NULL;
5456 /* Compute the pullback of "ma1" by the function represented by "ma2".
5457 * In other words, plug in "ma2" in "ma1".
5459 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5460 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5462 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5463 &isl_multi_aff_pullback_multi_aff_aligned);
5466 /* Extend the local space of "dst" to include the divs
5467 * in the local space of "src".
5469 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5470 __isl_keep isl_aff *src)
5472 isl_ctx *ctx;
5473 int *exp1 = NULL;
5474 int *exp2 = NULL;
5475 isl_mat *div;
5477 if (!src || !dst)
5478 return isl_aff_free(dst);
5480 ctx = isl_aff_get_ctx(src);
5481 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5482 isl_die(ctx, isl_error_invalid,
5483 "spaces don't match", goto error);
5485 if (src->ls->div->n_row == 0)
5486 return dst;
5488 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5489 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5490 if (!exp1 || (dst->ls->div->n_row && !exp2))
5491 goto error;
5493 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5494 dst = isl_aff_expand_divs(dst, div, exp2);
5495 free(exp1);
5496 free(exp2);
5498 return dst;
5499 error:
5500 free(exp1);
5501 free(exp2);
5502 return isl_aff_free(dst);
5505 /* Adjust the local spaces of the affine expressions in "maff"
5506 * such that they all have the save divs.
5508 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5509 __isl_take isl_multi_aff *maff)
5511 int i;
5513 if (!maff)
5514 return NULL;
5515 if (maff->n == 0)
5516 return maff;
5517 maff = isl_multi_aff_cow(maff);
5518 if (!maff)
5519 return NULL;
5521 for (i = 1; i < maff->n; ++i)
5522 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5523 for (i = 1; i < maff->n; ++i) {
5524 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5525 if (!maff->p[i])
5526 return isl_multi_aff_free(maff);
5529 return maff;
5532 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5534 aff = isl_aff_cow(aff);
5535 if (!aff)
5536 return NULL;
5538 aff->ls = isl_local_space_lift(aff->ls);
5539 if (!aff->ls)
5540 return isl_aff_free(aff);
5542 return aff;
5545 /* Lift "maff" to a space with extra dimensions such that the result
5546 * has no more existentially quantified variables.
5547 * If "ls" is not NULL, then *ls is assigned the local space that lies
5548 * at the basis of the lifting applied to "maff".
5550 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5551 __isl_give isl_local_space **ls)
5553 int i;
5554 isl_space *space;
5555 unsigned n_div;
5557 if (ls)
5558 *ls = NULL;
5560 if (!maff)
5561 return NULL;
5563 if (maff->n == 0) {
5564 if (ls) {
5565 isl_space *space = isl_multi_aff_get_domain_space(maff);
5566 *ls = isl_local_space_from_space(space);
5567 if (!*ls)
5568 return isl_multi_aff_free(maff);
5570 return maff;
5573 maff = isl_multi_aff_cow(maff);
5574 maff = isl_multi_aff_align_divs(maff);
5575 if (!maff)
5576 return NULL;
5578 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5579 space = isl_multi_aff_get_space(maff);
5580 space = isl_space_lift(isl_space_domain(space), n_div);
5581 space = isl_space_extend_domain_with_range(space,
5582 isl_multi_aff_get_space(maff));
5583 if (!space)
5584 return isl_multi_aff_free(maff);
5585 isl_space_free(maff->space);
5586 maff->space = space;
5588 if (ls) {
5589 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5590 if (!*ls)
5591 return isl_multi_aff_free(maff);
5594 for (i = 0; i < maff->n; ++i) {
5595 maff->p[i] = isl_aff_lift(maff->p[i]);
5596 if (!maff->p[i])
5597 goto error;
5600 return maff;
5601 error:
5602 if (ls)
5603 isl_local_space_free(*ls);
5604 return isl_multi_aff_free(maff);
5608 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5610 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5611 __isl_keep isl_pw_multi_aff *pma, int pos)
5613 int i;
5614 int n_out;
5615 isl_space *space;
5616 isl_pw_aff *pa;
5618 if (!pma)
5619 return NULL;
5621 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5622 if (pos < 0 || pos >= n_out)
5623 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5624 "index out of bounds", return NULL);
5626 space = isl_pw_multi_aff_get_space(pma);
5627 space = isl_space_drop_dims(space, isl_dim_out,
5628 pos + 1, n_out - pos - 1);
5629 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5631 pa = isl_pw_aff_alloc_size(space, pma->n);
5632 for (i = 0; i < pma->n; ++i) {
5633 isl_aff *aff;
5634 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5635 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5638 return pa;
5641 /* Return an isl_pw_multi_aff with the given "set" as domain and
5642 * an unnamed zero-dimensional range.
5644 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5645 __isl_take isl_set *set)
5647 isl_multi_aff *ma;
5648 isl_space *space;
5650 space = isl_set_get_space(set);
5651 space = isl_space_from_domain(space);
5652 ma = isl_multi_aff_zero(space);
5653 return isl_pw_multi_aff_alloc(set, ma);
5656 /* Add an isl_pw_multi_aff with the given "set" as domain and
5657 * an unnamed zero-dimensional range to *user.
5659 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5661 isl_union_pw_multi_aff **upma = user;
5662 isl_pw_multi_aff *pma;
5664 pma = isl_pw_multi_aff_from_domain(set);
5665 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5667 return 0;
5670 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5671 * an unnamed zero-dimensional range.
5673 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5674 __isl_take isl_union_set *uset)
5676 isl_space *space;
5677 isl_union_pw_multi_aff *upma;
5679 if (!uset)
5680 return NULL;
5682 space = isl_union_set_get_space(uset);
5683 upma = isl_union_pw_multi_aff_empty(space);
5685 if (isl_union_set_foreach_set(uset,
5686 &add_pw_multi_aff_from_domain, &upma) < 0)
5687 goto error;
5689 isl_union_set_free(uset);
5690 return upma;
5691 error:
5692 isl_union_set_free(uset);
5693 isl_union_pw_multi_aff_free(upma);
5694 return NULL;
5697 /* Convert "pma" to an isl_map and add it to *umap.
5699 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5701 isl_union_map **umap = user;
5702 isl_map *map;
5704 map = isl_map_from_pw_multi_aff(pma);
5705 *umap = isl_union_map_add_map(*umap, map);
5707 return 0;
5710 /* Construct a union map mapping the domain of the union
5711 * piecewise multi-affine expression to its range, with each dimension
5712 * in the range equated to the corresponding affine expression on its cell.
5714 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5715 __isl_take isl_union_pw_multi_aff *upma)
5717 isl_space *space;
5718 isl_union_map *umap;
5720 if (!upma)
5721 return NULL;
5723 space = isl_union_pw_multi_aff_get_space(upma);
5724 umap = isl_union_map_empty(space);
5726 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5727 &map_from_pw_multi_aff, &umap) < 0)
5728 goto error;
5730 isl_union_pw_multi_aff_free(upma);
5731 return umap;
5732 error:
5733 isl_union_pw_multi_aff_free(upma);
5734 isl_union_map_free(umap);
5735 return NULL;
5738 /* Local data for bin_entry and the callback "fn".
5740 struct isl_union_pw_multi_aff_bin_data {
5741 isl_union_pw_multi_aff *upma2;
5742 isl_union_pw_multi_aff *res;
5743 isl_pw_multi_aff *pma;
5744 int (*fn)(void **entry, void *user);
5747 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5748 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5750 static int bin_entry(void **entry, void *user)
5752 struct isl_union_pw_multi_aff_bin_data *data = user;
5753 isl_pw_multi_aff *pma = *entry;
5755 data->pma = pma;
5756 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5757 data->fn, data) < 0)
5758 return -1;
5760 return 0;
5763 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5764 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5765 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5766 * as *entry. The callback should adjust data->res if desired.
5768 static __isl_give isl_union_pw_multi_aff *bin_op(
5769 __isl_take isl_union_pw_multi_aff *upma1,
5770 __isl_take isl_union_pw_multi_aff *upma2,
5771 int (*fn)(void **entry, void *user))
5773 isl_space *space;
5774 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5776 space = isl_union_pw_multi_aff_get_space(upma2);
5777 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5778 space = isl_union_pw_multi_aff_get_space(upma1);
5779 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5781 if (!upma1 || !upma2)
5782 goto error;
5784 data.upma2 = upma2;
5785 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5786 upma1->table.n);
5787 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5788 &bin_entry, &data) < 0)
5789 goto error;
5791 isl_union_pw_multi_aff_free(upma1);
5792 isl_union_pw_multi_aff_free(upma2);
5793 return data.res;
5794 error:
5795 isl_union_pw_multi_aff_free(upma1);
5796 isl_union_pw_multi_aff_free(upma2);
5797 isl_union_pw_multi_aff_free(data.res);
5798 return NULL;
5801 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5802 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5804 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5805 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5807 isl_space *space;
5809 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5810 isl_pw_multi_aff_get_space(pma2));
5811 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5812 &isl_multi_aff_range_product);
5815 /* Given two isl_pw_multi_affs A -> B and C -> D,
5816 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5818 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5819 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5821 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5822 &pw_multi_aff_range_product);
5825 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5826 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5828 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5829 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5831 isl_space *space;
5833 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5834 isl_pw_multi_aff_get_space(pma2));
5835 space = isl_space_flatten_range(space);
5836 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5837 &isl_multi_aff_flat_range_product);
5840 /* Given two isl_pw_multi_affs A -> B and C -> D,
5841 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5843 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5844 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5846 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5847 &pw_multi_aff_flat_range_product);
5850 /* If data->pma and *entry have the same domain space, then compute
5851 * their flat range product and the result to data->res.
5853 static int flat_range_product_entry(void **entry, void *user)
5855 struct isl_union_pw_multi_aff_bin_data *data = user;
5856 isl_pw_multi_aff *pma2 = *entry;
5858 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5859 pma2->dim, isl_dim_in))
5860 return 0;
5862 pma2 = isl_pw_multi_aff_flat_range_product(
5863 isl_pw_multi_aff_copy(data->pma),
5864 isl_pw_multi_aff_copy(pma2));
5866 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5868 return 0;
5871 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5872 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5874 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5875 __isl_take isl_union_pw_multi_aff *upma1,
5876 __isl_take isl_union_pw_multi_aff *upma2)
5878 return bin_op(upma1, upma2, &flat_range_product_entry);
5881 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5882 * The parameters are assumed to have been aligned.
5884 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5885 * except that it works on two different isl_pw_* types.
5887 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5888 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5889 __isl_take isl_pw_aff *pa)
5891 int i, j, n;
5892 isl_pw_multi_aff *res = NULL;
5894 if (!pma || !pa)
5895 goto error;
5897 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5898 pa->dim, isl_dim_in))
5899 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5900 "domains don't match", goto error);
5901 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5902 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5903 "index out of bounds", goto error);
5905 n = pma->n * pa->n;
5906 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5908 for (i = 0; i < pma->n; ++i) {
5909 for (j = 0; j < pa->n; ++j) {
5910 isl_set *common;
5911 isl_multi_aff *res_ij;
5912 int empty;
5914 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5915 isl_set_copy(pa->p[j].set));
5916 empty = isl_set_plain_is_empty(common);
5917 if (empty < 0 || empty) {
5918 isl_set_free(common);
5919 if (empty < 0)
5920 goto error;
5921 continue;
5924 res_ij = isl_multi_aff_set_aff(
5925 isl_multi_aff_copy(pma->p[i].maff), pos,
5926 isl_aff_copy(pa->p[j].aff));
5927 res_ij = isl_multi_aff_gist(res_ij,
5928 isl_set_copy(common));
5930 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5934 isl_pw_multi_aff_free(pma);
5935 isl_pw_aff_free(pa);
5936 return res;
5937 error:
5938 isl_pw_multi_aff_free(pma);
5939 isl_pw_aff_free(pa);
5940 return isl_pw_multi_aff_free(res);
5943 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5945 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5946 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5947 __isl_take isl_pw_aff *pa)
5949 if (!pma || !pa)
5950 goto error;
5951 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5952 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5953 if (!isl_space_has_named_params(pma->dim) ||
5954 !isl_space_has_named_params(pa->dim))
5955 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5956 "unaligned unnamed parameters", goto error);
5957 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5958 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5959 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5960 error:
5961 isl_pw_multi_aff_free(pma);
5962 isl_pw_aff_free(pa);
5963 return NULL;
5966 /* Do the parameters of "pa" match those of "space"?
5968 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5969 __isl_keep isl_space *space)
5971 isl_space *pa_space;
5972 int match;
5974 if (!pa || !space)
5975 return -1;
5977 pa_space = isl_pw_aff_get_space(pa);
5979 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5981 isl_space_free(pa_space);
5982 return match;
5985 /* Check that the domain space of "pa" matches "space".
5987 * Return 0 on success and -1 on error.
5989 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5990 __isl_keep isl_space *space)
5992 isl_space *pa_space;
5993 int match;
5995 if (!pa || !space)
5996 return -1;
5998 pa_space = isl_pw_aff_get_space(pa);
6000 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6001 if (match < 0)
6002 goto error;
6003 if (!match)
6004 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6005 "parameters don't match", goto error);
6006 match = isl_space_tuple_is_equal(space, isl_dim_in,
6007 pa_space, isl_dim_in);
6008 if (match < 0)
6009 goto error;
6010 if (!match)
6011 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6012 "domains don't match", goto error);
6013 isl_space_free(pa_space);
6014 return 0;
6015 error:
6016 isl_space_free(pa_space);
6017 return -1;
6020 #undef BASE
6021 #define BASE pw_aff
6022 #undef DOMBASE
6023 #define DOMBASE set
6025 #include <isl_multi_templ.c>
6026 #include <isl_multi_apply_set.c>
6027 #include <isl_multi_gist.c>
6028 #include <isl_multi_intersect.c>
6030 /* Scale the elements of "pma" by the corresponding elements of "mv".
6032 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6033 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6035 int i;
6037 pma = isl_pw_multi_aff_cow(pma);
6038 if (!pma || !mv)
6039 goto error;
6040 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6041 mv->space, isl_dim_set))
6042 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6043 "spaces don't match", goto error);
6044 if (!isl_space_match(pma->dim, isl_dim_param,
6045 mv->space, isl_dim_param)) {
6046 pma = isl_pw_multi_aff_align_params(pma,
6047 isl_multi_val_get_space(mv));
6048 mv = isl_multi_val_align_params(mv,
6049 isl_pw_multi_aff_get_space(pma));
6050 if (!pma || !mv)
6051 goto error;
6054 for (i = 0; i < pma->n; ++i) {
6055 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6056 isl_multi_val_copy(mv));
6057 if (!pma->p[i].maff)
6058 goto error;
6061 isl_multi_val_free(mv);
6062 return pma;
6063 error:
6064 isl_multi_val_free(mv);
6065 isl_pw_multi_aff_free(pma);
6066 return NULL;
6069 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
6070 * mv contains the mv argument.
6071 * res collects the results.
6073 struct isl_union_pw_multi_aff_scale_multi_val_data {
6074 isl_multi_val *mv;
6075 isl_union_pw_multi_aff *res;
6078 /* This function is called for each entry of an isl_union_pw_multi_aff.
6079 * If the space of the entry matches that of data->mv,
6080 * then apply isl_pw_multi_aff_scale_multi_val and add the result
6081 * to data->res.
6083 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
6085 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
6086 isl_pw_multi_aff *pma = *entry;
6088 if (!pma)
6089 return -1;
6090 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6091 data->mv->space, isl_dim_set))
6092 return 0;
6094 pma = isl_pw_multi_aff_copy(pma);
6095 pma = isl_pw_multi_aff_scale_multi_val(pma,
6096 isl_multi_val_copy(data->mv));
6097 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6098 if (!data->res)
6099 return -1;
6101 return 0;
6104 /* Scale the elements of "upma" by the corresponding elements of "mv",
6105 * for those entries that match the space of "mv".
6107 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6108 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6110 struct isl_union_pw_multi_aff_scale_multi_val_data data;
6112 upma = isl_union_pw_multi_aff_align_params(upma,
6113 isl_multi_val_get_space(mv));
6114 mv = isl_multi_val_align_params(mv,
6115 isl_union_pw_multi_aff_get_space(upma));
6116 if (!upma || !mv)
6117 goto error;
6119 data.mv = mv;
6120 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
6121 upma->table.n);
6122 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
6123 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
6124 goto error;
6126 isl_multi_val_free(mv);
6127 isl_union_pw_multi_aff_free(upma);
6128 return data.res;
6129 error:
6130 isl_multi_val_free(mv);
6131 isl_union_pw_multi_aff_free(upma);
6132 return NULL;
6135 /* Construct and return a piecewise multi affine expression
6136 * in the given space with value zero in each of the output dimensions and
6137 * a universe domain.
6139 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6141 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6144 /* Construct and return a piecewise multi affine expression
6145 * that is equal to the given piecewise affine expression.
6147 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6148 __isl_take isl_pw_aff *pa)
6150 int i;
6151 isl_space *space;
6152 isl_pw_multi_aff *pma;
6154 if (!pa)
6155 return NULL;
6157 space = isl_pw_aff_get_space(pa);
6158 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6160 for (i = 0; i < pa->n; ++i) {
6161 isl_set *set;
6162 isl_multi_aff *ma;
6164 set = isl_set_copy(pa->p[i].set);
6165 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6166 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6169 isl_pw_aff_free(pa);
6170 return pma;
6173 /* Construct a set or map mapping the shared (parameter) domain
6174 * of the piecewise affine expressions to the range of "mpa"
6175 * with each dimension in the range equated to the
6176 * corresponding piecewise affine expression.
6178 static __isl_give isl_map *map_from_multi_pw_aff(
6179 __isl_take isl_multi_pw_aff *mpa)
6181 int i;
6182 isl_space *space;
6183 isl_map *map;
6185 if (!mpa)
6186 return NULL;
6188 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6189 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6190 "invalid space", goto error);
6192 space = isl_multi_pw_aff_get_domain_space(mpa);
6193 map = isl_map_universe(isl_space_from_domain(space));
6195 for (i = 0; i < mpa->n; ++i) {
6196 isl_pw_aff *pa;
6197 isl_map *map_i;
6199 pa = isl_pw_aff_copy(mpa->p[i]);
6200 map_i = map_from_pw_aff(pa);
6202 map = isl_map_flat_range_product(map, map_i);
6205 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6207 isl_multi_pw_aff_free(mpa);
6208 return map;
6209 error:
6210 isl_multi_pw_aff_free(mpa);
6211 return NULL;
6214 /* Construct a map mapping the shared domain
6215 * of the piecewise affine expressions to the range of "mpa"
6216 * with each dimension in the range equated to the
6217 * corresponding piecewise affine expression.
6219 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6221 if (!mpa)
6222 return NULL;
6223 if (isl_space_is_set(mpa->space))
6224 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6225 "space of input is not a map", goto error);
6227 return map_from_multi_pw_aff(mpa);
6228 error:
6229 isl_multi_pw_aff_free(mpa);
6230 return NULL;
6233 /* Construct a set mapping the shared parameter domain
6234 * of the piecewise affine expressions to the space of "mpa"
6235 * with each dimension in the range equated to the
6236 * corresponding piecewise affine expression.
6238 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6240 if (!mpa)
6241 return NULL;
6242 if (!isl_space_is_set(mpa->space))
6243 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6244 "space of input is not a set", goto error);
6246 return map_from_multi_pw_aff(mpa);
6247 error:
6248 isl_multi_pw_aff_free(mpa);
6249 return NULL;
6252 /* Construct and return a piecewise multi affine expression
6253 * that is equal to the given multi piecewise affine expression
6254 * on the shared domain of the piecewise affine expressions.
6256 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6257 __isl_take isl_multi_pw_aff *mpa)
6259 int i;
6260 isl_space *space;
6261 isl_pw_aff *pa;
6262 isl_pw_multi_aff *pma;
6264 if (!mpa)
6265 return NULL;
6267 space = isl_multi_pw_aff_get_space(mpa);
6269 if (mpa->n == 0) {
6270 isl_multi_pw_aff_free(mpa);
6271 return isl_pw_multi_aff_zero(space);
6274 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6275 pma = isl_pw_multi_aff_from_pw_aff(pa);
6277 for (i = 1; i < mpa->n; ++i) {
6278 isl_pw_multi_aff *pma_i;
6280 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6281 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6282 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6285 pma = isl_pw_multi_aff_reset_space(pma, space);
6287 isl_multi_pw_aff_free(mpa);
6288 return pma;
6291 /* Construct and return a multi piecewise affine expression
6292 * that is equal to the given multi affine expression.
6294 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6295 __isl_take isl_multi_aff *ma)
6297 int i, n;
6298 isl_multi_pw_aff *mpa;
6300 if (!ma)
6301 return NULL;
6303 n = isl_multi_aff_dim(ma, isl_dim_out);
6304 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6306 for (i = 0; i < n; ++i) {
6307 isl_pw_aff *pa;
6309 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6310 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6313 isl_multi_aff_free(ma);
6314 return mpa;
6317 /* Construct and return a multi piecewise affine expression
6318 * that is equal to the given piecewise multi affine expression.
6320 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6321 __isl_take isl_pw_multi_aff *pma)
6323 int i, n;
6324 isl_space *space;
6325 isl_multi_pw_aff *mpa;
6327 if (!pma)
6328 return NULL;
6330 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6331 space = isl_pw_multi_aff_get_space(pma);
6332 mpa = isl_multi_pw_aff_alloc(space);
6334 for (i = 0; i < n; ++i) {
6335 isl_pw_aff *pa;
6337 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6338 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6341 isl_pw_multi_aff_free(pma);
6342 return mpa;
6345 /* Do "pa1" and "pa2" represent the same function?
6347 * We first check if they are obviously equal.
6348 * If not, we convert them to maps and check if those are equal.
6350 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6352 int equal;
6353 isl_map *map1, *map2;
6355 if (!pa1 || !pa2)
6356 return -1;
6358 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6359 if (equal < 0 || equal)
6360 return equal;
6362 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6363 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6364 equal = isl_map_is_equal(map1, map2);
6365 isl_map_free(map1);
6366 isl_map_free(map2);
6368 return equal;
6371 /* Do "mpa1" and "mpa2" represent the same function?
6373 * Note that we cannot convert the entire isl_multi_pw_aff
6374 * to a map because the domains of the piecewise affine expressions
6375 * may not be the same.
6377 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6378 __isl_keep isl_multi_pw_aff *mpa2)
6380 int i;
6381 int equal;
6383 if (!mpa1 || !mpa2)
6384 return -1;
6386 if (!isl_space_match(mpa1->space, isl_dim_param,
6387 mpa2->space, isl_dim_param)) {
6388 if (!isl_space_has_named_params(mpa1->space))
6389 return 0;
6390 if (!isl_space_has_named_params(mpa2->space))
6391 return 0;
6392 mpa1 = isl_multi_pw_aff_copy(mpa1);
6393 mpa2 = isl_multi_pw_aff_copy(mpa2);
6394 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6395 isl_multi_pw_aff_get_space(mpa2));
6396 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6397 isl_multi_pw_aff_get_space(mpa1));
6398 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6399 isl_multi_pw_aff_free(mpa1);
6400 isl_multi_pw_aff_free(mpa2);
6401 return equal;
6404 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6405 if (equal < 0 || !equal)
6406 return equal;
6408 for (i = 0; i < mpa1->n; ++i) {
6409 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6410 if (equal < 0 || !equal)
6411 return equal;
6414 return 1;
6417 /* Coalesce the elements of "mpa".
6419 * Note that such coalescing does not change the meaning of "mpa"
6420 * so there is no need to cow. We do need to be careful not to
6421 * destroy any other copies of "mpa" in case of failure.
6423 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6424 __isl_take isl_multi_pw_aff *mpa)
6426 int i;
6428 if (!mpa)
6429 return NULL;
6431 for (i = 0; i < mpa->n; ++i) {
6432 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6433 pa = isl_pw_aff_coalesce(pa);
6434 if (!pa)
6435 return isl_multi_pw_aff_free(mpa);
6436 isl_pw_aff_free(mpa->p[i]);
6437 mpa->p[i] = pa;
6440 return mpa;
6443 /* Compute the pullback of "mpa" by the function represented by "ma".
6444 * In other words, plug in "ma" in "mpa".
6446 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6448 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6449 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6451 int i;
6452 isl_space *space = NULL;
6454 mpa = isl_multi_pw_aff_cow(mpa);
6455 if (!mpa || !ma)
6456 goto error;
6458 space = isl_space_join(isl_multi_aff_get_space(ma),
6459 isl_multi_pw_aff_get_space(mpa));
6460 if (!space)
6461 goto error;
6463 for (i = 0; i < mpa->n; ++i) {
6464 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6465 isl_multi_aff_copy(ma));
6466 if (!mpa->p[i])
6467 goto error;
6470 isl_multi_aff_free(ma);
6471 isl_space_free(mpa->space);
6472 mpa->space = space;
6473 return mpa;
6474 error:
6475 isl_space_free(space);
6476 isl_multi_pw_aff_free(mpa);
6477 isl_multi_aff_free(ma);
6478 return NULL;
6481 /* Compute the pullback of "mpa" by the function represented by "ma".
6482 * In other words, plug in "ma" in "mpa".
6484 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6485 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6487 if (!mpa || !ma)
6488 goto error;
6489 if (isl_space_match(mpa->space, isl_dim_param,
6490 ma->space, isl_dim_param))
6491 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6492 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6493 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6494 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6495 error:
6496 isl_multi_pw_aff_free(mpa);
6497 isl_multi_aff_free(ma);
6498 return NULL;
6501 /* Compute the pullback of "mpa" by the function represented by "pma".
6502 * In other words, plug in "pma" in "mpa".
6504 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6506 static __isl_give isl_multi_pw_aff *
6507 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6508 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6510 int i;
6511 isl_space *space = NULL;
6513 mpa = isl_multi_pw_aff_cow(mpa);
6514 if (!mpa || !pma)
6515 goto error;
6517 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6518 isl_multi_pw_aff_get_space(mpa));
6520 for (i = 0; i < mpa->n; ++i) {
6521 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6522 isl_pw_multi_aff_copy(pma));
6523 if (!mpa->p[i])
6524 goto error;
6527 isl_pw_multi_aff_free(pma);
6528 isl_space_free(mpa->space);
6529 mpa->space = space;
6530 return mpa;
6531 error:
6532 isl_space_free(space);
6533 isl_multi_pw_aff_free(mpa);
6534 isl_pw_multi_aff_free(pma);
6535 return NULL;
6538 /* Compute the pullback of "mpa" by the function represented by "pma".
6539 * In other words, plug in "pma" in "mpa".
6541 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6542 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6544 if (!mpa || !pma)
6545 goto error;
6546 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6547 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6548 mpa = isl_multi_pw_aff_align_params(mpa,
6549 isl_pw_multi_aff_get_space(pma));
6550 pma = isl_pw_multi_aff_align_params(pma,
6551 isl_multi_pw_aff_get_space(mpa));
6552 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6553 error:
6554 isl_multi_pw_aff_free(mpa);
6555 isl_pw_multi_aff_free(pma);
6556 return NULL;
6559 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6560 * with the domain of "aff". The domain of the result is the same
6561 * as that of "mpa".
6562 * "mpa" and "aff" are assumed to have been aligned.
6564 * We first extract the parametric constant from "aff", defined
6565 * over the correct domain.
6566 * Then we add the appropriate combinations of the members of "mpa".
6567 * Finally, we add the integer divisions through recursive calls.
6569 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6570 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6572 int i, n_param, n_in, n_div;
6573 isl_space *space;
6574 isl_val *v;
6575 isl_pw_aff *pa;
6576 isl_aff *tmp;
6578 n_param = isl_aff_dim(aff, isl_dim_param);
6579 n_in = isl_aff_dim(aff, isl_dim_in);
6580 n_div = isl_aff_dim(aff, isl_dim_div);
6582 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6583 tmp = isl_aff_copy(aff);
6584 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6585 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6586 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6587 isl_space_dim(space, isl_dim_set));
6588 tmp = isl_aff_reset_domain_space(tmp, space);
6589 pa = isl_pw_aff_from_aff(tmp);
6591 for (i = 0; i < n_in; ++i) {
6592 isl_pw_aff *pa_i;
6594 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6595 continue;
6596 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6597 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6598 pa_i = isl_pw_aff_scale_val(pa_i, v);
6599 pa = isl_pw_aff_add(pa, pa_i);
6602 for (i = 0; i < n_div; ++i) {
6603 isl_aff *div;
6604 isl_pw_aff *pa_i;
6606 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6607 continue;
6608 div = isl_aff_get_div(aff, i);
6609 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6610 isl_multi_pw_aff_copy(mpa), div);
6611 pa_i = isl_pw_aff_floor(pa_i);
6612 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6613 pa_i = isl_pw_aff_scale_val(pa_i, v);
6614 pa = isl_pw_aff_add(pa, pa_i);
6617 isl_multi_pw_aff_free(mpa);
6618 isl_aff_free(aff);
6620 return pa;
6623 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6624 * with the domain of "aff". The domain of the result is the same
6625 * as that of "mpa".
6627 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6628 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6630 if (!aff || !mpa)
6631 goto error;
6632 if (isl_space_match(aff->ls->dim, isl_dim_param,
6633 mpa->space, isl_dim_param))
6634 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6636 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6637 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6639 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6640 error:
6641 isl_aff_free(aff);
6642 isl_multi_pw_aff_free(mpa);
6643 return NULL;
6646 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6647 * with the domain of "pa". The domain of the result is the same
6648 * as that of "mpa".
6649 * "mpa" and "pa" are assumed to have been aligned.
6651 * We consider each piece in turn. Note that the domains of the
6652 * pieces are assumed to be disjoint and they remain disjoint
6653 * after taking the preimage (over the same function).
6655 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6656 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6658 isl_space *space;
6659 isl_pw_aff *res;
6660 int i;
6662 if (!mpa || !pa)
6663 goto error;
6665 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6666 isl_pw_aff_get_space(pa));
6667 res = isl_pw_aff_empty(space);
6669 for (i = 0; i < pa->n; ++i) {
6670 isl_pw_aff *pa_i;
6671 isl_set *domain;
6673 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6674 isl_multi_pw_aff_copy(mpa),
6675 isl_aff_copy(pa->p[i].aff));
6676 domain = isl_set_copy(pa->p[i].set);
6677 domain = isl_set_preimage_multi_pw_aff(domain,
6678 isl_multi_pw_aff_copy(mpa));
6679 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6680 res = isl_pw_aff_add_disjoint(res, pa_i);
6683 isl_pw_aff_free(pa);
6684 isl_multi_pw_aff_free(mpa);
6685 return res;
6686 error:
6687 isl_pw_aff_free(pa);
6688 isl_multi_pw_aff_free(mpa);
6689 return NULL;
6692 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6693 * with the domain of "pa". The domain of the result is the same
6694 * as that of "mpa".
6696 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6697 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6699 if (!pa || !mpa)
6700 goto error;
6701 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6702 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6704 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6705 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6707 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6708 error:
6709 isl_pw_aff_free(pa);
6710 isl_multi_pw_aff_free(mpa);
6711 return NULL;
6714 /* Compute the pullback of "pa" by the function represented by "mpa".
6715 * In other words, plug in "mpa" in "pa".
6716 * "pa" and "mpa" are assumed to have been aligned.
6718 * The pullback is computed by applying "pa" to "mpa".
6720 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6721 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6723 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6726 /* Compute the pullback of "pa" by the function represented by "mpa".
6727 * In other words, plug in "mpa" in "pa".
6729 * The pullback is computed by applying "pa" to "mpa".
6731 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6732 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6734 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6737 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6738 * In other words, plug in "mpa2" in "mpa1".
6740 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6742 * We pullback each member of "mpa1" in turn.
6744 static __isl_give isl_multi_pw_aff *
6745 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6746 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6748 int i;
6749 isl_space *space = NULL;
6751 mpa1 = isl_multi_pw_aff_cow(mpa1);
6752 if (!mpa1 || !mpa2)
6753 goto error;
6755 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6756 isl_multi_pw_aff_get_space(mpa1));
6758 for (i = 0; i < mpa1->n; ++i) {
6759 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6760 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6761 if (!mpa1->p[i])
6762 goto error;
6765 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6767 isl_multi_pw_aff_free(mpa2);
6768 return mpa1;
6769 error:
6770 isl_space_free(space);
6771 isl_multi_pw_aff_free(mpa1);
6772 isl_multi_pw_aff_free(mpa2);
6773 return NULL;
6776 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6777 * In other words, plug in "mpa2" in "mpa1".
6779 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6780 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6782 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6783 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6786 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6787 * of "mpa1" and "mpa2" live in the same space, construct map space
6788 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6789 * with this map space as extract argument.
6791 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6792 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6793 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6794 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6796 int match;
6797 isl_space *space1, *space2;
6798 isl_map *res;
6800 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6801 isl_multi_pw_aff_get_space(mpa2));
6802 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6803 isl_multi_pw_aff_get_space(mpa1));
6804 if (!mpa1 || !mpa2)
6805 goto error;
6806 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6807 mpa2->space, isl_dim_out);
6808 if (match < 0)
6809 goto error;
6810 if (!match)
6811 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6812 "range spaces don't match", goto error);
6813 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6814 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6815 space1 = isl_space_map_from_domain_and_range(space1, space2);
6817 res = order(mpa1, mpa2, space1);
6818 isl_multi_pw_aff_free(mpa1);
6819 isl_multi_pw_aff_free(mpa2);
6820 return res;
6821 error:
6822 isl_multi_pw_aff_free(mpa1);
6823 isl_multi_pw_aff_free(mpa2);
6824 return NULL;
6827 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6828 * where the function values are equal. "space" is the space of the result.
6829 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6831 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6832 * in the sequences are equal.
6834 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6835 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6836 __isl_take isl_space *space)
6838 int i, n;
6839 isl_map *res;
6841 res = isl_map_universe(space);
6843 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6844 for (i = 0; i < n; ++i) {
6845 isl_pw_aff *pa1, *pa2;
6846 isl_map *map;
6848 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6849 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6850 map = isl_pw_aff_eq_map(pa1, pa2);
6851 res = isl_map_intersect(res, map);
6854 return res;
6857 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6858 * where the function values are equal.
6860 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6861 __isl_take isl_multi_pw_aff *mpa2)
6863 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6864 &isl_multi_pw_aff_eq_map_on_space);
6867 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6868 * where the function values of "mpa1" is lexicographically satisfies "base"
6869 * compared to that of "mpa2". "space" is the space of the result.
6870 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6872 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6873 * if its i-th element satisfies "base" when compared to
6874 * the i-th element of "mpa2" while all previous elements are
6875 * pairwise equal.
6877 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6878 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6879 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6880 __isl_take isl_pw_aff *pa2),
6881 __isl_take isl_space *space)
6883 int i, n;
6884 isl_map *res, *rest;
6886 res = isl_map_empty(isl_space_copy(space));
6887 rest = isl_map_universe(space);
6889 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6890 for (i = 0; i < n; ++i) {
6891 isl_pw_aff *pa1, *pa2;
6892 isl_map *map;
6894 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6895 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6896 map = base(pa1, pa2);
6897 map = isl_map_intersect(map, isl_map_copy(rest));
6898 res = isl_map_union(res, map);
6900 if (i == n - 1)
6901 continue;
6903 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6904 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6905 map = isl_pw_aff_eq_map(pa1, pa2);
6906 rest = isl_map_intersect(rest, map);
6909 isl_map_free(rest);
6910 return res;
6913 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6914 * where the function value of "mpa1" is lexicographically less than that
6915 * of "mpa2". "space" is the space of the result.
6916 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6918 * "mpa1" is less than "mpa2" if its i-th element is smaller
6919 * than the i-th element of "mpa2" while all previous elements are
6920 * pairwise equal.
6922 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6923 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6924 __isl_take isl_space *space)
6926 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6927 &isl_pw_aff_lt_map, space);
6930 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6931 * where the function value of "mpa1" is lexicographically less than that
6932 * of "mpa2".
6934 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6935 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6937 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6938 &isl_multi_pw_aff_lex_lt_map_on_space);
6941 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6942 * where the function value of "mpa1" is lexicographically greater than that
6943 * of "mpa2". "space" is the space of the result.
6944 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6946 * "mpa1" is greater than "mpa2" if its i-th element is greater
6947 * than the i-th element of "mpa2" while all previous elements are
6948 * pairwise equal.
6950 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6951 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6952 __isl_take isl_space *space)
6954 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6955 &isl_pw_aff_gt_map, space);
6958 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6959 * where the function value of "mpa1" is lexicographically greater than that
6960 * of "mpa2".
6962 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
6963 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6965 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6966 &isl_multi_pw_aff_lex_gt_map_on_space);
6969 /* Compare two isl_affs.
6971 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6972 * than "aff2" and 0 if they are equal.
6974 * The order is fairly arbitrary. We do consider expressions that only involve
6975 * earlier dimensions as "smaller".
6977 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6979 int cmp;
6980 int last1, last2;
6982 if (aff1 == aff2)
6983 return 0;
6985 if (!aff1)
6986 return -1;
6987 if (!aff2)
6988 return 1;
6990 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6991 if (cmp != 0)
6992 return cmp;
6994 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6995 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6996 if (last1 != last2)
6997 return last1 - last2;
6999 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7002 /* Compare two isl_pw_affs.
7004 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7005 * than "pa2" and 0 if they are equal.
7007 * The order is fairly arbitrary. We do consider expressions that only involve
7008 * earlier dimensions as "smaller".
7010 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7011 __isl_keep isl_pw_aff *pa2)
7013 int i;
7014 int cmp;
7016 if (pa1 == pa2)
7017 return 0;
7019 if (!pa1)
7020 return -1;
7021 if (!pa2)
7022 return 1;
7024 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7025 if (cmp != 0)
7026 return cmp;
7028 if (pa1->n != pa2->n)
7029 return pa1->n - pa2->n;
7031 for (i = 0; i < pa1->n; ++i) {
7032 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7033 if (cmp != 0)
7034 return cmp;
7035 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7036 if (cmp != 0)
7037 return cmp;
7040 return 0;
7043 /* Return a piecewise affine expression that is equal to "v" on "domain".
7045 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7046 __isl_take isl_val *v)
7048 isl_space *space;
7049 isl_local_space *ls;
7050 isl_aff *aff;
7052 space = isl_set_get_space(domain);
7053 ls = isl_local_space_from_space(space);
7054 aff = isl_aff_val_on_domain(ls, v);
7056 return isl_pw_aff_alloc(domain, aff);
7059 /* Return a multi affine expression that is equal to "mv" on domain
7060 * space "space".
7062 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7063 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7065 int i, n;
7066 isl_space *space2;
7067 isl_local_space *ls;
7068 isl_multi_aff *ma;
7070 if (!space || !mv)
7071 goto error;
7073 n = isl_multi_val_dim(mv, isl_dim_set);
7074 space2 = isl_multi_val_get_space(mv);
7075 space2 = isl_space_align_params(space2, isl_space_copy(space));
7076 space = isl_space_align_params(space, isl_space_copy(space2));
7077 space = isl_space_map_from_domain_and_range(space, space2);
7078 ma = isl_multi_aff_alloc(isl_space_copy(space));
7079 ls = isl_local_space_from_space(isl_space_domain(space));
7080 for (i = 0; i < n; ++i) {
7081 isl_val *v;
7082 isl_aff *aff;
7084 v = isl_multi_val_get_val(mv, i);
7085 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7086 ma = isl_multi_aff_set_aff(ma, i, aff);
7088 isl_local_space_free(ls);
7090 isl_multi_val_free(mv);
7091 return ma;
7092 error:
7093 isl_space_free(space);
7094 isl_multi_val_free(mv);
7095 return NULL;
7098 /* Return a piecewise multi-affine expression
7099 * that is equal to "mv" on "domain".
7101 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7102 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7104 isl_space *space;
7105 isl_multi_aff *ma;
7107 space = isl_set_get_space(domain);
7108 ma = isl_multi_aff_multi_val_on_space(space, mv);
7110 return isl_pw_multi_aff_alloc(domain, ma);
7113 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7114 * mv is the value that should be attained on each domain set
7115 * res collects the results
7117 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7118 isl_multi_val *mv;
7119 isl_union_pw_multi_aff *res;
7122 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7123 * and add it to data->res.
7125 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7126 void *user)
7128 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7129 isl_pw_multi_aff *pma;
7130 isl_multi_val *mv;
7132 mv = isl_multi_val_copy(data->mv);
7133 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7134 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7136 return data->res ? 0 : -1;
7139 /* Return a union piecewise multi-affine expression
7140 * that is equal to "mv" on "domain".
7142 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7143 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7145 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7146 isl_space *space;
7148 space = isl_union_set_get_space(domain);
7149 data.res = isl_union_pw_multi_aff_empty(space);
7150 data.mv = mv;
7151 if (isl_union_set_foreach_set(domain,
7152 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7153 data.res = isl_union_pw_multi_aff_free(data.res);
7154 isl_union_set_free(domain);
7155 isl_multi_val_free(mv);
7156 return data.res;
7159 /* Compute the pullback of data->pma by the function represented by "pma2",
7160 * provided the spaces match, and add the results to data->res.
7162 static int pullback_entry(void **entry, void *user)
7164 struct isl_union_pw_multi_aff_bin_data *data = user;
7165 isl_pw_multi_aff *pma2 = *entry;
7167 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7168 pma2->dim, isl_dim_out))
7169 return 0;
7171 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7172 isl_pw_multi_aff_copy(data->pma),
7173 isl_pw_multi_aff_copy(pma2));
7175 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7176 if (!data->res)
7177 return -1;
7179 return 0;
7182 /* Compute the pullback of "upma1" by the function represented by "upma2".
7184 __isl_give isl_union_pw_multi_aff *
7185 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7186 __isl_take isl_union_pw_multi_aff *upma1,
7187 __isl_take isl_union_pw_multi_aff *upma2)
7189 return bin_op(upma1, upma2, &pullback_entry);
7192 /* Check that the domain space of "upa" matches "space".
7194 * Return 0 on success and -1 on error.
7196 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7197 * can in principle never fail since the space "space" is that
7198 * of the isl_multi_union_pw_aff and is a set space such that
7199 * there is no domain space to match.
7201 * We check the parameters and double-check that "space" is
7202 * indeed that of a set.
7204 static int isl_union_pw_aff_check_match_domain_space(
7205 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7207 isl_space *upa_space;
7208 int match;
7210 if (!upa || !space)
7211 return -1;
7213 match = isl_space_is_set(space);
7214 if (match < 0)
7215 return -1;
7216 if (!match)
7217 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7218 "expecting set space", return -1);
7220 upa_space = isl_union_pw_aff_get_space(upa);
7221 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7222 if (match < 0)
7223 goto error;
7224 if (!match)
7225 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7226 "parameters don't match", goto error);
7228 isl_space_free(upa_space);
7229 return 0;
7230 error:
7231 isl_space_free(upa_space);
7232 return -1;
7235 /* Do the parameters of "upa" match those of "space"?
7237 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7238 __isl_keep isl_space *space)
7240 isl_space *upa_space;
7241 int match;
7243 if (!upa || !space)
7244 return -1;
7246 upa_space = isl_union_pw_aff_get_space(upa);
7248 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7250 isl_space_free(upa_space);
7251 return match;
7254 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7255 * space represents the new parameters.
7256 * res collects the results.
7258 struct isl_union_pw_aff_reset_params_data {
7259 isl_space *space;
7260 isl_union_pw_aff *res;
7263 /* Replace the parameters of "pa" by data->space and
7264 * add the result to data->res.
7266 static int reset_params(__isl_take isl_pw_aff *pa, void *user)
7268 struct isl_union_pw_aff_reset_params_data *data = user;
7269 isl_space *space;
7271 space = isl_pw_aff_get_space(pa);
7272 space = isl_space_replace(space, isl_dim_param, data->space);
7273 pa = isl_pw_aff_reset_space(pa, space);
7274 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7276 return data->res ? 0 : -1;
7279 /* Replace the domain space of "upa" by "space".
7280 * Since a union expression does not have a (single) domain space,
7281 * "space" is necessarily a parameter space.
7283 * Since the order and the names of the parameters determine
7284 * the hash value, we need to create a new hash table.
7286 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7287 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7289 struct isl_union_pw_aff_reset_params_data data = { space };
7290 int match;
7292 match = isl_union_pw_aff_matching_params(upa, space);
7293 if (match < 0)
7294 upa = isl_union_pw_aff_free(upa);
7295 else if (match) {
7296 isl_space_free(space);
7297 return upa;
7300 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7301 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7302 data.res = isl_union_pw_aff_free(data.res);
7304 isl_union_pw_aff_free(upa);
7305 isl_space_free(space);
7306 return data.res;
7309 /* Replace the entry of isl_union_pw_aff to which "entry" points
7310 * by its floor.
7312 static int floor_entry(void **entry, void *user)
7314 isl_pw_aff **pa = (isl_pw_aff **) entry;
7316 *pa = isl_pw_aff_floor(*pa);
7317 if (!*pa)
7318 return -1;
7320 return 0;
7323 /* Given f, return floor(f).
7325 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7326 __isl_take isl_union_pw_aff *upa)
7328 isl_ctx *ctx;
7330 upa = isl_union_pw_aff_cow(upa);
7331 if (!upa)
7332 return NULL;
7334 ctx = isl_union_pw_aff_get_ctx(upa);
7335 if (isl_hash_table_foreach(ctx, &upa->table, &floor_entry, NULL) < 0)
7336 upa = isl_union_pw_aff_free(upa);
7338 return upa;
7341 /* Compute
7343 * upa mod m = upa - m * floor(upa/m)
7345 * with m an integer value.
7347 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7348 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7350 isl_union_pw_aff *res;
7352 if (!upa || !m)
7353 goto error;
7355 if (!isl_val_is_int(m))
7356 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7357 "expecting integer modulo", goto error);
7358 if (!isl_val_is_pos(m))
7359 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7360 "expecting positive modulo", goto error);
7362 res = isl_union_pw_aff_copy(upa);
7363 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7364 upa = isl_union_pw_aff_floor(upa);
7365 upa = isl_union_pw_aff_scale_val(upa, m);
7366 res = isl_union_pw_aff_sub(res, upa);
7368 return res;
7369 error:
7370 isl_val_free(m);
7371 isl_union_pw_aff_free(upa);
7372 return NULL;
7375 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7376 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7377 * needs to attain.
7378 * "res" collects the results.
7380 struct isl_union_pw_aff_aff_on_domain_data {
7381 isl_aff *aff;
7382 isl_union_pw_aff *res;
7385 /* Construct a piecewise affine expression that is equal to data->aff
7386 * on "domain" and add the result to data->res.
7388 static int pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7390 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7391 isl_pw_aff *pa;
7392 isl_aff *aff;
7393 int dim;
7395 aff = isl_aff_copy(data->aff);
7396 dim = isl_set_dim(domain, isl_dim_set);
7397 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7398 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7399 pa = isl_pw_aff_alloc(domain, aff);
7400 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7402 return data->res ? 0 : -1;
7405 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7406 * pos is the output position that needs to be extracted.
7407 * res collects the results.
7409 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7410 int pos;
7411 isl_union_pw_aff *res;
7414 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7415 * (assuming it has such a dimension) and add it to data->res.
7417 static int get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7419 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7420 int n_out;
7421 isl_pw_aff *pa;
7423 if (!pma)
7424 return -1;
7426 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7427 if (data->pos >= n_out) {
7428 isl_pw_multi_aff_free(pma);
7429 return 0;
7432 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7433 isl_pw_multi_aff_free(pma);
7435 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7437 return data->res ? 0 : -1;
7440 /* Extract an isl_union_pw_aff corresponding to
7441 * output dimension "pos" of "upma".
7443 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7444 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7446 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7447 isl_space *space;
7449 if (!upma)
7450 return NULL;
7452 if (pos < 0)
7453 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7454 "cannot extract at negative position", return NULL);
7456 space = isl_union_pw_multi_aff_get_space(upma);
7457 data.res = isl_union_pw_aff_empty(space);
7458 data.pos = pos;
7459 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7460 &get_union_pw_aff, &data) < 0)
7461 data.res = isl_union_pw_aff_free(data.res);
7463 return data.res;
7466 /* Return a union piecewise affine expression
7467 * that is equal to "aff" on "domain".
7469 * Construct an isl_pw_aff on each of the sets in "domain" and
7470 * collect the results.
7472 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7473 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7475 struct isl_union_pw_aff_aff_on_domain_data data;
7476 isl_space *space;
7478 if (!domain || !aff)
7479 goto error;
7480 if (!isl_local_space_is_params(aff->ls))
7481 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7482 "expecting parametric expression", goto error);
7484 space = isl_union_set_get_space(domain);
7485 data.res = isl_union_pw_aff_empty(space);
7486 data.aff = aff;
7487 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7488 data.res = isl_union_pw_aff_free(data.res);
7489 isl_union_set_free(domain);
7490 isl_aff_free(aff);
7491 return data.res;
7492 error:
7493 isl_union_set_free(domain);
7494 isl_aff_free(aff);
7495 return NULL;
7498 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7499 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7500 * "res" collects the results.
7502 struct isl_union_pw_aff_val_on_domain_data {
7503 isl_val *v;
7504 isl_union_pw_aff *res;
7507 /* Construct a piecewise affine expression that is equal to data->v
7508 * on "domain" and add the result to data->res.
7510 static int pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7512 struct isl_union_pw_aff_val_on_domain_data *data = user;
7513 isl_pw_aff *pa;
7514 isl_val *v;
7516 v = isl_val_copy(data->v);
7517 pa = isl_pw_aff_val_on_domain(domain, v);
7518 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7520 return data->res ? 0 : -1;
7523 /* Return a union piecewise affine expression
7524 * that is equal to "v" on "domain".
7526 * Construct an isl_pw_aff on each of the sets in "domain" and
7527 * collect the results.
7529 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7530 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7532 struct isl_union_pw_aff_val_on_domain_data data;
7533 isl_space *space;
7535 space = isl_union_set_get_space(domain);
7536 data.res = isl_union_pw_aff_empty(space);
7537 data.v = v;
7538 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7539 data.res = isl_union_pw_aff_free(data.res);
7540 isl_union_set_free(domain);
7541 isl_val_free(v);
7542 return data.res;
7545 /* Construct a piecewise multi affine expression
7546 * that is equal to "pa" and add it to upma.
7548 static int pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7550 isl_union_pw_multi_aff **upma = user;
7551 isl_pw_multi_aff *pma;
7553 pma = isl_pw_multi_aff_from_pw_aff(pa);
7554 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7556 return *upma ? 0 : -1;
7559 /* Construct and return a union piecewise multi affine expression
7560 * that is equal to the given union piecewise affine expression.
7562 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7563 __isl_take isl_union_pw_aff *upa)
7565 isl_space *space;
7566 isl_union_pw_multi_aff *upma;
7568 if (!upa)
7569 return NULL;
7571 space = isl_union_pw_aff_get_space(upa);
7572 upma = isl_union_pw_multi_aff_empty(space);
7574 if (isl_union_pw_aff_foreach_pw_aff(upa,
7575 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7576 upma = isl_union_pw_multi_aff_free(upma);
7578 isl_union_pw_aff_free(upa);
7579 return upma;
7582 /* Compute the set of elements in the domain of "pa" where it is zero and
7583 * add this set to "uset".
7585 static int zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7587 isl_union_set **uset = (isl_union_set **)user;
7589 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7591 return *uset ? 0 : -1;
7594 /* Return a union set containing those elements in the domain
7595 * of "upa" where it is zero.
7597 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7598 __isl_take isl_union_pw_aff *upa)
7600 isl_union_set *zero;
7602 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7603 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7604 zero = isl_union_set_free(zero);
7606 isl_union_pw_aff_free(upa);
7607 return zero;
7610 /* Convert "pa" to an isl_map and add it to *umap.
7612 static int map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7614 isl_union_map **umap = user;
7615 isl_map *map;
7617 map = isl_map_from_pw_aff(pa);
7618 *umap = isl_union_map_add_map(*umap, map);
7620 return *umap ? 0 : -1;
7623 /* Construct a union map mapping the domain of the union
7624 * piecewise affine expression to its range, with the single output dimension
7625 * equated to the corresponding affine expressions on their cells.
7627 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7628 __isl_take isl_union_pw_aff *upa)
7630 isl_space *space;
7631 isl_union_map *umap;
7633 if (!upa)
7634 return NULL;
7636 space = isl_union_pw_aff_get_space(upa);
7637 umap = isl_union_map_empty(space);
7639 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7640 &umap) < 0)
7641 umap = isl_union_map_free(umap);
7643 isl_union_pw_aff_free(upa);
7644 return umap;
7647 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7648 * upma is the function that is plugged in.
7649 * pa is the current part of the function in which upma is plugged in.
7650 * res collects the results.
7652 struct isl_union_pw_aff_pullback_upma_data {
7653 isl_union_pw_multi_aff *upma;
7654 isl_pw_aff *pa;
7655 isl_union_pw_aff *res;
7658 /* Check if "pma" can be plugged into data->pa.
7659 * If so, perform the pullback and add the result to data->res.
7661 static int pa_pb_pma(void **entry, void *user)
7663 struct isl_union_pw_aff_pullback_upma_data *data = user;
7664 isl_pw_multi_aff *pma = *entry;
7665 isl_pw_aff *pa;
7667 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7668 pma->dim, isl_dim_out))
7669 return 0;
7671 pma = isl_pw_multi_aff_copy(pma);
7672 pa = isl_pw_aff_copy(data->pa);
7673 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7675 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7677 return data->res ? 0 : -1;
7680 /* Check if any of the elements of data->upma can be plugged into pa,
7681 * add if so add the result to data->res.
7683 static int upa_pb_upma(void **entry, void *user)
7685 struct isl_union_pw_aff_pullback_upma_data *data = user;
7686 isl_ctx *ctx;
7687 isl_pw_aff *pa = *entry;
7689 data->pa = pa;
7690 ctx = isl_union_pw_multi_aff_get_ctx(data->upma);
7691 if (isl_hash_table_foreach(ctx, &data->upma->table,
7692 &pa_pb_pma, data) < 0)
7693 return -1;
7695 return 0;
7698 /* Compute the pullback of "upa" by the function represented by "upma".
7699 * In other words, plug in "upma" in "upa". The result contains
7700 * expressions defined over the domain space of "upma".
7702 * Run over all pairs of elements in "upa" and "upma", perform
7703 * the pullback when appropriate and collect the results.
7704 * If the hash value were based on the domain space rather than
7705 * the function space, then we could run through all elements
7706 * of "upma" and directly pick out the corresponding element of "upa".
7708 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7709 __isl_take isl_union_pw_aff *upa,
7710 __isl_take isl_union_pw_multi_aff *upma)
7712 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7713 isl_ctx *ctx;
7714 isl_space *space;
7716 space = isl_union_pw_multi_aff_get_space(upma);
7717 upa = isl_union_pw_aff_align_params(upa, space);
7718 space = isl_union_pw_aff_get_space(upa);
7719 upma = isl_union_pw_multi_aff_align_params(upma, space);
7721 if (!upa || !upma)
7722 goto error;
7724 ctx = isl_union_pw_aff_get_ctx(upa);
7725 data.upma = upma;
7726 space = isl_union_pw_aff_get_space(upa);
7727 data.res = isl_union_pw_aff_alloc(space, upa->table.n);
7728 if (isl_hash_table_foreach(ctx, &upa->table, &upa_pb_upma, &data) < 0)
7729 data.res = isl_union_pw_aff_free(data.res);
7731 isl_union_pw_aff_free(upa);
7732 isl_union_pw_multi_aff_free(upma);
7733 return data.res;
7734 error:
7735 isl_union_pw_aff_free(upa);
7736 isl_union_pw_multi_aff_free(upma);
7737 return NULL;
7740 #undef BASE
7741 #define BASE union_pw_aff
7742 #undef DOMBASE
7743 #define DOMBASE union_set
7745 #define NO_MOVE_DIMS
7746 #define NO_DIMS
7747 #define NO_DOMAIN
7748 #define NO_PRODUCT
7749 #define NO_SPLICE
7750 #define NO_ZERO
7751 #define NO_IDENTITY
7752 #define NO_GIST
7754 #include <isl_multi_templ.c>
7755 #include <isl_multi_apply_set.c>
7756 #include <isl_multi_apply_union_set.c>
7757 #include <isl_multi_floor.c>
7758 #include <isl_multi_gist.c>
7759 #include <isl_multi_intersect.c>
7761 /* Construct a multiple union piecewise affine expression
7762 * in the given space with value zero in each of the output dimensions.
7764 * Since there is no canonical zero value for
7765 * a union piecewise affine expression, we can only construct
7766 * zero-dimensional "zero" value.
7768 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7769 __isl_take isl_space *space)
7771 if (!space)
7772 return NULL;
7774 if (!isl_space_is_set(space))
7775 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7776 "expecting set space", goto error);
7777 if (isl_space_dim(space , isl_dim_out) != 0)
7778 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7779 "expecting 0D space", goto error);
7781 return isl_multi_union_pw_aff_alloc(space);
7782 error:
7783 isl_space_free(space);
7784 return NULL;
7787 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7788 * with the actual sum on the shared domain and
7789 * the defined expression on the symmetric difference of the domains.
7791 * We simply iterate over the elements in both arguments and
7792 * call isl_union_pw_aff_union_add on each of them.
7794 static __isl_give isl_multi_union_pw_aff *
7795 isl_multi_union_pw_aff_union_add_aligned(
7796 __isl_take isl_multi_union_pw_aff *mupa1,
7797 __isl_take isl_multi_union_pw_aff *mupa2)
7799 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7800 &isl_union_pw_aff_union_add);
7803 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7804 * with the actual sum on the shared domain and
7805 * the defined expression on the symmetric difference of the domains.
7807 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7808 __isl_take isl_multi_union_pw_aff *mupa1,
7809 __isl_take isl_multi_union_pw_aff *mupa2)
7811 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7812 &isl_multi_union_pw_aff_union_add_aligned);
7815 /* Construct and return a multi union piecewise affine expression
7816 * that is equal to the given multi affine expression.
7818 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7819 __isl_take isl_multi_aff *ma)
7821 isl_multi_pw_aff *mpa;
7823 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7824 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7827 /* Construct and return a multi union piecewise affine expression
7828 * that is equal to the given multi piecewise affine expression.
7830 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7831 __isl_take isl_multi_pw_aff *mpa)
7833 int i, n;
7834 isl_space *space;
7835 isl_multi_union_pw_aff *mupa;
7837 if (!mpa)
7838 return NULL;
7840 space = isl_multi_pw_aff_get_space(mpa);
7841 space = isl_space_range(space);
7842 mupa = isl_multi_union_pw_aff_alloc(space);
7844 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7845 for (i = 0; i < n; ++i) {
7846 isl_pw_aff *pa;
7847 isl_union_pw_aff *upa;
7849 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7850 upa = isl_union_pw_aff_from_pw_aff(pa);
7851 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7854 isl_multi_pw_aff_free(mpa);
7856 return mupa;
7859 /* Extract the range space of "pma" and assign it to *space.
7860 * If *space has already been set (through a previous call to this function),
7861 * then check that the range space is the same.
7863 static int extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7865 isl_space **space = user;
7866 isl_space *pma_space;
7867 int equal;
7869 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7870 isl_pw_multi_aff_free(pma);
7872 if (!pma_space)
7873 return -1;
7874 if (!*space) {
7875 *space = pma_space;
7876 return 0;
7879 equal = isl_space_is_equal(pma_space, *space);
7880 isl_space_free(pma_space);
7882 if (equal < 0)
7883 return -1;
7884 if (!equal)
7885 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7886 "range spaces not the same", return -1);
7887 return 0;
7890 /* Construct and return a multi union piecewise affine expression
7891 * that is equal to the given union piecewise multi affine expression.
7893 * In order to be able to perform the conversion, the input
7894 * needs to be non-empty and may only involve a single range space.
7896 __isl_give isl_multi_union_pw_aff *
7897 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7898 __isl_take isl_union_pw_multi_aff *upma)
7900 isl_space *space = NULL;
7901 isl_multi_union_pw_aff *mupa;
7902 int i, n;
7904 if (!upma)
7905 return NULL;
7906 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7907 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7908 "cannot extract range space from empty input",
7909 goto error);
7910 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7911 &space) < 0)
7912 goto error;
7914 if (!space)
7915 goto error;
7917 n = isl_space_dim(space, isl_dim_set);
7918 mupa = isl_multi_union_pw_aff_alloc(space);
7920 for (i = 0; i < n; ++i) {
7921 isl_union_pw_aff *upa;
7923 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7924 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7927 isl_union_pw_multi_aff_free(upma);
7928 return mupa;
7929 error:
7930 isl_space_free(space);
7931 isl_union_pw_multi_aff_free(upma);
7932 return NULL;
7935 /* Try and create an isl_multi_union_pw_aff that is equivalent
7936 * to the given isl_union_map.
7937 * The isl_union_map is required to be single-valued in each space.
7938 * Moreover, it cannot be empty and all range spaces need to be the same.
7939 * Otherwise, an error is produced.
7941 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7942 __isl_take isl_union_map *umap)
7944 isl_union_pw_multi_aff *upma;
7946 upma = isl_union_pw_multi_aff_from_union_map(umap);
7947 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7950 /* Return a multiple union piecewise affine expression
7951 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7952 * have been aligned.
7954 static __isl_give isl_multi_union_pw_aff *
7955 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7956 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7958 int i, n;
7959 isl_space *space;
7960 isl_multi_union_pw_aff *mupa;
7962 if (!domain || !mv)
7963 goto error;
7965 n = isl_multi_val_dim(mv, isl_dim_set);
7966 space = isl_multi_val_get_space(mv);
7967 mupa = isl_multi_union_pw_aff_alloc(space);
7968 for (i = 0; i < n; ++i) {
7969 isl_val *v;
7970 isl_union_pw_aff *upa;
7972 v = isl_multi_val_get_val(mv, i);
7973 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
7975 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7978 isl_union_set_free(domain);
7979 isl_multi_val_free(mv);
7980 return mupa;
7981 error:
7982 isl_union_set_free(domain);
7983 isl_multi_val_free(mv);
7984 return NULL;
7987 /* Return a multiple union piecewise affine expression
7988 * that is equal to "mv" on "domain".
7990 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
7991 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7993 if (!domain || !mv)
7994 goto error;
7995 if (isl_space_match(domain->dim, isl_dim_param,
7996 mv->space, isl_dim_param))
7997 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7998 domain, mv);
7999 domain = isl_union_set_align_params(domain,
8000 isl_multi_val_get_space(mv));
8001 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8002 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8003 error:
8004 isl_union_set_free(domain);
8005 isl_multi_val_free(mv);
8006 return NULL;
8009 /* Return a multiple union piecewise affine expression
8010 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8011 * have been aligned.
8013 static __isl_give isl_multi_union_pw_aff *
8014 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8015 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8017 int i, n;
8018 isl_space *space;
8019 isl_multi_union_pw_aff *mupa;
8021 if (!domain || !ma)
8022 goto error;
8024 n = isl_multi_aff_dim(ma, isl_dim_set);
8025 space = isl_multi_aff_get_space(ma);
8026 mupa = isl_multi_union_pw_aff_alloc(space);
8027 for (i = 0; i < n; ++i) {
8028 isl_aff *aff;
8029 isl_union_pw_aff *upa;
8031 aff = isl_multi_aff_get_aff(ma, i);
8032 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8033 aff);
8034 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8037 isl_union_set_free(domain);
8038 isl_multi_aff_free(ma);
8039 return mupa;
8040 error:
8041 isl_union_set_free(domain);
8042 isl_multi_aff_free(ma);
8043 return NULL;
8046 /* Return a multiple union piecewise affine expression
8047 * that is equal to "ma" on "domain".
8049 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8050 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8052 if (!domain || !ma)
8053 goto error;
8054 if (isl_space_match(domain->dim, isl_dim_param,
8055 ma->space, isl_dim_param))
8056 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8057 domain, ma);
8058 domain = isl_union_set_align_params(domain,
8059 isl_multi_aff_get_space(ma));
8060 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8061 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8062 error:
8063 isl_union_set_free(domain);
8064 isl_multi_aff_free(ma);
8065 return NULL;
8068 /* Return a union set containing those elements in the domains
8069 * of the elements of "mupa" where they are all zero.
8071 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8072 __isl_take isl_multi_union_pw_aff *mupa)
8074 int i, n;
8075 isl_union_pw_aff *upa;
8076 isl_union_set *zero;
8078 if (!mupa)
8079 return NULL;
8081 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8082 if (n == 0)
8083 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8084 "cannot determine zero set "
8085 "of zero-dimensional function", goto error);
8087 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8088 zero = isl_union_pw_aff_zero_union_set(upa);
8090 for (i = 1; i < n; ++i) {
8091 isl_union_set *zero_i;
8093 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8094 zero_i = isl_union_pw_aff_zero_union_set(upa);
8096 zero = isl_union_set_intersect(zero, zero_i);
8099 isl_multi_union_pw_aff_free(mupa);
8100 return zero;
8101 error:
8102 isl_multi_union_pw_aff_free(mupa);
8103 return NULL;
8106 /* Construct a union map mapping the shared domain
8107 * of the union piecewise affine expressions to the range of "mupa"
8108 * with each dimension in the range equated to the
8109 * corresponding union piecewise affine expression.
8111 * The input cannot be zero-dimensional as there is
8112 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8114 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8115 __isl_take isl_multi_union_pw_aff *mupa)
8117 int i, n;
8118 isl_space *space;
8119 isl_union_map *umap;
8120 isl_union_pw_aff *upa;
8122 if (!mupa)
8123 return NULL;
8125 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8126 if (n == 0)
8127 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8128 "cannot determine domain of zero-dimensional "
8129 "isl_multi_union_pw_aff", goto error);
8131 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8132 umap = isl_union_map_from_union_pw_aff(upa);
8134 for (i = 1; i < n; ++i) {
8135 isl_union_map *umap_i;
8137 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8138 umap_i = isl_union_map_from_union_pw_aff(upa);
8139 umap = isl_union_map_flat_range_product(umap, umap_i);
8142 space = isl_multi_union_pw_aff_get_space(mupa);
8143 umap = isl_union_map_reset_range_space(umap, space);
8145 isl_multi_union_pw_aff_free(mupa);
8146 return umap;
8147 error:
8148 isl_multi_union_pw_aff_free(mupa);
8149 return NULL;
8152 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8153 * "range" is the space from which to set the range space.
8154 * "res" collects the results.
8156 struct isl_union_pw_multi_aff_reset_range_space_data {
8157 isl_space *range;
8158 isl_union_pw_multi_aff *res;
8161 /* Replace the range space of "pma" by the range space of data->range and
8162 * add the result to data->res.
8164 static int reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8166 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8167 isl_space *space;
8169 space = isl_pw_multi_aff_get_space(pma);
8170 space = isl_space_domain(space);
8171 space = isl_space_extend_domain_with_range(space,
8172 isl_space_copy(data->range));
8173 pma = isl_pw_multi_aff_reset_space(pma, space);
8174 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8176 return data->res ? 0 : -1;
8179 /* Replace the range space of all the piecewise affine expressions in "upma" by
8180 * the range space of "space".
8182 * This assumes that all these expressions have the same output dimension.
8184 * Since the spaces of the expressions change, so do their hash values.
8185 * We therefore need to create a new isl_union_pw_multi_aff.
8186 * Note that the hash value is currently computed based on the entire
8187 * space even though there can only be a single expression with a given
8188 * domain space.
8190 static __isl_give isl_union_pw_multi_aff *
8191 isl_union_pw_multi_aff_reset_range_space(
8192 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8194 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8195 isl_space *space_upma;
8197 space_upma = isl_union_pw_multi_aff_get_space(upma);
8198 data.res = isl_union_pw_multi_aff_empty(space_upma);
8199 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8200 &reset_range_space, &data) < 0)
8201 data.res = isl_union_pw_multi_aff_free(data.res);
8203 isl_space_free(space);
8204 isl_union_pw_multi_aff_free(upma);
8205 return data.res;
8208 /* Construct and return a union piecewise multi affine expression
8209 * that is equal to the given multi union piecewise affine expression.
8211 * In order to be able to perform the conversion, the input
8212 * needs to have a least one output dimension.
8214 __isl_give isl_union_pw_multi_aff *
8215 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8216 __isl_take isl_multi_union_pw_aff *mupa)
8218 int i, n;
8219 isl_space *space;
8220 isl_union_pw_multi_aff *upma;
8221 isl_union_pw_aff *upa;
8223 if (!mupa)
8224 return NULL;
8226 space = isl_multi_union_pw_aff_get_space(mupa);
8228 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8229 if (n == 0)
8230 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8231 "cannot determine domain of zero-dimensional "
8232 "isl_multi_union_pw_aff", goto error);
8234 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8235 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8237 for (i = 1; i < n; ++i) {
8238 isl_union_pw_multi_aff *upma_i;
8240 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8241 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8242 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8245 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8247 isl_multi_union_pw_aff_free(mupa);
8248 return upma;
8249 error:
8250 isl_multi_union_pw_aff_free(mupa);
8251 return NULL;
8254 /* Intersect the range of "mupa" with "range".
8255 * That is, keep only those domain elements that have a function value
8256 * in "range".
8258 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8259 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8261 isl_union_pw_multi_aff *upma;
8262 isl_union_set *domain;
8263 isl_space *space;
8264 int n;
8265 int match;
8267 if (!mupa || !range)
8268 goto error;
8270 space = isl_set_get_space(range);
8271 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8272 space, isl_dim_set);
8273 isl_space_free(space);
8274 if (match < 0)
8275 goto error;
8276 if (!match)
8277 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8278 "space don't match", goto error);
8279 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8280 if (n == 0)
8281 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8282 "cannot intersect range of zero-dimensional "
8283 "isl_multi_union_pw_aff", goto error);
8285 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8286 isl_multi_union_pw_aff_copy(mupa));
8287 domain = isl_union_set_from_set(range);
8288 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8289 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8291 return mupa;
8292 error:
8293 isl_multi_union_pw_aff_free(mupa);
8294 isl_set_free(range);
8295 return NULL;
8298 /* Return the shared domain of the elements of "mupa".
8300 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8301 __isl_take isl_multi_union_pw_aff *mupa)
8303 int i, n;
8304 isl_union_pw_aff *upa;
8305 isl_union_set *dom;
8307 if (!mupa)
8308 return NULL;
8310 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8311 if (n == 0)
8312 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8313 "cannot determine domain", goto error);
8315 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8316 dom = isl_union_pw_aff_domain(upa);
8317 for (i = 1; i < n; ++i) {
8318 isl_union_set *dom_i;
8320 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8321 dom_i = isl_union_pw_aff_domain(upa);
8322 dom = isl_union_set_intersect(dom, dom_i);
8325 isl_multi_union_pw_aff_free(mupa);
8326 return dom;
8327 error:
8328 isl_multi_union_pw_aff_free(mupa);
8329 return NULL;
8332 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8333 * In particular, the spaces have been aligned.
8334 * The result is defined over the shared domain of the elements of "mupa"
8336 * We first extract the parametric constant part of "aff" and
8337 * define that over the shared domain.
8338 * Then we iterate over all input dimensions of "aff" and add the corresponding
8339 * multiples of the elements of "mupa".
8340 * Finally, we consider the integer divisions, calling the function
8341 * recursively to obtain an isl_union_pw_aff corresponding to the
8342 * integer division argument.
8344 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8345 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8347 int i, n_in, n_div;
8348 isl_union_pw_aff *upa;
8349 isl_union_set *uset;
8350 isl_val *v;
8351 isl_aff *cst;
8353 n_in = isl_aff_dim(aff, isl_dim_in);
8354 n_div = isl_aff_dim(aff, isl_dim_div);
8356 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8357 cst = isl_aff_copy(aff);
8358 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8359 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8360 cst = isl_aff_project_domain_on_params(cst);
8361 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8363 for (i = 0; i < n_in; ++i) {
8364 isl_union_pw_aff *upa_i;
8366 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8367 continue;
8368 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8369 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8370 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8371 upa = isl_union_pw_aff_add(upa, upa_i);
8374 for (i = 0; i < n_div; ++i) {
8375 isl_aff *div;
8376 isl_union_pw_aff *upa_i;
8378 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8379 continue;
8380 div = isl_aff_get_div(aff, i);
8381 upa_i = multi_union_pw_aff_apply_aff(
8382 isl_multi_union_pw_aff_copy(mupa), div);
8383 upa_i = isl_union_pw_aff_floor(upa_i);
8384 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8385 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8386 upa = isl_union_pw_aff_add(upa, upa_i);
8389 isl_multi_union_pw_aff_free(mupa);
8390 isl_aff_free(aff);
8392 return upa;
8395 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8396 * with the domain of "aff".
8397 * Furthermore, the dimension of this space needs to be greater than zero.
8398 * The result is defined over the shared domain of the elements of "mupa"
8400 * We perform these checks and then hand over control to
8401 * multi_union_pw_aff_apply_aff.
8403 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8404 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8406 isl_space *space1, *space2;
8407 int equal;
8409 mupa = isl_multi_union_pw_aff_align_params(mupa,
8410 isl_aff_get_space(aff));
8411 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8412 if (!mupa || !aff)
8413 goto error;
8415 space1 = isl_multi_union_pw_aff_get_space(mupa);
8416 space2 = isl_aff_get_domain_space(aff);
8417 equal = isl_space_is_equal(space1, space2);
8418 isl_space_free(space1);
8419 isl_space_free(space2);
8420 if (equal < 0)
8421 goto error;
8422 if (!equal)
8423 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8424 "spaces don't match", goto error);
8425 if (isl_aff_dim(aff, isl_dim_in) == 0)
8426 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8427 "cannot determine domains", goto error);
8429 return multi_union_pw_aff_apply_aff(mupa, aff);
8430 error:
8431 isl_multi_union_pw_aff_free(mupa);
8432 isl_aff_free(aff);
8433 return NULL;
8436 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8437 * with the domain of "ma".
8438 * Furthermore, the dimension of this space needs to be greater than zero,
8439 * unless the dimension of the target space of "ma" is also zero.
8440 * The result is defined over the shared domain of the elements of "mupa"
8442 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8443 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8445 isl_space *space1, *space2;
8446 isl_multi_union_pw_aff *res;
8447 int equal;
8448 int i, n_out;
8450 mupa = isl_multi_union_pw_aff_align_params(mupa,
8451 isl_multi_aff_get_space(ma));
8452 ma = isl_multi_aff_align_params(ma,
8453 isl_multi_union_pw_aff_get_space(mupa));
8454 if (!mupa || !ma)
8455 goto error;
8457 space1 = isl_multi_union_pw_aff_get_space(mupa);
8458 space2 = isl_multi_aff_get_domain_space(ma);
8459 equal = isl_space_is_equal(space1, space2);
8460 isl_space_free(space1);
8461 isl_space_free(space2);
8462 if (equal < 0)
8463 goto error;
8464 if (!equal)
8465 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8466 "spaces don't match", goto error);
8467 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8468 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8469 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8470 "cannot determine domains", goto error);
8472 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8473 res = isl_multi_union_pw_aff_alloc(space1);
8475 for (i = 0; i < n_out; ++i) {
8476 isl_aff *aff;
8477 isl_union_pw_aff *upa;
8479 aff = isl_multi_aff_get_aff(ma, i);
8480 upa = multi_union_pw_aff_apply_aff(
8481 isl_multi_union_pw_aff_copy(mupa), aff);
8482 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8485 isl_multi_aff_free(ma);
8486 isl_multi_union_pw_aff_free(mupa);
8487 return res;
8488 error:
8489 isl_multi_union_pw_aff_free(mupa);
8490 isl_multi_aff_free(ma);
8491 return NULL;
8494 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8495 * with the domain of "pa".
8496 * Furthermore, the dimension of this space needs to be greater than zero.
8497 * The result is defined over the shared domain of the elements of "mupa"
8499 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8500 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8502 int i;
8503 int equal;
8504 isl_space *space, *space2;
8505 isl_union_pw_aff *upa;
8507 mupa = isl_multi_union_pw_aff_align_params(mupa,
8508 isl_pw_aff_get_space(pa));
8509 pa = isl_pw_aff_align_params(pa,
8510 isl_multi_union_pw_aff_get_space(mupa));
8511 if (!mupa || !pa)
8512 goto error;
8514 space = isl_multi_union_pw_aff_get_space(mupa);
8515 space2 = isl_pw_aff_get_domain_space(pa);
8516 equal = isl_space_is_equal(space, space2);
8517 isl_space_free(space);
8518 isl_space_free(space2);
8519 if (equal < 0)
8520 goto error;
8521 if (!equal)
8522 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8523 "spaces don't match", goto error);
8524 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8525 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8526 "cannot determine domains", goto error);
8528 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8529 upa = isl_union_pw_aff_empty(space);
8531 for (i = 0; i < pa->n; ++i) {
8532 isl_aff *aff;
8533 isl_set *domain;
8534 isl_multi_union_pw_aff *mupa_i;
8535 isl_union_pw_aff *upa_i;
8537 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8538 domain = isl_set_copy(pa->p[i].set);
8539 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8540 aff = isl_aff_copy(pa->p[i].aff);
8541 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8542 upa = isl_union_pw_aff_union_add(upa, upa_i);
8545 isl_multi_union_pw_aff_free(mupa);
8546 isl_pw_aff_free(pa);
8547 return upa;
8548 error:
8549 isl_multi_union_pw_aff_free(mupa);
8550 isl_pw_aff_free(pa);
8551 return NULL;
8554 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8555 * with the domain of "pma".
8556 * Furthermore, the dimension of this space needs to be greater than zero,
8557 * unless the dimension of the target space of "pma" is also zero.
8558 * The result is defined over the shared domain of the elements of "mupa"
8560 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8561 __isl_take isl_multi_union_pw_aff *mupa,
8562 __isl_take isl_pw_multi_aff *pma)
8564 isl_space *space1, *space2;
8565 isl_multi_union_pw_aff *res;
8566 int equal;
8567 int i, n_out;
8569 mupa = isl_multi_union_pw_aff_align_params(mupa,
8570 isl_pw_multi_aff_get_space(pma));
8571 pma = isl_pw_multi_aff_align_params(pma,
8572 isl_multi_union_pw_aff_get_space(mupa));
8573 if (!mupa || !pma)
8574 goto error;
8576 space1 = isl_multi_union_pw_aff_get_space(mupa);
8577 space2 = isl_pw_multi_aff_get_domain_space(pma);
8578 equal = isl_space_is_equal(space1, space2);
8579 isl_space_free(space1);
8580 isl_space_free(space2);
8581 if (equal < 0)
8582 goto error;
8583 if (!equal)
8584 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8585 "spaces don't match", goto error);
8586 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8587 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8588 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8589 "cannot determine domains", goto error);
8591 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8592 res = isl_multi_union_pw_aff_alloc(space1);
8594 for (i = 0; i < n_out; ++i) {
8595 isl_pw_aff *pa;
8596 isl_union_pw_aff *upa;
8598 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8599 upa = isl_multi_union_pw_aff_apply_pw_aff(
8600 isl_multi_union_pw_aff_copy(mupa), pa);
8601 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8604 isl_pw_multi_aff_free(pma);
8605 isl_multi_union_pw_aff_free(mupa);
8606 return res;
8607 error:
8608 isl_multi_union_pw_aff_free(mupa);
8609 isl_pw_multi_aff_free(pma);
8610 return NULL;
8613 /* Compute the pullback of "mupa" by the function represented by "upma".
8614 * In other words, plug in "upma" in "mupa". The result contains
8615 * expressions defined over the domain space of "upma".
8617 * Run over all elements of "mupa" and plug in "upma" in each of them.
8619 __isl_give isl_multi_union_pw_aff *
8620 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8621 __isl_take isl_multi_union_pw_aff *mupa,
8622 __isl_take isl_union_pw_multi_aff *upma)
8624 int i, n;
8626 mupa = isl_multi_union_pw_aff_align_params(mupa,
8627 isl_union_pw_multi_aff_get_space(upma));
8628 upma = isl_union_pw_multi_aff_align_params(upma,
8629 isl_multi_union_pw_aff_get_space(mupa));
8630 if (!mupa || !upma)
8631 goto error;
8633 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8634 for (i = 0; i < n; ++i) {
8635 isl_union_pw_aff *upa;
8637 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8638 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8639 isl_union_pw_multi_aff_copy(upma));
8640 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8643 isl_union_pw_multi_aff_free(upma);
8644 return mupa;
8645 error:
8646 isl_multi_union_pw_aff_free(mupa);
8647 isl_union_pw_multi_aff_free(upma);
8648 return NULL;
8651 /* Extract the sequence of elements in "mupa" with domain space "space"
8652 * (ignoring parameters).
8654 * For the elements of "mupa" that are not defined on the specified space,
8655 * the corresponding element in the result is empty.
8657 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8658 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8660 int i, n;
8661 isl_space *space_mpa = NULL;
8662 isl_multi_pw_aff *mpa;
8664 if (!mupa || !space)
8665 goto error;
8667 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8668 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8669 space = isl_space_drop_dims(space, isl_dim_param,
8670 0, isl_space_dim(space, isl_dim_param));
8671 space = isl_space_align_params(space,
8672 isl_space_copy(space_mpa));
8673 if (!space)
8674 goto error;
8676 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8677 space_mpa);
8678 mpa = isl_multi_pw_aff_alloc(space_mpa);
8680 space = isl_space_from_domain(space);
8681 space = isl_space_add_dims(space, isl_dim_out, 1);
8682 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8683 for (i = 0; i < n; ++i) {
8684 isl_union_pw_aff *upa;
8685 isl_pw_aff *pa;
8687 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8688 pa = isl_union_pw_aff_extract_pw_aff(upa,
8689 isl_space_copy(space));
8690 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8691 isl_union_pw_aff_free(upa);
8694 isl_space_free(space);
8695 return mpa;
8696 error:
8697 isl_space_free(space_mpa);
8698 isl_space_free(space);
8699 return NULL;