add isl_multi_aff_zero
[isl.git] / isl_aff.c
blob8ddd78c31ec0824b9c305188c15ae43d47581c44
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 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 #include <isl_map_private.h>
16 #include <isl_aff_private.h>
17 #include <isl_space_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_list_private.h>
21 #include <isl/constraint.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl_config.h>
26 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
27 __isl_take isl_vec *v)
29 isl_aff *aff;
31 if (!ls || !v)
32 goto error;
34 aff = isl_calloc_type(v->ctx, struct isl_aff);
35 if (!aff)
36 goto error;
38 aff->ref = 1;
39 aff->ls = ls;
40 aff->v = v;
42 return aff;
43 error:
44 isl_local_space_free(ls);
45 isl_vec_free(v);
46 return NULL;
49 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
51 isl_ctx *ctx;
52 isl_vec *v;
53 unsigned total;
55 if (!ls)
56 return NULL;
58 ctx = isl_local_space_get_ctx(ls);
59 if (!isl_local_space_divs_known(ls))
60 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
61 goto error);
62 if (!isl_local_space_is_set(ls))
63 isl_die(ctx, isl_error_invalid,
64 "domain of affine expression should be a set",
65 goto error);
67 total = isl_local_space_dim(ls, isl_dim_all);
68 v = isl_vec_alloc(ctx, 1 + 1 + total);
69 return isl_aff_alloc_vec(ls, v);
70 error:
71 isl_local_space_free(ls);
72 return NULL;
75 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
77 isl_aff *aff;
79 aff = isl_aff_alloc(ls);
80 if (!aff)
81 return NULL;
83 isl_int_set_si(aff->v->el[0], 1);
84 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
86 return aff;
89 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
91 if (!aff)
92 return NULL;
94 aff->ref++;
95 return aff;
98 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
100 if (!aff)
101 return NULL;
103 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
104 isl_vec_copy(aff->v));
107 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
109 if (!aff)
110 return NULL;
112 if (aff->ref == 1)
113 return aff;
114 aff->ref--;
115 return isl_aff_dup(aff);
118 void *isl_aff_free(__isl_take isl_aff *aff)
120 if (!aff)
121 return NULL;
123 if (--aff->ref > 0)
124 return NULL;
126 isl_local_space_free(aff->ls);
127 isl_vec_free(aff->v);
129 free(aff);
131 return NULL;
134 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
136 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
139 /* Externally, an isl_aff has a map space, but internally, the
140 * ls field corresponds to the domain of that space.
142 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
144 if (!aff)
145 return 0;
146 if (type == isl_dim_out)
147 return 1;
148 if (type == isl_dim_in)
149 type = isl_dim_set;
150 return isl_local_space_dim(aff->ls, type);
153 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
155 return aff ? isl_local_space_get_space(aff->ls) : NULL;
158 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
160 isl_space *space;
161 if (!aff)
162 return NULL;
163 space = isl_local_space_get_space(aff->ls);
164 space = isl_space_from_domain(space);
165 space = isl_space_add_dims(space, isl_dim_out, 1);
166 return space;
169 __isl_give isl_local_space *isl_aff_get_domain_local_space(
170 __isl_keep isl_aff *aff)
172 return aff ? isl_local_space_copy(aff->ls) : NULL;
175 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
177 isl_local_space *ls;
178 if (!aff)
179 return NULL;
180 ls = isl_local_space_copy(aff->ls);
181 ls = isl_local_space_from_domain(ls);
182 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
183 return ls;
186 /* Externally, an isl_aff has a map space, but internally, the
187 * ls field corresponds to the domain of that space.
189 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
190 enum isl_dim_type type, unsigned pos)
192 if (!aff)
193 return NULL;
194 if (type == isl_dim_out)
195 return NULL;
196 if (type == isl_dim_in)
197 type = isl_dim_set;
198 return isl_local_space_get_dim_name(aff->ls, type, pos);
201 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
202 __isl_take isl_space *dim)
204 aff = isl_aff_cow(aff);
205 if (!aff || !dim)
206 goto error;
208 aff->ls = isl_local_space_reset_space(aff->ls, dim);
209 if (!aff->ls)
210 return isl_aff_free(aff);
212 return aff;
213 error:
214 isl_aff_free(aff);
215 isl_space_free(dim);
216 return NULL;
219 /* Reset the space of "aff". This function is called from isl_pw_templ.c
220 * and doesn't know if the space of an element object is represented
221 * directly or through its domain. It therefore passes along both.
223 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
224 __isl_take isl_space *space, __isl_take isl_space *domain)
226 isl_space_free(space);
227 return isl_aff_reset_domain_space(aff, domain);
230 /* Reorder the coefficients of the affine expression based
231 * on the given reodering.
232 * The reordering r is assumed to have been extended with the local
233 * variables.
235 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
236 __isl_take isl_reordering *r, int n_div)
238 isl_vec *res;
239 int i;
241 if (!vec || !r)
242 goto error;
244 res = isl_vec_alloc(vec->ctx,
245 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
246 isl_seq_cpy(res->el, vec->el, 2);
247 isl_seq_clr(res->el + 2, res->size - 2);
248 for (i = 0; i < r->len; ++i)
249 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
251 isl_reordering_free(r);
252 isl_vec_free(vec);
253 return res;
254 error:
255 isl_vec_free(vec);
256 isl_reordering_free(r);
257 return NULL;
260 /* Reorder the dimensions of the domain of "aff" according
261 * to the given reordering.
263 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
264 __isl_take isl_reordering *r)
266 aff = isl_aff_cow(aff);
267 if (!aff)
268 goto error;
270 r = isl_reordering_extend(r, aff->ls->div->n_row);
271 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
272 aff->ls->div->n_row);
273 aff->ls = isl_local_space_realign(aff->ls, r);
275 if (!aff->v || !aff->ls)
276 return isl_aff_free(aff);
278 return aff;
279 error:
280 isl_aff_free(aff);
281 isl_reordering_free(r);
282 return NULL;
285 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
286 __isl_take isl_space *model)
288 if (!aff || !model)
289 goto error;
291 if (!isl_space_match(aff->ls->dim, isl_dim_param,
292 model, isl_dim_param)) {
293 isl_reordering *exp;
295 model = isl_space_drop_dims(model, isl_dim_in,
296 0, isl_space_dim(model, isl_dim_in));
297 model = isl_space_drop_dims(model, isl_dim_out,
298 0, isl_space_dim(model, isl_dim_out));
299 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
300 exp = isl_reordering_extend_space(exp,
301 isl_aff_get_domain_space(aff));
302 aff = isl_aff_realign_domain(aff, exp);
305 isl_space_free(model);
306 return aff;
307 error:
308 isl_space_free(model);
309 isl_aff_free(aff);
310 return NULL;
313 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
315 if (!aff)
316 return -1;
318 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
321 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
323 int equal;
325 if (!aff1 || !aff2)
326 return -1;
328 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
329 if (equal < 0 || !equal)
330 return equal;
332 return isl_vec_is_equal(aff1->v, aff2->v);
335 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
337 if (!aff)
338 return -1;
339 isl_int_set(*v, aff->v->el[0]);
340 return 0;
343 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
345 if (!aff)
346 return -1;
347 isl_int_set(*v, aff->v->el[1]);
348 return 0;
351 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
352 enum isl_dim_type type, int pos, isl_int *v)
354 if (!aff)
355 return -1;
357 if (type == isl_dim_out)
358 isl_die(aff->v->ctx, isl_error_invalid,
359 "output/set dimension does not have a coefficient",
360 return -1);
361 if (type == isl_dim_in)
362 type = isl_dim_set;
364 if (pos >= isl_local_space_dim(aff->ls, type))
365 isl_die(aff->v->ctx, isl_error_invalid,
366 "position out of bounds", return -1);
368 pos += isl_local_space_offset(aff->ls, type);
369 isl_int_set(*v, aff->v->el[1 + pos]);
371 return 0;
374 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
376 aff = isl_aff_cow(aff);
377 if (!aff)
378 return NULL;
380 aff->v = isl_vec_cow(aff->v);
381 if (!aff->v)
382 return isl_aff_free(aff);
384 isl_int_set(aff->v->el[0], v);
386 return aff;
389 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
391 aff = isl_aff_cow(aff);
392 if (!aff)
393 return NULL;
395 aff->v = isl_vec_cow(aff->v);
396 if (!aff->v)
397 return isl_aff_free(aff);
399 isl_int_set(aff->v->el[1], v);
401 return aff;
404 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
406 if (isl_int_is_zero(v))
407 return aff;
409 aff = isl_aff_cow(aff);
410 if (!aff)
411 return NULL;
413 aff->v = isl_vec_cow(aff->v);
414 if (!aff->v)
415 return isl_aff_free(aff);
417 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
419 return aff;
422 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
424 isl_int t;
426 isl_int_init(t);
427 isl_int_set_si(t, v);
428 aff = isl_aff_add_constant(aff, t);
429 isl_int_clear(t);
431 return aff;
434 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
436 aff = isl_aff_cow(aff);
437 if (!aff)
438 return NULL;
440 aff->v = isl_vec_cow(aff->v);
441 if (!aff->v)
442 return isl_aff_free(aff);
444 isl_int_set_si(aff->v->el[1], v);
446 return aff;
449 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
450 enum isl_dim_type type, int pos, isl_int v)
452 if (!aff)
453 return NULL;
455 if (type == isl_dim_out)
456 isl_die(aff->v->ctx, isl_error_invalid,
457 "output/set dimension does not have a coefficient",
458 return isl_aff_free(aff));
459 if (type == isl_dim_in)
460 type = isl_dim_set;
462 if (pos >= isl_local_space_dim(aff->ls, type))
463 isl_die(aff->v->ctx, isl_error_invalid,
464 "position out of bounds", return isl_aff_free(aff));
466 aff = isl_aff_cow(aff);
467 if (!aff)
468 return NULL;
470 aff->v = isl_vec_cow(aff->v);
471 if (!aff->v)
472 return isl_aff_free(aff);
474 pos += isl_local_space_offset(aff->ls, type);
475 isl_int_set(aff->v->el[1 + pos], v);
477 return aff;
480 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
481 enum isl_dim_type type, int pos, int v)
483 if (!aff)
484 return NULL;
486 if (type == isl_dim_out)
487 isl_die(aff->v->ctx, isl_error_invalid,
488 "output/set dimension does not have a coefficient",
489 return isl_aff_free(aff));
490 if (type == isl_dim_in)
491 type = isl_dim_set;
493 if (pos >= isl_local_space_dim(aff->ls, type))
494 isl_die(aff->v->ctx, isl_error_invalid,
495 "position out of bounds", return isl_aff_free(aff));
497 aff = isl_aff_cow(aff);
498 if (!aff)
499 return NULL;
501 aff->v = isl_vec_cow(aff->v);
502 if (!aff->v)
503 return isl_aff_free(aff);
505 pos += isl_local_space_offset(aff->ls, type);
506 isl_int_set_si(aff->v->el[1 + pos], v);
508 return aff;
511 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
512 enum isl_dim_type type, int pos, isl_int v)
514 if (!aff)
515 return NULL;
517 if (type == isl_dim_out)
518 isl_die(aff->v->ctx, isl_error_invalid,
519 "output/set dimension does not have a coefficient",
520 return isl_aff_free(aff));
521 if (type == isl_dim_in)
522 type = isl_dim_set;
524 if (pos >= isl_local_space_dim(aff->ls, type))
525 isl_die(aff->v->ctx, isl_error_invalid,
526 "position out of bounds", return isl_aff_free(aff));
528 aff = isl_aff_cow(aff);
529 if (!aff)
530 return NULL;
532 aff->v = isl_vec_cow(aff->v);
533 if (!aff->v)
534 return isl_aff_free(aff);
536 pos += isl_local_space_offset(aff->ls, type);
537 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
539 return aff;
542 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
543 enum isl_dim_type type, int pos, int v)
545 isl_int t;
547 isl_int_init(t);
548 isl_int_set_si(t, v);
549 aff = isl_aff_add_coefficient(aff, type, pos, t);
550 isl_int_clear(t);
552 return aff;
555 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
557 if (!aff)
558 return NULL;
560 return isl_local_space_get_div(aff->ls, pos);
563 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
565 aff = isl_aff_cow(aff);
566 if (!aff)
567 return NULL;
568 aff->v = isl_vec_cow(aff->v);
569 if (!aff->v)
570 return isl_aff_free(aff);
572 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
574 return aff;
577 /* Remove divs from the local space that do not appear in the affine
578 * expression.
579 * We currently only remove divs at the end.
580 * Some intermediate divs may also not appear directly in the affine
581 * expression, but we would also need to check that no other divs are
582 * defined in terms of them.
584 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
586 int pos;
587 int off;
588 int n;
590 if (!aff)
591 return NULL;
593 n = isl_local_space_dim(aff->ls, isl_dim_div);
594 off = isl_local_space_offset(aff->ls, isl_dim_div);
596 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
597 if (pos == n)
598 return aff;
600 aff = isl_aff_cow(aff);
601 if (!aff)
602 return NULL;
604 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
605 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
606 if (!aff->ls || !aff->v)
607 return isl_aff_free(aff);
609 return aff;
612 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
614 if (!aff)
615 return NULL;
616 aff->v = isl_vec_normalize(aff->v);
617 if (!aff->v)
618 return isl_aff_free(aff);
619 aff = isl_aff_remove_unused_divs(aff);
620 return aff;
623 /* Given f, return floor(f).
624 * If f is an integer expression, then just return f.
625 * Otherwise, if f = g/m, write g = q m + r,
626 * create a new div d = [r/m] and return the expression q + d.
627 * The coefficients in r are taken to lie between -m/2 and m/2.
629 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
631 int i;
632 int size;
633 isl_ctx *ctx;
634 isl_vec *div;
636 if (!aff)
637 return NULL;
639 if (isl_int_is_one(aff->v->el[0]))
640 return aff;
642 aff = isl_aff_cow(aff);
643 if (!aff)
644 return NULL;
646 aff->v = isl_vec_cow(aff->v);
647 div = isl_vec_copy(aff->v);
648 div = isl_vec_cow(div);
649 if (!div)
650 return isl_aff_free(aff);
652 ctx = isl_aff_get_ctx(aff);
653 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
654 for (i = 1; i < aff->v->size; ++i) {
655 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
656 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
657 if (isl_int_gt(div->el[i], aff->v->el[0])) {
658 isl_int_sub(div->el[i], div->el[i], div->el[0]);
659 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
663 aff->ls = isl_local_space_add_div(aff->ls, div);
664 if (!aff->ls)
665 return isl_aff_free(aff);
667 size = aff->v->size;
668 aff->v = isl_vec_extend(aff->v, size + 1);
669 if (!aff->v)
670 return isl_aff_free(aff);
671 isl_int_set_si(aff->v->el[0], 1);
672 isl_int_set_si(aff->v->el[size], 1);
674 return aff;
677 /* Compute
679 * aff mod m = aff - m * floor(aff/m)
681 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
683 isl_aff *res;
685 res = isl_aff_copy(aff);
686 aff = isl_aff_scale_down(aff, m);
687 aff = isl_aff_floor(aff);
688 aff = isl_aff_scale(aff, m);
689 res = isl_aff_sub(res, aff);
691 return res;
694 /* Compute
696 * pwaff mod m = pwaff - m * floor(pwaff/m)
698 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
700 isl_pw_aff *res;
702 res = isl_pw_aff_copy(pwaff);
703 pwaff = isl_pw_aff_scale_down(pwaff, m);
704 pwaff = isl_pw_aff_floor(pwaff);
705 pwaff = isl_pw_aff_scale(pwaff, m);
706 res = isl_pw_aff_sub(res, pwaff);
708 return res;
711 /* Given f, return ceil(f).
712 * If f is an integer expression, then just return f.
713 * Otherwise, create a new div d = [-f] and return the expression -d.
715 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
717 if (!aff)
718 return NULL;
720 if (isl_int_is_one(aff->v->el[0]))
721 return aff;
723 aff = isl_aff_neg(aff);
724 aff = isl_aff_floor(aff);
725 aff = isl_aff_neg(aff);
727 return aff;
730 /* Apply the expansion computed by isl_merge_divs.
731 * The expansion itself is given by "exp" while the resulting
732 * list of divs is given by "div".
734 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
735 __isl_take isl_mat *div, int *exp)
737 int i, j;
738 int old_n_div;
739 int new_n_div;
740 int offset;
742 aff = isl_aff_cow(aff);
743 if (!aff || !div)
744 goto error;
746 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
747 new_n_div = isl_mat_rows(div);
748 if (new_n_div < old_n_div)
749 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
750 "not an expansion", goto error);
752 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
753 if (!aff->v)
754 goto error;
756 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
757 j = old_n_div - 1;
758 for (i = new_n_div - 1; i >= 0; --i) {
759 if (j >= 0 && exp[j] == i) {
760 if (i != j)
761 isl_int_swap(aff->v->el[offset + i],
762 aff->v->el[offset + j]);
763 j--;
764 } else
765 isl_int_set_si(aff->v->el[offset + i], 0);
768 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
769 if (!aff->ls)
770 goto error;
771 isl_mat_free(div);
772 return aff;
773 error:
774 isl_aff_free(aff);
775 isl_mat_free(div);
776 return NULL;
779 /* Add two affine expressions that live in the same local space.
781 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
782 __isl_take isl_aff *aff2)
784 isl_int gcd, f;
786 aff1 = isl_aff_cow(aff1);
787 if (!aff1 || !aff2)
788 goto error;
790 aff1->v = isl_vec_cow(aff1->v);
791 if (!aff1->v)
792 goto error;
794 isl_int_init(gcd);
795 isl_int_init(f);
796 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
797 isl_int_divexact(f, aff2->v->el[0], gcd);
798 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
799 isl_int_divexact(f, aff1->v->el[0], gcd);
800 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
801 isl_int_divexact(f, aff2->v->el[0], gcd);
802 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
803 isl_int_clear(f);
804 isl_int_clear(gcd);
806 isl_aff_free(aff2);
807 return aff1;
808 error:
809 isl_aff_free(aff1);
810 isl_aff_free(aff2);
811 return NULL;
814 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
815 __isl_take isl_aff *aff2)
817 isl_ctx *ctx;
818 int *exp1 = NULL;
819 int *exp2 = NULL;
820 isl_mat *div;
822 if (!aff1 || !aff2)
823 goto error;
825 ctx = isl_aff_get_ctx(aff1);
826 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
827 isl_die(ctx, isl_error_invalid,
828 "spaces don't match", goto error);
830 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
831 return add_expanded(aff1, aff2);
833 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
834 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
835 if (!exp1 || !exp2)
836 goto error;
838 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
839 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
840 aff2 = isl_aff_expand_divs(aff2, div, exp2);
841 free(exp1);
842 free(exp2);
844 return add_expanded(aff1, aff2);
845 error:
846 free(exp1);
847 free(exp2);
848 isl_aff_free(aff1);
849 isl_aff_free(aff2);
850 return NULL;
853 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
854 __isl_take isl_aff *aff2)
856 return isl_aff_add(aff1, isl_aff_neg(aff2));
859 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
861 isl_int gcd;
863 if (isl_int_is_one(f))
864 return aff;
866 aff = isl_aff_cow(aff);
867 if (!aff)
868 return NULL;
869 aff->v = isl_vec_cow(aff->v);
870 if (!aff->v)
871 return isl_aff_free(aff);
873 isl_int_init(gcd);
874 isl_int_gcd(gcd, aff->v->el[0], f);
875 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
876 isl_int_divexact(gcd, f, gcd);
877 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
878 isl_int_clear(gcd);
880 return aff;
883 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
885 isl_int gcd;
887 if (isl_int_is_one(f))
888 return aff;
890 aff = isl_aff_cow(aff);
891 if (!aff)
892 return NULL;
893 aff->v = isl_vec_cow(aff->v);
894 if (!aff->v)
895 return isl_aff_free(aff);
897 isl_int_init(gcd);
898 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
899 isl_int_gcd(gcd, gcd, f);
900 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
901 isl_int_divexact(gcd, f, gcd);
902 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
903 isl_int_clear(gcd);
905 return aff;
908 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
910 isl_int v;
912 if (f == 1)
913 return aff;
915 isl_int_init(v);
916 isl_int_set_ui(v, f);
917 aff = isl_aff_scale_down(aff, v);
918 isl_int_clear(v);
920 return aff;
923 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
924 enum isl_dim_type type, unsigned pos, const char *s)
926 aff = isl_aff_cow(aff);
927 if (!aff)
928 return NULL;
929 if (type == isl_dim_out)
930 isl_die(aff->v->ctx, isl_error_invalid,
931 "cannot set name of output/set dimension",
932 return isl_aff_free(aff));
933 if (type == isl_dim_in)
934 type = isl_dim_set;
935 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
936 if (!aff->ls)
937 return isl_aff_free(aff);
939 return aff;
942 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
943 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
945 aff = isl_aff_cow(aff);
946 if (!aff)
947 return isl_id_free(id);
948 if (type == isl_dim_out)
949 isl_die(aff->v->ctx, isl_error_invalid,
950 "cannot set name of output/set dimension",
951 goto error);
952 if (type == isl_dim_in)
953 type = isl_dim_set;
954 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
955 if (!aff->ls)
956 return isl_aff_free(aff);
958 return aff;
959 error:
960 isl_id_free(id);
961 isl_aff_free(aff);
962 return NULL;
965 /* Exploit the equalities in "eq" to simplify the affine expression
966 * and the expressions of the integer divisions in the local space.
967 * The integer divisions in this local space are assumed to appear
968 * as regular dimensions in "eq".
970 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
971 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
973 int i, j;
974 unsigned total;
975 unsigned n_div;
977 if (!eq)
978 goto error;
979 if (eq->n_eq == 0) {
980 isl_basic_set_free(eq);
981 return aff;
984 aff = isl_aff_cow(aff);
985 if (!aff)
986 goto error;
988 aff->ls = isl_local_space_substitute_equalities(aff->ls,
989 isl_basic_set_copy(eq));
990 if (!aff->ls)
991 goto error;
993 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
994 n_div = eq->n_div;
995 for (i = 0; i < eq->n_eq; ++i) {
996 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
997 if (j < 0 || j == 0 || j >= total)
998 continue;
1000 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1001 &aff->v->el[0]);
1004 isl_basic_set_free(eq);
1005 aff = isl_aff_normalize(aff);
1006 return aff;
1007 error:
1008 isl_basic_set_free(eq);
1009 isl_aff_free(aff);
1010 return NULL;
1013 /* Exploit the equalities in "eq" to simplify the affine expression
1014 * and the expressions of the integer divisions in the local space.
1016 static __isl_give isl_aff *isl_aff_substitute_equalities(
1017 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1019 int n_div;
1021 if (!aff || !eq)
1022 goto error;
1023 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1024 if (n_div > 0)
1025 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1026 return isl_aff_substitute_equalities_lifted(aff, eq);
1027 error:
1028 isl_basic_set_free(eq);
1029 isl_aff_free(aff);
1030 return NULL;
1033 /* Look for equalities among the variables shared by context and aff
1034 * and the integer divisions of aff, if any.
1035 * The equalities are then used to eliminate coefficients and/or integer
1036 * divisions from aff.
1038 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1039 __isl_take isl_set *context)
1041 isl_basic_set *hull;
1042 int n_div;
1044 if (!aff)
1045 goto error;
1046 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1047 if (n_div > 0) {
1048 isl_basic_set *bset;
1049 isl_local_space *ls;
1050 context = isl_set_add_dims(context, isl_dim_set, n_div);
1051 ls = isl_aff_get_domain_local_space(aff);
1052 bset = isl_basic_set_from_local_space(ls);
1053 bset = isl_basic_set_lift(bset);
1054 bset = isl_basic_set_flatten(bset);
1055 context = isl_set_intersect(context,
1056 isl_set_from_basic_set(bset));
1059 hull = isl_set_affine_hull(context);
1060 return isl_aff_substitute_equalities_lifted(aff, hull);
1061 error:
1062 isl_aff_free(aff);
1063 isl_set_free(context);
1064 return NULL;
1067 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1068 __isl_take isl_set *context)
1070 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1071 dom_context = isl_set_intersect_params(dom_context, context);
1072 return isl_aff_gist(aff, dom_context);
1075 /* Return a basic set containing those elements in the space
1076 * of aff where it is non-negative.
1078 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1080 isl_constraint *ineq;
1081 isl_basic_set *bset;
1083 ineq = isl_inequality_from_aff(aff);
1085 bset = isl_basic_set_from_constraint(ineq);
1086 bset = isl_basic_set_simplify(bset);
1087 return bset;
1090 /* Return a basic set containing those elements in the space
1091 * of aff where it is zero.
1093 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1095 isl_constraint *ineq;
1096 isl_basic_set *bset;
1098 ineq = isl_equality_from_aff(aff);
1100 bset = isl_basic_set_from_constraint(ineq);
1101 bset = isl_basic_set_simplify(bset);
1102 return bset;
1105 /* Return a basic set containing those elements in the shared space
1106 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1108 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1109 __isl_take isl_aff *aff2)
1111 aff1 = isl_aff_sub(aff1, aff2);
1113 return isl_aff_nonneg_basic_set(aff1);
1116 /* Return a basic set containing those elements in the shared space
1117 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1119 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1120 __isl_take isl_aff *aff2)
1122 return isl_aff_ge_basic_set(aff2, aff1);
1125 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1126 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1128 aff1 = isl_aff_add(aff1, aff2);
1129 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1130 return aff1;
1133 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1135 if (!aff)
1136 return -1;
1138 return 0;
1141 /* Check whether the given affine expression has non-zero coefficient
1142 * for any dimension in the given range or if any of these dimensions
1143 * appear with non-zero coefficients in any of the integer divisions
1144 * involved in the affine expression.
1146 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1147 enum isl_dim_type type, unsigned first, unsigned n)
1149 int i;
1150 isl_ctx *ctx;
1151 int *active = NULL;
1152 int involves = 0;
1154 if (!aff)
1155 return -1;
1156 if (n == 0)
1157 return 0;
1159 ctx = isl_aff_get_ctx(aff);
1160 if (first + n > isl_aff_dim(aff, type))
1161 isl_die(ctx, isl_error_invalid,
1162 "range out of bounds", return -1);
1164 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1165 if (!active)
1166 goto error;
1168 first += isl_local_space_offset(aff->ls, type) - 1;
1169 for (i = 0; i < n; ++i)
1170 if (active[first + i]) {
1171 involves = 1;
1172 break;
1175 free(active);
1177 return involves;
1178 error:
1179 free(active);
1180 return -1;
1183 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1184 enum isl_dim_type type, unsigned first, unsigned n)
1186 isl_ctx *ctx;
1188 if (!aff)
1189 return NULL;
1190 if (type == isl_dim_out)
1191 isl_die(aff->v->ctx, isl_error_invalid,
1192 "cannot drop output/set dimension",
1193 return isl_aff_free(aff));
1194 if (type == isl_dim_in)
1195 type = isl_dim_set;
1196 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1197 return aff;
1199 ctx = isl_aff_get_ctx(aff);
1200 if (first + n > isl_local_space_dim(aff->ls, type))
1201 isl_die(ctx, isl_error_invalid, "range out of bounds",
1202 return isl_aff_free(aff));
1204 aff = isl_aff_cow(aff);
1205 if (!aff)
1206 return NULL;
1208 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1209 if (!aff->ls)
1210 return isl_aff_free(aff);
1212 first += 1 + isl_local_space_offset(aff->ls, type);
1213 aff->v = isl_vec_drop_els(aff->v, first, n);
1214 if (!aff->v)
1215 return isl_aff_free(aff);
1217 return aff;
1220 /* Project the domain of the affine expression onto its parameter space.
1221 * The affine expression may not involve any of the domain dimensions.
1223 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1225 isl_space *space;
1226 unsigned n;
1227 int involves;
1229 n = isl_aff_dim(aff, isl_dim_in);
1230 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1231 if (involves < 0)
1232 return isl_aff_free(aff);
1233 if (involves)
1234 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1235 "affine expression involves some of the domain dimensions",
1236 return isl_aff_free(aff));
1237 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1238 space = isl_aff_get_domain_space(aff);
1239 space = isl_space_params(space);
1240 aff = isl_aff_reset_domain_space(aff, space);
1241 return aff;
1244 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1245 enum isl_dim_type type, unsigned first, unsigned n)
1247 isl_ctx *ctx;
1249 if (!aff)
1250 return NULL;
1251 if (type == isl_dim_out)
1252 isl_die(aff->v->ctx, isl_error_invalid,
1253 "cannot insert output/set dimensions",
1254 return isl_aff_free(aff));
1255 if (type == isl_dim_in)
1256 type = isl_dim_set;
1257 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1258 return aff;
1260 ctx = isl_aff_get_ctx(aff);
1261 if (first > isl_local_space_dim(aff->ls, type))
1262 isl_die(ctx, isl_error_invalid, "position out of bounds",
1263 return isl_aff_free(aff));
1265 aff = isl_aff_cow(aff);
1266 if (!aff)
1267 return NULL;
1269 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1270 if (!aff->ls)
1271 return isl_aff_free(aff);
1273 first += 1 + isl_local_space_offset(aff->ls, type);
1274 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1275 if (!aff->v)
1276 return isl_aff_free(aff);
1278 return aff;
1281 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1282 enum isl_dim_type type, unsigned n)
1284 unsigned pos;
1286 pos = isl_aff_dim(aff, type);
1288 return isl_aff_insert_dims(aff, type, pos, n);
1291 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1292 enum isl_dim_type type, unsigned n)
1294 unsigned pos;
1296 pos = isl_pw_aff_dim(pwaff, type);
1298 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1301 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1303 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1304 return isl_pw_aff_alloc(dom, aff);
1307 #undef PW
1308 #define PW isl_pw_aff
1309 #undef EL
1310 #define EL isl_aff
1311 #undef EL_IS_ZERO
1312 #define EL_IS_ZERO is_empty
1313 #undef ZERO
1314 #define ZERO empty
1315 #undef IS_ZERO
1316 #define IS_ZERO is_empty
1317 #undef FIELD
1318 #define FIELD aff
1319 #undef DEFAULT_IS_ZERO
1320 #define DEFAULT_IS_ZERO 0
1322 #define NO_EVAL
1323 #define NO_OPT
1324 #define NO_MOVE_DIMS
1325 #define NO_LIFT
1326 #define NO_MORPH
1328 #include <isl_pw_templ.c>
1330 static __isl_give isl_set *align_params_pw_pw_set_and(
1331 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1332 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1333 __isl_take isl_pw_aff *pwaff2))
1335 if (!pwaff1 || !pwaff2)
1336 goto error;
1337 if (isl_space_match(pwaff1->dim, isl_dim_param,
1338 pwaff2->dim, isl_dim_param))
1339 return fn(pwaff1, pwaff2);
1340 if (!isl_space_has_named_params(pwaff1->dim) ||
1341 !isl_space_has_named_params(pwaff2->dim))
1342 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1343 "unaligned unnamed parameters", goto error);
1344 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1345 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1346 return fn(pwaff1, pwaff2);
1347 error:
1348 isl_pw_aff_free(pwaff1);
1349 isl_pw_aff_free(pwaff2);
1350 return NULL;
1353 /* Compute a piecewise quasi-affine expression with a domain that
1354 * is the union of those of pwaff1 and pwaff2 and such that on each
1355 * cell, the quasi-affine expression is the better (according to cmp)
1356 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1357 * is defined on a given cell, then the associated expression
1358 * is the defined one.
1360 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1361 __isl_take isl_pw_aff *pwaff2,
1362 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1363 __isl_take isl_aff *aff2))
1365 int i, j, n;
1366 isl_pw_aff *res;
1367 isl_ctx *ctx;
1368 isl_set *set;
1370 if (!pwaff1 || !pwaff2)
1371 goto error;
1373 ctx = isl_space_get_ctx(pwaff1->dim);
1374 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1375 isl_die(ctx, isl_error_invalid,
1376 "arguments should live in same space", goto error);
1378 if (isl_pw_aff_is_empty(pwaff1)) {
1379 isl_pw_aff_free(pwaff1);
1380 return pwaff2;
1383 if (isl_pw_aff_is_empty(pwaff2)) {
1384 isl_pw_aff_free(pwaff2);
1385 return pwaff1;
1388 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1389 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1391 for (i = 0; i < pwaff1->n; ++i) {
1392 set = isl_set_copy(pwaff1->p[i].set);
1393 for (j = 0; j < pwaff2->n; ++j) {
1394 struct isl_set *common;
1395 isl_set *better;
1397 common = isl_set_intersect(
1398 isl_set_copy(pwaff1->p[i].set),
1399 isl_set_copy(pwaff2->p[j].set));
1400 better = isl_set_from_basic_set(cmp(
1401 isl_aff_copy(pwaff2->p[j].aff),
1402 isl_aff_copy(pwaff1->p[i].aff)));
1403 better = isl_set_intersect(common, better);
1404 if (isl_set_plain_is_empty(better)) {
1405 isl_set_free(better);
1406 continue;
1408 set = isl_set_subtract(set, isl_set_copy(better));
1410 res = isl_pw_aff_add_piece(res, better,
1411 isl_aff_copy(pwaff2->p[j].aff));
1413 res = isl_pw_aff_add_piece(res, set,
1414 isl_aff_copy(pwaff1->p[i].aff));
1417 for (j = 0; j < pwaff2->n; ++j) {
1418 set = isl_set_copy(pwaff2->p[j].set);
1419 for (i = 0; i < pwaff1->n; ++i)
1420 set = isl_set_subtract(set,
1421 isl_set_copy(pwaff1->p[i].set));
1422 res = isl_pw_aff_add_piece(res, set,
1423 isl_aff_copy(pwaff2->p[j].aff));
1426 isl_pw_aff_free(pwaff1);
1427 isl_pw_aff_free(pwaff2);
1429 return res;
1430 error:
1431 isl_pw_aff_free(pwaff1);
1432 isl_pw_aff_free(pwaff2);
1433 return NULL;
1436 /* Compute a piecewise quasi-affine expression with a domain that
1437 * is the union of those of pwaff1 and pwaff2 and such that on each
1438 * cell, the quasi-affine expression is the maximum of those of pwaff1
1439 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1440 * cell, then the associated expression is the defined one.
1442 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1443 __isl_take isl_pw_aff *pwaff2)
1445 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1448 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1449 __isl_take isl_pw_aff *pwaff2)
1451 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1452 &pw_aff_union_max);
1455 /* Compute a piecewise quasi-affine expression with a domain that
1456 * is the union of those of pwaff1 and pwaff2 and such that on each
1457 * cell, the quasi-affine expression is the minimum of those of pwaff1
1458 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1459 * cell, then the associated expression is the defined one.
1461 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1462 __isl_take isl_pw_aff *pwaff2)
1464 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1467 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1468 __isl_take isl_pw_aff *pwaff2)
1470 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1471 &pw_aff_union_min);
1474 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1475 __isl_take isl_pw_aff *pwaff2, int max)
1477 if (max)
1478 return isl_pw_aff_union_max(pwaff1, pwaff2);
1479 else
1480 return isl_pw_aff_union_min(pwaff1, pwaff2);
1483 /* Construct a map with as domain the domain of pwaff and
1484 * one-dimensional range corresponding to the affine expressions.
1486 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1488 int i;
1489 isl_space *dim;
1490 isl_map *map;
1492 if (!pwaff)
1493 return NULL;
1495 dim = isl_pw_aff_get_space(pwaff);
1496 map = isl_map_empty(dim);
1498 for (i = 0; i < pwaff->n; ++i) {
1499 isl_basic_map *bmap;
1500 isl_map *map_i;
1502 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1503 map_i = isl_map_from_basic_map(bmap);
1504 map_i = isl_map_intersect_domain(map_i,
1505 isl_set_copy(pwaff->p[i].set));
1506 map = isl_map_union_disjoint(map, map_i);
1509 isl_pw_aff_free(pwaff);
1511 return map;
1514 /* Construct a map with as domain the domain of pwaff and
1515 * one-dimensional range corresponding to the affine expressions.
1517 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1519 if (!pwaff)
1520 return NULL;
1521 if (isl_space_is_set(pwaff->dim))
1522 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1523 "space of input is not a map",
1524 return isl_pw_aff_free(pwaff));
1525 return map_from_pw_aff(pwaff);
1528 /* Construct a one-dimensional set with as parameter domain
1529 * the domain of pwaff and the single set dimension
1530 * corresponding to the affine expressions.
1532 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1534 if (!pwaff)
1535 return NULL;
1536 if (!isl_space_is_set(pwaff->dim))
1537 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1538 "space of input is not a set",
1539 return isl_pw_aff_free(pwaff));
1540 return map_from_pw_aff(pwaff);
1543 /* Return a set containing those elements in the domain
1544 * of pwaff where it is non-negative.
1546 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1548 int i;
1549 isl_set *set;
1551 if (!pwaff)
1552 return NULL;
1554 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1556 for (i = 0; i < pwaff->n; ++i) {
1557 isl_basic_set *bset;
1558 isl_set *set_i;
1560 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1561 set_i = isl_set_from_basic_set(bset);
1562 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1563 set = isl_set_union_disjoint(set, set_i);
1566 isl_pw_aff_free(pwaff);
1568 return set;
1571 /* Return a set containing those elements in the domain
1572 * of pwaff where it is zero (if complement is 0) or not zero
1573 * (if complement is 1).
1575 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1576 int complement)
1578 int i;
1579 isl_set *set;
1581 if (!pwaff)
1582 return NULL;
1584 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1586 for (i = 0; i < pwaff->n; ++i) {
1587 isl_basic_set *bset;
1588 isl_set *set_i, *zero;
1590 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1591 zero = isl_set_from_basic_set(bset);
1592 set_i = isl_set_copy(pwaff->p[i].set);
1593 if (complement)
1594 set_i = isl_set_subtract(set_i, zero);
1595 else
1596 set_i = isl_set_intersect(set_i, zero);
1597 set = isl_set_union_disjoint(set, set_i);
1600 isl_pw_aff_free(pwaff);
1602 return set;
1605 /* Return a set containing those elements in the domain
1606 * of pwaff where it is zero.
1608 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1610 return pw_aff_zero_set(pwaff, 0);
1613 /* Return a set containing those elements in the domain
1614 * of pwaff where it is not zero.
1616 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1618 return pw_aff_zero_set(pwaff, 1);
1621 /* Return a set containing those elements in the shared domain
1622 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1624 * We compute the difference on the shared domain and then construct
1625 * the set of values where this difference is non-negative.
1626 * If strict is set, we first subtract 1 from the difference.
1627 * If equal is set, we only return the elements where pwaff1 and pwaff2
1628 * are equal.
1630 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1631 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1633 isl_set *set1, *set2;
1635 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1636 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1637 set1 = isl_set_intersect(set1, set2);
1638 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1639 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1640 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1642 if (strict) {
1643 isl_space *dim = isl_set_get_space(set1);
1644 isl_aff *aff;
1645 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1646 aff = isl_aff_add_constant_si(aff, -1);
1647 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1648 } else
1649 isl_set_free(set1);
1651 if (equal)
1652 return isl_pw_aff_zero_set(pwaff1);
1653 return isl_pw_aff_nonneg_set(pwaff1);
1656 /* Return a set containing those elements in the shared domain
1657 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1659 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1660 __isl_take isl_pw_aff *pwaff2)
1662 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1665 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1666 __isl_take isl_pw_aff *pwaff2)
1668 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1671 /* Return a set containing those elements in the shared domain
1672 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1674 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1675 __isl_take isl_pw_aff *pwaff2)
1677 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1680 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1681 __isl_take isl_pw_aff *pwaff2)
1683 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1686 /* Return a set containing those elements in the shared domain
1687 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1689 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1690 __isl_take isl_pw_aff *pwaff2)
1692 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1695 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1696 __isl_take isl_pw_aff *pwaff2)
1698 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1701 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1702 __isl_take isl_pw_aff *pwaff2)
1704 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1707 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1708 __isl_take isl_pw_aff *pwaff2)
1710 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1713 /* Return a set containing those elements in the shared domain
1714 * of the elements of list1 and list2 where each element in list1
1715 * has the relation specified by "fn" with each element in list2.
1717 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1718 __isl_take isl_pw_aff_list *list2,
1719 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1720 __isl_take isl_pw_aff *pwaff2))
1722 int i, j;
1723 isl_ctx *ctx;
1724 isl_set *set;
1726 if (!list1 || !list2)
1727 goto error;
1729 ctx = isl_pw_aff_list_get_ctx(list1);
1730 if (list1->n < 1 || list2->n < 1)
1731 isl_die(ctx, isl_error_invalid,
1732 "list should contain at least one element", goto error);
1734 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1735 for (i = 0; i < list1->n; ++i)
1736 for (j = 0; j < list2->n; ++j) {
1737 isl_set *set_ij;
1739 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1740 isl_pw_aff_copy(list2->p[j]));
1741 set = isl_set_intersect(set, set_ij);
1744 isl_pw_aff_list_free(list1);
1745 isl_pw_aff_list_free(list2);
1746 return set;
1747 error:
1748 isl_pw_aff_list_free(list1);
1749 isl_pw_aff_list_free(list2);
1750 return NULL;
1753 /* Return a set containing those elements in the shared domain
1754 * of the elements of list1 and list2 where each element in list1
1755 * is equal to each element in list2.
1757 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1758 __isl_take isl_pw_aff_list *list2)
1760 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1763 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1764 __isl_take isl_pw_aff_list *list2)
1766 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1769 /* Return a set containing those elements in the shared domain
1770 * of the elements of list1 and list2 where each element in list1
1771 * is less than or equal to each element in list2.
1773 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1774 __isl_take isl_pw_aff_list *list2)
1776 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1779 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1780 __isl_take isl_pw_aff_list *list2)
1782 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1785 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1786 __isl_take isl_pw_aff_list *list2)
1788 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1791 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1792 __isl_take isl_pw_aff_list *list2)
1794 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1798 /* Return a set containing those elements in the shared domain
1799 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1801 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1802 __isl_take isl_pw_aff *pwaff2)
1804 isl_set *set_lt, *set_gt;
1806 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1807 isl_pw_aff_copy(pwaff2));
1808 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1809 return isl_set_union_disjoint(set_lt, set_gt);
1812 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1813 __isl_take isl_pw_aff *pwaff2)
1815 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1818 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1819 isl_int v)
1821 int i;
1823 if (isl_int_is_one(v))
1824 return pwaff;
1825 if (!isl_int_is_pos(v))
1826 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1827 "factor needs to be positive",
1828 return isl_pw_aff_free(pwaff));
1829 pwaff = isl_pw_aff_cow(pwaff);
1830 if (!pwaff)
1831 return NULL;
1832 if (pwaff->n == 0)
1833 return pwaff;
1835 for (i = 0; i < pwaff->n; ++i) {
1836 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1837 if (!pwaff->p[i].aff)
1838 return isl_pw_aff_free(pwaff);
1841 return pwaff;
1844 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1846 int i;
1848 pwaff = isl_pw_aff_cow(pwaff);
1849 if (!pwaff)
1850 return NULL;
1851 if (pwaff->n == 0)
1852 return pwaff;
1854 for (i = 0; i < pwaff->n; ++i) {
1855 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1856 if (!pwaff->p[i].aff)
1857 return isl_pw_aff_free(pwaff);
1860 return pwaff;
1863 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1865 int i;
1867 pwaff = isl_pw_aff_cow(pwaff);
1868 if (!pwaff)
1869 return NULL;
1870 if (pwaff->n == 0)
1871 return pwaff;
1873 for (i = 0; i < pwaff->n; ++i) {
1874 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1875 if (!pwaff->p[i].aff)
1876 return isl_pw_aff_free(pwaff);
1879 return pwaff;
1882 /* Assuming that "cond1" and "cond2" are disjoint,
1883 * return an affine expression that is equal to pwaff1 on cond1
1884 * and to pwaff2 on cond2.
1886 static __isl_give isl_pw_aff *isl_pw_aff_select(
1887 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
1888 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
1890 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
1891 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
1893 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
1896 /* Return an affine expression that is equal to pwaff_true for elements
1897 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1898 * is zero.
1899 * That is, return cond ? pwaff_true : pwaff_false;
1901 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
1902 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1904 isl_set *cond_true, *cond_false;
1906 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1907 cond_false = isl_pw_aff_zero_set(cond);
1908 return isl_pw_aff_select(cond_true, pwaff_true,
1909 cond_false, pwaff_false);
1912 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1914 if (!aff)
1915 return -1;
1917 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1920 /* Check whether pwaff is a piecewise constant.
1922 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1924 int i;
1926 if (!pwaff)
1927 return -1;
1929 for (i = 0; i < pwaff->n; ++i) {
1930 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1931 if (is_cst < 0 || !is_cst)
1932 return is_cst;
1935 return 1;
1938 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1939 __isl_take isl_aff *aff2)
1941 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1942 return isl_aff_mul(aff2, aff1);
1944 if (!isl_aff_is_cst(aff2))
1945 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1946 "at least one affine expression should be constant",
1947 goto error);
1949 aff1 = isl_aff_cow(aff1);
1950 if (!aff1 || !aff2)
1951 goto error;
1953 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1954 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1956 isl_aff_free(aff2);
1957 return aff1;
1958 error:
1959 isl_aff_free(aff1);
1960 isl_aff_free(aff2);
1961 return NULL;
1964 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1965 __isl_take isl_pw_aff *pwaff2)
1967 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1970 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1971 __isl_take isl_pw_aff *pwaff2)
1973 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1976 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1977 __isl_take isl_pw_aff *pwaff2)
1979 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1982 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1983 __isl_take isl_pw_aff *pwaff2)
1985 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1988 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1989 __isl_take isl_pw_aff *pwaff2)
1991 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1994 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1995 __isl_take isl_pw_aff *pwaff2)
1997 isl_set *le;
1998 isl_set *dom;
2000 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2001 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2002 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2003 isl_pw_aff_copy(pwaff2));
2004 dom = isl_set_subtract(dom, isl_set_copy(le));
2005 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2008 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2009 __isl_take isl_pw_aff *pwaff2)
2011 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2014 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2015 __isl_take isl_pw_aff *pwaff2)
2017 isl_set *ge;
2018 isl_set *dom;
2020 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2021 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2022 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2023 isl_pw_aff_copy(pwaff2));
2024 dom = isl_set_subtract(dom, isl_set_copy(ge));
2025 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2028 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2029 __isl_take isl_pw_aff *pwaff2)
2031 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2034 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2035 __isl_take isl_pw_aff_list *list,
2036 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2037 __isl_take isl_pw_aff *pwaff2))
2039 int i;
2040 isl_ctx *ctx;
2041 isl_pw_aff *res;
2043 if (!list)
2044 return NULL;
2046 ctx = isl_pw_aff_list_get_ctx(list);
2047 if (list->n < 1)
2048 isl_die(ctx, isl_error_invalid,
2049 "list should contain at least one element",
2050 return isl_pw_aff_list_free(list));
2052 res = isl_pw_aff_copy(list->p[0]);
2053 for (i = 1; i < list->n; ++i)
2054 res = fn(res, isl_pw_aff_copy(list->p[i]));
2056 isl_pw_aff_list_free(list);
2057 return res;
2060 /* Return an isl_pw_aff that maps each element in the intersection of the
2061 * domains of the elements of list to the minimal corresponding affine
2062 * expression.
2064 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2066 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2069 /* Return an isl_pw_aff that maps each element in the intersection of the
2070 * domains of the elements of list to the maximal corresponding affine
2071 * expression.
2073 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2075 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2078 #undef BASE
2079 #define BASE aff
2081 #include <isl_multi_templ.c>
2083 /* Construct an isl_multi_aff in the given space with value zero in
2084 * each of the output dimensions.
2086 __isl_give isl_multi_aff *isl_multi_aff_zero(__isl_take isl_space *space)
2088 int n;
2089 isl_multi_aff *ma;
2091 if (!space)
2092 return NULL;
2094 n = isl_space_dim(space , isl_dim_out);
2095 ma = isl_multi_aff_alloc(isl_space_copy(space));
2097 if (!n)
2098 isl_space_free(space);
2099 else {
2100 int i;
2101 isl_local_space *ls;
2102 isl_aff *aff;
2104 space = isl_space_domain(space);
2105 ls = isl_local_space_from_space(space);
2106 aff = isl_aff_zero_on_domain(ls);
2108 for (i = 0; i < n; ++i)
2109 ma = isl_multi_aff_set_aff(ma, i, isl_aff_copy(aff));
2111 isl_aff_free(aff);
2114 return ma;
2117 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2118 __isl_take isl_multi_aff *maff2)
2120 int i;
2121 isl_ctx *ctx;
2123 maff1 = isl_multi_aff_cow(maff1);
2124 if (!maff1 || !maff2)
2125 goto error;
2127 ctx = isl_multi_aff_get_ctx(maff1);
2128 if (!isl_space_is_equal(maff1->space, maff2->space))
2129 isl_die(ctx, isl_error_invalid,
2130 "spaces don't match", goto error);
2132 for (i = 0; i < maff1->n; ++i) {
2133 maff1->p[i] = isl_aff_add(maff1->p[i],
2134 isl_aff_copy(maff2->p[i]));
2135 if (!maff1->p[i])
2136 goto error;
2139 isl_multi_aff_free(maff2);
2140 return maff1;
2141 error:
2142 isl_multi_aff_free(maff1);
2143 isl_multi_aff_free(maff2);
2144 return NULL;
2147 /* Exploit the equalities in "eq" to simplify the affine expressions.
2149 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2150 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2152 int i;
2154 maff = isl_multi_aff_cow(maff);
2155 if (!maff || !eq)
2156 goto error;
2158 for (i = 0; i < maff->n; ++i) {
2159 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2160 isl_basic_set_copy(eq));
2161 if (!maff->p[i])
2162 goto error;
2165 isl_basic_set_free(eq);
2166 return maff;
2167 error:
2168 isl_basic_set_free(eq);
2169 isl_multi_aff_free(maff);
2170 return NULL;
2173 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2174 isl_int f)
2176 int i;
2178 maff = isl_multi_aff_cow(maff);
2179 if (!maff)
2180 return NULL;
2182 for (i = 0; i < maff->n; ++i) {
2183 maff->p[i] = isl_aff_scale(maff->p[i], f);
2184 if (!maff->p[i])
2185 return isl_multi_aff_free(maff);
2188 return maff;
2191 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2192 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2194 maff1 = isl_multi_aff_add(maff1, maff2);
2195 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2196 return maff1;
2199 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2201 if (!maff)
2202 return -1;
2204 return 0;
2207 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2208 __isl_keep isl_multi_aff *maff2)
2210 int i;
2211 int equal;
2213 if (!maff1 || !maff2)
2214 return -1;
2215 if (maff1->n != maff2->n)
2216 return 0;
2217 equal = isl_space_is_equal(maff1->space, maff2->space);
2218 if (equal < 0 || !equal)
2219 return equal;
2221 for (i = 0; i < maff1->n; ++i) {
2222 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2223 if (equal < 0 || !equal)
2224 return equal;
2227 return 1;
2230 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2231 __isl_take isl_multi_aff *maff,
2232 enum isl_dim_type type, unsigned pos, const char *s)
2234 int i;
2236 maff = isl_multi_aff_cow(maff);
2237 if (!maff)
2238 return NULL;
2240 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2241 if (!maff->space)
2242 return isl_multi_aff_free(maff);
2243 for (i = 0; i < maff->n; ++i) {
2244 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2245 if (!maff->p[i])
2246 return isl_multi_aff_free(maff);
2249 return maff;
2252 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2253 enum isl_dim_type type, unsigned first, unsigned n)
2255 int i;
2257 maff = isl_multi_aff_cow(maff);
2258 if (!maff)
2259 return NULL;
2261 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2262 if (!maff->space)
2263 return isl_multi_aff_free(maff);
2265 if (type == isl_dim_out) {
2266 for (i = 0; i < n; ++i)
2267 isl_aff_free(maff->p[first + i]);
2268 for (i = first; i + n < maff->n; ++i)
2269 maff->p[i] = maff->p[i + n];
2270 maff->n -= n;
2271 return maff;
2274 for (i = 0; i < maff->n; ++i) {
2275 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2276 if (!maff->p[i])
2277 return isl_multi_aff_free(maff);
2280 return maff;
2283 #undef PW
2284 #define PW isl_pw_multi_aff
2285 #undef EL
2286 #define EL isl_multi_aff
2287 #undef EL_IS_ZERO
2288 #define EL_IS_ZERO is_empty
2289 #undef ZERO
2290 #define ZERO empty
2291 #undef IS_ZERO
2292 #define IS_ZERO is_empty
2293 #undef FIELD
2294 #define FIELD maff
2295 #undef DEFAULT_IS_ZERO
2296 #define DEFAULT_IS_ZERO 0
2298 #define NO_NEG
2299 #define NO_EVAL
2300 #define NO_OPT
2301 #define NO_INVOLVES_DIMS
2302 #define NO_MOVE_DIMS
2303 #define NO_INSERT_DIMS
2304 #define NO_LIFT
2305 #define NO_MORPH
2307 #include <isl_pw_templ.c>
2309 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2310 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2312 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2313 &isl_multi_aff_add);
2316 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2317 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2320 &pw_multi_aff_add);
2323 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2324 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2326 return isl_pw_multi_aff_union_add_(pma1, pma2);
2329 /* Construct a map mapping the domain the piecewise multi-affine expression
2330 * to its range, with each dimension in the range equated to the
2331 * corresponding affine expression on its cell.
2333 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2335 int i;
2336 isl_map *map;
2338 if (!pma)
2339 return NULL;
2341 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2343 for (i = 0; i < pma->n; ++i) {
2344 isl_multi_aff *maff;
2345 isl_basic_map *bmap;
2346 isl_map *map_i;
2348 maff = isl_multi_aff_copy(pma->p[i].maff);
2349 bmap = isl_basic_map_from_multi_aff(maff);
2350 map_i = isl_map_from_basic_map(bmap);
2351 map_i = isl_map_intersect_domain(map_i,
2352 isl_set_copy(pma->p[i].set));
2353 map = isl_map_union_disjoint(map, map_i);
2356 isl_pw_multi_aff_free(pma);
2357 return map;
2360 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2362 if (!isl_space_is_set(pma->dim))
2363 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2364 "isl_pw_multi_aff cannot be converted into an isl_set",
2365 return isl_pw_multi_aff_free(pma));
2367 return isl_map_from_pw_multi_aff(pma);
2370 /* Given a basic map with a single output dimension that is defined
2371 * in terms of the parameters and input dimensions using an equality,
2372 * extract an isl_aff that expresses the output dimension in terms
2373 * of the parameters and input dimensions.
2375 * Since some applications expect the result of isl_pw_multi_aff_from_map
2376 * to only contain integer affine expressions, we compute the floor
2377 * of the expression before returning.
2379 * This function shares some similarities with
2380 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2382 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2383 __isl_take isl_basic_map *bmap)
2385 int i;
2386 unsigned offset;
2387 unsigned total;
2388 isl_local_space *ls;
2389 isl_aff *aff;
2391 if (!bmap)
2392 return NULL;
2393 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2394 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2395 "basic map should have a single output dimension",
2396 goto error);
2397 offset = isl_basic_map_offset(bmap, isl_dim_out);
2398 total = isl_basic_map_total_dim(bmap);
2399 for (i = 0; i < bmap->n_eq; ++i) {
2400 if (isl_int_is_zero(bmap->eq[i][offset]))
2401 continue;
2402 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2403 1 + total - (offset + 1)) != -1)
2404 continue;
2405 break;
2407 if (i >= bmap->n_eq)
2408 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2409 "unable to find suitable equality", goto error);
2410 ls = isl_basic_map_get_local_space(bmap);
2411 aff = isl_aff_alloc(isl_local_space_domain(ls));
2412 if (!aff)
2413 goto error;
2414 if (isl_int_is_neg(bmap->eq[i][offset]))
2415 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2416 else
2417 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2418 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2419 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2420 isl_basic_map_free(bmap);
2422 aff = isl_aff_remove_unused_divs(aff);
2423 aff = isl_aff_floor(aff);
2424 return aff;
2425 error:
2426 isl_basic_map_free(bmap);
2427 return NULL;
2430 /* Given a basic map where each output dimension is defined
2431 * in terms of the parameters and input dimensions using an equality,
2432 * extract an isl_multi_aff that expresses the output dimensions in terms
2433 * of the parameters and input dimensions.
2435 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2436 __isl_take isl_basic_map *bmap)
2438 int i;
2439 unsigned n_out;
2440 isl_multi_aff *ma;
2442 if (!bmap)
2443 return NULL;
2445 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2446 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2448 for (i = 0; i < n_out; ++i) {
2449 isl_basic_map *bmap_i;
2450 isl_aff *aff;
2452 bmap_i = isl_basic_map_copy(bmap);
2453 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2454 i + 1, n_out - (1 + i));
2455 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2456 aff = extract_isl_aff_from_basic_map(bmap_i);
2457 ma = isl_multi_aff_set_aff(ma, i, aff);
2460 isl_basic_map_free(bmap);
2462 return ma;
2465 /* Create an isl_pw_multi_aff that is equivalent to
2466 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2467 * The given basic map is such that each output dimension is defined
2468 * in terms of the parameters and input dimensions using an equality.
2470 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2471 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2473 isl_multi_aff *ma;
2475 ma = extract_isl_multi_aff_from_basic_map(bmap);
2476 return isl_pw_multi_aff_alloc(domain, ma);
2479 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2480 * This obivously only works if the input "map" is single-valued.
2481 * If so, we compute the lexicographic minimum of the image in the form
2482 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2483 * to its lexicographic minimum.
2484 * If the input is not single-valued, we produce an error.
2486 * As a special case, we first check if all output dimensions are uniquely
2487 * defined in terms of the parameters and input dimensions over the entire
2488 * domain. If so, we extract the desired isl_pw_multi_aff directly
2489 * from the affine hull of "map" and its domain.
2491 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2493 int i;
2494 int sv;
2495 isl_pw_multi_aff *pma;
2496 isl_basic_map *hull;
2498 if (!map)
2499 return NULL;
2501 hull = isl_map_affine_hull(isl_map_copy(map));
2502 sv = isl_basic_map_plain_is_single_valued(hull);
2503 if (sv >= 0 && sv)
2504 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2505 isl_basic_map_free(hull);
2506 if (sv < 0)
2507 goto error;
2509 sv = isl_map_is_single_valued(map);
2510 if (sv < 0)
2511 goto error;
2512 if (!sv)
2513 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2514 "map is not single-valued", goto error);
2515 map = isl_map_make_disjoint(map);
2516 if (!map)
2517 return NULL;
2519 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2521 for (i = 0; i < map->n; ++i) {
2522 isl_pw_multi_aff *pma_i;
2523 isl_basic_map *bmap;
2524 bmap = isl_basic_map_copy(map->p[i]);
2525 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2526 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2529 isl_map_free(map);
2530 return pma;
2531 error:
2532 isl_map_free(map);
2533 return NULL;
2536 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2538 return isl_pw_multi_aff_from_map(set);
2541 /* Return the piecewise affine expression "set ? 1 : 0".
2543 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
2545 isl_pw_aff *pa;
2546 isl_space *space = isl_set_get_space(set);
2547 isl_local_space *ls = isl_local_space_from_space(space);
2548 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
2549 isl_aff *one = isl_aff_zero_on_domain(ls);
2551 one = isl_aff_add_constant_si(one, 1);
2552 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
2553 set = isl_set_complement(set);
2554 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
2556 return pa;
2559 /* Plug in "subs" for dimension "type", "pos" of "aff".
2561 * Let i be the dimension to replace and let "subs" be of the form
2563 * f/d
2565 * and "aff" of the form
2567 * (a i + g)/m
2569 * The result is
2571 * floor((a f + d g')/(m d))
2573 * where g' is the result of plugging in "subs" in each of the integer
2574 * divisions in g.
2576 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2577 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2579 isl_ctx *ctx;
2580 isl_int v;
2582 aff = isl_aff_cow(aff);
2583 if (!aff || !subs)
2584 return isl_aff_free(aff);
2586 ctx = isl_aff_get_ctx(aff);
2587 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2588 isl_die(ctx, isl_error_invalid,
2589 "spaces don't match", return isl_aff_free(aff));
2590 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2591 isl_die(ctx, isl_error_unsupported,
2592 "cannot handle divs yet", return isl_aff_free(aff));
2594 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2595 if (!aff->ls)
2596 return isl_aff_free(aff);
2598 aff->v = isl_vec_cow(aff->v);
2599 if (!aff->v)
2600 return isl_aff_free(aff);
2602 pos += isl_local_space_offset(aff->ls, type);
2604 isl_int_init(v);
2605 isl_int_set(v, aff->v->el[1 + pos]);
2606 isl_int_set_si(aff->v->el[1 + pos], 0);
2607 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2608 v, subs->v->el + 1, subs->v->size - 1);
2609 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2610 isl_int_clear(v);
2612 return aff;
2615 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2616 * expressions in "maff".
2618 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2619 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2620 __isl_keep isl_aff *subs)
2622 int i;
2624 maff = isl_multi_aff_cow(maff);
2625 if (!maff || !subs)
2626 return isl_multi_aff_free(maff);
2628 if (type == isl_dim_in)
2629 type = isl_dim_set;
2631 for (i = 0; i < maff->n; ++i) {
2632 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2633 if (!maff->p[i])
2634 return isl_multi_aff_free(maff);
2637 return maff;
2640 /* Plug in "subs" for dimension "type", "pos" of "pma".
2642 * pma is of the form
2644 * A_i(v) -> M_i(v)
2646 * while subs is of the form
2648 * v' = B_j(v) -> S_j
2650 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2651 * has a contribution in the result, in particular
2653 * C_ij(S_j) -> M_i(S_j)
2655 * Note that plugging in S_j in C_ij may also result in an empty set
2656 * and this contribution should simply be discarded.
2658 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2659 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2660 __isl_keep isl_pw_aff *subs)
2662 int i, j, n;
2663 isl_pw_multi_aff *res;
2665 if (!pma || !subs)
2666 return isl_pw_multi_aff_free(pma);
2668 n = pma->n * subs->n;
2669 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2671 for (i = 0; i < pma->n; ++i) {
2672 for (j = 0; j < subs->n; ++j) {
2673 isl_set *common;
2674 isl_multi_aff *res_ij;
2675 common = isl_set_intersect(
2676 isl_set_copy(pma->p[i].set),
2677 isl_set_copy(subs->p[j].set));
2678 common = isl_set_substitute(common,
2679 type, pos, subs->p[j].aff);
2680 if (isl_set_plain_is_empty(common)) {
2681 isl_set_free(common);
2682 continue;
2685 res_ij = isl_multi_aff_substitute(
2686 isl_multi_aff_copy(pma->p[i].maff),
2687 type, pos, subs->p[j].aff);
2689 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2693 isl_pw_multi_aff_free(pma);
2694 return res;
2697 /* Extend the local space of "dst" to include the divs
2698 * in the local space of "src".
2700 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2701 __isl_keep isl_aff *src)
2703 isl_ctx *ctx;
2704 int *exp1 = NULL;
2705 int *exp2 = NULL;
2706 isl_mat *div;
2708 if (!src || !dst)
2709 return isl_aff_free(dst);
2711 ctx = isl_aff_get_ctx(src);
2712 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2713 isl_die(ctx, isl_error_invalid,
2714 "spaces don't match", goto error);
2716 if (src->ls->div->n_row == 0)
2717 return dst;
2719 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2720 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2721 if (!exp1 || !exp2)
2722 goto error;
2724 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2725 dst = isl_aff_expand_divs(dst, div, exp2);
2726 free(exp1);
2727 free(exp2);
2729 return dst;
2730 error:
2731 free(exp1);
2732 free(exp2);
2733 return isl_aff_free(dst);
2736 /* Adjust the local spaces of the affine expressions in "maff"
2737 * such that they all have the save divs.
2739 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2740 __isl_take isl_multi_aff *maff)
2742 int i;
2744 if (!maff)
2745 return NULL;
2746 if (maff->n == 0)
2747 return maff;
2748 maff = isl_multi_aff_cow(maff);
2749 if (!maff)
2750 return NULL;
2752 for (i = 1; i < maff->n; ++i)
2753 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2754 for (i = 1; i < maff->n; ++i) {
2755 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2756 if (!maff->p[i])
2757 return isl_multi_aff_free(maff);
2760 return maff;
2763 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2765 aff = isl_aff_cow(aff);
2766 if (!aff)
2767 return NULL;
2769 aff->ls = isl_local_space_lift(aff->ls);
2770 if (!aff->ls)
2771 return isl_aff_free(aff);
2773 return aff;
2776 /* Lift "maff" to a space with extra dimensions such that the result
2777 * has no more existentially quantified variables.
2778 * If "ls" is not NULL, then *ls is assigned the local space that lies
2779 * at the basis of the lifting applied to "maff".
2781 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2782 __isl_give isl_local_space **ls)
2784 int i;
2785 isl_space *space;
2786 unsigned n_div;
2788 if (ls)
2789 *ls = NULL;
2791 if (!maff)
2792 return NULL;
2794 if (maff->n == 0) {
2795 if (ls) {
2796 isl_space *space = isl_multi_aff_get_domain_space(maff);
2797 *ls = isl_local_space_from_space(space);
2798 if (!*ls)
2799 return isl_multi_aff_free(maff);
2801 return maff;
2804 maff = isl_multi_aff_cow(maff);
2805 maff = isl_multi_aff_align_divs(maff);
2806 if (!maff)
2807 return NULL;
2809 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2810 space = isl_multi_aff_get_space(maff);
2811 space = isl_space_lift(isl_space_domain(space), n_div);
2812 space = isl_space_extend_domain_with_range(space,
2813 isl_multi_aff_get_space(maff));
2814 if (!space)
2815 return isl_multi_aff_free(maff);
2816 isl_space_free(maff->space);
2817 maff->space = space;
2819 if (ls) {
2820 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2821 if (!*ls)
2822 return isl_multi_aff_free(maff);
2825 for (i = 0; i < maff->n; ++i) {
2826 maff->p[i] = isl_aff_lift(maff->p[i]);
2827 if (!maff->p[i])
2828 goto error;
2831 return maff;
2832 error:
2833 if (ls)
2834 isl_local_space_free(*ls);
2835 return isl_multi_aff_free(maff);
2839 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2841 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2842 __isl_keep isl_pw_multi_aff *pma, int pos)
2844 int i;
2845 int n_out;
2846 isl_space *space;
2847 isl_pw_aff *pa;
2849 if (!pma)
2850 return NULL;
2852 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2853 if (pos < 0 || pos >= n_out)
2854 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2855 "index out of bounds", return NULL);
2857 space = isl_pw_multi_aff_get_space(pma);
2858 space = isl_space_drop_dims(space, isl_dim_out,
2859 pos + 1, n_out - pos - 1);
2860 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2862 pa = isl_pw_aff_alloc_size(space, pma->n);
2863 for (i = 0; i < pma->n; ++i) {
2864 isl_aff *aff;
2865 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2866 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2869 return pa;