isl_ast_build_expr_from_set_internal: operate on list of basic sets
[isl.git] / isl_aff.c
blobd1d293fdfd382b4386fe26141ac7137df870b791
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 #define NO_EVAL
2583 #include <isl_union_templ.c>
2585 static __isl_give isl_set *align_params_pw_pw_set_and(
2586 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2587 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2588 __isl_take isl_pw_aff *pwaff2))
2590 if (!pwaff1 || !pwaff2)
2591 goto error;
2592 if (isl_space_match(pwaff1->dim, isl_dim_param,
2593 pwaff2->dim, isl_dim_param))
2594 return fn(pwaff1, pwaff2);
2595 if (!isl_space_has_named_params(pwaff1->dim) ||
2596 !isl_space_has_named_params(pwaff2->dim))
2597 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2598 "unaligned unnamed parameters", goto error);
2599 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2600 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2601 return fn(pwaff1, pwaff2);
2602 error:
2603 isl_pw_aff_free(pwaff1);
2604 isl_pw_aff_free(pwaff2);
2605 return NULL;
2608 /* Align the parameters of the to isl_pw_aff arguments and
2609 * then apply a function "fn" on them that returns an isl_map.
2611 static __isl_give isl_map *align_params_pw_pw_map_and(
2612 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2613 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2614 __isl_take isl_pw_aff *pa2))
2616 if (!pa1 || !pa2)
2617 goto error;
2618 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2619 return fn(pa1, pa2);
2620 if (!isl_space_has_named_params(pa1->dim) ||
2621 !isl_space_has_named_params(pa2->dim))
2622 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2623 "unaligned unnamed parameters", goto error);
2624 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2625 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2626 return fn(pa1, pa2);
2627 error:
2628 isl_pw_aff_free(pa1);
2629 isl_pw_aff_free(pa2);
2630 return NULL;
2633 /* Compute a piecewise quasi-affine expression with a domain that
2634 * is the union of those of pwaff1 and pwaff2 and such that on each
2635 * cell, the quasi-affine expression is the better (according to cmp)
2636 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2637 * is defined on a given cell, then the associated expression
2638 * is the defined one.
2640 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2641 __isl_take isl_pw_aff *pwaff2,
2642 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2643 __isl_take isl_aff *aff2))
2645 int i, j, n;
2646 isl_pw_aff *res;
2647 isl_ctx *ctx;
2648 isl_set *set;
2650 if (!pwaff1 || !pwaff2)
2651 goto error;
2653 ctx = isl_space_get_ctx(pwaff1->dim);
2654 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2655 isl_die(ctx, isl_error_invalid,
2656 "arguments should live in same space", goto error);
2658 if (isl_pw_aff_is_empty(pwaff1)) {
2659 isl_pw_aff_free(pwaff1);
2660 return pwaff2;
2663 if (isl_pw_aff_is_empty(pwaff2)) {
2664 isl_pw_aff_free(pwaff2);
2665 return pwaff1;
2668 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2669 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2671 for (i = 0; i < pwaff1->n; ++i) {
2672 set = isl_set_copy(pwaff1->p[i].set);
2673 for (j = 0; j < pwaff2->n; ++j) {
2674 struct isl_set *common;
2675 isl_set *better;
2677 common = isl_set_intersect(
2678 isl_set_copy(pwaff1->p[i].set),
2679 isl_set_copy(pwaff2->p[j].set));
2680 better = isl_set_from_basic_set(cmp(
2681 isl_aff_copy(pwaff2->p[j].aff),
2682 isl_aff_copy(pwaff1->p[i].aff)));
2683 better = isl_set_intersect(common, better);
2684 if (isl_set_plain_is_empty(better)) {
2685 isl_set_free(better);
2686 continue;
2688 set = isl_set_subtract(set, isl_set_copy(better));
2690 res = isl_pw_aff_add_piece(res, better,
2691 isl_aff_copy(pwaff2->p[j].aff));
2693 res = isl_pw_aff_add_piece(res, set,
2694 isl_aff_copy(pwaff1->p[i].aff));
2697 for (j = 0; j < pwaff2->n; ++j) {
2698 set = isl_set_copy(pwaff2->p[j].set);
2699 for (i = 0; i < pwaff1->n; ++i)
2700 set = isl_set_subtract(set,
2701 isl_set_copy(pwaff1->p[i].set));
2702 res = isl_pw_aff_add_piece(res, set,
2703 isl_aff_copy(pwaff2->p[j].aff));
2706 isl_pw_aff_free(pwaff1);
2707 isl_pw_aff_free(pwaff2);
2709 return res;
2710 error:
2711 isl_pw_aff_free(pwaff1);
2712 isl_pw_aff_free(pwaff2);
2713 return NULL;
2716 /* Compute a piecewise quasi-affine expression with a domain that
2717 * is the union of those of pwaff1 and pwaff2 and such that on each
2718 * cell, the quasi-affine expression is the maximum of those of pwaff1
2719 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2720 * cell, then the associated expression is the defined one.
2722 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2723 __isl_take isl_pw_aff *pwaff2)
2725 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2728 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2729 __isl_take isl_pw_aff *pwaff2)
2731 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2732 &pw_aff_union_max);
2735 /* Compute a piecewise quasi-affine expression with a domain that
2736 * is the union of those of pwaff1 and pwaff2 and such that on each
2737 * cell, the quasi-affine expression is the minimum of those of pwaff1
2738 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2739 * cell, then the associated expression is the defined one.
2741 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2742 __isl_take isl_pw_aff *pwaff2)
2744 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2747 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2748 __isl_take isl_pw_aff *pwaff2)
2750 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2751 &pw_aff_union_min);
2754 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2755 __isl_take isl_pw_aff *pwaff2, int max)
2757 if (max)
2758 return isl_pw_aff_union_max(pwaff1, pwaff2);
2759 else
2760 return isl_pw_aff_union_min(pwaff1, pwaff2);
2763 /* Construct a map with as domain the domain of pwaff and
2764 * one-dimensional range corresponding to the affine expressions.
2766 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2768 int i;
2769 isl_space *dim;
2770 isl_map *map;
2772 if (!pwaff)
2773 return NULL;
2775 dim = isl_pw_aff_get_space(pwaff);
2776 map = isl_map_empty(dim);
2778 for (i = 0; i < pwaff->n; ++i) {
2779 isl_basic_map *bmap;
2780 isl_map *map_i;
2782 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2783 map_i = isl_map_from_basic_map(bmap);
2784 map_i = isl_map_intersect_domain(map_i,
2785 isl_set_copy(pwaff->p[i].set));
2786 map = isl_map_union_disjoint(map, map_i);
2789 isl_pw_aff_free(pwaff);
2791 return map;
2794 /* Construct a map with as domain the domain of pwaff and
2795 * one-dimensional range corresponding to the affine expressions.
2797 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2799 if (!pwaff)
2800 return NULL;
2801 if (isl_space_is_set(pwaff->dim))
2802 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2803 "space of input is not a map", goto error);
2804 return map_from_pw_aff(pwaff);
2805 error:
2806 isl_pw_aff_free(pwaff);
2807 return NULL;
2810 /* Construct a one-dimensional set with as parameter domain
2811 * the domain of pwaff and the single set dimension
2812 * corresponding to the affine expressions.
2814 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2816 if (!pwaff)
2817 return NULL;
2818 if (!isl_space_is_set(pwaff->dim))
2819 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2820 "space of input is not a set", goto error);
2821 return map_from_pw_aff(pwaff);
2822 error:
2823 isl_pw_aff_free(pwaff);
2824 return NULL;
2827 /* Return a set containing those elements in the domain
2828 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2829 * does not satisfy "fn" (if complement is 1).
2831 * The pieces with a NaN never belong to the result since
2832 * NaN does not satisfy any property.
2834 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2835 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2836 int complement)
2838 int i;
2839 isl_set *set;
2841 if (!pwaff)
2842 return NULL;
2844 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2846 for (i = 0; i < pwaff->n; ++i) {
2847 isl_basic_set *bset;
2848 isl_set *set_i, *locus;
2849 int rational;
2851 if (isl_aff_is_nan(pwaff->p[i].aff))
2852 continue;
2854 rational = isl_set_has_rational(pwaff->p[i].set);
2855 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2856 locus = isl_set_from_basic_set(bset);
2857 set_i = isl_set_copy(pwaff->p[i].set);
2858 if (complement)
2859 set_i = isl_set_subtract(set_i, locus);
2860 else
2861 set_i = isl_set_intersect(set_i, locus);
2862 set = isl_set_union_disjoint(set, set_i);
2865 isl_pw_aff_free(pwaff);
2867 return set;
2870 /* Return a set containing those elements in the domain
2871 * of "pa" where it is positive.
2873 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2875 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2878 /* Return a set containing those elements in the domain
2879 * of pwaff where it is non-negative.
2881 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2883 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2886 /* Return a set containing those elements in the domain
2887 * of pwaff where it is zero.
2889 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2891 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2894 /* Return a set containing those elements in the domain
2895 * of pwaff where it is not zero.
2897 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2899 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2902 /* Return a set containing those elements in the shared domain
2903 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2905 * We compute the difference on the shared domain and then construct
2906 * the set of values where this difference is non-negative.
2907 * If strict is set, we first subtract 1 from the difference.
2908 * If equal is set, we only return the elements where pwaff1 and pwaff2
2909 * are equal.
2911 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2912 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2914 isl_set *set1, *set2;
2916 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2917 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2918 set1 = isl_set_intersect(set1, set2);
2919 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2920 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2921 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2923 if (strict) {
2924 isl_space *dim = isl_set_get_space(set1);
2925 isl_aff *aff;
2926 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2927 aff = isl_aff_add_constant_si(aff, -1);
2928 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2929 } else
2930 isl_set_free(set1);
2932 if (equal)
2933 return isl_pw_aff_zero_set(pwaff1);
2934 return isl_pw_aff_nonneg_set(pwaff1);
2937 /* Return a set containing those elements in the shared domain
2938 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2940 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2941 __isl_take isl_pw_aff *pwaff2)
2943 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2946 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2947 __isl_take isl_pw_aff *pwaff2)
2949 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2952 /* Return a set containing those elements in the shared domain
2953 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2955 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2956 __isl_take isl_pw_aff *pwaff2)
2958 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2961 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2962 __isl_take isl_pw_aff *pwaff2)
2964 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2967 /* Return a set containing those elements in the shared domain
2968 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2970 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2971 __isl_take isl_pw_aff *pwaff2)
2973 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2976 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2979 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2982 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2983 __isl_take isl_pw_aff *pwaff2)
2985 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2988 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2989 __isl_take isl_pw_aff *pwaff2)
2991 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2994 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2995 * where the function values are ordered in the same way as "order",
2996 * which returns a set in the shared domain of its two arguments.
2997 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2999 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3000 * We first pull back the two functions such that they are defined on
3001 * the domain [A -> B]. Then we apply "order", resulting in a set
3002 * in the space [A -> B]. Finally, we unwrap this set to obtain
3003 * a map in the space A -> B.
3005 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3006 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3007 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3008 __isl_take isl_pw_aff *pa2))
3010 isl_space *space1, *space2;
3011 isl_multi_aff *ma;
3012 isl_set *set;
3014 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3015 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3016 space1 = isl_space_map_from_domain_and_range(space1, space2);
3017 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3018 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3019 ma = isl_multi_aff_range_map(space1);
3020 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3021 set = order(pa1, pa2);
3023 return isl_set_unwrap(set);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function values are equal.
3028 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3030 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3031 __isl_take isl_pw_aff *pa2)
3033 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function values are equal.
3039 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3040 __isl_take isl_pw_aff *pa2)
3042 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is less than the function value of "pa2".
3047 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3049 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3050 __isl_take isl_pw_aff *pa2)
3052 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3055 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3056 * where the function value of "pa1" is less than the function value of "pa2".
3058 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3059 __isl_take isl_pw_aff *pa2)
3061 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3064 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3065 * where the function value of "pa1" is greater than the function value
3066 * of "pa2".
3067 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3069 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3070 __isl_take isl_pw_aff *pa2)
3072 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3075 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function value of "pa1" is greater than the function value
3077 * of "pa2".
3079 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3080 __isl_take isl_pw_aff *pa2)
3082 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3085 /* Return a set containing those elements in the shared domain
3086 * of the elements of list1 and list2 where each element in list1
3087 * has the relation specified by "fn" with each element in list2.
3089 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3090 __isl_take isl_pw_aff_list *list2,
3091 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3092 __isl_take isl_pw_aff *pwaff2))
3094 int i, j;
3095 isl_ctx *ctx;
3096 isl_set *set;
3098 if (!list1 || !list2)
3099 goto error;
3101 ctx = isl_pw_aff_list_get_ctx(list1);
3102 if (list1->n < 1 || list2->n < 1)
3103 isl_die(ctx, isl_error_invalid,
3104 "list should contain at least one element", goto error);
3106 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3107 for (i = 0; i < list1->n; ++i)
3108 for (j = 0; j < list2->n; ++j) {
3109 isl_set *set_ij;
3111 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3112 isl_pw_aff_copy(list2->p[j]));
3113 set = isl_set_intersect(set, set_ij);
3116 isl_pw_aff_list_free(list1);
3117 isl_pw_aff_list_free(list2);
3118 return set;
3119 error:
3120 isl_pw_aff_list_free(list1);
3121 isl_pw_aff_list_free(list2);
3122 return NULL;
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is equal to each element in list2.
3129 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3135 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3136 __isl_take isl_pw_aff_list *list2)
3138 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * is less than or equal to each element in list2.
3145 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2)
3148 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3151 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3152 __isl_take isl_pw_aff_list *list2)
3154 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3157 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3158 __isl_take isl_pw_aff_list *list2)
3160 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3163 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3164 __isl_take isl_pw_aff_list *list2)
3166 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3170 /* Return a set containing those elements in the shared domain
3171 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3173 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3174 __isl_take isl_pw_aff *pwaff2)
3176 isl_set *set_lt, *set_gt;
3178 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3179 isl_pw_aff_copy(pwaff2));
3180 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3181 return isl_set_union_disjoint(set_lt, set_gt);
3184 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3185 __isl_take isl_pw_aff *pwaff2)
3187 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3190 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3191 isl_int v)
3193 int i;
3195 if (isl_int_is_one(v))
3196 return pwaff;
3197 if (!isl_int_is_pos(v))
3198 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3199 "factor needs to be positive",
3200 return isl_pw_aff_free(pwaff));
3201 pwaff = isl_pw_aff_cow(pwaff);
3202 if (!pwaff)
3203 return NULL;
3204 if (pwaff->n == 0)
3205 return pwaff;
3207 for (i = 0; i < pwaff->n; ++i) {
3208 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3209 if (!pwaff->p[i].aff)
3210 return isl_pw_aff_free(pwaff);
3213 return pwaff;
3216 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3218 int i;
3220 pwaff = isl_pw_aff_cow(pwaff);
3221 if (!pwaff)
3222 return NULL;
3223 if (pwaff->n == 0)
3224 return pwaff;
3226 for (i = 0; i < pwaff->n; ++i) {
3227 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3228 if (!pwaff->p[i].aff)
3229 return isl_pw_aff_free(pwaff);
3232 return pwaff;
3235 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3237 int i;
3239 pwaff = isl_pw_aff_cow(pwaff);
3240 if (!pwaff)
3241 return NULL;
3242 if (pwaff->n == 0)
3243 return pwaff;
3245 for (i = 0; i < pwaff->n; ++i) {
3246 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3247 if (!pwaff->p[i].aff)
3248 return isl_pw_aff_free(pwaff);
3251 return pwaff;
3254 /* Assuming that "cond1" and "cond2" are disjoint,
3255 * return an affine expression that is equal to pwaff1 on cond1
3256 * and to pwaff2 on cond2.
3258 static __isl_give isl_pw_aff *isl_pw_aff_select(
3259 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3260 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3262 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3263 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3265 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3268 /* Return an affine expression that is equal to pwaff_true for elements
3269 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3270 * is zero.
3271 * That is, return cond ? pwaff_true : pwaff_false;
3273 * If "cond" involves and NaN, then we conservatively return a NaN
3274 * on its entire domain. In principle, we could consider the pieces
3275 * where it is NaN separately from those where it is not.
3277 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3278 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3280 isl_set *cond_true, *cond_false;
3282 if (!cond)
3283 goto error;
3284 if (isl_pw_aff_involves_nan(cond)) {
3285 isl_space *space = isl_pw_aff_get_domain_space(cond);
3286 isl_local_space *ls = isl_local_space_from_space(space);
3287 isl_pw_aff_free(cond);
3288 isl_pw_aff_free(pwaff_true);
3289 isl_pw_aff_free(pwaff_false);
3290 return isl_pw_aff_nan_on_domain(ls);
3293 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3294 cond_false = isl_pw_aff_zero_set(cond);
3295 return isl_pw_aff_select(cond_true, pwaff_true,
3296 cond_false, pwaff_false);
3297 error:
3298 isl_pw_aff_free(cond);
3299 isl_pw_aff_free(pwaff_true);
3300 isl_pw_aff_free(pwaff_false);
3301 return NULL;
3304 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3306 if (!aff)
3307 return isl_bool_error;
3309 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3312 /* Check whether pwaff is a piecewise constant.
3314 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3316 int i;
3318 if (!pwaff)
3319 return isl_bool_error;
3321 for (i = 0; i < pwaff->n; ++i) {
3322 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3323 if (is_cst < 0 || !is_cst)
3324 return is_cst;
3327 return isl_bool_true;
3330 /* Return the product of "aff1" and "aff2".
3332 * If either of the two is NaN, then the result is NaN.
3334 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3336 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3337 __isl_take isl_aff *aff2)
3339 if (!aff1 || !aff2)
3340 goto error;
3342 if (isl_aff_is_nan(aff1)) {
3343 isl_aff_free(aff2);
3344 return aff1;
3346 if (isl_aff_is_nan(aff2)) {
3347 isl_aff_free(aff1);
3348 return aff2;
3351 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3352 return isl_aff_mul(aff2, aff1);
3354 if (!isl_aff_is_cst(aff2))
3355 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3356 "at least one affine expression should be constant",
3357 goto error);
3359 aff1 = isl_aff_cow(aff1);
3360 if (!aff1 || !aff2)
3361 goto error;
3363 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3364 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3366 isl_aff_free(aff2);
3367 return aff1;
3368 error:
3369 isl_aff_free(aff1);
3370 isl_aff_free(aff2);
3371 return NULL;
3374 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3376 * If either of the two is NaN, then the result is NaN.
3378 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3379 __isl_take isl_aff *aff2)
3381 int is_cst;
3382 int neg;
3384 if (!aff1 || !aff2)
3385 goto error;
3387 if (isl_aff_is_nan(aff1)) {
3388 isl_aff_free(aff2);
3389 return aff1;
3391 if (isl_aff_is_nan(aff2)) {
3392 isl_aff_free(aff1);
3393 return aff2;
3396 is_cst = isl_aff_is_cst(aff2);
3397 if (is_cst < 0)
3398 goto error;
3399 if (!is_cst)
3400 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3401 "second argument should be a constant", goto error);
3403 if (!aff2)
3404 goto error;
3406 neg = isl_int_is_neg(aff2->v->el[1]);
3407 if (neg) {
3408 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3409 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3412 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3413 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3415 if (neg) {
3416 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3417 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3420 isl_aff_free(aff2);
3421 return aff1;
3422 error:
3423 isl_aff_free(aff1);
3424 isl_aff_free(aff2);
3425 return NULL;
3428 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3429 __isl_take isl_pw_aff *pwaff2)
3431 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3434 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3435 __isl_take isl_pw_aff *pwaff2)
3437 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3440 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3441 __isl_take isl_pw_aff *pwaff2)
3443 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3446 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3447 __isl_take isl_pw_aff *pwaff2)
3449 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3452 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3458 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3459 __isl_take isl_pw_aff *pa2)
3461 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3464 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3466 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3467 __isl_take isl_pw_aff *pa2)
3469 int is_cst;
3471 is_cst = isl_pw_aff_is_cst(pa2);
3472 if (is_cst < 0)
3473 goto error;
3474 if (!is_cst)
3475 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3476 "second argument should be a piecewise constant",
3477 goto error);
3478 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3479 error:
3480 isl_pw_aff_free(pa1);
3481 isl_pw_aff_free(pa2);
3482 return NULL;
3485 /* Compute the quotient of the integer division of "pa1" by "pa2"
3486 * with rounding towards zero.
3487 * "pa2" is assumed to be a piecewise constant.
3489 * In particular, return
3491 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3494 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3495 __isl_take isl_pw_aff *pa2)
3497 int is_cst;
3498 isl_set *cond;
3499 isl_pw_aff *f, *c;
3501 is_cst = isl_pw_aff_is_cst(pa2);
3502 if (is_cst < 0)
3503 goto error;
3504 if (!is_cst)
3505 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3506 "second argument should be a piecewise constant",
3507 goto error);
3509 pa1 = isl_pw_aff_div(pa1, pa2);
3511 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3512 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3513 c = isl_pw_aff_ceil(pa1);
3514 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3515 error:
3516 isl_pw_aff_free(pa1);
3517 isl_pw_aff_free(pa2);
3518 return NULL;
3521 /* Compute the remainder of the integer division of "pa1" by "pa2"
3522 * with rounding towards zero.
3523 * "pa2" is assumed to be a piecewise constant.
3525 * In particular, return
3527 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3530 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3531 __isl_take isl_pw_aff *pa2)
3533 int is_cst;
3534 isl_pw_aff *res;
3536 is_cst = isl_pw_aff_is_cst(pa2);
3537 if (is_cst < 0)
3538 goto error;
3539 if (!is_cst)
3540 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3541 "second argument should be a piecewise constant",
3542 goto error);
3543 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3544 res = isl_pw_aff_mul(pa2, res);
3545 res = isl_pw_aff_sub(pa1, res);
3546 return res;
3547 error:
3548 isl_pw_aff_free(pa1);
3549 isl_pw_aff_free(pa2);
3550 return NULL;
3553 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3554 __isl_take isl_pw_aff *pwaff2)
3556 isl_set *le;
3557 isl_set *dom;
3559 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3560 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3561 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3562 isl_pw_aff_copy(pwaff2));
3563 dom = isl_set_subtract(dom, isl_set_copy(le));
3564 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3567 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3568 __isl_take isl_pw_aff *pwaff2)
3570 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3573 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3574 __isl_take isl_pw_aff *pwaff2)
3576 isl_set *ge;
3577 isl_set *dom;
3579 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3580 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3581 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3582 isl_pw_aff_copy(pwaff2));
3583 dom = isl_set_subtract(dom, isl_set_copy(ge));
3584 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3587 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3588 __isl_take isl_pw_aff *pwaff2)
3590 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3593 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3594 __isl_take isl_pw_aff_list *list,
3595 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3596 __isl_take isl_pw_aff *pwaff2))
3598 int i;
3599 isl_ctx *ctx;
3600 isl_pw_aff *res;
3602 if (!list)
3603 return NULL;
3605 ctx = isl_pw_aff_list_get_ctx(list);
3606 if (list->n < 1)
3607 isl_die(ctx, isl_error_invalid,
3608 "list should contain at least one element", goto error);
3610 res = isl_pw_aff_copy(list->p[0]);
3611 for (i = 1; i < list->n; ++i)
3612 res = fn(res, isl_pw_aff_copy(list->p[i]));
3614 isl_pw_aff_list_free(list);
3615 return res;
3616 error:
3617 isl_pw_aff_list_free(list);
3618 return NULL;
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the minimal corresponding affine
3623 * expression.
3625 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3627 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3630 /* Return an isl_pw_aff that maps each element in the intersection of the
3631 * domains of the elements of list to the maximal corresponding affine
3632 * expression.
3634 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3636 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3639 /* Mark the domains of "pwaff" as rational.
3641 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3643 int i;
3645 pwaff = isl_pw_aff_cow(pwaff);
3646 if (!pwaff)
3647 return NULL;
3648 if (pwaff->n == 0)
3649 return pwaff;
3651 for (i = 0; i < pwaff->n; ++i) {
3652 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3653 if (!pwaff->p[i].set)
3654 return isl_pw_aff_free(pwaff);
3657 return pwaff;
3660 /* Mark the domains of the elements of "list" as rational.
3662 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3663 __isl_take isl_pw_aff_list *list)
3665 int i, n;
3667 if (!list)
3668 return NULL;
3669 if (list->n == 0)
3670 return list;
3672 n = list->n;
3673 for (i = 0; i < n; ++i) {
3674 isl_pw_aff *pa;
3676 pa = isl_pw_aff_list_get_pw_aff(list, i);
3677 pa = isl_pw_aff_set_rational(pa);
3678 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3681 return list;
3684 /* Do the parameters of "aff" match those of "space"?
3686 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3687 __isl_keep isl_space *space)
3689 isl_space *aff_space;
3690 int match;
3692 if (!aff || !space)
3693 return -1;
3695 aff_space = isl_aff_get_domain_space(aff);
3697 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3699 isl_space_free(aff_space);
3700 return match;
3703 /* Check that the domain space of "aff" matches "space".
3705 * Return 0 on success and -1 on error.
3707 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3708 __isl_keep isl_space *space)
3710 isl_space *aff_space;
3711 int match;
3713 if (!aff || !space)
3714 return -1;
3716 aff_space = isl_aff_get_domain_space(aff);
3718 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3719 if (match < 0)
3720 goto error;
3721 if (!match)
3722 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3723 "parameters don't match", goto error);
3724 match = isl_space_tuple_is_equal(space, isl_dim_in,
3725 aff_space, isl_dim_set);
3726 if (match < 0)
3727 goto error;
3728 if (!match)
3729 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3730 "domains don't match", goto error);
3731 isl_space_free(aff_space);
3732 return 0;
3733 error:
3734 isl_space_free(aff_space);
3735 return -1;
3738 #undef BASE
3739 #define BASE aff
3740 #undef DOMBASE
3741 #define DOMBASE set
3742 #define NO_DOMAIN
3744 #include <isl_multi_templ.c>
3745 #include <isl_multi_apply_set.c>
3746 #include <isl_multi_floor.c>
3747 #include <isl_multi_gist.c>
3749 #undef NO_DOMAIN
3751 /* Remove any internal structure of the domain of "ma".
3752 * If there is any such internal structure in the input,
3753 * then the name of the corresponding space is also removed.
3755 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3756 __isl_take isl_multi_aff *ma)
3758 isl_space *space;
3760 if (!ma)
3761 return NULL;
3763 if (!ma->space->nested[0])
3764 return ma;
3766 space = isl_multi_aff_get_space(ma);
3767 space = isl_space_flatten_domain(space);
3768 ma = isl_multi_aff_reset_space(ma, space);
3770 return ma;
3773 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3774 * of the space to its domain.
3776 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3778 int i, n_in;
3779 isl_local_space *ls;
3780 isl_multi_aff *ma;
3782 if (!space)
3783 return NULL;
3784 if (!isl_space_is_map(space))
3785 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3786 "not a map space", goto error);
3788 n_in = isl_space_dim(space, isl_dim_in);
3789 space = isl_space_domain_map(space);
3791 ma = isl_multi_aff_alloc(isl_space_copy(space));
3792 if (n_in == 0) {
3793 isl_space_free(space);
3794 return ma;
3797 space = isl_space_domain(space);
3798 ls = isl_local_space_from_space(space);
3799 for (i = 0; i < n_in; ++i) {
3800 isl_aff *aff;
3802 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3803 isl_dim_set, i);
3804 ma = isl_multi_aff_set_aff(ma, i, aff);
3806 isl_local_space_free(ls);
3807 return ma;
3808 error:
3809 isl_space_free(space);
3810 return NULL;
3813 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3814 * of the space to its range.
3816 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3818 int i, n_in, n_out;
3819 isl_local_space *ls;
3820 isl_multi_aff *ma;
3822 if (!space)
3823 return NULL;
3824 if (!isl_space_is_map(space))
3825 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3826 "not a map space", goto error);
3828 n_in = isl_space_dim(space, isl_dim_in);
3829 n_out = isl_space_dim(space, isl_dim_out);
3830 space = isl_space_range_map(space);
3832 ma = isl_multi_aff_alloc(isl_space_copy(space));
3833 if (n_out == 0) {
3834 isl_space_free(space);
3835 return ma;
3838 space = isl_space_domain(space);
3839 ls = isl_local_space_from_space(space);
3840 for (i = 0; i < n_out; ++i) {
3841 isl_aff *aff;
3843 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3844 isl_dim_set, n_in + i);
3845 ma = isl_multi_aff_set_aff(ma, i, aff);
3847 isl_local_space_free(ls);
3848 return ma;
3849 error:
3850 isl_space_free(space);
3851 return NULL;
3854 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3855 * of the space to its range.
3857 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3858 __isl_take isl_space *space)
3860 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3863 /* Given the space of a set and a range of set dimensions,
3864 * construct an isl_multi_aff that projects out those dimensions.
3866 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3867 __isl_take isl_space *space, enum isl_dim_type type,
3868 unsigned first, unsigned n)
3870 int i, dim;
3871 isl_local_space *ls;
3872 isl_multi_aff *ma;
3874 if (!space)
3875 return NULL;
3876 if (!isl_space_is_set(space))
3877 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3878 "expecting set space", goto error);
3879 if (type != isl_dim_set)
3880 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3881 "only set dimensions can be projected out", goto error);
3883 dim = isl_space_dim(space, isl_dim_set);
3884 if (first + n > dim)
3885 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3886 "range out of bounds", goto error);
3888 space = isl_space_from_domain(space);
3889 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3891 if (dim == n)
3892 return isl_multi_aff_alloc(space);
3894 ma = isl_multi_aff_alloc(isl_space_copy(space));
3895 space = isl_space_domain(space);
3896 ls = isl_local_space_from_space(space);
3898 for (i = 0; i < first; ++i) {
3899 isl_aff *aff;
3901 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3902 isl_dim_set, i);
3903 ma = isl_multi_aff_set_aff(ma, i, aff);
3906 for (i = 0; i < dim - (first + n); ++i) {
3907 isl_aff *aff;
3909 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3910 isl_dim_set, first + n + i);
3911 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3914 isl_local_space_free(ls);
3915 return ma;
3916 error:
3917 isl_space_free(space);
3918 return NULL;
3921 /* Given the space of a set and a range of set dimensions,
3922 * construct an isl_pw_multi_aff that projects out those dimensions.
3924 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3925 __isl_take isl_space *space, enum isl_dim_type type,
3926 unsigned first, unsigned n)
3928 isl_multi_aff *ma;
3930 ma = isl_multi_aff_project_out_map(space, type, first, n);
3931 return isl_pw_multi_aff_from_multi_aff(ma);
3934 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3935 * domain.
3937 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3938 __isl_take isl_multi_aff *ma)
3940 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3941 return isl_pw_multi_aff_alloc(dom, ma);
3944 /* Create a piecewise multi-affine expression in the given space that maps each
3945 * input dimension to the corresponding output dimension.
3947 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3948 __isl_take isl_space *space)
3950 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3953 /* Exploit the equalities in "eq" to simplify the affine expressions.
3955 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3956 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3958 int i;
3960 maff = isl_multi_aff_cow(maff);
3961 if (!maff || !eq)
3962 goto error;
3964 for (i = 0; i < maff->n; ++i) {
3965 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3966 isl_basic_set_copy(eq));
3967 if (!maff->p[i])
3968 goto error;
3971 isl_basic_set_free(eq);
3972 return maff;
3973 error:
3974 isl_basic_set_free(eq);
3975 isl_multi_aff_free(maff);
3976 return NULL;
3979 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3980 isl_int f)
3982 int i;
3984 maff = isl_multi_aff_cow(maff);
3985 if (!maff)
3986 return NULL;
3988 for (i = 0; i < maff->n; ++i) {
3989 maff->p[i] = isl_aff_scale(maff->p[i], f);
3990 if (!maff->p[i])
3991 return isl_multi_aff_free(maff);
3994 return maff;
3997 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3998 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4000 maff1 = isl_multi_aff_add(maff1, maff2);
4001 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4002 return maff1;
4005 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4007 if (!maff)
4008 return -1;
4010 return 0;
4013 /* Return the set of domain elements where "ma1" is lexicographically
4014 * smaller than or equal to "ma2".
4016 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4017 __isl_take isl_multi_aff *ma2)
4019 return isl_multi_aff_lex_ge_set(ma2, ma1);
4022 /* Return the set of domain elements where "ma1" is lexicographically
4023 * greater than or equal to "ma2".
4025 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4026 __isl_take isl_multi_aff *ma2)
4028 isl_space *space;
4029 isl_map *map1, *map2;
4030 isl_map *map, *ge;
4032 map1 = isl_map_from_multi_aff(ma1);
4033 map2 = isl_map_from_multi_aff(ma2);
4034 map = isl_map_range_product(map1, map2);
4035 space = isl_space_range(isl_map_get_space(map));
4036 space = isl_space_domain(isl_space_unwrap(space));
4037 ge = isl_map_lex_ge(space);
4038 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4040 return isl_map_domain(map);
4043 #undef PW
4044 #define PW isl_pw_multi_aff
4045 #undef EL
4046 #define EL isl_multi_aff
4047 #undef EL_IS_ZERO
4048 #define EL_IS_ZERO is_empty
4049 #undef ZERO
4050 #define ZERO empty
4051 #undef IS_ZERO
4052 #define IS_ZERO is_empty
4053 #undef FIELD
4054 #define FIELD maff
4055 #undef DEFAULT_IS_ZERO
4056 #define DEFAULT_IS_ZERO 0
4058 #define NO_SUB
4059 #define NO_EVAL
4060 #define NO_OPT
4061 #define NO_INVOLVES_DIMS
4062 #define NO_INSERT_DIMS
4063 #define NO_LIFT
4064 #define NO_MORPH
4066 #include <isl_pw_templ.c>
4068 #undef NO_SUB
4070 #undef UNION
4071 #define UNION isl_union_pw_multi_aff
4072 #undef PART
4073 #define PART isl_pw_multi_aff
4074 #undef PARTS
4075 #define PARTS pw_multi_aff
4077 #define NO_EVAL
4079 #include <isl_union_templ.c>
4081 /* Given a function "cmp" that returns the set of elements where
4082 * "ma1" is "better" than "ma2", return the intersection of this
4083 * set with "dom1" and "dom2".
4085 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
4086 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
4087 __isl_keep isl_multi_aff *ma2,
4088 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4089 __isl_take isl_multi_aff *ma2))
4091 isl_set *common;
4092 isl_set *better;
4093 int is_empty;
4095 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
4096 is_empty = isl_set_plain_is_empty(common);
4097 if (is_empty >= 0 && is_empty)
4098 return common;
4099 if (is_empty < 0)
4100 return isl_set_free(common);
4101 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
4102 better = isl_set_intersect(common, better);
4104 return better;
4107 /* Given a function "cmp" that returns the set of elements where
4108 * "ma1" is "better" than "ma2", return a piecewise multi affine
4109 * expression defined on the union of the definition domains
4110 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4111 * "pma2" on each cell. If only one of the two input functions
4112 * is defined on a given cell, then it is considered the best.
4114 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4115 __isl_take isl_pw_multi_aff *pma1,
4116 __isl_take isl_pw_multi_aff *pma2,
4117 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4118 __isl_take isl_multi_aff *ma2))
4120 int i, j, n;
4121 isl_pw_multi_aff *res = NULL;
4122 isl_ctx *ctx;
4123 isl_set *set = NULL;
4125 if (!pma1 || !pma2)
4126 goto error;
4128 ctx = isl_space_get_ctx(pma1->dim);
4129 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4130 isl_die(ctx, isl_error_invalid,
4131 "arguments should live in the same space", goto error);
4133 if (isl_pw_multi_aff_is_empty(pma1)) {
4134 isl_pw_multi_aff_free(pma1);
4135 return pma2;
4138 if (isl_pw_multi_aff_is_empty(pma2)) {
4139 isl_pw_multi_aff_free(pma2);
4140 return pma1;
4143 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4144 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4146 for (i = 0; i < pma1->n; ++i) {
4147 set = isl_set_copy(pma1->p[i].set);
4148 for (j = 0; j < pma2->n; ++j) {
4149 isl_set *better;
4150 int is_empty;
4152 better = shared_and_better(pma2->p[j].set,
4153 pma1->p[i].set, pma2->p[j].maff,
4154 pma1->p[i].maff, cmp);
4155 is_empty = isl_set_plain_is_empty(better);
4156 if (is_empty < 0 || is_empty) {
4157 isl_set_free(better);
4158 if (is_empty < 0)
4159 goto error;
4160 continue;
4162 set = isl_set_subtract(set, isl_set_copy(better));
4164 res = isl_pw_multi_aff_add_piece(res, better,
4165 isl_multi_aff_copy(pma2->p[j].maff));
4167 res = isl_pw_multi_aff_add_piece(res, set,
4168 isl_multi_aff_copy(pma1->p[i].maff));
4171 for (j = 0; j < pma2->n; ++j) {
4172 set = isl_set_copy(pma2->p[j].set);
4173 for (i = 0; i < pma1->n; ++i)
4174 set = isl_set_subtract(set,
4175 isl_set_copy(pma1->p[i].set));
4176 res = isl_pw_multi_aff_add_piece(res, set,
4177 isl_multi_aff_copy(pma2->p[j].maff));
4180 isl_pw_multi_aff_free(pma1);
4181 isl_pw_multi_aff_free(pma2);
4183 return res;
4184 error:
4185 isl_pw_multi_aff_free(pma1);
4186 isl_pw_multi_aff_free(pma2);
4187 isl_set_free(set);
4188 return isl_pw_multi_aff_free(res);
4191 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4192 __isl_take isl_pw_multi_aff *pma1,
4193 __isl_take isl_pw_multi_aff *pma2)
4195 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4198 /* Given two piecewise multi affine expressions, return a piecewise
4199 * multi-affine expression defined on the union of the definition domains
4200 * of the inputs that is equal to the lexicographic maximum of the two
4201 * inputs on each cell. If only one of the two inputs is defined on
4202 * a given cell, then it is considered to be the maximum.
4204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4205 __isl_take isl_pw_multi_aff *pma1,
4206 __isl_take isl_pw_multi_aff *pma2)
4208 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4209 &pw_multi_aff_union_lexmax);
4212 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4213 __isl_take isl_pw_multi_aff *pma1,
4214 __isl_take isl_pw_multi_aff *pma2)
4216 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4219 /* Given two piecewise multi affine expressions, return a piecewise
4220 * multi-affine expression defined on the union of the definition domains
4221 * of the inputs that is equal to the lexicographic minimum of the two
4222 * inputs on each cell. If only one of the two inputs is defined on
4223 * a given cell, then it is considered to be the minimum.
4225 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4226 __isl_take isl_pw_multi_aff *pma1,
4227 __isl_take isl_pw_multi_aff *pma2)
4229 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4230 &pw_multi_aff_union_lexmin);
4233 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4234 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4236 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4237 &isl_multi_aff_add);
4240 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4241 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4243 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4244 &pw_multi_aff_add);
4247 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4248 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4250 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4251 &isl_multi_aff_sub);
4254 /* Subtract "pma2" from "pma1" and return the result.
4256 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4257 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4259 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4260 &pw_multi_aff_sub);
4263 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4264 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4266 return isl_pw_multi_aff_union_add_(pma1, pma2);
4269 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4270 * with the actual sum on the shared domain and
4271 * the defined expression on the symmetric difference of the domains.
4273 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4274 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4276 return isl_union_pw_aff_union_add_(upa1, upa2);
4279 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4280 * with the actual sum on the shared domain and
4281 * the defined expression on the symmetric difference of the domains.
4283 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4284 __isl_take isl_union_pw_multi_aff *upma1,
4285 __isl_take isl_union_pw_multi_aff *upma2)
4287 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4290 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4291 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4293 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4294 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4296 int i, j, n;
4297 isl_space *space;
4298 isl_pw_multi_aff *res;
4300 if (!pma1 || !pma2)
4301 goto error;
4303 n = pma1->n * pma2->n;
4304 space = isl_space_product(isl_space_copy(pma1->dim),
4305 isl_space_copy(pma2->dim));
4306 res = isl_pw_multi_aff_alloc_size(space, n);
4308 for (i = 0; i < pma1->n; ++i) {
4309 for (j = 0; j < pma2->n; ++j) {
4310 isl_set *domain;
4311 isl_multi_aff *ma;
4313 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4314 isl_set_copy(pma2->p[j].set));
4315 ma = isl_multi_aff_product(
4316 isl_multi_aff_copy(pma1->p[i].maff),
4317 isl_multi_aff_copy(pma2->p[j].maff));
4318 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4322 isl_pw_multi_aff_free(pma1);
4323 isl_pw_multi_aff_free(pma2);
4324 return res;
4325 error:
4326 isl_pw_multi_aff_free(pma1);
4327 isl_pw_multi_aff_free(pma2);
4328 return NULL;
4331 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4332 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4334 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4335 &pw_multi_aff_product);
4338 /* Construct a map mapping the domain of the piecewise multi-affine expression
4339 * to its range, with each dimension in the range equated to the
4340 * corresponding affine expression on its cell.
4342 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4344 int i;
4345 isl_map *map;
4347 if (!pma)
4348 return NULL;
4350 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4352 for (i = 0; i < pma->n; ++i) {
4353 isl_multi_aff *maff;
4354 isl_basic_map *bmap;
4355 isl_map *map_i;
4357 maff = isl_multi_aff_copy(pma->p[i].maff);
4358 bmap = isl_basic_map_from_multi_aff(maff);
4359 map_i = isl_map_from_basic_map(bmap);
4360 map_i = isl_map_intersect_domain(map_i,
4361 isl_set_copy(pma->p[i].set));
4362 map = isl_map_union_disjoint(map, map_i);
4365 isl_pw_multi_aff_free(pma);
4366 return map;
4369 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4371 if (!pma)
4372 return NULL;
4374 if (!isl_space_is_set(pma->dim))
4375 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4376 "isl_pw_multi_aff cannot be converted into an isl_set",
4377 goto error);
4379 return isl_map_from_pw_multi_aff(pma);
4380 error:
4381 isl_pw_multi_aff_free(pma);
4382 return NULL;
4385 /* Given a basic map with a single output dimension that is defined
4386 * in terms of the parameters and input dimensions using an equality,
4387 * extract an isl_aff that expresses the output dimension in terms
4388 * of the parameters and input dimensions.
4389 * Note that this expression may involve integer divisions defined
4390 * in terms of parameters and input dimensions.
4392 * This function shares some similarities with
4393 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4395 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4396 __isl_take isl_basic_map *bmap)
4398 int eq;
4399 unsigned offset;
4400 unsigned n_div;
4401 isl_local_space *ls;
4402 isl_aff *aff;
4404 if (!bmap)
4405 return NULL;
4406 if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
4407 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4408 "basic map should have a single output dimension",
4409 goto error);
4410 eq = isl_basic_map_output_defining_equality(bmap, 0);
4411 if (eq >= bmap->n_eq)
4412 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4413 "unable to find suitable equality", goto error);
4414 ls = isl_basic_map_get_local_space(bmap);
4415 aff = isl_aff_alloc(isl_local_space_domain(ls));
4416 if (!aff)
4417 goto error;
4418 offset = isl_basic_map_offset(bmap, isl_dim_out);
4419 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4420 if (isl_int_is_neg(bmap->eq[eq][offset])) {
4421 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], offset);
4422 isl_seq_cpy(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4423 n_div);
4424 } else {
4425 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], offset);
4426 isl_seq_neg(aff->v->el + 1 + offset, bmap->eq[eq] + offset + 1,
4427 n_div);
4429 isl_int_abs(aff->v->el[0], bmap->eq[eq][offset]);
4430 isl_basic_map_free(bmap);
4432 aff = isl_aff_remove_unused_divs(aff);
4433 return aff;
4434 error:
4435 isl_basic_map_free(bmap);
4436 return NULL;
4439 /* Given a basic map where each output dimension is defined
4440 * in terms of the parameters and input dimensions using an equality,
4441 * extract an isl_multi_aff that expresses the output dimensions in terms
4442 * of the parameters and input dimensions.
4444 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4445 __isl_take isl_basic_map *bmap)
4447 int i;
4448 unsigned n_out;
4449 isl_multi_aff *ma;
4451 if (!bmap)
4452 return NULL;
4454 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4455 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4457 for (i = 0; i < n_out; ++i) {
4458 isl_basic_map *bmap_i;
4459 isl_aff *aff;
4461 bmap_i = isl_basic_map_copy(bmap);
4462 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
4463 i + 1, n_out - (1 + i));
4464 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
4465 aff = extract_isl_aff_from_basic_map(bmap_i);
4466 ma = isl_multi_aff_set_aff(ma, i, aff);
4469 isl_basic_map_free(bmap);
4471 return ma;
4474 /* Given a basic set where each set dimension is defined
4475 * in terms of the parameters using an equality,
4476 * extract an isl_multi_aff that expresses the set dimensions in terms
4477 * of the parameters.
4479 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4480 __isl_take isl_basic_set *bset)
4482 return extract_isl_multi_aff_from_basic_map(bset);
4485 /* Create an isl_pw_multi_aff that is equivalent to
4486 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4487 * The given basic map is such that each output dimension is defined
4488 * in terms of the parameters and input dimensions using an equality.
4490 * Since some applications expect the result of isl_pw_multi_aff_from_map
4491 * to only contain integer affine expressions, we compute the floor
4492 * of the expression before returning.
4494 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4495 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4497 isl_multi_aff *ma;
4499 ma = extract_isl_multi_aff_from_basic_map(bmap);
4500 ma = isl_multi_aff_floor(ma);
4501 return isl_pw_multi_aff_alloc(domain, ma);
4504 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4505 * This obviously only works if the input "map" is single-valued.
4506 * If so, we compute the lexicographic minimum of the image in the form
4507 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4508 * to its lexicographic minimum.
4509 * If the input is not single-valued, we produce an error.
4511 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4512 __isl_take isl_map *map)
4514 int i;
4515 int sv;
4516 isl_pw_multi_aff *pma;
4518 sv = isl_map_is_single_valued(map);
4519 if (sv < 0)
4520 goto error;
4521 if (!sv)
4522 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4523 "map is not single-valued", goto error);
4524 map = isl_map_make_disjoint(map);
4525 if (!map)
4526 return NULL;
4528 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4530 for (i = 0; i < map->n; ++i) {
4531 isl_pw_multi_aff *pma_i;
4532 isl_basic_map *bmap;
4533 bmap = isl_basic_map_copy(map->p[i]);
4534 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4535 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4538 isl_map_free(map);
4539 return pma;
4540 error:
4541 isl_map_free(map);
4542 return NULL;
4545 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4546 * taking into account that the output dimension at position "d"
4547 * can be represented as
4549 * x = floor((e(...) + c1) / m)
4551 * given that constraint "i" is of the form
4553 * e(...) + c1 - m x >= 0
4556 * Let "map" be of the form
4558 * A -> B
4560 * We construct a mapping
4562 * A -> [A -> x = floor(...)]
4564 * apply that to the map, obtaining
4566 * [A -> x = floor(...)] -> B
4568 * and equate dimension "d" to x.
4569 * We then compute a isl_pw_multi_aff representation of the resulting map
4570 * and plug in the mapping above.
4572 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4573 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4575 isl_ctx *ctx;
4576 isl_space *space;
4577 isl_local_space *ls;
4578 isl_multi_aff *ma;
4579 isl_aff *aff;
4580 isl_vec *v;
4581 isl_map *insert;
4582 int offset;
4583 int n;
4584 int n_in;
4585 isl_pw_multi_aff *pma;
4586 int is_set;
4588 is_set = isl_map_is_set(map);
4590 offset = isl_basic_map_offset(hull, isl_dim_out);
4591 ctx = isl_map_get_ctx(map);
4592 space = isl_space_domain(isl_map_get_space(map));
4593 n_in = isl_space_dim(space, isl_dim_set);
4594 n = isl_space_dim(space, isl_dim_all);
4596 v = isl_vec_alloc(ctx, 1 + 1 + n);
4597 if (v) {
4598 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4599 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4601 isl_basic_map_free(hull);
4603 ls = isl_local_space_from_space(isl_space_copy(space));
4604 aff = isl_aff_alloc_vec(ls, v);
4605 aff = isl_aff_floor(aff);
4606 if (is_set) {
4607 isl_space_free(space);
4608 ma = isl_multi_aff_from_aff(aff);
4609 } else {
4610 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4611 ma = isl_multi_aff_range_product(ma,
4612 isl_multi_aff_from_aff(aff));
4615 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4616 map = isl_map_apply_domain(map, insert);
4617 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4618 pma = isl_pw_multi_aff_from_map(map);
4619 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4621 return pma;
4624 /* Is constraint "c" of the form
4626 * e(...) + c1 - m x >= 0
4628 * or
4630 * -e(...) + c2 + m x >= 0
4632 * where m > 1 and e only depends on parameters and input dimemnsions?
4634 * "offset" is the offset of the output dimensions
4635 * "pos" is the position of output dimension x.
4637 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4639 if (isl_int_is_zero(c[offset + d]))
4640 return 0;
4641 if (isl_int_is_one(c[offset + d]))
4642 return 0;
4643 if (isl_int_is_negone(c[offset + d]))
4644 return 0;
4645 if (isl_seq_first_non_zero(c + offset, d) != -1)
4646 return 0;
4647 if (isl_seq_first_non_zero(c + offset + d + 1,
4648 total - (offset + d + 1)) != -1)
4649 return 0;
4650 return 1;
4653 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4655 * As a special case, we first check if there is any pair of constraints,
4656 * shared by all the basic maps in "map" that force a given dimension
4657 * to be equal to the floor of some affine combination of the input dimensions.
4659 * In particular, if we can find two constraints
4661 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4663 * and
4665 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4667 * where m > 1 and e only depends on parameters and input dimemnsions,
4668 * and such that
4670 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4672 * then we know that we can take
4674 * x = floor((e(...) + c1) / m)
4676 * without having to perform any computation.
4678 * Note that we know that
4680 * c1 + c2 >= 1
4682 * If c1 + c2 were 0, then we would have detected an equality during
4683 * simplification. If c1 + c2 were negative, then we would have detected
4684 * a contradiction.
4686 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4687 __isl_take isl_map *map)
4689 int d, dim;
4690 int i, j, n;
4691 int offset, total;
4692 isl_int sum;
4693 isl_basic_map *hull;
4695 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4696 if (!hull)
4697 goto error;
4699 isl_int_init(sum);
4700 dim = isl_map_dim(map, isl_dim_out);
4701 offset = isl_basic_map_offset(hull, isl_dim_out);
4702 total = 1 + isl_basic_map_total_dim(hull);
4703 n = hull->n_ineq;
4704 for (d = 0; d < dim; ++d) {
4705 for (i = 0; i < n; ++i) {
4706 if (!is_potential_div_constraint(hull->ineq[i],
4707 offset, d, total))
4708 continue;
4709 for (j = i + 1; j < n; ++j) {
4710 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4711 hull->ineq[j] + 1, total - 1))
4712 continue;
4713 isl_int_add(sum, hull->ineq[i][0],
4714 hull->ineq[j][0]);
4715 if (isl_int_abs_lt(sum,
4716 hull->ineq[i][offset + d]))
4717 break;
4720 if (j >= n)
4721 continue;
4722 isl_int_clear(sum);
4723 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4724 j = i;
4725 return pw_multi_aff_from_map_div(map, hull, d, j);
4728 isl_int_clear(sum);
4729 isl_basic_map_free(hull);
4730 return pw_multi_aff_from_map_base(map);
4731 error:
4732 isl_map_free(map);
4733 isl_basic_map_free(hull);
4734 return NULL;
4737 /* Given an affine expression
4739 * [A -> B] -> f(A,B)
4741 * construct an isl_multi_aff
4743 * [A -> B] -> B'
4745 * such that dimension "d" in B' is set to "aff" and the remaining
4746 * dimensions are set equal to the corresponding dimensions in B.
4747 * "n_in" is the dimension of the space A.
4748 * "n_out" is the dimension of the space B.
4750 * If "is_set" is set, then the affine expression is of the form
4752 * [B] -> f(B)
4754 * and we construct an isl_multi_aff
4756 * B -> B'
4758 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4759 unsigned n_in, unsigned n_out, int is_set)
4761 int i;
4762 isl_multi_aff *ma;
4763 isl_space *space, *space2;
4764 isl_local_space *ls;
4766 space = isl_aff_get_domain_space(aff);
4767 ls = isl_local_space_from_space(isl_space_copy(space));
4768 space2 = isl_space_copy(space);
4769 if (!is_set)
4770 space2 = isl_space_range(isl_space_unwrap(space2));
4771 space = isl_space_map_from_domain_and_range(space, space2);
4772 ma = isl_multi_aff_alloc(space);
4773 ma = isl_multi_aff_set_aff(ma, d, aff);
4775 for (i = 0; i < n_out; ++i) {
4776 if (i == d)
4777 continue;
4778 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4779 isl_dim_set, n_in + i);
4780 ma = isl_multi_aff_set_aff(ma, i, aff);
4783 isl_local_space_free(ls);
4785 return ma;
4788 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4789 * taking into account that the dimension at position "d" can be written as
4791 * x = m a + f(..) (1)
4793 * where m is equal to "gcd".
4794 * "i" is the index of the equality in "hull" that defines f(..).
4795 * In particular, the equality is of the form
4797 * f(..) - x + m g(existentials) = 0
4799 * or
4801 * -f(..) + x + m g(existentials) = 0
4803 * We basically plug (1) into "map", resulting in a map with "a"
4804 * in the range instead of "x". The corresponding isl_pw_multi_aff
4805 * defining "a" is then plugged back into (1) to obtain a definition fro "x".
4807 * Specifically, given the input map
4809 * A -> B
4811 * We first wrap it into a set
4813 * [A -> B]
4815 * and define (1) on top of the corresponding space, resulting in "aff".
4816 * We use this to create an isl_multi_aff that maps the output position "d"
4817 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4818 * We plug this into the wrapped map, unwrap the result and compute the
4819 * corresponding isl_pw_multi_aff.
4820 * The result is an expression
4822 * A -> T(A)
4824 * We adjust that to
4826 * A -> [A -> T(A)]
4828 * so that we can plug that into "aff", after extending the latter to
4829 * a mapping
4831 * [A -> B] -> B'
4834 * If "map" is actually a set, then there is no "A" space, meaning
4835 * that we do not need to perform any wrapping, and that the result
4836 * of the recursive call is of the form
4838 * [T]
4840 * which is plugged into a mapping of the form
4842 * B -> B'
4844 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4845 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4846 isl_int gcd)
4848 isl_set *set;
4849 isl_space *space;
4850 isl_local_space *ls;
4851 isl_aff *aff;
4852 isl_multi_aff *ma;
4853 isl_pw_multi_aff *pma, *id;
4854 unsigned n_in;
4855 unsigned o_out;
4856 unsigned n_out;
4857 int is_set;
4859 is_set = isl_map_is_set(map);
4861 n_in = isl_basic_map_dim(hull, isl_dim_in);
4862 n_out = isl_basic_map_dim(hull, isl_dim_out);
4863 o_out = isl_basic_map_offset(hull, isl_dim_out);
4865 if (is_set)
4866 set = map;
4867 else
4868 set = isl_map_wrap(map);
4869 space = isl_space_map_from_set(isl_set_get_space(set));
4870 ma = isl_multi_aff_identity(space);
4871 ls = isl_local_space_from_space(isl_set_get_space(set));
4872 aff = isl_aff_alloc(ls);
4873 if (aff) {
4874 isl_int_set_si(aff->v->el[0], 1);
4875 if (isl_int_is_one(hull->eq[i][o_out + d]))
4876 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4877 aff->v->size - 1);
4878 else
4879 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4880 aff->v->size - 1);
4881 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4883 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4884 set = isl_set_preimage_multi_aff(set, ma);
4886 ma = range_map(aff, d, n_in, n_out, is_set);
4888 if (is_set)
4889 map = set;
4890 else
4891 map = isl_set_unwrap(set);
4892 pma = isl_pw_multi_aff_from_map(map);
4894 if (!is_set) {
4895 space = isl_pw_multi_aff_get_domain_space(pma);
4896 space = isl_space_map_from_set(space);
4897 id = isl_pw_multi_aff_identity(space);
4898 pma = isl_pw_multi_aff_range_product(id, pma);
4900 id = isl_pw_multi_aff_from_multi_aff(ma);
4901 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4903 isl_basic_map_free(hull);
4904 return pma;
4907 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4909 * As a special case, we first check if all output dimensions are uniquely
4910 * defined in terms of the parameters and input dimensions over the entire
4911 * domain. If so, we extract the desired isl_pw_multi_aff directly
4912 * from the affine hull of "map" and its domain.
4914 * Otherwise, we check if any of the output dimensions is "strided".
4915 * That is, we check if can be written as
4917 * x = m a + f(..)
4919 * with m greater than 1, a some combination of existentiall quantified
4920 * variables and f and expression in the parameters and input dimensions.
4921 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4923 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4924 * special case.
4926 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4928 int i, j;
4929 int sv;
4930 isl_basic_map *hull;
4931 unsigned n_out;
4932 unsigned o_out;
4933 unsigned n_div;
4934 unsigned o_div;
4935 isl_int gcd;
4937 if (!map)
4938 return NULL;
4940 hull = isl_map_affine_hull(isl_map_copy(map));
4941 sv = isl_basic_map_plain_is_single_valued(hull);
4942 if (sv >= 0 && sv)
4943 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
4944 if (sv < 0)
4945 hull = isl_basic_map_free(hull);
4946 if (!hull)
4947 goto error;
4949 n_div = isl_basic_map_dim(hull, isl_dim_div);
4950 o_div = isl_basic_map_offset(hull, isl_dim_div);
4952 if (n_div == 0) {
4953 isl_basic_map_free(hull);
4954 return pw_multi_aff_from_map_check_div(map);
4957 isl_int_init(gcd);
4959 n_out = isl_basic_map_dim(hull, isl_dim_out);
4960 o_out = isl_basic_map_offset(hull, isl_dim_out);
4962 for (i = 0; i < n_out; ++i) {
4963 for (j = 0; j < hull->n_eq; ++j) {
4964 isl_int *eq = hull->eq[j];
4965 isl_pw_multi_aff *res;
4967 if (!isl_int_is_one(eq[o_out + i]) &&
4968 !isl_int_is_negone(eq[o_out + i]))
4969 continue;
4970 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
4971 continue;
4972 if (isl_seq_first_non_zero(eq + o_out + i + 1,
4973 n_out - (i + 1)) != -1)
4974 continue;
4975 isl_seq_gcd(eq + o_div, n_div, &gcd);
4976 if (isl_int_is_zero(gcd))
4977 continue;
4978 if (isl_int_is_one(gcd))
4979 continue;
4981 res = pw_multi_aff_from_map_stride(map, hull,
4982 i, j, gcd);
4983 isl_int_clear(gcd);
4984 return res;
4988 isl_int_clear(gcd);
4989 isl_basic_map_free(hull);
4990 return pw_multi_aff_from_map_check_div(map);
4991 error:
4992 isl_map_free(map);
4993 return NULL;
4996 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
4998 return isl_pw_multi_aff_from_map(set);
5001 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5002 * add it to *user.
5004 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5006 isl_union_pw_multi_aff **upma = user;
5007 isl_pw_multi_aff *pma;
5009 pma = isl_pw_multi_aff_from_map(map);
5010 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5012 return *upma ? isl_stat_ok : isl_stat_error;
5015 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5016 * domain.
5018 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5019 __isl_take isl_aff *aff)
5021 isl_multi_aff *ma;
5022 isl_pw_multi_aff *pma;
5024 ma = isl_multi_aff_from_aff(aff);
5025 pma = isl_pw_multi_aff_from_multi_aff(ma);
5026 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5029 /* Try and create an isl_union_pw_multi_aff that is equivalent
5030 * to the given isl_union_map.
5031 * The isl_union_map is required to be single-valued in each space.
5032 * Otherwise, an error is produced.
5034 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5035 __isl_take isl_union_map *umap)
5037 isl_space *space;
5038 isl_union_pw_multi_aff *upma;
5040 space = isl_union_map_get_space(umap);
5041 upma = isl_union_pw_multi_aff_empty(space);
5042 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5043 upma = isl_union_pw_multi_aff_free(upma);
5044 isl_union_map_free(umap);
5046 return upma;
5049 /* Try and create an isl_union_pw_multi_aff that is equivalent
5050 * to the given isl_union_set.
5051 * The isl_union_set is required to be a singleton in each space.
5052 * Otherwise, an error is produced.
5054 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5055 __isl_take isl_union_set *uset)
5057 return isl_union_pw_multi_aff_from_union_map(uset);
5060 /* Return the piecewise affine expression "set ? 1 : 0".
5062 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5064 isl_pw_aff *pa;
5065 isl_space *space = isl_set_get_space(set);
5066 isl_local_space *ls = isl_local_space_from_space(space);
5067 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5068 isl_aff *one = isl_aff_zero_on_domain(ls);
5070 one = isl_aff_add_constant_si(one, 1);
5071 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5072 set = isl_set_complement(set);
5073 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5075 return pa;
5078 /* Plug in "subs" for dimension "type", "pos" of "aff".
5080 * Let i be the dimension to replace and let "subs" be of the form
5082 * f/d
5084 * and "aff" of the form
5086 * (a i + g)/m
5088 * The result is
5090 * (a f + d g')/(m d)
5092 * where g' is the result of plugging in "subs" in each of the integer
5093 * divisions in g.
5095 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5096 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5098 isl_ctx *ctx;
5099 isl_int v;
5101 aff = isl_aff_cow(aff);
5102 if (!aff || !subs)
5103 return isl_aff_free(aff);
5105 ctx = isl_aff_get_ctx(aff);
5106 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5107 isl_die(ctx, isl_error_invalid,
5108 "spaces don't match", return isl_aff_free(aff));
5109 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5110 isl_die(ctx, isl_error_unsupported,
5111 "cannot handle divs yet", return isl_aff_free(aff));
5113 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5114 if (!aff->ls)
5115 return isl_aff_free(aff);
5117 aff->v = isl_vec_cow(aff->v);
5118 if (!aff->v)
5119 return isl_aff_free(aff);
5121 pos += isl_local_space_offset(aff->ls, type);
5123 isl_int_init(v);
5124 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5125 aff->v->size, subs->v->size, v);
5126 isl_int_clear(v);
5128 return aff;
5131 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5132 * expressions in "maff".
5134 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5135 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5136 __isl_keep isl_aff *subs)
5138 int i;
5140 maff = isl_multi_aff_cow(maff);
5141 if (!maff || !subs)
5142 return isl_multi_aff_free(maff);
5144 if (type == isl_dim_in)
5145 type = isl_dim_set;
5147 for (i = 0; i < maff->n; ++i) {
5148 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5149 if (!maff->p[i])
5150 return isl_multi_aff_free(maff);
5153 return maff;
5156 /* Plug in "subs" for dimension "type", "pos" of "pma".
5158 * pma is of the form
5160 * A_i(v) -> M_i(v)
5162 * while subs is of the form
5164 * v' = B_j(v) -> S_j
5166 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5167 * has a contribution in the result, in particular
5169 * C_ij(S_j) -> M_i(S_j)
5171 * Note that plugging in S_j in C_ij may also result in an empty set
5172 * and this contribution should simply be discarded.
5174 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5175 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5176 __isl_keep isl_pw_aff *subs)
5178 int i, j, n;
5179 isl_pw_multi_aff *res;
5181 if (!pma || !subs)
5182 return isl_pw_multi_aff_free(pma);
5184 n = pma->n * subs->n;
5185 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5187 for (i = 0; i < pma->n; ++i) {
5188 for (j = 0; j < subs->n; ++j) {
5189 isl_set *common;
5190 isl_multi_aff *res_ij;
5191 int empty;
5193 common = isl_set_intersect(
5194 isl_set_copy(pma->p[i].set),
5195 isl_set_copy(subs->p[j].set));
5196 common = isl_set_substitute(common,
5197 type, pos, subs->p[j].aff);
5198 empty = isl_set_plain_is_empty(common);
5199 if (empty < 0 || empty) {
5200 isl_set_free(common);
5201 if (empty < 0)
5202 goto error;
5203 continue;
5206 res_ij = isl_multi_aff_substitute(
5207 isl_multi_aff_copy(pma->p[i].maff),
5208 type, pos, subs->p[j].aff);
5210 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5214 isl_pw_multi_aff_free(pma);
5215 return res;
5216 error:
5217 isl_pw_multi_aff_free(pma);
5218 isl_pw_multi_aff_free(res);
5219 return NULL;
5222 /* Compute the preimage of a range of dimensions in the affine expression "src"
5223 * under "ma" and put the result in "dst". The number of dimensions in "src"
5224 * that precede the range is given by "n_before". The number of dimensions
5225 * in the range is given by the number of output dimensions of "ma".
5226 * The number of dimensions that follow the range is given by "n_after".
5227 * If "has_denom" is set (to one),
5228 * then "src" and "dst" have an extra initial denominator.
5229 * "n_div_ma" is the number of existentials in "ma"
5230 * "n_div_bset" is the number of existentials in "src"
5231 * The resulting "dst" (which is assumed to have been allocated by
5232 * the caller) contains coefficients for both sets of existentials,
5233 * first those in "ma" and then those in "src".
5234 * f, c1, c2 and g are temporary objects that have been initialized
5235 * by the caller.
5237 * Let src represent the expression
5239 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5241 * and let ma represent the expressions
5243 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5245 * We start out with the following expression for dst:
5247 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5249 * with the multiplication factor f initially equal to 1
5250 * and f \sum_i b_i v_i kept separately.
5251 * For each x_i that we substitute, we multiply the numerator
5252 * (and denominator) of dst by c_1 = m_i and add the numerator
5253 * of the x_i expression multiplied by c_2 = f b_i,
5254 * after removing the common factors of c_1 and c_2.
5255 * The multiplication factor f also needs to be multiplied by c_1
5256 * for the next x_j, j > i.
5258 void isl_seq_preimage(isl_int *dst, isl_int *src,
5259 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5260 int n_div_ma, int n_div_bmap,
5261 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5263 int i;
5264 int n_param, n_in, n_out;
5265 int o_dst, o_src;
5267 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5268 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5269 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5271 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5272 o_dst = o_src = has_denom + 1 + n_param + n_before;
5273 isl_seq_clr(dst + o_dst, n_in);
5274 o_dst += n_in;
5275 o_src += n_out;
5276 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5277 o_dst += n_after;
5278 o_src += n_after;
5279 isl_seq_clr(dst + o_dst, n_div_ma);
5280 o_dst += n_div_ma;
5281 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5283 isl_int_set_si(f, 1);
5285 for (i = 0; i < n_out; ++i) {
5286 int offset = has_denom + 1 + n_param + n_before + i;
5288 if (isl_int_is_zero(src[offset]))
5289 continue;
5290 isl_int_set(c1, ma->p[i]->v->el[0]);
5291 isl_int_mul(c2, f, src[offset]);
5292 isl_int_gcd(g, c1, c2);
5293 isl_int_divexact(c1, c1, g);
5294 isl_int_divexact(c2, c2, g);
5296 isl_int_mul(f, f, c1);
5297 o_dst = has_denom;
5298 o_src = 1;
5299 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5300 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5301 o_dst += 1 + n_param;
5302 o_src += 1 + n_param;
5303 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5304 o_dst += n_before;
5305 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5306 c2, ma->p[i]->v->el + o_src, n_in);
5307 o_dst += n_in;
5308 o_src += n_in;
5309 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5310 o_dst += n_after;
5311 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5312 c2, ma->p[i]->v->el + o_src, n_div_ma);
5313 o_dst += n_div_ma;
5314 o_src += n_div_ma;
5315 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5316 if (has_denom)
5317 isl_int_mul(dst[0], dst[0], c1);
5321 /* Compute the pullback of "aff" by the function represented by "ma".
5322 * In other words, plug in "ma" in "aff". The result is an affine expression
5323 * defined over the domain space of "ma".
5325 * If "aff" is represented by
5327 * (a(p) + b x + c(divs))/d
5329 * and ma is represented by
5331 * x = D(p) + F(y) + G(divs')
5333 * then the result is
5335 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5337 * The divs in the local space of the input are similarly adjusted
5338 * through a call to isl_local_space_preimage_multi_aff.
5340 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5341 __isl_take isl_multi_aff *ma)
5343 isl_aff *res = NULL;
5344 isl_local_space *ls;
5345 int n_div_aff, n_div_ma;
5346 isl_int f, c1, c2, g;
5348 ma = isl_multi_aff_align_divs(ma);
5349 if (!aff || !ma)
5350 goto error;
5352 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5353 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5355 ls = isl_aff_get_domain_local_space(aff);
5356 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5357 res = isl_aff_alloc(ls);
5358 if (!res)
5359 goto error;
5361 isl_int_init(f);
5362 isl_int_init(c1);
5363 isl_int_init(c2);
5364 isl_int_init(g);
5366 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5367 f, c1, c2, g, 1);
5369 isl_int_clear(f);
5370 isl_int_clear(c1);
5371 isl_int_clear(c2);
5372 isl_int_clear(g);
5374 isl_aff_free(aff);
5375 isl_multi_aff_free(ma);
5376 res = isl_aff_normalize(res);
5377 return res;
5378 error:
5379 isl_aff_free(aff);
5380 isl_multi_aff_free(ma);
5381 isl_aff_free(res);
5382 return NULL;
5385 /* Compute the pullback of "aff1" by the function represented by "aff2".
5386 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5387 * defined over the domain space of "aff1".
5389 * The domain of "aff1" should match the range of "aff2", which means
5390 * that it should be single-dimensional.
5392 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5393 __isl_take isl_aff *aff2)
5395 isl_multi_aff *ma;
5397 ma = isl_multi_aff_from_aff(aff2);
5398 return isl_aff_pullback_multi_aff(aff1, ma);
5401 /* Compute the pullback of "ma1" by the function represented by "ma2".
5402 * In other words, plug in "ma2" in "ma1".
5404 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5406 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5407 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5409 int i;
5410 isl_space *space = NULL;
5412 ma2 = isl_multi_aff_align_divs(ma2);
5413 ma1 = isl_multi_aff_cow(ma1);
5414 if (!ma1 || !ma2)
5415 goto error;
5417 space = isl_space_join(isl_multi_aff_get_space(ma2),
5418 isl_multi_aff_get_space(ma1));
5420 for (i = 0; i < ma1->n; ++i) {
5421 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5422 isl_multi_aff_copy(ma2));
5423 if (!ma1->p[i])
5424 goto error;
5427 ma1 = isl_multi_aff_reset_space(ma1, space);
5428 isl_multi_aff_free(ma2);
5429 return ma1;
5430 error:
5431 isl_space_free(space);
5432 isl_multi_aff_free(ma2);
5433 isl_multi_aff_free(ma1);
5434 return NULL;
5437 /* Compute the pullback of "ma1" by the function represented by "ma2".
5438 * In other words, plug in "ma2" in "ma1".
5440 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5441 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5443 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5444 &isl_multi_aff_pullback_multi_aff_aligned);
5447 /* Extend the local space of "dst" to include the divs
5448 * in the local space of "src".
5450 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5451 __isl_keep isl_aff *src)
5453 isl_ctx *ctx;
5454 int *exp1 = NULL;
5455 int *exp2 = NULL;
5456 isl_mat *div;
5458 if (!src || !dst)
5459 return isl_aff_free(dst);
5461 ctx = isl_aff_get_ctx(src);
5462 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5463 isl_die(ctx, isl_error_invalid,
5464 "spaces don't match", goto error);
5466 if (src->ls->div->n_row == 0)
5467 return dst;
5469 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5470 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5471 if (!exp1 || (dst->ls->div->n_row && !exp2))
5472 goto error;
5474 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5475 dst = isl_aff_expand_divs(dst, div, exp2);
5476 free(exp1);
5477 free(exp2);
5479 return dst;
5480 error:
5481 free(exp1);
5482 free(exp2);
5483 return isl_aff_free(dst);
5486 /* Adjust the local spaces of the affine expressions in "maff"
5487 * such that they all have the save divs.
5489 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5490 __isl_take isl_multi_aff *maff)
5492 int i;
5494 if (!maff)
5495 return NULL;
5496 if (maff->n == 0)
5497 return maff;
5498 maff = isl_multi_aff_cow(maff);
5499 if (!maff)
5500 return NULL;
5502 for (i = 1; i < maff->n; ++i)
5503 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5504 for (i = 1; i < maff->n; ++i) {
5505 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5506 if (!maff->p[i])
5507 return isl_multi_aff_free(maff);
5510 return maff;
5513 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5515 aff = isl_aff_cow(aff);
5516 if (!aff)
5517 return NULL;
5519 aff->ls = isl_local_space_lift(aff->ls);
5520 if (!aff->ls)
5521 return isl_aff_free(aff);
5523 return aff;
5526 /* Lift "maff" to a space with extra dimensions such that the result
5527 * has no more existentially quantified variables.
5528 * If "ls" is not NULL, then *ls is assigned the local space that lies
5529 * at the basis of the lifting applied to "maff".
5531 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5532 __isl_give isl_local_space **ls)
5534 int i;
5535 isl_space *space;
5536 unsigned n_div;
5538 if (ls)
5539 *ls = NULL;
5541 if (!maff)
5542 return NULL;
5544 if (maff->n == 0) {
5545 if (ls) {
5546 isl_space *space = isl_multi_aff_get_domain_space(maff);
5547 *ls = isl_local_space_from_space(space);
5548 if (!*ls)
5549 return isl_multi_aff_free(maff);
5551 return maff;
5554 maff = isl_multi_aff_cow(maff);
5555 maff = isl_multi_aff_align_divs(maff);
5556 if (!maff)
5557 return NULL;
5559 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5560 space = isl_multi_aff_get_space(maff);
5561 space = isl_space_lift(isl_space_domain(space), n_div);
5562 space = isl_space_extend_domain_with_range(space,
5563 isl_multi_aff_get_space(maff));
5564 if (!space)
5565 return isl_multi_aff_free(maff);
5566 isl_space_free(maff->space);
5567 maff->space = space;
5569 if (ls) {
5570 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5571 if (!*ls)
5572 return isl_multi_aff_free(maff);
5575 for (i = 0; i < maff->n; ++i) {
5576 maff->p[i] = isl_aff_lift(maff->p[i]);
5577 if (!maff->p[i])
5578 goto error;
5581 return maff;
5582 error:
5583 if (ls)
5584 isl_local_space_free(*ls);
5585 return isl_multi_aff_free(maff);
5589 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5591 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5592 __isl_keep isl_pw_multi_aff *pma, int pos)
5594 int i;
5595 int n_out;
5596 isl_space *space;
5597 isl_pw_aff *pa;
5599 if (!pma)
5600 return NULL;
5602 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5603 if (pos < 0 || pos >= n_out)
5604 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5605 "index out of bounds", return NULL);
5607 space = isl_pw_multi_aff_get_space(pma);
5608 space = isl_space_drop_dims(space, isl_dim_out,
5609 pos + 1, n_out - pos - 1);
5610 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5612 pa = isl_pw_aff_alloc_size(space, pma->n);
5613 for (i = 0; i < pma->n; ++i) {
5614 isl_aff *aff;
5615 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5616 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5619 return pa;
5622 /* Return an isl_pw_multi_aff with the given "set" as domain and
5623 * an unnamed zero-dimensional range.
5625 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5626 __isl_take isl_set *set)
5628 isl_multi_aff *ma;
5629 isl_space *space;
5631 space = isl_set_get_space(set);
5632 space = isl_space_from_domain(space);
5633 ma = isl_multi_aff_zero(space);
5634 return isl_pw_multi_aff_alloc(set, ma);
5637 /* Add an isl_pw_multi_aff with the given "set" as domain and
5638 * an unnamed zero-dimensional range to *user.
5640 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5641 void *user)
5643 isl_union_pw_multi_aff **upma = user;
5644 isl_pw_multi_aff *pma;
5646 pma = isl_pw_multi_aff_from_domain(set);
5647 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5649 return isl_stat_ok;
5652 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5653 * an unnamed zero-dimensional range.
5655 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5656 __isl_take isl_union_set *uset)
5658 isl_space *space;
5659 isl_union_pw_multi_aff *upma;
5661 if (!uset)
5662 return NULL;
5664 space = isl_union_set_get_space(uset);
5665 upma = isl_union_pw_multi_aff_empty(space);
5667 if (isl_union_set_foreach_set(uset,
5668 &add_pw_multi_aff_from_domain, &upma) < 0)
5669 goto error;
5671 isl_union_set_free(uset);
5672 return upma;
5673 error:
5674 isl_union_set_free(uset);
5675 isl_union_pw_multi_aff_free(upma);
5676 return NULL;
5679 /* Convert "pma" to an isl_map and add it to *umap.
5681 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5682 void *user)
5684 isl_union_map **umap = user;
5685 isl_map *map;
5687 map = isl_map_from_pw_multi_aff(pma);
5688 *umap = isl_union_map_add_map(*umap, map);
5690 return isl_stat_ok;
5693 /* Construct a union map mapping the domain of the union
5694 * piecewise multi-affine expression to its range, with each dimension
5695 * in the range equated to the corresponding affine expression on its cell.
5697 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5698 __isl_take isl_union_pw_multi_aff *upma)
5700 isl_space *space;
5701 isl_union_map *umap;
5703 if (!upma)
5704 return NULL;
5706 space = isl_union_pw_multi_aff_get_space(upma);
5707 umap = isl_union_map_empty(space);
5709 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5710 &map_from_pw_multi_aff, &umap) < 0)
5711 goto error;
5713 isl_union_pw_multi_aff_free(upma);
5714 return umap;
5715 error:
5716 isl_union_pw_multi_aff_free(upma);
5717 isl_union_map_free(umap);
5718 return NULL;
5721 /* Local data for bin_entry and the callback "fn".
5723 struct isl_union_pw_multi_aff_bin_data {
5724 isl_union_pw_multi_aff *upma2;
5725 isl_union_pw_multi_aff *res;
5726 isl_pw_multi_aff *pma;
5727 isl_stat (*fn)(void **entry, void *user);
5730 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5731 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5733 static isl_stat bin_entry(void **entry, void *user)
5735 struct isl_union_pw_multi_aff_bin_data *data = user;
5736 isl_pw_multi_aff *pma = *entry;
5738 data->pma = pma;
5739 if (isl_hash_table_foreach(data->upma2->space->ctx, &data->upma2->table,
5740 data->fn, data) < 0)
5741 return isl_stat_error;
5743 return isl_stat_ok;
5746 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5747 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5748 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5749 * as *entry. The callback should adjust data->res if desired.
5751 static __isl_give isl_union_pw_multi_aff *bin_op(
5752 __isl_take isl_union_pw_multi_aff *upma1,
5753 __isl_take isl_union_pw_multi_aff *upma2,
5754 isl_stat (*fn)(void **entry, void *user))
5756 isl_space *space;
5757 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5759 space = isl_union_pw_multi_aff_get_space(upma2);
5760 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5761 space = isl_union_pw_multi_aff_get_space(upma1);
5762 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5764 if (!upma1 || !upma2)
5765 goto error;
5767 data.upma2 = upma2;
5768 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->space),
5769 upma1->table.n);
5770 if (isl_hash_table_foreach(upma1->space->ctx, &upma1->table,
5771 &bin_entry, &data) < 0)
5772 goto error;
5774 isl_union_pw_multi_aff_free(upma1);
5775 isl_union_pw_multi_aff_free(upma2);
5776 return data.res;
5777 error:
5778 isl_union_pw_multi_aff_free(upma1);
5779 isl_union_pw_multi_aff_free(upma2);
5780 isl_union_pw_multi_aff_free(data.res);
5781 return NULL;
5784 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5785 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5787 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5788 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5790 isl_space *space;
5792 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5793 isl_pw_multi_aff_get_space(pma2));
5794 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5795 &isl_multi_aff_range_product);
5798 /* Given two isl_pw_multi_affs A -> B and C -> D,
5799 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5801 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5802 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5804 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5805 &pw_multi_aff_range_product);
5808 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5809 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5811 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5812 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5814 isl_space *space;
5816 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5817 isl_pw_multi_aff_get_space(pma2));
5818 space = isl_space_flatten_range(space);
5819 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5820 &isl_multi_aff_flat_range_product);
5823 /* Given two isl_pw_multi_affs A -> B and C -> D,
5824 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5826 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5827 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5829 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5830 &pw_multi_aff_flat_range_product);
5833 /* If data->pma and *entry have the same domain space, then compute
5834 * their flat range product and the result to data->res.
5836 static isl_stat flat_range_product_entry(void **entry, void *user)
5838 struct isl_union_pw_multi_aff_bin_data *data = user;
5839 isl_pw_multi_aff *pma2 = *entry;
5841 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5842 pma2->dim, isl_dim_in))
5843 return isl_stat_ok;
5845 pma2 = isl_pw_multi_aff_flat_range_product(
5846 isl_pw_multi_aff_copy(data->pma),
5847 isl_pw_multi_aff_copy(pma2));
5849 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5851 return isl_stat_ok;
5854 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5855 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5857 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5858 __isl_take isl_union_pw_multi_aff *upma1,
5859 __isl_take isl_union_pw_multi_aff *upma2)
5861 return bin_op(upma1, upma2, &flat_range_product_entry);
5864 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5865 * The parameters are assumed to have been aligned.
5867 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5868 * except that it works on two different isl_pw_* types.
5870 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5871 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5872 __isl_take isl_pw_aff *pa)
5874 int i, j, n;
5875 isl_pw_multi_aff *res = NULL;
5877 if (!pma || !pa)
5878 goto error;
5880 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5881 pa->dim, isl_dim_in))
5882 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5883 "domains don't match", goto error);
5884 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5885 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5886 "index out of bounds", goto error);
5888 n = pma->n * pa->n;
5889 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5891 for (i = 0; i < pma->n; ++i) {
5892 for (j = 0; j < pa->n; ++j) {
5893 isl_set *common;
5894 isl_multi_aff *res_ij;
5895 int empty;
5897 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5898 isl_set_copy(pa->p[j].set));
5899 empty = isl_set_plain_is_empty(common);
5900 if (empty < 0 || empty) {
5901 isl_set_free(common);
5902 if (empty < 0)
5903 goto error;
5904 continue;
5907 res_ij = isl_multi_aff_set_aff(
5908 isl_multi_aff_copy(pma->p[i].maff), pos,
5909 isl_aff_copy(pa->p[j].aff));
5910 res_ij = isl_multi_aff_gist(res_ij,
5911 isl_set_copy(common));
5913 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5917 isl_pw_multi_aff_free(pma);
5918 isl_pw_aff_free(pa);
5919 return res;
5920 error:
5921 isl_pw_multi_aff_free(pma);
5922 isl_pw_aff_free(pa);
5923 return isl_pw_multi_aff_free(res);
5926 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5928 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5929 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5930 __isl_take isl_pw_aff *pa)
5932 if (!pma || !pa)
5933 goto error;
5934 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5935 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5936 if (!isl_space_has_named_params(pma->dim) ||
5937 !isl_space_has_named_params(pa->dim))
5938 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5939 "unaligned unnamed parameters", goto error);
5940 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5941 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
5942 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5943 error:
5944 isl_pw_multi_aff_free(pma);
5945 isl_pw_aff_free(pa);
5946 return NULL;
5949 /* Do the parameters of "pa" match those of "space"?
5951 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
5952 __isl_keep isl_space *space)
5954 isl_space *pa_space;
5955 int match;
5957 if (!pa || !space)
5958 return -1;
5960 pa_space = isl_pw_aff_get_space(pa);
5962 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5964 isl_space_free(pa_space);
5965 return match;
5968 /* Check that the domain space of "pa" matches "space".
5970 * Return 0 on success and -1 on error.
5972 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
5973 __isl_keep isl_space *space)
5975 isl_space *pa_space;
5976 int match;
5978 if (!pa || !space)
5979 return -1;
5981 pa_space = isl_pw_aff_get_space(pa);
5983 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
5984 if (match < 0)
5985 goto error;
5986 if (!match)
5987 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5988 "parameters don't match", goto error);
5989 match = isl_space_tuple_is_equal(space, isl_dim_in,
5990 pa_space, isl_dim_in);
5991 if (match < 0)
5992 goto error;
5993 if (!match)
5994 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
5995 "domains don't match", goto error);
5996 isl_space_free(pa_space);
5997 return 0;
5998 error:
5999 isl_space_free(pa_space);
6000 return -1;
6003 #undef BASE
6004 #define BASE pw_aff
6005 #undef DOMBASE
6006 #define DOMBASE set
6008 #include <isl_multi_templ.c>
6009 #include <isl_multi_apply_set.c>
6010 #include <isl_multi_gist.c>
6011 #include <isl_multi_intersect.c>
6013 /* Scale the elements of "pma" by the corresponding elements of "mv".
6015 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6016 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6018 int i;
6020 pma = isl_pw_multi_aff_cow(pma);
6021 if (!pma || !mv)
6022 goto error;
6023 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6024 mv->space, isl_dim_set))
6025 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6026 "spaces don't match", goto error);
6027 if (!isl_space_match(pma->dim, isl_dim_param,
6028 mv->space, isl_dim_param)) {
6029 pma = isl_pw_multi_aff_align_params(pma,
6030 isl_multi_val_get_space(mv));
6031 mv = isl_multi_val_align_params(mv,
6032 isl_pw_multi_aff_get_space(pma));
6033 if (!pma || !mv)
6034 goto error;
6037 for (i = 0; i < pma->n; ++i) {
6038 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6039 isl_multi_val_copy(mv));
6040 if (!pma->p[i].maff)
6041 goto error;
6044 isl_multi_val_free(mv);
6045 return pma;
6046 error:
6047 isl_multi_val_free(mv);
6048 isl_pw_multi_aff_free(pma);
6049 return NULL;
6052 /* Internal data structure for isl_union_pw_multi_aff_scale_multi_val.
6053 * mv contains the mv argument.
6054 * res collects the results.
6056 struct isl_union_pw_multi_aff_scale_multi_val_data {
6057 isl_multi_val *mv;
6058 isl_union_pw_multi_aff *res;
6061 /* This function is called for each entry of an isl_union_pw_multi_aff.
6062 * If the space of the entry matches that of data->mv,
6063 * then apply isl_pw_multi_aff_scale_multi_val and add the result
6064 * to data->res.
6066 static isl_stat union_pw_multi_aff_scale_multi_val_entry(void **entry,
6067 void *user)
6069 struct isl_union_pw_multi_aff_scale_multi_val_data *data = user;
6070 isl_pw_multi_aff *pma = *entry;
6072 if (!pma)
6073 return isl_stat_error;
6074 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6075 data->mv->space, isl_dim_set))
6076 return isl_stat_ok;
6078 pma = isl_pw_multi_aff_copy(pma);
6079 pma = isl_pw_multi_aff_scale_multi_val(pma,
6080 isl_multi_val_copy(data->mv));
6081 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
6082 if (!data->res)
6083 return isl_stat_error;
6085 return isl_stat_ok;
6088 /* Scale the elements of "upma" by the corresponding elements of "mv",
6089 * for those entries that match the space of "mv".
6091 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6092 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6094 struct isl_union_pw_multi_aff_scale_multi_val_data data;
6096 upma = isl_union_pw_multi_aff_align_params(upma,
6097 isl_multi_val_get_space(mv));
6098 mv = isl_multi_val_align_params(mv,
6099 isl_union_pw_multi_aff_get_space(upma));
6100 if (!upma || !mv)
6101 goto error;
6103 data.mv = mv;
6104 data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma->space),
6105 upma->table.n);
6106 if (isl_hash_table_foreach(upma->space->ctx, &upma->table,
6107 &union_pw_multi_aff_scale_multi_val_entry, &data) < 0)
6108 goto error;
6110 isl_multi_val_free(mv);
6111 isl_union_pw_multi_aff_free(upma);
6112 return data.res;
6113 error:
6114 isl_multi_val_free(mv);
6115 isl_union_pw_multi_aff_free(upma);
6116 return NULL;
6119 /* Construct and return a piecewise multi affine expression
6120 * in the given space with value zero in each of the output dimensions and
6121 * a universe domain.
6123 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6125 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6128 /* Construct and return a piecewise multi affine expression
6129 * that is equal to the given piecewise affine expression.
6131 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6132 __isl_take isl_pw_aff *pa)
6134 int i;
6135 isl_space *space;
6136 isl_pw_multi_aff *pma;
6138 if (!pa)
6139 return NULL;
6141 space = isl_pw_aff_get_space(pa);
6142 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6144 for (i = 0; i < pa->n; ++i) {
6145 isl_set *set;
6146 isl_multi_aff *ma;
6148 set = isl_set_copy(pa->p[i].set);
6149 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6150 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6153 isl_pw_aff_free(pa);
6154 return pma;
6157 /* Construct a set or map mapping the shared (parameter) domain
6158 * of the piecewise affine expressions to the range of "mpa"
6159 * with each dimension in the range equated to the
6160 * corresponding piecewise affine expression.
6162 static __isl_give isl_map *map_from_multi_pw_aff(
6163 __isl_take isl_multi_pw_aff *mpa)
6165 int i;
6166 isl_space *space;
6167 isl_map *map;
6169 if (!mpa)
6170 return NULL;
6172 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6173 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6174 "invalid space", goto error);
6176 space = isl_multi_pw_aff_get_domain_space(mpa);
6177 map = isl_map_universe(isl_space_from_domain(space));
6179 for (i = 0; i < mpa->n; ++i) {
6180 isl_pw_aff *pa;
6181 isl_map *map_i;
6183 pa = isl_pw_aff_copy(mpa->p[i]);
6184 map_i = map_from_pw_aff(pa);
6186 map = isl_map_flat_range_product(map, map_i);
6189 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6191 isl_multi_pw_aff_free(mpa);
6192 return map;
6193 error:
6194 isl_multi_pw_aff_free(mpa);
6195 return NULL;
6198 /* Construct a map mapping the shared domain
6199 * of the piecewise affine expressions to the range of "mpa"
6200 * with each dimension in the range equated to the
6201 * corresponding piecewise affine expression.
6203 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6205 if (!mpa)
6206 return NULL;
6207 if (isl_space_is_set(mpa->space))
6208 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6209 "space of input is not a map", goto error);
6211 return map_from_multi_pw_aff(mpa);
6212 error:
6213 isl_multi_pw_aff_free(mpa);
6214 return NULL;
6217 /* Construct a set mapping the shared parameter domain
6218 * of the piecewise affine expressions to the space of "mpa"
6219 * with each dimension in the range equated to the
6220 * corresponding piecewise affine expression.
6222 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6224 if (!mpa)
6225 return NULL;
6226 if (!isl_space_is_set(mpa->space))
6227 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6228 "space of input is not a set", goto error);
6230 return map_from_multi_pw_aff(mpa);
6231 error:
6232 isl_multi_pw_aff_free(mpa);
6233 return NULL;
6236 /* Construct and return a piecewise multi affine expression
6237 * that is equal to the given multi piecewise affine expression
6238 * on the shared domain of the piecewise affine expressions.
6240 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6241 __isl_take isl_multi_pw_aff *mpa)
6243 int i;
6244 isl_space *space;
6245 isl_pw_aff *pa;
6246 isl_pw_multi_aff *pma;
6248 if (!mpa)
6249 return NULL;
6251 space = isl_multi_pw_aff_get_space(mpa);
6253 if (mpa->n == 0) {
6254 isl_multi_pw_aff_free(mpa);
6255 return isl_pw_multi_aff_zero(space);
6258 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6259 pma = isl_pw_multi_aff_from_pw_aff(pa);
6261 for (i = 1; i < mpa->n; ++i) {
6262 isl_pw_multi_aff *pma_i;
6264 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6265 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6266 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6269 pma = isl_pw_multi_aff_reset_space(pma, space);
6271 isl_multi_pw_aff_free(mpa);
6272 return pma;
6275 /* Construct and return a multi piecewise affine expression
6276 * that is equal to the given multi affine expression.
6278 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6279 __isl_take isl_multi_aff *ma)
6281 int i, n;
6282 isl_multi_pw_aff *mpa;
6284 if (!ma)
6285 return NULL;
6287 n = isl_multi_aff_dim(ma, isl_dim_out);
6288 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6290 for (i = 0; i < n; ++i) {
6291 isl_pw_aff *pa;
6293 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6294 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6297 isl_multi_aff_free(ma);
6298 return mpa;
6301 /* Construct and return a multi piecewise affine expression
6302 * that is equal to the given piecewise multi affine expression.
6304 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6305 __isl_take isl_pw_multi_aff *pma)
6307 int i, n;
6308 isl_space *space;
6309 isl_multi_pw_aff *mpa;
6311 if (!pma)
6312 return NULL;
6314 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6315 space = isl_pw_multi_aff_get_space(pma);
6316 mpa = isl_multi_pw_aff_alloc(space);
6318 for (i = 0; i < n; ++i) {
6319 isl_pw_aff *pa;
6321 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6322 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6325 isl_pw_multi_aff_free(pma);
6326 return mpa;
6329 /* Do "pa1" and "pa2" represent the same function?
6331 * We first check if they are obviously equal.
6332 * If not, we convert them to maps and check if those are equal.
6334 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6336 int equal;
6337 isl_map *map1, *map2;
6339 if (!pa1 || !pa2)
6340 return -1;
6342 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6343 if (equal < 0 || equal)
6344 return equal;
6346 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6347 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6348 equal = isl_map_is_equal(map1, map2);
6349 isl_map_free(map1);
6350 isl_map_free(map2);
6352 return equal;
6355 /* Do "mpa1" and "mpa2" represent the same function?
6357 * Note that we cannot convert the entire isl_multi_pw_aff
6358 * to a map because the domains of the piecewise affine expressions
6359 * may not be the same.
6361 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6362 __isl_keep isl_multi_pw_aff *mpa2)
6364 int i;
6365 isl_bool equal;
6367 if (!mpa1 || !mpa2)
6368 return isl_bool_error;
6370 if (!isl_space_match(mpa1->space, isl_dim_param,
6371 mpa2->space, isl_dim_param)) {
6372 if (!isl_space_has_named_params(mpa1->space))
6373 return isl_bool_false;
6374 if (!isl_space_has_named_params(mpa2->space))
6375 return isl_bool_false;
6376 mpa1 = isl_multi_pw_aff_copy(mpa1);
6377 mpa2 = isl_multi_pw_aff_copy(mpa2);
6378 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6379 isl_multi_pw_aff_get_space(mpa2));
6380 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6381 isl_multi_pw_aff_get_space(mpa1));
6382 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6383 isl_multi_pw_aff_free(mpa1);
6384 isl_multi_pw_aff_free(mpa2);
6385 return equal;
6388 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6389 if (equal < 0 || !equal)
6390 return equal;
6392 for (i = 0; i < mpa1->n; ++i) {
6393 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6394 if (equal < 0 || !equal)
6395 return equal;
6398 return isl_bool_true;
6401 /* Coalesce the elements of "mpa".
6403 * Note that such coalescing does not change the meaning of "mpa"
6404 * so there is no need to cow. We do need to be careful not to
6405 * destroy any other copies of "mpa" in case of failure.
6407 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_coalesce(
6408 __isl_take isl_multi_pw_aff *mpa)
6410 int i;
6412 if (!mpa)
6413 return NULL;
6415 for (i = 0; i < mpa->n; ++i) {
6416 isl_pw_aff *pa = isl_pw_aff_copy(mpa->p[i]);
6417 pa = isl_pw_aff_coalesce(pa);
6418 if (!pa)
6419 return isl_multi_pw_aff_free(mpa);
6420 isl_pw_aff_free(mpa->p[i]);
6421 mpa->p[i] = pa;
6424 return mpa;
6427 /* Compute the pullback of "mpa" by the function represented by "ma".
6428 * In other words, plug in "ma" in "mpa".
6430 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6432 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6433 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6435 int i;
6436 isl_space *space = NULL;
6438 mpa = isl_multi_pw_aff_cow(mpa);
6439 if (!mpa || !ma)
6440 goto error;
6442 space = isl_space_join(isl_multi_aff_get_space(ma),
6443 isl_multi_pw_aff_get_space(mpa));
6444 if (!space)
6445 goto error;
6447 for (i = 0; i < mpa->n; ++i) {
6448 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6449 isl_multi_aff_copy(ma));
6450 if (!mpa->p[i])
6451 goto error;
6454 isl_multi_aff_free(ma);
6455 isl_space_free(mpa->space);
6456 mpa->space = space;
6457 return mpa;
6458 error:
6459 isl_space_free(space);
6460 isl_multi_pw_aff_free(mpa);
6461 isl_multi_aff_free(ma);
6462 return NULL;
6465 /* Compute the pullback of "mpa" by the function represented by "ma".
6466 * In other words, plug in "ma" in "mpa".
6468 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6469 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6471 if (!mpa || !ma)
6472 goto error;
6473 if (isl_space_match(mpa->space, isl_dim_param,
6474 ma->space, isl_dim_param))
6475 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6476 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6477 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6478 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6479 error:
6480 isl_multi_pw_aff_free(mpa);
6481 isl_multi_aff_free(ma);
6482 return NULL;
6485 /* Compute the pullback of "mpa" by the function represented by "pma".
6486 * In other words, plug in "pma" in "mpa".
6488 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6490 static __isl_give isl_multi_pw_aff *
6491 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6492 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6494 int i;
6495 isl_space *space = NULL;
6497 mpa = isl_multi_pw_aff_cow(mpa);
6498 if (!mpa || !pma)
6499 goto error;
6501 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6502 isl_multi_pw_aff_get_space(mpa));
6504 for (i = 0; i < mpa->n; ++i) {
6505 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6506 isl_pw_multi_aff_copy(pma));
6507 if (!mpa->p[i])
6508 goto error;
6511 isl_pw_multi_aff_free(pma);
6512 isl_space_free(mpa->space);
6513 mpa->space = space;
6514 return mpa;
6515 error:
6516 isl_space_free(space);
6517 isl_multi_pw_aff_free(mpa);
6518 isl_pw_multi_aff_free(pma);
6519 return NULL;
6522 /* Compute the pullback of "mpa" by the function represented by "pma".
6523 * In other words, plug in "pma" in "mpa".
6525 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6526 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6528 if (!mpa || !pma)
6529 goto error;
6530 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6531 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6532 mpa = isl_multi_pw_aff_align_params(mpa,
6533 isl_pw_multi_aff_get_space(pma));
6534 pma = isl_pw_multi_aff_align_params(pma,
6535 isl_multi_pw_aff_get_space(mpa));
6536 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6537 error:
6538 isl_multi_pw_aff_free(mpa);
6539 isl_pw_multi_aff_free(pma);
6540 return NULL;
6543 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6544 * with the domain of "aff". The domain of the result is the same
6545 * as that of "mpa".
6546 * "mpa" and "aff" are assumed to have been aligned.
6548 * We first extract the parametric constant from "aff", defined
6549 * over the correct domain.
6550 * Then we add the appropriate combinations of the members of "mpa".
6551 * Finally, we add the integer divisions through recursive calls.
6553 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6554 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6556 int i, n_in, n_div;
6557 isl_space *space;
6558 isl_val *v;
6559 isl_pw_aff *pa;
6560 isl_aff *tmp;
6562 n_in = isl_aff_dim(aff, isl_dim_in);
6563 n_div = isl_aff_dim(aff, isl_dim_div);
6565 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6566 tmp = isl_aff_copy(aff);
6567 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6568 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6569 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6570 isl_space_dim(space, isl_dim_set));
6571 tmp = isl_aff_reset_domain_space(tmp, space);
6572 pa = isl_pw_aff_from_aff(tmp);
6574 for (i = 0; i < n_in; ++i) {
6575 isl_pw_aff *pa_i;
6577 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6578 continue;
6579 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6580 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6581 pa_i = isl_pw_aff_scale_val(pa_i, v);
6582 pa = isl_pw_aff_add(pa, pa_i);
6585 for (i = 0; i < n_div; ++i) {
6586 isl_aff *div;
6587 isl_pw_aff *pa_i;
6589 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6590 continue;
6591 div = isl_aff_get_div(aff, i);
6592 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6593 isl_multi_pw_aff_copy(mpa), div);
6594 pa_i = isl_pw_aff_floor(pa_i);
6595 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6596 pa_i = isl_pw_aff_scale_val(pa_i, v);
6597 pa = isl_pw_aff_add(pa, pa_i);
6600 isl_multi_pw_aff_free(mpa);
6601 isl_aff_free(aff);
6603 return pa;
6606 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6607 * with the domain of "aff". The domain of the result is the same
6608 * as that of "mpa".
6610 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6611 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6613 if (!aff || !mpa)
6614 goto error;
6615 if (isl_space_match(aff->ls->dim, isl_dim_param,
6616 mpa->space, isl_dim_param))
6617 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6619 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6620 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6622 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6623 error:
6624 isl_aff_free(aff);
6625 isl_multi_pw_aff_free(mpa);
6626 return NULL;
6629 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6630 * with the domain of "pa". The domain of the result is the same
6631 * as that of "mpa".
6632 * "mpa" and "pa" are assumed to have been aligned.
6634 * We consider each piece in turn. Note that the domains of the
6635 * pieces are assumed to be disjoint and they remain disjoint
6636 * after taking the preimage (over the same function).
6638 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6639 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6641 isl_space *space;
6642 isl_pw_aff *res;
6643 int i;
6645 if (!mpa || !pa)
6646 goto error;
6648 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6649 isl_pw_aff_get_space(pa));
6650 res = isl_pw_aff_empty(space);
6652 for (i = 0; i < pa->n; ++i) {
6653 isl_pw_aff *pa_i;
6654 isl_set *domain;
6656 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6657 isl_multi_pw_aff_copy(mpa),
6658 isl_aff_copy(pa->p[i].aff));
6659 domain = isl_set_copy(pa->p[i].set);
6660 domain = isl_set_preimage_multi_pw_aff(domain,
6661 isl_multi_pw_aff_copy(mpa));
6662 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6663 res = isl_pw_aff_add_disjoint(res, pa_i);
6666 isl_pw_aff_free(pa);
6667 isl_multi_pw_aff_free(mpa);
6668 return res;
6669 error:
6670 isl_pw_aff_free(pa);
6671 isl_multi_pw_aff_free(mpa);
6672 return NULL;
6675 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6676 * with the domain of "pa". The domain of the result is the same
6677 * as that of "mpa".
6679 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6680 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6682 if (!pa || !mpa)
6683 goto error;
6684 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6685 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6687 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6688 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6690 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6691 error:
6692 isl_pw_aff_free(pa);
6693 isl_multi_pw_aff_free(mpa);
6694 return NULL;
6697 /* Compute the pullback of "pa" by the function represented by "mpa".
6698 * In other words, plug in "mpa" in "pa".
6699 * "pa" and "mpa" are assumed to have been aligned.
6701 * The pullback is computed by applying "pa" to "mpa".
6703 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6704 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6706 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6709 /* Compute the pullback of "pa" by the function represented by "mpa".
6710 * In other words, plug in "mpa" in "pa".
6712 * The pullback is computed by applying "pa" to "mpa".
6714 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6715 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6717 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6720 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6721 * In other words, plug in "mpa2" in "mpa1".
6723 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6725 * We pullback each member of "mpa1" in turn.
6727 static __isl_give isl_multi_pw_aff *
6728 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6729 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6731 int i;
6732 isl_space *space = NULL;
6734 mpa1 = isl_multi_pw_aff_cow(mpa1);
6735 if (!mpa1 || !mpa2)
6736 goto error;
6738 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6739 isl_multi_pw_aff_get_space(mpa1));
6741 for (i = 0; i < mpa1->n; ++i) {
6742 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6743 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6744 if (!mpa1->p[i])
6745 goto error;
6748 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6750 isl_multi_pw_aff_free(mpa2);
6751 return mpa1;
6752 error:
6753 isl_space_free(space);
6754 isl_multi_pw_aff_free(mpa1);
6755 isl_multi_pw_aff_free(mpa2);
6756 return NULL;
6759 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6760 * In other words, plug in "mpa2" in "mpa1".
6762 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6763 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6765 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6766 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6769 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6770 * of "mpa1" and "mpa2" live in the same space, construct map space
6771 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6772 * with this map space as extract argument.
6774 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6775 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6776 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6777 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6779 int match;
6780 isl_space *space1, *space2;
6781 isl_map *res;
6783 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6784 isl_multi_pw_aff_get_space(mpa2));
6785 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6786 isl_multi_pw_aff_get_space(mpa1));
6787 if (!mpa1 || !mpa2)
6788 goto error;
6789 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6790 mpa2->space, isl_dim_out);
6791 if (match < 0)
6792 goto error;
6793 if (!match)
6794 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6795 "range spaces don't match", goto error);
6796 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6797 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6798 space1 = isl_space_map_from_domain_and_range(space1, space2);
6800 res = order(mpa1, mpa2, space1);
6801 isl_multi_pw_aff_free(mpa1);
6802 isl_multi_pw_aff_free(mpa2);
6803 return res;
6804 error:
6805 isl_multi_pw_aff_free(mpa1);
6806 isl_multi_pw_aff_free(mpa2);
6807 return NULL;
6810 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6811 * where the function values are equal. "space" is the space of the result.
6812 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6814 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6815 * in the sequences are equal.
6817 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6818 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6819 __isl_take isl_space *space)
6821 int i, n;
6822 isl_map *res;
6824 res = isl_map_universe(space);
6826 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6827 for (i = 0; i < n; ++i) {
6828 isl_pw_aff *pa1, *pa2;
6829 isl_map *map;
6831 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6832 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6833 map = isl_pw_aff_eq_map(pa1, pa2);
6834 res = isl_map_intersect(res, map);
6837 return res;
6840 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6841 * where the function values are equal.
6843 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6844 __isl_take isl_multi_pw_aff *mpa2)
6846 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6847 &isl_multi_pw_aff_eq_map_on_space);
6850 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6851 * where the function values of "mpa1" is lexicographically satisfies "base"
6852 * compared to that of "mpa2". "space" is the space of the result.
6853 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6855 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6856 * if its i-th element satisfies "base" when compared to
6857 * the i-th element of "mpa2" while all previous elements are
6858 * pairwise equal.
6860 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6861 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6862 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6863 __isl_take isl_pw_aff *pa2),
6864 __isl_take isl_space *space)
6866 int i, n;
6867 isl_map *res, *rest;
6869 res = isl_map_empty(isl_space_copy(space));
6870 rest = isl_map_universe(space);
6872 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6873 for (i = 0; i < n; ++i) {
6874 isl_pw_aff *pa1, *pa2;
6875 isl_map *map;
6877 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6878 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6879 map = base(pa1, pa2);
6880 map = isl_map_intersect(map, isl_map_copy(rest));
6881 res = isl_map_union(res, map);
6883 if (i == n - 1)
6884 continue;
6886 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6887 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6888 map = isl_pw_aff_eq_map(pa1, pa2);
6889 rest = isl_map_intersect(rest, map);
6892 isl_map_free(rest);
6893 return res;
6896 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6897 * where the function value of "mpa1" is lexicographically less than that
6898 * of "mpa2". "space" is the space of the result.
6899 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6901 * "mpa1" is less than "mpa2" if its i-th element is smaller
6902 * than the i-th element of "mpa2" while all previous elements are
6903 * pairwise equal.
6905 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6906 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6907 __isl_take isl_space *space)
6909 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6910 &isl_pw_aff_lt_map, space);
6913 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6914 * where the function value of "mpa1" is lexicographically less than that
6915 * of "mpa2".
6917 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6918 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6920 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6921 &isl_multi_pw_aff_lex_lt_map_on_space);
6924 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6925 * where the function value of "mpa1" is lexicographically greater than that
6926 * of "mpa2". "space" is the space of the result.
6927 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6929 * "mpa1" is greater than "mpa2" if its i-th element is greater
6930 * than the i-th element of "mpa2" while all previous elements are
6931 * pairwise equal.
6933 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6934 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6935 __isl_take isl_space *space)
6937 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6938 &isl_pw_aff_gt_map, space);
6941 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6942 * where the function value of "mpa1" is lexicographically greater than that
6943 * of "mpa2".
6945 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
6946 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6948 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6949 &isl_multi_pw_aff_lex_gt_map_on_space);
6952 /* Compare two isl_affs.
6954 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6955 * than "aff2" and 0 if they are equal.
6957 * The order is fairly arbitrary. We do consider expressions that only involve
6958 * earlier dimensions as "smaller".
6960 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6962 int cmp;
6963 int last1, last2;
6965 if (aff1 == aff2)
6966 return 0;
6968 if (!aff1)
6969 return -1;
6970 if (!aff2)
6971 return 1;
6973 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6974 if (cmp != 0)
6975 return cmp;
6977 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
6978 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
6979 if (last1 != last2)
6980 return last1 - last2;
6982 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
6985 /* Compare two isl_pw_affs.
6987 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
6988 * than "pa2" and 0 if they are equal.
6990 * The order is fairly arbitrary. We do consider expressions that only involve
6991 * earlier dimensions as "smaller".
6993 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
6994 __isl_keep isl_pw_aff *pa2)
6996 int i;
6997 int cmp;
6999 if (pa1 == pa2)
7000 return 0;
7002 if (!pa1)
7003 return -1;
7004 if (!pa2)
7005 return 1;
7007 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7008 if (cmp != 0)
7009 return cmp;
7011 if (pa1->n != pa2->n)
7012 return pa1->n - pa2->n;
7014 for (i = 0; i < pa1->n; ++i) {
7015 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7016 if (cmp != 0)
7017 return cmp;
7018 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7019 if (cmp != 0)
7020 return cmp;
7023 return 0;
7026 /* Return a piecewise affine expression that is equal to "v" on "domain".
7028 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7029 __isl_take isl_val *v)
7031 isl_space *space;
7032 isl_local_space *ls;
7033 isl_aff *aff;
7035 space = isl_set_get_space(domain);
7036 ls = isl_local_space_from_space(space);
7037 aff = isl_aff_val_on_domain(ls, v);
7039 return isl_pw_aff_alloc(domain, aff);
7042 /* Return a multi affine expression that is equal to "mv" on domain
7043 * space "space".
7045 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7046 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7048 int i, n;
7049 isl_space *space2;
7050 isl_local_space *ls;
7051 isl_multi_aff *ma;
7053 if (!space || !mv)
7054 goto error;
7056 n = isl_multi_val_dim(mv, isl_dim_set);
7057 space2 = isl_multi_val_get_space(mv);
7058 space2 = isl_space_align_params(space2, isl_space_copy(space));
7059 space = isl_space_align_params(space, isl_space_copy(space2));
7060 space = isl_space_map_from_domain_and_range(space, space2);
7061 ma = isl_multi_aff_alloc(isl_space_copy(space));
7062 ls = isl_local_space_from_space(isl_space_domain(space));
7063 for (i = 0; i < n; ++i) {
7064 isl_val *v;
7065 isl_aff *aff;
7067 v = isl_multi_val_get_val(mv, i);
7068 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7069 ma = isl_multi_aff_set_aff(ma, i, aff);
7071 isl_local_space_free(ls);
7073 isl_multi_val_free(mv);
7074 return ma;
7075 error:
7076 isl_space_free(space);
7077 isl_multi_val_free(mv);
7078 return NULL;
7081 /* Return a piecewise multi-affine expression
7082 * that is equal to "mv" on "domain".
7084 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7085 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7087 isl_space *space;
7088 isl_multi_aff *ma;
7090 space = isl_set_get_space(domain);
7091 ma = isl_multi_aff_multi_val_on_space(space, mv);
7093 return isl_pw_multi_aff_alloc(domain, ma);
7096 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7097 * mv is the value that should be attained on each domain set
7098 * res collects the results
7100 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7101 isl_multi_val *mv;
7102 isl_union_pw_multi_aff *res;
7105 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7106 * and add it to data->res.
7108 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7109 void *user)
7111 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7112 isl_pw_multi_aff *pma;
7113 isl_multi_val *mv;
7115 mv = isl_multi_val_copy(data->mv);
7116 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7117 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7119 return data->res ? isl_stat_ok : isl_stat_error;
7122 /* Return a union piecewise multi-affine expression
7123 * that is equal to "mv" on "domain".
7125 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7126 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7128 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7129 isl_space *space;
7131 space = isl_union_set_get_space(domain);
7132 data.res = isl_union_pw_multi_aff_empty(space);
7133 data.mv = mv;
7134 if (isl_union_set_foreach_set(domain,
7135 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7136 data.res = isl_union_pw_multi_aff_free(data.res);
7137 isl_union_set_free(domain);
7138 isl_multi_val_free(mv);
7139 return data.res;
7142 /* Compute the pullback of data->pma by the function represented by "pma2",
7143 * provided the spaces match, and add the results to data->res.
7145 static isl_stat pullback_entry(void **entry, void *user)
7147 struct isl_union_pw_multi_aff_bin_data *data = user;
7148 isl_pw_multi_aff *pma2 = *entry;
7150 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7151 pma2->dim, isl_dim_out))
7152 return isl_stat_ok;
7154 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7155 isl_pw_multi_aff_copy(data->pma),
7156 isl_pw_multi_aff_copy(pma2));
7158 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7159 if (!data->res)
7160 return isl_stat_error;
7162 return isl_stat_ok;
7165 /* Compute the pullback of "upma1" by the function represented by "upma2".
7167 __isl_give isl_union_pw_multi_aff *
7168 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7169 __isl_take isl_union_pw_multi_aff *upma1,
7170 __isl_take isl_union_pw_multi_aff *upma2)
7172 return bin_op(upma1, upma2, &pullback_entry);
7175 /* Check that the domain space of "upa" matches "space".
7177 * Return 0 on success and -1 on error.
7179 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7180 * can in principle never fail since the space "space" is that
7181 * of the isl_multi_union_pw_aff and is a set space such that
7182 * there is no domain space to match.
7184 * We check the parameters and double-check that "space" is
7185 * indeed that of a set.
7187 static int isl_union_pw_aff_check_match_domain_space(
7188 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7190 isl_space *upa_space;
7191 int match;
7193 if (!upa || !space)
7194 return -1;
7196 match = isl_space_is_set(space);
7197 if (match < 0)
7198 return -1;
7199 if (!match)
7200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7201 "expecting set space", return -1);
7203 upa_space = isl_union_pw_aff_get_space(upa);
7204 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7205 if (match < 0)
7206 goto error;
7207 if (!match)
7208 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7209 "parameters don't match", goto error);
7211 isl_space_free(upa_space);
7212 return 0;
7213 error:
7214 isl_space_free(upa_space);
7215 return -1;
7218 /* Do the parameters of "upa" match those of "space"?
7220 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7221 __isl_keep isl_space *space)
7223 isl_space *upa_space;
7224 int match;
7226 if (!upa || !space)
7227 return -1;
7229 upa_space = isl_union_pw_aff_get_space(upa);
7231 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7233 isl_space_free(upa_space);
7234 return match;
7237 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7238 * space represents the new parameters.
7239 * res collects the results.
7241 struct isl_union_pw_aff_reset_params_data {
7242 isl_space *space;
7243 isl_union_pw_aff *res;
7246 /* Replace the parameters of "pa" by data->space and
7247 * add the result to data->res.
7249 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7251 struct isl_union_pw_aff_reset_params_data *data = user;
7252 isl_space *space;
7254 space = isl_pw_aff_get_space(pa);
7255 space = isl_space_replace(space, isl_dim_param, data->space);
7256 pa = isl_pw_aff_reset_space(pa, space);
7257 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7259 return data->res ? isl_stat_ok : isl_stat_error;
7262 /* Replace the domain space of "upa" by "space".
7263 * Since a union expression does not have a (single) domain space,
7264 * "space" is necessarily a parameter space.
7266 * Since the order and the names of the parameters determine
7267 * the hash value, we need to create a new hash table.
7269 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7270 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7272 struct isl_union_pw_aff_reset_params_data data = { space };
7273 int match;
7275 match = isl_union_pw_aff_matching_params(upa, space);
7276 if (match < 0)
7277 upa = isl_union_pw_aff_free(upa);
7278 else if (match) {
7279 isl_space_free(space);
7280 return upa;
7283 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7284 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7285 data.res = isl_union_pw_aff_free(data.res);
7287 isl_union_pw_aff_free(upa);
7288 isl_space_free(space);
7289 return data.res;
7292 /* Replace the entry of isl_union_pw_aff to which "entry" points
7293 * by its floor.
7295 static isl_stat floor_entry(void **entry, void *user)
7297 isl_pw_aff **pa = (isl_pw_aff **) entry;
7299 *pa = isl_pw_aff_floor(*pa);
7300 if (!*pa)
7301 return isl_stat_error;
7303 return isl_stat_ok;
7306 /* Given f, return floor(f).
7308 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7309 __isl_take isl_union_pw_aff *upa)
7311 isl_ctx *ctx;
7313 upa = isl_union_pw_aff_cow(upa);
7314 if (!upa)
7315 return NULL;
7317 ctx = isl_union_pw_aff_get_ctx(upa);
7318 if (isl_hash_table_foreach(ctx, &upa->table, &floor_entry, NULL) < 0)
7319 upa = isl_union_pw_aff_free(upa);
7321 return upa;
7324 /* Compute
7326 * upa mod m = upa - m * floor(upa/m)
7328 * with m an integer value.
7330 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7331 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7333 isl_union_pw_aff *res;
7335 if (!upa || !m)
7336 goto error;
7338 if (!isl_val_is_int(m))
7339 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7340 "expecting integer modulo", goto error);
7341 if (!isl_val_is_pos(m))
7342 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7343 "expecting positive modulo", goto error);
7345 res = isl_union_pw_aff_copy(upa);
7346 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7347 upa = isl_union_pw_aff_floor(upa);
7348 upa = isl_union_pw_aff_scale_val(upa, m);
7349 res = isl_union_pw_aff_sub(res, upa);
7351 return res;
7352 error:
7353 isl_val_free(m);
7354 isl_union_pw_aff_free(upa);
7355 return NULL;
7358 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7359 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7360 * needs to attain.
7361 * "res" collects the results.
7363 struct isl_union_pw_aff_aff_on_domain_data {
7364 isl_aff *aff;
7365 isl_union_pw_aff *res;
7368 /* Construct a piecewise affine expression that is equal to data->aff
7369 * on "domain" and add the result to data->res.
7371 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7373 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7374 isl_pw_aff *pa;
7375 isl_aff *aff;
7376 int dim;
7378 aff = isl_aff_copy(data->aff);
7379 dim = isl_set_dim(domain, isl_dim_set);
7380 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7381 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7382 pa = isl_pw_aff_alloc(domain, aff);
7383 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7385 return data->res ? isl_stat_ok : isl_stat_error;
7388 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7389 * pos is the output position that needs to be extracted.
7390 * res collects the results.
7392 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7393 int pos;
7394 isl_union_pw_aff *res;
7397 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7398 * (assuming it has such a dimension) and add it to data->res.
7400 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7402 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7403 int n_out;
7404 isl_pw_aff *pa;
7406 if (!pma)
7407 return isl_stat_error;
7409 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7410 if (data->pos >= n_out) {
7411 isl_pw_multi_aff_free(pma);
7412 return isl_stat_ok;
7415 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7416 isl_pw_multi_aff_free(pma);
7418 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7420 return data->res ? isl_stat_ok : isl_stat_error;
7423 /* Extract an isl_union_pw_aff corresponding to
7424 * output dimension "pos" of "upma".
7426 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7427 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7429 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7430 isl_space *space;
7432 if (!upma)
7433 return NULL;
7435 if (pos < 0)
7436 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7437 "cannot extract at negative position", return NULL);
7439 space = isl_union_pw_multi_aff_get_space(upma);
7440 data.res = isl_union_pw_aff_empty(space);
7441 data.pos = pos;
7442 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7443 &get_union_pw_aff, &data) < 0)
7444 data.res = isl_union_pw_aff_free(data.res);
7446 return data.res;
7449 /* Return a union piecewise affine expression
7450 * that is equal to "aff" on "domain".
7452 * Construct an isl_pw_aff on each of the sets in "domain" and
7453 * collect the results.
7455 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7456 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7458 struct isl_union_pw_aff_aff_on_domain_data data;
7459 isl_space *space;
7461 if (!domain || !aff)
7462 goto error;
7463 if (!isl_local_space_is_params(aff->ls))
7464 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7465 "expecting parametric expression", goto error);
7467 space = isl_union_set_get_space(domain);
7468 data.res = isl_union_pw_aff_empty(space);
7469 data.aff = aff;
7470 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7471 data.res = isl_union_pw_aff_free(data.res);
7472 isl_union_set_free(domain);
7473 isl_aff_free(aff);
7474 return data.res;
7475 error:
7476 isl_union_set_free(domain);
7477 isl_aff_free(aff);
7478 return NULL;
7481 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7482 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7483 * "res" collects the results.
7485 struct isl_union_pw_aff_val_on_domain_data {
7486 isl_val *v;
7487 isl_union_pw_aff *res;
7490 /* Construct a piecewise affine expression that is equal to data->v
7491 * on "domain" and add the result to data->res.
7493 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7495 struct isl_union_pw_aff_val_on_domain_data *data = user;
7496 isl_pw_aff *pa;
7497 isl_val *v;
7499 v = isl_val_copy(data->v);
7500 pa = isl_pw_aff_val_on_domain(domain, v);
7501 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7503 return data->res ? isl_stat_ok : isl_stat_error;
7506 /* Return a union piecewise affine expression
7507 * that is equal to "v" on "domain".
7509 * Construct an isl_pw_aff on each of the sets in "domain" and
7510 * collect the results.
7512 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7513 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7515 struct isl_union_pw_aff_val_on_domain_data data;
7516 isl_space *space;
7518 space = isl_union_set_get_space(domain);
7519 data.res = isl_union_pw_aff_empty(space);
7520 data.v = v;
7521 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7522 data.res = isl_union_pw_aff_free(data.res);
7523 isl_union_set_free(domain);
7524 isl_val_free(v);
7525 return data.res;
7528 /* Construct a piecewise multi affine expression
7529 * that is equal to "pa" and add it to upma.
7531 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7532 void *user)
7534 isl_union_pw_multi_aff **upma = user;
7535 isl_pw_multi_aff *pma;
7537 pma = isl_pw_multi_aff_from_pw_aff(pa);
7538 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7540 return *upma ? isl_stat_ok : isl_stat_error;
7543 /* Construct and return a union piecewise multi affine expression
7544 * that is equal to the given union piecewise affine expression.
7546 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7547 __isl_take isl_union_pw_aff *upa)
7549 isl_space *space;
7550 isl_union_pw_multi_aff *upma;
7552 if (!upa)
7553 return NULL;
7555 space = isl_union_pw_aff_get_space(upa);
7556 upma = isl_union_pw_multi_aff_empty(space);
7558 if (isl_union_pw_aff_foreach_pw_aff(upa,
7559 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7560 upma = isl_union_pw_multi_aff_free(upma);
7562 isl_union_pw_aff_free(upa);
7563 return upma;
7566 /* Compute the set of elements in the domain of "pa" where it is zero and
7567 * add this set to "uset".
7569 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7571 isl_union_set **uset = (isl_union_set **)user;
7573 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7575 return *uset ? isl_stat_ok : isl_stat_error;
7578 /* Return a union set containing those elements in the domain
7579 * of "upa" where it is zero.
7581 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7582 __isl_take isl_union_pw_aff *upa)
7584 isl_union_set *zero;
7586 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7587 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7588 zero = isl_union_set_free(zero);
7590 isl_union_pw_aff_free(upa);
7591 return zero;
7594 /* Convert "pa" to an isl_map and add it to *umap.
7596 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7598 isl_union_map **umap = user;
7599 isl_map *map;
7601 map = isl_map_from_pw_aff(pa);
7602 *umap = isl_union_map_add_map(*umap, map);
7604 return *umap ? isl_stat_ok : isl_stat_error;
7607 /* Construct a union map mapping the domain of the union
7608 * piecewise affine expression to its range, with the single output dimension
7609 * equated to the corresponding affine expressions on their cells.
7611 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7612 __isl_take isl_union_pw_aff *upa)
7614 isl_space *space;
7615 isl_union_map *umap;
7617 if (!upa)
7618 return NULL;
7620 space = isl_union_pw_aff_get_space(upa);
7621 umap = isl_union_map_empty(space);
7623 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7624 &umap) < 0)
7625 umap = isl_union_map_free(umap);
7627 isl_union_pw_aff_free(upa);
7628 return umap;
7631 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7632 * upma is the function that is plugged in.
7633 * pa is the current part of the function in which upma is plugged in.
7634 * res collects the results.
7636 struct isl_union_pw_aff_pullback_upma_data {
7637 isl_union_pw_multi_aff *upma;
7638 isl_pw_aff *pa;
7639 isl_union_pw_aff *res;
7642 /* Check if "pma" can be plugged into data->pa.
7643 * If so, perform the pullback and add the result to data->res.
7645 static isl_stat pa_pb_pma(void **entry, void *user)
7647 struct isl_union_pw_aff_pullback_upma_data *data = user;
7648 isl_pw_multi_aff *pma = *entry;
7649 isl_pw_aff *pa;
7651 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7652 pma->dim, isl_dim_out))
7653 return isl_stat_ok;
7655 pma = isl_pw_multi_aff_copy(pma);
7656 pa = isl_pw_aff_copy(data->pa);
7657 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7659 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7661 return data->res ? isl_stat_ok : isl_stat_error;
7664 /* Check if any of the elements of data->upma can be plugged into pa,
7665 * add if so add the result to data->res.
7667 static isl_stat upa_pb_upma(void **entry, void *user)
7669 struct isl_union_pw_aff_pullback_upma_data *data = user;
7670 isl_ctx *ctx;
7671 isl_pw_aff *pa = *entry;
7673 data->pa = pa;
7674 ctx = isl_union_pw_multi_aff_get_ctx(data->upma);
7675 if (isl_hash_table_foreach(ctx, &data->upma->table,
7676 &pa_pb_pma, data) < 0)
7677 return isl_stat_error;
7679 return isl_stat_ok;
7682 /* Compute the pullback of "upa" by the function represented by "upma".
7683 * In other words, plug in "upma" in "upa". The result contains
7684 * expressions defined over the domain space of "upma".
7686 * Run over all pairs of elements in "upa" and "upma", perform
7687 * the pullback when appropriate and collect the results.
7688 * If the hash value were based on the domain space rather than
7689 * the function space, then we could run through all elements
7690 * of "upma" and directly pick out the corresponding element of "upa".
7692 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7693 __isl_take isl_union_pw_aff *upa,
7694 __isl_take isl_union_pw_multi_aff *upma)
7696 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7697 isl_ctx *ctx;
7698 isl_space *space;
7700 space = isl_union_pw_multi_aff_get_space(upma);
7701 upa = isl_union_pw_aff_align_params(upa, space);
7702 space = isl_union_pw_aff_get_space(upa);
7703 upma = isl_union_pw_multi_aff_align_params(upma, space);
7705 if (!upa || !upma)
7706 goto error;
7708 ctx = isl_union_pw_aff_get_ctx(upa);
7709 data.upma = upma;
7710 space = isl_union_pw_aff_get_space(upa);
7711 data.res = isl_union_pw_aff_alloc(space, upa->table.n);
7712 if (isl_hash_table_foreach(ctx, &upa->table, &upa_pb_upma, &data) < 0)
7713 data.res = isl_union_pw_aff_free(data.res);
7715 isl_union_pw_aff_free(upa);
7716 isl_union_pw_multi_aff_free(upma);
7717 return data.res;
7718 error:
7719 isl_union_pw_aff_free(upa);
7720 isl_union_pw_multi_aff_free(upma);
7721 return NULL;
7724 #undef BASE
7725 #define BASE union_pw_aff
7726 #undef DOMBASE
7727 #define DOMBASE union_set
7729 #define NO_MOVE_DIMS
7730 #define NO_DIMS
7731 #define NO_DOMAIN
7732 #define NO_PRODUCT
7733 #define NO_SPLICE
7734 #define NO_ZERO
7735 #define NO_IDENTITY
7736 #define NO_GIST
7738 #include <isl_multi_templ.c>
7739 #include <isl_multi_apply_set.c>
7740 #include <isl_multi_apply_union_set.c>
7741 #include <isl_multi_floor.c>
7742 #include <isl_multi_gist.c>
7743 #include <isl_multi_intersect.c>
7745 /* Construct a multiple union piecewise affine expression
7746 * in the given space with value zero in each of the output dimensions.
7748 * Since there is no canonical zero value for
7749 * a union piecewise affine expression, we can only construct
7750 * zero-dimensional "zero" value.
7752 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7753 __isl_take isl_space *space)
7755 if (!space)
7756 return NULL;
7758 if (!isl_space_is_set(space))
7759 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7760 "expecting set space", goto error);
7761 if (isl_space_dim(space , isl_dim_out) != 0)
7762 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7763 "expecting 0D space", goto error);
7765 return isl_multi_union_pw_aff_alloc(space);
7766 error:
7767 isl_space_free(space);
7768 return NULL;
7771 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7772 * with the actual sum on the shared domain and
7773 * the defined expression on the symmetric difference of the domains.
7775 * We simply iterate over the elements in both arguments and
7776 * call isl_union_pw_aff_union_add on each of them.
7778 static __isl_give isl_multi_union_pw_aff *
7779 isl_multi_union_pw_aff_union_add_aligned(
7780 __isl_take isl_multi_union_pw_aff *mupa1,
7781 __isl_take isl_multi_union_pw_aff *mupa2)
7783 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7784 &isl_union_pw_aff_union_add);
7787 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7788 * with the actual sum on the shared domain and
7789 * the defined expression on the symmetric difference of the domains.
7791 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7792 __isl_take isl_multi_union_pw_aff *mupa1,
7793 __isl_take isl_multi_union_pw_aff *mupa2)
7795 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7796 &isl_multi_union_pw_aff_union_add_aligned);
7799 /* Construct and return a multi union piecewise affine expression
7800 * that is equal to the given multi affine expression.
7802 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7803 __isl_take isl_multi_aff *ma)
7805 isl_multi_pw_aff *mpa;
7807 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7808 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7811 /* Construct and return a multi union piecewise affine expression
7812 * that is equal to the given multi piecewise affine expression.
7814 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7815 __isl_take isl_multi_pw_aff *mpa)
7817 int i, n;
7818 isl_space *space;
7819 isl_multi_union_pw_aff *mupa;
7821 if (!mpa)
7822 return NULL;
7824 space = isl_multi_pw_aff_get_space(mpa);
7825 space = isl_space_range(space);
7826 mupa = isl_multi_union_pw_aff_alloc(space);
7828 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7829 for (i = 0; i < n; ++i) {
7830 isl_pw_aff *pa;
7831 isl_union_pw_aff *upa;
7833 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7834 upa = isl_union_pw_aff_from_pw_aff(pa);
7835 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7838 isl_multi_pw_aff_free(mpa);
7840 return mupa;
7843 /* Extract the range space of "pma" and assign it to *space.
7844 * If *space has already been set (through a previous call to this function),
7845 * then check that the range space is the same.
7847 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7849 isl_space **space = user;
7850 isl_space *pma_space;
7851 isl_bool equal;
7853 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7854 isl_pw_multi_aff_free(pma);
7856 if (!pma_space)
7857 return isl_stat_error;
7858 if (!*space) {
7859 *space = pma_space;
7860 return isl_stat_ok;
7863 equal = isl_space_is_equal(pma_space, *space);
7864 isl_space_free(pma_space);
7866 if (equal < 0)
7867 return isl_stat_error;
7868 if (!equal)
7869 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7870 "range spaces not the same", return isl_stat_error);
7871 return isl_stat_ok;
7874 /* Construct and return a multi union piecewise affine expression
7875 * that is equal to the given union piecewise multi affine expression.
7877 * In order to be able to perform the conversion, the input
7878 * needs to be non-empty and may only involve a single range space.
7880 __isl_give isl_multi_union_pw_aff *
7881 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7882 __isl_take isl_union_pw_multi_aff *upma)
7884 isl_space *space = NULL;
7885 isl_multi_union_pw_aff *mupa;
7886 int i, n;
7888 if (!upma)
7889 return NULL;
7890 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7891 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7892 "cannot extract range space from empty input",
7893 goto error);
7894 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7895 &space) < 0)
7896 goto error;
7898 if (!space)
7899 goto error;
7901 n = isl_space_dim(space, isl_dim_set);
7902 mupa = isl_multi_union_pw_aff_alloc(space);
7904 for (i = 0; i < n; ++i) {
7905 isl_union_pw_aff *upa;
7907 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7908 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7911 isl_union_pw_multi_aff_free(upma);
7912 return mupa;
7913 error:
7914 isl_space_free(space);
7915 isl_union_pw_multi_aff_free(upma);
7916 return NULL;
7919 /* Try and create an isl_multi_union_pw_aff that is equivalent
7920 * to the given isl_union_map.
7921 * The isl_union_map is required to be single-valued in each space.
7922 * Moreover, it cannot be empty and all range spaces need to be the same.
7923 * Otherwise, an error is produced.
7925 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7926 __isl_take isl_union_map *umap)
7928 isl_union_pw_multi_aff *upma;
7930 upma = isl_union_pw_multi_aff_from_union_map(umap);
7931 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7934 /* Return a multiple union piecewise affine expression
7935 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7936 * have been aligned.
7938 static __isl_give isl_multi_union_pw_aff *
7939 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7940 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7942 int i, n;
7943 isl_space *space;
7944 isl_multi_union_pw_aff *mupa;
7946 if (!domain || !mv)
7947 goto error;
7949 n = isl_multi_val_dim(mv, isl_dim_set);
7950 space = isl_multi_val_get_space(mv);
7951 mupa = isl_multi_union_pw_aff_alloc(space);
7952 for (i = 0; i < n; ++i) {
7953 isl_val *v;
7954 isl_union_pw_aff *upa;
7956 v = isl_multi_val_get_val(mv, i);
7957 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
7959 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7962 isl_union_set_free(domain);
7963 isl_multi_val_free(mv);
7964 return mupa;
7965 error:
7966 isl_union_set_free(domain);
7967 isl_multi_val_free(mv);
7968 return NULL;
7971 /* Return a multiple union piecewise affine expression
7972 * that is equal to "mv" on "domain".
7974 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
7975 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7977 if (!domain || !mv)
7978 goto error;
7979 if (isl_space_match(domain->dim, isl_dim_param,
7980 mv->space, isl_dim_param))
7981 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7982 domain, mv);
7983 domain = isl_union_set_align_params(domain,
7984 isl_multi_val_get_space(mv));
7985 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
7986 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
7987 error:
7988 isl_union_set_free(domain);
7989 isl_multi_val_free(mv);
7990 return NULL;
7993 /* Return a multiple union piecewise affine expression
7994 * that is equal to "ma" on "domain", assuming "domain" and "ma"
7995 * have been aligned.
7997 static __isl_give isl_multi_union_pw_aff *
7998 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
7999 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8001 int i, n;
8002 isl_space *space;
8003 isl_multi_union_pw_aff *mupa;
8005 if (!domain || !ma)
8006 goto error;
8008 n = isl_multi_aff_dim(ma, isl_dim_set);
8009 space = isl_multi_aff_get_space(ma);
8010 mupa = isl_multi_union_pw_aff_alloc(space);
8011 for (i = 0; i < n; ++i) {
8012 isl_aff *aff;
8013 isl_union_pw_aff *upa;
8015 aff = isl_multi_aff_get_aff(ma, i);
8016 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8017 aff);
8018 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8021 isl_union_set_free(domain);
8022 isl_multi_aff_free(ma);
8023 return mupa;
8024 error:
8025 isl_union_set_free(domain);
8026 isl_multi_aff_free(ma);
8027 return NULL;
8030 /* Return a multiple union piecewise affine expression
8031 * that is equal to "ma" on "domain".
8033 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8034 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8036 if (!domain || !ma)
8037 goto error;
8038 if (isl_space_match(domain->dim, isl_dim_param,
8039 ma->space, isl_dim_param))
8040 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8041 domain, ma);
8042 domain = isl_union_set_align_params(domain,
8043 isl_multi_aff_get_space(ma));
8044 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8045 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8046 error:
8047 isl_union_set_free(domain);
8048 isl_multi_aff_free(ma);
8049 return NULL;
8052 /* Return a union set containing those elements in the domains
8053 * of the elements of "mupa" where they are all zero.
8055 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8056 __isl_take isl_multi_union_pw_aff *mupa)
8058 int i, n;
8059 isl_union_pw_aff *upa;
8060 isl_union_set *zero;
8062 if (!mupa)
8063 return NULL;
8065 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8066 if (n == 0)
8067 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8068 "cannot determine zero set "
8069 "of zero-dimensional function", goto error);
8071 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8072 zero = isl_union_pw_aff_zero_union_set(upa);
8074 for (i = 1; i < n; ++i) {
8075 isl_union_set *zero_i;
8077 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8078 zero_i = isl_union_pw_aff_zero_union_set(upa);
8080 zero = isl_union_set_intersect(zero, zero_i);
8083 isl_multi_union_pw_aff_free(mupa);
8084 return zero;
8085 error:
8086 isl_multi_union_pw_aff_free(mupa);
8087 return NULL;
8090 /* Construct a union map mapping the shared domain
8091 * of the union piecewise affine expressions to the range of "mupa"
8092 * with each dimension in the range equated to the
8093 * corresponding union piecewise affine expression.
8095 * The input cannot be zero-dimensional as there is
8096 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8098 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8099 __isl_take isl_multi_union_pw_aff *mupa)
8101 int i, n;
8102 isl_space *space;
8103 isl_union_map *umap;
8104 isl_union_pw_aff *upa;
8106 if (!mupa)
8107 return NULL;
8109 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8110 if (n == 0)
8111 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8112 "cannot determine domain of zero-dimensional "
8113 "isl_multi_union_pw_aff", goto error);
8115 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8116 umap = isl_union_map_from_union_pw_aff(upa);
8118 for (i = 1; i < n; ++i) {
8119 isl_union_map *umap_i;
8121 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8122 umap_i = isl_union_map_from_union_pw_aff(upa);
8123 umap = isl_union_map_flat_range_product(umap, umap_i);
8126 space = isl_multi_union_pw_aff_get_space(mupa);
8127 umap = isl_union_map_reset_range_space(umap, space);
8129 isl_multi_union_pw_aff_free(mupa);
8130 return umap;
8131 error:
8132 isl_multi_union_pw_aff_free(mupa);
8133 return NULL;
8136 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8137 * "range" is the space from which to set the range space.
8138 * "res" collects the results.
8140 struct isl_union_pw_multi_aff_reset_range_space_data {
8141 isl_space *range;
8142 isl_union_pw_multi_aff *res;
8145 /* Replace the range space of "pma" by the range space of data->range and
8146 * add the result to data->res.
8148 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8150 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8151 isl_space *space;
8153 space = isl_pw_multi_aff_get_space(pma);
8154 space = isl_space_domain(space);
8155 space = isl_space_extend_domain_with_range(space,
8156 isl_space_copy(data->range));
8157 pma = isl_pw_multi_aff_reset_space(pma, space);
8158 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8160 return data->res ? isl_stat_ok : isl_stat_error;
8163 /* Replace the range space of all the piecewise affine expressions in "upma" by
8164 * the range space of "space".
8166 * This assumes that all these expressions have the same output dimension.
8168 * Since the spaces of the expressions change, so do their hash values.
8169 * We therefore need to create a new isl_union_pw_multi_aff.
8170 * Note that the hash value is currently computed based on the entire
8171 * space even though there can only be a single expression with a given
8172 * domain space.
8174 static __isl_give isl_union_pw_multi_aff *
8175 isl_union_pw_multi_aff_reset_range_space(
8176 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8178 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8179 isl_space *space_upma;
8181 space_upma = isl_union_pw_multi_aff_get_space(upma);
8182 data.res = isl_union_pw_multi_aff_empty(space_upma);
8183 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8184 &reset_range_space, &data) < 0)
8185 data.res = isl_union_pw_multi_aff_free(data.res);
8187 isl_space_free(space);
8188 isl_union_pw_multi_aff_free(upma);
8189 return data.res;
8192 /* Construct and return a union piecewise multi affine expression
8193 * that is equal to the given multi union piecewise affine expression.
8195 * In order to be able to perform the conversion, the input
8196 * needs to have a least one output dimension.
8198 __isl_give isl_union_pw_multi_aff *
8199 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8200 __isl_take isl_multi_union_pw_aff *mupa)
8202 int i, n;
8203 isl_space *space;
8204 isl_union_pw_multi_aff *upma;
8205 isl_union_pw_aff *upa;
8207 if (!mupa)
8208 return NULL;
8210 space = isl_multi_union_pw_aff_get_space(mupa);
8212 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8213 if (n == 0)
8214 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8215 "cannot determine domain of zero-dimensional "
8216 "isl_multi_union_pw_aff", goto error);
8218 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8219 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8221 for (i = 1; i < n; ++i) {
8222 isl_union_pw_multi_aff *upma_i;
8224 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8225 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8226 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8229 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8231 isl_multi_union_pw_aff_free(mupa);
8232 return upma;
8233 error:
8234 isl_multi_union_pw_aff_free(mupa);
8235 return NULL;
8238 /* Intersect the range of "mupa" with "range".
8239 * That is, keep only those domain elements that have a function value
8240 * in "range".
8242 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8243 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8245 isl_union_pw_multi_aff *upma;
8246 isl_union_set *domain;
8247 isl_space *space;
8248 int n;
8249 int match;
8251 if (!mupa || !range)
8252 goto error;
8254 space = isl_set_get_space(range);
8255 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8256 space, isl_dim_set);
8257 isl_space_free(space);
8258 if (match < 0)
8259 goto error;
8260 if (!match)
8261 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8262 "space don't match", goto error);
8263 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8264 if (n == 0)
8265 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8266 "cannot intersect range of zero-dimensional "
8267 "isl_multi_union_pw_aff", goto error);
8269 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8270 isl_multi_union_pw_aff_copy(mupa));
8271 domain = isl_union_set_from_set(range);
8272 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8273 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8275 return mupa;
8276 error:
8277 isl_multi_union_pw_aff_free(mupa);
8278 isl_set_free(range);
8279 return NULL;
8282 /* Return the shared domain of the elements of "mupa".
8284 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8285 __isl_take isl_multi_union_pw_aff *mupa)
8287 int i, n;
8288 isl_union_pw_aff *upa;
8289 isl_union_set *dom;
8291 if (!mupa)
8292 return NULL;
8294 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8295 if (n == 0)
8296 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8297 "cannot determine domain", goto error);
8299 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8300 dom = isl_union_pw_aff_domain(upa);
8301 for (i = 1; i < n; ++i) {
8302 isl_union_set *dom_i;
8304 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8305 dom_i = isl_union_pw_aff_domain(upa);
8306 dom = isl_union_set_intersect(dom, dom_i);
8309 isl_multi_union_pw_aff_free(mupa);
8310 return dom;
8311 error:
8312 isl_multi_union_pw_aff_free(mupa);
8313 return NULL;
8316 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8317 * In particular, the spaces have been aligned.
8318 * The result is defined over the shared domain of the elements of "mupa"
8320 * We first extract the parametric constant part of "aff" and
8321 * define that over the shared domain.
8322 * Then we iterate over all input dimensions of "aff" and add the corresponding
8323 * multiples of the elements of "mupa".
8324 * Finally, we consider the integer divisions, calling the function
8325 * recursively to obtain an isl_union_pw_aff corresponding to the
8326 * integer division argument.
8328 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8329 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8331 int i, n_in, n_div;
8332 isl_union_pw_aff *upa;
8333 isl_union_set *uset;
8334 isl_val *v;
8335 isl_aff *cst;
8337 n_in = isl_aff_dim(aff, isl_dim_in);
8338 n_div = isl_aff_dim(aff, isl_dim_div);
8340 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8341 cst = isl_aff_copy(aff);
8342 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8343 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8344 cst = isl_aff_project_domain_on_params(cst);
8345 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8347 for (i = 0; i < n_in; ++i) {
8348 isl_union_pw_aff *upa_i;
8350 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8351 continue;
8352 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8353 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8354 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8355 upa = isl_union_pw_aff_add(upa, upa_i);
8358 for (i = 0; i < n_div; ++i) {
8359 isl_aff *div;
8360 isl_union_pw_aff *upa_i;
8362 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8363 continue;
8364 div = isl_aff_get_div(aff, i);
8365 upa_i = multi_union_pw_aff_apply_aff(
8366 isl_multi_union_pw_aff_copy(mupa), div);
8367 upa_i = isl_union_pw_aff_floor(upa_i);
8368 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8369 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8370 upa = isl_union_pw_aff_add(upa, upa_i);
8373 isl_multi_union_pw_aff_free(mupa);
8374 isl_aff_free(aff);
8376 return upa;
8379 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8380 * with the domain of "aff".
8381 * Furthermore, the dimension of this space needs to be greater than zero.
8382 * The result is defined over the shared domain of the elements of "mupa"
8384 * We perform these checks and then hand over control to
8385 * multi_union_pw_aff_apply_aff.
8387 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8388 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8390 isl_space *space1, *space2;
8391 int equal;
8393 mupa = isl_multi_union_pw_aff_align_params(mupa,
8394 isl_aff_get_space(aff));
8395 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8396 if (!mupa || !aff)
8397 goto error;
8399 space1 = isl_multi_union_pw_aff_get_space(mupa);
8400 space2 = isl_aff_get_domain_space(aff);
8401 equal = isl_space_is_equal(space1, space2);
8402 isl_space_free(space1);
8403 isl_space_free(space2);
8404 if (equal < 0)
8405 goto error;
8406 if (!equal)
8407 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8408 "spaces don't match", goto error);
8409 if (isl_aff_dim(aff, isl_dim_in) == 0)
8410 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8411 "cannot determine domains", goto error);
8413 return multi_union_pw_aff_apply_aff(mupa, aff);
8414 error:
8415 isl_multi_union_pw_aff_free(mupa);
8416 isl_aff_free(aff);
8417 return NULL;
8420 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8421 * with the domain of "ma".
8422 * Furthermore, the dimension of this space needs to be greater than zero,
8423 * unless the dimension of the target space of "ma" is also zero.
8424 * The result is defined over the shared domain of the elements of "mupa"
8426 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8427 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8429 isl_space *space1, *space2;
8430 isl_multi_union_pw_aff *res;
8431 int equal;
8432 int i, n_out;
8434 mupa = isl_multi_union_pw_aff_align_params(mupa,
8435 isl_multi_aff_get_space(ma));
8436 ma = isl_multi_aff_align_params(ma,
8437 isl_multi_union_pw_aff_get_space(mupa));
8438 if (!mupa || !ma)
8439 goto error;
8441 space1 = isl_multi_union_pw_aff_get_space(mupa);
8442 space2 = isl_multi_aff_get_domain_space(ma);
8443 equal = isl_space_is_equal(space1, space2);
8444 isl_space_free(space1);
8445 isl_space_free(space2);
8446 if (equal < 0)
8447 goto error;
8448 if (!equal)
8449 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8450 "spaces don't match", goto error);
8451 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8452 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8453 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8454 "cannot determine domains", goto error);
8456 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8457 res = isl_multi_union_pw_aff_alloc(space1);
8459 for (i = 0; i < n_out; ++i) {
8460 isl_aff *aff;
8461 isl_union_pw_aff *upa;
8463 aff = isl_multi_aff_get_aff(ma, i);
8464 upa = multi_union_pw_aff_apply_aff(
8465 isl_multi_union_pw_aff_copy(mupa), aff);
8466 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8469 isl_multi_aff_free(ma);
8470 isl_multi_union_pw_aff_free(mupa);
8471 return res;
8472 error:
8473 isl_multi_union_pw_aff_free(mupa);
8474 isl_multi_aff_free(ma);
8475 return NULL;
8478 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8479 * with the domain of "pa".
8480 * Furthermore, the dimension of this space needs to be greater than zero.
8481 * The result is defined over the shared domain of the elements of "mupa"
8483 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8484 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8486 int i;
8487 int equal;
8488 isl_space *space, *space2;
8489 isl_union_pw_aff *upa;
8491 mupa = isl_multi_union_pw_aff_align_params(mupa,
8492 isl_pw_aff_get_space(pa));
8493 pa = isl_pw_aff_align_params(pa,
8494 isl_multi_union_pw_aff_get_space(mupa));
8495 if (!mupa || !pa)
8496 goto error;
8498 space = isl_multi_union_pw_aff_get_space(mupa);
8499 space2 = isl_pw_aff_get_domain_space(pa);
8500 equal = isl_space_is_equal(space, space2);
8501 isl_space_free(space);
8502 isl_space_free(space2);
8503 if (equal < 0)
8504 goto error;
8505 if (!equal)
8506 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8507 "spaces don't match", goto error);
8508 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8509 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8510 "cannot determine domains", goto error);
8512 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8513 upa = isl_union_pw_aff_empty(space);
8515 for (i = 0; i < pa->n; ++i) {
8516 isl_aff *aff;
8517 isl_set *domain;
8518 isl_multi_union_pw_aff *mupa_i;
8519 isl_union_pw_aff *upa_i;
8521 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8522 domain = isl_set_copy(pa->p[i].set);
8523 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8524 aff = isl_aff_copy(pa->p[i].aff);
8525 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8526 upa = isl_union_pw_aff_union_add(upa, upa_i);
8529 isl_multi_union_pw_aff_free(mupa);
8530 isl_pw_aff_free(pa);
8531 return upa;
8532 error:
8533 isl_multi_union_pw_aff_free(mupa);
8534 isl_pw_aff_free(pa);
8535 return NULL;
8538 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8539 * with the domain of "pma".
8540 * Furthermore, the dimension of this space needs to be greater than zero,
8541 * unless the dimension of the target space of "pma" is also zero.
8542 * The result is defined over the shared domain of the elements of "mupa"
8544 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8545 __isl_take isl_multi_union_pw_aff *mupa,
8546 __isl_take isl_pw_multi_aff *pma)
8548 isl_space *space1, *space2;
8549 isl_multi_union_pw_aff *res;
8550 int equal;
8551 int i, n_out;
8553 mupa = isl_multi_union_pw_aff_align_params(mupa,
8554 isl_pw_multi_aff_get_space(pma));
8555 pma = isl_pw_multi_aff_align_params(pma,
8556 isl_multi_union_pw_aff_get_space(mupa));
8557 if (!mupa || !pma)
8558 goto error;
8560 space1 = isl_multi_union_pw_aff_get_space(mupa);
8561 space2 = isl_pw_multi_aff_get_domain_space(pma);
8562 equal = isl_space_is_equal(space1, space2);
8563 isl_space_free(space1);
8564 isl_space_free(space2);
8565 if (equal < 0)
8566 goto error;
8567 if (!equal)
8568 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8569 "spaces don't match", goto error);
8570 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8571 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8572 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8573 "cannot determine domains", goto error);
8575 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8576 res = isl_multi_union_pw_aff_alloc(space1);
8578 for (i = 0; i < n_out; ++i) {
8579 isl_pw_aff *pa;
8580 isl_union_pw_aff *upa;
8582 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8583 upa = isl_multi_union_pw_aff_apply_pw_aff(
8584 isl_multi_union_pw_aff_copy(mupa), pa);
8585 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8588 isl_pw_multi_aff_free(pma);
8589 isl_multi_union_pw_aff_free(mupa);
8590 return res;
8591 error:
8592 isl_multi_union_pw_aff_free(mupa);
8593 isl_pw_multi_aff_free(pma);
8594 return NULL;
8597 /* Compute the pullback of "mupa" by the function represented by "upma".
8598 * In other words, plug in "upma" in "mupa". The result contains
8599 * expressions defined over the domain space of "upma".
8601 * Run over all elements of "mupa" and plug in "upma" in each of them.
8603 __isl_give isl_multi_union_pw_aff *
8604 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8605 __isl_take isl_multi_union_pw_aff *mupa,
8606 __isl_take isl_union_pw_multi_aff *upma)
8608 int i, n;
8610 mupa = isl_multi_union_pw_aff_align_params(mupa,
8611 isl_union_pw_multi_aff_get_space(upma));
8612 upma = isl_union_pw_multi_aff_align_params(upma,
8613 isl_multi_union_pw_aff_get_space(mupa));
8614 if (!mupa || !upma)
8615 goto error;
8617 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8618 for (i = 0; i < n; ++i) {
8619 isl_union_pw_aff *upa;
8621 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8622 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8623 isl_union_pw_multi_aff_copy(upma));
8624 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8627 isl_union_pw_multi_aff_free(upma);
8628 return mupa;
8629 error:
8630 isl_multi_union_pw_aff_free(mupa);
8631 isl_union_pw_multi_aff_free(upma);
8632 return NULL;
8635 /* Extract the sequence of elements in "mupa" with domain space "space"
8636 * (ignoring parameters).
8638 * For the elements of "mupa" that are not defined on the specified space,
8639 * the corresponding element in the result is empty.
8641 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8642 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8644 int i, n;
8645 isl_space *space_mpa = NULL;
8646 isl_multi_pw_aff *mpa;
8648 if (!mupa || !space)
8649 goto error;
8651 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8652 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8653 space = isl_space_drop_dims(space, isl_dim_param,
8654 0, isl_space_dim(space, isl_dim_param));
8655 space = isl_space_align_params(space,
8656 isl_space_copy(space_mpa));
8657 if (!space)
8658 goto error;
8660 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8661 space_mpa);
8662 mpa = isl_multi_pw_aff_alloc(space_mpa);
8664 space = isl_space_from_domain(space);
8665 space = isl_space_add_dims(space, isl_dim_out, 1);
8666 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8667 for (i = 0; i < n; ++i) {
8668 isl_union_pw_aff *upa;
8669 isl_pw_aff *pa;
8671 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8672 pa = isl_union_pw_aff_extract_pw_aff(upa,
8673 isl_space_copy(space));
8674 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8675 isl_union_pw_aff_free(upa);
8678 isl_space_free(space);
8679 return mpa;
8680 error:
8681 isl_space_free(space_mpa);
8682 isl_space_free(space);
8683 return NULL;