substitute EXEEXT in bound_test.sh and pip_test.sh
[isl.git] / isl_aff.c
blob0f2102de8067436cfc83f678938516a89e7da3fc
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Universiteit Leiden
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
10 * and Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_local_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl/constraint.h>
20 #include <isl/seq.h>
21 #include <isl/set.h>
23 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
24 __isl_take isl_vec *v)
26 isl_aff *aff;
28 if (!ls || !v)
29 goto error;
31 aff = isl_calloc_type(v->ctx, struct isl_aff);
32 if (!aff)
33 goto error;
35 aff->ref = 1;
36 aff->ls = ls;
37 aff->v = v;
39 return aff;
40 error:
41 isl_local_space_free(ls);
42 isl_vec_free(v);
43 return NULL;
46 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
48 isl_ctx *ctx;
49 isl_vec *v;
50 unsigned total;
52 if (!ls)
53 return NULL;
55 ctx = isl_local_space_get_ctx(ls);
56 if (!isl_local_space_divs_known(ls))
57 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
58 goto error);
60 total = isl_local_space_dim(ls, isl_dim_all);
61 v = isl_vec_alloc(ctx, 1 + 1 + total);
62 return isl_aff_alloc_vec(ls, v);
63 error:
64 isl_local_space_free(ls);
65 return NULL;
68 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
70 isl_aff *aff;
72 aff = isl_aff_alloc(ls);
73 if (!aff)
74 return NULL;
76 isl_int_set_si(aff->v->el[0], 1);
77 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
79 return aff;
82 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
84 if (!aff)
85 return NULL;
87 aff->ref++;
88 return aff;
91 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
93 if (!aff)
94 return NULL;
96 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
97 isl_vec_copy(aff->v));
100 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
102 if (!aff)
103 return NULL;
105 if (aff->ref == 1)
106 return aff;
107 aff->ref--;
108 return isl_aff_dup(aff);
111 void *isl_aff_free(__isl_take isl_aff *aff)
113 if (!aff)
114 return NULL;
116 if (--aff->ref > 0)
117 return NULL;
119 isl_local_space_free(aff->ls);
120 isl_vec_free(aff->v);
122 free(aff);
124 return NULL;
127 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
129 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
132 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
134 return aff ? isl_local_space_dim(aff->ls, type) : 0;
137 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
139 return aff ? isl_local_space_get_dim(aff->ls) : NULL;
142 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
144 return aff ? isl_local_space_copy(aff->ls) : NULL;
147 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
148 enum isl_dim_type type, unsigned pos)
150 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
153 __isl_give isl_aff *isl_aff_reset_dim(__isl_take isl_aff *aff,
154 __isl_take isl_dim *dim)
156 aff = isl_aff_cow(aff);
157 if (!aff || !dim)
158 goto error;
160 aff->ls = isl_local_space_reset_dim(aff->ls, dim);
161 if (!aff->ls)
162 return isl_aff_free(aff);
164 return aff;
165 error:
166 isl_aff_free(aff);
167 isl_dim_free(dim);
168 return NULL;
171 /* Reorder the coefficients of the affine expression based
172 * on the given reodering.
173 * The reordering r is assumed to have been extended with the local
174 * variables.
176 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
177 __isl_take isl_reordering *r, int n_div)
179 isl_vec *res;
180 int i;
182 if (!vec || !r)
183 goto error;
185 res = isl_vec_alloc(vec->ctx, 2 + isl_dim_total(r->dim) + n_div);
186 isl_seq_cpy(res->el, vec->el, 2);
187 isl_seq_clr(res->el + 2, res->size - 2);
188 for (i = 0; i < r->len; ++i)
189 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
191 isl_reordering_free(r);
192 isl_vec_free(vec);
193 return res;
194 error:
195 isl_vec_free(vec);
196 isl_reordering_free(r);
197 return NULL;
200 /* Reorder the dimensions of "aff" according to the given reordering.
202 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
203 __isl_take isl_reordering *r)
205 aff = isl_aff_cow(aff);
206 if (!aff)
207 goto error;
209 r = isl_reordering_extend(r, aff->ls->div->n_row);
210 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
211 aff->ls->div->n_row);
212 aff->ls = isl_local_space_realign(aff->ls, r);
214 if (!aff->v || !aff->ls)
215 return isl_aff_free(aff);
217 return aff;
218 error:
219 isl_aff_free(aff);
220 isl_reordering_free(r);
221 return NULL;
224 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
226 if (!aff)
227 return -1;
229 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
232 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
234 int equal;
236 if (!aff1 || !aff2)
237 return -1;
239 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
240 if (equal < 0 || !equal)
241 return equal;
243 return isl_vec_is_equal(aff1->v, aff2->v);
246 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
248 if (!aff)
249 return -1;
250 isl_int_set(*v, aff->v->el[0]);
251 return 0;
254 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
256 if (!aff)
257 return -1;
258 isl_int_set(*v, aff->v->el[1]);
259 return 0;
262 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
263 enum isl_dim_type type, int pos, isl_int *v)
265 if (!aff)
266 return -1;
268 if (pos >= isl_local_space_dim(aff->ls, type))
269 isl_die(aff->v->ctx, isl_error_invalid,
270 "position out of bounds", return -1);
272 pos += isl_local_space_offset(aff->ls, type);
273 isl_int_set(*v, aff->v->el[1 + pos]);
275 return 0;
278 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
280 aff = isl_aff_cow(aff);
281 if (!aff)
282 return NULL;
284 aff->v = isl_vec_cow(aff->v);
285 if (!aff->v)
286 return isl_aff_free(aff);
288 isl_int_set(aff->v->el[0], v);
290 return aff;
293 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
295 aff = isl_aff_cow(aff);
296 if (!aff)
297 return NULL;
299 aff->v = isl_vec_cow(aff->v);
300 if (!aff->v)
301 return isl_aff_free(aff);
303 isl_int_set(aff->v->el[1], v);
305 return aff;
308 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
310 if (isl_int_is_zero(v))
311 return aff;
313 aff = isl_aff_cow(aff);
314 if (!aff)
315 return NULL;
317 aff->v = isl_vec_cow(aff->v);
318 if (!aff->v)
319 return isl_aff_free(aff);
321 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
323 return aff;
326 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
328 isl_int t;
330 isl_int_init(t);
331 isl_int_set_si(t, v);
332 aff = isl_aff_add_constant(aff, t);
333 isl_int_clear(t);
335 return aff;
338 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
340 aff = isl_aff_cow(aff);
341 if (!aff)
342 return NULL;
344 aff->v = isl_vec_cow(aff->v);
345 if (!aff->v)
346 return isl_aff_free(aff);
348 isl_int_set_si(aff->v->el[1], v);
350 return aff;
353 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
354 enum isl_dim_type type, int pos, isl_int v)
356 if (!aff)
357 return NULL;
359 if (pos >= isl_local_space_dim(aff->ls, type))
360 isl_die(aff->v->ctx, isl_error_invalid,
361 "position out of bounds", return isl_aff_free(aff));
363 aff = isl_aff_cow(aff);
364 if (!aff)
365 return NULL;
367 aff->v = isl_vec_cow(aff->v);
368 if (!aff->v)
369 return isl_aff_free(aff);
371 pos += isl_local_space_offset(aff->ls, type);
372 isl_int_set(aff->v->el[1 + pos], v);
374 return aff;
377 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
378 enum isl_dim_type type, int pos, int v)
380 if (!aff)
381 return NULL;
383 if (pos >= isl_local_space_dim(aff->ls, type))
384 isl_die(aff->v->ctx, isl_error_invalid,
385 "position out of bounds", return isl_aff_free(aff));
387 aff = isl_aff_cow(aff);
388 if (!aff)
389 return NULL;
391 aff->v = isl_vec_cow(aff->v);
392 if (!aff->v)
393 return isl_aff_free(aff);
395 pos += isl_local_space_offset(aff->ls, type);
396 isl_int_set_si(aff->v->el[1 + pos], v);
398 return aff;
401 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
402 enum isl_dim_type type, int pos, isl_int v)
404 if (!aff)
405 return NULL;
407 if (pos >= isl_local_space_dim(aff->ls, type))
408 isl_die(aff->v->ctx, isl_error_invalid,
409 "position out of bounds", return isl_aff_free(aff));
411 aff = isl_aff_cow(aff);
412 if (!aff)
413 return NULL;
415 aff->v = isl_vec_cow(aff->v);
416 if (!aff->v)
417 return isl_aff_free(aff);
419 pos += isl_local_space_offset(aff->ls, type);
420 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
422 return aff;
425 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
426 enum isl_dim_type type, int pos, int v)
428 isl_int t;
430 isl_int_init(t);
431 isl_int_set_si(t, v);
432 aff = isl_aff_add_coefficient(aff, type, pos, t);
433 isl_int_clear(t);
435 return aff;
438 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
440 if (!aff)
441 return NULL;
443 return isl_local_space_get_div(aff->ls, pos);
446 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
448 aff = isl_aff_cow(aff);
449 if (!aff)
450 return NULL;
451 aff->v = isl_vec_cow(aff->v);
452 if (!aff->v)
453 return isl_aff_free(aff);
455 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
457 return aff;
460 /* Given f, return floor(f).
461 * If f is an integer expression, then just return f.
462 * Otherwise, create a new div d = [f] and return the expression d.
464 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
466 int size;
467 isl_ctx *ctx;
469 if (!aff)
470 return NULL;
472 if (isl_int_is_one(aff->v->el[0]))
473 return aff;
475 aff = isl_aff_cow(aff);
476 if (!aff)
477 return NULL;
479 aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
480 if (!aff->ls)
481 return isl_aff_free(aff);
483 ctx = isl_aff_get_ctx(aff);
484 size = aff->v->size;
485 isl_vec_free(aff->v);
486 aff->v = isl_vec_alloc(ctx, size + 1);
487 aff->v = isl_vec_clr(aff->v);
488 if (!aff->v)
489 return isl_aff_free(aff);
490 isl_int_set_si(aff->v->el[0], 1);
491 isl_int_set_si(aff->v->el[size], 1);
493 return aff;
496 /* Given f, return ceil(f).
497 * If f is an integer expression, then just return f.
498 * Otherwise, create a new div d = [-f] and return the expression -d.
500 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
502 if (!aff)
503 return NULL;
505 if (isl_int_is_one(aff->v->el[0]))
506 return aff;
508 aff = isl_aff_neg(aff);
509 aff = isl_aff_floor(aff);
510 aff = isl_aff_neg(aff);
512 return aff;
515 /* Apply the expansion computed by isl_merge_divs.
516 * The expansion itself is given by "exp" while the resulting
517 * list of divs is given by "div".
519 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
520 __isl_take isl_mat *div, int *exp)
522 int i, j;
523 int old_n_div;
524 int new_n_div;
525 int offset;
527 aff = isl_aff_cow(aff);
528 if (!aff || !div)
529 goto error;
531 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
532 new_n_div = isl_mat_rows(div);
533 if (new_n_div < old_n_div)
534 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
535 "not an expansion", goto error);
537 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
538 if (!aff->v)
539 goto error;
541 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
542 j = old_n_div - 1;
543 for (i = new_n_div - 1; i >= 0; --i) {
544 if (j >= 0 && exp[j] == i) {
545 if (i != j)
546 isl_int_swap(aff->v->el[offset + i],
547 aff->v->el[offset + j]);
548 j--;
549 } else
550 isl_int_set_si(aff->v->el[offset + i], 0);
553 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
554 if (!aff->ls)
555 goto error;
556 isl_mat_free(div);
557 return aff;
558 error:
559 isl_aff_free(aff);
560 isl_mat_free(div);
561 return NULL;
564 /* Add two affine expressions that live in the same local space.
566 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
567 __isl_take isl_aff *aff2)
569 isl_int gcd, f;
571 aff1 = isl_aff_cow(aff1);
572 if (!aff1 || !aff2)
573 goto error;
575 aff1->v = isl_vec_cow(aff1->v);
576 if (!aff1->v)
577 goto error;
579 isl_int_init(gcd);
580 isl_int_init(f);
581 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
582 isl_int_divexact(f, aff2->v->el[0], gcd);
583 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
584 isl_int_divexact(f, aff1->v->el[0], gcd);
585 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
586 isl_int_divexact(f, aff2->v->el[0], gcd);
587 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
588 isl_int_clear(f);
589 isl_int_clear(gcd);
591 isl_aff_free(aff2);
592 return aff1;
593 error:
594 isl_aff_free(aff1);
595 isl_aff_free(aff2);
596 return NULL;
599 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
600 __isl_take isl_aff *aff2)
602 isl_ctx *ctx;
603 int *exp1 = NULL;
604 int *exp2 = NULL;
605 isl_mat *div;
607 if (!aff1 || !aff2)
608 goto error;
610 ctx = isl_aff_get_ctx(aff1);
611 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
612 isl_die(ctx, isl_error_invalid,
613 "spaces don't match", goto error);
615 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
616 return add_expanded(aff1, aff2);
618 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
619 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
620 if (!exp1 || !exp2)
621 goto error;
623 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
624 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
625 aff2 = isl_aff_expand_divs(aff2, div, exp2);
626 free(exp1);
627 free(exp2);
629 return add_expanded(aff1, aff2);
630 error:
631 free(exp1);
632 free(exp2);
633 isl_aff_free(aff1);
634 isl_aff_free(aff2);
635 return NULL;
638 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
639 __isl_take isl_aff *aff2)
641 return isl_aff_add(aff1, isl_aff_neg(aff2));
644 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
646 isl_int gcd;
648 if (isl_int_is_one(f))
649 return aff;
651 aff = isl_aff_cow(aff);
652 if (!aff)
653 return NULL;
654 aff->v = isl_vec_cow(aff->v);
655 if (!aff->v)
656 return isl_aff_free(aff);
658 isl_int_init(gcd);
659 isl_int_gcd(gcd, aff->v->el[0], f);
660 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
661 isl_int_divexact(gcd, f, gcd);
662 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
663 isl_int_clear(gcd);
665 return aff;
668 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
670 isl_int gcd;
672 if (isl_int_is_one(f))
673 return aff;
675 aff = isl_aff_cow(aff);
676 if (!aff)
677 return NULL;
678 aff->v = isl_vec_cow(aff->v);
679 if (!aff->v)
680 return isl_aff_free(aff);
682 isl_int_init(gcd);
683 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
684 isl_int_gcd(gcd, gcd, f);
685 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
686 isl_int_divexact(gcd, f, gcd);
687 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
688 isl_int_clear(gcd);
690 return aff;
693 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
695 isl_int v;
697 if (f == 1)
698 return aff;
700 isl_int_init(v);
701 isl_int_set_ui(v, f);
702 aff = isl_aff_scale_down(aff, v);
703 isl_int_clear(v);
705 return aff;
708 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
709 enum isl_dim_type type, unsigned pos, const char *s)
711 aff = isl_aff_cow(aff);
712 if (!aff)
713 return NULL;
714 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
715 if (!aff->ls)
716 return isl_aff_free(aff);
718 return aff;
721 /* Exploit the equalities in "eq" to simplify the affine expression
722 * and the expressions of the integer divisions in the local space.
723 * The integer divisions in this local space are assumed to appear
724 * as regular dimensions in "eq".
726 static __isl_give isl_aff *isl_aff_substitute_equalities(
727 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
729 int i, j;
730 unsigned total;
731 unsigned n_div;
733 if (!eq)
734 goto error;
735 if (eq->n_eq == 0) {
736 isl_basic_set_free(eq);
737 return aff;
740 aff = isl_aff_cow(aff);
741 if (!aff)
742 goto error;
744 aff->ls = isl_local_space_substitute_equalities(aff->ls,
745 isl_basic_set_copy(eq));
746 if (!aff->ls)
747 goto error;
749 total = 1 + isl_dim_total(eq->dim);
750 n_div = eq->n_div;
751 for (i = 0; i < eq->n_eq; ++i) {
752 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
753 if (j < 0 || j == 0 || j >= total)
754 continue;
756 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
757 &aff->v->el[0]);
760 isl_basic_set_free(eq);
761 return aff;
762 error:
763 isl_basic_set_free(eq);
764 isl_aff_free(aff);
765 return NULL;
768 /* Look for equalities among the variables shared by context and aff
769 * and the integer divisions of aff, if any.
770 * The equalities are then used to eliminate coefficients and/or integer
771 * divisions from aff.
773 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
774 __isl_take isl_set *context)
776 isl_basic_set *hull;
777 int n_div;
779 if (!aff)
780 goto error;
781 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
782 if (n_div > 0) {
783 isl_basic_set *bset;
784 context = isl_set_add_dims(context, isl_dim_set, n_div);
785 bset = isl_basic_set_from_local_space(
786 isl_aff_get_local_space(aff));
787 bset = isl_basic_set_lift(bset);
788 bset = isl_basic_set_flatten(bset);
789 context = isl_set_intersect(context,
790 isl_set_from_basic_set(bset));
793 hull = isl_set_affine_hull(context);
794 return isl_aff_substitute_equalities(aff, hull);
795 error:
796 isl_aff_free(aff);
797 isl_set_free(context);
798 return NULL;
801 /* Return a basic set containing those elements in the space
802 * of aff where it is non-negative.
804 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
806 isl_constraint *ineq;
808 ineq = isl_inequality_from_aff(aff);
810 return isl_basic_set_from_constraint(ineq);
813 /* Return a basic set containing those elements in the space
814 * of aff where it is zero.
816 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
818 isl_constraint *ineq;
820 ineq = isl_equality_from_aff(aff);
822 return isl_basic_set_from_constraint(ineq);
825 /* Return a basic set containing those elements in the shared space
826 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
828 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
829 __isl_take isl_aff *aff2)
831 aff1 = isl_aff_sub(aff1, aff2);
833 return isl_aff_nonneg_basic_set(aff1);
836 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
837 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
839 aff1 = isl_aff_add(aff1, aff2);
840 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
841 return aff1;
844 int isl_aff_is_empty(__isl_keep isl_aff *aff)
846 if (!aff)
847 return -1;
849 return 0;
852 /* Set active[i] to 1 if the dimension at position i is involved
853 * in the affine expression.
855 static int set_active(__isl_keep isl_aff *aff, int *active)
857 int i, j;
858 unsigned total;
859 unsigned offset;
861 if (!aff || !active)
862 return -1;
864 total = aff->v->size - 2;
865 for (i = 0; i < total; ++i)
866 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
868 offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
869 for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
870 if (!active[offset + i])
871 continue;
872 for (j = 0; j < total; ++j)
873 active[j] |=
874 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
877 return 0;
880 /* Check whether the given affine expression has non-zero coefficient
881 * for any dimension in the given range or if any of these dimensions
882 * appear with non-zero coefficients in any of the integer divisions
883 * involved in the affine expression.
885 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
886 enum isl_dim_type type, unsigned first, unsigned n)
888 int i;
889 isl_ctx *ctx;
890 int *active = NULL;
891 int involves = 0;
893 if (!aff)
894 return -1;
895 if (n == 0)
896 return 0;
898 ctx = isl_aff_get_ctx(aff);
899 if (first + n > isl_aff_dim(aff, type))
900 isl_die(ctx, isl_error_invalid,
901 "range out of bounds", return -1);
903 active = isl_calloc_array(ctx, int,
904 isl_local_space_dim(aff->ls, isl_dim_all));
905 if (set_active(aff, active) < 0)
906 goto error;
908 first += isl_local_space_offset(aff->ls, type) - 1;
909 for (i = 0; i < n; ++i)
910 if (active[first + i]) {
911 involves = 1;
912 break;
915 free(active);
917 return involves;
918 error:
919 free(active);
920 return -1;
923 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
924 enum isl_dim_type type, unsigned first, unsigned n)
926 isl_ctx *ctx;
928 if (!aff)
929 return NULL;
930 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
931 return aff;
933 ctx = isl_aff_get_ctx(aff);
934 if (first + n > isl_aff_dim(aff, type))
935 isl_die(ctx, isl_error_invalid, "range out of bounds",
936 return isl_aff_free(aff));
938 aff = isl_aff_cow(aff);
939 if (!aff)
940 return NULL;
942 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
943 if (!aff->ls)
944 return isl_aff_free(aff);
946 first += 1 + isl_local_space_offset(aff->ls, type);
947 aff->v = isl_vec_drop_els(aff->v, first, n);
948 if (!aff->v)
949 return isl_aff_free(aff);
951 return aff;
954 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
955 enum isl_dim_type type, unsigned first, unsigned n)
957 isl_ctx *ctx;
959 if (!aff)
960 return NULL;
961 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
962 return aff;
964 ctx = isl_aff_get_ctx(aff);
965 if (first > isl_aff_dim(aff, type))
966 isl_die(ctx, isl_error_invalid, "position out of bounds",
967 return isl_aff_free(aff));
969 aff = isl_aff_cow(aff);
970 if (!aff)
971 return NULL;
973 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
974 if (!aff->ls)
975 return isl_aff_free(aff);
977 first += 1 + isl_local_space_offset(aff->ls, type);
978 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
979 if (!aff->v)
980 return isl_aff_free(aff);
982 return aff;
985 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
986 enum isl_dim_type type, unsigned n)
988 unsigned pos;
990 pos = isl_aff_dim(aff, type);
992 return isl_aff_insert_dims(aff, type, pos, n);
995 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
996 enum isl_dim_type type, unsigned n)
998 unsigned pos;
1000 pos = isl_pw_aff_dim(pwaff, type);
1002 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1005 #undef PW
1006 #define PW isl_pw_aff
1007 #undef EL
1008 #define EL isl_aff
1009 #undef EL_IS_ZERO
1010 #define EL_IS_ZERO is_empty
1011 #undef ZERO
1012 #define ZERO empty
1013 #undef IS_ZERO
1014 #define IS_ZERO is_empty
1015 #undef FIELD
1016 #define FIELD aff
1018 #define NO_EVAL
1019 #define NO_OPT
1020 #define NO_MOVE_DIMS
1021 #define NO_LIFT
1022 #define NO_MORPH
1024 #include <isl_pw_templ.c>
1026 /* Compute a piecewise quasi-affine expression with a domain that
1027 * is the union of those of pwaff1 and pwaff2 and such that on each
1028 * cell, the quasi-affine expression is the maximum of those of pwaff1
1029 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1030 * cell, then the associated expression is the defined one.
1032 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1033 __isl_take isl_pw_aff *pwaff2)
1035 int i, j, n;
1036 isl_pw_aff *res;
1037 isl_ctx *ctx;
1038 isl_set *set;
1040 if (!pwaff1 || !pwaff2)
1041 goto error;
1043 ctx = isl_dim_get_ctx(pwaff1->dim);
1044 if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
1045 isl_die(ctx, isl_error_invalid,
1046 "arguments should live in same space", goto error);
1048 if (isl_pw_aff_is_empty(pwaff1)) {
1049 isl_pw_aff_free(pwaff1);
1050 return pwaff2;
1053 if (isl_pw_aff_is_empty(pwaff2)) {
1054 isl_pw_aff_free(pwaff2);
1055 return pwaff1;
1058 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1059 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
1061 for (i = 0; i < pwaff1->n; ++i) {
1062 set = isl_set_copy(pwaff1->p[i].set);
1063 for (j = 0; j < pwaff2->n; ++j) {
1064 struct isl_set *common;
1065 isl_set *ge;
1067 common = isl_set_intersect(
1068 isl_set_copy(pwaff1->p[i].set),
1069 isl_set_copy(pwaff2->p[j].set));
1070 ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
1071 isl_aff_copy(pwaff2->p[j].aff),
1072 isl_aff_copy(pwaff1->p[i].aff)));
1073 ge = isl_set_intersect(common, ge);
1074 if (isl_set_plain_is_empty(ge)) {
1075 isl_set_free(ge);
1076 continue;
1078 set = isl_set_subtract(set, isl_set_copy(ge));
1080 res = isl_pw_aff_add_piece(res, ge,
1081 isl_aff_copy(pwaff2->p[j].aff));
1083 res = isl_pw_aff_add_piece(res, set,
1084 isl_aff_copy(pwaff1->p[i].aff));
1087 for (j = 0; j < pwaff2->n; ++j) {
1088 set = isl_set_copy(pwaff2->p[j].set);
1089 for (i = 0; i < pwaff1->n; ++i)
1090 set = isl_set_subtract(set,
1091 isl_set_copy(pwaff1->p[i].set));
1092 res = isl_pw_aff_add_piece(res, set,
1093 isl_aff_copy(pwaff2->p[j].aff));
1096 isl_pw_aff_free(pwaff1);
1097 isl_pw_aff_free(pwaff2);
1099 return res;
1100 error:
1101 isl_pw_aff_free(pwaff1);
1102 isl_pw_aff_free(pwaff2);
1103 return NULL;
1106 /* Construct a map with as domain the domain of pwaff and
1107 * one-dimensional range corresponding to the affine expressions.
1109 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1111 int i;
1112 isl_dim *dim;
1113 isl_map *map;
1115 if (!pwaff)
1116 return NULL;
1118 dim = isl_pw_aff_get_dim(pwaff);
1119 dim = isl_dim_from_domain(dim);
1120 dim = isl_dim_add(dim, isl_dim_out, 1);
1121 map = isl_map_empty(dim);
1123 for (i = 0; i < pwaff->n; ++i) {
1124 isl_basic_map *bmap;
1125 isl_map *map_i;
1127 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1128 map_i = isl_map_from_basic_map(bmap);
1129 map_i = isl_map_intersect_domain(map_i,
1130 isl_set_copy(pwaff->p[i].set));
1131 map = isl_map_union_disjoint(map, map_i);
1134 isl_pw_aff_free(pwaff);
1136 return map;
1139 /* Return a set containing those elements in the domain
1140 * of pwaff where it is non-negative.
1142 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1144 int i;
1145 isl_set *set;
1147 if (!pwaff)
1148 return NULL;
1150 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1152 for (i = 0; i < pwaff->n; ++i) {
1153 isl_basic_set *bset;
1154 isl_set *set_i;
1156 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1157 set_i = isl_set_from_basic_set(bset);
1158 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1159 set = isl_set_union_disjoint(set, set_i);
1162 isl_pw_aff_free(pwaff);
1164 return set;
1167 /* Return a set containing those elements in the domain
1168 * of pwaff where it is zero.
1170 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1172 int i;
1173 isl_set *set;
1175 if (!pwaff)
1176 return NULL;
1178 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1180 for (i = 0; i < pwaff->n; ++i) {
1181 isl_basic_set *bset;
1182 isl_set *set_i;
1184 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1185 set_i = isl_set_from_basic_set(bset);
1186 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1187 set = isl_set_union_disjoint(set, set_i);
1190 isl_pw_aff_free(pwaff);
1192 return set;
1195 /* Return a set containing those elements in the shared domain
1196 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1198 * We compute the difference on the shared domain and then construct
1199 * the set of values where this difference is non-negative.
1200 * If strict is set, we first subtract 1 from the difference.
1201 * If equal is set, we only return the elements where pwaff1 and pwaff2
1202 * are equal.
1204 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1205 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1207 isl_set *set1, *set2;
1209 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1210 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1211 set1 = isl_set_intersect(set1, set2);
1212 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1213 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1214 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1216 if (strict) {
1217 isl_dim *dim = isl_set_get_dim(set1);
1218 isl_aff *aff;
1219 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1220 aff = isl_aff_add_constant_si(aff, -1);
1221 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1222 } else
1223 isl_set_free(set1);
1225 if (equal)
1226 return isl_pw_aff_zero_set(pwaff1);
1227 return isl_pw_aff_nonneg_set(pwaff1);
1230 /* Return a set containing those elements in the shared domain
1231 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1233 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1234 __isl_take isl_pw_aff *pwaff2)
1236 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1239 /* Return a set containing those elements in the shared domain
1240 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1242 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1243 __isl_take isl_pw_aff *pwaff2)
1245 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1248 /* Return a set containing those elements in the shared domain
1249 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1251 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1252 __isl_take isl_pw_aff *pwaff2)
1254 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1257 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1258 __isl_take isl_pw_aff *pwaff2)
1260 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1263 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1264 isl_int v)
1266 int i;
1268 if (isl_int_is_one(v))
1269 return pwaff;
1270 if (!isl_int_is_pos(v))
1271 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1272 "factor needs to be positive",
1273 return isl_pw_aff_free(pwaff));
1274 pwaff = isl_pw_aff_cow(pwaff);
1275 if (!pwaff)
1276 return NULL;
1277 if (pwaff->n == 0)
1278 return pwaff;
1280 for (i = 0; i < pwaff->n; ++i) {
1281 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1282 if (!pwaff->p[i].aff)
1283 return isl_pw_aff_free(pwaff);
1286 return pwaff;
1289 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1291 int i;
1293 pwaff = isl_pw_aff_cow(pwaff);
1294 if (!pwaff)
1295 return NULL;
1296 if (pwaff->n == 0)
1297 return pwaff;
1299 for (i = 0; i < pwaff->n; ++i) {
1300 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1301 if (!pwaff->p[i].aff)
1302 return isl_pw_aff_free(pwaff);
1305 return pwaff;
1308 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1310 int i;
1312 pwaff = isl_pw_aff_cow(pwaff);
1313 if (!pwaff)
1314 return NULL;
1315 if (pwaff->n == 0)
1316 return pwaff;
1318 for (i = 0; i < pwaff->n; ++i) {
1319 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1320 if (!pwaff->p[i].aff)
1321 return isl_pw_aff_free(pwaff);
1324 return pwaff;
1327 /* Return an affine expression that is equal to pwaff_true for elements
1328 * in "cond" and to pwaff_false for elements not in "cond".
1329 * That is, return cond ? pwaff_true : pwaff_false;
1331 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1332 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1334 isl_set *comp;
1336 comp = isl_set_complement(isl_set_copy(cond));
1337 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1338 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1340 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);