add isl_pw_aff_pos_set
[isl.git] / isl_aff.c
blob66e453bc9aaf3de9001f6684c8f64e6be1a94895
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 positive. "rational" should not be set.
2167 * If "aff" is NaN, then it is not positive.
2169 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2170 int rational)
2172 isl_constraint *ineq;
2173 isl_basic_set *bset;
2174 isl_val *c;
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);
2183 if (rational)
2184 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2185 "rational sets not supported", goto error);
2187 ineq = isl_inequality_from_aff(aff);
2188 c = isl_constraint_get_constant_val(ineq);
2189 c = isl_val_sub_ui(c, 1);
2190 ineq = isl_constraint_set_constant_val(ineq, c);
2192 bset = isl_basic_set_from_constraint(ineq);
2193 bset = isl_basic_set_simplify(bset);
2194 return bset;
2195 error:
2196 isl_aff_free(aff);
2197 return NULL;
2200 /* Return a basic set containing those elements in the space
2201 * of aff where it is non-negative.
2202 * If "rational" is set, then return a rational basic set.
2204 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2206 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2207 __isl_take isl_aff *aff, int rational)
2209 isl_constraint *ineq;
2210 isl_basic_set *bset;
2212 if (!aff)
2213 return NULL;
2214 if (isl_aff_is_nan(aff)) {
2215 isl_space *space = isl_aff_get_domain_space(aff);
2216 isl_aff_free(aff);
2217 return isl_basic_set_empty(space);
2220 ineq = isl_inequality_from_aff(aff);
2222 bset = isl_basic_set_from_constraint(ineq);
2223 if (rational)
2224 bset = isl_basic_set_set_rational(bset);
2225 bset = isl_basic_set_simplify(bset);
2226 return bset;
2229 /* Return a basic set containing those elements in the space
2230 * of aff where it is non-negative.
2232 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2234 return aff_nonneg_basic_set(aff, 0);
2237 /* Return a basic set containing those elements in the domain space
2238 * of aff where it is negative.
2240 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2242 aff = isl_aff_neg(aff);
2243 aff = isl_aff_add_constant_num_si(aff, -1);
2244 return isl_aff_nonneg_basic_set(aff);
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2249 * If "rational" is set, then return a rational basic set.
2251 * If "aff" is NaN, then it is not zero.
2253 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2254 int rational)
2256 isl_constraint *ineq;
2257 isl_basic_set *bset;
2259 if (!aff)
2260 return NULL;
2261 if (isl_aff_is_nan(aff)) {
2262 isl_space *space = isl_aff_get_domain_space(aff);
2263 isl_aff_free(aff);
2264 return isl_basic_set_empty(space);
2267 ineq = isl_equality_from_aff(aff);
2269 bset = isl_basic_set_from_constraint(ineq);
2270 if (rational)
2271 bset = isl_basic_set_set_rational(bset);
2272 bset = isl_basic_set_simplify(bset);
2273 return bset;
2276 /* Return a basic set containing those elements in the space
2277 * of aff where it is zero.
2279 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2281 return aff_zero_basic_set(aff, 0);
2284 /* Return a basic set containing those elements in the shared space
2285 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2287 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2288 __isl_take isl_aff *aff2)
2290 aff1 = isl_aff_sub(aff1, aff2);
2292 return isl_aff_nonneg_basic_set(aff1);
2295 /* Return a basic set containing those elements in the shared space
2296 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2298 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2299 __isl_take isl_aff *aff2)
2301 return isl_aff_ge_basic_set(aff2, aff1);
2304 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2305 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2307 aff1 = isl_aff_add(aff1, aff2);
2308 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2309 return aff1;
2312 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2314 if (!aff)
2315 return -1;
2317 return 0;
2320 /* Check whether the given affine expression has non-zero coefficient
2321 * for any dimension in the given range or if any of these dimensions
2322 * appear with non-zero coefficients in any of the integer divisions
2323 * involved in the affine expression.
2325 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2326 enum isl_dim_type type, unsigned first, unsigned n)
2328 int i;
2329 isl_ctx *ctx;
2330 int *active = NULL;
2331 int involves = 0;
2333 if (!aff)
2334 return -1;
2335 if (n == 0)
2336 return 0;
2338 ctx = isl_aff_get_ctx(aff);
2339 if (first + n > isl_aff_dim(aff, type))
2340 isl_die(ctx, isl_error_invalid,
2341 "range out of bounds", return -1);
2343 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2344 if (!active)
2345 goto error;
2347 first += isl_local_space_offset(aff->ls, type) - 1;
2348 for (i = 0; i < n; ++i)
2349 if (active[first + i]) {
2350 involves = 1;
2351 break;
2354 free(active);
2356 return involves;
2357 error:
2358 free(active);
2359 return -1;
2362 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2363 enum isl_dim_type type, unsigned first, unsigned n)
2365 isl_ctx *ctx;
2367 if (!aff)
2368 return NULL;
2369 if (type == isl_dim_out)
2370 isl_die(aff->v->ctx, isl_error_invalid,
2371 "cannot drop output/set dimension",
2372 return isl_aff_free(aff));
2373 if (type == isl_dim_in)
2374 type = isl_dim_set;
2375 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2376 return aff;
2378 ctx = isl_aff_get_ctx(aff);
2379 if (first + n > isl_local_space_dim(aff->ls, type))
2380 isl_die(ctx, isl_error_invalid, "range out of bounds",
2381 return isl_aff_free(aff));
2383 aff = isl_aff_cow(aff);
2384 if (!aff)
2385 return NULL;
2387 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2388 if (!aff->ls)
2389 return isl_aff_free(aff);
2391 first += 1 + isl_local_space_offset(aff->ls, type);
2392 aff->v = isl_vec_drop_els(aff->v, first, n);
2393 if (!aff->v)
2394 return isl_aff_free(aff);
2396 return aff;
2399 /* Project the domain of the affine expression onto its parameter space.
2400 * The affine expression may not involve any of the domain dimensions.
2402 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2404 isl_space *space;
2405 unsigned n;
2406 int involves;
2408 n = isl_aff_dim(aff, isl_dim_in);
2409 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2410 if (involves < 0)
2411 return isl_aff_free(aff);
2412 if (involves)
2413 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2414 "affine expression involves some of the domain dimensions",
2415 return isl_aff_free(aff));
2416 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2417 space = isl_aff_get_domain_space(aff);
2418 space = isl_space_params(space);
2419 aff = isl_aff_reset_domain_space(aff, space);
2420 return aff;
2423 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2424 enum isl_dim_type type, unsigned first, unsigned n)
2426 isl_ctx *ctx;
2428 if (!aff)
2429 return NULL;
2430 if (type == isl_dim_out)
2431 isl_die(aff->v->ctx, isl_error_invalid,
2432 "cannot insert output/set dimensions",
2433 return isl_aff_free(aff));
2434 if (type == isl_dim_in)
2435 type = isl_dim_set;
2436 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2437 return aff;
2439 ctx = isl_aff_get_ctx(aff);
2440 if (first > isl_local_space_dim(aff->ls, type))
2441 isl_die(ctx, isl_error_invalid, "position out of bounds",
2442 return isl_aff_free(aff));
2444 aff = isl_aff_cow(aff);
2445 if (!aff)
2446 return NULL;
2448 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2449 if (!aff->ls)
2450 return isl_aff_free(aff);
2452 first += 1 + isl_local_space_offset(aff->ls, type);
2453 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2454 if (!aff->v)
2455 return isl_aff_free(aff);
2457 return aff;
2460 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2461 enum isl_dim_type type, unsigned n)
2463 unsigned pos;
2465 pos = isl_aff_dim(aff, type);
2467 return isl_aff_insert_dims(aff, type, pos, n);
2470 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2471 enum isl_dim_type type, unsigned n)
2473 unsigned pos;
2475 pos = isl_pw_aff_dim(pwaff, type);
2477 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2480 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2481 * to dimensions of "dst_type" at "dst_pos".
2483 * We only support moving input dimensions to parameters and vice versa.
2485 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2486 enum isl_dim_type dst_type, unsigned dst_pos,
2487 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2489 unsigned g_dst_pos;
2490 unsigned g_src_pos;
2492 if (!aff)
2493 return NULL;
2494 if (n == 0 &&
2495 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2496 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2497 return aff;
2499 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2500 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2501 "cannot move output/set dimension", isl_aff_free(aff));
2502 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2503 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2504 "cannot move divs", isl_aff_free(aff));
2505 if (dst_type == isl_dim_in)
2506 dst_type = isl_dim_set;
2507 if (src_type == isl_dim_in)
2508 src_type = isl_dim_set;
2510 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2511 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2512 "range out of bounds", isl_aff_free(aff));
2513 if (dst_type == src_type)
2514 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2515 "moving dims within the same type not supported",
2516 isl_aff_free(aff));
2518 aff = isl_aff_cow(aff);
2519 if (!aff)
2520 return NULL;
2522 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2523 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2524 if (dst_type > src_type)
2525 g_dst_pos -= n;
2527 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2528 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2529 src_type, src_pos, n);
2530 if (!aff->v || !aff->ls)
2531 return isl_aff_free(aff);
2533 aff = sort_divs(aff);
2535 return aff;
2538 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2540 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2541 return isl_pw_aff_alloc(dom, aff);
2544 #undef PW
2545 #define PW isl_pw_aff
2546 #undef EL
2547 #define EL isl_aff
2548 #undef EL_IS_ZERO
2549 #define EL_IS_ZERO is_empty
2550 #undef ZERO
2551 #define ZERO empty
2552 #undef IS_ZERO
2553 #define IS_ZERO is_empty
2554 #undef FIELD
2555 #define FIELD aff
2556 #undef DEFAULT_IS_ZERO
2557 #define DEFAULT_IS_ZERO 0
2559 #define NO_EVAL
2560 #define NO_OPT
2561 #define NO_LIFT
2562 #define NO_MORPH
2564 #include <isl_pw_templ.c>
2566 static __isl_give isl_set *align_params_pw_pw_set_and(
2567 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2568 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2569 __isl_take isl_pw_aff *pwaff2))
2571 if (!pwaff1 || !pwaff2)
2572 goto error;
2573 if (isl_space_match(pwaff1->dim, isl_dim_param,
2574 pwaff2->dim, isl_dim_param))
2575 return fn(pwaff1, pwaff2);
2576 if (!isl_space_has_named_params(pwaff1->dim) ||
2577 !isl_space_has_named_params(pwaff2->dim))
2578 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2579 "unaligned unnamed parameters", goto error);
2580 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2581 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2582 return fn(pwaff1, pwaff2);
2583 error:
2584 isl_pw_aff_free(pwaff1);
2585 isl_pw_aff_free(pwaff2);
2586 return NULL;
2589 /* Compute a piecewise quasi-affine expression with a domain that
2590 * is the union of those of pwaff1 and pwaff2 and such that on each
2591 * cell, the quasi-affine expression is the better (according to cmp)
2592 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2593 * is defined on a given cell, then the associated expression
2594 * is the defined one.
2596 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2597 __isl_take isl_pw_aff *pwaff2,
2598 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2599 __isl_take isl_aff *aff2))
2601 int i, j, n;
2602 isl_pw_aff *res;
2603 isl_ctx *ctx;
2604 isl_set *set;
2606 if (!pwaff1 || !pwaff2)
2607 goto error;
2609 ctx = isl_space_get_ctx(pwaff1->dim);
2610 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2611 isl_die(ctx, isl_error_invalid,
2612 "arguments should live in same space", goto error);
2614 if (isl_pw_aff_is_empty(pwaff1)) {
2615 isl_pw_aff_free(pwaff1);
2616 return pwaff2;
2619 if (isl_pw_aff_is_empty(pwaff2)) {
2620 isl_pw_aff_free(pwaff2);
2621 return pwaff1;
2624 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2625 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2627 for (i = 0; i < pwaff1->n; ++i) {
2628 set = isl_set_copy(pwaff1->p[i].set);
2629 for (j = 0; j < pwaff2->n; ++j) {
2630 struct isl_set *common;
2631 isl_set *better;
2633 common = isl_set_intersect(
2634 isl_set_copy(pwaff1->p[i].set),
2635 isl_set_copy(pwaff2->p[j].set));
2636 better = isl_set_from_basic_set(cmp(
2637 isl_aff_copy(pwaff2->p[j].aff),
2638 isl_aff_copy(pwaff1->p[i].aff)));
2639 better = isl_set_intersect(common, better);
2640 if (isl_set_plain_is_empty(better)) {
2641 isl_set_free(better);
2642 continue;
2644 set = isl_set_subtract(set, isl_set_copy(better));
2646 res = isl_pw_aff_add_piece(res, better,
2647 isl_aff_copy(pwaff2->p[j].aff));
2649 res = isl_pw_aff_add_piece(res, set,
2650 isl_aff_copy(pwaff1->p[i].aff));
2653 for (j = 0; j < pwaff2->n; ++j) {
2654 set = isl_set_copy(pwaff2->p[j].set);
2655 for (i = 0; i < pwaff1->n; ++i)
2656 set = isl_set_subtract(set,
2657 isl_set_copy(pwaff1->p[i].set));
2658 res = isl_pw_aff_add_piece(res, set,
2659 isl_aff_copy(pwaff2->p[j].aff));
2662 isl_pw_aff_free(pwaff1);
2663 isl_pw_aff_free(pwaff2);
2665 return res;
2666 error:
2667 isl_pw_aff_free(pwaff1);
2668 isl_pw_aff_free(pwaff2);
2669 return NULL;
2672 /* Compute a piecewise quasi-affine expression with a domain that
2673 * is the union of those of pwaff1 and pwaff2 and such that on each
2674 * cell, the quasi-affine expression is the maximum of those of pwaff1
2675 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2676 * cell, then the associated expression is the defined one.
2678 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2679 __isl_take isl_pw_aff *pwaff2)
2681 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2684 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2685 __isl_take isl_pw_aff *pwaff2)
2687 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2688 &pw_aff_union_max);
2691 /* Compute a piecewise quasi-affine expression with a domain that
2692 * is the union of those of pwaff1 and pwaff2 and such that on each
2693 * cell, the quasi-affine expression is the minimum of those of pwaff1
2694 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2695 * cell, then the associated expression is the defined one.
2697 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2698 __isl_take isl_pw_aff *pwaff2)
2700 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2703 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2704 __isl_take isl_pw_aff *pwaff2)
2706 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2707 &pw_aff_union_min);
2710 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2711 __isl_take isl_pw_aff *pwaff2, int max)
2713 if (max)
2714 return isl_pw_aff_union_max(pwaff1, pwaff2);
2715 else
2716 return isl_pw_aff_union_min(pwaff1, pwaff2);
2719 /* Construct a map with as domain the domain of pwaff and
2720 * one-dimensional range corresponding to the affine expressions.
2722 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2724 int i;
2725 isl_space *dim;
2726 isl_map *map;
2728 if (!pwaff)
2729 return NULL;
2731 dim = isl_pw_aff_get_space(pwaff);
2732 map = isl_map_empty(dim);
2734 for (i = 0; i < pwaff->n; ++i) {
2735 isl_basic_map *bmap;
2736 isl_map *map_i;
2738 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2739 map_i = isl_map_from_basic_map(bmap);
2740 map_i = isl_map_intersect_domain(map_i,
2741 isl_set_copy(pwaff->p[i].set));
2742 map = isl_map_union_disjoint(map, map_i);
2745 isl_pw_aff_free(pwaff);
2747 return map;
2750 /* Construct a map with as domain the domain of pwaff and
2751 * one-dimensional range corresponding to the affine expressions.
2753 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2755 if (!pwaff)
2756 return NULL;
2757 if (isl_space_is_set(pwaff->dim))
2758 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2759 "space of input is not a map", goto error);
2760 return map_from_pw_aff(pwaff);
2761 error:
2762 isl_pw_aff_free(pwaff);
2763 return NULL;
2766 /* Construct a one-dimensional set with as parameter domain
2767 * the domain of pwaff and the single set dimension
2768 * corresponding to the affine expressions.
2770 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2772 if (!pwaff)
2773 return NULL;
2774 if (!isl_space_is_set(pwaff->dim))
2775 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2776 "space of input is not a set", goto error);
2777 return map_from_pw_aff(pwaff);
2778 error:
2779 isl_pw_aff_free(pwaff);
2780 return NULL;
2783 /* Return a set containing those elements in the domain
2784 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2785 * does not satisfy "fn" (if complement is 1).
2787 * The pieces with a NaN never belong to the result since
2788 * NaN does not satisfy any property.
2790 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2791 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2792 int complement)
2794 int i;
2795 isl_set *set;
2797 if (!pwaff)
2798 return NULL;
2800 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2802 for (i = 0; i < pwaff->n; ++i) {
2803 isl_basic_set *bset;
2804 isl_set *set_i, *locus;
2805 int rational;
2807 if (isl_aff_is_nan(pwaff->p[i].aff))
2808 continue;
2810 rational = isl_set_has_rational(pwaff->p[i].set);
2811 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2812 locus = isl_set_from_basic_set(bset);
2813 set_i = isl_set_copy(pwaff->p[i].set);
2814 if (complement)
2815 set_i = isl_set_subtract(set_i, locus);
2816 else
2817 set_i = isl_set_intersect(set_i, locus);
2818 set = isl_set_union_disjoint(set, set_i);
2821 isl_pw_aff_free(pwaff);
2823 return set;
2826 /* Return a set containing those elements in the domain
2827 * of "pa" where it is positive.
2829 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2831 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2834 /* Return a set containing those elements in the domain
2835 * of pwaff where it is non-negative.
2837 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2839 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2842 /* Return a set containing those elements in the domain
2843 * of pwaff where it is zero.
2845 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2847 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2850 /* Return a set containing those elements in the domain
2851 * of pwaff where it is not zero.
2853 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2855 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2858 /* Return a set containing those elements in the shared domain
2859 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2861 * We compute the difference on the shared domain and then construct
2862 * the set of values where this difference is non-negative.
2863 * If strict is set, we first subtract 1 from the difference.
2864 * If equal is set, we only return the elements where pwaff1 and pwaff2
2865 * are equal.
2867 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2868 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2870 isl_set *set1, *set2;
2872 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2873 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2874 set1 = isl_set_intersect(set1, set2);
2875 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2876 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2877 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2879 if (strict) {
2880 isl_space *dim = isl_set_get_space(set1);
2881 isl_aff *aff;
2882 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2883 aff = isl_aff_add_constant_si(aff, -1);
2884 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2885 } else
2886 isl_set_free(set1);
2888 if (equal)
2889 return isl_pw_aff_zero_set(pwaff1);
2890 return isl_pw_aff_nonneg_set(pwaff1);
2893 /* Return a set containing those elements in the shared domain
2894 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2896 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2897 __isl_take isl_pw_aff *pwaff2)
2899 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2902 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2903 __isl_take isl_pw_aff *pwaff2)
2905 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2908 /* Return a set containing those elements in the shared domain
2909 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2911 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2912 __isl_take isl_pw_aff *pwaff2)
2914 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2917 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2918 __isl_take isl_pw_aff *pwaff2)
2920 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2923 /* Return a set containing those elements in the shared domain
2924 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2926 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2927 __isl_take isl_pw_aff *pwaff2)
2929 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2932 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2933 __isl_take isl_pw_aff *pwaff2)
2935 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2938 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2939 __isl_take isl_pw_aff *pwaff2)
2941 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2944 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2945 __isl_take isl_pw_aff *pwaff2)
2947 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2950 /* Return a set containing those elements in the shared domain
2951 * of the elements of list1 and list2 where each element in list1
2952 * has the relation specified by "fn" with each element in list2.
2954 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2955 __isl_take isl_pw_aff_list *list2,
2956 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2957 __isl_take isl_pw_aff *pwaff2))
2959 int i, j;
2960 isl_ctx *ctx;
2961 isl_set *set;
2963 if (!list1 || !list2)
2964 goto error;
2966 ctx = isl_pw_aff_list_get_ctx(list1);
2967 if (list1->n < 1 || list2->n < 1)
2968 isl_die(ctx, isl_error_invalid,
2969 "list should contain at least one element", goto error);
2971 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2972 for (i = 0; i < list1->n; ++i)
2973 for (j = 0; j < list2->n; ++j) {
2974 isl_set *set_ij;
2976 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2977 isl_pw_aff_copy(list2->p[j]));
2978 set = isl_set_intersect(set, set_ij);
2981 isl_pw_aff_list_free(list1);
2982 isl_pw_aff_list_free(list2);
2983 return set;
2984 error:
2985 isl_pw_aff_list_free(list1);
2986 isl_pw_aff_list_free(list2);
2987 return NULL;
2990 /* Return a set containing those elements in the shared domain
2991 * of the elements of list1 and list2 where each element in list1
2992 * is equal to each element in list2.
2994 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2995 __isl_take isl_pw_aff_list *list2)
2997 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3000 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3001 __isl_take isl_pw_aff_list *list2)
3003 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3006 /* Return a set containing those elements in the shared domain
3007 * of the elements of list1 and list2 where each element in list1
3008 * is less than or equal to each element in list2.
3010 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3011 __isl_take isl_pw_aff_list *list2)
3013 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3016 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3017 __isl_take isl_pw_aff_list *list2)
3019 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3022 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3023 __isl_take isl_pw_aff_list *list2)
3025 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3028 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3029 __isl_take isl_pw_aff_list *list2)
3031 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3035 /* Return a set containing those elements in the shared domain
3036 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3038 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3039 __isl_take isl_pw_aff *pwaff2)
3041 isl_set *set_lt, *set_gt;
3043 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3044 isl_pw_aff_copy(pwaff2));
3045 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3046 return isl_set_union_disjoint(set_lt, set_gt);
3049 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3050 __isl_take isl_pw_aff *pwaff2)
3052 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3055 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3056 isl_int v)
3058 int i;
3060 if (isl_int_is_one(v))
3061 return pwaff;
3062 if (!isl_int_is_pos(v))
3063 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3064 "factor needs to be positive",
3065 return isl_pw_aff_free(pwaff));
3066 pwaff = isl_pw_aff_cow(pwaff);
3067 if (!pwaff)
3068 return NULL;
3069 if (pwaff->n == 0)
3070 return pwaff;
3072 for (i = 0; i < pwaff->n; ++i) {
3073 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3074 if (!pwaff->p[i].aff)
3075 return isl_pw_aff_free(pwaff);
3078 return pwaff;
3081 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3083 int i;
3085 pwaff = isl_pw_aff_cow(pwaff);
3086 if (!pwaff)
3087 return NULL;
3088 if (pwaff->n == 0)
3089 return pwaff;
3091 for (i = 0; i < pwaff->n; ++i) {
3092 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3093 if (!pwaff->p[i].aff)
3094 return isl_pw_aff_free(pwaff);
3097 return pwaff;
3100 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3102 int i;
3104 pwaff = isl_pw_aff_cow(pwaff);
3105 if (!pwaff)
3106 return NULL;
3107 if (pwaff->n == 0)
3108 return pwaff;
3110 for (i = 0; i < pwaff->n; ++i) {
3111 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3112 if (!pwaff->p[i].aff)
3113 return isl_pw_aff_free(pwaff);
3116 return pwaff;
3119 /* Assuming that "cond1" and "cond2" are disjoint,
3120 * return an affine expression that is equal to pwaff1 on cond1
3121 * and to pwaff2 on cond2.
3123 static __isl_give isl_pw_aff *isl_pw_aff_select(
3124 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3125 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3127 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3128 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3130 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3133 /* Return an affine expression that is equal to pwaff_true for elements
3134 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3135 * is zero.
3136 * That is, return cond ? pwaff_true : pwaff_false;
3138 * If "cond" involves and NaN, then we conservatively return a NaN
3139 * on its entire domain. In principle, we could consider the pieces
3140 * where it is NaN separately from those where it is not.
3142 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3143 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3145 isl_set *cond_true, *cond_false;
3147 if (!cond)
3148 goto error;
3149 if (isl_pw_aff_involves_nan(cond)) {
3150 isl_space *space = isl_pw_aff_get_domain_space(cond);
3151 isl_local_space *ls = isl_local_space_from_space(space);
3152 isl_pw_aff_free(cond);
3153 isl_pw_aff_free(pwaff_true);
3154 isl_pw_aff_free(pwaff_false);
3155 return isl_pw_aff_nan_on_domain(ls);
3158 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3159 cond_false = isl_pw_aff_zero_set(cond);
3160 return isl_pw_aff_select(cond_true, pwaff_true,
3161 cond_false, pwaff_false);
3162 error:
3163 isl_pw_aff_free(cond);
3164 isl_pw_aff_free(pwaff_true);
3165 isl_pw_aff_free(pwaff_false);
3166 return NULL;
3169 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3171 if (!aff)
3172 return -1;
3174 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3177 /* Check whether pwaff is a piecewise constant.
3179 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3181 int i;
3183 if (!pwaff)
3184 return -1;
3186 for (i = 0; i < pwaff->n; ++i) {
3187 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3188 if (is_cst < 0 || !is_cst)
3189 return is_cst;
3192 return 1;
3195 /* Return the product of "aff1" and "aff2".
3197 * If either of the two is NaN, then the result is NaN.
3199 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3201 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3202 __isl_take isl_aff *aff2)
3204 if (!aff1 || !aff2)
3205 goto error;
3207 if (isl_aff_is_nan(aff1)) {
3208 isl_aff_free(aff2);
3209 return aff1;
3211 if (isl_aff_is_nan(aff2)) {
3212 isl_aff_free(aff1);
3213 return aff2;
3216 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3217 return isl_aff_mul(aff2, aff1);
3219 if (!isl_aff_is_cst(aff2))
3220 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3221 "at least one affine expression should be constant",
3222 goto error);
3224 aff1 = isl_aff_cow(aff1);
3225 if (!aff1 || !aff2)
3226 goto error;
3228 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3229 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3231 isl_aff_free(aff2);
3232 return aff1;
3233 error:
3234 isl_aff_free(aff1);
3235 isl_aff_free(aff2);
3236 return NULL;
3239 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3241 * If either of the two is NaN, then the result is NaN.
3243 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3244 __isl_take isl_aff *aff2)
3246 int is_cst;
3247 int neg;
3249 if (!aff1 || !aff2)
3250 goto error;
3252 if (isl_aff_is_nan(aff1)) {
3253 isl_aff_free(aff2);
3254 return aff1;
3256 if (isl_aff_is_nan(aff2)) {
3257 isl_aff_free(aff1);
3258 return aff2;
3261 is_cst = isl_aff_is_cst(aff2);
3262 if (is_cst < 0)
3263 goto error;
3264 if (!is_cst)
3265 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3266 "second argument should be a constant", goto error);
3268 if (!aff2)
3269 goto error;
3271 neg = isl_int_is_neg(aff2->v->el[1]);
3272 if (neg) {
3273 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3274 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3277 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3278 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3280 if (neg) {
3281 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3282 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3285 isl_aff_free(aff2);
3286 return aff1;
3287 error:
3288 isl_aff_free(aff1);
3289 isl_aff_free(aff2);
3290 return NULL;
3293 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3294 __isl_take isl_pw_aff *pwaff2)
3296 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3299 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3300 __isl_take isl_pw_aff *pwaff2)
3302 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3305 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3306 __isl_take isl_pw_aff *pwaff2)
3308 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3311 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3312 __isl_take isl_pw_aff *pwaff2)
3314 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3317 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3318 __isl_take isl_pw_aff *pwaff2)
3320 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3323 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3324 __isl_take isl_pw_aff *pa2)
3326 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3329 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3331 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3332 __isl_take isl_pw_aff *pa2)
3334 int is_cst;
3336 is_cst = isl_pw_aff_is_cst(pa2);
3337 if (is_cst < 0)
3338 goto error;
3339 if (!is_cst)
3340 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3341 "second argument should be a piecewise constant",
3342 goto error);
3343 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3344 error:
3345 isl_pw_aff_free(pa1);
3346 isl_pw_aff_free(pa2);
3347 return NULL;
3350 /* Compute the quotient of the integer division of "pa1" by "pa2"
3351 * with rounding towards zero.
3352 * "pa2" is assumed to be a piecewise constant.
3354 * In particular, return
3356 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3359 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3360 __isl_take isl_pw_aff *pa2)
3362 int is_cst;
3363 isl_set *cond;
3364 isl_pw_aff *f, *c;
3366 is_cst = isl_pw_aff_is_cst(pa2);
3367 if (is_cst < 0)
3368 goto error;
3369 if (!is_cst)
3370 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3371 "second argument should be a piecewise constant",
3372 goto error);
3374 pa1 = isl_pw_aff_div(pa1, pa2);
3376 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3377 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3378 c = isl_pw_aff_ceil(pa1);
3379 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3380 error:
3381 isl_pw_aff_free(pa1);
3382 isl_pw_aff_free(pa2);
3383 return NULL;
3386 /* Compute the remainder of the integer division of "pa1" by "pa2"
3387 * with rounding towards zero.
3388 * "pa2" is assumed to be a piecewise constant.
3390 * In particular, return
3392 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3395 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3396 __isl_take isl_pw_aff *pa2)
3398 int is_cst;
3399 isl_pw_aff *res;
3401 is_cst = isl_pw_aff_is_cst(pa2);
3402 if (is_cst < 0)
3403 goto error;
3404 if (!is_cst)
3405 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3406 "second argument should be a piecewise constant",
3407 goto error);
3408 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3409 res = isl_pw_aff_mul(pa2, res);
3410 res = isl_pw_aff_sub(pa1, res);
3411 return res;
3412 error:
3413 isl_pw_aff_free(pa1);
3414 isl_pw_aff_free(pa2);
3415 return NULL;
3418 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3419 __isl_take isl_pw_aff *pwaff2)
3421 isl_set *le;
3422 isl_set *dom;
3424 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3425 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3426 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3427 isl_pw_aff_copy(pwaff2));
3428 dom = isl_set_subtract(dom, isl_set_copy(le));
3429 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3432 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3433 __isl_take isl_pw_aff *pwaff2)
3435 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3438 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3439 __isl_take isl_pw_aff *pwaff2)
3441 isl_set *ge;
3442 isl_set *dom;
3444 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3445 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3446 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3447 isl_pw_aff_copy(pwaff2));
3448 dom = isl_set_subtract(dom, isl_set_copy(ge));
3449 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3452 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3458 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3459 __isl_take isl_pw_aff_list *list,
3460 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3461 __isl_take isl_pw_aff *pwaff2))
3463 int i;
3464 isl_ctx *ctx;
3465 isl_pw_aff *res;
3467 if (!list)
3468 return NULL;
3470 ctx = isl_pw_aff_list_get_ctx(list);
3471 if (list->n < 1)
3472 isl_die(ctx, isl_error_invalid,
3473 "list should contain at least one element", goto error);
3475 res = isl_pw_aff_copy(list->p[0]);
3476 for (i = 1; i < list->n; ++i)
3477 res = fn(res, isl_pw_aff_copy(list->p[i]));
3479 isl_pw_aff_list_free(list);
3480 return res;
3481 error:
3482 isl_pw_aff_list_free(list);
3483 return NULL;
3486 /* Return an isl_pw_aff that maps each element in the intersection of the
3487 * domains of the elements of list to the minimal corresponding affine
3488 * expression.
3490 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3492 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3495 /* Return an isl_pw_aff that maps each element in the intersection of the
3496 * domains of the elements of list to the maximal corresponding affine
3497 * expression.
3499 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3501 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3504 /* Mark the domains of "pwaff" as rational.
3506 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3508 int i;
3510 pwaff = isl_pw_aff_cow(pwaff);
3511 if (!pwaff)
3512 return NULL;
3513 if (pwaff->n == 0)
3514 return pwaff;
3516 for (i = 0; i < pwaff->n; ++i) {
3517 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3518 if (!pwaff->p[i].set)
3519 return isl_pw_aff_free(pwaff);
3522 return pwaff;
3525 /* Mark the domains of the elements of "list" as rational.
3527 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3528 __isl_take isl_pw_aff_list *list)
3530 int i, n;
3532 if (!list)
3533 return NULL;
3534 if (list->n == 0)
3535 return list;
3537 n = list->n;
3538 for (i = 0; i < n; ++i) {
3539 isl_pw_aff *pa;
3541 pa = isl_pw_aff_list_get_pw_aff(list, i);
3542 pa = isl_pw_aff_set_rational(pa);
3543 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3546 return list;
3549 /* Do the parameters of "aff" match those of "space"?
3551 int isl_aff_matching_params(__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);
3564 isl_space_free(aff_space);
3565 return match;
3568 /* Check that the domain space of "aff" matches "space".
3570 * Return 0 on success and -1 on error.
3572 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3573 __isl_keep isl_space *space)
3575 isl_space *aff_space;
3576 int match;
3578 if (!aff || !space)
3579 return -1;
3581 aff_space = isl_aff_get_domain_space(aff);
3583 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3584 if (match < 0)
3585 goto error;
3586 if (!match)
3587 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3588 "parameters don't match", goto error);
3589 match = isl_space_tuple_is_equal(space, isl_dim_in,
3590 aff_space, isl_dim_set);
3591 if (match < 0)
3592 goto error;
3593 if (!match)
3594 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3595 "domains don't match", goto error);
3596 isl_space_free(aff_space);
3597 return 0;
3598 error:
3599 isl_space_free(aff_space);
3600 return -1;
3603 #undef BASE
3604 #define BASE aff
3605 #define NO_INTERSECT_DOMAIN
3606 #define NO_DOMAIN
3608 #include <isl_multi_templ.c>
3610 #undef NO_DOMAIN
3611 #undef NO_INTERSECT_DOMAIN
3613 /* Remove any internal structure of the domain of "ma".
3614 * If there is any such internal structure in the input,
3615 * then the name of the corresponding space is also removed.
3617 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3618 __isl_take isl_multi_aff *ma)
3620 isl_space *space;
3622 if (!ma)
3623 return NULL;
3625 if (!ma->space->nested[0])
3626 return ma;
3628 space = isl_multi_aff_get_space(ma);
3629 space = isl_space_flatten_domain(space);
3630 ma = isl_multi_aff_reset_space(ma, space);
3632 return ma;
3635 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3636 * of the space to its domain.
3638 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3640 int i, n_in;
3641 isl_local_space *ls;
3642 isl_multi_aff *ma;
3644 if (!space)
3645 return NULL;
3646 if (!isl_space_is_map(space))
3647 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3648 "not a map space", goto error);
3650 n_in = isl_space_dim(space, isl_dim_in);
3651 space = isl_space_domain_map(space);
3653 ma = isl_multi_aff_alloc(isl_space_copy(space));
3654 if (n_in == 0) {
3655 isl_space_free(space);
3656 return ma;
3659 space = isl_space_domain(space);
3660 ls = isl_local_space_from_space(space);
3661 for (i = 0; i < n_in; ++i) {
3662 isl_aff *aff;
3664 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3665 isl_dim_set, i);
3666 ma = isl_multi_aff_set_aff(ma, i, aff);
3668 isl_local_space_free(ls);
3669 return ma;
3670 error:
3671 isl_space_free(space);
3672 return NULL;
3675 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3676 * of the space to its range.
3678 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3680 int i, n_in, n_out;
3681 isl_local_space *ls;
3682 isl_multi_aff *ma;
3684 if (!space)
3685 return NULL;
3686 if (!isl_space_is_map(space))
3687 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3688 "not a map space", goto error);
3690 n_in = isl_space_dim(space, isl_dim_in);
3691 n_out = isl_space_dim(space, isl_dim_out);
3692 space = isl_space_range_map(space);
3694 ma = isl_multi_aff_alloc(isl_space_copy(space));
3695 if (n_out == 0) {
3696 isl_space_free(space);
3697 return ma;
3700 space = isl_space_domain(space);
3701 ls = isl_local_space_from_space(space);
3702 for (i = 0; i < n_out; ++i) {
3703 isl_aff *aff;
3705 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3706 isl_dim_set, n_in + i);
3707 ma = isl_multi_aff_set_aff(ma, i, aff);
3709 isl_local_space_free(ls);
3710 return ma;
3711 error:
3712 isl_space_free(space);
3713 return NULL;
3716 /* Given the space of a set and a range of set dimensions,
3717 * construct an isl_multi_aff that projects out those dimensions.
3719 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3720 __isl_take isl_space *space, enum isl_dim_type type,
3721 unsigned first, unsigned n)
3723 int i, dim;
3724 isl_local_space *ls;
3725 isl_multi_aff *ma;
3727 if (!space)
3728 return NULL;
3729 if (!isl_space_is_set(space))
3730 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3731 "expecting set space", goto error);
3732 if (type != isl_dim_set)
3733 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3734 "only set dimensions can be projected out", goto error);
3736 dim = isl_space_dim(space, isl_dim_set);
3737 if (first + n > dim)
3738 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3739 "range out of bounds", goto error);
3741 space = isl_space_from_domain(space);
3742 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3744 if (dim == n)
3745 return isl_multi_aff_alloc(space);
3747 ma = isl_multi_aff_alloc(isl_space_copy(space));
3748 space = isl_space_domain(space);
3749 ls = isl_local_space_from_space(space);
3751 for (i = 0; i < first; ++i) {
3752 isl_aff *aff;
3754 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3755 isl_dim_set, i);
3756 ma = isl_multi_aff_set_aff(ma, i, aff);
3759 for (i = 0; i < dim - (first + n); ++i) {
3760 isl_aff *aff;
3762 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3763 isl_dim_set, first + n + i);
3764 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3767 isl_local_space_free(ls);
3768 return ma;
3769 error:
3770 isl_space_free(space);
3771 return NULL;
3774 /* Given the space of a set and a range of set dimensions,
3775 * construct an isl_pw_multi_aff that projects out those dimensions.
3777 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3778 __isl_take isl_space *space, enum isl_dim_type type,
3779 unsigned first, unsigned n)
3781 isl_multi_aff *ma;
3783 ma = isl_multi_aff_project_out_map(space, type, first, n);
3784 return isl_pw_multi_aff_from_multi_aff(ma);
3787 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3788 * domain.
3790 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3791 __isl_take isl_multi_aff *ma)
3793 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3794 return isl_pw_multi_aff_alloc(dom, ma);
3797 /* Create a piecewise multi-affine expression in the given space that maps each
3798 * input dimension to the corresponding output dimension.
3800 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3801 __isl_take isl_space *space)
3803 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3806 /* Add "ma2" to "ma1" and return the result.
3808 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3810 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3811 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3813 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3816 /* Add "ma2" to "ma1" and return the result.
3818 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3819 __isl_take isl_multi_aff *ma2)
3821 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3822 &isl_multi_aff_add_aligned);
3825 /* Exploit the equalities in "eq" to simplify the affine expressions.
3827 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3828 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3830 int i;
3832 maff = isl_multi_aff_cow(maff);
3833 if (!maff || !eq)
3834 goto error;
3836 for (i = 0; i < maff->n; ++i) {
3837 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3838 isl_basic_set_copy(eq));
3839 if (!maff->p[i])
3840 goto error;
3843 isl_basic_set_free(eq);
3844 return maff;
3845 error:
3846 isl_basic_set_free(eq);
3847 isl_multi_aff_free(maff);
3848 return NULL;
3851 /* Given f, return floor(f).
3853 __isl_give isl_multi_aff *isl_multi_aff_floor(__isl_take isl_multi_aff *ma)
3855 int i;
3857 ma = isl_multi_aff_cow(ma);
3858 if (!ma)
3859 return NULL;
3861 for (i = 0; i < ma->n; ++i) {
3862 ma->p[i] = isl_aff_floor(ma->p[i]);
3863 if (!ma->p[i])
3864 return isl_multi_aff_free(ma);
3867 return ma;
3870 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3871 isl_int f)
3873 int i;
3875 maff = isl_multi_aff_cow(maff);
3876 if (!maff)
3877 return NULL;
3879 for (i = 0; i < maff->n; ++i) {
3880 maff->p[i] = isl_aff_scale(maff->p[i], f);
3881 if (!maff->p[i])
3882 return isl_multi_aff_free(maff);
3885 return maff;
3888 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3889 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3891 maff1 = isl_multi_aff_add(maff1, maff2);
3892 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3893 return maff1;
3896 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3898 if (!maff)
3899 return -1;
3901 return 0;
3904 /* Return the set of domain elements where "ma1" is lexicographically
3905 * smaller than or equal to "ma2".
3907 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3908 __isl_take isl_multi_aff *ma2)
3910 return isl_multi_aff_lex_ge_set(ma2, ma1);
3913 /* Return the set of domain elements where "ma1" is lexicographically
3914 * greater than or equal to "ma2".
3916 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3917 __isl_take isl_multi_aff *ma2)
3919 isl_space *space;
3920 isl_map *map1, *map2;
3921 isl_map *map, *ge;
3923 map1 = isl_map_from_multi_aff(ma1);
3924 map2 = isl_map_from_multi_aff(ma2);
3925 map = isl_map_range_product(map1, map2);
3926 space = isl_space_range(isl_map_get_space(map));
3927 space = isl_space_domain(isl_space_unwrap(space));
3928 ge = isl_map_lex_ge(space);
3929 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3931 return isl_map_domain(map);
3934 #undef PW
3935 #define PW isl_pw_multi_aff
3936 #undef EL
3937 #define EL isl_multi_aff
3938 #undef EL_IS_ZERO
3939 #define EL_IS_ZERO is_empty
3940 #undef ZERO
3941 #define ZERO empty
3942 #undef IS_ZERO
3943 #define IS_ZERO is_empty
3944 #undef FIELD
3945 #define FIELD maff
3946 #undef DEFAULT_IS_ZERO
3947 #define DEFAULT_IS_ZERO 0
3949 #define NO_SUB
3950 #define NO_EVAL
3951 #define NO_OPT
3952 #define NO_INVOLVES_DIMS
3953 #define NO_INSERT_DIMS
3954 #define NO_LIFT
3955 #define NO_MORPH
3957 #include <isl_pw_templ.c>
3959 #undef NO_SUB
3961 #undef UNION
3962 #define UNION isl_union_pw_multi_aff
3963 #undef PART
3964 #define PART isl_pw_multi_aff
3965 #undef PARTS
3966 #define PARTS pw_multi_aff
3967 #define ALIGN_DOMAIN
3969 #define NO_EVAL
3971 #include <isl_union_templ.c>
3973 /* Given a function "cmp" that returns the set of elements where
3974 * "ma1" is "better" than "ma2", return the intersection of this
3975 * set with "dom1" and "dom2".
3977 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3978 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3979 __isl_keep isl_multi_aff *ma2,
3980 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3981 __isl_take isl_multi_aff *ma2))
3983 isl_set *common;
3984 isl_set *better;
3985 int is_empty;
3987 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3988 is_empty = isl_set_plain_is_empty(common);
3989 if (is_empty >= 0 && is_empty)
3990 return common;
3991 if (is_empty < 0)
3992 return isl_set_free(common);
3993 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3994 better = isl_set_intersect(common, better);
3996 return better;
3999 /* Given a function "cmp" that returns the set of elements where
4000 * "ma1" is "better" than "ma2", return a piecewise multi affine
4001 * expression defined on the union of the definition domains
4002 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4003 * "pma2" on each cell. If only one of the two input functions
4004 * is defined on a given cell, then it is considered the best.
4006 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4007 __isl_take isl_pw_multi_aff *pma1,
4008 __isl_take isl_pw_multi_aff *pma2,
4009 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4010 __isl_take isl_multi_aff *ma2))
4012 int i, j, n;
4013 isl_pw_multi_aff *res = NULL;
4014 isl_ctx *ctx;
4015 isl_set *set = NULL;
4017 if (!pma1 || !pma2)
4018 goto error;
4020 ctx = isl_space_get_ctx(pma1->dim);
4021 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4022 isl_die(ctx, isl_error_invalid,
4023 "arguments should live in the same space", goto error);
4025 if (isl_pw_multi_aff_is_empty(pma1)) {
4026 isl_pw_multi_aff_free(pma1);
4027 return pma2;
4030 if (isl_pw_multi_aff_is_empty(pma2)) {
4031 isl_pw_multi_aff_free(pma2);
4032 return pma1;
4035 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4036 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4038 for (i = 0; i < pma1->n; ++i) {
4039 set = isl_set_copy(pma1->p[i].set);
4040 for (j = 0; j < pma2->n; ++j) {
4041 isl_set *better;
4042 int is_empty;
4044 better = shared_and_better(pma2->p[j].set,
4045 pma1->p[i].set, pma2->p[j].maff,
4046 pma1->p[i].maff, cmp);
4047 is_empty = isl_set_plain_is_empty(better);
4048 if (is_empty < 0 || is_empty) {
4049 isl_set_free(better);
4050 if (is_empty < 0)
4051 goto error;
4052 continue;
4054 set = isl_set_subtract(set, isl_set_copy(better));
4056 res = isl_pw_multi_aff_add_piece(res, better,
4057 isl_multi_aff_copy(pma2->p[j].maff));
4059 res = isl_pw_multi_aff_add_piece(res, set,
4060 isl_multi_aff_copy(pma1->p[i].maff));
4063 for (j = 0; j < pma2->n; ++j) {
4064 set = isl_set_copy(pma2->p[j].set);
4065 for (i = 0; i < pma1->n; ++i)
4066 set = isl_set_subtract(set,
4067 isl_set_copy(pma1->p[i].set));
4068 res = isl_pw_multi_aff_add_piece(res, set,
4069 isl_multi_aff_copy(pma2->p[j].maff));
4072 isl_pw_multi_aff_free(pma1);
4073 isl_pw_multi_aff_free(pma2);
4075 return res;
4076 error:
4077 isl_pw_multi_aff_free(pma1);
4078 isl_pw_multi_aff_free(pma2);
4079 isl_set_free(set);
4080 return isl_pw_multi_aff_free(res);
4083 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
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_ge_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 maximum 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 maximum.
4096 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
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_lexmax);
4104 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4105 __isl_take isl_pw_multi_aff *pma1,
4106 __isl_take isl_pw_multi_aff *pma2)
4108 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4111 /* Given two piecewise multi affine expressions, return a piecewise
4112 * multi-affine expression defined on the union of the definition domains
4113 * of the inputs that is equal to the lexicographic minimum of the two
4114 * inputs on each cell. If only one of the two inputs is defined on
4115 * a given cell, then it is considered to be the minimum.
4117 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4118 __isl_take isl_pw_multi_aff *pma1,
4119 __isl_take isl_pw_multi_aff *pma2)
4121 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4122 &pw_multi_aff_union_lexmin);
4125 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4126 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4128 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4129 &isl_multi_aff_add);
4132 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4133 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4135 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4136 &pw_multi_aff_add);
4139 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4140 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4142 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4143 &isl_multi_aff_sub);
4146 /* Subtract "pma2" from "pma1" and return the result.
4148 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4149 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4151 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4152 &pw_multi_aff_sub);
4155 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4156 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4158 return isl_pw_multi_aff_union_add_(pma1, pma2);
4161 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4162 * with the actual sum on the shared domain and
4163 * the defined expression on the symmetric difference of the domains.
4165 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4166 __isl_take isl_union_pw_multi_aff *upma1,
4167 __isl_take isl_union_pw_multi_aff *upma2)
4169 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4172 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4173 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4175 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4176 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4178 int i, j, n;
4179 isl_space *space;
4180 isl_pw_multi_aff *res;
4182 if (!pma1 || !pma2)
4183 goto error;
4185 n = pma1->n * pma2->n;
4186 space = isl_space_product(isl_space_copy(pma1->dim),
4187 isl_space_copy(pma2->dim));
4188 res = isl_pw_multi_aff_alloc_size(space, n);
4190 for (i = 0; i < pma1->n; ++i) {
4191 for (j = 0; j < pma2->n; ++j) {
4192 isl_set *domain;
4193 isl_multi_aff *ma;
4195 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4196 isl_set_copy(pma2->p[j].set));
4197 ma = isl_multi_aff_product(
4198 isl_multi_aff_copy(pma1->p[i].maff),
4199 isl_multi_aff_copy(pma2->p[j].maff));
4200 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4204 isl_pw_multi_aff_free(pma1);
4205 isl_pw_multi_aff_free(pma2);
4206 return res;
4207 error:
4208 isl_pw_multi_aff_free(pma1);
4209 isl_pw_multi_aff_free(pma2);
4210 return NULL;
4213 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4214 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4216 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4217 &pw_multi_aff_product);
4220 /* Construct a map mapping the domain of the piecewise multi-affine expression
4221 * to its range, with each dimension in the range equated to the
4222 * corresponding affine expression on its cell.
4224 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4226 int i;
4227 isl_map *map;
4229 if (!pma)
4230 return NULL;
4232 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4234 for (i = 0; i < pma->n; ++i) {
4235 isl_multi_aff *maff;
4236 isl_basic_map *bmap;
4237 isl_map *map_i;
4239 maff = isl_multi_aff_copy(pma->p[i].maff);
4240 bmap = isl_basic_map_from_multi_aff(maff);
4241 map_i = isl_map_from_basic_map(bmap);
4242 map_i = isl_map_intersect_domain(map_i,
4243 isl_set_copy(pma->p[i].set));
4244 map = isl_map_union_disjoint(map, map_i);
4247 isl_pw_multi_aff_free(pma);
4248 return map;
4251 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4253 if (!pma)
4254 return NULL;
4256 if (!isl_space_is_set(pma->dim))
4257 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4258 "isl_pw_multi_aff cannot be converted into an isl_set",
4259 goto error);
4261 return isl_map_from_pw_multi_aff(pma);
4262 error:
4263 isl_pw_multi_aff_free(pma);
4264 return NULL;
4267 /* Given a basic map with a single output dimension that is defined
4268 * in terms of the parameters and input dimensions using an equality,
4269 * extract an isl_aff that expresses the output dimension in terms
4270 * of the parameters and input dimensions.
4271 * Note that this expression may involve integer divisions defined
4272 * in terms of parameters and input dimensions.
4274 * This function shares some similarities with
4275 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4277 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4278 __isl_take isl_basic_map *bmap)
4280 int eq;
4281 unsigned offset;
4282 unsigned n_div;
4283 isl_local_space *ls;
4284 isl_aff *aff;
4286 if (!bmap)
4287 return NULL;
4288 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4289 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4290 "basic map should have a single output dimension",
4291 goto error);
4292 eq = isl_basic_map_output_defining_equality(bmap, 0);
4293 if (eq >= bmap->n_eq)
4294 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4295 "unable to find suitable equality", goto error);
4296 ls = isl_basic_map_get_local_space(bmap);
4297 aff = isl_aff_alloc(isl_local_space_domain(ls));
4298 if (!aff)
4299 goto error;
4300 offset = isl_basic_map_offset(bmap, isl_dim_out);
4301 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4302 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4303 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4304 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4305 n_div);
4306 } else {
4307 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4308 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4309 n_div);
4311 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4312 isl_basic_map_free(bmap);
4314 aff = isl_aff_remove_unused_divs(aff);
4315 return aff;
4316 error:
4317 isl_basic_map_free(bmap);
4318 return NULL;
4321 /* Given a basic map where each output dimension is defined
4322 * in terms of the parameters and input dimensions using an equality,
4323 * extract an isl_multi_aff that expresses the output dimensions in terms
4324 * of the parameters and input dimensions.
4326 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4327 __isl_take isl_basic_map *bmap)
4329 int i;
4330 unsigned n_out;
4331 isl_multi_aff *ma;
4333 if (!bmap)
4334 return NULL;
4336 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4337 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4339 for (i = 0; i < n_out; ++i) {
4340 isl_basic_map *bmap_i;
4341 isl_aff *aff;
4343 bmap_i = isl_basic_map_copy(bmap);
4344 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4345 i + 1, n_out - (1 + i));
4346 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4347 aff = extract_isl_aff_from_basic_map(bmap_i);
4348 ma = isl_multi_aff_set_aff(ma, i, aff);
4351 isl_basic_map_free(bmap);
4353 return ma;
4356 /* Given a basic set where each set dimension is defined
4357 * in terms of the parameters using an equality,
4358 * extract an isl_multi_aff that expresses the set dimensions in terms
4359 * of the parameters.
4361 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4362 __isl_take isl_basic_set *bset)
4364 return extract_isl_multi_aff_from_basic_map(bset);
4367 /* Create an isl_pw_multi_aff that is equivalent to
4368 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4369 * The given basic map is such that each output dimension is defined
4370 * in terms of the parameters and input dimensions using an equality.
4372 * Since some applications expect the result of isl_pw_multi_aff_from_map
4373 * to only contain integer affine expressions, we compute the floor
4374 * of the expression before returning.
4376 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4377 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4379 isl_multi_aff *ma;
4381 ma = extract_isl_multi_aff_from_basic_map(bmap);
4382 ma = isl_multi_aff_floor(ma);
4383 return isl_pw_multi_aff_alloc(domain, ma);
4386 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4387 * This obviously only works if the input "map" is single-valued.
4388 * If so, we compute the lexicographic minimum of the image in the form
4389 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4390 * to its lexicographic minimum.
4391 * If the input is not single-valued, we produce an error.
4393 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4394 __isl_take isl_map *map)
4396 int i;
4397 int sv;
4398 isl_pw_multi_aff *pma;
4400 sv = isl_map_is_single_valued(map);
4401 if (sv < 0)
4402 goto error;
4403 if (!sv)
4404 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4405 "map is not single-valued", goto error);
4406 map = isl_map_make_disjoint(map);
4407 if (!map)
4408 return NULL;
4410 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4412 for (i = 0; i < map->n; ++i) {
4413 isl_pw_multi_aff *pma_i;
4414 isl_basic_map *bmap;
4415 bmap = isl_basic_map_copy(map->p[i]);
4416 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4417 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4420 isl_map_free(map);
4421 return pma;
4422 error:
4423 isl_map_free(map);
4424 return NULL;
4427 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4428 * taking into account that the output dimension at position "d"
4429 * can be represented as
4431 * x = floor((e(...) + c1) / m)
4433 * given that constraint "i" is of the form
4435 * e(...) + c1 - m x >= 0
4438 * Let "map" be of the form
4440 * A -> B
4442 * We construct a mapping
4444 * A -> [A -> x = floor(...)]
4446 * apply that to the map, obtaining
4448 * [A -> x = floor(...)] -> B
4450 * and equate dimension "d" to x.
4451 * We then compute a isl_pw_multi_aff representation of the resulting map
4452 * and plug in the mapping above.
4454 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4455 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4457 isl_ctx *ctx;
4458 isl_space *space;
4459 isl_local_space *ls;
4460 isl_multi_aff *ma;
4461 isl_aff *aff;
4462 isl_vec *v;
4463 isl_map *insert;
4464 int offset;
4465 int n;
4466 int n_in;
4467 isl_pw_multi_aff *pma;
4468 int is_set;
4470 is_set = isl_map_is_set(map);
4472 offset = isl_basic_map_offset(hull, isl_dim_out);
4473 ctx = isl_map_get_ctx(map);
4474 space = isl_space_domain(isl_map_get_space(map));
4475 n_in = isl_space_dim(space, isl_dim_set);
4476 n = isl_space_dim(space, isl_dim_all);
4478 v = isl_vec_alloc(ctx, 1 + 1 + n);
4479 if (v) {
4480 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4481 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4483 isl_basic_map_free(hull);
4485 ls = isl_local_space_from_space(isl_space_copy(space));
4486 aff = isl_aff_alloc_vec(ls, v);
4487 aff = isl_aff_floor(aff);
4488 if (is_set) {
4489 isl_space_free(space);
4490 ma = isl_multi_aff_from_aff(aff);
4491 } else {
4492 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4493 ma = isl_multi_aff_range_product(ma,
4494 isl_multi_aff_from_aff(aff));
4497 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4498 map = isl_map_apply_domain(map, insert);
4499 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4500 pma = isl_pw_multi_aff_from_map(map);
4501 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4503 return pma;
4506 /* Is constraint "c" of the form
4508 * e(...) + c1 - m x >= 0
4510 * or
4512 * -e(...) + c2 + m x >= 0
4514 * where m > 1 and e only depends on parameters and input dimemnsions?
4516 * "offset" is the offset of the output dimensions
4517 * "pos" is the position of output dimension x.
4519 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4521 if (isl_int_is_zero(c[offset + d]))
4522 return 0;
4523 if (isl_int_is_one(c[offset + d]))
4524 return 0;
4525 if (isl_int_is_negone(c[offset + d]))
4526 return 0;
4527 if (isl_seq_first_non_zero(c + offset, d) != -1)
4528 return 0;
4529 if (isl_seq_first_non_zero(c + offset + d + 1,
4530 total - (offset + d + 1)) != -1)
4531 return 0;
4532 return 1;
4535 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4537 * As a special case, we first check if there is any pair of constraints,
4538 * shared by all the basic maps in "map" that force a given dimension
4539 * to be equal to the floor of some affine combination of the input dimensions.
4541 * In particular, if we can find two constraints
4543 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4545 * and
4547 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4549 * where m > 1 and e only depends on parameters and input dimemnsions,
4550 * and such that
4552 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4554 * then we know that we can take
4556 * x = floor((e(...) + c1) / m)
4558 * without having to perform any computation.
4560 * Note that we know that
4562 * c1 + c2 >= 1
4564 * If c1 + c2 were 0, then we would have detected an equality during
4565 * simplification. If c1 + c2 were negative, then we would have detected
4566 * a contradiction.
4568 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4569 __isl_take isl_map *map)
4571 int d, dim;
4572 int i, j, n;
4573 int offset, total;
4574 isl_int sum;
4575 isl_basic_map *hull;
4577 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4578 if (!hull)
4579 goto error;
4581 isl_int_init(sum);
4582 dim = isl_map_dim(map, isl_dim_out);
4583 offset = isl_basic_map_offset(hull, isl_dim_out);
4584 total = 1 + isl_basic_map_total_dim(hull);
4585 n = hull->n_ineq;
4586 for (d = 0; d < dim; ++d) {
4587 for (i = 0; i < n; ++i) {
4588 if (!is_potential_div_constraint(hull->ineq[i],
4589 offset, d, total))
4590 continue;
4591 for (j = i + 1; j < n; ++j) {
4592 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4593 hull->ineq[j] + 1, total - 1))
4594 continue;
4595 isl_int_add(sum, hull->ineq[i][0],
4596 hull->ineq[j][0]);
4597 if (isl_int_abs_lt(sum,
4598 hull->ineq[i][offset + d]))
4599 break;
4602 if (j >= n)
4603 continue;
4604 isl_int_clear(sum);
4605 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4606 j = i;
4607 return pw_multi_aff_from_map_div(map, hull, d, j);
4610 isl_int_clear(sum);
4611 isl_basic_map_free(hull);
4612 return pw_multi_aff_from_map_base(map);
4613 error:
4614 isl_map_free(map);
4615 isl_basic_map_free(hull);
4616 return NULL;
4619 /* Given an affine expression
4621 * [A -> B] -> f(A,B)
4623 * construct an isl_multi_aff
4625 * [A -> B] -> B'
4627 * such that dimension "d" in B' is set to "aff" and the remaining
4628 * dimensions are set equal to the corresponding dimensions in B.
4629 * "n_in" is the dimension of the space A.
4630 * "n_out" is the dimension of the space B.
4632 * If "is_set" is set, then the affine expression is of the form
4634 * [B] -> f(B)
4636 * and we construct an isl_multi_aff
4638 * B -> B'
4640 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4641 unsigned n_in, unsigned n_out, int is_set)
4643 int i;
4644 isl_multi_aff *ma;
4645 isl_space *space, *space2;
4646 isl_local_space *ls;
4648 space = isl_aff_get_domain_space(aff);
4649 ls = isl_local_space_from_space(isl_space_copy(space));
4650 space2 = isl_space_copy(space);
4651 if (!is_set)
4652 space2 = isl_space_range(isl_space_unwrap(space2));
4653 space = isl_space_map_from_domain_and_range(space, space2);
4654 ma = isl_multi_aff_alloc(space);
4655 ma = isl_multi_aff_set_aff(ma, d, aff);
4657 for (i = 0; i < n_out; ++i) {
4658 if (i == d)
4659 continue;
4660 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4661 isl_dim_set, n_in + i);
4662 ma = isl_multi_aff_set_aff(ma, i, aff);
4665 isl_local_space_free(ls);
4667 return ma;
4670 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4671 * taking into account that the dimension at position "d" can be written as
4673 * x = m a + f(..) (1)
4675 * where m is equal to "gcd".
4676 * "i" is the index of the equality in "hull" that defines f(..).
4677 * In particular, the equality is of the form
4679 * f(..) - x + m g(existentials) = 0
4681 * or
4683 * -f(..) + x + m g(existentials) = 0
4685 * We basically plug (1) into "map", resulting in a map with "a"
4686 * in the range instead of "x". The corresponding isl_pw_multi_aff
4687 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4689 * Specifically, given the input map
4691 * A -> B
4693 * We first wrap it into a set
4695 * [A -> B]
4697 * and define (1) on top of the corresponding space, resulting in "aff".
4698 * We use this to create an isl_multi_aff that maps the output position "d"
4699 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4700 * We plug this into the wrapped map, unwrap the result and compute the
4701 * corresponding isl_pw_multi_aff.
4702 * The result is an expression
4704 * A -> T(A)
4706 * We adjust that to
4708 * A -> [A -> T(A)]
4710 * so that we can plug that into "aff", after extending the latter to
4711 * a mapping
4713 * [A -> B] -> B'
4716 * If "map" is actually a set, then there is no "A" space, meaning
4717 * that we do not need to perform any wrapping, and that the result
4718 * of the recursive call is of the form
4720 * [T]
4722 * which is plugged into a mapping of the form
4724 * B -> B'
4726 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4727 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4728 isl_int gcd)
4730 isl_set *set;
4731 isl_space *space;
4732 isl_local_space *ls;
4733 isl_aff *aff;
4734 isl_multi_aff *ma;
4735 isl_pw_multi_aff *pma, *id;
4736 unsigned n_in;
4737 unsigned o_out;
4738 unsigned n_out;
4739 int is_set;
4741 is_set = isl_map_is_set(map);
4743 n_in = isl_basic_map_dim(hull, isl_dim_in);
4744 n_out = isl_basic_map_dim(hull, isl_dim_out);
4745 o_out = isl_basic_map_offset(hull, isl_dim_out);
4747 if (is_set)
4748 set = map;
4749 else
4750 set = isl_map_wrap(map);
4751 space = isl_space_map_from_set(isl_set_get_space(set));
4752 ma = isl_multi_aff_identity(space);
4753 ls = isl_local_space_from_space(isl_set_get_space(set));
4754 aff = isl_aff_alloc(ls);
4755 if (aff) {
4756 isl_int_set_si(aff->v->el[0], 1);
4757 if (isl_int_is_one(hull->eq[i][o_out + d]))
4758 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4759 aff->v->size - 1);
4760 else
4761 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4762 aff->v->size - 1);
4763 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4765 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4766 set = isl_set_preimage_multi_aff(set, ma);
4768 ma = range_map(aff, d, n_in, n_out, is_set);
4770 if (is_set)
4771 map = set;
4772 else
4773 map = isl_set_unwrap(set);
4774 pma = isl_pw_multi_aff_from_map(set);
4776 if (!is_set) {
4777 space = isl_pw_multi_aff_get_domain_space(pma);
4778 space = isl_space_map_from_set(space);
4779 id = isl_pw_multi_aff_identity(space);
4780 pma = isl_pw_multi_aff_range_product(id, pma);
4782 id = isl_pw_multi_aff_from_multi_aff(ma);
4783 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4785 isl_basic_map_free(hull);
4786 return pma;
4789 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4791 * As a special case, we first check if all output dimensions are uniquely
4792 * defined in terms of the parameters and input dimensions over the entire
4793 * domain. If so, we extract the desired isl_pw_multi_aff directly
4794 * from the affine hull of "map" and its domain.
4796 * Otherwise, we check if any of the output dimensions is "strided".
4797 * That is, we check if can be written as
4799 * x = m a + f(..)
4801 * with m greater than 1, a some combination of existentiall quantified
4802 * variables and f and expression in the parameters and input dimensions.
4803 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4805 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4806 * special case.
4808 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4810 int i, j;
4811 int sv;
4812 isl_basic_map *hull;
4813 unsigned n_out;
4814 unsigned o_out;
4815 unsigned n_div;
4816 unsigned o_div;
4817 isl_int gcd;
4819 if (!map)
4820 return NULL;
4822 hull = isl_map_affine_hull(isl_map_copy(map));
4823 sv = isl_basic_map_plain_is_single_valued(hull);
4824 if (sv >= 0 && sv)
4825 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4826 if (sv < 0)
4827 hull = isl_basic_map_free(hull);
4828 if (!hull)
4829 goto error;
4831 n_div = isl_basic_map_dim(hull, isl_dim_div);
4832 o_div = isl_basic_map_offset(hull, isl_dim_div);
4834 if (n_div == 0) {
4835 isl_basic_map_free(hull);
4836 return pw_multi_aff_from_map_check_div(map);
4839 isl_int_init(gcd);
4841 n_out = isl_basic_map_dim(hull, isl_dim_out);
4842 o_out = isl_basic_map_offset(hull, isl_dim_out);
4844 for (i = 0; i < n_out; ++i) {
4845 for (j = 0; j < hull->n_eq; ++j) {
4846 isl_int *eq = hull->eq[j];
4847 isl_pw_multi_aff *res;
4849 if (!isl_int_is_one(eq[o_out + i]) &&
4850 !isl_int_is_negone(eq[o_out + i]))
4851 continue;
4852 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4853 continue;
4854 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4855 n_out - (i + 1)) != -1)
4856 continue;
4857 isl_seq_gcd(eq + o_div, n_div, &gcd);
4858 if (isl_int_is_zero(gcd))
4859 continue;
4860 if (isl_int_is_one(gcd))
4861 continue;
4863 res = pw_multi_aff_from_map_stride(map, hull,
4864 i, j, gcd);
4865 isl_int_clear(gcd);
4866 return res;
4870 isl_int_clear(gcd);
4871 isl_basic_map_free(hull);
4872 return pw_multi_aff_from_map_check_div(map);
4873 error:
4874 isl_map_free(map);
4875 return NULL;
4878 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4880 return isl_pw_multi_aff_from_map(set);
4883 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4884 * add it to *user.
4886 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4888 isl_union_pw_multi_aff **upma = user;
4889 isl_pw_multi_aff *pma;
4891 pma = isl_pw_multi_aff_from_map(map);
4892 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4894 return *upma ? 0 : -1;
4897 /* Try and create an isl_union_pw_multi_aff that is equivalent
4898 * to the given isl_union_map.
4899 * The isl_union_map is required to be single-valued in each space.
4900 * Otherwise, an error is produced.
4902 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4903 __isl_take isl_union_map *umap)
4905 isl_space *space;
4906 isl_union_pw_multi_aff *upma;
4908 space = isl_union_map_get_space(umap);
4909 upma = isl_union_pw_multi_aff_empty(space);
4910 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4911 upma = isl_union_pw_multi_aff_free(upma);
4912 isl_union_map_free(umap);
4914 return upma;
4917 /* Try and create an isl_union_pw_multi_aff that is equivalent
4918 * to the given isl_union_set.
4919 * The isl_union_set is required to be a singleton in each space.
4920 * Otherwise, an error is produced.
4922 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4923 __isl_take isl_union_set *uset)
4925 return isl_union_pw_multi_aff_from_union_map(uset);
4928 /* Return the piecewise affine expression "set ? 1 : 0".
4930 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4932 isl_pw_aff *pa;
4933 isl_space *space = isl_set_get_space(set);
4934 isl_local_space *ls = isl_local_space_from_space(space);
4935 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4936 isl_aff *one = isl_aff_zero_on_domain(ls);
4938 one = isl_aff_add_constant_si(one, 1);
4939 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4940 set = isl_set_complement(set);
4941 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4943 return pa;
4946 /* Plug in "subs" for dimension "type", "pos" of "aff".
4948 * Let i be the dimension to replace and let "subs" be of the form
4950 * f/d
4952 * and "aff" of the form
4954 * (a i + g)/m
4956 * The result is
4958 * (a f + d g')/(m d)
4960 * where g' is the result of plugging in "subs" in each of the integer
4961 * divisions in g.
4963 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4964 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4966 isl_ctx *ctx;
4967 isl_int v;
4969 aff = isl_aff_cow(aff);
4970 if (!aff || !subs)
4971 return isl_aff_free(aff);
4973 ctx = isl_aff_get_ctx(aff);
4974 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4975 isl_die(ctx, isl_error_invalid,
4976 "spaces don't match", return isl_aff_free(aff));
4977 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4978 isl_die(ctx, isl_error_unsupported,
4979 "cannot handle divs yet", return isl_aff_free(aff));
4981 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4982 if (!aff->ls)
4983 return isl_aff_free(aff);
4985 aff->v = isl_vec_cow(aff->v);
4986 if (!aff->v)
4987 return isl_aff_free(aff);
4989 pos += isl_local_space_offset(aff->ls, type);
4991 isl_int_init(v);
4992 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4993 aff->v->size, subs->v->size, v);
4994 isl_int_clear(v);
4996 return aff;
4999 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5000 * expressions in "maff".
5002 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5003 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5004 __isl_keep isl_aff *subs)
5006 int i;
5008 maff = isl_multi_aff_cow(maff);
5009 if (!maff || !subs)
5010 return isl_multi_aff_free(maff);
5012 if (type == isl_dim_in)
5013 type = isl_dim_set;
5015 for (i = 0; i < maff->n; ++i) {
5016 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5017 if (!maff->p[i])
5018 return isl_multi_aff_free(maff);
5021 return maff;
5024 /* Plug in "subs" for dimension "type", "pos" of "pma".
5026 * pma is of the form
5028 * A_i(v) -> M_i(v)
5030 * while subs is of the form
5032 * v' = B_j(v) -> S_j
5034 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5035 * has a contribution in the result, in particular
5037 * C_ij(S_j) -> M_i(S_j)
5039 * Note that plugging in S_j in C_ij may also result in an empty set
5040 * and this contribution should simply be discarded.
5042 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5043 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5044 __isl_keep isl_pw_aff *subs)
5046 int i, j, n;
5047 isl_pw_multi_aff *res;
5049 if (!pma || !subs)
5050 return isl_pw_multi_aff_free(pma);
5052 n = pma->n * subs->n;
5053 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5055 for (i = 0; i < pma->n; ++i) {
5056 for (j = 0; j < subs->n; ++j) {
5057 isl_set *common;
5058 isl_multi_aff *res_ij;
5059 int empty;
5061 common = isl_set_intersect(
5062 isl_set_copy(pma->p[i].set),
5063 isl_set_copy(subs->p[j].set));
5064 common = isl_set_substitute(common,
5065 type, pos, subs->p[j].aff);
5066 empty = isl_set_plain_is_empty(common);
5067 if (empty < 0 || empty) {
5068 isl_set_free(common);
5069 if (empty < 0)
5070 goto error;
5071 continue;
5074 res_ij = isl_multi_aff_substitute(
5075 isl_multi_aff_copy(pma->p[i].maff),
5076 type, pos, subs->p[j].aff);
5078 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5082 isl_pw_multi_aff_free(pma);
5083 return res;
5084 error:
5085 isl_pw_multi_aff_free(pma);
5086 isl_pw_multi_aff_free(res);
5087 return NULL;
5090 /* Compute the preimage of a range of dimensions in the affine expression "src"
5091 * under "ma" and put the result in "dst". The number of dimensions in "src"
5092 * that precede the range is given by "n_before". The number of dimensions
5093 * in the range is given by the number of output dimensions of "ma".
5094 * The number of dimensions that follow the range is given by "n_after".
5095 * If "has_denom" is set (to one),
5096 * then "src" and "dst" have an extra initial denominator.
5097 * "n_div_ma" is the number of existentials in "ma"
5098 * "n_div_bset" is the number of existentials in "src"
5099 * The resulting "dst" (which is assumed to have been allocated by
5100 * the caller) contains coefficients for both sets of existentials,
5101 * first those in "ma" and then those in "src".
5102 * f, c1, c2 and g are temporary objects that have been initialized
5103 * by the caller.
5105 * Let src represent the expression
5107 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5109 * and let ma represent the expressions
5111 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5113 * We start out with the following expression for dst:
5115 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5117 * with the multiplication factor f initially equal to 1
5118 * and f \sum_i b_i v_i kept separately.
5119 * For each x_i that we substitute, we multiply the numerator
5120 * (and denominator) of dst by c_1 = m_i and add the numerator
5121 * of the x_i expression multiplied by c_2 = f b_i,
5122 * after removing the common factors of c_1 and c_2.
5123 * The multiplication factor f also needs to be multiplied by c_1
5124 * for the next x_j, j > i.
5126 void isl_seq_preimage(isl_int *dst, isl_int *src,
5127 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5128 int n_div_ma, int n_div_bmap,
5129 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5131 int i;
5132 int n_param, n_in, n_out;
5133 int o_dst, o_src;
5135 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5136 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5137 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5139 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5140 o_dst = o_src = has_denom + 1 + n_param + n_before;
5141 isl_seq_clr(dst + o_dst, n_in);
5142 o_dst += n_in;
5143 o_src += n_out;
5144 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5145 o_dst += n_after;
5146 o_src += n_after;
5147 isl_seq_clr(dst + o_dst, n_div_ma);
5148 o_dst += n_div_ma;
5149 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5151 isl_int_set_si(f, 1);
5153 for (i = 0; i < n_out; ++i) {
5154 int offset = has_denom + 1 + n_param + n_before + i;
5156 if (isl_int_is_zero(src[offset]))
5157 continue;
5158 isl_int_set(c1, ma->p[i]->v->el[0]);
5159 isl_int_mul(c2, f, src[offset]);
5160 isl_int_gcd(g, c1, c2);
5161 isl_int_divexact(c1, c1, g);
5162 isl_int_divexact(c2, c2, g);
5164 isl_int_mul(f, f, c1);
5165 o_dst = has_denom;
5166 o_src = 1;
5167 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5168 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5169 o_dst += 1 + n_param;
5170 o_src += 1 + n_param;
5171 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5172 o_dst += n_before;
5173 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5174 c2, ma->p[i]->v->el + o_src, n_in);
5175 o_dst += n_in;
5176 o_src += n_in;
5177 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5178 o_dst += n_after;
5179 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5180 c2, ma->p[i]->v->el + o_src, n_div_ma);
5181 o_dst += n_div_ma;
5182 o_src += n_div_ma;
5183 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5184 if (has_denom)
5185 isl_int_mul(dst[0], dst[0], c1);
5189 /* Compute the pullback of "aff" by the function represented by "ma".
5190 * In other words, plug in "ma" in "aff". The result is an affine expression
5191 * defined over the domain space of "ma".
5193 * If "aff" is represented by
5195 * (a(p) + b x + c(divs))/d
5197 * and ma is represented by
5199 * x = D(p) + F(y) + G(divs')
5201 * then the result is
5203 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5205 * The divs in the local space of the input are similarly adjusted
5206 * through a call to isl_local_space_preimage_multi_aff.
5208 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5209 __isl_take isl_multi_aff *ma)
5211 isl_aff *res = NULL;
5212 isl_local_space *ls;
5213 int n_div_aff, n_div_ma;
5214 isl_int f, c1, c2, g;
5216 ma = isl_multi_aff_align_divs(ma);
5217 if (!aff || !ma)
5218 goto error;
5220 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5221 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5223 ls = isl_aff_get_domain_local_space(aff);
5224 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5225 res = isl_aff_alloc(ls);
5226 if (!res)
5227 goto error;
5229 isl_int_init(f);
5230 isl_int_init(c1);
5231 isl_int_init(c2);
5232 isl_int_init(g);
5234 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5235 f, c1, c2, g, 1);
5237 isl_int_clear(f);
5238 isl_int_clear(c1);
5239 isl_int_clear(c2);
5240 isl_int_clear(g);
5242 isl_aff_free(aff);
5243 isl_multi_aff_free(ma);
5244 res = isl_aff_normalize(res);
5245 return res;
5246 error:
5247 isl_aff_free(aff);
5248 isl_multi_aff_free(ma);
5249 isl_aff_free(res);
5250 return NULL;
5253 /* Compute the pullback of "aff1" by the function represented by "aff2".
5254 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5255 * defined over the domain space of "aff1".
5257 * The domain of "aff1" should match the range of "aff2", which means
5258 * that it should be single-dimensional.
5260 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5261 __isl_take isl_aff *aff2)
5263 isl_multi_aff *ma;
5265 ma = isl_multi_aff_from_aff(aff2);
5266 return isl_aff_pullback_multi_aff(aff1, ma);
5269 /* Compute the pullback of "ma1" by the function represented by "ma2".
5270 * In other words, plug in "ma2" in "ma1".
5272 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5274 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5275 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5277 int i;
5278 isl_space *space = NULL;
5280 ma2 = isl_multi_aff_align_divs(ma2);
5281 ma1 = isl_multi_aff_cow(ma1);
5282 if (!ma1 || !ma2)
5283 goto error;
5285 space = isl_space_join(isl_multi_aff_get_space(ma2),
5286 isl_multi_aff_get_space(ma1));
5288 for (i = 0; i < ma1->n; ++i) {
5289 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5290 isl_multi_aff_copy(ma2));
5291 if (!ma1->p[i])
5292 goto error;
5295 ma1 = isl_multi_aff_reset_space(ma1, space);
5296 isl_multi_aff_free(ma2);
5297 return ma1;
5298 error:
5299 isl_space_free(space);
5300 isl_multi_aff_free(ma2);
5301 isl_multi_aff_free(ma1);
5302 return NULL;
5305 /* Compute the pullback of "ma1" by the function represented by "ma2".
5306 * In other words, plug in "ma2" in "ma1".
5308 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5309 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5311 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5312 &isl_multi_aff_pullback_multi_aff_aligned);
5315 /* Extend the local space of "dst" to include the divs
5316 * in the local space of "src".
5318 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5319 __isl_keep isl_aff *src)
5321 isl_ctx *ctx;
5322 int *exp1 = NULL;
5323 int *exp2 = NULL;
5324 isl_mat *div;
5326 if (!src || !dst)
5327 return isl_aff_free(dst);
5329 ctx = isl_aff_get_ctx(src);
5330 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5331 isl_die(ctx, isl_error_invalid,
5332 "spaces don't match", goto error);
5334 if (src->ls->div->n_row == 0)
5335 return dst;
5337 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5338 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5339 if (!exp1 || (dst->ls->div->n_row && !exp2))
5340 goto error;
5342 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5343 dst = isl_aff_expand_divs(dst, div, exp2);
5344 free(exp1);
5345 free(exp2);
5347 return dst;
5348 error:
5349 free(exp1);
5350 free(exp2);
5351 return isl_aff_free(dst);
5354 /* Adjust the local spaces of the affine expressions in "maff"
5355 * such that they all have the save divs.
5357 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5358 __isl_take isl_multi_aff *maff)
5360 int i;
5362 if (!maff)
5363 return NULL;
5364 if (maff->n == 0)
5365 return maff;
5366 maff = isl_multi_aff_cow(maff);
5367 if (!maff)
5368 return NULL;
5370 for (i = 1; i < maff->n; ++i)
5371 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5372 for (i = 1; i < maff->n; ++i) {
5373 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5374 if (!maff->p[i])
5375 return isl_multi_aff_free(maff);
5378 return maff;
5381 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5383 aff = isl_aff_cow(aff);
5384 if (!aff)
5385 return NULL;
5387 aff->ls = isl_local_space_lift(aff->ls);
5388 if (!aff->ls)
5389 return isl_aff_free(aff);
5391 return aff;
5394 /* Lift "maff" to a space with extra dimensions such that the result
5395 * has no more existentially quantified variables.
5396 * If "ls" is not NULL, then *ls is assigned the local space that lies
5397 * at the basis of the lifting applied to "maff".
5399 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5400 __isl_give isl_local_space **ls)
5402 int i;
5403 isl_space *space;
5404 unsigned n_div;
5406 if (ls)
5407 *ls = NULL;
5409 if (!maff)
5410 return NULL;
5412 if (maff->n == 0) {
5413 if (ls) {
5414 isl_space *space = isl_multi_aff_get_domain_space(maff);
5415 *ls = isl_local_space_from_space(space);
5416 if (!*ls)
5417 return isl_multi_aff_free(maff);
5419 return maff;
5422 maff = isl_multi_aff_cow(maff);
5423 maff = isl_multi_aff_align_divs(maff);
5424 if (!maff)
5425 return NULL;
5427 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5428 space = isl_multi_aff_get_space(maff);
5429 space = isl_space_lift(isl_space_domain(space), n_div);
5430 space = isl_space_extend_domain_with_range(space,
5431 isl_multi_aff_get_space(maff));
5432 if (!space)
5433 return isl_multi_aff_free(maff);
5434 isl_space_free(maff->space);
5435 maff->space = space;
5437 if (ls) {
5438 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5439 if (!*ls)
5440 return isl_multi_aff_free(maff);
5443 for (i = 0; i < maff->n; ++i) {
5444 maff->p[i] = isl_aff_lift(maff->p[i]);
5445 if (!maff->p[i])
5446 goto error;
5449 return maff;
5450 error:
5451 if (ls)
5452 isl_local_space_free(*ls);
5453 return isl_multi_aff_free(maff);
5457 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5459 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5460 __isl_keep isl_pw_multi_aff *pma, int pos)
5462 int i;
5463 int n_out;
5464 isl_space *space;
5465 isl_pw_aff *pa;
5467 if (!pma)
5468 return NULL;
5470 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5471 if (pos < 0 || pos >= n_out)
5472 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5473 "index out of bounds", return NULL);
5475 space = isl_pw_multi_aff_get_space(pma);
5476 space = isl_space_drop_dims(space, isl_dim_out,
5477 pos + 1, n_out - pos - 1);
5478 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5480 pa = isl_pw_aff_alloc_size(space, pma->n);
5481 for (i = 0; i < pma->n; ++i) {
5482 isl_aff *aff;
5483 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5484 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5487 return pa;
5490 /* Return an isl_pw_multi_aff with the given "set" as domain and
5491 * an unnamed zero-dimensional range.
5493 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5494 __isl_take isl_set *set)
5496 isl_multi_aff *ma;
5497 isl_space *space;
5499 space = isl_set_get_space(set);
5500 space = isl_space_from_domain(space);
5501 ma = isl_multi_aff_zero(space);
5502 return isl_pw_multi_aff_alloc(set, ma);
5505 /* Add an isl_pw_multi_aff with the given "set" as domain and
5506 * an unnamed zero-dimensional range to *user.
5508 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5510 isl_union_pw_multi_aff **upma = user;
5511 isl_pw_multi_aff *pma;
5513 pma = isl_pw_multi_aff_from_domain(set);
5514 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5516 return 0;
5519 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5520 * an unnamed zero-dimensional range.
5522 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5523 __isl_take isl_union_set *uset)
5525 isl_space *space;
5526 isl_union_pw_multi_aff *upma;
5528 if (!uset)
5529 return NULL;
5531 space = isl_union_set_get_space(uset);
5532 upma = isl_union_pw_multi_aff_empty(space);
5534 if (isl_union_set_foreach_set(uset,
5535 &add_pw_multi_aff_from_domain, &upma) < 0)
5536 goto error;
5538 isl_union_set_free(uset);
5539 return upma;
5540 error:
5541 isl_union_set_free(uset);
5542 isl_union_pw_multi_aff_free(upma);
5543 return NULL;
5546 /* Convert "pma" to an isl_map and add it to *umap.
5548 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5550 isl_union_map **umap = user;
5551 isl_map *map;
5553 map = isl_map_from_pw_multi_aff(pma);
5554 *umap = isl_union_map_add_map(*umap, map);
5556 return 0;
5559 /* Construct a union map mapping the domain of the union
5560 * piecewise multi-affine expression to its range, with each dimension
5561 * in the range equated to the corresponding affine expression on its cell.
5563 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5564 __isl_take isl_union_pw_multi_aff *upma)
5566 isl_space *space;
5567 isl_union_map *umap;
5569 if (!upma)
5570 return NULL;
5572 space = isl_union_pw_multi_aff_get_space(upma);
5573 umap = isl_union_map_empty(space);
5575 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5576 &map_from_pw_multi_aff, &umap) < 0)
5577 goto error;
5579 isl_union_pw_multi_aff_free(upma);
5580 return umap;
5581 error:
5582 isl_union_pw_multi_aff_free(upma);
5583 isl_union_map_free(umap);
5584 return NULL;
5587 /* Local data for bin_entry and the callback "fn".
5589 struct isl_union_pw_multi_aff_bin_data {
5590 isl_union_pw_multi_aff *upma2;
5591 isl_union_pw_multi_aff *res;
5592 isl_pw_multi_aff *pma;
5593 int (*fn)(void **entry, void *user);
5596 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5597 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5599 static int bin_entry(void **entry, void *user)
5601 struct isl_union_pw_multi_aff_bin_data *data = user;
5602 isl_pw_multi_aff *pma = *entry;
5604 data->pma = pma;
5605 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5606 data->fn, data) < 0)
5607 return -1;
5609 return 0;
5612 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5613 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5614 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5615 * as *entry. The callback should adjust data->res if desired.
5617 static __isl_give isl_union_pw_multi_aff *bin_op(
5618 __isl_take isl_union_pw_multi_aff *upma1,
5619 __isl_take isl_union_pw_multi_aff *upma2,
5620 int (*fn)(void **entry, void *user))
5622 isl_space *space;
5623 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5625 space = isl_union_pw_multi_aff_get_space(upma2);
5626 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5627 space = isl_union_pw_multi_aff_get_space(upma1);
5628 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5630 if (!upma1 || !upma2)
5631 goto error;
5633 data.upma2 = upma2;
5634 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5635 upma1->table.n);
5636 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5637 &bin_entry, &data) < 0)
5638 goto error;
5640 isl_union_pw_multi_aff_free(upma1);
5641 isl_union_pw_multi_aff_free(upma2);
5642 return data.res;
5643 error:
5644 isl_union_pw_multi_aff_free(upma1);
5645 isl_union_pw_multi_aff_free(upma2);
5646 isl_union_pw_multi_aff_free(data.res);
5647 return NULL;
5650 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5651 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5653 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5654 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5656 isl_space *space;
5658 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5659 isl_pw_multi_aff_get_space(pma2));
5660 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5661 &isl_multi_aff_range_product);
5664 /* Given two isl_pw_multi_affs A -> B and C -> D,
5665 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5667 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5668 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5670 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5671 &pw_multi_aff_range_product);
5674 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5675 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5677 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5678 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5680 isl_space *space;
5682 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5683 isl_pw_multi_aff_get_space(pma2));
5684 space = isl_space_flatten_range(space);
5685 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5686 &isl_multi_aff_flat_range_product);
5689 /* Given two isl_pw_multi_affs A -> B and C -> D,
5690 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5692 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5693 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5695 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5696 &pw_multi_aff_flat_range_product);
5699 /* If data->pma and *entry have the same domain space, then compute
5700 * their flat range product and the result to data->res.
5702 static int flat_range_product_entry(void **entry, void *user)
5704 struct isl_union_pw_multi_aff_bin_data *data = user;
5705 isl_pw_multi_aff *pma2 = *entry;
5707 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5708 pma2->dim, isl_dim_in))
5709 return 0;
5711 pma2 = isl_pw_multi_aff_flat_range_product(
5712 isl_pw_multi_aff_copy(data->pma),
5713 isl_pw_multi_aff_copy(pma2));
5715 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5717 return 0;
5720 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5721 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5723 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5724 __isl_take isl_union_pw_multi_aff *upma1,
5725 __isl_take isl_union_pw_multi_aff *upma2)
5727 return bin_op(upma1, upma2, &flat_range_product_entry);
5730 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5731 * The parameters are assumed to have been aligned.
5733 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5734 * except that it works on two different isl_pw_* types.
5736 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5737 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5738 __isl_take isl_pw_aff *pa)
5740 int i, j, n;
5741 isl_pw_multi_aff *res = NULL;
5743 if (!pma || !pa)
5744 goto error;
5746 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5747 pa->dim, isl_dim_in))
5748 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5749 "domains don't match", goto error);
5750 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5751 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5752 "index out of bounds", goto error);
5754 n = pma->n * pa->n;
5755 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5757 for (i = 0; i < pma->n; ++i) {
5758 for (j = 0; j < pa->n; ++j) {
5759 isl_set *common;
5760 isl_multi_aff *res_ij;
5761 int empty;
5763 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5764 isl_set_copy(pa->p[j].set));
5765 empty = isl_set_plain_is_empty(common);
5766 if (empty < 0 || empty) {
5767 isl_set_free(common);
5768 if (empty < 0)
5769 goto error;
5770 continue;
5773 res_ij = isl_multi_aff_set_aff(
5774 isl_multi_aff_copy(pma->p[i].maff), pos,
5775 isl_aff_copy(pa->p[j].aff));
5776 res_ij = isl_multi_aff_gist(res_ij,
5777 isl_set_copy(common));
5779 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5783 isl_pw_multi_aff_free(pma);
5784 isl_pw_aff_free(pa);
5785 return res;
5786 error:
5787 isl_pw_multi_aff_free(pma);
5788 isl_pw_aff_free(pa);
5789 return isl_pw_multi_aff_free(res);
5792 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5794 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5795 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5796 __isl_take isl_pw_aff *pa)
5798 if (!pma || !pa)
5799 goto error;
5800 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5801 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5802 if (!isl_space_has_named_params(pma->dim) ||
5803 !isl_space_has_named_params(pa->dim))
5804 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5805 "unaligned unnamed parameters", goto error);
5806 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5807 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5808 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5809 error:
5810 isl_pw_multi_aff_free(pma);
5811 isl_pw_aff_free(pa);
5812 return NULL;
5815 /* Do the parameters of "pa" match those of "space"?
5817 int isl_pw_aff_matching_params(__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);
5830 isl_space_free(pa_space);
5831 return match;
5834 /* Check that the domain space of "pa" matches "space".
5836 * Return 0 on success and -1 on error.
5838 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5839 __isl_keep isl_space *space)
5841 isl_space *pa_space;
5842 int match;
5844 if (!pa || !space)
5845 return -1;
5847 pa_space = isl_pw_aff_get_space(pa);
5849 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5850 if (match < 0)
5851 goto error;
5852 if (!match)
5853 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5854 "parameters don't match", goto error);
5855 match = isl_space_tuple_is_equal(space, isl_dim_in,
5856 pa_space, isl_dim_in);
5857 if (match < 0)
5858 goto error;
5859 if (!match)
5860 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5861 "domains don't match", goto error);
5862 isl_space_free(pa_space);
5863 return 0;
5864 error:
5865 isl_space_free(pa_space);
5866 return -1;
5869 #undef BASE
5870 #define BASE pw_aff
5872 #include <isl_multi_templ.c>
5874 /* Scale the elements of "pma" by the corresponding elements of "mv".
5876 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5877 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5879 int i;
5881 pma = isl_pw_multi_aff_cow(pma);
5882 if (!pma || !mv)
5883 goto error;
5884 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5885 mv->space, isl_dim_set))
5886 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5887 "spaces don't match", goto error);
5888 if (!isl_space_match(pma->dim, isl_dim_param,
5889 mv->space, isl_dim_param)) {
5890 pma = isl_pw_multi_aff_align_params(pma,
5891 isl_multi_val_get_space(mv));
5892 mv = isl_multi_val_align_params(mv,
5893 isl_pw_multi_aff_get_space(pma));
5894 if (!pma || !mv)
5895 goto error;
5898 for (i = 0; i < pma->n; ++i) {
5899 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5900 isl_multi_val_copy(mv));
5901 if (!pma->p[i].maff)
5902 goto error;
5905 isl_multi_val_free(mv);
5906 return pma;
5907 error:
5908 isl_multi_val_free(mv);
5909 isl_pw_multi_aff_free(pma);
5910 return NULL;
5913 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5914 * mv contains the mv argument.
5915 * res collects the results.
5917 struct isl_union_pw_multi_aff_scale_multi_val_data {
5918 isl_multi_val *mv;
5919 isl_union_pw_multi_aff *res;
5922 /* This function is called for each entry of an isl_union_pw_multi_aff.
5923 * If the space of the entry matches that of data->mv,
5924 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5925 * to data->res.
5927 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5929 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5930 isl_pw_multi_aff *pma = *entry;
5932 if (!pma)
5933 return -1;
5934 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5935 data->mv->space, isl_dim_set))
5936 return 0;
5938 pma = isl_pw_multi_aff_copy(pma);
5939 pma = isl_pw_multi_aff_scale_multi_val(pma,
5940 isl_multi_val_copy(data->mv));
5941 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5942 if (!data->res)
5943 return -1;
5945 return 0;
5948 /* Scale the elements of "upma" by the corresponding elements of "mv",
5949 * for those entries that match the space of "mv".
5951 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5952 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5954 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5956 upma = isl_union_pw_multi_aff_align_params(upma,
5957 isl_multi_val_get_space(mv));
5958 mv = isl_multi_val_align_params(mv,
5959 isl_union_pw_multi_aff_get_space(upma));
5960 if (!upma || !mv)
5961 goto error;
5963 data.mv = mv;
5964 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
5965 upma->table.n);
5966 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
5967 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5968 goto error;
5970 isl_multi_val_free(mv);
5971 isl_union_pw_multi_aff_free(upma);
5972 return data.res;
5973 error:
5974 isl_multi_val_free(mv);
5975 isl_union_pw_multi_aff_free(upma);
5976 return NULL;
5979 /* Construct and return a piecewise multi affine expression
5980 * in the given space with value zero in each of the output dimensions and
5981 * a universe domain.
5983 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5985 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5988 /* Construct and return a piecewise multi affine expression
5989 * that is equal to the given piecewise affine expression.
5991 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5992 __isl_take isl_pw_aff *pa)
5994 int i;
5995 isl_space *space;
5996 isl_pw_multi_aff *pma;
5998 if (!pa)
5999 return NULL;
6001 space = isl_pw_aff_get_space(pa);
6002 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6004 for (i = 0; i < pa->n; ++i) {
6005 isl_set *set;
6006 isl_multi_aff *ma;
6008 set = isl_set_copy(pa->p[i].set);
6009 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6010 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6013 isl_pw_aff_free(pa);
6014 return pma;
6017 /* Construct a set or map mapping the shared (parameter) domain
6018 * of the piecewise affine expressions to the range of "mpa"
6019 * with each dimension in the range equated to the
6020 * corresponding piecewise affine expression.
6022 static __isl_give isl_map *map_from_multi_pw_aff(
6023 __isl_take isl_multi_pw_aff *mpa)
6025 int i;
6026 isl_space *space;
6027 isl_map *map;
6029 if (!mpa)
6030 return NULL;
6032 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6033 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6034 "invalid space", goto error);
6036 space = isl_multi_pw_aff_get_domain_space(mpa);
6037 map = isl_map_universe(isl_space_from_domain(space));
6039 for (i = 0; i < mpa->n; ++i) {
6040 isl_pw_aff *pa;
6041 isl_map *map_i;
6043 pa = isl_pw_aff_copy(mpa->p[i]);
6044 map_i = map_from_pw_aff(pa);
6046 map = isl_map_flat_range_product(map, map_i);
6049 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6051 isl_multi_pw_aff_free(mpa);
6052 return map;
6053 error:
6054 isl_multi_pw_aff_free(mpa);
6055 return NULL;
6058 /* Construct a map mapping the shared domain
6059 * of the piecewise affine expressions to the range of "mpa"
6060 * with each dimension in the range equated to the
6061 * corresponding piecewise affine expression.
6063 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6065 if (!mpa)
6066 return NULL;
6067 if (isl_space_is_set(mpa->space))
6068 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6069 "space of input is not a map", goto error);
6071 return map_from_multi_pw_aff(mpa);
6072 error:
6073 isl_multi_pw_aff_free(mpa);
6074 return NULL;
6077 /* Construct a set mapping the shared parameter domain
6078 * of the piecewise affine expressions to the space of "mpa"
6079 * with each dimension in the range equated to the
6080 * corresponding piecewise affine expression.
6082 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6084 if (!mpa)
6085 return NULL;
6086 if (!isl_space_is_set(mpa->space))
6087 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6088 "space of input is not a set", goto error);
6090 return map_from_multi_pw_aff(mpa);
6091 error:
6092 isl_multi_pw_aff_free(mpa);
6093 return NULL;
6096 /* Construct and return a piecewise multi affine expression
6097 * that is equal to the given multi piecewise affine expression
6098 * on the shared domain of the piecewise affine expressions.
6100 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6101 __isl_take isl_multi_pw_aff *mpa)
6103 int i;
6104 isl_space *space;
6105 isl_pw_aff *pa;
6106 isl_pw_multi_aff *pma;
6108 if (!mpa)
6109 return NULL;
6111 space = isl_multi_pw_aff_get_space(mpa);
6113 if (mpa->n == 0) {
6114 isl_multi_pw_aff_free(mpa);
6115 return isl_pw_multi_aff_zero(space);
6118 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6119 pma = isl_pw_multi_aff_from_pw_aff(pa);
6121 for (i = 1; i < mpa->n; ++i) {
6122 isl_pw_multi_aff *pma_i;
6124 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6125 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6126 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6129 pma = isl_pw_multi_aff_reset_space(pma, space);
6131 isl_multi_pw_aff_free(mpa);
6132 return pma;
6135 /* Construct and return a multi piecewise affine expression
6136 * that is equal to the given multi affine expression.
6138 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6139 __isl_take isl_multi_aff *ma)
6141 int i, n;
6142 isl_multi_pw_aff *mpa;
6144 if (!ma)
6145 return NULL;
6147 n = isl_multi_aff_dim(ma, isl_dim_out);
6148 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6150 for (i = 0; i < n; ++i) {
6151 isl_pw_aff *pa;
6153 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6154 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6157 isl_multi_aff_free(ma);
6158 return mpa;
6161 /* Construct and return a multi piecewise affine expression
6162 * that is equal to the given piecewise multi affine expression.
6164 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6165 __isl_take isl_pw_multi_aff *pma)
6167 int i, n;
6168 isl_space *space;
6169 isl_multi_pw_aff *mpa;
6171 if (!pma)
6172 return NULL;
6174 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6175 space = isl_pw_multi_aff_get_space(pma);
6176 mpa = isl_multi_pw_aff_alloc(space);
6178 for (i = 0; i < n; ++i) {
6179 isl_pw_aff *pa;
6181 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6182 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6185 isl_pw_multi_aff_free(pma);
6186 return mpa;
6189 /* Do "pa1" and "pa2" represent the same function?
6191 * We first check if they are obviously equal.
6192 * If not, we convert them to maps and check if those are equal.
6194 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6196 int equal;
6197 isl_map *map1, *map2;
6199 if (!pa1 || !pa2)
6200 return -1;
6202 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6203 if (equal < 0 || equal)
6204 return equal;
6206 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6207 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6208 equal = isl_map_is_equal(map1, map2);
6209 isl_map_free(map1);
6210 isl_map_free(map2);
6212 return equal;
6215 /* Do "mpa1" and "mpa2" represent the same function?
6217 * Note that we cannot convert the entire isl_multi_pw_aff
6218 * to a map because the domains of the piecewise affine expressions
6219 * may not be the same.
6221 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6222 __isl_keep isl_multi_pw_aff *mpa2)
6224 int i;
6225 int equal;
6227 if (!mpa1 || !mpa2)
6228 return -1;
6230 if (!isl_space_match(mpa1->space, isl_dim_param,
6231 mpa2->space, isl_dim_param)) {
6232 if (!isl_space_has_named_params(mpa1->space))
6233 return 0;
6234 if (!isl_space_has_named_params(mpa2->space))
6235 return 0;
6236 mpa1 = isl_multi_pw_aff_copy(mpa1);
6237 mpa2 = isl_multi_pw_aff_copy(mpa2);
6238 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6239 isl_multi_pw_aff_get_space(mpa2));
6240 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6241 isl_multi_pw_aff_get_space(mpa1));
6242 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6243 isl_multi_pw_aff_free(mpa1);
6244 isl_multi_pw_aff_free(mpa2);
6245 return equal;
6248 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6249 if (equal < 0 || !equal)
6250 return equal;
6252 for (i = 0; i < mpa1->n; ++i) {
6253 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6254 if (equal < 0 || !equal)
6255 return equal;
6258 return 1;
6261 /* Coalesce the elements of "mpa".
6263 * Note that such coalescing does not change the meaning of "mpa"
6264 * so there is no need to cow. We do need to be careful not to
6265 * destroy any other copies of "mpa" in case of failure.
6267 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6268 __isl_take isl_multi_pw_aff *mpa)
6270 int i;
6272 if (!mpa)
6273 return NULL;
6275 for (i = 0; i < mpa->n; ++i) {
6276 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6277 pa = isl_pw_aff_coalesce(pa);
6278 if (!pa)
6279 return isl_multi_pw_aff_free(mpa);
6280 isl_pw_aff_free(mpa->p[i]);
6281 mpa->p[i] = pa;
6284 return mpa;
6287 /* Compute the pullback of "mpa" by the function represented by "ma".
6288 * In other words, plug in "ma" in "mpa".
6290 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6292 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6293 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6295 int i;
6296 isl_space *space = NULL;
6298 mpa = isl_multi_pw_aff_cow(mpa);
6299 if (!mpa || !ma)
6300 goto error;
6302 space = isl_space_join(isl_multi_aff_get_space(ma),
6303 isl_multi_pw_aff_get_space(mpa));
6304 if (!space)
6305 goto error;
6307 for (i = 0; i < mpa->n; ++i) {
6308 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6309 isl_multi_aff_copy(ma));
6310 if (!mpa->p[i])
6311 goto error;
6314 isl_multi_aff_free(ma);
6315 isl_space_free(mpa->space);
6316 mpa->space = space;
6317 return mpa;
6318 error:
6319 isl_space_free(space);
6320 isl_multi_pw_aff_free(mpa);
6321 isl_multi_aff_free(ma);
6322 return NULL;
6325 /* Compute the pullback of "mpa" by the function represented by "ma".
6326 * In other words, plug in "ma" in "mpa".
6328 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6329 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6331 if (!mpa || !ma)
6332 goto error;
6333 if (isl_space_match(mpa->space, isl_dim_param,
6334 ma->space, isl_dim_param))
6335 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6336 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6337 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6338 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6339 error:
6340 isl_multi_pw_aff_free(mpa);
6341 isl_multi_aff_free(ma);
6342 return NULL;
6345 /* Compute the pullback of "mpa" by the function represented by "pma".
6346 * In other words, plug in "pma" in "mpa".
6348 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6350 static __isl_give isl_multi_pw_aff *
6351 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6352 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6354 int i;
6355 isl_space *space = NULL;
6357 mpa = isl_multi_pw_aff_cow(mpa);
6358 if (!mpa || !pma)
6359 goto error;
6361 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6362 isl_multi_pw_aff_get_space(mpa));
6364 for (i = 0; i < mpa->n; ++i) {
6365 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6366 isl_pw_multi_aff_copy(pma));
6367 if (!mpa->p[i])
6368 goto error;
6371 isl_pw_multi_aff_free(pma);
6372 isl_space_free(mpa->space);
6373 mpa->space = space;
6374 return mpa;
6375 error:
6376 isl_space_free(space);
6377 isl_multi_pw_aff_free(mpa);
6378 isl_pw_multi_aff_free(pma);
6379 return NULL;
6382 /* Compute the pullback of "mpa" by the function represented by "pma".
6383 * In other words, plug in "pma" in "mpa".
6385 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6386 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6388 if (!mpa || !pma)
6389 goto error;
6390 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6391 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6392 mpa = isl_multi_pw_aff_align_params(mpa,
6393 isl_pw_multi_aff_get_space(pma));
6394 pma = isl_pw_multi_aff_align_params(pma,
6395 isl_multi_pw_aff_get_space(mpa));
6396 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6397 error:
6398 isl_multi_pw_aff_free(mpa);
6399 isl_pw_multi_aff_free(pma);
6400 return NULL;
6403 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6404 * with the domain of "aff". The domain of the result is the same
6405 * as that of "mpa".
6406 * "mpa" and "aff" are assumed to have been aligned.
6408 * We first extract the parametric constant from "aff", defined
6409 * over the correct domain.
6410 * Then we add the appropriate combinations of the members of "mpa".
6411 * Finally, we add the integer divisions through recursive calls.
6413 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6414 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6416 int i, n_param, n_in, n_div;
6417 isl_space *space;
6418 isl_val *v;
6419 isl_pw_aff *pa;
6420 isl_aff *tmp;
6422 n_param = isl_aff_dim(aff, isl_dim_param);
6423 n_in = isl_aff_dim(aff, isl_dim_in);
6424 n_div = isl_aff_dim(aff, isl_dim_div);
6426 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6427 tmp = isl_aff_copy(aff);
6428 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6429 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6430 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6431 isl_space_dim(space, isl_dim_set));
6432 tmp = isl_aff_reset_domain_space(tmp, space);
6433 pa = isl_pw_aff_from_aff(tmp);
6435 for (i = 0; i < n_in; ++i) {
6436 isl_pw_aff *pa_i;
6438 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6439 continue;
6440 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6441 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6442 pa_i = isl_pw_aff_scale_val(pa_i, v);
6443 pa = isl_pw_aff_add(pa, pa_i);
6446 for (i = 0; i < n_div; ++i) {
6447 isl_aff *div;
6448 isl_pw_aff *pa_i;
6450 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6451 continue;
6452 div = isl_aff_get_div(aff, i);
6453 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6454 isl_multi_pw_aff_copy(mpa), div);
6455 pa_i = isl_pw_aff_floor(pa_i);
6456 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6457 pa_i = isl_pw_aff_scale_val(pa_i, v);
6458 pa = isl_pw_aff_add(pa, pa_i);
6461 isl_multi_pw_aff_free(mpa);
6462 isl_aff_free(aff);
6464 return pa;
6467 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6468 * with the domain of "aff". The domain of the result is the same
6469 * as that of "mpa".
6471 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6472 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6474 if (!aff || !mpa)
6475 goto error;
6476 if (isl_space_match(aff->ls->dim, isl_dim_param,
6477 mpa->space, isl_dim_param))
6478 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6480 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6481 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6483 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6484 error:
6485 isl_aff_free(aff);
6486 isl_multi_pw_aff_free(mpa);
6487 return NULL;
6490 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6491 * with the domain of "pa". The domain of the result is the same
6492 * as that of "mpa".
6493 * "mpa" and "pa" are assumed to have been aligned.
6495 * We consider each piece in turn. Note that the domains of the
6496 * pieces are assumed to be disjoint and they remain disjoint
6497 * after taking the preimage (over the same function).
6499 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6500 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6502 isl_space *space;
6503 isl_pw_aff *res;
6504 int i;
6506 if (!mpa || !pa)
6507 goto error;
6509 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6510 isl_pw_aff_get_space(pa));
6511 res = isl_pw_aff_empty(space);
6513 for (i = 0; i < pa->n; ++i) {
6514 isl_pw_aff *pa_i;
6515 isl_set *domain;
6517 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6518 isl_multi_pw_aff_copy(mpa),
6519 isl_aff_copy(pa->p[i].aff));
6520 domain = isl_set_copy(pa->p[i].set);
6521 domain = isl_set_preimage_multi_pw_aff(domain,
6522 isl_multi_pw_aff_copy(mpa));
6523 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6524 res = isl_pw_aff_add_disjoint(res, pa_i);
6527 isl_pw_aff_free(pa);
6528 isl_multi_pw_aff_free(mpa);
6529 return res;
6530 error:
6531 isl_pw_aff_free(pa);
6532 isl_multi_pw_aff_free(mpa);
6533 return NULL;
6536 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6537 * with the domain of "pa". The domain of the result is the same
6538 * as that of "mpa".
6540 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6541 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6543 if (!pa || !mpa)
6544 goto error;
6545 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6546 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6548 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6549 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6551 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6552 error:
6553 isl_pw_aff_free(pa);
6554 isl_multi_pw_aff_free(mpa);
6555 return NULL;
6558 /* Compute the pullback of "pa" by the function represented by "mpa".
6559 * In other words, plug in "mpa" in "pa".
6560 * "pa" and "mpa" are assumed to have been aligned.
6562 * The pullback is computed by applying "pa" to "mpa".
6564 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6565 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6567 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6570 /* Compute the pullback of "pa" by the function represented by "mpa".
6571 * In other words, plug in "mpa" in "pa".
6573 * The pullback is computed by applying "pa" to "mpa".
6575 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6576 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6578 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6581 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6582 * In other words, plug in "mpa2" in "mpa1".
6584 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6586 * We pullback each member of "mpa1" in turn.
6588 static __isl_give isl_multi_pw_aff *
6589 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6590 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6592 int i;
6593 isl_space *space = NULL;
6595 mpa1 = isl_multi_pw_aff_cow(mpa1);
6596 if (!mpa1 || !mpa2)
6597 goto error;
6599 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6600 isl_multi_pw_aff_get_space(mpa1));
6602 for (i = 0; i < mpa1->n; ++i) {
6603 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6604 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6605 if (!mpa1->p[i])
6606 goto error;
6609 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6611 isl_multi_pw_aff_free(mpa2);
6612 return mpa1;
6613 error:
6614 isl_space_free(space);
6615 isl_multi_pw_aff_free(mpa1);
6616 isl_multi_pw_aff_free(mpa2);
6617 return NULL;
6620 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6621 * In other words, plug in "mpa2" in "mpa1".
6623 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6624 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6626 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6627 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6630 /* Compare two isl_affs.
6632 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6633 * than "aff2" and 0 if they are equal.
6635 * The order is fairly arbitrary. We do consider expressions that only involve
6636 * earlier dimensions as "smaller".
6638 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6640 int cmp;
6641 int last1, last2;
6643 if (aff1 == aff2)
6644 return 0;
6646 if (!aff1)
6647 return -1;
6648 if (!aff2)
6649 return 1;
6651 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6652 if (cmp != 0)
6653 return cmp;
6655 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6656 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6657 if (last1 != last2)
6658 return last1 - last2;
6660 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6663 /* Compare two isl_pw_affs.
6665 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6666 * than "pa2" and 0 if they are equal.
6668 * The order is fairly arbitrary. We do consider expressions that only involve
6669 * earlier dimensions as "smaller".
6671 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6672 __isl_keep isl_pw_aff *pa2)
6674 int i;
6675 int cmp;
6677 if (pa1 == pa2)
6678 return 0;
6680 if (!pa1)
6681 return -1;
6682 if (!pa2)
6683 return 1;
6685 cmp = isl_space_cmp(pa1->dim, pa2->dim);
6686 if (cmp != 0)
6687 return cmp;
6689 if (pa1->n != pa2->n)
6690 return pa1->n - pa2->n;
6692 for (i = 0; i < pa1->n; ++i) {
6693 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
6694 if (cmp != 0)
6695 return cmp;
6696 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
6697 if (cmp != 0)
6698 return cmp;
6701 return 0;
6704 /* Return a piecewise affine expression that is equal to "v" on "domain".
6706 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
6707 __isl_take isl_val *v)
6709 isl_space *space;
6710 isl_local_space *ls;
6711 isl_aff *aff;
6713 space = isl_set_get_space(domain);
6714 ls = isl_local_space_from_space(space);
6715 aff = isl_aff_val_on_domain(ls, v);
6717 return isl_pw_aff_alloc(domain, aff);
6720 /* Return a multi affine expression that is equal to "mv" on domain
6721 * space "space".
6723 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
6724 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
6726 int i, n;
6727 isl_space *space2;
6728 isl_local_space *ls;
6729 isl_multi_aff *ma;
6731 if (!space || !mv)
6732 goto error;
6734 n = isl_multi_val_dim(mv, isl_dim_set);
6735 space2 = isl_multi_val_get_space(mv);
6736 space2 = isl_space_align_params(space2, isl_space_copy(space));
6737 space = isl_space_align_params(space, isl_space_copy(space2));
6738 space = isl_space_map_from_domain_and_range(space, space2);
6739 ma = isl_multi_aff_alloc(isl_space_copy(space));
6740 ls = isl_local_space_from_space(isl_space_domain(space));
6741 for (i = 0; i < n; ++i) {
6742 isl_val *v;
6743 isl_aff *aff;
6745 v = isl_multi_val_get_val(mv, i);
6746 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
6747 ma = isl_multi_aff_set_aff(ma, i, aff);
6749 isl_local_space_free(ls);
6751 isl_multi_val_free(mv);
6752 return ma;
6753 error:
6754 isl_space_free(space);
6755 isl_multi_val_free(mv);
6756 return NULL;
6759 /* Return a piecewise multi-affine expression
6760 * that is equal to "mv" on "domain".
6762 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
6763 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
6765 isl_space *space;
6766 isl_multi_aff *ma;
6768 space = isl_set_get_space(domain);
6769 ma = isl_multi_aff_multi_val_on_space(space, mv);
6771 return isl_pw_multi_aff_alloc(domain, ma);
6774 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6775 * mv is the value that should be attained on each domain set
6776 * res collects the results
6778 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
6779 isl_multi_val *mv;
6780 isl_union_pw_multi_aff *res;
6783 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6784 * and add it to data->res.
6786 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
6787 void *user)
6789 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
6790 isl_pw_multi_aff *pma;
6791 isl_multi_val *mv;
6793 mv = isl_multi_val_copy(data->mv);
6794 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
6795 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6797 return data->res ? 0 : -1;
6800 /* Return a union piecewise multi-affine expression
6801 * that is equal to "mv" on "domain".
6803 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
6804 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
6806 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
6807 isl_space *space;
6809 space = isl_union_set_get_space(domain);
6810 data.res = isl_union_pw_multi_aff_empty(space);
6811 data.mv = mv;
6812 if (isl_union_set_foreach_set(domain,
6813 &pw_multi_aff_multi_val_on_domain, &data) < 0)
6814 data.res = isl_union_pw_multi_aff_free(data.res);
6815 isl_union_set_free(domain);
6816 isl_multi_val_free(mv);
6817 return data.res;