extract out isl_union_eval.c
[isl.git] / isl_aff.c
blobe252a21393b5f5f850a822a9c43f24553fa4b996
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
230 if (!aff)
231 return NULL;
233 aff->ref++;
234 return aff;
237 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
239 if (!aff)
240 return NULL;
242 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
243 isl_vec_copy(aff->v));
246 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
248 if (!aff)
249 return NULL;
251 if (aff->ref == 1)
252 return aff;
253 aff->ref--;
254 return isl_aff_dup(aff);
257 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
259 if (!aff)
260 return NULL;
262 if (--aff->ref > 0)
263 return NULL;
265 isl_local_space_free(aff->ls);
266 isl_vec_free(aff->v);
268 free(aff);
270 return NULL;
273 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
275 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
278 /* Externally, an isl_aff has a map space, but internally, the
279 * ls field corresponds to the domain of that space.
281 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
283 if (!aff)
284 return 0;
285 if (type == isl_dim_out)
286 return 1;
287 if (type == isl_dim_in)
288 type = isl_dim_set;
289 return isl_local_space_dim(aff->ls, type);
292 /* Return the position of the dimension of the given type and name
293 * in "aff".
294 * Return -1 if no such dimension can be found.
296 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
297 const char *name)
299 if (!aff)
300 return -1;
301 if (type == isl_dim_out)
302 return -1;
303 if (type == isl_dim_in)
304 type = isl_dim_set;
305 return isl_local_space_find_dim_by_name(aff->ls, type, name);
308 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
310 return aff ? isl_local_space_get_space(aff->ls) : NULL;
313 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
315 isl_space *space;
316 if (!aff)
317 return NULL;
318 space = isl_local_space_get_space(aff->ls);
319 space = isl_space_from_domain(space);
320 space = isl_space_add_dims(space, isl_dim_out, 1);
321 return space;
324 __isl_give isl_local_space *isl_aff_get_domain_local_space(
325 __isl_keep isl_aff *aff)
327 return aff ? isl_local_space_copy(aff->ls) : NULL;
330 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
332 isl_local_space *ls;
333 if (!aff)
334 return NULL;
335 ls = isl_local_space_copy(aff->ls);
336 ls = isl_local_space_from_domain(ls);
337 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
338 return ls;
341 /* Externally, an isl_aff has a map space, but internally, the
342 * ls field corresponds to the domain of that space.
344 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
345 enum isl_dim_type type, unsigned pos)
347 if (!aff)
348 return NULL;
349 if (type == isl_dim_out)
350 return NULL;
351 if (type == isl_dim_in)
352 type = isl_dim_set;
353 return isl_local_space_get_dim_name(aff->ls, type, pos);
356 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
357 __isl_take isl_space *dim)
359 aff = isl_aff_cow(aff);
360 if (!aff || !dim)
361 goto error;
363 aff->ls = isl_local_space_reset_space(aff->ls, dim);
364 if (!aff->ls)
365 return isl_aff_free(aff);
367 return aff;
368 error:
369 isl_aff_free(aff);
370 isl_space_free(dim);
371 return NULL;
374 /* Reset the space of "aff". This function is called from isl_pw_templ.c
375 * and doesn't know if the space of an element object is represented
376 * directly or through its domain. It therefore passes along both.
378 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
379 __isl_take isl_space *space, __isl_take isl_space *domain)
381 isl_space_free(space);
382 return isl_aff_reset_domain_space(aff, domain);
385 /* Reorder the coefficients of the affine expression based
386 * on the given reodering.
387 * The reordering r is assumed to have been extended with the local
388 * variables.
390 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
391 __isl_take isl_reordering *r, int n_div)
393 isl_vec *res;
394 int i;
396 if (!vec || !r)
397 goto error;
399 res = isl_vec_alloc(vec->ctx,
400 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
401 isl_seq_cpy(res->el, vec->el, 2);
402 isl_seq_clr(res->el + 2, res->size - 2);
403 for (i = 0; i < r->len; ++i)
404 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
406 isl_reordering_free(r);
407 isl_vec_free(vec);
408 return res;
409 error:
410 isl_vec_free(vec);
411 isl_reordering_free(r);
412 return NULL;
415 /* Reorder the dimensions of the domain of "aff" according
416 * to the given reordering.
418 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
419 __isl_take isl_reordering *r)
421 aff = isl_aff_cow(aff);
422 if (!aff)
423 goto error;
425 r = isl_reordering_extend(r, aff->ls->div->n_row);
426 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
427 aff->ls->div->n_row);
428 aff->ls = isl_local_space_realign(aff->ls, r);
430 if (!aff->v || !aff->ls)
431 return isl_aff_free(aff);
433 return aff;
434 error:
435 isl_aff_free(aff);
436 isl_reordering_free(r);
437 return NULL;
440 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
441 __isl_take isl_space *model)
443 if (!aff || !model)
444 goto error;
446 if (!isl_space_match(aff->ls->dim, isl_dim_param,
447 model, isl_dim_param)) {
448 isl_reordering *exp;
450 model = isl_space_drop_dims(model, isl_dim_in,
451 0, isl_space_dim(model, isl_dim_in));
452 model = isl_space_drop_dims(model, isl_dim_out,
453 0, isl_space_dim(model, isl_dim_out));
454 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
455 exp = isl_reordering_extend_space(exp,
456 isl_aff_get_domain_space(aff));
457 aff = isl_aff_realign_domain(aff, exp);
460 isl_space_free(model);
461 return aff;
462 error:
463 isl_space_free(model);
464 isl_aff_free(aff);
465 return NULL;
468 /* Is "aff" obviously equal to zero?
470 * If the denominator is zero, then "aff" is not equal to zero.
472 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
474 if (!aff)
475 return isl_bool_error;
477 if (isl_int_is_zero(aff->v->el[0]))
478 return isl_bool_false;
479 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
482 /* Does "aff" represent NaN?
484 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
486 if (!aff)
487 return isl_bool_error;
489 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
492 /* Does "pa" involve any NaNs?
494 isl_bool isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
496 int i;
498 if (!pa)
499 return isl_bool_error;
500 if (pa->n == 0)
501 return isl_bool_false;
503 for (i = 0; i < pa->n; ++i) {
504 isl_bool is_nan = isl_aff_is_nan(pa->p[i].aff);
505 if (is_nan < 0 || is_nan)
506 return is_nan;
509 return isl_bool_false;
512 /* Are "aff1" and "aff2" obviously equal?
514 * NaN is not equal to anything, not even to another NaN.
516 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
517 __isl_keep isl_aff *aff2)
519 isl_bool equal;
521 if (!aff1 || !aff2)
522 return isl_bool_error;
524 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
525 return isl_bool_false;
527 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
528 if (equal < 0 || !equal)
529 return equal;
531 return isl_vec_is_equal(aff1->v, aff2->v);
534 /* Return the common denominator of "aff" in "v".
536 * We cannot return anything meaningful in case of a NaN.
538 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
540 if (!aff)
541 return -1;
542 if (isl_aff_is_nan(aff))
543 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
544 "cannot get denominator of NaN", return -1);
545 isl_int_set(*v, aff->v->el[0]);
546 return 0;
549 /* Return the common denominator of "aff".
551 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
553 isl_ctx *ctx;
555 if (!aff)
556 return NULL;
558 ctx = isl_aff_get_ctx(aff);
559 if (isl_aff_is_nan(aff))
560 return isl_val_nan(ctx);
561 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
564 /* Return the constant term of "aff" in "v".
566 * We cannot return anything meaningful in case of a NaN.
568 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
570 if (!aff)
571 return -1;
572 if (isl_aff_is_nan(aff))
573 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
574 "cannot get constant term of NaN", return -1);
575 isl_int_set(*v, aff->v->el[1]);
576 return 0;
579 /* Return the constant term of "aff".
581 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
583 isl_ctx *ctx;
584 isl_val *v;
586 if (!aff)
587 return NULL;
589 ctx = isl_aff_get_ctx(aff);
590 if (isl_aff_is_nan(aff))
591 return isl_val_nan(ctx);
592 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
593 return isl_val_normalize(v);
596 /* Return the coefficient of the variable of type "type" at position "pos"
597 * of "aff" in "v".
599 * We cannot return anything meaningful in case of a NaN.
601 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
602 enum isl_dim_type type, int pos, isl_int *v)
604 if (!aff)
605 return -1;
607 if (type == isl_dim_out)
608 isl_die(aff->v->ctx, isl_error_invalid,
609 "output/set dimension does not have a coefficient",
610 return -1);
611 if (type == isl_dim_in)
612 type = isl_dim_set;
614 if (pos >= isl_local_space_dim(aff->ls, type))
615 isl_die(aff->v->ctx, isl_error_invalid,
616 "position out of bounds", return -1);
618 if (isl_aff_is_nan(aff))
619 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
620 "cannot get coefficient of NaN", return -1);
621 pos += isl_local_space_offset(aff->ls, type);
622 isl_int_set(*v, aff->v->el[1 + pos]);
624 return 0;
627 /* Return the coefficient of the variable of type "type" at position "pos"
628 * of "aff".
630 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
631 enum isl_dim_type type, int pos)
633 isl_ctx *ctx;
634 isl_val *v;
636 if (!aff)
637 return NULL;
639 ctx = isl_aff_get_ctx(aff);
640 if (type == isl_dim_out)
641 isl_die(ctx, isl_error_invalid,
642 "output/set dimension does not have a coefficient",
643 return NULL);
644 if (type == isl_dim_in)
645 type = isl_dim_set;
647 if (pos >= isl_local_space_dim(aff->ls, type))
648 isl_die(ctx, isl_error_invalid,
649 "position out of bounds", return NULL);
651 if (isl_aff_is_nan(aff))
652 return isl_val_nan(ctx);
653 pos += isl_local_space_offset(aff->ls, type);
654 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
655 return isl_val_normalize(v);
658 /* Return the sign of the coefficient of the variable of type "type"
659 * at position "pos" of "aff".
661 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
662 int pos)
664 isl_ctx *ctx;
666 if (!aff)
667 return 0;
669 ctx = isl_aff_get_ctx(aff);
670 if (type == isl_dim_out)
671 isl_die(ctx, isl_error_invalid,
672 "output/set dimension does not have a coefficient",
673 return 0);
674 if (type == isl_dim_in)
675 type = isl_dim_set;
677 if (pos >= isl_local_space_dim(aff->ls, type))
678 isl_die(ctx, isl_error_invalid,
679 "position out of bounds", return 0);
681 pos += isl_local_space_offset(aff->ls, type);
682 return isl_int_sgn(aff->v->el[1 + pos]);
685 /* Replace the denominator of "aff" by "v".
687 * A NaN is unaffected by this operation.
689 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
691 if (!aff)
692 return NULL;
693 if (isl_aff_is_nan(aff))
694 return aff;
695 aff = isl_aff_cow(aff);
696 if (!aff)
697 return NULL;
699 aff->v = isl_vec_cow(aff->v);
700 if (!aff->v)
701 return isl_aff_free(aff);
703 isl_int_set(aff->v->el[0], v);
705 return aff;
708 /* Replace the numerator of the constant term of "aff" by "v".
710 * A NaN is unaffected by this operation.
712 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
714 if (!aff)
715 return NULL;
716 if (isl_aff_is_nan(aff))
717 return aff;
718 aff = isl_aff_cow(aff);
719 if (!aff)
720 return NULL;
722 aff->v = isl_vec_cow(aff->v);
723 if (!aff->v)
724 return isl_aff_free(aff);
726 isl_int_set(aff->v->el[1], v);
728 return aff;
731 /* Replace the constant term of "aff" by "v".
733 * A NaN is unaffected by this operation.
735 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
736 __isl_take isl_val *v)
738 if (!aff || !v)
739 goto error;
741 if (isl_aff_is_nan(aff)) {
742 isl_val_free(v);
743 return aff;
746 if (!isl_val_is_rat(v))
747 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
748 "expecting rational value", goto error);
750 if (isl_int_eq(aff->v->el[1], v->n) &&
751 isl_int_eq(aff->v->el[0], v->d)) {
752 isl_val_free(v);
753 return aff;
756 aff = isl_aff_cow(aff);
757 if (!aff)
758 goto error;
759 aff->v = isl_vec_cow(aff->v);
760 if (!aff->v)
761 goto error;
763 if (isl_int_eq(aff->v->el[0], v->d)) {
764 isl_int_set(aff->v->el[1], v->n);
765 } else if (isl_int_is_one(v->d)) {
766 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
767 } else {
768 isl_seq_scale(aff->v->el + 1,
769 aff->v->el + 1, v->d, aff->v->size - 1);
770 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
771 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
772 aff->v = isl_vec_normalize(aff->v);
773 if (!aff->v)
774 goto error;
777 isl_val_free(v);
778 return aff;
779 error:
780 isl_aff_free(aff);
781 isl_val_free(v);
782 return NULL;
785 /* Add "v" to the constant term of "aff".
787 * A NaN is unaffected by this operation.
789 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
791 if (isl_int_is_zero(v))
792 return aff;
794 if (!aff)
795 return NULL;
796 if (isl_aff_is_nan(aff))
797 return aff;
798 aff = isl_aff_cow(aff);
799 if (!aff)
800 return NULL;
802 aff->v = isl_vec_cow(aff->v);
803 if (!aff->v)
804 return isl_aff_free(aff);
806 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
808 return aff;
811 /* Add "v" to the constant term of "aff".
813 * A NaN is unaffected by this operation.
815 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
816 __isl_take isl_val *v)
818 if (!aff || !v)
819 goto error;
821 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
822 isl_val_free(v);
823 return aff;
826 if (!isl_val_is_rat(v))
827 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
828 "expecting rational value", goto error);
830 aff = isl_aff_cow(aff);
831 if (!aff)
832 goto error;
834 aff->v = isl_vec_cow(aff->v);
835 if (!aff->v)
836 goto error;
838 if (isl_int_is_one(v->d)) {
839 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
840 } else if (isl_int_eq(aff->v->el[0], v->d)) {
841 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
842 aff->v = isl_vec_normalize(aff->v);
843 if (!aff->v)
844 goto error;
845 } else {
846 isl_seq_scale(aff->v->el + 1,
847 aff->v->el + 1, v->d, aff->v->size - 1);
848 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
849 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
850 aff->v = isl_vec_normalize(aff->v);
851 if (!aff->v)
852 goto error;
855 isl_val_free(v);
856 return aff;
857 error:
858 isl_aff_free(aff);
859 isl_val_free(v);
860 return NULL;
863 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
865 isl_int t;
867 isl_int_init(t);
868 isl_int_set_si(t, v);
869 aff = isl_aff_add_constant(aff, t);
870 isl_int_clear(t);
872 return aff;
875 /* Add "v" to the numerator of the constant term of "aff".
877 * A NaN is unaffected by this operation.
879 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
881 if (isl_int_is_zero(v))
882 return aff;
884 if (!aff)
885 return NULL;
886 if (isl_aff_is_nan(aff))
887 return aff;
888 aff = isl_aff_cow(aff);
889 if (!aff)
890 return NULL;
892 aff->v = isl_vec_cow(aff->v);
893 if (!aff->v)
894 return isl_aff_free(aff);
896 isl_int_add(aff->v->el[1], aff->v->el[1], v);
898 return aff;
901 /* Add "v" to the numerator of the constant term of "aff".
903 * A NaN is unaffected by this operation.
905 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
907 isl_int t;
909 if (v == 0)
910 return aff;
912 isl_int_init(t);
913 isl_int_set_si(t, v);
914 aff = isl_aff_add_constant_num(aff, t);
915 isl_int_clear(t);
917 return aff;
920 /* Replace the numerator of the constant term of "aff" by "v".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
926 if (!aff)
927 return NULL;
928 if (isl_aff_is_nan(aff))
929 return aff;
930 aff = isl_aff_cow(aff);
931 if (!aff)
932 return NULL;
934 aff->v = isl_vec_cow(aff->v);
935 if (!aff->v)
936 return isl_aff_free(aff);
938 isl_int_set_si(aff->v->el[1], v);
940 return aff;
943 /* Replace the numerator of the coefficient of the variable of type "type"
944 * at position "pos" of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
949 enum isl_dim_type type, int pos, isl_int v)
951 if (!aff)
952 return NULL;
954 if (type == isl_dim_out)
955 isl_die(aff->v->ctx, isl_error_invalid,
956 "output/set dimension does not have a coefficient",
957 return isl_aff_free(aff));
958 if (type == isl_dim_in)
959 type = isl_dim_set;
961 if (pos >= isl_local_space_dim(aff->ls, type))
962 isl_die(aff->v->ctx, isl_error_invalid,
963 "position out of bounds", return isl_aff_free(aff));
965 if (isl_aff_is_nan(aff))
966 return aff;
967 aff = isl_aff_cow(aff);
968 if (!aff)
969 return NULL;
971 aff->v = isl_vec_cow(aff->v);
972 if (!aff->v)
973 return isl_aff_free(aff);
975 pos += isl_local_space_offset(aff->ls, type);
976 isl_int_set(aff->v->el[1 + pos], v);
978 return aff;
981 /* Replace the numerator of the coefficient of the variable of type "type"
982 * at position "pos" of "aff" by "v".
984 * A NaN is unaffected by this operation.
986 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
987 enum isl_dim_type type, int pos, int v)
989 if (!aff)
990 return NULL;
992 if (type == isl_dim_out)
993 isl_die(aff->v->ctx, isl_error_invalid,
994 "output/set dimension does not have a coefficient",
995 return isl_aff_free(aff));
996 if (type == isl_dim_in)
997 type = isl_dim_set;
999 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1000 isl_die(aff->v->ctx, isl_error_invalid,
1001 "position out of bounds", return isl_aff_free(aff));
1003 if (isl_aff_is_nan(aff))
1004 return aff;
1005 pos += isl_local_space_offset(aff->ls, type);
1006 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1007 return aff;
1009 aff = isl_aff_cow(aff);
1010 if (!aff)
1011 return NULL;
1013 aff->v = isl_vec_cow(aff->v);
1014 if (!aff->v)
1015 return isl_aff_free(aff);
1017 isl_int_set_si(aff->v->el[1 + pos], v);
1019 return aff;
1022 /* Replace the coefficient of the variable of type "type" at position "pos"
1023 * of "aff" by "v".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1028 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1030 if (!aff || !v)
1031 goto error;
1033 if (type == isl_dim_out)
1034 isl_die(aff->v->ctx, isl_error_invalid,
1035 "output/set dimension does not have a coefficient",
1036 goto error);
1037 if (type == isl_dim_in)
1038 type = isl_dim_set;
1040 if (pos >= isl_local_space_dim(aff->ls, type))
1041 isl_die(aff->v->ctx, isl_error_invalid,
1042 "position out of bounds", goto error);
1044 if (isl_aff_is_nan(aff)) {
1045 isl_val_free(v);
1046 return aff;
1048 if (!isl_val_is_rat(v))
1049 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1050 "expecting rational value", goto error);
1052 pos += isl_local_space_offset(aff->ls, type);
1053 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1054 isl_int_eq(aff->v->el[0], v->d)) {
1055 isl_val_free(v);
1056 return aff;
1059 aff = isl_aff_cow(aff);
1060 if (!aff)
1061 goto error;
1062 aff->v = isl_vec_cow(aff->v);
1063 if (!aff->v)
1064 goto error;
1066 if (isl_int_eq(aff->v->el[0], v->d)) {
1067 isl_int_set(aff->v->el[1 + pos], v->n);
1068 } else if (isl_int_is_one(v->d)) {
1069 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1070 } else {
1071 isl_seq_scale(aff->v->el + 1,
1072 aff->v->el + 1, v->d, aff->v->size - 1);
1073 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1074 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1075 aff->v = isl_vec_normalize(aff->v);
1076 if (!aff->v)
1077 goto error;
1080 isl_val_free(v);
1081 return aff;
1082 error:
1083 isl_aff_free(aff);
1084 isl_val_free(v);
1085 return NULL;
1088 /* Add "v" to the coefficient of the variable of type "type"
1089 * at position "pos" of "aff".
1091 * A NaN is unaffected by this operation.
1093 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1094 enum isl_dim_type type, int pos, isl_int v)
1096 if (!aff)
1097 return NULL;
1099 if (type == isl_dim_out)
1100 isl_die(aff->v->ctx, isl_error_invalid,
1101 "output/set dimension does not have a coefficient",
1102 return isl_aff_free(aff));
1103 if (type == isl_dim_in)
1104 type = isl_dim_set;
1106 if (pos >= isl_local_space_dim(aff->ls, type))
1107 isl_die(aff->v->ctx, isl_error_invalid,
1108 "position out of bounds", return isl_aff_free(aff));
1110 if (isl_aff_is_nan(aff))
1111 return aff;
1112 aff = isl_aff_cow(aff);
1113 if (!aff)
1114 return NULL;
1116 aff->v = isl_vec_cow(aff->v);
1117 if (!aff->v)
1118 return isl_aff_free(aff);
1120 pos += isl_local_space_offset(aff->ls, type);
1121 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1123 return aff;
1126 /* Add "v" to the coefficient of the variable of type "type"
1127 * at position "pos" of "aff".
1129 * A NaN is unaffected by this operation.
1131 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1132 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1134 if (!aff || !v)
1135 goto error;
1137 if (isl_val_is_zero(v)) {
1138 isl_val_free(v);
1139 return aff;
1142 if (type == isl_dim_out)
1143 isl_die(aff->v->ctx, isl_error_invalid,
1144 "output/set dimension does not have a coefficient",
1145 goto error);
1146 if (type == isl_dim_in)
1147 type = isl_dim_set;
1149 if (pos >= isl_local_space_dim(aff->ls, type))
1150 isl_die(aff->v->ctx, isl_error_invalid,
1151 "position out of bounds", goto error);
1153 if (isl_aff_is_nan(aff)) {
1154 isl_val_free(v);
1155 return aff;
1157 if (!isl_val_is_rat(v))
1158 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1159 "expecting rational value", goto error);
1161 aff = isl_aff_cow(aff);
1162 if (!aff)
1163 goto error;
1165 aff->v = isl_vec_cow(aff->v);
1166 if (!aff->v)
1167 goto error;
1169 pos += isl_local_space_offset(aff->ls, type);
1170 if (isl_int_is_one(v->d)) {
1171 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1172 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1173 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1174 aff->v = isl_vec_normalize(aff->v);
1175 if (!aff->v)
1176 goto error;
1177 } else {
1178 isl_seq_scale(aff->v->el + 1,
1179 aff->v->el + 1, v->d, aff->v->size - 1);
1180 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1181 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1182 aff->v = isl_vec_normalize(aff->v);
1183 if (!aff->v)
1184 goto error;
1187 isl_val_free(v);
1188 return aff;
1189 error:
1190 isl_aff_free(aff);
1191 isl_val_free(v);
1192 return NULL;
1195 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1196 enum isl_dim_type type, int pos, int v)
1198 isl_int t;
1200 isl_int_init(t);
1201 isl_int_set_si(t, v);
1202 aff = isl_aff_add_coefficient(aff, type, pos, t);
1203 isl_int_clear(t);
1205 return aff;
1208 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1210 if (!aff)
1211 return NULL;
1213 return isl_local_space_get_div(aff->ls, pos);
1216 /* Return the negation of "aff".
1218 * As a special case, -NaN = NaN.
1220 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1222 if (!aff)
1223 return NULL;
1224 if (isl_aff_is_nan(aff))
1225 return aff;
1226 aff = isl_aff_cow(aff);
1227 if (!aff)
1228 return NULL;
1229 aff->v = isl_vec_cow(aff->v);
1230 if (!aff->v)
1231 return isl_aff_free(aff);
1233 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1235 return aff;
1238 /* Remove divs from the local space that do not appear in the affine
1239 * expression.
1240 * We currently only remove divs at the end.
1241 * Some intermediate divs may also not appear directly in the affine
1242 * expression, but we would also need to check that no other divs are
1243 * defined in terms of them.
1245 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
1247 int pos;
1248 int off;
1249 int n;
1251 if (!aff)
1252 return NULL;
1254 n = isl_local_space_dim(aff->ls, isl_dim_div);
1255 off = isl_local_space_offset(aff->ls, isl_dim_div);
1257 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1258 if (pos == n)
1259 return aff;
1261 aff = isl_aff_cow(aff);
1262 if (!aff)
1263 return NULL;
1265 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1266 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1267 if (!aff->ls || !aff->v)
1268 return isl_aff_free(aff);
1270 return aff;
1273 /* Given two affine expressions "p" of length p_len (including the
1274 * denominator and the constant term) and "subs" of length subs_len,
1275 * plug in "subs" for the variable at position "pos".
1276 * The variables of "subs" and "p" are assumed to match up to subs_len,
1277 * but "p" may have additional variables.
1278 * "v" is an initialized isl_int that can be used internally.
1280 * In particular, if "p" represents the expression
1282 * (a i + g)/m
1284 * with i the variable at position "pos" and "subs" represents the expression
1286 * f/d
1288 * then the result represents the expression
1290 * (a f + d g)/(m d)
1293 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1294 int p_len, int subs_len, isl_int v)
1296 isl_int_set(v, p[1 + pos]);
1297 isl_int_set_si(p[1 + pos], 0);
1298 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1299 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1300 isl_int_mul(p[0], p[0], subs[0]);
1303 /* Look for any divs in the aff->ls with a denominator equal to one
1304 * and plug them into the affine expression and any subsequent divs
1305 * that may reference the div.
1307 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1309 int i, n;
1310 int len;
1311 isl_int v;
1312 isl_vec *vec;
1313 isl_local_space *ls;
1314 unsigned pos;
1316 if (!aff)
1317 return NULL;
1319 n = isl_local_space_dim(aff->ls, isl_dim_div);
1320 len = aff->v->size;
1321 for (i = 0; i < n; ++i) {
1322 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1323 continue;
1324 ls = isl_local_space_copy(aff->ls);
1325 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1326 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1327 vec = isl_vec_copy(aff->v);
1328 vec = isl_vec_cow(vec);
1329 if (!ls || !vec)
1330 goto error;
1332 isl_int_init(v);
1334 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1335 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1336 len, len, v);
1338 isl_int_clear(v);
1340 isl_vec_free(aff->v);
1341 aff->v = vec;
1342 isl_local_space_free(aff->ls);
1343 aff->ls = ls;
1346 return aff;
1347 error:
1348 isl_vec_free(vec);
1349 isl_local_space_free(ls);
1350 return isl_aff_free(aff);
1353 /* Look for any divs j that appear with a unit coefficient inside
1354 * the definitions of other divs i and plug them into the definitions
1355 * of the divs i.
1357 * In particular, an expression of the form
1359 * floor((f(..) + floor(g(..)/n))/m)
1361 * is simplified to
1363 * floor((n * f(..) + g(..))/(n * m))
1365 * This simplification is correct because we can move the expression
1366 * f(..) into the inner floor in the original expression to obtain
1368 * floor(floor((n * f(..) + g(..))/n)/m)
1370 * from which we can derive the simplified expression.
1372 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1374 int i, j, n;
1375 int off;
1377 if (!aff)
1378 return NULL;
1380 n = isl_local_space_dim(aff->ls, isl_dim_div);
1381 off = isl_local_space_offset(aff->ls, isl_dim_div);
1382 for (i = 1; i < n; ++i) {
1383 for (j = 0; j < i; ++j) {
1384 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1385 continue;
1386 aff->ls = isl_local_space_substitute_seq(aff->ls,
1387 isl_dim_div, j, aff->ls->div->row[j],
1388 aff->v->size, i, 1);
1389 if (!aff->ls)
1390 return isl_aff_free(aff);
1394 return aff;
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1404 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1405 isl_local_space *ls;
1406 isl_vec *v;
1408 ls = isl_local_space_copy(aff->ls);
1409 ls = isl_local_space_swap_div(ls, a, b);
1410 v = isl_vec_copy(aff->v);
1411 v = isl_vec_cow(v);
1412 if (!ls || !v)
1413 goto error;
1415 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1416 isl_vec_free(aff->v);
1417 aff->v = v;
1418 isl_local_space_free(aff->ls);
1419 aff->ls = ls;
1421 return aff;
1422 error:
1423 isl_vec_free(v);
1424 isl_local_space_free(ls);
1425 return isl_aff_free(aff);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1435 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1437 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1438 return aff;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1 + off + a],
1445 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1446 isl_int_set_si(aff->v->el[1 + off + b], 0);
1448 return aff;
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1462 int i, j, n;
1464 if (!aff)
1465 return NULL;
1467 n = isl_aff_dim(aff, isl_dim_div);
1468 for (i = 1; i < n; ++i) {
1469 for (j = i - 1; j >= 0; --j) {
1470 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1471 if (cmp < 0)
1472 break;
1473 if (cmp == 0)
1474 aff = merge_divs(aff, j, j + 1);
1475 else
1476 aff = swap_div(aff, j, j + 1);
1477 if (!aff)
1478 return NULL;
1482 return aff;
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1493 if (!aff)
1494 return NULL;
1495 aff->v = isl_vec_normalize(aff->v);
1496 if (!aff->v)
1497 return isl_aff_free(aff);
1498 aff = plug_in_integral_divs(aff);
1499 aff = plug_in_unit_divs(aff);
1500 aff = sort_divs(aff);
1501 aff = isl_aff_remove_unused_divs(aff);
1502 return aff;
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * As a special case, floor(NaN) = NaN.
1514 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1516 int i;
1517 int size;
1518 isl_ctx *ctx;
1519 isl_vec *div;
1521 if (!aff)
1522 return NULL;
1524 if (isl_aff_is_nan(aff))
1525 return aff;
1526 if (isl_int_is_one(aff->v->el[0]))
1527 return aff;
1529 aff = isl_aff_cow(aff);
1530 if (!aff)
1531 return NULL;
1533 aff->v = isl_vec_cow(aff->v);
1534 if (!aff->v)
1535 return isl_aff_free(aff);
1537 if (isl_aff_is_cst(aff)) {
1538 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1539 isl_int_set_si(aff->v->el[0], 1);
1540 return aff;
1543 div = isl_vec_copy(aff->v);
1544 div = isl_vec_cow(div);
1545 if (!div)
1546 return isl_aff_free(aff);
1548 ctx = isl_aff_get_ctx(aff);
1549 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1550 for (i = 1; i < aff->v->size; ++i) {
1551 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1552 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1553 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1554 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1555 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1559 aff->ls = isl_local_space_add_div(aff->ls, div);
1560 if (!aff->ls)
1561 return isl_aff_free(aff);
1563 size = aff->v->size;
1564 aff->v = isl_vec_extend(aff->v, size + 1);
1565 if (!aff->v)
1566 return isl_aff_free(aff);
1567 isl_int_set_si(aff->v->el[0], 1);
1568 isl_int_set_si(aff->v->el[size], 1);
1570 aff = isl_aff_normalize(aff);
1572 return aff;
1575 /* Compute
1577 * aff mod m = aff - m * floor(aff/m)
1579 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1581 isl_aff *res;
1583 res = isl_aff_copy(aff);
1584 aff = isl_aff_scale_down(aff, m);
1585 aff = isl_aff_floor(aff);
1586 aff = isl_aff_scale(aff, m);
1587 res = isl_aff_sub(res, aff);
1589 return res;
1592 /* Compute
1594 * aff mod m = aff - m * floor(aff/m)
1596 * with m an integer value.
1598 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1599 __isl_take isl_val *m)
1601 isl_aff *res;
1603 if (!aff || !m)
1604 goto error;
1606 if (!isl_val_is_int(m))
1607 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1608 "expecting integer modulo", goto error);
1610 res = isl_aff_copy(aff);
1611 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1612 aff = isl_aff_floor(aff);
1613 aff = isl_aff_scale_val(aff, m);
1614 res = isl_aff_sub(res, aff);
1616 return res;
1617 error:
1618 isl_aff_free(aff);
1619 isl_val_free(m);
1620 return NULL;
1623 /* Compute
1625 * pwaff mod m = pwaff - m * floor(pwaff/m)
1627 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1629 isl_pw_aff *res;
1631 res = isl_pw_aff_copy(pwaff);
1632 pwaff = isl_pw_aff_scale_down(pwaff, m);
1633 pwaff = isl_pw_aff_floor(pwaff);
1634 pwaff = isl_pw_aff_scale(pwaff, m);
1635 res = isl_pw_aff_sub(res, pwaff);
1637 return res;
1640 /* Compute
1642 * pa mod m = pa - m * floor(pa/m)
1644 * with m an integer value.
1646 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1647 __isl_take isl_val *m)
1649 if (!pa || !m)
1650 goto error;
1651 if (!isl_val_is_int(m))
1652 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1653 "expecting integer modulo", goto error);
1654 pa = isl_pw_aff_mod(pa, m->n);
1655 isl_val_free(m);
1656 return pa;
1657 error:
1658 isl_pw_aff_free(pa);
1659 isl_val_free(m);
1660 return NULL;
1663 /* Given f, return ceil(f).
1664 * If f is an integer expression, then just return f.
1665 * Otherwise, let f be the expression
1667 * e/m
1669 * then return
1671 * floor((e + m - 1)/m)
1673 * As a special case, ceil(NaN) = NaN.
1675 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1677 if (!aff)
1678 return NULL;
1680 if (isl_aff_is_nan(aff))
1681 return aff;
1682 if (isl_int_is_one(aff->v->el[0]))
1683 return aff;
1685 aff = isl_aff_cow(aff);
1686 if (!aff)
1687 return NULL;
1688 aff->v = isl_vec_cow(aff->v);
1689 if (!aff->v)
1690 return isl_aff_free(aff);
1692 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1693 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1694 aff = isl_aff_floor(aff);
1696 return aff;
1699 /* Apply the expansion computed by isl_merge_divs.
1700 * The expansion itself is given by "exp" while the resulting
1701 * list of divs is given by "div".
1703 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1704 __isl_take isl_mat *div, int *exp)
1706 int i, j;
1707 int old_n_div;
1708 int new_n_div;
1709 int offset;
1711 aff = isl_aff_cow(aff);
1712 if (!aff || !div)
1713 goto error;
1715 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1716 new_n_div = isl_mat_rows(div);
1717 if (new_n_div < old_n_div)
1718 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1719 "not an expansion", goto error);
1721 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1722 if (!aff->v)
1723 goto error;
1725 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1726 j = old_n_div - 1;
1727 for (i = new_n_div - 1; i >= 0; --i) {
1728 if (j >= 0 && exp[j] == i) {
1729 if (i != j)
1730 isl_int_swap(aff->v->el[offset + i],
1731 aff->v->el[offset + j]);
1732 j--;
1733 } else
1734 isl_int_set_si(aff->v->el[offset + i], 0);
1737 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1738 if (!aff->ls)
1739 goto error;
1740 isl_mat_free(div);
1741 return aff;
1742 error:
1743 isl_aff_free(aff);
1744 isl_mat_free(div);
1745 return NULL;
1748 /* Add two affine expressions that live in the same local space.
1750 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1751 __isl_take isl_aff *aff2)
1753 isl_int gcd, f;
1755 aff1 = isl_aff_cow(aff1);
1756 if (!aff1 || !aff2)
1757 goto error;
1759 aff1->v = isl_vec_cow(aff1->v);
1760 if (!aff1->v)
1761 goto error;
1763 isl_int_init(gcd);
1764 isl_int_init(f);
1765 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1766 isl_int_divexact(f, aff2->v->el[0], gcd);
1767 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1768 isl_int_divexact(f, aff1->v->el[0], gcd);
1769 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1770 isl_int_divexact(f, aff2->v->el[0], gcd);
1771 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1772 isl_int_clear(f);
1773 isl_int_clear(gcd);
1775 isl_aff_free(aff2);
1776 return aff1;
1777 error:
1778 isl_aff_free(aff1);
1779 isl_aff_free(aff2);
1780 return NULL;
1783 /* Return the sum of "aff1" and "aff2".
1785 * If either of the two is NaN, then the result is NaN.
1787 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1788 __isl_take isl_aff *aff2)
1790 isl_ctx *ctx;
1791 int *exp1 = NULL;
1792 int *exp2 = NULL;
1793 isl_mat *div;
1794 int n_div1, n_div2;
1796 if (!aff1 || !aff2)
1797 goto error;
1799 ctx = isl_aff_get_ctx(aff1);
1800 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1801 isl_die(ctx, isl_error_invalid,
1802 "spaces don't match", goto error);
1804 if (isl_aff_is_nan(aff1)) {
1805 isl_aff_free(aff2);
1806 return aff1;
1808 if (isl_aff_is_nan(aff2)) {
1809 isl_aff_free(aff1);
1810 return aff2;
1813 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1814 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1815 if (n_div1 == 0 && n_div2 == 0)
1816 return add_expanded(aff1, aff2);
1818 exp1 = isl_alloc_array(ctx, int, n_div1);
1819 exp2 = isl_alloc_array(ctx, int, n_div2);
1820 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1821 goto error;
1823 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1824 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1825 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1826 free(exp1);
1827 free(exp2);
1829 return add_expanded(aff1, aff2);
1830 error:
1831 free(exp1);
1832 free(exp2);
1833 isl_aff_free(aff1);
1834 isl_aff_free(aff2);
1835 return NULL;
1838 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1839 __isl_take isl_aff *aff2)
1841 return isl_aff_add(aff1, isl_aff_neg(aff2));
1844 /* Return the result of scaling "aff" by a factor of "f".
1846 * As a special case, f * NaN = NaN.
1848 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1850 isl_int gcd;
1852 if (!aff)
1853 return NULL;
1854 if (isl_aff_is_nan(aff))
1855 return aff;
1857 if (isl_int_is_one(f))
1858 return aff;
1860 aff = isl_aff_cow(aff);
1861 if (!aff)
1862 return NULL;
1863 aff->v = isl_vec_cow(aff->v);
1864 if (!aff->v)
1865 return isl_aff_free(aff);
1867 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1868 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1869 return aff;
1872 isl_int_init(gcd);
1873 isl_int_gcd(gcd, aff->v->el[0], f);
1874 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1875 isl_int_divexact(gcd, f, gcd);
1876 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1877 isl_int_clear(gcd);
1879 return aff;
1882 /* Multiple "aff" by "v".
1884 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1885 __isl_take isl_val *v)
1887 if (!aff || !v)
1888 goto error;
1890 if (isl_val_is_one(v)) {
1891 isl_val_free(v);
1892 return aff;
1895 if (!isl_val_is_rat(v))
1896 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1897 "expecting rational factor", goto error);
1899 aff = isl_aff_scale(aff, v->n);
1900 aff = isl_aff_scale_down(aff, v->d);
1902 isl_val_free(v);
1903 return aff;
1904 error:
1905 isl_aff_free(aff);
1906 isl_val_free(v);
1907 return NULL;
1910 /* Return the result of scaling "aff" down by a factor of "f".
1912 * As a special case, NaN/f = NaN.
1914 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1916 isl_int gcd;
1918 if (!aff)
1919 return NULL;
1920 if (isl_aff_is_nan(aff))
1921 return aff;
1923 if (isl_int_is_one(f))
1924 return aff;
1926 aff = isl_aff_cow(aff);
1927 if (!aff)
1928 return NULL;
1930 if (isl_int_is_zero(f))
1931 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1932 "cannot scale down by zero", return isl_aff_free(aff));
1934 aff->v = isl_vec_cow(aff->v);
1935 if (!aff->v)
1936 return isl_aff_free(aff);
1938 isl_int_init(gcd);
1939 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1940 isl_int_gcd(gcd, gcd, f);
1941 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1942 isl_int_divexact(gcd, f, gcd);
1943 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1944 isl_int_clear(gcd);
1946 return aff;
1949 /* Divide "aff" by "v".
1951 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1952 __isl_take isl_val *v)
1954 if (!aff || !v)
1955 goto error;
1957 if (isl_val_is_one(v)) {
1958 isl_val_free(v);
1959 return aff;
1962 if (!isl_val_is_rat(v))
1963 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1964 "expecting rational factor", goto error);
1965 if (!isl_val_is_pos(v))
1966 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1967 "factor needs to be positive", goto error);
1969 aff = isl_aff_scale(aff, v->d);
1970 aff = isl_aff_scale_down(aff, v->n);
1972 isl_val_free(v);
1973 return aff;
1974 error:
1975 isl_aff_free(aff);
1976 isl_val_free(v);
1977 return NULL;
1980 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1982 isl_int v;
1984 if (f == 1)
1985 return aff;
1987 isl_int_init(v);
1988 isl_int_set_ui(v, f);
1989 aff = isl_aff_scale_down(aff, v);
1990 isl_int_clear(v);
1992 return aff;
1995 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1996 enum isl_dim_type type, unsigned pos, const char *s)
1998 aff = isl_aff_cow(aff);
1999 if (!aff)
2000 return NULL;
2001 if (type == isl_dim_out)
2002 isl_die(aff->v->ctx, isl_error_invalid,
2003 "cannot set name of output/set dimension",
2004 return isl_aff_free(aff));
2005 if (type == isl_dim_in)
2006 type = isl_dim_set;
2007 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2008 if (!aff->ls)
2009 return isl_aff_free(aff);
2011 return aff;
2014 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2015 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2017 aff = isl_aff_cow(aff);
2018 if (!aff)
2019 goto error;
2020 if (type == isl_dim_out)
2021 isl_die(aff->v->ctx, isl_error_invalid,
2022 "cannot set name of output/set dimension",
2023 goto error);
2024 if (type == isl_dim_in)
2025 type = isl_dim_set;
2026 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2027 if (!aff->ls)
2028 return isl_aff_free(aff);
2030 return aff;
2031 error:
2032 isl_id_free(id);
2033 isl_aff_free(aff);
2034 return NULL;
2037 /* Replace the identifier of the input tuple of "aff" by "id".
2038 * type is currently required to be equal to isl_dim_in
2040 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2041 enum isl_dim_type type, __isl_take isl_id *id)
2043 aff = isl_aff_cow(aff);
2044 if (!aff)
2045 goto error;
2046 if (type != isl_dim_out)
2047 isl_die(aff->v->ctx, isl_error_invalid,
2048 "cannot only set id of input tuple", goto error);
2049 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2050 if (!aff->ls)
2051 return isl_aff_free(aff);
2053 return aff;
2054 error:
2055 isl_id_free(id);
2056 isl_aff_free(aff);
2057 return NULL;
2060 /* Exploit the equalities in "eq" to simplify the affine expression
2061 * and the expressions of the integer divisions in the local space.
2062 * The integer divisions in this local space are assumed to appear
2063 * as regular dimensions in "eq".
2065 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2066 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2068 int i, j;
2069 unsigned total;
2070 unsigned n_div;
2072 if (!eq)
2073 goto error;
2074 if (eq->n_eq == 0) {
2075 isl_basic_set_free(eq);
2076 return aff;
2079 aff = isl_aff_cow(aff);
2080 if (!aff)
2081 goto error;
2083 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2084 isl_basic_set_copy(eq));
2085 aff->v = isl_vec_cow(aff->v);
2086 if (!aff->ls || !aff->v)
2087 goto error;
2089 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2090 n_div = eq->n_div;
2091 for (i = 0; i < eq->n_eq; ++i) {
2092 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2093 if (j < 0 || j == 0 || j >= total)
2094 continue;
2096 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2097 &aff->v->el[0]);
2100 isl_basic_set_free(eq);
2101 aff = isl_aff_normalize(aff);
2102 return aff;
2103 error:
2104 isl_basic_set_free(eq);
2105 isl_aff_free(aff);
2106 return NULL;
2109 /* Exploit the equalities in "eq" to simplify the affine expression
2110 * and the expressions of the integer divisions in the local space.
2112 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2113 __isl_take isl_basic_set *eq)
2115 int n_div;
2117 if (!aff || !eq)
2118 goto error;
2119 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2120 if (n_div > 0)
2121 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2122 return isl_aff_substitute_equalities_lifted(aff, eq);
2123 error:
2124 isl_basic_set_free(eq);
2125 isl_aff_free(aff);
2126 return NULL;
2129 /* Look for equalities among the variables shared by context and aff
2130 * and the integer divisions of aff, if any.
2131 * The equalities are then used to eliminate coefficients and/or integer
2132 * divisions from aff.
2134 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2135 __isl_take isl_set *context)
2137 isl_basic_set *hull;
2138 int n_div;
2140 if (!aff)
2141 goto error;
2142 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2143 if (n_div > 0) {
2144 isl_basic_set *bset;
2145 isl_local_space *ls;
2146 context = isl_set_add_dims(context, isl_dim_set, n_div);
2147 ls = isl_aff_get_domain_local_space(aff);
2148 bset = isl_basic_set_from_local_space(ls);
2149 bset = isl_basic_set_lift(bset);
2150 bset = isl_basic_set_flatten(bset);
2151 context = isl_set_intersect(context,
2152 isl_set_from_basic_set(bset));
2155 hull = isl_set_affine_hull(context);
2156 return isl_aff_substitute_equalities_lifted(aff, hull);
2157 error:
2158 isl_aff_free(aff);
2159 isl_set_free(context);
2160 return NULL;
2163 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2164 __isl_take isl_set *context)
2166 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2167 dom_context = isl_set_intersect_params(dom_context, context);
2168 return isl_aff_gist(aff, dom_context);
2171 /* Return a basic set containing those elements in the space
2172 * of aff where it is positive. "rational" should not be set.
2174 * If "aff" is NaN, then it is not positive.
2176 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2177 int rational)
2179 isl_constraint *ineq;
2180 isl_basic_set *bset;
2181 isl_val *c;
2183 if (!aff)
2184 return NULL;
2185 if (isl_aff_is_nan(aff)) {
2186 isl_space *space = isl_aff_get_domain_space(aff);
2187 isl_aff_free(aff);
2188 return isl_basic_set_empty(space);
2190 if (rational)
2191 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2192 "rational sets not supported", goto error);
2194 ineq = isl_inequality_from_aff(aff);
2195 c = isl_constraint_get_constant_val(ineq);
2196 c = isl_val_sub_ui(c, 1);
2197 ineq = isl_constraint_set_constant_val(ineq, c);
2199 bset = isl_basic_set_from_constraint(ineq);
2200 bset = isl_basic_set_simplify(bset);
2201 return bset;
2202 error:
2203 isl_aff_free(aff);
2204 return NULL;
2207 /* Return a basic set containing those elements in the space
2208 * of aff where it is non-negative.
2209 * If "rational" is set, then return a rational basic set.
2211 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2213 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2214 __isl_take isl_aff *aff, int rational)
2216 isl_constraint *ineq;
2217 isl_basic_set *bset;
2219 if (!aff)
2220 return NULL;
2221 if (isl_aff_is_nan(aff)) {
2222 isl_space *space = isl_aff_get_domain_space(aff);
2223 isl_aff_free(aff);
2224 return isl_basic_set_empty(space);
2227 ineq = isl_inequality_from_aff(aff);
2229 bset = isl_basic_set_from_constraint(ineq);
2230 if (rational)
2231 bset = isl_basic_set_set_rational(bset);
2232 bset = isl_basic_set_simplify(bset);
2233 return bset;
2236 /* Return a basic set containing those elements in the space
2237 * of aff where it is non-negative.
2239 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2241 return aff_nonneg_basic_set(aff, 0);
2244 /* Return a basic set containing those elements in the domain space
2245 * of aff where it is negative.
2247 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2249 aff = isl_aff_neg(aff);
2250 aff = isl_aff_add_constant_num_si(aff, -1);
2251 return isl_aff_nonneg_basic_set(aff);
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is zero.
2256 * If "rational" is set, then return a rational basic set.
2258 * If "aff" is NaN, then it is not zero.
2260 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2261 int rational)
2263 isl_constraint *ineq;
2264 isl_basic_set *bset;
2266 if (!aff)
2267 return NULL;
2268 if (isl_aff_is_nan(aff)) {
2269 isl_space *space = isl_aff_get_domain_space(aff);
2270 isl_aff_free(aff);
2271 return isl_basic_set_empty(space);
2274 ineq = isl_equality_from_aff(aff);
2276 bset = isl_basic_set_from_constraint(ineq);
2277 if (rational)
2278 bset = isl_basic_set_set_rational(bset);
2279 bset = isl_basic_set_simplify(bset);
2280 return bset;
2283 /* Return a basic set containing those elements in the space
2284 * of aff where it is zero.
2286 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2288 return aff_zero_basic_set(aff, 0);
2291 /* Return a basic set containing those elements in the shared space
2292 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2294 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2295 __isl_take isl_aff *aff2)
2297 aff1 = isl_aff_sub(aff1, aff2);
2299 return isl_aff_nonneg_basic_set(aff1);
2302 /* Return a basic set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2305 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2306 __isl_take isl_aff *aff2)
2308 return isl_aff_ge_basic_set(aff2, aff1);
2311 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2312 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2314 aff1 = isl_aff_add(aff1, aff2);
2315 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2316 return aff1;
2319 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2321 if (!aff)
2322 return -1;
2324 return 0;
2327 /* Check whether the given affine expression has non-zero coefficient
2328 * for any dimension in the given range or if any of these dimensions
2329 * appear with non-zero coefficients in any of the integer divisions
2330 * involved in the affine expression.
2332 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2333 enum isl_dim_type type, unsigned first, unsigned n)
2335 int i;
2336 isl_ctx *ctx;
2337 int *active = NULL;
2338 isl_bool involves = isl_bool_false;
2340 if (!aff)
2341 return isl_bool_error;
2342 if (n == 0)
2343 return isl_bool_false;
2345 ctx = isl_aff_get_ctx(aff);
2346 if (first + n > isl_aff_dim(aff, type))
2347 isl_die(ctx, isl_error_invalid,
2348 "range out of bounds", return isl_bool_error);
2350 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2351 if (!active)
2352 goto error;
2354 first += isl_local_space_offset(aff->ls, type) - 1;
2355 for (i = 0; i < n; ++i)
2356 if (active[first + i]) {
2357 involves = isl_bool_true;
2358 break;
2361 free(active);
2363 return involves;
2364 error:
2365 free(active);
2366 return isl_bool_error;
2369 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2370 enum isl_dim_type type, unsigned first, unsigned n)
2372 isl_ctx *ctx;
2374 if (!aff)
2375 return NULL;
2376 if (type == isl_dim_out)
2377 isl_die(aff->v->ctx, isl_error_invalid,
2378 "cannot drop output/set dimension",
2379 return isl_aff_free(aff));
2380 if (type == isl_dim_in)
2381 type = isl_dim_set;
2382 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2383 return aff;
2385 ctx = isl_aff_get_ctx(aff);
2386 if (first + n > isl_local_space_dim(aff->ls, type))
2387 isl_die(ctx, isl_error_invalid, "range out of bounds",
2388 return isl_aff_free(aff));
2390 aff = isl_aff_cow(aff);
2391 if (!aff)
2392 return NULL;
2394 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2395 if (!aff->ls)
2396 return isl_aff_free(aff);
2398 first += 1 + isl_local_space_offset(aff->ls, type);
2399 aff->v = isl_vec_drop_els(aff->v, first, n);
2400 if (!aff->v)
2401 return isl_aff_free(aff);
2403 return aff;
2406 /* Project the domain of the affine expression onto its parameter space.
2407 * The affine expression may not involve any of the domain dimensions.
2409 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2411 isl_space *space;
2412 unsigned n;
2413 int involves;
2415 n = isl_aff_dim(aff, isl_dim_in);
2416 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2417 if (involves < 0)
2418 return isl_aff_free(aff);
2419 if (involves)
2420 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2421 "affine expression involves some of the domain dimensions",
2422 return isl_aff_free(aff));
2423 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2424 space = isl_aff_get_domain_space(aff);
2425 space = isl_space_params(space);
2426 aff = isl_aff_reset_domain_space(aff, space);
2427 return aff;
2430 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2431 enum isl_dim_type type, unsigned first, unsigned n)
2433 isl_ctx *ctx;
2435 if (!aff)
2436 return NULL;
2437 if (type == isl_dim_out)
2438 isl_die(aff->v->ctx, isl_error_invalid,
2439 "cannot insert output/set dimensions",
2440 return isl_aff_free(aff));
2441 if (type == isl_dim_in)
2442 type = isl_dim_set;
2443 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2444 return aff;
2446 ctx = isl_aff_get_ctx(aff);
2447 if (first > isl_local_space_dim(aff->ls, type))
2448 isl_die(ctx, isl_error_invalid, "position out of bounds",
2449 return isl_aff_free(aff));
2451 aff = isl_aff_cow(aff);
2452 if (!aff)
2453 return NULL;
2455 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2456 if (!aff->ls)
2457 return isl_aff_free(aff);
2459 first += 1 + isl_local_space_offset(aff->ls, type);
2460 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2461 if (!aff->v)
2462 return isl_aff_free(aff);
2464 return aff;
2467 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2468 enum isl_dim_type type, unsigned n)
2470 unsigned pos;
2472 pos = isl_aff_dim(aff, type);
2474 return isl_aff_insert_dims(aff, type, pos, n);
2477 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2478 enum isl_dim_type type, unsigned n)
2480 unsigned pos;
2482 pos = isl_pw_aff_dim(pwaff, type);
2484 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2487 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2488 * to dimensions of "dst_type" at "dst_pos".
2490 * We only support moving input dimensions to parameters and vice versa.
2492 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2493 enum isl_dim_type dst_type, unsigned dst_pos,
2494 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2496 unsigned g_dst_pos;
2497 unsigned g_src_pos;
2499 if (!aff)
2500 return NULL;
2501 if (n == 0 &&
2502 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2503 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2504 return aff;
2506 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2507 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2508 "cannot move output/set dimension",
2509 return isl_aff_free(aff));
2510 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2511 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2512 "cannot move divs", return isl_aff_free(aff));
2513 if (dst_type == isl_dim_in)
2514 dst_type = isl_dim_set;
2515 if (src_type == isl_dim_in)
2516 src_type = isl_dim_set;
2518 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2519 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2520 "range out of bounds", return isl_aff_free(aff));
2521 if (dst_type == src_type)
2522 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2523 "moving dims within the same type not supported",
2524 return isl_aff_free(aff));
2526 aff = isl_aff_cow(aff);
2527 if (!aff)
2528 return NULL;
2530 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2531 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2532 if (dst_type > src_type)
2533 g_dst_pos -= n;
2535 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2536 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2537 src_type, src_pos, n);
2538 if (!aff->v || !aff->ls)
2539 return isl_aff_free(aff);
2541 aff = sort_divs(aff);
2543 return aff;
2546 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2548 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2549 return isl_pw_aff_alloc(dom, aff);
2552 #undef PW
2553 #define PW isl_pw_aff
2554 #undef EL
2555 #define EL isl_aff
2556 #undef EL_IS_ZERO
2557 #define EL_IS_ZERO is_empty
2558 #undef ZERO
2559 #define ZERO empty
2560 #undef IS_ZERO
2561 #define IS_ZERO is_empty
2562 #undef FIELD
2563 #define FIELD aff
2564 #undef DEFAULT_IS_ZERO
2565 #define DEFAULT_IS_ZERO 0
2567 #define NO_EVAL
2568 #define NO_OPT
2569 #define NO_LIFT
2570 #define NO_MORPH
2572 #include <isl_pw_templ.c>
2574 #undef UNION
2575 #define UNION isl_union_pw_aff
2576 #undef PART
2577 #define PART isl_pw_aff
2578 #undef PARTS
2579 #define PARTS pw_aff
2581 #include <isl_union_templ.c>
2583 static __isl_give isl_set *align_params_pw_pw_set_and(
2584 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2585 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2586 __isl_take isl_pw_aff *pwaff2))
2588 if (!pwaff1 || !pwaff2)
2589 goto error;
2590 if (isl_space_match(pwaff1->dim, isl_dim_param,
2591 pwaff2->dim, isl_dim_param))
2592 return fn(pwaff1, pwaff2);
2593 if (!isl_space_has_named_params(pwaff1->dim) ||
2594 !isl_space_has_named_params(pwaff2->dim))
2595 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2596 "unaligned unnamed parameters", goto error);
2597 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2598 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2599 return fn(pwaff1, pwaff2);
2600 error:
2601 isl_pw_aff_free(pwaff1);
2602 isl_pw_aff_free(pwaff2);
2603 return NULL;
2606 /* Align the parameters of the to isl_pw_aff arguments and
2607 * then apply a function "fn" on them that returns an isl_map.
2609 static __isl_give isl_map *align_params_pw_pw_map_and(
2610 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2611 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2612 __isl_take isl_pw_aff *pa2))
2614 if (!pa1 || !pa2)
2615 goto error;
2616 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2617 return fn(pa1, pa2);
2618 if (!isl_space_has_named_params(pa1->dim) ||
2619 !isl_space_has_named_params(pa2->dim))
2620 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2621 "unaligned unnamed parameters", goto error);
2622 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2623 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2624 return fn(pa1, pa2);
2625 error:
2626 isl_pw_aff_free(pa1);
2627 isl_pw_aff_free(pa2);
2628 return NULL;
2631 /* Compute a piecewise quasi-affine expression with a domain that
2632 * is the union of those of pwaff1 and pwaff2 and such that on each
2633 * cell, the quasi-affine expression is the better (according to cmp)
2634 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2635 * is defined on a given cell, then the associated expression
2636 * is the defined one.
2638 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2639 __isl_take isl_pw_aff *pwaff2,
2640 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2641 __isl_take isl_aff *aff2))
2643 int i, j, n;
2644 isl_pw_aff *res;
2645 isl_ctx *ctx;
2646 isl_set *set;
2648 if (!pwaff1 || !pwaff2)
2649 goto error;
2651 ctx = isl_space_get_ctx(pwaff1->dim);
2652 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2653 isl_die(ctx, isl_error_invalid,
2654 "arguments should live in same space", goto error);
2656 if (isl_pw_aff_is_empty(pwaff1)) {
2657 isl_pw_aff_free(pwaff1);
2658 return pwaff2;
2661 if (isl_pw_aff_is_empty(pwaff2)) {
2662 isl_pw_aff_free(pwaff2);
2663 return pwaff1;
2666 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2667 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2669 for (i = 0; i < pwaff1->n; ++i) {
2670 set = isl_set_copy(pwaff1->p[i].set);
2671 for (j = 0; j < pwaff2->n; ++j) {
2672 struct isl_set *common;
2673 isl_set *better;
2675 common = isl_set_intersect(
2676 isl_set_copy(pwaff1->p[i].set),
2677 isl_set_copy(pwaff2->p[j].set));
2678 better = isl_set_from_basic_set(cmp(
2679 isl_aff_copy(pwaff2->p[j].aff),
2680 isl_aff_copy(pwaff1->p[i].aff)));
2681 better = isl_set_intersect(common, better);
2682 if (isl_set_plain_is_empty(better)) {
2683 isl_set_free(better);
2684 continue;
2686 set = isl_set_subtract(set, isl_set_copy(better));
2688 res = isl_pw_aff_add_piece(res, better,
2689 isl_aff_copy(pwaff2->p[j].aff));
2691 res = isl_pw_aff_add_piece(res, set,
2692 isl_aff_copy(pwaff1->p[i].aff));
2695 for (j = 0; j < pwaff2->n; ++j) {
2696 set = isl_set_copy(pwaff2->p[j].set);
2697 for (i = 0; i < pwaff1->n; ++i)
2698 set = isl_set_subtract(set,
2699 isl_set_copy(pwaff1->p[i].set));
2700 res = isl_pw_aff_add_piece(res, set,
2701 isl_aff_copy(pwaff2->p[j].aff));
2704 isl_pw_aff_free(pwaff1);
2705 isl_pw_aff_free(pwaff2);
2707 return res;
2708 error:
2709 isl_pw_aff_free(pwaff1);
2710 isl_pw_aff_free(pwaff2);
2711 return NULL;
2714 /* Compute a piecewise quasi-affine expression with a domain that
2715 * is the union of those of pwaff1 and pwaff2 and such that on each
2716 * cell, the quasi-affine expression is the maximum of those of pwaff1
2717 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2718 * cell, then the associated expression is the defined one.
2720 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2721 __isl_take isl_pw_aff *pwaff2)
2723 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2726 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2727 __isl_take isl_pw_aff *pwaff2)
2729 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2730 &pw_aff_union_max);
2733 /* Compute a piecewise quasi-affine expression with a domain that
2734 * is the union of those of pwaff1 and pwaff2 and such that on each
2735 * cell, the quasi-affine expression is the minimum of those of pwaff1
2736 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2737 * cell, then the associated expression is the defined one.
2739 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2740 __isl_take isl_pw_aff *pwaff2)
2742 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2745 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2746 __isl_take isl_pw_aff *pwaff2)
2748 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2749 &pw_aff_union_min);
2752 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2753 __isl_take isl_pw_aff *pwaff2, int max)
2755 if (max)
2756 return isl_pw_aff_union_max(pwaff1, pwaff2);
2757 else
2758 return isl_pw_aff_union_min(pwaff1, pwaff2);
2761 /* Construct a map with as domain the domain of pwaff and
2762 * one-dimensional range corresponding to the affine expressions.
2764 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2766 int i;
2767 isl_space *dim;
2768 isl_map *map;
2770 if (!pwaff)
2771 return NULL;
2773 dim = isl_pw_aff_get_space(pwaff);
2774 map = isl_map_empty(dim);
2776 for (i = 0; i < pwaff->n; ++i) {
2777 isl_basic_map *bmap;
2778 isl_map *map_i;
2780 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2781 map_i = isl_map_from_basic_map(bmap);
2782 map_i = isl_map_intersect_domain(map_i,
2783 isl_set_copy(pwaff->p[i].set));
2784 map = isl_map_union_disjoint(map, map_i);
2787 isl_pw_aff_free(pwaff);
2789 return map;
2792 /* Construct a map with as domain the domain of pwaff and
2793 * one-dimensional range corresponding to the affine expressions.
2795 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2797 if (!pwaff)
2798 return NULL;
2799 if (isl_space_is_set(pwaff->dim))
2800 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2801 "space of input is not a map", goto error);
2802 return map_from_pw_aff(pwaff);
2803 error:
2804 isl_pw_aff_free(pwaff);
2805 return NULL;
2808 /* Construct a one-dimensional set with as parameter domain
2809 * the domain of pwaff and the single set dimension
2810 * corresponding to the affine expressions.
2812 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2814 if (!pwaff)
2815 return NULL;
2816 if (!isl_space_is_set(pwaff->dim))
2817 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2818 "space of input is not a set", goto error);
2819 return map_from_pw_aff(pwaff);
2820 error:
2821 isl_pw_aff_free(pwaff);
2822 return NULL;
2825 /* Return a set containing those elements in the domain
2826 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2827 * does not satisfy "fn" (if complement is 1).
2829 * The pieces with a NaN never belong to the result since
2830 * NaN does not satisfy any property.
2832 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2833 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2834 int complement)
2836 int i;
2837 isl_set *set;
2839 if (!pwaff)
2840 return NULL;
2842 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2844 for (i = 0; i < pwaff->n; ++i) {
2845 isl_basic_set *bset;
2846 isl_set *set_i, *locus;
2847 int rational;
2849 if (isl_aff_is_nan(pwaff->p[i].aff))
2850 continue;
2852 rational = isl_set_has_rational(pwaff->p[i].set);
2853 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2854 locus = isl_set_from_basic_set(bset);
2855 set_i = isl_set_copy(pwaff->p[i].set);
2856 if (complement)
2857 set_i = isl_set_subtract(set_i, locus);
2858 else
2859 set_i = isl_set_intersect(set_i, locus);
2860 set = isl_set_union_disjoint(set, set_i);
2863 isl_pw_aff_free(pwaff);
2865 return set;
2868 /* Return a set containing those elements in the domain
2869 * of "pa" where it is positive.
2871 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2873 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2876 /* Return a set containing those elements in the domain
2877 * of pwaff where it is non-negative.
2879 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2881 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2884 /* Return a set containing those elements in the domain
2885 * of pwaff where it is zero.
2887 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2889 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2892 /* Return a set containing those elements in the domain
2893 * of pwaff where it is not zero.
2895 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2897 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2900 /* Return a set containing those elements in the shared domain
2901 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2903 * We compute the difference on the shared domain and then construct
2904 * the set of values where this difference is non-negative.
2905 * If strict is set, we first subtract 1 from the difference.
2906 * If equal is set, we only return the elements where pwaff1 and pwaff2
2907 * are equal.
2909 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2910 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2912 isl_set *set1, *set2;
2914 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2915 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2916 set1 = isl_set_intersect(set1, set2);
2917 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2918 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2919 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2921 if (strict) {
2922 isl_space *dim = isl_set_get_space(set1);
2923 isl_aff *aff;
2924 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2925 aff = isl_aff_add_constant_si(aff, -1);
2926 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2927 } else
2928 isl_set_free(set1);
2930 if (equal)
2931 return isl_pw_aff_zero_set(pwaff1);
2932 return isl_pw_aff_nonneg_set(pwaff1);
2935 /* Return a set containing those elements in the shared domain
2936 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2938 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2939 __isl_take isl_pw_aff *pwaff2)
2941 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2944 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2945 __isl_take isl_pw_aff *pwaff2)
2947 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2950 /* Return a set containing those elements in the shared domain
2951 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2953 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2954 __isl_take isl_pw_aff *pwaff2)
2956 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2959 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2960 __isl_take isl_pw_aff *pwaff2)
2962 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2965 /* Return a set containing those elements in the shared domain
2966 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2968 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2969 __isl_take isl_pw_aff *pwaff2)
2971 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2974 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2975 __isl_take isl_pw_aff *pwaff2)
2977 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2980 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2981 __isl_take isl_pw_aff *pwaff2)
2983 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2986 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2987 __isl_take isl_pw_aff *pwaff2)
2989 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2992 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2993 * where the function values are ordered in the same way as "order",
2994 * which returns a set in the shared domain of its two arguments.
2995 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2997 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2998 * We first pull back the two functions such that they are defined on
2999 * the domain [A -> B]. Then we apply "order", resulting in a set
3000 * in the space [A -> B]. Finally, we unwrap this set to obtain
3001 * a map in the space A -> B.
3003 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3004 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3005 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3006 __isl_take isl_pw_aff *pa2))
3008 isl_space *space1, *space2;
3009 isl_multi_aff *ma;
3010 isl_set *set;
3012 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3013 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3014 space1 = isl_space_map_from_domain_and_range(space1, space2);
3015 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3016 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3017 ma = isl_multi_aff_range_map(space1);
3018 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3019 set = order(pa1, pa2);
3021 return isl_set_unwrap(set);
3024 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3025 * where the function values are equal.
3026 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3028 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3029 __isl_take isl_pw_aff *pa2)
3031 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function values are equal.
3037 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3038 __isl_take isl_pw_aff *pa2)
3040 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3043 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3044 * where the function value of "pa1" is less than the function value of "pa2".
3045 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3047 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3048 __isl_take isl_pw_aff *pa2)
3050 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3053 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3054 * where the function value of "pa1" is less than the function value of "pa2".
3056 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3057 __isl_take isl_pw_aff *pa2)
3059 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3062 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3063 * where the function value of "pa1" is greater than the function value
3064 * of "pa2".
3065 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3067 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3068 __isl_take isl_pw_aff *pa2)
3070 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3073 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3074 * where the function value of "pa1" is greater than the function value
3075 * of "pa2".
3077 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3078 __isl_take isl_pw_aff *pa2)
3080 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3083 /* Return a set containing those elements in the shared domain
3084 * of the elements of list1 and list2 where each element in list1
3085 * has the relation specified by "fn" with each element in list2.
3087 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3088 __isl_take isl_pw_aff_list *list2,
3089 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3090 __isl_take isl_pw_aff *pwaff2))
3092 int i, j;
3093 isl_ctx *ctx;
3094 isl_set *set;
3096 if (!list1 || !list2)
3097 goto error;
3099 ctx = isl_pw_aff_list_get_ctx(list1);
3100 if (list1->n < 1 || list2->n < 1)
3101 isl_die(ctx, isl_error_invalid,
3102 "list should contain at least one element", goto error);
3104 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3105 for (i = 0; i < list1->n; ++i)
3106 for (j = 0; j < list2->n; ++j) {
3107 isl_set *set_ij;
3109 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3110 isl_pw_aff_copy(list2->p[j]));
3111 set = isl_set_intersect(set, set_ij);
3114 isl_pw_aff_list_free(list1);
3115 isl_pw_aff_list_free(list2);
3116 return set;
3117 error:
3118 isl_pw_aff_list_free(list1);
3119 isl_pw_aff_list_free(list2);
3120 return NULL;
3123 /* Return a set containing those elements in the shared domain
3124 * of the elements of list1 and list2 where each element in list1
3125 * is equal to each element in list2.
3127 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3128 __isl_take isl_pw_aff_list *list2)
3130 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3133 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3134 __isl_take isl_pw_aff_list *list2)
3136 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3139 /* Return a set containing those elements in the shared domain
3140 * of the elements of list1 and list2 where each element in list1
3141 * is less than or equal to each element in list2.
3143 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3144 __isl_take isl_pw_aff_list *list2)
3146 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3149 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3150 __isl_take isl_pw_aff_list *list2)
3152 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3155 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3156 __isl_take isl_pw_aff_list *list2)
3158 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3161 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3162 __isl_take isl_pw_aff_list *list2)
3164 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3168 /* Return a set containing those elements in the shared domain
3169 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3171 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3172 __isl_take isl_pw_aff *pwaff2)
3174 isl_set *set_lt, *set_gt;
3176 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3177 isl_pw_aff_copy(pwaff2));
3178 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3179 return isl_set_union_disjoint(set_lt, set_gt);
3182 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3183 __isl_take isl_pw_aff *pwaff2)
3185 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3188 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3189 isl_int v)
3191 int i;
3193 if (isl_int_is_one(v))
3194 return pwaff;
3195 if (!isl_int_is_pos(v))
3196 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3197 "factor needs to be positive",
3198 return isl_pw_aff_free(pwaff));
3199 pwaff = isl_pw_aff_cow(pwaff);
3200 if (!pwaff)
3201 return NULL;
3202 if (pwaff->n == 0)
3203 return pwaff;
3205 for (i = 0; i < pwaff->n; ++i) {
3206 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3207 if (!pwaff->p[i].aff)
3208 return isl_pw_aff_free(pwaff);
3211 return pwaff;
3214 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3216 int i;
3218 pwaff = isl_pw_aff_cow(pwaff);
3219 if (!pwaff)
3220 return NULL;
3221 if (pwaff->n == 0)
3222 return pwaff;
3224 for (i = 0; i < pwaff->n; ++i) {
3225 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3226 if (!pwaff->p[i].aff)
3227 return isl_pw_aff_free(pwaff);
3230 return pwaff;
3233 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3235 int i;
3237 pwaff = isl_pw_aff_cow(pwaff);
3238 if (!pwaff)
3239 return NULL;
3240 if (pwaff->n == 0)
3241 return pwaff;
3243 for (i = 0; i < pwaff->n; ++i) {
3244 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3245 if (!pwaff->p[i].aff)
3246 return isl_pw_aff_free(pwaff);
3249 return pwaff;
3252 /* Assuming that "cond1" and "cond2" are disjoint,
3253 * return an affine expression that is equal to pwaff1 on cond1
3254 * and to pwaff2 on cond2.
3256 static __isl_give isl_pw_aff *isl_pw_aff_select(
3257 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3258 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3260 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3261 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3263 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3266 /* Return an affine expression that is equal to pwaff_true for elements
3267 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3268 * is zero.
3269 * That is, return cond ? pwaff_true : pwaff_false;
3271 * If "cond" involves and NaN, then we conservatively return a NaN
3272 * on its entire domain. In principle, we could consider the pieces
3273 * where it is NaN separately from those where it is not.
3275 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3276 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3278 isl_set *cond_true, *cond_false;
3280 if (!cond)
3281 goto error;
3282 if (isl_pw_aff_involves_nan(cond)) {
3283 isl_space *space = isl_pw_aff_get_domain_space(cond);
3284 isl_local_space *ls = isl_local_space_from_space(space);
3285 isl_pw_aff_free(cond);
3286 isl_pw_aff_free(pwaff_true);
3287 isl_pw_aff_free(pwaff_false);
3288 return isl_pw_aff_nan_on_domain(ls);
3291 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3292 cond_false = isl_pw_aff_zero_set(cond);
3293 return isl_pw_aff_select(cond_true, pwaff_true,
3294 cond_false, pwaff_false);
3295 error:
3296 isl_pw_aff_free(cond);
3297 isl_pw_aff_free(pwaff_true);
3298 isl_pw_aff_free(pwaff_false);
3299 return NULL;
3302 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3304 if (!aff)
3305 return isl_bool_error;
3307 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3310 /* Check whether pwaff is a piecewise constant.
3312 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3314 int i;
3316 if (!pwaff)
3317 return isl_bool_error;
3319 for (i = 0; i < pwaff->n; ++i) {
3320 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3321 if (is_cst < 0 || !is_cst)
3322 return is_cst;
3325 return isl_bool_true;
3328 /* Return the product of "aff1" and "aff2".
3330 * If either of the two is NaN, then the result is NaN.
3332 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3334 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3335 __isl_take isl_aff *aff2)
3337 if (!aff1 || !aff2)
3338 goto error;
3340 if (isl_aff_is_nan(aff1)) {
3341 isl_aff_free(aff2);
3342 return aff1;
3344 if (isl_aff_is_nan(aff2)) {
3345 isl_aff_free(aff1);
3346 return aff2;
3349 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3350 return isl_aff_mul(aff2, aff1);
3352 if (!isl_aff_is_cst(aff2))
3353 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3354 "at least one affine expression should be constant",
3355 goto error);
3357 aff1 = isl_aff_cow(aff1);
3358 if (!aff1 || !aff2)
3359 goto error;
3361 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3362 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3364 isl_aff_free(aff2);
3365 return aff1;
3366 error:
3367 isl_aff_free(aff1);
3368 isl_aff_free(aff2);
3369 return NULL;
3372 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3374 * If either of the two is NaN, then the result is NaN.
3376 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3377 __isl_take isl_aff *aff2)
3379 int is_cst;
3380 int neg;
3382 if (!aff1 || !aff2)
3383 goto error;
3385 if (isl_aff_is_nan(aff1)) {
3386 isl_aff_free(aff2);
3387 return aff1;
3389 if (isl_aff_is_nan(aff2)) {
3390 isl_aff_free(aff1);
3391 return aff2;
3394 is_cst = isl_aff_is_cst(aff2);
3395 if (is_cst < 0)
3396 goto error;
3397 if (!is_cst)
3398 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3399 "second argument should be a constant", goto error);
3401 if (!aff2)
3402 goto error;
3404 neg = isl_int_is_neg(aff2->v->el[1]);
3405 if (neg) {
3406 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3407 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3410 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3411 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3413 if (neg) {
3414 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3415 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3418 isl_aff_free(aff2);
3419 return aff1;
3420 error:
3421 isl_aff_free(aff1);
3422 isl_aff_free(aff2);
3423 return NULL;
3426 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3427 __isl_take isl_pw_aff *pwaff2)
3429 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3432 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3433 __isl_take isl_pw_aff *pwaff2)
3435 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3438 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3439 __isl_take isl_pw_aff *pwaff2)
3441 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3444 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3445 __isl_take isl_pw_aff *pwaff2)
3447 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3450 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3451 __isl_take isl_pw_aff *pwaff2)
3453 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3456 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3457 __isl_take isl_pw_aff *pa2)
3459 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3462 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3464 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3465 __isl_take isl_pw_aff *pa2)
3467 int is_cst;
3469 is_cst = isl_pw_aff_is_cst(pa2);
3470 if (is_cst < 0)
3471 goto error;
3472 if (!is_cst)
3473 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3474 "second argument should be a piecewise constant",
3475 goto error);
3476 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3477 error:
3478 isl_pw_aff_free(pa1);
3479 isl_pw_aff_free(pa2);
3480 return NULL;
3483 /* Compute the quotient of the integer division of "pa1" by "pa2"
3484 * with rounding towards zero.
3485 * "pa2" is assumed to be a piecewise constant.
3487 * In particular, return
3489 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3492 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3493 __isl_take isl_pw_aff *pa2)
3495 int is_cst;
3496 isl_set *cond;
3497 isl_pw_aff *f, *c;
3499 is_cst = isl_pw_aff_is_cst(pa2);
3500 if (is_cst < 0)
3501 goto error;
3502 if (!is_cst)
3503 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3504 "second argument should be a piecewise constant",
3505 goto error);
3507 pa1 = isl_pw_aff_div(pa1, pa2);
3509 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3510 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3511 c = isl_pw_aff_ceil(pa1);
3512 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3513 error:
3514 isl_pw_aff_free(pa1);
3515 isl_pw_aff_free(pa2);
3516 return NULL;
3519 /* Compute the remainder of the integer division of "pa1" by "pa2"
3520 * with rounding towards zero.
3521 * "pa2" is assumed to be a piecewise constant.
3523 * In particular, return
3525 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3528 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3529 __isl_take isl_pw_aff *pa2)
3531 int is_cst;
3532 isl_pw_aff *res;
3534 is_cst = isl_pw_aff_is_cst(pa2);
3535 if (is_cst < 0)
3536 goto error;
3537 if (!is_cst)
3538 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3539 "second argument should be a piecewise constant",
3540 goto error);
3541 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3542 res = isl_pw_aff_mul(pa2, res);
3543 res = isl_pw_aff_sub(pa1, res);
3544 return res;
3545 error:
3546 isl_pw_aff_free(pa1);
3547 isl_pw_aff_free(pa2);
3548 return NULL;
3551 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3552 __isl_take isl_pw_aff *pwaff2)
3554 isl_set *le;
3555 isl_set *dom;
3557 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3558 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3559 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3560 isl_pw_aff_copy(pwaff2));
3561 dom = isl_set_subtract(dom, isl_set_copy(le));
3562 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3565 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3566 __isl_take isl_pw_aff *pwaff2)
3568 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3571 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3572 __isl_take isl_pw_aff *pwaff2)
3574 isl_set *ge;
3575 isl_set *dom;
3577 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3578 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3579 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3580 isl_pw_aff_copy(pwaff2));
3581 dom = isl_set_subtract(dom, isl_set_copy(ge));
3582 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3585 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3586 __isl_take isl_pw_aff *pwaff2)
3588 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3591 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3592 __isl_take isl_pw_aff_list *list,
3593 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3594 __isl_take isl_pw_aff *pwaff2))
3596 int i;
3597 isl_ctx *ctx;
3598 isl_pw_aff *res;
3600 if (!list)
3601 return NULL;
3603 ctx = isl_pw_aff_list_get_ctx(list);
3604 if (list->n < 1)
3605 isl_die(ctx, isl_error_invalid,
3606 "list should contain at least one element", goto error);
3608 res = isl_pw_aff_copy(list->p[0]);
3609 for (i = 1; i < list->n; ++i)
3610 res = fn(res, isl_pw_aff_copy(list->p[i]));
3612 isl_pw_aff_list_free(list);
3613 return res;
3614 error:
3615 isl_pw_aff_list_free(list);
3616 return NULL;
3619 /* Return an isl_pw_aff that maps each element in the intersection of the
3620 * domains of the elements of list to the minimal corresponding affine
3621 * expression.
3623 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3625 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3628 /* Return an isl_pw_aff that maps each element in the intersection of the
3629 * domains of the elements of list to the maximal corresponding affine
3630 * expression.
3632 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3634 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3637 /* Mark the domains of "pwaff" as rational.
3639 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3641 int i;
3643 pwaff = isl_pw_aff_cow(pwaff);
3644 if (!pwaff)
3645 return NULL;
3646 if (pwaff->n == 0)
3647 return pwaff;
3649 for (i = 0; i < pwaff->n; ++i) {
3650 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3651 if (!pwaff->p[i].set)
3652 return isl_pw_aff_free(pwaff);
3655 return pwaff;
3658 /* Mark the domains of the elements of "list" as rational.
3660 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3661 __isl_take isl_pw_aff_list *list)
3663 int i, n;
3665 if (!list)
3666 return NULL;
3667 if (list->n == 0)
3668 return list;
3670 n = list->n;
3671 for (i = 0; i < n; ++i) {
3672 isl_pw_aff *pa;
3674 pa = isl_pw_aff_list_get_pw_aff(list, i);
3675 pa = isl_pw_aff_set_rational(pa);
3676 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3679 return list;
3682 /* Do the parameters of "aff" match those of "space"?
3684 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3685 __isl_keep isl_space *space)
3687 isl_space *aff_space;
3688 int match;
3690 if (!aff || !space)
3691 return -1;
3693 aff_space = isl_aff_get_domain_space(aff);
3695 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3697 isl_space_free(aff_space);
3698 return match;
3701 /* Check that the domain space of "aff" matches "space".
3703 * Return 0 on success and -1 on error.
3705 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3706 __isl_keep isl_space *space)
3708 isl_space *aff_space;
3709 int match;
3711 if (!aff || !space)
3712 return -1;
3714 aff_space = isl_aff_get_domain_space(aff);
3716 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3717 if (match < 0)
3718 goto error;
3719 if (!match)
3720 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3721 "parameters don't match", goto error);
3722 match = isl_space_tuple_is_equal(space, isl_dim_in,
3723 aff_space, isl_dim_set);
3724 if (match < 0)
3725 goto error;
3726 if (!match)
3727 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3728 "domains don't match", goto error);
3729 isl_space_free(aff_space);
3730 return 0;
3731 error:
3732 isl_space_free(aff_space);
3733 return -1;
3736 #undef BASE
3737 #define BASE aff
3738 #undef DOMBASE
3739 #define DOMBASE set
3740 #define NO_DOMAIN
3742 #include <isl_multi_templ.c>
3743 #include <isl_multi_apply_set.c>
3744 #include <isl_multi_floor.c>
3745 #include <isl_multi_gist.c>
3747 #undef NO_DOMAIN
3749 /* Remove any internal structure of the domain of "ma".
3750 * If there is any such internal structure in the input,
3751 * then the name of the corresponding space is also removed.
3753 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3754 __isl_take isl_multi_aff *ma)
3756 isl_space *space;
3758 if (!ma)
3759 return NULL;
3761 if (!ma->space->nested[0])
3762 return ma;
3764 space = isl_multi_aff_get_space(ma);
3765 space = isl_space_flatten_domain(space);
3766 ma = isl_multi_aff_reset_space(ma, space);
3768 return ma;
3771 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3772 * of the space to its domain.
3774 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3776 int i, n_in;
3777 isl_local_space *ls;
3778 isl_multi_aff *ma;
3780 if (!space)
3781 return NULL;
3782 if (!isl_space_is_map(space))
3783 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3784 "not a map space", goto error);
3786 n_in = isl_space_dim(space, isl_dim_in);
3787 space = isl_space_domain_map(space);
3789 ma = isl_multi_aff_alloc(isl_space_copy(space));
3790 if (n_in == 0) {
3791 isl_space_free(space);
3792 return ma;
3795 space = isl_space_domain(space);
3796 ls = isl_local_space_from_space(space);
3797 for (i = 0; i < n_in; ++i) {
3798 isl_aff *aff;
3800 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3801 isl_dim_set, i);
3802 ma = isl_multi_aff_set_aff(ma, i, aff);
3804 isl_local_space_free(ls);
3805 return ma;
3806 error:
3807 isl_space_free(space);
3808 return NULL;
3811 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3812 * of the space to its range.
3814 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3816 int i, n_in, n_out;
3817 isl_local_space *ls;
3818 isl_multi_aff *ma;
3820 if (!space)
3821 return NULL;
3822 if (!isl_space_is_map(space))
3823 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3824 "not a map space", goto error);
3826 n_in = isl_space_dim(space, isl_dim_in);
3827 n_out = isl_space_dim(space, isl_dim_out);
3828 space = isl_space_range_map(space);
3830 ma = isl_multi_aff_alloc(isl_space_copy(space));
3831 if (n_out == 0) {
3832 isl_space_free(space);
3833 return ma;
3836 space = isl_space_domain(space);
3837 ls = isl_local_space_from_space(space);
3838 for (i = 0; i < n_out; ++i) {
3839 isl_aff *aff;
3841 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3842 isl_dim_set, n_in + i);
3843 ma = isl_multi_aff_set_aff(ma, i, aff);
3845 isl_local_space_free(ls);
3846 return ma;
3847 error:
3848 isl_space_free(space);
3849 return NULL;
3852 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3853 * of the space to its range.
3855 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3856 __isl_take isl_space *space)
3858 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3861 /* Given the space of a set and a range of set dimensions,
3862 * construct an isl_multi_aff that projects out those dimensions.
3864 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3865 __isl_take isl_space *space, enum isl_dim_type type,
3866 unsigned first, unsigned n)
3868 int i, dim;
3869 isl_local_space *ls;
3870 isl_multi_aff *ma;
3872 if (!space)
3873 return NULL;
3874 if (!isl_space_is_set(space))
3875 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3876 "expecting set space", goto error);
3877 if (type != isl_dim_set)
3878 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3879 "only set dimensions can be projected out", goto error);
3881 dim = isl_space_dim(space, isl_dim_set);
3882 if (first + n > dim)
3883 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3884 "range out of bounds", goto error);
3886 space = isl_space_from_domain(space);
3887 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3889 if (dim == n)
3890 return isl_multi_aff_alloc(space);
3892 ma = isl_multi_aff_alloc(isl_space_copy(space));
3893 space = isl_space_domain(space);
3894 ls = isl_local_space_from_space(space);
3896 for (i = 0; i < first; ++i) {
3897 isl_aff *aff;
3899 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3900 isl_dim_set, i);
3901 ma = isl_multi_aff_set_aff(ma, i, aff);
3904 for (i = 0; i < dim - (first + n); ++i) {
3905 isl_aff *aff;
3907 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3908 isl_dim_set, first + n + i);
3909 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3912 isl_local_space_free(ls);
3913 return ma;
3914 error:
3915 isl_space_free(space);
3916 return NULL;
3919 /* Given the space of a set and a range of set dimensions,
3920 * construct an isl_pw_multi_aff that projects out those dimensions.
3922 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3923 __isl_take isl_space *space, enum isl_dim_type type,
3924 unsigned first, unsigned n)
3926 isl_multi_aff *ma;
3928 ma = isl_multi_aff_project_out_map(space, type, first, n);
3929 return isl_pw_multi_aff_from_multi_aff(ma);
3932 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3933 * domain.
3935 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3936 __isl_take isl_multi_aff *ma)
3938 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3939 return isl_pw_multi_aff_alloc(dom, ma);
3942 /* Create a piecewise multi-affine expression in the given space that maps each
3943 * input dimension to the corresponding output dimension.
3945 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3946 __isl_take isl_space *space)
3948 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3951 /* Exploit the equalities in "eq" to simplify the affine expressions.
3953 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3954 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3956 int i;
3958 maff = isl_multi_aff_cow(maff);
3959 if (!maff || !eq)
3960 goto error;
3962 for (i = 0; i < maff->n; ++i) {
3963 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3964 isl_basic_set_copy(eq));
3965 if (!maff->p[i])
3966 goto error;
3969 isl_basic_set_free(eq);
3970 return maff;
3971 error:
3972 isl_basic_set_free(eq);
3973 isl_multi_aff_free(maff);
3974 return NULL;
3977 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3978 isl_int f)
3980 int i;
3982 maff = isl_multi_aff_cow(maff);
3983 if (!maff)
3984 return NULL;
3986 for (i = 0; i < maff->n; ++i) {
3987 maff->p[i] = isl_aff_scale(maff->p[i], f);
3988 if (!maff->p[i])
3989 return isl_multi_aff_free(maff);
3992 return maff;
3995 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3996 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3998 maff1 = isl_multi_aff_add(maff1, maff2);
3999 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4000 return maff1;
4003 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4005 if (!maff)
4006 return -1;
4008 return 0;
4011 /* Return the set of domain elements where "ma1" is lexicographically
4012 * smaller than or equal to "ma2".
4014 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4015 __isl_take isl_multi_aff *ma2)
4017 return isl_multi_aff_lex_ge_set(ma2, ma1);
4020 /* Return the set of domain elements where "ma1" is lexicographically
4021 * greater than or equal to "ma2".
4023 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4024 __isl_take isl_multi_aff *ma2)
4026 isl_space *space;
4027 isl_map *map1, *map2;
4028 isl_map *map, *ge;
4030 map1 = isl_map_from_multi_aff(ma1);
4031 map2 = isl_map_from_multi_aff(ma2);
4032 map = isl_map_range_product(map1, map2);
4033 space = isl_space_range(isl_map_get_space(map));
4034 space = isl_space_domain(isl_space_unwrap(space));
4035 ge = isl_map_lex_ge(space);
4036 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4038 return isl_map_domain(map);
4041 #undef PW
4042 #define PW isl_pw_multi_aff
4043 #undef EL
4044 #define EL isl_multi_aff
4045 #undef EL_IS_ZERO
4046 #define EL_IS_ZERO is_empty
4047 #undef ZERO
4048 #define ZERO empty
4049 #undef IS_ZERO
4050 #define IS_ZERO is_empty
4051 #undef FIELD
4052 #define FIELD maff
4053 #undef DEFAULT_IS_ZERO
4054 #define DEFAULT_IS_ZERO 0
4056 #define NO_SUB
4057 #define NO_EVAL
4058 #define NO_OPT
4059 #define NO_INVOLVES_DIMS
4060 #define NO_INSERT_DIMS
4061 #define NO_LIFT
4062 #define NO_MORPH
4064 #include <isl_pw_templ.c>
4066 #undef NO_SUB
4068 #undef UNION
4069 #define UNION isl_union_pw_multi_aff
4070 #undef PART
4071 #define PART isl_pw_multi_aff
4072 #undef PARTS
4073 #define PARTS pw_multi_aff
4075 #include <isl_union_templ.c>
4077 /* Given a function "cmp" that returns the set of elements where
4078 * "ma1" is "better" than "ma2", return the intersection of this
4079 * set with "dom1" and "dom2".
4081 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
4082 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
4083 __isl_keep isl_multi_aff *ma2,
4084 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4085 __isl_take isl_multi_aff *ma2))
4087 isl_set *common;
4088 isl_set *better;
4089 int is_empty;
4091 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
4092 is_empty = isl_set_plain_is_empty(common);
4093 if (is_empty >= 0 && is_empty)
4094 return common;
4095 if (is_empty < 0)
4096 return isl_set_free(common);
4097 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
4098 better = isl_set_intersect(common, better);
4100 return better;
4103 /* Given a function "cmp" that returns the set of elements where
4104 * "ma1" is "better" than "ma2", return a piecewise multi affine
4105 * expression defined on the union of the definition domains
4106 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4107 * "pma2" on each cell. If only one of the two input functions
4108 * is defined on a given cell, then it is considered the best.
4110 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4111 __isl_take isl_pw_multi_aff *pma1,
4112 __isl_take isl_pw_multi_aff *pma2,
4113 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4114 __isl_take isl_multi_aff *ma2))
4116 int i, j, n;
4117 isl_pw_multi_aff *res = NULL;
4118 isl_ctx *ctx;
4119 isl_set *set = NULL;
4121 if (!pma1 || !pma2)
4122 goto error;
4124 ctx = isl_space_get_ctx(pma1->dim);
4125 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4126 isl_die(ctx, isl_error_invalid,
4127 "arguments should live in the same space", goto error);
4129 if (isl_pw_multi_aff_is_empty(pma1)) {
4130 isl_pw_multi_aff_free(pma1);
4131 return pma2;
4134 if (isl_pw_multi_aff_is_empty(pma2)) {
4135 isl_pw_multi_aff_free(pma2);
4136 return pma1;
4139 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4140 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4142 for (i = 0; i < pma1->n; ++i) {
4143 set = isl_set_copy(pma1->p[i].set);
4144 for (j = 0; j < pma2->n; ++j) {
4145 isl_set *better;
4146 int is_empty;
4148 better = shared_and_better(pma2->p[j].set,
4149 pma1->p[i].set, pma2->p[j].maff,
4150 pma1->p[i].maff, cmp);
4151 is_empty = isl_set_plain_is_empty(better);
4152 if (is_empty < 0 || is_empty) {
4153 isl_set_free(better);
4154 if (is_empty < 0)
4155 goto error;
4156 continue;
4158 set = isl_set_subtract(set, isl_set_copy(better));
4160 res = isl_pw_multi_aff_add_piece(res, better,
4161 isl_multi_aff_copy(pma2->p[j].maff));
4163 res = isl_pw_multi_aff_add_piece(res, set,
4164 isl_multi_aff_copy(pma1->p[i].maff));
4167 for (j = 0; j < pma2->n; ++j) {
4168 set = isl_set_copy(pma2->p[j].set);
4169 for (i = 0; i < pma1->n; ++i)
4170 set = isl_set_subtract(set,
4171 isl_set_copy(pma1->p[i].set));
4172 res = isl_pw_multi_aff_add_piece(res, set,
4173 isl_multi_aff_copy(pma2->p[j].maff));
4176 isl_pw_multi_aff_free(pma1);
4177 isl_pw_multi_aff_free(pma2);
4179 return res;
4180 error:
4181 isl_pw_multi_aff_free(pma1);
4182 isl_pw_multi_aff_free(pma2);
4183 isl_set_free(set);
4184 return isl_pw_multi_aff_free(res);
4187 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4188 __isl_take isl_pw_multi_aff *pma1,
4189 __isl_take isl_pw_multi_aff *pma2)
4191 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4194 /* Given two piecewise multi affine expressions, return a piecewise
4195 * multi-affine expression defined on the union of the definition domains
4196 * of the inputs that is equal to the lexicographic maximum of the two
4197 * inputs on each cell. If only one of the two inputs is defined on
4198 * a given cell, then it is considered to be the maximum.
4200 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4201 __isl_take isl_pw_multi_aff *pma1,
4202 __isl_take isl_pw_multi_aff *pma2)
4204 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4205 &pw_multi_aff_union_lexmax);
4208 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4209 __isl_take isl_pw_multi_aff *pma1,
4210 __isl_take isl_pw_multi_aff *pma2)
4212 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4215 /* Given two piecewise multi affine expressions, return a piecewise
4216 * multi-affine expression defined on the union of the definition domains
4217 * of the inputs that is equal to the lexicographic minimum of the two
4218 * inputs on each cell. If only one of the two inputs is defined on
4219 * a given cell, then it is considered to be the minimum.
4221 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4222 __isl_take isl_pw_multi_aff *pma1,
4223 __isl_take isl_pw_multi_aff *pma2)
4225 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4226 &pw_multi_aff_union_lexmin);
4229 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4230 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4232 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4233 &isl_multi_aff_add);
4236 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4237 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4239 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4240 &pw_multi_aff_add);
4243 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4244 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4246 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4247 &isl_multi_aff_sub);
4250 /* Subtract "pma2" from "pma1" and return the result.
4252 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4253 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4255 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4256 &pw_multi_aff_sub);
4259 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4260 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4262 return isl_pw_multi_aff_union_add_(pma1, pma2);
4265 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4266 * with the actual sum on the shared domain and
4267 * the defined expression on the symmetric difference of the domains.
4269 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4270 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4272 return isl_union_pw_aff_union_add_(upa1, upa2);
4275 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4276 * with the actual sum on the shared domain and
4277 * the defined expression on the symmetric difference of the domains.
4279 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4280 __isl_take isl_union_pw_multi_aff *upma1,
4281 __isl_take isl_union_pw_multi_aff *upma2)
4283 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4286 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4287 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4289 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4290 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4292 int i, j, n;
4293 isl_space *space;
4294 isl_pw_multi_aff *res;
4296 if (!pma1 || !pma2)
4297 goto error;
4299 n = pma1->n * pma2->n;
4300 space = isl_space_product(isl_space_copy(pma1->dim),
4301 isl_space_copy(pma2->dim));
4302 res = isl_pw_multi_aff_alloc_size(space, n);
4304 for (i = 0; i < pma1->n; ++i) {
4305 for (j = 0; j < pma2->n; ++j) {
4306 isl_set *domain;
4307 isl_multi_aff *ma;
4309 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4310 isl_set_copy(pma2->p[j].set));
4311 ma = isl_multi_aff_product(
4312 isl_multi_aff_copy(pma1->p[i].maff),
4313 isl_multi_aff_copy(pma2->p[j].maff));
4314 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4318 isl_pw_multi_aff_free(pma1);
4319 isl_pw_multi_aff_free(pma2);
4320 return res;
4321 error:
4322 isl_pw_multi_aff_free(pma1);
4323 isl_pw_multi_aff_free(pma2);
4324 return NULL;
4327 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4328 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4330 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4331 &pw_multi_aff_product);
4334 /* Construct a map mapping the domain of the piecewise multi-affine expression
4335 * to its range, with each dimension in the range equated to the
4336 * corresponding affine expression on its cell.
4338 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4340 int i;
4341 isl_map *map;
4343 if (!pma)
4344 return NULL;
4346 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4348 for (i = 0; i < pma->n; ++i) {
4349 isl_multi_aff *maff;
4350 isl_basic_map *bmap;
4351 isl_map *map_i;
4353 maff = isl_multi_aff_copy(pma->p[i].maff);
4354 bmap = isl_basic_map_from_multi_aff(maff);
4355 map_i = isl_map_from_basic_map(bmap);
4356 map_i = isl_map_intersect_domain(map_i,
4357 isl_set_copy(pma->p[i].set));
4358 map = isl_map_union_disjoint(map, map_i);
4361 isl_pw_multi_aff_free(pma);
4362 return map;
4365 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4367 if (!pma)
4368 return NULL;
4370 if (!isl_space_is_set(pma->dim))
4371 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4372 "isl_pw_multi_aff cannot be converted into an isl_set",
4373 goto error);
4375 return isl_map_from_pw_multi_aff(pma);
4376 error:
4377 isl_pw_multi_aff_free(pma);
4378 return NULL;
4381 /* Given a basic map with a single output dimension that is defined
4382 * in terms of the parameters and input dimensions using an equality,
4383 * extract an isl_aff that expresses the output dimension in terms
4384 * of the parameters and input dimensions.
4385 * Note that this expression may involve integer divisions defined
4386 * in terms of parameters and input dimensions.
4388 * This function shares some similarities with
4389 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4391 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4392 __isl_take isl_basic_map *bmap)
4394 int eq;
4395 unsigned offset;
4396 unsigned n_div;
4397 isl_local_space *ls;
4398 isl_aff *aff;
4400 if (!bmap)
4401 return NULL;
4402 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4403 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4404 "basic map should have a single output dimension",
4405 goto error);
4406 eq = isl_basic_map_output_defining_equality(bmap, 0);
4407 if (eq >= bmap->n_eq)
4408 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4409 "unable to find suitable equality", goto error);
4410 ls = isl_basic_map_get_local_space(bmap);
4411 aff = isl_aff_alloc(isl_local_space_domain(ls));
4412 if (!aff)
4413 goto error;
4414 offset = isl_basic_map_offset(bmap, isl_dim_out);
4415 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4416 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4417 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4418 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4419 n_div);
4420 } else {
4421 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4422 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4423 n_div);
4425 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4426 isl_basic_map_free(bmap);
4428 aff = isl_aff_remove_unused_divs(aff);
4429 return aff;
4430 error:
4431 isl_basic_map_free(bmap);
4432 return NULL;
4435 /* Given a basic map where each output dimension is defined
4436 * in terms of the parameters and input dimensions using an equality,
4437 * extract an isl_multi_aff that expresses the output dimensions in terms
4438 * of the parameters and input dimensions.
4440 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4441 __isl_take isl_basic_map *bmap)
4443 int i;
4444 unsigned n_out;
4445 isl_multi_aff *ma;
4447 if (!bmap)
4448 return NULL;
4450 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4451 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4453 for (i = 0; i < n_out; ++i) {
4454 isl_basic_map *bmap_i;
4455 isl_aff *aff;
4457 bmap_i = isl_basic_map_copy(bmap);
4458 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4459 i + 1, n_out - (1 + i));
4460 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4461 aff = extract_isl_aff_from_basic_map(bmap_i);
4462 ma = isl_multi_aff_set_aff(ma, i, aff);
4465 isl_basic_map_free(bmap);
4467 return ma;
4470 /* Given a basic set where each set dimension is defined
4471 * in terms of the parameters using an equality,
4472 * extract an isl_multi_aff that expresses the set dimensions in terms
4473 * of the parameters.
4475 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4476 __isl_take isl_basic_set *bset)
4478 return extract_isl_multi_aff_from_basic_map(bset);
4481 /* Create an isl_pw_multi_aff that is equivalent to
4482 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4483 * The given basic map is such that each output dimension is defined
4484 * in terms of the parameters and input dimensions using an equality.
4486 * Since some applications expect the result of isl_pw_multi_aff_from_map
4487 * to only contain integer affine expressions, we compute the floor
4488 * of the expression before returning.
4490 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4491 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4493 isl_multi_aff *ma;
4495 ma = extract_isl_multi_aff_from_basic_map(bmap);
4496 ma = isl_multi_aff_floor(ma);
4497 return isl_pw_multi_aff_alloc(domain, ma);
4500 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4501 * This obviously only works if the input "map" is single-valued.
4502 * If so, we compute the lexicographic minimum of the image in the form
4503 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4504 * to its lexicographic minimum.
4505 * If the input is not single-valued, we produce an error.
4507 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4508 __isl_take isl_map *map)
4510 int i;
4511 int sv;
4512 isl_pw_multi_aff *pma;
4514 sv = isl_map_is_single_valued(map);
4515 if (sv < 0)
4516 goto error;
4517 if (!sv)
4518 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4519 "map is not single-valued", goto error);
4520 map = isl_map_make_disjoint(map);
4521 if (!map)
4522 return NULL;
4524 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4526 for (i = 0; i < map->n; ++i) {
4527 isl_pw_multi_aff *pma_i;
4528 isl_basic_map *bmap;
4529 bmap = isl_basic_map_copy(map->p[i]);
4530 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4531 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4534 isl_map_free(map);
4535 return pma;
4536 error:
4537 isl_map_free(map);
4538 return NULL;
4541 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4542 * taking into account that the output dimension at position "d"
4543 * can be represented as
4545 * x = floor((e(...) + c1) / m)
4547 * given that constraint "i" is of the form
4549 * e(...) + c1 - m x >= 0
4552 * Let "map" be of the form
4554 * A -> B
4556 * We construct a mapping
4558 * A -> [A -> x = floor(...)]
4560 * apply that to the map, obtaining
4562 * [A -> x = floor(...)] -> B
4564 * and equate dimension "d" to x.
4565 * We then compute a isl_pw_multi_aff representation of the resulting map
4566 * and plug in the mapping above.
4568 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4569 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4571 isl_ctx *ctx;
4572 isl_space *space;
4573 isl_local_space *ls;
4574 isl_multi_aff *ma;
4575 isl_aff *aff;
4576 isl_vec *v;
4577 isl_map *insert;
4578 int offset;
4579 int n;
4580 int n_in;
4581 isl_pw_multi_aff *pma;
4582 int is_set;
4584 is_set = isl_map_is_set(map);
4586 offset = isl_basic_map_offset(hull, isl_dim_out);
4587 ctx = isl_map_get_ctx(map);
4588 space = isl_space_domain(isl_map_get_space(map));
4589 n_in = isl_space_dim(space, isl_dim_set);
4590 n = isl_space_dim(space, isl_dim_all);
4592 v = isl_vec_alloc(ctx, 1 + 1 + n);
4593 if (v) {
4594 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4595 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4597 isl_basic_map_free(hull);
4599 ls = isl_local_space_from_space(isl_space_copy(space));
4600 aff = isl_aff_alloc_vec(ls, v);
4601 aff = isl_aff_floor(aff);
4602 if (is_set) {
4603 isl_space_free(space);
4604 ma = isl_multi_aff_from_aff(aff);
4605 } else {
4606 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4607 ma = isl_multi_aff_range_product(ma,
4608 isl_multi_aff_from_aff(aff));
4611 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4612 map = isl_map_apply_domain(map, insert);
4613 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4614 pma = isl_pw_multi_aff_from_map(map);
4615 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4617 return pma;
4620 /* Is constraint "c" of the form
4622 * e(...) + c1 - m x >= 0
4624 * or
4626 * -e(...) + c2 + m x >= 0
4628 * where m > 1 and e only depends on parameters and input dimemnsions?
4630 * "offset" is the offset of the output dimensions
4631 * "pos" is the position of output dimension x.
4633 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4635 if (isl_int_is_zero(c[offset + d]))
4636 return 0;
4637 if (isl_int_is_one(c[offset + d]))
4638 return 0;
4639 if (isl_int_is_negone(c[offset + d]))
4640 return 0;
4641 if (isl_seq_first_non_zero(c + offset, d) != -1)
4642 return 0;
4643 if (isl_seq_first_non_zero(c + offset + d + 1,
4644 total - (offset + d + 1)) != -1)
4645 return 0;
4646 return 1;
4649 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4651 * As a special case, we first check if there is any pair of constraints,
4652 * shared by all the basic maps in "map" that force a given dimension
4653 * to be equal to the floor of some affine combination of the input dimensions.
4655 * In particular, if we can find two constraints
4657 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4659 * and
4661 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4663 * where m > 1 and e only depends on parameters and input dimemnsions,
4664 * and such that
4666 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4668 * then we know that we can take
4670 * x = floor((e(...) + c1) / m)
4672 * without having to perform any computation.
4674 * Note that we know that
4676 * c1 + c2 >= 1
4678 * If c1 + c2 were 0, then we would have detected an equality during
4679 * simplification. If c1 + c2 were negative, then we would have detected
4680 * a contradiction.
4682 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4683 __isl_take isl_map *map)
4685 int d, dim;
4686 int i, j, n;
4687 int offset, total;
4688 isl_int sum;
4689 isl_basic_map *hull;
4691 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4692 if (!hull)
4693 goto error;
4695 isl_int_init(sum);
4696 dim = isl_map_dim(map, isl_dim_out);
4697 offset = isl_basic_map_offset(hull, isl_dim_out);
4698 total = 1 + isl_basic_map_total_dim(hull);
4699 n = hull->n_ineq;
4700 for (d = 0; d < dim; ++d) {
4701 for (i = 0; i < n; ++i) {
4702 if (!is_potential_div_constraint(hull->ineq[i],
4703 offset, d, total))
4704 continue;
4705 for (j = i + 1; j < n; ++j) {
4706 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4707 hull->ineq[j] + 1, total - 1))
4708 continue;
4709 isl_int_add(sum, hull->ineq[i][0],
4710 hull->ineq[j][0]);
4711 if (isl_int_abs_lt(sum,
4712 hull->ineq[i][offset + d]))
4713 break;
4716 if (j >= n)
4717 continue;
4718 isl_int_clear(sum);
4719 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4720 j = i;
4721 return pw_multi_aff_from_map_div(map, hull, d, j);
4724 isl_int_clear(sum);
4725 isl_basic_map_free(hull);
4726 return pw_multi_aff_from_map_base(map);
4727 error:
4728 isl_map_free(map);
4729 isl_basic_map_free(hull);
4730 return NULL;
4733 /* Given an affine expression
4735 * [A -> B] -> f(A,B)
4737 * construct an isl_multi_aff
4739 * [A -> B] -> B'
4741 * such that dimension "d" in B' is set to "aff" and the remaining
4742 * dimensions are set equal to the corresponding dimensions in B.
4743 * "n_in" is the dimension of the space A.
4744 * "n_out" is the dimension of the space B.
4746 * If "is_set" is set, then the affine expression is of the form
4748 * [B] -> f(B)
4750 * and we construct an isl_multi_aff
4752 * B -> B'
4754 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4755 unsigned n_in, unsigned n_out, int is_set)
4757 int i;
4758 isl_multi_aff *ma;
4759 isl_space *space, *space2;
4760 isl_local_space *ls;
4762 space = isl_aff_get_domain_space(aff);
4763 ls = isl_local_space_from_space(isl_space_copy(space));
4764 space2 = isl_space_copy(space);
4765 if (!is_set)
4766 space2 = isl_space_range(isl_space_unwrap(space2));
4767 space = isl_space_map_from_domain_and_range(space, space2);
4768 ma = isl_multi_aff_alloc(space);
4769 ma = isl_multi_aff_set_aff(ma, d, aff);
4771 for (i = 0; i < n_out; ++i) {
4772 if (i == d)
4773 continue;
4774 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4775 isl_dim_set, n_in + i);
4776 ma = isl_multi_aff_set_aff(ma, i, aff);
4779 isl_local_space_free(ls);
4781 return ma;
4784 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4785 * taking into account that the dimension at position "d" can be written as
4787 * x = m a + f(..) (1)
4789 * where m is equal to "gcd".
4790 * "i" is the index of the equality in "hull" that defines f(..).
4791 * In particular, the equality is of the form
4793 * f(..) - x + m g(existentials) = 0
4795 * or
4797 * -f(..) + x + m g(existentials) = 0
4799 * We basically plug (1) into "map", resulting in a map with "a"
4800 * in the range instead of "x". The corresponding isl_pw_multi_aff
4801 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4803 * Specifically, given the input map
4805 * A -> B
4807 * We first wrap it into a set
4809 * [A -> B]
4811 * and define (1) on top of the corresponding space, resulting in "aff".
4812 * We use this to create an isl_multi_aff that maps the output position "d"
4813 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4814 * We plug this into the wrapped map, unwrap the result and compute the
4815 * corresponding isl_pw_multi_aff.
4816 * The result is an expression
4818 * A -> T(A)
4820 * We adjust that to
4822 * A -> [A -> T(A)]
4824 * so that we can plug that into "aff", after extending the latter to
4825 * a mapping
4827 * [A -> B] -> B'
4830 * If "map" is actually a set, then there is no "A" space, meaning
4831 * that we do not need to perform any wrapping, and that the result
4832 * of the recursive call is of the form
4834 * [T]
4836 * which is plugged into a mapping of the form
4838 * B -> B'
4840 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4841 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4842 isl_int gcd)
4844 isl_set *set;
4845 isl_space *space;
4846 isl_local_space *ls;
4847 isl_aff *aff;
4848 isl_multi_aff *ma;
4849 isl_pw_multi_aff *pma, *id;
4850 unsigned n_in;
4851 unsigned o_out;
4852 unsigned n_out;
4853 int is_set;
4855 is_set = isl_map_is_set(map);
4857 n_in = isl_basic_map_dim(hull, isl_dim_in);
4858 n_out = isl_basic_map_dim(hull, isl_dim_out);
4859 o_out = isl_basic_map_offset(hull, isl_dim_out);
4861 if (is_set)
4862 set = map;
4863 else
4864 set = isl_map_wrap(map);
4865 space = isl_space_map_from_set(isl_set_get_space(set));
4866 ma = isl_multi_aff_identity(space);
4867 ls = isl_local_space_from_space(isl_set_get_space(set));
4868 aff = isl_aff_alloc(ls);
4869 if (aff) {
4870 isl_int_set_si(aff->v->el[0], 1);
4871 if (isl_int_is_one(hull->eq[i][o_out + d]))
4872 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4873 aff->v->size - 1);
4874 else
4875 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4876 aff->v->size - 1);
4877 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4879 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4880 set = isl_set_preimage_multi_aff(set, ma);
4882 ma = range_map(aff, d, n_in, n_out, is_set);
4884 if (is_set)
4885 map = set;
4886 else
4887 map = isl_set_unwrap(set);
4888 pma = isl_pw_multi_aff_from_map(map);
4890 if (!is_set) {
4891 space = isl_pw_multi_aff_get_domain_space(pma);
4892 space = isl_space_map_from_set(space);
4893 id = isl_pw_multi_aff_identity(space);
4894 pma = isl_pw_multi_aff_range_product(id, pma);
4896 id = isl_pw_multi_aff_from_multi_aff(ma);
4897 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4899 isl_basic_map_free(hull);
4900 return pma;
4903 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4905 * As a special case, we first check if all output dimensions are uniquely
4906 * defined in terms of the parameters and input dimensions over the entire
4907 * domain. If so, we extract the desired isl_pw_multi_aff directly
4908 * from the affine hull of "map" and its domain.
4910 * Otherwise, we check if any of the output dimensions is "strided".
4911 * That is, we check if can be written as
4913 * x = m a + f(..)
4915 * with m greater than 1, a some combination of existentiall quantified
4916 * variables and f and expression in the parameters and input dimensions.
4917 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4919 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4920 * special case.
4922 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4924 int i, j;
4925 int sv;
4926 isl_basic_map *hull;
4927 unsigned n_out;
4928 unsigned o_out;
4929 unsigned n_div;
4930 unsigned o_div;
4931 isl_int gcd;
4933 if (!map)
4934 return NULL;
4936 hull = isl_map_affine_hull(isl_map_copy(map));
4937 sv = isl_basic_map_plain_is_single_valued(hull);
4938 if (sv >= 0 && sv)
4939 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4940 if (sv < 0)
4941 hull = isl_basic_map_free(hull);
4942 if (!hull)
4943 goto error;
4945 n_div = isl_basic_map_dim(hull, isl_dim_div);
4946 o_div = isl_basic_map_offset(hull, isl_dim_div);
4948 if (n_div == 0) {
4949 isl_basic_map_free(hull);
4950 return pw_multi_aff_from_map_check_div(map);
4953 isl_int_init(gcd);
4955 n_out = isl_basic_map_dim(hull, isl_dim_out);
4956 o_out = isl_basic_map_offset(hull, isl_dim_out);
4958 for (i = 0; i < n_out; ++i) {
4959 for (j = 0; j < hull->n_eq; ++j) {
4960 isl_int *eq = hull->eq[j];
4961 isl_pw_multi_aff *res;
4963 if (!isl_int_is_one(eq[o_out + i]) &&
4964 !isl_int_is_negone(eq[o_out + i]))
4965 continue;
4966 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4967 continue;
4968 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4969 n_out - (i + 1)) != -1)
4970 continue;
4971 isl_seq_gcd(eq + o_div, n_div, &gcd);
4972 if (isl_int_is_zero(gcd))
4973 continue;
4974 if (isl_int_is_one(gcd))
4975 continue;
4977 res = pw_multi_aff_from_map_stride(map, hull,
4978 i, j, gcd);
4979 isl_int_clear(gcd);
4980 return res;
4984 isl_int_clear(gcd);
4985 isl_basic_map_free(hull);
4986 return pw_multi_aff_from_map_check_div(map);
4987 error:
4988 isl_map_free(map);
4989 return NULL;
4992 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4994 return isl_pw_multi_aff_from_map(set);
4997 /* Convert "map" into an isl_pw_multi_aff (if possible) and
4998 * add it to *user.
5000 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5002 isl_union_pw_multi_aff **upma = user;
5003 isl_pw_multi_aff *pma;
5005 pma = isl_pw_multi_aff_from_map(map);
5006 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5008 return *upma ? isl_stat_ok : isl_stat_error;
5011 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5012 * domain.
5014 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5015 __isl_take isl_aff *aff)
5017 isl_multi_aff *ma;
5018 isl_pw_multi_aff *pma;
5020 ma = isl_multi_aff_from_aff(aff);
5021 pma = isl_pw_multi_aff_from_multi_aff(ma);
5022 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5025 /* Try and create an isl_union_pw_multi_aff that is equivalent
5026 * to the given isl_union_map.
5027 * The isl_union_map is required to be single-valued in each space.
5028 * Otherwise, an error is produced.
5030 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5031 __isl_take isl_union_map *umap)
5033 isl_space *space;
5034 isl_union_pw_multi_aff *upma;
5036 space = isl_union_map_get_space(umap);
5037 upma = isl_union_pw_multi_aff_empty(space);
5038 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5039 upma = isl_union_pw_multi_aff_free(upma);
5040 isl_union_map_free(umap);
5042 return upma;
5045 /* Try and create an isl_union_pw_multi_aff that is equivalent
5046 * to the given isl_union_set.
5047 * The isl_union_set is required to be a singleton in each space.
5048 * Otherwise, an error is produced.
5050 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5051 __isl_take isl_union_set *uset)
5053 return isl_union_pw_multi_aff_from_union_map(uset);
5056 /* Return the piecewise affine expression "set ? 1 : 0".
5058 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5060 isl_pw_aff *pa;
5061 isl_space *space = isl_set_get_space(set);
5062 isl_local_space *ls = isl_local_space_from_space(space);
5063 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5064 isl_aff *one = isl_aff_zero_on_domain(ls);
5066 one = isl_aff_add_constant_si(one, 1);
5067 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5068 set = isl_set_complement(set);
5069 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5071 return pa;
5074 /* Plug in "subs" for dimension "type", "pos" of "aff".
5076 * Let i be the dimension to replace and let "subs" be of the form
5078 * f/d
5080 * and "aff" of the form
5082 * (a i + g)/m
5084 * The result is
5086 * (a f + d g')/(m d)
5088 * where g' is the result of plugging in "subs" in each of the integer
5089 * divisions in g.
5091 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5092 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5094 isl_ctx *ctx;
5095 isl_int v;
5097 aff = isl_aff_cow(aff);
5098 if (!aff || !subs)
5099 return isl_aff_free(aff);
5101 ctx = isl_aff_get_ctx(aff);
5102 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5103 isl_die(ctx, isl_error_invalid,
5104 "spaces don't match", return isl_aff_free(aff));
5105 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5106 isl_die(ctx, isl_error_unsupported,
5107 "cannot handle divs yet", return isl_aff_free(aff));
5109 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5110 if (!aff->ls)
5111 return isl_aff_free(aff);
5113 aff->v = isl_vec_cow(aff->v);
5114 if (!aff->v)
5115 return isl_aff_free(aff);
5117 pos += isl_local_space_offset(aff->ls, type);
5119 isl_int_init(v);
5120 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5121 aff->v->size, subs->v->size, v);
5122 isl_int_clear(v);
5124 return aff;
5127 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5128 * expressions in "maff".
5130 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5131 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5132 __isl_keep isl_aff *subs)
5134 int i;
5136 maff = isl_multi_aff_cow(maff);
5137 if (!maff || !subs)
5138 return isl_multi_aff_free(maff);
5140 if (type == isl_dim_in)
5141 type = isl_dim_set;
5143 for (i = 0; i < maff->n; ++i) {
5144 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5145 if (!maff->p[i])
5146 return isl_multi_aff_free(maff);
5149 return maff;
5152 /* Plug in "subs" for dimension "type", "pos" of "pma".
5154 * pma is of the form
5156 * A_i(v) -> M_i(v)
5158 * while subs is of the form
5160 * v' = B_j(v) -> S_j
5162 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5163 * has a contribution in the result, in particular
5165 * C_ij(S_j) -> M_i(S_j)
5167 * Note that plugging in S_j in C_ij may also result in an empty set
5168 * and this contribution should simply be discarded.
5170 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5171 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5172 __isl_keep isl_pw_aff *subs)
5174 int i, j, n;
5175 isl_pw_multi_aff *res;
5177 if (!pma || !subs)
5178 return isl_pw_multi_aff_free(pma);
5180 n = pma->n * subs->n;
5181 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5183 for (i = 0; i < pma->n; ++i) {
5184 for (j = 0; j < subs->n; ++j) {
5185 isl_set *common;
5186 isl_multi_aff *res_ij;
5187 int empty;
5189 common = isl_set_intersect(
5190 isl_set_copy(pma->p[i].set),
5191 isl_set_copy(subs->p[j].set));
5192 common = isl_set_substitute(common,
5193 type, pos, subs->p[j].aff);
5194 empty = isl_set_plain_is_empty(common);
5195 if (empty < 0 || empty) {
5196 isl_set_free(common);
5197 if (empty < 0)
5198 goto error;
5199 continue;
5202 res_ij = isl_multi_aff_substitute(
5203 isl_multi_aff_copy(pma->p[i].maff),
5204 type, pos, subs->p[j].aff);
5206 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5210 isl_pw_multi_aff_free(pma);
5211 return res;
5212 error:
5213 isl_pw_multi_aff_free(pma);
5214 isl_pw_multi_aff_free(res);
5215 return NULL;
5218 /* Compute the preimage of a range of dimensions in the affine expression "src"
5219 * under "ma" and put the result in "dst". The number of dimensions in "src"
5220 * that precede the range is given by "n_before". The number of dimensions
5221 * in the range is given by the number of output dimensions of "ma".
5222 * The number of dimensions that follow the range is given by "n_after".
5223 * If "has_denom" is set (to one),
5224 * then "src" and "dst" have an extra initial denominator.
5225 * "n_div_ma" is the number of existentials in "ma"
5226 * "n_div_bset" is the number of existentials in "src"
5227 * The resulting "dst" (which is assumed to have been allocated by
5228 * the caller) contains coefficients for both sets of existentials,
5229 * first those in "ma" and then those in "src".
5230 * f, c1, c2 and g are temporary objects that have been initialized
5231 * by the caller.
5233 * Let src represent the expression
5235 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5237 * and let ma represent the expressions
5239 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5241 * We start out with the following expression for dst:
5243 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5245 * with the multiplication factor f initially equal to 1
5246 * and f \sum_i b_i v_i kept separately.
5247 * For each x_i that we substitute, we multiply the numerator
5248 * (and denominator) of dst by c_1 = m_i and add the numerator
5249 * of the x_i expression multiplied by c_2 = f b_i,
5250 * after removing the common factors of c_1 and c_2.
5251 * The multiplication factor f also needs to be multiplied by c_1
5252 * for the next x_j, j > i.
5254 void isl_seq_preimage(isl_int *dst, isl_int *src,
5255 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5256 int n_div_ma, int n_div_bmap,
5257 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5259 int i;
5260 int n_param, n_in, n_out;
5261 int o_dst, o_src;
5263 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5264 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5265 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5267 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5268 o_dst = o_src = has_denom + 1 + n_param + n_before;
5269 isl_seq_clr(dst + o_dst, n_in);
5270 o_dst += n_in;
5271 o_src += n_out;
5272 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5273 o_dst += n_after;
5274 o_src += n_after;
5275 isl_seq_clr(dst + o_dst, n_div_ma);
5276 o_dst += n_div_ma;
5277 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5279 isl_int_set_si(f, 1);
5281 for (i = 0; i < n_out; ++i) {
5282 int offset = has_denom + 1 + n_param + n_before + i;
5284 if (isl_int_is_zero(src[offset]))
5285 continue;
5286 isl_int_set(c1, ma->p[i]->v->el[0]);
5287 isl_int_mul(c2, f, src[offset]);
5288 isl_int_gcd(g, c1, c2);
5289 isl_int_divexact(c1, c1, g);
5290 isl_int_divexact(c2, c2, g);
5292 isl_int_mul(f, f, c1);
5293 o_dst = has_denom;
5294 o_src = 1;
5295 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5296 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5297 o_dst += 1 + n_param;
5298 o_src += 1 + n_param;
5299 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5300 o_dst += n_before;
5301 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5302 c2, ma->p[i]->v->el + o_src, n_in);
5303 o_dst += n_in;
5304 o_src += n_in;
5305 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5306 o_dst += n_after;
5307 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5308 c2, ma->p[i]->v->el + o_src, n_div_ma);
5309 o_dst += n_div_ma;
5310 o_src += n_div_ma;
5311 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5312 if (has_denom)
5313 isl_int_mul(dst[0], dst[0], c1);
5317 /* Compute the pullback of "aff" by the function represented by "ma".
5318 * In other words, plug in "ma" in "aff". The result is an affine expression
5319 * defined over the domain space of "ma".
5321 * If "aff" is represented by
5323 * (a(p) + b x + c(divs))/d
5325 * and ma is represented by
5327 * x = D(p) + F(y) + G(divs')
5329 * then the result is
5331 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5333 * The divs in the local space of the input are similarly adjusted
5334 * through a call to isl_local_space_preimage_multi_aff.
5336 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5337 __isl_take isl_multi_aff *ma)
5339 isl_aff *res = NULL;
5340 isl_local_space *ls;
5341 int n_div_aff, n_div_ma;
5342 isl_int f, c1, c2, g;
5344 ma = isl_multi_aff_align_divs(ma);
5345 if (!aff || !ma)
5346 goto error;
5348 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5349 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5351 ls = isl_aff_get_domain_local_space(aff);
5352 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5353 res = isl_aff_alloc(ls);
5354 if (!res)
5355 goto error;
5357 isl_int_init(f);
5358 isl_int_init(c1);
5359 isl_int_init(c2);
5360 isl_int_init(g);
5362 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5363 f, c1, c2, g, 1);
5365 isl_int_clear(f);
5366 isl_int_clear(c1);
5367 isl_int_clear(c2);
5368 isl_int_clear(g);
5370 isl_aff_free(aff);
5371 isl_multi_aff_free(ma);
5372 res = isl_aff_normalize(res);
5373 return res;
5374 error:
5375 isl_aff_free(aff);
5376 isl_multi_aff_free(ma);
5377 isl_aff_free(res);
5378 return NULL;
5381 /* Compute the pullback of "aff1" by the function represented by "aff2".
5382 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5383 * defined over the domain space of "aff1".
5385 * The domain of "aff1" should match the range of "aff2", which means
5386 * that it should be single-dimensional.
5388 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5389 __isl_take isl_aff *aff2)
5391 isl_multi_aff *ma;
5393 ma = isl_multi_aff_from_aff(aff2);
5394 return isl_aff_pullback_multi_aff(aff1, ma);
5397 /* Compute the pullback of "ma1" by the function represented by "ma2".
5398 * In other words, plug in "ma2" in "ma1".
5400 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5402 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5403 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5405 int i;
5406 isl_space *space = NULL;
5408 ma2 = isl_multi_aff_align_divs(ma2);
5409 ma1 = isl_multi_aff_cow(ma1);
5410 if (!ma1 || !ma2)
5411 goto error;
5413 space = isl_space_join(isl_multi_aff_get_space(ma2),
5414 isl_multi_aff_get_space(ma1));
5416 for (i = 0; i < ma1->n; ++i) {
5417 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5418 isl_multi_aff_copy(ma2));
5419 if (!ma1->p[i])
5420 goto error;
5423 ma1 = isl_multi_aff_reset_space(ma1, space);
5424 isl_multi_aff_free(ma2);
5425 return ma1;
5426 error:
5427 isl_space_free(space);
5428 isl_multi_aff_free(ma2);
5429 isl_multi_aff_free(ma1);
5430 return NULL;
5433 /* Compute the pullback of "ma1" by the function represented by "ma2".
5434 * In other words, plug in "ma2" in "ma1".
5436 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5437 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5439 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5440 &isl_multi_aff_pullback_multi_aff_aligned);
5443 /* Extend the local space of "dst" to include the divs
5444 * in the local space of "src".
5446 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5447 __isl_keep isl_aff *src)
5449 isl_ctx *ctx;
5450 int *exp1 = NULL;
5451 int *exp2 = NULL;
5452 isl_mat *div;
5454 if (!src || !dst)
5455 return isl_aff_free(dst);
5457 ctx = isl_aff_get_ctx(src);
5458 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5459 isl_die(ctx, isl_error_invalid,
5460 "spaces don't match", goto error);
5462 if (src->ls->div->n_row == 0)
5463 return dst;
5465 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5466 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5467 if (!exp1 || (dst->ls->div->n_row && !exp2))
5468 goto error;
5470 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5471 dst = isl_aff_expand_divs(dst, div, exp2);
5472 free(exp1);
5473 free(exp2);
5475 return dst;
5476 error:
5477 free(exp1);
5478 free(exp2);
5479 return isl_aff_free(dst);
5482 /* Adjust the local spaces of the affine expressions in "maff"
5483 * such that they all have the save divs.
5485 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5486 __isl_take isl_multi_aff *maff)
5488 int i;
5490 if (!maff)
5491 return NULL;
5492 if (maff->n == 0)
5493 return maff;
5494 maff = isl_multi_aff_cow(maff);
5495 if (!maff)
5496 return NULL;
5498 for (i = 1; i < maff->n; ++i)
5499 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5500 for (i = 1; i < maff->n; ++i) {
5501 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5502 if (!maff->p[i])
5503 return isl_multi_aff_free(maff);
5506 return maff;
5509 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5511 aff = isl_aff_cow(aff);
5512 if (!aff)
5513 return NULL;
5515 aff->ls = isl_local_space_lift(aff->ls);
5516 if (!aff->ls)
5517 return isl_aff_free(aff);
5519 return aff;
5522 /* Lift "maff" to a space with extra dimensions such that the result
5523 * has no more existentially quantified variables.
5524 * If "ls" is not NULL, then *ls is assigned the local space that lies
5525 * at the basis of the lifting applied to "maff".
5527 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5528 __isl_give isl_local_space **ls)
5530 int i;
5531 isl_space *space;
5532 unsigned n_div;
5534 if (ls)
5535 *ls = NULL;
5537 if (!maff)
5538 return NULL;
5540 if (maff->n == 0) {
5541 if (ls) {
5542 isl_space *space = isl_multi_aff_get_domain_space(maff);
5543 *ls = isl_local_space_from_space(space);
5544 if (!*ls)
5545 return isl_multi_aff_free(maff);
5547 return maff;
5550 maff = isl_multi_aff_cow(maff);
5551 maff = isl_multi_aff_align_divs(maff);
5552 if (!maff)
5553 return NULL;
5555 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5556 space = isl_multi_aff_get_space(maff);
5557 space = isl_space_lift(isl_space_domain(space), n_div);
5558 space = isl_space_extend_domain_with_range(space,
5559 isl_multi_aff_get_space(maff));
5560 if (!space)
5561 return isl_multi_aff_free(maff);
5562 isl_space_free(maff->space);
5563 maff->space = space;
5565 if (ls) {
5566 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5567 if (!*ls)
5568 return isl_multi_aff_free(maff);
5571 for (i = 0; i < maff->n; ++i) {
5572 maff->p[i] = isl_aff_lift(maff->p[i]);
5573 if (!maff->p[i])
5574 goto error;
5577 return maff;
5578 error:
5579 if (ls)
5580 isl_local_space_free(*ls);
5581 return isl_multi_aff_free(maff);
5585 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5587 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5588 __isl_keep isl_pw_multi_aff *pma, int pos)
5590 int i;
5591 int n_out;
5592 isl_space *space;
5593 isl_pw_aff *pa;
5595 if (!pma)
5596 return NULL;
5598 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5599 if (pos < 0 || pos >= n_out)
5600 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5601 "index out of bounds", return NULL);
5603 space = isl_pw_multi_aff_get_space(pma);
5604 space = isl_space_drop_dims(space, isl_dim_out,
5605 pos + 1, n_out - pos - 1);
5606 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5608 pa = isl_pw_aff_alloc_size(space, pma->n);
5609 for (i = 0; i < pma->n; ++i) {
5610 isl_aff *aff;
5611 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5612 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5615 return pa;
5618 /* Return an isl_pw_multi_aff with the given "set" as domain and
5619 * an unnamed zero-dimensional range.
5621 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5622 __isl_take isl_set *set)
5624 isl_multi_aff *ma;
5625 isl_space *space;
5627 space = isl_set_get_space(set);
5628 space = isl_space_from_domain(space);
5629 ma = isl_multi_aff_zero(space);
5630 return isl_pw_multi_aff_alloc(set, ma);
5633 /* Add an isl_pw_multi_aff with the given "set" as domain and
5634 * an unnamed zero-dimensional range to *user.
5636 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5637 void *user)
5639 isl_union_pw_multi_aff **upma = user;
5640 isl_pw_multi_aff *pma;
5642 pma = isl_pw_multi_aff_from_domain(set);
5643 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5645 return isl_stat_ok;
5648 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5649 * an unnamed zero-dimensional range.
5651 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5652 __isl_take isl_union_set *uset)
5654 isl_space *space;
5655 isl_union_pw_multi_aff *upma;
5657 if (!uset)
5658 return NULL;
5660 space = isl_union_set_get_space(uset);
5661 upma = isl_union_pw_multi_aff_empty(space);
5663 if (isl_union_set_foreach_set(uset,
5664 &add_pw_multi_aff_from_domain, &upma) < 0)
5665 goto error;
5667 isl_union_set_free(uset);
5668 return upma;
5669 error:
5670 isl_union_set_free(uset);
5671 isl_union_pw_multi_aff_free(upma);
5672 return NULL;
5675 /* Convert "pma" to an isl_map and add it to *umap.
5677 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5678 void *user)
5680 isl_union_map **umap = user;
5681 isl_map *map;
5683 map = isl_map_from_pw_multi_aff(pma);
5684 *umap = isl_union_map_add_map(*umap, map);
5686 return isl_stat_ok;
5689 /* Construct a union map mapping the domain of the union
5690 * piecewise multi-affine expression to its range, with each dimension
5691 * in the range equated to the corresponding affine expression on its cell.
5693 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5694 __isl_take isl_union_pw_multi_aff *upma)
5696 isl_space *space;
5697 isl_union_map *umap;
5699 if (!upma)
5700 return NULL;
5702 space = isl_union_pw_multi_aff_get_space(upma);
5703 umap = isl_union_map_empty(space);
5705 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5706 &map_from_pw_multi_aff, &umap) < 0)
5707 goto error;
5709 isl_union_pw_multi_aff_free(upma);
5710 return umap;
5711 error:
5712 isl_union_pw_multi_aff_free(upma);
5713 isl_union_map_free(umap);
5714 return NULL;
5717 /* Local data for bin_entry and the callback "fn".
5719 struct isl_union_pw_multi_aff_bin_data {
5720 isl_union_pw_multi_aff *upma2;
5721 isl_union_pw_multi_aff *res;
5722 isl_pw_multi_aff *pma;
5723 isl_stat (*fn)(void **entry, void *user);
5726 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5727 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5729 static isl_stat bin_entry(void **entry, void *user)
5731 struct isl_union_pw_multi_aff_bin_data *data = user;
5732 isl_pw_multi_aff *pma = *entry;
5734 data->pma = pma;
5735 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5736 data->fn, data) < 0)
5737 return isl_stat_error;
5739 return isl_stat_ok;
5742 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5743 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5744 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5745 * as *entry. The callback should adjust data->res if desired.
5747 static __isl_give isl_union_pw_multi_aff *bin_op(
5748 __isl_take isl_union_pw_multi_aff *upma1,
5749 __isl_take isl_union_pw_multi_aff *upma2,
5750 isl_stat (*fn)(void **entry, void *user))
5752 isl_space *space;
5753 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5755 space = isl_union_pw_multi_aff_get_space(upma2);
5756 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5757 space = isl_union_pw_multi_aff_get_space(upma1);
5758 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5760 if (!upma1 || !upma2)
5761 goto error;
5763 data.upma2 = upma2;
5764 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5765 upma1->table.n);
5766 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5767 &bin_entry, &data) < 0)
5768 goto error;
5770 isl_union_pw_multi_aff_free(upma1);
5771 isl_union_pw_multi_aff_free(upma2);
5772 return data.res;
5773 error:
5774 isl_union_pw_multi_aff_free(upma1);
5775 isl_union_pw_multi_aff_free(upma2);
5776 isl_union_pw_multi_aff_free(data.res);
5777 return NULL;
5780 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5781 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5783 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5784 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5786 isl_space *space;
5788 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5789 isl_pw_multi_aff_get_space(pma2));
5790 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5791 &isl_multi_aff_range_product);
5794 /* Given two isl_pw_multi_affs A -> B and C -> D,
5795 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5797 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5798 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5800 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5801 &pw_multi_aff_range_product);
5804 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5805 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5807 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5808 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5810 isl_space *space;
5812 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5813 isl_pw_multi_aff_get_space(pma2));
5814 space = isl_space_flatten_range(space);
5815 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5816 &isl_multi_aff_flat_range_product);
5819 /* Given two isl_pw_multi_affs A -> B and C -> D,
5820 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5822 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5823 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5825 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5826 &pw_multi_aff_flat_range_product);
5829 /* If data->pma and *entry have the same domain space, then compute
5830 * their flat range product and the result to data->res.
5832 static isl_stat flat_range_product_entry(void **entry, void *user)
5834 struct isl_union_pw_multi_aff_bin_data *data = user;
5835 isl_pw_multi_aff *pma2 = *entry;
5837 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5838 pma2->dim, isl_dim_in))
5839 return isl_stat_ok;
5841 pma2 = isl_pw_multi_aff_flat_range_product(
5842 isl_pw_multi_aff_copy(data->pma),
5843 isl_pw_multi_aff_copy(pma2));
5845 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5847 return isl_stat_ok;
5850 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5851 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5853 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5854 __isl_take isl_union_pw_multi_aff *upma1,
5855 __isl_take isl_union_pw_multi_aff *upma2)
5857 return bin_op(upma1, upma2, &flat_range_product_entry);
5860 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5861 * The parameters are assumed to have been aligned.
5863 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5864 * except that it works on two different isl_pw_* types.
5866 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5867 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5868 __isl_take isl_pw_aff *pa)
5870 int i, j, n;
5871 isl_pw_multi_aff *res = NULL;
5873 if (!pma || !pa)
5874 goto error;
5876 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5877 pa->dim, isl_dim_in))
5878 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5879 "domains don't match", goto error);
5880 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5881 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5882 "index out of bounds", goto error);
5884 n = pma->n * pa->n;
5885 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5887 for (i = 0; i < pma->n; ++i) {
5888 for (j = 0; j < pa->n; ++j) {
5889 isl_set *common;
5890 isl_multi_aff *res_ij;
5891 int empty;
5893 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5894 isl_set_copy(pa->p[j].set));
5895 empty = isl_set_plain_is_empty(common);
5896 if (empty < 0 || empty) {
5897 isl_set_free(common);
5898 if (empty < 0)
5899 goto error;
5900 continue;
5903 res_ij = isl_multi_aff_set_aff(
5904 isl_multi_aff_copy(pma->p[i].maff), pos,
5905 isl_aff_copy(pa->p[j].aff));
5906 res_ij = isl_multi_aff_gist(res_ij,
5907 isl_set_copy(common));
5909 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5913 isl_pw_multi_aff_free(pma);
5914 isl_pw_aff_free(pa);
5915 return res;
5916 error:
5917 isl_pw_multi_aff_free(pma);
5918 isl_pw_aff_free(pa);
5919 return isl_pw_multi_aff_free(res);
5922 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5924 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5925 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5926 __isl_take isl_pw_aff *pa)
5928 if (!pma || !pa)
5929 goto error;
5930 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5931 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5932 if (!isl_space_has_named_params(pma->dim) ||
5933 !isl_space_has_named_params(pa->dim))
5934 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5935 "unaligned unnamed parameters", goto error);
5936 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5937 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5938 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5939 error:
5940 isl_pw_multi_aff_free(pma);
5941 isl_pw_aff_free(pa);
5942 return NULL;
5945 /* Do the parameters of "pa" match those of "space"?
5947 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5948 __isl_keep isl_space *space)
5950 isl_space *pa_space;
5951 int match;
5953 if (!pa || !space)
5954 return -1;
5956 pa_space = isl_pw_aff_get_space(pa);
5958 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5960 isl_space_free(pa_space);
5961 return match;
5964 /* Check that the domain space of "pa" matches "space".
5966 * Return 0 on success and -1 on error.
5968 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5969 __isl_keep isl_space *space)
5971 isl_space *pa_space;
5972 int match;
5974 if (!pa || !space)
5975 return -1;
5977 pa_space = isl_pw_aff_get_space(pa);
5979 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5980 if (match < 0)
5981 goto error;
5982 if (!match)
5983 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5984 "parameters don't match", goto error);
5985 match = isl_space_tuple_is_equal(space, isl_dim_in,
5986 pa_space, isl_dim_in);
5987 if (match < 0)
5988 goto error;
5989 if (!match)
5990 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5991 "domains don't match", goto error);
5992 isl_space_free(pa_space);
5993 return 0;
5994 error:
5995 isl_space_free(pa_space);
5996 return -1;
5999 #undef BASE
6000 #define BASE pw_aff
6001 #undef DOMBASE
6002 #define DOMBASE set
6004 #include <isl_multi_templ.c>
6005 #include <isl_multi_apply_set.c>
6006 #include <isl_multi_gist.c>
6007 #include <isl_multi_intersect.c>
6009 /* Scale the elements of "pma" by the corresponding elements of "mv".
6011 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6012 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6014 int i;
6016 pma = isl_pw_multi_aff_cow(pma);
6017 if (!pma || !mv)
6018 goto error;
6019 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6020 mv->space, isl_dim_set))
6021 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6022 "spaces don't match", goto error);
6023 if (!isl_space_match(pma->dim, isl_dim_param,
6024 mv->space, isl_dim_param)) {
6025 pma = isl_pw_multi_aff_align_params(pma,
6026 isl_multi_val_get_space(mv));
6027 mv = isl_multi_val_align_params(mv,
6028 isl_pw_multi_aff_get_space(pma));
6029 if (!pma || !mv)
6030 goto error;
6033 for (i = 0; i < pma->n; ++i) {
6034 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6035 isl_multi_val_copy(mv));
6036 if (!pma->p[i].maff)
6037 goto error;
6040 isl_multi_val_free(mv);
6041 return pma;
6042 error:
6043 isl_multi_val_free(mv);
6044 isl_pw_multi_aff_free(pma);
6045 return NULL;
6048 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
6049 * mv contains the mv argument.
6050 * res collects the results.
6052 struct isl_union_pw_multi_aff_scale_multi_val_data {
6053 isl_multi_val *mv;
6054 isl_union_pw_multi_aff *res;
6057 /* This function is called for each entry of an isl_union_pw_multi_aff.
6058 * If the space of the entry matches that of data->mv,
6059 * then apply isl_pw_multi_aff_scale_multi_val and add the result
6060 * to data->res.
6062 static isl_stat union_pw_multi_aff_scale_multi_val_entry(void **entry,
6063 void *user)
6065 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
6066 isl_pw_multi_aff *pma = *entry;
6068 if (!pma)
6069 return isl_stat_error;
6070 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6071 data->mv->space, isl_dim_set))
6072 return isl_stat_ok;
6074 pma = isl_pw_multi_aff_copy(pma);
6075 pma = isl_pw_multi_aff_scale_multi_val(pma,
6076 isl_multi_val_copy(data->mv));
6077 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6078 if (!data->res)
6079 return isl_stat_error;
6081 return isl_stat_ok;
6084 /* Scale the elements of "upma" by the corresponding elements of "mv",
6085 * for those entries that match the space of "mv".
6087 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6088 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6090 struct isl_union_pw_multi_aff_scale_multi_val_data data;
6092 upma = isl_union_pw_multi_aff_align_params(upma,
6093 isl_multi_val_get_space(mv));
6094 mv = isl_multi_val_align_params(mv,
6095 isl_union_pw_multi_aff_get_space(upma));
6096 if (!upma || !mv)
6097 goto error;
6099 data.mv = mv;
6100 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
6101 upma->table.n);
6102 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
6103 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
6104 goto error;
6106 isl_multi_val_free(mv);
6107 isl_union_pw_multi_aff_free(upma);
6108 return data.res;
6109 error:
6110 isl_multi_val_free(mv);
6111 isl_union_pw_multi_aff_free(upma);
6112 return NULL;
6115 /* Construct and return a piecewise multi affine expression
6116 * in the given space with value zero in each of the output dimensions and
6117 * a universe domain.
6119 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6121 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6124 /* Construct and return a piecewise multi affine expression
6125 * that is equal to the given piecewise affine expression.
6127 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6128 __isl_take isl_pw_aff *pa)
6130 int i;
6131 isl_space *space;
6132 isl_pw_multi_aff *pma;
6134 if (!pa)
6135 return NULL;
6137 space = isl_pw_aff_get_space(pa);
6138 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6140 for (i = 0; i < pa->n; ++i) {
6141 isl_set *set;
6142 isl_multi_aff *ma;
6144 set = isl_set_copy(pa->p[i].set);
6145 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6146 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6149 isl_pw_aff_free(pa);
6150 return pma;
6153 /* Construct a set or map mapping the shared (parameter) domain
6154 * of the piecewise affine expressions to the range of "mpa"
6155 * with each dimension in the range equated to the
6156 * corresponding piecewise affine expression.
6158 static __isl_give isl_map *map_from_multi_pw_aff(
6159 __isl_take isl_multi_pw_aff *mpa)
6161 int i;
6162 isl_space *space;
6163 isl_map *map;
6165 if (!mpa)
6166 return NULL;
6168 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6169 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6170 "invalid space", goto error);
6172 space = isl_multi_pw_aff_get_domain_space(mpa);
6173 map = isl_map_universe(isl_space_from_domain(space));
6175 for (i = 0; i < mpa->n; ++i) {
6176 isl_pw_aff *pa;
6177 isl_map *map_i;
6179 pa = isl_pw_aff_copy(mpa->p[i]);
6180 map_i = map_from_pw_aff(pa);
6182 map = isl_map_flat_range_product(map, map_i);
6185 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6187 isl_multi_pw_aff_free(mpa);
6188 return map;
6189 error:
6190 isl_multi_pw_aff_free(mpa);
6191 return NULL;
6194 /* Construct a map mapping the shared domain
6195 * of the piecewise affine expressions to the range of "mpa"
6196 * with each dimension in the range equated to the
6197 * corresponding piecewise affine expression.
6199 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6201 if (!mpa)
6202 return NULL;
6203 if (isl_space_is_set(mpa->space))
6204 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6205 "space of input is not a map", goto error);
6207 return map_from_multi_pw_aff(mpa);
6208 error:
6209 isl_multi_pw_aff_free(mpa);
6210 return NULL;
6213 /* Construct a set mapping the shared parameter domain
6214 * of the piecewise affine expressions to the space of "mpa"
6215 * with each dimension in the range equated to the
6216 * corresponding piecewise affine expression.
6218 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6220 if (!mpa)
6221 return NULL;
6222 if (!isl_space_is_set(mpa->space))
6223 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6224 "space of input is not a set", goto error);
6226 return map_from_multi_pw_aff(mpa);
6227 error:
6228 isl_multi_pw_aff_free(mpa);
6229 return NULL;
6232 /* Construct and return a piecewise multi affine expression
6233 * that is equal to the given multi piecewise affine expression
6234 * on the shared domain of the piecewise affine expressions.
6236 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6237 __isl_take isl_multi_pw_aff *mpa)
6239 int i;
6240 isl_space *space;
6241 isl_pw_aff *pa;
6242 isl_pw_multi_aff *pma;
6244 if (!mpa)
6245 return NULL;
6247 space = isl_multi_pw_aff_get_space(mpa);
6249 if (mpa->n == 0) {
6250 isl_multi_pw_aff_free(mpa);
6251 return isl_pw_multi_aff_zero(space);
6254 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6255 pma = isl_pw_multi_aff_from_pw_aff(pa);
6257 for (i = 1; i < mpa->n; ++i) {
6258 isl_pw_multi_aff *pma_i;
6260 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6261 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6262 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6265 pma = isl_pw_multi_aff_reset_space(pma, space);
6267 isl_multi_pw_aff_free(mpa);
6268 return pma;
6271 /* Construct and return a multi piecewise affine expression
6272 * that is equal to the given multi affine expression.
6274 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6275 __isl_take isl_multi_aff *ma)
6277 int i, n;
6278 isl_multi_pw_aff *mpa;
6280 if (!ma)
6281 return NULL;
6283 n = isl_multi_aff_dim(ma, isl_dim_out);
6284 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6286 for (i = 0; i < n; ++i) {
6287 isl_pw_aff *pa;
6289 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6290 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6293 isl_multi_aff_free(ma);
6294 return mpa;
6297 /* Construct and return a multi piecewise affine expression
6298 * that is equal to the given piecewise multi affine expression.
6300 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6301 __isl_take isl_pw_multi_aff *pma)
6303 int i, n;
6304 isl_space *space;
6305 isl_multi_pw_aff *mpa;
6307 if (!pma)
6308 return NULL;
6310 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6311 space = isl_pw_multi_aff_get_space(pma);
6312 mpa = isl_multi_pw_aff_alloc(space);
6314 for (i = 0; i < n; ++i) {
6315 isl_pw_aff *pa;
6317 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6318 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6321 isl_pw_multi_aff_free(pma);
6322 return mpa;
6325 /* Do "pa1" and "pa2" represent the same function?
6327 * We first check if they are obviously equal.
6328 * If not, we convert them to maps and check if those are equal.
6330 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6332 int equal;
6333 isl_map *map1, *map2;
6335 if (!pa1 || !pa2)
6336 return -1;
6338 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6339 if (equal < 0 || equal)
6340 return equal;
6342 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6343 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6344 equal = isl_map_is_equal(map1, map2);
6345 isl_map_free(map1);
6346 isl_map_free(map2);
6348 return equal;
6351 /* Do "mpa1" and "mpa2" represent the same function?
6353 * Note that we cannot convert the entire isl_multi_pw_aff
6354 * to a map because the domains of the piecewise affine expressions
6355 * may not be the same.
6357 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6358 __isl_keep isl_multi_pw_aff *mpa2)
6360 int i;
6361 isl_bool equal;
6363 if (!mpa1 || !mpa2)
6364 return isl_bool_error;
6366 if (!isl_space_match(mpa1->space, isl_dim_param,
6367 mpa2->space, isl_dim_param)) {
6368 if (!isl_space_has_named_params(mpa1->space))
6369 return isl_bool_false;
6370 if (!isl_space_has_named_params(mpa2->space))
6371 return isl_bool_false;
6372 mpa1 = isl_multi_pw_aff_copy(mpa1);
6373 mpa2 = isl_multi_pw_aff_copy(mpa2);
6374 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6375 isl_multi_pw_aff_get_space(mpa2));
6376 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6377 isl_multi_pw_aff_get_space(mpa1));
6378 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6379 isl_multi_pw_aff_free(mpa1);
6380 isl_multi_pw_aff_free(mpa2);
6381 return equal;
6384 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6385 if (equal < 0 || !equal)
6386 return equal;
6388 for (i = 0; i < mpa1->n; ++i) {
6389 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6390 if (equal < 0 || !equal)
6391 return equal;
6394 return isl_bool_true;
6397 /* Coalesce the elements of "mpa".
6399 * Note that such coalescing does not change the meaning of "mpa"
6400 * so there is no need to cow. We do need to be careful not to
6401 * destroy any other copies of "mpa" in case of failure.
6403 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6404 __isl_take isl_multi_pw_aff *mpa)
6406 int i;
6408 if (!mpa)
6409 return NULL;
6411 for (i = 0; i < mpa->n; ++i) {
6412 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6413 pa = isl_pw_aff_coalesce(pa);
6414 if (!pa)
6415 return isl_multi_pw_aff_free(mpa);
6416 isl_pw_aff_free(mpa->p[i]);
6417 mpa->p[i] = pa;
6420 return mpa;
6423 /* Compute the pullback of "mpa" by the function represented by "ma".
6424 * In other words, plug in "ma" in "mpa".
6426 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6428 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6429 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6431 int i;
6432 isl_space *space = NULL;
6434 mpa = isl_multi_pw_aff_cow(mpa);
6435 if (!mpa || !ma)
6436 goto error;
6438 space = isl_space_join(isl_multi_aff_get_space(ma),
6439 isl_multi_pw_aff_get_space(mpa));
6440 if (!space)
6441 goto error;
6443 for (i = 0; i < mpa->n; ++i) {
6444 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6445 isl_multi_aff_copy(ma));
6446 if (!mpa->p[i])
6447 goto error;
6450 isl_multi_aff_free(ma);
6451 isl_space_free(mpa->space);
6452 mpa->space = space;
6453 return mpa;
6454 error:
6455 isl_space_free(space);
6456 isl_multi_pw_aff_free(mpa);
6457 isl_multi_aff_free(ma);
6458 return NULL;
6461 /* Compute the pullback of "mpa" by the function represented by "ma".
6462 * In other words, plug in "ma" in "mpa".
6464 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6465 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6467 if (!mpa || !ma)
6468 goto error;
6469 if (isl_space_match(mpa->space, isl_dim_param,
6470 ma->space, isl_dim_param))
6471 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6472 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6473 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6474 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6475 error:
6476 isl_multi_pw_aff_free(mpa);
6477 isl_multi_aff_free(ma);
6478 return NULL;
6481 /* Compute the pullback of "mpa" by the function represented by "pma".
6482 * In other words, plug in "pma" in "mpa".
6484 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6486 static __isl_give isl_multi_pw_aff *
6487 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6488 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6490 int i;
6491 isl_space *space = NULL;
6493 mpa = isl_multi_pw_aff_cow(mpa);
6494 if (!mpa || !pma)
6495 goto error;
6497 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6498 isl_multi_pw_aff_get_space(mpa));
6500 for (i = 0; i < mpa->n; ++i) {
6501 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6502 isl_pw_multi_aff_copy(pma));
6503 if (!mpa->p[i])
6504 goto error;
6507 isl_pw_multi_aff_free(pma);
6508 isl_space_free(mpa->space);
6509 mpa->space = space;
6510 return mpa;
6511 error:
6512 isl_space_free(space);
6513 isl_multi_pw_aff_free(mpa);
6514 isl_pw_multi_aff_free(pma);
6515 return NULL;
6518 /* Compute the pullback of "mpa" by the function represented by "pma".
6519 * In other words, plug in "pma" in "mpa".
6521 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6522 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6524 if (!mpa || !pma)
6525 goto error;
6526 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6527 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6528 mpa = isl_multi_pw_aff_align_params(mpa,
6529 isl_pw_multi_aff_get_space(pma));
6530 pma = isl_pw_multi_aff_align_params(pma,
6531 isl_multi_pw_aff_get_space(mpa));
6532 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6533 error:
6534 isl_multi_pw_aff_free(mpa);
6535 isl_pw_multi_aff_free(pma);
6536 return NULL;
6539 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6540 * with the domain of "aff". The domain of the result is the same
6541 * as that of "mpa".
6542 * "mpa" and "aff" are assumed to have been aligned.
6544 * We first extract the parametric constant from "aff", defined
6545 * over the correct domain.
6546 * Then we add the appropriate combinations of the members of "mpa".
6547 * Finally, we add the integer divisions through recursive calls.
6549 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6550 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6552 int i, n_in, n_div;
6553 isl_space *space;
6554 isl_val *v;
6555 isl_pw_aff *pa;
6556 isl_aff *tmp;
6558 n_in = isl_aff_dim(aff, isl_dim_in);
6559 n_div = isl_aff_dim(aff, isl_dim_div);
6561 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6562 tmp = isl_aff_copy(aff);
6563 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6564 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6565 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6566 isl_space_dim(space, isl_dim_set));
6567 tmp = isl_aff_reset_domain_space(tmp, space);
6568 pa = isl_pw_aff_from_aff(tmp);
6570 for (i = 0; i < n_in; ++i) {
6571 isl_pw_aff *pa_i;
6573 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6574 continue;
6575 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6576 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6577 pa_i = isl_pw_aff_scale_val(pa_i, v);
6578 pa = isl_pw_aff_add(pa, pa_i);
6581 for (i = 0; i < n_div; ++i) {
6582 isl_aff *div;
6583 isl_pw_aff *pa_i;
6585 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6586 continue;
6587 div = isl_aff_get_div(aff, i);
6588 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6589 isl_multi_pw_aff_copy(mpa), div);
6590 pa_i = isl_pw_aff_floor(pa_i);
6591 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6592 pa_i = isl_pw_aff_scale_val(pa_i, v);
6593 pa = isl_pw_aff_add(pa, pa_i);
6596 isl_multi_pw_aff_free(mpa);
6597 isl_aff_free(aff);
6599 return pa;
6602 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6603 * with the domain of "aff". The domain of the result is the same
6604 * as that of "mpa".
6606 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6607 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6609 if (!aff || !mpa)
6610 goto error;
6611 if (isl_space_match(aff->ls->dim, isl_dim_param,
6612 mpa->space, isl_dim_param))
6613 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6615 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6616 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6618 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6619 error:
6620 isl_aff_free(aff);
6621 isl_multi_pw_aff_free(mpa);
6622 return NULL;
6625 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6626 * with the domain of "pa". The domain of the result is the same
6627 * as that of "mpa".
6628 * "mpa" and "pa" are assumed to have been aligned.
6630 * We consider each piece in turn. Note that the domains of the
6631 * pieces are assumed to be disjoint and they remain disjoint
6632 * after taking the preimage (over the same function).
6634 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6635 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6637 isl_space *space;
6638 isl_pw_aff *res;
6639 int i;
6641 if (!mpa || !pa)
6642 goto error;
6644 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6645 isl_pw_aff_get_space(pa));
6646 res = isl_pw_aff_empty(space);
6648 for (i = 0; i < pa->n; ++i) {
6649 isl_pw_aff *pa_i;
6650 isl_set *domain;
6652 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6653 isl_multi_pw_aff_copy(mpa),
6654 isl_aff_copy(pa->p[i].aff));
6655 domain = isl_set_copy(pa->p[i].set);
6656 domain = isl_set_preimage_multi_pw_aff(domain,
6657 isl_multi_pw_aff_copy(mpa));
6658 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6659 res = isl_pw_aff_add_disjoint(res, pa_i);
6662 isl_pw_aff_free(pa);
6663 isl_multi_pw_aff_free(mpa);
6664 return res;
6665 error:
6666 isl_pw_aff_free(pa);
6667 isl_multi_pw_aff_free(mpa);
6668 return NULL;
6671 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6672 * with the domain of "pa". The domain of the result is the same
6673 * as that of "mpa".
6675 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6676 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6678 if (!pa || !mpa)
6679 goto error;
6680 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6681 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6683 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6684 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6686 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6687 error:
6688 isl_pw_aff_free(pa);
6689 isl_multi_pw_aff_free(mpa);
6690 return NULL;
6693 /* Compute the pullback of "pa" by the function represented by "mpa".
6694 * In other words, plug in "mpa" in "pa".
6695 * "pa" and "mpa" are assumed to have been aligned.
6697 * The pullback is computed by applying "pa" to "mpa".
6699 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6700 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6702 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6705 /* Compute the pullback of "pa" by the function represented by "mpa".
6706 * In other words, plug in "mpa" in "pa".
6708 * The pullback is computed by applying "pa" to "mpa".
6710 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6711 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6713 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6716 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6717 * In other words, plug in "mpa2" in "mpa1".
6719 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6721 * We pullback each member of "mpa1" in turn.
6723 static __isl_give isl_multi_pw_aff *
6724 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6725 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6727 int i;
6728 isl_space *space = NULL;
6730 mpa1 = isl_multi_pw_aff_cow(mpa1);
6731 if (!mpa1 || !mpa2)
6732 goto error;
6734 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6735 isl_multi_pw_aff_get_space(mpa1));
6737 for (i = 0; i < mpa1->n; ++i) {
6738 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6739 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6740 if (!mpa1->p[i])
6741 goto error;
6744 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6746 isl_multi_pw_aff_free(mpa2);
6747 return mpa1;
6748 error:
6749 isl_space_free(space);
6750 isl_multi_pw_aff_free(mpa1);
6751 isl_multi_pw_aff_free(mpa2);
6752 return NULL;
6755 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6756 * In other words, plug in "mpa2" in "mpa1".
6758 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6759 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6761 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6762 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6765 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6766 * of "mpa1" and "mpa2" live in the same space, construct map space
6767 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6768 * with this map space as extract argument.
6770 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6771 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6772 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6773 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6775 int match;
6776 isl_space *space1, *space2;
6777 isl_map *res;
6779 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6780 isl_multi_pw_aff_get_space(mpa2));
6781 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6782 isl_multi_pw_aff_get_space(mpa1));
6783 if (!mpa1 || !mpa2)
6784 goto error;
6785 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6786 mpa2->space, isl_dim_out);
6787 if (match < 0)
6788 goto error;
6789 if (!match)
6790 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6791 "range spaces don't match", goto error);
6792 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6793 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6794 space1 = isl_space_map_from_domain_and_range(space1, space2);
6796 res = order(mpa1, mpa2, space1);
6797 isl_multi_pw_aff_free(mpa1);
6798 isl_multi_pw_aff_free(mpa2);
6799 return res;
6800 error:
6801 isl_multi_pw_aff_free(mpa1);
6802 isl_multi_pw_aff_free(mpa2);
6803 return NULL;
6806 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6807 * where the function values are equal. "space" is the space of the result.
6808 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6810 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6811 * in the sequences are equal.
6813 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6814 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6815 __isl_take isl_space *space)
6817 int i, n;
6818 isl_map *res;
6820 res = isl_map_universe(space);
6822 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6823 for (i = 0; i < n; ++i) {
6824 isl_pw_aff *pa1, *pa2;
6825 isl_map *map;
6827 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6828 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6829 map = isl_pw_aff_eq_map(pa1, pa2);
6830 res = isl_map_intersect(res, map);
6833 return res;
6836 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6837 * where the function values are equal.
6839 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6840 __isl_take isl_multi_pw_aff *mpa2)
6842 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6843 &isl_multi_pw_aff_eq_map_on_space);
6846 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6847 * where the function values of "mpa1" is lexicographically satisfies "base"
6848 * compared to that of "mpa2". "space" is the space of the result.
6849 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6851 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6852 * if its i-th element satisfies "base" when compared to
6853 * the i-th element of "mpa2" while all previous elements are
6854 * pairwise equal.
6856 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6857 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6858 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6859 __isl_take isl_pw_aff *pa2),
6860 __isl_take isl_space *space)
6862 int i, n;
6863 isl_map *res, *rest;
6865 res = isl_map_empty(isl_space_copy(space));
6866 rest = isl_map_universe(space);
6868 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6869 for (i = 0; i < n; ++i) {
6870 isl_pw_aff *pa1, *pa2;
6871 isl_map *map;
6873 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6874 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6875 map = base(pa1, pa2);
6876 map = isl_map_intersect(map, isl_map_copy(rest));
6877 res = isl_map_union(res, map);
6879 if (i == n - 1)
6880 continue;
6882 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6883 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6884 map = isl_pw_aff_eq_map(pa1, pa2);
6885 rest = isl_map_intersect(rest, map);
6888 isl_map_free(rest);
6889 return res;
6892 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6893 * where the function value of "mpa1" is lexicographically less than that
6894 * of "mpa2". "space" is the space of the result.
6895 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6897 * "mpa1" is less than "mpa2" if its i-th element is smaller
6898 * than the i-th element of "mpa2" while all previous elements are
6899 * pairwise equal.
6901 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6902 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6903 __isl_take isl_space *space)
6905 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6906 &isl_pw_aff_lt_map, space);
6909 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6910 * where the function value of "mpa1" is lexicographically less than that
6911 * of "mpa2".
6913 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6914 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6916 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6917 &isl_multi_pw_aff_lex_lt_map_on_space);
6920 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6921 * where the function value of "mpa1" is lexicographically greater than that
6922 * of "mpa2". "space" is the space of the result.
6923 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6925 * "mpa1" is greater than "mpa2" if its i-th element is greater
6926 * than the i-th element of "mpa2" while all previous elements are
6927 * pairwise equal.
6929 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6930 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6931 __isl_take isl_space *space)
6933 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6934 &isl_pw_aff_gt_map, space);
6937 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6938 * where the function value of "mpa1" is lexicographically greater than that
6939 * of "mpa2".
6941 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
6942 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6944 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6945 &isl_multi_pw_aff_lex_gt_map_on_space);
6948 /* Compare two isl_affs.
6950 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6951 * than "aff2" and 0 if they are equal.
6953 * The order is fairly arbitrary. We do consider expressions that only involve
6954 * earlier dimensions as "smaller".
6956 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6958 int cmp;
6959 int last1, last2;
6961 if (aff1 == aff2)
6962 return 0;
6964 if (!aff1)
6965 return -1;
6966 if (!aff2)
6967 return 1;
6969 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6970 if (cmp != 0)
6971 return cmp;
6973 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6974 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6975 if (last1 != last2)
6976 return last1 - last2;
6978 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6981 /* Compare two isl_pw_affs.
6983 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6984 * than "pa2" and 0 if they are equal.
6986 * The order is fairly arbitrary. We do consider expressions that only involve
6987 * earlier dimensions as "smaller".
6989 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6990 __isl_keep isl_pw_aff *pa2)
6992 int i;
6993 int cmp;
6995 if (pa1 == pa2)
6996 return 0;
6998 if (!pa1)
6999 return -1;
7000 if (!pa2)
7001 return 1;
7003 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7004 if (cmp != 0)
7005 return cmp;
7007 if (pa1->n != pa2->n)
7008 return pa1->n - pa2->n;
7010 for (i = 0; i < pa1->n; ++i) {
7011 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7012 if (cmp != 0)
7013 return cmp;
7014 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7015 if (cmp != 0)
7016 return cmp;
7019 return 0;
7022 /* Return a piecewise affine expression that is equal to "v" on "domain".
7024 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7025 __isl_take isl_val *v)
7027 isl_space *space;
7028 isl_local_space *ls;
7029 isl_aff *aff;
7031 space = isl_set_get_space(domain);
7032 ls = isl_local_space_from_space(space);
7033 aff = isl_aff_val_on_domain(ls, v);
7035 return isl_pw_aff_alloc(domain, aff);
7038 /* Return a multi affine expression that is equal to "mv" on domain
7039 * space "space".
7041 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7042 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7044 int i, n;
7045 isl_space *space2;
7046 isl_local_space *ls;
7047 isl_multi_aff *ma;
7049 if (!space || !mv)
7050 goto error;
7052 n = isl_multi_val_dim(mv, isl_dim_set);
7053 space2 = isl_multi_val_get_space(mv);
7054 space2 = isl_space_align_params(space2, isl_space_copy(space));
7055 space = isl_space_align_params(space, isl_space_copy(space2));
7056 space = isl_space_map_from_domain_and_range(space, space2);
7057 ma = isl_multi_aff_alloc(isl_space_copy(space));
7058 ls = isl_local_space_from_space(isl_space_domain(space));
7059 for (i = 0; i < n; ++i) {
7060 isl_val *v;
7061 isl_aff *aff;
7063 v = isl_multi_val_get_val(mv, i);
7064 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7065 ma = isl_multi_aff_set_aff(ma, i, aff);
7067 isl_local_space_free(ls);
7069 isl_multi_val_free(mv);
7070 return ma;
7071 error:
7072 isl_space_free(space);
7073 isl_multi_val_free(mv);
7074 return NULL;
7077 /* Return a piecewise multi-affine expression
7078 * that is equal to "mv" on "domain".
7080 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7081 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7083 isl_space *space;
7084 isl_multi_aff *ma;
7086 space = isl_set_get_space(domain);
7087 ma = isl_multi_aff_multi_val_on_space(space, mv);
7089 return isl_pw_multi_aff_alloc(domain, ma);
7092 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7093 * mv is the value that should be attained on each domain set
7094 * res collects the results
7096 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7097 isl_multi_val *mv;
7098 isl_union_pw_multi_aff *res;
7101 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7102 * and add it to data->res.
7104 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7105 void *user)
7107 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7108 isl_pw_multi_aff *pma;
7109 isl_multi_val *mv;
7111 mv = isl_multi_val_copy(data->mv);
7112 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7113 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7115 return data->res ? isl_stat_ok : isl_stat_error;
7118 /* Return a union piecewise multi-affine expression
7119 * that is equal to "mv" on "domain".
7121 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7122 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7124 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7125 isl_space *space;
7127 space = isl_union_set_get_space(domain);
7128 data.res = isl_union_pw_multi_aff_empty(space);
7129 data.mv = mv;
7130 if (isl_union_set_foreach_set(domain,
7131 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7132 data.res = isl_union_pw_multi_aff_free(data.res);
7133 isl_union_set_free(domain);
7134 isl_multi_val_free(mv);
7135 return data.res;
7138 /* Compute the pullback of data->pma by the function represented by "pma2",
7139 * provided the spaces match, and add the results to data->res.
7141 static isl_stat pullback_entry(void **entry, void *user)
7143 struct isl_union_pw_multi_aff_bin_data *data = user;
7144 isl_pw_multi_aff *pma2 = *entry;
7146 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7147 pma2->dim, isl_dim_out))
7148 return isl_stat_ok;
7150 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7151 isl_pw_multi_aff_copy(data->pma),
7152 isl_pw_multi_aff_copy(pma2));
7154 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7155 if (!data->res)
7156 return isl_stat_error;
7158 return isl_stat_ok;
7161 /* Compute the pullback of "upma1" by the function represented by "upma2".
7163 __isl_give isl_union_pw_multi_aff *
7164 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7165 __isl_take isl_union_pw_multi_aff *upma1,
7166 __isl_take isl_union_pw_multi_aff *upma2)
7168 return bin_op(upma1, upma2, &pullback_entry);
7171 /* Check that the domain space of "upa" matches "space".
7173 * Return 0 on success and -1 on error.
7175 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7176 * can in principle never fail since the space "space" is that
7177 * of the isl_multi_union_pw_aff and is a set space such that
7178 * there is no domain space to match.
7180 * We check the parameters and double-check that "space" is
7181 * indeed that of a set.
7183 static int isl_union_pw_aff_check_match_domain_space(
7184 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7186 isl_space *upa_space;
7187 int match;
7189 if (!upa || !space)
7190 return -1;
7192 match = isl_space_is_set(space);
7193 if (match < 0)
7194 return -1;
7195 if (!match)
7196 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7197 "expecting set space", return -1);
7199 upa_space = isl_union_pw_aff_get_space(upa);
7200 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7201 if (match < 0)
7202 goto error;
7203 if (!match)
7204 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7205 "parameters don't match", goto error);
7207 isl_space_free(upa_space);
7208 return 0;
7209 error:
7210 isl_space_free(upa_space);
7211 return -1;
7214 /* Do the parameters of "upa" match those of "space"?
7216 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7217 __isl_keep isl_space *space)
7219 isl_space *upa_space;
7220 int match;
7222 if (!upa || !space)
7223 return -1;
7225 upa_space = isl_union_pw_aff_get_space(upa);
7227 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7229 isl_space_free(upa_space);
7230 return match;
7233 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7234 * space represents the new parameters.
7235 * res collects the results.
7237 struct isl_union_pw_aff_reset_params_data {
7238 isl_space *space;
7239 isl_union_pw_aff *res;
7242 /* Replace the parameters of "pa" by data->space and
7243 * add the result to data->res.
7245 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7247 struct isl_union_pw_aff_reset_params_data *data = user;
7248 isl_space *space;
7250 space = isl_pw_aff_get_space(pa);
7251 space = isl_space_replace(space, isl_dim_param, data->space);
7252 pa = isl_pw_aff_reset_space(pa, space);
7253 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7255 return data->res ? isl_stat_ok : isl_stat_error;
7258 /* Replace the domain space of "upa" by "space".
7259 * Since a union expression does not have a (single) domain space,
7260 * "space" is necessarily a parameter space.
7262 * Since the order and the names of the parameters determine
7263 * the hash value, we need to create a new hash table.
7265 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7266 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7268 struct isl_union_pw_aff_reset_params_data data = { space };
7269 int match;
7271 match = isl_union_pw_aff_matching_params(upa, space);
7272 if (match < 0)
7273 upa = isl_union_pw_aff_free(upa);
7274 else if (match) {
7275 isl_space_free(space);
7276 return upa;
7279 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7280 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7281 data.res = isl_union_pw_aff_free(data.res);
7283 isl_union_pw_aff_free(upa);
7284 isl_space_free(space);
7285 return data.res;
7288 /* Replace the entry of isl_union_pw_aff to which "entry" points
7289 * by its floor.
7291 static isl_stat floor_entry(void **entry, void *user)
7293 isl_pw_aff **pa = (isl_pw_aff **) entry;
7295 *pa = isl_pw_aff_floor(*pa);
7296 if (!*pa)
7297 return isl_stat_error;
7299 return isl_stat_ok;
7302 /* Given f, return floor(f).
7304 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7305 __isl_take isl_union_pw_aff *upa)
7307 isl_ctx *ctx;
7309 upa = isl_union_pw_aff_cow(upa);
7310 if (!upa)
7311 return NULL;
7313 ctx = isl_union_pw_aff_get_ctx(upa);
7314 if (isl_hash_table_foreach(ctx, &upa->table, &floor_entry, NULL) < 0)
7315 upa = isl_union_pw_aff_free(upa);
7317 return upa;
7320 /* Compute
7322 * upa mod m = upa - m * floor(upa/m)
7324 * with m an integer value.
7326 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7327 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7329 isl_union_pw_aff *res;
7331 if (!upa || !m)
7332 goto error;
7334 if (!isl_val_is_int(m))
7335 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7336 "expecting integer modulo", goto error);
7337 if (!isl_val_is_pos(m))
7338 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7339 "expecting positive modulo", goto error);
7341 res = isl_union_pw_aff_copy(upa);
7342 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7343 upa = isl_union_pw_aff_floor(upa);
7344 upa = isl_union_pw_aff_scale_val(upa, m);
7345 res = isl_union_pw_aff_sub(res, upa);
7347 return res;
7348 error:
7349 isl_val_free(m);
7350 isl_union_pw_aff_free(upa);
7351 return NULL;
7354 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7355 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7356 * needs to attain.
7357 * "res" collects the results.
7359 struct isl_union_pw_aff_aff_on_domain_data {
7360 isl_aff *aff;
7361 isl_union_pw_aff *res;
7364 /* Construct a piecewise affine expression that is equal to data->aff
7365 * on "domain" and add the result to data->res.
7367 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7369 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7370 isl_pw_aff *pa;
7371 isl_aff *aff;
7372 int dim;
7374 aff = isl_aff_copy(data->aff);
7375 dim = isl_set_dim(domain, isl_dim_set);
7376 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7377 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7378 pa = isl_pw_aff_alloc(domain, aff);
7379 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7381 return data->res ? isl_stat_ok : isl_stat_error;
7384 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7385 * pos is the output position that needs to be extracted.
7386 * res collects the results.
7388 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7389 int pos;
7390 isl_union_pw_aff *res;
7393 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7394 * (assuming it has such a dimension) and add it to data->res.
7396 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7398 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7399 int n_out;
7400 isl_pw_aff *pa;
7402 if (!pma)
7403 return isl_stat_error;
7405 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7406 if (data->pos >= n_out) {
7407 isl_pw_multi_aff_free(pma);
7408 return isl_stat_ok;
7411 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7412 isl_pw_multi_aff_free(pma);
7414 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7416 return data->res ? isl_stat_ok : isl_stat_error;
7419 /* Extract an isl_union_pw_aff corresponding to
7420 * output dimension "pos" of "upma".
7422 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7423 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7425 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7426 isl_space *space;
7428 if (!upma)
7429 return NULL;
7431 if (pos < 0)
7432 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7433 "cannot extract at negative position", return NULL);
7435 space = isl_union_pw_multi_aff_get_space(upma);
7436 data.res = isl_union_pw_aff_empty(space);
7437 data.pos = pos;
7438 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7439 &get_union_pw_aff, &data) < 0)
7440 data.res = isl_union_pw_aff_free(data.res);
7442 return data.res;
7445 /* Return a union piecewise affine expression
7446 * that is equal to "aff" on "domain".
7448 * Construct an isl_pw_aff on each of the sets in "domain" and
7449 * collect the results.
7451 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7452 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7454 struct isl_union_pw_aff_aff_on_domain_data data;
7455 isl_space *space;
7457 if (!domain || !aff)
7458 goto error;
7459 if (!isl_local_space_is_params(aff->ls))
7460 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7461 "expecting parametric expression", goto error);
7463 space = isl_union_set_get_space(domain);
7464 data.res = isl_union_pw_aff_empty(space);
7465 data.aff = aff;
7466 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7467 data.res = isl_union_pw_aff_free(data.res);
7468 isl_union_set_free(domain);
7469 isl_aff_free(aff);
7470 return data.res;
7471 error:
7472 isl_union_set_free(domain);
7473 isl_aff_free(aff);
7474 return NULL;
7477 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7478 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7479 * "res" collects the results.
7481 struct isl_union_pw_aff_val_on_domain_data {
7482 isl_val *v;
7483 isl_union_pw_aff *res;
7486 /* Construct a piecewise affine expression that is equal to data->v
7487 * on "domain" and add the result to data->res.
7489 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7491 struct isl_union_pw_aff_val_on_domain_data *data = user;
7492 isl_pw_aff *pa;
7493 isl_val *v;
7495 v = isl_val_copy(data->v);
7496 pa = isl_pw_aff_val_on_domain(domain, v);
7497 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7499 return data->res ? isl_stat_ok : isl_stat_error;
7502 /* Return a union piecewise affine expression
7503 * that is equal to "v" on "domain".
7505 * Construct an isl_pw_aff on each of the sets in "domain" and
7506 * collect the results.
7508 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7509 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7511 struct isl_union_pw_aff_val_on_domain_data data;
7512 isl_space *space;
7514 space = isl_union_set_get_space(domain);
7515 data.res = isl_union_pw_aff_empty(space);
7516 data.v = v;
7517 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7518 data.res = isl_union_pw_aff_free(data.res);
7519 isl_union_set_free(domain);
7520 isl_val_free(v);
7521 return data.res;
7524 /* Construct a piecewise multi affine expression
7525 * that is equal to "pa" and add it to upma.
7527 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7528 void *user)
7530 isl_union_pw_multi_aff **upma = user;
7531 isl_pw_multi_aff *pma;
7533 pma = isl_pw_multi_aff_from_pw_aff(pa);
7534 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7536 return *upma ? isl_stat_ok : isl_stat_error;
7539 /* Construct and return a union piecewise multi affine expression
7540 * that is equal to the given union piecewise affine expression.
7542 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7543 __isl_take isl_union_pw_aff *upa)
7545 isl_space *space;
7546 isl_union_pw_multi_aff *upma;
7548 if (!upa)
7549 return NULL;
7551 space = isl_union_pw_aff_get_space(upa);
7552 upma = isl_union_pw_multi_aff_empty(space);
7554 if (isl_union_pw_aff_foreach_pw_aff(upa,
7555 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7556 upma = isl_union_pw_multi_aff_free(upma);
7558 isl_union_pw_aff_free(upa);
7559 return upma;
7562 /* Compute the set of elements in the domain of "pa" where it is zero and
7563 * add this set to "uset".
7565 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7567 isl_union_set **uset = (isl_union_set **)user;
7569 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7571 return *uset ? isl_stat_ok : isl_stat_error;
7574 /* Return a union set containing those elements in the domain
7575 * of "upa" where it is zero.
7577 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7578 __isl_take isl_union_pw_aff *upa)
7580 isl_union_set *zero;
7582 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7583 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7584 zero = isl_union_set_free(zero);
7586 isl_union_pw_aff_free(upa);
7587 return zero;
7590 /* Convert "pa" to an isl_map and add it to *umap.
7592 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7594 isl_union_map **umap = user;
7595 isl_map *map;
7597 map = isl_map_from_pw_aff(pa);
7598 *umap = isl_union_map_add_map(*umap, map);
7600 return *umap ? isl_stat_ok : isl_stat_error;
7603 /* Construct a union map mapping the domain of the union
7604 * piecewise affine expression to its range, with the single output dimension
7605 * equated to the corresponding affine expressions on their cells.
7607 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7608 __isl_take isl_union_pw_aff *upa)
7610 isl_space *space;
7611 isl_union_map *umap;
7613 if (!upa)
7614 return NULL;
7616 space = isl_union_pw_aff_get_space(upa);
7617 umap = isl_union_map_empty(space);
7619 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7620 &umap) < 0)
7621 umap = isl_union_map_free(umap);
7623 isl_union_pw_aff_free(upa);
7624 return umap;
7627 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7628 * upma is the function that is plugged in.
7629 * pa is the current part of the function in which upma is plugged in.
7630 * res collects the results.
7632 struct isl_union_pw_aff_pullback_upma_data {
7633 isl_union_pw_multi_aff *upma;
7634 isl_pw_aff *pa;
7635 isl_union_pw_aff *res;
7638 /* Check if "pma" can be plugged into data->pa.
7639 * If so, perform the pullback and add the result to data->res.
7641 static isl_stat pa_pb_pma(void **entry, void *user)
7643 struct isl_union_pw_aff_pullback_upma_data *data = user;
7644 isl_pw_multi_aff *pma = *entry;
7645 isl_pw_aff *pa;
7647 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7648 pma->dim, isl_dim_out))
7649 return isl_stat_ok;
7651 pma = isl_pw_multi_aff_copy(pma);
7652 pa = isl_pw_aff_copy(data->pa);
7653 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7655 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7657 return data->res ? isl_stat_ok : isl_stat_error;
7660 /* Check if any of the elements of data->upma can be plugged into pa,
7661 * add if so add the result to data->res.
7663 static isl_stat upa_pb_upma(void **entry, void *user)
7665 struct isl_union_pw_aff_pullback_upma_data *data = user;
7666 isl_ctx *ctx;
7667 isl_pw_aff *pa = *entry;
7669 data->pa = pa;
7670 ctx = isl_union_pw_multi_aff_get_ctx(data->upma);
7671 if (isl_hash_table_foreach(ctx, &data->upma->table,
7672 &pa_pb_pma, data) < 0)
7673 return isl_stat_error;
7675 return isl_stat_ok;
7678 /* Compute the pullback of "upa" by the function represented by "upma".
7679 * In other words, plug in "upma" in "upa". The result contains
7680 * expressions defined over the domain space of "upma".
7682 * Run over all pairs of elements in "upa" and "upma", perform
7683 * the pullback when appropriate and collect the results.
7684 * If the hash value were based on the domain space rather than
7685 * the function space, then we could run through all elements
7686 * of "upma" and directly pick out the corresponding element of "upa".
7688 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7689 __isl_take isl_union_pw_aff *upa,
7690 __isl_take isl_union_pw_multi_aff *upma)
7692 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7693 isl_ctx *ctx;
7694 isl_space *space;
7696 space = isl_union_pw_multi_aff_get_space(upma);
7697 upa = isl_union_pw_aff_align_params(upa, space);
7698 space = isl_union_pw_aff_get_space(upa);
7699 upma = isl_union_pw_multi_aff_align_params(upma, space);
7701 if (!upa || !upma)
7702 goto error;
7704 ctx = isl_union_pw_aff_get_ctx(upa);
7705 data.upma = upma;
7706 space = isl_union_pw_aff_get_space(upa);
7707 data.res = isl_union_pw_aff_alloc(space, upa->table.n);
7708 if (isl_hash_table_foreach(ctx, &upa->table, &upa_pb_upma, &data) < 0)
7709 data.res = isl_union_pw_aff_free(data.res);
7711 isl_union_pw_aff_free(upa);
7712 isl_union_pw_multi_aff_free(upma);
7713 return data.res;
7714 error:
7715 isl_union_pw_aff_free(upa);
7716 isl_union_pw_multi_aff_free(upma);
7717 return NULL;
7720 #undef BASE
7721 #define BASE union_pw_aff
7722 #undef DOMBASE
7723 #define DOMBASE union_set
7725 #define NO_MOVE_DIMS
7726 #define NO_DIMS
7727 #define NO_DOMAIN
7728 #define NO_PRODUCT
7729 #define NO_SPLICE
7730 #define NO_ZERO
7731 #define NO_IDENTITY
7732 #define NO_GIST
7734 #include <isl_multi_templ.c>
7735 #include <isl_multi_apply_set.c>
7736 #include <isl_multi_apply_union_set.c>
7737 #include <isl_multi_floor.c>
7738 #include <isl_multi_gist.c>
7739 #include <isl_multi_intersect.c>
7741 /* Construct a multiple union piecewise affine expression
7742 * in the given space with value zero in each of the output dimensions.
7744 * Since there is no canonical zero value for
7745 * a union piecewise affine expression, we can only construct
7746 * zero-dimensional "zero" value.
7748 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7749 __isl_take isl_space *space)
7751 if (!space)
7752 return NULL;
7754 if (!isl_space_is_set(space))
7755 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7756 "expecting set space", goto error);
7757 if (isl_space_dim(space , isl_dim_out) != 0)
7758 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7759 "expecting 0D space", goto error);
7761 return isl_multi_union_pw_aff_alloc(space);
7762 error:
7763 isl_space_free(space);
7764 return NULL;
7767 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7768 * with the actual sum on the shared domain and
7769 * the defined expression on the symmetric difference of the domains.
7771 * We simply iterate over the elements in both arguments and
7772 * call isl_union_pw_aff_union_add on each of them.
7774 static __isl_give isl_multi_union_pw_aff *
7775 isl_multi_union_pw_aff_union_add_aligned(
7776 __isl_take isl_multi_union_pw_aff *mupa1,
7777 __isl_take isl_multi_union_pw_aff *mupa2)
7779 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7780 &isl_union_pw_aff_union_add);
7783 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7784 * with the actual sum on the shared domain and
7785 * the defined expression on the symmetric difference of the domains.
7787 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7788 __isl_take isl_multi_union_pw_aff *mupa1,
7789 __isl_take isl_multi_union_pw_aff *mupa2)
7791 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7792 &isl_multi_union_pw_aff_union_add_aligned);
7795 /* Construct and return a multi union piecewise affine expression
7796 * that is equal to the given multi affine expression.
7798 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7799 __isl_take isl_multi_aff *ma)
7801 isl_multi_pw_aff *mpa;
7803 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7804 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7807 /* Construct and return a multi union piecewise affine expression
7808 * that is equal to the given multi piecewise affine expression.
7810 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7811 __isl_take isl_multi_pw_aff *mpa)
7813 int i, n;
7814 isl_space *space;
7815 isl_multi_union_pw_aff *mupa;
7817 if (!mpa)
7818 return NULL;
7820 space = isl_multi_pw_aff_get_space(mpa);
7821 space = isl_space_range(space);
7822 mupa = isl_multi_union_pw_aff_alloc(space);
7824 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7825 for (i = 0; i < n; ++i) {
7826 isl_pw_aff *pa;
7827 isl_union_pw_aff *upa;
7829 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7830 upa = isl_union_pw_aff_from_pw_aff(pa);
7831 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7834 isl_multi_pw_aff_free(mpa);
7836 return mupa;
7839 /* Extract the range space of "pma" and assign it to *space.
7840 * If *space has already been set (through a previous call to this function),
7841 * then check that the range space is the same.
7843 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7845 isl_space **space = user;
7846 isl_space *pma_space;
7847 isl_bool equal;
7849 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7850 isl_pw_multi_aff_free(pma);
7852 if (!pma_space)
7853 return isl_stat_error;
7854 if (!*space) {
7855 *space = pma_space;
7856 return isl_stat_ok;
7859 equal = isl_space_is_equal(pma_space, *space);
7860 isl_space_free(pma_space);
7862 if (equal < 0)
7863 return isl_stat_error;
7864 if (!equal)
7865 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7866 "range spaces not the same", return isl_stat_error);
7867 return isl_stat_ok;
7870 /* Construct and return a multi union piecewise affine expression
7871 * that is equal to the given union piecewise multi affine expression.
7873 * In order to be able to perform the conversion, the input
7874 * needs to be non-empty and may only involve a single range space.
7876 __isl_give isl_multi_union_pw_aff *
7877 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7878 __isl_take isl_union_pw_multi_aff *upma)
7880 isl_space *space = NULL;
7881 isl_multi_union_pw_aff *mupa;
7882 int i, n;
7884 if (!upma)
7885 return NULL;
7886 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7887 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7888 "cannot extract range space from empty input",
7889 goto error);
7890 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7891 &space) < 0)
7892 goto error;
7894 if (!space)
7895 goto error;
7897 n = isl_space_dim(space, isl_dim_set);
7898 mupa = isl_multi_union_pw_aff_alloc(space);
7900 for (i = 0; i < n; ++i) {
7901 isl_union_pw_aff *upa;
7903 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7904 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7907 isl_union_pw_multi_aff_free(upma);
7908 return mupa;
7909 error:
7910 isl_space_free(space);
7911 isl_union_pw_multi_aff_free(upma);
7912 return NULL;
7915 /* Try and create an isl_multi_union_pw_aff that is equivalent
7916 * to the given isl_union_map.
7917 * The isl_union_map is required to be single-valued in each space.
7918 * Moreover, it cannot be empty and all range spaces need to be the same.
7919 * Otherwise, an error is produced.
7921 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7922 __isl_take isl_union_map *umap)
7924 isl_union_pw_multi_aff *upma;
7926 upma = isl_union_pw_multi_aff_from_union_map(umap);
7927 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7930 /* Return a multiple union piecewise affine expression
7931 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7932 * have been aligned.
7934 static __isl_give isl_multi_union_pw_aff *
7935 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7936 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7938 int i, n;
7939 isl_space *space;
7940 isl_multi_union_pw_aff *mupa;
7942 if (!domain || !mv)
7943 goto error;
7945 n = isl_multi_val_dim(mv, isl_dim_set);
7946 space = isl_multi_val_get_space(mv);
7947 mupa = isl_multi_union_pw_aff_alloc(space);
7948 for (i = 0; i < n; ++i) {
7949 isl_val *v;
7950 isl_union_pw_aff *upa;
7952 v = isl_multi_val_get_val(mv, i);
7953 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
7955 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7958 isl_union_set_free(domain);
7959 isl_multi_val_free(mv);
7960 return mupa;
7961 error:
7962 isl_union_set_free(domain);
7963 isl_multi_val_free(mv);
7964 return NULL;
7967 /* Return a multiple union piecewise affine expression
7968 * that is equal to "mv" on "domain".
7970 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
7971 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7973 if (!domain || !mv)
7974 goto error;
7975 if (isl_space_match(domain->dim, isl_dim_param,
7976 mv->space, isl_dim_param))
7977 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7978 domain, mv);
7979 domain = isl_union_set_align_params(domain,
7980 isl_multi_val_get_space(mv));
7981 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
7982 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
7983 error:
7984 isl_union_set_free(domain);
7985 isl_multi_val_free(mv);
7986 return NULL;
7989 /* Return a multiple union piecewise affine expression
7990 * that is equal to "ma" on "domain", assuming "domain" and "ma"
7991 * have been aligned.
7993 static __isl_give isl_multi_union_pw_aff *
7994 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
7995 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
7997 int i, n;
7998 isl_space *space;
7999 isl_multi_union_pw_aff *mupa;
8001 if (!domain || !ma)
8002 goto error;
8004 n = isl_multi_aff_dim(ma, isl_dim_set);
8005 space = isl_multi_aff_get_space(ma);
8006 mupa = isl_multi_union_pw_aff_alloc(space);
8007 for (i = 0; i < n; ++i) {
8008 isl_aff *aff;
8009 isl_union_pw_aff *upa;
8011 aff = isl_multi_aff_get_aff(ma, i);
8012 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8013 aff);
8014 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8017 isl_union_set_free(domain);
8018 isl_multi_aff_free(ma);
8019 return mupa;
8020 error:
8021 isl_union_set_free(domain);
8022 isl_multi_aff_free(ma);
8023 return NULL;
8026 /* Return a multiple union piecewise affine expression
8027 * that is equal to "ma" on "domain".
8029 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8030 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8032 if (!domain || !ma)
8033 goto error;
8034 if (isl_space_match(domain->dim, isl_dim_param,
8035 ma->space, isl_dim_param))
8036 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8037 domain, ma);
8038 domain = isl_union_set_align_params(domain,
8039 isl_multi_aff_get_space(ma));
8040 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8041 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8042 error:
8043 isl_union_set_free(domain);
8044 isl_multi_aff_free(ma);
8045 return NULL;
8048 /* Return a union set containing those elements in the domains
8049 * of the elements of "mupa" where they are all zero.
8051 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8052 __isl_take isl_multi_union_pw_aff *mupa)
8054 int i, n;
8055 isl_union_pw_aff *upa;
8056 isl_union_set *zero;
8058 if (!mupa)
8059 return NULL;
8061 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8062 if (n == 0)
8063 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8064 "cannot determine zero set "
8065 "of zero-dimensional function", goto error);
8067 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8068 zero = isl_union_pw_aff_zero_union_set(upa);
8070 for (i = 1; i < n; ++i) {
8071 isl_union_set *zero_i;
8073 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8074 zero_i = isl_union_pw_aff_zero_union_set(upa);
8076 zero = isl_union_set_intersect(zero, zero_i);
8079 isl_multi_union_pw_aff_free(mupa);
8080 return zero;
8081 error:
8082 isl_multi_union_pw_aff_free(mupa);
8083 return NULL;
8086 /* Construct a union map mapping the shared domain
8087 * of the union piecewise affine expressions to the range of "mupa"
8088 * with each dimension in the range equated to the
8089 * corresponding union piecewise affine expression.
8091 * The input cannot be zero-dimensional as there is
8092 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8094 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8095 __isl_take isl_multi_union_pw_aff *mupa)
8097 int i, n;
8098 isl_space *space;
8099 isl_union_map *umap;
8100 isl_union_pw_aff *upa;
8102 if (!mupa)
8103 return NULL;
8105 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8106 if (n == 0)
8107 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8108 "cannot determine domain of zero-dimensional "
8109 "isl_multi_union_pw_aff", goto error);
8111 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8112 umap = isl_union_map_from_union_pw_aff(upa);
8114 for (i = 1; i < n; ++i) {
8115 isl_union_map *umap_i;
8117 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8118 umap_i = isl_union_map_from_union_pw_aff(upa);
8119 umap = isl_union_map_flat_range_product(umap, umap_i);
8122 space = isl_multi_union_pw_aff_get_space(mupa);
8123 umap = isl_union_map_reset_range_space(umap, space);
8125 isl_multi_union_pw_aff_free(mupa);
8126 return umap;
8127 error:
8128 isl_multi_union_pw_aff_free(mupa);
8129 return NULL;
8132 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8133 * "range" is the space from which to set the range space.
8134 * "res" collects the results.
8136 struct isl_union_pw_multi_aff_reset_range_space_data {
8137 isl_space *range;
8138 isl_union_pw_multi_aff *res;
8141 /* Replace the range space of "pma" by the range space of data->range and
8142 * add the result to data->res.
8144 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8146 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8147 isl_space *space;
8149 space = isl_pw_multi_aff_get_space(pma);
8150 space = isl_space_domain(space);
8151 space = isl_space_extend_domain_with_range(space,
8152 isl_space_copy(data->range));
8153 pma = isl_pw_multi_aff_reset_space(pma, space);
8154 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8156 return data->res ? isl_stat_ok : isl_stat_error;
8159 /* Replace the range space of all the piecewise affine expressions in "upma" by
8160 * the range space of "space".
8162 * This assumes that all these expressions have the same output dimension.
8164 * Since the spaces of the expressions change, so do their hash values.
8165 * We therefore need to create a new isl_union_pw_multi_aff.
8166 * Note that the hash value is currently computed based on the entire
8167 * space even though there can only be a single expression with a given
8168 * domain space.
8170 static __isl_give isl_union_pw_multi_aff *
8171 isl_union_pw_multi_aff_reset_range_space(
8172 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8174 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8175 isl_space *space_upma;
8177 space_upma = isl_union_pw_multi_aff_get_space(upma);
8178 data.res = isl_union_pw_multi_aff_empty(space_upma);
8179 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8180 &reset_range_space, &data) < 0)
8181 data.res = isl_union_pw_multi_aff_free(data.res);
8183 isl_space_free(space);
8184 isl_union_pw_multi_aff_free(upma);
8185 return data.res;
8188 /* Construct and return a union piecewise multi affine expression
8189 * that is equal to the given multi union piecewise affine expression.
8191 * In order to be able to perform the conversion, the input
8192 * needs to have a least one output dimension.
8194 __isl_give isl_union_pw_multi_aff *
8195 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8196 __isl_take isl_multi_union_pw_aff *mupa)
8198 int i, n;
8199 isl_space *space;
8200 isl_union_pw_multi_aff *upma;
8201 isl_union_pw_aff *upa;
8203 if (!mupa)
8204 return NULL;
8206 space = isl_multi_union_pw_aff_get_space(mupa);
8208 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8209 if (n == 0)
8210 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8211 "cannot determine domain of zero-dimensional "
8212 "isl_multi_union_pw_aff", goto error);
8214 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8215 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8217 for (i = 1; i < n; ++i) {
8218 isl_union_pw_multi_aff *upma_i;
8220 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8221 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8222 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8225 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8227 isl_multi_union_pw_aff_free(mupa);
8228 return upma;
8229 error:
8230 isl_multi_union_pw_aff_free(mupa);
8231 return NULL;
8234 /* Intersect the range of "mupa" with "range".
8235 * That is, keep only those domain elements that have a function value
8236 * in "range".
8238 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8239 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8241 isl_union_pw_multi_aff *upma;
8242 isl_union_set *domain;
8243 isl_space *space;
8244 int n;
8245 int match;
8247 if (!mupa || !range)
8248 goto error;
8250 space = isl_set_get_space(range);
8251 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8252 space, isl_dim_set);
8253 isl_space_free(space);
8254 if (match < 0)
8255 goto error;
8256 if (!match)
8257 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8258 "space don't match", goto error);
8259 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8260 if (n == 0)
8261 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8262 "cannot intersect range of zero-dimensional "
8263 "isl_multi_union_pw_aff", goto error);
8265 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8266 isl_multi_union_pw_aff_copy(mupa));
8267 domain = isl_union_set_from_set(range);
8268 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8269 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8271 return mupa;
8272 error:
8273 isl_multi_union_pw_aff_free(mupa);
8274 isl_set_free(range);
8275 return NULL;
8278 /* Return the shared domain of the elements of "mupa".
8280 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8281 __isl_take isl_multi_union_pw_aff *mupa)
8283 int i, n;
8284 isl_union_pw_aff *upa;
8285 isl_union_set *dom;
8287 if (!mupa)
8288 return NULL;
8290 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8291 if (n == 0)
8292 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8293 "cannot determine domain", goto error);
8295 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8296 dom = isl_union_pw_aff_domain(upa);
8297 for (i = 1; i < n; ++i) {
8298 isl_union_set *dom_i;
8300 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8301 dom_i = isl_union_pw_aff_domain(upa);
8302 dom = isl_union_set_intersect(dom, dom_i);
8305 isl_multi_union_pw_aff_free(mupa);
8306 return dom;
8307 error:
8308 isl_multi_union_pw_aff_free(mupa);
8309 return NULL;
8312 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8313 * In particular, the spaces have been aligned.
8314 * The result is defined over the shared domain of the elements of "mupa"
8316 * We first extract the parametric constant part of "aff" and
8317 * define that over the shared domain.
8318 * Then we iterate over all input dimensions of "aff" and add the corresponding
8319 * multiples of the elements of "mupa".
8320 * Finally, we consider the integer divisions, calling the function
8321 * recursively to obtain an isl_union_pw_aff corresponding to the
8322 * integer division argument.
8324 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8325 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8327 int i, n_in, n_div;
8328 isl_union_pw_aff *upa;
8329 isl_union_set *uset;
8330 isl_val *v;
8331 isl_aff *cst;
8333 n_in = isl_aff_dim(aff, isl_dim_in);
8334 n_div = isl_aff_dim(aff, isl_dim_div);
8336 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8337 cst = isl_aff_copy(aff);
8338 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8339 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8340 cst = isl_aff_project_domain_on_params(cst);
8341 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8343 for (i = 0; i < n_in; ++i) {
8344 isl_union_pw_aff *upa_i;
8346 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8347 continue;
8348 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8349 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8350 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8351 upa = isl_union_pw_aff_add(upa, upa_i);
8354 for (i = 0; i < n_div; ++i) {
8355 isl_aff *div;
8356 isl_union_pw_aff *upa_i;
8358 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8359 continue;
8360 div = isl_aff_get_div(aff, i);
8361 upa_i = multi_union_pw_aff_apply_aff(
8362 isl_multi_union_pw_aff_copy(mupa), div);
8363 upa_i = isl_union_pw_aff_floor(upa_i);
8364 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8365 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8366 upa = isl_union_pw_aff_add(upa, upa_i);
8369 isl_multi_union_pw_aff_free(mupa);
8370 isl_aff_free(aff);
8372 return upa;
8375 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8376 * with the domain of "aff".
8377 * Furthermore, the dimension of this space needs to be greater than zero.
8378 * The result is defined over the shared domain of the elements of "mupa"
8380 * We perform these checks and then hand over control to
8381 * multi_union_pw_aff_apply_aff.
8383 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8384 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8386 isl_space *space1, *space2;
8387 int equal;
8389 mupa = isl_multi_union_pw_aff_align_params(mupa,
8390 isl_aff_get_space(aff));
8391 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8392 if (!mupa || !aff)
8393 goto error;
8395 space1 = isl_multi_union_pw_aff_get_space(mupa);
8396 space2 = isl_aff_get_domain_space(aff);
8397 equal = isl_space_is_equal(space1, space2);
8398 isl_space_free(space1);
8399 isl_space_free(space2);
8400 if (equal < 0)
8401 goto error;
8402 if (!equal)
8403 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8404 "spaces don't match", goto error);
8405 if (isl_aff_dim(aff, isl_dim_in) == 0)
8406 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8407 "cannot determine domains", goto error);
8409 return multi_union_pw_aff_apply_aff(mupa, aff);
8410 error:
8411 isl_multi_union_pw_aff_free(mupa);
8412 isl_aff_free(aff);
8413 return NULL;
8416 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8417 * with the domain of "ma".
8418 * Furthermore, the dimension of this space needs to be greater than zero,
8419 * unless the dimension of the target space of "ma" is also zero.
8420 * The result is defined over the shared domain of the elements of "mupa"
8422 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8423 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8425 isl_space *space1, *space2;
8426 isl_multi_union_pw_aff *res;
8427 int equal;
8428 int i, n_out;
8430 mupa = isl_multi_union_pw_aff_align_params(mupa,
8431 isl_multi_aff_get_space(ma));
8432 ma = isl_multi_aff_align_params(ma,
8433 isl_multi_union_pw_aff_get_space(mupa));
8434 if (!mupa || !ma)
8435 goto error;
8437 space1 = isl_multi_union_pw_aff_get_space(mupa);
8438 space2 = isl_multi_aff_get_domain_space(ma);
8439 equal = isl_space_is_equal(space1, space2);
8440 isl_space_free(space1);
8441 isl_space_free(space2);
8442 if (equal < 0)
8443 goto error;
8444 if (!equal)
8445 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8446 "spaces don't match", goto error);
8447 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8448 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8449 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8450 "cannot determine domains", goto error);
8452 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8453 res = isl_multi_union_pw_aff_alloc(space1);
8455 for (i = 0; i < n_out; ++i) {
8456 isl_aff *aff;
8457 isl_union_pw_aff *upa;
8459 aff = isl_multi_aff_get_aff(ma, i);
8460 upa = multi_union_pw_aff_apply_aff(
8461 isl_multi_union_pw_aff_copy(mupa), aff);
8462 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8465 isl_multi_aff_free(ma);
8466 isl_multi_union_pw_aff_free(mupa);
8467 return res;
8468 error:
8469 isl_multi_union_pw_aff_free(mupa);
8470 isl_multi_aff_free(ma);
8471 return NULL;
8474 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8475 * with the domain of "pa".
8476 * Furthermore, the dimension of this space needs to be greater than zero.
8477 * The result is defined over the shared domain of the elements of "mupa"
8479 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8480 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8482 int i;
8483 int equal;
8484 isl_space *space, *space2;
8485 isl_union_pw_aff *upa;
8487 mupa = isl_multi_union_pw_aff_align_params(mupa,
8488 isl_pw_aff_get_space(pa));
8489 pa = isl_pw_aff_align_params(pa,
8490 isl_multi_union_pw_aff_get_space(mupa));
8491 if (!mupa || !pa)
8492 goto error;
8494 space = isl_multi_union_pw_aff_get_space(mupa);
8495 space2 = isl_pw_aff_get_domain_space(pa);
8496 equal = isl_space_is_equal(space, space2);
8497 isl_space_free(space);
8498 isl_space_free(space2);
8499 if (equal < 0)
8500 goto error;
8501 if (!equal)
8502 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8503 "spaces don't match", goto error);
8504 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8505 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8506 "cannot determine domains", goto error);
8508 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8509 upa = isl_union_pw_aff_empty(space);
8511 for (i = 0; i < pa->n; ++i) {
8512 isl_aff *aff;
8513 isl_set *domain;
8514 isl_multi_union_pw_aff *mupa_i;
8515 isl_union_pw_aff *upa_i;
8517 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8518 domain = isl_set_copy(pa->p[i].set);
8519 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8520 aff = isl_aff_copy(pa->p[i].aff);
8521 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8522 upa = isl_union_pw_aff_union_add(upa, upa_i);
8525 isl_multi_union_pw_aff_free(mupa);
8526 isl_pw_aff_free(pa);
8527 return upa;
8528 error:
8529 isl_multi_union_pw_aff_free(mupa);
8530 isl_pw_aff_free(pa);
8531 return NULL;
8534 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8535 * with the domain of "pma".
8536 * Furthermore, the dimension of this space needs to be greater than zero,
8537 * unless the dimension of the target space of "pma" is also zero.
8538 * The result is defined over the shared domain of the elements of "mupa"
8540 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8541 __isl_take isl_multi_union_pw_aff *mupa,
8542 __isl_take isl_pw_multi_aff *pma)
8544 isl_space *space1, *space2;
8545 isl_multi_union_pw_aff *res;
8546 int equal;
8547 int i, n_out;
8549 mupa = isl_multi_union_pw_aff_align_params(mupa,
8550 isl_pw_multi_aff_get_space(pma));
8551 pma = isl_pw_multi_aff_align_params(pma,
8552 isl_multi_union_pw_aff_get_space(mupa));
8553 if (!mupa || !pma)
8554 goto error;
8556 space1 = isl_multi_union_pw_aff_get_space(mupa);
8557 space2 = isl_pw_multi_aff_get_domain_space(pma);
8558 equal = isl_space_is_equal(space1, space2);
8559 isl_space_free(space1);
8560 isl_space_free(space2);
8561 if (equal < 0)
8562 goto error;
8563 if (!equal)
8564 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8565 "spaces don't match", goto error);
8566 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8567 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8568 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8569 "cannot determine domains", goto error);
8571 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8572 res = isl_multi_union_pw_aff_alloc(space1);
8574 for (i = 0; i < n_out; ++i) {
8575 isl_pw_aff *pa;
8576 isl_union_pw_aff *upa;
8578 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8579 upa = isl_multi_union_pw_aff_apply_pw_aff(
8580 isl_multi_union_pw_aff_copy(mupa), pa);
8581 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8584 isl_pw_multi_aff_free(pma);
8585 isl_multi_union_pw_aff_free(mupa);
8586 return res;
8587 error:
8588 isl_multi_union_pw_aff_free(mupa);
8589 isl_pw_multi_aff_free(pma);
8590 return NULL;
8593 /* Compute the pullback of "mupa" by the function represented by "upma".
8594 * In other words, plug in "upma" in "mupa". The result contains
8595 * expressions defined over the domain space of "upma".
8597 * Run over all elements of "mupa" and plug in "upma" in each of them.
8599 __isl_give isl_multi_union_pw_aff *
8600 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8601 __isl_take isl_multi_union_pw_aff *mupa,
8602 __isl_take isl_union_pw_multi_aff *upma)
8604 int i, n;
8606 mupa = isl_multi_union_pw_aff_align_params(mupa,
8607 isl_union_pw_multi_aff_get_space(upma));
8608 upma = isl_union_pw_multi_aff_align_params(upma,
8609 isl_multi_union_pw_aff_get_space(mupa));
8610 if (!mupa || !upma)
8611 goto error;
8613 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8614 for (i = 0; i < n; ++i) {
8615 isl_union_pw_aff *upa;
8617 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8618 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8619 isl_union_pw_multi_aff_copy(upma));
8620 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8623 isl_union_pw_multi_aff_free(upma);
8624 return mupa;
8625 error:
8626 isl_multi_union_pw_aff_free(mupa);
8627 isl_union_pw_multi_aff_free(upma);
8628 return NULL;
8631 /* Extract the sequence of elements in "mupa" with domain space "space"
8632 * (ignoring parameters).
8634 * For the elements of "mupa" that are not defined on the specified space,
8635 * the corresponding element in the result is empty.
8637 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8638 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8640 int i, n;
8641 isl_space *space_mpa = NULL;
8642 isl_multi_pw_aff *mpa;
8644 if (!mupa || !space)
8645 goto error;
8647 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8648 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8649 space = isl_space_drop_dims(space, isl_dim_param,
8650 0, isl_space_dim(space, isl_dim_param));
8651 space = isl_space_align_params(space,
8652 isl_space_copy(space_mpa));
8653 if (!space)
8654 goto error;
8656 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8657 space_mpa);
8658 mpa = isl_multi_pw_aff_alloc(space_mpa);
8660 space = isl_space_from_domain(space);
8661 space = isl_space_add_dims(space, isl_dim_out, 1);
8662 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8663 for (i = 0; i < n; ++i) {
8664 isl_union_pw_aff *upa;
8665 isl_pw_aff *pa;
8667 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8668 pa = isl_union_pw_aff_extract_pw_aff(upa,
8669 isl_space_copy(space));
8670 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8671 isl_union_pw_aff_free(upa);
8674 isl_space_free(space);
8675 return mpa;
8676 error:
8677 isl_space_free(space_mpa);
8678 isl_space_free(space);
8679 return NULL;