add isl_ast_build_access_from_pw_multi_aff
[isl.git] / isl_aff.c
blob94f8f3ab38314c2aefb72d71138ae864083067f8
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2013 Ecole Normale Superieure
6 * Use of this software is governed by the MIT 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 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_mat_private.h>
23 #include <isl/constraint.h>
24 #include <isl_seq.h>
25 #include <isl/set.h>
26 #include <isl_val_private.h>
27 #include <isl/deprecated/aff_int.h>
28 #include <isl_config.h>
30 #undef BASE
31 #define BASE aff
33 #include <isl_list_templ.c>
35 #undef BASE
36 #define BASE pw_aff
38 #include <isl_list_templ.c>
40 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
41 __isl_take isl_vec *v)
43 isl_aff *aff;
45 if (!ls || !v)
46 goto error;
48 aff = isl_calloc_type(v->ctx, struct isl_aff);
49 if (!aff)
50 goto error;
52 aff->ref = 1;
53 aff->ls = ls;
54 aff->v = v;
56 return aff;
57 error:
58 isl_local_space_free(ls);
59 isl_vec_free(v);
60 return NULL;
63 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
65 isl_ctx *ctx;
66 isl_vec *v;
67 unsigned total;
69 if (!ls)
70 return NULL;
72 ctx = isl_local_space_get_ctx(ls);
73 if (!isl_local_space_divs_known(ls))
74 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
75 goto error);
76 if (!isl_local_space_is_set(ls))
77 isl_die(ctx, isl_error_invalid,
78 "domain of affine expression should be a set",
79 goto error);
81 total = isl_local_space_dim(ls, isl_dim_all);
82 v = isl_vec_alloc(ctx, 1 + 1 + total);
83 return isl_aff_alloc_vec(ls, v);
84 error:
85 isl_local_space_free(ls);
86 return NULL;
89 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
91 isl_aff *aff;
93 aff = isl_aff_alloc(ls);
94 if (!aff)
95 return NULL;
97 isl_int_set_si(aff->v->el[0], 1);
98 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
100 return aff;
103 /* Return a piecewise affine expression defined on the specified domain
104 * that is equal to zero.
106 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
108 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
111 /* Return an affine expression that is equal to "val" on
112 * domain local space "ls".
114 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
115 __isl_take isl_val *val)
117 isl_aff *aff;
119 if (!ls || !val)
120 goto error;
121 if (!isl_val_is_rat(val))
122 isl_die(isl_val_get_ctx(val), isl_error_invalid,
123 "expecting rational value", goto error);
125 aff = isl_aff_alloc(isl_local_space_copy(ls));
126 if (!aff)
127 goto error;
129 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
130 isl_int_set(aff->v->el[1], val->n);
131 isl_int_set(aff->v->el[0], val->d);
133 isl_local_space_free(ls);
134 isl_val_free(val);
135 return aff;
136 error:
137 isl_local_space_free(ls);
138 isl_val_free(val);
139 return NULL;
142 /* Return an affine expression that is equal to the specified dimension
143 * in "ls".
145 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
146 enum isl_dim_type type, unsigned pos)
148 isl_space *space;
149 isl_aff *aff;
151 if (!ls)
152 return NULL;
154 space = isl_local_space_get_space(ls);
155 if (!space)
156 goto error;
157 if (isl_space_is_map(space))
158 isl_die(isl_space_get_ctx(space), isl_error_invalid,
159 "expecting (parameter) set space", goto error);
160 if (pos >= isl_local_space_dim(ls, type))
161 isl_die(isl_space_get_ctx(space), isl_error_invalid,
162 "position out of bounds", goto error);
164 isl_space_free(space);
165 aff = isl_aff_alloc(ls);
166 if (!aff)
167 return NULL;
169 pos += isl_local_space_offset(aff->ls, type);
171 isl_int_set_si(aff->v->el[0], 1);
172 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
173 isl_int_set_si(aff->v->el[1 + pos], 1);
175 return aff;
176 error:
177 isl_local_space_free(ls);
178 isl_space_free(space);
179 return NULL;
182 /* Return a piecewise affine expression that is equal to
183 * the specified dimension in "ls".
185 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
186 enum isl_dim_type type, unsigned pos)
188 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
191 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
193 if (!aff)
194 return NULL;
196 aff->ref++;
197 return aff;
200 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
202 if (!aff)
203 return NULL;
205 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
206 isl_vec_copy(aff->v));
209 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
211 if (!aff)
212 return NULL;
214 if (aff->ref == 1)
215 return aff;
216 aff->ref--;
217 return isl_aff_dup(aff);
220 void *isl_aff_free(__isl_take isl_aff *aff)
222 if (!aff)
223 return NULL;
225 if (--aff->ref > 0)
226 return NULL;
228 isl_local_space_free(aff->ls);
229 isl_vec_free(aff->v);
231 free(aff);
233 return NULL;
236 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
238 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
241 /* Externally, an isl_aff has a map space, but internally, the
242 * ls field corresponds to the domain of that space.
244 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
246 if (!aff)
247 return 0;
248 if (type == isl_dim_out)
249 return 1;
250 if (type == isl_dim_in)
251 type = isl_dim_set;
252 return isl_local_space_dim(aff->ls, type);
255 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
257 return aff ? isl_local_space_get_space(aff->ls) : NULL;
260 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
262 isl_space *space;
263 if (!aff)
264 return NULL;
265 space = isl_local_space_get_space(aff->ls);
266 space = isl_space_from_domain(space);
267 space = isl_space_add_dims(space, isl_dim_out, 1);
268 return space;
271 __isl_give isl_local_space *isl_aff_get_domain_local_space(
272 __isl_keep isl_aff *aff)
274 return aff ? isl_local_space_copy(aff->ls) : NULL;
277 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
279 isl_local_space *ls;
280 if (!aff)
281 return NULL;
282 ls = isl_local_space_copy(aff->ls);
283 ls = isl_local_space_from_domain(ls);
284 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
285 return ls;
288 /* Externally, an isl_aff has a map space, but internally, the
289 * ls field corresponds to the domain of that space.
291 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
292 enum isl_dim_type type, unsigned pos)
294 if (!aff)
295 return NULL;
296 if (type == isl_dim_out)
297 return NULL;
298 if (type == isl_dim_in)
299 type = isl_dim_set;
300 return isl_local_space_get_dim_name(aff->ls, type, pos);
303 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
304 __isl_take isl_space *dim)
306 aff = isl_aff_cow(aff);
307 if (!aff || !dim)
308 goto error;
310 aff->ls = isl_local_space_reset_space(aff->ls, dim);
311 if (!aff->ls)
312 return isl_aff_free(aff);
314 return aff;
315 error:
316 isl_aff_free(aff);
317 isl_space_free(dim);
318 return NULL;
321 /* Reset the space of "aff". This function is called from isl_pw_templ.c
322 * and doesn't know if the space of an element object is represented
323 * directly or through its domain. It therefore passes along both.
325 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
326 __isl_take isl_space *space, __isl_take isl_space *domain)
328 isl_space_free(space);
329 return isl_aff_reset_domain_space(aff, domain);
332 /* Reorder the coefficients of the affine expression based
333 * on the given reodering.
334 * The reordering r is assumed to have been extended with the local
335 * variables.
337 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
338 __isl_take isl_reordering *r, int n_div)
340 isl_vec *res;
341 int i;
343 if (!vec || !r)
344 goto error;
346 res = isl_vec_alloc(vec->ctx,
347 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
348 isl_seq_cpy(res->el, vec->el, 2);
349 isl_seq_clr(res->el + 2, res->size - 2);
350 for (i = 0; i < r->len; ++i)
351 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
353 isl_reordering_free(r);
354 isl_vec_free(vec);
355 return res;
356 error:
357 isl_vec_free(vec);
358 isl_reordering_free(r);
359 return NULL;
362 /* Reorder the dimensions of the domain of "aff" according
363 * to the given reordering.
365 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
366 __isl_take isl_reordering *r)
368 aff = isl_aff_cow(aff);
369 if (!aff)
370 goto error;
372 r = isl_reordering_extend(r, aff->ls->div->n_row);
373 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
374 aff->ls->div->n_row);
375 aff->ls = isl_local_space_realign(aff->ls, r);
377 if (!aff->v || !aff->ls)
378 return isl_aff_free(aff);
380 return aff;
381 error:
382 isl_aff_free(aff);
383 isl_reordering_free(r);
384 return NULL;
387 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
388 __isl_take isl_space *model)
390 if (!aff || !model)
391 goto error;
393 if (!isl_space_match(aff->ls->dim, isl_dim_param,
394 model, isl_dim_param)) {
395 isl_reordering *exp;
397 model = isl_space_drop_dims(model, isl_dim_in,
398 0, isl_space_dim(model, isl_dim_in));
399 model = isl_space_drop_dims(model, isl_dim_out,
400 0, isl_space_dim(model, isl_dim_out));
401 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
402 exp = isl_reordering_extend_space(exp,
403 isl_aff_get_domain_space(aff));
404 aff = isl_aff_realign_domain(aff, exp);
407 isl_space_free(model);
408 return aff;
409 error:
410 isl_space_free(model);
411 isl_aff_free(aff);
412 return NULL;
415 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
417 if (!aff)
418 return -1;
420 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
423 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
425 int equal;
427 if (!aff1 || !aff2)
428 return -1;
430 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
431 if (equal < 0 || !equal)
432 return equal;
434 return isl_vec_is_equal(aff1->v, aff2->v);
437 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
439 if (!aff)
440 return -1;
441 isl_int_set(*v, aff->v->el[0]);
442 return 0;
445 /* Return the common denominator of "aff".
447 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
449 isl_ctx *ctx;
451 if (!aff)
452 return NULL;
454 ctx = isl_aff_get_ctx(aff);
455 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
458 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
460 if (!aff)
461 return -1;
462 isl_int_set(*v, aff->v->el[1]);
463 return 0;
466 /* Return the constant term of "aff".
468 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
470 isl_ctx *ctx;
471 isl_val *v;
473 if (!aff)
474 return NULL;
476 ctx = isl_aff_get_ctx(aff);
477 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
478 return isl_val_normalize(v);
481 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
482 enum isl_dim_type type, int pos, isl_int *v)
484 if (!aff)
485 return -1;
487 if (type == isl_dim_out)
488 isl_die(aff->v->ctx, isl_error_invalid,
489 "output/set dimension does not have a coefficient",
490 return -1);
491 if (type == isl_dim_in)
492 type = isl_dim_set;
494 if (pos >= isl_local_space_dim(aff->ls, type))
495 isl_die(aff->v->ctx, isl_error_invalid,
496 "position out of bounds", return -1);
498 pos += isl_local_space_offset(aff->ls, type);
499 isl_int_set(*v, aff->v->el[1 + pos]);
501 return 0;
504 /* Return the coefficient of the variable of type "type" at position "pos"
505 * of "aff".
507 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
508 enum isl_dim_type type, int pos)
510 isl_ctx *ctx;
511 isl_val *v;
513 if (!aff)
514 return NULL;
516 ctx = isl_aff_get_ctx(aff);
517 if (type == isl_dim_out)
518 isl_die(ctx, isl_error_invalid,
519 "output/set dimension does not have a coefficient",
520 return NULL);
521 if (type == isl_dim_in)
522 type = isl_dim_set;
524 if (pos >= isl_local_space_dim(aff->ls, type))
525 isl_die(ctx, isl_error_invalid,
526 "position out of bounds", return NULL);
528 pos += isl_local_space_offset(aff->ls, type);
529 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
530 return isl_val_normalize(v);
533 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
535 aff = isl_aff_cow(aff);
536 if (!aff)
537 return NULL;
539 aff->v = isl_vec_cow(aff->v);
540 if (!aff->v)
541 return isl_aff_free(aff);
543 isl_int_set(aff->v->el[0], v);
545 return aff;
548 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
550 aff = isl_aff_cow(aff);
551 if (!aff)
552 return NULL;
554 aff->v = isl_vec_cow(aff->v);
555 if (!aff->v)
556 return isl_aff_free(aff);
558 isl_int_set(aff->v->el[1], v);
560 return aff;
563 /* Replace the constant term of "aff" by "v".
565 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
566 __isl_take isl_val *v)
568 if (!aff || !v)
569 goto error;
571 if (!isl_val_is_rat(v))
572 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
573 "expecting rational value", goto error);
575 if (isl_int_eq(aff->v->el[1], v->n) &&
576 isl_int_eq(aff->v->el[0], v->d)) {
577 isl_val_free(v);
578 return aff;
581 aff = isl_aff_cow(aff);
582 if (!aff)
583 goto error;
584 aff->v = isl_vec_cow(aff->v);
585 if (!aff->v)
586 goto error;
588 if (isl_int_eq(aff->v->el[0], v->d)) {
589 isl_int_set(aff->v->el[1], v->n);
590 } else if (isl_int_is_one(v->d)) {
591 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
592 } else {
593 isl_seq_scale(aff->v->el + 1,
594 aff->v->el + 1, v->d, aff->v->size - 1);
595 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
596 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
597 aff->v = isl_vec_normalize(aff->v);
598 if (!aff->v)
599 goto error;
602 isl_val_free(v);
603 return aff;
604 error:
605 isl_aff_free(aff);
606 isl_val_free(v);
607 return NULL;
610 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
612 if (isl_int_is_zero(v))
613 return aff;
615 aff = isl_aff_cow(aff);
616 if (!aff)
617 return NULL;
619 aff->v = isl_vec_cow(aff->v);
620 if (!aff->v)
621 return isl_aff_free(aff);
623 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
625 return aff;
628 /* Add "v" to the constant term of "aff".
630 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
631 __isl_take isl_val *v)
633 if (!aff || !v)
634 goto error;
636 if (isl_val_is_zero(v)) {
637 isl_val_free(v);
638 return aff;
641 if (!isl_val_is_rat(v))
642 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
643 "expecting rational value", goto error);
645 aff = isl_aff_cow(aff);
646 if (!aff)
647 goto error;
649 aff->v = isl_vec_cow(aff->v);
650 if (!aff->v)
651 goto error;
653 if (isl_int_is_one(v->d)) {
654 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
655 } else if (isl_int_eq(aff->v->el[0], v->d)) {
656 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
657 aff->v = isl_vec_normalize(aff->v);
658 if (!aff->v)
659 goto error;
660 } else {
661 isl_seq_scale(aff->v->el + 1,
662 aff->v->el + 1, v->d, aff->v->size - 1);
663 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
664 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
665 aff->v = isl_vec_normalize(aff->v);
666 if (!aff->v)
667 goto error;
670 isl_val_free(v);
671 return aff;
672 error:
673 isl_aff_free(aff);
674 isl_val_free(v);
675 return NULL;
678 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
680 isl_int t;
682 isl_int_init(t);
683 isl_int_set_si(t, v);
684 aff = isl_aff_add_constant(aff, t);
685 isl_int_clear(t);
687 return aff;
690 /* Add "v" to the numerator of the constant term of "aff".
692 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
694 if (isl_int_is_zero(v))
695 return aff;
697 aff = isl_aff_cow(aff);
698 if (!aff)
699 return NULL;
701 aff->v = isl_vec_cow(aff->v);
702 if (!aff->v)
703 return isl_aff_free(aff);
705 isl_int_add(aff->v->el[1], aff->v->el[1], v);
707 return aff;
710 /* Add "v" to the numerator of the constant term of "aff".
712 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
714 isl_int t;
716 if (v == 0)
717 return aff;
719 isl_int_init(t);
720 isl_int_set_si(t, v);
721 aff = isl_aff_add_constant_num(aff, t);
722 isl_int_clear(t);
724 return aff;
727 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
729 aff = isl_aff_cow(aff);
730 if (!aff)
731 return NULL;
733 aff->v = isl_vec_cow(aff->v);
734 if (!aff->v)
735 return isl_aff_free(aff);
737 isl_int_set_si(aff->v->el[1], v);
739 return aff;
742 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
743 enum isl_dim_type type, int pos, isl_int v)
745 if (!aff)
746 return NULL;
748 if (type == isl_dim_out)
749 isl_die(aff->v->ctx, isl_error_invalid,
750 "output/set dimension does not have a coefficient",
751 return isl_aff_free(aff));
752 if (type == isl_dim_in)
753 type = isl_dim_set;
755 if (pos >= isl_local_space_dim(aff->ls, type))
756 isl_die(aff->v->ctx, isl_error_invalid,
757 "position out of bounds", return isl_aff_free(aff));
759 aff = isl_aff_cow(aff);
760 if (!aff)
761 return NULL;
763 aff->v = isl_vec_cow(aff->v);
764 if (!aff->v)
765 return isl_aff_free(aff);
767 pos += isl_local_space_offset(aff->ls, type);
768 isl_int_set(aff->v->el[1 + pos], v);
770 return aff;
773 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
774 enum isl_dim_type type, int pos, int v)
776 if (!aff)
777 return NULL;
779 if (type == isl_dim_out)
780 isl_die(aff->v->ctx, isl_error_invalid,
781 "output/set dimension does not have a coefficient",
782 return isl_aff_free(aff));
783 if (type == isl_dim_in)
784 type = isl_dim_set;
786 if (pos >= isl_local_space_dim(aff->ls, type))
787 isl_die(aff->v->ctx, isl_error_invalid,
788 "position out of bounds", return isl_aff_free(aff));
790 aff = isl_aff_cow(aff);
791 if (!aff)
792 return NULL;
794 aff->v = isl_vec_cow(aff->v);
795 if (!aff->v)
796 return isl_aff_free(aff);
798 pos += isl_local_space_offset(aff->ls, type);
799 isl_int_set_si(aff->v->el[1 + pos], v);
801 return aff;
804 /* Replace the coefficient of the variable of type "type" at position "pos"
805 * of "aff" by "v".
807 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
808 enum isl_dim_type type, int pos, __isl_take isl_val *v)
810 if (!aff || !v)
811 goto error;
813 if (type == isl_dim_out)
814 isl_die(aff->v->ctx, isl_error_invalid,
815 "output/set dimension does not have a coefficient",
816 goto error);
817 if (type == isl_dim_in)
818 type = isl_dim_set;
820 if (pos >= isl_local_space_dim(aff->ls, type))
821 isl_die(aff->v->ctx, isl_error_invalid,
822 "position out of bounds", goto error);
824 if (!isl_val_is_rat(v))
825 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
826 "expecting rational value", goto error);
828 pos += isl_local_space_offset(aff->ls, type);
829 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
830 isl_int_eq(aff->v->el[0], v->d)) {
831 isl_val_free(v);
832 return aff;
835 aff = isl_aff_cow(aff);
836 if (!aff)
837 goto error;
838 aff->v = isl_vec_cow(aff->v);
839 if (!aff->v)
840 goto error;
842 if (isl_int_eq(aff->v->el[0], v->d)) {
843 isl_int_set(aff->v->el[1 + pos], v->n);
844 } else if (isl_int_is_one(v->d)) {
845 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
846 } else {
847 isl_seq_scale(aff->v->el + 1,
848 aff->v->el + 1, v->d, aff->v->size - 1);
849 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
850 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
851 aff->v = isl_vec_normalize(aff->v);
852 if (!aff->v)
853 goto error;
856 isl_val_free(v);
857 return aff;
858 error:
859 isl_aff_free(aff);
860 isl_val_free(v);
861 return NULL;
864 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
865 enum isl_dim_type type, int pos, isl_int v)
867 if (!aff)
868 return NULL;
870 if (type == isl_dim_out)
871 isl_die(aff->v->ctx, isl_error_invalid,
872 "output/set dimension does not have a coefficient",
873 return isl_aff_free(aff));
874 if (type == isl_dim_in)
875 type = isl_dim_set;
877 if (pos >= isl_local_space_dim(aff->ls, type))
878 isl_die(aff->v->ctx, isl_error_invalid,
879 "position out of bounds", return isl_aff_free(aff));
881 aff = isl_aff_cow(aff);
882 if (!aff)
883 return NULL;
885 aff->v = isl_vec_cow(aff->v);
886 if (!aff->v)
887 return isl_aff_free(aff);
889 pos += isl_local_space_offset(aff->ls, type);
890 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
892 return aff;
895 /* Add "v" to the coefficient of the variable of type "type"
896 * at position "pos" of "aff".
898 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
899 enum isl_dim_type type, int pos, __isl_take isl_val *v)
901 if (!aff || !v)
902 goto error;
904 if (isl_val_is_zero(v)) {
905 isl_val_free(v);
906 return aff;
909 if (type == isl_dim_out)
910 isl_die(aff->v->ctx, isl_error_invalid,
911 "output/set dimension does not have a coefficient",
912 goto error);
913 if (type == isl_dim_in)
914 type = isl_dim_set;
916 if (pos >= isl_local_space_dim(aff->ls, type))
917 isl_die(aff->v->ctx, isl_error_invalid,
918 "position out of bounds", goto error);
920 if (!isl_val_is_rat(v))
921 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
922 "expecting rational value", goto error);
924 aff = isl_aff_cow(aff);
925 if (!aff)
926 goto error;
928 aff->v = isl_vec_cow(aff->v);
929 if (!aff->v)
930 goto error;
932 pos += isl_local_space_offset(aff->ls, type);
933 if (isl_int_is_one(v->d)) {
934 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
935 } else if (isl_int_eq(aff->v->el[0], v->d)) {
936 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
937 aff->v = isl_vec_normalize(aff->v);
938 if (!aff->v)
939 goto error;
940 } else {
941 isl_seq_scale(aff->v->el + 1,
942 aff->v->el + 1, v->d, aff->v->size - 1);
943 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
944 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
945 aff->v = isl_vec_normalize(aff->v);
946 if (!aff->v)
947 goto error;
950 isl_val_free(v);
951 return aff;
952 error:
953 isl_aff_free(aff);
954 isl_val_free(v);
955 return NULL;
958 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
959 enum isl_dim_type type, int pos, int v)
961 isl_int t;
963 isl_int_init(t);
964 isl_int_set_si(t, v);
965 aff = isl_aff_add_coefficient(aff, type, pos, t);
966 isl_int_clear(t);
968 return aff;
971 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
973 if (!aff)
974 return NULL;
976 return isl_local_space_get_div(aff->ls, pos);
979 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
981 aff = isl_aff_cow(aff);
982 if (!aff)
983 return NULL;
984 aff->v = isl_vec_cow(aff->v);
985 if (!aff->v)
986 return isl_aff_free(aff);
988 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
990 return aff;
993 /* Remove divs from the local space that do not appear in the affine
994 * expression.
995 * We currently only remove divs at the end.
996 * Some intermediate divs may also not appear directly in the affine
997 * expression, but we would also need to check that no other divs are
998 * defined in terms of them.
1000 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1002 int pos;
1003 int off;
1004 int n;
1006 if (!aff)
1007 return NULL;
1009 n = isl_local_space_dim(aff->ls, isl_dim_div);
1010 off = isl_local_space_offset(aff->ls, isl_dim_div);
1012 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1013 if (pos == n)
1014 return aff;
1016 aff = isl_aff_cow(aff);
1017 if (!aff)
1018 return NULL;
1020 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1021 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1022 if (!aff->ls || !aff->v)
1023 return isl_aff_free(aff);
1025 return aff;
1028 /* Given two affine expressions "p" of length p_len (including the
1029 * denominator and the constant term) and "subs" of length subs_len,
1030 * plug in "subs" for the variable at position "pos".
1031 * The variables of "subs" and "p" are assumed to match up to subs_len,
1032 * but "p" may have additional variables.
1033 * "v" is an initialized isl_int that can be used internally.
1035 * In particular, if "p" represents the expression
1037 * (a i + g)/m
1039 * with i the variable at position "pos" and "subs" represents the expression
1041 * f/d
1043 * then the result represents the expression
1045 * (a f + d g)/(m d)
1048 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1049 int p_len, int subs_len, isl_int v)
1051 isl_int_set(v, p[1 + pos]);
1052 isl_int_set_si(p[1 + pos], 0);
1053 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1054 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1055 isl_int_mul(p[0], p[0], subs[0]);
1058 /* Look for any divs in the aff->ls with a denominator equal to one
1059 * and plug them into the affine expression and any subsequent divs
1060 * that may reference the div.
1062 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1064 int i, n;
1065 int len;
1066 isl_int v;
1067 isl_vec *vec;
1068 isl_local_space *ls;
1069 unsigned pos;
1071 if (!aff)
1072 return NULL;
1074 n = isl_local_space_dim(aff->ls, isl_dim_div);
1075 len = aff->v->size;
1076 for (i = 0; i < n; ++i) {
1077 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1078 continue;
1079 ls = isl_local_space_copy(aff->ls);
1080 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1081 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1082 vec = isl_vec_copy(aff->v);
1083 vec = isl_vec_cow(vec);
1084 if (!ls || !vec)
1085 goto error;
1087 isl_int_init(v);
1089 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1090 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1091 len, len, v);
1093 isl_int_clear(v);
1095 isl_vec_free(aff->v);
1096 aff->v = vec;
1097 isl_local_space_free(aff->ls);
1098 aff->ls = ls;
1101 return aff;
1102 error:
1103 isl_vec_free(vec);
1104 isl_local_space_free(ls);
1105 return isl_aff_free(aff);
1108 /* Look for any divs j that appear with a unit coefficient inside
1109 * the definitions of other divs i and plug them into the definitions
1110 * of the divs i.
1112 * In particular, an expression of the form
1114 * floor((f(..) + floor(g(..)/n))/m)
1116 * is simplified to
1118 * floor((n * f(..) + g(..))/(n * m))
1120 * This simplification is correct because we can move the expression
1121 * f(..) into the inner floor in the original expression to obtain
1123 * floor(floor((n * f(..) + g(..))/n)/m)
1125 * from which we can derive the simplified expression.
1127 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1129 int i, j, n;
1130 int off;
1132 if (!aff)
1133 return NULL;
1135 n = isl_local_space_dim(aff->ls, isl_dim_div);
1136 off = isl_local_space_offset(aff->ls, isl_dim_div);
1137 for (i = 1; i < n; ++i) {
1138 for (j = 0; j < i; ++j) {
1139 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1140 continue;
1141 aff->ls = isl_local_space_substitute_seq(aff->ls,
1142 isl_dim_div, j, aff->ls->div->row[j],
1143 aff->v->size, i, 1);
1144 if (!aff->ls)
1145 return isl_aff_free(aff);
1149 return aff;
1152 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1154 * Even though this function is only called on isl_affs with a single
1155 * reference, we are careful to only change aff->v and aff->ls together.
1157 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1159 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1160 isl_local_space *ls;
1161 isl_vec *v;
1163 ls = isl_local_space_copy(aff->ls);
1164 ls = isl_local_space_swap_div(ls, a, b);
1165 v = isl_vec_copy(aff->v);
1166 v = isl_vec_cow(v);
1167 if (!ls || !v)
1168 goto error;
1170 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1171 isl_vec_free(aff->v);
1172 aff->v = v;
1173 isl_local_space_free(aff->ls);
1174 aff->ls = ls;
1176 return aff;
1177 error:
1178 isl_vec_free(v);
1179 isl_local_space_free(ls);
1180 return isl_aff_free(aff);
1183 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1185 * We currently do not actually remove div "b", but simply add its
1186 * coefficient to that of "a" and then zero it out.
1188 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1190 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1192 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1193 return aff;
1195 aff->v = isl_vec_cow(aff->v);
1196 if (!aff->v)
1197 return isl_aff_free(aff);
1199 isl_int_add(aff->v->el[1 + off + a],
1200 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1201 isl_int_set_si(aff->v->el[1 + off + b], 0);
1203 return aff;
1206 /* Sort the divs in the local space of "aff" according to
1207 * the comparison function "cmp_row" in isl_local_space.c,
1208 * combining the coefficients of identical divs.
1210 * Reordering divs does not change the semantics of "aff",
1211 * so there is no need to call isl_aff_cow.
1212 * Moreover, this function is currently only called on isl_affs
1213 * with a single reference.
1215 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1217 int i, j, n;
1218 unsigned off;
1220 if (!aff)
1221 return NULL;
1223 off = isl_local_space_offset(aff->ls, isl_dim_div);
1224 n = isl_aff_dim(aff, isl_dim_div);
1225 for (i = 1; i < n; ++i) {
1226 for (j = i - 1; j >= 0; --j) {
1227 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1228 if (cmp < 0)
1229 break;
1230 if (cmp == 0)
1231 aff = merge_divs(aff, j, j + 1);
1232 else
1233 aff = swap_div(aff, j, j + 1);
1234 if (!aff)
1235 return NULL;
1239 return aff;
1242 /* Normalize the representation of "aff".
1244 * This function should only be called of "new" isl_affs, i.e.,
1245 * with only a single reference. We therefore do not need to
1246 * worry about affecting other instances.
1248 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1250 if (!aff)
1251 return NULL;
1252 aff->v = isl_vec_normalize(aff->v);
1253 if (!aff->v)
1254 return isl_aff_free(aff);
1255 aff = plug_in_integral_divs(aff);
1256 aff = plug_in_unit_divs(aff);
1257 aff = sort_divs(aff);
1258 aff = isl_aff_remove_unused_divs(aff);
1259 return aff;
1262 /* Given f, return floor(f).
1263 * If f is an integer expression, then just return f.
1264 * If f is a constant, then return the constant floor(f).
1265 * Otherwise, if f = g/m, write g = q m + r,
1266 * create a new div d = [r/m] and return the expression q + d.
1267 * The coefficients in r are taken to lie between -m/2 and m/2.
1269 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1271 int i;
1272 int size;
1273 isl_ctx *ctx;
1274 isl_vec *div;
1276 if (!aff)
1277 return NULL;
1279 if (isl_int_is_one(aff->v->el[0]))
1280 return aff;
1282 aff = isl_aff_cow(aff);
1283 if (!aff)
1284 return NULL;
1286 aff->v = isl_vec_cow(aff->v);
1287 if (!aff->v)
1288 return isl_aff_free(aff);
1290 if (isl_aff_is_cst(aff)) {
1291 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1292 isl_int_set_si(aff->v->el[0], 1);
1293 return aff;
1296 div = isl_vec_copy(aff->v);
1297 div = isl_vec_cow(div);
1298 if (!div)
1299 return isl_aff_free(aff);
1301 ctx = isl_aff_get_ctx(aff);
1302 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1303 for (i = 1; i < aff->v->size; ++i) {
1304 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1305 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1306 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1307 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1308 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1312 aff->ls = isl_local_space_add_div(aff->ls, div);
1313 if (!aff->ls)
1314 return isl_aff_free(aff);
1316 size = aff->v->size;
1317 aff->v = isl_vec_extend(aff->v, size + 1);
1318 if (!aff->v)
1319 return isl_aff_free(aff);
1320 isl_int_set_si(aff->v->el[0], 1);
1321 isl_int_set_si(aff->v->el[size], 1);
1323 aff = isl_aff_normalize(aff);
1325 return aff;
1328 /* Compute
1330 * aff mod m = aff - m * floor(aff/m)
1332 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1334 isl_aff *res;
1336 res = isl_aff_copy(aff);
1337 aff = isl_aff_scale_down(aff, m);
1338 aff = isl_aff_floor(aff);
1339 aff = isl_aff_scale(aff, m);
1340 res = isl_aff_sub(res, aff);
1342 return res;
1345 /* Compute
1347 * aff mod m = aff - m * floor(aff/m)
1349 * with m an integer value.
1351 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1352 __isl_take isl_val *m)
1354 isl_aff *res;
1356 if (!aff || !m)
1357 goto error;
1359 if (!isl_val_is_int(m))
1360 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1361 "expecting integer modulo", goto error);
1363 res = isl_aff_copy(aff);
1364 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1365 aff = isl_aff_floor(aff);
1366 aff = isl_aff_scale_val(aff, m);
1367 res = isl_aff_sub(res, aff);
1369 return res;
1370 error:
1371 isl_aff_free(aff);
1372 isl_val_free(m);
1373 return NULL;
1376 /* Compute
1378 * pwaff mod m = pwaff - m * floor(pwaff/m)
1380 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1382 isl_pw_aff *res;
1384 res = isl_pw_aff_copy(pwaff);
1385 pwaff = isl_pw_aff_scale_down(pwaff, m);
1386 pwaff = isl_pw_aff_floor(pwaff);
1387 pwaff = isl_pw_aff_scale(pwaff, m);
1388 res = isl_pw_aff_sub(res, pwaff);
1390 return res;
1393 /* Compute
1395 * pa mod m = pa - m * floor(pa/m)
1397 * with m an integer value.
1399 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1400 __isl_take isl_val *m)
1402 if (!pa || !m)
1403 goto error;
1404 if (!isl_val_is_int(m))
1405 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1406 "expecting integer modulo", goto error);
1407 pa = isl_pw_aff_mod(pa, m->n);
1408 isl_val_free(m);
1409 return pa;
1410 error:
1411 isl_pw_aff_free(pa);
1412 isl_val_free(m);
1413 return NULL;
1416 /* Given f, return ceil(f).
1417 * If f is an integer expression, then just return f.
1418 * Otherwise, let f be the expression
1420 * e/m
1422 * then return
1424 * floor((e + m - 1)/m)
1426 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1428 if (!aff)
1429 return NULL;
1431 if (isl_int_is_one(aff->v->el[0]))
1432 return aff;
1434 aff = isl_aff_cow(aff);
1435 if (!aff)
1436 return NULL;
1437 aff->v = isl_vec_cow(aff->v);
1438 if (!aff->v)
1439 return isl_aff_free(aff);
1441 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1442 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1443 aff = isl_aff_floor(aff);
1445 return aff;
1448 /* Apply the expansion computed by isl_merge_divs.
1449 * The expansion itself is given by "exp" while the resulting
1450 * list of divs is given by "div".
1452 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1453 __isl_take isl_mat *div, int *exp)
1455 int i, j;
1456 int old_n_div;
1457 int new_n_div;
1458 int offset;
1460 aff = isl_aff_cow(aff);
1461 if (!aff || !div)
1462 goto error;
1464 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1465 new_n_div = isl_mat_rows(div);
1466 if (new_n_div < old_n_div)
1467 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1468 "not an expansion", goto error);
1470 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1471 if (!aff->v)
1472 goto error;
1474 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1475 j = old_n_div - 1;
1476 for (i = new_n_div - 1; i >= 0; --i) {
1477 if (j >= 0 && exp[j] == i) {
1478 if (i != j)
1479 isl_int_swap(aff->v->el[offset + i],
1480 aff->v->el[offset + j]);
1481 j--;
1482 } else
1483 isl_int_set_si(aff->v->el[offset + i], 0);
1486 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1487 if (!aff->ls)
1488 goto error;
1489 isl_mat_free(div);
1490 return aff;
1491 error:
1492 isl_aff_free(aff);
1493 isl_mat_free(div);
1494 return NULL;
1497 /* Add two affine expressions that live in the same local space.
1499 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1500 __isl_take isl_aff *aff2)
1502 isl_int gcd, f;
1504 aff1 = isl_aff_cow(aff1);
1505 if (!aff1 || !aff2)
1506 goto error;
1508 aff1->v = isl_vec_cow(aff1->v);
1509 if (!aff1->v)
1510 goto error;
1512 isl_int_init(gcd);
1513 isl_int_init(f);
1514 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1515 isl_int_divexact(f, aff2->v->el[0], gcd);
1516 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1517 isl_int_divexact(f, aff1->v->el[0], gcd);
1518 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1519 isl_int_divexact(f, aff2->v->el[0], gcd);
1520 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1521 isl_int_clear(f);
1522 isl_int_clear(gcd);
1524 isl_aff_free(aff2);
1525 return aff1;
1526 error:
1527 isl_aff_free(aff1);
1528 isl_aff_free(aff2);
1529 return NULL;
1532 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1533 __isl_take isl_aff *aff2)
1535 isl_ctx *ctx;
1536 int *exp1 = NULL;
1537 int *exp2 = NULL;
1538 isl_mat *div;
1539 int n_div1, n_div2;
1541 if (!aff1 || !aff2)
1542 goto error;
1544 ctx = isl_aff_get_ctx(aff1);
1545 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1546 isl_die(ctx, isl_error_invalid,
1547 "spaces don't match", goto error);
1549 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1550 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1551 if (n_div1 == 0 && n_div2 == 0)
1552 return add_expanded(aff1, aff2);
1554 exp1 = isl_alloc_array(ctx, int, n_div1);
1555 exp2 = isl_alloc_array(ctx, int, n_div2);
1556 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1557 goto error;
1559 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1560 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1561 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1562 free(exp1);
1563 free(exp2);
1565 return add_expanded(aff1, aff2);
1566 error:
1567 free(exp1);
1568 free(exp2);
1569 isl_aff_free(aff1);
1570 isl_aff_free(aff2);
1571 return NULL;
1574 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1575 __isl_take isl_aff *aff2)
1577 return isl_aff_add(aff1, isl_aff_neg(aff2));
1580 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1582 isl_int gcd;
1584 if (isl_int_is_one(f))
1585 return aff;
1587 aff = isl_aff_cow(aff);
1588 if (!aff)
1589 return NULL;
1590 aff->v = isl_vec_cow(aff->v);
1591 if (!aff->v)
1592 return isl_aff_free(aff);
1594 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1595 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1596 return aff;
1599 isl_int_init(gcd);
1600 isl_int_gcd(gcd, aff->v->el[0], f);
1601 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1602 isl_int_divexact(gcd, f, gcd);
1603 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1604 isl_int_clear(gcd);
1606 return aff;
1609 /* Multiple "aff" by "v".
1611 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1612 __isl_take isl_val *v)
1614 if (!aff || !v)
1615 goto error;
1617 if (isl_val_is_one(v)) {
1618 isl_val_free(v);
1619 return aff;
1622 if (!isl_val_is_rat(v))
1623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1624 "expecting rational factor", goto error);
1626 aff = isl_aff_scale(aff, v->n);
1627 aff = isl_aff_scale_down(aff, v->d);
1629 isl_val_free(v);
1630 return aff;
1631 error:
1632 isl_aff_free(aff);
1633 isl_val_free(v);
1634 return NULL;
1637 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1639 isl_int gcd;
1641 if (isl_int_is_one(f))
1642 return aff;
1644 aff = isl_aff_cow(aff);
1645 if (!aff)
1646 return NULL;
1648 if (isl_int_is_zero(f))
1649 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1650 "cannot scale down by zero", return isl_aff_free(aff));
1652 aff->v = isl_vec_cow(aff->v);
1653 if (!aff->v)
1654 return isl_aff_free(aff);
1656 isl_int_init(gcd);
1657 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1658 isl_int_gcd(gcd, gcd, f);
1659 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1660 isl_int_divexact(gcd, f, gcd);
1661 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1662 isl_int_clear(gcd);
1664 return aff;
1667 /* Divide "aff" by "v".
1669 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1670 __isl_take isl_val *v)
1672 if (!aff || !v)
1673 goto error;
1675 if (isl_val_is_one(v)) {
1676 isl_val_free(v);
1677 return aff;
1680 if (!isl_val_is_rat(v))
1681 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1682 "expecting rational factor", goto error);
1683 if (!isl_val_is_pos(v))
1684 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1685 "factor needs to be positive", goto error);
1687 aff = isl_aff_scale(aff, v->d);
1688 aff = isl_aff_scale_down(aff, v->n);
1690 isl_val_free(v);
1691 return aff;
1692 error:
1693 isl_aff_free(aff);
1694 isl_val_free(v);
1695 return NULL;
1698 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1700 isl_int v;
1702 if (f == 1)
1703 return aff;
1705 isl_int_init(v);
1706 isl_int_set_ui(v, f);
1707 aff = isl_aff_scale_down(aff, v);
1708 isl_int_clear(v);
1710 return aff;
1713 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1714 enum isl_dim_type type, unsigned pos, const char *s)
1716 aff = isl_aff_cow(aff);
1717 if (!aff)
1718 return NULL;
1719 if (type == isl_dim_out)
1720 isl_die(aff->v->ctx, isl_error_invalid,
1721 "cannot set name of output/set dimension",
1722 return isl_aff_free(aff));
1723 if (type == isl_dim_in)
1724 type = isl_dim_set;
1725 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1726 if (!aff->ls)
1727 return isl_aff_free(aff);
1729 return aff;
1732 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1733 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1735 aff = isl_aff_cow(aff);
1736 if (!aff)
1737 return isl_id_free(id);
1738 if (type == isl_dim_out)
1739 isl_die(aff->v->ctx, isl_error_invalid,
1740 "cannot set name of output/set dimension",
1741 goto error);
1742 if (type == isl_dim_in)
1743 type = isl_dim_set;
1744 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1745 if (!aff->ls)
1746 return isl_aff_free(aff);
1748 return aff;
1749 error:
1750 isl_id_free(id);
1751 isl_aff_free(aff);
1752 return NULL;
1755 /* Exploit the equalities in "eq" to simplify the affine expression
1756 * and the expressions of the integer divisions in the local space.
1757 * The integer divisions in this local space are assumed to appear
1758 * as regular dimensions in "eq".
1760 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1761 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1763 int i, j;
1764 unsigned total;
1765 unsigned n_div;
1767 if (!eq)
1768 goto error;
1769 if (eq->n_eq == 0) {
1770 isl_basic_set_free(eq);
1771 return aff;
1774 aff = isl_aff_cow(aff);
1775 if (!aff)
1776 goto error;
1778 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1779 isl_basic_set_copy(eq));
1780 aff->v = isl_vec_cow(aff->v);
1781 if (!aff->ls || !aff->v)
1782 goto error;
1784 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1785 n_div = eq->n_div;
1786 for (i = 0; i < eq->n_eq; ++i) {
1787 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1788 if (j < 0 || j == 0 || j >= total)
1789 continue;
1791 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1792 &aff->v->el[0]);
1795 isl_basic_set_free(eq);
1796 aff = isl_aff_normalize(aff);
1797 return aff;
1798 error:
1799 isl_basic_set_free(eq);
1800 isl_aff_free(aff);
1801 return NULL;
1804 /* Exploit the equalities in "eq" to simplify the affine expression
1805 * and the expressions of the integer divisions in the local space.
1807 static __isl_give isl_aff *isl_aff_substitute_equalities(
1808 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1810 int n_div;
1812 if (!aff || !eq)
1813 goto error;
1814 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1815 if (n_div > 0)
1816 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1817 return isl_aff_substitute_equalities_lifted(aff, eq);
1818 error:
1819 isl_basic_set_free(eq);
1820 isl_aff_free(aff);
1821 return NULL;
1824 /* Look for equalities among the variables shared by context and aff
1825 * and the integer divisions of aff, if any.
1826 * The equalities are then used to eliminate coefficients and/or integer
1827 * divisions from aff.
1829 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1830 __isl_take isl_set *context)
1832 isl_basic_set *hull;
1833 int n_div;
1835 if (!aff)
1836 goto error;
1837 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1838 if (n_div > 0) {
1839 isl_basic_set *bset;
1840 isl_local_space *ls;
1841 context = isl_set_add_dims(context, isl_dim_set, n_div);
1842 ls = isl_aff_get_domain_local_space(aff);
1843 bset = isl_basic_set_from_local_space(ls);
1844 bset = isl_basic_set_lift(bset);
1845 bset = isl_basic_set_flatten(bset);
1846 context = isl_set_intersect(context,
1847 isl_set_from_basic_set(bset));
1850 hull = isl_set_affine_hull(context);
1851 return isl_aff_substitute_equalities_lifted(aff, hull);
1852 error:
1853 isl_aff_free(aff);
1854 isl_set_free(context);
1855 return NULL;
1858 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1859 __isl_take isl_set *context)
1861 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1862 dom_context = isl_set_intersect_params(dom_context, context);
1863 return isl_aff_gist(aff, dom_context);
1866 /* Return a basic set containing those elements in the space
1867 * of aff where it is non-negative.
1868 * If "rational" is set, then return a rational basic set.
1870 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1871 __isl_take isl_aff *aff, int rational)
1873 isl_constraint *ineq;
1874 isl_basic_set *bset;
1876 ineq = isl_inequality_from_aff(aff);
1878 bset = isl_basic_set_from_constraint(ineq);
1879 if (rational)
1880 bset = isl_basic_set_set_rational(bset);
1881 bset = isl_basic_set_simplify(bset);
1882 return bset;
1885 /* Return a basic set containing those elements in the space
1886 * of aff where it is non-negative.
1888 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1890 return aff_nonneg_basic_set(aff, 0);
1893 /* Return a basic set containing those elements in the domain space
1894 * of aff where it is negative.
1896 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1898 aff = isl_aff_neg(aff);
1899 aff = isl_aff_add_constant_num_si(aff, -1);
1900 return isl_aff_nonneg_basic_set(aff);
1903 /* Return a basic set containing those elements in the space
1904 * of aff where it is zero.
1905 * If "rational" is set, then return a rational basic set.
1907 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1908 int rational)
1910 isl_constraint *ineq;
1911 isl_basic_set *bset;
1913 ineq = isl_equality_from_aff(aff);
1915 bset = isl_basic_set_from_constraint(ineq);
1916 if (rational)
1917 bset = isl_basic_set_set_rational(bset);
1918 bset = isl_basic_set_simplify(bset);
1919 return bset;
1922 /* Return a basic set containing those elements in the space
1923 * of aff where it is zero.
1925 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1927 return aff_zero_basic_set(aff, 0);
1930 /* Return a basic set containing those elements in the shared space
1931 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1933 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1934 __isl_take isl_aff *aff2)
1936 aff1 = isl_aff_sub(aff1, aff2);
1938 return isl_aff_nonneg_basic_set(aff1);
1941 /* Return a basic set containing those elements in the shared space
1942 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1944 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1945 __isl_take isl_aff *aff2)
1947 return isl_aff_ge_basic_set(aff2, aff1);
1950 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1951 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1953 aff1 = isl_aff_add(aff1, aff2);
1954 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1955 return aff1;
1958 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1960 if (!aff)
1961 return -1;
1963 return 0;
1966 /* Check whether the given affine expression has non-zero coefficient
1967 * for any dimension in the given range or if any of these dimensions
1968 * appear with non-zero coefficients in any of the integer divisions
1969 * involved in the affine expression.
1971 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1972 enum isl_dim_type type, unsigned first, unsigned n)
1974 int i;
1975 isl_ctx *ctx;
1976 int *active = NULL;
1977 int involves = 0;
1979 if (!aff)
1980 return -1;
1981 if (n == 0)
1982 return 0;
1984 ctx = isl_aff_get_ctx(aff);
1985 if (first + n > isl_aff_dim(aff, type))
1986 isl_die(ctx, isl_error_invalid,
1987 "range out of bounds", return -1);
1989 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1990 if (!active)
1991 goto error;
1993 first += isl_local_space_offset(aff->ls, type) - 1;
1994 for (i = 0; i < n; ++i)
1995 if (active[first + i]) {
1996 involves = 1;
1997 break;
2000 free(active);
2002 return involves;
2003 error:
2004 free(active);
2005 return -1;
2008 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2009 enum isl_dim_type type, unsigned first, unsigned n)
2011 isl_ctx *ctx;
2013 if (!aff)
2014 return NULL;
2015 if (type == isl_dim_out)
2016 isl_die(aff->v->ctx, isl_error_invalid,
2017 "cannot drop output/set dimension",
2018 return isl_aff_free(aff));
2019 if (type == isl_dim_in)
2020 type = isl_dim_set;
2021 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2022 return aff;
2024 ctx = isl_aff_get_ctx(aff);
2025 if (first + n > isl_local_space_dim(aff->ls, type))
2026 isl_die(ctx, isl_error_invalid, "range out of bounds",
2027 return isl_aff_free(aff));
2029 aff = isl_aff_cow(aff);
2030 if (!aff)
2031 return NULL;
2033 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2034 if (!aff->ls)
2035 return isl_aff_free(aff);
2037 first += 1 + isl_local_space_offset(aff->ls, type);
2038 aff->v = isl_vec_drop_els(aff->v, first, n);
2039 if (!aff->v)
2040 return isl_aff_free(aff);
2042 return aff;
2045 /* Project the domain of the affine expression onto its parameter space.
2046 * The affine expression may not involve any of the domain dimensions.
2048 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2050 isl_space *space;
2051 unsigned n;
2052 int involves;
2054 n = isl_aff_dim(aff, isl_dim_in);
2055 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2056 if (involves < 0)
2057 return isl_aff_free(aff);
2058 if (involves)
2059 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2060 "affine expression involves some of the domain dimensions",
2061 return isl_aff_free(aff));
2062 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2063 space = isl_aff_get_domain_space(aff);
2064 space = isl_space_params(space);
2065 aff = isl_aff_reset_domain_space(aff, space);
2066 return aff;
2069 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2070 enum isl_dim_type type, unsigned first, unsigned n)
2072 isl_ctx *ctx;
2074 if (!aff)
2075 return NULL;
2076 if (type == isl_dim_out)
2077 isl_die(aff->v->ctx, isl_error_invalid,
2078 "cannot insert output/set dimensions",
2079 return isl_aff_free(aff));
2080 if (type == isl_dim_in)
2081 type = isl_dim_set;
2082 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2083 return aff;
2085 ctx = isl_aff_get_ctx(aff);
2086 if (first > isl_local_space_dim(aff->ls, type))
2087 isl_die(ctx, isl_error_invalid, "position out of bounds",
2088 return isl_aff_free(aff));
2090 aff = isl_aff_cow(aff);
2091 if (!aff)
2092 return NULL;
2094 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2095 if (!aff->ls)
2096 return isl_aff_free(aff);
2098 first += 1 + isl_local_space_offset(aff->ls, type);
2099 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2100 if (!aff->v)
2101 return isl_aff_free(aff);
2103 return aff;
2106 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2107 enum isl_dim_type type, unsigned n)
2109 unsigned pos;
2111 pos = isl_aff_dim(aff, type);
2113 return isl_aff_insert_dims(aff, type, pos, n);
2116 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2117 enum isl_dim_type type, unsigned n)
2119 unsigned pos;
2121 pos = isl_pw_aff_dim(pwaff, type);
2123 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2126 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2127 * to dimensions of "dst_type" at "dst_pos".
2129 * We only support moving input dimensions to parameters and vice versa.
2131 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2132 enum isl_dim_type dst_type, unsigned dst_pos,
2133 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2135 unsigned g_dst_pos;
2136 unsigned g_src_pos;
2138 if (!aff)
2139 return NULL;
2140 if (n == 0 &&
2141 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2142 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2143 return aff;
2145 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2146 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2147 "cannot move output/set dimension", isl_aff_free(aff));
2148 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2149 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2150 "cannot move divs", isl_aff_free(aff));
2151 if (dst_type == isl_dim_in)
2152 dst_type = isl_dim_set;
2153 if (src_type == isl_dim_in)
2154 src_type = isl_dim_set;
2156 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2157 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2158 "range out of bounds", isl_aff_free(aff));
2159 if (dst_type == src_type)
2160 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2161 "moving dims within the same type not supported",
2162 isl_aff_free(aff));
2164 aff = isl_aff_cow(aff);
2165 if (!aff)
2166 return NULL;
2168 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2169 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2170 if (dst_type > src_type)
2171 g_dst_pos -= n;
2173 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2174 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2175 src_type, src_pos, n);
2176 if (!aff->v || !aff->ls)
2177 return isl_aff_free(aff);
2179 aff = sort_divs(aff);
2181 return aff;
2184 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2186 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2187 return isl_pw_aff_alloc(dom, aff);
2190 #undef PW
2191 #define PW isl_pw_aff
2192 #undef EL
2193 #define EL isl_aff
2194 #undef EL_IS_ZERO
2195 #define EL_IS_ZERO is_empty
2196 #undef ZERO
2197 #define ZERO empty
2198 #undef IS_ZERO
2199 #define IS_ZERO is_empty
2200 #undef FIELD
2201 #define FIELD aff
2202 #undef DEFAULT_IS_ZERO
2203 #define DEFAULT_IS_ZERO 0
2205 #define NO_EVAL
2206 #define NO_OPT
2207 #define NO_LIFT
2208 #define NO_MORPH
2210 #include <isl_pw_templ.c>
2212 static __isl_give isl_set *align_params_pw_pw_set_and(
2213 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2214 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2215 __isl_take isl_pw_aff *pwaff2))
2217 if (!pwaff1 || !pwaff2)
2218 goto error;
2219 if (isl_space_match(pwaff1->dim, isl_dim_param,
2220 pwaff2->dim, isl_dim_param))
2221 return fn(pwaff1, pwaff2);
2222 if (!isl_space_has_named_params(pwaff1->dim) ||
2223 !isl_space_has_named_params(pwaff2->dim))
2224 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2225 "unaligned unnamed parameters", goto error);
2226 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2227 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2228 return fn(pwaff1, pwaff2);
2229 error:
2230 isl_pw_aff_free(pwaff1);
2231 isl_pw_aff_free(pwaff2);
2232 return NULL;
2235 /* Compute a piecewise quasi-affine expression with a domain that
2236 * is the union of those of pwaff1 and pwaff2 and such that on each
2237 * cell, the quasi-affine expression is the better (according to cmp)
2238 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2239 * is defined on a given cell, then the associated expression
2240 * is the defined one.
2242 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2243 __isl_take isl_pw_aff *pwaff2,
2244 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2245 __isl_take isl_aff *aff2))
2247 int i, j, n;
2248 isl_pw_aff *res;
2249 isl_ctx *ctx;
2250 isl_set *set;
2252 if (!pwaff1 || !pwaff2)
2253 goto error;
2255 ctx = isl_space_get_ctx(pwaff1->dim);
2256 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2257 isl_die(ctx, isl_error_invalid,
2258 "arguments should live in same space", goto error);
2260 if (isl_pw_aff_is_empty(pwaff1)) {
2261 isl_pw_aff_free(pwaff1);
2262 return pwaff2;
2265 if (isl_pw_aff_is_empty(pwaff2)) {
2266 isl_pw_aff_free(pwaff2);
2267 return pwaff1;
2270 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2271 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2273 for (i = 0; i < pwaff1->n; ++i) {
2274 set = isl_set_copy(pwaff1->p[i].set);
2275 for (j = 0; j < pwaff2->n; ++j) {
2276 struct isl_set *common;
2277 isl_set *better;
2279 common = isl_set_intersect(
2280 isl_set_copy(pwaff1->p[i].set),
2281 isl_set_copy(pwaff2->p[j].set));
2282 better = isl_set_from_basic_set(cmp(
2283 isl_aff_copy(pwaff2->p[j].aff),
2284 isl_aff_copy(pwaff1->p[i].aff)));
2285 better = isl_set_intersect(common, better);
2286 if (isl_set_plain_is_empty(better)) {
2287 isl_set_free(better);
2288 continue;
2290 set = isl_set_subtract(set, isl_set_copy(better));
2292 res = isl_pw_aff_add_piece(res, better,
2293 isl_aff_copy(pwaff2->p[j].aff));
2295 res = isl_pw_aff_add_piece(res, set,
2296 isl_aff_copy(pwaff1->p[i].aff));
2299 for (j = 0; j < pwaff2->n; ++j) {
2300 set = isl_set_copy(pwaff2->p[j].set);
2301 for (i = 0; i < pwaff1->n; ++i)
2302 set = isl_set_subtract(set,
2303 isl_set_copy(pwaff1->p[i].set));
2304 res = isl_pw_aff_add_piece(res, set,
2305 isl_aff_copy(pwaff2->p[j].aff));
2308 isl_pw_aff_free(pwaff1);
2309 isl_pw_aff_free(pwaff2);
2311 return res;
2312 error:
2313 isl_pw_aff_free(pwaff1);
2314 isl_pw_aff_free(pwaff2);
2315 return NULL;
2318 /* Compute a piecewise quasi-affine expression with a domain that
2319 * is the union of those of pwaff1 and pwaff2 and such that on each
2320 * cell, the quasi-affine expression is the maximum of those of pwaff1
2321 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2322 * cell, then the associated expression is the defined one.
2324 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2325 __isl_take isl_pw_aff *pwaff2)
2327 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2330 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2331 __isl_take isl_pw_aff *pwaff2)
2333 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2334 &pw_aff_union_max);
2337 /* Compute a piecewise quasi-affine expression with a domain that
2338 * is the union of those of pwaff1 and pwaff2 and such that on each
2339 * cell, the quasi-affine expression is the minimum of those of pwaff1
2340 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2341 * cell, then the associated expression is the defined one.
2343 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2344 __isl_take isl_pw_aff *pwaff2)
2346 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2349 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2350 __isl_take isl_pw_aff *pwaff2)
2352 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2353 &pw_aff_union_min);
2356 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2357 __isl_take isl_pw_aff *pwaff2, int max)
2359 if (max)
2360 return isl_pw_aff_union_max(pwaff1, pwaff2);
2361 else
2362 return isl_pw_aff_union_min(pwaff1, pwaff2);
2365 /* Construct a map with as domain the domain of pwaff and
2366 * one-dimensional range corresponding to the affine expressions.
2368 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2370 int i;
2371 isl_space *dim;
2372 isl_map *map;
2374 if (!pwaff)
2375 return NULL;
2377 dim = isl_pw_aff_get_space(pwaff);
2378 map = isl_map_empty(dim);
2380 for (i = 0; i < pwaff->n; ++i) {
2381 isl_basic_map *bmap;
2382 isl_map *map_i;
2384 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2385 map_i = isl_map_from_basic_map(bmap);
2386 map_i = isl_map_intersect_domain(map_i,
2387 isl_set_copy(pwaff->p[i].set));
2388 map = isl_map_union_disjoint(map, map_i);
2391 isl_pw_aff_free(pwaff);
2393 return map;
2396 /* Construct a map with as domain the domain of pwaff and
2397 * one-dimensional range corresponding to the affine expressions.
2399 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2401 if (!pwaff)
2402 return NULL;
2403 if (isl_space_is_set(pwaff->dim))
2404 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2405 "space of input is not a map",
2406 return isl_pw_aff_free(pwaff));
2407 return map_from_pw_aff(pwaff);
2410 /* Construct a one-dimensional set with as parameter domain
2411 * the domain of pwaff and the single set dimension
2412 * corresponding to the affine expressions.
2414 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2416 if (!pwaff)
2417 return NULL;
2418 if (!isl_space_is_set(pwaff->dim))
2419 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2420 "space of input is not a set",
2421 return isl_pw_aff_free(pwaff));
2422 return map_from_pw_aff(pwaff);
2425 /* Return a set containing those elements in the domain
2426 * of pwaff where it is non-negative.
2428 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2430 int i;
2431 isl_set *set;
2433 if (!pwaff)
2434 return NULL;
2436 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2438 for (i = 0; i < pwaff->n; ++i) {
2439 isl_basic_set *bset;
2440 isl_set *set_i;
2441 int rational;
2443 rational = isl_set_has_rational(pwaff->p[i].set);
2444 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2445 rational);
2446 set_i = isl_set_from_basic_set(bset);
2447 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2448 set = isl_set_union_disjoint(set, set_i);
2451 isl_pw_aff_free(pwaff);
2453 return set;
2456 /* Return a set containing those elements in the domain
2457 * of pwaff where it is zero (if complement is 0) or not zero
2458 * (if complement is 1).
2460 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2461 int complement)
2463 int i;
2464 isl_set *set;
2466 if (!pwaff)
2467 return NULL;
2469 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2471 for (i = 0; i < pwaff->n; ++i) {
2472 isl_basic_set *bset;
2473 isl_set *set_i, *zero;
2474 int rational;
2476 rational = isl_set_has_rational(pwaff->p[i].set);
2477 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2478 rational);
2479 zero = isl_set_from_basic_set(bset);
2480 set_i = isl_set_copy(pwaff->p[i].set);
2481 if (complement)
2482 set_i = isl_set_subtract(set_i, zero);
2483 else
2484 set_i = isl_set_intersect(set_i, zero);
2485 set = isl_set_union_disjoint(set, set_i);
2488 isl_pw_aff_free(pwaff);
2490 return set;
2493 /* Return a set containing those elements in the domain
2494 * of pwaff where it is zero.
2496 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2498 return pw_aff_zero_set(pwaff, 0);
2501 /* Return a set containing those elements in the domain
2502 * of pwaff where it is not zero.
2504 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2506 return pw_aff_zero_set(pwaff, 1);
2509 /* Return a set containing those elements in the shared domain
2510 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2512 * We compute the difference on the shared domain and then construct
2513 * the set of values where this difference is non-negative.
2514 * If strict is set, we first subtract 1 from the difference.
2515 * If equal is set, we only return the elements where pwaff1 and pwaff2
2516 * are equal.
2518 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2519 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2521 isl_set *set1, *set2;
2523 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2524 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2525 set1 = isl_set_intersect(set1, set2);
2526 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2527 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2528 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2530 if (strict) {
2531 isl_space *dim = isl_set_get_space(set1);
2532 isl_aff *aff;
2533 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2534 aff = isl_aff_add_constant_si(aff, -1);
2535 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2536 } else
2537 isl_set_free(set1);
2539 if (equal)
2540 return isl_pw_aff_zero_set(pwaff1);
2541 return isl_pw_aff_nonneg_set(pwaff1);
2544 /* Return a set containing those elements in the shared domain
2545 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2547 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2548 __isl_take isl_pw_aff *pwaff2)
2550 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2553 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2554 __isl_take isl_pw_aff *pwaff2)
2556 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2559 /* Return a set containing those elements in the shared domain
2560 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2562 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2563 __isl_take isl_pw_aff *pwaff2)
2565 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2568 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2569 __isl_take isl_pw_aff *pwaff2)
2571 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2574 /* Return a set containing those elements in the shared domain
2575 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2577 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2578 __isl_take isl_pw_aff *pwaff2)
2580 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2583 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2584 __isl_take isl_pw_aff *pwaff2)
2586 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2589 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2590 __isl_take isl_pw_aff *pwaff2)
2592 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2595 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2596 __isl_take isl_pw_aff *pwaff2)
2598 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2601 /* Return a set containing those elements in the shared domain
2602 * of the elements of list1 and list2 where each element in list1
2603 * has the relation specified by "fn" with each element in list2.
2605 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2606 __isl_take isl_pw_aff_list *list2,
2607 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2608 __isl_take isl_pw_aff *pwaff2))
2610 int i, j;
2611 isl_ctx *ctx;
2612 isl_set *set;
2614 if (!list1 || !list2)
2615 goto error;
2617 ctx = isl_pw_aff_list_get_ctx(list1);
2618 if (list1->n < 1 || list2->n < 1)
2619 isl_die(ctx, isl_error_invalid,
2620 "list should contain at least one element", goto error);
2622 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2623 for (i = 0; i < list1->n; ++i)
2624 for (j = 0; j < list2->n; ++j) {
2625 isl_set *set_ij;
2627 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2628 isl_pw_aff_copy(list2->p[j]));
2629 set = isl_set_intersect(set, set_ij);
2632 isl_pw_aff_list_free(list1);
2633 isl_pw_aff_list_free(list2);
2634 return set;
2635 error:
2636 isl_pw_aff_list_free(list1);
2637 isl_pw_aff_list_free(list2);
2638 return NULL;
2641 /* Return a set containing those elements in the shared domain
2642 * of the elements of list1 and list2 where each element in list1
2643 * is equal to each element in list2.
2645 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2646 __isl_take isl_pw_aff_list *list2)
2648 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2651 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2652 __isl_take isl_pw_aff_list *list2)
2654 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2657 /* Return a set containing those elements in the shared domain
2658 * of the elements of list1 and list2 where each element in list1
2659 * is less than or equal to each element in list2.
2661 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2662 __isl_take isl_pw_aff_list *list2)
2664 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2667 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2668 __isl_take isl_pw_aff_list *list2)
2670 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2673 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2674 __isl_take isl_pw_aff_list *list2)
2676 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2679 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2680 __isl_take isl_pw_aff_list *list2)
2682 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2686 /* Return a set containing those elements in the shared domain
2687 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2689 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2690 __isl_take isl_pw_aff *pwaff2)
2692 isl_set *set_lt, *set_gt;
2694 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2695 isl_pw_aff_copy(pwaff2));
2696 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2697 return isl_set_union_disjoint(set_lt, set_gt);
2700 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2701 __isl_take isl_pw_aff *pwaff2)
2703 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2706 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2707 isl_int v)
2709 int i;
2711 if (isl_int_is_one(v))
2712 return pwaff;
2713 if (!isl_int_is_pos(v))
2714 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2715 "factor needs to be positive",
2716 return isl_pw_aff_free(pwaff));
2717 pwaff = isl_pw_aff_cow(pwaff);
2718 if (!pwaff)
2719 return NULL;
2720 if (pwaff->n == 0)
2721 return pwaff;
2723 for (i = 0; i < pwaff->n; ++i) {
2724 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2725 if (!pwaff->p[i].aff)
2726 return isl_pw_aff_free(pwaff);
2729 return pwaff;
2732 /* Divide "pa" by "f".
2734 __isl_give isl_pw_aff *isl_pw_aff_scale_down_val(__isl_take isl_pw_aff *pa,
2735 __isl_take isl_val *f)
2737 int i;
2739 if (!pa || !f)
2740 goto error;
2742 if (isl_val_is_one(f)) {
2743 isl_val_free(f);
2744 return pa;
2747 if (!isl_val_is_rat(f))
2748 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2749 "expecting rational factor", goto error);
2750 if (!isl_val_is_pos(f))
2751 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2752 "factor needs to be positive", goto error);
2754 pa = isl_pw_aff_cow(pa);
2755 if (!pa)
2756 return NULL;
2757 if (pa->n == 0)
2758 return pa;
2760 for (i = 0; i < pa->n; ++i) {
2761 pa->p[i].aff = isl_aff_scale_down_val(pa->p[i].aff,
2762 isl_val_copy(f));
2763 if (!pa->p[i].aff)
2764 goto error;
2767 isl_val_free(f);
2768 return pa;
2769 error:
2770 isl_pw_aff_free(pa);
2771 isl_val_free(f);
2772 return NULL;
2775 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2777 int i;
2779 pwaff = isl_pw_aff_cow(pwaff);
2780 if (!pwaff)
2781 return NULL;
2782 if (pwaff->n == 0)
2783 return pwaff;
2785 for (i = 0; i < pwaff->n; ++i) {
2786 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2787 if (!pwaff->p[i].aff)
2788 return isl_pw_aff_free(pwaff);
2791 return pwaff;
2794 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2796 int i;
2798 pwaff = isl_pw_aff_cow(pwaff);
2799 if (!pwaff)
2800 return NULL;
2801 if (pwaff->n == 0)
2802 return pwaff;
2804 for (i = 0; i < pwaff->n; ++i) {
2805 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2806 if (!pwaff->p[i].aff)
2807 return isl_pw_aff_free(pwaff);
2810 return pwaff;
2813 /* Assuming that "cond1" and "cond2" are disjoint,
2814 * return an affine expression that is equal to pwaff1 on cond1
2815 * and to pwaff2 on cond2.
2817 static __isl_give isl_pw_aff *isl_pw_aff_select(
2818 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2819 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2821 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2822 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2824 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2827 /* Return an affine expression that is equal to pwaff_true for elements
2828 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2829 * is zero.
2830 * That is, return cond ? pwaff_true : pwaff_false;
2832 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2833 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2835 isl_set *cond_true, *cond_false;
2837 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2838 cond_false = isl_pw_aff_zero_set(cond);
2839 return isl_pw_aff_select(cond_true, pwaff_true,
2840 cond_false, pwaff_false);
2843 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2845 if (!aff)
2846 return -1;
2848 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2851 /* Check whether pwaff is a piecewise constant.
2853 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2855 int i;
2857 if (!pwaff)
2858 return -1;
2860 for (i = 0; i < pwaff->n; ++i) {
2861 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2862 if (is_cst < 0 || !is_cst)
2863 return is_cst;
2866 return 1;
2869 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2870 __isl_take isl_aff *aff2)
2872 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2873 return isl_aff_mul(aff2, aff1);
2875 if (!isl_aff_is_cst(aff2))
2876 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2877 "at least one affine expression should be constant",
2878 goto error);
2880 aff1 = isl_aff_cow(aff1);
2881 if (!aff1 || !aff2)
2882 goto error;
2884 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2885 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2887 isl_aff_free(aff2);
2888 return aff1;
2889 error:
2890 isl_aff_free(aff1);
2891 isl_aff_free(aff2);
2892 return NULL;
2895 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2897 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2898 __isl_take isl_aff *aff2)
2900 int is_cst;
2901 int neg;
2903 is_cst = isl_aff_is_cst(aff2);
2904 if (is_cst < 0)
2905 goto error;
2906 if (!is_cst)
2907 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2908 "second argument should be a constant", goto error);
2910 if (!aff2)
2911 goto error;
2913 neg = isl_int_is_neg(aff2->v->el[1]);
2914 if (neg) {
2915 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2916 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2919 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2920 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2922 if (neg) {
2923 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2924 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2927 isl_aff_free(aff2);
2928 return aff1;
2929 error:
2930 isl_aff_free(aff1);
2931 isl_aff_free(aff2);
2932 return NULL;
2935 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2936 __isl_take isl_pw_aff *pwaff2)
2938 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2941 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2942 __isl_take isl_pw_aff *pwaff2)
2944 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2947 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2948 __isl_take isl_pw_aff *pwaff2)
2950 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2953 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2954 __isl_take isl_pw_aff *pwaff2)
2956 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2959 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2960 __isl_take isl_pw_aff *pwaff2)
2962 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2965 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2966 __isl_take isl_pw_aff *pa2)
2968 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2971 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2973 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2974 __isl_take isl_pw_aff *pa2)
2976 int is_cst;
2978 is_cst = isl_pw_aff_is_cst(pa2);
2979 if (is_cst < 0)
2980 goto error;
2981 if (!is_cst)
2982 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2983 "second argument should be a piecewise constant",
2984 goto error);
2985 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2986 error:
2987 isl_pw_aff_free(pa1);
2988 isl_pw_aff_free(pa2);
2989 return NULL;
2992 /* Compute the quotient of the integer division of "pa1" by "pa2"
2993 * with rounding towards zero.
2994 * "pa2" is assumed to be a piecewise constant.
2996 * In particular, return
2998 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3001 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3002 __isl_take isl_pw_aff *pa2)
3004 int is_cst;
3005 isl_set *cond;
3006 isl_pw_aff *f, *c;
3008 is_cst = isl_pw_aff_is_cst(pa2);
3009 if (is_cst < 0)
3010 goto error;
3011 if (!is_cst)
3012 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3013 "second argument should be a piecewise constant",
3014 goto error);
3016 pa1 = isl_pw_aff_div(pa1, pa2);
3018 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3019 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3020 c = isl_pw_aff_ceil(pa1);
3021 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3022 error:
3023 isl_pw_aff_free(pa1);
3024 isl_pw_aff_free(pa2);
3025 return NULL;
3028 /* Compute the remainder of the integer division of "pa1" by "pa2"
3029 * with rounding towards zero.
3030 * "pa2" is assumed to be a piecewise constant.
3032 * In particular, return
3034 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3037 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3038 __isl_take isl_pw_aff *pa2)
3040 int is_cst;
3041 isl_pw_aff *res;
3043 is_cst = isl_pw_aff_is_cst(pa2);
3044 if (is_cst < 0)
3045 goto error;
3046 if (!is_cst)
3047 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3048 "second argument should be a piecewise constant",
3049 goto error);
3050 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3051 res = isl_pw_aff_mul(pa2, res);
3052 res = isl_pw_aff_sub(pa1, res);
3053 return res;
3054 error:
3055 isl_pw_aff_free(pa1);
3056 isl_pw_aff_free(pa2);
3057 return NULL;
3060 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3061 __isl_take isl_pw_aff *pwaff2)
3063 isl_set *le;
3064 isl_set *dom;
3066 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3067 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3068 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3069 isl_pw_aff_copy(pwaff2));
3070 dom = isl_set_subtract(dom, isl_set_copy(le));
3071 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3074 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3075 __isl_take isl_pw_aff *pwaff2)
3077 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3080 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3081 __isl_take isl_pw_aff *pwaff2)
3083 isl_set *ge;
3084 isl_set *dom;
3086 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3087 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3088 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3089 isl_pw_aff_copy(pwaff2));
3090 dom = isl_set_subtract(dom, isl_set_copy(ge));
3091 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3094 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3095 __isl_take isl_pw_aff *pwaff2)
3097 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3100 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3101 __isl_take isl_pw_aff_list *list,
3102 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3103 __isl_take isl_pw_aff *pwaff2))
3105 int i;
3106 isl_ctx *ctx;
3107 isl_pw_aff *res;
3109 if (!list)
3110 return NULL;
3112 ctx = isl_pw_aff_list_get_ctx(list);
3113 if (list->n < 1)
3114 isl_die(ctx, isl_error_invalid,
3115 "list should contain at least one element",
3116 return isl_pw_aff_list_free(list));
3118 res = isl_pw_aff_copy(list->p[0]);
3119 for (i = 1; i < list->n; ++i)
3120 res = fn(res, isl_pw_aff_copy(list->p[i]));
3122 isl_pw_aff_list_free(list);
3123 return res;
3126 /* Return an isl_pw_aff that maps each element in the intersection of the
3127 * domains of the elements of list to the minimal corresponding affine
3128 * expression.
3130 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3132 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3135 /* Return an isl_pw_aff that maps each element in the intersection of the
3136 * domains of the elements of list to the maximal corresponding affine
3137 * expression.
3139 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3141 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3144 /* Mark the domains of "pwaff" as rational.
3146 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3148 int i;
3150 pwaff = isl_pw_aff_cow(pwaff);
3151 if (!pwaff)
3152 return NULL;
3153 if (pwaff->n == 0)
3154 return pwaff;
3156 for (i = 0; i < pwaff->n; ++i) {
3157 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3158 if (!pwaff->p[i].set)
3159 return isl_pw_aff_free(pwaff);
3162 return pwaff;
3165 /* Mark the domains of the elements of "list" as rational.
3167 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3168 __isl_take isl_pw_aff_list *list)
3170 int i, n;
3172 if (!list)
3173 return NULL;
3174 if (list->n == 0)
3175 return list;
3177 n = list->n;
3178 for (i = 0; i < n; ++i) {
3179 isl_pw_aff *pa;
3181 pa = isl_pw_aff_list_get_pw_aff(list, i);
3182 pa = isl_pw_aff_set_rational(pa);
3183 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3186 return list;
3189 /* Do the parameters of "aff" match those of "space"?
3191 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3192 __isl_keep isl_space *space)
3194 isl_space *aff_space;
3195 int match;
3197 if (!aff || !space)
3198 return -1;
3200 aff_space = isl_aff_get_domain_space(aff);
3202 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3204 isl_space_free(aff_space);
3205 return match;
3208 /* Check that the domain space of "aff" matches "space".
3210 * Return 0 on success and -1 on error.
3212 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3213 __isl_keep isl_space *space)
3215 isl_space *aff_space;
3216 int match;
3218 if (!aff || !space)
3219 return -1;
3221 aff_space = isl_aff_get_domain_space(aff);
3223 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3224 if (match < 0)
3225 goto error;
3226 if (!match)
3227 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3228 "parameters don't match", goto error);
3229 match = isl_space_tuple_match(space, isl_dim_in,
3230 aff_space, isl_dim_set);
3231 if (match < 0)
3232 goto error;
3233 if (!match)
3234 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3235 "domains don't match", goto error);
3236 isl_space_free(aff_space);
3237 return 0;
3238 error:
3239 isl_space_free(aff_space);
3240 return -1;
3243 #undef BASE
3244 #define BASE aff
3245 #define NO_INTERSECT_DOMAIN
3246 #define NO_DOMAIN
3248 #include <isl_multi_templ.c>
3250 #undef NO_DOMAIN
3251 #undef NO_INTERSECT_DOMAIN
3253 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3254 * of the space to its domain.
3256 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3258 int i, n_in;
3259 isl_local_space *ls;
3260 isl_multi_aff *ma;
3262 if (!space)
3263 return NULL;
3264 if (!isl_space_is_map(space))
3265 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3266 "not a map space", goto error);
3268 n_in = isl_space_dim(space, isl_dim_in);
3269 space = isl_space_domain_map(space);
3271 ma = isl_multi_aff_alloc(isl_space_copy(space));
3272 if (n_in == 0) {
3273 isl_space_free(space);
3274 return ma;
3277 space = isl_space_domain(space);
3278 ls = isl_local_space_from_space(space);
3279 for (i = 0; i < n_in; ++i) {
3280 isl_aff *aff;
3282 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3283 isl_dim_set, i);
3284 ma = isl_multi_aff_set_aff(ma, i, aff);
3286 isl_local_space_free(ls);
3287 return ma;
3288 error:
3289 isl_space_free(space);
3290 return NULL;
3293 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3294 * of the space to its range.
3296 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3298 int i, n_in, n_out;
3299 isl_local_space *ls;
3300 isl_multi_aff *ma;
3302 if (!space)
3303 return NULL;
3304 if (!isl_space_is_map(space))
3305 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3306 "not a map space", goto error);
3308 n_in = isl_space_dim(space, isl_dim_in);
3309 n_out = isl_space_dim(space, isl_dim_out);
3310 space = isl_space_range_map(space);
3312 ma = isl_multi_aff_alloc(isl_space_copy(space));
3313 if (n_out == 0) {
3314 isl_space_free(space);
3315 return ma;
3318 space = isl_space_domain(space);
3319 ls = isl_local_space_from_space(space);
3320 for (i = 0; i < n_out; ++i) {
3321 isl_aff *aff;
3323 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3324 isl_dim_set, n_in + i);
3325 ma = isl_multi_aff_set_aff(ma, i, aff);
3327 isl_local_space_free(ls);
3328 return ma;
3329 error:
3330 isl_space_free(space);
3331 return NULL;
3334 /* Given the space of a set and a range of set dimensions,
3335 * construct an isl_multi_aff that projects out those dimensions.
3337 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3338 __isl_take isl_space *space, enum isl_dim_type type,
3339 unsigned first, unsigned n)
3341 int i, dim;
3342 isl_local_space *ls;
3343 isl_multi_aff *ma;
3345 if (!space)
3346 return NULL;
3347 if (!isl_space_is_set(space))
3348 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3349 "expecting set space", goto error);
3350 if (type != isl_dim_set)
3351 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3352 "only set dimensions can be projected out", goto error);
3354 dim = isl_space_dim(space, isl_dim_set);
3355 if (first + n > dim)
3356 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3357 "range out of bounds", goto error);
3359 space = isl_space_from_domain(space);
3360 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3362 if (dim == n)
3363 return isl_multi_aff_alloc(space);
3365 ma = isl_multi_aff_alloc(isl_space_copy(space));
3366 space = isl_space_domain(space);
3367 ls = isl_local_space_from_space(space);
3369 for (i = 0; i < first; ++i) {
3370 isl_aff *aff;
3372 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3373 isl_dim_set, i);
3374 ma = isl_multi_aff_set_aff(ma, i, aff);
3377 for (i = 0; i < dim - (first + n); ++i) {
3378 isl_aff *aff;
3380 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3381 isl_dim_set, first + n + i);
3382 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3385 isl_local_space_free(ls);
3386 return ma;
3387 error:
3388 isl_space_free(space);
3389 return NULL;
3392 /* Given the space of a set and a range of set dimensions,
3393 * construct an isl_pw_multi_aff that projects out those dimensions.
3395 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3396 __isl_take isl_space *space, enum isl_dim_type type,
3397 unsigned first, unsigned n)
3399 isl_multi_aff *ma;
3401 ma = isl_multi_aff_project_out_map(space, type, first, n);
3402 return isl_pw_multi_aff_from_multi_aff(ma);
3405 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3406 * domain.
3408 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3409 __isl_take isl_multi_aff *ma)
3411 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3412 return isl_pw_multi_aff_alloc(dom, ma);
3415 /* Create a piecewise multi-affine expression in the given space that maps each
3416 * input dimension to the corresponding output dimension.
3418 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3419 __isl_take isl_space *space)
3421 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3424 /* Add "ma2" to "ma1" and return the result.
3426 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3428 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3429 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3431 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3434 /* Add "ma2" to "ma1" and return the result.
3436 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3437 __isl_take isl_multi_aff *ma2)
3439 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3440 &isl_multi_aff_add_aligned);
3443 /* Subtract "ma2" from "ma1" and return the result.
3445 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3447 static __isl_give isl_multi_aff *isl_multi_aff_sub_aligned(
3448 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3450 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3453 /* Subtract "ma2" from "ma1" and return the result.
3455 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3456 __isl_take isl_multi_aff *ma2)
3458 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3459 &isl_multi_aff_sub_aligned);
3462 /* Exploit the equalities in "eq" to simplify the affine expressions.
3464 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3465 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3467 int i;
3469 maff = isl_multi_aff_cow(maff);
3470 if (!maff || !eq)
3471 goto error;
3473 for (i = 0; i < maff->n; ++i) {
3474 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3475 isl_basic_set_copy(eq));
3476 if (!maff->p[i])
3477 goto error;
3480 isl_basic_set_free(eq);
3481 return maff;
3482 error:
3483 isl_basic_set_free(eq);
3484 isl_multi_aff_free(maff);
3485 return NULL;
3488 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3489 isl_int f)
3491 int i;
3493 maff = isl_multi_aff_cow(maff);
3494 if (!maff)
3495 return NULL;
3497 for (i = 0; i < maff->n; ++i) {
3498 maff->p[i] = isl_aff_scale(maff->p[i], f);
3499 if (!maff->p[i])
3500 return isl_multi_aff_free(maff);
3503 return maff;
3506 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3507 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3509 maff1 = isl_multi_aff_add(maff1, maff2);
3510 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3511 return maff1;
3514 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3516 if (!maff)
3517 return -1;
3519 return 0;
3522 /* Return the set of domain elements where "ma1" is lexicographically
3523 * smaller than or equal to "ma2".
3525 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3526 __isl_take isl_multi_aff *ma2)
3528 return isl_multi_aff_lex_ge_set(ma2, ma1);
3531 /* Return the set of domain elements where "ma1" is lexicographically
3532 * greater than or equal to "ma2".
3534 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3535 __isl_take isl_multi_aff *ma2)
3537 isl_space *space;
3538 isl_map *map1, *map2;
3539 isl_map *map, *ge;
3541 map1 = isl_map_from_multi_aff(ma1);
3542 map2 = isl_map_from_multi_aff(ma2);
3543 map = isl_map_range_product(map1, map2);
3544 space = isl_space_range(isl_map_get_space(map));
3545 space = isl_space_domain(isl_space_unwrap(space));
3546 ge = isl_map_lex_ge(space);
3547 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3549 return isl_map_domain(map);
3552 #undef PW
3553 #define PW isl_pw_multi_aff
3554 #undef EL
3555 #define EL isl_multi_aff
3556 #undef EL_IS_ZERO
3557 #define EL_IS_ZERO is_empty
3558 #undef ZERO
3559 #define ZERO empty
3560 #undef IS_ZERO
3561 #define IS_ZERO is_empty
3562 #undef FIELD
3563 #define FIELD maff
3564 #undef DEFAULT_IS_ZERO
3565 #define DEFAULT_IS_ZERO 0
3567 #define NO_NEG
3568 #define NO_EVAL
3569 #define NO_OPT
3570 #define NO_INVOLVES_DIMS
3571 #define NO_INSERT_DIMS
3572 #define NO_LIFT
3573 #define NO_MORPH
3575 #include <isl_pw_templ.c>
3577 #undef UNION
3578 #define UNION isl_union_pw_multi_aff
3579 #undef PART
3580 #define PART isl_pw_multi_aff
3581 #undef PARTS
3582 #define PARTS pw_multi_aff
3583 #define ALIGN_DOMAIN
3585 #define NO_EVAL
3587 #include <isl_union_templ.c>
3589 /* Given a function "cmp" that returns the set of elements where
3590 * "ma1" is "better" than "ma2", return the intersection of this
3591 * set with "dom1" and "dom2".
3593 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3594 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3595 __isl_keep isl_multi_aff *ma2,
3596 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3597 __isl_take isl_multi_aff *ma2))
3599 isl_set *common;
3600 isl_set *better;
3601 int is_empty;
3603 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3604 is_empty = isl_set_plain_is_empty(common);
3605 if (is_empty >= 0 && is_empty)
3606 return common;
3607 if (is_empty < 0)
3608 return isl_set_free(common);
3609 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3610 better = isl_set_intersect(common, better);
3612 return better;
3615 /* Given a function "cmp" that returns the set of elements where
3616 * "ma1" is "better" than "ma2", return a piecewise multi affine
3617 * expression defined on the union of the definition domains
3618 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3619 * "pma2" on each cell. If only one of the two input functions
3620 * is defined on a given cell, then it is considered the best.
3622 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3623 __isl_take isl_pw_multi_aff *pma1,
3624 __isl_take isl_pw_multi_aff *pma2,
3625 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3626 __isl_take isl_multi_aff *ma2))
3628 int i, j, n;
3629 isl_pw_multi_aff *res = NULL;
3630 isl_ctx *ctx;
3631 isl_set *set = NULL;
3633 if (!pma1 || !pma2)
3634 goto error;
3636 ctx = isl_space_get_ctx(pma1->dim);
3637 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3638 isl_die(ctx, isl_error_invalid,
3639 "arguments should live in the same space", goto error);
3641 if (isl_pw_multi_aff_is_empty(pma1)) {
3642 isl_pw_multi_aff_free(pma1);
3643 return pma2;
3646 if (isl_pw_multi_aff_is_empty(pma2)) {
3647 isl_pw_multi_aff_free(pma2);
3648 return pma1;
3651 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3652 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3654 for (i = 0; i < pma1->n; ++i) {
3655 set = isl_set_copy(pma1->p[i].set);
3656 for (j = 0; j < pma2->n; ++j) {
3657 isl_set *better;
3658 int is_empty;
3660 better = shared_and_better(pma2->p[j].set,
3661 pma1->p[i].set, pma2->p[j].maff,
3662 pma1->p[i].maff, cmp);
3663 is_empty = isl_set_plain_is_empty(better);
3664 if (is_empty < 0 || is_empty) {
3665 isl_set_free(better);
3666 if (is_empty < 0)
3667 goto error;
3668 continue;
3670 set = isl_set_subtract(set, isl_set_copy(better));
3672 res = isl_pw_multi_aff_add_piece(res, better,
3673 isl_multi_aff_copy(pma2->p[j].maff));
3675 res = isl_pw_multi_aff_add_piece(res, set,
3676 isl_multi_aff_copy(pma1->p[i].maff));
3679 for (j = 0; j < pma2->n; ++j) {
3680 set = isl_set_copy(pma2->p[j].set);
3681 for (i = 0; i < pma1->n; ++i)
3682 set = isl_set_subtract(set,
3683 isl_set_copy(pma1->p[i].set));
3684 res = isl_pw_multi_aff_add_piece(res, set,
3685 isl_multi_aff_copy(pma2->p[j].maff));
3688 isl_pw_multi_aff_free(pma1);
3689 isl_pw_multi_aff_free(pma2);
3691 return res;
3692 error:
3693 isl_pw_multi_aff_free(pma1);
3694 isl_pw_multi_aff_free(pma2);
3695 isl_set_free(set);
3696 return isl_pw_multi_aff_free(res);
3699 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3700 __isl_take isl_pw_multi_aff *pma1,
3701 __isl_take isl_pw_multi_aff *pma2)
3703 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3706 /* Given two piecewise multi affine expressions, return a piecewise
3707 * multi-affine expression defined on the union of the definition domains
3708 * of the inputs that is equal to the lexicographic maximum of the two
3709 * inputs on each cell. If only one of the two inputs is defined on
3710 * a given cell, then it is considered to be the maximum.
3712 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3713 __isl_take isl_pw_multi_aff *pma1,
3714 __isl_take isl_pw_multi_aff *pma2)
3716 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3717 &pw_multi_aff_union_lexmax);
3720 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3721 __isl_take isl_pw_multi_aff *pma1,
3722 __isl_take isl_pw_multi_aff *pma2)
3724 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3727 /* Given two piecewise multi affine expressions, return a piecewise
3728 * multi-affine expression defined on the union of the definition domains
3729 * of the inputs that is equal to the lexicographic minimum of the two
3730 * inputs on each cell. If only one of the two inputs is defined on
3731 * a given cell, then it is considered to be the minimum.
3733 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3734 __isl_take isl_pw_multi_aff *pma1,
3735 __isl_take isl_pw_multi_aff *pma2)
3737 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3738 &pw_multi_aff_union_lexmin);
3741 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3742 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3744 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3745 &isl_multi_aff_add);
3748 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3749 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3751 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3752 &pw_multi_aff_add);
3755 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3756 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3758 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3759 &isl_multi_aff_sub);
3762 /* Subtract "pma2" from "pma1" and return the result.
3764 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3765 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3767 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3768 &pw_multi_aff_sub);
3771 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3772 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3774 return isl_pw_multi_aff_union_add_(pma1, pma2);
3777 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3778 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3780 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3781 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3783 int i, j, n;
3784 isl_space *space;
3785 isl_pw_multi_aff *res;
3787 if (!pma1 || !pma2)
3788 goto error;
3790 n = pma1->n * pma2->n;
3791 space = isl_space_product(isl_space_copy(pma1->dim),
3792 isl_space_copy(pma2->dim));
3793 res = isl_pw_multi_aff_alloc_size(space, n);
3795 for (i = 0; i < pma1->n; ++i) {
3796 for (j = 0; j < pma2->n; ++j) {
3797 isl_set *domain;
3798 isl_multi_aff *ma;
3800 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3801 isl_set_copy(pma2->p[j].set));
3802 ma = isl_multi_aff_product(
3803 isl_multi_aff_copy(pma1->p[i].maff),
3804 isl_multi_aff_copy(pma2->p[j].maff));
3805 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3809 isl_pw_multi_aff_free(pma1);
3810 isl_pw_multi_aff_free(pma2);
3811 return res;
3812 error:
3813 isl_pw_multi_aff_free(pma1);
3814 isl_pw_multi_aff_free(pma2);
3815 return NULL;
3818 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3819 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3821 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3822 &pw_multi_aff_product);
3825 /* Construct a map mapping the domain of the piecewise multi-affine expression
3826 * to its range, with each dimension in the range equated to the
3827 * corresponding affine expression on its cell.
3829 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3831 int i;
3832 isl_map *map;
3834 if (!pma)
3835 return NULL;
3837 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3839 for (i = 0; i < pma->n; ++i) {
3840 isl_multi_aff *maff;
3841 isl_basic_map *bmap;
3842 isl_map *map_i;
3844 maff = isl_multi_aff_copy(pma->p[i].maff);
3845 bmap = isl_basic_map_from_multi_aff(maff);
3846 map_i = isl_map_from_basic_map(bmap);
3847 map_i = isl_map_intersect_domain(map_i,
3848 isl_set_copy(pma->p[i].set));
3849 map = isl_map_union_disjoint(map, map_i);
3852 isl_pw_multi_aff_free(pma);
3853 return map;
3856 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3858 if (!pma)
3859 return NULL;
3861 if (!isl_space_is_set(pma->dim))
3862 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3863 "isl_pw_multi_aff cannot be converted into an isl_set",
3864 return isl_pw_multi_aff_free(pma));
3866 return isl_map_from_pw_multi_aff(pma);
3869 /* Given a basic map with a single output dimension that is defined
3870 * in terms of the parameters and input dimensions using an equality,
3871 * extract an isl_aff that expresses the output dimension in terms
3872 * of the parameters and input dimensions.
3874 * Since some applications expect the result of isl_pw_multi_aff_from_map
3875 * to only contain integer affine expressions, we compute the floor
3876 * of the expression before returning.
3878 * This function shares some similarities with
3879 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3881 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3882 __isl_take isl_basic_map *bmap)
3884 int i;
3885 unsigned offset;
3886 unsigned total;
3887 isl_local_space *ls;
3888 isl_aff *aff;
3890 if (!bmap)
3891 return NULL;
3892 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3893 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3894 "basic map should have a single output dimension",
3895 goto error);
3896 offset = isl_basic_map_offset(bmap, isl_dim_out);
3897 total = isl_basic_map_total_dim(bmap);
3898 for (i = 0; i < bmap->n_eq; ++i) {
3899 if (isl_int_is_zero(bmap->eq[i][offset]))
3900 continue;
3901 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3902 1 + total - (offset + 1)) != -1)
3903 continue;
3904 break;
3906 if (i >= bmap->n_eq)
3907 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3908 "unable to find suitable equality", goto error);
3909 ls = isl_basic_map_get_local_space(bmap);
3910 aff = isl_aff_alloc(isl_local_space_domain(ls));
3911 if (!aff)
3912 goto error;
3913 if (isl_int_is_neg(bmap->eq[i][offset]))
3914 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3915 else
3916 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3917 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3918 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3919 isl_basic_map_free(bmap);
3921 aff = isl_aff_remove_unused_divs(aff);
3922 aff = isl_aff_floor(aff);
3923 return aff;
3924 error:
3925 isl_basic_map_free(bmap);
3926 return NULL;
3929 /* Given a basic map where each output dimension is defined
3930 * in terms of the parameters and input dimensions using an equality,
3931 * extract an isl_multi_aff that expresses the output dimensions in terms
3932 * of the parameters and input dimensions.
3934 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3935 __isl_take isl_basic_map *bmap)
3937 int i;
3938 unsigned n_out;
3939 isl_multi_aff *ma;
3941 if (!bmap)
3942 return NULL;
3944 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3945 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3947 for (i = 0; i < n_out; ++i) {
3948 isl_basic_map *bmap_i;
3949 isl_aff *aff;
3951 bmap_i = isl_basic_map_copy(bmap);
3952 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3953 i + 1, n_out - (1 + i));
3954 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3955 aff = extract_isl_aff_from_basic_map(bmap_i);
3956 ma = isl_multi_aff_set_aff(ma, i, aff);
3959 isl_basic_map_free(bmap);
3961 return ma;
3964 /* Create an isl_pw_multi_aff that is equivalent to
3965 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3966 * The given basic map is such that each output dimension is defined
3967 * in terms of the parameters and input dimensions using an equality.
3969 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3970 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3972 isl_multi_aff *ma;
3974 ma = extract_isl_multi_aff_from_basic_map(bmap);
3975 return isl_pw_multi_aff_alloc(domain, ma);
3978 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3979 * This obviously only works if the input "map" is single-valued.
3980 * If so, we compute the lexicographic minimum of the image in the form
3981 * of an isl_pw_multi_aff. Since the image is unique, it is equal
3982 * to its lexicographic minimum.
3983 * If the input is not single-valued, we produce an error.
3985 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3986 __isl_take isl_map *map)
3988 int i;
3989 int sv;
3990 isl_pw_multi_aff *pma;
3992 sv = isl_map_is_single_valued(map);
3993 if (sv < 0)
3994 goto error;
3995 if (!sv)
3996 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3997 "map is not single-valued", goto error);
3998 map = isl_map_make_disjoint(map);
3999 if (!map)
4000 return NULL;
4002 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4004 for (i = 0; i < map->n; ++i) {
4005 isl_pw_multi_aff *pma_i;
4006 isl_basic_map *bmap;
4007 bmap = isl_basic_map_copy(map->p[i]);
4008 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4009 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4012 isl_map_free(map);
4013 return pma;
4014 error:
4015 isl_map_free(map);
4016 return NULL;
4019 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4020 * taking into account that the output dimension at position "d"
4021 * can be represented as
4023 * x = floor((e(...) + c1) / m)
4025 * given that constraint "i" is of the form
4027 * e(...) + c1 - m x >= 0
4030 * Let "map" be of the form
4032 * A -> B
4034 * We construct a mapping
4036 * A -> [A -> x = floor(...)]
4038 * apply that to the map, obtaining
4040 * [A -> x = floor(...)] -> B
4042 * and equate dimension "d" to x.
4043 * We then compute a isl_pw_multi_aff representation of the resulting map
4044 * and plug in the mapping above.
4046 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4047 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4049 isl_ctx *ctx;
4050 isl_space *space;
4051 isl_local_space *ls;
4052 isl_multi_aff *ma;
4053 isl_aff *aff;
4054 isl_vec *v;
4055 isl_map *insert;
4056 int offset;
4057 int n;
4058 int n_in;
4059 isl_pw_multi_aff *pma;
4060 int is_set;
4062 is_set = isl_map_is_set(map);
4064 offset = isl_basic_map_offset(hull, isl_dim_out);
4065 ctx = isl_map_get_ctx(map);
4066 space = isl_space_domain(isl_map_get_space(map));
4067 n_in = isl_space_dim(space, isl_dim_set);
4068 n = isl_space_dim(space, isl_dim_all);
4070 v = isl_vec_alloc(ctx, 1 + 1 + n);
4071 if (v) {
4072 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4073 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4075 isl_basic_map_free(hull);
4077 ls = isl_local_space_from_space(isl_space_copy(space));
4078 aff = isl_aff_alloc_vec(ls, v);
4079 aff = isl_aff_floor(aff);
4080 if (is_set) {
4081 isl_space_free(space);
4082 ma = isl_multi_aff_from_aff(aff);
4083 } else {
4084 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4085 ma = isl_multi_aff_range_product(ma,
4086 isl_multi_aff_from_aff(aff));
4089 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4090 map = isl_map_apply_domain(map, insert);
4091 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4092 pma = isl_pw_multi_aff_from_map(map);
4093 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4095 return pma;
4098 /* Is constraint "c" of the form
4100 * e(...) + c1 - m x >= 0
4102 * or
4104 * -e(...) + c2 + m x >= 0
4106 * where m > 1 and e only depends on parameters and input dimemnsions?
4108 * "offset" is the offset of the output dimensions
4109 * "pos" is the position of output dimension x.
4111 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4113 if (isl_int_is_zero(c[offset + d]))
4114 return 0;
4115 if (isl_int_is_one(c[offset + d]))
4116 return 0;
4117 if (isl_int_is_negone(c[offset + d]))
4118 return 0;
4119 if (isl_seq_first_non_zero(c + offset, d) != -1)
4120 return 0;
4121 if (isl_seq_first_non_zero(c + offset + d + 1,
4122 total - (offset + d + 1)) != -1)
4123 return 0;
4124 return 1;
4127 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4129 * As a special case, we first check if there is any pair of constraints,
4130 * shared by all the basic maps in "map" that force a given dimension
4131 * to be equal to the floor of some affine combination of the input dimensions.
4133 * In particular, if we can find two constraints
4135 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4137 * and
4139 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4141 * where m > 1 and e only depends on parameters and input dimemnsions,
4142 * and such that
4144 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4146 * then we know that we can take
4148 * x = floor((e(...) + c1) / m)
4150 * without having to perform any computation.
4152 * Note that we know that
4154 * c1 + c2 >= 1
4156 * If c1 + c2 were 0, then we would have detected an equality during
4157 * simplification. If c1 + c2 were negative, then we would have detected
4158 * a contradiction.
4160 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4161 __isl_take isl_map *map)
4163 int d, dim;
4164 int i, j, n;
4165 int offset, total;
4166 isl_int sum;
4167 isl_basic_map *hull;
4169 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4170 if (!hull)
4171 goto error;
4173 isl_int_init(sum);
4174 dim = isl_map_dim(map, isl_dim_out);
4175 offset = isl_basic_map_offset(hull, isl_dim_out);
4176 total = 1 + isl_basic_map_total_dim(hull);
4177 n = hull->n_ineq;
4178 for (d = 0; d < dim; ++d) {
4179 for (i = 0; i < n; ++i) {
4180 if (!is_potential_div_constraint(hull->ineq[i],
4181 offset, d, total))
4182 continue;
4183 for (j = i + 1; j < n; ++j) {
4184 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4185 hull->ineq[j] + 1, total - 1))
4186 continue;
4187 isl_int_add(sum, hull->ineq[i][0],
4188 hull->ineq[j][0]);
4189 if (isl_int_abs_lt(sum,
4190 hull->ineq[i][offset + d]))
4191 break;
4194 if (j >= n)
4195 continue;
4196 isl_int_clear(sum);
4197 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4198 j = i;
4199 return pw_multi_aff_from_map_div(map, hull, d, j);
4202 isl_int_clear(sum);
4203 isl_basic_map_free(hull);
4204 return pw_multi_aff_from_map_base(map);
4205 error:
4206 isl_map_free(map);
4207 isl_basic_map_free(hull);
4208 return NULL;
4211 /* Given an affine expression
4213 * [A -> B] -> f(A,B)
4215 * construct an isl_multi_aff
4217 * [A -> B] -> B'
4219 * such that dimension "d" in B' is set to "aff" and the remaining
4220 * dimensions are set equal to the corresponding dimensions in B.
4221 * "n_in" is the dimension of the space A.
4222 * "n_out" is the dimension of the space B.
4224 * If "is_set" is set, then the affine expression is of the form
4226 * [B] -> f(B)
4228 * and we construct an isl_multi_aff
4230 * B -> B'
4232 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4233 unsigned n_in, unsigned n_out, int is_set)
4235 int i;
4236 isl_multi_aff *ma;
4237 isl_space *space, *space2;
4238 isl_local_space *ls;
4240 space = isl_aff_get_domain_space(aff);
4241 ls = isl_local_space_from_space(isl_space_copy(space));
4242 space2 = isl_space_copy(space);
4243 if (!is_set)
4244 space2 = isl_space_range(isl_space_unwrap(space2));
4245 space = isl_space_map_from_domain_and_range(space, space2);
4246 ma = isl_multi_aff_alloc(space);
4247 ma = isl_multi_aff_set_aff(ma, d, aff);
4249 for (i = 0; i < n_out; ++i) {
4250 if (i == d)
4251 continue;
4252 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4253 isl_dim_set, n_in + i);
4254 ma = isl_multi_aff_set_aff(ma, i, aff);
4257 isl_local_space_free(ls);
4259 return ma;
4262 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4263 * taking into account that the dimension at position "d" can be written as
4265 * x = m a + f(..) (1)
4267 * where m is equal to "gcd".
4268 * "i" is the index of the equality in "hull" that defines f(..).
4269 * In particular, the equality is of the form
4271 * f(..) - x + m g(existentials) = 0
4273 * or
4275 * -f(..) + x + m g(existentials) = 0
4277 * We basically plug (1) into "map", resulting in a map with "a"
4278 * in the range instead of "x". The corresponding isl_pw_multi_aff
4279 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4281 * Specifically, given the input map
4283 * A -> B
4285 * We first wrap it into a set
4287 * [A -> B]
4289 * and define (1) on top of the corresponding space, resulting in "aff".
4290 * We use this to create an isl_multi_aff that maps the output position "d"
4291 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4292 * We plug this into the wrapped map, unwrap the result and compute the
4293 * corresponding isl_pw_multi_aff.
4294 * The result is an expression
4296 * A -> T(A)
4298 * We adjust that to
4300 * A -> [A -> T(A)]
4302 * so that we can plug that into "aff", after extending the latter to
4303 * a mapping
4305 * [A -> B] -> B'
4308 * If "map" is actually a set, then there is no "A" space, meaning
4309 * that we do not need to perform any wrapping, and that the result
4310 * of the recursive call is of the form
4312 * [T]
4314 * which is plugged into a mapping of the form
4316 * B -> B'
4318 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4319 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4320 isl_int gcd)
4322 isl_set *set;
4323 isl_space *space;
4324 isl_local_space *ls;
4325 isl_aff *aff;
4326 isl_multi_aff *ma;
4327 isl_pw_multi_aff *pma, *id;
4328 unsigned n_in;
4329 unsigned o_out;
4330 unsigned n_out;
4331 int is_set;
4333 is_set = isl_map_is_set(map);
4335 n_in = isl_basic_map_dim(hull, isl_dim_in);
4336 n_out = isl_basic_map_dim(hull, isl_dim_out);
4337 o_out = isl_basic_map_offset(hull, isl_dim_out);
4339 if (is_set)
4340 set = map;
4341 else
4342 set = isl_map_wrap(map);
4343 space = isl_space_map_from_set(isl_set_get_space(set));
4344 ma = isl_multi_aff_identity(space);
4345 ls = isl_local_space_from_space(isl_set_get_space(set));
4346 aff = isl_aff_alloc(ls);
4347 if (aff) {
4348 isl_int_set_si(aff->v->el[0], 1);
4349 if (isl_int_is_one(hull->eq[i][o_out + d]))
4350 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4351 aff->v->size - 1);
4352 else
4353 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4354 aff->v->size - 1);
4355 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4357 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4358 set = isl_set_preimage_multi_aff(set, ma);
4360 ma = range_map(aff, d, n_in, n_out, is_set);
4362 if (is_set)
4363 map = set;
4364 else
4365 map = isl_set_unwrap(set);
4366 pma = isl_pw_multi_aff_from_map(set);
4368 if (!is_set) {
4369 space = isl_pw_multi_aff_get_domain_space(pma);
4370 space = isl_space_map_from_set(space);
4371 id = isl_pw_multi_aff_identity(space);
4372 pma = isl_pw_multi_aff_range_product(id, pma);
4374 id = isl_pw_multi_aff_from_multi_aff(ma);
4375 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4377 isl_basic_map_free(hull);
4378 return pma;
4381 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4383 * As a special case, we first check if all output dimensions are uniquely
4384 * defined in terms of the parameters and input dimensions over the entire
4385 * domain. If so, we extract the desired isl_pw_multi_aff directly
4386 * from the affine hull of "map" and its domain.
4388 * Otherwise, we check if any of the output dimensions is "strided".
4389 * That is, we check if can be written as
4391 * x = m a + f(..)
4393 * with m greater than 1, a some combination of existentiall quantified
4394 * variables and f and expression in the parameters and input dimensions.
4395 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4397 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4398 * special case.
4400 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4402 int i, j;
4403 int sv;
4404 isl_basic_map *hull;
4405 unsigned n_out;
4406 unsigned o_out;
4407 unsigned n_div;
4408 unsigned o_div;
4409 isl_int gcd;
4411 if (!map)
4412 return NULL;
4414 hull = isl_map_affine_hull(isl_map_copy(map));
4415 sv = isl_basic_map_plain_is_single_valued(hull);
4416 if (sv >= 0 && sv)
4417 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4418 if (sv < 0)
4419 hull = isl_basic_map_free(hull);
4420 if (!hull)
4421 goto error;
4423 n_div = isl_basic_map_dim(hull, isl_dim_div);
4424 o_div = isl_basic_map_offset(hull, isl_dim_div);
4426 if (n_div == 0) {
4427 isl_basic_map_free(hull);
4428 return pw_multi_aff_from_map_check_div(map);
4431 isl_int_init(gcd);
4433 n_out = isl_basic_map_dim(hull, isl_dim_out);
4434 o_out = isl_basic_map_offset(hull, isl_dim_out);
4436 for (i = 0; i < n_out; ++i) {
4437 for (j = 0; j < hull->n_eq; ++j) {
4438 isl_int *eq = hull->eq[j];
4439 isl_pw_multi_aff *res;
4441 if (!isl_int_is_one(eq[o_out + i]) &&
4442 !isl_int_is_negone(eq[o_out + i]))
4443 continue;
4444 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4445 continue;
4446 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4447 n_out - (i + 1)) != -1)
4448 continue;
4449 isl_seq_gcd(eq + o_div, n_div, &gcd);
4450 if (isl_int_is_zero(gcd))
4451 continue;
4452 if (isl_int_is_one(gcd))
4453 continue;
4455 res = pw_multi_aff_from_map_stride(map, hull,
4456 i, j, gcd);
4457 isl_int_clear(gcd);
4458 return res;
4462 isl_int_clear(gcd);
4463 isl_basic_map_free(hull);
4464 return pw_multi_aff_from_map_check_div(map);
4465 error:
4466 isl_map_free(map);
4467 return NULL;
4470 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4472 return isl_pw_multi_aff_from_map(set);
4475 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4476 * add it to *user.
4478 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4480 isl_union_pw_multi_aff **upma = user;
4481 isl_pw_multi_aff *pma;
4483 pma = isl_pw_multi_aff_from_map(map);
4484 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4486 return *upma ? 0 : -1;
4489 /* Try and create an isl_union_pw_multi_aff that is equivalent
4490 * to the given isl_union_map.
4491 * The isl_union_map is required to be single-valued in each space.
4492 * Otherwise, an error is produced.
4494 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4495 __isl_take isl_union_map *umap)
4497 isl_space *space;
4498 isl_union_pw_multi_aff *upma;
4500 space = isl_union_map_get_space(umap);
4501 upma = isl_union_pw_multi_aff_empty(space);
4502 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4503 upma = isl_union_pw_multi_aff_free(upma);
4504 isl_union_map_free(umap);
4506 return upma;
4509 /* Try and create an isl_union_pw_multi_aff that is equivalent
4510 * to the given isl_union_set.
4511 * The isl_union_set is required to be a singleton in each space.
4512 * Otherwise, an error is produced.
4514 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4515 __isl_take isl_union_set *uset)
4517 return isl_union_pw_multi_aff_from_union_map(uset);
4520 /* Return the piecewise affine expression "set ? 1 : 0".
4522 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4524 isl_pw_aff *pa;
4525 isl_space *space = isl_set_get_space(set);
4526 isl_local_space *ls = isl_local_space_from_space(space);
4527 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4528 isl_aff *one = isl_aff_zero_on_domain(ls);
4530 one = isl_aff_add_constant_si(one, 1);
4531 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4532 set = isl_set_complement(set);
4533 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4535 return pa;
4538 /* Plug in "subs" for dimension "type", "pos" of "aff".
4540 * Let i be the dimension to replace and let "subs" be of the form
4542 * f/d
4544 * and "aff" of the form
4546 * (a i + g)/m
4548 * The result is
4550 * (a f + d g')/(m d)
4552 * where g' is the result of plugging in "subs" in each of the integer
4553 * divisions in g.
4555 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4556 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4558 isl_ctx *ctx;
4559 isl_int v;
4561 aff = isl_aff_cow(aff);
4562 if (!aff || !subs)
4563 return isl_aff_free(aff);
4565 ctx = isl_aff_get_ctx(aff);
4566 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4567 isl_die(ctx, isl_error_invalid,
4568 "spaces don't match", return isl_aff_free(aff));
4569 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4570 isl_die(ctx, isl_error_unsupported,
4571 "cannot handle divs yet", return isl_aff_free(aff));
4573 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4574 if (!aff->ls)
4575 return isl_aff_free(aff);
4577 aff->v = isl_vec_cow(aff->v);
4578 if (!aff->v)
4579 return isl_aff_free(aff);
4581 pos += isl_local_space_offset(aff->ls, type);
4583 isl_int_init(v);
4584 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4585 aff->v->size, subs->v->size, v);
4586 isl_int_clear(v);
4588 return aff;
4591 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4592 * expressions in "maff".
4594 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4595 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4596 __isl_keep isl_aff *subs)
4598 int i;
4600 maff = isl_multi_aff_cow(maff);
4601 if (!maff || !subs)
4602 return isl_multi_aff_free(maff);
4604 if (type == isl_dim_in)
4605 type = isl_dim_set;
4607 for (i = 0; i < maff->n; ++i) {
4608 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4609 if (!maff->p[i])
4610 return isl_multi_aff_free(maff);
4613 return maff;
4616 /* Plug in "subs" for dimension "type", "pos" of "pma".
4618 * pma is of the form
4620 * A_i(v) -> M_i(v)
4622 * while subs is of the form
4624 * v' = B_j(v) -> S_j
4626 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4627 * has a contribution in the result, in particular
4629 * C_ij(S_j) -> M_i(S_j)
4631 * Note that plugging in S_j in C_ij may also result in an empty set
4632 * and this contribution should simply be discarded.
4634 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4635 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4636 __isl_keep isl_pw_aff *subs)
4638 int i, j, n;
4639 isl_pw_multi_aff *res;
4641 if (!pma || !subs)
4642 return isl_pw_multi_aff_free(pma);
4644 n = pma->n * subs->n;
4645 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4647 for (i = 0; i < pma->n; ++i) {
4648 for (j = 0; j < subs->n; ++j) {
4649 isl_set *common;
4650 isl_multi_aff *res_ij;
4651 int empty;
4653 common = isl_set_intersect(
4654 isl_set_copy(pma->p[i].set),
4655 isl_set_copy(subs->p[j].set));
4656 common = isl_set_substitute(common,
4657 type, pos, subs->p[j].aff);
4658 empty = isl_set_plain_is_empty(common);
4659 if (empty < 0 || empty) {
4660 isl_set_free(common);
4661 if (empty < 0)
4662 goto error;
4663 continue;
4666 res_ij = isl_multi_aff_substitute(
4667 isl_multi_aff_copy(pma->p[i].maff),
4668 type, pos, subs->p[j].aff);
4670 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4674 isl_pw_multi_aff_free(pma);
4675 return res;
4676 error:
4677 isl_pw_multi_aff_free(pma);
4678 isl_pw_multi_aff_free(res);
4679 return NULL;
4682 /* Compute the preimage of a range of dimensions in the affine expression "src"
4683 * under "ma" and put the result in "dst". The number of dimensions in "src"
4684 * that precede the range is given by "n_before". The number of dimensions
4685 * in the range is given by the number of output dimensions of "ma".
4686 * The number of dimensions that follow the range is given by "n_after".
4687 * If "has_denom" is set (to one),
4688 * then "src" and "dst" have an extra initial denominator.
4689 * "n_div_ma" is the number of existentials in "ma"
4690 * "n_div_bset" is the number of existentials in "src"
4691 * The resulting "dst" (which is assumed to have been allocated by
4692 * the caller) contains coefficients for both sets of existentials,
4693 * first those in "ma" and then those in "src".
4694 * f, c1, c2 and g are temporary objects that have been initialized
4695 * by the caller.
4697 * Let src represent the expression
4699 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4701 * and let ma represent the expressions
4703 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4705 * We start out with the following expression for dst:
4707 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4709 * with the multiplication factor f initially equal to 1
4710 * and f \sum_i b_i v_i kept separately.
4711 * For each x_i that we substitute, we multiply the numerator
4712 * (and denominator) of dst by c_1 = m_i and add the numerator
4713 * of the x_i expression multiplied by c_2 = f b_i,
4714 * after removing the common factors of c_1 and c_2.
4715 * The multiplication factor f also needs to be multiplied by c_1
4716 * for the next x_j, j > i.
4718 void isl_seq_preimage(isl_int *dst, isl_int *src,
4719 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4720 int n_div_ma, int n_div_bmap,
4721 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4723 int i;
4724 int n_param, n_in, n_out;
4725 int o_dst, o_src;
4727 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4728 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4729 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4731 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4732 o_dst = o_src = has_denom + 1 + n_param + n_before;
4733 isl_seq_clr(dst + o_dst, n_in);
4734 o_dst += n_in;
4735 o_src += n_out;
4736 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4737 o_dst += n_after;
4738 o_src += n_after;
4739 isl_seq_clr(dst + o_dst, n_div_ma);
4740 o_dst += n_div_ma;
4741 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4743 isl_int_set_si(f, 1);
4745 for (i = 0; i < n_out; ++i) {
4746 int offset = has_denom + 1 + n_param + n_before + i;
4748 if (isl_int_is_zero(src[offset]))
4749 continue;
4750 isl_int_set(c1, ma->p[i]->v->el[0]);
4751 isl_int_mul(c2, f, src[offset]);
4752 isl_int_gcd(g, c1, c2);
4753 isl_int_divexact(c1, c1, g);
4754 isl_int_divexact(c2, c2, g);
4756 isl_int_mul(f, f, c1);
4757 o_dst = has_denom;
4758 o_src = 1;
4759 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4760 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4761 o_dst += 1 + n_param;
4762 o_src += 1 + n_param;
4763 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4764 o_dst += n_before;
4765 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4766 c2, ma->p[i]->v->el + o_src, n_in);
4767 o_dst += n_in;
4768 o_src += n_in;
4769 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4770 o_dst += n_after;
4771 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4772 c2, ma->p[i]->v->el + o_src, n_div_ma);
4773 o_dst += n_div_ma;
4774 o_src += n_div_ma;
4775 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4776 if (has_denom)
4777 isl_int_mul(dst[0], dst[0], c1);
4781 /* Compute the pullback of "aff" by the function represented by "ma".
4782 * In other words, plug in "ma" in "aff". The result is an affine expression
4783 * defined over the domain space of "ma".
4785 * If "aff" is represented by
4787 * (a(p) + b x + c(divs))/d
4789 * and ma is represented by
4791 * x = D(p) + F(y) + G(divs')
4793 * then the result is
4795 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4797 * The divs in the local space of the input are similarly adjusted
4798 * through a call to isl_local_space_preimage_multi_aff.
4800 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4801 __isl_take isl_multi_aff *ma)
4803 isl_aff *res = NULL;
4804 isl_local_space *ls;
4805 int n_div_aff, n_div_ma;
4806 isl_int f, c1, c2, g;
4808 ma = isl_multi_aff_align_divs(ma);
4809 if (!aff || !ma)
4810 goto error;
4812 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4813 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4815 ls = isl_aff_get_domain_local_space(aff);
4816 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4817 res = isl_aff_alloc(ls);
4818 if (!res)
4819 goto error;
4821 isl_int_init(f);
4822 isl_int_init(c1);
4823 isl_int_init(c2);
4824 isl_int_init(g);
4826 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4827 f, c1, c2, g, 1);
4829 isl_int_clear(f);
4830 isl_int_clear(c1);
4831 isl_int_clear(c2);
4832 isl_int_clear(g);
4834 isl_aff_free(aff);
4835 isl_multi_aff_free(ma);
4836 res = isl_aff_normalize(res);
4837 return res;
4838 error:
4839 isl_aff_free(aff);
4840 isl_multi_aff_free(ma);
4841 isl_aff_free(res);
4842 return NULL;
4845 /* Compute the pullback of "aff1" by the function represented by "aff2".
4846 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4847 * defined over the domain space of "aff1".
4849 * The domain of "aff1" should match the range of "aff2", which means
4850 * that it should be single-dimensional.
4852 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
4853 __isl_take isl_aff *aff2)
4855 isl_multi_aff *ma;
4857 ma = isl_multi_aff_from_aff(aff2);
4858 return isl_aff_pullback_multi_aff(aff1, ma);
4861 /* Compute the pullback of "ma1" by the function represented by "ma2".
4862 * In other words, plug in "ma2" in "ma1".
4864 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4866 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
4867 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4869 int i;
4870 isl_space *space = NULL;
4872 ma2 = isl_multi_aff_align_divs(ma2);
4873 ma1 = isl_multi_aff_cow(ma1);
4874 if (!ma1 || !ma2)
4875 goto error;
4877 space = isl_space_join(isl_multi_aff_get_space(ma2),
4878 isl_multi_aff_get_space(ma1));
4880 for (i = 0; i < ma1->n; ++i) {
4881 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4882 isl_multi_aff_copy(ma2));
4883 if (!ma1->p[i])
4884 goto error;
4887 ma1 = isl_multi_aff_reset_space(ma1, space);
4888 isl_multi_aff_free(ma2);
4889 return ma1;
4890 error:
4891 isl_space_free(space);
4892 isl_multi_aff_free(ma2);
4893 isl_multi_aff_free(ma1);
4894 return NULL;
4897 /* Compute the pullback of "ma1" by the function represented by "ma2".
4898 * In other words, plug in "ma2" in "ma1".
4900 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4901 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4903 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
4904 &isl_multi_aff_pullback_multi_aff_aligned);
4907 /* Extend the local space of "dst" to include the divs
4908 * in the local space of "src".
4910 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4911 __isl_keep isl_aff *src)
4913 isl_ctx *ctx;
4914 int *exp1 = NULL;
4915 int *exp2 = NULL;
4916 isl_mat *div;
4918 if (!src || !dst)
4919 return isl_aff_free(dst);
4921 ctx = isl_aff_get_ctx(src);
4922 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4923 isl_die(ctx, isl_error_invalid,
4924 "spaces don't match", goto error);
4926 if (src->ls->div->n_row == 0)
4927 return dst;
4929 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4930 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4931 if (!exp1 || (dst->ls->div->n_row && !exp2))
4932 goto error;
4934 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4935 dst = isl_aff_expand_divs(dst, div, exp2);
4936 free(exp1);
4937 free(exp2);
4939 return dst;
4940 error:
4941 free(exp1);
4942 free(exp2);
4943 return isl_aff_free(dst);
4946 /* Adjust the local spaces of the affine expressions in "maff"
4947 * such that they all have the save divs.
4949 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4950 __isl_take isl_multi_aff *maff)
4952 int i;
4954 if (!maff)
4955 return NULL;
4956 if (maff->n == 0)
4957 return maff;
4958 maff = isl_multi_aff_cow(maff);
4959 if (!maff)
4960 return NULL;
4962 for (i = 1; i < maff->n; ++i)
4963 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4964 for (i = 1; i < maff->n; ++i) {
4965 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4966 if (!maff->p[i])
4967 return isl_multi_aff_free(maff);
4970 return maff;
4973 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4975 aff = isl_aff_cow(aff);
4976 if (!aff)
4977 return NULL;
4979 aff->ls = isl_local_space_lift(aff->ls);
4980 if (!aff->ls)
4981 return isl_aff_free(aff);
4983 return aff;
4986 /* Lift "maff" to a space with extra dimensions such that the result
4987 * has no more existentially quantified variables.
4988 * If "ls" is not NULL, then *ls is assigned the local space that lies
4989 * at the basis of the lifting applied to "maff".
4991 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4992 __isl_give isl_local_space **ls)
4994 int i;
4995 isl_space *space;
4996 unsigned n_div;
4998 if (ls)
4999 *ls = NULL;
5001 if (!maff)
5002 return NULL;
5004 if (maff->n == 0) {
5005 if (ls) {
5006 isl_space *space = isl_multi_aff_get_domain_space(maff);
5007 *ls = isl_local_space_from_space(space);
5008 if (!*ls)
5009 return isl_multi_aff_free(maff);
5011 return maff;
5014 maff = isl_multi_aff_cow(maff);
5015 maff = isl_multi_aff_align_divs(maff);
5016 if (!maff)
5017 return NULL;
5019 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5020 space = isl_multi_aff_get_space(maff);
5021 space = isl_space_lift(isl_space_domain(space), n_div);
5022 space = isl_space_extend_domain_with_range(space,
5023 isl_multi_aff_get_space(maff));
5024 if (!space)
5025 return isl_multi_aff_free(maff);
5026 isl_space_free(maff->space);
5027 maff->space = space;
5029 if (ls) {
5030 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5031 if (!*ls)
5032 return isl_multi_aff_free(maff);
5035 for (i = 0; i < maff->n; ++i) {
5036 maff->p[i] = isl_aff_lift(maff->p[i]);
5037 if (!maff->p[i])
5038 goto error;
5041 return maff;
5042 error:
5043 if (ls)
5044 isl_local_space_free(*ls);
5045 return isl_multi_aff_free(maff);
5049 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5051 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5052 __isl_keep isl_pw_multi_aff *pma, int pos)
5054 int i;
5055 int n_out;
5056 isl_space *space;
5057 isl_pw_aff *pa;
5059 if (!pma)
5060 return NULL;
5062 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5063 if (pos < 0 || pos >= n_out)
5064 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5065 "index out of bounds", return NULL);
5067 space = isl_pw_multi_aff_get_space(pma);
5068 space = isl_space_drop_dims(space, isl_dim_out,
5069 pos + 1, n_out - pos - 1);
5070 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5072 pa = isl_pw_aff_alloc_size(space, pma->n);
5073 for (i = 0; i < pma->n; ++i) {
5074 isl_aff *aff;
5075 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5076 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5079 return pa;
5082 /* Return an isl_pw_multi_aff with the given "set" as domain and
5083 * an unnamed zero-dimensional range.
5085 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5086 __isl_take isl_set *set)
5088 isl_multi_aff *ma;
5089 isl_space *space;
5091 space = isl_set_get_space(set);
5092 space = isl_space_from_domain(space);
5093 ma = isl_multi_aff_zero(space);
5094 return isl_pw_multi_aff_alloc(set, ma);
5097 /* Add an isl_pw_multi_aff with the given "set" as domain and
5098 * an unnamed zero-dimensional range to *user.
5100 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5102 isl_union_pw_multi_aff **upma = user;
5103 isl_pw_multi_aff *pma;
5105 pma = isl_pw_multi_aff_from_domain(set);
5106 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5108 return 0;
5111 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5112 * an unnamed zero-dimensional range.
5114 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5115 __isl_take isl_union_set *uset)
5117 isl_space *space;
5118 isl_union_pw_multi_aff *upma;
5120 if (!uset)
5121 return NULL;
5123 space = isl_union_set_get_space(uset);
5124 upma = isl_union_pw_multi_aff_empty(space);
5126 if (isl_union_set_foreach_set(uset,
5127 &add_pw_multi_aff_from_domain, &upma) < 0)
5128 goto error;
5130 isl_union_set_free(uset);
5131 return upma;
5132 error:
5133 isl_union_set_free(uset);
5134 isl_union_pw_multi_aff_free(upma);
5135 return NULL;
5138 /* Convert "pma" to an isl_map and add it to *umap.
5140 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5142 isl_union_map **umap = user;
5143 isl_map *map;
5145 map = isl_map_from_pw_multi_aff(pma);
5146 *umap = isl_union_map_add_map(*umap, map);
5148 return 0;
5151 /* Construct a union map mapping the domain of the union
5152 * piecewise multi-affine expression to its range, with each dimension
5153 * in the range equated to the corresponding affine expression on its cell.
5155 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5156 __isl_take isl_union_pw_multi_aff *upma)
5158 isl_space *space;
5159 isl_union_map *umap;
5161 if (!upma)
5162 return NULL;
5164 space = isl_union_pw_multi_aff_get_space(upma);
5165 umap = isl_union_map_empty(space);
5167 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5168 &map_from_pw_multi_aff, &umap) < 0)
5169 goto error;
5171 isl_union_pw_multi_aff_free(upma);
5172 return umap;
5173 error:
5174 isl_union_pw_multi_aff_free(upma);
5175 isl_union_map_free(umap);
5176 return NULL;
5179 /* Local data for bin_entry and the callback "fn".
5181 struct isl_union_pw_multi_aff_bin_data {
5182 isl_union_pw_multi_aff *upma2;
5183 isl_union_pw_multi_aff *res;
5184 isl_pw_multi_aff *pma;
5185 int (*fn)(void **entry, void *user);
5188 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5189 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5191 static int bin_entry(void **entry, void *user)
5193 struct isl_union_pw_multi_aff_bin_data *data = user;
5194 isl_pw_multi_aff *pma = *entry;
5196 data->pma = pma;
5197 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
5198 data->fn, data) < 0)
5199 return -1;
5201 return 0;
5204 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5205 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5206 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5207 * as *entry. The callback should adjust data->res if desired.
5209 static __isl_give isl_union_pw_multi_aff *bin_op(
5210 __isl_take isl_union_pw_multi_aff *upma1,
5211 __isl_take isl_union_pw_multi_aff *upma2,
5212 int (*fn)(void **entry, void *user))
5214 isl_space *space;
5215 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5217 space = isl_union_pw_multi_aff_get_space(upma2);
5218 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5219 space = isl_union_pw_multi_aff_get_space(upma1);
5220 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5222 if (!upma1 || !upma2)
5223 goto error;
5225 data.upma2 = upma2;
5226 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
5227 upma1->table.n);
5228 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
5229 &bin_entry, &data) < 0)
5230 goto error;
5232 isl_union_pw_multi_aff_free(upma1);
5233 isl_union_pw_multi_aff_free(upma2);
5234 return data.res;
5235 error:
5236 isl_union_pw_multi_aff_free(upma1);
5237 isl_union_pw_multi_aff_free(upma2);
5238 isl_union_pw_multi_aff_free(data.res);
5239 return NULL;
5242 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5243 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5245 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5246 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5248 isl_space *space;
5250 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5251 isl_pw_multi_aff_get_space(pma2));
5252 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5253 &isl_multi_aff_range_product);
5256 /* Given two isl_pw_multi_affs A -> B and C -> D,
5257 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5259 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5260 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5262 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5263 &pw_multi_aff_range_product);
5266 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5267 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5269 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5270 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5272 isl_space *space;
5274 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5275 isl_pw_multi_aff_get_space(pma2));
5276 space = isl_space_flatten_range(space);
5277 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5278 &isl_multi_aff_flat_range_product);
5281 /* Given two isl_pw_multi_affs A -> B and C -> D,
5282 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5284 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5285 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5287 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5288 &pw_multi_aff_flat_range_product);
5291 /* If data->pma and *entry have the same domain space, then compute
5292 * their flat range product and the result to data->res.
5294 static int flat_range_product_entry(void **entry, void *user)
5296 struct isl_union_pw_multi_aff_bin_data *data = user;
5297 isl_pw_multi_aff *pma2 = *entry;
5299 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
5300 pma2->dim, isl_dim_in))
5301 return 0;
5303 pma2 = isl_pw_multi_aff_flat_range_product(
5304 isl_pw_multi_aff_copy(data->pma),
5305 isl_pw_multi_aff_copy(pma2));
5307 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5309 return 0;
5312 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5313 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5315 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5316 __isl_take isl_union_pw_multi_aff *upma1,
5317 __isl_take isl_union_pw_multi_aff *upma2)
5319 return bin_op(upma1, upma2, &flat_range_product_entry);
5322 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5323 * The parameters are assumed to have been aligned.
5325 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5326 * except that it works on two different isl_pw_* types.
5328 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5329 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5330 __isl_take isl_pw_aff *pa)
5332 int i, j, n;
5333 isl_pw_multi_aff *res = NULL;
5335 if (!pma || !pa)
5336 goto error;
5338 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
5339 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5340 "domains don't match", goto error);
5341 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5342 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5343 "index out of bounds", goto error);
5345 n = pma->n * pa->n;
5346 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5348 for (i = 0; i < pma->n; ++i) {
5349 for (j = 0; j < pa->n; ++j) {
5350 isl_set *common;
5351 isl_multi_aff *res_ij;
5352 int empty;
5354 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5355 isl_set_copy(pa->p[j].set));
5356 empty = isl_set_plain_is_empty(common);
5357 if (empty < 0 || empty) {
5358 isl_set_free(common);
5359 if (empty < 0)
5360 goto error;
5361 continue;
5364 res_ij = isl_multi_aff_set_aff(
5365 isl_multi_aff_copy(pma->p[i].maff), pos,
5366 isl_aff_copy(pa->p[j].aff));
5367 res_ij = isl_multi_aff_gist(res_ij,
5368 isl_set_copy(common));
5370 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5374 isl_pw_multi_aff_free(pma);
5375 isl_pw_aff_free(pa);
5376 return res;
5377 error:
5378 isl_pw_multi_aff_free(pma);
5379 isl_pw_aff_free(pa);
5380 return isl_pw_multi_aff_free(res);
5383 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5385 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5386 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5387 __isl_take isl_pw_aff *pa)
5389 if (!pma || !pa)
5390 goto error;
5391 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5392 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5393 if (!isl_space_has_named_params(pma->dim) ||
5394 !isl_space_has_named_params(pa->dim))
5395 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5396 "unaligned unnamed parameters", goto error);
5397 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5398 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5399 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5400 error:
5401 isl_pw_multi_aff_free(pma);
5402 isl_pw_aff_free(pa);
5403 return NULL;
5406 /* Do the parameters of "pa" match those of "space"?
5408 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5409 __isl_keep isl_space *space)
5411 isl_space *pa_space;
5412 int match;
5414 if (!pa || !space)
5415 return -1;
5417 pa_space = isl_pw_aff_get_space(pa);
5419 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5421 isl_space_free(pa_space);
5422 return match;
5425 /* Check that the domain space of "pa" matches "space".
5427 * Return 0 on success and -1 on error.
5429 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5430 __isl_keep isl_space *space)
5432 isl_space *pa_space;
5433 int match;
5435 if (!pa || !space)
5436 return -1;
5438 pa_space = isl_pw_aff_get_space(pa);
5440 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5441 if (match < 0)
5442 goto error;
5443 if (!match)
5444 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5445 "parameters don't match", goto error);
5446 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5447 if (match < 0)
5448 goto error;
5449 if (!match)
5450 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5451 "domains don't match", goto error);
5452 isl_space_free(pa_space);
5453 return 0;
5454 error:
5455 isl_space_free(pa_space);
5456 return -1;
5459 #undef BASE
5460 #define BASE pw_aff
5462 #include <isl_multi_templ.c>
5464 /* Scale the elements of "pma" by the corresponding elements of "mv".
5466 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5467 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5469 int i;
5471 pma = isl_pw_multi_aff_cow(pma);
5472 if (!pma || !mv)
5473 goto error;
5474 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5475 mv->space, isl_dim_set))
5476 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5477 "spaces don't match", goto error);
5478 if (!isl_space_match(pma->dim, isl_dim_param,
5479 mv->space, isl_dim_param)) {
5480 pma = isl_pw_multi_aff_align_params(pma,
5481 isl_multi_val_get_space(mv));
5482 mv = isl_multi_val_align_params(mv,
5483 isl_pw_multi_aff_get_space(pma));
5484 if (!pma || !mv)
5485 goto error;
5488 for (i = 0; i < pma->n; ++i) {
5489 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5490 isl_multi_val_copy(mv));
5491 if (!pma->p[i].maff)
5492 goto error;
5495 isl_multi_val_free(mv);
5496 return pma;
5497 error:
5498 isl_multi_val_free(mv);
5499 isl_pw_multi_aff_free(pma);
5500 return NULL;
5503 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5504 * mv contains the mv argument.
5505 * res collects the results.
5507 struct isl_union_pw_multi_aff_scale_multi_val_data {
5508 isl_multi_val *mv;
5509 isl_union_pw_multi_aff *res;
5512 /* This function is called for each entry of an isl_union_pw_multi_aff.
5513 * If the space of the entry matches that of data->mv,
5514 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5515 * to data->res.
5517 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5519 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5520 isl_pw_multi_aff *pma = *entry;
5522 if (!pma)
5523 return -1;
5524 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5525 data->mv->space, isl_dim_set))
5526 return 0;
5528 pma = isl_pw_multi_aff_copy(pma);
5529 pma = isl_pw_multi_aff_scale_multi_val(pma,
5530 isl_multi_val_copy(data->mv));
5531 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5532 if (!data->res)
5533 return -1;
5535 return 0;
5538 /* Scale the elements of "upma" by the corresponding elements of "mv",
5539 * for those entries that match the space of "mv".
5541 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5542 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5544 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5546 upma = isl_union_pw_multi_aff_align_params(upma,
5547 isl_multi_val_get_space(mv));
5548 mv = isl_multi_val_align_params(mv,
5549 isl_union_pw_multi_aff_get_space(upma));
5550 if (!upma || !mv)
5551 goto error;
5553 data.mv = mv;
5554 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->dim),
5555 upma->table.n);
5556 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5557 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5558 goto error;
5560 isl_multi_val_free(mv);
5561 isl_union_pw_multi_aff_free(upma);
5562 return data.res;
5563 error:
5564 isl_multi_val_free(mv);
5565 isl_union_pw_multi_aff_free(upma);
5566 return NULL;
5569 /* Construct and return a piecewise multi affine expression
5570 * in the given space with value zero in each of the output dimensions and
5571 * a universe domain.
5573 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5575 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5578 /* Construct and return a piecewise multi affine expression
5579 * that is equal to the given piecewise affine expression.
5581 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5582 __isl_take isl_pw_aff *pa)
5584 int i;
5585 isl_space *space;
5586 isl_pw_multi_aff *pma;
5588 if (!pa)
5589 return NULL;
5591 space = isl_pw_aff_get_space(pa);
5592 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5594 for (i = 0; i < pa->n; ++i) {
5595 isl_set *set;
5596 isl_multi_aff *ma;
5598 set = isl_set_copy(pa->p[i].set);
5599 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
5600 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
5603 isl_pw_aff_free(pa);
5604 return pma;
5607 /* Construct a set or map mapping the shared (parameter) domain
5608 * of the piecewise affine expressions to the range of "mpa"
5609 * with each dimension in the range equated to the
5610 * corresponding piecewise affine expression.
5612 static __isl_give isl_map *map_from_multi_pw_aff(
5613 __isl_take isl_multi_pw_aff *mpa)
5615 int i;
5616 isl_space *space;
5617 isl_map *map;
5619 if (!mpa)
5620 return NULL;
5622 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
5623 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5624 "invalid space", return isl_multi_pw_aff_free(mpa));
5626 space = isl_multi_pw_aff_get_domain_space(mpa);
5627 map = isl_map_universe(isl_space_from_domain(space));
5629 for (i = 0; i < mpa->n; ++i) {
5630 isl_pw_aff *pa;
5631 isl_map *map_i;
5633 pa = isl_pw_aff_copy(mpa->p[i]);
5634 map_i = map_from_pw_aff(pa);
5636 map = isl_map_flat_range_product(map, map_i);
5639 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
5641 isl_multi_pw_aff_free(mpa);
5642 return map;
5645 /* Construct a map mapping the shared domain
5646 * of the piecewise affine expressions to the range of "mpa"
5647 * with each dimension in the range equated to the
5648 * corresponding piecewise affine expression.
5650 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5652 if (!mpa)
5653 return NULL;
5654 if (isl_space_is_set(mpa->space))
5655 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5656 "space of input is not a map", goto error);
5658 return map_from_multi_pw_aff(mpa);
5659 error:
5660 isl_multi_pw_aff_free(mpa);
5661 return NULL;
5664 /* Construct a set mapping the shared parameter domain
5665 * of the piecewise affine expressions to the space of "mpa"
5666 * with each dimension in the range equated to the
5667 * corresponding piecewise affine expression.
5669 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5671 if (!mpa)
5672 return NULL;
5673 if (!isl_space_is_set(mpa->space))
5674 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5675 "space of input is not a set", goto error);
5677 return map_from_multi_pw_aff(mpa);
5678 error:
5679 isl_multi_pw_aff_free(mpa);
5680 return NULL;
5683 /* Construct and return a piecewise multi affine expression
5684 * that is equal to the given multi piecewise affine expression
5685 * on the shared domain of the piecewise affine expressions.
5687 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
5688 __isl_take isl_multi_pw_aff *mpa)
5690 int i;
5691 isl_space *space;
5692 isl_pw_aff *pa;
5693 isl_pw_multi_aff *pma;
5695 if (!mpa)
5696 return NULL;
5698 space = isl_multi_pw_aff_get_space(mpa);
5700 if (mpa->n == 0) {
5701 isl_multi_pw_aff_free(mpa);
5702 return isl_pw_multi_aff_zero(space);
5705 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
5706 pma = isl_pw_multi_aff_from_pw_aff(pa);
5708 for (i = 1; i < mpa->n; ++i) {
5709 isl_pw_multi_aff *pma_i;
5711 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
5712 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
5713 pma = isl_pw_multi_aff_range_product(pma, pma_i);
5716 pma = isl_pw_multi_aff_reset_space(pma, space);
5718 isl_multi_pw_aff_free(mpa);
5719 return pma;
5722 /* Construct and return a multi piecewise affine expression
5723 * that is equal to the given multi affine expression.
5725 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
5726 __isl_take isl_multi_aff *ma)
5728 int i, n;
5729 isl_multi_pw_aff *mpa;
5731 if (!ma)
5732 return NULL;
5734 n = isl_multi_aff_dim(ma, isl_dim_out);
5735 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
5737 for (i = 0; i < n; ++i) {
5738 isl_pw_aff *pa;
5740 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
5741 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5744 isl_multi_aff_free(ma);
5745 return mpa;
5748 /* Construct and return a multi piecewise affine expression
5749 * that is equal to the given piecewise multi affine expression.
5751 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
5752 __isl_take isl_pw_multi_aff *pma)
5754 int i, n;
5755 isl_space *space;
5756 isl_multi_pw_aff *mpa;
5758 if (!pma)
5759 return NULL;
5761 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
5762 space = isl_pw_multi_aff_get_space(pma);
5763 mpa = isl_multi_pw_aff_alloc(space);
5765 for (i = 0; i < n; ++i) {
5766 isl_pw_aff *pa;
5768 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
5769 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5772 isl_pw_multi_aff_free(pma);
5773 return mpa;
5776 /* Do "pa1" and "pa2" represent the same function?
5778 * We first check if they are obviously equal.
5779 * If not, we convert them to maps and check if those are equal.
5781 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
5783 int equal;
5784 isl_map *map1, *map2;
5786 if (!pa1 || !pa2)
5787 return -1;
5789 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
5790 if (equal < 0 || equal)
5791 return equal;
5793 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
5794 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
5795 equal = isl_map_is_equal(map1, map2);
5796 isl_map_free(map1);
5797 isl_map_free(map2);
5799 return equal;
5802 /* Do "mpa1" and "mpa2" represent the same function?
5804 * Note that we cannot convert the entire isl_multi_pw_aff
5805 * to a map because the domains of the piecewise affine expressions
5806 * may not be the same.
5808 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
5809 __isl_keep isl_multi_pw_aff *mpa2)
5811 int i;
5812 int equal;
5814 if (!mpa1 || !mpa2)
5815 return -1;
5817 if (!isl_space_match(mpa1->space, isl_dim_param,
5818 mpa2->space, isl_dim_param)) {
5819 if (!isl_space_has_named_params(mpa1->space))
5820 return 0;
5821 if (!isl_space_has_named_params(mpa2->space))
5822 return 0;
5823 mpa1 = isl_multi_pw_aff_copy(mpa1);
5824 mpa2 = isl_multi_pw_aff_copy(mpa2);
5825 mpa1 = isl_multi_pw_aff_align_params(mpa1,
5826 isl_multi_pw_aff_get_space(mpa2));
5827 mpa2 = isl_multi_pw_aff_align_params(mpa2,
5828 isl_multi_pw_aff_get_space(mpa1));
5829 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
5830 isl_multi_pw_aff_free(mpa1);
5831 isl_multi_pw_aff_free(mpa2);
5832 return equal;
5835 equal = isl_space_is_equal(mpa1->space, mpa2->space);
5836 if (equal < 0 || !equal)
5837 return equal;
5839 for (i = 0; i < mpa1->n; ++i) {
5840 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
5841 if (equal < 0 || !equal)
5842 return equal;
5845 return 1;
5848 /* Coalesce the elements of "mpa".
5850 * Note that such coalescing does not change the meaning of "mpa"
5851 * so there is no need to cow. We do need to be careful not to
5852 * destroy any other copies of "mpa" in case of failure.
5854 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
5855 __isl_take isl_multi_pw_aff *mpa)
5857 int i;
5859 if (!mpa)
5860 return NULL;
5862 for (i = 0; i < mpa->n; ++i) {
5863 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
5864 pa = isl_pw_aff_coalesce(pa);
5865 if (!pa)
5866 return isl_multi_pw_aff_free(mpa);
5867 isl_pw_aff_free(mpa->p[i]);
5868 mpa->p[i] = pa;
5871 return mpa;
5874 /* Compute the pullback of "mpa" by the function represented by "ma".
5875 * In other words, plug in "ma" in "mpa".
5877 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5879 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
5880 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5882 int i;
5883 isl_space *space = NULL;
5885 mpa = isl_multi_pw_aff_cow(mpa);
5886 if (!mpa || !ma)
5887 goto error;
5889 space = isl_space_join(isl_multi_aff_get_space(ma),
5890 isl_multi_pw_aff_get_space(mpa));
5891 if (!space)
5892 goto error;
5894 for (i = 0; i < mpa->n; ++i) {
5895 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
5896 isl_multi_aff_copy(ma));
5897 if (!mpa->p[i])
5898 goto error;
5901 isl_multi_aff_free(ma);
5902 isl_space_free(mpa->space);
5903 mpa->space = space;
5904 return mpa;
5905 error:
5906 isl_space_free(space);
5907 isl_multi_pw_aff_free(mpa);
5908 isl_multi_aff_free(ma);
5909 return NULL;
5912 /* Compute the pullback of "mpa" by the function represented by "ma".
5913 * In other words, plug in "ma" in "mpa".
5915 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
5916 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5918 if (!mpa || !ma)
5919 goto error;
5920 if (isl_space_match(mpa->space, isl_dim_param,
5921 ma->space, isl_dim_param))
5922 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5923 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
5924 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
5925 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5926 error:
5927 isl_multi_pw_aff_free(mpa);
5928 isl_multi_aff_free(ma);
5929 return NULL;
5932 /* Compute the pullback of "mpa" by the function represented by "pma".
5933 * In other words, plug in "pma" in "mpa".
5935 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5937 static __isl_give isl_multi_pw_aff *
5938 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5939 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
5941 int i;
5942 isl_space *space = NULL;
5944 mpa = isl_multi_pw_aff_cow(mpa);
5945 if (!mpa || !pma)
5946 goto error;
5948 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
5949 isl_multi_pw_aff_get_space(mpa));
5951 for (i = 0; i < mpa->n; ++i) {
5952 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
5953 isl_pw_multi_aff_copy(pma));
5954 if (!mpa->p[i])
5955 goto error;
5958 isl_pw_multi_aff_free(pma);
5959 isl_space_free(mpa->space);
5960 mpa->space = space;
5961 return mpa;
5962 error:
5963 isl_space_free(space);
5964 isl_multi_pw_aff_free(mpa);
5965 isl_pw_multi_aff_free(pma);
5966 return NULL;
5969 /* Compute the pullback of "mpa" by the function represented by "pma".
5970 * In other words, plug in "pma" in "mpa".
5972 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
5973 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
5975 if (!mpa || !pma)
5976 goto error;
5977 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
5978 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
5979 mpa = isl_multi_pw_aff_align_params(mpa,
5980 isl_pw_multi_aff_get_space(pma));
5981 pma = isl_pw_multi_aff_align_params(pma,
5982 isl_multi_pw_aff_get_space(mpa));
5983 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
5984 error:
5985 isl_multi_pw_aff_free(mpa);
5986 isl_pw_multi_aff_free(pma);
5987 return NULL;
5990 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
5991 * In other words, plug in "mpa2" in "mpa1".
5993 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
5995 static __isl_give isl_multi_pw_aff *
5996 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
5997 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
5999 isl_pw_multi_aff *pma;
6001 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa2);
6002 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa1, pma);
6005 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6006 * In other words, plug in "mpa2" in "mpa1".
6008 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6009 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6011 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6012 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);