privately export isl_basic_map_divs_known
[isl.git] / isl_aff.c
blobed66c74cdd1c9f49633228d06a22766a21e12c0e
1 /*
2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_map_private.h>
12 #include <isl_aff_private.h>
13 #include <isl_dim_private.h>
14 #include <isl_local_space_private.h>
15 #include <isl_mat_private.h>
16 #include <isl/constraint.h>
17 #include <isl/seq.h>
18 #include <isl/set.h>
20 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
21 __isl_take isl_vec *v)
23 isl_aff *aff;
25 if (!ls || !v)
26 goto error;
28 aff = isl_calloc_type(v->ctx, struct isl_aff);
29 if (!aff)
30 goto error;
32 aff->ref = 1;
33 aff->ls = ls;
34 aff->v = v;
36 return aff;
37 error:
38 isl_local_space_free(ls);
39 isl_vec_free(v);
40 return NULL;
43 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
45 isl_ctx *ctx;
46 isl_vec *v;
47 unsigned total;
49 if (!ls)
50 return NULL;
52 ctx = isl_local_space_get_ctx(ls);
53 if (!isl_local_space_divs_known(ls))
54 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
55 goto error);
57 total = isl_local_space_dim(ls, isl_dim_all);
58 v = isl_vec_alloc(ctx, 1 + 1 + total);
59 return isl_aff_alloc_vec(ls, v);
60 error:
61 isl_local_space_free(ls);
62 return NULL;
65 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
67 isl_aff *aff;
69 aff = isl_aff_alloc(ls);
70 if (!aff)
71 return NULL;
73 isl_int_set_si(aff->v->el[0], 1);
74 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
76 return aff;
79 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
81 if (!aff)
82 return NULL;
84 aff->ref++;
85 return aff;
88 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
90 if (!aff)
91 return NULL;
93 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
94 isl_vec_copy(aff->v));
97 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
99 if (!aff)
100 return NULL;
102 if (aff->ref == 1)
103 return aff;
104 aff->ref--;
105 return isl_aff_dup(aff);
108 void *isl_aff_free(__isl_take isl_aff *aff)
110 if (!aff)
111 return NULL;
113 if (--aff->ref > 0)
114 return NULL;
116 isl_local_space_free(aff->ls);
117 isl_vec_free(aff->v);
119 free(aff);
121 return NULL;
124 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
126 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
129 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
131 return aff ? isl_local_space_dim(aff->ls, type) : 0;
134 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
136 return aff ? isl_local_space_get_dim(aff->ls) : NULL;
139 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
141 return aff ? isl_local_space_copy(aff->ls) : NULL;
144 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
145 enum isl_dim_type type, unsigned pos)
147 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
150 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
152 if (!aff)
153 return -1;
155 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
158 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
160 int equal;
162 if (!aff1 || !aff2)
163 return -1;
165 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
166 if (equal < 0 || !equal)
167 return equal;
169 return isl_vec_is_equal(aff1->v, aff2->v);
172 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
174 if (!aff)
175 return -1;
176 isl_int_set(*v, aff->v->el[0]);
177 return 0;
180 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
182 if (!aff)
183 return -1;
184 isl_int_set(*v, aff->v->el[1]);
185 return 0;
188 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
189 enum isl_dim_type type, int pos, isl_int *v)
191 if (!aff)
192 return -1;
194 if (pos >= isl_local_space_dim(aff->ls, type))
195 isl_die(aff->v->ctx, isl_error_invalid,
196 "position out of bounds", return -1);
198 pos += isl_local_space_offset(aff->ls, type);
199 isl_int_set(*v, aff->v->el[1 + pos]);
201 return 0;
204 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
206 aff = isl_aff_cow(aff);
207 if (!aff)
208 return NULL;
210 aff->v = isl_vec_cow(aff->v);
211 if (!aff->v)
212 return isl_aff_free(aff);
214 isl_int_set(aff->v->el[0], v);
216 return aff;
219 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
221 aff = isl_aff_cow(aff);
222 if (!aff)
223 return NULL;
225 aff->v = isl_vec_cow(aff->v);
226 if (!aff->v)
227 return isl_aff_free(aff);
229 isl_int_set(aff->v->el[1], v);
231 return aff;
234 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
236 if (isl_int_is_zero(v))
237 return aff;
239 aff = isl_aff_cow(aff);
240 if (!aff)
241 return NULL;
243 aff->v = isl_vec_cow(aff->v);
244 if (!aff->v)
245 return isl_aff_free(aff);
247 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
249 return aff;
252 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
254 isl_int t;
256 isl_int_init(t);
257 isl_int_set_si(t, v);
258 aff = isl_aff_add_constant(aff, t);
259 isl_int_clear(t);
261 return aff;
264 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
266 aff = isl_aff_cow(aff);
267 if (!aff)
268 return NULL;
270 aff->v = isl_vec_cow(aff->v);
271 if (!aff->v)
272 return isl_aff_free(aff);
274 isl_int_set_si(aff->v->el[1], v);
276 return aff;
279 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
280 enum isl_dim_type type, int pos, isl_int v)
282 if (!aff)
283 return NULL;
285 if (pos >= isl_local_space_dim(aff->ls, type))
286 isl_die(aff->v->ctx, isl_error_invalid,
287 "position out of bounds", return isl_aff_free(aff));
289 aff = isl_aff_cow(aff);
290 if (!aff)
291 return NULL;
293 aff->v = isl_vec_cow(aff->v);
294 if (!aff->v)
295 return isl_aff_free(aff);
297 pos += isl_local_space_offset(aff->ls, type);
298 isl_int_set(aff->v->el[1 + pos], v);
300 return aff;
303 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
304 enum isl_dim_type type, int pos, int v)
306 if (!aff)
307 return NULL;
309 if (pos >= isl_local_space_dim(aff->ls, type))
310 isl_die(aff->v->ctx, isl_error_invalid,
311 "position out of bounds", return isl_aff_free(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 pos += isl_local_space_offset(aff->ls, type);
322 isl_int_set_si(aff->v->el[1 + pos], v);
324 return aff;
327 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
328 enum isl_dim_type type, int pos, isl_int v)
330 if (!aff)
331 return NULL;
333 if (pos >= isl_local_space_dim(aff->ls, type))
334 isl_die(aff->v->ctx, isl_error_invalid,
335 "position out of bounds", return isl_aff_free(aff));
337 aff = isl_aff_cow(aff);
338 if (!aff)
339 return NULL;
341 aff->v = isl_vec_cow(aff->v);
342 if (!aff->v)
343 return isl_aff_free(aff);
345 pos += isl_local_space_offset(aff->ls, type);
346 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
348 return aff;
351 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
352 enum isl_dim_type type, int pos, int v)
354 isl_int t;
356 isl_int_init(t);
357 isl_int_set_si(t, v);
358 aff = isl_aff_add_coefficient(aff, type, pos, t);
359 isl_int_clear(t);
361 return aff;
364 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
366 if (!aff)
367 return NULL;
369 return isl_local_space_get_div(aff->ls, pos);
372 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
374 aff = isl_aff_cow(aff);
375 if (!aff)
376 return NULL;
377 aff->v = isl_vec_cow(aff->v);
378 if (!aff->v)
379 return isl_aff_free(aff);
381 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
383 return aff;
386 /* Given f, return floor(f).
387 * If f is an integer expression, then just return f.
388 * Otherwise, create a new div d = [f] and return the expression d.
390 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
392 int size;
393 isl_ctx *ctx;
395 if (!aff)
396 return NULL;
398 if (isl_int_is_one(aff->v->el[0]))
399 return aff;
401 aff = isl_aff_cow(aff);
402 if (!aff)
403 return NULL;
405 aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
406 if (!aff->ls)
407 return isl_aff_free(aff);
409 ctx = isl_aff_get_ctx(aff);
410 size = aff->v->size;
411 isl_vec_free(aff->v);
412 aff->v = isl_vec_alloc(ctx, size + 1);
413 aff->v = isl_vec_clr(aff->v);
414 if (!aff->v)
415 return isl_aff_free(aff);
416 isl_int_set_si(aff->v->el[0], 1);
417 isl_int_set_si(aff->v->el[size], 1);
419 return aff;
422 /* Given f, return ceil(f).
423 * If f is an integer expression, then just return f.
424 * Otherwise, create a new div d = [-f] and return the expression -d.
426 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
428 if (!aff)
429 return NULL;
431 if (isl_int_is_one(aff->v->el[0]))
432 return aff;
434 aff = isl_aff_neg(aff);
435 aff = isl_aff_floor(aff);
436 aff = isl_aff_neg(aff);
438 return aff;
441 /* Apply the expansion computed by isl_merge_divs.
442 * The expansion itself is given by "exp" while the resulting
443 * list of divs is given by "div".
445 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
446 __isl_take isl_mat *div, int *exp)
448 int i, j;
449 int old_n_div;
450 int new_n_div;
451 int offset;
453 aff = isl_aff_cow(aff);
454 if (!aff || !div)
455 goto error;
457 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
458 new_n_div = isl_mat_rows(div);
459 if (new_n_div < old_n_div)
460 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
461 "not an expansion", goto error);
463 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
464 if (!aff->v)
465 goto error;
467 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
468 j = old_n_div - 1;
469 for (i = new_n_div - 1; i >= 0; --i) {
470 if (j >= 0 && exp[j] == i) {
471 if (i != j)
472 isl_int_swap(aff->v->el[offset + i],
473 aff->v->el[offset + j]);
474 j--;
475 } else
476 isl_int_set_si(aff->v->el[offset + i], 0);
479 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
480 if (!aff->ls)
481 goto error;
482 isl_mat_free(div);
483 return aff;
484 error:
485 isl_aff_free(aff);
486 isl_mat_free(div);
487 return NULL;
490 /* Add two affine expressions that live in the same local space.
492 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
493 __isl_take isl_aff *aff2)
495 isl_int gcd, f;
497 aff1 = isl_aff_cow(aff1);
498 if (!aff1 || !aff2)
499 goto error;
501 aff1->v = isl_vec_cow(aff1->v);
502 if (!aff1->v)
503 goto error;
505 isl_int_init(gcd);
506 isl_int_init(f);
507 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
508 isl_int_divexact(f, aff2->v->el[0], gcd);
509 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
510 isl_int_divexact(f, aff1->v->el[0], gcd);
511 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
512 isl_int_divexact(f, aff2->v->el[0], gcd);
513 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
514 isl_int_clear(f);
515 isl_int_clear(gcd);
517 isl_aff_free(aff2);
518 return aff1;
519 error:
520 isl_aff_free(aff1);
521 isl_aff_free(aff2);
522 return NULL;
525 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
526 __isl_take isl_aff *aff2)
528 isl_ctx *ctx;
529 int *exp1 = NULL;
530 int *exp2 = NULL;
531 isl_mat *div;
533 if (!aff1 || !aff2)
534 goto error;
536 ctx = isl_aff_get_ctx(aff1);
537 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
538 isl_die(ctx, isl_error_invalid,
539 "spaces don't match", goto error);
541 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
542 return add_expanded(aff1, aff2);
544 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
545 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
546 if (!exp1 || !exp2)
547 goto error;
549 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
550 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
551 aff2 = isl_aff_expand_divs(aff2, div, exp2);
552 free(exp1);
553 free(exp2);
555 return add_expanded(aff1, aff2);
556 error:
557 free(exp1);
558 free(exp2);
559 isl_aff_free(aff1);
560 isl_aff_free(aff2);
561 return NULL;
564 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
565 __isl_take isl_aff *aff2)
567 return isl_aff_add(aff1, isl_aff_neg(aff2));
570 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
572 isl_int gcd;
574 if (isl_int_is_one(f))
575 return aff;
577 aff = isl_aff_cow(aff);
578 if (!aff)
579 return NULL;
580 aff->v = isl_vec_cow(aff->v);
581 if (!aff->v)
582 return isl_aff_free(aff);
584 isl_int_init(gcd);
585 isl_int_gcd(gcd, aff->v->el[0], f);
586 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
587 isl_int_divexact(gcd, f, gcd);
588 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
589 isl_int_clear(gcd);
591 return aff;
594 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
596 isl_int gcd;
598 if (isl_int_is_one(f))
599 return aff;
601 aff = isl_aff_cow(aff);
602 if (!aff)
603 return NULL;
604 aff->v = isl_vec_cow(aff->v);
605 if (!aff->v)
606 return isl_aff_free(aff);
608 isl_int_init(gcd);
609 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
610 isl_int_gcd(gcd, gcd, f);
611 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
612 isl_int_divexact(gcd, f, gcd);
613 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
614 isl_int_clear(gcd);
616 return aff;
619 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
621 isl_int v;
623 if (f == 1)
624 return aff;
626 isl_int_init(v);
627 isl_int_set_ui(v, f);
628 aff = isl_aff_scale_down(aff, v);
629 isl_int_clear(v);
631 return aff;
634 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
635 enum isl_dim_type type, unsigned pos, const char *s)
637 aff = isl_aff_cow(aff);
638 if (!aff)
639 return NULL;
640 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
641 if (!aff->ls)
642 return isl_aff_free(aff);
644 return aff;
647 /* Exploit the equalities in "eq" to simplify the affine expression
648 * and the expressions of the integer divisions in the local space.
649 * The integer divisions in this local space are assumed to appear
650 * as regular dimensions in "eq".
652 static __isl_give isl_aff *isl_aff_substitute_equalities(
653 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
655 int i, j;
656 unsigned total;
657 unsigned n_div;
659 if (!eq)
660 goto error;
661 if (eq->n_eq == 0) {
662 isl_basic_set_free(eq);
663 return aff;
666 aff = isl_aff_cow(aff);
667 if (!aff)
668 goto error;
670 aff->ls = isl_local_space_substitute_equalities(aff->ls,
671 isl_basic_set_copy(eq));
672 if (!aff->ls)
673 goto error;
675 total = 1 + isl_dim_total(eq->dim);
676 n_div = eq->n_div;
677 for (i = 0; i < eq->n_eq; ++i) {
678 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
679 if (j < 0 || j == 0 || j >= total)
680 continue;
682 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
683 &aff->v->el[0]);
686 isl_basic_set_free(eq);
687 return aff;
688 error:
689 isl_basic_set_free(eq);
690 isl_aff_free(aff);
691 return NULL;
694 /* Look for equalities among the variables shared by context and aff
695 * and the integer divisions of aff, if any.
696 * The equalities are then used to eliminate coefficients and/or integer
697 * divisions from aff.
699 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
700 __isl_take isl_set *context)
702 isl_basic_set *hull;
703 int n_div;
705 if (!aff)
706 goto error;
707 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
708 if (n_div > 0) {
709 isl_basic_set *bset;
710 context = isl_set_add_dims(context, isl_dim_set, n_div);
711 bset = isl_basic_set_from_local_space(
712 isl_aff_get_local_space(aff));
713 bset = isl_basic_set_lift(bset);
714 bset = isl_basic_set_flatten(bset);
715 context = isl_set_intersect(context,
716 isl_set_from_basic_set(bset));
719 hull = isl_set_affine_hull(context);
720 return isl_aff_substitute_equalities(aff, hull);
721 error:
722 isl_aff_free(aff);
723 isl_set_free(context);
724 return NULL;
727 /* Return a basic set containing those elements in the space
728 * of aff where it is non-negative.
730 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
732 isl_constraint *ineq;
734 ineq = isl_inequality_from_aff(aff);
736 return isl_basic_set_from_constraint(ineq);
739 /* Return a basic set containing those elements in the shared space
740 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
742 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
743 __isl_take isl_aff *aff2)
745 aff1 = isl_aff_sub(aff1, aff2);
747 return isl_aff_nonneg_basic_set(aff1);
750 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
751 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
753 aff1 = isl_aff_add(aff1, aff2);
754 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
755 return aff1;
758 int isl_aff_is_empty(__isl_keep isl_aff *aff)
760 if (!aff)
761 return -1;
763 return 0;
766 /* Set active[i] to 1 if the dimension at position i is involved
767 * in the affine expression.
769 static int set_active(__isl_keep isl_aff *aff, int *active)
771 int i, j;
772 unsigned total;
773 unsigned offset;
775 if (!aff || !active)
776 return -1;
778 total = aff->v->size - 2;
779 for (i = 0; i < total; ++i)
780 active[i] = !isl_int_is_zero(aff->v->el[2 + i]);
782 offset = isl_local_space_offset(aff->ls, isl_dim_div) - 1;
783 for (i = aff->ls->div->n_row - 1; i >= 0; --i) {
784 if (!active[offset + i])
785 continue;
786 for (j = 0; j < total; ++j)
787 active[j] |=
788 !isl_int_is_zero(aff->ls->div->row[i][2 + j]);
791 return 0;
794 /* Check whether the given affine expression has non-zero coefficient
795 * for any dimension in the given range or if any of these dimensions
796 * appear with non-zero coefficients in any of the integer divisions
797 * involved in the affine expression.
799 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
800 enum isl_dim_type type, unsigned first, unsigned n)
802 int i;
803 isl_ctx *ctx;
804 int *active = NULL;
805 int involves = 0;
807 if (!aff)
808 return -1;
809 if (n == 0)
810 return 0;
812 ctx = isl_aff_get_ctx(aff);
813 if (first + n > isl_aff_dim(aff, type))
814 isl_die(ctx, isl_error_invalid,
815 "range out of bounds", return -1);
817 active = isl_calloc_array(ctx, int,
818 isl_local_space_dim(aff->ls, isl_dim_all));
819 if (set_active(aff, active) < 0)
820 goto error;
822 first += isl_local_space_offset(aff->ls, type) - 1;
823 for (i = 0; i < n; ++i)
824 if (active[first + i]) {
825 involves = 1;
826 break;
829 free(active);
831 return involves;
832 error:
833 free(active);
834 return -1;
837 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
838 enum isl_dim_type type, unsigned first, unsigned n)
840 isl_ctx *ctx;
842 if (!aff)
843 return NULL;
844 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
845 return aff;
847 ctx = isl_aff_get_ctx(aff);
848 if (first + n > isl_aff_dim(aff, type))
849 isl_die(ctx, isl_error_invalid, "range out of bounds",
850 return isl_aff_free(aff));
852 aff = isl_aff_cow(aff);
853 if (!aff)
854 return NULL;
856 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
857 if (!aff->ls)
858 return isl_aff_free(aff);
860 first += 1 + isl_local_space_offset(aff->ls, type);
861 aff->v = isl_vec_drop_els(aff->v, first, n);
862 if (!aff->v)
863 return isl_aff_free(aff);
865 return aff;
868 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
869 enum isl_dim_type type, unsigned first, unsigned n)
871 isl_ctx *ctx;
873 if (!aff)
874 return NULL;
875 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
876 return aff;
878 ctx = isl_aff_get_ctx(aff);
879 if (first > isl_aff_dim(aff, type))
880 isl_die(ctx, isl_error_invalid, "position out of bounds",
881 return isl_aff_free(aff));
883 aff = isl_aff_cow(aff);
884 if (!aff)
885 return NULL;
887 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
888 if (!aff->ls)
889 return isl_aff_free(aff);
891 first += 1 + isl_local_space_offset(aff->ls, type);
892 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
893 if (!aff->v)
894 return isl_aff_free(aff);
896 return aff;
899 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
900 enum isl_dim_type type, unsigned n)
902 unsigned pos;
904 pos = isl_aff_dim(aff, type);
906 return isl_aff_insert_dims(aff, type, pos, n);
909 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
910 enum isl_dim_type type, unsigned n)
912 unsigned pos;
914 pos = isl_pw_aff_dim(pwaff, type);
916 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
919 #undef PW
920 #define PW isl_pw_aff
921 #undef EL
922 #define EL isl_aff
923 #undef EL_IS_ZERO
924 #define EL_IS_ZERO is_empty
925 #undef ZERO
926 #define ZERO empty
927 #undef IS_ZERO
928 #define IS_ZERO is_empty
929 #undef FIELD
930 #define FIELD aff
932 #define NO_EVAL
933 #define NO_OPT
934 #define NO_MOVE_DIMS
935 #define NO_REALIGN
936 #define NO_LIFT
937 #define NO_MORPH
938 #define NO_RESET_DIM
940 #include <isl_pw_templ.c>
942 /* Compute a piecewise quasi-affine expression with a domain that
943 * is the union of those of pwaff1 and pwaff2 and such that on each
944 * cell, the quasi-affine expression is the maximum of those of pwaff1
945 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
946 * cell, then the associated expression is the defined one.
948 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
949 __isl_take isl_pw_aff *pwaff2)
951 int i, j, n;
952 isl_pw_aff *res;
953 isl_ctx *ctx;
954 isl_set *set;
956 if (!pwaff1 || !pwaff2)
957 goto error;
959 ctx = isl_dim_get_ctx(pwaff1->dim);
960 if (!isl_dim_equal(pwaff1->dim, pwaff2->dim))
961 isl_die(ctx, isl_error_invalid,
962 "arguments should live in same space", goto error);
964 if (isl_pw_aff_is_empty(pwaff1)) {
965 isl_pw_aff_free(pwaff1);
966 return pwaff2;
969 if (isl_pw_aff_is_empty(pwaff2)) {
970 isl_pw_aff_free(pwaff2);
971 return pwaff1;
974 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
975 res = isl_pw_aff_alloc_(isl_dim_copy(pwaff1->dim), n);
977 for (i = 0; i < pwaff1->n; ++i) {
978 set = isl_set_copy(pwaff1->p[i].set);
979 for (j = 0; j < pwaff2->n; ++j) {
980 struct isl_set *common;
981 isl_set *ge;
983 common = isl_set_intersect(
984 isl_set_copy(pwaff1->p[i].set),
985 isl_set_copy(pwaff2->p[j].set));
986 ge = isl_set_from_basic_set(isl_aff_ge_basic_set(
987 isl_aff_copy(pwaff2->p[j].aff),
988 isl_aff_copy(pwaff1->p[i].aff)));
989 ge = isl_set_intersect(common, ge);
990 if (isl_set_plain_is_empty(ge)) {
991 isl_set_free(ge);
992 continue;
994 set = isl_set_subtract(set, isl_set_copy(ge));
996 res = isl_pw_aff_add_piece(res, ge,
997 isl_aff_copy(pwaff2->p[j].aff));
999 res = isl_pw_aff_add_piece(res, set,
1000 isl_aff_copy(pwaff1->p[i].aff));
1003 for (j = 0; j < pwaff2->n; ++j) {
1004 set = isl_set_copy(pwaff2->p[j].set);
1005 for (i = 0; i < pwaff1->n; ++i)
1006 set = isl_set_subtract(set,
1007 isl_set_copy(pwaff1->p[i].set));
1008 res = isl_pw_aff_add_piece(res, set,
1009 isl_aff_copy(pwaff2->p[j].aff));
1012 isl_pw_aff_free(pwaff1);
1013 isl_pw_aff_free(pwaff2);
1015 return res;
1016 error:
1017 isl_pw_aff_free(pwaff1);
1018 isl_pw_aff_free(pwaff2);
1019 return NULL;
1022 /* Construct a map with as domain the domain of pwaff and
1023 * one-dimensional range corresponding to the affine expressions.
1025 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1027 int i;
1028 isl_dim *dim;
1029 isl_map *map;
1031 if (!pwaff)
1032 return NULL;
1034 dim = isl_pw_aff_get_dim(pwaff);
1035 dim = isl_dim_from_domain(dim);
1036 dim = isl_dim_add(dim, isl_dim_out, 1);
1037 map = isl_map_empty(dim);
1039 for (i = 0; i < pwaff->n; ++i) {
1040 isl_basic_map *bmap;
1041 isl_map *map_i;
1043 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1044 map_i = isl_map_from_basic_map(bmap);
1045 map_i = isl_map_intersect_domain(map_i,
1046 isl_set_copy(pwaff->p[i].set));
1047 map = isl_map_union_disjoint(map, map_i);
1050 isl_pw_aff_free(pwaff);
1052 return map;
1055 /* Return a set containing those elements in the domain
1056 * of pwaff where it is non-negative.
1058 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1060 int i;
1061 isl_set *set;
1063 if (!pwaff)
1064 return NULL;
1066 set = isl_set_empty(isl_pw_aff_get_dim(pwaff));
1068 for (i = 0; i < pwaff->n; ++i) {
1069 isl_basic_set *bset;
1070 isl_set *set_i;
1072 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1073 set_i = isl_set_from_basic_set(bset);
1074 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1075 set = isl_set_union_disjoint(set, set_i);
1078 isl_pw_aff_free(pwaff);
1080 return set;
1083 /* Return a set containing those elements in the shared domain
1084 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1086 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1087 __isl_take isl_pw_aff *pwaff2)
1089 isl_set *set1, *set2;
1091 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1092 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1093 set1 = isl_set_intersect(set1, set2);
1094 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1095 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, set1);
1096 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1098 return isl_pw_aff_nonneg_set(pwaff1);