add isl_aff_align_params
[isl.git] / isl_aff.c
blobfd8d8ca3a7faf9363c7ca0e38fd1203e03d12039
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_space_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_mat_private.h>
18 #include <isl_list_private.h>
19 #include <isl/constraint.h>
20 #include <isl/seq.h>
21 #include <isl/set.h>
22 #include <isl_config.h>
24 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
25 __isl_take isl_vec *v)
27 isl_aff *aff;
29 if (!ls || !v)
30 goto error;
32 aff = isl_calloc_type(v->ctx, struct isl_aff);
33 if (!aff)
34 goto error;
36 aff->ref = 1;
37 aff->ls = ls;
38 aff->v = v;
40 return aff;
41 error:
42 isl_local_space_free(ls);
43 isl_vec_free(v);
44 return NULL;
47 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
49 isl_ctx *ctx;
50 isl_vec *v;
51 unsigned total;
53 if (!ls)
54 return NULL;
56 ctx = isl_local_space_get_ctx(ls);
57 if (!isl_local_space_divs_known(ls))
58 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
59 goto error);
60 if (!isl_local_space_is_set(ls))
61 isl_die(ctx, isl_error_invalid,
62 "domain of affine expression should be a set",
63 goto error);
65 total = isl_local_space_dim(ls, isl_dim_all);
66 v = isl_vec_alloc(ctx, 1 + 1 + total);
67 return isl_aff_alloc_vec(ls, v);
68 error:
69 isl_local_space_free(ls);
70 return NULL;
73 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
75 isl_aff *aff;
77 aff = isl_aff_alloc(ls);
78 if (!aff)
79 return NULL;
81 isl_int_set_si(aff->v->el[0], 1);
82 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
84 return aff;
87 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
89 if (!aff)
90 return NULL;
92 aff->ref++;
93 return aff;
96 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
98 if (!aff)
99 return NULL;
101 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
102 isl_vec_copy(aff->v));
105 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
107 if (!aff)
108 return NULL;
110 if (aff->ref == 1)
111 return aff;
112 aff->ref--;
113 return isl_aff_dup(aff);
116 void *isl_aff_free(__isl_take isl_aff *aff)
118 if (!aff)
119 return NULL;
121 if (--aff->ref > 0)
122 return NULL;
124 isl_local_space_free(aff->ls);
125 isl_vec_free(aff->v);
127 free(aff);
129 return NULL;
132 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
134 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
137 /* Externally, an isl_aff has a map space, but internally, the
138 * ls field corresponds to the domain of that space.
140 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
142 if (!aff)
143 return 0;
144 if (type == isl_dim_out)
145 return 1;
146 if (type == isl_dim_in)
147 type = isl_dim_set;
148 return isl_local_space_dim(aff->ls, type);
151 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
153 return aff ? isl_local_space_get_space(aff->ls) : NULL;
156 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
158 isl_space *space;
159 if (!aff)
160 return NULL;
161 space = isl_local_space_get_space(aff->ls);
162 space = isl_space_from_domain(space);
163 space = isl_space_add_dims(space, isl_dim_out, 1);
164 return space;
167 __isl_give isl_local_space *isl_aff_get_domain_local_space(
168 __isl_keep isl_aff *aff)
170 return aff ? isl_local_space_copy(aff->ls) : NULL;
173 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
175 isl_local_space *ls;
176 if (!aff)
177 return NULL;
178 ls = isl_local_space_copy(aff->ls);
179 ls = isl_local_space_from_domain(ls);
180 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
181 return ls;
184 /* Externally, an isl_aff has a map space, but internally, the
185 * ls field corresponds to the domain of that space.
187 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
188 enum isl_dim_type type, unsigned pos)
190 if (!aff)
191 return NULL;
192 if (type == isl_dim_out)
193 return NULL;
194 if (type == isl_dim_in)
195 type = isl_dim_set;
196 return isl_local_space_get_dim_name(aff->ls, type, pos);
199 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
200 __isl_take isl_space *dim)
202 aff = isl_aff_cow(aff);
203 if (!aff || !dim)
204 goto error;
206 aff->ls = isl_local_space_reset_space(aff->ls, dim);
207 if (!aff->ls)
208 return isl_aff_free(aff);
210 return aff;
211 error:
212 isl_aff_free(aff);
213 isl_space_free(dim);
214 return NULL;
217 /* Reset the space of "aff". This function is called from isl_pw_templ.c
218 * and doesn't know if the space of an element object is represented
219 * directly or through its domain. It therefore passes along both.
221 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
222 __isl_take isl_space *space, __isl_take isl_space *domain)
224 isl_space_free(space);
225 return isl_aff_reset_domain_space(aff, domain);
228 /* Reorder the coefficients of the affine expression based
229 * on the given reodering.
230 * The reordering r is assumed to have been extended with the local
231 * variables.
233 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
234 __isl_take isl_reordering *r, int n_div)
236 isl_vec *res;
237 int i;
239 if (!vec || !r)
240 goto error;
242 res = isl_vec_alloc(vec->ctx,
243 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
244 isl_seq_cpy(res->el, vec->el, 2);
245 isl_seq_clr(res->el + 2, res->size - 2);
246 for (i = 0; i < r->len; ++i)
247 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
249 isl_reordering_free(r);
250 isl_vec_free(vec);
251 return res;
252 error:
253 isl_vec_free(vec);
254 isl_reordering_free(r);
255 return NULL;
258 /* Reorder the dimensions of the domain of "aff" according
259 * to the given reordering.
261 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
262 __isl_take isl_reordering *r)
264 aff = isl_aff_cow(aff);
265 if (!aff)
266 goto error;
268 r = isl_reordering_extend(r, aff->ls->div->n_row);
269 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
270 aff->ls->div->n_row);
271 aff->ls = isl_local_space_realign(aff->ls, r);
273 if (!aff->v || !aff->ls)
274 return isl_aff_free(aff);
276 return aff;
277 error:
278 isl_aff_free(aff);
279 isl_reordering_free(r);
280 return NULL;
283 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
284 __isl_take isl_space *model)
286 if (!aff || !model)
287 goto error;
289 if (!isl_space_match(aff->ls->dim, isl_dim_param,
290 model, isl_dim_param)) {
291 isl_reordering *exp;
293 model = isl_space_drop_dims(model, isl_dim_in,
294 0, isl_space_dim(model, isl_dim_in));
295 model = isl_space_drop_dims(model, isl_dim_out,
296 0, isl_space_dim(model, isl_dim_out));
297 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
298 exp = isl_reordering_extend_space(exp,
299 isl_aff_get_domain_space(aff));
300 aff = isl_aff_realign_domain(aff, exp);
303 isl_space_free(model);
304 return aff;
305 error:
306 isl_space_free(model);
307 isl_aff_free(aff);
308 return NULL;
311 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
313 if (!aff)
314 return -1;
316 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
319 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
321 int equal;
323 if (!aff1 || !aff2)
324 return -1;
326 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
327 if (equal < 0 || !equal)
328 return equal;
330 return isl_vec_is_equal(aff1->v, aff2->v);
333 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
335 if (!aff)
336 return -1;
337 isl_int_set(*v, aff->v->el[0]);
338 return 0;
341 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
343 if (!aff)
344 return -1;
345 isl_int_set(*v, aff->v->el[1]);
346 return 0;
349 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
350 enum isl_dim_type type, int pos, isl_int *v)
352 if (!aff)
353 return -1;
355 if (type == isl_dim_out)
356 isl_die(aff->v->ctx, isl_error_invalid,
357 "output/set dimension does not have a coefficient",
358 return -1);
359 if (type == isl_dim_in)
360 type = isl_dim_set;
362 if (pos >= isl_local_space_dim(aff->ls, type))
363 isl_die(aff->v->ctx, isl_error_invalid,
364 "position out of bounds", return -1);
366 pos += isl_local_space_offset(aff->ls, type);
367 isl_int_set(*v, aff->v->el[1 + pos]);
369 return 0;
372 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
374 aff = isl_aff_cow(aff);
375 if (!aff)
376 return NULL;
378 aff->v = isl_vec_cow(aff->v);
379 if (!aff->v)
380 return isl_aff_free(aff);
382 isl_int_set(aff->v->el[0], v);
384 return aff;
387 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
389 aff = isl_aff_cow(aff);
390 if (!aff)
391 return NULL;
393 aff->v = isl_vec_cow(aff->v);
394 if (!aff->v)
395 return isl_aff_free(aff);
397 isl_int_set(aff->v->el[1], v);
399 return aff;
402 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
404 if (isl_int_is_zero(v))
405 return aff;
407 aff = isl_aff_cow(aff);
408 if (!aff)
409 return NULL;
411 aff->v = isl_vec_cow(aff->v);
412 if (!aff->v)
413 return isl_aff_free(aff);
415 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
417 return aff;
420 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
422 isl_int t;
424 isl_int_init(t);
425 isl_int_set_si(t, v);
426 aff = isl_aff_add_constant(aff, t);
427 isl_int_clear(t);
429 return aff;
432 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
434 aff = isl_aff_cow(aff);
435 if (!aff)
436 return NULL;
438 aff->v = isl_vec_cow(aff->v);
439 if (!aff->v)
440 return isl_aff_free(aff);
442 isl_int_set_si(aff->v->el[1], v);
444 return aff;
447 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
448 enum isl_dim_type type, int pos, isl_int v)
450 if (!aff)
451 return NULL;
453 if (type == isl_dim_out)
454 isl_die(aff->v->ctx, isl_error_invalid,
455 "output/set dimension does not have a coefficient",
456 return isl_aff_free(aff));
457 if (type == isl_dim_in)
458 type = isl_dim_set;
460 if (pos >= isl_local_space_dim(aff->ls, type))
461 isl_die(aff->v->ctx, isl_error_invalid,
462 "position out of bounds", return isl_aff_free(aff));
464 aff = isl_aff_cow(aff);
465 if (!aff)
466 return NULL;
468 aff->v = isl_vec_cow(aff->v);
469 if (!aff->v)
470 return isl_aff_free(aff);
472 pos += isl_local_space_offset(aff->ls, type);
473 isl_int_set(aff->v->el[1 + pos], v);
475 return aff;
478 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
479 enum isl_dim_type type, int pos, int v)
481 if (!aff)
482 return NULL;
484 if (type == isl_dim_out)
485 isl_die(aff->v->ctx, isl_error_invalid,
486 "output/set dimension does not have a coefficient",
487 return isl_aff_free(aff));
488 if (type == isl_dim_in)
489 type = isl_dim_set;
491 if (pos >= isl_local_space_dim(aff->ls, type))
492 isl_die(aff->v->ctx, isl_error_invalid,
493 "position out of bounds", return isl_aff_free(aff));
495 aff = isl_aff_cow(aff);
496 if (!aff)
497 return NULL;
499 aff->v = isl_vec_cow(aff->v);
500 if (!aff->v)
501 return isl_aff_free(aff);
503 pos += isl_local_space_offset(aff->ls, type);
504 isl_int_set_si(aff->v->el[1 + pos], v);
506 return aff;
509 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
510 enum isl_dim_type type, int pos, isl_int v)
512 if (!aff)
513 return NULL;
515 if (type == isl_dim_out)
516 isl_die(aff->v->ctx, isl_error_invalid,
517 "output/set dimension does not have a coefficient",
518 return isl_aff_free(aff));
519 if (type == isl_dim_in)
520 type = isl_dim_set;
522 if (pos >= isl_local_space_dim(aff->ls, type))
523 isl_die(aff->v->ctx, isl_error_invalid,
524 "position out of bounds", return isl_aff_free(aff));
526 aff = isl_aff_cow(aff);
527 if (!aff)
528 return NULL;
530 aff->v = isl_vec_cow(aff->v);
531 if (!aff->v)
532 return isl_aff_free(aff);
534 pos += isl_local_space_offset(aff->ls, type);
535 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
537 return aff;
540 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
541 enum isl_dim_type type, int pos, int v)
543 isl_int t;
545 isl_int_init(t);
546 isl_int_set_si(t, v);
547 aff = isl_aff_add_coefficient(aff, type, pos, t);
548 isl_int_clear(t);
550 return aff;
553 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
555 if (!aff)
556 return NULL;
558 return isl_local_space_get_div(aff->ls, pos);
561 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
563 aff = isl_aff_cow(aff);
564 if (!aff)
565 return NULL;
566 aff->v = isl_vec_cow(aff->v);
567 if (!aff->v)
568 return isl_aff_free(aff);
570 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
572 return aff;
575 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
577 if (!aff)
578 return NULL;
579 aff->v = isl_vec_normalize(aff->v);
580 if (!aff->v)
581 return isl_aff_free(aff);
582 return aff;
585 /* Given f, return floor(f).
586 * If f is an integer expression, then just return f.
587 * Otherwise, if f = g/m, write g = q m + r,
588 * create a new div d = [r/m] and return the expression q + d.
589 * The coefficients in r are taken to lie between -m/2 and m/2.
591 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
593 int i;
594 int size;
595 isl_ctx *ctx;
596 isl_vec *div;
598 if (!aff)
599 return NULL;
601 if (isl_int_is_one(aff->v->el[0]))
602 return aff;
604 aff = isl_aff_cow(aff);
605 if (!aff)
606 return NULL;
608 aff->v = isl_vec_cow(aff->v);
609 div = isl_vec_copy(aff->v);
610 div = isl_vec_cow(div);
611 if (!div)
612 return isl_aff_free(aff);
614 ctx = isl_aff_get_ctx(aff);
615 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
616 for (i = 1; i < aff->v->size; ++i) {
617 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
618 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
619 if (isl_int_gt(div->el[i], aff->v->el[0])) {
620 isl_int_sub(div->el[i], div->el[i], div->el[0]);
621 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
625 aff->ls = isl_local_space_add_div(aff->ls, div);
626 if (!aff->ls)
627 return isl_aff_free(aff);
629 size = aff->v->size;
630 aff->v = isl_vec_extend(aff->v, size + 1);
631 if (!aff->v)
632 return isl_aff_free(aff);
633 isl_int_set_si(aff->v->el[0], 1);
634 isl_int_set_si(aff->v->el[size], 1);
636 return aff;
639 /* Compute
641 * aff mod m = aff - m * floor(aff/m)
643 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
645 isl_aff *res;
647 res = isl_aff_copy(aff);
648 aff = isl_aff_scale_down(aff, m);
649 aff = isl_aff_floor(aff);
650 aff = isl_aff_scale(aff, m);
651 res = isl_aff_sub(res, aff);
653 return res;
656 /* Compute
658 * pwaff mod m = pwaff - m * floor(pwaff/m)
660 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
662 isl_pw_aff *res;
664 res = isl_pw_aff_copy(pwaff);
665 pwaff = isl_pw_aff_scale_down(pwaff, m);
666 pwaff = isl_pw_aff_floor(pwaff);
667 pwaff = isl_pw_aff_scale(pwaff, m);
668 res = isl_pw_aff_sub(res, pwaff);
670 return res;
673 /* Given f, return ceil(f).
674 * If f is an integer expression, then just return f.
675 * Otherwise, create a new div d = [-f] and return the expression -d.
677 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
679 if (!aff)
680 return NULL;
682 if (isl_int_is_one(aff->v->el[0]))
683 return aff;
685 aff = isl_aff_neg(aff);
686 aff = isl_aff_floor(aff);
687 aff = isl_aff_neg(aff);
689 return aff;
692 /* Apply the expansion computed by isl_merge_divs.
693 * The expansion itself is given by "exp" while the resulting
694 * list of divs is given by "div".
696 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
697 __isl_take isl_mat *div, int *exp)
699 int i, j;
700 int old_n_div;
701 int new_n_div;
702 int offset;
704 aff = isl_aff_cow(aff);
705 if (!aff || !div)
706 goto error;
708 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
709 new_n_div = isl_mat_rows(div);
710 if (new_n_div < old_n_div)
711 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
712 "not an expansion", goto error);
714 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
715 if (!aff->v)
716 goto error;
718 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
719 j = old_n_div - 1;
720 for (i = new_n_div - 1; i >= 0; --i) {
721 if (j >= 0 && exp[j] == i) {
722 if (i != j)
723 isl_int_swap(aff->v->el[offset + i],
724 aff->v->el[offset + j]);
725 j--;
726 } else
727 isl_int_set_si(aff->v->el[offset + i], 0);
730 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
731 if (!aff->ls)
732 goto error;
733 isl_mat_free(div);
734 return aff;
735 error:
736 isl_aff_free(aff);
737 isl_mat_free(div);
738 return NULL;
741 /* Add two affine expressions that live in the same local space.
743 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
744 __isl_take isl_aff *aff2)
746 isl_int gcd, f;
748 aff1 = isl_aff_cow(aff1);
749 if (!aff1 || !aff2)
750 goto error;
752 aff1->v = isl_vec_cow(aff1->v);
753 if (!aff1->v)
754 goto error;
756 isl_int_init(gcd);
757 isl_int_init(f);
758 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
759 isl_int_divexact(f, aff2->v->el[0], gcd);
760 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
761 isl_int_divexact(f, aff1->v->el[0], gcd);
762 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
763 isl_int_divexact(f, aff2->v->el[0], gcd);
764 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
765 isl_int_clear(f);
766 isl_int_clear(gcd);
768 isl_aff_free(aff2);
769 return aff1;
770 error:
771 isl_aff_free(aff1);
772 isl_aff_free(aff2);
773 return NULL;
776 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
777 __isl_take isl_aff *aff2)
779 isl_ctx *ctx;
780 int *exp1 = NULL;
781 int *exp2 = NULL;
782 isl_mat *div;
784 if (!aff1 || !aff2)
785 goto error;
787 ctx = isl_aff_get_ctx(aff1);
788 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
789 isl_die(ctx, isl_error_invalid,
790 "spaces don't match", goto error);
792 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
793 return add_expanded(aff1, aff2);
795 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
796 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
797 if (!exp1 || !exp2)
798 goto error;
800 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
801 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
802 aff2 = isl_aff_expand_divs(aff2, div, exp2);
803 free(exp1);
804 free(exp2);
806 return add_expanded(aff1, aff2);
807 error:
808 free(exp1);
809 free(exp2);
810 isl_aff_free(aff1);
811 isl_aff_free(aff2);
812 return NULL;
815 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
816 __isl_take isl_aff *aff2)
818 return isl_aff_add(aff1, isl_aff_neg(aff2));
821 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
823 isl_int gcd;
825 if (isl_int_is_one(f))
826 return aff;
828 aff = isl_aff_cow(aff);
829 if (!aff)
830 return NULL;
831 aff->v = isl_vec_cow(aff->v);
832 if (!aff->v)
833 return isl_aff_free(aff);
835 isl_int_init(gcd);
836 isl_int_gcd(gcd, aff->v->el[0], f);
837 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
838 isl_int_divexact(gcd, f, gcd);
839 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
840 isl_int_clear(gcd);
842 return aff;
845 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
847 isl_int gcd;
849 if (isl_int_is_one(f))
850 return aff;
852 aff = isl_aff_cow(aff);
853 if (!aff)
854 return NULL;
855 aff->v = isl_vec_cow(aff->v);
856 if (!aff->v)
857 return isl_aff_free(aff);
859 isl_int_init(gcd);
860 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
861 isl_int_gcd(gcd, gcd, f);
862 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
863 isl_int_divexact(gcd, f, gcd);
864 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
865 isl_int_clear(gcd);
867 return aff;
870 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
872 isl_int v;
874 if (f == 1)
875 return aff;
877 isl_int_init(v);
878 isl_int_set_ui(v, f);
879 aff = isl_aff_scale_down(aff, v);
880 isl_int_clear(v);
882 return aff;
885 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
886 enum isl_dim_type type, unsigned pos, const char *s)
888 aff = isl_aff_cow(aff);
889 if (!aff)
890 return NULL;
891 if (type == isl_dim_out)
892 isl_die(aff->v->ctx, isl_error_invalid,
893 "cannot set name of output/set dimension",
894 return isl_aff_free(aff));
895 if (type == isl_dim_in)
896 type = isl_dim_set;
897 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
898 if (!aff->ls)
899 return isl_aff_free(aff);
901 return aff;
904 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
905 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
907 aff = isl_aff_cow(aff);
908 if (!aff)
909 return isl_id_free(id);
910 if (type == isl_dim_out)
911 isl_die(aff->v->ctx, isl_error_invalid,
912 "cannot set name of output/set dimension",
913 goto error);
914 if (type == isl_dim_in)
915 type = isl_dim_set;
916 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
917 if (!aff->ls)
918 return isl_aff_free(aff);
920 return aff;
921 error:
922 isl_id_free(id);
923 isl_aff_free(aff);
924 return NULL;
927 /* Exploit the equalities in "eq" to simplify the affine expression
928 * and the expressions of the integer divisions in the local space.
929 * The integer divisions in this local space are assumed to appear
930 * as regular dimensions in "eq".
932 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
933 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
935 int i, j;
936 unsigned total;
937 unsigned n_div;
939 if (!eq)
940 goto error;
941 if (eq->n_eq == 0) {
942 isl_basic_set_free(eq);
943 return aff;
946 aff = isl_aff_cow(aff);
947 if (!aff)
948 goto error;
950 aff->ls = isl_local_space_substitute_equalities(aff->ls,
951 isl_basic_set_copy(eq));
952 if (!aff->ls)
953 goto error;
955 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
956 n_div = eq->n_div;
957 for (i = 0; i < eq->n_eq; ++i) {
958 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
959 if (j < 0 || j == 0 || j >= total)
960 continue;
962 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
963 &aff->v->el[0]);
966 isl_basic_set_free(eq);
967 aff = isl_aff_normalize(aff);
968 return aff;
969 error:
970 isl_basic_set_free(eq);
971 isl_aff_free(aff);
972 return NULL;
975 /* Exploit the equalities in "eq" to simplify the affine expression
976 * and the expressions of the integer divisions in the local space.
978 static __isl_give isl_aff *isl_aff_substitute_equalities(
979 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
981 int n_div;
983 if (!aff || !eq)
984 goto error;
985 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
986 if (n_div > 0)
987 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
988 return isl_aff_substitute_equalities_lifted(aff, eq);
989 error:
990 isl_basic_set_free(eq);
991 isl_aff_free(aff);
992 return NULL;
995 /* Look for equalities among the variables shared by context and aff
996 * and the integer divisions of aff, if any.
997 * The equalities are then used to eliminate coefficients and/or integer
998 * divisions from aff.
1000 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1001 __isl_take isl_set *context)
1003 isl_basic_set *hull;
1004 int n_div;
1006 if (!aff)
1007 goto error;
1008 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1009 if (n_div > 0) {
1010 isl_basic_set *bset;
1011 isl_local_space *ls;
1012 context = isl_set_add_dims(context, isl_dim_set, n_div);
1013 ls = isl_aff_get_domain_local_space(aff);
1014 bset = isl_basic_set_from_local_space(ls);
1015 bset = isl_basic_set_lift(bset);
1016 bset = isl_basic_set_flatten(bset);
1017 context = isl_set_intersect(context,
1018 isl_set_from_basic_set(bset));
1021 hull = isl_set_affine_hull(context);
1022 return isl_aff_substitute_equalities_lifted(aff, hull);
1023 error:
1024 isl_aff_free(aff);
1025 isl_set_free(context);
1026 return NULL;
1029 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1030 __isl_take isl_set *context)
1032 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1033 dom_context = isl_set_intersect_params(dom_context, context);
1034 return isl_aff_gist(aff, dom_context);
1037 /* Return a basic set containing those elements in the space
1038 * of aff where it is non-negative.
1040 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1042 isl_constraint *ineq;
1044 ineq = isl_inequality_from_aff(aff);
1046 return isl_basic_set_from_constraint(ineq);
1049 /* Return a basic set containing those elements in the space
1050 * of aff where it is zero.
1052 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1054 isl_constraint *ineq;
1056 ineq = isl_equality_from_aff(aff);
1058 return isl_basic_set_from_constraint(ineq);
1061 /* Return a basic set containing those elements in the shared space
1062 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1064 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1065 __isl_take isl_aff *aff2)
1067 aff1 = isl_aff_sub(aff1, aff2);
1069 return isl_aff_nonneg_basic_set(aff1);
1072 /* Return a basic set containing those elements in the shared space
1073 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1075 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1076 __isl_take isl_aff *aff2)
1078 return isl_aff_ge_basic_set(aff2, aff1);
1081 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1082 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1084 aff1 = isl_aff_add(aff1, aff2);
1085 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1086 return aff1;
1089 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1091 if (!aff)
1092 return -1;
1094 return 0;
1097 /* Check whether the given affine expression has non-zero coefficient
1098 * for any dimension in the given range or if any of these dimensions
1099 * appear with non-zero coefficients in any of the integer divisions
1100 * involved in the affine expression.
1102 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1103 enum isl_dim_type type, unsigned first, unsigned n)
1105 int i;
1106 isl_ctx *ctx;
1107 int *active = NULL;
1108 int involves = 0;
1110 if (!aff)
1111 return -1;
1112 if (n == 0)
1113 return 0;
1115 ctx = isl_aff_get_ctx(aff);
1116 if (first + n > isl_aff_dim(aff, type))
1117 isl_die(ctx, isl_error_invalid,
1118 "range out of bounds", return -1);
1120 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1121 if (!active)
1122 goto error;
1124 first += isl_local_space_offset(aff->ls, type) - 1;
1125 for (i = 0; i < n; ++i)
1126 if (active[first + i]) {
1127 involves = 1;
1128 break;
1131 free(active);
1133 return involves;
1134 error:
1135 free(active);
1136 return -1;
1139 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1140 enum isl_dim_type type, unsigned first, unsigned n)
1142 isl_ctx *ctx;
1144 if (!aff)
1145 return NULL;
1146 if (type == isl_dim_out)
1147 isl_die(aff->v->ctx, isl_error_invalid,
1148 "cannot drop output/set dimension",
1149 return isl_aff_free(aff));
1150 if (type == isl_dim_in)
1151 type = isl_dim_set;
1152 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1153 return aff;
1155 ctx = isl_aff_get_ctx(aff);
1156 if (first + n > isl_local_space_dim(aff->ls, type))
1157 isl_die(ctx, isl_error_invalid, "range out of bounds",
1158 return isl_aff_free(aff));
1160 aff = isl_aff_cow(aff);
1161 if (!aff)
1162 return NULL;
1164 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1165 if (!aff->ls)
1166 return isl_aff_free(aff);
1168 first += 1 + isl_local_space_offset(aff->ls, type);
1169 aff->v = isl_vec_drop_els(aff->v, first, n);
1170 if (!aff->v)
1171 return isl_aff_free(aff);
1173 return aff;
1176 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1177 enum isl_dim_type type, unsigned first, unsigned n)
1179 isl_ctx *ctx;
1181 if (!aff)
1182 return NULL;
1183 if (type == isl_dim_out)
1184 isl_die(aff->v->ctx, isl_error_invalid,
1185 "cannot insert output/set dimensions",
1186 return isl_aff_free(aff));
1187 if (type == isl_dim_in)
1188 type = isl_dim_set;
1189 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1190 return aff;
1192 ctx = isl_aff_get_ctx(aff);
1193 if (first > isl_local_space_dim(aff->ls, type))
1194 isl_die(ctx, isl_error_invalid, "position out of bounds",
1195 return isl_aff_free(aff));
1197 aff = isl_aff_cow(aff);
1198 if (!aff)
1199 return NULL;
1201 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1202 if (!aff->ls)
1203 return isl_aff_free(aff);
1205 first += 1 + isl_local_space_offset(aff->ls, type);
1206 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1207 if (!aff->v)
1208 return isl_aff_free(aff);
1210 return aff;
1213 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1214 enum isl_dim_type type, unsigned n)
1216 unsigned pos;
1218 pos = isl_aff_dim(aff, type);
1220 return isl_aff_insert_dims(aff, type, pos, n);
1223 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1224 enum isl_dim_type type, unsigned n)
1226 unsigned pos;
1228 pos = isl_pw_aff_dim(pwaff, type);
1230 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1233 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1235 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1236 return isl_pw_aff_alloc(dom, aff);
1239 #undef PW
1240 #define PW isl_pw_aff
1241 #undef EL
1242 #define EL isl_aff
1243 #undef EL_IS_ZERO
1244 #define EL_IS_ZERO is_empty
1245 #undef ZERO
1246 #define ZERO empty
1247 #undef IS_ZERO
1248 #define IS_ZERO is_empty
1249 #undef FIELD
1250 #define FIELD aff
1251 #undef DEFAULT_IS_ZERO
1252 #define DEFAULT_IS_ZERO 0
1254 #define NO_EVAL
1255 #define NO_OPT
1256 #define NO_MOVE_DIMS
1257 #define NO_LIFT
1258 #define NO_MORPH
1260 #include <isl_pw_templ.c>
1262 static __isl_give isl_set *align_params_pw_pw_set_and(
1263 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1264 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1265 __isl_take isl_pw_aff *pwaff2))
1267 if (!pwaff1 || !pwaff2)
1268 goto error;
1269 if (isl_space_match(pwaff1->dim, isl_dim_param,
1270 pwaff2->dim, isl_dim_param))
1271 return fn(pwaff1, pwaff2);
1272 if (!isl_space_has_named_params(pwaff1->dim) ||
1273 !isl_space_has_named_params(pwaff2->dim))
1274 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1275 "unaligned unnamed parameters", goto error);
1276 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1277 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1278 return fn(pwaff1, pwaff2);
1279 error:
1280 isl_pw_aff_free(pwaff1);
1281 isl_pw_aff_free(pwaff2);
1282 return NULL;
1285 /* Compute a piecewise quasi-affine expression with a domain that
1286 * is the union of those of pwaff1 and pwaff2 and such that on each
1287 * cell, the quasi-affine expression is the better (according to cmp)
1288 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1289 * is defined on a given cell, then the associated expression
1290 * is the defined one.
1292 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1293 __isl_take isl_pw_aff *pwaff2,
1294 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1295 __isl_take isl_aff *aff2))
1297 int i, j, n;
1298 isl_pw_aff *res;
1299 isl_ctx *ctx;
1300 isl_set *set;
1302 if (!pwaff1 || !pwaff2)
1303 goto error;
1305 ctx = isl_space_get_ctx(pwaff1->dim);
1306 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1307 isl_die(ctx, isl_error_invalid,
1308 "arguments should live in same space", goto error);
1310 if (isl_pw_aff_is_empty(pwaff1)) {
1311 isl_pw_aff_free(pwaff1);
1312 return pwaff2;
1315 if (isl_pw_aff_is_empty(pwaff2)) {
1316 isl_pw_aff_free(pwaff2);
1317 return pwaff1;
1320 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1321 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1323 for (i = 0; i < pwaff1->n; ++i) {
1324 set = isl_set_copy(pwaff1->p[i].set);
1325 for (j = 0; j < pwaff2->n; ++j) {
1326 struct isl_set *common;
1327 isl_set *better;
1329 common = isl_set_intersect(
1330 isl_set_copy(pwaff1->p[i].set),
1331 isl_set_copy(pwaff2->p[j].set));
1332 better = isl_set_from_basic_set(cmp(
1333 isl_aff_copy(pwaff2->p[j].aff),
1334 isl_aff_copy(pwaff1->p[i].aff)));
1335 better = isl_set_intersect(common, better);
1336 if (isl_set_plain_is_empty(better)) {
1337 isl_set_free(better);
1338 continue;
1340 set = isl_set_subtract(set, isl_set_copy(better));
1342 res = isl_pw_aff_add_piece(res, better,
1343 isl_aff_copy(pwaff2->p[j].aff));
1345 res = isl_pw_aff_add_piece(res, set,
1346 isl_aff_copy(pwaff1->p[i].aff));
1349 for (j = 0; j < pwaff2->n; ++j) {
1350 set = isl_set_copy(pwaff2->p[j].set);
1351 for (i = 0; i < pwaff1->n; ++i)
1352 set = isl_set_subtract(set,
1353 isl_set_copy(pwaff1->p[i].set));
1354 res = isl_pw_aff_add_piece(res, set,
1355 isl_aff_copy(pwaff2->p[j].aff));
1358 isl_pw_aff_free(pwaff1);
1359 isl_pw_aff_free(pwaff2);
1361 return res;
1362 error:
1363 isl_pw_aff_free(pwaff1);
1364 isl_pw_aff_free(pwaff2);
1365 return NULL;
1368 /* Compute a piecewise quasi-affine expression with a domain that
1369 * is the union of those of pwaff1 and pwaff2 and such that on each
1370 * cell, the quasi-affine expression is the maximum of those of pwaff1
1371 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1372 * cell, then the associated expression is the defined one.
1374 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1375 __isl_take isl_pw_aff *pwaff2)
1377 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1380 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1381 __isl_take isl_pw_aff *pwaff2)
1383 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1384 &pw_aff_union_max);
1387 /* Compute a piecewise quasi-affine expression with a domain that
1388 * is the union of those of pwaff1 and pwaff2 and such that on each
1389 * cell, the quasi-affine expression is the minimum of those of pwaff1
1390 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1391 * cell, then the associated expression is the defined one.
1393 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1394 __isl_take isl_pw_aff *pwaff2)
1396 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1399 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1400 __isl_take isl_pw_aff *pwaff2)
1402 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1403 &pw_aff_union_min);
1406 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1407 __isl_take isl_pw_aff *pwaff2, int max)
1409 if (max)
1410 return isl_pw_aff_union_max(pwaff1, pwaff2);
1411 else
1412 return isl_pw_aff_union_min(pwaff1, pwaff2);
1415 /* Construct a map with as domain the domain of pwaff and
1416 * one-dimensional range corresponding to the affine expressions.
1418 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1420 int i;
1421 isl_space *dim;
1422 isl_map *map;
1424 if (!pwaff)
1425 return NULL;
1427 dim = isl_pw_aff_get_space(pwaff);
1428 map = isl_map_empty(dim);
1430 for (i = 0; i < pwaff->n; ++i) {
1431 isl_basic_map *bmap;
1432 isl_map *map_i;
1434 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1435 map_i = isl_map_from_basic_map(bmap);
1436 map_i = isl_map_intersect_domain(map_i,
1437 isl_set_copy(pwaff->p[i].set));
1438 map = isl_map_union_disjoint(map, map_i);
1441 isl_pw_aff_free(pwaff);
1443 return map;
1446 /* Construct a map with as domain the domain of pwaff and
1447 * one-dimensional range corresponding to the affine expressions.
1449 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1451 if (!pwaff)
1452 return NULL;
1453 if (isl_space_is_set(pwaff->dim))
1454 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1455 "space of input is not a map",
1456 return isl_pw_aff_free(pwaff));
1457 return map_from_pw_aff(pwaff);
1460 /* Construct a one-dimensional set with as parameter domain
1461 * the domain of pwaff and the single set dimension
1462 * corresponding to the affine expressions.
1464 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1466 if (!pwaff)
1467 return NULL;
1468 if (!isl_space_is_set(pwaff->dim))
1469 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1470 "space of input is not a set",
1471 return isl_pw_aff_free(pwaff));
1472 return map_from_pw_aff(pwaff);
1475 /* Return a set containing those elements in the domain
1476 * of pwaff where it is non-negative.
1478 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1480 int i;
1481 isl_set *set;
1483 if (!pwaff)
1484 return NULL;
1486 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1488 for (i = 0; i < pwaff->n; ++i) {
1489 isl_basic_set *bset;
1490 isl_set *set_i;
1492 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1493 set_i = isl_set_from_basic_set(bset);
1494 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1495 set = isl_set_union_disjoint(set, set_i);
1498 isl_pw_aff_free(pwaff);
1500 return set;
1503 /* Return a set containing those elements in the domain
1504 * of pwaff where it is zero.
1506 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1508 int i;
1509 isl_set *set;
1511 if (!pwaff)
1512 return NULL;
1514 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1516 for (i = 0; i < pwaff->n; ++i) {
1517 isl_basic_set *bset;
1518 isl_set *set_i;
1520 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1521 set_i = isl_set_from_basic_set(bset);
1522 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1523 set = isl_set_union_disjoint(set, set_i);
1526 isl_pw_aff_free(pwaff);
1528 return set;
1531 /* Return a set containing those elements in the domain
1532 * of pwaff where it is not zero.
1534 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1536 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1539 /* Return a set containing those elements in the shared domain
1540 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1542 * We compute the difference on the shared domain and then construct
1543 * the set of values where this difference is non-negative.
1544 * If strict is set, we first subtract 1 from the difference.
1545 * If equal is set, we only return the elements where pwaff1 and pwaff2
1546 * are equal.
1548 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1549 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1551 isl_set *set1, *set2;
1553 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1554 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1555 set1 = isl_set_intersect(set1, set2);
1556 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1557 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1558 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1560 if (strict) {
1561 isl_space *dim = isl_set_get_space(set1);
1562 isl_aff *aff;
1563 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1564 aff = isl_aff_add_constant_si(aff, -1);
1565 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1566 } else
1567 isl_set_free(set1);
1569 if (equal)
1570 return isl_pw_aff_zero_set(pwaff1);
1571 return isl_pw_aff_nonneg_set(pwaff1);
1574 /* Return a set containing those elements in the shared domain
1575 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1577 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1578 __isl_take isl_pw_aff *pwaff2)
1580 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1583 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1584 __isl_take isl_pw_aff *pwaff2)
1586 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1589 /* Return a set containing those elements in the shared domain
1590 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1592 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1593 __isl_take isl_pw_aff *pwaff2)
1595 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1598 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1599 __isl_take isl_pw_aff *pwaff2)
1601 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1604 /* Return a set containing those elements in the shared domain
1605 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1607 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1608 __isl_take isl_pw_aff *pwaff2)
1610 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1613 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1614 __isl_take isl_pw_aff *pwaff2)
1616 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1619 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1620 __isl_take isl_pw_aff *pwaff2)
1622 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1625 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1626 __isl_take isl_pw_aff *pwaff2)
1628 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1631 /* Return a set containing those elements in the shared domain
1632 * of the elements of list1 and list2 where each element in list1
1633 * has the relation specified by "fn" with each element in list2.
1635 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1636 __isl_take isl_pw_aff_list *list2,
1637 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1638 __isl_take isl_pw_aff *pwaff2))
1640 int i, j;
1641 isl_ctx *ctx;
1642 isl_set *set;
1644 if (!list1 || !list2)
1645 goto error;
1647 ctx = isl_pw_aff_list_get_ctx(list1);
1648 if (list1->n < 1 || list2->n < 1)
1649 isl_die(ctx, isl_error_invalid,
1650 "list should contain at least one element", goto error);
1652 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1653 for (i = 0; i < list1->n; ++i)
1654 for (j = 0; j < list2->n; ++j) {
1655 isl_set *set_ij;
1657 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1658 isl_pw_aff_copy(list2->p[j]));
1659 set = isl_set_intersect(set, set_ij);
1662 isl_pw_aff_list_free(list1);
1663 isl_pw_aff_list_free(list2);
1664 return set;
1665 error:
1666 isl_pw_aff_list_free(list1);
1667 isl_pw_aff_list_free(list2);
1668 return NULL;
1671 /* Return a set containing those elements in the shared domain
1672 * of the elements of list1 and list2 where each element in list1
1673 * is equal to each element in list2.
1675 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1676 __isl_take isl_pw_aff_list *list2)
1678 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1681 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1682 __isl_take isl_pw_aff_list *list2)
1684 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1687 /* Return a set containing those elements in the shared domain
1688 * of the elements of list1 and list2 where each element in list1
1689 * is less than or equal to each element in list2.
1691 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1692 __isl_take isl_pw_aff_list *list2)
1694 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1697 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1698 __isl_take isl_pw_aff_list *list2)
1700 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1703 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1704 __isl_take isl_pw_aff_list *list2)
1706 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1709 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1710 __isl_take isl_pw_aff_list *list2)
1712 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1716 /* Return a set containing those elements in the shared domain
1717 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1719 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1720 __isl_take isl_pw_aff *pwaff2)
1722 isl_set *set_lt, *set_gt;
1724 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1725 isl_pw_aff_copy(pwaff2));
1726 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1727 return isl_set_union_disjoint(set_lt, set_gt);
1730 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1731 __isl_take isl_pw_aff *pwaff2)
1733 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1736 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1737 isl_int v)
1739 int i;
1741 if (isl_int_is_one(v))
1742 return pwaff;
1743 if (!isl_int_is_pos(v))
1744 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1745 "factor needs to be positive",
1746 return isl_pw_aff_free(pwaff));
1747 pwaff = isl_pw_aff_cow(pwaff);
1748 if (!pwaff)
1749 return NULL;
1750 if (pwaff->n == 0)
1751 return pwaff;
1753 for (i = 0; i < pwaff->n; ++i) {
1754 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1755 if (!pwaff->p[i].aff)
1756 return isl_pw_aff_free(pwaff);
1759 return pwaff;
1762 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1764 int i;
1766 pwaff = isl_pw_aff_cow(pwaff);
1767 if (!pwaff)
1768 return NULL;
1769 if (pwaff->n == 0)
1770 return pwaff;
1772 for (i = 0; i < pwaff->n; ++i) {
1773 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1774 if (!pwaff->p[i].aff)
1775 return isl_pw_aff_free(pwaff);
1778 return pwaff;
1781 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1783 int i;
1785 pwaff = isl_pw_aff_cow(pwaff);
1786 if (!pwaff)
1787 return NULL;
1788 if (pwaff->n == 0)
1789 return pwaff;
1791 for (i = 0; i < pwaff->n; ++i) {
1792 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1793 if (!pwaff->p[i].aff)
1794 return isl_pw_aff_free(pwaff);
1797 return pwaff;
1800 /* Return an affine expression that is equal to pwaff_true for elements
1801 * in "cond" and to pwaff_false for elements not in "cond".
1802 * That is, return cond ? pwaff_true : pwaff_false;
1804 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1805 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1807 isl_set *comp;
1809 comp = isl_set_complement(isl_set_copy(cond));
1810 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1811 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1813 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1816 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1818 if (!aff)
1819 return -1;
1821 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1824 /* Check whether pwaff is a piecewise constant.
1826 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1828 int i;
1830 if (!pwaff)
1831 return -1;
1833 for (i = 0; i < pwaff->n; ++i) {
1834 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1835 if (is_cst < 0 || !is_cst)
1836 return is_cst;
1839 return 1;
1842 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1843 __isl_take isl_aff *aff2)
1845 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1846 return isl_aff_mul(aff2, aff1);
1848 if (!isl_aff_is_cst(aff2))
1849 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1850 "at least one affine expression should be constant",
1851 goto error);
1853 aff1 = isl_aff_cow(aff1);
1854 if (!aff1 || !aff2)
1855 goto error;
1857 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1858 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1860 isl_aff_free(aff2);
1861 return aff1;
1862 error:
1863 isl_aff_free(aff1);
1864 isl_aff_free(aff2);
1865 return NULL;
1868 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1869 __isl_take isl_pw_aff *pwaff2)
1871 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1874 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1875 __isl_take isl_pw_aff *pwaff2)
1877 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1880 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1881 __isl_take isl_pw_aff *pwaff2)
1883 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1886 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1887 __isl_take isl_pw_aff *pwaff2)
1889 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1892 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1893 __isl_take isl_pw_aff *pwaff2)
1895 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1898 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1899 __isl_take isl_pw_aff *pwaff2)
1901 isl_set *le;
1903 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1904 isl_pw_aff_copy(pwaff2));
1905 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1908 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1909 __isl_take isl_pw_aff *pwaff2)
1911 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1914 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1915 __isl_take isl_pw_aff *pwaff2)
1917 isl_set *le;
1919 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1920 isl_pw_aff_copy(pwaff2));
1921 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1924 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1925 __isl_take isl_pw_aff *pwaff2)
1927 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1930 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1931 __isl_take isl_pw_aff_list *list,
1932 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1933 __isl_take isl_pw_aff *pwaff2))
1935 int i;
1936 isl_ctx *ctx;
1937 isl_pw_aff *res;
1939 if (!list)
1940 return NULL;
1942 ctx = isl_pw_aff_list_get_ctx(list);
1943 if (list->n < 1)
1944 isl_die(ctx, isl_error_invalid,
1945 "list should contain at least one element",
1946 return isl_pw_aff_list_free(list));
1948 res = isl_pw_aff_copy(list->p[0]);
1949 for (i = 1; i < list->n; ++i)
1950 res = fn(res, isl_pw_aff_copy(list->p[i]));
1952 isl_pw_aff_list_free(list);
1953 return res;
1956 /* Return an isl_pw_aff that maps each element in the intersection of the
1957 * domains of the elements of list to the minimal corresponding affine
1958 * expression.
1960 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1962 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1965 /* Return an isl_pw_aff that maps each element in the intersection of the
1966 * domains of the elements of list to the maximal corresponding affine
1967 * expression.
1969 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1971 return pw_aff_list_reduce(list, &isl_pw_aff_max);
1974 #undef BASE
1975 #define BASE aff
1977 #include <isl_multi_templ.c>
1979 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
1980 __isl_take isl_multi_aff *maff2)
1982 int i;
1983 isl_ctx *ctx;
1985 maff1 = isl_multi_aff_cow(maff1);
1986 if (!maff1 || !maff2)
1987 goto error;
1989 ctx = isl_multi_aff_get_ctx(maff1);
1990 if (!isl_space_is_equal(maff1->space, maff2->space))
1991 isl_die(ctx, isl_error_invalid,
1992 "spaces don't match", goto error);
1994 for (i = 0; i < maff1->n; ++i) {
1995 maff1->p[i] = isl_aff_add(maff1->p[i],
1996 isl_aff_copy(maff2->p[i]));
1997 if (!maff1->p[i])
1998 goto error;
2001 isl_multi_aff_free(maff2);
2002 return maff1;
2003 error:
2004 isl_multi_aff_free(maff1);
2005 isl_multi_aff_free(maff2);
2006 return NULL;
2009 /* Exploit the equalities in "eq" to simplify the affine expressions.
2011 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2012 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2014 int i;
2016 maff = isl_multi_aff_cow(maff);
2017 if (!maff || !eq)
2018 goto error;
2020 for (i = 0; i < maff->n; ++i) {
2021 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2022 isl_basic_set_copy(eq));
2023 if (!maff->p[i])
2024 goto error;
2027 isl_basic_set_free(eq);
2028 return maff;
2029 error:
2030 isl_basic_set_free(eq);
2031 isl_multi_aff_free(maff);
2032 return NULL;
2035 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2036 isl_int f)
2038 int i;
2040 maff = isl_multi_aff_cow(maff);
2041 if (!maff)
2042 return NULL;
2044 for (i = 0; i < maff->n; ++i) {
2045 maff->p[i] = isl_aff_scale(maff->p[i], f);
2046 if (!maff->p[i])
2047 return isl_multi_aff_free(maff);
2050 return maff;
2053 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2054 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2056 maff1 = isl_multi_aff_add(maff1, maff2);
2057 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2058 return maff1;
2061 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2063 if (!maff)
2064 return -1;
2066 return 0;
2069 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2070 __isl_keep isl_multi_aff *maff2)
2072 int i;
2073 int equal;
2075 if (!maff1 || !maff2)
2076 return -1;
2077 if (maff1->n != maff2->n)
2078 return 0;
2079 equal = isl_space_is_equal(maff1->space, maff2->space);
2080 if (equal < 0 || !equal)
2081 return equal;
2083 for (i = 0; i < maff1->n; ++i) {
2084 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2085 if (equal < 0 || !equal)
2086 return equal;
2089 return 1;
2092 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2093 __isl_take isl_multi_aff *maff,
2094 enum isl_dim_type type, unsigned pos, const char *s)
2096 int i;
2098 maff = isl_multi_aff_cow(maff);
2099 if (!maff)
2100 return NULL;
2102 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2103 if (!maff->space)
2104 return isl_multi_aff_free(maff);
2105 for (i = 0; i < maff->n; ++i) {
2106 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2107 if (!maff->p[i])
2108 return isl_multi_aff_free(maff);
2111 return maff;
2114 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2115 enum isl_dim_type type, unsigned first, unsigned n)
2117 int i;
2119 maff = isl_multi_aff_cow(maff);
2120 if (!maff)
2121 return NULL;
2123 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2124 if (!maff->space)
2125 return isl_multi_aff_free(maff);
2126 for (i = 0; i < maff->n; ++i) {
2127 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2128 if (!maff->p[i])
2129 return isl_multi_aff_free(maff);
2132 return maff;
2135 #undef PW
2136 #define PW isl_pw_multi_aff
2137 #undef EL
2138 #define EL isl_multi_aff
2139 #undef EL_IS_ZERO
2140 #define EL_IS_ZERO is_empty
2141 #undef ZERO
2142 #define ZERO empty
2143 #undef IS_ZERO
2144 #define IS_ZERO is_empty
2145 #undef FIELD
2146 #define FIELD maff
2147 #undef DEFAULT_IS_ZERO
2148 #define DEFAULT_IS_ZERO 0
2150 #define NO_NEG
2151 #define NO_EVAL
2152 #define NO_OPT
2153 #define NO_INVOLVES_DIMS
2154 #define NO_MOVE_DIMS
2155 #define NO_INSERT_DIMS
2156 #define NO_LIFT
2157 #define NO_MORPH
2159 #include <isl_pw_templ.c>
2161 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2162 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2164 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2165 &isl_multi_aff_add);
2168 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2169 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2171 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2172 &pw_multi_aff_add);
2175 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2176 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2178 return isl_pw_multi_aff_union_add_(pma1, pma2);
2181 /* Construct a map mapping the domain the piecewise multi-affine expression
2182 * to its range, with each dimension in the range equated to the
2183 * corresponding affine expression on its cell.
2185 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2187 int i;
2188 isl_map *map;
2190 if (!pma)
2191 return NULL;
2193 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2195 for (i = 0; i < pma->n; ++i) {
2196 isl_multi_aff *maff;
2197 isl_basic_map *bmap;
2198 isl_map *map_i;
2200 maff = isl_multi_aff_copy(pma->p[i].maff);
2201 bmap = isl_basic_map_from_multi_aff(maff);
2202 map_i = isl_map_from_basic_map(bmap);
2203 map_i = isl_map_intersect_domain(map_i,
2204 isl_set_copy(pma->p[i].set));
2205 map = isl_map_union_disjoint(map, map_i);
2208 isl_pw_multi_aff_free(pma);
2209 return map;
2212 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2214 if (!isl_space_is_set(pma->dim))
2215 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2216 "isl_pw_multi_aff cannot be converted into an isl_set",
2217 return isl_pw_multi_aff_free(pma));
2219 return isl_map_from_pw_multi_aff(pma);
2222 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2223 * This obivously only works if the input "map" is single-valued.
2224 * If so, we compute the lexicographic minimum of the image in the form
2225 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2226 * to its lexicographic minimum.
2227 * If the input is not single-valued, we produce an error.
2229 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2231 int i;
2232 int sv;
2233 isl_pw_multi_aff *pma;
2235 if (!map)
2236 return NULL;
2238 sv = isl_map_is_single_valued(map);
2239 if (sv < 0)
2240 goto error;
2241 if (!sv)
2242 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2243 "map is not single-valued", goto error);
2244 map = isl_map_make_disjoint(map);
2245 if (!map)
2246 return NULL;
2248 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2250 for (i = 0; i < map->n; ++i) {
2251 isl_pw_multi_aff *pma_i;
2252 isl_basic_map *bmap;
2253 bmap = isl_basic_map_copy(map->p[i]);
2254 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2255 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2258 isl_map_free(map);
2259 return pma;
2260 error:
2261 isl_map_free(map);
2262 return NULL;
2265 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2267 return isl_pw_multi_aff_from_map(set);
2270 /* Plug in "subs" for dimension "type", "pos" of "aff".
2272 * Let i be the dimension to replace and let "subs" be of the form
2274 * f/d
2276 * and "aff" of the form
2278 * (a i + g)/m
2280 * The result is
2282 * floor((a f + d g')/(m d))
2284 * where g' is the result of plugging in "subs" in each of the integer
2285 * divisions in g.
2287 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2288 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2290 isl_ctx *ctx;
2291 isl_int v;
2293 aff = isl_aff_cow(aff);
2294 if (!aff || !subs)
2295 return isl_aff_free(aff);
2297 ctx = isl_aff_get_ctx(aff);
2298 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2299 isl_die(ctx, isl_error_invalid,
2300 "spaces don't match", return isl_aff_free(aff));
2301 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2302 isl_die(ctx, isl_error_unsupported,
2303 "cannot handle divs yet", return isl_aff_free(aff));
2305 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2306 if (!aff->ls)
2307 return isl_aff_free(aff);
2309 aff->v = isl_vec_cow(aff->v);
2310 if (!aff->v)
2311 return isl_aff_free(aff);
2313 pos += isl_local_space_offset(aff->ls, type);
2315 isl_int_init(v);
2316 isl_int_set(v, aff->v->el[1 + pos]);
2317 isl_int_set_si(aff->v->el[1 + pos], 0);
2318 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2319 v, subs->v->el + 1, subs->v->size - 1);
2320 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2321 isl_int_clear(v);
2323 return aff;
2326 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2327 * expressions in "maff".
2329 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2330 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2331 __isl_keep isl_aff *subs)
2333 int i;
2335 maff = isl_multi_aff_cow(maff);
2336 if (!maff || !subs)
2337 return isl_multi_aff_free(maff);
2339 if (type == isl_dim_in)
2340 type = isl_dim_set;
2342 for (i = 0; i < maff->n; ++i) {
2343 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2344 if (!maff->p[i])
2345 return isl_multi_aff_free(maff);
2348 return maff;
2351 /* Plug in "subs" for dimension "type", "pos" of "pma".
2353 * pma is of the form
2355 * A_i(v) -> M_i(v)
2357 * while subs is of the form
2359 * v' = B_j(v) -> S_j
2361 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2362 * has a contribution in the result, in particular
2364 * C_ij(S_j) -> M_i(S_j)
2366 * Note that plugging in S_j in C_ij may also result in an empty set
2367 * and this contribution should simply be discarded.
2369 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2370 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2371 __isl_keep isl_pw_aff *subs)
2373 int i, j, n;
2374 isl_pw_multi_aff *res;
2376 if (!pma || !subs)
2377 return isl_pw_multi_aff_free(pma);
2379 n = pma->n * subs->n;
2380 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2382 for (i = 0; i < pma->n; ++i) {
2383 for (j = 0; j < subs->n; ++j) {
2384 isl_set *common;
2385 isl_multi_aff *res_ij;
2386 common = isl_set_intersect(
2387 isl_set_copy(pma->p[i].set),
2388 isl_set_copy(subs->p[j].set));
2389 common = isl_set_substitute(common,
2390 type, pos, subs->p[j].aff);
2391 if (isl_set_plain_is_empty(common)) {
2392 isl_set_free(common);
2393 continue;
2396 res_ij = isl_multi_aff_substitute(
2397 isl_multi_aff_copy(pma->p[i].maff),
2398 type, pos, subs->p[j].aff);
2400 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2404 isl_pw_multi_aff_free(pma);
2405 return res;
2408 /* Extend the local space of "dst" to include the divs
2409 * in the local space of "src".
2411 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2412 __isl_keep isl_aff *src)
2414 isl_ctx *ctx;
2415 int *exp1 = NULL;
2416 int *exp2 = NULL;
2417 isl_mat *div;
2419 if (!src || !dst)
2420 return isl_aff_free(dst);
2422 ctx = isl_aff_get_ctx(src);
2423 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2424 isl_die(ctx, isl_error_invalid,
2425 "spaces don't match", goto error);
2427 if (src->ls->div->n_row == 0)
2428 return dst;
2430 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2431 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2432 if (!exp1 || !exp2)
2433 goto error;
2435 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2436 dst = isl_aff_expand_divs(dst, div, exp2);
2437 free(exp1);
2438 free(exp2);
2440 return dst;
2441 error:
2442 free(exp1);
2443 free(exp2);
2444 return isl_aff_free(dst);
2447 /* Adjust the local spaces of the affine expressions in "maff"
2448 * such that they all have the save divs.
2450 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2451 __isl_take isl_multi_aff *maff)
2453 int i;
2455 if (!maff)
2456 return NULL;
2457 if (maff->n == 0)
2458 return maff;
2459 maff = isl_multi_aff_cow(maff);
2460 if (!maff)
2461 return NULL;
2463 for (i = 1; i < maff->n; ++i)
2464 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2465 for (i = 1; i < maff->n; ++i) {
2466 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2467 if (!maff->p[i])
2468 return isl_multi_aff_free(maff);
2471 return maff;
2474 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2476 aff = isl_aff_cow(aff);
2477 if (!aff)
2478 return NULL;
2480 aff->ls = isl_local_space_lift(aff->ls);
2481 if (!aff->ls)
2482 return isl_aff_free(aff);
2484 return aff;
2487 /* Lift "maff" to a space with extra dimensions such that the result
2488 * has no more existentially quantified variables.
2489 * If "ls" is not NULL, then *ls is assigned the local space that lies
2490 * at the basis of the lifting applied to "maff".
2492 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2493 __isl_give isl_local_space **ls)
2495 int i;
2496 isl_space *space;
2497 unsigned n_div;
2499 if (ls)
2500 *ls = NULL;
2502 if (!maff)
2503 return NULL;
2505 if (maff->n == 0) {
2506 if (ls) {
2507 isl_space *space = isl_multi_aff_get_domain_space(maff);
2508 *ls = isl_local_space_from_space(space);
2509 if (!*ls)
2510 return isl_multi_aff_free(maff);
2512 return maff;
2515 maff = isl_multi_aff_cow(maff);
2516 maff = isl_multi_aff_align_divs(maff);
2517 if (!maff)
2518 return NULL;
2520 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2521 space = isl_multi_aff_get_space(maff);
2522 space = isl_space_lift(isl_space_domain(space), n_div);
2523 space = isl_space_extend_domain_with_range(space,
2524 isl_multi_aff_get_space(maff));
2525 if (!space)
2526 return isl_multi_aff_free(maff);
2527 isl_space_free(maff->space);
2528 maff->space = space;
2530 if (ls) {
2531 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2532 if (!*ls)
2533 return isl_multi_aff_free(maff);
2536 for (i = 0; i < maff->n; ++i) {
2537 maff->p[i] = isl_aff_lift(maff->p[i]);
2538 if (!maff->p[i])
2539 goto error;
2542 return maff;
2543 error:
2544 if (ls)
2545 isl_local_space_free(*ls);
2546 return isl_multi_aff_free(maff);
2550 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2552 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2553 __isl_keep isl_pw_multi_aff *pma, int pos)
2555 int i;
2556 int n_out;
2557 isl_space *space;
2558 isl_pw_aff *pa;
2560 if (!pma)
2561 return NULL;
2563 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2564 if (pos < 0 || pos >= n_out)
2565 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2566 "index out of bounds", return NULL);
2568 space = isl_pw_multi_aff_get_space(pma);
2569 space = isl_space_drop_dims(space, isl_dim_out,
2570 pos + 1, n_out - pos - 1);
2571 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2573 pa = isl_pw_aff_alloc_size(space, pma->n);
2574 for (i = 0; i < pma->n; ++i) {
2575 isl_aff *aff;
2576 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2577 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2580 return pa;