add isl_mat_get_hash
[isl.git] / isl_aff.c
blob845d7e339c031945ba7f43e604fd6636f45a7c9c
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
230 if (!aff)
231 return NULL;
233 aff->ref++;
234 return aff;
237 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
239 if (!aff)
240 return NULL;
242 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
243 isl_vec_copy(aff->v));
246 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
248 if (!aff)
249 return NULL;
251 if (aff->ref == 1)
252 return aff;
253 aff->ref--;
254 return isl_aff_dup(aff);
257 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
259 if (!aff)
260 return NULL;
262 if (--aff->ref > 0)
263 return NULL;
265 isl_local_space_free(aff->ls);
266 isl_vec_free(aff->v);
268 free(aff);
270 return NULL;
273 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
275 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
278 /* Externally, an isl_aff has a map space, but internally, the
279 * ls field corresponds to the domain of that space.
281 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
283 if (!aff)
284 return 0;
285 if (type == isl_dim_out)
286 return 1;
287 if (type == isl_dim_in)
288 type = isl_dim_set;
289 return isl_local_space_dim(aff->ls, type);
292 /* Return the position of the dimension of the given type and name
293 * in "aff".
294 * Return -1 if no such dimension can be found.
296 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
297 const char *name)
299 if (!aff)
300 return -1;
301 if (type == isl_dim_out)
302 return -1;
303 if (type == isl_dim_in)
304 type = isl_dim_set;
305 return isl_local_space_find_dim_by_name(aff->ls, type, name);
308 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
310 return aff ? isl_local_space_get_space(aff->ls) : NULL;
313 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
315 isl_space *space;
316 if (!aff)
317 return NULL;
318 space = isl_local_space_get_space(aff->ls);
319 space = isl_space_from_domain(space);
320 space = isl_space_add_dims(space, isl_dim_out, 1);
321 return space;
324 __isl_give isl_local_space *isl_aff_get_domain_local_space(
325 __isl_keep isl_aff *aff)
327 return aff ? isl_local_space_copy(aff->ls) : NULL;
330 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
332 isl_local_space *ls;
333 if (!aff)
334 return NULL;
335 ls = isl_local_space_copy(aff->ls);
336 ls = isl_local_space_from_domain(ls);
337 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
338 return ls;
341 /* Externally, an isl_aff has a map space, but internally, the
342 * ls field corresponds to the domain of that space.
344 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
345 enum isl_dim_type type, unsigned pos)
347 if (!aff)
348 return NULL;
349 if (type == isl_dim_out)
350 return NULL;
351 if (type == isl_dim_in)
352 type = isl_dim_set;
353 return isl_local_space_get_dim_name(aff->ls, type, pos);
356 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
357 __isl_take isl_space *dim)
359 aff = isl_aff_cow(aff);
360 if (!aff || !dim)
361 goto error;
363 aff->ls = isl_local_space_reset_space(aff->ls, dim);
364 if (!aff->ls)
365 return isl_aff_free(aff);
367 return aff;
368 error:
369 isl_aff_free(aff);
370 isl_space_free(dim);
371 return NULL;
374 /* Reset the space of "aff". This function is called from isl_pw_templ.c
375 * and doesn't know if the space of an element object is represented
376 * directly or through its domain. It therefore passes along both.
378 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
379 __isl_take isl_space *space, __isl_take isl_space *domain)
381 isl_space_free(space);
382 return isl_aff_reset_domain_space(aff, domain);
385 /* Reorder the coefficients of the affine expression based
386 * on the given reodering.
387 * The reordering r is assumed to have been extended with the local
388 * variables.
390 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
391 __isl_take isl_reordering *r, int n_div)
393 isl_vec *res;
394 int i;
396 if (!vec || !r)
397 goto error;
399 res = isl_vec_alloc(vec->ctx,
400 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
401 isl_seq_cpy(res->el, vec->el, 2);
402 isl_seq_clr(res->el + 2, res->size - 2);
403 for (i = 0; i < r->len; ++i)
404 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
406 isl_reordering_free(r);
407 isl_vec_free(vec);
408 return res;
409 error:
410 isl_vec_free(vec);
411 isl_reordering_free(r);
412 return NULL;
415 /* Reorder the dimensions of the domain of "aff" according
416 * to the given reordering.
418 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
419 __isl_take isl_reordering *r)
421 aff = isl_aff_cow(aff);
422 if (!aff)
423 goto error;
425 r = isl_reordering_extend(r, aff->ls->div->n_row);
426 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
427 aff->ls->div->n_row);
428 aff->ls = isl_local_space_realign(aff->ls, r);
430 if (!aff->v || !aff->ls)
431 return isl_aff_free(aff);
433 return aff;
434 error:
435 isl_aff_free(aff);
436 isl_reordering_free(r);
437 return NULL;
440 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
441 __isl_take isl_space *model)
443 if (!aff || !model)
444 goto error;
446 if (!isl_space_match(aff->ls->dim, isl_dim_param,
447 model, isl_dim_param)) {
448 isl_reordering *exp;
450 model = isl_space_drop_dims(model, isl_dim_in,
451 0, isl_space_dim(model, isl_dim_in));
452 model = isl_space_drop_dims(model, isl_dim_out,
453 0, isl_space_dim(model, isl_dim_out));
454 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
455 exp = isl_reordering_extend_space(exp,
456 isl_aff_get_domain_space(aff));
457 aff = isl_aff_realign_domain(aff, exp);
460 isl_space_free(model);
461 return aff;
462 error:
463 isl_space_free(model);
464 isl_aff_free(aff);
465 return NULL;
468 /* Is "aff" obviously equal to zero?
470 * If the denominator is zero, then "aff" is not equal to zero.
472 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
474 if (!aff)
475 return isl_bool_error;
477 if (isl_int_is_zero(aff->v->el[0]))
478 return isl_bool_false;
479 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
482 /* Does "aff" represent NaN?
484 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
486 if (!aff)
487 return isl_bool_error;
489 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
492 /* Does "pa" involve any NaNs?
494 isl_bool isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
496 int i;
498 if (!pa)
499 return isl_bool_error;
500 if (pa->n == 0)
501 return isl_bool_false;
503 for (i = 0; i < pa->n; ++i) {
504 isl_bool is_nan = isl_aff_is_nan(pa->p[i].aff);
505 if (is_nan < 0 || is_nan)
506 return is_nan;
509 return isl_bool_false;
512 /* Are "aff1" and "aff2" obviously equal?
514 * NaN is not equal to anything, not even to another NaN.
516 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
517 __isl_keep isl_aff *aff2)
519 isl_bool equal;
521 if (!aff1 || !aff2)
522 return isl_bool_error;
524 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
525 return isl_bool_false;
527 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
528 if (equal < 0 || !equal)
529 return equal;
531 return isl_vec_is_equal(aff1->v, aff2->v);
534 /* Return the common denominator of "aff" in "v".
536 * We cannot return anything meaningful in case of a NaN.
538 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
540 if (!aff)
541 return -1;
542 if (isl_aff_is_nan(aff))
543 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
544 "cannot get denominator of NaN", return -1);
545 isl_int_set(*v, aff->v->el[0]);
546 return 0;
549 /* Return the common denominator of "aff".
551 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
553 isl_ctx *ctx;
555 if (!aff)
556 return NULL;
558 ctx = isl_aff_get_ctx(aff);
559 if (isl_aff_is_nan(aff))
560 return isl_val_nan(ctx);
561 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
564 /* Return the constant term of "aff" in "v".
566 * We cannot return anything meaningful in case of a NaN.
568 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
570 if (!aff)
571 return -1;
572 if (isl_aff_is_nan(aff))
573 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
574 "cannot get constant term of NaN", return -1);
575 isl_int_set(*v, aff->v->el[1]);
576 return 0;
579 /* Return the constant term of "aff".
581 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
583 isl_ctx *ctx;
584 isl_val *v;
586 if (!aff)
587 return NULL;
589 ctx = isl_aff_get_ctx(aff);
590 if (isl_aff_is_nan(aff))
591 return isl_val_nan(ctx);
592 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
593 return isl_val_normalize(v);
596 /* Return the coefficient of the variable of type "type" at position "pos"
597 * of "aff" in "v".
599 * We cannot return anything meaningful in case of a NaN.
601 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
602 enum isl_dim_type type, int pos, isl_int *v)
604 if (!aff)
605 return -1;
607 if (type == isl_dim_out)
608 isl_die(aff->v->ctx, isl_error_invalid,
609 "output/set dimension does not have a coefficient",
610 return -1);
611 if (type == isl_dim_in)
612 type = isl_dim_set;
614 if (pos >= isl_local_space_dim(aff->ls, type))
615 isl_die(aff->v->ctx, isl_error_invalid,
616 "position out of bounds", return -1);
618 if (isl_aff_is_nan(aff))
619 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
620 "cannot get coefficient of NaN", return -1);
621 pos += isl_local_space_offset(aff->ls, type);
622 isl_int_set(*v, aff->v->el[1 + pos]);
624 return 0;
627 /* Return the coefficient of the variable of type "type" at position "pos"
628 * of "aff".
630 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
631 enum isl_dim_type type, int pos)
633 isl_ctx *ctx;
634 isl_val *v;
636 if (!aff)
637 return NULL;
639 ctx = isl_aff_get_ctx(aff);
640 if (type == isl_dim_out)
641 isl_die(ctx, isl_error_invalid,
642 "output/set dimension does not have a coefficient",
643 return NULL);
644 if (type == isl_dim_in)
645 type = isl_dim_set;
647 if (pos >= isl_local_space_dim(aff->ls, type))
648 isl_die(ctx, isl_error_invalid,
649 "position out of bounds", return NULL);
651 if (isl_aff_is_nan(aff))
652 return isl_val_nan(ctx);
653 pos += isl_local_space_offset(aff->ls, type);
654 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
655 return isl_val_normalize(v);
658 /* Return the sign of the coefficient of the variable of type "type"
659 * at position "pos" of "aff".
661 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
662 int pos)
664 isl_ctx *ctx;
666 if (!aff)
667 return 0;
669 ctx = isl_aff_get_ctx(aff);
670 if (type == isl_dim_out)
671 isl_die(ctx, isl_error_invalid,
672 "output/set dimension does not have a coefficient",
673 return 0);
674 if (type == isl_dim_in)
675 type = isl_dim_set;
677 if (pos >= isl_local_space_dim(aff->ls, type))
678 isl_die(ctx, isl_error_invalid,
679 "position out of bounds", return 0);
681 pos += isl_local_space_offset(aff->ls, type);
682 return isl_int_sgn(aff->v->el[1 + pos]);
685 /* Replace the denominator of "aff" by "v".
687 * A NaN is unaffected by this operation.
689 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
691 if (!aff)
692 return NULL;
693 if (isl_aff_is_nan(aff))
694 return aff;
695 aff = isl_aff_cow(aff);
696 if (!aff)
697 return NULL;
699 aff->v = isl_vec_cow(aff->v);
700 if (!aff->v)
701 return isl_aff_free(aff);
703 isl_int_set(aff->v->el[0], v);
705 return aff;
708 /* Replace the numerator of the constant term of "aff" by "v".
710 * A NaN is unaffected by this operation.
712 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
714 if (!aff)
715 return NULL;
716 if (isl_aff_is_nan(aff))
717 return aff;
718 aff = isl_aff_cow(aff);
719 if (!aff)
720 return NULL;
722 aff->v = isl_vec_cow(aff->v);
723 if (!aff->v)
724 return isl_aff_free(aff);
726 isl_int_set(aff->v->el[1], v);
728 return aff;
731 /* Replace the constant term of "aff" by "v".
733 * A NaN is unaffected by this operation.
735 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
736 __isl_take isl_val *v)
738 if (!aff || !v)
739 goto error;
741 if (isl_aff_is_nan(aff)) {
742 isl_val_free(v);
743 return aff;
746 if (!isl_val_is_rat(v))
747 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
748 "expecting rational value", goto error);
750 if (isl_int_eq(aff->v->el[1], v->n) &&
751 isl_int_eq(aff->v->el[0], v->d)) {
752 isl_val_free(v);
753 return aff;
756 aff = isl_aff_cow(aff);
757 if (!aff)
758 goto error;
759 aff->v = isl_vec_cow(aff->v);
760 if (!aff->v)
761 goto error;
763 if (isl_int_eq(aff->v->el[0], v->d)) {
764 isl_int_set(aff->v->el[1], v->n);
765 } else if (isl_int_is_one(v->d)) {
766 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
767 } else {
768 isl_seq_scale(aff->v->el + 1,
769 aff->v->el + 1, v->d, aff->v->size - 1);
770 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
771 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
772 aff->v = isl_vec_normalize(aff->v);
773 if (!aff->v)
774 goto error;
777 isl_val_free(v);
778 return aff;
779 error:
780 isl_aff_free(aff);
781 isl_val_free(v);
782 return NULL;
785 /* Add "v" to the constant term of "aff".
787 * A NaN is unaffected by this operation.
789 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
791 if (isl_int_is_zero(v))
792 return aff;
794 if (!aff)
795 return NULL;
796 if (isl_aff_is_nan(aff))
797 return aff;
798 aff = isl_aff_cow(aff);
799 if (!aff)
800 return NULL;
802 aff->v = isl_vec_cow(aff->v);
803 if (!aff->v)
804 return isl_aff_free(aff);
806 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
808 return aff;
811 /* Add "v" to the constant term of "aff".
813 * A NaN is unaffected by this operation.
815 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
816 __isl_take isl_val *v)
818 if (!aff || !v)
819 goto error;
821 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
822 isl_val_free(v);
823 return aff;
826 if (!isl_val_is_rat(v))
827 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
828 "expecting rational value", goto error);
830 aff = isl_aff_cow(aff);
831 if (!aff)
832 goto error;
834 aff->v = isl_vec_cow(aff->v);
835 if (!aff->v)
836 goto error;
838 if (isl_int_is_one(v->d)) {
839 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
840 } else if (isl_int_eq(aff->v->el[0], v->d)) {
841 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
842 aff->v = isl_vec_normalize(aff->v);
843 if (!aff->v)
844 goto error;
845 } else {
846 isl_seq_scale(aff->v->el + 1,
847 aff->v->el + 1, v->d, aff->v->size - 1);
848 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
849 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
850 aff->v = isl_vec_normalize(aff->v);
851 if (!aff->v)
852 goto error;
855 isl_val_free(v);
856 return aff;
857 error:
858 isl_aff_free(aff);
859 isl_val_free(v);
860 return NULL;
863 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
865 isl_int t;
867 isl_int_init(t);
868 isl_int_set_si(t, v);
869 aff = isl_aff_add_constant(aff, t);
870 isl_int_clear(t);
872 return aff;
875 /* Add "v" to the numerator of the constant term of "aff".
877 * A NaN is unaffected by this operation.
879 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
881 if (isl_int_is_zero(v))
882 return aff;
884 if (!aff)
885 return NULL;
886 if (isl_aff_is_nan(aff))
887 return aff;
888 aff = isl_aff_cow(aff);
889 if (!aff)
890 return NULL;
892 aff->v = isl_vec_cow(aff->v);
893 if (!aff->v)
894 return isl_aff_free(aff);
896 isl_int_add(aff->v->el[1], aff->v->el[1], v);
898 return aff;
901 /* Add "v" to the numerator of the constant term of "aff".
903 * A NaN is unaffected by this operation.
905 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
907 isl_int t;
909 if (v == 0)
910 return aff;
912 isl_int_init(t);
913 isl_int_set_si(t, v);
914 aff = isl_aff_add_constant_num(aff, t);
915 isl_int_clear(t);
917 return aff;
920 /* Replace the numerator of the constant term of "aff" by "v".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
926 if (!aff)
927 return NULL;
928 if (isl_aff_is_nan(aff))
929 return aff;
930 aff = isl_aff_cow(aff);
931 if (!aff)
932 return NULL;
934 aff->v = isl_vec_cow(aff->v);
935 if (!aff->v)
936 return isl_aff_free(aff);
938 isl_int_set_si(aff->v->el[1], v);
940 return aff;
943 /* Replace the numerator of the coefficient of the variable of type "type"
944 * at position "pos" of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
949 enum isl_dim_type type, int pos, isl_int v)
951 if (!aff)
952 return NULL;
954 if (type == isl_dim_out)
955 isl_die(aff->v->ctx, isl_error_invalid,
956 "output/set dimension does not have a coefficient",
957 return isl_aff_free(aff));
958 if (type == isl_dim_in)
959 type = isl_dim_set;
961 if (pos >= isl_local_space_dim(aff->ls, type))
962 isl_die(aff->v->ctx, isl_error_invalid,
963 "position out of bounds", return isl_aff_free(aff));
965 if (isl_aff_is_nan(aff))
966 return aff;
967 aff = isl_aff_cow(aff);
968 if (!aff)
969 return NULL;
971 aff->v = isl_vec_cow(aff->v);
972 if (!aff->v)
973 return isl_aff_free(aff);
975 pos += isl_local_space_offset(aff->ls, type);
976 isl_int_set(aff->v->el[1 + pos], v);
978 return aff;
981 /* Replace the numerator of the coefficient of the variable of type "type"
982 * at position "pos" of "aff" by "v".
984 * A NaN is unaffected by this operation.
986 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
987 enum isl_dim_type type, int pos, int v)
989 if (!aff)
990 return NULL;
992 if (type == isl_dim_out)
993 isl_die(aff->v->ctx, isl_error_invalid,
994 "output/set dimension does not have a coefficient",
995 return isl_aff_free(aff));
996 if (type == isl_dim_in)
997 type = isl_dim_set;
999 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1000 isl_die(aff->v->ctx, isl_error_invalid,
1001 "position out of bounds", return isl_aff_free(aff));
1003 if (isl_aff_is_nan(aff))
1004 return aff;
1005 pos += isl_local_space_offset(aff->ls, type);
1006 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1007 return aff;
1009 aff = isl_aff_cow(aff);
1010 if (!aff)
1011 return NULL;
1013 aff->v = isl_vec_cow(aff->v);
1014 if (!aff->v)
1015 return isl_aff_free(aff);
1017 isl_int_set_si(aff->v->el[1 + pos], v);
1019 return aff;
1022 /* Replace the coefficient of the variable of type "type" at position "pos"
1023 * of "aff" by "v".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1028 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1030 if (!aff || !v)
1031 goto error;
1033 if (type == isl_dim_out)
1034 isl_die(aff->v->ctx, isl_error_invalid,
1035 "output/set dimension does not have a coefficient",
1036 goto error);
1037 if (type == isl_dim_in)
1038 type = isl_dim_set;
1040 if (pos >= isl_local_space_dim(aff->ls, type))
1041 isl_die(aff->v->ctx, isl_error_invalid,
1042 "position out of bounds", goto error);
1044 if (isl_aff_is_nan(aff)) {
1045 isl_val_free(v);
1046 return aff;
1048 if (!isl_val_is_rat(v))
1049 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1050 "expecting rational value", goto error);
1052 pos += isl_local_space_offset(aff->ls, type);
1053 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1054 isl_int_eq(aff->v->el[0], v->d)) {
1055 isl_val_free(v);
1056 return aff;
1059 aff = isl_aff_cow(aff);
1060 if (!aff)
1061 goto error;
1062 aff->v = isl_vec_cow(aff->v);
1063 if (!aff->v)
1064 goto error;
1066 if (isl_int_eq(aff->v->el[0], v->d)) {
1067 isl_int_set(aff->v->el[1 + pos], v->n);
1068 } else if (isl_int_is_one(v->d)) {
1069 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1070 } else {
1071 isl_seq_scale(aff->v->el + 1,
1072 aff->v->el + 1, v->d, aff->v->size - 1);
1073 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1074 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1075 aff->v = isl_vec_normalize(aff->v);
1076 if (!aff->v)
1077 goto error;
1080 isl_val_free(v);
1081 return aff;
1082 error:
1083 isl_aff_free(aff);
1084 isl_val_free(v);
1085 return NULL;
1088 /* Add "v" to the coefficient of the variable of type "type"
1089 * at position "pos" of "aff".
1091 * A NaN is unaffected by this operation.
1093 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1094 enum isl_dim_type type, int pos, isl_int v)
1096 if (!aff)
1097 return NULL;
1099 if (type == isl_dim_out)
1100 isl_die(aff->v->ctx, isl_error_invalid,
1101 "output/set dimension does not have a coefficient",
1102 return isl_aff_free(aff));
1103 if (type == isl_dim_in)
1104 type = isl_dim_set;
1106 if (pos >= isl_local_space_dim(aff->ls, type))
1107 isl_die(aff->v->ctx, isl_error_invalid,
1108 "position out of bounds", return isl_aff_free(aff));
1110 if (isl_aff_is_nan(aff))
1111 return aff;
1112 aff = isl_aff_cow(aff);
1113 if (!aff)
1114 return NULL;
1116 aff->v = isl_vec_cow(aff->v);
1117 if (!aff->v)
1118 return isl_aff_free(aff);
1120 pos += isl_local_space_offset(aff->ls, type);
1121 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1123 return aff;
1126 /* Add "v" to the coefficient of the variable of type "type"
1127 * at position "pos" of "aff".
1129 * A NaN is unaffected by this operation.
1131 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1132 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1134 if (!aff || !v)
1135 goto error;
1137 if (isl_val_is_zero(v)) {
1138 isl_val_free(v);
1139 return aff;
1142 if (type == isl_dim_out)
1143 isl_die(aff->v->ctx, isl_error_invalid,
1144 "output/set dimension does not have a coefficient",
1145 goto error);
1146 if (type == isl_dim_in)
1147 type = isl_dim_set;
1149 if (pos >= isl_local_space_dim(aff->ls, type))
1150 isl_die(aff->v->ctx, isl_error_invalid,
1151 "position out of bounds", goto error);
1153 if (isl_aff_is_nan(aff)) {
1154 isl_val_free(v);
1155 return aff;
1157 if (!isl_val_is_rat(v))
1158 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1159 "expecting rational value", goto error);
1161 aff = isl_aff_cow(aff);
1162 if (!aff)
1163 goto error;
1165 aff->v = isl_vec_cow(aff->v);
1166 if (!aff->v)
1167 goto error;
1169 pos += isl_local_space_offset(aff->ls, type);
1170 if (isl_int_is_one(v->d)) {
1171 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1172 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1173 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1174 aff->v = isl_vec_normalize(aff->v);
1175 if (!aff->v)
1176 goto error;
1177 } else {
1178 isl_seq_scale(aff->v->el + 1,
1179 aff->v->el + 1, v->d, aff->v->size - 1);
1180 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1181 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1182 aff->v = isl_vec_normalize(aff->v);
1183 if (!aff->v)
1184 goto error;
1187 isl_val_free(v);
1188 return aff;
1189 error:
1190 isl_aff_free(aff);
1191 isl_val_free(v);
1192 return NULL;
1195 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1196 enum isl_dim_type type, int pos, int v)
1198 isl_int t;
1200 isl_int_init(t);
1201 isl_int_set_si(t, v);
1202 aff = isl_aff_add_coefficient(aff, type, pos, t);
1203 isl_int_clear(t);
1205 return aff;
1208 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1210 if (!aff)
1211 return NULL;
1213 return isl_local_space_get_div(aff->ls, pos);
1216 /* Return the negation of "aff".
1218 * As a special case, -NaN = NaN.
1220 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1222 if (!aff)
1223 return NULL;
1224 if (isl_aff_is_nan(aff))
1225 return aff;
1226 aff = isl_aff_cow(aff);
1227 if (!aff)
1228 return NULL;
1229 aff->v = isl_vec_cow(aff->v);
1230 if (!aff->v)
1231 return isl_aff_free(aff);
1233 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1235 return aff;
1238 /* Remove divs from the local space that do not appear in the affine
1239 * expression.
1240 * We currently only remove divs at the end.
1241 * Some intermediate divs may also not appear directly in the affine
1242 * expression, but we would also need to check that no other divs are
1243 * defined in terms of them.
1245 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1247 int pos;
1248 int off;
1249 int n;
1251 if (!aff)
1252 return NULL;
1254 n = isl_local_space_dim(aff->ls, isl_dim_div);
1255 off = isl_local_space_offset(aff->ls, isl_dim_div);
1257 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1258 if (pos == n)
1259 return aff;
1261 aff = isl_aff_cow(aff);
1262 if (!aff)
1263 return NULL;
1265 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1266 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1267 if (!aff->ls || !aff->v)
1268 return isl_aff_free(aff);
1270 return aff;
1273 /* Given two affine expressions "p" of length p_len (including the
1274 * denominator and the constant term) and "subs" of length subs_len,
1275 * plug in "subs" for the variable at position "pos".
1276 * The variables of "subs" and "p" are assumed to match up to subs_len,
1277 * but "p" may have additional variables.
1278 * "v" is an initialized isl_int that can be used internally.
1280 * In particular, if "p" represents the expression
1282 * (a i + g)/m
1284 * with i the variable at position "pos" and "subs" represents the expression
1286 * f/d
1288 * then the result represents the expression
1290 * (a f + d g)/(m d)
1293 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1294 int p_len, int subs_len, isl_int v)
1296 isl_int_set(v, p[1 + pos]);
1297 isl_int_set_si(p[1 + pos], 0);
1298 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1299 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1300 isl_int_mul(p[0], p[0], subs[0]);
1303 /* Look for any divs in the aff->ls with a denominator equal to one
1304 * and plug them into the affine expression and any subsequent divs
1305 * that may reference the div.
1307 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1309 int i, n;
1310 int len;
1311 isl_int v;
1312 isl_vec *vec;
1313 isl_local_space *ls;
1314 unsigned pos;
1316 if (!aff)
1317 return NULL;
1319 n = isl_local_space_dim(aff->ls, isl_dim_div);
1320 len = aff->v->size;
1321 for (i = 0; i < n; ++i) {
1322 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1323 continue;
1324 ls = isl_local_space_copy(aff->ls);
1325 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1326 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1327 vec = isl_vec_copy(aff->v);
1328 vec = isl_vec_cow(vec);
1329 if (!ls || !vec)
1330 goto error;
1332 isl_int_init(v);
1334 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1335 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1336 len, len, v);
1338 isl_int_clear(v);
1340 isl_vec_free(aff->v);
1341 aff->v = vec;
1342 isl_local_space_free(aff->ls);
1343 aff->ls = ls;
1346 return aff;
1347 error:
1348 isl_vec_free(vec);
1349 isl_local_space_free(ls);
1350 return isl_aff_free(aff);
1353 /* Look for any divs j that appear with a unit coefficient inside
1354 * the definitions of other divs i and plug them into the definitions
1355 * of the divs i.
1357 * In particular, an expression of the form
1359 * floor((f(..) + floor(g(..)/n))/m)
1361 * is simplified to
1363 * floor((n * f(..) + g(..))/(n * m))
1365 * This simplification is correct because we can move the expression
1366 * f(..) into the inner floor in the original expression to obtain
1368 * floor(floor((n * f(..) + g(..))/n)/m)
1370 * from which we can derive the simplified expression.
1372 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1374 int i, j, n;
1375 int off;
1377 if (!aff)
1378 return NULL;
1380 n = isl_local_space_dim(aff->ls, isl_dim_div);
1381 off = isl_local_space_offset(aff->ls, isl_dim_div);
1382 for (i = 1; i < n; ++i) {
1383 for (j = 0; j < i; ++j) {
1384 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1385 continue;
1386 aff->ls = isl_local_space_substitute_seq(aff->ls,
1387 isl_dim_div, j, aff->ls->div->row[j],
1388 aff->v->size, i, 1);
1389 if (!aff->ls)
1390 return isl_aff_free(aff);
1394 return aff;
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1404 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1405 isl_local_space *ls;
1406 isl_vec *v;
1408 ls = isl_local_space_copy(aff->ls);
1409 ls = isl_local_space_swap_div(ls, a, b);
1410 v = isl_vec_copy(aff->v);
1411 v = isl_vec_cow(v);
1412 if (!ls || !v)
1413 goto error;
1415 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1416 isl_vec_free(aff->v);
1417 aff->v = v;
1418 isl_local_space_free(aff->ls);
1419 aff->ls = ls;
1421 return aff;
1422 error:
1423 isl_vec_free(v);
1424 isl_local_space_free(ls);
1425 return isl_aff_free(aff);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1435 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1437 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1438 return aff;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1 + off + a],
1445 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1446 isl_int_set_si(aff->v->el[1 + off + b], 0);
1448 return aff;
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1462 int i, j, n;
1464 if (!aff)
1465 return NULL;
1467 n = isl_aff_dim(aff, isl_dim_div);
1468 for (i = 1; i < n; ++i) {
1469 for (j = i - 1; j >= 0; --j) {
1470 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1471 if (cmp < 0)
1472 break;
1473 if (cmp == 0)
1474 aff = merge_divs(aff, j, j + 1);
1475 else
1476 aff = swap_div(aff, j, j + 1);
1477 if (!aff)
1478 return NULL;
1482 return aff;
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1493 if (!aff)
1494 return NULL;
1495 aff->v = isl_vec_normalize(aff->v);
1496 if (!aff->v)
1497 return isl_aff_free(aff);
1498 aff = plug_in_integral_divs(aff);
1499 aff = plug_in_unit_divs(aff);
1500 aff = sort_divs(aff);
1501 aff = isl_aff_remove_unused_divs(aff);
1502 return aff;
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * As a special case, floor(NaN) = NaN.
1514 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1516 int i;
1517 int size;
1518 isl_ctx *ctx;
1519 isl_vec *div;
1521 if (!aff)
1522 return NULL;
1524 if (isl_aff_is_nan(aff))
1525 return aff;
1526 if (isl_int_is_one(aff->v->el[0]))
1527 return aff;
1529 aff = isl_aff_cow(aff);
1530 if (!aff)
1531 return NULL;
1533 aff->v = isl_vec_cow(aff->v);
1534 if (!aff->v)
1535 return isl_aff_free(aff);
1537 if (isl_aff_is_cst(aff)) {
1538 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1539 isl_int_set_si(aff->v->el[0], 1);
1540 return aff;
1543 div = isl_vec_copy(aff->v);
1544 div = isl_vec_cow(div);
1545 if (!div)
1546 return isl_aff_free(aff);
1548 ctx = isl_aff_get_ctx(aff);
1549 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1550 for (i = 1; i < aff->v->size; ++i) {
1551 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1552 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1553 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1554 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1555 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1559 aff->ls = isl_local_space_add_div(aff->ls, div);
1560 if (!aff->ls)
1561 return isl_aff_free(aff);
1563 size = aff->v->size;
1564 aff->v = isl_vec_extend(aff->v, size + 1);
1565 if (!aff->v)
1566 return isl_aff_free(aff);
1567 isl_int_set_si(aff->v->el[0], 1);
1568 isl_int_set_si(aff->v->el[size], 1);
1570 aff = isl_aff_normalize(aff);
1572 return aff;
1575 /* Compute
1577 * aff mod m = aff - m * floor(aff/m)
1579 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1581 isl_aff *res;
1583 res = isl_aff_copy(aff);
1584 aff = isl_aff_scale_down(aff, m);
1585 aff = isl_aff_floor(aff);
1586 aff = isl_aff_scale(aff, m);
1587 res = isl_aff_sub(res, aff);
1589 return res;
1592 /* Compute
1594 * aff mod m = aff - m * floor(aff/m)
1596 * with m an integer value.
1598 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1599 __isl_take isl_val *m)
1601 isl_aff *res;
1603 if (!aff || !m)
1604 goto error;
1606 if (!isl_val_is_int(m))
1607 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1608 "expecting integer modulo", goto error);
1610 res = isl_aff_copy(aff);
1611 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1612 aff = isl_aff_floor(aff);
1613 aff = isl_aff_scale_val(aff, m);
1614 res = isl_aff_sub(res, aff);
1616 return res;
1617 error:
1618 isl_aff_free(aff);
1619 isl_val_free(m);
1620 return NULL;
1623 /* Compute
1625 * pwaff mod m = pwaff - m * floor(pwaff/m)
1627 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1629 isl_pw_aff *res;
1631 res = isl_pw_aff_copy(pwaff);
1632 pwaff = isl_pw_aff_scale_down(pwaff, m);
1633 pwaff = isl_pw_aff_floor(pwaff);
1634 pwaff = isl_pw_aff_scale(pwaff, m);
1635 res = isl_pw_aff_sub(res, pwaff);
1637 return res;
1640 /* Compute
1642 * pa mod m = pa - m * floor(pa/m)
1644 * with m an integer value.
1646 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1647 __isl_take isl_val *m)
1649 if (!pa || !m)
1650 goto error;
1651 if (!isl_val_is_int(m))
1652 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1653 "expecting integer modulo", goto error);
1654 pa = isl_pw_aff_mod(pa, m->n);
1655 isl_val_free(m);
1656 return pa;
1657 error:
1658 isl_pw_aff_free(pa);
1659 isl_val_free(m);
1660 return NULL;
1663 /* Given f, return ceil(f).
1664 * If f is an integer expression, then just return f.
1665 * Otherwise, let f be the expression
1667 * e/m
1669 * then return
1671 * floor((e + m - 1)/m)
1673 * As a special case, ceil(NaN) = NaN.
1675 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1677 if (!aff)
1678 return NULL;
1680 if (isl_aff_is_nan(aff))
1681 return aff;
1682 if (isl_int_is_one(aff->v->el[0]))
1683 return aff;
1685 aff = isl_aff_cow(aff);
1686 if (!aff)
1687 return NULL;
1688 aff->v = isl_vec_cow(aff->v);
1689 if (!aff->v)
1690 return isl_aff_free(aff);
1692 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1693 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1694 aff = isl_aff_floor(aff);
1696 return aff;
1699 /* Apply the expansion computed by isl_merge_divs.
1700 * The expansion itself is given by "exp" while the resulting
1701 * list of divs is given by "div".
1703 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1704 __isl_take isl_mat *div, int *exp)
1706 int i, j;
1707 int old_n_div;
1708 int new_n_div;
1709 int offset;
1711 aff = isl_aff_cow(aff);
1712 if (!aff || !div)
1713 goto error;
1715 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1716 new_n_div = isl_mat_rows(div);
1717 if (new_n_div < old_n_div)
1718 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1719 "not an expansion", goto error);
1721 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1722 if (!aff->v)
1723 goto error;
1725 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1726 j = old_n_div - 1;
1727 for (i = new_n_div - 1; i >= 0; --i) {
1728 if (j >= 0 && exp[j] == i) {
1729 if (i != j)
1730 isl_int_swap(aff->v->el[offset + i],
1731 aff->v->el[offset + j]);
1732 j--;
1733 } else
1734 isl_int_set_si(aff->v->el[offset + i], 0);
1737 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1738 if (!aff->ls)
1739 goto error;
1740 isl_mat_free(div);
1741 return aff;
1742 error:
1743 isl_aff_free(aff);
1744 isl_mat_free(div);
1745 return NULL;
1748 /* Add two affine expressions that live in the same local space.
1750 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1751 __isl_take isl_aff *aff2)
1753 isl_int gcd, f;
1755 aff1 = isl_aff_cow(aff1);
1756 if (!aff1 || !aff2)
1757 goto error;
1759 aff1->v = isl_vec_cow(aff1->v);
1760 if (!aff1->v)
1761 goto error;
1763 isl_int_init(gcd);
1764 isl_int_init(f);
1765 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1766 isl_int_divexact(f, aff2->v->el[0], gcd);
1767 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1768 isl_int_divexact(f, aff1->v->el[0], gcd);
1769 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1770 isl_int_divexact(f, aff2->v->el[0], gcd);
1771 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1772 isl_int_clear(f);
1773 isl_int_clear(gcd);
1775 isl_aff_free(aff2);
1776 return aff1;
1777 error:
1778 isl_aff_free(aff1);
1779 isl_aff_free(aff2);
1780 return NULL;
1783 /* Return the sum of "aff1" and "aff2".
1785 * If either of the two is NaN, then the result is NaN.
1787 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1788 __isl_take isl_aff *aff2)
1790 isl_ctx *ctx;
1791 int *exp1 = NULL;
1792 int *exp2 = NULL;
1793 isl_mat *div;
1794 int n_div1, n_div2;
1796 if (!aff1 || !aff2)
1797 goto error;
1799 ctx = isl_aff_get_ctx(aff1);
1800 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1801 isl_die(ctx, isl_error_invalid,
1802 "spaces don't match", goto error);
1804 if (isl_aff_is_nan(aff1)) {
1805 isl_aff_free(aff2);
1806 return aff1;
1808 if (isl_aff_is_nan(aff2)) {
1809 isl_aff_free(aff1);
1810 return aff2;
1813 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1814 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1815 if (n_div1 == 0 && n_div2 == 0)
1816 return add_expanded(aff1, aff2);
1818 exp1 = isl_alloc_array(ctx, int, n_div1);
1819 exp2 = isl_alloc_array(ctx, int, n_div2);
1820 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1821 goto error;
1823 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1824 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1825 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1826 free(exp1);
1827 free(exp2);
1829 return add_expanded(aff1, aff2);
1830 error:
1831 free(exp1);
1832 free(exp2);
1833 isl_aff_free(aff1);
1834 isl_aff_free(aff2);
1835 return NULL;
1838 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1839 __isl_take isl_aff *aff2)
1841 return isl_aff_add(aff1, isl_aff_neg(aff2));
1844 /* Return the result of scaling "aff" by a factor of "f".
1846 * As a special case, f * NaN = NaN.
1848 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1850 isl_int gcd;
1852 if (!aff)
1853 return NULL;
1854 if (isl_aff_is_nan(aff))
1855 return aff;
1857 if (isl_int_is_one(f))
1858 return aff;
1860 aff = isl_aff_cow(aff);
1861 if (!aff)
1862 return NULL;
1863 aff->v = isl_vec_cow(aff->v);
1864 if (!aff->v)
1865 return isl_aff_free(aff);
1867 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1868 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1869 return aff;
1872 isl_int_init(gcd);
1873 isl_int_gcd(gcd, aff->v->el[0], f);
1874 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1875 isl_int_divexact(gcd, f, gcd);
1876 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1877 isl_int_clear(gcd);
1879 return aff;
1882 /* Multiple "aff" by "v".
1884 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1885 __isl_take isl_val *v)
1887 if (!aff || !v)
1888 goto error;
1890 if (isl_val_is_one(v)) {
1891 isl_val_free(v);
1892 return aff;
1895 if (!isl_val_is_rat(v))
1896 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1897 "expecting rational factor", goto error);
1899 aff = isl_aff_scale(aff, v->n);
1900 aff = isl_aff_scale_down(aff, v->d);
1902 isl_val_free(v);
1903 return aff;
1904 error:
1905 isl_aff_free(aff);
1906 isl_val_free(v);
1907 return NULL;
1910 /* Return the result of scaling "aff" down by a factor of "f".
1912 * As a special case, NaN/f = NaN.
1914 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1916 isl_int gcd;
1918 if (!aff)
1919 return NULL;
1920 if (isl_aff_is_nan(aff))
1921 return aff;
1923 if (isl_int_is_one(f))
1924 return aff;
1926 aff = isl_aff_cow(aff);
1927 if (!aff)
1928 return NULL;
1930 if (isl_int_is_zero(f))
1931 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1932 "cannot scale down by zero", return isl_aff_free(aff));
1934 aff->v = isl_vec_cow(aff->v);
1935 if (!aff->v)
1936 return isl_aff_free(aff);
1938 isl_int_init(gcd);
1939 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1940 isl_int_gcd(gcd, gcd, f);
1941 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1942 isl_int_divexact(gcd, f, gcd);
1943 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1944 isl_int_clear(gcd);
1946 return aff;
1949 /* Divide "aff" by "v".
1951 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1952 __isl_take isl_val *v)
1954 if (!aff || !v)
1955 goto error;
1957 if (isl_val_is_one(v)) {
1958 isl_val_free(v);
1959 return aff;
1962 if (!isl_val_is_rat(v))
1963 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1964 "expecting rational factor", goto error);
1965 if (!isl_val_is_pos(v))
1966 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1967 "factor needs to be positive", goto error);
1969 aff = isl_aff_scale(aff, v->d);
1970 aff = isl_aff_scale_down(aff, v->n);
1972 isl_val_free(v);
1973 return aff;
1974 error:
1975 isl_aff_free(aff);
1976 isl_val_free(v);
1977 return NULL;
1980 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1982 isl_int v;
1984 if (f == 1)
1985 return aff;
1987 isl_int_init(v);
1988 isl_int_set_ui(v, f);
1989 aff = isl_aff_scale_down(aff, v);
1990 isl_int_clear(v);
1992 return aff;
1995 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1996 enum isl_dim_type type, unsigned pos, const char *s)
1998 aff = isl_aff_cow(aff);
1999 if (!aff)
2000 return NULL;
2001 if (type == isl_dim_out)
2002 isl_die(aff->v->ctx, isl_error_invalid,
2003 "cannot set name of output/set dimension",
2004 return isl_aff_free(aff));
2005 if (type == isl_dim_in)
2006 type = isl_dim_set;
2007 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2008 if (!aff->ls)
2009 return isl_aff_free(aff);
2011 return aff;
2014 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2015 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2017 aff = isl_aff_cow(aff);
2018 if (!aff)
2019 goto error;
2020 if (type == isl_dim_out)
2021 isl_die(aff->v->ctx, isl_error_invalid,
2022 "cannot set name of output/set dimension",
2023 goto error);
2024 if (type == isl_dim_in)
2025 type = isl_dim_set;
2026 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2027 if (!aff->ls)
2028 return isl_aff_free(aff);
2030 return aff;
2031 error:
2032 isl_id_free(id);
2033 isl_aff_free(aff);
2034 return NULL;
2037 /* Replace the identifier of the input tuple of "aff" by "id".
2038 * type is currently required to be equal to isl_dim_in
2040 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2041 enum isl_dim_type type, __isl_take isl_id *id)
2043 aff = isl_aff_cow(aff);
2044 if (!aff)
2045 goto error;
2046 if (type != isl_dim_out)
2047 isl_die(aff->v->ctx, isl_error_invalid,
2048 "cannot only set id of input tuple", goto error);
2049 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2050 if (!aff->ls)
2051 return isl_aff_free(aff);
2053 return aff;
2054 error:
2055 isl_id_free(id);
2056 isl_aff_free(aff);
2057 return NULL;
2060 /* Exploit the equalities in "eq" to simplify the affine expression
2061 * and the expressions of the integer divisions in the local space.
2062 * The integer divisions in this local space are assumed to appear
2063 * as regular dimensions in "eq".
2065 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2066 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2068 int i, j;
2069 unsigned total;
2070 unsigned n_div;
2072 if (!eq)
2073 goto error;
2074 if (eq->n_eq == 0) {
2075 isl_basic_set_free(eq);
2076 return aff;
2079 aff = isl_aff_cow(aff);
2080 if (!aff)
2081 goto error;
2083 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2084 isl_basic_set_copy(eq));
2085 aff->v = isl_vec_cow(aff->v);
2086 if (!aff->ls || !aff->v)
2087 goto error;
2089 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2090 n_div = eq->n_div;
2091 for (i = 0; i < eq->n_eq; ++i) {
2092 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2093 if (j < 0 || j == 0 || j >= total)
2094 continue;
2096 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2097 &aff->v->el[0]);
2100 isl_basic_set_free(eq);
2101 aff = isl_aff_normalize(aff);
2102 return aff;
2103 error:
2104 isl_basic_set_free(eq);
2105 isl_aff_free(aff);
2106 return NULL;
2109 /* Exploit the equalities in "eq" to simplify the affine expression
2110 * and the expressions of the integer divisions in the local space.
2112 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2113 __isl_take isl_basic_set *eq)
2115 int n_div;
2117 if (!aff || !eq)
2118 goto error;
2119 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2120 if (n_div > 0)
2121 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2122 return isl_aff_substitute_equalities_lifted(aff, eq);
2123 error:
2124 isl_basic_set_free(eq);
2125 isl_aff_free(aff);
2126 return NULL;
2129 /* Look for equalities among the variables shared by context and aff
2130 * and the integer divisions of aff, if any.
2131 * The equalities are then used to eliminate coefficients and/or integer
2132 * divisions from aff.
2134 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2135 __isl_take isl_set *context)
2137 isl_basic_set *hull;
2138 int n_div;
2140 if (!aff)
2141 goto error;
2142 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2143 if (n_div > 0) {
2144 isl_basic_set *bset;
2145 isl_local_space *ls;
2146 context = isl_set_add_dims(context, isl_dim_set, n_div);
2147 ls = isl_aff_get_domain_local_space(aff);
2148 bset = isl_basic_set_from_local_space(ls);
2149 bset = isl_basic_set_lift(bset);
2150 bset = isl_basic_set_flatten(bset);
2151 context = isl_set_intersect(context,
2152 isl_set_from_basic_set(bset));
2155 hull = isl_set_affine_hull(context);
2156 return isl_aff_substitute_equalities_lifted(aff, hull);
2157 error:
2158 isl_aff_free(aff);
2159 isl_set_free(context);
2160 return NULL;
2163 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2164 __isl_take isl_set *context)
2166 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2167 dom_context = isl_set_intersect_params(dom_context, context);
2168 return isl_aff_gist(aff, dom_context);
2171 /* Return a basic set containing those elements in the space
2172 * of aff where it is positive. "rational" should not be set.
2174 * If "aff" is NaN, then it is not positive.
2176 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2177 int rational)
2179 isl_constraint *ineq;
2180 isl_basic_set *bset;
2181 isl_val *c;
2183 if (!aff)
2184 return NULL;
2185 if (isl_aff_is_nan(aff)) {
2186 isl_space *space = isl_aff_get_domain_space(aff);
2187 isl_aff_free(aff);
2188 return isl_basic_set_empty(space);
2190 if (rational)
2191 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2192 "rational sets not supported", goto error);
2194 ineq = isl_inequality_from_aff(aff);
2195 c = isl_constraint_get_constant_val(ineq);
2196 c = isl_val_sub_ui(c, 1);
2197 ineq = isl_constraint_set_constant_val(ineq, c);
2199 bset = isl_basic_set_from_constraint(ineq);
2200 bset = isl_basic_set_simplify(bset);
2201 return bset;
2202 error:
2203 isl_aff_free(aff);
2204 return NULL;
2207 /* Return a basic set containing those elements in the space
2208 * of aff where it is non-negative.
2209 * If "rational" is set, then return a rational basic set.
2211 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2213 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2214 __isl_take isl_aff *aff, int rational)
2216 isl_constraint *ineq;
2217 isl_basic_set *bset;
2219 if (!aff)
2220 return NULL;
2221 if (isl_aff_is_nan(aff)) {
2222 isl_space *space = isl_aff_get_domain_space(aff);
2223 isl_aff_free(aff);
2224 return isl_basic_set_empty(space);
2227 ineq = isl_inequality_from_aff(aff);
2229 bset = isl_basic_set_from_constraint(ineq);
2230 if (rational)
2231 bset = isl_basic_set_set_rational(bset);
2232 bset = isl_basic_set_simplify(bset);
2233 return bset;
2236 /* Return a basic set containing those elements in the space
2237 * of aff where it is non-negative.
2239 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2241 return aff_nonneg_basic_set(aff, 0);
2244 /* Return a basic set containing those elements in the domain space
2245 * of aff where it is negative.
2247 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2249 aff = isl_aff_neg(aff);
2250 aff = isl_aff_add_constant_num_si(aff, -1);
2251 return isl_aff_nonneg_basic_set(aff);
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is zero.
2256 * If "rational" is set, then return a rational basic set.
2258 * If "aff" is NaN, then it is not zero.
2260 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2261 int rational)
2263 isl_constraint *ineq;
2264 isl_basic_set *bset;
2266 if (!aff)
2267 return NULL;
2268 if (isl_aff_is_nan(aff)) {
2269 isl_space *space = isl_aff_get_domain_space(aff);
2270 isl_aff_free(aff);
2271 return isl_basic_set_empty(space);
2274 ineq = isl_equality_from_aff(aff);
2276 bset = isl_basic_set_from_constraint(ineq);
2277 if (rational)
2278 bset = isl_basic_set_set_rational(bset);
2279 bset = isl_basic_set_simplify(bset);
2280 return bset;
2283 /* Return a basic set containing those elements in the space
2284 * of aff where it is zero.
2286 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2288 return aff_zero_basic_set(aff, 0);
2291 /* Return a basic set containing those elements in the shared space
2292 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2294 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2295 __isl_take isl_aff *aff2)
2297 aff1 = isl_aff_sub(aff1, aff2);
2299 return isl_aff_nonneg_basic_set(aff1);
2302 /* Return a basic set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2305 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2306 __isl_take isl_aff *aff2)
2308 return isl_aff_ge_basic_set(aff2, aff1);
2311 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2312 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2314 aff1 = isl_aff_add(aff1, aff2);
2315 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2316 return aff1;
2319 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2321 if (!aff)
2322 return -1;
2324 return 0;
2327 /* Check whether the given affine expression has non-zero coefficient
2328 * for any dimension in the given range or if any of these dimensions
2329 * appear with non-zero coefficients in any of the integer divisions
2330 * involved in the affine expression.
2332 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2333 enum isl_dim_type type, unsigned first, unsigned n)
2335 int i;
2336 isl_ctx *ctx;
2337 int *active = NULL;
2338 isl_bool involves = isl_bool_false;
2340 if (!aff)
2341 return isl_bool_error;
2342 if (n == 0)
2343 return isl_bool_false;
2345 ctx = isl_aff_get_ctx(aff);
2346 if (first + n > isl_aff_dim(aff, type))
2347 isl_die(ctx, isl_error_invalid,
2348 "range out of bounds", return isl_bool_error);
2350 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2351 if (!active)
2352 goto error;
2354 first += isl_local_space_offset(aff->ls, type) - 1;
2355 for (i = 0; i < n; ++i)
2356 if (active[first + i]) {
2357 involves = isl_bool_true;
2358 break;
2361 free(active);
2363 return involves;
2364 error:
2365 free(active);
2366 return isl_bool_error;
2369 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2370 enum isl_dim_type type, unsigned first, unsigned n)
2372 isl_ctx *ctx;
2374 if (!aff)
2375 return NULL;
2376 if (type == isl_dim_out)
2377 isl_die(aff->v->ctx, isl_error_invalid,
2378 "cannot drop output/set dimension",
2379 return isl_aff_free(aff));
2380 if (type == isl_dim_in)
2381 type = isl_dim_set;
2382 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2383 return aff;
2385 ctx = isl_aff_get_ctx(aff);
2386 if (first + n > isl_local_space_dim(aff->ls, type))
2387 isl_die(ctx, isl_error_invalid, "range out of bounds",
2388 return isl_aff_free(aff));
2390 aff = isl_aff_cow(aff);
2391 if (!aff)
2392 return NULL;
2394 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2395 if (!aff->ls)
2396 return isl_aff_free(aff);
2398 first += 1 + isl_local_space_offset(aff->ls, type);
2399 aff->v = isl_vec_drop_els(aff->v, first, n);
2400 if (!aff->v)
2401 return isl_aff_free(aff);
2403 return aff;
2406 /* Project the domain of the affine expression onto its parameter space.
2407 * The affine expression may not involve any of the domain dimensions.
2409 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2411 isl_space *space;
2412 unsigned n;
2413 int involves;
2415 n = isl_aff_dim(aff, isl_dim_in);
2416 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2417 if (involves < 0)
2418 return isl_aff_free(aff);
2419 if (involves)
2420 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2421 "affine expression involves some of the domain dimensions",
2422 return isl_aff_free(aff));
2423 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2424 space = isl_aff_get_domain_space(aff);
2425 space = isl_space_params(space);
2426 aff = isl_aff_reset_domain_space(aff, space);
2427 return aff;
2430 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2431 enum isl_dim_type type, unsigned first, unsigned n)
2433 isl_ctx *ctx;
2435 if (!aff)
2436 return NULL;
2437 if (type == isl_dim_out)
2438 isl_die(aff->v->ctx, isl_error_invalid,
2439 "cannot insert output/set dimensions",
2440 return isl_aff_free(aff));
2441 if (type == isl_dim_in)
2442 type = isl_dim_set;
2443 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2444 return aff;
2446 ctx = isl_aff_get_ctx(aff);
2447 if (first > isl_local_space_dim(aff->ls, type))
2448 isl_die(ctx, isl_error_invalid, "position out of bounds",
2449 return isl_aff_free(aff));
2451 aff = isl_aff_cow(aff);
2452 if (!aff)
2453 return NULL;
2455 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2456 if (!aff->ls)
2457 return isl_aff_free(aff);
2459 first += 1 + isl_local_space_offset(aff->ls, type);
2460 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2461 if (!aff->v)
2462 return isl_aff_free(aff);
2464 return aff;
2467 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2468 enum isl_dim_type type, unsigned n)
2470 unsigned pos;
2472 pos = isl_aff_dim(aff, type);
2474 return isl_aff_insert_dims(aff, type, pos, n);
2477 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2478 enum isl_dim_type type, unsigned n)
2480 unsigned pos;
2482 pos = isl_pw_aff_dim(pwaff, type);
2484 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2487 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2488 * to dimensions of "dst_type" at "dst_pos".
2490 * We only support moving input dimensions to parameters and vice versa.
2492 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2493 enum isl_dim_type dst_type, unsigned dst_pos,
2494 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2496 unsigned g_dst_pos;
2497 unsigned g_src_pos;
2499 if (!aff)
2500 return NULL;
2501 if (n == 0 &&
2502 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2503 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2504 return aff;
2506 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2507 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2508 "cannot move output/set dimension",
2509 return isl_aff_free(aff));
2510 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2511 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2512 "cannot move divs", return isl_aff_free(aff));
2513 if (dst_type == isl_dim_in)
2514 dst_type = isl_dim_set;
2515 if (src_type == isl_dim_in)
2516 src_type = isl_dim_set;
2518 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2519 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2520 "range out of bounds", return isl_aff_free(aff));
2521 if (dst_type == src_type)
2522 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2523 "moving dims within the same type not supported",
2524 return isl_aff_free(aff));
2526 aff = isl_aff_cow(aff);
2527 if (!aff)
2528 return NULL;
2530 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2531 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2532 if (dst_type > src_type)
2533 g_dst_pos -= n;
2535 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2536 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2537 src_type, src_pos, n);
2538 if (!aff->v || !aff->ls)
2539 return isl_aff_free(aff);
2541 aff = sort_divs(aff);
2543 return aff;
2546 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2548 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2549 return isl_pw_aff_alloc(dom, aff);
2552 #undef PW
2553 #define PW isl_pw_aff
2554 #undef EL
2555 #define EL isl_aff
2556 #undef EL_IS_ZERO
2557 #define EL_IS_ZERO is_empty
2558 #undef ZERO
2559 #define ZERO empty
2560 #undef IS_ZERO
2561 #define IS_ZERO is_empty
2562 #undef FIELD
2563 #define FIELD aff
2564 #undef DEFAULT_IS_ZERO
2565 #define DEFAULT_IS_ZERO 0
2567 #define NO_EVAL
2568 #define NO_OPT
2569 #define NO_LIFT
2570 #define NO_MORPH
2572 #include <isl_pw_templ.c>
2574 #undef UNION
2575 #define UNION isl_union_pw_aff
2576 #undef PART
2577 #define PART isl_pw_aff
2578 #undef PARTS
2579 #define PARTS pw_aff
2581 #include <isl_union_single.c>
2582 #include <isl_union_neg.c>
2584 static __isl_give isl_set *align_params_pw_pw_set_and(
2585 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2586 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2587 __isl_take isl_pw_aff *pwaff2))
2589 if (!pwaff1 || !pwaff2)
2590 goto error;
2591 if (isl_space_match(pwaff1->dim, isl_dim_param,
2592 pwaff2->dim, isl_dim_param))
2593 return fn(pwaff1, pwaff2);
2594 if (!isl_space_has_named_params(pwaff1->dim) ||
2595 !isl_space_has_named_params(pwaff2->dim))
2596 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2597 "unaligned unnamed parameters", goto error);
2598 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2599 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2600 return fn(pwaff1, pwaff2);
2601 error:
2602 isl_pw_aff_free(pwaff1);
2603 isl_pw_aff_free(pwaff2);
2604 return NULL;
2607 /* Align the parameters of the to isl_pw_aff arguments and
2608 * then apply a function "fn" on them that returns an isl_map.
2610 static __isl_give isl_map *align_params_pw_pw_map_and(
2611 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2612 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2613 __isl_take isl_pw_aff *pa2))
2615 if (!pa1 || !pa2)
2616 goto error;
2617 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2618 return fn(pa1, pa2);
2619 if (!isl_space_has_named_params(pa1->dim) ||
2620 !isl_space_has_named_params(pa2->dim))
2621 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2622 "unaligned unnamed parameters", goto error);
2623 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2624 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2625 return fn(pa1, pa2);
2626 error:
2627 isl_pw_aff_free(pa1);
2628 isl_pw_aff_free(pa2);
2629 return NULL;
2632 /* Compute a piecewise quasi-affine expression with a domain that
2633 * is the union of those of pwaff1 and pwaff2 and such that on each
2634 * cell, the quasi-affine expression is the better (according to cmp)
2635 * of those of pwaff1 and pwaff2. If only one of pwaff1 or pwaff2
2636 * is defined on a given cell, then the associated expression
2637 * is the defined one.
2639 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2640 __isl_take isl_pw_aff *pwaff2,
2641 __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
2642 __isl_take isl_aff *aff2))
2644 int i, j, n;
2645 isl_pw_aff *res;
2646 isl_ctx *ctx;
2647 isl_set *set;
2649 if (!pwaff1 || !pwaff2)
2650 goto error;
2652 ctx = isl_space_get_ctx(pwaff1->dim);
2653 if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
2654 isl_die(ctx, isl_error_invalid,
2655 "arguments should live in same space", goto error);
2657 if (isl_pw_aff_is_empty(pwaff1)) {
2658 isl_pw_aff_free(pwaff1);
2659 return pwaff2;
2662 if (isl_pw_aff_is_empty(pwaff2)) {
2663 isl_pw_aff_free(pwaff2);
2664 return pwaff1;
2667 n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
2668 res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
2670 for (i = 0; i < pwaff1->n; ++i) {
2671 set = isl_set_copy(pwaff1->p[i].set);
2672 for (j = 0; j < pwaff2->n; ++j) {
2673 struct isl_set *common;
2674 isl_set *better;
2676 common = isl_set_intersect(
2677 isl_set_copy(pwaff1->p[i].set),
2678 isl_set_copy(pwaff2->p[j].set));
2679 better = isl_set_from_basic_set(cmp(
2680 isl_aff_copy(pwaff2->p[j].aff),
2681 isl_aff_copy(pwaff1->p[i].aff)));
2682 better = isl_set_intersect(common, better);
2683 if (isl_set_plain_is_empty(better)) {
2684 isl_set_free(better);
2685 continue;
2687 set = isl_set_subtract(set, isl_set_copy(better));
2689 res = isl_pw_aff_add_piece(res, better,
2690 isl_aff_copy(pwaff2->p[j].aff));
2692 res = isl_pw_aff_add_piece(res, set,
2693 isl_aff_copy(pwaff1->p[i].aff));
2696 for (j = 0; j < pwaff2->n; ++j) {
2697 set = isl_set_copy(pwaff2->p[j].set);
2698 for (i = 0; i < pwaff1->n; ++i)
2699 set = isl_set_subtract(set,
2700 isl_set_copy(pwaff1->p[i].set));
2701 res = isl_pw_aff_add_piece(res, set,
2702 isl_aff_copy(pwaff2->p[j].aff));
2705 isl_pw_aff_free(pwaff1);
2706 isl_pw_aff_free(pwaff2);
2708 return res;
2709 error:
2710 isl_pw_aff_free(pwaff1);
2711 isl_pw_aff_free(pwaff2);
2712 return NULL;
2715 /* Compute a piecewise quasi-affine expression with a domain that
2716 * is the union of those of pwaff1 and pwaff2 and such that on each
2717 * cell, the quasi-affine expression is the maximum of those of pwaff1
2718 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2719 * cell, then the associated expression is the defined one.
2721 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2722 __isl_take isl_pw_aff *pwaff2)
2724 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
2727 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2728 __isl_take isl_pw_aff *pwaff2)
2730 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2731 &pw_aff_union_max);
2734 /* Compute a piecewise quasi-affine expression with a domain that
2735 * is the union of those of pwaff1 and pwaff2 and such that on each
2736 * cell, the quasi-affine expression is the minimum of those of pwaff1
2737 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2738 * cell, then the associated expression is the defined one.
2740 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2741 __isl_take isl_pw_aff *pwaff2)
2743 return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
2746 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2747 __isl_take isl_pw_aff *pwaff2)
2749 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2750 &pw_aff_union_min);
2753 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2754 __isl_take isl_pw_aff *pwaff2, int max)
2756 if (max)
2757 return isl_pw_aff_union_max(pwaff1, pwaff2);
2758 else
2759 return isl_pw_aff_union_min(pwaff1, pwaff2);
2762 /* Construct a map with as domain the domain of pwaff and
2763 * one-dimensional range corresponding to the affine expressions.
2765 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2767 int i;
2768 isl_space *dim;
2769 isl_map *map;
2771 if (!pwaff)
2772 return NULL;
2774 dim = isl_pw_aff_get_space(pwaff);
2775 map = isl_map_empty(dim);
2777 for (i = 0; i < pwaff->n; ++i) {
2778 isl_basic_map *bmap;
2779 isl_map *map_i;
2781 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2782 map_i = isl_map_from_basic_map(bmap);
2783 map_i = isl_map_intersect_domain(map_i,
2784 isl_set_copy(pwaff->p[i].set));
2785 map = isl_map_union_disjoint(map, map_i);
2788 isl_pw_aff_free(pwaff);
2790 return map;
2793 /* Construct a map with as domain the domain of pwaff and
2794 * one-dimensional range corresponding to the affine expressions.
2796 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2798 if (!pwaff)
2799 return NULL;
2800 if (isl_space_is_set(pwaff->dim))
2801 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2802 "space of input is not a map", goto error);
2803 return map_from_pw_aff(pwaff);
2804 error:
2805 isl_pw_aff_free(pwaff);
2806 return NULL;
2809 /* Construct a one-dimensional set with as parameter domain
2810 * the domain of pwaff and the single set dimension
2811 * corresponding to the affine expressions.
2813 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2815 if (!pwaff)
2816 return NULL;
2817 if (!isl_space_is_set(pwaff->dim))
2818 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2819 "space of input is not a set", goto error);
2820 return map_from_pw_aff(pwaff);
2821 error:
2822 isl_pw_aff_free(pwaff);
2823 return NULL;
2826 /* Return a set containing those elements in the domain
2827 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2828 * does not satisfy "fn" (if complement is 1).
2830 * The pieces with a NaN never belong to the result since
2831 * NaN does not satisfy any property.
2833 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2834 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2835 int complement)
2837 int i;
2838 isl_set *set;
2840 if (!pwaff)
2841 return NULL;
2843 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2845 for (i = 0; i < pwaff->n; ++i) {
2846 isl_basic_set *bset;
2847 isl_set *set_i, *locus;
2848 int rational;
2850 if (isl_aff_is_nan(pwaff->p[i].aff))
2851 continue;
2853 rational = isl_set_has_rational(pwaff->p[i].set);
2854 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2855 locus = isl_set_from_basic_set(bset);
2856 set_i = isl_set_copy(pwaff->p[i].set);
2857 if (complement)
2858 set_i = isl_set_subtract(set_i, locus);
2859 else
2860 set_i = isl_set_intersect(set_i, locus);
2861 set = isl_set_union_disjoint(set, set_i);
2864 isl_pw_aff_free(pwaff);
2866 return set;
2869 /* Return a set containing those elements in the domain
2870 * of "pa" where it is positive.
2872 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2874 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2877 /* Return a set containing those elements in the domain
2878 * of pwaff where it is non-negative.
2880 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2882 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2885 /* Return a set containing those elements in the domain
2886 * of pwaff where it is zero.
2888 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2890 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2893 /* Return a set containing those elements in the domain
2894 * of pwaff where it is not zero.
2896 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2898 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2901 /* Return a set containing those elements in the shared domain
2902 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2904 * We compute the difference on the shared domain and then construct
2905 * the set of values where this difference is non-negative.
2906 * If strict is set, we first subtract 1 from the difference.
2907 * If equal is set, we only return the elements where pwaff1 and pwaff2
2908 * are equal.
2910 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2911 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2913 isl_set *set1, *set2;
2915 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2916 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2917 set1 = isl_set_intersect(set1, set2);
2918 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2919 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2920 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2922 if (strict) {
2923 isl_space *dim = isl_set_get_space(set1);
2924 isl_aff *aff;
2925 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2926 aff = isl_aff_add_constant_si(aff, -1);
2927 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2928 } else
2929 isl_set_free(set1);
2931 if (equal)
2932 return isl_pw_aff_zero_set(pwaff1);
2933 return isl_pw_aff_nonneg_set(pwaff1);
2936 /* Return a set containing those elements in the shared domain
2937 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2939 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2940 __isl_take isl_pw_aff *pwaff2)
2942 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2945 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2946 __isl_take isl_pw_aff *pwaff2)
2948 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2951 /* Return a set containing those elements in the shared domain
2952 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2954 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2955 __isl_take isl_pw_aff *pwaff2)
2957 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2960 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2961 __isl_take isl_pw_aff *pwaff2)
2963 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2966 /* Return a set containing those elements in the shared domain
2967 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2969 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2970 __isl_take isl_pw_aff *pwaff2)
2972 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2975 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2976 __isl_take isl_pw_aff *pwaff2)
2978 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2981 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2982 __isl_take isl_pw_aff *pwaff2)
2984 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2987 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2988 __isl_take isl_pw_aff *pwaff2)
2990 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2993 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2994 * where the function values are ordered in the same way as "order",
2995 * which returns a set in the shared domain of its two arguments.
2996 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2998 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2999 * We first pull back the two functions such that they are defined on
3000 * the domain [A -> B]. Then we apply "order", resulting in a set
3001 * in the space [A -> B]. Finally, we unwrap this set to obtain
3002 * a map in the space A -> B.
3004 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3005 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3006 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3007 __isl_take isl_pw_aff *pa2))
3009 isl_space *space1, *space2;
3010 isl_multi_aff *ma;
3011 isl_set *set;
3013 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3014 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3015 space1 = isl_space_map_from_domain_and_range(space1, space2);
3016 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3017 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3018 ma = isl_multi_aff_range_map(space1);
3019 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3020 set = order(pa1, pa2);
3022 return isl_set_unwrap(set);
3025 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3026 * where the function values are equal.
3027 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3029 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3030 __isl_take isl_pw_aff *pa2)
3032 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3035 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3036 * where the function values are equal.
3038 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3039 __isl_take isl_pw_aff *pa2)
3041 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3044 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3045 * where the function value of "pa1" is less than the function value of "pa2".
3046 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3048 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3049 __isl_take isl_pw_aff *pa2)
3051 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3054 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3055 * where the function value of "pa1" is less than the function value of "pa2".
3057 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3058 __isl_take isl_pw_aff *pa2)
3060 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3063 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3064 * where the function value of "pa1" is greater than the function value
3065 * of "pa2".
3066 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3068 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3069 __isl_take isl_pw_aff *pa2)
3071 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3074 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3075 * where the function value of "pa1" is greater than the function value
3076 * of "pa2".
3078 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3079 __isl_take isl_pw_aff *pa2)
3081 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3084 /* Return a set containing those elements in the shared domain
3085 * of the elements of list1 and list2 where each element in list1
3086 * has the relation specified by "fn" with each element in list2.
3088 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3089 __isl_take isl_pw_aff_list *list2,
3090 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3091 __isl_take isl_pw_aff *pwaff2))
3093 int i, j;
3094 isl_ctx *ctx;
3095 isl_set *set;
3097 if (!list1 || !list2)
3098 goto error;
3100 ctx = isl_pw_aff_list_get_ctx(list1);
3101 if (list1->n < 1 || list2->n < 1)
3102 isl_die(ctx, isl_error_invalid,
3103 "list should contain at least one element", goto error);
3105 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3106 for (i = 0; i < list1->n; ++i)
3107 for (j = 0; j < list2->n; ++j) {
3108 isl_set *set_ij;
3110 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3111 isl_pw_aff_copy(list2->p[j]));
3112 set = isl_set_intersect(set, set_ij);
3115 isl_pw_aff_list_free(list1);
3116 isl_pw_aff_list_free(list2);
3117 return set;
3118 error:
3119 isl_pw_aff_list_free(list1);
3120 isl_pw_aff_list_free(list2);
3121 return NULL;
3124 /* Return a set containing those elements in the shared domain
3125 * of the elements of list1 and list2 where each element in list1
3126 * is equal to each element in list2.
3128 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3129 __isl_take isl_pw_aff_list *list2)
3131 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3134 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3135 __isl_take isl_pw_aff_list *list2)
3137 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3140 /* Return a set containing those elements in the shared domain
3141 * of the elements of list1 and list2 where each element in list1
3142 * is less than or equal to each element in list2.
3144 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3145 __isl_take isl_pw_aff_list *list2)
3147 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3150 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3151 __isl_take isl_pw_aff_list *list2)
3153 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3156 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3157 __isl_take isl_pw_aff_list *list2)
3159 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3162 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3163 __isl_take isl_pw_aff_list *list2)
3165 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3169 /* Return a set containing those elements in the shared domain
3170 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3172 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3173 __isl_take isl_pw_aff *pwaff2)
3175 isl_set *set_lt, *set_gt;
3177 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3178 isl_pw_aff_copy(pwaff2));
3179 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3180 return isl_set_union_disjoint(set_lt, set_gt);
3183 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3184 __isl_take isl_pw_aff *pwaff2)
3186 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3189 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3190 isl_int v)
3192 int i;
3194 if (isl_int_is_one(v))
3195 return pwaff;
3196 if (!isl_int_is_pos(v))
3197 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3198 "factor needs to be positive",
3199 return isl_pw_aff_free(pwaff));
3200 pwaff = isl_pw_aff_cow(pwaff);
3201 if (!pwaff)
3202 return NULL;
3203 if (pwaff->n == 0)
3204 return pwaff;
3206 for (i = 0; i < pwaff->n; ++i) {
3207 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3208 if (!pwaff->p[i].aff)
3209 return isl_pw_aff_free(pwaff);
3212 return pwaff;
3215 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3217 int i;
3219 pwaff = isl_pw_aff_cow(pwaff);
3220 if (!pwaff)
3221 return NULL;
3222 if (pwaff->n == 0)
3223 return pwaff;
3225 for (i = 0; i < pwaff->n; ++i) {
3226 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3227 if (!pwaff->p[i].aff)
3228 return isl_pw_aff_free(pwaff);
3231 return pwaff;
3234 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3236 int i;
3238 pwaff = isl_pw_aff_cow(pwaff);
3239 if (!pwaff)
3240 return NULL;
3241 if (pwaff->n == 0)
3242 return pwaff;
3244 for (i = 0; i < pwaff->n; ++i) {
3245 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3246 if (!pwaff->p[i].aff)
3247 return isl_pw_aff_free(pwaff);
3250 return pwaff;
3253 /* Assuming that "cond1" and "cond2" are disjoint,
3254 * return an affine expression that is equal to pwaff1 on cond1
3255 * and to pwaff2 on cond2.
3257 static __isl_give isl_pw_aff *isl_pw_aff_select(
3258 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3259 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3261 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3262 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3264 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3267 /* Return an affine expression that is equal to pwaff_true for elements
3268 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3269 * is zero.
3270 * That is, return cond ? pwaff_true : pwaff_false;
3272 * If "cond" involves and NaN, then we conservatively return a NaN
3273 * on its entire domain. In principle, we could consider the pieces
3274 * where it is NaN separately from those where it is not.
3276 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3277 * then only use the domain of "cond" to restrict the domain.
3279 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3280 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3282 isl_set *cond_true, *cond_false;
3283 isl_bool equal;
3285 if (!cond)
3286 goto error;
3287 if (isl_pw_aff_involves_nan(cond)) {
3288 isl_space *space = isl_pw_aff_get_domain_space(cond);
3289 isl_local_space *ls = isl_local_space_from_space(space);
3290 isl_pw_aff_free(cond);
3291 isl_pw_aff_free(pwaff_true);
3292 isl_pw_aff_free(pwaff_false);
3293 return isl_pw_aff_nan_on_domain(ls);
3296 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3297 isl_pw_aff_get_space(pwaff_false));
3298 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3299 isl_pw_aff_get_space(pwaff_true));
3300 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3301 if (equal < 0)
3302 goto error;
3303 if (equal) {
3304 isl_set *dom;
3306 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3307 isl_pw_aff_free(pwaff_false);
3308 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3311 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3312 cond_false = isl_pw_aff_zero_set(cond);
3313 return isl_pw_aff_select(cond_true, pwaff_true,
3314 cond_false, pwaff_false);
3315 error:
3316 isl_pw_aff_free(cond);
3317 isl_pw_aff_free(pwaff_true);
3318 isl_pw_aff_free(pwaff_false);
3319 return NULL;
3322 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3324 if (!aff)
3325 return isl_bool_error;
3327 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3330 /* Check whether pwaff is a piecewise constant.
3332 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3334 int i;
3336 if (!pwaff)
3337 return isl_bool_error;
3339 for (i = 0; i < pwaff->n; ++i) {
3340 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3341 if (is_cst < 0 || !is_cst)
3342 return is_cst;
3345 return isl_bool_true;
3348 /* Are all elements of "mpa" piecewise constants?
3350 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3352 int i;
3354 if (!mpa)
3355 return isl_bool_error;
3357 for (i = 0; i < mpa->n; ++i) {
3358 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3359 if (is_cst < 0 || !is_cst)
3360 return is_cst;
3363 return isl_bool_true;
3366 /* Return the product of "aff1" and "aff2".
3368 * If either of the two is NaN, then the result is NaN.
3370 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3372 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3373 __isl_take isl_aff *aff2)
3375 if (!aff1 || !aff2)
3376 goto error;
3378 if (isl_aff_is_nan(aff1)) {
3379 isl_aff_free(aff2);
3380 return aff1;
3382 if (isl_aff_is_nan(aff2)) {
3383 isl_aff_free(aff1);
3384 return aff2;
3387 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3388 return isl_aff_mul(aff2, aff1);
3390 if (!isl_aff_is_cst(aff2))
3391 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3392 "at least one affine expression should be constant",
3393 goto error);
3395 aff1 = isl_aff_cow(aff1);
3396 if (!aff1 || !aff2)
3397 goto error;
3399 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3400 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3402 isl_aff_free(aff2);
3403 return aff1;
3404 error:
3405 isl_aff_free(aff1);
3406 isl_aff_free(aff2);
3407 return NULL;
3410 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3412 * If either of the two is NaN, then the result is NaN.
3414 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3415 __isl_take isl_aff *aff2)
3417 int is_cst;
3418 int neg;
3420 if (!aff1 || !aff2)
3421 goto error;
3423 if (isl_aff_is_nan(aff1)) {
3424 isl_aff_free(aff2);
3425 return aff1;
3427 if (isl_aff_is_nan(aff2)) {
3428 isl_aff_free(aff1);
3429 return aff2;
3432 is_cst = isl_aff_is_cst(aff2);
3433 if (is_cst < 0)
3434 goto error;
3435 if (!is_cst)
3436 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3437 "second argument should be a constant", goto error);
3439 if (!aff2)
3440 goto error;
3442 neg = isl_int_is_neg(aff2->v->el[1]);
3443 if (neg) {
3444 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3445 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3448 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3449 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3451 if (neg) {
3452 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3453 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3456 isl_aff_free(aff2);
3457 return aff1;
3458 error:
3459 isl_aff_free(aff1);
3460 isl_aff_free(aff2);
3461 return NULL;
3464 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3465 __isl_take isl_pw_aff *pwaff2)
3467 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3470 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3471 __isl_take isl_pw_aff *pwaff2)
3473 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3476 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3477 __isl_take isl_pw_aff *pwaff2)
3479 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3482 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3483 __isl_take isl_pw_aff *pwaff2)
3485 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3488 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3489 __isl_take isl_pw_aff *pwaff2)
3491 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3494 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3495 __isl_take isl_pw_aff *pa2)
3497 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3500 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3502 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3503 __isl_take isl_pw_aff *pa2)
3505 int is_cst;
3507 is_cst = isl_pw_aff_is_cst(pa2);
3508 if (is_cst < 0)
3509 goto error;
3510 if (!is_cst)
3511 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3512 "second argument should be a piecewise constant",
3513 goto error);
3514 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3515 error:
3516 isl_pw_aff_free(pa1);
3517 isl_pw_aff_free(pa2);
3518 return NULL;
3521 /* Compute the quotient 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 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3530 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3531 __isl_take isl_pw_aff *pa2)
3533 int is_cst;
3534 isl_set *cond;
3535 isl_pw_aff *f, *c;
3537 is_cst = isl_pw_aff_is_cst(pa2);
3538 if (is_cst < 0)
3539 goto error;
3540 if (!is_cst)
3541 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3542 "second argument should be a piecewise constant",
3543 goto error);
3545 pa1 = isl_pw_aff_div(pa1, pa2);
3547 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3548 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3549 c = isl_pw_aff_ceil(pa1);
3550 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3551 error:
3552 isl_pw_aff_free(pa1);
3553 isl_pw_aff_free(pa2);
3554 return NULL;
3557 /* Compute the remainder of the integer division of "pa1" by "pa2"
3558 * with rounding towards zero.
3559 * "pa2" is assumed to be a piecewise constant.
3561 * In particular, return
3563 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3566 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3567 __isl_take isl_pw_aff *pa2)
3569 int is_cst;
3570 isl_pw_aff *res;
3572 is_cst = isl_pw_aff_is_cst(pa2);
3573 if (is_cst < 0)
3574 goto error;
3575 if (!is_cst)
3576 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3577 "second argument should be a piecewise constant",
3578 goto error);
3579 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3580 res = isl_pw_aff_mul(pa2, res);
3581 res = isl_pw_aff_sub(pa1, res);
3582 return res;
3583 error:
3584 isl_pw_aff_free(pa1);
3585 isl_pw_aff_free(pa2);
3586 return NULL;
3589 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3590 __isl_take isl_pw_aff *pwaff2)
3592 isl_set *le;
3593 isl_set *dom;
3595 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3596 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3597 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3598 isl_pw_aff_copy(pwaff2));
3599 dom = isl_set_subtract(dom, isl_set_copy(le));
3600 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3603 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3604 __isl_take isl_pw_aff *pwaff2)
3606 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3609 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3610 __isl_take isl_pw_aff *pwaff2)
3612 isl_set *ge;
3613 isl_set *dom;
3615 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3616 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3617 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3618 isl_pw_aff_copy(pwaff2));
3619 dom = isl_set_subtract(dom, isl_set_copy(ge));
3620 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3623 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3624 __isl_take isl_pw_aff *pwaff2)
3626 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3629 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3630 __isl_take isl_pw_aff_list *list,
3631 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3632 __isl_take isl_pw_aff *pwaff2))
3634 int i;
3635 isl_ctx *ctx;
3636 isl_pw_aff *res;
3638 if (!list)
3639 return NULL;
3641 ctx = isl_pw_aff_list_get_ctx(list);
3642 if (list->n < 1)
3643 isl_die(ctx, isl_error_invalid,
3644 "list should contain at least one element", goto error);
3646 res = isl_pw_aff_copy(list->p[0]);
3647 for (i = 1; i < list->n; ++i)
3648 res = fn(res, isl_pw_aff_copy(list->p[i]));
3650 isl_pw_aff_list_free(list);
3651 return res;
3652 error:
3653 isl_pw_aff_list_free(list);
3654 return NULL;
3657 /* Return an isl_pw_aff that maps each element in the intersection of the
3658 * domains of the elements of list to the minimal corresponding affine
3659 * expression.
3661 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3663 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3666 /* Return an isl_pw_aff that maps each element in the intersection of the
3667 * domains of the elements of list to the maximal corresponding affine
3668 * expression.
3670 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3672 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3675 /* Mark the domains of "pwaff" as rational.
3677 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3679 int i;
3681 pwaff = isl_pw_aff_cow(pwaff);
3682 if (!pwaff)
3683 return NULL;
3684 if (pwaff->n == 0)
3685 return pwaff;
3687 for (i = 0; i < pwaff->n; ++i) {
3688 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3689 if (!pwaff->p[i].set)
3690 return isl_pw_aff_free(pwaff);
3693 return pwaff;
3696 /* Mark the domains of the elements of "list" as rational.
3698 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3699 __isl_take isl_pw_aff_list *list)
3701 int i, n;
3703 if (!list)
3704 return NULL;
3705 if (list->n == 0)
3706 return list;
3708 n = list->n;
3709 for (i = 0; i < n; ++i) {
3710 isl_pw_aff *pa;
3712 pa = isl_pw_aff_list_get_pw_aff(list, i);
3713 pa = isl_pw_aff_set_rational(pa);
3714 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3717 return list;
3720 /* Do the parameters of "aff" match those of "space"?
3722 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3723 __isl_keep isl_space *space)
3725 isl_space *aff_space;
3726 int match;
3728 if (!aff || !space)
3729 return -1;
3731 aff_space = isl_aff_get_domain_space(aff);
3733 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3735 isl_space_free(aff_space);
3736 return match;
3739 /* Check that the domain space of "aff" matches "space".
3741 * Return 0 on success and -1 on error.
3743 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3744 __isl_keep isl_space *space)
3746 isl_space *aff_space;
3747 int match;
3749 if (!aff || !space)
3750 return -1;
3752 aff_space = isl_aff_get_domain_space(aff);
3754 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3755 if (match < 0)
3756 goto error;
3757 if (!match)
3758 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3759 "parameters don't match", goto error);
3760 match = isl_space_tuple_is_equal(space, isl_dim_in,
3761 aff_space, isl_dim_set);
3762 if (match < 0)
3763 goto error;
3764 if (!match)
3765 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3766 "domains don't match", goto error);
3767 isl_space_free(aff_space);
3768 return 0;
3769 error:
3770 isl_space_free(aff_space);
3771 return -1;
3774 #undef BASE
3775 #define BASE aff
3776 #undef DOMBASE
3777 #define DOMBASE set
3778 #define NO_DOMAIN
3780 #include <isl_multi_templ.c>
3781 #include <isl_multi_apply_set.c>
3782 #include <isl_multi_floor.c>
3783 #include <isl_multi_gist.c>
3785 #undef NO_DOMAIN
3787 /* Remove any internal structure of the domain of "ma".
3788 * If there is any such internal structure in the input,
3789 * then the name of the corresponding space is also removed.
3791 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3792 __isl_take isl_multi_aff *ma)
3794 isl_space *space;
3796 if (!ma)
3797 return NULL;
3799 if (!ma->space->nested[0])
3800 return ma;
3802 space = isl_multi_aff_get_space(ma);
3803 space = isl_space_flatten_domain(space);
3804 ma = isl_multi_aff_reset_space(ma, space);
3806 return ma;
3809 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3810 * of the space to its domain.
3812 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3814 int i, n_in;
3815 isl_local_space *ls;
3816 isl_multi_aff *ma;
3818 if (!space)
3819 return NULL;
3820 if (!isl_space_is_map(space))
3821 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3822 "not a map space", goto error);
3824 n_in = isl_space_dim(space, isl_dim_in);
3825 space = isl_space_domain_map(space);
3827 ma = isl_multi_aff_alloc(isl_space_copy(space));
3828 if (n_in == 0) {
3829 isl_space_free(space);
3830 return ma;
3833 space = isl_space_domain(space);
3834 ls = isl_local_space_from_space(space);
3835 for (i = 0; i < n_in; ++i) {
3836 isl_aff *aff;
3838 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3839 isl_dim_set, i);
3840 ma = isl_multi_aff_set_aff(ma, i, aff);
3842 isl_local_space_free(ls);
3843 return ma;
3844 error:
3845 isl_space_free(space);
3846 return NULL;
3849 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3850 * of the space to its range.
3852 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3854 int i, n_in, n_out;
3855 isl_local_space *ls;
3856 isl_multi_aff *ma;
3858 if (!space)
3859 return NULL;
3860 if (!isl_space_is_map(space))
3861 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3862 "not a map space", goto error);
3864 n_in = isl_space_dim(space, isl_dim_in);
3865 n_out = isl_space_dim(space, isl_dim_out);
3866 space = isl_space_range_map(space);
3868 ma = isl_multi_aff_alloc(isl_space_copy(space));
3869 if (n_out == 0) {
3870 isl_space_free(space);
3871 return ma;
3874 space = isl_space_domain(space);
3875 ls = isl_local_space_from_space(space);
3876 for (i = 0; i < n_out; ++i) {
3877 isl_aff *aff;
3879 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3880 isl_dim_set, n_in + i);
3881 ma = isl_multi_aff_set_aff(ma, i, aff);
3883 isl_local_space_free(ls);
3884 return ma;
3885 error:
3886 isl_space_free(space);
3887 return NULL;
3890 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3891 * of the space to its range.
3893 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3894 __isl_take isl_space *space)
3896 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3899 /* Given the space of a set and a range of set dimensions,
3900 * construct an isl_multi_aff that projects out those dimensions.
3902 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3903 __isl_take isl_space *space, enum isl_dim_type type,
3904 unsigned first, unsigned n)
3906 int i, dim;
3907 isl_local_space *ls;
3908 isl_multi_aff *ma;
3910 if (!space)
3911 return NULL;
3912 if (!isl_space_is_set(space))
3913 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3914 "expecting set space", goto error);
3915 if (type != isl_dim_set)
3916 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3917 "only set dimensions can be projected out", goto error);
3919 dim = isl_space_dim(space, isl_dim_set);
3920 if (first + n > dim)
3921 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3922 "range out of bounds", goto error);
3924 space = isl_space_from_domain(space);
3925 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3927 if (dim == n)
3928 return isl_multi_aff_alloc(space);
3930 ma = isl_multi_aff_alloc(isl_space_copy(space));
3931 space = isl_space_domain(space);
3932 ls = isl_local_space_from_space(space);
3934 for (i = 0; i < first; ++i) {
3935 isl_aff *aff;
3937 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3938 isl_dim_set, i);
3939 ma = isl_multi_aff_set_aff(ma, i, aff);
3942 for (i = 0; i < dim - (first + n); ++i) {
3943 isl_aff *aff;
3945 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3946 isl_dim_set, first + n + i);
3947 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3950 isl_local_space_free(ls);
3951 return ma;
3952 error:
3953 isl_space_free(space);
3954 return NULL;
3957 /* Given the space of a set and a range of set dimensions,
3958 * construct an isl_pw_multi_aff that projects out those dimensions.
3960 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3961 __isl_take isl_space *space, enum isl_dim_type type,
3962 unsigned first, unsigned n)
3964 isl_multi_aff *ma;
3966 ma = isl_multi_aff_project_out_map(space, type, first, n);
3967 return isl_pw_multi_aff_from_multi_aff(ma);
3970 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3971 * domain.
3973 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3974 __isl_take isl_multi_aff *ma)
3976 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3977 return isl_pw_multi_aff_alloc(dom, ma);
3980 /* Create a piecewise multi-affine expression in the given space that maps each
3981 * input dimension to the corresponding output dimension.
3983 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3984 __isl_take isl_space *space)
3986 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3989 /* Exploit the equalities in "eq" to simplify the affine expressions.
3991 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3992 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3994 int i;
3996 maff = isl_multi_aff_cow(maff);
3997 if (!maff || !eq)
3998 goto error;
4000 for (i = 0; i < maff->n; ++i) {
4001 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4002 isl_basic_set_copy(eq));
4003 if (!maff->p[i])
4004 goto error;
4007 isl_basic_set_free(eq);
4008 return maff;
4009 error:
4010 isl_basic_set_free(eq);
4011 isl_multi_aff_free(maff);
4012 return NULL;
4015 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4016 isl_int f)
4018 int i;
4020 maff = isl_multi_aff_cow(maff);
4021 if (!maff)
4022 return NULL;
4024 for (i = 0; i < maff->n; ++i) {
4025 maff->p[i] = isl_aff_scale(maff->p[i], f);
4026 if (!maff->p[i])
4027 return isl_multi_aff_free(maff);
4030 return maff;
4033 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4034 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4036 maff1 = isl_multi_aff_add(maff1, maff2);
4037 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4038 return maff1;
4041 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4043 if (!maff)
4044 return -1;
4046 return 0;
4049 /* Return the set of domain elements where "ma1" is lexicographically
4050 * smaller than or equal to "ma2".
4052 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4053 __isl_take isl_multi_aff *ma2)
4055 return isl_multi_aff_lex_ge_set(ma2, ma1);
4058 /* Return the set of domain elements where "ma1" is lexicographically
4059 * greater than or equal to "ma2".
4061 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4062 __isl_take isl_multi_aff *ma2)
4064 isl_space *space;
4065 isl_map *map1, *map2;
4066 isl_map *map, *ge;
4068 map1 = isl_map_from_multi_aff(ma1);
4069 map2 = isl_map_from_multi_aff(ma2);
4070 map = isl_map_range_product(map1, map2);
4071 space = isl_space_range(isl_map_get_space(map));
4072 space = isl_space_domain(isl_space_unwrap(space));
4073 ge = isl_map_lex_ge(space);
4074 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4076 return isl_map_domain(map);
4079 #undef PW
4080 #define PW isl_pw_multi_aff
4081 #undef EL
4082 #define EL isl_multi_aff
4083 #undef EL_IS_ZERO
4084 #define EL_IS_ZERO is_empty
4085 #undef ZERO
4086 #define ZERO empty
4087 #undef IS_ZERO
4088 #define IS_ZERO is_empty
4089 #undef FIELD
4090 #define FIELD maff
4091 #undef DEFAULT_IS_ZERO
4092 #define DEFAULT_IS_ZERO 0
4094 #define NO_SUB
4095 #define NO_EVAL
4096 #define NO_OPT
4097 #define NO_INVOLVES_DIMS
4098 #define NO_INSERT_DIMS
4099 #define NO_LIFT
4100 #define NO_MORPH
4102 #include <isl_pw_templ.c>
4104 #undef NO_SUB
4106 #undef UNION
4107 #define UNION isl_union_pw_multi_aff
4108 #undef PART
4109 #define PART isl_pw_multi_aff
4110 #undef PARTS
4111 #define PARTS pw_multi_aff
4113 #include <isl_union_multi.c>
4114 #include <isl_union_neg.c>
4116 /* Given a function "cmp" that returns the set of elements where
4117 * "ma1" is "better" than "ma2", return the intersection of this
4118 * set with "dom1" and "dom2".
4120 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
4121 __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
4122 __isl_keep isl_multi_aff *ma2,
4123 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4124 __isl_take isl_multi_aff *ma2))
4126 isl_set *common;
4127 isl_set *better;
4128 int is_empty;
4130 common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
4131 is_empty = isl_set_plain_is_empty(common);
4132 if (is_empty >= 0 && is_empty)
4133 return common;
4134 if (is_empty < 0)
4135 return isl_set_free(common);
4136 better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
4137 better = isl_set_intersect(common, better);
4139 return better;
4142 /* Given a function "cmp" that returns the set of elements where
4143 * "ma1" is "better" than "ma2", return a piecewise multi affine
4144 * expression defined on the union of the definition domains
4145 * of "pma1" and "pma2" that maps to the "best" of "pma1" and
4146 * "pma2" on each cell. If only one of the two input functions
4147 * is defined on a given cell, then it is considered the best.
4149 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
4150 __isl_take isl_pw_multi_aff *pma1,
4151 __isl_take isl_pw_multi_aff *pma2,
4152 __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
4153 __isl_take isl_multi_aff *ma2))
4155 int i, j, n;
4156 isl_pw_multi_aff *res = NULL;
4157 isl_ctx *ctx;
4158 isl_set *set = NULL;
4160 if (!pma1 || !pma2)
4161 goto error;
4163 ctx = isl_space_get_ctx(pma1->dim);
4164 if (!isl_space_is_equal(pma1->dim, pma2->dim))
4165 isl_die(ctx, isl_error_invalid,
4166 "arguments should live in the same space", goto error);
4168 if (isl_pw_multi_aff_is_empty(pma1)) {
4169 isl_pw_multi_aff_free(pma1);
4170 return pma2;
4173 if (isl_pw_multi_aff_is_empty(pma2)) {
4174 isl_pw_multi_aff_free(pma2);
4175 return pma1;
4178 n = 2 * (pma1->n + 1) * (pma2->n + 1);
4179 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
4181 for (i = 0; i < pma1->n; ++i) {
4182 set = isl_set_copy(pma1->p[i].set);
4183 for (j = 0; j < pma2->n; ++j) {
4184 isl_set *better;
4185 int is_empty;
4187 better = shared_and_better(pma2->p[j].set,
4188 pma1->p[i].set, pma2->p[j].maff,
4189 pma1->p[i].maff, cmp);
4190 is_empty = isl_set_plain_is_empty(better);
4191 if (is_empty < 0 || is_empty) {
4192 isl_set_free(better);
4193 if (is_empty < 0)
4194 goto error;
4195 continue;
4197 set = isl_set_subtract(set, isl_set_copy(better));
4199 res = isl_pw_multi_aff_add_piece(res, better,
4200 isl_multi_aff_copy(pma2->p[j].maff));
4202 res = isl_pw_multi_aff_add_piece(res, set,
4203 isl_multi_aff_copy(pma1->p[i].maff));
4206 for (j = 0; j < pma2->n; ++j) {
4207 set = isl_set_copy(pma2->p[j].set);
4208 for (i = 0; i < pma1->n; ++i)
4209 set = isl_set_subtract(set,
4210 isl_set_copy(pma1->p[i].set));
4211 res = isl_pw_multi_aff_add_piece(res, set,
4212 isl_multi_aff_copy(pma2->p[j].maff));
4215 isl_pw_multi_aff_free(pma1);
4216 isl_pw_multi_aff_free(pma2);
4218 return res;
4219 error:
4220 isl_pw_multi_aff_free(pma1);
4221 isl_pw_multi_aff_free(pma2);
4222 isl_set_free(set);
4223 return isl_pw_multi_aff_free(res);
4226 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4227 __isl_take isl_pw_multi_aff *pma1,
4228 __isl_take isl_pw_multi_aff *pma2)
4230 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
4233 /* Given two piecewise multi affine expressions, return a piecewise
4234 * multi-affine expression defined on the union of the definition domains
4235 * of the inputs that is equal to the lexicographic maximum of the two
4236 * inputs on each cell. If only one of the two inputs is defined on
4237 * a given cell, then it is considered to be the maximum.
4239 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4240 __isl_take isl_pw_multi_aff *pma1,
4241 __isl_take isl_pw_multi_aff *pma2)
4243 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4244 &pw_multi_aff_union_lexmax);
4247 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4248 __isl_take isl_pw_multi_aff *pma1,
4249 __isl_take isl_pw_multi_aff *pma2)
4251 return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
4254 /* Given two piecewise multi affine expressions, return a piecewise
4255 * multi-affine expression defined on the union of the definition domains
4256 * of the inputs that is equal to the lexicographic minimum of the two
4257 * inputs on each cell. If only one of the two inputs is defined on
4258 * a given cell, then it is considered to be the minimum.
4260 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4261 __isl_take isl_pw_multi_aff *pma1,
4262 __isl_take isl_pw_multi_aff *pma2)
4264 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4265 &pw_multi_aff_union_lexmin);
4268 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4269 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4271 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4272 &isl_multi_aff_add);
4275 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4276 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4278 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4279 &pw_multi_aff_add);
4282 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4283 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4285 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4286 &isl_multi_aff_sub);
4289 /* Subtract "pma2" from "pma1" and return the result.
4291 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4292 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4294 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4295 &pw_multi_aff_sub);
4298 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4299 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4301 return isl_pw_multi_aff_union_add_(pma1, pma2);
4304 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4305 * with the actual sum on the shared domain and
4306 * the defined expression on the symmetric difference of the domains.
4308 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4309 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4311 return isl_union_pw_aff_union_add_(upa1, upa2);
4314 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4315 * with the actual sum on the shared domain and
4316 * the defined expression on the symmetric difference of the domains.
4318 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4319 __isl_take isl_union_pw_multi_aff *upma1,
4320 __isl_take isl_union_pw_multi_aff *upma2)
4322 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4325 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4326 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4328 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4329 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4331 int i, j, n;
4332 isl_space *space;
4333 isl_pw_multi_aff *res;
4335 if (!pma1 || !pma2)
4336 goto error;
4338 n = pma1->n * pma2->n;
4339 space = isl_space_product(isl_space_copy(pma1->dim),
4340 isl_space_copy(pma2->dim));
4341 res = isl_pw_multi_aff_alloc_size(space, n);
4343 for (i = 0; i < pma1->n; ++i) {
4344 for (j = 0; j < pma2->n; ++j) {
4345 isl_set *domain;
4346 isl_multi_aff *ma;
4348 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4349 isl_set_copy(pma2->p[j].set));
4350 ma = isl_multi_aff_product(
4351 isl_multi_aff_copy(pma1->p[i].maff),
4352 isl_multi_aff_copy(pma2->p[j].maff));
4353 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4357 isl_pw_multi_aff_free(pma1);
4358 isl_pw_multi_aff_free(pma2);
4359 return res;
4360 error:
4361 isl_pw_multi_aff_free(pma1);
4362 isl_pw_multi_aff_free(pma2);
4363 return NULL;
4366 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4367 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4369 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4370 &pw_multi_aff_product);
4373 /* Construct a map mapping the domain of the piecewise multi-affine expression
4374 * to its range, with each dimension in the range equated to the
4375 * corresponding affine expression on its cell.
4377 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4379 int i;
4380 isl_map *map;
4382 if (!pma)
4383 return NULL;
4385 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4387 for (i = 0; i < pma->n; ++i) {
4388 isl_multi_aff *maff;
4389 isl_basic_map *bmap;
4390 isl_map *map_i;
4392 maff = isl_multi_aff_copy(pma->p[i].maff);
4393 bmap = isl_basic_map_from_multi_aff(maff);
4394 map_i = isl_map_from_basic_map(bmap);
4395 map_i = isl_map_intersect_domain(map_i,
4396 isl_set_copy(pma->p[i].set));
4397 map = isl_map_union_disjoint(map, map_i);
4400 isl_pw_multi_aff_free(pma);
4401 return map;
4404 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4406 if (!pma)
4407 return NULL;
4409 if (!isl_space_is_set(pma->dim))
4410 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4411 "isl_pw_multi_aff cannot be converted into an isl_set",
4412 goto error);
4414 return isl_map_from_pw_multi_aff(pma);
4415 error:
4416 isl_pw_multi_aff_free(pma);
4417 return NULL;
4420 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4421 * denominator "denom".
4422 * "denom" is allowed to be negative, in which case the actual denominator
4423 * is -denom and the expressions are added instead.
4425 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4426 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4428 int i, first;
4429 int sign;
4430 isl_int d;
4432 first = isl_seq_first_non_zero(c, n);
4433 if (first == -1)
4434 return aff;
4436 sign = isl_int_sgn(denom);
4437 isl_int_init(d);
4438 isl_int_abs(d, denom);
4439 for (i = first; i < n; ++i) {
4440 isl_aff *aff_i;
4442 if (isl_int_is_zero(c[i]))
4443 continue;
4444 aff_i = isl_multi_aff_get_aff(ma, i);
4445 aff_i = isl_aff_scale(aff_i, c[i]);
4446 aff_i = isl_aff_scale_down(aff_i, d);
4447 if (sign >= 0)
4448 aff = isl_aff_sub(aff, aff_i);
4449 else
4450 aff = isl_aff_add(aff, aff_i);
4452 isl_int_clear(d);
4454 return aff;
4457 /* Extract an affine expression that expresses the output dimension "pos"
4458 * of "bmap" in terms of the parameters and input dimensions from
4459 * equality "eq".
4460 * Note that this expression may involve integer divisions defined
4461 * in terms of parameters and input dimensions.
4462 * The equality may also involve references to earlier (but not later)
4463 * output dimensions. These are replaced by the corresponding elements
4464 * in "ma".
4466 * If the equality is of the form
4468 * f(i) + h(j) + a x + g(i) = 0,
4470 * with f(i) a linear combinations of the parameters and input dimensions,
4471 * g(i) a linear combination of integer divisions defined in terms of the same
4472 * and h(j) a linear combinations of earlier output dimensions,
4473 * then the affine expression is
4475 * (-f(i) - g(i))/a - h(j)/a
4477 * If the equality is of the form
4479 * f(i) + h(j) - a x + g(i) = 0,
4481 * then the affine expression is
4483 * (f(i) + g(i))/a - h(j)/(-a)
4486 * If "div" refers to an integer division (i.e., it is smaller than
4487 * the number of integer divisions), then the equality constraint
4488 * does involve an integer division (the one at position "div") that
4489 * is defined in terms of output dimensions. However, this integer
4490 * division can be eliminated by exploiting a pair of constraints
4491 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4492 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4493 * -l + x >= 0.
4494 * In particular, let
4496 * x = e(i) + m floor(...)
4498 * with e(i) the expression derived above and floor(...) the integer
4499 * division involving output dimensions.
4500 * From
4502 * l <= x <= l + n,
4504 * we have
4506 * 0 <= x - l <= n
4508 * This means
4510 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4511 * = (e(i) - l) mod m
4513 * Therefore,
4515 * x - l = (e(i) - l) mod m
4517 * or
4519 * x = ((e(i) - l) mod m) + l
4521 * The variable "shift" below contains the expression -l, which may
4522 * also involve a linear combination of earlier output dimensions.
4524 static __isl_give isl_aff *extract_aff_from_equality(
4525 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4526 __isl_keep isl_multi_aff *ma)
4528 unsigned o_out;
4529 unsigned n_div, n_out;
4530 isl_ctx *ctx;
4531 isl_local_space *ls;
4532 isl_aff *aff, *shift;
4533 isl_val *mod;
4535 ctx = isl_basic_map_get_ctx(bmap);
4536 ls = isl_basic_map_get_local_space(bmap);
4537 ls = isl_local_space_domain(ls);
4538 aff = isl_aff_alloc(isl_local_space_copy(ls));
4539 if (!aff)
4540 goto error;
4541 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4542 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4543 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4544 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4545 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4546 isl_seq_cpy(aff->v->el + 1 + o_out,
4547 bmap->eq[eq] + o_out + n_out, n_div);
4548 } else {
4549 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4550 isl_seq_neg(aff->v->el + 1 + o_out,
4551 bmap->eq[eq] + o_out + n_out, n_div);
4553 if (div < n_div)
4554 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4555 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4556 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4557 bmap->eq[eq][o_out + pos]);
4558 if (div < n_div) {
4559 shift = isl_aff_alloc(isl_local_space_copy(ls));
4560 if (!shift)
4561 goto error;
4562 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4563 isl_seq_cpy(shift->v->el + 1 + o_out,
4564 bmap->ineq[ineq] + o_out + n_out, n_div);
4565 isl_int_set_si(shift->v->el[0], 1);
4566 shift = subtract_initial(shift, ma, pos,
4567 bmap->ineq[ineq] + o_out, ctx->negone);
4568 aff = isl_aff_add(aff, isl_aff_copy(shift));
4569 mod = isl_val_int_from_isl_int(ctx,
4570 bmap->eq[eq][o_out + n_out + div]);
4571 mod = isl_val_abs(mod);
4572 aff = isl_aff_mod_val(aff, mod);
4573 aff = isl_aff_sub(aff, shift);
4576 isl_local_space_free(ls);
4577 return aff;
4578 error:
4579 isl_local_space_free(ls);
4580 isl_aff_free(aff);
4581 return NULL;
4584 /* Given a basic map with output dimensions defined
4585 * in terms of the parameters input dimensions and earlier
4586 * output dimensions using an equality (and possibly a pair on inequalities),
4587 * extract an isl_aff that expresses output dimension "pos" in terms
4588 * of the parameters and input dimensions.
4589 * Note that this expression may involve integer divisions defined
4590 * in terms of parameters and input dimensions.
4591 * "ma" contains the expressions corresponding to earlier output dimensions.
4593 * This function shares some similarities with
4594 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4596 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4597 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4599 int eq, div, ineq;
4600 isl_aff *aff;
4602 if (!bmap)
4603 return NULL;
4604 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4605 if (eq >= bmap->n_eq)
4606 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4607 "unable to find suitable equality", return NULL);
4608 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4610 aff = isl_aff_remove_unused_divs(aff);
4611 return aff;
4614 /* Given a basic map where each output dimension is defined
4615 * in terms of the parameters and input dimensions using an equality,
4616 * extract an isl_multi_aff that expresses the output dimensions in terms
4617 * of the parameters and input dimensions.
4619 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4620 __isl_take isl_basic_map *bmap)
4622 int i;
4623 unsigned n_out;
4624 isl_multi_aff *ma;
4626 if (!bmap)
4627 return NULL;
4629 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4630 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4632 for (i = 0; i < n_out; ++i) {
4633 isl_aff *aff;
4635 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4636 ma = isl_multi_aff_set_aff(ma, i, aff);
4639 isl_basic_map_free(bmap);
4641 return ma;
4644 /* Given a basic set where each set dimension is defined
4645 * in terms of the parameters using an equality,
4646 * extract an isl_multi_aff that expresses the set dimensions in terms
4647 * of the parameters.
4649 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4650 __isl_take isl_basic_set *bset)
4652 return extract_isl_multi_aff_from_basic_map(bset);
4655 /* Create an isl_pw_multi_aff that is equivalent to
4656 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4657 * The given basic map is such that each output dimension is defined
4658 * in terms of the parameters and input dimensions using an equality.
4660 * Since some applications expect the result of isl_pw_multi_aff_from_map
4661 * to only contain integer affine expressions, we compute the floor
4662 * of the expression before returning.
4664 * Remove all constraints involving local variables without
4665 * an explicit representation (resulting in the removal of those
4666 * local variables) prior to the actual extraction to ensure
4667 * that the local spaces in which the resulting affine expressions
4668 * are created do not contain any unknown local variables.
4669 * Removing such constraints is safe because constraints involving
4670 * unknown local variables are not used to determine whether
4671 * a basic map is obviously single-valued.
4673 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4674 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4676 isl_multi_aff *ma;
4678 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4679 ma = extract_isl_multi_aff_from_basic_map(bmap);
4680 ma = isl_multi_aff_floor(ma);
4681 return isl_pw_multi_aff_alloc(domain, ma);
4684 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4685 * This obviously only works if the input "map" is single-valued.
4686 * If so, we compute the lexicographic minimum of the image in the form
4687 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4688 * to its lexicographic minimum.
4689 * If the input is not single-valued, we produce an error.
4691 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4692 __isl_take isl_map *map)
4694 int i;
4695 int sv;
4696 isl_pw_multi_aff *pma;
4698 sv = isl_map_is_single_valued(map);
4699 if (sv < 0)
4700 goto error;
4701 if (!sv)
4702 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4703 "map is not single-valued", goto error);
4704 map = isl_map_make_disjoint(map);
4705 if (!map)
4706 return NULL;
4708 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4710 for (i = 0; i < map->n; ++i) {
4711 isl_pw_multi_aff *pma_i;
4712 isl_basic_map *bmap;
4713 bmap = isl_basic_map_copy(map->p[i]);
4714 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4715 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4718 isl_map_free(map);
4719 return pma;
4720 error:
4721 isl_map_free(map);
4722 return NULL;
4725 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4726 * taking into account that the output dimension at position "d"
4727 * can be represented as
4729 * x = floor((e(...) + c1) / m)
4731 * given that constraint "i" is of the form
4733 * e(...) + c1 - m x >= 0
4736 * Let "map" be of the form
4738 * A -> B
4740 * We construct a mapping
4742 * A -> [A -> x = floor(...)]
4744 * apply that to the map, obtaining
4746 * [A -> x = floor(...)] -> B
4748 * and equate dimension "d" to x.
4749 * We then compute a isl_pw_multi_aff representation of the resulting map
4750 * and plug in the mapping above.
4752 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4753 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4755 isl_ctx *ctx;
4756 isl_space *space;
4757 isl_local_space *ls;
4758 isl_multi_aff *ma;
4759 isl_aff *aff;
4760 isl_vec *v;
4761 isl_map *insert;
4762 int offset;
4763 int n;
4764 int n_in;
4765 isl_pw_multi_aff *pma;
4766 int is_set;
4768 is_set = isl_map_is_set(map);
4770 offset = isl_basic_map_offset(hull, isl_dim_out);
4771 ctx = isl_map_get_ctx(map);
4772 space = isl_space_domain(isl_map_get_space(map));
4773 n_in = isl_space_dim(space, isl_dim_set);
4774 n = isl_space_dim(space, isl_dim_all);
4776 v = isl_vec_alloc(ctx, 1 + 1 + n);
4777 if (v) {
4778 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4779 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4781 isl_basic_map_free(hull);
4783 ls = isl_local_space_from_space(isl_space_copy(space));
4784 aff = isl_aff_alloc_vec(ls, v);
4785 aff = isl_aff_floor(aff);
4786 if (is_set) {
4787 isl_space_free(space);
4788 ma = isl_multi_aff_from_aff(aff);
4789 } else {
4790 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4791 ma = isl_multi_aff_range_product(ma,
4792 isl_multi_aff_from_aff(aff));
4795 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4796 map = isl_map_apply_domain(map, insert);
4797 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4798 pma = isl_pw_multi_aff_from_map(map);
4799 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4801 return pma;
4804 /* Is constraint "c" of the form
4806 * e(...) + c1 - m x >= 0
4808 * or
4810 * -e(...) + c2 + m x >= 0
4812 * where m > 1 and e only depends on parameters and input dimemnsions?
4814 * "offset" is the offset of the output dimensions
4815 * "pos" is the position of output dimension x.
4817 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4819 if (isl_int_is_zero(c[offset + d]))
4820 return 0;
4821 if (isl_int_is_one(c[offset + d]))
4822 return 0;
4823 if (isl_int_is_negone(c[offset + d]))
4824 return 0;
4825 if (isl_seq_first_non_zero(c + offset, d) != -1)
4826 return 0;
4827 if (isl_seq_first_non_zero(c + offset + d + 1,
4828 total - (offset + d + 1)) != -1)
4829 return 0;
4830 return 1;
4833 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4835 * As a special case, we first check if there is any pair of constraints,
4836 * shared by all the basic maps in "map" that force a given dimension
4837 * to be equal to the floor of some affine combination of the input dimensions.
4839 * In particular, if we can find two constraints
4841 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4843 * and
4845 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4847 * where m > 1 and e only depends on parameters and input dimemnsions,
4848 * and such that
4850 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4852 * then we know that we can take
4854 * x = floor((e(...) + c1) / m)
4856 * without having to perform any computation.
4858 * Note that we know that
4860 * c1 + c2 >= 1
4862 * If c1 + c2 were 0, then we would have detected an equality during
4863 * simplification. If c1 + c2 were negative, then we would have detected
4864 * a contradiction.
4866 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4867 __isl_take isl_map *map)
4869 int d, dim;
4870 int i, j, n;
4871 int offset, total;
4872 isl_int sum;
4873 isl_basic_map *hull;
4875 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4876 if (!hull)
4877 goto error;
4879 isl_int_init(sum);
4880 dim = isl_map_dim(map, isl_dim_out);
4881 offset = isl_basic_map_offset(hull, isl_dim_out);
4882 total = 1 + isl_basic_map_total_dim(hull);
4883 n = hull->n_ineq;
4884 for (d = 0; d < dim; ++d) {
4885 for (i = 0; i < n; ++i) {
4886 if (!is_potential_div_constraint(hull->ineq[i],
4887 offset, d, total))
4888 continue;
4889 for (j = i + 1; j < n; ++j) {
4890 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4891 hull->ineq[j] + 1, total - 1))
4892 continue;
4893 isl_int_add(sum, hull->ineq[i][0],
4894 hull->ineq[j][0]);
4895 if (isl_int_abs_lt(sum,
4896 hull->ineq[i][offset + d]))
4897 break;
4900 if (j >= n)
4901 continue;
4902 isl_int_clear(sum);
4903 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4904 j = i;
4905 return pw_multi_aff_from_map_div(map, hull, d, j);
4908 isl_int_clear(sum);
4909 isl_basic_map_free(hull);
4910 return pw_multi_aff_from_map_base(map);
4911 error:
4912 isl_map_free(map);
4913 isl_basic_map_free(hull);
4914 return NULL;
4917 /* Given an affine expression
4919 * [A -> B] -> f(A,B)
4921 * construct an isl_multi_aff
4923 * [A -> B] -> B'
4925 * such that dimension "d" in B' is set to "aff" and the remaining
4926 * dimensions are set equal to the corresponding dimensions in B.
4927 * "n_in" is the dimension of the space A.
4928 * "n_out" is the dimension of the space B.
4930 * If "is_set" is set, then the affine expression is of the form
4932 * [B] -> f(B)
4934 * and we construct an isl_multi_aff
4936 * B -> B'
4938 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4939 unsigned n_in, unsigned n_out, int is_set)
4941 int i;
4942 isl_multi_aff *ma;
4943 isl_space *space, *space2;
4944 isl_local_space *ls;
4946 space = isl_aff_get_domain_space(aff);
4947 ls = isl_local_space_from_space(isl_space_copy(space));
4948 space2 = isl_space_copy(space);
4949 if (!is_set)
4950 space2 = isl_space_range(isl_space_unwrap(space2));
4951 space = isl_space_map_from_domain_and_range(space, space2);
4952 ma = isl_multi_aff_alloc(space);
4953 ma = isl_multi_aff_set_aff(ma, d, aff);
4955 for (i = 0; i < n_out; ++i) {
4956 if (i == d)
4957 continue;
4958 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4959 isl_dim_set, n_in + i);
4960 ma = isl_multi_aff_set_aff(ma, i, aff);
4963 isl_local_space_free(ls);
4965 return ma;
4968 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4969 * taking into account that the dimension at position "d" can be written as
4971 * x = m a + f(..) (1)
4973 * where m is equal to "gcd".
4974 * "i" is the index of the equality in "hull" that defines f(..).
4975 * In particular, the equality is of the form
4977 * f(..) - x + m g(existentials) = 0
4979 * or
4981 * -f(..) + x + m g(existentials) = 0
4983 * We basically plug (1) into "map", resulting in a map with "a"
4984 * in the range instead of "x". The corresponding isl_pw_multi_aff
4985 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4987 * Specifically, given the input map
4989 * A -> B
4991 * We first wrap it into a set
4993 * [A -> B]
4995 * and define (1) on top of the corresponding space, resulting in "aff".
4996 * We use this to create an isl_multi_aff that maps the output position "d"
4997 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4998 * We plug this into the wrapped map, unwrap the result and compute the
4999 * corresponding isl_pw_multi_aff.
5000 * The result is an expression
5002 * A -> T(A)
5004 * We adjust that to
5006 * A -> [A -> T(A)]
5008 * so that we can plug that into "aff", after extending the latter to
5009 * a mapping
5011 * [A -> B] -> B'
5014 * If "map" is actually a set, then there is no "A" space, meaning
5015 * that we do not need to perform any wrapping, and that the result
5016 * of the recursive call is of the form
5018 * [T]
5020 * which is plugged into a mapping of the form
5022 * B -> B'
5024 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5025 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5026 isl_int gcd)
5028 isl_set *set;
5029 isl_space *space;
5030 isl_local_space *ls;
5031 isl_aff *aff;
5032 isl_multi_aff *ma;
5033 isl_pw_multi_aff *pma, *id;
5034 unsigned n_in;
5035 unsigned o_out;
5036 unsigned n_out;
5037 int is_set;
5039 is_set = isl_map_is_set(map);
5041 n_in = isl_basic_map_dim(hull, isl_dim_in);
5042 n_out = isl_basic_map_dim(hull, isl_dim_out);
5043 o_out = isl_basic_map_offset(hull, isl_dim_out);
5045 if (is_set)
5046 set = map;
5047 else
5048 set = isl_map_wrap(map);
5049 space = isl_space_map_from_set(isl_set_get_space(set));
5050 ma = isl_multi_aff_identity(space);
5051 ls = isl_local_space_from_space(isl_set_get_space(set));
5052 aff = isl_aff_alloc(ls);
5053 if (aff) {
5054 isl_int_set_si(aff->v->el[0], 1);
5055 if (isl_int_is_one(hull->eq[i][o_out + d]))
5056 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5057 aff->v->size - 1);
5058 else
5059 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5060 aff->v->size - 1);
5061 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5063 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5064 set = isl_set_preimage_multi_aff(set, ma);
5066 ma = range_map(aff, d, n_in, n_out, is_set);
5068 if (is_set)
5069 map = set;
5070 else
5071 map = isl_set_unwrap(set);
5072 pma = isl_pw_multi_aff_from_map(map);
5074 if (!is_set) {
5075 space = isl_pw_multi_aff_get_domain_space(pma);
5076 space = isl_space_map_from_set(space);
5077 id = isl_pw_multi_aff_identity(space);
5078 pma = isl_pw_multi_aff_range_product(id, pma);
5080 id = isl_pw_multi_aff_from_multi_aff(ma);
5081 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5083 isl_basic_map_free(hull);
5084 return pma;
5087 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5089 * As a special case, we first check if all output dimensions are uniquely
5090 * defined in terms of the parameters and input dimensions over the entire
5091 * domain. If so, we extract the desired isl_pw_multi_aff directly
5092 * from the affine hull of "map" and its domain.
5094 * Otherwise, we check if any of the output dimensions is "strided".
5095 * That is, we check if can be written as
5097 * x = m a + f(..)
5099 * with m greater than 1, a some combination of existentially quantified
5100 * variables and f an expression in the parameters and input dimensions.
5101 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5103 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5104 * special case.
5106 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5108 int i, j;
5109 isl_bool sv;
5110 isl_basic_map *hull;
5111 unsigned n_out;
5112 unsigned o_out;
5113 unsigned n_div;
5114 unsigned o_div;
5115 isl_int gcd;
5117 if (!map)
5118 return NULL;
5120 map = isl_map_detect_equalities(map);
5121 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5122 sv = isl_basic_map_plain_is_single_valued(hull);
5123 if (sv >= 0 && sv)
5124 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5125 if (sv < 0)
5126 hull = isl_basic_map_free(hull);
5127 if (!hull)
5128 goto error;
5130 n_div = isl_basic_map_dim(hull, isl_dim_div);
5131 o_div = isl_basic_map_offset(hull, isl_dim_div);
5133 if (n_div == 0) {
5134 isl_basic_map_free(hull);
5135 return pw_multi_aff_from_map_check_div(map);
5138 isl_int_init(gcd);
5140 n_out = isl_basic_map_dim(hull, isl_dim_out);
5141 o_out = isl_basic_map_offset(hull, isl_dim_out);
5143 for (i = 0; i < n_out; ++i) {
5144 for (j = 0; j < hull->n_eq; ++j) {
5145 isl_int *eq = hull->eq[j];
5146 isl_pw_multi_aff *res;
5148 if (!isl_int_is_one(eq[o_out + i]) &&
5149 !isl_int_is_negone(eq[o_out + i]))
5150 continue;
5151 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5152 continue;
5153 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5154 n_out - (i + 1)) != -1)
5155 continue;
5156 isl_seq_gcd(eq + o_div, n_div, &gcd);
5157 if (isl_int_is_zero(gcd))
5158 continue;
5159 if (isl_int_is_one(gcd))
5160 continue;
5162 res = pw_multi_aff_from_map_stride(map, hull,
5163 i, j, gcd);
5164 isl_int_clear(gcd);
5165 return res;
5169 isl_int_clear(gcd);
5170 isl_basic_map_free(hull);
5171 return pw_multi_aff_from_map_check_div(map);
5172 error:
5173 isl_map_free(map);
5174 return NULL;
5177 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5179 return isl_pw_multi_aff_from_map(set);
5182 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5183 * add it to *user.
5185 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5187 isl_union_pw_multi_aff **upma = user;
5188 isl_pw_multi_aff *pma;
5190 pma = isl_pw_multi_aff_from_map(map);
5191 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5193 return *upma ? isl_stat_ok : isl_stat_error;
5196 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5197 * domain.
5199 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5200 __isl_take isl_aff *aff)
5202 isl_multi_aff *ma;
5203 isl_pw_multi_aff *pma;
5205 ma = isl_multi_aff_from_aff(aff);
5206 pma = isl_pw_multi_aff_from_multi_aff(ma);
5207 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5210 /* Try and create an isl_union_pw_multi_aff that is equivalent
5211 * to the given isl_union_map.
5212 * The isl_union_map is required to be single-valued in each space.
5213 * Otherwise, an error is produced.
5215 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5216 __isl_take isl_union_map *umap)
5218 isl_space *space;
5219 isl_union_pw_multi_aff *upma;
5221 space = isl_union_map_get_space(umap);
5222 upma = isl_union_pw_multi_aff_empty(space);
5223 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5224 upma = isl_union_pw_multi_aff_free(upma);
5225 isl_union_map_free(umap);
5227 return upma;
5230 /* Try and create an isl_union_pw_multi_aff that is equivalent
5231 * to the given isl_union_set.
5232 * The isl_union_set is required to be a singleton in each space.
5233 * Otherwise, an error is produced.
5235 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5236 __isl_take isl_union_set *uset)
5238 return isl_union_pw_multi_aff_from_union_map(uset);
5241 /* Return the piecewise affine expression "set ? 1 : 0".
5243 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5245 isl_pw_aff *pa;
5246 isl_space *space = isl_set_get_space(set);
5247 isl_local_space *ls = isl_local_space_from_space(space);
5248 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5249 isl_aff *one = isl_aff_zero_on_domain(ls);
5251 one = isl_aff_add_constant_si(one, 1);
5252 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5253 set = isl_set_complement(set);
5254 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5256 return pa;
5259 /* Plug in "subs" for dimension "type", "pos" of "aff".
5261 * Let i be the dimension to replace and let "subs" be of the form
5263 * f/d
5265 * and "aff" of the form
5267 * (a i + g)/m
5269 * The result is
5271 * (a f + d g')/(m d)
5273 * where g' is the result of plugging in "subs" in each of the integer
5274 * divisions in g.
5276 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5277 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5279 isl_ctx *ctx;
5280 isl_int v;
5282 aff = isl_aff_cow(aff);
5283 if (!aff || !subs)
5284 return isl_aff_free(aff);
5286 ctx = isl_aff_get_ctx(aff);
5287 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5288 isl_die(ctx, isl_error_invalid,
5289 "spaces don't match", return isl_aff_free(aff));
5290 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5291 isl_die(ctx, isl_error_unsupported,
5292 "cannot handle divs yet", return isl_aff_free(aff));
5294 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5295 if (!aff->ls)
5296 return isl_aff_free(aff);
5298 aff->v = isl_vec_cow(aff->v);
5299 if (!aff->v)
5300 return isl_aff_free(aff);
5302 pos += isl_local_space_offset(aff->ls, type);
5304 isl_int_init(v);
5305 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5306 aff->v->size, subs->v->size, v);
5307 isl_int_clear(v);
5309 return aff;
5312 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5313 * expressions in "maff".
5315 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5316 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5317 __isl_keep isl_aff *subs)
5319 int i;
5321 maff = isl_multi_aff_cow(maff);
5322 if (!maff || !subs)
5323 return isl_multi_aff_free(maff);
5325 if (type == isl_dim_in)
5326 type = isl_dim_set;
5328 for (i = 0; i < maff->n; ++i) {
5329 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5330 if (!maff->p[i])
5331 return isl_multi_aff_free(maff);
5334 return maff;
5337 /* Plug in "subs" for dimension "type", "pos" of "pma".
5339 * pma is of the form
5341 * A_i(v) -> M_i(v)
5343 * while subs is of the form
5345 * v' = B_j(v) -> S_j
5347 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5348 * has a contribution in the result, in particular
5350 * C_ij(S_j) -> M_i(S_j)
5352 * Note that plugging in S_j in C_ij may also result in an empty set
5353 * and this contribution should simply be discarded.
5355 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5356 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5357 __isl_keep isl_pw_aff *subs)
5359 int i, j, n;
5360 isl_pw_multi_aff *res;
5362 if (!pma || !subs)
5363 return isl_pw_multi_aff_free(pma);
5365 n = pma->n * subs->n;
5366 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5368 for (i = 0; i < pma->n; ++i) {
5369 for (j = 0; j < subs->n; ++j) {
5370 isl_set *common;
5371 isl_multi_aff *res_ij;
5372 int empty;
5374 common = isl_set_intersect(
5375 isl_set_copy(pma->p[i].set),
5376 isl_set_copy(subs->p[j].set));
5377 common = isl_set_substitute(common,
5378 type, pos, subs->p[j].aff);
5379 empty = isl_set_plain_is_empty(common);
5380 if (empty < 0 || empty) {
5381 isl_set_free(common);
5382 if (empty < 0)
5383 goto error;
5384 continue;
5387 res_ij = isl_multi_aff_substitute(
5388 isl_multi_aff_copy(pma->p[i].maff),
5389 type, pos, subs->p[j].aff);
5391 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5395 isl_pw_multi_aff_free(pma);
5396 return res;
5397 error:
5398 isl_pw_multi_aff_free(pma);
5399 isl_pw_multi_aff_free(res);
5400 return NULL;
5403 /* Compute the preimage of a range of dimensions in the affine expression "src"
5404 * under "ma" and put the result in "dst". The number of dimensions in "src"
5405 * that precede the range is given by "n_before". The number of dimensions
5406 * in the range is given by the number of output dimensions of "ma".
5407 * The number of dimensions that follow the range is given by "n_after".
5408 * If "has_denom" is set (to one),
5409 * then "src" and "dst" have an extra initial denominator.
5410 * "n_div_ma" is the number of existentials in "ma"
5411 * "n_div_bset" is the number of existentials in "src"
5412 * The resulting "dst" (which is assumed to have been allocated by
5413 * the caller) contains coefficients for both sets of existentials,
5414 * first those in "ma" and then those in "src".
5415 * f, c1, c2 and g are temporary objects that have been initialized
5416 * by the caller.
5418 * Let src represent the expression
5420 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5422 * and let ma represent the expressions
5424 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5426 * We start out with the following expression for dst:
5428 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5430 * with the multiplication factor f initially equal to 1
5431 * and f \sum_i b_i v_i kept separately.
5432 * For each x_i that we substitute, we multiply the numerator
5433 * (and denominator) of dst by c_1 = m_i and add the numerator
5434 * of the x_i expression multiplied by c_2 = f b_i,
5435 * after removing the common factors of c_1 and c_2.
5436 * The multiplication factor f also needs to be multiplied by c_1
5437 * for the next x_j, j > i.
5439 void isl_seq_preimage(isl_int *dst, isl_int *src,
5440 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5441 int n_div_ma, int n_div_bmap,
5442 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5444 int i;
5445 int n_param, n_in, n_out;
5446 int o_dst, o_src;
5448 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5449 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5450 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5452 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5453 o_dst = o_src = has_denom + 1 + n_param + n_before;
5454 isl_seq_clr(dst + o_dst, n_in);
5455 o_dst += n_in;
5456 o_src += n_out;
5457 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5458 o_dst += n_after;
5459 o_src += n_after;
5460 isl_seq_clr(dst + o_dst, n_div_ma);
5461 o_dst += n_div_ma;
5462 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5464 isl_int_set_si(f, 1);
5466 for (i = 0; i < n_out; ++i) {
5467 int offset = has_denom + 1 + n_param + n_before + i;
5469 if (isl_int_is_zero(src[offset]))
5470 continue;
5471 isl_int_set(c1, ma->p[i]->v->el[0]);
5472 isl_int_mul(c2, f, src[offset]);
5473 isl_int_gcd(g, c1, c2);
5474 isl_int_divexact(c1, c1, g);
5475 isl_int_divexact(c2, c2, g);
5477 isl_int_mul(f, f, c1);
5478 o_dst = has_denom;
5479 o_src = 1;
5480 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5481 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5482 o_dst += 1 + n_param;
5483 o_src += 1 + n_param;
5484 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5485 o_dst += n_before;
5486 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5487 c2, ma->p[i]->v->el + o_src, n_in);
5488 o_dst += n_in;
5489 o_src += n_in;
5490 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5491 o_dst += n_after;
5492 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5493 c2, ma->p[i]->v->el + o_src, n_div_ma);
5494 o_dst += n_div_ma;
5495 o_src += n_div_ma;
5496 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5497 if (has_denom)
5498 isl_int_mul(dst[0], dst[0], c1);
5502 /* Compute the pullback of "aff" by the function represented by "ma".
5503 * In other words, plug in "ma" in "aff". The result is an affine expression
5504 * defined over the domain space of "ma".
5506 * If "aff" is represented by
5508 * (a(p) + b x + c(divs))/d
5510 * and ma is represented by
5512 * x = D(p) + F(y) + G(divs')
5514 * then the result is
5516 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5518 * The divs in the local space of the input are similarly adjusted
5519 * through a call to isl_local_space_preimage_multi_aff.
5521 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5522 __isl_take isl_multi_aff *ma)
5524 isl_aff *res = NULL;
5525 isl_local_space *ls;
5526 int n_div_aff, n_div_ma;
5527 isl_int f, c1, c2, g;
5529 ma = isl_multi_aff_align_divs(ma);
5530 if (!aff || !ma)
5531 goto error;
5533 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5534 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5536 ls = isl_aff_get_domain_local_space(aff);
5537 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5538 res = isl_aff_alloc(ls);
5539 if (!res)
5540 goto error;
5542 isl_int_init(f);
5543 isl_int_init(c1);
5544 isl_int_init(c2);
5545 isl_int_init(g);
5547 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5548 f, c1, c2, g, 1);
5550 isl_int_clear(f);
5551 isl_int_clear(c1);
5552 isl_int_clear(c2);
5553 isl_int_clear(g);
5555 isl_aff_free(aff);
5556 isl_multi_aff_free(ma);
5557 res = isl_aff_normalize(res);
5558 return res;
5559 error:
5560 isl_aff_free(aff);
5561 isl_multi_aff_free(ma);
5562 isl_aff_free(res);
5563 return NULL;
5566 /* Compute the pullback of "aff1" by the function represented by "aff2".
5567 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5568 * defined over the domain space of "aff1".
5570 * The domain of "aff1" should match the range of "aff2", which means
5571 * that it should be single-dimensional.
5573 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5574 __isl_take isl_aff *aff2)
5576 isl_multi_aff *ma;
5578 ma = isl_multi_aff_from_aff(aff2);
5579 return isl_aff_pullback_multi_aff(aff1, ma);
5582 /* Compute the pullback of "ma1" by the function represented by "ma2".
5583 * In other words, plug in "ma2" in "ma1".
5585 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5587 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5588 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5590 int i;
5591 isl_space *space = NULL;
5593 ma2 = isl_multi_aff_align_divs(ma2);
5594 ma1 = isl_multi_aff_cow(ma1);
5595 if (!ma1 || !ma2)
5596 goto error;
5598 space = isl_space_join(isl_multi_aff_get_space(ma2),
5599 isl_multi_aff_get_space(ma1));
5601 for (i = 0; i < ma1->n; ++i) {
5602 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5603 isl_multi_aff_copy(ma2));
5604 if (!ma1->p[i])
5605 goto error;
5608 ma1 = isl_multi_aff_reset_space(ma1, space);
5609 isl_multi_aff_free(ma2);
5610 return ma1;
5611 error:
5612 isl_space_free(space);
5613 isl_multi_aff_free(ma2);
5614 isl_multi_aff_free(ma1);
5615 return NULL;
5618 /* Compute the pullback of "ma1" by the function represented by "ma2".
5619 * In other words, plug in "ma2" in "ma1".
5621 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5622 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5624 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5625 &isl_multi_aff_pullback_multi_aff_aligned);
5628 /* Extend the local space of "dst" to include the divs
5629 * in the local space of "src".
5631 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5632 __isl_keep isl_aff *src)
5634 isl_ctx *ctx;
5635 int *exp1 = NULL;
5636 int *exp2 = NULL;
5637 isl_mat *div;
5639 if (!src || !dst)
5640 return isl_aff_free(dst);
5642 ctx = isl_aff_get_ctx(src);
5643 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5644 isl_die(ctx, isl_error_invalid,
5645 "spaces don't match", goto error);
5647 if (src->ls->div->n_row == 0)
5648 return dst;
5650 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5651 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5652 if (!exp1 || (dst->ls->div->n_row && !exp2))
5653 goto error;
5655 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5656 dst = isl_aff_expand_divs(dst, div, exp2);
5657 free(exp1);
5658 free(exp2);
5660 return dst;
5661 error:
5662 free(exp1);
5663 free(exp2);
5664 return isl_aff_free(dst);
5667 /* Adjust the local spaces of the affine expressions in "maff"
5668 * such that they all have the save divs.
5670 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5671 __isl_take isl_multi_aff *maff)
5673 int i;
5675 if (!maff)
5676 return NULL;
5677 if (maff->n == 0)
5678 return maff;
5679 maff = isl_multi_aff_cow(maff);
5680 if (!maff)
5681 return NULL;
5683 for (i = 1; i < maff->n; ++i)
5684 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5685 for (i = 1; i < maff->n; ++i) {
5686 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5687 if (!maff->p[i])
5688 return isl_multi_aff_free(maff);
5691 return maff;
5694 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5696 aff = isl_aff_cow(aff);
5697 if (!aff)
5698 return NULL;
5700 aff->ls = isl_local_space_lift(aff->ls);
5701 if (!aff->ls)
5702 return isl_aff_free(aff);
5704 return aff;
5707 /* Lift "maff" to a space with extra dimensions such that the result
5708 * has no more existentially quantified variables.
5709 * If "ls" is not NULL, then *ls is assigned the local space that lies
5710 * at the basis of the lifting applied to "maff".
5712 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5713 __isl_give isl_local_space **ls)
5715 int i;
5716 isl_space *space;
5717 unsigned n_div;
5719 if (ls)
5720 *ls = NULL;
5722 if (!maff)
5723 return NULL;
5725 if (maff->n == 0) {
5726 if (ls) {
5727 isl_space *space = isl_multi_aff_get_domain_space(maff);
5728 *ls = isl_local_space_from_space(space);
5729 if (!*ls)
5730 return isl_multi_aff_free(maff);
5732 return maff;
5735 maff = isl_multi_aff_cow(maff);
5736 maff = isl_multi_aff_align_divs(maff);
5737 if (!maff)
5738 return NULL;
5740 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5741 space = isl_multi_aff_get_space(maff);
5742 space = isl_space_lift(isl_space_domain(space), n_div);
5743 space = isl_space_extend_domain_with_range(space,
5744 isl_multi_aff_get_space(maff));
5745 if (!space)
5746 return isl_multi_aff_free(maff);
5747 isl_space_free(maff->space);
5748 maff->space = space;
5750 if (ls) {
5751 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5752 if (!*ls)
5753 return isl_multi_aff_free(maff);
5756 for (i = 0; i < maff->n; ++i) {
5757 maff->p[i] = isl_aff_lift(maff->p[i]);
5758 if (!maff->p[i])
5759 goto error;
5762 return maff;
5763 error:
5764 if (ls)
5765 isl_local_space_free(*ls);
5766 return isl_multi_aff_free(maff);
5770 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5772 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5773 __isl_keep isl_pw_multi_aff *pma, int pos)
5775 int i;
5776 int n_out;
5777 isl_space *space;
5778 isl_pw_aff *pa;
5780 if (!pma)
5781 return NULL;
5783 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5784 if (pos < 0 || pos >= n_out)
5785 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5786 "index out of bounds", return NULL);
5788 space = isl_pw_multi_aff_get_space(pma);
5789 space = isl_space_drop_dims(space, isl_dim_out,
5790 pos + 1, n_out - pos - 1);
5791 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5793 pa = isl_pw_aff_alloc_size(space, pma->n);
5794 for (i = 0; i < pma->n; ++i) {
5795 isl_aff *aff;
5796 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5797 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5800 return pa;
5803 /* Return an isl_pw_multi_aff with the given "set" as domain and
5804 * an unnamed zero-dimensional range.
5806 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5807 __isl_take isl_set *set)
5809 isl_multi_aff *ma;
5810 isl_space *space;
5812 space = isl_set_get_space(set);
5813 space = isl_space_from_domain(space);
5814 ma = isl_multi_aff_zero(space);
5815 return isl_pw_multi_aff_alloc(set, ma);
5818 /* Add an isl_pw_multi_aff with the given "set" as domain and
5819 * an unnamed zero-dimensional range to *user.
5821 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5822 void *user)
5824 isl_union_pw_multi_aff **upma = user;
5825 isl_pw_multi_aff *pma;
5827 pma = isl_pw_multi_aff_from_domain(set);
5828 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5830 return isl_stat_ok;
5833 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5834 * an unnamed zero-dimensional range.
5836 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5837 __isl_take isl_union_set *uset)
5839 isl_space *space;
5840 isl_union_pw_multi_aff *upma;
5842 if (!uset)
5843 return NULL;
5845 space = isl_union_set_get_space(uset);
5846 upma = isl_union_pw_multi_aff_empty(space);
5848 if (isl_union_set_foreach_set(uset,
5849 &add_pw_multi_aff_from_domain, &upma) < 0)
5850 goto error;
5852 isl_union_set_free(uset);
5853 return upma;
5854 error:
5855 isl_union_set_free(uset);
5856 isl_union_pw_multi_aff_free(upma);
5857 return NULL;
5860 /* Convert "pma" to an isl_map and add it to *umap.
5862 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5863 void *user)
5865 isl_union_map **umap = user;
5866 isl_map *map;
5868 map = isl_map_from_pw_multi_aff(pma);
5869 *umap = isl_union_map_add_map(*umap, map);
5871 return isl_stat_ok;
5874 /* Construct a union map mapping the domain of the union
5875 * piecewise multi-affine expression to its range, with each dimension
5876 * in the range equated to the corresponding affine expression on its cell.
5878 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5879 __isl_take isl_union_pw_multi_aff *upma)
5881 isl_space *space;
5882 isl_union_map *umap;
5884 if (!upma)
5885 return NULL;
5887 space = isl_union_pw_multi_aff_get_space(upma);
5888 umap = isl_union_map_empty(space);
5890 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5891 &map_from_pw_multi_aff, &umap) < 0)
5892 goto error;
5894 isl_union_pw_multi_aff_free(upma);
5895 return umap;
5896 error:
5897 isl_union_pw_multi_aff_free(upma);
5898 isl_union_map_free(umap);
5899 return NULL;
5902 /* Local data for bin_entry and the callback "fn".
5904 struct isl_union_pw_multi_aff_bin_data {
5905 isl_union_pw_multi_aff *upma2;
5906 isl_union_pw_multi_aff *res;
5907 isl_pw_multi_aff *pma;
5908 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5911 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5912 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5914 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5916 struct isl_union_pw_multi_aff_bin_data *data = user;
5917 isl_stat r;
5919 data->pma = pma;
5920 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5921 data->fn, data);
5922 isl_pw_multi_aff_free(pma);
5924 return r;
5927 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5928 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5929 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5930 * as *entry. The callback should adjust data->res if desired.
5932 static __isl_give isl_union_pw_multi_aff *bin_op(
5933 __isl_take isl_union_pw_multi_aff *upma1,
5934 __isl_take isl_union_pw_multi_aff *upma2,
5935 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5937 isl_space *space;
5938 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5940 space = isl_union_pw_multi_aff_get_space(upma2);
5941 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5942 space = isl_union_pw_multi_aff_get_space(upma1);
5943 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5945 if (!upma1 || !upma2)
5946 goto error;
5948 data.upma2 = upma2;
5949 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5950 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5951 &bin_entry, &data) < 0)
5952 goto error;
5954 isl_union_pw_multi_aff_free(upma1);
5955 isl_union_pw_multi_aff_free(upma2);
5956 return data.res;
5957 error:
5958 isl_union_pw_multi_aff_free(upma1);
5959 isl_union_pw_multi_aff_free(upma2);
5960 isl_union_pw_multi_aff_free(data.res);
5961 return NULL;
5964 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5965 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5967 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5968 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5970 isl_space *space;
5972 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5973 isl_pw_multi_aff_get_space(pma2));
5974 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5975 &isl_multi_aff_range_product);
5978 /* Given two isl_pw_multi_affs A -> B and C -> D,
5979 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5981 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5982 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5984 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5985 &pw_multi_aff_range_product);
5988 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5989 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5991 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5992 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5994 isl_space *space;
5996 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5997 isl_pw_multi_aff_get_space(pma2));
5998 space = isl_space_flatten_range(space);
5999 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6000 &isl_multi_aff_flat_range_product);
6003 /* Given two isl_pw_multi_affs A -> B and C -> D,
6004 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6006 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6007 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6009 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6010 &pw_multi_aff_flat_range_product);
6013 /* If data->pma and "pma2" have the same domain space, then compute
6014 * their flat range product and the result to data->res.
6016 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6017 void *user)
6019 struct isl_union_pw_multi_aff_bin_data *data = user;
6021 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6022 pma2->dim, isl_dim_in)) {
6023 isl_pw_multi_aff_free(pma2);
6024 return isl_stat_ok;
6027 pma2 = isl_pw_multi_aff_flat_range_product(
6028 isl_pw_multi_aff_copy(data->pma), pma2);
6030 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6032 return isl_stat_ok;
6035 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6036 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6038 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6039 __isl_take isl_union_pw_multi_aff *upma1,
6040 __isl_take isl_union_pw_multi_aff *upma2)
6042 return bin_op(upma1, upma2, &flat_range_product_entry);
6045 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6046 * The parameters are assumed to have been aligned.
6048 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6049 * except that it works on two different isl_pw_* types.
6051 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6052 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6053 __isl_take isl_pw_aff *pa)
6055 int i, j, n;
6056 isl_pw_multi_aff *res = NULL;
6058 if (!pma || !pa)
6059 goto error;
6061 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6062 pa->dim, isl_dim_in))
6063 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6064 "domains don't match", goto error);
6065 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6066 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6067 "index out of bounds", goto error);
6069 n = pma->n * pa->n;
6070 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6072 for (i = 0; i < pma->n; ++i) {
6073 for (j = 0; j < pa->n; ++j) {
6074 isl_set *common;
6075 isl_multi_aff *res_ij;
6076 int empty;
6078 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6079 isl_set_copy(pa->p[j].set));
6080 empty = isl_set_plain_is_empty(common);
6081 if (empty < 0 || empty) {
6082 isl_set_free(common);
6083 if (empty < 0)
6084 goto error;
6085 continue;
6088 res_ij = isl_multi_aff_set_aff(
6089 isl_multi_aff_copy(pma->p[i].maff), pos,
6090 isl_aff_copy(pa->p[j].aff));
6091 res_ij = isl_multi_aff_gist(res_ij,
6092 isl_set_copy(common));
6094 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6098 isl_pw_multi_aff_free(pma);
6099 isl_pw_aff_free(pa);
6100 return res;
6101 error:
6102 isl_pw_multi_aff_free(pma);
6103 isl_pw_aff_free(pa);
6104 return isl_pw_multi_aff_free(res);
6107 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6109 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6110 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6111 __isl_take isl_pw_aff *pa)
6113 if (!pma || !pa)
6114 goto error;
6115 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
6116 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6117 if (!isl_space_has_named_params(pma->dim) ||
6118 !isl_space_has_named_params(pa->dim))
6119 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6120 "unaligned unnamed parameters", goto error);
6121 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6122 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6123 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6124 error:
6125 isl_pw_multi_aff_free(pma);
6126 isl_pw_aff_free(pa);
6127 return NULL;
6130 /* Do the parameters of "pa" match those of "space"?
6132 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6133 __isl_keep isl_space *space)
6135 isl_space *pa_space;
6136 int match;
6138 if (!pa || !space)
6139 return -1;
6141 pa_space = isl_pw_aff_get_space(pa);
6143 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6145 isl_space_free(pa_space);
6146 return match;
6149 /* Check that the domain space of "pa" matches "space".
6151 * Return 0 on success and -1 on error.
6153 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6154 __isl_keep isl_space *space)
6156 isl_space *pa_space;
6157 int match;
6159 if (!pa || !space)
6160 return -1;
6162 pa_space = isl_pw_aff_get_space(pa);
6164 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6165 if (match < 0)
6166 goto error;
6167 if (!match)
6168 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6169 "parameters don't match", goto error);
6170 match = isl_space_tuple_is_equal(space, isl_dim_in,
6171 pa_space, isl_dim_in);
6172 if (match < 0)
6173 goto error;
6174 if (!match)
6175 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6176 "domains don't match", goto error);
6177 isl_space_free(pa_space);
6178 return 0;
6179 error:
6180 isl_space_free(pa_space);
6181 return -1;
6184 #undef BASE
6185 #define BASE pw_aff
6186 #undef DOMBASE
6187 #define DOMBASE set
6189 #include <isl_multi_templ.c>
6190 #include <isl_multi_apply_set.c>
6191 #include <isl_multi_coalesce.c>
6192 #include <isl_multi_gist.c>
6193 #include <isl_multi_intersect.c>
6195 /* Scale the elements of "pma" by the corresponding elements of "mv".
6197 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6198 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6200 int i;
6202 pma = isl_pw_multi_aff_cow(pma);
6203 if (!pma || !mv)
6204 goto error;
6205 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6206 mv->space, isl_dim_set))
6207 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6208 "spaces don't match", goto error);
6209 if (!isl_space_match(pma->dim, isl_dim_param,
6210 mv->space, isl_dim_param)) {
6211 pma = isl_pw_multi_aff_align_params(pma,
6212 isl_multi_val_get_space(mv));
6213 mv = isl_multi_val_align_params(mv,
6214 isl_pw_multi_aff_get_space(pma));
6215 if (!pma || !mv)
6216 goto error;
6219 for (i = 0; i < pma->n; ++i) {
6220 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6221 isl_multi_val_copy(mv));
6222 if (!pma->p[i].maff)
6223 goto error;
6226 isl_multi_val_free(mv);
6227 return pma;
6228 error:
6229 isl_multi_val_free(mv);
6230 isl_pw_multi_aff_free(pma);
6231 return NULL;
6234 /* This function is called for each entry of an isl_union_pw_multi_aff.
6235 * If the space of the entry matches that of data->mv,
6236 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6237 * Otherwise, return an empty isl_pw_multi_aff.
6239 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6240 __isl_take isl_pw_multi_aff *pma, void *user)
6242 isl_multi_val *mv = user;
6244 if (!pma)
6245 return NULL;
6246 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6247 mv->space, isl_dim_set)) {
6248 isl_space *space = isl_pw_multi_aff_get_space(pma);
6249 isl_pw_multi_aff_free(pma);
6250 return isl_pw_multi_aff_empty(space);
6253 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6256 /* Scale the elements of "upma" by the corresponding elements of "mv",
6257 * for those entries that match the space of "mv".
6259 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6260 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6262 upma = isl_union_pw_multi_aff_align_params(upma,
6263 isl_multi_val_get_space(mv));
6264 mv = isl_multi_val_align_params(mv,
6265 isl_union_pw_multi_aff_get_space(upma));
6266 if (!upma || !mv)
6267 goto error;
6269 return isl_union_pw_multi_aff_transform(upma,
6270 &union_pw_multi_aff_scale_multi_val_entry, mv);
6272 isl_multi_val_free(mv);
6273 return upma;
6274 error:
6275 isl_multi_val_free(mv);
6276 isl_union_pw_multi_aff_free(upma);
6277 return NULL;
6280 /* Construct and return a piecewise multi affine expression
6281 * in the given space with value zero in each of the output dimensions and
6282 * a universe domain.
6284 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6286 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6289 /* Construct and return a piecewise multi affine expression
6290 * that is equal to the given piecewise affine expression.
6292 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6293 __isl_take isl_pw_aff *pa)
6295 int i;
6296 isl_space *space;
6297 isl_pw_multi_aff *pma;
6299 if (!pa)
6300 return NULL;
6302 space = isl_pw_aff_get_space(pa);
6303 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6305 for (i = 0; i < pa->n; ++i) {
6306 isl_set *set;
6307 isl_multi_aff *ma;
6309 set = isl_set_copy(pa->p[i].set);
6310 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6311 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6314 isl_pw_aff_free(pa);
6315 return pma;
6318 /* Construct a set or map mapping the shared (parameter) domain
6319 * of the piecewise affine expressions to the range of "mpa"
6320 * with each dimension in the range equated to the
6321 * corresponding piecewise affine expression.
6323 static __isl_give isl_map *map_from_multi_pw_aff(
6324 __isl_take isl_multi_pw_aff *mpa)
6326 int i;
6327 isl_space *space;
6328 isl_map *map;
6330 if (!mpa)
6331 return NULL;
6333 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6334 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6335 "invalid space", goto error);
6337 space = isl_multi_pw_aff_get_domain_space(mpa);
6338 map = isl_map_universe(isl_space_from_domain(space));
6340 for (i = 0; i < mpa->n; ++i) {
6341 isl_pw_aff *pa;
6342 isl_map *map_i;
6344 pa = isl_pw_aff_copy(mpa->p[i]);
6345 map_i = map_from_pw_aff(pa);
6347 map = isl_map_flat_range_product(map, map_i);
6350 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6352 isl_multi_pw_aff_free(mpa);
6353 return map;
6354 error:
6355 isl_multi_pw_aff_free(mpa);
6356 return NULL;
6359 /* Construct a map mapping the shared domain
6360 * of the piecewise affine expressions to the range of "mpa"
6361 * with each dimension in the range equated to the
6362 * corresponding piecewise affine expression.
6364 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6366 if (!mpa)
6367 return NULL;
6368 if (isl_space_is_set(mpa->space))
6369 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6370 "space of input is not a map", goto error);
6372 return map_from_multi_pw_aff(mpa);
6373 error:
6374 isl_multi_pw_aff_free(mpa);
6375 return NULL;
6378 /* Construct a set mapping the shared parameter domain
6379 * of the piecewise affine expressions to the space of "mpa"
6380 * with each dimension in the range equated to the
6381 * corresponding piecewise affine expression.
6383 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6385 if (!mpa)
6386 return NULL;
6387 if (!isl_space_is_set(mpa->space))
6388 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6389 "space of input is not a set", goto error);
6391 return map_from_multi_pw_aff(mpa);
6392 error:
6393 isl_multi_pw_aff_free(mpa);
6394 return NULL;
6397 /* Construct and return a piecewise multi affine expression
6398 * that is equal to the given multi piecewise affine expression
6399 * on the shared domain of the piecewise affine expressions.
6401 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6402 __isl_take isl_multi_pw_aff *mpa)
6404 int i;
6405 isl_space *space;
6406 isl_pw_aff *pa;
6407 isl_pw_multi_aff *pma;
6409 if (!mpa)
6410 return NULL;
6412 space = isl_multi_pw_aff_get_space(mpa);
6414 if (mpa->n == 0) {
6415 isl_multi_pw_aff_free(mpa);
6416 return isl_pw_multi_aff_zero(space);
6419 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6420 pma = isl_pw_multi_aff_from_pw_aff(pa);
6422 for (i = 1; i < mpa->n; ++i) {
6423 isl_pw_multi_aff *pma_i;
6425 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6426 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6427 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6430 pma = isl_pw_multi_aff_reset_space(pma, space);
6432 isl_multi_pw_aff_free(mpa);
6433 return pma;
6436 /* Construct and return a multi piecewise affine expression
6437 * that is equal to the given multi affine expression.
6439 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6440 __isl_take isl_multi_aff *ma)
6442 int i, n;
6443 isl_multi_pw_aff *mpa;
6445 if (!ma)
6446 return NULL;
6448 n = isl_multi_aff_dim(ma, isl_dim_out);
6449 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6451 for (i = 0; i < n; ++i) {
6452 isl_pw_aff *pa;
6454 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6455 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6458 isl_multi_aff_free(ma);
6459 return mpa;
6462 /* Construct and return a multi piecewise affine expression
6463 * that is equal to the given piecewise multi affine expression.
6465 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6466 __isl_take isl_pw_multi_aff *pma)
6468 int i, n;
6469 isl_space *space;
6470 isl_multi_pw_aff *mpa;
6472 if (!pma)
6473 return NULL;
6475 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6476 space = isl_pw_multi_aff_get_space(pma);
6477 mpa = isl_multi_pw_aff_alloc(space);
6479 for (i = 0; i < n; ++i) {
6480 isl_pw_aff *pa;
6482 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6483 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6486 isl_pw_multi_aff_free(pma);
6487 return mpa;
6490 /* Do "pa1" and "pa2" represent the same function?
6492 * We first check if they are obviously equal.
6493 * If not, we convert them to maps and check if those are equal.
6495 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6497 int equal;
6498 isl_map *map1, *map2;
6500 if (!pa1 || !pa2)
6501 return -1;
6503 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6504 if (equal < 0 || equal)
6505 return equal;
6507 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6508 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6509 equal = isl_map_is_equal(map1, map2);
6510 isl_map_free(map1);
6511 isl_map_free(map2);
6513 return equal;
6516 /* Do "mpa1" and "mpa2" represent the same function?
6518 * Note that we cannot convert the entire isl_multi_pw_aff
6519 * to a map because the domains of the piecewise affine expressions
6520 * may not be the same.
6522 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6523 __isl_keep isl_multi_pw_aff *mpa2)
6525 int i;
6526 isl_bool equal;
6528 if (!mpa1 || !mpa2)
6529 return isl_bool_error;
6531 if (!isl_space_match(mpa1->space, isl_dim_param,
6532 mpa2->space, isl_dim_param)) {
6533 if (!isl_space_has_named_params(mpa1->space))
6534 return isl_bool_false;
6535 if (!isl_space_has_named_params(mpa2->space))
6536 return isl_bool_false;
6537 mpa1 = isl_multi_pw_aff_copy(mpa1);
6538 mpa2 = isl_multi_pw_aff_copy(mpa2);
6539 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6540 isl_multi_pw_aff_get_space(mpa2));
6541 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6542 isl_multi_pw_aff_get_space(mpa1));
6543 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6544 isl_multi_pw_aff_free(mpa1);
6545 isl_multi_pw_aff_free(mpa2);
6546 return equal;
6549 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6550 if (equal < 0 || !equal)
6551 return equal;
6553 for (i = 0; i < mpa1->n; ++i) {
6554 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6555 if (equal < 0 || !equal)
6556 return equal;
6559 return isl_bool_true;
6562 /* Compute the pullback of "mpa" by the function represented by "ma".
6563 * In other words, plug in "ma" in "mpa".
6565 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6567 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6568 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6570 int i;
6571 isl_space *space = NULL;
6573 mpa = isl_multi_pw_aff_cow(mpa);
6574 if (!mpa || !ma)
6575 goto error;
6577 space = isl_space_join(isl_multi_aff_get_space(ma),
6578 isl_multi_pw_aff_get_space(mpa));
6579 if (!space)
6580 goto error;
6582 for (i = 0; i < mpa->n; ++i) {
6583 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6584 isl_multi_aff_copy(ma));
6585 if (!mpa->p[i])
6586 goto error;
6589 isl_multi_aff_free(ma);
6590 isl_space_free(mpa->space);
6591 mpa->space = space;
6592 return mpa;
6593 error:
6594 isl_space_free(space);
6595 isl_multi_pw_aff_free(mpa);
6596 isl_multi_aff_free(ma);
6597 return NULL;
6600 /* Compute the pullback of "mpa" by the function represented by "ma".
6601 * In other words, plug in "ma" in "mpa".
6603 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6604 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6606 if (!mpa || !ma)
6607 goto error;
6608 if (isl_space_match(mpa->space, isl_dim_param,
6609 ma->space, isl_dim_param))
6610 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6611 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6612 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6613 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6614 error:
6615 isl_multi_pw_aff_free(mpa);
6616 isl_multi_aff_free(ma);
6617 return NULL;
6620 /* Compute the pullback of "mpa" by the function represented by "pma".
6621 * In other words, plug in "pma" in "mpa".
6623 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6625 static __isl_give isl_multi_pw_aff *
6626 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6627 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6629 int i;
6630 isl_space *space = NULL;
6632 mpa = isl_multi_pw_aff_cow(mpa);
6633 if (!mpa || !pma)
6634 goto error;
6636 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6637 isl_multi_pw_aff_get_space(mpa));
6639 for (i = 0; i < mpa->n; ++i) {
6640 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6641 isl_pw_multi_aff_copy(pma));
6642 if (!mpa->p[i])
6643 goto error;
6646 isl_pw_multi_aff_free(pma);
6647 isl_space_free(mpa->space);
6648 mpa->space = space;
6649 return mpa;
6650 error:
6651 isl_space_free(space);
6652 isl_multi_pw_aff_free(mpa);
6653 isl_pw_multi_aff_free(pma);
6654 return NULL;
6657 /* Compute the pullback of "mpa" by the function represented by "pma".
6658 * In other words, plug in "pma" in "mpa".
6660 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6661 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6663 if (!mpa || !pma)
6664 goto error;
6665 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6666 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6667 mpa = isl_multi_pw_aff_align_params(mpa,
6668 isl_pw_multi_aff_get_space(pma));
6669 pma = isl_pw_multi_aff_align_params(pma,
6670 isl_multi_pw_aff_get_space(mpa));
6671 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6672 error:
6673 isl_multi_pw_aff_free(mpa);
6674 isl_pw_multi_aff_free(pma);
6675 return NULL;
6678 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6679 * with the domain of "aff". The domain of the result is the same
6680 * as that of "mpa".
6681 * "mpa" and "aff" are assumed to have been aligned.
6683 * We first extract the parametric constant from "aff", defined
6684 * over the correct domain.
6685 * Then we add the appropriate combinations of the members of "mpa".
6686 * Finally, we add the integer divisions through recursive calls.
6688 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6689 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6691 int i, n_in, n_div;
6692 isl_space *space;
6693 isl_val *v;
6694 isl_pw_aff *pa;
6695 isl_aff *tmp;
6697 n_in = isl_aff_dim(aff, isl_dim_in);
6698 n_div = isl_aff_dim(aff, isl_dim_div);
6700 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6701 tmp = isl_aff_copy(aff);
6702 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6703 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6704 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6705 isl_space_dim(space, isl_dim_set));
6706 tmp = isl_aff_reset_domain_space(tmp, space);
6707 pa = isl_pw_aff_from_aff(tmp);
6709 for (i = 0; i < n_in; ++i) {
6710 isl_pw_aff *pa_i;
6712 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6713 continue;
6714 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6715 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6716 pa_i = isl_pw_aff_scale_val(pa_i, v);
6717 pa = isl_pw_aff_add(pa, pa_i);
6720 for (i = 0; i < n_div; ++i) {
6721 isl_aff *div;
6722 isl_pw_aff *pa_i;
6724 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6725 continue;
6726 div = isl_aff_get_div(aff, i);
6727 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6728 isl_multi_pw_aff_copy(mpa), div);
6729 pa_i = isl_pw_aff_floor(pa_i);
6730 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6731 pa_i = isl_pw_aff_scale_val(pa_i, v);
6732 pa = isl_pw_aff_add(pa, pa_i);
6735 isl_multi_pw_aff_free(mpa);
6736 isl_aff_free(aff);
6738 return pa;
6741 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6742 * with the domain of "aff". The domain of the result is the same
6743 * as that of "mpa".
6745 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6746 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6748 if (!aff || !mpa)
6749 goto error;
6750 if (isl_space_match(aff->ls->dim, isl_dim_param,
6751 mpa->space, isl_dim_param))
6752 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6754 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6755 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6757 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6758 error:
6759 isl_aff_free(aff);
6760 isl_multi_pw_aff_free(mpa);
6761 return NULL;
6764 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6765 * with the domain of "pa". The domain of the result is the same
6766 * as that of "mpa".
6767 * "mpa" and "pa" are assumed to have been aligned.
6769 * We consider each piece in turn. Note that the domains of the
6770 * pieces are assumed to be disjoint and they remain disjoint
6771 * after taking the preimage (over the same function).
6773 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6774 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6776 isl_space *space;
6777 isl_pw_aff *res;
6778 int i;
6780 if (!mpa || !pa)
6781 goto error;
6783 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6784 isl_pw_aff_get_space(pa));
6785 res = isl_pw_aff_empty(space);
6787 for (i = 0; i < pa->n; ++i) {
6788 isl_pw_aff *pa_i;
6789 isl_set *domain;
6791 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6792 isl_multi_pw_aff_copy(mpa),
6793 isl_aff_copy(pa->p[i].aff));
6794 domain = isl_set_copy(pa->p[i].set);
6795 domain = isl_set_preimage_multi_pw_aff(domain,
6796 isl_multi_pw_aff_copy(mpa));
6797 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6798 res = isl_pw_aff_add_disjoint(res, pa_i);
6801 isl_pw_aff_free(pa);
6802 isl_multi_pw_aff_free(mpa);
6803 return res;
6804 error:
6805 isl_pw_aff_free(pa);
6806 isl_multi_pw_aff_free(mpa);
6807 return NULL;
6810 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6811 * with the domain of "pa". The domain of the result is the same
6812 * as that of "mpa".
6814 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6815 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6817 if (!pa || !mpa)
6818 goto error;
6819 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6820 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6822 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6823 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6825 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6826 error:
6827 isl_pw_aff_free(pa);
6828 isl_multi_pw_aff_free(mpa);
6829 return NULL;
6832 /* Compute the pullback of "pa" by the function represented by "mpa".
6833 * In other words, plug in "mpa" in "pa".
6834 * "pa" and "mpa" are assumed to have been aligned.
6836 * The pullback is computed by applying "pa" to "mpa".
6838 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6839 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6841 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6844 /* Compute the pullback of "pa" by the function represented by "mpa".
6845 * In other words, plug in "mpa" in "pa".
6847 * The pullback is computed by applying "pa" to "mpa".
6849 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6850 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6852 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6855 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6856 * In other words, plug in "mpa2" in "mpa1".
6858 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6860 * We pullback each member of "mpa1" in turn.
6862 static __isl_give isl_multi_pw_aff *
6863 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6864 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6866 int i;
6867 isl_space *space = NULL;
6869 mpa1 = isl_multi_pw_aff_cow(mpa1);
6870 if (!mpa1 || !mpa2)
6871 goto error;
6873 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6874 isl_multi_pw_aff_get_space(mpa1));
6876 for (i = 0; i < mpa1->n; ++i) {
6877 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6878 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6879 if (!mpa1->p[i])
6880 goto error;
6883 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6885 isl_multi_pw_aff_free(mpa2);
6886 return mpa1;
6887 error:
6888 isl_space_free(space);
6889 isl_multi_pw_aff_free(mpa1);
6890 isl_multi_pw_aff_free(mpa2);
6891 return NULL;
6894 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6895 * In other words, plug in "mpa2" in "mpa1".
6897 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6898 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6900 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6901 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6904 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6905 * of "mpa1" and "mpa2" live in the same space, construct map space
6906 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6907 * with this map space as extract argument.
6909 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6910 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6911 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6912 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6914 int match;
6915 isl_space *space1, *space2;
6916 isl_map *res;
6918 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6919 isl_multi_pw_aff_get_space(mpa2));
6920 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6921 isl_multi_pw_aff_get_space(mpa1));
6922 if (!mpa1 || !mpa2)
6923 goto error;
6924 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6925 mpa2->space, isl_dim_out);
6926 if (match < 0)
6927 goto error;
6928 if (!match)
6929 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6930 "range spaces don't match", goto error);
6931 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6932 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6933 space1 = isl_space_map_from_domain_and_range(space1, space2);
6935 res = order(mpa1, mpa2, space1);
6936 isl_multi_pw_aff_free(mpa1);
6937 isl_multi_pw_aff_free(mpa2);
6938 return res;
6939 error:
6940 isl_multi_pw_aff_free(mpa1);
6941 isl_multi_pw_aff_free(mpa2);
6942 return NULL;
6945 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6946 * where the function values are equal. "space" is the space of the result.
6947 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6949 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6950 * in the sequences are equal.
6952 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6953 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6954 __isl_take isl_space *space)
6956 int i, n;
6957 isl_map *res;
6959 res = isl_map_universe(space);
6961 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6962 for (i = 0; i < n; ++i) {
6963 isl_pw_aff *pa1, *pa2;
6964 isl_map *map;
6966 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6967 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6968 map = isl_pw_aff_eq_map(pa1, pa2);
6969 res = isl_map_intersect(res, map);
6972 return res;
6975 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6976 * where the function values are equal.
6978 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6979 __isl_take isl_multi_pw_aff *mpa2)
6981 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6982 &isl_multi_pw_aff_eq_map_on_space);
6985 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6986 * where the function values of "mpa1" is lexicographically satisfies "base"
6987 * compared to that of "mpa2". "space" is the space of the result.
6988 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6990 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6991 * if its i-th element satisfies "base" when compared to
6992 * the i-th element of "mpa2" while all previous elements are
6993 * pairwise equal.
6995 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6996 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6997 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6998 __isl_take isl_pw_aff *pa2),
6999 __isl_take isl_space *space)
7001 int i, n;
7002 isl_map *res, *rest;
7004 res = isl_map_empty(isl_space_copy(space));
7005 rest = isl_map_universe(space);
7007 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7008 for (i = 0; i < n; ++i) {
7009 isl_pw_aff *pa1, *pa2;
7010 isl_map *map;
7012 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7013 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7014 map = base(pa1, pa2);
7015 map = isl_map_intersect(map, isl_map_copy(rest));
7016 res = isl_map_union(res, map);
7018 if (i == n - 1)
7019 continue;
7021 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7022 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7023 map = isl_pw_aff_eq_map(pa1, pa2);
7024 rest = isl_map_intersect(rest, map);
7027 isl_map_free(rest);
7028 return res;
7031 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7032 * where the function value of "mpa1" is lexicographically less than that
7033 * of "mpa2". "space" is the space of the result.
7034 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7036 * "mpa1" is less than "mpa2" if its i-th element is smaller
7037 * than the i-th element of "mpa2" while all previous elements are
7038 * pairwise equal.
7040 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7041 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7042 __isl_take isl_space *space)
7044 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7045 &isl_pw_aff_lt_map, space);
7048 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7049 * where the function value of "mpa1" is lexicographically less than that
7050 * of "mpa2".
7052 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7053 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7055 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7056 &isl_multi_pw_aff_lex_lt_map_on_space);
7059 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7060 * where the function value of "mpa1" is lexicographically greater than that
7061 * of "mpa2". "space" is the space of the result.
7062 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7064 * "mpa1" is greater than "mpa2" if its i-th element is greater
7065 * than the i-th element of "mpa2" while all previous elements are
7066 * pairwise equal.
7068 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7069 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7070 __isl_take isl_space *space)
7072 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7073 &isl_pw_aff_gt_map, space);
7076 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7077 * where the function value of "mpa1" is lexicographically greater than that
7078 * of "mpa2".
7080 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7081 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7083 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7084 &isl_multi_pw_aff_lex_gt_map_on_space);
7087 /* Compare two isl_affs.
7089 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7090 * than "aff2" and 0 if they are equal.
7092 * The order is fairly arbitrary. We do consider expressions that only involve
7093 * earlier dimensions as "smaller".
7095 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7097 int cmp;
7098 int last1, last2;
7100 if (aff1 == aff2)
7101 return 0;
7103 if (!aff1)
7104 return -1;
7105 if (!aff2)
7106 return 1;
7108 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7109 if (cmp != 0)
7110 return cmp;
7112 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7113 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7114 if (last1 != last2)
7115 return last1 - last2;
7117 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7120 /* Compare two isl_pw_affs.
7122 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7123 * than "pa2" and 0 if they are equal.
7125 * The order is fairly arbitrary. We do consider expressions that only involve
7126 * earlier dimensions as "smaller".
7128 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7129 __isl_keep isl_pw_aff *pa2)
7131 int i;
7132 int cmp;
7134 if (pa1 == pa2)
7135 return 0;
7137 if (!pa1)
7138 return -1;
7139 if (!pa2)
7140 return 1;
7142 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7143 if (cmp != 0)
7144 return cmp;
7146 if (pa1->n != pa2->n)
7147 return pa1->n - pa2->n;
7149 for (i = 0; i < pa1->n; ++i) {
7150 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7151 if (cmp != 0)
7152 return cmp;
7153 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7154 if (cmp != 0)
7155 return cmp;
7158 return 0;
7161 /* Return a piecewise affine expression that is equal to "v" on "domain".
7163 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7164 __isl_take isl_val *v)
7166 isl_space *space;
7167 isl_local_space *ls;
7168 isl_aff *aff;
7170 space = isl_set_get_space(domain);
7171 ls = isl_local_space_from_space(space);
7172 aff = isl_aff_val_on_domain(ls, v);
7174 return isl_pw_aff_alloc(domain, aff);
7177 /* Return a multi affine expression that is equal to "mv" on domain
7178 * space "space".
7180 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7181 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7183 int i, n;
7184 isl_space *space2;
7185 isl_local_space *ls;
7186 isl_multi_aff *ma;
7188 if (!space || !mv)
7189 goto error;
7191 n = isl_multi_val_dim(mv, isl_dim_set);
7192 space2 = isl_multi_val_get_space(mv);
7193 space2 = isl_space_align_params(space2, isl_space_copy(space));
7194 space = isl_space_align_params(space, isl_space_copy(space2));
7195 space = isl_space_map_from_domain_and_range(space, space2);
7196 ma = isl_multi_aff_alloc(isl_space_copy(space));
7197 ls = isl_local_space_from_space(isl_space_domain(space));
7198 for (i = 0; i < n; ++i) {
7199 isl_val *v;
7200 isl_aff *aff;
7202 v = isl_multi_val_get_val(mv, i);
7203 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7204 ma = isl_multi_aff_set_aff(ma, i, aff);
7206 isl_local_space_free(ls);
7208 isl_multi_val_free(mv);
7209 return ma;
7210 error:
7211 isl_space_free(space);
7212 isl_multi_val_free(mv);
7213 return NULL;
7216 /* Return a piecewise multi-affine expression
7217 * that is equal to "mv" on "domain".
7219 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7220 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7222 isl_space *space;
7223 isl_multi_aff *ma;
7225 space = isl_set_get_space(domain);
7226 ma = isl_multi_aff_multi_val_on_space(space, mv);
7228 return isl_pw_multi_aff_alloc(domain, ma);
7231 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7232 * mv is the value that should be attained on each domain set
7233 * res collects the results
7235 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7236 isl_multi_val *mv;
7237 isl_union_pw_multi_aff *res;
7240 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7241 * and add it to data->res.
7243 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7244 void *user)
7246 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7247 isl_pw_multi_aff *pma;
7248 isl_multi_val *mv;
7250 mv = isl_multi_val_copy(data->mv);
7251 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7252 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7254 return data->res ? isl_stat_ok : isl_stat_error;
7257 /* Return a union piecewise multi-affine expression
7258 * that is equal to "mv" on "domain".
7260 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7261 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7263 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7264 isl_space *space;
7266 space = isl_union_set_get_space(domain);
7267 data.res = isl_union_pw_multi_aff_empty(space);
7268 data.mv = mv;
7269 if (isl_union_set_foreach_set(domain,
7270 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7271 data.res = isl_union_pw_multi_aff_free(data.res);
7272 isl_union_set_free(domain);
7273 isl_multi_val_free(mv);
7274 return data.res;
7277 /* Compute the pullback of data->pma by the function represented by "pma2",
7278 * provided the spaces match, and add the results to data->res.
7280 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7282 struct isl_union_pw_multi_aff_bin_data *data = user;
7284 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7285 pma2->dim, isl_dim_out)) {
7286 isl_pw_multi_aff_free(pma2);
7287 return isl_stat_ok;
7290 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7291 isl_pw_multi_aff_copy(data->pma), pma2);
7293 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7294 if (!data->res)
7295 return isl_stat_error;
7297 return isl_stat_ok;
7300 /* Compute the pullback of "upma1" by the function represented by "upma2".
7302 __isl_give isl_union_pw_multi_aff *
7303 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7304 __isl_take isl_union_pw_multi_aff *upma1,
7305 __isl_take isl_union_pw_multi_aff *upma2)
7307 return bin_op(upma1, upma2, &pullback_entry);
7310 /* Check that the domain space of "upa" matches "space".
7312 * Return 0 on success and -1 on error.
7314 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7315 * can in principle never fail since the space "space" is that
7316 * of the isl_multi_union_pw_aff and is a set space such that
7317 * there is no domain space to match.
7319 * We check the parameters and double-check that "space" is
7320 * indeed that of a set.
7322 static int isl_union_pw_aff_check_match_domain_space(
7323 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7325 isl_space *upa_space;
7326 int match;
7328 if (!upa || !space)
7329 return -1;
7331 match = isl_space_is_set(space);
7332 if (match < 0)
7333 return -1;
7334 if (!match)
7335 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7336 "expecting set space", return -1);
7338 upa_space = isl_union_pw_aff_get_space(upa);
7339 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7340 if (match < 0)
7341 goto error;
7342 if (!match)
7343 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7344 "parameters don't match", goto error);
7346 isl_space_free(upa_space);
7347 return 0;
7348 error:
7349 isl_space_free(upa_space);
7350 return -1;
7353 /* Do the parameters of "upa" match those of "space"?
7355 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7356 __isl_keep isl_space *space)
7358 isl_space *upa_space;
7359 int match;
7361 if (!upa || !space)
7362 return -1;
7364 upa_space = isl_union_pw_aff_get_space(upa);
7366 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7368 isl_space_free(upa_space);
7369 return match;
7372 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7373 * space represents the new parameters.
7374 * res collects the results.
7376 struct isl_union_pw_aff_reset_params_data {
7377 isl_space *space;
7378 isl_union_pw_aff *res;
7381 /* Replace the parameters of "pa" by data->space and
7382 * add the result to data->res.
7384 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7386 struct isl_union_pw_aff_reset_params_data *data = user;
7387 isl_space *space;
7389 space = isl_pw_aff_get_space(pa);
7390 space = isl_space_replace(space, isl_dim_param, data->space);
7391 pa = isl_pw_aff_reset_space(pa, space);
7392 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7394 return data->res ? isl_stat_ok : isl_stat_error;
7397 /* Replace the domain space of "upa" by "space".
7398 * Since a union expression does not have a (single) domain space,
7399 * "space" is necessarily a parameter space.
7401 * Since the order and the names of the parameters determine
7402 * the hash value, we need to create a new hash table.
7404 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7405 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7407 struct isl_union_pw_aff_reset_params_data data = { space };
7408 int match;
7410 match = isl_union_pw_aff_matching_params(upa, space);
7411 if (match < 0)
7412 upa = isl_union_pw_aff_free(upa);
7413 else if (match) {
7414 isl_space_free(space);
7415 return upa;
7418 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7419 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7420 data.res = isl_union_pw_aff_free(data.res);
7422 isl_union_pw_aff_free(upa);
7423 isl_space_free(space);
7424 return data.res;
7427 /* Return the floor of "pa".
7429 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7431 return isl_pw_aff_floor(pa);
7434 /* Given f, return floor(f).
7436 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7437 __isl_take isl_union_pw_aff *upa)
7439 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7442 /* Compute
7444 * upa mod m = upa - m * floor(upa/m)
7446 * with m an integer value.
7448 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7449 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7451 isl_union_pw_aff *res;
7453 if (!upa || !m)
7454 goto error;
7456 if (!isl_val_is_int(m))
7457 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7458 "expecting integer modulo", goto error);
7459 if (!isl_val_is_pos(m))
7460 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7461 "expecting positive modulo", goto error);
7463 res = isl_union_pw_aff_copy(upa);
7464 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7465 upa = isl_union_pw_aff_floor(upa);
7466 upa = isl_union_pw_aff_scale_val(upa, m);
7467 res = isl_union_pw_aff_sub(res, upa);
7469 return res;
7470 error:
7471 isl_val_free(m);
7472 isl_union_pw_aff_free(upa);
7473 return NULL;
7476 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7477 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7478 * needs to attain.
7479 * "res" collects the results.
7481 struct isl_union_pw_aff_aff_on_domain_data {
7482 isl_aff *aff;
7483 isl_union_pw_aff *res;
7486 /* Construct a piecewise affine expression that is equal to data->aff
7487 * on "domain" and add the result to data->res.
7489 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7491 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7492 isl_pw_aff *pa;
7493 isl_aff *aff;
7494 int dim;
7496 aff = isl_aff_copy(data->aff);
7497 dim = isl_set_dim(domain, isl_dim_set);
7498 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7499 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7500 pa = isl_pw_aff_alloc(domain, aff);
7501 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7503 return data->res ? isl_stat_ok : isl_stat_error;
7506 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7507 * pos is the output position that needs to be extracted.
7508 * res collects the results.
7510 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7511 int pos;
7512 isl_union_pw_aff *res;
7515 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7516 * (assuming it has such a dimension) and add it to data->res.
7518 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7520 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7521 int n_out;
7522 isl_pw_aff *pa;
7524 if (!pma)
7525 return isl_stat_error;
7527 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7528 if (data->pos >= n_out) {
7529 isl_pw_multi_aff_free(pma);
7530 return isl_stat_ok;
7533 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7534 isl_pw_multi_aff_free(pma);
7536 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7538 return data->res ? isl_stat_ok : isl_stat_error;
7541 /* Extract an isl_union_pw_aff corresponding to
7542 * output dimension "pos" of "upma".
7544 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7545 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7547 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7548 isl_space *space;
7550 if (!upma)
7551 return NULL;
7553 if (pos < 0)
7554 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7555 "cannot extract at negative position", return NULL);
7557 space = isl_union_pw_multi_aff_get_space(upma);
7558 data.res = isl_union_pw_aff_empty(space);
7559 data.pos = pos;
7560 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7561 &get_union_pw_aff, &data) < 0)
7562 data.res = isl_union_pw_aff_free(data.res);
7564 return data.res;
7567 /* Return a union piecewise affine expression
7568 * that is equal to "aff" on "domain".
7570 * Construct an isl_pw_aff on each of the sets in "domain" and
7571 * collect the results.
7573 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7574 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7576 struct isl_union_pw_aff_aff_on_domain_data data;
7577 isl_space *space;
7579 if (!domain || !aff)
7580 goto error;
7581 if (!isl_local_space_is_params(aff->ls))
7582 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7583 "expecting parametric expression", goto error);
7585 space = isl_union_set_get_space(domain);
7586 data.res = isl_union_pw_aff_empty(space);
7587 data.aff = aff;
7588 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7589 data.res = isl_union_pw_aff_free(data.res);
7590 isl_union_set_free(domain);
7591 isl_aff_free(aff);
7592 return data.res;
7593 error:
7594 isl_union_set_free(domain);
7595 isl_aff_free(aff);
7596 return NULL;
7599 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7600 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7601 * "res" collects the results.
7603 struct isl_union_pw_aff_val_on_domain_data {
7604 isl_val *v;
7605 isl_union_pw_aff *res;
7608 /* Construct a piecewise affine expression that is equal to data->v
7609 * on "domain" and add the result to data->res.
7611 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7613 struct isl_union_pw_aff_val_on_domain_data *data = user;
7614 isl_pw_aff *pa;
7615 isl_val *v;
7617 v = isl_val_copy(data->v);
7618 pa = isl_pw_aff_val_on_domain(domain, v);
7619 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7621 return data->res ? isl_stat_ok : isl_stat_error;
7624 /* Return a union piecewise affine expression
7625 * that is equal to "v" on "domain".
7627 * Construct an isl_pw_aff on each of the sets in "domain" and
7628 * collect the results.
7630 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7631 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7633 struct isl_union_pw_aff_val_on_domain_data data;
7634 isl_space *space;
7636 space = isl_union_set_get_space(domain);
7637 data.res = isl_union_pw_aff_empty(space);
7638 data.v = v;
7639 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7640 data.res = isl_union_pw_aff_free(data.res);
7641 isl_union_set_free(domain);
7642 isl_val_free(v);
7643 return data.res;
7646 /* Construct a piecewise multi affine expression
7647 * that is equal to "pa" and add it to upma.
7649 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7650 void *user)
7652 isl_union_pw_multi_aff **upma = user;
7653 isl_pw_multi_aff *pma;
7655 pma = isl_pw_multi_aff_from_pw_aff(pa);
7656 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7658 return *upma ? isl_stat_ok : isl_stat_error;
7661 /* Construct and return a union piecewise multi affine expression
7662 * that is equal to the given union piecewise affine expression.
7664 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7665 __isl_take isl_union_pw_aff *upa)
7667 isl_space *space;
7668 isl_union_pw_multi_aff *upma;
7670 if (!upa)
7671 return NULL;
7673 space = isl_union_pw_aff_get_space(upa);
7674 upma = isl_union_pw_multi_aff_empty(space);
7676 if (isl_union_pw_aff_foreach_pw_aff(upa,
7677 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7678 upma = isl_union_pw_multi_aff_free(upma);
7680 isl_union_pw_aff_free(upa);
7681 return upma;
7684 /* Compute the set of elements in the domain of "pa" where it is zero and
7685 * add this set to "uset".
7687 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7689 isl_union_set **uset = (isl_union_set **)user;
7691 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7693 return *uset ? isl_stat_ok : isl_stat_error;
7696 /* Return a union set containing those elements in the domain
7697 * of "upa" where it is zero.
7699 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7700 __isl_take isl_union_pw_aff *upa)
7702 isl_union_set *zero;
7704 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7705 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7706 zero = isl_union_set_free(zero);
7708 isl_union_pw_aff_free(upa);
7709 return zero;
7712 /* Convert "pa" to an isl_map and add it to *umap.
7714 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7716 isl_union_map **umap = user;
7717 isl_map *map;
7719 map = isl_map_from_pw_aff(pa);
7720 *umap = isl_union_map_add_map(*umap, map);
7722 return *umap ? isl_stat_ok : isl_stat_error;
7725 /* Construct a union map mapping the domain of the union
7726 * piecewise affine expression to its range, with the single output dimension
7727 * equated to the corresponding affine expressions on their cells.
7729 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7730 __isl_take isl_union_pw_aff *upa)
7732 isl_space *space;
7733 isl_union_map *umap;
7735 if (!upa)
7736 return NULL;
7738 space = isl_union_pw_aff_get_space(upa);
7739 umap = isl_union_map_empty(space);
7741 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7742 &umap) < 0)
7743 umap = isl_union_map_free(umap);
7745 isl_union_pw_aff_free(upa);
7746 return umap;
7749 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7750 * upma is the function that is plugged in.
7751 * pa is the current part of the function in which upma is plugged in.
7752 * res collects the results.
7754 struct isl_union_pw_aff_pullback_upma_data {
7755 isl_union_pw_multi_aff *upma;
7756 isl_pw_aff *pa;
7757 isl_union_pw_aff *res;
7760 /* Check if "pma" can be plugged into data->pa.
7761 * If so, perform the pullback and add the result to data->res.
7763 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7765 struct isl_union_pw_aff_pullback_upma_data *data = user;
7766 isl_pw_aff *pa;
7768 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7769 pma->dim, isl_dim_out)) {
7770 isl_pw_multi_aff_free(pma);
7771 return isl_stat_ok;
7774 pa = isl_pw_aff_copy(data->pa);
7775 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7777 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7779 return data->res ? isl_stat_ok : isl_stat_error;
7782 /* Check if any of the elements of data->upma can be plugged into pa,
7783 * add if so add the result to data->res.
7785 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7787 struct isl_union_pw_aff_pullback_upma_data *data = user;
7788 isl_stat r;
7790 data->pa = pa;
7791 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7792 &pa_pb_pma, data);
7793 isl_pw_aff_free(pa);
7795 return r;
7798 /* Compute the pullback of "upa" by the function represented by "upma".
7799 * In other words, plug in "upma" in "upa". The result contains
7800 * expressions defined over the domain space of "upma".
7802 * Run over all pairs of elements in "upa" and "upma", perform
7803 * the pullback when appropriate and collect the results.
7804 * If the hash value were based on the domain space rather than
7805 * the function space, then we could run through all elements
7806 * of "upma" and directly pick out the corresponding element of "upa".
7808 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7809 __isl_take isl_union_pw_aff *upa,
7810 __isl_take isl_union_pw_multi_aff *upma)
7812 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7813 isl_space *space;
7815 space = isl_union_pw_multi_aff_get_space(upma);
7816 upa = isl_union_pw_aff_align_params(upa, space);
7817 space = isl_union_pw_aff_get_space(upa);
7818 upma = isl_union_pw_multi_aff_align_params(upma, space);
7820 if (!upa || !upma)
7821 goto error;
7823 data.upma = upma;
7824 data.res = isl_union_pw_aff_alloc_same_size(upa);
7825 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7826 data.res = isl_union_pw_aff_free(data.res);
7828 isl_union_pw_aff_free(upa);
7829 isl_union_pw_multi_aff_free(upma);
7830 return data.res;
7831 error:
7832 isl_union_pw_aff_free(upa);
7833 isl_union_pw_multi_aff_free(upma);
7834 return NULL;
7837 #undef BASE
7838 #define BASE union_pw_aff
7839 #undef DOMBASE
7840 #define DOMBASE union_set
7842 #define NO_MOVE_DIMS
7843 #define NO_DIMS
7844 #define NO_DOMAIN
7845 #define NO_PRODUCT
7846 #define NO_SPLICE
7847 #define NO_ZERO
7848 #define NO_IDENTITY
7849 #define NO_GIST
7851 #include <isl_multi_templ.c>
7852 #include <isl_multi_apply_set.c>
7853 #include <isl_multi_apply_union_set.c>
7854 #include <isl_multi_coalesce.c>
7855 #include <isl_multi_floor.c>
7856 #include <isl_multi_gist.c>
7857 #include <isl_multi_intersect.c>
7859 /* Construct a multiple union piecewise affine expression
7860 * in the given space with value zero in each of the output dimensions.
7862 * Since there is no canonical zero value for
7863 * a union piecewise affine expression, we can only construct
7864 * zero-dimensional "zero" value.
7866 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7867 __isl_take isl_space *space)
7869 if (!space)
7870 return NULL;
7872 if (!isl_space_is_set(space))
7873 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7874 "expecting set space", goto error);
7875 if (isl_space_dim(space , isl_dim_out) != 0)
7876 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7877 "expecting 0D space", goto error);
7879 return isl_multi_union_pw_aff_alloc(space);
7880 error:
7881 isl_space_free(space);
7882 return NULL;
7885 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7886 * with the actual sum on the shared domain and
7887 * the defined expression on the symmetric difference of the domains.
7889 * We simply iterate over the elements in both arguments and
7890 * call isl_union_pw_aff_union_add on each of them.
7892 static __isl_give isl_multi_union_pw_aff *
7893 isl_multi_union_pw_aff_union_add_aligned(
7894 __isl_take isl_multi_union_pw_aff *mupa1,
7895 __isl_take isl_multi_union_pw_aff *mupa2)
7897 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7898 &isl_union_pw_aff_union_add);
7901 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7902 * with the actual sum on the shared domain and
7903 * the defined expression on the symmetric difference of the domains.
7905 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7906 __isl_take isl_multi_union_pw_aff *mupa1,
7907 __isl_take isl_multi_union_pw_aff *mupa2)
7909 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7910 &isl_multi_union_pw_aff_union_add_aligned);
7913 /* Construct and return a multi union piecewise affine expression
7914 * that is equal to the given multi affine expression.
7916 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7917 __isl_take isl_multi_aff *ma)
7919 isl_multi_pw_aff *mpa;
7921 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7922 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7925 /* Construct and return a multi union piecewise affine expression
7926 * that is equal to the given multi piecewise affine expression.
7928 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7929 __isl_take isl_multi_pw_aff *mpa)
7931 int i, n;
7932 isl_space *space;
7933 isl_multi_union_pw_aff *mupa;
7935 if (!mpa)
7936 return NULL;
7938 space = isl_multi_pw_aff_get_space(mpa);
7939 space = isl_space_range(space);
7940 mupa = isl_multi_union_pw_aff_alloc(space);
7942 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7943 for (i = 0; i < n; ++i) {
7944 isl_pw_aff *pa;
7945 isl_union_pw_aff *upa;
7947 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7948 upa = isl_union_pw_aff_from_pw_aff(pa);
7949 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7952 isl_multi_pw_aff_free(mpa);
7954 return mupa;
7957 /* Extract the range space of "pma" and assign it to *space.
7958 * If *space has already been set (through a previous call to this function),
7959 * then check that the range space is the same.
7961 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7963 isl_space **space = user;
7964 isl_space *pma_space;
7965 isl_bool equal;
7967 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7968 isl_pw_multi_aff_free(pma);
7970 if (!pma_space)
7971 return isl_stat_error;
7972 if (!*space) {
7973 *space = pma_space;
7974 return isl_stat_ok;
7977 equal = isl_space_is_equal(pma_space, *space);
7978 isl_space_free(pma_space);
7980 if (equal < 0)
7981 return isl_stat_error;
7982 if (!equal)
7983 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7984 "range spaces not the same", return isl_stat_error);
7985 return isl_stat_ok;
7988 /* Construct and return a multi union piecewise affine expression
7989 * that is equal to the given union piecewise multi affine expression.
7991 * In order to be able to perform the conversion, the input
7992 * needs to be non-empty and may only involve a single range space.
7994 __isl_give isl_multi_union_pw_aff *
7995 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7996 __isl_take isl_union_pw_multi_aff *upma)
7998 isl_space *space = NULL;
7999 isl_multi_union_pw_aff *mupa;
8000 int i, n;
8002 if (!upma)
8003 return NULL;
8004 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8005 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8006 "cannot extract range space from empty input",
8007 goto error);
8008 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8009 &space) < 0)
8010 goto error;
8012 if (!space)
8013 goto error;
8015 n = isl_space_dim(space, isl_dim_set);
8016 mupa = isl_multi_union_pw_aff_alloc(space);
8018 for (i = 0; i < n; ++i) {
8019 isl_union_pw_aff *upa;
8021 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8022 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8025 isl_union_pw_multi_aff_free(upma);
8026 return mupa;
8027 error:
8028 isl_space_free(space);
8029 isl_union_pw_multi_aff_free(upma);
8030 return NULL;
8033 /* Try and create an isl_multi_union_pw_aff that is equivalent
8034 * to the given isl_union_map.
8035 * The isl_union_map is required to be single-valued in each space.
8036 * Moreover, it cannot be empty and all range spaces need to be the same.
8037 * Otherwise, an error is produced.
8039 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8040 __isl_take isl_union_map *umap)
8042 isl_union_pw_multi_aff *upma;
8044 upma = isl_union_pw_multi_aff_from_union_map(umap);
8045 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8048 /* Return a multiple union piecewise affine expression
8049 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8050 * have been aligned.
8052 static __isl_give isl_multi_union_pw_aff *
8053 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8054 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8056 int i, n;
8057 isl_space *space;
8058 isl_multi_union_pw_aff *mupa;
8060 if (!domain || !mv)
8061 goto error;
8063 n = isl_multi_val_dim(mv, isl_dim_set);
8064 space = isl_multi_val_get_space(mv);
8065 mupa = isl_multi_union_pw_aff_alloc(space);
8066 for (i = 0; i < n; ++i) {
8067 isl_val *v;
8068 isl_union_pw_aff *upa;
8070 v = isl_multi_val_get_val(mv, i);
8071 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8073 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8076 isl_union_set_free(domain);
8077 isl_multi_val_free(mv);
8078 return mupa;
8079 error:
8080 isl_union_set_free(domain);
8081 isl_multi_val_free(mv);
8082 return NULL;
8085 /* Return a multiple union piecewise affine expression
8086 * that is equal to "mv" on "domain".
8088 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8089 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8091 if (!domain || !mv)
8092 goto error;
8093 if (isl_space_match(domain->dim, isl_dim_param,
8094 mv->space, isl_dim_param))
8095 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8096 domain, mv);
8097 domain = isl_union_set_align_params(domain,
8098 isl_multi_val_get_space(mv));
8099 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8100 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8101 error:
8102 isl_union_set_free(domain);
8103 isl_multi_val_free(mv);
8104 return NULL;
8107 /* Return a multiple union piecewise affine expression
8108 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8109 * have been aligned.
8111 static __isl_give isl_multi_union_pw_aff *
8112 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8113 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8115 int i, n;
8116 isl_space *space;
8117 isl_multi_union_pw_aff *mupa;
8119 if (!domain || !ma)
8120 goto error;
8122 n = isl_multi_aff_dim(ma, isl_dim_set);
8123 space = isl_multi_aff_get_space(ma);
8124 mupa = isl_multi_union_pw_aff_alloc(space);
8125 for (i = 0; i < n; ++i) {
8126 isl_aff *aff;
8127 isl_union_pw_aff *upa;
8129 aff = isl_multi_aff_get_aff(ma, i);
8130 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8131 aff);
8132 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8135 isl_union_set_free(domain);
8136 isl_multi_aff_free(ma);
8137 return mupa;
8138 error:
8139 isl_union_set_free(domain);
8140 isl_multi_aff_free(ma);
8141 return NULL;
8144 /* Return a multiple union piecewise affine expression
8145 * that is equal to "ma" on "domain".
8147 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8148 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8150 if (!domain || !ma)
8151 goto error;
8152 if (isl_space_match(domain->dim, isl_dim_param,
8153 ma->space, isl_dim_param))
8154 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8155 domain, ma);
8156 domain = isl_union_set_align_params(domain,
8157 isl_multi_aff_get_space(ma));
8158 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8159 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8160 error:
8161 isl_union_set_free(domain);
8162 isl_multi_aff_free(ma);
8163 return NULL;
8166 /* Return a union set containing those elements in the domains
8167 * of the elements of "mupa" where they are all zero.
8169 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8170 __isl_take isl_multi_union_pw_aff *mupa)
8172 int i, n;
8173 isl_union_pw_aff *upa;
8174 isl_union_set *zero;
8176 if (!mupa)
8177 return NULL;
8179 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8180 if (n == 0)
8181 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8182 "cannot determine zero set "
8183 "of zero-dimensional function", goto error);
8185 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8186 zero = isl_union_pw_aff_zero_union_set(upa);
8188 for (i = 1; i < n; ++i) {
8189 isl_union_set *zero_i;
8191 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8192 zero_i = isl_union_pw_aff_zero_union_set(upa);
8194 zero = isl_union_set_intersect(zero, zero_i);
8197 isl_multi_union_pw_aff_free(mupa);
8198 return zero;
8199 error:
8200 isl_multi_union_pw_aff_free(mupa);
8201 return NULL;
8204 /* Construct a union map mapping the shared domain
8205 * of the union piecewise affine expressions to the range of "mupa"
8206 * with each dimension in the range equated to the
8207 * corresponding union piecewise affine expression.
8209 * The input cannot be zero-dimensional as there is
8210 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8212 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8213 __isl_take isl_multi_union_pw_aff *mupa)
8215 int i, n;
8216 isl_space *space;
8217 isl_union_map *umap;
8218 isl_union_pw_aff *upa;
8220 if (!mupa)
8221 return NULL;
8223 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8224 if (n == 0)
8225 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8226 "cannot determine domain of zero-dimensional "
8227 "isl_multi_union_pw_aff", goto error);
8229 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8230 umap = isl_union_map_from_union_pw_aff(upa);
8232 for (i = 1; i < n; ++i) {
8233 isl_union_map *umap_i;
8235 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8236 umap_i = isl_union_map_from_union_pw_aff(upa);
8237 umap = isl_union_map_flat_range_product(umap, umap_i);
8240 space = isl_multi_union_pw_aff_get_space(mupa);
8241 umap = isl_union_map_reset_range_space(umap, space);
8243 isl_multi_union_pw_aff_free(mupa);
8244 return umap;
8245 error:
8246 isl_multi_union_pw_aff_free(mupa);
8247 return NULL;
8250 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8251 * "range" is the space from which to set the range space.
8252 * "res" collects the results.
8254 struct isl_union_pw_multi_aff_reset_range_space_data {
8255 isl_space *range;
8256 isl_union_pw_multi_aff *res;
8259 /* Replace the range space of "pma" by the range space of data->range and
8260 * add the result to data->res.
8262 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8264 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8265 isl_space *space;
8267 space = isl_pw_multi_aff_get_space(pma);
8268 space = isl_space_domain(space);
8269 space = isl_space_extend_domain_with_range(space,
8270 isl_space_copy(data->range));
8271 pma = isl_pw_multi_aff_reset_space(pma, space);
8272 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8274 return data->res ? isl_stat_ok : isl_stat_error;
8277 /* Replace the range space of all the piecewise affine expressions in "upma" by
8278 * the range space of "space".
8280 * This assumes that all these expressions have the same output dimension.
8282 * Since the spaces of the expressions change, so do their hash values.
8283 * We therefore need to create a new isl_union_pw_multi_aff.
8284 * Note that the hash value is currently computed based on the entire
8285 * space even though there can only be a single expression with a given
8286 * domain space.
8288 static __isl_give isl_union_pw_multi_aff *
8289 isl_union_pw_multi_aff_reset_range_space(
8290 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8292 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8293 isl_space *space_upma;
8295 space_upma = isl_union_pw_multi_aff_get_space(upma);
8296 data.res = isl_union_pw_multi_aff_empty(space_upma);
8297 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8298 &reset_range_space, &data) < 0)
8299 data.res = isl_union_pw_multi_aff_free(data.res);
8301 isl_space_free(space);
8302 isl_union_pw_multi_aff_free(upma);
8303 return data.res;
8306 /* Construct and return a union piecewise multi affine expression
8307 * that is equal to the given multi union piecewise affine expression.
8309 * In order to be able to perform the conversion, the input
8310 * needs to have a least one output dimension.
8312 __isl_give isl_union_pw_multi_aff *
8313 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8314 __isl_take isl_multi_union_pw_aff *mupa)
8316 int i, n;
8317 isl_space *space;
8318 isl_union_pw_multi_aff *upma;
8319 isl_union_pw_aff *upa;
8321 if (!mupa)
8322 return NULL;
8324 space = isl_multi_union_pw_aff_get_space(mupa);
8326 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8327 if (n == 0)
8328 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8329 "cannot determine domain of zero-dimensional "
8330 "isl_multi_union_pw_aff", goto error);
8332 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8333 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8335 for (i = 1; i < n; ++i) {
8336 isl_union_pw_multi_aff *upma_i;
8338 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8339 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8340 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8343 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8345 isl_multi_union_pw_aff_free(mupa);
8346 return upma;
8347 error:
8348 isl_multi_union_pw_aff_free(mupa);
8349 return NULL;
8352 /* Intersect the range of "mupa" with "range".
8353 * That is, keep only those domain elements that have a function value
8354 * in "range".
8356 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8357 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8359 isl_union_pw_multi_aff *upma;
8360 isl_union_set *domain;
8361 isl_space *space;
8362 int n;
8363 int match;
8365 if (!mupa || !range)
8366 goto error;
8368 space = isl_set_get_space(range);
8369 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8370 space, isl_dim_set);
8371 isl_space_free(space);
8372 if (match < 0)
8373 goto error;
8374 if (!match)
8375 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8376 "space don't match", goto error);
8377 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8378 if (n == 0)
8379 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8380 "cannot intersect range of zero-dimensional "
8381 "isl_multi_union_pw_aff", goto error);
8383 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8384 isl_multi_union_pw_aff_copy(mupa));
8385 domain = isl_union_set_from_set(range);
8386 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8387 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8389 return mupa;
8390 error:
8391 isl_multi_union_pw_aff_free(mupa);
8392 isl_set_free(range);
8393 return NULL;
8396 /* Return the shared domain of the elements of "mupa".
8398 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8399 __isl_take isl_multi_union_pw_aff *mupa)
8401 int i, n;
8402 isl_union_pw_aff *upa;
8403 isl_union_set *dom;
8405 if (!mupa)
8406 return NULL;
8408 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8409 if (n == 0)
8410 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8411 "cannot determine domain", goto error);
8413 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8414 dom = isl_union_pw_aff_domain(upa);
8415 for (i = 1; i < n; ++i) {
8416 isl_union_set *dom_i;
8418 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8419 dom_i = isl_union_pw_aff_domain(upa);
8420 dom = isl_union_set_intersect(dom, dom_i);
8423 isl_multi_union_pw_aff_free(mupa);
8424 return dom;
8425 error:
8426 isl_multi_union_pw_aff_free(mupa);
8427 return NULL;
8430 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8431 * In particular, the spaces have been aligned.
8432 * The result is defined over the shared domain of the elements of "mupa"
8434 * We first extract the parametric constant part of "aff" and
8435 * define that over the shared domain.
8436 * Then we iterate over all input dimensions of "aff" and add the corresponding
8437 * multiples of the elements of "mupa".
8438 * Finally, we consider the integer divisions, calling the function
8439 * recursively to obtain an isl_union_pw_aff corresponding to the
8440 * integer division argument.
8442 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8443 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8445 int i, n_in, n_div;
8446 isl_union_pw_aff *upa;
8447 isl_union_set *uset;
8448 isl_val *v;
8449 isl_aff *cst;
8451 n_in = isl_aff_dim(aff, isl_dim_in);
8452 n_div = isl_aff_dim(aff, isl_dim_div);
8454 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8455 cst = isl_aff_copy(aff);
8456 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8457 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8458 cst = isl_aff_project_domain_on_params(cst);
8459 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8461 for (i = 0; i < n_in; ++i) {
8462 isl_union_pw_aff *upa_i;
8464 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8465 continue;
8466 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8467 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8468 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8469 upa = isl_union_pw_aff_add(upa, upa_i);
8472 for (i = 0; i < n_div; ++i) {
8473 isl_aff *div;
8474 isl_union_pw_aff *upa_i;
8476 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8477 continue;
8478 div = isl_aff_get_div(aff, i);
8479 upa_i = multi_union_pw_aff_apply_aff(
8480 isl_multi_union_pw_aff_copy(mupa), div);
8481 upa_i = isl_union_pw_aff_floor(upa_i);
8482 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8483 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8484 upa = isl_union_pw_aff_add(upa, upa_i);
8487 isl_multi_union_pw_aff_free(mupa);
8488 isl_aff_free(aff);
8490 return upa;
8493 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8494 * with the domain of "aff".
8495 * Furthermore, the dimension of this space needs to be greater than zero.
8496 * The result is defined over the shared domain of the elements of "mupa"
8498 * We perform these checks and then hand over control to
8499 * multi_union_pw_aff_apply_aff.
8501 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8502 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8504 isl_space *space1, *space2;
8505 int equal;
8507 mupa = isl_multi_union_pw_aff_align_params(mupa,
8508 isl_aff_get_space(aff));
8509 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8510 if (!mupa || !aff)
8511 goto error;
8513 space1 = isl_multi_union_pw_aff_get_space(mupa);
8514 space2 = isl_aff_get_domain_space(aff);
8515 equal = isl_space_is_equal(space1, space2);
8516 isl_space_free(space1);
8517 isl_space_free(space2);
8518 if (equal < 0)
8519 goto error;
8520 if (!equal)
8521 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8522 "spaces don't match", goto error);
8523 if (isl_aff_dim(aff, isl_dim_in) == 0)
8524 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8525 "cannot determine domains", goto error);
8527 return multi_union_pw_aff_apply_aff(mupa, aff);
8528 error:
8529 isl_multi_union_pw_aff_free(mupa);
8530 isl_aff_free(aff);
8531 return NULL;
8534 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8535 * with the domain of "ma".
8536 * Furthermore, the dimension of this space needs to be greater than zero,
8537 * unless the dimension of the target space of "ma" is also zero.
8538 * The result is defined over the shared domain of the elements of "mupa"
8540 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8541 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8543 isl_space *space1, *space2;
8544 isl_multi_union_pw_aff *res;
8545 int equal;
8546 int i, n_out;
8548 mupa = isl_multi_union_pw_aff_align_params(mupa,
8549 isl_multi_aff_get_space(ma));
8550 ma = isl_multi_aff_align_params(ma,
8551 isl_multi_union_pw_aff_get_space(mupa));
8552 if (!mupa || !ma)
8553 goto error;
8555 space1 = isl_multi_union_pw_aff_get_space(mupa);
8556 space2 = isl_multi_aff_get_domain_space(ma);
8557 equal = isl_space_is_equal(space1, space2);
8558 isl_space_free(space1);
8559 isl_space_free(space2);
8560 if (equal < 0)
8561 goto error;
8562 if (!equal)
8563 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8564 "spaces don't match", goto error);
8565 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8566 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8567 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8568 "cannot determine domains", goto error);
8570 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8571 res = isl_multi_union_pw_aff_alloc(space1);
8573 for (i = 0; i < n_out; ++i) {
8574 isl_aff *aff;
8575 isl_union_pw_aff *upa;
8577 aff = isl_multi_aff_get_aff(ma, i);
8578 upa = multi_union_pw_aff_apply_aff(
8579 isl_multi_union_pw_aff_copy(mupa), aff);
8580 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8583 isl_multi_aff_free(ma);
8584 isl_multi_union_pw_aff_free(mupa);
8585 return res;
8586 error:
8587 isl_multi_union_pw_aff_free(mupa);
8588 isl_multi_aff_free(ma);
8589 return NULL;
8592 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8593 * with the domain of "pa".
8594 * Furthermore, the dimension of this space needs to be greater than zero.
8595 * The result is defined over the shared domain of the elements of "mupa"
8597 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8598 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8600 int i;
8601 int equal;
8602 isl_space *space, *space2;
8603 isl_union_pw_aff *upa;
8605 mupa = isl_multi_union_pw_aff_align_params(mupa,
8606 isl_pw_aff_get_space(pa));
8607 pa = isl_pw_aff_align_params(pa,
8608 isl_multi_union_pw_aff_get_space(mupa));
8609 if (!mupa || !pa)
8610 goto error;
8612 space = isl_multi_union_pw_aff_get_space(mupa);
8613 space2 = isl_pw_aff_get_domain_space(pa);
8614 equal = isl_space_is_equal(space, space2);
8615 isl_space_free(space);
8616 isl_space_free(space2);
8617 if (equal < 0)
8618 goto error;
8619 if (!equal)
8620 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8621 "spaces don't match", goto error);
8622 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8623 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8624 "cannot determine domains", goto error);
8626 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8627 upa = isl_union_pw_aff_empty(space);
8629 for (i = 0; i < pa->n; ++i) {
8630 isl_aff *aff;
8631 isl_set *domain;
8632 isl_multi_union_pw_aff *mupa_i;
8633 isl_union_pw_aff *upa_i;
8635 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8636 domain = isl_set_copy(pa->p[i].set);
8637 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8638 aff = isl_aff_copy(pa->p[i].aff);
8639 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8640 upa = isl_union_pw_aff_union_add(upa, upa_i);
8643 isl_multi_union_pw_aff_free(mupa);
8644 isl_pw_aff_free(pa);
8645 return upa;
8646 error:
8647 isl_multi_union_pw_aff_free(mupa);
8648 isl_pw_aff_free(pa);
8649 return NULL;
8652 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8653 * with the domain of "pma".
8654 * Furthermore, the dimension of this space needs to be greater than zero,
8655 * unless the dimension of the target space of "pma" is also zero.
8656 * The result is defined over the shared domain of the elements of "mupa"
8658 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8659 __isl_take isl_multi_union_pw_aff *mupa,
8660 __isl_take isl_pw_multi_aff *pma)
8662 isl_space *space1, *space2;
8663 isl_multi_union_pw_aff *res;
8664 int equal;
8665 int i, n_out;
8667 mupa = isl_multi_union_pw_aff_align_params(mupa,
8668 isl_pw_multi_aff_get_space(pma));
8669 pma = isl_pw_multi_aff_align_params(pma,
8670 isl_multi_union_pw_aff_get_space(mupa));
8671 if (!mupa || !pma)
8672 goto error;
8674 space1 = isl_multi_union_pw_aff_get_space(mupa);
8675 space2 = isl_pw_multi_aff_get_domain_space(pma);
8676 equal = isl_space_is_equal(space1, space2);
8677 isl_space_free(space1);
8678 isl_space_free(space2);
8679 if (equal < 0)
8680 goto error;
8681 if (!equal)
8682 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8683 "spaces don't match", goto error);
8684 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8685 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8686 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8687 "cannot determine domains", goto error);
8689 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8690 res = isl_multi_union_pw_aff_alloc(space1);
8692 for (i = 0; i < n_out; ++i) {
8693 isl_pw_aff *pa;
8694 isl_union_pw_aff *upa;
8696 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8697 upa = isl_multi_union_pw_aff_apply_pw_aff(
8698 isl_multi_union_pw_aff_copy(mupa), pa);
8699 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8702 isl_pw_multi_aff_free(pma);
8703 isl_multi_union_pw_aff_free(mupa);
8704 return res;
8705 error:
8706 isl_multi_union_pw_aff_free(mupa);
8707 isl_pw_multi_aff_free(pma);
8708 return NULL;
8711 /* Compute the pullback of "mupa" by the function represented by "upma".
8712 * In other words, plug in "upma" in "mupa". The result contains
8713 * expressions defined over the domain space of "upma".
8715 * Run over all elements of "mupa" and plug in "upma" in each of them.
8717 __isl_give isl_multi_union_pw_aff *
8718 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8719 __isl_take isl_multi_union_pw_aff *mupa,
8720 __isl_take isl_union_pw_multi_aff *upma)
8722 int i, n;
8724 mupa = isl_multi_union_pw_aff_align_params(mupa,
8725 isl_union_pw_multi_aff_get_space(upma));
8726 upma = isl_union_pw_multi_aff_align_params(upma,
8727 isl_multi_union_pw_aff_get_space(mupa));
8728 if (!mupa || !upma)
8729 goto error;
8731 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8732 for (i = 0; i < n; ++i) {
8733 isl_union_pw_aff *upa;
8735 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8736 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8737 isl_union_pw_multi_aff_copy(upma));
8738 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8741 isl_union_pw_multi_aff_free(upma);
8742 return mupa;
8743 error:
8744 isl_multi_union_pw_aff_free(mupa);
8745 isl_union_pw_multi_aff_free(upma);
8746 return NULL;
8749 /* Extract the sequence of elements in "mupa" with domain space "space"
8750 * (ignoring parameters).
8752 * For the elements of "mupa" that are not defined on the specified space,
8753 * the corresponding element in the result is empty.
8755 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8756 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8758 int i, n;
8759 isl_space *space_mpa = NULL;
8760 isl_multi_pw_aff *mpa;
8762 if (!mupa || !space)
8763 goto error;
8765 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8766 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8767 space = isl_space_drop_dims(space, isl_dim_param,
8768 0, isl_space_dim(space, isl_dim_param));
8769 space = isl_space_align_params(space,
8770 isl_space_copy(space_mpa));
8771 if (!space)
8772 goto error;
8774 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8775 space_mpa);
8776 mpa = isl_multi_pw_aff_alloc(space_mpa);
8778 space = isl_space_from_domain(space);
8779 space = isl_space_add_dims(space, isl_dim_out, 1);
8780 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8781 for (i = 0; i < n; ++i) {
8782 isl_union_pw_aff *upa;
8783 isl_pw_aff *pa;
8785 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8786 pa = isl_union_pw_aff_extract_pw_aff(upa,
8787 isl_space_copy(space));
8788 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8789 isl_union_pw_aff_free(upa);
8792 isl_space_free(space);
8793 return mpa;
8794 error:
8795 isl_space_free(space_mpa);
8796 isl_space_free(space);
8797 return NULL;