isl_space_free: rename dim variable to space
[isl.git] / isl_aff.c
blob9fe7a0fdd2dcf72ca8a764f54297cbf2ae576ef1
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 pos += isl_local_space_offset(aff->ls, type);
791 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
792 return aff;
794 aff = isl_aff_cow(aff);
795 if (!aff)
796 return NULL;
798 aff->v = isl_vec_cow(aff->v);
799 if (!aff->v)
800 return isl_aff_free(aff);
802 isl_int_set_si(aff->v->el[1 + pos], v);
804 return aff;
807 /* Replace the coefficient of the variable of type "type" at position "pos"
808 * of "aff" by "v".
810 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
811 enum isl_dim_type type, int pos, __isl_take isl_val *v)
813 if (!aff || !v)
814 goto error;
816 if (type == isl_dim_out)
817 isl_die(aff->v->ctx, isl_error_invalid,
818 "output/set dimension does not have a coefficient",
819 goto error);
820 if (type == isl_dim_in)
821 type = isl_dim_set;
823 if (pos >= isl_local_space_dim(aff->ls, type))
824 isl_die(aff->v->ctx, isl_error_invalid,
825 "position out of bounds", goto error);
827 if (!isl_val_is_rat(v))
828 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
829 "expecting rational value", goto error);
831 pos += isl_local_space_offset(aff->ls, type);
832 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
833 isl_int_eq(aff->v->el[0], v->d)) {
834 isl_val_free(v);
835 return aff;
838 aff = isl_aff_cow(aff);
839 if (!aff)
840 goto error;
841 aff->v = isl_vec_cow(aff->v);
842 if (!aff->v)
843 goto error;
845 if (isl_int_eq(aff->v->el[0], v->d)) {
846 isl_int_set(aff->v->el[1 + pos], v->n);
847 } else if (isl_int_is_one(v->d)) {
848 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
849 } else {
850 isl_seq_scale(aff->v->el + 1,
851 aff->v->el + 1, v->d, aff->v->size - 1);
852 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
853 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
854 aff->v = isl_vec_normalize(aff->v);
855 if (!aff->v)
856 goto error;
859 isl_val_free(v);
860 return aff;
861 error:
862 isl_aff_free(aff);
863 isl_val_free(v);
864 return NULL;
867 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
868 enum isl_dim_type type, int pos, isl_int v)
870 if (!aff)
871 return NULL;
873 if (type == isl_dim_out)
874 isl_die(aff->v->ctx, isl_error_invalid,
875 "output/set dimension does not have a coefficient",
876 return isl_aff_free(aff));
877 if (type == isl_dim_in)
878 type = isl_dim_set;
880 if (pos >= isl_local_space_dim(aff->ls, type))
881 isl_die(aff->v->ctx, isl_error_invalid,
882 "position out of bounds", return isl_aff_free(aff));
884 aff = isl_aff_cow(aff);
885 if (!aff)
886 return NULL;
888 aff->v = isl_vec_cow(aff->v);
889 if (!aff->v)
890 return isl_aff_free(aff);
892 pos += isl_local_space_offset(aff->ls, type);
893 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
895 return aff;
898 /* Add "v" to the coefficient of the variable of type "type"
899 * at position "pos" of "aff".
901 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
902 enum isl_dim_type type, int pos, __isl_take isl_val *v)
904 if (!aff || !v)
905 goto error;
907 if (isl_val_is_zero(v)) {
908 isl_val_free(v);
909 return aff;
912 if (type == isl_dim_out)
913 isl_die(aff->v->ctx, isl_error_invalid,
914 "output/set dimension does not have a coefficient",
915 goto error);
916 if (type == isl_dim_in)
917 type = isl_dim_set;
919 if (pos >= isl_local_space_dim(aff->ls, type))
920 isl_die(aff->v->ctx, isl_error_invalid,
921 "position out of bounds", goto error);
923 if (!isl_val_is_rat(v))
924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
925 "expecting rational value", goto error);
927 aff = isl_aff_cow(aff);
928 if (!aff)
929 goto error;
931 aff->v = isl_vec_cow(aff->v);
932 if (!aff->v)
933 goto error;
935 pos += isl_local_space_offset(aff->ls, type);
936 if (isl_int_is_one(v->d)) {
937 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
938 } else if (isl_int_eq(aff->v->el[0], v->d)) {
939 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
940 aff->v = isl_vec_normalize(aff->v);
941 if (!aff->v)
942 goto error;
943 } else {
944 isl_seq_scale(aff->v->el + 1,
945 aff->v->el + 1, v->d, aff->v->size - 1);
946 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
947 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
948 aff->v = isl_vec_normalize(aff->v);
949 if (!aff->v)
950 goto error;
953 isl_val_free(v);
954 return aff;
955 error:
956 isl_aff_free(aff);
957 isl_val_free(v);
958 return NULL;
961 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
962 enum isl_dim_type type, int pos, int v)
964 isl_int t;
966 isl_int_init(t);
967 isl_int_set_si(t, v);
968 aff = isl_aff_add_coefficient(aff, type, pos, t);
969 isl_int_clear(t);
971 return aff;
974 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
976 if (!aff)
977 return NULL;
979 return isl_local_space_get_div(aff->ls, pos);
982 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
984 aff = isl_aff_cow(aff);
985 if (!aff)
986 return NULL;
987 aff->v = isl_vec_cow(aff->v);
988 if (!aff->v)
989 return isl_aff_free(aff);
991 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
993 return aff;
996 /* Remove divs from the local space that do not appear in the affine
997 * expression.
998 * We currently only remove divs at the end.
999 * Some intermediate divs may also not appear directly in the affine
1000 * expression, but we would also need to check that no other divs are
1001 * defined in terms of them.
1003 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1005 int pos;
1006 int off;
1007 int n;
1009 if (!aff)
1010 return NULL;
1012 n = isl_local_space_dim(aff->ls, isl_dim_div);
1013 off = isl_local_space_offset(aff->ls, isl_dim_div);
1015 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1016 if (pos == n)
1017 return aff;
1019 aff = isl_aff_cow(aff);
1020 if (!aff)
1021 return NULL;
1023 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1024 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1025 if (!aff->ls || !aff->v)
1026 return isl_aff_free(aff);
1028 return aff;
1031 /* Given two affine expressions "p" of length p_len (including the
1032 * denominator and the constant term) and "subs" of length subs_len,
1033 * plug in "subs" for the variable at position "pos".
1034 * The variables of "subs" and "p" are assumed to match up to subs_len,
1035 * but "p" may have additional variables.
1036 * "v" is an initialized isl_int that can be used internally.
1038 * In particular, if "p" represents the expression
1040 * (a i + g)/m
1042 * with i the variable at position "pos" and "subs" represents the expression
1044 * f/d
1046 * then the result represents the expression
1048 * (a f + d g)/(m d)
1051 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1052 int p_len, int subs_len, isl_int v)
1054 isl_int_set(v, p[1 + pos]);
1055 isl_int_set_si(p[1 + pos], 0);
1056 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1057 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1058 isl_int_mul(p[0], p[0], subs[0]);
1061 /* Look for any divs in the aff->ls with a denominator equal to one
1062 * and plug them into the affine expression and any subsequent divs
1063 * that may reference the div.
1065 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1067 int i, n;
1068 int len;
1069 isl_int v;
1070 isl_vec *vec;
1071 isl_local_space *ls;
1072 unsigned pos;
1074 if (!aff)
1075 return NULL;
1077 n = isl_local_space_dim(aff->ls, isl_dim_div);
1078 len = aff->v->size;
1079 for (i = 0; i < n; ++i) {
1080 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1081 continue;
1082 ls = isl_local_space_copy(aff->ls);
1083 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1084 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1085 vec = isl_vec_copy(aff->v);
1086 vec = isl_vec_cow(vec);
1087 if (!ls || !vec)
1088 goto error;
1090 isl_int_init(v);
1092 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1093 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1094 len, len, v);
1096 isl_int_clear(v);
1098 isl_vec_free(aff->v);
1099 aff->v = vec;
1100 isl_local_space_free(aff->ls);
1101 aff->ls = ls;
1104 return aff;
1105 error:
1106 isl_vec_free(vec);
1107 isl_local_space_free(ls);
1108 return isl_aff_free(aff);
1111 /* Look for any divs j that appear with a unit coefficient inside
1112 * the definitions of other divs i and plug them into the definitions
1113 * of the divs i.
1115 * In particular, an expression of the form
1117 * floor((f(..) + floor(g(..)/n))/m)
1119 * is simplified to
1121 * floor((n * f(..) + g(..))/(n * m))
1123 * This simplification is correct because we can move the expression
1124 * f(..) into the inner floor in the original expression to obtain
1126 * floor(floor((n * f(..) + g(..))/n)/m)
1128 * from which we can derive the simplified expression.
1130 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1132 int i, j, n;
1133 int off;
1135 if (!aff)
1136 return NULL;
1138 n = isl_local_space_dim(aff->ls, isl_dim_div);
1139 off = isl_local_space_offset(aff->ls, isl_dim_div);
1140 for (i = 1; i < n; ++i) {
1141 for (j = 0; j < i; ++j) {
1142 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1143 continue;
1144 aff->ls = isl_local_space_substitute_seq(aff->ls,
1145 isl_dim_div, j, aff->ls->div->row[j],
1146 aff->v->size, i, 1);
1147 if (!aff->ls)
1148 return isl_aff_free(aff);
1152 return aff;
1155 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1157 * Even though this function is only called on isl_affs with a single
1158 * reference, we are careful to only change aff->v and aff->ls together.
1160 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1162 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1163 isl_local_space *ls;
1164 isl_vec *v;
1166 ls = isl_local_space_copy(aff->ls);
1167 ls = isl_local_space_swap_div(ls, a, b);
1168 v = isl_vec_copy(aff->v);
1169 v = isl_vec_cow(v);
1170 if (!ls || !v)
1171 goto error;
1173 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1174 isl_vec_free(aff->v);
1175 aff->v = v;
1176 isl_local_space_free(aff->ls);
1177 aff->ls = ls;
1179 return aff;
1180 error:
1181 isl_vec_free(v);
1182 isl_local_space_free(ls);
1183 return isl_aff_free(aff);
1186 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1188 * We currently do not actually remove div "b", but simply add its
1189 * coefficient to that of "a" and then zero it out.
1191 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1193 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1195 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1196 return aff;
1198 aff->v = isl_vec_cow(aff->v);
1199 if (!aff->v)
1200 return isl_aff_free(aff);
1202 isl_int_add(aff->v->el[1 + off + a],
1203 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1204 isl_int_set_si(aff->v->el[1 + off + b], 0);
1206 return aff;
1209 /* Sort the divs in the local space of "aff" according to
1210 * the comparison function "cmp_row" in isl_local_space.c,
1211 * combining the coefficients of identical divs.
1213 * Reordering divs does not change the semantics of "aff",
1214 * so there is no need to call isl_aff_cow.
1215 * Moreover, this function is currently only called on isl_affs
1216 * with a single reference.
1218 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1220 int i, j, n;
1221 unsigned off;
1223 if (!aff)
1224 return NULL;
1226 off = isl_local_space_offset(aff->ls, isl_dim_div);
1227 n = isl_aff_dim(aff, isl_dim_div);
1228 for (i = 1; i < n; ++i) {
1229 for (j = i - 1; j >= 0; --j) {
1230 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1231 if (cmp < 0)
1232 break;
1233 if (cmp == 0)
1234 aff = merge_divs(aff, j, j + 1);
1235 else
1236 aff = swap_div(aff, j, j + 1);
1237 if (!aff)
1238 return NULL;
1242 return aff;
1245 /* Normalize the representation of "aff".
1247 * This function should only be called of "new" isl_affs, i.e.,
1248 * with only a single reference. We therefore do not need to
1249 * worry about affecting other instances.
1251 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1253 if (!aff)
1254 return NULL;
1255 aff->v = isl_vec_normalize(aff->v);
1256 if (!aff->v)
1257 return isl_aff_free(aff);
1258 aff = plug_in_integral_divs(aff);
1259 aff = plug_in_unit_divs(aff);
1260 aff = sort_divs(aff);
1261 aff = isl_aff_remove_unused_divs(aff);
1262 return aff;
1265 /* Given f, return floor(f).
1266 * If f is an integer expression, then just return f.
1267 * If f is a constant, then return the constant floor(f).
1268 * Otherwise, if f = g/m, write g = q m + r,
1269 * create a new div d = [r/m] and return the expression q + d.
1270 * The coefficients in r are taken to lie between -m/2 and m/2.
1272 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1274 int i;
1275 int size;
1276 isl_ctx *ctx;
1277 isl_vec *div;
1279 if (!aff)
1280 return NULL;
1282 if (isl_int_is_one(aff->v->el[0]))
1283 return aff;
1285 aff = isl_aff_cow(aff);
1286 if (!aff)
1287 return NULL;
1289 aff->v = isl_vec_cow(aff->v);
1290 if (!aff->v)
1291 return isl_aff_free(aff);
1293 if (isl_aff_is_cst(aff)) {
1294 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1295 isl_int_set_si(aff->v->el[0], 1);
1296 return aff;
1299 div = isl_vec_copy(aff->v);
1300 div = isl_vec_cow(div);
1301 if (!div)
1302 return isl_aff_free(aff);
1304 ctx = isl_aff_get_ctx(aff);
1305 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1306 for (i = 1; i < aff->v->size; ++i) {
1307 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1308 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1309 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1310 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1311 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1315 aff->ls = isl_local_space_add_div(aff->ls, div);
1316 if (!aff->ls)
1317 return isl_aff_free(aff);
1319 size = aff->v->size;
1320 aff->v = isl_vec_extend(aff->v, size + 1);
1321 if (!aff->v)
1322 return isl_aff_free(aff);
1323 isl_int_set_si(aff->v->el[0], 1);
1324 isl_int_set_si(aff->v->el[size], 1);
1326 aff = isl_aff_normalize(aff);
1328 return aff;
1331 /* Compute
1333 * aff mod m = aff - m * floor(aff/m)
1335 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1337 isl_aff *res;
1339 res = isl_aff_copy(aff);
1340 aff = isl_aff_scale_down(aff, m);
1341 aff = isl_aff_floor(aff);
1342 aff = isl_aff_scale(aff, m);
1343 res = isl_aff_sub(res, aff);
1345 return res;
1348 /* Compute
1350 * aff mod m = aff - m * floor(aff/m)
1352 * with m an integer value.
1354 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1355 __isl_take isl_val *m)
1357 isl_aff *res;
1359 if (!aff || !m)
1360 goto error;
1362 if (!isl_val_is_int(m))
1363 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1364 "expecting integer modulo", goto error);
1366 res = isl_aff_copy(aff);
1367 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1368 aff = isl_aff_floor(aff);
1369 aff = isl_aff_scale_val(aff, m);
1370 res = isl_aff_sub(res, aff);
1372 return res;
1373 error:
1374 isl_aff_free(aff);
1375 isl_val_free(m);
1376 return NULL;
1379 /* Compute
1381 * pwaff mod m = pwaff - m * floor(pwaff/m)
1383 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1385 isl_pw_aff *res;
1387 res = isl_pw_aff_copy(pwaff);
1388 pwaff = isl_pw_aff_scale_down(pwaff, m);
1389 pwaff = isl_pw_aff_floor(pwaff);
1390 pwaff = isl_pw_aff_scale(pwaff, m);
1391 res = isl_pw_aff_sub(res, pwaff);
1393 return res;
1396 /* Compute
1398 * pa mod m = pa - m * floor(pa/m)
1400 * with m an integer value.
1402 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1403 __isl_take isl_val *m)
1405 if (!pa || !m)
1406 goto error;
1407 if (!isl_val_is_int(m))
1408 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1409 "expecting integer modulo", goto error);
1410 pa = isl_pw_aff_mod(pa, m->n);
1411 isl_val_free(m);
1412 return pa;
1413 error:
1414 isl_pw_aff_free(pa);
1415 isl_val_free(m);
1416 return NULL;
1419 /* Given f, return ceil(f).
1420 * If f is an integer expression, then just return f.
1421 * Otherwise, let f be the expression
1423 * e/m
1425 * then return
1427 * floor((e + m - 1)/m)
1429 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1431 if (!aff)
1432 return NULL;
1434 if (isl_int_is_one(aff->v->el[0]))
1435 return aff;
1437 aff = isl_aff_cow(aff);
1438 if (!aff)
1439 return NULL;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1445 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1446 aff = isl_aff_floor(aff);
1448 return aff;
1451 /* Apply the expansion computed by isl_merge_divs.
1452 * The expansion itself is given by "exp" while the resulting
1453 * list of divs is given by "div".
1455 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1456 __isl_take isl_mat *div, int *exp)
1458 int i, j;
1459 int old_n_div;
1460 int new_n_div;
1461 int offset;
1463 aff = isl_aff_cow(aff);
1464 if (!aff || !div)
1465 goto error;
1467 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1468 new_n_div = isl_mat_rows(div);
1469 if (new_n_div < old_n_div)
1470 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1471 "not an expansion", goto error);
1473 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1474 if (!aff->v)
1475 goto error;
1477 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1478 j = old_n_div - 1;
1479 for (i = new_n_div - 1; i >= 0; --i) {
1480 if (j >= 0 && exp[j] == i) {
1481 if (i != j)
1482 isl_int_swap(aff->v->el[offset + i],
1483 aff->v->el[offset + j]);
1484 j--;
1485 } else
1486 isl_int_set_si(aff->v->el[offset + i], 0);
1489 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1490 if (!aff->ls)
1491 goto error;
1492 isl_mat_free(div);
1493 return aff;
1494 error:
1495 isl_aff_free(aff);
1496 isl_mat_free(div);
1497 return NULL;
1500 /* Add two affine expressions that live in the same local space.
1502 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1503 __isl_take isl_aff *aff2)
1505 isl_int gcd, f;
1507 aff1 = isl_aff_cow(aff1);
1508 if (!aff1 || !aff2)
1509 goto error;
1511 aff1->v = isl_vec_cow(aff1->v);
1512 if (!aff1->v)
1513 goto error;
1515 isl_int_init(gcd);
1516 isl_int_init(f);
1517 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1518 isl_int_divexact(f, aff2->v->el[0], gcd);
1519 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1520 isl_int_divexact(f, aff1->v->el[0], gcd);
1521 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1522 isl_int_divexact(f, aff2->v->el[0], gcd);
1523 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1524 isl_int_clear(f);
1525 isl_int_clear(gcd);
1527 isl_aff_free(aff2);
1528 return aff1;
1529 error:
1530 isl_aff_free(aff1);
1531 isl_aff_free(aff2);
1532 return NULL;
1535 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1536 __isl_take isl_aff *aff2)
1538 isl_ctx *ctx;
1539 int *exp1 = NULL;
1540 int *exp2 = NULL;
1541 isl_mat *div;
1542 int n_div1, n_div2;
1544 if (!aff1 || !aff2)
1545 goto error;
1547 ctx = isl_aff_get_ctx(aff1);
1548 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1549 isl_die(ctx, isl_error_invalid,
1550 "spaces don't match", goto error);
1552 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1553 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1554 if (n_div1 == 0 && n_div2 == 0)
1555 return add_expanded(aff1, aff2);
1557 exp1 = isl_alloc_array(ctx, int, n_div1);
1558 exp2 = isl_alloc_array(ctx, int, n_div2);
1559 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1560 goto error;
1562 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1563 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1564 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1565 free(exp1);
1566 free(exp2);
1568 return add_expanded(aff1, aff2);
1569 error:
1570 free(exp1);
1571 free(exp2);
1572 isl_aff_free(aff1);
1573 isl_aff_free(aff2);
1574 return NULL;
1577 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1578 __isl_take isl_aff *aff2)
1580 return isl_aff_add(aff1, isl_aff_neg(aff2));
1583 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1585 isl_int gcd;
1587 if (isl_int_is_one(f))
1588 return aff;
1590 aff = isl_aff_cow(aff);
1591 if (!aff)
1592 return NULL;
1593 aff->v = isl_vec_cow(aff->v);
1594 if (!aff->v)
1595 return isl_aff_free(aff);
1597 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1598 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1599 return aff;
1602 isl_int_init(gcd);
1603 isl_int_gcd(gcd, aff->v->el[0], f);
1604 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1605 isl_int_divexact(gcd, f, gcd);
1606 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1607 isl_int_clear(gcd);
1609 return aff;
1612 /* Multiple "aff" by "v".
1614 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1615 __isl_take isl_val *v)
1617 if (!aff || !v)
1618 goto error;
1620 if (isl_val_is_one(v)) {
1621 isl_val_free(v);
1622 return aff;
1625 if (!isl_val_is_rat(v))
1626 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1627 "expecting rational factor", goto error);
1629 aff = isl_aff_scale(aff, v->n);
1630 aff = isl_aff_scale_down(aff, v->d);
1632 isl_val_free(v);
1633 return aff;
1634 error:
1635 isl_aff_free(aff);
1636 isl_val_free(v);
1637 return NULL;
1640 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1642 isl_int gcd;
1644 if (isl_int_is_one(f))
1645 return aff;
1647 aff = isl_aff_cow(aff);
1648 if (!aff)
1649 return NULL;
1651 if (isl_int_is_zero(f))
1652 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1653 "cannot scale down by zero", return isl_aff_free(aff));
1655 aff->v = isl_vec_cow(aff->v);
1656 if (!aff->v)
1657 return isl_aff_free(aff);
1659 isl_int_init(gcd);
1660 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1661 isl_int_gcd(gcd, gcd, f);
1662 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1663 isl_int_divexact(gcd, f, gcd);
1664 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1665 isl_int_clear(gcd);
1667 return aff;
1670 /* Divide "aff" by "v".
1672 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1673 __isl_take isl_val *v)
1675 if (!aff || !v)
1676 goto error;
1678 if (isl_val_is_one(v)) {
1679 isl_val_free(v);
1680 return aff;
1683 if (!isl_val_is_rat(v))
1684 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1685 "expecting rational factor", goto error);
1686 if (!isl_val_is_pos(v))
1687 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1688 "factor needs to be positive", goto error);
1690 aff = isl_aff_scale(aff, v->d);
1691 aff = isl_aff_scale_down(aff, v->n);
1693 isl_val_free(v);
1694 return aff;
1695 error:
1696 isl_aff_free(aff);
1697 isl_val_free(v);
1698 return NULL;
1701 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1703 isl_int v;
1705 if (f == 1)
1706 return aff;
1708 isl_int_init(v);
1709 isl_int_set_ui(v, f);
1710 aff = isl_aff_scale_down(aff, v);
1711 isl_int_clear(v);
1713 return aff;
1716 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1717 enum isl_dim_type type, unsigned pos, const char *s)
1719 aff = isl_aff_cow(aff);
1720 if (!aff)
1721 return NULL;
1722 if (type == isl_dim_out)
1723 isl_die(aff->v->ctx, isl_error_invalid,
1724 "cannot set name of output/set dimension",
1725 return isl_aff_free(aff));
1726 if (type == isl_dim_in)
1727 type = isl_dim_set;
1728 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1729 if (!aff->ls)
1730 return isl_aff_free(aff);
1732 return aff;
1735 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1736 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1738 aff = isl_aff_cow(aff);
1739 if (!aff)
1740 return isl_id_free(id);
1741 if (type == isl_dim_out)
1742 isl_die(aff->v->ctx, isl_error_invalid,
1743 "cannot set name of output/set dimension",
1744 goto error);
1745 if (type == isl_dim_in)
1746 type = isl_dim_set;
1747 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1748 if (!aff->ls)
1749 return isl_aff_free(aff);
1751 return aff;
1752 error:
1753 isl_id_free(id);
1754 isl_aff_free(aff);
1755 return NULL;
1758 /* Replace the identifier of the input tuple of "aff" by "id".
1759 * type is currently required to be equal to isl_dim_in
1761 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1762 enum isl_dim_type type, __isl_take isl_id *id)
1764 aff = isl_aff_cow(aff);
1765 if (!aff)
1766 return isl_id_free(id);
1767 if (type != isl_dim_out)
1768 isl_die(aff->v->ctx, isl_error_invalid,
1769 "cannot only set id of input tuple", goto error);
1770 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1771 if (!aff->ls)
1772 return isl_aff_free(aff);
1774 return aff;
1775 error:
1776 isl_id_free(id);
1777 isl_aff_free(aff);
1778 return NULL;
1781 /* Exploit the equalities in "eq" to simplify the affine expression
1782 * and the expressions of the integer divisions in the local space.
1783 * The integer divisions in this local space are assumed to appear
1784 * as regular dimensions in "eq".
1786 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1787 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1789 int i, j;
1790 unsigned total;
1791 unsigned n_div;
1793 if (!eq)
1794 goto error;
1795 if (eq->n_eq == 0) {
1796 isl_basic_set_free(eq);
1797 return aff;
1800 aff = isl_aff_cow(aff);
1801 if (!aff)
1802 goto error;
1804 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1805 isl_basic_set_copy(eq));
1806 aff->v = isl_vec_cow(aff->v);
1807 if (!aff->ls || !aff->v)
1808 goto error;
1810 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1811 n_div = eq->n_div;
1812 for (i = 0; i < eq->n_eq; ++i) {
1813 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1814 if (j < 0 || j == 0 || j >= total)
1815 continue;
1817 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1818 &aff->v->el[0]);
1821 isl_basic_set_free(eq);
1822 aff = isl_aff_normalize(aff);
1823 return aff;
1824 error:
1825 isl_basic_set_free(eq);
1826 isl_aff_free(aff);
1827 return NULL;
1830 /* Exploit the equalities in "eq" to simplify the affine expression
1831 * and the expressions of the integer divisions in the local space.
1833 static __isl_give isl_aff *isl_aff_substitute_equalities(
1834 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1836 int n_div;
1838 if (!aff || !eq)
1839 goto error;
1840 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1841 if (n_div > 0)
1842 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1843 return isl_aff_substitute_equalities_lifted(aff, eq);
1844 error:
1845 isl_basic_set_free(eq);
1846 isl_aff_free(aff);
1847 return NULL;
1850 /* Look for equalities among the variables shared by context and aff
1851 * and the integer divisions of aff, if any.
1852 * The equalities are then used to eliminate coefficients and/or integer
1853 * divisions from aff.
1855 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1856 __isl_take isl_set *context)
1858 isl_basic_set *hull;
1859 int n_div;
1861 if (!aff)
1862 goto error;
1863 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1864 if (n_div > 0) {
1865 isl_basic_set *bset;
1866 isl_local_space *ls;
1867 context = isl_set_add_dims(context, isl_dim_set, n_div);
1868 ls = isl_aff_get_domain_local_space(aff);
1869 bset = isl_basic_set_from_local_space(ls);
1870 bset = isl_basic_set_lift(bset);
1871 bset = isl_basic_set_flatten(bset);
1872 context = isl_set_intersect(context,
1873 isl_set_from_basic_set(bset));
1876 hull = isl_set_affine_hull(context);
1877 return isl_aff_substitute_equalities_lifted(aff, hull);
1878 error:
1879 isl_aff_free(aff);
1880 isl_set_free(context);
1881 return NULL;
1884 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1885 __isl_take isl_set *context)
1887 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1888 dom_context = isl_set_intersect_params(dom_context, context);
1889 return isl_aff_gist(aff, dom_context);
1892 /* Return a basic set containing those elements in the space
1893 * of aff where it is non-negative.
1894 * If "rational" is set, then return a rational basic set.
1896 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1897 __isl_take isl_aff *aff, int rational)
1899 isl_constraint *ineq;
1900 isl_basic_set *bset;
1902 ineq = isl_inequality_from_aff(aff);
1904 bset = isl_basic_set_from_constraint(ineq);
1905 if (rational)
1906 bset = isl_basic_set_set_rational(bset);
1907 bset = isl_basic_set_simplify(bset);
1908 return bset;
1911 /* Return a basic set containing those elements in the space
1912 * of aff where it is non-negative.
1914 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1916 return aff_nonneg_basic_set(aff, 0);
1919 /* Return a basic set containing those elements in the domain space
1920 * of aff where it is negative.
1922 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1924 aff = isl_aff_neg(aff);
1925 aff = isl_aff_add_constant_num_si(aff, -1);
1926 return isl_aff_nonneg_basic_set(aff);
1929 /* Return a basic set containing those elements in the space
1930 * of aff where it is zero.
1931 * If "rational" is set, then return a rational basic set.
1933 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1934 int rational)
1936 isl_constraint *ineq;
1937 isl_basic_set *bset;
1939 ineq = isl_equality_from_aff(aff);
1941 bset = isl_basic_set_from_constraint(ineq);
1942 if (rational)
1943 bset = isl_basic_set_set_rational(bset);
1944 bset = isl_basic_set_simplify(bset);
1945 return bset;
1948 /* Return a basic set containing those elements in the space
1949 * of aff where it is zero.
1951 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1953 return aff_zero_basic_set(aff, 0);
1956 /* Return a basic set containing those elements in the shared space
1957 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1959 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1960 __isl_take isl_aff *aff2)
1962 aff1 = isl_aff_sub(aff1, aff2);
1964 return isl_aff_nonneg_basic_set(aff1);
1967 /* Return a basic set containing those elements in the shared space
1968 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1970 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1971 __isl_take isl_aff *aff2)
1973 return isl_aff_ge_basic_set(aff2, aff1);
1976 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1977 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1979 aff1 = isl_aff_add(aff1, aff2);
1980 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1981 return aff1;
1984 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1986 if (!aff)
1987 return -1;
1989 return 0;
1992 /* Check whether the given affine expression has non-zero coefficient
1993 * for any dimension in the given range or if any of these dimensions
1994 * appear with non-zero coefficients in any of the integer divisions
1995 * involved in the affine expression.
1997 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1998 enum isl_dim_type type, unsigned first, unsigned n)
2000 int i;
2001 isl_ctx *ctx;
2002 int *active = NULL;
2003 int involves = 0;
2005 if (!aff)
2006 return -1;
2007 if (n == 0)
2008 return 0;
2010 ctx = isl_aff_get_ctx(aff);
2011 if (first + n > isl_aff_dim(aff, type))
2012 isl_die(ctx, isl_error_invalid,
2013 "range out of bounds", return -1);
2015 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2016 if (!active)
2017 goto error;
2019 first += isl_local_space_offset(aff->ls, type) - 1;
2020 for (i = 0; i < n; ++i)
2021 if (active[first + i]) {
2022 involves = 1;
2023 break;
2026 free(active);
2028 return involves;
2029 error:
2030 free(active);
2031 return -1;
2034 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2035 enum isl_dim_type type, unsigned first, unsigned n)
2037 isl_ctx *ctx;
2039 if (!aff)
2040 return NULL;
2041 if (type == isl_dim_out)
2042 isl_die(aff->v->ctx, isl_error_invalid,
2043 "cannot drop output/set dimension",
2044 return isl_aff_free(aff));
2045 if (type == isl_dim_in)
2046 type = isl_dim_set;
2047 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2048 return aff;
2050 ctx = isl_aff_get_ctx(aff);
2051 if (first + n > isl_local_space_dim(aff->ls, type))
2052 isl_die(ctx, isl_error_invalid, "range out of bounds",
2053 return isl_aff_free(aff));
2055 aff = isl_aff_cow(aff);
2056 if (!aff)
2057 return NULL;
2059 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2060 if (!aff->ls)
2061 return isl_aff_free(aff);
2063 first += 1 + isl_local_space_offset(aff->ls, type);
2064 aff->v = isl_vec_drop_els(aff->v, first, n);
2065 if (!aff->v)
2066 return isl_aff_free(aff);
2068 return aff;
2071 /* Project the domain of the affine expression onto its parameter space.
2072 * The affine expression may not involve any of the domain dimensions.
2074 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2076 isl_space *space;
2077 unsigned n;
2078 int involves;
2080 n = isl_aff_dim(aff, isl_dim_in);
2081 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2082 if (involves < 0)
2083 return isl_aff_free(aff);
2084 if (involves)
2085 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2086 "affine expression involves some of the domain dimensions",
2087 return isl_aff_free(aff));
2088 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2089 space = isl_aff_get_domain_space(aff);
2090 space = isl_space_params(space);
2091 aff = isl_aff_reset_domain_space(aff, space);
2092 return aff;
2095 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2096 enum isl_dim_type type, unsigned first, unsigned n)
2098 isl_ctx *ctx;
2100 if (!aff)
2101 return NULL;
2102 if (type == isl_dim_out)
2103 isl_die(aff->v->ctx, isl_error_invalid,
2104 "cannot insert output/set dimensions",
2105 return isl_aff_free(aff));
2106 if (type == isl_dim_in)
2107 type = isl_dim_set;
2108 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2109 return aff;
2111 ctx = isl_aff_get_ctx(aff);
2112 if (first > isl_local_space_dim(aff->ls, type))
2113 isl_die(ctx, isl_error_invalid, "position out of bounds",
2114 return isl_aff_free(aff));
2116 aff = isl_aff_cow(aff);
2117 if (!aff)
2118 return NULL;
2120 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2121 if (!aff->ls)
2122 return isl_aff_free(aff);
2124 first += 1 + isl_local_space_offset(aff->ls, type);
2125 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2126 if (!aff->v)
2127 return isl_aff_free(aff);
2129 return aff;
2132 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2133 enum isl_dim_type type, unsigned n)
2135 unsigned pos;
2137 pos = isl_aff_dim(aff, type);
2139 return isl_aff_insert_dims(aff, type, pos, n);
2142 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2143 enum isl_dim_type type, unsigned n)
2145 unsigned pos;
2147 pos = isl_pw_aff_dim(pwaff, type);
2149 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2152 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2153 * to dimensions of "dst_type" at "dst_pos".
2155 * We only support moving input dimensions to parameters and vice versa.
2157 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2158 enum isl_dim_type dst_type, unsigned dst_pos,
2159 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2161 unsigned g_dst_pos;
2162 unsigned g_src_pos;
2164 if (!aff)
2165 return NULL;
2166 if (n == 0 &&
2167 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2168 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2169 return aff;
2171 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2172 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2173 "cannot move output/set dimension", isl_aff_free(aff));
2174 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2175 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2176 "cannot move divs", isl_aff_free(aff));
2177 if (dst_type == isl_dim_in)
2178 dst_type = isl_dim_set;
2179 if (src_type == isl_dim_in)
2180 src_type = isl_dim_set;
2182 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2183 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2184 "range out of bounds", isl_aff_free(aff));
2185 if (dst_type == src_type)
2186 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2187 "moving dims within the same type not supported",
2188 isl_aff_free(aff));
2190 aff = isl_aff_cow(aff);
2191 if (!aff)
2192 return NULL;
2194 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2195 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2196 if (dst_type > src_type)
2197 g_dst_pos -= n;
2199 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2200 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2201 src_type, src_pos, n);
2202 if (!aff->v || !aff->ls)
2203 return isl_aff_free(aff);
2205 aff = sort_divs(aff);
2207 return aff;
2210 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2212 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2213 return isl_pw_aff_alloc(dom, aff);
2216 #undef PW
2217 #define PW isl_pw_aff
2218 #undef EL
2219 #define EL isl_aff
2220 #undef EL_IS_ZERO
2221 #define EL_IS_ZERO is_empty
2222 #undef ZERO
2223 #define ZERO empty
2224 #undef IS_ZERO
2225 #define IS_ZERO is_empty
2226 #undef FIELD
2227 #define FIELD aff
2228 #undef DEFAULT_IS_ZERO
2229 #define DEFAULT_IS_ZERO 0
2231 #define NO_EVAL
2232 #define NO_OPT
2233 #define NO_LIFT
2234 #define NO_MORPH
2236 #include <isl_pw_templ.c>
2238 static __isl_give isl_set *align_params_pw_pw_set_and(
2239 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2240 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2241 __isl_take isl_pw_aff *pwaff2))
2243 if (!pwaff1 || !pwaff2)
2244 goto error;
2245 if (isl_space_match(pwaff1->dim, isl_dim_param,
2246 pwaff2->dim, isl_dim_param))
2247 return fn(pwaff1, pwaff2);
2248 if (!isl_space_has_named_params(pwaff1->dim) ||
2249 !isl_space_has_named_params(pwaff2->dim))
2250 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2251 "unaligned unnamed parameters", goto error);
2252 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2253 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2254 return fn(pwaff1, pwaff2);
2255 error:
2256 isl_pw_aff_free(pwaff1);
2257 isl_pw_aff_free(pwaff2);
2258 return NULL;
2261 /* Compute a piecewise quasi-affine expression with a domain that
2262 * is the union of those of pwaff1 and pwaff2 and such that on each
2263 * cell, the quasi-affine expression is the better (according to cmp)
2264 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2265 * is defined on a given cell, then the associated expression
2266 * is the defined one.
2268 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2269 __isl_take isl_pw_aff *pwaff2,
2270 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2271 __isl_take isl_aff *aff2))
2273 int i, j, n;
2274 isl_pw_aff *res;
2275 isl_ctx *ctx;
2276 isl_set *set;
2278 if (!pwaff1 || !pwaff2)
2279 goto error;
2281 ctx = isl_space_get_ctx(pwaff1->dim);
2282 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2283 isl_die(ctx, isl_error_invalid,
2284 "arguments should live in same space", goto error);
2286 if (isl_pw_aff_is_empty(pwaff1)) {
2287 isl_pw_aff_free(pwaff1);
2288 return pwaff2;
2291 if (isl_pw_aff_is_empty(pwaff2)) {
2292 isl_pw_aff_free(pwaff2);
2293 return pwaff1;
2296 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2297 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2299 for (i = 0; i < pwaff1->n; ++i) {
2300 set = isl_set_copy(pwaff1->p[i].set);
2301 for (j = 0; j < pwaff2->n; ++j) {
2302 struct isl_set *common;
2303 isl_set *better;
2305 common = isl_set_intersect(
2306 isl_set_copy(pwaff1->p[i].set),
2307 isl_set_copy(pwaff2->p[j].set));
2308 better = isl_set_from_basic_set(cmp(
2309 isl_aff_copy(pwaff2->p[j].aff),
2310 isl_aff_copy(pwaff1->p[i].aff)));
2311 better = isl_set_intersect(common, better);
2312 if (isl_set_plain_is_empty(better)) {
2313 isl_set_free(better);
2314 continue;
2316 set = isl_set_subtract(set, isl_set_copy(better));
2318 res = isl_pw_aff_add_piece(res, better,
2319 isl_aff_copy(pwaff2->p[j].aff));
2321 res = isl_pw_aff_add_piece(res, set,
2322 isl_aff_copy(pwaff1->p[i].aff));
2325 for (j = 0; j < pwaff2->n; ++j) {
2326 set = isl_set_copy(pwaff2->p[j].set);
2327 for (i = 0; i < pwaff1->n; ++i)
2328 set = isl_set_subtract(set,
2329 isl_set_copy(pwaff1->p[i].set));
2330 res = isl_pw_aff_add_piece(res, set,
2331 isl_aff_copy(pwaff2->p[j].aff));
2334 isl_pw_aff_free(pwaff1);
2335 isl_pw_aff_free(pwaff2);
2337 return res;
2338 error:
2339 isl_pw_aff_free(pwaff1);
2340 isl_pw_aff_free(pwaff2);
2341 return NULL;
2344 /* Compute a piecewise quasi-affine expression with a domain that
2345 * is the union of those of pwaff1 and pwaff2 and such that on each
2346 * cell, the quasi-affine expression is the maximum of those of pwaff1
2347 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2348 * cell, then the associated expression is the defined one.
2350 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2351 __isl_take isl_pw_aff *pwaff2)
2353 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2356 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2357 __isl_take isl_pw_aff *pwaff2)
2359 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2360 &pw_aff_union_max);
2363 /* Compute a piecewise quasi-affine expression with a domain that
2364 * is the union of those of pwaff1 and pwaff2 and such that on each
2365 * cell, the quasi-affine expression is the minimum of those of pwaff1
2366 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2367 * cell, then the associated expression is the defined one.
2369 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2370 __isl_take isl_pw_aff *pwaff2)
2372 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2375 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2376 __isl_take isl_pw_aff *pwaff2)
2378 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2379 &pw_aff_union_min);
2382 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2383 __isl_take isl_pw_aff *pwaff2, int max)
2385 if (max)
2386 return isl_pw_aff_union_max(pwaff1, pwaff2);
2387 else
2388 return isl_pw_aff_union_min(pwaff1, pwaff2);
2391 /* Construct a map with as domain the domain of pwaff and
2392 * one-dimensional range corresponding to the affine expressions.
2394 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2396 int i;
2397 isl_space *dim;
2398 isl_map *map;
2400 if (!pwaff)
2401 return NULL;
2403 dim = isl_pw_aff_get_space(pwaff);
2404 map = isl_map_empty(dim);
2406 for (i = 0; i < pwaff->n; ++i) {
2407 isl_basic_map *bmap;
2408 isl_map *map_i;
2410 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2411 map_i = isl_map_from_basic_map(bmap);
2412 map_i = isl_map_intersect_domain(map_i,
2413 isl_set_copy(pwaff->p[i].set));
2414 map = isl_map_union_disjoint(map, map_i);
2417 isl_pw_aff_free(pwaff);
2419 return map;
2422 /* Construct a map with as domain the domain of pwaff and
2423 * one-dimensional range corresponding to the affine expressions.
2425 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2427 if (!pwaff)
2428 return NULL;
2429 if (isl_space_is_set(pwaff->dim))
2430 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2431 "space of input is not a map",
2432 return isl_pw_aff_free(pwaff));
2433 return map_from_pw_aff(pwaff);
2436 /* Construct a one-dimensional set with as parameter domain
2437 * the domain of pwaff and the single set dimension
2438 * corresponding to the affine expressions.
2440 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2442 if (!pwaff)
2443 return NULL;
2444 if (!isl_space_is_set(pwaff->dim))
2445 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2446 "space of input is not a set",
2447 return isl_pw_aff_free(pwaff));
2448 return map_from_pw_aff(pwaff);
2451 /* Return a set containing those elements in the domain
2452 * of pwaff where it is non-negative.
2454 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2456 int i;
2457 isl_set *set;
2459 if (!pwaff)
2460 return NULL;
2462 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2464 for (i = 0; i < pwaff->n; ++i) {
2465 isl_basic_set *bset;
2466 isl_set *set_i;
2467 int rational;
2469 rational = isl_set_has_rational(pwaff->p[i].set);
2470 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2471 rational);
2472 set_i = isl_set_from_basic_set(bset);
2473 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2474 set = isl_set_union_disjoint(set, set_i);
2477 isl_pw_aff_free(pwaff);
2479 return set;
2482 /* Return a set containing those elements in the domain
2483 * of pwaff where it is zero (if complement is 0) or not zero
2484 * (if complement is 1).
2486 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2487 int complement)
2489 int i;
2490 isl_set *set;
2492 if (!pwaff)
2493 return NULL;
2495 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2497 for (i = 0; i < pwaff->n; ++i) {
2498 isl_basic_set *bset;
2499 isl_set *set_i, *zero;
2500 int rational;
2502 rational = isl_set_has_rational(pwaff->p[i].set);
2503 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2504 rational);
2505 zero = isl_set_from_basic_set(bset);
2506 set_i = isl_set_copy(pwaff->p[i].set);
2507 if (complement)
2508 set_i = isl_set_subtract(set_i, zero);
2509 else
2510 set_i = isl_set_intersect(set_i, zero);
2511 set = isl_set_union_disjoint(set, set_i);
2514 isl_pw_aff_free(pwaff);
2516 return set;
2519 /* Return a set containing those elements in the domain
2520 * of pwaff where it is zero.
2522 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2524 return pw_aff_zero_set(pwaff, 0);
2527 /* Return a set containing those elements in the domain
2528 * of pwaff where it is not zero.
2530 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2532 return pw_aff_zero_set(pwaff, 1);
2535 /* Return a set containing those elements in the shared domain
2536 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2538 * We compute the difference on the shared domain and then construct
2539 * the set of values where this difference is non-negative.
2540 * If strict is set, we first subtract 1 from the difference.
2541 * If equal is set, we only return the elements where pwaff1 and pwaff2
2542 * are equal.
2544 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2545 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2547 isl_set *set1, *set2;
2549 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2550 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2551 set1 = isl_set_intersect(set1, set2);
2552 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2553 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2554 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2556 if (strict) {
2557 isl_space *dim = isl_set_get_space(set1);
2558 isl_aff *aff;
2559 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2560 aff = isl_aff_add_constant_si(aff, -1);
2561 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2562 } else
2563 isl_set_free(set1);
2565 if (equal)
2566 return isl_pw_aff_zero_set(pwaff1);
2567 return isl_pw_aff_nonneg_set(pwaff1);
2570 /* Return a set containing those elements in the shared domain
2571 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2573 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2574 __isl_take isl_pw_aff *pwaff2)
2576 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2579 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2580 __isl_take isl_pw_aff *pwaff2)
2582 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2585 /* Return a set containing those elements in the shared domain
2586 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2588 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2589 __isl_take isl_pw_aff *pwaff2)
2591 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2594 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2595 __isl_take isl_pw_aff *pwaff2)
2597 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2600 /* Return a set containing those elements in the shared domain
2601 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2603 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2604 __isl_take isl_pw_aff *pwaff2)
2606 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2609 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2610 __isl_take isl_pw_aff *pwaff2)
2612 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2615 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2616 __isl_take isl_pw_aff *pwaff2)
2618 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2621 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2622 __isl_take isl_pw_aff *pwaff2)
2624 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2627 /* Return a set containing those elements in the shared domain
2628 * of the elements of list1 and list2 where each element in list1
2629 * has the relation specified by "fn" with each element in list2.
2631 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2632 __isl_take isl_pw_aff_list *list2,
2633 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2634 __isl_take isl_pw_aff *pwaff2))
2636 int i, j;
2637 isl_ctx *ctx;
2638 isl_set *set;
2640 if (!list1 || !list2)
2641 goto error;
2643 ctx = isl_pw_aff_list_get_ctx(list1);
2644 if (list1->n < 1 || list2->n < 1)
2645 isl_die(ctx, isl_error_invalid,
2646 "list should contain at least one element", goto error);
2648 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2649 for (i = 0; i < list1->n; ++i)
2650 for (j = 0; j < list2->n; ++j) {
2651 isl_set *set_ij;
2653 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2654 isl_pw_aff_copy(list2->p[j]));
2655 set = isl_set_intersect(set, set_ij);
2658 isl_pw_aff_list_free(list1);
2659 isl_pw_aff_list_free(list2);
2660 return set;
2661 error:
2662 isl_pw_aff_list_free(list1);
2663 isl_pw_aff_list_free(list2);
2664 return NULL;
2667 /* Return a set containing those elements in the shared domain
2668 * of the elements of list1 and list2 where each element in list1
2669 * is equal to each element in list2.
2671 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2672 __isl_take isl_pw_aff_list *list2)
2674 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2677 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2678 __isl_take isl_pw_aff_list *list2)
2680 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2683 /* Return a set containing those elements in the shared domain
2684 * of the elements of list1 and list2 where each element in list1
2685 * is less than or equal to each element in list2.
2687 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2688 __isl_take isl_pw_aff_list *list2)
2690 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2693 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2694 __isl_take isl_pw_aff_list *list2)
2696 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2699 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2700 __isl_take isl_pw_aff_list *list2)
2702 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2705 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2706 __isl_take isl_pw_aff_list *list2)
2708 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2712 /* Return a set containing those elements in the shared domain
2713 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2715 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2716 __isl_take isl_pw_aff *pwaff2)
2718 isl_set *set_lt, *set_gt;
2720 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2721 isl_pw_aff_copy(pwaff2));
2722 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2723 return isl_set_union_disjoint(set_lt, set_gt);
2726 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2727 __isl_take isl_pw_aff *pwaff2)
2729 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2732 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2733 isl_int v)
2735 int i;
2737 if (isl_int_is_one(v))
2738 return pwaff;
2739 if (!isl_int_is_pos(v))
2740 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2741 "factor needs to be positive",
2742 return isl_pw_aff_free(pwaff));
2743 pwaff = isl_pw_aff_cow(pwaff);
2744 if (!pwaff)
2745 return NULL;
2746 if (pwaff->n == 0)
2747 return pwaff;
2749 for (i = 0; i < pwaff->n; ++i) {
2750 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2751 if (!pwaff->p[i].aff)
2752 return isl_pw_aff_free(pwaff);
2755 return pwaff;
2758 /* Divide "pa" by "f".
2760 __isl_give isl_pw_aff *isl_pw_aff_scale_down_val(__isl_take isl_pw_aff *pa,
2761 __isl_take isl_val *f)
2763 int i;
2765 if (!pa || !f)
2766 goto error;
2768 if (isl_val_is_one(f)) {
2769 isl_val_free(f);
2770 return pa;
2773 if (!isl_val_is_rat(f))
2774 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2775 "expecting rational factor", goto error);
2776 if (!isl_val_is_pos(f))
2777 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
2778 "factor needs to be positive", goto error);
2780 pa = isl_pw_aff_cow(pa);
2781 if (!pa)
2782 return NULL;
2783 if (pa->n == 0)
2784 return pa;
2786 for (i = 0; i < pa->n; ++i) {
2787 pa->p[i].aff = isl_aff_scale_down_val(pa->p[i].aff,
2788 isl_val_copy(f));
2789 if (!pa->p[i].aff)
2790 goto error;
2793 isl_val_free(f);
2794 return pa;
2795 error:
2796 isl_pw_aff_free(pa);
2797 isl_val_free(f);
2798 return NULL;
2801 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2803 int i;
2805 pwaff = isl_pw_aff_cow(pwaff);
2806 if (!pwaff)
2807 return NULL;
2808 if (pwaff->n == 0)
2809 return pwaff;
2811 for (i = 0; i < pwaff->n; ++i) {
2812 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2813 if (!pwaff->p[i].aff)
2814 return isl_pw_aff_free(pwaff);
2817 return pwaff;
2820 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2822 int i;
2824 pwaff = isl_pw_aff_cow(pwaff);
2825 if (!pwaff)
2826 return NULL;
2827 if (pwaff->n == 0)
2828 return pwaff;
2830 for (i = 0; i < pwaff->n; ++i) {
2831 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2832 if (!pwaff->p[i].aff)
2833 return isl_pw_aff_free(pwaff);
2836 return pwaff;
2839 /* Assuming that "cond1" and "cond2" are disjoint,
2840 * return an affine expression that is equal to pwaff1 on cond1
2841 * and to pwaff2 on cond2.
2843 static __isl_give isl_pw_aff *isl_pw_aff_select(
2844 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2845 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2847 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2848 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2850 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2853 /* Return an affine expression that is equal to pwaff_true for elements
2854 * where "cond" is non-zero and to pwaff_false for elements where "cond"
2855 * is zero.
2856 * That is, return cond ? pwaff_true : pwaff_false;
2858 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2859 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2861 isl_set *cond_true, *cond_false;
2863 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2864 cond_false = isl_pw_aff_zero_set(cond);
2865 return isl_pw_aff_select(cond_true, pwaff_true,
2866 cond_false, pwaff_false);
2869 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2871 if (!aff)
2872 return -1;
2874 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2877 /* Check whether pwaff is a piecewise constant.
2879 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2881 int i;
2883 if (!pwaff)
2884 return -1;
2886 for (i = 0; i < pwaff->n; ++i) {
2887 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2888 if (is_cst < 0 || !is_cst)
2889 return is_cst;
2892 return 1;
2895 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2896 __isl_take isl_aff *aff2)
2898 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2899 return isl_aff_mul(aff2, aff1);
2901 if (!isl_aff_is_cst(aff2))
2902 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2903 "at least one affine expression should be constant",
2904 goto error);
2906 aff1 = isl_aff_cow(aff1);
2907 if (!aff1 || !aff2)
2908 goto error;
2910 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2911 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2913 isl_aff_free(aff2);
2914 return aff1;
2915 error:
2916 isl_aff_free(aff1);
2917 isl_aff_free(aff2);
2918 return NULL;
2921 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2923 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2924 __isl_take isl_aff *aff2)
2926 int is_cst;
2927 int neg;
2929 is_cst = isl_aff_is_cst(aff2);
2930 if (is_cst < 0)
2931 goto error;
2932 if (!is_cst)
2933 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2934 "second argument should be a constant", goto error);
2936 if (!aff2)
2937 goto error;
2939 neg = isl_int_is_neg(aff2->v->el[1]);
2940 if (neg) {
2941 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2942 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2945 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2946 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2948 if (neg) {
2949 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2950 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2953 isl_aff_free(aff2);
2954 return aff1;
2955 error:
2956 isl_aff_free(aff1);
2957 isl_aff_free(aff2);
2958 return NULL;
2961 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2962 __isl_take isl_pw_aff *pwaff2)
2964 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2967 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2968 __isl_take isl_pw_aff *pwaff2)
2970 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2973 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2974 __isl_take isl_pw_aff *pwaff2)
2976 return isl_pw_aff_union_add_(pwaff1, pwaff2);
2979 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2980 __isl_take isl_pw_aff *pwaff2)
2982 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2985 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2986 __isl_take isl_pw_aff *pwaff2)
2988 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2991 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2992 __isl_take isl_pw_aff *pa2)
2994 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2997 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2999 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3000 __isl_take isl_pw_aff *pa2)
3002 int is_cst;
3004 is_cst = isl_pw_aff_is_cst(pa2);
3005 if (is_cst < 0)
3006 goto error;
3007 if (!is_cst)
3008 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3009 "second argument should be a piecewise constant",
3010 goto error);
3011 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3012 error:
3013 isl_pw_aff_free(pa1);
3014 isl_pw_aff_free(pa2);
3015 return NULL;
3018 /* Compute the quotient of the integer division of "pa1" by "pa2"
3019 * with rounding towards zero.
3020 * "pa2" is assumed to be a piecewise constant.
3022 * In particular, return
3024 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3027 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3028 __isl_take isl_pw_aff *pa2)
3030 int is_cst;
3031 isl_set *cond;
3032 isl_pw_aff *f, *c;
3034 is_cst = isl_pw_aff_is_cst(pa2);
3035 if (is_cst < 0)
3036 goto error;
3037 if (!is_cst)
3038 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3039 "second argument should be a piecewise constant",
3040 goto error);
3042 pa1 = isl_pw_aff_div(pa1, pa2);
3044 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3045 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3046 c = isl_pw_aff_ceil(pa1);
3047 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3048 error:
3049 isl_pw_aff_free(pa1);
3050 isl_pw_aff_free(pa2);
3051 return NULL;
3054 /* Compute the remainder of the integer division of "pa1" by "pa2"
3055 * with rounding towards zero.
3056 * "pa2" is assumed to be a piecewise constant.
3058 * In particular, return
3060 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3063 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3064 __isl_take isl_pw_aff *pa2)
3066 int is_cst;
3067 isl_pw_aff *res;
3069 is_cst = isl_pw_aff_is_cst(pa2);
3070 if (is_cst < 0)
3071 goto error;
3072 if (!is_cst)
3073 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3074 "second argument should be a piecewise constant",
3075 goto error);
3076 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3077 res = isl_pw_aff_mul(pa2, res);
3078 res = isl_pw_aff_sub(pa1, res);
3079 return res;
3080 error:
3081 isl_pw_aff_free(pa1);
3082 isl_pw_aff_free(pa2);
3083 return NULL;
3086 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3087 __isl_take isl_pw_aff *pwaff2)
3089 isl_set *le;
3090 isl_set *dom;
3092 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3093 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3094 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3095 isl_pw_aff_copy(pwaff2));
3096 dom = isl_set_subtract(dom, isl_set_copy(le));
3097 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3100 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3101 __isl_take isl_pw_aff *pwaff2)
3103 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3106 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3107 __isl_take isl_pw_aff *pwaff2)
3109 isl_set *ge;
3110 isl_set *dom;
3112 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3113 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3114 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3115 isl_pw_aff_copy(pwaff2));
3116 dom = isl_set_subtract(dom, isl_set_copy(ge));
3117 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3120 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3121 __isl_take isl_pw_aff *pwaff2)
3123 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3126 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3127 __isl_take isl_pw_aff_list *list,
3128 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3129 __isl_take isl_pw_aff *pwaff2))
3131 int i;
3132 isl_ctx *ctx;
3133 isl_pw_aff *res;
3135 if (!list)
3136 return NULL;
3138 ctx = isl_pw_aff_list_get_ctx(list);
3139 if (list->n < 1)
3140 isl_die(ctx, isl_error_invalid,
3141 "list should contain at least one element",
3142 return isl_pw_aff_list_free(list));
3144 res = isl_pw_aff_copy(list->p[0]);
3145 for (i = 1; i < list->n; ++i)
3146 res = fn(res, isl_pw_aff_copy(list->p[i]));
3148 isl_pw_aff_list_free(list);
3149 return res;
3152 /* Return an isl_pw_aff that maps each element in the intersection of the
3153 * domains of the elements of list to the minimal corresponding affine
3154 * expression.
3156 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3158 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3161 /* Return an isl_pw_aff that maps each element in the intersection of the
3162 * domains of the elements of list to the maximal corresponding affine
3163 * expression.
3165 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3167 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3170 /* Mark the domains of "pwaff" as rational.
3172 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3174 int i;
3176 pwaff = isl_pw_aff_cow(pwaff);
3177 if (!pwaff)
3178 return NULL;
3179 if (pwaff->n == 0)
3180 return pwaff;
3182 for (i = 0; i < pwaff->n; ++i) {
3183 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3184 if (!pwaff->p[i].set)
3185 return isl_pw_aff_free(pwaff);
3188 return pwaff;
3191 /* Mark the domains of the elements of "list" as rational.
3193 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3194 __isl_take isl_pw_aff_list *list)
3196 int i, n;
3198 if (!list)
3199 return NULL;
3200 if (list->n == 0)
3201 return list;
3203 n = list->n;
3204 for (i = 0; i < n; ++i) {
3205 isl_pw_aff *pa;
3207 pa = isl_pw_aff_list_get_pw_aff(list, i);
3208 pa = isl_pw_aff_set_rational(pa);
3209 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3212 return list;
3215 /* Do the parameters of "aff" match those of "space"?
3217 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3218 __isl_keep isl_space *space)
3220 isl_space *aff_space;
3221 int match;
3223 if (!aff || !space)
3224 return -1;
3226 aff_space = isl_aff_get_domain_space(aff);
3228 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3230 isl_space_free(aff_space);
3231 return match;
3234 /* Check that the domain space of "aff" matches "space".
3236 * Return 0 on success and -1 on error.
3238 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3239 __isl_keep isl_space *space)
3241 isl_space *aff_space;
3242 int match;
3244 if (!aff || !space)
3245 return -1;
3247 aff_space = isl_aff_get_domain_space(aff);
3249 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3250 if (match < 0)
3251 goto error;
3252 if (!match)
3253 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3254 "parameters don't match", goto error);
3255 match = isl_space_tuple_match(space, isl_dim_in,
3256 aff_space, isl_dim_set);
3257 if (match < 0)
3258 goto error;
3259 if (!match)
3260 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3261 "domains don't match", goto error);
3262 isl_space_free(aff_space);
3263 return 0;
3264 error:
3265 isl_space_free(aff_space);
3266 return -1;
3269 #undef BASE
3270 #define BASE aff
3271 #define NO_INTERSECT_DOMAIN
3272 #define NO_DOMAIN
3274 #include <isl_multi_templ.c>
3276 #undef NO_DOMAIN
3277 #undef NO_INTERSECT_DOMAIN
3279 /* Remove any internal structure of the domain of "ma".
3280 * If there is any such internal structure in the input,
3281 * then the name of the corresponding space is also removed.
3283 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3284 __isl_take isl_multi_aff *ma)
3286 isl_space *space;
3288 if (!ma)
3289 return NULL;
3291 if (!ma->space->nested[0])
3292 return ma;
3294 space = isl_multi_aff_get_space(ma);
3295 space = isl_space_flatten_domain(space);
3296 ma = isl_multi_aff_reset_space(ma, space);
3298 return ma;
3301 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3302 * of the space to its domain.
3304 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3306 int i, n_in;
3307 isl_local_space *ls;
3308 isl_multi_aff *ma;
3310 if (!space)
3311 return NULL;
3312 if (!isl_space_is_map(space))
3313 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3314 "not a map space", goto error);
3316 n_in = isl_space_dim(space, isl_dim_in);
3317 space = isl_space_domain_map(space);
3319 ma = isl_multi_aff_alloc(isl_space_copy(space));
3320 if (n_in == 0) {
3321 isl_space_free(space);
3322 return ma;
3325 space = isl_space_domain(space);
3326 ls = isl_local_space_from_space(space);
3327 for (i = 0; i < n_in; ++i) {
3328 isl_aff *aff;
3330 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3331 isl_dim_set, i);
3332 ma = isl_multi_aff_set_aff(ma, i, aff);
3334 isl_local_space_free(ls);
3335 return ma;
3336 error:
3337 isl_space_free(space);
3338 return NULL;
3341 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3342 * of the space to its range.
3344 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3346 int i, n_in, n_out;
3347 isl_local_space *ls;
3348 isl_multi_aff *ma;
3350 if (!space)
3351 return NULL;
3352 if (!isl_space_is_map(space))
3353 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3354 "not a map space", goto error);
3356 n_in = isl_space_dim(space, isl_dim_in);
3357 n_out = isl_space_dim(space, isl_dim_out);
3358 space = isl_space_range_map(space);
3360 ma = isl_multi_aff_alloc(isl_space_copy(space));
3361 if (n_out == 0) {
3362 isl_space_free(space);
3363 return ma;
3366 space = isl_space_domain(space);
3367 ls = isl_local_space_from_space(space);
3368 for (i = 0; i < n_out; ++i) {
3369 isl_aff *aff;
3371 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3372 isl_dim_set, n_in + i);
3373 ma = isl_multi_aff_set_aff(ma, i, aff);
3375 isl_local_space_free(ls);
3376 return ma;
3377 error:
3378 isl_space_free(space);
3379 return NULL;
3382 /* Given the space of a set and a range of set dimensions,
3383 * construct an isl_multi_aff that projects out those dimensions.
3385 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3386 __isl_take isl_space *space, enum isl_dim_type type,
3387 unsigned first, unsigned n)
3389 int i, dim;
3390 isl_local_space *ls;
3391 isl_multi_aff *ma;
3393 if (!space)
3394 return NULL;
3395 if (!isl_space_is_set(space))
3396 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3397 "expecting set space", goto error);
3398 if (type != isl_dim_set)
3399 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3400 "only set dimensions can be projected out", goto error);
3402 dim = isl_space_dim(space, isl_dim_set);
3403 if (first + n > dim)
3404 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3405 "range out of bounds", goto error);
3407 space = isl_space_from_domain(space);
3408 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3410 if (dim == n)
3411 return isl_multi_aff_alloc(space);
3413 ma = isl_multi_aff_alloc(isl_space_copy(space));
3414 space = isl_space_domain(space);
3415 ls = isl_local_space_from_space(space);
3417 for (i = 0; i < first; ++i) {
3418 isl_aff *aff;
3420 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3421 isl_dim_set, i);
3422 ma = isl_multi_aff_set_aff(ma, i, aff);
3425 for (i = 0; i < dim - (first + n); ++i) {
3426 isl_aff *aff;
3428 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3429 isl_dim_set, first + n + i);
3430 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3433 isl_local_space_free(ls);
3434 return ma;
3435 error:
3436 isl_space_free(space);
3437 return NULL;
3440 /* Given the space of a set and a range of set dimensions,
3441 * construct an isl_pw_multi_aff that projects out those dimensions.
3443 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3444 __isl_take isl_space *space, enum isl_dim_type type,
3445 unsigned first, unsigned n)
3447 isl_multi_aff *ma;
3449 ma = isl_multi_aff_project_out_map(space, type, first, n);
3450 return isl_pw_multi_aff_from_multi_aff(ma);
3453 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3454 * domain.
3456 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3457 __isl_take isl_multi_aff *ma)
3459 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3460 return isl_pw_multi_aff_alloc(dom, ma);
3463 /* Create a piecewise multi-affine expression in the given space that maps each
3464 * input dimension to the corresponding output dimension.
3466 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3467 __isl_take isl_space *space)
3469 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3472 /* Add "ma2" to "ma1" and return the result.
3474 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3476 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3477 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3479 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3482 /* Add "ma2" to "ma1" and return the result.
3484 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3485 __isl_take isl_multi_aff *ma2)
3487 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3488 &isl_multi_aff_add_aligned);
3491 /* Subtract "ma2" from "ma1" and return the result.
3493 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3495 static __isl_give isl_multi_aff *isl_multi_aff_sub_aligned(
3496 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
3498 return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
3501 /* Subtract "ma2" from "ma1" and return the result.
3503 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
3504 __isl_take isl_multi_aff *ma2)
3506 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3507 &isl_multi_aff_sub_aligned);
3510 /* Exploit the equalities in "eq" to simplify the affine expressions.
3512 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3513 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3515 int i;
3517 maff = isl_multi_aff_cow(maff);
3518 if (!maff || !eq)
3519 goto error;
3521 for (i = 0; i < maff->n; ++i) {
3522 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3523 isl_basic_set_copy(eq));
3524 if (!maff->p[i])
3525 goto error;
3528 isl_basic_set_free(eq);
3529 return maff;
3530 error:
3531 isl_basic_set_free(eq);
3532 isl_multi_aff_free(maff);
3533 return NULL;
3536 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3537 isl_int f)
3539 int i;
3541 maff = isl_multi_aff_cow(maff);
3542 if (!maff)
3543 return NULL;
3545 for (i = 0; i < maff->n; ++i) {
3546 maff->p[i] = isl_aff_scale(maff->p[i], f);
3547 if (!maff->p[i])
3548 return isl_multi_aff_free(maff);
3551 return maff;
3554 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3555 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3557 maff1 = isl_multi_aff_add(maff1, maff2);
3558 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3559 return maff1;
3562 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3564 if (!maff)
3565 return -1;
3567 return 0;
3570 /* Return the set of domain elements where "ma1" is lexicographically
3571 * smaller than or equal to "ma2".
3573 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3574 __isl_take isl_multi_aff *ma2)
3576 return isl_multi_aff_lex_ge_set(ma2, ma1);
3579 /* Return the set of domain elements where "ma1" is lexicographically
3580 * greater than or equal to "ma2".
3582 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3583 __isl_take isl_multi_aff *ma2)
3585 isl_space *space;
3586 isl_map *map1, *map2;
3587 isl_map *map, *ge;
3589 map1 = isl_map_from_multi_aff(ma1);
3590 map2 = isl_map_from_multi_aff(ma2);
3591 map = isl_map_range_product(map1, map2);
3592 space = isl_space_range(isl_map_get_space(map));
3593 space = isl_space_domain(isl_space_unwrap(space));
3594 ge = isl_map_lex_ge(space);
3595 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3597 return isl_map_domain(map);
3600 #undef PW
3601 #define PW isl_pw_multi_aff
3602 #undef EL
3603 #define EL isl_multi_aff
3604 #undef EL_IS_ZERO
3605 #define EL_IS_ZERO is_empty
3606 #undef ZERO
3607 #define ZERO empty
3608 #undef IS_ZERO
3609 #define IS_ZERO is_empty
3610 #undef FIELD
3611 #define FIELD maff
3612 #undef DEFAULT_IS_ZERO
3613 #define DEFAULT_IS_ZERO 0
3615 #define NO_NEG
3616 #define NO_EVAL
3617 #define NO_OPT
3618 #define NO_INVOLVES_DIMS
3619 #define NO_INSERT_DIMS
3620 #define NO_LIFT
3621 #define NO_MORPH
3623 #include <isl_pw_templ.c>
3625 #undef UNION
3626 #define UNION isl_union_pw_multi_aff
3627 #undef PART
3628 #define PART isl_pw_multi_aff
3629 #undef PARTS
3630 #define PARTS pw_multi_aff
3631 #define ALIGN_DOMAIN
3633 #define NO_EVAL
3635 #include <isl_union_templ.c>
3637 /* Given a function "cmp" that returns the set of elements where
3638 * "ma1" is "better" than "ma2", return the intersection of this
3639 * set with "dom1" and "dom2".
3641 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
3642 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
3643 __isl_keep isl_multi_aff *ma2,
3644 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3645 __isl_take isl_multi_aff *ma2))
3647 isl_set *common;
3648 isl_set *better;
3649 int is_empty;
3651 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
3652 is_empty = isl_set_plain_is_empty(common);
3653 if (is_empty >= 0 && is_empty)
3654 return common;
3655 if (is_empty < 0)
3656 return isl_set_free(common);
3657 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
3658 better = isl_set_intersect(common, better);
3660 return better;
3663 /* Given a function "cmp" that returns the set of elements where
3664 * "ma1" is "better" than "ma2", return a piecewise multi affine
3665 * expression defined on the union of the definition domains
3666 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3667 * "pma2" on each cell. If only one of the two input functions
3668 * is defined on a given cell, then it is considered the best.
3670 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3671 __isl_take isl_pw_multi_aff *pma1,
3672 __isl_take isl_pw_multi_aff *pma2,
3673 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3674 __isl_take isl_multi_aff *ma2))
3676 int i, j, n;
3677 isl_pw_multi_aff *res = NULL;
3678 isl_ctx *ctx;
3679 isl_set *set = NULL;
3681 if (!pma1 || !pma2)
3682 goto error;
3684 ctx = isl_space_get_ctx(pma1->dim);
3685 if (!isl_space_is_equal(pma1->dim, pma2->dim))
3686 isl_die(ctx, isl_error_invalid,
3687 "arguments should live in the same space", goto error);
3689 if (isl_pw_multi_aff_is_empty(pma1)) {
3690 isl_pw_multi_aff_free(pma1);
3691 return pma2;
3694 if (isl_pw_multi_aff_is_empty(pma2)) {
3695 isl_pw_multi_aff_free(pma2);
3696 return pma1;
3699 n = 2 * (pma1->n + 1) * (pma2->n + 1);
3700 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3702 for (i = 0; i < pma1->n; ++i) {
3703 set = isl_set_copy(pma1->p[i].set);
3704 for (j = 0; j < pma2->n; ++j) {
3705 isl_set *better;
3706 int is_empty;
3708 better = shared_and_better(pma2->p[j].set,
3709 pma1->p[i].set, pma2->p[j].maff,
3710 pma1->p[i].maff, cmp);
3711 is_empty = isl_set_plain_is_empty(better);
3712 if (is_empty < 0 || is_empty) {
3713 isl_set_free(better);
3714 if (is_empty < 0)
3715 goto error;
3716 continue;
3718 set = isl_set_subtract(set, isl_set_copy(better));
3720 res = isl_pw_multi_aff_add_piece(res, better,
3721 isl_multi_aff_copy(pma2->p[j].maff));
3723 res = isl_pw_multi_aff_add_piece(res, set,
3724 isl_multi_aff_copy(pma1->p[i].maff));
3727 for (j = 0; j < pma2->n; ++j) {
3728 set = isl_set_copy(pma2->p[j].set);
3729 for (i = 0; i < pma1->n; ++i)
3730 set = isl_set_subtract(set,
3731 isl_set_copy(pma1->p[i].set));
3732 res = isl_pw_multi_aff_add_piece(res, set,
3733 isl_multi_aff_copy(pma2->p[j].maff));
3736 isl_pw_multi_aff_free(pma1);
3737 isl_pw_multi_aff_free(pma2);
3739 return res;
3740 error:
3741 isl_pw_multi_aff_free(pma1);
3742 isl_pw_multi_aff_free(pma2);
3743 isl_set_free(set);
3744 return isl_pw_multi_aff_free(res);
3747 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3748 __isl_take isl_pw_multi_aff *pma1,
3749 __isl_take isl_pw_multi_aff *pma2)
3751 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3754 /* Given two piecewise multi affine expressions, return a piecewise
3755 * multi-affine expression defined on the union of the definition domains
3756 * of the inputs that is equal to the lexicographic maximum of the two
3757 * inputs on each cell. If only one of the two inputs is defined on
3758 * a given cell, then it is considered to be the maximum.
3760 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3761 __isl_take isl_pw_multi_aff *pma1,
3762 __isl_take isl_pw_multi_aff *pma2)
3764 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3765 &pw_multi_aff_union_lexmax);
3768 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3769 __isl_take isl_pw_multi_aff *pma1,
3770 __isl_take isl_pw_multi_aff *pma2)
3772 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3775 /* Given two piecewise multi affine expressions, return a piecewise
3776 * multi-affine expression defined on the union of the definition domains
3777 * of the inputs that is equal to the lexicographic minimum of the two
3778 * inputs on each cell. If only one of the two inputs is defined on
3779 * a given cell, then it is considered to be the minimum.
3781 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3782 __isl_take isl_pw_multi_aff *pma1,
3783 __isl_take isl_pw_multi_aff *pma2)
3785 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3786 &pw_multi_aff_union_lexmin);
3789 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3790 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3792 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3793 &isl_multi_aff_add);
3796 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3797 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3799 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3800 &pw_multi_aff_add);
3803 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3804 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3806 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3807 &isl_multi_aff_sub);
3810 /* Subtract "pma2" from "pma1" and return the result.
3812 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3813 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3815 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3816 &pw_multi_aff_sub);
3819 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3820 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3822 return isl_pw_multi_aff_union_add_(pma1, pma2);
3825 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3826 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3828 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3829 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3831 int i, j, n;
3832 isl_space *space;
3833 isl_pw_multi_aff *res;
3835 if (!pma1 || !pma2)
3836 goto error;
3838 n = pma1->n * pma2->n;
3839 space = isl_space_product(isl_space_copy(pma1->dim),
3840 isl_space_copy(pma2->dim));
3841 res = isl_pw_multi_aff_alloc_size(space, n);
3843 for (i = 0; i < pma1->n; ++i) {
3844 for (j = 0; j < pma2->n; ++j) {
3845 isl_set *domain;
3846 isl_multi_aff *ma;
3848 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3849 isl_set_copy(pma2->p[j].set));
3850 ma = isl_multi_aff_product(
3851 isl_multi_aff_copy(pma1->p[i].maff),
3852 isl_multi_aff_copy(pma2->p[j].maff));
3853 res = isl_pw_multi_aff_add_piece(res, domain, ma);
3857 isl_pw_multi_aff_free(pma1);
3858 isl_pw_multi_aff_free(pma2);
3859 return res;
3860 error:
3861 isl_pw_multi_aff_free(pma1);
3862 isl_pw_multi_aff_free(pma2);
3863 return NULL;
3866 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3867 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3869 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3870 &pw_multi_aff_product);
3873 /* Construct a map mapping the domain of the piecewise multi-affine expression
3874 * to its range, with each dimension in the range equated to the
3875 * corresponding affine expression on its cell.
3877 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3879 int i;
3880 isl_map *map;
3882 if (!pma)
3883 return NULL;
3885 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3887 for (i = 0; i < pma->n; ++i) {
3888 isl_multi_aff *maff;
3889 isl_basic_map *bmap;
3890 isl_map *map_i;
3892 maff = isl_multi_aff_copy(pma->p[i].maff);
3893 bmap = isl_basic_map_from_multi_aff(maff);
3894 map_i = isl_map_from_basic_map(bmap);
3895 map_i = isl_map_intersect_domain(map_i,
3896 isl_set_copy(pma->p[i].set));
3897 map = isl_map_union_disjoint(map, map_i);
3900 isl_pw_multi_aff_free(pma);
3901 return map;
3904 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3906 if (!pma)
3907 return NULL;
3909 if (!isl_space_is_set(pma->dim))
3910 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3911 "isl_pw_multi_aff cannot be converted into an isl_set",
3912 return isl_pw_multi_aff_free(pma));
3914 return isl_map_from_pw_multi_aff(pma);
3917 /* Given a basic map with a single output dimension that is defined
3918 * in terms of the parameters and input dimensions using an equality,
3919 * extract an isl_aff that expresses the output dimension in terms
3920 * of the parameters and input dimensions.
3922 * Since some applications expect the result of isl_pw_multi_aff_from_map
3923 * to only contain integer affine expressions, we compute the floor
3924 * of the expression before returning.
3926 * This function shares some similarities with
3927 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3929 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3930 __isl_take isl_basic_map *bmap)
3932 int i;
3933 unsigned offset;
3934 unsigned total;
3935 isl_local_space *ls;
3936 isl_aff *aff;
3938 if (!bmap)
3939 return NULL;
3940 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3941 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3942 "basic map should have a single output dimension",
3943 goto error);
3944 offset = isl_basic_map_offset(bmap, isl_dim_out);
3945 total = isl_basic_map_total_dim(bmap);
3946 for (i = 0; i < bmap->n_eq; ++i) {
3947 if (isl_int_is_zero(bmap->eq[i][offset]))
3948 continue;
3949 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3950 1 + total - (offset + 1)) != -1)
3951 continue;
3952 break;
3954 if (i >= bmap->n_eq)
3955 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3956 "unable to find suitable equality", goto error);
3957 ls = isl_basic_map_get_local_space(bmap);
3958 aff = isl_aff_alloc(isl_local_space_domain(ls));
3959 if (!aff)
3960 goto error;
3961 if (isl_int_is_neg(bmap->eq[i][offset]))
3962 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3963 else
3964 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3965 isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3966 isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3967 isl_basic_map_free(bmap);
3969 aff = isl_aff_remove_unused_divs(aff);
3970 aff = isl_aff_floor(aff);
3971 return aff;
3972 error:
3973 isl_basic_map_free(bmap);
3974 return NULL;
3977 /* Given a basic map where each output dimension is defined
3978 * in terms of the parameters and input dimensions using an equality,
3979 * extract an isl_multi_aff that expresses the output dimensions in terms
3980 * of the parameters and input dimensions.
3982 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3983 __isl_take isl_basic_map *bmap)
3985 int i;
3986 unsigned n_out;
3987 isl_multi_aff *ma;
3989 if (!bmap)
3990 return NULL;
3992 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3993 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3995 for (i = 0; i < n_out; ++i) {
3996 isl_basic_map *bmap_i;
3997 isl_aff *aff;
3999 bmap_i = isl_basic_map_copy(bmap);
4000 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4001 i + 1, n_out - (1 + i));
4002 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4003 aff = extract_isl_aff_from_basic_map(bmap_i);
4004 ma = isl_multi_aff_set_aff(ma, i, aff);
4007 isl_basic_map_free(bmap);
4009 return ma;
4012 /* Create an isl_pw_multi_aff that is equivalent to
4013 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4014 * The given basic map is such that each output dimension is defined
4015 * in terms of the parameters and input dimensions using an equality.
4017 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4018 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4020 isl_multi_aff *ma;
4022 ma = extract_isl_multi_aff_from_basic_map(bmap);
4023 return isl_pw_multi_aff_alloc(domain, ma);
4026 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4027 * This obviously only works if the input "map" is single-valued.
4028 * If so, we compute the lexicographic minimum of the image in the form
4029 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4030 * to its lexicographic minimum.
4031 * If the input is not single-valued, we produce an error.
4033 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4034 __isl_take isl_map *map)
4036 int i;
4037 int sv;
4038 isl_pw_multi_aff *pma;
4040 sv = isl_map_is_single_valued(map);
4041 if (sv < 0)
4042 goto error;
4043 if (!sv)
4044 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4045 "map is not single-valued", goto error);
4046 map = isl_map_make_disjoint(map);
4047 if (!map)
4048 return NULL;
4050 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4052 for (i = 0; i < map->n; ++i) {
4053 isl_pw_multi_aff *pma_i;
4054 isl_basic_map *bmap;
4055 bmap = isl_basic_map_copy(map->p[i]);
4056 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4057 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4060 isl_map_free(map);
4061 return pma;
4062 error:
4063 isl_map_free(map);
4064 return NULL;
4067 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4068 * taking into account that the output dimension at position "d"
4069 * can be represented as
4071 * x = floor((e(...) + c1) / m)
4073 * given that constraint "i" is of the form
4075 * e(...) + c1 - m x >= 0
4078 * Let "map" be of the form
4080 * A -> B
4082 * We construct a mapping
4084 * A -> [A -> x = floor(...)]
4086 * apply that to the map, obtaining
4088 * [A -> x = floor(...)] -> B
4090 * and equate dimension "d" to x.
4091 * We then compute a isl_pw_multi_aff representation of the resulting map
4092 * and plug in the mapping above.
4094 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4095 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4097 isl_ctx *ctx;
4098 isl_space *space;
4099 isl_local_space *ls;
4100 isl_multi_aff *ma;
4101 isl_aff *aff;
4102 isl_vec *v;
4103 isl_map *insert;
4104 int offset;
4105 int n;
4106 int n_in;
4107 isl_pw_multi_aff *pma;
4108 int is_set;
4110 is_set = isl_map_is_set(map);
4112 offset = isl_basic_map_offset(hull, isl_dim_out);
4113 ctx = isl_map_get_ctx(map);
4114 space = isl_space_domain(isl_map_get_space(map));
4115 n_in = isl_space_dim(space, isl_dim_set);
4116 n = isl_space_dim(space, isl_dim_all);
4118 v = isl_vec_alloc(ctx, 1 + 1 + n);
4119 if (v) {
4120 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4121 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4123 isl_basic_map_free(hull);
4125 ls = isl_local_space_from_space(isl_space_copy(space));
4126 aff = isl_aff_alloc_vec(ls, v);
4127 aff = isl_aff_floor(aff);
4128 if (is_set) {
4129 isl_space_free(space);
4130 ma = isl_multi_aff_from_aff(aff);
4131 } else {
4132 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4133 ma = isl_multi_aff_range_product(ma,
4134 isl_multi_aff_from_aff(aff));
4137 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4138 map = isl_map_apply_domain(map, insert);
4139 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4140 pma = isl_pw_multi_aff_from_map(map);
4141 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4143 return pma;
4146 /* Is constraint "c" of the form
4148 * e(...) + c1 - m x >= 0
4150 * or
4152 * -e(...) + c2 + m x >= 0
4154 * where m > 1 and e only depends on parameters and input dimemnsions?
4156 * "offset" is the offset of the output dimensions
4157 * "pos" is the position of output dimension x.
4159 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4161 if (isl_int_is_zero(c[offset + d]))
4162 return 0;
4163 if (isl_int_is_one(c[offset + d]))
4164 return 0;
4165 if (isl_int_is_negone(c[offset + d]))
4166 return 0;
4167 if (isl_seq_first_non_zero(c + offset, d) != -1)
4168 return 0;
4169 if (isl_seq_first_non_zero(c + offset + d + 1,
4170 total - (offset + d + 1)) != -1)
4171 return 0;
4172 return 1;
4175 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4177 * As a special case, we first check if there is any pair of constraints,
4178 * shared by all the basic maps in "map" that force a given dimension
4179 * to be equal to the floor of some affine combination of the input dimensions.
4181 * In particular, if we can find two constraints
4183 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4185 * and
4187 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4189 * where m > 1 and e only depends on parameters and input dimemnsions,
4190 * and such that
4192 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4194 * then we know that we can take
4196 * x = floor((e(...) + c1) / m)
4198 * without having to perform any computation.
4200 * Note that we know that
4202 * c1 + c2 >= 1
4204 * If c1 + c2 were 0, then we would have detected an equality during
4205 * simplification. If c1 + c2 were negative, then we would have detected
4206 * a contradiction.
4208 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4209 __isl_take isl_map *map)
4211 int d, dim;
4212 int i, j, n;
4213 int offset, total;
4214 isl_int sum;
4215 isl_basic_map *hull;
4217 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4218 if (!hull)
4219 goto error;
4221 isl_int_init(sum);
4222 dim = isl_map_dim(map, isl_dim_out);
4223 offset = isl_basic_map_offset(hull, isl_dim_out);
4224 total = 1 + isl_basic_map_total_dim(hull);
4225 n = hull->n_ineq;
4226 for (d = 0; d < dim; ++d) {
4227 for (i = 0; i < n; ++i) {
4228 if (!is_potential_div_constraint(hull->ineq[i],
4229 offset, d, total))
4230 continue;
4231 for (j = i + 1; j < n; ++j) {
4232 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4233 hull->ineq[j] + 1, total - 1))
4234 continue;
4235 isl_int_add(sum, hull->ineq[i][0],
4236 hull->ineq[j][0]);
4237 if (isl_int_abs_lt(sum,
4238 hull->ineq[i][offset + d]))
4239 break;
4242 if (j >= n)
4243 continue;
4244 isl_int_clear(sum);
4245 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4246 j = i;
4247 return pw_multi_aff_from_map_div(map, hull, d, j);
4250 isl_int_clear(sum);
4251 isl_basic_map_free(hull);
4252 return pw_multi_aff_from_map_base(map);
4253 error:
4254 isl_map_free(map);
4255 isl_basic_map_free(hull);
4256 return NULL;
4259 /* Given an affine expression
4261 * [A -> B] -> f(A,B)
4263 * construct an isl_multi_aff
4265 * [A -> B] -> B'
4267 * such that dimension "d" in B' is set to "aff" and the remaining
4268 * dimensions are set equal to the corresponding dimensions in B.
4269 * "n_in" is the dimension of the space A.
4270 * "n_out" is the dimension of the space B.
4272 * If "is_set" is set, then the affine expression is of the form
4274 * [B] -> f(B)
4276 * and we construct an isl_multi_aff
4278 * B -> B'
4280 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4281 unsigned n_in, unsigned n_out, int is_set)
4283 int i;
4284 isl_multi_aff *ma;
4285 isl_space *space, *space2;
4286 isl_local_space *ls;
4288 space = isl_aff_get_domain_space(aff);
4289 ls = isl_local_space_from_space(isl_space_copy(space));
4290 space2 = isl_space_copy(space);
4291 if (!is_set)
4292 space2 = isl_space_range(isl_space_unwrap(space2));
4293 space = isl_space_map_from_domain_and_range(space, space2);
4294 ma = isl_multi_aff_alloc(space);
4295 ma = isl_multi_aff_set_aff(ma, d, aff);
4297 for (i = 0; i < n_out; ++i) {
4298 if (i == d)
4299 continue;
4300 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4301 isl_dim_set, n_in + i);
4302 ma = isl_multi_aff_set_aff(ma, i, aff);
4305 isl_local_space_free(ls);
4307 return ma;
4310 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4311 * taking into account that the dimension at position "d" can be written as
4313 * x = m a + f(..) (1)
4315 * where m is equal to "gcd".
4316 * "i" is the index of the equality in "hull" that defines f(..).
4317 * In particular, the equality is of the form
4319 * f(..) - x + m g(existentials) = 0
4321 * or
4323 * -f(..) + x + m g(existentials) = 0
4325 * We basically plug (1) into "map", resulting in a map with "a"
4326 * in the range instead of "x". The corresponding isl_pw_multi_aff
4327 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4329 * Specifically, given the input map
4331 * A -> B
4333 * We first wrap it into a set
4335 * [A -> B]
4337 * and define (1) on top of the corresponding space, resulting in "aff".
4338 * We use this to create an isl_multi_aff that maps the output position "d"
4339 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4340 * We plug this into the wrapped map, unwrap the result and compute the
4341 * corresponding isl_pw_multi_aff.
4342 * The result is an expression
4344 * A -> T(A)
4346 * We adjust that to
4348 * A -> [A -> T(A)]
4350 * so that we can plug that into "aff", after extending the latter to
4351 * a mapping
4353 * [A -> B] -> B'
4356 * If "map" is actually a set, then there is no "A" space, meaning
4357 * that we do not need to perform any wrapping, and that the result
4358 * of the recursive call is of the form
4360 * [T]
4362 * which is plugged into a mapping of the form
4364 * B -> B'
4366 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4367 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4368 isl_int gcd)
4370 isl_set *set;
4371 isl_space *space;
4372 isl_local_space *ls;
4373 isl_aff *aff;
4374 isl_multi_aff *ma;
4375 isl_pw_multi_aff *pma, *id;
4376 unsigned n_in;
4377 unsigned o_out;
4378 unsigned n_out;
4379 int is_set;
4381 is_set = isl_map_is_set(map);
4383 n_in = isl_basic_map_dim(hull, isl_dim_in);
4384 n_out = isl_basic_map_dim(hull, isl_dim_out);
4385 o_out = isl_basic_map_offset(hull, isl_dim_out);
4387 if (is_set)
4388 set = map;
4389 else
4390 set = isl_map_wrap(map);
4391 space = isl_space_map_from_set(isl_set_get_space(set));
4392 ma = isl_multi_aff_identity(space);
4393 ls = isl_local_space_from_space(isl_set_get_space(set));
4394 aff = isl_aff_alloc(ls);
4395 if (aff) {
4396 isl_int_set_si(aff->v->el[0], 1);
4397 if (isl_int_is_one(hull->eq[i][o_out + d]))
4398 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4399 aff->v->size - 1);
4400 else
4401 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4402 aff->v->size - 1);
4403 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4405 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4406 set = isl_set_preimage_multi_aff(set, ma);
4408 ma = range_map(aff, d, n_in, n_out, is_set);
4410 if (is_set)
4411 map = set;
4412 else
4413 map = isl_set_unwrap(set);
4414 pma = isl_pw_multi_aff_from_map(set);
4416 if (!is_set) {
4417 space = isl_pw_multi_aff_get_domain_space(pma);
4418 space = isl_space_map_from_set(space);
4419 id = isl_pw_multi_aff_identity(space);
4420 pma = isl_pw_multi_aff_range_product(id, pma);
4422 id = isl_pw_multi_aff_from_multi_aff(ma);
4423 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4425 isl_basic_map_free(hull);
4426 return pma;
4429 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4431 * As a special case, we first check if all output dimensions are uniquely
4432 * defined in terms of the parameters and input dimensions over the entire
4433 * domain. If so, we extract the desired isl_pw_multi_aff directly
4434 * from the affine hull of "map" and its domain.
4436 * Otherwise, we check if any of the output dimensions is "strided".
4437 * That is, we check if can be written as
4439 * x = m a + f(..)
4441 * with m greater than 1, a some combination of existentiall quantified
4442 * variables and f and expression in the parameters and input dimensions.
4443 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4445 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4446 * special case.
4448 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4450 int i, j;
4451 int sv;
4452 isl_basic_map *hull;
4453 unsigned n_out;
4454 unsigned o_out;
4455 unsigned n_div;
4456 unsigned o_div;
4457 isl_int gcd;
4459 if (!map)
4460 return NULL;
4462 hull = isl_map_affine_hull(isl_map_copy(map));
4463 sv = isl_basic_map_plain_is_single_valued(hull);
4464 if (sv >= 0 && sv)
4465 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4466 if (sv < 0)
4467 hull = isl_basic_map_free(hull);
4468 if (!hull)
4469 goto error;
4471 n_div = isl_basic_map_dim(hull, isl_dim_div);
4472 o_div = isl_basic_map_offset(hull, isl_dim_div);
4474 if (n_div == 0) {
4475 isl_basic_map_free(hull);
4476 return pw_multi_aff_from_map_check_div(map);
4479 isl_int_init(gcd);
4481 n_out = isl_basic_map_dim(hull, isl_dim_out);
4482 o_out = isl_basic_map_offset(hull, isl_dim_out);
4484 for (i = 0; i < n_out; ++i) {
4485 for (j = 0; j < hull->n_eq; ++j) {
4486 isl_int *eq = hull->eq[j];
4487 isl_pw_multi_aff *res;
4489 if (!isl_int_is_one(eq[o_out + i]) &&
4490 !isl_int_is_negone(eq[o_out + i]))
4491 continue;
4492 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4493 continue;
4494 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4495 n_out - (i + 1)) != -1)
4496 continue;
4497 isl_seq_gcd(eq + o_div, n_div, &gcd);
4498 if (isl_int_is_zero(gcd))
4499 continue;
4500 if (isl_int_is_one(gcd))
4501 continue;
4503 res = pw_multi_aff_from_map_stride(map, hull,
4504 i, j, gcd);
4505 isl_int_clear(gcd);
4506 return res;
4510 isl_int_clear(gcd);
4511 isl_basic_map_free(hull);
4512 return pw_multi_aff_from_map_check_div(map);
4513 error:
4514 isl_map_free(map);
4515 return NULL;
4518 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4520 return isl_pw_multi_aff_from_map(set);
4523 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4524 * add it to *user.
4526 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4528 isl_union_pw_multi_aff **upma = user;
4529 isl_pw_multi_aff *pma;
4531 pma = isl_pw_multi_aff_from_map(map);
4532 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4534 return *upma ? 0 : -1;
4537 /* Try and create an isl_union_pw_multi_aff that is equivalent
4538 * to the given isl_union_map.
4539 * The isl_union_map is required to be single-valued in each space.
4540 * Otherwise, an error is produced.
4542 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4543 __isl_take isl_union_map *umap)
4545 isl_space *space;
4546 isl_union_pw_multi_aff *upma;
4548 space = isl_union_map_get_space(umap);
4549 upma = isl_union_pw_multi_aff_empty(space);
4550 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4551 upma = isl_union_pw_multi_aff_free(upma);
4552 isl_union_map_free(umap);
4554 return upma;
4557 /* Try and create an isl_union_pw_multi_aff that is equivalent
4558 * to the given isl_union_set.
4559 * The isl_union_set is required to be a singleton in each space.
4560 * Otherwise, an error is produced.
4562 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4563 __isl_take isl_union_set *uset)
4565 return isl_union_pw_multi_aff_from_union_map(uset);
4568 /* Return the piecewise affine expression "set ? 1 : 0".
4570 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4572 isl_pw_aff *pa;
4573 isl_space *space = isl_set_get_space(set);
4574 isl_local_space *ls = isl_local_space_from_space(space);
4575 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4576 isl_aff *one = isl_aff_zero_on_domain(ls);
4578 one = isl_aff_add_constant_si(one, 1);
4579 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4580 set = isl_set_complement(set);
4581 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4583 return pa;
4586 /* Plug in "subs" for dimension "type", "pos" of "aff".
4588 * Let i be the dimension to replace and let "subs" be of the form
4590 * f/d
4592 * and "aff" of the form
4594 * (a i + g)/m
4596 * The result is
4598 * (a f + d g')/(m d)
4600 * where g' is the result of plugging in "subs" in each of the integer
4601 * divisions in g.
4603 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
4604 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
4606 isl_ctx *ctx;
4607 isl_int v;
4609 aff = isl_aff_cow(aff);
4610 if (!aff || !subs)
4611 return isl_aff_free(aff);
4613 ctx = isl_aff_get_ctx(aff);
4614 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
4615 isl_die(ctx, isl_error_invalid,
4616 "spaces don't match", return isl_aff_free(aff));
4617 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
4618 isl_die(ctx, isl_error_unsupported,
4619 "cannot handle divs yet", return isl_aff_free(aff));
4621 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
4622 if (!aff->ls)
4623 return isl_aff_free(aff);
4625 aff->v = isl_vec_cow(aff->v);
4626 if (!aff->v)
4627 return isl_aff_free(aff);
4629 pos += isl_local_space_offset(aff->ls, type);
4631 isl_int_init(v);
4632 isl_seq_substitute(aff->v->el, pos, subs->v->el,
4633 aff->v->size, subs->v->size, v);
4634 isl_int_clear(v);
4636 return aff;
4639 /* Plug in "subs" for dimension "type", "pos" in each of the affine
4640 * expressions in "maff".
4642 __isl_give isl_multi_aff *isl_multi_aff_substitute(
4643 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
4644 __isl_keep isl_aff *subs)
4646 int i;
4648 maff = isl_multi_aff_cow(maff);
4649 if (!maff || !subs)
4650 return isl_multi_aff_free(maff);
4652 if (type == isl_dim_in)
4653 type = isl_dim_set;
4655 for (i = 0; i < maff->n; ++i) {
4656 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
4657 if (!maff->p[i])
4658 return isl_multi_aff_free(maff);
4661 return maff;
4664 /* Plug in "subs" for dimension "type", "pos" of "pma".
4666 * pma is of the form
4668 * A_i(v) -> M_i(v)
4670 * while subs is of the form
4672 * v' = B_j(v) -> S_j
4674 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4675 * has a contribution in the result, in particular
4677 * C_ij(S_j) -> M_i(S_j)
4679 * Note that plugging in S_j in C_ij may also result in an empty set
4680 * and this contribution should simply be discarded.
4682 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4683 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4684 __isl_keep isl_pw_aff *subs)
4686 int i, j, n;
4687 isl_pw_multi_aff *res;
4689 if (!pma || !subs)
4690 return isl_pw_multi_aff_free(pma);
4692 n = pma->n * subs->n;
4693 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4695 for (i = 0; i < pma->n; ++i) {
4696 for (j = 0; j < subs->n; ++j) {
4697 isl_set *common;
4698 isl_multi_aff *res_ij;
4699 int empty;
4701 common = isl_set_intersect(
4702 isl_set_copy(pma->p[i].set),
4703 isl_set_copy(subs->p[j].set));
4704 common = isl_set_substitute(common,
4705 type, pos, subs->p[j].aff);
4706 empty = isl_set_plain_is_empty(common);
4707 if (empty < 0 || empty) {
4708 isl_set_free(common);
4709 if (empty < 0)
4710 goto error;
4711 continue;
4714 res_ij = isl_multi_aff_substitute(
4715 isl_multi_aff_copy(pma->p[i].maff),
4716 type, pos, subs->p[j].aff);
4718 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4722 isl_pw_multi_aff_free(pma);
4723 return res;
4724 error:
4725 isl_pw_multi_aff_free(pma);
4726 isl_pw_multi_aff_free(res);
4727 return NULL;
4730 /* Compute the preimage of a range of dimensions in the affine expression "src"
4731 * under "ma" and put the result in "dst". The number of dimensions in "src"
4732 * that precede the range is given by "n_before". The number of dimensions
4733 * in the range is given by the number of output dimensions of "ma".
4734 * The number of dimensions that follow the range is given by "n_after".
4735 * If "has_denom" is set (to one),
4736 * then "src" and "dst" have an extra initial denominator.
4737 * "n_div_ma" is the number of existentials in "ma"
4738 * "n_div_bset" is the number of existentials in "src"
4739 * The resulting "dst" (which is assumed to have been allocated by
4740 * the caller) contains coefficients for both sets of existentials,
4741 * first those in "ma" and then those in "src".
4742 * f, c1, c2 and g are temporary objects that have been initialized
4743 * by the caller.
4745 * Let src represent the expression
4747 * (a(p) + f_u u + b v + f_w w + c(divs))/d
4749 * and let ma represent the expressions
4751 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4753 * We start out with the following expression for dst:
4755 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4757 * with the multiplication factor f initially equal to 1
4758 * and f \sum_i b_i v_i kept separately.
4759 * For each x_i that we substitute, we multiply the numerator
4760 * (and denominator) of dst by c_1 = m_i and add the numerator
4761 * of the x_i expression multiplied by c_2 = f b_i,
4762 * after removing the common factors of c_1 and c_2.
4763 * The multiplication factor f also needs to be multiplied by c_1
4764 * for the next x_j, j > i.
4766 void isl_seq_preimage(isl_int *dst, isl_int *src,
4767 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4768 int n_div_ma, int n_div_bmap,
4769 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4771 int i;
4772 int n_param, n_in, n_out;
4773 int o_dst, o_src;
4775 n_param = isl_multi_aff_dim(ma, isl_dim_param);
4776 n_in = isl_multi_aff_dim(ma, isl_dim_in);
4777 n_out = isl_multi_aff_dim(ma, isl_dim_out);
4779 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4780 o_dst = o_src = has_denom + 1 + n_param + n_before;
4781 isl_seq_clr(dst + o_dst, n_in);
4782 o_dst += n_in;
4783 o_src += n_out;
4784 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4785 o_dst += n_after;
4786 o_src += n_after;
4787 isl_seq_clr(dst + o_dst, n_div_ma);
4788 o_dst += n_div_ma;
4789 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4791 isl_int_set_si(f, 1);
4793 for (i = 0; i < n_out; ++i) {
4794 int offset = has_denom + 1 + n_param + n_before + i;
4796 if (isl_int_is_zero(src[offset]))
4797 continue;
4798 isl_int_set(c1, ma->p[i]->v->el[0]);
4799 isl_int_mul(c2, f, src[offset]);
4800 isl_int_gcd(g, c1, c2);
4801 isl_int_divexact(c1, c1, g);
4802 isl_int_divexact(c2, c2, g);
4804 isl_int_mul(f, f, c1);
4805 o_dst = has_denom;
4806 o_src = 1;
4807 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4808 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4809 o_dst += 1 + n_param;
4810 o_src += 1 + n_param;
4811 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4812 o_dst += n_before;
4813 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4814 c2, ma->p[i]->v->el + o_src, n_in);
4815 o_dst += n_in;
4816 o_src += n_in;
4817 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4818 o_dst += n_after;
4819 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4820 c2, ma->p[i]->v->el + o_src, n_div_ma);
4821 o_dst += n_div_ma;
4822 o_src += n_div_ma;
4823 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4824 if (has_denom)
4825 isl_int_mul(dst[0], dst[0], c1);
4829 /* Compute the pullback of "aff" by the function represented by "ma".
4830 * In other words, plug in "ma" in "aff". The result is an affine expression
4831 * defined over the domain space of "ma".
4833 * If "aff" is represented by
4835 * (a(p) + b x + c(divs))/d
4837 * and ma is represented by
4839 * x = D(p) + F(y) + G(divs')
4841 * then the result is
4843 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4845 * The divs in the local space of the input are similarly adjusted
4846 * through a call to isl_local_space_preimage_multi_aff.
4848 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4849 __isl_take isl_multi_aff *ma)
4851 isl_aff *res = NULL;
4852 isl_local_space *ls;
4853 int n_div_aff, n_div_ma;
4854 isl_int f, c1, c2, g;
4856 ma = isl_multi_aff_align_divs(ma);
4857 if (!aff || !ma)
4858 goto error;
4860 n_div_aff = isl_aff_dim(aff, isl_dim_div);
4861 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4863 ls = isl_aff_get_domain_local_space(aff);
4864 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4865 res = isl_aff_alloc(ls);
4866 if (!res)
4867 goto error;
4869 isl_int_init(f);
4870 isl_int_init(c1);
4871 isl_int_init(c2);
4872 isl_int_init(g);
4874 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4875 f, c1, c2, g, 1);
4877 isl_int_clear(f);
4878 isl_int_clear(c1);
4879 isl_int_clear(c2);
4880 isl_int_clear(g);
4882 isl_aff_free(aff);
4883 isl_multi_aff_free(ma);
4884 res = isl_aff_normalize(res);
4885 return res;
4886 error:
4887 isl_aff_free(aff);
4888 isl_multi_aff_free(ma);
4889 isl_aff_free(res);
4890 return NULL;
4893 /* Compute the pullback of "aff1" by the function represented by "aff2".
4894 * In other words, plug in "aff2" in "aff1". The result is an affine expression
4895 * defined over the domain space of "aff1".
4897 * The domain of "aff1" should match the range of "aff2", which means
4898 * that it should be single-dimensional.
4900 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
4901 __isl_take isl_aff *aff2)
4903 isl_multi_aff *ma;
4905 ma = isl_multi_aff_from_aff(aff2);
4906 return isl_aff_pullback_multi_aff(aff1, ma);
4909 /* Compute the pullback of "ma1" by the function represented by "ma2".
4910 * In other words, plug in "ma2" in "ma1".
4912 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
4914 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
4915 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4917 int i;
4918 isl_space *space = NULL;
4920 ma2 = isl_multi_aff_align_divs(ma2);
4921 ma1 = isl_multi_aff_cow(ma1);
4922 if (!ma1 || !ma2)
4923 goto error;
4925 space = isl_space_join(isl_multi_aff_get_space(ma2),
4926 isl_multi_aff_get_space(ma1));
4928 for (i = 0; i < ma1->n; ++i) {
4929 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4930 isl_multi_aff_copy(ma2));
4931 if (!ma1->p[i])
4932 goto error;
4935 ma1 = isl_multi_aff_reset_space(ma1, space);
4936 isl_multi_aff_free(ma2);
4937 return ma1;
4938 error:
4939 isl_space_free(space);
4940 isl_multi_aff_free(ma2);
4941 isl_multi_aff_free(ma1);
4942 return NULL;
4945 /* Compute the pullback of "ma1" by the function represented by "ma2".
4946 * In other words, plug in "ma2" in "ma1".
4948 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4949 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4951 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
4952 &isl_multi_aff_pullback_multi_aff_aligned);
4955 /* Extend the local space of "dst" to include the divs
4956 * in the local space of "src".
4958 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4959 __isl_keep isl_aff *src)
4961 isl_ctx *ctx;
4962 int *exp1 = NULL;
4963 int *exp2 = NULL;
4964 isl_mat *div;
4966 if (!src || !dst)
4967 return isl_aff_free(dst);
4969 ctx = isl_aff_get_ctx(src);
4970 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4971 isl_die(ctx, isl_error_invalid,
4972 "spaces don't match", goto error);
4974 if (src->ls->div->n_row == 0)
4975 return dst;
4977 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4978 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4979 if (!exp1 || (dst->ls->div->n_row && !exp2))
4980 goto error;
4982 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4983 dst = isl_aff_expand_divs(dst, div, exp2);
4984 free(exp1);
4985 free(exp2);
4987 return dst;
4988 error:
4989 free(exp1);
4990 free(exp2);
4991 return isl_aff_free(dst);
4994 /* Adjust the local spaces of the affine expressions in "maff"
4995 * such that they all have the save divs.
4997 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4998 __isl_take isl_multi_aff *maff)
5000 int i;
5002 if (!maff)
5003 return NULL;
5004 if (maff->n == 0)
5005 return maff;
5006 maff = isl_multi_aff_cow(maff);
5007 if (!maff)
5008 return NULL;
5010 for (i = 1; i < maff->n; ++i)
5011 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5012 for (i = 1; i < maff->n; ++i) {
5013 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5014 if (!maff->p[i])
5015 return isl_multi_aff_free(maff);
5018 return maff;
5021 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5023 aff = isl_aff_cow(aff);
5024 if (!aff)
5025 return NULL;
5027 aff->ls = isl_local_space_lift(aff->ls);
5028 if (!aff->ls)
5029 return isl_aff_free(aff);
5031 return aff;
5034 /* Lift "maff" to a space with extra dimensions such that the result
5035 * has no more existentially quantified variables.
5036 * If "ls" is not NULL, then *ls is assigned the local space that lies
5037 * at the basis of the lifting applied to "maff".
5039 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5040 __isl_give isl_local_space **ls)
5042 int i;
5043 isl_space *space;
5044 unsigned n_div;
5046 if (ls)
5047 *ls = NULL;
5049 if (!maff)
5050 return NULL;
5052 if (maff->n == 0) {
5053 if (ls) {
5054 isl_space *space = isl_multi_aff_get_domain_space(maff);
5055 *ls = isl_local_space_from_space(space);
5056 if (!*ls)
5057 return isl_multi_aff_free(maff);
5059 return maff;
5062 maff = isl_multi_aff_cow(maff);
5063 maff = isl_multi_aff_align_divs(maff);
5064 if (!maff)
5065 return NULL;
5067 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5068 space = isl_multi_aff_get_space(maff);
5069 space = isl_space_lift(isl_space_domain(space), n_div);
5070 space = isl_space_extend_domain_with_range(space,
5071 isl_multi_aff_get_space(maff));
5072 if (!space)
5073 return isl_multi_aff_free(maff);
5074 isl_space_free(maff->space);
5075 maff->space = space;
5077 if (ls) {
5078 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5079 if (!*ls)
5080 return isl_multi_aff_free(maff);
5083 for (i = 0; i < maff->n; ++i) {
5084 maff->p[i] = isl_aff_lift(maff->p[i]);
5085 if (!maff->p[i])
5086 goto error;
5089 return maff;
5090 error:
5091 if (ls)
5092 isl_local_space_free(*ls);
5093 return isl_multi_aff_free(maff);
5097 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5099 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5100 __isl_keep isl_pw_multi_aff *pma, int pos)
5102 int i;
5103 int n_out;
5104 isl_space *space;
5105 isl_pw_aff *pa;
5107 if (!pma)
5108 return NULL;
5110 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5111 if (pos < 0 || pos >= n_out)
5112 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5113 "index out of bounds", return NULL);
5115 space = isl_pw_multi_aff_get_space(pma);
5116 space = isl_space_drop_dims(space, isl_dim_out,
5117 pos + 1, n_out - pos - 1);
5118 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5120 pa = isl_pw_aff_alloc_size(space, pma->n);
5121 for (i = 0; i < pma->n; ++i) {
5122 isl_aff *aff;
5123 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5124 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5127 return pa;
5130 /* Return an isl_pw_multi_aff with the given "set" as domain and
5131 * an unnamed zero-dimensional range.
5133 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5134 __isl_take isl_set *set)
5136 isl_multi_aff *ma;
5137 isl_space *space;
5139 space = isl_set_get_space(set);
5140 space = isl_space_from_domain(space);
5141 ma = isl_multi_aff_zero(space);
5142 return isl_pw_multi_aff_alloc(set, ma);
5145 /* Add an isl_pw_multi_aff with the given "set" as domain and
5146 * an unnamed zero-dimensional range to *user.
5148 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5150 isl_union_pw_multi_aff **upma = user;
5151 isl_pw_multi_aff *pma;
5153 pma = isl_pw_multi_aff_from_domain(set);
5154 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5156 return 0;
5159 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5160 * an unnamed zero-dimensional range.
5162 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5163 __isl_take isl_union_set *uset)
5165 isl_space *space;
5166 isl_union_pw_multi_aff *upma;
5168 if (!uset)
5169 return NULL;
5171 space = isl_union_set_get_space(uset);
5172 upma = isl_union_pw_multi_aff_empty(space);
5174 if (isl_union_set_foreach_set(uset,
5175 &add_pw_multi_aff_from_domain, &upma) < 0)
5176 goto error;
5178 isl_union_set_free(uset);
5179 return upma;
5180 error:
5181 isl_union_set_free(uset);
5182 isl_union_pw_multi_aff_free(upma);
5183 return NULL;
5186 /* Convert "pma" to an isl_map and add it to *umap.
5188 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5190 isl_union_map **umap = user;
5191 isl_map *map;
5193 map = isl_map_from_pw_multi_aff(pma);
5194 *umap = isl_union_map_add_map(*umap, map);
5196 return 0;
5199 /* Construct a union map mapping the domain of the union
5200 * piecewise multi-affine expression to its range, with each dimension
5201 * in the range equated to the corresponding affine expression on its cell.
5203 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5204 __isl_take isl_union_pw_multi_aff *upma)
5206 isl_space *space;
5207 isl_union_map *umap;
5209 if (!upma)
5210 return NULL;
5212 space = isl_union_pw_multi_aff_get_space(upma);
5213 umap = isl_union_map_empty(space);
5215 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5216 &map_from_pw_multi_aff, &umap) < 0)
5217 goto error;
5219 isl_union_pw_multi_aff_free(upma);
5220 return umap;
5221 error:
5222 isl_union_pw_multi_aff_free(upma);
5223 isl_union_map_free(umap);
5224 return NULL;
5227 /* Local data for bin_entry and the callback "fn".
5229 struct isl_union_pw_multi_aff_bin_data {
5230 isl_union_pw_multi_aff *upma2;
5231 isl_union_pw_multi_aff *res;
5232 isl_pw_multi_aff *pma;
5233 int (*fn)(void **entry, void *user);
5236 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5237 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5239 static int bin_entry(void **entry, void *user)
5241 struct isl_union_pw_multi_aff_bin_data *data = user;
5242 isl_pw_multi_aff *pma = *entry;
5244 data->pma = pma;
5245 if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
5246 data->fn, data) < 0)
5247 return -1;
5249 return 0;
5252 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5253 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5254 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5255 * as *entry. The callback should adjust data->res if desired.
5257 static __isl_give isl_union_pw_multi_aff *bin_op(
5258 __isl_take isl_union_pw_multi_aff *upma1,
5259 __isl_take isl_union_pw_multi_aff *upma2,
5260 int (*fn)(void **entry, void *user))
5262 isl_space *space;
5263 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5265 space = isl_union_pw_multi_aff_get_space(upma2);
5266 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5267 space = isl_union_pw_multi_aff_get_space(upma1);
5268 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5270 if (!upma1 || !upma2)
5271 goto error;
5273 data.upma2 = upma2;
5274 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
5275 upma1->table.n);
5276 if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
5277 &bin_entry, &data) < 0)
5278 goto error;
5280 isl_union_pw_multi_aff_free(upma1);
5281 isl_union_pw_multi_aff_free(upma2);
5282 return data.res;
5283 error:
5284 isl_union_pw_multi_aff_free(upma1);
5285 isl_union_pw_multi_aff_free(upma2);
5286 isl_union_pw_multi_aff_free(data.res);
5287 return NULL;
5290 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5291 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5293 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5294 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5296 isl_space *space;
5298 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5299 isl_pw_multi_aff_get_space(pma2));
5300 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5301 &isl_multi_aff_range_product);
5304 /* Given two isl_pw_multi_affs A -> B and C -> D,
5305 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5307 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5308 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5310 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5311 &pw_multi_aff_range_product);
5314 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5315 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5317 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5318 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5320 isl_space *space;
5322 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5323 isl_pw_multi_aff_get_space(pma2));
5324 space = isl_space_flatten_range(space);
5325 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5326 &isl_multi_aff_flat_range_product);
5329 /* Given two isl_pw_multi_affs A -> B and C -> D,
5330 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5332 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5333 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5335 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5336 &pw_multi_aff_flat_range_product);
5339 /* If data->pma and *entry have the same domain space, then compute
5340 * their flat range product and the result to data->res.
5342 static int flat_range_product_entry(void **entry, void *user)
5344 struct isl_union_pw_multi_aff_bin_data *data = user;
5345 isl_pw_multi_aff *pma2 = *entry;
5347 if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
5348 pma2->dim, isl_dim_in))
5349 return 0;
5351 pma2 = isl_pw_multi_aff_flat_range_product(
5352 isl_pw_multi_aff_copy(data->pma),
5353 isl_pw_multi_aff_copy(pma2));
5355 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5357 return 0;
5360 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5361 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5363 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5364 __isl_take isl_union_pw_multi_aff *upma1,
5365 __isl_take isl_union_pw_multi_aff *upma2)
5367 return bin_op(upma1, upma2, &flat_range_product_entry);
5370 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5371 * The parameters are assumed to have been aligned.
5373 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5374 * except that it works on two different isl_pw_* types.
5376 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5377 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5378 __isl_take isl_pw_aff *pa)
5380 int i, j, n;
5381 isl_pw_multi_aff *res = NULL;
5383 if (!pma || !pa)
5384 goto error;
5386 if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
5387 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5388 "domains don't match", goto error);
5389 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5390 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5391 "index out of bounds", goto error);
5393 n = pma->n * pa->n;
5394 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5396 for (i = 0; i < pma->n; ++i) {
5397 for (j = 0; j < pa->n; ++j) {
5398 isl_set *common;
5399 isl_multi_aff *res_ij;
5400 int empty;
5402 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5403 isl_set_copy(pa->p[j].set));
5404 empty = isl_set_plain_is_empty(common);
5405 if (empty < 0 || empty) {
5406 isl_set_free(common);
5407 if (empty < 0)
5408 goto error;
5409 continue;
5412 res_ij = isl_multi_aff_set_aff(
5413 isl_multi_aff_copy(pma->p[i].maff), pos,
5414 isl_aff_copy(pa->p[j].aff));
5415 res_ij = isl_multi_aff_gist(res_ij,
5416 isl_set_copy(common));
5418 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5422 isl_pw_multi_aff_free(pma);
5423 isl_pw_aff_free(pa);
5424 return res;
5425 error:
5426 isl_pw_multi_aff_free(pma);
5427 isl_pw_aff_free(pa);
5428 return isl_pw_multi_aff_free(res);
5431 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5433 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5434 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5435 __isl_take isl_pw_aff *pa)
5437 if (!pma || !pa)
5438 goto error;
5439 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5440 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5441 if (!isl_space_has_named_params(pma->dim) ||
5442 !isl_space_has_named_params(pa->dim))
5443 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5444 "unaligned unnamed parameters", goto error);
5445 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5446 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5447 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5448 error:
5449 isl_pw_multi_aff_free(pma);
5450 isl_pw_aff_free(pa);
5451 return NULL;
5454 /* Do the parameters of "pa" match those of "space"?
5456 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5457 __isl_keep isl_space *space)
5459 isl_space *pa_space;
5460 int match;
5462 if (!pa || !space)
5463 return -1;
5465 pa_space = isl_pw_aff_get_space(pa);
5467 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5469 isl_space_free(pa_space);
5470 return match;
5473 /* Check that the domain space of "pa" matches "space".
5475 * Return 0 on success and -1 on error.
5477 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5478 __isl_keep isl_space *space)
5480 isl_space *pa_space;
5481 int match;
5483 if (!pa || !space)
5484 return -1;
5486 pa_space = isl_pw_aff_get_space(pa);
5488 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5489 if (match < 0)
5490 goto error;
5491 if (!match)
5492 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5493 "parameters don't match", goto error);
5494 match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
5495 if (match < 0)
5496 goto error;
5497 if (!match)
5498 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5499 "domains don't match", goto error);
5500 isl_space_free(pa_space);
5501 return 0;
5502 error:
5503 isl_space_free(pa_space);
5504 return -1;
5507 #undef BASE
5508 #define BASE pw_aff
5510 #include <isl_multi_templ.c>
5512 /* Scale the elements of "pma" by the corresponding elements of "mv".
5514 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5515 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5517 int i;
5519 pma = isl_pw_multi_aff_cow(pma);
5520 if (!pma || !mv)
5521 goto error;
5522 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5523 mv->space, isl_dim_set))
5524 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5525 "spaces don't match", goto error);
5526 if (!isl_space_match(pma->dim, isl_dim_param,
5527 mv->space, isl_dim_param)) {
5528 pma = isl_pw_multi_aff_align_params(pma,
5529 isl_multi_val_get_space(mv));
5530 mv = isl_multi_val_align_params(mv,
5531 isl_pw_multi_aff_get_space(pma));
5532 if (!pma || !mv)
5533 goto error;
5536 for (i = 0; i < pma->n; ++i) {
5537 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5538 isl_multi_val_copy(mv));
5539 if (!pma->p[i].maff)
5540 goto error;
5543 isl_multi_val_free(mv);
5544 return pma;
5545 error:
5546 isl_multi_val_free(mv);
5547 isl_pw_multi_aff_free(pma);
5548 return NULL;
5551 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5552 * mv contains the mv argument.
5553 * res collects the results.
5555 struct isl_union_pw_multi_aff_scale_multi_val_data {
5556 isl_multi_val *mv;
5557 isl_union_pw_multi_aff *res;
5560 /* This function is called for each entry of an isl_union_pw_multi_aff.
5561 * If the space of the entry matches that of data->mv,
5562 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5563 * to data->res.
5565 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5567 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5568 isl_pw_multi_aff *pma = *entry;
5570 if (!pma)
5571 return -1;
5572 if (!isl_space_tuple_match(pma->dim, isl_dim_out,
5573 data->mv->space, isl_dim_set))
5574 return 0;
5576 pma = isl_pw_multi_aff_copy(pma);
5577 pma = isl_pw_multi_aff_scale_multi_val(pma,
5578 isl_multi_val_copy(data->mv));
5579 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5580 if (!data->res)
5581 return -1;
5583 return 0;
5586 /* Scale the elements of "upma" by the corresponding elements of "mv",
5587 * for those entries that match the space of "mv".
5589 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
5590 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
5592 struct isl_union_pw_multi_aff_scale_multi_val_data data;
5594 upma = isl_union_pw_multi_aff_align_params(upma,
5595 isl_multi_val_get_space(mv));
5596 mv = isl_multi_val_align_params(mv,
5597 isl_union_pw_multi_aff_get_space(upma));
5598 if (!upma || !mv)
5599 goto error;
5601 data.mv = mv;
5602 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->dim),
5603 upma->table.n);
5604 if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
5605 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
5606 goto error;
5608 isl_multi_val_free(mv);
5609 isl_union_pw_multi_aff_free(upma);
5610 return data.res;
5611 error:
5612 isl_multi_val_free(mv);
5613 isl_union_pw_multi_aff_free(upma);
5614 return NULL;
5617 /* Construct and return a piecewise multi affine expression
5618 * in the given space with value zero in each of the output dimensions and
5619 * a universe domain.
5621 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
5623 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
5626 /* Construct and return a piecewise multi affine expression
5627 * that is equal to the given piecewise affine expression.
5629 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
5630 __isl_take isl_pw_aff *pa)
5632 int i;
5633 isl_space *space;
5634 isl_pw_multi_aff *pma;
5636 if (!pa)
5637 return NULL;
5639 space = isl_pw_aff_get_space(pa);
5640 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
5642 for (i = 0; i < pa->n; ++i) {
5643 isl_set *set;
5644 isl_multi_aff *ma;
5646 set = isl_set_copy(pa->p[i].set);
5647 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
5648 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
5651 isl_pw_aff_free(pa);
5652 return pma;
5655 /* Construct a set or map mapping the shared (parameter) domain
5656 * of the piecewise affine expressions to the range of "mpa"
5657 * with each dimension in the range equated to the
5658 * corresponding piecewise affine expression.
5660 static __isl_give isl_map *map_from_multi_pw_aff(
5661 __isl_take isl_multi_pw_aff *mpa)
5663 int i;
5664 isl_space *space;
5665 isl_map *map;
5667 if (!mpa)
5668 return NULL;
5670 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
5671 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5672 "invalid space", return isl_multi_pw_aff_free(mpa));
5674 space = isl_multi_pw_aff_get_domain_space(mpa);
5675 map = isl_map_universe(isl_space_from_domain(space));
5677 for (i = 0; i < mpa->n; ++i) {
5678 isl_pw_aff *pa;
5679 isl_map *map_i;
5681 pa = isl_pw_aff_copy(mpa->p[i]);
5682 map_i = map_from_pw_aff(pa);
5684 map = isl_map_flat_range_product(map, map_i);
5687 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
5689 isl_multi_pw_aff_free(mpa);
5690 return map;
5693 /* Construct a map mapping the shared domain
5694 * of the piecewise affine expressions to the range of "mpa"
5695 * with each dimension in the range equated to the
5696 * corresponding piecewise affine expression.
5698 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5700 if (!mpa)
5701 return NULL;
5702 if (isl_space_is_set(mpa->space))
5703 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5704 "space of input is not a map", goto error);
5706 return map_from_multi_pw_aff(mpa);
5707 error:
5708 isl_multi_pw_aff_free(mpa);
5709 return NULL;
5712 /* Construct a set mapping the shared parameter domain
5713 * of the piecewise affine expressions to the space of "mpa"
5714 * with each dimension in the range equated to the
5715 * corresponding piecewise affine expression.
5717 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
5719 if (!mpa)
5720 return NULL;
5721 if (!isl_space_is_set(mpa->space))
5722 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
5723 "space of input is not a set", goto error);
5725 return map_from_multi_pw_aff(mpa);
5726 error:
5727 isl_multi_pw_aff_free(mpa);
5728 return NULL;
5731 /* Construct and return a piecewise multi affine expression
5732 * that is equal to the given multi piecewise affine expression
5733 * on the shared domain of the piecewise affine expressions.
5735 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
5736 __isl_take isl_multi_pw_aff *mpa)
5738 int i;
5739 isl_space *space;
5740 isl_pw_aff *pa;
5741 isl_pw_multi_aff *pma;
5743 if (!mpa)
5744 return NULL;
5746 space = isl_multi_pw_aff_get_space(mpa);
5748 if (mpa->n == 0) {
5749 isl_multi_pw_aff_free(mpa);
5750 return isl_pw_multi_aff_zero(space);
5753 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
5754 pma = isl_pw_multi_aff_from_pw_aff(pa);
5756 for (i = 1; i < mpa->n; ++i) {
5757 isl_pw_multi_aff *pma_i;
5759 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
5760 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
5761 pma = isl_pw_multi_aff_range_product(pma, pma_i);
5764 pma = isl_pw_multi_aff_reset_space(pma, space);
5766 isl_multi_pw_aff_free(mpa);
5767 return pma;
5770 /* Construct and return a multi piecewise affine expression
5771 * that is equal to the given multi affine expression.
5773 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
5774 __isl_take isl_multi_aff *ma)
5776 int i, n;
5777 isl_multi_pw_aff *mpa;
5779 if (!ma)
5780 return NULL;
5782 n = isl_multi_aff_dim(ma, isl_dim_out);
5783 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
5785 for (i = 0; i < n; ++i) {
5786 isl_pw_aff *pa;
5788 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
5789 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5792 isl_multi_aff_free(ma);
5793 return mpa;
5796 /* Construct and return a multi piecewise affine expression
5797 * that is equal to the given piecewise multi affine expression.
5799 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
5800 __isl_take isl_pw_multi_aff *pma)
5802 int i, n;
5803 isl_space *space;
5804 isl_multi_pw_aff *mpa;
5806 if (!pma)
5807 return NULL;
5809 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
5810 space = isl_pw_multi_aff_get_space(pma);
5811 mpa = isl_multi_pw_aff_alloc(space);
5813 for (i = 0; i < n; ++i) {
5814 isl_pw_aff *pa;
5816 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
5817 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
5820 isl_pw_multi_aff_free(pma);
5821 return mpa;
5824 /* Do "pa1" and "pa2" represent the same function?
5826 * We first check if they are obviously equal.
5827 * If not, we convert them to maps and check if those are equal.
5829 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
5831 int equal;
5832 isl_map *map1, *map2;
5834 if (!pa1 || !pa2)
5835 return -1;
5837 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
5838 if (equal < 0 || equal)
5839 return equal;
5841 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
5842 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
5843 equal = isl_map_is_equal(map1, map2);
5844 isl_map_free(map1);
5845 isl_map_free(map2);
5847 return equal;
5850 /* Do "mpa1" and "mpa2" represent the same function?
5852 * Note that we cannot convert the entire isl_multi_pw_aff
5853 * to a map because the domains of the piecewise affine expressions
5854 * may not be the same.
5856 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
5857 __isl_keep isl_multi_pw_aff *mpa2)
5859 int i;
5860 int equal;
5862 if (!mpa1 || !mpa2)
5863 return -1;
5865 if (!isl_space_match(mpa1->space, isl_dim_param,
5866 mpa2->space, isl_dim_param)) {
5867 if (!isl_space_has_named_params(mpa1->space))
5868 return 0;
5869 if (!isl_space_has_named_params(mpa2->space))
5870 return 0;
5871 mpa1 = isl_multi_pw_aff_copy(mpa1);
5872 mpa2 = isl_multi_pw_aff_copy(mpa2);
5873 mpa1 = isl_multi_pw_aff_align_params(mpa1,
5874 isl_multi_pw_aff_get_space(mpa2));
5875 mpa2 = isl_multi_pw_aff_align_params(mpa2,
5876 isl_multi_pw_aff_get_space(mpa1));
5877 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
5878 isl_multi_pw_aff_free(mpa1);
5879 isl_multi_pw_aff_free(mpa2);
5880 return equal;
5883 equal = isl_space_is_equal(mpa1->space, mpa2->space);
5884 if (equal < 0 || !equal)
5885 return equal;
5887 for (i = 0; i < mpa1->n; ++i) {
5888 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
5889 if (equal < 0 || !equal)
5890 return equal;
5893 return 1;
5896 /* Coalesce the elements of "mpa".
5898 * Note that such coalescing does not change the meaning of "mpa"
5899 * so there is no need to cow. We do need to be careful not to
5900 * destroy any other copies of "mpa" in case of failure.
5902 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
5903 __isl_take isl_multi_pw_aff *mpa)
5905 int i;
5907 if (!mpa)
5908 return NULL;
5910 for (i = 0; i < mpa->n; ++i) {
5911 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
5912 pa = isl_pw_aff_coalesce(pa);
5913 if (!pa)
5914 return isl_multi_pw_aff_free(mpa);
5915 isl_pw_aff_free(mpa->p[i]);
5916 mpa->p[i] = pa;
5919 return mpa;
5922 /* Compute the pullback of "mpa" by the function represented by "ma".
5923 * In other words, plug in "ma" in "mpa".
5925 * The parameters of "mpa" and "ma" are assumed to have been aligned.
5927 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
5928 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5930 int i;
5931 isl_space *space = NULL;
5933 mpa = isl_multi_pw_aff_cow(mpa);
5934 if (!mpa || !ma)
5935 goto error;
5937 space = isl_space_join(isl_multi_aff_get_space(ma),
5938 isl_multi_pw_aff_get_space(mpa));
5939 if (!space)
5940 goto error;
5942 for (i = 0; i < mpa->n; ++i) {
5943 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
5944 isl_multi_aff_copy(ma));
5945 if (!mpa->p[i])
5946 goto error;
5949 isl_multi_aff_free(ma);
5950 isl_space_free(mpa->space);
5951 mpa->space = space;
5952 return mpa;
5953 error:
5954 isl_space_free(space);
5955 isl_multi_pw_aff_free(mpa);
5956 isl_multi_aff_free(ma);
5957 return NULL;
5960 /* Compute the pullback of "mpa" by the function represented by "ma".
5961 * In other words, plug in "ma" in "mpa".
5963 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
5964 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
5966 if (!mpa || !ma)
5967 goto error;
5968 if (isl_space_match(mpa->space, isl_dim_param,
5969 ma->space, isl_dim_param))
5970 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5971 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
5972 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
5973 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
5974 error:
5975 isl_multi_pw_aff_free(mpa);
5976 isl_multi_aff_free(ma);
5977 return NULL;
5980 /* Compute the pullback of "mpa" by the function represented by "pma".
5981 * In other words, plug in "pma" in "mpa".
5983 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
5985 static __isl_give isl_multi_pw_aff *
5986 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
5987 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
5989 int i;
5990 isl_space *space = NULL;
5992 mpa = isl_multi_pw_aff_cow(mpa);
5993 if (!mpa || !pma)
5994 goto error;
5996 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
5997 isl_multi_pw_aff_get_space(mpa));
5999 for (i = 0; i < mpa->n; ++i) {
6000 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6001 isl_pw_multi_aff_copy(pma));
6002 if (!mpa->p[i])
6003 goto error;
6006 isl_pw_multi_aff_free(pma);
6007 isl_space_free(mpa->space);
6008 mpa->space = space;
6009 return mpa;
6010 error:
6011 isl_space_free(space);
6012 isl_multi_pw_aff_free(mpa);
6013 isl_pw_multi_aff_free(pma);
6014 return NULL;
6017 /* Compute the pullback of "mpa" by the function represented by "pma".
6018 * In other words, plug in "pma" in "mpa".
6020 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6021 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6023 if (!mpa || !pma)
6024 goto error;
6025 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6026 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6027 mpa = isl_multi_pw_aff_align_params(mpa,
6028 isl_pw_multi_aff_get_space(pma));
6029 pma = isl_pw_multi_aff_align_params(pma,
6030 isl_multi_pw_aff_get_space(mpa));
6031 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6032 error:
6033 isl_multi_pw_aff_free(mpa);
6034 isl_pw_multi_aff_free(pma);
6035 return NULL;
6038 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6039 * with the domain of "aff". The domain of the result is the same
6040 * as that of "mpa".
6041 * "mpa" and "aff" are assumed to have been aligned.
6043 * We first extract the parametric constant from "aff", defined
6044 * over the correct domain.
6045 * Then we add the appropriate combinations of the members of "mpa".
6046 * Finally, we add the integer divisions through recursive calls.
6048 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6049 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6051 int i, n_param, n_in, n_div;
6052 isl_space *space;
6053 isl_val *v;
6054 isl_pw_aff *pa;
6055 isl_aff *tmp;
6057 n_param = isl_aff_dim(aff, isl_dim_param);
6058 n_in = isl_aff_dim(aff, isl_dim_in);
6059 n_div = isl_aff_dim(aff, isl_dim_div);
6061 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6062 tmp = isl_aff_copy(aff);
6063 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6064 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6065 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6066 isl_space_dim(space, isl_dim_set));
6067 tmp = isl_aff_reset_domain_space(tmp, space);
6068 pa = isl_pw_aff_from_aff(tmp);
6070 for (i = 0; i < n_in; ++i) {
6071 isl_pw_aff *pa_i;
6073 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6074 continue;
6075 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6076 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6077 pa_i = isl_pw_aff_scale_val(pa_i, v);
6078 pa = isl_pw_aff_add(pa, pa_i);
6081 for (i = 0; i < n_div; ++i) {
6082 isl_aff *div;
6083 isl_pw_aff *pa_i;
6085 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6086 continue;
6087 div = isl_aff_get_div(aff, i);
6088 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6089 isl_multi_pw_aff_copy(mpa), div);
6090 pa_i = isl_pw_aff_floor(pa_i);
6091 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6092 pa_i = isl_pw_aff_scale_val(pa_i, v);
6093 pa = isl_pw_aff_add(pa, pa_i);
6096 isl_multi_pw_aff_free(mpa);
6097 isl_aff_free(aff);
6099 return pa;
6102 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6103 * with the domain of "aff". The domain of the result is the same
6104 * as that of "mpa".
6106 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6107 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6109 if (!aff || !mpa)
6110 goto error;
6111 if (isl_space_match(aff->ls->dim, isl_dim_param,
6112 mpa->space, isl_dim_param))
6113 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6115 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6116 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6118 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6119 error:
6120 isl_aff_free(aff);
6121 isl_multi_pw_aff_free(mpa);
6122 return NULL;
6125 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6126 * with the domain of "pa". The domain of the result is the same
6127 * as that of "mpa".
6128 * "mpa" and "pa" are assumed to have been aligned.
6130 * We consider each piece in turn. Note that the domains of the
6131 * pieces are assumed to be disjoint and they remain disjoint
6132 * after taking the preimage (over the same function).
6134 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6135 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6137 isl_space *space;
6138 isl_pw_aff *res;
6139 int i;
6141 if (!mpa || !pa)
6142 goto error;
6144 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6145 isl_pw_aff_get_space(pa));
6146 res = isl_pw_aff_empty(space);
6148 for (i = 0; i < pa->n; ++i) {
6149 isl_pw_aff *pa_i;
6150 isl_set *domain;
6152 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6153 isl_multi_pw_aff_copy(mpa),
6154 isl_aff_copy(pa->p[i].aff));
6155 domain = isl_set_copy(pa->p[i].set);
6156 domain = isl_set_preimage_multi_pw_aff(domain,
6157 isl_multi_pw_aff_copy(mpa));
6158 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6159 res = isl_pw_aff_add_disjoint(res, pa_i);
6162 isl_pw_aff_free(pa);
6163 isl_multi_pw_aff_free(mpa);
6164 return res;
6165 error:
6166 isl_pw_aff_free(pa);
6167 isl_multi_pw_aff_free(mpa);
6168 return NULL;
6171 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6172 * with the domain of "pa". The domain of the result is the same
6173 * as that of "mpa".
6175 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6176 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6178 if (!pa || !mpa)
6179 goto error;
6180 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6181 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6183 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6184 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6186 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6187 error:
6188 isl_pw_aff_free(pa);
6189 isl_multi_pw_aff_free(mpa);
6190 return NULL;
6193 /* Compute the pullback of "pa" by the function represented by "mpa".
6194 * In other words, plug in "mpa" in "pa".
6195 * "pa" and "mpa" are assumed to have been aligned.
6197 * The pullback is computed by applying "pa" to "mpa".
6199 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6200 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6202 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6205 /* Compute the pullback of "pa" by the function represented by "mpa".
6206 * In other words, plug in "mpa" in "pa".
6208 * The pullback is computed by applying "pa" to "mpa".
6210 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6211 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6213 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6216 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6217 * In other words, plug in "mpa2" in "mpa1".
6219 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6221 * We pullback each member of "mpa1" in turn.
6223 static __isl_give isl_multi_pw_aff *
6224 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6225 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6227 int i;
6228 isl_space *space = NULL;
6230 mpa1 = isl_multi_pw_aff_cow(mpa1);
6231 if (!mpa1 || !mpa2)
6232 goto error;
6234 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6235 isl_multi_pw_aff_get_space(mpa1));
6237 for (i = 0; i < mpa1->n; ++i) {
6238 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6239 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6240 if (!mpa1->p[i])
6241 goto error;
6244 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6246 isl_multi_pw_aff_free(mpa2);
6247 return mpa1;
6248 error:
6249 isl_space_free(space);
6250 isl_multi_pw_aff_free(mpa1);
6251 isl_multi_pw_aff_free(mpa2);
6252 return NULL;
6255 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6256 * In other words, plug in "mpa2" in "mpa1".
6258 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6259 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6261 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6262 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);