add isl_map_preimage_range_multi_aff
[isl.git] / isl_aff.c
blob3ff056c857cdbbe9410cf0c1209fe056f7b0e927
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 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 /* Replace the identifier of the input tuple of "aff" by "id".
1756 * type is currently required to be equal to isl_dim_in
1758 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1759 enum isl_dim_type type, __isl_take isl_id *id)
1761 aff = isl_aff_cow(aff);
1762 if (!aff)
1763 return isl_id_free(id);
1764 if (type != isl_dim_out)
1765 isl_die(aff->v->ctx, isl_error_invalid,
1766 "cannot only set id of input tuple", goto error);
1767 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1768 if (!aff->ls)
1769 return isl_aff_free(aff);
1771 return aff;
1772 error:
1773 isl_id_free(id);
1774 isl_aff_free(aff);
1775 return NULL;
1778 /* Exploit the equalities in "eq" to simplify the affine expression
1779 * and the expressions of the integer divisions in the local space.
1780 * The integer divisions in this local space are assumed to appear
1781 * as regular dimensions in "eq".
1783 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1784 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1786 int i, j;
1787 unsigned total;
1788 unsigned n_div;
1790 if (!eq)
1791 goto error;
1792 if (eq->n_eq == 0) {
1793 isl_basic_set_free(eq);
1794 return aff;
1797 aff = isl_aff_cow(aff);
1798 if (!aff)
1799 goto error;
1801 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1802 isl_basic_set_copy(eq));
1803 aff->v = isl_vec_cow(aff->v);
1804 if (!aff->ls || !aff->v)
1805 goto error;
1807 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1808 n_div = eq->n_div;
1809 for (i = 0; i < eq->n_eq; ++i) {
1810 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1811 if (j < 0 || j == 0 || j >= total)
1812 continue;
1814 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1815 &aff->v->el[0]);
1818 isl_basic_set_free(eq);
1819 aff = isl_aff_normalize(aff);
1820 return aff;
1821 error:
1822 isl_basic_set_free(eq);
1823 isl_aff_free(aff);
1824 return NULL;
1827 /* Exploit the equalities in "eq" to simplify the affine expression
1828 * and the expressions of the integer divisions in the local space.
1830 static __isl_give isl_aff *isl_aff_substitute_equalities(
1831 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1833 int n_div;
1835 if (!aff || !eq)
1836 goto error;
1837 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1838 if (n_div > 0)
1839 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1840 return isl_aff_substitute_equalities_lifted(aff, eq);
1841 error:
1842 isl_basic_set_free(eq);
1843 isl_aff_free(aff);
1844 return NULL;
1847 /* Look for equalities among the variables shared by context and aff
1848 * and the integer divisions of aff, if any.
1849 * The equalities are then used to eliminate coefficients and/or integer
1850 * divisions from aff.
1852 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1853 __isl_take isl_set *context)
1855 isl_basic_set *hull;
1856 int n_div;
1858 if (!aff)
1859 goto error;
1860 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1861 if (n_div > 0) {
1862 isl_basic_set *bset;
1863 isl_local_space *ls;
1864 context = isl_set_add_dims(context, isl_dim_set, n_div);
1865 ls = isl_aff_get_domain_local_space(aff);
1866 bset = isl_basic_set_from_local_space(ls);
1867 bset = isl_basic_set_lift(bset);
1868 bset = isl_basic_set_flatten(bset);
1869 context = isl_set_intersect(context,
1870 isl_set_from_basic_set(bset));
1873 hull = isl_set_affine_hull(context);
1874 return isl_aff_substitute_equalities_lifted(aff, hull);
1875 error:
1876 isl_aff_free(aff);
1877 isl_set_free(context);
1878 return NULL;
1881 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1882 __isl_take isl_set *context)
1884 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1885 dom_context = isl_set_intersect_params(dom_context, context);
1886 return isl_aff_gist(aff, dom_context);
1889 /* Return a basic set containing those elements in the space
1890 * of aff where it is non-negative.
1891 * If "rational" is set, then return a rational basic set.
1893 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1894 __isl_take isl_aff *aff, int rational)
1896 isl_constraint *ineq;
1897 isl_basic_set *bset;
1899 ineq = isl_inequality_from_aff(aff);
1901 bset = isl_basic_set_from_constraint(ineq);
1902 if (rational)
1903 bset = isl_basic_set_set_rational(bset);
1904 bset = isl_basic_set_simplify(bset);
1905 return bset;
1908 /* Return a basic set containing those elements in the space
1909 * of aff where it is non-negative.
1911 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1913 return aff_nonneg_basic_set(aff, 0);
1916 /* Return a basic set containing those elements in the domain space
1917 * of aff where it is negative.
1919 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1921 aff = isl_aff_neg(aff);
1922 aff = isl_aff_add_constant_num_si(aff, -1);
1923 return isl_aff_nonneg_basic_set(aff);
1926 /* Return a basic set containing those elements in the space
1927 * of aff where it is zero.
1928 * If "rational" is set, then return a rational basic set.
1930 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1931 int rational)
1933 isl_constraint *ineq;
1934 isl_basic_set *bset;
1936 ineq = isl_equality_from_aff(aff);
1938 bset = isl_basic_set_from_constraint(ineq);
1939 if (rational)
1940 bset = isl_basic_set_set_rational(bset);
1941 bset = isl_basic_set_simplify(bset);
1942 return bset;
1945 /* Return a basic set containing those elements in the space
1946 * of aff where it is zero.
1948 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1950 return aff_zero_basic_set(aff, 0);
1953 /* Return a basic set containing those elements in the shared space
1954 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1956 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1957 __isl_take isl_aff *aff2)
1959 aff1 = isl_aff_sub(aff1, aff2);
1961 return isl_aff_nonneg_basic_set(aff1);
1964 /* Return a basic set containing those elements in the shared space
1965 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1967 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1968 __isl_take isl_aff *aff2)
1970 return isl_aff_ge_basic_set(aff2, aff1);
1973 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1974 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1976 aff1 = isl_aff_add(aff1, aff2);
1977 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1978 return aff1;
1981 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1983 if (!aff)
1984 return -1;
1986 return 0;
1989 /* Check whether the given affine expression has non-zero coefficient
1990 * for any dimension in the given range or if any of these dimensions
1991 * appear with non-zero coefficients in any of the integer divisions
1992 * involved in the affine expression.
1994 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1995 enum isl_dim_type type, unsigned first, unsigned n)
1997 int i;
1998 isl_ctx *ctx;
1999 int *active = NULL;
2000 int involves = 0;
2002 if (!aff)
2003 return -1;
2004 if (n == 0)
2005 return 0;
2007 ctx = isl_aff_get_ctx(aff);
2008 if (first + n > isl_aff_dim(aff, type))
2009 isl_die(ctx, isl_error_invalid,
2010 "range out of bounds", return -1);
2012 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2013 if (!active)
2014 goto error;
2016 first += isl_local_space_offset(aff->ls, type) - 1;
2017 for (i = 0; i < n; ++i)
2018 if (active[first + i]) {
2019 involves = 1;
2020 break;
2023 free(active);
2025 return involves;
2026 error:
2027 free(active);
2028 return -1;
2031 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2032 enum isl_dim_type type, unsigned first, unsigned n)
2034 isl_ctx *ctx;
2036 if (!aff)
2037 return NULL;
2038 if (type == isl_dim_out)
2039 isl_die(aff->v->ctx, isl_error_invalid,
2040 "cannot drop output/set dimension",
2041 return isl_aff_free(aff));
2042 if (type == isl_dim_in)
2043 type = isl_dim_set;
2044 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2045 return aff;
2047 ctx = isl_aff_get_ctx(aff);
2048 if (first + n > isl_local_space_dim(aff->ls, type))
2049 isl_die(ctx, isl_error_invalid, "range out of bounds",
2050 return isl_aff_free(aff));
2052 aff = isl_aff_cow(aff);
2053 if (!aff)
2054 return NULL;
2056 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2057 if (!aff->ls)
2058 return isl_aff_free(aff);
2060 first += 1 + isl_local_space_offset(aff->ls, type);
2061 aff->v = isl_vec_drop_els(aff->v, first, n);
2062 if (!aff->v)
2063 return isl_aff_free(aff);
2065 return aff;
2068 /* Project the domain of the affine expression onto its parameter space.
2069 * The affine expression may not involve any of the domain dimensions.
2071 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2073 isl_space *space;
2074 unsigned n;
2075 int involves;
2077 n = isl_aff_dim(aff, isl_dim_in);
2078 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2079 if (involves < 0)
2080 return isl_aff_free(aff);
2081 if (involves)
2082 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2083 "affine expression involves some of the domain dimensions",
2084 return isl_aff_free(aff));
2085 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2086 space = isl_aff_get_domain_space(aff);
2087 space = isl_space_params(space);
2088 aff = isl_aff_reset_domain_space(aff, space);
2089 return aff;
2092 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2093 enum isl_dim_type type, unsigned first, unsigned n)
2095 isl_ctx *ctx;
2097 if (!aff)
2098 return NULL;
2099 if (type == isl_dim_out)
2100 isl_die(aff->v->ctx, isl_error_invalid,
2101 "cannot insert output/set dimensions",
2102 return isl_aff_free(aff));
2103 if (type == isl_dim_in)
2104 type = isl_dim_set;
2105 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2106 return aff;
2108 ctx = isl_aff_get_ctx(aff);
2109 if (first > isl_local_space_dim(aff->ls, type))
2110 isl_die(ctx, isl_error_invalid, "position out of bounds",
2111 return isl_aff_free(aff));
2113 aff = isl_aff_cow(aff);
2114 if (!aff)
2115 return NULL;
2117 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2118 if (!aff->ls)
2119 return isl_aff_free(aff);
2121 first += 1 + isl_local_space_offset(aff->ls, type);
2122 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2123 if (!aff->v)
2124 return isl_aff_free(aff);
2126 return aff;
2129 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2130 enum isl_dim_type type, unsigned n)
2132 unsigned pos;
2134 pos = isl_aff_dim(aff, type);
2136 return isl_aff_insert_dims(aff, type, pos, n);
2139 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2140 enum isl_dim_type type, unsigned n)
2142 unsigned pos;
2144 pos = isl_pw_aff_dim(pwaff, type);
2146 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2149 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2150 * to dimensions of "dst_type" at "dst_pos".
2152 * We only support moving input dimensions to parameters and vice versa.
2154 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2155 enum isl_dim_type dst_type, unsigned dst_pos,
2156 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2158 unsigned g_dst_pos;
2159 unsigned g_src_pos;
2161 if (!aff)
2162 return NULL;
2163 if (n == 0 &&
2164 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2165 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2166 return aff;
2168 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2169 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2170 "cannot move output/set dimension", isl_aff_free(aff));
2171 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2172 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2173 "cannot move divs", isl_aff_free(aff));
2174 if (dst_type == isl_dim_in)
2175 dst_type = isl_dim_set;
2176 if (src_type == isl_dim_in)
2177 src_type = isl_dim_set;
2179 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2181 "range out of bounds", isl_aff_free(aff));
2182 if (dst_type == src_type)
2183 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2184 "moving dims within the same type not supported",
2185 isl_aff_free(aff));
2187 aff = isl_aff_cow(aff);
2188 if (!aff)
2189 return NULL;
2191 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2192 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2193 if (dst_type > src_type)
2194 g_dst_pos -= n;
2196 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2197 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2198 src_type, src_pos, n);
2199 if (!aff->v || !aff->ls)
2200 return isl_aff_free(aff);
2202 aff = sort_divs(aff);
2204 return aff;
2207 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2209 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2210 return isl_pw_aff_alloc(dom, aff);
2213 #undef PW
2214 #define PW isl_pw_aff
2215 #undef EL
2216 #define EL isl_aff
2217 #undef EL_IS_ZERO
2218 #define EL_IS_ZERO is_empty
2219 #undef ZERO
2220 #define ZERO empty
2221 #undef IS_ZERO
2222 #define IS_ZERO is_empty
2223 #undef FIELD
2224 #define FIELD aff
2225 #undef DEFAULT_IS_ZERO
2226 #define DEFAULT_IS_ZERO 0
2228 #define NO_EVAL
2229 #define NO_OPT
2230 #define NO_LIFT
2231 #define NO_MORPH
2233 #include <isl_pw_templ.c>
2235 static __isl_give isl_set *align_params_pw_pw_set_and(
2236 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2237 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2238 __isl_take isl_pw_aff *pwaff2))
2240 if (!pwaff1 || !pwaff2)
2241 goto error;
2242 if (isl_space_match(pwaff1->dim, isl_dim_param,
2243 pwaff2->dim, isl_dim_param))
2244 return fn(pwaff1, pwaff2);
2245 if (!isl_space_has_named_params(pwaff1->dim) ||
2246 !isl_space_has_named_params(pwaff2->dim))
2247 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2248 "unaligned unnamed parameters", goto error);
2249 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2250 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2251 return fn(pwaff1, pwaff2);
2252 error:
2253 isl_pw_aff_free(pwaff1);
2254 isl_pw_aff_free(pwaff2);
2255 return NULL;
2258 /* Compute a piecewise quasi-affine expression with a domain that
2259 * is the union of those of pwaff1 and pwaff2 and such that on each
2260 * cell, the quasi-affine expression is the better (according to cmp)
2261 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2262 * is defined on a given cell, then the associated expression
2263 * is the defined one.
2265 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2266 __isl_take isl_pw_aff *pwaff2,
2267 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2268 __isl_take isl_aff *aff2))
2270 int i, j, n;
2271 isl_pw_aff *res;
2272 isl_ctx *ctx;
2273 isl_set *set;
2275 if (!pwaff1 || !pwaff2)
2276 goto error;
2278 ctx = isl_space_get_ctx(pwaff1->dim);
2279 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2280 isl_die(ctx, isl_error_invalid,
2281 "arguments should live in same space", goto error);
2283 if (isl_pw_aff_is_empty(pwaff1)) {
2284 isl_pw_aff_free(pwaff1);
2285 return pwaff2;
2288 if (isl_pw_aff_is_empty(pwaff2)) {
2289 isl_pw_aff_free(pwaff2);
2290 return pwaff1;
2293 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2294 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2296 for (i = 0; i < pwaff1->n; ++i) {
2297 set = isl_set_copy(pwaff1->p[i].set);
2298 for (j = 0; j < pwaff2->n; ++j) {
2299 struct isl_set *common;
2300 isl_set *better;
2302 common = isl_set_intersect(
2303 isl_set_copy(pwaff1->p[i].set),
2304 isl_set_copy(pwaff2->p[j].set));
2305 better = isl_set_from_basic_set(cmp(
2306 isl_aff_copy(pwaff2->p[j].aff),
2307 isl_aff_copy(pwaff1->p[i].aff)));
2308 better = isl_set_intersect(common, better);
2309 if (isl_set_plain_is_empty(better)) {
2310 isl_set_free(better);
2311 continue;
2313 set = isl_set_subtract(set, isl_set_copy(better));
2315 res = isl_pw_aff_add_piece(res, better,
2316 isl_aff_copy(pwaff2->p[j].aff));
2318 res = isl_pw_aff_add_piece(res, set,
2319 isl_aff_copy(pwaff1->p[i].aff));
2322 for (j = 0; j < pwaff2->n; ++j) {
2323 set = isl_set_copy(pwaff2->p[j].set);
2324 for (i = 0; i < pwaff1->n; ++i)
2325 set = isl_set_subtract(set,
2326 isl_set_copy(pwaff1->p[i].set));
2327 res = isl_pw_aff_add_piece(res, set,
2328 isl_aff_copy(pwaff2->p[j].aff));
2331 isl_pw_aff_free(pwaff1);
2332 isl_pw_aff_free(pwaff2);
2334 return res;
2335 error:
2336 isl_pw_aff_free(pwaff1);
2337 isl_pw_aff_free(pwaff2);
2338 return NULL;
2341 /* Compute a piecewise quasi-affine expression with a domain that
2342 * is the union of those of pwaff1 and pwaff2 and such that on each
2343 * cell, the quasi-affine expression is the maximum of those of pwaff1
2344 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2345 * cell, then the associated expression is the defined one.
2347 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2348 __isl_take isl_pw_aff *pwaff2)
2350 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2353 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2354 __isl_take isl_pw_aff *pwaff2)
2356 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2357 &pw_aff_union_max);
2360 /* Compute a piecewise quasi-affine expression with a domain that
2361 * is the union of those of pwaff1 and pwaff2 and such that on each
2362 * cell, the quasi-affine expression is the minimum of those of pwaff1
2363 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2364 * cell, then the associated expression is the defined one.
2366 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2367 __isl_take isl_pw_aff *pwaff2)
2369 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2372 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2373 __isl_take isl_pw_aff *pwaff2)
2375 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2376 &pw_aff_union_min);
2379 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2380 __isl_take isl_pw_aff *pwaff2, int max)
2382 if (max)
2383 return isl_pw_aff_union_max(pwaff1, pwaff2);
2384 else
2385 return isl_pw_aff_union_min(pwaff1, pwaff2);
2388 /* Construct a map with as domain the domain of pwaff and
2389 * one-dimensional range corresponding to the affine expressions.
2391 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2393 int i;
2394 isl_space *dim;
2395 isl_map *map;
2397 if (!pwaff)
2398 return NULL;
2400 dim = isl_pw_aff_get_space(pwaff);
2401 map = isl_map_empty(dim);
2403 for (i = 0; i < pwaff->n; ++i) {
2404 isl_basic_map *bmap;
2405 isl_map *map_i;
2407 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2408 map_i = isl_map_from_basic_map(bmap);
2409 map_i = isl_map_intersect_domain(map_i,
2410 isl_set_copy(pwaff->p[i].set));
2411 map = isl_map_union_disjoint(map, map_i);
2414 isl_pw_aff_free(pwaff);
2416 return map;
2419 /* Construct a map with as domain the domain of pwaff and
2420 * one-dimensional range corresponding to the affine expressions.
2422 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2424 if (!pwaff)
2425 return NULL;
2426 if (isl_space_is_set(pwaff->dim))
2427 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2428 "space of input is not a map",
2429 return isl_pw_aff_free(pwaff));
2430 return map_from_pw_aff(pwaff);
2433 /* Construct a one-dimensional set with as parameter domain
2434 * the domain of pwaff and the single set dimension
2435 * corresponding to the affine expressions.
2437 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2439 if (!pwaff)
2440 return NULL;
2441 if (!isl_space_is_set(pwaff->dim))
2442 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2443 "space of input is not a set",
2444 return isl_pw_aff_free(pwaff));
2445 return map_from_pw_aff(pwaff);
2448 /* Return a set containing those elements in the domain
2449 * of pwaff where it is non-negative.
2451 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2453 int i;
2454 isl_set *set;
2456 if (!pwaff)
2457 return NULL;
2459 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2461 for (i = 0; i < pwaff->n; ++i) {
2462 isl_basic_set *bset;
2463 isl_set *set_i;
2464 int rational;
2466 rational = isl_set_has_rational(pwaff->p[i].set);
2467 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2468 rational);
2469 set_i = isl_set_from_basic_set(bset);
2470 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2471 set = isl_set_union_disjoint(set, set_i);
2474 isl_pw_aff_free(pwaff);
2476 return set;
2479 /* Return a set containing those elements in the domain
2480 * of pwaff where it is zero (if complement is 0) or not zero
2481 * (if complement is 1).
2483 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2484 int complement)
2486 int i;
2487 isl_set *set;
2489 if (!pwaff)
2490 return NULL;
2492 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2494 for (i = 0; i < pwaff->n; ++i) {
2495 isl_basic_set *bset;
2496 isl_set *set_i, *zero;
2497 int rational;
2499 rational = isl_set_has_rational(pwaff->p[i].set);
2500 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2501 rational);
2502 zero = isl_set_from_basic_set(bset);
2503 set_i = isl_set_copy(pwaff->p[i].set);
2504 if (complement)
2505 set_i = isl_set_subtract(set_i, zero);
2506 else
2507 set_i = isl_set_intersect(set_i, zero);
2508 set = isl_set_union_disjoint(set, set_i);
2511 isl_pw_aff_free(pwaff);
2513 return set;
2516 /* Return a set containing those elements in the domain
2517 * of pwaff where it is zero.
2519 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2521 return pw_aff_zero_set(pwaff, 0);
2524 /* Return a set containing those elements in the domain
2525 * of pwaff where it is not zero.
2527 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2529 return pw_aff_zero_set(pwaff, 1);
2532 /* Return a set containing those elements in the shared domain
2533 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2535 * We compute the difference on the shared domain and then construct
2536 * the set of values where this difference is non-negative.
2537 * If strict is set, we first subtract 1 from the difference.
2538 * If equal is set, we only return the elements where pwaff1 and pwaff2
2539 * are equal.
2541 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2542 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2544 isl_set *set1, *set2;
2546 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2547 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2548 set1 = isl_set_intersect(set1, set2);
2549 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2550 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2551 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2553 if (strict) {
2554 isl_space *dim = isl_set_get_space(set1);
2555 isl_aff *aff;
2556 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2557 aff = isl_aff_add_constant_si(aff, -1);
2558 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2559 } else
2560 isl_set_free(set1);
2562 if (equal)
2563 return isl_pw_aff_zero_set(pwaff1);
2564 return isl_pw_aff_nonneg_set(pwaff1);
2567 /* Return a set containing those elements in the shared domain
2568 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2570 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2571 __isl_take isl_pw_aff *pwaff2)
2573 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2576 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2577 __isl_take isl_pw_aff *pwaff2)
2579 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2582 /* Return a set containing those elements in the shared domain
2583 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2585 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2586 __isl_take isl_pw_aff *pwaff2)
2588 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2591 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2592 __isl_take isl_pw_aff *pwaff2)
2594 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2597 /* Return a set containing those elements in the shared domain
2598 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2600 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2601 __isl_take isl_pw_aff *pwaff2)
2603 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2606 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2607 __isl_take isl_pw_aff *pwaff2)
2609 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2612 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2613 __isl_take isl_pw_aff *pwaff2)
2615 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2618 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2619 __isl_take isl_pw_aff *pwaff2)
2621 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2624 /* Return a set containing those elements in the shared domain
2625 * of the elements of list1 and list2 where each element in list1
2626 * has the relation specified by "fn" with each element in list2.
2628 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2629 __isl_take isl_pw_aff_list *list2,
2630 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2631 __isl_take isl_pw_aff *pwaff2))
2633 int i, j;
2634 isl_ctx *ctx;
2635 isl_set *set;
2637 if (!list1 || !list2)
2638 goto error;
2640 ctx = isl_pw_aff_list_get_ctx(list1);
2641 if (list1->n < 1 || list2->n < 1)
2642 isl_die(ctx, isl_error_invalid,
2643 "list should contain at least one element", goto error);
2645 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2646 for (i = 0; i < list1->n; ++i)
2647 for (j = 0; j < list2->n; ++j) {
2648 isl_set *set_ij;
2650 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2651 isl_pw_aff_copy(list2->p[j]));
2652 set = isl_set_intersect(set, set_ij);
2655 isl_pw_aff_list_free(list1);
2656 isl_pw_aff_list_free(list2);
2657 return set;
2658 error:
2659 isl_pw_aff_list_free(list1);
2660 isl_pw_aff_list_free(list2);
2661 return NULL;
2664 /* Return a set containing those elements in the shared domain
2665 * of the elements of list1 and list2 where each element in list1
2666 * is equal to each element in list2.
2668 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2669 __isl_take isl_pw_aff_list *list2)
2671 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2674 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2675 __isl_take isl_pw_aff_list *list2)
2677 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2680 /* Return a set containing those elements in the shared domain
2681 * of the elements of list1 and list2 where each element in list1
2682 * is less than or equal to each element in list2.
2684 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2685 __isl_take isl_pw_aff_list *list2)
2687 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2690 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2691 __isl_take isl_pw_aff_list *list2)
2693 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2696 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2697 __isl_take isl_pw_aff_list *list2)
2699 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2702 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2703 __isl_take isl_pw_aff_list *list2)
2705 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2709 /* Return a set containing those elements in the shared domain
2710 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2712 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2713 __isl_take isl_pw_aff *pwaff2)
2715 isl_set *set_lt, *set_gt;
2717 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2718 isl_pw_aff_copy(pwaff2));
2719 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2720 return isl_set_union_disjoint(set_lt, set_gt);
2723 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2724 __isl_take isl_pw_aff *pwaff2)
2726 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2729 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2730 isl_int v)
2732 int i;
2734 if (isl_int_is_one(v))
2735 return pwaff;
2736 if (!isl_int_is_pos(v))
2737 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2738 "factor needs to be positive",
2739 return isl_pw_aff_free(pwaff));
2740 pwaff = isl_pw_aff_cow(pwaff);
2741 if (!pwaff)
2742 return NULL;
2743 if (pwaff->n == 0)
2744 return pwaff;
2746 for (i = 0; i < pwaff->n; ++i) {
2747 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2748 if (!pwaff->p[i].aff)
2749 return isl_pw_aff_free(pwaff);
2752 return pwaff;
2755 /* Divide "pa" by "f".
2757 __isl_give isl_pw_aff *isl_pw_aff_scale_down_val(__isl_take isl_pw_aff *pa,
2758 __isl_take isl_val *f)
2760 int i;
2762 if (!pa || !f)
2763 goto error;
2765 if (isl_val_is_one(f)) {
2766 isl_val_free(f);
2767 return pa;
2770 if (!isl_val_is_rat(f))
2771 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2772 "expecting rational factor", goto error);
2773 if (!isl_val_is_pos(f))
2774 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2775 "factor needs to be positive", goto error);
2777 pa = isl_pw_aff_cow(pa);
2778 if (!pa)
2779 return NULL;
2780 if (pa->n == 0)
2781 return pa;
2783 for (i = 0; i < pa->n; ++i) {
2784 pa->p[i].aff = isl_aff_scale_down_val(pa->p[i].aff,
2785 isl_val_copy(f));
2786 if (!pa->p[i].aff)
2787 goto error;
2790 isl_val_free(f);
2791 return pa;
2792 error:
2793 isl_pw_aff_free(pa);
2794 isl_val_free(f);
2795 return NULL;
2798 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2800 int i;
2802 pwaff = isl_pw_aff_cow(pwaff);
2803 if (!pwaff)
2804 return NULL;
2805 if (pwaff->n == 0)
2806 return pwaff;
2808 for (i = 0; i < pwaff->n; ++i) {
2809 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2810 if (!pwaff->p[i].aff)
2811 return isl_pw_aff_free(pwaff);
2814 return pwaff;
2817 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2819 int i;
2821 pwaff = isl_pw_aff_cow(pwaff);
2822 if (!pwaff)
2823 return NULL;
2824 if (pwaff->n == 0)
2825 return pwaff;
2827 for (i = 0; i < pwaff->n; ++i) {
2828 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2829 if (!pwaff->p[i].aff)
2830 return isl_pw_aff_free(pwaff);
2833 return pwaff;
2836 /* Assuming that "cond1" and "cond2" are disjoint,
2837 * return an affine expression that is equal to pwaff1 on cond1
2838 * and to pwaff2 on cond2.
2840 static __isl_give isl_pw_aff *isl_pw_aff_select(
2841 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2842 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2844 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2845 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2847 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2850 /* Return an affine expression that is equal to pwaff_true for elements
2851 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2852 * is zero.
2853 * That is, return cond ? pwaff_true : pwaff_false;
2855 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2856 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2858 isl_set *cond_true, *cond_false;
2860 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2861 cond_false = isl_pw_aff_zero_set(cond);
2862 return isl_pw_aff_select(cond_true, pwaff_true,
2863 cond_false, pwaff_false);
2866 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2868 if (!aff)
2869 return -1;
2871 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2874 /* Check whether pwaff is a piecewise constant.
2876 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2878 int i;
2880 if (!pwaff)
2881 return -1;
2883 for (i = 0; i < pwaff->n; ++i) {
2884 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2885 if (is_cst < 0 || !is_cst)
2886 return is_cst;
2889 return 1;
2892 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2893 __isl_take isl_aff *aff2)
2895 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2896 return isl_aff_mul(aff2, aff1);
2898 if (!isl_aff_is_cst(aff2))
2899 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2900 "at least one affine expression should be constant",
2901 goto error);
2903 aff1 = isl_aff_cow(aff1);
2904 if (!aff1 || !aff2)
2905 goto error;
2907 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2908 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2910 isl_aff_free(aff2);
2911 return aff1;
2912 error:
2913 isl_aff_free(aff1);
2914 isl_aff_free(aff2);
2915 return NULL;
2918 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2920 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2921 __isl_take isl_aff *aff2)
2923 int is_cst;
2924 int neg;
2926 is_cst = isl_aff_is_cst(aff2);
2927 if (is_cst < 0)
2928 goto error;
2929 if (!is_cst)
2930 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2931 "second argument should be a constant", goto error);
2933 if (!aff2)
2934 goto error;
2936 neg = isl_int_is_neg(aff2->v->el[1]);
2937 if (neg) {
2938 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2939 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2942 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2943 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2945 if (neg) {
2946 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2947 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2950 isl_aff_free(aff2);
2951 return aff1;
2952 error:
2953 isl_aff_free(aff1);
2954 isl_aff_free(aff2);
2955 return NULL;
2958 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2959 __isl_take isl_pw_aff *pwaff2)
2961 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2964 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2965 __isl_take isl_pw_aff *pwaff2)
2967 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2970 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2971 __isl_take isl_pw_aff *pwaff2)
2973 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2976 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2979 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2982 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2983 __isl_take isl_pw_aff *pwaff2)
2985 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2988 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2989 __isl_take isl_pw_aff *pa2)
2991 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2994 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2996 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2997 __isl_take isl_pw_aff *pa2)
2999 int is_cst;
3001 is_cst = isl_pw_aff_is_cst(pa2);
3002 if (is_cst < 0)
3003 goto error;
3004 if (!is_cst)
3005 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3006 "second argument should be a piecewise constant",
3007 goto error);
3008 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3009 error:
3010 isl_pw_aff_free(pa1);
3011 isl_pw_aff_free(pa2);
3012 return NULL;
3015 /* Compute the quotient of the integer division of "pa1" by "pa2"
3016 * with rounding towards zero.
3017 * "pa2" is assumed to be a piecewise constant.
3019 * In particular, return
3021 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3024 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3025 __isl_take isl_pw_aff *pa2)
3027 int is_cst;
3028 isl_set *cond;
3029 isl_pw_aff *f, *c;
3031 is_cst = isl_pw_aff_is_cst(pa2);
3032 if (is_cst < 0)
3033 goto error;
3034 if (!is_cst)
3035 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3036 "second argument should be a piecewise constant",
3037 goto error);
3039 pa1 = isl_pw_aff_div(pa1, pa2);
3041 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3042 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3043 c = isl_pw_aff_ceil(pa1);
3044 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3045 error:
3046 isl_pw_aff_free(pa1);
3047 isl_pw_aff_free(pa2);
3048 return NULL;
3051 /* Compute the remainder of the integer division of "pa1" by "pa2"
3052 * with rounding towards zero.
3053 * "pa2" is assumed to be a piecewise constant.
3055 * In particular, return
3057 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3060 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3061 __isl_take isl_pw_aff *pa2)
3063 int is_cst;
3064 isl_pw_aff *res;
3066 is_cst = isl_pw_aff_is_cst(pa2);
3067 if (is_cst < 0)
3068 goto error;
3069 if (!is_cst)
3070 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3071 "second argument should be a piecewise constant",
3072 goto error);
3073 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3074 res = isl_pw_aff_mul(pa2, res);
3075 res = isl_pw_aff_sub(pa1, res);
3076 return res;
3077 error:
3078 isl_pw_aff_free(pa1);
3079 isl_pw_aff_free(pa2);
3080 return NULL;
3083 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3084 __isl_take isl_pw_aff *pwaff2)
3086 isl_set *le;
3087 isl_set *dom;
3089 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3090 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3091 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3092 isl_pw_aff_copy(pwaff2));
3093 dom = isl_set_subtract(dom, isl_set_copy(le));
3094 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3097 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3098 __isl_take isl_pw_aff *pwaff2)
3100 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3103 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3104 __isl_take isl_pw_aff *pwaff2)
3106 isl_set *ge;
3107 isl_set *dom;
3109 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3110 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3111 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3112 isl_pw_aff_copy(pwaff2));
3113 dom = isl_set_subtract(dom, isl_set_copy(ge));
3114 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3117 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3118 __isl_take isl_pw_aff *pwaff2)
3120 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3123 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3124 __isl_take isl_pw_aff_list *list,
3125 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3126 __isl_take isl_pw_aff *pwaff2))
3128 int i;
3129 isl_ctx *ctx;
3130 isl_pw_aff *res;
3132 if (!list)
3133 return NULL;
3135 ctx = isl_pw_aff_list_get_ctx(list);
3136 if (list->n < 1)
3137 isl_die(ctx, isl_error_invalid,
3138 "list should contain at least one element",
3139 return isl_pw_aff_list_free(list));
3141 res = isl_pw_aff_copy(list->p[0]);
3142 for (i = 1; i < list->n; ++i)
3143 res = fn(res, isl_pw_aff_copy(list->p[i]));
3145 isl_pw_aff_list_free(list);
3146 return res;
3149 /* Return an isl_pw_aff that maps each element in the intersection of the
3150 * domains of the elements of list to the minimal corresponding affine
3151 * expression.
3153 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3155 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3158 /* Return an isl_pw_aff that maps each element in the intersection of the
3159 * domains of the elements of list to the maximal corresponding affine
3160 * expression.
3162 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3164 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3167 /* Mark the domains of "pwaff" as rational.
3169 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3171 int i;
3173 pwaff = isl_pw_aff_cow(pwaff);
3174 if (!pwaff)
3175 return NULL;
3176 if (pwaff->n == 0)
3177 return pwaff;
3179 for (i = 0; i < pwaff->n; ++i) {
3180 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3181 if (!pwaff->p[i].set)
3182 return isl_pw_aff_free(pwaff);
3185 return pwaff;
3188 /* Mark the domains of the elements of "list" as rational.
3190 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3191 __isl_take isl_pw_aff_list *list)
3193 int i, n;
3195 if (!list)
3196 return NULL;
3197 if (list->n == 0)
3198 return list;
3200 n = list->n;
3201 for (i = 0; i < n; ++i) {
3202 isl_pw_aff *pa;
3204 pa = isl_pw_aff_list_get_pw_aff(list, i);
3205 pa = isl_pw_aff_set_rational(pa);
3206 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3209 return list;
3212 /* Do the parameters of "aff" match those of "space"?
3214 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3215 __isl_keep isl_space *space)
3217 isl_space *aff_space;
3218 int match;
3220 if (!aff || !space)
3221 return -1;
3223 aff_space = isl_aff_get_domain_space(aff);
3225 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3227 isl_space_free(aff_space);
3228 return match;
3231 /* Check that the domain space of "aff" matches "space".
3233 * Return 0 on success and -1 on error.
3235 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3236 __isl_keep isl_space *space)
3238 isl_space *aff_space;
3239 int match;
3241 if (!aff || !space)
3242 return -1;
3244 aff_space = isl_aff_get_domain_space(aff);
3246 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3247 if (match < 0)
3248 goto error;
3249 if (!match)
3250 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3251 "parameters don't match", goto error);
3252 match = isl_space_tuple_match(space, isl_dim_in,
3253 aff_space, isl_dim_set);
3254 if (match < 0)
3255 goto error;
3256 if (!match)
3257 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3258 "domains don't match", goto error);
3259 isl_space_free(aff_space);
3260 return 0;
3261 error:
3262 isl_space_free(aff_space);
3263 return -1;
3266 #undef BASE
3267 #define BASE aff
3268 #define NO_INTERSECT_DOMAIN
3269 #define NO_DOMAIN
3271 #include <isl_multi_templ.c>
3273 #undef NO_DOMAIN
3274 #undef NO_INTERSECT_DOMAIN
3276 /* Remove any internal structure of the domain of "ma".
3277 * If there is any such internal structure in the input,
3278 * then the name of the corresponding space is also removed.
3280 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3281 __isl_take isl_multi_aff *ma)
3283 isl_space *space;
3285 if (!ma)
3286 return NULL;
3288 if (!ma->space->nested[0])
3289 return ma;
3291 space = isl_multi_aff_get_space(ma);
3292 space = isl_space_flatten_domain(space);
3293 ma = isl_multi_aff_reset_space(ma, space);
3295 return ma;
3298 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3299 * of the space to its domain.
3301 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3303 int i, n_in;
3304 isl_local_space *ls;
3305 isl_multi_aff *ma;
3307 if (!space)
3308 return NULL;
3309 if (!isl_space_is_map(space))
3310 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3311 "not a map space", goto error);
3313 n_in = isl_space_dim(space, isl_dim_in);
3314 space = isl_space_domain_map(space);
3316 ma = isl_multi_aff_alloc(isl_space_copy(space));
3317 if (n_in == 0) {
3318 isl_space_free(space);
3319 return ma;
3322 space = isl_space_domain(space);
3323 ls = isl_local_space_from_space(space);
3324 for (i = 0; i < n_in; ++i) {
3325 isl_aff *aff;
3327 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3328 isl_dim_set, i);
3329 ma = isl_multi_aff_set_aff(ma, i, aff);
3331 isl_local_space_free(ls);
3332 return ma;
3333 error:
3334 isl_space_free(space);
3335 return NULL;
3338 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3339 * of the space to its range.
3341 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3343 int i, n_in, n_out;
3344 isl_local_space *ls;
3345 isl_multi_aff *ma;
3347 if (!space)
3348 return NULL;
3349 if (!isl_space_is_map(space))
3350 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3351 "not a map space", goto error);
3353 n_in = isl_space_dim(space, isl_dim_in);
3354 n_out = isl_space_dim(space, isl_dim_out);
3355 space = isl_space_range_map(space);
3357 ma = isl_multi_aff_alloc(isl_space_copy(space));
3358 if (n_out == 0) {
3359 isl_space_free(space);
3360 return ma;
3363 space = isl_space_domain(space);
3364 ls = isl_local_space_from_space(space);
3365 for (i = 0; i < n_out; ++i) {
3366 isl_aff *aff;
3368 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3369 isl_dim_set, n_in + i);
3370 ma = isl_multi_aff_set_aff(ma, i, aff);
3372 isl_local_space_free(ls);
3373 return ma;
3374 error:
3375 isl_space_free(space);
3376 return NULL;
3379 /* Given the space of a set and a range of set dimensions,
3380 * construct an isl_multi_aff that projects out those dimensions.
3382 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3383 __isl_take isl_space *space, enum isl_dim_type type,
3384 unsigned first, unsigned n)
3386 int i, dim;
3387 isl_local_space *ls;
3388 isl_multi_aff *ma;
3390 if (!space)
3391 return NULL;
3392 if (!isl_space_is_set(space))
3393 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3394 "expecting set space", goto error);
3395 if (type != isl_dim_set)
3396 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3397 "only set dimensions can be projected out", goto error);
3399 dim = isl_space_dim(space, isl_dim_set);
3400 if (first + n > dim)
3401 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3402 "range out of bounds", goto error);
3404 space = isl_space_from_domain(space);
3405 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3407 if (dim == n)
3408 return isl_multi_aff_alloc(space);
3410 ma = isl_multi_aff_alloc(isl_space_copy(space));
3411 space = isl_space_domain(space);
3412 ls = isl_local_space_from_space(space);
3414 for (i = 0; i < first; ++i) {
3415 isl_aff *aff;
3417 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3418 isl_dim_set, i);
3419 ma = isl_multi_aff_set_aff(ma, i, aff);
3422 for (i = 0; i < dim - (first + n); ++i) {
3423 isl_aff *aff;
3425 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3426 isl_dim_set, first + n + i);
3427 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3430 isl_local_space_free(ls);
3431 return ma;
3432 error:
3433 isl_space_free(space);
3434 return NULL;
3437 /* Given the space of a set and a range of set dimensions,
3438 * construct an isl_pw_multi_aff that projects out those dimensions.
3440 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3441 __isl_take isl_space *space, enum isl_dim_type type,
3442 unsigned first, unsigned n)
3444 isl_multi_aff *ma;
3446 ma = isl_multi_aff_project_out_map(space, type, first, n);
3447 return isl_pw_multi_aff_from_multi_aff(ma);
3450 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3451 * domain.
3453 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3454 __isl_take isl_multi_aff *ma)
3456 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3457 return isl_pw_multi_aff_alloc(dom, ma);
3460 /* Create a piecewise multi-affine expression in the given space that maps each
3461 * input dimension to the corresponding output dimension.
3463 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3464 __isl_take isl_space *space)
3466 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3469 /* Add "ma2" to "ma1" and return the result.
3471 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3473 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3474 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3476 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3479 /* Add "ma2" to "ma1" and return the result.
3481 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3482 __isl_take isl_multi_aff *ma2)
3484 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3485 &isl_multi_aff_add_aligned);
3488 /* Subtract "ma2" from "ma1" and return the result.
3490 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3492 static __isl_give isl_multi_aff *isl_multi_aff_sub_aligned(
3493 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3495 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3498 /* Subtract "ma2" from "ma1" and return the result.
3500 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3501 __isl_take isl_multi_aff *ma2)
3503 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3504 &isl_multi_aff_sub_aligned);
3507 /* Exploit the equalities in "eq" to simplify the affine expressions.
3509 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3510 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3512 int i;
3514 maff = isl_multi_aff_cow(maff);
3515 if (!maff || !eq)
3516 goto error;
3518 for (i = 0; i < maff->n; ++i) {
3519 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3520 isl_basic_set_copy(eq));
3521 if (!maff->p[i])
3522 goto error;
3525 isl_basic_set_free(eq);
3526 return maff;
3527 error:
3528 isl_basic_set_free(eq);
3529 isl_multi_aff_free(maff);
3530 return NULL;
3533 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3534 isl_int f)
3536 int i;
3538 maff = isl_multi_aff_cow(maff);
3539 if (!maff)
3540 return NULL;
3542 for (i = 0; i < maff->n; ++i) {
3543 maff->p[i] = isl_aff_scale(maff->p[i], f);
3544 if (!maff->p[i])
3545 return isl_multi_aff_free(maff);
3548 return maff;
3551 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3552 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3554 maff1 = isl_multi_aff_add(maff1, maff2);
3555 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3556 return maff1;
3559 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3561 if (!maff)
3562 return -1;
3564 return 0;
3567 /* Return the set of domain elements where "ma1" is lexicographically
3568 * smaller than or equal to "ma2".
3570 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3571 __isl_take isl_multi_aff *ma2)
3573 return isl_multi_aff_lex_ge_set(ma2, ma1);
3576 /* Return the set of domain elements where "ma1" is lexicographically
3577 * greater than or equal to "ma2".
3579 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3580 __isl_take isl_multi_aff *ma2)
3582 isl_space *space;
3583 isl_map *map1, *map2;
3584 isl_map *map, *ge;
3586 map1 = isl_map_from_multi_aff(ma1);
3587 map2 = isl_map_from_multi_aff(ma2);
3588 map = isl_map_range_product(map1, map2);
3589 space = isl_space_range(isl_map_get_space(map));
3590 space = isl_space_domain(isl_space_unwrap(space));
3591 ge = isl_map_lex_ge(space);
3592 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3594 return isl_map_domain(map);
3597 #undef PW
3598 #define PW isl_pw_multi_aff
3599 #undef EL
3600 #define EL isl_multi_aff
3601 #undef EL_IS_ZERO
3602 #define EL_IS_ZERO is_empty
3603 #undef ZERO
3604 #define ZERO empty
3605 #undef IS_ZERO
3606 #define IS_ZERO is_empty
3607 #undef FIELD
3608 #define FIELD maff
3609 #undef DEFAULT_IS_ZERO
3610 #define DEFAULT_IS_ZERO 0
3612 #define NO_NEG
3613 #define NO_EVAL
3614 #define NO_OPT
3615 #define NO_INVOLVES_DIMS
3616 #define NO_INSERT_DIMS
3617 #define NO_LIFT
3618 #define NO_MORPH
3620 #include <isl_pw_templ.c>
3622 #undef UNION
3623 #define UNION isl_union_pw_multi_aff
3624 #undef PART
3625 #define PART isl_pw_multi_aff
3626 #undef PARTS
3627 #define PARTS pw_multi_aff
3628 #define ALIGN_DOMAIN
3630 #define NO_EVAL
3632 #include <isl_union_templ.c>
3634 /* Given a function "cmp" that returns the set of elements where
3635 * "ma1" is "better" than "ma2", return the intersection of this
3636 * set with "dom1" and "dom2".
3638 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3639 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3640 __isl_keep isl_multi_aff *ma2,
3641 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3642 __isl_take isl_multi_aff *ma2))
3644 isl_set *common;
3645 isl_set *better;
3646 int is_empty;
3648 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3649 is_empty = isl_set_plain_is_empty(common);
3650 if (is_empty >= 0 && is_empty)
3651 return common;
3652 if (is_empty < 0)
3653 return isl_set_free(common);
3654 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3655 better = isl_set_intersect(common, better);
3657 return better;
3660 /* Given a function "cmp" that returns the set of elements where
3661 * "ma1" is "better" than "ma2", return a piecewise multi affine
3662 * expression defined on the union of the definition domains
3663 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3664 * "pma2" on each cell. If only one of the two input functions
3665 * is defined on a given cell, then it is considered the best.
3667 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3668 __isl_take isl_pw_multi_aff *pma1,
3669 __isl_take isl_pw_multi_aff *pma2,
3670 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3671 __isl_take isl_multi_aff *ma2))
3673 int i, j, n;
3674 isl_pw_multi_aff *res = NULL;
3675 isl_ctx *ctx;
3676 isl_set *set = NULL;
3678 if (!pma1 || !pma2)
3679 goto error;
3681 ctx = isl_space_get_ctx(pma1->dim);
3682 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3683 isl_die(ctx, isl_error_invalid,
3684 "arguments should live in the same space", goto error);
3686 if (isl_pw_multi_aff_is_empty(pma1)) {
3687 isl_pw_multi_aff_free(pma1);
3688 return pma2;
3691 if (isl_pw_multi_aff_is_empty(pma2)) {
3692 isl_pw_multi_aff_free(pma2);
3693 return pma1;
3696 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3697 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3699 for (i = 0; i < pma1->n; ++i) {
3700 set = isl_set_copy(pma1->p[i].set);
3701 for (j = 0; j < pma2->n; ++j) {
3702 isl_set *better;
3703 int is_empty;
3705 better = shared_and_better(pma2->p[j].set,
3706 pma1->p[i].set, pma2->p[j].maff,
3707 pma1->p[i].maff, cmp);
3708 is_empty = isl_set_plain_is_empty(better);
3709 if (is_empty < 0 || is_empty) {
3710 isl_set_free(better);
3711 if (is_empty < 0)
3712 goto error;
3713 continue;
3715 set = isl_set_subtract(set, isl_set_copy(better));
3717 res = isl_pw_multi_aff_add_piece(res, better,
3718 isl_multi_aff_copy(pma2->p[j].maff));
3720 res = isl_pw_multi_aff_add_piece(res, set,
3721 isl_multi_aff_copy(pma1->p[i].maff));
3724 for (j = 0; j < pma2->n; ++j) {
3725 set = isl_set_copy(pma2->p[j].set);
3726 for (i = 0; i < pma1->n; ++i)
3727 set = isl_set_subtract(set,
3728 isl_set_copy(pma1->p[i].set));
3729 res = isl_pw_multi_aff_add_piece(res, set,
3730 isl_multi_aff_copy(pma2->p[j].maff));
3733 isl_pw_multi_aff_free(pma1);
3734 isl_pw_multi_aff_free(pma2);
3736 return res;
3737 error:
3738 isl_pw_multi_aff_free(pma1);
3739 isl_pw_multi_aff_free(pma2);
3740 isl_set_free(set);
3741 return isl_pw_multi_aff_free(res);
3744 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3745 __isl_take isl_pw_multi_aff *pma1,
3746 __isl_take isl_pw_multi_aff *pma2)
3748 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3751 /* Given two piecewise multi affine expressions, return a piecewise
3752 * multi-affine expression defined on the union of the definition domains
3753 * of the inputs that is equal to the lexicographic maximum of the two
3754 * inputs on each cell. If only one of the two inputs is defined on
3755 * a given cell, then it is considered to be the maximum.
3757 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3758 __isl_take isl_pw_multi_aff *pma1,
3759 __isl_take isl_pw_multi_aff *pma2)
3761 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3762 &pw_multi_aff_union_lexmax);
3765 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3766 __isl_take isl_pw_multi_aff *pma1,
3767 __isl_take isl_pw_multi_aff *pma2)
3769 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3772 /* Given two piecewise multi affine expressions, return a piecewise
3773 * multi-affine expression defined on the union of the definition domains
3774 * of the inputs that is equal to the lexicographic minimum of the two
3775 * inputs on each cell. If only one of the two inputs is defined on
3776 * a given cell, then it is considered to be the minimum.
3778 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3779 __isl_take isl_pw_multi_aff *pma1,
3780 __isl_take isl_pw_multi_aff *pma2)
3782 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3783 &pw_multi_aff_union_lexmin);
3786 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3787 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3789 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3790 &isl_multi_aff_add);
3793 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3794 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3796 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3797 &pw_multi_aff_add);
3800 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3801 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3803 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3804 &isl_multi_aff_sub);
3807 /* Subtract "pma2" from "pma1" and return the result.
3809 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3810 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3812 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3813 &pw_multi_aff_sub);
3816 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3817 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3819 return isl_pw_multi_aff_union_add_(pma1, pma2);
3822 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3823 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3825 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3826 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3828 int i, j, n;
3829 isl_space *space;
3830 isl_pw_multi_aff *res;
3832 if (!pma1 || !pma2)
3833 goto error;
3835 n = pma1->n * pma2->n;
3836 space = isl_space_product(isl_space_copy(pma1->dim),
3837 isl_space_copy(pma2->dim));
3838 res = isl_pw_multi_aff_alloc_size(space, n);
3840 for (i = 0; i < pma1->n; ++i) {
3841 for (j = 0; j < pma2->n; ++j) {
3842 isl_set *domain;
3843 isl_multi_aff *ma;
3845 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3846 isl_set_copy(pma2->p[j].set));
3847 ma = isl_multi_aff_product(
3848 isl_multi_aff_copy(pma1->p[i].maff),
3849 isl_multi_aff_copy(pma2->p[j].maff));
3850 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3854 isl_pw_multi_aff_free(pma1);
3855 isl_pw_multi_aff_free(pma2);
3856 return res;
3857 error:
3858 isl_pw_multi_aff_free(pma1);
3859 isl_pw_multi_aff_free(pma2);
3860 return NULL;
3863 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3864 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3866 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3867 &pw_multi_aff_product);
3870 /* Construct a map mapping the domain of the piecewise multi-affine expression
3871 * to its range, with each dimension in the range equated to the
3872 * corresponding affine expression on its cell.
3874 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3876 int i;
3877 isl_map *map;
3879 if (!pma)
3880 return NULL;
3882 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3884 for (i = 0; i < pma->n; ++i) {
3885 isl_multi_aff *maff;
3886 isl_basic_map *bmap;
3887 isl_map *map_i;
3889 maff = isl_multi_aff_copy(pma->p[i].maff);
3890 bmap = isl_basic_map_from_multi_aff(maff);
3891 map_i = isl_map_from_basic_map(bmap);
3892 map_i = isl_map_intersect_domain(map_i,
3893 isl_set_copy(pma->p[i].set));
3894 map = isl_map_union_disjoint(map, map_i);
3897 isl_pw_multi_aff_free(pma);
3898 return map;
3901 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3903 if (!pma)
3904 return NULL;
3906 if (!isl_space_is_set(pma->dim))
3907 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3908 "isl_pw_multi_aff cannot be converted into an isl_set",
3909 return isl_pw_multi_aff_free(pma));
3911 return isl_map_from_pw_multi_aff(pma);
3914 /* Given a basic map with a single output dimension that is defined
3915 * in terms of the parameters and input dimensions using an equality,
3916 * extract an isl_aff that expresses the output dimension in terms
3917 * of the parameters and input dimensions.
3919 * Since some applications expect the result of isl_pw_multi_aff_from_map
3920 * to only contain integer affine expressions, we compute the floor
3921 * of the expression before returning.
3923 * This function shares some similarities with
3924 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3926 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3927 __isl_take isl_basic_map *bmap)
3929 int i;
3930 unsigned offset;
3931 unsigned total;
3932 isl_local_space *ls;
3933 isl_aff *aff;
3935 if (!bmap)
3936 return NULL;
3937 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3938 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3939 "basic map should have a single output dimension",
3940 goto error);
3941 offset = isl_basic_map_offset(bmap, isl_dim_out);
3942 total = isl_basic_map_total_dim(bmap);
3943 for (i = 0; i < bmap->n_eq; ++i) {
3944 if (isl_int_is_zero(bmap->eq[i][offset]))
3945 continue;
3946 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3947 1 + total - (offset + 1)) != -1)
3948 continue;
3949 break;
3951 if (i >= bmap->n_eq)
3952 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3953 "unable to find suitable equality", goto error);
3954 ls = isl_basic_map_get_local_space(bmap);
3955 aff = isl_aff_alloc(isl_local_space_domain(ls));
3956 if (!aff)
3957 goto error;
3958 if (isl_int_is_neg(bmap->eq[i][offset]))
3959 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3960 else
3961 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3962 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3963 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3964 isl_basic_map_free(bmap);
3966 aff = isl_aff_remove_unused_divs(aff);
3967 aff = isl_aff_floor(aff);
3968 return aff;
3969 error:
3970 isl_basic_map_free(bmap);
3971 return NULL;
3974 /* Given a basic map where each output dimension is defined
3975 * in terms of the parameters and input dimensions using an equality,
3976 * extract an isl_multi_aff that expresses the output dimensions in terms
3977 * of the parameters and input dimensions.
3979 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3980 __isl_take isl_basic_map *bmap)
3982 int i;
3983 unsigned n_out;
3984 isl_multi_aff *ma;
3986 if (!bmap)
3987 return NULL;
3989 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3990 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3992 for (i = 0; i < n_out; ++i) {
3993 isl_basic_map *bmap_i;
3994 isl_aff *aff;
3996 bmap_i = isl_basic_map_copy(bmap);
3997 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3998 i + 1, n_out - (1 + i));
3999 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4000 aff = extract_isl_aff_from_basic_map(bmap_i);
4001 ma = isl_multi_aff_set_aff(ma, i, aff);
4004 isl_basic_map_free(bmap);
4006 return ma;
4009 /* Create an isl_pw_multi_aff that is equivalent to
4010 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4011 * The given basic map is such that each output dimension is defined
4012 * in terms of the parameters and input dimensions using an equality.
4014 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4015 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4017 isl_multi_aff *ma;
4019 ma = extract_isl_multi_aff_from_basic_map(bmap);
4020 return isl_pw_multi_aff_alloc(domain, ma);
4023 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4024 * This obviously only works if the input "map" is single-valued.
4025 * If so, we compute the lexicographic minimum of the image in the form
4026 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4027 * to its lexicographic minimum.
4028 * If the input is not single-valued, we produce an error.
4030 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4031 __isl_take isl_map *map)
4033 int i;
4034 int sv;
4035 isl_pw_multi_aff *pma;
4037 sv = isl_map_is_single_valued(map);
4038 if (sv < 0)
4039 goto error;
4040 if (!sv)
4041 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4042 "map is not single-valued", goto error);
4043 map = isl_map_make_disjoint(map);
4044 if (!map)
4045 return NULL;
4047 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4049 for (i = 0; i < map->n; ++i) {
4050 isl_pw_multi_aff *pma_i;
4051 isl_basic_map *bmap;
4052 bmap = isl_basic_map_copy(map->p[i]);
4053 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4054 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4057 isl_map_free(map);
4058 return pma;
4059 error:
4060 isl_map_free(map);
4061 return NULL;
4064 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4065 * taking into account that the output dimension at position "d"
4066 * can be represented as
4068 * x = floor((e(...) + c1) / m)
4070 * given that constraint "i" is of the form
4072 * e(...) + c1 - m x >= 0
4075 * Let "map" be of the form
4077 * A -> B
4079 * We construct a mapping
4081 * A -> [A -> x = floor(...)]
4083 * apply that to the map, obtaining
4085 * [A -> x = floor(...)] -> B
4087 * and equate dimension "d" to x.
4088 * We then compute a isl_pw_multi_aff representation of the resulting map
4089 * and plug in the mapping above.
4091 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4092 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4094 isl_ctx *ctx;
4095 isl_space *space;
4096 isl_local_space *ls;
4097 isl_multi_aff *ma;
4098 isl_aff *aff;
4099 isl_vec *v;
4100 isl_map *insert;
4101 int offset;
4102 int n;
4103 int n_in;
4104 isl_pw_multi_aff *pma;
4105 int is_set;
4107 is_set = isl_map_is_set(map);
4109 offset = isl_basic_map_offset(hull, isl_dim_out);
4110 ctx = isl_map_get_ctx(map);
4111 space = isl_space_domain(isl_map_get_space(map));
4112 n_in = isl_space_dim(space, isl_dim_set);
4113 n = isl_space_dim(space, isl_dim_all);
4115 v = isl_vec_alloc(ctx, 1 + 1 + n);
4116 if (v) {
4117 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4118 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4120 isl_basic_map_free(hull);
4122 ls = isl_local_space_from_space(isl_space_copy(space));
4123 aff = isl_aff_alloc_vec(ls, v);
4124 aff = isl_aff_floor(aff);
4125 if (is_set) {
4126 isl_space_free(space);
4127 ma = isl_multi_aff_from_aff(aff);
4128 } else {
4129 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4130 ma = isl_multi_aff_range_product(ma,
4131 isl_multi_aff_from_aff(aff));
4134 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4135 map = isl_map_apply_domain(map, insert);
4136 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4137 pma = isl_pw_multi_aff_from_map(map);
4138 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4140 return pma;
4143 /* Is constraint "c" of the form
4145 * e(...) + c1 - m x >= 0
4147 * or
4149 * -e(...) + c2 + m x >= 0
4151 * where m > 1 and e only depends on parameters and input dimemnsions?
4153 * "offset" is the offset of the output dimensions
4154 * "pos" is the position of output dimension x.
4156 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4158 if (isl_int_is_zero(c[offset + d]))
4159 return 0;
4160 if (isl_int_is_one(c[offset + d]))
4161 return 0;
4162 if (isl_int_is_negone(c[offset + d]))
4163 return 0;
4164 if (isl_seq_first_non_zero(c + offset, d) != -1)
4165 return 0;
4166 if (isl_seq_first_non_zero(c + offset + d + 1,
4167 total - (offset + d + 1)) != -1)
4168 return 0;
4169 return 1;
4172 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4174 * As a special case, we first check if there is any pair of constraints,
4175 * shared by all the basic maps in "map" that force a given dimension
4176 * to be equal to the floor of some affine combination of the input dimensions.
4178 * In particular, if we can find two constraints
4180 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4182 * and
4184 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4186 * where m > 1 and e only depends on parameters and input dimemnsions,
4187 * and such that
4189 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4191 * then we know that we can take
4193 * x = floor((e(...) + c1) / m)
4195 * without having to perform any computation.
4197 * Note that we know that
4199 * c1 + c2 >= 1
4201 * If c1 + c2 were 0, then we would have detected an equality during
4202 * simplification. If c1 + c2 were negative, then we would have detected
4203 * a contradiction.
4205 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4206 __isl_take isl_map *map)
4208 int d, dim;
4209 int i, j, n;
4210 int offset, total;
4211 isl_int sum;
4212 isl_basic_map *hull;
4214 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4215 if (!hull)
4216 goto error;
4218 isl_int_init(sum);
4219 dim = isl_map_dim(map, isl_dim_out);
4220 offset = isl_basic_map_offset(hull, isl_dim_out);
4221 total = 1 + isl_basic_map_total_dim(hull);
4222 n = hull->n_ineq;
4223 for (d = 0; d < dim; ++d) {
4224 for (i = 0; i < n; ++i) {
4225 if (!is_potential_div_constraint(hull->ineq[i],
4226 offset, d, total))
4227 continue;
4228 for (j = i + 1; j < n; ++j) {
4229 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4230 hull->ineq[j] + 1, total - 1))
4231 continue;
4232 isl_int_add(sum, hull->ineq[i][0],
4233 hull->ineq[j][0]);
4234 if (isl_int_abs_lt(sum,
4235 hull->ineq[i][offset + d]))
4236 break;
4239 if (j >= n)
4240 continue;
4241 isl_int_clear(sum);
4242 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4243 j = i;
4244 return pw_multi_aff_from_map_div(map, hull, d, j);
4247 isl_int_clear(sum);
4248 isl_basic_map_free(hull);
4249 return pw_multi_aff_from_map_base(map);
4250 error:
4251 isl_map_free(map);
4252 isl_basic_map_free(hull);
4253 return NULL;
4256 /* Given an affine expression
4258 * [A -> B] -> f(A,B)
4260 * construct an isl_multi_aff
4262 * [A -> B] -> B'
4264 * such that dimension "d" in B' is set to "aff" and the remaining
4265 * dimensions are set equal to the corresponding dimensions in B.
4266 * "n_in" is the dimension of the space A.
4267 * "n_out" is the dimension of the space B.
4269 * If "is_set" is set, then the affine expression is of the form
4271 * [B] -> f(B)
4273 * and we construct an isl_multi_aff
4275 * B -> B'
4277 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4278 unsigned n_in, unsigned n_out, int is_set)
4280 int i;
4281 isl_multi_aff *ma;
4282 isl_space *space, *space2;
4283 isl_local_space *ls;
4285 space = isl_aff_get_domain_space(aff);
4286 ls = isl_local_space_from_space(isl_space_copy(space));
4287 space2 = isl_space_copy(space);
4288 if (!is_set)
4289 space2 = isl_space_range(isl_space_unwrap(space2));
4290 space = isl_space_map_from_domain_and_range(space, space2);
4291 ma = isl_multi_aff_alloc(space);
4292 ma = isl_multi_aff_set_aff(ma, d, aff);
4294 for (i = 0; i < n_out; ++i) {
4295 if (i == d)
4296 continue;
4297 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4298 isl_dim_set, n_in + i);
4299 ma = isl_multi_aff_set_aff(ma, i, aff);
4302 isl_local_space_free(ls);
4304 return ma;
4307 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4308 * taking into account that the dimension at position "d" can be written as
4310 * x = m a + f(..) (1)
4312 * where m is equal to "gcd".
4313 * "i" is the index of the equality in "hull" that defines f(..).
4314 * In particular, the equality is of the form
4316 * f(..) - x + m g(existentials) = 0
4318 * or
4320 * -f(..) + x + m g(existentials) = 0
4322 * We basically plug (1) into "map", resulting in a map with "a"
4323 * in the range instead of "x". The corresponding isl_pw_multi_aff
4324 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4326 * Specifically, given the input map
4328 * A -> B
4330 * We first wrap it into a set
4332 * [A -> B]
4334 * and define (1) on top of the corresponding space, resulting in "aff".
4335 * We use this to create an isl_multi_aff that maps the output position "d"
4336 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4337 * We plug this into the wrapped map, unwrap the result and compute the
4338 * corresponding isl_pw_multi_aff.
4339 * The result is an expression
4341 * A -> T(A)
4343 * We adjust that to
4345 * A -> [A -> T(A)]
4347 * so that we can plug that into "aff", after extending the latter to
4348 * a mapping
4350 * [A -> B] -> B'
4353 * If "map" is actually a set, then there is no "A" space, meaning
4354 * that we do not need to perform any wrapping, and that the result
4355 * of the recursive call is of the form
4357 * [T]
4359 * which is plugged into a mapping of the form
4361 * B -> B'
4363 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4364 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4365 isl_int gcd)
4367 isl_set *set;
4368 isl_space *space;
4369 isl_local_space *ls;
4370 isl_aff *aff;
4371 isl_multi_aff *ma;
4372 isl_pw_multi_aff *pma, *id;
4373 unsigned n_in;
4374 unsigned o_out;
4375 unsigned n_out;
4376 int is_set;
4378 is_set = isl_map_is_set(map);
4380 n_in = isl_basic_map_dim(hull, isl_dim_in);
4381 n_out = isl_basic_map_dim(hull, isl_dim_out);
4382 o_out = isl_basic_map_offset(hull, isl_dim_out);
4384 if (is_set)
4385 set = map;
4386 else
4387 set = isl_map_wrap(map);
4388 space = isl_space_map_from_set(isl_set_get_space(set));
4389 ma = isl_multi_aff_identity(space);
4390 ls = isl_local_space_from_space(isl_set_get_space(set));
4391 aff = isl_aff_alloc(ls);
4392 if (aff) {
4393 isl_int_set_si(aff->v->el[0], 1);
4394 if (isl_int_is_one(hull->eq[i][o_out + d]))
4395 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4396 aff->v->size - 1);
4397 else
4398 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4399 aff->v->size - 1);
4400 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4402 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4403 set = isl_set_preimage_multi_aff(set, ma);
4405 ma = range_map(aff, d, n_in, n_out, is_set);
4407 if (is_set)
4408 map = set;
4409 else
4410 map = isl_set_unwrap(set);
4411 pma = isl_pw_multi_aff_from_map(set);
4413 if (!is_set) {
4414 space = isl_pw_multi_aff_get_domain_space(pma);
4415 space = isl_space_map_from_set(space);
4416 id = isl_pw_multi_aff_identity(space);
4417 pma = isl_pw_multi_aff_range_product(id, pma);
4419 id = isl_pw_multi_aff_from_multi_aff(ma);
4420 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4422 isl_basic_map_free(hull);
4423 return pma;
4426 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4428 * As a special case, we first check if all output dimensions are uniquely
4429 * defined in terms of the parameters and input dimensions over the entire
4430 * domain. If so, we extract the desired isl_pw_multi_aff directly
4431 * from the affine hull of "map" and its domain.
4433 * Otherwise, we check if any of the output dimensions is "strided".
4434 * That is, we check if can be written as
4436 * x = m a + f(..)
4438 * with m greater than 1, a some combination of existentiall quantified
4439 * variables and f and expression in the parameters and input dimensions.
4440 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4442 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4443 * special case.
4445 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4447 int i, j;
4448 int sv;
4449 isl_basic_map *hull;
4450 unsigned n_out;
4451 unsigned o_out;
4452 unsigned n_div;
4453 unsigned o_div;
4454 isl_int gcd;
4456 if (!map)
4457 return NULL;
4459 hull = isl_map_affine_hull(isl_map_copy(map));
4460 sv = isl_basic_map_plain_is_single_valued(hull);
4461 if (sv >= 0 && sv)
4462 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4463 if (sv < 0)
4464 hull = isl_basic_map_free(hull);
4465 if (!hull)
4466 goto error;
4468 n_div = isl_basic_map_dim(hull, isl_dim_div);
4469 o_div = isl_basic_map_offset(hull, isl_dim_div);
4471 if (n_div == 0) {
4472 isl_basic_map_free(hull);
4473 return pw_multi_aff_from_map_check_div(map);
4476 isl_int_init(gcd);
4478 n_out = isl_basic_map_dim(hull, isl_dim_out);
4479 o_out = isl_basic_map_offset(hull, isl_dim_out);
4481 for (i = 0; i < n_out; ++i) {
4482 for (j = 0; j < hull->n_eq; ++j) {
4483 isl_int *eq = hull->eq[j];
4484 isl_pw_multi_aff *res;
4486 if (!isl_int_is_one(eq[o_out + i]) &&
4487 !isl_int_is_negone(eq[o_out + i]))
4488 continue;
4489 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4490 continue;
4491 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4492 n_out - (i + 1)) != -1)
4493 continue;
4494 isl_seq_gcd(eq + o_div, n_div, &gcd);
4495 if (isl_int_is_zero(gcd))
4496 continue;
4497 if (isl_int_is_one(gcd))
4498 continue;
4500 res = pw_multi_aff_from_map_stride(map, hull,
4501 i, j, gcd);
4502 isl_int_clear(gcd);
4503 return res;
4507 isl_int_clear(gcd);
4508 isl_basic_map_free(hull);
4509 return pw_multi_aff_from_map_check_div(map);
4510 error:
4511 isl_map_free(map);
4512 return NULL;
4515 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4517 return isl_pw_multi_aff_from_map(set);
4520 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4521 * add it to *user.
4523 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4525 isl_union_pw_multi_aff **upma = user;
4526 isl_pw_multi_aff *pma;
4528 pma = isl_pw_multi_aff_from_map(map);
4529 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4531 return *upma ? 0 : -1;
4534 /* Try and create an isl_union_pw_multi_aff that is equivalent
4535 * to the given isl_union_map.
4536 * The isl_union_map is required to be single-valued in each space.
4537 * Otherwise, an error is produced.
4539 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4540 __isl_take isl_union_map *umap)
4542 isl_space *space;
4543 isl_union_pw_multi_aff *upma;
4545 space = isl_union_map_get_space(umap);
4546 upma = isl_union_pw_multi_aff_empty(space);
4547 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4548 upma = isl_union_pw_multi_aff_free(upma);
4549 isl_union_map_free(umap);
4551 return upma;
4554 /* Try and create an isl_union_pw_multi_aff that is equivalent
4555 * to the given isl_union_set.
4556 * The isl_union_set is required to be a singleton in each space.
4557 * Otherwise, an error is produced.
4559 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4560 __isl_take isl_union_set *uset)
4562 return isl_union_pw_multi_aff_from_union_map(uset);
4565 /* Return the piecewise affine expression "set ? 1 : 0".
4567 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4569 isl_pw_aff *pa;
4570 isl_space *space = isl_set_get_space(set);
4571 isl_local_space *ls = isl_local_space_from_space(space);
4572 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4573 isl_aff *one = isl_aff_zero_on_domain(ls);
4575 one = isl_aff_add_constant_si(one, 1);
4576 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4577 set = isl_set_complement(set);
4578 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4580 return pa;
4583 /* Plug in "subs" for dimension "type", "pos" of "aff".
4585 * Let i be the dimension to replace and let "subs" be of the form
4587 * f/d
4589 * and "aff" of the form
4591 * (a i + g)/m
4593 * The result is
4595 * (a f + d g')/(m d)
4597 * where g' is the result of plugging in "subs" in each of the integer
4598 * divisions in g.
4600 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4601 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4603 isl_ctx *ctx;
4604 isl_int v;
4606 aff = isl_aff_cow(aff);
4607 if (!aff || !subs)
4608 return isl_aff_free(aff);
4610 ctx = isl_aff_get_ctx(aff);
4611 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4612 isl_die(ctx, isl_error_invalid,
4613 "spaces don't match", return isl_aff_free(aff));
4614 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4615 isl_die(ctx, isl_error_unsupported,
4616 "cannot handle divs yet", return isl_aff_free(aff));
4618 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4619 if (!aff->ls)
4620 return isl_aff_free(aff);
4622 aff->v = isl_vec_cow(aff->v);
4623 if (!aff->v)
4624 return isl_aff_free(aff);
4626 pos += isl_local_space_offset(aff->ls, type);
4628 isl_int_init(v);
4629 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4630 aff->v->size, subs->v->size, v);
4631 isl_int_clear(v);
4633 return aff;
4636 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4637 * expressions in "maff".
4639 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4640 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4641 __isl_keep isl_aff *subs)
4643 int i;
4645 maff = isl_multi_aff_cow(maff);
4646 if (!maff || !subs)
4647 return isl_multi_aff_free(maff);
4649 if (type == isl_dim_in)
4650 type = isl_dim_set;
4652 for (i = 0; i < maff->n; ++i) {
4653 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4654 if (!maff->p[i])
4655 return isl_multi_aff_free(maff);
4658 return maff;
4661 /* Plug in "subs" for dimension "type", "pos" of "pma".
4663 * pma is of the form
4665 * A_i(v) -> M_i(v)
4667 * while subs is of the form
4669 * v' = B_j(v) -> S_j
4671 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4672 * has a contribution in the result, in particular
4674 * C_ij(S_j) -> M_i(S_j)
4676 * Note that plugging in S_j in C_ij may also result in an empty set
4677 * and this contribution should simply be discarded.
4679 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4680 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4681 __isl_keep isl_pw_aff *subs)
4683 int i, j, n;
4684 isl_pw_multi_aff *res;
4686 if (!pma || !subs)
4687 return isl_pw_multi_aff_free(pma);
4689 n = pma->n * subs->n;
4690 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4692 for (i = 0; i < pma->n; ++i) {
4693 for (j = 0; j < subs->n; ++j) {
4694 isl_set *common;
4695 isl_multi_aff *res_ij;
4696 int empty;
4698 common = isl_set_intersect(
4699 isl_set_copy(pma->p[i].set),
4700 isl_set_copy(subs->p[j].set));
4701 common = isl_set_substitute(common,
4702 type, pos, subs->p[j].aff);
4703 empty = isl_set_plain_is_empty(common);
4704 if (empty < 0 || empty) {
4705 isl_set_free(common);
4706 if (empty < 0)
4707 goto error;
4708 continue;
4711 res_ij = isl_multi_aff_substitute(
4712 isl_multi_aff_copy(pma->p[i].maff),
4713 type, pos, subs->p[j].aff);
4715 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4719 isl_pw_multi_aff_free(pma);
4720 return res;
4721 error:
4722 isl_pw_multi_aff_free(pma);
4723 isl_pw_multi_aff_free(res);
4724 return NULL;
4727 /* Compute the preimage of a range of dimensions in the affine expression "src"
4728 * under "ma" and put the result in "dst". The number of dimensions in "src"
4729 * that precede the range is given by "n_before". The number of dimensions
4730 * in the range is given by the number of output dimensions of "ma".
4731 * The number of dimensions that follow the range is given by "n_after".
4732 * If "has_denom" is set (to one),
4733 * then "src" and "dst" have an extra initial denominator.
4734 * "n_div_ma" is the number of existentials in "ma"
4735 * "n_div_bset" is the number of existentials in "src"
4736 * The resulting "dst" (which is assumed to have been allocated by
4737 * the caller) contains coefficients for both sets of existentials,
4738 * first those in "ma" and then those in "src".
4739 * f, c1, c2 and g are temporary objects that have been initialized
4740 * by the caller.
4742 * Let src represent the expression
4744 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4746 * and let ma represent the expressions
4748 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4750 * We start out with the following expression for dst:
4752 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4754 * with the multiplication factor f initially equal to 1
4755 * and f \sum_i b_i v_i kept separately.
4756 * For each x_i that we substitute, we multiply the numerator
4757 * (and denominator) of dst by c_1 = m_i and add the numerator
4758 * of the x_i expression multiplied by c_2 = f b_i,
4759 * after removing the common factors of c_1 and c_2.
4760 * The multiplication factor f also needs to be multiplied by c_1
4761 * for the next x_j, j > i.
4763 void isl_seq_preimage(isl_int *dst, isl_int *src,
4764 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4765 int n_div_ma, int n_div_bmap,
4766 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4768 int i;
4769 int n_param, n_in, n_out;
4770 int o_dst, o_src;
4772 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4773 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4774 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4776 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4777 o_dst = o_src = has_denom + 1 + n_param + n_before;
4778 isl_seq_clr(dst + o_dst, n_in);
4779 o_dst += n_in;
4780 o_src += n_out;
4781 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4782 o_dst += n_after;
4783 o_src += n_after;
4784 isl_seq_clr(dst + o_dst, n_div_ma);
4785 o_dst += n_div_ma;
4786 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4788 isl_int_set_si(f, 1);
4790 for (i = 0; i < n_out; ++i) {
4791 int offset = has_denom + 1 + n_param + n_before + i;
4793 if (isl_int_is_zero(src[offset]))
4794 continue;
4795 isl_int_set(c1, ma->p[i]->v->el[0]);
4796 isl_int_mul(c2, f, src[offset]);
4797 isl_int_gcd(g, c1, c2);
4798 isl_int_divexact(c1, c1, g);
4799 isl_int_divexact(c2, c2, g);
4801 isl_int_mul(f, f, c1);
4802 o_dst = has_denom;
4803 o_src = 1;
4804 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4805 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4806 o_dst += 1 + n_param;
4807 o_src += 1 + n_param;
4808 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4809 o_dst += n_before;
4810 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4811 c2, ma->p[i]->v->el + o_src, n_in);
4812 o_dst += n_in;
4813 o_src += n_in;
4814 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4815 o_dst += n_after;
4816 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4817 c2, ma->p[i]->v->el + o_src, n_div_ma);
4818 o_dst += n_div_ma;
4819 o_src += n_div_ma;
4820 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4821 if (has_denom)
4822 isl_int_mul(dst[0], dst[0], c1);
4826 /* Compute the pullback of "aff" by the function represented by "ma".
4827 * In other words, plug in "ma" in "aff". The result is an affine expression
4828 * defined over the domain space of "ma".
4830 * If "aff" is represented by
4832 * (a(p) + b x + c(divs))/d
4834 * and ma is represented by
4836 * x = D(p) + F(y) + G(divs')
4838 * then the result is
4840 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4842 * The divs in the local space of the input are similarly adjusted
4843 * through a call to isl_local_space_preimage_multi_aff.
4845 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4846 __isl_take isl_multi_aff *ma)
4848 isl_aff *res = NULL;
4849 isl_local_space *ls;
4850 int n_div_aff, n_div_ma;
4851 isl_int f, c1, c2, g;
4853 ma = isl_multi_aff_align_divs(ma);
4854 if (!aff || !ma)
4855 goto error;
4857 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4858 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4860 ls = isl_aff_get_domain_local_space(aff);
4861 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4862 res = isl_aff_alloc(ls);
4863 if (!res)
4864 goto error;
4866 isl_int_init(f);
4867 isl_int_init(c1);
4868 isl_int_init(c2);
4869 isl_int_init(g);
4871 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4872 f, c1, c2, g, 1);
4874 isl_int_clear(f);
4875 isl_int_clear(c1);
4876 isl_int_clear(c2);
4877 isl_int_clear(g);
4879 isl_aff_free(aff);
4880 isl_multi_aff_free(ma);
4881 res = isl_aff_normalize(res);
4882 return res;
4883 error:
4884 isl_aff_free(aff);
4885 isl_multi_aff_free(ma);
4886 isl_aff_free(res);
4887 return NULL;
4890 /* Compute the pullback of "aff1" by the function represented by "aff2".
4891 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4892 * defined over the domain space of "aff1".
4894 * The domain of "aff1" should match the range of "aff2", which means
4895 * that it should be single-dimensional.
4897 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
4898 __isl_take isl_aff *aff2)
4900 isl_multi_aff *ma;
4902 ma = isl_multi_aff_from_aff(aff2);
4903 return isl_aff_pullback_multi_aff(aff1, ma);
4906 /* Compute the pullback of "ma1" by the function represented by "ma2".
4907 * In other words, plug in "ma2" in "ma1".
4909 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4911 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
4912 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4914 int i;
4915 isl_space *space = NULL;
4917 ma2 = isl_multi_aff_align_divs(ma2);
4918 ma1 = isl_multi_aff_cow(ma1);
4919 if (!ma1 || !ma2)
4920 goto error;
4922 space = isl_space_join(isl_multi_aff_get_space(ma2),
4923 isl_multi_aff_get_space(ma1));
4925 for (i = 0; i < ma1->n; ++i) {
4926 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4927 isl_multi_aff_copy(ma2));
4928 if (!ma1->p[i])
4929 goto error;
4932 ma1 = isl_multi_aff_reset_space(ma1, space);
4933 isl_multi_aff_free(ma2);
4934 return ma1;
4935 error:
4936 isl_space_free(space);
4937 isl_multi_aff_free(ma2);
4938 isl_multi_aff_free(ma1);
4939 return NULL;
4942 /* Compute the pullback of "ma1" by the function represented by "ma2".
4943 * In other words, plug in "ma2" in "ma1".
4945 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4946 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4948 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
4949 &isl_multi_aff_pullback_multi_aff_aligned);
4952 /* Extend the local space of "dst" to include the divs
4953 * in the local space of "src".
4955 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4956 __isl_keep isl_aff *src)
4958 isl_ctx *ctx;
4959 int *exp1 = NULL;
4960 int *exp2 = NULL;
4961 isl_mat *div;
4963 if (!src || !dst)
4964 return isl_aff_free(dst);
4966 ctx = isl_aff_get_ctx(src);
4967 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4968 isl_die(ctx, isl_error_invalid,
4969 "spaces don't match", goto error);
4971 if (src->ls->div->n_row == 0)
4972 return dst;
4974 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4975 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4976 if (!exp1 || (dst->ls->div->n_row && !exp2))
4977 goto error;
4979 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4980 dst = isl_aff_expand_divs(dst, div, exp2);
4981 free(exp1);
4982 free(exp2);
4984 return dst;
4985 error:
4986 free(exp1);
4987 free(exp2);
4988 return isl_aff_free(dst);
4991 /* Adjust the local spaces of the affine expressions in "maff"
4992 * such that they all have the save divs.
4994 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4995 __isl_take isl_multi_aff *maff)
4997 int i;
4999 if (!maff)
5000 return NULL;
5001 if (maff->n == 0)
5002 return maff;
5003 maff = isl_multi_aff_cow(maff);
5004 if (!maff)
5005 return NULL;
5007 for (i = 1; i < maff->n; ++i)
5008 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5009 for (i = 1; i < maff->n; ++i) {
5010 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5011 if (!maff->p[i])
5012 return isl_multi_aff_free(maff);
5015 return maff;
5018 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5020 aff = isl_aff_cow(aff);
5021 if (!aff)
5022 return NULL;
5024 aff->ls = isl_local_space_lift(aff->ls);
5025 if (!aff->ls)
5026 return isl_aff_free(aff);
5028 return aff;
5031 /* Lift "maff" to a space with extra dimensions such that the result
5032 * has no more existentially quantified variables.
5033 * If "ls" is not NULL, then *ls is assigned the local space that lies
5034 * at the basis of the lifting applied to "maff".
5036 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5037 __isl_give isl_local_space **ls)
5039 int i;
5040 isl_space *space;
5041 unsigned n_div;
5043 if (ls)
5044 *ls = NULL;
5046 if (!maff)
5047 return NULL;
5049 if (maff->n == 0) {
5050 if (ls) {
5051 isl_space *space = isl_multi_aff_get_domain_space(maff);
5052 *ls = isl_local_space_from_space(space);
5053 if (!*ls)
5054 return isl_multi_aff_free(maff);
5056 return maff;
5059 maff = isl_multi_aff_cow(maff);
5060 maff = isl_multi_aff_align_divs(maff);
5061 if (!maff)
5062 return NULL;
5064 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5065 space = isl_multi_aff_get_space(maff);
5066 space = isl_space_lift(isl_space_domain(space), n_div);
5067 space = isl_space_extend_domain_with_range(space,
5068 isl_multi_aff_get_space(maff));
5069 if (!space)
5070 return isl_multi_aff_free(maff);
5071 isl_space_free(maff->space);
5072 maff->space = space;
5074 if (ls) {
5075 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5076 if (!*ls)
5077 return isl_multi_aff_free(maff);
5080 for (i = 0; i < maff->n; ++i) {
5081 maff->p[i] = isl_aff_lift(maff->p[i]);
5082 if (!maff->p[i])
5083 goto error;
5086 return maff;
5087 error:
5088 if (ls)
5089 isl_local_space_free(*ls);
5090 return isl_multi_aff_free(maff);
5094 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5096 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5097 __isl_keep isl_pw_multi_aff *pma, int pos)
5099 int i;
5100 int n_out;
5101 isl_space *space;
5102 isl_pw_aff *pa;
5104 if (!pma)
5105 return NULL;
5107 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5108 if (pos < 0 || pos >= n_out)
5109 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5110 "index out of bounds", return NULL);
5112 space = isl_pw_multi_aff_get_space(pma);
5113 space = isl_space_drop_dims(space, isl_dim_out,
5114 pos + 1, n_out - pos - 1);
5115 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5117 pa = isl_pw_aff_alloc_size(space, pma->n);
5118 for (i = 0; i < pma->n; ++i) {
5119 isl_aff *aff;
5120 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5121 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5124 return pa;
5127 /* Return an isl_pw_multi_aff with the given "set" as domain and
5128 * an unnamed zero-dimensional range.
5130 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5131 __isl_take isl_set *set)
5133 isl_multi_aff *ma;
5134 isl_space *space;
5136 space = isl_set_get_space(set);
5137 space = isl_space_from_domain(space);
5138 ma = isl_multi_aff_zero(space);
5139 return isl_pw_multi_aff_alloc(set, ma);
5142 /* Add an isl_pw_multi_aff with the given "set" as domain and
5143 * an unnamed zero-dimensional range to *user.
5145 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5147 isl_union_pw_multi_aff **upma = user;
5148 isl_pw_multi_aff *pma;
5150 pma = isl_pw_multi_aff_from_domain(set);
5151 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5153 return 0;
5156 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5157 * an unnamed zero-dimensional range.
5159 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5160 __isl_take isl_union_set *uset)
5162 isl_space *space;
5163 isl_union_pw_multi_aff *upma;
5165 if (!uset)
5166 return NULL;
5168 space = isl_union_set_get_space(uset);
5169 upma = isl_union_pw_multi_aff_empty(space);
5171 if (isl_union_set_foreach_set(uset,
5172 &add_pw_multi_aff_from_domain, &upma) < 0)
5173 goto error;
5175 isl_union_set_free(uset);
5176 return upma;
5177 error:
5178 isl_union_set_free(uset);
5179 isl_union_pw_multi_aff_free(upma);
5180 return NULL;
5183 /* Convert "pma" to an isl_map and add it to *umap.
5185 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5187 isl_union_map **umap = user;
5188 isl_map *map;
5190 map = isl_map_from_pw_multi_aff(pma);
5191 *umap = isl_union_map_add_map(*umap, map);
5193 return 0;
5196 /* Construct a union map mapping the domain of the union
5197 * piecewise multi-affine expression to its range, with each dimension
5198 * in the range equated to the corresponding affine expression on its cell.
5200 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5201 __isl_take isl_union_pw_multi_aff *upma)
5203 isl_space *space;
5204 isl_union_map *umap;
5206 if (!upma)
5207 return NULL;
5209 space = isl_union_pw_multi_aff_get_space(upma);
5210 umap = isl_union_map_empty(space);
5212 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5213 &map_from_pw_multi_aff, &umap) < 0)
5214 goto error;
5216 isl_union_pw_multi_aff_free(upma);
5217 return umap;
5218 error:
5219 isl_union_pw_multi_aff_free(upma);
5220 isl_union_map_free(umap);
5221 return NULL;
5224 /* Local data for bin_entry and the callback "fn".
5226 struct isl_union_pw_multi_aff_bin_data {
5227 isl_union_pw_multi_aff *upma2;
5228 isl_union_pw_multi_aff *res;
5229 isl_pw_multi_aff *pma;
5230 int (*fn)(void **entry, void *user);
5233 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5234 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5236 static int bin_entry(void **entry, void *user)
5238 struct isl_union_pw_multi_aff_bin_data *data = user;
5239 isl_pw_multi_aff *pma = *entry;
5241 data->pma = pma;
5242 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
5243 data->fn, data) < 0)
5244 return -1;
5246 return 0;
5249 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5250 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5251 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5252 * as *entry. The callback should adjust data->res if desired.
5254 static __isl_give isl_union_pw_multi_aff *bin_op(
5255 __isl_take isl_union_pw_multi_aff *upma1,
5256 __isl_take isl_union_pw_multi_aff *upma2,
5257 int (*fn)(void **entry, void *user))
5259 isl_space *space;
5260 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5262 space = isl_union_pw_multi_aff_get_space(upma2);
5263 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5264 space = isl_union_pw_multi_aff_get_space(upma1);
5265 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5267 if (!upma1 || !upma2)
5268 goto error;
5270 data.upma2 = upma2;
5271 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
5272 upma1->table.n);
5273 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
5274 &bin_entry, &data) < 0)
5275 goto error;
5277 isl_union_pw_multi_aff_free(upma1);
5278 isl_union_pw_multi_aff_free(upma2);
5279 return data.res;
5280 error:
5281 isl_union_pw_multi_aff_free(upma1);
5282 isl_union_pw_multi_aff_free(upma2);
5283 isl_union_pw_multi_aff_free(data.res);
5284 return NULL;
5287 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5288 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5290 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5291 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5293 isl_space *space;
5295 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5296 isl_pw_multi_aff_get_space(pma2));
5297 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5298 &isl_multi_aff_range_product);
5301 /* Given two isl_pw_multi_affs A -> B and C -> D,
5302 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5304 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5305 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5307 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5308 &pw_multi_aff_range_product);
5311 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5312 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5314 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5315 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5317 isl_space *space;
5319 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5320 isl_pw_multi_aff_get_space(pma2));
5321 space = isl_space_flatten_range(space);
5322 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5323 &isl_multi_aff_flat_range_product);
5326 /* Given two isl_pw_multi_affs A -> B and C -> D,
5327 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5329 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5330 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5332 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5333 &pw_multi_aff_flat_range_product);
5336 /* If data->pma and *entry have the same domain space, then compute
5337 * their flat range product and the result to data->res.
5339 static int flat_range_product_entry(void **entry, void *user)
5341 struct isl_union_pw_multi_aff_bin_data *data = user;
5342 isl_pw_multi_aff *pma2 = *entry;
5344 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
5345 pma2->dim, isl_dim_in))
5346 return 0;
5348 pma2 = isl_pw_multi_aff_flat_range_product(
5349 isl_pw_multi_aff_copy(data->pma),
5350 isl_pw_multi_aff_copy(pma2));
5352 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5354 return 0;
5357 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5358 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5360 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5361 __isl_take isl_union_pw_multi_aff *upma1,
5362 __isl_take isl_union_pw_multi_aff *upma2)
5364 return bin_op(upma1, upma2, &flat_range_product_entry);
5367 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5368 * The parameters are assumed to have been aligned.
5370 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5371 * except that it works on two different isl_pw_* types.
5373 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5374 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5375 __isl_take isl_pw_aff *pa)
5377 int i, j, n;
5378 isl_pw_multi_aff *res = NULL;
5380 if (!pma || !pa)
5381 goto error;
5383 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
5384 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5385 "domains don't match", goto error);
5386 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5387 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5388 "index out of bounds", goto error);
5390 n = pma->n * pa->n;
5391 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5393 for (i = 0; i < pma->n; ++i) {
5394 for (j = 0; j < pa->n; ++j) {
5395 isl_set *common;
5396 isl_multi_aff *res_ij;
5397 int empty;
5399 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5400 isl_set_copy(pa->p[j].set));
5401 empty = isl_set_plain_is_empty(common);
5402 if (empty < 0 || empty) {
5403 isl_set_free(common);
5404 if (empty < 0)
5405 goto error;
5406 continue;
5409 res_ij = isl_multi_aff_set_aff(
5410 isl_multi_aff_copy(pma->p[i].maff), pos,
5411 isl_aff_copy(pa->p[j].aff));
5412 res_ij = isl_multi_aff_gist(res_ij,
5413 isl_set_copy(common));
5415 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5419 isl_pw_multi_aff_free(pma);
5420 isl_pw_aff_free(pa);
5421 return res;
5422 error:
5423 isl_pw_multi_aff_free(pma);
5424 isl_pw_aff_free(pa);
5425 return isl_pw_multi_aff_free(res);
5428 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5430 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5431 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5432 __isl_take isl_pw_aff *pa)
5434 if (!pma || !pa)
5435 goto error;
5436 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5437 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5438 if (!isl_space_has_named_params(pma->dim) ||
5439 !isl_space_has_named_params(pa->dim))
5440 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5441 "unaligned unnamed parameters", goto error);
5442 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5443 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5444 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5445 error:
5446 isl_pw_multi_aff_free(pma);
5447 isl_pw_aff_free(pa);
5448 return NULL;
5451 /* Do the parameters of "pa" match those of "space"?
5453 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5454 __isl_keep isl_space *space)
5456 isl_space *pa_space;
5457 int match;
5459 if (!pa || !space)
5460 return -1;
5462 pa_space = isl_pw_aff_get_space(pa);
5464 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5466 isl_space_free(pa_space);
5467 return match;
5470 /* Check that the domain space of "pa" matches "space".
5472 * Return 0 on success and -1 on error.
5474 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5475 __isl_keep isl_space *space)
5477 isl_space *pa_space;
5478 int match;
5480 if (!pa || !space)
5481 return -1;
5483 pa_space = isl_pw_aff_get_space(pa);
5485 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5486 if (match < 0)
5487 goto error;
5488 if (!match)
5489 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5490 "parameters don't match", goto error);
5491 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5492 if (match < 0)
5493 goto error;
5494 if (!match)
5495 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5496 "domains don't match", goto error);
5497 isl_space_free(pa_space);
5498 return 0;
5499 error:
5500 isl_space_free(pa_space);
5501 return -1;
5504 #undef BASE
5505 #define BASE pw_aff
5507 #include <isl_multi_templ.c>
5509 /* Scale the elements of "pma" by the corresponding elements of "mv".
5511 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5512 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5514 int i;
5516 pma = isl_pw_multi_aff_cow(pma);
5517 if (!pma || !mv)
5518 goto error;
5519 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5520 mv->space, isl_dim_set))
5521 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5522 "spaces don't match", goto error);
5523 if (!isl_space_match(pma->dim, isl_dim_param,
5524 mv->space, isl_dim_param)) {
5525 pma = isl_pw_multi_aff_align_params(pma,
5526 isl_multi_val_get_space(mv));
5527 mv = isl_multi_val_align_params(mv,
5528 isl_pw_multi_aff_get_space(pma));
5529 if (!pma || !mv)
5530 goto error;
5533 for (i = 0; i < pma->n; ++i) {
5534 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5535 isl_multi_val_copy(mv));
5536 if (!pma->p[i].maff)
5537 goto error;
5540 isl_multi_val_free(mv);
5541 return pma;
5542 error:
5543 isl_multi_val_free(mv);
5544 isl_pw_multi_aff_free(pma);
5545 return NULL;
5548 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5549 * mv contains the mv argument.
5550 * res collects the results.
5552 struct isl_union_pw_multi_aff_scale_multi_val_data {
5553 isl_multi_val *mv;
5554 isl_union_pw_multi_aff *res;
5557 /* This function is called for each entry of an isl_union_pw_multi_aff.
5558 * If the space of the entry matches that of data->mv,
5559 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5560 * to data->res.
5562 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5564 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5565 isl_pw_multi_aff *pma = *entry;
5567 if (!pma)
5568 return -1;
5569 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5570 data->mv->space, isl_dim_set))
5571 return 0;
5573 pma = isl_pw_multi_aff_copy(pma);
5574 pma = isl_pw_multi_aff_scale_multi_val(pma,
5575 isl_multi_val_copy(data->mv));
5576 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5577 if (!data->res)
5578 return -1;
5580 return 0;
5583 /* Scale the elements of "upma" by the corresponding elements of "mv",
5584 * for those entries that match the space of "mv".
5586 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5587 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5589 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5591 upma = isl_union_pw_multi_aff_align_params(upma,
5592 isl_multi_val_get_space(mv));
5593 mv = isl_multi_val_align_params(mv,
5594 isl_union_pw_multi_aff_get_space(upma));
5595 if (!upma || !mv)
5596 goto error;
5598 data.mv = mv;
5599 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->dim),
5600 upma->table.n);
5601 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5602 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5603 goto error;
5605 isl_multi_val_free(mv);
5606 isl_union_pw_multi_aff_free(upma);
5607 return data.res;
5608 error:
5609 isl_multi_val_free(mv);
5610 isl_union_pw_multi_aff_free(upma);
5611 return NULL;
5614 /* Construct and return a piecewise multi affine expression
5615 * in the given space with value zero in each of the output dimensions and
5616 * a universe domain.
5618 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5620 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5623 /* Construct and return a piecewise multi affine expression
5624 * that is equal to the given piecewise affine expression.
5626 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5627 __isl_take isl_pw_aff *pa)
5629 int i;
5630 isl_space *space;
5631 isl_pw_multi_aff *pma;
5633 if (!pa)
5634 return NULL;
5636 space = isl_pw_aff_get_space(pa);
5637 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5639 for (i = 0; i < pa->n; ++i) {
5640 isl_set *set;
5641 isl_multi_aff *ma;
5643 set = isl_set_copy(pa->p[i].set);
5644 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
5645 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
5648 isl_pw_aff_free(pa);
5649 return pma;
5652 /* Construct a set or map mapping the shared (parameter) domain
5653 * of the piecewise affine expressions to the range of "mpa"
5654 * with each dimension in the range equated to the
5655 * corresponding piecewise affine expression.
5657 static __isl_give isl_map *map_from_multi_pw_aff(
5658 __isl_take isl_multi_pw_aff *mpa)
5660 int i;
5661 isl_space *space;
5662 isl_map *map;
5664 if (!mpa)
5665 return NULL;
5667 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
5668 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5669 "invalid space", return isl_multi_pw_aff_free(mpa));
5671 space = isl_multi_pw_aff_get_domain_space(mpa);
5672 map = isl_map_universe(isl_space_from_domain(space));
5674 for (i = 0; i < mpa->n; ++i) {
5675 isl_pw_aff *pa;
5676 isl_map *map_i;
5678 pa = isl_pw_aff_copy(mpa->p[i]);
5679 map_i = map_from_pw_aff(pa);
5681 map = isl_map_flat_range_product(map, map_i);
5684 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
5686 isl_multi_pw_aff_free(mpa);
5687 return map;
5690 /* Construct a map mapping the shared domain
5691 * of the piecewise affine expressions to the range of "mpa"
5692 * with each dimension in the range equated to the
5693 * corresponding piecewise affine expression.
5695 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5697 if (!mpa)
5698 return NULL;
5699 if (isl_space_is_set(mpa->space))
5700 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5701 "space of input is not a map", goto error);
5703 return map_from_multi_pw_aff(mpa);
5704 error:
5705 isl_multi_pw_aff_free(mpa);
5706 return NULL;
5709 /* Construct a set mapping the shared parameter domain
5710 * of the piecewise affine expressions to the space of "mpa"
5711 * with each dimension in the range equated to the
5712 * corresponding piecewise affine expression.
5714 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5716 if (!mpa)
5717 return NULL;
5718 if (!isl_space_is_set(mpa->space))
5719 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5720 "space of input is not a set", goto error);
5722 return map_from_multi_pw_aff(mpa);
5723 error:
5724 isl_multi_pw_aff_free(mpa);
5725 return NULL;
5728 /* Construct and return a piecewise multi affine expression
5729 * that is equal to the given multi piecewise affine expression
5730 * on the shared domain of the piecewise affine expressions.
5732 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
5733 __isl_take isl_multi_pw_aff *mpa)
5735 int i;
5736 isl_space *space;
5737 isl_pw_aff *pa;
5738 isl_pw_multi_aff *pma;
5740 if (!mpa)
5741 return NULL;
5743 space = isl_multi_pw_aff_get_space(mpa);
5745 if (mpa->n == 0) {
5746 isl_multi_pw_aff_free(mpa);
5747 return isl_pw_multi_aff_zero(space);
5750 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
5751 pma = isl_pw_multi_aff_from_pw_aff(pa);
5753 for (i = 1; i < mpa->n; ++i) {
5754 isl_pw_multi_aff *pma_i;
5756 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
5757 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
5758 pma = isl_pw_multi_aff_range_product(pma, pma_i);
5761 pma = isl_pw_multi_aff_reset_space(pma, space);
5763 isl_multi_pw_aff_free(mpa);
5764 return pma;
5767 /* Construct and return a multi piecewise affine expression
5768 * that is equal to the given multi affine expression.
5770 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
5771 __isl_take isl_multi_aff *ma)
5773 int i, n;
5774 isl_multi_pw_aff *mpa;
5776 if (!ma)
5777 return NULL;
5779 n = isl_multi_aff_dim(ma, isl_dim_out);
5780 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
5782 for (i = 0; i < n; ++i) {
5783 isl_pw_aff *pa;
5785 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
5786 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5789 isl_multi_aff_free(ma);
5790 return mpa;
5793 /* Construct and return a multi piecewise affine expression
5794 * that is equal to the given piecewise multi affine expression.
5796 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
5797 __isl_take isl_pw_multi_aff *pma)
5799 int i, n;
5800 isl_space *space;
5801 isl_multi_pw_aff *mpa;
5803 if (!pma)
5804 return NULL;
5806 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
5807 space = isl_pw_multi_aff_get_space(pma);
5808 mpa = isl_multi_pw_aff_alloc(space);
5810 for (i = 0; i < n; ++i) {
5811 isl_pw_aff *pa;
5813 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
5814 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5817 isl_pw_multi_aff_free(pma);
5818 return mpa;
5821 /* Do "pa1" and "pa2" represent the same function?
5823 * We first check if they are obviously equal.
5824 * If not, we convert them to maps and check if those are equal.
5826 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
5828 int equal;
5829 isl_map *map1, *map2;
5831 if (!pa1 || !pa2)
5832 return -1;
5834 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
5835 if (equal < 0 || equal)
5836 return equal;
5838 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
5839 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
5840 equal = isl_map_is_equal(map1, map2);
5841 isl_map_free(map1);
5842 isl_map_free(map2);
5844 return equal;
5847 /* Do "mpa1" and "mpa2" represent the same function?
5849 * Note that we cannot convert the entire isl_multi_pw_aff
5850 * to a map because the domains of the piecewise affine expressions
5851 * may not be the same.
5853 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
5854 __isl_keep isl_multi_pw_aff *mpa2)
5856 int i;
5857 int equal;
5859 if (!mpa1 || !mpa2)
5860 return -1;
5862 if (!isl_space_match(mpa1->space, isl_dim_param,
5863 mpa2->space, isl_dim_param)) {
5864 if (!isl_space_has_named_params(mpa1->space))
5865 return 0;
5866 if (!isl_space_has_named_params(mpa2->space))
5867 return 0;
5868 mpa1 = isl_multi_pw_aff_copy(mpa1);
5869 mpa2 = isl_multi_pw_aff_copy(mpa2);
5870 mpa1 = isl_multi_pw_aff_align_params(mpa1,
5871 isl_multi_pw_aff_get_space(mpa2));
5872 mpa2 = isl_multi_pw_aff_align_params(mpa2,
5873 isl_multi_pw_aff_get_space(mpa1));
5874 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
5875 isl_multi_pw_aff_free(mpa1);
5876 isl_multi_pw_aff_free(mpa2);
5877 return equal;
5880 equal = isl_space_is_equal(mpa1->space, mpa2->space);
5881 if (equal < 0 || !equal)
5882 return equal;
5884 for (i = 0; i < mpa1->n; ++i) {
5885 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
5886 if (equal < 0 || !equal)
5887 return equal;
5890 return 1;
5893 /* Coalesce the elements of "mpa".
5895 * Note that such coalescing does not change the meaning of "mpa"
5896 * so there is no need to cow. We do need to be careful not to
5897 * destroy any other copies of "mpa" in case of failure.
5899 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
5900 __isl_take isl_multi_pw_aff *mpa)
5902 int i;
5904 if (!mpa)
5905 return NULL;
5907 for (i = 0; i < mpa->n; ++i) {
5908 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
5909 pa = isl_pw_aff_coalesce(pa);
5910 if (!pa)
5911 return isl_multi_pw_aff_free(mpa);
5912 isl_pw_aff_free(mpa->p[i]);
5913 mpa->p[i] = pa;
5916 return mpa;
5919 /* Compute the pullback of "mpa" by the function represented by "ma".
5920 * In other words, plug in "ma" in "mpa".
5922 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5924 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
5925 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5927 int i;
5928 isl_space *space = NULL;
5930 mpa = isl_multi_pw_aff_cow(mpa);
5931 if (!mpa || !ma)
5932 goto error;
5934 space = isl_space_join(isl_multi_aff_get_space(ma),
5935 isl_multi_pw_aff_get_space(mpa));
5936 if (!space)
5937 goto error;
5939 for (i = 0; i < mpa->n; ++i) {
5940 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
5941 isl_multi_aff_copy(ma));
5942 if (!mpa->p[i])
5943 goto error;
5946 isl_multi_aff_free(ma);
5947 isl_space_free(mpa->space);
5948 mpa->space = space;
5949 return mpa;
5950 error:
5951 isl_space_free(space);
5952 isl_multi_pw_aff_free(mpa);
5953 isl_multi_aff_free(ma);
5954 return NULL;
5957 /* Compute the pullback of "mpa" by the function represented by "ma".
5958 * In other words, plug in "ma" in "mpa".
5960 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
5961 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5963 if (!mpa || !ma)
5964 goto error;
5965 if (isl_space_match(mpa->space, isl_dim_param,
5966 ma->space, isl_dim_param))
5967 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5968 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
5969 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
5970 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5971 error:
5972 isl_multi_pw_aff_free(mpa);
5973 isl_multi_aff_free(ma);
5974 return NULL;
5977 /* Compute the pullback of "mpa" by the function represented by "pma".
5978 * In other words, plug in "pma" in "mpa".
5980 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5982 static __isl_give isl_multi_pw_aff *
5983 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5984 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
5986 int i;
5987 isl_space *space = NULL;
5989 mpa = isl_multi_pw_aff_cow(mpa);
5990 if (!mpa || !pma)
5991 goto error;
5993 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
5994 isl_multi_pw_aff_get_space(mpa));
5996 for (i = 0; i < mpa->n; ++i) {
5997 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
5998 isl_pw_multi_aff_copy(pma));
5999 if (!mpa->p[i])
6000 goto error;
6003 isl_pw_multi_aff_free(pma);
6004 isl_space_free(mpa->space);
6005 mpa->space = space;
6006 return mpa;
6007 error:
6008 isl_space_free(space);
6009 isl_multi_pw_aff_free(mpa);
6010 isl_pw_multi_aff_free(pma);
6011 return NULL;
6014 /* Compute the pullback of "mpa" by the function represented by "pma".
6015 * In other words, plug in "pma" in "mpa".
6017 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6018 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6020 if (!mpa || !pma)
6021 goto error;
6022 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6023 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6024 mpa = isl_multi_pw_aff_align_params(mpa,
6025 isl_pw_multi_aff_get_space(pma));
6026 pma = isl_pw_multi_aff_align_params(pma,
6027 isl_multi_pw_aff_get_space(mpa));
6028 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6029 error:
6030 isl_multi_pw_aff_free(mpa);
6031 isl_pw_multi_aff_free(pma);
6032 return NULL;
6035 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6036 * with the domain of "aff". The domain of the result is the same
6037 * as that of "mpa".
6038 * "mpa" and "aff" are assumed to have been aligned.
6040 * We first extract the parametric constant from "aff", defined
6041 * over the correct domain.
6042 * Then we add the appropriate combinations of the members of "mpa".
6043 * Finally, we add the integer divisions through recursive calls.
6045 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6046 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6048 int i, n_param, n_in, n_div;
6049 isl_space *space;
6050 isl_val *v;
6051 isl_pw_aff *pa;
6052 isl_aff *tmp;
6054 n_param = isl_aff_dim(aff, isl_dim_param);
6055 n_in = isl_aff_dim(aff, isl_dim_in);
6056 n_div = isl_aff_dim(aff, isl_dim_div);
6058 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6059 tmp = isl_aff_copy(aff);
6060 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6061 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6062 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6063 isl_space_dim(space, isl_dim_set));
6064 tmp = isl_aff_reset_domain_space(tmp, space);
6065 pa = isl_pw_aff_from_aff(tmp);
6067 for (i = 0; i < n_in; ++i) {
6068 isl_pw_aff *pa_i;
6070 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6071 continue;
6072 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6073 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6074 pa_i = isl_pw_aff_scale_val(pa_i, v);
6075 pa = isl_pw_aff_add(pa, pa_i);
6078 for (i = 0; i < n_div; ++i) {
6079 isl_aff *div;
6080 isl_pw_aff *pa_i;
6082 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6083 continue;
6084 div = isl_aff_get_div(aff, i);
6085 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6086 isl_multi_pw_aff_copy(mpa), div);
6087 pa_i = isl_pw_aff_floor(pa_i);
6088 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6089 pa_i = isl_pw_aff_scale_val(pa_i, v);
6090 pa = isl_pw_aff_add(pa, pa_i);
6093 isl_multi_pw_aff_free(mpa);
6094 isl_aff_free(aff);
6096 return pa;
6099 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6100 * with the domain of "aff". The domain of the result is the same
6101 * as that of "mpa".
6103 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6104 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6106 if (!aff || !mpa)
6107 goto error;
6108 if (isl_space_match(aff->ls->dim, isl_dim_param,
6109 mpa->space, isl_dim_param))
6110 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6112 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6113 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6115 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6116 error:
6117 isl_aff_free(aff);
6118 isl_multi_pw_aff_free(mpa);
6119 return NULL;
6122 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6123 * with the domain of "pa". The domain of the result is the same
6124 * as that of "mpa".
6125 * "mpa" and "pa" are assumed to have been aligned.
6127 * We consider each piece in turn. Note that the domains of the
6128 * pieces are assumed to be disjoint and they remain disjoint
6129 * after taking the preimage (over the same function).
6131 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6132 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6134 isl_space *space;
6135 isl_pw_aff *res;
6136 int i;
6138 if (!mpa || !pa)
6139 goto error;
6141 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6142 isl_pw_aff_get_space(pa));
6143 res = isl_pw_aff_empty(space);
6145 for (i = 0; i < pa->n; ++i) {
6146 isl_pw_aff *pa_i;
6147 isl_set *domain;
6149 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6150 isl_multi_pw_aff_copy(mpa),
6151 isl_aff_copy(pa->p[i].aff));
6152 domain = isl_set_copy(pa->p[i].set);
6153 domain = isl_set_preimage_multi_pw_aff(domain,
6154 isl_multi_pw_aff_copy(mpa));
6155 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6156 res = isl_pw_aff_add_disjoint(res, pa_i);
6159 isl_pw_aff_free(pa);
6160 isl_multi_pw_aff_free(mpa);
6161 return res;
6162 error:
6163 isl_pw_aff_free(pa);
6164 isl_multi_pw_aff_free(mpa);
6165 return NULL;
6168 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6169 * with the domain of "pa". The domain of the result is the same
6170 * as that of "mpa".
6172 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6173 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6175 if (!pa || !mpa)
6176 goto error;
6177 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6178 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6180 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6181 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6183 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6184 error:
6185 isl_pw_aff_free(pa);
6186 isl_multi_pw_aff_free(mpa);
6187 return NULL;
6190 /* Compute the pullback of "pa" by the function represented by "mpa".
6191 * In other words, plug in "mpa" in "pa".
6192 * "pa" and "mpa" are assumed to have been aligned.
6194 * The pullback is computed by applying "pa" to "mpa".
6196 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6197 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6199 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6202 /* Compute the pullback of "pa" by the function represented by "mpa".
6203 * In other words, plug in "mpa" in "pa".
6205 * The pullback is computed by applying "pa" to "mpa".
6207 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6208 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6210 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6213 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6214 * In other words, plug in "mpa2" in "mpa1".
6216 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6218 * We pullback each member of "mpa1" in turn.
6220 static __isl_give isl_multi_pw_aff *
6221 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6222 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6224 int i;
6225 isl_space *space = NULL;
6227 mpa1 = isl_multi_pw_aff_cow(mpa1);
6228 if (!mpa1 || !mpa2)
6229 goto error;
6231 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6232 isl_multi_pw_aff_get_space(mpa1));
6234 for (i = 0; i < mpa1->n; ++i) {
6235 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6236 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6237 if (!mpa1->p[i])
6238 goto error;
6241 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6243 isl_multi_pw_aff_free(mpa2);
6244 return mpa1;
6245 error:
6246 isl_space_free(space);
6247 isl_multi_pw_aff_free(mpa1);
6248 isl_multi_pw_aff_free(mpa2);
6249 return NULL;
6252 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6253 * In other words, plug in "mpa2" in "mpa1".
6255 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6256 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6258 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6259 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);