add isl_pw_aff_lt_set and isl_pw_aff_gt_set
[isl.git] / isl_aff.c
blob69ab36e15ad218218383727e68afe0cc177343da
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 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
155 if (!aff)
156 return -1;
158 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
161 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
163 int equal;
165 if (!aff1 || !aff2)
166 return -1;
168 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
169 if (equal < 0 || !equal)
170 return equal;
172 return isl_vec_is_equal(aff1->v, aff2->v);
175 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
177 if (!aff)
178 return -1;
179 isl_int_set(*v, aff->v->el[0]);
180 return 0;
183 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
185 if (!aff)
186 return -1;
187 isl_int_set(*v, aff->v->el[1]);
188 return 0;
191 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
192 enum isl_dim_type type, int pos, isl_int *v)
194 if (!aff)
195 return -1;
197 if (pos >= isl_local_space_dim(aff->ls, type))
198 isl_die(aff->v->ctx, isl_error_invalid,
199 "position out of bounds", return -1);
201 pos += isl_local_space_offset(aff->ls, type);
202 isl_int_set(*v, aff->v->el[1 + pos]);
204 return 0;
207 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
209 aff = isl_aff_cow(aff);
210 if (!aff)
211 return NULL;
213 aff->v = isl_vec_cow(aff->v);
214 if (!aff->v)
215 return isl_aff_free(aff);
217 isl_int_set(aff->v->el[0], v);
219 return aff;
222 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
224 aff = isl_aff_cow(aff);
225 if (!aff)
226 return NULL;
228 aff->v = isl_vec_cow(aff->v);
229 if (!aff->v)
230 return isl_aff_free(aff);
232 isl_int_set(aff->v->el[1], v);
234 return aff;
237 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
239 if (isl_int_is_zero(v))
240 return aff;
242 aff = isl_aff_cow(aff);
243 if (!aff)
244 return NULL;
246 aff->v = isl_vec_cow(aff->v);
247 if (!aff->v)
248 return isl_aff_free(aff);
250 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
252 return aff;
255 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
257 isl_int t;
259 isl_int_init(t);
260 isl_int_set_si(t, v);
261 aff = isl_aff_add_constant(aff, t);
262 isl_int_clear(t);
264 return aff;
267 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
269 aff = isl_aff_cow(aff);
270 if (!aff)
271 return NULL;
273 aff->v = isl_vec_cow(aff->v);
274 if (!aff->v)
275 return isl_aff_free(aff);
277 isl_int_set_si(aff->v->el[1], v);
279 return aff;
282 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
283 enum isl_dim_type type, int pos, isl_int v)
285 if (!aff)
286 return NULL;
288 if (pos >= isl_local_space_dim(aff->ls, type))
289 isl_die(aff->v->ctx, isl_error_invalid,
290 "position out of bounds", return isl_aff_free(aff));
292 aff = isl_aff_cow(aff);
293 if (!aff)
294 return NULL;
296 aff->v = isl_vec_cow(aff->v);
297 if (!aff->v)
298 return isl_aff_free(aff);
300 pos += isl_local_space_offset(aff->ls, type);
301 isl_int_set(aff->v->el[1 + pos], v);
303 return aff;
306 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
307 enum isl_dim_type type, int pos, int v)
309 if (!aff)
310 return NULL;
312 if (pos >= isl_local_space_dim(aff->ls, type))
313 isl_die(aff->v->ctx, isl_error_invalid,
314 "position out of bounds", return isl_aff_free(aff));
316 aff = isl_aff_cow(aff);
317 if (!aff)
318 return NULL;
320 aff->v = isl_vec_cow(aff->v);
321 if (!aff->v)
322 return isl_aff_free(aff);
324 pos += isl_local_space_offset(aff->ls, type);
325 isl_int_set_si(aff->v->el[1 + pos], v);
327 return aff;
330 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
331 enum isl_dim_type type, int pos, isl_int v)
333 if (!aff)
334 return NULL;
336 if (pos >= isl_local_space_dim(aff->ls, type))
337 isl_die(aff->v->ctx, isl_error_invalid,
338 "position out of bounds", return isl_aff_free(aff));
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 pos += isl_local_space_offset(aff->ls, type);
349 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
351 return aff;
354 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
355 enum isl_dim_type type, int pos, int v)
357 isl_int t;
359 isl_int_init(t);
360 isl_int_set_si(t, v);
361 aff = isl_aff_add_coefficient(aff, type, pos, t);
362 isl_int_clear(t);
364 return aff;
367 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
369 if (!aff)
370 return NULL;
372 return isl_local_space_get_div(aff->ls, pos);
375 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
377 aff = isl_aff_cow(aff);
378 if (!aff)
379 return NULL;
380 aff->v = isl_vec_cow(aff->v);
381 if (!aff->v)
382 return isl_aff_free(aff);
384 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
386 return aff;
389 /* Given f, return floor(f).
390 * If f is an integer expression, then just return f.
391 * Otherwise, create a new div d = [f] and return the expression d.
393 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
395 int size;
396 isl_ctx *ctx;
398 if (!aff)
399 return NULL;
401 if (isl_int_is_one(aff->v->el[0]))
402 return aff;
404 aff = isl_aff_cow(aff);
405 if (!aff)
406 return NULL;
408 aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
409 if (!aff->ls)
410 return isl_aff_free(aff);
412 ctx = isl_aff_get_ctx(aff);
413 size = aff->v->size;
414 isl_vec_free(aff->v);
415 aff->v = isl_vec_alloc(ctx, size + 1);
416 aff->v = isl_vec_clr(aff->v);
417 if (!aff->v)
418 return isl_aff_free(aff);
419 isl_int_set_si(aff->v->el[0], 1);
420 isl_int_set_si(aff->v->el[size], 1);
422 return aff;
425 /* Given f, return ceil(f).
426 * If f is an integer expression, then just return f.
427 * Otherwise, create a new div d = [-f] and return the expression -d.
429 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
431 if (!aff)
432 return NULL;
434 if (isl_int_is_one(aff->v->el[0]))
435 return aff;
437 aff = isl_aff_neg(aff);
438 aff = isl_aff_floor(aff);
439 aff = isl_aff_neg(aff);
441 return aff;
444 /* Apply the expansion computed by isl_merge_divs.
445 * The expansion itself is given by "exp" while the resulting
446 * list of divs is given by "div".
448 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
449 __isl_take isl_mat *div, int *exp)
451 int i, j;
452 int old_n_div;
453 int new_n_div;
454 int offset;
456 aff = isl_aff_cow(aff);
457 if (!aff || !div)
458 goto error;
460 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
461 new_n_div = isl_mat_rows(div);
462 if (new_n_div < old_n_div)
463 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
464 "not an expansion", goto error);
466 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
467 if (!aff->v)
468 goto error;
470 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
471 j = old_n_div - 1;
472 for (i = new_n_div - 1; i >= 0; --i) {
473 if (j >= 0 && exp[j] == i) {
474 if (i != j)
475 isl_int_swap(aff->v->el[offset + i],
476 aff->v->el[offset + j]);
477 j--;
478 } else
479 isl_int_set_si(aff->v->el[offset + i], 0);
482 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
483 if (!aff->ls)
484 goto error;
485 isl_mat_free(div);
486 return aff;
487 error:
488 isl_aff_free(aff);
489 isl_mat_free(div);
490 return NULL;
493 /* Add two affine expressions that live in the same local space.
495 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
496 __isl_take isl_aff *aff2)
498 isl_int gcd, f;
500 aff1 = isl_aff_cow(aff1);
501 if (!aff1 || !aff2)
502 goto error;
504 aff1->v = isl_vec_cow(aff1->v);
505 if (!aff1->v)
506 goto error;
508 isl_int_init(gcd);
509 isl_int_init(f);
510 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
511 isl_int_divexact(f, aff2->v->el[0], gcd);
512 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
513 isl_int_divexact(f, aff1->v->el[0], gcd);
514 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
515 isl_int_divexact(f, aff2->v->el[0], gcd);
516 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
517 isl_int_clear(f);
518 isl_int_clear(gcd);
520 isl_aff_free(aff2);
521 return aff1;
522 error:
523 isl_aff_free(aff1);
524 isl_aff_free(aff2);
525 return NULL;
528 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
529 __isl_take isl_aff *aff2)
531 isl_ctx *ctx;
532 int *exp1 = NULL;
533 int *exp2 = NULL;
534 isl_mat *div;
536 if (!aff1 || !aff2)
537 goto error;
539 ctx = isl_aff_get_ctx(aff1);
540 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
541 isl_die(ctx, isl_error_invalid,
542 "spaces don't match", goto error);
544 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
545 return add_expanded(aff1, aff2);
547 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
548 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
549 if (!exp1 || !exp2)
550 goto error;
552 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
553 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
554 aff2 = isl_aff_expand_divs(aff2, div, exp2);
555 free(exp1);
556 free(exp2);
558 return add_expanded(aff1, aff2);
559 error:
560 free(exp1);
561 free(exp2);
562 isl_aff_free(aff1);
563 isl_aff_free(aff2);
564 return NULL;
567 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
568 __isl_take isl_aff *aff2)
570 return isl_aff_add(aff1, isl_aff_neg(aff2));
573 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
575 isl_int gcd;
577 if (isl_int_is_one(f))
578 return aff;
580 aff = isl_aff_cow(aff);
581 if (!aff)
582 return NULL;
583 aff->v = isl_vec_cow(aff->v);
584 if (!aff->v)
585 return isl_aff_free(aff);
587 isl_int_init(gcd);
588 isl_int_gcd(gcd, aff->v->el[0], f);
589 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
590 isl_int_divexact(gcd, f, gcd);
591 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
592 isl_int_clear(gcd);
594 return aff;
597 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
599 isl_int gcd;
601 if (isl_int_is_one(f))
602 return aff;
604 aff = isl_aff_cow(aff);
605 if (!aff)
606 return NULL;
607 aff->v = isl_vec_cow(aff->v);
608 if (!aff->v)
609 return isl_aff_free(aff);
611 isl_int_init(gcd);
612 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
613 isl_int_gcd(gcd, gcd, f);
614 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
615 isl_int_divexact(gcd, f, gcd);
616 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
617 isl_int_clear(gcd);
619 return aff;
622 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
624 isl_int v;
626 if (f == 1)
627 return aff;
629 isl_int_init(v);
630 isl_int_set_ui(v, f);
631 aff = isl_aff_scale_down(aff, v);
632 isl_int_clear(v);
634 return aff;
637 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
638 enum isl_dim_type type, unsigned pos, const char *s)
640 aff = isl_aff_cow(aff);
641 if (!aff)
642 return NULL;
643 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
644 if (!aff->ls)
645 return isl_aff_free(aff);
647 return aff;
650 /* Exploit the equalities in "eq" to simplify the affine expression
651 * and the expressions of the integer divisions in the local space.
652 * The integer divisions in this local space are assumed to appear
653 * as regular dimensions in "eq".
655 static __isl_give isl_aff *isl_aff_substitute_equalities(
656 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
658 int i, j;
659 unsigned total;
660 unsigned n_div;
662 if (!eq)
663 goto error;
664 if (eq->n_eq == 0) {
665 isl_basic_set_free(eq);
666 return aff;
669 aff = isl_aff_cow(aff);
670 if (!aff)
671 goto error;
673 aff->ls = isl_local_space_substitute_equalities(aff->ls,
674 isl_basic_set_copy(eq));
675 if (!aff->ls)
676 goto error;
678 total = 1 + isl_dim_total(eq->dim);
679 n_div = eq->n_div;
680 for (i = 0; i < eq->n_eq; ++i) {
681 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
682 if (j < 0 || j == 0 || j >= total)
683 continue;
685 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
686 &aff->v->el[0]);
689 isl_basic_set_free(eq);
690 return aff;
691 error:
692 isl_basic_set_free(eq);
693 isl_aff_free(aff);
694 return NULL;
697 /* Look for equalities among the variables shared by context and aff
698 * and the integer divisions of aff, if any.
699 * The equalities are then used to eliminate coefficients and/or integer
700 * divisions from aff.
702 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
703 __isl_take isl_set *context)
705 isl_basic_set *hull;
706 int n_div;
708 if (!aff)
709 goto error;
710 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
711 if (n_div > 0) {
712 isl_basic_set *bset;
713 context = isl_set_add_dims(context, isl_dim_set, n_div);
714 bset = isl_basic_set_from_local_space(
715 isl_aff_get_local_space(aff));
716 bset = isl_basic_set_lift(bset);
717 bset = isl_basic_set_flatten(bset);
718 context = isl_set_intersect(context,
719 isl_set_from_basic_set(bset));
722 hull = isl_set_affine_hull(context);
723 return isl_aff_substitute_equalities(aff, hull);
724 error:
725 isl_aff_free(aff);
726 isl_set_free(context);
727 return NULL;
730 /* Return a basic set containing those elements in the space
731 * of aff where it is non-negative.
733 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
735 isl_constraint *ineq;
737 ineq = isl_inequality_from_aff(aff);
739 return isl_basic_set_from_constraint(ineq);
742 /* Return a basic set containing those elements in the shared space
743 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
745 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
746 __isl_take isl_aff *aff2)
748 aff1 = isl_aff_sub(aff1, aff2);
750 return isl_aff_nonneg_basic_set(aff1);
753 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
754 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
756 aff1 = isl_aff_add(aff1, aff2);
757 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
758 return aff1;
761 int isl_aff_is_empty(__isl_keep isl_aff *aff)
763 if (!aff)
764 return -1;
766 return 0;
769 /* Set active[i] to 1 if the dimension at position i is involved
770 * in the affine expression.
772 static int set_active(__isl_keep isl_aff *aff, int *active)
774 int i, j;
775 unsigned total;
776 unsigned offset;
778 if (!aff || !active)
779 return -1;
781 total = aff->v->size - 2;
782 for (i = 0; i < total; ++i)
783 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
785 offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
786 for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
787 if (!active[offset + i])
788 continue;
789 for (j = 0; j < total; ++j)
790 active[j] |=
791 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
794 return 0;
797 /* Check whether the given affine expression has non-zero coefficient
798 * for any dimension in the given range or if any of these dimensions
799 * appear with non-zero coefficients in any of the integer divisions
800 * involved in the affine expression.
802 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
803 enum isl_dim_type type, unsigned first, unsigned n)
805 int i;
806 isl_ctx *ctx;
807 int *active = NULL;
808 int involves = 0;
810 if (!aff)
811 return -1;
812 if (n == 0)
813 return 0;
815 ctx = isl_aff_get_ctx(aff);
816 if (first + n > isl_aff_dim(aff, type))
817 isl_die(ctx, isl_error_invalid,
818 "range out of bounds", return -1);
820 active = isl_calloc_array(ctx, int,
821 isl_local_space_dim(aff->ls, isl_dim_all));
822 if (set_active(aff, active) < 0)
823 goto error;
825 first += isl_local_space_offset(aff->ls, type) - 1;
826 for (i = 0; i < n; ++i)
827 if (active[first + i]) {
828 involves = 1;
829 break;
832 free(active);
834 return involves;
835 error:
836 free(active);
837 return -1;
840 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
841 enum isl_dim_type type, unsigned first, unsigned n)
843 isl_ctx *ctx;
845 if (!aff)
846 return NULL;
847 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
848 return aff;
850 ctx = isl_aff_get_ctx(aff);
851 if (first + n > isl_aff_dim(aff, type))
852 isl_die(ctx, isl_error_invalid, "range out of bounds",
853 return isl_aff_free(aff));
855 aff = isl_aff_cow(aff);
856 if (!aff)
857 return NULL;
859 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
860 if (!aff->ls)
861 return isl_aff_free(aff);
863 first += 1 + isl_local_space_offset(aff->ls, type);
864 aff->v = isl_vec_drop_els(aff->v, first, n);
865 if (!aff->v)
866 return isl_aff_free(aff);
868 return aff;
871 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
872 enum isl_dim_type type, unsigned first, unsigned n)
874 isl_ctx *ctx;
876 if (!aff)
877 return NULL;
878 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
879 return aff;
881 ctx = isl_aff_get_ctx(aff);
882 if (first > isl_aff_dim(aff, type))
883 isl_die(ctx, isl_error_invalid, "position out of bounds",
884 return isl_aff_free(aff));
886 aff = isl_aff_cow(aff);
887 if (!aff)
888 return NULL;
890 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
891 if (!aff->ls)
892 return isl_aff_free(aff);
894 first += 1 + isl_local_space_offset(aff->ls, type);
895 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
896 if (!aff->v)
897 return isl_aff_free(aff);
899 return aff;
902 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
903 enum isl_dim_type type, unsigned n)
905 unsigned pos;
907 pos = isl_aff_dim(aff, type);
909 return isl_aff_insert_dims(aff, type, pos, n);
912 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
913 enum isl_dim_type type, unsigned n)
915 unsigned pos;
917 pos = isl_pw_aff_dim(pwaff, type);
919 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
922 #undef PW
923 #define PW isl_pw_aff
924 #undef EL
925 #define EL isl_aff
926 #undef EL_IS_ZERO
927 #define EL_IS_ZERO is_empty
928 #undef ZERO
929 #define ZERO empty
930 #undef IS_ZERO
931 #define IS_ZERO is_empty
932 #undef FIELD
933 #define FIELD aff
935 #define NO_EVAL
936 #define NO_OPT
937 #define NO_MOVE_DIMS
938 #define NO_REALIGN
939 #define NO_LIFT
940 #define NO_MORPH
941 #define NO_RESET_DIM
943 #include <isl_pw_templ.c>
945 /* Compute a piecewise quasi-affine expression with a domain that
946 * is the union of those of pwaff1 and pwaff2 and such that on each
947 * cell, the quasi-affine expression is the maximum of those of pwaff1
948 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
949 * cell, then the associated expression is the defined one.
951 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
952 __isl_take isl_pw_aff *pwaff2)
954 int i, j, n;
955 isl_pw_aff *res;
956 isl_ctx *ctx;
957 isl_set *set;
959 if (!pwaff1 || !pwaff2)
960 goto error;
962 ctx = isl_dim_get_ctx(pwaff1->dim);
963 if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
964 isl_die(ctx, isl_error_invalid,
965 "arguments should live in same space", goto error);
967 if (isl_pw_aff_is_empty(pwaff1)) {
968 isl_pw_aff_free(pwaff1);
969 return pwaff2;
972 if (isl_pw_aff_is_empty(pwaff2)) {
973 isl_pw_aff_free(pwaff2);
974 return pwaff1;
977 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
978 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
980 for (i = 0; i < pwaff1->n; ++i) {
981 set = isl_set_copy(pwaff1->p[i].set);
982 for (j = 0; j < pwaff2->n; ++j) {
983 struct isl_set *common;
984 isl_set *ge;
986 common = isl_set_intersect(
987 isl_set_copy(pwaff1->p[i].set),
988 isl_set_copy(pwaff2->p[j].set));
989 ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
990 isl_aff_copy(pwaff2->p[j].aff),
991 isl_aff_copy(pwaff1->p[i].aff)));
992 ge = isl_set_intersect(common, ge);
993 if (isl_set_plain_is_empty(ge)) {
994 isl_set_free(ge);
995 continue;
997 set = isl_set_subtract(set, isl_set_copy(ge));
999 res = isl_pw_aff_add_piece(res, ge,
1000 isl_aff_copy(pwaff2->p[j].aff));
1002 res = isl_pw_aff_add_piece(res, set,
1003 isl_aff_copy(pwaff1->p[i].aff));
1006 for (j = 0; j < pwaff2->n; ++j) {
1007 set = isl_set_copy(pwaff2->p[j].set);
1008 for (i = 0; i < pwaff1->n; ++i)
1009 set = isl_set_subtract(set,
1010 isl_set_copy(pwaff1->p[i].set));
1011 res = isl_pw_aff_add_piece(res, set,
1012 isl_aff_copy(pwaff2->p[j].aff));
1015 isl_pw_aff_free(pwaff1);
1016 isl_pw_aff_free(pwaff2);
1018 return res;
1019 error:
1020 isl_pw_aff_free(pwaff1);
1021 isl_pw_aff_free(pwaff2);
1022 return NULL;
1025 /* Construct a map with as domain the domain of pwaff and
1026 * one-dimensional range corresponding to the affine expressions.
1028 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1030 int i;
1031 isl_dim *dim;
1032 isl_map *map;
1034 if (!pwaff)
1035 return NULL;
1037 dim = isl_pw_aff_get_dim(pwaff);
1038 dim = isl_dim_from_domain(dim);
1039 dim = isl_dim_add(dim, isl_dim_out, 1);
1040 map = isl_map_empty(dim);
1042 for (i = 0; i < pwaff->n; ++i) {
1043 isl_basic_map *bmap;
1044 isl_map *map_i;
1046 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1047 map_i = isl_map_from_basic_map(bmap);
1048 map_i = isl_map_intersect_domain(map_i,
1049 isl_set_copy(pwaff->p[i].set));
1050 map = isl_map_union_disjoint(map, map_i);
1053 isl_pw_aff_free(pwaff);
1055 return map;
1058 /* Return a set containing those elements in the domain
1059 * of pwaff where it is non-negative.
1061 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1063 int i;
1064 isl_set *set;
1066 if (!pwaff)
1067 return NULL;
1069 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1071 for (i = 0; i < pwaff->n; ++i) {
1072 isl_basic_set *bset;
1073 isl_set *set_i;
1075 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1076 set_i = isl_set_from_basic_set(bset);
1077 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1078 set = isl_set_union_disjoint(set, set_i);
1081 isl_pw_aff_free(pwaff);
1083 return set;
1086 /* Return a set containing those elements in the shared domain
1087 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1089 * We compute the difference on the shared domain and then construct
1090 * the set of values where this difference is non-negative.
1091 * If strict is set, we first subtract 1 from the difference.
1093 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1094 __isl_take isl_pw_aff *pwaff2, int strict)
1096 isl_set *set1, *set2;
1098 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1099 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1100 set1 = isl_set_intersect(set1, set2);
1101 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1102 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1103 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1105 if (strict) {
1106 isl_dim *dim = isl_set_get_dim(set1);
1107 isl_aff *aff;
1108 aff = isl_aff_zero(isl_local_space_from_dim(dim));
1109 aff = isl_aff_add_constant_si(aff, -1);
1110 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1111 } else
1112 isl_set_free(set1);
1114 return isl_pw_aff_nonneg_set(pwaff1);
1117 /* Return a set containing those elements in the shared domain
1118 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1120 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1121 __isl_take isl_pw_aff *pwaff2)
1123 return pw_aff_gte_set(pwaff1, pwaff2, 0);
1126 /* Return a set containing those elements in the shared domain
1127 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1129 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1130 __isl_take isl_pw_aff *pwaff2)
1132 return pw_aff_gte_set(pwaff1, pwaff2, 1);
1135 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1136 __isl_take isl_pw_aff *pwaff2)
1138 return isl_pw_aff_gt_set(pwaff2, pwaff1);