extract out isl_multi_macro.h
[isl.git] / isl_aff.c
blob46d4ac469838110273ca877fd37a0a08e6befc9e
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 #undef BASE
41 #define BASE union_pw_aff
43 #include <isl_list_templ.c>
45 #undef BASE
46 #define BASE union_pw_multi_aff
48 #include <isl_list_templ.c>
50 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
51 __isl_take isl_vec *v)
53 isl_aff *aff;
55 if (!ls || !v)
56 goto error;
58 aff = isl_calloc_type(v->ctx, struct isl_aff);
59 if (!aff)
60 goto error;
62 aff->ref = 1;
63 aff->ls = ls;
64 aff->v = v;
66 return aff;
67 error:
68 isl_local_space_free(ls);
69 isl_vec_free(v);
70 return NULL;
73 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
75 isl_ctx *ctx;
76 isl_vec *v;
77 unsigned total;
79 if (!ls)
80 return NULL;
82 ctx = isl_local_space_get_ctx(ls);
83 if (!isl_local_space_divs_known(ls))
84 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
85 goto error);
86 if (!isl_local_space_is_set(ls))
87 isl_die(ctx, isl_error_invalid,
88 "domain of affine expression should be a set",
89 goto error);
91 total = isl_local_space_dim(ls, isl_dim_all);
92 v = isl_vec_alloc(ctx, 1 + 1 + total);
93 return isl_aff_alloc_vec(ls, v);
94 error:
95 isl_local_space_free(ls);
96 return NULL;
99 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
101 isl_aff *aff;
103 aff = isl_aff_alloc(ls);
104 if (!aff)
105 return NULL;
107 isl_int_set_si(aff->v->el[0], 1);
108 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
110 return aff;
113 /* Return a piecewise affine expression defined on the specified domain
114 * that is equal to zero.
116 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
118 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
121 /* Return an affine expression defined on the specified domain
122 * that represents NaN.
124 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
126 isl_aff *aff;
128 aff = isl_aff_alloc(ls);
129 if (!aff)
130 return NULL;
132 isl_seq_clr(aff->v->el, aff->v->size);
134 return aff;
137 /* Return a piecewise affine expression defined on the specified domain
138 * that represents NaN.
140 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
142 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
145 /* Return an affine expression that is equal to "val" on
146 * domain local space "ls".
148 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
149 __isl_take isl_val *val)
151 isl_aff *aff;
153 if (!ls || !val)
154 goto error;
155 if (!isl_val_is_rat(val))
156 isl_die(isl_val_get_ctx(val), isl_error_invalid,
157 "expecting rational value", goto error);
159 aff = isl_aff_alloc(isl_local_space_copy(ls));
160 if (!aff)
161 goto error;
163 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
164 isl_int_set(aff->v->el[1], val->n);
165 isl_int_set(aff->v->el[0], val->d);
167 isl_local_space_free(ls);
168 isl_val_free(val);
169 return aff;
170 error:
171 isl_local_space_free(ls);
172 isl_val_free(val);
173 return NULL;
176 /* Return an affine expression that is equal to the specified dimension
177 * in "ls".
179 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
180 enum isl_dim_type type, unsigned pos)
182 isl_space *space;
183 isl_aff *aff;
185 if (!ls)
186 return NULL;
188 space = isl_local_space_get_space(ls);
189 if (!space)
190 goto error;
191 if (isl_space_is_map(space))
192 isl_die(isl_space_get_ctx(space), isl_error_invalid,
193 "expecting (parameter) set space", goto error);
194 if (pos >= isl_local_space_dim(ls, type))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "position out of bounds", goto error);
198 isl_space_free(space);
199 aff = isl_aff_alloc(ls);
200 if (!aff)
201 return NULL;
203 pos += isl_local_space_offset(aff->ls, type);
205 isl_int_set_si(aff->v->el[0], 1);
206 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
207 isl_int_set_si(aff->v->el[1 + pos], 1);
209 return aff;
210 error:
211 isl_local_space_free(ls);
212 isl_space_free(space);
213 return NULL;
216 /* Return a piecewise affine expression that is equal to
217 * the specified dimension in "ls".
219 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
220 enum isl_dim_type type, unsigned pos)
222 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
225 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
227 if (!aff)
228 return NULL;
230 aff->ref++;
231 return aff;
234 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
236 if (!aff)
237 return NULL;
239 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
240 isl_vec_copy(aff->v));
243 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
245 if (!aff)
246 return NULL;
248 if (aff->ref == 1)
249 return aff;
250 aff->ref--;
251 return isl_aff_dup(aff);
254 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
256 if (!aff)
257 return NULL;
259 if (--aff->ref > 0)
260 return NULL;
262 isl_local_space_free(aff->ls);
263 isl_vec_free(aff->v);
265 free(aff);
267 return NULL;
270 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
272 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
275 /* Externally, an isl_aff has a map space, but internally, the
276 * ls field corresponds to the domain of that space.
278 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
280 if (!aff)
281 return 0;
282 if (type == isl_dim_out)
283 return 1;
284 if (type == isl_dim_in)
285 type = isl_dim_set;
286 return isl_local_space_dim(aff->ls, type);
289 /* Return the position of the dimension of the given type and name
290 * in "aff".
291 * Return -1 if no such dimension can be found.
293 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
294 const char *name)
296 if (!aff)
297 return -1;
298 if (type == isl_dim_out)
299 return -1;
300 if (type == isl_dim_in)
301 type = isl_dim_set;
302 return isl_local_space_find_dim_by_name(aff->ls, type, name);
305 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
307 return aff ? isl_local_space_get_space(aff->ls) : NULL;
310 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
312 isl_space *space;
313 if (!aff)
314 return NULL;
315 space = isl_local_space_get_space(aff->ls);
316 space = isl_space_from_domain(space);
317 space = isl_space_add_dims(space, isl_dim_out, 1);
318 return space;
321 __isl_give isl_local_space *isl_aff_get_domain_local_space(
322 __isl_keep isl_aff *aff)
324 return aff ? isl_local_space_copy(aff->ls) : NULL;
327 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
329 isl_local_space *ls;
330 if (!aff)
331 return NULL;
332 ls = isl_local_space_copy(aff->ls);
333 ls = isl_local_space_from_domain(ls);
334 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
335 return ls;
338 /* Externally, an isl_aff has a map space, but internally, the
339 * ls field corresponds to the domain of that space.
341 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
342 enum isl_dim_type type, unsigned pos)
344 if (!aff)
345 return NULL;
346 if (type == isl_dim_out)
347 return NULL;
348 if (type == isl_dim_in)
349 type = isl_dim_set;
350 return isl_local_space_get_dim_name(aff->ls, type, pos);
353 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
354 __isl_take isl_space *dim)
356 aff = isl_aff_cow(aff);
357 if (!aff || !dim)
358 goto error;
360 aff->ls = isl_local_space_reset_space(aff->ls, dim);
361 if (!aff->ls)
362 return isl_aff_free(aff);
364 return aff;
365 error:
366 isl_aff_free(aff);
367 isl_space_free(dim);
368 return NULL;
371 /* Reset the space of "aff". This function is called from isl_pw_templ.c
372 * and doesn't know if the space of an element object is represented
373 * directly or through its domain. It therefore passes along both.
375 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
376 __isl_take isl_space *space, __isl_take isl_space *domain)
378 isl_space_free(space);
379 return isl_aff_reset_domain_space(aff, domain);
382 /* Reorder the coefficients of the affine expression based
383 * on the given reodering.
384 * The reordering r is assumed to have been extended with the local
385 * variables.
387 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
388 __isl_take isl_reordering *r, int n_div)
390 isl_vec *res;
391 int i;
393 if (!vec || !r)
394 goto error;
396 res = isl_vec_alloc(vec->ctx,
397 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
398 isl_seq_cpy(res->el, vec->el, 2);
399 isl_seq_clr(res->el + 2, res->size - 2);
400 for (i = 0; i < r->len; ++i)
401 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
403 isl_reordering_free(r);
404 isl_vec_free(vec);
405 return res;
406 error:
407 isl_vec_free(vec);
408 isl_reordering_free(r);
409 return NULL;
412 /* Reorder the dimensions of the domain of "aff" according
413 * to the given reordering.
415 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
416 __isl_take isl_reordering *r)
418 aff = isl_aff_cow(aff);
419 if (!aff)
420 goto error;
422 r = isl_reordering_extend(r, aff->ls->div->n_row);
423 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
424 aff->ls->div->n_row);
425 aff->ls = isl_local_space_realign(aff->ls, r);
427 if (!aff->v || !aff->ls)
428 return isl_aff_free(aff);
430 return aff;
431 error:
432 isl_aff_free(aff);
433 isl_reordering_free(r);
434 return NULL;
437 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
438 __isl_take isl_space *model)
440 if (!aff || !model)
441 goto error;
443 if (!isl_space_match(aff->ls->dim, isl_dim_param,
444 model, isl_dim_param)) {
445 isl_reordering *exp;
447 model = isl_space_drop_dims(model, isl_dim_in,
448 0, isl_space_dim(model, isl_dim_in));
449 model = isl_space_drop_dims(model, isl_dim_out,
450 0, isl_space_dim(model, isl_dim_out));
451 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
452 exp = isl_reordering_extend_space(exp,
453 isl_aff_get_domain_space(aff));
454 aff = isl_aff_realign_domain(aff, exp);
457 isl_space_free(model);
458 return aff;
459 error:
460 isl_space_free(model);
461 isl_aff_free(aff);
462 return NULL;
465 /* Is "aff" obviously equal to zero?
467 * If the denominator is zero, then "aff" is not equal to zero.
469 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
471 if (!aff)
472 return -1;
474 if (isl_int_is_zero(aff->v->el[0]))
475 return 0;
476 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
479 /* Does "aff" represent NaN?
481 int isl_aff_is_nan(__isl_keep isl_aff *aff)
483 if (!aff)
484 return -1;
486 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
489 /* Does "pa" involve any NaNs?
491 int isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
493 int i;
495 if (!pa)
496 return -1;
497 if (pa->n == 0)
498 return 0;
500 for (i = 0; i < pa->n; ++i) {
501 int is_nan = isl_aff_is_nan(pa->p[i].aff);
502 if (is_nan < 0 || is_nan)
503 return is_nan;
506 return 0;
509 /* Are "aff1" and "aff2" obviously equal?
511 * NaN is not equal to anything, not even to another NaN.
513 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
515 int equal;
517 if (!aff1 || !aff2)
518 return -1;
520 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
521 return 0;
523 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
524 if (equal < 0 || !equal)
525 return equal;
527 return isl_vec_is_equal(aff1->v, aff2->v);
530 /* Return the common denominator of "aff" in "v".
532 * We cannot return anything meaningful in case of a NaN.
534 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
536 if (!aff)
537 return -1;
538 if (isl_aff_is_nan(aff))
539 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
540 "cannot get denominator of NaN", return -1);
541 isl_int_set(*v, aff->v->el[0]);
542 return 0;
545 /* Return the common denominator of "aff".
547 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
549 isl_ctx *ctx;
551 if (!aff)
552 return NULL;
554 ctx = isl_aff_get_ctx(aff);
555 if (isl_aff_is_nan(aff))
556 return isl_val_nan(ctx);
557 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
560 /* Return the constant term of "aff" in "v".
562 * We cannot return anything meaningful in case of a NaN.
564 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
566 if (!aff)
567 return -1;
568 if (isl_aff_is_nan(aff))
569 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
570 "cannot get constant term of NaN", return -1);
571 isl_int_set(*v, aff->v->el[1]);
572 return 0;
575 /* Return the constant term of "aff".
577 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
579 isl_ctx *ctx;
580 isl_val *v;
582 if (!aff)
583 return NULL;
585 ctx = isl_aff_get_ctx(aff);
586 if (isl_aff_is_nan(aff))
587 return isl_val_nan(ctx);
588 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
589 return isl_val_normalize(v);
592 /* Return the coefficient of the variable of type "type" at position "pos"
593 * of "aff" in "v".
595 * We cannot return anything meaningful in case of a NaN.
597 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
598 enum isl_dim_type type, int pos, isl_int *v)
600 if (!aff)
601 return -1;
603 if (type == isl_dim_out)
604 isl_die(aff->v->ctx, isl_error_invalid,
605 "output/set dimension does not have a coefficient",
606 return -1);
607 if (type == isl_dim_in)
608 type = isl_dim_set;
610 if (pos >= isl_local_space_dim(aff->ls, type))
611 isl_die(aff->v->ctx, isl_error_invalid,
612 "position out of bounds", return -1);
614 if (isl_aff_is_nan(aff))
615 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
616 "cannot get coefficient of NaN", return -1);
617 pos += isl_local_space_offset(aff->ls, type);
618 isl_int_set(*v, aff->v->el[1 + pos]);
620 return 0;
623 /* Return the coefficient of the variable of type "type" at position "pos"
624 * of "aff".
626 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
627 enum isl_dim_type type, int pos)
629 isl_ctx *ctx;
630 isl_val *v;
632 if (!aff)
633 return NULL;
635 ctx = isl_aff_get_ctx(aff);
636 if (type == isl_dim_out)
637 isl_die(ctx, isl_error_invalid,
638 "output/set dimension does not have a coefficient",
639 return NULL);
640 if (type == isl_dim_in)
641 type = isl_dim_set;
643 if (pos >= isl_local_space_dim(aff->ls, type))
644 isl_die(ctx, isl_error_invalid,
645 "position out of bounds", return NULL);
647 if (isl_aff_is_nan(aff))
648 return isl_val_nan(ctx);
649 pos += isl_local_space_offset(aff->ls, type);
650 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
651 return isl_val_normalize(v);
654 /* Return the sign of the coefficient of the variable of type "type"
655 * at position "pos" of "aff".
657 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
658 int pos)
660 isl_ctx *ctx;
662 if (!aff)
663 return 0;
665 ctx = isl_aff_get_ctx(aff);
666 if (type == isl_dim_out)
667 isl_die(ctx, isl_error_invalid,
668 "output/set dimension does not have a coefficient",
669 return 0);
670 if (type == isl_dim_in)
671 type = isl_dim_set;
673 if (pos >= isl_local_space_dim(aff->ls, type))
674 isl_die(ctx, isl_error_invalid,
675 "position out of bounds", return 0);
677 pos += isl_local_space_offset(aff->ls, type);
678 return isl_int_sgn(aff->v->el[1 + pos]);
681 /* Replace the denominator of "aff" by "v".
683 * A NaN is unaffected by this operation.
685 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
687 if (!aff)
688 return NULL;
689 if (isl_aff_is_nan(aff))
690 return aff;
691 aff = isl_aff_cow(aff);
692 if (!aff)
693 return NULL;
695 aff->v = isl_vec_cow(aff->v);
696 if (!aff->v)
697 return isl_aff_free(aff);
699 isl_int_set(aff->v->el[0], v);
701 return aff;
704 /* Replace the numerator of the constant term of "aff" by "v".
706 * A NaN is unaffected by this operation.
708 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
710 if (!aff)
711 return NULL;
712 if (isl_aff_is_nan(aff))
713 return aff;
714 aff = isl_aff_cow(aff);
715 if (!aff)
716 return NULL;
718 aff->v = isl_vec_cow(aff->v);
719 if (!aff->v)
720 return isl_aff_free(aff);
722 isl_int_set(aff->v->el[1], v);
724 return aff;
727 /* Replace the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
732 __isl_take isl_val *v)
734 if (!aff || !v)
735 goto error;
737 if (isl_aff_is_nan(aff)) {
738 isl_val_free(v);
739 return aff;
742 if (!isl_val_is_rat(v))
743 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
744 "expecting rational value", goto error);
746 if (isl_int_eq(aff->v->el[1], v->n) &&
747 isl_int_eq(aff->v->el[0], v->d)) {
748 isl_val_free(v);
749 return aff;
752 aff = isl_aff_cow(aff);
753 if (!aff)
754 goto error;
755 aff->v = isl_vec_cow(aff->v);
756 if (!aff->v)
757 goto error;
759 if (isl_int_eq(aff->v->el[0], v->d)) {
760 isl_int_set(aff->v->el[1], v->n);
761 } else if (isl_int_is_one(v->d)) {
762 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
763 } else {
764 isl_seq_scale(aff->v->el + 1,
765 aff->v->el + 1, v->d, aff->v->size - 1);
766 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
767 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
768 aff->v = isl_vec_normalize(aff->v);
769 if (!aff->v)
770 goto error;
773 isl_val_free(v);
774 return aff;
775 error:
776 isl_aff_free(aff);
777 isl_val_free(v);
778 return NULL;
781 /* Add "v" to the constant term of "aff".
783 * A NaN is unaffected by this operation.
785 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
787 if (isl_int_is_zero(v))
788 return aff;
790 if (!aff)
791 return NULL;
792 if (isl_aff_is_nan(aff))
793 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_addmul(aff->v->el[1], aff->v->el[0], v);
804 return aff;
807 /* Add "v" to the constant term of "aff".
809 * A NaN is unaffected by this operation.
811 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
812 __isl_take isl_val *v)
814 if (!aff || !v)
815 goto error;
817 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
818 isl_val_free(v);
819 return aff;
822 if (!isl_val_is_rat(v))
823 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
824 "expecting rational value", goto error);
826 aff = isl_aff_cow(aff);
827 if (!aff)
828 goto error;
830 aff->v = isl_vec_cow(aff->v);
831 if (!aff->v)
832 goto error;
834 if (isl_int_is_one(v->d)) {
835 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
836 } else if (isl_int_eq(aff->v->el[0], v->d)) {
837 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
838 aff->v = isl_vec_normalize(aff->v);
839 if (!aff->v)
840 goto error;
841 } else {
842 isl_seq_scale(aff->v->el + 1,
843 aff->v->el + 1, v->d, aff->v->size - 1);
844 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
845 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
846 aff->v = isl_vec_normalize(aff->v);
847 if (!aff->v)
848 goto error;
851 isl_val_free(v);
852 return aff;
853 error:
854 isl_aff_free(aff);
855 isl_val_free(v);
856 return NULL;
859 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
861 isl_int t;
863 isl_int_init(t);
864 isl_int_set_si(t, v);
865 aff = isl_aff_add_constant(aff, t);
866 isl_int_clear(t);
868 return aff;
871 /* Add "v" to the numerator of the constant term of "aff".
873 * A NaN is unaffected by this operation.
875 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
877 if (isl_int_is_zero(v))
878 return aff;
880 if (!aff)
881 return NULL;
882 if (isl_aff_is_nan(aff))
883 return 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 isl_int_add(aff->v->el[1], aff->v->el[1], v);
894 return aff;
897 /* Add "v" to the numerator of the constant term of "aff".
899 * A NaN is unaffected by this operation.
901 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
903 isl_int t;
905 if (v == 0)
906 return aff;
908 isl_int_init(t);
909 isl_int_set_si(t, v);
910 aff = isl_aff_add_constant_num(aff, t);
911 isl_int_clear(t);
913 return aff;
916 /* Replace the numerator of the constant term of "aff" by "v".
918 * A NaN is unaffected by this operation.
920 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
922 if (!aff)
923 return NULL;
924 if (isl_aff_is_nan(aff))
925 return aff;
926 aff = isl_aff_cow(aff);
927 if (!aff)
928 return NULL;
930 aff->v = isl_vec_cow(aff->v);
931 if (!aff->v)
932 return isl_aff_free(aff);
934 isl_int_set_si(aff->v->el[1], v);
936 return aff;
939 /* Replace the numerator of the coefficient of the variable of type "type"
940 * at position "pos" of "aff" by "v".
942 * A NaN is unaffected by this operation.
944 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
945 enum isl_dim_type type, int pos, isl_int v)
947 if (!aff)
948 return NULL;
950 if (type == isl_dim_out)
951 isl_die(aff->v->ctx, isl_error_invalid,
952 "output/set dimension does not have a coefficient",
953 return isl_aff_free(aff));
954 if (type == isl_dim_in)
955 type = isl_dim_set;
957 if (pos >= isl_local_space_dim(aff->ls, type))
958 isl_die(aff->v->ctx, isl_error_invalid,
959 "position out of bounds", return isl_aff_free(aff));
961 if (isl_aff_is_nan(aff))
962 return aff;
963 aff = isl_aff_cow(aff);
964 if (!aff)
965 return NULL;
967 aff->v = isl_vec_cow(aff->v);
968 if (!aff->v)
969 return isl_aff_free(aff);
971 pos += isl_local_space_offset(aff->ls, type);
972 isl_int_set(aff->v->el[1 + pos], v);
974 return aff;
977 /* Replace the numerator of the coefficient of the variable of type "type"
978 * at position "pos" of "aff" by "v".
980 * A NaN is unaffected by this operation.
982 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
983 enum isl_dim_type type, int pos, int v)
985 if (!aff)
986 return NULL;
988 if (type == isl_dim_out)
989 isl_die(aff->v->ctx, isl_error_invalid,
990 "output/set dimension does not have a coefficient",
991 return isl_aff_free(aff));
992 if (type == isl_dim_in)
993 type = isl_dim_set;
995 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
996 isl_die(aff->v->ctx, isl_error_invalid,
997 "position out of bounds", return isl_aff_free(aff));
999 if (isl_aff_is_nan(aff))
1000 return aff;
1001 pos += isl_local_space_offset(aff->ls, type);
1002 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1003 return aff;
1005 aff = isl_aff_cow(aff);
1006 if (!aff)
1007 return NULL;
1009 aff->v = isl_vec_cow(aff->v);
1010 if (!aff->v)
1011 return isl_aff_free(aff);
1013 isl_int_set_si(aff->v->el[1 + pos], v);
1015 return aff;
1018 /* Replace the coefficient of the variable of type "type" at position "pos"
1019 * of "aff" by "v".
1021 * A NaN is unaffected by this operation.
1023 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1024 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1026 if (!aff || !v)
1027 goto error;
1029 if (type == isl_dim_out)
1030 isl_die(aff->v->ctx, isl_error_invalid,
1031 "output/set dimension does not have a coefficient",
1032 goto error);
1033 if (type == isl_dim_in)
1034 type = isl_dim_set;
1036 if (pos >= isl_local_space_dim(aff->ls, type))
1037 isl_die(aff->v->ctx, isl_error_invalid,
1038 "position out of bounds", goto error);
1040 if (isl_aff_is_nan(aff)) {
1041 isl_val_free(v);
1042 return aff;
1044 if (!isl_val_is_rat(v))
1045 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1046 "expecting rational value", goto error);
1048 pos += isl_local_space_offset(aff->ls, type);
1049 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1050 isl_int_eq(aff->v->el[0], v->d)) {
1051 isl_val_free(v);
1052 return aff;
1055 aff = isl_aff_cow(aff);
1056 if (!aff)
1057 goto error;
1058 aff->v = isl_vec_cow(aff->v);
1059 if (!aff->v)
1060 goto error;
1062 if (isl_int_eq(aff->v->el[0], v->d)) {
1063 isl_int_set(aff->v->el[1 + pos], v->n);
1064 } else if (isl_int_is_one(v->d)) {
1065 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1066 } else {
1067 isl_seq_scale(aff->v->el + 1,
1068 aff->v->el + 1, v->d, aff->v->size - 1);
1069 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1070 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1071 aff->v = isl_vec_normalize(aff->v);
1072 if (!aff->v)
1073 goto error;
1076 isl_val_free(v);
1077 return aff;
1078 error:
1079 isl_aff_free(aff);
1080 isl_val_free(v);
1081 return NULL;
1084 /* Add "v" to the coefficient of the variable of type "type"
1085 * at position "pos" of "aff".
1087 * A NaN is unaffected by this operation.
1089 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1090 enum isl_dim_type type, int pos, isl_int v)
1092 if (!aff)
1093 return NULL;
1095 if (type == isl_dim_out)
1096 isl_die(aff->v->ctx, isl_error_invalid,
1097 "output/set dimension does not have a coefficient",
1098 return isl_aff_free(aff));
1099 if (type == isl_dim_in)
1100 type = isl_dim_set;
1102 if (pos >= isl_local_space_dim(aff->ls, type))
1103 isl_die(aff->v->ctx, isl_error_invalid,
1104 "position out of bounds", return isl_aff_free(aff));
1106 if (isl_aff_is_nan(aff))
1107 return aff;
1108 aff = isl_aff_cow(aff);
1109 if (!aff)
1110 return NULL;
1112 aff->v = isl_vec_cow(aff->v);
1113 if (!aff->v)
1114 return isl_aff_free(aff);
1116 pos += isl_local_space_offset(aff->ls, type);
1117 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1119 return aff;
1122 /* Add "v" to the coefficient of the variable of type "type"
1123 * at position "pos" of "aff".
1125 * A NaN is unaffected by this operation.
1127 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1128 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1130 if (!aff || !v)
1131 goto error;
1133 if (isl_val_is_zero(v)) {
1134 isl_val_free(v);
1135 return aff;
1138 if (type == isl_dim_out)
1139 isl_die(aff->v->ctx, isl_error_invalid,
1140 "output/set dimension does not have a coefficient",
1141 goto error);
1142 if (type == isl_dim_in)
1143 type = isl_dim_set;
1145 if (pos >= isl_local_space_dim(aff->ls, type))
1146 isl_die(aff->v->ctx, isl_error_invalid,
1147 "position out of bounds", goto error);
1149 if (isl_aff_is_nan(aff)) {
1150 isl_val_free(v);
1151 return aff;
1153 if (!isl_val_is_rat(v))
1154 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1155 "expecting rational value", goto error);
1157 aff = isl_aff_cow(aff);
1158 if (!aff)
1159 goto error;
1161 aff->v = isl_vec_cow(aff->v);
1162 if (!aff->v)
1163 goto error;
1165 pos += isl_local_space_offset(aff->ls, type);
1166 if (isl_int_is_one(v->d)) {
1167 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1168 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1169 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1170 aff->v = isl_vec_normalize(aff->v);
1171 if (!aff->v)
1172 goto error;
1173 } else {
1174 isl_seq_scale(aff->v->el + 1,
1175 aff->v->el + 1, v->d, aff->v->size - 1);
1176 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1177 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1178 aff->v = isl_vec_normalize(aff->v);
1179 if (!aff->v)
1180 goto error;
1183 isl_val_free(v);
1184 return aff;
1185 error:
1186 isl_aff_free(aff);
1187 isl_val_free(v);
1188 return NULL;
1191 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1192 enum isl_dim_type type, int pos, int v)
1194 isl_int t;
1196 isl_int_init(t);
1197 isl_int_set_si(t, v);
1198 aff = isl_aff_add_coefficient(aff, type, pos, t);
1199 isl_int_clear(t);
1201 return aff;
1204 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1206 if (!aff)
1207 return NULL;
1209 return isl_local_space_get_div(aff->ls, pos);
1212 /* Return the negation of "aff".
1214 * As a special case, -NaN = NaN.
1216 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1218 if (!aff)
1219 return NULL;
1220 if (isl_aff_is_nan(aff))
1221 return aff;
1222 aff = isl_aff_cow(aff);
1223 if (!aff)
1224 return NULL;
1225 aff->v = isl_vec_cow(aff->v);
1226 if (!aff->v)
1227 return isl_aff_free(aff);
1229 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1231 return aff;
1234 /* Remove divs from the local space that do not appear in the affine
1235 * expression.
1236 * We currently only remove divs at the end.
1237 * Some intermediate divs may also not appear directly in the affine
1238 * expression, but we would also need to check that no other divs are
1239 * defined in terms of them.
1241 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1243 int pos;
1244 int off;
1245 int n;
1247 if (!aff)
1248 return NULL;
1250 n = isl_local_space_dim(aff->ls, isl_dim_div);
1251 off = isl_local_space_offset(aff->ls, isl_dim_div);
1253 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1254 if (pos == n)
1255 return aff;
1257 aff = isl_aff_cow(aff);
1258 if (!aff)
1259 return NULL;
1261 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1262 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1263 if (!aff->ls || !aff->v)
1264 return isl_aff_free(aff);
1266 return aff;
1269 /* Given two affine expressions "p" of length p_len (including the
1270 * denominator and the constant term) and "subs" of length subs_len,
1271 * plug in "subs" for the variable at position "pos".
1272 * The variables of "subs" and "p" are assumed to match up to subs_len,
1273 * but "p" may have additional variables.
1274 * "v" is an initialized isl_int that can be used internally.
1276 * In particular, if "p" represents the expression
1278 * (a i + g)/m
1280 * with i the variable at position "pos" and "subs" represents the expression
1282 * f/d
1284 * then the result represents the expression
1286 * (a f + d g)/(m d)
1289 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1290 int p_len, int subs_len, isl_int v)
1292 isl_int_set(v, p[1 + pos]);
1293 isl_int_set_si(p[1 + pos], 0);
1294 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1295 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1296 isl_int_mul(p[0], p[0], subs[0]);
1299 /* Look for any divs in the aff->ls with a denominator equal to one
1300 * and plug them into the affine expression and any subsequent divs
1301 * that may reference the div.
1303 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1305 int i, n;
1306 int len;
1307 isl_int v;
1308 isl_vec *vec;
1309 isl_local_space *ls;
1310 unsigned pos;
1312 if (!aff)
1313 return NULL;
1315 n = isl_local_space_dim(aff->ls, isl_dim_div);
1316 len = aff->v->size;
1317 for (i = 0; i < n; ++i) {
1318 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1319 continue;
1320 ls = isl_local_space_copy(aff->ls);
1321 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1322 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1323 vec = isl_vec_copy(aff->v);
1324 vec = isl_vec_cow(vec);
1325 if (!ls || !vec)
1326 goto error;
1328 isl_int_init(v);
1330 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1331 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1332 len, len, v);
1334 isl_int_clear(v);
1336 isl_vec_free(aff->v);
1337 aff->v = vec;
1338 isl_local_space_free(aff->ls);
1339 aff->ls = ls;
1342 return aff;
1343 error:
1344 isl_vec_free(vec);
1345 isl_local_space_free(ls);
1346 return isl_aff_free(aff);
1349 /* Look for any divs j that appear with a unit coefficient inside
1350 * the definitions of other divs i and plug them into the definitions
1351 * of the divs i.
1353 * In particular, an expression of the form
1355 * floor((f(..) + floor(g(..)/n))/m)
1357 * is simplified to
1359 * floor((n * f(..) + g(..))/(n * m))
1361 * This simplification is correct because we can move the expression
1362 * f(..) into the inner floor in the original expression to obtain
1364 * floor(floor((n * f(..) + g(..))/n)/m)
1366 * from which we can derive the simplified expression.
1368 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1370 int i, j, n;
1371 int off;
1373 if (!aff)
1374 return NULL;
1376 n = isl_local_space_dim(aff->ls, isl_dim_div);
1377 off = isl_local_space_offset(aff->ls, isl_dim_div);
1378 for (i = 1; i < n; ++i) {
1379 for (j = 0; j < i; ++j) {
1380 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1381 continue;
1382 aff->ls = isl_local_space_substitute_seq(aff->ls,
1383 isl_dim_div, j, aff->ls->div->row[j],
1384 aff->v->size, i, 1);
1385 if (!aff->ls)
1386 return isl_aff_free(aff);
1390 return aff;
1393 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1395 * Even though this function is only called on isl_affs with a single
1396 * reference, we are careful to only change aff->v and aff->ls together.
1398 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1400 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1401 isl_local_space *ls;
1402 isl_vec *v;
1404 ls = isl_local_space_copy(aff->ls);
1405 ls = isl_local_space_swap_div(ls, a, b);
1406 v = isl_vec_copy(aff->v);
1407 v = isl_vec_cow(v);
1408 if (!ls || !v)
1409 goto error;
1411 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1412 isl_vec_free(aff->v);
1413 aff->v = v;
1414 isl_local_space_free(aff->ls);
1415 aff->ls = ls;
1417 return aff;
1418 error:
1419 isl_vec_free(v);
1420 isl_local_space_free(ls);
1421 return isl_aff_free(aff);
1424 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1426 * We currently do not actually remove div "b", but simply add its
1427 * coefficient to that of "a" and then zero it out.
1429 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1431 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1433 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1434 return aff;
1436 aff->v = isl_vec_cow(aff->v);
1437 if (!aff->v)
1438 return isl_aff_free(aff);
1440 isl_int_add(aff->v->el[1 + off + a],
1441 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1442 isl_int_set_si(aff->v->el[1 + off + b], 0);
1444 return aff;
1447 /* Sort the divs in the local space of "aff" according to
1448 * the comparison function "cmp_row" in isl_local_space.c,
1449 * combining the coefficients of identical divs.
1451 * Reordering divs does not change the semantics of "aff",
1452 * so there is no need to call isl_aff_cow.
1453 * Moreover, this function is currently only called on isl_affs
1454 * with a single reference.
1456 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1458 int i, j, n;
1459 unsigned off;
1461 if (!aff)
1462 return NULL;
1464 off = isl_local_space_offset(aff->ls, isl_dim_div);
1465 n = isl_aff_dim(aff, isl_dim_div);
1466 for (i = 1; i < n; ++i) {
1467 for (j = i - 1; j >= 0; --j) {
1468 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1469 if (cmp < 0)
1470 break;
1471 if (cmp == 0)
1472 aff = merge_divs(aff, j, j + 1);
1473 else
1474 aff = swap_div(aff, j, j + 1);
1475 if (!aff)
1476 return NULL;
1480 return aff;
1483 /* Normalize the representation of "aff".
1485 * This function should only be called of "new" isl_affs, i.e.,
1486 * with only a single reference. We therefore do not need to
1487 * worry about affecting other instances.
1489 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1491 if (!aff)
1492 return NULL;
1493 aff->v = isl_vec_normalize(aff->v);
1494 if (!aff->v)
1495 return isl_aff_free(aff);
1496 aff = plug_in_integral_divs(aff);
1497 aff = plug_in_unit_divs(aff);
1498 aff = sort_divs(aff);
1499 aff = isl_aff_remove_unused_divs(aff);
1500 return aff;
1503 /* Given f, return floor(f).
1504 * If f is an integer expression, then just return f.
1505 * If f is a constant, then return the constant floor(f).
1506 * Otherwise, if f = g/m, write g = q m + r,
1507 * create a new div d = [r/m] and return the expression q + d.
1508 * The coefficients in r are taken to lie between -m/2 and m/2.
1510 * As a special case, floor(NaN) = NaN.
1512 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1514 int i;
1515 int size;
1516 isl_ctx *ctx;
1517 isl_vec *div;
1519 if (!aff)
1520 return NULL;
1522 if (isl_aff_is_nan(aff))
1523 return aff;
1524 if (isl_int_is_one(aff->v->el[0]))
1525 return aff;
1527 aff = isl_aff_cow(aff);
1528 if (!aff)
1529 return NULL;
1531 aff->v = isl_vec_cow(aff->v);
1532 if (!aff->v)
1533 return isl_aff_free(aff);
1535 if (isl_aff_is_cst(aff)) {
1536 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1537 isl_int_set_si(aff->v->el[0], 1);
1538 return aff;
1541 div = isl_vec_copy(aff->v);
1542 div = isl_vec_cow(div);
1543 if (!div)
1544 return isl_aff_free(aff);
1546 ctx = isl_aff_get_ctx(aff);
1547 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1548 for (i = 1; i < aff->v->size; ++i) {
1549 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1550 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1551 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1552 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1553 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1557 aff->ls = isl_local_space_add_div(aff->ls, div);
1558 if (!aff->ls)
1559 return isl_aff_free(aff);
1561 size = aff->v->size;
1562 aff->v = isl_vec_extend(aff->v, size + 1);
1563 if (!aff->v)
1564 return isl_aff_free(aff);
1565 isl_int_set_si(aff->v->el[0], 1);
1566 isl_int_set_si(aff->v->el[size], 1);
1568 aff = isl_aff_normalize(aff);
1570 return aff;
1573 /* Compute
1575 * aff mod m = aff - m * floor(aff/m)
1577 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1579 isl_aff *res;
1581 res = isl_aff_copy(aff);
1582 aff = isl_aff_scale_down(aff, m);
1583 aff = isl_aff_floor(aff);
1584 aff = isl_aff_scale(aff, m);
1585 res = isl_aff_sub(res, aff);
1587 return res;
1590 /* Compute
1592 * aff mod m = aff - m * floor(aff/m)
1594 * with m an integer value.
1596 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1597 __isl_take isl_val *m)
1599 isl_aff *res;
1601 if (!aff || !m)
1602 goto error;
1604 if (!isl_val_is_int(m))
1605 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1606 "expecting integer modulo", goto error);
1608 res = isl_aff_copy(aff);
1609 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1610 aff = isl_aff_floor(aff);
1611 aff = isl_aff_scale_val(aff, m);
1612 res = isl_aff_sub(res, aff);
1614 return res;
1615 error:
1616 isl_aff_free(aff);
1617 isl_val_free(m);
1618 return NULL;
1621 /* Compute
1623 * pwaff mod m = pwaff - m * floor(pwaff/m)
1625 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1627 isl_pw_aff *res;
1629 res = isl_pw_aff_copy(pwaff);
1630 pwaff = isl_pw_aff_scale_down(pwaff, m);
1631 pwaff = isl_pw_aff_floor(pwaff);
1632 pwaff = isl_pw_aff_scale(pwaff, m);
1633 res = isl_pw_aff_sub(res, pwaff);
1635 return res;
1638 /* Compute
1640 * pa mod m = pa - m * floor(pa/m)
1642 * with m an integer value.
1644 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1645 __isl_take isl_val *m)
1647 if (!pa || !m)
1648 goto error;
1649 if (!isl_val_is_int(m))
1650 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1651 "expecting integer modulo", goto error);
1652 pa = isl_pw_aff_mod(pa, m->n);
1653 isl_val_free(m);
1654 return pa;
1655 error:
1656 isl_pw_aff_free(pa);
1657 isl_val_free(m);
1658 return NULL;
1661 /* Given f, return ceil(f).
1662 * If f is an integer expression, then just return f.
1663 * Otherwise, let f be the expression
1665 * e/m
1667 * then return
1669 * floor((e + m - 1)/m)
1671 * As a special case, ceil(NaN) = NaN.
1673 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1675 if (!aff)
1676 return NULL;
1678 if (isl_aff_is_nan(aff))
1679 return aff;
1680 if (isl_int_is_one(aff->v->el[0]))
1681 return aff;
1683 aff = isl_aff_cow(aff);
1684 if (!aff)
1685 return NULL;
1686 aff->v = isl_vec_cow(aff->v);
1687 if (!aff->v)
1688 return isl_aff_free(aff);
1690 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1691 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1692 aff = isl_aff_floor(aff);
1694 return aff;
1697 /* Apply the expansion computed by isl_merge_divs.
1698 * The expansion itself is given by "exp" while the resulting
1699 * list of divs is given by "div".
1701 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1702 __isl_take isl_mat *div, int *exp)
1704 int i, j;
1705 int old_n_div;
1706 int new_n_div;
1707 int offset;
1709 aff = isl_aff_cow(aff);
1710 if (!aff || !div)
1711 goto error;
1713 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1714 new_n_div = isl_mat_rows(div);
1715 if (new_n_div < old_n_div)
1716 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1717 "not an expansion", goto error);
1719 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1720 if (!aff->v)
1721 goto error;
1723 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1724 j = old_n_div - 1;
1725 for (i = new_n_div - 1; i >= 0; --i) {
1726 if (j >= 0 && exp[j] == i) {
1727 if (i != j)
1728 isl_int_swap(aff->v->el[offset + i],
1729 aff->v->el[offset + j]);
1730 j--;
1731 } else
1732 isl_int_set_si(aff->v->el[offset + i], 0);
1735 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1736 if (!aff->ls)
1737 goto error;
1738 isl_mat_free(div);
1739 return aff;
1740 error:
1741 isl_aff_free(aff);
1742 isl_mat_free(div);
1743 return NULL;
1746 /* Add two affine expressions that live in the same local space.
1748 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1749 __isl_take isl_aff *aff2)
1751 isl_int gcd, f;
1753 aff1 = isl_aff_cow(aff1);
1754 if (!aff1 || !aff2)
1755 goto error;
1757 aff1->v = isl_vec_cow(aff1->v);
1758 if (!aff1->v)
1759 goto error;
1761 isl_int_init(gcd);
1762 isl_int_init(f);
1763 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1764 isl_int_divexact(f, aff2->v->el[0], gcd);
1765 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1766 isl_int_divexact(f, aff1->v->el[0], gcd);
1767 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1768 isl_int_divexact(f, aff2->v->el[0], gcd);
1769 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1770 isl_int_clear(f);
1771 isl_int_clear(gcd);
1773 isl_aff_free(aff2);
1774 return aff1;
1775 error:
1776 isl_aff_free(aff1);
1777 isl_aff_free(aff2);
1778 return NULL;
1781 /* Return the sum of "aff1" and "aff2".
1783 * If either of the two is NaN, then the result is NaN.
1785 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1786 __isl_take isl_aff *aff2)
1788 isl_ctx *ctx;
1789 int *exp1 = NULL;
1790 int *exp2 = NULL;
1791 isl_mat *div;
1792 int n_div1, n_div2;
1794 if (!aff1 || !aff2)
1795 goto error;
1797 ctx = isl_aff_get_ctx(aff1);
1798 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1799 isl_die(ctx, isl_error_invalid,
1800 "spaces don't match", goto error);
1802 if (isl_aff_is_nan(aff1)) {
1803 isl_aff_free(aff2);
1804 return aff1;
1806 if (isl_aff_is_nan(aff2)) {
1807 isl_aff_free(aff1);
1808 return aff2;
1811 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1812 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1813 if (n_div1 == 0 && n_div2 == 0)
1814 return add_expanded(aff1, aff2);
1816 exp1 = isl_alloc_array(ctx, int, n_div1);
1817 exp2 = isl_alloc_array(ctx, int, n_div2);
1818 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1819 goto error;
1821 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1822 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1823 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1824 free(exp1);
1825 free(exp2);
1827 return add_expanded(aff1, aff2);
1828 error:
1829 free(exp1);
1830 free(exp2);
1831 isl_aff_free(aff1);
1832 isl_aff_free(aff2);
1833 return NULL;
1836 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1837 __isl_take isl_aff *aff2)
1839 return isl_aff_add(aff1, isl_aff_neg(aff2));
1842 /* Return the result of scaling "aff" by a factor of "f".
1844 * As a special case, f * NaN = NaN.
1846 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1848 isl_int gcd;
1850 if (!aff)
1851 return NULL;
1852 if (isl_aff_is_nan(aff))
1853 return aff;
1855 if (isl_int_is_one(f))
1856 return aff;
1858 aff = isl_aff_cow(aff);
1859 if (!aff)
1860 return NULL;
1861 aff->v = isl_vec_cow(aff->v);
1862 if (!aff->v)
1863 return isl_aff_free(aff);
1865 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1866 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1867 return aff;
1870 isl_int_init(gcd);
1871 isl_int_gcd(gcd, aff->v->el[0], f);
1872 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1873 isl_int_divexact(gcd, f, gcd);
1874 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1875 isl_int_clear(gcd);
1877 return aff;
1880 /* Multiple "aff" by "v".
1882 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1883 __isl_take isl_val *v)
1885 if (!aff || !v)
1886 goto error;
1888 if (isl_val_is_one(v)) {
1889 isl_val_free(v);
1890 return aff;
1893 if (!isl_val_is_rat(v))
1894 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1895 "expecting rational factor", goto error);
1897 aff = isl_aff_scale(aff, v->n);
1898 aff = isl_aff_scale_down(aff, v->d);
1900 isl_val_free(v);
1901 return aff;
1902 error:
1903 isl_aff_free(aff);
1904 isl_val_free(v);
1905 return NULL;
1908 /* Return the result of scaling "aff" down by a factor of "f".
1910 * As a special case, NaN/f = NaN.
1912 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1914 isl_int gcd;
1916 if (!aff)
1917 return NULL;
1918 if (isl_aff_is_nan(aff))
1919 return aff;
1921 if (isl_int_is_one(f))
1922 return aff;
1924 aff = isl_aff_cow(aff);
1925 if (!aff)
1926 return NULL;
1928 if (isl_int_is_zero(f))
1929 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1930 "cannot scale down by zero", return isl_aff_free(aff));
1932 aff->v = isl_vec_cow(aff->v);
1933 if (!aff->v)
1934 return isl_aff_free(aff);
1936 isl_int_init(gcd);
1937 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1938 isl_int_gcd(gcd, gcd, f);
1939 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1940 isl_int_divexact(gcd, f, gcd);
1941 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1942 isl_int_clear(gcd);
1944 return aff;
1947 /* Divide "aff" by "v".
1949 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1950 __isl_take isl_val *v)
1952 if (!aff || !v)
1953 goto error;
1955 if (isl_val_is_one(v)) {
1956 isl_val_free(v);
1957 return aff;
1960 if (!isl_val_is_rat(v))
1961 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1962 "expecting rational factor", goto error);
1963 if (!isl_val_is_pos(v))
1964 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1965 "factor needs to be positive", goto error);
1967 aff = isl_aff_scale(aff, v->d);
1968 aff = isl_aff_scale_down(aff, v->n);
1970 isl_val_free(v);
1971 return aff;
1972 error:
1973 isl_aff_free(aff);
1974 isl_val_free(v);
1975 return NULL;
1978 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1980 isl_int v;
1982 if (f == 1)
1983 return aff;
1985 isl_int_init(v);
1986 isl_int_set_ui(v, f);
1987 aff = isl_aff_scale_down(aff, v);
1988 isl_int_clear(v);
1990 return aff;
1993 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1994 enum isl_dim_type type, unsigned pos, const char *s)
1996 aff = isl_aff_cow(aff);
1997 if (!aff)
1998 return NULL;
1999 if (type == isl_dim_out)
2000 isl_die(aff->v->ctx, isl_error_invalid,
2001 "cannot set name of output/set dimension",
2002 return isl_aff_free(aff));
2003 if (type == isl_dim_in)
2004 type = isl_dim_set;
2005 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2006 if (!aff->ls)
2007 return isl_aff_free(aff);
2009 return aff;
2012 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2013 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2015 aff = isl_aff_cow(aff);
2016 if (!aff)
2017 goto error;
2018 if (type == isl_dim_out)
2019 isl_die(aff->v->ctx, isl_error_invalid,
2020 "cannot set name of output/set dimension",
2021 goto error);
2022 if (type == isl_dim_in)
2023 type = isl_dim_set;
2024 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2025 if (!aff->ls)
2026 return isl_aff_free(aff);
2028 return aff;
2029 error:
2030 isl_id_free(id);
2031 isl_aff_free(aff);
2032 return NULL;
2035 /* Replace the identifier of the input tuple of "aff" by "id".
2036 * type is currently required to be equal to isl_dim_in
2038 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2039 enum isl_dim_type type, __isl_take isl_id *id)
2041 aff = isl_aff_cow(aff);
2042 if (!aff)
2043 goto error;
2044 if (type != isl_dim_out)
2045 isl_die(aff->v->ctx, isl_error_invalid,
2046 "cannot only set id of input tuple", goto error);
2047 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2048 if (!aff->ls)
2049 return isl_aff_free(aff);
2051 return aff;
2052 error:
2053 isl_id_free(id);
2054 isl_aff_free(aff);
2055 return NULL;
2058 /* Exploit the equalities in "eq" to simplify the affine expression
2059 * and the expressions of the integer divisions in the local space.
2060 * The integer divisions in this local space are assumed to appear
2061 * as regular dimensions in "eq".
2063 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2064 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2066 int i, j;
2067 unsigned total;
2068 unsigned n_div;
2070 if (!eq)
2071 goto error;
2072 if (eq->n_eq == 0) {
2073 isl_basic_set_free(eq);
2074 return aff;
2077 aff = isl_aff_cow(aff);
2078 if (!aff)
2079 goto error;
2081 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2082 isl_basic_set_copy(eq));
2083 aff->v = isl_vec_cow(aff->v);
2084 if (!aff->ls || !aff->v)
2085 goto error;
2087 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2088 n_div = eq->n_div;
2089 for (i = 0; i < eq->n_eq; ++i) {
2090 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2091 if (j < 0 || j == 0 || j >= total)
2092 continue;
2094 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2095 &aff->v->el[0]);
2098 isl_basic_set_free(eq);
2099 aff = isl_aff_normalize(aff);
2100 return aff;
2101 error:
2102 isl_basic_set_free(eq);
2103 isl_aff_free(aff);
2104 return NULL;
2107 /* Exploit the equalities in "eq" to simplify the affine expression
2108 * and the expressions of the integer divisions in the local space.
2110 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2111 __isl_take isl_basic_set *eq)
2113 int n_div;
2115 if (!aff || !eq)
2116 goto error;
2117 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2118 if (n_div > 0)
2119 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2120 return isl_aff_substitute_equalities_lifted(aff, eq);
2121 error:
2122 isl_basic_set_free(eq);
2123 isl_aff_free(aff);
2124 return NULL;
2127 /* Look for equalities among the variables shared by context and aff
2128 * and the integer divisions of aff, if any.
2129 * The equalities are then used to eliminate coefficients and/or integer
2130 * divisions from aff.
2132 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2133 __isl_take isl_set *context)
2135 isl_basic_set *hull;
2136 int n_div;
2138 if (!aff)
2139 goto error;
2140 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2141 if (n_div > 0) {
2142 isl_basic_set *bset;
2143 isl_local_space *ls;
2144 context = isl_set_add_dims(context, isl_dim_set, n_div);
2145 ls = isl_aff_get_domain_local_space(aff);
2146 bset = isl_basic_set_from_local_space(ls);
2147 bset = isl_basic_set_lift(bset);
2148 bset = isl_basic_set_flatten(bset);
2149 context = isl_set_intersect(context,
2150 isl_set_from_basic_set(bset));
2153 hull = isl_set_affine_hull(context);
2154 return isl_aff_substitute_equalities_lifted(aff, hull);
2155 error:
2156 isl_aff_free(aff);
2157 isl_set_free(context);
2158 return NULL;
2161 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2162 __isl_take isl_set *context)
2164 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2165 dom_context = isl_set_intersect_params(dom_context, context);
2166 return isl_aff_gist(aff, dom_context);
2169 /* Return a basic set containing those elements in the space
2170 * of aff where it is positive. "rational" should not be set.
2172 * If "aff" is NaN, then it is not positive.
2174 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2175 int rational)
2177 isl_constraint *ineq;
2178 isl_basic_set *bset;
2179 isl_val *c;
2181 if (!aff)
2182 return NULL;
2183 if (isl_aff_is_nan(aff)) {
2184 isl_space *space = isl_aff_get_domain_space(aff);
2185 isl_aff_free(aff);
2186 return isl_basic_set_empty(space);
2188 if (rational)
2189 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2190 "rational sets not supported", goto error);
2192 ineq = isl_inequality_from_aff(aff);
2193 c = isl_constraint_get_constant_val(ineq);
2194 c = isl_val_sub_ui(c, 1);
2195 ineq = isl_constraint_set_constant_val(ineq, c);
2197 bset = isl_basic_set_from_constraint(ineq);
2198 bset = isl_basic_set_simplify(bset);
2199 return bset;
2200 error:
2201 isl_aff_free(aff);
2202 return NULL;
2205 /* Return a basic set containing those elements in the space
2206 * of aff where it is non-negative.
2207 * If "rational" is set, then return a rational basic set.
2209 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2211 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2212 __isl_take isl_aff *aff, int rational)
2214 isl_constraint *ineq;
2215 isl_basic_set *bset;
2217 if (!aff)
2218 return NULL;
2219 if (isl_aff_is_nan(aff)) {
2220 isl_space *space = isl_aff_get_domain_space(aff);
2221 isl_aff_free(aff);
2222 return isl_basic_set_empty(space);
2225 ineq = isl_inequality_from_aff(aff);
2227 bset = isl_basic_set_from_constraint(ineq);
2228 if (rational)
2229 bset = isl_basic_set_set_rational(bset);
2230 bset = isl_basic_set_simplify(bset);
2231 return bset;
2234 /* Return a basic set containing those elements in the space
2235 * of aff where it is non-negative.
2237 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2239 return aff_nonneg_basic_set(aff, 0);
2242 /* Return a basic set containing those elements in the domain space
2243 * of aff where it is negative.
2245 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2247 aff = isl_aff_neg(aff);
2248 aff = isl_aff_add_constant_num_si(aff, -1);
2249 return isl_aff_nonneg_basic_set(aff);
2252 /* Return a basic set containing those elements in the space
2253 * of aff where it is zero.
2254 * If "rational" is set, then return a rational basic set.
2256 * If "aff" is NaN, then it is not zero.
2258 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2259 int rational)
2261 isl_constraint *ineq;
2262 isl_basic_set *bset;
2264 if (!aff)
2265 return NULL;
2266 if (isl_aff_is_nan(aff)) {
2267 isl_space *space = isl_aff_get_domain_space(aff);
2268 isl_aff_free(aff);
2269 return isl_basic_set_empty(space);
2272 ineq = isl_equality_from_aff(aff);
2274 bset = isl_basic_set_from_constraint(ineq);
2275 if (rational)
2276 bset = isl_basic_set_set_rational(bset);
2277 bset = isl_basic_set_simplify(bset);
2278 return bset;
2281 /* Return a basic set containing those elements in the space
2282 * of aff where it is zero.
2284 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2286 return aff_zero_basic_set(aff, 0);
2289 /* Return a basic set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2292 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2293 __isl_take isl_aff *aff2)
2295 aff1 = isl_aff_sub(aff1, aff2);
2297 return isl_aff_nonneg_basic_set(aff1);
2300 /* Return a basic set containing those elements in the shared space
2301 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2303 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2304 __isl_take isl_aff *aff2)
2306 return isl_aff_ge_basic_set(aff2, aff1);
2309 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2310 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2312 aff1 = isl_aff_add(aff1, aff2);
2313 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2314 return aff1;
2317 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2319 if (!aff)
2320 return -1;
2322 return 0;
2325 /* Check whether the given affine expression has non-zero coefficient
2326 * for any dimension in the given range or if any of these dimensions
2327 * appear with non-zero coefficients in any of the integer divisions
2328 * involved in the affine expression.
2330 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
2331 enum isl_dim_type type, unsigned first, unsigned n)
2333 int i;
2334 isl_ctx *ctx;
2335 int *active = NULL;
2336 int involves = 0;
2338 if (!aff)
2339 return -1;
2340 if (n == 0)
2341 return 0;
2343 ctx = isl_aff_get_ctx(aff);
2344 if (first + n > isl_aff_dim(aff, type))
2345 isl_die(ctx, isl_error_invalid,
2346 "range out of bounds", return -1);
2348 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2349 if (!active)
2350 goto error;
2352 first += isl_local_space_offset(aff->ls, type) - 1;
2353 for (i = 0; i < n; ++i)
2354 if (active[first + i]) {
2355 involves = 1;
2356 break;
2359 free(active);
2361 return involves;
2362 error:
2363 free(active);
2364 return -1;
2367 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2368 enum isl_dim_type type, unsigned first, unsigned n)
2370 isl_ctx *ctx;
2372 if (!aff)
2373 return NULL;
2374 if (type == isl_dim_out)
2375 isl_die(aff->v->ctx, isl_error_invalid,
2376 "cannot drop output/set dimension",
2377 return isl_aff_free(aff));
2378 if (type == isl_dim_in)
2379 type = isl_dim_set;
2380 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2381 return aff;
2383 ctx = isl_aff_get_ctx(aff);
2384 if (first + n > isl_local_space_dim(aff->ls, type))
2385 isl_die(ctx, isl_error_invalid, "range out of bounds",
2386 return isl_aff_free(aff));
2388 aff = isl_aff_cow(aff);
2389 if (!aff)
2390 return NULL;
2392 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2393 if (!aff->ls)
2394 return isl_aff_free(aff);
2396 first += 1 + isl_local_space_offset(aff->ls, type);
2397 aff->v = isl_vec_drop_els(aff->v, first, n);
2398 if (!aff->v)
2399 return isl_aff_free(aff);
2401 return aff;
2404 /* Project the domain of the affine expression onto its parameter space.
2405 * The affine expression may not involve any of the domain dimensions.
2407 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2409 isl_space *space;
2410 unsigned n;
2411 int involves;
2413 n = isl_aff_dim(aff, isl_dim_in);
2414 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2415 if (involves < 0)
2416 return isl_aff_free(aff);
2417 if (involves)
2418 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2419 "affine expression involves some of the domain dimensions",
2420 return isl_aff_free(aff));
2421 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2422 space = isl_aff_get_domain_space(aff);
2423 space = isl_space_params(space);
2424 aff = isl_aff_reset_domain_space(aff, space);
2425 return aff;
2428 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2429 enum isl_dim_type type, unsigned first, unsigned n)
2431 isl_ctx *ctx;
2433 if (!aff)
2434 return NULL;
2435 if (type == isl_dim_out)
2436 isl_die(aff->v->ctx, isl_error_invalid,
2437 "cannot insert output/set dimensions",
2438 return isl_aff_free(aff));
2439 if (type == isl_dim_in)
2440 type = isl_dim_set;
2441 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2442 return aff;
2444 ctx = isl_aff_get_ctx(aff);
2445 if (first > isl_local_space_dim(aff->ls, type))
2446 isl_die(ctx, isl_error_invalid, "position out of bounds",
2447 return isl_aff_free(aff));
2449 aff = isl_aff_cow(aff);
2450 if (!aff)
2451 return NULL;
2453 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2454 if (!aff->ls)
2455 return isl_aff_free(aff);
2457 first += 1 + isl_local_space_offset(aff->ls, type);
2458 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2459 if (!aff->v)
2460 return isl_aff_free(aff);
2462 return aff;
2465 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2466 enum isl_dim_type type, unsigned n)
2468 unsigned pos;
2470 pos = isl_aff_dim(aff, type);
2472 return isl_aff_insert_dims(aff, type, pos, n);
2475 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2476 enum isl_dim_type type, unsigned n)
2478 unsigned pos;
2480 pos = isl_pw_aff_dim(pwaff, type);
2482 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2485 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2486 * to dimensions of "dst_type" at "dst_pos".
2488 * We only support moving input dimensions to parameters and vice versa.
2490 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2491 enum isl_dim_type dst_type, unsigned dst_pos,
2492 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2494 unsigned g_dst_pos;
2495 unsigned g_src_pos;
2497 if (!aff)
2498 return NULL;
2499 if (n == 0 &&
2500 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2501 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2502 return aff;
2504 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2505 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2506 "cannot move output/set dimension", isl_aff_free(aff));
2507 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2508 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2509 "cannot move divs", isl_aff_free(aff));
2510 if (dst_type == isl_dim_in)
2511 dst_type = isl_dim_set;
2512 if (src_type == isl_dim_in)
2513 src_type = isl_dim_set;
2515 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2516 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2517 "range out of bounds", isl_aff_free(aff));
2518 if (dst_type == src_type)
2519 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2520 "moving dims within the same type not supported",
2521 isl_aff_free(aff));
2523 aff = isl_aff_cow(aff);
2524 if (!aff)
2525 return NULL;
2527 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2528 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2529 if (dst_type > src_type)
2530 g_dst_pos -= n;
2532 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2533 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2534 src_type, src_pos, n);
2535 if (!aff->v || !aff->ls)
2536 return isl_aff_free(aff);
2538 aff = sort_divs(aff);
2540 return aff;
2543 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2545 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2546 return isl_pw_aff_alloc(dom, aff);
2549 #undef PW
2550 #define PW isl_pw_aff
2551 #undef EL
2552 #define EL isl_aff
2553 #undef EL_IS_ZERO
2554 #define EL_IS_ZERO is_empty
2555 #undef ZERO
2556 #define ZERO empty
2557 #undef IS_ZERO
2558 #define IS_ZERO is_empty
2559 #undef FIELD
2560 #define FIELD aff
2561 #undef DEFAULT_IS_ZERO
2562 #define DEFAULT_IS_ZERO 0
2564 #define NO_EVAL
2565 #define NO_OPT
2566 #define NO_LIFT
2567 #define NO_MORPH
2569 #include <isl_pw_templ.c>
2571 #undef UNION
2572 #define UNION isl_union_pw_aff
2573 #undef PART
2574 #define PART isl_pw_aff
2575 #undef PARTS
2576 #define PARTS pw_aff
2578 #define NO_EVAL
2580 #include <isl_union_templ.c>
2582 static __isl_give isl_set *align_params_pw_pw_set_and(
2583 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2584 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2585 __isl_take isl_pw_aff *pwaff2))
2587 if (!pwaff1 || !pwaff2)
2588 goto error;
2589 if (isl_space_match(pwaff1->dim, isl_dim_param,
2590 pwaff2->dim, isl_dim_param))
2591 return fn(pwaff1, pwaff2);
2592 if (!isl_space_has_named_params(pwaff1->dim) ||
2593 !isl_space_has_named_params(pwaff2->dim))
2594 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2595 "unaligned unnamed parameters", goto error);
2596 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2597 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2598 return fn(pwaff1, pwaff2);
2599 error:
2600 isl_pw_aff_free(pwaff1);
2601 isl_pw_aff_free(pwaff2);
2602 return NULL;
2605 /* Compute a piecewise quasi-affine expression with a domain that
2606 * is the union of those of pwaff1 and pwaff2 and such that on each
2607 * cell, the quasi-affine expression is the better (according to cmp)
2608 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2609 * is defined on a given cell, then the associated expression
2610 * is the defined one.
2612 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2613 __isl_take isl_pw_aff *pwaff2,
2614 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2615 __isl_take isl_aff *aff2))
2617 int i, j, n;
2618 isl_pw_aff *res;
2619 isl_ctx *ctx;
2620 isl_set *set;
2622 if (!pwaff1 || !pwaff2)
2623 goto error;
2625 ctx = isl_space_get_ctx(pwaff1->dim);
2626 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2627 isl_die(ctx, isl_error_invalid,
2628 "arguments should live in same space", goto error);
2630 if (isl_pw_aff_is_empty(pwaff1)) {
2631 isl_pw_aff_free(pwaff1);
2632 return pwaff2;
2635 if (isl_pw_aff_is_empty(pwaff2)) {
2636 isl_pw_aff_free(pwaff2);
2637 return pwaff1;
2640 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2641 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2643 for (i = 0; i < pwaff1->n; ++i) {
2644 set = isl_set_copy(pwaff1->p[i].set);
2645 for (j = 0; j < pwaff2->n; ++j) {
2646 struct isl_set *common;
2647 isl_set *better;
2649 common = isl_set_intersect(
2650 isl_set_copy(pwaff1->p[i].set),
2651 isl_set_copy(pwaff2->p[j].set));
2652 better = isl_set_from_basic_set(cmp(
2653 isl_aff_copy(pwaff2->p[j].aff),
2654 isl_aff_copy(pwaff1->p[i].aff)));
2655 better = isl_set_intersect(common, better);
2656 if (isl_set_plain_is_empty(better)) {
2657 isl_set_free(better);
2658 continue;
2660 set = isl_set_subtract(set, isl_set_copy(better));
2662 res = isl_pw_aff_add_piece(res, better,
2663 isl_aff_copy(pwaff2->p[j].aff));
2665 res = isl_pw_aff_add_piece(res, set,
2666 isl_aff_copy(pwaff1->p[i].aff));
2669 for (j = 0; j < pwaff2->n; ++j) {
2670 set = isl_set_copy(pwaff2->p[j].set);
2671 for (i = 0; i < pwaff1->n; ++i)
2672 set = isl_set_subtract(set,
2673 isl_set_copy(pwaff1->p[i].set));
2674 res = isl_pw_aff_add_piece(res, set,
2675 isl_aff_copy(pwaff2->p[j].aff));
2678 isl_pw_aff_free(pwaff1);
2679 isl_pw_aff_free(pwaff2);
2681 return res;
2682 error:
2683 isl_pw_aff_free(pwaff1);
2684 isl_pw_aff_free(pwaff2);
2685 return NULL;
2688 /* Compute a piecewise quasi-affine expression with a domain that
2689 * is the union of those of pwaff1 and pwaff2 and such that on each
2690 * cell, the quasi-affine expression is the maximum of those of pwaff1
2691 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2692 * cell, then the associated expression is the defined one.
2694 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2695 __isl_take isl_pw_aff *pwaff2)
2697 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2700 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2701 __isl_take isl_pw_aff *pwaff2)
2703 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2704 &pw_aff_union_max);
2707 /* Compute a piecewise quasi-affine expression with a domain that
2708 * is the union of those of pwaff1 and pwaff2 and such that on each
2709 * cell, the quasi-affine expression is the minimum of those of pwaff1
2710 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2711 * cell, then the associated expression is the defined one.
2713 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2714 __isl_take isl_pw_aff *pwaff2)
2716 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2719 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2720 __isl_take isl_pw_aff *pwaff2)
2722 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2723 &pw_aff_union_min);
2726 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2727 __isl_take isl_pw_aff *pwaff2, int max)
2729 if (max)
2730 return isl_pw_aff_union_max(pwaff1, pwaff2);
2731 else
2732 return isl_pw_aff_union_min(pwaff1, pwaff2);
2735 /* Construct a map with as domain the domain of pwaff and
2736 * one-dimensional range corresponding to the affine expressions.
2738 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2740 int i;
2741 isl_space *dim;
2742 isl_map *map;
2744 if (!pwaff)
2745 return NULL;
2747 dim = isl_pw_aff_get_space(pwaff);
2748 map = isl_map_empty(dim);
2750 for (i = 0; i < pwaff->n; ++i) {
2751 isl_basic_map *bmap;
2752 isl_map *map_i;
2754 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2755 map_i = isl_map_from_basic_map(bmap);
2756 map_i = isl_map_intersect_domain(map_i,
2757 isl_set_copy(pwaff->p[i].set));
2758 map = isl_map_union_disjoint(map, map_i);
2761 isl_pw_aff_free(pwaff);
2763 return map;
2766 /* Construct a map with as domain the domain of pwaff and
2767 * one-dimensional range corresponding to the affine expressions.
2769 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2771 if (!pwaff)
2772 return NULL;
2773 if (isl_space_is_set(pwaff->dim))
2774 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2775 "space of input is not a map", goto error);
2776 return map_from_pw_aff(pwaff);
2777 error:
2778 isl_pw_aff_free(pwaff);
2779 return NULL;
2782 /* Construct a one-dimensional set with as parameter domain
2783 * the domain of pwaff and the single set dimension
2784 * corresponding to the affine expressions.
2786 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2788 if (!pwaff)
2789 return NULL;
2790 if (!isl_space_is_set(pwaff->dim))
2791 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2792 "space of input is not a set", goto error);
2793 return map_from_pw_aff(pwaff);
2794 error:
2795 isl_pw_aff_free(pwaff);
2796 return NULL;
2799 /* Return a set containing those elements in the domain
2800 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2801 * does not satisfy "fn" (if complement is 1).
2803 * The pieces with a NaN never belong to the result since
2804 * NaN does not satisfy any property.
2806 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2807 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2808 int complement)
2810 int i;
2811 isl_set *set;
2813 if (!pwaff)
2814 return NULL;
2816 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2818 for (i = 0; i < pwaff->n; ++i) {
2819 isl_basic_set *bset;
2820 isl_set *set_i, *locus;
2821 int rational;
2823 if (isl_aff_is_nan(pwaff->p[i].aff))
2824 continue;
2826 rational = isl_set_has_rational(pwaff->p[i].set);
2827 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2828 locus = isl_set_from_basic_set(bset);
2829 set_i = isl_set_copy(pwaff->p[i].set);
2830 if (complement)
2831 set_i = isl_set_subtract(set_i, locus);
2832 else
2833 set_i = isl_set_intersect(set_i, locus);
2834 set = isl_set_union_disjoint(set, set_i);
2837 isl_pw_aff_free(pwaff);
2839 return set;
2842 /* Return a set containing those elements in the domain
2843 * of "pa" where it is positive.
2845 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2847 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2850 /* Return a set containing those elements in the domain
2851 * of pwaff where it is non-negative.
2853 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2855 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2858 /* Return a set containing those elements in the domain
2859 * of pwaff where it is zero.
2861 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2863 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2866 /* Return a set containing those elements in the domain
2867 * of pwaff where it is not zero.
2869 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2871 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2874 /* Return a set containing those elements in the shared domain
2875 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2877 * We compute the difference on the shared domain and then construct
2878 * the set of values where this difference is non-negative.
2879 * If strict is set, we first subtract 1 from the difference.
2880 * If equal is set, we only return the elements where pwaff1 and pwaff2
2881 * are equal.
2883 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2884 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2886 isl_set *set1, *set2;
2888 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2889 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2890 set1 = isl_set_intersect(set1, set2);
2891 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2892 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2893 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2895 if (strict) {
2896 isl_space *dim = isl_set_get_space(set1);
2897 isl_aff *aff;
2898 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2899 aff = isl_aff_add_constant_si(aff, -1);
2900 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2901 } else
2902 isl_set_free(set1);
2904 if (equal)
2905 return isl_pw_aff_zero_set(pwaff1);
2906 return isl_pw_aff_nonneg_set(pwaff1);
2909 /* Return a set containing those elements in the shared domain
2910 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2912 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2913 __isl_take isl_pw_aff *pwaff2)
2915 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2918 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2919 __isl_take isl_pw_aff *pwaff2)
2921 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2924 /* Return a set containing those elements in the shared domain
2925 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2927 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2928 __isl_take isl_pw_aff *pwaff2)
2930 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2933 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2934 __isl_take isl_pw_aff *pwaff2)
2936 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2939 /* Return a set containing those elements in the shared domain
2940 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2942 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2943 __isl_take isl_pw_aff *pwaff2)
2945 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2948 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2949 __isl_take isl_pw_aff *pwaff2)
2951 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2954 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2955 __isl_take isl_pw_aff *pwaff2)
2957 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2960 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2961 __isl_take isl_pw_aff *pwaff2)
2963 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2966 /* Return a set containing those elements in the shared domain
2967 * of the elements of list1 and list2 where each element in list1
2968 * has the relation specified by "fn" with each element in list2.
2970 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2971 __isl_take isl_pw_aff_list *list2,
2972 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2973 __isl_take isl_pw_aff *pwaff2))
2975 int i, j;
2976 isl_ctx *ctx;
2977 isl_set *set;
2979 if (!list1 || !list2)
2980 goto error;
2982 ctx = isl_pw_aff_list_get_ctx(list1);
2983 if (list1->n < 1 || list2->n < 1)
2984 isl_die(ctx, isl_error_invalid,
2985 "list should contain at least one element", goto error);
2987 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2988 for (i = 0; i < list1->n; ++i)
2989 for (j = 0; j < list2->n; ++j) {
2990 isl_set *set_ij;
2992 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2993 isl_pw_aff_copy(list2->p[j]));
2994 set = isl_set_intersect(set, set_ij);
2997 isl_pw_aff_list_free(list1);
2998 isl_pw_aff_list_free(list2);
2999 return set;
3000 error:
3001 isl_pw_aff_list_free(list1);
3002 isl_pw_aff_list_free(list2);
3003 return NULL;
3006 /* Return a set containing those elements in the shared domain
3007 * of the elements of list1 and list2 where each element in list1
3008 * is equal to each element in list2.
3010 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3011 __isl_take isl_pw_aff_list *list2)
3013 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3016 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3017 __isl_take isl_pw_aff_list *list2)
3019 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3022 /* Return a set containing those elements in the shared domain
3023 * of the elements of list1 and list2 where each element in list1
3024 * is less than or equal to each element in list2.
3026 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3027 __isl_take isl_pw_aff_list *list2)
3029 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3032 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3033 __isl_take isl_pw_aff_list *list2)
3035 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3038 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3039 __isl_take isl_pw_aff_list *list2)
3041 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3044 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3045 __isl_take isl_pw_aff_list *list2)
3047 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3051 /* Return a set containing those elements in the shared domain
3052 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3054 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3055 __isl_take isl_pw_aff *pwaff2)
3057 isl_set *set_lt, *set_gt;
3059 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3060 isl_pw_aff_copy(pwaff2));
3061 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3062 return isl_set_union_disjoint(set_lt, set_gt);
3065 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3066 __isl_take isl_pw_aff *pwaff2)
3068 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3071 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3072 isl_int v)
3074 int i;
3076 if (isl_int_is_one(v))
3077 return pwaff;
3078 if (!isl_int_is_pos(v))
3079 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3080 "factor needs to be positive",
3081 return isl_pw_aff_free(pwaff));
3082 pwaff = isl_pw_aff_cow(pwaff);
3083 if (!pwaff)
3084 return NULL;
3085 if (pwaff->n == 0)
3086 return pwaff;
3088 for (i = 0; i < pwaff->n; ++i) {
3089 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3090 if (!pwaff->p[i].aff)
3091 return isl_pw_aff_free(pwaff);
3094 return pwaff;
3097 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3099 int i;
3101 pwaff = isl_pw_aff_cow(pwaff);
3102 if (!pwaff)
3103 return NULL;
3104 if (pwaff->n == 0)
3105 return pwaff;
3107 for (i = 0; i < pwaff->n; ++i) {
3108 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3109 if (!pwaff->p[i].aff)
3110 return isl_pw_aff_free(pwaff);
3113 return pwaff;
3116 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3118 int i;
3120 pwaff = isl_pw_aff_cow(pwaff);
3121 if (!pwaff)
3122 return NULL;
3123 if (pwaff->n == 0)
3124 return pwaff;
3126 for (i = 0; i < pwaff->n; ++i) {
3127 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3128 if (!pwaff->p[i].aff)
3129 return isl_pw_aff_free(pwaff);
3132 return pwaff;
3135 /* Assuming that "cond1" and "cond2" are disjoint,
3136 * return an affine expression that is equal to pwaff1 on cond1
3137 * and to pwaff2 on cond2.
3139 static __isl_give isl_pw_aff *isl_pw_aff_select(
3140 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3141 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3143 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3144 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3146 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3149 /* Return an affine expression that is equal to pwaff_true for elements
3150 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3151 * is zero.
3152 * That is, return cond ? pwaff_true : pwaff_false;
3154 * If "cond" involves and NaN, then we conservatively return a NaN
3155 * on its entire domain. In principle, we could consider the pieces
3156 * where it is NaN separately from those where it is not.
3158 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3159 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3161 isl_set *cond_true, *cond_false;
3163 if (!cond)
3164 goto error;
3165 if (isl_pw_aff_involves_nan(cond)) {
3166 isl_space *space = isl_pw_aff_get_domain_space(cond);
3167 isl_local_space *ls = isl_local_space_from_space(space);
3168 isl_pw_aff_free(cond);
3169 isl_pw_aff_free(pwaff_true);
3170 isl_pw_aff_free(pwaff_false);
3171 return isl_pw_aff_nan_on_domain(ls);
3174 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3175 cond_false = isl_pw_aff_zero_set(cond);
3176 return isl_pw_aff_select(cond_true, pwaff_true,
3177 cond_false, pwaff_false);
3178 error:
3179 isl_pw_aff_free(cond);
3180 isl_pw_aff_free(pwaff_true);
3181 isl_pw_aff_free(pwaff_false);
3182 return NULL;
3185 int isl_aff_is_cst(__isl_keep isl_aff *aff)
3187 if (!aff)
3188 return -1;
3190 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3193 /* Check whether pwaff is a piecewise constant.
3195 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3197 int i;
3199 if (!pwaff)
3200 return -1;
3202 for (i = 0; i < pwaff->n; ++i) {
3203 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3204 if (is_cst < 0 || !is_cst)
3205 return is_cst;
3208 return 1;
3211 /* Return the product of "aff1" and "aff2".
3213 * If either of the two is NaN, then the result is NaN.
3215 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3217 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3218 __isl_take isl_aff *aff2)
3220 if (!aff1 || !aff2)
3221 goto error;
3223 if (isl_aff_is_nan(aff1)) {
3224 isl_aff_free(aff2);
3225 return aff1;
3227 if (isl_aff_is_nan(aff2)) {
3228 isl_aff_free(aff1);
3229 return aff2;
3232 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3233 return isl_aff_mul(aff2, aff1);
3235 if (!isl_aff_is_cst(aff2))
3236 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3237 "at least one affine expression should be constant",
3238 goto error);
3240 aff1 = isl_aff_cow(aff1);
3241 if (!aff1 || !aff2)
3242 goto error;
3244 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3245 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3247 isl_aff_free(aff2);
3248 return aff1;
3249 error:
3250 isl_aff_free(aff1);
3251 isl_aff_free(aff2);
3252 return NULL;
3255 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3257 * If either of the two is NaN, then the result is NaN.
3259 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3260 __isl_take isl_aff *aff2)
3262 int is_cst;
3263 int neg;
3265 if (!aff1 || !aff2)
3266 goto error;
3268 if (isl_aff_is_nan(aff1)) {
3269 isl_aff_free(aff2);
3270 return aff1;
3272 if (isl_aff_is_nan(aff2)) {
3273 isl_aff_free(aff1);
3274 return aff2;
3277 is_cst = isl_aff_is_cst(aff2);
3278 if (is_cst < 0)
3279 goto error;
3280 if (!is_cst)
3281 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3282 "second argument should be a constant", goto error);
3284 if (!aff2)
3285 goto error;
3287 neg = isl_int_is_neg(aff2->v->el[1]);
3288 if (neg) {
3289 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3290 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3293 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3294 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3296 if (neg) {
3297 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3298 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3301 isl_aff_free(aff2);
3302 return aff1;
3303 error:
3304 isl_aff_free(aff1);
3305 isl_aff_free(aff2);
3306 return NULL;
3309 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3310 __isl_take isl_pw_aff *pwaff2)
3312 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3315 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3316 __isl_take isl_pw_aff *pwaff2)
3318 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3321 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3322 __isl_take isl_pw_aff *pwaff2)
3324 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3327 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3328 __isl_take isl_pw_aff *pwaff2)
3330 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3333 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3334 __isl_take isl_pw_aff *pwaff2)
3336 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3339 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3340 __isl_take isl_pw_aff *pa2)
3342 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3345 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3347 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3348 __isl_take isl_pw_aff *pa2)
3350 int is_cst;
3352 is_cst = isl_pw_aff_is_cst(pa2);
3353 if (is_cst < 0)
3354 goto error;
3355 if (!is_cst)
3356 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3357 "second argument should be a piecewise constant",
3358 goto error);
3359 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3360 error:
3361 isl_pw_aff_free(pa1);
3362 isl_pw_aff_free(pa2);
3363 return NULL;
3366 /* Compute the quotient of the integer division of "pa1" by "pa2"
3367 * with rounding towards zero.
3368 * "pa2" is assumed to be a piecewise constant.
3370 * In particular, return
3372 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3375 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3376 __isl_take isl_pw_aff *pa2)
3378 int is_cst;
3379 isl_set *cond;
3380 isl_pw_aff *f, *c;
3382 is_cst = isl_pw_aff_is_cst(pa2);
3383 if (is_cst < 0)
3384 goto error;
3385 if (!is_cst)
3386 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3387 "second argument should be a piecewise constant",
3388 goto error);
3390 pa1 = isl_pw_aff_div(pa1, pa2);
3392 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3393 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3394 c = isl_pw_aff_ceil(pa1);
3395 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3396 error:
3397 isl_pw_aff_free(pa1);
3398 isl_pw_aff_free(pa2);
3399 return NULL;
3402 /* Compute the remainder of the integer division of "pa1" by "pa2"
3403 * with rounding towards zero.
3404 * "pa2" is assumed to be a piecewise constant.
3406 * In particular, return
3408 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3411 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3412 __isl_take isl_pw_aff *pa2)
3414 int is_cst;
3415 isl_pw_aff *res;
3417 is_cst = isl_pw_aff_is_cst(pa2);
3418 if (is_cst < 0)
3419 goto error;
3420 if (!is_cst)
3421 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3422 "second argument should be a piecewise constant",
3423 goto error);
3424 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3425 res = isl_pw_aff_mul(pa2, res);
3426 res = isl_pw_aff_sub(pa1, res);
3427 return res;
3428 error:
3429 isl_pw_aff_free(pa1);
3430 isl_pw_aff_free(pa2);
3431 return NULL;
3434 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3435 __isl_take isl_pw_aff *pwaff2)
3437 isl_set *le;
3438 isl_set *dom;
3440 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3441 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3442 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3443 isl_pw_aff_copy(pwaff2));
3444 dom = isl_set_subtract(dom, isl_set_copy(le));
3445 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3448 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3449 __isl_take isl_pw_aff *pwaff2)
3451 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3454 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3455 __isl_take isl_pw_aff *pwaff2)
3457 isl_set *ge;
3458 isl_set *dom;
3460 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3461 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3462 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3463 isl_pw_aff_copy(pwaff2));
3464 dom = isl_set_subtract(dom, isl_set_copy(ge));
3465 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3468 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3469 __isl_take isl_pw_aff *pwaff2)
3471 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3474 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3475 __isl_take isl_pw_aff_list *list,
3476 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3477 __isl_take isl_pw_aff *pwaff2))
3479 int i;
3480 isl_ctx *ctx;
3481 isl_pw_aff *res;
3483 if (!list)
3484 return NULL;
3486 ctx = isl_pw_aff_list_get_ctx(list);
3487 if (list->n < 1)
3488 isl_die(ctx, isl_error_invalid,
3489 "list should contain at least one element", goto error);
3491 res = isl_pw_aff_copy(list->p[0]);
3492 for (i = 1; i < list->n; ++i)
3493 res = fn(res, isl_pw_aff_copy(list->p[i]));
3495 isl_pw_aff_list_free(list);
3496 return res;
3497 error:
3498 isl_pw_aff_list_free(list);
3499 return NULL;
3502 /* Return an isl_pw_aff that maps each element in the intersection of the
3503 * domains of the elements of list to the minimal corresponding affine
3504 * expression.
3506 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3508 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3511 /* Return an isl_pw_aff that maps each element in the intersection of the
3512 * domains of the elements of list to the maximal corresponding affine
3513 * expression.
3515 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3517 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3520 /* Mark the domains of "pwaff" as rational.
3522 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3524 int i;
3526 pwaff = isl_pw_aff_cow(pwaff);
3527 if (!pwaff)
3528 return NULL;
3529 if (pwaff->n == 0)
3530 return pwaff;
3532 for (i = 0; i < pwaff->n; ++i) {
3533 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3534 if (!pwaff->p[i].set)
3535 return isl_pw_aff_free(pwaff);
3538 return pwaff;
3541 /* Mark the domains of the elements of "list" as rational.
3543 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3544 __isl_take isl_pw_aff_list *list)
3546 int i, n;
3548 if (!list)
3549 return NULL;
3550 if (list->n == 0)
3551 return list;
3553 n = list->n;
3554 for (i = 0; i < n; ++i) {
3555 isl_pw_aff *pa;
3557 pa = isl_pw_aff_list_get_pw_aff(list, i);
3558 pa = isl_pw_aff_set_rational(pa);
3559 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3562 return list;
3565 /* Do the parameters of "aff" match those of "space"?
3567 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3568 __isl_keep isl_space *space)
3570 isl_space *aff_space;
3571 int match;
3573 if (!aff || !space)
3574 return -1;
3576 aff_space = isl_aff_get_domain_space(aff);
3578 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3580 isl_space_free(aff_space);
3581 return match;
3584 /* Check that the domain space of "aff" matches "space".
3586 * Return 0 on success and -1 on error.
3588 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3589 __isl_keep isl_space *space)
3591 isl_space *aff_space;
3592 int match;
3594 if (!aff || !space)
3595 return -1;
3597 aff_space = isl_aff_get_domain_space(aff);
3599 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3600 if (match < 0)
3601 goto error;
3602 if (!match)
3603 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3604 "parameters don't match", goto error);
3605 match = isl_space_tuple_is_equal(space, isl_dim_in,
3606 aff_space, isl_dim_set);
3607 if (match < 0)
3608 goto error;
3609 if (!match)
3610 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3611 "domains don't match", goto error);
3612 isl_space_free(aff_space);
3613 return 0;
3614 error:
3615 isl_space_free(aff_space);
3616 return -1;
3619 #undef BASE
3620 #define BASE aff
3621 #define NO_INTERSECT_DOMAIN
3622 #define NO_DOMAIN
3624 #include <isl_multi_templ.c>
3626 #undef NO_DOMAIN
3627 #undef NO_INTERSECT_DOMAIN
3629 /* Remove any internal structure of the domain of "ma".
3630 * If there is any such internal structure in the input,
3631 * then the name of the corresponding space is also removed.
3633 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3634 __isl_take isl_multi_aff *ma)
3636 isl_space *space;
3638 if (!ma)
3639 return NULL;
3641 if (!ma->space->nested[0])
3642 return ma;
3644 space = isl_multi_aff_get_space(ma);
3645 space = isl_space_flatten_domain(space);
3646 ma = isl_multi_aff_reset_space(ma, space);
3648 return ma;
3651 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3652 * of the space to its domain.
3654 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3656 int i, n_in;
3657 isl_local_space *ls;
3658 isl_multi_aff *ma;
3660 if (!space)
3661 return NULL;
3662 if (!isl_space_is_map(space))
3663 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3664 "not a map space", goto error);
3666 n_in = isl_space_dim(space, isl_dim_in);
3667 space = isl_space_domain_map(space);
3669 ma = isl_multi_aff_alloc(isl_space_copy(space));
3670 if (n_in == 0) {
3671 isl_space_free(space);
3672 return ma;
3675 space = isl_space_domain(space);
3676 ls = isl_local_space_from_space(space);
3677 for (i = 0; i < n_in; ++i) {
3678 isl_aff *aff;
3680 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3681 isl_dim_set, i);
3682 ma = isl_multi_aff_set_aff(ma, i, aff);
3684 isl_local_space_free(ls);
3685 return ma;
3686 error:
3687 isl_space_free(space);
3688 return NULL;
3691 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3692 * of the space to its range.
3694 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3696 int i, n_in, n_out;
3697 isl_local_space *ls;
3698 isl_multi_aff *ma;
3700 if (!space)
3701 return NULL;
3702 if (!isl_space_is_map(space))
3703 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3704 "not a map space", goto error);
3706 n_in = isl_space_dim(space, isl_dim_in);
3707 n_out = isl_space_dim(space, isl_dim_out);
3708 space = isl_space_range_map(space);
3710 ma = isl_multi_aff_alloc(isl_space_copy(space));
3711 if (n_out == 0) {
3712 isl_space_free(space);
3713 return ma;
3716 space = isl_space_domain(space);
3717 ls = isl_local_space_from_space(space);
3718 for (i = 0; i < n_out; ++i) {
3719 isl_aff *aff;
3721 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3722 isl_dim_set, n_in + i);
3723 ma = isl_multi_aff_set_aff(ma, i, aff);
3725 isl_local_space_free(ls);
3726 return ma;
3727 error:
3728 isl_space_free(space);
3729 return NULL;
3732 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3733 * of the space to its range.
3735 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3736 __isl_take isl_space *space)
3738 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3741 /* Given the space of a set and a range of set dimensions,
3742 * construct an isl_multi_aff that projects out those dimensions.
3744 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3745 __isl_take isl_space *space, enum isl_dim_type type,
3746 unsigned first, unsigned n)
3748 int i, dim;
3749 isl_local_space *ls;
3750 isl_multi_aff *ma;
3752 if (!space)
3753 return NULL;
3754 if (!isl_space_is_set(space))
3755 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3756 "expecting set space", goto error);
3757 if (type != isl_dim_set)
3758 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3759 "only set dimensions can be projected out", goto error);
3761 dim = isl_space_dim(space, isl_dim_set);
3762 if (first + n > dim)
3763 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3764 "range out of bounds", goto error);
3766 space = isl_space_from_domain(space);
3767 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3769 if (dim == n)
3770 return isl_multi_aff_alloc(space);
3772 ma = isl_multi_aff_alloc(isl_space_copy(space));
3773 space = isl_space_domain(space);
3774 ls = isl_local_space_from_space(space);
3776 for (i = 0; i < first; ++i) {
3777 isl_aff *aff;
3779 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3780 isl_dim_set, i);
3781 ma = isl_multi_aff_set_aff(ma, i, aff);
3784 for (i = 0; i < dim - (first + n); ++i) {
3785 isl_aff *aff;
3787 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3788 isl_dim_set, first + n + i);
3789 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3792 isl_local_space_free(ls);
3793 return ma;
3794 error:
3795 isl_space_free(space);
3796 return NULL;
3799 /* Given the space of a set and a range of set dimensions,
3800 * construct an isl_pw_multi_aff that projects out those dimensions.
3802 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3803 __isl_take isl_space *space, enum isl_dim_type type,
3804 unsigned first, unsigned n)
3806 isl_multi_aff *ma;
3808 ma = isl_multi_aff_project_out_map(space, type, first, n);
3809 return isl_pw_multi_aff_from_multi_aff(ma);
3812 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3813 * domain.
3815 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3816 __isl_take isl_multi_aff *ma)
3818 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3819 return isl_pw_multi_aff_alloc(dom, ma);
3822 /* Create a piecewise multi-affine expression in the given space that maps each
3823 * input dimension to the corresponding output dimension.
3825 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3826 __isl_take isl_space *space)
3828 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3831 /* Add "ma2" to "ma1" and return the result.
3833 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
3835 static __isl_give isl_multi_aff *isl_multi_aff_add_aligned(
3836 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3838 return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
3841 /* Add "ma2" to "ma1" and return the result.
3843 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *ma1,
3844 __isl_take isl_multi_aff *ma2)
3846 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
3847 &isl_multi_aff_add_aligned);
3850 /* Exploit the equalities in "eq" to simplify the affine expressions.
3852 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3853 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3855 int i;
3857 maff = isl_multi_aff_cow(maff);
3858 if (!maff || !eq)
3859 goto error;
3861 for (i = 0; i < maff->n; ++i) {
3862 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3863 isl_basic_set_copy(eq));
3864 if (!maff->p[i])
3865 goto error;
3868 isl_basic_set_free(eq);
3869 return maff;
3870 error:
3871 isl_basic_set_free(eq);
3872 isl_multi_aff_free(maff);
3873 return NULL;
3876 /* Given f, return floor(f).
3878 __isl_give isl_multi_aff *isl_multi_aff_floor(__isl_take isl_multi_aff *ma)
3880 int i;
3882 ma = isl_multi_aff_cow(ma);
3883 if (!ma)
3884 return NULL;
3886 for (i = 0; i < ma->n; ++i) {
3887 ma->p[i] = isl_aff_floor(ma->p[i]);
3888 if (!ma->p[i])
3889 return isl_multi_aff_free(ma);
3892 return ma;
3895 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3896 isl_int f)
3898 int i;
3900 maff = isl_multi_aff_cow(maff);
3901 if (!maff)
3902 return NULL;
3904 for (i = 0; i < maff->n; ++i) {
3905 maff->p[i] = isl_aff_scale(maff->p[i], f);
3906 if (!maff->p[i])
3907 return isl_multi_aff_free(maff);
3910 return maff;
3913 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3914 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3916 maff1 = isl_multi_aff_add(maff1, maff2);
3917 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3918 return maff1;
3921 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3923 if (!maff)
3924 return -1;
3926 return 0;
3929 /* Return the set of domain elements where "ma1" is lexicographically
3930 * smaller than or equal to "ma2".
3932 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
3933 __isl_take isl_multi_aff *ma2)
3935 return isl_multi_aff_lex_ge_set(ma2, ma1);
3938 /* Return the set of domain elements where "ma1" is lexicographically
3939 * greater than or equal to "ma2".
3941 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
3942 __isl_take isl_multi_aff *ma2)
3944 isl_space *space;
3945 isl_map *map1, *map2;
3946 isl_map *map, *ge;
3948 map1 = isl_map_from_multi_aff(ma1);
3949 map2 = isl_map_from_multi_aff(ma2);
3950 map = isl_map_range_product(map1, map2);
3951 space = isl_space_range(isl_map_get_space(map));
3952 space = isl_space_domain(isl_space_unwrap(space));
3953 ge = isl_map_lex_ge(space);
3954 map = isl_map_intersect_range(map, isl_map_wrap(ge));
3956 return isl_map_domain(map);
3959 #undef PW
3960 #define PW isl_pw_multi_aff
3961 #undef EL
3962 #define EL isl_multi_aff
3963 #undef EL_IS_ZERO
3964 #define EL_IS_ZERO is_empty
3965 #undef ZERO
3966 #define ZERO empty
3967 #undef IS_ZERO
3968 #define IS_ZERO is_empty
3969 #undef FIELD
3970 #define FIELD maff
3971 #undef DEFAULT_IS_ZERO
3972 #define DEFAULT_IS_ZERO 0
3974 #define NO_SUB
3975 #define NO_EVAL
3976 #define NO_OPT
3977 #define NO_INVOLVES_DIMS
3978 #define NO_INSERT_DIMS
3979 #define NO_LIFT
3980 #define NO_MORPH
3982 #include <isl_pw_templ.c>
3984 #undef NO_SUB
3986 #undef UNION
3987 #define UNION isl_union_pw_multi_aff
3988 #undef PART
3989 #define PART isl_pw_multi_aff
3990 #undef PARTS
3991 #define PARTS pw_multi_aff
3993 #define NO_EVAL
3995 #include <isl_union_templ.c>
3997 /* Given a function "cmp" that returns the set of elements where
3998 * "ma1" is "better" than "ma2", return the intersection of this
3999 * set with "dom1" and "dom2".
4001 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
4002 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
4003 __isl_keep isl_multi_aff *ma2,
4004 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4005 __isl_take isl_multi_aff *ma2))
4007 isl_set *common;
4008 isl_set *better;
4009 int is_empty;
4011 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
4012 is_empty = isl_set_plain_is_empty(common);
4013 if (is_empty >= 0 && is_empty)
4014 return common;
4015 if (is_empty < 0)
4016 return isl_set_free(common);
4017 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
4018 better = isl_set_intersect(common, better);
4020 return better;
4023 /* Given a function "cmp" that returns the set of elements where
4024 * "ma1" is "better" than "ma2", return a piecewise multi affine
4025 * expression defined on the union of the definition domains
4026 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4027 * "pma2" on each cell. If only one of the two input functions
4028 * is defined on a given cell, then it is considered the best.
4030 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4031 __isl_take isl_pw_multi_aff *pma1,
4032 __isl_take isl_pw_multi_aff *pma2,
4033 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4034 __isl_take isl_multi_aff *ma2))
4036 int i, j, n;
4037 isl_pw_multi_aff *res = NULL;
4038 isl_ctx *ctx;
4039 isl_set *set = NULL;
4041 if (!pma1 || !pma2)
4042 goto error;
4044 ctx = isl_space_get_ctx(pma1->dim);
4045 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4046 isl_die(ctx, isl_error_invalid,
4047 "arguments should live in the same space", goto error);
4049 if (isl_pw_multi_aff_is_empty(pma1)) {
4050 isl_pw_multi_aff_free(pma1);
4051 return pma2;
4054 if (isl_pw_multi_aff_is_empty(pma2)) {
4055 isl_pw_multi_aff_free(pma2);
4056 return pma1;
4059 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4060 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4062 for (i = 0; i < pma1->n; ++i) {
4063 set = isl_set_copy(pma1->p[i].set);
4064 for (j = 0; j < pma2->n; ++j) {
4065 isl_set *better;
4066 int is_empty;
4068 better = shared_and_better(pma2->p[j].set,
4069 pma1->p[i].set, pma2->p[j].maff,
4070 pma1->p[i].maff, cmp);
4071 is_empty = isl_set_plain_is_empty(better);
4072 if (is_empty < 0 || is_empty) {
4073 isl_set_free(better);
4074 if (is_empty < 0)
4075 goto error;
4076 continue;
4078 set = isl_set_subtract(set, isl_set_copy(better));
4080 res = isl_pw_multi_aff_add_piece(res, better,
4081 isl_multi_aff_copy(pma2->p[j].maff));
4083 res = isl_pw_multi_aff_add_piece(res, set,
4084 isl_multi_aff_copy(pma1->p[i].maff));
4087 for (j = 0; j < pma2->n; ++j) {
4088 set = isl_set_copy(pma2->p[j].set);
4089 for (i = 0; i < pma1->n; ++i)
4090 set = isl_set_subtract(set,
4091 isl_set_copy(pma1->p[i].set));
4092 res = isl_pw_multi_aff_add_piece(res, set,
4093 isl_multi_aff_copy(pma2->p[j].maff));
4096 isl_pw_multi_aff_free(pma1);
4097 isl_pw_multi_aff_free(pma2);
4099 return res;
4100 error:
4101 isl_pw_multi_aff_free(pma1);
4102 isl_pw_multi_aff_free(pma2);
4103 isl_set_free(set);
4104 return isl_pw_multi_aff_free(res);
4107 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4108 __isl_take isl_pw_multi_aff *pma1,
4109 __isl_take isl_pw_multi_aff *pma2)
4111 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4114 /* Given two piecewise multi affine expressions, return a piecewise
4115 * multi-affine expression defined on the union of the definition domains
4116 * of the inputs that is equal to the lexicographic maximum of the two
4117 * inputs on each cell. If only one of the two inputs is defined on
4118 * a given cell, then it is considered to be the maximum.
4120 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4121 __isl_take isl_pw_multi_aff *pma1,
4122 __isl_take isl_pw_multi_aff *pma2)
4124 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4125 &pw_multi_aff_union_lexmax);
4128 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4129 __isl_take isl_pw_multi_aff *pma1,
4130 __isl_take isl_pw_multi_aff *pma2)
4132 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4135 /* Given two piecewise multi affine expressions, return a piecewise
4136 * multi-affine expression defined on the union of the definition domains
4137 * of the inputs that is equal to the lexicographic minimum of the two
4138 * inputs on each cell. If only one of the two inputs is defined on
4139 * a given cell, then it is considered to be the minimum.
4141 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4142 __isl_take isl_pw_multi_aff *pma1,
4143 __isl_take isl_pw_multi_aff *pma2)
4145 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4146 &pw_multi_aff_union_lexmin);
4149 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4150 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4152 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4153 &isl_multi_aff_add);
4156 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4157 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4159 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4160 &pw_multi_aff_add);
4163 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4164 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4166 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4167 &isl_multi_aff_sub);
4170 /* Subtract "pma2" from "pma1" and return the result.
4172 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4173 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4175 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4176 &pw_multi_aff_sub);
4179 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4180 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4182 return isl_pw_multi_aff_union_add_(pma1, pma2);
4185 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4186 * with the actual sum on the shared domain and
4187 * the defined expression on the symmetric difference of the domains.
4189 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4190 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4192 return isl_union_pw_aff_union_add_(upa1, upa2);
4195 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4196 * with the actual sum on the shared domain and
4197 * the defined expression on the symmetric difference of the domains.
4199 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4200 __isl_take isl_union_pw_multi_aff *upma1,
4201 __isl_take isl_union_pw_multi_aff *upma2)
4203 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4206 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4207 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4209 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4210 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4212 int i, j, n;
4213 isl_space *space;
4214 isl_pw_multi_aff *res;
4216 if (!pma1 || !pma2)
4217 goto error;
4219 n = pma1->n * pma2->n;
4220 space = isl_space_product(isl_space_copy(pma1->dim),
4221 isl_space_copy(pma2->dim));
4222 res = isl_pw_multi_aff_alloc_size(space, n);
4224 for (i = 0; i < pma1->n; ++i) {
4225 for (j = 0; j < pma2->n; ++j) {
4226 isl_set *domain;
4227 isl_multi_aff *ma;
4229 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4230 isl_set_copy(pma2->p[j].set));
4231 ma = isl_multi_aff_product(
4232 isl_multi_aff_copy(pma1->p[i].maff),
4233 isl_multi_aff_copy(pma2->p[j].maff));
4234 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4238 isl_pw_multi_aff_free(pma1);
4239 isl_pw_multi_aff_free(pma2);
4240 return res;
4241 error:
4242 isl_pw_multi_aff_free(pma1);
4243 isl_pw_multi_aff_free(pma2);
4244 return NULL;
4247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4248 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4250 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4251 &pw_multi_aff_product);
4254 /* Construct a map mapping the domain of the piecewise multi-affine expression
4255 * to its range, with each dimension in the range equated to the
4256 * corresponding affine expression on its cell.
4258 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4260 int i;
4261 isl_map *map;
4263 if (!pma)
4264 return NULL;
4266 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4268 for (i = 0; i < pma->n; ++i) {
4269 isl_multi_aff *maff;
4270 isl_basic_map *bmap;
4271 isl_map *map_i;
4273 maff = isl_multi_aff_copy(pma->p[i].maff);
4274 bmap = isl_basic_map_from_multi_aff(maff);
4275 map_i = isl_map_from_basic_map(bmap);
4276 map_i = isl_map_intersect_domain(map_i,
4277 isl_set_copy(pma->p[i].set));
4278 map = isl_map_union_disjoint(map, map_i);
4281 isl_pw_multi_aff_free(pma);
4282 return map;
4285 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4287 if (!pma)
4288 return NULL;
4290 if (!isl_space_is_set(pma->dim))
4291 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4292 "isl_pw_multi_aff cannot be converted into an isl_set",
4293 goto error);
4295 return isl_map_from_pw_multi_aff(pma);
4296 error:
4297 isl_pw_multi_aff_free(pma);
4298 return NULL;
4301 /* Given a basic map with a single output dimension that is defined
4302 * in terms of the parameters and input dimensions using an equality,
4303 * extract an isl_aff that expresses the output dimension in terms
4304 * of the parameters and input dimensions.
4305 * Note that this expression may involve integer divisions defined
4306 * in terms of parameters and input dimensions.
4308 * This function shares some similarities with
4309 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4311 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4312 __isl_take isl_basic_map *bmap)
4314 int eq;
4315 unsigned offset;
4316 unsigned n_div;
4317 isl_local_space *ls;
4318 isl_aff *aff;
4320 if (!bmap)
4321 return NULL;
4322 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4323 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4324 "basic map should have a single output dimension",
4325 goto error);
4326 eq = isl_basic_map_output_defining_equality(bmap, 0);
4327 if (eq >= bmap->n_eq)
4328 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4329 "unable to find suitable equality", goto error);
4330 ls = isl_basic_map_get_local_space(bmap);
4331 aff = isl_aff_alloc(isl_local_space_domain(ls));
4332 if (!aff)
4333 goto error;
4334 offset = isl_basic_map_offset(bmap, isl_dim_out);
4335 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4336 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4337 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4338 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4339 n_div);
4340 } else {
4341 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4342 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4343 n_div);
4345 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4346 isl_basic_map_free(bmap);
4348 aff = isl_aff_remove_unused_divs(aff);
4349 return aff;
4350 error:
4351 isl_basic_map_free(bmap);
4352 return NULL;
4355 /* Given a basic map where each output dimension is defined
4356 * in terms of the parameters and input dimensions using an equality,
4357 * extract an isl_multi_aff that expresses the output dimensions in terms
4358 * of the parameters and input dimensions.
4360 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4361 __isl_take isl_basic_map *bmap)
4363 int i;
4364 unsigned n_out;
4365 isl_multi_aff *ma;
4367 if (!bmap)
4368 return NULL;
4370 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4371 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4373 for (i = 0; i < n_out; ++i) {
4374 isl_basic_map *bmap_i;
4375 isl_aff *aff;
4377 bmap_i = isl_basic_map_copy(bmap);
4378 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4379 i + 1, n_out - (1 + i));
4380 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4381 aff = extract_isl_aff_from_basic_map(bmap_i);
4382 ma = isl_multi_aff_set_aff(ma, i, aff);
4385 isl_basic_map_free(bmap);
4387 return ma;
4390 /* Given a basic set where each set dimension is defined
4391 * in terms of the parameters using an equality,
4392 * extract an isl_multi_aff that expresses the set dimensions in terms
4393 * of the parameters.
4395 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4396 __isl_take isl_basic_set *bset)
4398 return extract_isl_multi_aff_from_basic_map(bset);
4401 /* Create an isl_pw_multi_aff that is equivalent to
4402 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4403 * The given basic map is such that each output dimension is defined
4404 * in terms of the parameters and input dimensions using an equality.
4406 * Since some applications expect the result of isl_pw_multi_aff_from_map
4407 * to only contain integer affine expressions, we compute the floor
4408 * of the expression before returning.
4410 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4411 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4413 isl_multi_aff *ma;
4415 ma = extract_isl_multi_aff_from_basic_map(bmap);
4416 ma = isl_multi_aff_floor(ma);
4417 return isl_pw_multi_aff_alloc(domain, ma);
4420 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4421 * This obviously only works if the input "map" is single-valued.
4422 * If so, we compute the lexicographic minimum of the image in the form
4423 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4424 * to its lexicographic minimum.
4425 * If the input is not single-valued, we produce an error.
4427 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4428 __isl_take isl_map *map)
4430 int i;
4431 int sv;
4432 isl_pw_multi_aff *pma;
4434 sv = isl_map_is_single_valued(map);
4435 if (sv < 0)
4436 goto error;
4437 if (!sv)
4438 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4439 "map is not single-valued", goto error);
4440 map = isl_map_make_disjoint(map);
4441 if (!map)
4442 return NULL;
4444 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4446 for (i = 0; i < map->n; ++i) {
4447 isl_pw_multi_aff *pma_i;
4448 isl_basic_map *bmap;
4449 bmap = isl_basic_map_copy(map->p[i]);
4450 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4451 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4454 isl_map_free(map);
4455 return pma;
4456 error:
4457 isl_map_free(map);
4458 return NULL;
4461 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4462 * taking into account that the output dimension at position "d"
4463 * can be represented as
4465 * x = floor((e(...) + c1) / m)
4467 * given that constraint "i" is of the form
4469 * e(...) + c1 - m x >= 0
4472 * Let "map" be of the form
4474 * A -> B
4476 * We construct a mapping
4478 * A -> [A -> x = floor(...)]
4480 * apply that to the map, obtaining
4482 * [A -> x = floor(...)] -> B
4484 * and equate dimension "d" to x.
4485 * We then compute a isl_pw_multi_aff representation of the resulting map
4486 * and plug in the mapping above.
4488 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4489 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4491 isl_ctx *ctx;
4492 isl_space *space;
4493 isl_local_space *ls;
4494 isl_multi_aff *ma;
4495 isl_aff *aff;
4496 isl_vec *v;
4497 isl_map *insert;
4498 int offset;
4499 int n;
4500 int n_in;
4501 isl_pw_multi_aff *pma;
4502 int is_set;
4504 is_set = isl_map_is_set(map);
4506 offset = isl_basic_map_offset(hull, isl_dim_out);
4507 ctx = isl_map_get_ctx(map);
4508 space = isl_space_domain(isl_map_get_space(map));
4509 n_in = isl_space_dim(space, isl_dim_set);
4510 n = isl_space_dim(space, isl_dim_all);
4512 v = isl_vec_alloc(ctx, 1 + 1 + n);
4513 if (v) {
4514 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4515 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4517 isl_basic_map_free(hull);
4519 ls = isl_local_space_from_space(isl_space_copy(space));
4520 aff = isl_aff_alloc_vec(ls, v);
4521 aff = isl_aff_floor(aff);
4522 if (is_set) {
4523 isl_space_free(space);
4524 ma = isl_multi_aff_from_aff(aff);
4525 } else {
4526 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4527 ma = isl_multi_aff_range_product(ma,
4528 isl_multi_aff_from_aff(aff));
4531 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4532 map = isl_map_apply_domain(map, insert);
4533 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4534 pma = isl_pw_multi_aff_from_map(map);
4535 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4537 return pma;
4540 /* Is constraint "c" of the form
4542 * e(...) + c1 - m x >= 0
4544 * or
4546 * -e(...) + c2 + m x >= 0
4548 * where m > 1 and e only depends on parameters and input dimemnsions?
4550 * "offset" is the offset of the output dimensions
4551 * "pos" is the position of output dimension x.
4553 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4555 if (isl_int_is_zero(c[offset + d]))
4556 return 0;
4557 if (isl_int_is_one(c[offset + d]))
4558 return 0;
4559 if (isl_int_is_negone(c[offset + d]))
4560 return 0;
4561 if (isl_seq_first_non_zero(c + offset, d) != -1)
4562 return 0;
4563 if (isl_seq_first_non_zero(c + offset + d + 1,
4564 total - (offset + d + 1)) != -1)
4565 return 0;
4566 return 1;
4569 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4571 * As a special case, we first check if there is any pair of constraints,
4572 * shared by all the basic maps in "map" that force a given dimension
4573 * to be equal to the floor of some affine combination of the input dimensions.
4575 * In particular, if we can find two constraints
4577 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4579 * and
4581 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4583 * where m > 1 and e only depends on parameters and input dimemnsions,
4584 * and such that
4586 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4588 * then we know that we can take
4590 * x = floor((e(...) + c1) / m)
4592 * without having to perform any computation.
4594 * Note that we know that
4596 * c1 + c2 >= 1
4598 * If c1 + c2 were 0, then we would have detected an equality during
4599 * simplification. If c1 + c2 were negative, then we would have detected
4600 * a contradiction.
4602 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4603 __isl_take isl_map *map)
4605 int d, dim;
4606 int i, j, n;
4607 int offset, total;
4608 isl_int sum;
4609 isl_basic_map *hull;
4611 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4612 if (!hull)
4613 goto error;
4615 isl_int_init(sum);
4616 dim = isl_map_dim(map, isl_dim_out);
4617 offset = isl_basic_map_offset(hull, isl_dim_out);
4618 total = 1 + isl_basic_map_total_dim(hull);
4619 n = hull->n_ineq;
4620 for (d = 0; d < dim; ++d) {
4621 for (i = 0; i < n; ++i) {
4622 if (!is_potential_div_constraint(hull->ineq[i],
4623 offset, d, total))
4624 continue;
4625 for (j = i + 1; j < n; ++j) {
4626 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4627 hull->ineq[j] + 1, total - 1))
4628 continue;
4629 isl_int_add(sum, hull->ineq[i][0],
4630 hull->ineq[j][0]);
4631 if (isl_int_abs_lt(sum,
4632 hull->ineq[i][offset + d]))
4633 break;
4636 if (j >= n)
4637 continue;
4638 isl_int_clear(sum);
4639 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4640 j = i;
4641 return pw_multi_aff_from_map_div(map, hull, d, j);
4644 isl_int_clear(sum);
4645 isl_basic_map_free(hull);
4646 return pw_multi_aff_from_map_base(map);
4647 error:
4648 isl_map_free(map);
4649 isl_basic_map_free(hull);
4650 return NULL;
4653 /* Given an affine expression
4655 * [A -> B] -> f(A,B)
4657 * construct an isl_multi_aff
4659 * [A -> B] -> B'
4661 * such that dimension "d" in B' is set to "aff" and the remaining
4662 * dimensions are set equal to the corresponding dimensions in B.
4663 * "n_in" is the dimension of the space A.
4664 * "n_out" is the dimension of the space B.
4666 * If "is_set" is set, then the affine expression is of the form
4668 * [B] -> f(B)
4670 * and we construct an isl_multi_aff
4672 * B -> B'
4674 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4675 unsigned n_in, unsigned n_out, int is_set)
4677 int i;
4678 isl_multi_aff *ma;
4679 isl_space *space, *space2;
4680 isl_local_space *ls;
4682 space = isl_aff_get_domain_space(aff);
4683 ls = isl_local_space_from_space(isl_space_copy(space));
4684 space2 = isl_space_copy(space);
4685 if (!is_set)
4686 space2 = isl_space_range(isl_space_unwrap(space2));
4687 space = isl_space_map_from_domain_and_range(space, space2);
4688 ma = isl_multi_aff_alloc(space);
4689 ma = isl_multi_aff_set_aff(ma, d, aff);
4691 for (i = 0; i < n_out; ++i) {
4692 if (i == d)
4693 continue;
4694 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4695 isl_dim_set, n_in + i);
4696 ma = isl_multi_aff_set_aff(ma, i, aff);
4699 isl_local_space_free(ls);
4701 return ma;
4704 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4705 * taking into account that the dimension at position "d" can be written as
4707 * x = m a + f(..) (1)
4709 * where m is equal to "gcd".
4710 * "i" is the index of the equality in "hull" that defines f(..).
4711 * In particular, the equality is of the form
4713 * f(..) - x + m g(existentials) = 0
4715 * or
4717 * -f(..) + x + m g(existentials) = 0
4719 * We basically plug (1) into "map", resulting in a map with "a"
4720 * in the range instead of "x". The corresponding isl_pw_multi_aff
4721 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4723 * Specifically, given the input map
4725 * A -> B
4727 * We first wrap it into a set
4729 * [A -> B]
4731 * and define (1) on top of the corresponding space, resulting in "aff".
4732 * We use this to create an isl_multi_aff that maps the output position "d"
4733 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4734 * We plug this into the wrapped map, unwrap the result and compute the
4735 * corresponding isl_pw_multi_aff.
4736 * The result is an expression
4738 * A -> T(A)
4740 * We adjust that to
4742 * A -> [A -> T(A)]
4744 * so that we can plug that into "aff", after extending the latter to
4745 * a mapping
4747 * [A -> B] -> B'
4750 * If "map" is actually a set, then there is no "A" space, meaning
4751 * that we do not need to perform any wrapping, and that the result
4752 * of the recursive call is of the form
4754 * [T]
4756 * which is plugged into a mapping of the form
4758 * B -> B'
4760 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4761 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4762 isl_int gcd)
4764 isl_set *set;
4765 isl_space *space;
4766 isl_local_space *ls;
4767 isl_aff *aff;
4768 isl_multi_aff *ma;
4769 isl_pw_multi_aff *pma, *id;
4770 unsigned n_in;
4771 unsigned o_out;
4772 unsigned n_out;
4773 int is_set;
4775 is_set = isl_map_is_set(map);
4777 n_in = isl_basic_map_dim(hull, isl_dim_in);
4778 n_out = isl_basic_map_dim(hull, isl_dim_out);
4779 o_out = isl_basic_map_offset(hull, isl_dim_out);
4781 if (is_set)
4782 set = map;
4783 else
4784 set = isl_map_wrap(map);
4785 space = isl_space_map_from_set(isl_set_get_space(set));
4786 ma = isl_multi_aff_identity(space);
4787 ls = isl_local_space_from_space(isl_set_get_space(set));
4788 aff = isl_aff_alloc(ls);
4789 if (aff) {
4790 isl_int_set_si(aff->v->el[0], 1);
4791 if (isl_int_is_one(hull->eq[i][o_out + d]))
4792 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4793 aff->v->size - 1);
4794 else
4795 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4796 aff->v->size - 1);
4797 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4799 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4800 set = isl_set_preimage_multi_aff(set, ma);
4802 ma = range_map(aff, d, n_in, n_out, is_set);
4804 if (is_set)
4805 map = set;
4806 else
4807 map = isl_set_unwrap(set);
4808 pma = isl_pw_multi_aff_from_map(set);
4810 if (!is_set) {
4811 space = isl_pw_multi_aff_get_domain_space(pma);
4812 space = isl_space_map_from_set(space);
4813 id = isl_pw_multi_aff_identity(space);
4814 pma = isl_pw_multi_aff_range_product(id, pma);
4816 id = isl_pw_multi_aff_from_multi_aff(ma);
4817 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4819 isl_basic_map_free(hull);
4820 return pma;
4823 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4825 * As a special case, we first check if all output dimensions are uniquely
4826 * defined in terms of the parameters and input dimensions over the entire
4827 * domain. If so, we extract the desired isl_pw_multi_aff directly
4828 * from the affine hull of "map" and its domain.
4830 * Otherwise, we check if any of the output dimensions is "strided".
4831 * That is, we check if can be written as
4833 * x = m a + f(..)
4835 * with m greater than 1, a some combination of existentiall quantified
4836 * variables and f and expression in the parameters and input dimensions.
4837 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4839 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4840 * special case.
4842 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4844 int i, j;
4845 int sv;
4846 isl_basic_map *hull;
4847 unsigned n_out;
4848 unsigned o_out;
4849 unsigned n_div;
4850 unsigned o_div;
4851 isl_int gcd;
4853 if (!map)
4854 return NULL;
4856 hull = isl_map_affine_hull(isl_map_copy(map));
4857 sv = isl_basic_map_plain_is_single_valued(hull);
4858 if (sv >= 0 && sv)
4859 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4860 if (sv < 0)
4861 hull = isl_basic_map_free(hull);
4862 if (!hull)
4863 goto error;
4865 n_div = isl_basic_map_dim(hull, isl_dim_div);
4866 o_div = isl_basic_map_offset(hull, isl_dim_div);
4868 if (n_div == 0) {
4869 isl_basic_map_free(hull);
4870 return pw_multi_aff_from_map_check_div(map);
4873 isl_int_init(gcd);
4875 n_out = isl_basic_map_dim(hull, isl_dim_out);
4876 o_out = isl_basic_map_offset(hull, isl_dim_out);
4878 for (i = 0; i < n_out; ++i) {
4879 for (j = 0; j < hull->n_eq; ++j) {
4880 isl_int *eq = hull->eq[j];
4881 isl_pw_multi_aff *res;
4883 if (!isl_int_is_one(eq[o_out + i]) &&
4884 !isl_int_is_negone(eq[o_out + i]))
4885 continue;
4886 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4887 continue;
4888 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4889 n_out - (i + 1)) != -1)
4890 continue;
4891 isl_seq_gcd(eq + o_div, n_div, &gcd);
4892 if (isl_int_is_zero(gcd))
4893 continue;
4894 if (isl_int_is_one(gcd))
4895 continue;
4897 res = pw_multi_aff_from_map_stride(map, hull,
4898 i, j, gcd);
4899 isl_int_clear(gcd);
4900 return res;
4904 isl_int_clear(gcd);
4905 isl_basic_map_free(hull);
4906 return pw_multi_aff_from_map_check_div(map);
4907 error:
4908 isl_map_free(map);
4909 return NULL;
4912 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4914 return isl_pw_multi_aff_from_map(set);
4917 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4918 * add it to *user.
4920 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
4922 isl_union_pw_multi_aff **upma = user;
4923 isl_pw_multi_aff *pma;
4925 pma = isl_pw_multi_aff_from_map(map);
4926 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4928 return *upma ? 0 : -1;
4931 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
4932 * domain.
4934 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
4935 __isl_take isl_aff *aff)
4937 isl_multi_aff *ma;
4938 isl_pw_multi_aff *pma;
4940 ma = isl_multi_aff_from_aff(aff);
4941 pma = isl_pw_multi_aff_from_multi_aff(ma);
4942 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
4945 /* Try and create an isl_union_pw_multi_aff that is equivalent
4946 * to the given isl_union_map.
4947 * The isl_union_map is required to be single-valued in each space.
4948 * Otherwise, an error is produced.
4950 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
4951 __isl_take isl_union_map *umap)
4953 isl_space *space;
4954 isl_union_pw_multi_aff *upma;
4956 space = isl_union_map_get_space(umap);
4957 upma = isl_union_pw_multi_aff_empty(space);
4958 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
4959 upma = isl_union_pw_multi_aff_free(upma);
4960 isl_union_map_free(umap);
4962 return upma;
4965 /* Try and create an isl_union_pw_multi_aff that is equivalent
4966 * to the given isl_union_set.
4967 * The isl_union_set is required to be a singleton in each space.
4968 * Otherwise, an error is produced.
4970 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
4971 __isl_take isl_union_set *uset)
4973 return isl_union_pw_multi_aff_from_union_map(uset);
4976 /* Return the piecewise affine expression "set ? 1 : 0".
4978 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
4980 isl_pw_aff *pa;
4981 isl_space *space = isl_set_get_space(set);
4982 isl_local_space *ls = isl_local_space_from_space(space);
4983 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
4984 isl_aff *one = isl_aff_zero_on_domain(ls);
4986 one = isl_aff_add_constant_si(one, 1);
4987 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
4988 set = isl_set_complement(set);
4989 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
4991 return pa;
4994 /* Plug in "subs" for dimension "type", "pos" of "aff".
4996 * Let i be the dimension to replace and let "subs" be of the form
4998 * f/d
5000 * and "aff" of the form
5002 * (a i + g)/m
5004 * The result is
5006 * (a f + d g')/(m d)
5008 * where g' is the result of plugging in "subs" in each of the integer
5009 * divisions in g.
5011 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5012 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5014 isl_ctx *ctx;
5015 isl_int v;
5017 aff = isl_aff_cow(aff);
5018 if (!aff || !subs)
5019 return isl_aff_free(aff);
5021 ctx = isl_aff_get_ctx(aff);
5022 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5023 isl_die(ctx, isl_error_invalid,
5024 "spaces don't match", return isl_aff_free(aff));
5025 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5026 isl_die(ctx, isl_error_unsupported,
5027 "cannot handle divs yet", return isl_aff_free(aff));
5029 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5030 if (!aff->ls)
5031 return isl_aff_free(aff);
5033 aff->v = isl_vec_cow(aff->v);
5034 if (!aff->v)
5035 return isl_aff_free(aff);
5037 pos += isl_local_space_offset(aff->ls, type);
5039 isl_int_init(v);
5040 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5041 aff->v->size, subs->v->size, v);
5042 isl_int_clear(v);
5044 return aff;
5047 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5048 * expressions in "maff".
5050 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5051 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5052 __isl_keep isl_aff *subs)
5054 int i;
5056 maff = isl_multi_aff_cow(maff);
5057 if (!maff || !subs)
5058 return isl_multi_aff_free(maff);
5060 if (type == isl_dim_in)
5061 type = isl_dim_set;
5063 for (i = 0; i < maff->n; ++i) {
5064 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5065 if (!maff->p[i])
5066 return isl_multi_aff_free(maff);
5069 return maff;
5072 /* Plug in "subs" for dimension "type", "pos" of "pma".
5074 * pma is of the form
5076 * A_i(v) -> M_i(v)
5078 * while subs is of the form
5080 * v' = B_j(v) -> S_j
5082 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5083 * has a contribution in the result, in particular
5085 * C_ij(S_j) -> M_i(S_j)
5087 * Note that plugging in S_j in C_ij may also result in an empty set
5088 * and this contribution should simply be discarded.
5090 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5091 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5092 __isl_keep isl_pw_aff *subs)
5094 int i, j, n;
5095 isl_pw_multi_aff *res;
5097 if (!pma || !subs)
5098 return isl_pw_multi_aff_free(pma);
5100 n = pma->n * subs->n;
5101 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5103 for (i = 0; i < pma->n; ++i) {
5104 for (j = 0; j < subs->n; ++j) {
5105 isl_set *common;
5106 isl_multi_aff *res_ij;
5107 int empty;
5109 common = isl_set_intersect(
5110 isl_set_copy(pma->p[i].set),
5111 isl_set_copy(subs->p[j].set));
5112 common = isl_set_substitute(common,
5113 type, pos, subs->p[j].aff);
5114 empty = isl_set_plain_is_empty(common);
5115 if (empty < 0 || empty) {
5116 isl_set_free(common);
5117 if (empty < 0)
5118 goto error;
5119 continue;
5122 res_ij = isl_multi_aff_substitute(
5123 isl_multi_aff_copy(pma->p[i].maff),
5124 type, pos, subs->p[j].aff);
5126 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5130 isl_pw_multi_aff_free(pma);
5131 return res;
5132 error:
5133 isl_pw_multi_aff_free(pma);
5134 isl_pw_multi_aff_free(res);
5135 return NULL;
5138 /* Compute the preimage of a range of dimensions in the affine expression "src"
5139 * under "ma" and put the result in "dst". The number of dimensions in "src"
5140 * that precede the range is given by "n_before". The number of dimensions
5141 * in the range is given by the number of output dimensions of "ma".
5142 * The number of dimensions that follow the range is given by "n_after".
5143 * If "has_denom" is set (to one),
5144 * then "src" and "dst" have an extra initial denominator.
5145 * "n_div_ma" is the number of existentials in "ma"
5146 * "n_div_bset" is the number of existentials in "src"
5147 * The resulting "dst" (which is assumed to have been allocated by
5148 * the caller) contains coefficients for both sets of existentials,
5149 * first those in "ma" and then those in "src".
5150 * f, c1, c2 and g are temporary objects that have been initialized
5151 * by the caller.
5153 * Let src represent the expression
5155 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5157 * and let ma represent the expressions
5159 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5161 * We start out with the following expression for dst:
5163 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5165 * with the multiplication factor f initially equal to 1
5166 * and f \sum_i b_i v_i kept separately.
5167 * For each x_i that we substitute, we multiply the numerator
5168 * (and denominator) of dst by c_1 = m_i and add the numerator
5169 * of the x_i expression multiplied by c_2 = f b_i,
5170 * after removing the common factors of c_1 and c_2.
5171 * The multiplication factor f also needs to be multiplied by c_1
5172 * for the next x_j, j > i.
5174 void isl_seq_preimage(isl_int *dst, isl_int *src,
5175 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5176 int n_div_ma, int n_div_bmap,
5177 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5179 int i;
5180 int n_param, n_in, n_out;
5181 int o_dst, o_src;
5183 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5184 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5185 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5187 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5188 o_dst = o_src = has_denom + 1 + n_param + n_before;
5189 isl_seq_clr(dst + o_dst, n_in);
5190 o_dst += n_in;
5191 o_src += n_out;
5192 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5193 o_dst += n_after;
5194 o_src += n_after;
5195 isl_seq_clr(dst + o_dst, n_div_ma);
5196 o_dst += n_div_ma;
5197 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5199 isl_int_set_si(f, 1);
5201 for (i = 0; i < n_out; ++i) {
5202 int offset = has_denom + 1 + n_param + n_before + i;
5204 if (isl_int_is_zero(src[offset]))
5205 continue;
5206 isl_int_set(c1, ma->p[i]->v->el[0]);
5207 isl_int_mul(c2, f, src[offset]);
5208 isl_int_gcd(g, c1, c2);
5209 isl_int_divexact(c1, c1, g);
5210 isl_int_divexact(c2, c2, g);
5212 isl_int_mul(f, f, c1);
5213 o_dst = has_denom;
5214 o_src = 1;
5215 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5216 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5217 o_dst += 1 + n_param;
5218 o_src += 1 + n_param;
5219 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5220 o_dst += n_before;
5221 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5222 c2, ma->p[i]->v->el + o_src, n_in);
5223 o_dst += n_in;
5224 o_src += n_in;
5225 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5226 o_dst += n_after;
5227 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5228 c2, ma->p[i]->v->el + o_src, n_div_ma);
5229 o_dst += n_div_ma;
5230 o_src += n_div_ma;
5231 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5232 if (has_denom)
5233 isl_int_mul(dst[0], dst[0], c1);
5237 /* Compute the pullback of "aff" by the function represented by "ma".
5238 * In other words, plug in "ma" in "aff". The result is an affine expression
5239 * defined over the domain space of "ma".
5241 * If "aff" is represented by
5243 * (a(p) + b x + c(divs))/d
5245 * and ma is represented by
5247 * x = D(p) + F(y) + G(divs')
5249 * then the result is
5251 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5253 * The divs in the local space of the input are similarly adjusted
5254 * through a call to isl_local_space_preimage_multi_aff.
5256 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5257 __isl_take isl_multi_aff *ma)
5259 isl_aff *res = NULL;
5260 isl_local_space *ls;
5261 int n_div_aff, n_div_ma;
5262 isl_int f, c1, c2, g;
5264 ma = isl_multi_aff_align_divs(ma);
5265 if (!aff || !ma)
5266 goto error;
5268 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5269 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5271 ls = isl_aff_get_domain_local_space(aff);
5272 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5273 res = isl_aff_alloc(ls);
5274 if (!res)
5275 goto error;
5277 isl_int_init(f);
5278 isl_int_init(c1);
5279 isl_int_init(c2);
5280 isl_int_init(g);
5282 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5283 f, c1, c2, g, 1);
5285 isl_int_clear(f);
5286 isl_int_clear(c1);
5287 isl_int_clear(c2);
5288 isl_int_clear(g);
5290 isl_aff_free(aff);
5291 isl_multi_aff_free(ma);
5292 res = isl_aff_normalize(res);
5293 return res;
5294 error:
5295 isl_aff_free(aff);
5296 isl_multi_aff_free(ma);
5297 isl_aff_free(res);
5298 return NULL;
5301 /* Compute the pullback of "aff1" by the function represented by "aff2".
5302 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5303 * defined over the domain space of "aff1".
5305 * The domain of "aff1" should match the range of "aff2", which means
5306 * that it should be single-dimensional.
5308 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5309 __isl_take isl_aff *aff2)
5311 isl_multi_aff *ma;
5313 ma = isl_multi_aff_from_aff(aff2);
5314 return isl_aff_pullback_multi_aff(aff1, ma);
5317 /* Compute the pullback of "ma1" by the function represented by "ma2".
5318 * In other words, plug in "ma2" in "ma1".
5320 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5322 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5323 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5325 int i;
5326 isl_space *space = NULL;
5328 ma2 = isl_multi_aff_align_divs(ma2);
5329 ma1 = isl_multi_aff_cow(ma1);
5330 if (!ma1 || !ma2)
5331 goto error;
5333 space = isl_space_join(isl_multi_aff_get_space(ma2),
5334 isl_multi_aff_get_space(ma1));
5336 for (i = 0; i < ma1->n; ++i) {
5337 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5338 isl_multi_aff_copy(ma2));
5339 if (!ma1->p[i])
5340 goto error;
5343 ma1 = isl_multi_aff_reset_space(ma1, space);
5344 isl_multi_aff_free(ma2);
5345 return ma1;
5346 error:
5347 isl_space_free(space);
5348 isl_multi_aff_free(ma2);
5349 isl_multi_aff_free(ma1);
5350 return NULL;
5353 /* Compute the pullback of "ma1" by the function represented by "ma2".
5354 * In other words, plug in "ma2" in "ma1".
5356 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5357 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5359 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5360 &isl_multi_aff_pullback_multi_aff_aligned);
5363 /* Extend the local space of "dst" to include the divs
5364 * in the local space of "src".
5366 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5367 __isl_keep isl_aff *src)
5369 isl_ctx *ctx;
5370 int *exp1 = NULL;
5371 int *exp2 = NULL;
5372 isl_mat *div;
5374 if (!src || !dst)
5375 return isl_aff_free(dst);
5377 ctx = isl_aff_get_ctx(src);
5378 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5379 isl_die(ctx, isl_error_invalid,
5380 "spaces don't match", goto error);
5382 if (src->ls->div->n_row == 0)
5383 return dst;
5385 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5386 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5387 if (!exp1 || (dst->ls->div->n_row && !exp2))
5388 goto error;
5390 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5391 dst = isl_aff_expand_divs(dst, div, exp2);
5392 free(exp1);
5393 free(exp2);
5395 return dst;
5396 error:
5397 free(exp1);
5398 free(exp2);
5399 return isl_aff_free(dst);
5402 /* Adjust the local spaces of the affine expressions in "maff"
5403 * such that they all have the save divs.
5405 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5406 __isl_take isl_multi_aff *maff)
5408 int i;
5410 if (!maff)
5411 return NULL;
5412 if (maff->n == 0)
5413 return maff;
5414 maff = isl_multi_aff_cow(maff);
5415 if (!maff)
5416 return NULL;
5418 for (i = 1; i < maff->n; ++i)
5419 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5420 for (i = 1; i < maff->n; ++i) {
5421 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5422 if (!maff->p[i])
5423 return isl_multi_aff_free(maff);
5426 return maff;
5429 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5431 aff = isl_aff_cow(aff);
5432 if (!aff)
5433 return NULL;
5435 aff->ls = isl_local_space_lift(aff->ls);
5436 if (!aff->ls)
5437 return isl_aff_free(aff);
5439 return aff;
5442 /* Lift "maff" to a space with extra dimensions such that the result
5443 * has no more existentially quantified variables.
5444 * If "ls" is not NULL, then *ls is assigned the local space that lies
5445 * at the basis of the lifting applied to "maff".
5447 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5448 __isl_give isl_local_space **ls)
5450 int i;
5451 isl_space *space;
5452 unsigned n_div;
5454 if (ls)
5455 *ls = NULL;
5457 if (!maff)
5458 return NULL;
5460 if (maff->n == 0) {
5461 if (ls) {
5462 isl_space *space = isl_multi_aff_get_domain_space(maff);
5463 *ls = isl_local_space_from_space(space);
5464 if (!*ls)
5465 return isl_multi_aff_free(maff);
5467 return maff;
5470 maff = isl_multi_aff_cow(maff);
5471 maff = isl_multi_aff_align_divs(maff);
5472 if (!maff)
5473 return NULL;
5475 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5476 space = isl_multi_aff_get_space(maff);
5477 space = isl_space_lift(isl_space_domain(space), n_div);
5478 space = isl_space_extend_domain_with_range(space,
5479 isl_multi_aff_get_space(maff));
5480 if (!space)
5481 return isl_multi_aff_free(maff);
5482 isl_space_free(maff->space);
5483 maff->space = space;
5485 if (ls) {
5486 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5487 if (!*ls)
5488 return isl_multi_aff_free(maff);
5491 for (i = 0; i < maff->n; ++i) {
5492 maff->p[i] = isl_aff_lift(maff->p[i]);
5493 if (!maff->p[i])
5494 goto error;
5497 return maff;
5498 error:
5499 if (ls)
5500 isl_local_space_free(*ls);
5501 return isl_multi_aff_free(maff);
5505 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5507 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5508 __isl_keep isl_pw_multi_aff *pma, int pos)
5510 int i;
5511 int n_out;
5512 isl_space *space;
5513 isl_pw_aff *pa;
5515 if (!pma)
5516 return NULL;
5518 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5519 if (pos < 0 || pos >= n_out)
5520 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5521 "index out of bounds", return NULL);
5523 space = isl_pw_multi_aff_get_space(pma);
5524 space = isl_space_drop_dims(space, isl_dim_out,
5525 pos + 1, n_out - pos - 1);
5526 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5528 pa = isl_pw_aff_alloc_size(space, pma->n);
5529 for (i = 0; i < pma->n; ++i) {
5530 isl_aff *aff;
5531 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5532 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5535 return pa;
5538 /* Return an isl_pw_multi_aff with the given "set" as domain and
5539 * an unnamed zero-dimensional range.
5541 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5542 __isl_take isl_set *set)
5544 isl_multi_aff *ma;
5545 isl_space *space;
5547 space = isl_set_get_space(set);
5548 space = isl_space_from_domain(space);
5549 ma = isl_multi_aff_zero(space);
5550 return isl_pw_multi_aff_alloc(set, ma);
5553 /* Add an isl_pw_multi_aff with the given "set" as domain and
5554 * an unnamed zero-dimensional range to *user.
5556 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
5558 isl_union_pw_multi_aff **upma = user;
5559 isl_pw_multi_aff *pma;
5561 pma = isl_pw_multi_aff_from_domain(set);
5562 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5564 return 0;
5567 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5568 * an unnamed zero-dimensional range.
5570 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5571 __isl_take isl_union_set *uset)
5573 isl_space *space;
5574 isl_union_pw_multi_aff *upma;
5576 if (!uset)
5577 return NULL;
5579 space = isl_union_set_get_space(uset);
5580 upma = isl_union_pw_multi_aff_empty(space);
5582 if (isl_union_set_foreach_set(uset,
5583 &add_pw_multi_aff_from_domain, &upma) < 0)
5584 goto error;
5586 isl_union_set_free(uset);
5587 return upma;
5588 error:
5589 isl_union_set_free(uset);
5590 isl_union_pw_multi_aff_free(upma);
5591 return NULL;
5594 /* Convert "pma" to an isl_map and add it to *umap.
5596 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
5598 isl_union_map **umap = user;
5599 isl_map *map;
5601 map = isl_map_from_pw_multi_aff(pma);
5602 *umap = isl_union_map_add_map(*umap, map);
5604 return 0;
5607 /* Construct a union map mapping the domain of the union
5608 * piecewise multi-affine expression to its range, with each dimension
5609 * in the range equated to the corresponding affine expression on its cell.
5611 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5612 __isl_take isl_union_pw_multi_aff *upma)
5614 isl_space *space;
5615 isl_union_map *umap;
5617 if (!upma)
5618 return NULL;
5620 space = isl_union_pw_multi_aff_get_space(upma);
5621 umap = isl_union_map_empty(space);
5623 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5624 &map_from_pw_multi_aff, &umap) < 0)
5625 goto error;
5627 isl_union_pw_multi_aff_free(upma);
5628 return umap;
5629 error:
5630 isl_union_pw_multi_aff_free(upma);
5631 isl_union_map_free(umap);
5632 return NULL;
5635 /* Local data for bin_entry and the callback "fn".
5637 struct isl_union_pw_multi_aff_bin_data {
5638 isl_union_pw_multi_aff *upma2;
5639 isl_union_pw_multi_aff *res;
5640 isl_pw_multi_aff *pma;
5641 int (*fn)(void **entry, void *user);
5644 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5645 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5647 static int bin_entry(void **entry, void *user)
5649 struct isl_union_pw_multi_aff_bin_data *data = user;
5650 isl_pw_multi_aff *pma = *entry;
5652 data->pma = pma;
5653 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5654 data->fn, data) < 0)
5655 return -1;
5657 return 0;
5660 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5661 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5662 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5663 * as *entry. The callback should adjust data->res if desired.
5665 static __isl_give isl_union_pw_multi_aff *bin_op(
5666 __isl_take isl_union_pw_multi_aff *upma1,
5667 __isl_take isl_union_pw_multi_aff *upma2,
5668 int (*fn)(void **entry, void *user))
5670 isl_space *space;
5671 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5673 space = isl_union_pw_multi_aff_get_space(upma2);
5674 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5675 space = isl_union_pw_multi_aff_get_space(upma1);
5676 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5678 if (!upma1 || !upma2)
5679 goto error;
5681 data.upma2 = upma2;
5682 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5683 upma1->table.n);
5684 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5685 &bin_entry, &data) < 0)
5686 goto error;
5688 isl_union_pw_multi_aff_free(upma1);
5689 isl_union_pw_multi_aff_free(upma2);
5690 return data.res;
5691 error:
5692 isl_union_pw_multi_aff_free(upma1);
5693 isl_union_pw_multi_aff_free(upma2);
5694 isl_union_pw_multi_aff_free(data.res);
5695 return NULL;
5698 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5699 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5701 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5702 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5704 isl_space *space;
5706 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5707 isl_pw_multi_aff_get_space(pma2));
5708 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5709 &isl_multi_aff_range_product);
5712 /* Given two isl_pw_multi_affs A -> B and C -> D,
5713 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5715 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5716 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5718 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5719 &pw_multi_aff_range_product);
5722 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5723 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5725 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5726 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5728 isl_space *space;
5730 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5731 isl_pw_multi_aff_get_space(pma2));
5732 space = isl_space_flatten_range(space);
5733 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5734 &isl_multi_aff_flat_range_product);
5737 /* Given two isl_pw_multi_affs A -> B and C -> D,
5738 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5740 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5741 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5743 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5744 &pw_multi_aff_flat_range_product);
5747 /* If data->pma and *entry have the same domain space, then compute
5748 * their flat range product and the result to data->res.
5750 static int flat_range_product_entry(void **entry, void *user)
5752 struct isl_union_pw_multi_aff_bin_data *data = user;
5753 isl_pw_multi_aff *pma2 = *entry;
5755 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5756 pma2->dim, isl_dim_in))
5757 return 0;
5759 pma2 = isl_pw_multi_aff_flat_range_product(
5760 isl_pw_multi_aff_copy(data->pma),
5761 isl_pw_multi_aff_copy(pma2));
5763 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5765 return 0;
5768 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5769 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5771 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5772 __isl_take isl_union_pw_multi_aff *upma1,
5773 __isl_take isl_union_pw_multi_aff *upma2)
5775 return bin_op(upma1, upma2, &flat_range_product_entry);
5778 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5779 * The parameters are assumed to have been aligned.
5781 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5782 * except that it works on two different isl_pw_* types.
5784 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5785 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5786 __isl_take isl_pw_aff *pa)
5788 int i, j, n;
5789 isl_pw_multi_aff *res = NULL;
5791 if (!pma || !pa)
5792 goto error;
5794 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5795 pa->dim, isl_dim_in))
5796 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5797 "domains don't match", goto error);
5798 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5799 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5800 "index out of bounds", goto error);
5802 n = pma->n * pa->n;
5803 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5805 for (i = 0; i < pma->n; ++i) {
5806 for (j = 0; j < pa->n; ++j) {
5807 isl_set *common;
5808 isl_multi_aff *res_ij;
5809 int empty;
5811 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5812 isl_set_copy(pa->p[j].set));
5813 empty = isl_set_plain_is_empty(common);
5814 if (empty < 0 || empty) {
5815 isl_set_free(common);
5816 if (empty < 0)
5817 goto error;
5818 continue;
5821 res_ij = isl_multi_aff_set_aff(
5822 isl_multi_aff_copy(pma->p[i].maff), pos,
5823 isl_aff_copy(pa->p[j].aff));
5824 res_ij = isl_multi_aff_gist(res_ij,
5825 isl_set_copy(common));
5827 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5831 isl_pw_multi_aff_free(pma);
5832 isl_pw_aff_free(pa);
5833 return res;
5834 error:
5835 isl_pw_multi_aff_free(pma);
5836 isl_pw_aff_free(pa);
5837 return isl_pw_multi_aff_free(res);
5840 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5842 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5843 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5844 __isl_take isl_pw_aff *pa)
5846 if (!pma || !pa)
5847 goto error;
5848 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5849 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5850 if (!isl_space_has_named_params(pma->dim) ||
5851 !isl_space_has_named_params(pa->dim))
5852 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5853 "unaligned unnamed parameters", goto error);
5854 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5855 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5856 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5857 error:
5858 isl_pw_multi_aff_free(pma);
5859 isl_pw_aff_free(pa);
5860 return NULL;
5863 /* Do the parameters of "pa" match those of "space"?
5865 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5866 __isl_keep isl_space *space)
5868 isl_space *pa_space;
5869 int match;
5871 if (!pa || !space)
5872 return -1;
5874 pa_space = isl_pw_aff_get_space(pa);
5876 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5878 isl_space_free(pa_space);
5879 return match;
5882 /* Check that the domain space of "pa" matches "space".
5884 * Return 0 on success and -1 on error.
5886 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5887 __isl_keep isl_space *space)
5889 isl_space *pa_space;
5890 int match;
5892 if (!pa || !space)
5893 return -1;
5895 pa_space = isl_pw_aff_get_space(pa);
5897 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5898 if (match < 0)
5899 goto error;
5900 if (!match)
5901 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5902 "parameters don't match", goto error);
5903 match = isl_space_tuple_is_equal(space, isl_dim_in,
5904 pa_space, isl_dim_in);
5905 if (match < 0)
5906 goto error;
5907 if (!match)
5908 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5909 "domains don't match", goto error);
5910 isl_space_free(pa_space);
5911 return 0;
5912 error:
5913 isl_space_free(pa_space);
5914 return -1;
5917 #undef BASE
5918 #define BASE pw_aff
5920 #include <isl_multi_templ.c>
5922 /* Scale the elements of "pma" by the corresponding elements of "mv".
5924 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
5925 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
5927 int i;
5929 pma = isl_pw_multi_aff_cow(pma);
5930 if (!pma || !mv)
5931 goto error;
5932 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5933 mv->space, isl_dim_set))
5934 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5935 "spaces don't match", goto error);
5936 if (!isl_space_match(pma->dim, isl_dim_param,
5937 mv->space, isl_dim_param)) {
5938 pma = isl_pw_multi_aff_align_params(pma,
5939 isl_multi_val_get_space(mv));
5940 mv = isl_multi_val_align_params(mv,
5941 isl_pw_multi_aff_get_space(pma));
5942 if (!pma || !mv)
5943 goto error;
5946 for (i = 0; i < pma->n; ++i) {
5947 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
5948 isl_multi_val_copy(mv));
5949 if (!pma->p[i].maff)
5950 goto error;
5953 isl_multi_val_free(mv);
5954 return pma;
5955 error:
5956 isl_multi_val_free(mv);
5957 isl_pw_multi_aff_free(pma);
5958 return NULL;
5961 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
5962 * mv contains the mv argument.
5963 * res collects the results.
5965 struct isl_union_pw_multi_aff_scale_multi_val_data {
5966 isl_multi_val *mv;
5967 isl_union_pw_multi_aff *res;
5970 /* This function is called for each entry of an isl_union_pw_multi_aff.
5971 * If the space of the entry matches that of data->mv,
5972 * then apply isl_pw_multi_aff_scale_multi_val and add the result
5973 * to data->res.
5975 static int union_pw_multi_aff_scale_multi_val_entry(void **entry, void *user)
5977 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
5978 isl_pw_multi_aff *pma = *entry;
5980 if (!pma)
5981 return -1;
5982 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
5983 data->mv->space, isl_dim_set))
5984 return 0;
5986 pma = isl_pw_multi_aff_copy(pma);
5987 pma = isl_pw_multi_aff_scale_multi_val(pma,
5988 isl_multi_val_copy(data->mv));
5989 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
5990 if (!data->res)
5991 return -1;
5993 return 0;
5996 /* Scale the elements of "upma" by the corresponding elements of "mv",
5997 * for those entries that match the space of "mv".
5999 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6000 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6002 struct isl_union_pw_multi_aff_scale_multi_val_data data;
6004 upma = isl_union_pw_multi_aff_align_params(upma,
6005 isl_multi_val_get_space(mv));
6006 mv = isl_multi_val_align_params(mv,
6007 isl_union_pw_multi_aff_get_space(upma));
6008 if (!upma || !mv)
6009 goto error;
6011 data.mv = mv;
6012 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
6013 upma->table.n);
6014 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
6015 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
6016 goto error;
6018 isl_multi_val_free(mv);
6019 isl_union_pw_multi_aff_free(upma);
6020 return data.res;
6021 error:
6022 isl_multi_val_free(mv);
6023 isl_union_pw_multi_aff_free(upma);
6024 return NULL;
6027 /* Construct and return a piecewise multi affine expression
6028 * in the given space with value zero in each of the output dimensions and
6029 * a universe domain.
6031 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6033 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6036 /* Construct and return a piecewise multi affine expression
6037 * that is equal to the given piecewise affine expression.
6039 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6040 __isl_take isl_pw_aff *pa)
6042 int i;
6043 isl_space *space;
6044 isl_pw_multi_aff *pma;
6046 if (!pa)
6047 return NULL;
6049 space = isl_pw_aff_get_space(pa);
6050 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6052 for (i = 0; i < pa->n; ++i) {
6053 isl_set *set;
6054 isl_multi_aff *ma;
6056 set = isl_set_copy(pa->p[i].set);
6057 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6058 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6061 isl_pw_aff_free(pa);
6062 return pma;
6065 /* Construct a set or map mapping the shared (parameter) domain
6066 * of the piecewise affine expressions to the range of "mpa"
6067 * with each dimension in the range equated to the
6068 * corresponding piecewise affine expression.
6070 static __isl_give isl_map *map_from_multi_pw_aff(
6071 __isl_take isl_multi_pw_aff *mpa)
6073 int i;
6074 isl_space *space;
6075 isl_map *map;
6077 if (!mpa)
6078 return NULL;
6080 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6081 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6082 "invalid space", goto error);
6084 space = isl_multi_pw_aff_get_domain_space(mpa);
6085 map = isl_map_universe(isl_space_from_domain(space));
6087 for (i = 0; i < mpa->n; ++i) {
6088 isl_pw_aff *pa;
6089 isl_map *map_i;
6091 pa = isl_pw_aff_copy(mpa->p[i]);
6092 map_i = map_from_pw_aff(pa);
6094 map = isl_map_flat_range_product(map, map_i);
6097 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6099 isl_multi_pw_aff_free(mpa);
6100 return map;
6101 error:
6102 isl_multi_pw_aff_free(mpa);
6103 return NULL;
6106 /* Construct a map mapping the shared domain
6107 * of the piecewise affine expressions to the range of "mpa"
6108 * with each dimension in the range equated to the
6109 * corresponding piecewise affine expression.
6111 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6113 if (!mpa)
6114 return NULL;
6115 if (isl_space_is_set(mpa->space))
6116 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6117 "space of input is not a map", goto error);
6119 return map_from_multi_pw_aff(mpa);
6120 error:
6121 isl_multi_pw_aff_free(mpa);
6122 return NULL;
6125 /* Construct a set mapping the shared parameter domain
6126 * of the piecewise affine expressions to the space of "mpa"
6127 * with each dimension in the range equated to the
6128 * corresponding piecewise affine expression.
6130 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6132 if (!mpa)
6133 return NULL;
6134 if (!isl_space_is_set(mpa->space))
6135 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6136 "space of input is not a set", goto error);
6138 return map_from_multi_pw_aff(mpa);
6139 error:
6140 isl_multi_pw_aff_free(mpa);
6141 return NULL;
6144 /* Construct and return a piecewise multi affine expression
6145 * that is equal to the given multi piecewise affine expression
6146 * on the shared domain of the piecewise affine expressions.
6148 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6149 __isl_take isl_multi_pw_aff *mpa)
6151 int i;
6152 isl_space *space;
6153 isl_pw_aff *pa;
6154 isl_pw_multi_aff *pma;
6156 if (!mpa)
6157 return NULL;
6159 space = isl_multi_pw_aff_get_space(mpa);
6161 if (mpa->n == 0) {
6162 isl_multi_pw_aff_free(mpa);
6163 return isl_pw_multi_aff_zero(space);
6166 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6167 pma = isl_pw_multi_aff_from_pw_aff(pa);
6169 for (i = 1; i < mpa->n; ++i) {
6170 isl_pw_multi_aff *pma_i;
6172 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6173 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6174 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6177 pma = isl_pw_multi_aff_reset_space(pma, space);
6179 isl_multi_pw_aff_free(mpa);
6180 return pma;
6183 /* Construct and return a multi piecewise affine expression
6184 * that is equal to the given multi affine expression.
6186 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6187 __isl_take isl_multi_aff *ma)
6189 int i, n;
6190 isl_multi_pw_aff *mpa;
6192 if (!ma)
6193 return NULL;
6195 n = isl_multi_aff_dim(ma, isl_dim_out);
6196 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6198 for (i = 0; i < n; ++i) {
6199 isl_pw_aff *pa;
6201 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6202 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6205 isl_multi_aff_free(ma);
6206 return mpa;
6209 /* Construct and return a multi piecewise affine expression
6210 * that is equal to the given piecewise multi affine expression.
6212 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6213 __isl_take isl_pw_multi_aff *pma)
6215 int i, n;
6216 isl_space *space;
6217 isl_multi_pw_aff *mpa;
6219 if (!pma)
6220 return NULL;
6222 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6223 space = isl_pw_multi_aff_get_space(pma);
6224 mpa = isl_multi_pw_aff_alloc(space);
6226 for (i = 0; i < n; ++i) {
6227 isl_pw_aff *pa;
6229 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6230 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6233 isl_pw_multi_aff_free(pma);
6234 return mpa;
6237 /* Do "pa1" and "pa2" represent the same function?
6239 * We first check if they are obviously equal.
6240 * If not, we convert them to maps and check if those are equal.
6242 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6244 int equal;
6245 isl_map *map1, *map2;
6247 if (!pa1 || !pa2)
6248 return -1;
6250 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6251 if (equal < 0 || equal)
6252 return equal;
6254 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6255 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6256 equal = isl_map_is_equal(map1, map2);
6257 isl_map_free(map1);
6258 isl_map_free(map2);
6260 return equal;
6263 /* Do "mpa1" and "mpa2" represent the same function?
6265 * Note that we cannot convert the entire isl_multi_pw_aff
6266 * to a map because the domains of the piecewise affine expressions
6267 * may not be the same.
6269 int isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6270 __isl_keep isl_multi_pw_aff *mpa2)
6272 int i;
6273 int equal;
6275 if (!mpa1 || !mpa2)
6276 return -1;
6278 if (!isl_space_match(mpa1->space, isl_dim_param,
6279 mpa2->space, isl_dim_param)) {
6280 if (!isl_space_has_named_params(mpa1->space))
6281 return 0;
6282 if (!isl_space_has_named_params(mpa2->space))
6283 return 0;
6284 mpa1 = isl_multi_pw_aff_copy(mpa1);
6285 mpa2 = isl_multi_pw_aff_copy(mpa2);
6286 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6287 isl_multi_pw_aff_get_space(mpa2));
6288 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6289 isl_multi_pw_aff_get_space(mpa1));
6290 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6291 isl_multi_pw_aff_free(mpa1);
6292 isl_multi_pw_aff_free(mpa2);
6293 return equal;
6296 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6297 if (equal < 0 || !equal)
6298 return equal;
6300 for (i = 0; i < mpa1->n; ++i) {
6301 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6302 if (equal < 0 || !equal)
6303 return equal;
6306 return 1;
6309 /* Coalesce the elements of "mpa".
6311 * Note that such coalescing does not change the meaning of "mpa"
6312 * so there is no need to cow. We do need to be careful not to
6313 * destroy any other copies of "mpa" in case of failure.
6315 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6316 __isl_take isl_multi_pw_aff *mpa)
6318 int i;
6320 if (!mpa)
6321 return NULL;
6323 for (i = 0; i < mpa->n; ++i) {
6324 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6325 pa = isl_pw_aff_coalesce(pa);
6326 if (!pa)
6327 return isl_multi_pw_aff_free(mpa);
6328 isl_pw_aff_free(mpa->p[i]);
6329 mpa->p[i] = pa;
6332 return mpa;
6335 /* Compute the pullback of "mpa" by the function represented by "ma".
6336 * In other words, plug in "ma" in "mpa".
6338 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6340 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6341 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6343 int i;
6344 isl_space *space = NULL;
6346 mpa = isl_multi_pw_aff_cow(mpa);
6347 if (!mpa || !ma)
6348 goto error;
6350 space = isl_space_join(isl_multi_aff_get_space(ma),
6351 isl_multi_pw_aff_get_space(mpa));
6352 if (!space)
6353 goto error;
6355 for (i = 0; i < mpa->n; ++i) {
6356 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6357 isl_multi_aff_copy(ma));
6358 if (!mpa->p[i])
6359 goto error;
6362 isl_multi_aff_free(ma);
6363 isl_space_free(mpa->space);
6364 mpa->space = space;
6365 return mpa;
6366 error:
6367 isl_space_free(space);
6368 isl_multi_pw_aff_free(mpa);
6369 isl_multi_aff_free(ma);
6370 return NULL;
6373 /* Compute the pullback of "mpa" by the function represented by "ma".
6374 * In other words, plug in "ma" in "mpa".
6376 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6377 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6379 if (!mpa || !ma)
6380 goto error;
6381 if (isl_space_match(mpa->space, isl_dim_param,
6382 ma->space, isl_dim_param))
6383 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6384 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6385 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6386 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6387 error:
6388 isl_multi_pw_aff_free(mpa);
6389 isl_multi_aff_free(ma);
6390 return NULL;
6393 /* Compute the pullback of "mpa" by the function represented by "pma".
6394 * In other words, plug in "pma" in "mpa".
6396 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6398 static __isl_give isl_multi_pw_aff *
6399 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6400 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6402 int i;
6403 isl_space *space = NULL;
6405 mpa = isl_multi_pw_aff_cow(mpa);
6406 if (!mpa || !pma)
6407 goto error;
6409 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6410 isl_multi_pw_aff_get_space(mpa));
6412 for (i = 0; i < mpa->n; ++i) {
6413 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6414 isl_pw_multi_aff_copy(pma));
6415 if (!mpa->p[i])
6416 goto error;
6419 isl_pw_multi_aff_free(pma);
6420 isl_space_free(mpa->space);
6421 mpa->space = space;
6422 return mpa;
6423 error:
6424 isl_space_free(space);
6425 isl_multi_pw_aff_free(mpa);
6426 isl_pw_multi_aff_free(pma);
6427 return NULL;
6430 /* Compute the pullback of "mpa" by the function represented by "pma".
6431 * In other words, plug in "pma" in "mpa".
6433 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6434 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6436 if (!mpa || !pma)
6437 goto error;
6438 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6439 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6440 mpa = isl_multi_pw_aff_align_params(mpa,
6441 isl_pw_multi_aff_get_space(pma));
6442 pma = isl_pw_multi_aff_align_params(pma,
6443 isl_multi_pw_aff_get_space(mpa));
6444 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6445 error:
6446 isl_multi_pw_aff_free(mpa);
6447 isl_pw_multi_aff_free(pma);
6448 return NULL;
6451 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6452 * with the domain of "aff". The domain of the result is the same
6453 * as that of "mpa".
6454 * "mpa" and "aff" are assumed to have been aligned.
6456 * We first extract the parametric constant from "aff", defined
6457 * over the correct domain.
6458 * Then we add the appropriate combinations of the members of "mpa".
6459 * Finally, we add the integer divisions through recursive calls.
6461 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6462 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6464 int i, n_param, n_in, n_div;
6465 isl_space *space;
6466 isl_val *v;
6467 isl_pw_aff *pa;
6468 isl_aff *tmp;
6470 n_param = isl_aff_dim(aff, isl_dim_param);
6471 n_in = isl_aff_dim(aff, isl_dim_in);
6472 n_div = isl_aff_dim(aff, isl_dim_div);
6474 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6475 tmp = isl_aff_copy(aff);
6476 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6477 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6478 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6479 isl_space_dim(space, isl_dim_set));
6480 tmp = isl_aff_reset_domain_space(tmp, space);
6481 pa = isl_pw_aff_from_aff(tmp);
6483 for (i = 0; i < n_in; ++i) {
6484 isl_pw_aff *pa_i;
6486 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6487 continue;
6488 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6489 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6490 pa_i = isl_pw_aff_scale_val(pa_i, v);
6491 pa = isl_pw_aff_add(pa, pa_i);
6494 for (i = 0; i < n_div; ++i) {
6495 isl_aff *div;
6496 isl_pw_aff *pa_i;
6498 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6499 continue;
6500 div = isl_aff_get_div(aff, i);
6501 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6502 isl_multi_pw_aff_copy(mpa), div);
6503 pa_i = isl_pw_aff_floor(pa_i);
6504 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6505 pa_i = isl_pw_aff_scale_val(pa_i, v);
6506 pa = isl_pw_aff_add(pa, pa_i);
6509 isl_multi_pw_aff_free(mpa);
6510 isl_aff_free(aff);
6512 return pa;
6515 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6516 * with the domain of "aff". The domain of the result is the same
6517 * as that of "mpa".
6519 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6520 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6522 if (!aff || !mpa)
6523 goto error;
6524 if (isl_space_match(aff->ls->dim, isl_dim_param,
6525 mpa->space, isl_dim_param))
6526 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6528 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6529 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6531 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6532 error:
6533 isl_aff_free(aff);
6534 isl_multi_pw_aff_free(mpa);
6535 return NULL;
6538 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6539 * with the domain of "pa". The domain of the result is the same
6540 * as that of "mpa".
6541 * "mpa" and "pa" are assumed to have been aligned.
6543 * We consider each piece in turn. Note that the domains of the
6544 * pieces are assumed to be disjoint and they remain disjoint
6545 * after taking the preimage (over the same function).
6547 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6548 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6550 isl_space *space;
6551 isl_pw_aff *res;
6552 int i;
6554 if (!mpa || !pa)
6555 goto error;
6557 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6558 isl_pw_aff_get_space(pa));
6559 res = isl_pw_aff_empty(space);
6561 for (i = 0; i < pa->n; ++i) {
6562 isl_pw_aff *pa_i;
6563 isl_set *domain;
6565 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6566 isl_multi_pw_aff_copy(mpa),
6567 isl_aff_copy(pa->p[i].aff));
6568 domain = isl_set_copy(pa->p[i].set);
6569 domain = isl_set_preimage_multi_pw_aff(domain,
6570 isl_multi_pw_aff_copy(mpa));
6571 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6572 res = isl_pw_aff_add_disjoint(res, pa_i);
6575 isl_pw_aff_free(pa);
6576 isl_multi_pw_aff_free(mpa);
6577 return res;
6578 error:
6579 isl_pw_aff_free(pa);
6580 isl_multi_pw_aff_free(mpa);
6581 return NULL;
6584 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6585 * with the domain of "pa". The domain of the result is the same
6586 * as that of "mpa".
6588 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6589 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6591 if (!pa || !mpa)
6592 goto error;
6593 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6594 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6596 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6597 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6599 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6600 error:
6601 isl_pw_aff_free(pa);
6602 isl_multi_pw_aff_free(mpa);
6603 return NULL;
6606 /* Compute the pullback of "pa" by the function represented by "mpa".
6607 * In other words, plug in "mpa" in "pa".
6608 * "pa" and "mpa" are assumed to have been aligned.
6610 * The pullback is computed by applying "pa" to "mpa".
6612 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6613 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6615 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6618 /* Compute the pullback of "pa" by the function represented by "mpa".
6619 * In other words, plug in "mpa" in "pa".
6621 * The pullback is computed by applying "pa" to "mpa".
6623 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6624 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6626 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6629 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6630 * In other words, plug in "mpa2" in "mpa1".
6632 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6634 * We pullback each member of "mpa1" in turn.
6636 static __isl_give isl_multi_pw_aff *
6637 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6638 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6640 int i;
6641 isl_space *space = NULL;
6643 mpa1 = isl_multi_pw_aff_cow(mpa1);
6644 if (!mpa1 || !mpa2)
6645 goto error;
6647 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6648 isl_multi_pw_aff_get_space(mpa1));
6650 for (i = 0; i < mpa1->n; ++i) {
6651 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6652 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6653 if (!mpa1->p[i])
6654 goto error;
6657 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6659 isl_multi_pw_aff_free(mpa2);
6660 return mpa1;
6661 error:
6662 isl_space_free(space);
6663 isl_multi_pw_aff_free(mpa1);
6664 isl_multi_pw_aff_free(mpa2);
6665 return NULL;
6668 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6669 * In other words, plug in "mpa2" in "mpa1".
6671 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6672 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6674 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6675 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6678 /* Compare two isl_affs.
6680 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6681 * than "aff2" and 0 if they are equal.
6683 * The order is fairly arbitrary. We do consider expressions that only involve
6684 * earlier dimensions as "smaller".
6686 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6688 int cmp;
6689 int last1, last2;
6691 if (aff1 == aff2)
6692 return 0;
6694 if (!aff1)
6695 return -1;
6696 if (!aff2)
6697 return 1;
6699 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6700 if (cmp != 0)
6701 return cmp;
6703 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6704 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6705 if (last1 != last2)
6706 return last1 - last2;
6708 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6711 /* Compare two isl_pw_affs.
6713 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6714 * than "pa2" and 0 if they are equal.
6716 * The order is fairly arbitrary. We do consider expressions that only involve
6717 * earlier dimensions as "smaller".
6719 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6720 __isl_keep isl_pw_aff *pa2)
6722 int i;
6723 int cmp;
6725 if (pa1 == pa2)
6726 return 0;
6728 if (!pa1)
6729 return -1;
6730 if (!pa2)
6731 return 1;
6733 cmp = isl_space_cmp(pa1->dim, pa2->dim);
6734 if (cmp != 0)
6735 return cmp;
6737 if (pa1->n != pa2->n)
6738 return pa1->n - pa2->n;
6740 for (i = 0; i < pa1->n; ++i) {
6741 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
6742 if (cmp != 0)
6743 return cmp;
6744 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
6745 if (cmp != 0)
6746 return cmp;
6749 return 0;
6752 /* Return a piecewise affine expression that is equal to "v" on "domain".
6754 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
6755 __isl_take isl_val *v)
6757 isl_space *space;
6758 isl_local_space *ls;
6759 isl_aff *aff;
6761 space = isl_set_get_space(domain);
6762 ls = isl_local_space_from_space(space);
6763 aff = isl_aff_val_on_domain(ls, v);
6765 return isl_pw_aff_alloc(domain, aff);
6768 /* Return a multi affine expression that is equal to "mv" on domain
6769 * space "space".
6771 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
6772 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
6774 int i, n;
6775 isl_space *space2;
6776 isl_local_space *ls;
6777 isl_multi_aff *ma;
6779 if (!space || !mv)
6780 goto error;
6782 n = isl_multi_val_dim(mv, isl_dim_set);
6783 space2 = isl_multi_val_get_space(mv);
6784 space2 = isl_space_align_params(space2, isl_space_copy(space));
6785 space = isl_space_align_params(space, isl_space_copy(space2));
6786 space = isl_space_map_from_domain_and_range(space, space2);
6787 ma = isl_multi_aff_alloc(isl_space_copy(space));
6788 ls = isl_local_space_from_space(isl_space_domain(space));
6789 for (i = 0; i < n; ++i) {
6790 isl_val *v;
6791 isl_aff *aff;
6793 v = isl_multi_val_get_val(mv, i);
6794 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
6795 ma = isl_multi_aff_set_aff(ma, i, aff);
6797 isl_local_space_free(ls);
6799 isl_multi_val_free(mv);
6800 return ma;
6801 error:
6802 isl_space_free(space);
6803 isl_multi_val_free(mv);
6804 return NULL;
6807 /* Return a piecewise multi-affine expression
6808 * that is equal to "mv" on "domain".
6810 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
6811 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
6813 isl_space *space;
6814 isl_multi_aff *ma;
6816 space = isl_set_get_space(domain);
6817 ma = isl_multi_aff_multi_val_on_space(space, mv);
6819 return isl_pw_multi_aff_alloc(domain, ma);
6822 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
6823 * mv is the value that should be attained on each domain set
6824 * res collects the results
6826 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
6827 isl_multi_val *mv;
6828 isl_union_pw_multi_aff *res;
6831 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
6832 * and add it to data->res.
6834 static int pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
6835 void *user)
6837 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
6838 isl_pw_multi_aff *pma;
6839 isl_multi_val *mv;
6841 mv = isl_multi_val_copy(data->mv);
6842 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
6843 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6845 return data->res ? 0 : -1;
6848 /* Return a union piecewise multi-affine expression
6849 * that is equal to "mv" on "domain".
6851 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
6852 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
6854 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
6855 isl_space *space;
6857 space = isl_union_set_get_space(domain);
6858 data.res = isl_union_pw_multi_aff_empty(space);
6859 data.mv = mv;
6860 if (isl_union_set_foreach_set(domain,
6861 &pw_multi_aff_multi_val_on_domain, &data) < 0)
6862 data.res = isl_union_pw_multi_aff_free(data.res);
6863 isl_union_set_free(domain);
6864 isl_multi_val_free(mv);
6865 return data.res;
6868 /* Compute the pullback of data->pma by the function represented by "pma2",
6869 * provided the spaces match, and add the results to data->res.
6871 static int pullback_entry(void **entry, void *user)
6873 struct isl_union_pw_multi_aff_bin_data *data = user;
6874 isl_pw_multi_aff *pma2 = *entry;
6876 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6877 pma2->dim, isl_dim_out))
6878 return 0;
6880 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
6881 isl_pw_multi_aff_copy(data->pma),
6882 isl_pw_multi_aff_copy(pma2));
6884 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6885 if (!data->res)
6886 return -1;
6888 return 0;
6891 /* Compute the pullback of "upma1" by the function represented by "upma2".
6893 __isl_give isl_union_pw_multi_aff *
6894 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
6895 __isl_take isl_union_pw_multi_aff *upma1,
6896 __isl_take isl_union_pw_multi_aff *upma2)
6898 return bin_op(upma1, upma2, &pullback_entry);
6901 /* Replace the entry of isl_union_pw_aff to which "entry" points
6902 * by its floor.
6904 static int floor_entry(void **entry, void *user)
6906 isl_pw_aff **pa = (isl_pw_aff **) entry;
6908 *pa = isl_pw_aff_floor(*pa);
6909 if (!*pa)
6910 return -1;
6912 return 0;
6915 /* Given f, return floor(f).
6917 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
6918 __isl_take isl_union_pw_aff *upa)
6920 isl_ctx *ctx;
6922 upa = isl_union_pw_aff_cow(upa);
6923 if (!upa)
6924 return NULL;
6926 ctx = isl_union_pw_aff_get_ctx(upa);
6927 if (isl_hash_table_foreach(ctx, &upa->table, &floor_entry, NULL) < 0)
6928 upa = isl_union_pw_aff_free(upa);
6930 return upa;
6933 /* Compute
6935 * upa mod m = upa - m * floor(upa/m)
6937 * with m an integer value.
6939 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
6940 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
6942 isl_union_pw_aff *res;
6944 if (!upa || !m)
6945 goto error;
6947 if (!isl_val_is_int(m))
6948 isl_die(isl_val_get_ctx(m), isl_error_invalid,
6949 "expecting integer modulo", goto error);
6950 if (!isl_val_is_pos(m))
6951 isl_die(isl_val_get_ctx(m), isl_error_invalid,
6952 "expecting positive modulo", goto error);
6954 res = isl_union_pw_aff_copy(upa);
6955 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
6956 upa = isl_union_pw_aff_floor(upa);
6957 upa = isl_union_pw_aff_scale_val(upa, m);
6958 res = isl_union_pw_aff_sub(res, upa);
6960 return res;
6961 error:
6962 isl_val_free(m);
6963 isl_union_pw_aff_free(upa);
6964 return NULL;
6967 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
6968 * "aff" is the symbolic value that the resulting isl_union_pw_aff
6969 * needs to attain.
6970 * "res" collects the results.
6972 struct isl_union_pw_aff_aff_on_domain_data {
6973 isl_aff *aff;
6974 isl_union_pw_aff *res;
6977 /* Construct a piecewise affine expression that is equal to data->aff
6978 * on "domain" and add the result to data->res.
6980 static int pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
6982 struct isl_union_pw_aff_aff_on_domain_data *data = user;
6983 isl_pw_aff *pa;
6984 isl_aff *aff;
6985 int dim;
6987 aff = isl_aff_copy(data->aff);
6988 dim = isl_set_dim(domain, isl_dim_set);
6989 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
6990 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
6991 pa = isl_pw_aff_alloc(domain, aff);
6992 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
6994 return data->res ? 0 : -1;
6997 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
6998 * pos is the output position that needs to be extracted.
6999 * res collects the results.
7001 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7002 int pos;
7003 isl_union_pw_aff *res;
7006 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7007 * (assuming it has such a dimension) and add it to data->res.
7009 static int get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7011 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7012 int n_out;
7013 isl_pw_aff *pa;
7015 if (!pma)
7016 return -1;
7018 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7019 if (data->pos >= n_out) {
7020 isl_pw_multi_aff_free(pma);
7021 return 0;
7024 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7025 isl_pw_multi_aff_free(pma);
7027 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7029 return data->res ? 0 : -1;
7032 /* Extract an isl_union_pw_aff corresponding to
7033 * output dimension "pos" of "upma".
7035 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7036 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7038 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7039 isl_space *space;
7041 if (!upma)
7042 return NULL;
7044 if (pos < 0)
7045 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7046 "cannot extract at negative position", return NULL);
7048 space = isl_union_pw_multi_aff_get_space(upma);
7049 data.res = isl_union_pw_aff_empty(space);
7050 data.pos = pos;
7051 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7052 &get_union_pw_aff, &data) < 0)
7053 data.res = isl_union_pw_aff_free(data.res);
7055 return data.res;
7058 /* Return a union piecewise affine expression
7059 * that is equal to "aff" on "domain".
7061 * Construct an isl_pw_aff on each of the sets in "domain" and
7062 * collect the results.
7064 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7065 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7067 struct isl_union_pw_aff_aff_on_domain_data data;
7068 isl_space *space;
7070 if (!domain || !aff)
7071 goto error;
7072 if (!isl_local_space_is_params(aff->ls))
7073 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7074 "expecting parametric expression", goto error);
7076 space = isl_union_set_get_space(domain);
7077 data.res = isl_union_pw_aff_empty(space);
7078 data.aff = aff;
7079 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7080 data.res = isl_union_pw_aff_free(data.res);
7081 isl_union_set_free(domain);
7082 isl_aff_free(aff);
7083 return data.res;
7084 error:
7085 isl_union_set_free(domain);
7086 isl_aff_free(aff);
7087 return NULL;
7090 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7091 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7092 * "res" collects the results.
7094 struct isl_union_pw_aff_val_on_domain_data {
7095 isl_val *v;
7096 isl_union_pw_aff *res;
7099 /* Construct a piecewise affine expression that is equal to data->v
7100 * on "domain" and add the result to data->res.
7102 static int pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7104 struct isl_union_pw_aff_val_on_domain_data *data = user;
7105 isl_pw_aff *pa;
7106 isl_val *v;
7108 v = isl_val_copy(data->v);
7109 pa = isl_pw_aff_val_on_domain(domain, v);
7110 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7112 return data->res ? 0 : -1;
7115 /* Return a union piecewise affine expression
7116 * that is equal to "v" on "domain".
7118 * Construct an isl_pw_aff on each of the sets in "domain" and
7119 * collect the results.
7121 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7122 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7124 struct isl_union_pw_aff_val_on_domain_data data;
7125 isl_space *space;
7127 space = isl_union_set_get_space(domain);
7128 data.res = isl_union_pw_aff_empty(space);
7129 data.v = v;
7130 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7131 data.res = isl_union_pw_aff_free(data.res);
7132 isl_union_set_free(domain);
7133 isl_val_free(v);
7134 return data.res;
7137 /* Construct a piecewise multi affine expression
7138 * that is equal to "pa" and add it to upma.
7140 static int pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7142 isl_union_pw_multi_aff **upma = user;
7143 isl_pw_multi_aff *pma;
7145 pma = isl_pw_multi_aff_from_pw_aff(pa);
7146 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7148 return *upma ? 0 : -1;
7151 /* Construct and return a union piecewise multi affine expression
7152 * that is equal to the given union piecewise affine expression.
7154 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7155 __isl_take isl_union_pw_aff *upa)
7157 isl_space *space;
7158 isl_union_pw_multi_aff *upma;
7160 if (!upa)
7161 return NULL;
7163 space = isl_union_pw_aff_get_space(upa);
7164 upma = isl_union_pw_multi_aff_empty(space);
7166 if (isl_union_pw_aff_foreach_pw_aff(upa,
7167 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7168 upma = isl_union_pw_multi_aff_free(upma);
7170 isl_union_pw_aff_free(upa);
7171 return upma;
7174 /* Compute the set of elements in the domain of "pa" where it is zero and
7175 * add this set to "uset".
7177 static int zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7179 isl_union_set **uset = (isl_union_set **)user;
7181 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7183 return *uset ? 0 : -1;
7186 /* Return a union set containing those elements in the domain
7187 * of "upa" where it is zero.
7189 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7190 __isl_take isl_union_pw_aff *upa)
7192 isl_union_set *zero;
7194 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7195 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7196 zero = isl_union_set_free(zero);
7198 isl_union_pw_aff_free(upa);
7199 return zero;
7202 /* Convert "pa" to an isl_map and add it to *umap.
7204 static int map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7206 isl_union_map **umap = user;
7207 isl_map *map;
7209 map = isl_map_from_pw_aff(pa);
7210 *umap = isl_union_map_add_map(*umap, map);
7212 return *umap ? 0 : -1;
7215 /* Construct a union map mapping the domain of the union
7216 * piecewise affine expression to its range, with the single output dimension
7217 * equated to the corresponding affine expressions on their cells.
7219 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7220 __isl_take isl_union_pw_aff *upa)
7222 isl_space *space;
7223 isl_union_map *umap;
7225 if (!upa)
7226 return NULL;
7228 space = isl_union_pw_aff_get_space(upa);
7229 umap = isl_union_map_empty(space);
7231 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7232 &umap) < 0)
7233 umap = isl_union_map_free(umap);
7235 isl_union_pw_aff_free(upa);
7236 return umap;
7239 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7240 * upma is the function that is plugged in.
7241 * pa is the current part of the function in which upma is plugged in.
7242 * res collects the results.
7244 struct isl_union_pw_aff_pullback_upma_data {
7245 isl_union_pw_multi_aff *upma;
7246 isl_pw_aff *pa;
7247 isl_union_pw_aff *res;
7250 /* Check if "pma" can be plugged into data->pa.
7251 * If so, perform the pullback and add the result to data->res.
7253 static int pa_pb_pma(void **entry, void *user)
7255 struct isl_union_pw_aff_pullback_upma_data *data = user;
7256 isl_pw_multi_aff *pma = *entry;
7257 isl_pw_aff *pa;
7259 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7260 pma->dim, isl_dim_out))
7261 return 0;
7263 pma = isl_pw_multi_aff_copy(pma);
7264 pa = isl_pw_aff_copy(data->pa);
7265 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7267 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7269 return data->res ? 0 : -1;
7272 /* Check if any of the elements of data->upma can be plugged into pa,
7273 * add if so add the result to data->res.
7275 static int upa_pb_upma(void **entry, void *user)
7277 struct isl_union_pw_aff_pullback_upma_data *data = user;
7278 isl_ctx *ctx;
7279 isl_pw_aff *pa = *entry;
7281 data->pa = pa;
7282 ctx = isl_union_pw_multi_aff_get_ctx(data->upma);
7283 if (isl_hash_table_foreach(ctx, &data->upma->table,
7284 &pa_pb_pma, data) < 0)
7285 return -1;
7287 return 0;
7290 /* Compute the pullback of "upa" by the function represented by "upma".
7291 * In other words, plug in "upma" in "upa". The result contains
7292 * expressions defined over the domain space of "upma".
7294 * Run over all pairs of elements in "upa" and "upma", perform
7295 * the pullback when appropriate and collect the results.
7296 * If the hash value were based on the domain space rather than
7297 * the function space, then we could run through all elements
7298 * of "upma" and directly pick out the corresponding element of "upa".
7300 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7301 __isl_take isl_union_pw_aff *upa,
7302 __isl_take isl_union_pw_multi_aff *upma)
7304 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7305 isl_ctx *ctx;
7306 isl_space *space;
7308 space = isl_union_pw_multi_aff_get_space(upma);
7309 upa = isl_union_pw_aff_align_params(upa, space);
7310 space = isl_union_pw_aff_get_space(upa);
7311 upma = isl_union_pw_multi_aff_align_params(upma, space);
7313 if (!upa || !upma)
7314 goto error;
7316 ctx = isl_union_pw_aff_get_ctx(upa);
7317 data.upma = upma;
7318 space = isl_union_pw_aff_get_space(upa);
7319 data.res = isl_union_pw_aff_alloc(space, upa->table.n);
7320 if (isl_hash_table_foreach(ctx, &upa->table, &upa_pb_upma, &data) < 0)
7321 data.res = isl_union_pw_aff_free(data.res);
7323 isl_union_pw_aff_free(upa);
7324 isl_union_pw_multi_aff_free(upma);
7325 return data.res;
7326 error:
7327 isl_union_pw_aff_free(upa);
7328 isl_union_pw_multi_aff_free(upma);
7329 return NULL;