isl_polynomial.c: compressed_multiplicative_call: rename "dim" var to "space"
[isl.git] / isl_aff.c
blobd7767ee0f54d017673ede03ff51410d4cf6b4521
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 * As a special case, floor(NaN) = NaN.
1534 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1536 int i;
1537 int size;
1538 isl_ctx *ctx;
1539 isl_vec *div;
1541 if (!aff)
1542 return NULL;
1544 if (isl_aff_is_nan(aff))
1545 return aff;
1546 if (isl_int_is_one(aff->v->el[0]))
1547 return aff;
1549 aff = isl_aff_cow(aff);
1550 if (!aff)
1551 return NULL;
1553 aff->v = isl_vec_cow(aff->v);
1554 if (!aff->v)
1555 return isl_aff_free(aff);
1557 if (isl_aff_is_cst(aff)) {
1558 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1559 isl_int_set_si(aff->v->el[0], 1);
1560 return aff;
1563 div = isl_vec_copy(aff->v);
1564 div = isl_vec_cow(div);
1565 if (!div)
1566 return isl_aff_free(aff);
1568 ctx = isl_aff_get_ctx(aff);
1569 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1570 for (i = 1; i < aff->v->size; ++i) {
1571 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1572 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1573 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1574 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1575 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1579 aff->ls = isl_local_space_add_div(aff->ls, div);
1580 if (!aff->ls)
1581 return isl_aff_free(aff);
1583 size = aff->v->size;
1584 aff->v = isl_vec_extend(aff->v, size + 1);
1585 if (!aff->v)
1586 return isl_aff_free(aff);
1587 isl_int_set_si(aff->v->el[0], 1);
1588 isl_int_set_si(aff->v->el[size], 1);
1590 aff = isl_aff_normalize(aff);
1592 return aff;
1595 /* Compute
1597 * aff mod m = aff - m * floor(aff/m)
1599 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1601 isl_aff *res;
1603 res = isl_aff_copy(aff);
1604 aff = isl_aff_scale_down(aff, m);
1605 aff = isl_aff_floor(aff);
1606 aff = isl_aff_scale(aff, m);
1607 res = isl_aff_sub(res, aff);
1609 return res;
1612 /* Compute
1614 * aff mod m = aff - m * floor(aff/m)
1616 * with m an integer value.
1618 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1619 __isl_take isl_val *m)
1621 isl_aff *res;
1623 if (!aff || !m)
1624 goto error;
1626 if (!isl_val_is_int(m))
1627 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1628 "expecting integer modulo", goto error);
1630 res = isl_aff_copy(aff);
1631 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1632 aff = isl_aff_floor(aff);
1633 aff = isl_aff_scale_val(aff, m);
1634 res = isl_aff_sub(res, aff);
1636 return res;
1637 error:
1638 isl_aff_free(aff);
1639 isl_val_free(m);
1640 return NULL;
1643 /* Compute
1645 * pwaff mod m = pwaff - m * floor(pwaff/m)
1647 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1649 isl_pw_aff *res;
1651 res = isl_pw_aff_copy(pwaff);
1652 pwaff = isl_pw_aff_scale_down(pwaff, m);
1653 pwaff = isl_pw_aff_floor(pwaff);
1654 pwaff = isl_pw_aff_scale(pwaff, m);
1655 res = isl_pw_aff_sub(res, pwaff);
1657 return res;
1660 /* Compute
1662 * pa mod m = pa - m * floor(pa/m)
1664 * with m an integer value.
1666 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1667 __isl_take isl_val *m)
1669 if (!pa || !m)
1670 goto error;
1671 if (!isl_val_is_int(m))
1672 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1673 "expecting integer modulo", goto error);
1674 pa = isl_pw_aff_mod(pa, m->n);
1675 isl_val_free(m);
1676 return pa;
1677 error:
1678 isl_pw_aff_free(pa);
1679 isl_val_free(m);
1680 return NULL;
1683 /* Given f, return ceil(f).
1684 * If f is an integer expression, then just return f.
1685 * Otherwise, let f be the expression
1687 * e/m
1689 * then return
1691 * floor((e + m - 1)/m)
1693 * As a special case, ceil(NaN) = NaN.
1695 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1697 if (!aff)
1698 return NULL;
1700 if (isl_aff_is_nan(aff))
1701 return aff;
1702 if (isl_int_is_one(aff->v->el[0]))
1703 return aff;
1705 aff = isl_aff_cow(aff);
1706 if (!aff)
1707 return NULL;
1708 aff->v = isl_vec_cow(aff->v);
1709 if (!aff->v)
1710 return isl_aff_free(aff);
1712 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1713 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1714 aff = isl_aff_floor(aff);
1716 return aff;
1719 /* Apply the expansion computed by isl_merge_divs.
1720 * The expansion itself is given by "exp" while the resulting
1721 * list of divs is given by "div".
1723 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1724 __isl_take isl_mat *div, int *exp)
1726 int old_n_div;
1727 int new_n_div;
1728 int offset;
1730 aff = isl_aff_cow(aff);
1731 if (!aff || !div)
1732 goto error;
1734 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1735 new_n_div = isl_mat_rows(div);
1736 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1738 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1739 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1740 if (!aff->v || !aff->ls)
1741 return isl_aff_free(aff);
1742 return aff;
1743 error:
1744 isl_aff_free(aff);
1745 isl_mat_free(div);
1746 return NULL;
1749 /* Add two affine expressions that live in the same local space.
1751 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1752 __isl_take isl_aff *aff2)
1754 isl_int gcd, f;
1756 aff1 = isl_aff_cow(aff1);
1757 if (!aff1 || !aff2)
1758 goto error;
1760 aff1->v = isl_vec_cow(aff1->v);
1761 if (!aff1->v)
1762 goto error;
1764 isl_int_init(gcd);
1765 isl_int_init(f);
1766 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1767 isl_int_divexact(f, aff2->v->el[0], gcd);
1768 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1769 isl_int_divexact(f, aff1->v->el[0], gcd);
1770 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1771 isl_int_divexact(f, aff2->v->el[0], gcd);
1772 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1773 isl_int_clear(f);
1774 isl_int_clear(gcd);
1776 isl_aff_free(aff2);
1777 return aff1;
1778 error:
1779 isl_aff_free(aff1);
1780 isl_aff_free(aff2);
1781 return NULL;
1784 /* Return the sum of "aff1" and "aff2".
1786 * If either of the two is NaN, then the result is NaN.
1788 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1789 __isl_take isl_aff *aff2)
1791 isl_ctx *ctx;
1792 int *exp1 = NULL;
1793 int *exp2 = NULL;
1794 isl_mat *div;
1795 int n_div1, n_div2;
1797 if (!aff1 || !aff2)
1798 goto error;
1800 ctx = isl_aff_get_ctx(aff1);
1801 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1802 isl_die(ctx, isl_error_invalid,
1803 "spaces don't match", goto error);
1805 if (isl_aff_is_nan(aff1)) {
1806 isl_aff_free(aff2);
1807 return aff1;
1809 if (isl_aff_is_nan(aff2)) {
1810 isl_aff_free(aff1);
1811 return aff2;
1814 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1815 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1816 if (n_div1 == 0 && n_div2 == 0)
1817 return add_expanded(aff1, aff2);
1819 exp1 = isl_alloc_array(ctx, int, n_div1);
1820 exp2 = isl_alloc_array(ctx, int, n_div2);
1821 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1822 goto error;
1824 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1825 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1826 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1827 free(exp1);
1828 free(exp2);
1830 return add_expanded(aff1, aff2);
1831 error:
1832 free(exp1);
1833 free(exp2);
1834 isl_aff_free(aff1);
1835 isl_aff_free(aff2);
1836 return NULL;
1839 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1840 __isl_take isl_aff *aff2)
1842 return isl_aff_add(aff1, isl_aff_neg(aff2));
1845 /* Return the result of scaling "aff" by a factor of "f".
1847 * As a special case, f * NaN = NaN.
1849 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1851 isl_int gcd;
1853 if (!aff)
1854 return NULL;
1855 if (isl_aff_is_nan(aff))
1856 return aff;
1858 if (isl_int_is_one(f))
1859 return aff;
1861 aff = isl_aff_cow(aff);
1862 if (!aff)
1863 return NULL;
1864 aff->v = isl_vec_cow(aff->v);
1865 if (!aff->v)
1866 return isl_aff_free(aff);
1868 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1869 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1870 return aff;
1873 isl_int_init(gcd);
1874 isl_int_gcd(gcd, aff->v->el[0], f);
1875 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1876 isl_int_divexact(gcd, f, gcd);
1877 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1878 isl_int_clear(gcd);
1880 return aff;
1883 /* Multiple "aff" by "v".
1885 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1886 __isl_take isl_val *v)
1888 if (!aff || !v)
1889 goto error;
1891 if (isl_val_is_one(v)) {
1892 isl_val_free(v);
1893 return aff;
1896 if (!isl_val_is_rat(v))
1897 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1898 "expecting rational factor", goto error);
1900 aff = isl_aff_scale(aff, v->n);
1901 aff = isl_aff_scale_down(aff, v->d);
1903 isl_val_free(v);
1904 return aff;
1905 error:
1906 isl_aff_free(aff);
1907 isl_val_free(v);
1908 return NULL;
1911 /* Return the result of scaling "aff" down by a factor of "f".
1913 * As a special case, NaN/f = NaN.
1915 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1917 isl_int gcd;
1919 if (!aff)
1920 return NULL;
1921 if (isl_aff_is_nan(aff))
1922 return aff;
1924 if (isl_int_is_one(f))
1925 return aff;
1927 aff = isl_aff_cow(aff);
1928 if (!aff)
1929 return NULL;
1931 if (isl_int_is_zero(f))
1932 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1933 "cannot scale down by zero", return isl_aff_free(aff));
1935 aff->v = isl_vec_cow(aff->v);
1936 if (!aff->v)
1937 return isl_aff_free(aff);
1939 isl_int_init(gcd);
1940 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1941 isl_int_gcd(gcd, gcd, f);
1942 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1943 isl_int_divexact(gcd, f, gcd);
1944 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1945 isl_int_clear(gcd);
1947 return aff;
1950 /* Divide "aff" by "v".
1952 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1953 __isl_take isl_val *v)
1955 if (!aff || !v)
1956 goto error;
1958 if (isl_val_is_one(v)) {
1959 isl_val_free(v);
1960 return aff;
1963 if (!isl_val_is_rat(v))
1964 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1965 "expecting rational factor", goto error);
1966 if (!isl_val_is_pos(v))
1967 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1968 "factor needs to be positive", goto error);
1970 aff = isl_aff_scale(aff, v->d);
1971 aff = isl_aff_scale_down(aff, v->n);
1973 isl_val_free(v);
1974 return aff;
1975 error:
1976 isl_aff_free(aff);
1977 isl_val_free(v);
1978 return NULL;
1981 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1983 isl_int v;
1985 if (f == 1)
1986 return aff;
1988 isl_int_init(v);
1989 isl_int_set_ui(v, f);
1990 aff = isl_aff_scale_down(aff, v);
1991 isl_int_clear(v);
1993 return aff;
1996 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1997 enum isl_dim_type type, unsigned pos, const char *s)
1999 aff = isl_aff_cow(aff);
2000 if (!aff)
2001 return NULL;
2002 if (type == isl_dim_out)
2003 isl_die(aff->v->ctx, isl_error_invalid,
2004 "cannot set name of output/set dimension",
2005 return isl_aff_free(aff));
2006 if (type == isl_dim_in)
2007 type = isl_dim_set;
2008 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2009 if (!aff->ls)
2010 return isl_aff_free(aff);
2012 return aff;
2015 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2016 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2018 aff = isl_aff_cow(aff);
2019 if (!aff)
2020 goto error;
2021 if (type == isl_dim_out)
2022 isl_die(aff->v->ctx, isl_error_invalid,
2023 "cannot set name of output/set dimension",
2024 goto error);
2025 if (type == isl_dim_in)
2026 type = isl_dim_set;
2027 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2028 if (!aff->ls)
2029 return isl_aff_free(aff);
2031 return aff;
2032 error:
2033 isl_id_free(id);
2034 isl_aff_free(aff);
2035 return NULL;
2038 /* Replace the identifier of the input tuple of "aff" by "id".
2039 * type is currently required to be equal to isl_dim_in
2041 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2042 enum isl_dim_type type, __isl_take isl_id *id)
2044 aff = isl_aff_cow(aff);
2045 if (!aff)
2046 goto error;
2047 if (type != isl_dim_out)
2048 isl_die(aff->v->ctx, isl_error_invalid,
2049 "cannot only set id of input tuple", goto error);
2050 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2051 if (!aff->ls)
2052 return isl_aff_free(aff);
2054 return aff;
2055 error:
2056 isl_id_free(id);
2057 isl_aff_free(aff);
2058 return NULL;
2061 /* Exploit the equalities in "eq" to simplify the affine expression
2062 * and the expressions of the integer divisions in the local space.
2063 * The integer divisions in this local space are assumed to appear
2064 * as regular dimensions in "eq".
2066 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2067 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2069 int i, j;
2070 unsigned total;
2071 unsigned n_div;
2073 if (!eq)
2074 goto error;
2075 if (eq->n_eq == 0) {
2076 isl_basic_set_free(eq);
2077 return aff;
2080 aff = isl_aff_cow(aff);
2081 if (!aff)
2082 goto error;
2084 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2085 isl_basic_set_copy(eq));
2086 aff->v = isl_vec_cow(aff->v);
2087 if (!aff->ls || !aff->v)
2088 goto error;
2090 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2091 n_div = eq->n_div;
2092 for (i = 0; i < eq->n_eq; ++i) {
2093 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2094 if (j < 0 || j == 0 || j >= total)
2095 continue;
2097 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2098 &aff->v->el[0]);
2101 isl_basic_set_free(eq);
2102 aff = isl_aff_normalize(aff);
2103 return aff;
2104 error:
2105 isl_basic_set_free(eq);
2106 isl_aff_free(aff);
2107 return NULL;
2110 /* Exploit the equalities in "eq" to simplify the affine expression
2111 * and the expressions of the integer divisions in the local space.
2113 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2114 __isl_take isl_basic_set *eq)
2116 int n_div;
2118 if (!aff || !eq)
2119 goto error;
2120 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2121 if (n_div > 0)
2122 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2123 return isl_aff_substitute_equalities_lifted(aff, eq);
2124 error:
2125 isl_basic_set_free(eq);
2126 isl_aff_free(aff);
2127 return NULL;
2130 /* Look for equalities among the variables shared by context and aff
2131 * and the integer divisions of aff, if any.
2132 * The equalities are then used to eliminate coefficients and/or integer
2133 * divisions from aff.
2135 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2136 __isl_take isl_set *context)
2138 isl_basic_set *hull;
2139 int n_div;
2141 if (!aff)
2142 goto error;
2143 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2144 if (n_div > 0) {
2145 isl_basic_set *bset;
2146 isl_local_space *ls;
2147 context = isl_set_add_dims(context, isl_dim_set, n_div);
2148 ls = isl_aff_get_domain_local_space(aff);
2149 bset = isl_basic_set_from_local_space(ls);
2150 bset = isl_basic_set_lift(bset);
2151 bset = isl_basic_set_flatten(bset);
2152 context = isl_set_intersect(context,
2153 isl_set_from_basic_set(bset));
2156 hull = isl_set_affine_hull(context);
2157 return isl_aff_substitute_equalities_lifted(aff, hull);
2158 error:
2159 isl_aff_free(aff);
2160 isl_set_free(context);
2161 return NULL;
2164 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2165 __isl_take isl_set *context)
2167 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2168 dom_context = isl_set_intersect_params(dom_context, context);
2169 return isl_aff_gist(aff, dom_context);
2172 /* Return a basic set containing those elements in the space
2173 * of aff where it is positive. "rational" should not be set.
2175 * If "aff" is NaN, then it is not positive.
2177 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2178 int rational)
2180 isl_constraint *ineq;
2181 isl_basic_set *bset;
2182 isl_val *c;
2184 if (!aff)
2185 return NULL;
2186 if (isl_aff_is_nan(aff)) {
2187 isl_space *space = isl_aff_get_domain_space(aff);
2188 isl_aff_free(aff);
2189 return isl_basic_set_empty(space);
2191 if (rational)
2192 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2193 "rational sets not supported", goto error);
2195 ineq = isl_inequality_from_aff(aff);
2196 c = isl_constraint_get_constant_val(ineq);
2197 c = isl_val_sub_ui(c, 1);
2198 ineq = isl_constraint_set_constant_val(ineq, c);
2200 bset = isl_basic_set_from_constraint(ineq);
2201 bset = isl_basic_set_simplify(bset);
2202 return bset;
2203 error:
2204 isl_aff_free(aff);
2205 return NULL;
2208 /* Return a basic set containing those elements in the space
2209 * of aff where it is non-negative.
2210 * If "rational" is set, then return a rational basic set.
2212 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2214 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2215 __isl_take isl_aff *aff, int rational)
2217 isl_constraint *ineq;
2218 isl_basic_set *bset;
2220 if (!aff)
2221 return NULL;
2222 if (isl_aff_is_nan(aff)) {
2223 isl_space *space = isl_aff_get_domain_space(aff);
2224 isl_aff_free(aff);
2225 return isl_basic_set_empty(space);
2228 ineq = isl_inequality_from_aff(aff);
2230 bset = isl_basic_set_from_constraint(ineq);
2231 if (rational)
2232 bset = isl_basic_set_set_rational(bset);
2233 bset = isl_basic_set_simplify(bset);
2234 return bset;
2237 /* Return a basic set containing those elements in the space
2238 * of aff where it is non-negative.
2240 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2242 return aff_nonneg_basic_set(aff, 0);
2245 /* Return a basic set containing those elements in the domain space
2246 * of aff where it is negative.
2248 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2250 aff = isl_aff_neg(aff);
2251 aff = isl_aff_add_constant_num_si(aff, -1);
2252 return isl_aff_nonneg_basic_set(aff);
2255 /* Return a basic set containing those elements in the space
2256 * of aff where it is zero.
2257 * If "rational" is set, then return a rational basic set.
2259 * If "aff" is NaN, then it is not zero.
2261 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2262 int rational)
2264 isl_constraint *ineq;
2265 isl_basic_set *bset;
2267 if (!aff)
2268 return NULL;
2269 if (isl_aff_is_nan(aff)) {
2270 isl_space *space = isl_aff_get_domain_space(aff);
2271 isl_aff_free(aff);
2272 return isl_basic_set_empty(space);
2275 ineq = isl_equality_from_aff(aff);
2277 bset = isl_basic_set_from_constraint(ineq);
2278 if (rational)
2279 bset = isl_basic_set_set_rational(bset);
2280 bset = isl_basic_set_simplify(bset);
2281 return bset;
2284 /* Return a basic set containing those elements in the space
2285 * of aff where it is zero.
2287 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2289 return aff_zero_basic_set(aff, 0);
2292 /* Return a basic set containing those elements in the shared space
2293 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2295 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2296 __isl_take isl_aff *aff2)
2298 aff1 = isl_aff_sub(aff1, aff2);
2300 return isl_aff_nonneg_basic_set(aff1);
2303 /* Return a set containing those elements in the shared space
2304 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2306 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2307 __isl_take isl_aff *aff2)
2309 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2312 /* Return a basic set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2315 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2316 __isl_take isl_aff *aff2)
2318 return isl_aff_ge_basic_set(aff2, aff1);
2321 /* Return a set containing those elements in the shared space
2322 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2324 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2325 __isl_take isl_aff *aff2)
2327 return isl_aff_ge_set(aff2, aff1);
2330 /* Return a basic set containing those elements in the shared space
2331 * of aff1 and aff2 where aff1 and aff2 are equal.
2333 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2334 __isl_take isl_aff *aff2)
2336 aff1 = isl_aff_sub(aff1, aff2);
2338 return isl_aff_zero_basic_set(aff1);
2341 /* Return a set containing those elements in the shared space
2342 * of aff1 and aff2 where aff1 and aff2 are equal.
2344 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2345 __isl_take isl_aff *aff2)
2347 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2350 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2351 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2353 aff1 = isl_aff_add(aff1, aff2);
2354 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2355 return aff1;
2358 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2360 if (!aff)
2361 return -1;
2363 return 0;
2366 /* Check whether the given affine expression has non-zero coefficient
2367 * for any dimension in the given range or if any of these dimensions
2368 * appear with non-zero coefficients in any of the integer divisions
2369 * involved in the affine expression.
2371 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2372 enum isl_dim_type type, unsigned first, unsigned n)
2374 int i;
2375 isl_ctx *ctx;
2376 int *active = NULL;
2377 isl_bool involves = isl_bool_false;
2379 if (!aff)
2380 return isl_bool_error;
2381 if (n == 0)
2382 return isl_bool_false;
2384 ctx = isl_aff_get_ctx(aff);
2385 if (first + n > isl_aff_dim(aff, type))
2386 isl_die(ctx, isl_error_invalid,
2387 "range out of bounds", return isl_bool_error);
2389 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2390 if (!active)
2391 goto error;
2393 first += isl_local_space_offset(aff->ls, type) - 1;
2394 for (i = 0; i < n; ++i)
2395 if (active[first + i]) {
2396 involves = isl_bool_true;
2397 break;
2400 free(active);
2402 return involves;
2403 error:
2404 free(active);
2405 return isl_bool_error;
2408 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2409 enum isl_dim_type type, unsigned first, unsigned n)
2411 isl_ctx *ctx;
2413 if (!aff)
2414 return NULL;
2415 if (type == isl_dim_out)
2416 isl_die(aff->v->ctx, isl_error_invalid,
2417 "cannot drop output/set dimension",
2418 return isl_aff_free(aff));
2419 if (type == isl_dim_in)
2420 type = isl_dim_set;
2421 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2422 return aff;
2424 ctx = isl_aff_get_ctx(aff);
2425 if (first + n > isl_local_space_dim(aff->ls, type))
2426 isl_die(ctx, isl_error_invalid, "range out of bounds",
2427 return isl_aff_free(aff));
2429 aff = isl_aff_cow(aff);
2430 if (!aff)
2431 return NULL;
2433 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2434 if (!aff->ls)
2435 return isl_aff_free(aff);
2437 first += 1 + isl_local_space_offset(aff->ls, type);
2438 aff->v = isl_vec_drop_els(aff->v, first, n);
2439 if (!aff->v)
2440 return isl_aff_free(aff);
2442 return aff;
2445 /* Project the domain of the affine expression onto its parameter space.
2446 * The affine expression may not involve any of the domain dimensions.
2448 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2450 isl_space *space;
2451 unsigned n;
2452 int involves;
2454 n = isl_aff_dim(aff, isl_dim_in);
2455 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2456 if (involves < 0)
2457 return isl_aff_free(aff);
2458 if (involves)
2459 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2460 "affine expression involves some of the domain dimensions",
2461 return isl_aff_free(aff));
2462 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2463 space = isl_aff_get_domain_space(aff);
2464 space = isl_space_params(space);
2465 aff = isl_aff_reset_domain_space(aff, space);
2466 return aff;
2469 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2470 enum isl_dim_type type, unsigned first, unsigned n)
2472 isl_ctx *ctx;
2474 if (!aff)
2475 return NULL;
2476 if (type == isl_dim_out)
2477 isl_die(aff->v->ctx, isl_error_invalid,
2478 "cannot insert output/set dimensions",
2479 return isl_aff_free(aff));
2480 if (type == isl_dim_in)
2481 type = isl_dim_set;
2482 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2483 return aff;
2485 ctx = isl_aff_get_ctx(aff);
2486 if (first > isl_local_space_dim(aff->ls, type))
2487 isl_die(ctx, isl_error_invalid, "position out of bounds",
2488 return isl_aff_free(aff));
2490 aff = isl_aff_cow(aff);
2491 if (!aff)
2492 return NULL;
2494 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2495 if (!aff->ls)
2496 return isl_aff_free(aff);
2498 first += 1 + isl_local_space_offset(aff->ls, type);
2499 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2500 if (!aff->v)
2501 return isl_aff_free(aff);
2503 return aff;
2506 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2507 enum isl_dim_type type, unsigned n)
2509 unsigned pos;
2511 pos = isl_aff_dim(aff, type);
2513 return isl_aff_insert_dims(aff, type, pos, n);
2516 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2517 enum isl_dim_type type, unsigned n)
2519 unsigned pos;
2521 pos = isl_pw_aff_dim(pwaff, type);
2523 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2526 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2527 * to dimensions of "dst_type" at "dst_pos".
2529 * We only support moving input dimensions to parameters and vice versa.
2531 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2532 enum isl_dim_type dst_type, unsigned dst_pos,
2533 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2535 unsigned g_dst_pos;
2536 unsigned g_src_pos;
2538 if (!aff)
2539 return NULL;
2540 if (n == 0 &&
2541 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2542 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2543 return aff;
2545 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2546 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2547 "cannot move output/set dimension",
2548 return isl_aff_free(aff));
2549 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2550 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2551 "cannot move divs", return isl_aff_free(aff));
2552 if (dst_type == isl_dim_in)
2553 dst_type = isl_dim_set;
2554 if (src_type == isl_dim_in)
2555 src_type = isl_dim_set;
2557 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2558 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2559 "range out of bounds", return isl_aff_free(aff));
2560 if (dst_type == src_type)
2561 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2562 "moving dims within the same type not supported",
2563 return isl_aff_free(aff));
2565 aff = isl_aff_cow(aff);
2566 if (!aff)
2567 return NULL;
2569 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2570 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2571 if (dst_type > src_type)
2572 g_dst_pos -= n;
2574 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2575 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2576 src_type, src_pos, n);
2577 if (!aff->v || !aff->ls)
2578 return isl_aff_free(aff);
2580 aff = sort_divs(aff);
2582 return aff;
2585 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2587 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2588 return isl_pw_aff_alloc(dom, aff);
2591 #undef PW
2592 #define PW isl_pw_aff
2593 #undef EL
2594 #define EL isl_aff
2595 #undef EL_IS_ZERO
2596 #define EL_IS_ZERO is_empty
2597 #undef ZERO
2598 #define ZERO empty
2599 #undef IS_ZERO
2600 #define IS_ZERO is_empty
2601 #undef FIELD
2602 #define FIELD aff
2603 #undef DEFAULT_IS_ZERO
2604 #define DEFAULT_IS_ZERO 0
2606 #define NO_EVAL
2607 #define NO_OPT
2608 #define NO_LIFT
2609 #define NO_MORPH
2611 #include <isl_pw_templ.c>
2612 #include <isl_pw_hash.c>
2613 #include <isl_pw_union_opt.c>
2615 #undef UNION
2616 #define UNION isl_union_pw_aff
2617 #undef PART
2618 #define PART isl_pw_aff
2619 #undef PARTS
2620 #define PARTS pw_aff
2622 #include <isl_union_single.c>
2623 #include <isl_union_neg.c>
2625 static __isl_give isl_set *align_params_pw_pw_set_and(
2626 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2627 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2628 __isl_take isl_pw_aff *pwaff2))
2630 if (!pwaff1 || !pwaff2)
2631 goto error;
2632 if (isl_space_match(pwaff1->dim, isl_dim_param,
2633 pwaff2->dim, isl_dim_param))
2634 return fn(pwaff1, pwaff2);
2635 if (!isl_space_has_named_params(pwaff1->dim) ||
2636 !isl_space_has_named_params(pwaff2->dim))
2637 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2638 "unaligned unnamed parameters", goto error);
2639 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2640 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2641 return fn(pwaff1, pwaff2);
2642 error:
2643 isl_pw_aff_free(pwaff1);
2644 isl_pw_aff_free(pwaff2);
2645 return NULL;
2648 /* Align the parameters of the to isl_pw_aff arguments and
2649 * then apply a function "fn" on them that returns an isl_map.
2651 static __isl_give isl_map *align_params_pw_pw_map_and(
2652 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2653 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2654 __isl_take isl_pw_aff *pa2))
2656 if (!pa1 || !pa2)
2657 goto error;
2658 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2659 return fn(pa1, pa2);
2660 if (!isl_space_has_named_params(pa1->dim) ||
2661 !isl_space_has_named_params(pa2->dim))
2662 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2663 "unaligned unnamed parameters", goto error);
2664 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2665 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2666 return fn(pa1, pa2);
2667 error:
2668 isl_pw_aff_free(pa1);
2669 isl_pw_aff_free(pa2);
2670 return NULL;
2673 /* Compute a piecewise quasi-affine expression with a domain that
2674 * is the union of those of pwaff1 and pwaff2 and such that on each
2675 * cell, the quasi-affine expression is the maximum of those of pwaff1
2676 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2677 * cell, then the associated expression is the defined one.
2679 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2680 __isl_take isl_pw_aff *pwaff2)
2682 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2685 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2686 __isl_take isl_pw_aff *pwaff2)
2688 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2689 &pw_aff_union_max);
2692 /* Compute a piecewise quasi-affine expression with a domain that
2693 * is the union of those of pwaff1 and pwaff2 and such that on each
2694 * cell, the quasi-affine expression is the minimum of those of pwaff1
2695 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2696 * cell, then the associated expression is the defined one.
2698 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2699 __isl_take isl_pw_aff *pwaff2)
2701 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2704 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2705 __isl_take isl_pw_aff *pwaff2)
2707 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2708 &pw_aff_union_min);
2711 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2712 __isl_take isl_pw_aff *pwaff2, int max)
2714 if (max)
2715 return isl_pw_aff_union_max(pwaff1, pwaff2);
2716 else
2717 return isl_pw_aff_union_min(pwaff1, pwaff2);
2720 /* Construct a map with as domain the domain of pwaff and
2721 * one-dimensional range corresponding to the affine expressions.
2723 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2725 int i;
2726 isl_space *dim;
2727 isl_map *map;
2729 if (!pwaff)
2730 return NULL;
2732 dim = isl_pw_aff_get_space(pwaff);
2733 map = isl_map_empty(dim);
2735 for (i = 0; i < pwaff->n; ++i) {
2736 isl_basic_map *bmap;
2737 isl_map *map_i;
2739 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2740 map_i = isl_map_from_basic_map(bmap);
2741 map_i = isl_map_intersect_domain(map_i,
2742 isl_set_copy(pwaff->p[i].set));
2743 map = isl_map_union_disjoint(map, map_i);
2746 isl_pw_aff_free(pwaff);
2748 return map;
2751 /* Construct a map with as domain the domain of pwaff and
2752 * one-dimensional range corresponding to the affine expressions.
2754 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2756 if (!pwaff)
2757 return NULL;
2758 if (isl_space_is_set(pwaff->dim))
2759 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2760 "space of input is not a map", goto error);
2761 return map_from_pw_aff(pwaff);
2762 error:
2763 isl_pw_aff_free(pwaff);
2764 return NULL;
2767 /* Construct a one-dimensional set with as parameter domain
2768 * the domain of pwaff and the single set dimension
2769 * corresponding to the affine expressions.
2771 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2773 if (!pwaff)
2774 return NULL;
2775 if (!isl_space_is_set(pwaff->dim))
2776 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2777 "space of input is not a set", goto error);
2778 return map_from_pw_aff(pwaff);
2779 error:
2780 isl_pw_aff_free(pwaff);
2781 return NULL;
2784 /* Return a set containing those elements in the domain
2785 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2786 * does not satisfy "fn" (if complement is 1).
2788 * The pieces with a NaN never belong to the result since
2789 * NaN does not satisfy any property.
2791 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2792 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2793 int complement)
2795 int i;
2796 isl_set *set;
2798 if (!pwaff)
2799 return NULL;
2801 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2803 for (i = 0; i < pwaff->n; ++i) {
2804 isl_basic_set *bset;
2805 isl_set *set_i, *locus;
2806 int rational;
2808 if (isl_aff_is_nan(pwaff->p[i].aff))
2809 continue;
2811 rational = isl_set_has_rational(pwaff->p[i].set);
2812 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2813 locus = isl_set_from_basic_set(bset);
2814 set_i = isl_set_copy(pwaff->p[i].set);
2815 if (complement)
2816 set_i = isl_set_subtract(set_i, locus);
2817 else
2818 set_i = isl_set_intersect(set_i, locus);
2819 set = isl_set_union_disjoint(set, set_i);
2822 isl_pw_aff_free(pwaff);
2824 return set;
2827 /* Return a set containing those elements in the domain
2828 * of "pa" where it is positive.
2830 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2832 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2835 /* Return a set containing those elements in the domain
2836 * of pwaff where it is non-negative.
2838 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2840 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2843 /* Return a set containing those elements in the domain
2844 * of pwaff where it is zero.
2846 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2848 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2851 /* Return a set containing those elements in the domain
2852 * of pwaff where it is not zero.
2854 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2856 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2859 /* Return a set containing those elements in the shared domain
2860 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2862 * We compute the difference on the shared domain and then construct
2863 * the set of values where this difference is non-negative.
2864 * If strict is set, we first subtract 1 from the difference.
2865 * If equal is set, we only return the elements where pwaff1 and pwaff2
2866 * are equal.
2868 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2869 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2871 isl_set *set1, *set2;
2873 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2874 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2875 set1 = isl_set_intersect(set1, set2);
2876 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2877 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2878 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2880 if (strict) {
2881 isl_space *dim = isl_set_get_space(set1);
2882 isl_aff *aff;
2883 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2884 aff = isl_aff_add_constant_si(aff, -1);
2885 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2886 } else
2887 isl_set_free(set1);
2889 if (equal)
2890 return isl_pw_aff_zero_set(pwaff1);
2891 return isl_pw_aff_nonneg_set(pwaff1);
2894 /* Return a set containing those elements in the shared domain
2895 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2897 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2898 __isl_take isl_pw_aff *pwaff2)
2900 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2903 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2904 __isl_take isl_pw_aff *pwaff2)
2906 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2909 /* Return a set containing those elements in the shared domain
2910 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2912 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2913 __isl_take isl_pw_aff *pwaff2)
2915 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2918 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2919 __isl_take isl_pw_aff *pwaff2)
2921 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2924 /* Return a set containing those elements in the shared domain
2925 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2927 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2928 __isl_take isl_pw_aff *pwaff2)
2930 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2933 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2934 __isl_take isl_pw_aff *pwaff2)
2936 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2939 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2940 __isl_take isl_pw_aff *pwaff2)
2942 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2945 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2946 __isl_take isl_pw_aff *pwaff2)
2948 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2951 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2952 * where the function values are ordered in the same way as "order",
2953 * which returns a set in the shared domain of its two arguments.
2954 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2956 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2957 * We first pull back the two functions such that they are defined on
2958 * the domain [A -> B]. Then we apply "order", resulting in a set
2959 * in the space [A -> B]. Finally, we unwrap this set to obtain
2960 * a map in the space A -> B.
2962 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2963 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2964 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2965 __isl_take isl_pw_aff *pa2))
2967 isl_space *space1, *space2;
2968 isl_multi_aff *ma;
2969 isl_set *set;
2971 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2972 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2973 space1 = isl_space_map_from_domain_and_range(space1, space2);
2974 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2975 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2976 ma = isl_multi_aff_range_map(space1);
2977 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2978 set = order(pa1, pa2);
2980 return isl_set_unwrap(set);
2983 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2984 * where the function values are equal.
2985 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2987 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2988 __isl_take isl_pw_aff *pa2)
2990 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2993 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2994 * where the function values are equal.
2996 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2997 __isl_take isl_pw_aff *pa2)
2999 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3002 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3003 * where the function value of "pa1" is less than the function value of "pa2".
3004 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3006 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3007 __isl_take isl_pw_aff *pa2)
3009 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3012 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3013 * where the function value of "pa1" is less than the function value of "pa2".
3015 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3016 __isl_take isl_pw_aff *pa2)
3018 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3021 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3022 * where the function value of "pa1" is greater than the function value
3023 * of "pa2".
3024 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3026 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3027 __isl_take isl_pw_aff *pa2)
3029 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3032 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3033 * where the function value of "pa1" is greater than the function value
3034 * of "pa2".
3036 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3037 __isl_take isl_pw_aff *pa2)
3039 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3042 /* Return a set containing those elements in the shared domain
3043 * of the elements of list1 and list2 where each element in list1
3044 * has the relation specified by "fn" with each element in list2.
3046 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3047 __isl_take isl_pw_aff_list *list2,
3048 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3049 __isl_take isl_pw_aff *pwaff2))
3051 int i, j;
3052 isl_ctx *ctx;
3053 isl_set *set;
3055 if (!list1 || !list2)
3056 goto error;
3058 ctx = isl_pw_aff_list_get_ctx(list1);
3059 if (list1->n < 1 || list2->n < 1)
3060 isl_die(ctx, isl_error_invalid,
3061 "list should contain at least one element", goto error);
3063 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3064 for (i = 0; i < list1->n; ++i)
3065 for (j = 0; j < list2->n; ++j) {
3066 isl_set *set_ij;
3068 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3069 isl_pw_aff_copy(list2->p[j]));
3070 set = isl_set_intersect(set, set_ij);
3073 isl_pw_aff_list_free(list1);
3074 isl_pw_aff_list_free(list2);
3075 return set;
3076 error:
3077 isl_pw_aff_list_free(list1);
3078 isl_pw_aff_list_free(list2);
3079 return NULL;
3082 /* Return a set containing those elements in the shared domain
3083 * of the elements of list1 and list2 where each element in list1
3084 * is equal to each element in list2.
3086 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3087 __isl_take isl_pw_aff_list *list2)
3089 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3092 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3093 __isl_take isl_pw_aff_list *list2)
3095 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3098 /* Return a set containing those elements in the shared domain
3099 * of the elements of list1 and list2 where each element in list1
3100 * is less than or equal to each element in list2.
3102 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3103 __isl_take isl_pw_aff_list *list2)
3105 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3108 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3109 __isl_take isl_pw_aff_list *list2)
3111 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3114 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3115 __isl_take isl_pw_aff_list *list2)
3117 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3120 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3121 __isl_take isl_pw_aff_list *list2)
3123 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3127 /* Return a set containing those elements in the shared domain
3128 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3130 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3131 __isl_take isl_pw_aff *pwaff2)
3133 isl_set *set_lt, *set_gt;
3135 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3136 isl_pw_aff_copy(pwaff2));
3137 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3138 return isl_set_union_disjoint(set_lt, set_gt);
3141 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3142 __isl_take isl_pw_aff *pwaff2)
3144 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3147 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3148 isl_int v)
3150 int i;
3152 if (isl_int_is_one(v))
3153 return pwaff;
3154 if (!isl_int_is_pos(v))
3155 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3156 "factor needs to be positive",
3157 return isl_pw_aff_free(pwaff));
3158 pwaff = isl_pw_aff_cow(pwaff);
3159 if (!pwaff)
3160 return NULL;
3161 if (pwaff->n == 0)
3162 return pwaff;
3164 for (i = 0; i < pwaff->n; ++i) {
3165 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3166 if (!pwaff->p[i].aff)
3167 return isl_pw_aff_free(pwaff);
3170 return pwaff;
3173 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3175 int i;
3177 pwaff = isl_pw_aff_cow(pwaff);
3178 if (!pwaff)
3179 return NULL;
3180 if (pwaff->n == 0)
3181 return pwaff;
3183 for (i = 0; i < pwaff->n; ++i) {
3184 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3185 if (!pwaff->p[i].aff)
3186 return isl_pw_aff_free(pwaff);
3189 return pwaff;
3192 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3194 int i;
3196 pwaff = isl_pw_aff_cow(pwaff);
3197 if (!pwaff)
3198 return NULL;
3199 if (pwaff->n == 0)
3200 return pwaff;
3202 for (i = 0; i < pwaff->n; ++i) {
3203 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3204 if (!pwaff->p[i].aff)
3205 return isl_pw_aff_free(pwaff);
3208 return pwaff;
3211 /* Assuming that "cond1" and "cond2" are disjoint,
3212 * return an affine expression that is equal to pwaff1 on cond1
3213 * and to pwaff2 on cond2.
3215 static __isl_give isl_pw_aff *isl_pw_aff_select(
3216 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3217 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3219 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3220 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3222 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3225 /* Return an affine expression that is equal to pwaff_true for elements
3226 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3227 * is zero.
3228 * That is, return cond ? pwaff_true : pwaff_false;
3230 * If "cond" involves and NaN, then we conservatively return a NaN
3231 * on its entire domain. In principle, we could consider the pieces
3232 * where it is NaN separately from those where it is not.
3234 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3235 * then only use the domain of "cond" to restrict the domain.
3237 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3238 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3240 isl_set *cond_true, *cond_false;
3241 isl_bool equal;
3243 if (!cond)
3244 goto error;
3245 if (isl_pw_aff_involves_nan(cond)) {
3246 isl_space *space = isl_pw_aff_get_domain_space(cond);
3247 isl_local_space *ls = isl_local_space_from_space(space);
3248 isl_pw_aff_free(cond);
3249 isl_pw_aff_free(pwaff_true);
3250 isl_pw_aff_free(pwaff_false);
3251 return isl_pw_aff_nan_on_domain(ls);
3254 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3255 isl_pw_aff_get_space(pwaff_false));
3256 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3257 isl_pw_aff_get_space(pwaff_true));
3258 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3259 if (equal < 0)
3260 goto error;
3261 if (equal) {
3262 isl_set *dom;
3264 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3265 isl_pw_aff_free(pwaff_false);
3266 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3269 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3270 cond_false = isl_pw_aff_zero_set(cond);
3271 return isl_pw_aff_select(cond_true, pwaff_true,
3272 cond_false, pwaff_false);
3273 error:
3274 isl_pw_aff_free(cond);
3275 isl_pw_aff_free(pwaff_true);
3276 isl_pw_aff_free(pwaff_false);
3277 return NULL;
3280 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3282 if (!aff)
3283 return isl_bool_error;
3285 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3288 /* Check whether pwaff is a piecewise constant.
3290 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3292 int i;
3294 if (!pwaff)
3295 return isl_bool_error;
3297 for (i = 0; i < pwaff->n; ++i) {
3298 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3299 if (is_cst < 0 || !is_cst)
3300 return is_cst;
3303 return isl_bool_true;
3306 /* Are all elements of "mpa" piecewise constants?
3308 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3310 int i;
3312 if (!mpa)
3313 return isl_bool_error;
3315 for (i = 0; i < mpa->n; ++i) {
3316 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3317 if (is_cst < 0 || !is_cst)
3318 return is_cst;
3321 return isl_bool_true;
3324 /* Return the product of "aff1" and "aff2".
3326 * If either of the two is NaN, then the result is NaN.
3328 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3330 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3331 __isl_take isl_aff *aff2)
3333 if (!aff1 || !aff2)
3334 goto error;
3336 if (isl_aff_is_nan(aff1)) {
3337 isl_aff_free(aff2);
3338 return aff1;
3340 if (isl_aff_is_nan(aff2)) {
3341 isl_aff_free(aff1);
3342 return aff2;
3345 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3346 return isl_aff_mul(aff2, aff1);
3348 if (!isl_aff_is_cst(aff2))
3349 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3350 "at least one affine expression should be constant",
3351 goto error);
3353 aff1 = isl_aff_cow(aff1);
3354 if (!aff1 || !aff2)
3355 goto error;
3357 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3358 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3360 isl_aff_free(aff2);
3361 return aff1;
3362 error:
3363 isl_aff_free(aff1);
3364 isl_aff_free(aff2);
3365 return NULL;
3368 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3370 * If either of the two is NaN, then the result is NaN.
3372 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3373 __isl_take isl_aff *aff2)
3375 int is_cst;
3376 int neg;
3378 if (!aff1 || !aff2)
3379 goto error;
3381 if (isl_aff_is_nan(aff1)) {
3382 isl_aff_free(aff2);
3383 return aff1;
3385 if (isl_aff_is_nan(aff2)) {
3386 isl_aff_free(aff1);
3387 return aff2;
3390 is_cst = isl_aff_is_cst(aff2);
3391 if (is_cst < 0)
3392 goto error;
3393 if (!is_cst)
3394 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3395 "second argument should be a constant", goto error);
3397 if (!aff2)
3398 goto error;
3400 neg = isl_int_is_neg(aff2->v->el[1]);
3401 if (neg) {
3402 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3403 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3406 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3407 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3409 if (neg) {
3410 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3411 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3414 isl_aff_free(aff2);
3415 return aff1;
3416 error:
3417 isl_aff_free(aff1);
3418 isl_aff_free(aff2);
3419 return NULL;
3422 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3423 __isl_take isl_pw_aff *pwaff2)
3425 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3428 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3429 __isl_take isl_pw_aff *pwaff2)
3431 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3434 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3435 __isl_take isl_pw_aff *pwaff2)
3437 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3440 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3441 __isl_take isl_pw_aff *pwaff2)
3443 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3446 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3447 __isl_take isl_pw_aff *pwaff2)
3449 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3452 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3453 __isl_take isl_pw_aff *pa2)
3455 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3458 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3460 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3461 __isl_take isl_pw_aff *pa2)
3463 int is_cst;
3465 is_cst = isl_pw_aff_is_cst(pa2);
3466 if (is_cst < 0)
3467 goto error;
3468 if (!is_cst)
3469 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3470 "second argument should be a piecewise constant",
3471 goto error);
3472 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3473 error:
3474 isl_pw_aff_free(pa1);
3475 isl_pw_aff_free(pa2);
3476 return NULL;
3479 /* Compute the quotient of the integer division of "pa1" by "pa2"
3480 * with rounding towards zero.
3481 * "pa2" is assumed to be a piecewise constant.
3483 * In particular, return
3485 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3488 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3489 __isl_take isl_pw_aff *pa2)
3491 int is_cst;
3492 isl_set *cond;
3493 isl_pw_aff *f, *c;
3495 is_cst = isl_pw_aff_is_cst(pa2);
3496 if (is_cst < 0)
3497 goto error;
3498 if (!is_cst)
3499 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3500 "second argument should be a piecewise constant",
3501 goto error);
3503 pa1 = isl_pw_aff_div(pa1, pa2);
3505 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3506 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3507 c = isl_pw_aff_ceil(pa1);
3508 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3509 error:
3510 isl_pw_aff_free(pa1);
3511 isl_pw_aff_free(pa2);
3512 return NULL;
3515 /* Compute the remainder of the integer division of "pa1" by "pa2"
3516 * with rounding towards zero.
3517 * "pa2" is assumed to be a piecewise constant.
3519 * In particular, return
3521 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3524 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3525 __isl_take isl_pw_aff *pa2)
3527 int is_cst;
3528 isl_pw_aff *res;
3530 is_cst = isl_pw_aff_is_cst(pa2);
3531 if (is_cst < 0)
3532 goto error;
3533 if (!is_cst)
3534 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3535 "second argument should be a piecewise constant",
3536 goto error);
3537 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3538 res = isl_pw_aff_mul(pa2, res);
3539 res = isl_pw_aff_sub(pa1, res);
3540 return res;
3541 error:
3542 isl_pw_aff_free(pa1);
3543 isl_pw_aff_free(pa2);
3544 return NULL;
3547 /* Does either of "pa1" or "pa2" involve any NaN2?
3549 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3550 __isl_keep isl_pw_aff *pa2)
3552 isl_bool has_nan;
3554 has_nan = isl_pw_aff_involves_nan(pa1);
3555 if (has_nan < 0 || has_nan)
3556 return has_nan;
3557 return isl_pw_aff_involves_nan(pa2);
3560 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3561 * by a NaN on their shared domain.
3563 * In principle, the result could be refined to only being NaN
3564 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3566 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3567 __isl_take isl_pw_aff *pa2)
3569 isl_local_space *ls;
3570 isl_set *dom;
3571 isl_pw_aff *pa;
3573 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3574 ls = isl_local_space_from_space(isl_set_get_space(dom));
3575 pa = isl_pw_aff_nan_on_domain(ls);
3576 pa = isl_pw_aff_intersect_domain(pa, dom);
3578 return pa;
3581 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3582 __isl_take isl_pw_aff *pwaff2)
3584 isl_set *le;
3585 isl_set *dom;
3587 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3588 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3589 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3590 isl_pw_aff_copy(pwaff2));
3591 dom = isl_set_subtract(dom, isl_set_copy(le));
3592 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3595 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3596 __isl_take isl_pw_aff *pwaff2)
3598 isl_set *ge;
3599 isl_set *dom;
3601 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3602 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3603 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3604 isl_pw_aff_copy(pwaff2));
3605 dom = isl_set_subtract(dom, isl_set_copy(ge));
3606 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3609 /* Return an expression for the minimum (if "max" is not set) or
3610 * the maximum (if "max" is set) of "pa1" and "pa2".
3611 * If either expression involves any NaN, then return a NaN
3612 * on the shared domain as result.
3614 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3615 __isl_take isl_pw_aff *pa2, int max)
3617 isl_bool has_nan;
3619 has_nan = either_involves_nan(pa1, pa2);
3620 if (has_nan < 0)
3621 pa1 = isl_pw_aff_free(pa1);
3622 else if (has_nan)
3623 return replace_by_nan(pa1, pa2);
3625 if (max)
3626 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3627 else
3628 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3631 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3633 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3634 __isl_take isl_pw_aff *pwaff2)
3636 return pw_aff_min_max(pwaff1, pwaff2, 0);
3639 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3641 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3642 __isl_take isl_pw_aff *pwaff2)
3644 return pw_aff_min_max(pwaff1, pwaff2, 1);
3647 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3648 __isl_take isl_pw_aff_list *list,
3649 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3650 __isl_take isl_pw_aff *pwaff2))
3652 int i;
3653 isl_ctx *ctx;
3654 isl_pw_aff *res;
3656 if (!list)
3657 return NULL;
3659 ctx = isl_pw_aff_list_get_ctx(list);
3660 if (list->n < 1)
3661 isl_die(ctx, isl_error_invalid,
3662 "list should contain at least one element", goto error);
3664 res = isl_pw_aff_copy(list->p[0]);
3665 for (i = 1; i < list->n; ++i)
3666 res = fn(res, isl_pw_aff_copy(list->p[i]));
3668 isl_pw_aff_list_free(list);
3669 return res;
3670 error:
3671 isl_pw_aff_list_free(list);
3672 return NULL;
3675 /* Return an isl_pw_aff that maps each element in the intersection of the
3676 * domains of the elements of list to the minimal corresponding affine
3677 * expression.
3679 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3681 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3684 /* Return an isl_pw_aff that maps each element in the intersection of the
3685 * domains of the elements of list to the maximal corresponding affine
3686 * expression.
3688 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3690 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3693 /* Mark the domains of "pwaff" as rational.
3695 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3697 int i;
3699 pwaff = isl_pw_aff_cow(pwaff);
3700 if (!pwaff)
3701 return NULL;
3702 if (pwaff->n == 0)
3703 return pwaff;
3705 for (i = 0; i < pwaff->n; ++i) {
3706 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3707 if (!pwaff->p[i].set)
3708 return isl_pw_aff_free(pwaff);
3711 return pwaff;
3714 /* Mark the domains of the elements of "list" as rational.
3716 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3717 __isl_take isl_pw_aff_list *list)
3719 int i, n;
3721 if (!list)
3722 return NULL;
3723 if (list->n == 0)
3724 return list;
3726 n = list->n;
3727 for (i = 0; i < n; ++i) {
3728 isl_pw_aff *pa;
3730 pa = isl_pw_aff_list_get_pw_aff(list, i);
3731 pa = isl_pw_aff_set_rational(pa);
3732 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3735 return list;
3738 /* Do the parameters of "aff" match those of "space"?
3740 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3741 __isl_keep isl_space *space)
3743 isl_space *aff_space;
3744 int match;
3746 if (!aff || !space)
3747 return -1;
3749 aff_space = isl_aff_get_domain_space(aff);
3751 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3753 isl_space_free(aff_space);
3754 return match;
3757 /* Check that the domain space of "aff" matches "space".
3759 * Return 0 on success and -1 on error.
3761 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3762 __isl_keep isl_space *space)
3764 isl_space *aff_space;
3765 int match;
3767 if (!aff || !space)
3768 return -1;
3770 aff_space = isl_aff_get_domain_space(aff);
3772 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3773 if (match < 0)
3774 goto error;
3775 if (!match)
3776 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3777 "parameters don't match", goto error);
3778 match = isl_space_tuple_is_equal(space, isl_dim_in,
3779 aff_space, isl_dim_set);
3780 if (match < 0)
3781 goto error;
3782 if (!match)
3783 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3784 "domains don't match", goto error);
3785 isl_space_free(aff_space);
3786 return 0;
3787 error:
3788 isl_space_free(aff_space);
3789 return -1;
3792 #undef BASE
3793 #define BASE aff
3794 #undef DOMBASE
3795 #define DOMBASE set
3796 #define NO_DOMAIN
3798 #include <isl_multi_templ.c>
3799 #include <isl_multi_apply_set.c>
3800 #include <isl_multi_cmp.c>
3801 #include <isl_multi_floor.c>
3802 #include <isl_multi_gist.c>
3804 #undef NO_DOMAIN
3806 /* Remove any internal structure of the domain of "ma".
3807 * If there is any such internal structure in the input,
3808 * then the name of the corresponding space is also removed.
3810 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3811 __isl_take isl_multi_aff *ma)
3813 isl_space *space;
3815 if (!ma)
3816 return NULL;
3818 if (!ma->space->nested[0])
3819 return ma;
3821 space = isl_multi_aff_get_space(ma);
3822 space = isl_space_flatten_domain(space);
3823 ma = isl_multi_aff_reset_space(ma, space);
3825 return ma;
3828 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3829 * of the space to its domain.
3831 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3833 int i, n_in;
3834 isl_local_space *ls;
3835 isl_multi_aff *ma;
3837 if (!space)
3838 return NULL;
3839 if (!isl_space_is_map(space))
3840 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3841 "not a map space", goto error);
3843 n_in = isl_space_dim(space, isl_dim_in);
3844 space = isl_space_domain_map(space);
3846 ma = isl_multi_aff_alloc(isl_space_copy(space));
3847 if (n_in == 0) {
3848 isl_space_free(space);
3849 return ma;
3852 space = isl_space_domain(space);
3853 ls = isl_local_space_from_space(space);
3854 for (i = 0; i < n_in; ++i) {
3855 isl_aff *aff;
3857 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3858 isl_dim_set, i);
3859 ma = isl_multi_aff_set_aff(ma, i, aff);
3861 isl_local_space_free(ls);
3862 return ma;
3863 error:
3864 isl_space_free(space);
3865 return NULL;
3868 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3869 * of the space to its range.
3871 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3873 int i, n_in, n_out;
3874 isl_local_space *ls;
3875 isl_multi_aff *ma;
3877 if (!space)
3878 return NULL;
3879 if (!isl_space_is_map(space))
3880 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3881 "not a map space", goto error);
3883 n_in = isl_space_dim(space, isl_dim_in);
3884 n_out = isl_space_dim(space, isl_dim_out);
3885 space = isl_space_range_map(space);
3887 ma = isl_multi_aff_alloc(isl_space_copy(space));
3888 if (n_out == 0) {
3889 isl_space_free(space);
3890 return ma;
3893 space = isl_space_domain(space);
3894 ls = isl_local_space_from_space(space);
3895 for (i = 0; i < n_out; ++i) {
3896 isl_aff *aff;
3898 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3899 isl_dim_set, n_in + i);
3900 ma = isl_multi_aff_set_aff(ma, i, aff);
3902 isl_local_space_free(ls);
3903 return ma;
3904 error:
3905 isl_space_free(space);
3906 return NULL;
3909 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3910 * of the space to its range.
3912 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3913 __isl_take isl_space *space)
3915 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3918 /* Given the space of a set and a range of set dimensions,
3919 * construct an isl_multi_aff that projects out those dimensions.
3921 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3922 __isl_take isl_space *space, enum isl_dim_type type,
3923 unsigned first, unsigned n)
3925 int i, dim;
3926 isl_local_space *ls;
3927 isl_multi_aff *ma;
3929 if (!space)
3930 return NULL;
3931 if (!isl_space_is_set(space))
3932 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3933 "expecting set space", goto error);
3934 if (type != isl_dim_set)
3935 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3936 "only set dimensions can be projected out", goto error);
3938 dim = isl_space_dim(space, isl_dim_set);
3939 if (first + n > dim)
3940 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3941 "range out of bounds", goto error);
3943 space = isl_space_from_domain(space);
3944 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3946 if (dim == n)
3947 return isl_multi_aff_alloc(space);
3949 ma = isl_multi_aff_alloc(isl_space_copy(space));
3950 space = isl_space_domain(space);
3951 ls = isl_local_space_from_space(space);
3953 for (i = 0; i < first; ++i) {
3954 isl_aff *aff;
3956 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3957 isl_dim_set, i);
3958 ma = isl_multi_aff_set_aff(ma, i, aff);
3961 for (i = 0; i < dim - (first + n); ++i) {
3962 isl_aff *aff;
3964 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3965 isl_dim_set, first + n + i);
3966 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3969 isl_local_space_free(ls);
3970 return ma;
3971 error:
3972 isl_space_free(space);
3973 return NULL;
3976 /* Given the space of a set and a range of set dimensions,
3977 * construct an isl_pw_multi_aff that projects out those dimensions.
3979 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3980 __isl_take isl_space *space, enum isl_dim_type type,
3981 unsigned first, unsigned n)
3983 isl_multi_aff *ma;
3985 ma = isl_multi_aff_project_out_map(space, type, first, n);
3986 return isl_pw_multi_aff_from_multi_aff(ma);
3989 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3990 * domain.
3992 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3993 __isl_take isl_multi_aff *ma)
3995 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3996 return isl_pw_multi_aff_alloc(dom, ma);
3999 /* Create a piecewise multi-affine expression in the given space that maps each
4000 * input dimension to the corresponding output dimension.
4002 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4003 __isl_take isl_space *space)
4005 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4008 /* Exploit the equalities in "eq" to simplify the affine expressions.
4010 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4011 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4013 int i;
4015 maff = isl_multi_aff_cow(maff);
4016 if (!maff || !eq)
4017 goto error;
4019 for (i = 0; i < maff->n; ++i) {
4020 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4021 isl_basic_set_copy(eq));
4022 if (!maff->p[i])
4023 goto error;
4026 isl_basic_set_free(eq);
4027 return maff;
4028 error:
4029 isl_basic_set_free(eq);
4030 isl_multi_aff_free(maff);
4031 return NULL;
4034 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4035 isl_int f)
4037 int i;
4039 maff = isl_multi_aff_cow(maff);
4040 if (!maff)
4041 return NULL;
4043 for (i = 0; i < maff->n; ++i) {
4044 maff->p[i] = isl_aff_scale(maff->p[i], f);
4045 if (!maff->p[i])
4046 return isl_multi_aff_free(maff);
4049 return maff;
4052 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4053 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4055 maff1 = isl_multi_aff_add(maff1, maff2);
4056 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4057 return maff1;
4060 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4062 if (!maff)
4063 return -1;
4065 return 0;
4068 /* Return the set of domain elements where "ma1" is lexicographically
4069 * smaller than or equal to "ma2".
4071 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4072 __isl_take isl_multi_aff *ma2)
4074 return isl_multi_aff_lex_ge_set(ma2, ma1);
4077 /* Return the set of domain elements where "ma1" is lexicographically
4078 * smaller than "ma2".
4080 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4081 __isl_take isl_multi_aff *ma2)
4083 return isl_multi_aff_lex_gt_set(ma2, ma1);
4086 /* Return the set of domain elements where "ma1" and "ma2"
4087 * satisfy "order".
4089 static __isl_give isl_set *isl_multi_aff_order_set(
4090 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4091 __isl_give isl_map *order(__isl_take isl_space *set_space))
4093 isl_space *space;
4094 isl_map *map1, *map2;
4095 isl_map *map, *ge;
4097 map1 = isl_map_from_multi_aff(ma1);
4098 map2 = isl_map_from_multi_aff(ma2);
4099 map = isl_map_range_product(map1, map2);
4100 space = isl_space_range(isl_map_get_space(map));
4101 space = isl_space_domain(isl_space_unwrap(space));
4102 ge = order(space);
4103 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4105 return isl_map_domain(map);
4108 /* Return the set of domain elements where "ma1" is lexicographically
4109 * greater than or equal to "ma2".
4111 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4112 __isl_take isl_multi_aff *ma2)
4114 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4117 /* Return the set of domain elements where "ma1" is lexicographically
4118 * greater than "ma2".
4120 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4121 __isl_take isl_multi_aff *ma2)
4123 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4126 #undef PW
4127 #define PW isl_pw_multi_aff
4128 #undef EL
4129 #define EL isl_multi_aff
4130 #undef EL_IS_ZERO
4131 #define EL_IS_ZERO is_empty
4132 #undef ZERO
4133 #define ZERO empty
4134 #undef IS_ZERO
4135 #define IS_ZERO is_empty
4136 #undef FIELD
4137 #define FIELD maff
4138 #undef DEFAULT_IS_ZERO
4139 #define DEFAULT_IS_ZERO 0
4141 #define NO_SUB
4142 #define NO_EVAL
4143 #define NO_OPT
4144 #define NO_INVOLVES_DIMS
4145 #define NO_INSERT_DIMS
4146 #define NO_LIFT
4147 #define NO_MORPH
4149 #include <isl_pw_templ.c>
4150 #include <isl_pw_union_opt.c>
4152 #undef NO_SUB
4154 #undef UNION
4155 #define UNION isl_union_pw_multi_aff
4156 #undef PART
4157 #define PART isl_pw_multi_aff
4158 #undef PARTS
4159 #define PARTS pw_multi_aff
4161 #include <isl_union_multi.c>
4162 #include <isl_union_neg.c>
4164 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4165 __isl_take isl_pw_multi_aff *pma1,
4166 __isl_take isl_pw_multi_aff *pma2)
4168 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4169 &isl_multi_aff_lex_ge_set);
4172 /* Given two piecewise multi affine expressions, return a piecewise
4173 * multi-affine expression defined on the union of the definition domains
4174 * of the inputs that is equal to the lexicographic maximum of the two
4175 * inputs on each cell. If only one of the two inputs is defined on
4176 * a given cell, then it is considered to be the maximum.
4178 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4179 __isl_take isl_pw_multi_aff *pma1,
4180 __isl_take isl_pw_multi_aff *pma2)
4182 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4183 &pw_multi_aff_union_lexmax);
4186 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4187 __isl_take isl_pw_multi_aff *pma1,
4188 __isl_take isl_pw_multi_aff *pma2)
4190 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4191 &isl_multi_aff_lex_le_set);
4194 /* Given two piecewise multi affine expressions, return a piecewise
4195 * multi-affine expression defined on the union of the definition domains
4196 * of the inputs that is equal to the lexicographic minimum of the two
4197 * inputs on each cell. If only one of the two inputs is defined on
4198 * a given cell, then it is considered to be the minimum.
4200 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4201 __isl_take isl_pw_multi_aff *pma1,
4202 __isl_take isl_pw_multi_aff *pma2)
4204 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4205 &pw_multi_aff_union_lexmin);
4208 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4209 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4211 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4212 &isl_multi_aff_add);
4215 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4216 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4218 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4219 &pw_multi_aff_add);
4222 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4223 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4225 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4226 &isl_multi_aff_sub);
4229 /* Subtract "pma2" from "pma1" and return the result.
4231 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4232 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4234 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4235 &pw_multi_aff_sub);
4238 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4239 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4241 return isl_pw_multi_aff_union_add_(pma1, pma2);
4244 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4245 * with the actual sum on the shared domain and
4246 * the defined expression on the symmetric difference of the domains.
4248 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4249 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4251 return isl_union_pw_aff_union_add_(upa1, upa2);
4254 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4255 * with the actual sum on the shared domain and
4256 * the defined expression on the symmetric difference of the domains.
4258 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4259 __isl_take isl_union_pw_multi_aff *upma1,
4260 __isl_take isl_union_pw_multi_aff *upma2)
4262 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4265 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4266 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4268 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4269 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4271 int i, j, n;
4272 isl_space *space;
4273 isl_pw_multi_aff *res;
4275 if (!pma1 || !pma2)
4276 goto error;
4278 n = pma1->n * pma2->n;
4279 space = isl_space_product(isl_space_copy(pma1->dim),
4280 isl_space_copy(pma2->dim));
4281 res = isl_pw_multi_aff_alloc_size(space, n);
4283 for (i = 0; i < pma1->n; ++i) {
4284 for (j = 0; j < pma2->n; ++j) {
4285 isl_set *domain;
4286 isl_multi_aff *ma;
4288 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4289 isl_set_copy(pma2->p[j].set));
4290 ma = isl_multi_aff_product(
4291 isl_multi_aff_copy(pma1->p[i].maff),
4292 isl_multi_aff_copy(pma2->p[j].maff));
4293 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4297 isl_pw_multi_aff_free(pma1);
4298 isl_pw_multi_aff_free(pma2);
4299 return res;
4300 error:
4301 isl_pw_multi_aff_free(pma1);
4302 isl_pw_multi_aff_free(pma2);
4303 return NULL;
4306 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4307 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4309 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4310 &pw_multi_aff_product);
4313 /* Construct a map mapping the domain of the piecewise multi-affine expression
4314 * to its range, with each dimension in the range equated to the
4315 * corresponding affine expression on its cell.
4317 * If the domain of "pma" is rational, then so is the constructed "map".
4319 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4321 int i;
4322 isl_map *map;
4324 if (!pma)
4325 return NULL;
4327 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4329 for (i = 0; i < pma->n; ++i) {
4330 isl_bool rational;
4331 isl_multi_aff *maff;
4332 isl_basic_map *bmap;
4333 isl_map *map_i;
4335 rational = isl_set_is_rational(pma->p[i].set);
4336 if (rational < 0)
4337 map = isl_map_free(map);
4338 maff = isl_multi_aff_copy(pma->p[i].maff);
4339 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4340 map_i = isl_map_from_basic_map(bmap);
4341 map_i = isl_map_intersect_domain(map_i,
4342 isl_set_copy(pma->p[i].set));
4343 map = isl_map_union_disjoint(map, map_i);
4346 isl_pw_multi_aff_free(pma);
4347 return map;
4350 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4352 if (!pma)
4353 return NULL;
4355 if (!isl_space_is_set(pma->dim))
4356 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4357 "isl_pw_multi_aff cannot be converted into an isl_set",
4358 goto error);
4360 return isl_map_from_pw_multi_aff(pma);
4361 error:
4362 isl_pw_multi_aff_free(pma);
4363 return NULL;
4366 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4367 * denominator "denom".
4368 * "denom" is allowed to be negative, in which case the actual denominator
4369 * is -denom and the expressions are added instead.
4371 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4372 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4374 int i, first;
4375 int sign;
4376 isl_int d;
4378 first = isl_seq_first_non_zero(c, n);
4379 if (first == -1)
4380 return aff;
4382 sign = isl_int_sgn(denom);
4383 isl_int_init(d);
4384 isl_int_abs(d, denom);
4385 for (i = first; i < n; ++i) {
4386 isl_aff *aff_i;
4388 if (isl_int_is_zero(c[i]))
4389 continue;
4390 aff_i = isl_multi_aff_get_aff(ma, i);
4391 aff_i = isl_aff_scale(aff_i, c[i]);
4392 aff_i = isl_aff_scale_down(aff_i, d);
4393 if (sign >= 0)
4394 aff = isl_aff_sub(aff, aff_i);
4395 else
4396 aff = isl_aff_add(aff, aff_i);
4398 isl_int_clear(d);
4400 return aff;
4403 /* Extract an affine expression that expresses the output dimension "pos"
4404 * of "bmap" in terms of the parameters and input dimensions from
4405 * equality "eq".
4406 * Note that this expression may involve integer divisions defined
4407 * in terms of parameters and input dimensions.
4408 * The equality may also involve references to earlier (but not later)
4409 * output dimensions. These are replaced by the corresponding elements
4410 * in "ma".
4412 * If the equality is of the form
4414 * f(i) + h(j) + a x + g(i) = 0,
4416 * with f(i) a linear combinations of the parameters and input dimensions,
4417 * g(i) a linear combination of integer divisions defined in terms of the same
4418 * and h(j) a linear combinations of earlier output dimensions,
4419 * then the affine expression is
4421 * (-f(i) - g(i))/a - h(j)/a
4423 * If the equality is of the form
4425 * f(i) + h(j) - a x + g(i) = 0,
4427 * then the affine expression is
4429 * (f(i) + g(i))/a - h(j)/(-a)
4432 * If "div" refers to an integer division (i.e., it is smaller than
4433 * the number of integer divisions), then the equality constraint
4434 * does involve an integer division (the one at position "div") that
4435 * is defined in terms of output dimensions. However, this integer
4436 * division can be eliminated by exploiting a pair of constraints
4437 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4438 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4439 * -l + x >= 0.
4440 * In particular, let
4442 * x = e(i) + m floor(...)
4444 * with e(i) the expression derived above and floor(...) the integer
4445 * division involving output dimensions.
4446 * From
4448 * l <= x <= l + n,
4450 * we have
4452 * 0 <= x - l <= n
4454 * This means
4456 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4457 * = (e(i) - l) mod m
4459 * Therefore,
4461 * x - l = (e(i) - l) mod m
4463 * or
4465 * x = ((e(i) - l) mod m) + l
4467 * The variable "shift" below contains the expression -l, which may
4468 * also involve a linear combination of earlier output dimensions.
4470 static __isl_give isl_aff *extract_aff_from_equality(
4471 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4472 __isl_keep isl_multi_aff *ma)
4474 unsigned o_out;
4475 unsigned n_div, n_out;
4476 isl_ctx *ctx;
4477 isl_local_space *ls;
4478 isl_aff *aff, *shift;
4479 isl_val *mod;
4481 ctx = isl_basic_map_get_ctx(bmap);
4482 ls = isl_basic_map_get_local_space(bmap);
4483 ls = isl_local_space_domain(ls);
4484 aff = isl_aff_alloc(isl_local_space_copy(ls));
4485 if (!aff)
4486 goto error;
4487 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4488 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4489 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4490 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4491 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4492 isl_seq_cpy(aff->v->el + 1 + o_out,
4493 bmap->eq[eq] + o_out + n_out, n_div);
4494 } else {
4495 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4496 isl_seq_neg(aff->v->el + 1 + o_out,
4497 bmap->eq[eq] + o_out + n_out, n_div);
4499 if (div < n_div)
4500 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4501 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4502 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4503 bmap->eq[eq][o_out + pos]);
4504 if (div < n_div) {
4505 shift = isl_aff_alloc(isl_local_space_copy(ls));
4506 if (!shift)
4507 goto error;
4508 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4509 isl_seq_cpy(shift->v->el + 1 + o_out,
4510 bmap->ineq[ineq] + o_out + n_out, n_div);
4511 isl_int_set_si(shift->v->el[0], 1);
4512 shift = subtract_initial(shift, ma, pos,
4513 bmap->ineq[ineq] + o_out, ctx->negone);
4514 aff = isl_aff_add(aff, isl_aff_copy(shift));
4515 mod = isl_val_int_from_isl_int(ctx,
4516 bmap->eq[eq][o_out + n_out + div]);
4517 mod = isl_val_abs(mod);
4518 aff = isl_aff_mod_val(aff, mod);
4519 aff = isl_aff_sub(aff, shift);
4522 isl_local_space_free(ls);
4523 return aff;
4524 error:
4525 isl_local_space_free(ls);
4526 isl_aff_free(aff);
4527 return NULL;
4530 /* Given a basic map with output dimensions defined
4531 * in terms of the parameters input dimensions and earlier
4532 * output dimensions using an equality (and possibly a pair on inequalities),
4533 * extract an isl_aff that expresses output dimension "pos" in terms
4534 * of the parameters and input dimensions.
4535 * Note that this expression may involve integer divisions defined
4536 * in terms of parameters and input dimensions.
4537 * "ma" contains the expressions corresponding to earlier output dimensions.
4539 * This function shares some similarities with
4540 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4542 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4543 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4545 int eq, div, ineq;
4546 isl_aff *aff;
4548 if (!bmap)
4549 return NULL;
4550 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4551 if (eq >= bmap->n_eq)
4552 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4553 "unable to find suitable equality", return NULL);
4554 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4556 aff = isl_aff_remove_unused_divs(aff);
4557 return aff;
4560 /* Given a basic map where each output dimension is defined
4561 * in terms of the parameters and input dimensions using an equality,
4562 * extract an isl_multi_aff that expresses the output dimensions in terms
4563 * of the parameters and input dimensions.
4565 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4566 __isl_take isl_basic_map *bmap)
4568 int i;
4569 unsigned n_out;
4570 isl_multi_aff *ma;
4572 if (!bmap)
4573 return NULL;
4575 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4576 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4578 for (i = 0; i < n_out; ++i) {
4579 isl_aff *aff;
4581 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4582 ma = isl_multi_aff_set_aff(ma, i, aff);
4585 isl_basic_map_free(bmap);
4587 return ma;
4590 /* Given a basic set where each set dimension is defined
4591 * in terms of the parameters using an equality,
4592 * extract an isl_multi_aff that expresses the set dimensions in terms
4593 * of the parameters.
4595 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4596 __isl_take isl_basic_set *bset)
4598 return extract_isl_multi_aff_from_basic_map(bset);
4601 /* Create an isl_pw_multi_aff that is equivalent to
4602 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4603 * The given basic map is such that each output dimension is defined
4604 * in terms of the parameters and input dimensions using an equality.
4606 * Since some applications expect the result of isl_pw_multi_aff_from_map
4607 * to only contain integer affine expressions, we compute the floor
4608 * of the expression before returning.
4610 * Remove all constraints involving local variables without
4611 * an explicit representation (resulting in the removal of those
4612 * local variables) prior to the actual extraction to ensure
4613 * that the local spaces in which the resulting affine expressions
4614 * are created do not contain any unknown local variables.
4615 * Removing such constraints is safe because constraints involving
4616 * unknown local variables are not used to determine whether
4617 * a basic map is obviously single-valued.
4619 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4620 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4622 isl_multi_aff *ma;
4624 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4625 ma = extract_isl_multi_aff_from_basic_map(bmap);
4626 ma = isl_multi_aff_floor(ma);
4627 return isl_pw_multi_aff_alloc(domain, ma);
4630 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4631 * This obviously only works if the input "map" is single-valued.
4632 * If so, we compute the lexicographic minimum of the image in the form
4633 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4634 * to its lexicographic minimum.
4635 * If the input is not single-valued, we produce an error.
4637 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4638 __isl_take isl_map *map)
4640 int i;
4641 int sv;
4642 isl_pw_multi_aff *pma;
4644 sv = isl_map_is_single_valued(map);
4645 if (sv < 0)
4646 goto error;
4647 if (!sv)
4648 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4649 "map is not single-valued", goto error);
4650 map = isl_map_make_disjoint(map);
4651 if (!map)
4652 return NULL;
4654 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4656 for (i = 0; i < map->n; ++i) {
4657 isl_pw_multi_aff *pma_i;
4658 isl_basic_map *bmap;
4659 bmap = isl_basic_map_copy(map->p[i]);
4660 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4661 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4664 isl_map_free(map);
4665 return pma;
4666 error:
4667 isl_map_free(map);
4668 return NULL;
4671 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4672 * taking into account that the output dimension at position "d"
4673 * can be represented as
4675 * x = floor((e(...) + c1) / m)
4677 * given that constraint "i" is of the form
4679 * e(...) + c1 - m x >= 0
4682 * Let "map" be of the form
4684 * A -> B
4686 * We construct a mapping
4688 * A -> [A -> x = floor(...)]
4690 * apply that to the map, obtaining
4692 * [A -> x = floor(...)] -> B
4694 * and equate dimension "d" to x.
4695 * We then compute a isl_pw_multi_aff representation of the resulting map
4696 * and plug in the mapping above.
4698 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4699 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4701 isl_ctx *ctx;
4702 isl_space *space;
4703 isl_local_space *ls;
4704 isl_multi_aff *ma;
4705 isl_aff *aff;
4706 isl_vec *v;
4707 isl_map *insert;
4708 int offset;
4709 int n;
4710 int n_in;
4711 isl_pw_multi_aff *pma;
4712 int is_set;
4714 is_set = isl_map_is_set(map);
4716 offset = isl_basic_map_offset(hull, isl_dim_out);
4717 ctx = isl_map_get_ctx(map);
4718 space = isl_space_domain(isl_map_get_space(map));
4719 n_in = isl_space_dim(space, isl_dim_set);
4720 n = isl_space_dim(space, isl_dim_all);
4722 v = isl_vec_alloc(ctx, 1 + 1 + n);
4723 if (v) {
4724 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4725 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4727 isl_basic_map_free(hull);
4729 ls = isl_local_space_from_space(isl_space_copy(space));
4730 aff = isl_aff_alloc_vec(ls, v);
4731 aff = isl_aff_floor(aff);
4732 if (is_set) {
4733 isl_space_free(space);
4734 ma = isl_multi_aff_from_aff(aff);
4735 } else {
4736 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4737 ma = isl_multi_aff_range_product(ma,
4738 isl_multi_aff_from_aff(aff));
4741 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4742 map = isl_map_apply_domain(map, insert);
4743 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4744 pma = isl_pw_multi_aff_from_map(map);
4745 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4747 return pma;
4750 /* Is constraint "c" of the form
4752 * e(...) + c1 - m x >= 0
4754 * or
4756 * -e(...) + c2 + m x >= 0
4758 * where m > 1 and e only depends on parameters and input dimemnsions?
4760 * "offset" is the offset of the output dimensions
4761 * "pos" is the position of output dimension x.
4763 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4765 if (isl_int_is_zero(c[offset + d]))
4766 return 0;
4767 if (isl_int_is_one(c[offset + d]))
4768 return 0;
4769 if (isl_int_is_negone(c[offset + d]))
4770 return 0;
4771 if (isl_seq_first_non_zero(c + offset, d) != -1)
4772 return 0;
4773 if (isl_seq_first_non_zero(c + offset + d + 1,
4774 total - (offset + d + 1)) != -1)
4775 return 0;
4776 return 1;
4779 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4781 * As a special case, we first check if there is any pair of constraints,
4782 * shared by all the basic maps in "map" that force a given dimension
4783 * to be equal to the floor of some affine combination of the input dimensions.
4785 * In particular, if we can find two constraints
4787 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4789 * and
4791 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4793 * where m > 1 and e only depends on parameters and input dimemnsions,
4794 * and such that
4796 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4798 * then we know that we can take
4800 * x = floor((e(...) + c1) / m)
4802 * without having to perform any computation.
4804 * Note that we know that
4806 * c1 + c2 >= 1
4808 * If c1 + c2 were 0, then we would have detected an equality during
4809 * simplification. If c1 + c2 were negative, then we would have detected
4810 * a contradiction.
4812 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4813 __isl_take isl_map *map)
4815 int d, dim;
4816 int i, j, n;
4817 int offset, total;
4818 isl_int sum;
4819 isl_basic_map *hull;
4821 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4822 if (!hull)
4823 goto error;
4825 isl_int_init(sum);
4826 dim = isl_map_dim(map, isl_dim_out);
4827 offset = isl_basic_map_offset(hull, isl_dim_out);
4828 total = 1 + isl_basic_map_total_dim(hull);
4829 n = hull->n_ineq;
4830 for (d = 0; d < dim; ++d) {
4831 for (i = 0; i < n; ++i) {
4832 if (!is_potential_div_constraint(hull->ineq[i],
4833 offset, d, total))
4834 continue;
4835 for (j = i + 1; j < n; ++j) {
4836 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4837 hull->ineq[j] + 1, total - 1))
4838 continue;
4839 isl_int_add(sum, hull->ineq[i][0],
4840 hull->ineq[j][0]);
4841 if (isl_int_abs_lt(sum,
4842 hull->ineq[i][offset + d]))
4843 break;
4846 if (j >= n)
4847 continue;
4848 isl_int_clear(sum);
4849 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4850 j = i;
4851 return pw_multi_aff_from_map_div(map, hull, d, j);
4854 isl_int_clear(sum);
4855 isl_basic_map_free(hull);
4856 return pw_multi_aff_from_map_base(map);
4857 error:
4858 isl_map_free(map);
4859 isl_basic_map_free(hull);
4860 return NULL;
4863 /* Given an affine expression
4865 * [A -> B] -> f(A,B)
4867 * construct an isl_multi_aff
4869 * [A -> B] -> B'
4871 * such that dimension "d" in B' is set to "aff" and the remaining
4872 * dimensions are set equal to the corresponding dimensions in B.
4873 * "n_in" is the dimension of the space A.
4874 * "n_out" is the dimension of the space B.
4876 * If "is_set" is set, then the affine expression is of the form
4878 * [B] -> f(B)
4880 * and we construct an isl_multi_aff
4882 * B -> B'
4884 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4885 unsigned n_in, unsigned n_out, int is_set)
4887 int i;
4888 isl_multi_aff *ma;
4889 isl_space *space, *space2;
4890 isl_local_space *ls;
4892 space = isl_aff_get_domain_space(aff);
4893 ls = isl_local_space_from_space(isl_space_copy(space));
4894 space2 = isl_space_copy(space);
4895 if (!is_set)
4896 space2 = isl_space_range(isl_space_unwrap(space2));
4897 space = isl_space_map_from_domain_and_range(space, space2);
4898 ma = isl_multi_aff_alloc(space);
4899 ma = isl_multi_aff_set_aff(ma, d, aff);
4901 for (i = 0; i < n_out; ++i) {
4902 if (i == d)
4903 continue;
4904 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4905 isl_dim_set, n_in + i);
4906 ma = isl_multi_aff_set_aff(ma, i, aff);
4909 isl_local_space_free(ls);
4911 return ma;
4914 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4915 * taking into account that the dimension at position "d" can be written as
4917 * x = m a + f(..) (1)
4919 * where m is equal to "gcd".
4920 * "i" is the index of the equality in "hull" that defines f(..).
4921 * In particular, the equality is of the form
4923 * f(..) - x + m g(existentials) = 0
4925 * or
4927 * -f(..) + x + m g(existentials) = 0
4929 * We basically plug (1) into "map", resulting in a map with "a"
4930 * in the range instead of "x". The corresponding isl_pw_multi_aff
4931 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4933 * Specifically, given the input map
4935 * A -> B
4937 * We first wrap it into a set
4939 * [A -> B]
4941 * and define (1) on top of the corresponding space, resulting in "aff".
4942 * We use this to create an isl_multi_aff that maps the output position "d"
4943 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4944 * We plug this into the wrapped map, unwrap the result and compute the
4945 * corresponding isl_pw_multi_aff.
4946 * The result is an expression
4948 * A -> T(A)
4950 * We adjust that to
4952 * A -> [A -> T(A)]
4954 * so that we can plug that into "aff", after extending the latter to
4955 * a mapping
4957 * [A -> B] -> B'
4960 * If "map" is actually a set, then there is no "A" space, meaning
4961 * that we do not need to perform any wrapping, and that the result
4962 * of the recursive call is of the form
4964 * [T]
4966 * which is plugged into a mapping of the form
4968 * B -> B'
4970 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4971 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4972 isl_int gcd)
4974 isl_set *set;
4975 isl_space *space;
4976 isl_local_space *ls;
4977 isl_aff *aff;
4978 isl_multi_aff *ma;
4979 isl_pw_multi_aff *pma, *id;
4980 unsigned n_in;
4981 unsigned o_out;
4982 unsigned n_out;
4983 int is_set;
4985 is_set = isl_map_is_set(map);
4987 n_in = isl_basic_map_dim(hull, isl_dim_in);
4988 n_out = isl_basic_map_dim(hull, isl_dim_out);
4989 o_out = isl_basic_map_offset(hull, isl_dim_out);
4991 if (is_set)
4992 set = map;
4993 else
4994 set = isl_map_wrap(map);
4995 space = isl_space_map_from_set(isl_set_get_space(set));
4996 ma = isl_multi_aff_identity(space);
4997 ls = isl_local_space_from_space(isl_set_get_space(set));
4998 aff = isl_aff_alloc(ls);
4999 if (aff) {
5000 isl_int_set_si(aff->v->el[0], 1);
5001 if (isl_int_is_one(hull->eq[i][o_out + d]))
5002 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5003 aff->v->size - 1);
5004 else
5005 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5006 aff->v->size - 1);
5007 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5009 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5010 set = isl_set_preimage_multi_aff(set, ma);
5012 ma = range_map(aff, d, n_in, n_out, is_set);
5014 if (is_set)
5015 map = set;
5016 else
5017 map = isl_set_unwrap(set);
5018 pma = isl_pw_multi_aff_from_map(map);
5020 if (!is_set) {
5021 space = isl_pw_multi_aff_get_domain_space(pma);
5022 space = isl_space_map_from_set(space);
5023 id = isl_pw_multi_aff_identity(space);
5024 pma = isl_pw_multi_aff_range_product(id, pma);
5026 id = isl_pw_multi_aff_from_multi_aff(ma);
5027 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5029 isl_basic_map_free(hull);
5030 return pma;
5033 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5034 * "hull" contains the equalities valid for "map".
5036 * Check if any of the output dimensions is "strided".
5037 * That is, we check if it can be written as
5039 * x = m a + f(..)
5041 * with m greater than 1, a some combination of existentially quantified
5042 * variables and f an expression in the parameters and input dimensions.
5043 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5045 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5046 * special case.
5048 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5049 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5051 int i, j;
5052 unsigned n_out;
5053 unsigned o_out;
5054 unsigned n_div;
5055 unsigned o_div;
5056 isl_int gcd;
5058 n_div = isl_basic_map_dim(hull, isl_dim_div);
5059 o_div = isl_basic_map_offset(hull, isl_dim_div);
5061 if (n_div == 0) {
5062 isl_basic_map_free(hull);
5063 return pw_multi_aff_from_map_check_div(map);
5066 isl_int_init(gcd);
5068 n_out = isl_basic_map_dim(hull, isl_dim_out);
5069 o_out = isl_basic_map_offset(hull, isl_dim_out);
5071 for (i = 0; i < n_out; ++i) {
5072 for (j = 0; j < hull->n_eq; ++j) {
5073 isl_int *eq = hull->eq[j];
5074 isl_pw_multi_aff *res;
5076 if (!isl_int_is_one(eq[o_out + i]) &&
5077 !isl_int_is_negone(eq[o_out + i]))
5078 continue;
5079 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5080 continue;
5081 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5082 n_out - (i + 1)) != -1)
5083 continue;
5084 isl_seq_gcd(eq + o_div, n_div, &gcd);
5085 if (isl_int_is_zero(gcd))
5086 continue;
5087 if (isl_int_is_one(gcd))
5088 continue;
5090 res = pw_multi_aff_from_map_stride(map, hull,
5091 i, j, gcd);
5092 isl_int_clear(gcd);
5093 return res;
5097 isl_int_clear(gcd);
5098 isl_basic_map_free(hull);
5099 return pw_multi_aff_from_map_check_div(map);
5102 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5104 * As a special case, we first check if all output dimensions are uniquely
5105 * defined in terms of the parameters and input dimensions over the entire
5106 * domain. If so, we extract the desired isl_pw_multi_aff directly
5107 * from the affine hull of "map" and its domain.
5109 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5110 * special cases.
5112 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5114 isl_bool sv;
5115 isl_basic_map *hull;
5117 if (!map)
5118 return NULL;
5120 if (isl_map_n_basic_map(map) == 1) {
5121 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5122 hull = isl_basic_map_plain_affine_hull(hull);
5123 sv = isl_basic_map_plain_is_single_valued(hull);
5124 if (sv >= 0 && sv)
5125 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5126 hull);
5127 isl_basic_map_free(hull);
5129 map = isl_map_detect_equalities(map);
5130 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5131 sv = isl_basic_map_plain_is_single_valued(hull);
5132 if (sv >= 0 && sv)
5133 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5134 if (sv >= 0)
5135 return pw_multi_aff_from_map_check_strides(map, hull);
5136 isl_basic_map_free(hull);
5137 isl_map_free(map);
5138 return NULL;
5141 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5143 return isl_pw_multi_aff_from_map(set);
5146 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5147 * add it to *user.
5149 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5151 isl_union_pw_multi_aff **upma = user;
5152 isl_pw_multi_aff *pma;
5154 pma = isl_pw_multi_aff_from_map(map);
5155 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5157 return *upma ? isl_stat_ok : isl_stat_error;
5160 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5161 * domain.
5163 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5164 __isl_take isl_aff *aff)
5166 isl_multi_aff *ma;
5167 isl_pw_multi_aff *pma;
5169 ma = isl_multi_aff_from_aff(aff);
5170 pma = isl_pw_multi_aff_from_multi_aff(ma);
5171 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5174 /* Try and create an isl_union_pw_multi_aff that is equivalent
5175 * to the given isl_union_map.
5176 * The isl_union_map is required to be single-valued in each space.
5177 * Otherwise, an error is produced.
5179 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5180 __isl_take isl_union_map *umap)
5182 isl_space *space;
5183 isl_union_pw_multi_aff *upma;
5185 space = isl_union_map_get_space(umap);
5186 upma = isl_union_pw_multi_aff_empty(space);
5187 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5188 upma = isl_union_pw_multi_aff_free(upma);
5189 isl_union_map_free(umap);
5191 return upma;
5194 /* Try and create an isl_union_pw_multi_aff that is equivalent
5195 * to the given isl_union_set.
5196 * The isl_union_set is required to be a singleton in each space.
5197 * Otherwise, an error is produced.
5199 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5200 __isl_take isl_union_set *uset)
5202 return isl_union_pw_multi_aff_from_union_map(uset);
5205 /* Return the piecewise affine expression "set ? 1 : 0".
5207 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5209 isl_pw_aff *pa;
5210 isl_space *space = isl_set_get_space(set);
5211 isl_local_space *ls = isl_local_space_from_space(space);
5212 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5213 isl_aff *one = isl_aff_zero_on_domain(ls);
5215 one = isl_aff_add_constant_si(one, 1);
5216 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5217 set = isl_set_complement(set);
5218 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5220 return pa;
5223 /* Plug in "subs" for dimension "type", "pos" of "aff".
5225 * Let i be the dimension to replace and let "subs" be of the form
5227 * f/d
5229 * and "aff" of the form
5231 * (a i + g)/m
5233 * The result is
5235 * (a f + d g')/(m d)
5237 * where g' is the result of plugging in "subs" in each of the integer
5238 * divisions in g.
5240 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5241 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5243 isl_ctx *ctx;
5244 isl_int v;
5246 aff = isl_aff_cow(aff);
5247 if (!aff || !subs)
5248 return isl_aff_free(aff);
5250 ctx = isl_aff_get_ctx(aff);
5251 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5252 isl_die(ctx, isl_error_invalid,
5253 "spaces don't match", return isl_aff_free(aff));
5254 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5255 isl_die(ctx, isl_error_unsupported,
5256 "cannot handle divs yet", return isl_aff_free(aff));
5258 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5259 if (!aff->ls)
5260 return isl_aff_free(aff);
5262 aff->v = isl_vec_cow(aff->v);
5263 if (!aff->v)
5264 return isl_aff_free(aff);
5266 pos += isl_local_space_offset(aff->ls, type);
5268 isl_int_init(v);
5269 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5270 aff->v->size, subs->v->size, v);
5271 isl_int_clear(v);
5273 return aff;
5276 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5277 * expressions in "maff".
5279 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5280 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5281 __isl_keep isl_aff *subs)
5283 int i;
5285 maff = isl_multi_aff_cow(maff);
5286 if (!maff || !subs)
5287 return isl_multi_aff_free(maff);
5289 if (type == isl_dim_in)
5290 type = isl_dim_set;
5292 for (i = 0; i < maff->n; ++i) {
5293 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5294 if (!maff->p[i])
5295 return isl_multi_aff_free(maff);
5298 return maff;
5301 /* Plug in "subs" for dimension "type", "pos" of "pma".
5303 * pma is of the form
5305 * A_i(v) -> M_i(v)
5307 * while subs is of the form
5309 * v' = B_j(v) -> S_j
5311 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5312 * has a contribution in the result, in particular
5314 * C_ij(S_j) -> M_i(S_j)
5316 * Note that plugging in S_j in C_ij may also result in an empty set
5317 * and this contribution should simply be discarded.
5319 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5320 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5321 __isl_keep isl_pw_aff *subs)
5323 int i, j, n;
5324 isl_pw_multi_aff *res;
5326 if (!pma || !subs)
5327 return isl_pw_multi_aff_free(pma);
5329 n = pma->n * subs->n;
5330 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5332 for (i = 0; i < pma->n; ++i) {
5333 for (j = 0; j < subs->n; ++j) {
5334 isl_set *common;
5335 isl_multi_aff *res_ij;
5336 int empty;
5338 common = isl_set_intersect(
5339 isl_set_copy(pma->p[i].set),
5340 isl_set_copy(subs->p[j].set));
5341 common = isl_set_substitute(common,
5342 type, pos, subs->p[j].aff);
5343 empty = isl_set_plain_is_empty(common);
5344 if (empty < 0 || empty) {
5345 isl_set_free(common);
5346 if (empty < 0)
5347 goto error;
5348 continue;
5351 res_ij = isl_multi_aff_substitute(
5352 isl_multi_aff_copy(pma->p[i].maff),
5353 type, pos, subs->p[j].aff);
5355 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5359 isl_pw_multi_aff_free(pma);
5360 return res;
5361 error:
5362 isl_pw_multi_aff_free(pma);
5363 isl_pw_multi_aff_free(res);
5364 return NULL;
5367 /* Compute the preimage of a range of dimensions in the affine expression "src"
5368 * under "ma" and put the result in "dst". The number of dimensions in "src"
5369 * that precede the range is given by "n_before". The number of dimensions
5370 * in the range is given by the number of output dimensions of "ma".
5371 * The number of dimensions that follow the range is given by "n_after".
5372 * If "has_denom" is set (to one),
5373 * then "src" and "dst" have an extra initial denominator.
5374 * "n_div_ma" is the number of existentials in "ma"
5375 * "n_div_bset" is the number of existentials in "src"
5376 * The resulting "dst" (which is assumed to have been allocated by
5377 * the caller) contains coefficients for both sets of existentials,
5378 * first those in "ma" and then those in "src".
5379 * f, c1, c2 and g are temporary objects that have been initialized
5380 * by the caller.
5382 * Let src represent the expression
5384 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5386 * and let ma represent the expressions
5388 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5390 * We start out with the following expression for dst:
5392 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5394 * with the multiplication factor f initially equal to 1
5395 * and f \sum_i b_i v_i kept separately.
5396 * For each x_i that we substitute, we multiply the numerator
5397 * (and denominator) of dst by c_1 = m_i and add the numerator
5398 * of the x_i expression multiplied by c_2 = f b_i,
5399 * after removing the common factors of c_1 and c_2.
5400 * The multiplication factor f also needs to be multiplied by c_1
5401 * for the next x_j, j > i.
5403 void isl_seq_preimage(isl_int *dst, isl_int *src,
5404 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5405 int n_div_ma, int n_div_bmap,
5406 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5408 int i;
5409 int n_param, n_in, n_out;
5410 int o_dst, o_src;
5412 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5413 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5414 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5416 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5417 o_dst = o_src = has_denom + 1 + n_param + n_before;
5418 isl_seq_clr(dst + o_dst, n_in);
5419 o_dst += n_in;
5420 o_src += n_out;
5421 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5422 o_dst += n_after;
5423 o_src += n_after;
5424 isl_seq_clr(dst + o_dst, n_div_ma);
5425 o_dst += n_div_ma;
5426 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5428 isl_int_set_si(f, 1);
5430 for (i = 0; i < n_out; ++i) {
5431 int offset = has_denom + 1 + n_param + n_before + i;
5433 if (isl_int_is_zero(src[offset]))
5434 continue;
5435 isl_int_set(c1, ma->p[i]->v->el[0]);
5436 isl_int_mul(c2, f, src[offset]);
5437 isl_int_gcd(g, c1, c2);
5438 isl_int_divexact(c1, c1, g);
5439 isl_int_divexact(c2, c2, g);
5441 isl_int_mul(f, f, c1);
5442 o_dst = has_denom;
5443 o_src = 1;
5444 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5445 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5446 o_dst += 1 + n_param;
5447 o_src += 1 + n_param;
5448 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5449 o_dst += n_before;
5450 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5451 c2, ma->p[i]->v->el + o_src, n_in);
5452 o_dst += n_in;
5453 o_src += n_in;
5454 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5455 o_dst += n_after;
5456 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5457 c2, ma->p[i]->v->el + o_src, n_div_ma);
5458 o_dst += n_div_ma;
5459 o_src += n_div_ma;
5460 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5461 if (has_denom)
5462 isl_int_mul(dst[0], dst[0], c1);
5466 /* Compute the pullback of "aff" by the function represented by "ma".
5467 * In other words, plug in "ma" in "aff". The result is an affine expression
5468 * defined over the domain space of "ma".
5470 * If "aff" is represented by
5472 * (a(p) + b x + c(divs))/d
5474 * and ma is represented by
5476 * x = D(p) + F(y) + G(divs')
5478 * then the result is
5480 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5482 * The divs in the local space of the input are similarly adjusted
5483 * through a call to isl_local_space_preimage_multi_aff.
5485 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5486 __isl_take isl_multi_aff *ma)
5488 isl_aff *res = NULL;
5489 isl_local_space *ls;
5490 int n_div_aff, n_div_ma;
5491 isl_int f, c1, c2, g;
5493 ma = isl_multi_aff_align_divs(ma);
5494 if (!aff || !ma)
5495 goto error;
5497 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5498 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5500 ls = isl_aff_get_domain_local_space(aff);
5501 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5502 res = isl_aff_alloc(ls);
5503 if (!res)
5504 goto error;
5506 isl_int_init(f);
5507 isl_int_init(c1);
5508 isl_int_init(c2);
5509 isl_int_init(g);
5511 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5512 f, c1, c2, g, 1);
5514 isl_int_clear(f);
5515 isl_int_clear(c1);
5516 isl_int_clear(c2);
5517 isl_int_clear(g);
5519 isl_aff_free(aff);
5520 isl_multi_aff_free(ma);
5521 res = isl_aff_normalize(res);
5522 return res;
5523 error:
5524 isl_aff_free(aff);
5525 isl_multi_aff_free(ma);
5526 isl_aff_free(res);
5527 return NULL;
5530 /* Compute the pullback of "aff1" by the function represented by "aff2".
5531 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5532 * defined over the domain space of "aff1".
5534 * The domain of "aff1" should match the range of "aff2", which means
5535 * that it should be single-dimensional.
5537 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5538 __isl_take isl_aff *aff2)
5540 isl_multi_aff *ma;
5542 ma = isl_multi_aff_from_aff(aff2);
5543 return isl_aff_pullback_multi_aff(aff1, ma);
5546 /* Compute the pullback of "ma1" by the function represented by "ma2".
5547 * In other words, plug in "ma2" in "ma1".
5549 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5551 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5552 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5554 int i;
5555 isl_space *space = NULL;
5557 ma2 = isl_multi_aff_align_divs(ma2);
5558 ma1 = isl_multi_aff_cow(ma1);
5559 if (!ma1 || !ma2)
5560 goto error;
5562 space = isl_space_join(isl_multi_aff_get_space(ma2),
5563 isl_multi_aff_get_space(ma1));
5565 for (i = 0; i < ma1->n; ++i) {
5566 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5567 isl_multi_aff_copy(ma2));
5568 if (!ma1->p[i])
5569 goto error;
5572 ma1 = isl_multi_aff_reset_space(ma1, space);
5573 isl_multi_aff_free(ma2);
5574 return ma1;
5575 error:
5576 isl_space_free(space);
5577 isl_multi_aff_free(ma2);
5578 isl_multi_aff_free(ma1);
5579 return NULL;
5582 /* Compute the pullback of "ma1" by the function represented by "ma2".
5583 * In other words, plug in "ma2" in "ma1".
5585 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5586 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5588 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5589 &isl_multi_aff_pullback_multi_aff_aligned);
5592 /* Extend the local space of "dst" to include the divs
5593 * in the local space of "src".
5595 * If "src" does not have any divs or if the local spaces of "dst" and
5596 * "src" are the same, then no extension is required.
5598 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5599 __isl_keep isl_aff *src)
5601 isl_ctx *ctx;
5602 int src_n_div, dst_n_div;
5603 int *exp1 = NULL;
5604 int *exp2 = NULL;
5605 isl_bool equal;
5606 isl_mat *div;
5608 if (!src || !dst)
5609 return isl_aff_free(dst);
5611 ctx = isl_aff_get_ctx(src);
5612 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5613 if (equal < 0)
5614 return isl_aff_free(dst);
5615 if (!equal)
5616 isl_die(ctx, isl_error_invalid,
5617 "spaces don't match", goto error);
5619 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5620 if (src_n_div == 0)
5621 return dst;
5622 equal = isl_local_space_is_equal(src->ls, dst->ls);
5623 if (equal < 0)
5624 return isl_aff_free(dst);
5625 if (equal)
5626 return dst;
5628 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5629 exp1 = isl_alloc_array(ctx, int, src_n_div);
5630 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5631 if (!exp1 || (dst_n_div && !exp2))
5632 goto error;
5634 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5635 dst = isl_aff_expand_divs(dst, div, exp2);
5636 free(exp1);
5637 free(exp2);
5639 return dst;
5640 error:
5641 free(exp1);
5642 free(exp2);
5643 return isl_aff_free(dst);
5646 /* Adjust the local spaces of the affine expressions in "maff"
5647 * such that they all have the save divs.
5649 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5650 __isl_take isl_multi_aff *maff)
5652 int i;
5654 if (!maff)
5655 return NULL;
5656 if (maff->n == 0)
5657 return maff;
5658 maff = isl_multi_aff_cow(maff);
5659 if (!maff)
5660 return NULL;
5662 for (i = 1; i < maff->n; ++i)
5663 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5664 for (i = 1; i < maff->n; ++i) {
5665 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5666 if (!maff->p[i])
5667 return isl_multi_aff_free(maff);
5670 return maff;
5673 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5675 aff = isl_aff_cow(aff);
5676 if (!aff)
5677 return NULL;
5679 aff->ls = isl_local_space_lift(aff->ls);
5680 if (!aff->ls)
5681 return isl_aff_free(aff);
5683 return aff;
5686 /* Lift "maff" to a space with extra dimensions such that the result
5687 * has no more existentially quantified variables.
5688 * If "ls" is not NULL, then *ls is assigned the local space that lies
5689 * at the basis of the lifting applied to "maff".
5691 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5692 __isl_give isl_local_space **ls)
5694 int i;
5695 isl_space *space;
5696 unsigned n_div;
5698 if (ls)
5699 *ls = NULL;
5701 if (!maff)
5702 return NULL;
5704 if (maff->n == 0) {
5705 if (ls) {
5706 isl_space *space = isl_multi_aff_get_domain_space(maff);
5707 *ls = isl_local_space_from_space(space);
5708 if (!*ls)
5709 return isl_multi_aff_free(maff);
5711 return maff;
5714 maff = isl_multi_aff_cow(maff);
5715 maff = isl_multi_aff_align_divs(maff);
5716 if (!maff)
5717 return NULL;
5719 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5720 space = isl_multi_aff_get_space(maff);
5721 space = isl_space_lift(isl_space_domain(space), n_div);
5722 space = isl_space_extend_domain_with_range(space,
5723 isl_multi_aff_get_space(maff));
5724 if (!space)
5725 return isl_multi_aff_free(maff);
5726 isl_space_free(maff->space);
5727 maff->space = space;
5729 if (ls) {
5730 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5731 if (!*ls)
5732 return isl_multi_aff_free(maff);
5735 for (i = 0; i < maff->n; ++i) {
5736 maff->p[i] = isl_aff_lift(maff->p[i]);
5737 if (!maff->p[i])
5738 goto error;
5741 return maff;
5742 error:
5743 if (ls)
5744 isl_local_space_free(*ls);
5745 return isl_multi_aff_free(maff);
5749 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5751 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5752 __isl_keep isl_pw_multi_aff *pma, int pos)
5754 int i;
5755 int n_out;
5756 isl_space *space;
5757 isl_pw_aff *pa;
5759 if (!pma)
5760 return NULL;
5762 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5763 if (pos < 0 || pos >= n_out)
5764 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5765 "index out of bounds", return NULL);
5767 space = isl_pw_multi_aff_get_space(pma);
5768 space = isl_space_drop_dims(space, isl_dim_out,
5769 pos + 1, n_out - pos - 1);
5770 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5772 pa = isl_pw_aff_alloc_size(space, pma->n);
5773 for (i = 0; i < pma->n; ++i) {
5774 isl_aff *aff;
5775 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5776 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5779 return pa;
5782 /* Return an isl_pw_multi_aff with the given "set" as domain and
5783 * an unnamed zero-dimensional range.
5785 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5786 __isl_take isl_set *set)
5788 isl_multi_aff *ma;
5789 isl_space *space;
5791 space = isl_set_get_space(set);
5792 space = isl_space_from_domain(space);
5793 ma = isl_multi_aff_zero(space);
5794 return isl_pw_multi_aff_alloc(set, ma);
5797 /* Add an isl_pw_multi_aff with the given "set" as domain and
5798 * an unnamed zero-dimensional range to *user.
5800 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5801 void *user)
5803 isl_union_pw_multi_aff **upma = user;
5804 isl_pw_multi_aff *pma;
5806 pma = isl_pw_multi_aff_from_domain(set);
5807 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5809 return isl_stat_ok;
5812 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5813 * an unnamed zero-dimensional range.
5815 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5816 __isl_take isl_union_set *uset)
5818 isl_space *space;
5819 isl_union_pw_multi_aff *upma;
5821 if (!uset)
5822 return NULL;
5824 space = isl_union_set_get_space(uset);
5825 upma = isl_union_pw_multi_aff_empty(space);
5827 if (isl_union_set_foreach_set(uset,
5828 &add_pw_multi_aff_from_domain, &upma) < 0)
5829 goto error;
5831 isl_union_set_free(uset);
5832 return upma;
5833 error:
5834 isl_union_set_free(uset);
5835 isl_union_pw_multi_aff_free(upma);
5836 return NULL;
5839 /* Convert "pma" to an isl_map and add it to *umap.
5841 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5842 void *user)
5844 isl_union_map **umap = user;
5845 isl_map *map;
5847 map = isl_map_from_pw_multi_aff(pma);
5848 *umap = isl_union_map_add_map(*umap, map);
5850 return isl_stat_ok;
5853 /* Construct a union map mapping the domain of the union
5854 * piecewise multi-affine expression to its range, with each dimension
5855 * in the range equated to the corresponding affine expression on its cell.
5857 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5858 __isl_take isl_union_pw_multi_aff *upma)
5860 isl_space *space;
5861 isl_union_map *umap;
5863 if (!upma)
5864 return NULL;
5866 space = isl_union_pw_multi_aff_get_space(upma);
5867 umap = isl_union_map_empty(space);
5869 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5870 &map_from_pw_multi_aff, &umap) < 0)
5871 goto error;
5873 isl_union_pw_multi_aff_free(upma);
5874 return umap;
5875 error:
5876 isl_union_pw_multi_aff_free(upma);
5877 isl_union_map_free(umap);
5878 return NULL;
5881 /* Local data for bin_entry and the callback "fn".
5883 struct isl_union_pw_multi_aff_bin_data {
5884 isl_union_pw_multi_aff *upma2;
5885 isl_union_pw_multi_aff *res;
5886 isl_pw_multi_aff *pma;
5887 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5890 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5891 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5893 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5895 struct isl_union_pw_multi_aff_bin_data *data = user;
5896 isl_stat r;
5898 data->pma = pma;
5899 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5900 data->fn, data);
5901 isl_pw_multi_aff_free(pma);
5903 return r;
5906 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5907 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5908 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5909 * as *entry. The callback should adjust data->res if desired.
5911 static __isl_give isl_union_pw_multi_aff *bin_op(
5912 __isl_take isl_union_pw_multi_aff *upma1,
5913 __isl_take isl_union_pw_multi_aff *upma2,
5914 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5916 isl_space *space;
5917 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5919 space = isl_union_pw_multi_aff_get_space(upma2);
5920 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5921 space = isl_union_pw_multi_aff_get_space(upma1);
5922 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5924 if (!upma1 || !upma2)
5925 goto error;
5927 data.upma2 = upma2;
5928 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5929 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5930 &bin_entry, &data) < 0)
5931 goto error;
5933 isl_union_pw_multi_aff_free(upma1);
5934 isl_union_pw_multi_aff_free(upma2);
5935 return data.res;
5936 error:
5937 isl_union_pw_multi_aff_free(upma1);
5938 isl_union_pw_multi_aff_free(upma2);
5939 isl_union_pw_multi_aff_free(data.res);
5940 return NULL;
5943 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5944 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5946 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5947 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5949 isl_space *space;
5951 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5952 isl_pw_multi_aff_get_space(pma2));
5953 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5954 &isl_multi_aff_range_product);
5957 /* Given two isl_pw_multi_affs A -> B and C -> D,
5958 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5960 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5961 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5963 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5964 &pw_multi_aff_range_product);
5967 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5968 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5970 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5971 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5973 isl_space *space;
5975 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5976 isl_pw_multi_aff_get_space(pma2));
5977 space = isl_space_flatten_range(space);
5978 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5979 &isl_multi_aff_flat_range_product);
5982 /* Given two isl_pw_multi_affs A -> B and C -> D,
5983 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5985 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5986 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5988 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5989 &pw_multi_aff_flat_range_product);
5992 /* If data->pma and "pma2" have the same domain space, then compute
5993 * their flat range product and the result to data->res.
5995 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5996 void *user)
5998 struct isl_union_pw_multi_aff_bin_data *data = user;
6000 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6001 pma2->dim, isl_dim_in)) {
6002 isl_pw_multi_aff_free(pma2);
6003 return isl_stat_ok;
6006 pma2 = isl_pw_multi_aff_flat_range_product(
6007 isl_pw_multi_aff_copy(data->pma), pma2);
6009 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6011 return isl_stat_ok;
6014 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6015 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6017 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6018 __isl_take isl_union_pw_multi_aff *upma1,
6019 __isl_take isl_union_pw_multi_aff *upma2)
6021 return bin_op(upma1, upma2, &flat_range_product_entry);
6024 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6025 * The parameters are assumed to have been aligned.
6027 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6028 * except that it works on two different isl_pw_* types.
6030 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6031 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6032 __isl_take isl_pw_aff *pa)
6034 int i, j, n;
6035 isl_pw_multi_aff *res = NULL;
6037 if (!pma || !pa)
6038 goto error;
6040 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6041 pa->dim, isl_dim_in))
6042 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6043 "domains don't match", goto error);
6044 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6045 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6046 "index out of bounds", goto error);
6048 n = pma->n * pa->n;
6049 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6051 for (i = 0; i < pma->n; ++i) {
6052 for (j = 0; j < pa->n; ++j) {
6053 isl_set *common;
6054 isl_multi_aff *res_ij;
6055 int empty;
6057 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6058 isl_set_copy(pa->p[j].set));
6059 empty = isl_set_plain_is_empty(common);
6060 if (empty < 0 || empty) {
6061 isl_set_free(common);
6062 if (empty < 0)
6063 goto error;
6064 continue;
6067 res_ij = isl_multi_aff_set_aff(
6068 isl_multi_aff_copy(pma->p[i].maff), pos,
6069 isl_aff_copy(pa->p[j].aff));
6070 res_ij = isl_multi_aff_gist(res_ij,
6071 isl_set_copy(common));
6073 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6077 isl_pw_multi_aff_free(pma);
6078 isl_pw_aff_free(pa);
6079 return res;
6080 error:
6081 isl_pw_multi_aff_free(pma);
6082 isl_pw_aff_free(pa);
6083 return isl_pw_multi_aff_free(res);
6086 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6088 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6089 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6090 __isl_take isl_pw_aff *pa)
6092 if (!pma || !pa)
6093 goto error;
6094 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
6095 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6096 if (!isl_space_has_named_params(pma->dim) ||
6097 !isl_space_has_named_params(pa->dim))
6098 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6099 "unaligned unnamed parameters", goto error);
6100 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6101 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6102 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6103 error:
6104 isl_pw_multi_aff_free(pma);
6105 isl_pw_aff_free(pa);
6106 return NULL;
6109 /* Do the parameters of "pa" match those of "space"?
6111 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6112 __isl_keep isl_space *space)
6114 isl_space *pa_space;
6115 int match;
6117 if (!pa || !space)
6118 return -1;
6120 pa_space = isl_pw_aff_get_space(pa);
6122 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6124 isl_space_free(pa_space);
6125 return match;
6128 /* Check that the domain space of "pa" matches "space".
6130 * Return 0 on success and -1 on error.
6132 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6133 __isl_keep isl_space *space)
6135 isl_space *pa_space;
6136 int match;
6138 if (!pa || !space)
6139 return -1;
6141 pa_space = isl_pw_aff_get_space(pa);
6143 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6144 if (match < 0)
6145 goto error;
6146 if (!match)
6147 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6148 "parameters don't match", goto error);
6149 match = isl_space_tuple_is_equal(space, isl_dim_in,
6150 pa_space, isl_dim_in);
6151 if (match < 0)
6152 goto error;
6153 if (!match)
6154 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6155 "domains don't match", goto error);
6156 isl_space_free(pa_space);
6157 return 0;
6158 error:
6159 isl_space_free(pa_space);
6160 return -1;
6163 #undef BASE
6164 #define BASE pw_aff
6165 #undef DOMBASE
6166 #define DOMBASE set
6168 #include <isl_multi_templ.c>
6169 #include <isl_multi_apply_set.c>
6170 #include <isl_multi_coalesce.c>
6171 #include <isl_multi_gist.c>
6172 #include <isl_multi_hash.c>
6173 #include <isl_multi_intersect.c>
6175 /* Scale the elements of "pma" by the corresponding elements of "mv".
6177 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6178 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6180 int i;
6182 pma = isl_pw_multi_aff_cow(pma);
6183 if (!pma || !mv)
6184 goto error;
6185 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6186 mv->space, isl_dim_set))
6187 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6188 "spaces don't match", goto error);
6189 if (!isl_space_match(pma->dim, isl_dim_param,
6190 mv->space, isl_dim_param)) {
6191 pma = isl_pw_multi_aff_align_params(pma,
6192 isl_multi_val_get_space(mv));
6193 mv = isl_multi_val_align_params(mv,
6194 isl_pw_multi_aff_get_space(pma));
6195 if (!pma || !mv)
6196 goto error;
6199 for (i = 0; i < pma->n; ++i) {
6200 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6201 isl_multi_val_copy(mv));
6202 if (!pma->p[i].maff)
6203 goto error;
6206 isl_multi_val_free(mv);
6207 return pma;
6208 error:
6209 isl_multi_val_free(mv);
6210 isl_pw_multi_aff_free(pma);
6211 return NULL;
6214 /* This function is called for each entry of an isl_union_pw_multi_aff.
6215 * If the space of the entry matches that of data->mv,
6216 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6217 * Otherwise, return an empty isl_pw_multi_aff.
6219 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6220 __isl_take isl_pw_multi_aff *pma, void *user)
6222 isl_multi_val *mv = user;
6224 if (!pma)
6225 return NULL;
6226 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6227 mv->space, isl_dim_set)) {
6228 isl_space *space = isl_pw_multi_aff_get_space(pma);
6229 isl_pw_multi_aff_free(pma);
6230 return isl_pw_multi_aff_empty(space);
6233 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6236 /* Scale the elements of "upma" by the corresponding elements of "mv",
6237 * for those entries that match the space of "mv".
6239 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6240 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6242 upma = isl_union_pw_multi_aff_align_params(upma,
6243 isl_multi_val_get_space(mv));
6244 mv = isl_multi_val_align_params(mv,
6245 isl_union_pw_multi_aff_get_space(upma));
6246 if (!upma || !mv)
6247 goto error;
6249 return isl_union_pw_multi_aff_transform(upma,
6250 &union_pw_multi_aff_scale_multi_val_entry, mv);
6252 isl_multi_val_free(mv);
6253 return upma;
6254 error:
6255 isl_multi_val_free(mv);
6256 isl_union_pw_multi_aff_free(upma);
6257 return NULL;
6260 /* Construct and return a piecewise multi affine expression
6261 * in the given space with value zero in each of the output dimensions and
6262 * a universe domain.
6264 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6266 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6269 /* Construct and return a piecewise multi affine expression
6270 * that is equal to the given piecewise affine expression.
6272 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6273 __isl_take isl_pw_aff *pa)
6275 int i;
6276 isl_space *space;
6277 isl_pw_multi_aff *pma;
6279 if (!pa)
6280 return NULL;
6282 space = isl_pw_aff_get_space(pa);
6283 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6285 for (i = 0; i < pa->n; ++i) {
6286 isl_set *set;
6287 isl_multi_aff *ma;
6289 set = isl_set_copy(pa->p[i].set);
6290 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6291 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6294 isl_pw_aff_free(pa);
6295 return pma;
6298 /* Construct a set or map mapping the shared (parameter) domain
6299 * of the piecewise affine expressions to the range of "mpa"
6300 * with each dimension in the range equated to the
6301 * corresponding piecewise affine expression.
6303 static __isl_give isl_map *map_from_multi_pw_aff(
6304 __isl_take isl_multi_pw_aff *mpa)
6306 int i;
6307 isl_space *space;
6308 isl_map *map;
6310 if (!mpa)
6311 return NULL;
6313 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6314 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6315 "invalid space", goto error);
6317 space = isl_multi_pw_aff_get_domain_space(mpa);
6318 map = isl_map_universe(isl_space_from_domain(space));
6320 for (i = 0; i < mpa->n; ++i) {
6321 isl_pw_aff *pa;
6322 isl_map *map_i;
6324 pa = isl_pw_aff_copy(mpa->p[i]);
6325 map_i = map_from_pw_aff(pa);
6327 map = isl_map_flat_range_product(map, map_i);
6330 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6332 isl_multi_pw_aff_free(mpa);
6333 return map;
6334 error:
6335 isl_multi_pw_aff_free(mpa);
6336 return NULL;
6339 /* Construct a map mapping the shared domain
6340 * of the piecewise affine expressions to the range of "mpa"
6341 * with each dimension in the range equated to the
6342 * corresponding piecewise affine expression.
6344 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6346 if (!mpa)
6347 return NULL;
6348 if (isl_space_is_set(mpa->space))
6349 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6350 "space of input is not a map", goto error);
6352 return map_from_multi_pw_aff(mpa);
6353 error:
6354 isl_multi_pw_aff_free(mpa);
6355 return NULL;
6358 /* Construct a set mapping the shared parameter domain
6359 * of the piecewise affine expressions to the space of "mpa"
6360 * with each dimension in the range equated to the
6361 * corresponding piecewise affine expression.
6363 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6365 if (!mpa)
6366 return NULL;
6367 if (!isl_space_is_set(mpa->space))
6368 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6369 "space of input is not a set", goto error);
6371 return map_from_multi_pw_aff(mpa);
6372 error:
6373 isl_multi_pw_aff_free(mpa);
6374 return NULL;
6377 /* Construct and return a piecewise multi affine expression
6378 * that is equal to the given multi piecewise affine expression
6379 * on the shared domain of the piecewise affine expressions.
6381 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6382 __isl_take isl_multi_pw_aff *mpa)
6384 int i;
6385 isl_space *space;
6386 isl_pw_aff *pa;
6387 isl_pw_multi_aff *pma;
6389 if (!mpa)
6390 return NULL;
6392 space = isl_multi_pw_aff_get_space(mpa);
6394 if (mpa->n == 0) {
6395 isl_multi_pw_aff_free(mpa);
6396 return isl_pw_multi_aff_zero(space);
6399 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6400 pma = isl_pw_multi_aff_from_pw_aff(pa);
6402 for (i = 1; i < mpa->n; ++i) {
6403 isl_pw_multi_aff *pma_i;
6405 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6406 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6407 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6410 pma = isl_pw_multi_aff_reset_space(pma, space);
6412 isl_multi_pw_aff_free(mpa);
6413 return pma;
6416 /* Construct and return a multi piecewise affine expression
6417 * that is equal to the given multi affine expression.
6419 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6420 __isl_take isl_multi_aff *ma)
6422 int i, n;
6423 isl_multi_pw_aff *mpa;
6425 if (!ma)
6426 return NULL;
6428 n = isl_multi_aff_dim(ma, isl_dim_out);
6429 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6431 for (i = 0; i < n; ++i) {
6432 isl_pw_aff *pa;
6434 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6435 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6438 isl_multi_aff_free(ma);
6439 return mpa;
6442 /* Construct and return a multi piecewise affine expression
6443 * that is equal to the given piecewise multi affine expression.
6445 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6446 __isl_take isl_pw_multi_aff *pma)
6448 int i, n;
6449 isl_space *space;
6450 isl_multi_pw_aff *mpa;
6452 if (!pma)
6453 return NULL;
6455 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6456 space = isl_pw_multi_aff_get_space(pma);
6457 mpa = isl_multi_pw_aff_alloc(space);
6459 for (i = 0; i < n; ++i) {
6460 isl_pw_aff *pa;
6462 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6463 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6466 isl_pw_multi_aff_free(pma);
6467 return mpa;
6470 /* Do "pa1" and "pa2" represent the same function?
6472 * We first check if they are obviously equal.
6473 * If not, we convert them to maps and check if those are equal.
6475 * If "pa1" or "pa2" contain any NaNs, then they are considered
6476 * not to be the same. A NaN is not equal to anything, not even
6477 * to another NaN.
6479 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6481 int equal;
6482 isl_bool has_nan;
6483 isl_map *map1, *map2;
6485 if (!pa1 || !pa2)
6486 return -1;
6488 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6489 if (equal < 0 || equal)
6490 return equal;
6491 has_nan = either_involves_nan(pa1, pa2);
6492 if (has_nan < 0)
6493 return -1;
6494 if (has_nan)
6495 return 0;
6497 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6498 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6499 equal = isl_map_is_equal(map1, map2);
6500 isl_map_free(map1);
6501 isl_map_free(map2);
6503 return equal;
6506 /* Do "mpa1" and "mpa2" represent the same function?
6508 * Note that we cannot convert the entire isl_multi_pw_aff
6509 * to a map because the domains of the piecewise affine expressions
6510 * may not be the same.
6512 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6513 __isl_keep isl_multi_pw_aff *mpa2)
6515 int i;
6516 isl_bool equal;
6518 if (!mpa1 || !mpa2)
6519 return isl_bool_error;
6521 if (!isl_space_match(mpa1->space, isl_dim_param,
6522 mpa2->space, isl_dim_param)) {
6523 if (!isl_space_has_named_params(mpa1->space))
6524 return isl_bool_false;
6525 if (!isl_space_has_named_params(mpa2->space))
6526 return isl_bool_false;
6527 mpa1 = isl_multi_pw_aff_copy(mpa1);
6528 mpa2 = isl_multi_pw_aff_copy(mpa2);
6529 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6530 isl_multi_pw_aff_get_space(mpa2));
6531 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6532 isl_multi_pw_aff_get_space(mpa1));
6533 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6534 isl_multi_pw_aff_free(mpa1);
6535 isl_multi_pw_aff_free(mpa2);
6536 return equal;
6539 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6540 if (equal < 0 || !equal)
6541 return equal;
6543 for (i = 0; i < mpa1->n; ++i) {
6544 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6545 if (equal < 0 || !equal)
6546 return equal;
6549 return isl_bool_true;
6552 /* Compute the pullback of "mpa" by the function represented by "ma".
6553 * In other words, plug in "ma" in "mpa".
6555 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6557 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6558 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6560 int i;
6561 isl_space *space = NULL;
6563 mpa = isl_multi_pw_aff_cow(mpa);
6564 if (!mpa || !ma)
6565 goto error;
6567 space = isl_space_join(isl_multi_aff_get_space(ma),
6568 isl_multi_pw_aff_get_space(mpa));
6569 if (!space)
6570 goto error;
6572 for (i = 0; i < mpa->n; ++i) {
6573 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6574 isl_multi_aff_copy(ma));
6575 if (!mpa->p[i])
6576 goto error;
6579 isl_multi_aff_free(ma);
6580 isl_space_free(mpa->space);
6581 mpa->space = space;
6582 return mpa;
6583 error:
6584 isl_space_free(space);
6585 isl_multi_pw_aff_free(mpa);
6586 isl_multi_aff_free(ma);
6587 return NULL;
6590 /* Compute the pullback of "mpa" by the function represented by "ma".
6591 * In other words, plug in "ma" in "mpa".
6593 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6594 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6596 if (!mpa || !ma)
6597 goto error;
6598 if (isl_space_match(mpa->space, isl_dim_param,
6599 ma->space, isl_dim_param))
6600 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6601 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6602 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6603 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6604 error:
6605 isl_multi_pw_aff_free(mpa);
6606 isl_multi_aff_free(ma);
6607 return NULL;
6610 /* Compute the pullback of "mpa" by the function represented by "pma".
6611 * In other words, plug in "pma" in "mpa".
6613 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6615 static __isl_give isl_multi_pw_aff *
6616 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6617 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6619 int i;
6620 isl_space *space = NULL;
6622 mpa = isl_multi_pw_aff_cow(mpa);
6623 if (!mpa || !pma)
6624 goto error;
6626 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6627 isl_multi_pw_aff_get_space(mpa));
6629 for (i = 0; i < mpa->n; ++i) {
6630 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6631 isl_pw_multi_aff_copy(pma));
6632 if (!mpa->p[i])
6633 goto error;
6636 isl_pw_multi_aff_free(pma);
6637 isl_space_free(mpa->space);
6638 mpa->space = space;
6639 return mpa;
6640 error:
6641 isl_space_free(space);
6642 isl_multi_pw_aff_free(mpa);
6643 isl_pw_multi_aff_free(pma);
6644 return NULL;
6647 /* Compute the pullback of "mpa" by the function represented by "pma".
6648 * In other words, plug in "pma" in "mpa".
6650 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6651 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6653 if (!mpa || !pma)
6654 goto error;
6655 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6656 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6657 mpa = isl_multi_pw_aff_align_params(mpa,
6658 isl_pw_multi_aff_get_space(pma));
6659 pma = isl_pw_multi_aff_align_params(pma,
6660 isl_multi_pw_aff_get_space(mpa));
6661 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6662 error:
6663 isl_multi_pw_aff_free(mpa);
6664 isl_pw_multi_aff_free(pma);
6665 return NULL;
6668 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6669 * with the domain of "aff". The domain of the result is the same
6670 * as that of "mpa".
6671 * "mpa" and "aff" are assumed to have been aligned.
6673 * We first extract the parametric constant from "aff", defined
6674 * over the correct domain.
6675 * Then we add the appropriate combinations of the members of "mpa".
6676 * Finally, we add the integer divisions through recursive calls.
6678 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6679 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6681 int i, n_in, n_div;
6682 isl_space *space;
6683 isl_val *v;
6684 isl_pw_aff *pa;
6685 isl_aff *tmp;
6687 n_in = isl_aff_dim(aff, isl_dim_in);
6688 n_div = isl_aff_dim(aff, isl_dim_div);
6690 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6691 tmp = isl_aff_copy(aff);
6692 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6693 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6694 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6695 isl_space_dim(space, isl_dim_set));
6696 tmp = isl_aff_reset_domain_space(tmp, space);
6697 pa = isl_pw_aff_from_aff(tmp);
6699 for (i = 0; i < n_in; ++i) {
6700 isl_pw_aff *pa_i;
6702 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6703 continue;
6704 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6705 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6706 pa_i = isl_pw_aff_scale_val(pa_i, v);
6707 pa = isl_pw_aff_add(pa, pa_i);
6710 for (i = 0; i < n_div; ++i) {
6711 isl_aff *div;
6712 isl_pw_aff *pa_i;
6714 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6715 continue;
6716 div = isl_aff_get_div(aff, i);
6717 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6718 isl_multi_pw_aff_copy(mpa), div);
6719 pa_i = isl_pw_aff_floor(pa_i);
6720 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6721 pa_i = isl_pw_aff_scale_val(pa_i, v);
6722 pa = isl_pw_aff_add(pa, pa_i);
6725 isl_multi_pw_aff_free(mpa);
6726 isl_aff_free(aff);
6728 return pa;
6731 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6732 * with the domain of "aff". The domain of the result is the same
6733 * as that of "mpa".
6735 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6736 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6738 if (!aff || !mpa)
6739 goto error;
6740 if (isl_space_match(aff->ls->dim, isl_dim_param,
6741 mpa->space, isl_dim_param))
6742 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6744 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6745 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6747 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6748 error:
6749 isl_aff_free(aff);
6750 isl_multi_pw_aff_free(mpa);
6751 return NULL;
6754 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6755 * with the domain of "pa". The domain of the result is the same
6756 * as that of "mpa".
6757 * "mpa" and "pa" are assumed to have been aligned.
6759 * We consider each piece in turn. Note that the domains of the
6760 * pieces are assumed to be disjoint and they remain disjoint
6761 * after taking the preimage (over the same function).
6763 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6764 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6766 isl_space *space;
6767 isl_pw_aff *res;
6768 int i;
6770 if (!mpa || !pa)
6771 goto error;
6773 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6774 isl_pw_aff_get_space(pa));
6775 res = isl_pw_aff_empty(space);
6777 for (i = 0; i < pa->n; ++i) {
6778 isl_pw_aff *pa_i;
6779 isl_set *domain;
6781 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6782 isl_multi_pw_aff_copy(mpa),
6783 isl_aff_copy(pa->p[i].aff));
6784 domain = isl_set_copy(pa->p[i].set);
6785 domain = isl_set_preimage_multi_pw_aff(domain,
6786 isl_multi_pw_aff_copy(mpa));
6787 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6788 res = isl_pw_aff_add_disjoint(res, pa_i);
6791 isl_pw_aff_free(pa);
6792 isl_multi_pw_aff_free(mpa);
6793 return res;
6794 error:
6795 isl_pw_aff_free(pa);
6796 isl_multi_pw_aff_free(mpa);
6797 return NULL;
6800 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6801 * with the domain of "pa". The domain of the result is the same
6802 * as that of "mpa".
6804 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6805 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6807 if (!pa || !mpa)
6808 goto error;
6809 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6810 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6812 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6813 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6815 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6816 error:
6817 isl_pw_aff_free(pa);
6818 isl_multi_pw_aff_free(mpa);
6819 return NULL;
6822 /* Compute the pullback of "pa" by the function represented by "mpa".
6823 * In other words, plug in "mpa" in "pa".
6824 * "pa" and "mpa" are assumed to have been aligned.
6826 * The pullback is computed by applying "pa" to "mpa".
6828 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6829 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6831 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6834 /* Compute the pullback of "pa" by the function represented by "mpa".
6835 * In other words, plug in "mpa" in "pa".
6837 * The pullback is computed by applying "pa" to "mpa".
6839 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6840 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6842 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6845 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6846 * In other words, plug in "mpa2" in "mpa1".
6848 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6850 * We pullback each member of "mpa1" in turn.
6852 static __isl_give isl_multi_pw_aff *
6853 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6854 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6856 int i;
6857 isl_space *space = NULL;
6859 mpa1 = isl_multi_pw_aff_cow(mpa1);
6860 if (!mpa1 || !mpa2)
6861 goto error;
6863 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6864 isl_multi_pw_aff_get_space(mpa1));
6866 for (i = 0; i < mpa1->n; ++i) {
6867 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6868 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6869 if (!mpa1->p[i])
6870 goto error;
6873 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6875 isl_multi_pw_aff_free(mpa2);
6876 return mpa1;
6877 error:
6878 isl_space_free(space);
6879 isl_multi_pw_aff_free(mpa1);
6880 isl_multi_pw_aff_free(mpa2);
6881 return NULL;
6884 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6885 * In other words, plug in "mpa2" in "mpa1".
6887 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6888 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6890 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6891 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6894 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6895 * of "mpa1" and "mpa2" live in the same space, construct map space
6896 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6897 * with this map space as extract argument.
6899 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6900 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6901 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6902 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6904 int match;
6905 isl_space *space1, *space2;
6906 isl_map *res;
6908 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6909 isl_multi_pw_aff_get_space(mpa2));
6910 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6911 isl_multi_pw_aff_get_space(mpa1));
6912 if (!mpa1 || !mpa2)
6913 goto error;
6914 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6915 mpa2->space, isl_dim_out);
6916 if (match < 0)
6917 goto error;
6918 if (!match)
6919 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6920 "range spaces don't match", goto error);
6921 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6922 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6923 space1 = isl_space_map_from_domain_and_range(space1, space2);
6925 res = order(mpa1, mpa2, space1);
6926 isl_multi_pw_aff_free(mpa1);
6927 isl_multi_pw_aff_free(mpa2);
6928 return res;
6929 error:
6930 isl_multi_pw_aff_free(mpa1);
6931 isl_multi_pw_aff_free(mpa2);
6932 return NULL;
6935 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6936 * where the function values are equal. "space" is the space of the result.
6937 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6939 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6940 * in the sequences are equal.
6942 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6943 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6944 __isl_take isl_space *space)
6946 int i, n;
6947 isl_map *res;
6949 res = isl_map_universe(space);
6951 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6952 for (i = 0; i < n; ++i) {
6953 isl_pw_aff *pa1, *pa2;
6954 isl_map *map;
6956 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6957 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6958 map = isl_pw_aff_eq_map(pa1, pa2);
6959 res = isl_map_intersect(res, map);
6962 return res;
6965 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6966 * where the function values are equal.
6968 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6969 __isl_take isl_multi_pw_aff *mpa2)
6971 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6972 &isl_multi_pw_aff_eq_map_on_space);
6975 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6976 * where the function values of "mpa1" is lexicographically satisfies "base"
6977 * compared to that of "mpa2". "space" is the space of the result.
6978 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6980 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6981 * if its i-th element satisfies "base" when compared to
6982 * the i-th element of "mpa2" while all previous elements are
6983 * pairwise equal.
6985 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6986 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6987 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6988 __isl_take isl_pw_aff *pa2),
6989 __isl_take isl_space *space)
6991 int i, n;
6992 isl_map *res, *rest;
6994 res = isl_map_empty(isl_space_copy(space));
6995 rest = isl_map_universe(space);
6997 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6998 for (i = 0; i < n; ++i) {
6999 isl_pw_aff *pa1, *pa2;
7000 isl_map *map;
7002 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7003 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7004 map = base(pa1, pa2);
7005 map = isl_map_intersect(map, isl_map_copy(rest));
7006 res = isl_map_union(res, map);
7008 if (i == n - 1)
7009 continue;
7011 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7012 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7013 map = isl_pw_aff_eq_map(pa1, pa2);
7014 rest = isl_map_intersect(rest, map);
7017 isl_map_free(rest);
7018 return res;
7021 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7022 * where the function value of "mpa1" is lexicographically less than that
7023 * of "mpa2". "space" is the space of the result.
7024 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7026 * "mpa1" is less than "mpa2" if its i-th element is smaller
7027 * than the i-th element of "mpa2" while all previous elements are
7028 * pairwise equal.
7030 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7031 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7032 __isl_take isl_space *space)
7034 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7035 &isl_pw_aff_lt_map, space);
7038 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7039 * where the function value of "mpa1" is lexicographically less than that
7040 * of "mpa2".
7042 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7043 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7045 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7046 &isl_multi_pw_aff_lex_lt_map_on_space);
7049 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7050 * where the function value of "mpa1" is lexicographically greater than that
7051 * of "mpa2". "space" is the space of the result.
7052 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7054 * "mpa1" is greater than "mpa2" if its i-th element is greater
7055 * than the i-th element of "mpa2" while all previous elements are
7056 * pairwise equal.
7058 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7059 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7060 __isl_take isl_space *space)
7062 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7063 &isl_pw_aff_gt_map, space);
7066 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7067 * where the function value of "mpa1" is lexicographically greater than that
7068 * of "mpa2".
7070 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7071 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7073 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7074 &isl_multi_pw_aff_lex_gt_map_on_space);
7077 /* Compare two isl_affs.
7079 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7080 * than "aff2" and 0 if they are equal.
7082 * The order is fairly arbitrary. We do consider expressions that only involve
7083 * earlier dimensions as "smaller".
7085 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7087 int cmp;
7088 int last1, last2;
7090 if (aff1 == aff2)
7091 return 0;
7093 if (!aff1)
7094 return -1;
7095 if (!aff2)
7096 return 1;
7098 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7099 if (cmp != 0)
7100 return cmp;
7102 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7103 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7104 if (last1 != last2)
7105 return last1 - last2;
7107 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7110 /* Compare two isl_pw_affs.
7112 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7113 * than "pa2" and 0 if they are equal.
7115 * The order is fairly arbitrary. We do consider expressions that only involve
7116 * earlier dimensions as "smaller".
7118 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7119 __isl_keep isl_pw_aff *pa2)
7121 int i;
7122 int cmp;
7124 if (pa1 == pa2)
7125 return 0;
7127 if (!pa1)
7128 return -1;
7129 if (!pa2)
7130 return 1;
7132 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7133 if (cmp != 0)
7134 return cmp;
7136 if (pa1->n != pa2->n)
7137 return pa1->n - pa2->n;
7139 for (i = 0; i < pa1->n; ++i) {
7140 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7141 if (cmp != 0)
7142 return cmp;
7143 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7144 if (cmp != 0)
7145 return cmp;
7148 return 0;
7151 /* Return a piecewise affine expression that is equal to "v" on "domain".
7153 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7154 __isl_take isl_val *v)
7156 isl_space *space;
7157 isl_local_space *ls;
7158 isl_aff *aff;
7160 space = isl_set_get_space(domain);
7161 ls = isl_local_space_from_space(space);
7162 aff = isl_aff_val_on_domain(ls, v);
7164 return isl_pw_aff_alloc(domain, aff);
7167 /* Return a multi affine expression that is equal to "mv" on domain
7168 * space "space".
7170 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7171 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7173 int i, n;
7174 isl_space *space2;
7175 isl_local_space *ls;
7176 isl_multi_aff *ma;
7178 if (!space || !mv)
7179 goto error;
7181 n = isl_multi_val_dim(mv, isl_dim_set);
7182 space2 = isl_multi_val_get_space(mv);
7183 space2 = isl_space_align_params(space2, isl_space_copy(space));
7184 space = isl_space_align_params(space, isl_space_copy(space2));
7185 space = isl_space_map_from_domain_and_range(space, space2);
7186 ma = isl_multi_aff_alloc(isl_space_copy(space));
7187 ls = isl_local_space_from_space(isl_space_domain(space));
7188 for (i = 0; i < n; ++i) {
7189 isl_val *v;
7190 isl_aff *aff;
7192 v = isl_multi_val_get_val(mv, i);
7193 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7194 ma = isl_multi_aff_set_aff(ma, i, aff);
7196 isl_local_space_free(ls);
7198 isl_multi_val_free(mv);
7199 return ma;
7200 error:
7201 isl_space_free(space);
7202 isl_multi_val_free(mv);
7203 return NULL;
7206 /* Return a piecewise multi-affine expression
7207 * that is equal to "mv" on "domain".
7209 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7210 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7212 isl_space *space;
7213 isl_multi_aff *ma;
7215 space = isl_set_get_space(domain);
7216 ma = isl_multi_aff_multi_val_on_space(space, mv);
7218 return isl_pw_multi_aff_alloc(domain, ma);
7221 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7222 * mv is the value that should be attained on each domain set
7223 * res collects the results
7225 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7226 isl_multi_val *mv;
7227 isl_union_pw_multi_aff *res;
7230 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7231 * and add it to data->res.
7233 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7234 void *user)
7236 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7237 isl_pw_multi_aff *pma;
7238 isl_multi_val *mv;
7240 mv = isl_multi_val_copy(data->mv);
7241 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7242 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7244 return data->res ? isl_stat_ok : isl_stat_error;
7247 /* Return a union piecewise multi-affine expression
7248 * that is equal to "mv" on "domain".
7250 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7251 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7253 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7254 isl_space *space;
7256 space = isl_union_set_get_space(domain);
7257 data.res = isl_union_pw_multi_aff_empty(space);
7258 data.mv = mv;
7259 if (isl_union_set_foreach_set(domain,
7260 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7261 data.res = isl_union_pw_multi_aff_free(data.res);
7262 isl_union_set_free(domain);
7263 isl_multi_val_free(mv);
7264 return data.res;
7267 /* Compute the pullback of data->pma by the function represented by "pma2",
7268 * provided the spaces match, and add the results to data->res.
7270 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7272 struct isl_union_pw_multi_aff_bin_data *data = user;
7274 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7275 pma2->dim, isl_dim_out)) {
7276 isl_pw_multi_aff_free(pma2);
7277 return isl_stat_ok;
7280 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7281 isl_pw_multi_aff_copy(data->pma), pma2);
7283 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7284 if (!data->res)
7285 return isl_stat_error;
7287 return isl_stat_ok;
7290 /* Compute the pullback of "upma1" by the function represented by "upma2".
7292 __isl_give isl_union_pw_multi_aff *
7293 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7294 __isl_take isl_union_pw_multi_aff *upma1,
7295 __isl_take isl_union_pw_multi_aff *upma2)
7297 return bin_op(upma1, upma2, &pullback_entry);
7300 /* Check that the domain space of "upa" matches "space".
7302 * Return 0 on success and -1 on error.
7304 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7305 * can in principle never fail since the space "space" is that
7306 * of the isl_multi_union_pw_aff and is a set space such that
7307 * there is no domain space to match.
7309 * We check the parameters and double-check that "space" is
7310 * indeed that of a set.
7312 static int isl_union_pw_aff_check_match_domain_space(
7313 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7315 isl_space *upa_space;
7316 int match;
7318 if (!upa || !space)
7319 return -1;
7321 match = isl_space_is_set(space);
7322 if (match < 0)
7323 return -1;
7324 if (!match)
7325 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7326 "expecting set space", return -1);
7328 upa_space = isl_union_pw_aff_get_space(upa);
7329 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7330 if (match < 0)
7331 goto error;
7332 if (!match)
7333 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7334 "parameters don't match", goto error);
7336 isl_space_free(upa_space);
7337 return 0;
7338 error:
7339 isl_space_free(upa_space);
7340 return -1;
7343 /* Do the parameters of "upa" match those of "space"?
7345 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7346 __isl_keep isl_space *space)
7348 isl_space *upa_space;
7349 int match;
7351 if (!upa || !space)
7352 return -1;
7354 upa_space = isl_union_pw_aff_get_space(upa);
7356 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7358 isl_space_free(upa_space);
7359 return match;
7362 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7363 * space represents the new parameters.
7364 * res collects the results.
7366 struct isl_union_pw_aff_reset_params_data {
7367 isl_space *space;
7368 isl_union_pw_aff *res;
7371 /* Replace the parameters of "pa" by data->space and
7372 * add the result to data->res.
7374 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7376 struct isl_union_pw_aff_reset_params_data *data = user;
7377 isl_space *space;
7379 space = isl_pw_aff_get_space(pa);
7380 space = isl_space_replace(space, isl_dim_param, data->space);
7381 pa = isl_pw_aff_reset_space(pa, space);
7382 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7384 return data->res ? isl_stat_ok : isl_stat_error;
7387 /* Replace the domain space of "upa" by "space".
7388 * Since a union expression does not have a (single) domain space,
7389 * "space" is necessarily a parameter space.
7391 * Since the order and the names of the parameters determine
7392 * the hash value, we need to create a new hash table.
7394 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7395 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7397 struct isl_union_pw_aff_reset_params_data data = { space };
7398 int match;
7400 match = isl_union_pw_aff_matching_params(upa, space);
7401 if (match < 0)
7402 upa = isl_union_pw_aff_free(upa);
7403 else if (match) {
7404 isl_space_free(space);
7405 return upa;
7408 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7409 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7410 data.res = isl_union_pw_aff_free(data.res);
7412 isl_union_pw_aff_free(upa);
7413 isl_space_free(space);
7414 return data.res;
7417 /* Return the floor of "pa".
7419 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7421 return isl_pw_aff_floor(pa);
7424 /* Given f, return floor(f).
7426 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7427 __isl_take isl_union_pw_aff *upa)
7429 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7432 /* Compute
7434 * upa mod m = upa - m * floor(upa/m)
7436 * with m an integer value.
7438 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7439 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7441 isl_union_pw_aff *res;
7443 if (!upa || !m)
7444 goto error;
7446 if (!isl_val_is_int(m))
7447 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7448 "expecting integer modulo", goto error);
7449 if (!isl_val_is_pos(m))
7450 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7451 "expecting positive modulo", goto error);
7453 res = isl_union_pw_aff_copy(upa);
7454 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7455 upa = isl_union_pw_aff_floor(upa);
7456 upa = isl_union_pw_aff_scale_val(upa, m);
7457 res = isl_union_pw_aff_sub(res, upa);
7459 return res;
7460 error:
7461 isl_val_free(m);
7462 isl_union_pw_aff_free(upa);
7463 return NULL;
7466 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7467 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7468 * needs to attain.
7469 * "res" collects the results.
7471 struct isl_union_pw_aff_aff_on_domain_data {
7472 isl_aff *aff;
7473 isl_union_pw_aff *res;
7476 /* Construct a piecewise affine expression that is equal to data->aff
7477 * on "domain" and add the result to data->res.
7479 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7481 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7482 isl_pw_aff *pa;
7483 isl_aff *aff;
7484 int dim;
7486 aff = isl_aff_copy(data->aff);
7487 dim = isl_set_dim(domain, isl_dim_set);
7488 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7489 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7490 pa = isl_pw_aff_alloc(domain, aff);
7491 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7493 return data->res ? isl_stat_ok : isl_stat_error;
7496 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7497 * pos is the output position that needs to be extracted.
7498 * res collects the results.
7500 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7501 int pos;
7502 isl_union_pw_aff *res;
7505 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7506 * (assuming it has such a dimension) and add it to data->res.
7508 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7510 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7511 int n_out;
7512 isl_pw_aff *pa;
7514 if (!pma)
7515 return isl_stat_error;
7517 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7518 if (data->pos >= n_out) {
7519 isl_pw_multi_aff_free(pma);
7520 return isl_stat_ok;
7523 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7524 isl_pw_multi_aff_free(pma);
7526 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7528 return data->res ? isl_stat_ok : isl_stat_error;
7531 /* Extract an isl_union_pw_aff corresponding to
7532 * output dimension "pos" of "upma".
7534 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7535 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7537 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7538 isl_space *space;
7540 if (!upma)
7541 return NULL;
7543 if (pos < 0)
7544 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7545 "cannot extract at negative position", return NULL);
7547 space = isl_union_pw_multi_aff_get_space(upma);
7548 data.res = isl_union_pw_aff_empty(space);
7549 data.pos = pos;
7550 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7551 &get_union_pw_aff, &data) < 0)
7552 data.res = isl_union_pw_aff_free(data.res);
7554 return data.res;
7557 /* Return a union piecewise affine expression
7558 * that is equal to "aff" on "domain".
7560 * Construct an isl_pw_aff on each of the sets in "domain" and
7561 * collect the results.
7563 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7564 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7566 struct isl_union_pw_aff_aff_on_domain_data data;
7567 isl_space *space;
7569 if (!domain || !aff)
7570 goto error;
7571 if (!isl_local_space_is_params(aff->ls))
7572 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7573 "expecting parametric expression", goto error);
7575 space = isl_union_set_get_space(domain);
7576 data.res = isl_union_pw_aff_empty(space);
7577 data.aff = aff;
7578 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7579 data.res = isl_union_pw_aff_free(data.res);
7580 isl_union_set_free(domain);
7581 isl_aff_free(aff);
7582 return data.res;
7583 error:
7584 isl_union_set_free(domain);
7585 isl_aff_free(aff);
7586 return NULL;
7589 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7590 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7591 * "res" collects the results.
7593 struct isl_union_pw_aff_val_on_domain_data {
7594 isl_val *v;
7595 isl_union_pw_aff *res;
7598 /* Construct a piecewise affine expression that is equal to data->v
7599 * on "domain" and add the result to data->res.
7601 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7603 struct isl_union_pw_aff_val_on_domain_data *data = user;
7604 isl_pw_aff *pa;
7605 isl_val *v;
7607 v = isl_val_copy(data->v);
7608 pa = isl_pw_aff_val_on_domain(domain, v);
7609 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7611 return data->res ? isl_stat_ok : isl_stat_error;
7614 /* Return a union piecewise affine expression
7615 * that is equal to "v" on "domain".
7617 * Construct an isl_pw_aff on each of the sets in "domain" and
7618 * collect the results.
7620 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7621 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7623 struct isl_union_pw_aff_val_on_domain_data data;
7624 isl_space *space;
7626 space = isl_union_set_get_space(domain);
7627 data.res = isl_union_pw_aff_empty(space);
7628 data.v = v;
7629 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7630 data.res = isl_union_pw_aff_free(data.res);
7631 isl_union_set_free(domain);
7632 isl_val_free(v);
7633 return data.res;
7636 /* Construct a piecewise multi affine expression
7637 * that is equal to "pa" and add it to upma.
7639 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7640 void *user)
7642 isl_union_pw_multi_aff **upma = user;
7643 isl_pw_multi_aff *pma;
7645 pma = isl_pw_multi_aff_from_pw_aff(pa);
7646 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7648 return *upma ? isl_stat_ok : isl_stat_error;
7651 /* Construct and return a union piecewise multi affine expression
7652 * that is equal to the given union piecewise affine expression.
7654 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7655 __isl_take isl_union_pw_aff *upa)
7657 isl_space *space;
7658 isl_union_pw_multi_aff *upma;
7660 if (!upa)
7661 return NULL;
7663 space = isl_union_pw_aff_get_space(upa);
7664 upma = isl_union_pw_multi_aff_empty(space);
7666 if (isl_union_pw_aff_foreach_pw_aff(upa,
7667 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7668 upma = isl_union_pw_multi_aff_free(upma);
7670 isl_union_pw_aff_free(upa);
7671 return upma;
7674 /* Compute the set of elements in the domain of "pa" where it is zero and
7675 * add this set to "uset".
7677 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7679 isl_union_set **uset = (isl_union_set **)user;
7681 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7683 return *uset ? isl_stat_ok : isl_stat_error;
7686 /* Return a union set containing those elements in the domain
7687 * of "upa" where it is zero.
7689 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7690 __isl_take isl_union_pw_aff *upa)
7692 isl_union_set *zero;
7694 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7695 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7696 zero = isl_union_set_free(zero);
7698 isl_union_pw_aff_free(upa);
7699 return zero;
7702 /* Convert "pa" to an isl_map and add it to *umap.
7704 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7706 isl_union_map **umap = user;
7707 isl_map *map;
7709 map = isl_map_from_pw_aff(pa);
7710 *umap = isl_union_map_add_map(*umap, map);
7712 return *umap ? isl_stat_ok : isl_stat_error;
7715 /* Construct a union map mapping the domain of the union
7716 * piecewise affine expression to its range, with the single output dimension
7717 * equated to the corresponding affine expressions on their cells.
7719 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7720 __isl_take isl_union_pw_aff *upa)
7722 isl_space *space;
7723 isl_union_map *umap;
7725 if (!upa)
7726 return NULL;
7728 space = isl_union_pw_aff_get_space(upa);
7729 umap = isl_union_map_empty(space);
7731 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7732 &umap) < 0)
7733 umap = isl_union_map_free(umap);
7735 isl_union_pw_aff_free(upa);
7736 return umap;
7739 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7740 * upma is the function that is plugged in.
7741 * pa is the current part of the function in which upma is plugged in.
7742 * res collects the results.
7744 struct isl_union_pw_aff_pullback_upma_data {
7745 isl_union_pw_multi_aff *upma;
7746 isl_pw_aff *pa;
7747 isl_union_pw_aff *res;
7750 /* Check if "pma" can be plugged into data->pa.
7751 * If so, perform the pullback and add the result to data->res.
7753 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7755 struct isl_union_pw_aff_pullback_upma_data *data = user;
7756 isl_pw_aff *pa;
7758 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7759 pma->dim, isl_dim_out)) {
7760 isl_pw_multi_aff_free(pma);
7761 return isl_stat_ok;
7764 pa = isl_pw_aff_copy(data->pa);
7765 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7767 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7769 return data->res ? isl_stat_ok : isl_stat_error;
7772 /* Check if any of the elements of data->upma can be plugged into pa,
7773 * add if so add the result to data->res.
7775 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7777 struct isl_union_pw_aff_pullback_upma_data *data = user;
7778 isl_stat r;
7780 data->pa = pa;
7781 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7782 &pa_pb_pma, data);
7783 isl_pw_aff_free(pa);
7785 return r;
7788 /* Compute the pullback of "upa" by the function represented by "upma".
7789 * In other words, plug in "upma" in "upa". The result contains
7790 * expressions defined over the domain space of "upma".
7792 * Run over all pairs of elements in "upa" and "upma", perform
7793 * the pullback when appropriate and collect the results.
7794 * If the hash value were based on the domain space rather than
7795 * the function space, then we could run through all elements
7796 * of "upma" and directly pick out the corresponding element of "upa".
7798 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7799 __isl_take isl_union_pw_aff *upa,
7800 __isl_take isl_union_pw_multi_aff *upma)
7802 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7803 isl_space *space;
7805 space = isl_union_pw_multi_aff_get_space(upma);
7806 upa = isl_union_pw_aff_align_params(upa, space);
7807 space = isl_union_pw_aff_get_space(upa);
7808 upma = isl_union_pw_multi_aff_align_params(upma, space);
7810 if (!upa || !upma)
7811 goto error;
7813 data.upma = upma;
7814 data.res = isl_union_pw_aff_alloc_same_size(upa);
7815 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7816 data.res = isl_union_pw_aff_free(data.res);
7818 isl_union_pw_aff_free(upa);
7819 isl_union_pw_multi_aff_free(upma);
7820 return data.res;
7821 error:
7822 isl_union_pw_aff_free(upa);
7823 isl_union_pw_multi_aff_free(upma);
7824 return NULL;
7827 #undef BASE
7828 #define BASE union_pw_aff
7829 #undef DOMBASE
7830 #define DOMBASE union_set
7832 #define NO_MOVE_DIMS
7833 #define NO_DIMS
7834 #define NO_DOMAIN
7835 #define NO_PRODUCT
7836 #define NO_SPLICE
7837 #define NO_ZERO
7838 #define NO_IDENTITY
7839 #define NO_GIST
7841 #include <isl_multi_templ.c>
7842 #include <isl_multi_apply_set.c>
7843 #include <isl_multi_apply_union_set.c>
7844 #include <isl_multi_coalesce.c>
7845 #include <isl_multi_floor.c>
7846 #include <isl_multi_gist.c>
7847 #include <isl_multi_intersect.c>
7849 /* Construct a multiple union piecewise affine expression
7850 * in the given space with value zero in each of the output dimensions.
7852 * Since there is no canonical zero value for
7853 * a union piecewise affine expression, we can only construct
7854 * zero-dimensional "zero" value.
7856 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7857 __isl_take isl_space *space)
7859 if (!space)
7860 return NULL;
7862 if (!isl_space_is_set(space))
7863 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7864 "expecting set space", goto error);
7865 if (isl_space_dim(space , isl_dim_out) != 0)
7866 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7867 "expecting 0D space", goto error);
7869 return isl_multi_union_pw_aff_alloc(space);
7870 error:
7871 isl_space_free(space);
7872 return NULL;
7875 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7876 * with the actual sum on the shared domain and
7877 * the defined expression on the symmetric difference of the domains.
7879 * We simply iterate over the elements in both arguments and
7880 * call isl_union_pw_aff_union_add on each of them.
7882 static __isl_give isl_multi_union_pw_aff *
7883 isl_multi_union_pw_aff_union_add_aligned(
7884 __isl_take isl_multi_union_pw_aff *mupa1,
7885 __isl_take isl_multi_union_pw_aff *mupa2)
7887 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7888 &isl_union_pw_aff_union_add);
7891 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7892 * with the actual sum on the shared domain and
7893 * the defined expression on the symmetric difference of the domains.
7895 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7896 __isl_take isl_multi_union_pw_aff *mupa1,
7897 __isl_take isl_multi_union_pw_aff *mupa2)
7899 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7900 &isl_multi_union_pw_aff_union_add_aligned);
7903 /* Construct and return a multi union piecewise affine expression
7904 * that is equal to the given multi affine expression.
7906 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7907 __isl_take isl_multi_aff *ma)
7909 isl_multi_pw_aff *mpa;
7911 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7912 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7915 /* Construct and return a multi union piecewise affine expression
7916 * that is equal to the given multi piecewise affine expression.
7918 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7919 __isl_take isl_multi_pw_aff *mpa)
7921 int i, n;
7922 isl_space *space;
7923 isl_multi_union_pw_aff *mupa;
7925 if (!mpa)
7926 return NULL;
7928 space = isl_multi_pw_aff_get_space(mpa);
7929 space = isl_space_range(space);
7930 mupa = isl_multi_union_pw_aff_alloc(space);
7932 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7933 for (i = 0; i < n; ++i) {
7934 isl_pw_aff *pa;
7935 isl_union_pw_aff *upa;
7937 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7938 upa = isl_union_pw_aff_from_pw_aff(pa);
7939 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7942 isl_multi_pw_aff_free(mpa);
7944 return mupa;
7947 /* Extract the range space of "pma" and assign it to *space.
7948 * If *space has already been set (through a previous call to this function),
7949 * then check that the range space is the same.
7951 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7953 isl_space **space = user;
7954 isl_space *pma_space;
7955 isl_bool equal;
7957 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7958 isl_pw_multi_aff_free(pma);
7960 if (!pma_space)
7961 return isl_stat_error;
7962 if (!*space) {
7963 *space = pma_space;
7964 return isl_stat_ok;
7967 equal = isl_space_is_equal(pma_space, *space);
7968 isl_space_free(pma_space);
7970 if (equal < 0)
7971 return isl_stat_error;
7972 if (!equal)
7973 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7974 "range spaces not the same", return isl_stat_error);
7975 return isl_stat_ok;
7978 /* Construct and return a multi union piecewise affine expression
7979 * that is equal to the given union piecewise multi affine expression.
7981 * In order to be able to perform the conversion, the input
7982 * needs to be non-empty and may only involve a single range space.
7984 __isl_give isl_multi_union_pw_aff *
7985 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7986 __isl_take isl_union_pw_multi_aff *upma)
7988 isl_space *space = NULL;
7989 isl_multi_union_pw_aff *mupa;
7990 int i, n;
7992 if (!upma)
7993 return NULL;
7994 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7995 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7996 "cannot extract range space from empty input",
7997 goto error);
7998 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7999 &space) < 0)
8000 goto error;
8002 if (!space)
8003 goto error;
8005 n = isl_space_dim(space, isl_dim_set);
8006 mupa = isl_multi_union_pw_aff_alloc(space);
8008 for (i = 0; i < n; ++i) {
8009 isl_union_pw_aff *upa;
8011 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8012 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8015 isl_union_pw_multi_aff_free(upma);
8016 return mupa;
8017 error:
8018 isl_space_free(space);
8019 isl_union_pw_multi_aff_free(upma);
8020 return NULL;
8023 /* Try and create an isl_multi_union_pw_aff that is equivalent
8024 * to the given isl_union_map.
8025 * The isl_union_map is required to be single-valued in each space.
8026 * Moreover, it cannot be empty and all range spaces need to be the same.
8027 * Otherwise, an error is produced.
8029 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8030 __isl_take isl_union_map *umap)
8032 isl_union_pw_multi_aff *upma;
8034 upma = isl_union_pw_multi_aff_from_union_map(umap);
8035 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8038 /* Return a multiple union piecewise affine expression
8039 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8040 * have been aligned.
8042 static __isl_give isl_multi_union_pw_aff *
8043 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8044 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8046 int i, n;
8047 isl_space *space;
8048 isl_multi_union_pw_aff *mupa;
8050 if (!domain || !mv)
8051 goto error;
8053 n = isl_multi_val_dim(mv, isl_dim_set);
8054 space = isl_multi_val_get_space(mv);
8055 mupa = isl_multi_union_pw_aff_alloc(space);
8056 for (i = 0; i < n; ++i) {
8057 isl_val *v;
8058 isl_union_pw_aff *upa;
8060 v = isl_multi_val_get_val(mv, i);
8061 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8063 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8066 isl_union_set_free(domain);
8067 isl_multi_val_free(mv);
8068 return mupa;
8069 error:
8070 isl_union_set_free(domain);
8071 isl_multi_val_free(mv);
8072 return NULL;
8075 /* Return a multiple union piecewise affine expression
8076 * that is equal to "mv" on "domain".
8078 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8079 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8081 if (!domain || !mv)
8082 goto error;
8083 if (isl_space_match(domain->dim, isl_dim_param,
8084 mv->space, isl_dim_param))
8085 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8086 domain, mv);
8087 domain = isl_union_set_align_params(domain,
8088 isl_multi_val_get_space(mv));
8089 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8090 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8091 error:
8092 isl_union_set_free(domain);
8093 isl_multi_val_free(mv);
8094 return NULL;
8097 /* Return a multiple union piecewise affine expression
8098 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8099 * have been aligned.
8101 static __isl_give isl_multi_union_pw_aff *
8102 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8103 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8105 int i, n;
8106 isl_space *space;
8107 isl_multi_union_pw_aff *mupa;
8109 if (!domain || !ma)
8110 goto error;
8112 n = isl_multi_aff_dim(ma, isl_dim_set);
8113 space = isl_multi_aff_get_space(ma);
8114 mupa = isl_multi_union_pw_aff_alloc(space);
8115 for (i = 0; i < n; ++i) {
8116 isl_aff *aff;
8117 isl_union_pw_aff *upa;
8119 aff = isl_multi_aff_get_aff(ma, i);
8120 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8121 aff);
8122 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8125 isl_union_set_free(domain);
8126 isl_multi_aff_free(ma);
8127 return mupa;
8128 error:
8129 isl_union_set_free(domain);
8130 isl_multi_aff_free(ma);
8131 return NULL;
8134 /* Return a multiple union piecewise affine expression
8135 * that is equal to "ma" on "domain".
8137 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8138 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8140 if (!domain || !ma)
8141 goto error;
8142 if (isl_space_match(domain->dim, isl_dim_param,
8143 ma->space, isl_dim_param))
8144 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8145 domain, ma);
8146 domain = isl_union_set_align_params(domain,
8147 isl_multi_aff_get_space(ma));
8148 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8149 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8150 error:
8151 isl_union_set_free(domain);
8152 isl_multi_aff_free(ma);
8153 return NULL;
8156 /* Return a union set containing those elements in the domains
8157 * of the elements of "mupa" where they are all zero.
8159 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8160 __isl_take isl_multi_union_pw_aff *mupa)
8162 int i, n;
8163 isl_union_pw_aff *upa;
8164 isl_union_set *zero;
8166 if (!mupa)
8167 return NULL;
8169 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8170 if (n == 0)
8171 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8172 "cannot determine zero set "
8173 "of zero-dimensional function", goto error);
8175 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8176 zero = isl_union_pw_aff_zero_union_set(upa);
8178 for (i = 1; i < n; ++i) {
8179 isl_union_set *zero_i;
8181 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8182 zero_i = isl_union_pw_aff_zero_union_set(upa);
8184 zero = isl_union_set_intersect(zero, zero_i);
8187 isl_multi_union_pw_aff_free(mupa);
8188 return zero;
8189 error:
8190 isl_multi_union_pw_aff_free(mupa);
8191 return NULL;
8194 /* Construct a union map mapping the shared domain
8195 * of the union piecewise affine expressions to the range of "mupa"
8196 * with each dimension in the range equated to the
8197 * corresponding union piecewise affine expression.
8199 * The input cannot be zero-dimensional as there is
8200 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8202 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8203 __isl_take isl_multi_union_pw_aff *mupa)
8205 int i, n;
8206 isl_space *space;
8207 isl_union_map *umap;
8208 isl_union_pw_aff *upa;
8210 if (!mupa)
8211 return NULL;
8213 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8214 if (n == 0)
8215 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8216 "cannot determine domain of zero-dimensional "
8217 "isl_multi_union_pw_aff", goto error);
8219 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8220 umap = isl_union_map_from_union_pw_aff(upa);
8222 for (i = 1; i < n; ++i) {
8223 isl_union_map *umap_i;
8225 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8226 umap_i = isl_union_map_from_union_pw_aff(upa);
8227 umap = isl_union_map_flat_range_product(umap, umap_i);
8230 space = isl_multi_union_pw_aff_get_space(mupa);
8231 umap = isl_union_map_reset_range_space(umap, space);
8233 isl_multi_union_pw_aff_free(mupa);
8234 return umap;
8235 error:
8236 isl_multi_union_pw_aff_free(mupa);
8237 return NULL;
8240 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8241 * "range" is the space from which to set the range space.
8242 * "res" collects the results.
8244 struct isl_union_pw_multi_aff_reset_range_space_data {
8245 isl_space *range;
8246 isl_union_pw_multi_aff *res;
8249 /* Replace the range space of "pma" by the range space of data->range and
8250 * add the result to data->res.
8252 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8254 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8255 isl_space *space;
8257 space = isl_pw_multi_aff_get_space(pma);
8258 space = isl_space_domain(space);
8259 space = isl_space_extend_domain_with_range(space,
8260 isl_space_copy(data->range));
8261 pma = isl_pw_multi_aff_reset_space(pma, space);
8262 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8264 return data->res ? isl_stat_ok : isl_stat_error;
8267 /* Replace the range space of all the piecewise affine expressions in "upma" by
8268 * the range space of "space".
8270 * This assumes that all these expressions have the same output dimension.
8272 * Since the spaces of the expressions change, so do their hash values.
8273 * We therefore need to create a new isl_union_pw_multi_aff.
8274 * Note that the hash value is currently computed based on the entire
8275 * space even though there can only be a single expression with a given
8276 * domain space.
8278 static __isl_give isl_union_pw_multi_aff *
8279 isl_union_pw_multi_aff_reset_range_space(
8280 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8282 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8283 isl_space *space_upma;
8285 space_upma = isl_union_pw_multi_aff_get_space(upma);
8286 data.res = isl_union_pw_multi_aff_empty(space_upma);
8287 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8288 &reset_range_space, &data) < 0)
8289 data.res = isl_union_pw_multi_aff_free(data.res);
8291 isl_space_free(space);
8292 isl_union_pw_multi_aff_free(upma);
8293 return data.res;
8296 /* Construct and return a union piecewise multi affine expression
8297 * that is equal to the given multi union piecewise affine expression.
8299 * In order to be able to perform the conversion, the input
8300 * needs to have a least one output dimension.
8302 __isl_give isl_union_pw_multi_aff *
8303 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8304 __isl_take isl_multi_union_pw_aff *mupa)
8306 int i, n;
8307 isl_space *space;
8308 isl_union_pw_multi_aff *upma;
8309 isl_union_pw_aff *upa;
8311 if (!mupa)
8312 return NULL;
8314 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8315 if (n == 0)
8316 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8317 "cannot determine domain of zero-dimensional "
8318 "isl_multi_union_pw_aff", goto error);
8320 space = isl_multi_union_pw_aff_get_space(mupa);
8321 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8322 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8324 for (i = 1; i < n; ++i) {
8325 isl_union_pw_multi_aff *upma_i;
8327 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8328 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8329 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8332 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8334 isl_multi_union_pw_aff_free(mupa);
8335 return upma;
8336 error:
8337 isl_multi_union_pw_aff_free(mupa);
8338 return NULL;
8341 /* Intersect the range of "mupa" with "range".
8342 * That is, keep only those domain elements that have a function value
8343 * in "range".
8345 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8346 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8348 isl_union_pw_multi_aff *upma;
8349 isl_union_set *domain;
8350 isl_space *space;
8351 int n;
8352 int match;
8354 if (!mupa || !range)
8355 goto error;
8357 space = isl_set_get_space(range);
8358 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8359 space, isl_dim_set);
8360 isl_space_free(space);
8361 if (match < 0)
8362 goto error;
8363 if (!match)
8364 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8365 "space don't match", goto error);
8366 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8367 if (n == 0)
8368 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8369 "cannot intersect range of zero-dimensional "
8370 "isl_multi_union_pw_aff", goto error);
8372 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8373 isl_multi_union_pw_aff_copy(mupa));
8374 domain = isl_union_set_from_set(range);
8375 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8376 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8378 return mupa;
8379 error:
8380 isl_multi_union_pw_aff_free(mupa);
8381 isl_set_free(range);
8382 return NULL;
8385 /* Return the shared domain of the elements of "mupa".
8387 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8388 __isl_take isl_multi_union_pw_aff *mupa)
8390 int i, n;
8391 isl_union_pw_aff *upa;
8392 isl_union_set *dom;
8394 if (!mupa)
8395 return NULL;
8397 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8398 if (n == 0)
8399 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8400 "cannot determine domain", goto error);
8402 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8403 dom = isl_union_pw_aff_domain(upa);
8404 for (i = 1; i < n; ++i) {
8405 isl_union_set *dom_i;
8407 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8408 dom_i = isl_union_pw_aff_domain(upa);
8409 dom = isl_union_set_intersect(dom, dom_i);
8412 isl_multi_union_pw_aff_free(mupa);
8413 return dom;
8414 error:
8415 isl_multi_union_pw_aff_free(mupa);
8416 return NULL;
8419 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8420 * In particular, the spaces have been aligned.
8421 * The result is defined over the shared domain of the elements of "mupa"
8423 * We first extract the parametric constant part of "aff" and
8424 * define that over the shared domain.
8425 * Then we iterate over all input dimensions of "aff" and add the corresponding
8426 * multiples of the elements of "mupa".
8427 * Finally, we consider the integer divisions, calling the function
8428 * recursively to obtain an isl_union_pw_aff corresponding to the
8429 * integer division argument.
8431 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8432 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8434 int i, n_in, n_div;
8435 isl_union_pw_aff *upa;
8436 isl_union_set *uset;
8437 isl_val *v;
8438 isl_aff *cst;
8440 n_in = isl_aff_dim(aff, isl_dim_in);
8441 n_div = isl_aff_dim(aff, isl_dim_div);
8443 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8444 cst = isl_aff_copy(aff);
8445 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8446 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8447 cst = isl_aff_project_domain_on_params(cst);
8448 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8450 for (i = 0; i < n_in; ++i) {
8451 isl_union_pw_aff *upa_i;
8453 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8454 continue;
8455 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8456 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8457 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8458 upa = isl_union_pw_aff_add(upa, upa_i);
8461 for (i = 0; i < n_div; ++i) {
8462 isl_aff *div;
8463 isl_union_pw_aff *upa_i;
8465 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8466 continue;
8467 div = isl_aff_get_div(aff, i);
8468 upa_i = multi_union_pw_aff_apply_aff(
8469 isl_multi_union_pw_aff_copy(mupa), div);
8470 upa_i = isl_union_pw_aff_floor(upa_i);
8471 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8472 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8473 upa = isl_union_pw_aff_add(upa, upa_i);
8476 isl_multi_union_pw_aff_free(mupa);
8477 isl_aff_free(aff);
8479 return upa;
8482 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8483 * with the domain of "aff".
8484 * Furthermore, the dimension of this space needs to be greater than zero.
8485 * The result is defined over the shared domain of the elements of "mupa"
8487 * We perform these checks and then hand over control to
8488 * multi_union_pw_aff_apply_aff.
8490 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8491 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8493 isl_space *space1, *space2;
8494 int equal;
8496 mupa = isl_multi_union_pw_aff_align_params(mupa,
8497 isl_aff_get_space(aff));
8498 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8499 if (!mupa || !aff)
8500 goto error;
8502 space1 = isl_multi_union_pw_aff_get_space(mupa);
8503 space2 = isl_aff_get_domain_space(aff);
8504 equal = isl_space_is_equal(space1, space2);
8505 isl_space_free(space1);
8506 isl_space_free(space2);
8507 if (equal < 0)
8508 goto error;
8509 if (!equal)
8510 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8511 "spaces don't match", goto error);
8512 if (isl_aff_dim(aff, isl_dim_in) == 0)
8513 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8514 "cannot determine domains", goto error);
8516 return multi_union_pw_aff_apply_aff(mupa, aff);
8517 error:
8518 isl_multi_union_pw_aff_free(mupa);
8519 isl_aff_free(aff);
8520 return NULL;
8523 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8524 * with the domain of "ma".
8525 * Furthermore, the dimension of this space needs to be greater than zero,
8526 * unless the dimension of the target space of "ma" is also zero.
8527 * The result is defined over the shared domain of the elements of "mupa"
8529 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8530 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8532 isl_space *space1, *space2;
8533 isl_multi_union_pw_aff *res;
8534 int equal;
8535 int i, n_out;
8537 mupa = isl_multi_union_pw_aff_align_params(mupa,
8538 isl_multi_aff_get_space(ma));
8539 ma = isl_multi_aff_align_params(ma,
8540 isl_multi_union_pw_aff_get_space(mupa));
8541 if (!mupa || !ma)
8542 goto error;
8544 space1 = isl_multi_union_pw_aff_get_space(mupa);
8545 space2 = isl_multi_aff_get_domain_space(ma);
8546 equal = isl_space_is_equal(space1, space2);
8547 isl_space_free(space1);
8548 isl_space_free(space2);
8549 if (equal < 0)
8550 goto error;
8551 if (!equal)
8552 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8553 "spaces don't match", goto error);
8554 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8555 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8556 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8557 "cannot determine domains", goto error);
8559 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8560 res = isl_multi_union_pw_aff_alloc(space1);
8562 for (i = 0; i < n_out; ++i) {
8563 isl_aff *aff;
8564 isl_union_pw_aff *upa;
8566 aff = isl_multi_aff_get_aff(ma, i);
8567 upa = multi_union_pw_aff_apply_aff(
8568 isl_multi_union_pw_aff_copy(mupa), aff);
8569 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8572 isl_multi_aff_free(ma);
8573 isl_multi_union_pw_aff_free(mupa);
8574 return res;
8575 error:
8576 isl_multi_union_pw_aff_free(mupa);
8577 isl_multi_aff_free(ma);
8578 return NULL;
8581 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8582 * with the domain of "pa".
8583 * Furthermore, the dimension of this space needs to be greater than zero.
8584 * The result is defined over the shared domain of the elements of "mupa"
8586 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8587 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8589 int i;
8590 int equal;
8591 isl_space *space, *space2;
8592 isl_union_pw_aff *upa;
8594 mupa = isl_multi_union_pw_aff_align_params(mupa,
8595 isl_pw_aff_get_space(pa));
8596 pa = isl_pw_aff_align_params(pa,
8597 isl_multi_union_pw_aff_get_space(mupa));
8598 if (!mupa || !pa)
8599 goto error;
8601 space = isl_multi_union_pw_aff_get_space(mupa);
8602 space2 = isl_pw_aff_get_domain_space(pa);
8603 equal = isl_space_is_equal(space, space2);
8604 isl_space_free(space);
8605 isl_space_free(space2);
8606 if (equal < 0)
8607 goto error;
8608 if (!equal)
8609 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8610 "spaces don't match", goto error);
8611 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8612 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8613 "cannot determine domains", goto error);
8615 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8616 upa = isl_union_pw_aff_empty(space);
8618 for (i = 0; i < pa->n; ++i) {
8619 isl_aff *aff;
8620 isl_set *domain;
8621 isl_multi_union_pw_aff *mupa_i;
8622 isl_union_pw_aff *upa_i;
8624 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8625 domain = isl_set_copy(pa->p[i].set);
8626 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8627 aff = isl_aff_copy(pa->p[i].aff);
8628 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8629 upa = isl_union_pw_aff_union_add(upa, upa_i);
8632 isl_multi_union_pw_aff_free(mupa);
8633 isl_pw_aff_free(pa);
8634 return upa;
8635 error:
8636 isl_multi_union_pw_aff_free(mupa);
8637 isl_pw_aff_free(pa);
8638 return NULL;
8641 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8642 * with the domain of "pma".
8643 * Furthermore, the dimension of this space needs to be greater than zero,
8644 * unless the dimension of the target space of "pma" is also zero.
8645 * The result is defined over the shared domain of the elements of "mupa"
8647 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8648 __isl_take isl_multi_union_pw_aff *mupa,
8649 __isl_take isl_pw_multi_aff *pma)
8651 isl_space *space1, *space2;
8652 isl_multi_union_pw_aff *res;
8653 int equal;
8654 int i, n_out;
8656 mupa = isl_multi_union_pw_aff_align_params(mupa,
8657 isl_pw_multi_aff_get_space(pma));
8658 pma = isl_pw_multi_aff_align_params(pma,
8659 isl_multi_union_pw_aff_get_space(mupa));
8660 if (!mupa || !pma)
8661 goto error;
8663 space1 = isl_multi_union_pw_aff_get_space(mupa);
8664 space2 = isl_pw_multi_aff_get_domain_space(pma);
8665 equal = isl_space_is_equal(space1, space2);
8666 isl_space_free(space1);
8667 isl_space_free(space2);
8668 if (equal < 0)
8669 goto error;
8670 if (!equal)
8671 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8672 "spaces don't match", goto error);
8673 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8674 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8675 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8676 "cannot determine domains", goto error);
8678 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8679 res = isl_multi_union_pw_aff_alloc(space1);
8681 for (i = 0; i < n_out; ++i) {
8682 isl_pw_aff *pa;
8683 isl_union_pw_aff *upa;
8685 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8686 upa = isl_multi_union_pw_aff_apply_pw_aff(
8687 isl_multi_union_pw_aff_copy(mupa), pa);
8688 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8691 isl_pw_multi_aff_free(pma);
8692 isl_multi_union_pw_aff_free(mupa);
8693 return res;
8694 error:
8695 isl_multi_union_pw_aff_free(mupa);
8696 isl_pw_multi_aff_free(pma);
8697 return NULL;
8700 /* Compute the pullback of "mupa" by the function represented by "upma".
8701 * In other words, plug in "upma" in "mupa". The result contains
8702 * expressions defined over the domain space of "upma".
8704 * Run over all elements of "mupa" and plug in "upma" in each of them.
8706 __isl_give isl_multi_union_pw_aff *
8707 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8708 __isl_take isl_multi_union_pw_aff *mupa,
8709 __isl_take isl_union_pw_multi_aff *upma)
8711 int i, n;
8713 mupa = isl_multi_union_pw_aff_align_params(mupa,
8714 isl_union_pw_multi_aff_get_space(upma));
8715 upma = isl_union_pw_multi_aff_align_params(upma,
8716 isl_multi_union_pw_aff_get_space(mupa));
8717 if (!mupa || !upma)
8718 goto error;
8720 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8721 for (i = 0; i < n; ++i) {
8722 isl_union_pw_aff *upa;
8724 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8725 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8726 isl_union_pw_multi_aff_copy(upma));
8727 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8730 isl_union_pw_multi_aff_free(upma);
8731 return mupa;
8732 error:
8733 isl_multi_union_pw_aff_free(mupa);
8734 isl_union_pw_multi_aff_free(upma);
8735 return NULL;
8738 /* Extract the sequence of elements in "mupa" with domain space "space"
8739 * (ignoring parameters).
8741 * For the elements of "mupa" that are not defined on the specified space,
8742 * the corresponding element in the result is empty.
8744 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8745 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8747 int i, n;
8748 isl_space *space_mpa = NULL;
8749 isl_multi_pw_aff *mpa;
8751 if (!mupa || !space)
8752 goto error;
8754 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8755 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8756 space = isl_space_drop_dims(space, isl_dim_param,
8757 0, isl_space_dim(space, isl_dim_param));
8758 space = isl_space_align_params(space,
8759 isl_space_copy(space_mpa));
8760 if (!space)
8761 goto error;
8763 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8764 space_mpa);
8765 mpa = isl_multi_pw_aff_alloc(space_mpa);
8767 space = isl_space_from_domain(space);
8768 space = isl_space_add_dims(space, isl_dim_out, 1);
8769 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8770 for (i = 0; i < n; ++i) {
8771 isl_union_pw_aff *upa;
8772 isl_pw_aff *pa;
8774 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8775 pa = isl_union_pw_aff_extract_pw_aff(upa,
8776 isl_space_copy(space));
8777 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8778 isl_union_pw_aff_free(upa);
8781 isl_space_free(space);
8782 return mpa;
8783 error:
8784 isl_space_free(space_mpa);
8785 isl_space_free(space);
8786 return NULL;