Merge branch 'maint'
[isl.git] / isl_aff.c
blobbecf73bb1a22c174fa27d0ddd17764112878764c
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the GNU LGPLv2.1 license
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl_aff_private.h>
17 #include <isl_space_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_list_private.h>
21 #include <isl/constraint.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl_config.h>
26 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
27 __isl_take isl_vec *v)
29 isl_aff *aff;
31 if (!ls || !v)
32 goto error;
34 aff = isl_calloc_type(v->ctx, struct isl_aff);
35 if (!aff)
36 goto error;
38 aff->ref = 1;
39 aff->ls = ls;
40 aff->v = v;
42 return aff;
43 error:
44 isl_local_space_free(ls);
45 isl_vec_free(v);
46 return NULL;
49 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
51 isl_ctx *ctx;
52 isl_vec *v;
53 unsigned total;
55 if (!ls)
56 return NULL;
58 ctx = isl_local_space_get_ctx(ls);
59 if (!isl_local_space_divs_known(ls))
60 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
61 goto error);
62 if (!isl_local_space_is_set(ls))
63 isl_die(ctx, isl_error_invalid,
64 "domain of affine expression should be a set",
65 goto error);
67 total = isl_local_space_dim(ls, isl_dim_all);
68 v = isl_vec_alloc(ctx, 1 + 1 + total);
69 return isl_aff_alloc_vec(ls, v);
70 error:
71 isl_local_space_free(ls);
72 return NULL;
75 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
77 isl_aff *aff;
79 aff = isl_aff_alloc(ls);
80 if (!aff)
81 return NULL;
83 isl_int_set_si(aff->v->el[0], 1);
84 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
86 return aff;
89 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
91 if (!aff)
92 return NULL;
94 aff->ref++;
95 return aff;
98 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
100 if (!aff)
101 return NULL;
103 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
104 isl_vec_copy(aff->v));
107 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
109 if (!aff)
110 return NULL;
112 if (aff->ref == 1)
113 return aff;
114 aff->ref--;
115 return isl_aff_dup(aff);
118 void *isl_aff_free(__isl_take isl_aff *aff)
120 if (!aff)
121 return NULL;
123 if (--aff->ref > 0)
124 return NULL;
126 isl_local_space_free(aff->ls);
127 isl_vec_free(aff->v);
129 free(aff);
131 return NULL;
134 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
136 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
139 /* Externally, an isl_aff has a map space, but internally, the
140 * ls field corresponds to the domain of that space.
142 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
144 if (!aff)
145 return 0;
146 if (type == isl_dim_out)
147 return 1;
148 if (type == isl_dim_in)
149 type = isl_dim_set;
150 return isl_local_space_dim(aff->ls, type);
153 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
155 return aff ? isl_local_space_get_space(aff->ls) : NULL;
158 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
160 isl_space *space;
161 if (!aff)
162 return NULL;
163 space = isl_local_space_get_space(aff->ls);
164 space = isl_space_from_domain(space);
165 space = isl_space_add_dims(space, isl_dim_out, 1);
166 return space;
169 __isl_give isl_local_space *isl_aff_get_domain_local_space(
170 __isl_keep isl_aff *aff)
172 return aff ? isl_local_space_copy(aff->ls) : NULL;
175 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
177 isl_local_space *ls;
178 if (!aff)
179 return NULL;
180 ls = isl_local_space_copy(aff->ls);
181 ls = isl_local_space_from_domain(ls);
182 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
183 return ls;
186 /* Externally, an isl_aff has a map space, but internally, the
187 * ls field corresponds to the domain of that space.
189 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
190 enum isl_dim_type type, unsigned pos)
192 if (!aff)
193 return NULL;
194 if (type == isl_dim_out)
195 return NULL;
196 if (type == isl_dim_in)
197 type = isl_dim_set;
198 return isl_local_space_get_dim_name(aff->ls, type, pos);
201 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
202 __isl_take isl_space *dim)
204 aff = isl_aff_cow(aff);
205 if (!aff || !dim)
206 goto error;
208 aff->ls = isl_local_space_reset_space(aff->ls, dim);
209 if (!aff->ls)
210 return isl_aff_free(aff);
212 return aff;
213 error:
214 isl_aff_free(aff);
215 isl_space_free(dim);
216 return NULL;
219 /* Reset the space of "aff". This function is called from isl_pw_templ.c
220 * and doesn't know if the space of an element object is represented
221 * directly or through its domain. It therefore passes along both.
223 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
224 __isl_take isl_space *space, __isl_take isl_space *domain)
226 isl_space_free(space);
227 return isl_aff_reset_domain_space(aff, domain);
230 /* Reorder the coefficients of the affine expression based
231 * on the given reodering.
232 * The reordering r is assumed to have been extended with the local
233 * variables.
235 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
236 __isl_take isl_reordering *r, int n_div)
238 isl_vec *res;
239 int i;
241 if (!vec || !r)
242 goto error;
244 res = isl_vec_alloc(vec->ctx,
245 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
246 isl_seq_cpy(res->el, vec->el, 2);
247 isl_seq_clr(res->el + 2, res->size - 2);
248 for (i = 0; i < r->len; ++i)
249 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
251 isl_reordering_free(r);
252 isl_vec_free(vec);
253 return res;
254 error:
255 isl_vec_free(vec);
256 isl_reordering_free(r);
257 return NULL;
260 /* Reorder the dimensions of the domain of "aff" according
261 * to the given reordering.
263 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
264 __isl_take isl_reordering *r)
266 aff = isl_aff_cow(aff);
267 if (!aff)
268 goto error;
270 r = isl_reordering_extend(r, aff->ls->div->n_row);
271 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
272 aff->ls->div->n_row);
273 aff->ls = isl_local_space_realign(aff->ls, r);
275 if (!aff->v || !aff->ls)
276 return isl_aff_free(aff);
278 return aff;
279 error:
280 isl_aff_free(aff);
281 isl_reordering_free(r);
282 return NULL;
285 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
286 __isl_take isl_space *model)
288 if (!aff || !model)
289 goto error;
291 if (!isl_space_match(aff->ls->dim, isl_dim_param,
292 model, isl_dim_param)) {
293 isl_reordering *exp;
295 model = isl_space_drop_dims(model, isl_dim_in,
296 0, isl_space_dim(model, isl_dim_in));
297 model = isl_space_drop_dims(model, isl_dim_out,
298 0, isl_space_dim(model, isl_dim_out));
299 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
300 exp = isl_reordering_extend_space(exp,
301 isl_aff_get_domain_space(aff));
302 aff = isl_aff_realign_domain(aff, exp);
305 isl_space_free(model);
306 return aff;
307 error:
308 isl_space_free(model);
309 isl_aff_free(aff);
310 return NULL;
313 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
315 if (!aff)
316 return -1;
318 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
321 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
323 int equal;
325 if (!aff1 || !aff2)
326 return -1;
328 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
329 if (equal < 0 || !equal)
330 return equal;
332 return isl_vec_is_equal(aff1->v, aff2->v);
335 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
337 if (!aff)
338 return -1;
339 isl_int_set(*v, aff->v->el[0]);
340 return 0;
343 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
345 if (!aff)
346 return -1;
347 isl_int_set(*v, aff->v->el[1]);
348 return 0;
351 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
352 enum isl_dim_type type, int pos, isl_int *v)
354 if (!aff)
355 return -1;
357 if (type == isl_dim_out)
358 isl_die(aff->v->ctx, isl_error_invalid,
359 "output/set dimension does not have a coefficient",
360 return -1);
361 if (type == isl_dim_in)
362 type = isl_dim_set;
364 if (pos >= isl_local_space_dim(aff->ls, type))
365 isl_die(aff->v->ctx, isl_error_invalid,
366 "position out of bounds", return -1);
368 pos += isl_local_space_offset(aff->ls, type);
369 isl_int_set(*v, aff->v->el[1 + pos]);
371 return 0;
374 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
376 aff = isl_aff_cow(aff);
377 if (!aff)
378 return NULL;
380 aff->v = isl_vec_cow(aff->v);
381 if (!aff->v)
382 return isl_aff_free(aff);
384 isl_int_set(aff->v->el[0], v);
386 return aff;
389 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
391 aff = isl_aff_cow(aff);
392 if (!aff)
393 return NULL;
395 aff->v = isl_vec_cow(aff->v);
396 if (!aff->v)
397 return isl_aff_free(aff);
399 isl_int_set(aff->v->el[1], v);
401 return aff;
404 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
406 if (isl_int_is_zero(v))
407 return aff;
409 aff = isl_aff_cow(aff);
410 if (!aff)
411 return NULL;
413 aff->v = isl_vec_cow(aff->v);
414 if (!aff->v)
415 return isl_aff_free(aff);
417 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
419 return aff;
422 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
424 isl_int t;
426 isl_int_init(t);
427 isl_int_set_si(t, v);
428 aff = isl_aff_add_constant(aff, t);
429 isl_int_clear(t);
431 return aff;
434 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
436 aff = isl_aff_cow(aff);
437 if (!aff)
438 return NULL;
440 aff->v = isl_vec_cow(aff->v);
441 if (!aff->v)
442 return isl_aff_free(aff);
444 isl_int_set_si(aff->v->el[1], v);
446 return aff;
449 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
450 enum isl_dim_type type, int pos, isl_int v)
452 if (!aff)
453 return NULL;
455 if (type == isl_dim_out)
456 isl_die(aff->v->ctx, isl_error_invalid,
457 "output/set dimension does not have a coefficient",
458 return isl_aff_free(aff));
459 if (type == isl_dim_in)
460 type = isl_dim_set;
462 if (pos >= isl_local_space_dim(aff->ls, type))
463 isl_die(aff->v->ctx, isl_error_invalid,
464 "position out of bounds", return isl_aff_free(aff));
466 aff = isl_aff_cow(aff);
467 if (!aff)
468 return NULL;
470 aff->v = isl_vec_cow(aff->v);
471 if (!aff->v)
472 return isl_aff_free(aff);
474 pos += isl_local_space_offset(aff->ls, type);
475 isl_int_set(aff->v->el[1 + pos], v);
477 return aff;
480 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
481 enum isl_dim_type type, int pos, int v)
483 if (!aff)
484 return NULL;
486 if (type == isl_dim_out)
487 isl_die(aff->v->ctx, isl_error_invalid,
488 "output/set dimension does not have a coefficient",
489 return isl_aff_free(aff));
490 if (type == isl_dim_in)
491 type = isl_dim_set;
493 if (pos >= isl_local_space_dim(aff->ls, type))
494 isl_die(aff->v->ctx, isl_error_invalid,
495 "position out of bounds", return isl_aff_free(aff));
497 aff = isl_aff_cow(aff);
498 if (!aff)
499 return NULL;
501 aff->v = isl_vec_cow(aff->v);
502 if (!aff->v)
503 return isl_aff_free(aff);
505 pos += isl_local_space_offset(aff->ls, type);
506 isl_int_set_si(aff->v->el[1 + pos], v);
508 return aff;
511 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
512 enum isl_dim_type type, int pos, isl_int v)
514 if (!aff)
515 return NULL;
517 if (type == isl_dim_out)
518 isl_die(aff->v->ctx, isl_error_invalid,
519 "output/set dimension does not have a coefficient",
520 return isl_aff_free(aff));
521 if (type == isl_dim_in)
522 type = isl_dim_set;
524 if (pos >= isl_local_space_dim(aff->ls, type))
525 isl_die(aff->v->ctx, isl_error_invalid,
526 "position out of bounds", return isl_aff_free(aff));
528 aff = isl_aff_cow(aff);
529 if (!aff)
530 return NULL;
532 aff->v = isl_vec_cow(aff->v);
533 if (!aff->v)
534 return isl_aff_free(aff);
536 pos += isl_local_space_offset(aff->ls, type);
537 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
539 return aff;
542 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
543 enum isl_dim_type type, int pos, int v)
545 isl_int t;
547 isl_int_init(t);
548 isl_int_set_si(t, v);
549 aff = isl_aff_add_coefficient(aff, type, pos, t);
550 isl_int_clear(t);
552 return aff;
555 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
557 if (!aff)
558 return NULL;
560 return isl_local_space_get_div(aff->ls, pos);
563 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
565 aff = isl_aff_cow(aff);
566 if (!aff)
567 return NULL;
568 aff->v = isl_vec_cow(aff->v);
569 if (!aff->v)
570 return isl_aff_free(aff);
572 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
574 return aff;
577 /* Remove divs from the local space that do not appear in the affine
578 * expression.
579 * We currently only remove divs at the end.
580 * Some intermediate divs may also not appear directly in the affine
581 * expression, but we would also need to check that no other divs are
582 * defined in terms of them.
584 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
586 int pos;
587 int off;
588 int n;
590 if (!aff)
591 return NULL;
593 n = isl_local_space_dim(aff->ls, isl_dim_div);
594 off = isl_local_space_offset(aff->ls, isl_dim_div);
596 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
597 if (pos == n)
598 return aff;
600 aff = isl_aff_cow(aff);
601 if (!aff)
602 return NULL;
604 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
605 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
606 if (!aff->ls || !aff->v)
607 return isl_aff_free(aff);
609 return aff;
612 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
614 if (!aff)
615 return NULL;
616 aff->v = isl_vec_normalize(aff->v);
617 if (!aff->v)
618 return isl_aff_free(aff);
619 aff = isl_aff_remove_unused_divs(aff);
620 return aff;
623 /* Given f, return floor(f).
624 * If f is an integer expression, then just return f.
625 * Otherwise, if f = g/m, write g = q m + r,
626 * create a new div d = [r/m] and return the expression q + d.
627 * The coefficients in r are taken to lie between -m/2 and m/2.
629 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
631 int i;
632 int size;
633 isl_ctx *ctx;
634 isl_vec *div;
636 if (!aff)
637 return NULL;
639 if (isl_int_is_one(aff->v->el[0]))
640 return aff;
642 aff = isl_aff_cow(aff);
643 if (!aff)
644 return NULL;
646 aff->v = isl_vec_cow(aff->v);
647 div = isl_vec_copy(aff->v);
648 div = isl_vec_cow(div);
649 if (!div)
650 return isl_aff_free(aff);
652 ctx = isl_aff_get_ctx(aff);
653 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
654 for (i = 1; i < aff->v->size; ++i) {
655 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
656 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
657 if (isl_int_gt(div->el[i], aff->v->el[0])) {
658 isl_int_sub(div->el[i], div->el[i], div->el[0]);
659 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
663 aff->ls = isl_local_space_add_div(aff->ls, div);
664 if (!aff->ls)
665 return isl_aff_free(aff);
667 size = aff->v->size;
668 aff->v = isl_vec_extend(aff->v, size + 1);
669 if (!aff->v)
670 return isl_aff_free(aff);
671 isl_int_set_si(aff->v->el[0], 1);
672 isl_int_set_si(aff->v->el[size], 1);
674 return aff;
677 /* Compute
679 * aff mod m = aff - m * floor(aff/m)
681 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
683 isl_aff *res;
685 res = isl_aff_copy(aff);
686 aff = isl_aff_scale_down(aff, m);
687 aff = isl_aff_floor(aff);
688 aff = isl_aff_scale(aff, m);
689 res = isl_aff_sub(res, aff);
691 return res;
694 /* Compute
696 * pwaff mod m = pwaff - m * floor(pwaff/m)
698 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
700 isl_pw_aff *res;
702 res = isl_pw_aff_copy(pwaff);
703 pwaff = isl_pw_aff_scale_down(pwaff, m);
704 pwaff = isl_pw_aff_floor(pwaff);
705 pwaff = isl_pw_aff_scale(pwaff, m);
706 res = isl_pw_aff_sub(res, pwaff);
708 return res;
711 /* Given f, return ceil(f).
712 * If f is an integer expression, then just return f.
713 * Otherwise, create a new div d = [-f] and return the expression -d.
715 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
717 if (!aff)
718 return NULL;
720 if (isl_int_is_one(aff->v->el[0]))
721 return aff;
723 aff = isl_aff_neg(aff);
724 aff = isl_aff_floor(aff);
725 aff = isl_aff_neg(aff);
727 return aff;
730 /* Apply the expansion computed by isl_merge_divs.
731 * The expansion itself is given by "exp" while the resulting
732 * list of divs is given by "div".
734 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
735 __isl_take isl_mat *div, int *exp)
737 int i, j;
738 int old_n_div;
739 int new_n_div;
740 int offset;
742 aff = isl_aff_cow(aff);
743 if (!aff || !div)
744 goto error;
746 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
747 new_n_div = isl_mat_rows(div);
748 if (new_n_div < old_n_div)
749 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
750 "not an expansion", goto error);
752 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
753 if (!aff->v)
754 goto error;
756 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
757 j = old_n_div - 1;
758 for (i = new_n_div - 1; i >= 0; --i) {
759 if (j >= 0 && exp[j] == i) {
760 if (i != j)
761 isl_int_swap(aff->v->el[offset + i],
762 aff->v->el[offset + j]);
763 j--;
764 } else
765 isl_int_set_si(aff->v->el[offset + i], 0);
768 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
769 if (!aff->ls)
770 goto error;
771 isl_mat_free(div);
772 return aff;
773 error:
774 isl_aff_free(aff);
775 isl_mat_free(div);
776 return NULL;
779 /* Add two affine expressions that live in the same local space.
781 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
782 __isl_take isl_aff *aff2)
784 isl_int gcd, f;
786 aff1 = isl_aff_cow(aff1);
787 if (!aff1 || !aff2)
788 goto error;
790 aff1->v = isl_vec_cow(aff1->v);
791 if (!aff1->v)
792 goto error;
794 isl_int_init(gcd);
795 isl_int_init(f);
796 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
797 isl_int_divexact(f, aff2->v->el[0], gcd);
798 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
799 isl_int_divexact(f, aff1->v->el[0], gcd);
800 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
801 isl_int_divexact(f, aff2->v->el[0], gcd);
802 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
803 isl_int_clear(f);
804 isl_int_clear(gcd);
806 isl_aff_free(aff2);
807 return aff1;
808 error:
809 isl_aff_free(aff1);
810 isl_aff_free(aff2);
811 return NULL;
814 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
815 __isl_take isl_aff *aff2)
817 isl_ctx *ctx;
818 int *exp1 = NULL;
819 int *exp2 = NULL;
820 isl_mat *div;
822 if (!aff1 || !aff2)
823 goto error;
825 ctx = isl_aff_get_ctx(aff1);
826 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
827 isl_die(ctx, isl_error_invalid,
828 "spaces don't match", goto error);
830 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
831 return add_expanded(aff1, aff2);
833 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
834 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
835 if (!exp1 || !exp2)
836 goto error;
838 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
839 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
840 aff2 = isl_aff_expand_divs(aff2, div, exp2);
841 free(exp1);
842 free(exp2);
844 return add_expanded(aff1, aff2);
845 error:
846 free(exp1);
847 free(exp2);
848 isl_aff_free(aff1);
849 isl_aff_free(aff2);
850 return NULL;
853 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
854 __isl_take isl_aff *aff2)
856 return isl_aff_add(aff1, isl_aff_neg(aff2));
859 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
861 isl_int gcd;
863 if (isl_int_is_one(f))
864 return aff;
866 aff = isl_aff_cow(aff);
867 if (!aff)
868 return NULL;
869 aff->v = isl_vec_cow(aff->v);
870 if (!aff->v)
871 return isl_aff_free(aff);
873 isl_int_init(gcd);
874 isl_int_gcd(gcd, aff->v->el[0], f);
875 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
876 isl_int_divexact(gcd, f, gcd);
877 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
878 isl_int_clear(gcd);
880 return aff;
883 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
885 isl_int gcd;
887 if (isl_int_is_one(f))
888 return aff;
890 aff = isl_aff_cow(aff);
891 if (!aff)
892 return NULL;
893 aff->v = isl_vec_cow(aff->v);
894 if (!aff->v)
895 return isl_aff_free(aff);
897 isl_int_init(gcd);
898 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
899 isl_int_gcd(gcd, gcd, f);
900 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
901 isl_int_divexact(gcd, f, gcd);
902 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
903 isl_int_clear(gcd);
905 return aff;
908 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
910 isl_int v;
912 if (f == 1)
913 return aff;
915 isl_int_init(v);
916 isl_int_set_ui(v, f);
917 aff = isl_aff_scale_down(aff, v);
918 isl_int_clear(v);
920 return aff;
923 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
924 enum isl_dim_type type, unsigned pos, const char *s)
926 aff = isl_aff_cow(aff);
927 if (!aff)
928 return NULL;
929 if (type == isl_dim_out)
930 isl_die(aff->v->ctx, isl_error_invalid,
931 "cannot set name of output/set dimension",
932 return isl_aff_free(aff));
933 if (type == isl_dim_in)
934 type = isl_dim_set;
935 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
936 if (!aff->ls)
937 return isl_aff_free(aff);
939 return aff;
942 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
943 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
945 aff = isl_aff_cow(aff);
946 if (!aff)
947 return isl_id_free(id);
948 if (type == isl_dim_out)
949 isl_die(aff->v->ctx, isl_error_invalid,
950 "cannot set name of output/set dimension",
951 goto error);
952 if (type == isl_dim_in)
953 type = isl_dim_set;
954 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
955 if (!aff->ls)
956 return isl_aff_free(aff);
958 return aff;
959 error:
960 isl_id_free(id);
961 isl_aff_free(aff);
962 return NULL;
965 /* Exploit the equalities in "eq" to simplify the affine expression
966 * and the expressions of the integer divisions in the local space.
967 * The integer divisions in this local space are assumed to appear
968 * as regular dimensions in "eq".
970 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
971 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
973 int i, j;
974 unsigned total;
975 unsigned n_div;
977 if (!eq)
978 goto error;
979 if (eq->n_eq == 0) {
980 isl_basic_set_free(eq);
981 return aff;
984 aff = isl_aff_cow(aff);
985 if (!aff)
986 goto error;
988 aff->ls = isl_local_space_substitute_equalities(aff->ls,
989 isl_basic_set_copy(eq));
990 if (!aff->ls)
991 goto error;
993 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
994 n_div = eq->n_div;
995 for (i = 0; i < eq->n_eq; ++i) {
996 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
997 if (j < 0 || j == 0 || j >= total)
998 continue;
1000 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1001 &aff->v->el[0]);
1004 isl_basic_set_free(eq);
1005 aff = isl_aff_normalize(aff);
1006 return aff;
1007 error:
1008 isl_basic_set_free(eq);
1009 isl_aff_free(aff);
1010 return NULL;
1013 /* Exploit the equalities in "eq" to simplify the affine expression
1014 * and the expressions of the integer divisions in the local space.
1016 static __isl_give isl_aff *isl_aff_substitute_equalities(
1017 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1019 int n_div;
1021 if (!aff || !eq)
1022 goto error;
1023 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1024 if (n_div > 0)
1025 eq = isl_basic_set_add(eq, isl_dim_set, n_div);
1026 return isl_aff_substitute_equalities_lifted(aff, eq);
1027 error:
1028 isl_basic_set_free(eq);
1029 isl_aff_free(aff);
1030 return NULL;
1033 /* Look for equalities among the variables shared by context and aff
1034 * and the integer divisions of aff, if any.
1035 * The equalities are then used to eliminate coefficients and/or integer
1036 * divisions from aff.
1038 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1039 __isl_take isl_set *context)
1041 isl_basic_set *hull;
1042 int n_div;
1044 if (!aff)
1045 goto error;
1046 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1047 if (n_div > 0) {
1048 isl_basic_set *bset;
1049 isl_local_space *ls;
1050 context = isl_set_add_dims(context, isl_dim_set, n_div);
1051 ls = isl_aff_get_domain_local_space(aff);
1052 bset = isl_basic_set_from_local_space(ls);
1053 bset = isl_basic_set_lift(bset);
1054 bset = isl_basic_set_flatten(bset);
1055 context = isl_set_intersect(context,
1056 isl_set_from_basic_set(bset));
1059 hull = isl_set_affine_hull(context);
1060 return isl_aff_substitute_equalities_lifted(aff, hull);
1061 error:
1062 isl_aff_free(aff);
1063 isl_set_free(context);
1064 return NULL;
1067 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1068 __isl_take isl_set *context)
1070 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1071 dom_context = isl_set_intersect_params(dom_context, context);
1072 return isl_aff_gist(aff, dom_context);
1075 /* Return a basic set containing those elements in the space
1076 * of aff where it is non-negative.
1078 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1080 isl_constraint *ineq;
1081 isl_basic_set *bset;
1083 ineq = isl_inequality_from_aff(aff);
1085 bset = isl_basic_set_from_constraint(ineq);
1086 bset = isl_basic_set_simplify(bset);
1087 return bset;
1090 /* Return a basic set containing those elements in the space
1091 * of aff where it is zero.
1093 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1095 isl_constraint *ineq;
1096 isl_basic_set *bset;
1098 ineq = isl_equality_from_aff(aff);
1100 bset = isl_basic_set_from_constraint(ineq);
1101 bset = isl_basic_set_simplify(bset);
1102 return bset;
1105 /* Return a basic set containing those elements in the shared space
1106 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1108 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1109 __isl_take isl_aff *aff2)
1111 aff1 = isl_aff_sub(aff1, aff2);
1113 return isl_aff_nonneg_basic_set(aff1);
1116 /* Return a basic set containing those elements in the shared space
1117 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1119 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1120 __isl_take isl_aff *aff2)
1122 return isl_aff_ge_basic_set(aff2, aff1);
1125 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1126 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1128 aff1 = isl_aff_add(aff1, aff2);
1129 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1130 return aff1;
1133 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1135 if (!aff)
1136 return -1;
1138 return 0;
1141 /* Check whether the given affine expression has non-zero coefficient
1142 * for any dimension in the given range or if any of these dimensions
1143 * appear with non-zero coefficients in any of the integer divisions
1144 * involved in the affine expression.
1146 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1147 enum isl_dim_type type, unsigned first, unsigned n)
1149 int i;
1150 isl_ctx *ctx;
1151 int *active = NULL;
1152 int involves = 0;
1154 if (!aff)
1155 return -1;
1156 if (n == 0)
1157 return 0;
1159 ctx = isl_aff_get_ctx(aff);
1160 if (first + n > isl_aff_dim(aff, type))
1161 isl_die(ctx, isl_error_invalid,
1162 "range out of bounds", return -1);
1164 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1165 if (!active)
1166 goto error;
1168 first += isl_local_space_offset(aff->ls, type) - 1;
1169 for (i = 0; i < n; ++i)
1170 if (active[first + i]) {
1171 involves = 1;
1172 break;
1175 free(active);
1177 return involves;
1178 error:
1179 free(active);
1180 return -1;
1183 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1184 enum isl_dim_type type, unsigned first, unsigned n)
1186 isl_ctx *ctx;
1188 if (!aff)
1189 return NULL;
1190 if (type == isl_dim_out)
1191 isl_die(aff->v->ctx, isl_error_invalid,
1192 "cannot drop output/set dimension",
1193 return isl_aff_free(aff));
1194 if (type == isl_dim_in)
1195 type = isl_dim_set;
1196 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1197 return aff;
1199 ctx = isl_aff_get_ctx(aff);
1200 if (first + n > isl_local_space_dim(aff->ls, type))
1201 isl_die(ctx, isl_error_invalid, "range out of bounds",
1202 return isl_aff_free(aff));
1204 aff = isl_aff_cow(aff);
1205 if (!aff)
1206 return NULL;
1208 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1209 if (!aff->ls)
1210 return isl_aff_free(aff);
1212 first += 1 + isl_local_space_offset(aff->ls, type);
1213 aff->v = isl_vec_drop_els(aff->v, first, n);
1214 if (!aff->v)
1215 return isl_aff_free(aff);
1217 return aff;
1220 /* Project the domain of the affine expression onto its parameter space.
1221 * The affine expression may not involve any of the domain dimensions.
1223 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1225 isl_space *space;
1226 unsigned n;
1227 int involves;
1229 n = isl_aff_dim(aff, isl_dim_in);
1230 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1231 if (involves < 0)
1232 return isl_aff_free(aff);
1233 if (involves)
1234 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1235 "affine expression involves some of the domain dimensions",
1236 return isl_aff_free(aff));
1237 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1238 space = isl_aff_get_domain_space(aff);
1239 space = isl_space_params(space);
1240 aff = isl_aff_reset_domain_space(aff, space);
1241 return aff;
1244 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1245 enum isl_dim_type type, unsigned first, unsigned n)
1247 isl_ctx *ctx;
1249 if (!aff)
1250 return NULL;
1251 if (type == isl_dim_out)
1252 isl_die(aff->v->ctx, isl_error_invalid,
1253 "cannot insert output/set dimensions",
1254 return isl_aff_free(aff));
1255 if (type == isl_dim_in)
1256 type = isl_dim_set;
1257 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1258 return aff;
1260 ctx = isl_aff_get_ctx(aff);
1261 if (first > isl_local_space_dim(aff->ls, type))
1262 isl_die(ctx, isl_error_invalid, "position out of bounds",
1263 return isl_aff_free(aff));
1265 aff = isl_aff_cow(aff);
1266 if (!aff)
1267 return NULL;
1269 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1270 if (!aff->ls)
1271 return isl_aff_free(aff);
1273 first += 1 + isl_local_space_offset(aff->ls, type);
1274 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1275 if (!aff->v)
1276 return isl_aff_free(aff);
1278 return aff;
1281 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1282 enum isl_dim_type type, unsigned n)
1284 unsigned pos;
1286 pos = isl_aff_dim(aff, type);
1288 return isl_aff_insert_dims(aff, type, pos, n);
1291 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1292 enum isl_dim_type type, unsigned n)
1294 unsigned pos;
1296 pos = isl_pw_aff_dim(pwaff, type);
1298 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1301 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1303 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1304 return isl_pw_aff_alloc(dom, aff);
1307 #undef PW
1308 #define PW isl_pw_aff
1309 #undef EL
1310 #define EL isl_aff
1311 #undef EL_IS_ZERO
1312 #define EL_IS_ZERO is_empty
1313 #undef ZERO
1314 #define ZERO empty
1315 #undef IS_ZERO
1316 #define IS_ZERO is_empty
1317 #undef FIELD
1318 #define FIELD aff
1319 #undef DEFAULT_IS_ZERO
1320 #define DEFAULT_IS_ZERO 0
1322 #define NO_EVAL
1323 #define NO_OPT
1324 #define NO_MOVE_DIMS
1325 #define NO_LIFT
1326 #define NO_MORPH
1328 #include <isl_pw_templ.c>
1330 static __isl_give isl_set *align_params_pw_pw_set_and(
1331 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1332 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1333 __isl_take isl_pw_aff *pwaff2))
1335 if (!pwaff1 || !pwaff2)
1336 goto error;
1337 if (isl_space_match(pwaff1->dim, isl_dim_param,
1338 pwaff2->dim, isl_dim_param))
1339 return fn(pwaff1, pwaff2);
1340 if (!isl_space_has_named_params(pwaff1->dim) ||
1341 !isl_space_has_named_params(pwaff2->dim))
1342 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1343 "unaligned unnamed parameters", goto error);
1344 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1345 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1346 return fn(pwaff1, pwaff2);
1347 error:
1348 isl_pw_aff_free(pwaff1);
1349 isl_pw_aff_free(pwaff2);
1350 return NULL;
1353 /* Compute a piecewise quasi-affine expression with a domain that
1354 * is the union of those of pwaff1 and pwaff2 and such that on each
1355 * cell, the quasi-affine expression is the better (according to cmp)
1356 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
1357 * is defined on a given cell, then the associated expression
1358 * is the defined one.
1360 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1361 __isl_take isl_pw_aff *pwaff2,
1362 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1363 __isl_take isl_aff *aff2))
1365 int i, j, n;
1366 isl_pw_aff *res;
1367 isl_ctx *ctx;
1368 isl_set *set;
1370 if (!pwaff1 || !pwaff2)
1371 goto error;
1373 ctx = isl_space_get_ctx(pwaff1->dim);
1374 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1375 isl_die(ctx, isl_error_invalid,
1376 "arguments should live in same space", goto error);
1378 if (isl_pw_aff_is_empty(pwaff1)) {
1379 isl_pw_aff_free(pwaff1);
1380 return pwaff2;
1383 if (isl_pw_aff_is_empty(pwaff2)) {
1384 isl_pw_aff_free(pwaff2);
1385 return pwaff1;
1388 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1389 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1391 for (i = 0; i < pwaff1->n; ++i) {
1392 set = isl_set_copy(pwaff1->p[i].set);
1393 for (j = 0; j < pwaff2->n; ++j) {
1394 struct isl_set *common;
1395 isl_set *better;
1397 common = isl_set_intersect(
1398 isl_set_copy(pwaff1->p[i].set),
1399 isl_set_copy(pwaff2->p[j].set));
1400 better = isl_set_from_basic_set(cmp(
1401 isl_aff_copy(pwaff2->p[j].aff),
1402 isl_aff_copy(pwaff1->p[i].aff)));
1403 better = isl_set_intersect(common, better);
1404 if (isl_set_plain_is_empty(better)) {
1405 isl_set_free(better);
1406 continue;
1408 set = isl_set_subtract(set, isl_set_copy(better));
1410 res = isl_pw_aff_add_piece(res, better,
1411 isl_aff_copy(pwaff2->p[j].aff));
1413 res = isl_pw_aff_add_piece(res, set,
1414 isl_aff_copy(pwaff1->p[i].aff));
1417 for (j = 0; j < pwaff2->n; ++j) {
1418 set = isl_set_copy(pwaff2->p[j].set);
1419 for (i = 0; i < pwaff1->n; ++i)
1420 set = isl_set_subtract(set,
1421 isl_set_copy(pwaff1->p[i].set));
1422 res = isl_pw_aff_add_piece(res, set,
1423 isl_aff_copy(pwaff2->p[j].aff));
1426 isl_pw_aff_free(pwaff1);
1427 isl_pw_aff_free(pwaff2);
1429 return res;
1430 error:
1431 isl_pw_aff_free(pwaff1);
1432 isl_pw_aff_free(pwaff2);
1433 return NULL;
1436 /* Compute a piecewise quasi-affine expression with a domain that
1437 * is the union of those of pwaff1 and pwaff2 and such that on each
1438 * cell, the quasi-affine expression is the maximum of those of pwaff1
1439 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1440 * cell, then the associated expression is the defined one.
1442 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1443 __isl_take isl_pw_aff *pwaff2)
1445 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1448 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1449 __isl_take isl_pw_aff *pwaff2)
1451 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1452 &pw_aff_union_max);
1455 /* Compute a piecewise quasi-affine expression with a domain that
1456 * is the union of those of pwaff1 and pwaff2 and such that on each
1457 * cell, the quasi-affine expression is the minimum of those of pwaff1
1458 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
1459 * cell, then the associated expression is the defined one.
1461 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1462 __isl_take isl_pw_aff *pwaff2)
1464 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1467 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1468 __isl_take isl_pw_aff *pwaff2)
1470 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1471 &pw_aff_union_min);
1474 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1475 __isl_take isl_pw_aff *pwaff2, int max)
1477 if (max)
1478 return isl_pw_aff_union_max(pwaff1, pwaff2);
1479 else
1480 return isl_pw_aff_union_min(pwaff1, pwaff2);
1483 /* Construct a map with as domain the domain of pwaff and
1484 * one-dimensional range corresponding to the affine expressions.
1486 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1488 int i;
1489 isl_space *dim;
1490 isl_map *map;
1492 if (!pwaff)
1493 return NULL;
1495 dim = isl_pw_aff_get_space(pwaff);
1496 map = isl_map_empty(dim);
1498 for (i = 0; i < pwaff->n; ++i) {
1499 isl_basic_map *bmap;
1500 isl_map *map_i;
1502 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1503 map_i = isl_map_from_basic_map(bmap);
1504 map_i = isl_map_intersect_domain(map_i,
1505 isl_set_copy(pwaff->p[i].set));
1506 map = isl_map_union_disjoint(map, map_i);
1509 isl_pw_aff_free(pwaff);
1511 return map;
1514 /* Construct a map with as domain the domain of pwaff and
1515 * one-dimensional range corresponding to the affine expressions.
1517 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1519 if (!pwaff)
1520 return NULL;
1521 if (isl_space_is_set(pwaff->dim))
1522 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1523 "space of input is not a map",
1524 return isl_pw_aff_free(pwaff));
1525 return map_from_pw_aff(pwaff);
1528 /* Construct a one-dimensional set with as parameter domain
1529 * the domain of pwaff and the single set dimension
1530 * corresponding to the affine expressions.
1532 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1534 if (!pwaff)
1535 return NULL;
1536 if (!isl_space_is_set(pwaff->dim))
1537 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1538 "space of input is not a set",
1539 return isl_pw_aff_free(pwaff));
1540 return map_from_pw_aff(pwaff);
1543 /* Return a set containing those elements in the domain
1544 * of pwaff where it is non-negative.
1546 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1548 int i;
1549 isl_set *set;
1551 if (!pwaff)
1552 return NULL;
1554 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1556 for (i = 0; i < pwaff->n; ++i) {
1557 isl_basic_set *bset;
1558 isl_set *set_i;
1560 bset = isl_aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff));
1561 set_i = isl_set_from_basic_set(bset);
1562 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
1563 set = isl_set_union_disjoint(set, set_i);
1566 isl_pw_aff_free(pwaff);
1568 return set;
1571 /* Return a set containing those elements in the domain
1572 * of pwaff where it is zero (if complement is 0) or not zero
1573 * (if complement is 1).
1575 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
1576 int complement)
1578 int i;
1579 isl_set *set;
1581 if (!pwaff)
1582 return NULL;
1584 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
1586 for (i = 0; i < pwaff->n; ++i) {
1587 isl_basic_set *bset;
1588 isl_set *set_i, *zero;
1590 bset = isl_aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff));
1591 zero = isl_set_from_basic_set(bset);
1592 set_i = isl_set_copy(pwaff->p[i].set);
1593 if (complement)
1594 set_i = isl_set_subtract(set_i, zero);
1595 else
1596 set_i = isl_set_intersect(set_i, zero);
1597 set = isl_set_union_disjoint(set, set_i);
1600 isl_pw_aff_free(pwaff);
1602 return set;
1605 /* Return a set containing those elements in the domain
1606 * of pwaff where it is zero.
1608 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
1610 return pw_aff_zero_set(pwaff, 0);
1613 /* Return a set containing those elements in the domain
1614 * of pwaff where it is not zero.
1616 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
1618 return pw_aff_zero_set(pwaff, 1);
1621 /* Return a set containing those elements in the shared domain
1622 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1624 * We compute the difference on the shared domain and then construct
1625 * the set of values where this difference is non-negative.
1626 * If strict is set, we first subtract 1 from the difference.
1627 * If equal is set, we only return the elements where pwaff1 and pwaff2
1628 * are equal.
1630 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
1631 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
1633 isl_set *set1, *set2;
1635 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
1636 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
1637 set1 = isl_set_intersect(set1, set2);
1638 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
1639 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
1640 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
1642 if (strict) {
1643 isl_space *dim = isl_set_get_space(set1);
1644 isl_aff *aff;
1645 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
1646 aff = isl_aff_add_constant_si(aff, -1);
1647 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
1648 } else
1649 isl_set_free(set1);
1651 if (equal)
1652 return isl_pw_aff_zero_set(pwaff1);
1653 return isl_pw_aff_nonneg_set(pwaff1);
1656 /* Return a set containing those elements in the shared domain
1657 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
1659 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1660 __isl_take isl_pw_aff *pwaff2)
1662 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
1665 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
1666 __isl_take isl_pw_aff *pwaff2)
1668 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
1671 /* Return a set containing those elements in the shared domain
1672 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1674 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1675 __isl_take isl_pw_aff *pwaff2)
1677 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
1680 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
1681 __isl_take isl_pw_aff *pwaff2)
1683 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
1686 /* Return a set containing those elements in the shared domain
1687 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1689 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1690 __isl_take isl_pw_aff *pwaff2)
1692 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
1695 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
1696 __isl_take isl_pw_aff *pwaff2)
1698 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
1701 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
1702 __isl_take isl_pw_aff *pwaff2)
1704 return isl_pw_aff_ge_set(pwaff2, pwaff1);
1707 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
1708 __isl_take isl_pw_aff *pwaff2)
1710 return isl_pw_aff_gt_set(pwaff2, pwaff1);
1713 /* Return a set containing those elements in the shared domain
1714 * of the elements of list1 and list2 where each element in list1
1715 * has the relation specified by "fn" with each element in list2.
1717 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
1718 __isl_take isl_pw_aff_list *list2,
1719 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1720 __isl_take isl_pw_aff *pwaff2))
1722 int i, j;
1723 isl_ctx *ctx;
1724 isl_set *set;
1726 if (!list1 || !list2)
1727 goto error;
1729 ctx = isl_pw_aff_list_get_ctx(list1);
1730 if (list1->n < 1 || list2->n < 1)
1731 isl_die(ctx, isl_error_invalid,
1732 "list should contain at least one element", goto error);
1734 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
1735 for (i = 0; i < list1->n; ++i)
1736 for (j = 0; j < list2->n; ++j) {
1737 isl_set *set_ij;
1739 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
1740 isl_pw_aff_copy(list2->p[j]));
1741 set = isl_set_intersect(set, set_ij);
1744 isl_pw_aff_list_free(list1);
1745 isl_pw_aff_list_free(list2);
1746 return set;
1747 error:
1748 isl_pw_aff_list_free(list1);
1749 isl_pw_aff_list_free(list2);
1750 return NULL;
1753 /* Return a set containing those elements in the shared domain
1754 * of the elements of list1 and list2 where each element in list1
1755 * is equal to each element in list2.
1757 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
1758 __isl_take isl_pw_aff_list *list2)
1760 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
1763 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
1764 __isl_take isl_pw_aff_list *list2)
1766 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
1769 /* Return a set containing those elements in the shared domain
1770 * of the elements of list1 and list2 where each element in list1
1771 * is less than or equal to each element in list2.
1773 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
1774 __isl_take isl_pw_aff_list *list2)
1776 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
1779 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
1780 __isl_take isl_pw_aff_list *list2)
1782 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
1785 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
1786 __isl_take isl_pw_aff_list *list2)
1788 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
1791 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
1792 __isl_take isl_pw_aff_list *list2)
1794 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
1798 /* Return a set containing those elements in the shared domain
1799 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
1801 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1802 __isl_take isl_pw_aff *pwaff2)
1804 isl_set *set_lt, *set_gt;
1806 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
1807 isl_pw_aff_copy(pwaff2));
1808 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
1809 return isl_set_union_disjoint(set_lt, set_gt);
1812 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
1813 __isl_take isl_pw_aff *pwaff2)
1815 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
1818 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
1819 isl_int v)
1821 int i;
1823 if (isl_int_is_one(v))
1824 return pwaff;
1825 if (!isl_int_is_pos(v))
1826 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1827 "factor needs to be positive",
1828 return isl_pw_aff_free(pwaff));
1829 pwaff = isl_pw_aff_cow(pwaff);
1830 if (!pwaff)
1831 return NULL;
1832 if (pwaff->n == 0)
1833 return pwaff;
1835 for (i = 0; i < pwaff->n; ++i) {
1836 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
1837 if (!pwaff->p[i].aff)
1838 return isl_pw_aff_free(pwaff);
1841 return pwaff;
1844 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
1846 int i;
1848 pwaff = isl_pw_aff_cow(pwaff);
1849 if (!pwaff)
1850 return NULL;
1851 if (pwaff->n == 0)
1852 return pwaff;
1854 for (i = 0; i < pwaff->n; ++i) {
1855 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
1856 if (!pwaff->p[i].aff)
1857 return isl_pw_aff_free(pwaff);
1860 return pwaff;
1863 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
1865 int i;
1867 pwaff = isl_pw_aff_cow(pwaff);
1868 if (!pwaff)
1869 return NULL;
1870 if (pwaff->n == 0)
1871 return pwaff;
1873 for (i = 0; i < pwaff->n; ++i) {
1874 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
1875 if (!pwaff->p[i].aff)
1876 return isl_pw_aff_free(pwaff);
1879 return pwaff;
1882 /* Assuming that "cond1" and "cond2" are disjoint,
1883 * return an affine expression that is equal to pwaff1 on cond1
1884 * and to pwaff2 on cond2.
1886 static __isl_give isl_pw_aff *isl_pw_aff_select(
1887 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
1888 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
1890 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
1891 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
1893 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
1896 /* Return an affine expression that is equal to pwaff_true for elements
1897 * where "cond" is non-zero and to pwaff_false for elements where "cond"
1898 * is zero.
1899 * That is, return cond ? pwaff_true : pwaff_false;
1901 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
1902 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
1904 isl_set *cond_true, *cond_false;
1906 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
1907 cond_false = isl_pw_aff_zero_set(cond);
1908 return isl_pw_aff_select(cond_true, pwaff_true,
1909 cond_false, pwaff_false);
1912 int isl_aff_is_cst(__isl_keep isl_aff *aff)
1914 if (!aff)
1915 return -1;
1917 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
1920 /* Check whether pwaff is a piecewise constant.
1922 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
1924 int i;
1926 if (!pwaff)
1927 return -1;
1929 for (i = 0; i < pwaff->n; ++i) {
1930 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
1931 if (is_cst < 0 || !is_cst)
1932 return is_cst;
1935 return 1;
1938 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
1939 __isl_take isl_aff *aff2)
1941 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
1942 return isl_aff_mul(aff2, aff1);
1944 if (!isl_aff_is_cst(aff2))
1945 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
1946 "at least one affine expression should be constant",
1947 goto error);
1949 aff1 = isl_aff_cow(aff1);
1950 if (!aff1 || !aff2)
1951 goto error;
1953 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
1954 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
1956 isl_aff_free(aff2);
1957 return aff1;
1958 error:
1959 isl_aff_free(aff1);
1960 isl_aff_free(aff2);
1961 return NULL;
1964 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1965 __isl_take isl_pw_aff *pwaff2)
1967 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
1970 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
1971 __isl_take isl_pw_aff *pwaff2)
1973 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
1976 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
1977 __isl_take isl_pw_aff *pwaff2)
1979 return isl_pw_aff_union_add_(pwaff1, pwaff2);
1982 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1983 __isl_take isl_pw_aff *pwaff2)
1985 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
1988 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
1989 __isl_take isl_pw_aff *pwaff2)
1991 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
1994 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
1995 __isl_take isl_pw_aff *pwaff2)
1997 isl_set *le;
1998 isl_set *dom;
2000 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2001 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2002 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2003 isl_pw_aff_copy(pwaff2));
2004 dom = isl_set_subtract(dom, isl_set_copy(le));
2005 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2008 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2009 __isl_take isl_pw_aff *pwaff2)
2011 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2014 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2015 __isl_take isl_pw_aff *pwaff2)
2017 isl_set *ge;
2018 isl_set *dom;
2020 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2021 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2022 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2023 isl_pw_aff_copy(pwaff2));
2024 dom = isl_set_subtract(dom, isl_set_copy(ge));
2025 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2028 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2029 __isl_take isl_pw_aff *pwaff2)
2031 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2034 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2035 __isl_take isl_pw_aff_list *list,
2036 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2037 __isl_take isl_pw_aff *pwaff2))
2039 int i;
2040 isl_ctx *ctx;
2041 isl_pw_aff *res;
2043 if (!list)
2044 return NULL;
2046 ctx = isl_pw_aff_list_get_ctx(list);
2047 if (list->n < 1)
2048 isl_die(ctx, isl_error_invalid,
2049 "list should contain at least one element",
2050 return isl_pw_aff_list_free(list));
2052 res = isl_pw_aff_copy(list->p[0]);
2053 for (i = 1; i < list->n; ++i)
2054 res = fn(res, isl_pw_aff_copy(list->p[i]));
2056 isl_pw_aff_list_free(list);
2057 return res;
2060 /* Return an isl_pw_aff that maps each element in the intersection of the
2061 * domains of the elements of list to the minimal corresponding affine
2062 * expression.
2064 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2066 return pw_aff_list_reduce(list, &isl_pw_aff_min);
2069 /* Return an isl_pw_aff that maps each element in the intersection of the
2070 * domains of the elements of list to the maximal corresponding affine
2071 * expression.
2073 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2075 return pw_aff_list_reduce(list, &isl_pw_aff_max);
2078 #undef BASE
2079 #define BASE aff
2081 #include <isl_multi_templ.c>
2083 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2084 __isl_take isl_multi_aff *maff2)
2086 int i;
2087 isl_ctx *ctx;
2089 maff1 = isl_multi_aff_cow(maff1);
2090 if (!maff1 || !maff2)
2091 goto error;
2093 ctx = isl_multi_aff_get_ctx(maff1);
2094 if (!isl_space_is_equal(maff1->space, maff2->space))
2095 isl_die(ctx, isl_error_invalid,
2096 "spaces don't match", goto error);
2098 for (i = 0; i < maff1->n; ++i) {
2099 maff1->p[i] = isl_aff_add(maff1->p[i],
2100 isl_aff_copy(maff2->p[i]));
2101 if (!maff1->p[i])
2102 goto error;
2105 isl_multi_aff_free(maff2);
2106 return maff1;
2107 error:
2108 isl_multi_aff_free(maff1);
2109 isl_multi_aff_free(maff2);
2110 return NULL;
2113 /* Exploit the equalities in "eq" to simplify the affine expressions.
2115 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2116 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2118 int i;
2120 maff = isl_multi_aff_cow(maff);
2121 if (!maff || !eq)
2122 goto error;
2124 for (i = 0; i < maff->n; ++i) {
2125 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2126 isl_basic_set_copy(eq));
2127 if (!maff->p[i])
2128 goto error;
2131 isl_basic_set_free(eq);
2132 return maff;
2133 error:
2134 isl_basic_set_free(eq);
2135 isl_multi_aff_free(maff);
2136 return NULL;
2139 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2140 isl_int f)
2142 int i;
2144 maff = isl_multi_aff_cow(maff);
2145 if (!maff)
2146 return NULL;
2148 for (i = 0; i < maff->n; ++i) {
2149 maff->p[i] = isl_aff_scale(maff->p[i], f);
2150 if (!maff->p[i])
2151 return isl_multi_aff_free(maff);
2154 return maff;
2157 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2158 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2160 maff1 = isl_multi_aff_add(maff1, maff2);
2161 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2162 return maff1;
2165 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2167 if (!maff)
2168 return -1;
2170 return 0;
2173 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2174 __isl_keep isl_multi_aff *maff2)
2176 int i;
2177 int equal;
2179 if (!maff1 || !maff2)
2180 return -1;
2181 if (maff1->n != maff2->n)
2182 return 0;
2183 equal = isl_space_is_equal(maff1->space, maff2->space);
2184 if (equal < 0 || !equal)
2185 return equal;
2187 for (i = 0; i < maff1->n; ++i) {
2188 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2189 if (equal < 0 || !equal)
2190 return equal;
2193 return 1;
2196 __isl_give isl_multi_aff *isl_multi_aff_set_dim_name(
2197 __isl_take isl_multi_aff *maff,
2198 enum isl_dim_type type, unsigned pos, const char *s)
2200 int i;
2202 maff = isl_multi_aff_cow(maff);
2203 if (!maff)
2204 return NULL;
2206 maff->space = isl_space_set_dim_name(maff->space, type, pos, s);
2207 if (!maff->space)
2208 return isl_multi_aff_free(maff);
2209 for (i = 0; i < maff->n; ++i) {
2210 maff->p[i] = isl_aff_set_dim_name(maff->p[i], type, pos, s);
2211 if (!maff->p[i])
2212 return isl_multi_aff_free(maff);
2215 return maff;
2218 __isl_give isl_multi_aff *isl_multi_aff_drop_dims(__isl_take isl_multi_aff *maff,
2219 enum isl_dim_type type, unsigned first, unsigned n)
2221 int i;
2223 maff = isl_multi_aff_cow(maff);
2224 if (!maff)
2225 return NULL;
2227 maff->space = isl_space_drop_dims(maff->space, type, first, n);
2228 if (!maff->space)
2229 return isl_multi_aff_free(maff);
2231 if (type == isl_dim_out) {
2232 for (i = 0; i < n; ++i)
2233 isl_aff_free(maff->p[first + i]);
2234 for (i = first; i + n < maff->n; ++i)
2235 maff->p[i] = maff->p[i + n];
2236 maff->n -= n;
2237 return maff;
2240 for (i = 0; i < maff->n; ++i) {
2241 maff->p[i] = isl_aff_drop_dims(maff->p[i], type, first, n);
2242 if (!maff->p[i])
2243 return isl_multi_aff_free(maff);
2246 return maff;
2249 #undef PW
2250 #define PW isl_pw_multi_aff
2251 #undef EL
2252 #define EL isl_multi_aff
2253 #undef EL_IS_ZERO
2254 #define EL_IS_ZERO is_empty
2255 #undef ZERO
2256 #define ZERO empty
2257 #undef IS_ZERO
2258 #define IS_ZERO is_empty
2259 #undef FIELD
2260 #define FIELD maff
2261 #undef DEFAULT_IS_ZERO
2262 #define DEFAULT_IS_ZERO 0
2264 #define NO_NEG
2265 #define NO_EVAL
2266 #define NO_OPT
2267 #define NO_INVOLVES_DIMS
2268 #define NO_MOVE_DIMS
2269 #define NO_INSERT_DIMS
2270 #define NO_LIFT
2271 #define NO_MORPH
2273 #include <isl_pw_templ.c>
2275 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
2276 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2278 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
2279 &isl_multi_aff_add);
2282 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
2283 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2285 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
2286 &pw_multi_aff_add);
2289 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
2290 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
2292 return isl_pw_multi_aff_union_add_(pma1, pma2);
2295 /* Construct a map mapping the domain the piecewise multi-affine expression
2296 * to its range, with each dimension in the range equated to the
2297 * corresponding affine expression on its cell.
2299 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2301 int i;
2302 isl_map *map;
2304 if (!pma)
2305 return NULL;
2307 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
2309 for (i = 0; i < pma->n; ++i) {
2310 isl_multi_aff *maff;
2311 isl_basic_map *bmap;
2312 isl_map *map_i;
2314 maff = isl_multi_aff_copy(pma->p[i].maff);
2315 bmap = isl_basic_map_from_multi_aff(maff);
2316 map_i = isl_map_from_basic_map(bmap);
2317 map_i = isl_map_intersect_domain(map_i,
2318 isl_set_copy(pma->p[i].set));
2319 map = isl_map_union_disjoint(map, map_i);
2322 isl_pw_multi_aff_free(pma);
2323 return map;
2326 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
2328 if (!isl_space_is_set(pma->dim))
2329 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2330 "isl_pw_multi_aff cannot be converted into an isl_set",
2331 return isl_pw_multi_aff_free(pma));
2333 return isl_map_from_pw_multi_aff(pma);
2336 /* Given a basic map with a single output dimension that is defined
2337 * in terms of the parameters and input dimensions using an equality,
2338 * extract an isl_aff that expresses the output dimension in terms
2339 * of the parameters and input dimensions.
2341 * Since some applications expect the result of isl_pw_multi_aff_from_map
2342 * to only contain integer affine expressions, we compute the floor
2343 * of the expression before returning.
2345 * This function shares some similarities with
2346 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
2348 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
2349 __isl_take isl_basic_map *bmap)
2351 int i;
2352 unsigned offset;
2353 unsigned total;
2354 isl_local_space *ls;
2355 isl_aff *aff;
2357 if (!bmap)
2358 return NULL;
2359 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
2360 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2361 "basic map should have a single output dimension",
2362 goto error);
2363 offset = isl_basic_map_offset(bmap, isl_dim_out);
2364 total = isl_basic_map_total_dim(bmap);
2365 for (i = 0; i < bmap->n_eq; ++i) {
2366 if (isl_int_is_zero(bmap->eq[i][offset]))
2367 continue;
2368 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
2369 1 + total - (offset + 1)) != -1)
2370 continue;
2371 break;
2373 if (i >= bmap->n_eq)
2374 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
2375 "unable to find suitable equality", goto error);
2376 ls = isl_basic_map_get_local_space(bmap);
2377 aff = isl_aff_alloc(isl_local_space_domain(ls));
2378 if (!aff)
2379 goto error;
2380 if (isl_int_is_neg(bmap->eq[i][offset]))
2381 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
2382 else
2383 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
2384 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
2385 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
2386 isl_basic_map_free(bmap);
2388 aff = isl_aff_remove_unused_divs(aff);
2389 aff = isl_aff_floor(aff);
2390 return aff;
2391 error:
2392 isl_basic_map_free(bmap);
2393 return NULL;
2396 /* Given a basic map where each output dimension is defined
2397 * in terms of the parameters and input dimensions using an equality,
2398 * extract an isl_multi_aff that expresses the output dimensions in terms
2399 * of the parameters and input dimensions.
2401 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
2402 __isl_take isl_basic_map *bmap)
2404 int i;
2405 unsigned n_out;
2406 isl_multi_aff *ma;
2408 if (!bmap)
2409 return NULL;
2411 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
2412 n_out = isl_basic_map_dim(bmap, isl_dim_out);
2414 for (i = 0; i < n_out; ++i) {
2415 isl_basic_map *bmap_i;
2416 isl_aff *aff;
2418 bmap_i = isl_basic_map_copy(bmap);
2419 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
2420 i + 1, n_out - (1 + i));
2421 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
2422 aff = extract_isl_aff_from_basic_map(bmap_i);
2423 ma = isl_multi_aff_set_aff(ma, i, aff);
2426 isl_basic_map_free(bmap);
2428 return ma;
2431 /* Create an isl_pw_multi_aff that is equivalent to
2432 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
2433 * The given basic map is such that each output dimension is defined
2434 * in terms of the parameters and input dimensions using an equality.
2436 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
2437 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
2439 isl_multi_aff *ma;
2441 ma = extract_isl_multi_aff_from_basic_map(bmap);
2442 return isl_pw_multi_aff_alloc(domain, ma);
2445 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
2446 * This obivously only works if the input "map" is single-valued.
2447 * If so, we compute the lexicographic minimum of the image in the form
2448 * of an isl_pw_multi_aff. Since the image is unique, it is equal
2449 * to its lexicographic minimum.
2450 * If the input is not single-valued, we produce an error.
2452 * As a special case, we first check if all output dimensions are uniquely
2453 * defined in terms of the parameters and input dimensions over the entire
2454 * domain. If so, we extract the desired isl_pw_multi_aff directly
2455 * from the affine hull of "map" and its domain.
2457 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
2459 int i;
2460 int sv;
2461 isl_pw_multi_aff *pma;
2462 isl_basic_map *hull;
2464 if (!map)
2465 return NULL;
2467 hull = isl_map_affine_hull(isl_map_copy(map));
2468 sv = isl_basic_map_plain_is_single_valued(hull);
2469 if (sv >= 0 && sv)
2470 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
2471 isl_basic_map_free(hull);
2472 if (sv < 0)
2473 goto error;
2475 sv = isl_map_is_single_valued(map);
2476 if (sv < 0)
2477 goto error;
2478 if (!sv)
2479 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2480 "map is not single-valued", goto error);
2481 map = isl_map_make_disjoint(map);
2482 if (!map)
2483 return NULL;
2485 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
2487 for (i = 0; i < map->n; ++i) {
2488 isl_pw_multi_aff *pma_i;
2489 isl_basic_map *bmap;
2490 bmap = isl_basic_map_copy(map->p[i]);
2491 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
2492 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
2495 isl_map_free(map);
2496 return pma;
2497 error:
2498 isl_map_free(map);
2499 return NULL;
2502 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
2504 return isl_pw_multi_aff_from_map(set);
2507 /* Return the piecewise affine expression "set ? 1 : 0".
2509 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
2511 isl_pw_aff *pa;
2512 isl_space *space = isl_set_get_space(set);
2513 isl_local_space *ls = isl_local_space_from_space(space);
2514 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
2515 isl_aff *one = isl_aff_zero_on_domain(ls);
2517 one = isl_aff_add_constant_si(one, 1);
2518 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
2519 set = isl_set_complement(set);
2520 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
2522 return pa;
2525 /* Plug in "subs" for dimension "type", "pos" of "aff".
2527 * Let i be the dimension to replace and let "subs" be of the form
2529 * f/d
2531 * and "aff" of the form
2533 * (a i + g)/m
2535 * The result is
2537 * floor((a f + d g')/(m d))
2539 * where g' is the result of plugging in "subs" in each of the integer
2540 * divisions in g.
2542 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
2543 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
2545 isl_ctx *ctx;
2546 isl_int v;
2548 aff = isl_aff_cow(aff);
2549 if (!aff || !subs)
2550 return isl_aff_free(aff);
2552 ctx = isl_aff_get_ctx(aff);
2553 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
2554 isl_die(ctx, isl_error_invalid,
2555 "spaces don't match", return isl_aff_free(aff));
2556 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
2557 isl_die(ctx, isl_error_unsupported,
2558 "cannot handle divs yet", return isl_aff_free(aff));
2560 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
2561 if (!aff->ls)
2562 return isl_aff_free(aff);
2564 aff->v = isl_vec_cow(aff->v);
2565 if (!aff->v)
2566 return isl_aff_free(aff);
2568 pos += isl_local_space_offset(aff->ls, type);
2570 isl_int_init(v);
2571 isl_int_set(v, aff->v->el[1 + pos]);
2572 isl_int_set_si(aff->v->el[1 + pos], 0);
2573 isl_seq_combine(aff->v->el + 1, subs->v->el[0], aff->v->el + 1,
2574 v, subs->v->el + 1, subs->v->size - 1);
2575 isl_int_mul(aff->v->el[0], aff->v->el[0], subs->v->el[0]);
2576 isl_int_clear(v);
2578 return aff;
2581 /* Plug in "subs" for dimension "type", "pos" in each of the affine
2582 * expressions in "maff".
2584 __isl_give isl_multi_aff *isl_multi_aff_substitute(
2585 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
2586 __isl_keep isl_aff *subs)
2588 int i;
2590 maff = isl_multi_aff_cow(maff);
2591 if (!maff || !subs)
2592 return isl_multi_aff_free(maff);
2594 if (type == isl_dim_in)
2595 type = isl_dim_set;
2597 for (i = 0; i < maff->n; ++i) {
2598 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
2599 if (!maff->p[i])
2600 return isl_multi_aff_free(maff);
2603 return maff;
2606 /* Plug in "subs" for dimension "type", "pos" of "pma".
2608 * pma is of the form
2610 * A_i(v) -> M_i(v)
2612 * while subs is of the form
2614 * v' = B_j(v) -> S_j
2616 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
2617 * has a contribution in the result, in particular
2619 * C_ij(S_j) -> M_i(S_j)
2621 * Note that plugging in S_j in C_ij may also result in an empty set
2622 * and this contribution should simply be discarded.
2624 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
2625 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
2626 __isl_keep isl_pw_aff *subs)
2628 int i, j, n;
2629 isl_pw_multi_aff *res;
2631 if (!pma || !subs)
2632 return isl_pw_multi_aff_free(pma);
2634 n = pma->n * subs->n;
2635 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
2637 for (i = 0; i < pma->n; ++i) {
2638 for (j = 0; j < subs->n; ++j) {
2639 isl_set *common;
2640 isl_multi_aff *res_ij;
2641 common = isl_set_intersect(
2642 isl_set_copy(pma->p[i].set),
2643 isl_set_copy(subs->p[j].set));
2644 common = isl_set_substitute(common,
2645 type, pos, subs->p[j].aff);
2646 if (isl_set_plain_is_empty(common)) {
2647 isl_set_free(common);
2648 continue;
2651 res_ij = isl_multi_aff_substitute(
2652 isl_multi_aff_copy(pma->p[i].maff),
2653 type, pos, subs->p[j].aff);
2655 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
2659 isl_pw_multi_aff_free(pma);
2660 return res;
2663 /* Extend the local space of "dst" to include the divs
2664 * in the local space of "src".
2666 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
2667 __isl_keep isl_aff *src)
2669 isl_ctx *ctx;
2670 int *exp1 = NULL;
2671 int *exp2 = NULL;
2672 isl_mat *div;
2674 if (!src || !dst)
2675 return isl_aff_free(dst);
2677 ctx = isl_aff_get_ctx(src);
2678 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
2679 isl_die(ctx, isl_error_invalid,
2680 "spaces don't match", goto error);
2682 if (src->ls->div->n_row == 0)
2683 return dst;
2685 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
2686 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
2687 if (!exp1 || !exp2)
2688 goto error;
2690 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
2691 dst = isl_aff_expand_divs(dst, div, exp2);
2692 free(exp1);
2693 free(exp2);
2695 return dst;
2696 error:
2697 free(exp1);
2698 free(exp2);
2699 return isl_aff_free(dst);
2702 /* Adjust the local spaces of the affine expressions in "maff"
2703 * such that they all have the save divs.
2705 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
2706 __isl_take isl_multi_aff *maff)
2708 int i;
2710 if (!maff)
2711 return NULL;
2712 if (maff->n == 0)
2713 return maff;
2714 maff = isl_multi_aff_cow(maff);
2715 if (!maff)
2716 return NULL;
2718 for (i = 1; i < maff->n; ++i)
2719 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
2720 for (i = 1; i < maff->n; ++i) {
2721 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
2722 if (!maff->p[i])
2723 return isl_multi_aff_free(maff);
2726 return maff;
2729 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
2731 aff = isl_aff_cow(aff);
2732 if (!aff)
2733 return NULL;
2735 aff->ls = isl_local_space_lift(aff->ls);
2736 if (!aff->ls)
2737 return isl_aff_free(aff);
2739 return aff;
2742 /* Lift "maff" to a space with extra dimensions such that the result
2743 * has no more existentially quantified variables.
2744 * If "ls" is not NULL, then *ls is assigned the local space that lies
2745 * at the basis of the lifting applied to "maff".
2747 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
2748 __isl_give isl_local_space **ls)
2750 int i;
2751 isl_space *space;
2752 unsigned n_div;
2754 if (ls)
2755 *ls = NULL;
2757 if (!maff)
2758 return NULL;
2760 if (maff->n == 0) {
2761 if (ls) {
2762 isl_space *space = isl_multi_aff_get_domain_space(maff);
2763 *ls = isl_local_space_from_space(space);
2764 if (!*ls)
2765 return isl_multi_aff_free(maff);
2767 return maff;
2770 maff = isl_multi_aff_cow(maff);
2771 maff = isl_multi_aff_align_divs(maff);
2772 if (!maff)
2773 return NULL;
2775 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
2776 space = isl_multi_aff_get_space(maff);
2777 space = isl_space_lift(isl_space_domain(space), n_div);
2778 space = isl_space_extend_domain_with_range(space,
2779 isl_multi_aff_get_space(maff));
2780 if (!space)
2781 return isl_multi_aff_free(maff);
2782 isl_space_free(maff->space);
2783 maff->space = space;
2785 if (ls) {
2786 *ls = isl_aff_get_domain_local_space(maff->p[0]);
2787 if (!*ls)
2788 return isl_multi_aff_free(maff);
2791 for (i = 0; i < maff->n; ++i) {
2792 maff->p[i] = isl_aff_lift(maff->p[i]);
2793 if (!maff->p[i])
2794 goto error;
2797 return maff;
2798 error:
2799 if (ls)
2800 isl_local_space_free(*ls);
2801 return isl_multi_aff_free(maff);
2805 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
2807 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
2808 __isl_keep isl_pw_multi_aff *pma, int pos)
2810 int i;
2811 int n_out;
2812 isl_space *space;
2813 isl_pw_aff *pa;
2815 if (!pma)
2816 return NULL;
2818 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
2819 if (pos < 0 || pos >= n_out)
2820 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
2821 "index out of bounds", return NULL);
2823 space = isl_pw_multi_aff_get_space(pma);
2824 space = isl_space_drop_dims(space, isl_dim_out,
2825 pos + 1, n_out - pos - 1);
2826 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
2828 pa = isl_pw_aff_alloc_size(space, pma->n);
2829 for (i = 0; i < pma->n; ++i) {
2830 isl_aff *aff;
2831 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
2832 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
2835 return pa;