add isl_union_*_scale_down_val
[isl.git] / isl_aff.c
blobf21afa9ae4763d0183e85191c74509a335b1b1ed
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
24 #include <isl_seq.h>
25 #include <isl/set.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
30 #undef BASE
31 #define BASE aff
33 #include <isl_list_templ.c>
35 #undef BASE
36 #define BASE pw_aff
38 #include <isl_list_templ.c>
40 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
41 __isl_take isl_vec *v)
43 isl_aff *aff;
45 if (!ls || !v)
46 goto error;
48 aff = isl_calloc_type(v->ctx, struct isl_aff);
49 if (!aff)
50 goto error;
52 aff->ref = 1;
53 aff->ls = ls;
54 aff->v = v;
56 return aff;
57 error:
58 isl_local_space_free(ls);
59 isl_vec_free(v);
60 return NULL;
63 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
65 isl_ctx *ctx;
66 isl_vec *v;
67 unsigned total;
69 if (!ls)
70 return NULL;
72 ctx = isl_local_space_get_ctx(ls);
73 if (!isl_local_space_divs_known(ls))
74 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
75 goto error);
76 if (!isl_local_space_is_set(ls))
77 isl_die(ctx, isl_error_invalid,
78 "domain of affine expression should be a set",
79 goto error);
81 total = isl_local_space_dim(ls, isl_dim_all);
82 v = isl_vec_alloc(ctx, 1 + 1 + total);
83 return isl_aff_alloc_vec(ls, v);
84 error:
85 isl_local_space_free(ls);
86 return NULL;
89 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
91 isl_aff *aff;
93 aff = isl_aff_alloc(ls);
94 if (!aff)
95 return NULL;
97 isl_int_set_si(aff->v->el[0], 1);
98 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
100 return aff;
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
111 /* Return an affine expression defined on the specified domain
112 * that represents NaN.
114 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
116 isl_aff *aff;
118 aff = isl_aff_alloc(ls);
119 if (!aff)
120 return NULL;
122 isl_seq_clr(aff->v->el, aff->v->size);
124 return aff;
127 /* Return a piecewise affine expression defined on the specified domain
128 * that represents NaN.
130 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
132 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
135 /* Return an affine expression that is equal to "val" on
136 * domain local space "ls".
138 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
139 __isl_take isl_val *val)
141 isl_aff *aff;
143 if (!ls || !val)
144 goto error;
145 if (!isl_val_is_rat(val))
146 isl_die(isl_val_get_ctx(val), isl_error_invalid,
147 "expecting rational value", goto error);
149 aff = isl_aff_alloc(isl_local_space_copy(ls));
150 if (!aff)
151 goto error;
153 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
154 isl_int_set(aff->v->el[1], val->n);
155 isl_int_set(aff->v->el[0], val->d);
157 isl_local_space_free(ls);
158 isl_val_free(val);
159 return aff;
160 error:
161 isl_local_space_free(ls);
162 isl_val_free(val);
163 return NULL;
166 /* Return an affine expression that is equal to the specified dimension
167 * in "ls".
169 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
170 enum isl_dim_type type, unsigned pos)
172 isl_space *space;
173 isl_aff *aff;
175 if (!ls)
176 return NULL;
178 space = isl_local_space_get_space(ls);
179 if (!space)
180 goto error;
181 if (isl_space_is_map(space))
182 isl_die(isl_space_get_ctx(space), isl_error_invalid,
183 "expecting (parameter) set space", goto error);
184 if (pos >= isl_local_space_dim(ls, type))
185 isl_die(isl_space_get_ctx(space), isl_error_invalid,
186 "position out of bounds", goto error);
188 isl_space_free(space);
189 aff = isl_aff_alloc(ls);
190 if (!aff)
191 return NULL;
193 pos += isl_local_space_offset(aff->ls, type);
195 isl_int_set_si(aff->v->el[0], 1);
196 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
197 isl_int_set_si(aff->v->el[1 + pos], 1);
199 return aff;
200 error:
201 isl_local_space_free(ls);
202 isl_space_free(space);
203 return NULL;
206 /* Return a piecewise affine expression that is equal to
207 * the specified dimension in "ls".
209 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
210 enum isl_dim_type type, unsigned pos)
212 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
215 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
217 if (!aff)
218 return NULL;
220 aff->ref++;
221 return aff;
224 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
226 if (!aff)
227 return NULL;
229 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
230 isl_vec_copy(aff->v));
233 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
235 if (!aff)
236 return NULL;
238 if (aff->ref == 1)
239 return aff;
240 aff->ref--;
241 return isl_aff_dup(aff);
244 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
246 if (!aff)
247 return NULL;
249 if (--aff->ref > 0)
250 return NULL;
252 isl_local_space_free(aff->ls);
253 isl_vec_free(aff->v);
255 free(aff);
257 return NULL;
260 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
262 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
265 /* Externally, an isl_aff has a map space, but internally, the
266 * ls field corresponds to the domain of that space.
268 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
270 if (!aff)
271 return 0;
272 if (type == isl_dim_out)
273 return 1;
274 if (type == isl_dim_in)
275 type = isl_dim_set;
276 return isl_local_space_dim(aff->ls, type);
279 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
281 return aff ? isl_local_space_get_space(aff->ls) : NULL;
284 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
286 isl_space *space;
287 if (!aff)
288 return NULL;
289 space = isl_local_space_get_space(aff->ls);
290 space = isl_space_from_domain(space);
291 space = isl_space_add_dims(space, isl_dim_out, 1);
292 return space;
295 __isl_give isl_local_space *isl_aff_get_domain_local_space(
296 __isl_keep isl_aff *aff)
298 return aff ? isl_local_space_copy(aff->ls) : NULL;
301 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
303 isl_local_space *ls;
304 if (!aff)
305 return NULL;
306 ls = isl_local_space_copy(aff->ls);
307 ls = isl_local_space_from_domain(ls);
308 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
309 return ls;
312 /* Externally, an isl_aff has a map space, but internally, the
313 * ls field corresponds to the domain of that space.
315 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
316 enum isl_dim_type type, unsigned pos)
318 if (!aff)
319 return NULL;
320 if (type == isl_dim_out)
321 return NULL;
322 if (type == isl_dim_in)
323 type = isl_dim_set;
324 return isl_local_space_get_dim_name(aff->ls, type, pos);
327 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
328 __isl_take isl_space *dim)
330 aff = isl_aff_cow(aff);
331 if (!aff || !dim)
332 goto error;
334 aff->ls = isl_local_space_reset_space(aff->ls, dim);
335 if (!aff->ls)
336 return isl_aff_free(aff);
338 return aff;
339 error:
340 isl_aff_free(aff);
341 isl_space_free(dim);
342 return NULL;
345 /* Reset the space of "aff". This function is called from isl_pw_templ.c
346 * and doesn't know if the space of an element object is represented
347 * directly or through its domain. It therefore passes along both.
349 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
350 __isl_take isl_space *space, __isl_take isl_space *domain)
352 isl_space_free(space);
353 return isl_aff_reset_domain_space(aff, domain);
356 /* Reorder the coefficients of the affine expression based
357 * on the given reodering.
358 * The reordering r is assumed to have been extended with the local
359 * variables.
361 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
362 __isl_take isl_reordering *r, int n_div)
364 isl_vec *res;
365 int i;
367 if (!vec || !r)
368 goto error;
370 res = isl_vec_alloc(vec->ctx,
371 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
372 isl_seq_cpy(res->el, vec->el, 2);
373 isl_seq_clr(res->el + 2, res->size - 2);
374 for (i = 0; i < r->len; ++i)
375 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
377 isl_reordering_free(r);
378 isl_vec_free(vec);
379 return res;
380 error:
381 isl_vec_free(vec);
382 isl_reordering_free(r);
383 return NULL;
386 /* Reorder the dimensions of the domain of "aff" according
387 * to the given reordering.
389 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
390 __isl_take isl_reordering *r)
392 aff = isl_aff_cow(aff);
393 if (!aff)
394 goto error;
396 r = isl_reordering_extend(r, aff->ls->div->n_row);
397 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
398 aff->ls->div->n_row);
399 aff->ls = isl_local_space_realign(aff->ls, r);
401 if (!aff->v || !aff->ls)
402 return isl_aff_free(aff);
404 return aff;
405 error:
406 isl_aff_free(aff);
407 isl_reordering_free(r);
408 return NULL;
411 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
412 __isl_take isl_space *model)
414 if (!aff || !model)
415 goto error;
417 if (!isl_space_match(aff->ls->dim, isl_dim_param,
418 model, isl_dim_param)) {
419 isl_reordering *exp;
421 model = isl_space_drop_dims(model, isl_dim_in,
422 0, isl_space_dim(model, isl_dim_in));
423 model = isl_space_drop_dims(model, isl_dim_out,
424 0, isl_space_dim(model, isl_dim_out));
425 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
426 exp = isl_reordering_extend_space(exp,
427 isl_aff_get_domain_space(aff));
428 aff = isl_aff_realign_domain(aff, exp);
431 isl_space_free(model);
432 return aff;
433 error:
434 isl_space_free(model);
435 isl_aff_free(aff);
436 return NULL;
439 /* Is "aff" obviously equal to zero?
441 * If the denominator is zero, then "aff" is not equal to zero.
443 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
445 if (!aff)
446 return -1;
448 if (isl_int_is_zero(aff->v->el[0]))
449 return 0;
450 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
453 /* Does "aff" represent NaN?
455 int isl_aff_is_nan(__isl_keep isl_aff *aff)
457 if (!aff)
458 return -1;
460 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
463 /* Does "pa" involve any NaNs?
465 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
467 int i;
469 if (!pa)
470 return -1;
471 if (pa->n == 0)
472 return 0;
474 for (i = 0; i < pa->n; ++i) {
475 int is_nan = isl_aff_is_nan(pa->p[i].aff);
476 if (is_nan < 0 || is_nan)
477 return is_nan;
480 return 0;
483 /* Are "aff1" and "aff2" obviously equal?
485 * NaN is not equal to anything, not even to another NaN.
487 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
489 int equal;
491 if (!aff1 || !aff2)
492 return -1;
494 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
495 return 0;
497 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
498 if (equal < 0 || !equal)
499 return equal;
501 return isl_vec_is_equal(aff1->v, aff2->v);
504 /* Return the common denominator of "aff" in "v".
506 * We cannot return anything meaningful in case of a NaN.
508 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
510 if (!aff)
511 return -1;
512 if (isl_aff_is_nan(aff))
513 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
514 "cannot get denominator of NaN", return -1);
515 isl_int_set(*v, aff->v->el[0]);
516 return 0;
519 /* Return the common denominator of "aff".
521 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
523 isl_ctx *ctx;
525 if (!aff)
526 return NULL;
528 ctx = isl_aff_get_ctx(aff);
529 if (isl_aff_is_nan(aff))
530 return isl_val_nan(ctx);
531 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
534 /* Return the constant term of "aff" in "v".
536 * We cannot return anything meaningful in case of a NaN.
538 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
540 if (!aff)
541 return -1;
542 if (isl_aff_is_nan(aff))
543 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
544 "cannot get constant term of NaN", return -1);
545 isl_int_set(*v, aff->v->el[1]);
546 return 0;
549 /* Return the constant term of "aff".
551 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
553 isl_ctx *ctx;
554 isl_val *v;
556 if (!aff)
557 return NULL;
559 ctx = isl_aff_get_ctx(aff);
560 if (isl_aff_is_nan(aff))
561 return isl_val_nan(ctx);
562 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
563 return isl_val_normalize(v);
566 /* Return the coefficient of the variable of type "type" at position "pos"
567 * of "aff" in "v".
569 * We cannot return anything meaningful in case of a NaN.
571 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
572 enum isl_dim_type type, int pos, isl_int *v)
574 if (!aff)
575 return -1;
577 if (type == isl_dim_out)
578 isl_die(aff->v->ctx, isl_error_invalid,
579 "output/set dimension does not have a coefficient",
580 return -1);
581 if (type == isl_dim_in)
582 type = isl_dim_set;
584 if (pos >= isl_local_space_dim(aff->ls, type))
585 isl_die(aff->v->ctx, isl_error_invalid,
586 "position out of bounds", return -1);
588 if (isl_aff_is_nan(aff))
589 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
590 "cannot get coefficient of NaN", return -1);
591 pos += isl_local_space_offset(aff->ls, type);
592 isl_int_set(*v, aff->v->el[1 + pos]);
594 return 0;
597 /* Return the coefficient of the variable of type "type" at position "pos"
598 * of "aff".
600 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
601 enum isl_dim_type type, int pos)
603 isl_ctx *ctx;
604 isl_val *v;
606 if (!aff)
607 return NULL;
609 ctx = isl_aff_get_ctx(aff);
610 if (type == isl_dim_out)
611 isl_die(ctx, isl_error_invalid,
612 "output/set dimension does not have a coefficient",
613 return NULL);
614 if (type == isl_dim_in)
615 type = isl_dim_set;
617 if (pos >= isl_local_space_dim(aff->ls, type))
618 isl_die(ctx, isl_error_invalid,
619 "position out of bounds", return NULL);
621 if (isl_aff_is_nan(aff))
622 return isl_val_nan(ctx);
623 pos += isl_local_space_offset(aff->ls, type);
624 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
625 return isl_val_normalize(v);
628 /* Return the sign of the coefficient of the variable of type "type"
629 * at position "pos" of "aff".
631 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
632 int pos)
634 isl_ctx *ctx;
636 if (!aff)
637 return 0;
639 ctx = isl_aff_get_ctx(aff);
640 if (type == isl_dim_out)
641 isl_die(ctx, isl_error_invalid,
642 "output/set dimension does not have a coefficient",
643 return 0);
644 if (type == isl_dim_in)
645 type = isl_dim_set;
647 if (pos >= isl_local_space_dim(aff->ls, type))
648 isl_die(ctx, isl_error_invalid,
649 "position out of bounds", return 0);
651 pos += isl_local_space_offset(aff->ls, type);
652 return isl_int_sgn(aff->v->el[1 + pos]);
655 /* Replace the denominator of "aff" by "v".
657 * A NaN is unaffected by this operation.
659 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
661 if (!aff)
662 return NULL;
663 if (isl_aff_is_nan(aff))
664 return aff;
665 aff = isl_aff_cow(aff);
666 if (!aff)
667 return NULL;
669 aff->v = isl_vec_cow(aff->v);
670 if (!aff->v)
671 return isl_aff_free(aff);
673 isl_int_set(aff->v->el[0], v);
675 return aff;
678 /* Replace the numerator of the constant term of "aff" by "v".
680 * A NaN is unaffected by this operation.
682 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
684 if (!aff)
685 return NULL;
686 if (isl_aff_is_nan(aff))
687 return aff;
688 aff = isl_aff_cow(aff);
689 if (!aff)
690 return NULL;
692 aff->v = isl_vec_cow(aff->v);
693 if (!aff->v)
694 return isl_aff_free(aff);
696 isl_int_set(aff->v->el[1], v);
698 return aff;
701 /* Replace the constant term of "aff" by "v".
703 * A NaN is unaffected by this operation.
705 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
706 __isl_take isl_val *v)
708 if (!aff || !v)
709 goto error;
711 if (isl_aff_is_nan(aff)) {
712 isl_val_free(v);
713 return aff;
716 if (!isl_val_is_rat(v))
717 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
718 "expecting rational value", goto error);
720 if (isl_int_eq(aff->v->el[1], v->n) &&
721 isl_int_eq(aff->v->el[0], v->d)) {
722 isl_val_free(v);
723 return aff;
726 aff = isl_aff_cow(aff);
727 if (!aff)
728 goto error;
729 aff->v = isl_vec_cow(aff->v);
730 if (!aff->v)
731 goto error;
733 if (isl_int_eq(aff->v->el[0], v->d)) {
734 isl_int_set(aff->v->el[1], v->n);
735 } else if (isl_int_is_one(v->d)) {
736 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
737 } else {
738 isl_seq_scale(aff->v->el + 1,
739 aff->v->el + 1, v->d, aff->v->size - 1);
740 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
741 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
742 aff->v = isl_vec_normalize(aff->v);
743 if (!aff->v)
744 goto error;
747 isl_val_free(v);
748 return aff;
749 error:
750 isl_aff_free(aff);
751 isl_val_free(v);
752 return NULL;
755 /* Add "v" to the constant term of "aff".
757 * A NaN is unaffected by this operation.
759 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
761 if (isl_int_is_zero(v))
762 return aff;
764 if (!aff)
765 return NULL;
766 if (isl_aff_is_nan(aff))
767 return aff;
768 aff = isl_aff_cow(aff);
769 if (!aff)
770 return NULL;
772 aff->v = isl_vec_cow(aff->v);
773 if (!aff->v)
774 return isl_aff_free(aff);
776 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
778 return aff;
781 /* Add "v" to the constant term of "aff".
783 * A NaN is unaffected by this operation.
785 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
786 __isl_take isl_val *v)
788 if (!aff || !v)
789 goto error;
791 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
792 isl_val_free(v);
793 return aff;
796 if (!isl_val_is_rat(v))
797 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
798 "expecting rational value", goto error);
800 aff = isl_aff_cow(aff);
801 if (!aff)
802 goto error;
804 aff->v = isl_vec_cow(aff->v);
805 if (!aff->v)
806 goto error;
808 if (isl_int_is_one(v->d)) {
809 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
810 } else if (isl_int_eq(aff->v->el[0], v->d)) {
811 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
812 aff->v = isl_vec_normalize(aff->v);
813 if (!aff->v)
814 goto error;
815 } else {
816 isl_seq_scale(aff->v->el + 1,
817 aff->v->el + 1, v->d, aff->v->size - 1);
818 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
819 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
820 aff->v = isl_vec_normalize(aff->v);
821 if (!aff->v)
822 goto error;
825 isl_val_free(v);
826 return aff;
827 error:
828 isl_aff_free(aff);
829 isl_val_free(v);
830 return NULL;
833 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
835 isl_int t;
837 isl_int_init(t);
838 isl_int_set_si(t, v);
839 aff = isl_aff_add_constant(aff, t);
840 isl_int_clear(t);
842 return aff;
845 /* Add "v" to the numerator of the constant term of "aff".
847 * A NaN is unaffected by this operation.
849 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
851 if (isl_int_is_zero(v))
852 return aff;
854 if (!aff)
855 return NULL;
856 if (isl_aff_is_nan(aff))
857 return aff;
858 aff = isl_aff_cow(aff);
859 if (!aff)
860 return NULL;
862 aff->v = isl_vec_cow(aff->v);
863 if (!aff->v)
864 return isl_aff_free(aff);
866 isl_int_add(aff->v->el[1], aff->v->el[1], v);
868 return aff;
871 /* Add "v" to the numerator of the constant term of "aff".
873 * A NaN is unaffected by this operation.
875 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
877 isl_int t;
879 if (v == 0)
880 return aff;
882 isl_int_init(t);
883 isl_int_set_si(t, v);
884 aff = isl_aff_add_constant_num(aff, t);
885 isl_int_clear(t);
887 return aff;
890 /* Replace the numerator of the constant term of "aff" by "v".
892 * A NaN is unaffected by this operation.
894 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
896 if (!aff)
897 return NULL;
898 if (isl_aff_is_nan(aff))
899 return aff;
900 aff = isl_aff_cow(aff);
901 if (!aff)
902 return NULL;
904 aff->v = isl_vec_cow(aff->v);
905 if (!aff->v)
906 return isl_aff_free(aff);
908 isl_int_set_si(aff->v->el[1], v);
910 return aff;
913 /* Replace the numerator of the coefficient of the variable of type "type"
914 * at position "pos" of "aff" by "v".
916 * A NaN is unaffected by this operation.
918 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
919 enum isl_dim_type type, int pos, isl_int v)
921 if (!aff)
922 return NULL;
924 if (type == isl_dim_out)
925 isl_die(aff->v->ctx, isl_error_invalid,
926 "output/set dimension does not have a coefficient",
927 return isl_aff_free(aff));
928 if (type == isl_dim_in)
929 type = isl_dim_set;
931 if (pos >= isl_local_space_dim(aff->ls, type))
932 isl_die(aff->v->ctx, isl_error_invalid,
933 "position out of bounds", return isl_aff_free(aff));
935 if (isl_aff_is_nan(aff))
936 return aff;
937 aff = isl_aff_cow(aff);
938 if (!aff)
939 return NULL;
941 aff->v = isl_vec_cow(aff->v);
942 if (!aff->v)
943 return isl_aff_free(aff);
945 pos += isl_local_space_offset(aff->ls, type);
946 isl_int_set(aff->v->el[1 + pos], v);
948 return aff;
951 /* Replace the numerator of the coefficient of the variable of type "type"
952 * at position "pos" of "aff" by "v".
954 * A NaN is unaffected by this operation.
956 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
957 enum isl_dim_type type, int pos, int v)
959 if (!aff)
960 return NULL;
962 if (type == isl_dim_out)
963 isl_die(aff->v->ctx, isl_error_invalid,
964 "output/set dimension does not have a coefficient",
965 return isl_aff_free(aff));
966 if (type == isl_dim_in)
967 type = isl_dim_set;
969 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
970 isl_die(aff->v->ctx, isl_error_invalid,
971 "position out of bounds", return isl_aff_free(aff));
973 if (isl_aff_is_nan(aff))
974 return aff;
975 pos += isl_local_space_offset(aff->ls, type);
976 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
977 return aff;
979 aff = isl_aff_cow(aff);
980 if (!aff)
981 return NULL;
983 aff->v = isl_vec_cow(aff->v);
984 if (!aff->v)
985 return isl_aff_free(aff);
987 isl_int_set_si(aff->v->el[1 + pos], v);
989 return aff;
992 /* Replace the coefficient of the variable of type "type" at position "pos"
993 * of "aff" by "v".
995 * A NaN is unaffected by this operation.
997 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
998 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1000 if (!aff || !v)
1001 goto error;
1003 if (type == isl_dim_out)
1004 isl_die(aff->v->ctx, isl_error_invalid,
1005 "output/set dimension does not have a coefficient",
1006 goto error);
1007 if (type == isl_dim_in)
1008 type = isl_dim_set;
1010 if (pos >= isl_local_space_dim(aff->ls, type))
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "position out of bounds", goto error);
1014 if (isl_aff_is_nan(aff)) {
1015 isl_val_free(v);
1016 return aff;
1018 if (!isl_val_is_rat(v))
1019 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1020 "expecting rational value", goto error);
1022 pos += isl_local_space_offset(aff->ls, type);
1023 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1024 isl_int_eq(aff->v->el[0], v->d)) {
1025 isl_val_free(v);
1026 return aff;
1029 aff = isl_aff_cow(aff);
1030 if (!aff)
1031 goto error;
1032 aff->v = isl_vec_cow(aff->v);
1033 if (!aff->v)
1034 goto error;
1036 if (isl_int_eq(aff->v->el[0], v->d)) {
1037 isl_int_set(aff->v->el[1 + pos], v->n);
1038 } else if (isl_int_is_one(v->d)) {
1039 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1040 } else {
1041 isl_seq_scale(aff->v->el + 1,
1042 aff->v->el + 1, v->d, aff->v->size - 1);
1043 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1044 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1045 aff->v = isl_vec_normalize(aff->v);
1046 if (!aff->v)
1047 goto error;
1050 isl_val_free(v);
1051 return aff;
1052 error:
1053 isl_aff_free(aff);
1054 isl_val_free(v);
1055 return NULL;
1058 /* Add "v" to the coefficient of the variable of type "type"
1059 * at position "pos" of "aff".
1061 * A NaN is unaffected by this operation.
1063 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1064 enum isl_dim_type type, int pos, isl_int v)
1066 if (!aff)
1067 return NULL;
1069 if (type == isl_dim_out)
1070 isl_die(aff->v->ctx, isl_error_invalid,
1071 "output/set dimension does not have a coefficient",
1072 return isl_aff_free(aff));
1073 if (type == isl_dim_in)
1074 type = isl_dim_set;
1076 if (pos >= isl_local_space_dim(aff->ls, type))
1077 isl_die(aff->v->ctx, isl_error_invalid,
1078 "position out of bounds", return isl_aff_free(aff));
1080 if (isl_aff_is_nan(aff))
1081 return aff;
1082 aff = isl_aff_cow(aff);
1083 if (!aff)
1084 return NULL;
1086 aff->v = isl_vec_cow(aff->v);
1087 if (!aff->v)
1088 return isl_aff_free(aff);
1090 pos += isl_local_space_offset(aff->ls, type);
1091 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1093 return aff;
1096 /* Add "v" to the coefficient of the variable of type "type"
1097 * at position "pos" of "aff".
1099 * A NaN is unaffected by this operation.
1101 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1102 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1104 if (!aff || !v)
1105 goto error;
1107 if (isl_val_is_zero(v)) {
1108 isl_val_free(v);
1109 return aff;
1112 if (type == isl_dim_out)
1113 isl_die(aff->v->ctx, isl_error_invalid,
1114 "output/set dimension does not have a coefficient",
1115 goto error);
1116 if (type == isl_dim_in)
1117 type = isl_dim_set;
1119 if (pos >= isl_local_space_dim(aff->ls, type))
1120 isl_die(aff->v->ctx, isl_error_invalid,
1121 "position out of bounds", goto error);
1123 if (isl_aff_is_nan(aff)) {
1124 isl_val_free(v);
1125 return aff;
1127 if (!isl_val_is_rat(v))
1128 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1129 "expecting rational value", goto error);
1131 aff = isl_aff_cow(aff);
1132 if (!aff)
1133 goto error;
1135 aff->v = isl_vec_cow(aff->v);
1136 if (!aff->v)
1137 goto error;
1139 pos += isl_local_space_offset(aff->ls, type);
1140 if (isl_int_is_one(v->d)) {
1141 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1142 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1143 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1144 aff->v = isl_vec_normalize(aff->v);
1145 if (!aff->v)
1146 goto error;
1147 } else {
1148 isl_seq_scale(aff->v->el + 1,
1149 aff->v->el + 1, v->d, aff->v->size - 1);
1150 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1151 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1152 aff->v = isl_vec_normalize(aff->v);
1153 if (!aff->v)
1154 goto error;
1157 isl_val_free(v);
1158 return aff;
1159 error:
1160 isl_aff_free(aff);
1161 isl_val_free(v);
1162 return NULL;
1165 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1166 enum isl_dim_type type, int pos, int v)
1168 isl_int t;
1170 isl_int_init(t);
1171 isl_int_set_si(t, v);
1172 aff = isl_aff_add_coefficient(aff, type, pos, t);
1173 isl_int_clear(t);
1175 return aff;
1178 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1180 if (!aff)
1181 return NULL;
1183 return isl_local_space_get_div(aff->ls, pos);
1186 /* Return the negation of "aff".
1188 * As a special case, -NaN = NaN.
1190 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1192 if (!aff)
1193 return NULL;
1194 if (isl_aff_is_nan(aff))
1195 return aff;
1196 aff = isl_aff_cow(aff);
1197 if (!aff)
1198 return NULL;
1199 aff->v = isl_vec_cow(aff->v);
1200 if (!aff->v)
1201 return isl_aff_free(aff);
1203 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1205 return aff;
1208 /* Remove divs from the local space that do not appear in the affine
1209 * expression.
1210 * We currently only remove divs at the end.
1211 * Some intermediate divs may also not appear directly in the affine
1212 * expression, but we would also need to check that no other divs are
1213 * defined in terms of them.
1215 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1217 int pos;
1218 int off;
1219 int n;
1221 if (!aff)
1222 return NULL;
1224 n = isl_local_space_dim(aff->ls, isl_dim_div);
1225 off = isl_local_space_offset(aff->ls, isl_dim_div);
1227 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1228 if (pos == n)
1229 return aff;
1231 aff = isl_aff_cow(aff);
1232 if (!aff)
1233 return NULL;
1235 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1236 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1237 if (!aff->ls || !aff->v)
1238 return isl_aff_free(aff);
1240 return aff;
1243 /* Given two affine expressions "p" of length p_len (including the
1244 * denominator and the constant term) and "subs" of length subs_len,
1245 * plug in "subs" for the variable at position "pos".
1246 * The variables of "subs" and "p" are assumed to match up to subs_len,
1247 * but "p" may have additional variables.
1248 * "v" is an initialized isl_int that can be used internally.
1250 * In particular, if "p" represents the expression
1252 * (a i + g)/m
1254 * with i the variable at position "pos" and "subs" represents the expression
1256 * f/d
1258 * then the result represents the expression
1260 * (a f + d g)/(m d)
1263 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1264 int p_len, int subs_len, isl_int v)
1266 isl_int_set(v, p[1 + pos]);
1267 isl_int_set_si(p[1 + pos], 0);
1268 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1269 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1270 isl_int_mul(p[0], p[0], subs[0]);
1273 /* Look for any divs in the aff->ls with a denominator equal to one
1274 * and plug them into the affine expression and any subsequent divs
1275 * that may reference the div.
1277 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1279 int i, n;
1280 int len;
1281 isl_int v;
1282 isl_vec *vec;
1283 isl_local_space *ls;
1284 unsigned pos;
1286 if (!aff)
1287 return NULL;
1289 n = isl_local_space_dim(aff->ls, isl_dim_div);
1290 len = aff->v->size;
1291 for (i = 0; i < n; ++i) {
1292 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1293 continue;
1294 ls = isl_local_space_copy(aff->ls);
1295 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1296 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1297 vec = isl_vec_copy(aff->v);
1298 vec = isl_vec_cow(vec);
1299 if (!ls || !vec)
1300 goto error;
1302 isl_int_init(v);
1304 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1305 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1306 len, len, v);
1308 isl_int_clear(v);
1310 isl_vec_free(aff->v);
1311 aff->v = vec;
1312 isl_local_space_free(aff->ls);
1313 aff->ls = ls;
1316 return aff;
1317 error:
1318 isl_vec_free(vec);
1319 isl_local_space_free(ls);
1320 return isl_aff_free(aff);
1323 /* Look for any divs j that appear with a unit coefficient inside
1324 * the definitions of other divs i and plug them into the definitions
1325 * of the divs i.
1327 * In particular, an expression of the form
1329 * floor((f(..) + floor(g(..)/n))/m)
1331 * is simplified to
1333 * floor((n * f(..) + g(..))/(n * m))
1335 * This simplification is correct because we can move the expression
1336 * f(..) into the inner floor in the original expression to obtain
1338 * floor(floor((n * f(..) + g(..))/n)/m)
1340 * from which we can derive the simplified expression.
1342 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1344 int i, j, n;
1345 int off;
1347 if (!aff)
1348 return NULL;
1350 n = isl_local_space_dim(aff->ls, isl_dim_div);
1351 off = isl_local_space_offset(aff->ls, isl_dim_div);
1352 for (i = 1; i < n; ++i) {
1353 for (j = 0; j < i; ++j) {
1354 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1355 continue;
1356 aff->ls = isl_local_space_substitute_seq(aff->ls,
1357 isl_dim_div, j, aff->ls->div->row[j],
1358 aff->v->size, i, 1);
1359 if (!aff->ls)
1360 return isl_aff_free(aff);
1364 return aff;
1367 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1369 * Even though this function is only called on isl_affs with a single
1370 * reference, we are careful to only change aff->v and aff->ls together.
1372 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1374 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1375 isl_local_space *ls;
1376 isl_vec *v;
1378 ls = isl_local_space_copy(aff->ls);
1379 ls = isl_local_space_swap_div(ls, a, b);
1380 v = isl_vec_copy(aff->v);
1381 v = isl_vec_cow(v);
1382 if (!ls || !v)
1383 goto error;
1385 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1386 isl_vec_free(aff->v);
1387 aff->v = v;
1388 isl_local_space_free(aff->ls);
1389 aff->ls = ls;
1391 return aff;
1392 error:
1393 isl_vec_free(v);
1394 isl_local_space_free(ls);
1395 return isl_aff_free(aff);
1398 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1400 * We currently do not actually remove div "b", but simply add its
1401 * coefficient to that of "a" and then zero it out.
1403 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1405 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1407 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1408 return aff;
1410 aff->v = isl_vec_cow(aff->v);
1411 if (!aff->v)
1412 return isl_aff_free(aff);
1414 isl_int_add(aff->v->el[1 + off + a],
1415 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1416 isl_int_set_si(aff->v->el[1 + off + b], 0);
1418 return aff;
1421 /* Sort the divs in the local space of "aff" according to
1422 * the comparison function "cmp_row" in isl_local_space.c,
1423 * combining the coefficients of identical divs.
1425 * Reordering divs does not change the semantics of "aff",
1426 * so there is no need to call isl_aff_cow.
1427 * Moreover, this function is currently only called on isl_affs
1428 * with a single reference.
1430 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1432 int i, j, n;
1433 unsigned off;
1435 if (!aff)
1436 return NULL;
1438 off = isl_local_space_offset(aff->ls, isl_dim_div);
1439 n = isl_aff_dim(aff, isl_dim_div);
1440 for (i = 1; i < n; ++i) {
1441 for (j = i - 1; j >= 0; --j) {
1442 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1443 if (cmp < 0)
1444 break;
1445 if (cmp == 0)
1446 aff = merge_divs(aff, j, j + 1);
1447 else
1448 aff = swap_div(aff, j, j + 1);
1449 if (!aff)
1450 return NULL;
1454 return aff;
1457 /* Normalize the representation of "aff".
1459 * This function should only be called of "new" isl_affs, i.e.,
1460 * with only a single reference. We therefore do not need to
1461 * worry about affecting other instances.
1463 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1465 if (!aff)
1466 return NULL;
1467 aff->v = isl_vec_normalize(aff->v);
1468 if (!aff->v)
1469 return isl_aff_free(aff);
1470 aff = plug_in_integral_divs(aff);
1471 aff = plug_in_unit_divs(aff);
1472 aff = sort_divs(aff);
1473 aff = isl_aff_remove_unused_divs(aff);
1474 return aff;
1477 /* Given f, return floor(f).
1478 * If f is an integer expression, then just return f.
1479 * If f is a constant, then return the constant floor(f).
1480 * Otherwise, if f = g/m, write g = q m + r,
1481 * create a new div d = [r/m] and return the expression q + d.
1482 * The coefficients in r are taken to lie between -m/2 and m/2.
1484 * As a special case, floor(NaN) = NaN.
1486 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1488 int i;
1489 int size;
1490 isl_ctx *ctx;
1491 isl_vec *div;
1493 if (!aff)
1494 return NULL;
1496 if (isl_aff_is_nan(aff))
1497 return aff;
1498 if (isl_int_is_one(aff->v->el[0]))
1499 return aff;
1501 aff = isl_aff_cow(aff);
1502 if (!aff)
1503 return NULL;
1505 aff->v = isl_vec_cow(aff->v);
1506 if (!aff->v)
1507 return isl_aff_free(aff);
1509 if (isl_aff_is_cst(aff)) {
1510 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1511 isl_int_set_si(aff->v->el[0], 1);
1512 return aff;
1515 div = isl_vec_copy(aff->v);
1516 div = isl_vec_cow(div);
1517 if (!div)
1518 return isl_aff_free(aff);
1520 ctx = isl_aff_get_ctx(aff);
1521 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1522 for (i = 1; i < aff->v->size; ++i) {
1523 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1524 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1525 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1526 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1527 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1531 aff->ls = isl_local_space_add_div(aff->ls, div);
1532 if (!aff->ls)
1533 return isl_aff_free(aff);
1535 size = aff->v->size;
1536 aff->v = isl_vec_extend(aff->v, size + 1);
1537 if (!aff->v)
1538 return isl_aff_free(aff);
1539 isl_int_set_si(aff->v->el[0], 1);
1540 isl_int_set_si(aff->v->el[size], 1);
1542 aff = isl_aff_normalize(aff);
1544 return aff;
1547 /* Compute
1549 * aff mod m = aff - m * floor(aff/m)
1551 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1553 isl_aff *res;
1555 res = isl_aff_copy(aff);
1556 aff = isl_aff_scale_down(aff, m);
1557 aff = isl_aff_floor(aff);
1558 aff = isl_aff_scale(aff, m);
1559 res = isl_aff_sub(res, aff);
1561 return res;
1564 /* Compute
1566 * aff mod m = aff - m * floor(aff/m)
1568 * with m an integer value.
1570 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1571 __isl_take isl_val *m)
1573 isl_aff *res;
1575 if (!aff || !m)
1576 goto error;
1578 if (!isl_val_is_int(m))
1579 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1580 "expecting integer modulo", goto error);
1582 res = isl_aff_copy(aff);
1583 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1584 aff = isl_aff_floor(aff);
1585 aff = isl_aff_scale_val(aff, m);
1586 res = isl_aff_sub(res, aff);
1588 return res;
1589 error:
1590 isl_aff_free(aff);
1591 isl_val_free(m);
1592 return NULL;
1595 /* Compute
1597 * pwaff mod m = pwaff - m * floor(pwaff/m)
1599 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1601 isl_pw_aff *res;
1603 res = isl_pw_aff_copy(pwaff);
1604 pwaff = isl_pw_aff_scale_down(pwaff, m);
1605 pwaff = isl_pw_aff_floor(pwaff);
1606 pwaff = isl_pw_aff_scale(pwaff, m);
1607 res = isl_pw_aff_sub(res, pwaff);
1609 return res;
1612 /* Compute
1614 * pa mod m = pa - m * floor(pa/m)
1616 * with m an integer value.
1618 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1619 __isl_take isl_val *m)
1621 if (!pa || !m)
1622 goto error;
1623 if (!isl_val_is_int(m))
1624 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1625 "expecting integer modulo", goto error);
1626 pa = isl_pw_aff_mod(pa, m->n);
1627 isl_val_free(m);
1628 return pa;
1629 error:
1630 isl_pw_aff_free(pa);
1631 isl_val_free(m);
1632 return NULL;
1635 /* Given f, return ceil(f).
1636 * If f is an integer expression, then just return f.
1637 * Otherwise, let f be the expression
1639 * e/m
1641 * then return
1643 * floor((e + m - 1)/m)
1645 * As a special case, ceil(NaN) = NaN.
1647 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1649 if (!aff)
1650 return NULL;
1652 if (isl_aff_is_nan(aff))
1653 return aff;
1654 if (isl_int_is_one(aff->v->el[0]))
1655 return aff;
1657 aff = isl_aff_cow(aff);
1658 if (!aff)
1659 return NULL;
1660 aff->v = isl_vec_cow(aff->v);
1661 if (!aff->v)
1662 return isl_aff_free(aff);
1664 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1665 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1666 aff = isl_aff_floor(aff);
1668 return aff;
1671 /* Apply the expansion computed by isl_merge_divs.
1672 * The expansion itself is given by "exp" while the resulting
1673 * list of divs is given by "div".
1675 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1676 __isl_take isl_mat *div, int *exp)
1678 int i, j;
1679 int old_n_div;
1680 int new_n_div;
1681 int offset;
1683 aff = isl_aff_cow(aff);
1684 if (!aff || !div)
1685 goto error;
1687 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1688 new_n_div = isl_mat_rows(div);
1689 if (new_n_div < old_n_div)
1690 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1691 "not an expansion", goto error);
1693 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1694 if (!aff->v)
1695 goto error;
1697 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1698 j = old_n_div - 1;
1699 for (i = new_n_div - 1; i >= 0; --i) {
1700 if (j >= 0 && exp[j] == i) {
1701 if (i != j)
1702 isl_int_swap(aff->v->el[offset + i],
1703 aff->v->el[offset + j]);
1704 j--;
1705 } else
1706 isl_int_set_si(aff->v->el[offset + i], 0);
1709 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1710 if (!aff->ls)
1711 goto error;
1712 isl_mat_free(div);
1713 return aff;
1714 error:
1715 isl_aff_free(aff);
1716 isl_mat_free(div);
1717 return NULL;
1720 /* Add two affine expressions that live in the same local space.
1722 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1723 __isl_take isl_aff *aff2)
1725 isl_int gcd, f;
1727 aff1 = isl_aff_cow(aff1);
1728 if (!aff1 || !aff2)
1729 goto error;
1731 aff1->v = isl_vec_cow(aff1->v);
1732 if (!aff1->v)
1733 goto error;
1735 isl_int_init(gcd);
1736 isl_int_init(f);
1737 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1738 isl_int_divexact(f, aff2->v->el[0], gcd);
1739 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1740 isl_int_divexact(f, aff1->v->el[0], gcd);
1741 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1742 isl_int_divexact(f, aff2->v->el[0], gcd);
1743 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1744 isl_int_clear(f);
1745 isl_int_clear(gcd);
1747 isl_aff_free(aff2);
1748 return aff1;
1749 error:
1750 isl_aff_free(aff1);
1751 isl_aff_free(aff2);
1752 return NULL;
1755 /* Return the sum of "aff1" and "aff2".
1757 * If either of the two is NaN, then the result is NaN.
1759 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1760 __isl_take isl_aff *aff2)
1762 isl_ctx *ctx;
1763 int *exp1 = NULL;
1764 int *exp2 = NULL;
1765 isl_mat *div;
1766 int n_div1, n_div2;
1768 if (!aff1 || !aff2)
1769 goto error;
1771 ctx = isl_aff_get_ctx(aff1);
1772 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1773 isl_die(ctx, isl_error_invalid,
1774 "spaces don't match", goto error);
1776 if (isl_aff_is_nan(aff1)) {
1777 isl_aff_free(aff2);
1778 return aff1;
1780 if (isl_aff_is_nan(aff2)) {
1781 isl_aff_free(aff1);
1782 return aff2;
1785 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1786 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1787 if (n_div1 == 0 && n_div2 == 0)
1788 return add_expanded(aff1, aff2);
1790 exp1 = isl_alloc_array(ctx, int, n_div1);
1791 exp2 = isl_alloc_array(ctx, int, n_div2);
1792 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1793 goto error;
1795 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1796 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1797 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1798 free(exp1);
1799 free(exp2);
1801 return add_expanded(aff1, aff2);
1802 error:
1803 free(exp1);
1804 free(exp2);
1805 isl_aff_free(aff1);
1806 isl_aff_free(aff2);
1807 return NULL;
1810 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1811 __isl_take isl_aff *aff2)
1813 return isl_aff_add(aff1, isl_aff_neg(aff2));
1816 /* Return the result of scaling "aff" by a factor of "f".
1818 * As a special case, f * NaN = NaN.
1820 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1822 isl_int gcd;
1824 if (!aff)
1825 return NULL;
1826 if (isl_aff_is_nan(aff))
1827 return aff;
1829 if (isl_int_is_one(f))
1830 return aff;
1832 aff = isl_aff_cow(aff);
1833 if (!aff)
1834 return NULL;
1835 aff->v = isl_vec_cow(aff->v);
1836 if (!aff->v)
1837 return isl_aff_free(aff);
1839 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1840 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1841 return aff;
1844 isl_int_init(gcd);
1845 isl_int_gcd(gcd, aff->v->el[0], f);
1846 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1847 isl_int_divexact(gcd, f, gcd);
1848 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1849 isl_int_clear(gcd);
1851 return aff;
1854 /* Multiple "aff" by "v".
1856 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1857 __isl_take isl_val *v)
1859 if (!aff || !v)
1860 goto error;
1862 if (isl_val_is_one(v)) {
1863 isl_val_free(v);
1864 return aff;
1867 if (!isl_val_is_rat(v))
1868 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1869 "expecting rational factor", goto error);
1871 aff = isl_aff_scale(aff, v->n);
1872 aff = isl_aff_scale_down(aff, v->d);
1874 isl_val_free(v);
1875 return aff;
1876 error:
1877 isl_aff_free(aff);
1878 isl_val_free(v);
1879 return NULL;
1882 /* Return the result of scaling "aff" down by a factor of "f".
1884 * As a special case, NaN/f = NaN.
1886 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1888 isl_int gcd;
1890 if (!aff)
1891 return NULL;
1892 if (isl_aff_is_nan(aff))
1893 return aff;
1895 if (isl_int_is_one(f))
1896 return aff;
1898 aff = isl_aff_cow(aff);
1899 if (!aff)
1900 return NULL;
1902 if (isl_int_is_zero(f))
1903 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1904 "cannot scale down by zero", return isl_aff_free(aff));
1906 aff->v = isl_vec_cow(aff->v);
1907 if (!aff->v)
1908 return isl_aff_free(aff);
1910 isl_int_init(gcd);
1911 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1912 isl_int_gcd(gcd, gcd, f);
1913 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1914 isl_int_divexact(gcd, f, gcd);
1915 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1916 isl_int_clear(gcd);
1918 return aff;
1921 /* Divide "aff" by "v".
1923 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1924 __isl_take isl_val *v)
1926 if (!aff || !v)
1927 goto error;
1929 if (isl_val_is_one(v)) {
1930 isl_val_free(v);
1931 return aff;
1934 if (!isl_val_is_rat(v))
1935 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1936 "expecting rational factor", goto error);
1937 if (!isl_val_is_pos(v))
1938 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1939 "factor needs to be positive", goto error);
1941 aff = isl_aff_scale(aff, v->d);
1942 aff = isl_aff_scale_down(aff, v->n);
1944 isl_val_free(v);
1945 return aff;
1946 error:
1947 isl_aff_free(aff);
1948 isl_val_free(v);
1949 return NULL;
1952 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1954 isl_int v;
1956 if (f == 1)
1957 return aff;
1959 isl_int_init(v);
1960 isl_int_set_ui(v, f);
1961 aff = isl_aff_scale_down(aff, v);
1962 isl_int_clear(v);
1964 return aff;
1967 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1968 enum isl_dim_type type, unsigned pos, const char *s)
1970 aff = isl_aff_cow(aff);
1971 if (!aff)
1972 return NULL;
1973 if (type == isl_dim_out)
1974 isl_die(aff->v->ctx, isl_error_invalid,
1975 "cannot set name of output/set dimension",
1976 return isl_aff_free(aff));
1977 if (type == isl_dim_in)
1978 type = isl_dim_set;
1979 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1980 if (!aff->ls)
1981 return isl_aff_free(aff);
1983 return aff;
1986 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1987 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1989 aff = isl_aff_cow(aff);
1990 if (!aff)
1991 goto error;
1992 if (type == isl_dim_out)
1993 isl_die(aff->v->ctx, isl_error_invalid,
1994 "cannot set name of output/set dimension",
1995 goto error);
1996 if (type == isl_dim_in)
1997 type = isl_dim_set;
1998 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1999 if (!aff->ls)
2000 return isl_aff_free(aff);
2002 return aff;
2003 error:
2004 isl_id_free(id);
2005 isl_aff_free(aff);
2006 return NULL;
2009 /* Replace the identifier of the input tuple of "aff" by "id".
2010 * type is currently required to be equal to isl_dim_in
2012 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2013 enum isl_dim_type type, __isl_take isl_id *id)
2015 aff = isl_aff_cow(aff);
2016 if (!aff)
2017 goto error;
2018 if (type != isl_dim_out)
2019 isl_die(aff->v->ctx, isl_error_invalid,
2020 "cannot only set id of input tuple", goto error);
2021 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2022 if (!aff->ls)
2023 return isl_aff_free(aff);
2025 return aff;
2026 error:
2027 isl_id_free(id);
2028 isl_aff_free(aff);
2029 return NULL;
2032 /* Exploit the equalities in "eq" to simplify the affine expression
2033 * and the expressions of the integer divisions in the local space.
2034 * The integer divisions in this local space are assumed to appear
2035 * as regular dimensions in "eq".
2037 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2038 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2040 int i, j;
2041 unsigned total;
2042 unsigned n_div;
2044 if (!eq)
2045 goto error;
2046 if (eq->n_eq == 0) {
2047 isl_basic_set_free(eq);
2048 return aff;
2051 aff = isl_aff_cow(aff);
2052 if (!aff)
2053 goto error;
2055 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2056 isl_basic_set_copy(eq));
2057 aff->v = isl_vec_cow(aff->v);
2058 if (!aff->ls || !aff->v)
2059 goto error;
2061 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2062 n_div = eq->n_div;
2063 for (i = 0; i < eq->n_eq; ++i) {
2064 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2065 if (j < 0 || j == 0 || j >= total)
2066 continue;
2068 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2069 &aff->v->el[0]);
2072 isl_basic_set_free(eq);
2073 aff = isl_aff_normalize(aff);
2074 return aff;
2075 error:
2076 isl_basic_set_free(eq);
2077 isl_aff_free(aff);
2078 return NULL;
2081 /* Exploit the equalities in "eq" to simplify the affine expression
2082 * and the expressions of the integer divisions in the local space.
2084 static __isl_give isl_aff *isl_aff_substitute_equalities(
2085 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2087 int n_div;
2089 if (!aff || !eq)
2090 goto error;
2091 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2092 if (n_div > 0)
2093 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2094 return isl_aff_substitute_equalities_lifted(aff, eq);
2095 error:
2096 isl_basic_set_free(eq);
2097 isl_aff_free(aff);
2098 return NULL;
2101 /* Look for equalities among the variables shared by context and aff
2102 * and the integer divisions of aff, if any.
2103 * The equalities are then used to eliminate coefficients and/or integer
2104 * divisions from aff.
2106 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2107 __isl_take isl_set *context)
2109 isl_basic_set *hull;
2110 int n_div;
2112 if (!aff)
2113 goto error;
2114 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2115 if (n_div > 0) {
2116 isl_basic_set *bset;
2117 isl_local_space *ls;
2118 context = isl_set_add_dims(context, isl_dim_set, n_div);
2119 ls = isl_aff_get_domain_local_space(aff);
2120 bset = isl_basic_set_from_local_space(ls);
2121 bset = isl_basic_set_lift(bset);
2122 bset = isl_basic_set_flatten(bset);
2123 context = isl_set_intersect(context,
2124 isl_set_from_basic_set(bset));
2127 hull = isl_set_affine_hull(context);
2128 return isl_aff_substitute_equalities_lifted(aff, hull);
2129 error:
2130 isl_aff_free(aff);
2131 isl_set_free(context);
2132 return NULL;
2135 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2136 __isl_take isl_set *context)
2138 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2139 dom_context = isl_set_intersect_params(dom_context, context);
2140 return isl_aff_gist(aff, dom_context);
2143 /* Return a basic set containing those elements in the space
2144 * of aff where it is non-negative.
2145 * If "rational" is set, then return a rational basic set.
2147 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2149 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2150 __isl_take isl_aff *aff, int rational)
2152 isl_constraint *ineq;
2153 isl_basic_set *bset;
2155 if (!aff)
2156 return NULL;
2157 if (isl_aff_is_nan(aff)) {
2158 isl_space *space = isl_aff_get_domain_space(aff);
2159 isl_aff_free(aff);
2160 return isl_basic_set_empty(space);
2163 ineq = isl_inequality_from_aff(aff);
2165 bset = isl_basic_set_from_constraint(ineq);
2166 if (rational)
2167 bset = isl_basic_set_set_rational(bset);
2168 bset = isl_basic_set_simplify(bset);
2169 return bset;
2172 /* Return a basic set containing those elements in the space
2173 * of aff where it is non-negative.
2175 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2177 return aff_nonneg_basic_set(aff, 0);
2180 /* Return a basic set containing those elements in the domain space
2181 * of aff where it is negative.
2183 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2185 aff = isl_aff_neg(aff);
2186 aff = isl_aff_add_constant_num_si(aff, -1);
2187 return isl_aff_nonneg_basic_set(aff);
2190 /* Return a basic set containing those elements in the space
2191 * of aff where it is zero.
2192 * If "rational" is set, then return a rational basic set.
2194 * If "aff" is NaN, then it is not zero.
2196 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2197 int rational)
2199 isl_constraint *ineq;
2200 isl_basic_set *bset;
2202 if (!aff)
2203 return NULL;
2204 if (isl_aff_is_nan(aff)) {
2205 isl_space *space = isl_aff_get_domain_space(aff);
2206 isl_aff_free(aff);
2207 return isl_basic_set_empty(space);
2210 ineq = isl_equality_from_aff(aff);
2212 bset = isl_basic_set_from_constraint(ineq);
2213 if (rational)
2214 bset = isl_basic_set_set_rational(bset);
2215 bset = isl_basic_set_simplify(bset);
2216 return bset;
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is zero.
2222 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2224 return aff_zero_basic_set(aff, 0);
2227 /* Return a basic set containing those elements in the shared space
2228 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2230 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2231 __isl_take isl_aff *aff2)
2233 aff1 = isl_aff_sub(aff1, aff2);
2235 return isl_aff_nonneg_basic_set(aff1);
2238 /* Return a basic set containing those elements in the shared space
2239 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2241 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2242 __isl_take isl_aff *aff2)
2244 return isl_aff_ge_basic_set(aff2, aff1);
2247 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2248 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2250 aff1 = isl_aff_add(aff1, aff2);
2251 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2252 return aff1;
2255 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2257 if (!aff)
2258 return -1;
2260 return 0;
2263 /* Check whether the given affine expression has non-zero coefficient
2264 * for any dimension in the given range or if any of these dimensions
2265 * appear with non-zero coefficients in any of the integer divisions
2266 * involved in the affine expression.
2268 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2269 enum isl_dim_type type, unsigned first, unsigned n)
2271 int i;
2272 isl_ctx *ctx;
2273 int *active = NULL;
2274 int involves = 0;
2276 if (!aff)
2277 return -1;
2278 if (n == 0)
2279 return 0;
2281 ctx = isl_aff_get_ctx(aff);
2282 if (first + n > isl_aff_dim(aff, type))
2283 isl_die(ctx, isl_error_invalid,
2284 "range out of bounds", return -1);
2286 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2287 if (!active)
2288 goto error;
2290 first += isl_local_space_offset(aff->ls, type) - 1;
2291 for (i = 0; i < n; ++i)
2292 if (active[first + i]) {
2293 involves = 1;
2294 break;
2297 free(active);
2299 return involves;
2300 error:
2301 free(active);
2302 return -1;
2305 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2306 enum isl_dim_type type, unsigned first, unsigned n)
2308 isl_ctx *ctx;
2310 if (!aff)
2311 return NULL;
2312 if (type == isl_dim_out)
2313 isl_die(aff->v->ctx, isl_error_invalid,
2314 "cannot drop output/set dimension",
2315 return isl_aff_free(aff));
2316 if (type == isl_dim_in)
2317 type = isl_dim_set;
2318 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2319 return aff;
2321 ctx = isl_aff_get_ctx(aff);
2322 if (first + n > isl_local_space_dim(aff->ls, type))
2323 isl_die(ctx, isl_error_invalid, "range out of bounds",
2324 return isl_aff_free(aff));
2326 aff = isl_aff_cow(aff);
2327 if (!aff)
2328 return NULL;
2330 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2331 if (!aff->ls)
2332 return isl_aff_free(aff);
2334 first += 1 + isl_local_space_offset(aff->ls, type);
2335 aff->v = isl_vec_drop_els(aff->v, first, n);
2336 if (!aff->v)
2337 return isl_aff_free(aff);
2339 return aff;
2342 /* Project the domain of the affine expression onto its parameter space.
2343 * The affine expression may not involve any of the domain dimensions.
2345 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2347 isl_space *space;
2348 unsigned n;
2349 int involves;
2351 n = isl_aff_dim(aff, isl_dim_in);
2352 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2353 if (involves < 0)
2354 return isl_aff_free(aff);
2355 if (involves)
2356 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2357 "affine expression involves some of the domain dimensions",
2358 return isl_aff_free(aff));
2359 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2360 space = isl_aff_get_domain_space(aff);
2361 space = isl_space_params(space);
2362 aff = isl_aff_reset_domain_space(aff, space);
2363 return aff;
2366 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2367 enum isl_dim_type type, unsigned first, unsigned n)
2369 isl_ctx *ctx;
2371 if (!aff)
2372 return NULL;
2373 if (type == isl_dim_out)
2374 isl_die(aff->v->ctx, isl_error_invalid,
2375 "cannot insert output/set dimensions",
2376 return isl_aff_free(aff));
2377 if (type == isl_dim_in)
2378 type = isl_dim_set;
2379 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2380 return aff;
2382 ctx = isl_aff_get_ctx(aff);
2383 if (first > isl_local_space_dim(aff->ls, type))
2384 isl_die(ctx, isl_error_invalid, "position out of bounds",
2385 return isl_aff_free(aff));
2387 aff = isl_aff_cow(aff);
2388 if (!aff)
2389 return NULL;
2391 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2392 if (!aff->ls)
2393 return isl_aff_free(aff);
2395 first += 1 + isl_local_space_offset(aff->ls, type);
2396 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2397 if (!aff->v)
2398 return isl_aff_free(aff);
2400 return aff;
2403 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2404 enum isl_dim_type type, unsigned n)
2406 unsigned pos;
2408 pos = isl_aff_dim(aff, type);
2410 return isl_aff_insert_dims(aff, type, pos, n);
2413 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2414 enum isl_dim_type type, unsigned n)
2416 unsigned pos;
2418 pos = isl_pw_aff_dim(pwaff, type);
2420 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2423 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2424 * to dimensions of "dst_type" at "dst_pos".
2426 * We only support moving input dimensions to parameters and vice versa.
2428 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2429 enum isl_dim_type dst_type, unsigned dst_pos,
2430 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2432 unsigned g_dst_pos;
2433 unsigned g_src_pos;
2435 if (!aff)
2436 return NULL;
2437 if (n == 0 &&
2438 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2439 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2440 return aff;
2442 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2443 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2444 "cannot move output/set dimension", isl_aff_free(aff));
2445 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2446 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2447 "cannot move divs", isl_aff_free(aff));
2448 if (dst_type == isl_dim_in)
2449 dst_type = isl_dim_set;
2450 if (src_type == isl_dim_in)
2451 src_type = isl_dim_set;
2453 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2454 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2455 "range out of bounds", isl_aff_free(aff));
2456 if (dst_type == src_type)
2457 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2458 "moving dims within the same type not supported",
2459 isl_aff_free(aff));
2461 aff = isl_aff_cow(aff);
2462 if (!aff)
2463 return NULL;
2465 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2466 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2467 if (dst_type > src_type)
2468 g_dst_pos -= n;
2470 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2471 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2472 src_type, src_pos, n);
2473 if (!aff->v || !aff->ls)
2474 return isl_aff_free(aff);
2476 aff = sort_divs(aff);
2478 return aff;
2481 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2483 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2484 return isl_pw_aff_alloc(dom, aff);
2487 #undef PW
2488 #define PW isl_pw_aff
2489 #undef EL
2490 #define EL isl_aff
2491 #undef EL_IS_ZERO
2492 #define EL_IS_ZERO is_empty
2493 #undef ZERO
2494 #define ZERO empty
2495 #undef IS_ZERO
2496 #define IS_ZERO is_empty
2497 #undef FIELD
2498 #define FIELD aff
2499 #undef DEFAULT_IS_ZERO
2500 #define DEFAULT_IS_ZERO 0
2502 #define NO_EVAL
2503 #define NO_OPT
2504 #define NO_LIFT
2505 #define NO_MORPH
2507 #include <isl_pw_templ.c>
2509 static __isl_give isl_set *align_params_pw_pw_set_and(
2510 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2511 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2512 __isl_take isl_pw_aff *pwaff2))
2514 if (!pwaff1 || !pwaff2)
2515 goto error;
2516 if (isl_space_match(pwaff1->dim, isl_dim_param,
2517 pwaff2->dim, isl_dim_param))
2518 return fn(pwaff1, pwaff2);
2519 if (!isl_space_has_named_params(pwaff1->dim) ||
2520 !isl_space_has_named_params(pwaff2->dim))
2521 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2522 "unaligned unnamed parameters", goto error);
2523 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2524 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2525 return fn(pwaff1, pwaff2);
2526 error:
2527 isl_pw_aff_free(pwaff1);
2528 isl_pw_aff_free(pwaff2);
2529 return NULL;
2532 /* Compute a piecewise quasi-affine expression with a domain that
2533 * is the union of those of pwaff1 and pwaff2 and such that on each
2534 * cell, the quasi-affine expression is the better (according to cmp)
2535 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2536 * is defined on a given cell, then the associated expression
2537 * is the defined one.
2539 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2540 __isl_take isl_pw_aff *pwaff2,
2541 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2542 __isl_take isl_aff *aff2))
2544 int i, j, n;
2545 isl_pw_aff *res;
2546 isl_ctx *ctx;
2547 isl_set *set;
2549 if (!pwaff1 || !pwaff2)
2550 goto error;
2552 ctx = isl_space_get_ctx(pwaff1->dim);
2553 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2554 isl_die(ctx, isl_error_invalid,
2555 "arguments should live in same space", goto error);
2557 if (isl_pw_aff_is_empty(pwaff1)) {
2558 isl_pw_aff_free(pwaff1);
2559 return pwaff2;
2562 if (isl_pw_aff_is_empty(pwaff2)) {
2563 isl_pw_aff_free(pwaff2);
2564 return pwaff1;
2567 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2568 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2570 for (i = 0; i < pwaff1->n; ++i) {
2571 set = isl_set_copy(pwaff1->p[i].set);
2572 for (j = 0; j < pwaff2->n; ++j) {
2573 struct isl_set *common;
2574 isl_set *better;
2576 common = isl_set_intersect(
2577 isl_set_copy(pwaff1->p[i].set),
2578 isl_set_copy(pwaff2->p[j].set));
2579 better = isl_set_from_basic_set(cmp(
2580 isl_aff_copy(pwaff2->p[j].aff),
2581 isl_aff_copy(pwaff1->p[i].aff)));
2582 better = isl_set_intersect(common, better);
2583 if (isl_set_plain_is_empty(better)) {
2584 isl_set_free(better);
2585 continue;
2587 set = isl_set_subtract(set, isl_set_copy(better));
2589 res = isl_pw_aff_add_piece(res, better,
2590 isl_aff_copy(pwaff2->p[j].aff));
2592 res = isl_pw_aff_add_piece(res, set,
2593 isl_aff_copy(pwaff1->p[i].aff));
2596 for (j = 0; j < pwaff2->n; ++j) {
2597 set = isl_set_copy(pwaff2->p[j].set);
2598 for (i = 0; i < pwaff1->n; ++i)
2599 set = isl_set_subtract(set,
2600 isl_set_copy(pwaff1->p[i].set));
2601 res = isl_pw_aff_add_piece(res, set,
2602 isl_aff_copy(pwaff2->p[j].aff));
2605 isl_pw_aff_free(pwaff1);
2606 isl_pw_aff_free(pwaff2);
2608 return res;
2609 error:
2610 isl_pw_aff_free(pwaff1);
2611 isl_pw_aff_free(pwaff2);
2612 return NULL;
2615 /* Compute a piecewise quasi-affine expression with a domain that
2616 * is the union of those of pwaff1 and pwaff2 and such that on each
2617 * cell, the quasi-affine expression is the maximum of those of pwaff1
2618 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2619 * cell, then the associated expression is the defined one.
2621 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2622 __isl_take isl_pw_aff *pwaff2)
2624 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2627 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2628 __isl_take isl_pw_aff *pwaff2)
2630 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2631 &pw_aff_union_max);
2634 /* Compute a piecewise quasi-affine expression with a domain that
2635 * is the union of those of pwaff1 and pwaff2 and such that on each
2636 * cell, the quasi-affine expression is the minimum of those of pwaff1
2637 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2638 * cell, then the associated expression is the defined one.
2640 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2641 __isl_take isl_pw_aff *pwaff2)
2643 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2646 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2647 __isl_take isl_pw_aff *pwaff2)
2649 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2650 &pw_aff_union_min);
2653 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2654 __isl_take isl_pw_aff *pwaff2, int max)
2656 if (max)
2657 return isl_pw_aff_union_max(pwaff1, pwaff2);
2658 else
2659 return isl_pw_aff_union_min(pwaff1, pwaff2);
2662 /* Construct a map with as domain the domain of pwaff and
2663 * one-dimensional range corresponding to the affine expressions.
2665 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2667 int i;
2668 isl_space *dim;
2669 isl_map *map;
2671 if (!pwaff)
2672 return NULL;
2674 dim = isl_pw_aff_get_space(pwaff);
2675 map = isl_map_empty(dim);
2677 for (i = 0; i < pwaff->n; ++i) {
2678 isl_basic_map *bmap;
2679 isl_map *map_i;
2681 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2682 map_i = isl_map_from_basic_map(bmap);
2683 map_i = isl_map_intersect_domain(map_i,
2684 isl_set_copy(pwaff->p[i].set));
2685 map = isl_map_union_disjoint(map, map_i);
2688 isl_pw_aff_free(pwaff);
2690 return map;
2693 /* Construct a map with as domain the domain of pwaff and
2694 * one-dimensional range corresponding to the affine expressions.
2696 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2698 if (!pwaff)
2699 return NULL;
2700 if (isl_space_is_set(pwaff->dim))
2701 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2702 "space of input is not a map", goto error);
2703 return map_from_pw_aff(pwaff);
2704 error:
2705 isl_pw_aff_free(pwaff);
2706 return NULL;
2709 /* Construct a one-dimensional set with as parameter domain
2710 * the domain of pwaff and the single set dimension
2711 * corresponding to the affine expressions.
2713 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2715 if (!pwaff)
2716 return NULL;
2717 if (!isl_space_is_set(pwaff->dim))
2718 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2719 "space of input is not a set", goto error);
2720 return map_from_pw_aff(pwaff);
2721 error:
2722 isl_pw_aff_free(pwaff);
2723 return NULL;
2726 /* Return a set containing those elements in the domain
2727 * of pwaff where it is non-negative.
2729 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2731 int i;
2732 isl_set *set;
2734 if (!pwaff)
2735 return NULL;
2737 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2739 for (i = 0; i < pwaff->n; ++i) {
2740 isl_basic_set *bset;
2741 isl_set *set_i;
2742 int rational;
2744 rational = isl_set_has_rational(pwaff->p[i].set);
2745 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2746 rational);
2747 set_i = isl_set_from_basic_set(bset);
2748 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2749 set = isl_set_union_disjoint(set, set_i);
2752 isl_pw_aff_free(pwaff);
2754 return set;
2757 /* Return a set containing those elements in the domain
2758 * of pwaff where it is zero (if complement is 0) or not zero
2759 * (if complement is 1).
2761 * The pieces with a NaN never belong to the result since
2762 * NaN is neither zero nor non-zero.
2764 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2765 int complement)
2767 int i;
2768 isl_set *set;
2770 if (!pwaff)
2771 return NULL;
2773 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2775 for (i = 0; i < pwaff->n; ++i) {
2776 isl_basic_set *bset;
2777 isl_set *set_i, *zero;
2778 int rational;
2780 if (isl_aff_is_nan(pwaff->p[i].aff))
2781 continue;
2783 rational = isl_set_has_rational(pwaff->p[i].set);
2784 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2785 rational);
2786 zero = isl_set_from_basic_set(bset);
2787 set_i = isl_set_copy(pwaff->p[i].set);
2788 if (complement)
2789 set_i = isl_set_subtract(set_i, zero);
2790 else
2791 set_i = isl_set_intersect(set_i, zero);
2792 set = isl_set_union_disjoint(set, set_i);
2795 isl_pw_aff_free(pwaff);
2797 return set;
2800 /* Return a set containing those elements in the domain
2801 * of pwaff where it is zero.
2803 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2805 return pw_aff_zero_set(pwaff, 0);
2808 /* Return a set containing those elements in the domain
2809 * of pwaff where it is not zero.
2811 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2813 return pw_aff_zero_set(pwaff, 1);
2816 /* Return a set containing those elements in the shared domain
2817 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2819 * We compute the difference on the shared domain and then construct
2820 * the set of values where this difference is non-negative.
2821 * If strict is set, we first subtract 1 from the difference.
2822 * If equal is set, we only return the elements where pwaff1 and pwaff2
2823 * are equal.
2825 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2826 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2828 isl_set *set1, *set2;
2830 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2831 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2832 set1 = isl_set_intersect(set1, set2);
2833 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2834 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2835 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2837 if (strict) {
2838 isl_space *dim = isl_set_get_space(set1);
2839 isl_aff *aff;
2840 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2841 aff = isl_aff_add_constant_si(aff, -1);
2842 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2843 } else
2844 isl_set_free(set1);
2846 if (equal)
2847 return isl_pw_aff_zero_set(pwaff1);
2848 return isl_pw_aff_nonneg_set(pwaff1);
2851 /* Return a set containing those elements in the shared domain
2852 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2854 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2855 __isl_take isl_pw_aff *pwaff2)
2857 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2860 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2861 __isl_take isl_pw_aff *pwaff2)
2863 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2866 /* Return a set containing those elements in the shared domain
2867 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2869 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2870 __isl_take isl_pw_aff *pwaff2)
2872 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2875 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2876 __isl_take isl_pw_aff *pwaff2)
2878 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2881 /* Return a set containing those elements in the shared domain
2882 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2884 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2885 __isl_take isl_pw_aff *pwaff2)
2887 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2890 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2891 __isl_take isl_pw_aff *pwaff2)
2893 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2896 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2897 __isl_take isl_pw_aff *pwaff2)
2899 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2902 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2903 __isl_take isl_pw_aff *pwaff2)
2905 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2908 /* Return a set containing those elements in the shared domain
2909 * of the elements of list1 and list2 where each element in list1
2910 * has the relation specified by "fn" with each element in list2.
2912 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2913 __isl_take isl_pw_aff_list *list2,
2914 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2915 __isl_take isl_pw_aff *pwaff2))
2917 int i, j;
2918 isl_ctx *ctx;
2919 isl_set *set;
2921 if (!list1 || !list2)
2922 goto error;
2924 ctx = isl_pw_aff_list_get_ctx(list1);
2925 if (list1->n < 1 || list2->n < 1)
2926 isl_die(ctx, isl_error_invalid,
2927 "list should contain at least one element", goto error);
2929 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2930 for (i = 0; i < list1->n; ++i)
2931 for (j = 0; j < list2->n; ++j) {
2932 isl_set *set_ij;
2934 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2935 isl_pw_aff_copy(list2->p[j]));
2936 set = isl_set_intersect(set, set_ij);
2939 isl_pw_aff_list_free(list1);
2940 isl_pw_aff_list_free(list2);
2941 return set;
2942 error:
2943 isl_pw_aff_list_free(list1);
2944 isl_pw_aff_list_free(list2);
2945 return NULL;
2948 /* Return a set containing those elements in the shared domain
2949 * of the elements of list1 and list2 where each element in list1
2950 * is equal to each element in list2.
2952 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2953 __isl_take isl_pw_aff_list *list2)
2955 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2958 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2959 __isl_take isl_pw_aff_list *list2)
2961 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2964 /* Return a set containing those elements in the shared domain
2965 * of the elements of list1 and list2 where each element in list1
2966 * is less than or equal to each element in list2.
2968 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2969 __isl_take isl_pw_aff_list *list2)
2971 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2974 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2975 __isl_take isl_pw_aff_list *list2)
2977 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2980 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2981 __isl_take isl_pw_aff_list *list2)
2983 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2986 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2987 __isl_take isl_pw_aff_list *list2)
2989 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2993 /* Return a set containing those elements in the shared domain
2994 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2996 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2997 __isl_take isl_pw_aff *pwaff2)
2999 isl_set *set_lt, *set_gt;
3001 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3002 isl_pw_aff_copy(pwaff2));
3003 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3004 return isl_set_union_disjoint(set_lt, set_gt);
3007 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3008 __isl_take isl_pw_aff *pwaff2)
3010 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3013 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3014 isl_int v)
3016 int i;
3018 if (isl_int_is_one(v))
3019 return pwaff;
3020 if (!isl_int_is_pos(v))
3021 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3022 "factor needs to be positive",
3023 return isl_pw_aff_free(pwaff));
3024 pwaff = isl_pw_aff_cow(pwaff);
3025 if (!pwaff)
3026 return NULL;
3027 if (pwaff->n == 0)
3028 return pwaff;
3030 for (i = 0; i < pwaff->n; ++i) {
3031 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3032 if (!pwaff->p[i].aff)
3033 return isl_pw_aff_free(pwaff);
3036 return pwaff;
3039 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3041 int i;
3043 pwaff = isl_pw_aff_cow(pwaff);
3044 if (!pwaff)
3045 return NULL;
3046 if (pwaff->n == 0)
3047 return pwaff;
3049 for (i = 0; i < pwaff->n; ++i) {
3050 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3051 if (!pwaff->p[i].aff)
3052 return isl_pw_aff_free(pwaff);
3055 return pwaff;
3058 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3060 int i;
3062 pwaff = isl_pw_aff_cow(pwaff);
3063 if (!pwaff)
3064 return NULL;
3065 if (pwaff->n == 0)
3066 return pwaff;
3068 for (i = 0; i < pwaff->n; ++i) {
3069 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3070 if (!pwaff->p[i].aff)
3071 return isl_pw_aff_free(pwaff);
3074 return pwaff;
3077 /* Assuming that "cond1" and "cond2" are disjoint,
3078 * return an affine expression that is equal to pwaff1 on cond1
3079 * and to pwaff2 on cond2.
3081 static __isl_give isl_pw_aff *isl_pw_aff_select(
3082 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3083 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3085 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3086 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3088 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3091 /* Return an affine expression that is equal to pwaff_true for elements
3092 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3093 * is zero.
3094 * That is, return cond ? pwaff_true : pwaff_false;
3096 * If "cond" involves and NaN, then we conservatively return a NaN
3097 * on its entire domain. In principle, we could consider the pieces
3098 * where it is NaN separately from those where it is not.
3100 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3101 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3103 isl_set *cond_true, *cond_false;
3105 if (!cond)
3106 goto error;
3107 if (isl_pw_aff_involves_nan(cond)) {
3108 isl_space *space = isl_pw_aff_get_domain_space(cond);
3109 isl_local_space *ls = isl_local_space_from_space(space);
3110 isl_pw_aff_free(cond);
3111 isl_pw_aff_free(pwaff_true);
3112 isl_pw_aff_free(pwaff_false);
3113 return isl_pw_aff_nan_on_domain(ls);
3116 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3117 cond_false = isl_pw_aff_zero_set(cond);
3118 return isl_pw_aff_select(cond_true, pwaff_true,
3119 cond_false, pwaff_false);
3120 error:
3121 isl_pw_aff_free(cond);
3122 isl_pw_aff_free(pwaff_true);
3123 isl_pw_aff_free(pwaff_false);
3124 return NULL;
3127 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3129 if (!aff)
3130 return -1;
3132 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3135 /* Check whether pwaff is a piecewise constant.
3137 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3139 int i;
3141 if (!pwaff)
3142 return -1;
3144 for (i = 0; i < pwaff->n; ++i) {
3145 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3146 if (is_cst < 0 || !is_cst)
3147 return is_cst;
3150 return 1;
3153 /* Return the product of "aff1" and "aff2".
3155 * If either of the two is NaN, then the result is NaN.
3157 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3159 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3160 __isl_take isl_aff *aff2)
3162 if (!aff1 || !aff2)
3163 goto error;
3165 if (isl_aff_is_nan(aff1)) {
3166 isl_aff_free(aff2);
3167 return aff1;
3169 if (isl_aff_is_nan(aff2)) {
3170 isl_aff_free(aff1);
3171 return aff2;
3174 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3175 return isl_aff_mul(aff2, aff1);
3177 if (!isl_aff_is_cst(aff2))
3178 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3179 "at least one affine expression should be constant",
3180 goto error);
3182 aff1 = isl_aff_cow(aff1);
3183 if (!aff1 || !aff2)
3184 goto error;
3186 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3187 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3189 isl_aff_free(aff2);
3190 return aff1;
3191 error:
3192 isl_aff_free(aff1);
3193 isl_aff_free(aff2);
3194 return NULL;
3197 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3199 * If either of the two is NaN, then the result is NaN.
3201 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3202 __isl_take isl_aff *aff2)
3204 int is_cst;
3205 int neg;
3207 if (!aff1 || !aff2)
3208 goto error;
3210 if (isl_aff_is_nan(aff1)) {
3211 isl_aff_free(aff2);
3212 return aff1;
3214 if (isl_aff_is_nan(aff2)) {
3215 isl_aff_free(aff1);
3216 return aff2;
3219 is_cst = isl_aff_is_cst(aff2);
3220 if (is_cst < 0)
3221 goto error;
3222 if (!is_cst)
3223 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3224 "second argument should be a constant", goto error);
3226 if (!aff2)
3227 goto error;
3229 neg = isl_int_is_neg(aff2->v->el[1]);
3230 if (neg) {
3231 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3232 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3235 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3236 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3238 if (neg) {
3239 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3240 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3243 isl_aff_free(aff2);
3244 return aff1;
3245 error:
3246 isl_aff_free(aff1);
3247 isl_aff_free(aff2);
3248 return NULL;
3251 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3252 __isl_take isl_pw_aff *pwaff2)
3254 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3257 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3258 __isl_take isl_pw_aff *pwaff2)
3260 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3263 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3264 __isl_take isl_pw_aff *pwaff2)
3266 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3269 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3270 __isl_take isl_pw_aff *pwaff2)
3272 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3275 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3276 __isl_take isl_pw_aff *pwaff2)
3278 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3281 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3282 __isl_take isl_pw_aff *pa2)
3284 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3287 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3289 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3290 __isl_take isl_pw_aff *pa2)
3292 int is_cst;
3294 is_cst = isl_pw_aff_is_cst(pa2);
3295 if (is_cst < 0)
3296 goto error;
3297 if (!is_cst)
3298 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3299 "second argument should be a piecewise constant",
3300 goto error);
3301 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3302 error:
3303 isl_pw_aff_free(pa1);
3304 isl_pw_aff_free(pa2);
3305 return NULL;
3308 /* Compute the quotient of the integer division of "pa1" by "pa2"
3309 * with rounding towards zero.
3310 * "pa2" is assumed to be a piecewise constant.
3312 * In particular, return
3314 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3317 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3318 __isl_take isl_pw_aff *pa2)
3320 int is_cst;
3321 isl_set *cond;
3322 isl_pw_aff *f, *c;
3324 is_cst = isl_pw_aff_is_cst(pa2);
3325 if (is_cst < 0)
3326 goto error;
3327 if (!is_cst)
3328 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3329 "second argument should be a piecewise constant",
3330 goto error);
3332 pa1 = isl_pw_aff_div(pa1, pa2);
3334 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3335 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3336 c = isl_pw_aff_ceil(pa1);
3337 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3338 error:
3339 isl_pw_aff_free(pa1);
3340 isl_pw_aff_free(pa2);
3341 return NULL;
3344 /* Compute the remainder of the integer division of "pa1" by "pa2"
3345 * with rounding towards zero.
3346 * "pa2" is assumed to be a piecewise constant.
3348 * In particular, return
3350 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3353 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3354 __isl_take isl_pw_aff *pa2)
3356 int is_cst;
3357 isl_pw_aff *res;
3359 is_cst = isl_pw_aff_is_cst(pa2);
3360 if (is_cst < 0)
3361 goto error;
3362 if (!is_cst)
3363 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3364 "second argument should be a piecewise constant",
3365 goto error);
3366 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3367 res = isl_pw_aff_mul(pa2, res);
3368 res = isl_pw_aff_sub(pa1, res);
3369 return res;
3370 error:
3371 isl_pw_aff_free(pa1);
3372 isl_pw_aff_free(pa2);
3373 return NULL;
3376 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3377 __isl_take isl_pw_aff *pwaff2)
3379 isl_set *le;
3380 isl_set *dom;
3382 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3383 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3384 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3385 isl_pw_aff_copy(pwaff2));
3386 dom = isl_set_subtract(dom, isl_set_copy(le));
3387 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3390 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3391 __isl_take isl_pw_aff *pwaff2)
3393 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3396 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3397 __isl_take isl_pw_aff *pwaff2)
3399 isl_set *ge;
3400 isl_set *dom;
3402 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3403 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3404 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3405 isl_pw_aff_copy(pwaff2));
3406 dom = isl_set_subtract(dom, isl_set_copy(ge));
3407 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3410 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3411 __isl_take isl_pw_aff *pwaff2)
3413 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3416 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3417 __isl_take isl_pw_aff_list *list,
3418 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3419 __isl_take isl_pw_aff *pwaff2))
3421 int i;
3422 isl_ctx *ctx;
3423 isl_pw_aff *res;
3425 if (!list)
3426 return NULL;
3428 ctx = isl_pw_aff_list_get_ctx(list);
3429 if (list->n < 1)
3430 isl_die(ctx, isl_error_invalid,
3431 "list should contain at least one element", goto error);
3433 res = isl_pw_aff_copy(list->p[0]);
3434 for (i = 1; i < list->n; ++i)
3435 res = fn(res, isl_pw_aff_copy(list->p[i]));
3437 isl_pw_aff_list_free(list);
3438 return res;
3439 error:
3440 isl_pw_aff_list_free(list);
3441 return NULL;
3444 /* Return an isl_pw_aff that maps each element in the intersection of the
3445 * domains of the elements of list to the minimal corresponding affine
3446 * expression.
3448 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3450 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3453 /* Return an isl_pw_aff that maps each element in the intersection of the
3454 * domains of the elements of list to the maximal corresponding affine
3455 * expression.
3457 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3459 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3462 /* Mark the domains of "pwaff" as rational.
3464 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3466 int i;
3468 pwaff = isl_pw_aff_cow(pwaff);
3469 if (!pwaff)
3470 return NULL;
3471 if (pwaff->n == 0)
3472 return pwaff;
3474 for (i = 0; i < pwaff->n; ++i) {
3475 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3476 if (!pwaff->p[i].set)
3477 return isl_pw_aff_free(pwaff);
3480 return pwaff;
3483 /* Mark the domains of the elements of "list" as rational.
3485 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3486 __isl_take isl_pw_aff_list *list)
3488 int i, n;
3490 if (!list)
3491 return NULL;
3492 if (list->n == 0)
3493 return list;
3495 n = list->n;
3496 for (i = 0; i < n; ++i) {
3497 isl_pw_aff *pa;
3499 pa = isl_pw_aff_list_get_pw_aff(list, i);
3500 pa = isl_pw_aff_set_rational(pa);
3501 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3504 return list;
3507 /* Do the parameters of "aff" match those of "space"?
3509 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3510 __isl_keep isl_space *space)
3512 isl_space *aff_space;
3513 int match;
3515 if (!aff || !space)
3516 return -1;
3518 aff_space = isl_aff_get_domain_space(aff);
3520 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3522 isl_space_free(aff_space);
3523 return match;
3526 /* Check that the domain space of "aff" matches "space".
3528 * Return 0 on success and -1 on error.
3530 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3531 __isl_keep isl_space *space)
3533 isl_space *aff_space;
3534 int match;
3536 if (!aff || !space)
3537 return -1;
3539 aff_space = isl_aff_get_domain_space(aff);
3541 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3542 if (match < 0)
3543 goto error;
3544 if (!match)
3545 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3546 "parameters don't match", goto error);
3547 match = isl_space_tuple_is_equal(space, isl_dim_in,
3548 aff_space, isl_dim_set);
3549 if (match < 0)
3550 goto error;
3551 if (!match)
3552 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3553 "domains don't match", goto error);
3554 isl_space_free(aff_space);
3555 return 0;
3556 error:
3557 isl_space_free(aff_space);
3558 return -1;
3561 #undef BASE
3562 #define BASE aff
3563 #define NO_INTERSECT_DOMAIN
3564 #define NO_DOMAIN
3566 #include <isl_multi_templ.c>
3568 #undef NO_DOMAIN
3569 #undef NO_INTERSECT_DOMAIN
3571 /* Remove any internal structure of the domain of "ma".
3572 * If there is any such internal structure in the input,
3573 * then the name of the corresponding space is also removed.
3575 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3576 __isl_take isl_multi_aff *ma)
3578 isl_space *space;
3580 if (!ma)
3581 return NULL;
3583 if (!ma->space->nested[0])
3584 return ma;
3586 space = isl_multi_aff_get_space(ma);
3587 space = isl_space_flatten_domain(space);
3588 ma = isl_multi_aff_reset_space(ma, space);
3590 return ma;
3593 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3594 * of the space to its domain.
3596 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3598 int i, n_in;
3599 isl_local_space *ls;
3600 isl_multi_aff *ma;
3602 if (!space)
3603 return NULL;
3604 if (!isl_space_is_map(space))
3605 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3606 "not a map space", goto error);
3608 n_in = isl_space_dim(space, isl_dim_in);
3609 space = isl_space_domain_map(space);
3611 ma = isl_multi_aff_alloc(isl_space_copy(space));
3612 if (n_in == 0) {
3613 isl_space_free(space);
3614 return ma;
3617 space = isl_space_domain(space);
3618 ls = isl_local_space_from_space(space);
3619 for (i = 0; i < n_in; ++i) {
3620 isl_aff *aff;
3622 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3623 isl_dim_set, i);
3624 ma = isl_multi_aff_set_aff(ma, i, aff);
3626 isl_local_space_free(ls);
3627 return ma;
3628 error:
3629 isl_space_free(space);
3630 return NULL;
3633 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3634 * of the space to its range.
3636 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3638 int i, n_in, n_out;
3639 isl_local_space *ls;
3640 isl_multi_aff *ma;
3642 if (!space)
3643 return NULL;
3644 if (!isl_space_is_map(space))
3645 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3646 "not a map space", goto error);
3648 n_in = isl_space_dim(space, isl_dim_in);
3649 n_out = isl_space_dim(space, isl_dim_out);
3650 space = isl_space_range_map(space);
3652 ma = isl_multi_aff_alloc(isl_space_copy(space));
3653 if (n_out == 0) {
3654 isl_space_free(space);
3655 return ma;
3658 space = isl_space_domain(space);
3659 ls = isl_local_space_from_space(space);
3660 for (i = 0; i < n_out; ++i) {
3661 isl_aff *aff;
3663 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3664 isl_dim_set, n_in + i);
3665 ma = isl_multi_aff_set_aff(ma, i, aff);
3667 isl_local_space_free(ls);
3668 return ma;
3669 error:
3670 isl_space_free(space);
3671 return NULL;
3674 /* Given the space of a set and a range of set dimensions,
3675 * construct an isl_multi_aff that projects out those dimensions.
3677 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3678 __isl_take isl_space *space, enum isl_dim_type type,
3679 unsigned first, unsigned n)
3681 int i, dim;
3682 isl_local_space *ls;
3683 isl_multi_aff *ma;
3685 if (!space)
3686 return NULL;
3687 if (!isl_space_is_set(space))
3688 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3689 "expecting set space", goto error);
3690 if (type != isl_dim_set)
3691 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3692 "only set dimensions can be projected out", goto error);
3694 dim = isl_space_dim(space, isl_dim_set);
3695 if (first + n > dim)
3696 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3697 "range out of bounds", goto error);
3699 space = isl_space_from_domain(space);
3700 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3702 if (dim == n)
3703 return isl_multi_aff_alloc(space);
3705 ma = isl_multi_aff_alloc(isl_space_copy(space));
3706 space = isl_space_domain(space);
3707 ls = isl_local_space_from_space(space);
3709 for (i = 0; i < first; ++i) {
3710 isl_aff *aff;
3712 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3713 isl_dim_set, i);
3714 ma = isl_multi_aff_set_aff(ma, i, aff);
3717 for (i = 0; i < dim - (first + n); ++i) {
3718 isl_aff *aff;
3720 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3721 isl_dim_set, first + n + i);
3722 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3725 isl_local_space_free(ls);
3726 return ma;
3727 error:
3728 isl_space_free(space);
3729 return NULL;
3732 /* Given the space of a set and a range of set dimensions,
3733 * construct an isl_pw_multi_aff that projects out those dimensions.
3735 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3736 __isl_take isl_space *space, enum isl_dim_type type,
3737 unsigned first, unsigned n)
3739 isl_multi_aff *ma;
3741 ma = isl_multi_aff_project_out_map(space, type, first, n);
3742 return isl_pw_multi_aff_from_multi_aff(ma);
3745 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3746 * domain.
3748 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3749 __isl_take isl_multi_aff *ma)
3751 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3752 return isl_pw_multi_aff_alloc(dom, ma);
3755 /* Create a piecewise multi-affine expression in the given space that maps each
3756 * input dimension to the corresponding output dimension.
3758 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3759 __isl_take isl_space *space)
3761 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3764 /* Add "ma2" to "ma1" and return the result.
3766 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3768 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3769 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3771 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3774 /* Add "ma2" to "ma1" and return the result.
3776 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3777 __isl_take isl_multi_aff *ma2)
3779 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3780 &isl_multi_aff_add_aligned);
3783 /* Subtract "ma2" from "ma1" and return the result.
3785 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3787 static __isl_give isl_multi_aff *isl_multi_aff_sub_aligned(
3788 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3790 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3793 /* Subtract "ma2" from "ma1" and return the result.
3795 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3796 __isl_take isl_multi_aff *ma2)
3798 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3799 &isl_multi_aff_sub_aligned);
3802 /* Exploit the equalities in "eq" to simplify the affine expressions.
3804 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3805 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3807 int i;
3809 maff = isl_multi_aff_cow(maff);
3810 if (!maff || !eq)
3811 goto error;
3813 for (i = 0; i < maff->n; ++i) {
3814 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3815 isl_basic_set_copy(eq));
3816 if (!maff->p[i])
3817 goto error;
3820 isl_basic_set_free(eq);
3821 return maff;
3822 error:
3823 isl_basic_set_free(eq);
3824 isl_multi_aff_free(maff);
3825 return NULL;
3828 /* Given f, return floor(f).
3830 __isl_give isl_multi_aff *isl_multi_aff_floor(__isl_take isl_multi_aff *ma)
3832 int i;
3834 ma = isl_multi_aff_cow(ma);
3835 if (!ma)
3836 return NULL;
3838 for (i = 0; i < ma->n; ++i) {
3839 ma->p[i] = isl_aff_floor(ma->p[i]);
3840 if (!ma->p[i])
3841 return isl_multi_aff_free(ma);
3844 return ma;
3847 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3848 isl_int f)
3850 int i;
3852 maff = isl_multi_aff_cow(maff);
3853 if (!maff)
3854 return NULL;
3856 for (i = 0; i < maff->n; ++i) {
3857 maff->p[i] = isl_aff_scale(maff->p[i], f);
3858 if (!maff->p[i])
3859 return isl_multi_aff_free(maff);
3862 return maff;
3865 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3866 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3868 maff1 = isl_multi_aff_add(maff1, maff2);
3869 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3870 return maff1;
3873 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3875 if (!maff)
3876 return -1;
3878 return 0;
3881 /* Return the set of domain elements where "ma1" is lexicographically
3882 * smaller than or equal to "ma2".
3884 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3885 __isl_take isl_multi_aff *ma2)
3887 return isl_multi_aff_lex_ge_set(ma2, ma1);
3890 /* Return the set of domain elements where "ma1" is lexicographically
3891 * greater than or equal to "ma2".
3893 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3894 __isl_take isl_multi_aff *ma2)
3896 isl_space *space;
3897 isl_map *map1, *map2;
3898 isl_map *map, *ge;
3900 map1 = isl_map_from_multi_aff(ma1);
3901 map2 = isl_map_from_multi_aff(ma2);
3902 map = isl_map_range_product(map1, map2);
3903 space = isl_space_range(isl_map_get_space(map));
3904 space = isl_space_domain(isl_space_unwrap(space));
3905 ge = isl_map_lex_ge(space);
3906 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3908 return isl_map_domain(map);
3911 #undef PW
3912 #define PW isl_pw_multi_aff
3913 #undef EL
3914 #define EL isl_multi_aff
3915 #undef EL_IS_ZERO
3916 #define EL_IS_ZERO is_empty
3917 #undef ZERO
3918 #define ZERO empty
3919 #undef IS_ZERO
3920 #define IS_ZERO is_empty
3921 #undef FIELD
3922 #define FIELD maff
3923 #undef DEFAULT_IS_ZERO
3924 #define DEFAULT_IS_ZERO 0
3926 #define NO_NEG
3927 #define NO_EVAL
3928 #define NO_OPT
3929 #define NO_INVOLVES_DIMS
3930 #define NO_INSERT_DIMS
3931 #define NO_LIFT
3932 #define NO_MORPH
3934 #include <isl_pw_templ.c>
3936 #undef UNION
3937 #define UNION isl_union_pw_multi_aff
3938 #undef PART
3939 #define PART isl_pw_multi_aff
3940 #undef PARTS
3941 #define PARTS pw_multi_aff
3942 #define ALIGN_DOMAIN
3944 #define NO_EVAL
3946 #include <isl_union_templ.c>
3948 /* Given a function "cmp" that returns the set of elements where
3949 * "ma1" is "better" than "ma2", return the intersection of this
3950 * set with "dom1" and "dom2".
3952 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3953 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3954 __isl_keep isl_multi_aff *ma2,
3955 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3956 __isl_take isl_multi_aff *ma2))
3958 isl_set *common;
3959 isl_set *better;
3960 int is_empty;
3962 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3963 is_empty = isl_set_plain_is_empty(common);
3964 if (is_empty >= 0 && is_empty)
3965 return common;
3966 if (is_empty < 0)
3967 return isl_set_free(common);
3968 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3969 better = isl_set_intersect(common, better);
3971 return better;
3974 /* Given a function "cmp" that returns the set of elements where
3975 * "ma1" is "better" than "ma2", return a piecewise multi affine
3976 * expression defined on the union of the definition domains
3977 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3978 * "pma2" on each cell. If only one of the two input functions
3979 * is defined on a given cell, then it is considered the best.
3981 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3982 __isl_take isl_pw_multi_aff *pma1,
3983 __isl_take isl_pw_multi_aff *pma2,
3984 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3985 __isl_take isl_multi_aff *ma2))
3987 int i, j, n;
3988 isl_pw_multi_aff *res = NULL;
3989 isl_ctx *ctx;
3990 isl_set *set = NULL;
3992 if (!pma1 || !pma2)
3993 goto error;
3995 ctx = isl_space_get_ctx(pma1->dim);
3996 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3997 isl_die(ctx, isl_error_invalid,
3998 "arguments should live in the same space", goto error);
4000 if (isl_pw_multi_aff_is_empty(pma1)) {
4001 isl_pw_multi_aff_free(pma1);
4002 return pma2;
4005 if (isl_pw_multi_aff_is_empty(pma2)) {
4006 isl_pw_multi_aff_free(pma2);
4007 return pma1;
4010 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4011 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4013 for (i = 0; i < pma1->n; ++i) {
4014 set = isl_set_copy(pma1->p[i].set);
4015 for (j = 0; j < pma2->n; ++j) {
4016 isl_set *better;
4017 int is_empty;
4019 better = shared_and_better(pma2->p[j].set,
4020 pma1->p[i].set, pma2->p[j].maff,
4021 pma1->p[i].maff, cmp);
4022 is_empty = isl_set_plain_is_empty(better);
4023 if (is_empty < 0 || is_empty) {
4024 isl_set_free(better);
4025 if (is_empty < 0)
4026 goto error;
4027 continue;
4029 set = isl_set_subtract(set, isl_set_copy(better));
4031 res = isl_pw_multi_aff_add_piece(res, better,
4032 isl_multi_aff_copy(pma2->p[j].maff));
4034 res = isl_pw_multi_aff_add_piece(res, set,
4035 isl_multi_aff_copy(pma1->p[i].maff));
4038 for (j = 0; j < pma2->n; ++j) {
4039 set = isl_set_copy(pma2->p[j].set);
4040 for (i = 0; i < pma1->n; ++i)
4041 set = isl_set_subtract(set,
4042 isl_set_copy(pma1->p[i].set));
4043 res = isl_pw_multi_aff_add_piece(res, set,
4044 isl_multi_aff_copy(pma2->p[j].maff));
4047 isl_pw_multi_aff_free(pma1);
4048 isl_pw_multi_aff_free(pma2);
4050 return res;
4051 error:
4052 isl_pw_multi_aff_free(pma1);
4053 isl_pw_multi_aff_free(pma2);
4054 isl_set_free(set);
4055 return isl_pw_multi_aff_free(res);
4058 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4059 __isl_take isl_pw_multi_aff *pma1,
4060 __isl_take isl_pw_multi_aff *pma2)
4062 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4065 /* Given two piecewise multi affine expressions, return a piecewise
4066 * multi-affine expression defined on the union of the definition domains
4067 * of the inputs that is equal to the lexicographic maximum of the two
4068 * inputs on each cell. If only one of the two inputs is defined on
4069 * a given cell, then it is considered to be the maximum.
4071 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4072 __isl_take isl_pw_multi_aff *pma1,
4073 __isl_take isl_pw_multi_aff *pma2)
4075 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4076 &pw_multi_aff_union_lexmax);
4079 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4080 __isl_take isl_pw_multi_aff *pma1,
4081 __isl_take isl_pw_multi_aff *pma2)
4083 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4086 /* Given two piecewise multi affine expressions, return a piecewise
4087 * multi-affine expression defined on the union of the definition domains
4088 * of the inputs that is equal to the lexicographic minimum of the two
4089 * inputs on each cell. If only one of the two inputs is defined on
4090 * a given cell, then it is considered to be the minimum.
4092 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4093 __isl_take isl_pw_multi_aff *pma1,
4094 __isl_take isl_pw_multi_aff *pma2)
4096 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4097 &pw_multi_aff_union_lexmin);
4100 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4101 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4103 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4104 &isl_multi_aff_add);
4107 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4108 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4110 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4111 &pw_multi_aff_add);
4114 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4115 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4117 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4118 &isl_multi_aff_sub);
4121 /* Subtract "pma2" from "pma1" and return the result.
4123 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4124 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4126 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4127 &pw_multi_aff_sub);
4130 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4131 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4133 return isl_pw_multi_aff_union_add_(pma1, pma2);
4136 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4137 * with the actual sum on the shared domain and
4138 * the defined expression on the symmetric difference of the domains.
4140 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4141 __isl_take isl_union_pw_multi_aff *upma1,
4142 __isl_take isl_union_pw_multi_aff *upma2)
4144 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4147 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4148 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4150 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4151 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4153 int i, j, n;
4154 isl_space *space;
4155 isl_pw_multi_aff *res;
4157 if (!pma1 || !pma2)
4158 goto error;
4160 n = pma1->n * pma2->n;
4161 space = isl_space_product(isl_space_copy(pma1->dim),
4162 isl_space_copy(pma2->dim));
4163 res = isl_pw_multi_aff_alloc_size(space, n);
4165 for (i = 0; i < pma1->n; ++i) {
4166 for (j = 0; j < pma2->n; ++j) {
4167 isl_set *domain;
4168 isl_multi_aff *ma;
4170 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4171 isl_set_copy(pma2->p[j].set));
4172 ma = isl_multi_aff_product(
4173 isl_multi_aff_copy(pma1->p[i].maff),
4174 isl_multi_aff_copy(pma2->p[j].maff));
4175 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4179 isl_pw_multi_aff_free(pma1);
4180 isl_pw_multi_aff_free(pma2);
4181 return res;
4182 error:
4183 isl_pw_multi_aff_free(pma1);
4184 isl_pw_multi_aff_free(pma2);
4185 return NULL;
4188 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4189 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4191 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4192 &pw_multi_aff_product);
4195 /* Construct a map mapping the domain of the piecewise multi-affine expression
4196 * to its range, with each dimension in the range equated to the
4197 * corresponding affine expression on its cell.
4199 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4201 int i;
4202 isl_map *map;
4204 if (!pma)
4205 return NULL;
4207 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4209 for (i = 0; i < pma->n; ++i) {
4210 isl_multi_aff *maff;
4211 isl_basic_map *bmap;
4212 isl_map *map_i;
4214 maff = isl_multi_aff_copy(pma->p[i].maff);
4215 bmap = isl_basic_map_from_multi_aff(maff);
4216 map_i = isl_map_from_basic_map(bmap);
4217 map_i = isl_map_intersect_domain(map_i,
4218 isl_set_copy(pma->p[i].set));
4219 map = isl_map_union_disjoint(map, map_i);
4222 isl_pw_multi_aff_free(pma);
4223 return map;
4226 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4228 if (!pma)
4229 return NULL;
4231 if (!isl_space_is_set(pma->dim))
4232 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4233 "isl_pw_multi_aff cannot be converted into an isl_set",
4234 goto error);
4236 return isl_map_from_pw_multi_aff(pma);
4237 error:
4238 isl_pw_multi_aff_free(pma);
4239 return NULL;
4242 /* Given a basic map with a single output dimension that is defined
4243 * in terms of the parameters and input dimensions using an equality,
4244 * extract an isl_aff that expresses the output dimension in terms
4245 * of the parameters and input dimensions.
4246 * Note that this expression may involve integer divisions defined
4247 * in terms of parameters and input dimensions.
4249 * This function shares some similarities with
4250 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4252 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4253 __isl_take isl_basic_map *bmap)
4255 int eq;
4256 unsigned offset;
4257 unsigned n_div;
4258 isl_local_space *ls;
4259 isl_aff *aff;
4261 if (!bmap)
4262 return NULL;
4263 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4264 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4265 "basic map should have a single output dimension",
4266 goto error);
4267 eq = isl_basic_map_output_defining_equality(bmap, 0);
4268 if (eq >= bmap->n_eq)
4269 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4270 "unable to find suitable equality", goto error);
4271 ls = isl_basic_map_get_local_space(bmap);
4272 aff = isl_aff_alloc(isl_local_space_domain(ls));
4273 if (!aff)
4274 goto error;
4275 offset = isl_basic_map_offset(bmap, isl_dim_out);
4276 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4277 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4278 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4279 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4280 n_div);
4281 } else {
4282 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4283 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4284 n_div);
4286 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4287 isl_basic_map_free(bmap);
4289 aff = isl_aff_remove_unused_divs(aff);
4290 return aff;
4291 error:
4292 isl_basic_map_free(bmap);
4293 return NULL;
4296 /* Given a basic map where each output dimension is defined
4297 * in terms of the parameters and input dimensions using an equality,
4298 * extract an isl_multi_aff that expresses the output dimensions in terms
4299 * of the parameters and input dimensions.
4301 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4302 __isl_take isl_basic_map *bmap)
4304 int i;
4305 unsigned n_out;
4306 isl_multi_aff *ma;
4308 if (!bmap)
4309 return NULL;
4311 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4312 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4314 for (i = 0; i < n_out; ++i) {
4315 isl_basic_map *bmap_i;
4316 isl_aff *aff;
4318 bmap_i = isl_basic_map_copy(bmap);
4319 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4320 i + 1, n_out - (1 + i));
4321 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4322 aff = extract_isl_aff_from_basic_map(bmap_i);
4323 ma = isl_multi_aff_set_aff(ma, i, aff);
4326 isl_basic_map_free(bmap);
4328 return ma;
4331 /* Given a basic set where each set dimension is defined
4332 * in terms of the parameters using an equality,
4333 * extract an isl_multi_aff that expresses the set dimensions in terms
4334 * of the parameters.
4336 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4337 __isl_take isl_basic_set *bset)
4339 return extract_isl_multi_aff_from_basic_map(bset);
4342 /* Create an isl_pw_multi_aff that is equivalent to
4343 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4344 * The given basic map is such that each output dimension is defined
4345 * in terms of the parameters and input dimensions using an equality.
4347 * Since some applications expect the result of isl_pw_multi_aff_from_map
4348 * to only contain integer affine expressions, we compute the floor
4349 * of the expression before returning.
4351 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4352 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4354 isl_multi_aff *ma;
4356 ma = extract_isl_multi_aff_from_basic_map(bmap);
4357 ma = isl_multi_aff_floor(ma);
4358 return isl_pw_multi_aff_alloc(domain, ma);
4361 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4362 * This obviously only works if the input "map" is single-valued.
4363 * If so, we compute the lexicographic minimum of the image in the form
4364 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4365 * to its lexicographic minimum.
4366 * If the input is not single-valued, we produce an error.
4368 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4369 __isl_take isl_map *map)
4371 int i;
4372 int sv;
4373 isl_pw_multi_aff *pma;
4375 sv = isl_map_is_single_valued(map);
4376 if (sv < 0)
4377 goto error;
4378 if (!sv)
4379 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4380 "map is not single-valued", goto error);
4381 map = isl_map_make_disjoint(map);
4382 if (!map)
4383 return NULL;
4385 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4387 for (i = 0; i < map->n; ++i) {
4388 isl_pw_multi_aff *pma_i;
4389 isl_basic_map *bmap;
4390 bmap = isl_basic_map_copy(map->p[i]);
4391 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4392 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4395 isl_map_free(map);
4396 return pma;
4397 error:
4398 isl_map_free(map);
4399 return NULL;
4402 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4403 * taking into account that the output dimension at position "d"
4404 * can be represented as
4406 * x = floor((e(...) + c1) / m)
4408 * given that constraint "i" is of the form
4410 * e(...) + c1 - m x >= 0
4413 * Let "map" be of the form
4415 * A -> B
4417 * We construct a mapping
4419 * A -> [A -> x = floor(...)]
4421 * apply that to the map, obtaining
4423 * [A -> x = floor(...)] -> B
4425 * and equate dimension "d" to x.
4426 * We then compute a isl_pw_multi_aff representation of the resulting map
4427 * and plug in the mapping above.
4429 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4430 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4432 isl_ctx *ctx;
4433 isl_space *space;
4434 isl_local_space *ls;
4435 isl_multi_aff *ma;
4436 isl_aff *aff;
4437 isl_vec *v;
4438 isl_map *insert;
4439 int offset;
4440 int n;
4441 int n_in;
4442 isl_pw_multi_aff *pma;
4443 int is_set;
4445 is_set = isl_map_is_set(map);
4447 offset = isl_basic_map_offset(hull, isl_dim_out);
4448 ctx = isl_map_get_ctx(map);
4449 space = isl_space_domain(isl_map_get_space(map));
4450 n_in = isl_space_dim(space, isl_dim_set);
4451 n = isl_space_dim(space, isl_dim_all);
4453 v = isl_vec_alloc(ctx, 1 + 1 + n);
4454 if (v) {
4455 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4456 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4458 isl_basic_map_free(hull);
4460 ls = isl_local_space_from_space(isl_space_copy(space));
4461 aff = isl_aff_alloc_vec(ls, v);
4462 aff = isl_aff_floor(aff);
4463 if (is_set) {
4464 isl_space_free(space);
4465 ma = isl_multi_aff_from_aff(aff);
4466 } else {
4467 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4468 ma = isl_multi_aff_range_product(ma,
4469 isl_multi_aff_from_aff(aff));
4472 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4473 map = isl_map_apply_domain(map, insert);
4474 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4475 pma = isl_pw_multi_aff_from_map(map);
4476 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4478 return pma;
4481 /* Is constraint "c" of the form
4483 * e(...) + c1 - m x >= 0
4485 * or
4487 * -e(...) + c2 + m x >= 0
4489 * where m > 1 and e only depends on parameters and input dimemnsions?
4491 * "offset" is the offset of the output dimensions
4492 * "pos" is the position of output dimension x.
4494 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4496 if (isl_int_is_zero(c[offset + d]))
4497 return 0;
4498 if (isl_int_is_one(c[offset + d]))
4499 return 0;
4500 if (isl_int_is_negone(c[offset + d]))
4501 return 0;
4502 if (isl_seq_first_non_zero(c + offset, d) != -1)
4503 return 0;
4504 if (isl_seq_first_non_zero(c + offset + d + 1,
4505 total - (offset + d + 1)) != -1)
4506 return 0;
4507 return 1;
4510 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4512 * As a special case, we first check if there is any pair of constraints,
4513 * shared by all the basic maps in "map" that force a given dimension
4514 * to be equal to the floor of some affine combination of the input dimensions.
4516 * In particular, if we can find two constraints
4518 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4520 * and
4522 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4524 * where m > 1 and e only depends on parameters and input dimemnsions,
4525 * and such that
4527 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4529 * then we know that we can take
4531 * x = floor((e(...) + c1) / m)
4533 * without having to perform any computation.
4535 * Note that we know that
4537 * c1 + c2 >= 1
4539 * If c1 + c2 were 0, then we would have detected an equality during
4540 * simplification. If c1 + c2 were negative, then we would have detected
4541 * a contradiction.
4543 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4544 __isl_take isl_map *map)
4546 int d, dim;
4547 int i, j, n;
4548 int offset, total;
4549 isl_int sum;
4550 isl_basic_map *hull;
4552 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4553 if (!hull)
4554 goto error;
4556 isl_int_init(sum);
4557 dim = isl_map_dim(map, isl_dim_out);
4558 offset = isl_basic_map_offset(hull, isl_dim_out);
4559 total = 1 + isl_basic_map_total_dim(hull);
4560 n = hull->n_ineq;
4561 for (d = 0; d < dim; ++d) {
4562 for (i = 0; i < n; ++i) {
4563 if (!is_potential_div_constraint(hull->ineq[i],
4564 offset, d, total))
4565 continue;
4566 for (j = i + 1; j < n; ++j) {
4567 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4568 hull->ineq[j] + 1, total - 1))
4569 continue;
4570 isl_int_add(sum, hull->ineq[i][0],
4571 hull->ineq[j][0]);
4572 if (isl_int_abs_lt(sum,
4573 hull->ineq[i][offset + d]))
4574 break;
4577 if (j >= n)
4578 continue;
4579 isl_int_clear(sum);
4580 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4581 j = i;
4582 return pw_multi_aff_from_map_div(map, hull, d, j);
4585 isl_int_clear(sum);
4586 isl_basic_map_free(hull);
4587 return pw_multi_aff_from_map_base(map);
4588 error:
4589 isl_map_free(map);
4590 isl_basic_map_free(hull);
4591 return NULL;
4594 /* Given an affine expression
4596 * [A -> B] -> f(A,B)
4598 * construct an isl_multi_aff
4600 * [A -> B] -> B'
4602 * such that dimension "d" in B' is set to "aff" and the remaining
4603 * dimensions are set equal to the corresponding dimensions in B.
4604 * "n_in" is the dimension of the space A.
4605 * "n_out" is the dimension of the space B.
4607 * If "is_set" is set, then the affine expression is of the form
4609 * [B] -> f(B)
4611 * and we construct an isl_multi_aff
4613 * B -> B'
4615 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4616 unsigned n_in, unsigned n_out, int is_set)
4618 int i;
4619 isl_multi_aff *ma;
4620 isl_space *space, *space2;
4621 isl_local_space *ls;
4623 space = isl_aff_get_domain_space(aff);
4624 ls = isl_local_space_from_space(isl_space_copy(space));
4625 space2 = isl_space_copy(space);
4626 if (!is_set)
4627 space2 = isl_space_range(isl_space_unwrap(space2));
4628 space = isl_space_map_from_domain_and_range(space, space2);
4629 ma = isl_multi_aff_alloc(space);
4630 ma = isl_multi_aff_set_aff(ma, d, aff);
4632 for (i = 0; i < n_out; ++i) {
4633 if (i == d)
4634 continue;
4635 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4636 isl_dim_set, n_in + i);
4637 ma = isl_multi_aff_set_aff(ma, i, aff);
4640 isl_local_space_free(ls);
4642 return ma;
4645 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4646 * taking into account that the dimension at position "d" can be written as
4648 * x = m a + f(..) (1)
4650 * where m is equal to "gcd".
4651 * "i" is the index of the equality in "hull" that defines f(..).
4652 * In particular, the equality is of the form
4654 * f(..) - x + m g(existentials) = 0
4656 * or
4658 * -f(..) + x + m g(existentials) = 0
4660 * We basically plug (1) into "map", resulting in a map with "a"
4661 * in the range instead of "x". The corresponding isl_pw_multi_aff
4662 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4664 * Specifically, given the input map
4666 * A -> B
4668 * We first wrap it into a set
4670 * [A -> B]
4672 * and define (1) on top of the corresponding space, resulting in "aff".
4673 * We use this to create an isl_multi_aff that maps the output position "d"
4674 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4675 * We plug this into the wrapped map, unwrap the result and compute the
4676 * corresponding isl_pw_multi_aff.
4677 * The result is an expression
4679 * A -> T(A)
4681 * We adjust that to
4683 * A -> [A -> T(A)]
4685 * so that we can plug that into "aff", after extending the latter to
4686 * a mapping
4688 * [A -> B] -> B'
4691 * If "map" is actually a set, then there is no "A" space, meaning
4692 * that we do not need to perform any wrapping, and that the result
4693 * of the recursive call is of the form
4695 * [T]
4697 * which is plugged into a mapping of the form
4699 * B -> B'
4701 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4702 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4703 isl_int gcd)
4705 isl_set *set;
4706 isl_space *space;
4707 isl_local_space *ls;
4708 isl_aff *aff;
4709 isl_multi_aff *ma;
4710 isl_pw_multi_aff *pma, *id;
4711 unsigned n_in;
4712 unsigned o_out;
4713 unsigned n_out;
4714 int is_set;
4716 is_set = isl_map_is_set(map);
4718 n_in = isl_basic_map_dim(hull, isl_dim_in);
4719 n_out = isl_basic_map_dim(hull, isl_dim_out);
4720 o_out = isl_basic_map_offset(hull, isl_dim_out);
4722 if (is_set)
4723 set = map;
4724 else
4725 set = isl_map_wrap(map);
4726 space = isl_space_map_from_set(isl_set_get_space(set));
4727 ma = isl_multi_aff_identity(space);
4728 ls = isl_local_space_from_space(isl_set_get_space(set));
4729 aff = isl_aff_alloc(ls);
4730 if (aff) {
4731 isl_int_set_si(aff->v->el[0], 1);
4732 if (isl_int_is_one(hull->eq[i][o_out + d]))
4733 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4734 aff->v->size - 1);
4735 else
4736 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4737 aff->v->size - 1);
4738 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4740 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4741 set = isl_set_preimage_multi_aff(set, ma);
4743 ma = range_map(aff, d, n_in, n_out, is_set);
4745 if (is_set)
4746 map = set;
4747 else
4748 map = isl_set_unwrap(set);
4749 pma = isl_pw_multi_aff_from_map(set);
4751 if (!is_set) {
4752 space = isl_pw_multi_aff_get_domain_space(pma);
4753 space = isl_space_map_from_set(space);
4754 id = isl_pw_multi_aff_identity(space);
4755 pma = isl_pw_multi_aff_range_product(id, pma);
4757 id = isl_pw_multi_aff_from_multi_aff(ma);
4758 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4760 isl_basic_map_free(hull);
4761 return pma;
4764 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4766 * As a special case, we first check if all output dimensions are uniquely
4767 * defined in terms of the parameters and input dimensions over the entire
4768 * domain. If so, we extract the desired isl_pw_multi_aff directly
4769 * from the affine hull of "map" and its domain.
4771 * Otherwise, we check if any of the output dimensions is "strided".
4772 * That is, we check if can be written as
4774 * x = m a + f(..)
4776 * with m greater than 1, a some combination of existentiall quantified
4777 * variables and f and expression in the parameters and input dimensions.
4778 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4780 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4781 * special case.
4783 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4785 int i, j;
4786 int sv;
4787 isl_basic_map *hull;
4788 unsigned n_out;
4789 unsigned o_out;
4790 unsigned n_div;
4791 unsigned o_div;
4792 isl_int gcd;
4794 if (!map)
4795 return NULL;
4797 hull = isl_map_affine_hull(isl_map_copy(map));
4798 sv = isl_basic_map_plain_is_single_valued(hull);
4799 if (sv >= 0 && sv)
4800 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4801 if (sv < 0)
4802 hull = isl_basic_map_free(hull);
4803 if (!hull)
4804 goto error;
4806 n_div = isl_basic_map_dim(hull, isl_dim_div);
4807 o_div = isl_basic_map_offset(hull, isl_dim_div);
4809 if (n_div == 0) {
4810 isl_basic_map_free(hull);
4811 return pw_multi_aff_from_map_check_div(map);
4814 isl_int_init(gcd);
4816 n_out = isl_basic_map_dim(hull, isl_dim_out);
4817 o_out = isl_basic_map_offset(hull, isl_dim_out);
4819 for (i = 0; i < n_out; ++i) {
4820 for (j = 0; j < hull->n_eq; ++j) {
4821 isl_int *eq = hull->eq[j];
4822 isl_pw_multi_aff *res;
4824 if (!isl_int_is_one(eq[o_out + i]) &&
4825 !isl_int_is_negone(eq[o_out + i]))
4826 continue;
4827 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4828 continue;
4829 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4830 n_out - (i + 1)) != -1)
4831 continue;
4832 isl_seq_gcd(eq + o_div, n_div, &gcd);
4833 if (isl_int_is_zero(gcd))
4834 continue;
4835 if (isl_int_is_one(gcd))
4836 continue;
4838 res = pw_multi_aff_from_map_stride(map, hull,
4839 i, j, gcd);
4840 isl_int_clear(gcd);
4841 return res;
4845 isl_int_clear(gcd);
4846 isl_basic_map_free(hull);
4847 return pw_multi_aff_from_map_check_div(map);
4848 error:
4849 isl_map_free(map);
4850 return NULL;
4853 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4855 return isl_pw_multi_aff_from_map(set);
4858 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4859 * add it to *user.
4861 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4863 isl_union_pw_multi_aff **upma = user;
4864 isl_pw_multi_aff *pma;
4866 pma = isl_pw_multi_aff_from_map(map);
4867 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4869 return *upma ? 0 : -1;
4872 /* Try and create an isl_union_pw_multi_aff that is equivalent
4873 * to the given isl_union_map.
4874 * The isl_union_map is required to be single-valued in each space.
4875 * Otherwise, an error is produced.
4877 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4878 __isl_take isl_union_map *umap)
4880 isl_space *space;
4881 isl_union_pw_multi_aff *upma;
4883 space = isl_union_map_get_space(umap);
4884 upma = isl_union_pw_multi_aff_empty(space);
4885 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4886 upma = isl_union_pw_multi_aff_free(upma);
4887 isl_union_map_free(umap);
4889 return upma;
4892 /* Try and create an isl_union_pw_multi_aff that is equivalent
4893 * to the given isl_union_set.
4894 * The isl_union_set is required to be a singleton in each space.
4895 * Otherwise, an error is produced.
4897 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4898 __isl_take isl_union_set *uset)
4900 return isl_union_pw_multi_aff_from_union_map(uset);
4903 /* Return the piecewise affine expression "set ? 1 : 0".
4905 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4907 isl_pw_aff *pa;
4908 isl_space *space = isl_set_get_space(set);
4909 isl_local_space *ls = isl_local_space_from_space(space);
4910 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4911 isl_aff *one = isl_aff_zero_on_domain(ls);
4913 one = isl_aff_add_constant_si(one, 1);
4914 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4915 set = isl_set_complement(set);
4916 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4918 return pa;
4921 /* Plug in "subs" for dimension "type", "pos" of "aff".
4923 * Let i be the dimension to replace and let "subs" be of the form
4925 * f/d
4927 * and "aff" of the form
4929 * (a i + g)/m
4931 * The result is
4933 * (a f + d g')/(m d)
4935 * where g' is the result of plugging in "subs" in each of the integer
4936 * divisions in g.
4938 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4939 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4941 isl_ctx *ctx;
4942 isl_int v;
4944 aff = isl_aff_cow(aff);
4945 if (!aff || !subs)
4946 return isl_aff_free(aff);
4948 ctx = isl_aff_get_ctx(aff);
4949 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4950 isl_die(ctx, isl_error_invalid,
4951 "spaces don't match", return isl_aff_free(aff));
4952 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4953 isl_die(ctx, isl_error_unsupported,
4954 "cannot handle divs yet", return isl_aff_free(aff));
4956 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4957 if (!aff->ls)
4958 return isl_aff_free(aff);
4960 aff->v = isl_vec_cow(aff->v);
4961 if (!aff->v)
4962 return isl_aff_free(aff);
4964 pos += isl_local_space_offset(aff->ls, type);
4966 isl_int_init(v);
4967 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4968 aff->v->size, subs->v->size, v);
4969 isl_int_clear(v);
4971 return aff;
4974 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4975 * expressions in "maff".
4977 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4978 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4979 __isl_keep isl_aff *subs)
4981 int i;
4983 maff = isl_multi_aff_cow(maff);
4984 if (!maff || !subs)
4985 return isl_multi_aff_free(maff);
4987 if (type == isl_dim_in)
4988 type = isl_dim_set;
4990 for (i = 0; i < maff->n; ++i) {
4991 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4992 if (!maff->p[i])
4993 return isl_multi_aff_free(maff);
4996 return maff;
4999 /* Plug in "subs" for dimension "type", "pos" of "pma".
5001 * pma is of the form
5003 * A_i(v) -> M_i(v)
5005 * while subs is of the form
5007 * v' = B_j(v) -> S_j
5009 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5010 * has a contribution in the result, in particular
5012 * C_ij(S_j) -> M_i(S_j)
5014 * Note that plugging in S_j in C_ij may also result in an empty set
5015 * and this contribution should simply be discarded.
5017 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5018 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5019 __isl_keep isl_pw_aff *subs)
5021 int i, j, n;
5022 isl_pw_multi_aff *res;
5024 if (!pma || !subs)
5025 return isl_pw_multi_aff_free(pma);
5027 n = pma->n * subs->n;
5028 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5030 for (i = 0; i < pma->n; ++i) {
5031 for (j = 0; j < subs->n; ++j) {
5032 isl_set *common;
5033 isl_multi_aff *res_ij;
5034 int empty;
5036 common = isl_set_intersect(
5037 isl_set_copy(pma->p[i].set),
5038 isl_set_copy(subs->p[j].set));
5039 common = isl_set_substitute(common,
5040 type, pos, subs->p[j].aff);
5041 empty = isl_set_plain_is_empty(common);
5042 if (empty < 0 || empty) {
5043 isl_set_free(common);
5044 if (empty < 0)
5045 goto error;
5046 continue;
5049 res_ij = isl_multi_aff_substitute(
5050 isl_multi_aff_copy(pma->p[i].maff),
5051 type, pos, subs->p[j].aff);
5053 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5057 isl_pw_multi_aff_free(pma);
5058 return res;
5059 error:
5060 isl_pw_multi_aff_free(pma);
5061 isl_pw_multi_aff_free(res);
5062 return NULL;
5065 /* Compute the preimage of a range of dimensions in the affine expression "src"
5066 * under "ma" and put the result in "dst". The number of dimensions in "src"
5067 * that precede the range is given by "n_before". The number of dimensions
5068 * in the range is given by the number of output dimensions of "ma".
5069 * The number of dimensions that follow the range is given by "n_after".
5070 * If "has_denom" is set (to one),
5071 * then "src" and "dst" have an extra initial denominator.
5072 * "n_div_ma" is the number of existentials in "ma"
5073 * "n_div_bset" is the number of existentials in "src"
5074 * The resulting "dst" (which is assumed to have been allocated by
5075 * the caller) contains coefficients for both sets of existentials,
5076 * first those in "ma" and then those in "src".
5077 * f, c1, c2 and g are temporary objects that have been initialized
5078 * by the caller.
5080 * Let src represent the expression
5082 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5084 * and let ma represent the expressions
5086 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5088 * We start out with the following expression for dst:
5090 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5092 * with the multiplication factor f initially equal to 1
5093 * and f \sum_i b_i v_i kept separately.
5094 * For each x_i that we substitute, we multiply the numerator
5095 * (and denominator) of dst by c_1 = m_i and add the numerator
5096 * of the x_i expression multiplied by c_2 = f b_i,
5097 * after removing the common factors of c_1 and c_2.
5098 * The multiplication factor f also needs to be multiplied by c_1
5099 * for the next x_j, j > i.
5101 void isl_seq_preimage(isl_int *dst, isl_int *src,
5102 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5103 int n_div_ma, int n_div_bmap,
5104 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5106 int i;
5107 int n_param, n_in, n_out;
5108 int o_dst, o_src;
5110 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5111 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5112 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5114 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5115 o_dst = o_src = has_denom + 1 + n_param + n_before;
5116 isl_seq_clr(dst + o_dst, n_in);
5117 o_dst += n_in;
5118 o_src += n_out;
5119 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5120 o_dst += n_after;
5121 o_src += n_after;
5122 isl_seq_clr(dst + o_dst, n_div_ma);
5123 o_dst += n_div_ma;
5124 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5126 isl_int_set_si(f, 1);
5128 for (i = 0; i < n_out; ++i) {
5129 int offset = has_denom + 1 + n_param + n_before + i;
5131 if (isl_int_is_zero(src[offset]))
5132 continue;
5133 isl_int_set(c1, ma->p[i]->v->el[0]);
5134 isl_int_mul(c2, f, src[offset]);
5135 isl_int_gcd(g, c1, c2);
5136 isl_int_divexact(c1, c1, g);
5137 isl_int_divexact(c2, c2, g);
5139 isl_int_mul(f, f, c1);
5140 o_dst = has_denom;
5141 o_src = 1;
5142 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5143 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5144 o_dst += 1 + n_param;
5145 o_src += 1 + n_param;
5146 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5147 o_dst += n_before;
5148 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5149 c2, ma->p[i]->v->el + o_src, n_in);
5150 o_dst += n_in;
5151 o_src += n_in;
5152 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5153 o_dst += n_after;
5154 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5155 c2, ma->p[i]->v->el + o_src, n_div_ma);
5156 o_dst += n_div_ma;
5157 o_src += n_div_ma;
5158 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5159 if (has_denom)
5160 isl_int_mul(dst[0], dst[0], c1);
5164 /* Compute the pullback of "aff" by the function represented by "ma".
5165 * In other words, plug in "ma" in "aff". The result is an affine expression
5166 * defined over the domain space of "ma".
5168 * If "aff" is represented by
5170 * (a(p) + b x + c(divs))/d
5172 * and ma is represented by
5174 * x = D(p) + F(y) + G(divs')
5176 * then the result is
5178 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5180 * The divs in the local space of the input are similarly adjusted
5181 * through a call to isl_local_space_preimage_multi_aff.
5183 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5184 __isl_take isl_multi_aff *ma)
5186 isl_aff *res = NULL;
5187 isl_local_space *ls;
5188 int n_div_aff, n_div_ma;
5189 isl_int f, c1, c2, g;
5191 ma = isl_multi_aff_align_divs(ma);
5192 if (!aff || !ma)
5193 goto error;
5195 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5196 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5198 ls = isl_aff_get_domain_local_space(aff);
5199 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5200 res = isl_aff_alloc(ls);
5201 if (!res)
5202 goto error;
5204 isl_int_init(f);
5205 isl_int_init(c1);
5206 isl_int_init(c2);
5207 isl_int_init(g);
5209 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5210 f, c1, c2, g, 1);
5212 isl_int_clear(f);
5213 isl_int_clear(c1);
5214 isl_int_clear(c2);
5215 isl_int_clear(g);
5217 isl_aff_free(aff);
5218 isl_multi_aff_free(ma);
5219 res = isl_aff_normalize(res);
5220 return res;
5221 error:
5222 isl_aff_free(aff);
5223 isl_multi_aff_free(ma);
5224 isl_aff_free(res);
5225 return NULL;
5228 /* Compute the pullback of "aff1" by the function represented by "aff2".
5229 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5230 * defined over the domain space of "aff1".
5232 * The domain of "aff1" should match the range of "aff2", which means
5233 * that it should be single-dimensional.
5235 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5236 __isl_take isl_aff *aff2)
5238 isl_multi_aff *ma;
5240 ma = isl_multi_aff_from_aff(aff2);
5241 return isl_aff_pullback_multi_aff(aff1, ma);
5244 /* Compute the pullback of "ma1" by the function represented by "ma2".
5245 * In other words, plug in "ma2" in "ma1".
5247 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5249 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5250 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5252 int i;
5253 isl_space *space = NULL;
5255 ma2 = isl_multi_aff_align_divs(ma2);
5256 ma1 = isl_multi_aff_cow(ma1);
5257 if (!ma1 || !ma2)
5258 goto error;
5260 space = isl_space_join(isl_multi_aff_get_space(ma2),
5261 isl_multi_aff_get_space(ma1));
5263 for (i = 0; i < ma1->n; ++i) {
5264 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5265 isl_multi_aff_copy(ma2));
5266 if (!ma1->p[i])
5267 goto error;
5270 ma1 = isl_multi_aff_reset_space(ma1, space);
5271 isl_multi_aff_free(ma2);
5272 return ma1;
5273 error:
5274 isl_space_free(space);
5275 isl_multi_aff_free(ma2);
5276 isl_multi_aff_free(ma1);
5277 return NULL;
5280 /* Compute the pullback of "ma1" by the function represented by "ma2".
5281 * In other words, plug in "ma2" in "ma1".
5283 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5284 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5286 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5287 &isl_multi_aff_pullback_multi_aff_aligned);
5290 /* Extend the local space of "dst" to include the divs
5291 * in the local space of "src".
5293 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5294 __isl_keep isl_aff *src)
5296 isl_ctx *ctx;
5297 int *exp1 = NULL;
5298 int *exp2 = NULL;
5299 isl_mat *div;
5301 if (!src || !dst)
5302 return isl_aff_free(dst);
5304 ctx = isl_aff_get_ctx(src);
5305 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5306 isl_die(ctx, isl_error_invalid,
5307 "spaces don't match", goto error);
5309 if (src->ls->div->n_row == 0)
5310 return dst;
5312 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5313 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5314 if (!exp1 || (dst->ls->div->n_row && !exp2))
5315 goto error;
5317 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5318 dst = isl_aff_expand_divs(dst, div, exp2);
5319 free(exp1);
5320 free(exp2);
5322 return dst;
5323 error:
5324 free(exp1);
5325 free(exp2);
5326 return isl_aff_free(dst);
5329 /* Adjust the local spaces of the affine expressions in "maff"
5330 * such that they all have the save divs.
5332 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5333 __isl_take isl_multi_aff *maff)
5335 int i;
5337 if (!maff)
5338 return NULL;
5339 if (maff->n == 0)
5340 return maff;
5341 maff = isl_multi_aff_cow(maff);
5342 if (!maff)
5343 return NULL;
5345 for (i = 1; i < maff->n; ++i)
5346 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5347 for (i = 1; i < maff->n; ++i) {
5348 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5349 if (!maff->p[i])
5350 return isl_multi_aff_free(maff);
5353 return maff;
5356 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5358 aff = isl_aff_cow(aff);
5359 if (!aff)
5360 return NULL;
5362 aff->ls = isl_local_space_lift(aff->ls);
5363 if (!aff->ls)
5364 return isl_aff_free(aff);
5366 return aff;
5369 /* Lift "maff" to a space with extra dimensions such that the result
5370 * has no more existentially quantified variables.
5371 * If "ls" is not NULL, then *ls is assigned the local space that lies
5372 * at the basis of the lifting applied to "maff".
5374 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5375 __isl_give isl_local_space **ls)
5377 int i;
5378 isl_space *space;
5379 unsigned n_div;
5381 if (ls)
5382 *ls = NULL;
5384 if (!maff)
5385 return NULL;
5387 if (maff->n == 0) {
5388 if (ls) {
5389 isl_space *space = isl_multi_aff_get_domain_space(maff);
5390 *ls = isl_local_space_from_space(space);
5391 if (!*ls)
5392 return isl_multi_aff_free(maff);
5394 return maff;
5397 maff = isl_multi_aff_cow(maff);
5398 maff = isl_multi_aff_align_divs(maff);
5399 if (!maff)
5400 return NULL;
5402 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5403 space = isl_multi_aff_get_space(maff);
5404 space = isl_space_lift(isl_space_domain(space), n_div);
5405 space = isl_space_extend_domain_with_range(space,
5406 isl_multi_aff_get_space(maff));
5407 if (!space)
5408 return isl_multi_aff_free(maff);
5409 isl_space_free(maff->space);
5410 maff->space = space;
5412 if (ls) {
5413 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5414 if (!*ls)
5415 return isl_multi_aff_free(maff);
5418 for (i = 0; i < maff->n; ++i) {
5419 maff->p[i] = isl_aff_lift(maff->p[i]);
5420 if (!maff->p[i])
5421 goto error;
5424 return maff;
5425 error:
5426 if (ls)
5427 isl_local_space_free(*ls);
5428 return isl_multi_aff_free(maff);
5432 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5434 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5435 __isl_keep isl_pw_multi_aff *pma, int pos)
5437 int i;
5438 int n_out;
5439 isl_space *space;
5440 isl_pw_aff *pa;
5442 if (!pma)
5443 return NULL;
5445 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5446 if (pos < 0 || pos >= n_out)
5447 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5448 "index out of bounds", return NULL);
5450 space = isl_pw_multi_aff_get_space(pma);
5451 space = isl_space_drop_dims(space, isl_dim_out,
5452 pos + 1, n_out - pos - 1);
5453 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5455 pa = isl_pw_aff_alloc_size(space, pma->n);
5456 for (i = 0; i < pma->n; ++i) {
5457 isl_aff *aff;
5458 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5459 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5462 return pa;
5465 /* Return an isl_pw_multi_aff with the given "set" as domain and
5466 * an unnamed zero-dimensional range.
5468 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5469 __isl_take isl_set *set)
5471 isl_multi_aff *ma;
5472 isl_space *space;
5474 space = isl_set_get_space(set);
5475 space = isl_space_from_domain(space);
5476 ma = isl_multi_aff_zero(space);
5477 return isl_pw_multi_aff_alloc(set, ma);
5480 /* Add an isl_pw_multi_aff with the given "set" as domain and
5481 * an unnamed zero-dimensional range to *user.
5483 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5485 isl_union_pw_multi_aff **upma = user;
5486 isl_pw_multi_aff *pma;
5488 pma = isl_pw_multi_aff_from_domain(set);
5489 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5491 return 0;
5494 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5495 * an unnamed zero-dimensional range.
5497 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5498 __isl_take isl_union_set *uset)
5500 isl_space *space;
5501 isl_union_pw_multi_aff *upma;
5503 if (!uset)
5504 return NULL;
5506 space = isl_union_set_get_space(uset);
5507 upma = isl_union_pw_multi_aff_empty(space);
5509 if (isl_union_set_foreach_set(uset,
5510 &add_pw_multi_aff_from_domain, &upma) < 0)
5511 goto error;
5513 isl_union_set_free(uset);
5514 return upma;
5515 error:
5516 isl_union_set_free(uset);
5517 isl_union_pw_multi_aff_free(upma);
5518 return NULL;
5521 /* Convert "pma" to an isl_map and add it to *umap.
5523 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5525 isl_union_map **umap = user;
5526 isl_map *map;
5528 map = isl_map_from_pw_multi_aff(pma);
5529 *umap = isl_union_map_add_map(*umap, map);
5531 return 0;
5534 /* Construct a union map mapping the domain of the union
5535 * piecewise multi-affine expression to its range, with each dimension
5536 * in the range equated to the corresponding affine expression on its cell.
5538 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5539 __isl_take isl_union_pw_multi_aff *upma)
5541 isl_space *space;
5542 isl_union_map *umap;
5544 if (!upma)
5545 return NULL;
5547 space = isl_union_pw_multi_aff_get_space(upma);
5548 umap = isl_union_map_empty(space);
5550 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5551 &map_from_pw_multi_aff, &umap) < 0)
5552 goto error;
5554 isl_union_pw_multi_aff_free(upma);
5555 return umap;
5556 error:
5557 isl_union_pw_multi_aff_free(upma);
5558 isl_union_map_free(umap);
5559 return NULL;
5562 /* Local data for bin_entry and the callback "fn".
5564 struct isl_union_pw_multi_aff_bin_data {
5565 isl_union_pw_multi_aff *upma2;
5566 isl_union_pw_multi_aff *res;
5567 isl_pw_multi_aff *pma;
5568 int (*fn)(void **entry, void *user);
5571 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5572 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5574 static int bin_entry(void **entry, void *user)
5576 struct isl_union_pw_multi_aff_bin_data *data = user;
5577 isl_pw_multi_aff *pma = *entry;
5579 data->pma = pma;
5580 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5581 data->fn, data) < 0)
5582 return -1;
5584 return 0;
5587 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5588 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5589 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5590 * as *entry. The callback should adjust data->res if desired.
5592 static __isl_give isl_union_pw_multi_aff *bin_op(
5593 __isl_take isl_union_pw_multi_aff *upma1,
5594 __isl_take isl_union_pw_multi_aff *upma2,
5595 int (*fn)(void **entry, void *user))
5597 isl_space *space;
5598 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5600 space = isl_union_pw_multi_aff_get_space(upma2);
5601 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5602 space = isl_union_pw_multi_aff_get_space(upma1);
5603 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5605 if (!upma1 || !upma2)
5606 goto error;
5608 data.upma2 = upma2;
5609 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5610 upma1->table.n);
5611 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5612 &bin_entry, &data) < 0)
5613 goto error;
5615 isl_union_pw_multi_aff_free(upma1);
5616 isl_union_pw_multi_aff_free(upma2);
5617 return data.res;
5618 error:
5619 isl_union_pw_multi_aff_free(upma1);
5620 isl_union_pw_multi_aff_free(upma2);
5621 isl_union_pw_multi_aff_free(data.res);
5622 return NULL;
5625 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5626 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5628 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5629 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5631 isl_space *space;
5633 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5634 isl_pw_multi_aff_get_space(pma2));
5635 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5636 &isl_multi_aff_range_product);
5639 /* Given two isl_pw_multi_affs A -> B and C -> D,
5640 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5642 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5643 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5645 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5646 &pw_multi_aff_range_product);
5649 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5650 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5652 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5653 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5655 isl_space *space;
5657 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5658 isl_pw_multi_aff_get_space(pma2));
5659 space = isl_space_flatten_range(space);
5660 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5661 &isl_multi_aff_flat_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_flat_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_flat_range_product);
5674 /* If data->pma and *entry have the same domain space, then compute
5675 * their flat range product and the result to data->res.
5677 static int flat_range_product_entry(void **entry, void *user)
5679 struct isl_union_pw_multi_aff_bin_data *data = user;
5680 isl_pw_multi_aff *pma2 = *entry;
5682 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5683 pma2->dim, isl_dim_in))
5684 return 0;
5686 pma2 = isl_pw_multi_aff_flat_range_product(
5687 isl_pw_multi_aff_copy(data->pma),
5688 isl_pw_multi_aff_copy(pma2));
5690 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5692 return 0;
5695 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5696 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5698 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5699 __isl_take isl_union_pw_multi_aff *upma1,
5700 __isl_take isl_union_pw_multi_aff *upma2)
5702 return bin_op(upma1, upma2, &flat_range_product_entry);
5705 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5706 * The parameters are assumed to have been aligned.
5708 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5709 * except that it works on two different isl_pw_* types.
5711 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5712 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5713 __isl_take isl_pw_aff *pa)
5715 int i, j, n;
5716 isl_pw_multi_aff *res = NULL;
5718 if (!pma || !pa)
5719 goto error;
5721 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5722 pa->dim, isl_dim_in))
5723 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5724 "domains don't match", goto error);
5725 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5726 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5727 "index out of bounds", goto error);
5729 n = pma->n * pa->n;
5730 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5732 for (i = 0; i < pma->n; ++i) {
5733 for (j = 0; j < pa->n; ++j) {
5734 isl_set *common;
5735 isl_multi_aff *res_ij;
5736 int empty;
5738 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5739 isl_set_copy(pa->p[j].set));
5740 empty = isl_set_plain_is_empty(common);
5741 if (empty < 0 || empty) {
5742 isl_set_free(common);
5743 if (empty < 0)
5744 goto error;
5745 continue;
5748 res_ij = isl_multi_aff_set_aff(
5749 isl_multi_aff_copy(pma->p[i].maff), pos,
5750 isl_aff_copy(pa->p[j].aff));
5751 res_ij = isl_multi_aff_gist(res_ij,
5752 isl_set_copy(common));
5754 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5758 isl_pw_multi_aff_free(pma);
5759 isl_pw_aff_free(pa);
5760 return res;
5761 error:
5762 isl_pw_multi_aff_free(pma);
5763 isl_pw_aff_free(pa);
5764 return isl_pw_multi_aff_free(res);
5767 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5769 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5770 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5771 __isl_take isl_pw_aff *pa)
5773 if (!pma || !pa)
5774 goto error;
5775 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5776 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5777 if (!isl_space_has_named_params(pma->dim) ||
5778 !isl_space_has_named_params(pa->dim))
5779 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5780 "unaligned unnamed parameters", goto error);
5781 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5782 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5783 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5784 error:
5785 isl_pw_multi_aff_free(pma);
5786 isl_pw_aff_free(pa);
5787 return NULL;
5790 /* Do the parameters of "pa" match those of "space"?
5792 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5793 __isl_keep isl_space *space)
5795 isl_space *pa_space;
5796 int match;
5798 if (!pa || !space)
5799 return -1;
5801 pa_space = isl_pw_aff_get_space(pa);
5803 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5805 isl_space_free(pa_space);
5806 return match;
5809 /* Check that the domain space of "pa" matches "space".
5811 * Return 0 on success and -1 on error.
5813 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5814 __isl_keep isl_space *space)
5816 isl_space *pa_space;
5817 int match;
5819 if (!pa || !space)
5820 return -1;
5822 pa_space = isl_pw_aff_get_space(pa);
5824 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5825 if (match < 0)
5826 goto error;
5827 if (!match)
5828 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5829 "parameters don't match", goto error);
5830 match = isl_space_tuple_is_equal(space, isl_dim_in,
5831 pa_space, isl_dim_in);
5832 if (match < 0)
5833 goto error;
5834 if (!match)
5835 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5836 "domains don't match", goto error);
5837 isl_space_free(pa_space);
5838 return 0;
5839 error:
5840 isl_space_free(pa_space);
5841 return -1;
5844 #undef BASE
5845 #define BASE pw_aff
5847 #include <isl_multi_templ.c>
5849 /* Scale the elements of "pma" by the corresponding elements of "mv".
5851 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5852 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5854 int i;
5856 pma = isl_pw_multi_aff_cow(pma);
5857 if (!pma || !mv)
5858 goto error;
5859 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5860 mv->space, isl_dim_set))
5861 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5862 "spaces don't match", goto error);
5863 if (!isl_space_match(pma->dim, isl_dim_param,
5864 mv->space, isl_dim_param)) {
5865 pma = isl_pw_multi_aff_align_params(pma,
5866 isl_multi_val_get_space(mv));
5867 mv = isl_multi_val_align_params(mv,
5868 isl_pw_multi_aff_get_space(pma));
5869 if (!pma || !mv)
5870 goto error;
5873 for (i = 0; i < pma->n; ++i) {
5874 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5875 isl_multi_val_copy(mv));
5876 if (!pma->p[i].maff)
5877 goto error;
5880 isl_multi_val_free(mv);
5881 return pma;
5882 error:
5883 isl_multi_val_free(mv);
5884 isl_pw_multi_aff_free(pma);
5885 return NULL;
5888 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5889 * mv contains the mv argument.
5890 * res collects the results.
5892 struct isl_union_pw_multi_aff_scale_multi_val_data {
5893 isl_multi_val *mv;
5894 isl_union_pw_multi_aff *res;
5897 /* This function is called for each entry of an isl_union_pw_multi_aff.
5898 * If the space of the entry matches that of data->mv,
5899 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5900 * to data->res.
5902 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5904 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5905 isl_pw_multi_aff *pma = *entry;
5907 if (!pma)
5908 return -1;
5909 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5910 data->mv->space, isl_dim_set))
5911 return 0;
5913 pma = isl_pw_multi_aff_copy(pma);
5914 pma = isl_pw_multi_aff_scale_multi_val(pma,
5915 isl_multi_val_copy(data->mv));
5916 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5917 if (!data->res)
5918 return -1;
5920 return 0;
5923 /* Scale the elements of "upma" by the corresponding elements of "mv",
5924 * for those entries that match the space of "mv".
5926 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5927 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5929 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5931 upma = isl_union_pw_multi_aff_align_params(upma,
5932 isl_multi_val_get_space(mv));
5933 mv = isl_multi_val_align_params(mv,
5934 isl_union_pw_multi_aff_get_space(upma));
5935 if (!upma || !mv)
5936 goto error;
5938 data.mv = mv;
5939 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
5940 upma->table.n);
5941 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
5942 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5943 goto error;
5945 isl_multi_val_free(mv);
5946 isl_union_pw_multi_aff_free(upma);
5947 return data.res;
5948 error:
5949 isl_multi_val_free(mv);
5950 isl_union_pw_multi_aff_free(upma);
5951 return NULL;
5954 /* Construct and return a piecewise multi affine expression
5955 * in the given space with value zero in each of the output dimensions and
5956 * a universe domain.
5958 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5960 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5963 /* Construct and return a piecewise multi affine expression
5964 * that is equal to the given piecewise affine expression.
5966 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5967 __isl_take isl_pw_aff *pa)
5969 int i;
5970 isl_space *space;
5971 isl_pw_multi_aff *pma;
5973 if (!pa)
5974 return NULL;
5976 space = isl_pw_aff_get_space(pa);
5977 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5979 for (i = 0; i < pa->n; ++i) {
5980 isl_set *set;
5981 isl_multi_aff *ma;
5983 set = isl_set_copy(pa->p[i].set);
5984 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
5985 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
5988 isl_pw_aff_free(pa);
5989 return pma;
5992 /* Construct a set or map mapping the shared (parameter) domain
5993 * of the piecewise affine expressions to the range of "mpa"
5994 * with each dimension in the range equated to the
5995 * corresponding piecewise affine expression.
5997 static __isl_give isl_map *map_from_multi_pw_aff(
5998 __isl_take isl_multi_pw_aff *mpa)
6000 int i;
6001 isl_space *space;
6002 isl_map *map;
6004 if (!mpa)
6005 return NULL;
6007 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6008 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6009 "invalid space", goto error);
6011 space = isl_multi_pw_aff_get_domain_space(mpa);
6012 map = isl_map_universe(isl_space_from_domain(space));
6014 for (i = 0; i < mpa->n; ++i) {
6015 isl_pw_aff *pa;
6016 isl_map *map_i;
6018 pa = isl_pw_aff_copy(mpa->p[i]);
6019 map_i = map_from_pw_aff(pa);
6021 map = isl_map_flat_range_product(map, map_i);
6024 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6026 isl_multi_pw_aff_free(mpa);
6027 return map;
6028 error:
6029 isl_multi_pw_aff_free(mpa);
6030 return NULL;
6033 /* Construct a map mapping the shared domain
6034 * of the piecewise affine expressions to the range of "mpa"
6035 * with each dimension in the range equated to the
6036 * corresponding piecewise affine expression.
6038 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6040 if (!mpa)
6041 return NULL;
6042 if (isl_space_is_set(mpa->space))
6043 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6044 "space of input is not a map", goto error);
6046 return map_from_multi_pw_aff(mpa);
6047 error:
6048 isl_multi_pw_aff_free(mpa);
6049 return NULL;
6052 /* Construct a set mapping the shared parameter domain
6053 * of the piecewise affine expressions to the space of "mpa"
6054 * with each dimension in the range equated to the
6055 * corresponding piecewise affine expression.
6057 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6059 if (!mpa)
6060 return NULL;
6061 if (!isl_space_is_set(mpa->space))
6062 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6063 "space of input is not a set", goto error);
6065 return map_from_multi_pw_aff(mpa);
6066 error:
6067 isl_multi_pw_aff_free(mpa);
6068 return NULL;
6071 /* Construct and return a piecewise multi affine expression
6072 * that is equal to the given multi piecewise affine expression
6073 * on the shared domain of the piecewise affine expressions.
6075 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6076 __isl_take isl_multi_pw_aff *mpa)
6078 int i;
6079 isl_space *space;
6080 isl_pw_aff *pa;
6081 isl_pw_multi_aff *pma;
6083 if (!mpa)
6084 return NULL;
6086 space = isl_multi_pw_aff_get_space(mpa);
6088 if (mpa->n == 0) {
6089 isl_multi_pw_aff_free(mpa);
6090 return isl_pw_multi_aff_zero(space);
6093 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6094 pma = isl_pw_multi_aff_from_pw_aff(pa);
6096 for (i = 1; i < mpa->n; ++i) {
6097 isl_pw_multi_aff *pma_i;
6099 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6100 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6101 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6104 pma = isl_pw_multi_aff_reset_space(pma, space);
6106 isl_multi_pw_aff_free(mpa);
6107 return pma;
6110 /* Construct and return a multi piecewise affine expression
6111 * that is equal to the given multi affine expression.
6113 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6114 __isl_take isl_multi_aff *ma)
6116 int i, n;
6117 isl_multi_pw_aff *mpa;
6119 if (!ma)
6120 return NULL;
6122 n = isl_multi_aff_dim(ma, isl_dim_out);
6123 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6125 for (i = 0; i < n; ++i) {
6126 isl_pw_aff *pa;
6128 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6129 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6132 isl_multi_aff_free(ma);
6133 return mpa;
6136 /* Construct and return a multi piecewise affine expression
6137 * that is equal to the given piecewise multi affine expression.
6139 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6140 __isl_take isl_pw_multi_aff *pma)
6142 int i, n;
6143 isl_space *space;
6144 isl_multi_pw_aff *mpa;
6146 if (!pma)
6147 return NULL;
6149 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6150 space = isl_pw_multi_aff_get_space(pma);
6151 mpa = isl_multi_pw_aff_alloc(space);
6153 for (i = 0; i < n; ++i) {
6154 isl_pw_aff *pa;
6156 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6157 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6160 isl_pw_multi_aff_free(pma);
6161 return mpa;
6164 /* Do "pa1" and "pa2" represent the same function?
6166 * We first check if they are obviously equal.
6167 * If not, we convert them to maps and check if those are equal.
6169 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6171 int equal;
6172 isl_map *map1, *map2;
6174 if (!pa1 || !pa2)
6175 return -1;
6177 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6178 if (equal < 0 || equal)
6179 return equal;
6181 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6182 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6183 equal = isl_map_is_equal(map1, map2);
6184 isl_map_free(map1);
6185 isl_map_free(map2);
6187 return equal;
6190 /* Do "mpa1" and "mpa2" represent the same function?
6192 * Note that we cannot convert the entire isl_multi_pw_aff
6193 * to a map because the domains of the piecewise affine expressions
6194 * may not be the same.
6196 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6197 __isl_keep isl_multi_pw_aff *mpa2)
6199 int i;
6200 int equal;
6202 if (!mpa1 || !mpa2)
6203 return -1;
6205 if (!isl_space_match(mpa1->space, isl_dim_param,
6206 mpa2->space, isl_dim_param)) {
6207 if (!isl_space_has_named_params(mpa1->space))
6208 return 0;
6209 if (!isl_space_has_named_params(mpa2->space))
6210 return 0;
6211 mpa1 = isl_multi_pw_aff_copy(mpa1);
6212 mpa2 = isl_multi_pw_aff_copy(mpa2);
6213 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6214 isl_multi_pw_aff_get_space(mpa2));
6215 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6216 isl_multi_pw_aff_get_space(mpa1));
6217 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6218 isl_multi_pw_aff_free(mpa1);
6219 isl_multi_pw_aff_free(mpa2);
6220 return equal;
6223 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6224 if (equal < 0 || !equal)
6225 return equal;
6227 for (i = 0; i < mpa1->n; ++i) {
6228 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6229 if (equal < 0 || !equal)
6230 return equal;
6233 return 1;
6236 /* Coalesce the elements of "mpa".
6238 * Note that such coalescing does not change the meaning of "mpa"
6239 * so there is no need to cow. We do need to be careful not to
6240 * destroy any other copies of "mpa" in case of failure.
6242 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6243 __isl_take isl_multi_pw_aff *mpa)
6245 int i;
6247 if (!mpa)
6248 return NULL;
6250 for (i = 0; i < mpa->n; ++i) {
6251 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6252 pa = isl_pw_aff_coalesce(pa);
6253 if (!pa)
6254 return isl_multi_pw_aff_free(mpa);
6255 isl_pw_aff_free(mpa->p[i]);
6256 mpa->p[i] = pa;
6259 return mpa;
6262 /* Compute the pullback of "mpa" by the function represented by "ma".
6263 * In other words, plug in "ma" in "mpa".
6265 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6267 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6268 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6270 int i;
6271 isl_space *space = NULL;
6273 mpa = isl_multi_pw_aff_cow(mpa);
6274 if (!mpa || !ma)
6275 goto error;
6277 space = isl_space_join(isl_multi_aff_get_space(ma),
6278 isl_multi_pw_aff_get_space(mpa));
6279 if (!space)
6280 goto error;
6282 for (i = 0; i < mpa->n; ++i) {
6283 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6284 isl_multi_aff_copy(ma));
6285 if (!mpa->p[i])
6286 goto error;
6289 isl_multi_aff_free(ma);
6290 isl_space_free(mpa->space);
6291 mpa->space = space;
6292 return mpa;
6293 error:
6294 isl_space_free(space);
6295 isl_multi_pw_aff_free(mpa);
6296 isl_multi_aff_free(ma);
6297 return NULL;
6300 /* Compute the pullback of "mpa" by the function represented by "ma".
6301 * In other words, plug in "ma" in "mpa".
6303 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6304 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6306 if (!mpa || !ma)
6307 goto error;
6308 if (isl_space_match(mpa->space, isl_dim_param,
6309 ma->space, isl_dim_param))
6310 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6311 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6312 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6313 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6314 error:
6315 isl_multi_pw_aff_free(mpa);
6316 isl_multi_aff_free(ma);
6317 return NULL;
6320 /* Compute the pullback of "mpa" by the function represented by "pma".
6321 * In other words, plug in "pma" in "mpa".
6323 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6325 static __isl_give isl_multi_pw_aff *
6326 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6327 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6329 int i;
6330 isl_space *space = NULL;
6332 mpa = isl_multi_pw_aff_cow(mpa);
6333 if (!mpa || !pma)
6334 goto error;
6336 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6337 isl_multi_pw_aff_get_space(mpa));
6339 for (i = 0; i < mpa->n; ++i) {
6340 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6341 isl_pw_multi_aff_copy(pma));
6342 if (!mpa->p[i])
6343 goto error;
6346 isl_pw_multi_aff_free(pma);
6347 isl_space_free(mpa->space);
6348 mpa->space = space;
6349 return mpa;
6350 error:
6351 isl_space_free(space);
6352 isl_multi_pw_aff_free(mpa);
6353 isl_pw_multi_aff_free(pma);
6354 return NULL;
6357 /* Compute the pullback of "mpa" by the function represented by "pma".
6358 * In other words, plug in "pma" in "mpa".
6360 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6361 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6363 if (!mpa || !pma)
6364 goto error;
6365 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6366 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6367 mpa = isl_multi_pw_aff_align_params(mpa,
6368 isl_pw_multi_aff_get_space(pma));
6369 pma = isl_pw_multi_aff_align_params(pma,
6370 isl_multi_pw_aff_get_space(mpa));
6371 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6372 error:
6373 isl_multi_pw_aff_free(mpa);
6374 isl_pw_multi_aff_free(pma);
6375 return NULL;
6378 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6379 * with the domain of "aff". The domain of the result is the same
6380 * as that of "mpa".
6381 * "mpa" and "aff" are assumed to have been aligned.
6383 * We first extract the parametric constant from "aff", defined
6384 * over the correct domain.
6385 * Then we add the appropriate combinations of the members of "mpa".
6386 * Finally, we add the integer divisions through recursive calls.
6388 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6389 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6391 int i, n_param, n_in, n_div;
6392 isl_space *space;
6393 isl_val *v;
6394 isl_pw_aff *pa;
6395 isl_aff *tmp;
6397 n_param = isl_aff_dim(aff, isl_dim_param);
6398 n_in = isl_aff_dim(aff, isl_dim_in);
6399 n_div = isl_aff_dim(aff, isl_dim_div);
6401 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6402 tmp = isl_aff_copy(aff);
6403 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6404 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6405 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6406 isl_space_dim(space, isl_dim_set));
6407 tmp = isl_aff_reset_domain_space(tmp, space);
6408 pa = isl_pw_aff_from_aff(tmp);
6410 for (i = 0; i < n_in; ++i) {
6411 isl_pw_aff *pa_i;
6413 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6414 continue;
6415 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6416 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6417 pa_i = isl_pw_aff_scale_val(pa_i, v);
6418 pa = isl_pw_aff_add(pa, pa_i);
6421 for (i = 0; i < n_div; ++i) {
6422 isl_aff *div;
6423 isl_pw_aff *pa_i;
6425 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6426 continue;
6427 div = isl_aff_get_div(aff, i);
6428 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6429 isl_multi_pw_aff_copy(mpa), div);
6430 pa_i = isl_pw_aff_floor(pa_i);
6431 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6432 pa_i = isl_pw_aff_scale_val(pa_i, v);
6433 pa = isl_pw_aff_add(pa, pa_i);
6436 isl_multi_pw_aff_free(mpa);
6437 isl_aff_free(aff);
6439 return pa;
6442 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6443 * with the domain of "aff". The domain of the result is the same
6444 * as that of "mpa".
6446 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6447 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6449 if (!aff || !mpa)
6450 goto error;
6451 if (isl_space_match(aff->ls->dim, isl_dim_param,
6452 mpa->space, isl_dim_param))
6453 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6455 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6456 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6458 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6459 error:
6460 isl_aff_free(aff);
6461 isl_multi_pw_aff_free(mpa);
6462 return NULL;
6465 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6466 * with the domain of "pa". The domain of the result is the same
6467 * as that of "mpa".
6468 * "mpa" and "pa" are assumed to have been aligned.
6470 * We consider each piece in turn. Note that the domains of the
6471 * pieces are assumed to be disjoint and they remain disjoint
6472 * after taking the preimage (over the same function).
6474 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6475 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6477 isl_space *space;
6478 isl_pw_aff *res;
6479 int i;
6481 if (!mpa || !pa)
6482 goto error;
6484 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6485 isl_pw_aff_get_space(pa));
6486 res = isl_pw_aff_empty(space);
6488 for (i = 0; i < pa->n; ++i) {
6489 isl_pw_aff *pa_i;
6490 isl_set *domain;
6492 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6493 isl_multi_pw_aff_copy(mpa),
6494 isl_aff_copy(pa->p[i].aff));
6495 domain = isl_set_copy(pa->p[i].set);
6496 domain = isl_set_preimage_multi_pw_aff(domain,
6497 isl_multi_pw_aff_copy(mpa));
6498 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6499 res = isl_pw_aff_add_disjoint(res, pa_i);
6502 isl_pw_aff_free(pa);
6503 isl_multi_pw_aff_free(mpa);
6504 return res;
6505 error:
6506 isl_pw_aff_free(pa);
6507 isl_multi_pw_aff_free(mpa);
6508 return NULL;
6511 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6512 * with the domain of "pa". The domain of the result is the same
6513 * as that of "mpa".
6515 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6516 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6518 if (!pa || !mpa)
6519 goto error;
6520 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6521 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6523 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6524 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6526 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6527 error:
6528 isl_pw_aff_free(pa);
6529 isl_multi_pw_aff_free(mpa);
6530 return NULL;
6533 /* Compute the pullback of "pa" by the function represented by "mpa".
6534 * In other words, plug in "mpa" in "pa".
6535 * "pa" and "mpa" are assumed to have been aligned.
6537 * The pullback is computed by applying "pa" to "mpa".
6539 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6540 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6542 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6545 /* Compute the pullback of "pa" by the function represented by "mpa".
6546 * In other words, plug in "mpa" in "pa".
6548 * The pullback is computed by applying "pa" to "mpa".
6550 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6551 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6553 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6556 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6557 * In other words, plug in "mpa2" in "mpa1".
6559 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6561 * We pullback each member of "mpa1" in turn.
6563 static __isl_give isl_multi_pw_aff *
6564 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6565 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6567 int i;
6568 isl_space *space = NULL;
6570 mpa1 = isl_multi_pw_aff_cow(mpa1);
6571 if (!mpa1 || !mpa2)
6572 goto error;
6574 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6575 isl_multi_pw_aff_get_space(mpa1));
6577 for (i = 0; i < mpa1->n; ++i) {
6578 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6579 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6580 if (!mpa1->p[i])
6581 goto error;
6584 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6586 isl_multi_pw_aff_free(mpa2);
6587 return mpa1;
6588 error:
6589 isl_space_free(space);
6590 isl_multi_pw_aff_free(mpa1);
6591 isl_multi_pw_aff_free(mpa2);
6592 return NULL;
6595 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6596 * In other words, plug in "mpa2" in "mpa1".
6598 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6599 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6601 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6602 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6605 /* Compare two isl_affs.
6607 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6608 * than "aff2" and 0 if they are equal.
6610 * The order is fairly arbitrary. We do consider expressions that only involve
6611 * earlier dimensions as "smaller".
6613 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6615 int cmp;
6616 int last1, last2;
6618 if (aff1 == aff2)
6619 return 0;
6621 if (!aff1)
6622 return -1;
6623 if (!aff2)
6624 return 1;
6626 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6627 if (cmp != 0)
6628 return cmp;
6630 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6631 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6632 if (last1 != last2)
6633 return last1 - last2;
6635 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6638 /* Compare two isl_pw_affs.
6640 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6641 * than "pa2" and 0 if they are equal.
6643 * The order is fairly arbitrary. We do consider expressions that only involve
6644 * earlier dimensions as "smaller".
6646 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6647 __isl_keep isl_pw_aff *pa2)
6649 int i;
6650 int cmp;
6652 if (pa1 == pa2)
6653 return 0;
6655 if (!pa1)
6656 return -1;
6657 if (!pa2)
6658 return 1;
6660 cmp = isl_space_cmp(pa1->dim, pa2->dim);
6661 if (cmp != 0)
6662 return cmp;
6664 if (pa1->n != pa2->n)
6665 return pa1->n - pa2->n;
6667 for (i = 0; i < pa1->n; ++i) {
6668 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
6669 if (cmp != 0)
6670 return cmp;
6671 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
6672 if (cmp != 0)
6673 return cmp;
6676 return 0;