isl_tab_pip.c: set_row_cst_to_div: don't assume coefficient of div is zero
[isl.git] / isl_aff.c
blob23d17f02bf6687502807a05fccd4b5e6e88dd766
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 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
285 if (!aff)
286 return -1;
288 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
291 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
293 int equal;
295 if (!aff1 || !aff2)
296 return -1;
298 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
299 if (equal < 0 || !equal)
300 return equal;
302 return isl_vec_is_equal(aff1->v, aff2->v);
305 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
307 if (!aff)
308 return -1;
309 isl_int_set(*v, aff->v->el[0]);
310 return 0;
313 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
315 if (!aff)
316 return -1;
317 isl_int_set(*v, aff->v->el[1]);
318 return 0;
321 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
322 enum isl_dim_type type, int pos, isl_int *v)
324 if (!aff)
325 return -1;
327 if (type == isl_dim_out)
328 isl_die(aff->v->ctx, isl_error_invalid,
329 "output/set dimension does not have a coefficient",
330 return -1);
331 if (type == isl_dim_in)
332 type = isl_dim_set;
334 if (pos >= isl_local_space_dim(aff->ls, type))
335 isl_die(aff->v->ctx, isl_error_invalid,
336 "position out of bounds", return -1);
338 pos += isl_local_space_offset(aff->ls, type);
339 isl_int_set(*v, aff->v->el[1 + pos]);
341 return 0;
344 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
346 aff = isl_aff_cow(aff);
347 if (!aff)
348 return NULL;
350 aff->v = isl_vec_cow(aff->v);
351 if (!aff->v)
352 return isl_aff_free(aff);
354 isl_int_set(aff->v->el[0], v);
356 return aff;
359 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
361 aff = isl_aff_cow(aff);
362 if (!aff)
363 return NULL;
365 aff->v = isl_vec_cow(aff->v);
366 if (!aff->v)
367 return isl_aff_free(aff);
369 isl_int_set(aff->v->el[1], v);
371 return aff;
374 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
376 if (isl_int_is_zero(v))
377 return aff;
379 aff = isl_aff_cow(aff);
380 if (!aff)
381 return NULL;
383 aff->v = isl_vec_cow(aff->v);
384 if (!aff->v)
385 return isl_aff_free(aff);
387 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
389 return aff;
392 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
394 isl_int t;
396 isl_int_init(t);
397 isl_int_set_si(t, v);
398 aff = isl_aff_add_constant(aff, t);
399 isl_int_clear(t);
401 return aff;
404 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
406 aff = isl_aff_cow(aff);
407 if (!aff)
408 return NULL;
410 aff->v = isl_vec_cow(aff->v);
411 if (!aff->v)
412 return isl_aff_free(aff);
414 isl_int_set_si(aff->v->el[1], v);
416 return aff;
419 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
420 enum isl_dim_type type, int pos, isl_int v)
422 if (!aff)
423 return NULL;
425 if (type == isl_dim_out)
426 isl_die(aff->v->ctx, isl_error_invalid,
427 "output/set dimension does not have a coefficient",
428 return isl_aff_free(aff));
429 if (type == isl_dim_in)
430 type = isl_dim_set;
432 if (pos >= isl_local_space_dim(aff->ls, type))
433 isl_die(aff->v->ctx, isl_error_invalid,
434 "position out of bounds", return isl_aff_free(aff));
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 pos += isl_local_space_offset(aff->ls, type);
445 isl_int_set(aff->v->el[1 + pos], v);
447 return aff;
450 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
451 enum isl_dim_type type, int pos, int v)
453 if (!aff)
454 return NULL;
456 if (type == isl_dim_out)
457 isl_die(aff->v->ctx, isl_error_invalid,
458 "output/set dimension does not have a coefficient",
459 return isl_aff_free(aff));
460 if (type == isl_dim_in)
461 type = isl_dim_set;
463 if (pos >= isl_local_space_dim(aff->ls, type))
464 isl_die(aff->v->ctx, isl_error_invalid,
465 "position out of bounds", return isl_aff_free(aff));
467 aff = isl_aff_cow(aff);
468 if (!aff)
469 return NULL;
471 aff->v = isl_vec_cow(aff->v);
472 if (!aff->v)
473 return isl_aff_free(aff);
475 pos += isl_local_space_offset(aff->ls, type);
476 isl_int_set_si(aff->v->el[1 + pos], v);
478 return aff;
481 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
482 enum isl_dim_type type, int pos, isl_int v)
484 if (!aff)
485 return NULL;
487 if (type == isl_dim_out)
488 isl_die(aff->v->ctx, isl_error_invalid,
489 "output/set dimension does not have a coefficient",
490 return isl_aff_free(aff));
491 if (type == isl_dim_in)
492 type = isl_dim_set;
494 if (pos >= isl_local_space_dim(aff->ls, type))
495 isl_die(aff->v->ctx, isl_error_invalid,
496 "position out of bounds", return isl_aff_free(aff));
498 aff = isl_aff_cow(aff);
499 if (!aff)
500 return NULL;
502 aff->v = isl_vec_cow(aff->v);
503 if (!aff->v)
504 return isl_aff_free(aff);
506 pos += isl_local_space_offset(aff->ls, type);
507 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
509 return aff;
512 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
513 enum isl_dim_type type, int pos, int v)
515 isl_int t;
517 isl_int_init(t);
518 isl_int_set_si(t, v);
519 aff = isl_aff_add_coefficient(aff, type, pos, t);
520 isl_int_clear(t);
522 return aff;
525 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
527 if (!aff)
528 return NULL;
530 return isl_local_space_get_div(aff->ls, pos);
533 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
535 aff = isl_aff_cow(aff);
536 if (!aff)
537 return NULL;
538 aff->v = isl_vec_cow(aff->v);
539 if (!aff->v)
540 return isl_aff_free(aff);
542 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
544 return aff;
547 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
549 if (!aff)
550 return NULL;
551 aff->v = isl_vec_normalize(aff->v);
552 if (!aff->v)
553 return isl_aff_free(aff);
554 return aff;
557 /* Given f, return floor(f).
558 * If f is an integer expression, then just return f.
559 * Otherwise, if f = g/m, write g = q m + r,
560 * create a new div d = [r/m] and return the expression q + d.
561 * The coefficients in r are taken to lie between -m/2 and m/2.
563 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
565 int i;
566 int size;
567 isl_ctx *ctx;
568 isl_vec *div;
570 if (!aff)
571 return NULL;
573 if (isl_int_is_one(aff->v->el[0]))
574 return aff;
576 aff = isl_aff_cow(aff);
577 if (!aff)
578 return NULL;
580 aff->v = isl_vec_cow(aff->v);
581 div = isl_vec_copy(aff->v);
582 div = isl_vec_cow(div);
583 if (!div)
584 return isl_aff_free(aff);
586 ctx = isl_aff_get_ctx(aff);
587 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
588 for (i = 1; i < aff->v->size; ++i) {
589 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
590 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
591 if (isl_int_gt(div->el[i], aff->v->el[0])) {
592 isl_int_sub(div->el[i], div->el[i], div->el[0]);
593 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
597 aff->ls = isl_local_space_add_div(aff->ls, div);
598 if (!aff->ls)
599 return isl_aff_free(aff);
601 size = aff->v->size;
602 aff->v = isl_vec_extend(aff->v, size + 1);
603 if (!aff->v)
604 return isl_aff_free(aff);
605 isl_int_set_si(aff->v->el[0], 1);
606 isl_int_set_si(aff->v->el[size], 1);
608 return aff;
611 /* Compute
613 * aff mod m = aff - m * floor(aff/m)
615 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
617 isl_aff *res;
619 res = isl_aff_copy(aff);
620 aff = isl_aff_scale_down(aff, m);
621 aff = isl_aff_floor(aff);
622 aff = isl_aff_scale(aff, m);
623 res = isl_aff_sub(res, aff);
625 return res;
628 /* Compute
630 * pwaff mod m = pwaff - m * floor(pwaff/m)
632 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
634 isl_pw_aff *res;
636 res = isl_pw_aff_copy(pwaff);
637 pwaff = isl_pw_aff_scale_down(pwaff, m);
638 pwaff = isl_pw_aff_floor(pwaff);
639 pwaff = isl_pw_aff_scale(pwaff, m);
640 res = isl_pw_aff_sub(res, pwaff);
642 return res;
645 /* Given f, return ceil(f).
646 * If f is an integer expression, then just return f.
647 * Otherwise, create a new div d = [-f] and return the expression -d.
649 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
651 if (!aff)
652 return NULL;
654 if (isl_int_is_one(aff->v->el[0]))
655 return aff;
657 aff = isl_aff_neg(aff);
658 aff = isl_aff_floor(aff);
659 aff = isl_aff_neg(aff);
661 return aff;
664 /* Apply the expansion computed by isl_merge_divs.
665 * The expansion itself is given by "exp" while the resulting
666 * list of divs is given by "div".
668 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
669 __isl_take isl_mat *div, int *exp)
671 int i, j;
672 int old_n_div;
673 int new_n_div;
674 int offset;
676 aff = isl_aff_cow(aff);
677 if (!aff || !div)
678 goto error;
680 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
681 new_n_div = isl_mat_rows(div);
682 if (new_n_div < old_n_div)
683 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
684 "not an expansion", goto error);
686 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
687 if (!aff->v)
688 goto error;
690 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
691 j = old_n_div - 1;
692 for (i = new_n_div - 1; i >= 0; --i) {
693 if (j >= 0 && exp[j] == i) {
694 if (i != j)
695 isl_int_swap(aff->v->el[offset + i],
696 aff->v->el[offset + j]);
697 j--;
698 } else
699 isl_int_set_si(aff->v->el[offset + i], 0);
702 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
703 if (!aff->ls)
704 goto error;
705 isl_mat_free(div);
706 return aff;
707 error:
708 isl_aff_free(aff);
709 isl_mat_free(div);
710 return NULL;
713 /* Add two affine expressions that live in the same local space.
715 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
716 __isl_take isl_aff *aff2)
718 isl_int gcd, f;
720 aff1 = isl_aff_cow(aff1);
721 if (!aff1 || !aff2)
722 goto error;
724 aff1->v = isl_vec_cow(aff1->v);
725 if (!aff1->v)
726 goto error;
728 isl_int_init(gcd);
729 isl_int_init(f);
730 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
731 isl_int_divexact(f, aff2->v->el[0], gcd);
732 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
733 isl_int_divexact(f, aff1->v->el[0], gcd);
734 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
735 isl_int_divexact(f, aff2->v->el[0], gcd);
736 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
737 isl_int_clear(f);
738 isl_int_clear(gcd);
740 isl_aff_free(aff2);
741 return aff1;
742 error:
743 isl_aff_free(aff1);
744 isl_aff_free(aff2);
745 return NULL;
748 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
749 __isl_take isl_aff *aff2)
751 isl_ctx *ctx;
752 int *exp1 = NULL;
753 int *exp2 = NULL;
754 isl_mat *div;
756 if (!aff1 || !aff2)
757 goto error;
759 ctx = isl_aff_get_ctx(aff1);
760 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
761 isl_die(ctx, isl_error_invalid,
762 "spaces don't match", goto error);
764 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
765 return add_expanded(aff1, aff2);
767 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
768 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
769 if (!exp1 || !exp2)
770 goto error;
772 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
773 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
774 aff2 = isl_aff_expand_divs(aff2, div, exp2);
775 free(exp1);
776 free(exp2);
778 return add_expanded(aff1, aff2);
779 error:
780 free(exp1);
781 free(exp2);
782 isl_aff_free(aff1);
783 isl_aff_free(aff2);
784 return NULL;
787 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
788 __isl_take isl_aff *aff2)
790 return isl_aff_add(aff1, isl_aff_neg(aff2));
793 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
795 isl_int gcd;
797 if (isl_int_is_one(f))
798 return aff;
800 aff = isl_aff_cow(aff);
801 if (!aff)
802 return NULL;
803 aff->v = isl_vec_cow(aff->v);
804 if (!aff->v)
805 return isl_aff_free(aff);
807 isl_int_init(gcd);
808 isl_int_gcd(gcd, aff->v->el[0], f);
809 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
810 isl_int_divexact(gcd, f, gcd);
811 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
812 isl_int_clear(gcd);
814 return aff;
817 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
819 isl_int gcd;
821 if (isl_int_is_one(f))
822 return aff;
824 aff = isl_aff_cow(aff);
825 if (!aff)
826 return NULL;
827 aff->v = isl_vec_cow(aff->v);
828 if (!aff->v)
829 return isl_aff_free(aff);
831 isl_int_init(gcd);
832 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
833 isl_int_gcd(gcd, gcd, f);
834 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
835 isl_int_divexact(gcd, f, gcd);
836 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
837 isl_int_clear(gcd);
839 return aff;
842 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
844 isl_int v;
846 if (f == 1)
847 return aff;
849 isl_int_init(v);
850 isl_int_set_ui(v, f);
851 aff = isl_aff_scale_down(aff, v);
852 isl_int_clear(v);
854 return aff;
857 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
858 enum isl_dim_type type, unsigned pos, const char *s)
860 aff = isl_aff_cow(aff);
861 if (!aff)
862 return NULL;
863 if (type == isl_dim_out)
864 isl_die(aff->v->ctx, isl_error_invalid,
865 "cannot set name of output/set dimension",
866 return isl_aff_free(aff));
867 if (type == isl_dim_in)
868 type = isl_dim_set;
869 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
870 if (!aff->ls)
871 return isl_aff_free(aff);
873 return aff;
876 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
877 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
879 aff = isl_aff_cow(aff);
880 if (!aff)
881 return isl_id_free(id);
882 if (type == isl_dim_out)
883 isl_die(aff->v->ctx, isl_error_invalid,
884 "cannot set name of output/set dimension",
885 goto error);
886 if (type == isl_dim_in)
887 type = isl_dim_set;
888 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
889 if (!aff->ls)
890 return isl_aff_free(aff);
892 return aff;
893 error:
894 isl_id_free(id);
895 isl_aff_free(aff);
896 return NULL;
899 /* Exploit the equalities in "eq" to simplify the affine expression
900 * and the expressions of the integer divisions in the local space.
901 * The integer divisions in this local space are assumed to appear
902 * as regular dimensions in "eq".
904 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
905 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
907 int i, j;
908 unsigned total;
909 unsigned n_div;
911 if (!eq)
912 goto error;
913 if (eq->n_eq == 0) {
914 isl_basic_set_free(eq);
915 return aff;
918 aff = isl_aff_cow(aff);
919 if (!aff)
920 goto error;
922 aff->ls = isl_local_space_substitute_equalities(aff->ls,
923 isl_basic_set_copy(eq));
924 if (!aff->ls)
925 goto error;
927 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
928 n_div = eq->n_div;
929 for (i = 0; i < eq->n_eq; ++i) {
930 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
931 if (j < 0 || j == 0 || j >= total)
932 continue;
934 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
935 &aff->v->el[0]);
938 isl_basic_set_free(eq);
939 return aff;
940 error:
941 isl_basic_set_free(eq);
942 isl_aff_free(aff);
943 return NULL;
946 /* Exploit the equalities in "eq" to simplify the affine expression
947 * and the expressions of the integer divisions in the local space.
949 static __isl_give isl_aff *isl_aff_substitute_equalities(
950 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
952 int n_div;
954 if (!aff || !eq)
955 goto error;
956 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
957 if (n_div > 0)
958 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
959 return isl_aff_substitute_equalities_lifted(aff, eq);
960 error:
961 isl_basic_set_free(eq);
962 isl_aff_free(aff);
963 return NULL;
966 /* Look for equalities among the variables shared by context and aff
967 * and the integer divisions of aff, if any.
968 * The equalities are then used to eliminate coefficients and/or integer
969 * divisions from aff.
971 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
972 __isl_take isl_set *context)
974 isl_basic_set *hull;
975 int n_div;
977 if (!aff)
978 goto error;
979 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
980 if (n_div > 0) {
981 isl_basic_set *bset;
982 isl_local_space *ls;
983 context = isl_set_add_dims(context, isl_dim_set, n_div);
984 ls = isl_aff_get_domain_local_space(aff);
985 bset = isl_basic_set_from_local_space(ls);
986 bset = isl_basic_set_lift(bset);
987 bset = isl_basic_set_flatten(bset);
988 context = isl_set_intersect(context,
989 isl_set_from_basic_set(bset));
992 hull = isl_set_affine_hull(context);
993 return isl_aff_substitute_equalities_lifted(aff, hull);
994 error:
995 isl_aff_free(aff);
996 isl_set_free(context);
997 return NULL;
1000 /* Return a basic set containing those elements in the space
1001 * of aff where it is non-negative.
1003 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1005 isl_constraint *ineq;
1007 ineq = isl_inequality_from_aff(aff);
1009 return isl_basic_set_from_constraint(ineq);
1012 /* Return a basic set containing those elements in the space
1013 * of aff where it is zero.
1015 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1017 isl_constraint *ineq;
1019 ineq = isl_equality_from_aff(aff);
1021 return isl_basic_set_from_constraint(ineq);
1024 /* Return a basic set containing those elements in the shared space
1025 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1027 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1028 __isl_take isl_aff *aff2)
1030 aff1 = isl_aff_sub(aff1, aff2);
1032 return isl_aff_nonneg_basic_set(aff1);
1035 /* Return a basic set containing those elements in the shared space
1036 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1038 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1039 __isl_take isl_aff *aff2)
1041 return isl_aff_ge_basic_set(aff2, aff1);
1044 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1045 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1047 aff1 = isl_aff_add(aff1, aff2);
1048 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1049 return aff1;
1052 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1054 if (!aff)
1055 return -1;
1057 return 0;
1060 /* Check whether the given affine expression has non-zero coefficient
1061 * for any dimension in the given range or if any of these dimensions
1062 * appear with non-zero coefficients in any of the integer divisions
1063 * involved in the affine expression.
1065 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1066 enum isl_dim_type type, unsigned first, unsigned n)
1068 int i;
1069 isl_ctx *ctx;
1070 int *active = NULL;
1071 int involves = 0;
1073 if (!aff)
1074 return -1;
1075 if (n == 0)
1076 return 0;
1078 ctx = isl_aff_get_ctx(aff);
1079 if (first + n > isl_aff_dim(aff, type))
1080 isl_die(ctx, isl_error_invalid,
1081 "range out of bounds", return -1);
1083 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1084 if (!active)
1085 goto error;
1087 first += isl_local_space_offset(aff->ls, type) - 1;
1088 for (i = 0; i < n; ++i)
1089 if (active[first + i]) {
1090 involves = 1;
1091 break;
1094 free(active);
1096 return involves;
1097 error:
1098 free(active);
1099 return -1;
1102 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1103 enum isl_dim_type type, unsigned first, unsigned n)
1105 isl_ctx *ctx;
1107 if (!aff)
1108 return NULL;
1109 if (type == isl_dim_out)
1110 isl_die(aff->v->ctx, isl_error_invalid,
1111 "cannot drop output/set dimension",
1112 return isl_aff_free(aff));
1113 if (type == isl_dim_in)
1114 type = isl_dim_set;
1115 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1116 return aff;
1118 ctx = isl_aff_get_ctx(aff);
1119 if (first + n > isl_local_space_dim(aff->ls, type))
1120 isl_die(ctx, isl_error_invalid, "range out of bounds",
1121 return isl_aff_free(aff));
1123 aff = isl_aff_cow(aff);
1124 if (!aff)
1125 return NULL;
1127 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1128 if (!aff->ls)
1129 return isl_aff_free(aff);
1131 first += 1 + isl_local_space_offset(aff->ls, type);
1132 aff->v = isl_vec_drop_els(aff->v, first, n);
1133 if (!aff->v)
1134 return isl_aff_free(aff);
1136 return aff;
1139 __isl_give isl_aff *isl_aff_insert_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 insert output/set dimensions",
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 > isl_local_space_dim(aff->ls, type))
1157 isl_die(ctx, isl_error_invalid, "position 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_insert_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_insert_zero_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_add_dims(__isl_take isl_aff *aff,
1177 enum isl_dim_type type, unsigned n)
1179 unsigned pos;
1181 pos = isl_aff_dim(aff, type);
1183 return isl_aff_insert_dims(aff, type, pos, n);
1186 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1187 enum isl_dim_type type, unsigned n)
1189 unsigned pos;
1191 pos = isl_pw_aff_dim(pwaff, type);
1193 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1196 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1198 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1199 return isl_pw_aff_alloc(dom, aff);
1202 #undef PW
1203 #define PW isl_pw_aff
1204 #undef EL
1205 #define EL isl_aff
1206 #undef EL_IS_ZERO
1207 #define EL_IS_ZERO is_empty
1208 #undef ZERO
1209 #define ZERO empty
1210 #undef IS_ZERO
1211 #define IS_ZERO is_empty
1212 #undef FIELD
1213 #define FIELD aff
1215 #define NO_EVAL
1216 #define NO_OPT
1217 #define NO_MOVE_DIMS
1218 #define NO_LIFT
1219 #define NO_MORPH
1221 #include <isl_pw_templ.c>
1223 static __isl_give isl_set *align_params_pw_pw_set_and(
1224 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1225 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1226 __isl_take isl_pw_aff *pwaff2))
1228 if (!pwaff1 || !pwaff2)
1229 goto error;
1230 if (isl_space_match(pwaff1->dim, isl_dim_param,
1231 pwaff2->dim, isl_dim_param))
1232 return fn(pwaff1, pwaff2);
1233 if (!isl_space_has_named_params(pwaff1->dim) ||
1234 !isl_space_has_named_params(pwaff2->dim))
1235 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1236 "unaligned unnamed parameters", goto error);
1237 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1238 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1239 return fn(pwaff1, pwaff2);
1240 error:
1241 isl_pw_aff_free(pwaff1);
1242 isl_pw_aff_free(pwaff2);
1243 return NULL;
1246 /* Compute a piecewise quasi-affine expression with a domain that
1247 * is the union of those of pwaff1 and pwaff2 and such that on each
1248 * cell, the quasi-affine expression is the better (according to cmp)
1249 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1250 * is defined on a given cell, then the associated expression
1251 * is the defined one.
1253 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1254 __isl_take isl_pw_aff *pwaff2,
1255 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1256 __isl_take isl_aff *aff2))
1258 int i, j, n;
1259 isl_pw_aff *res;
1260 isl_ctx *ctx;
1261 isl_set *set;
1263 if (!pwaff1 || !pwaff2)
1264 goto error;
1266 ctx = isl_space_get_ctx(pwaff1->dim);
1267 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1268 isl_die(ctx, isl_error_invalid,
1269 "arguments should live in same space", goto error);
1271 if (isl_pw_aff_is_empty(pwaff1)) {
1272 isl_pw_aff_free(pwaff1);
1273 return pwaff2;
1276 if (isl_pw_aff_is_empty(pwaff2)) {
1277 isl_pw_aff_free(pwaff2);
1278 return pwaff1;
1281 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1282 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1284 for (i = 0; i < pwaff1->n; ++i) {
1285 set = isl_set_copy(pwaff1->p[i].set);
1286 for (j = 0; j < pwaff2->n; ++j) {
1287 struct isl_set *common;
1288 isl_set *better;
1290 common = isl_set_intersect(
1291 isl_set_copy(pwaff1->p[i].set),
1292 isl_set_copy(pwaff2->p[j].set));
1293 better = isl_set_from_basic_set(cmp(
1294 isl_aff_copy(pwaff2->p[j].aff),
1295 isl_aff_copy(pwaff1->p[i].aff)));
1296 better = isl_set_intersect(common, better);
1297 if (isl_set_plain_is_empty(better)) {
1298 isl_set_free(better);
1299 continue;
1301 set = isl_set_subtract(set, isl_set_copy(better));
1303 res = isl_pw_aff_add_piece(res, better,
1304 isl_aff_copy(pwaff2->p[j].aff));
1306 res = isl_pw_aff_add_piece(res, set,
1307 isl_aff_copy(pwaff1->p[i].aff));
1310 for (j = 0; j < pwaff2->n; ++j) {
1311 set = isl_set_copy(pwaff2->p[j].set);
1312 for (i = 0; i < pwaff1->n; ++i)
1313 set = isl_set_subtract(set,
1314 isl_set_copy(pwaff1->p[i].set));
1315 res = isl_pw_aff_add_piece(res, set,
1316 isl_aff_copy(pwaff2->p[j].aff));
1319 isl_pw_aff_free(pwaff1);
1320 isl_pw_aff_free(pwaff2);
1322 return res;
1323 error:
1324 isl_pw_aff_free(pwaff1);
1325 isl_pw_aff_free(pwaff2);
1326 return NULL;
1329 /* Compute a piecewise quasi-affine expression with a domain that
1330 * is the union of those of pwaff1 and pwaff2 and such that on each
1331 * cell, the quasi-affine expression is the maximum of those of pwaff1
1332 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1333 * cell, then the associated expression is the defined one.
1335 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1336 __isl_take isl_pw_aff *pwaff2)
1338 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1341 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1342 __isl_take isl_pw_aff *pwaff2)
1344 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1345 &pw_aff_union_max);
1348 /* Compute a piecewise quasi-affine expression with a domain that
1349 * is the union of those of pwaff1 and pwaff2 and such that on each
1350 * cell, the quasi-affine expression is the minimum of those of pwaff1
1351 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1352 * cell, then the associated expression is the defined one.
1354 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1355 __isl_take isl_pw_aff *pwaff2)
1357 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1360 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1361 __isl_take isl_pw_aff *pwaff2)
1363 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1364 &pw_aff_union_min);
1367 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1368 __isl_take isl_pw_aff *pwaff2, int max)
1370 if (max)
1371 return isl_pw_aff_union_max(pwaff1, pwaff2);
1372 else
1373 return isl_pw_aff_union_min(pwaff1, pwaff2);
1376 /* Construct a map with as domain the domain of pwaff and
1377 * one-dimensional range corresponding to the affine expressions.
1379 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1381 int i;
1382 isl_space *dim;
1383 isl_map *map;
1385 if (!pwaff)
1386 return NULL;
1388 dim = isl_pw_aff_get_space(pwaff);
1389 map = isl_map_empty(dim);
1391 for (i = 0; i < pwaff->n; ++i) {
1392 isl_basic_map *bmap;
1393 isl_map *map_i;
1395 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1396 map_i = isl_map_from_basic_map(bmap);
1397 map_i = isl_map_intersect_domain(map_i,
1398 isl_set_copy(pwaff->p[i].set));
1399 map = isl_map_union_disjoint(map, map_i);
1402 isl_pw_aff_free(pwaff);
1404 return map;
1407 /* Construct a map with as domain the domain of pwaff and
1408 * one-dimensional range corresponding to the affine expressions.
1410 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1412 if (isl_space_is_set(pwaff->dim))
1413 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1414 "space of input is not a map",
1415 return isl_pw_aff_free(pwaff));
1416 return map_from_pw_aff(pwaff);
1419 /* Construct a one-dimensional set with as parameter domain
1420 * the domain of pwaff and the single set dimension
1421 * corresponding to the affine expressions.
1423 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1425 if (!isl_space_is_set(pwaff->dim))
1426 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1427 "space of input is not a set",
1428 return isl_pw_aff_free(pwaff));
1429 return map_from_pw_aff(pwaff);
1432 /* Return a set containing those elements in the domain
1433 * of pwaff where it is non-negative.
1435 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1437 int i;
1438 isl_set *set;
1440 if (!pwaff)
1441 return NULL;
1443 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1445 for (i = 0; i < pwaff->n; ++i) {
1446 isl_basic_set *bset;
1447 isl_set *set_i;
1449 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1450 set_i = isl_set_from_basic_set(bset);
1451 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1452 set = isl_set_union_disjoint(set, set_i);
1455 isl_pw_aff_free(pwaff);
1457 return set;
1460 /* Return a set containing those elements in the domain
1461 * of pwaff where it is zero.
1463 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1465 int i;
1466 isl_set *set;
1468 if (!pwaff)
1469 return NULL;
1471 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1473 for (i = 0; i < pwaff->n; ++i) {
1474 isl_basic_set *bset;
1475 isl_set *set_i;
1477 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1478 set_i = isl_set_from_basic_set(bset);
1479 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1480 set = isl_set_union_disjoint(set, set_i);
1483 isl_pw_aff_free(pwaff);
1485 return set;
1488 /* Return a set containing those elements in the domain
1489 * of pwaff where it is not zero.
1491 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1493 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1496 /* Return a set containing those elements in the shared domain
1497 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1499 * We compute the difference on the shared domain and then construct
1500 * the set of values where this difference is non-negative.
1501 * If strict is set, we first subtract 1 from the difference.
1502 * If equal is set, we only return the elements where pwaff1 and pwaff2
1503 * are equal.
1505 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1506 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1508 isl_set *set1, *set2;
1510 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1511 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1512 set1 = isl_set_intersect(set1, set2);
1513 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1514 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1515 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1517 if (strict) {
1518 isl_space *dim = isl_set_get_space(set1);
1519 isl_aff *aff;
1520 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1521 aff = isl_aff_add_constant_si(aff, -1);
1522 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1523 } else
1524 isl_set_free(set1);
1526 if (equal)
1527 return isl_pw_aff_zero_set(pwaff1);
1528 return isl_pw_aff_nonneg_set(pwaff1);
1531 /* Return a set containing those elements in the shared domain
1532 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1534 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1535 __isl_take isl_pw_aff *pwaff2)
1537 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1540 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1541 __isl_take isl_pw_aff *pwaff2)
1543 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1546 /* Return a set containing those elements in the shared domain
1547 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1549 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1550 __isl_take isl_pw_aff *pwaff2)
1552 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1555 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1556 __isl_take isl_pw_aff *pwaff2)
1558 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1561 /* Return a set containing those elements in the shared domain
1562 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1564 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1565 __isl_take isl_pw_aff *pwaff2)
1567 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1570 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1571 __isl_take isl_pw_aff *pwaff2)
1573 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1576 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1577 __isl_take isl_pw_aff *pwaff2)
1579 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1582 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1583 __isl_take isl_pw_aff *pwaff2)
1585 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1588 /* Return a set containing those elements in the shared domain
1589 * of the elements of list1 and list2 where each element in list1
1590 * has the relation specified by "fn" with each element in list2.
1592 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1593 __isl_take isl_pw_aff_list *list2,
1594 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1595 __isl_take isl_pw_aff *pwaff2))
1597 int i, j;
1598 isl_ctx *ctx;
1599 isl_set *set;
1601 if (!list1 || !list2)
1602 goto error;
1604 ctx = isl_pw_aff_list_get_ctx(list1);
1605 if (list1->n < 1 || list2->n < 1)
1606 isl_die(ctx, isl_error_invalid,
1607 "list should contain at least one element", goto error);
1609 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1610 for (i = 0; i < list1->n; ++i)
1611 for (j = 0; j < list2->n; ++j) {
1612 isl_set *set_ij;
1614 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1615 isl_pw_aff_copy(list2->p[j]));
1616 set = isl_set_intersect(set, set_ij);
1619 isl_pw_aff_list_free(list1);
1620 isl_pw_aff_list_free(list2);
1621 return set;
1622 error:
1623 isl_pw_aff_list_free(list1);
1624 isl_pw_aff_list_free(list2);
1625 return NULL;
1628 /* Return a set containing those elements in the shared domain
1629 * of the elements of list1 and list2 where each element in list1
1630 * is equal to each element in list2.
1632 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1633 __isl_take isl_pw_aff_list *list2)
1635 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1638 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1639 __isl_take isl_pw_aff_list *list2)
1641 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1644 /* Return a set containing those elements in the shared domain
1645 * of the elements of list1 and list2 where each element in list1
1646 * is less than or equal to each element in list2.
1648 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1649 __isl_take isl_pw_aff_list *list2)
1651 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1654 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1655 __isl_take isl_pw_aff_list *list2)
1657 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1660 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1661 __isl_take isl_pw_aff_list *list2)
1663 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1666 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1667 __isl_take isl_pw_aff_list *list2)
1669 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1673 /* Return a set containing those elements in the shared domain
1674 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1676 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1677 __isl_take isl_pw_aff *pwaff2)
1679 isl_set *set_lt, *set_gt;
1681 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1682 isl_pw_aff_copy(pwaff2));
1683 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1684 return isl_set_union_disjoint(set_lt, set_gt);
1687 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1688 __isl_take isl_pw_aff *pwaff2)
1690 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1693 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1694 isl_int v)
1696 int i;
1698 if (isl_int_is_one(v))
1699 return pwaff;
1700 if (!isl_int_is_pos(v))
1701 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1702 "factor needs to be positive",
1703 return isl_pw_aff_free(pwaff));
1704 pwaff = isl_pw_aff_cow(pwaff);
1705 if (!pwaff)
1706 return NULL;
1707 if (pwaff->n == 0)
1708 return pwaff;
1710 for (i = 0; i < pwaff->n; ++i) {
1711 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1712 if (!pwaff->p[i].aff)
1713 return isl_pw_aff_free(pwaff);
1716 return pwaff;
1719 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1721 int i;
1723 pwaff = isl_pw_aff_cow(pwaff);
1724 if (!pwaff)
1725 return NULL;
1726 if (pwaff->n == 0)
1727 return pwaff;
1729 for (i = 0; i < pwaff->n; ++i) {
1730 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1731 if (!pwaff->p[i].aff)
1732 return isl_pw_aff_free(pwaff);
1735 return pwaff;
1738 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1740 int i;
1742 pwaff = isl_pw_aff_cow(pwaff);
1743 if (!pwaff)
1744 return NULL;
1745 if (pwaff->n == 0)
1746 return pwaff;
1748 for (i = 0; i < pwaff->n; ++i) {
1749 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1750 if (!pwaff->p[i].aff)
1751 return isl_pw_aff_free(pwaff);
1754 return pwaff;
1757 /* Return an affine expression that is equal to pwaff_true for elements
1758 * in "cond" and to pwaff_false for elements not in "cond".
1759 * That is, return cond ? pwaff_true : pwaff_false;
1761 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1762 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1764 isl_set *comp;
1766 comp = isl_set_complement(isl_set_copy(cond));
1767 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1768 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1770 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1773 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1775 if (!aff)
1776 return -1;
1778 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1781 /* Check whether pwaff is a piecewise constant.
1783 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1785 int i;
1787 if (!pwaff)
1788 return -1;
1790 for (i = 0; i < pwaff->n; ++i) {
1791 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1792 if (is_cst < 0 || !is_cst)
1793 return is_cst;
1796 return 1;
1799 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1800 __isl_take isl_aff *aff2)
1802 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1803 return isl_aff_mul(aff2, aff1);
1805 if (!isl_aff_is_cst(aff2))
1806 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1807 "at least one affine expression should be constant",
1808 goto error);
1810 aff1 = isl_aff_cow(aff1);
1811 if (!aff1 || !aff2)
1812 goto error;
1814 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1815 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1817 isl_aff_free(aff2);
1818 return aff1;
1819 error:
1820 isl_aff_free(aff1);
1821 isl_aff_free(aff2);
1822 return NULL;
1825 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1826 __isl_take isl_pw_aff *pwaff2)
1828 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1831 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1832 __isl_take isl_pw_aff *pwaff2)
1834 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1837 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1838 __isl_take isl_pw_aff *pwaff2)
1840 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1843 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1844 __isl_take isl_pw_aff *pwaff2)
1846 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1849 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1850 __isl_take isl_pw_aff *pwaff2)
1852 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1855 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1856 __isl_take isl_pw_aff *pwaff2)
1858 isl_set *le;
1860 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1861 isl_pw_aff_copy(pwaff2));
1862 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1865 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1866 __isl_take isl_pw_aff *pwaff2)
1868 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1871 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1872 __isl_take isl_pw_aff *pwaff2)
1874 isl_set *le;
1876 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1877 isl_pw_aff_copy(pwaff2));
1878 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1881 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1882 __isl_take isl_pw_aff *pwaff2)
1884 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1887 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1888 __isl_take isl_pw_aff_list *list,
1889 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1890 __isl_take isl_pw_aff *pwaff2))
1892 int i;
1893 isl_ctx *ctx;
1894 isl_pw_aff *res;
1896 if (!list)
1897 return NULL;
1899 ctx = isl_pw_aff_list_get_ctx(list);
1900 if (list->n < 1)
1901 isl_die(ctx, isl_error_invalid,
1902 "list should contain at least one element",
1903 return isl_pw_aff_list_free(list));
1905 res = isl_pw_aff_copy(list->p[0]);
1906 for (i = 1; i < list->n; ++i)
1907 res = fn(res, isl_pw_aff_copy(list->p[i]));
1909 isl_pw_aff_list_free(list);
1910 return res;
1913 /* Return an isl_pw_aff that maps each element in the intersection of the
1914 * domains of the elements of list to the minimal corresponding affine
1915 * expression.
1917 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1919 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1922 /* Return an isl_pw_aff that maps each element in the intersection of the
1923 * domains of the elements of list to the maximal corresponding affine
1924 * expression.
1926 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1928 return pw_aff_list_reduce(list, &isl_pw_aff_max);
1931 #undef BASE
1932 #define BASE aff
1934 #include <isl_multi_templ.c>
1936 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
1937 __isl_take isl_multi_aff *maff2)
1939 int i;
1940 isl_ctx *ctx;
1942 maff1 = isl_multi_aff_cow(maff1);
1943 if (!maff1 || !maff2)
1944 goto error;
1946 ctx = isl_multi_aff_get_ctx(maff1);
1947 if (!isl_space_is_equal(maff1->space, maff2->space))
1948 isl_die(ctx, isl_error_invalid,
1949 "spaces don't match", goto error);
1951 for (i = 0; i < maff1->n; ++i) {
1952 maff1->p[i] = isl_aff_add(maff1->p[i],
1953 isl_aff_copy(maff2->p[i]));
1954 if (!maff1->p[i])
1955 goto error;
1958 isl_multi_aff_free(maff2);
1959 return maff1;
1960 error:
1961 isl_multi_aff_free(maff1);
1962 isl_multi_aff_free(maff2);
1963 return NULL;
1966 /* Exploit the equalities in "eq" to simplify the affine expressions.
1968 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
1969 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
1971 int i;
1973 maff = isl_multi_aff_cow(maff);
1974 if (!maff || !eq)
1975 goto error;
1977 for (i = 0; i < maff->n; ++i) {
1978 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
1979 isl_basic_set_copy(eq));
1980 if (!maff->p[i])
1981 goto error;
1984 isl_basic_set_free(eq);
1985 return maff;
1986 error:
1987 isl_basic_set_free(eq);
1988 isl_multi_aff_free(maff);
1989 return NULL;
1992 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
1993 isl_int f)
1995 int i;
1997 maff = isl_multi_aff_cow(maff);
1998 if (!maff)
1999 return NULL;
2001 for (i = 0; i < maff->n; ++i) {
2002 maff->p[i] = isl_aff_scale(maff->p[i], f);
2003 if (!maff->p[i])
2004 return isl_multi_aff_free(maff);
2007 return maff;
2010 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2011 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2013 maff1 = isl_multi_aff_add(maff1, maff2);
2014 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2015 return maff1;
2018 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2020 if (!maff)
2021 return -1;
2023 return 0;
2026 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2027 __isl_keep isl_multi_aff *maff2)
2029 int i;
2030 int equal;
2032 if (!maff1 || !maff2)
2033 return -1;
2034 if (maff1->n != maff2->n)
2035 return 0;
2036 equal = isl_space_is_equal(maff1->space, maff2->space);
2037 if (equal < 0 || !equal)
2038 return equal;
2040 for (i = 0; i < maff1->n; ++i) {
2041 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2042 if (equal < 0 || !equal)
2043 return equal;
2046 return 1;
2049 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2050 __isl_take isl_multi_aff *maff,
2051 enum isl_dim_type type, unsigned pos, const char *s)
2053 int i;
2055 maff = isl_multi_aff_cow(maff);
2056 if (!maff)
2057 return NULL;
2059 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2060 if (!maff->space)
2061 return isl_multi_aff_free(maff);
2062 for (i = 0; i < maff->n; ++i) {
2063 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2064 if (!maff->p[i])
2065 return isl_multi_aff_free(maff);
2068 return maff;
2071 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2072 enum isl_dim_type type, unsigned first, unsigned n)
2074 int i;
2076 maff = isl_multi_aff_cow(maff);
2077 if (!maff)
2078 return NULL;
2080 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2081 if (!maff->space)
2082 return isl_multi_aff_free(maff);
2083 for (i = 0; i < maff->n; ++i) {
2084 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2085 if (!maff->p[i])
2086 return isl_multi_aff_free(maff);
2089 return maff;
2092 #undef PW
2093 #define PW isl_pw_multi_aff
2094 #undef EL
2095 #define EL isl_multi_aff
2096 #undef EL_IS_ZERO
2097 #define EL_IS_ZERO is_empty
2098 #undef ZERO
2099 #define ZERO empty
2100 #undef IS_ZERO
2101 #define IS_ZERO is_empty
2102 #undef FIELD
2103 #define FIELD maff
2105 #define NO_NEG
2106 #define NO_EVAL
2107 #define NO_OPT
2108 #define NO_INVOLVES_DIMS
2109 #define NO_MOVE_DIMS
2110 #define NO_INSERT_DIMS
2111 #define NO_LIFT
2112 #define NO_MORPH
2114 #include <isl_pw_templ.c>
2116 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2117 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2119 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2120 &isl_multi_aff_add);
2123 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2124 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2126 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2127 &pw_multi_aff_add);
2130 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2131 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2133 return isl_pw_multi_aff_union_add_(pma1, pma2);
2136 /* Construct a map mapping the domain the piecewise multi-affine expression
2137 * to its range, with each dimension in the range equated to the
2138 * corresponding affine expression on its cell.
2140 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2142 int i;
2143 isl_map *map;
2145 if (!pma)
2146 return NULL;
2148 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2150 for (i = 0; i < pma->n; ++i) {
2151 isl_multi_aff *maff;
2152 isl_basic_map *bmap;
2153 isl_map *map_i;
2155 maff = isl_multi_aff_copy(pma->p[i].maff);
2156 bmap = isl_basic_map_from_multi_aff(maff);
2157 map_i = isl_map_from_basic_map(bmap);
2158 map_i = isl_map_intersect_domain(map_i,
2159 isl_set_copy(pma->p[i].set));
2160 map = isl_map_union_disjoint(map, map_i);
2163 isl_pw_multi_aff_free(pma);
2164 return map;
2167 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2169 if (!isl_space_is_set(pma->dim))
2170 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2171 "isl_pw_multi_aff cannot be converted into an isl_set",
2172 return isl_pw_multi_aff_free(pma));
2174 return isl_map_from_pw_multi_aff(pma);
2177 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2178 * This obivously only works if the input "map" is single-valued.
2179 * If so, we compute the lexicographic minimum of the image in the form
2180 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2181 * to its lexicographic minimum.
2182 * If the input is not single-valued, we produce an error.
2184 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2186 int i;
2187 int sv;
2188 isl_pw_multi_aff *pma;
2190 if (!map)
2191 return NULL;
2193 sv = isl_map_is_single_valued(map);
2194 if (sv < 0)
2195 goto error;
2196 if (!sv)
2197 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2198 "map is not single-valued", goto error);
2199 map = isl_map_make_disjoint(map);
2200 if (!map)
2201 return NULL;
2203 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2205 for (i = 0; i < map->n; ++i) {
2206 isl_pw_multi_aff *pma_i;
2207 isl_basic_map *bmap;
2208 bmap = isl_basic_map_copy(map->p[i]);
2209 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2210 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2213 isl_map_free(map);
2214 return pma;
2215 error:
2216 isl_map_free(map);
2217 return NULL;
2220 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2222 return isl_pw_multi_aff_from_map(set);
2225 /* Plug in "subs" for dimension "type", "pos" of "aff".
2227 * Let i be the dimension to replace and let "subs" be of the form
2229 * f/d
2231 * and "aff" of the form
2233 * (a i + g)/m
2235 * The result is
2237 * floor((a f + d g')/(m d))
2239 * where g' is the result of plugging in "subs" in each of the integer
2240 * divisions in g.
2242 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2243 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2245 isl_ctx *ctx;
2246 isl_int v;
2248 aff = isl_aff_cow(aff);
2249 if (!aff || !subs)
2250 return isl_aff_free(aff);
2252 ctx = isl_aff_get_ctx(aff);
2253 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2254 isl_die(ctx, isl_error_invalid,
2255 "spaces don't match", return isl_aff_free(aff));
2256 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2257 isl_die(ctx, isl_error_unsupported,
2258 "cannot handle divs yet", return isl_aff_free(aff));
2260 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2261 if (!aff->ls)
2262 return isl_aff_free(aff);
2264 aff->v = isl_vec_cow(aff->v);
2265 if (!aff->v)
2266 return isl_aff_free(aff);
2268 pos += isl_local_space_offset(aff->ls, type);
2270 isl_int_init(v);
2271 isl_int_set(v, aff->v->el[1 + pos]);
2272 isl_int_set_si(aff->v->el[1 + pos], 0);
2273 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2274 v, subs->v->el + 1, subs->v->size - 1);
2275 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2276 isl_int_clear(v);
2278 return aff;
2281 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2282 * expressions in "maff".
2284 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2285 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2286 __isl_keep isl_aff *subs)
2288 int i;
2290 maff = isl_multi_aff_cow(maff);
2291 if (!maff || !subs)
2292 return isl_multi_aff_free(maff);
2294 if (type == isl_dim_in)
2295 type = isl_dim_set;
2297 for (i = 0; i < maff->n; ++i) {
2298 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2299 if (!maff->p[i])
2300 return isl_multi_aff_free(maff);
2303 return maff;
2306 /* Plug in "subs" for dimension "type", "pos" of "pma".
2308 * pma is of the form
2310 * A_i(v) -> M_i(v)
2312 * while subs is of the form
2314 * v' = B_j(v) -> S_j
2316 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2317 * has a contribution in the result, in particular
2319 * C_ij(S_j) -> M_i(S_j)
2321 * Note that plugging in S_j in C_ij may also result in an empty set
2322 * and this contribution should simply be discarded.
2324 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2325 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2326 __isl_keep isl_pw_aff *subs)
2328 int i, j, n;
2329 isl_pw_multi_aff *res;
2331 if (!pma || !subs)
2332 return isl_pw_multi_aff_free(pma);
2334 n = pma->n * subs->n;
2335 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2337 for (i = 0; i < pma->n; ++i) {
2338 for (j = 0; j < subs->n; ++j) {
2339 isl_set *common;
2340 isl_multi_aff *res_ij;
2341 common = isl_set_intersect(
2342 isl_set_copy(pma->p[i].set),
2343 isl_set_copy(subs->p[j].set));
2344 common = isl_set_substitute(common,
2345 type, pos, subs->p[j].aff);
2346 if (isl_set_plain_is_empty(common)) {
2347 isl_set_free(common);
2348 continue;
2351 res_ij = isl_multi_aff_substitute(
2352 isl_multi_aff_copy(pma->p[i].maff),
2353 type, pos, subs->p[j].aff);
2355 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2359 isl_pw_multi_aff_free(pma);
2360 return res;
2363 /* Extend the local space of "dst" to include the divs
2364 * in the local space of "src".
2366 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2367 __isl_keep isl_aff *src)
2369 isl_ctx *ctx;
2370 int *exp1 = NULL;
2371 int *exp2 = NULL;
2372 isl_mat *div;
2374 if (!src || !dst)
2375 return isl_aff_free(dst);
2377 ctx = isl_aff_get_ctx(src);
2378 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2379 isl_die(ctx, isl_error_invalid,
2380 "spaces don't match", goto error);
2382 if (src->ls->div->n_row == 0)
2383 return dst;
2385 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2386 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2387 if (!exp1 || !exp2)
2388 goto error;
2390 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2391 dst = isl_aff_expand_divs(dst, div, exp2);
2392 free(exp1);
2393 free(exp2);
2395 return dst;
2396 error:
2397 free(exp1);
2398 free(exp2);
2399 return isl_aff_free(dst);
2402 /* Adjust the local spaces of the affine expressions in "maff"
2403 * such that they all have the save divs.
2405 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2406 __isl_take isl_multi_aff *maff)
2408 int i;
2410 if (!maff)
2411 return NULL;
2412 if (maff->n == 0)
2413 return maff;
2414 maff = isl_multi_aff_cow(maff);
2415 if (!maff)
2416 return NULL;
2418 for (i = 1; i < maff->n; ++i)
2419 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2420 for (i = 1; i < maff->n; ++i) {
2421 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2422 if (!maff->p[i])
2423 return isl_multi_aff_free(maff);
2426 return maff;
2429 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2431 aff = isl_aff_cow(aff);
2432 if (!aff)
2433 return NULL;
2435 aff->ls = isl_local_space_lift(aff->ls);
2436 if (!aff->ls)
2437 return isl_aff_free(aff);
2439 return aff;
2442 /* Lift "maff" to a space with extra dimensions such that the result
2443 * has no more existentially quantified variables.
2444 * If "ls" is not NULL, then *ls is assigned the local space that lies
2445 * at the basis of the lifting applied to "maff".
2447 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2448 __isl_give isl_local_space **ls)
2450 int i;
2451 isl_space *space;
2452 unsigned n_div;
2454 if (ls)
2455 *ls = NULL;
2457 if (!maff)
2458 return NULL;
2460 if (maff->n == 0) {
2461 if (ls) {
2462 isl_space *space = isl_multi_aff_get_domain_space(maff);
2463 *ls = isl_local_space_from_space(space);
2464 if (!*ls)
2465 return isl_multi_aff_free(maff);
2467 return maff;
2470 maff = isl_multi_aff_cow(maff);
2471 maff = isl_multi_aff_align_divs(maff);
2472 if (!maff)
2473 return NULL;
2475 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2476 space = isl_multi_aff_get_space(maff);
2477 space = isl_space_lift(isl_space_domain(space), n_div);
2478 space = isl_space_extend_domain_with_range(space,
2479 isl_multi_aff_get_space(maff));
2480 if (!space)
2481 return isl_multi_aff_free(maff);
2482 isl_space_free(maff->space);
2483 maff->space = space;
2485 if (ls) {
2486 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2487 if (!*ls)
2488 return isl_multi_aff_free(maff);
2491 for (i = 0; i < maff->n; ++i) {
2492 maff->p[i] = isl_aff_lift(maff->p[i]);
2493 if (!maff->p[i])
2494 goto error;
2497 return maff;
2498 error:
2499 if (ls)
2500 isl_local_space_free(*ls);
2501 return isl_multi_aff_free(maff);