add isl_union_set_list
[isl.git] / isl_aff.c
blob7c7850476350d7dbfefbb431c0c193d98ede6c98
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
24 #include <isl_seq.h>
25 #include <isl/set.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
30 #undef BASE
31 #define BASE aff
33 #include <isl_list_templ.c>
35 #undef BASE
36 #define BASE pw_aff
38 #include <isl_list_templ.c>
40 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
41 __isl_take isl_vec *v)
43 isl_aff *aff;
45 if (!ls || !v)
46 goto error;
48 aff = isl_calloc_type(v->ctx, struct isl_aff);
49 if (!aff)
50 goto error;
52 aff->ref = 1;
53 aff->ls = ls;
54 aff->v = v;
56 return aff;
57 error:
58 isl_local_space_free(ls);
59 isl_vec_free(v);
60 return NULL;
63 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
65 isl_ctx *ctx;
66 isl_vec *v;
67 unsigned total;
69 if (!ls)
70 return NULL;
72 ctx = isl_local_space_get_ctx(ls);
73 if (!isl_local_space_divs_known(ls))
74 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
75 goto error);
76 if (!isl_local_space_is_set(ls))
77 isl_die(ctx, isl_error_invalid,
78 "domain of affine expression should be a set",
79 goto error);
81 total = isl_local_space_dim(ls, isl_dim_all);
82 v = isl_vec_alloc(ctx, 1 + 1 + total);
83 return isl_aff_alloc_vec(ls, v);
84 error:
85 isl_local_space_free(ls);
86 return NULL;
89 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
91 isl_aff *aff;
93 aff = isl_aff_alloc(ls);
94 if (!aff)
95 return NULL;
97 isl_int_set_si(aff->v->el[0], 1);
98 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
100 return aff;
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
111 /* Return an affine expression defined on the specified domain
112 * that represents NaN.
114 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
116 isl_aff *aff;
118 aff = isl_aff_alloc(ls);
119 if (!aff)
120 return NULL;
122 isl_seq_clr(aff->v->el, aff->v->size);
124 return aff;
127 /* Return a piecewise affine expression defined on the specified domain
128 * that represents NaN.
130 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
132 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
135 /* Return an affine expression that is equal to "val" on
136 * domain local space "ls".
138 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
139 __isl_take isl_val *val)
141 isl_aff *aff;
143 if (!ls || !val)
144 goto error;
145 if (!isl_val_is_rat(val))
146 isl_die(isl_val_get_ctx(val), isl_error_invalid,
147 "expecting rational value", goto error);
149 aff = isl_aff_alloc(isl_local_space_copy(ls));
150 if (!aff)
151 goto error;
153 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
154 isl_int_set(aff->v->el[1], val->n);
155 isl_int_set(aff->v->el[0], val->d);
157 isl_local_space_free(ls);
158 isl_val_free(val);
159 return aff;
160 error:
161 isl_local_space_free(ls);
162 isl_val_free(val);
163 return NULL;
166 /* Return an affine expression that is equal to the specified dimension
167 * in "ls".
169 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
170 enum isl_dim_type type, unsigned pos)
172 isl_space *space;
173 isl_aff *aff;
175 if (!ls)
176 return NULL;
178 space = isl_local_space_get_space(ls);
179 if (!space)
180 goto error;
181 if (isl_space_is_map(space))
182 isl_die(isl_space_get_ctx(space), isl_error_invalid,
183 "expecting (parameter) set space", goto error);
184 if (pos >= isl_local_space_dim(ls, type))
185 isl_die(isl_space_get_ctx(space), isl_error_invalid,
186 "position out of bounds", goto error);
188 isl_space_free(space);
189 aff = isl_aff_alloc(ls);
190 if (!aff)
191 return NULL;
193 pos += isl_local_space_offset(aff->ls, type);
195 isl_int_set_si(aff->v->el[0], 1);
196 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
197 isl_int_set_si(aff->v->el[1 + pos], 1);
199 return aff;
200 error:
201 isl_local_space_free(ls);
202 isl_space_free(space);
203 return NULL;
206 /* Return a piecewise affine expression that is equal to
207 * the specified dimension in "ls".
209 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
210 enum isl_dim_type type, unsigned pos)
212 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
215 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
217 if (!aff)
218 return NULL;
220 aff->ref++;
221 return aff;
224 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
226 if (!aff)
227 return NULL;
229 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
230 isl_vec_copy(aff->v));
233 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
235 if (!aff)
236 return NULL;
238 if (aff->ref == 1)
239 return aff;
240 aff->ref--;
241 return isl_aff_dup(aff);
244 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
246 if (!aff)
247 return NULL;
249 if (--aff->ref > 0)
250 return NULL;
252 isl_local_space_free(aff->ls);
253 isl_vec_free(aff->v);
255 free(aff);
257 return NULL;
260 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
262 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
265 /* Externally, an isl_aff has a map space, but internally, the
266 * ls field corresponds to the domain of that space.
268 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
270 if (!aff)
271 return 0;
272 if (type == isl_dim_out)
273 return 1;
274 if (type == isl_dim_in)
275 type = isl_dim_set;
276 return isl_local_space_dim(aff->ls, type);
279 /* Return the position of the dimension of the given type and name
280 * in "aff".
281 * Return -1 if no such dimension can be found.
283 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
284 const char *name)
286 if (!aff)
287 return -1;
288 if (type == isl_dim_out)
289 return -1;
290 if (type == isl_dim_in)
291 type = isl_dim_set;
292 return isl_local_space_find_dim_by_name(aff->ls, type, name);
295 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
297 return aff ? isl_local_space_get_space(aff->ls) : NULL;
300 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
302 isl_space *space;
303 if (!aff)
304 return NULL;
305 space = isl_local_space_get_space(aff->ls);
306 space = isl_space_from_domain(space);
307 space = isl_space_add_dims(space, isl_dim_out, 1);
308 return space;
311 __isl_give isl_local_space *isl_aff_get_domain_local_space(
312 __isl_keep isl_aff *aff)
314 return aff ? isl_local_space_copy(aff->ls) : NULL;
317 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
319 isl_local_space *ls;
320 if (!aff)
321 return NULL;
322 ls = isl_local_space_copy(aff->ls);
323 ls = isl_local_space_from_domain(ls);
324 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
325 return ls;
328 /* Externally, an isl_aff has a map space, but internally, the
329 * ls field corresponds to the domain of that space.
331 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
332 enum isl_dim_type type, unsigned pos)
334 if (!aff)
335 return NULL;
336 if (type == isl_dim_out)
337 return NULL;
338 if (type == isl_dim_in)
339 type = isl_dim_set;
340 return isl_local_space_get_dim_name(aff->ls, type, pos);
343 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
344 __isl_take isl_space *dim)
346 aff = isl_aff_cow(aff);
347 if (!aff || !dim)
348 goto error;
350 aff->ls = isl_local_space_reset_space(aff->ls, dim);
351 if (!aff->ls)
352 return isl_aff_free(aff);
354 return aff;
355 error:
356 isl_aff_free(aff);
357 isl_space_free(dim);
358 return NULL;
361 /* Reset the space of "aff". This function is called from isl_pw_templ.c
362 * and doesn't know if the space of an element object is represented
363 * directly or through its domain. It therefore passes along both.
365 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
366 __isl_take isl_space *space, __isl_take isl_space *domain)
368 isl_space_free(space);
369 return isl_aff_reset_domain_space(aff, domain);
372 /* Reorder the coefficients of the affine expression based
373 * on the given reodering.
374 * The reordering r is assumed to have been extended with the local
375 * variables.
377 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
378 __isl_take isl_reordering *r, int n_div)
380 isl_vec *res;
381 int i;
383 if (!vec || !r)
384 goto error;
386 res = isl_vec_alloc(vec->ctx,
387 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
388 isl_seq_cpy(res->el, vec->el, 2);
389 isl_seq_clr(res->el + 2, res->size - 2);
390 for (i = 0; i < r->len; ++i)
391 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
393 isl_reordering_free(r);
394 isl_vec_free(vec);
395 return res;
396 error:
397 isl_vec_free(vec);
398 isl_reordering_free(r);
399 return NULL;
402 /* Reorder the dimensions of the domain of "aff" according
403 * to the given reordering.
405 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
406 __isl_take isl_reordering *r)
408 aff = isl_aff_cow(aff);
409 if (!aff)
410 goto error;
412 r = isl_reordering_extend(r, aff->ls->div->n_row);
413 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
414 aff->ls->div->n_row);
415 aff->ls = isl_local_space_realign(aff->ls, r);
417 if (!aff->v || !aff->ls)
418 return isl_aff_free(aff);
420 return aff;
421 error:
422 isl_aff_free(aff);
423 isl_reordering_free(r);
424 return NULL;
427 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
428 __isl_take isl_space *model)
430 if (!aff || !model)
431 goto error;
433 if (!isl_space_match(aff->ls->dim, isl_dim_param,
434 model, isl_dim_param)) {
435 isl_reordering *exp;
437 model = isl_space_drop_dims(model, isl_dim_in,
438 0, isl_space_dim(model, isl_dim_in));
439 model = isl_space_drop_dims(model, isl_dim_out,
440 0, isl_space_dim(model, isl_dim_out));
441 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
442 exp = isl_reordering_extend_space(exp,
443 isl_aff_get_domain_space(aff));
444 aff = isl_aff_realign_domain(aff, exp);
447 isl_space_free(model);
448 return aff;
449 error:
450 isl_space_free(model);
451 isl_aff_free(aff);
452 return NULL;
455 /* Is "aff" obviously equal to zero?
457 * If the denominator is zero, then "aff" is not equal to zero.
459 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
461 if (!aff)
462 return -1;
464 if (isl_int_is_zero(aff->v->el[0]))
465 return 0;
466 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
469 /* Does "aff" represent NaN?
471 int isl_aff_is_nan(__isl_keep isl_aff *aff)
473 if (!aff)
474 return -1;
476 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
479 /* Does "pa" involve any NaNs?
481 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
483 int i;
485 if (!pa)
486 return -1;
487 if (pa->n == 0)
488 return 0;
490 for (i = 0; i < pa->n; ++i) {
491 int is_nan = isl_aff_is_nan(pa->p[i].aff);
492 if (is_nan < 0 || is_nan)
493 return is_nan;
496 return 0;
499 /* Are "aff1" and "aff2" obviously equal?
501 * NaN is not equal to anything, not even to another NaN.
503 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
505 int equal;
507 if (!aff1 || !aff2)
508 return -1;
510 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
511 return 0;
513 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
514 if (equal < 0 || !equal)
515 return equal;
517 return isl_vec_is_equal(aff1->v, aff2->v);
520 /* Return the common denominator of "aff" in "v".
522 * We cannot return anything meaningful in case of a NaN.
524 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
526 if (!aff)
527 return -1;
528 if (isl_aff_is_nan(aff))
529 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
530 "cannot get denominator of NaN", return -1);
531 isl_int_set(*v, aff->v->el[0]);
532 return 0;
535 /* Return the common denominator of "aff".
537 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
539 isl_ctx *ctx;
541 if (!aff)
542 return NULL;
544 ctx = isl_aff_get_ctx(aff);
545 if (isl_aff_is_nan(aff))
546 return isl_val_nan(ctx);
547 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
550 /* Return the constant term of "aff" in "v".
552 * We cannot return anything meaningful in case of a NaN.
554 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
556 if (!aff)
557 return -1;
558 if (isl_aff_is_nan(aff))
559 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
560 "cannot get constant term of NaN", return -1);
561 isl_int_set(*v, aff->v->el[1]);
562 return 0;
565 /* Return the constant term of "aff".
567 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
569 isl_ctx *ctx;
570 isl_val *v;
572 if (!aff)
573 return NULL;
575 ctx = isl_aff_get_ctx(aff);
576 if (isl_aff_is_nan(aff))
577 return isl_val_nan(ctx);
578 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
579 return isl_val_normalize(v);
582 /* Return the coefficient of the variable of type "type" at position "pos"
583 * of "aff" in "v".
585 * We cannot return anything meaningful in case of a NaN.
587 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
588 enum isl_dim_type type, int pos, isl_int *v)
590 if (!aff)
591 return -1;
593 if (type == isl_dim_out)
594 isl_die(aff->v->ctx, isl_error_invalid,
595 "output/set dimension does not have a coefficient",
596 return -1);
597 if (type == isl_dim_in)
598 type = isl_dim_set;
600 if (pos >= isl_local_space_dim(aff->ls, type))
601 isl_die(aff->v->ctx, isl_error_invalid,
602 "position out of bounds", return -1);
604 if (isl_aff_is_nan(aff))
605 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
606 "cannot get coefficient of NaN", return -1);
607 pos += isl_local_space_offset(aff->ls, type);
608 isl_int_set(*v, aff->v->el[1 + pos]);
610 return 0;
613 /* Return the coefficient of the variable of type "type" at position "pos"
614 * of "aff".
616 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
617 enum isl_dim_type type, int pos)
619 isl_ctx *ctx;
620 isl_val *v;
622 if (!aff)
623 return NULL;
625 ctx = isl_aff_get_ctx(aff);
626 if (type == isl_dim_out)
627 isl_die(ctx, isl_error_invalid,
628 "output/set dimension does not have a coefficient",
629 return NULL);
630 if (type == isl_dim_in)
631 type = isl_dim_set;
633 if (pos >= isl_local_space_dim(aff->ls, type))
634 isl_die(ctx, isl_error_invalid,
635 "position out of bounds", return NULL);
637 if (isl_aff_is_nan(aff))
638 return isl_val_nan(ctx);
639 pos += isl_local_space_offset(aff->ls, type);
640 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
641 return isl_val_normalize(v);
644 /* Return the sign of the coefficient of the variable of type "type"
645 * at position "pos" of "aff".
647 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
648 int pos)
650 isl_ctx *ctx;
652 if (!aff)
653 return 0;
655 ctx = isl_aff_get_ctx(aff);
656 if (type == isl_dim_out)
657 isl_die(ctx, isl_error_invalid,
658 "output/set dimension does not have a coefficient",
659 return 0);
660 if (type == isl_dim_in)
661 type = isl_dim_set;
663 if (pos >= isl_local_space_dim(aff->ls, type))
664 isl_die(ctx, isl_error_invalid,
665 "position out of bounds", return 0);
667 pos += isl_local_space_offset(aff->ls, type);
668 return isl_int_sgn(aff->v->el[1 + pos]);
671 /* Replace the denominator of "aff" by "v".
673 * A NaN is unaffected by this operation.
675 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
677 if (!aff)
678 return NULL;
679 if (isl_aff_is_nan(aff))
680 return aff;
681 aff = isl_aff_cow(aff);
682 if (!aff)
683 return NULL;
685 aff->v = isl_vec_cow(aff->v);
686 if (!aff->v)
687 return isl_aff_free(aff);
689 isl_int_set(aff->v->el[0], v);
691 return aff;
694 /* Replace the numerator of the constant term of "aff" by "v".
696 * A NaN is unaffected by this operation.
698 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
700 if (!aff)
701 return NULL;
702 if (isl_aff_is_nan(aff))
703 return aff;
704 aff = isl_aff_cow(aff);
705 if (!aff)
706 return NULL;
708 aff->v = isl_vec_cow(aff->v);
709 if (!aff->v)
710 return isl_aff_free(aff);
712 isl_int_set(aff->v->el[1], v);
714 return aff;
717 /* Replace the constant term of "aff" by "v".
719 * A NaN is unaffected by this operation.
721 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
722 __isl_take isl_val *v)
724 if (!aff || !v)
725 goto error;
727 if (isl_aff_is_nan(aff)) {
728 isl_val_free(v);
729 return aff;
732 if (!isl_val_is_rat(v))
733 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
734 "expecting rational value", goto error);
736 if (isl_int_eq(aff->v->el[1], v->n) &&
737 isl_int_eq(aff->v->el[0], v->d)) {
738 isl_val_free(v);
739 return aff;
742 aff = isl_aff_cow(aff);
743 if (!aff)
744 goto error;
745 aff->v = isl_vec_cow(aff->v);
746 if (!aff->v)
747 goto error;
749 if (isl_int_eq(aff->v->el[0], v->d)) {
750 isl_int_set(aff->v->el[1], v->n);
751 } else if (isl_int_is_one(v->d)) {
752 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
753 } else {
754 isl_seq_scale(aff->v->el + 1,
755 aff->v->el + 1, v->d, aff->v->size - 1);
756 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
757 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
758 aff->v = isl_vec_normalize(aff->v);
759 if (!aff->v)
760 goto error;
763 isl_val_free(v);
764 return aff;
765 error:
766 isl_aff_free(aff);
767 isl_val_free(v);
768 return NULL;
771 /* Add "v" to the constant term of "aff".
773 * A NaN is unaffected by this operation.
775 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
777 if (isl_int_is_zero(v))
778 return aff;
780 if (!aff)
781 return NULL;
782 if (isl_aff_is_nan(aff))
783 return aff;
784 aff = isl_aff_cow(aff);
785 if (!aff)
786 return NULL;
788 aff->v = isl_vec_cow(aff->v);
789 if (!aff->v)
790 return isl_aff_free(aff);
792 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
794 return aff;
797 /* Add "v" to the constant term of "aff".
799 * A NaN is unaffected by this operation.
801 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
802 __isl_take isl_val *v)
804 if (!aff || !v)
805 goto error;
807 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
808 isl_val_free(v);
809 return aff;
812 if (!isl_val_is_rat(v))
813 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
814 "expecting rational value", goto error);
816 aff = isl_aff_cow(aff);
817 if (!aff)
818 goto error;
820 aff->v = isl_vec_cow(aff->v);
821 if (!aff->v)
822 goto error;
824 if (isl_int_is_one(v->d)) {
825 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
826 } else if (isl_int_eq(aff->v->el[0], v->d)) {
827 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
828 aff->v = isl_vec_normalize(aff->v);
829 if (!aff->v)
830 goto error;
831 } else {
832 isl_seq_scale(aff->v->el + 1,
833 aff->v->el + 1, v->d, aff->v->size - 1);
834 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
835 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
836 aff->v = isl_vec_normalize(aff->v);
837 if (!aff->v)
838 goto error;
841 isl_val_free(v);
842 return aff;
843 error:
844 isl_aff_free(aff);
845 isl_val_free(v);
846 return NULL;
849 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
851 isl_int t;
853 isl_int_init(t);
854 isl_int_set_si(t, v);
855 aff = isl_aff_add_constant(aff, t);
856 isl_int_clear(t);
858 return aff;
861 /* Add "v" to the numerator of the constant term of "aff".
863 * A NaN is unaffected by this operation.
865 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
867 if (isl_int_is_zero(v))
868 return aff;
870 if (!aff)
871 return NULL;
872 if (isl_aff_is_nan(aff))
873 return aff;
874 aff = isl_aff_cow(aff);
875 if (!aff)
876 return NULL;
878 aff->v = isl_vec_cow(aff->v);
879 if (!aff->v)
880 return isl_aff_free(aff);
882 isl_int_add(aff->v->el[1], aff->v->el[1], v);
884 return aff;
887 /* Add "v" to the numerator of the constant term of "aff".
889 * A NaN is unaffected by this operation.
891 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
893 isl_int t;
895 if (v == 0)
896 return aff;
898 isl_int_init(t);
899 isl_int_set_si(t, v);
900 aff = isl_aff_add_constant_num(aff, t);
901 isl_int_clear(t);
903 return aff;
906 /* Replace the numerator of the constant term of "aff" by "v".
908 * A NaN is unaffected by this operation.
910 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
912 if (!aff)
913 return NULL;
914 if (isl_aff_is_nan(aff))
915 return aff;
916 aff = isl_aff_cow(aff);
917 if (!aff)
918 return NULL;
920 aff->v = isl_vec_cow(aff->v);
921 if (!aff->v)
922 return isl_aff_free(aff);
924 isl_int_set_si(aff->v->el[1], v);
926 return aff;
929 /* Replace the numerator of the coefficient of the variable of type "type"
930 * at position "pos" of "aff" by "v".
932 * A NaN is unaffected by this operation.
934 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
935 enum isl_dim_type type, int pos, isl_int v)
937 if (!aff)
938 return NULL;
940 if (type == isl_dim_out)
941 isl_die(aff->v->ctx, isl_error_invalid,
942 "output/set dimension does not have a coefficient",
943 return isl_aff_free(aff));
944 if (type == isl_dim_in)
945 type = isl_dim_set;
947 if (pos >= isl_local_space_dim(aff->ls, type))
948 isl_die(aff->v->ctx, isl_error_invalid,
949 "position out of bounds", return isl_aff_free(aff));
951 if (isl_aff_is_nan(aff))
952 return aff;
953 aff = isl_aff_cow(aff);
954 if (!aff)
955 return NULL;
957 aff->v = isl_vec_cow(aff->v);
958 if (!aff->v)
959 return isl_aff_free(aff);
961 pos += isl_local_space_offset(aff->ls, type);
962 isl_int_set(aff->v->el[1 + pos], v);
964 return aff;
967 /* Replace the numerator of the coefficient of the variable of type "type"
968 * at position "pos" of "aff" by "v".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
973 enum isl_dim_type type, int pos, int v)
975 if (!aff)
976 return NULL;
978 if (type == isl_dim_out)
979 isl_die(aff->v->ctx, isl_error_invalid,
980 "output/set dimension does not have a coefficient",
981 return isl_aff_free(aff));
982 if (type == isl_dim_in)
983 type = isl_dim_set;
985 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
986 isl_die(aff->v->ctx, isl_error_invalid,
987 "position out of bounds", return isl_aff_free(aff));
989 if (isl_aff_is_nan(aff))
990 return aff;
991 pos += isl_local_space_offset(aff->ls, type);
992 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
993 return aff;
995 aff = isl_aff_cow(aff);
996 if (!aff)
997 return NULL;
999 aff->v = isl_vec_cow(aff->v);
1000 if (!aff->v)
1001 return isl_aff_free(aff);
1003 isl_int_set_si(aff->v->el[1 + pos], v);
1005 return aff;
1008 /* Replace the coefficient of the variable of type "type" at position "pos"
1009 * of "aff" by "v".
1011 * A NaN is unaffected by this operation.
1013 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1014 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1016 if (!aff || !v)
1017 goto error;
1019 if (type == isl_dim_out)
1020 isl_die(aff->v->ctx, isl_error_invalid,
1021 "output/set dimension does not have a coefficient",
1022 goto error);
1023 if (type == isl_dim_in)
1024 type = isl_dim_set;
1026 if (pos >= isl_local_space_dim(aff->ls, type))
1027 isl_die(aff->v->ctx, isl_error_invalid,
1028 "position out of bounds", goto error);
1030 if (isl_aff_is_nan(aff)) {
1031 isl_val_free(v);
1032 return aff;
1034 if (!isl_val_is_rat(v))
1035 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1036 "expecting rational value", goto error);
1038 pos += isl_local_space_offset(aff->ls, type);
1039 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1040 isl_int_eq(aff->v->el[0], v->d)) {
1041 isl_val_free(v);
1042 return aff;
1045 aff = isl_aff_cow(aff);
1046 if (!aff)
1047 goto error;
1048 aff->v = isl_vec_cow(aff->v);
1049 if (!aff->v)
1050 goto error;
1052 if (isl_int_eq(aff->v->el[0], v->d)) {
1053 isl_int_set(aff->v->el[1 + pos], v->n);
1054 } else if (isl_int_is_one(v->d)) {
1055 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1056 } else {
1057 isl_seq_scale(aff->v->el + 1,
1058 aff->v->el + 1, v->d, aff->v->size - 1);
1059 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1060 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1061 aff->v = isl_vec_normalize(aff->v);
1062 if (!aff->v)
1063 goto error;
1066 isl_val_free(v);
1067 return aff;
1068 error:
1069 isl_aff_free(aff);
1070 isl_val_free(v);
1071 return NULL;
1074 /* Add "v" to the coefficient of the variable of type "type"
1075 * at position "pos" of "aff".
1077 * A NaN is unaffected by this operation.
1079 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1080 enum isl_dim_type type, int pos, isl_int v)
1082 if (!aff)
1083 return NULL;
1085 if (type == isl_dim_out)
1086 isl_die(aff->v->ctx, isl_error_invalid,
1087 "output/set dimension does not have a coefficient",
1088 return isl_aff_free(aff));
1089 if (type == isl_dim_in)
1090 type = isl_dim_set;
1092 if (pos >= isl_local_space_dim(aff->ls, type))
1093 isl_die(aff->v->ctx, isl_error_invalid,
1094 "position out of bounds", return isl_aff_free(aff));
1096 if (isl_aff_is_nan(aff))
1097 return aff;
1098 aff = isl_aff_cow(aff);
1099 if (!aff)
1100 return NULL;
1102 aff->v = isl_vec_cow(aff->v);
1103 if (!aff->v)
1104 return isl_aff_free(aff);
1106 pos += isl_local_space_offset(aff->ls, type);
1107 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1109 return aff;
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1118 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1120 if (!aff || !v)
1121 goto error;
1123 if (isl_val_is_zero(v)) {
1124 isl_val_free(v);
1125 return aff;
1128 if (type == isl_dim_out)
1129 isl_die(aff->v->ctx, isl_error_invalid,
1130 "output/set dimension does not have a coefficient",
1131 goto error);
1132 if (type == isl_dim_in)
1133 type = isl_dim_set;
1135 if (pos >= isl_local_space_dim(aff->ls, type))
1136 isl_die(aff->v->ctx, isl_error_invalid,
1137 "position out of bounds", goto error);
1139 if (isl_aff_is_nan(aff)) {
1140 isl_val_free(v);
1141 return aff;
1143 if (!isl_val_is_rat(v))
1144 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1145 "expecting rational value", goto error);
1147 aff = isl_aff_cow(aff);
1148 if (!aff)
1149 goto error;
1151 aff->v = isl_vec_cow(aff->v);
1152 if (!aff->v)
1153 goto error;
1155 pos += isl_local_space_offset(aff->ls, type);
1156 if (isl_int_is_one(v->d)) {
1157 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1158 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1159 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1160 aff->v = isl_vec_normalize(aff->v);
1161 if (!aff->v)
1162 goto error;
1163 } else {
1164 isl_seq_scale(aff->v->el + 1,
1165 aff->v->el + 1, v->d, aff->v->size - 1);
1166 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1167 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1168 aff->v = isl_vec_normalize(aff->v);
1169 if (!aff->v)
1170 goto error;
1173 isl_val_free(v);
1174 return aff;
1175 error:
1176 isl_aff_free(aff);
1177 isl_val_free(v);
1178 return NULL;
1181 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1182 enum isl_dim_type type, int pos, int v)
1184 isl_int t;
1186 isl_int_init(t);
1187 isl_int_set_si(t, v);
1188 aff = isl_aff_add_coefficient(aff, type, pos, t);
1189 isl_int_clear(t);
1191 return aff;
1194 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1196 if (!aff)
1197 return NULL;
1199 return isl_local_space_get_div(aff->ls, pos);
1202 /* Return the negation of "aff".
1204 * As a special case, -NaN = NaN.
1206 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1208 if (!aff)
1209 return NULL;
1210 if (isl_aff_is_nan(aff))
1211 return aff;
1212 aff = isl_aff_cow(aff);
1213 if (!aff)
1214 return NULL;
1215 aff->v = isl_vec_cow(aff->v);
1216 if (!aff->v)
1217 return isl_aff_free(aff);
1219 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1221 return aff;
1224 /* Remove divs from the local space that do not appear in the affine
1225 * expression.
1226 * We currently only remove divs at the end.
1227 * Some intermediate divs may also not appear directly in the affine
1228 * expression, but we would also need to check that no other divs are
1229 * defined in terms of them.
1231 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1233 int pos;
1234 int off;
1235 int n;
1237 if (!aff)
1238 return NULL;
1240 n = isl_local_space_dim(aff->ls, isl_dim_div);
1241 off = isl_local_space_offset(aff->ls, isl_dim_div);
1243 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1244 if (pos == n)
1245 return aff;
1247 aff = isl_aff_cow(aff);
1248 if (!aff)
1249 return NULL;
1251 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1252 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1253 if (!aff->ls || !aff->v)
1254 return isl_aff_free(aff);
1256 return aff;
1259 /* Given two affine expressions "p" of length p_len (including the
1260 * denominator and the constant term) and "subs" of length subs_len,
1261 * plug in "subs" for the variable at position "pos".
1262 * The variables of "subs" and "p" are assumed to match up to subs_len,
1263 * but "p" may have additional variables.
1264 * "v" is an initialized isl_int that can be used internally.
1266 * In particular, if "p" represents the expression
1268 * (a i + g)/m
1270 * with i the variable at position "pos" and "subs" represents the expression
1272 * f/d
1274 * then the result represents the expression
1276 * (a f + d g)/(m d)
1279 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1280 int p_len, int subs_len, isl_int v)
1282 isl_int_set(v, p[1 + pos]);
1283 isl_int_set_si(p[1 + pos], 0);
1284 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1285 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1286 isl_int_mul(p[0], p[0], subs[0]);
1289 /* Look for any divs in the aff->ls with a denominator equal to one
1290 * and plug them into the affine expression and any subsequent divs
1291 * that may reference the div.
1293 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1295 int i, n;
1296 int len;
1297 isl_int v;
1298 isl_vec *vec;
1299 isl_local_space *ls;
1300 unsigned pos;
1302 if (!aff)
1303 return NULL;
1305 n = isl_local_space_dim(aff->ls, isl_dim_div);
1306 len = aff->v->size;
1307 for (i = 0; i < n; ++i) {
1308 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1309 continue;
1310 ls = isl_local_space_copy(aff->ls);
1311 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1312 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1313 vec = isl_vec_copy(aff->v);
1314 vec = isl_vec_cow(vec);
1315 if (!ls || !vec)
1316 goto error;
1318 isl_int_init(v);
1320 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1321 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1322 len, len, v);
1324 isl_int_clear(v);
1326 isl_vec_free(aff->v);
1327 aff->v = vec;
1328 isl_local_space_free(aff->ls);
1329 aff->ls = ls;
1332 return aff;
1333 error:
1334 isl_vec_free(vec);
1335 isl_local_space_free(ls);
1336 return isl_aff_free(aff);
1339 /* Look for any divs j that appear with a unit coefficient inside
1340 * the definitions of other divs i and plug them into the definitions
1341 * of the divs i.
1343 * In particular, an expression of the form
1345 * floor((f(..) + floor(g(..)/n))/m)
1347 * is simplified to
1349 * floor((n * f(..) + g(..))/(n * m))
1351 * This simplification is correct because we can move the expression
1352 * f(..) into the inner floor in the original expression to obtain
1354 * floor(floor((n * f(..) + g(..))/n)/m)
1356 * from which we can derive the simplified expression.
1358 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1360 int i, j, n;
1361 int off;
1363 if (!aff)
1364 return NULL;
1366 n = isl_local_space_dim(aff->ls, isl_dim_div);
1367 off = isl_local_space_offset(aff->ls, isl_dim_div);
1368 for (i = 1; i < n; ++i) {
1369 for (j = 0; j < i; ++j) {
1370 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1371 continue;
1372 aff->ls = isl_local_space_substitute_seq(aff->ls,
1373 isl_dim_div, j, aff->ls->div->row[j],
1374 aff->v->size, i, 1);
1375 if (!aff->ls)
1376 return isl_aff_free(aff);
1380 return aff;
1383 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1385 * Even though this function is only called on isl_affs with a single
1386 * reference, we are careful to only change aff->v and aff->ls together.
1388 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1390 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1391 isl_local_space *ls;
1392 isl_vec *v;
1394 ls = isl_local_space_copy(aff->ls);
1395 ls = isl_local_space_swap_div(ls, a, b);
1396 v = isl_vec_copy(aff->v);
1397 v = isl_vec_cow(v);
1398 if (!ls || !v)
1399 goto error;
1401 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1402 isl_vec_free(aff->v);
1403 aff->v = v;
1404 isl_local_space_free(aff->ls);
1405 aff->ls = ls;
1407 return aff;
1408 error:
1409 isl_vec_free(v);
1410 isl_local_space_free(ls);
1411 return isl_aff_free(aff);
1414 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1416 * We currently do not actually remove div "b", but simply add its
1417 * coefficient to that of "a" and then zero it out.
1419 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1421 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1423 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1424 return aff;
1426 aff->v = isl_vec_cow(aff->v);
1427 if (!aff->v)
1428 return isl_aff_free(aff);
1430 isl_int_add(aff->v->el[1 + off + a],
1431 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1432 isl_int_set_si(aff->v->el[1 + off + b], 0);
1434 return aff;
1437 /* Sort the divs in the local space of "aff" according to
1438 * the comparison function "cmp_row" in isl_local_space.c,
1439 * combining the coefficients of identical divs.
1441 * Reordering divs does not change the semantics of "aff",
1442 * so there is no need to call isl_aff_cow.
1443 * Moreover, this function is currently only called on isl_affs
1444 * with a single reference.
1446 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1448 int i, j, n;
1449 unsigned off;
1451 if (!aff)
1452 return NULL;
1454 off = isl_local_space_offset(aff->ls, isl_dim_div);
1455 n = isl_aff_dim(aff, isl_dim_div);
1456 for (i = 1; i < n; ++i) {
1457 for (j = i - 1; j >= 0; --j) {
1458 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1459 if (cmp < 0)
1460 break;
1461 if (cmp == 0)
1462 aff = merge_divs(aff, j, j + 1);
1463 else
1464 aff = swap_div(aff, j, j + 1);
1465 if (!aff)
1466 return NULL;
1470 return aff;
1473 /* Normalize the representation of "aff".
1475 * This function should only be called of "new" isl_affs, i.e.,
1476 * with only a single reference. We therefore do not need to
1477 * worry about affecting other instances.
1479 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1481 if (!aff)
1482 return NULL;
1483 aff->v = isl_vec_normalize(aff->v);
1484 if (!aff->v)
1485 return isl_aff_free(aff);
1486 aff = plug_in_integral_divs(aff);
1487 aff = plug_in_unit_divs(aff);
1488 aff = sort_divs(aff);
1489 aff = isl_aff_remove_unused_divs(aff);
1490 return aff;
1493 /* Given f, return floor(f).
1494 * If f is an integer expression, then just return f.
1495 * If f is a constant, then return the constant floor(f).
1496 * Otherwise, if f = g/m, write g = q m + r,
1497 * create a new div d = [r/m] and return the expression q + d.
1498 * The coefficients in r are taken to lie between -m/2 and m/2.
1500 * As a special case, floor(NaN) = NaN.
1502 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1504 int i;
1505 int size;
1506 isl_ctx *ctx;
1507 isl_vec *div;
1509 if (!aff)
1510 return NULL;
1512 if (isl_aff_is_nan(aff))
1513 return aff;
1514 if (isl_int_is_one(aff->v->el[0]))
1515 return aff;
1517 aff = isl_aff_cow(aff);
1518 if (!aff)
1519 return NULL;
1521 aff->v = isl_vec_cow(aff->v);
1522 if (!aff->v)
1523 return isl_aff_free(aff);
1525 if (isl_aff_is_cst(aff)) {
1526 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1527 isl_int_set_si(aff->v->el[0], 1);
1528 return aff;
1531 div = isl_vec_copy(aff->v);
1532 div = isl_vec_cow(div);
1533 if (!div)
1534 return isl_aff_free(aff);
1536 ctx = isl_aff_get_ctx(aff);
1537 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1538 for (i = 1; i < aff->v->size; ++i) {
1539 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1540 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1541 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1542 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1543 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1547 aff->ls = isl_local_space_add_div(aff->ls, div);
1548 if (!aff->ls)
1549 return isl_aff_free(aff);
1551 size = aff->v->size;
1552 aff->v = isl_vec_extend(aff->v, size + 1);
1553 if (!aff->v)
1554 return isl_aff_free(aff);
1555 isl_int_set_si(aff->v->el[0], 1);
1556 isl_int_set_si(aff->v->el[size], 1);
1558 aff = isl_aff_normalize(aff);
1560 return aff;
1563 /* Compute
1565 * aff mod m = aff - m * floor(aff/m)
1567 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1569 isl_aff *res;
1571 res = isl_aff_copy(aff);
1572 aff = isl_aff_scale_down(aff, m);
1573 aff = isl_aff_floor(aff);
1574 aff = isl_aff_scale(aff, m);
1575 res = isl_aff_sub(res, aff);
1577 return res;
1580 /* Compute
1582 * aff mod m = aff - m * floor(aff/m)
1584 * with m an integer value.
1586 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1587 __isl_take isl_val *m)
1589 isl_aff *res;
1591 if (!aff || !m)
1592 goto error;
1594 if (!isl_val_is_int(m))
1595 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1596 "expecting integer modulo", goto error);
1598 res = isl_aff_copy(aff);
1599 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1600 aff = isl_aff_floor(aff);
1601 aff = isl_aff_scale_val(aff, m);
1602 res = isl_aff_sub(res, aff);
1604 return res;
1605 error:
1606 isl_aff_free(aff);
1607 isl_val_free(m);
1608 return NULL;
1611 /* Compute
1613 * pwaff mod m = pwaff - m * floor(pwaff/m)
1615 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1617 isl_pw_aff *res;
1619 res = isl_pw_aff_copy(pwaff);
1620 pwaff = isl_pw_aff_scale_down(pwaff, m);
1621 pwaff = isl_pw_aff_floor(pwaff);
1622 pwaff = isl_pw_aff_scale(pwaff, m);
1623 res = isl_pw_aff_sub(res, pwaff);
1625 return res;
1628 /* Compute
1630 * pa mod m = pa - m * floor(pa/m)
1632 * with m an integer value.
1634 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1635 __isl_take isl_val *m)
1637 if (!pa || !m)
1638 goto error;
1639 if (!isl_val_is_int(m))
1640 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1641 "expecting integer modulo", goto error);
1642 pa = isl_pw_aff_mod(pa, m->n);
1643 isl_val_free(m);
1644 return pa;
1645 error:
1646 isl_pw_aff_free(pa);
1647 isl_val_free(m);
1648 return NULL;
1651 /* Given f, return ceil(f).
1652 * If f is an integer expression, then just return f.
1653 * Otherwise, let f be the expression
1655 * e/m
1657 * then return
1659 * floor((e + m - 1)/m)
1661 * As a special case, ceil(NaN) = NaN.
1663 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1665 if (!aff)
1666 return NULL;
1668 if (isl_aff_is_nan(aff))
1669 return aff;
1670 if (isl_int_is_one(aff->v->el[0]))
1671 return aff;
1673 aff = isl_aff_cow(aff);
1674 if (!aff)
1675 return NULL;
1676 aff->v = isl_vec_cow(aff->v);
1677 if (!aff->v)
1678 return isl_aff_free(aff);
1680 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1681 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1682 aff = isl_aff_floor(aff);
1684 return aff;
1687 /* Apply the expansion computed by isl_merge_divs.
1688 * The expansion itself is given by "exp" while the resulting
1689 * list of divs is given by "div".
1691 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1692 __isl_take isl_mat *div, int *exp)
1694 int i, j;
1695 int old_n_div;
1696 int new_n_div;
1697 int offset;
1699 aff = isl_aff_cow(aff);
1700 if (!aff || !div)
1701 goto error;
1703 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1704 new_n_div = isl_mat_rows(div);
1705 if (new_n_div < old_n_div)
1706 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1707 "not an expansion", goto error);
1709 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1710 if (!aff->v)
1711 goto error;
1713 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1714 j = old_n_div - 1;
1715 for (i = new_n_div - 1; i >= 0; --i) {
1716 if (j >= 0 && exp[j] == i) {
1717 if (i != j)
1718 isl_int_swap(aff->v->el[offset + i],
1719 aff->v->el[offset + j]);
1720 j--;
1721 } else
1722 isl_int_set_si(aff->v->el[offset + i], 0);
1725 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1726 if (!aff->ls)
1727 goto error;
1728 isl_mat_free(div);
1729 return aff;
1730 error:
1731 isl_aff_free(aff);
1732 isl_mat_free(div);
1733 return NULL;
1736 /* Add two affine expressions that live in the same local space.
1738 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1739 __isl_take isl_aff *aff2)
1741 isl_int gcd, f;
1743 aff1 = isl_aff_cow(aff1);
1744 if (!aff1 || !aff2)
1745 goto error;
1747 aff1->v = isl_vec_cow(aff1->v);
1748 if (!aff1->v)
1749 goto error;
1751 isl_int_init(gcd);
1752 isl_int_init(f);
1753 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1754 isl_int_divexact(f, aff2->v->el[0], gcd);
1755 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1756 isl_int_divexact(f, aff1->v->el[0], gcd);
1757 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1758 isl_int_divexact(f, aff2->v->el[0], gcd);
1759 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1760 isl_int_clear(f);
1761 isl_int_clear(gcd);
1763 isl_aff_free(aff2);
1764 return aff1;
1765 error:
1766 isl_aff_free(aff1);
1767 isl_aff_free(aff2);
1768 return NULL;
1771 /* Return the sum of "aff1" and "aff2".
1773 * If either of the two is NaN, then the result is NaN.
1775 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1776 __isl_take isl_aff *aff2)
1778 isl_ctx *ctx;
1779 int *exp1 = NULL;
1780 int *exp2 = NULL;
1781 isl_mat *div;
1782 int n_div1, n_div2;
1784 if (!aff1 || !aff2)
1785 goto error;
1787 ctx = isl_aff_get_ctx(aff1);
1788 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1789 isl_die(ctx, isl_error_invalid,
1790 "spaces don't match", goto error);
1792 if (isl_aff_is_nan(aff1)) {
1793 isl_aff_free(aff2);
1794 return aff1;
1796 if (isl_aff_is_nan(aff2)) {
1797 isl_aff_free(aff1);
1798 return aff2;
1801 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1802 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1803 if (n_div1 == 0 && n_div2 == 0)
1804 return add_expanded(aff1, aff2);
1806 exp1 = isl_alloc_array(ctx, int, n_div1);
1807 exp2 = isl_alloc_array(ctx, int, n_div2);
1808 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1809 goto error;
1811 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1812 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1813 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1814 free(exp1);
1815 free(exp2);
1817 return add_expanded(aff1, aff2);
1818 error:
1819 free(exp1);
1820 free(exp2);
1821 isl_aff_free(aff1);
1822 isl_aff_free(aff2);
1823 return NULL;
1826 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1827 __isl_take isl_aff *aff2)
1829 return isl_aff_add(aff1, isl_aff_neg(aff2));
1832 /* Return the result of scaling "aff" by a factor of "f".
1834 * As a special case, f * NaN = NaN.
1836 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1838 isl_int gcd;
1840 if (!aff)
1841 return NULL;
1842 if (isl_aff_is_nan(aff))
1843 return aff;
1845 if (isl_int_is_one(f))
1846 return aff;
1848 aff = isl_aff_cow(aff);
1849 if (!aff)
1850 return NULL;
1851 aff->v = isl_vec_cow(aff->v);
1852 if (!aff->v)
1853 return isl_aff_free(aff);
1855 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1856 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1857 return aff;
1860 isl_int_init(gcd);
1861 isl_int_gcd(gcd, aff->v->el[0], f);
1862 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1863 isl_int_divexact(gcd, f, gcd);
1864 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1865 isl_int_clear(gcd);
1867 return aff;
1870 /* Multiple "aff" by "v".
1872 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1873 __isl_take isl_val *v)
1875 if (!aff || !v)
1876 goto error;
1878 if (isl_val_is_one(v)) {
1879 isl_val_free(v);
1880 return aff;
1883 if (!isl_val_is_rat(v))
1884 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1885 "expecting rational factor", goto error);
1887 aff = isl_aff_scale(aff, v->n);
1888 aff = isl_aff_scale_down(aff, v->d);
1890 isl_val_free(v);
1891 return aff;
1892 error:
1893 isl_aff_free(aff);
1894 isl_val_free(v);
1895 return NULL;
1898 /* Return the result of scaling "aff" down by a factor of "f".
1900 * As a special case, NaN/f = NaN.
1902 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1904 isl_int gcd;
1906 if (!aff)
1907 return NULL;
1908 if (isl_aff_is_nan(aff))
1909 return aff;
1911 if (isl_int_is_one(f))
1912 return aff;
1914 aff = isl_aff_cow(aff);
1915 if (!aff)
1916 return NULL;
1918 if (isl_int_is_zero(f))
1919 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1920 "cannot scale down by zero", return isl_aff_free(aff));
1922 aff->v = isl_vec_cow(aff->v);
1923 if (!aff->v)
1924 return isl_aff_free(aff);
1926 isl_int_init(gcd);
1927 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1928 isl_int_gcd(gcd, gcd, f);
1929 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1930 isl_int_divexact(gcd, f, gcd);
1931 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1932 isl_int_clear(gcd);
1934 return aff;
1937 /* Divide "aff" by "v".
1939 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1940 __isl_take isl_val *v)
1942 if (!aff || !v)
1943 goto error;
1945 if (isl_val_is_one(v)) {
1946 isl_val_free(v);
1947 return aff;
1950 if (!isl_val_is_rat(v))
1951 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1952 "expecting rational factor", goto error);
1953 if (!isl_val_is_pos(v))
1954 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1955 "factor needs to be positive", goto error);
1957 aff = isl_aff_scale(aff, v->d);
1958 aff = isl_aff_scale_down(aff, v->n);
1960 isl_val_free(v);
1961 return aff;
1962 error:
1963 isl_aff_free(aff);
1964 isl_val_free(v);
1965 return NULL;
1968 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1970 isl_int v;
1972 if (f == 1)
1973 return aff;
1975 isl_int_init(v);
1976 isl_int_set_ui(v, f);
1977 aff = isl_aff_scale_down(aff, v);
1978 isl_int_clear(v);
1980 return aff;
1983 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1984 enum isl_dim_type type, unsigned pos, const char *s)
1986 aff = isl_aff_cow(aff);
1987 if (!aff)
1988 return NULL;
1989 if (type == isl_dim_out)
1990 isl_die(aff->v->ctx, isl_error_invalid,
1991 "cannot set name of output/set dimension",
1992 return isl_aff_free(aff));
1993 if (type == isl_dim_in)
1994 type = isl_dim_set;
1995 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1996 if (!aff->ls)
1997 return isl_aff_free(aff);
1999 return aff;
2002 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2003 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2005 aff = isl_aff_cow(aff);
2006 if (!aff)
2007 goto error;
2008 if (type == isl_dim_out)
2009 isl_die(aff->v->ctx, isl_error_invalid,
2010 "cannot set name of output/set dimension",
2011 goto error);
2012 if (type == isl_dim_in)
2013 type = isl_dim_set;
2014 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2015 if (!aff->ls)
2016 return isl_aff_free(aff);
2018 return aff;
2019 error:
2020 isl_id_free(id);
2021 isl_aff_free(aff);
2022 return NULL;
2025 /* Replace the identifier of the input tuple of "aff" by "id".
2026 * type is currently required to be equal to isl_dim_in
2028 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2029 enum isl_dim_type type, __isl_take isl_id *id)
2031 aff = isl_aff_cow(aff);
2032 if (!aff)
2033 goto error;
2034 if (type != isl_dim_out)
2035 isl_die(aff->v->ctx, isl_error_invalid,
2036 "cannot only set id of input tuple", goto error);
2037 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2038 if (!aff->ls)
2039 return isl_aff_free(aff);
2041 return aff;
2042 error:
2043 isl_id_free(id);
2044 isl_aff_free(aff);
2045 return NULL;
2048 /* Exploit the equalities in "eq" to simplify the affine expression
2049 * and the expressions of the integer divisions in the local space.
2050 * The integer divisions in this local space are assumed to appear
2051 * as regular dimensions in "eq".
2053 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2054 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2056 int i, j;
2057 unsigned total;
2058 unsigned n_div;
2060 if (!eq)
2061 goto error;
2062 if (eq->n_eq == 0) {
2063 isl_basic_set_free(eq);
2064 return aff;
2067 aff = isl_aff_cow(aff);
2068 if (!aff)
2069 goto error;
2071 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2072 isl_basic_set_copy(eq));
2073 aff->v = isl_vec_cow(aff->v);
2074 if (!aff->ls || !aff->v)
2075 goto error;
2077 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2078 n_div = eq->n_div;
2079 for (i = 0; i < eq->n_eq; ++i) {
2080 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2081 if (j < 0 || j == 0 || j >= total)
2082 continue;
2084 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2085 &aff->v->el[0]);
2088 isl_basic_set_free(eq);
2089 aff = isl_aff_normalize(aff);
2090 return aff;
2091 error:
2092 isl_basic_set_free(eq);
2093 isl_aff_free(aff);
2094 return NULL;
2097 /* Exploit the equalities in "eq" to simplify the affine expression
2098 * and the expressions of the integer divisions in the local space.
2100 static __isl_give isl_aff *isl_aff_substitute_equalities(
2101 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2103 int n_div;
2105 if (!aff || !eq)
2106 goto error;
2107 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2108 if (n_div > 0)
2109 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2110 return isl_aff_substitute_equalities_lifted(aff, eq);
2111 error:
2112 isl_basic_set_free(eq);
2113 isl_aff_free(aff);
2114 return NULL;
2117 /* Look for equalities among the variables shared by context and aff
2118 * and the integer divisions of aff, if any.
2119 * The equalities are then used to eliminate coefficients and/or integer
2120 * divisions from aff.
2122 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2123 __isl_take isl_set *context)
2125 isl_basic_set *hull;
2126 int n_div;
2128 if (!aff)
2129 goto error;
2130 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2131 if (n_div > 0) {
2132 isl_basic_set *bset;
2133 isl_local_space *ls;
2134 context = isl_set_add_dims(context, isl_dim_set, n_div);
2135 ls = isl_aff_get_domain_local_space(aff);
2136 bset = isl_basic_set_from_local_space(ls);
2137 bset = isl_basic_set_lift(bset);
2138 bset = isl_basic_set_flatten(bset);
2139 context = isl_set_intersect(context,
2140 isl_set_from_basic_set(bset));
2143 hull = isl_set_affine_hull(context);
2144 return isl_aff_substitute_equalities_lifted(aff, hull);
2145 error:
2146 isl_aff_free(aff);
2147 isl_set_free(context);
2148 return NULL;
2151 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2152 __isl_take isl_set *context)
2154 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2155 dom_context = isl_set_intersect_params(dom_context, context);
2156 return isl_aff_gist(aff, dom_context);
2159 /* Return a basic set containing those elements in the space
2160 * of aff where it is non-negative.
2161 * If "rational" is set, then return a rational basic set.
2163 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2165 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2166 __isl_take isl_aff *aff, int rational)
2168 isl_constraint *ineq;
2169 isl_basic_set *bset;
2171 if (!aff)
2172 return NULL;
2173 if (isl_aff_is_nan(aff)) {
2174 isl_space *space = isl_aff_get_domain_space(aff);
2175 isl_aff_free(aff);
2176 return isl_basic_set_empty(space);
2179 ineq = isl_inequality_from_aff(aff);
2181 bset = isl_basic_set_from_constraint(ineq);
2182 if (rational)
2183 bset = isl_basic_set_set_rational(bset);
2184 bset = isl_basic_set_simplify(bset);
2185 return bset;
2188 /* Return a basic set containing those elements in the space
2189 * of aff where it is non-negative.
2191 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2193 return aff_nonneg_basic_set(aff, 0);
2196 /* Return a basic set containing those elements in the domain space
2197 * of aff where it is negative.
2199 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2201 aff = isl_aff_neg(aff);
2202 aff = isl_aff_add_constant_num_si(aff, -1);
2203 return isl_aff_nonneg_basic_set(aff);
2206 /* Return a basic set containing those elements in the space
2207 * of aff where it is zero.
2208 * If "rational" is set, then return a rational basic set.
2210 * If "aff" is NaN, then it is not zero.
2212 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2213 int rational)
2215 isl_constraint *ineq;
2216 isl_basic_set *bset;
2218 if (!aff)
2219 return NULL;
2220 if (isl_aff_is_nan(aff)) {
2221 isl_space *space = isl_aff_get_domain_space(aff);
2222 isl_aff_free(aff);
2223 return isl_basic_set_empty(space);
2226 ineq = isl_equality_from_aff(aff);
2228 bset = isl_basic_set_from_constraint(ineq);
2229 if (rational)
2230 bset = isl_basic_set_set_rational(bset);
2231 bset = isl_basic_set_simplify(bset);
2232 return bset;
2235 /* Return a basic set containing those elements in the space
2236 * of aff where it is zero.
2238 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2240 return aff_zero_basic_set(aff, 0);
2243 /* Return a basic set containing those elements in the shared space
2244 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2246 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2247 __isl_take isl_aff *aff2)
2249 aff1 = isl_aff_sub(aff1, aff2);
2251 return isl_aff_nonneg_basic_set(aff1);
2254 /* Return a basic set containing those elements in the shared space
2255 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2257 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2258 __isl_take isl_aff *aff2)
2260 return isl_aff_ge_basic_set(aff2, aff1);
2263 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2264 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2266 aff1 = isl_aff_add(aff1, aff2);
2267 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2268 return aff1;
2271 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2273 if (!aff)
2274 return -1;
2276 return 0;
2279 /* Check whether the given affine expression has non-zero coefficient
2280 * for any dimension in the given range or if any of these dimensions
2281 * appear with non-zero coefficients in any of the integer divisions
2282 * involved in the affine expression.
2284 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2285 enum isl_dim_type type, unsigned first, unsigned n)
2287 int i;
2288 isl_ctx *ctx;
2289 int *active = NULL;
2290 int involves = 0;
2292 if (!aff)
2293 return -1;
2294 if (n == 0)
2295 return 0;
2297 ctx = isl_aff_get_ctx(aff);
2298 if (first + n > isl_aff_dim(aff, type))
2299 isl_die(ctx, isl_error_invalid,
2300 "range out of bounds", return -1);
2302 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2303 if (!active)
2304 goto error;
2306 first += isl_local_space_offset(aff->ls, type) - 1;
2307 for (i = 0; i < n; ++i)
2308 if (active[first + i]) {
2309 involves = 1;
2310 break;
2313 free(active);
2315 return involves;
2316 error:
2317 free(active);
2318 return -1;
2321 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2322 enum isl_dim_type type, unsigned first, unsigned n)
2324 isl_ctx *ctx;
2326 if (!aff)
2327 return NULL;
2328 if (type == isl_dim_out)
2329 isl_die(aff->v->ctx, isl_error_invalid,
2330 "cannot drop output/set dimension",
2331 return isl_aff_free(aff));
2332 if (type == isl_dim_in)
2333 type = isl_dim_set;
2334 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2335 return aff;
2337 ctx = isl_aff_get_ctx(aff);
2338 if (first + n > isl_local_space_dim(aff->ls, type))
2339 isl_die(ctx, isl_error_invalid, "range out of bounds",
2340 return isl_aff_free(aff));
2342 aff = isl_aff_cow(aff);
2343 if (!aff)
2344 return NULL;
2346 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2347 if (!aff->ls)
2348 return isl_aff_free(aff);
2350 first += 1 + isl_local_space_offset(aff->ls, type);
2351 aff->v = isl_vec_drop_els(aff->v, first, n);
2352 if (!aff->v)
2353 return isl_aff_free(aff);
2355 return aff;
2358 /* Project the domain of the affine expression onto its parameter space.
2359 * The affine expression may not involve any of the domain dimensions.
2361 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2363 isl_space *space;
2364 unsigned n;
2365 int involves;
2367 n = isl_aff_dim(aff, isl_dim_in);
2368 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2369 if (involves < 0)
2370 return isl_aff_free(aff);
2371 if (involves)
2372 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2373 "affine expression involves some of the domain dimensions",
2374 return isl_aff_free(aff));
2375 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2376 space = isl_aff_get_domain_space(aff);
2377 space = isl_space_params(space);
2378 aff = isl_aff_reset_domain_space(aff, space);
2379 return aff;
2382 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2383 enum isl_dim_type type, unsigned first, unsigned n)
2385 isl_ctx *ctx;
2387 if (!aff)
2388 return NULL;
2389 if (type == isl_dim_out)
2390 isl_die(aff->v->ctx, isl_error_invalid,
2391 "cannot insert output/set dimensions",
2392 return isl_aff_free(aff));
2393 if (type == isl_dim_in)
2394 type = isl_dim_set;
2395 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2396 return aff;
2398 ctx = isl_aff_get_ctx(aff);
2399 if (first > isl_local_space_dim(aff->ls, type))
2400 isl_die(ctx, isl_error_invalid, "position out of bounds",
2401 return isl_aff_free(aff));
2403 aff = isl_aff_cow(aff);
2404 if (!aff)
2405 return NULL;
2407 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2408 if (!aff->ls)
2409 return isl_aff_free(aff);
2411 first += 1 + isl_local_space_offset(aff->ls, type);
2412 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2413 if (!aff->v)
2414 return isl_aff_free(aff);
2416 return aff;
2419 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2420 enum isl_dim_type type, unsigned n)
2422 unsigned pos;
2424 pos = isl_aff_dim(aff, type);
2426 return isl_aff_insert_dims(aff, type, pos, n);
2429 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2430 enum isl_dim_type type, unsigned n)
2432 unsigned pos;
2434 pos = isl_pw_aff_dim(pwaff, type);
2436 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2439 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2440 * to dimensions of "dst_type" at "dst_pos".
2442 * We only support moving input dimensions to parameters and vice versa.
2444 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2445 enum isl_dim_type dst_type, unsigned dst_pos,
2446 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2448 unsigned g_dst_pos;
2449 unsigned g_src_pos;
2451 if (!aff)
2452 return NULL;
2453 if (n == 0 &&
2454 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2455 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2456 return aff;
2458 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2459 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2460 "cannot move output/set dimension", isl_aff_free(aff));
2461 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2462 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2463 "cannot move divs", isl_aff_free(aff));
2464 if (dst_type == isl_dim_in)
2465 dst_type = isl_dim_set;
2466 if (src_type == isl_dim_in)
2467 src_type = isl_dim_set;
2469 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2470 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2471 "range out of bounds", isl_aff_free(aff));
2472 if (dst_type == src_type)
2473 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2474 "moving dims within the same type not supported",
2475 isl_aff_free(aff));
2477 aff = isl_aff_cow(aff);
2478 if (!aff)
2479 return NULL;
2481 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2482 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2483 if (dst_type > src_type)
2484 g_dst_pos -= n;
2486 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2487 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2488 src_type, src_pos, n);
2489 if (!aff->v || !aff->ls)
2490 return isl_aff_free(aff);
2492 aff = sort_divs(aff);
2494 return aff;
2497 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2499 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2500 return isl_pw_aff_alloc(dom, aff);
2503 #undef PW
2504 #define PW isl_pw_aff
2505 #undef EL
2506 #define EL isl_aff
2507 #undef EL_IS_ZERO
2508 #define EL_IS_ZERO is_empty
2509 #undef ZERO
2510 #define ZERO empty
2511 #undef IS_ZERO
2512 #define IS_ZERO is_empty
2513 #undef FIELD
2514 #define FIELD aff
2515 #undef DEFAULT_IS_ZERO
2516 #define DEFAULT_IS_ZERO 0
2518 #define NO_EVAL
2519 #define NO_OPT
2520 #define NO_LIFT
2521 #define NO_MORPH
2523 #include <isl_pw_templ.c>
2525 static __isl_give isl_set *align_params_pw_pw_set_and(
2526 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2527 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2528 __isl_take isl_pw_aff *pwaff2))
2530 if (!pwaff1 || !pwaff2)
2531 goto error;
2532 if (isl_space_match(pwaff1->dim, isl_dim_param,
2533 pwaff2->dim, isl_dim_param))
2534 return fn(pwaff1, pwaff2);
2535 if (!isl_space_has_named_params(pwaff1->dim) ||
2536 !isl_space_has_named_params(pwaff2->dim))
2537 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2538 "unaligned unnamed parameters", goto error);
2539 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2540 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2541 return fn(pwaff1, pwaff2);
2542 error:
2543 isl_pw_aff_free(pwaff1);
2544 isl_pw_aff_free(pwaff2);
2545 return NULL;
2548 /* Compute a piecewise quasi-affine expression with a domain that
2549 * is the union of those of pwaff1 and pwaff2 and such that on each
2550 * cell, the quasi-affine expression is the better (according to cmp)
2551 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2552 * is defined on a given cell, then the associated expression
2553 * is the defined one.
2555 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2556 __isl_take isl_pw_aff *pwaff2,
2557 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2558 __isl_take isl_aff *aff2))
2560 int i, j, n;
2561 isl_pw_aff *res;
2562 isl_ctx *ctx;
2563 isl_set *set;
2565 if (!pwaff1 || !pwaff2)
2566 goto error;
2568 ctx = isl_space_get_ctx(pwaff1->dim);
2569 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2570 isl_die(ctx, isl_error_invalid,
2571 "arguments should live in same space", goto error);
2573 if (isl_pw_aff_is_empty(pwaff1)) {
2574 isl_pw_aff_free(pwaff1);
2575 return pwaff2;
2578 if (isl_pw_aff_is_empty(pwaff2)) {
2579 isl_pw_aff_free(pwaff2);
2580 return pwaff1;
2583 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2584 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2586 for (i = 0; i < pwaff1->n; ++i) {
2587 set = isl_set_copy(pwaff1->p[i].set);
2588 for (j = 0; j < pwaff2->n; ++j) {
2589 struct isl_set *common;
2590 isl_set *better;
2592 common = isl_set_intersect(
2593 isl_set_copy(pwaff1->p[i].set),
2594 isl_set_copy(pwaff2->p[j].set));
2595 better = isl_set_from_basic_set(cmp(
2596 isl_aff_copy(pwaff2->p[j].aff),
2597 isl_aff_copy(pwaff1->p[i].aff)));
2598 better = isl_set_intersect(common, better);
2599 if (isl_set_plain_is_empty(better)) {
2600 isl_set_free(better);
2601 continue;
2603 set = isl_set_subtract(set, isl_set_copy(better));
2605 res = isl_pw_aff_add_piece(res, better,
2606 isl_aff_copy(pwaff2->p[j].aff));
2608 res = isl_pw_aff_add_piece(res, set,
2609 isl_aff_copy(pwaff1->p[i].aff));
2612 for (j = 0; j < pwaff2->n; ++j) {
2613 set = isl_set_copy(pwaff2->p[j].set);
2614 for (i = 0; i < pwaff1->n; ++i)
2615 set = isl_set_subtract(set,
2616 isl_set_copy(pwaff1->p[i].set));
2617 res = isl_pw_aff_add_piece(res, set,
2618 isl_aff_copy(pwaff2->p[j].aff));
2621 isl_pw_aff_free(pwaff1);
2622 isl_pw_aff_free(pwaff2);
2624 return res;
2625 error:
2626 isl_pw_aff_free(pwaff1);
2627 isl_pw_aff_free(pwaff2);
2628 return NULL;
2631 /* Compute a piecewise quasi-affine expression with a domain that
2632 * is the union of those of pwaff1 and pwaff2 and such that on each
2633 * cell, the quasi-affine expression is the maximum of those of pwaff1
2634 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2635 * cell, then the associated expression is the defined one.
2637 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2638 __isl_take isl_pw_aff *pwaff2)
2640 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2643 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2644 __isl_take isl_pw_aff *pwaff2)
2646 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2647 &pw_aff_union_max);
2650 /* Compute a piecewise quasi-affine expression with a domain that
2651 * is the union of those of pwaff1 and pwaff2 and such that on each
2652 * cell, the quasi-affine expression is the minimum of those of pwaff1
2653 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2654 * cell, then the associated expression is the defined one.
2656 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2657 __isl_take isl_pw_aff *pwaff2)
2659 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2662 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2663 __isl_take isl_pw_aff *pwaff2)
2665 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2666 &pw_aff_union_min);
2669 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2670 __isl_take isl_pw_aff *pwaff2, int max)
2672 if (max)
2673 return isl_pw_aff_union_max(pwaff1, pwaff2);
2674 else
2675 return isl_pw_aff_union_min(pwaff1, pwaff2);
2678 /* Construct a map with as domain the domain of pwaff and
2679 * one-dimensional range corresponding to the affine expressions.
2681 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2683 int i;
2684 isl_space *dim;
2685 isl_map *map;
2687 if (!pwaff)
2688 return NULL;
2690 dim = isl_pw_aff_get_space(pwaff);
2691 map = isl_map_empty(dim);
2693 for (i = 0; i < pwaff->n; ++i) {
2694 isl_basic_map *bmap;
2695 isl_map *map_i;
2697 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2698 map_i = isl_map_from_basic_map(bmap);
2699 map_i = isl_map_intersect_domain(map_i,
2700 isl_set_copy(pwaff->p[i].set));
2701 map = isl_map_union_disjoint(map, map_i);
2704 isl_pw_aff_free(pwaff);
2706 return map;
2709 /* Construct a map with as domain the domain of pwaff and
2710 * one-dimensional range corresponding to the affine expressions.
2712 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2714 if (!pwaff)
2715 return NULL;
2716 if (isl_space_is_set(pwaff->dim))
2717 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2718 "space of input is not a map", goto error);
2719 return map_from_pw_aff(pwaff);
2720 error:
2721 isl_pw_aff_free(pwaff);
2722 return NULL;
2725 /* Construct a one-dimensional set with as parameter domain
2726 * the domain of pwaff and the single set dimension
2727 * corresponding to the affine expressions.
2729 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2731 if (!pwaff)
2732 return NULL;
2733 if (!isl_space_is_set(pwaff->dim))
2734 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2735 "space of input is not a set", goto error);
2736 return map_from_pw_aff(pwaff);
2737 error:
2738 isl_pw_aff_free(pwaff);
2739 return NULL;
2742 /* Return a set containing those elements in the domain
2743 * of pwaff where it is non-negative.
2745 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2747 int i;
2748 isl_set *set;
2750 if (!pwaff)
2751 return NULL;
2753 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2755 for (i = 0; i < pwaff->n; ++i) {
2756 isl_basic_set *bset;
2757 isl_set *set_i;
2758 int rational;
2760 rational = isl_set_has_rational(pwaff->p[i].set);
2761 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2762 rational);
2763 set_i = isl_set_from_basic_set(bset);
2764 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2765 set = isl_set_union_disjoint(set, set_i);
2768 isl_pw_aff_free(pwaff);
2770 return set;
2773 /* Return a set containing those elements in the domain
2774 * of pwaff where it is zero (if complement is 0) or not zero
2775 * (if complement is 1).
2777 * The pieces with a NaN never belong to the result since
2778 * NaN is neither zero nor non-zero.
2780 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2781 int complement)
2783 int i;
2784 isl_set *set;
2786 if (!pwaff)
2787 return NULL;
2789 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2791 for (i = 0; i < pwaff->n; ++i) {
2792 isl_basic_set *bset;
2793 isl_set *set_i, *zero;
2794 int rational;
2796 if (isl_aff_is_nan(pwaff->p[i].aff))
2797 continue;
2799 rational = isl_set_has_rational(pwaff->p[i].set);
2800 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2801 rational);
2802 zero = isl_set_from_basic_set(bset);
2803 set_i = isl_set_copy(pwaff->p[i].set);
2804 if (complement)
2805 set_i = isl_set_subtract(set_i, zero);
2806 else
2807 set_i = isl_set_intersect(set_i, zero);
2808 set = isl_set_union_disjoint(set, set_i);
2811 isl_pw_aff_free(pwaff);
2813 return set;
2816 /* Return a set containing those elements in the domain
2817 * of pwaff where it is zero.
2819 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2821 return pw_aff_zero_set(pwaff, 0);
2824 /* Return a set containing those elements in the domain
2825 * of pwaff where it is not zero.
2827 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2829 return pw_aff_zero_set(pwaff, 1);
2832 /* Return a set containing those elements in the shared domain
2833 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2835 * We compute the difference on the shared domain and then construct
2836 * the set of values where this difference is non-negative.
2837 * If strict is set, we first subtract 1 from the difference.
2838 * If equal is set, we only return the elements where pwaff1 and pwaff2
2839 * are equal.
2841 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2842 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2844 isl_set *set1, *set2;
2846 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2847 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2848 set1 = isl_set_intersect(set1, set2);
2849 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2850 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2851 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2853 if (strict) {
2854 isl_space *dim = isl_set_get_space(set1);
2855 isl_aff *aff;
2856 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2857 aff = isl_aff_add_constant_si(aff, -1);
2858 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2859 } else
2860 isl_set_free(set1);
2862 if (equal)
2863 return isl_pw_aff_zero_set(pwaff1);
2864 return isl_pw_aff_nonneg_set(pwaff1);
2867 /* Return a set containing those elements in the shared domain
2868 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2870 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2871 __isl_take isl_pw_aff *pwaff2)
2873 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2876 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2877 __isl_take isl_pw_aff *pwaff2)
2879 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2882 /* Return a set containing those elements in the shared domain
2883 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2885 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2886 __isl_take isl_pw_aff *pwaff2)
2888 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2891 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2892 __isl_take isl_pw_aff *pwaff2)
2894 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2897 /* Return a set containing those elements in the shared domain
2898 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2900 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2901 __isl_take isl_pw_aff *pwaff2)
2903 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2906 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2907 __isl_take isl_pw_aff *pwaff2)
2909 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2912 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2913 __isl_take isl_pw_aff *pwaff2)
2915 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2918 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2919 __isl_take isl_pw_aff *pwaff2)
2921 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2924 /* Return a set containing those elements in the shared domain
2925 * of the elements of list1 and list2 where each element in list1
2926 * has the relation specified by "fn" with each element in list2.
2928 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2929 __isl_take isl_pw_aff_list *list2,
2930 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2931 __isl_take isl_pw_aff *pwaff2))
2933 int i, j;
2934 isl_ctx *ctx;
2935 isl_set *set;
2937 if (!list1 || !list2)
2938 goto error;
2940 ctx = isl_pw_aff_list_get_ctx(list1);
2941 if (list1->n < 1 || list2->n < 1)
2942 isl_die(ctx, isl_error_invalid,
2943 "list should contain at least one element", goto error);
2945 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2946 for (i = 0; i < list1->n; ++i)
2947 for (j = 0; j < list2->n; ++j) {
2948 isl_set *set_ij;
2950 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2951 isl_pw_aff_copy(list2->p[j]));
2952 set = isl_set_intersect(set, set_ij);
2955 isl_pw_aff_list_free(list1);
2956 isl_pw_aff_list_free(list2);
2957 return set;
2958 error:
2959 isl_pw_aff_list_free(list1);
2960 isl_pw_aff_list_free(list2);
2961 return NULL;
2964 /* Return a set containing those elements in the shared domain
2965 * of the elements of list1 and list2 where each element in list1
2966 * is equal to each element in list2.
2968 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2969 __isl_take isl_pw_aff_list *list2)
2971 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2974 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2975 __isl_take isl_pw_aff_list *list2)
2977 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2980 /* Return a set containing those elements in the shared domain
2981 * of the elements of list1 and list2 where each element in list1
2982 * is less than or equal to each element in list2.
2984 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2985 __isl_take isl_pw_aff_list *list2)
2987 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2990 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2991 __isl_take isl_pw_aff_list *list2)
2993 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2996 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2997 __isl_take isl_pw_aff_list *list2)
2999 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3002 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3003 __isl_take isl_pw_aff_list *list2)
3005 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3009 /* Return a set containing those elements in the shared domain
3010 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3012 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3013 __isl_take isl_pw_aff *pwaff2)
3015 isl_set *set_lt, *set_gt;
3017 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3018 isl_pw_aff_copy(pwaff2));
3019 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3020 return isl_set_union_disjoint(set_lt, set_gt);
3023 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3024 __isl_take isl_pw_aff *pwaff2)
3026 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3029 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3030 isl_int v)
3032 int i;
3034 if (isl_int_is_one(v))
3035 return pwaff;
3036 if (!isl_int_is_pos(v))
3037 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3038 "factor needs to be positive",
3039 return isl_pw_aff_free(pwaff));
3040 pwaff = isl_pw_aff_cow(pwaff);
3041 if (!pwaff)
3042 return NULL;
3043 if (pwaff->n == 0)
3044 return pwaff;
3046 for (i = 0; i < pwaff->n; ++i) {
3047 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3048 if (!pwaff->p[i].aff)
3049 return isl_pw_aff_free(pwaff);
3052 return pwaff;
3055 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3057 int i;
3059 pwaff = isl_pw_aff_cow(pwaff);
3060 if (!pwaff)
3061 return NULL;
3062 if (pwaff->n == 0)
3063 return pwaff;
3065 for (i = 0; i < pwaff->n; ++i) {
3066 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3067 if (!pwaff->p[i].aff)
3068 return isl_pw_aff_free(pwaff);
3071 return pwaff;
3074 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3076 int i;
3078 pwaff = isl_pw_aff_cow(pwaff);
3079 if (!pwaff)
3080 return NULL;
3081 if (pwaff->n == 0)
3082 return pwaff;
3084 for (i = 0; i < pwaff->n; ++i) {
3085 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3086 if (!pwaff->p[i].aff)
3087 return isl_pw_aff_free(pwaff);
3090 return pwaff;
3093 /* Assuming that "cond1" and "cond2" are disjoint,
3094 * return an affine expression that is equal to pwaff1 on cond1
3095 * and to pwaff2 on cond2.
3097 static __isl_give isl_pw_aff *isl_pw_aff_select(
3098 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3099 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3101 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3102 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3104 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3107 /* Return an affine expression that is equal to pwaff_true for elements
3108 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3109 * is zero.
3110 * That is, return cond ? pwaff_true : pwaff_false;
3112 * If "cond" involves and NaN, then we conservatively return a NaN
3113 * on its entire domain. In principle, we could consider the pieces
3114 * where it is NaN separately from those where it is not.
3116 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3117 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3119 isl_set *cond_true, *cond_false;
3121 if (!cond)
3122 goto error;
3123 if (isl_pw_aff_involves_nan(cond)) {
3124 isl_space *space = isl_pw_aff_get_domain_space(cond);
3125 isl_local_space *ls = isl_local_space_from_space(space);
3126 isl_pw_aff_free(cond);
3127 isl_pw_aff_free(pwaff_true);
3128 isl_pw_aff_free(pwaff_false);
3129 return isl_pw_aff_nan_on_domain(ls);
3132 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3133 cond_false = isl_pw_aff_zero_set(cond);
3134 return isl_pw_aff_select(cond_true, pwaff_true,
3135 cond_false, pwaff_false);
3136 error:
3137 isl_pw_aff_free(cond);
3138 isl_pw_aff_free(pwaff_true);
3139 isl_pw_aff_free(pwaff_false);
3140 return NULL;
3143 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3145 if (!aff)
3146 return -1;
3148 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3151 /* Check whether pwaff is a piecewise constant.
3153 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3155 int i;
3157 if (!pwaff)
3158 return -1;
3160 for (i = 0; i < pwaff->n; ++i) {
3161 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3162 if (is_cst < 0 || !is_cst)
3163 return is_cst;
3166 return 1;
3169 /* Return the product of "aff1" and "aff2".
3171 * If either of the two is NaN, then the result is NaN.
3173 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3175 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3176 __isl_take isl_aff *aff2)
3178 if (!aff1 || !aff2)
3179 goto error;
3181 if (isl_aff_is_nan(aff1)) {
3182 isl_aff_free(aff2);
3183 return aff1;
3185 if (isl_aff_is_nan(aff2)) {
3186 isl_aff_free(aff1);
3187 return aff2;
3190 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3191 return isl_aff_mul(aff2, aff1);
3193 if (!isl_aff_is_cst(aff2))
3194 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3195 "at least one affine expression should be constant",
3196 goto error);
3198 aff1 = isl_aff_cow(aff1);
3199 if (!aff1 || !aff2)
3200 goto error;
3202 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3203 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3205 isl_aff_free(aff2);
3206 return aff1;
3207 error:
3208 isl_aff_free(aff1);
3209 isl_aff_free(aff2);
3210 return NULL;
3213 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3215 * If either of the two is NaN, then the result is NaN.
3217 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3218 __isl_take isl_aff *aff2)
3220 int is_cst;
3221 int neg;
3223 if (!aff1 || !aff2)
3224 goto error;
3226 if (isl_aff_is_nan(aff1)) {
3227 isl_aff_free(aff2);
3228 return aff1;
3230 if (isl_aff_is_nan(aff2)) {
3231 isl_aff_free(aff1);
3232 return aff2;
3235 is_cst = isl_aff_is_cst(aff2);
3236 if (is_cst < 0)
3237 goto error;
3238 if (!is_cst)
3239 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3240 "second argument should be a constant", goto error);
3242 if (!aff2)
3243 goto error;
3245 neg = isl_int_is_neg(aff2->v->el[1]);
3246 if (neg) {
3247 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3248 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3251 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3252 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3254 if (neg) {
3255 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3256 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3259 isl_aff_free(aff2);
3260 return aff1;
3261 error:
3262 isl_aff_free(aff1);
3263 isl_aff_free(aff2);
3264 return NULL;
3267 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3268 __isl_take isl_pw_aff *pwaff2)
3270 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3273 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3274 __isl_take isl_pw_aff *pwaff2)
3276 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3279 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3280 __isl_take isl_pw_aff *pwaff2)
3282 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3285 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3286 __isl_take isl_pw_aff *pwaff2)
3288 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3291 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3292 __isl_take isl_pw_aff *pwaff2)
3294 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3297 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3298 __isl_take isl_pw_aff *pa2)
3300 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3303 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3305 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3306 __isl_take isl_pw_aff *pa2)
3308 int is_cst;
3310 is_cst = isl_pw_aff_is_cst(pa2);
3311 if (is_cst < 0)
3312 goto error;
3313 if (!is_cst)
3314 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3315 "second argument should be a piecewise constant",
3316 goto error);
3317 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3318 error:
3319 isl_pw_aff_free(pa1);
3320 isl_pw_aff_free(pa2);
3321 return NULL;
3324 /* Compute the quotient of the integer division of "pa1" by "pa2"
3325 * with rounding towards zero.
3326 * "pa2" is assumed to be a piecewise constant.
3328 * In particular, return
3330 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3333 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3334 __isl_take isl_pw_aff *pa2)
3336 int is_cst;
3337 isl_set *cond;
3338 isl_pw_aff *f, *c;
3340 is_cst = isl_pw_aff_is_cst(pa2);
3341 if (is_cst < 0)
3342 goto error;
3343 if (!is_cst)
3344 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3345 "second argument should be a piecewise constant",
3346 goto error);
3348 pa1 = isl_pw_aff_div(pa1, pa2);
3350 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3351 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3352 c = isl_pw_aff_ceil(pa1);
3353 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3354 error:
3355 isl_pw_aff_free(pa1);
3356 isl_pw_aff_free(pa2);
3357 return NULL;
3360 /* Compute the remainder of the integer division of "pa1" by "pa2"
3361 * with rounding towards zero.
3362 * "pa2" is assumed to be a piecewise constant.
3364 * In particular, return
3366 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3369 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3370 __isl_take isl_pw_aff *pa2)
3372 int is_cst;
3373 isl_pw_aff *res;
3375 is_cst = isl_pw_aff_is_cst(pa2);
3376 if (is_cst < 0)
3377 goto error;
3378 if (!is_cst)
3379 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3380 "second argument should be a piecewise constant",
3381 goto error);
3382 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3383 res = isl_pw_aff_mul(pa2, res);
3384 res = isl_pw_aff_sub(pa1, res);
3385 return res;
3386 error:
3387 isl_pw_aff_free(pa1);
3388 isl_pw_aff_free(pa2);
3389 return NULL;
3392 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3393 __isl_take isl_pw_aff *pwaff2)
3395 isl_set *le;
3396 isl_set *dom;
3398 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3399 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3400 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3401 isl_pw_aff_copy(pwaff2));
3402 dom = isl_set_subtract(dom, isl_set_copy(le));
3403 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3406 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3407 __isl_take isl_pw_aff *pwaff2)
3409 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3412 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3413 __isl_take isl_pw_aff *pwaff2)
3415 isl_set *ge;
3416 isl_set *dom;
3418 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3419 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3420 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3421 isl_pw_aff_copy(pwaff2));
3422 dom = isl_set_subtract(dom, isl_set_copy(ge));
3423 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3426 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3427 __isl_take isl_pw_aff *pwaff2)
3429 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3432 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3433 __isl_take isl_pw_aff_list *list,
3434 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3435 __isl_take isl_pw_aff *pwaff2))
3437 int i;
3438 isl_ctx *ctx;
3439 isl_pw_aff *res;
3441 if (!list)
3442 return NULL;
3444 ctx = isl_pw_aff_list_get_ctx(list);
3445 if (list->n < 1)
3446 isl_die(ctx, isl_error_invalid,
3447 "list should contain at least one element", goto error);
3449 res = isl_pw_aff_copy(list->p[0]);
3450 for (i = 1; i < list->n; ++i)
3451 res = fn(res, isl_pw_aff_copy(list->p[i]));
3453 isl_pw_aff_list_free(list);
3454 return res;
3455 error:
3456 isl_pw_aff_list_free(list);
3457 return NULL;
3460 /* Return an isl_pw_aff that maps each element in the intersection of the
3461 * domains of the elements of list to the minimal corresponding affine
3462 * expression.
3464 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3466 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3469 /* Return an isl_pw_aff that maps each element in the intersection of the
3470 * domains of the elements of list to the maximal corresponding affine
3471 * expression.
3473 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3475 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3478 /* Mark the domains of "pwaff" as rational.
3480 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3482 int i;
3484 pwaff = isl_pw_aff_cow(pwaff);
3485 if (!pwaff)
3486 return NULL;
3487 if (pwaff->n == 0)
3488 return pwaff;
3490 for (i = 0; i < pwaff->n; ++i) {
3491 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3492 if (!pwaff->p[i].set)
3493 return isl_pw_aff_free(pwaff);
3496 return pwaff;
3499 /* Mark the domains of the elements of "list" as rational.
3501 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3502 __isl_take isl_pw_aff_list *list)
3504 int i, n;
3506 if (!list)
3507 return NULL;
3508 if (list->n == 0)
3509 return list;
3511 n = list->n;
3512 for (i = 0; i < n; ++i) {
3513 isl_pw_aff *pa;
3515 pa = isl_pw_aff_list_get_pw_aff(list, i);
3516 pa = isl_pw_aff_set_rational(pa);
3517 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3520 return list;
3523 /* Do the parameters of "aff" match those of "space"?
3525 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3526 __isl_keep isl_space *space)
3528 isl_space *aff_space;
3529 int match;
3531 if (!aff || !space)
3532 return -1;
3534 aff_space = isl_aff_get_domain_space(aff);
3536 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3538 isl_space_free(aff_space);
3539 return match;
3542 /* Check that the domain space of "aff" matches "space".
3544 * Return 0 on success and -1 on error.
3546 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3547 __isl_keep isl_space *space)
3549 isl_space *aff_space;
3550 int match;
3552 if (!aff || !space)
3553 return -1;
3555 aff_space = isl_aff_get_domain_space(aff);
3557 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3558 if (match < 0)
3559 goto error;
3560 if (!match)
3561 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3562 "parameters don't match", goto error);
3563 match = isl_space_tuple_is_equal(space, isl_dim_in,
3564 aff_space, isl_dim_set);
3565 if (match < 0)
3566 goto error;
3567 if (!match)
3568 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3569 "domains don't match", goto error);
3570 isl_space_free(aff_space);
3571 return 0;
3572 error:
3573 isl_space_free(aff_space);
3574 return -1;
3577 #undef BASE
3578 #define BASE aff
3579 #define NO_INTERSECT_DOMAIN
3580 #define NO_DOMAIN
3582 #include <isl_multi_templ.c>
3584 #undef NO_DOMAIN
3585 #undef NO_INTERSECT_DOMAIN
3587 /* Remove any internal structure of the domain of "ma".
3588 * If there is any such internal structure in the input,
3589 * then the name of the corresponding space is also removed.
3591 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3592 __isl_take isl_multi_aff *ma)
3594 isl_space *space;
3596 if (!ma)
3597 return NULL;
3599 if (!ma->space->nested[0])
3600 return ma;
3602 space = isl_multi_aff_get_space(ma);
3603 space = isl_space_flatten_domain(space);
3604 ma = isl_multi_aff_reset_space(ma, space);
3606 return ma;
3609 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3610 * of the space to its domain.
3612 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3614 int i, n_in;
3615 isl_local_space *ls;
3616 isl_multi_aff *ma;
3618 if (!space)
3619 return NULL;
3620 if (!isl_space_is_map(space))
3621 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3622 "not a map space", goto error);
3624 n_in = isl_space_dim(space, isl_dim_in);
3625 space = isl_space_domain_map(space);
3627 ma = isl_multi_aff_alloc(isl_space_copy(space));
3628 if (n_in == 0) {
3629 isl_space_free(space);
3630 return ma;
3633 space = isl_space_domain(space);
3634 ls = isl_local_space_from_space(space);
3635 for (i = 0; i < n_in; ++i) {
3636 isl_aff *aff;
3638 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3639 isl_dim_set, i);
3640 ma = isl_multi_aff_set_aff(ma, i, aff);
3642 isl_local_space_free(ls);
3643 return ma;
3644 error:
3645 isl_space_free(space);
3646 return NULL;
3649 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3650 * of the space to its range.
3652 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3654 int i, n_in, n_out;
3655 isl_local_space *ls;
3656 isl_multi_aff *ma;
3658 if (!space)
3659 return NULL;
3660 if (!isl_space_is_map(space))
3661 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3662 "not a map space", goto error);
3664 n_in = isl_space_dim(space, isl_dim_in);
3665 n_out = isl_space_dim(space, isl_dim_out);
3666 space = isl_space_range_map(space);
3668 ma = isl_multi_aff_alloc(isl_space_copy(space));
3669 if (n_out == 0) {
3670 isl_space_free(space);
3671 return ma;
3674 space = isl_space_domain(space);
3675 ls = isl_local_space_from_space(space);
3676 for (i = 0; i < n_out; ++i) {
3677 isl_aff *aff;
3679 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3680 isl_dim_set, n_in + i);
3681 ma = isl_multi_aff_set_aff(ma, i, aff);
3683 isl_local_space_free(ls);
3684 return ma;
3685 error:
3686 isl_space_free(space);
3687 return NULL;
3690 /* Given the space of a set and a range of set dimensions,
3691 * construct an isl_multi_aff that projects out those dimensions.
3693 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3694 __isl_take isl_space *space, enum isl_dim_type type,
3695 unsigned first, unsigned n)
3697 int i, dim;
3698 isl_local_space *ls;
3699 isl_multi_aff *ma;
3701 if (!space)
3702 return NULL;
3703 if (!isl_space_is_set(space))
3704 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3705 "expecting set space", goto error);
3706 if (type != isl_dim_set)
3707 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3708 "only set dimensions can be projected out", goto error);
3710 dim = isl_space_dim(space, isl_dim_set);
3711 if (first + n > dim)
3712 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3713 "range out of bounds", goto error);
3715 space = isl_space_from_domain(space);
3716 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3718 if (dim == n)
3719 return isl_multi_aff_alloc(space);
3721 ma = isl_multi_aff_alloc(isl_space_copy(space));
3722 space = isl_space_domain(space);
3723 ls = isl_local_space_from_space(space);
3725 for (i = 0; i < first; ++i) {
3726 isl_aff *aff;
3728 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3729 isl_dim_set, i);
3730 ma = isl_multi_aff_set_aff(ma, i, aff);
3733 for (i = 0; i < dim - (first + n); ++i) {
3734 isl_aff *aff;
3736 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3737 isl_dim_set, first + n + i);
3738 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3741 isl_local_space_free(ls);
3742 return ma;
3743 error:
3744 isl_space_free(space);
3745 return NULL;
3748 /* Given the space of a set and a range of set dimensions,
3749 * construct an isl_pw_multi_aff that projects out those dimensions.
3751 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3752 __isl_take isl_space *space, enum isl_dim_type type,
3753 unsigned first, unsigned n)
3755 isl_multi_aff *ma;
3757 ma = isl_multi_aff_project_out_map(space, type, first, n);
3758 return isl_pw_multi_aff_from_multi_aff(ma);
3761 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3762 * domain.
3764 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3765 __isl_take isl_multi_aff *ma)
3767 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3768 return isl_pw_multi_aff_alloc(dom, ma);
3771 /* Create a piecewise multi-affine expression in the given space that maps each
3772 * input dimension to the corresponding output dimension.
3774 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3775 __isl_take isl_space *space)
3777 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3780 /* Add "ma2" to "ma1" and return the result.
3782 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3784 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3785 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3787 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3790 /* Add "ma2" to "ma1" and return the result.
3792 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3793 __isl_take isl_multi_aff *ma2)
3795 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3796 &isl_multi_aff_add_aligned);
3799 /* Subtract "ma2" from "ma1" and return the result.
3801 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3803 static __isl_give isl_multi_aff *isl_multi_aff_sub_aligned(
3804 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3806 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3809 /* Subtract "ma2" from "ma1" and return the result.
3811 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3812 __isl_take isl_multi_aff *ma2)
3814 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3815 &isl_multi_aff_sub_aligned);
3818 /* Exploit the equalities in "eq" to simplify the affine expressions.
3820 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3821 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3823 int i;
3825 maff = isl_multi_aff_cow(maff);
3826 if (!maff || !eq)
3827 goto error;
3829 for (i = 0; i < maff->n; ++i) {
3830 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3831 isl_basic_set_copy(eq));
3832 if (!maff->p[i])
3833 goto error;
3836 isl_basic_set_free(eq);
3837 return maff;
3838 error:
3839 isl_basic_set_free(eq);
3840 isl_multi_aff_free(maff);
3841 return NULL;
3844 /* Given f, return floor(f).
3846 __isl_give isl_multi_aff *isl_multi_aff_floor(__isl_take isl_multi_aff *ma)
3848 int i;
3850 ma = isl_multi_aff_cow(ma);
3851 if (!ma)
3852 return NULL;
3854 for (i = 0; i < ma->n; ++i) {
3855 ma->p[i] = isl_aff_floor(ma->p[i]);
3856 if (!ma->p[i])
3857 return isl_multi_aff_free(ma);
3860 return ma;
3863 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3864 isl_int f)
3866 int i;
3868 maff = isl_multi_aff_cow(maff);
3869 if (!maff)
3870 return NULL;
3872 for (i = 0; i < maff->n; ++i) {
3873 maff->p[i] = isl_aff_scale(maff->p[i], f);
3874 if (!maff->p[i])
3875 return isl_multi_aff_free(maff);
3878 return maff;
3881 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3882 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3884 maff1 = isl_multi_aff_add(maff1, maff2);
3885 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3886 return maff1;
3889 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3891 if (!maff)
3892 return -1;
3894 return 0;
3897 /* Return the set of domain elements where "ma1" is lexicographically
3898 * smaller than or equal to "ma2".
3900 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3901 __isl_take isl_multi_aff *ma2)
3903 return isl_multi_aff_lex_ge_set(ma2, ma1);
3906 /* Return the set of domain elements where "ma1" is lexicographically
3907 * greater than or equal to "ma2".
3909 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3910 __isl_take isl_multi_aff *ma2)
3912 isl_space *space;
3913 isl_map *map1, *map2;
3914 isl_map *map, *ge;
3916 map1 = isl_map_from_multi_aff(ma1);
3917 map2 = isl_map_from_multi_aff(ma2);
3918 map = isl_map_range_product(map1, map2);
3919 space = isl_space_range(isl_map_get_space(map));
3920 space = isl_space_domain(isl_space_unwrap(space));
3921 ge = isl_map_lex_ge(space);
3922 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3924 return isl_map_domain(map);
3927 #undef PW
3928 #define PW isl_pw_multi_aff
3929 #undef EL
3930 #define EL isl_multi_aff
3931 #undef EL_IS_ZERO
3932 #define EL_IS_ZERO is_empty
3933 #undef ZERO
3934 #define ZERO empty
3935 #undef IS_ZERO
3936 #define IS_ZERO is_empty
3937 #undef FIELD
3938 #define FIELD maff
3939 #undef DEFAULT_IS_ZERO
3940 #define DEFAULT_IS_ZERO 0
3942 #define NO_NEG
3943 #define NO_EVAL
3944 #define NO_OPT
3945 #define NO_INVOLVES_DIMS
3946 #define NO_INSERT_DIMS
3947 #define NO_LIFT
3948 #define NO_MORPH
3950 #include <isl_pw_templ.c>
3952 #undef UNION
3953 #define UNION isl_union_pw_multi_aff
3954 #undef PART
3955 #define PART isl_pw_multi_aff
3956 #undef PARTS
3957 #define PARTS pw_multi_aff
3958 #define ALIGN_DOMAIN
3960 #define NO_EVAL
3962 #include <isl_union_templ.c>
3964 /* Given a function "cmp" that returns the set of elements where
3965 * "ma1" is "better" than "ma2", return the intersection of this
3966 * set with "dom1" and "dom2".
3968 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3969 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3970 __isl_keep isl_multi_aff *ma2,
3971 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3972 __isl_take isl_multi_aff *ma2))
3974 isl_set *common;
3975 isl_set *better;
3976 int is_empty;
3978 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3979 is_empty = isl_set_plain_is_empty(common);
3980 if (is_empty >= 0 && is_empty)
3981 return common;
3982 if (is_empty < 0)
3983 return isl_set_free(common);
3984 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3985 better = isl_set_intersect(common, better);
3987 return better;
3990 /* Given a function "cmp" that returns the set of elements where
3991 * "ma1" is "better" than "ma2", return a piecewise multi affine
3992 * expression defined on the union of the definition domains
3993 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3994 * "pma2" on each cell. If only one of the two input functions
3995 * is defined on a given cell, then it is considered the best.
3997 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3998 __isl_take isl_pw_multi_aff *pma1,
3999 __isl_take isl_pw_multi_aff *pma2,
4000 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4001 __isl_take isl_multi_aff *ma2))
4003 int i, j, n;
4004 isl_pw_multi_aff *res = NULL;
4005 isl_ctx *ctx;
4006 isl_set *set = NULL;
4008 if (!pma1 || !pma2)
4009 goto error;
4011 ctx = isl_space_get_ctx(pma1->dim);
4012 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4013 isl_die(ctx, isl_error_invalid,
4014 "arguments should live in the same space", goto error);
4016 if (isl_pw_multi_aff_is_empty(pma1)) {
4017 isl_pw_multi_aff_free(pma1);
4018 return pma2;
4021 if (isl_pw_multi_aff_is_empty(pma2)) {
4022 isl_pw_multi_aff_free(pma2);
4023 return pma1;
4026 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4027 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4029 for (i = 0; i < pma1->n; ++i) {
4030 set = isl_set_copy(pma1->p[i].set);
4031 for (j = 0; j < pma2->n; ++j) {
4032 isl_set *better;
4033 int is_empty;
4035 better = shared_and_better(pma2->p[j].set,
4036 pma1->p[i].set, pma2->p[j].maff,
4037 pma1->p[i].maff, cmp);
4038 is_empty = isl_set_plain_is_empty(better);
4039 if (is_empty < 0 || is_empty) {
4040 isl_set_free(better);
4041 if (is_empty < 0)
4042 goto error;
4043 continue;
4045 set = isl_set_subtract(set, isl_set_copy(better));
4047 res = isl_pw_multi_aff_add_piece(res, better,
4048 isl_multi_aff_copy(pma2->p[j].maff));
4050 res = isl_pw_multi_aff_add_piece(res, set,
4051 isl_multi_aff_copy(pma1->p[i].maff));
4054 for (j = 0; j < pma2->n; ++j) {
4055 set = isl_set_copy(pma2->p[j].set);
4056 for (i = 0; i < pma1->n; ++i)
4057 set = isl_set_subtract(set,
4058 isl_set_copy(pma1->p[i].set));
4059 res = isl_pw_multi_aff_add_piece(res, set,
4060 isl_multi_aff_copy(pma2->p[j].maff));
4063 isl_pw_multi_aff_free(pma1);
4064 isl_pw_multi_aff_free(pma2);
4066 return res;
4067 error:
4068 isl_pw_multi_aff_free(pma1);
4069 isl_pw_multi_aff_free(pma2);
4070 isl_set_free(set);
4071 return isl_pw_multi_aff_free(res);
4074 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4075 __isl_take isl_pw_multi_aff *pma1,
4076 __isl_take isl_pw_multi_aff *pma2)
4078 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4081 /* Given two piecewise multi affine expressions, return a piecewise
4082 * multi-affine expression defined on the union of the definition domains
4083 * of the inputs that is equal to the lexicographic maximum of the two
4084 * inputs on each cell. If only one of the two inputs is defined on
4085 * a given cell, then it is considered to be the maximum.
4087 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4088 __isl_take isl_pw_multi_aff *pma1,
4089 __isl_take isl_pw_multi_aff *pma2)
4091 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4092 &pw_multi_aff_union_lexmax);
4095 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4096 __isl_take isl_pw_multi_aff *pma1,
4097 __isl_take isl_pw_multi_aff *pma2)
4099 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4102 /* Given two piecewise multi affine expressions, return a piecewise
4103 * multi-affine expression defined on the union of the definition domains
4104 * of the inputs that is equal to the lexicographic minimum of the two
4105 * inputs on each cell. If only one of the two inputs is defined on
4106 * a given cell, then it is considered to be the minimum.
4108 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4109 __isl_take isl_pw_multi_aff *pma1,
4110 __isl_take isl_pw_multi_aff *pma2)
4112 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4113 &pw_multi_aff_union_lexmin);
4116 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4117 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4119 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4120 &isl_multi_aff_add);
4123 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4124 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4126 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4127 &pw_multi_aff_add);
4130 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4131 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4133 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4134 &isl_multi_aff_sub);
4137 /* Subtract "pma2" from "pma1" and return the result.
4139 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4140 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4142 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4143 &pw_multi_aff_sub);
4146 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4147 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4149 return isl_pw_multi_aff_union_add_(pma1, pma2);
4152 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4153 * with the actual sum on the shared domain and
4154 * the defined expression on the symmetric difference of the domains.
4156 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4157 __isl_take isl_union_pw_multi_aff *upma1,
4158 __isl_take isl_union_pw_multi_aff *upma2)
4160 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4163 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4164 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4166 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4167 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4169 int i, j, n;
4170 isl_space *space;
4171 isl_pw_multi_aff *res;
4173 if (!pma1 || !pma2)
4174 goto error;
4176 n = pma1->n * pma2->n;
4177 space = isl_space_product(isl_space_copy(pma1->dim),
4178 isl_space_copy(pma2->dim));
4179 res = isl_pw_multi_aff_alloc_size(space, n);
4181 for (i = 0; i < pma1->n; ++i) {
4182 for (j = 0; j < pma2->n; ++j) {
4183 isl_set *domain;
4184 isl_multi_aff *ma;
4186 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4187 isl_set_copy(pma2->p[j].set));
4188 ma = isl_multi_aff_product(
4189 isl_multi_aff_copy(pma1->p[i].maff),
4190 isl_multi_aff_copy(pma2->p[j].maff));
4191 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4195 isl_pw_multi_aff_free(pma1);
4196 isl_pw_multi_aff_free(pma2);
4197 return res;
4198 error:
4199 isl_pw_multi_aff_free(pma1);
4200 isl_pw_multi_aff_free(pma2);
4201 return NULL;
4204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4205 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4207 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4208 &pw_multi_aff_product);
4211 /* Construct a map mapping the domain of the piecewise multi-affine expression
4212 * to its range, with each dimension in the range equated to the
4213 * corresponding affine expression on its cell.
4215 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4217 int i;
4218 isl_map *map;
4220 if (!pma)
4221 return NULL;
4223 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4225 for (i = 0; i < pma->n; ++i) {
4226 isl_multi_aff *maff;
4227 isl_basic_map *bmap;
4228 isl_map *map_i;
4230 maff = isl_multi_aff_copy(pma->p[i].maff);
4231 bmap = isl_basic_map_from_multi_aff(maff);
4232 map_i = isl_map_from_basic_map(bmap);
4233 map_i = isl_map_intersect_domain(map_i,
4234 isl_set_copy(pma->p[i].set));
4235 map = isl_map_union_disjoint(map, map_i);
4238 isl_pw_multi_aff_free(pma);
4239 return map;
4242 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4244 if (!pma)
4245 return NULL;
4247 if (!isl_space_is_set(pma->dim))
4248 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4249 "isl_pw_multi_aff cannot be converted into an isl_set",
4250 goto error);
4252 return isl_map_from_pw_multi_aff(pma);
4253 error:
4254 isl_pw_multi_aff_free(pma);
4255 return NULL;
4258 /* Given a basic map with a single output dimension that is defined
4259 * in terms of the parameters and input dimensions using an equality,
4260 * extract an isl_aff that expresses the output dimension in terms
4261 * of the parameters and input dimensions.
4262 * Note that this expression may involve integer divisions defined
4263 * in terms of parameters and input dimensions.
4265 * This function shares some similarities with
4266 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4268 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4269 __isl_take isl_basic_map *bmap)
4271 int eq;
4272 unsigned offset;
4273 unsigned n_div;
4274 isl_local_space *ls;
4275 isl_aff *aff;
4277 if (!bmap)
4278 return NULL;
4279 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4280 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4281 "basic map should have a single output dimension",
4282 goto error);
4283 eq = isl_basic_map_output_defining_equality(bmap, 0);
4284 if (eq >= bmap->n_eq)
4285 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4286 "unable to find suitable equality", goto error);
4287 ls = isl_basic_map_get_local_space(bmap);
4288 aff = isl_aff_alloc(isl_local_space_domain(ls));
4289 if (!aff)
4290 goto error;
4291 offset = isl_basic_map_offset(bmap, isl_dim_out);
4292 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4293 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4294 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4295 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4296 n_div);
4297 } else {
4298 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4299 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4300 n_div);
4302 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4303 isl_basic_map_free(bmap);
4305 aff = isl_aff_remove_unused_divs(aff);
4306 return aff;
4307 error:
4308 isl_basic_map_free(bmap);
4309 return NULL;
4312 /* Given a basic map where each output dimension is defined
4313 * in terms of the parameters and input dimensions using an equality,
4314 * extract an isl_multi_aff that expresses the output dimensions in terms
4315 * of the parameters and input dimensions.
4317 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4318 __isl_take isl_basic_map *bmap)
4320 int i;
4321 unsigned n_out;
4322 isl_multi_aff *ma;
4324 if (!bmap)
4325 return NULL;
4327 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4328 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4330 for (i = 0; i < n_out; ++i) {
4331 isl_basic_map *bmap_i;
4332 isl_aff *aff;
4334 bmap_i = isl_basic_map_copy(bmap);
4335 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4336 i + 1, n_out - (1 + i));
4337 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4338 aff = extract_isl_aff_from_basic_map(bmap_i);
4339 ma = isl_multi_aff_set_aff(ma, i, aff);
4342 isl_basic_map_free(bmap);
4344 return ma;
4347 /* Given a basic set where each set dimension is defined
4348 * in terms of the parameters using an equality,
4349 * extract an isl_multi_aff that expresses the set dimensions in terms
4350 * of the parameters.
4352 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4353 __isl_take isl_basic_set *bset)
4355 return extract_isl_multi_aff_from_basic_map(bset);
4358 /* Create an isl_pw_multi_aff that is equivalent to
4359 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4360 * The given basic map is such that each output dimension is defined
4361 * in terms of the parameters and input dimensions using an equality.
4363 * Since some applications expect the result of isl_pw_multi_aff_from_map
4364 * to only contain integer affine expressions, we compute the floor
4365 * of the expression before returning.
4367 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4368 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4370 isl_multi_aff *ma;
4372 ma = extract_isl_multi_aff_from_basic_map(bmap);
4373 ma = isl_multi_aff_floor(ma);
4374 return isl_pw_multi_aff_alloc(domain, ma);
4377 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4378 * This obviously only works if the input "map" is single-valued.
4379 * If so, we compute the lexicographic minimum of the image in the form
4380 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4381 * to its lexicographic minimum.
4382 * If the input is not single-valued, we produce an error.
4384 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4385 __isl_take isl_map *map)
4387 int i;
4388 int sv;
4389 isl_pw_multi_aff *pma;
4391 sv = isl_map_is_single_valued(map);
4392 if (sv < 0)
4393 goto error;
4394 if (!sv)
4395 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4396 "map is not single-valued", goto error);
4397 map = isl_map_make_disjoint(map);
4398 if (!map)
4399 return NULL;
4401 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4403 for (i = 0; i < map->n; ++i) {
4404 isl_pw_multi_aff *pma_i;
4405 isl_basic_map *bmap;
4406 bmap = isl_basic_map_copy(map->p[i]);
4407 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4408 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4411 isl_map_free(map);
4412 return pma;
4413 error:
4414 isl_map_free(map);
4415 return NULL;
4418 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4419 * taking into account that the output dimension at position "d"
4420 * can be represented as
4422 * x = floor((e(...) + c1) / m)
4424 * given that constraint "i" is of the form
4426 * e(...) + c1 - m x >= 0
4429 * Let "map" be of the form
4431 * A -> B
4433 * We construct a mapping
4435 * A -> [A -> x = floor(...)]
4437 * apply that to the map, obtaining
4439 * [A -> x = floor(...)] -> B
4441 * and equate dimension "d" to x.
4442 * We then compute a isl_pw_multi_aff representation of the resulting map
4443 * and plug in the mapping above.
4445 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4446 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4448 isl_ctx *ctx;
4449 isl_space *space;
4450 isl_local_space *ls;
4451 isl_multi_aff *ma;
4452 isl_aff *aff;
4453 isl_vec *v;
4454 isl_map *insert;
4455 int offset;
4456 int n;
4457 int n_in;
4458 isl_pw_multi_aff *pma;
4459 int is_set;
4461 is_set = isl_map_is_set(map);
4463 offset = isl_basic_map_offset(hull, isl_dim_out);
4464 ctx = isl_map_get_ctx(map);
4465 space = isl_space_domain(isl_map_get_space(map));
4466 n_in = isl_space_dim(space, isl_dim_set);
4467 n = isl_space_dim(space, isl_dim_all);
4469 v = isl_vec_alloc(ctx, 1 + 1 + n);
4470 if (v) {
4471 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4472 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4474 isl_basic_map_free(hull);
4476 ls = isl_local_space_from_space(isl_space_copy(space));
4477 aff = isl_aff_alloc_vec(ls, v);
4478 aff = isl_aff_floor(aff);
4479 if (is_set) {
4480 isl_space_free(space);
4481 ma = isl_multi_aff_from_aff(aff);
4482 } else {
4483 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4484 ma = isl_multi_aff_range_product(ma,
4485 isl_multi_aff_from_aff(aff));
4488 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4489 map = isl_map_apply_domain(map, insert);
4490 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4491 pma = isl_pw_multi_aff_from_map(map);
4492 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4494 return pma;
4497 /* Is constraint "c" of the form
4499 * e(...) + c1 - m x >= 0
4501 * or
4503 * -e(...) + c2 + m x >= 0
4505 * where m > 1 and e only depends on parameters and input dimemnsions?
4507 * "offset" is the offset of the output dimensions
4508 * "pos" is the position of output dimension x.
4510 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4512 if (isl_int_is_zero(c[offset + d]))
4513 return 0;
4514 if (isl_int_is_one(c[offset + d]))
4515 return 0;
4516 if (isl_int_is_negone(c[offset + d]))
4517 return 0;
4518 if (isl_seq_first_non_zero(c + offset, d) != -1)
4519 return 0;
4520 if (isl_seq_first_non_zero(c + offset + d + 1,
4521 total - (offset + d + 1)) != -1)
4522 return 0;
4523 return 1;
4526 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4528 * As a special case, we first check if there is any pair of constraints,
4529 * shared by all the basic maps in "map" that force a given dimension
4530 * to be equal to the floor of some affine combination of the input dimensions.
4532 * In particular, if we can find two constraints
4534 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4536 * and
4538 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4540 * where m > 1 and e only depends on parameters and input dimemnsions,
4541 * and such that
4543 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4545 * then we know that we can take
4547 * x = floor((e(...) + c1) / m)
4549 * without having to perform any computation.
4551 * Note that we know that
4553 * c1 + c2 >= 1
4555 * If c1 + c2 were 0, then we would have detected an equality during
4556 * simplification. If c1 + c2 were negative, then we would have detected
4557 * a contradiction.
4559 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4560 __isl_take isl_map *map)
4562 int d, dim;
4563 int i, j, n;
4564 int offset, total;
4565 isl_int sum;
4566 isl_basic_map *hull;
4568 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4569 if (!hull)
4570 goto error;
4572 isl_int_init(sum);
4573 dim = isl_map_dim(map, isl_dim_out);
4574 offset = isl_basic_map_offset(hull, isl_dim_out);
4575 total = 1 + isl_basic_map_total_dim(hull);
4576 n = hull->n_ineq;
4577 for (d = 0; d < dim; ++d) {
4578 for (i = 0; i < n; ++i) {
4579 if (!is_potential_div_constraint(hull->ineq[i],
4580 offset, d, total))
4581 continue;
4582 for (j = i + 1; j < n; ++j) {
4583 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4584 hull->ineq[j] + 1, total - 1))
4585 continue;
4586 isl_int_add(sum, hull->ineq[i][0],
4587 hull->ineq[j][0]);
4588 if (isl_int_abs_lt(sum,
4589 hull->ineq[i][offset + d]))
4590 break;
4593 if (j >= n)
4594 continue;
4595 isl_int_clear(sum);
4596 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4597 j = i;
4598 return pw_multi_aff_from_map_div(map, hull, d, j);
4601 isl_int_clear(sum);
4602 isl_basic_map_free(hull);
4603 return pw_multi_aff_from_map_base(map);
4604 error:
4605 isl_map_free(map);
4606 isl_basic_map_free(hull);
4607 return NULL;
4610 /* Given an affine expression
4612 * [A -> B] -> f(A,B)
4614 * construct an isl_multi_aff
4616 * [A -> B] -> B'
4618 * such that dimension "d" in B' is set to "aff" and the remaining
4619 * dimensions are set equal to the corresponding dimensions in B.
4620 * "n_in" is the dimension of the space A.
4621 * "n_out" is the dimension of the space B.
4623 * If "is_set" is set, then the affine expression is of the form
4625 * [B] -> f(B)
4627 * and we construct an isl_multi_aff
4629 * B -> B'
4631 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4632 unsigned n_in, unsigned n_out, int is_set)
4634 int i;
4635 isl_multi_aff *ma;
4636 isl_space *space, *space2;
4637 isl_local_space *ls;
4639 space = isl_aff_get_domain_space(aff);
4640 ls = isl_local_space_from_space(isl_space_copy(space));
4641 space2 = isl_space_copy(space);
4642 if (!is_set)
4643 space2 = isl_space_range(isl_space_unwrap(space2));
4644 space = isl_space_map_from_domain_and_range(space, space2);
4645 ma = isl_multi_aff_alloc(space);
4646 ma = isl_multi_aff_set_aff(ma, d, aff);
4648 for (i = 0; i < n_out; ++i) {
4649 if (i == d)
4650 continue;
4651 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4652 isl_dim_set, n_in + i);
4653 ma = isl_multi_aff_set_aff(ma, i, aff);
4656 isl_local_space_free(ls);
4658 return ma;
4661 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4662 * taking into account that the dimension at position "d" can be written as
4664 * x = m a + f(..) (1)
4666 * where m is equal to "gcd".
4667 * "i" is the index of the equality in "hull" that defines f(..).
4668 * In particular, the equality is of the form
4670 * f(..) - x + m g(existentials) = 0
4672 * or
4674 * -f(..) + x + m g(existentials) = 0
4676 * We basically plug (1) into "map", resulting in a map with "a"
4677 * in the range instead of "x". The corresponding isl_pw_multi_aff
4678 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4680 * Specifically, given the input map
4682 * A -> B
4684 * We first wrap it into a set
4686 * [A -> B]
4688 * and define (1) on top of the corresponding space, resulting in "aff".
4689 * We use this to create an isl_multi_aff that maps the output position "d"
4690 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4691 * We plug this into the wrapped map, unwrap the result and compute the
4692 * corresponding isl_pw_multi_aff.
4693 * The result is an expression
4695 * A -> T(A)
4697 * We adjust that to
4699 * A -> [A -> T(A)]
4701 * so that we can plug that into "aff", after extending the latter to
4702 * a mapping
4704 * [A -> B] -> B'
4707 * If "map" is actually a set, then there is no "A" space, meaning
4708 * that we do not need to perform any wrapping, and that the result
4709 * of the recursive call is of the form
4711 * [T]
4713 * which is plugged into a mapping of the form
4715 * B -> B'
4717 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4718 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4719 isl_int gcd)
4721 isl_set *set;
4722 isl_space *space;
4723 isl_local_space *ls;
4724 isl_aff *aff;
4725 isl_multi_aff *ma;
4726 isl_pw_multi_aff *pma, *id;
4727 unsigned n_in;
4728 unsigned o_out;
4729 unsigned n_out;
4730 int is_set;
4732 is_set = isl_map_is_set(map);
4734 n_in = isl_basic_map_dim(hull, isl_dim_in);
4735 n_out = isl_basic_map_dim(hull, isl_dim_out);
4736 o_out = isl_basic_map_offset(hull, isl_dim_out);
4738 if (is_set)
4739 set = map;
4740 else
4741 set = isl_map_wrap(map);
4742 space = isl_space_map_from_set(isl_set_get_space(set));
4743 ma = isl_multi_aff_identity(space);
4744 ls = isl_local_space_from_space(isl_set_get_space(set));
4745 aff = isl_aff_alloc(ls);
4746 if (aff) {
4747 isl_int_set_si(aff->v->el[0], 1);
4748 if (isl_int_is_one(hull->eq[i][o_out + d]))
4749 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4750 aff->v->size - 1);
4751 else
4752 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4753 aff->v->size - 1);
4754 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4756 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4757 set = isl_set_preimage_multi_aff(set, ma);
4759 ma = range_map(aff, d, n_in, n_out, is_set);
4761 if (is_set)
4762 map = set;
4763 else
4764 map = isl_set_unwrap(set);
4765 pma = isl_pw_multi_aff_from_map(set);
4767 if (!is_set) {
4768 space = isl_pw_multi_aff_get_domain_space(pma);
4769 space = isl_space_map_from_set(space);
4770 id = isl_pw_multi_aff_identity(space);
4771 pma = isl_pw_multi_aff_range_product(id, pma);
4773 id = isl_pw_multi_aff_from_multi_aff(ma);
4774 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4776 isl_basic_map_free(hull);
4777 return pma;
4780 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4782 * As a special case, we first check if all output dimensions are uniquely
4783 * defined in terms of the parameters and input dimensions over the entire
4784 * domain. If so, we extract the desired isl_pw_multi_aff directly
4785 * from the affine hull of "map" and its domain.
4787 * Otherwise, we check if any of the output dimensions is "strided".
4788 * That is, we check if can be written as
4790 * x = m a + f(..)
4792 * with m greater than 1, a some combination of existentiall quantified
4793 * variables and f and expression in the parameters and input dimensions.
4794 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4796 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4797 * special case.
4799 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4801 int i, j;
4802 int sv;
4803 isl_basic_map *hull;
4804 unsigned n_out;
4805 unsigned o_out;
4806 unsigned n_div;
4807 unsigned o_div;
4808 isl_int gcd;
4810 if (!map)
4811 return NULL;
4813 hull = isl_map_affine_hull(isl_map_copy(map));
4814 sv = isl_basic_map_plain_is_single_valued(hull);
4815 if (sv >= 0 && sv)
4816 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4817 if (sv < 0)
4818 hull = isl_basic_map_free(hull);
4819 if (!hull)
4820 goto error;
4822 n_div = isl_basic_map_dim(hull, isl_dim_div);
4823 o_div = isl_basic_map_offset(hull, isl_dim_div);
4825 if (n_div == 0) {
4826 isl_basic_map_free(hull);
4827 return pw_multi_aff_from_map_check_div(map);
4830 isl_int_init(gcd);
4832 n_out = isl_basic_map_dim(hull, isl_dim_out);
4833 o_out = isl_basic_map_offset(hull, isl_dim_out);
4835 for (i = 0; i < n_out; ++i) {
4836 for (j = 0; j < hull->n_eq; ++j) {
4837 isl_int *eq = hull->eq[j];
4838 isl_pw_multi_aff *res;
4840 if (!isl_int_is_one(eq[o_out + i]) &&
4841 !isl_int_is_negone(eq[o_out + i]))
4842 continue;
4843 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4844 continue;
4845 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4846 n_out - (i + 1)) != -1)
4847 continue;
4848 isl_seq_gcd(eq + o_div, n_div, &gcd);
4849 if (isl_int_is_zero(gcd))
4850 continue;
4851 if (isl_int_is_one(gcd))
4852 continue;
4854 res = pw_multi_aff_from_map_stride(map, hull,
4855 i, j, gcd);
4856 isl_int_clear(gcd);
4857 return res;
4861 isl_int_clear(gcd);
4862 isl_basic_map_free(hull);
4863 return pw_multi_aff_from_map_check_div(map);
4864 error:
4865 isl_map_free(map);
4866 return NULL;
4869 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4871 return isl_pw_multi_aff_from_map(set);
4874 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4875 * add it to *user.
4877 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4879 isl_union_pw_multi_aff **upma = user;
4880 isl_pw_multi_aff *pma;
4882 pma = isl_pw_multi_aff_from_map(map);
4883 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4885 return *upma ? 0 : -1;
4888 /* Try and create an isl_union_pw_multi_aff that is equivalent
4889 * to the given isl_union_map.
4890 * The isl_union_map is required to be single-valued in each space.
4891 * Otherwise, an error is produced.
4893 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4894 __isl_take isl_union_map *umap)
4896 isl_space *space;
4897 isl_union_pw_multi_aff *upma;
4899 space = isl_union_map_get_space(umap);
4900 upma = isl_union_pw_multi_aff_empty(space);
4901 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4902 upma = isl_union_pw_multi_aff_free(upma);
4903 isl_union_map_free(umap);
4905 return upma;
4908 /* Try and create an isl_union_pw_multi_aff that is equivalent
4909 * to the given isl_union_set.
4910 * The isl_union_set is required to be a singleton in each space.
4911 * Otherwise, an error is produced.
4913 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4914 __isl_take isl_union_set *uset)
4916 return isl_union_pw_multi_aff_from_union_map(uset);
4919 /* Return the piecewise affine expression "set ? 1 : 0".
4921 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4923 isl_pw_aff *pa;
4924 isl_space *space = isl_set_get_space(set);
4925 isl_local_space *ls = isl_local_space_from_space(space);
4926 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4927 isl_aff *one = isl_aff_zero_on_domain(ls);
4929 one = isl_aff_add_constant_si(one, 1);
4930 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4931 set = isl_set_complement(set);
4932 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4934 return pa;
4937 /* Plug in "subs" for dimension "type", "pos" of "aff".
4939 * Let i be the dimension to replace and let "subs" be of the form
4941 * f/d
4943 * and "aff" of the form
4945 * (a i + g)/m
4947 * The result is
4949 * (a f + d g')/(m d)
4951 * where g' is the result of plugging in "subs" in each of the integer
4952 * divisions in g.
4954 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4955 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4957 isl_ctx *ctx;
4958 isl_int v;
4960 aff = isl_aff_cow(aff);
4961 if (!aff || !subs)
4962 return isl_aff_free(aff);
4964 ctx = isl_aff_get_ctx(aff);
4965 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4966 isl_die(ctx, isl_error_invalid,
4967 "spaces don't match", return isl_aff_free(aff));
4968 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4969 isl_die(ctx, isl_error_unsupported,
4970 "cannot handle divs yet", return isl_aff_free(aff));
4972 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4973 if (!aff->ls)
4974 return isl_aff_free(aff);
4976 aff->v = isl_vec_cow(aff->v);
4977 if (!aff->v)
4978 return isl_aff_free(aff);
4980 pos += isl_local_space_offset(aff->ls, type);
4982 isl_int_init(v);
4983 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4984 aff->v->size, subs->v->size, v);
4985 isl_int_clear(v);
4987 return aff;
4990 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4991 * expressions in "maff".
4993 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4994 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4995 __isl_keep isl_aff *subs)
4997 int i;
4999 maff = isl_multi_aff_cow(maff);
5000 if (!maff || !subs)
5001 return isl_multi_aff_free(maff);
5003 if (type == isl_dim_in)
5004 type = isl_dim_set;
5006 for (i = 0; i < maff->n; ++i) {
5007 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5008 if (!maff->p[i])
5009 return isl_multi_aff_free(maff);
5012 return maff;
5015 /* Plug in "subs" for dimension "type", "pos" of "pma".
5017 * pma is of the form
5019 * A_i(v) -> M_i(v)
5021 * while subs is of the form
5023 * v' = B_j(v) -> S_j
5025 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5026 * has a contribution in the result, in particular
5028 * C_ij(S_j) -> M_i(S_j)
5030 * Note that plugging in S_j in C_ij may also result in an empty set
5031 * and this contribution should simply be discarded.
5033 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5034 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5035 __isl_keep isl_pw_aff *subs)
5037 int i, j, n;
5038 isl_pw_multi_aff *res;
5040 if (!pma || !subs)
5041 return isl_pw_multi_aff_free(pma);
5043 n = pma->n * subs->n;
5044 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5046 for (i = 0; i < pma->n; ++i) {
5047 for (j = 0; j < subs->n; ++j) {
5048 isl_set *common;
5049 isl_multi_aff *res_ij;
5050 int empty;
5052 common = isl_set_intersect(
5053 isl_set_copy(pma->p[i].set),
5054 isl_set_copy(subs->p[j].set));
5055 common = isl_set_substitute(common,
5056 type, pos, subs->p[j].aff);
5057 empty = isl_set_plain_is_empty(common);
5058 if (empty < 0 || empty) {
5059 isl_set_free(common);
5060 if (empty < 0)
5061 goto error;
5062 continue;
5065 res_ij = isl_multi_aff_substitute(
5066 isl_multi_aff_copy(pma->p[i].maff),
5067 type, pos, subs->p[j].aff);
5069 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5073 isl_pw_multi_aff_free(pma);
5074 return res;
5075 error:
5076 isl_pw_multi_aff_free(pma);
5077 isl_pw_multi_aff_free(res);
5078 return NULL;
5081 /* Compute the preimage of a range of dimensions in the affine expression "src"
5082 * under "ma" and put the result in "dst". The number of dimensions in "src"
5083 * that precede the range is given by "n_before". The number of dimensions
5084 * in the range is given by the number of output dimensions of "ma".
5085 * The number of dimensions that follow the range is given by "n_after".
5086 * If "has_denom" is set (to one),
5087 * then "src" and "dst" have an extra initial denominator.
5088 * "n_div_ma" is the number of existentials in "ma"
5089 * "n_div_bset" is the number of existentials in "src"
5090 * The resulting "dst" (which is assumed to have been allocated by
5091 * the caller) contains coefficients for both sets of existentials,
5092 * first those in "ma" and then those in "src".
5093 * f, c1, c2 and g are temporary objects that have been initialized
5094 * by the caller.
5096 * Let src represent the expression
5098 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5100 * and let ma represent the expressions
5102 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5104 * We start out with the following expression for dst:
5106 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5108 * with the multiplication factor f initially equal to 1
5109 * and f \sum_i b_i v_i kept separately.
5110 * For each x_i that we substitute, we multiply the numerator
5111 * (and denominator) of dst by c_1 = m_i and add the numerator
5112 * of the x_i expression multiplied by c_2 = f b_i,
5113 * after removing the common factors of c_1 and c_2.
5114 * The multiplication factor f also needs to be multiplied by c_1
5115 * for the next x_j, j > i.
5117 void isl_seq_preimage(isl_int *dst, isl_int *src,
5118 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5119 int n_div_ma, int n_div_bmap,
5120 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5122 int i;
5123 int n_param, n_in, n_out;
5124 int o_dst, o_src;
5126 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5127 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5128 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5130 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5131 o_dst = o_src = has_denom + 1 + n_param + n_before;
5132 isl_seq_clr(dst + o_dst, n_in);
5133 o_dst += n_in;
5134 o_src += n_out;
5135 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5136 o_dst += n_after;
5137 o_src += n_after;
5138 isl_seq_clr(dst + o_dst, n_div_ma);
5139 o_dst += n_div_ma;
5140 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5142 isl_int_set_si(f, 1);
5144 for (i = 0; i < n_out; ++i) {
5145 int offset = has_denom + 1 + n_param + n_before + i;
5147 if (isl_int_is_zero(src[offset]))
5148 continue;
5149 isl_int_set(c1, ma->p[i]->v->el[0]);
5150 isl_int_mul(c2, f, src[offset]);
5151 isl_int_gcd(g, c1, c2);
5152 isl_int_divexact(c1, c1, g);
5153 isl_int_divexact(c2, c2, g);
5155 isl_int_mul(f, f, c1);
5156 o_dst = has_denom;
5157 o_src = 1;
5158 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5159 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5160 o_dst += 1 + n_param;
5161 o_src += 1 + n_param;
5162 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5163 o_dst += n_before;
5164 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5165 c2, ma->p[i]->v->el + o_src, n_in);
5166 o_dst += n_in;
5167 o_src += n_in;
5168 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5169 o_dst += n_after;
5170 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5171 c2, ma->p[i]->v->el + o_src, n_div_ma);
5172 o_dst += n_div_ma;
5173 o_src += n_div_ma;
5174 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5175 if (has_denom)
5176 isl_int_mul(dst[0], dst[0], c1);
5180 /* Compute the pullback of "aff" by the function represented by "ma".
5181 * In other words, plug in "ma" in "aff". The result is an affine expression
5182 * defined over the domain space of "ma".
5184 * If "aff" is represented by
5186 * (a(p) + b x + c(divs))/d
5188 * and ma is represented by
5190 * x = D(p) + F(y) + G(divs')
5192 * then the result is
5194 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5196 * The divs in the local space of the input are similarly adjusted
5197 * through a call to isl_local_space_preimage_multi_aff.
5199 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5200 __isl_take isl_multi_aff *ma)
5202 isl_aff *res = NULL;
5203 isl_local_space *ls;
5204 int n_div_aff, n_div_ma;
5205 isl_int f, c1, c2, g;
5207 ma = isl_multi_aff_align_divs(ma);
5208 if (!aff || !ma)
5209 goto error;
5211 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5212 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5214 ls = isl_aff_get_domain_local_space(aff);
5215 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5216 res = isl_aff_alloc(ls);
5217 if (!res)
5218 goto error;
5220 isl_int_init(f);
5221 isl_int_init(c1);
5222 isl_int_init(c2);
5223 isl_int_init(g);
5225 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5226 f, c1, c2, g, 1);
5228 isl_int_clear(f);
5229 isl_int_clear(c1);
5230 isl_int_clear(c2);
5231 isl_int_clear(g);
5233 isl_aff_free(aff);
5234 isl_multi_aff_free(ma);
5235 res = isl_aff_normalize(res);
5236 return res;
5237 error:
5238 isl_aff_free(aff);
5239 isl_multi_aff_free(ma);
5240 isl_aff_free(res);
5241 return NULL;
5244 /* Compute the pullback of "aff1" by the function represented by "aff2".
5245 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5246 * defined over the domain space of "aff1".
5248 * The domain of "aff1" should match the range of "aff2", which means
5249 * that it should be single-dimensional.
5251 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5252 __isl_take isl_aff *aff2)
5254 isl_multi_aff *ma;
5256 ma = isl_multi_aff_from_aff(aff2);
5257 return isl_aff_pullback_multi_aff(aff1, ma);
5260 /* Compute the pullback of "ma1" by the function represented by "ma2".
5261 * In other words, plug in "ma2" in "ma1".
5263 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5265 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5266 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5268 int i;
5269 isl_space *space = NULL;
5271 ma2 = isl_multi_aff_align_divs(ma2);
5272 ma1 = isl_multi_aff_cow(ma1);
5273 if (!ma1 || !ma2)
5274 goto error;
5276 space = isl_space_join(isl_multi_aff_get_space(ma2),
5277 isl_multi_aff_get_space(ma1));
5279 for (i = 0; i < ma1->n; ++i) {
5280 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5281 isl_multi_aff_copy(ma2));
5282 if (!ma1->p[i])
5283 goto error;
5286 ma1 = isl_multi_aff_reset_space(ma1, space);
5287 isl_multi_aff_free(ma2);
5288 return ma1;
5289 error:
5290 isl_space_free(space);
5291 isl_multi_aff_free(ma2);
5292 isl_multi_aff_free(ma1);
5293 return NULL;
5296 /* Compute the pullback of "ma1" by the function represented by "ma2".
5297 * In other words, plug in "ma2" in "ma1".
5299 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5300 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5302 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5303 &isl_multi_aff_pullback_multi_aff_aligned);
5306 /* Extend the local space of "dst" to include the divs
5307 * in the local space of "src".
5309 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5310 __isl_keep isl_aff *src)
5312 isl_ctx *ctx;
5313 int *exp1 = NULL;
5314 int *exp2 = NULL;
5315 isl_mat *div;
5317 if (!src || !dst)
5318 return isl_aff_free(dst);
5320 ctx = isl_aff_get_ctx(src);
5321 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5322 isl_die(ctx, isl_error_invalid,
5323 "spaces don't match", goto error);
5325 if (src->ls->div->n_row == 0)
5326 return dst;
5328 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5329 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5330 if (!exp1 || (dst->ls->div->n_row && !exp2))
5331 goto error;
5333 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5334 dst = isl_aff_expand_divs(dst, div, exp2);
5335 free(exp1);
5336 free(exp2);
5338 return dst;
5339 error:
5340 free(exp1);
5341 free(exp2);
5342 return isl_aff_free(dst);
5345 /* Adjust the local spaces of the affine expressions in "maff"
5346 * such that they all have the save divs.
5348 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5349 __isl_take isl_multi_aff *maff)
5351 int i;
5353 if (!maff)
5354 return NULL;
5355 if (maff->n == 0)
5356 return maff;
5357 maff = isl_multi_aff_cow(maff);
5358 if (!maff)
5359 return NULL;
5361 for (i = 1; i < maff->n; ++i)
5362 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5363 for (i = 1; i < maff->n; ++i) {
5364 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5365 if (!maff->p[i])
5366 return isl_multi_aff_free(maff);
5369 return maff;
5372 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5374 aff = isl_aff_cow(aff);
5375 if (!aff)
5376 return NULL;
5378 aff->ls = isl_local_space_lift(aff->ls);
5379 if (!aff->ls)
5380 return isl_aff_free(aff);
5382 return aff;
5385 /* Lift "maff" to a space with extra dimensions such that the result
5386 * has no more existentially quantified variables.
5387 * If "ls" is not NULL, then *ls is assigned the local space that lies
5388 * at the basis of the lifting applied to "maff".
5390 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5391 __isl_give isl_local_space **ls)
5393 int i;
5394 isl_space *space;
5395 unsigned n_div;
5397 if (ls)
5398 *ls = NULL;
5400 if (!maff)
5401 return NULL;
5403 if (maff->n == 0) {
5404 if (ls) {
5405 isl_space *space = isl_multi_aff_get_domain_space(maff);
5406 *ls = isl_local_space_from_space(space);
5407 if (!*ls)
5408 return isl_multi_aff_free(maff);
5410 return maff;
5413 maff = isl_multi_aff_cow(maff);
5414 maff = isl_multi_aff_align_divs(maff);
5415 if (!maff)
5416 return NULL;
5418 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5419 space = isl_multi_aff_get_space(maff);
5420 space = isl_space_lift(isl_space_domain(space), n_div);
5421 space = isl_space_extend_domain_with_range(space,
5422 isl_multi_aff_get_space(maff));
5423 if (!space)
5424 return isl_multi_aff_free(maff);
5425 isl_space_free(maff->space);
5426 maff->space = space;
5428 if (ls) {
5429 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5430 if (!*ls)
5431 return isl_multi_aff_free(maff);
5434 for (i = 0; i < maff->n; ++i) {
5435 maff->p[i] = isl_aff_lift(maff->p[i]);
5436 if (!maff->p[i])
5437 goto error;
5440 return maff;
5441 error:
5442 if (ls)
5443 isl_local_space_free(*ls);
5444 return isl_multi_aff_free(maff);
5448 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5450 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5451 __isl_keep isl_pw_multi_aff *pma, int pos)
5453 int i;
5454 int n_out;
5455 isl_space *space;
5456 isl_pw_aff *pa;
5458 if (!pma)
5459 return NULL;
5461 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5462 if (pos < 0 || pos >= n_out)
5463 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5464 "index out of bounds", return NULL);
5466 space = isl_pw_multi_aff_get_space(pma);
5467 space = isl_space_drop_dims(space, isl_dim_out,
5468 pos + 1, n_out - pos - 1);
5469 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5471 pa = isl_pw_aff_alloc_size(space, pma->n);
5472 for (i = 0; i < pma->n; ++i) {
5473 isl_aff *aff;
5474 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5475 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5478 return pa;
5481 /* Return an isl_pw_multi_aff with the given "set" as domain and
5482 * an unnamed zero-dimensional range.
5484 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5485 __isl_take isl_set *set)
5487 isl_multi_aff *ma;
5488 isl_space *space;
5490 space = isl_set_get_space(set);
5491 space = isl_space_from_domain(space);
5492 ma = isl_multi_aff_zero(space);
5493 return isl_pw_multi_aff_alloc(set, ma);
5496 /* Add an isl_pw_multi_aff with the given "set" as domain and
5497 * an unnamed zero-dimensional range to *user.
5499 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5501 isl_union_pw_multi_aff **upma = user;
5502 isl_pw_multi_aff *pma;
5504 pma = isl_pw_multi_aff_from_domain(set);
5505 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5507 return 0;
5510 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5511 * an unnamed zero-dimensional range.
5513 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5514 __isl_take isl_union_set *uset)
5516 isl_space *space;
5517 isl_union_pw_multi_aff *upma;
5519 if (!uset)
5520 return NULL;
5522 space = isl_union_set_get_space(uset);
5523 upma = isl_union_pw_multi_aff_empty(space);
5525 if (isl_union_set_foreach_set(uset,
5526 &add_pw_multi_aff_from_domain, &upma) < 0)
5527 goto error;
5529 isl_union_set_free(uset);
5530 return upma;
5531 error:
5532 isl_union_set_free(uset);
5533 isl_union_pw_multi_aff_free(upma);
5534 return NULL;
5537 /* Convert "pma" to an isl_map and add it to *umap.
5539 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5541 isl_union_map **umap = user;
5542 isl_map *map;
5544 map = isl_map_from_pw_multi_aff(pma);
5545 *umap = isl_union_map_add_map(*umap, map);
5547 return 0;
5550 /* Construct a union map mapping the domain of the union
5551 * piecewise multi-affine expression to its range, with each dimension
5552 * in the range equated to the corresponding affine expression on its cell.
5554 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5555 __isl_take isl_union_pw_multi_aff *upma)
5557 isl_space *space;
5558 isl_union_map *umap;
5560 if (!upma)
5561 return NULL;
5563 space = isl_union_pw_multi_aff_get_space(upma);
5564 umap = isl_union_map_empty(space);
5566 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5567 &map_from_pw_multi_aff, &umap) < 0)
5568 goto error;
5570 isl_union_pw_multi_aff_free(upma);
5571 return umap;
5572 error:
5573 isl_union_pw_multi_aff_free(upma);
5574 isl_union_map_free(umap);
5575 return NULL;
5578 /* Local data for bin_entry and the callback "fn".
5580 struct isl_union_pw_multi_aff_bin_data {
5581 isl_union_pw_multi_aff *upma2;
5582 isl_union_pw_multi_aff *res;
5583 isl_pw_multi_aff *pma;
5584 int (*fn)(void **entry, void *user);
5587 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5588 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5590 static int bin_entry(void **entry, void *user)
5592 struct isl_union_pw_multi_aff_bin_data *data = user;
5593 isl_pw_multi_aff *pma = *entry;
5595 data->pma = pma;
5596 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5597 data->fn, data) < 0)
5598 return -1;
5600 return 0;
5603 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5604 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5605 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5606 * as *entry. The callback should adjust data->res if desired.
5608 static __isl_give isl_union_pw_multi_aff *bin_op(
5609 __isl_take isl_union_pw_multi_aff *upma1,
5610 __isl_take isl_union_pw_multi_aff *upma2,
5611 int (*fn)(void **entry, void *user))
5613 isl_space *space;
5614 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5616 space = isl_union_pw_multi_aff_get_space(upma2);
5617 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5618 space = isl_union_pw_multi_aff_get_space(upma1);
5619 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5621 if (!upma1 || !upma2)
5622 goto error;
5624 data.upma2 = upma2;
5625 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5626 upma1->table.n);
5627 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5628 &bin_entry, &data) < 0)
5629 goto error;
5631 isl_union_pw_multi_aff_free(upma1);
5632 isl_union_pw_multi_aff_free(upma2);
5633 return data.res;
5634 error:
5635 isl_union_pw_multi_aff_free(upma1);
5636 isl_union_pw_multi_aff_free(upma2);
5637 isl_union_pw_multi_aff_free(data.res);
5638 return NULL;
5641 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5642 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5644 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5645 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5647 isl_space *space;
5649 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5650 isl_pw_multi_aff_get_space(pma2));
5651 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5652 &isl_multi_aff_range_product);
5655 /* Given two isl_pw_multi_affs A -> B and C -> D,
5656 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5658 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5659 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5661 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5662 &pw_multi_aff_range_product);
5665 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5666 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5668 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5669 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5671 isl_space *space;
5673 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5674 isl_pw_multi_aff_get_space(pma2));
5675 space = isl_space_flatten_range(space);
5676 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5677 &isl_multi_aff_flat_range_product);
5680 /* Given two isl_pw_multi_affs A -> B and C -> D,
5681 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5683 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5684 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5686 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5687 &pw_multi_aff_flat_range_product);
5690 /* If data->pma and *entry have the same domain space, then compute
5691 * their flat range product and the result to data->res.
5693 static int flat_range_product_entry(void **entry, void *user)
5695 struct isl_union_pw_multi_aff_bin_data *data = user;
5696 isl_pw_multi_aff *pma2 = *entry;
5698 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5699 pma2->dim, isl_dim_in))
5700 return 0;
5702 pma2 = isl_pw_multi_aff_flat_range_product(
5703 isl_pw_multi_aff_copy(data->pma),
5704 isl_pw_multi_aff_copy(pma2));
5706 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5708 return 0;
5711 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5712 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5714 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5715 __isl_take isl_union_pw_multi_aff *upma1,
5716 __isl_take isl_union_pw_multi_aff *upma2)
5718 return bin_op(upma1, upma2, &flat_range_product_entry);
5721 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5722 * The parameters are assumed to have been aligned.
5724 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5725 * except that it works on two different isl_pw_* types.
5727 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5728 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5729 __isl_take isl_pw_aff *pa)
5731 int i, j, n;
5732 isl_pw_multi_aff *res = NULL;
5734 if (!pma || !pa)
5735 goto error;
5737 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5738 pa->dim, isl_dim_in))
5739 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5740 "domains don't match", goto error);
5741 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5742 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5743 "index out of bounds", goto error);
5745 n = pma->n * pa->n;
5746 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5748 for (i = 0; i < pma->n; ++i) {
5749 for (j = 0; j < pa->n; ++j) {
5750 isl_set *common;
5751 isl_multi_aff *res_ij;
5752 int empty;
5754 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5755 isl_set_copy(pa->p[j].set));
5756 empty = isl_set_plain_is_empty(common);
5757 if (empty < 0 || empty) {
5758 isl_set_free(common);
5759 if (empty < 0)
5760 goto error;
5761 continue;
5764 res_ij = isl_multi_aff_set_aff(
5765 isl_multi_aff_copy(pma->p[i].maff), pos,
5766 isl_aff_copy(pa->p[j].aff));
5767 res_ij = isl_multi_aff_gist(res_ij,
5768 isl_set_copy(common));
5770 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5774 isl_pw_multi_aff_free(pma);
5775 isl_pw_aff_free(pa);
5776 return res;
5777 error:
5778 isl_pw_multi_aff_free(pma);
5779 isl_pw_aff_free(pa);
5780 return isl_pw_multi_aff_free(res);
5783 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5785 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5786 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5787 __isl_take isl_pw_aff *pa)
5789 if (!pma || !pa)
5790 goto error;
5791 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5792 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5793 if (!isl_space_has_named_params(pma->dim) ||
5794 !isl_space_has_named_params(pa->dim))
5795 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5796 "unaligned unnamed parameters", goto error);
5797 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5798 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5799 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5800 error:
5801 isl_pw_multi_aff_free(pma);
5802 isl_pw_aff_free(pa);
5803 return NULL;
5806 /* Do the parameters of "pa" match those of "space"?
5808 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5809 __isl_keep isl_space *space)
5811 isl_space *pa_space;
5812 int match;
5814 if (!pa || !space)
5815 return -1;
5817 pa_space = isl_pw_aff_get_space(pa);
5819 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5821 isl_space_free(pa_space);
5822 return match;
5825 /* Check that the domain space of "pa" matches "space".
5827 * Return 0 on success and -1 on error.
5829 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5830 __isl_keep isl_space *space)
5832 isl_space *pa_space;
5833 int match;
5835 if (!pa || !space)
5836 return -1;
5838 pa_space = isl_pw_aff_get_space(pa);
5840 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5841 if (match < 0)
5842 goto error;
5843 if (!match)
5844 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5845 "parameters don't match", goto error);
5846 match = isl_space_tuple_is_equal(space, isl_dim_in,
5847 pa_space, isl_dim_in);
5848 if (match < 0)
5849 goto error;
5850 if (!match)
5851 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5852 "domains don't match", goto error);
5853 isl_space_free(pa_space);
5854 return 0;
5855 error:
5856 isl_space_free(pa_space);
5857 return -1;
5860 #undef BASE
5861 #define BASE pw_aff
5863 #include <isl_multi_templ.c>
5865 /* Scale the elements of "pma" by the corresponding elements of "mv".
5867 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5868 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5870 int i;
5872 pma = isl_pw_multi_aff_cow(pma);
5873 if (!pma || !mv)
5874 goto error;
5875 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5876 mv->space, isl_dim_set))
5877 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5878 "spaces don't match", goto error);
5879 if (!isl_space_match(pma->dim, isl_dim_param,
5880 mv->space, isl_dim_param)) {
5881 pma = isl_pw_multi_aff_align_params(pma,
5882 isl_multi_val_get_space(mv));
5883 mv = isl_multi_val_align_params(mv,
5884 isl_pw_multi_aff_get_space(pma));
5885 if (!pma || !mv)
5886 goto error;
5889 for (i = 0; i < pma->n; ++i) {
5890 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5891 isl_multi_val_copy(mv));
5892 if (!pma->p[i].maff)
5893 goto error;
5896 isl_multi_val_free(mv);
5897 return pma;
5898 error:
5899 isl_multi_val_free(mv);
5900 isl_pw_multi_aff_free(pma);
5901 return NULL;
5904 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5905 * mv contains the mv argument.
5906 * res collects the results.
5908 struct isl_union_pw_multi_aff_scale_multi_val_data {
5909 isl_multi_val *mv;
5910 isl_union_pw_multi_aff *res;
5913 /* This function is called for each entry of an isl_union_pw_multi_aff.
5914 * If the space of the entry matches that of data->mv,
5915 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5916 * to data->res.
5918 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5920 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5921 isl_pw_multi_aff *pma = *entry;
5923 if (!pma)
5924 return -1;
5925 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5926 data->mv->space, isl_dim_set))
5927 return 0;
5929 pma = isl_pw_multi_aff_copy(pma);
5930 pma = isl_pw_multi_aff_scale_multi_val(pma,
5931 isl_multi_val_copy(data->mv));
5932 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5933 if (!data->res)
5934 return -1;
5936 return 0;
5939 /* Scale the elements of "upma" by the corresponding elements of "mv",
5940 * for those entries that match the space of "mv".
5942 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5943 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5945 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5947 upma = isl_union_pw_multi_aff_align_params(upma,
5948 isl_multi_val_get_space(mv));
5949 mv = isl_multi_val_align_params(mv,
5950 isl_union_pw_multi_aff_get_space(upma));
5951 if (!upma || !mv)
5952 goto error;
5954 data.mv = mv;
5955 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
5956 upma->table.n);
5957 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
5958 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5959 goto error;
5961 isl_multi_val_free(mv);
5962 isl_union_pw_multi_aff_free(upma);
5963 return data.res;
5964 error:
5965 isl_multi_val_free(mv);
5966 isl_union_pw_multi_aff_free(upma);
5967 return NULL;
5970 /* Construct and return a piecewise multi affine expression
5971 * in the given space with value zero in each of the output dimensions and
5972 * a universe domain.
5974 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5976 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5979 /* Construct and return a piecewise multi affine expression
5980 * that is equal to the given piecewise affine expression.
5982 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5983 __isl_take isl_pw_aff *pa)
5985 int i;
5986 isl_space *space;
5987 isl_pw_multi_aff *pma;
5989 if (!pa)
5990 return NULL;
5992 space = isl_pw_aff_get_space(pa);
5993 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5995 for (i = 0; i < pa->n; ++i) {
5996 isl_set *set;
5997 isl_multi_aff *ma;
5999 set = isl_set_copy(pa->p[i].set);
6000 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6001 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6004 isl_pw_aff_free(pa);
6005 return pma;
6008 /* Construct a set or map mapping the shared (parameter) domain
6009 * of the piecewise affine expressions to the range of "mpa"
6010 * with each dimension in the range equated to the
6011 * corresponding piecewise affine expression.
6013 static __isl_give isl_map *map_from_multi_pw_aff(
6014 __isl_take isl_multi_pw_aff *mpa)
6016 int i;
6017 isl_space *space;
6018 isl_map *map;
6020 if (!mpa)
6021 return NULL;
6023 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6024 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6025 "invalid space", goto error);
6027 space = isl_multi_pw_aff_get_domain_space(mpa);
6028 map = isl_map_universe(isl_space_from_domain(space));
6030 for (i = 0; i < mpa->n; ++i) {
6031 isl_pw_aff *pa;
6032 isl_map *map_i;
6034 pa = isl_pw_aff_copy(mpa->p[i]);
6035 map_i = map_from_pw_aff(pa);
6037 map = isl_map_flat_range_product(map, map_i);
6040 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6042 isl_multi_pw_aff_free(mpa);
6043 return map;
6044 error:
6045 isl_multi_pw_aff_free(mpa);
6046 return NULL;
6049 /* Construct a map mapping the shared domain
6050 * of the piecewise affine expressions to the range of "mpa"
6051 * with each dimension in the range equated to the
6052 * corresponding piecewise affine expression.
6054 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6056 if (!mpa)
6057 return NULL;
6058 if (isl_space_is_set(mpa->space))
6059 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6060 "space of input is not a map", goto error);
6062 return map_from_multi_pw_aff(mpa);
6063 error:
6064 isl_multi_pw_aff_free(mpa);
6065 return NULL;
6068 /* Construct a set mapping the shared parameter domain
6069 * of the piecewise affine expressions to the space of "mpa"
6070 * with each dimension in the range equated to the
6071 * corresponding piecewise affine expression.
6073 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6075 if (!mpa)
6076 return NULL;
6077 if (!isl_space_is_set(mpa->space))
6078 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6079 "space of input is not a set", goto error);
6081 return map_from_multi_pw_aff(mpa);
6082 error:
6083 isl_multi_pw_aff_free(mpa);
6084 return NULL;
6087 /* Construct and return a piecewise multi affine expression
6088 * that is equal to the given multi piecewise affine expression
6089 * on the shared domain of the piecewise affine expressions.
6091 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6092 __isl_take isl_multi_pw_aff *mpa)
6094 int i;
6095 isl_space *space;
6096 isl_pw_aff *pa;
6097 isl_pw_multi_aff *pma;
6099 if (!mpa)
6100 return NULL;
6102 space = isl_multi_pw_aff_get_space(mpa);
6104 if (mpa->n == 0) {
6105 isl_multi_pw_aff_free(mpa);
6106 return isl_pw_multi_aff_zero(space);
6109 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6110 pma = isl_pw_multi_aff_from_pw_aff(pa);
6112 for (i = 1; i < mpa->n; ++i) {
6113 isl_pw_multi_aff *pma_i;
6115 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6116 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6117 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6120 pma = isl_pw_multi_aff_reset_space(pma, space);
6122 isl_multi_pw_aff_free(mpa);
6123 return pma;
6126 /* Construct and return a multi piecewise affine expression
6127 * that is equal to the given multi affine expression.
6129 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6130 __isl_take isl_multi_aff *ma)
6132 int i, n;
6133 isl_multi_pw_aff *mpa;
6135 if (!ma)
6136 return NULL;
6138 n = isl_multi_aff_dim(ma, isl_dim_out);
6139 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6141 for (i = 0; i < n; ++i) {
6142 isl_pw_aff *pa;
6144 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6145 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6148 isl_multi_aff_free(ma);
6149 return mpa;
6152 /* Construct and return a multi piecewise affine expression
6153 * that is equal to the given piecewise multi affine expression.
6155 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6156 __isl_take isl_pw_multi_aff *pma)
6158 int i, n;
6159 isl_space *space;
6160 isl_multi_pw_aff *mpa;
6162 if (!pma)
6163 return NULL;
6165 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6166 space = isl_pw_multi_aff_get_space(pma);
6167 mpa = isl_multi_pw_aff_alloc(space);
6169 for (i = 0; i < n; ++i) {
6170 isl_pw_aff *pa;
6172 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6173 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6176 isl_pw_multi_aff_free(pma);
6177 return mpa;
6180 /* Do "pa1" and "pa2" represent the same function?
6182 * We first check if they are obviously equal.
6183 * If not, we convert them to maps and check if those are equal.
6185 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6187 int equal;
6188 isl_map *map1, *map2;
6190 if (!pa1 || !pa2)
6191 return -1;
6193 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6194 if (equal < 0 || equal)
6195 return equal;
6197 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6198 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6199 equal = isl_map_is_equal(map1, map2);
6200 isl_map_free(map1);
6201 isl_map_free(map2);
6203 return equal;
6206 /* Do "mpa1" and "mpa2" represent the same function?
6208 * Note that we cannot convert the entire isl_multi_pw_aff
6209 * to a map because the domains of the piecewise affine expressions
6210 * may not be the same.
6212 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6213 __isl_keep isl_multi_pw_aff *mpa2)
6215 int i;
6216 int equal;
6218 if (!mpa1 || !mpa2)
6219 return -1;
6221 if (!isl_space_match(mpa1->space, isl_dim_param,
6222 mpa2->space, isl_dim_param)) {
6223 if (!isl_space_has_named_params(mpa1->space))
6224 return 0;
6225 if (!isl_space_has_named_params(mpa2->space))
6226 return 0;
6227 mpa1 = isl_multi_pw_aff_copy(mpa1);
6228 mpa2 = isl_multi_pw_aff_copy(mpa2);
6229 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6230 isl_multi_pw_aff_get_space(mpa2));
6231 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6232 isl_multi_pw_aff_get_space(mpa1));
6233 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6234 isl_multi_pw_aff_free(mpa1);
6235 isl_multi_pw_aff_free(mpa2);
6236 return equal;
6239 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6240 if (equal < 0 || !equal)
6241 return equal;
6243 for (i = 0; i < mpa1->n; ++i) {
6244 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6245 if (equal < 0 || !equal)
6246 return equal;
6249 return 1;
6252 /* Coalesce the elements of "mpa".
6254 * Note that such coalescing does not change the meaning of "mpa"
6255 * so there is no need to cow. We do need to be careful not to
6256 * destroy any other copies of "mpa" in case of failure.
6258 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6259 __isl_take isl_multi_pw_aff *mpa)
6261 int i;
6263 if (!mpa)
6264 return NULL;
6266 for (i = 0; i < mpa->n; ++i) {
6267 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6268 pa = isl_pw_aff_coalesce(pa);
6269 if (!pa)
6270 return isl_multi_pw_aff_free(mpa);
6271 isl_pw_aff_free(mpa->p[i]);
6272 mpa->p[i] = pa;
6275 return mpa;
6278 /* Compute the pullback of "mpa" by the function represented by "ma".
6279 * In other words, plug in "ma" in "mpa".
6281 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6283 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6284 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6286 int i;
6287 isl_space *space = NULL;
6289 mpa = isl_multi_pw_aff_cow(mpa);
6290 if (!mpa || !ma)
6291 goto error;
6293 space = isl_space_join(isl_multi_aff_get_space(ma),
6294 isl_multi_pw_aff_get_space(mpa));
6295 if (!space)
6296 goto error;
6298 for (i = 0; i < mpa->n; ++i) {
6299 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6300 isl_multi_aff_copy(ma));
6301 if (!mpa->p[i])
6302 goto error;
6305 isl_multi_aff_free(ma);
6306 isl_space_free(mpa->space);
6307 mpa->space = space;
6308 return mpa;
6309 error:
6310 isl_space_free(space);
6311 isl_multi_pw_aff_free(mpa);
6312 isl_multi_aff_free(ma);
6313 return NULL;
6316 /* Compute the pullback of "mpa" by the function represented by "ma".
6317 * In other words, plug in "ma" in "mpa".
6319 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6320 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6322 if (!mpa || !ma)
6323 goto error;
6324 if (isl_space_match(mpa->space, isl_dim_param,
6325 ma->space, isl_dim_param))
6326 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6327 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6328 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6329 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6330 error:
6331 isl_multi_pw_aff_free(mpa);
6332 isl_multi_aff_free(ma);
6333 return NULL;
6336 /* Compute the pullback of "mpa" by the function represented by "pma".
6337 * In other words, plug in "pma" in "mpa".
6339 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6341 static __isl_give isl_multi_pw_aff *
6342 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6343 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6345 int i;
6346 isl_space *space = NULL;
6348 mpa = isl_multi_pw_aff_cow(mpa);
6349 if (!mpa || !pma)
6350 goto error;
6352 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6353 isl_multi_pw_aff_get_space(mpa));
6355 for (i = 0; i < mpa->n; ++i) {
6356 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6357 isl_pw_multi_aff_copy(pma));
6358 if (!mpa->p[i])
6359 goto error;
6362 isl_pw_multi_aff_free(pma);
6363 isl_space_free(mpa->space);
6364 mpa->space = space;
6365 return mpa;
6366 error:
6367 isl_space_free(space);
6368 isl_multi_pw_aff_free(mpa);
6369 isl_pw_multi_aff_free(pma);
6370 return NULL;
6373 /* Compute the pullback of "mpa" by the function represented by "pma".
6374 * In other words, plug in "pma" in "mpa".
6376 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6377 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6379 if (!mpa || !pma)
6380 goto error;
6381 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6382 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6383 mpa = isl_multi_pw_aff_align_params(mpa,
6384 isl_pw_multi_aff_get_space(pma));
6385 pma = isl_pw_multi_aff_align_params(pma,
6386 isl_multi_pw_aff_get_space(mpa));
6387 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6388 error:
6389 isl_multi_pw_aff_free(mpa);
6390 isl_pw_multi_aff_free(pma);
6391 return NULL;
6394 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6395 * with the domain of "aff". The domain of the result is the same
6396 * as that of "mpa".
6397 * "mpa" and "aff" are assumed to have been aligned.
6399 * We first extract the parametric constant from "aff", defined
6400 * over the correct domain.
6401 * Then we add the appropriate combinations of the members of "mpa".
6402 * Finally, we add the integer divisions through recursive calls.
6404 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6405 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6407 int i, n_param, n_in, n_div;
6408 isl_space *space;
6409 isl_val *v;
6410 isl_pw_aff *pa;
6411 isl_aff *tmp;
6413 n_param = isl_aff_dim(aff, isl_dim_param);
6414 n_in = isl_aff_dim(aff, isl_dim_in);
6415 n_div = isl_aff_dim(aff, isl_dim_div);
6417 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6418 tmp = isl_aff_copy(aff);
6419 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6420 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6421 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6422 isl_space_dim(space, isl_dim_set));
6423 tmp = isl_aff_reset_domain_space(tmp, space);
6424 pa = isl_pw_aff_from_aff(tmp);
6426 for (i = 0; i < n_in; ++i) {
6427 isl_pw_aff *pa_i;
6429 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6430 continue;
6431 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6432 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6433 pa_i = isl_pw_aff_scale_val(pa_i, v);
6434 pa = isl_pw_aff_add(pa, pa_i);
6437 for (i = 0; i < n_div; ++i) {
6438 isl_aff *div;
6439 isl_pw_aff *pa_i;
6441 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6442 continue;
6443 div = isl_aff_get_div(aff, i);
6444 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6445 isl_multi_pw_aff_copy(mpa), div);
6446 pa_i = isl_pw_aff_floor(pa_i);
6447 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6448 pa_i = isl_pw_aff_scale_val(pa_i, v);
6449 pa = isl_pw_aff_add(pa, pa_i);
6452 isl_multi_pw_aff_free(mpa);
6453 isl_aff_free(aff);
6455 return pa;
6458 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6459 * with the domain of "aff". The domain of the result is the same
6460 * as that of "mpa".
6462 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6463 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6465 if (!aff || !mpa)
6466 goto error;
6467 if (isl_space_match(aff->ls->dim, isl_dim_param,
6468 mpa->space, isl_dim_param))
6469 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6471 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6472 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6474 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6475 error:
6476 isl_aff_free(aff);
6477 isl_multi_pw_aff_free(mpa);
6478 return NULL;
6481 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6482 * with the domain of "pa". The domain of the result is the same
6483 * as that of "mpa".
6484 * "mpa" and "pa" are assumed to have been aligned.
6486 * We consider each piece in turn. Note that the domains of the
6487 * pieces are assumed to be disjoint and they remain disjoint
6488 * after taking the preimage (over the same function).
6490 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6491 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6493 isl_space *space;
6494 isl_pw_aff *res;
6495 int i;
6497 if (!mpa || !pa)
6498 goto error;
6500 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6501 isl_pw_aff_get_space(pa));
6502 res = isl_pw_aff_empty(space);
6504 for (i = 0; i < pa->n; ++i) {
6505 isl_pw_aff *pa_i;
6506 isl_set *domain;
6508 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6509 isl_multi_pw_aff_copy(mpa),
6510 isl_aff_copy(pa->p[i].aff));
6511 domain = isl_set_copy(pa->p[i].set);
6512 domain = isl_set_preimage_multi_pw_aff(domain,
6513 isl_multi_pw_aff_copy(mpa));
6514 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6515 res = isl_pw_aff_add_disjoint(res, pa_i);
6518 isl_pw_aff_free(pa);
6519 isl_multi_pw_aff_free(mpa);
6520 return res;
6521 error:
6522 isl_pw_aff_free(pa);
6523 isl_multi_pw_aff_free(mpa);
6524 return NULL;
6527 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6528 * with the domain of "pa". The domain of the result is the same
6529 * as that of "mpa".
6531 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6532 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6534 if (!pa || !mpa)
6535 goto error;
6536 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6537 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6539 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6540 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6542 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6543 error:
6544 isl_pw_aff_free(pa);
6545 isl_multi_pw_aff_free(mpa);
6546 return NULL;
6549 /* Compute the pullback of "pa" by the function represented by "mpa".
6550 * In other words, plug in "mpa" in "pa".
6551 * "pa" and "mpa" are assumed to have been aligned.
6553 * The pullback is computed by applying "pa" to "mpa".
6555 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6556 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6558 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6561 /* Compute the pullback of "pa" by the function represented by "mpa".
6562 * In other words, plug in "mpa" in "pa".
6564 * The pullback is computed by applying "pa" to "mpa".
6566 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6567 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6569 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6572 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6573 * In other words, plug in "mpa2" in "mpa1".
6575 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6577 * We pullback each member of "mpa1" in turn.
6579 static __isl_give isl_multi_pw_aff *
6580 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6581 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6583 int i;
6584 isl_space *space = NULL;
6586 mpa1 = isl_multi_pw_aff_cow(mpa1);
6587 if (!mpa1 || !mpa2)
6588 goto error;
6590 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6591 isl_multi_pw_aff_get_space(mpa1));
6593 for (i = 0; i < mpa1->n; ++i) {
6594 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6595 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6596 if (!mpa1->p[i])
6597 goto error;
6600 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6602 isl_multi_pw_aff_free(mpa2);
6603 return mpa1;
6604 error:
6605 isl_space_free(space);
6606 isl_multi_pw_aff_free(mpa1);
6607 isl_multi_pw_aff_free(mpa2);
6608 return NULL;
6611 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6612 * In other words, plug in "mpa2" in "mpa1".
6614 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6615 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6617 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6618 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6621 /* Compare two isl_affs.
6623 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6624 * than "aff2" and 0 if they are equal.
6626 * The order is fairly arbitrary. We do consider expressions that only involve
6627 * earlier dimensions as "smaller".
6629 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6631 int cmp;
6632 int last1, last2;
6634 if (aff1 == aff2)
6635 return 0;
6637 if (!aff1)
6638 return -1;
6639 if (!aff2)
6640 return 1;
6642 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6643 if (cmp != 0)
6644 return cmp;
6646 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6647 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6648 if (last1 != last2)
6649 return last1 - last2;
6651 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6654 /* Compare two isl_pw_affs.
6656 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6657 * than "pa2" and 0 if they are equal.
6659 * The order is fairly arbitrary. We do consider expressions that only involve
6660 * earlier dimensions as "smaller".
6662 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6663 __isl_keep isl_pw_aff *pa2)
6665 int i;
6666 int cmp;
6668 if (pa1 == pa2)
6669 return 0;
6671 if (!pa1)
6672 return -1;
6673 if (!pa2)
6674 return 1;
6676 cmp = isl_space_cmp(pa1->dim, pa2->dim);
6677 if (cmp != 0)
6678 return cmp;
6680 if (pa1->n != pa2->n)
6681 return pa1->n - pa2->n;
6683 for (i = 0; i < pa1->n; ++i) {
6684 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
6685 if (cmp != 0)
6686 return cmp;
6687 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
6688 if (cmp != 0)
6689 return cmp;
6692 return 0;
6695 /* Return a piecewise affine expression that is equal to "v" on "domain".
6697 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
6698 __isl_take isl_val *v)
6700 isl_space *space;
6701 isl_local_space *ls;
6702 isl_aff *aff;
6704 space = isl_set_get_space(domain);
6705 ls = isl_local_space_from_space(space);
6706 aff = isl_aff_val_on_domain(ls, v);
6708 return isl_pw_aff_alloc(domain, aff);
6711 /* Return a multi affine expression that is equal to "mv" on domain
6712 * space "space".
6714 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
6715 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
6717 int i, n;
6718 isl_space *space2;
6719 isl_local_space *ls;
6720 isl_multi_aff *ma;
6722 if (!space || !mv)
6723 goto error;
6725 n = isl_multi_val_dim(mv, isl_dim_set);
6726 space2 = isl_multi_val_get_space(mv);
6727 space2 = isl_space_align_params(space2, isl_space_copy(space));
6728 space = isl_space_align_params(space, isl_space_copy(space2));
6729 space = isl_space_map_from_domain_and_range(space, space2);
6730 ma = isl_multi_aff_alloc(isl_space_copy(space));
6731 ls = isl_local_space_from_space(isl_space_domain(space));
6732 for (i = 0; i < n; ++i) {
6733 isl_val *v;
6734 isl_aff *aff;
6736 v = isl_multi_val_get_val(mv, i);
6737 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
6738 ma = isl_multi_aff_set_aff(ma, i, aff);
6740 isl_local_space_free(ls);
6742 isl_multi_val_free(mv);
6743 return ma;
6744 error:
6745 isl_space_free(space);
6746 isl_multi_val_free(mv);
6747 return NULL;
6750 /* Return a piecewise multi-affine expression
6751 * that is equal to "mv" on "domain".
6753 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
6754 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
6756 isl_space *space;
6757 isl_multi_aff *ma;
6759 space = isl_set_get_space(domain);
6760 ma = isl_multi_aff_multi_val_on_space(space, mv);
6762 return isl_pw_multi_aff_alloc(domain, ma);
6765 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6766 * mv is the value that should be attained on each domain set
6767 * res collects the results
6769 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
6770 isl_multi_val *mv;
6771 isl_union_pw_multi_aff *res;
6774 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6775 * and add it to data->res.
6777 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
6778 void *user)
6780 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
6781 isl_pw_multi_aff *pma;
6782 isl_multi_val *mv;
6784 mv = isl_multi_val_copy(data->mv);
6785 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
6786 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6788 return data->res ? 0 : -1;
6791 /* Return a union piecewise multi-affine expression
6792 * that is equal to "mv" on "domain".
6794 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
6795 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
6797 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
6798 isl_space *space;
6800 space = isl_union_set_get_space(domain);
6801 data.res = isl_union_pw_multi_aff_empty(space);
6802 data.mv = mv;
6803 if (isl_union_set_foreach_set(domain,
6804 &pw_multi_aff_multi_val_on_domain, &data) < 0)
6805 data.res = isl_union_pw_multi_aff_free(data.res);
6806 isl_union_set_free(domain);
6807 isl_multi_val_free(mv);
6808 return data.res;