isl_basic_map_simplify: reduce coefficients of integer divisions
[isl.git] / isl_aff.c
blob8ca370622b3434ed6bab33c8627c854ae5afc84f
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 /* Return a hash value that digests "aff".
280 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
282 uint32_t hash, ls_hash, v_hash;
284 if (!aff)
285 return 0;
287 hash = isl_hash_init();
288 ls_hash = isl_local_space_get_hash(aff->ls);
289 isl_hash_hash(hash, ls_hash);
290 v_hash = isl_vec_get_hash(aff->v);
291 isl_hash_hash(hash, v_hash);
293 return hash;
296 /* Externally, an isl_aff has a map space, but internally, the
297 * ls field corresponds to the domain of that space.
299 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
301 if (!aff)
302 return 0;
303 if (type == isl_dim_out)
304 return 1;
305 if (type == isl_dim_in)
306 type = isl_dim_set;
307 return isl_local_space_dim(aff->ls, type);
310 /* Return the position of the dimension of the given type and name
311 * in "aff".
312 * Return -1 if no such dimension can be found.
314 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
315 const char *name)
317 if (!aff)
318 return -1;
319 if (type == isl_dim_out)
320 return -1;
321 if (type == isl_dim_in)
322 type = isl_dim_set;
323 return isl_local_space_find_dim_by_name(aff->ls, type, name);
326 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
328 return aff ? isl_local_space_get_space(aff->ls) : NULL;
331 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
333 isl_space *space;
334 if (!aff)
335 return NULL;
336 space = isl_local_space_get_space(aff->ls);
337 space = isl_space_from_domain(space);
338 space = isl_space_add_dims(space, isl_dim_out, 1);
339 return space;
342 __isl_give isl_local_space *isl_aff_get_domain_local_space(
343 __isl_keep isl_aff *aff)
345 return aff ? isl_local_space_copy(aff->ls) : NULL;
348 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
350 isl_local_space *ls;
351 if (!aff)
352 return NULL;
353 ls = isl_local_space_copy(aff->ls);
354 ls = isl_local_space_from_domain(ls);
355 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
356 return ls;
359 /* Externally, an isl_aff has a map space, but internally, the
360 * ls field corresponds to the domain of that space.
362 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
363 enum isl_dim_type type, unsigned pos)
365 if (!aff)
366 return NULL;
367 if (type == isl_dim_out)
368 return NULL;
369 if (type == isl_dim_in)
370 type = isl_dim_set;
371 return isl_local_space_get_dim_name(aff->ls, type, pos);
374 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
375 __isl_take isl_space *dim)
377 aff = isl_aff_cow(aff);
378 if (!aff || !dim)
379 goto error;
381 aff->ls = isl_local_space_reset_space(aff->ls, dim);
382 if (!aff->ls)
383 return isl_aff_free(aff);
385 return aff;
386 error:
387 isl_aff_free(aff);
388 isl_space_free(dim);
389 return NULL;
392 /* Reset the space of "aff". This function is called from isl_pw_templ.c
393 * and doesn't know if the space of an element object is represented
394 * directly or through its domain. It therefore passes along both.
396 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
397 __isl_take isl_space *space, __isl_take isl_space *domain)
399 isl_space_free(space);
400 return isl_aff_reset_domain_space(aff, domain);
403 /* Reorder the coefficients of the affine expression based
404 * on the given reodering.
405 * The reordering r is assumed to have been extended with the local
406 * variables.
408 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
409 __isl_take isl_reordering *r, int n_div)
411 isl_vec *res;
412 int i;
414 if (!vec || !r)
415 goto error;
417 res = isl_vec_alloc(vec->ctx,
418 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
419 if (!res)
420 goto error;
421 isl_seq_cpy(res->el, vec->el, 2);
422 isl_seq_clr(res->el + 2, res->size - 2);
423 for (i = 0; i < r->len; ++i)
424 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
426 isl_reordering_free(r);
427 isl_vec_free(vec);
428 return res;
429 error:
430 isl_vec_free(vec);
431 isl_reordering_free(r);
432 return NULL;
435 /* Reorder the dimensions of the domain of "aff" according
436 * to the given reordering.
438 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
439 __isl_take isl_reordering *r)
441 aff = isl_aff_cow(aff);
442 if (!aff)
443 goto error;
445 r = isl_reordering_extend(r, aff->ls->div->n_row);
446 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
447 aff->ls->div->n_row);
448 aff->ls = isl_local_space_realign(aff->ls, r);
450 if (!aff->v || !aff->ls)
451 return isl_aff_free(aff);
453 return aff;
454 error:
455 isl_aff_free(aff);
456 isl_reordering_free(r);
457 return NULL;
460 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
461 __isl_take isl_space *model)
463 if (!aff || !model)
464 goto error;
466 if (!isl_space_match(aff->ls->dim, isl_dim_param,
467 model, isl_dim_param)) {
468 isl_reordering *exp;
470 model = isl_space_drop_dims(model, isl_dim_in,
471 0, isl_space_dim(model, isl_dim_in));
472 model = isl_space_drop_dims(model, isl_dim_out,
473 0, isl_space_dim(model, isl_dim_out));
474 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
475 exp = isl_reordering_extend_space(exp,
476 isl_aff_get_domain_space(aff));
477 aff = isl_aff_realign_domain(aff, exp);
480 isl_space_free(model);
481 return aff;
482 error:
483 isl_space_free(model);
484 isl_aff_free(aff);
485 return NULL;
488 /* Is "aff" obviously equal to zero?
490 * If the denominator is zero, then "aff" is not equal to zero.
492 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
494 if (!aff)
495 return isl_bool_error;
497 if (isl_int_is_zero(aff->v->el[0]))
498 return isl_bool_false;
499 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
502 /* Does "aff" represent NaN?
504 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
506 if (!aff)
507 return isl_bool_error;
509 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
512 /* Does "pa" involve any NaNs?
514 isl_bool isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
516 int i;
518 if (!pa)
519 return isl_bool_error;
520 if (pa->n == 0)
521 return isl_bool_false;
523 for (i = 0; i < pa->n; ++i) {
524 isl_bool is_nan = isl_aff_is_nan(pa->p[i].aff);
525 if (is_nan < 0 || is_nan)
526 return is_nan;
529 return isl_bool_false;
532 /* Are "aff1" and "aff2" obviously equal?
534 * NaN is not equal to anything, not even to another NaN.
536 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
537 __isl_keep isl_aff *aff2)
539 isl_bool equal;
541 if (!aff1 || !aff2)
542 return isl_bool_error;
544 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
545 return isl_bool_false;
547 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
548 if (equal < 0 || !equal)
549 return equal;
551 return isl_vec_is_equal(aff1->v, aff2->v);
554 /* Return the common denominator of "aff" in "v".
556 * We cannot return anything meaningful in case of a NaN.
558 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
560 if (!aff)
561 return -1;
562 if (isl_aff_is_nan(aff))
563 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
564 "cannot get denominator of NaN", return -1);
565 isl_int_set(*v, aff->v->el[0]);
566 return 0;
569 /* Return the common denominator of "aff".
571 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
573 isl_ctx *ctx;
575 if (!aff)
576 return NULL;
578 ctx = isl_aff_get_ctx(aff);
579 if (isl_aff_is_nan(aff))
580 return isl_val_nan(ctx);
581 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
584 /* Return the constant term of "aff" in "v".
586 * We cannot return anything meaningful in case of a NaN.
588 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
590 if (!aff)
591 return -1;
592 if (isl_aff_is_nan(aff))
593 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
594 "cannot get constant term of NaN", return -1);
595 isl_int_set(*v, aff->v->el[1]);
596 return 0;
599 /* Return the constant term of "aff".
601 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
603 isl_ctx *ctx;
604 isl_val *v;
606 if (!aff)
607 return NULL;
609 ctx = isl_aff_get_ctx(aff);
610 if (isl_aff_is_nan(aff))
611 return isl_val_nan(ctx);
612 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
613 return isl_val_normalize(v);
616 /* Return the coefficient of the variable of type "type" at position "pos"
617 * of "aff" in "v".
619 * We cannot return anything meaningful in case of a NaN.
621 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
622 enum isl_dim_type type, int pos, isl_int *v)
624 if (!aff)
625 return -1;
627 if (type == isl_dim_out)
628 isl_die(aff->v->ctx, isl_error_invalid,
629 "output/set dimension does not have a coefficient",
630 return -1);
631 if (type == isl_dim_in)
632 type = isl_dim_set;
634 if (pos >= isl_local_space_dim(aff->ls, type))
635 isl_die(aff->v->ctx, isl_error_invalid,
636 "position out of bounds", return -1);
638 if (isl_aff_is_nan(aff))
639 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
640 "cannot get coefficient of NaN", return -1);
641 pos += isl_local_space_offset(aff->ls, type);
642 isl_int_set(*v, aff->v->el[1 + pos]);
644 return 0;
647 /* Return the coefficient of the variable of type "type" at position "pos"
648 * of "aff".
650 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
651 enum isl_dim_type type, int pos)
653 isl_ctx *ctx;
654 isl_val *v;
656 if (!aff)
657 return NULL;
659 ctx = isl_aff_get_ctx(aff);
660 if (type == isl_dim_out)
661 isl_die(ctx, isl_error_invalid,
662 "output/set dimension does not have a coefficient",
663 return NULL);
664 if (type == isl_dim_in)
665 type = isl_dim_set;
667 if (pos >= isl_local_space_dim(aff->ls, type))
668 isl_die(ctx, isl_error_invalid,
669 "position out of bounds", return NULL);
671 if (isl_aff_is_nan(aff))
672 return isl_val_nan(ctx);
673 pos += isl_local_space_offset(aff->ls, type);
674 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
675 return isl_val_normalize(v);
678 /* Return the sign of the coefficient of the variable of type "type"
679 * at position "pos" of "aff".
681 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
682 int pos)
684 isl_ctx *ctx;
686 if (!aff)
687 return 0;
689 ctx = isl_aff_get_ctx(aff);
690 if (type == isl_dim_out)
691 isl_die(ctx, isl_error_invalid,
692 "output/set dimension does not have a coefficient",
693 return 0);
694 if (type == isl_dim_in)
695 type = isl_dim_set;
697 if (pos >= isl_local_space_dim(aff->ls, type))
698 isl_die(ctx, isl_error_invalid,
699 "position out of bounds", return 0);
701 pos += isl_local_space_offset(aff->ls, type);
702 return isl_int_sgn(aff->v->el[1 + pos]);
705 /* Replace the denominator of "aff" by "v".
707 * A NaN is unaffected by this operation.
709 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
711 if (!aff)
712 return NULL;
713 if (isl_aff_is_nan(aff))
714 return aff;
715 aff = isl_aff_cow(aff);
716 if (!aff)
717 return NULL;
719 aff->v = isl_vec_cow(aff->v);
720 if (!aff->v)
721 return isl_aff_free(aff);
723 isl_int_set(aff->v->el[0], v);
725 return aff;
728 /* Replace the numerator of the constant term of "aff" by "v".
730 * A NaN is unaffected by this operation.
732 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
734 if (!aff)
735 return NULL;
736 if (isl_aff_is_nan(aff))
737 return aff;
738 aff = isl_aff_cow(aff);
739 if (!aff)
740 return NULL;
742 aff->v = isl_vec_cow(aff->v);
743 if (!aff->v)
744 return isl_aff_free(aff);
746 isl_int_set(aff->v->el[1], v);
748 return aff;
751 /* Replace the constant term of "aff" by "v".
753 * A NaN is unaffected by this operation.
755 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
756 __isl_take isl_val *v)
758 if (!aff || !v)
759 goto error;
761 if (isl_aff_is_nan(aff)) {
762 isl_val_free(v);
763 return aff;
766 if (!isl_val_is_rat(v))
767 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
768 "expecting rational value", goto error);
770 if (isl_int_eq(aff->v->el[1], v->n) &&
771 isl_int_eq(aff->v->el[0], v->d)) {
772 isl_val_free(v);
773 return aff;
776 aff = isl_aff_cow(aff);
777 if (!aff)
778 goto error;
779 aff->v = isl_vec_cow(aff->v);
780 if (!aff->v)
781 goto error;
783 if (isl_int_eq(aff->v->el[0], v->d)) {
784 isl_int_set(aff->v->el[1], v->n);
785 } else if (isl_int_is_one(v->d)) {
786 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
787 } else {
788 isl_seq_scale(aff->v->el + 1,
789 aff->v->el + 1, v->d, aff->v->size - 1);
790 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
791 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
792 aff->v = isl_vec_normalize(aff->v);
793 if (!aff->v)
794 goto error;
797 isl_val_free(v);
798 return aff;
799 error:
800 isl_aff_free(aff);
801 isl_val_free(v);
802 return NULL;
805 /* Add "v" to the constant term of "aff".
807 * A NaN is unaffected by this operation.
809 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
811 if (isl_int_is_zero(v))
812 return aff;
814 if (!aff)
815 return NULL;
816 if (isl_aff_is_nan(aff))
817 return aff;
818 aff = isl_aff_cow(aff);
819 if (!aff)
820 return NULL;
822 aff->v = isl_vec_cow(aff->v);
823 if (!aff->v)
824 return isl_aff_free(aff);
826 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
828 return aff;
831 /* Add "v" to the constant term of "aff".
833 * A NaN is unaffected by this operation.
835 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
836 __isl_take isl_val *v)
838 if (!aff || !v)
839 goto error;
841 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
842 isl_val_free(v);
843 return aff;
846 if (!isl_val_is_rat(v))
847 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
848 "expecting rational value", goto error);
850 aff = isl_aff_cow(aff);
851 if (!aff)
852 goto error;
854 aff->v = isl_vec_cow(aff->v);
855 if (!aff->v)
856 goto error;
858 if (isl_int_is_one(v->d)) {
859 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
860 } else if (isl_int_eq(aff->v->el[0], v->d)) {
861 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
862 aff->v = isl_vec_normalize(aff->v);
863 if (!aff->v)
864 goto error;
865 } else {
866 isl_seq_scale(aff->v->el + 1,
867 aff->v->el + 1, v->d, aff->v->size - 1);
868 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
869 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
870 aff->v = isl_vec_normalize(aff->v);
871 if (!aff->v)
872 goto error;
875 isl_val_free(v);
876 return aff;
877 error:
878 isl_aff_free(aff);
879 isl_val_free(v);
880 return NULL;
883 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
885 isl_int t;
887 isl_int_init(t);
888 isl_int_set_si(t, v);
889 aff = isl_aff_add_constant(aff, t);
890 isl_int_clear(t);
892 return aff;
895 /* Add "v" to the numerator of the constant term of "aff".
897 * A NaN is unaffected by this operation.
899 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
901 if (isl_int_is_zero(v))
902 return aff;
904 if (!aff)
905 return NULL;
906 if (isl_aff_is_nan(aff))
907 return aff;
908 aff = isl_aff_cow(aff);
909 if (!aff)
910 return NULL;
912 aff->v = isl_vec_cow(aff->v);
913 if (!aff->v)
914 return isl_aff_free(aff);
916 isl_int_add(aff->v->el[1], aff->v->el[1], v);
918 return aff;
921 /* Add "v" to the numerator of the constant term of "aff".
923 * A NaN is unaffected by this operation.
925 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
927 isl_int t;
929 if (v == 0)
930 return aff;
932 isl_int_init(t);
933 isl_int_set_si(t, v);
934 aff = isl_aff_add_constant_num(aff, t);
935 isl_int_clear(t);
937 return aff;
940 /* Replace the numerator of the constant term of "aff" by "v".
942 * A NaN is unaffected by this operation.
944 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
946 if (!aff)
947 return NULL;
948 if (isl_aff_is_nan(aff))
949 return aff;
950 aff = isl_aff_cow(aff);
951 if (!aff)
952 return NULL;
954 aff->v = isl_vec_cow(aff->v);
955 if (!aff->v)
956 return isl_aff_free(aff);
958 isl_int_set_si(aff->v->el[1], v);
960 return aff;
963 /* Replace the numerator of the coefficient of the variable of type "type"
964 * at position "pos" of "aff" by "v".
966 * A NaN is unaffected by this operation.
968 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
969 enum isl_dim_type type, int pos, isl_int v)
971 if (!aff)
972 return NULL;
974 if (type == isl_dim_out)
975 isl_die(aff->v->ctx, isl_error_invalid,
976 "output/set dimension does not have a coefficient",
977 return isl_aff_free(aff));
978 if (type == isl_dim_in)
979 type = isl_dim_set;
981 if (pos >= isl_local_space_dim(aff->ls, type))
982 isl_die(aff->v->ctx, isl_error_invalid,
983 "position out of bounds", return isl_aff_free(aff));
985 if (isl_aff_is_nan(aff))
986 return aff;
987 aff = isl_aff_cow(aff);
988 if (!aff)
989 return NULL;
991 aff->v = isl_vec_cow(aff->v);
992 if (!aff->v)
993 return isl_aff_free(aff);
995 pos += isl_local_space_offset(aff->ls, type);
996 isl_int_set(aff->v->el[1 + pos], v);
998 return aff;
1001 /* Replace the numerator of the coefficient of the variable of type "type"
1002 * at position "pos" of "aff" by "v".
1004 * A NaN is unaffected by this operation.
1006 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1007 enum isl_dim_type type, int pos, int v)
1009 if (!aff)
1010 return NULL;
1012 if (type == isl_dim_out)
1013 isl_die(aff->v->ctx, isl_error_invalid,
1014 "output/set dimension does not have a coefficient",
1015 return isl_aff_free(aff));
1016 if (type == isl_dim_in)
1017 type = isl_dim_set;
1019 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1020 isl_die(aff->v->ctx, isl_error_invalid,
1021 "position out of bounds", return isl_aff_free(aff));
1023 if (isl_aff_is_nan(aff))
1024 return aff;
1025 pos += isl_local_space_offset(aff->ls, type);
1026 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1027 return aff;
1029 aff = isl_aff_cow(aff);
1030 if (!aff)
1031 return NULL;
1033 aff->v = isl_vec_cow(aff->v);
1034 if (!aff->v)
1035 return isl_aff_free(aff);
1037 isl_int_set_si(aff->v->el[1 + pos], v);
1039 return aff;
1042 /* Replace the coefficient of the variable of type "type" at position "pos"
1043 * of "aff" by "v".
1045 * A NaN is unaffected by this operation.
1047 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1048 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1050 if (!aff || !v)
1051 goto error;
1053 if (type == isl_dim_out)
1054 isl_die(aff->v->ctx, isl_error_invalid,
1055 "output/set dimension does not have a coefficient",
1056 goto error);
1057 if (type == isl_dim_in)
1058 type = isl_dim_set;
1060 if (pos >= isl_local_space_dim(aff->ls, type))
1061 isl_die(aff->v->ctx, isl_error_invalid,
1062 "position out of bounds", goto error);
1064 if (isl_aff_is_nan(aff)) {
1065 isl_val_free(v);
1066 return aff;
1068 if (!isl_val_is_rat(v))
1069 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1070 "expecting rational value", goto error);
1072 pos += isl_local_space_offset(aff->ls, type);
1073 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1074 isl_int_eq(aff->v->el[0], v->d)) {
1075 isl_val_free(v);
1076 return aff;
1079 aff = isl_aff_cow(aff);
1080 if (!aff)
1081 goto error;
1082 aff->v = isl_vec_cow(aff->v);
1083 if (!aff->v)
1084 goto error;
1086 if (isl_int_eq(aff->v->el[0], v->d)) {
1087 isl_int_set(aff->v->el[1 + pos], v->n);
1088 } else if (isl_int_is_one(v->d)) {
1089 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1090 } else {
1091 isl_seq_scale(aff->v->el + 1,
1092 aff->v->el + 1, v->d, aff->v->size - 1);
1093 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1094 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1095 aff->v = isl_vec_normalize(aff->v);
1096 if (!aff->v)
1097 goto error;
1100 isl_val_free(v);
1101 return aff;
1102 error:
1103 isl_aff_free(aff);
1104 isl_val_free(v);
1105 return NULL;
1108 /* Add "v" to the coefficient of the variable of type "type"
1109 * at position "pos" of "aff".
1111 * A NaN is unaffected by this operation.
1113 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1114 enum isl_dim_type type, int pos, isl_int v)
1116 if (!aff)
1117 return NULL;
1119 if (type == isl_dim_out)
1120 isl_die(aff->v->ctx, isl_error_invalid,
1121 "output/set dimension does not have a coefficient",
1122 return isl_aff_free(aff));
1123 if (type == isl_dim_in)
1124 type = isl_dim_set;
1126 if (pos >= isl_local_space_dim(aff->ls, type))
1127 isl_die(aff->v->ctx, isl_error_invalid,
1128 "position out of bounds", return isl_aff_free(aff));
1130 if (isl_aff_is_nan(aff))
1131 return aff;
1132 aff = isl_aff_cow(aff);
1133 if (!aff)
1134 return NULL;
1136 aff->v = isl_vec_cow(aff->v);
1137 if (!aff->v)
1138 return isl_aff_free(aff);
1140 pos += isl_local_space_offset(aff->ls, type);
1141 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1143 return aff;
1146 /* Add "v" to the coefficient of the variable of type "type"
1147 * at position "pos" of "aff".
1149 * A NaN is unaffected by this operation.
1151 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1152 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1154 if (!aff || !v)
1155 goto error;
1157 if (isl_val_is_zero(v)) {
1158 isl_val_free(v);
1159 return aff;
1162 if (type == isl_dim_out)
1163 isl_die(aff->v->ctx, isl_error_invalid,
1164 "output/set dimension does not have a coefficient",
1165 goto error);
1166 if (type == isl_dim_in)
1167 type = isl_dim_set;
1169 if (pos >= isl_local_space_dim(aff->ls, type))
1170 isl_die(aff->v->ctx, isl_error_invalid,
1171 "position out of bounds", goto error);
1173 if (isl_aff_is_nan(aff)) {
1174 isl_val_free(v);
1175 return aff;
1177 if (!isl_val_is_rat(v))
1178 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1179 "expecting rational value", goto error);
1181 aff = isl_aff_cow(aff);
1182 if (!aff)
1183 goto error;
1185 aff->v = isl_vec_cow(aff->v);
1186 if (!aff->v)
1187 goto error;
1189 pos += isl_local_space_offset(aff->ls, type);
1190 if (isl_int_is_one(v->d)) {
1191 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1192 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1193 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1194 aff->v = isl_vec_normalize(aff->v);
1195 if (!aff->v)
1196 goto error;
1197 } else {
1198 isl_seq_scale(aff->v->el + 1,
1199 aff->v->el + 1, v->d, aff->v->size - 1);
1200 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1201 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1202 aff->v = isl_vec_normalize(aff->v);
1203 if (!aff->v)
1204 goto error;
1207 isl_val_free(v);
1208 return aff;
1209 error:
1210 isl_aff_free(aff);
1211 isl_val_free(v);
1212 return NULL;
1215 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1216 enum isl_dim_type type, int pos, int v)
1218 isl_int t;
1220 isl_int_init(t);
1221 isl_int_set_si(t, v);
1222 aff = isl_aff_add_coefficient(aff, type, pos, t);
1223 isl_int_clear(t);
1225 return aff;
1228 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1230 if (!aff)
1231 return NULL;
1233 return isl_local_space_get_div(aff->ls, pos);
1236 /* Return the negation of "aff".
1238 * As a special case, -NaN = NaN.
1240 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1242 if (!aff)
1243 return NULL;
1244 if (isl_aff_is_nan(aff))
1245 return aff;
1246 aff = isl_aff_cow(aff);
1247 if (!aff)
1248 return NULL;
1249 aff->v = isl_vec_cow(aff->v);
1250 if (!aff->v)
1251 return isl_aff_free(aff);
1253 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1255 return aff;
1258 /* Remove divs from the local space that do not appear in the affine
1259 * expression.
1260 * We currently only remove divs at the end.
1261 * Some intermediate divs may also not appear directly in the affine
1262 * expression, but we would also need to check that no other divs are
1263 * defined in terms of them.
1265 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1267 int pos;
1268 int off;
1269 int n;
1271 if (!aff)
1272 return NULL;
1274 n = isl_local_space_dim(aff->ls, isl_dim_div);
1275 off = isl_local_space_offset(aff->ls, isl_dim_div);
1277 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1278 if (pos == n)
1279 return aff;
1281 aff = isl_aff_cow(aff);
1282 if (!aff)
1283 return NULL;
1285 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1286 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1287 if (!aff->ls || !aff->v)
1288 return isl_aff_free(aff);
1290 return aff;
1293 /* Given two affine expressions "p" of length p_len (including the
1294 * denominator and the constant term) and "subs" of length subs_len,
1295 * plug in "subs" for the variable at position "pos".
1296 * The variables of "subs" and "p" are assumed to match up to subs_len,
1297 * but "p" may have additional variables.
1298 * "v" is an initialized isl_int that can be used internally.
1300 * In particular, if "p" represents the expression
1302 * (a i + g)/m
1304 * with i the variable at position "pos" and "subs" represents the expression
1306 * f/d
1308 * then the result represents the expression
1310 * (a f + d g)/(m d)
1313 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1314 int p_len, int subs_len, isl_int v)
1316 isl_int_set(v, p[1 + pos]);
1317 isl_int_set_si(p[1 + pos], 0);
1318 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1319 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1320 isl_int_mul(p[0], p[0], subs[0]);
1323 /* Look for any divs in the aff->ls with a denominator equal to one
1324 * and plug them into the affine expression and any subsequent divs
1325 * that may reference the div.
1327 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1329 int i, n;
1330 int len;
1331 isl_int v;
1332 isl_vec *vec;
1333 isl_local_space *ls;
1334 unsigned pos;
1336 if (!aff)
1337 return NULL;
1339 n = isl_local_space_dim(aff->ls, isl_dim_div);
1340 len = aff->v->size;
1341 for (i = 0; i < n; ++i) {
1342 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1343 continue;
1344 ls = isl_local_space_copy(aff->ls);
1345 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1346 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1347 vec = isl_vec_copy(aff->v);
1348 vec = isl_vec_cow(vec);
1349 if (!ls || !vec)
1350 goto error;
1352 isl_int_init(v);
1354 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1355 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1356 len, len, v);
1358 isl_int_clear(v);
1360 isl_vec_free(aff->v);
1361 aff->v = vec;
1362 isl_local_space_free(aff->ls);
1363 aff->ls = ls;
1366 return aff;
1367 error:
1368 isl_vec_free(vec);
1369 isl_local_space_free(ls);
1370 return isl_aff_free(aff);
1373 /* Look for any divs j that appear with a unit coefficient inside
1374 * the definitions of other divs i and plug them into the definitions
1375 * of the divs i.
1377 * In particular, an expression of the form
1379 * floor((f(..) + floor(g(..)/n))/m)
1381 * is simplified to
1383 * floor((n * f(..) + g(..))/(n * m))
1385 * This simplification is correct because we can move the expression
1386 * f(..) into the inner floor in the original expression to obtain
1388 * floor(floor((n * f(..) + g(..))/n)/m)
1390 * from which we can derive the simplified expression.
1392 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1394 int i, j, n;
1395 int off;
1397 if (!aff)
1398 return NULL;
1400 n = isl_local_space_dim(aff->ls, isl_dim_div);
1401 off = isl_local_space_offset(aff->ls, isl_dim_div);
1402 for (i = 1; i < n; ++i) {
1403 for (j = 0; j < i; ++j) {
1404 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1405 continue;
1406 aff->ls = isl_local_space_substitute_seq(aff->ls,
1407 isl_dim_div, j, aff->ls->div->row[j],
1408 aff->v->size, i, 1);
1409 if (!aff->ls)
1410 return isl_aff_free(aff);
1414 return aff;
1417 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1419 * Even though this function is only called on isl_affs with a single
1420 * reference, we are careful to only change aff->v and aff->ls together.
1422 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1424 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1425 isl_local_space *ls;
1426 isl_vec *v;
1428 ls = isl_local_space_copy(aff->ls);
1429 ls = isl_local_space_swap_div(ls, a, b);
1430 v = isl_vec_copy(aff->v);
1431 v = isl_vec_cow(v);
1432 if (!ls || !v)
1433 goto error;
1435 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1436 isl_vec_free(aff->v);
1437 aff->v = v;
1438 isl_local_space_free(aff->ls);
1439 aff->ls = ls;
1441 return aff;
1442 error:
1443 isl_vec_free(v);
1444 isl_local_space_free(ls);
1445 return isl_aff_free(aff);
1448 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1450 * We currently do not actually remove div "b", but simply add its
1451 * coefficient to that of "a" and then zero it out.
1453 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1455 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1457 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1458 return aff;
1460 aff->v = isl_vec_cow(aff->v);
1461 if (!aff->v)
1462 return isl_aff_free(aff);
1464 isl_int_add(aff->v->el[1 + off + a],
1465 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1466 isl_int_set_si(aff->v->el[1 + off + b], 0);
1468 return aff;
1471 /* Sort the divs in the local space of "aff" according to
1472 * the comparison function "cmp_row" in isl_local_space.c,
1473 * combining the coefficients of identical divs.
1475 * Reordering divs does not change the semantics of "aff",
1476 * so there is no need to call isl_aff_cow.
1477 * Moreover, this function is currently only called on isl_affs
1478 * with a single reference.
1480 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1482 int i, j, n;
1484 if (!aff)
1485 return NULL;
1487 n = isl_aff_dim(aff, isl_dim_div);
1488 for (i = 1; i < n; ++i) {
1489 for (j = i - 1; j >= 0; --j) {
1490 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1491 if (cmp < 0)
1492 break;
1493 if (cmp == 0)
1494 aff = merge_divs(aff, j, j + 1);
1495 else
1496 aff = swap_div(aff, j, j + 1);
1497 if (!aff)
1498 return NULL;
1502 return aff;
1505 /* Normalize the representation of "aff".
1507 * This function should only be called of "new" isl_affs, i.e.,
1508 * with only a single reference. We therefore do not need to
1509 * worry about affecting other instances.
1511 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1513 if (!aff)
1514 return NULL;
1515 aff->v = isl_vec_normalize(aff->v);
1516 if (!aff->v)
1517 return isl_aff_free(aff);
1518 aff = plug_in_integral_divs(aff);
1519 aff = plug_in_unit_divs(aff);
1520 aff = sort_divs(aff);
1521 aff = isl_aff_remove_unused_divs(aff);
1522 return aff;
1525 /* Given f, return floor(f).
1526 * If f is an integer expression, then just return f.
1527 * If f is a constant, then return the constant floor(f).
1528 * Otherwise, if f = g/m, write g = q m + r,
1529 * create a new div d = [r/m] and return the expression q + d.
1530 * The coefficients in r are taken to lie between -m/2 and m/2.
1532 * reduce_div_coefficients performs the same normalization.
1534 * As a special case, floor(NaN) = NaN.
1536 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1538 int i;
1539 int size;
1540 isl_ctx *ctx;
1541 isl_vec *div;
1543 if (!aff)
1544 return NULL;
1546 if (isl_aff_is_nan(aff))
1547 return aff;
1548 if (isl_int_is_one(aff->v->el[0]))
1549 return aff;
1551 aff = isl_aff_cow(aff);
1552 if (!aff)
1553 return NULL;
1555 aff->v = isl_vec_cow(aff->v);
1556 if (!aff->v)
1557 return isl_aff_free(aff);
1559 if (isl_aff_is_cst(aff)) {
1560 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1561 isl_int_set_si(aff->v->el[0], 1);
1562 return aff;
1565 div = isl_vec_copy(aff->v);
1566 div = isl_vec_cow(div);
1567 if (!div)
1568 return isl_aff_free(aff);
1570 ctx = isl_aff_get_ctx(aff);
1571 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1572 for (i = 1; i < aff->v->size; ++i) {
1573 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1574 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1575 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1576 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1577 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1581 aff->ls = isl_local_space_add_div(aff->ls, div);
1582 if (!aff->ls)
1583 return isl_aff_free(aff);
1585 size = aff->v->size;
1586 aff->v = isl_vec_extend(aff->v, size + 1);
1587 if (!aff->v)
1588 return isl_aff_free(aff);
1589 isl_int_set_si(aff->v->el[0], 1);
1590 isl_int_set_si(aff->v->el[size], 1);
1592 aff = isl_aff_normalize(aff);
1594 return aff;
1597 /* Compute
1599 * aff mod m = aff - m * floor(aff/m)
1601 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1603 isl_aff *res;
1605 res = isl_aff_copy(aff);
1606 aff = isl_aff_scale_down(aff, m);
1607 aff = isl_aff_floor(aff);
1608 aff = isl_aff_scale(aff, m);
1609 res = isl_aff_sub(res, aff);
1611 return res;
1614 /* Compute
1616 * aff mod m = aff - m * floor(aff/m)
1618 * with m an integer value.
1620 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1621 __isl_take isl_val *m)
1623 isl_aff *res;
1625 if (!aff || !m)
1626 goto error;
1628 if (!isl_val_is_int(m))
1629 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1630 "expecting integer modulo", goto error);
1632 res = isl_aff_copy(aff);
1633 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1634 aff = isl_aff_floor(aff);
1635 aff = isl_aff_scale_val(aff, m);
1636 res = isl_aff_sub(res, aff);
1638 return res;
1639 error:
1640 isl_aff_free(aff);
1641 isl_val_free(m);
1642 return NULL;
1645 /* Compute
1647 * pwaff mod m = pwaff - m * floor(pwaff/m)
1649 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1651 isl_pw_aff *res;
1653 res = isl_pw_aff_copy(pwaff);
1654 pwaff = isl_pw_aff_scale_down(pwaff, m);
1655 pwaff = isl_pw_aff_floor(pwaff);
1656 pwaff = isl_pw_aff_scale(pwaff, m);
1657 res = isl_pw_aff_sub(res, pwaff);
1659 return res;
1662 /* Compute
1664 * pa mod m = pa - m * floor(pa/m)
1666 * with m an integer value.
1668 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1669 __isl_take isl_val *m)
1671 if (!pa || !m)
1672 goto error;
1673 if (!isl_val_is_int(m))
1674 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1675 "expecting integer modulo", goto error);
1676 pa = isl_pw_aff_mod(pa, m->n);
1677 isl_val_free(m);
1678 return pa;
1679 error:
1680 isl_pw_aff_free(pa);
1681 isl_val_free(m);
1682 return NULL;
1685 /* Given f, return ceil(f).
1686 * If f is an integer expression, then just return f.
1687 * Otherwise, let f be the expression
1689 * e/m
1691 * then return
1693 * floor((e + m - 1)/m)
1695 * As a special case, ceil(NaN) = NaN.
1697 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1699 if (!aff)
1700 return NULL;
1702 if (isl_aff_is_nan(aff))
1703 return aff;
1704 if (isl_int_is_one(aff->v->el[0]))
1705 return aff;
1707 aff = isl_aff_cow(aff);
1708 if (!aff)
1709 return NULL;
1710 aff->v = isl_vec_cow(aff->v);
1711 if (!aff->v)
1712 return isl_aff_free(aff);
1714 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1715 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1716 aff = isl_aff_floor(aff);
1718 return aff;
1721 /* Apply the expansion computed by isl_merge_divs.
1722 * The expansion itself is given by "exp" while the resulting
1723 * list of divs is given by "div".
1725 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1726 __isl_take isl_mat *div, int *exp)
1728 int old_n_div;
1729 int new_n_div;
1730 int offset;
1732 aff = isl_aff_cow(aff);
1733 if (!aff || !div)
1734 goto error;
1736 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1737 new_n_div = isl_mat_rows(div);
1738 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1740 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1741 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1742 if (!aff->v || !aff->ls)
1743 return isl_aff_free(aff);
1744 return aff;
1745 error:
1746 isl_aff_free(aff);
1747 isl_mat_free(div);
1748 return NULL;
1751 /* Add two affine expressions that live in the same local space.
1753 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1754 __isl_take isl_aff *aff2)
1756 isl_int gcd, f;
1758 aff1 = isl_aff_cow(aff1);
1759 if (!aff1 || !aff2)
1760 goto error;
1762 aff1->v = isl_vec_cow(aff1->v);
1763 if (!aff1->v)
1764 goto error;
1766 isl_int_init(gcd);
1767 isl_int_init(f);
1768 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1769 isl_int_divexact(f, aff2->v->el[0], gcd);
1770 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1771 isl_int_divexact(f, aff1->v->el[0], gcd);
1772 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1773 isl_int_divexact(f, aff2->v->el[0], gcd);
1774 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1775 isl_int_clear(f);
1776 isl_int_clear(gcd);
1778 isl_aff_free(aff2);
1779 return aff1;
1780 error:
1781 isl_aff_free(aff1);
1782 isl_aff_free(aff2);
1783 return NULL;
1786 /* Return the sum of "aff1" and "aff2".
1788 * If either of the two is NaN, then the result is NaN.
1790 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1791 __isl_take isl_aff *aff2)
1793 isl_ctx *ctx;
1794 int *exp1 = NULL;
1795 int *exp2 = NULL;
1796 isl_mat *div;
1797 int n_div1, n_div2;
1799 if (!aff1 || !aff2)
1800 goto error;
1802 ctx = isl_aff_get_ctx(aff1);
1803 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1804 isl_die(ctx, isl_error_invalid,
1805 "spaces don't match", goto error);
1807 if (isl_aff_is_nan(aff1)) {
1808 isl_aff_free(aff2);
1809 return aff1;
1811 if (isl_aff_is_nan(aff2)) {
1812 isl_aff_free(aff1);
1813 return aff2;
1816 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1817 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1818 if (n_div1 == 0 && n_div2 == 0)
1819 return add_expanded(aff1, aff2);
1821 exp1 = isl_alloc_array(ctx, int, n_div1);
1822 exp2 = isl_alloc_array(ctx, int, n_div2);
1823 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1824 goto error;
1826 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1827 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1828 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1829 free(exp1);
1830 free(exp2);
1832 return add_expanded(aff1, aff2);
1833 error:
1834 free(exp1);
1835 free(exp2);
1836 isl_aff_free(aff1);
1837 isl_aff_free(aff2);
1838 return NULL;
1841 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1842 __isl_take isl_aff *aff2)
1844 return isl_aff_add(aff1, isl_aff_neg(aff2));
1847 /* Return the result of scaling "aff" by a factor of "f".
1849 * As a special case, f * NaN = NaN.
1851 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1853 isl_int gcd;
1855 if (!aff)
1856 return NULL;
1857 if (isl_aff_is_nan(aff))
1858 return aff;
1860 if (isl_int_is_one(f))
1861 return aff;
1863 aff = isl_aff_cow(aff);
1864 if (!aff)
1865 return NULL;
1866 aff->v = isl_vec_cow(aff->v);
1867 if (!aff->v)
1868 return isl_aff_free(aff);
1870 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1871 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1872 return aff;
1875 isl_int_init(gcd);
1876 isl_int_gcd(gcd, aff->v->el[0], f);
1877 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1878 isl_int_divexact(gcd, f, gcd);
1879 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1880 isl_int_clear(gcd);
1882 return aff;
1885 /* Multiple "aff" by "v".
1887 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1888 __isl_take isl_val *v)
1890 if (!aff || !v)
1891 goto error;
1893 if (isl_val_is_one(v)) {
1894 isl_val_free(v);
1895 return aff;
1898 if (!isl_val_is_rat(v))
1899 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1900 "expecting rational factor", goto error);
1902 aff = isl_aff_scale(aff, v->n);
1903 aff = isl_aff_scale_down(aff, v->d);
1905 isl_val_free(v);
1906 return aff;
1907 error:
1908 isl_aff_free(aff);
1909 isl_val_free(v);
1910 return NULL;
1913 /* Return the result of scaling "aff" down by a factor of "f".
1915 * As a special case, NaN/f = NaN.
1917 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1919 isl_int gcd;
1921 if (!aff)
1922 return NULL;
1923 if (isl_aff_is_nan(aff))
1924 return aff;
1926 if (isl_int_is_one(f))
1927 return aff;
1929 aff = isl_aff_cow(aff);
1930 if (!aff)
1931 return NULL;
1933 if (isl_int_is_zero(f))
1934 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1935 "cannot scale down by zero", return isl_aff_free(aff));
1937 aff->v = isl_vec_cow(aff->v);
1938 if (!aff->v)
1939 return isl_aff_free(aff);
1941 isl_int_init(gcd);
1942 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1943 isl_int_gcd(gcd, gcd, f);
1944 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1945 isl_int_divexact(gcd, f, gcd);
1946 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1947 isl_int_clear(gcd);
1949 return aff;
1952 /* Divide "aff" by "v".
1954 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1955 __isl_take isl_val *v)
1957 if (!aff || !v)
1958 goto error;
1960 if (isl_val_is_one(v)) {
1961 isl_val_free(v);
1962 return aff;
1965 if (!isl_val_is_rat(v))
1966 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1967 "expecting rational factor", goto error);
1968 if (!isl_val_is_pos(v))
1969 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1970 "factor needs to be positive", goto error);
1972 aff = isl_aff_scale(aff, v->d);
1973 aff = isl_aff_scale_down(aff, v->n);
1975 isl_val_free(v);
1976 return aff;
1977 error:
1978 isl_aff_free(aff);
1979 isl_val_free(v);
1980 return NULL;
1983 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1985 isl_int v;
1987 if (f == 1)
1988 return aff;
1990 isl_int_init(v);
1991 isl_int_set_ui(v, f);
1992 aff = isl_aff_scale_down(aff, v);
1993 isl_int_clear(v);
1995 return aff;
1998 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1999 enum isl_dim_type type, unsigned pos, const char *s)
2001 aff = isl_aff_cow(aff);
2002 if (!aff)
2003 return NULL;
2004 if (type == isl_dim_out)
2005 isl_die(aff->v->ctx, isl_error_invalid,
2006 "cannot set name of output/set dimension",
2007 return isl_aff_free(aff));
2008 if (type == isl_dim_in)
2009 type = isl_dim_set;
2010 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2011 if (!aff->ls)
2012 return isl_aff_free(aff);
2014 return aff;
2017 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2018 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2020 aff = isl_aff_cow(aff);
2021 if (!aff)
2022 goto error;
2023 if (type == isl_dim_out)
2024 isl_die(aff->v->ctx, isl_error_invalid,
2025 "cannot set name of output/set dimension",
2026 goto error);
2027 if (type == isl_dim_in)
2028 type = isl_dim_set;
2029 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2030 if (!aff->ls)
2031 return isl_aff_free(aff);
2033 return aff;
2034 error:
2035 isl_id_free(id);
2036 isl_aff_free(aff);
2037 return NULL;
2040 /* Replace the identifier of the input tuple of "aff" by "id".
2041 * type is currently required to be equal to isl_dim_in
2043 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2044 enum isl_dim_type type, __isl_take isl_id *id)
2046 aff = isl_aff_cow(aff);
2047 if (!aff)
2048 goto error;
2049 if (type != isl_dim_out)
2050 isl_die(aff->v->ctx, isl_error_invalid,
2051 "cannot only set id of input tuple", goto error);
2052 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2053 if (!aff->ls)
2054 return isl_aff_free(aff);
2056 return aff;
2057 error:
2058 isl_id_free(id);
2059 isl_aff_free(aff);
2060 return NULL;
2063 /* Exploit the equalities in "eq" to simplify the affine expression
2064 * and the expressions of the integer divisions in the local space.
2065 * The integer divisions in this local space are assumed to appear
2066 * as regular dimensions in "eq".
2068 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2069 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2071 int i, j;
2072 unsigned total;
2073 unsigned n_div;
2075 if (!eq)
2076 goto error;
2077 if (eq->n_eq == 0) {
2078 isl_basic_set_free(eq);
2079 return aff;
2082 aff = isl_aff_cow(aff);
2083 if (!aff)
2084 goto error;
2086 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2087 isl_basic_set_copy(eq));
2088 aff->v = isl_vec_cow(aff->v);
2089 if (!aff->ls || !aff->v)
2090 goto error;
2092 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2093 n_div = eq->n_div;
2094 for (i = 0; i < eq->n_eq; ++i) {
2095 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2096 if (j < 0 || j == 0 || j >= total)
2097 continue;
2099 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2100 &aff->v->el[0]);
2103 isl_basic_set_free(eq);
2104 aff = isl_aff_normalize(aff);
2105 return aff;
2106 error:
2107 isl_basic_set_free(eq);
2108 isl_aff_free(aff);
2109 return NULL;
2112 /* Exploit the equalities in "eq" to simplify the affine expression
2113 * and the expressions of the integer divisions in the local space.
2115 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2116 __isl_take isl_basic_set *eq)
2118 int n_div;
2120 if (!aff || !eq)
2121 goto error;
2122 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2123 if (n_div > 0)
2124 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2125 return isl_aff_substitute_equalities_lifted(aff, eq);
2126 error:
2127 isl_basic_set_free(eq);
2128 isl_aff_free(aff);
2129 return NULL;
2132 /* Look for equalities among the variables shared by context and aff
2133 * and the integer divisions of aff, if any.
2134 * The equalities are then used to eliminate coefficients and/or integer
2135 * divisions from aff.
2137 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2138 __isl_take isl_set *context)
2140 isl_basic_set *hull;
2141 int n_div;
2143 if (!aff)
2144 goto error;
2145 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2146 if (n_div > 0) {
2147 isl_basic_set *bset;
2148 isl_local_space *ls;
2149 context = isl_set_add_dims(context, isl_dim_set, n_div);
2150 ls = isl_aff_get_domain_local_space(aff);
2151 bset = isl_basic_set_from_local_space(ls);
2152 bset = isl_basic_set_lift(bset);
2153 bset = isl_basic_set_flatten(bset);
2154 context = isl_set_intersect(context,
2155 isl_set_from_basic_set(bset));
2158 hull = isl_set_affine_hull(context);
2159 return isl_aff_substitute_equalities_lifted(aff, hull);
2160 error:
2161 isl_aff_free(aff);
2162 isl_set_free(context);
2163 return NULL;
2166 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2167 __isl_take isl_set *context)
2169 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2170 dom_context = isl_set_intersect_params(dom_context, context);
2171 return isl_aff_gist(aff, dom_context);
2174 /* Return a basic set containing those elements in the space
2175 * of aff where it is positive. "rational" should not be set.
2177 * If "aff" is NaN, then it is not positive.
2179 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2180 int rational)
2182 isl_constraint *ineq;
2183 isl_basic_set *bset;
2184 isl_val *c;
2186 if (!aff)
2187 return NULL;
2188 if (isl_aff_is_nan(aff)) {
2189 isl_space *space = isl_aff_get_domain_space(aff);
2190 isl_aff_free(aff);
2191 return isl_basic_set_empty(space);
2193 if (rational)
2194 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2195 "rational sets not supported", goto error);
2197 ineq = isl_inequality_from_aff(aff);
2198 c = isl_constraint_get_constant_val(ineq);
2199 c = isl_val_sub_ui(c, 1);
2200 ineq = isl_constraint_set_constant_val(ineq, c);
2202 bset = isl_basic_set_from_constraint(ineq);
2203 bset = isl_basic_set_simplify(bset);
2204 return bset;
2205 error:
2206 isl_aff_free(aff);
2207 return NULL;
2210 /* Return a basic set containing those elements in the space
2211 * of aff where it is non-negative.
2212 * If "rational" is set, then return a rational basic set.
2214 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2216 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2217 __isl_take isl_aff *aff, int rational)
2219 isl_constraint *ineq;
2220 isl_basic_set *bset;
2222 if (!aff)
2223 return NULL;
2224 if (isl_aff_is_nan(aff)) {
2225 isl_space *space = isl_aff_get_domain_space(aff);
2226 isl_aff_free(aff);
2227 return isl_basic_set_empty(space);
2230 ineq = isl_inequality_from_aff(aff);
2232 bset = isl_basic_set_from_constraint(ineq);
2233 if (rational)
2234 bset = isl_basic_set_set_rational(bset);
2235 bset = isl_basic_set_simplify(bset);
2236 return bset;
2239 /* Return a basic set containing those elements in the space
2240 * of aff where it is non-negative.
2242 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2244 return aff_nonneg_basic_set(aff, 0);
2247 /* Return a basic set containing those elements in the domain space
2248 * of aff where it is negative.
2250 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2252 aff = isl_aff_neg(aff);
2253 aff = isl_aff_add_constant_num_si(aff, -1);
2254 return isl_aff_nonneg_basic_set(aff);
2257 /* Return a basic set containing those elements in the space
2258 * of aff where it is zero.
2259 * If "rational" is set, then return a rational basic set.
2261 * If "aff" is NaN, then it is not zero.
2263 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2264 int rational)
2266 isl_constraint *ineq;
2267 isl_basic_set *bset;
2269 if (!aff)
2270 return NULL;
2271 if (isl_aff_is_nan(aff)) {
2272 isl_space *space = isl_aff_get_domain_space(aff);
2273 isl_aff_free(aff);
2274 return isl_basic_set_empty(space);
2277 ineq = isl_equality_from_aff(aff);
2279 bset = isl_basic_set_from_constraint(ineq);
2280 if (rational)
2281 bset = isl_basic_set_set_rational(bset);
2282 bset = isl_basic_set_simplify(bset);
2283 return bset;
2286 /* Return a basic set containing those elements in the space
2287 * of aff where it is zero.
2289 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2291 return aff_zero_basic_set(aff, 0);
2294 /* Return a basic set containing those elements in the shared space
2295 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2297 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2298 __isl_take isl_aff *aff2)
2300 aff1 = isl_aff_sub(aff1, aff2);
2302 return isl_aff_nonneg_basic_set(aff1);
2305 /* Return a set containing those elements in the shared space
2306 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2308 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2309 __isl_take isl_aff *aff2)
2311 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2314 /* Return a basic set containing those elements in the shared space
2315 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2317 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2318 __isl_take isl_aff *aff2)
2320 return isl_aff_ge_basic_set(aff2, aff1);
2323 /* Return a set containing those elements in the shared space
2324 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2326 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2327 __isl_take isl_aff *aff2)
2329 return isl_aff_ge_set(aff2, aff1);
2332 /* Return a basic set containing those elements in the shared space
2333 * of aff1 and aff2 where aff1 and aff2 are equal.
2335 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2336 __isl_take isl_aff *aff2)
2338 aff1 = isl_aff_sub(aff1, aff2);
2340 return isl_aff_zero_basic_set(aff1);
2343 /* Return a set containing those elements in the shared space
2344 * of aff1 and aff2 where aff1 and aff2 are equal.
2346 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2347 __isl_take isl_aff *aff2)
2349 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2352 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2353 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2355 aff1 = isl_aff_add(aff1, aff2);
2356 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2357 return aff1;
2360 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2362 if (!aff)
2363 return -1;
2365 return 0;
2368 /* Check whether the given affine expression has non-zero coefficient
2369 * for any dimension in the given range or if any of these dimensions
2370 * appear with non-zero coefficients in any of the integer divisions
2371 * involved in the affine expression.
2373 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2374 enum isl_dim_type type, unsigned first, unsigned n)
2376 int i;
2377 isl_ctx *ctx;
2378 int *active = NULL;
2379 isl_bool involves = isl_bool_false;
2381 if (!aff)
2382 return isl_bool_error;
2383 if (n == 0)
2384 return isl_bool_false;
2386 ctx = isl_aff_get_ctx(aff);
2387 if (first + n > isl_aff_dim(aff, type))
2388 isl_die(ctx, isl_error_invalid,
2389 "range out of bounds", return isl_bool_error);
2391 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2392 if (!active)
2393 goto error;
2395 first += isl_local_space_offset(aff->ls, type) - 1;
2396 for (i = 0; i < n; ++i)
2397 if (active[first + i]) {
2398 involves = isl_bool_true;
2399 break;
2402 free(active);
2404 return involves;
2405 error:
2406 free(active);
2407 return isl_bool_error;
2410 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2411 enum isl_dim_type type, unsigned first, unsigned n)
2413 isl_ctx *ctx;
2415 if (!aff)
2416 return NULL;
2417 if (type == isl_dim_out)
2418 isl_die(aff->v->ctx, isl_error_invalid,
2419 "cannot drop output/set dimension",
2420 return isl_aff_free(aff));
2421 if (type == isl_dim_in)
2422 type = isl_dim_set;
2423 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2424 return aff;
2426 ctx = isl_aff_get_ctx(aff);
2427 if (first + n > isl_local_space_dim(aff->ls, type))
2428 isl_die(ctx, isl_error_invalid, "range out of bounds",
2429 return isl_aff_free(aff));
2431 aff = isl_aff_cow(aff);
2432 if (!aff)
2433 return NULL;
2435 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2436 if (!aff->ls)
2437 return isl_aff_free(aff);
2439 first += 1 + isl_local_space_offset(aff->ls, type);
2440 aff->v = isl_vec_drop_els(aff->v, first, n);
2441 if (!aff->v)
2442 return isl_aff_free(aff);
2444 return aff;
2447 /* Project the domain of the affine expression onto its parameter space.
2448 * The affine expression may not involve any of the domain dimensions.
2450 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2452 isl_space *space;
2453 unsigned n;
2454 int involves;
2456 n = isl_aff_dim(aff, isl_dim_in);
2457 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2458 if (involves < 0)
2459 return isl_aff_free(aff);
2460 if (involves)
2461 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2462 "affine expression involves some of the domain dimensions",
2463 return isl_aff_free(aff));
2464 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2465 space = isl_aff_get_domain_space(aff);
2466 space = isl_space_params(space);
2467 aff = isl_aff_reset_domain_space(aff, space);
2468 return aff;
2471 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 isl_ctx *ctx;
2476 if (!aff)
2477 return NULL;
2478 if (type == isl_dim_out)
2479 isl_die(aff->v->ctx, isl_error_invalid,
2480 "cannot insert output/set dimensions",
2481 return isl_aff_free(aff));
2482 if (type == isl_dim_in)
2483 type = isl_dim_set;
2484 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2485 return aff;
2487 ctx = isl_aff_get_ctx(aff);
2488 if (first > isl_local_space_dim(aff->ls, type))
2489 isl_die(ctx, isl_error_invalid, "position out of bounds",
2490 return isl_aff_free(aff));
2492 aff = isl_aff_cow(aff);
2493 if (!aff)
2494 return NULL;
2496 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2497 if (!aff->ls)
2498 return isl_aff_free(aff);
2500 first += 1 + isl_local_space_offset(aff->ls, type);
2501 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2502 if (!aff->v)
2503 return isl_aff_free(aff);
2505 return aff;
2508 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2509 enum isl_dim_type type, unsigned n)
2511 unsigned pos;
2513 pos = isl_aff_dim(aff, type);
2515 return isl_aff_insert_dims(aff, type, pos, n);
2518 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2519 enum isl_dim_type type, unsigned n)
2521 unsigned pos;
2523 pos = isl_pw_aff_dim(pwaff, type);
2525 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2528 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2529 * to dimensions of "dst_type" at "dst_pos".
2531 * We only support moving input dimensions to parameters and vice versa.
2533 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2534 enum isl_dim_type dst_type, unsigned dst_pos,
2535 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2537 unsigned g_dst_pos;
2538 unsigned g_src_pos;
2540 if (!aff)
2541 return NULL;
2542 if (n == 0 &&
2543 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2544 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2545 return aff;
2547 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2548 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2549 "cannot move output/set dimension",
2550 return isl_aff_free(aff));
2551 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2552 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2553 "cannot move divs", return isl_aff_free(aff));
2554 if (dst_type == isl_dim_in)
2555 dst_type = isl_dim_set;
2556 if (src_type == isl_dim_in)
2557 src_type = isl_dim_set;
2559 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2560 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2561 "range out of bounds", return isl_aff_free(aff));
2562 if (dst_type == src_type)
2563 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2564 "moving dims within the same type not supported",
2565 return isl_aff_free(aff));
2567 aff = isl_aff_cow(aff);
2568 if (!aff)
2569 return NULL;
2571 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2572 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2573 if (dst_type > src_type)
2574 g_dst_pos -= n;
2576 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2577 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2578 src_type, src_pos, n);
2579 if (!aff->v || !aff->ls)
2580 return isl_aff_free(aff);
2582 aff = sort_divs(aff);
2584 return aff;
2587 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2589 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2590 return isl_pw_aff_alloc(dom, aff);
2593 #undef PW
2594 #define PW isl_pw_aff
2595 #undef EL
2596 #define EL isl_aff
2597 #undef EL_IS_ZERO
2598 #define EL_IS_ZERO is_empty
2599 #undef ZERO
2600 #define ZERO empty
2601 #undef IS_ZERO
2602 #define IS_ZERO is_empty
2603 #undef FIELD
2604 #define FIELD aff
2605 #undef DEFAULT_IS_ZERO
2606 #define DEFAULT_IS_ZERO 0
2608 #define NO_EVAL
2609 #define NO_OPT
2610 #define NO_LIFT
2611 #define NO_MORPH
2613 #include <isl_pw_templ.c>
2614 #include <isl_pw_hash.c>
2615 #include <isl_pw_union_opt.c>
2617 #undef UNION
2618 #define UNION isl_union_pw_aff
2619 #undef PART
2620 #define PART isl_pw_aff
2621 #undef PARTS
2622 #define PARTS pw_aff
2624 #include <isl_union_single.c>
2625 #include <isl_union_neg.c>
2627 static __isl_give isl_set *align_params_pw_pw_set_and(
2628 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2629 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2630 __isl_take isl_pw_aff *pwaff2))
2632 if (!pwaff1 || !pwaff2)
2633 goto error;
2634 if (isl_space_match(pwaff1->dim, isl_dim_param,
2635 pwaff2->dim, isl_dim_param))
2636 return fn(pwaff1, pwaff2);
2637 if (!isl_space_has_named_params(pwaff1->dim) ||
2638 !isl_space_has_named_params(pwaff2->dim))
2639 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2640 "unaligned unnamed parameters", goto error);
2641 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2642 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2643 return fn(pwaff1, pwaff2);
2644 error:
2645 isl_pw_aff_free(pwaff1);
2646 isl_pw_aff_free(pwaff2);
2647 return NULL;
2650 /* Align the parameters of the to isl_pw_aff arguments and
2651 * then apply a function "fn" on them that returns an isl_map.
2653 static __isl_give isl_map *align_params_pw_pw_map_and(
2654 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2655 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2656 __isl_take isl_pw_aff *pa2))
2658 if (!pa1 || !pa2)
2659 goto error;
2660 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2661 return fn(pa1, pa2);
2662 if (!isl_space_has_named_params(pa1->dim) ||
2663 !isl_space_has_named_params(pa2->dim))
2664 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2665 "unaligned unnamed parameters", goto error);
2666 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2667 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2668 return fn(pa1, pa2);
2669 error:
2670 isl_pw_aff_free(pa1);
2671 isl_pw_aff_free(pa2);
2672 return NULL;
2675 /* Compute a piecewise quasi-affine expression with a domain that
2676 * is the union of those of pwaff1 and pwaff2 and such that on each
2677 * cell, the quasi-affine expression is the maximum of those of pwaff1
2678 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2679 * cell, then the associated expression is the defined one.
2681 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2682 __isl_take isl_pw_aff *pwaff2)
2684 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2687 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2688 __isl_take isl_pw_aff *pwaff2)
2690 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2691 &pw_aff_union_max);
2694 /* Compute a piecewise quasi-affine expression with a domain that
2695 * is the union of those of pwaff1 and pwaff2 and such that on each
2696 * cell, the quasi-affine expression is the minimum of those of pwaff1
2697 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2698 * cell, then the associated expression is the defined one.
2700 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2701 __isl_take isl_pw_aff *pwaff2)
2703 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2706 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2707 __isl_take isl_pw_aff *pwaff2)
2709 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2710 &pw_aff_union_min);
2713 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2714 __isl_take isl_pw_aff *pwaff2, int max)
2716 if (max)
2717 return isl_pw_aff_union_max(pwaff1, pwaff2);
2718 else
2719 return isl_pw_aff_union_min(pwaff1, pwaff2);
2722 /* Construct a map with as domain the domain of pwaff and
2723 * one-dimensional range corresponding to the affine expressions.
2725 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2727 int i;
2728 isl_space *dim;
2729 isl_map *map;
2731 if (!pwaff)
2732 return NULL;
2734 dim = isl_pw_aff_get_space(pwaff);
2735 map = isl_map_empty(dim);
2737 for (i = 0; i < pwaff->n; ++i) {
2738 isl_basic_map *bmap;
2739 isl_map *map_i;
2741 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2742 map_i = isl_map_from_basic_map(bmap);
2743 map_i = isl_map_intersect_domain(map_i,
2744 isl_set_copy(pwaff->p[i].set));
2745 map = isl_map_union_disjoint(map, map_i);
2748 isl_pw_aff_free(pwaff);
2750 return map;
2753 /* Construct a map with as domain the domain of pwaff and
2754 * one-dimensional range corresponding to the affine expressions.
2756 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2758 if (!pwaff)
2759 return NULL;
2760 if (isl_space_is_set(pwaff->dim))
2761 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2762 "space of input is not a map", goto error);
2763 return map_from_pw_aff(pwaff);
2764 error:
2765 isl_pw_aff_free(pwaff);
2766 return NULL;
2769 /* Construct a one-dimensional set with as parameter domain
2770 * the domain of pwaff and the single set dimension
2771 * corresponding to the affine expressions.
2773 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2775 if (!pwaff)
2776 return NULL;
2777 if (!isl_space_is_set(pwaff->dim))
2778 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2779 "space of input is not a set", goto error);
2780 return map_from_pw_aff(pwaff);
2781 error:
2782 isl_pw_aff_free(pwaff);
2783 return NULL;
2786 /* Return a set containing those elements in the domain
2787 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2788 * does not satisfy "fn" (if complement is 1).
2790 * The pieces with a NaN never belong to the result since
2791 * NaN does not satisfy any property.
2793 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2794 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2795 int complement)
2797 int i;
2798 isl_set *set;
2800 if (!pwaff)
2801 return NULL;
2803 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2805 for (i = 0; i < pwaff->n; ++i) {
2806 isl_basic_set *bset;
2807 isl_set *set_i, *locus;
2808 int rational;
2810 if (isl_aff_is_nan(pwaff->p[i].aff))
2811 continue;
2813 rational = isl_set_has_rational(pwaff->p[i].set);
2814 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2815 locus = isl_set_from_basic_set(bset);
2816 set_i = isl_set_copy(pwaff->p[i].set);
2817 if (complement)
2818 set_i = isl_set_subtract(set_i, locus);
2819 else
2820 set_i = isl_set_intersect(set_i, locus);
2821 set = isl_set_union_disjoint(set, set_i);
2824 isl_pw_aff_free(pwaff);
2826 return set;
2829 /* Return a set containing those elements in the domain
2830 * of "pa" where it is positive.
2832 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2834 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2837 /* Return a set containing those elements in the domain
2838 * of pwaff where it is non-negative.
2840 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2842 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2845 /* Return a set containing those elements in the domain
2846 * of pwaff where it is zero.
2848 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2850 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2853 /* Return a set containing those elements in the domain
2854 * of pwaff where it is not zero.
2856 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2858 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2861 /* Return a set containing those elements in the shared domain
2862 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2864 * We compute the difference on the shared domain and then construct
2865 * the set of values where this difference is non-negative.
2866 * If strict is set, we first subtract 1 from the difference.
2867 * If equal is set, we only return the elements where pwaff1 and pwaff2
2868 * are equal.
2870 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2871 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2873 isl_set *set1, *set2;
2875 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2876 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2877 set1 = isl_set_intersect(set1, set2);
2878 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2879 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2880 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2882 if (strict) {
2883 isl_space *dim = isl_set_get_space(set1);
2884 isl_aff *aff;
2885 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2886 aff = isl_aff_add_constant_si(aff, -1);
2887 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2888 } else
2889 isl_set_free(set1);
2891 if (equal)
2892 return isl_pw_aff_zero_set(pwaff1);
2893 return isl_pw_aff_nonneg_set(pwaff1);
2896 /* Return a set containing those elements in the shared domain
2897 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2899 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2900 __isl_take isl_pw_aff *pwaff2)
2902 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2905 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2906 __isl_take isl_pw_aff *pwaff2)
2908 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2911 /* Return a set containing those elements in the shared domain
2912 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2914 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2915 __isl_take isl_pw_aff *pwaff2)
2917 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2920 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2921 __isl_take isl_pw_aff *pwaff2)
2923 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2926 /* Return a set containing those elements in the shared domain
2927 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2929 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2930 __isl_take isl_pw_aff *pwaff2)
2932 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2935 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2936 __isl_take isl_pw_aff *pwaff2)
2938 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2941 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2942 __isl_take isl_pw_aff *pwaff2)
2944 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2947 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2948 __isl_take isl_pw_aff *pwaff2)
2950 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2953 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2954 * where the function values are ordered in the same way as "order",
2955 * which returns a set in the shared domain of its two arguments.
2956 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2958 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2959 * We first pull back the two functions such that they are defined on
2960 * the domain [A -> B]. Then we apply "order", resulting in a set
2961 * in the space [A -> B]. Finally, we unwrap this set to obtain
2962 * a map in the space A -> B.
2964 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2965 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2966 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2967 __isl_take isl_pw_aff *pa2))
2969 isl_space *space1, *space2;
2970 isl_multi_aff *ma;
2971 isl_set *set;
2973 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2974 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2975 space1 = isl_space_map_from_domain_and_range(space1, space2);
2976 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2977 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2978 ma = isl_multi_aff_range_map(space1);
2979 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2980 set = order(pa1, pa2);
2982 return isl_set_unwrap(set);
2985 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2986 * where the function values are equal.
2987 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2989 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2990 __isl_take isl_pw_aff *pa2)
2992 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2995 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2996 * where the function values are equal.
2998 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2999 __isl_take isl_pw_aff *pa2)
3001 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3004 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3005 * where the function value of "pa1" is less than the function value of "pa2".
3006 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3008 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3009 __isl_take isl_pw_aff *pa2)
3011 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3014 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3015 * where the function value of "pa1" is less than the function value of "pa2".
3017 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3018 __isl_take isl_pw_aff *pa2)
3020 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3023 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3024 * where the function value of "pa1" is greater than the function value
3025 * of "pa2".
3026 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3028 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3029 __isl_take isl_pw_aff *pa2)
3031 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function value of "pa1" is greater than the function value
3036 * of "pa2".
3038 __isl_give isl_map *isl_pw_aff_gt_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_gt_map_aligned);
3044 /* Return a set containing those elements in the shared domain
3045 * of the elements of list1 and list2 where each element in list1
3046 * has the relation specified by "fn" with each element in list2.
3048 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3049 __isl_take isl_pw_aff_list *list2,
3050 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3051 __isl_take isl_pw_aff *pwaff2))
3053 int i, j;
3054 isl_ctx *ctx;
3055 isl_set *set;
3057 if (!list1 || !list2)
3058 goto error;
3060 ctx = isl_pw_aff_list_get_ctx(list1);
3061 if (list1->n < 1 || list2->n < 1)
3062 isl_die(ctx, isl_error_invalid,
3063 "list should contain at least one element", goto error);
3065 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3066 for (i = 0; i < list1->n; ++i)
3067 for (j = 0; j < list2->n; ++j) {
3068 isl_set *set_ij;
3070 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3071 isl_pw_aff_copy(list2->p[j]));
3072 set = isl_set_intersect(set, set_ij);
3075 isl_pw_aff_list_free(list1);
3076 isl_pw_aff_list_free(list2);
3077 return set;
3078 error:
3079 isl_pw_aff_list_free(list1);
3080 isl_pw_aff_list_free(list2);
3081 return NULL;
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 * is equal to each element in list2.
3088 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3089 __isl_take isl_pw_aff_list *list2)
3091 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3094 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3095 __isl_take isl_pw_aff_list *list2)
3097 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3100 /* Return a set containing those elements in the shared domain
3101 * of the elements of list1 and list2 where each element in list1
3102 * is less than or equal to each element in list2.
3104 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3105 __isl_take isl_pw_aff_list *list2)
3107 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3110 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3111 __isl_take isl_pw_aff_list *list2)
3113 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3116 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3117 __isl_take isl_pw_aff_list *list2)
3119 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3122 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3123 __isl_take isl_pw_aff_list *list2)
3125 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3129 /* Return a set containing those elements in the shared domain
3130 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3132 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3133 __isl_take isl_pw_aff *pwaff2)
3135 isl_set *set_lt, *set_gt;
3137 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3138 isl_pw_aff_copy(pwaff2));
3139 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3140 return isl_set_union_disjoint(set_lt, set_gt);
3143 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3144 __isl_take isl_pw_aff *pwaff2)
3146 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3149 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3150 isl_int v)
3152 int i;
3154 if (isl_int_is_one(v))
3155 return pwaff;
3156 if (!isl_int_is_pos(v))
3157 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3158 "factor needs to be positive",
3159 return isl_pw_aff_free(pwaff));
3160 pwaff = isl_pw_aff_cow(pwaff);
3161 if (!pwaff)
3162 return NULL;
3163 if (pwaff->n == 0)
3164 return pwaff;
3166 for (i = 0; i < pwaff->n; ++i) {
3167 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3168 if (!pwaff->p[i].aff)
3169 return isl_pw_aff_free(pwaff);
3172 return pwaff;
3175 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3177 int i;
3179 pwaff = isl_pw_aff_cow(pwaff);
3180 if (!pwaff)
3181 return NULL;
3182 if (pwaff->n == 0)
3183 return pwaff;
3185 for (i = 0; i < pwaff->n; ++i) {
3186 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3187 if (!pwaff->p[i].aff)
3188 return isl_pw_aff_free(pwaff);
3191 return pwaff;
3194 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3196 int i;
3198 pwaff = isl_pw_aff_cow(pwaff);
3199 if (!pwaff)
3200 return NULL;
3201 if (pwaff->n == 0)
3202 return pwaff;
3204 for (i = 0; i < pwaff->n; ++i) {
3205 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3206 if (!pwaff->p[i].aff)
3207 return isl_pw_aff_free(pwaff);
3210 return pwaff;
3213 /* Assuming that "cond1" and "cond2" are disjoint,
3214 * return an affine expression that is equal to pwaff1 on cond1
3215 * and to pwaff2 on cond2.
3217 static __isl_give isl_pw_aff *isl_pw_aff_select(
3218 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3219 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3221 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3222 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3224 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3227 /* Return an affine expression that is equal to pwaff_true for elements
3228 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3229 * is zero.
3230 * That is, return cond ? pwaff_true : pwaff_false;
3232 * If "cond" involves and NaN, then we conservatively return a NaN
3233 * on its entire domain. In principle, we could consider the pieces
3234 * where it is NaN separately from those where it is not.
3236 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3237 * then only use the domain of "cond" to restrict the domain.
3239 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3240 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3242 isl_set *cond_true, *cond_false;
3243 isl_bool equal;
3245 if (!cond)
3246 goto error;
3247 if (isl_pw_aff_involves_nan(cond)) {
3248 isl_space *space = isl_pw_aff_get_domain_space(cond);
3249 isl_local_space *ls = isl_local_space_from_space(space);
3250 isl_pw_aff_free(cond);
3251 isl_pw_aff_free(pwaff_true);
3252 isl_pw_aff_free(pwaff_false);
3253 return isl_pw_aff_nan_on_domain(ls);
3256 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3257 isl_pw_aff_get_space(pwaff_false));
3258 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3259 isl_pw_aff_get_space(pwaff_true));
3260 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3261 if (equal < 0)
3262 goto error;
3263 if (equal) {
3264 isl_set *dom;
3266 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3267 isl_pw_aff_free(pwaff_false);
3268 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3271 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3272 cond_false = isl_pw_aff_zero_set(cond);
3273 return isl_pw_aff_select(cond_true, pwaff_true,
3274 cond_false, pwaff_false);
3275 error:
3276 isl_pw_aff_free(cond);
3277 isl_pw_aff_free(pwaff_true);
3278 isl_pw_aff_free(pwaff_false);
3279 return NULL;
3282 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3284 if (!aff)
3285 return isl_bool_error;
3287 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3290 /* Check whether pwaff is a piecewise constant.
3292 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3294 int i;
3296 if (!pwaff)
3297 return isl_bool_error;
3299 for (i = 0; i < pwaff->n; ++i) {
3300 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3301 if (is_cst < 0 || !is_cst)
3302 return is_cst;
3305 return isl_bool_true;
3308 /* Are all elements of "mpa" piecewise constants?
3310 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3312 int i;
3314 if (!mpa)
3315 return isl_bool_error;
3317 for (i = 0; i < mpa->n; ++i) {
3318 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3319 if (is_cst < 0 || !is_cst)
3320 return is_cst;
3323 return isl_bool_true;
3326 /* Return the product of "aff1" and "aff2".
3328 * If either of the two is NaN, then the result is NaN.
3330 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3332 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3333 __isl_take isl_aff *aff2)
3335 if (!aff1 || !aff2)
3336 goto error;
3338 if (isl_aff_is_nan(aff1)) {
3339 isl_aff_free(aff2);
3340 return aff1;
3342 if (isl_aff_is_nan(aff2)) {
3343 isl_aff_free(aff1);
3344 return aff2;
3347 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3348 return isl_aff_mul(aff2, aff1);
3350 if (!isl_aff_is_cst(aff2))
3351 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3352 "at least one affine expression should be constant",
3353 goto error);
3355 aff1 = isl_aff_cow(aff1);
3356 if (!aff1 || !aff2)
3357 goto error;
3359 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3360 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3362 isl_aff_free(aff2);
3363 return aff1;
3364 error:
3365 isl_aff_free(aff1);
3366 isl_aff_free(aff2);
3367 return NULL;
3370 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3372 * If either of the two is NaN, then the result is NaN.
3374 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3375 __isl_take isl_aff *aff2)
3377 int is_cst;
3378 int neg;
3380 if (!aff1 || !aff2)
3381 goto error;
3383 if (isl_aff_is_nan(aff1)) {
3384 isl_aff_free(aff2);
3385 return aff1;
3387 if (isl_aff_is_nan(aff2)) {
3388 isl_aff_free(aff1);
3389 return aff2;
3392 is_cst = isl_aff_is_cst(aff2);
3393 if (is_cst < 0)
3394 goto error;
3395 if (!is_cst)
3396 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3397 "second argument should be a constant", goto error);
3399 if (!aff2)
3400 goto error;
3402 neg = isl_int_is_neg(aff2->v->el[1]);
3403 if (neg) {
3404 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3405 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3408 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3409 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3411 if (neg) {
3412 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3413 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3416 isl_aff_free(aff2);
3417 return aff1;
3418 error:
3419 isl_aff_free(aff1);
3420 isl_aff_free(aff2);
3421 return NULL;
3424 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3425 __isl_take isl_pw_aff *pwaff2)
3427 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3430 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3431 __isl_take isl_pw_aff *pwaff2)
3433 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3436 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3437 __isl_take isl_pw_aff *pwaff2)
3439 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3442 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3443 __isl_take isl_pw_aff *pwaff2)
3445 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3448 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3449 __isl_take isl_pw_aff *pwaff2)
3451 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3454 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3455 __isl_take isl_pw_aff *pa2)
3457 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3460 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3462 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3463 __isl_take isl_pw_aff *pa2)
3465 int is_cst;
3467 is_cst = isl_pw_aff_is_cst(pa2);
3468 if (is_cst < 0)
3469 goto error;
3470 if (!is_cst)
3471 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3472 "second argument should be a piecewise constant",
3473 goto error);
3474 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3475 error:
3476 isl_pw_aff_free(pa1);
3477 isl_pw_aff_free(pa2);
3478 return NULL;
3481 /* Compute the quotient of the integer division of "pa1" by "pa2"
3482 * with rounding towards zero.
3483 * "pa2" is assumed to be a piecewise constant.
3485 * In particular, return
3487 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3490 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3491 __isl_take isl_pw_aff *pa2)
3493 int is_cst;
3494 isl_set *cond;
3495 isl_pw_aff *f, *c;
3497 is_cst = isl_pw_aff_is_cst(pa2);
3498 if (is_cst < 0)
3499 goto error;
3500 if (!is_cst)
3501 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3502 "second argument should be a piecewise constant",
3503 goto error);
3505 pa1 = isl_pw_aff_div(pa1, pa2);
3507 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3508 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3509 c = isl_pw_aff_ceil(pa1);
3510 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3511 error:
3512 isl_pw_aff_free(pa1);
3513 isl_pw_aff_free(pa2);
3514 return NULL;
3517 /* Compute the remainder of the integer division of "pa1" by "pa2"
3518 * with rounding towards zero.
3519 * "pa2" is assumed to be a piecewise constant.
3521 * In particular, return
3523 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3526 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3527 __isl_take isl_pw_aff *pa2)
3529 int is_cst;
3530 isl_pw_aff *res;
3532 is_cst = isl_pw_aff_is_cst(pa2);
3533 if (is_cst < 0)
3534 goto error;
3535 if (!is_cst)
3536 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3537 "second argument should be a piecewise constant",
3538 goto error);
3539 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3540 res = isl_pw_aff_mul(pa2, res);
3541 res = isl_pw_aff_sub(pa1, res);
3542 return res;
3543 error:
3544 isl_pw_aff_free(pa1);
3545 isl_pw_aff_free(pa2);
3546 return NULL;
3549 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3550 __isl_take isl_pw_aff *pwaff2)
3552 isl_set *le;
3553 isl_set *dom;
3555 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3556 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3557 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3558 isl_pw_aff_copy(pwaff2));
3559 dom = isl_set_subtract(dom, isl_set_copy(le));
3560 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3563 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3564 __isl_take isl_pw_aff *pwaff2)
3566 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3569 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3570 __isl_take isl_pw_aff *pwaff2)
3572 isl_set *ge;
3573 isl_set *dom;
3575 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3576 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3577 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3578 isl_pw_aff_copy(pwaff2));
3579 dom = isl_set_subtract(dom, isl_set_copy(ge));
3580 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3583 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3584 __isl_take isl_pw_aff *pwaff2)
3586 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3589 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3590 __isl_take isl_pw_aff_list *list,
3591 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3592 __isl_take isl_pw_aff *pwaff2))
3594 int i;
3595 isl_ctx *ctx;
3596 isl_pw_aff *res;
3598 if (!list)
3599 return NULL;
3601 ctx = isl_pw_aff_list_get_ctx(list);
3602 if (list->n < 1)
3603 isl_die(ctx, isl_error_invalid,
3604 "list should contain at least one element", goto error);
3606 res = isl_pw_aff_copy(list->p[0]);
3607 for (i = 1; i < list->n; ++i)
3608 res = fn(res, isl_pw_aff_copy(list->p[i]));
3610 isl_pw_aff_list_free(list);
3611 return res;
3612 error:
3613 isl_pw_aff_list_free(list);
3614 return NULL;
3617 /* Return an isl_pw_aff that maps each element in the intersection of the
3618 * domains of the elements of list to the minimal corresponding affine
3619 * expression.
3621 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3623 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3626 /* Return an isl_pw_aff that maps each element in the intersection of the
3627 * domains of the elements of list to the maximal corresponding affine
3628 * expression.
3630 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3632 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3635 /* Mark the domains of "pwaff" as rational.
3637 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3639 int i;
3641 pwaff = isl_pw_aff_cow(pwaff);
3642 if (!pwaff)
3643 return NULL;
3644 if (pwaff->n == 0)
3645 return pwaff;
3647 for (i = 0; i < pwaff->n; ++i) {
3648 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3649 if (!pwaff->p[i].set)
3650 return isl_pw_aff_free(pwaff);
3653 return pwaff;
3656 /* Mark the domains of the elements of "list" as rational.
3658 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3659 __isl_take isl_pw_aff_list *list)
3661 int i, n;
3663 if (!list)
3664 return NULL;
3665 if (list->n == 0)
3666 return list;
3668 n = list->n;
3669 for (i = 0; i < n; ++i) {
3670 isl_pw_aff *pa;
3672 pa = isl_pw_aff_list_get_pw_aff(list, i);
3673 pa = isl_pw_aff_set_rational(pa);
3674 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3677 return list;
3680 /* Do the parameters of "aff" match those of "space"?
3682 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3683 __isl_keep isl_space *space)
3685 isl_space *aff_space;
3686 int match;
3688 if (!aff || !space)
3689 return -1;
3691 aff_space = isl_aff_get_domain_space(aff);
3693 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3695 isl_space_free(aff_space);
3696 return match;
3699 /* Check that the domain space of "aff" matches "space".
3701 * Return 0 on success and -1 on error.
3703 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3704 __isl_keep isl_space *space)
3706 isl_space *aff_space;
3707 int match;
3709 if (!aff || !space)
3710 return -1;
3712 aff_space = isl_aff_get_domain_space(aff);
3714 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3715 if (match < 0)
3716 goto error;
3717 if (!match)
3718 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3719 "parameters don't match", goto error);
3720 match = isl_space_tuple_is_equal(space, isl_dim_in,
3721 aff_space, isl_dim_set);
3722 if (match < 0)
3723 goto error;
3724 if (!match)
3725 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3726 "domains don't match", goto error);
3727 isl_space_free(aff_space);
3728 return 0;
3729 error:
3730 isl_space_free(aff_space);
3731 return -1;
3734 #undef BASE
3735 #define BASE aff
3736 #undef DOMBASE
3737 #define DOMBASE set
3738 #define NO_DOMAIN
3740 #include <isl_multi_templ.c>
3741 #include <isl_multi_apply_set.c>
3742 #include <isl_multi_cmp.c>
3743 #include <isl_multi_floor.c>
3744 #include <isl_multi_gist.c>
3746 #undef NO_DOMAIN
3748 /* Remove any internal structure of the domain of "ma".
3749 * If there is any such internal structure in the input,
3750 * then the name of the corresponding space is also removed.
3752 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3753 __isl_take isl_multi_aff *ma)
3755 isl_space *space;
3757 if (!ma)
3758 return NULL;
3760 if (!ma->space->nested[0])
3761 return ma;
3763 space = isl_multi_aff_get_space(ma);
3764 space = isl_space_flatten_domain(space);
3765 ma = isl_multi_aff_reset_space(ma, space);
3767 return ma;
3770 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3771 * of the space to its domain.
3773 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3775 int i, n_in;
3776 isl_local_space *ls;
3777 isl_multi_aff *ma;
3779 if (!space)
3780 return NULL;
3781 if (!isl_space_is_map(space))
3782 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3783 "not a map space", goto error);
3785 n_in = isl_space_dim(space, isl_dim_in);
3786 space = isl_space_domain_map(space);
3788 ma = isl_multi_aff_alloc(isl_space_copy(space));
3789 if (n_in == 0) {
3790 isl_space_free(space);
3791 return ma;
3794 space = isl_space_domain(space);
3795 ls = isl_local_space_from_space(space);
3796 for (i = 0; i < n_in; ++i) {
3797 isl_aff *aff;
3799 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3800 isl_dim_set, i);
3801 ma = isl_multi_aff_set_aff(ma, i, aff);
3803 isl_local_space_free(ls);
3804 return ma;
3805 error:
3806 isl_space_free(space);
3807 return NULL;
3810 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3811 * of the space to its range.
3813 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3815 int i, n_in, n_out;
3816 isl_local_space *ls;
3817 isl_multi_aff *ma;
3819 if (!space)
3820 return NULL;
3821 if (!isl_space_is_map(space))
3822 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3823 "not a map space", goto error);
3825 n_in = isl_space_dim(space, isl_dim_in);
3826 n_out = isl_space_dim(space, isl_dim_out);
3827 space = isl_space_range_map(space);
3829 ma = isl_multi_aff_alloc(isl_space_copy(space));
3830 if (n_out == 0) {
3831 isl_space_free(space);
3832 return ma;
3835 space = isl_space_domain(space);
3836 ls = isl_local_space_from_space(space);
3837 for (i = 0; i < n_out; ++i) {
3838 isl_aff *aff;
3840 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3841 isl_dim_set, n_in + i);
3842 ma = isl_multi_aff_set_aff(ma, i, aff);
3844 isl_local_space_free(ls);
3845 return ma;
3846 error:
3847 isl_space_free(space);
3848 return NULL;
3851 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3852 * of the space to its range.
3854 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3855 __isl_take isl_space *space)
3857 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3860 /* Given the space of a set and a range of set dimensions,
3861 * construct an isl_multi_aff that projects out those dimensions.
3863 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3864 __isl_take isl_space *space, enum isl_dim_type type,
3865 unsigned first, unsigned n)
3867 int i, dim;
3868 isl_local_space *ls;
3869 isl_multi_aff *ma;
3871 if (!space)
3872 return NULL;
3873 if (!isl_space_is_set(space))
3874 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3875 "expecting set space", goto error);
3876 if (type != isl_dim_set)
3877 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3878 "only set dimensions can be projected out", goto error);
3880 dim = isl_space_dim(space, isl_dim_set);
3881 if (first + n > dim)
3882 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3883 "range out of bounds", goto error);
3885 space = isl_space_from_domain(space);
3886 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3888 if (dim == n)
3889 return isl_multi_aff_alloc(space);
3891 ma = isl_multi_aff_alloc(isl_space_copy(space));
3892 space = isl_space_domain(space);
3893 ls = isl_local_space_from_space(space);
3895 for (i = 0; i < first; ++i) {
3896 isl_aff *aff;
3898 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3899 isl_dim_set, i);
3900 ma = isl_multi_aff_set_aff(ma, i, aff);
3903 for (i = 0; i < dim - (first + n); ++i) {
3904 isl_aff *aff;
3906 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3907 isl_dim_set, first + n + i);
3908 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3911 isl_local_space_free(ls);
3912 return ma;
3913 error:
3914 isl_space_free(space);
3915 return NULL;
3918 /* Given the space of a set and a range of set dimensions,
3919 * construct an isl_pw_multi_aff that projects out those dimensions.
3921 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3922 __isl_take isl_space *space, enum isl_dim_type type,
3923 unsigned first, unsigned n)
3925 isl_multi_aff *ma;
3927 ma = isl_multi_aff_project_out_map(space, type, first, n);
3928 return isl_pw_multi_aff_from_multi_aff(ma);
3931 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3932 * domain.
3934 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3935 __isl_take isl_multi_aff *ma)
3937 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3938 return isl_pw_multi_aff_alloc(dom, ma);
3941 /* Create a piecewise multi-affine expression in the given space that maps each
3942 * input dimension to the corresponding output dimension.
3944 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3945 __isl_take isl_space *space)
3947 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3950 /* Exploit the equalities in "eq" to simplify the affine expressions.
3952 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3953 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3955 int i;
3957 maff = isl_multi_aff_cow(maff);
3958 if (!maff || !eq)
3959 goto error;
3961 for (i = 0; i < maff->n; ++i) {
3962 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3963 isl_basic_set_copy(eq));
3964 if (!maff->p[i])
3965 goto error;
3968 isl_basic_set_free(eq);
3969 return maff;
3970 error:
3971 isl_basic_set_free(eq);
3972 isl_multi_aff_free(maff);
3973 return NULL;
3976 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3977 isl_int f)
3979 int i;
3981 maff = isl_multi_aff_cow(maff);
3982 if (!maff)
3983 return NULL;
3985 for (i = 0; i < maff->n; ++i) {
3986 maff->p[i] = isl_aff_scale(maff->p[i], f);
3987 if (!maff->p[i])
3988 return isl_multi_aff_free(maff);
3991 return maff;
3994 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3995 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3997 maff1 = isl_multi_aff_add(maff1, maff2);
3998 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3999 return maff1;
4002 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4004 if (!maff)
4005 return -1;
4007 return 0;
4010 /* Return the set of domain elements where "ma1" is lexicographically
4011 * smaller than or equal to "ma2".
4013 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4014 __isl_take isl_multi_aff *ma2)
4016 return isl_multi_aff_lex_ge_set(ma2, ma1);
4019 /* Return the set of domain elements where "ma1" is lexicographically
4020 * smaller than "ma2".
4022 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4023 __isl_take isl_multi_aff *ma2)
4025 return isl_multi_aff_lex_gt_set(ma2, ma1);
4028 /* Return the set of domain elements where "ma1" and "ma2"
4029 * satisfy "order".
4031 static __isl_give isl_set *isl_multi_aff_order_set(
4032 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4033 __isl_give isl_map *order(__isl_take isl_space *set_space))
4035 isl_space *space;
4036 isl_map *map1, *map2;
4037 isl_map *map, *ge;
4039 map1 = isl_map_from_multi_aff(ma1);
4040 map2 = isl_map_from_multi_aff(ma2);
4041 map = isl_map_range_product(map1, map2);
4042 space = isl_space_range(isl_map_get_space(map));
4043 space = isl_space_domain(isl_space_unwrap(space));
4044 ge = order(space);
4045 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4047 return isl_map_domain(map);
4050 /* Return the set of domain elements where "ma1" is lexicographically
4051 * greater than or equal to "ma2".
4053 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4054 __isl_take isl_multi_aff *ma2)
4056 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4059 /* Return the set of domain elements where "ma1" is lexicographically
4060 * greater than "ma2".
4062 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4063 __isl_take isl_multi_aff *ma2)
4065 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4068 #undef PW
4069 #define PW isl_pw_multi_aff
4070 #undef EL
4071 #define EL isl_multi_aff
4072 #undef EL_IS_ZERO
4073 #define EL_IS_ZERO is_empty
4074 #undef ZERO
4075 #define ZERO empty
4076 #undef IS_ZERO
4077 #define IS_ZERO is_empty
4078 #undef FIELD
4079 #define FIELD maff
4080 #undef DEFAULT_IS_ZERO
4081 #define DEFAULT_IS_ZERO 0
4083 #define NO_SUB
4084 #define NO_EVAL
4085 #define NO_OPT
4086 #define NO_INVOLVES_DIMS
4087 #define NO_INSERT_DIMS
4088 #define NO_LIFT
4089 #define NO_MORPH
4091 #include <isl_pw_templ.c>
4092 #include <isl_pw_union_opt.c>
4094 #undef NO_SUB
4096 #undef UNION
4097 #define UNION isl_union_pw_multi_aff
4098 #undef PART
4099 #define PART isl_pw_multi_aff
4100 #undef PARTS
4101 #define PARTS pw_multi_aff
4103 #include <isl_union_multi.c>
4104 #include <isl_union_neg.c>
4106 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4107 __isl_take isl_pw_multi_aff *pma1,
4108 __isl_take isl_pw_multi_aff *pma2)
4110 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4111 &isl_multi_aff_lex_ge_set);
4114 /* Given two piecewise multi affine expressions, return a piecewise
4115 * multi-affine expression defined on the union of the definition domains
4116 * of the inputs that is equal to the lexicographic maximum of the two
4117 * inputs on each cell. If only one of the two inputs is defined on
4118 * a given cell, then it is considered to be the maximum.
4120 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4121 __isl_take isl_pw_multi_aff *pma1,
4122 __isl_take isl_pw_multi_aff *pma2)
4124 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4125 &pw_multi_aff_union_lexmax);
4128 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4129 __isl_take isl_pw_multi_aff *pma1,
4130 __isl_take isl_pw_multi_aff *pma2)
4132 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4133 &isl_multi_aff_lex_le_set);
4136 /* Given two piecewise multi affine expressions, return a piecewise
4137 * multi-affine expression defined on the union of the definition domains
4138 * of the inputs that is equal to the lexicographic minimum of the two
4139 * inputs on each cell. If only one of the two inputs is defined on
4140 * a given cell, then it is considered to be the minimum.
4142 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4143 __isl_take isl_pw_multi_aff *pma1,
4144 __isl_take isl_pw_multi_aff *pma2)
4146 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4147 &pw_multi_aff_union_lexmin);
4150 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4151 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4153 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4154 &isl_multi_aff_add);
4157 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4158 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4160 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4161 &pw_multi_aff_add);
4164 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4165 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4167 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4168 &isl_multi_aff_sub);
4171 /* Subtract "pma2" from "pma1" and return the result.
4173 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4174 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4176 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4177 &pw_multi_aff_sub);
4180 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4181 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4183 return isl_pw_multi_aff_union_add_(pma1, pma2);
4186 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4187 * with the actual sum on the shared domain and
4188 * the defined expression on the symmetric difference of the domains.
4190 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4191 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4193 return isl_union_pw_aff_union_add_(upa1, upa2);
4196 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4197 * with the actual sum on the shared domain and
4198 * the defined expression on the symmetric difference of the domains.
4200 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4201 __isl_take isl_union_pw_multi_aff *upma1,
4202 __isl_take isl_union_pw_multi_aff *upma2)
4204 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4207 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4208 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4210 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4211 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4213 int i, j, n;
4214 isl_space *space;
4215 isl_pw_multi_aff *res;
4217 if (!pma1 || !pma2)
4218 goto error;
4220 n = pma1->n * pma2->n;
4221 space = isl_space_product(isl_space_copy(pma1->dim),
4222 isl_space_copy(pma2->dim));
4223 res = isl_pw_multi_aff_alloc_size(space, n);
4225 for (i = 0; i < pma1->n; ++i) {
4226 for (j = 0; j < pma2->n; ++j) {
4227 isl_set *domain;
4228 isl_multi_aff *ma;
4230 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4231 isl_set_copy(pma2->p[j].set));
4232 ma = isl_multi_aff_product(
4233 isl_multi_aff_copy(pma1->p[i].maff),
4234 isl_multi_aff_copy(pma2->p[j].maff));
4235 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4239 isl_pw_multi_aff_free(pma1);
4240 isl_pw_multi_aff_free(pma2);
4241 return res;
4242 error:
4243 isl_pw_multi_aff_free(pma1);
4244 isl_pw_multi_aff_free(pma2);
4245 return NULL;
4248 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4249 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4251 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4252 &pw_multi_aff_product);
4255 /* Construct a map mapping the domain of the piecewise multi-affine expression
4256 * to its range, with each dimension in the range equated to the
4257 * corresponding affine expression on its cell.
4259 * If the domain of "pma" is rational, then so is the constructed "map".
4261 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4263 int i;
4264 isl_map *map;
4266 if (!pma)
4267 return NULL;
4269 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4271 for (i = 0; i < pma->n; ++i) {
4272 isl_bool rational;
4273 isl_multi_aff *maff;
4274 isl_basic_map *bmap;
4275 isl_map *map_i;
4277 rational = isl_set_is_rational(pma->p[i].set);
4278 if (rational < 0)
4279 map = isl_map_free(map);
4280 maff = isl_multi_aff_copy(pma->p[i].maff);
4281 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4282 map_i = isl_map_from_basic_map(bmap);
4283 map_i = isl_map_intersect_domain(map_i,
4284 isl_set_copy(pma->p[i].set));
4285 map = isl_map_union_disjoint(map, map_i);
4288 isl_pw_multi_aff_free(pma);
4289 return map;
4292 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4294 if (!pma)
4295 return NULL;
4297 if (!isl_space_is_set(pma->dim))
4298 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4299 "isl_pw_multi_aff cannot be converted into an isl_set",
4300 goto error);
4302 return isl_map_from_pw_multi_aff(pma);
4303 error:
4304 isl_pw_multi_aff_free(pma);
4305 return NULL;
4308 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4309 * denominator "denom".
4310 * "denom" is allowed to be negative, in which case the actual denominator
4311 * is -denom and the expressions are added instead.
4313 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4314 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4316 int i, first;
4317 int sign;
4318 isl_int d;
4320 first = isl_seq_first_non_zero(c, n);
4321 if (first == -1)
4322 return aff;
4324 sign = isl_int_sgn(denom);
4325 isl_int_init(d);
4326 isl_int_abs(d, denom);
4327 for (i = first; i < n; ++i) {
4328 isl_aff *aff_i;
4330 if (isl_int_is_zero(c[i]))
4331 continue;
4332 aff_i = isl_multi_aff_get_aff(ma, i);
4333 aff_i = isl_aff_scale(aff_i, c[i]);
4334 aff_i = isl_aff_scale_down(aff_i, d);
4335 if (sign >= 0)
4336 aff = isl_aff_sub(aff, aff_i);
4337 else
4338 aff = isl_aff_add(aff, aff_i);
4340 isl_int_clear(d);
4342 return aff;
4345 /* Extract an affine expression that expresses the output dimension "pos"
4346 * of "bmap" in terms of the parameters and input dimensions from
4347 * equality "eq".
4348 * Note that this expression may involve integer divisions defined
4349 * in terms of parameters and input dimensions.
4350 * The equality may also involve references to earlier (but not later)
4351 * output dimensions. These are replaced by the corresponding elements
4352 * in "ma".
4354 * If the equality is of the form
4356 * f(i) + h(j) + a x + g(i) = 0,
4358 * with f(i) a linear combinations of the parameters and input dimensions,
4359 * g(i) a linear combination of integer divisions defined in terms of the same
4360 * and h(j) a linear combinations of earlier output dimensions,
4361 * then the affine expression is
4363 * (-f(i) - g(i))/a - h(j)/a
4365 * If the equality is of the form
4367 * f(i) + h(j) - a x + g(i) = 0,
4369 * then the affine expression is
4371 * (f(i) + g(i))/a - h(j)/(-a)
4374 * If "div" refers to an integer division (i.e., it is smaller than
4375 * the number of integer divisions), then the equality constraint
4376 * does involve an integer division (the one at position "div") that
4377 * is defined in terms of output dimensions. However, this integer
4378 * division can be eliminated by exploiting a pair of constraints
4379 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4380 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4381 * -l + x >= 0.
4382 * In particular, let
4384 * x = e(i) + m floor(...)
4386 * with e(i) the expression derived above and floor(...) the integer
4387 * division involving output dimensions.
4388 * From
4390 * l <= x <= l + n,
4392 * we have
4394 * 0 <= x - l <= n
4396 * This means
4398 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4399 * = (e(i) - l) mod m
4401 * Therefore,
4403 * x - l = (e(i) - l) mod m
4405 * or
4407 * x = ((e(i) - l) mod m) + l
4409 * The variable "shift" below contains the expression -l, which may
4410 * also involve a linear combination of earlier output dimensions.
4412 static __isl_give isl_aff *extract_aff_from_equality(
4413 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4414 __isl_keep isl_multi_aff *ma)
4416 unsigned o_out;
4417 unsigned n_div, n_out;
4418 isl_ctx *ctx;
4419 isl_local_space *ls;
4420 isl_aff *aff, *shift;
4421 isl_val *mod;
4423 ctx = isl_basic_map_get_ctx(bmap);
4424 ls = isl_basic_map_get_local_space(bmap);
4425 ls = isl_local_space_domain(ls);
4426 aff = isl_aff_alloc(isl_local_space_copy(ls));
4427 if (!aff)
4428 goto error;
4429 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4430 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4431 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4432 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4433 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4434 isl_seq_cpy(aff->v->el + 1 + o_out,
4435 bmap->eq[eq] + o_out + n_out, n_div);
4436 } else {
4437 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4438 isl_seq_neg(aff->v->el + 1 + o_out,
4439 bmap->eq[eq] + o_out + n_out, n_div);
4441 if (div < n_div)
4442 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4443 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4444 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4445 bmap->eq[eq][o_out + pos]);
4446 if (div < n_div) {
4447 shift = isl_aff_alloc(isl_local_space_copy(ls));
4448 if (!shift)
4449 goto error;
4450 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4451 isl_seq_cpy(shift->v->el + 1 + o_out,
4452 bmap->ineq[ineq] + o_out + n_out, n_div);
4453 isl_int_set_si(shift->v->el[0], 1);
4454 shift = subtract_initial(shift, ma, pos,
4455 bmap->ineq[ineq] + o_out, ctx->negone);
4456 aff = isl_aff_add(aff, isl_aff_copy(shift));
4457 mod = isl_val_int_from_isl_int(ctx,
4458 bmap->eq[eq][o_out + n_out + div]);
4459 mod = isl_val_abs(mod);
4460 aff = isl_aff_mod_val(aff, mod);
4461 aff = isl_aff_sub(aff, shift);
4464 isl_local_space_free(ls);
4465 return aff;
4466 error:
4467 isl_local_space_free(ls);
4468 isl_aff_free(aff);
4469 return NULL;
4472 /* Given a basic map with output dimensions defined
4473 * in terms of the parameters input dimensions and earlier
4474 * output dimensions using an equality (and possibly a pair on inequalities),
4475 * extract an isl_aff that expresses output dimension "pos" in terms
4476 * of the parameters and input dimensions.
4477 * Note that this expression may involve integer divisions defined
4478 * in terms of parameters and input dimensions.
4479 * "ma" contains the expressions corresponding to earlier output dimensions.
4481 * This function shares some similarities with
4482 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4484 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4485 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4487 int eq, div, ineq;
4488 isl_aff *aff;
4490 if (!bmap)
4491 return NULL;
4492 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4493 if (eq >= bmap->n_eq)
4494 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4495 "unable to find suitable equality", return NULL);
4496 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4498 aff = isl_aff_remove_unused_divs(aff);
4499 return aff;
4502 /* Given a basic map where each output dimension is defined
4503 * in terms of the parameters and input dimensions using an equality,
4504 * extract an isl_multi_aff that expresses the output dimensions in terms
4505 * of the parameters and input dimensions.
4507 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4508 __isl_take isl_basic_map *bmap)
4510 int i;
4511 unsigned n_out;
4512 isl_multi_aff *ma;
4514 if (!bmap)
4515 return NULL;
4517 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4518 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4520 for (i = 0; i < n_out; ++i) {
4521 isl_aff *aff;
4523 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4524 ma = isl_multi_aff_set_aff(ma, i, aff);
4527 isl_basic_map_free(bmap);
4529 return ma;
4532 /* Given a basic set where each set dimension is defined
4533 * in terms of the parameters using an equality,
4534 * extract an isl_multi_aff that expresses the set dimensions in terms
4535 * of the parameters.
4537 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4538 __isl_take isl_basic_set *bset)
4540 return extract_isl_multi_aff_from_basic_map(bset);
4543 /* Create an isl_pw_multi_aff that is equivalent to
4544 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4545 * The given basic map is such that each output dimension is defined
4546 * in terms of the parameters and input dimensions using an equality.
4548 * Since some applications expect the result of isl_pw_multi_aff_from_map
4549 * to only contain integer affine expressions, we compute the floor
4550 * of the expression before returning.
4552 * Remove all constraints involving local variables without
4553 * an explicit representation (resulting in the removal of those
4554 * local variables) prior to the actual extraction to ensure
4555 * that the local spaces in which the resulting affine expressions
4556 * are created do not contain any unknown local variables.
4557 * Removing such constraints is safe because constraints involving
4558 * unknown local variables are not used to determine whether
4559 * a basic map is obviously single-valued.
4561 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4562 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4564 isl_multi_aff *ma;
4566 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4567 ma = extract_isl_multi_aff_from_basic_map(bmap);
4568 ma = isl_multi_aff_floor(ma);
4569 return isl_pw_multi_aff_alloc(domain, ma);
4572 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4573 * This obviously only works if the input "map" is single-valued.
4574 * If so, we compute the lexicographic minimum of the image in the form
4575 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4576 * to its lexicographic minimum.
4577 * If the input is not single-valued, we produce an error.
4579 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4580 __isl_take isl_map *map)
4582 int i;
4583 int sv;
4584 isl_pw_multi_aff *pma;
4586 sv = isl_map_is_single_valued(map);
4587 if (sv < 0)
4588 goto error;
4589 if (!sv)
4590 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4591 "map is not single-valued", goto error);
4592 map = isl_map_make_disjoint(map);
4593 if (!map)
4594 return NULL;
4596 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4598 for (i = 0; i < map->n; ++i) {
4599 isl_pw_multi_aff *pma_i;
4600 isl_basic_map *bmap;
4601 bmap = isl_basic_map_copy(map->p[i]);
4602 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4603 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4606 isl_map_free(map);
4607 return pma;
4608 error:
4609 isl_map_free(map);
4610 return NULL;
4613 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4614 * taking into account that the output dimension at position "d"
4615 * can be represented as
4617 * x = floor((e(...) + c1) / m)
4619 * given that constraint "i" is of the form
4621 * e(...) + c1 - m x >= 0
4624 * Let "map" be of the form
4626 * A -> B
4628 * We construct a mapping
4630 * A -> [A -> x = floor(...)]
4632 * apply that to the map, obtaining
4634 * [A -> x = floor(...)] -> B
4636 * and equate dimension "d" to x.
4637 * We then compute a isl_pw_multi_aff representation of the resulting map
4638 * and plug in the mapping above.
4640 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4641 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4643 isl_ctx *ctx;
4644 isl_space *space;
4645 isl_local_space *ls;
4646 isl_multi_aff *ma;
4647 isl_aff *aff;
4648 isl_vec *v;
4649 isl_map *insert;
4650 int offset;
4651 int n;
4652 int n_in;
4653 isl_pw_multi_aff *pma;
4654 int is_set;
4656 is_set = isl_map_is_set(map);
4658 offset = isl_basic_map_offset(hull, isl_dim_out);
4659 ctx = isl_map_get_ctx(map);
4660 space = isl_space_domain(isl_map_get_space(map));
4661 n_in = isl_space_dim(space, isl_dim_set);
4662 n = isl_space_dim(space, isl_dim_all);
4664 v = isl_vec_alloc(ctx, 1 + 1 + n);
4665 if (v) {
4666 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4667 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4669 isl_basic_map_free(hull);
4671 ls = isl_local_space_from_space(isl_space_copy(space));
4672 aff = isl_aff_alloc_vec(ls, v);
4673 aff = isl_aff_floor(aff);
4674 if (is_set) {
4675 isl_space_free(space);
4676 ma = isl_multi_aff_from_aff(aff);
4677 } else {
4678 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4679 ma = isl_multi_aff_range_product(ma,
4680 isl_multi_aff_from_aff(aff));
4683 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4684 map = isl_map_apply_domain(map, insert);
4685 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4686 pma = isl_pw_multi_aff_from_map(map);
4687 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4689 return pma;
4692 /* Is constraint "c" of the form
4694 * e(...) + c1 - m x >= 0
4696 * or
4698 * -e(...) + c2 + m x >= 0
4700 * where m > 1 and e only depends on parameters and input dimemnsions?
4702 * "offset" is the offset of the output dimensions
4703 * "pos" is the position of output dimension x.
4705 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4707 if (isl_int_is_zero(c[offset + d]))
4708 return 0;
4709 if (isl_int_is_one(c[offset + d]))
4710 return 0;
4711 if (isl_int_is_negone(c[offset + d]))
4712 return 0;
4713 if (isl_seq_first_non_zero(c + offset, d) != -1)
4714 return 0;
4715 if (isl_seq_first_non_zero(c + offset + d + 1,
4716 total - (offset + d + 1)) != -1)
4717 return 0;
4718 return 1;
4721 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4723 * As a special case, we first check if there is any pair of constraints,
4724 * shared by all the basic maps in "map" that force a given dimension
4725 * to be equal to the floor of some affine combination of the input dimensions.
4727 * In particular, if we can find two constraints
4729 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4731 * and
4733 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4735 * where m > 1 and e only depends on parameters and input dimemnsions,
4736 * and such that
4738 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4740 * then we know that we can take
4742 * x = floor((e(...) + c1) / m)
4744 * without having to perform any computation.
4746 * Note that we know that
4748 * c1 + c2 >= 1
4750 * If c1 + c2 were 0, then we would have detected an equality during
4751 * simplification. If c1 + c2 were negative, then we would have detected
4752 * a contradiction.
4754 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4755 __isl_take isl_map *map)
4757 int d, dim;
4758 int i, j, n;
4759 int offset, total;
4760 isl_int sum;
4761 isl_basic_map *hull;
4763 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4764 if (!hull)
4765 goto error;
4767 isl_int_init(sum);
4768 dim = isl_map_dim(map, isl_dim_out);
4769 offset = isl_basic_map_offset(hull, isl_dim_out);
4770 total = 1 + isl_basic_map_total_dim(hull);
4771 n = hull->n_ineq;
4772 for (d = 0; d < dim; ++d) {
4773 for (i = 0; i < n; ++i) {
4774 if (!is_potential_div_constraint(hull->ineq[i],
4775 offset, d, total))
4776 continue;
4777 for (j = i + 1; j < n; ++j) {
4778 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4779 hull->ineq[j] + 1, total - 1))
4780 continue;
4781 isl_int_add(sum, hull->ineq[i][0],
4782 hull->ineq[j][0]);
4783 if (isl_int_abs_lt(sum,
4784 hull->ineq[i][offset + d]))
4785 break;
4788 if (j >= n)
4789 continue;
4790 isl_int_clear(sum);
4791 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4792 j = i;
4793 return pw_multi_aff_from_map_div(map, hull, d, j);
4796 isl_int_clear(sum);
4797 isl_basic_map_free(hull);
4798 return pw_multi_aff_from_map_base(map);
4799 error:
4800 isl_map_free(map);
4801 isl_basic_map_free(hull);
4802 return NULL;
4805 /* Given an affine expression
4807 * [A -> B] -> f(A,B)
4809 * construct an isl_multi_aff
4811 * [A -> B] -> B'
4813 * such that dimension "d" in B' is set to "aff" and the remaining
4814 * dimensions are set equal to the corresponding dimensions in B.
4815 * "n_in" is the dimension of the space A.
4816 * "n_out" is the dimension of the space B.
4818 * If "is_set" is set, then the affine expression is of the form
4820 * [B] -> f(B)
4822 * and we construct an isl_multi_aff
4824 * B -> B'
4826 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4827 unsigned n_in, unsigned n_out, int is_set)
4829 int i;
4830 isl_multi_aff *ma;
4831 isl_space *space, *space2;
4832 isl_local_space *ls;
4834 space = isl_aff_get_domain_space(aff);
4835 ls = isl_local_space_from_space(isl_space_copy(space));
4836 space2 = isl_space_copy(space);
4837 if (!is_set)
4838 space2 = isl_space_range(isl_space_unwrap(space2));
4839 space = isl_space_map_from_domain_and_range(space, space2);
4840 ma = isl_multi_aff_alloc(space);
4841 ma = isl_multi_aff_set_aff(ma, d, aff);
4843 for (i = 0; i < n_out; ++i) {
4844 if (i == d)
4845 continue;
4846 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4847 isl_dim_set, n_in + i);
4848 ma = isl_multi_aff_set_aff(ma, i, aff);
4851 isl_local_space_free(ls);
4853 return ma;
4856 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4857 * taking into account that the dimension at position "d" can be written as
4859 * x = m a + f(..) (1)
4861 * where m is equal to "gcd".
4862 * "i" is the index of the equality in "hull" that defines f(..).
4863 * In particular, the equality is of the form
4865 * f(..) - x + m g(existentials) = 0
4867 * or
4869 * -f(..) + x + m g(existentials) = 0
4871 * We basically plug (1) into "map", resulting in a map with "a"
4872 * in the range instead of "x". The corresponding isl_pw_multi_aff
4873 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4875 * Specifically, given the input map
4877 * A -> B
4879 * We first wrap it into a set
4881 * [A -> B]
4883 * and define (1) on top of the corresponding space, resulting in "aff".
4884 * We use this to create an isl_multi_aff that maps the output position "d"
4885 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4886 * We plug this into the wrapped map, unwrap the result and compute the
4887 * corresponding isl_pw_multi_aff.
4888 * The result is an expression
4890 * A -> T(A)
4892 * We adjust that to
4894 * A -> [A -> T(A)]
4896 * so that we can plug that into "aff", after extending the latter to
4897 * a mapping
4899 * [A -> B] -> B'
4902 * If "map" is actually a set, then there is no "A" space, meaning
4903 * that we do not need to perform any wrapping, and that the result
4904 * of the recursive call is of the form
4906 * [T]
4908 * which is plugged into a mapping of the form
4910 * B -> B'
4912 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4913 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4914 isl_int gcd)
4916 isl_set *set;
4917 isl_space *space;
4918 isl_local_space *ls;
4919 isl_aff *aff;
4920 isl_multi_aff *ma;
4921 isl_pw_multi_aff *pma, *id;
4922 unsigned n_in;
4923 unsigned o_out;
4924 unsigned n_out;
4925 int is_set;
4927 is_set = isl_map_is_set(map);
4929 n_in = isl_basic_map_dim(hull, isl_dim_in);
4930 n_out = isl_basic_map_dim(hull, isl_dim_out);
4931 o_out = isl_basic_map_offset(hull, isl_dim_out);
4933 if (is_set)
4934 set = map;
4935 else
4936 set = isl_map_wrap(map);
4937 space = isl_space_map_from_set(isl_set_get_space(set));
4938 ma = isl_multi_aff_identity(space);
4939 ls = isl_local_space_from_space(isl_set_get_space(set));
4940 aff = isl_aff_alloc(ls);
4941 if (aff) {
4942 isl_int_set_si(aff->v->el[0], 1);
4943 if (isl_int_is_one(hull->eq[i][o_out + d]))
4944 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4945 aff->v->size - 1);
4946 else
4947 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4948 aff->v->size - 1);
4949 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4951 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4952 set = isl_set_preimage_multi_aff(set, ma);
4954 ma = range_map(aff, d, n_in, n_out, is_set);
4956 if (is_set)
4957 map = set;
4958 else
4959 map = isl_set_unwrap(set);
4960 pma = isl_pw_multi_aff_from_map(map);
4962 if (!is_set) {
4963 space = isl_pw_multi_aff_get_domain_space(pma);
4964 space = isl_space_map_from_set(space);
4965 id = isl_pw_multi_aff_identity(space);
4966 pma = isl_pw_multi_aff_range_product(id, pma);
4968 id = isl_pw_multi_aff_from_multi_aff(ma);
4969 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4971 isl_basic_map_free(hull);
4972 return pma;
4975 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4976 * "hull" contains the equalities valid for "map".
4978 * Check if any of the output dimensions is "strided".
4979 * That is, we check if it can be written as
4981 * x = m a + f(..)
4983 * with m greater than 1, a some combination of existentially quantified
4984 * variables and f an expression in the parameters and input dimensions.
4985 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4987 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4988 * special case.
4990 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
4991 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
4993 int i, j;
4994 unsigned n_out;
4995 unsigned o_out;
4996 unsigned n_div;
4997 unsigned o_div;
4998 isl_int gcd;
5000 n_div = isl_basic_map_dim(hull, isl_dim_div);
5001 o_div = isl_basic_map_offset(hull, isl_dim_div);
5003 if (n_div == 0) {
5004 isl_basic_map_free(hull);
5005 return pw_multi_aff_from_map_check_div(map);
5008 isl_int_init(gcd);
5010 n_out = isl_basic_map_dim(hull, isl_dim_out);
5011 o_out = isl_basic_map_offset(hull, isl_dim_out);
5013 for (i = 0; i < n_out; ++i) {
5014 for (j = 0; j < hull->n_eq; ++j) {
5015 isl_int *eq = hull->eq[j];
5016 isl_pw_multi_aff *res;
5018 if (!isl_int_is_one(eq[o_out + i]) &&
5019 !isl_int_is_negone(eq[o_out + i]))
5020 continue;
5021 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5022 continue;
5023 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5024 n_out - (i + 1)) != -1)
5025 continue;
5026 isl_seq_gcd(eq + o_div, n_div, &gcd);
5027 if (isl_int_is_zero(gcd))
5028 continue;
5029 if (isl_int_is_one(gcd))
5030 continue;
5032 res = pw_multi_aff_from_map_stride(map, hull,
5033 i, j, gcd);
5034 isl_int_clear(gcd);
5035 return res;
5039 isl_int_clear(gcd);
5040 isl_basic_map_free(hull);
5041 return pw_multi_aff_from_map_check_div(map);
5044 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5046 * As a special case, we first check if all output dimensions are uniquely
5047 * defined in terms of the parameters and input dimensions over the entire
5048 * domain. If so, we extract the desired isl_pw_multi_aff directly
5049 * from the affine hull of "map" and its domain.
5051 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5052 * special cases.
5054 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5056 isl_bool sv;
5057 isl_basic_map *hull;
5059 if (!map)
5060 return NULL;
5062 if (isl_map_n_basic_map(map) == 1) {
5063 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5064 hull = isl_basic_map_plain_affine_hull(hull);
5065 sv = isl_basic_map_plain_is_single_valued(hull);
5066 if (sv >= 0 && sv)
5067 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5068 hull);
5069 isl_basic_map_free(hull);
5071 map = isl_map_detect_equalities(map);
5072 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5073 sv = isl_basic_map_plain_is_single_valued(hull);
5074 if (sv >= 0 && sv)
5075 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5076 if (sv >= 0)
5077 return pw_multi_aff_from_map_check_strides(map, hull);
5078 isl_basic_map_free(hull);
5079 isl_map_free(map);
5080 return NULL;
5083 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5085 return isl_pw_multi_aff_from_map(set);
5088 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5089 * add it to *user.
5091 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5093 isl_union_pw_multi_aff **upma = user;
5094 isl_pw_multi_aff *pma;
5096 pma = isl_pw_multi_aff_from_map(map);
5097 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5099 return *upma ? isl_stat_ok : isl_stat_error;
5102 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5103 * domain.
5105 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5106 __isl_take isl_aff *aff)
5108 isl_multi_aff *ma;
5109 isl_pw_multi_aff *pma;
5111 ma = isl_multi_aff_from_aff(aff);
5112 pma = isl_pw_multi_aff_from_multi_aff(ma);
5113 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5116 /* Try and create an isl_union_pw_multi_aff that is equivalent
5117 * to the given isl_union_map.
5118 * The isl_union_map is required to be single-valued in each space.
5119 * Otherwise, an error is produced.
5121 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5122 __isl_take isl_union_map *umap)
5124 isl_space *space;
5125 isl_union_pw_multi_aff *upma;
5127 space = isl_union_map_get_space(umap);
5128 upma = isl_union_pw_multi_aff_empty(space);
5129 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5130 upma = isl_union_pw_multi_aff_free(upma);
5131 isl_union_map_free(umap);
5133 return upma;
5136 /* Try and create an isl_union_pw_multi_aff that is equivalent
5137 * to the given isl_union_set.
5138 * The isl_union_set is required to be a singleton in each space.
5139 * Otherwise, an error is produced.
5141 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5142 __isl_take isl_union_set *uset)
5144 return isl_union_pw_multi_aff_from_union_map(uset);
5147 /* Return the piecewise affine expression "set ? 1 : 0".
5149 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5151 isl_pw_aff *pa;
5152 isl_space *space = isl_set_get_space(set);
5153 isl_local_space *ls = isl_local_space_from_space(space);
5154 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5155 isl_aff *one = isl_aff_zero_on_domain(ls);
5157 one = isl_aff_add_constant_si(one, 1);
5158 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5159 set = isl_set_complement(set);
5160 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5162 return pa;
5165 /* Plug in "subs" for dimension "type", "pos" of "aff".
5167 * Let i be the dimension to replace and let "subs" be of the form
5169 * f/d
5171 * and "aff" of the form
5173 * (a i + g)/m
5175 * The result is
5177 * (a f + d g')/(m d)
5179 * where g' is the result of plugging in "subs" in each of the integer
5180 * divisions in g.
5182 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5183 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5185 isl_ctx *ctx;
5186 isl_int v;
5188 aff = isl_aff_cow(aff);
5189 if (!aff || !subs)
5190 return isl_aff_free(aff);
5192 ctx = isl_aff_get_ctx(aff);
5193 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5194 isl_die(ctx, isl_error_invalid,
5195 "spaces don't match", return isl_aff_free(aff));
5196 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5197 isl_die(ctx, isl_error_unsupported,
5198 "cannot handle divs yet", return isl_aff_free(aff));
5200 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5201 if (!aff->ls)
5202 return isl_aff_free(aff);
5204 aff->v = isl_vec_cow(aff->v);
5205 if (!aff->v)
5206 return isl_aff_free(aff);
5208 pos += isl_local_space_offset(aff->ls, type);
5210 isl_int_init(v);
5211 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5212 aff->v->size, subs->v->size, v);
5213 isl_int_clear(v);
5215 return aff;
5218 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5219 * expressions in "maff".
5221 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5222 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5223 __isl_keep isl_aff *subs)
5225 int i;
5227 maff = isl_multi_aff_cow(maff);
5228 if (!maff || !subs)
5229 return isl_multi_aff_free(maff);
5231 if (type == isl_dim_in)
5232 type = isl_dim_set;
5234 for (i = 0; i < maff->n; ++i) {
5235 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5236 if (!maff->p[i])
5237 return isl_multi_aff_free(maff);
5240 return maff;
5243 /* Plug in "subs" for dimension "type", "pos" of "pma".
5245 * pma is of the form
5247 * A_i(v) -> M_i(v)
5249 * while subs is of the form
5251 * v' = B_j(v) -> S_j
5253 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5254 * has a contribution in the result, in particular
5256 * C_ij(S_j) -> M_i(S_j)
5258 * Note that plugging in S_j in C_ij may also result in an empty set
5259 * and this contribution should simply be discarded.
5261 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5262 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5263 __isl_keep isl_pw_aff *subs)
5265 int i, j, n;
5266 isl_pw_multi_aff *res;
5268 if (!pma || !subs)
5269 return isl_pw_multi_aff_free(pma);
5271 n = pma->n * subs->n;
5272 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5274 for (i = 0; i < pma->n; ++i) {
5275 for (j = 0; j < subs->n; ++j) {
5276 isl_set *common;
5277 isl_multi_aff *res_ij;
5278 int empty;
5280 common = isl_set_intersect(
5281 isl_set_copy(pma->p[i].set),
5282 isl_set_copy(subs->p[j].set));
5283 common = isl_set_substitute(common,
5284 type, pos, subs->p[j].aff);
5285 empty = isl_set_plain_is_empty(common);
5286 if (empty < 0 || empty) {
5287 isl_set_free(common);
5288 if (empty < 0)
5289 goto error;
5290 continue;
5293 res_ij = isl_multi_aff_substitute(
5294 isl_multi_aff_copy(pma->p[i].maff),
5295 type, pos, subs->p[j].aff);
5297 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5301 isl_pw_multi_aff_free(pma);
5302 return res;
5303 error:
5304 isl_pw_multi_aff_free(pma);
5305 isl_pw_multi_aff_free(res);
5306 return NULL;
5309 /* Compute the preimage of a range of dimensions in the affine expression "src"
5310 * under "ma" and put the result in "dst". The number of dimensions in "src"
5311 * that precede the range is given by "n_before". The number of dimensions
5312 * in the range is given by the number of output dimensions of "ma".
5313 * The number of dimensions that follow the range is given by "n_after".
5314 * If "has_denom" is set (to one),
5315 * then "src" and "dst" have an extra initial denominator.
5316 * "n_div_ma" is the number of existentials in "ma"
5317 * "n_div_bset" is the number of existentials in "src"
5318 * The resulting "dst" (which is assumed to have been allocated by
5319 * the caller) contains coefficients for both sets of existentials,
5320 * first those in "ma" and then those in "src".
5321 * f, c1, c2 and g are temporary objects that have been initialized
5322 * by the caller.
5324 * Let src represent the expression
5326 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5328 * and let ma represent the expressions
5330 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5332 * We start out with the following expression for dst:
5334 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5336 * with the multiplication factor f initially equal to 1
5337 * and f \sum_i b_i v_i kept separately.
5338 * For each x_i that we substitute, we multiply the numerator
5339 * (and denominator) of dst by c_1 = m_i and add the numerator
5340 * of the x_i expression multiplied by c_2 = f b_i,
5341 * after removing the common factors of c_1 and c_2.
5342 * The multiplication factor f also needs to be multiplied by c_1
5343 * for the next x_j, j > i.
5345 void isl_seq_preimage(isl_int *dst, isl_int *src,
5346 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5347 int n_div_ma, int n_div_bmap,
5348 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5350 int i;
5351 int n_param, n_in, n_out;
5352 int o_dst, o_src;
5354 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5355 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5356 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5358 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5359 o_dst = o_src = has_denom + 1 + n_param + n_before;
5360 isl_seq_clr(dst + o_dst, n_in);
5361 o_dst += n_in;
5362 o_src += n_out;
5363 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5364 o_dst += n_after;
5365 o_src += n_after;
5366 isl_seq_clr(dst + o_dst, n_div_ma);
5367 o_dst += n_div_ma;
5368 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5370 isl_int_set_si(f, 1);
5372 for (i = 0; i < n_out; ++i) {
5373 int offset = has_denom + 1 + n_param + n_before + i;
5375 if (isl_int_is_zero(src[offset]))
5376 continue;
5377 isl_int_set(c1, ma->p[i]->v->el[0]);
5378 isl_int_mul(c2, f, src[offset]);
5379 isl_int_gcd(g, c1, c2);
5380 isl_int_divexact(c1, c1, g);
5381 isl_int_divexact(c2, c2, g);
5383 isl_int_mul(f, f, c1);
5384 o_dst = has_denom;
5385 o_src = 1;
5386 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5387 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5388 o_dst += 1 + n_param;
5389 o_src += 1 + n_param;
5390 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5391 o_dst += n_before;
5392 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5393 c2, ma->p[i]->v->el + o_src, n_in);
5394 o_dst += n_in;
5395 o_src += n_in;
5396 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5397 o_dst += n_after;
5398 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5399 c2, ma->p[i]->v->el + o_src, n_div_ma);
5400 o_dst += n_div_ma;
5401 o_src += n_div_ma;
5402 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5403 if (has_denom)
5404 isl_int_mul(dst[0], dst[0], c1);
5408 /* Compute the pullback of "aff" by the function represented by "ma".
5409 * In other words, plug in "ma" in "aff". The result is an affine expression
5410 * defined over the domain space of "ma".
5412 * If "aff" is represented by
5414 * (a(p) + b x + c(divs))/d
5416 * and ma is represented by
5418 * x = D(p) + F(y) + G(divs')
5420 * then the result is
5422 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5424 * The divs in the local space of the input are similarly adjusted
5425 * through a call to isl_local_space_preimage_multi_aff.
5427 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5428 __isl_take isl_multi_aff *ma)
5430 isl_aff *res = NULL;
5431 isl_local_space *ls;
5432 int n_div_aff, n_div_ma;
5433 isl_int f, c1, c2, g;
5435 ma = isl_multi_aff_align_divs(ma);
5436 if (!aff || !ma)
5437 goto error;
5439 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5440 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5442 ls = isl_aff_get_domain_local_space(aff);
5443 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5444 res = isl_aff_alloc(ls);
5445 if (!res)
5446 goto error;
5448 isl_int_init(f);
5449 isl_int_init(c1);
5450 isl_int_init(c2);
5451 isl_int_init(g);
5453 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5454 f, c1, c2, g, 1);
5456 isl_int_clear(f);
5457 isl_int_clear(c1);
5458 isl_int_clear(c2);
5459 isl_int_clear(g);
5461 isl_aff_free(aff);
5462 isl_multi_aff_free(ma);
5463 res = isl_aff_normalize(res);
5464 return res;
5465 error:
5466 isl_aff_free(aff);
5467 isl_multi_aff_free(ma);
5468 isl_aff_free(res);
5469 return NULL;
5472 /* Compute the pullback of "aff1" by the function represented by "aff2".
5473 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5474 * defined over the domain space of "aff1".
5476 * The domain of "aff1" should match the range of "aff2", which means
5477 * that it should be single-dimensional.
5479 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5480 __isl_take isl_aff *aff2)
5482 isl_multi_aff *ma;
5484 ma = isl_multi_aff_from_aff(aff2);
5485 return isl_aff_pullback_multi_aff(aff1, ma);
5488 /* Compute the pullback of "ma1" by the function represented by "ma2".
5489 * In other words, plug in "ma2" in "ma1".
5491 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5493 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5494 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5496 int i;
5497 isl_space *space = NULL;
5499 ma2 = isl_multi_aff_align_divs(ma2);
5500 ma1 = isl_multi_aff_cow(ma1);
5501 if (!ma1 || !ma2)
5502 goto error;
5504 space = isl_space_join(isl_multi_aff_get_space(ma2),
5505 isl_multi_aff_get_space(ma1));
5507 for (i = 0; i < ma1->n; ++i) {
5508 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5509 isl_multi_aff_copy(ma2));
5510 if (!ma1->p[i])
5511 goto error;
5514 ma1 = isl_multi_aff_reset_space(ma1, space);
5515 isl_multi_aff_free(ma2);
5516 return ma1;
5517 error:
5518 isl_space_free(space);
5519 isl_multi_aff_free(ma2);
5520 isl_multi_aff_free(ma1);
5521 return NULL;
5524 /* Compute the pullback of "ma1" by the function represented by "ma2".
5525 * In other words, plug in "ma2" in "ma1".
5527 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5528 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5530 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5531 &isl_multi_aff_pullback_multi_aff_aligned);
5534 /* Extend the local space of "dst" to include the divs
5535 * in the local space of "src".
5537 * If "src" does not have any divs or if the local spaces of "dst" and
5538 * "src" are the same, then no extension is required.
5540 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5541 __isl_keep isl_aff *src)
5543 isl_ctx *ctx;
5544 int src_n_div, dst_n_div;
5545 int *exp1 = NULL;
5546 int *exp2 = NULL;
5547 isl_bool equal;
5548 isl_mat *div;
5550 if (!src || !dst)
5551 return isl_aff_free(dst);
5553 ctx = isl_aff_get_ctx(src);
5554 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5555 if (equal < 0)
5556 return isl_aff_free(dst);
5557 if (!equal)
5558 isl_die(ctx, isl_error_invalid,
5559 "spaces don't match", goto error);
5561 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5562 if (src_n_div == 0)
5563 return dst;
5564 equal = isl_local_space_is_equal(src->ls, dst->ls);
5565 if (equal < 0)
5566 return isl_aff_free(dst);
5567 if (equal)
5568 return dst;
5570 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5571 exp1 = isl_alloc_array(ctx, int, src_n_div);
5572 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5573 if (!exp1 || (dst_n_div && !exp2))
5574 goto error;
5576 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5577 dst = isl_aff_expand_divs(dst, div, exp2);
5578 free(exp1);
5579 free(exp2);
5581 return dst;
5582 error:
5583 free(exp1);
5584 free(exp2);
5585 return isl_aff_free(dst);
5588 /* Adjust the local spaces of the affine expressions in "maff"
5589 * such that they all have the save divs.
5591 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5592 __isl_take isl_multi_aff *maff)
5594 int i;
5596 if (!maff)
5597 return NULL;
5598 if (maff->n == 0)
5599 return maff;
5600 maff = isl_multi_aff_cow(maff);
5601 if (!maff)
5602 return NULL;
5604 for (i = 1; i < maff->n; ++i)
5605 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5606 for (i = 1; i < maff->n; ++i) {
5607 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5608 if (!maff->p[i])
5609 return isl_multi_aff_free(maff);
5612 return maff;
5615 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5617 aff = isl_aff_cow(aff);
5618 if (!aff)
5619 return NULL;
5621 aff->ls = isl_local_space_lift(aff->ls);
5622 if (!aff->ls)
5623 return isl_aff_free(aff);
5625 return aff;
5628 /* Lift "maff" to a space with extra dimensions such that the result
5629 * has no more existentially quantified variables.
5630 * If "ls" is not NULL, then *ls is assigned the local space that lies
5631 * at the basis of the lifting applied to "maff".
5633 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5634 __isl_give isl_local_space **ls)
5636 int i;
5637 isl_space *space;
5638 unsigned n_div;
5640 if (ls)
5641 *ls = NULL;
5643 if (!maff)
5644 return NULL;
5646 if (maff->n == 0) {
5647 if (ls) {
5648 isl_space *space = isl_multi_aff_get_domain_space(maff);
5649 *ls = isl_local_space_from_space(space);
5650 if (!*ls)
5651 return isl_multi_aff_free(maff);
5653 return maff;
5656 maff = isl_multi_aff_cow(maff);
5657 maff = isl_multi_aff_align_divs(maff);
5658 if (!maff)
5659 return NULL;
5661 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5662 space = isl_multi_aff_get_space(maff);
5663 space = isl_space_lift(isl_space_domain(space), n_div);
5664 space = isl_space_extend_domain_with_range(space,
5665 isl_multi_aff_get_space(maff));
5666 if (!space)
5667 return isl_multi_aff_free(maff);
5668 isl_space_free(maff->space);
5669 maff->space = space;
5671 if (ls) {
5672 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5673 if (!*ls)
5674 return isl_multi_aff_free(maff);
5677 for (i = 0; i < maff->n; ++i) {
5678 maff->p[i] = isl_aff_lift(maff->p[i]);
5679 if (!maff->p[i])
5680 goto error;
5683 return maff;
5684 error:
5685 if (ls)
5686 isl_local_space_free(*ls);
5687 return isl_multi_aff_free(maff);
5691 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5693 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5694 __isl_keep isl_pw_multi_aff *pma, int pos)
5696 int i;
5697 int n_out;
5698 isl_space *space;
5699 isl_pw_aff *pa;
5701 if (!pma)
5702 return NULL;
5704 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5705 if (pos < 0 || pos >= n_out)
5706 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5707 "index out of bounds", return NULL);
5709 space = isl_pw_multi_aff_get_space(pma);
5710 space = isl_space_drop_dims(space, isl_dim_out,
5711 pos + 1, n_out - pos - 1);
5712 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5714 pa = isl_pw_aff_alloc_size(space, pma->n);
5715 for (i = 0; i < pma->n; ++i) {
5716 isl_aff *aff;
5717 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5718 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5721 return pa;
5724 /* Return an isl_pw_multi_aff with the given "set" as domain and
5725 * an unnamed zero-dimensional range.
5727 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5728 __isl_take isl_set *set)
5730 isl_multi_aff *ma;
5731 isl_space *space;
5733 space = isl_set_get_space(set);
5734 space = isl_space_from_domain(space);
5735 ma = isl_multi_aff_zero(space);
5736 return isl_pw_multi_aff_alloc(set, ma);
5739 /* Add an isl_pw_multi_aff with the given "set" as domain and
5740 * an unnamed zero-dimensional range to *user.
5742 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5743 void *user)
5745 isl_union_pw_multi_aff **upma = user;
5746 isl_pw_multi_aff *pma;
5748 pma = isl_pw_multi_aff_from_domain(set);
5749 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5751 return isl_stat_ok;
5754 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5755 * an unnamed zero-dimensional range.
5757 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5758 __isl_take isl_union_set *uset)
5760 isl_space *space;
5761 isl_union_pw_multi_aff *upma;
5763 if (!uset)
5764 return NULL;
5766 space = isl_union_set_get_space(uset);
5767 upma = isl_union_pw_multi_aff_empty(space);
5769 if (isl_union_set_foreach_set(uset,
5770 &add_pw_multi_aff_from_domain, &upma) < 0)
5771 goto error;
5773 isl_union_set_free(uset);
5774 return upma;
5775 error:
5776 isl_union_set_free(uset);
5777 isl_union_pw_multi_aff_free(upma);
5778 return NULL;
5781 /* Convert "pma" to an isl_map and add it to *umap.
5783 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5784 void *user)
5786 isl_union_map **umap = user;
5787 isl_map *map;
5789 map = isl_map_from_pw_multi_aff(pma);
5790 *umap = isl_union_map_add_map(*umap, map);
5792 return isl_stat_ok;
5795 /* Construct a union map mapping the domain of the union
5796 * piecewise multi-affine expression to its range, with each dimension
5797 * in the range equated to the corresponding affine expression on its cell.
5799 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5800 __isl_take isl_union_pw_multi_aff *upma)
5802 isl_space *space;
5803 isl_union_map *umap;
5805 if (!upma)
5806 return NULL;
5808 space = isl_union_pw_multi_aff_get_space(upma);
5809 umap = isl_union_map_empty(space);
5811 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5812 &map_from_pw_multi_aff, &umap) < 0)
5813 goto error;
5815 isl_union_pw_multi_aff_free(upma);
5816 return umap;
5817 error:
5818 isl_union_pw_multi_aff_free(upma);
5819 isl_union_map_free(umap);
5820 return NULL;
5823 /* Local data for bin_entry and the callback "fn".
5825 struct isl_union_pw_multi_aff_bin_data {
5826 isl_union_pw_multi_aff *upma2;
5827 isl_union_pw_multi_aff *res;
5828 isl_pw_multi_aff *pma;
5829 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5832 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5833 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5835 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5837 struct isl_union_pw_multi_aff_bin_data *data = user;
5838 isl_stat r;
5840 data->pma = pma;
5841 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5842 data->fn, data);
5843 isl_pw_multi_aff_free(pma);
5845 return r;
5848 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5849 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5850 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5851 * as *entry. The callback should adjust data->res if desired.
5853 static __isl_give isl_union_pw_multi_aff *bin_op(
5854 __isl_take isl_union_pw_multi_aff *upma1,
5855 __isl_take isl_union_pw_multi_aff *upma2,
5856 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5858 isl_space *space;
5859 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5861 space = isl_union_pw_multi_aff_get_space(upma2);
5862 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5863 space = isl_union_pw_multi_aff_get_space(upma1);
5864 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5866 if (!upma1 || !upma2)
5867 goto error;
5869 data.upma2 = upma2;
5870 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5871 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5872 &bin_entry, &data) < 0)
5873 goto error;
5875 isl_union_pw_multi_aff_free(upma1);
5876 isl_union_pw_multi_aff_free(upma2);
5877 return data.res;
5878 error:
5879 isl_union_pw_multi_aff_free(upma1);
5880 isl_union_pw_multi_aff_free(upma2);
5881 isl_union_pw_multi_aff_free(data.res);
5882 return NULL;
5885 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5886 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5888 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5889 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5891 isl_space *space;
5893 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5894 isl_pw_multi_aff_get_space(pma2));
5895 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5896 &isl_multi_aff_range_product);
5899 /* Given two isl_pw_multi_affs A -> B and C -> D,
5900 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5902 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5903 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5905 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5906 &pw_multi_aff_range_product);
5909 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5910 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5912 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5913 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5915 isl_space *space;
5917 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5918 isl_pw_multi_aff_get_space(pma2));
5919 space = isl_space_flatten_range(space);
5920 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5921 &isl_multi_aff_flat_range_product);
5924 /* Given two isl_pw_multi_affs A -> B and C -> D,
5925 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5927 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5928 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5930 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5931 &pw_multi_aff_flat_range_product);
5934 /* If data->pma and "pma2" have the same domain space, then compute
5935 * their flat range product and the result to data->res.
5937 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5938 void *user)
5940 struct isl_union_pw_multi_aff_bin_data *data = user;
5942 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5943 pma2->dim, isl_dim_in)) {
5944 isl_pw_multi_aff_free(pma2);
5945 return isl_stat_ok;
5948 pma2 = isl_pw_multi_aff_flat_range_product(
5949 isl_pw_multi_aff_copy(data->pma), pma2);
5951 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5953 return isl_stat_ok;
5956 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5957 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5959 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5960 __isl_take isl_union_pw_multi_aff *upma1,
5961 __isl_take isl_union_pw_multi_aff *upma2)
5963 return bin_op(upma1, upma2, &flat_range_product_entry);
5966 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5967 * The parameters are assumed to have been aligned.
5969 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5970 * except that it works on two different isl_pw_* types.
5972 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5973 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5974 __isl_take isl_pw_aff *pa)
5976 int i, j, n;
5977 isl_pw_multi_aff *res = NULL;
5979 if (!pma || !pa)
5980 goto error;
5982 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5983 pa->dim, isl_dim_in))
5984 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5985 "domains don't match", goto error);
5986 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5987 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5988 "index out of bounds", goto error);
5990 n = pma->n * pa->n;
5991 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5993 for (i = 0; i < pma->n; ++i) {
5994 for (j = 0; j < pa->n; ++j) {
5995 isl_set *common;
5996 isl_multi_aff *res_ij;
5997 int empty;
5999 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6000 isl_set_copy(pa->p[j].set));
6001 empty = isl_set_plain_is_empty(common);
6002 if (empty < 0 || empty) {
6003 isl_set_free(common);
6004 if (empty < 0)
6005 goto error;
6006 continue;
6009 res_ij = isl_multi_aff_set_aff(
6010 isl_multi_aff_copy(pma->p[i].maff), pos,
6011 isl_aff_copy(pa->p[j].aff));
6012 res_ij = isl_multi_aff_gist(res_ij,
6013 isl_set_copy(common));
6015 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6019 isl_pw_multi_aff_free(pma);
6020 isl_pw_aff_free(pa);
6021 return res;
6022 error:
6023 isl_pw_multi_aff_free(pma);
6024 isl_pw_aff_free(pa);
6025 return isl_pw_multi_aff_free(res);
6028 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6030 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6031 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6032 __isl_take isl_pw_aff *pa)
6034 if (!pma || !pa)
6035 goto error;
6036 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
6037 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6038 if (!isl_space_has_named_params(pma->dim) ||
6039 !isl_space_has_named_params(pa->dim))
6040 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6041 "unaligned unnamed parameters", goto error);
6042 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6043 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6044 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6045 error:
6046 isl_pw_multi_aff_free(pma);
6047 isl_pw_aff_free(pa);
6048 return NULL;
6051 /* Do the parameters of "pa" match those of "space"?
6053 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6054 __isl_keep isl_space *space)
6056 isl_space *pa_space;
6057 int match;
6059 if (!pa || !space)
6060 return -1;
6062 pa_space = isl_pw_aff_get_space(pa);
6064 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6066 isl_space_free(pa_space);
6067 return match;
6070 /* Check that the domain space of "pa" matches "space".
6072 * Return 0 on success and -1 on error.
6074 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6075 __isl_keep isl_space *space)
6077 isl_space *pa_space;
6078 int match;
6080 if (!pa || !space)
6081 return -1;
6083 pa_space = isl_pw_aff_get_space(pa);
6085 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6086 if (match < 0)
6087 goto error;
6088 if (!match)
6089 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6090 "parameters don't match", goto error);
6091 match = isl_space_tuple_is_equal(space, isl_dim_in,
6092 pa_space, isl_dim_in);
6093 if (match < 0)
6094 goto error;
6095 if (!match)
6096 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6097 "domains don't match", goto error);
6098 isl_space_free(pa_space);
6099 return 0;
6100 error:
6101 isl_space_free(pa_space);
6102 return -1;
6105 #undef BASE
6106 #define BASE pw_aff
6107 #undef DOMBASE
6108 #define DOMBASE set
6110 #include <isl_multi_templ.c>
6111 #include <isl_multi_apply_set.c>
6112 #include <isl_multi_coalesce.c>
6113 #include <isl_multi_gist.c>
6114 #include <isl_multi_hash.c>
6115 #include <isl_multi_intersect.c>
6117 /* Scale the elements of "pma" by the corresponding elements of "mv".
6119 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6120 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6122 int i;
6124 pma = isl_pw_multi_aff_cow(pma);
6125 if (!pma || !mv)
6126 goto error;
6127 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6128 mv->space, isl_dim_set))
6129 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6130 "spaces don't match", goto error);
6131 if (!isl_space_match(pma->dim, isl_dim_param,
6132 mv->space, isl_dim_param)) {
6133 pma = isl_pw_multi_aff_align_params(pma,
6134 isl_multi_val_get_space(mv));
6135 mv = isl_multi_val_align_params(mv,
6136 isl_pw_multi_aff_get_space(pma));
6137 if (!pma || !mv)
6138 goto error;
6141 for (i = 0; i < pma->n; ++i) {
6142 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6143 isl_multi_val_copy(mv));
6144 if (!pma->p[i].maff)
6145 goto error;
6148 isl_multi_val_free(mv);
6149 return pma;
6150 error:
6151 isl_multi_val_free(mv);
6152 isl_pw_multi_aff_free(pma);
6153 return NULL;
6156 /* This function is called for each entry of an isl_union_pw_multi_aff.
6157 * If the space of the entry matches that of data->mv,
6158 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6159 * Otherwise, return an empty isl_pw_multi_aff.
6161 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6162 __isl_take isl_pw_multi_aff *pma, void *user)
6164 isl_multi_val *mv = user;
6166 if (!pma)
6167 return NULL;
6168 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6169 mv->space, isl_dim_set)) {
6170 isl_space *space = isl_pw_multi_aff_get_space(pma);
6171 isl_pw_multi_aff_free(pma);
6172 return isl_pw_multi_aff_empty(space);
6175 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6178 /* Scale the elements of "upma" by the corresponding elements of "mv",
6179 * for those entries that match the space of "mv".
6181 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6182 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6184 upma = isl_union_pw_multi_aff_align_params(upma,
6185 isl_multi_val_get_space(mv));
6186 mv = isl_multi_val_align_params(mv,
6187 isl_union_pw_multi_aff_get_space(upma));
6188 if (!upma || !mv)
6189 goto error;
6191 return isl_union_pw_multi_aff_transform(upma,
6192 &union_pw_multi_aff_scale_multi_val_entry, mv);
6194 isl_multi_val_free(mv);
6195 return upma;
6196 error:
6197 isl_multi_val_free(mv);
6198 isl_union_pw_multi_aff_free(upma);
6199 return NULL;
6202 /* Construct and return a piecewise multi affine expression
6203 * in the given space with value zero in each of the output dimensions and
6204 * a universe domain.
6206 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6208 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6211 /* Construct and return a piecewise multi affine expression
6212 * that is equal to the given piecewise affine expression.
6214 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6215 __isl_take isl_pw_aff *pa)
6217 int i;
6218 isl_space *space;
6219 isl_pw_multi_aff *pma;
6221 if (!pa)
6222 return NULL;
6224 space = isl_pw_aff_get_space(pa);
6225 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6227 for (i = 0; i < pa->n; ++i) {
6228 isl_set *set;
6229 isl_multi_aff *ma;
6231 set = isl_set_copy(pa->p[i].set);
6232 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6233 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6236 isl_pw_aff_free(pa);
6237 return pma;
6240 /* Construct a set or map mapping the shared (parameter) domain
6241 * of the piecewise affine expressions to the range of "mpa"
6242 * with each dimension in the range equated to the
6243 * corresponding piecewise affine expression.
6245 static __isl_give isl_map *map_from_multi_pw_aff(
6246 __isl_take isl_multi_pw_aff *mpa)
6248 int i;
6249 isl_space *space;
6250 isl_map *map;
6252 if (!mpa)
6253 return NULL;
6255 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6256 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6257 "invalid space", goto error);
6259 space = isl_multi_pw_aff_get_domain_space(mpa);
6260 map = isl_map_universe(isl_space_from_domain(space));
6262 for (i = 0; i < mpa->n; ++i) {
6263 isl_pw_aff *pa;
6264 isl_map *map_i;
6266 pa = isl_pw_aff_copy(mpa->p[i]);
6267 map_i = map_from_pw_aff(pa);
6269 map = isl_map_flat_range_product(map, map_i);
6272 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6274 isl_multi_pw_aff_free(mpa);
6275 return map;
6276 error:
6277 isl_multi_pw_aff_free(mpa);
6278 return NULL;
6281 /* Construct a map mapping the shared domain
6282 * of the piecewise affine expressions to the range of "mpa"
6283 * with each dimension in the range equated to the
6284 * corresponding piecewise affine expression.
6286 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6288 if (!mpa)
6289 return NULL;
6290 if (isl_space_is_set(mpa->space))
6291 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6292 "space of input is not a map", goto error);
6294 return map_from_multi_pw_aff(mpa);
6295 error:
6296 isl_multi_pw_aff_free(mpa);
6297 return NULL;
6300 /* Construct a set mapping the shared parameter domain
6301 * of the piecewise affine expressions to the space of "mpa"
6302 * with each dimension in the range equated to the
6303 * corresponding piecewise affine expression.
6305 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6307 if (!mpa)
6308 return NULL;
6309 if (!isl_space_is_set(mpa->space))
6310 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6311 "space of input is not a set", goto error);
6313 return map_from_multi_pw_aff(mpa);
6314 error:
6315 isl_multi_pw_aff_free(mpa);
6316 return NULL;
6319 /* Construct and return a piecewise multi affine expression
6320 * that is equal to the given multi piecewise affine expression
6321 * on the shared domain of the piecewise affine expressions.
6323 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6324 __isl_take isl_multi_pw_aff *mpa)
6326 int i;
6327 isl_space *space;
6328 isl_pw_aff *pa;
6329 isl_pw_multi_aff *pma;
6331 if (!mpa)
6332 return NULL;
6334 space = isl_multi_pw_aff_get_space(mpa);
6336 if (mpa->n == 0) {
6337 isl_multi_pw_aff_free(mpa);
6338 return isl_pw_multi_aff_zero(space);
6341 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6342 pma = isl_pw_multi_aff_from_pw_aff(pa);
6344 for (i = 1; i < mpa->n; ++i) {
6345 isl_pw_multi_aff *pma_i;
6347 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6348 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6349 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6352 pma = isl_pw_multi_aff_reset_space(pma, space);
6354 isl_multi_pw_aff_free(mpa);
6355 return pma;
6358 /* Construct and return a multi piecewise affine expression
6359 * that is equal to the given multi affine expression.
6361 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6362 __isl_take isl_multi_aff *ma)
6364 int i, n;
6365 isl_multi_pw_aff *mpa;
6367 if (!ma)
6368 return NULL;
6370 n = isl_multi_aff_dim(ma, isl_dim_out);
6371 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6373 for (i = 0; i < n; ++i) {
6374 isl_pw_aff *pa;
6376 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6377 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6380 isl_multi_aff_free(ma);
6381 return mpa;
6384 /* Construct and return a multi piecewise affine expression
6385 * that is equal to the given piecewise multi affine expression.
6387 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6388 __isl_take isl_pw_multi_aff *pma)
6390 int i, n;
6391 isl_space *space;
6392 isl_multi_pw_aff *mpa;
6394 if (!pma)
6395 return NULL;
6397 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6398 space = isl_pw_multi_aff_get_space(pma);
6399 mpa = isl_multi_pw_aff_alloc(space);
6401 for (i = 0; i < n; ++i) {
6402 isl_pw_aff *pa;
6404 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6405 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6408 isl_pw_multi_aff_free(pma);
6409 return mpa;
6412 /* Do "pa1" and "pa2" represent the same function?
6414 * We first check if they are obviously equal.
6415 * If not, we convert them to maps and check if those are equal.
6417 * If "pa1" or "pa2" contain any NaNs, then they are considered
6418 * not to be the same. A NaN is not equal to anything, not even
6419 * to another NaN.
6421 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6423 int equal;
6424 isl_bool has_nan;
6425 isl_map *map1, *map2;
6427 if (!pa1 || !pa2)
6428 return -1;
6430 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6431 if (equal < 0 || equal)
6432 return equal;
6433 has_nan = isl_pw_aff_involves_nan(pa1);
6434 if (has_nan >= 0 && !has_nan)
6435 has_nan = isl_pw_aff_involves_nan(pa2);
6436 if (has_nan < 0)
6437 return -1;
6438 if (has_nan)
6439 return 0;
6441 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6442 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6443 equal = isl_map_is_equal(map1, map2);
6444 isl_map_free(map1);
6445 isl_map_free(map2);
6447 return equal;
6450 /* Do "mpa1" and "mpa2" represent the same function?
6452 * Note that we cannot convert the entire isl_multi_pw_aff
6453 * to a map because the domains of the piecewise affine expressions
6454 * may not be the same.
6456 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6457 __isl_keep isl_multi_pw_aff *mpa2)
6459 int i;
6460 isl_bool equal;
6462 if (!mpa1 || !mpa2)
6463 return isl_bool_error;
6465 if (!isl_space_match(mpa1->space, isl_dim_param,
6466 mpa2->space, isl_dim_param)) {
6467 if (!isl_space_has_named_params(mpa1->space))
6468 return isl_bool_false;
6469 if (!isl_space_has_named_params(mpa2->space))
6470 return isl_bool_false;
6471 mpa1 = isl_multi_pw_aff_copy(mpa1);
6472 mpa2 = isl_multi_pw_aff_copy(mpa2);
6473 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6474 isl_multi_pw_aff_get_space(mpa2));
6475 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6476 isl_multi_pw_aff_get_space(mpa1));
6477 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6478 isl_multi_pw_aff_free(mpa1);
6479 isl_multi_pw_aff_free(mpa2);
6480 return equal;
6483 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6484 if (equal < 0 || !equal)
6485 return equal;
6487 for (i = 0; i < mpa1->n; ++i) {
6488 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6489 if (equal < 0 || !equal)
6490 return equal;
6493 return isl_bool_true;
6496 /* Compute the pullback of "mpa" by the function represented by "ma".
6497 * In other words, plug in "ma" in "mpa".
6499 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6501 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6502 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6504 int i;
6505 isl_space *space = NULL;
6507 mpa = isl_multi_pw_aff_cow(mpa);
6508 if (!mpa || !ma)
6509 goto error;
6511 space = isl_space_join(isl_multi_aff_get_space(ma),
6512 isl_multi_pw_aff_get_space(mpa));
6513 if (!space)
6514 goto error;
6516 for (i = 0; i < mpa->n; ++i) {
6517 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6518 isl_multi_aff_copy(ma));
6519 if (!mpa->p[i])
6520 goto error;
6523 isl_multi_aff_free(ma);
6524 isl_space_free(mpa->space);
6525 mpa->space = space;
6526 return mpa;
6527 error:
6528 isl_space_free(space);
6529 isl_multi_pw_aff_free(mpa);
6530 isl_multi_aff_free(ma);
6531 return NULL;
6534 /* Compute the pullback of "mpa" by the function represented by "ma".
6535 * In other words, plug in "ma" in "mpa".
6537 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6538 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6540 if (!mpa || !ma)
6541 goto error;
6542 if (isl_space_match(mpa->space, isl_dim_param,
6543 ma->space, isl_dim_param))
6544 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6545 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6546 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6547 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6548 error:
6549 isl_multi_pw_aff_free(mpa);
6550 isl_multi_aff_free(ma);
6551 return NULL;
6554 /* Compute the pullback of "mpa" by the function represented by "pma".
6555 * In other words, plug in "pma" in "mpa".
6557 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6559 static __isl_give isl_multi_pw_aff *
6560 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6561 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6563 int i;
6564 isl_space *space = NULL;
6566 mpa = isl_multi_pw_aff_cow(mpa);
6567 if (!mpa || !pma)
6568 goto error;
6570 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6571 isl_multi_pw_aff_get_space(mpa));
6573 for (i = 0; i < mpa->n; ++i) {
6574 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6575 isl_pw_multi_aff_copy(pma));
6576 if (!mpa->p[i])
6577 goto error;
6580 isl_pw_multi_aff_free(pma);
6581 isl_space_free(mpa->space);
6582 mpa->space = space;
6583 return mpa;
6584 error:
6585 isl_space_free(space);
6586 isl_multi_pw_aff_free(mpa);
6587 isl_pw_multi_aff_free(pma);
6588 return NULL;
6591 /* Compute the pullback of "mpa" by the function represented by "pma".
6592 * In other words, plug in "pma" in "mpa".
6594 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6595 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6597 if (!mpa || !pma)
6598 goto error;
6599 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6600 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6601 mpa = isl_multi_pw_aff_align_params(mpa,
6602 isl_pw_multi_aff_get_space(pma));
6603 pma = isl_pw_multi_aff_align_params(pma,
6604 isl_multi_pw_aff_get_space(mpa));
6605 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6606 error:
6607 isl_multi_pw_aff_free(mpa);
6608 isl_pw_multi_aff_free(pma);
6609 return NULL;
6612 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6613 * with the domain of "aff". The domain of the result is the same
6614 * as that of "mpa".
6615 * "mpa" and "aff" are assumed to have been aligned.
6617 * We first extract the parametric constant from "aff", defined
6618 * over the correct domain.
6619 * Then we add the appropriate combinations of the members of "mpa".
6620 * Finally, we add the integer divisions through recursive calls.
6622 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6623 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6625 int i, n_in, n_div;
6626 isl_space *space;
6627 isl_val *v;
6628 isl_pw_aff *pa;
6629 isl_aff *tmp;
6631 n_in = isl_aff_dim(aff, isl_dim_in);
6632 n_div = isl_aff_dim(aff, isl_dim_div);
6634 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6635 tmp = isl_aff_copy(aff);
6636 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6637 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6638 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6639 isl_space_dim(space, isl_dim_set));
6640 tmp = isl_aff_reset_domain_space(tmp, space);
6641 pa = isl_pw_aff_from_aff(tmp);
6643 for (i = 0; i < n_in; ++i) {
6644 isl_pw_aff *pa_i;
6646 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6647 continue;
6648 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6649 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6650 pa_i = isl_pw_aff_scale_val(pa_i, v);
6651 pa = isl_pw_aff_add(pa, pa_i);
6654 for (i = 0; i < n_div; ++i) {
6655 isl_aff *div;
6656 isl_pw_aff *pa_i;
6658 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6659 continue;
6660 div = isl_aff_get_div(aff, i);
6661 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6662 isl_multi_pw_aff_copy(mpa), div);
6663 pa_i = isl_pw_aff_floor(pa_i);
6664 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6665 pa_i = isl_pw_aff_scale_val(pa_i, v);
6666 pa = isl_pw_aff_add(pa, pa_i);
6669 isl_multi_pw_aff_free(mpa);
6670 isl_aff_free(aff);
6672 return pa;
6675 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6676 * with the domain of "aff". The domain of the result is the same
6677 * as that of "mpa".
6679 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6680 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6682 if (!aff || !mpa)
6683 goto error;
6684 if (isl_space_match(aff->ls->dim, isl_dim_param,
6685 mpa->space, isl_dim_param))
6686 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6688 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6689 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6691 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6692 error:
6693 isl_aff_free(aff);
6694 isl_multi_pw_aff_free(mpa);
6695 return NULL;
6698 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6699 * with the domain of "pa". The domain of the result is the same
6700 * as that of "mpa".
6701 * "mpa" and "pa" are assumed to have been aligned.
6703 * We consider each piece in turn. Note that the domains of the
6704 * pieces are assumed to be disjoint and they remain disjoint
6705 * after taking the preimage (over the same function).
6707 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6708 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6710 isl_space *space;
6711 isl_pw_aff *res;
6712 int i;
6714 if (!mpa || !pa)
6715 goto error;
6717 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6718 isl_pw_aff_get_space(pa));
6719 res = isl_pw_aff_empty(space);
6721 for (i = 0; i < pa->n; ++i) {
6722 isl_pw_aff *pa_i;
6723 isl_set *domain;
6725 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6726 isl_multi_pw_aff_copy(mpa),
6727 isl_aff_copy(pa->p[i].aff));
6728 domain = isl_set_copy(pa->p[i].set);
6729 domain = isl_set_preimage_multi_pw_aff(domain,
6730 isl_multi_pw_aff_copy(mpa));
6731 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6732 res = isl_pw_aff_add_disjoint(res, pa_i);
6735 isl_pw_aff_free(pa);
6736 isl_multi_pw_aff_free(mpa);
6737 return res;
6738 error:
6739 isl_pw_aff_free(pa);
6740 isl_multi_pw_aff_free(mpa);
6741 return NULL;
6744 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6745 * with the domain of "pa". The domain of the result is the same
6746 * as that of "mpa".
6748 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6749 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6751 if (!pa || !mpa)
6752 goto error;
6753 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6754 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6756 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6757 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6759 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6760 error:
6761 isl_pw_aff_free(pa);
6762 isl_multi_pw_aff_free(mpa);
6763 return NULL;
6766 /* Compute the pullback of "pa" by the function represented by "mpa".
6767 * In other words, plug in "mpa" in "pa".
6768 * "pa" and "mpa" are assumed to have been aligned.
6770 * The pullback is computed by applying "pa" to "mpa".
6772 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6773 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6775 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6778 /* Compute the pullback of "pa" by the function represented by "mpa".
6779 * In other words, plug in "mpa" in "pa".
6781 * The pullback is computed by applying "pa" to "mpa".
6783 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6784 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6786 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6789 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6790 * In other words, plug in "mpa2" in "mpa1".
6792 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6794 * We pullback each member of "mpa1" in turn.
6796 static __isl_give isl_multi_pw_aff *
6797 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6798 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6800 int i;
6801 isl_space *space = NULL;
6803 mpa1 = isl_multi_pw_aff_cow(mpa1);
6804 if (!mpa1 || !mpa2)
6805 goto error;
6807 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6808 isl_multi_pw_aff_get_space(mpa1));
6810 for (i = 0; i < mpa1->n; ++i) {
6811 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6812 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6813 if (!mpa1->p[i])
6814 goto error;
6817 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6819 isl_multi_pw_aff_free(mpa2);
6820 return mpa1;
6821 error:
6822 isl_space_free(space);
6823 isl_multi_pw_aff_free(mpa1);
6824 isl_multi_pw_aff_free(mpa2);
6825 return NULL;
6828 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6829 * In other words, plug in "mpa2" in "mpa1".
6831 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6832 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6834 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6835 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6838 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6839 * of "mpa1" and "mpa2" live in the same space, construct map space
6840 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6841 * with this map space as extract argument.
6843 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6844 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6845 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6846 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6848 int match;
6849 isl_space *space1, *space2;
6850 isl_map *res;
6852 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6853 isl_multi_pw_aff_get_space(mpa2));
6854 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6855 isl_multi_pw_aff_get_space(mpa1));
6856 if (!mpa1 || !mpa2)
6857 goto error;
6858 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6859 mpa2->space, isl_dim_out);
6860 if (match < 0)
6861 goto error;
6862 if (!match)
6863 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6864 "range spaces don't match", goto error);
6865 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6866 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6867 space1 = isl_space_map_from_domain_and_range(space1, space2);
6869 res = order(mpa1, mpa2, space1);
6870 isl_multi_pw_aff_free(mpa1);
6871 isl_multi_pw_aff_free(mpa2);
6872 return res;
6873 error:
6874 isl_multi_pw_aff_free(mpa1);
6875 isl_multi_pw_aff_free(mpa2);
6876 return NULL;
6879 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6880 * where the function values are equal. "space" is the space of the result.
6881 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6883 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6884 * in the sequences are equal.
6886 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6887 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6888 __isl_take isl_space *space)
6890 int i, n;
6891 isl_map *res;
6893 res = isl_map_universe(space);
6895 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6896 for (i = 0; i < n; ++i) {
6897 isl_pw_aff *pa1, *pa2;
6898 isl_map *map;
6900 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6901 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6902 map = isl_pw_aff_eq_map(pa1, pa2);
6903 res = isl_map_intersect(res, map);
6906 return res;
6909 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6910 * where the function values are equal.
6912 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6913 __isl_take isl_multi_pw_aff *mpa2)
6915 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6916 &isl_multi_pw_aff_eq_map_on_space);
6919 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6920 * where the function values of "mpa1" is lexicographically satisfies "base"
6921 * compared to that of "mpa2". "space" is the space of the result.
6922 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6924 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6925 * if its i-th element satisfies "base" when compared to
6926 * the i-th element of "mpa2" while all previous elements are
6927 * pairwise equal.
6929 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6930 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6931 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6932 __isl_take isl_pw_aff *pa2),
6933 __isl_take isl_space *space)
6935 int i, n;
6936 isl_map *res, *rest;
6938 res = isl_map_empty(isl_space_copy(space));
6939 rest = isl_map_universe(space);
6941 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6942 for (i = 0; i < n; ++i) {
6943 isl_pw_aff *pa1, *pa2;
6944 isl_map *map;
6946 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6947 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6948 map = base(pa1, pa2);
6949 map = isl_map_intersect(map, isl_map_copy(rest));
6950 res = isl_map_union(res, map);
6952 if (i == n - 1)
6953 continue;
6955 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6956 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6957 map = isl_pw_aff_eq_map(pa1, pa2);
6958 rest = isl_map_intersect(rest, map);
6961 isl_map_free(rest);
6962 return res;
6965 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6966 * where the function value of "mpa1" is lexicographically less than that
6967 * of "mpa2". "space" is the space of the result.
6968 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6970 * "mpa1" is less than "mpa2" if its i-th element is smaller
6971 * than the i-th element of "mpa2" while all previous elements are
6972 * pairwise equal.
6974 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6975 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6976 __isl_take isl_space *space)
6978 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6979 &isl_pw_aff_lt_map, space);
6982 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6983 * where the function value of "mpa1" is lexicographically less than that
6984 * of "mpa2".
6986 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6987 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6989 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6990 &isl_multi_pw_aff_lex_lt_map_on_space);
6993 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6994 * where the function value of "mpa1" is lexicographically greater than that
6995 * of "mpa2". "space" is the space of the result.
6996 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6998 * "mpa1" is greater than "mpa2" if its i-th element is greater
6999 * than the i-th element of "mpa2" while all previous elements are
7000 * pairwise equal.
7002 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7003 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7004 __isl_take isl_space *space)
7006 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7007 &isl_pw_aff_gt_map, space);
7010 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7011 * where the function value of "mpa1" is lexicographically greater than that
7012 * of "mpa2".
7014 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7015 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7017 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7018 &isl_multi_pw_aff_lex_gt_map_on_space);
7021 /* Compare two isl_affs.
7023 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7024 * than "aff2" and 0 if they are equal.
7026 * The order is fairly arbitrary. We do consider expressions that only involve
7027 * earlier dimensions as "smaller".
7029 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7031 int cmp;
7032 int last1, last2;
7034 if (aff1 == aff2)
7035 return 0;
7037 if (!aff1)
7038 return -1;
7039 if (!aff2)
7040 return 1;
7042 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7043 if (cmp != 0)
7044 return cmp;
7046 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7047 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7048 if (last1 != last2)
7049 return last1 - last2;
7051 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7054 /* Compare two isl_pw_affs.
7056 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7057 * than "pa2" and 0 if they are equal.
7059 * The order is fairly arbitrary. We do consider expressions that only involve
7060 * earlier dimensions as "smaller".
7062 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7063 __isl_keep isl_pw_aff *pa2)
7065 int i;
7066 int cmp;
7068 if (pa1 == pa2)
7069 return 0;
7071 if (!pa1)
7072 return -1;
7073 if (!pa2)
7074 return 1;
7076 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7077 if (cmp != 0)
7078 return cmp;
7080 if (pa1->n != pa2->n)
7081 return pa1->n - pa2->n;
7083 for (i = 0; i < pa1->n; ++i) {
7084 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7085 if (cmp != 0)
7086 return cmp;
7087 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7088 if (cmp != 0)
7089 return cmp;
7092 return 0;
7095 /* Return a piecewise affine expression that is equal to "v" on "domain".
7097 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7098 __isl_take isl_val *v)
7100 isl_space *space;
7101 isl_local_space *ls;
7102 isl_aff *aff;
7104 space = isl_set_get_space(domain);
7105 ls = isl_local_space_from_space(space);
7106 aff = isl_aff_val_on_domain(ls, v);
7108 return isl_pw_aff_alloc(domain, aff);
7111 /* Return a multi affine expression that is equal to "mv" on domain
7112 * space "space".
7114 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7115 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7117 int i, n;
7118 isl_space *space2;
7119 isl_local_space *ls;
7120 isl_multi_aff *ma;
7122 if (!space || !mv)
7123 goto error;
7125 n = isl_multi_val_dim(mv, isl_dim_set);
7126 space2 = isl_multi_val_get_space(mv);
7127 space2 = isl_space_align_params(space2, isl_space_copy(space));
7128 space = isl_space_align_params(space, isl_space_copy(space2));
7129 space = isl_space_map_from_domain_and_range(space, space2);
7130 ma = isl_multi_aff_alloc(isl_space_copy(space));
7131 ls = isl_local_space_from_space(isl_space_domain(space));
7132 for (i = 0; i < n; ++i) {
7133 isl_val *v;
7134 isl_aff *aff;
7136 v = isl_multi_val_get_val(mv, i);
7137 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7138 ma = isl_multi_aff_set_aff(ma, i, aff);
7140 isl_local_space_free(ls);
7142 isl_multi_val_free(mv);
7143 return ma;
7144 error:
7145 isl_space_free(space);
7146 isl_multi_val_free(mv);
7147 return NULL;
7150 /* Return a piecewise multi-affine expression
7151 * that is equal to "mv" on "domain".
7153 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7154 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7156 isl_space *space;
7157 isl_multi_aff *ma;
7159 space = isl_set_get_space(domain);
7160 ma = isl_multi_aff_multi_val_on_space(space, mv);
7162 return isl_pw_multi_aff_alloc(domain, ma);
7165 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7166 * mv is the value that should be attained on each domain set
7167 * res collects the results
7169 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7170 isl_multi_val *mv;
7171 isl_union_pw_multi_aff *res;
7174 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7175 * and add it to data->res.
7177 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7178 void *user)
7180 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7181 isl_pw_multi_aff *pma;
7182 isl_multi_val *mv;
7184 mv = isl_multi_val_copy(data->mv);
7185 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7186 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7188 return data->res ? isl_stat_ok : isl_stat_error;
7191 /* Return a union piecewise multi-affine expression
7192 * that is equal to "mv" on "domain".
7194 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7195 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7197 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7198 isl_space *space;
7200 space = isl_union_set_get_space(domain);
7201 data.res = isl_union_pw_multi_aff_empty(space);
7202 data.mv = mv;
7203 if (isl_union_set_foreach_set(domain,
7204 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7205 data.res = isl_union_pw_multi_aff_free(data.res);
7206 isl_union_set_free(domain);
7207 isl_multi_val_free(mv);
7208 return data.res;
7211 /* Compute the pullback of data->pma by the function represented by "pma2",
7212 * provided the spaces match, and add the results to data->res.
7214 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7216 struct isl_union_pw_multi_aff_bin_data *data = user;
7218 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7219 pma2->dim, isl_dim_out)) {
7220 isl_pw_multi_aff_free(pma2);
7221 return isl_stat_ok;
7224 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7225 isl_pw_multi_aff_copy(data->pma), pma2);
7227 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7228 if (!data->res)
7229 return isl_stat_error;
7231 return isl_stat_ok;
7234 /* Compute the pullback of "upma1" by the function represented by "upma2".
7236 __isl_give isl_union_pw_multi_aff *
7237 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7238 __isl_take isl_union_pw_multi_aff *upma1,
7239 __isl_take isl_union_pw_multi_aff *upma2)
7241 return bin_op(upma1, upma2, &pullback_entry);
7244 /* Check that the domain space of "upa" matches "space".
7246 * Return 0 on success and -1 on error.
7248 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7249 * can in principle never fail since the space "space" is that
7250 * of the isl_multi_union_pw_aff and is a set space such that
7251 * there is no domain space to match.
7253 * We check the parameters and double-check that "space" is
7254 * indeed that of a set.
7256 static int isl_union_pw_aff_check_match_domain_space(
7257 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7259 isl_space *upa_space;
7260 int match;
7262 if (!upa || !space)
7263 return -1;
7265 match = isl_space_is_set(space);
7266 if (match < 0)
7267 return -1;
7268 if (!match)
7269 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7270 "expecting set space", return -1);
7272 upa_space = isl_union_pw_aff_get_space(upa);
7273 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7274 if (match < 0)
7275 goto error;
7276 if (!match)
7277 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7278 "parameters don't match", goto error);
7280 isl_space_free(upa_space);
7281 return 0;
7282 error:
7283 isl_space_free(upa_space);
7284 return -1;
7287 /* Do the parameters of "upa" match those of "space"?
7289 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7290 __isl_keep isl_space *space)
7292 isl_space *upa_space;
7293 int match;
7295 if (!upa || !space)
7296 return -1;
7298 upa_space = isl_union_pw_aff_get_space(upa);
7300 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7302 isl_space_free(upa_space);
7303 return match;
7306 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7307 * space represents the new parameters.
7308 * res collects the results.
7310 struct isl_union_pw_aff_reset_params_data {
7311 isl_space *space;
7312 isl_union_pw_aff *res;
7315 /* Replace the parameters of "pa" by data->space and
7316 * add the result to data->res.
7318 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7320 struct isl_union_pw_aff_reset_params_data *data = user;
7321 isl_space *space;
7323 space = isl_pw_aff_get_space(pa);
7324 space = isl_space_replace(space, isl_dim_param, data->space);
7325 pa = isl_pw_aff_reset_space(pa, space);
7326 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7328 return data->res ? isl_stat_ok : isl_stat_error;
7331 /* Replace the domain space of "upa" by "space".
7332 * Since a union expression does not have a (single) domain space,
7333 * "space" is necessarily a parameter space.
7335 * Since the order and the names of the parameters determine
7336 * the hash value, we need to create a new hash table.
7338 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7339 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7341 struct isl_union_pw_aff_reset_params_data data = { space };
7342 int match;
7344 match = isl_union_pw_aff_matching_params(upa, space);
7345 if (match < 0)
7346 upa = isl_union_pw_aff_free(upa);
7347 else if (match) {
7348 isl_space_free(space);
7349 return upa;
7352 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7353 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7354 data.res = isl_union_pw_aff_free(data.res);
7356 isl_union_pw_aff_free(upa);
7357 isl_space_free(space);
7358 return data.res;
7361 /* Return the floor of "pa".
7363 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7365 return isl_pw_aff_floor(pa);
7368 /* Given f, return floor(f).
7370 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7371 __isl_take isl_union_pw_aff *upa)
7373 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7376 /* Compute
7378 * upa mod m = upa - m * floor(upa/m)
7380 * with m an integer value.
7382 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7383 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7385 isl_union_pw_aff *res;
7387 if (!upa || !m)
7388 goto error;
7390 if (!isl_val_is_int(m))
7391 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7392 "expecting integer modulo", goto error);
7393 if (!isl_val_is_pos(m))
7394 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7395 "expecting positive modulo", goto error);
7397 res = isl_union_pw_aff_copy(upa);
7398 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7399 upa = isl_union_pw_aff_floor(upa);
7400 upa = isl_union_pw_aff_scale_val(upa, m);
7401 res = isl_union_pw_aff_sub(res, upa);
7403 return res;
7404 error:
7405 isl_val_free(m);
7406 isl_union_pw_aff_free(upa);
7407 return NULL;
7410 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7411 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7412 * needs to attain.
7413 * "res" collects the results.
7415 struct isl_union_pw_aff_aff_on_domain_data {
7416 isl_aff *aff;
7417 isl_union_pw_aff *res;
7420 /* Construct a piecewise affine expression that is equal to data->aff
7421 * on "domain" and add the result to data->res.
7423 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7425 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7426 isl_pw_aff *pa;
7427 isl_aff *aff;
7428 int dim;
7430 aff = isl_aff_copy(data->aff);
7431 dim = isl_set_dim(domain, isl_dim_set);
7432 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7433 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7434 pa = isl_pw_aff_alloc(domain, aff);
7435 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7437 return data->res ? isl_stat_ok : isl_stat_error;
7440 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7441 * pos is the output position that needs to be extracted.
7442 * res collects the results.
7444 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7445 int pos;
7446 isl_union_pw_aff *res;
7449 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7450 * (assuming it has such a dimension) and add it to data->res.
7452 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7454 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7455 int n_out;
7456 isl_pw_aff *pa;
7458 if (!pma)
7459 return isl_stat_error;
7461 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7462 if (data->pos >= n_out) {
7463 isl_pw_multi_aff_free(pma);
7464 return isl_stat_ok;
7467 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7468 isl_pw_multi_aff_free(pma);
7470 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7472 return data->res ? isl_stat_ok : isl_stat_error;
7475 /* Extract an isl_union_pw_aff corresponding to
7476 * output dimension "pos" of "upma".
7478 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7479 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7481 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7482 isl_space *space;
7484 if (!upma)
7485 return NULL;
7487 if (pos < 0)
7488 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7489 "cannot extract at negative position", return NULL);
7491 space = isl_union_pw_multi_aff_get_space(upma);
7492 data.res = isl_union_pw_aff_empty(space);
7493 data.pos = pos;
7494 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7495 &get_union_pw_aff, &data) < 0)
7496 data.res = isl_union_pw_aff_free(data.res);
7498 return data.res;
7501 /* Return a union piecewise affine expression
7502 * that is equal to "aff" on "domain".
7504 * Construct an isl_pw_aff on each of the sets in "domain" and
7505 * collect the results.
7507 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7508 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7510 struct isl_union_pw_aff_aff_on_domain_data data;
7511 isl_space *space;
7513 if (!domain || !aff)
7514 goto error;
7515 if (!isl_local_space_is_params(aff->ls))
7516 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7517 "expecting parametric expression", goto error);
7519 space = isl_union_set_get_space(domain);
7520 data.res = isl_union_pw_aff_empty(space);
7521 data.aff = aff;
7522 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7523 data.res = isl_union_pw_aff_free(data.res);
7524 isl_union_set_free(domain);
7525 isl_aff_free(aff);
7526 return data.res;
7527 error:
7528 isl_union_set_free(domain);
7529 isl_aff_free(aff);
7530 return NULL;
7533 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7534 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7535 * "res" collects the results.
7537 struct isl_union_pw_aff_val_on_domain_data {
7538 isl_val *v;
7539 isl_union_pw_aff *res;
7542 /* Construct a piecewise affine expression that is equal to data->v
7543 * on "domain" and add the result to data->res.
7545 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7547 struct isl_union_pw_aff_val_on_domain_data *data = user;
7548 isl_pw_aff *pa;
7549 isl_val *v;
7551 v = isl_val_copy(data->v);
7552 pa = isl_pw_aff_val_on_domain(domain, v);
7553 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7555 return data->res ? isl_stat_ok : isl_stat_error;
7558 /* Return a union piecewise affine expression
7559 * that is equal to "v" on "domain".
7561 * Construct an isl_pw_aff on each of the sets in "domain" and
7562 * collect the results.
7564 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7565 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7567 struct isl_union_pw_aff_val_on_domain_data data;
7568 isl_space *space;
7570 space = isl_union_set_get_space(domain);
7571 data.res = isl_union_pw_aff_empty(space);
7572 data.v = v;
7573 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7574 data.res = isl_union_pw_aff_free(data.res);
7575 isl_union_set_free(domain);
7576 isl_val_free(v);
7577 return data.res;
7580 /* Construct a piecewise multi affine expression
7581 * that is equal to "pa" and add it to upma.
7583 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7584 void *user)
7586 isl_union_pw_multi_aff **upma = user;
7587 isl_pw_multi_aff *pma;
7589 pma = isl_pw_multi_aff_from_pw_aff(pa);
7590 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7592 return *upma ? isl_stat_ok : isl_stat_error;
7595 /* Construct and return a union piecewise multi affine expression
7596 * that is equal to the given union piecewise affine expression.
7598 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7599 __isl_take isl_union_pw_aff *upa)
7601 isl_space *space;
7602 isl_union_pw_multi_aff *upma;
7604 if (!upa)
7605 return NULL;
7607 space = isl_union_pw_aff_get_space(upa);
7608 upma = isl_union_pw_multi_aff_empty(space);
7610 if (isl_union_pw_aff_foreach_pw_aff(upa,
7611 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7612 upma = isl_union_pw_multi_aff_free(upma);
7614 isl_union_pw_aff_free(upa);
7615 return upma;
7618 /* Compute the set of elements in the domain of "pa" where it is zero and
7619 * add this set to "uset".
7621 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7623 isl_union_set **uset = (isl_union_set **)user;
7625 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7627 return *uset ? isl_stat_ok : isl_stat_error;
7630 /* Return a union set containing those elements in the domain
7631 * of "upa" where it is zero.
7633 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7634 __isl_take isl_union_pw_aff *upa)
7636 isl_union_set *zero;
7638 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7639 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7640 zero = isl_union_set_free(zero);
7642 isl_union_pw_aff_free(upa);
7643 return zero;
7646 /* Convert "pa" to an isl_map and add it to *umap.
7648 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7650 isl_union_map **umap = user;
7651 isl_map *map;
7653 map = isl_map_from_pw_aff(pa);
7654 *umap = isl_union_map_add_map(*umap, map);
7656 return *umap ? isl_stat_ok : isl_stat_error;
7659 /* Construct a union map mapping the domain of the union
7660 * piecewise affine expression to its range, with the single output dimension
7661 * equated to the corresponding affine expressions on their cells.
7663 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7664 __isl_take isl_union_pw_aff *upa)
7666 isl_space *space;
7667 isl_union_map *umap;
7669 if (!upa)
7670 return NULL;
7672 space = isl_union_pw_aff_get_space(upa);
7673 umap = isl_union_map_empty(space);
7675 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7676 &umap) < 0)
7677 umap = isl_union_map_free(umap);
7679 isl_union_pw_aff_free(upa);
7680 return umap;
7683 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7684 * upma is the function that is plugged in.
7685 * pa is the current part of the function in which upma is plugged in.
7686 * res collects the results.
7688 struct isl_union_pw_aff_pullback_upma_data {
7689 isl_union_pw_multi_aff *upma;
7690 isl_pw_aff *pa;
7691 isl_union_pw_aff *res;
7694 /* Check if "pma" can be plugged into data->pa.
7695 * If so, perform the pullback and add the result to data->res.
7697 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7699 struct isl_union_pw_aff_pullback_upma_data *data = user;
7700 isl_pw_aff *pa;
7702 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7703 pma->dim, isl_dim_out)) {
7704 isl_pw_multi_aff_free(pma);
7705 return isl_stat_ok;
7708 pa = isl_pw_aff_copy(data->pa);
7709 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7711 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7713 return data->res ? isl_stat_ok : isl_stat_error;
7716 /* Check if any of the elements of data->upma can be plugged into pa,
7717 * add if so add the result to data->res.
7719 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7721 struct isl_union_pw_aff_pullback_upma_data *data = user;
7722 isl_stat r;
7724 data->pa = pa;
7725 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7726 &pa_pb_pma, data);
7727 isl_pw_aff_free(pa);
7729 return r;
7732 /* Compute the pullback of "upa" by the function represented by "upma".
7733 * In other words, plug in "upma" in "upa". The result contains
7734 * expressions defined over the domain space of "upma".
7736 * Run over all pairs of elements in "upa" and "upma", perform
7737 * the pullback when appropriate and collect the results.
7738 * If the hash value were based on the domain space rather than
7739 * the function space, then we could run through all elements
7740 * of "upma" and directly pick out the corresponding element of "upa".
7742 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7743 __isl_take isl_union_pw_aff *upa,
7744 __isl_take isl_union_pw_multi_aff *upma)
7746 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7747 isl_space *space;
7749 space = isl_union_pw_multi_aff_get_space(upma);
7750 upa = isl_union_pw_aff_align_params(upa, space);
7751 space = isl_union_pw_aff_get_space(upa);
7752 upma = isl_union_pw_multi_aff_align_params(upma, space);
7754 if (!upa || !upma)
7755 goto error;
7757 data.upma = upma;
7758 data.res = isl_union_pw_aff_alloc_same_size(upa);
7759 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7760 data.res = isl_union_pw_aff_free(data.res);
7762 isl_union_pw_aff_free(upa);
7763 isl_union_pw_multi_aff_free(upma);
7764 return data.res;
7765 error:
7766 isl_union_pw_aff_free(upa);
7767 isl_union_pw_multi_aff_free(upma);
7768 return NULL;
7771 #undef BASE
7772 #define BASE union_pw_aff
7773 #undef DOMBASE
7774 #define DOMBASE union_set
7776 #define NO_MOVE_DIMS
7777 #define NO_DIMS
7778 #define NO_DOMAIN
7779 #define NO_PRODUCT
7780 #define NO_SPLICE
7781 #define NO_ZERO
7782 #define NO_IDENTITY
7783 #define NO_GIST
7785 #include <isl_multi_templ.c>
7786 #include <isl_multi_apply_set.c>
7787 #include <isl_multi_apply_union_set.c>
7788 #include <isl_multi_coalesce.c>
7789 #include <isl_multi_floor.c>
7790 #include <isl_multi_gist.c>
7791 #include <isl_multi_intersect.c>
7793 /* Construct a multiple union piecewise affine expression
7794 * in the given space with value zero in each of the output dimensions.
7796 * Since there is no canonical zero value for
7797 * a union piecewise affine expression, we can only construct
7798 * zero-dimensional "zero" value.
7800 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7801 __isl_take isl_space *space)
7803 if (!space)
7804 return NULL;
7806 if (!isl_space_is_set(space))
7807 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7808 "expecting set space", goto error);
7809 if (isl_space_dim(space , isl_dim_out) != 0)
7810 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7811 "expecting 0D space", goto error);
7813 return isl_multi_union_pw_aff_alloc(space);
7814 error:
7815 isl_space_free(space);
7816 return NULL;
7819 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7820 * with the actual sum on the shared domain and
7821 * the defined expression on the symmetric difference of the domains.
7823 * We simply iterate over the elements in both arguments and
7824 * call isl_union_pw_aff_union_add on each of them.
7826 static __isl_give isl_multi_union_pw_aff *
7827 isl_multi_union_pw_aff_union_add_aligned(
7828 __isl_take isl_multi_union_pw_aff *mupa1,
7829 __isl_take isl_multi_union_pw_aff *mupa2)
7831 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7832 &isl_union_pw_aff_union_add);
7835 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7836 * with the actual sum on the shared domain and
7837 * the defined expression on the symmetric difference of the domains.
7839 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7840 __isl_take isl_multi_union_pw_aff *mupa1,
7841 __isl_take isl_multi_union_pw_aff *mupa2)
7843 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7844 &isl_multi_union_pw_aff_union_add_aligned);
7847 /* Construct and return a multi union piecewise affine expression
7848 * that is equal to the given multi affine expression.
7850 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7851 __isl_take isl_multi_aff *ma)
7853 isl_multi_pw_aff *mpa;
7855 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7856 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7859 /* Construct and return a multi union piecewise affine expression
7860 * that is equal to the given multi piecewise affine expression.
7862 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7863 __isl_take isl_multi_pw_aff *mpa)
7865 int i, n;
7866 isl_space *space;
7867 isl_multi_union_pw_aff *mupa;
7869 if (!mpa)
7870 return NULL;
7872 space = isl_multi_pw_aff_get_space(mpa);
7873 space = isl_space_range(space);
7874 mupa = isl_multi_union_pw_aff_alloc(space);
7876 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7877 for (i = 0; i < n; ++i) {
7878 isl_pw_aff *pa;
7879 isl_union_pw_aff *upa;
7881 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7882 upa = isl_union_pw_aff_from_pw_aff(pa);
7883 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7886 isl_multi_pw_aff_free(mpa);
7888 return mupa;
7891 /* Extract the range space of "pma" and assign it to *space.
7892 * If *space has already been set (through a previous call to this function),
7893 * then check that the range space is the same.
7895 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7897 isl_space **space = user;
7898 isl_space *pma_space;
7899 isl_bool equal;
7901 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7902 isl_pw_multi_aff_free(pma);
7904 if (!pma_space)
7905 return isl_stat_error;
7906 if (!*space) {
7907 *space = pma_space;
7908 return isl_stat_ok;
7911 equal = isl_space_is_equal(pma_space, *space);
7912 isl_space_free(pma_space);
7914 if (equal < 0)
7915 return isl_stat_error;
7916 if (!equal)
7917 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7918 "range spaces not the same", return isl_stat_error);
7919 return isl_stat_ok;
7922 /* Construct and return a multi union piecewise affine expression
7923 * that is equal to the given union piecewise multi affine expression.
7925 * In order to be able to perform the conversion, the input
7926 * needs to be non-empty and may only involve a single range space.
7928 __isl_give isl_multi_union_pw_aff *
7929 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7930 __isl_take isl_union_pw_multi_aff *upma)
7932 isl_space *space = NULL;
7933 isl_multi_union_pw_aff *mupa;
7934 int i, n;
7936 if (!upma)
7937 return NULL;
7938 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7939 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7940 "cannot extract range space from empty input",
7941 goto error);
7942 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7943 &space) < 0)
7944 goto error;
7946 if (!space)
7947 goto error;
7949 n = isl_space_dim(space, isl_dim_set);
7950 mupa = isl_multi_union_pw_aff_alloc(space);
7952 for (i = 0; i < n; ++i) {
7953 isl_union_pw_aff *upa;
7955 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7956 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7959 isl_union_pw_multi_aff_free(upma);
7960 return mupa;
7961 error:
7962 isl_space_free(space);
7963 isl_union_pw_multi_aff_free(upma);
7964 return NULL;
7967 /* Try and create an isl_multi_union_pw_aff that is equivalent
7968 * to the given isl_union_map.
7969 * The isl_union_map is required to be single-valued in each space.
7970 * Moreover, it cannot be empty and all range spaces need to be the same.
7971 * Otherwise, an error is produced.
7973 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7974 __isl_take isl_union_map *umap)
7976 isl_union_pw_multi_aff *upma;
7978 upma = isl_union_pw_multi_aff_from_union_map(umap);
7979 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7982 /* Return a multiple union piecewise affine expression
7983 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7984 * have been aligned.
7986 static __isl_give isl_multi_union_pw_aff *
7987 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7988 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7990 int i, n;
7991 isl_space *space;
7992 isl_multi_union_pw_aff *mupa;
7994 if (!domain || !mv)
7995 goto error;
7997 n = isl_multi_val_dim(mv, isl_dim_set);
7998 space = isl_multi_val_get_space(mv);
7999 mupa = isl_multi_union_pw_aff_alloc(space);
8000 for (i = 0; i < n; ++i) {
8001 isl_val *v;
8002 isl_union_pw_aff *upa;
8004 v = isl_multi_val_get_val(mv, i);
8005 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8007 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8010 isl_union_set_free(domain);
8011 isl_multi_val_free(mv);
8012 return mupa;
8013 error:
8014 isl_union_set_free(domain);
8015 isl_multi_val_free(mv);
8016 return NULL;
8019 /* Return a multiple union piecewise affine expression
8020 * that is equal to "mv" on "domain".
8022 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8023 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8025 if (!domain || !mv)
8026 goto error;
8027 if (isl_space_match(domain->dim, isl_dim_param,
8028 mv->space, isl_dim_param))
8029 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8030 domain, mv);
8031 domain = isl_union_set_align_params(domain,
8032 isl_multi_val_get_space(mv));
8033 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8034 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8035 error:
8036 isl_union_set_free(domain);
8037 isl_multi_val_free(mv);
8038 return NULL;
8041 /* Return a multiple union piecewise affine expression
8042 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8043 * have been aligned.
8045 static __isl_give isl_multi_union_pw_aff *
8046 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8047 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8049 int i, n;
8050 isl_space *space;
8051 isl_multi_union_pw_aff *mupa;
8053 if (!domain || !ma)
8054 goto error;
8056 n = isl_multi_aff_dim(ma, isl_dim_set);
8057 space = isl_multi_aff_get_space(ma);
8058 mupa = isl_multi_union_pw_aff_alloc(space);
8059 for (i = 0; i < n; ++i) {
8060 isl_aff *aff;
8061 isl_union_pw_aff *upa;
8063 aff = isl_multi_aff_get_aff(ma, i);
8064 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8065 aff);
8066 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8069 isl_union_set_free(domain);
8070 isl_multi_aff_free(ma);
8071 return mupa;
8072 error:
8073 isl_union_set_free(domain);
8074 isl_multi_aff_free(ma);
8075 return NULL;
8078 /* Return a multiple union piecewise affine expression
8079 * that is equal to "ma" on "domain".
8081 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8082 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8084 if (!domain || !ma)
8085 goto error;
8086 if (isl_space_match(domain->dim, isl_dim_param,
8087 ma->space, isl_dim_param))
8088 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8089 domain, ma);
8090 domain = isl_union_set_align_params(domain,
8091 isl_multi_aff_get_space(ma));
8092 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8093 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8094 error:
8095 isl_union_set_free(domain);
8096 isl_multi_aff_free(ma);
8097 return NULL;
8100 /* Return a union set containing those elements in the domains
8101 * of the elements of "mupa" where they are all zero.
8103 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8104 __isl_take isl_multi_union_pw_aff *mupa)
8106 int i, n;
8107 isl_union_pw_aff *upa;
8108 isl_union_set *zero;
8110 if (!mupa)
8111 return NULL;
8113 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8114 if (n == 0)
8115 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8116 "cannot determine zero set "
8117 "of zero-dimensional function", goto error);
8119 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8120 zero = isl_union_pw_aff_zero_union_set(upa);
8122 for (i = 1; i < n; ++i) {
8123 isl_union_set *zero_i;
8125 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8126 zero_i = isl_union_pw_aff_zero_union_set(upa);
8128 zero = isl_union_set_intersect(zero, zero_i);
8131 isl_multi_union_pw_aff_free(mupa);
8132 return zero;
8133 error:
8134 isl_multi_union_pw_aff_free(mupa);
8135 return NULL;
8138 /* Construct a union map mapping the shared domain
8139 * of the union piecewise affine expressions to the range of "mupa"
8140 * with each dimension in the range equated to the
8141 * corresponding union piecewise affine expression.
8143 * The input cannot be zero-dimensional as there is
8144 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8146 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8147 __isl_take isl_multi_union_pw_aff *mupa)
8149 int i, n;
8150 isl_space *space;
8151 isl_union_map *umap;
8152 isl_union_pw_aff *upa;
8154 if (!mupa)
8155 return NULL;
8157 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8158 if (n == 0)
8159 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8160 "cannot determine domain of zero-dimensional "
8161 "isl_multi_union_pw_aff", goto error);
8163 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8164 umap = isl_union_map_from_union_pw_aff(upa);
8166 for (i = 1; i < n; ++i) {
8167 isl_union_map *umap_i;
8169 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8170 umap_i = isl_union_map_from_union_pw_aff(upa);
8171 umap = isl_union_map_flat_range_product(umap, umap_i);
8174 space = isl_multi_union_pw_aff_get_space(mupa);
8175 umap = isl_union_map_reset_range_space(umap, space);
8177 isl_multi_union_pw_aff_free(mupa);
8178 return umap;
8179 error:
8180 isl_multi_union_pw_aff_free(mupa);
8181 return NULL;
8184 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8185 * "range" is the space from which to set the range space.
8186 * "res" collects the results.
8188 struct isl_union_pw_multi_aff_reset_range_space_data {
8189 isl_space *range;
8190 isl_union_pw_multi_aff *res;
8193 /* Replace the range space of "pma" by the range space of data->range and
8194 * add the result to data->res.
8196 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8198 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8199 isl_space *space;
8201 space = isl_pw_multi_aff_get_space(pma);
8202 space = isl_space_domain(space);
8203 space = isl_space_extend_domain_with_range(space,
8204 isl_space_copy(data->range));
8205 pma = isl_pw_multi_aff_reset_space(pma, space);
8206 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8208 return data->res ? isl_stat_ok : isl_stat_error;
8211 /* Replace the range space of all the piecewise affine expressions in "upma" by
8212 * the range space of "space".
8214 * This assumes that all these expressions have the same output dimension.
8216 * Since the spaces of the expressions change, so do their hash values.
8217 * We therefore need to create a new isl_union_pw_multi_aff.
8218 * Note that the hash value is currently computed based on the entire
8219 * space even though there can only be a single expression with a given
8220 * domain space.
8222 static __isl_give isl_union_pw_multi_aff *
8223 isl_union_pw_multi_aff_reset_range_space(
8224 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8226 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8227 isl_space *space_upma;
8229 space_upma = isl_union_pw_multi_aff_get_space(upma);
8230 data.res = isl_union_pw_multi_aff_empty(space_upma);
8231 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8232 &reset_range_space, &data) < 0)
8233 data.res = isl_union_pw_multi_aff_free(data.res);
8235 isl_space_free(space);
8236 isl_union_pw_multi_aff_free(upma);
8237 return data.res;
8240 /* Construct and return a union piecewise multi affine expression
8241 * that is equal to the given multi union piecewise affine expression.
8243 * In order to be able to perform the conversion, the input
8244 * needs to have a least one output dimension.
8246 __isl_give isl_union_pw_multi_aff *
8247 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8248 __isl_take isl_multi_union_pw_aff *mupa)
8250 int i, n;
8251 isl_space *space;
8252 isl_union_pw_multi_aff *upma;
8253 isl_union_pw_aff *upa;
8255 if (!mupa)
8256 return NULL;
8258 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8259 if (n == 0)
8260 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8261 "cannot determine domain of zero-dimensional "
8262 "isl_multi_union_pw_aff", goto error);
8264 space = isl_multi_union_pw_aff_get_space(mupa);
8265 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8266 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8268 for (i = 1; i < n; ++i) {
8269 isl_union_pw_multi_aff *upma_i;
8271 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8272 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8273 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8276 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8278 isl_multi_union_pw_aff_free(mupa);
8279 return upma;
8280 error:
8281 isl_multi_union_pw_aff_free(mupa);
8282 return NULL;
8285 /* Intersect the range of "mupa" with "range".
8286 * That is, keep only those domain elements that have a function value
8287 * in "range".
8289 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8290 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8292 isl_union_pw_multi_aff *upma;
8293 isl_union_set *domain;
8294 isl_space *space;
8295 int n;
8296 int match;
8298 if (!mupa || !range)
8299 goto error;
8301 space = isl_set_get_space(range);
8302 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8303 space, isl_dim_set);
8304 isl_space_free(space);
8305 if (match < 0)
8306 goto error;
8307 if (!match)
8308 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8309 "space don't match", goto error);
8310 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8311 if (n == 0)
8312 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8313 "cannot intersect range of zero-dimensional "
8314 "isl_multi_union_pw_aff", goto error);
8316 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8317 isl_multi_union_pw_aff_copy(mupa));
8318 domain = isl_union_set_from_set(range);
8319 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8320 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8322 return mupa;
8323 error:
8324 isl_multi_union_pw_aff_free(mupa);
8325 isl_set_free(range);
8326 return NULL;
8329 /* Return the shared domain of the elements of "mupa".
8331 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8332 __isl_take isl_multi_union_pw_aff *mupa)
8334 int i, n;
8335 isl_union_pw_aff *upa;
8336 isl_union_set *dom;
8338 if (!mupa)
8339 return NULL;
8341 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8342 if (n == 0)
8343 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8344 "cannot determine domain", goto error);
8346 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8347 dom = isl_union_pw_aff_domain(upa);
8348 for (i = 1; i < n; ++i) {
8349 isl_union_set *dom_i;
8351 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8352 dom_i = isl_union_pw_aff_domain(upa);
8353 dom = isl_union_set_intersect(dom, dom_i);
8356 isl_multi_union_pw_aff_free(mupa);
8357 return dom;
8358 error:
8359 isl_multi_union_pw_aff_free(mupa);
8360 return NULL;
8363 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8364 * In particular, the spaces have been aligned.
8365 * The result is defined over the shared domain of the elements of "mupa"
8367 * We first extract the parametric constant part of "aff" and
8368 * define that over the shared domain.
8369 * Then we iterate over all input dimensions of "aff" and add the corresponding
8370 * multiples of the elements of "mupa".
8371 * Finally, we consider the integer divisions, calling the function
8372 * recursively to obtain an isl_union_pw_aff corresponding to the
8373 * integer division argument.
8375 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8376 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8378 int i, n_in, n_div;
8379 isl_union_pw_aff *upa;
8380 isl_union_set *uset;
8381 isl_val *v;
8382 isl_aff *cst;
8384 n_in = isl_aff_dim(aff, isl_dim_in);
8385 n_div = isl_aff_dim(aff, isl_dim_div);
8387 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8388 cst = isl_aff_copy(aff);
8389 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8390 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8391 cst = isl_aff_project_domain_on_params(cst);
8392 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8394 for (i = 0; i < n_in; ++i) {
8395 isl_union_pw_aff *upa_i;
8397 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8398 continue;
8399 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8400 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8401 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8402 upa = isl_union_pw_aff_add(upa, upa_i);
8405 for (i = 0; i < n_div; ++i) {
8406 isl_aff *div;
8407 isl_union_pw_aff *upa_i;
8409 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8410 continue;
8411 div = isl_aff_get_div(aff, i);
8412 upa_i = multi_union_pw_aff_apply_aff(
8413 isl_multi_union_pw_aff_copy(mupa), div);
8414 upa_i = isl_union_pw_aff_floor(upa_i);
8415 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8416 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8417 upa = isl_union_pw_aff_add(upa, upa_i);
8420 isl_multi_union_pw_aff_free(mupa);
8421 isl_aff_free(aff);
8423 return upa;
8426 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8427 * with the domain of "aff".
8428 * Furthermore, the dimension of this space needs to be greater than zero.
8429 * The result is defined over the shared domain of the elements of "mupa"
8431 * We perform these checks and then hand over control to
8432 * multi_union_pw_aff_apply_aff.
8434 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8435 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8437 isl_space *space1, *space2;
8438 int equal;
8440 mupa = isl_multi_union_pw_aff_align_params(mupa,
8441 isl_aff_get_space(aff));
8442 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8443 if (!mupa || !aff)
8444 goto error;
8446 space1 = isl_multi_union_pw_aff_get_space(mupa);
8447 space2 = isl_aff_get_domain_space(aff);
8448 equal = isl_space_is_equal(space1, space2);
8449 isl_space_free(space1);
8450 isl_space_free(space2);
8451 if (equal < 0)
8452 goto error;
8453 if (!equal)
8454 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8455 "spaces don't match", goto error);
8456 if (isl_aff_dim(aff, isl_dim_in) == 0)
8457 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8458 "cannot determine domains", goto error);
8460 return multi_union_pw_aff_apply_aff(mupa, aff);
8461 error:
8462 isl_multi_union_pw_aff_free(mupa);
8463 isl_aff_free(aff);
8464 return NULL;
8467 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8468 * with the domain of "ma".
8469 * Furthermore, the dimension of this space needs to be greater than zero,
8470 * unless the dimension of the target space of "ma" is also zero.
8471 * The result is defined over the shared domain of the elements of "mupa"
8473 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8474 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8476 isl_space *space1, *space2;
8477 isl_multi_union_pw_aff *res;
8478 int equal;
8479 int i, n_out;
8481 mupa = isl_multi_union_pw_aff_align_params(mupa,
8482 isl_multi_aff_get_space(ma));
8483 ma = isl_multi_aff_align_params(ma,
8484 isl_multi_union_pw_aff_get_space(mupa));
8485 if (!mupa || !ma)
8486 goto error;
8488 space1 = isl_multi_union_pw_aff_get_space(mupa);
8489 space2 = isl_multi_aff_get_domain_space(ma);
8490 equal = isl_space_is_equal(space1, space2);
8491 isl_space_free(space1);
8492 isl_space_free(space2);
8493 if (equal < 0)
8494 goto error;
8495 if (!equal)
8496 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8497 "spaces don't match", goto error);
8498 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8499 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8500 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8501 "cannot determine domains", goto error);
8503 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8504 res = isl_multi_union_pw_aff_alloc(space1);
8506 for (i = 0; i < n_out; ++i) {
8507 isl_aff *aff;
8508 isl_union_pw_aff *upa;
8510 aff = isl_multi_aff_get_aff(ma, i);
8511 upa = multi_union_pw_aff_apply_aff(
8512 isl_multi_union_pw_aff_copy(mupa), aff);
8513 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8516 isl_multi_aff_free(ma);
8517 isl_multi_union_pw_aff_free(mupa);
8518 return res;
8519 error:
8520 isl_multi_union_pw_aff_free(mupa);
8521 isl_multi_aff_free(ma);
8522 return NULL;
8525 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8526 * with the domain of "pa".
8527 * Furthermore, the dimension of this space needs to be greater than zero.
8528 * The result is defined over the shared domain of the elements of "mupa"
8530 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8531 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8533 int i;
8534 int equal;
8535 isl_space *space, *space2;
8536 isl_union_pw_aff *upa;
8538 mupa = isl_multi_union_pw_aff_align_params(mupa,
8539 isl_pw_aff_get_space(pa));
8540 pa = isl_pw_aff_align_params(pa,
8541 isl_multi_union_pw_aff_get_space(mupa));
8542 if (!mupa || !pa)
8543 goto error;
8545 space = isl_multi_union_pw_aff_get_space(mupa);
8546 space2 = isl_pw_aff_get_domain_space(pa);
8547 equal = isl_space_is_equal(space, space2);
8548 isl_space_free(space);
8549 isl_space_free(space2);
8550 if (equal < 0)
8551 goto error;
8552 if (!equal)
8553 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8554 "spaces don't match", goto error);
8555 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8556 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8557 "cannot determine domains", goto error);
8559 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8560 upa = isl_union_pw_aff_empty(space);
8562 for (i = 0; i < pa->n; ++i) {
8563 isl_aff *aff;
8564 isl_set *domain;
8565 isl_multi_union_pw_aff *mupa_i;
8566 isl_union_pw_aff *upa_i;
8568 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8569 domain = isl_set_copy(pa->p[i].set);
8570 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8571 aff = isl_aff_copy(pa->p[i].aff);
8572 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8573 upa = isl_union_pw_aff_union_add(upa, upa_i);
8576 isl_multi_union_pw_aff_free(mupa);
8577 isl_pw_aff_free(pa);
8578 return upa;
8579 error:
8580 isl_multi_union_pw_aff_free(mupa);
8581 isl_pw_aff_free(pa);
8582 return NULL;
8585 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8586 * with the domain of "pma".
8587 * Furthermore, the dimension of this space needs to be greater than zero,
8588 * unless the dimension of the target space of "pma" is also zero.
8589 * The result is defined over the shared domain of the elements of "mupa"
8591 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8592 __isl_take isl_multi_union_pw_aff *mupa,
8593 __isl_take isl_pw_multi_aff *pma)
8595 isl_space *space1, *space2;
8596 isl_multi_union_pw_aff *res;
8597 int equal;
8598 int i, n_out;
8600 mupa = isl_multi_union_pw_aff_align_params(mupa,
8601 isl_pw_multi_aff_get_space(pma));
8602 pma = isl_pw_multi_aff_align_params(pma,
8603 isl_multi_union_pw_aff_get_space(mupa));
8604 if (!mupa || !pma)
8605 goto error;
8607 space1 = isl_multi_union_pw_aff_get_space(mupa);
8608 space2 = isl_pw_multi_aff_get_domain_space(pma);
8609 equal = isl_space_is_equal(space1, space2);
8610 isl_space_free(space1);
8611 isl_space_free(space2);
8612 if (equal < 0)
8613 goto error;
8614 if (!equal)
8615 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8616 "spaces don't match", goto error);
8617 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8618 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8619 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8620 "cannot determine domains", goto error);
8622 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8623 res = isl_multi_union_pw_aff_alloc(space1);
8625 for (i = 0; i < n_out; ++i) {
8626 isl_pw_aff *pa;
8627 isl_union_pw_aff *upa;
8629 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8630 upa = isl_multi_union_pw_aff_apply_pw_aff(
8631 isl_multi_union_pw_aff_copy(mupa), pa);
8632 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8635 isl_pw_multi_aff_free(pma);
8636 isl_multi_union_pw_aff_free(mupa);
8637 return res;
8638 error:
8639 isl_multi_union_pw_aff_free(mupa);
8640 isl_pw_multi_aff_free(pma);
8641 return NULL;
8644 /* Compute the pullback of "mupa" by the function represented by "upma".
8645 * In other words, plug in "upma" in "mupa". The result contains
8646 * expressions defined over the domain space of "upma".
8648 * Run over all elements of "mupa" and plug in "upma" in each of them.
8650 __isl_give isl_multi_union_pw_aff *
8651 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8652 __isl_take isl_multi_union_pw_aff *mupa,
8653 __isl_take isl_union_pw_multi_aff *upma)
8655 int i, n;
8657 mupa = isl_multi_union_pw_aff_align_params(mupa,
8658 isl_union_pw_multi_aff_get_space(upma));
8659 upma = isl_union_pw_multi_aff_align_params(upma,
8660 isl_multi_union_pw_aff_get_space(mupa));
8661 if (!mupa || !upma)
8662 goto error;
8664 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8665 for (i = 0; i < n; ++i) {
8666 isl_union_pw_aff *upa;
8668 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8669 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8670 isl_union_pw_multi_aff_copy(upma));
8671 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8674 isl_union_pw_multi_aff_free(upma);
8675 return mupa;
8676 error:
8677 isl_multi_union_pw_aff_free(mupa);
8678 isl_union_pw_multi_aff_free(upma);
8679 return NULL;
8682 /* Extract the sequence of elements in "mupa" with domain space "space"
8683 * (ignoring parameters).
8685 * For the elements of "mupa" that are not defined on the specified space,
8686 * the corresponding element in the result is empty.
8688 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8689 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8691 int i, n;
8692 isl_space *space_mpa = NULL;
8693 isl_multi_pw_aff *mpa;
8695 if (!mupa || !space)
8696 goto error;
8698 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8699 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8700 space = isl_space_drop_dims(space, isl_dim_param,
8701 0, isl_space_dim(space, isl_dim_param));
8702 space = isl_space_align_params(space,
8703 isl_space_copy(space_mpa));
8704 if (!space)
8705 goto error;
8707 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8708 space_mpa);
8709 mpa = isl_multi_pw_aff_alloc(space_mpa);
8711 space = isl_space_from_domain(space);
8712 space = isl_space_add_dims(space, isl_dim_out, 1);
8713 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8714 for (i = 0; i < n; ++i) {
8715 isl_union_pw_aff *upa;
8716 isl_pw_aff *pa;
8718 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8719 pa = isl_union_pw_aff_extract_pw_aff(upa,
8720 isl_space_copy(space));
8721 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8722 isl_union_pw_aff_free(upa);
8725 isl_space_free(space);
8726 return mpa;
8727 error:
8728 isl_space_free(space_mpa);
8729 isl_space_free(space);
8730 return NULL;