add isl_vec_neg
[isl.git] / isl_aff.c
blob26095b2baa85a1896d61a8be85889e0002049a0e
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>
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_space *isl_aff_get_space(__isl_keep isl_aff *aff)
139 return aff ? isl_local_space_get_space(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_space(__isl_take isl_aff *aff,
154 __isl_take isl_space *dim)
156 aff = isl_aff_cow(aff);
157 if (!aff || !dim)
158 goto error;
160 aff->ls = isl_local_space_reset_space(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_space_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,
186 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
187 isl_seq_cpy(res->el, vec->el, 2);
188 isl_seq_clr(res->el + 2, res->size - 2);
189 for (i = 0; i < r->len; ++i)
190 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
192 isl_reordering_free(r);
193 isl_vec_free(vec);
194 return res;
195 error:
196 isl_vec_free(vec);
197 isl_reordering_free(r);
198 return NULL;
201 /* Reorder the dimensions of "aff" according to the given reordering.
203 __isl_give isl_aff *isl_aff_realign(__isl_take isl_aff *aff,
204 __isl_take isl_reordering *r)
206 aff = isl_aff_cow(aff);
207 if (!aff)
208 goto error;
210 r = isl_reordering_extend(r, aff->ls->div->n_row);
211 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
212 aff->ls->div->n_row);
213 aff->ls = isl_local_space_realign(aff->ls, r);
215 if (!aff->v || !aff->ls)
216 return isl_aff_free(aff);
218 return aff;
219 error:
220 isl_aff_free(aff);
221 isl_reordering_free(r);
222 return NULL;
225 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
227 if (!aff)
228 return -1;
230 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
233 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
235 int equal;
237 if (!aff1 || !aff2)
238 return -1;
240 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
241 if (equal < 0 || !equal)
242 return equal;
244 return isl_vec_is_equal(aff1->v, aff2->v);
247 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
249 if (!aff)
250 return -1;
251 isl_int_set(*v, aff->v->el[0]);
252 return 0;
255 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
257 if (!aff)
258 return -1;
259 isl_int_set(*v, aff->v->el[1]);
260 return 0;
263 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
264 enum isl_dim_type type, int pos, isl_int *v)
266 if (!aff)
267 return -1;
269 if (pos >= isl_local_space_dim(aff->ls, type))
270 isl_die(aff->v->ctx, isl_error_invalid,
271 "position out of bounds", return -1);
273 pos += isl_local_space_offset(aff->ls, type);
274 isl_int_set(*v, aff->v->el[1 + pos]);
276 return 0;
279 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
281 aff = isl_aff_cow(aff);
282 if (!aff)
283 return NULL;
285 aff->v = isl_vec_cow(aff->v);
286 if (!aff->v)
287 return isl_aff_free(aff);
289 isl_int_set(aff->v->el[0], v);
291 return aff;
294 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
296 aff = isl_aff_cow(aff);
297 if (!aff)
298 return NULL;
300 aff->v = isl_vec_cow(aff->v);
301 if (!aff->v)
302 return isl_aff_free(aff);
304 isl_int_set(aff->v->el[1], v);
306 return aff;
309 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
311 if (isl_int_is_zero(v))
312 return aff;
314 aff = isl_aff_cow(aff);
315 if (!aff)
316 return NULL;
318 aff->v = isl_vec_cow(aff->v);
319 if (!aff->v)
320 return isl_aff_free(aff);
322 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
324 return aff;
327 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
329 isl_int t;
331 isl_int_init(t);
332 isl_int_set_si(t, v);
333 aff = isl_aff_add_constant(aff, t);
334 isl_int_clear(t);
336 return aff;
339 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
341 aff = isl_aff_cow(aff);
342 if (!aff)
343 return NULL;
345 aff->v = isl_vec_cow(aff->v);
346 if (!aff->v)
347 return isl_aff_free(aff);
349 isl_int_set_si(aff->v->el[1], v);
351 return aff;
354 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
355 enum isl_dim_type type, int pos, isl_int v)
357 if (!aff)
358 return NULL;
360 if (pos >= isl_local_space_dim(aff->ls, type))
361 isl_die(aff->v->ctx, isl_error_invalid,
362 "position out of bounds", return isl_aff_free(aff));
364 aff = isl_aff_cow(aff);
365 if (!aff)
366 return NULL;
368 aff->v = isl_vec_cow(aff->v);
369 if (!aff->v)
370 return isl_aff_free(aff);
372 pos += isl_local_space_offset(aff->ls, type);
373 isl_int_set(aff->v->el[1 + pos], v);
375 return aff;
378 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
379 enum isl_dim_type type, int pos, int v)
381 if (!aff)
382 return NULL;
384 if (pos >= isl_local_space_dim(aff->ls, type))
385 isl_die(aff->v->ctx, isl_error_invalid,
386 "position out of bounds", return isl_aff_free(aff));
388 aff = isl_aff_cow(aff);
389 if (!aff)
390 return NULL;
392 aff->v = isl_vec_cow(aff->v);
393 if (!aff->v)
394 return isl_aff_free(aff);
396 pos += isl_local_space_offset(aff->ls, type);
397 isl_int_set_si(aff->v->el[1 + pos], v);
399 return aff;
402 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
403 enum isl_dim_type type, int pos, isl_int v)
405 if (!aff)
406 return NULL;
408 if (pos >= isl_local_space_dim(aff->ls, type))
409 isl_die(aff->v->ctx, isl_error_invalid,
410 "position out of bounds", return isl_aff_free(aff));
412 aff = isl_aff_cow(aff);
413 if (!aff)
414 return NULL;
416 aff->v = isl_vec_cow(aff->v);
417 if (!aff->v)
418 return isl_aff_free(aff);
420 pos += isl_local_space_offset(aff->ls, type);
421 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
423 return aff;
426 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
427 enum isl_dim_type type, int pos, int v)
429 isl_int t;
431 isl_int_init(t);
432 isl_int_set_si(t, v);
433 aff = isl_aff_add_coefficient(aff, type, pos, t);
434 isl_int_clear(t);
436 return aff;
439 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
441 if (!aff)
442 return NULL;
444 return isl_local_space_get_div(aff->ls, pos);
447 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
449 aff = isl_aff_cow(aff);
450 if (!aff)
451 return NULL;
452 aff->v = isl_vec_cow(aff->v);
453 if (!aff->v)
454 return isl_aff_free(aff);
456 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
458 return aff;
461 /* Given f, return floor(f).
462 * If f is an integer expression, then just return f.
463 * Otherwise, if f = g/m, write g = q m + r,
464 * create a new div d = [r/m] and return the expression q + d.
465 * The coefficients in r are taken to lie between -m/2 and m/2.
467 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
469 int i;
470 int size;
471 isl_ctx *ctx;
472 isl_vec *div;
474 if (!aff)
475 return NULL;
477 if (isl_int_is_one(aff->v->el[0]))
478 return aff;
480 aff = isl_aff_cow(aff);
481 if (!aff)
482 return NULL;
484 aff->v = isl_vec_cow(aff->v);
485 div = isl_vec_copy(aff->v);
486 div = isl_vec_cow(div);
487 if (!div)
488 return isl_aff_free(aff);
490 ctx = isl_aff_get_ctx(aff);
491 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
492 for (i = 1; i < aff->v->size; ++i) {
493 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
494 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
495 if (isl_int_gt(div->el[i], aff->v->el[0])) {
496 isl_int_sub(div->el[i], div->el[i], div->el[0]);
497 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
501 aff->ls = isl_local_space_add_div(aff->ls, div);
502 if (!aff->ls)
503 return isl_aff_free(aff);
505 size = aff->v->size;
506 aff->v = isl_vec_extend(aff->v, size + 1);
507 if (!aff->v)
508 return isl_aff_free(aff);
509 isl_int_set_si(aff->v->el[0], 1);
510 isl_int_set_si(aff->v->el[size], 1);
512 return aff;
515 /* Compute
517 * aff mod m = aff - m * floor(aff/m)
519 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
521 isl_aff *res;
523 res = isl_aff_copy(aff);
524 aff = isl_aff_scale_down(aff, m);
525 aff = isl_aff_floor(aff);
526 aff = isl_aff_scale(aff, m);
527 res = isl_aff_sub(res, aff);
529 return res;
532 /* Compute
534 * pwaff mod m = pwaff - m * floor(pwaff/m)
536 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
538 isl_pw_aff *res;
540 res = isl_pw_aff_copy(pwaff);
541 pwaff = isl_pw_aff_scale_down(pwaff, m);
542 pwaff = isl_pw_aff_floor(pwaff);
543 pwaff = isl_pw_aff_scale(pwaff, m);
544 res = isl_pw_aff_sub(res, pwaff);
546 return res;
549 /* Given f, return ceil(f).
550 * If f is an integer expression, then just return f.
551 * Otherwise, create a new div d = [-f] and return the expression -d.
553 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
555 if (!aff)
556 return NULL;
558 if (isl_int_is_one(aff->v->el[0]))
559 return aff;
561 aff = isl_aff_neg(aff);
562 aff = isl_aff_floor(aff);
563 aff = isl_aff_neg(aff);
565 return aff;
568 /* Apply the expansion computed by isl_merge_divs.
569 * The expansion itself is given by "exp" while the resulting
570 * list of divs is given by "div".
572 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
573 __isl_take isl_mat *div, int *exp)
575 int i, j;
576 int old_n_div;
577 int new_n_div;
578 int offset;
580 aff = isl_aff_cow(aff);
581 if (!aff || !div)
582 goto error;
584 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
585 new_n_div = isl_mat_rows(div);
586 if (new_n_div < old_n_div)
587 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
588 "not an expansion", goto error);
590 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
591 if (!aff->v)
592 goto error;
594 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
595 j = old_n_div - 1;
596 for (i = new_n_div - 1; i >= 0; --i) {
597 if (j >= 0 && exp[j] == i) {
598 if (i != j)
599 isl_int_swap(aff->v->el[offset + i],
600 aff->v->el[offset + j]);
601 j--;
602 } else
603 isl_int_set_si(aff->v->el[offset + i], 0);
606 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
607 if (!aff->ls)
608 goto error;
609 isl_mat_free(div);
610 return aff;
611 error:
612 isl_aff_free(aff);
613 isl_mat_free(div);
614 return NULL;
617 /* Add two affine expressions that live in the same local space.
619 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
620 __isl_take isl_aff *aff2)
622 isl_int gcd, f;
624 aff1 = isl_aff_cow(aff1);
625 if (!aff1 || !aff2)
626 goto error;
628 aff1->v = isl_vec_cow(aff1->v);
629 if (!aff1->v)
630 goto error;
632 isl_int_init(gcd);
633 isl_int_init(f);
634 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
635 isl_int_divexact(f, aff2->v->el[0], gcd);
636 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
637 isl_int_divexact(f, aff1->v->el[0], gcd);
638 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
639 isl_int_divexact(f, aff2->v->el[0], gcd);
640 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
641 isl_int_clear(f);
642 isl_int_clear(gcd);
644 isl_aff_free(aff2);
645 return aff1;
646 error:
647 isl_aff_free(aff1);
648 isl_aff_free(aff2);
649 return NULL;
652 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
653 __isl_take isl_aff *aff2)
655 isl_ctx *ctx;
656 int *exp1 = NULL;
657 int *exp2 = NULL;
658 isl_mat *div;
660 if (!aff1 || !aff2)
661 goto error;
663 ctx = isl_aff_get_ctx(aff1);
664 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
665 isl_die(ctx, isl_error_invalid,
666 "spaces don't match", goto error);
668 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
669 return add_expanded(aff1, aff2);
671 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
672 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
673 if (!exp1 || !exp2)
674 goto error;
676 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
677 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
678 aff2 = isl_aff_expand_divs(aff2, div, exp2);
679 free(exp1);
680 free(exp2);
682 return add_expanded(aff1, aff2);
683 error:
684 free(exp1);
685 free(exp2);
686 isl_aff_free(aff1);
687 isl_aff_free(aff2);
688 return NULL;
691 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
692 __isl_take isl_aff *aff2)
694 return isl_aff_add(aff1, isl_aff_neg(aff2));
697 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
699 isl_int gcd;
701 if (isl_int_is_one(f))
702 return aff;
704 aff = isl_aff_cow(aff);
705 if (!aff)
706 return NULL;
707 aff->v = isl_vec_cow(aff->v);
708 if (!aff->v)
709 return isl_aff_free(aff);
711 isl_int_init(gcd);
712 isl_int_gcd(gcd, aff->v->el[0], f);
713 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
714 isl_int_divexact(gcd, f, gcd);
715 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
716 isl_int_clear(gcd);
718 return aff;
721 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
723 isl_int gcd;
725 if (isl_int_is_one(f))
726 return aff;
728 aff = isl_aff_cow(aff);
729 if (!aff)
730 return NULL;
731 aff->v = isl_vec_cow(aff->v);
732 if (!aff->v)
733 return isl_aff_free(aff);
735 isl_int_init(gcd);
736 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
737 isl_int_gcd(gcd, gcd, f);
738 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
739 isl_int_divexact(gcd, f, gcd);
740 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
741 isl_int_clear(gcd);
743 return aff;
746 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
748 isl_int v;
750 if (f == 1)
751 return aff;
753 isl_int_init(v);
754 isl_int_set_ui(v, f);
755 aff = isl_aff_scale_down(aff, v);
756 isl_int_clear(v);
758 return aff;
761 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
762 enum isl_dim_type type, unsigned pos, const char *s)
764 aff = isl_aff_cow(aff);
765 if (!aff)
766 return NULL;
767 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
768 if (!aff->ls)
769 return isl_aff_free(aff);
771 return aff;
774 /* Exploit the equalities in "eq" to simplify the affine expression
775 * and the expressions of the integer divisions in the local space.
776 * The integer divisions in this local space are assumed to appear
777 * as regular dimensions in "eq".
779 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
780 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
782 int i, j;
783 unsigned total;
784 unsigned n_div;
786 if (!eq)
787 goto error;
788 if (eq->n_eq == 0) {
789 isl_basic_set_free(eq);
790 return aff;
793 aff = isl_aff_cow(aff);
794 if (!aff)
795 goto error;
797 aff->ls = isl_local_space_substitute_equalities(aff->ls,
798 isl_basic_set_copy(eq));
799 if (!aff->ls)
800 goto error;
802 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
803 n_div = eq->n_div;
804 for (i = 0; i < eq->n_eq; ++i) {
805 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
806 if (j < 0 || j == 0 || j >= total)
807 continue;
809 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
810 &aff->v->el[0]);
813 isl_basic_set_free(eq);
814 return aff;
815 error:
816 isl_basic_set_free(eq);
817 isl_aff_free(aff);
818 return NULL;
821 /* Exploit the equalities in "eq" to simplify the affine expression
822 * and the expressions of the integer divisions in the local space.
824 static __isl_give isl_aff *isl_aff_substitute_equalities(
825 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
827 int n_div;
829 if (!aff || !eq)
830 goto error;
831 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
832 if (n_div > 0)
833 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
834 return isl_aff_substitute_equalities_lifted(aff, eq);
835 error:
836 isl_basic_set_free(eq);
837 isl_aff_free(aff);
838 return NULL;
841 /* Look for equalities among the variables shared by context and aff
842 * and the integer divisions of aff, if any.
843 * The equalities are then used to eliminate coefficients and/or integer
844 * divisions from aff.
846 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
847 __isl_take isl_set *context)
849 isl_basic_set *hull;
850 int n_div;
852 if (!aff)
853 goto error;
854 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
855 if (n_div > 0) {
856 isl_basic_set *bset;
857 context = isl_set_add_dims(context, isl_dim_set, n_div);
858 bset = isl_basic_set_from_local_space(
859 isl_aff_get_local_space(aff));
860 bset = isl_basic_set_lift(bset);
861 bset = isl_basic_set_flatten(bset);
862 context = isl_set_intersect(context,
863 isl_set_from_basic_set(bset));
866 hull = isl_set_affine_hull(context);
867 return isl_aff_substitute_equalities_lifted(aff, hull);
868 error:
869 isl_aff_free(aff);
870 isl_set_free(context);
871 return NULL;
874 /* Return a basic set containing those elements in the space
875 * of aff where it is non-negative.
877 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
879 isl_constraint *ineq;
881 ineq = isl_inequality_from_aff(aff);
883 return isl_basic_set_from_constraint(ineq);
886 /* Return a basic set containing those elements in the space
887 * of aff where it is zero.
889 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
891 isl_constraint *ineq;
893 ineq = isl_equality_from_aff(aff);
895 return isl_basic_set_from_constraint(ineq);
898 /* Return a basic set containing those elements in the shared space
899 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
901 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
902 __isl_take isl_aff *aff2)
904 aff1 = isl_aff_sub(aff1, aff2);
906 return isl_aff_nonneg_basic_set(aff1);
909 /* Return a basic set containing those elements in the shared space
910 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
912 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
913 __isl_take isl_aff *aff2)
915 return isl_aff_ge_basic_set(aff2, aff1);
918 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
919 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
921 aff1 = isl_aff_add(aff1, aff2);
922 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
923 return aff1;
926 int isl_aff_is_empty(__isl_keep isl_aff *aff)
928 if (!aff)
929 return -1;
931 return 0;
934 /* Check whether the given affine expression has non-zero coefficient
935 * for any dimension in the given range or if any of these dimensions
936 * appear with non-zero coefficients in any of the integer divisions
937 * involved in the affine expression.
939 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
940 enum isl_dim_type type, unsigned first, unsigned n)
942 int i;
943 isl_ctx *ctx;
944 int *active = NULL;
945 int involves = 0;
947 if (!aff)
948 return -1;
949 if (n == 0)
950 return 0;
952 ctx = isl_aff_get_ctx(aff);
953 if (first + n > isl_aff_dim(aff, type))
954 isl_die(ctx, isl_error_invalid,
955 "range out of bounds", return -1);
957 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
958 if (!active)
959 goto error;
961 first += isl_local_space_offset(aff->ls, type) - 1;
962 for (i = 0; i < n; ++i)
963 if (active[first + i]) {
964 involves = 1;
965 break;
968 free(active);
970 return involves;
971 error:
972 free(active);
973 return -1;
976 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
977 enum isl_dim_type type, unsigned first, unsigned n)
979 isl_ctx *ctx;
981 if (!aff)
982 return NULL;
983 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
984 return aff;
986 ctx = isl_aff_get_ctx(aff);
987 if (first + n > isl_aff_dim(aff, type))
988 isl_die(ctx, isl_error_invalid, "range out of bounds",
989 return isl_aff_free(aff));
991 aff = isl_aff_cow(aff);
992 if (!aff)
993 return NULL;
995 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
996 if (!aff->ls)
997 return isl_aff_free(aff);
999 first += 1 + isl_local_space_offset(aff->ls, type);
1000 aff->v = isl_vec_drop_els(aff->v, first, n);
1001 if (!aff->v)
1002 return isl_aff_free(aff);
1004 return aff;
1007 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1008 enum isl_dim_type type, unsigned first, unsigned n)
1010 isl_ctx *ctx;
1012 if (!aff)
1013 return NULL;
1014 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1015 return aff;
1017 ctx = isl_aff_get_ctx(aff);
1018 if (first > isl_aff_dim(aff, type))
1019 isl_die(ctx, isl_error_invalid, "position out of bounds",
1020 return isl_aff_free(aff));
1022 aff = isl_aff_cow(aff);
1023 if (!aff)
1024 return NULL;
1026 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1027 if (!aff->ls)
1028 return isl_aff_free(aff);
1030 first += 1 + isl_local_space_offset(aff->ls, type);
1031 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1032 if (!aff->v)
1033 return isl_aff_free(aff);
1035 return aff;
1038 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1039 enum isl_dim_type type, unsigned n)
1041 unsigned pos;
1043 pos = isl_aff_dim(aff, type);
1045 return isl_aff_insert_dims(aff, type, pos, n);
1048 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1049 enum isl_dim_type type, unsigned n)
1051 unsigned pos;
1053 pos = isl_pw_aff_dim(pwaff, type);
1055 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1058 __isl_give isl_pw_aff *isl_pw_aff_set_tuple_id(__isl_take isl_pw_aff *pwaff,
1059 __isl_take isl_id *id)
1061 isl_space *dim;
1063 dim = isl_pw_aff_get_space(pwaff);
1064 dim = isl_space_set_tuple_id(dim, isl_dim_set, id);
1066 return isl_pw_aff_reset_space(pwaff, dim);
1069 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1071 isl_set *dom = isl_set_universe(isl_aff_get_space(aff));
1072 return isl_pw_aff_alloc(dom, aff);
1075 #undef PW
1076 #define PW isl_pw_aff
1077 #undef EL
1078 #define EL isl_aff
1079 #undef EL_IS_ZERO
1080 #define EL_IS_ZERO is_empty
1081 #undef ZERO
1082 #define ZERO empty
1083 #undef IS_ZERO
1084 #define IS_ZERO is_empty
1085 #undef FIELD
1086 #define FIELD aff
1088 #define NO_EVAL
1089 #define NO_OPT
1090 #define NO_MOVE_DIMS
1091 #define NO_LIFT
1092 #define NO_MORPH
1094 #include <isl_pw_templ.c>
1096 static __isl_give isl_set *align_params_pw_pw_set_and(
1097 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1098 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1099 __isl_take isl_pw_aff *pwaff2))
1101 if (!pwaff1 || !pwaff2)
1102 goto error;
1103 if (isl_space_match(pwaff1->dim, isl_dim_param,
1104 pwaff2->dim, isl_dim_param))
1105 return fn(pwaff1, pwaff2);
1106 if (!isl_space_has_named_params(pwaff1->dim) ||
1107 !isl_space_has_named_params(pwaff2->dim))
1108 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1109 "unaligned unnamed parameters", goto error);
1110 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1111 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1112 return fn(pwaff1, pwaff2);
1113 error:
1114 isl_pw_aff_free(pwaff1);
1115 isl_pw_aff_free(pwaff2);
1116 return NULL;
1119 /* Compute a piecewise quasi-affine expression with a domain that
1120 * is the union of those of pwaff1 and pwaff2 and such that on each
1121 * cell, the quasi-affine expression is the better (according to cmp)
1122 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1123 * is defined on a given cell, then the associated expression
1124 * is the defined one.
1126 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1127 __isl_take isl_pw_aff *pwaff2,
1128 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1129 __isl_take isl_aff *aff2))
1131 int i, j, n;
1132 isl_pw_aff *res;
1133 isl_ctx *ctx;
1134 isl_set *set;
1136 if (!pwaff1 || !pwaff2)
1137 goto error;
1139 ctx = isl_space_get_ctx(pwaff1->dim);
1140 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1141 isl_die(ctx, isl_error_invalid,
1142 "arguments should live in same space", goto error);
1144 if (isl_pw_aff_is_empty(pwaff1)) {
1145 isl_pw_aff_free(pwaff1);
1146 return pwaff2;
1149 if (isl_pw_aff_is_empty(pwaff2)) {
1150 isl_pw_aff_free(pwaff2);
1151 return pwaff1;
1154 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1155 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1157 for (i = 0; i < pwaff1->n; ++i) {
1158 set = isl_set_copy(pwaff1->p[i].set);
1159 for (j = 0; j < pwaff2->n; ++j) {
1160 struct isl_set *common;
1161 isl_set *better;
1163 common = isl_set_intersect(
1164 isl_set_copy(pwaff1->p[i].set),
1165 isl_set_copy(pwaff2->p[j].set));
1166 better = isl_set_from_basic_set(cmp(
1167 isl_aff_copy(pwaff2->p[j].aff),
1168 isl_aff_copy(pwaff1->p[i].aff)));
1169 better = isl_set_intersect(common, better);
1170 if (isl_set_plain_is_empty(better)) {
1171 isl_set_free(better);
1172 continue;
1174 set = isl_set_subtract(set, isl_set_copy(better));
1176 res = isl_pw_aff_add_piece(res, better,
1177 isl_aff_copy(pwaff2->p[j].aff));
1179 res = isl_pw_aff_add_piece(res, set,
1180 isl_aff_copy(pwaff1->p[i].aff));
1183 for (j = 0; j < pwaff2->n; ++j) {
1184 set = isl_set_copy(pwaff2->p[j].set);
1185 for (i = 0; i < pwaff1->n; ++i)
1186 set = isl_set_subtract(set,
1187 isl_set_copy(pwaff1->p[i].set));
1188 res = isl_pw_aff_add_piece(res, set,
1189 isl_aff_copy(pwaff2->p[j].aff));
1192 isl_pw_aff_free(pwaff1);
1193 isl_pw_aff_free(pwaff2);
1195 return res;
1196 error:
1197 isl_pw_aff_free(pwaff1);
1198 isl_pw_aff_free(pwaff2);
1199 return NULL;
1202 /* Compute a piecewise quasi-affine expression with a domain that
1203 * is the union of those of pwaff1 and pwaff2 and such that on each
1204 * cell, the quasi-affine expression is the maximum of those of pwaff1
1205 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1206 * cell, then the associated expression is the defined one.
1208 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1209 __isl_take isl_pw_aff *pwaff2)
1211 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1214 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1215 __isl_take isl_pw_aff *pwaff2)
1217 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_union_max);
1220 /* Compute a piecewise quasi-affine expression with a domain that
1221 * is the union of those of pwaff1 and pwaff2 and such that on each
1222 * cell, the quasi-affine expression is the minimum of those of pwaff1
1223 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1224 * cell, then the associated expression is the defined one.
1226 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1227 __isl_take isl_pw_aff *pwaff2)
1229 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1232 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1233 __isl_take isl_pw_aff *pwaff2)
1235 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_union_min);
1238 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1239 __isl_take isl_pw_aff *pwaff2, int max)
1241 if (max)
1242 return isl_pw_aff_union_max(pwaff1, pwaff2);
1243 else
1244 return isl_pw_aff_union_min(pwaff1, pwaff2);
1247 /* Construct a map with as domain the domain of pwaff and
1248 * one-dimensional range corresponding to the affine expressions.
1250 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1252 int i;
1253 isl_space *dim;
1254 isl_map *map;
1256 if (!pwaff)
1257 return NULL;
1259 dim = isl_pw_aff_get_space(pwaff);
1260 dim = isl_space_from_domain(dim);
1261 dim = isl_space_add_dims(dim, isl_dim_out, 1);
1262 map = isl_map_empty(dim);
1264 for (i = 0; i < pwaff->n; ++i) {
1265 isl_basic_map *bmap;
1266 isl_map *map_i;
1268 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1269 map_i = isl_map_from_basic_map(bmap);
1270 map_i = isl_map_intersect_domain(map_i,
1271 isl_set_copy(pwaff->p[i].set));
1272 map = isl_map_union_disjoint(map, map_i);
1275 isl_pw_aff_free(pwaff);
1277 return map;
1280 /* Construct a map with as domain the domain of pwaff and
1281 * one-dimensional range corresponding to the affine expressions.
1283 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1285 if (isl_space_is_params(pwaff->dim))
1286 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1287 "space of input is not a map",
1288 return isl_pw_aff_free(pwaff));
1289 return map_from_pw_aff(pwaff);
1292 /* Construct a one-dimensional set with as parameter domain
1293 * the domain of pwaff and the single set dimension
1294 * corresponding to the affine expressions.
1296 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1298 if (!isl_space_is_params(pwaff->dim))
1299 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1300 "space of input is not a set",
1301 return isl_pw_aff_free(pwaff));
1302 return map_from_pw_aff(pwaff);
1305 /* Return a set containing those elements in the domain
1306 * of pwaff where it is non-negative.
1308 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1310 int i;
1311 isl_set *set;
1313 if (!pwaff)
1314 return NULL;
1316 set = isl_set_empty(isl_pw_aff_get_space(pwaff));
1318 for (i = 0; i < pwaff->n; ++i) {
1319 isl_basic_set *bset;
1320 isl_set *set_i;
1322 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1323 set_i = isl_set_from_basic_set(bset);
1324 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1325 set = isl_set_union_disjoint(set, set_i);
1328 isl_pw_aff_free(pwaff);
1330 return set;
1333 /* Return a set containing those elements in the domain
1334 * of pwaff where it is zero.
1336 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1338 int i;
1339 isl_set *set;
1341 if (!pwaff)
1342 return NULL;
1344 set = isl_set_empty(isl_pw_aff_get_space(pwaff));
1346 for (i = 0; i < pwaff->n; ++i) {
1347 isl_basic_set *bset;
1348 isl_set *set_i;
1350 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1351 set_i = isl_set_from_basic_set(bset);
1352 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1353 set = isl_set_union_disjoint(set, set_i);
1356 isl_pw_aff_free(pwaff);
1358 return set;
1361 /* Return a set containing those elements in the domain
1362 * of pwaff where it is not zero.
1364 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1366 return isl_set_complement(isl_pw_aff_zero_set(pwaff));
1369 /* Return a set containing those elements in the shared domain
1370 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1372 * We compute the difference on the shared domain and then construct
1373 * the set of values where this difference is non-negative.
1374 * If strict is set, we first subtract 1 from the difference.
1375 * If equal is set, we only return the elements where pwaff1 and pwaff2
1376 * are equal.
1378 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1379 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1381 isl_set *set1, *set2;
1383 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1384 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1385 set1 = isl_set_intersect(set1, set2);
1386 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1387 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1388 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1390 if (strict) {
1391 isl_space *dim = isl_set_get_space(set1);
1392 isl_aff *aff;
1393 aff = isl_aff_zero(isl_local_space_from_space(dim));
1394 aff = isl_aff_add_constant_si(aff, -1);
1395 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1396 } else
1397 isl_set_free(set1);
1399 if (equal)
1400 return isl_pw_aff_zero_set(pwaff1);
1401 return isl_pw_aff_nonneg_set(pwaff1);
1404 /* Return a set containing those elements in the shared domain
1405 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1407 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1408 __isl_take isl_pw_aff *pwaff2)
1410 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1413 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1414 __isl_take isl_pw_aff *pwaff2)
1416 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1419 /* Return a set containing those elements in the shared domain
1420 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1422 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1423 __isl_take isl_pw_aff *pwaff2)
1425 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1428 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1429 __isl_take isl_pw_aff *pwaff2)
1431 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1434 /* Return a set containing those elements in the shared domain
1435 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1437 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1438 __isl_take isl_pw_aff *pwaff2)
1440 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1443 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1444 __isl_take isl_pw_aff *pwaff2)
1446 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1449 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1450 __isl_take isl_pw_aff *pwaff2)
1452 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1455 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1456 __isl_take isl_pw_aff *pwaff2)
1458 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1461 /* Return a set containing those elements in the shared domain
1462 * of the elements of list1 and list2 where each element in list1
1463 * has the relation specified by "fn" with each element in list2.
1465 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1466 __isl_take isl_pw_aff_list *list2,
1467 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1468 __isl_take isl_pw_aff *pwaff2))
1470 int i, j;
1471 isl_ctx *ctx;
1472 isl_set *set;
1474 if (!list1 || !list2)
1475 goto error;
1477 ctx = isl_pw_aff_list_get_ctx(list1);
1478 if (list1->n < 1 || list2->n < 1)
1479 isl_die(ctx, isl_error_invalid,
1480 "list should contain at least one element", goto error);
1482 set = isl_set_universe(isl_pw_aff_get_space(list1->p[0]));
1483 for (i = 0; i < list1->n; ++i)
1484 for (j = 0; j < list2->n; ++j) {
1485 isl_set *set_ij;
1487 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1488 isl_pw_aff_copy(list2->p[j]));
1489 set = isl_set_intersect(set, set_ij);
1492 isl_pw_aff_list_free(list1);
1493 isl_pw_aff_list_free(list2);
1494 return set;
1495 error:
1496 isl_pw_aff_list_free(list1);
1497 isl_pw_aff_list_free(list2);
1498 return NULL;
1501 /* Return a set containing those elements in the shared domain
1502 * of the elements of list1 and list2 where each element in list1
1503 * is equal to each element in list2.
1505 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1506 __isl_take isl_pw_aff_list *list2)
1508 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1511 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1512 __isl_take isl_pw_aff_list *list2)
1514 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1517 /* Return a set containing those elements in the shared domain
1518 * of the elements of list1 and list2 where each element in list1
1519 * is less than or equal to each element in list2.
1521 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1522 __isl_take isl_pw_aff_list *list2)
1524 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1527 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1528 __isl_take isl_pw_aff_list *list2)
1530 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1533 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1534 __isl_take isl_pw_aff_list *list2)
1536 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1539 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1540 __isl_take isl_pw_aff_list *list2)
1542 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1546 /* Return a set containing those elements in the shared domain
1547 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1549 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1550 __isl_take isl_pw_aff *pwaff2)
1552 isl_set *set_lt, *set_gt;
1554 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1555 isl_pw_aff_copy(pwaff2));
1556 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1557 return isl_set_union_disjoint(set_lt, set_gt);
1560 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1561 __isl_take isl_pw_aff *pwaff2)
1563 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1566 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1567 isl_int v)
1569 int i;
1571 if (isl_int_is_one(v))
1572 return pwaff;
1573 if (!isl_int_is_pos(v))
1574 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1575 "factor needs to be positive",
1576 return isl_pw_aff_free(pwaff));
1577 pwaff = isl_pw_aff_cow(pwaff);
1578 if (!pwaff)
1579 return NULL;
1580 if (pwaff->n == 0)
1581 return pwaff;
1583 for (i = 0; i < pwaff->n; ++i) {
1584 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1585 if (!pwaff->p[i].aff)
1586 return isl_pw_aff_free(pwaff);
1589 return pwaff;
1592 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1594 int i;
1596 pwaff = isl_pw_aff_cow(pwaff);
1597 if (!pwaff)
1598 return NULL;
1599 if (pwaff->n == 0)
1600 return pwaff;
1602 for (i = 0; i < pwaff->n; ++i) {
1603 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1604 if (!pwaff->p[i].aff)
1605 return isl_pw_aff_free(pwaff);
1608 return pwaff;
1611 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1613 int i;
1615 pwaff = isl_pw_aff_cow(pwaff);
1616 if (!pwaff)
1617 return NULL;
1618 if (pwaff->n == 0)
1619 return pwaff;
1621 for (i = 0; i < pwaff->n; ++i) {
1622 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1623 if (!pwaff->p[i].aff)
1624 return isl_pw_aff_free(pwaff);
1627 return pwaff;
1630 /* Return an affine expression that is equal to pwaff_true for elements
1631 * in "cond" and to pwaff_false for elements not in "cond".
1632 * That is, return cond ? pwaff_true : pwaff_false;
1634 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_set *cond,
1635 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1637 isl_set *comp;
1639 comp = isl_set_complement(isl_set_copy(cond));
1640 pwaff_true = isl_pw_aff_intersect_domain(pwaff_true, cond);
1641 pwaff_false = isl_pw_aff_intersect_domain(pwaff_false, comp);
1643 return isl_pw_aff_add_disjoint(pwaff_true, pwaff_false);
1646 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1648 if (!aff)
1649 return -1;
1651 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1654 /* Check whether pwaff is a piecewise constant.
1656 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1658 int i;
1660 if (!pwaff)
1661 return -1;
1663 for (i = 0; i < pwaff->n; ++i) {
1664 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1665 if (is_cst < 0 || !is_cst)
1666 return is_cst;
1669 return 1;
1672 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1673 __isl_take isl_aff *aff2)
1675 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1676 return isl_aff_mul(aff2, aff1);
1678 if (!isl_aff_is_cst(aff2))
1679 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1680 "at least one affine expression should be constant",
1681 goto error);
1683 aff1 = isl_aff_cow(aff1);
1684 if (!aff1 || !aff2)
1685 goto error;
1687 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1688 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1690 isl_aff_free(aff2);
1691 return aff1;
1692 error:
1693 isl_aff_free(aff1);
1694 isl_aff_free(aff2);
1695 return NULL;
1698 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1699 __isl_take isl_pw_aff *pwaff2)
1701 int i, j, n;
1702 isl_pw_aff *res;
1704 if (!pwaff1 || !pwaff2)
1705 goto error;
1707 n = pwaff1->n * pwaff2->n;
1708 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1710 for (i = 0; i < pwaff1->n; ++i) {
1711 for (j = 0; j < pwaff2->n; ++j) {
1712 isl_set *common;
1713 isl_aff *prod;
1714 common = isl_set_intersect(
1715 isl_set_copy(pwaff1->p[i].set),
1716 isl_set_copy(pwaff2->p[j].set));
1717 if (isl_set_plain_is_empty(common)) {
1718 isl_set_free(common);
1719 continue;
1722 prod = isl_aff_mul(isl_aff_copy(pwaff1->p[i].aff),
1723 isl_aff_copy(pwaff2->p[j].aff));
1725 res = isl_pw_aff_add_piece(res, common, prod);
1729 isl_pw_aff_free(pwaff1);
1730 isl_pw_aff_free(pwaff2);
1731 return res;
1732 error:
1733 isl_pw_aff_free(pwaff1);
1734 isl_pw_aff_free(pwaff2);
1735 return NULL;
1738 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1739 __isl_take isl_pw_aff *pwaff2)
1741 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1744 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1745 __isl_take isl_pw_aff *pwaff2)
1747 isl_set *le;
1749 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
1750 isl_pw_aff_copy(pwaff2));
1751 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1754 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1755 __isl_take isl_pw_aff *pwaff2)
1757 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
1760 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1761 __isl_take isl_pw_aff *pwaff2)
1763 isl_set *le;
1765 le = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
1766 isl_pw_aff_copy(pwaff2));
1767 return isl_pw_aff_cond(le, pwaff1, pwaff2);
1770 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
1771 __isl_take isl_pw_aff *pwaff2)
1773 return align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
1776 static __isl_give isl_pw_aff *pw_aff_list_reduce(
1777 __isl_take isl_pw_aff_list *list,
1778 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
1779 __isl_take isl_pw_aff *pwaff2))
1781 int i;
1782 isl_ctx *ctx;
1783 isl_pw_aff *res;
1785 if (!list)
1786 return NULL;
1788 ctx = isl_pw_aff_list_get_ctx(list);
1789 if (list->n < 1)
1790 isl_die(ctx, isl_error_invalid,
1791 "list should contain at least one element",
1792 return isl_pw_aff_list_free(list));
1794 res = isl_pw_aff_copy(list->p[0]);
1795 for (i = 1; i < list->n; ++i)
1796 res = fn(res, isl_pw_aff_copy(list->p[i]));
1798 isl_pw_aff_list_free(list);
1799 return res;
1802 /* Return an isl_pw_aff that maps each element in the intersection of the
1803 * domains of the elements of list to the minimal corresponding affine
1804 * expression.
1806 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
1808 return pw_aff_list_reduce(list, &isl_pw_aff_min);
1811 /* Return an isl_pw_aff that maps each element in the intersection of the
1812 * domains of the elements of list to the maximal corresponding affine
1813 * expression.
1815 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
1817 return pw_aff_list_reduce(list, &isl_pw_aff_max);