Fix some typos in comments
[isl.git] / isl_aff.c
blob1bc39033fbeae8ed30b1218702b289925c510289
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 #undef BASE
41 #define BASE union_pw_multi_aff
43 #include <isl_list_templ.c>
45 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
46 __isl_take isl_vec *v)
48 isl_aff *aff;
50 if (!ls || !v)
51 goto error;
53 aff = isl_calloc_type(v->ctx, struct isl_aff);
54 if (!aff)
55 goto error;
57 aff->ref = 1;
58 aff->ls = ls;
59 aff->v = v;
61 return aff;
62 error:
63 isl_local_space_free(ls);
64 isl_vec_free(v);
65 return NULL;
68 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
70 isl_ctx *ctx;
71 isl_vec *v;
72 unsigned total;
74 if (!ls)
75 return NULL;
77 ctx = isl_local_space_get_ctx(ls);
78 if (!isl_local_space_divs_known(ls))
79 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
80 goto error);
81 if (!isl_local_space_is_set(ls))
82 isl_die(ctx, isl_error_invalid,
83 "domain of affine expression should be a set",
84 goto error);
86 total = isl_local_space_dim(ls, isl_dim_all);
87 v = isl_vec_alloc(ctx, 1 + 1 + total);
88 return isl_aff_alloc_vec(ls, v);
89 error:
90 isl_local_space_free(ls);
91 return NULL;
94 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
96 isl_aff *aff;
98 aff = isl_aff_alloc(ls);
99 if (!aff)
100 return NULL;
102 isl_int_set_si(aff->v->el[0], 1);
103 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
105 return aff;
108 /* Return a piecewise affine expression defined on the specified domain
109 * that is equal to zero.
111 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
113 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
116 /* Return an affine expression defined on the specified domain
117 * that represents NaN.
119 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
121 isl_aff *aff;
123 aff = isl_aff_alloc(ls);
124 if (!aff)
125 return NULL;
127 isl_seq_clr(aff->v->el, aff->v->size);
129 return aff;
132 /* Return a piecewise affine expression defined on the specified domain
133 * that represents NaN.
135 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
137 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
140 /* Return an affine expression that is equal to "val" on
141 * domain local space "ls".
143 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
144 __isl_take isl_val *val)
146 isl_aff *aff;
148 if (!ls || !val)
149 goto error;
150 if (!isl_val_is_rat(val))
151 isl_die(isl_val_get_ctx(val), isl_error_invalid,
152 "expecting rational value", goto error);
154 aff = isl_aff_alloc(isl_local_space_copy(ls));
155 if (!aff)
156 goto error;
158 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
159 isl_int_set(aff->v->el[1], val->n);
160 isl_int_set(aff->v->el[0], val->d);
162 isl_local_space_free(ls);
163 isl_val_free(val);
164 return aff;
165 error:
166 isl_local_space_free(ls);
167 isl_val_free(val);
168 return NULL;
171 /* Return an affine expression that is equal to the specified dimension
172 * in "ls".
174 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
175 enum isl_dim_type type, unsigned pos)
177 isl_space *space;
178 isl_aff *aff;
180 if (!ls)
181 return NULL;
183 space = isl_local_space_get_space(ls);
184 if (!space)
185 goto error;
186 if (isl_space_is_map(space))
187 isl_die(isl_space_get_ctx(space), isl_error_invalid,
188 "expecting (parameter) set space", goto error);
189 if (pos >= isl_local_space_dim(ls, type))
190 isl_die(isl_space_get_ctx(space), isl_error_invalid,
191 "position out of bounds", goto error);
193 isl_space_free(space);
194 aff = isl_aff_alloc(ls);
195 if (!aff)
196 return NULL;
198 pos += isl_local_space_offset(aff->ls, type);
200 isl_int_set_si(aff->v->el[0], 1);
201 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
202 isl_int_set_si(aff->v->el[1 + pos], 1);
204 return aff;
205 error:
206 isl_local_space_free(ls);
207 isl_space_free(space);
208 return NULL;
211 /* Return a piecewise affine expression that is equal to
212 * the specified dimension in "ls".
214 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
215 enum isl_dim_type type, unsigned pos)
217 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
220 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
222 if (!aff)
223 return NULL;
225 aff->ref++;
226 return aff;
229 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
231 if (!aff)
232 return NULL;
234 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
235 isl_vec_copy(aff->v));
238 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
240 if (!aff)
241 return NULL;
243 if (aff->ref == 1)
244 return aff;
245 aff->ref--;
246 return isl_aff_dup(aff);
249 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
251 if (!aff)
252 return NULL;
254 if (--aff->ref > 0)
255 return NULL;
257 isl_local_space_free(aff->ls);
258 isl_vec_free(aff->v);
260 free(aff);
262 return NULL;
265 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
267 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
270 /* Externally, an isl_aff has a map space, but internally, the
271 * ls field corresponds to the domain of that space.
273 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
275 if (!aff)
276 return 0;
277 if (type == isl_dim_out)
278 return 1;
279 if (type == isl_dim_in)
280 type = isl_dim_set;
281 return isl_local_space_dim(aff->ls, type);
284 /* Return the position of the dimension of the given type and name
285 * in "aff".
286 * Return -1 if no such dimension can be found.
288 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
289 const char *name)
291 if (!aff)
292 return -1;
293 if (type == isl_dim_out)
294 return -1;
295 if (type == isl_dim_in)
296 type = isl_dim_set;
297 return isl_local_space_find_dim_by_name(aff->ls, type, name);
300 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
302 return aff ? isl_local_space_get_space(aff->ls) : NULL;
305 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
307 isl_space *space;
308 if (!aff)
309 return NULL;
310 space = isl_local_space_get_space(aff->ls);
311 space = isl_space_from_domain(space);
312 space = isl_space_add_dims(space, isl_dim_out, 1);
313 return space;
316 __isl_give isl_local_space *isl_aff_get_domain_local_space(
317 __isl_keep isl_aff *aff)
319 return aff ? isl_local_space_copy(aff->ls) : NULL;
322 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
324 isl_local_space *ls;
325 if (!aff)
326 return NULL;
327 ls = isl_local_space_copy(aff->ls);
328 ls = isl_local_space_from_domain(ls);
329 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
330 return ls;
333 /* Externally, an isl_aff has a map space, but internally, the
334 * ls field corresponds to the domain of that space.
336 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
337 enum isl_dim_type type, unsigned pos)
339 if (!aff)
340 return NULL;
341 if (type == isl_dim_out)
342 return NULL;
343 if (type == isl_dim_in)
344 type = isl_dim_set;
345 return isl_local_space_get_dim_name(aff->ls, type, pos);
348 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
349 __isl_take isl_space *dim)
351 aff = isl_aff_cow(aff);
352 if (!aff || !dim)
353 goto error;
355 aff->ls = isl_local_space_reset_space(aff->ls, dim);
356 if (!aff->ls)
357 return isl_aff_free(aff);
359 return aff;
360 error:
361 isl_aff_free(aff);
362 isl_space_free(dim);
363 return NULL;
366 /* Reset the space of "aff". This function is called from isl_pw_templ.c
367 * and doesn't know if the space of an element object is represented
368 * directly or through its domain. It therefore passes along both.
370 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
371 __isl_take isl_space *space, __isl_take isl_space *domain)
373 isl_space_free(space);
374 return isl_aff_reset_domain_space(aff, domain);
377 /* Reorder the coefficients of the affine expression based
378 * on the given reodering.
379 * The reordering r is assumed to have been extended with the local
380 * variables.
382 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
383 __isl_take isl_reordering *r, int n_div)
385 isl_vec *res;
386 int i;
388 if (!vec || !r)
389 goto error;
391 res = isl_vec_alloc(vec->ctx,
392 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
393 isl_seq_cpy(res->el, vec->el, 2);
394 isl_seq_clr(res->el + 2, res->size - 2);
395 for (i = 0; i < r->len; ++i)
396 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
398 isl_reordering_free(r);
399 isl_vec_free(vec);
400 return res;
401 error:
402 isl_vec_free(vec);
403 isl_reordering_free(r);
404 return NULL;
407 /* Reorder the dimensions of the domain of "aff" according
408 * to the given reordering.
410 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
411 __isl_take isl_reordering *r)
413 aff = isl_aff_cow(aff);
414 if (!aff)
415 goto error;
417 r = isl_reordering_extend(r, aff->ls->div->n_row);
418 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
419 aff->ls->div->n_row);
420 aff->ls = isl_local_space_realign(aff->ls, r);
422 if (!aff->v || !aff->ls)
423 return isl_aff_free(aff);
425 return aff;
426 error:
427 isl_aff_free(aff);
428 isl_reordering_free(r);
429 return NULL;
432 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
433 __isl_take isl_space *model)
435 if (!aff || !model)
436 goto error;
438 if (!isl_space_match(aff->ls->dim, isl_dim_param,
439 model, isl_dim_param)) {
440 isl_reordering *exp;
442 model = isl_space_drop_dims(model, isl_dim_in,
443 0, isl_space_dim(model, isl_dim_in));
444 model = isl_space_drop_dims(model, isl_dim_out,
445 0, isl_space_dim(model, isl_dim_out));
446 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
447 exp = isl_reordering_extend_space(exp,
448 isl_aff_get_domain_space(aff));
449 aff = isl_aff_realign_domain(aff, exp);
452 isl_space_free(model);
453 return aff;
454 error:
455 isl_space_free(model);
456 isl_aff_free(aff);
457 return NULL;
460 /* Is "aff" obviously equal to zero?
462 * If the denominator is zero, then "aff" is not equal to zero.
464 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
466 if (!aff)
467 return -1;
469 if (isl_int_is_zero(aff->v->el[0]))
470 return 0;
471 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
474 /* Does "aff" represent NaN?
476 int isl_aff_is_nan(__isl_keep isl_aff *aff)
478 if (!aff)
479 return -1;
481 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
484 /* Does "pa" involve any NaNs?
486 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
488 int i;
490 if (!pa)
491 return -1;
492 if (pa->n == 0)
493 return 0;
495 for (i = 0; i < pa->n; ++i) {
496 int is_nan = isl_aff_is_nan(pa->p[i].aff);
497 if (is_nan < 0 || is_nan)
498 return is_nan;
501 return 0;
504 /* Are "aff1" and "aff2" obviously equal?
506 * NaN is not equal to anything, not even to another NaN.
508 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
510 int equal;
512 if (!aff1 || !aff2)
513 return -1;
515 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
516 return 0;
518 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
519 if (equal < 0 || !equal)
520 return equal;
522 return isl_vec_is_equal(aff1->v, aff2->v);
525 /* Return the common denominator of "aff" in "v".
527 * We cannot return anything meaningful in case of a NaN.
529 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
531 if (!aff)
532 return -1;
533 if (isl_aff_is_nan(aff))
534 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
535 "cannot get denominator of NaN", return -1);
536 isl_int_set(*v, aff->v->el[0]);
537 return 0;
540 /* Return the common denominator of "aff".
542 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
544 isl_ctx *ctx;
546 if (!aff)
547 return NULL;
549 ctx = isl_aff_get_ctx(aff);
550 if (isl_aff_is_nan(aff))
551 return isl_val_nan(ctx);
552 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
555 /* Return the constant term of "aff" in "v".
557 * We cannot return anything meaningful in case of a NaN.
559 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
561 if (!aff)
562 return -1;
563 if (isl_aff_is_nan(aff))
564 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
565 "cannot get constant term of NaN", return -1);
566 isl_int_set(*v, aff->v->el[1]);
567 return 0;
570 /* Return the constant term of "aff".
572 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
574 isl_ctx *ctx;
575 isl_val *v;
577 if (!aff)
578 return NULL;
580 ctx = isl_aff_get_ctx(aff);
581 if (isl_aff_is_nan(aff))
582 return isl_val_nan(ctx);
583 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
584 return isl_val_normalize(v);
587 /* Return the coefficient of the variable of type "type" at position "pos"
588 * of "aff" in "v".
590 * We cannot return anything meaningful in case of a NaN.
592 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
593 enum isl_dim_type type, int pos, isl_int *v)
595 if (!aff)
596 return -1;
598 if (type == isl_dim_out)
599 isl_die(aff->v->ctx, isl_error_invalid,
600 "output/set dimension does not have a coefficient",
601 return -1);
602 if (type == isl_dim_in)
603 type = isl_dim_set;
605 if (pos >= isl_local_space_dim(aff->ls, type))
606 isl_die(aff->v->ctx, isl_error_invalid,
607 "position out of bounds", return -1);
609 if (isl_aff_is_nan(aff))
610 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
611 "cannot get coefficient of NaN", return -1);
612 pos += isl_local_space_offset(aff->ls, type);
613 isl_int_set(*v, aff->v->el[1 + pos]);
615 return 0;
618 /* Return the coefficient of the variable of type "type" at position "pos"
619 * of "aff".
621 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
622 enum isl_dim_type type, int pos)
624 isl_ctx *ctx;
625 isl_val *v;
627 if (!aff)
628 return NULL;
630 ctx = isl_aff_get_ctx(aff);
631 if (type == isl_dim_out)
632 isl_die(ctx, isl_error_invalid,
633 "output/set dimension does not have a coefficient",
634 return NULL);
635 if (type == isl_dim_in)
636 type = isl_dim_set;
638 if (pos >= isl_local_space_dim(aff->ls, type))
639 isl_die(ctx, isl_error_invalid,
640 "position out of bounds", return NULL);
642 if (isl_aff_is_nan(aff))
643 return isl_val_nan(ctx);
644 pos += isl_local_space_offset(aff->ls, type);
645 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
646 return isl_val_normalize(v);
649 /* Return the sign of the coefficient of the variable of type "type"
650 * at position "pos" of "aff".
652 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
653 int pos)
655 isl_ctx *ctx;
657 if (!aff)
658 return 0;
660 ctx = isl_aff_get_ctx(aff);
661 if (type == isl_dim_out)
662 isl_die(ctx, isl_error_invalid,
663 "output/set dimension does not have a coefficient",
664 return 0);
665 if (type == isl_dim_in)
666 type = isl_dim_set;
668 if (pos >= isl_local_space_dim(aff->ls, type))
669 isl_die(ctx, isl_error_invalid,
670 "position out of bounds", return 0);
672 pos += isl_local_space_offset(aff->ls, type);
673 return isl_int_sgn(aff->v->el[1 + pos]);
676 /* Replace the denominator of "aff" by "v".
678 * A NaN is unaffected by this operation.
680 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
682 if (!aff)
683 return NULL;
684 if (isl_aff_is_nan(aff))
685 return aff;
686 aff = isl_aff_cow(aff);
687 if (!aff)
688 return NULL;
690 aff->v = isl_vec_cow(aff->v);
691 if (!aff->v)
692 return isl_aff_free(aff);
694 isl_int_set(aff->v->el[0], v);
696 return aff;
699 /* Replace the numerator of the constant term of "aff" by "v".
701 * A NaN is unaffected by this operation.
703 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
705 if (!aff)
706 return NULL;
707 if (isl_aff_is_nan(aff))
708 return aff;
709 aff = isl_aff_cow(aff);
710 if (!aff)
711 return NULL;
713 aff->v = isl_vec_cow(aff->v);
714 if (!aff->v)
715 return isl_aff_free(aff);
717 isl_int_set(aff->v->el[1], v);
719 return aff;
722 /* Replace the constant term of "aff" by "v".
724 * A NaN is unaffected by this operation.
726 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
727 __isl_take isl_val *v)
729 if (!aff || !v)
730 goto error;
732 if (isl_aff_is_nan(aff)) {
733 isl_val_free(v);
734 return aff;
737 if (!isl_val_is_rat(v))
738 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
739 "expecting rational value", goto error);
741 if (isl_int_eq(aff->v->el[1], v->n) &&
742 isl_int_eq(aff->v->el[0], v->d)) {
743 isl_val_free(v);
744 return aff;
747 aff = isl_aff_cow(aff);
748 if (!aff)
749 goto error;
750 aff->v = isl_vec_cow(aff->v);
751 if (!aff->v)
752 goto error;
754 if (isl_int_eq(aff->v->el[0], v->d)) {
755 isl_int_set(aff->v->el[1], v->n);
756 } else if (isl_int_is_one(v->d)) {
757 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
758 } else {
759 isl_seq_scale(aff->v->el + 1,
760 aff->v->el + 1, v->d, aff->v->size - 1);
761 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
762 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
763 aff->v = isl_vec_normalize(aff->v);
764 if (!aff->v)
765 goto error;
768 isl_val_free(v);
769 return aff;
770 error:
771 isl_aff_free(aff);
772 isl_val_free(v);
773 return NULL;
776 /* Add "v" to the constant term of "aff".
778 * A NaN is unaffected by this operation.
780 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
782 if (isl_int_is_zero(v))
783 return aff;
785 if (!aff)
786 return NULL;
787 if (isl_aff_is_nan(aff))
788 return aff;
789 aff = isl_aff_cow(aff);
790 if (!aff)
791 return NULL;
793 aff->v = isl_vec_cow(aff->v);
794 if (!aff->v)
795 return isl_aff_free(aff);
797 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
799 return aff;
802 /* Add "v" to the constant term of "aff".
804 * A NaN is unaffected by this operation.
806 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
807 __isl_take isl_val *v)
809 if (!aff || !v)
810 goto error;
812 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
813 isl_val_free(v);
814 return aff;
817 if (!isl_val_is_rat(v))
818 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
819 "expecting rational value", goto error);
821 aff = isl_aff_cow(aff);
822 if (!aff)
823 goto error;
825 aff->v = isl_vec_cow(aff->v);
826 if (!aff->v)
827 goto error;
829 if (isl_int_is_one(v->d)) {
830 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
831 } else if (isl_int_eq(aff->v->el[0], v->d)) {
832 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
833 aff->v = isl_vec_normalize(aff->v);
834 if (!aff->v)
835 goto error;
836 } else {
837 isl_seq_scale(aff->v->el + 1,
838 aff->v->el + 1, v->d, aff->v->size - 1);
839 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
840 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
841 aff->v = isl_vec_normalize(aff->v);
842 if (!aff->v)
843 goto error;
846 isl_val_free(v);
847 return aff;
848 error:
849 isl_aff_free(aff);
850 isl_val_free(v);
851 return NULL;
854 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
856 isl_int t;
858 isl_int_init(t);
859 isl_int_set_si(t, v);
860 aff = isl_aff_add_constant(aff, t);
861 isl_int_clear(t);
863 return aff;
866 /* Add "v" to the numerator of the constant term of "aff".
868 * A NaN is unaffected by this operation.
870 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
872 if (isl_int_is_zero(v))
873 return aff;
875 if (!aff)
876 return NULL;
877 if (isl_aff_is_nan(aff))
878 return aff;
879 aff = isl_aff_cow(aff);
880 if (!aff)
881 return NULL;
883 aff->v = isl_vec_cow(aff->v);
884 if (!aff->v)
885 return isl_aff_free(aff);
887 isl_int_add(aff->v->el[1], aff->v->el[1], v);
889 return aff;
892 /* Add "v" to the numerator of the constant term of "aff".
894 * A NaN is unaffected by this operation.
896 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
898 isl_int t;
900 if (v == 0)
901 return aff;
903 isl_int_init(t);
904 isl_int_set_si(t, v);
905 aff = isl_aff_add_constant_num(aff, t);
906 isl_int_clear(t);
908 return aff;
911 /* Replace the numerator of the constant term of "aff" by "v".
913 * A NaN is unaffected by this operation.
915 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
917 if (!aff)
918 return NULL;
919 if (isl_aff_is_nan(aff))
920 return aff;
921 aff = isl_aff_cow(aff);
922 if (!aff)
923 return NULL;
925 aff->v = isl_vec_cow(aff->v);
926 if (!aff->v)
927 return isl_aff_free(aff);
929 isl_int_set_si(aff->v->el[1], v);
931 return aff;
934 /* Replace the numerator of the coefficient of the variable of type "type"
935 * at position "pos" of "aff" by "v".
937 * A NaN is unaffected by this operation.
939 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
940 enum isl_dim_type type, int pos, isl_int v)
942 if (!aff)
943 return NULL;
945 if (type == isl_dim_out)
946 isl_die(aff->v->ctx, isl_error_invalid,
947 "output/set dimension does not have a coefficient",
948 return isl_aff_free(aff));
949 if (type == isl_dim_in)
950 type = isl_dim_set;
952 if (pos >= isl_local_space_dim(aff->ls, type))
953 isl_die(aff->v->ctx, isl_error_invalid,
954 "position out of bounds", return isl_aff_free(aff));
956 if (isl_aff_is_nan(aff))
957 return aff;
958 aff = isl_aff_cow(aff);
959 if (!aff)
960 return NULL;
962 aff->v = isl_vec_cow(aff->v);
963 if (!aff->v)
964 return isl_aff_free(aff);
966 pos += isl_local_space_offset(aff->ls, type);
967 isl_int_set(aff->v->el[1 + pos], v);
969 return aff;
972 /* Replace the numerator of the coefficient of the variable of type "type"
973 * at position "pos" of "aff" by "v".
975 * A NaN is unaffected by this operation.
977 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
978 enum isl_dim_type type, int pos, int v)
980 if (!aff)
981 return NULL;
983 if (type == isl_dim_out)
984 isl_die(aff->v->ctx, isl_error_invalid,
985 "output/set dimension does not have a coefficient",
986 return isl_aff_free(aff));
987 if (type == isl_dim_in)
988 type = isl_dim_set;
990 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
991 isl_die(aff->v->ctx, isl_error_invalid,
992 "position out of bounds", return isl_aff_free(aff));
994 if (isl_aff_is_nan(aff))
995 return aff;
996 pos += isl_local_space_offset(aff->ls, type);
997 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
998 return aff;
1000 aff = isl_aff_cow(aff);
1001 if (!aff)
1002 return NULL;
1004 aff->v = isl_vec_cow(aff->v);
1005 if (!aff->v)
1006 return isl_aff_free(aff);
1008 isl_int_set_si(aff->v->el[1 + pos], v);
1010 return aff;
1013 /* Replace the coefficient of the variable of type "type" at position "pos"
1014 * of "aff" by "v".
1016 * A NaN is unaffected by this operation.
1018 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1019 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1021 if (!aff || !v)
1022 goto error;
1024 if (type == isl_dim_out)
1025 isl_die(aff->v->ctx, isl_error_invalid,
1026 "output/set dimension does not have a coefficient",
1027 goto error);
1028 if (type == isl_dim_in)
1029 type = isl_dim_set;
1031 if (pos >= isl_local_space_dim(aff->ls, type))
1032 isl_die(aff->v->ctx, isl_error_invalid,
1033 "position out of bounds", goto error);
1035 if (isl_aff_is_nan(aff)) {
1036 isl_val_free(v);
1037 return aff;
1039 if (!isl_val_is_rat(v))
1040 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1041 "expecting rational value", goto error);
1043 pos += isl_local_space_offset(aff->ls, type);
1044 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1045 isl_int_eq(aff->v->el[0], v->d)) {
1046 isl_val_free(v);
1047 return aff;
1050 aff = isl_aff_cow(aff);
1051 if (!aff)
1052 goto error;
1053 aff->v = isl_vec_cow(aff->v);
1054 if (!aff->v)
1055 goto error;
1057 if (isl_int_eq(aff->v->el[0], v->d)) {
1058 isl_int_set(aff->v->el[1 + pos], v->n);
1059 } else if (isl_int_is_one(v->d)) {
1060 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1061 } else {
1062 isl_seq_scale(aff->v->el + 1,
1063 aff->v->el + 1, v->d, aff->v->size - 1);
1064 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1065 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1066 aff->v = isl_vec_normalize(aff->v);
1067 if (!aff->v)
1068 goto error;
1071 isl_val_free(v);
1072 return aff;
1073 error:
1074 isl_aff_free(aff);
1075 isl_val_free(v);
1076 return NULL;
1079 /* Add "v" to the coefficient of the variable of type "type"
1080 * at position "pos" of "aff".
1082 * A NaN is unaffected by this operation.
1084 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1085 enum isl_dim_type type, int pos, isl_int v)
1087 if (!aff)
1088 return NULL;
1090 if (type == isl_dim_out)
1091 isl_die(aff->v->ctx, isl_error_invalid,
1092 "output/set dimension does not have a coefficient",
1093 return isl_aff_free(aff));
1094 if (type == isl_dim_in)
1095 type = isl_dim_set;
1097 if (pos >= isl_local_space_dim(aff->ls, type))
1098 isl_die(aff->v->ctx, isl_error_invalid,
1099 "position out of bounds", return isl_aff_free(aff));
1101 if (isl_aff_is_nan(aff))
1102 return aff;
1103 aff = isl_aff_cow(aff);
1104 if (!aff)
1105 return NULL;
1107 aff->v = isl_vec_cow(aff->v);
1108 if (!aff->v)
1109 return isl_aff_free(aff);
1111 pos += isl_local_space_offset(aff->ls, type);
1112 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1114 return aff;
1117 /* Add "v" to the coefficient of the variable of type "type"
1118 * at position "pos" of "aff".
1120 * A NaN is unaffected by this operation.
1122 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1123 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1125 if (!aff || !v)
1126 goto error;
1128 if (isl_val_is_zero(v)) {
1129 isl_val_free(v);
1130 return aff;
1133 if (type == isl_dim_out)
1134 isl_die(aff->v->ctx, isl_error_invalid,
1135 "output/set dimension does not have a coefficient",
1136 goto error);
1137 if (type == isl_dim_in)
1138 type = isl_dim_set;
1140 if (pos >= isl_local_space_dim(aff->ls, type))
1141 isl_die(aff->v->ctx, isl_error_invalid,
1142 "position out of bounds", goto error);
1144 if (isl_aff_is_nan(aff)) {
1145 isl_val_free(v);
1146 return aff;
1148 if (!isl_val_is_rat(v))
1149 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1150 "expecting rational value", goto error);
1152 aff = isl_aff_cow(aff);
1153 if (!aff)
1154 goto error;
1156 aff->v = isl_vec_cow(aff->v);
1157 if (!aff->v)
1158 goto error;
1160 pos += isl_local_space_offset(aff->ls, type);
1161 if (isl_int_is_one(v->d)) {
1162 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1163 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1164 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1165 aff->v = isl_vec_normalize(aff->v);
1166 if (!aff->v)
1167 goto error;
1168 } else {
1169 isl_seq_scale(aff->v->el + 1,
1170 aff->v->el + 1, v->d, aff->v->size - 1);
1171 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1172 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1173 aff->v = isl_vec_normalize(aff->v);
1174 if (!aff->v)
1175 goto error;
1178 isl_val_free(v);
1179 return aff;
1180 error:
1181 isl_aff_free(aff);
1182 isl_val_free(v);
1183 return NULL;
1186 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1187 enum isl_dim_type type, int pos, int v)
1189 isl_int t;
1191 isl_int_init(t);
1192 isl_int_set_si(t, v);
1193 aff = isl_aff_add_coefficient(aff, type, pos, t);
1194 isl_int_clear(t);
1196 return aff;
1199 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1201 if (!aff)
1202 return NULL;
1204 return isl_local_space_get_div(aff->ls, pos);
1207 /* Return the negation of "aff".
1209 * As a special case, -NaN = NaN.
1211 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1213 if (!aff)
1214 return NULL;
1215 if (isl_aff_is_nan(aff))
1216 return aff;
1217 aff = isl_aff_cow(aff);
1218 if (!aff)
1219 return NULL;
1220 aff->v = isl_vec_cow(aff->v);
1221 if (!aff->v)
1222 return isl_aff_free(aff);
1224 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1226 return aff;
1229 /* Remove divs from the local space that do not appear in the affine
1230 * expression.
1231 * We currently only remove divs at the end.
1232 * Some intermediate divs may also not appear directly in the affine
1233 * expression, but we would also need to check that no other divs are
1234 * defined in terms of them.
1236 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1238 int pos;
1239 int off;
1240 int n;
1242 if (!aff)
1243 return NULL;
1245 n = isl_local_space_dim(aff->ls, isl_dim_div);
1246 off = isl_local_space_offset(aff->ls, isl_dim_div);
1248 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1249 if (pos == n)
1250 return aff;
1252 aff = isl_aff_cow(aff);
1253 if (!aff)
1254 return NULL;
1256 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1257 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1258 if (!aff->ls || !aff->v)
1259 return isl_aff_free(aff);
1261 return aff;
1264 /* Given two affine expressions "p" of length p_len (including the
1265 * denominator and the constant term) and "subs" of length subs_len,
1266 * plug in "subs" for the variable at position "pos".
1267 * The variables of "subs" and "p" are assumed to match up to subs_len,
1268 * but "p" may have additional variables.
1269 * "v" is an initialized isl_int that can be used internally.
1271 * In particular, if "p" represents the expression
1273 * (a i + g)/m
1275 * with i the variable at position "pos" and "subs" represents the expression
1277 * f/d
1279 * then the result represents the expression
1281 * (a f + d g)/(m d)
1284 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1285 int p_len, int subs_len, isl_int v)
1287 isl_int_set(v, p[1 + pos]);
1288 isl_int_set_si(p[1 + pos], 0);
1289 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1290 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1291 isl_int_mul(p[0], p[0], subs[0]);
1294 /* Look for any divs in the aff->ls with a denominator equal to one
1295 * and plug them into the affine expression and any subsequent divs
1296 * that may reference the div.
1298 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1300 int i, n;
1301 int len;
1302 isl_int v;
1303 isl_vec *vec;
1304 isl_local_space *ls;
1305 unsigned pos;
1307 if (!aff)
1308 return NULL;
1310 n = isl_local_space_dim(aff->ls, isl_dim_div);
1311 len = aff->v->size;
1312 for (i = 0; i < n; ++i) {
1313 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1314 continue;
1315 ls = isl_local_space_copy(aff->ls);
1316 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1317 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1318 vec = isl_vec_copy(aff->v);
1319 vec = isl_vec_cow(vec);
1320 if (!ls || !vec)
1321 goto error;
1323 isl_int_init(v);
1325 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1326 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1327 len, len, v);
1329 isl_int_clear(v);
1331 isl_vec_free(aff->v);
1332 aff->v = vec;
1333 isl_local_space_free(aff->ls);
1334 aff->ls = ls;
1337 return aff;
1338 error:
1339 isl_vec_free(vec);
1340 isl_local_space_free(ls);
1341 return isl_aff_free(aff);
1344 /* Look for any divs j that appear with a unit coefficient inside
1345 * the definitions of other divs i and plug them into the definitions
1346 * of the divs i.
1348 * In particular, an expression of the form
1350 * floor((f(..) + floor(g(..)/n))/m)
1352 * is simplified to
1354 * floor((n * f(..) + g(..))/(n * m))
1356 * This simplification is correct because we can move the expression
1357 * f(..) into the inner floor in the original expression to obtain
1359 * floor(floor((n * f(..) + g(..))/n)/m)
1361 * from which we can derive the simplified expression.
1363 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1365 int i, j, n;
1366 int off;
1368 if (!aff)
1369 return NULL;
1371 n = isl_local_space_dim(aff->ls, isl_dim_div);
1372 off = isl_local_space_offset(aff->ls, isl_dim_div);
1373 for (i = 1; i < n; ++i) {
1374 for (j = 0; j < i; ++j) {
1375 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1376 continue;
1377 aff->ls = isl_local_space_substitute_seq(aff->ls,
1378 isl_dim_div, j, aff->ls->div->row[j],
1379 aff->v->size, i, 1);
1380 if (!aff->ls)
1381 return isl_aff_free(aff);
1385 return aff;
1388 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1390 * Even though this function is only called on isl_affs with a single
1391 * reference, we are careful to only change aff->v and aff->ls together.
1393 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1395 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1396 isl_local_space *ls;
1397 isl_vec *v;
1399 ls = isl_local_space_copy(aff->ls);
1400 ls = isl_local_space_swap_div(ls, a, b);
1401 v = isl_vec_copy(aff->v);
1402 v = isl_vec_cow(v);
1403 if (!ls || !v)
1404 goto error;
1406 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1407 isl_vec_free(aff->v);
1408 aff->v = v;
1409 isl_local_space_free(aff->ls);
1410 aff->ls = ls;
1412 return aff;
1413 error:
1414 isl_vec_free(v);
1415 isl_local_space_free(ls);
1416 return isl_aff_free(aff);
1419 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1421 * We currently do not actually remove div "b", but simply add its
1422 * coefficient to that of "a" and then zero it out.
1424 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1426 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1428 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1429 return aff;
1431 aff->v = isl_vec_cow(aff->v);
1432 if (!aff->v)
1433 return isl_aff_free(aff);
1435 isl_int_add(aff->v->el[1 + off + a],
1436 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1437 isl_int_set_si(aff->v->el[1 + off + b], 0);
1439 return aff;
1442 /* Sort the divs in the local space of "aff" according to
1443 * the comparison function "cmp_row" in isl_local_space.c,
1444 * combining the coefficients of identical divs.
1446 * Reordering divs does not change the semantics of "aff",
1447 * so there is no need to call isl_aff_cow.
1448 * Moreover, this function is currently only called on isl_affs
1449 * with a single reference.
1451 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1453 int i, j, n;
1454 unsigned off;
1456 if (!aff)
1457 return NULL;
1459 off = isl_local_space_offset(aff->ls, isl_dim_div);
1460 n = isl_aff_dim(aff, isl_dim_div);
1461 for (i = 1; i < n; ++i) {
1462 for (j = i - 1; j >= 0; --j) {
1463 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1464 if (cmp < 0)
1465 break;
1466 if (cmp == 0)
1467 aff = merge_divs(aff, j, j + 1);
1468 else
1469 aff = swap_div(aff, j, j + 1);
1470 if (!aff)
1471 return NULL;
1475 return aff;
1478 /* Normalize the representation of "aff".
1480 * This function should only be called of "new" isl_affs, i.e.,
1481 * with only a single reference. We therefore do not need to
1482 * worry about affecting other instances.
1484 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1486 if (!aff)
1487 return NULL;
1488 aff->v = isl_vec_normalize(aff->v);
1489 if (!aff->v)
1490 return isl_aff_free(aff);
1491 aff = plug_in_integral_divs(aff);
1492 aff = plug_in_unit_divs(aff);
1493 aff = sort_divs(aff);
1494 aff = isl_aff_remove_unused_divs(aff);
1495 return aff;
1498 /* Given f, return floor(f).
1499 * If f is an integer expression, then just return f.
1500 * If f is a constant, then return the constant floor(f).
1501 * Otherwise, if f = g/m, write g = q m + r,
1502 * create a new div d = [r/m] and return the expression q + d.
1503 * The coefficients in r are taken to lie between -m/2 and m/2.
1505 * As a special case, floor(NaN) = NaN.
1507 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1509 int i;
1510 int size;
1511 isl_ctx *ctx;
1512 isl_vec *div;
1514 if (!aff)
1515 return NULL;
1517 if (isl_aff_is_nan(aff))
1518 return aff;
1519 if (isl_int_is_one(aff->v->el[0]))
1520 return aff;
1522 aff = isl_aff_cow(aff);
1523 if (!aff)
1524 return NULL;
1526 aff->v = isl_vec_cow(aff->v);
1527 if (!aff->v)
1528 return isl_aff_free(aff);
1530 if (isl_aff_is_cst(aff)) {
1531 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1532 isl_int_set_si(aff->v->el[0], 1);
1533 return aff;
1536 div = isl_vec_copy(aff->v);
1537 div = isl_vec_cow(div);
1538 if (!div)
1539 return isl_aff_free(aff);
1541 ctx = isl_aff_get_ctx(aff);
1542 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1543 for (i = 1; i < aff->v->size; ++i) {
1544 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1545 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1546 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1547 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1548 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1552 aff->ls = isl_local_space_add_div(aff->ls, div);
1553 if (!aff->ls)
1554 return isl_aff_free(aff);
1556 size = aff->v->size;
1557 aff->v = isl_vec_extend(aff->v, size + 1);
1558 if (!aff->v)
1559 return isl_aff_free(aff);
1560 isl_int_set_si(aff->v->el[0], 1);
1561 isl_int_set_si(aff->v->el[size], 1);
1563 aff = isl_aff_normalize(aff);
1565 return aff;
1568 /* Compute
1570 * aff mod m = aff - m * floor(aff/m)
1572 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1574 isl_aff *res;
1576 res = isl_aff_copy(aff);
1577 aff = isl_aff_scale_down(aff, m);
1578 aff = isl_aff_floor(aff);
1579 aff = isl_aff_scale(aff, m);
1580 res = isl_aff_sub(res, aff);
1582 return res;
1585 /* Compute
1587 * aff mod m = aff - m * floor(aff/m)
1589 * with m an integer value.
1591 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1592 __isl_take isl_val *m)
1594 isl_aff *res;
1596 if (!aff || !m)
1597 goto error;
1599 if (!isl_val_is_int(m))
1600 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1601 "expecting integer modulo", goto error);
1603 res = isl_aff_copy(aff);
1604 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1605 aff = isl_aff_floor(aff);
1606 aff = isl_aff_scale_val(aff, m);
1607 res = isl_aff_sub(res, aff);
1609 return res;
1610 error:
1611 isl_aff_free(aff);
1612 isl_val_free(m);
1613 return NULL;
1616 /* Compute
1618 * pwaff mod m = pwaff - m * floor(pwaff/m)
1620 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1622 isl_pw_aff *res;
1624 res = isl_pw_aff_copy(pwaff);
1625 pwaff = isl_pw_aff_scale_down(pwaff, m);
1626 pwaff = isl_pw_aff_floor(pwaff);
1627 pwaff = isl_pw_aff_scale(pwaff, m);
1628 res = isl_pw_aff_sub(res, pwaff);
1630 return res;
1633 /* Compute
1635 * pa mod m = pa - m * floor(pa/m)
1637 * with m an integer value.
1639 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1640 __isl_take isl_val *m)
1642 if (!pa || !m)
1643 goto error;
1644 if (!isl_val_is_int(m))
1645 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1646 "expecting integer modulo", goto error);
1647 pa = isl_pw_aff_mod(pa, m->n);
1648 isl_val_free(m);
1649 return pa;
1650 error:
1651 isl_pw_aff_free(pa);
1652 isl_val_free(m);
1653 return NULL;
1656 /* Given f, return ceil(f).
1657 * If f is an integer expression, then just return f.
1658 * Otherwise, let f be the expression
1660 * e/m
1662 * then return
1664 * floor((e + m - 1)/m)
1666 * As a special case, ceil(NaN) = NaN.
1668 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1670 if (!aff)
1671 return NULL;
1673 if (isl_aff_is_nan(aff))
1674 return aff;
1675 if (isl_int_is_one(aff->v->el[0]))
1676 return aff;
1678 aff = isl_aff_cow(aff);
1679 if (!aff)
1680 return NULL;
1681 aff->v = isl_vec_cow(aff->v);
1682 if (!aff->v)
1683 return isl_aff_free(aff);
1685 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1686 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1687 aff = isl_aff_floor(aff);
1689 return aff;
1692 /* Apply the expansion computed by isl_merge_divs.
1693 * The expansion itself is given by "exp" while the resulting
1694 * list of divs is given by "div".
1696 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1697 __isl_take isl_mat *div, int *exp)
1699 int i, j;
1700 int old_n_div;
1701 int new_n_div;
1702 int offset;
1704 aff = isl_aff_cow(aff);
1705 if (!aff || !div)
1706 goto error;
1708 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1709 new_n_div = isl_mat_rows(div);
1710 if (new_n_div < old_n_div)
1711 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1712 "not an expansion", goto error);
1714 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1715 if (!aff->v)
1716 goto error;
1718 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1719 j = old_n_div - 1;
1720 for (i = new_n_div - 1; i >= 0; --i) {
1721 if (j >= 0 && exp[j] == i) {
1722 if (i != j)
1723 isl_int_swap(aff->v->el[offset + i],
1724 aff->v->el[offset + j]);
1725 j--;
1726 } else
1727 isl_int_set_si(aff->v->el[offset + i], 0);
1730 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1731 if (!aff->ls)
1732 goto error;
1733 isl_mat_free(div);
1734 return aff;
1735 error:
1736 isl_aff_free(aff);
1737 isl_mat_free(div);
1738 return NULL;
1741 /* Add two affine expressions that live in the same local space.
1743 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1744 __isl_take isl_aff *aff2)
1746 isl_int gcd, f;
1748 aff1 = isl_aff_cow(aff1);
1749 if (!aff1 || !aff2)
1750 goto error;
1752 aff1->v = isl_vec_cow(aff1->v);
1753 if (!aff1->v)
1754 goto error;
1756 isl_int_init(gcd);
1757 isl_int_init(f);
1758 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1759 isl_int_divexact(f, aff2->v->el[0], gcd);
1760 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1761 isl_int_divexact(f, aff1->v->el[0], gcd);
1762 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1763 isl_int_divexact(f, aff2->v->el[0], gcd);
1764 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1765 isl_int_clear(f);
1766 isl_int_clear(gcd);
1768 isl_aff_free(aff2);
1769 return aff1;
1770 error:
1771 isl_aff_free(aff1);
1772 isl_aff_free(aff2);
1773 return NULL;
1776 /* Return the sum of "aff1" and "aff2".
1778 * If either of the two is NaN, then the result is NaN.
1780 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1781 __isl_take isl_aff *aff2)
1783 isl_ctx *ctx;
1784 int *exp1 = NULL;
1785 int *exp2 = NULL;
1786 isl_mat *div;
1787 int n_div1, n_div2;
1789 if (!aff1 || !aff2)
1790 goto error;
1792 ctx = isl_aff_get_ctx(aff1);
1793 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1794 isl_die(ctx, isl_error_invalid,
1795 "spaces don't match", goto error);
1797 if (isl_aff_is_nan(aff1)) {
1798 isl_aff_free(aff2);
1799 return aff1;
1801 if (isl_aff_is_nan(aff2)) {
1802 isl_aff_free(aff1);
1803 return aff2;
1806 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1807 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1808 if (n_div1 == 0 && n_div2 == 0)
1809 return add_expanded(aff1, aff2);
1811 exp1 = isl_alloc_array(ctx, int, n_div1);
1812 exp2 = isl_alloc_array(ctx, int, n_div2);
1813 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1814 goto error;
1816 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1817 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1818 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1819 free(exp1);
1820 free(exp2);
1822 return add_expanded(aff1, aff2);
1823 error:
1824 free(exp1);
1825 free(exp2);
1826 isl_aff_free(aff1);
1827 isl_aff_free(aff2);
1828 return NULL;
1831 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1832 __isl_take isl_aff *aff2)
1834 return isl_aff_add(aff1, isl_aff_neg(aff2));
1837 /* Return the result of scaling "aff" by a factor of "f".
1839 * As a special case, f * NaN = NaN.
1841 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1843 isl_int gcd;
1845 if (!aff)
1846 return NULL;
1847 if (isl_aff_is_nan(aff))
1848 return aff;
1850 if (isl_int_is_one(f))
1851 return aff;
1853 aff = isl_aff_cow(aff);
1854 if (!aff)
1855 return NULL;
1856 aff->v = isl_vec_cow(aff->v);
1857 if (!aff->v)
1858 return isl_aff_free(aff);
1860 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1861 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1862 return aff;
1865 isl_int_init(gcd);
1866 isl_int_gcd(gcd, aff->v->el[0], f);
1867 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1868 isl_int_divexact(gcd, f, gcd);
1869 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1870 isl_int_clear(gcd);
1872 return aff;
1875 /* Multiple "aff" by "v".
1877 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1878 __isl_take isl_val *v)
1880 if (!aff || !v)
1881 goto error;
1883 if (isl_val_is_one(v)) {
1884 isl_val_free(v);
1885 return aff;
1888 if (!isl_val_is_rat(v))
1889 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1890 "expecting rational factor", goto error);
1892 aff = isl_aff_scale(aff, v->n);
1893 aff = isl_aff_scale_down(aff, v->d);
1895 isl_val_free(v);
1896 return aff;
1897 error:
1898 isl_aff_free(aff);
1899 isl_val_free(v);
1900 return NULL;
1903 /* Return the result of scaling "aff" down by a factor of "f".
1905 * As a special case, NaN/f = NaN.
1907 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1909 isl_int gcd;
1911 if (!aff)
1912 return NULL;
1913 if (isl_aff_is_nan(aff))
1914 return aff;
1916 if (isl_int_is_one(f))
1917 return aff;
1919 aff = isl_aff_cow(aff);
1920 if (!aff)
1921 return NULL;
1923 if (isl_int_is_zero(f))
1924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1925 "cannot scale down by zero", return isl_aff_free(aff));
1927 aff->v = isl_vec_cow(aff->v);
1928 if (!aff->v)
1929 return isl_aff_free(aff);
1931 isl_int_init(gcd);
1932 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1933 isl_int_gcd(gcd, gcd, f);
1934 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1935 isl_int_divexact(gcd, f, gcd);
1936 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1937 isl_int_clear(gcd);
1939 return aff;
1942 /* Divide "aff" by "v".
1944 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1945 __isl_take isl_val *v)
1947 if (!aff || !v)
1948 goto error;
1950 if (isl_val_is_one(v)) {
1951 isl_val_free(v);
1952 return aff;
1955 if (!isl_val_is_rat(v))
1956 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1957 "expecting rational factor", goto error);
1958 if (!isl_val_is_pos(v))
1959 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1960 "factor needs to be positive", goto error);
1962 aff = isl_aff_scale(aff, v->d);
1963 aff = isl_aff_scale_down(aff, v->n);
1965 isl_val_free(v);
1966 return aff;
1967 error:
1968 isl_aff_free(aff);
1969 isl_val_free(v);
1970 return NULL;
1973 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1975 isl_int v;
1977 if (f == 1)
1978 return aff;
1980 isl_int_init(v);
1981 isl_int_set_ui(v, f);
1982 aff = isl_aff_scale_down(aff, v);
1983 isl_int_clear(v);
1985 return aff;
1988 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1989 enum isl_dim_type type, unsigned pos, const char *s)
1991 aff = isl_aff_cow(aff);
1992 if (!aff)
1993 return NULL;
1994 if (type == isl_dim_out)
1995 isl_die(aff->v->ctx, isl_error_invalid,
1996 "cannot set name of output/set dimension",
1997 return isl_aff_free(aff));
1998 if (type == isl_dim_in)
1999 type = isl_dim_set;
2000 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2001 if (!aff->ls)
2002 return isl_aff_free(aff);
2004 return aff;
2007 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2008 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2010 aff = isl_aff_cow(aff);
2011 if (!aff)
2012 goto error;
2013 if (type == isl_dim_out)
2014 isl_die(aff->v->ctx, isl_error_invalid,
2015 "cannot set name of output/set dimension",
2016 goto error);
2017 if (type == isl_dim_in)
2018 type = isl_dim_set;
2019 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2020 if (!aff->ls)
2021 return isl_aff_free(aff);
2023 return aff;
2024 error:
2025 isl_id_free(id);
2026 isl_aff_free(aff);
2027 return NULL;
2030 /* Replace the identifier of the input tuple of "aff" by "id".
2031 * type is currently required to be equal to isl_dim_in
2033 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2034 enum isl_dim_type type, __isl_take isl_id *id)
2036 aff = isl_aff_cow(aff);
2037 if (!aff)
2038 goto error;
2039 if (type != isl_dim_out)
2040 isl_die(aff->v->ctx, isl_error_invalid,
2041 "cannot only set id of input tuple", goto error);
2042 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2043 if (!aff->ls)
2044 return isl_aff_free(aff);
2046 return aff;
2047 error:
2048 isl_id_free(id);
2049 isl_aff_free(aff);
2050 return NULL;
2053 /* Exploit the equalities in "eq" to simplify the affine expression
2054 * and the expressions of the integer divisions in the local space.
2055 * The integer divisions in this local space are assumed to appear
2056 * as regular dimensions in "eq".
2058 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2059 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2061 int i, j;
2062 unsigned total;
2063 unsigned n_div;
2065 if (!eq)
2066 goto error;
2067 if (eq->n_eq == 0) {
2068 isl_basic_set_free(eq);
2069 return aff;
2072 aff = isl_aff_cow(aff);
2073 if (!aff)
2074 goto error;
2076 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2077 isl_basic_set_copy(eq));
2078 aff->v = isl_vec_cow(aff->v);
2079 if (!aff->ls || !aff->v)
2080 goto error;
2082 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2083 n_div = eq->n_div;
2084 for (i = 0; i < eq->n_eq; ++i) {
2085 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2086 if (j < 0 || j == 0 || j >= total)
2087 continue;
2089 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2090 &aff->v->el[0]);
2093 isl_basic_set_free(eq);
2094 aff = isl_aff_normalize(aff);
2095 return aff;
2096 error:
2097 isl_basic_set_free(eq);
2098 isl_aff_free(aff);
2099 return NULL;
2102 /* Exploit the equalities in "eq" to simplify the affine expression
2103 * and the expressions of the integer divisions in the local space.
2105 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2106 __isl_take isl_basic_set *eq)
2108 int n_div;
2110 if (!aff || !eq)
2111 goto error;
2112 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2113 if (n_div > 0)
2114 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2115 return isl_aff_substitute_equalities_lifted(aff, eq);
2116 error:
2117 isl_basic_set_free(eq);
2118 isl_aff_free(aff);
2119 return NULL;
2122 /* Look for equalities among the variables shared by context and aff
2123 * and the integer divisions of aff, if any.
2124 * The equalities are then used to eliminate coefficients and/or integer
2125 * divisions from aff.
2127 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2128 __isl_take isl_set *context)
2130 isl_basic_set *hull;
2131 int n_div;
2133 if (!aff)
2134 goto error;
2135 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2136 if (n_div > 0) {
2137 isl_basic_set *bset;
2138 isl_local_space *ls;
2139 context = isl_set_add_dims(context, isl_dim_set, n_div);
2140 ls = isl_aff_get_domain_local_space(aff);
2141 bset = isl_basic_set_from_local_space(ls);
2142 bset = isl_basic_set_lift(bset);
2143 bset = isl_basic_set_flatten(bset);
2144 context = isl_set_intersect(context,
2145 isl_set_from_basic_set(bset));
2148 hull = isl_set_affine_hull(context);
2149 return isl_aff_substitute_equalities_lifted(aff, hull);
2150 error:
2151 isl_aff_free(aff);
2152 isl_set_free(context);
2153 return NULL;
2156 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2157 __isl_take isl_set *context)
2159 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2160 dom_context = isl_set_intersect_params(dom_context, context);
2161 return isl_aff_gist(aff, dom_context);
2164 /* Return a basic set containing those elements in the space
2165 * of aff where it is non-negative.
2166 * If "rational" is set, then return a rational basic set.
2168 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2171 __isl_take isl_aff *aff, int rational)
2173 isl_constraint *ineq;
2174 isl_basic_set *bset;
2176 if (!aff)
2177 return NULL;
2178 if (isl_aff_is_nan(aff)) {
2179 isl_space *space = isl_aff_get_domain_space(aff);
2180 isl_aff_free(aff);
2181 return isl_basic_set_empty(space);
2184 ineq = isl_inequality_from_aff(aff);
2186 bset = isl_basic_set_from_constraint(ineq);
2187 if (rational)
2188 bset = isl_basic_set_set_rational(bset);
2189 bset = isl_basic_set_simplify(bset);
2190 return bset;
2193 /* Return a basic set containing those elements in the space
2194 * of aff where it is non-negative.
2196 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2198 return aff_nonneg_basic_set(aff, 0);
2201 /* Return a basic set containing those elements in the domain space
2202 * of aff where it is negative.
2204 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2206 aff = isl_aff_neg(aff);
2207 aff = isl_aff_add_constant_num_si(aff, -1);
2208 return isl_aff_nonneg_basic_set(aff);
2211 /* Return a basic set containing those elements in the space
2212 * of aff where it is zero.
2213 * If "rational" is set, then return a rational basic set.
2215 * If "aff" is NaN, then it is not zero.
2217 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2218 int rational)
2220 isl_constraint *ineq;
2221 isl_basic_set *bset;
2223 if (!aff)
2224 return NULL;
2225 if (isl_aff_is_nan(aff)) {
2226 isl_space *space = isl_aff_get_domain_space(aff);
2227 isl_aff_free(aff);
2228 return isl_basic_set_empty(space);
2231 ineq = isl_equality_from_aff(aff);
2233 bset = isl_basic_set_from_constraint(ineq);
2234 if (rational)
2235 bset = isl_basic_set_set_rational(bset);
2236 bset = isl_basic_set_simplify(bset);
2237 return bset;
2240 /* Return a basic set containing those elements in the space
2241 * of aff where it is zero.
2243 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2245 return aff_zero_basic_set(aff, 0);
2248 /* Return a basic set containing those elements in the shared space
2249 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2251 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2252 __isl_take isl_aff *aff2)
2254 aff1 = isl_aff_sub(aff1, aff2);
2256 return isl_aff_nonneg_basic_set(aff1);
2259 /* Return a basic set containing those elements in the shared space
2260 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2262 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2263 __isl_take isl_aff *aff2)
2265 return isl_aff_ge_basic_set(aff2, aff1);
2268 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2269 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2271 aff1 = isl_aff_add(aff1, aff2);
2272 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2273 return aff1;
2276 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2278 if (!aff)
2279 return -1;
2281 return 0;
2284 /* Check whether the given affine expression has non-zero coefficient
2285 * for any dimension in the given range or if any of these dimensions
2286 * appear with non-zero coefficients in any of the integer divisions
2287 * involved in the affine expression.
2289 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2290 enum isl_dim_type type, unsigned first, unsigned n)
2292 int i;
2293 isl_ctx *ctx;
2294 int *active = NULL;
2295 int involves = 0;
2297 if (!aff)
2298 return -1;
2299 if (n == 0)
2300 return 0;
2302 ctx = isl_aff_get_ctx(aff);
2303 if (first + n > isl_aff_dim(aff, type))
2304 isl_die(ctx, isl_error_invalid,
2305 "range out of bounds", return -1);
2307 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2308 if (!active)
2309 goto error;
2311 first += isl_local_space_offset(aff->ls, type) - 1;
2312 for (i = 0; i < n; ++i)
2313 if (active[first + i]) {
2314 involves = 1;
2315 break;
2318 free(active);
2320 return involves;
2321 error:
2322 free(active);
2323 return -1;
2326 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2327 enum isl_dim_type type, unsigned first, unsigned n)
2329 isl_ctx *ctx;
2331 if (!aff)
2332 return NULL;
2333 if (type == isl_dim_out)
2334 isl_die(aff->v->ctx, isl_error_invalid,
2335 "cannot drop output/set dimension",
2336 return isl_aff_free(aff));
2337 if (type == isl_dim_in)
2338 type = isl_dim_set;
2339 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2340 return aff;
2342 ctx = isl_aff_get_ctx(aff);
2343 if (first + n > isl_local_space_dim(aff->ls, type))
2344 isl_die(ctx, isl_error_invalid, "range out of bounds",
2345 return isl_aff_free(aff));
2347 aff = isl_aff_cow(aff);
2348 if (!aff)
2349 return NULL;
2351 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2352 if (!aff->ls)
2353 return isl_aff_free(aff);
2355 first += 1 + isl_local_space_offset(aff->ls, type);
2356 aff->v = isl_vec_drop_els(aff->v, first, n);
2357 if (!aff->v)
2358 return isl_aff_free(aff);
2360 return aff;
2363 /* Project the domain of the affine expression onto its parameter space.
2364 * The affine expression may not involve any of the domain dimensions.
2366 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2368 isl_space *space;
2369 unsigned n;
2370 int involves;
2372 n = isl_aff_dim(aff, isl_dim_in);
2373 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2374 if (involves < 0)
2375 return isl_aff_free(aff);
2376 if (involves)
2377 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2378 "affine expression involves some of the domain dimensions",
2379 return isl_aff_free(aff));
2380 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2381 space = isl_aff_get_domain_space(aff);
2382 space = isl_space_params(space);
2383 aff = isl_aff_reset_domain_space(aff, space);
2384 return aff;
2387 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2388 enum isl_dim_type type, unsigned first, unsigned n)
2390 isl_ctx *ctx;
2392 if (!aff)
2393 return NULL;
2394 if (type == isl_dim_out)
2395 isl_die(aff->v->ctx, isl_error_invalid,
2396 "cannot insert output/set dimensions",
2397 return isl_aff_free(aff));
2398 if (type == isl_dim_in)
2399 type = isl_dim_set;
2400 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2401 return aff;
2403 ctx = isl_aff_get_ctx(aff);
2404 if (first > isl_local_space_dim(aff->ls, type))
2405 isl_die(ctx, isl_error_invalid, "position out of bounds",
2406 return isl_aff_free(aff));
2408 aff = isl_aff_cow(aff);
2409 if (!aff)
2410 return NULL;
2412 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2413 if (!aff->ls)
2414 return isl_aff_free(aff);
2416 first += 1 + isl_local_space_offset(aff->ls, type);
2417 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2418 if (!aff->v)
2419 return isl_aff_free(aff);
2421 return aff;
2424 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2425 enum isl_dim_type type, unsigned n)
2427 unsigned pos;
2429 pos = isl_aff_dim(aff, type);
2431 return isl_aff_insert_dims(aff, type, pos, n);
2434 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2435 enum isl_dim_type type, unsigned n)
2437 unsigned pos;
2439 pos = isl_pw_aff_dim(pwaff, type);
2441 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2444 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2445 * to dimensions of "dst_type" at "dst_pos".
2447 * We only support moving input dimensions to parameters and vice versa.
2449 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2450 enum isl_dim_type dst_type, unsigned dst_pos,
2451 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2453 unsigned g_dst_pos;
2454 unsigned g_src_pos;
2456 if (!aff)
2457 return NULL;
2458 if (n == 0 &&
2459 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2460 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2461 return aff;
2463 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2464 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2465 "cannot move output/set dimension", isl_aff_free(aff));
2466 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2467 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2468 "cannot move divs", isl_aff_free(aff));
2469 if (dst_type == isl_dim_in)
2470 dst_type = isl_dim_set;
2471 if (src_type == isl_dim_in)
2472 src_type = isl_dim_set;
2474 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2475 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2476 "range out of bounds", isl_aff_free(aff));
2477 if (dst_type == src_type)
2478 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2479 "moving dims within the same type not supported",
2480 isl_aff_free(aff));
2482 aff = isl_aff_cow(aff);
2483 if (!aff)
2484 return NULL;
2486 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2487 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2488 if (dst_type > src_type)
2489 g_dst_pos -= n;
2491 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2492 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2493 src_type, src_pos, n);
2494 if (!aff->v || !aff->ls)
2495 return isl_aff_free(aff);
2497 aff = sort_divs(aff);
2499 return aff;
2502 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2504 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2505 return isl_pw_aff_alloc(dom, aff);
2508 #undef PW
2509 #define PW isl_pw_aff
2510 #undef EL
2511 #define EL isl_aff
2512 #undef EL_IS_ZERO
2513 #define EL_IS_ZERO is_empty
2514 #undef ZERO
2515 #define ZERO empty
2516 #undef IS_ZERO
2517 #define IS_ZERO is_empty
2518 #undef FIELD
2519 #define FIELD aff
2520 #undef DEFAULT_IS_ZERO
2521 #define DEFAULT_IS_ZERO 0
2523 #define NO_EVAL
2524 #define NO_OPT
2525 #define NO_LIFT
2526 #define NO_MORPH
2528 #include <isl_pw_templ.c>
2530 static __isl_give isl_set *align_params_pw_pw_set_and(
2531 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2532 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2533 __isl_take isl_pw_aff *pwaff2))
2535 if (!pwaff1 || !pwaff2)
2536 goto error;
2537 if (isl_space_match(pwaff1->dim, isl_dim_param,
2538 pwaff2->dim, isl_dim_param))
2539 return fn(pwaff1, pwaff2);
2540 if (!isl_space_has_named_params(pwaff1->dim) ||
2541 !isl_space_has_named_params(pwaff2->dim))
2542 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2543 "unaligned unnamed parameters", goto error);
2544 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2545 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2546 return fn(pwaff1, pwaff2);
2547 error:
2548 isl_pw_aff_free(pwaff1);
2549 isl_pw_aff_free(pwaff2);
2550 return NULL;
2553 /* Compute a piecewise quasi-affine expression with a domain that
2554 * is the union of those of pwaff1 and pwaff2 and such that on each
2555 * cell, the quasi-affine expression is the better (according to cmp)
2556 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2557 * is defined on a given cell, then the associated expression
2558 * is the defined one.
2560 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2561 __isl_take isl_pw_aff *pwaff2,
2562 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2563 __isl_take isl_aff *aff2))
2565 int i, j, n;
2566 isl_pw_aff *res;
2567 isl_ctx *ctx;
2568 isl_set *set;
2570 if (!pwaff1 || !pwaff2)
2571 goto error;
2573 ctx = isl_space_get_ctx(pwaff1->dim);
2574 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2575 isl_die(ctx, isl_error_invalid,
2576 "arguments should live in same space", goto error);
2578 if (isl_pw_aff_is_empty(pwaff1)) {
2579 isl_pw_aff_free(pwaff1);
2580 return pwaff2;
2583 if (isl_pw_aff_is_empty(pwaff2)) {
2584 isl_pw_aff_free(pwaff2);
2585 return pwaff1;
2588 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2589 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2591 for (i = 0; i < pwaff1->n; ++i) {
2592 set = isl_set_copy(pwaff1->p[i].set);
2593 for (j = 0; j < pwaff2->n; ++j) {
2594 struct isl_set *common;
2595 isl_set *better;
2597 common = isl_set_intersect(
2598 isl_set_copy(pwaff1->p[i].set),
2599 isl_set_copy(pwaff2->p[j].set));
2600 better = isl_set_from_basic_set(cmp(
2601 isl_aff_copy(pwaff2->p[j].aff),
2602 isl_aff_copy(pwaff1->p[i].aff)));
2603 better = isl_set_intersect(common, better);
2604 if (isl_set_plain_is_empty(better)) {
2605 isl_set_free(better);
2606 continue;
2608 set = isl_set_subtract(set, isl_set_copy(better));
2610 res = isl_pw_aff_add_piece(res, better,
2611 isl_aff_copy(pwaff2->p[j].aff));
2613 res = isl_pw_aff_add_piece(res, set,
2614 isl_aff_copy(pwaff1->p[i].aff));
2617 for (j = 0; j < pwaff2->n; ++j) {
2618 set = isl_set_copy(pwaff2->p[j].set);
2619 for (i = 0; i < pwaff1->n; ++i)
2620 set = isl_set_subtract(set,
2621 isl_set_copy(pwaff1->p[i].set));
2622 res = isl_pw_aff_add_piece(res, set,
2623 isl_aff_copy(pwaff2->p[j].aff));
2626 isl_pw_aff_free(pwaff1);
2627 isl_pw_aff_free(pwaff2);
2629 return res;
2630 error:
2631 isl_pw_aff_free(pwaff1);
2632 isl_pw_aff_free(pwaff2);
2633 return NULL;
2636 /* Compute a piecewise quasi-affine expression with a domain that
2637 * is the union of those of pwaff1 and pwaff2 and such that on each
2638 * cell, the quasi-affine expression is the maximum of those of pwaff1
2639 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2640 * cell, then the associated expression is the defined one.
2642 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2643 __isl_take isl_pw_aff *pwaff2)
2645 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2648 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2649 __isl_take isl_pw_aff *pwaff2)
2651 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2652 &pw_aff_union_max);
2655 /* Compute a piecewise quasi-affine expression with a domain that
2656 * is the union of those of pwaff1 and pwaff2 and such that on each
2657 * cell, the quasi-affine expression is the minimum of those of pwaff1
2658 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2659 * cell, then the associated expression is the defined one.
2661 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2662 __isl_take isl_pw_aff *pwaff2)
2664 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2667 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2668 __isl_take isl_pw_aff *pwaff2)
2670 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2671 &pw_aff_union_min);
2674 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2675 __isl_take isl_pw_aff *pwaff2, int max)
2677 if (max)
2678 return isl_pw_aff_union_max(pwaff1, pwaff2);
2679 else
2680 return isl_pw_aff_union_min(pwaff1, pwaff2);
2683 /* Construct a map with as domain the domain of pwaff and
2684 * one-dimensional range corresponding to the affine expressions.
2686 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2688 int i;
2689 isl_space *dim;
2690 isl_map *map;
2692 if (!pwaff)
2693 return NULL;
2695 dim = isl_pw_aff_get_space(pwaff);
2696 map = isl_map_empty(dim);
2698 for (i = 0; i < pwaff->n; ++i) {
2699 isl_basic_map *bmap;
2700 isl_map *map_i;
2702 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2703 map_i = isl_map_from_basic_map(bmap);
2704 map_i = isl_map_intersect_domain(map_i,
2705 isl_set_copy(pwaff->p[i].set));
2706 map = isl_map_union_disjoint(map, map_i);
2709 isl_pw_aff_free(pwaff);
2711 return map;
2714 /* Construct a map with as domain the domain of pwaff and
2715 * one-dimensional range corresponding to the affine expressions.
2717 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2719 if (!pwaff)
2720 return NULL;
2721 if (isl_space_is_set(pwaff->dim))
2722 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2723 "space of input is not a map", goto error);
2724 return map_from_pw_aff(pwaff);
2725 error:
2726 isl_pw_aff_free(pwaff);
2727 return NULL;
2730 /* Construct a one-dimensional set with as parameter domain
2731 * the domain of pwaff and the single set dimension
2732 * corresponding to the affine expressions.
2734 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2736 if (!pwaff)
2737 return NULL;
2738 if (!isl_space_is_set(pwaff->dim))
2739 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2740 "space of input is not a set", goto error);
2741 return map_from_pw_aff(pwaff);
2742 error:
2743 isl_pw_aff_free(pwaff);
2744 return NULL;
2747 /* Return a set containing those elements in the domain
2748 * of pwaff where it is non-negative.
2750 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2752 int i;
2753 isl_set *set;
2755 if (!pwaff)
2756 return NULL;
2758 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2760 for (i = 0; i < pwaff->n; ++i) {
2761 isl_basic_set *bset;
2762 isl_set *set_i;
2763 int rational;
2765 rational = isl_set_has_rational(pwaff->p[i].set);
2766 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2767 rational);
2768 set_i = isl_set_from_basic_set(bset);
2769 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2770 set = isl_set_union_disjoint(set, set_i);
2773 isl_pw_aff_free(pwaff);
2775 return set;
2778 /* Return a set containing those elements in the domain
2779 * of pwaff where it is zero (if complement is 0) or not zero
2780 * (if complement is 1).
2782 * The pieces with a NaN never belong to the result since
2783 * NaN is neither zero nor non-zero.
2785 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2786 int complement)
2788 int i;
2789 isl_set *set;
2791 if (!pwaff)
2792 return NULL;
2794 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2796 for (i = 0; i < pwaff->n; ++i) {
2797 isl_basic_set *bset;
2798 isl_set *set_i, *zero;
2799 int rational;
2801 if (isl_aff_is_nan(pwaff->p[i].aff))
2802 continue;
2804 rational = isl_set_has_rational(pwaff->p[i].set);
2805 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2806 rational);
2807 zero = isl_set_from_basic_set(bset);
2808 set_i = isl_set_copy(pwaff->p[i].set);
2809 if (complement)
2810 set_i = isl_set_subtract(set_i, zero);
2811 else
2812 set_i = isl_set_intersect(set_i, zero);
2813 set = isl_set_union_disjoint(set, set_i);
2816 isl_pw_aff_free(pwaff);
2818 return set;
2821 /* Return a set containing those elements in the domain
2822 * of pwaff where it is zero.
2824 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2826 return pw_aff_zero_set(pwaff, 0);
2829 /* Return a set containing those elements in the domain
2830 * of pwaff where it is not zero.
2832 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2834 return pw_aff_zero_set(pwaff, 1);
2837 /* Return a set containing those elements in the shared domain
2838 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2840 * We compute the difference on the shared domain and then construct
2841 * the set of values where this difference is non-negative.
2842 * If strict is set, we first subtract 1 from the difference.
2843 * If equal is set, we only return the elements where pwaff1 and pwaff2
2844 * are equal.
2846 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2847 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2849 isl_set *set1, *set2;
2851 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2852 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2853 set1 = isl_set_intersect(set1, set2);
2854 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2855 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2856 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2858 if (strict) {
2859 isl_space *dim = isl_set_get_space(set1);
2860 isl_aff *aff;
2861 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2862 aff = isl_aff_add_constant_si(aff, -1);
2863 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2864 } else
2865 isl_set_free(set1);
2867 if (equal)
2868 return isl_pw_aff_zero_set(pwaff1);
2869 return isl_pw_aff_nonneg_set(pwaff1);
2872 /* Return a set containing those elements in the shared domain
2873 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2875 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2876 __isl_take isl_pw_aff *pwaff2)
2878 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2881 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2882 __isl_take isl_pw_aff *pwaff2)
2884 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2887 /* Return a set containing those elements in the shared domain
2888 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2890 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2891 __isl_take isl_pw_aff *pwaff2)
2893 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2896 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2897 __isl_take isl_pw_aff *pwaff2)
2899 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2902 /* Return a set containing those elements in the shared domain
2903 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2905 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2906 __isl_take isl_pw_aff *pwaff2)
2908 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2911 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2912 __isl_take isl_pw_aff *pwaff2)
2914 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2917 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2918 __isl_take isl_pw_aff *pwaff2)
2920 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2923 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2924 __isl_take isl_pw_aff *pwaff2)
2926 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2929 /* Return a set containing those elements in the shared domain
2930 * of the elements of list1 and list2 where each element in list1
2931 * has the relation specified by "fn" with each element in list2.
2933 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2934 __isl_take isl_pw_aff_list *list2,
2935 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2936 __isl_take isl_pw_aff *pwaff2))
2938 int i, j;
2939 isl_ctx *ctx;
2940 isl_set *set;
2942 if (!list1 || !list2)
2943 goto error;
2945 ctx = isl_pw_aff_list_get_ctx(list1);
2946 if (list1->n < 1 || list2->n < 1)
2947 isl_die(ctx, isl_error_invalid,
2948 "list should contain at least one element", goto error);
2950 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2951 for (i = 0; i < list1->n; ++i)
2952 for (j = 0; j < list2->n; ++j) {
2953 isl_set *set_ij;
2955 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2956 isl_pw_aff_copy(list2->p[j]));
2957 set = isl_set_intersect(set, set_ij);
2960 isl_pw_aff_list_free(list1);
2961 isl_pw_aff_list_free(list2);
2962 return set;
2963 error:
2964 isl_pw_aff_list_free(list1);
2965 isl_pw_aff_list_free(list2);
2966 return NULL;
2969 /* Return a set containing those elements in the shared domain
2970 * of the elements of list1 and list2 where each element in list1
2971 * is equal to each element in list2.
2973 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2974 __isl_take isl_pw_aff_list *list2)
2976 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2979 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2980 __isl_take isl_pw_aff_list *list2)
2982 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2985 /* Return a set containing those elements in the shared domain
2986 * of the elements of list1 and list2 where each element in list1
2987 * is less than or equal to each element in list2.
2989 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2990 __isl_take isl_pw_aff_list *list2)
2992 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2995 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2996 __isl_take isl_pw_aff_list *list2)
2998 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3001 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3002 __isl_take isl_pw_aff_list *list2)
3004 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3007 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3008 __isl_take isl_pw_aff_list *list2)
3010 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3014 /* Return a set containing those elements in the shared domain
3015 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3017 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3018 __isl_take isl_pw_aff *pwaff2)
3020 isl_set *set_lt, *set_gt;
3022 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3023 isl_pw_aff_copy(pwaff2));
3024 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3025 return isl_set_union_disjoint(set_lt, set_gt);
3028 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3029 __isl_take isl_pw_aff *pwaff2)
3031 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3034 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3035 isl_int v)
3037 int i;
3039 if (isl_int_is_one(v))
3040 return pwaff;
3041 if (!isl_int_is_pos(v))
3042 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3043 "factor needs to be positive",
3044 return isl_pw_aff_free(pwaff));
3045 pwaff = isl_pw_aff_cow(pwaff);
3046 if (!pwaff)
3047 return NULL;
3048 if (pwaff->n == 0)
3049 return pwaff;
3051 for (i = 0; i < pwaff->n; ++i) {
3052 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3053 if (!pwaff->p[i].aff)
3054 return isl_pw_aff_free(pwaff);
3057 return pwaff;
3060 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3062 int i;
3064 pwaff = isl_pw_aff_cow(pwaff);
3065 if (!pwaff)
3066 return NULL;
3067 if (pwaff->n == 0)
3068 return pwaff;
3070 for (i = 0; i < pwaff->n; ++i) {
3071 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3072 if (!pwaff->p[i].aff)
3073 return isl_pw_aff_free(pwaff);
3076 return pwaff;
3079 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3081 int i;
3083 pwaff = isl_pw_aff_cow(pwaff);
3084 if (!pwaff)
3085 return NULL;
3086 if (pwaff->n == 0)
3087 return pwaff;
3089 for (i = 0; i < pwaff->n; ++i) {
3090 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3091 if (!pwaff->p[i].aff)
3092 return isl_pw_aff_free(pwaff);
3095 return pwaff;
3098 /* Assuming that "cond1" and "cond2" are disjoint,
3099 * return an affine expression that is equal to pwaff1 on cond1
3100 * and to pwaff2 on cond2.
3102 static __isl_give isl_pw_aff *isl_pw_aff_select(
3103 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3104 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3106 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3107 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3109 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3112 /* Return an affine expression that is equal to pwaff_true for elements
3113 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3114 * is zero.
3115 * That is, return cond ? pwaff_true : pwaff_false;
3117 * If "cond" involves and NaN, then we conservatively return a NaN
3118 * on its entire domain. In principle, we could consider the pieces
3119 * where it is NaN separately from those where it is not.
3121 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3122 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3124 isl_set *cond_true, *cond_false;
3126 if (!cond)
3127 goto error;
3128 if (isl_pw_aff_involves_nan(cond)) {
3129 isl_space *space = isl_pw_aff_get_domain_space(cond);
3130 isl_local_space *ls = isl_local_space_from_space(space);
3131 isl_pw_aff_free(cond);
3132 isl_pw_aff_free(pwaff_true);
3133 isl_pw_aff_free(pwaff_false);
3134 return isl_pw_aff_nan_on_domain(ls);
3137 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3138 cond_false = isl_pw_aff_zero_set(cond);
3139 return isl_pw_aff_select(cond_true, pwaff_true,
3140 cond_false, pwaff_false);
3141 error:
3142 isl_pw_aff_free(cond);
3143 isl_pw_aff_free(pwaff_true);
3144 isl_pw_aff_free(pwaff_false);
3145 return NULL;
3148 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3150 if (!aff)
3151 return -1;
3153 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3156 /* Check whether pwaff is a piecewise constant.
3158 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3160 int i;
3162 if (!pwaff)
3163 return -1;
3165 for (i = 0; i < pwaff->n; ++i) {
3166 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3167 if (is_cst < 0 || !is_cst)
3168 return is_cst;
3171 return 1;
3174 /* Return the product of "aff1" and "aff2".
3176 * If either of the two is NaN, then the result is NaN.
3178 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3180 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3181 __isl_take isl_aff *aff2)
3183 if (!aff1 || !aff2)
3184 goto error;
3186 if (isl_aff_is_nan(aff1)) {
3187 isl_aff_free(aff2);
3188 return aff1;
3190 if (isl_aff_is_nan(aff2)) {
3191 isl_aff_free(aff1);
3192 return aff2;
3195 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3196 return isl_aff_mul(aff2, aff1);
3198 if (!isl_aff_is_cst(aff2))
3199 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3200 "at least one affine expression should be constant",
3201 goto error);
3203 aff1 = isl_aff_cow(aff1);
3204 if (!aff1 || !aff2)
3205 goto error;
3207 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3208 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3210 isl_aff_free(aff2);
3211 return aff1;
3212 error:
3213 isl_aff_free(aff1);
3214 isl_aff_free(aff2);
3215 return NULL;
3218 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3220 * If either of the two is NaN, then the result is NaN.
3222 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3223 __isl_take isl_aff *aff2)
3225 int is_cst;
3226 int neg;
3228 if (!aff1 || !aff2)
3229 goto error;
3231 if (isl_aff_is_nan(aff1)) {
3232 isl_aff_free(aff2);
3233 return aff1;
3235 if (isl_aff_is_nan(aff2)) {
3236 isl_aff_free(aff1);
3237 return aff2;
3240 is_cst = isl_aff_is_cst(aff2);
3241 if (is_cst < 0)
3242 goto error;
3243 if (!is_cst)
3244 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3245 "second argument should be a constant", goto error);
3247 if (!aff2)
3248 goto error;
3250 neg = isl_int_is_neg(aff2->v->el[1]);
3251 if (neg) {
3252 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3253 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3256 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3257 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3259 if (neg) {
3260 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3261 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3264 isl_aff_free(aff2);
3265 return aff1;
3266 error:
3267 isl_aff_free(aff1);
3268 isl_aff_free(aff2);
3269 return NULL;
3272 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3273 __isl_take isl_pw_aff *pwaff2)
3275 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3278 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3279 __isl_take isl_pw_aff *pwaff2)
3281 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3284 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3285 __isl_take isl_pw_aff *pwaff2)
3287 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3290 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3291 __isl_take isl_pw_aff *pwaff2)
3293 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3296 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3297 __isl_take isl_pw_aff *pwaff2)
3299 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3302 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3303 __isl_take isl_pw_aff *pa2)
3305 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3308 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3310 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3311 __isl_take isl_pw_aff *pa2)
3313 int is_cst;
3315 is_cst = isl_pw_aff_is_cst(pa2);
3316 if (is_cst < 0)
3317 goto error;
3318 if (!is_cst)
3319 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3320 "second argument should be a piecewise constant",
3321 goto error);
3322 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3323 error:
3324 isl_pw_aff_free(pa1);
3325 isl_pw_aff_free(pa2);
3326 return NULL;
3329 /* Compute the quotient of the integer division of "pa1" by "pa2"
3330 * with rounding towards zero.
3331 * "pa2" is assumed to be a piecewise constant.
3333 * In particular, return
3335 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3338 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3339 __isl_take isl_pw_aff *pa2)
3341 int is_cst;
3342 isl_set *cond;
3343 isl_pw_aff *f, *c;
3345 is_cst = isl_pw_aff_is_cst(pa2);
3346 if (is_cst < 0)
3347 goto error;
3348 if (!is_cst)
3349 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3350 "second argument should be a piecewise constant",
3351 goto error);
3353 pa1 = isl_pw_aff_div(pa1, pa2);
3355 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3356 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3357 c = isl_pw_aff_ceil(pa1);
3358 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3359 error:
3360 isl_pw_aff_free(pa1);
3361 isl_pw_aff_free(pa2);
3362 return NULL;
3365 /* Compute the remainder of the integer division of "pa1" by "pa2"
3366 * with rounding towards zero.
3367 * "pa2" is assumed to be a piecewise constant.
3369 * In particular, return
3371 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3374 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3375 __isl_take isl_pw_aff *pa2)
3377 int is_cst;
3378 isl_pw_aff *res;
3380 is_cst = isl_pw_aff_is_cst(pa2);
3381 if (is_cst < 0)
3382 goto error;
3383 if (!is_cst)
3384 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3385 "second argument should be a piecewise constant",
3386 goto error);
3387 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3388 res = isl_pw_aff_mul(pa2, res);
3389 res = isl_pw_aff_sub(pa1, res);
3390 return res;
3391 error:
3392 isl_pw_aff_free(pa1);
3393 isl_pw_aff_free(pa2);
3394 return NULL;
3397 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3398 __isl_take isl_pw_aff *pwaff2)
3400 isl_set *le;
3401 isl_set *dom;
3403 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3404 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3405 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3406 isl_pw_aff_copy(pwaff2));
3407 dom = isl_set_subtract(dom, isl_set_copy(le));
3408 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3411 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3412 __isl_take isl_pw_aff *pwaff2)
3414 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3417 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3418 __isl_take isl_pw_aff *pwaff2)
3420 isl_set *ge;
3421 isl_set *dom;
3423 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3424 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3425 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3426 isl_pw_aff_copy(pwaff2));
3427 dom = isl_set_subtract(dom, isl_set_copy(ge));
3428 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3431 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3432 __isl_take isl_pw_aff *pwaff2)
3434 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3437 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3438 __isl_take isl_pw_aff_list *list,
3439 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3440 __isl_take isl_pw_aff *pwaff2))
3442 int i;
3443 isl_ctx *ctx;
3444 isl_pw_aff *res;
3446 if (!list)
3447 return NULL;
3449 ctx = isl_pw_aff_list_get_ctx(list);
3450 if (list->n < 1)
3451 isl_die(ctx, isl_error_invalid,
3452 "list should contain at least one element", goto error);
3454 res = isl_pw_aff_copy(list->p[0]);
3455 for (i = 1; i < list->n; ++i)
3456 res = fn(res, isl_pw_aff_copy(list->p[i]));
3458 isl_pw_aff_list_free(list);
3459 return res;
3460 error:
3461 isl_pw_aff_list_free(list);
3462 return NULL;
3465 /* Return an isl_pw_aff that maps each element in the intersection of the
3466 * domains of the elements of list to the minimal corresponding affine
3467 * expression.
3469 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3471 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3474 /* Return an isl_pw_aff that maps each element in the intersection of the
3475 * domains of the elements of list to the maximal corresponding affine
3476 * expression.
3478 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3480 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3483 /* Mark the domains of "pwaff" as rational.
3485 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3487 int i;
3489 pwaff = isl_pw_aff_cow(pwaff);
3490 if (!pwaff)
3491 return NULL;
3492 if (pwaff->n == 0)
3493 return pwaff;
3495 for (i = 0; i < pwaff->n; ++i) {
3496 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3497 if (!pwaff->p[i].set)
3498 return isl_pw_aff_free(pwaff);
3501 return pwaff;
3504 /* Mark the domains of the elements of "list" as rational.
3506 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3507 __isl_take isl_pw_aff_list *list)
3509 int i, n;
3511 if (!list)
3512 return NULL;
3513 if (list->n == 0)
3514 return list;
3516 n = list->n;
3517 for (i = 0; i < n; ++i) {
3518 isl_pw_aff *pa;
3520 pa = isl_pw_aff_list_get_pw_aff(list, i);
3521 pa = isl_pw_aff_set_rational(pa);
3522 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3525 return list;
3528 /* Do the parameters of "aff" match those of "space"?
3530 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3531 __isl_keep isl_space *space)
3533 isl_space *aff_space;
3534 int match;
3536 if (!aff || !space)
3537 return -1;
3539 aff_space = isl_aff_get_domain_space(aff);
3541 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3543 isl_space_free(aff_space);
3544 return match;
3547 /* Check that the domain space of "aff" matches "space".
3549 * Return 0 on success and -1 on error.
3551 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3552 __isl_keep isl_space *space)
3554 isl_space *aff_space;
3555 int match;
3557 if (!aff || !space)
3558 return -1;
3560 aff_space = isl_aff_get_domain_space(aff);
3562 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3563 if (match < 0)
3564 goto error;
3565 if (!match)
3566 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3567 "parameters don't match", goto error);
3568 match = isl_space_tuple_is_equal(space, isl_dim_in,
3569 aff_space, isl_dim_set);
3570 if (match < 0)
3571 goto error;
3572 if (!match)
3573 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3574 "domains don't match", goto error);
3575 isl_space_free(aff_space);
3576 return 0;
3577 error:
3578 isl_space_free(aff_space);
3579 return -1;
3582 #undef BASE
3583 #define BASE aff
3584 #define NO_INTERSECT_DOMAIN
3585 #define NO_DOMAIN
3587 #include <isl_multi_templ.c>
3589 #undef NO_DOMAIN
3590 #undef NO_INTERSECT_DOMAIN
3592 /* Remove any internal structure of the domain of "ma".
3593 * If there is any such internal structure in the input,
3594 * then the name of the corresponding space is also removed.
3596 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3597 __isl_take isl_multi_aff *ma)
3599 isl_space *space;
3601 if (!ma)
3602 return NULL;
3604 if (!ma->space->nested[0])
3605 return ma;
3607 space = isl_multi_aff_get_space(ma);
3608 space = isl_space_flatten_domain(space);
3609 ma = isl_multi_aff_reset_space(ma, space);
3611 return ma;
3614 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3615 * of the space to its domain.
3617 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3619 int i, n_in;
3620 isl_local_space *ls;
3621 isl_multi_aff *ma;
3623 if (!space)
3624 return NULL;
3625 if (!isl_space_is_map(space))
3626 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3627 "not a map space", goto error);
3629 n_in = isl_space_dim(space, isl_dim_in);
3630 space = isl_space_domain_map(space);
3632 ma = isl_multi_aff_alloc(isl_space_copy(space));
3633 if (n_in == 0) {
3634 isl_space_free(space);
3635 return ma;
3638 space = isl_space_domain(space);
3639 ls = isl_local_space_from_space(space);
3640 for (i = 0; i < n_in; ++i) {
3641 isl_aff *aff;
3643 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3644 isl_dim_set, i);
3645 ma = isl_multi_aff_set_aff(ma, i, aff);
3647 isl_local_space_free(ls);
3648 return ma;
3649 error:
3650 isl_space_free(space);
3651 return NULL;
3654 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3655 * of the space to its range.
3657 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3659 int i, n_in, n_out;
3660 isl_local_space *ls;
3661 isl_multi_aff *ma;
3663 if (!space)
3664 return NULL;
3665 if (!isl_space_is_map(space))
3666 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3667 "not a map space", goto error);
3669 n_in = isl_space_dim(space, isl_dim_in);
3670 n_out = isl_space_dim(space, isl_dim_out);
3671 space = isl_space_range_map(space);
3673 ma = isl_multi_aff_alloc(isl_space_copy(space));
3674 if (n_out == 0) {
3675 isl_space_free(space);
3676 return ma;
3679 space = isl_space_domain(space);
3680 ls = isl_local_space_from_space(space);
3681 for (i = 0; i < n_out; ++i) {
3682 isl_aff *aff;
3684 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3685 isl_dim_set, n_in + i);
3686 ma = isl_multi_aff_set_aff(ma, i, aff);
3688 isl_local_space_free(ls);
3689 return ma;
3690 error:
3691 isl_space_free(space);
3692 return NULL;
3695 /* Given the space of a set and a range of set dimensions,
3696 * construct an isl_multi_aff that projects out those dimensions.
3698 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3699 __isl_take isl_space *space, enum isl_dim_type type,
3700 unsigned first, unsigned n)
3702 int i, dim;
3703 isl_local_space *ls;
3704 isl_multi_aff *ma;
3706 if (!space)
3707 return NULL;
3708 if (!isl_space_is_set(space))
3709 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3710 "expecting set space", goto error);
3711 if (type != isl_dim_set)
3712 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3713 "only set dimensions can be projected out", goto error);
3715 dim = isl_space_dim(space, isl_dim_set);
3716 if (first + n > dim)
3717 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3718 "range out of bounds", goto error);
3720 space = isl_space_from_domain(space);
3721 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3723 if (dim == n)
3724 return isl_multi_aff_alloc(space);
3726 ma = isl_multi_aff_alloc(isl_space_copy(space));
3727 space = isl_space_domain(space);
3728 ls = isl_local_space_from_space(space);
3730 for (i = 0; i < first; ++i) {
3731 isl_aff *aff;
3733 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3734 isl_dim_set, i);
3735 ma = isl_multi_aff_set_aff(ma, i, aff);
3738 for (i = 0; i < dim - (first + n); ++i) {
3739 isl_aff *aff;
3741 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3742 isl_dim_set, first + n + i);
3743 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3746 isl_local_space_free(ls);
3747 return ma;
3748 error:
3749 isl_space_free(space);
3750 return NULL;
3753 /* Given the space of a set and a range of set dimensions,
3754 * construct an isl_pw_multi_aff that projects out those dimensions.
3756 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3757 __isl_take isl_space *space, enum isl_dim_type type,
3758 unsigned first, unsigned n)
3760 isl_multi_aff *ma;
3762 ma = isl_multi_aff_project_out_map(space, type, first, n);
3763 return isl_pw_multi_aff_from_multi_aff(ma);
3766 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3767 * domain.
3769 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3770 __isl_take isl_multi_aff *ma)
3772 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3773 return isl_pw_multi_aff_alloc(dom, ma);
3776 /* Create a piecewise multi-affine expression in the given space that maps each
3777 * input dimension to the corresponding output dimension.
3779 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3780 __isl_take isl_space *space)
3782 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3785 /* Add "ma2" to "ma1" and return the result.
3787 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3789 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3790 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3792 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3795 /* Add "ma2" to "ma1" and return the result.
3797 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3798 __isl_take isl_multi_aff *ma2)
3800 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3801 &isl_multi_aff_add_aligned);
3804 /* Exploit the equalities in "eq" to simplify the affine expressions.
3806 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3807 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3809 int i;
3811 maff = isl_multi_aff_cow(maff);
3812 if (!maff || !eq)
3813 goto error;
3815 for (i = 0; i < maff->n; ++i) {
3816 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3817 isl_basic_set_copy(eq));
3818 if (!maff->p[i])
3819 goto error;
3822 isl_basic_set_free(eq);
3823 return maff;
3824 error:
3825 isl_basic_set_free(eq);
3826 isl_multi_aff_free(maff);
3827 return NULL;
3830 /* Given f, return floor(f).
3832 __isl_give isl_multi_aff *isl_multi_aff_floor(__isl_take isl_multi_aff *ma)
3834 int i;
3836 ma = isl_multi_aff_cow(ma);
3837 if (!ma)
3838 return NULL;
3840 for (i = 0; i < ma->n; ++i) {
3841 ma->p[i] = isl_aff_floor(ma->p[i]);
3842 if (!ma->p[i])
3843 return isl_multi_aff_free(ma);
3846 return ma;
3849 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3850 isl_int f)
3852 int i;
3854 maff = isl_multi_aff_cow(maff);
3855 if (!maff)
3856 return NULL;
3858 for (i = 0; i < maff->n; ++i) {
3859 maff->p[i] = isl_aff_scale(maff->p[i], f);
3860 if (!maff->p[i])
3861 return isl_multi_aff_free(maff);
3864 return maff;
3867 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3868 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3870 maff1 = isl_multi_aff_add(maff1, maff2);
3871 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3872 return maff1;
3875 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3877 if (!maff)
3878 return -1;
3880 return 0;
3883 /* Return the set of domain elements where "ma1" is lexicographically
3884 * smaller than or equal to "ma2".
3886 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3887 __isl_take isl_multi_aff *ma2)
3889 return isl_multi_aff_lex_ge_set(ma2, ma1);
3892 /* Return the set of domain elements where "ma1" is lexicographically
3893 * greater than or equal to "ma2".
3895 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3896 __isl_take isl_multi_aff *ma2)
3898 isl_space *space;
3899 isl_map *map1, *map2;
3900 isl_map *map, *ge;
3902 map1 = isl_map_from_multi_aff(ma1);
3903 map2 = isl_map_from_multi_aff(ma2);
3904 map = isl_map_range_product(map1, map2);
3905 space = isl_space_range(isl_map_get_space(map));
3906 space = isl_space_domain(isl_space_unwrap(space));
3907 ge = isl_map_lex_ge(space);
3908 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3910 return isl_map_domain(map);
3913 #undef PW
3914 #define PW isl_pw_multi_aff
3915 #undef EL
3916 #define EL isl_multi_aff
3917 #undef EL_IS_ZERO
3918 #define EL_IS_ZERO is_empty
3919 #undef ZERO
3920 #define ZERO empty
3921 #undef IS_ZERO
3922 #define IS_ZERO is_empty
3923 #undef FIELD
3924 #define FIELD maff
3925 #undef DEFAULT_IS_ZERO
3926 #define DEFAULT_IS_ZERO 0
3928 #define NO_SUB
3929 #define NO_EVAL
3930 #define NO_OPT
3931 #define NO_INVOLVES_DIMS
3932 #define NO_INSERT_DIMS
3933 #define NO_LIFT
3934 #define NO_MORPH
3936 #include <isl_pw_templ.c>
3938 #undef NO_SUB
3940 #undef UNION
3941 #define UNION isl_union_pw_multi_aff
3942 #undef PART
3943 #define PART isl_pw_multi_aff
3944 #undef PARTS
3945 #define PARTS pw_multi_aff
3946 #define ALIGN_DOMAIN
3948 #define NO_EVAL
3950 #include <isl_union_templ.c>
3952 /* Given a function "cmp" that returns the set of elements where
3953 * "ma1" is "better" than "ma2", return the intersection of this
3954 * set with "dom1" and "dom2".
3956 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3957 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3958 __isl_keep isl_multi_aff *ma2,
3959 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3960 __isl_take isl_multi_aff *ma2))
3962 isl_set *common;
3963 isl_set *better;
3964 int is_empty;
3966 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3967 is_empty = isl_set_plain_is_empty(common);
3968 if (is_empty >= 0 && is_empty)
3969 return common;
3970 if (is_empty < 0)
3971 return isl_set_free(common);
3972 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3973 better = isl_set_intersect(common, better);
3975 return better;
3978 /* Given a function "cmp" that returns the set of elements where
3979 * "ma1" is "better" than "ma2", return a piecewise multi affine
3980 * expression defined on the union of the definition domains
3981 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3982 * "pma2" on each cell. If only one of the two input functions
3983 * is defined on a given cell, then it is considered the best.
3985 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3986 __isl_take isl_pw_multi_aff *pma1,
3987 __isl_take isl_pw_multi_aff *pma2,
3988 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3989 __isl_take isl_multi_aff *ma2))
3991 int i, j, n;
3992 isl_pw_multi_aff *res = NULL;
3993 isl_ctx *ctx;
3994 isl_set *set = NULL;
3996 if (!pma1 || !pma2)
3997 goto error;
3999 ctx = isl_space_get_ctx(pma1->dim);
4000 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4001 isl_die(ctx, isl_error_invalid,
4002 "arguments should live in the same space", goto error);
4004 if (isl_pw_multi_aff_is_empty(pma1)) {
4005 isl_pw_multi_aff_free(pma1);
4006 return pma2;
4009 if (isl_pw_multi_aff_is_empty(pma2)) {
4010 isl_pw_multi_aff_free(pma2);
4011 return pma1;
4014 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4015 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4017 for (i = 0; i < pma1->n; ++i) {
4018 set = isl_set_copy(pma1->p[i].set);
4019 for (j = 0; j < pma2->n; ++j) {
4020 isl_set *better;
4021 int is_empty;
4023 better = shared_and_better(pma2->p[j].set,
4024 pma1->p[i].set, pma2->p[j].maff,
4025 pma1->p[i].maff, cmp);
4026 is_empty = isl_set_plain_is_empty(better);
4027 if (is_empty < 0 || is_empty) {
4028 isl_set_free(better);
4029 if (is_empty < 0)
4030 goto error;
4031 continue;
4033 set = isl_set_subtract(set, isl_set_copy(better));
4035 res = isl_pw_multi_aff_add_piece(res, better,
4036 isl_multi_aff_copy(pma2->p[j].maff));
4038 res = isl_pw_multi_aff_add_piece(res, set,
4039 isl_multi_aff_copy(pma1->p[i].maff));
4042 for (j = 0; j < pma2->n; ++j) {
4043 set = isl_set_copy(pma2->p[j].set);
4044 for (i = 0; i < pma1->n; ++i)
4045 set = isl_set_subtract(set,
4046 isl_set_copy(pma1->p[i].set));
4047 res = isl_pw_multi_aff_add_piece(res, set,
4048 isl_multi_aff_copy(pma2->p[j].maff));
4051 isl_pw_multi_aff_free(pma1);
4052 isl_pw_multi_aff_free(pma2);
4054 return res;
4055 error:
4056 isl_pw_multi_aff_free(pma1);
4057 isl_pw_multi_aff_free(pma2);
4058 isl_set_free(set);
4059 return isl_pw_multi_aff_free(res);
4062 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4063 __isl_take isl_pw_multi_aff *pma1,
4064 __isl_take isl_pw_multi_aff *pma2)
4066 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4069 /* Given two piecewise multi affine expressions, return a piecewise
4070 * multi-affine expression defined on the union of the definition domains
4071 * of the inputs that is equal to the lexicographic maximum of the two
4072 * inputs on each cell. If only one of the two inputs is defined on
4073 * a given cell, then it is considered to be the maximum.
4075 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4076 __isl_take isl_pw_multi_aff *pma1,
4077 __isl_take isl_pw_multi_aff *pma2)
4079 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4080 &pw_multi_aff_union_lexmax);
4083 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4084 __isl_take isl_pw_multi_aff *pma1,
4085 __isl_take isl_pw_multi_aff *pma2)
4087 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4090 /* Given two piecewise multi affine expressions, return a piecewise
4091 * multi-affine expression defined on the union of the definition domains
4092 * of the inputs that is equal to the lexicographic minimum of the two
4093 * inputs on each cell. If only one of the two inputs is defined on
4094 * a given cell, then it is considered to be the minimum.
4096 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4097 __isl_take isl_pw_multi_aff *pma1,
4098 __isl_take isl_pw_multi_aff *pma2)
4100 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4101 &pw_multi_aff_union_lexmin);
4104 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4105 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4107 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4108 &isl_multi_aff_add);
4111 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4112 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4114 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4115 &pw_multi_aff_add);
4118 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4119 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4121 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4122 &isl_multi_aff_sub);
4125 /* Subtract "pma2" from "pma1" and return the result.
4127 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4128 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4130 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4131 &pw_multi_aff_sub);
4134 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4135 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4137 return isl_pw_multi_aff_union_add_(pma1, pma2);
4140 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4141 * with the actual sum on the shared domain and
4142 * the defined expression on the symmetric difference of the domains.
4144 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4145 __isl_take isl_union_pw_multi_aff *upma1,
4146 __isl_take isl_union_pw_multi_aff *upma2)
4148 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4151 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4152 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4154 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4155 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4157 int i, j, n;
4158 isl_space *space;
4159 isl_pw_multi_aff *res;
4161 if (!pma1 || !pma2)
4162 goto error;
4164 n = pma1->n * pma2->n;
4165 space = isl_space_product(isl_space_copy(pma1->dim),
4166 isl_space_copy(pma2->dim));
4167 res = isl_pw_multi_aff_alloc_size(space, n);
4169 for (i = 0; i < pma1->n; ++i) {
4170 for (j = 0; j < pma2->n; ++j) {
4171 isl_set *domain;
4172 isl_multi_aff *ma;
4174 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4175 isl_set_copy(pma2->p[j].set));
4176 ma = isl_multi_aff_product(
4177 isl_multi_aff_copy(pma1->p[i].maff),
4178 isl_multi_aff_copy(pma2->p[j].maff));
4179 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4183 isl_pw_multi_aff_free(pma1);
4184 isl_pw_multi_aff_free(pma2);
4185 return res;
4186 error:
4187 isl_pw_multi_aff_free(pma1);
4188 isl_pw_multi_aff_free(pma2);
4189 return NULL;
4192 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4193 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4195 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4196 &pw_multi_aff_product);
4199 /* Construct a map mapping the domain of the piecewise multi-affine expression
4200 * to its range, with each dimension in the range equated to the
4201 * corresponding affine expression on its cell.
4203 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4205 int i;
4206 isl_map *map;
4208 if (!pma)
4209 return NULL;
4211 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4213 for (i = 0; i < pma->n; ++i) {
4214 isl_multi_aff *maff;
4215 isl_basic_map *bmap;
4216 isl_map *map_i;
4218 maff = isl_multi_aff_copy(pma->p[i].maff);
4219 bmap = isl_basic_map_from_multi_aff(maff);
4220 map_i = isl_map_from_basic_map(bmap);
4221 map_i = isl_map_intersect_domain(map_i,
4222 isl_set_copy(pma->p[i].set));
4223 map = isl_map_union_disjoint(map, map_i);
4226 isl_pw_multi_aff_free(pma);
4227 return map;
4230 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4232 if (!pma)
4233 return NULL;
4235 if (!isl_space_is_set(pma->dim))
4236 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4237 "isl_pw_multi_aff cannot be converted into an isl_set",
4238 goto error);
4240 return isl_map_from_pw_multi_aff(pma);
4241 error:
4242 isl_pw_multi_aff_free(pma);
4243 return NULL;
4246 /* Given a basic map with a single output dimension that is defined
4247 * in terms of the parameters and input dimensions using an equality,
4248 * extract an isl_aff that expresses the output dimension in terms
4249 * of the parameters and input dimensions.
4250 * Note that this expression may involve integer divisions defined
4251 * in terms of parameters and input dimensions.
4253 * This function shares some similarities with
4254 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4256 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4257 __isl_take isl_basic_map *bmap)
4259 int eq;
4260 unsigned offset;
4261 unsigned n_div;
4262 isl_local_space *ls;
4263 isl_aff *aff;
4265 if (!bmap)
4266 return NULL;
4267 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4268 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4269 "basic map should have a single output dimension",
4270 goto error);
4271 eq = isl_basic_map_output_defining_equality(bmap, 0);
4272 if (eq >= bmap->n_eq)
4273 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4274 "unable to find suitable equality", goto error);
4275 ls = isl_basic_map_get_local_space(bmap);
4276 aff = isl_aff_alloc(isl_local_space_domain(ls));
4277 if (!aff)
4278 goto error;
4279 offset = isl_basic_map_offset(bmap, isl_dim_out);
4280 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4281 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4282 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4283 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4284 n_div);
4285 } else {
4286 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4287 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4288 n_div);
4290 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4291 isl_basic_map_free(bmap);
4293 aff = isl_aff_remove_unused_divs(aff);
4294 return aff;
4295 error:
4296 isl_basic_map_free(bmap);
4297 return NULL;
4300 /* Given a basic map where each output dimension is defined
4301 * in terms of the parameters and input dimensions using an equality,
4302 * extract an isl_multi_aff that expresses the output dimensions in terms
4303 * of the parameters and input dimensions.
4305 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4306 __isl_take isl_basic_map *bmap)
4308 int i;
4309 unsigned n_out;
4310 isl_multi_aff *ma;
4312 if (!bmap)
4313 return NULL;
4315 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4316 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4318 for (i = 0; i < n_out; ++i) {
4319 isl_basic_map *bmap_i;
4320 isl_aff *aff;
4322 bmap_i = isl_basic_map_copy(bmap);
4323 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4324 i + 1, n_out - (1 + i));
4325 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4326 aff = extract_isl_aff_from_basic_map(bmap_i);
4327 ma = isl_multi_aff_set_aff(ma, i, aff);
4330 isl_basic_map_free(bmap);
4332 return ma;
4335 /* Given a basic set where each set dimension is defined
4336 * in terms of the parameters using an equality,
4337 * extract an isl_multi_aff that expresses the set dimensions in terms
4338 * of the parameters.
4340 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4341 __isl_take isl_basic_set *bset)
4343 return extract_isl_multi_aff_from_basic_map(bset);
4346 /* Create an isl_pw_multi_aff that is equivalent to
4347 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4348 * The given basic map is such that each output dimension is defined
4349 * in terms of the parameters and input dimensions using an equality.
4351 * Since some applications expect the result of isl_pw_multi_aff_from_map
4352 * to only contain integer affine expressions, we compute the floor
4353 * of the expression before returning.
4355 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4356 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4358 isl_multi_aff *ma;
4360 ma = extract_isl_multi_aff_from_basic_map(bmap);
4361 ma = isl_multi_aff_floor(ma);
4362 return isl_pw_multi_aff_alloc(domain, ma);
4365 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4366 * This obviously only works if the input "map" is single-valued.
4367 * If so, we compute the lexicographic minimum of the image in the form
4368 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4369 * to its lexicographic minimum.
4370 * If the input is not single-valued, we produce an error.
4372 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4373 __isl_take isl_map *map)
4375 int i;
4376 int sv;
4377 isl_pw_multi_aff *pma;
4379 sv = isl_map_is_single_valued(map);
4380 if (sv < 0)
4381 goto error;
4382 if (!sv)
4383 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4384 "map is not single-valued", goto error);
4385 map = isl_map_make_disjoint(map);
4386 if (!map)
4387 return NULL;
4389 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4391 for (i = 0; i < map->n; ++i) {
4392 isl_pw_multi_aff *pma_i;
4393 isl_basic_map *bmap;
4394 bmap = isl_basic_map_copy(map->p[i]);
4395 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4396 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4399 isl_map_free(map);
4400 return pma;
4401 error:
4402 isl_map_free(map);
4403 return NULL;
4406 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4407 * taking into account that the output dimension at position "d"
4408 * can be represented as
4410 * x = floor((e(...) + c1) / m)
4412 * given that constraint "i" is of the form
4414 * e(...) + c1 - m x >= 0
4417 * Let "map" be of the form
4419 * A -> B
4421 * We construct a mapping
4423 * A -> [A -> x = floor(...)]
4425 * apply that to the map, obtaining
4427 * [A -> x = floor(...)] -> B
4429 * and equate dimension "d" to x.
4430 * We then compute a isl_pw_multi_aff representation of the resulting map
4431 * and plug in the mapping above.
4433 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4434 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4436 isl_ctx *ctx;
4437 isl_space *space;
4438 isl_local_space *ls;
4439 isl_multi_aff *ma;
4440 isl_aff *aff;
4441 isl_vec *v;
4442 isl_map *insert;
4443 int offset;
4444 int n;
4445 int n_in;
4446 isl_pw_multi_aff *pma;
4447 int is_set;
4449 is_set = isl_map_is_set(map);
4451 offset = isl_basic_map_offset(hull, isl_dim_out);
4452 ctx = isl_map_get_ctx(map);
4453 space = isl_space_domain(isl_map_get_space(map));
4454 n_in = isl_space_dim(space, isl_dim_set);
4455 n = isl_space_dim(space, isl_dim_all);
4457 v = isl_vec_alloc(ctx, 1 + 1 + n);
4458 if (v) {
4459 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4460 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4462 isl_basic_map_free(hull);
4464 ls = isl_local_space_from_space(isl_space_copy(space));
4465 aff = isl_aff_alloc_vec(ls, v);
4466 aff = isl_aff_floor(aff);
4467 if (is_set) {
4468 isl_space_free(space);
4469 ma = isl_multi_aff_from_aff(aff);
4470 } else {
4471 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4472 ma = isl_multi_aff_range_product(ma,
4473 isl_multi_aff_from_aff(aff));
4476 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4477 map = isl_map_apply_domain(map, insert);
4478 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4479 pma = isl_pw_multi_aff_from_map(map);
4480 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4482 return pma;
4485 /* Is constraint "c" of the form
4487 * e(...) + c1 - m x >= 0
4489 * or
4491 * -e(...) + c2 + m x >= 0
4493 * where m > 1 and e only depends on parameters and input dimemnsions?
4495 * "offset" is the offset of the output dimensions
4496 * "pos" is the position of output dimension x.
4498 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4500 if (isl_int_is_zero(c[offset + d]))
4501 return 0;
4502 if (isl_int_is_one(c[offset + d]))
4503 return 0;
4504 if (isl_int_is_negone(c[offset + d]))
4505 return 0;
4506 if (isl_seq_first_non_zero(c + offset, d) != -1)
4507 return 0;
4508 if (isl_seq_first_non_zero(c + offset + d + 1,
4509 total - (offset + d + 1)) != -1)
4510 return 0;
4511 return 1;
4514 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4516 * As a special case, we first check if there is any pair of constraints,
4517 * shared by all the basic maps in "map" that force a given dimension
4518 * to be equal to the floor of some affine combination of the input dimensions.
4520 * In particular, if we can find two constraints
4522 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4524 * and
4526 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4528 * where m > 1 and e only depends on parameters and input dimemnsions,
4529 * and such that
4531 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4533 * then we know that we can take
4535 * x = floor((e(...) + c1) / m)
4537 * without having to perform any computation.
4539 * Note that we know that
4541 * c1 + c2 >= 1
4543 * If c1 + c2 were 0, then we would have detected an equality during
4544 * simplification. If c1 + c2 were negative, then we would have detected
4545 * a contradiction.
4547 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4548 __isl_take isl_map *map)
4550 int d, dim;
4551 int i, j, n;
4552 int offset, total;
4553 isl_int sum;
4554 isl_basic_map *hull;
4556 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4557 if (!hull)
4558 goto error;
4560 isl_int_init(sum);
4561 dim = isl_map_dim(map, isl_dim_out);
4562 offset = isl_basic_map_offset(hull, isl_dim_out);
4563 total = 1 + isl_basic_map_total_dim(hull);
4564 n = hull->n_ineq;
4565 for (d = 0; d < dim; ++d) {
4566 for (i = 0; i < n; ++i) {
4567 if (!is_potential_div_constraint(hull->ineq[i],
4568 offset, d, total))
4569 continue;
4570 for (j = i + 1; j < n; ++j) {
4571 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4572 hull->ineq[j] + 1, total - 1))
4573 continue;
4574 isl_int_add(sum, hull->ineq[i][0],
4575 hull->ineq[j][0]);
4576 if (isl_int_abs_lt(sum,
4577 hull->ineq[i][offset + d]))
4578 break;
4581 if (j >= n)
4582 continue;
4583 isl_int_clear(sum);
4584 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4585 j = i;
4586 return pw_multi_aff_from_map_div(map, hull, d, j);
4589 isl_int_clear(sum);
4590 isl_basic_map_free(hull);
4591 return pw_multi_aff_from_map_base(map);
4592 error:
4593 isl_map_free(map);
4594 isl_basic_map_free(hull);
4595 return NULL;
4598 /* Given an affine expression
4600 * [A -> B] -> f(A,B)
4602 * construct an isl_multi_aff
4604 * [A -> B] -> B'
4606 * such that dimension "d" in B' is set to "aff" and the remaining
4607 * dimensions are set equal to the corresponding dimensions in B.
4608 * "n_in" is the dimension of the space A.
4609 * "n_out" is the dimension of the space B.
4611 * If "is_set" is set, then the affine expression is of the form
4613 * [B] -> f(B)
4615 * and we construct an isl_multi_aff
4617 * B -> B'
4619 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4620 unsigned n_in, unsigned n_out, int is_set)
4622 int i;
4623 isl_multi_aff *ma;
4624 isl_space *space, *space2;
4625 isl_local_space *ls;
4627 space = isl_aff_get_domain_space(aff);
4628 ls = isl_local_space_from_space(isl_space_copy(space));
4629 space2 = isl_space_copy(space);
4630 if (!is_set)
4631 space2 = isl_space_range(isl_space_unwrap(space2));
4632 space = isl_space_map_from_domain_and_range(space, space2);
4633 ma = isl_multi_aff_alloc(space);
4634 ma = isl_multi_aff_set_aff(ma, d, aff);
4636 for (i = 0; i < n_out; ++i) {
4637 if (i == d)
4638 continue;
4639 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4640 isl_dim_set, n_in + i);
4641 ma = isl_multi_aff_set_aff(ma, i, aff);
4644 isl_local_space_free(ls);
4646 return ma;
4649 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4650 * taking into account that the dimension at position "d" can be written as
4652 * x = m a + f(..) (1)
4654 * where m is equal to "gcd".
4655 * "i" is the index of the equality in "hull" that defines f(..).
4656 * In particular, the equality is of the form
4658 * f(..) - x + m g(existentials) = 0
4660 * or
4662 * -f(..) + x + m g(existentials) = 0
4664 * We basically plug (1) into "map", resulting in a map with "a"
4665 * in the range instead of "x". The corresponding isl_pw_multi_aff
4666 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4668 * Specifically, given the input map
4670 * A -> B
4672 * We first wrap it into a set
4674 * [A -> B]
4676 * and define (1) on top of the corresponding space, resulting in "aff".
4677 * We use this to create an isl_multi_aff that maps the output position "d"
4678 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4679 * We plug this into the wrapped map, unwrap the result and compute the
4680 * corresponding isl_pw_multi_aff.
4681 * The result is an expression
4683 * A -> T(A)
4685 * We adjust that to
4687 * A -> [A -> T(A)]
4689 * so that we can plug that into "aff", after extending the latter to
4690 * a mapping
4692 * [A -> B] -> B'
4695 * If "map" is actually a set, then there is no "A" space, meaning
4696 * that we do not need to perform any wrapping, and that the result
4697 * of the recursive call is of the form
4699 * [T]
4701 * which is plugged into a mapping of the form
4703 * B -> B'
4705 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4706 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4707 isl_int gcd)
4709 isl_set *set;
4710 isl_space *space;
4711 isl_local_space *ls;
4712 isl_aff *aff;
4713 isl_multi_aff *ma;
4714 isl_pw_multi_aff *pma, *id;
4715 unsigned n_in;
4716 unsigned o_out;
4717 unsigned n_out;
4718 int is_set;
4720 is_set = isl_map_is_set(map);
4722 n_in = isl_basic_map_dim(hull, isl_dim_in);
4723 n_out = isl_basic_map_dim(hull, isl_dim_out);
4724 o_out = isl_basic_map_offset(hull, isl_dim_out);
4726 if (is_set)
4727 set = map;
4728 else
4729 set = isl_map_wrap(map);
4730 space = isl_space_map_from_set(isl_set_get_space(set));
4731 ma = isl_multi_aff_identity(space);
4732 ls = isl_local_space_from_space(isl_set_get_space(set));
4733 aff = isl_aff_alloc(ls);
4734 if (aff) {
4735 isl_int_set_si(aff->v->el[0], 1);
4736 if (isl_int_is_one(hull->eq[i][o_out + d]))
4737 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4738 aff->v->size - 1);
4739 else
4740 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4741 aff->v->size - 1);
4742 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4744 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4745 set = isl_set_preimage_multi_aff(set, ma);
4747 ma = range_map(aff, d, n_in, n_out, is_set);
4749 if (is_set)
4750 map = set;
4751 else
4752 map = isl_set_unwrap(set);
4753 pma = isl_pw_multi_aff_from_map(set);
4755 if (!is_set) {
4756 space = isl_pw_multi_aff_get_domain_space(pma);
4757 space = isl_space_map_from_set(space);
4758 id = isl_pw_multi_aff_identity(space);
4759 pma = isl_pw_multi_aff_range_product(id, pma);
4761 id = isl_pw_multi_aff_from_multi_aff(ma);
4762 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4764 isl_basic_map_free(hull);
4765 return pma;
4768 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4770 * As a special case, we first check if all output dimensions are uniquely
4771 * defined in terms of the parameters and input dimensions over the entire
4772 * domain. If so, we extract the desired isl_pw_multi_aff directly
4773 * from the affine hull of "map" and its domain.
4775 * Otherwise, we check if any of the output dimensions is "strided".
4776 * That is, we check if can be written as
4778 * x = m a + f(..)
4780 * with m greater than 1, a some combination of existentiall quantified
4781 * variables and f and expression in the parameters and input dimensions.
4782 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4784 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4785 * special case.
4787 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4789 int i, j;
4790 int sv;
4791 isl_basic_map *hull;
4792 unsigned n_out;
4793 unsigned o_out;
4794 unsigned n_div;
4795 unsigned o_div;
4796 isl_int gcd;
4798 if (!map)
4799 return NULL;
4801 hull = isl_map_affine_hull(isl_map_copy(map));
4802 sv = isl_basic_map_plain_is_single_valued(hull);
4803 if (sv >= 0 && sv)
4804 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4805 if (sv < 0)
4806 hull = isl_basic_map_free(hull);
4807 if (!hull)
4808 goto error;
4810 n_div = isl_basic_map_dim(hull, isl_dim_div);
4811 o_div = isl_basic_map_offset(hull, isl_dim_div);
4813 if (n_div == 0) {
4814 isl_basic_map_free(hull);
4815 return pw_multi_aff_from_map_check_div(map);
4818 isl_int_init(gcd);
4820 n_out = isl_basic_map_dim(hull, isl_dim_out);
4821 o_out = isl_basic_map_offset(hull, isl_dim_out);
4823 for (i = 0; i < n_out; ++i) {
4824 for (j = 0; j < hull->n_eq; ++j) {
4825 isl_int *eq = hull->eq[j];
4826 isl_pw_multi_aff *res;
4828 if (!isl_int_is_one(eq[o_out + i]) &&
4829 !isl_int_is_negone(eq[o_out + i]))
4830 continue;
4831 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4832 continue;
4833 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4834 n_out - (i + 1)) != -1)
4835 continue;
4836 isl_seq_gcd(eq + o_div, n_div, &gcd);
4837 if (isl_int_is_zero(gcd))
4838 continue;
4839 if (isl_int_is_one(gcd))
4840 continue;
4842 res = pw_multi_aff_from_map_stride(map, hull,
4843 i, j, gcd);
4844 isl_int_clear(gcd);
4845 return res;
4849 isl_int_clear(gcd);
4850 isl_basic_map_free(hull);
4851 return pw_multi_aff_from_map_check_div(map);
4852 error:
4853 isl_map_free(map);
4854 return NULL;
4857 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4859 return isl_pw_multi_aff_from_map(set);
4862 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4863 * add it to *user.
4865 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4867 isl_union_pw_multi_aff **upma = user;
4868 isl_pw_multi_aff *pma;
4870 pma = isl_pw_multi_aff_from_map(map);
4871 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4873 return *upma ? 0 : -1;
4876 /* Try and create an isl_union_pw_multi_aff that is equivalent
4877 * to the given isl_union_map.
4878 * The isl_union_map is required to be single-valued in each space.
4879 * Otherwise, an error is produced.
4881 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4882 __isl_take isl_union_map *umap)
4884 isl_space *space;
4885 isl_union_pw_multi_aff *upma;
4887 space = isl_union_map_get_space(umap);
4888 upma = isl_union_pw_multi_aff_empty(space);
4889 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4890 upma = isl_union_pw_multi_aff_free(upma);
4891 isl_union_map_free(umap);
4893 return upma;
4896 /* Try and create an isl_union_pw_multi_aff that is equivalent
4897 * to the given isl_union_set.
4898 * The isl_union_set is required to be a singleton in each space.
4899 * Otherwise, an error is produced.
4901 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4902 __isl_take isl_union_set *uset)
4904 return isl_union_pw_multi_aff_from_union_map(uset);
4907 /* Return the piecewise affine expression "set ? 1 : 0".
4909 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4911 isl_pw_aff *pa;
4912 isl_space *space = isl_set_get_space(set);
4913 isl_local_space *ls = isl_local_space_from_space(space);
4914 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4915 isl_aff *one = isl_aff_zero_on_domain(ls);
4917 one = isl_aff_add_constant_si(one, 1);
4918 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4919 set = isl_set_complement(set);
4920 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4922 return pa;
4925 /* Plug in "subs" for dimension "type", "pos" of "aff".
4927 * Let i be the dimension to replace and let "subs" be of the form
4929 * f/d
4931 * and "aff" of the form
4933 * (a i + g)/m
4935 * The result is
4937 * (a f + d g')/(m d)
4939 * where g' is the result of plugging in "subs" in each of the integer
4940 * divisions in g.
4942 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4943 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4945 isl_ctx *ctx;
4946 isl_int v;
4948 aff = isl_aff_cow(aff);
4949 if (!aff || !subs)
4950 return isl_aff_free(aff);
4952 ctx = isl_aff_get_ctx(aff);
4953 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4954 isl_die(ctx, isl_error_invalid,
4955 "spaces don't match", return isl_aff_free(aff));
4956 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4957 isl_die(ctx, isl_error_unsupported,
4958 "cannot handle divs yet", return isl_aff_free(aff));
4960 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4961 if (!aff->ls)
4962 return isl_aff_free(aff);
4964 aff->v = isl_vec_cow(aff->v);
4965 if (!aff->v)
4966 return isl_aff_free(aff);
4968 pos += isl_local_space_offset(aff->ls, type);
4970 isl_int_init(v);
4971 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4972 aff->v->size, subs->v->size, v);
4973 isl_int_clear(v);
4975 return aff;
4978 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4979 * expressions in "maff".
4981 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4982 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4983 __isl_keep isl_aff *subs)
4985 int i;
4987 maff = isl_multi_aff_cow(maff);
4988 if (!maff || !subs)
4989 return isl_multi_aff_free(maff);
4991 if (type == isl_dim_in)
4992 type = isl_dim_set;
4994 for (i = 0; i < maff->n; ++i) {
4995 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4996 if (!maff->p[i])
4997 return isl_multi_aff_free(maff);
5000 return maff;
5003 /* Plug in "subs" for dimension "type", "pos" of "pma".
5005 * pma is of the form
5007 * A_i(v) -> M_i(v)
5009 * while subs is of the form
5011 * v' = B_j(v) -> S_j
5013 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5014 * has a contribution in the result, in particular
5016 * C_ij(S_j) -> M_i(S_j)
5018 * Note that plugging in S_j in C_ij may also result in an empty set
5019 * and this contribution should simply be discarded.
5021 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5022 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5023 __isl_keep isl_pw_aff *subs)
5025 int i, j, n;
5026 isl_pw_multi_aff *res;
5028 if (!pma || !subs)
5029 return isl_pw_multi_aff_free(pma);
5031 n = pma->n * subs->n;
5032 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5034 for (i = 0; i < pma->n; ++i) {
5035 for (j = 0; j < subs->n; ++j) {
5036 isl_set *common;
5037 isl_multi_aff *res_ij;
5038 int empty;
5040 common = isl_set_intersect(
5041 isl_set_copy(pma->p[i].set),
5042 isl_set_copy(subs->p[j].set));
5043 common = isl_set_substitute(common,
5044 type, pos, subs->p[j].aff);
5045 empty = isl_set_plain_is_empty(common);
5046 if (empty < 0 || empty) {
5047 isl_set_free(common);
5048 if (empty < 0)
5049 goto error;
5050 continue;
5053 res_ij = isl_multi_aff_substitute(
5054 isl_multi_aff_copy(pma->p[i].maff),
5055 type, pos, subs->p[j].aff);
5057 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5061 isl_pw_multi_aff_free(pma);
5062 return res;
5063 error:
5064 isl_pw_multi_aff_free(pma);
5065 isl_pw_multi_aff_free(res);
5066 return NULL;
5069 /* Compute the preimage of a range of dimensions in the affine expression "src"
5070 * under "ma" and put the result in "dst". The number of dimensions in "src"
5071 * that precede the range is given by "n_before". The number of dimensions
5072 * in the range is given by the number of output dimensions of "ma".
5073 * The number of dimensions that follow the range is given by "n_after".
5074 * If "has_denom" is set (to one),
5075 * then "src" and "dst" have an extra initial denominator.
5076 * "n_div_ma" is the number of existentials in "ma"
5077 * "n_div_bset" is the number of existentials in "src"
5078 * The resulting "dst" (which is assumed to have been allocated by
5079 * the caller) contains coefficients for both sets of existentials,
5080 * first those in "ma" and then those in "src".
5081 * f, c1, c2 and g are temporary objects that have been initialized
5082 * by the caller.
5084 * Let src represent the expression
5086 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5088 * and let ma represent the expressions
5090 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5092 * We start out with the following expression for dst:
5094 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5096 * with the multiplication factor f initially equal to 1
5097 * and f \sum_i b_i v_i kept separately.
5098 * For each x_i that we substitute, we multiply the numerator
5099 * (and denominator) of dst by c_1 = m_i and add the numerator
5100 * of the x_i expression multiplied by c_2 = f b_i,
5101 * after removing the common factors of c_1 and c_2.
5102 * The multiplication factor f also needs to be multiplied by c_1
5103 * for the next x_j, j > i.
5105 void isl_seq_preimage(isl_int *dst, isl_int *src,
5106 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5107 int n_div_ma, int n_div_bmap,
5108 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5110 int i;
5111 int n_param, n_in, n_out;
5112 int o_dst, o_src;
5114 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5115 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5116 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5118 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5119 o_dst = o_src = has_denom + 1 + n_param + n_before;
5120 isl_seq_clr(dst + o_dst, n_in);
5121 o_dst += n_in;
5122 o_src += n_out;
5123 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5124 o_dst += n_after;
5125 o_src += n_after;
5126 isl_seq_clr(dst + o_dst, n_div_ma);
5127 o_dst += n_div_ma;
5128 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5130 isl_int_set_si(f, 1);
5132 for (i = 0; i < n_out; ++i) {
5133 int offset = has_denom + 1 + n_param + n_before + i;
5135 if (isl_int_is_zero(src[offset]))
5136 continue;
5137 isl_int_set(c1, ma->p[i]->v->el[0]);
5138 isl_int_mul(c2, f, src[offset]);
5139 isl_int_gcd(g, c1, c2);
5140 isl_int_divexact(c1, c1, g);
5141 isl_int_divexact(c2, c2, g);
5143 isl_int_mul(f, f, c1);
5144 o_dst = has_denom;
5145 o_src = 1;
5146 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5147 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5148 o_dst += 1 + n_param;
5149 o_src += 1 + n_param;
5150 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5151 o_dst += n_before;
5152 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5153 c2, ma->p[i]->v->el + o_src, n_in);
5154 o_dst += n_in;
5155 o_src += n_in;
5156 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5157 o_dst += n_after;
5158 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5159 c2, ma->p[i]->v->el + o_src, n_div_ma);
5160 o_dst += n_div_ma;
5161 o_src += n_div_ma;
5162 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5163 if (has_denom)
5164 isl_int_mul(dst[0], dst[0], c1);
5168 /* Compute the pullback of "aff" by the function represented by "ma".
5169 * In other words, plug in "ma" in "aff". The result is an affine expression
5170 * defined over the domain space of "ma".
5172 * If "aff" is represented by
5174 * (a(p) + b x + c(divs))/d
5176 * and ma is represented by
5178 * x = D(p) + F(y) + G(divs')
5180 * then the result is
5182 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5184 * The divs in the local space of the input are similarly adjusted
5185 * through a call to isl_local_space_preimage_multi_aff.
5187 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5188 __isl_take isl_multi_aff *ma)
5190 isl_aff *res = NULL;
5191 isl_local_space *ls;
5192 int n_div_aff, n_div_ma;
5193 isl_int f, c1, c2, g;
5195 ma = isl_multi_aff_align_divs(ma);
5196 if (!aff || !ma)
5197 goto error;
5199 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5200 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5202 ls = isl_aff_get_domain_local_space(aff);
5203 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5204 res = isl_aff_alloc(ls);
5205 if (!res)
5206 goto error;
5208 isl_int_init(f);
5209 isl_int_init(c1);
5210 isl_int_init(c2);
5211 isl_int_init(g);
5213 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5214 f, c1, c2, g, 1);
5216 isl_int_clear(f);
5217 isl_int_clear(c1);
5218 isl_int_clear(c2);
5219 isl_int_clear(g);
5221 isl_aff_free(aff);
5222 isl_multi_aff_free(ma);
5223 res = isl_aff_normalize(res);
5224 return res;
5225 error:
5226 isl_aff_free(aff);
5227 isl_multi_aff_free(ma);
5228 isl_aff_free(res);
5229 return NULL;
5232 /* Compute the pullback of "aff1" by the function represented by "aff2".
5233 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5234 * defined over the domain space of "aff1".
5236 * The domain of "aff1" should match the range of "aff2", which means
5237 * that it should be single-dimensional.
5239 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5240 __isl_take isl_aff *aff2)
5242 isl_multi_aff *ma;
5244 ma = isl_multi_aff_from_aff(aff2);
5245 return isl_aff_pullback_multi_aff(aff1, ma);
5248 /* Compute the pullback of "ma1" by the function represented by "ma2".
5249 * In other words, plug in "ma2" in "ma1".
5251 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5253 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5254 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5256 int i;
5257 isl_space *space = NULL;
5259 ma2 = isl_multi_aff_align_divs(ma2);
5260 ma1 = isl_multi_aff_cow(ma1);
5261 if (!ma1 || !ma2)
5262 goto error;
5264 space = isl_space_join(isl_multi_aff_get_space(ma2),
5265 isl_multi_aff_get_space(ma1));
5267 for (i = 0; i < ma1->n; ++i) {
5268 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5269 isl_multi_aff_copy(ma2));
5270 if (!ma1->p[i])
5271 goto error;
5274 ma1 = isl_multi_aff_reset_space(ma1, space);
5275 isl_multi_aff_free(ma2);
5276 return ma1;
5277 error:
5278 isl_space_free(space);
5279 isl_multi_aff_free(ma2);
5280 isl_multi_aff_free(ma1);
5281 return NULL;
5284 /* Compute the pullback of "ma1" by the function represented by "ma2".
5285 * In other words, plug in "ma2" in "ma1".
5287 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5288 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5290 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5291 &isl_multi_aff_pullback_multi_aff_aligned);
5294 /* Extend the local space of "dst" to include the divs
5295 * in the local space of "src".
5297 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5298 __isl_keep isl_aff *src)
5300 isl_ctx *ctx;
5301 int *exp1 = NULL;
5302 int *exp2 = NULL;
5303 isl_mat *div;
5305 if (!src || !dst)
5306 return isl_aff_free(dst);
5308 ctx = isl_aff_get_ctx(src);
5309 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5310 isl_die(ctx, isl_error_invalid,
5311 "spaces don't match", goto error);
5313 if (src->ls->div->n_row == 0)
5314 return dst;
5316 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5317 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5318 if (!exp1 || (dst->ls->div->n_row && !exp2))
5319 goto error;
5321 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5322 dst = isl_aff_expand_divs(dst, div, exp2);
5323 free(exp1);
5324 free(exp2);
5326 return dst;
5327 error:
5328 free(exp1);
5329 free(exp2);
5330 return isl_aff_free(dst);
5333 /* Adjust the local spaces of the affine expressions in "maff"
5334 * such that they all have the save divs.
5336 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5337 __isl_take isl_multi_aff *maff)
5339 int i;
5341 if (!maff)
5342 return NULL;
5343 if (maff->n == 0)
5344 return maff;
5345 maff = isl_multi_aff_cow(maff);
5346 if (!maff)
5347 return NULL;
5349 for (i = 1; i < maff->n; ++i)
5350 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5351 for (i = 1; i < maff->n; ++i) {
5352 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5353 if (!maff->p[i])
5354 return isl_multi_aff_free(maff);
5357 return maff;
5360 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5362 aff = isl_aff_cow(aff);
5363 if (!aff)
5364 return NULL;
5366 aff->ls = isl_local_space_lift(aff->ls);
5367 if (!aff->ls)
5368 return isl_aff_free(aff);
5370 return aff;
5373 /* Lift "maff" to a space with extra dimensions such that the result
5374 * has no more existentially quantified variables.
5375 * If "ls" is not NULL, then *ls is assigned the local space that lies
5376 * at the basis of the lifting applied to "maff".
5378 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5379 __isl_give isl_local_space **ls)
5381 int i;
5382 isl_space *space;
5383 unsigned n_div;
5385 if (ls)
5386 *ls = NULL;
5388 if (!maff)
5389 return NULL;
5391 if (maff->n == 0) {
5392 if (ls) {
5393 isl_space *space = isl_multi_aff_get_domain_space(maff);
5394 *ls = isl_local_space_from_space(space);
5395 if (!*ls)
5396 return isl_multi_aff_free(maff);
5398 return maff;
5401 maff = isl_multi_aff_cow(maff);
5402 maff = isl_multi_aff_align_divs(maff);
5403 if (!maff)
5404 return NULL;
5406 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5407 space = isl_multi_aff_get_space(maff);
5408 space = isl_space_lift(isl_space_domain(space), n_div);
5409 space = isl_space_extend_domain_with_range(space,
5410 isl_multi_aff_get_space(maff));
5411 if (!space)
5412 return isl_multi_aff_free(maff);
5413 isl_space_free(maff->space);
5414 maff->space = space;
5416 if (ls) {
5417 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5418 if (!*ls)
5419 return isl_multi_aff_free(maff);
5422 for (i = 0; i < maff->n; ++i) {
5423 maff->p[i] = isl_aff_lift(maff->p[i]);
5424 if (!maff->p[i])
5425 goto error;
5428 return maff;
5429 error:
5430 if (ls)
5431 isl_local_space_free(*ls);
5432 return isl_multi_aff_free(maff);
5436 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5438 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5439 __isl_keep isl_pw_multi_aff *pma, int pos)
5441 int i;
5442 int n_out;
5443 isl_space *space;
5444 isl_pw_aff *pa;
5446 if (!pma)
5447 return NULL;
5449 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5450 if (pos < 0 || pos >= n_out)
5451 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5452 "index out of bounds", return NULL);
5454 space = isl_pw_multi_aff_get_space(pma);
5455 space = isl_space_drop_dims(space, isl_dim_out,
5456 pos + 1, n_out - pos - 1);
5457 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5459 pa = isl_pw_aff_alloc_size(space, pma->n);
5460 for (i = 0; i < pma->n; ++i) {
5461 isl_aff *aff;
5462 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5463 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5466 return pa;
5469 /* Return an isl_pw_multi_aff with the given "set" as domain and
5470 * an unnamed zero-dimensional range.
5472 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5473 __isl_take isl_set *set)
5475 isl_multi_aff *ma;
5476 isl_space *space;
5478 space = isl_set_get_space(set);
5479 space = isl_space_from_domain(space);
5480 ma = isl_multi_aff_zero(space);
5481 return isl_pw_multi_aff_alloc(set, ma);
5484 /* Add an isl_pw_multi_aff with the given "set" as domain and
5485 * an unnamed zero-dimensional range to *user.
5487 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5489 isl_union_pw_multi_aff **upma = user;
5490 isl_pw_multi_aff *pma;
5492 pma = isl_pw_multi_aff_from_domain(set);
5493 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5495 return 0;
5498 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5499 * an unnamed zero-dimensional range.
5501 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5502 __isl_take isl_union_set *uset)
5504 isl_space *space;
5505 isl_union_pw_multi_aff *upma;
5507 if (!uset)
5508 return NULL;
5510 space = isl_union_set_get_space(uset);
5511 upma = isl_union_pw_multi_aff_empty(space);
5513 if (isl_union_set_foreach_set(uset,
5514 &add_pw_multi_aff_from_domain, &upma) < 0)
5515 goto error;
5517 isl_union_set_free(uset);
5518 return upma;
5519 error:
5520 isl_union_set_free(uset);
5521 isl_union_pw_multi_aff_free(upma);
5522 return NULL;
5525 /* Convert "pma" to an isl_map and add it to *umap.
5527 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5529 isl_union_map **umap = user;
5530 isl_map *map;
5532 map = isl_map_from_pw_multi_aff(pma);
5533 *umap = isl_union_map_add_map(*umap, map);
5535 return 0;
5538 /* Construct a union map mapping the domain of the union
5539 * piecewise multi-affine expression to its range, with each dimension
5540 * in the range equated to the corresponding affine expression on its cell.
5542 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5543 __isl_take isl_union_pw_multi_aff *upma)
5545 isl_space *space;
5546 isl_union_map *umap;
5548 if (!upma)
5549 return NULL;
5551 space = isl_union_pw_multi_aff_get_space(upma);
5552 umap = isl_union_map_empty(space);
5554 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5555 &map_from_pw_multi_aff, &umap) < 0)
5556 goto error;
5558 isl_union_pw_multi_aff_free(upma);
5559 return umap;
5560 error:
5561 isl_union_pw_multi_aff_free(upma);
5562 isl_union_map_free(umap);
5563 return NULL;
5566 /* Local data for bin_entry and the callback "fn".
5568 struct isl_union_pw_multi_aff_bin_data {
5569 isl_union_pw_multi_aff *upma2;
5570 isl_union_pw_multi_aff *res;
5571 isl_pw_multi_aff *pma;
5572 int (*fn)(void **entry, void *user);
5575 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5576 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5578 static int bin_entry(void **entry, void *user)
5580 struct isl_union_pw_multi_aff_bin_data *data = user;
5581 isl_pw_multi_aff *pma = *entry;
5583 data->pma = pma;
5584 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5585 data->fn, data) < 0)
5586 return -1;
5588 return 0;
5591 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5592 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5593 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5594 * as *entry. The callback should adjust data->res if desired.
5596 static __isl_give isl_union_pw_multi_aff *bin_op(
5597 __isl_take isl_union_pw_multi_aff *upma1,
5598 __isl_take isl_union_pw_multi_aff *upma2,
5599 int (*fn)(void **entry, void *user))
5601 isl_space *space;
5602 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5604 space = isl_union_pw_multi_aff_get_space(upma2);
5605 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5606 space = isl_union_pw_multi_aff_get_space(upma1);
5607 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5609 if (!upma1 || !upma2)
5610 goto error;
5612 data.upma2 = upma2;
5613 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5614 upma1->table.n);
5615 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5616 &bin_entry, &data) < 0)
5617 goto error;
5619 isl_union_pw_multi_aff_free(upma1);
5620 isl_union_pw_multi_aff_free(upma2);
5621 return data.res;
5622 error:
5623 isl_union_pw_multi_aff_free(upma1);
5624 isl_union_pw_multi_aff_free(upma2);
5625 isl_union_pw_multi_aff_free(data.res);
5626 return NULL;
5629 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5630 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5632 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5633 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5635 isl_space *space;
5637 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5638 isl_pw_multi_aff_get_space(pma2));
5639 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5640 &isl_multi_aff_range_product);
5643 /* Given two isl_pw_multi_affs A -> B and C -> D,
5644 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5646 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5647 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5649 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5650 &pw_multi_aff_range_product);
5653 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5654 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5656 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5657 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5659 isl_space *space;
5661 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5662 isl_pw_multi_aff_get_space(pma2));
5663 space = isl_space_flatten_range(space);
5664 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5665 &isl_multi_aff_flat_range_product);
5668 /* Given two isl_pw_multi_affs A -> B and C -> D,
5669 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5671 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5672 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5674 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5675 &pw_multi_aff_flat_range_product);
5678 /* If data->pma and *entry have the same domain space, then compute
5679 * their flat range product and the result to data->res.
5681 static int flat_range_product_entry(void **entry, void *user)
5683 struct isl_union_pw_multi_aff_bin_data *data = user;
5684 isl_pw_multi_aff *pma2 = *entry;
5686 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5687 pma2->dim, isl_dim_in))
5688 return 0;
5690 pma2 = isl_pw_multi_aff_flat_range_product(
5691 isl_pw_multi_aff_copy(data->pma),
5692 isl_pw_multi_aff_copy(pma2));
5694 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5696 return 0;
5699 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5700 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5702 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5703 __isl_take isl_union_pw_multi_aff *upma1,
5704 __isl_take isl_union_pw_multi_aff *upma2)
5706 return bin_op(upma1, upma2, &flat_range_product_entry);
5709 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5710 * The parameters are assumed to have been aligned.
5712 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5713 * except that it works on two different isl_pw_* types.
5715 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5716 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5717 __isl_take isl_pw_aff *pa)
5719 int i, j, n;
5720 isl_pw_multi_aff *res = NULL;
5722 if (!pma || !pa)
5723 goto error;
5725 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5726 pa->dim, isl_dim_in))
5727 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5728 "domains don't match", goto error);
5729 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5730 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5731 "index out of bounds", goto error);
5733 n = pma->n * pa->n;
5734 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5736 for (i = 0; i < pma->n; ++i) {
5737 for (j = 0; j < pa->n; ++j) {
5738 isl_set *common;
5739 isl_multi_aff *res_ij;
5740 int empty;
5742 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5743 isl_set_copy(pa->p[j].set));
5744 empty = isl_set_plain_is_empty(common);
5745 if (empty < 0 || empty) {
5746 isl_set_free(common);
5747 if (empty < 0)
5748 goto error;
5749 continue;
5752 res_ij = isl_multi_aff_set_aff(
5753 isl_multi_aff_copy(pma->p[i].maff), pos,
5754 isl_aff_copy(pa->p[j].aff));
5755 res_ij = isl_multi_aff_gist(res_ij,
5756 isl_set_copy(common));
5758 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5762 isl_pw_multi_aff_free(pma);
5763 isl_pw_aff_free(pa);
5764 return res;
5765 error:
5766 isl_pw_multi_aff_free(pma);
5767 isl_pw_aff_free(pa);
5768 return isl_pw_multi_aff_free(res);
5771 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5773 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5774 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5775 __isl_take isl_pw_aff *pa)
5777 if (!pma || !pa)
5778 goto error;
5779 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5780 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5781 if (!isl_space_has_named_params(pma->dim) ||
5782 !isl_space_has_named_params(pa->dim))
5783 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5784 "unaligned unnamed parameters", goto error);
5785 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5786 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5787 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5788 error:
5789 isl_pw_multi_aff_free(pma);
5790 isl_pw_aff_free(pa);
5791 return NULL;
5794 /* Do the parameters of "pa" match those of "space"?
5796 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5797 __isl_keep isl_space *space)
5799 isl_space *pa_space;
5800 int match;
5802 if (!pa || !space)
5803 return -1;
5805 pa_space = isl_pw_aff_get_space(pa);
5807 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5809 isl_space_free(pa_space);
5810 return match;
5813 /* Check that the domain space of "pa" matches "space".
5815 * Return 0 on success and -1 on error.
5817 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5818 __isl_keep isl_space *space)
5820 isl_space *pa_space;
5821 int match;
5823 if (!pa || !space)
5824 return -1;
5826 pa_space = isl_pw_aff_get_space(pa);
5828 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5829 if (match < 0)
5830 goto error;
5831 if (!match)
5832 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5833 "parameters don't match", goto error);
5834 match = isl_space_tuple_is_equal(space, isl_dim_in,
5835 pa_space, isl_dim_in);
5836 if (match < 0)
5837 goto error;
5838 if (!match)
5839 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5840 "domains don't match", goto error);
5841 isl_space_free(pa_space);
5842 return 0;
5843 error:
5844 isl_space_free(pa_space);
5845 return -1;
5848 #undef BASE
5849 #define BASE pw_aff
5851 #include <isl_multi_templ.c>
5853 /* Scale the elements of "pma" by the corresponding elements of "mv".
5855 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5856 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5858 int i;
5860 pma = isl_pw_multi_aff_cow(pma);
5861 if (!pma || !mv)
5862 goto error;
5863 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5864 mv->space, isl_dim_set))
5865 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5866 "spaces don't match", goto error);
5867 if (!isl_space_match(pma->dim, isl_dim_param,
5868 mv->space, isl_dim_param)) {
5869 pma = isl_pw_multi_aff_align_params(pma,
5870 isl_multi_val_get_space(mv));
5871 mv = isl_multi_val_align_params(mv,
5872 isl_pw_multi_aff_get_space(pma));
5873 if (!pma || !mv)
5874 goto error;
5877 for (i = 0; i < pma->n; ++i) {
5878 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5879 isl_multi_val_copy(mv));
5880 if (!pma->p[i].maff)
5881 goto error;
5884 isl_multi_val_free(mv);
5885 return pma;
5886 error:
5887 isl_multi_val_free(mv);
5888 isl_pw_multi_aff_free(pma);
5889 return NULL;
5892 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5893 * mv contains the mv argument.
5894 * res collects the results.
5896 struct isl_union_pw_multi_aff_scale_multi_val_data {
5897 isl_multi_val *mv;
5898 isl_union_pw_multi_aff *res;
5901 /* This function is called for each entry of an isl_union_pw_multi_aff.
5902 * If the space of the entry matches that of data->mv,
5903 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5904 * to data->res.
5906 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5908 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5909 isl_pw_multi_aff *pma = *entry;
5911 if (!pma)
5912 return -1;
5913 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5914 data->mv->space, isl_dim_set))
5915 return 0;
5917 pma = isl_pw_multi_aff_copy(pma);
5918 pma = isl_pw_multi_aff_scale_multi_val(pma,
5919 isl_multi_val_copy(data->mv));
5920 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5921 if (!data->res)
5922 return -1;
5924 return 0;
5927 /* Scale the elements of "upma" by the corresponding elements of "mv",
5928 * for those entries that match the space of "mv".
5930 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5931 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5933 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5935 upma = isl_union_pw_multi_aff_align_params(upma,
5936 isl_multi_val_get_space(mv));
5937 mv = isl_multi_val_align_params(mv,
5938 isl_union_pw_multi_aff_get_space(upma));
5939 if (!upma || !mv)
5940 goto error;
5942 data.mv = mv;
5943 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
5944 upma->table.n);
5945 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
5946 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5947 goto error;
5949 isl_multi_val_free(mv);
5950 isl_union_pw_multi_aff_free(upma);
5951 return data.res;
5952 error:
5953 isl_multi_val_free(mv);
5954 isl_union_pw_multi_aff_free(upma);
5955 return NULL;
5958 /* Construct and return a piecewise multi affine expression
5959 * in the given space with value zero in each of the output dimensions and
5960 * a universe domain.
5962 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5964 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5967 /* Construct and return a piecewise multi affine expression
5968 * that is equal to the given piecewise affine expression.
5970 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5971 __isl_take isl_pw_aff *pa)
5973 int i;
5974 isl_space *space;
5975 isl_pw_multi_aff *pma;
5977 if (!pa)
5978 return NULL;
5980 space = isl_pw_aff_get_space(pa);
5981 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5983 for (i = 0; i < pa->n; ++i) {
5984 isl_set *set;
5985 isl_multi_aff *ma;
5987 set = isl_set_copy(pa->p[i].set);
5988 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
5989 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
5992 isl_pw_aff_free(pa);
5993 return pma;
5996 /* Construct a set or map mapping the shared (parameter) domain
5997 * of the piecewise affine expressions to the range of "mpa"
5998 * with each dimension in the range equated to the
5999 * corresponding piecewise affine expression.
6001 static __isl_give isl_map *map_from_multi_pw_aff(
6002 __isl_take isl_multi_pw_aff *mpa)
6004 int i;
6005 isl_space *space;
6006 isl_map *map;
6008 if (!mpa)
6009 return NULL;
6011 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6012 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6013 "invalid space", goto error);
6015 space = isl_multi_pw_aff_get_domain_space(mpa);
6016 map = isl_map_universe(isl_space_from_domain(space));
6018 for (i = 0; i < mpa->n; ++i) {
6019 isl_pw_aff *pa;
6020 isl_map *map_i;
6022 pa = isl_pw_aff_copy(mpa->p[i]);
6023 map_i = map_from_pw_aff(pa);
6025 map = isl_map_flat_range_product(map, map_i);
6028 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6030 isl_multi_pw_aff_free(mpa);
6031 return map;
6032 error:
6033 isl_multi_pw_aff_free(mpa);
6034 return NULL;
6037 /* Construct a map mapping the shared domain
6038 * of the piecewise affine expressions to the range of "mpa"
6039 * with each dimension in the range equated to the
6040 * corresponding piecewise affine expression.
6042 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6044 if (!mpa)
6045 return NULL;
6046 if (isl_space_is_set(mpa->space))
6047 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6048 "space of input is not a map", goto error);
6050 return map_from_multi_pw_aff(mpa);
6051 error:
6052 isl_multi_pw_aff_free(mpa);
6053 return NULL;
6056 /* Construct a set mapping the shared parameter domain
6057 * of the piecewise affine expressions to the space of "mpa"
6058 * with each dimension in the range equated to the
6059 * corresponding piecewise affine expression.
6061 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6063 if (!mpa)
6064 return NULL;
6065 if (!isl_space_is_set(mpa->space))
6066 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6067 "space of input is not a set", goto error);
6069 return map_from_multi_pw_aff(mpa);
6070 error:
6071 isl_multi_pw_aff_free(mpa);
6072 return NULL;
6075 /* Construct and return a piecewise multi affine expression
6076 * that is equal to the given multi piecewise affine expression
6077 * on the shared domain of the piecewise affine expressions.
6079 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6080 __isl_take isl_multi_pw_aff *mpa)
6082 int i;
6083 isl_space *space;
6084 isl_pw_aff *pa;
6085 isl_pw_multi_aff *pma;
6087 if (!mpa)
6088 return NULL;
6090 space = isl_multi_pw_aff_get_space(mpa);
6092 if (mpa->n == 0) {
6093 isl_multi_pw_aff_free(mpa);
6094 return isl_pw_multi_aff_zero(space);
6097 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6098 pma = isl_pw_multi_aff_from_pw_aff(pa);
6100 for (i = 1; i < mpa->n; ++i) {
6101 isl_pw_multi_aff *pma_i;
6103 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6104 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6105 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6108 pma = isl_pw_multi_aff_reset_space(pma, space);
6110 isl_multi_pw_aff_free(mpa);
6111 return pma;
6114 /* Construct and return a multi piecewise affine expression
6115 * that is equal to the given multi affine expression.
6117 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6118 __isl_take isl_multi_aff *ma)
6120 int i, n;
6121 isl_multi_pw_aff *mpa;
6123 if (!ma)
6124 return NULL;
6126 n = isl_multi_aff_dim(ma, isl_dim_out);
6127 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6129 for (i = 0; i < n; ++i) {
6130 isl_pw_aff *pa;
6132 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6133 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6136 isl_multi_aff_free(ma);
6137 return mpa;
6140 /* Construct and return a multi piecewise affine expression
6141 * that is equal to the given piecewise multi affine expression.
6143 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6144 __isl_take isl_pw_multi_aff *pma)
6146 int i, n;
6147 isl_space *space;
6148 isl_multi_pw_aff *mpa;
6150 if (!pma)
6151 return NULL;
6153 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6154 space = isl_pw_multi_aff_get_space(pma);
6155 mpa = isl_multi_pw_aff_alloc(space);
6157 for (i = 0; i < n; ++i) {
6158 isl_pw_aff *pa;
6160 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6161 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6164 isl_pw_multi_aff_free(pma);
6165 return mpa;
6168 /* Do "pa1" and "pa2" represent the same function?
6170 * We first check if they are obviously equal.
6171 * If not, we convert them to maps and check if those are equal.
6173 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6175 int equal;
6176 isl_map *map1, *map2;
6178 if (!pa1 || !pa2)
6179 return -1;
6181 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6182 if (equal < 0 || equal)
6183 return equal;
6185 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6186 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6187 equal = isl_map_is_equal(map1, map2);
6188 isl_map_free(map1);
6189 isl_map_free(map2);
6191 return equal;
6194 /* Do "mpa1" and "mpa2" represent the same function?
6196 * Note that we cannot convert the entire isl_multi_pw_aff
6197 * to a map because the domains of the piecewise affine expressions
6198 * may not be the same.
6200 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6201 __isl_keep isl_multi_pw_aff *mpa2)
6203 int i;
6204 int equal;
6206 if (!mpa1 || !mpa2)
6207 return -1;
6209 if (!isl_space_match(mpa1->space, isl_dim_param,
6210 mpa2->space, isl_dim_param)) {
6211 if (!isl_space_has_named_params(mpa1->space))
6212 return 0;
6213 if (!isl_space_has_named_params(mpa2->space))
6214 return 0;
6215 mpa1 = isl_multi_pw_aff_copy(mpa1);
6216 mpa2 = isl_multi_pw_aff_copy(mpa2);
6217 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6218 isl_multi_pw_aff_get_space(mpa2));
6219 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6220 isl_multi_pw_aff_get_space(mpa1));
6221 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6222 isl_multi_pw_aff_free(mpa1);
6223 isl_multi_pw_aff_free(mpa2);
6224 return equal;
6227 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6228 if (equal < 0 || !equal)
6229 return equal;
6231 for (i = 0; i < mpa1->n; ++i) {
6232 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6233 if (equal < 0 || !equal)
6234 return equal;
6237 return 1;
6240 /* Coalesce the elements of "mpa".
6242 * Note that such coalescing does not change the meaning of "mpa"
6243 * so there is no need to cow. We do need to be careful not to
6244 * destroy any other copies of "mpa" in case of failure.
6246 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6247 __isl_take isl_multi_pw_aff *mpa)
6249 int i;
6251 if (!mpa)
6252 return NULL;
6254 for (i = 0; i < mpa->n; ++i) {
6255 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6256 pa = isl_pw_aff_coalesce(pa);
6257 if (!pa)
6258 return isl_multi_pw_aff_free(mpa);
6259 isl_pw_aff_free(mpa->p[i]);
6260 mpa->p[i] = pa;
6263 return mpa;
6266 /* Compute the pullback of "mpa" by the function represented by "ma".
6267 * In other words, plug in "ma" in "mpa".
6269 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6271 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6272 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6274 int i;
6275 isl_space *space = NULL;
6277 mpa = isl_multi_pw_aff_cow(mpa);
6278 if (!mpa || !ma)
6279 goto error;
6281 space = isl_space_join(isl_multi_aff_get_space(ma),
6282 isl_multi_pw_aff_get_space(mpa));
6283 if (!space)
6284 goto error;
6286 for (i = 0; i < mpa->n; ++i) {
6287 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6288 isl_multi_aff_copy(ma));
6289 if (!mpa->p[i])
6290 goto error;
6293 isl_multi_aff_free(ma);
6294 isl_space_free(mpa->space);
6295 mpa->space = space;
6296 return mpa;
6297 error:
6298 isl_space_free(space);
6299 isl_multi_pw_aff_free(mpa);
6300 isl_multi_aff_free(ma);
6301 return NULL;
6304 /* Compute the pullback of "mpa" by the function represented by "ma".
6305 * In other words, plug in "ma" in "mpa".
6307 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6308 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6310 if (!mpa || !ma)
6311 goto error;
6312 if (isl_space_match(mpa->space, isl_dim_param,
6313 ma->space, isl_dim_param))
6314 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6315 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6316 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6317 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6318 error:
6319 isl_multi_pw_aff_free(mpa);
6320 isl_multi_aff_free(ma);
6321 return NULL;
6324 /* Compute the pullback of "mpa" by the function represented by "pma".
6325 * In other words, plug in "pma" in "mpa".
6327 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6329 static __isl_give isl_multi_pw_aff *
6330 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6331 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6333 int i;
6334 isl_space *space = NULL;
6336 mpa = isl_multi_pw_aff_cow(mpa);
6337 if (!mpa || !pma)
6338 goto error;
6340 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6341 isl_multi_pw_aff_get_space(mpa));
6343 for (i = 0; i < mpa->n; ++i) {
6344 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6345 isl_pw_multi_aff_copy(pma));
6346 if (!mpa->p[i])
6347 goto error;
6350 isl_pw_multi_aff_free(pma);
6351 isl_space_free(mpa->space);
6352 mpa->space = space;
6353 return mpa;
6354 error:
6355 isl_space_free(space);
6356 isl_multi_pw_aff_free(mpa);
6357 isl_pw_multi_aff_free(pma);
6358 return NULL;
6361 /* Compute the pullback of "mpa" by the function represented by "pma".
6362 * In other words, plug in "pma" in "mpa".
6364 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6365 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6367 if (!mpa || !pma)
6368 goto error;
6369 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6370 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6371 mpa = isl_multi_pw_aff_align_params(mpa,
6372 isl_pw_multi_aff_get_space(pma));
6373 pma = isl_pw_multi_aff_align_params(pma,
6374 isl_multi_pw_aff_get_space(mpa));
6375 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6376 error:
6377 isl_multi_pw_aff_free(mpa);
6378 isl_pw_multi_aff_free(pma);
6379 return NULL;
6382 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6383 * with the domain of "aff". The domain of the result is the same
6384 * as that of "mpa".
6385 * "mpa" and "aff" are assumed to have been aligned.
6387 * We first extract the parametric constant from "aff", defined
6388 * over the correct domain.
6389 * Then we add the appropriate combinations of the members of "mpa".
6390 * Finally, we add the integer divisions through recursive calls.
6392 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6393 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6395 int i, n_param, n_in, n_div;
6396 isl_space *space;
6397 isl_val *v;
6398 isl_pw_aff *pa;
6399 isl_aff *tmp;
6401 n_param = isl_aff_dim(aff, isl_dim_param);
6402 n_in = isl_aff_dim(aff, isl_dim_in);
6403 n_div = isl_aff_dim(aff, isl_dim_div);
6405 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6406 tmp = isl_aff_copy(aff);
6407 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6408 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6409 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6410 isl_space_dim(space, isl_dim_set));
6411 tmp = isl_aff_reset_domain_space(tmp, space);
6412 pa = isl_pw_aff_from_aff(tmp);
6414 for (i = 0; i < n_in; ++i) {
6415 isl_pw_aff *pa_i;
6417 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6418 continue;
6419 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6420 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6421 pa_i = isl_pw_aff_scale_val(pa_i, v);
6422 pa = isl_pw_aff_add(pa, pa_i);
6425 for (i = 0; i < n_div; ++i) {
6426 isl_aff *div;
6427 isl_pw_aff *pa_i;
6429 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6430 continue;
6431 div = isl_aff_get_div(aff, i);
6432 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6433 isl_multi_pw_aff_copy(mpa), div);
6434 pa_i = isl_pw_aff_floor(pa_i);
6435 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6436 pa_i = isl_pw_aff_scale_val(pa_i, v);
6437 pa = isl_pw_aff_add(pa, pa_i);
6440 isl_multi_pw_aff_free(mpa);
6441 isl_aff_free(aff);
6443 return pa;
6446 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6447 * with the domain of "aff". The domain of the result is the same
6448 * as that of "mpa".
6450 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6451 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6453 if (!aff || !mpa)
6454 goto error;
6455 if (isl_space_match(aff->ls->dim, isl_dim_param,
6456 mpa->space, isl_dim_param))
6457 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6459 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6460 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6462 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6463 error:
6464 isl_aff_free(aff);
6465 isl_multi_pw_aff_free(mpa);
6466 return NULL;
6469 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6470 * with the domain of "pa". The domain of the result is the same
6471 * as that of "mpa".
6472 * "mpa" and "pa" are assumed to have been aligned.
6474 * We consider each piece in turn. Note that the domains of the
6475 * pieces are assumed to be disjoint and they remain disjoint
6476 * after taking the preimage (over the same function).
6478 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6479 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6481 isl_space *space;
6482 isl_pw_aff *res;
6483 int i;
6485 if (!mpa || !pa)
6486 goto error;
6488 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6489 isl_pw_aff_get_space(pa));
6490 res = isl_pw_aff_empty(space);
6492 for (i = 0; i < pa->n; ++i) {
6493 isl_pw_aff *pa_i;
6494 isl_set *domain;
6496 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6497 isl_multi_pw_aff_copy(mpa),
6498 isl_aff_copy(pa->p[i].aff));
6499 domain = isl_set_copy(pa->p[i].set);
6500 domain = isl_set_preimage_multi_pw_aff(domain,
6501 isl_multi_pw_aff_copy(mpa));
6502 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6503 res = isl_pw_aff_add_disjoint(res, pa_i);
6506 isl_pw_aff_free(pa);
6507 isl_multi_pw_aff_free(mpa);
6508 return res;
6509 error:
6510 isl_pw_aff_free(pa);
6511 isl_multi_pw_aff_free(mpa);
6512 return NULL;
6515 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6516 * with the domain of "pa". The domain of the result is the same
6517 * as that of "mpa".
6519 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6520 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6522 if (!pa || !mpa)
6523 goto error;
6524 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6525 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6527 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6528 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6530 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6531 error:
6532 isl_pw_aff_free(pa);
6533 isl_multi_pw_aff_free(mpa);
6534 return NULL;
6537 /* Compute the pullback of "pa" by the function represented by "mpa".
6538 * In other words, plug in "mpa" in "pa".
6539 * "pa" and "mpa" are assumed to have been aligned.
6541 * The pullback is computed by applying "pa" to "mpa".
6543 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6544 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6546 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6549 /* Compute the pullback of "pa" by the function represented by "mpa".
6550 * In other words, plug in "mpa" in "pa".
6552 * The pullback is computed by applying "pa" to "mpa".
6554 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6555 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6557 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6560 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6561 * In other words, plug in "mpa2" in "mpa1".
6563 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6565 * We pullback each member of "mpa1" in turn.
6567 static __isl_give isl_multi_pw_aff *
6568 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6569 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6571 int i;
6572 isl_space *space = NULL;
6574 mpa1 = isl_multi_pw_aff_cow(mpa1);
6575 if (!mpa1 || !mpa2)
6576 goto error;
6578 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6579 isl_multi_pw_aff_get_space(mpa1));
6581 for (i = 0; i < mpa1->n; ++i) {
6582 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6583 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6584 if (!mpa1->p[i])
6585 goto error;
6588 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6590 isl_multi_pw_aff_free(mpa2);
6591 return mpa1;
6592 error:
6593 isl_space_free(space);
6594 isl_multi_pw_aff_free(mpa1);
6595 isl_multi_pw_aff_free(mpa2);
6596 return NULL;
6599 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6600 * In other words, plug in "mpa2" in "mpa1".
6602 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6603 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6605 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6606 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6609 /* Compare two isl_affs.
6611 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6612 * than "aff2" and 0 if they are equal.
6614 * The order is fairly arbitrary. We do consider expressions that only involve
6615 * earlier dimensions as "smaller".
6617 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6619 int cmp;
6620 int last1, last2;
6622 if (aff1 == aff2)
6623 return 0;
6625 if (!aff1)
6626 return -1;
6627 if (!aff2)
6628 return 1;
6630 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6631 if (cmp != 0)
6632 return cmp;
6634 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6635 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6636 if (last1 != last2)
6637 return last1 - last2;
6639 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6642 /* Compare two isl_pw_affs.
6644 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6645 * than "pa2" and 0 if they are equal.
6647 * The order is fairly arbitrary. We do consider expressions that only involve
6648 * earlier dimensions as "smaller".
6650 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6651 __isl_keep isl_pw_aff *pa2)
6653 int i;
6654 int cmp;
6656 if (pa1 == pa2)
6657 return 0;
6659 if (!pa1)
6660 return -1;
6661 if (!pa2)
6662 return 1;
6664 cmp = isl_space_cmp(pa1->dim, pa2->dim);
6665 if (cmp != 0)
6666 return cmp;
6668 if (pa1->n != pa2->n)
6669 return pa1->n - pa2->n;
6671 for (i = 0; i < pa1->n; ++i) {
6672 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
6673 if (cmp != 0)
6674 return cmp;
6675 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
6676 if (cmp != 0)
6677 return cmp;
6680 return 0;
6683 /* Return a piecewise affine expression that is equal to "v" on "domain".
6685 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
6686 __isl_take isl_val *v)
6688 isl_space *space;
6689 isl_local_space *ls;
6690 isl_aff *aff;
6692 space = isl_set_get_space(domain);
6693 ls = isl_local_space_from_space(space);
6694 aff = isl_aff_val_on_domain(ls, v);
6696 return isl_pw_aff_alloc(domain, aff);
6699 /* Return a multi affine expression that is equal to "mv" on domain
6700 * space "space".
6702 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
6703 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
6705 int i, n;
6706 isl_space *space2;
6707 isl_local_space *ls;
6708 isl_multi_aff *ma;
6710 if (!space || !mv)
6711 goto error;
6713 n = isl_multi_val_dim(mv, isl_dim_set);
6714 space2 = isl_multi_val_get_space(mv);
6715 space2 = isl_space_align_params(space2, isl_space_copy(space));
6716 space = isl_space_align_params(space, isl_space_copy(space2));
6717 space = isl_space_map_from_domain_and_range(space, space2);
6718 ma = isl_multi_aff_alloc(isl_space_copy(space));
6719 ls = isl_local_space_from_space(isl_space_domain(space));
6720 for (i = 0; i < n; ++i) {
6721 isl_val *v;
6722 isl_aff *aff;
6724 v = isl_multi_val_get_val(mv, i);
6725 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
6726 ma = isl_multi_aff_set_aff(ma, i, aff);
6728 isl_local_space_free(ls);
6730 isl_multi_val_free(mv);
6731 return ma;
6732 error:
6733 isl_space_free(space);
6734 isl_multi_val_free(mv);
6735 return NULL;
6738 /* Return a piecewise multi-affine expression
6739 * that is equal to "mv" on "domain".
6741 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
6742 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
6744 isl_space *space;
6745 isl_multi_aff *ma;
6747 space = isl_set_get_space(domain);
6748 ma = isl_multi_aff_multi_val_on_space(space, mv);
6750 return isl_pw_multi_aff_alloc(domain, ma);
6753 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6754 * mv is the value that should be attained on each domain set
6755 * res collects the results
6757 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
6758 isl_multi_val *mv;
6759 isl_union_pw_multi_aff *res;
6762 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6763 * and add it to data->res.
6765 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
6766 void *user)
6768 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
6769 isl_pw_multi_aff *pma;
6770 isl_multi_val *mv;
6772 mv = isl_multi_val_copy(data->mv);
6773 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
6774 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6776 return data->res ? 0 : -1;
6779 /* Return a union piecewise multi-affine expression
6780 * that is equal to "mv" on "domain".
6782 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
6783 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
6785 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
6786 isl_space *space;
6788 space = isl_union_set_get_space(domain);
6789 data.res = isl_union_pw_multi_aff_empty(space);
6790 data.mv = mv;
6791 if (isl_union_set_foreach_set(domain,
6792 &pw_multi_aff_multi_val_on_domain, &data) < 0)
6793 data.res = isl_union_pw_multi_aff_free(data.res);
6794 isl_union_set_free(domain);
6795 isl_multi_val_free(mv);
6796 return data.res;